2 * dz.c: Serial port driver for DECstations equipped
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
7 * Email: olivier.lebaillif@ifrsys.com
9 * Copyright (C) 2004, 2006, 2007 Maciej W. Rozycki
12 * Changed IRQ to use Harald's dec internals interrupts.h
13 * removed base_addr code - moving address assignment to setup.c
14 * Changed name of dz_init to rs_init to be consistent with tc code
15 * [13-NOV-98] triemer fixed code to receive characters
16 * after patches by harald to irq code.
17 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
18 * field from "current" - somewhere between 2.1.121 and 2.1.131
19 Qua Jun 27 15:02:26 BRT 2001
20 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
22 * Parts (C) 1999 David Airlie, airlied@linux.ie
23 * [07-SEP-99] Bugfixes
25 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
26 * Converted to new serial core
31 #if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
35 #include <linux/bitops.h>
36 #include <linux/compiler.h>
37 #include <linux/console.h>
38 #include <linux/delay.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/kernel.h>
43 #include <linux/major.h>
44 #include <linux/module.h>
45 #include <linux/serial.h>
46 #include <linux/serial_core.h>
47 #include <linux/sysrq.h>
48 #include <linux/tty.h>
50 #include <asm/bootinfo.h>
51 #include <asm/system.h>
53 #include <asm/dec/interrupts.h>
54 #include <asm/dec/kn01.h>
55 #include <asm/dec/kn02.h>
56 #include <asm/dec/machtype.h>
57 #include <asm/dec/prom.h>
61 static char *dz_name = "DECstation DZ serial driver version ";
62 static char *dz_version = "1.03";
65 struct uart_port port;
69 static struct dz_port dz_ports[DZ_NB_PORT];
71 static inline struct dz_port *to_dport(struct uart_port *uport)
73 return container_of(uport, struct dz_port, port);
77 * ------------------------------------------------------------
78 * dz_in () and dz_out ()
80 * These routines are used to access the registers of the DZ
81 * chip, hiding relocation differences between implementation.
82 * ------------------------------------------------------------
85 static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
87 volatile unsigned short *addr =
88 (volatile unsigned short *) (dport->port.membase + offset);
93 static inline void dz_out(struct dz_port *dport, unsigned offset,
96 volatile unsigned short *addr =
97 (volatile unsigned short *) (dport->port.membase + offset);
103 * ------------------------------------------------------------
104 * rs_stop () and rs_start ()
106 * These routines are called before setting or resetting
107 * tty->stopped. They enable or disable transmitter interrupts,
109 * ------------------------------------------------------------
112 static void dz_stop_tx(struct uart_port *uport)
114 struct dz_port *dport = to_dport(uport);
115 unsigned short tmp, mask = 1 << dport->port.line;
117 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
118 tmp &= ~mask; /* clear the TX flag */
119 dz_out(dport, DZ_TCR, tmp);
122 static void dz_start_tx(struct uart_port *uport)
124 struct dz_port *dport = to_dport(uport);
125 unsigned short tmp, mask = 1 << dport->port.line;
127 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
128 tmp |= mask; /* set the TX flag */
129 dz_out(dport, DZ_TCR, tmp);
132 static void dz_stop_rx(struct uart_port *uport)
134 struct dz_port *dport = to_dport(uport);
136 dport->cflag &= ~DZ_RXENAB;
137 dz_out(dport, DZ_LPR, dport->cflag);
140 static void dz_enable_ms(struct uart_port *port)
146 * ------------------------------------------------------------
148 * Here start the interrupt handling routines. All of the following
149 * subroutines are declared as inline and are folded into
150 * dz_interrupt. They were separated out for readability's sake.
152 * Note: dz_interrupt() is a "fast" interrupt, which means that it
153 * runs with interrupts turned off. People who may want to modify
154 * dz_interrupt() should try to keep the interrupt handler as fast as
155 * possible. After you are done making modifications, it is not a bad
158 * make drivers/serial/dz.s
160 * and look at the resulting assemble code in dz.s.
162 * ------------------------------------------------------------
166 * ------------------------------------------------------------
169 * This routine deals with inputs from any lines.
170 * ------------------------------------------------------------
172 static inline void dz_receive_chars(struct dz_port *dport_in)
174 struct uart_port *uport;
175 struct dz_port *dport;
176 struct tty_struct *tty = NULL;
177 struct uart_icount *icount;
178 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
179 unsigned short status;
180 unsigned char ch, flag;
183 while ((status = dz_in(dport_in, DZ_RBUF)) & DZ_DVAL) {
184 dport = &dz_ports[LINE(status)];
185 uport = &dport->port;
186 tty = uport->info->tty; /* point to the proper dev */
188 ch = UCHAR(status); /* grab the char */
191 icount = &uport->icount;
194 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
197 * There is no separate BREAK status bit, so treat
198 * null characters with framing errors as BREAKs;
199 * normally, otherwise. For this move the Framing
200 * Error bit to a simulated BREAK bit.
203 status |= (status & DZ_FERR) >>
204 (ffs(DZ_FERR) - ffs(DZ_BREAK));
208 /* Handle SysRq/SAK & keep track of the statistics. */
209 if (status & DZ_BREAK) {
211 if (uart_handle_break(uport))
213 } else if (status & DZ_FERR)
215 else if (status & DZ_PERR)
217 if (status & DZ_OERR)
220 status &= uport->read_status_mask;
221 if (status & DZ_BREAK)
223 else if (status & DZ_FERR)
225 else if (status & DZ_PERR)
230 if (uart_handle_sysrq_char(uport, ch))
233 uart_insert_char(uport, status, DZ_OERR, ch, flag);
234 lines_rx[LINE(status)] = 1;
236 for (i = 0; i < DZ_NB_PORT; i++)
238 tty_flip_buffer_push(dz_ports[i].port.info->tty);
242 * ------------------------------------------------------------
245 * This routine deals with outputs to any lines.
246 * ------------------------------------------------------------
248 static inline void dz_transmit_chars(struct dz_port *dport_in)
250 struct dz_port *dport;
251 struct circ_buf *xmit;
252 unsigned short status;
255 status = dz_in(dport_in, DZ_CSR);
256 dport = &dz_ports[LINE(status)];
257 xmit = &dport->port.info->xmit;
259 if (dport->port.x_char) { /* XON/XOFF chars */
260 dz_out(dport, DZ_TDR, dport->port.x_char);
261 dport->port.icount.tx++;
262 dport->port.x_char = 0;
265 /* If nothing to do or stopped or hardware stopped. */
266 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
267 spin_lock(&dport->port.lock);
268 dz_stop_tx(&dport->port);
269 spin_unlock(&dport->port.lock);
274 * If something to do... (remember the dz has no output fifo,
275 * so we go one char at a time) :-<
277 tmp = xmit->buf[xmit->tail];
278 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
279 dz_out(dport, DZ_TDR, tmp);
280 dport->port.icount.tx++;
282 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
283 uart_write_wakeup(&dport->port);
285 /* Are we are done. */
286 if (uart_circ_empty(xmit)) {
287 spin_lock(&dport->port.lock);
288 dz_stop_tx(&dport->port);
289 spin_unlock(&dport->port.lock);
294 * ------------------------------------------------------------
295 * check_modem_status()
297 * DS 3100 & 5100: Only valid for the MODEM line, duh!
298 * DS 5000/200: Valid for the MODEM and PRINTER line.
299 * ------------------------------------------------------------
301 static inline void check_modem_status(struct dz_port *dport)
305 * 1. No status change interrupt; use a timer.
306 * 2. Handle the 3100/5000 as appropriate. --macro
308 unsigned short status;
310 /* If not the modem line just return. */
311 if (dport->port.line != DZ_MODEM)
314 status = dz_in(dport, DZ_MSR);
316 /* it's easy, since DSR2 is the only bit in the register */
318 dport->port.icount.dsr++;
322 * ------------------------------------------------------------
325 * this is the main interrupt routine for the DZ chip.
326 * It deals with the multiple ports.
327 * ------------------------------------------------------------
329 static irqreturn_t dz_interrupt(int irq, void *dev)
331 struct dz_port *dport = dev;
332 unsigned short status;
334 /* get the reason why we just got an irq */
335 status = dz_in(dport, DZ_CSR);
337 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
338 dz_receive_chars(dport);
340 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
341 dz_transmit_chars(dport);
347 * -------------------------------------------------------------------
348 * Here ends the DZ interrupt routines.
349 * -------------------------------------------------------------------
352 static unsigned int dz_get_mctrl(struct uart_port *uport)
355 * FIXME: Handle the 3100/5000 as appropriate. --macro
357 struct dz_port *dport = to_dport(uport);
358 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
360 if (dport->port.line == DZ_MODEM) {
361 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
368 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
371 * FIXME: Handle the 3100/5000 as appropriate. --macro
373 struct dz_port *dport = to_dport(uport);
376 if (dport->port.line == DZ_MODEM) {
377 tmp = dz_in(dport, DZ_TCR);
378 if (mctrl & TIOCM_DTR)
379 tmp &= ~DZ_MODEM_DTR;
382 dz_out(dport, DZ_TCR, tmp);
387 * -------------------------------------------------------------------
390 * various initialization tasks
391 * -------------------------------------------------------------------
393 static int dz_startup(struct uart_port *uport)
395 struct dz_port *dport = to_dport(uport);
399 spin_lock_irqsave(&dport->port.lock, flags);
401 /* enable the interrupt and the scanning */
402 tmp = dz_in(dport, DZ_CSR);
403 tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
404 dz_out(dport, DZ_CSR, tmp);
406 spin_unlock_irqrestore(&dport->port.lock, flags);
412 * -------------------------------------------------------------------
415 * This routine will shutdown a serial port; interrupts are disabled, and
416 * DTR is dropped if the hangup on close termio flag is on.
417 * -------------------------------------------------------------------
419 static void dz_shutdown(struct uart_port *uport)
421 struct dz_port *dport = to_dport(uport);
424 spin_lock_irqsave(&dport->port.lock, flags);
425 dz_stop_tx(&dport->port);
426 spin_unlock_irqrestore(&dport->port.lock, flags);
430 * -------------------------------------------------------------------
431 * dz_tx_empty() -- get the transmitter empty status
433 * Purpose: Let user call ioctl() to get info when the UART physically
434 * is emptied. On bus types like RS485, the transmitter must
435 * release the bus after transmitting. This must be done when
436 * the transmit shift register is empty, not be done when the
437 * transmit holding register is empty. This functionality
438 * allows an RS485 driver to be written in user space.
439 * -------------------------------------------------------------------
441 static unsigned int dz_tx_empty(struct uart_port *uport)
443 struct dz_port *dport = to_dport(uport);
444 unsigned short tmp, mask = 1 << dport->port.line;
446 tmp = dz_in(dport, DZ_TCR);
449 return tmp ? 0 : TIOCSER_TEMT;
452 static void dz_break_ctl(struct uart_port *uport, int break_state)
455 * FIXME: Can't access BREAK bits in TDR easily;
456 * reuse the code for polled TX. --macro
458 struct dz_port *dport = to_dport(uport);
460 unsigned short tmp, mask = 1 << dport->port.line;
462 spin_lock_irqsave(&uport->lock, flags);
463 tmp = dz_in(dport, DZ_TCR);
468 dz_out(dport, DZ_TCR, tmp);
469 spin_unlock_irqrestore(&uport->lock, flags);
472 static int dz_encode_baud_rate(unsigned int baud)
510 static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
511 struct ktermios *old_termios)
513 struct dz_port *dport = to_dport(uport);
515 unsigned int cflag, baud;
518 cflag = dport->port.line;
520 switch (termios->c_cflag & CSIZE) {
535 if (termios->c_cflag & CSTOPB)
537 if (termios->c_cflag & PARENB)
539 if (termios->c_cflag & PARODD)
542 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
543 bflag = dz_encode_baud_rate(baud);
544 if (bflag < 0) { /* Try to keep unchanged. */
545 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
546 bflag = dz_encode_baud_rate(baud);
547 if (bflag < 0) { /* Resort to 9600. */
551 tty_termios_encode_baud_rate(termios, baud, baud);
555 if (termios->c_cflag & CREAD)
558 spin_lock_irqsave(&dport->port.lock, flags);
560 uart_update_timeout(uport, termios->c_cflag, baud);
562 dz_out(dport, DZ_LPR, cflag);
563 dport->cflag = cflag;
565 /* setup accept flag */
566 dport->port.read_status_mask = DZ_OERR;
567 if (termios->c_iflag & INPCK)
568 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
569 if (termios->c_iflag & (BRKINT | PARMRK))
570 dport->port.read_status_mask |= DZ_BREAK;
572 /* characters to ignore */
573 uport->ignore_status_mask = 0;
574 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
575 dport->port.ignore_status_mask |= DZ_OERR;
576 if (termios->c_iflag & IGNPAR)
577 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
578 if (termios->c_iflag & IGNBRK)
579 dport->port.ignore_status_mask |= DZ_BREAK;
581 spin_unlock_irqrestore(&dport->port.lock, flags);
584 static const char *dz_type(struct uart_port *port)
589 static void dz_release_port(struct uart_port *port)
594 static int dz_request_port(struct uart_port *port)
599 static void dz_config_port(struct uart_port *port, int flags)
601 if (flags & UART_CONFIG_TYPE)
602 port->type = PORT_DZ;
606 * verify the new serial_struct (for TIOCSSERIAL).
608 static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
611 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
613 if (ser->irq != port->irq)
618 static struct uart_ops dz_ops = {
619 .tx_empty = dz_tx_empty,
620 .get_mctrl = dz_get_mctrl,
621 .set_mctrl = dz_set_mctrl,
622 .stop_tx = dz_stop_tx,
623 .start_tx = dz_start_tx,
624 .stop_rx = dz_stop_rx,
625 .enable_ms = dz_enable_ms,
626 .break_ctl = dz_break_ctl,
627 .startup = dz_startup,
628 .shutdown = dz_shutdown,
629 .set_termios = dz_set_termios,
631 .release_port = dz_release_port,
632 .request_port = dz_request_port,
633 .config_port = dz_config_port,
634 .verify_port = dz_verify_port,
637 static void __init dz_init_ports(void)
639 static int first = 1;
640 struct dz_port *dport;
648 if (mips_machtype == MACH_DS23100 ||
649 mips_machtype == MACH_DS5100)
650 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
652 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
654 for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
655 spin_lock_init(&dport->port.lock);
656 dport->port.membase = (char *) base;
657 dport->port.iotype = UPIO_MEM;
658 dport->port.irq = dec_interrupt[DEC_IRQ_DZ11];
659 dport->port.line = i;
660 dport->port.fifosize = 1;
661 dport->port.ops = &dz_ops;
662 dport->port.flags = UPF_BOOT_AUTOCONF;
666 static void dz_reset(struct dz_port *dport)
668 dz_out(dport, DZ_CSR, DZ_CLR);
669 while (dz_in(dport, DZ_CSR) & DZ_CLR);
672 /* enable scanning */
673 dz_out(dport, DZ_CSR, DZ_MSE);
676 #ifdef CONFIG_SERIAL_DZ_CONSOLE
678 * -------------------------------------------------------------------
679 * dz_console_putchar() -- transmit a character
681 * Polled transmission. This is tricky. We need to mask transmit
682 * interrupts so that they do not interfere, enable the transmitter
683 * for the line requested and then wait till the transmit scanner
684 * requests data for this line. But it may request data for another
685 * line first, in which case we have to disable its transmitter and
686 * repeat waiting till our line pops up. Only then the character may
687 * be transmitted. Finally, the state of the transmitter mask is
688 * restored. Welcome to the world of PDP-11!
689 * -------------------------------------------------------------------
691 static void dz_console_putchar(struct uart_port *uport, int ch)
693 struct dz_port *dport = to_dport(uport);
695 unsigned short csr, tcr, trdy, mask;
698 spin_lock_irqsave(&dport->port.lock, flags);
699 csr = dz_in(dport, DZ_CSR);
700 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
701 tcr = dz_in(dport, DZ_TCR);
702 tcr |= 1 << dport->port.line;
704 dz_out(dport, DZ_TCR, mask);
706 spin_unlock_irqrestore(&dport->port.lock, flags);
709 trdy = dz_in(dport, DZ_CSR);
710 if (!(trdy & DZ_TRDY))
712 trdy = (trdy & DZ_TLINE) >> 8;
713 if (trdy == dport->port.line)
715 mask &= ~(1 << trdy);
716 dz_out(dport, DZ_TCR, mask);
721 if (loops) /* Cannot send otherwise. */
722 dz_out(dport, DZ_TDR, ch);
724 dz_out(dport, DZ_TCR, tcr);
725 dz_out(dport, DZ_CSR, csr);
729 * -------------------------------------------------------------------
730 * dz_console_print ()
732 * dz_console_print is registered for printk.
733 * The console must be locked when we get here.
734 * -------------------------------------------------------------------
736 static void dz_console_print(struct console *co,
740 struct dz_port *dport = &dz_ports[co->index];
742 prom_printf((char *) str);
744 uart_console_write(&dport->port, str, count, dz_console_putchar);
747 static int __init dz_console_setup(struct console *co, char *options)
749 struct dz_port *dport = &dz_ports[co->index];
756 uart_parse_options(options, &baud, &parity, &bits, &flow);
760 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
763 static struct uart_driver dz_reg;
764 static struct console dz_console = {
766 .write = dz_console_print,
767 .device = uart_console_device,
768 .setup = dz_console_setup,
769 .flags = CON_PRINTBUFFER,
774 static int __init dz_serial_console_init(void)
778 register_console(&dz_console);
784 console_initcall(dz_serial_console_init);
786 #define SERIAL_DZ_CONSOLE &dz_console
788 #define SERIAL_DZ_CONSOLE NULL
789 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
791 static struct uart_driver dz_reg = {
792 .owner = THIS_MODULE,
793 .driver_name = "serial",
798 .cons = SERIAL_DZ_CONSOLE,
801 static int __init dz_init(void)
808 printk("%s%s\n", dz_name, dz_version);
812 #ifndef CONFIG_SERIAL_DZ_CONSOLE
814 dz_reset(&dz_ports[0]);
817 ret = uart_register_driver(&dz_reg);
821 ret = request_irq(dz_ports[0].port.irq, dz_interrupt, IRQF_DISABLED,
824 printk(KERN_ERR "dz: Cannot get IRQ %d!\n",
825 dz_ports[0].port.irq);
829 for (i = 0; i < DZ_NB_PORT; i++)
830 uart_add_one_port(&dz_reg, &dz_ports[i].port);
835 uart_unregister_driver(&dz_reg);
841 module_init(dz_init);
843 MODULE_DESCRIPTION("DECstation DZ serial driver");
844 MODULE_LICENSE("GPL");