Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[sfrench/cifs-2.6.git] / drivers / serial / dz.c
index 8a98aae80e228ffd8afc2c57b868b8394aa7d765..116211fcd36fc196ff12de4aeb36ee8e6194031f 100644 (file)
@@ -1,11 +1,13 @@
 /*
- * dz.c: Serial port driver for DECStations equiped
+ * dz.c: Serial port driver for DECstations equipped
  *       with the DZ chipset.
  *
  * Copyright (C) 1998 Olivier A. D. Lebaillif
  *
  * Email: olivier.lebaillif@ifrsys.com
  *
+ * Copyright (C) 2004, 2006, 2007  Maciej W. Rozycki
+ *
  * [31-AUG-98] triemer
  * Changed IRQ to use Harald's dec internals interrupts.h
  * removed base_addr code - moving address assignment to setup.c
 
 #undef DEBUG_DZ
 
-#include <linux/module.h>
-#include <linux/interrupt.h>
-#include <linux/init.h>
+#if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
+#define SUPPORT_SYSRQ
+#endif
+
+#include <linux/bitops.h>
+#include <linux/compiler.h>
 #include <linux/console.h>
-#include <linux/tty.h>
-#include <linux/tty_flip.h>
-#include <linux/serial_core.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/major.h>
+#include <linux/module.h>
 #include <linux/serial.h>
+#include <linux/serial_core.h>
+#include <linux/sysrq.h>
+#include <linux/tty.h>
 
+#include <asm/atomic.h>
 #include <asm/bootinfo.h>
+#include <asm/io.h>
+#include <asm/system.h>
+
 #include <asm/dec/interrupts.h>
 #include <asm/dec/kn01.h>
 #include <asm/dec/kn02.h>
 #include <asm/dec/machtype.h>
 #include <asm/dec/prom.h>
-#include <asm/irq.h>
-#include <asm/system.h>
-#include <asm/uaccess.h>
-
-#define CONSOLE_LINE (3)       /* for definition of struct console */
+#include <asm/dec/system.h>
 
 #include "dz.h"
 
-#define DZ_INTR_DEBUG 1
 
-static char *dz_name = "DECstation DZ serial driver version ";
-static char *dz_version = "1.02";
+MODULE_DESCRIPTION("DECstation DZ serial driver");
+MODULE_LICENSE("GPL");
+
+
+static char dz_name[] __initdata = "DECstation DZ serial driver version ";
+static char dz_version[] __initdata = "1.04";
 
 struct dz_port {
+       struct dz_mux           *mux;
        struct uart_port        port;
        unsigned int            cflag;
 };
 
-static struct dz_port dz_ports[DZ_NB_PORT];
+struct dz_mux {
+       struct dz_port          dport[DZ_NB_PORT];
+       atomic_t                map_guard;
+       atomic_t                irq_guard;
+       int                     initialised;
+};
 
-#ifdef DEBUG_DZ
-/*
- * debugging code to send out chars via prom
- */
-static void debug_console(const char *s, int count)
-{
-       unsigned i;
+static struct dz_mux dz_mux;
 
-       for (i = 0; i < count; i++) {
-               if (*s == 10)
-                       prom_printf("%c", 13);
-               prom_printf("%c", *s++);
-       }
+static inline struct dz_port *to_dport(struct uart_port *uport)
+{
+       return container_of(uport, struct dz_port, port);
 }
-#endif
 
 /*
  * ------------------------------------------------------------
@@ -86,19 +99,18 @@ static void debug_console(const char *s, int count)
  * ------------------------------------------------------------
  */
 
-static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
+static u16 dz_in(struct dz_port *dport, unsigned offset)
 {
-       volatile unsigned short *addr =
-               (volatile unsigned short *) (dport->port.membase + offset);
-       return *addr;
+       void __iomem *addr = dport->port.membase + offset;
+
+       return readw(addr);
 }
 
-static inline void dz_out(struct dz_port *dport, unsigned offset,
-                          unsigned short value)
+static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
 {
-       volatile unsigned short *addr =
-               (volatile unsigned short *) (dport->port.membase + offset);
-       *addr = value;
+       void __iomem *addr = dport->port.membase + offset;
+
+       writew(value, addr);
 }
 
 /*
@@ -113,56 +125,47 @@ static inline void dz_out(struct dz_port *dport, unsigned offset,
 
 static void dz_stop_tx(struct uart_port *uport)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
-       unsigned short tmp, mask = 1 << dport->port.line;
-       unsigned long flags;
+       struct dz_port *dport = to_dport(uport);
+       u16 tmp, mask = 1 << dport->port.line;
 
-       spin_lock_irqsave(&dport->port.lock, flags);
        tmp = dz_in(dport, DZ_TCR);     /* read the TX flag */
        tmp &= ~mask;                   /* clear the TX flag */
        dz_out(dport, DZ_TCR, tmp);
-       spin_unlock_irqrestore(&dport->port.lock, flags);
 }
 
 static void dz_start_tx(struct uart_port *uport)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
