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/ioport.h>
43 #include <linux/kernel.h>
44 #include <linux/major.h>
45 #include <linux/module.h>
46 #include <linux/serial.h>
47 #include <linux/serial_core.h>
48 #include <linux/sysrq.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
52 #include <linux/atomic.h>
53 #include <asm/bootinfo.h>
56 #include <asm/dec/interrupts.h>
57 #include <asm/dec/kn01.h>
58 #include <asm/dec/kn02.h>
59 #include <asm/dec/machtype.h>
60 #include <asm/dec/prom.h>
61 #include <asm/dec/system.h>
66 MODULE_DESCRIPTION("DECstation DZ serial driver");
67 MODULE_LICENSE("GPL");
70 static char dz_name[] __initdata = "DECstation DZ serial driver version ";
71 static char dz_version[] __initdata = "1.04";
75 struct uart_port port;
80 struct dz_port dport[DZ_NB_PORT];
86 static struct dz_mux dz_mux;
88 static inline struct dz_port *to_dport(struct uart_port *uport)
90 return container_of(uport, struct dz_port, port);
94 * ------------------------------------------------------------
95 * dz_in () and dz_out ()
97 * These routines are used to access the registers of the DZ
98 * chip, hiding relocation differences between implementation.
99 * ------------------------------------------------------------
102 static u16 dz_in(struct dz_port *dport, unsigned offset)
104 void __iomem *addr = dport->port.membase + offset;
109 static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
111 void __iomem *addr = dport->port.membase + offset;
117 * ------------------------------------------------------------
118 * rs_stop () and rs_start ()
120 * These routines are called before setting or resetting
121 * tty->stopped. They enable or disable transmitter interrupts,
123 * ------------------------------------------------------------
126 static void dz_stop_tx(struct uart_port *uport)
128 struct dz_port *dport = to_dport(uport);
129 u16 tmp, mask = 1 << dport->port.line;
131 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
132 tmp &= ~mask; /* clear the TX flag */
133 dz_out(dport, DZ_TCR, tmp);
136 static void dz_start_tx(struct uart_port *uport)
138 struct dz_port *dport = to_dport(uport);
139 u16 tmp, mask = 1 << dport->port.line;
141 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
142 tmp |= mask; /* set the TX flag */
143 dz_out(dport, DZ_TCR, tmp);
146 static void dz_stop_rx(struct uart_port *uport)
148 struct dz_port *dport = to_dport(uport);
150 dport->cflag &= ~DZ_RXENAB;
151 dz_out(dport, DZ_LPR, dport->cflag);
155 * ------------------------------------------------------------
157 * Here start the interrupt handling routines. All of the following
158 * subroutines are declared as inline and are folded into
159 * dz_interrupt. They were separated out for readability's sake.
161 * Note: dz_interrupt() is a "fast" interrupt, which means that it
162 * runs with interrupts turned off. People who may want to modify
163 * dz_interrupt() should try to keep the interrupt handler as fast as
164 * possible. After you are done making modifications, it is not a bad
167 * make drivers/serial/dz.s
169 * and look at the resulting assemble code in dz.s.
171 * ------------------------------------------------------------
175 * ------------------------------------------------------------
178 * This routine deals with inputs from any lines.
179 * ------------------------------------------------------------
181 static inline void dz_receive_chars(struct dz_mux *mux)
183 struct uart_port *uport;
184 struct dz_port *dport = &mux->dport[0];
185 struct uart_icount *icount;
186 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
187 unsigned char ch, flag;
191 while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
192 dport = &mux->dport[LINE(status)];
193 uport = &dport->port;
195 ch = UCHAR(status); /* grab the char */
198 icount = &uport->icount;
201 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
204 * There is no separate BREAK status bit, so treat
205 * null characters with framing errors as BREAKs;
206 * normally, otherwise. For this move the Framing
207 * Error bit to a simulated BREAK bit.
210 status |= (status & DZ_FERR) >>
211 (ffs(DZ_FERR) - ffs(DZ_BREAK));
215 /* Handle SysRq/SAK & keep track of the statistics. */
216 if (status & DZ_BREAK) {
218 if (uart_handle_break(uport))
220 } else if (status & DZ_FERR)
222 else if (status & DZ_PERR)
224 if (status & DZ_OERR)
227 status &= uport->read_status_mask;
228 if (status & DZ_BREAK)
230 else if (status & DZ_FERR)
232 else if (status & DZ_PERR)
237 if (uart_handle_sysrq_char(uport, ch))
240 uart_insert_char(uport, status, DZ_OERR, ch, flag);
241 lines_rx[LINE(status)] = 1;
243 for (i = 0; i < DZ_NB_PORT; i++)
245 tty_flip_buffer_push(&mux->dport[i].port.state->port);
249 * ------------------------------------------------------------
252 * This routine deals with outputs to any lines.
253 * ------------------------------------------------------------
255 static inline void dz_transmit_chars(struct dz_mux *mux)
257 struct dz_port *dport = &mux->dport[0];
258 struct circ_buf *xmit;
262 status = dz_in(dport, DZ_CSR);
263 dport = &mux->dport[LINE(status)];
264 xmit = &dport->port.state->xmit;
266 if (dport->port.x_char) { /* XON/XOFF chars */
267 dz_out(dport, DZ_TDR, dport->port.x_char);
268 dport->port.icount.tx++;
269 dport->port.x_char = 0;
272 /* If nothing to do or stopped or hardware stopped. */
273 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
274 spin_lock(&dport->port.lock);
275 dz_stop_tx(&dport->port);
276 spin_unlock(&dport->port.lock);
281 * If something to do... (remember the dz has no output fifo,
282 * so we go one char at a time) :-<
284 tmp = xmit->buf[xmit->tail];
285 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
286 dz_out(dport, DZ_TDR, tmp);
287 dport->port.icount.tx++;
289 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
290 uart_write_wakeup(&dport->port);
292 /* Are we are done. */
293 if (uart_circ_empty(xmit)) {
294 spin_lock(&dport->port.lock);
295 dz_stop_tx(&dport->port);
296 spin_unlock(&dport->port.lock);
301 * ------------------------------------------------------------
302 * check_modem_status()
304 * DS 3100 & 5100: Only valid for the MODEM line, duh!
305 * DS 5000/200: Valid for the MODEM and PRINTER line.
306 * ------------------------------------------------------------
308 static inline void check_modem_status(struct dz_port *dport)
312 * 1. No status change interrupt; use a timer.
313 * 2. Handle the 3100/5000 as appropriate. --macro
317 /* If not the modem line just return. */
318 if (dport->port.line != DZ_MODEM)
321 status = dz_in(dport, DZ_MSR);
323 /* it's easy, since DSR2 is the only bit in the register */
325 dport->port.icount.dsr++;
329 * ------------------------------------------------------------
332 * this is the main interrupt routine for the DZ chip.
333 * It deals with the multiple ports.
334 * ------------------------------------------------------------
336 static irqreturn_t dz_interrupt(int irq, void *dev_id)
338 struct dz_mux *mux = dev_id;
339 struct dz_port *dport = &mux->dport[0];
342 /* get the reason why we just got an irq */
343 status = dz_in(dport, DZ_CSR);
345 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
346 dz_receive_chars(mux);
348 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
349 dz_transmit_chars(mux);
355 * -------------------------------------------------------------------
356 * Here ends the DZ interrupt routines.
357 * -------------------------------------------------------------------
360 static unsigned int dz_get_mctrl(struct uart_port *uport)
363 * FIXME: Handle the 3100/5000 as appropriate. --macro
365 struct dz_port *dport = to_dport(uport);
366 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
368 if (dport->port.line == DZ_MODEM) {
369 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
376 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
379 * FIXME: Handle the 3100/5000 as appropriate. --macro
381 struct dz_port *dport = to_dport(uport);
384 if (dport->port.line == DZ_MODEM) {
385 tmp = dz_in(dport, DZ_TCR);
386 if (mctrl & TIOCM_DTR)
387 tmp &= ~DZ_MODEM_DTR;
390 dz_out(dport, DZ_TCR, tmp);
395 * -------------------------------------------------------------------
398 * various initialization tasks
399 * -------------------------------------------------------------------
401 static int dz_startup(struct uart_port *uport)
403 struct dz_port *dport = to_dport(uport);
404 struct dz_mux *mux = dport->mux;
410 irq_guard = atomic_add_return(1, &mux->irq_guard);
414 ret = request_irq(dport->port.irq, dz_interrupt,
415 IRQF_SHARED, "dz", mux);
417 atomic_add(-1, &mux->irq_guard);
418 printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
422 spin_lock_irqsave(&dport->port.lock, flags);
424 /* Enable interrupts. */
425 tmp = dz_in(dport, DZ_CSR);
426 tmp |= DZ_RIE | DZ_TIE;
427 dz_out(dport, DZ_CSR, tmp);
429 spin_unlock_irqrestore(&dport->port.lock, flags);
435 * -------------------------------------------------------------------
438 * This routine will shutdown a serial port; interrupts are disabled, and
439 * DTR is dropped if the hangup on close termio flag is on.
440 * -------------------------------------------------------------------
442 static void dz_shutdown(struct uart_port *uport)
444 struct dz_port *dport = to_dport(uport);
445 struct dz_mux *mux = dport->mux;
450 spin_lock_irqsave(&dport->port.lock, flags);
451 dz_stop_tx(&dport->port);
452 spin_unlock_irqrestore(&dport->port.lock, flags);
454 irq_guard = atomic_add_return(-1, &mux->irq_guard);
456 /* Disable interrupts. */
457 tmp = dz_in(dport, DZ_CSR);
458 tmp &= ~(DZ_RIE | DZ_TIE);
459 dz_out(dport, DZ_CSR, tmp);
461 free_irq(dport->port.irq, mux);
466 * -------------------------------------------------------------------
467 * dz_tx_empty() -- get the transmitter empty status
469 * Purpose: Let user call ioctl() to get info when the UART physically
470 * is emptied. On bus types like RS485, the transmitter must
471 * release the bus after transmitting. This must be done when
472 * the transmit shift register is empty, not be done when the
473 * transmit holding register is empty. This functionality
474 * allows an RS485 driver to be written in user space.
475 * -------------------------------------------------------------------
477 static unsigned int dz_tx_empty(struct uart_port *uport)
479 struct dz_port *dport = to_dport(uport);
480 unsigned short tmp, mask = 1 << dport->port.line;
482 tmp = dz_in(dport, DZ_TCR);
485 return tmp ? 0 : TIOCSER_TEMT;
488 static void dz_break_ctl(struct uart_port *uport, int break_state)
491 * FIXME: Can't access BREAK bits in TDR easily;
492 * reuse the code for polled TX. --macro
494 struct dz_port *dport = to_dport(uport);
496 unsigned short tmp, mask = 1 << dport->port.line;
498 spin_lock_irqsave(&uport->lock, flags);
499 tmp = dz_in(dport, DZ_TCR);
504 dz_out(dport, DZ_TCR, tmp);
505 spin_unlock_irqrestore(&uport->lock, flags);
508 static int dz_encode_baud_rate(unsigned int baud)
547 static void dz_reset(struct dz_port *dport)
549 struct dz_mux *mux = dport->mux;
551 if (mux->initialised)
554 dz_out(dport, DZ_CSR, DZ_CLR);
555 while (dz_in(dport, DZ_CSR) & DZ_CLR);
558 /* Enable scanning. */
559 dz_out(dport, DZ_CSR, DZ_MSE);
561 mux->initialised = 1;
564 static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
565 struct ktermios *old_termios)
567 struct dz_port *dport = to_dport(uport);
569 unsigned int cflag, baud;
572 cflag = dport->port.line;
574 switch (termios->c_cflag & CSIZE) {
589 if (termios->c_cflag & CSTOPB)
591 if (termios->c_cflag & PARENB)
593 if (termios->c_cflag & PARODD)
596 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
597 bflag = dz_encode_baud_rate(baud);
598 if (bflag < 0) { /* Try to keep unchanged. */
599 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
600 bflag = dz_encode_baud_rate(baud);
601 if (bflag < 0) { /* Resort to 9600. */
605 tty_termios_encode_baud_rate(termios, baud, baud);
609 if (termios->c_cflag & CREAD)
612 spin_lock_irqsave(&dport->port.lock, flags);
614 uart_update_timeout(uport, termios->c_cflag, baud);
616 dz_out(dport, DZ_LPR, cflag);
617 dport->cflag = cflag;
619 /* setup accept flag */
620 dport->port.read_status_mask = DZ_OERR;
621 if (termios->c_iflag & INPCK)
622 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
623 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
624 dport->port.read_status_mask |= DZ_BREAK;
626 /* characters to ignore */
627 uport->ignore_status_mask = 0;
628 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
629 dport->port.ignore_status_mask |= DZ_OERR;
630 if (termios->c_iflag & IGNPAR)
631 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
632 if (termios->c_iflag & IGNBRK)
633 dport->port.ignore_status_mask |= DZ_BREAK;
635 spin_unlock_irqrestore(&dport->port.lock, flags);
640 * Required solely so that the initial PROM-based console
641 * works undisturbed in parallel with this one.
643 static void dz_pm(struct uart_port *uport, unsigned int state,
644 unsigned int oldstate)
646 struct dz_port *dport = to_dport(uport);
649 spin_lock_irqsave(&dport->port.lock, flags);
651 dz_start_tx(&dport->port);
653 dz_stop_tx(&dport->port);
654 spin_unlock_irqrestore(&dport->port.lock, flags);
658 static const char *dz_type(struct uart_port *uport)
663 static void dz_release_port(struct uart_port *uport)
665 struct dz_mux *mux = to_dport(uport)->mux;
668 iounmap(uport->membase);
669 uport->membase = NULL;
671 map_guard = atomic_add_return(-1, &mux->map_guard);
673 release_mem_region(uport->mapbase, dec_kn_slot_size);
676 static int dz_map_port(struct uart_port *uport)
679 uport->membase = ioremap_nocache(uport->mapbase,
681 if (!uport->membase) {
682 printk(KERN_ERR "dz: Cannot map MMIO\n");
688 static int dz_request_port(struct uart_port *uport)
690 struct dz_mux *mux = to_dport(uport)->mux;
694 map_guard = atomic_add_return(1, &mux->map_guard);
695 if (map_guard == 1) {
696 if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
698 atomic_add(-1, &mux->map_guard);
700 "dz: Unable to reserve MMIO resource\n");
704 ret = dz_map_port(uport);
706 map_guard = atomic_add_return(-1, &mux->map_guard);
708 release_mem_region(uport->mapbase, dec_kn_slot_size);
714 static void dz_config_port(struct uart_port *uport, int flags)
716 struct dz_port *dport = to_dport(uport);
718 if (flags & UART_CONFIG_TYPE) {
719 if (dz_request_port(uport))
722 uport->type = PORT_DZ;
729 * Verify the new serial_struct (for TIOCSSERIAL).
731 static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
735 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
737 if (ser->irq != uport->irq)
742 static struct uart_ops dz_ops = {
743 .tx_empty = dz_tx_empty,
744 .get_mctrl = dz_get_mctrl,
745 .set_mctrl = dz_set_mctrl,
746 .stop_tx = dz_stop_tx,
747 .start_tx = dz_start_tx,
748 .stop_rx = dz_stop_rx,
749 .break_ctl = dz_break_ctl,
750 .startup = dz_startup,
751 .shutdown = dz_shutdown,
752 .set_termios = dz_set_termios,
755 .release_port = dz_release_port,
756 .request_port = dz_request_port,
757 .config_port = dz_config_port,
758 .verify_port = dz_verify_port,
761 static void __init dz_init_ports(void)
763 static int first = 1;
771 if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
772 base = dec_kn_slot_base + KN01_DZ11;
774 base = dec_kn_slot_base + KN02_DZ11;
776 for (line = 0; line < DZ_NB_PORT; line++) {
777 struct dz_port *dport = &dz_mux.dport[line];
778 struct uart_port *uport = &dport->port;
780 dport->mux = &dz_mux;
782 uport->irq = dec_interrupt[DEC_IRQ_DZ11];
784 uport->iotype = UPIO_MEM;
785 uport->flags = UPF_BOOT_AUTOCONF;
786 uport->ops = &dz_ops;
788 uport->mapbase = base;
792 #ifdef CONFIG_SERIAL_DZ_CONSOLE
794 * -------------------------------------------------------------------
795 * dz_console_putchar() -- transmit a character
797 * Polled transmission. This is tricky. We need to mask transmit
798 * interrupts so that they do not interfere, enable the transmitter
799 * for the line requested and then wait till the transmit scanner
800 * requests data for this line. But it may request data for another
801 * line first, in which case we have to disable its transmitter and
802 * repeat waiting till our line pops up. Only then the character may
803 * be transmitted. Finally, the state of the transmitter mask is
804 * restored. Welcome to the world of PDP-11!
805 * -------------------------------------------------------------------
807 static void dz_console_putchar(struct uart_port *uport, int ch)
809 struct dz_port *dport = to_dport(uport);
811 unsigned short csr, tcr, trdy, mask;
814 spin_lock_irqsave(&dport->port.lock, flags);
815 csr = dz_in(dport, DZ_CSR);
816 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
817 tcr = dz_in(dport, DZ_TCR);
818 tcr |= 1 << dport->port.line;
820 dz_out(dport, DZ_TCR, mask);
822 spin_unlock_irqrestore(&dport->port.lock, flags);
825 trdy = dz_in(dport, DZ_CSR);
826 if (!(trdy & DZ_TRDY))
828 trdy = (trdy & DZ_TLINE) >> 8;
829 if (trdy == dport->port.line)
831 mask &= ~(1 << trdy);
832 dz_out(dport, DZ_TCR, mask);
837 if (loops) /* Cannot send otherwise. */
838 dz_out(dport, DZ_TDR, ch);
840 dz_out(dport, DZ_TCR, tcr);
841 dz_out(dport, DZ_CSR, csr);
845 * -------------------------------------------------------------------
846 * dz_console_print ()
848 * dz_console_print is registered for printk.
849 * The console must be locked when we get here.
850 * -------------------------------------------------------------------
852 static void dz_console_print(struct console *co,
856 struct dz_port *dport = &dz_mux.dport[co->index];
858 prom_printf((char *) str);
860 uart_console_write(&dport->port, str, count, dz_console_putchar);
863 static int __init dz_console_setup(struct console *co, char *options)
865 struct dz_port *dport = &dz_mux.dport[co->index];
866 struct uart_port *uport = &dport->port;
873 ret = dz_map_port(uport);
877 spin_lock_init(&dport->port.lock); /* For dz_pm(). */
883 uart_parse_options(options, &baud, &parity, &bits, &flow);
885 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
888 static struct uart_driver dz_reg;
889 static struct console dz_console = {
891 .write = dz_console_print,
892 .device = uart_console_device,
893 .setup = dz_console_setup,
894 .flags = CON_PRINTBUFFER,
899 static int __init dz_serial_console_init(void)
903 register_console(&dz_console);
909 console_initcall(dz_serial_console_init);
911 #define SERIAL_DZ_CONSOLE &dz_console
913 #define SERIAL_DZ_CONSOLE NULL
914 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
916 static struct uart_driver dz_reg = {
917 .owner = THIS_MODULE,
918 .driver_name = "serial",
923 .cons = SERIAL_DZ_CONSOLE,
926 static int __init dz_init(void)
933 printk("%s%s\n", dz_name, dz_version);
937 ret = uart_register_driver(&dz_reg);
941 for (i = 0; i < DZ_NB_PORT; i++)
942 uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
947 module_init(dz_init);