/drivers/tty/serial/dz.c

http://github.com/mirrors/linux · C · 945 lines · 612 code · 141 blank · 192 comment · 91 complexity · 8c651f6485b9d78e2fd8402535422702 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dz.c: Serial port driver for DECstations equipped
  4. * with the DZ chipset.
  5. *
  6. * Copyright (C) 1998 Olivier A. D. Lebaillif
  7. *
  8. * Email: olivier.lebaillif@ifrsys.com
  9. *
  10. * Copyright (C) 2004, 2006, 2007 Maciej W. Rozycki
  11. *
  12. * [31-AUG-98] triemer
  13. * Changed IRQ to use Harald's dec internals interrupts.h
  14. * removed base_addr code - moving address assignment to setup.c
  15. * Changed name of dz_init to rs_init to be consistent with tc code
  16. * [13-NOV-98] triemer fixed code to receive characters
  17. * after patches by harald to irq code.
  18. * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
  19. * field from "current" - somewhere between 2.1.121 and 2.1.131
  20. Qua Jun 27 15:02:26 BRT 2001
  21. * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
  22. *
  23. * Parts (C) 1999 David Airlie, airlied@linux.ie
  24. * [07-SEP-99] Bugfixes
  25. *
  26. * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
  27. * Converted to new serial core
  28. */
  29. #undef DEBUG_DZ
  30. #include <linux/bitops.h>
  31. #include <linux/compiler.h>
  32. #include <linux/console.h>
  33. #include <linux/delay.h>
  34. #include <linux/errno.h>
  35. #include <linux/init.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ioport.h>
  38. #include <linux/kernel.h>
  39. #include <linux/major.h>
  40. #include <linux/module.h>
  41. #include <linux/serial.h>
  42. #include <linux/serial_core.h>
  43. #include <linux/sysrq.h>
  44. #include <linux/tty.h>
  45. #include <linux/tty_flip.h>
  46. #include <linux/atomic.h>
  47. #include <asm/bootinfo.h>
  48. #include <asm/io.h>
  49. #include <asm/dec/interrupts.h>
  50. #include <asm/dec/kn01.h>
  51. #include <asm/dec/kn02.h>
  52. #include <asm/dec/machtype.h>
  53. #include <asm/dec/prom.h>
  54. #include <asm/dec/system.h>
  55. #include "dz.h"
  56. MODULE_DESCRIPTION("DECstation DZ serial driver");
  57. MODULE_LICENSE("GPL");
  58. static char dz_name[] __initdata = "DECstation DZ serial driver version ";
  59. static char dz_version[] __initdata = "1.04";
  60. struct dz_port {
  61. struct dz_mux *mux;
  62. struct uart_port port;
  63. unsigned int cflag;
  64. };
  65. struct dz_mux {
  66. struct dz_port dport[DZ_NB_PORT];
  67. atomic_t map_guard;
  68. atomic_t irq_guard;
  69. int initialised;
  70. };
  71. static struct dz_mux dz_mux;
  72. static inline struct dz_port *to_dport(struct uart_port *uport)
  73. {
  74. return container_of(uport, struct dz_port, port);
  75. }
  76. /*
  77. * ------------------------------------------------------------
  78. * dz_in () and dz_out ()
  79. *
  80. * These routines are used to access the registers of the DZ
  81. * chip, hiding relocation differences between implementation.
  82. * ------------------------------------------------------------
  83. */
  84. static u16 dz_in(struct dz_port *dport, unsigned offset)
  85. {
  86. void __iomem *addr = dport->port.membase + offset;
  87. return readw(addr);
  88. }
  89. static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
  90. {
  91. void __iomem *addr = dport->port.membase + offset;
  92. writew(value, addr);
  93. }
  94. /*
  95. * ------------------------------------------------------------
  96. * rs_stop () and rs_start ()
  97. *
  98. * These routines are called before setting or resetting
  99. * tty->stopped. They enable or disable transmitter interrupts,
  100. * as necessary.
  101. * ------------------------------------------------------------
  102. */
  103. static void dz_stop_tx(struct uart_port *uport)
  104. {
  105. struct dz_port *dport = to_dport(uport);
  106. u16 tmp, mask = 1 << dport->port.line;
  107. tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
  108. tmp &= ~mask; /* clear the TX flag */
  109. dz_out(dport, DZ_TCR, tmp);
  110. }
  111. static void dz_start_tx(struct uart_port *uport)
  112. {
  113. struct dz_port *dport = to_dport(uport);
  114. u16 tmp, mask = 1 << dport->port.line;
  115. tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
  116. tmp |= mask; /* set the TX flag */
  117. dz_out(dport, DZ_TCR, tmp);
  118. }
  119. static void dz_stop_rx(struct uart_port *uport)
  120. {
  121. struct dz_port *dport = to_dport(uport);
  122. dport->cflag &= ~DZ_RXENAB;
  123. dz_out(dport, DZ_LPR, dport->cflag);
  124. }
  125. /*
  126. * ------------------------------------------------------------
  127. *
  128. * Here start the interrupt handling routines. All of the following
  129. * subroutines are declared as inline and are folded into
  130. * dz_interrupt. They were separated out for readability's sake.
  131. *
  132. * Note: dz_interrupt() is a "fast" interrupt, which means that it
  133. * runs with interrupts turned off. People who may want to modify
  134. * dz_interrupt() should try to keep the interrupt handler as fast as
  135. * possible. After you are done making modifications, it is not a bad
  136. * idea to do:
  137. *
  138. * make drivers/serial/dz.s
  139. *
  140. * and look at the resulting assemble code in dz.s.
  141. *
  142. * ------------------------------------------------------------
  143. */
  144. /*
  145. * ------------------------------------------------------------
  146. * receive_char ()
  147. *
  148. * This routine deals with inputs from any lines.
  149. * ------------------------------------------------------------
  150. */
  151. static inline void dz_receive_chars(struct dz_mux *mux)
  152. {
  153. struct uart_port *uport;
  154. struct dz_port *dport = &mux->dport[0];
  155. struct uart_icount *icount;
  156. int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
  157. unsigned char ch, flag;
  158. u16 status;
  159. int i;
  160. while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
  161. dport = &mux->dport[LINE(status)];
  162. uport = &dport->port;
  163. ch = UCHAR(status); /* grab the char */
  164. flag = TTY_NORMAL;
  165. icount = &uport->icount;
  166. icount->rx++;
  167. if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
  168. /*
  169. * There is no separate BREAK status bit, so treat
  170. * null characters with framing errors as BREAKs;
  171. * normally, otherwise. For this move the Framing
  172. * Error bit to a simulated BREAK bit.
  173. */
  174. if (!ch) {
  175. status |= (status & DZ_FERR) >>
  176. (ffs(DZ_FERR) - ffs(DZ_BREAK));
  177. status &= ~DZ_FERR;
  178. }
  179. /* Handle SysRq/SAK & keep track of the statistics. */
  180. if (status & DZ_BREAK) {
  181. icount->brk++;
  182. if (uart_handle_break(uport))
  183. continue;
  184. } else if (status & DZ_FERR)
  185. icount->frame++;
  186. else if (status & DZ_PERR)
  187. icount->parity++;
  188. if (status & DZ_OERR)
  189. icount->overrun++;
  190. status &= uport->read_status_mask;
  191. if (status & DZ_BREAK)
  192. flag = TTY_BREAK;
  193. else if (status & DZ_FERR)
  194. flag = TTY_FRAME;
  195. else if (status & DZ_PERR)
  196. flag = TTY_PARITY;
  197. }
  198. if (uart_handle_sysrq_char(uport, ch))
  199. continue;
  200. uart_insert_char(uport, status, DZ_OERR, ch, flag);
  201. lines_rx[LINE(status)] = 1;
  202. }
  203. for (i = 0; i < DZ_NB_PORT; i++)
  204. if (lines_rx[i])
  205. tty_flip_buffer_push(&mux->dport[i].port.state->port);
  206. }
  207. /*
  208. * ------------------------------------------------------------
  209. * transmit_char ()
  210. *
  211. * This routine deals with outputs to any lines.
  212. * ------------------------------------------------------------
  213. */
  214. static inline void dz_transmit_chars(struct dz_mux *mux)
  215. {
  216. struct dz_port *dport = &mux->dport[0];
  217. struct circ_buf *xmit;
  218. unsigned char tmp;
  219. u16 status;
  220. status = dz_in(dport, DZ_CSR);
  221. dport = &mux->dport[LINE(status)];
  222. xmit = &dport->port.state->xmit;
  223. if (dport->port.x_char) { /* XON/XOFF chars */
  224. dz_out(dport, DZ_TDR, dport->port.x_char);
  225. dport->port.icount.tx++;
  226. dport->port.x_char = 0;
  227. return;
  228. }
  229. /* If nothing to do or stopped or hardware stopped. */
  230. if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
  231. spin_lock(&dport->port.lock);
  232. dz_stop_tx(&dport->port);
  233. spin_unlock(&dport->port.lock);
  234. return;
  235. }
  236. /*
  237. * If something to do... (remember the dz has no output fifo,
  238. * so we go one char at a time) :-<
  239. */
  240. tmp = xmit->buf[xmit->tail];
  241. xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
  242. dz_out(dport, DZ_TDR, tmp);
  243. dport->port.icount.tx++;
  244. if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
  245. uart_write_wakeup(&dport->port);
  246. /* Are we are done. */
  247. if (uart_circ_empty(xmit)) {
  248. spin_lock(&dport->port.lock);
  249. dz_stop_tx(&dport->port);
  250. spin_unlock(&dport->port.lock);
  251. }
  252. }
  253. /*
  254. * ------------------------------------------------------------
  255. * check_modem_status()
  256. *
  257. * DS 3100 & 5100: Only valid for the MODEM line, duh!
  258. * DS 5000/200: Valid for the MODEM and PRINTER line.
  259. * ------------------------------------------------------------
  260. */
  261. static inline void check_modem_status(struct dz_port *dport)
  262. {
  263. /*
  264. * FIXME:
  265. * 1. No status change interrupt; use a timer.
  266. * 2. Handle the 3100/5000 as appropriate. --macro
  267. */
  268. u16 status;
  269. /* If not the modem line just return. */
  270. if (dport->port.line != DZ_MODEM)
  271. return;
  272. status = dz_in(dport, DZ_MSR);
  273. /* it's easy, since DSR2 is the only bit in the register */
  274. if (status)
  275. dport->port.icount.dsr++;
  276. }
  277. /*
  278. * ------------------------------------------------------------
  279. * dz_interrupt ()
  280. *
  281. * this is the main interrupt routine for the DZ chip.
  282. * It deals with the multiple ports.
  283. * ------------------------------------------------------------
  284. */
  285. static irqreturn_t dz_interrupt(int irq, void *dev_id)
  286. {
  287. struct dz_mux *mux = dev_id;
  288. struct dz_port *dport = &mux->dport[0];
  289. u16 status;
  290. /* get the reason why we just got an irq */
  291. status = dz_in(dport, DZ_CSR);
  292. if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
  293. dz_receive_chars(mux);
  294. if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
  295. dz_transmit_chars(mux);
  296. return IRQ_HANDLED;
  297. }
  298. /*
  299. * -------------------------------------------------------------------
  300. * Here ends the DZ interrupt routines.
  301. * -------------------------------------------------------------------
  302. */
  303. static unsigned int dz_get_mctrl(struct uart_port *uport)
  304. {
  305. /*
  306. * FIXME: Handle the 3100/5000 as appropriate. --macro
  307. */
  308. struct dz_port *dport = to_dport(uport);
  309. unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  310. if (dport->port.line == DZ_MODEM) {
  311. if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
  312. mctrl &= ~TIOCM_DSR;
  313. }
  314. return mctrl;
  315. }
  316. static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
  317. {
  318. /*
  319. * FIXME: Handle the 3100/5000 as appropriate. --macro
  320. */
  321. struct dz_port *dport = to_dport(uport);
  322. u16 tmp;
  323. if (dport->port.line == DZ_MODEM) {
  324. tmp = dz_in(dport, DZ_TCR);
  325. if (mctrl & TIOCM_DTR)
  326. tmp &= ~DZ_MODEM_DTR;
  327. else
  328. tmp |= DZ_MODEM_DTR;
  329. dz_out(dport, DZ_TCR, tmp);
  330. }
  331. }
  332. /*
  333. * -------------------------------------------------------------------
  334. * startup ()
  335. *
  336. * various initialization tasks
  337. * -------------------------------------------------------------------
  338. */
  339. static int dz_startup(struct uart_port *uport)
  340. {
  341. struct dz_port *dport = to_dport(uport);
  342. struct dz_mux *mux = dport->mux;
  343. unsigned long flags;
  344. int irq_guard;
  345. int ret;
  346. u16 tmp;
  347. irq_guard = atomic_add_return(1, &mux->irq_guard);
  348. if (irq_guard != 1)
  349. return 0;
  350. ret = request_irq(dport->port.irq, dz_interrupt,
  351. IRQF_SHARED, "dz", mux);
  352. if (ret) {
  353. atomic_add(-1, &mux->irq_guard);
  354. printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
  355. return ret;
  356. }
  357. spin_lock_irqsave(&dport->port.lock, flags);
  358. /* Enable interrupts. */
  359. tmp = dz_in(dport, DZ_CSR);
  360. tmp |= DZ_RIE | DZ_TIE;
  361. dz_out(dport, DZ_CSR, tmp);
  362. spin_unlock_irqrestore(&dport->port.lock, flags);
  363. return 0;
  364. }
  365. /*
  366. * -------------------------------------------------------------------
  367. * shutdown ()
  368. *
  369. * This routine will shutdown a serial port; interrupts are disabled, and
  370. * DTR is dropped if the hangup on close termio flag is on.
  371. * -------------------------------------------------------------------
  372. */
  373. static void dz_shutdown(struct uart_port *uport)
  374. {
  375. struct dz_port *dport = to_dport(uport);
  376. struct dz_mux *mux = dport->mux;
  377. unsigned long flags;
  378. int irq_guard;
  379. u16 tmp;
  380. spin_lock_irqsave(&dport->port.lock, flags);
  381. dz_stop_tx(&dport->port);
  382. spin_unlock_irqrestore(&dport->port.lock, flags);
  383. irq_guard = atomic_add_return(-1, &mux->irq_guard);
  384. if (!irq_guard) {
  385. /* Disable interrupts. */
  386. tmp = dz_in(dport, DZ_CSR);
  387. tmp &= ~(DZ_RIE | DZ_TIE);
  388. dz_out(dport, DZ_CSR, tmp);
  389. free_irq(dport->port.irq, mux);
  390. }
  391. }
  392. /*
  393. * -------------------------------------------------------------------
  394. * dz_tx_empty() -- get the transmitter empty status
  395. *
  396. * Purpose: Let user call ioctl() to get info when the UART physically
  397. * is emptied. On bus types like RS485, the transmitter must
  398. * release the bus after transmitting. This must be done when
  399. * the transmit shift register is empty, not be done when the
  400. * transmit holding register is empty. This functionality
  401. * allows an RS485 driver to be written in user space.
  402. * -------------------------------------------------------------------
  403. */
  404. static unsigned int dz_tx_empty(struct uart_port *uport)
  405. {
  406. struct dz_port *dport = to_dport(uport);
  407. unsigned short tmp, mask = 1 << dport->port.line;
  408. tmp = dz_in(dport, DZ_TCR);
  409. tmp &= mask;
  410. return tmp ? 0 : TIOCSER_TEMT;
  411. }
  412. static void dz_break_ctl(struct uart_port *uport, int break_state)
  413. {
  414. /*
  415. * FIXME: Can't access BREAK bits in TDR easily;
  416. * reuse the code for polled TX. --macro
  417. */
  418. struct dz_port *dport = to_dport(uport);
  419. unsigned long flags;
  420. unsigned short tmp, mask = 1 << dport->port.line;
  421. spin_lock_irqsave(&uport->lock, flags);
  422. tmp = dz_in(dport, DZ_TCR);
  423. if (break_state)
  424. tmp |= mask;
  425. else
  426. tmp &= ~mask;
  427. dz_out(dport, DZ_TCR, tmp);
  428. spin_unlock_irqrestore(&uport->lock, flags);
  429. }
  430. static int dz_encode_baud_rate(unsigned int baud)
  431. {
  432. switch (baud) {
  433. case 50:
  434. return DZ_B50;
  435. case 75:
  436. return DZ_B75;
  437. case 110:
  438. return DZ_B110;
  439. case 134:
  440. return DZ_B134;
  441. case 150:
  442. return DZ_B150;
  443. case 300:
  444. return DZ_B300;
  445. case 600:
  446. return DZ_B600;
  447. case 1200:
  448. return DZ_B1200;
  449. case 1800:
  450. return DZ_B1800;
  451. case 2000:
  452. return DZ_B2000;
  453. case 2400:
  454. return DZ_B2400;
  455. case 3600:
  456. return DZ_B3600;
  457. case 4800:
  458. return DZ_B4800;
  459. case 7200:
  460. return DZ_B7200;
  461. case 9600:
  462. return DZ_B9600;
  463. default:
  464. return -1;
  465. }
  466. }
  467. static void dz_reset(struct dz_port *dport)
  468. {
  469. struct dz_mux *mux = dport->mux;
  470. if (mux->initialised)
  471. return;
  472. dz_out(dport, DZ_CSR, DZ_CLR);
  473. while (dz_in(dport, DZ_CSR) & DZ_CLR);
  474. iob();
  475. /* Enable scanning. */
  476. dz_out(dport, DZ_CSR, DZ_MSE);
  477. mux->initialised = 1;
  478. }
  479. static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
  480. struct ktermios *old_termios)
  481. {
  482. struct dz_port *dport = to_dport(uport);
  483. unsigned long flags;
  484. unsigned int cflag, baud;
  485. int bflag;
  486. cflag = dport->port.line;
  487. switch (termios->c_cflag & CSIZE) {
  488. case CS5:
  489. cflag |= DZ_CS5;
  490. break;
  491. case CS6:
  492. cflag |= DZ_CS6;
  493. break;
  494. case CS7:
  495. cflag |= DZ_CS7;
  496. break;
  497. case CS8:
  498. default:
  499. cflag |= DZ_CS8;
  500. }
  501. if (termios->c_cflag & CSTOPB)
  502. cflag |= DZ_CSTOPB;
  503. if (termios->c_cflag & PARENB)
  504. cflag |= DZ_PARENB;
  505. if (termios->c_cflag & PARODD)
  506. cflag |= DZ_PARODD;
  507. baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
  508. bflag = dz_encode_baud_rate(baud);
  509. if (bflag < 0) { /* Try to keep unchanged. */
  510. baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
  511. bflag = dz_encode_baud_rate(baud);
  512. if (bflag < 0) { /* Resort to 9600. */
  513. baud = 9600;
  514. bflag = DZ_B9600;
  515. }
  516. tty_termios_encode_baud_rate(termios, baud, baud);
  517. }
  518. cflag |= bflag;
  519. if (termios->c_cflag & CREAD)
  520. cflag |= DZ_RXENAB;
  521. spin_lock_irqsave(&dport->port.lock, flags);
  522. uart_update_timeout(uport, termios->c_cflag, baud);
  523. dz_out(dport, DZ_LPR, cflag);
  524. dport->cflag = cflag;
  525. /* setup accept flag */
  526. dport->port.read_status_mask = DZ_OERR;
  527. if (termios->c_iflag & INPCK)
  528. dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
  529. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  530. dport->port.read_status_mask |= DZ_BREAK;
  531. /* characters to ignore */
  532. uport->ignore_status_mask = 0;
  533. if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
  534. dport->port.ignore_status_mask |= DZ_OERR;
  535. if (termios->c_iflag & IGNPAR)
  536. dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
  537. if (termios->c_iflag & IGNBRK)
  538. dport->port.ignore_status_mask |= DZ_BREAK;
  539. spin_unlock_irqrestore(&dport->port.lock, flags);
  540. }
  541. /*
  542. * Hack alert!
  543. * Required solely so that the initial PROM-based console
  544. * works undisturbed in parallel with this one.
  545. */
  546. static void dz_pm(struct uart_port *uport, unsigned int state,
  547. unsigned int oldstate)
  548. {
  549. struct dz_port *dport = to_dport(uport);
  550. unsigned long flags;
  551. spin_lock_irqsave(&dport->port.lock, flags);
  552. if (state < 3)
  553. dz_start_tx(&dport->port);
  554. else
  555. dz_stop_tx(&dport->port);
  556. spin_unlock_irqrestore(&dport->port.lock, flags);
  557. }
  558. static const char *dz_type(struct uart_port *uport)
  559. {
  560. return "DZ";
  561. }
  562. static void dz_release_port(struct uart_port *uport)
  563. {
  564. struct dz_mux *mux = to_dport(uport)->mux;
  565. int map_guard;
  566. iounmap(uport->membase);
  567. uport->membase = NULL;
  568. map_guard = atomic_add_return(-1, &mux->map_guard);
  569. if (!map_guard)
  570. release_mem_region(uport->mapbase, dec_kn_slot_size);
  571. }
  572. static int dz_map_port(struct uart_port *uport)
  573. {
  574. if (!uport->membase)
  575. uport->membase = ioremap(uport->mapbase,
  576. dec_kn_slot_size);
  577. if (!uport->membase) {
  578. printk(KERN_ERR "dz: Cannot map MMIO\n");
  579. return -ENOMEM;
  580. }
  581. return 0;
  582. }
  583. static int dz_request_port(struct uart_port *uport)
  584. {
  585. struct dz_mux *mux = to_dport(uport)->mux;
  586. int map_guard;
  587. int ret;
  588. map_guard = atomic_add_return(1, &mux->map_guard);
  589. if (map_guard == 1) {
  590. if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
  591. "dz")) {
  592. atomic_add(-1, &mux->map_guard);
  593. printk(KERN_ERR
  594. "dz: Unable to reserve MMIO resource\n");
  595. return -EBUSY;
  596. }
  597. }
  598. ret = dz_map_port(uport);
  599. if (ret) {
  600. map_guard = atomic_add_return(-1, &mux->map_guard);
  601. if (!map_guard)
  602. release_mem_region(uport->mapbase, dec_kn_slot_size);
  603. return ret;
  604. }
  605. return 0;
  606. }
  607. static void dz_config_port(struct uart_port *uport, int flags)
  608. {
  609. struct dz_port *dport = to_dport(uport);
  610. if (flags & UART_CONFIG_TYPE) {
  611. if (dz_request_port(uport))
  612. return;
  613. uport->type = PORT_DZ;
  614. dz_reset(dport);
  615. }
  616. }
  617. /*
  618. * Verify the new serial_struct (for TIOCSSERIAL).
  619. */
  620. static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
  621. {
  622. int ret = 0;
  623. if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
  624. ret = -EINVAL;
  625. if (ser->irq != uport->irq)
  626. ret = -EINVAL;
  627. return ret;
  628. }
  629. static const struct uart_ops dz_ops = {
  630. .tx_empty = dz_tx_empty,
  631. .get_mctrl = dz_get_mctrl,
  632. .set_mctrl = dz_set_mctrl,
  633. .stop_tx = dz_stop_tx,
  634. .start_tx = dz_start_tx,
  635. .stop_rx = dz_stop_rx,
  636. .break_ctl = dz_break_ctl,
  637. .startup = dz_startup,
  638. .shutdown = dz_shutdown,
  639. .set_termios = dz_set_termios,
  640. .pm = dz_pm,
  641. .type = dz_type,
  642. .release_port = dz_release_port,
  643. .request_port = dz_request_port,
  644. .config_port = dz_config_port,
  645. .verify_port = dz_verify_port,
  646. };
  647. static void __init dz_init_ports(void)
  648. {
  649. static int first = 1;
  650. unsigned long base;
  651. int line;
  652. if (!first)
  653. return;
  654. first = 0;
  655. if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
  656. base = dec_kn_slot_base + KN01_DZ11;
  657. else
  658. base = dec_kn_slot_base + KN02_DZ11;
  659. for (line = 0; line < DZ_NB_PORT; line++) {
  660. struct dz_port *dport = &dz_mux.dport[line];
  661. struct uart_port *uport = &dport->port;
  662. dport->mux = &dz_mux;
  663. uport->irq = dec_interrupt[DEC_IRQ_DZ11];
  664. uport->fifosize = 1;
  665. uport->iotype = UPIO_MEM;
  666. uport->flags = UPF_BOOT_AUTOCONF;
  667. uport->ops = &dz_ops;
  668. uport->line = line;
  669. uport->mapbase = base;
  670. uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_DZ_CONSOLE);
  671. }
  672. }
  673. #ifdef CONFIG_SERIAL_DZ_CONSOLE
  674. /*
  675. * -------------------------------------------------------------------
  676. * dz_console_putchar() -- transmit a character
  677. *
  678. * Polled transmission. This is tricky. We need to mask transmit
  679. * interrupts so that they do not interfere, enable the transmitter
  680. * for the line requested and then wait till the transmit scanner
  681. * requests data for this line. But it may request data for another
  682. * line first, in which case we have to disable its transmitter and
  683. * repeat waiting till our line pops up. Only then the character may
  684. * be transmitted. Finally, the state of the transmitter mask is
  685. * restored. Welcome to the world of PDP-11!
  686. * -------------------------------------------------------------------
  687. */
  688. static void dz_console_putchar(struct uart_port *uport, int ch)
  689. {
  690. struct dz_port *dport = to_dport(uport);
  691. unsigned long flags;
  692. unsigned short csr, tcr, trdy, mask;
  693. int loops = 10000;
  694. spin_lock_irqsave(&dport->port.lock, flags);
  695. csr = dz_in(dport, DZ_CSR);
  696. dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
  697. tcr = dz_in(dport, DZ_TCR);
  698. tcr |= 1 << dport->port.line;
  699. mask = tcr;
  700. dz_out(dport, DZ_TCR, mask);
  701. iob();
  702. spin_unlock_irqrestore(&dport->port.lock, flags);
  703. do {
  704. trdy = dz_in(dport, DZ_CSR);
  705. if (!(trdy & DZ_TRDY))
  706. continue;
  707. trdy = (trdy & DZ_TLINE) >> 8;
  708. if (trdy == dport->port.line)
  709. break;
  710. mask &= ~(1 << trdy);
  711. dz_out(dport, DZ_TCR, mask);
  712. iob();
  713. udelay(2);
  714. } while (--loops);
  715. if (loops) /* Cannot send otherwise. */
  716. dz_out(dport, DZ_TDR, ch);
  717. dz_out(dport, DZ_TCR, tcr);
  718. dz_out(dport, DZ_CSR, csr);
  719. }
  720. /*
  721. * -------------------------------------------------------------------
  722. * dz_console_print ()
  723. *
  724. * dz_console_print is registered for printk.
  725. * The console must be locked when we get here.
  726. * -------------------------------------------------------------------
  727. */
  728. static void dz_console_print(struct console *co,
  729. const char *str,
  730. unsigned int count)
  731. {
  732. struct dz_port *dport = &dz_mux.dport[co->index];
  733. #ifdef DEBUG_DZ
  734. prom_printf((char *) str);
  735. #endif
  736. uart_console_write(&dport->port, str, count, dz_console_putchar);
  737. }
  738. static int __init dz_console_setup(struct console *co, char *options)
  739. {
  740. struct dz_port *dport = &dz_mux.dport[co->index];
  741. struct uart_port *uport = &dport->port;
  742. int baud = 9600;
  743. int bits = 8;
  744. int parity = 'n';
  745. int flow = 'n';
  746. int ret;
  747. ret = dz_map_port(uport);
  748. if (ret)
  749. return ret;
  750. spin_lock_init(&dport->port.lock); /* For dz_pm(). */
  751. dz_reset(dport);
  752. dz_pm(uport, 0, -1);
  753. if (options)
  754. uart_parse_options(options, &baud, &parity, &bits, &flow);
  755. return uart_set_options(&dport->port, co, baud, parity, bits, flow);
  756. }
  757. static struct uart_driver dz_reg;
  758. static struct console dz_console = {
  759. .name = "ttyS",
  760. .write = dz_console_print,
  761. .device = uart_console_device,
  762. .setup = dz_console_setup,
  763. .flags = CON_PRINTBUFFER,
  764. .index = -1,
  765. .data = &dz_reg,
  766. };
  767. static int __init dz_serial_console_init(void)
  768. {
  769. if (!IOASIC) {
  770. dz_init_ports();
  771. register_console(&dz_console);
  772. return 0;
  773. } else
  774. return -ENXIO;
  775. }
  776. console_initcall(dz_serial_console_init);
  777. #define SERIAL_DZ_CONSOLE &dz_console
  778. #else
  779. #define SERIAL_DZ_CONSOLE NULL
  780. #endif /* CONFIG_SERIAL_DZ_CONSOLE */
  781. static struct uart_driver dz_reg = {
  782. .owner = THIS_MODULE,
  783. .driver_name = "serial",
  784. .dev_name = "ttyS",
  785. .major = TTY_MAJOR,
  786. .minor = 64,
  787. .nr = DZ_NB_PORT,
  788. .cons = SERIAL_DZ_CONSOLE,
  789. };
  790. static int __init dz_init(void)
  791. {
  792. int ret, i;
  793. if (IOASIC)
  794. return -ENXIO;
  795. printk("%s%s\n", dz_name, dz_version);
  796. dz_init_ports();
  797. ret = uart_register_driver(&dz_reg);
  798. if (ret)
  799. return ret;
  800. for (i = 0; i < DZ_NB_PORT; i++)
  801. uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
  802. return 0;
  803. }
  804. module_init(dz_init);