-       unsigned short tmp, mask = 1 << dport->port.line;
-       unsigned long flags;
+       struct dz_port *dport = to_dport(uport);
+       u16 tmp, mask = 1 << dport->port.line;
 
-       spin_lock_irqsave(&dport->port.lock, flags);
        tmp = dz_in(dport, DZ_TCR);     /* read the TX flag */
        tmp |= mask;                    /* set the TX flag */
        dz_out(dport, DZ_TCR, tmp);
-       spin_unlock_irqrestore(&dport->port.lock, flags);
 }
 
 static void dz_stop_rx(struct uart_port *uport)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
-       unsigned long flags;
+       struct dz_port *dport = to_dport(uport);
 
-       spin_lock_irqsave(&dport->port.lock, flags);
-       dport->cflag &= ~DZ_CREAD;
+       dport->cflag &= ~DZ_RXENAB;
        dz_out(dport, DZ_LPR, dport->cflag);
-       spin_unlock_irqrestore(&dport->port.lock, flags);
 }
 
-static void dz_enable_ms(struct uart_port *port)
+static void dz_enable_ms(struct uart_port *uport)
 {
        /* nothing to do */
 }
 
 /*
  * ------------------------------------------------------------
- * Here starts the interrupt handling routines.  All of the
- * following subroutines are declared as inline and are folded
- * into dz_interrupt.  They were separated out for readability's
- * sake.
  *
- * Note: rs_interrupt() is a "fast" interrupt, which means that it
+ * Here start the interrupt handling routines.  All of the following
+ * subroutines are declared as inline and are folded into
+ * dz_interrupt.  They were separated out for readability's sake.
+ *
+ * Note: dz_interrupt() is a "fast" interrupt, which means that it
  * runs with interrupts turned off.  People who may want to modify
- * rs_interrupt() should try to keep the interrupt handler as fast as
+ * dz_interrupt() should try to keep the interrupt handler as fast as
  * possible.  After you are done making modifications, it is not a bad
  * idea to do:
  *
@@ -180,92 +183,73 @@ static void dz_enable_ms(struct uart_port *port)
  * This routine deals with inputs from any lines.
  * ------------------------------------------------------------
  */
