2 * dz.c: Serial port driver for DECStations equiped
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
7 * Email: olivier.lebaillif@ifrsys.com
10 * Changed IRQ to use Harald's dec internals interrupts.h
11 * removed base_addr code - moving address assignment to setup.c
12 * Changed name of dz_init to rs_init to be consistent with tc code
13 * [13-NOV-98] triemer fixed code to receive characters
14 * after patches by harald to irq code.
15 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
16 * field from "current" - somewhere between 2.1.121 and 2.1.131
17 Qua Jun 27 15:02:26 BRT 2001
18 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
20 * Parts (C) 1999 David Airlie, airlied@linux.ie
21 * [07-SEP-99] Bugfixes
23 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
24 * Converted to new serial core
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/console.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/serial_core.h>
37 #include <linux/serial.h>
39 #include <asm/bootinfo.h>
40 #include <asm/dec/interrupts.h>
41 #include <asm/dec/kn01.h>
42 #include <asm/dec/kn02.h>
43 #include <asm/dec/machtype.h>
44 #include <asm/dec/prom.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #define CONSOLE_LINE (3) /* for definition of struct console */
53 #define DZ_INTR_DEBUG 1
55 static char *dz_name = "DECstation DZ serial driver version ";
56 static char *dz_version = "1.02";
59 struct uart_port port;
63 static struct dz_port dz_ports[DZ_NB_PORT];
67 * debugging code to send out chars via prom
69 static void debug_console(const char *s, int count)
73 for (i = 0; i < count; i++) {
75 prom_printf("%c", 13);
76 prom_printf("%c", *s++);
82 * ------------------------------------------------------------
83 * dz_in () and dz_out ()
85 * These routines are used to access the registers of the DZ
86 * chip, hiding relocation differences between implementation.
87 * ------------------------------------------------------------
90 static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
92 volatile unsigned short *addr =
93 (volatile unsigned short *) (dport->port.membase + offset);
97 static inline void dz_out(struct dz_port *dport, unsigned offset,
100 volatile unsigned short *addr =
101 (volatile unsigned short *) (dport->port.membase + offset);
106 * ------------------------------------------------------------
107 * rs_stop () and rs_start ()
109 * These routines are called before setting or resetting
110 * tty->stopped. They enable or disable transmitter interrupts,
112 * ------------------------------------------------------------
115 static void dz_stop_tx(struct uart_port *uport)
117 struct dz_port *dport = (struct dz_port *)uport;
118 unsigned short tmp, mask = 1 << dport->port.line;
121 spin_lock_irqsave(&dport->port.lock, flags);
122 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
123 tmp &= ~mask; /* clear the TX flag */
124 dz_out(dport, DZ_TCR, tmp);
125 spin_unlock_irqrestore(&dport->port.lock, flags);
128 static void dz_start_tx(struct uart_port *uport)
130 struct dz_port *dport = (struct dz_port *)uport;
131 unsigned short tmp, mask = 1 << dport->port.line;
134 spin_lock_irqsave(&dport->port.lock, flags);
135 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
136 tmp |= mask; /* set the TX flag */
137 dz_out(dport, DZ_TCR, tmp);
138 spin_unlock_irqrestore(&dport->port.lock, flags);
141 static void dz_stop_rx(struct uart_port *uport)
143 struct dz_port *dport = (struct dz_port *)uport;
146 spin_lock_irqsave(&dport->port.lock, flags);
147 dport->cflag &= ~DZ_CREAD;
148 dz_out(dport, DZ_LPR, dport->cflag);
149 spin_unlock_irqrestore(&dport->port.lock, flags);
152 static void dz_enable_ms(struct uart_port *port)
158 * ------------------------------------------------------------
159 * Here starts the interrupt handling routines. All of the
160 * following subroutines are declared as inline and are folded
161 * into dz_interrupt. They were separated out for readability's
164 * Note: rs_interrupt() is a "fast" interrupt, which means that it
165 * runs with interrupts turned off. People who may want to modify
166 * rs_interrupt() should try to keep the interrupt handler as fast as
167 * possible. After you are done making modifications, it is not a bad
170 * make drivers/serial/dz.s
172 * and look at the resulting assemble code in dz.s.
174 * ------------------------------------------------------------
178 * ------------------------------------------------------------
181 * This routine deals with inputs from any lines.
182 * ------------------------------------------------------------
184 static inline void dz_receive_chars(struct dz_port *dport)
186 struct tty_struct *tty = NULL;
187 struct uart_icount *icount;
189 unsigned short status, tmp;
190 unsigned char ch, flag;
192 /* this code is going to be a problem...
193 the call to tty_flip_buffer is going to need
197 status = dz_in(dport, DZ_RBUF);
199 /* punt so we don't get duplicate characters */
200 if (!(status & DZ_DVAL))
204 ch = UCHAR(status); /* grab the char */
208 if (info->is_console) {
210 return; /* it's a break ... */
214 tty = dport->port.info->tty;/* now tty points to the proper dev */
215 icount = &dport->port.icount;
222 /* keep track of the statistics */
223 if (status & (DZ_OERR | DZ_FERR | DZ_PERR)) {
224 if (status & DZ_PERR) /* parity error */
226 else if (status & DZ_FERR) /* frame error */
228 if (status & DZ_OERR) /* overrun error */
231 /* check to see if we should ignore the character
232 and mask off conditions that should be ignored
235 if (status & dport->port.ignore_status_mask) {
240 /* mask off the error conditions we want to ignore */
241 tmp = status & dport->port.read_status_mask;
246 debug_console("PERR\n", 5);
248 } else if (tmp & DZ_FERR) {
251 debug_console("FERR\n", 5);
256 debug_console("OERR\n", 5);
258 tty_insert_flip_char(tty, ch, flag);
263 tty_insert_flip_char(tty, ch, flag);
265 } while (status & DZ_DVAL);
268 tty_flip_buffer_push(tty);
272 * ------------------------------------------------------------
275 * This routine deals with outputs to any lines.
276 * ------------------------------------------------------------
278 static inline void dz_transmit_chars(struct dz_port *dport)
280 struct circ_buf *xmit = &dport->port.info->xmit;
283 if (dport->port.x_char) { /* XON/XOFF chars */
284 dz_out(dport, DZ_TDR, dport->port.x_char);
285 dport->port.icount.tx++;
286 dport->port.x_char = 0;
289 /* if nothing to do or stopped or hardware stopped */
290 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
291 dz_stop_tx(&dport->port);
296 * if something to do ... (rember the dz has no output fifo so we go
297 * one char at a time :-<
299 tmp = xmit->buf[xmit->tail];
300 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
301 dz_out(dport, DZ_TDR, tmp);
302 dport->port.icount.tx++;
304 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
305 uart_write_wakeup(&dport->port);
308 if (uart_circ_empty(xmit))
309 dz_stop_tx(&dport->port);
313 * ------------------------------------------------------------
314 * check_modem_status ()
316 * Only valid for the MODEM line duh !
317 * ------------------------------------------------------------
319 static inline void check_modem_status(struct dz_port *dport)
321 unsigned short status;
323 /* if not ne modem line just return */
324 if (dport->port.line != DZ_MODEM)
327 status = dz_in(dport, DZ_MSR);
329 /* it's easy, since DSR2 is the only bit in the register */
331 dport->port.icount.dsr++;
335 * ------------------------------------------------------------
338 * this is the main interrupt routine for the DZ chip.
339 * It deals with the multiple ports.
340 * ------------------------------------------------------------
342 static irqreturn_t dz_interrupt(int irq, void *dev, struct pt_regs *regs)
344 struct dz_port *dport;
345 unsigned short status;
347 /* get the reason why we just got an irq */
348 status = dz_in((struct dz_port *)dev, DZ_CSR);
349 dport = &dz_ports[LINE(status)];
351 if (status & DZ_RDONE)
352 dz_receive_chars(dport);
354 if (status & DZ_TRDY)
355 dz_transmit_chars(dport);
357 /* FIXME: what about check modem status??? --rmk */
363 * -------------------------------------------------------------------
364 * Here ends the DZ interrupt routines.
365 * -------------------------------------------------------------------
368 static unsigned int dz_get_mctrl(struct uart_port *uport)
370 struct dz_port *dport = (struct dz_port *)uport;
371 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
373 if (dport->port.line == DZ_MODEM) {
375 * CHECKME: This is a guess from the other code... --rmk
377 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
384 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
386 struct dz_port *dport = (struct dz_port *)uport;
389 if (dport->port.line == DZ_MODEM) {
390 tmp = dz_in(dport, DZ_TCR);
391 if (mctrl & TIOCM_DTR)
392 tmp &= ~DZ_MODEM_DTR;
395 dz_out(dport, DZ_TCR, tmp);
400 * -------------------------------------------------------------------
403 * various initialization tasks
404 * -------------------------------------------------------------------
406 static int dz_startup(struct uart_port *uport)
408 struct dz_port *dport = (struct dz_port *)uport;
412 /* The dz lines for the mouse/keyboard must be
413 * opened using their respective drivers.
415 if ((dport->port.line == DZ_KEYBOARD) ||
416 (dport->port.line == DZ_MOUSE))
419 spin_lock_irqsave(&dport->port.lock, flags);
421 /* enable the interrupt and the scanning */
422 tmp = dz_in(dport, DZ_CSR);
423 tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
424 dz_out(dport, DZ_CSR, tmp);
426 spin_unlock_irqrestore(&dport->port.lock, flags);
432 * -------------------------------------------------------------------
435 * This routine will shutdown a serial port; interrupts are disabled, and
436 * DTR is dropped if the hangup on close termio flag is on.
437 * -------------------------------------------------------------------
439 static void dz_shutdown(struct uart_port *uport)
445 * get_lsr_info - get line status register info
447 * Purpose: Let user call ioctl() to get info when the UART physically
448 * is emptied. On bus types like RS485, the transmitter must
449 * release the bus after transmitting. This must be done when
450 * the transmit shift register is empty, not be done when the
451 * transmit holding register is empty. This functionality
452 * allows an RS485 driver to be written in user space.
454 static unsigned int dz_tx_empty(struct uart_port *uport)
456 struct dz_port *dport = (struct dz_port *)uport;
457 unsigned short status = dz_in(dport, DZ_LPR);
459 /* FIXME: this appears to be obviously broken --rmk. */
460 return status ? TIOCSER_TEMT : 0;
463 static void dz_break_ctl(struct uart_port *uport, int break_state)
465 struct dz_port *dport = (struct dz_port *)uport;
467 unsigned short tmp, mask = 1 << uport->line;
469 spin_lock_irqsave(&uport->lock, flags);
470 tmp = dz_in(dport, DZ_TCR);
475 dz_out(dport, DZ_TCR, tmp);
476 spin_unlock_irqrestore(&uport->lock, flags);
479 static void dz_set_termios(struct uart_port *uport, struct termios *termios,
480 struct termios *old_termios)
482 struct dz_port *dport = (struct dz_port *)uport;
484 unsigned int cflag, baud;
486 cflag = dport->port.line;
488 switch (termios->c_cflag & CSIZE) {
503 if (termios->c_cflag & CSTOPB)
505 if (termios->c_cflag & PARENB)
507 if (termios->c_cflag & PARODD)
510 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
559 if (termios->c_cflag & CREAD)
562 spin_lock_irqsave(&dport->port.lock, flags);
564 dz_out(dport, DZ_LPR, cflag);
565 dport->cflag = cflag;
567 /* setup accept flag */
568 dport->port.read_status_mask = DZ_OERR;
569 if (termios->c_iflag & INPCK)
570 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
572 /* characters to ignore */
573 uport->ignore_status_mask = 0;
574 if (termios->c_iflag & IGNPAR)
575 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
577 spin_unlock_irqrestore(&dport->port.lock, flags);
580 static const char *dz_type(struct uart_port *port)
585 static void dz_release_port(struct uart_port *port)
590 static int dz_request_port(struct uart_port *port)
595 static void dz_config_port(struct uart_port *port, int flags)
597 if (flags & UART_CONFIG_TYPE)
598 port->type = PORT_DZ;
602 * verify the new serial_struct (for TIOCSSERIAL).
604 static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
607 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
609 if (ser->irq != port->irq)
614 static struct uart_ops dz_ops = {
615 .tx_empty = dz_tx_empty,
616 .get_mctrl = dz_get_mctrl,
617 .set_mctrl = dz_set_mctrl,
618 .stop_tx = dz_stop_tx,
619 .start_tx = dz_start_tx,
620 .stop_rx = dz_stop_rx,
621 .enable_ms = dz_enable_ms,
622 .break_ctl = dz_break_ctl,
623 .startup = dz_startup,
624 .shutdown = dz_shutdown,
625 .set_termios = dz_set_termios,
627 .release_port = dz_release_port,
628 .request_port = dz_request_port,
629 .config_port = dz_config_port,
630 .verify_port = dz_verify_port,
633 static void __init dz_init_ports(void)
635 static int first = 1;
636 struct dz_port *dport;
644 if (mips_machtype == MACH_DS23100 ||
645 mips_machtype == MACH_DS5100)
646 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
648 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
650 for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
651 spin_lock_init(&dport->port.lock);
652 dport->port.membase = (char *) base;
653 dport->port.iotype = SERIAL_IO_PORT;
654 dport->port.irq = dec_interrupt[DEC_IRQ_DZ11];
655 dport->port.line = i;
656 dport->port.fifosize = 1;
657 dport->port.ops = &dz_ops;
658 dport->port.flags = UPF_BOOT_AUTOCONF;
662 static void dz_reset(struct dz_port *dport)
664 dz_out(dport, DZ_CSR, DZ_CLR);
666 while (dz_in(dport, DZ_CSR) & DZ_CLR);
667 /* FIXME: cpu_relax? */
671 /* enable scanning */
672 dz_out(dport, DZ_CSR, DZ_MSE);
675 #ifdef CONFIG_SERIAL_DZ_CONSOLE
676 static void dz_console_put_char(struct dz_port *dport, unsigned char ch)
680 unsigned short tmp = ch;
681 /* this code sends stuff out to serial device - spinning its
682 wheels and waiting. */
684 spin_lock_irqsave(&dport->port.lock, flags);
686 /* spin our wheels */
687 while (((dz_in(dport, DZ_CSR) & DZ_TRDY) != DZ_TRDY) && loops--)
688 /* FIXME: cpu_relax, udelay? --rmk */
691 /* Actually transmit the character. */
692 dz_out(dport, DZ_TDR, tmp);
694 spin_unlock_irqrestore(&dport->port.lock, flags);
697 * -------------------------------------------------------------------
698 * dz_console_print ()
700 * dz_console_print is registered for printk.
701 * The console must be locked when we get here.
702 * -------------------------------------------------------------------
704 static void dz_console_print(struct console *cons,
708 struct dz_port *dport = &dz_ports[CONSOLE_LINE];
710 prom_printf((char *) str);
714 dz_console_put_char(dport, '\r');
715 dz_console_put_char(dport, *str++);
719 static int __init dz_console_setup(struct console *co, char *options)
721 struct dz_port *dport = &dz_ports[CONSOLE_LINE];
727 unsigned short mask, tmp;
730 uart_parse_options(options, &baud, &parity, &bits, &flow);
734 ret = uart_set_options(&dport->port, co, baud, parity, bits, flow);
736 mask = 1 << dport->port.line;
737 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
739 tmp |= mask; /* set the TX flag */
740 dz_out(dport, DZ_TCR, tmp);
747 static struct console dz_sercons =
750 .write = dz_console_print,
751 .device = uart_console_device,
752 .setup = dz_console_setup,
753 .flags = CON_CONSDEV | CON_PRINTBUFFER,
754 .index = CONSOLE_LINE,
757 void __init dz_serial_console_init(void)
761 register_console(&dz_sercons);
764 #define SERIAL_DZ_CONSOLE &dz_sercons
766 #define SERIAL_DZ_CONSOLE NULL
767 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
769 static struct uart_driver dz_reg = {
770 .owner = THIS_MODULE,
771 .driver_name = "serial",
773 .dev_name = "tts/%d",
775 .dev_name = "ttyS%d",
780 .cons = SERIAL_DZ_CONSOLE,
783 int __init dz_init(void)
788 printk("%s%s\n", dz_name, dz_version);
795 #ifndef CONFIG_SERIAL_DZ_CONSOLE
797 dz_reset(&dz_ports[0]);
800 /* order matters here... the trick is that flags
801 is updated... in request_irq - to immediatedly obliterate
803 restore_flags(flags);
805 if (request_irq(dz_ports[0].port.irq, dz_interrupt,
806 SA_INTERRUPT, "DZ", &dz_ports[0]))
807 panic("Unable to register DZ interrupt");
809 ret = uart_register_driver(&dz_reg);
813 for (i = 0; i < DZ_NB_PORT; i++)
814 uart_add_one_port(&dz_reg, &dz_ports[i].port);
819 MODULE_DESCRIPTION("DECstation DZ serial driver");
820 MODULE_LICENSE("GPL");