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];
72 * ------------------------------------------------------------
73 * dz_in () and dz_out ()
75 * These routines are used to access the registers of the DZ
76 * chip, hiding relocation differences between implementation.
77 * ------------------------------------------------------------
80 static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
82 volatile unsigned short *addr =
83 (volatile unsigned short *) (dport->port.membase + offset);
88 static inline void dz_out(struct dz_port *dport, unsigned offset,
91 volatile unsigned short *addr =
92 (volatile unsigned short *) (dport->port.membase + offset);
98 * ------------------------------------------------------------
99 * rs_stop () and rs_start ()
101 * These routines are called before setting or resetting
102 * tty->stopped. They enable or disable transmitter interrupts,
104 * ------------------------------------------------------------
107 static void dz_stop_tx(struct uart_port *uport)
109 struct dz_port *dport = (struct dz_port *)uport;
110 unsigned short tmp, mask = 1 << dport->port.line;
112 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
113 tmp &= ~mask; /* clear the TX flag */
114 dz_out(dport, DZ_TCR, tmp);
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;
122 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
123 tmp |= mask; /* set the TX flag */
124 dz_out(dport, DZ_TCR, tmp);
127 static void dz_stop_rx(struct uart_port *uport)
129 struct dz_port *dport = (struct dz_port *)uport;
131 dport->cflag &= ~DZ_RXENAB;
132 dz_out(dport, DZ_LPR, dport->cflag);
135 static void dz_enable_ms(struct uart_port *port)
141 * ------------------------------------------------------------
143 * Here start the interrupt handling routines. All of the following
144 * subroutines are declared as inline and are folded into
145 * dz_interrupt. They were separated out for readability's sake.
147 * Note: dz_interrupt() is a "fast" interrupt, which means that it
148 * runs with interrupts turned off. People who may want to modify
149 * dz_interrupt() should try to keep the interrupt handler as fast as
150 * possible. After you are done making modifications, it is not a bad
153 * make drivers/serial/dz.s
155 * and look at the resulting assemble code in dz.s.
157 * ------------------------------------------------------------
161 * ------------------------------------------------------------
164 * This routine deals with inputs from any lines.
165 * ------------------------------------------------------------
167 static inline void dz_receive_chars(struct dz_port *dport_in)
169 struct uart_port *uport;
170 struct dz_port *dport;
171 struct tty_struct *tty = NULL;
172 struct uart_icount *icount;
173 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
174 unsigned short status;
175 unsigned char ch, flag;
178 while ((status = dz_in(dport_in, DZ_RBUF)) & DZ_DVAL) {
179 dport = &dz_ports[LINE(status)];
180 uport = &dport->port;
181 tty = uport->info->tty; /* point to the proper dev */
183 ch = UCHAR(status); /* grab the char */
186 icount = &uport->icount;
189 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
192 * There is no separate BREAK status bit, so treat
193 * null characters with framing errors as BREAKs;
194 * normally, otherwise. For this move the Framing
195 * Error bit to a simulated BREAK bit.
198 status |= (status & DZ_FERR) >>
199 (ffs(DZ_FERR) - ffs(DZ_BREAK));
203 /* Handle SysRq/SAK & keep track of the statistics. */
204 if (status & DZ_BREAK) {
206 if (uart_handle_break(uport))
208 } else if (status & DZ_FERR)
210 else if (status & DZ_PERR)
212 if (status & DZ_OERR)
215 status &= uport->read_status_mask;
216 if (status & DZ_BREAK)
218 else if (status & DZ_FERR)
220 else if (status & DZ_PERR)
225 if (uart_handle_sysrq_char(uport, ch))
228 uart_insert_char(uport, status, DZ_OERR, ch, flag);
229 lines_rx[LINE(status)] = 1;
231 for (i = 0; i < DZ_NB_PORT; i++)
233 tty_flip_buffer_push(dz_ports[i].port.info->tty);
237 * ------------------------------------------------------------
240 * This routine deals with outputs to any lines.
241 * ------------------------------------------------------------
243 static inline void dz_transmit_chars(struct dz_port *dport_in)
245 struct dz_port *dport;
246 struct circ_buf *xmit;
247 unsigned short status;
250 status = dz_in(dport_in, DZ_CSR);
251 dport = &dz_ports[LINE(status)];
252 xmit = &dport->port.info->xmit;
254 if (dport->port.x_char) { /* XON/XOFF chars */
255 dz_out(dport, DZ_TDR, dport->port.x_char);
256 dport->port.icount.tx++;
257 dport->port.x_char = 0;
260 /* If nothing to do or stopped or hardware stopped. */
261 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
262 spin_lock(&dport->port.lock);
263 dz_stop_tx(&dport->port);
264 spin_unlock(&dport->port.lock);
269 * If something to do... (remember the dz has no output fifo,
270 * so we go one char at a time) :-<
272 tmp = xmit->buf[xmit->tail];
273 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
274 dz_out(dport, DZ_TDR, tmp);
275 dport->port.icount.tx++;
277 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
278 uart_write_wakeup(&dport->port);
280 /* Are we are done. */
281 if (uart_circ_empty(xmit)) {
282 spin_lock(&dport->port.lock);
283 dz_stop_tx(&dport->port);
284 spin_unlock(&dport->port.lock);
289 * ------------------------------------------------------------
290 * check_modem_status()
292 * DS 3100 & 5100: Only valid for the MODEM line, duh!
293 * DS 5000/200: Valid for the MODEM and PRINTER line.
294 * ------------------------------------------------------------
296 static inline void check_modem_status(struct dz_port *dport)
300 * 1. No status change interrupt; use a timer.
301 * 2. Handle the 3100/5000 as appropriate. --macro
303 unsigned short status;
305 /* If not the modem line just return. */
306 if (dport->port.line != DZ_MODEM)
309 status = dz_in(dport, DZ_MSR);
311 /* it's easy, since DSR2 is the only bit in the register */
313 dport->port.icount.dsr++;
317 * ------------------------------------------------------------
320 * this is the main interrupt routine for the DZ chip.
321 * It deals with the multiple ports.
322 * ------------------------------------------------------------
324 static irqreturn_t dz_interrupt(int irq, void *dev)
326 struct dz_port *dport = dev;
327 unsigned short status;
329 /* get the reason why we just got an irq */
330 status = dz_in(dport, DZ_CSR);
332 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
333 dz_receive_chars(dport);
335 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
336 dz_transmit_chars(dport);
342 * -------------------------------------------------------------------
343 * Here ends the DZ interrupt routines.
344 * -------------------------------------------------------------------
347 static unsigned int dz_get_mctrl(struct uart_port *uport)
350 * FIXME: Handle the 3100/5000 as appropriate. --macro
352 struct dz_port *dport = (struct dz_port *)uport;
353 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
355 if (dport->port.line == DZ_MODEM) {
356 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
363 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
366 * FIXME: Handle the 3100/5000 as appropriate. --macro
368 struct dz_port *dport = (struct dz_port *)uport;
371 if (dport->port.line == DZ_MODEM) {
372 tmp = dz_in(dport, DZ_TCR);
373 if (mctrl & TIOCM_DTR)
374 tmp &= ~DZ_MODEM_DTR;
377 dz_out(dport, DZ_TCR, tmp);
382 * -------------------------------------------------------------------
385 * various initialization tasks
386 * -------------------------------------------------------------------
388 static int dz_startup(struct uart_port *uport)
390 struct dz_port *dport = (struct dz_port *)uport;
394 spin_lock_irqsave(&dport->port.lock, flags);
396 /* enable the interrupt and the scanning */
397 tmp = dz_in(dport, DZ_CSR);
398 tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
399 dz_out(dport, DZ_CSR, tmp);
401 spin_unlock_irqrestore(&dport->port.lock, flags);
407 * -------------------------------------------------------------------
410 * This routine will shutdown a serial port; interrupts are disabled, and
411 * DTR is dropped if the hangup on close termio flag is on.
412 * -------------------------------------------------------------------
414 static void dz_shutdown(struct uart_port *uport)
416 struct dz_port *dport = (struct dz_port *)uport;
419 spin_lock_irqsave(&dport->port.lock, flags);
420 dz_stop_tx(&dport->port);
421 spin_unlock_irqrestore(&dport->port.lock, flags);
425 * -------------------------------------------------------------------
426 * dz_tx_empty() -- get the transmitter empty status
428 * Purpose: Let user call ioctl() to get info when the UART physically
429 * is emptied. On bus types like RS485, the transmitter must
430 * release the bus after transmitting. This must be done when
431 * the transmit shift register is empty, not be done when the
432 * transmit holding register is empty. This functionality
433 * allows an RS485 driver to be written in user space.
434 * -------------------------------------------------------------------
436 static unsigned int dz_tx_empty(struct uart_port *uport)
438 struct dz_port *dport = (struct dz_port *)uport;
439 unsigned short tmp, mask = 1 << dport->port.line;
441 tmp = dz_in(dport, DZ_TCR);
444 return tmp ? 0 : TIOCSER_TEMT;
447 static void dz_break_ctl(struct uart_port *uport, int break_state)
450 * FIXME: Can't access BREAK bits in TDR easily;
451 * reuse the code for polled TX. --macro
453 struct dz_port *dport = (struct dz_port *)uport;
455 unsigned short tmp, mask = 1 << dport->port.line;
457 spin_lock_irqsave(&uport->lock, flags);
458 tmp = dz_in(dport, DZ_TCR);
463 dz_out(dport, DZ_TCR, tmp);
464 spin_unlock_irqrestore(&uport->lock, flags);
467 static int dz_encode_baud_rate(unsigned int baud)
505 static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
506 struct ktermios *old_termios)
508 struct dz_port *dport = (struct dz_port *)uport;
510 unsigned int cflag, baud;
513 cflag = dport->port.line;
515 switch (termios->c_cflag & CSIZE) {
530 if (termios->c_cflag & CSTOPB)
532 if (termios->c_cflag & PARENB)
534 if (termios->c_cflag & PARODD)
537 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
538 bflag = dz_encode_baud_rate(baud);
539 if (bflag < 0) { /* Try to keep unchanged. */
540 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
541 bflag = dz_encode_baud_rate(baud);
542 if (bflag < 0) { /* Resort to 9600. */
546 tty_termios_encode_baud_rate(termios, baud, baud);
550 if (termios->c_cflag & CREAD)
553 spin_lock_irqsave(&dport->port.lock, flags);
555 uart_update_timeout(uport, termios->c_cflag, baud);
557 dz_out(dport, DZ_LPR, cflag);
558 dport->cflag = cflag;
560 /* setup accept flag */
561 dport->port.read_status_mask = DZ_OERR;
562 if (termios->c_iflag & INPCK)
563 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
564 if (termios->c_iflag & (BRKINT | PARMRK))
565 dport->port.read_status_mask |= DZ_BREAK;
567 /* characters to ignore */
568 uport->ignore_status_mask = 0;
569 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
570 dport->port.ignore_status_mask |= DZ_OERR;
571 if (termios->c_iflag & IGNPAR)
572 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
573 if (termios->c_iflag & IGNBRK)
574 dport->port.ignore_status_mask |= DZ_BREAK;
576 spin_unlock_irqrestore(&dport->port.lock, flags);
579 static const char *dz_type(struct uart_port *port)
584 static void dz_release_port(struct uart_port *port)
589 static int dz_request_port(struct uart_port *port)
594 static void dz_config_port(struct uart_port *port, int flags)
596 if (flags & UART_CONFIG_TYPE)
597 port->type = PORT_DZ;
601 * verify the new serial_struct (for TIOCSSERIAL).
603 static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
606 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
608 if (ser->irq != port->irq)
613 static struct uart_ops dz_ops = {
614 .tx_empty = dz_tx_empty,
615 .get_mctrl = dz_get_mctrl,
616 .set_mctrl = dz_set_mctrl,
617 .stop_tx = dz_stop_tx,
618 .start_tx = dz_start_tx,
619 .stop_rx = dz_stop_rx,
620 .enable_ms = dz_enable_ms,
621 .break_ctl = dz_break_ctl,
622 .startup = dz_startup,
623 .shutdown = dz_shutdown,
624 .set_termios = dz_set_termios,
626 .release_port = dz_release_port,
627 .request_port = dz_request_port,
628 .config_port = dz_config_port,
629 .verify_port = dz_verify_port,
632 static void __init dz_init_ports(void)
634 static int first = 1;
635 struct dz_port *dport;
643 if (mips_machtype == MACH_DS23100 ||
644 mips_machtype == MACH_DS5100)
645 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
647 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
649 for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
650 spin_lock_init(&dport->port.lock);
651 dport->port.membase = (char *) base;
652 dport->port.iotype = UPIO_MEM;
653 dport->port.irq = dec_interrupt[DEC_IRQ_DZ11];
654 dport->port.line = i;
655 dport->port.fifosize = 1;
656 dport->port.ops = &dz_ops;
657 dport->port.flags = UPF_BOOT_AUTOCONF;
661 static void dz_reset(struct dz_port *dport)
663 dz_out(dport, DZ_CSR, DZ_CLR);
664 while (dz_in(dport, DZ_CSR) & DZ_CLR);
667 /* enable scanning */
668 dz_out(dport, DZ_CSR, DZ_MSE);
671 #ifdef CONFIG_SERIAL_DZ_CONSOLE
673 * -------------------------------------------------------------------
674 * dz_console_putchar() -- transmit a character
676 * Polled transmission. This is tricky. We need to mask transmit
677 * interrupts so that they do not interfere, enable the transmitter
678 * for the line requested and then wait till the transmit scanner
679 * requests data for this line. But it may request data for another
680 * line first, in which case we have to disable its transmitter and
681 * repeat waiting till our line pops up. Only then the character may
682 * be transmitted. Finally, the state of the transmitter mask is
683 * restored. Welcome to the world of PDP-11!
684 * -------------------------------------------------------------------
686 static void dz_console_putchar(struct uart_port *uport, int ch)
688 struct dz_port *dport = (struct dz_port *)uport;
690 unsigned short csr, tcr, trdy, mask;
693 spin_lock_irqsave(&dport->port.lock, flags);
694 csr = dz_in(dport, DZ_CSR);
695 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
696 tcr = dz_in(dport, DZ_TCR);
697 tcr |= 1 << dport->port.line;
699 dz_out(dport, DZ_TCR, mask);
701 spin_unlock_irqrestore(&dport->port.lock, flags);
704 trdy = dz_in(dport, DZ_CSR);
705 if (!(trdy & DZ_TRDY))
707 trdy = (trdy & DZ_TLINE) >> 8;
708 if (trdy == dport->port.line)
710 mask &= ~(1 << trdy);
711 dz_out(dport, DZ_TCR, mask);
716 if (loops) /* Cannot send otherwise. */
717 dz_out(dport, DZ_TDR, ch);
719 dz_out(dport, DZ_TCR, tcr);
720 dz_out(dport, DZ_CSR, csr);
724 * -------------------------------------------------------------------
725 * dz_console_print ()
727 * dz_console_print is registered for printk.
728 * The console must be locked when we get here.
729 * -------------------------------------------------------------------
731 static void dz_console_print(struct console *co,
735 struct dz_port *dport = &dz_ports[co->index];
737 prom_printf((char *) str);
739 uart_console_write(&dport->port, str, count, dz_console_putchar);
742 static int __init dz_console_setup(struct console *co, char *options)
744 struct dz_port *dport = &dz_ports[co->index];
751 uart_parse_options(options, &baud, &parity, &bits, &flow);
755 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
758 static struct uart_driver dz_reg;
759 static struct console dz_console = {
761 .write = dz_console_print,
762 .device = uart_console_device,
763 .setup = dz_console_setup,
764 .flags = CON_PRINTBUFFER,
769 static int __init dz_serial_console_init(void)
773 register_console(&dz_console);
779 console_initcall(dz_serial_console_init);
781 #define SERIAL_DZ_CONSOLE &dz_console
783 #define SERIAL_DZ_CONSOLE NULL
784 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
786 static struct uart_driver dz_reg = {
787 .owner = THIS_MODULE,
788 .driver_name = "serial",
793 .cons = SERIAL_DZ_CONSOLE,
796 static int __init dz_init(void)
803 printk("%s%s\n", dz_name, dz_version);
807 #ifndef CONFIG_SERIAL_DZ_CONSOLE
809 dz_reset(&dz_ports[0]);
812 ret = uart_register_driver(&dz_reg);
816 ret = request_irq(dz_ports[0].port.irq, dz_interrupt, IRQF_DISABLED,
819 printk(KERN_ERR "dz: Cannot get IRQ %d!\n",
820 dz_ports[0].port.irq);
824 for (i = 0; i < DZ_NB_PORT; i++)
825 uart_add_one_port(&dz_reg, &dz_ports[i].port);
830 uart_unregister_driver(&dz_reg);
836 module_init(dz_init);
838 MODULE_DESCRIPTION("DECstation DZ serial driver");
839 MODULE_LICENSE("GPL");