-static inline void dz_receive_chars(struct dz_port *dport)
+static inline void dz_receive_chars(struct dz_mux *mux)
 {
+       struct uart_port *uport;
+       struct dz_port *dport = &mux->dport[0];
        struct tty_struct *tty = NULL;
        struct uart_icount *icount;
-       int ignore = 0;
-       unsigned short status, tmp;
+       int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
        unsigned char ch, flag;
+       u16 status;
+       int i;
 
-       /* this code is going to be a problem...
-          the call to tty_flip_buffer is going to need
-          to be rethought...
-        */
-       do {
-               status = dz_in(dport, DZ_RBUF);
-
-               /* punt so we don't get duplicate characters */
-               if (!(status & DZ_DVAL))
-                       goto ignore_char;
-
+       while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
+               dport = &mux->dport[LINE(status)];
+               uport = &dport->port;
+               tty = uport->info->tty;         /* point to the proper dev */
 
-               ch = UCHAR(status);     /* grab the char */
+               ch = UCHAR(status);             /* grab the char */
                flag = TTY_NORMAL;
 
-#if 0
-               if (info->is_console) {
-                       if (ch == 0)
-                               return;         /* it's a break ... */
-               }
-#endif
-
-               tty = dport->port.info->tty;/* now tty points to the proper dev */
-               icount = &dport->port.icount;
-
-               if (!tty)
-                       break;
-
+               icount = &uport->icount;
                icount->rx++;
 
-               /* keep track of the statistics */
-               if (status & (DZ_OERR | DZ_FERR | DZ_PERR)) {
-                       if (status & DZ_PERR)   /* parity error */
-                               icount->parity++;
-                       else if (status & DZ_FERR)      /* frame error */
-                               icount->frame++;
-                       if (status & DZ_OERR)   /* overrun error */
-                               icount->overrun++;
+               if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
 
-                       /*  check to see if we should ignore the character
-                          and mask off conditions that should be ignored
+                       /*
+                        * There is no separate BREAK status bit, so treat
+                        * null characters with framing errors as BREAKs;
+                        * normally, otherwise.  For this move the Framing
+                        * Error bit to a simulated BREAK bit.
                         */
-
-                       if (status & dport->port.ignore_status_mask) {
-                               if (++ignore > 100)
-                                       break;
-                               goto ignore_char;
+                       if (!ch) {
+                               status |= (status & DZ_FERR) >>
+                                         (ffs(DZ_FERR) - ffs(DZ_BREAK));
+                               status &= ~DZ_FERR;
                        }
-                       /* mask off the error conditions we want to ignore */
-                       tmp = status & dport->port.read_status_mask;
 
-                       if (tmp & DZ_PERR) {
-                               flag = TTY_PARITY;
-#ifdef DEBUG_DZ
-                               debug_console("PERR\n", 5);
-#endif
-                       } else if (tmp & DZ_FERR) {
+                       /* Handle SysRq/SAK & keep track of the statistics. */
+                       if (status & DZ_BREAK) {
+                               icount->brk++;
+                               if (uart_handle_break(uport))
+                                       continue;
+                       } else if (status & DZ_FERR)
+                               icount->frame++;
+                       else if (status & DZ_PERR)
+                               icount->parity++;
+                       if (status & DZ_OERR)
+                               icount->overrun++;
+
+                       status &= uport->read_status_mask;
+                       if (status & DZ_BREAK)
+                               flag = TTY_BREAK;
+                       else if (status & DZ_FERR)
                                flag = TTY_FRAME;
-#ifdef DEBUG_DZ
-                               debug_console("FERR\n", 5);
-#endif
-                       }
-                       if (tmp & DZ_OERR) {
-#ifdef DEBUG_DZ
-                               debug_console("OERR\n", 5);
-#endif
-                               tty_insert_flip_char(tty, ch, flag);
-                               ch = 0;
-                               flag = TTY_OVERRUN;
-                       }
+                       else if (status & DZ_PERR)
+                               flag = TTY_PARITY;
+
                }
-               tty_insert_flip_char(tty, ch, flag);
-             ignore_char:
-                       ;
-       } while (status & DZ_DVAL);
 
-       if (tty)
-               tty_flip_buffer_push(tty);
+               if (uart_handle_sysrq_char(uport, ch))
+                       continue;
+
+               uart_insert_char(uport, status, DZ_OERR, ch, flag);
+               lines_rx[LINE(status)] = 1;
+       }
+       for (i = 0; i < DZ_NB_PORT; i++)
+               if (lines_rx[i])
+                       tty_flip_buffer_push(mux->dport[i].port.info->tty);
 }
 
 /*
@@ -275,26 +259,34 @@ static inline void dz_receive_chars(struct dz_port *dport)
  * This routine deals with outputs to any lines.
  * ------------------------------------------------------------
  */
