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 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/delay.h>
36 #include <linux/module.h>
37 #include <linux/interrupt.h>
38 #include <linux/init.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/serial_core.h>
44 #include <linux/serial.h>
46 #include <asm/bootinfo.h>
47 #include <asm/dec/interrupts.h>
48 #include <asm/dec/kn01.h>
49 #include <asm/dec/kn02.h>
50 #include <asm/dec/machtype.h>
51 #include <asm/dec/prom.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
58 static char *dz_name = "DECstation DZ serial driver version ";
59 static char *dz_version = "1.03";
62 struct uart_port port;
66 static struct dz_port dz_ports[DZ_NB_PORT];
69 * ------------------------------------------------------------
70 * dz_in () and dz_out ()
72 * These routines are used to access the registers of the DZ
73 * chip, hiding relocation differences between implementation.
74 * ------------------------------------------------------------
77 static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
79 volatile unsigned short *addr =
80 (volatile unsigned short *) (dport->port.membase + offset);
85 static inline void dz_out(struct dz_port *dport, unsigned offset,
88 volatile unsigned short *addr =
89 (volatile unsigned short *) (dport->port.membase + offset);
95 * ------------------------------------------------------------
96 * rs_stop () and rs_start ()
98 * These routines are called before setting or resetting
99 * tty->stopped. They enable or disable transmitter interrupts,
101 * ------------------------------------------------------------
104 static void dz_stop_tx(struct uart_port *uport)
106 struct dz_port *dport = (struct dz_port *)uport;
107 unsigned short tmp, mask = 1 << dport->port.line;
110 spin_lock_irqsave(&dport->port.lock, flags);
111 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
112 tmp &= ~mask; /* clear the TX flag */
113 dz_out(dport, DZ_TCR, tmp);
114 spin_unlock_irqrestore(&dport->port.lock, flags);
117 static void dz_start_tx(struct uart_port *uport)
119 struct dz_port *dport = (struct dz_port *)uport;
120 unsigned short tmp, mask = 1 << dport->port.line;
123 spin_lock_irqsave(&dport->port.lock, flags);
124 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
125 tmp |= mask; /* set the TX flag */
126 dz_out(dport, DZ_TCR, tmp);
127 spin_unlock_irqrestore(&dport->port.lock, flags);
130 static void dz_stop_rx(struct uart_port *uport)
132 struct dz_port *dport = (struct dz_port *)uport;
135 spin_lock_irqsave(&dport->port.lock, flags);
136 dport->cflag &= ~DZ_CREAD;
137 dz_out(dport, DZ_LPR, dport->cflag | dport->port.line);
138 spin_unlock_irqrestore(&dport->port.lock, flags);
141 static void dz_enable_ms(struct uart_port *port)
147 * ------------------------------------------------------------
149 * Here start the interrupt handling routines. All of the following
150 * subroutines are declared as inline and are folded into
151 * dz_interrupt. They were separated out for readability's sake.
153 * Note: dz_interrupt() is a "fast" interrupt, which means that it
154 * runs with interrupts turned off. People who may want to modify
155 * dz_interrupt() should try to keep the interrupt handler as fast as
156 * possible. After you are done making modifications, it is not a bad
159 * make drivers/serial/dz.s
161 * and look at the resulting assemble code in dz.s.
163 * ------------------------------------------------------------
167 * ------------------------------------------------------------
170 * This routine deals with inputs from any lines.
171 * ------------------------------------------------------------
173 static inline void dz_receive_chars(struct dz_port *dport_in,
174 struct pt_regs *regs)
176 struct dz_port *dport;
177 struct tty_struct *tty = NULL;
178 struct uart_icount *icount;
179 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
180 unsigned short status;
181 unsigned char ch, flag;
184 while ((status = dz_in(dport_in, DZ_RBUF)) & DZ_DVAL) {
185 dport = &dz_ports[LINE(status)];
186 tty = dport->port.info->tty; /* point to the proper dev */
188 ch = UCHAR(status); /* grab the char */
190 icount = &dport->port.icount;
194 if (status & DZ_FERR) { /* frame error */
196 * There is no separate BREAK status bit, so
197 * treat framing errors as BREAKs for Magic SysRq
198 * and SAK; normally, otherwise.
200 if (uart_handle_break(&dport->port))
202 if (dport->port.flags & UPF_SAK)
206 } else if (status & DZ_OERR) /* overrun error */
208 else if (status & DZ_PERR) /* parity error */
211 /* keep track of the statistics */
229 if (uart_handle_sysrq_char(&dport->port, ch, regs))
232 if ((status & dport->port.ignore_status_mask) == 0) {
233 uart_insert_char(&dport->port,
234 status, DZ_OERR, ch, flag);
235 lines_rx[LINE(status)] = 1;
238 for (i = 0; i < DZ_NB_PORT; i++)
240 tty_flip_buffer_push(dz_ports[i].port.info->tty);
244 * ------------------------------------------------------------
247 * This routine deals with outputs to any lines.
248 * ------------------------------------------------------------
250 static inline void dz_transmit_chars(struct dz_port *dport_in)
252 struct dz_port *dport;
253 struct circ_buf *xmit;
254 unsigned short status;
257 status = dz_in(dport_in, DZ_CSR);
258 dport = &dz_ports[LINE(status)];
259 xmit = &dport->port.info->xmit;
261 if (dport->port.x_char) { /* XON/XOFF chars */
262 dz_out(dport, DZ_TDR, dport->port.x_char);
263 dport->port.icount.tx++;
264 dport->port.x_char = 0;
267 /* If nothing to do or stopped or hardware stopped. */
268 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
269 dz_stop_tx(&dport->port);
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 dz_stop_tx(&dport->port);
291 * ------------------------------------------------------------
292 * check_modem_status()
294 * DS 3100 & 5100: Only valid for the MODEM line, duh!
295 * DS 5000/200: Valid for the MODEM and PRINTER line.
296 * ------------------------------------------------------------
298 static inline void check_modem_status(struct dz_port *dport)
302 * 1. No status change interrupt; use a timer.
303 * 2. Handle the 3100/5000 as appropriate. --macro
305 unsigned short status;
307 /* If not the modem line just return. */
308 if (dport->port.line != DZ_MODEM)
311 status = dz_in(dport, DZ_MSR);
313 /* it's easy, since DSR2 is the only bit in the register */
315 dport->port.icount.dsr++;
319 * ------------------------------------------------------------
322 * this is the main interrupt routine for the DZ chip.
323 * It deals with the multiple ports.
324 * ------------------------------------------------------------
326 static irqreturn_t dz_interrupt(int irq, void *dev)
328 struct dz_port *dport = (struct dz_port *)dev;
329 unsigned short status;
331 /* get the reason why we just got an irq */
332 status = dz_in(dport, DZ_CSR);
334 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
335 dz_receive_chars(dport, regs);
337 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
338 dz_transmit_chars(dport);
344 * -------------------------------------------------------------------
345 * Here ends the DZ interrupt routines.
346 * -------------------------------------------------------------------
349 static unsigned int dz_get_mctrl(struct uart_port *uport)
352 * FIXME: Handle the 3100/5000 as appropriate. --macro
354 struct dz_port *dport = (struct dz_port *)uport;
355 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
357 if (dport->port.line == DZ_MODEM) {
358 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
365 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
368 * FIXME: Handle the 3100/5000 as appropriate. --macro
370 struct dz_port *dport = (struct dz_port *)uport;
373 if (dport->port.line == DZ_MODEM) {
374 tmp = dz_in(dport, DZ_TCR);
375 if (mctrl & TIOCM_DTR)
376 tmp &= ~DZ_MODEM_DTR;
379 dz_out(dport, DZ_TCR, tmp);
384 * -------------------------------------------------------------------
387 * various initialization tasks
388 * -------------------------------------------------------------------
390 static int dz_startup(struct uart_port *uport)
392 struct dz_port *dport = (struct dz_port *)uport;
396 spin_lock_irqsave(&dport->port.lock, flags);
398 /* enable the interrupt and the scanning */
399 tmp = dz_in(dport, DZ_CSR);
400 tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
401 dz_out(dport, DZ_CSR, tmp);
403 spin_unlock_irqrestore(&dport->port.lock, flags);
409 * -------------------------------------------------------------------
412 * This routine will shutdown a serial port; interrupts are disabled, and
413 * DTR is dropped if the hangup on close termio flag is on.
414 * -------------------------------------------------------------------
416 static void dz_shutdown(struct uart_port *uport)
422 * -------------------------------------------------------------------
423 * dz_tx_empty() -- get the transmitter empty status
425 * Purpose: Let user call ioctl() to get info when the UART physically
426 * is emptied. On bus types like RS485, the transmitter must
427 * release the bus after transmitting. This must be done when
428 * the transmit shift register is empty, not be done when the
429 * transmit holding register is empty. This functionality
430 * allows an RS485 driver to be written in user space.
431 * -------------------------------------------------------------------
433 static unsigned int dz_tx_empty(struct uart_port *uport)
435 struct dz_port *dport = (struct dz_port *)uport;
436 unsigned short tmp, mask = 1 << dport->port.line;
438 tmp = dz_in(dport, DZ_TCR);
441 return tmp ? 0 : TIOCSER_TEMT;
444 static void dz_break_ctl(struct uart_port *uport, int break_state)
447 * FIXME: Can't access BREAK bits in TDR easily;
448 * reuse the code for polled TX. --macro
450 struct dz_port *dport = (struct dz_port *)uport;
452 unsigned short tmp, mask = 1 << dport->port.line;
454 spin_lock_irqsave(&uport->lock, flags);
455 tmp = dz_in(dport, DZ_TCR);
460 dz_out(dport, DZ_TCR, tmp);
461 spin_unlock_irqrestore(&uport->lock, flags);
464 static void dz_set_termios(struct uart_port *uport, struct termios *termios,
465 struct termios *old_termios)
467 struct dz_port *dport = (struct dz_port *)uport;
469 unsigned int cflag, baud;
471 cflag = dport->port.line;
473 switch (termios->c_cflag & CSIZE) {
488 if (termios->c_cflag & CSTOPB)
490 if (termios->c_cflag & PARENB)
492 if (termios->c_cflag & PARODD)
495 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
544 if (termios->c_cflag & CREAD)
547 spin_lock_irqsave(&dport->port.lock, flags);
549 dz_out(dport, DZ_LPR, cflag | dport->port.line);
550 dport->cflag = cflag;
552 /* setup accept flag */
553 dport->port.read_status_mask = DZ_OERR;
554 if (termios->c_iflag & INPCK)
555 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
557 /* characters to ignore */
558 uport->ignore_status_mask = 0;
559 if (termios->c_iflag & IGNPAR)
560 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
562 spin_unlock_irqrestore(&dport->port.lock, flags);
565 static const char *dz_type(struct uart_port *port)
570 static void dz_release_port(struct uart_port *port)
575 static int dz_request_port(struct uart_port *port)
580 static void dz_config_port(struct uart_port *port, int flags)
582 if (flags & UART_CONFIG_TYPE)
583 port->type = PORT_DZ;
587 * verify the new serial_struct (for TIOCSSERIAL).
589 static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
592 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
594 if (ser->irq != port->irq)
599 static struct uart_ops dz_ops = {
600 .tx_empty = dz_tx_empty,
601 .get_mctrl = dz_get_mctrl,
602 .set_mctrl = dz_set_mctrl,
603 .stop_tx = dz_stop_tx,
604 .start_tx = dz_start_tx,
605 .stop_rx = dz_stop_rx,
606 .enable_ms = dz_enable_ms,
607 .break_ctl = dz_break_ctl,
608 .startup = dz_startup,
609 .shutdown = dz_shutdown,
610 .set_termios = dz_set_termios,
612 .release_port = dz_release_port,
613 .request_port = dz_request_port,
614 .config_port = dz_config_port,
615 .verify_port = dz_verify_port,
618 static void __init dz_init_ports(void)
620 static int first = 1;
621 struct dz_port *dport;
629 if (mips_machtype == MACH_DS23100 ||
630 mips_machtype == MACH_DS5100)
631 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
633 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
635 for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
636 spin_lock_init(&dport->port.lock);
637 dport->port.membase = (char *) base;
638 dport->port.iotype = UPIO_MEM;
639 dport->port.irq = dec_interrupt[DEC_IRQ_DZ11];
640 dport->port.line = i;
641 dport->port.fifosize = 1;
642 dport->port.ops = &dz_ops;
643 dport->port.flags = UPF_BOOT_AUTOCONF;
647 static void dz_reset(struct dz_port *dport)
649 dz_out(dport, DZ_CSR, DZ_CLR);
650 while (dz_in(dport, DZ_CSR) & DZ_CLR);
653 /* enable scanning */
654 dz_out(dport, DZ_CSR, DZ_MSE);
657 #ifdef CONFIG_SERIAL_DZ_CONSOLE
659 * -------------------------------------------------------------------
660 * dz_console_putchar() -- transmit a character
662 * Polled transmission. This is tricky. We need to mask transmit
663 * interrupts so that they do not interfere, enable the transmitter
664 * for the line requested and then wait till the transmit scanner
665 * requests data for this line. But it may request data for another
666 * line first, in which case we have to disable its transmitter and
667 * repeat waiting till our line pops up. Only then the character may
668 * be transmitted. Finally, the state of the transmitter mask is
669 * restored. Welcome to the world of PDP-11!
670 * -------------------------------------------------------------------
672 static void dz_console_putchar(struct uart_port *uport, int ch)
674 struct dz_port *dport = (struct dz_port *)uport;
676 unsigned short csr, tcr, trdy, mask;
679 spin_lock_irqsave(&dport->port.lock, flags);
680 csr = dz_in(dport, DZ_CSR);
681 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
682 tcr = dz_in(dport, DZ_TCR);
683 tcr |= 1 << dport->port.line;
685 dz_out(dport, DZ_TCR, mask);
687 spin_unlock_irqrestore(&dport->port.lock, flags);
690 trdy = dz_in(dport, DZ_CSR);
691 if (!(trdy & DZ_TRDY))
693 trdy = (trdy & DZ_TLINE) >> 8;
694 if (trdy == dport->port.line)
696 mask &= ~(1 << trdy);
697 dz_out(dport, DZ_TCR, mask);
702 if (loops) /* Cannot send otherwise. */
703 dz_out(dport, DZ_TDR, ch);
705 dz_out(dport, DZ_TCR, tcr);
706 dz_out(dport, DZ_CSR, csr);
710 * -------------------------------------------------------------------
711 * dz_console_print ()
713 * dz_console_print is registered for printk.
714 * The console must be locked when we get here.
715 * -------------------------------------------------------------------
717 static void dz_console_print(struct console *co,
721 struct dz_port *dport = &dz_ports[co->index];
723 prom_printf((char *) str);
725 uart_console_write(&dport->port, str, count, dz_console_putchar);
728 static int __init dz_console_setup(struct console *co, char *options)
730 struct dz_port *dport = &dz_ports[co->index];
737 uart_parse_options(options, &baud, &parity, &bits, &flow);
741 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
744 static struct uart_driver dz_reg;
745 static struct console dz_sercons = {
747 .write = dz_console_print,
748 .device = uart_console_device,
749 .setup = dz_console_setup,
750 .flags = CON_PRINTBUFFER,
755 static int __init dz_serial_console_init(void)
759 register_console(&dz_sercons);
765 console_initcall(dz_serial_console_init);
767 #define SERIAL_DZ_CONSOLE &dz_sercons
769 #define SERIAL_DZ_CONSOLE NULL
770 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
772 static struct uart_driver dz_reg = {
773 .owner = THIS_MODULE,
774 .driver_name = "serial",
779 .cons = SERIAL_DZ_CONSOLE,
782 static int __init dz_init(void)
789 printk("%s%s\n", dz_name, dz_version);
793 #ifndef CONFIG_SERIAL_DZ_CONSOLE
795 dz_reset(&dz_ports[0]);
798 if (request_irq(dz_ports[0].port.irq, dz_interrupt,
799 IRQF_DISABLED, "DZ", &dz_ports[0]))
800 panic("Unable to register DZ interrupt");
802 ret = uart_register_driver(&dz_reg);
806 for (i = 0; i < DZ_NB_PORT; i++)
807 uart_add_one_port(&dz_reg, &dz_ports[i].port);
812 module_init(dz_init);
814 MODULE_DESCRIPTION("DECstation DZ serial driver");
815 MODULE_LICENSE("GPL");