1 // SPDX-License-Identifier: GPL-2.0
3 * dz.c: Serial port driver for DECstations equipped
6 * Copyright (C) 1998 Olivier A. D. Lebaillif
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
24 * [07-SEP-99] Bugfixes
27 * Converted to new serial core
32 #if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/bitops.h>
37 #include <linux/compiler.h>
38 #include <linux/console.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/ioport.h>
44 #include <linux/kernel.h>
45 #include <linux/major.h>
46 #include <linux/module.h>
47 #include <linux/serial.h>
48 #include <linux/serial_core.h>
49 #include <linux/sysrq.h>
50 #include <linux/tty.h>
51 #include <linux/tty_flip.h>
53 #include <linux/atomic.h>
54 #include <asm/bootinfo.h>
57 #include <asm/dec/interrupts.h>
58 #include <asm/dec/kn01.h>
59 #include <asm/dec/kn02.h>
60 #include <asm/dec/machtype.h>
61 #include <asm/dec/prom.h>
62 #include <asm/dec/system.h>
67 MODULE_DESCRIPTION("DECstation DZ serial driver");
68 MODULE_LICENSE("GPL");
71 static char dz_name[] __initdata = "DECstation DZ serial driver version ";
72 static char dz_version[] __initdata = "1.04";
76 struct uart_port port;
81 struct dz_port dport[DZ_NB_PORT];
87 static struct dz_mux dz_mux;
89 static inline struct dz_port *to_dport(struct uart_port *uport)
91 return container_of(uport, struct dz_port, port);
95 * ------------------------------------------------------------
96 * dz_in () and dz_out ()
98 * These routines are used to access the registers of the DZ
99 * chip, hiding relocation differences between implementation.
100 * ------------------------------------------------------------
103 static u16 dz_in(struct dz_port *dport, unsigned offset)
105 void __iomem *addr = dport->port.membase + offset;
110 static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
112 void __iomem *addr = dport->port.membase + offset;
118 * ------------------------------------------------------------
119 * rs_stop () and rs_start ()
121 * These routines are called before setting or resetting
122 * tty->stopped. They enable or disable transmitter interrupts,
124 * ------------------------------------------------------------
127 static void dz_stop_tx(struct uart_port *uport)
129 struct dz_port *dport = to_dport(uport);
130 u16 tmp, mask = 1 << dport->port.line;
132 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
133 tmp &= ~mask; /* clear the TX flag */
134 dz_out(dport, DZ_TCR, tmp);
137 static void dz_start_tx(struct uart_port *uport)
139 struct dz_port *dport = to_dport(uport);
140 u16 tmp, mask = 1 << dport->port.line;
142 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
143 tmp |= mask; /* set the TX flag */
144 dz_out(dport, DZ_TCR, tmp);
147 static void dz_stop_rx(struct uart_port *uport)
149 struct dz_port *dport = to_dport(uport);
151 dport->cflag &= ~DZ_RXENAB;
152 dz_out(dport, DZ_LPR, dport->cflag);
156 * ------------------------------------------------------------
158 * Here start the interrupt handling routines. All of the following
159 * subroutines are declared as inline and are folded into
160 * dz_interrupt. They were separated out for readability's sake.
162 * Note: dz_interrupt() is a "fast" interrupt, which means that it
163 * runs with interrupts turned off. People who may want to modify
164 * dz_interrupt() should try to keep the interrupt handler as fast as
165 * possible. After you are done making modifications, it is not a bad
168 * make drivers/serial/dz.s
170 * and look at the resulting assemble code in dz.s.
172 * ------------------------------------------------------------
176 * ------------------------------------------------------------
179 * This routine deals with inputs from any lines.
180 * ------------------------------------------------------------
182 static inline void dz_receive_chars(struct dz_mux *mux)
184 struct uart_port *uport;
185 struct dz_port *dport = &mux->dport[0];
186 struct uart_icount *icount;
187 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
188 unsigned char ch, flag;
192 while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
193 dport = &mux->dport[LINE(status)];
194 uport = &dport->port;
196 ch = UCHAR(status); /* grab the char */
199 icount = &uport->icount;
202 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
205 * There is no separate BREAK status bit, so treat
206 * null characters with framing errors as BREAKs;
207 * normally, otherwise. For this move the Framing
208 * Error bit to a simulated BREAK bit.
211 status |= (status & DZ_FERR) >>
212 (ffs(DZ_FERR) - ffs(DZ_BREAK));
216 /* Handle SysRq/SAK & keep track of the statistics. */
217 if (status & DZ_BREAK) {
219 if (uart_handle_break(uport))
221 } else if (status & DZ_FERR)
223 else if (status & DZ_PERR)
225 if (status & DZ_OERR)
228 status &= uport->read_status_mask;
229 if (status & DZ_BREAK)
231 else if (status & DZ_FERR)
233 else if (status & DZ_PERR)
238 if (uart_handle_sysrq_char(uport, ch))
241 uart_insert_char(uport, status, DZ_OERR, ch, flag);
242 lines_rx[LINE(status)] = 1;
244 for (i = 0; i < DZ_NB_PORT; i++)
246 tty_flip_buffer_push(&mux->dport[i].port.state->port);
250 * ------------------------------------------------------------
253 * This routine deals with outputs to any lines.
254 * ------------------------------------------------------------
256 static inline void dz_transmit_chars(struct dz_mux *mux)
258 struct dz_port *dport = &mux->dport[0];
259 struct circ_buf *xmit;
263 status = dz_in(dport, DZ_CSR);
264 dport = &mux->dport[LINE(status)];
265 xmit = &dport->port.state->xmit;
267 if (dport->port.x_char) { /* XON/XOFF chars */
268 dz_out(dport, DZ_TDR, dport->port.x_char);
269 dport->port.icount.tx++;
270 dport->port.x_char = 0;
273 /* If nothing to do or stopped or hardware stopped. */
274 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
275 spin_lock(&dport->port.lock);
276 dz_stop_tx(&dport->port);
277 spin_unlock(&dport->port.lock);
282 * If something to do... (remember the dz has no output fifo,
283 * so we go one char at a time) :-<
285 tmp = xmit->buf[xmit->tail];
286 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
287 dz_out(dport, DZ_TDR, tmp);
288 dport->port.icount.tx++;
290 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
291 uart_write_wakeup(&dport->port);
293 /* Are we are done. */
294 if (uart_circ_empty(xmit)) {
295 spin_lock(&dport->port.lock);
296 dz_stop_tx(&dport->port);
297 spin_unlock(&dport->port.lock);
302 * ------------------------------------------------------------
303 * check_modem_status()
305 * DS 3100 & 5100: Only valid for the MODEM line, duh!
306 * DS 5000/200: Valid for the MODEM and PRINTER line.
307 * ------------------------------------------------------------
309 static inline void check_modem_status(struct dz_port *dport)
313 * 1. No status change interrupt; use a timer.
314 * 2. Handle the 3100/5000 as appropriate. --macro
318 /* If not the modem line just return. */
319 if (dport->port.line != DZ_MODEM)
322 status = dz_in(dport, DZ_MSR);
324 /* it's easy, since DSR2 is the only bit in the register */
326 dport->port.icount.dsr++;
330 * ------------------------------------------------------------
333 * this is the main interrupt routine for the DZ chip.
334 * It deals with the multiple ports.
335 * ------------------------------------------------------------
337 static irqreturn_t dz_interrupt(int irq, void *dev_id)
339 struct dz_mux *mux = dev_id;
340 struct dz_port *dport = &mux->dport[0];
343 /* get the reason why we just got an irq */
344 status = dz_in(dport, DZ_CSR);
346 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
347 dz_receive_chars(mux);
349 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
350 dz_transmit_chars(mux);
356 * -------------------------------------------------------------------
357 * Here ends the DZ interrupt routines.
358 * -------------------------------------------------------------------
361 static unsigned int dz_get_mctrl(struct uart_port *uport)
364 * FIXME: Handle the 3100/5000 as appropriate. --macro
366 struct dz_port *dport = to_dport(uport);
367 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
369 if (dport->port.line == DZ_MODEM) {
370 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
377 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
380 * FIXME: Handle the 3100/5000 as appropriate. --macro
382 struct dz_port *dport = to_dport(uport);
385 if (dport->port.line == DZ_MODEM) {
386 tmp = dz_in(dport, DZ_TCR);
387 if (mctrl & TIOCM_DTR)
388 tmp &= ~DZ_MODEM_DTR;
391 dz_out(dport, DZ_TCR, tmp);
396 * -------------------------------------------------------------------
399 * various initialization tasks
400 * -------------------------------------------------------------------
402 static int dz_startup(struct uart_port *uport)
404 struct dz_port *dport = to_dport(uport);
405 struct dz_mux *mux = dport->mux;
411 irq_guard = atomic_add_return(1, &mux->irq_guard);
415 ret = request_irq(dport->port.irq, dz_interrupt,
416 IRQF_SHARED, "dz", mux);
418 atomic_add(-1, &mux->irq_guard);
419 printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
423 spin_lock_irqsave(&dport->port.lock, flags);
425 /* Enable interrupts. */
426 tmp = dz_in(dport, DZ_CSR);
427 tmp |= DZ_RIE | DZ_TIE;
428 dz_out(dport, DZ_CSR, tmp);
430 spin_unlock_irqrestore(&dport->port.lock, flags);
436 * -------------------------------------------------------------------
439 * This routine will shutdown a serial port; interrupts are disabled, and
440 * DTR is dropped if the hangup on close termio flag is on.
441 * -------------------------------------------------------------------
443 static void dz_shutdown(struct uart_port *uport)
445 struct dz_port *dport = to_dport(uport);
446 struct dz_mux *mux = dport->mux;
451 spin_lock_irqsave(&dport->port.lock, flags);
452 dz_stop_tx(&dport->port);
453 spin_unlock_irqrestore(&dport->port.lock, flags);
455 irq_guard = atomic_add_return(-1, &mux->irq_guard);
457 /* Disable interrupts. */
458 tmp = dz_in(dport, DZ_CSR);
459 tmp &= ~(DZ_RIE | DZ_TIE);
460 dz_out(dport, DZ_CSR, tmp);
462 free_irq(dport->port.irq, mux);
467 * -------------------------------------------------------------------
468 * dz_tx_empty() -- get the transmitter empty status
470 * Purpose: Let user call ioctl() to get info when the UART physically
471 * is emptied. On bus types like RS485, the transmitter must
472 * release the bus after transmitting. This must be done when
473 * the transmit shift register is empty, not be done when the
474 * transmit holding register is empty. This functionality
475 * allows an RS485 driver to be written in user space.
476 * -------------------------------------------------------------------
478 static unsigned int dz_tx_empty(struct uart_port *uport)
480 struct dz_port *dport = to_dport(uport);
481 unsigned short tmp, mask = 1 << dport->port.line;
483 tmp = dz_in(dport, DZ_TCR);
486 return tmp ? 0 : TIOCSER_TEMT;
489 static void dz_break_ctl(struct uart_port *uport, int break_state)
492 * FIXME: Can't access BREAK bits in TDR easily;
493 * reuse the code for polled TX. --macro
495 struct dz_port *dport = to_dport(uport);
497 unsigned short tmp, mask = 1 << dport->port.line;
499 spin_lock_irqsave(&uport->lock, flags);
500 tmp = dz_in(dport, DZ_TCR);
505 dz_out(dport, DZ_TCR, tmp);
506 spin_unlock_irqrestore(&uport->lock, flags);
509 static int dz_encode_baud_rate(unsigned int baud)
548 static void dz_reset(struct dz_port *dport)
550 struct dz_mux *mux = dport->mux;
552 if (mux->initialised)
555 dz_out(dport, DZ_CSR, DZ_CLR);
556 while (dz_in(dport, DZ_CSR) & DZ_CLR);
559 /* Enable scanning. */
560 dz_out(dport, DZ_CSR, DZ_MSE);
562 mux->initialised = 1;
565 static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
566 struct ktermios *old_termios)
568 struct dz_port *dport = to_dport(uport);
570 unsigned int cflag, baud;
573 cflag = dport->port.line;
575 switch (termios->c_cflag & CSIZE) {
590 if (termios->c_cflag & CSTOPB)
592 if (termios->c_cflag & PARENB)
594 if (termios->c_cflag & PARODD)
597 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
598 bflag = dz_encode_baud_rate(baud);
599 if (bflag < 0) { /* Try to keep unchanged. */
600 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
601 bflag = dz_encode_baud_rate(baud);
602 if (bflag < 0) { /* Resort to 9600. */
606 tty_termios_encode_baud_rate(termios, baud, baud);
610 if (termios->c_cflag & CREAD)
613 spin_lock_irqsave(&dport->port.lock, flags);
615 uart_update_timeout(uport, termios->c_cflag, baud);
617 dz_out(dport, DZ_LPR, cflag);
618 dport->cflag = cflag;
620 /* setup accept flag */
621 dport->port.read_status_mask = DZ_OERR;
622 if (termios->c_iflag & INPCK)
623 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
624 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
625 dport->port.read_status_mask |= DZ_BREAK;
627 /* characters to ignore */
628 uport->ignore_status_mask = 0;
629 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
630 dport->port.ignore_status_mask |= DZ_OERR;
631 if (termios->c_iflag & IGNPAR)
632 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
633 if (termios->c_iflag & IGNBRK)
634 dport->port.ignore_status_mask |= DZ_BREAK;
636 spin_unlock_irqrestore(&dport->port.lock, flags);
641 * Required solely so that the initial PROM-based console
642 * works undisturbed in parallel with this one.
644 static void dz_pm(struct uart_port *uport, unsigned int state,
645 unsigned int oldstate)
647 struct dz_port *dport = to_dport(uport);
650 spin_lock_irqsave(&dport->port.lock, flags);
652 dz_start_tx(&dport->port);
654 dz_stop_tx(&dport->port);
655 spin_unlock_irqrestore(&dport->port.lock, flags);
659 static const char *dz_type(struct uart_port *uport)
664 static void dz_release_port(struct uart_port *uport)
666 struct dz_mux *mux = to_dport(uport)->mux;
669 iounmap(uport->membase);
670 uport->membase = NULL;
672 map_guard = atomic_add_return(-1, &mux->map_guard);
674 release_mem_region(uport->mapbase, dec_kn_slot_size);
677 static int dz_map_port(struct uart_port *uport)
680 uport->membase = ioremap_nocache(uport->mapbase,
682 if (!uport->membase) {
683 printk(KERN_ERR "dz: Cannot map MMIO\n");
689 static int dz_request_port(struct uart_port *uport)
691 struct dz_mux *mux = to_dport(uport)->mux;
695 map_guard = atomic_add_return(1, &mux->map_guard);
696 if (map_guard == 1) {
697 if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
699 atomic_add(-1, &mux->map_guard);
701 "dz: Unable to reserve MMIO resource\n");
705 ret = dz_map_port(uport);
707 map_guard = atomic_add_return(-1, &mux->map_guard);
709 release_mem_region(uport->mapbase, dec_kn_slot_size);
715 static void dz_config_port(struct uart_port *uport, int flags)
717 struct dz_port *dport = to_dport(uport);
719 if (flags & UART_CONFIG_TYPE) {
720 if (dz_request_port(uport))
723 uport->type = PORT_DZ;
730 * Verify the new serial_struct (for TIOCSSERIAL).
732 static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
736 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
738 if (ser->irq != uport->irq)
743 static const struct uart_ops dz_ops = {
744 .tx_empty = dz_tx_empty,
745 .get_mctrl = dz_get_mctrl,
746 .set_mctrl = dz_set_mctrl,
747 .stop_tx = dz_stop_tx,
748 .start_tx = dz_start_tx,
749 .stop_rx = dz_stop_rx,
750 .break_ctl = dz_break_ctl,
751 .startup = dz_startup,
752 .shutdown = dz_shutdown,
753 .set_termios = dz_set_termios,
756 .release_port = dz_release_port,
757 .request_port = dz_request_port,
758 .config_port = dz_config_port,
759 .verify_port = dz_verify_port,
762 static void __init dz_init_ports(void)
764 static int first = 1;
772 if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
773 base = dec_kn_slot_base + KN01_DZ11;
775 base = dec_kn_slot_base + KN02_DZ11;
777 for (line = 0; line < DZ_NB_PORT; line++) {
778 struct dz_port *dport = &dz_mux.dport[line];
779 struct uart_port *uport = &dport->port;
781 dport->mux = &dz_mux;
783 uport->irq = dec_interrupt[DEC_IRQ_DZ11];
785 uport->iotype = UPIO_MEM;
786 uport->flags = UPF_BOOT_AUTOCONF;
787 uport->ops = &dz_ops;
789 uport->mapbase = base;
793 #ifdef CONFIG_SERIAL_DZ_CONSOLE
795 * -------------------------------------------------------------------
796 * dz_console_putchar() -- transmit a character
798 * Polled transmission. This is tricky. We need to mask transmit
799 * interrupts so that they do not interfere, enable the transmitter
800 * for the line requested and then wait till the transmit scanner
801 * requests data for this line. But it may request data for another
802 * line first, in which case we have to disable its transmitter and
803 * repeat waiting till our line pops up. Only then the character may
804 * be transmitted. Finally, the state of the transmitter mask is
805 * restored. Welcome to the world of PDP-11!
806 * -------------------------------------------------------------------
808 static void dz_console_putchar(struct uart_port *uport, int ch)
810 struct dz_port *dport = to_dport(uport);
812 unsigned short csr, tcr, trdy, mask;
815 spin_lock_irqsave(&dport->port.lock, flags);
816 csr = dz_in(dport, DZ_CSR);
817 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
818 tcr = dz_in(dport, DZ_TCR);
819 tcr |= 1 << dport->port.line;
821 dz_out(dport, DZ_TCR, mask);
823 spin_unlock_irqrestore(&dport->port.lock, flags);
826 trdy = dz_in(dport, DZ_CSR);
827 if (!(trdy & DZ_TRDY))
829 trdy = (trdy & DZ_TLINE) >> 8;
830 if (trdy == dport->port.line)
832 mask &= ~(1 << trdy);
833 dz_out(dport, DZ_TCR, mask);
838 if (loops) /* Cannot send otherwise. */
839 dz_out(dport, DZ_TDR, ch);
841 dz_out(dport, DZ_TCR, tcr);
842 dz_out(dport, DZ_CSR, csr);
846 * -------------------------------------------------------------------
847 * dz_console_print ()
849 * dz_console_print is registered for printk.
850 * The console must be locked when we get here.
851 * -------------------------------------------------------------------
853 static void dz_console_print(struct console *co,
857 struct dz_port *dport = &dz_mux.dport[co->index];
859 prom_printf((char *) str);
861 uart_console_write(&dport->port, str, count, dz_console_putchar);
864 static int __init dz_console_setup(struct console *co, char *options)
866 struct dz_port *dport = &dz_mux.dport[co->index];
867 struct uart_port *uport = &dport->port;
874 ret = dz_map_port(uport);
878 spin_lock_init(&dport->port.lock); /* For dz_pm(). */
884 uart_parse_options(options, &baud, &parity, &bits, &flow);
886 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
889 static struct uart_driver dz_reg;
890 static struct console dz_console = {
892 .write = dz_console_print,
893 .device = uart_console_device,
894 .setup = dz_console_setup,
895 .flags = CON_PRINTBUFFER,
900 static int __init dz_serial_console_init(void)
904 register_console(&dz_console);
910 console_initcall(dz_serial_console_init);
912 #define SERIAL_DZ_CONSOLE &dz_console
914 #define SERIAL_DZ_CONSOLE NULL
915 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
917 static struct uart_driver dz_reg = {
918 .owner = THIS_MODULE,
919 .driver_name = "serial",
924 .cons = SERIAL_DZ_CONSOLE,
927 static int __init dz_init(void)
934 printk("%s%s\n", dz_name, dz_version);
938 ret = uart_register_driver(&dz_reg);
942 for (i = 0; i < DZ_NB_PORT; i++)
943 uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
948 module_init(dz_init);