-static inline void dz_transmit_chars(struct dz_port *dport)
+static inline void dz_transmit_chars(struct dz_mux *mux)
 {
-       struct circ_buf *xmit = &dport->port.info->xmit;
+       struct dz_port *dport = &mux->dport[0];
+       struct circ_buf *xmit;
        unsigned char tmp;
+       u16 status;
+
+       status = dz_in(dport, DZ_CSR);
+       dport = &mux->dport[LINE(status)];
+       xmit = &dport->port.info->xmit;
 
-       if (dport->port.x_char) {       /* XON/XOFF chars */
+       if (dport->port.x_char) {               /* XON/XOFF chars */
                dz_out(dport, DZ_TDR, dport->port.x_char);
                dport->port.icount.tx++;
                dport->port.x_char = 0;
                return;
        }
-       /* if nothing to do or stopped or hardware stopped */
+       /* If nothing to do or stopped or hardware stopped. */
        if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
+               spin_lock(&dport->port.lock);
                dz_stop_tx(&dport->port);
+               spin_unlock(&dport->port.lock);
                return;
        }
 
        /*
-        * if something to do ... (rember the dz has no output fifo so we go
-        * one char at a time :-<
+        * If something to do... (remember the dz has no output fifo,
+        * so we go one char at a time) :-<
         */
        tmp = xmit->buf[xmit->tail];
        xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
@@ -304,23 +296,32 @@ static inline void dz_transmit_chars(struct dz_port *dport)
        if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
                uart_write_wakeup(&dport->port);
 
-       /* Are we done */
-       if (uart_circ_empty(xmit))
+       /* Are we are done. */
+       if (uart_circ_empty(xmit)) {
+               spin_lock(&dport->port.lock);
                dz_stop_tx(&dport->port);
+               spin_unlock(&dport->port.lock);
+       }
 }
 
 /*
  * ------------------------------------------------------------
- * check_modem_status ()
+ * check_modem_status()
  *
- * Only valid for the MODEM line duh !
+ * DS 3100 & 5100: Only valid for the MODEM line, duh!
+ * DS 5000/200: Valid for the MODEM and PRINTER line.
  * ------------------------------------------------------------
  */
 static inline void check_modem_status(struct dz_port *dport)
 {
-       unsigned short status;
+       /*
+        * FIXME:
+        * 1. No status change interrupt; use a timer.
+        * 2. Handle the 3100/5000 as appropriate. --macro
+        */
+       u16 status;
 
-       /* if not ne modem line just return */
+       /* If not the modem line just return.  */
        if (dport->port.line != DZ_MODEM)
                return;
 
@@ -339,22 +340,20 @@ static inline void check_modem_status(struct dz_port *dport)
  * It deals with the multiple ports.
  * ------------------------------------------------------------
  */
-static irqreturn_t dz_interrupt(int irq, void *dev, struct pt_regs *regs)
+static irqreturn_t dz_interrupt(int irq, void *dev_id)
 {
-       struct dz_port *dport;
-       unsigned short status;
+       struct dz_mux *mux = dev_id;
+       struct dz_port *dport = &mux->dport[0];
+       u16 status;
 
        /* get the reason why we just got an irq */
-       status = dz_in((struct dz_port *)dev, DZ_CSR);
-       dport = &dz_ports[LINE(status)];
-
-       if (status & DZ_RDONE)
-               dz_receive_chars(dport);
+       status = dz_in(dport, DZ_CSR);
 
-       if (status & DZ_TRDY)
-               dz_transmit_chars(dport);
+       if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
+               dz_receive_chars(mux);
 
-       /* FIXME: what about check modem status??? --rmk */
+       if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
+               dz_transmit_chars(mux);
 
        return IRQ_HANDLED;
 }
@@ -367,13 +366,13 @@ static irqreturn_t dz_interrupt(int irq, void *dev, struct pt_regs *regs)
 
 static unsigned int dz_get_mctrl(struct uart_port *uport)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
+       /*
+        * FIXME: Handle the 3100/5000 as appropriate. --macro
+        */
+       struct dz_port *dport = to_dport(uport);
        unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
 
        if (dport->port.line == DZ_MODEM) {
-               /*
-                * CHECKME: This is a guess from the other code... --rmk
-                */
                if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
                        mctrl &= ~TIOCM_DSR;
        }
@@ -383,8 +382,11 @@ static unsigned int dz_get_mctrl(struct uart_port *uport)
 
 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
-       unsigned short tmp;
+       /*
+        * FIXME: Handle the 3100/5000 as appropriate. --macro
+        */
+       struct dz_port *dport = to_dport(uport);
+       u16 tmp;
 
        if (dport->port.line == DZ_MODEM) {
                tmp = dz_in(dport, DZ_TCR);
@@ -405,22 +407,30 @@ static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
  */
 static int dz_startup(struct uart_port *uport)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
+       struct dz_port *dport = to_dport(uport);
+       struct dz_mux *mux = dport->mux;
        unsigned long flags;
-       unsigned short tmp;
+       int irq_guard;
+       int ret;
+       u16 tmp;
 
-       /* The dz lines for the mouse/keyboard must be
-        * opened using their respective drivers.
-        */
-       if ((dport->port.line == DZ_KEYBOARD) ||
-           (dport->port.line == DZ_MOUSE))
-               return -ENODEV;
+       irq_guard = atomic_add_return(1, &mux->irq_guard);
+       if (irq_guard != 1)
+               return 0;
+
+       ret = request_irq(dport->port.irq, dz_interrupt,
+                         IRQF_SHARED, "dz", mux);
+       if (ret) {
+               atomic_add(-1, &mux->irq_guard);
+               printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
+               return ret;
+       }
 
        spin_lock_irqsave(&dport->port.lock, flags);
 
-       /* enable the interrupt and the scanning */
+       /* Enable interrupts.  */
        tmp = dz_in(dport, DZ_CSR);
-       tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
+       tmp |= DZ_RIE | DZ_TIE;
        dz_out(dport, DZ_CSR, tmp);
 
        spin_unlock_irqrestore(&dport->port.lock, flags);
@@ -438,11 +448,30 @@ static int dz_startup(struct uart_port *uport)
  */
 static void dz_shutdown(struct uart_port *uport)
 {
-       dz_stop_tx(uport);
+       struct dz_port *dport = to_dport(uport);
+       struct dz_mux *mux = dport->mux;
+       unsigned long flags;
+       int irq_guard;
+       u16 tmp;
+
+       spin_lock_irqsave(&dport->port.lock, flags);
+       dz_stop_tx(&dport->port);
+       spin_unlock_irqrestore(&dport->port.lock, flags);
+
+       irq_guard = atomic_add_return(-1, &mux->irq_guard);
+       if (!irq_guard) {
+               /* Disable interrupts.  */
+               tmp = dz_in(dport, DZ_CSR);
+               tmp &= ~(DZ_RIE | DZ_TIE);
+               dz_out(dport, DZ_CSR, tmp);
+
+               free_irq(dport->port.irq, mux);
+       }
 }
 
 /*
- * get_lsr_info - get line status register info
+ * -------------------------------------------------------------------
+ * dz_tx_empty() -- get the transmitter empty status
  *
  * Purpose: Let user call ioctl() to get info when the UART physically
  *          is emptied.  On bus types like RS485, the transmitter must
@@ -450,21 +479,28 @@ static void dz_shutdown(struct uart_port *uport)
  *          the transmit shift register is empty, not be done when the
  *          transmit holding register is empty.  This functionality
  *          allows an RS485 driver to be written in user space.
+ * -------------------------------------------------------------------
  */
 static unsigned int dz_tx_empty(struct uart_port *uport)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
-       unsigned short status = dz_in(dport, DZ_LPR);
+       struct dz_port *dport = to_dport(uport);
+       unsigned short tmp, mask = 1 << dport->port.line;
+
+       tmp = dz_in(dport, DZ_TCR);
+       tmp &= mask;
 
-       /* FIXME: this appears to be obviously broken --rmk. */
-       return status ? TIOCSER_TEMT : 0;
+       return tmp ? 0 : TIOCSER_TEMT;
 }
 
 static void dz_break_ctl(struct uart_port *uport, int break_state)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
+       /*
+        * FIXME: Can't access BREAK bits in TDR easily;
+        * reuse the code for polled TX. --macro
+        */
+       struct dz_port *dport = to_dport(uport);
        unsigned long flags;
-       unsigned short tmp, mask = 1 << uport->line;
+       unsigned short tmp, mask = 1 << dport->port.line;
 
        spin_lock_irqsave(&uport->lock, flags);
        tmp = dz_in(dport, DZ_TCR);
@@ -476,12 +512,69 @@ static void dz_break_ctl(struct uart_port *uport, int break_state)
        spin_unlock_irqrestore(&uport->lock, flags);
 }
 
