1 // SPDX-License-Identifier: GPL-2.0
3 * dz.c: Serial port driver for DECstations equipped
6 * Copyright (C) 1998 Olivier A. D. Lebaillif
8 * Email: olivier.lebaillif@ifrsys.com
10 * Copyright (C) 2004, 2006, 2007 Maciej W. Rozycki
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
23 * Parts (C) 1999 David Airlie, airlied@linux.ie
24 * [07-SEP-99] Bugfixes
26 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
27 * Converted to new serial core
32 #include <linux/bitops.h>
33 #include <linux/compiler.h>
34 #include <linux/console.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/ioport.h>
40 #include <linux/kernel.h>
41 #include <linux/major.h>
42 #include <linux/module.h>
43 #include <linux/serial.h>
44 #include <linux/serial_core.h>
45 #include <linux/sysrq.h>
46 #include <linux/tty.h>
47 #include <linux/tty_flip.h>
49 #include <linux/atomic.h>
51 #include <asm/bootinfo.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>
58 #include <asm/dec/system.h>
63 MODULE_DESCRIPTION("DECstation DZ serial driver");
64 MODULE_LICENSE("GPL");
67 static char dz_name[] __initdata = "DECstation DZ serial driver version ";
68 static char dz_version[] __initdata = "1.04";
72 struct uart_port port;
77 struct dz_port dport[DZ_NB_PORT];
83 static struct dz_mux dz_mux;
85 static inline struct dz_port *to_dport(struct uart_port *uport)
87 return container_of(uport, struct dz_port, port);
91 * ------------------------------------------------------------
92 * dz_in () and dz_out ()
94 * These routines are used to access the registers of the DZ
95 * chip, hiding relocation differences between implementation.
96 * ------------------------------------------------------------
99 static u16 dz_in(struct dz_port *dport, unsigned offset)
101 void __iomem *addr = dport->port.membase + offset;
106 static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
108 void __iomem *addr = dport->port.membase + offset;
114 * ------------------------------------------------------------
115 * rs_stop () and rs_start ()
117 * These routines are called before setting or resetting
118 * tty->flow.stopped. They enable or disable transmitter interrupts,
120 * ------------------------------------------------------------
123 static void dz_stop_tx(struct uart_port *uport)
125 struct dz_port *dport = to_dport(uport);
126 u16 tmp, mask = 1 << dport->port.line;
128 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
129 tmp &= ~mask; /* clear the TX flag */
130 dz_out(dport, DZ_TCR, tmp);
133 static void dz_start_tx(struct uart_port *uport)
135 struct dz_port *dport = to_dport(uport);
136 u16 tmp, mask = 1 << dport->port.line;
138 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
139 tmp |= mask; /* set the TX flag */
140 dz_out(dport, DZ_TCR, tmp);
143 static void dz_stop_rx(struct uart_port *uport)
145 struct dz_port *dport = to_dport(uport);
147 dport->cflag &= ~DZ_RXENAB;
148 dz_out(dport, DZ_LPR, dport->cflag);
152 * ------------------------------------------------------------
154 * Here start the interrupt handling routines. All of the following
155 * subroutines are declared as inline and are folded into
156 * dz_interrupt. They were separated out for readability's sake.
158 * Note: dz_interrupt() is a "fast" interrupt, which means that it
159 * runs with interrupts turned off. People who may want to modify
160 * dz_interrupt() should try to keep the interrupt handler as fast as
161 * possible. After you are done making modifications, it is not a bad
164 * make drivers/serial/dz.s
166 * and look at the resulting assemble code in dz.s.
168 * ------------------------------------------------------------
172 * ------------------------------------------------------------
175 * This routine deals with inputs from any lines.
176 * ------------------------------------------------------------
178 static inline void dz_receive_chars(struct dz_mux *mux)
180 struct uart_port *uport;
181 struct dz_port *dport = &mux->dport[0];
182 struct uart_icount *icount;
183 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
188 while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
189 dport = &mux->dport[LINE(status)];
190 uport = &dport->port;
192 ch = UCHAR(status); /* grab the char */
195 icount = &uport->icount;
198 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
201 * There is no separate BREAK status bit, so treat
202 * null characters with framing errors as BREAKs;
203 * normally, otherwise. For this move the Framing
204 * Error bit to a simulated BREAK bit.
207 status |= (status & DZ_FERR) >>
208 (ffs(DZ_FERR) - ffs(DZ_BREAK));
212 /* Handle SysRq/SAK & keep track of the statistics. */
213 if (status & DZ_BREAK) {
215 if (uart_handle_break(uport))
217 } else if (status & DZ_FERR)
219 else if (status & DZ_PERR)
221 if (status & DZ_OERR)
224 status &= uport->read_status_mask;
225 if (status & DZ_BREAK)
227 else if (status & DZ_FERR)
229 else if (status & DZ_PERR)
234 if (uart_handle_sysrq_char(uport, ch))
237 uart_insert_char(uport, status, DZ_OERR, ch, flag);
238 lines_rx[LINE(status)] = 1;
240 for (i = 0; i < DZ_NB_PORT; i++)
242 tty_flip_buffer_push(&mux->dport[i].port.state->port);
246 * ------------------------------------------------------------
249 * This routine deals with outputs to any lines.
250 * ------------------------------------------------------------
252 static inline void dz_transmit_chars(struct dz_mux *mux)
254 struct dz_port *dport = &mux->dport[0];
255 struct circ_buf *xmit;
259 status = dz_in(dport, DZ_CSR);
260 dport = &mux->dport[LINE(status)];
261 xmit = &dport->port.state->xmit;
263 if (dport->port.x_char) { /* XON/XOFF chars */
264 dz_out(dport, DZ_TDR, dport->port.x_char);
265 dport->port.icount.tx++;
266 dport->port.x_char = 0;
269 /* If nothing to do or stopped or hardware stopped. */
270 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
271 spin_lock(&dport->port.lock);
272 dz_stop_tx(&dport->port);
273 spin_unlock(&dport->port.lock);
278 * If something to do... (remember the dz has no output fifo,
279 * so we go one char at a time) :-<
281 tmp = xmit->buf[xmit->tail];
282 dz_out(dport, DZ_TDR, tmp);
283 uart_xmit_advance(&dport->port, 1);
285 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
286 uart_write_wakeup(&dport->port);
288 /* Are we are done. */
289 if (uart_circ_empty(xmit)) {
290 spin_lock(&dport->port.lock);
291 dz_stop_tx(&dport->port);
292 spin_unlock(&dport->port.lock);
297 * ------------------------------------------------------------
298 * check_modem_status()
300 * DS 3100 & 5100: Only valid for the MODEM line, duh!
301 * DS 5000/200: Valid for the MODEM and PRINTER line.
302 * ------------------------------------------------------------
304 static inline void check_modem_status(struct dz_port *dport)
308 * 1. No status change interrupt; use a timer.
309 * 2. Handle the 3100/5000 as appropriate. --macro
313 /* If not the modem line just return. */
314 if (dport->port.line != DZ_MODEM)
317 status = dz_in(dport, DZ_MSR);
319 /* it's easy, since DSR2 is the only bit in the register */
321 dport->port.icount.dsr++;
325 * ------------------------------------------------------------
328 * this is the main interrupt routine for the DZ chip.
329 * It deals with the multiple ports.
330 * ------------------------------------------------------------
332 static irqreturn_t dz_interrupt(int irq, void *dev_id)
334 struct dz_mux *mux = dev_id;
335 struct dz_port *dport = &mux->dport[0];
338 /* get the reason why we just got an irq */
339 status = dz_in(dport, DZ_CSR);
341 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
342 dz_receive_chars(mux);
344 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
345 dz_transmit_chars(mux);
351 * -------------------------------------------------------------------
352 * Here ends the DZ interrupt routines.
353 * -------------------------------------------------------------------
356 static unsigned int dz_get_mctrl(struct uart_port *uport)
359 * FIXME: Handle the 3100/5000 as appropriate. --macro
361 struct dz_port *dport = to_dport(uport);
362 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
364 if (dport->port.line == DZ_MODEM) {
365 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
372 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
375 * FIXME: Handle the 3100/5000 as appropriate. --macro
377 struct dz_port *dport = to_dport(uport);
380 if (dport->port.line == DZ_MODEM) {
381 tmp = dz_in(dport, DZ_TCR);
382 if (mctrl & TIOCM_DTR)
383 tmp &= ~DZ_MODEM_DTR;
386 dz_out(dport, DZ_TCR, tmp);
391 * -------------------------------------------------------------------
394 * various initialization tasks
395 * -------------------------------------------------------------------
397 static int dz_startup(struct uart_port *uport)
399 struct dz_port *dport = to_dport(uport);
400 struct dz_mux *mux = dport->mux;
406 irq_guard = atomic_add_return(1, &mux->irq_guard);
410 ret = request_irq(dport->port.irq, dz_interrupt,
411 IRQF_SHARED, "dz", mux);
413 atomic_add(-1, &mux->irq_guard);
414 printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
418 spin_lock_irqsave(&dport->port.lock, flags);
420 /* Enable interrupts. */
421 tmp = dz_in(dport, DZ_CSR);
422 tmp |= DZ_RIE | DZ_TIE;
423 dz_out(dport, DZ_CSR, tmp);
425 spin_unlock_irqrestore(&dport->port.lock, flags);
431 * -------------------------------------------------------------------
434 * This routine will shutdown a serial port; interrupts are disabled, and
435 * DTR is dropped if the hangup on close termio flag is on.
436 * -------------------------------------------------------------------
438 static void dz_shutdown(struct uart_port *uport)
440 struct dz_port *dport = to_dport(uport);
441 struct dz_mux *mux = dport->mux;
446 spin_lock_irqsave(&dport->port.lock, flags);
447 dz_stop_tx(&dport->port);
448 spin_unlock_irqrestore(&dport->port.lock, flags);
450 irq_guard = atomic_add_return(-1, &mux->irq_guard);
452 /* Disable interrupts. */
453 tmp = dz_in(dport, DZ_CSR);
454 tmp &= ~(DZ_RIE | DZ_TIE);
455 dz_out(dport, DZ_CSR, tmp);
457 free_irq(dport->port.irq, mux);
462 * -------------------------------------------------------------------
463 * dz_tx_empty() -- get the transmitter empty status
465 * Purpose: Let user call ioctl() to get info when the UART physically
466 * is emptied. On bus types like RS485, the transmitter must
467 * release the bus after transmitting. This must be done when
468 * the transmit shift register is empty, not be done when the
469 * transmit holding register is empty. This functionality
470 * allows an RS485 driver to be written in user space.
471 * -------------------------------------------------------------------
473 static unsigned int dz_tx_empty(struct uart_port *uport)
475 struct dz_port *dport = to_dport(uport);
476 unsigned short tmp, mask = 1 << dport->port.line;
478 tmp = dz_in(dport, DZ_TCR);
481 return tmp ? 0 : TIOCSER_TEMT;
484 static void dz_break_ctl(struct uart_port *uport, int break_state)
487 * FIXME: Can't access BREAK bits in TDR easily;
488 * reuse the code for polled TX. --macro
490 struct dz_port *dport = to_dport(uport);
492 unsigned short tmp, mask = 1 << dport->port.line;
494 spin_lock_irqsave(&uport->lock, flags);
495 tmp = dz_in(dport, DZ_TCR);
500 dz_out(dport, DZ_TCR, tmp);
501 spin_unlock_irqrestore(&uport->lock, flags);
504 static int dz_encode_baud_rate(unsigned int baud)
543 static void dz_reset(struct dz_port *dport)
545 struct dz_mux *mux = dport->mux;
547 if (mux->initialised)
550 dz_out(dport, DZ_CSR, DZ_CLR);
551 while (dz_in(dport, DZ_CSR) & DZ_CLR);
554 /* Enable scanning. */
555 dz_out(dport, DZ_CSR, DZ_MSE);
557 mux->initialised = 1;
560 static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
561 const struct ktermios *old_termios)
563 struct dz_port *dport = to_dport(uport);
565 unsigned int cflag, baud;
568 cflag = dport->port.line;
570 switch (termios->c_cflag & CSIZE) {
585 if (termios->c_cflag & CSTOPB)
587 if (termios->c_cflag & PARENB)
589 if (termios->c_cflag & PARODD)
592 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
593 bflag = dz_encode_baud_rate(baud);
596 /* Keep unchanged. */
597 baud = tty_termios_baud_rate(old_termios);
598 bflag = dz_encode_baud_rate(baud);
600 if (bflag < 0) { /* Resort to 9600. */
604 tty_termios_encode_baud_rate(termios, baud, baud);
608 if (termios->c_cflag & CREAD)
611 spin_lock_irqsave(&dport->port.lock, flags);
613 uart_update_timeout(uport, termios->c_cflag, baud);
615 dz_out(dport, DZ_LPR, cflag);
616 dport->cflag = cflag;
618 /* setup accept flag */
619 dport->port.read_status_mask = DZ_OERR;
620 if (termios->c_iflag & INPCK)
621 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
622 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
623 dport->port.read_status_mask |= DZ_BREAK;
625 /* characters to ignore */
626 uport->ignore_status_mask = 0;
627 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
628 dport->port.ignore_status_mask |= DZ_OERR;
629 if (termios->c_iflag & IGNPAR)
630 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
631 if (termios->c_iflag & IGNBRK)
632 dport->port.ignore_status_mask |= DZ_BREAK;
634 spin_unlock_irqrestore(&dport->port.lock, flags);
639 * Required solely so that the initial PROM-based console
640 * works undisturbed in parallel with this one.
642 static void dz_pm(struct uart_port *uport, unsigned int state,
643 unsigned int oldstate)
645 struct dz_port *dport = to_dport(uport);
648 spin_lock_irqsave(&dport->port.lock, flags);
650 dz_start_tx(&dport->port);
652 dz_stop_tx(&dport->port);
653 spin_unlock_irqrestore(&dport->port.lock, flags);
657 static const char *dz_type(struct uart_port *uport)
662 static void dz_release_port(struct uart_port *uport)
664 struct dz_mux *mux = to_dport(uport)->mux;
667 iounmap(uport->membase);
668 uport->membase = NULL;
670 map_guard = atomic_add_return(-1, &mux->map_guard);
672 release_mem_region(uport->mapbase, dec_kn_slot_size);
675 static int dz_map_port(struct uart_port *uport)
678 uport->membase = ioremap(uport->mapbase,
680 if (!uport->membase) {
681 printk(KERN_ERR "dz: Cannot map MMIO\n");
687 static int dz_request_port(struct uart_port *uport)
689 struct dz_mux *mux = to_dport(uport)->mux;
693 map_guard = atomic_add_return(1, &mux->map_guard);
694 if (map_guard == 1) {
695 if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
697 atomic_add(-1, &mux->map_guard);
699 "dz: Unable to reserve MMIO resource\n");
703 ret = dz_map_port(uport);
705 map_guard = atomic_add_return(-1, &mux->map_guard);
707 release_mem_region(uport->mapbase, dec_kn_slot_size);
713 static void dz_config_port(struct uart_port *uport, int flags)
715 struct dz_port *dport = to_dport(uport);
717 if (flags & UART_CONFIG_TYPE) {
718 if (dz_request_port(uport))
721 uport->type = PORT_DZ;
728 * Verify the new serial_struct (for TIOCSSERIAL).
730 static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
734 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
736 if (ser->irq != uport->irq)
741 static const struct uart_ops dz_ops = {
742 .tx_empty = dz_tx_empty,
743 .get_mctrl = dz_get_mctrl,
744 .set_mctrl = dz_set_mctrl,
745 .stop_tx = dz_stop_tx,
746 .start_tx = dz_start_tx,
747 .stop_rx = dz_stop_rx,
748 .break_ctl = dz_break_ctl,
749 .startup = dz_startup,
750 .shutdown = dz_shutdown,
751 .set_termios = dz_set_termios,
754 .release_port = dz_release_port,
755 .request_port = dz_request_port,
756 .config_port = dz_config_port,
757 .verify_port = dz_verify_port,
760 static void __init dz_init_ports(void)
762 static int first = 1;
770 if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
771 base = dec_kn_slot_base + KN01_DZ11;
773 base = dec_kn_slot_base + KN02_DZ11;
775 for (line = 0; line < DZ_NB_PORT; line++) {
776 struct dz_port *dport = &dz_mux.dport[line];
777 struct uart_port *uport = &dport->port;
779 dport->mux = &dz_mux;
781 uport->irq = dec_interrupt[DEC_IRQ_DZ11];
783 uport->iotype = UPIO_MEM;
784 uport->flags = UPF_BOOT_AUTOCONF;
785 uport->ops = &dz_ops;
787 uport->mapbase = base;
788 uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_DZ_CONSOLE);
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, unsigned char 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);