-static void dz_set_termios(struct uart_port *uport, struct termios *termios,
-                          struct termios *old_termios)
+static int dz_encode_baud_rate(unsigned int baud)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
+       switch (baud) {
+       case 50:
+               return DZ_B50;
+       case 75:
+               return DZ_B75;
+       case 110:
+               return DZ_B110;
+       case 134:
+               return DZ_B134;
+       case 150:
+               return DZ_B150;
+       case 300:
+               return DZ_B300;
+       case 600:
+               return DZ_B600;
+       case 1200:
+               return DZ_B1200;
+       case 1800:
+               return DZ_B1800;
+       case 2000:
+               return DZ_B2000;
+       case 2400:
+               return DZ_B2400;
+       case 3600:
+               return DZ_B3600;
+       case 4800:
+               return DZ_B4800;
+       case 7200:
+               return DZ_B7200;
+       case 9600:
+               return DZ_B9600;
+       default:
+               return -1;
+       }
+}
+
+
+static void dz_reset(struct dz_port *dport)
+{
+       struct dz_mux *mux = dport->mux;
+
+       if (mux->initialised)
+               return;
+
+       dz_out(dport, DZ_CSR, DZ_CLR);
+       while (dz_in(dport, DZ_CSR) & DZ_CLR);
+       iob();
+
+       /* Enable scanning.  */
+       dz_out(dport, DZ_CSR, DZ_MSE);
+
+       mux->initialised = 1;
+}
+
+static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
+                          struct ktermios *old_termios)
+{
+       struct dz_port *dport = to_dport(uport);
        unsigned long flags;
        unsigned int cflag, baud;
+       int bflag;
 
        cflag = dport->port.line;
 
@@ -508,59 +601,25 @@ static void dz_set_termios(struct uart_port *uport, struct termios *termios,
                cflag |= DZ_PARODD;
 
        baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
-       switch (baud) {
-       case 50:
-               cflag |= DZ_B50;
-               break;
-       case 75:
-               cflag |= DZ_B75;
-               break;
-       case 110:
-               cflag |= DZ_B110;
-               break;
-       case 134:
-               cflag |= DZ_B134;
-               break;
-       case 150:
-               cflag |= DZ_B150;
-               break;
-       case 300:
-               cflag |= DZ_B300;
-               break;
-       case 600:
-               cflag |= DZ_B600;
-               break;
-       case 1200:
-               cflag |= DZ_B1200;
-               break;
-       case 1800:
-               cflag |= DZ_B1800;
-               break;
-       case 2000:
-               cflag |= DZ_B2000;
-               break;
-       case 2400:
-               cflag |= DZ_B2400;
-               break;
-       case 3600:
-               cflag |= DZ_B3600;
-               break;
-       case 4800:
-               cflag |= DZ_B4800;
-               break;
-       case 7200:
-               cflag |= DZ_B7200;
-               break;
-       case 9600:
-       default:
-               cflag |= DZ_B9600;
+       bflag = dz_encode_baud_rate(baud);
+       if (bflag < 0)  {                       /* Try to keep unchanged.  */
+               baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
+               bflag = dz_encode_baud_rate(baud);
+               if (bflag < 0)  {               /* Resort to 9600.  */
+                       baud = 9600;
+                       bflag = DZ_B9600;
+               }
+               tty_termios_encode_baud_rate(termios, baud, baud);
        }
+       cflag |= bflag;
 
        if (termios->c_cflag & CREAD)
                cflag |= DZ_RXENAB;
 
        spin_lock_irqsave(&dport->port.lock, flags);
 
+       uart_update_timeout(uport, termios->c_cflag, baud);
+
        dz_out(dport, DZ_LPR, cflag);
        dport->cflag = cflag;
 
@@ -568,45 +627,101 @@ static void dz_set_termios(struct uart_port *uport, struct termios *termios,
        dport->port.read_status_mask = DZ_OERR;
        if (termios->c_iflag & INPCK)
                dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
+       if (termios->c_iflag & (BRKINT | PARMRK))
+               dport->port.read_status_mask |= DZ_BREAK;
 
        /* characters to ignore */
        uport->ignore_status_mask = 0;
+       if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
+               dport->port.ignore_status_mask |= DZ_OERR;
        if (termios->c_iflag & IGNPAR)
                dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
+       if (termios->c_iflag & IGNBRK)
+               dport->port.ignore_status_mask |= DZ_BREAK;
 
        spin_unlock_irqrestore(&dport->port.lock, flags);
 }
 
-static const char *dz_type(struct uart_port *port)
+static const char *dz_type(struct uart_port *uport)
 {
        return "DZ";
 }
 
-static void dz_release_port(struct uart_port *port)
+static void dz_release_port(struct uart_port *uport)
 {
-       /* nothing to do */
+       struct dz_mux *mux = to_dport(uport)->mux;
+       int map_guard;
+
+       iounmap(uport->membase);
+       uport->membase = NULL;
+
+       map_guard = atomic_add_return(-1, &mux->map_guard);
+       if (!map_guard)
+               release_mem_region(uport->mapbase, dec_kn_slot_size);
+}
+
+static int dz_map_port(struct uart_port *uport)
+{
+       if (!uport->membase)
+               uport->membase = ioremap_nocache(uport->mapbase,
+                                                dec_kn_slot_size);
+       if (!uport->membase) {
+               printk(KERN_ERR "dz: Cannot map MMIO\n");
+               return -ENOMEM;
+       }
+       return 0;
 }
 
-static int dz_request_port(struct uart_port *port)
+static int dz_request_port(struct uart_port *uport)
 {
+       struct dz_mux *mux = to_dport(uport)->mux;
+       int map_guard;
+       int ret;
+
+       map_guard = atomic_add_return(1, &mux->map_guard);
+       if (map_guard == 1) {
+               if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
+                                       "dz")) {
+                       atomic_add(-1, &mux->map_guard);
+                       printk(KERN_ERR
+                              "dz: Unable to reserve MMIO resource\n");
+                       return -EBUSY;
+               }
+       }
+       ret = dz_map_port(uport);
+       if (ret) {
+               map_guard = atomic_add_return(-1, &mux->map_guard);
+               if (!map_guard)
+                       release_mem_region(uport->mapbase, dec_kn_slot_size);
+               return ret;
+       }
        return 0;
 }
 
-static void dz_config_port(struct uart_port *port, int flags)
+static void dz_config_port(struct uart_port *uport, int flags)
 {
-       if (flags & UART_CONFIG_TYPE)
-               port->type = PORT_DZ;
+       struct dz_port *dport = to_dport(uport);
+
+       if (flags & UART_CONFIG_TYPE) {
+               if (dz_request_port(uport))
+                       return;
+
+               uport->type = PORT_DZ;
+
+               dz_reset(dport);
+       }
 }
 
 /*
- * verify the new serial_struct (for TIOCSSERIAL).
+ * Verify the new serial_struct (for TIOCSSERIAL).
  */
-static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
+static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
 {
        int ret = 0;
+
        if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
                ret = -EINVAL;
-       if (ser->irq != port->irq)
+       if (ser->irq != uport->irq)
                ret = -EINVAL;
        return ret;
 }
@@ -633,66 +748,84 @@ static struct uart_ops dz_ops = {
 static void __init dz_init_ports(void)
 {
        static int first = 1;
-       struct dz_port *dport;
        unsigned long base;
-       int i;
+       int line;
 
        if (!first)
                return;
        first = 0;
 
-       if (mips_machtype == MACH_DS23100 ||
-           mips_machtype == MACH_DS5100)
-               base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
+       if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
+               base = dec_kn_slot_base + KN01_DZ11;
        else
-               base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
-
-       for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
-               spin_lock_init(&dport->port.lock);
-               dport->port.membase     = (char *) base;
-               dport->port.iotype      = UPIO_PORT;
-               dport->port.irq         = dec_interrupt[DEC_IRQ_DZ11];
-               dport->port.line        = i;
-               dport->port.fifosize    = 1;
-               dport->port.ops         = &dz_ops;
-               dport->port.flags       = UPF_BOOT_AUTOCONF;
-       }
-}
-
-static void dz_reset(struct dz_port *dport)
-{
-       dz_out(dport, DZ_CSR, DZ_CLR);
+               base = dec_kn_slot_base + KN02_DZ11;
 
-       while (dz_in(dport, DZ_CSR) & DZ_CLR);
-               /* FIXME: cpu_relax? */
+       for (line = 0; line < DZ_NB_PORT; line++) {
+               struct dz_port *dport = &dz_mux.dport[line];
+               struct uart_port *uport = &dport->port;
 
-       iob();
+               dport->mux      = &dz_mux;
 
-       /* enable scanning */
-       dz_out(dport, DZ_CSR, DZ_MSE);
+               uport->irq      = dec_interrupt[DEC_IRQ_DZ11];
+               uport->fifosize = 1;
+               uport->iotype   = UPIO_MEM;
+               uport->flags    = UPF_BOOT_AUTOCONF;
+               uport->ops      = &dz_ops;
+               uport->line     = line;
+               uport->mapbase  = base;
+       }
 }
 
 #ifdef CONFIG_SERIAL_DZ_CONSOLE
+/*
+ * -------------------------------------------------------------------
+ * dz_console_putchar() -- transmit a character
+ *
+ * Polled transmission.  This is tricky.  We need to mask transmit
+ * interrupts so that they do not interfere, enable the transmitter
+ * for the line requested and then wait till the transmit scanner
+ * requests data for this line.  But it may request data for another
+ * line first, in which case we have to disable its transmitter and
+ * repeat waiting till our line pops up.  Only then the character may
+ * be transmitted.  Finally, the state of the transmitter mask is
+ * restored.  Welcome to the world of PDP-11!
+ * -------------------------------------------------------------------
+ */
 static void dz_console_putchar(struct uart_port *uport, int ch)
 {
-       struct dz_port *dport = (struct dz_port *)uport;
+       struct dz_port *dport = to_dport(uport);
        unsigned long flags;
-       int loops = 2500;
-       unsigned short tmp = (unsigned char)ch;
-       /* this code sends stuff out to serial device - spinning its
-          wheels and waiting. */
+       unsigned short csr, tcr, trdy, mask;
+       int loops = 10000;
 
        spin_lock_irqsave(&dport->port.lock, flags);
+       csr = dz_in(dport, DZ_CSR);
+       dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
+       tcr = dz_in(dport, DZ_TCR);
+       tcr |= 1 << dport->port.line;
+       mask = tcr;
+       dz_out(dport, DZ_TCR, mask);
+       iob();
+       spin_unlock_irqrestore(&dport->port.lock, flags);
 
-       /* spin our wheels */
-       while (((dz_in(dport, DZ_CSR) & DZ_TRDY) != DZ_TRDY) && loops--)
-               /* FIXME: cpu_relax, udelay? --rmk */
-               ;
+       do {
+               trdy = dz_in(dport, DZ_CSR);
+               if (!(trdy & DZ_TRDY))
+                       continue;
+               trdy = (trdy & DZ_TLINE) >> 8;
+               if (trdy == dport->port.line)
+                       break;
+               mask &= ~(1 << trdy);
+               dz_out(dport, DZ_TCR, mask);
+               iob();
+               udelay(2);
+       } while (loops--);
 
-       /* Actually transmit the character. */
-       dz_out(dport, DZ_TDR, tmp);
+       if (loops)                              /* Cannot send otherwise. */
+               dz_out(dport, DZ_TDR, ch);
 
-       spin_unlock_irqrestore(&dport->port.lock, flags);
+       dz_out(dport, DZ_TCR, tcr);
+       dz_out(dport, DZ_CSR, csr);
 }
 
 /*
@@ -703,11 +836,11 @@ static void dz_console_putchar(struct uart_port *uport, int ch)
  * The console must be locked when we get here.
  * -------------------------------------------------------------------
  */
-static void dz_console_print(struct console *cons,
+static void dz_console_print(struct console *co,
                             const char *str,
                             unsigned int count)
 {
-       struct dz_port *dport = &dz_ports[CONSOLE_LINE];
+       struct dz_port *dport = &dz_mux.dport[co->index];
 #ifdef DEBUG_DZ
        prom_printf((char *) str);
 #endif
@@ -716,50 +849,50 @@ static void dz_console_print(struct console *cons,
 
 static int __init dz_console_setup(struct console *co, char *options)
 {
-       struct dz_port *dport = &dz_ports[CONSOLE_LINE];
+       struct dz_port *dport = &dz_mux.dport[co->index];
+       struct uart_port *uport = &dport->port;
        int baud = 9600;
        int bits = 8;
        int parity = 'n';
        int flow = 'n';
        int ret;
-       unsigned short mask, tmp;
 
-       if (options)
-               uart_parse_options(options, &baud, &parity, &bits, &flow);
+       ret = dz_map_port(uport);
+       if (ret)
+               return ret;
 
        dz_reset(dport);
 
-       ret = uart_set_options(&dport->port, co, baud, parity, bits, flow);
-       if (ret == 0) {
-               mask = 1 << dport->port.line;
-               tmp = dz_in(dport, DZ_TCR);     /* read the TX flag */
-               if (!(tmp & mask)) {
-                       tmp |= mask;            /* set the TX flag */
-                       dz_out(dport, DZ_TCR, tmp);
-               }
-       }
+       if (options)
+               uart_parse_options(options, &baud, &parity, &bits, &flow);
 
-       return ret;
+       return uart_set_options(&dport->port, co, baud, parity, bits, flow);
 }
 
-static struct console dz_sercons =
-{
+static struct uart_driver dz_reg;
+static struct console dz_console = {
        .name   = "ttyS",
        .write  = dz_console_print,
        .device = uart_console_device,
        .setup  = dz_console_setup,
-       .flags  = CON_CONSDEV | CON_PRINTBUFFER,
-       .index  = CONSOLE_LINE,
+       .flags  = CON_PRINTBUFFER,
+       .index  = -1,
+       .data   = &dz_reg,
 };
 
-void __init dz_serial_console_init(void)
+static int __init dz_serial_console_init(void)
 {
-       dz_init_ports();
-
-       register_console(&dz_sercons);
+       if (!IOASIC) {
+               dz_init_ports();
+               register_console(&dz_console);
+               return 0;
+       } else
+               return -ENXIO;
 }
 
-#define SERIAL_DZ_CONSOLE      &dz_sercons
+console_initcall(dz_serial_console_init);
+
+#define SERIAL_DZ_CONSOLE      &dz_console
 #else
 #define SERIAL_DZ_CONSOLE      NULL
 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
@@ -767,48 +900,32 @@ void __init dz_serial_console_init(void)
 static struct uart_driver dz_reg = {
        .owner                  = THIS_MODULE,
        .driver_name            = "serial",
-       .dev_name               = "ttyS%d",
+       .dev_name               = "ttyS",
        .major                  = TTY_MAJOR,
        .minor                  = 64,
        .nr                     = DZ_NB_PORT,
        .cons                   = SERIAL_DZ_CONSOLE,
 };
 
-int __init dz_init(void)
+static int __init dz_init(void)
 {
-       unsigned long flags;
        int ret, i;
 
+       if (IOASIC)
+               return -ENXIO;
+
        printk("%s%s\n", dz_name, dz_version);
 
        dz_init_ports();
 
-       save_flags(flags);
-       cli();
-
-#ifndef CONFIG_SERIAL_DZ_CONSOLE
-       /* reset the chip */
-       dz_reset(&dz_ports[0]);
-#endif
-
-       /* order matters here... the trick is that flags
-          is updated... in request_irq - to immediatedly obliterate
-          it is unwise. */
-       restore_flags(flags);
-
-       if (request_irq(dz_ports[0].port.irq, dz_interrupt,
-                       IRQF_DISABLED, "DZ", &dz_ports[0]))
-               panic("Unable to register DZ interrupt");
-
        ret = uart_register_driver(&dz_reg);
-       if (ret != 0)
+       if (ret)
                return ret;
 
        for (i = 0; i < DZ_NB_PORT; i++)
-               uart_add_one_port(&dz_reg, &dz_ports[i].port);
+               uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
 
-       return ret;
+       return 0;
 }
 
-MODULE_DESCRIPTION("DECstation DZ serial driver");
-MODULE_LICENSE("GPL");
+module_init(dz_init);