drivers/serial: use nr_irqs
[safe/jmp/linux-2.6] / drivers / serial / cpm_uart / cpm_uart_core.c
index 16af562..e54574c 100644 (file)
  *  Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
  *              Pantelis Antoniou (panto@intracom.gr) (CPM1)
  *
- *  Copyright (C) 2004 Freescale Semiconductor, Inc.
+ *  Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
  *            (C) 2004 Intracom, S.A.
- *            (C) 2005 MontaVista Software, Inc. by Vitaly Bordug <vbordug@ru.mvista.com>
+ *            (C) 2005-2006 MontaVista Software, Inc.
+ *             Vitaly Bordug <vbordug@ru.mvista.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,7 +31,6 @@
  *
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/tty.h>
 #include <linux/ioport.h>
 #include <linux/device.h>
 #include <linux/bootmem.h>
 #include <linux/dma-mapping.h>
+#include <linux/fs_uart_pd.h>
+#include <linux/of_platform.h>
+#include <linux/gpio.h>
+#include <linux/of_gpio.h>
+#include <linux/clk.h>
 
 #include <asm/io.h>
 #include <asm/irq.h>
 #include <asm/delay.h>
+#include <asm/fs_pd.h>
+#include <asm/udbg.h>
 
 #if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
 #define SUPPORT_SYSRQ
 
 #include "cpm_uart.h"
 
-/***********************************************************************/
-
-/* Track which ports are configured as uarts */
-int cpm_uart_port_map[UART_NR];
-/* How many ports did we config as uarts */
-int cpm_uart_nr;
 
 /**************************************************************/
 
@@ -71,34 +72,20 @@ static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
 
 /**************************************************************/
 
-static inline unsigned long cpu2cpm_addr(void *addr)
-{
-       if ((unsigned long)addr >= CPM_ADDR)
-               return (unsigned long)addr;
-       return virt_to_bus(addr);
-}
-
-static inline void *cpm2cpu_addr(unsigned long addr)
-{
-       if (addr >= CPM_ADDR)
-               return (void *)addr;
-       return bus_to_virt(addr);
-}
-
 /*
  * Check, if transmit buffers are processed
 */
 static unsigned int cpm_uart_tx_empty(struct uart_port *port)
 {
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile cbd_t *bdp = pinfo->tx_bd_base;
+       cbd_t __iomem *bdp = pinfo->tx_bd_base;
        int ret = 0;
 
        while (1) {
-               if (bdp->cbd_sc & BD_SC_READY)
+               if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
                        break;
 
-               if (bdp->cbd_sc & BD_SC_WRAP) {
+               if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
                        ret = TIOCSER_TEMT;
                        break;
                }
@@ -112,13 +99,41 @@ static unsigned int cpm_uart_tx_empty(struct uart_port *port)
 
 static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
 {
-       /* Whee. Do nothing. */
+       struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
+
+       if (pinfo->gpios[GPIO_RTS] >= 0)
+               gpio_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
+
+       if (pinfo->gpios[GPIO_DTR] >= 0)
+               gpio_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
 }
 
 static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
 {
-       /* Whee. Do nothing. */
-       return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
+       struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
+       unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
+
+       if (pinfo->gpios[GPIO_CTS] >= 0) {
+               if (gpio_get_value(pinfo->gpios[GPIO_CTS]))
+                       mctrl &= ~TIOCM_CTS;
+       }
+
+       if (pinfo->gpios[GPIO_DSR] >= 0) {
+               if (gpio_get_value(pinfo->gpios[GPIO_DSR]))
+                       mctrl &= ~TIOCM_DSR;
+       }
+
+       if (pinfo->gpios[GPIO_DCD] >= 0) {
+               if (gpio_get_value(pinfo->gpios[GPIO_DCD]))
+                       mctrl &= ~TIOCM_CAR;
+       }
+
+       if (pinfo->gpios[GPIO_RI] >= 0) {
+               if (!gpio_get_value(pinfo->gpios[GPIO_RI]))
+                       mctrl |= TIOCM_RNG;
+       }
+
+       return mctrl;
 }
 
 /*
@@ -127,15 +142,15 @@ static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
 static void cpm_uart_stop_tx(struct uart_port *port)
 {
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile smc_t *smcp = pinfo->smcp;
-       volatile scc_t *sccp = pinfo->sccp;
+       smc_t __iomem *smcp = pinfo->smcp;
+       scc_t __iomem *sccp = pinfo->sccp;
 
        pr_debug("CPM uart[%d]:stop tx\n", port->line);
 
        if (IS_SMC(pinfo))
-               smcp->smc_smcm &= ~SMCM_TX;
+               clrbits8(&smcp->smc_smcm, SMCM_TX);
        else
-               sccp->scc_sccm &= ~UART_SCCM_TX;
+               clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
 }
 
 /*
@@ -144,26 +159,24 @@ static void cpm_uart_stop_tx(struct uart_port *port)
 static void cpm_uart_start_tx(struct uart_port *port)
 {
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile smc_t *smcp = pinfo->smcp;
-       volatile scc_t *sccp = pinfo->sccp;
+       smc_t __iomem *smcp = pinfo->smcp;
+       scc_t __iomem *sccp = pinfo->sccp;
 
        pr_debug("CPM uart[%d]:start tx\n", port->line);
 
        if (IS_SMC(pinfo)) {
-               if (smcp->smc_smcm & SMCM_TX)
+               if (in_8(&smcp->smc_smcm) & SMCM_TX)
                        return;
        } else {
-               if (sccp->scc_sccm & UART_SCCM_TX)
+               if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
                        return;
        }
 
        if (cpm_uart_tx_pump(port) != 0) {
                if (IS_SMC(pinfo)) {
-                       smcp->smc_smcm |= SMCM_TX;
-                       smcp->smc_smcmr |= SMCMR_TEN;
+                       setbits8(&smcp->smc_smcm, SMCM_TX);
                } else {
-                       sccp->scc_sccm |= UART_SCCM_TX;
-                       pinfo->sccp->scc_gsmrl |= SCC_GSMRL_ENT;
+                       setbits16(&sccp->scc_sccm, UART_SCCM_TX);
                }
        }
 }
@@ -174,15 +187,15 @@ static void cpm_uart_start_tx(struct uart_port *port)
 static void cpm_uart_stop_rx(struct uart_port *port)
 {
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile smc_t *smcp = pinfo->smcp;
-       volatile scc_t *sccp = pinfo->sccp;
+       smc_t __iomem *smcp = pinfo->smcp;
+       scc_t __iomem *sccp = pinfo->sccp;
 
        pr_debug("CPM uart[%d]:stop rx\n", port->line);
 
        if (IS_SMC(pinfo))
-               smcp->smc_smcm &= ~SMCM_RX;
+               clrbits8(&smcp->smc_smcm, SMCM_RX);
        else
-               sccp->scc_sccm &= ~UART_SCCM_RX;
+               clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
 }
 
 /*
@@ -199,37 +212,41 @@ static void cpm_uart_enable_ms(struct uart_port *port)
 static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
 {
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       int line = pinfo - cpm_uart_ports;
 
        pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
                break_state);
 
        if (break_state)
-               cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
+               cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
        else
-               cpm_line_cr_cmd(line, CPM_CR_RESTART_TX);
+               cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
 }
 
 /*
  * Transmit characters, refill buffer descriptor, if possible
  */
-static void cpm_uart_int_tx(struct uart_port *port, struct pt_regs *regs)
+static void cpm_uart_int_tx(struct uart_port *port)
 {
        pr_debug("CPM uart[%d]:TX INT\n", port->line);
 
        cpm_uart_tx_pump(port);
 }
 
+#ifdef CONFIG_CONSOLE_POLL
+static int serial_polled;
+#endif
+
 /*
  * Receive characters
  */
-static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
+static void cpm_uart_int_rx(struct uart_port *port)
 {
        int i;
-       unsigned char ch, *cp;
-       struct tty_struct *tty = port->info->tty;
+       unsigned char ch;
+       u8 *cp;
+       struct tty_struct *tty = port->info->port.tty;
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile cbd_t *bdp;
+       cbd_t __iomem *bdp;
        u16 status;
        unsigned int flg;
 
@@ -240,28 +257,31 @@ static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
         */
        bdp = pinfo->rx_cur;
        for (;;) {
+#ifdef CONFIG_CONSOLE_POLL
+               if (unlikely(serial_polled)) {
+                       serial_polled = 0;
+                       return;
+               }
+#endif
                /* get status */
-               status = bdp->cbd_sc;
+               status = in_be16(&bdp->cbd_sc);
                /* If this one is empty, return happy */
                if (status & BD_SC_EMPTY)
                        break;
 
                /* get number of characters, and check spce in flip-buffer */
-               i = bdp->cbd_datlen;
+               i = in_be16(&bdp->cbd_datlen);
 
                /* If we have not enough room in tty flip buffer, then we try
                 * later, which will be the next rx-interrupt or a timeout
                 */
-               if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE) {
-                       tty->flip.work.func((void *)tty);
-                       if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE) {
-                               printk(KERN_WARNING "TTY_DONT_FLIP set\n");
-                               return;
-                       }
+               if(tty_buffer_request_room(tty, i) < i) {
+                       printk(KERN_WARNING "No room in flip buffer\n");
+                       return;
                }
 
                /* get pointer */
-               cp = cpm2cpu_addr(bdp->cbd_bufaddr);
+               cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
 
                /* loop through the buffer */
                while (i-- > 0) {
@@ -272,21 +292,25 @@ static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
                        if (status &
                            (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
                                goto handle_error;
-                       if (uart_handle_sysrq_char(port, ch, regs))
+                       if (uart_handle_sysrq_char(port, ch))
                                continue;
-
+#ifdef CONFIG_CONSOLE_POLL
+                       if (unlikely(serial_polled)) {
+                               serial_polled = 0;
+                               return;
+                       }
+#endif
                      error_return:
-                       *tty->flip.char_buf_ptr++ = ch;
-                       *tty->flip.flag_buf_ptr++ = flg;
-                       tty->flip.count++;
+                       tty_insert_flip_char(tty, ch, flg);
 
                }               /* End while (i--) */
 
                /* This BD is ready to be used again. Clear status. get next */
-               bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
-               bdp->cbd_sc |= BD_SC_EMPTY;
+               clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
+                                       BD_SC_OV | BD_SC_ID);
+               setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
 
-               if (bdp->cbd_sc & BD_SC_WRAP)
+               if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
                        bdp = pinfo->rx_bd_base;
                else
                        bdp++;
@@ -294,7 +318,7 @@ static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
        } /* End for (;;) */
 
        /* Write back buffer pointer */
-       pinfo->rx_cur = (volatile cbd_t *) bdp;
+       pinfo->rx_cur = bdp;
 
        /* activate BH processing */
        tty_flip_buffer_push(tty);
@@ -343,34 +367,34 @@ static void cpm_uart_int_rx(struct uart_port *port, struct pt_regs *regs)
 /*
  * Asynchron mode interrupt handler
  */
-static irqreturn_t cpm_uart_int(int irq, void *data, struct pt_regs *regs)
+static irqreturn_t cpm_uart_int(int irq, void *data)
 {
        u8 events;
-       struct uart_port *port = (struct uart_port *)data;
+       struct uart_port *port = data;
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile smc_t *smcp = pinfo->smcp;
-       volatile scc_t *sccp = pinfo->sccp;
+       smc_t __iomem *smcp = pinfo->smcp;
+       scc_t __iomem *sccp = pinfo->sccp;
 
        pr_debug("CPM uart[%d]:IRQ\n", port->line);
 
        if (IS_SMC(pinfo)) {
-               events = smcp->smc_smce;
-               smcp->smc_smce = events;
+               events = in_8(&smcp->smc_smce);
+               out_8(&smcp->smc_smce, events);
                if (events & SMCM_BRKE)
                        uart_handle_break(port);
                if (events & SMCM_RX)
-                       cpm_uart_int_rx(port, regs);
+                       cpm_uart_int_rx(port);
                if (events & SMCM_TX)
-                       cpm_uart_int_tx(port, regs);
+                       cpm_uart_int_tx(port);
        } else {
-               events = sccp->scc_scce;
-               sccp->scc_scce = events;
+               events = in_be16(&sccp->scc_scce);
+               out_be16(&sccp->scc_scce, events);
                if (events & UART_SCCM_BRKE)
                        uart_handle_break(port);
                if (events & UART_SCCM_RX)
-                       cpm_uart_int_rx(port, regs);
+                       cpm_uart_int_rx(port);
                if (events & UART_SCCM_TX)
-                       cpm_uart_int_tx(port, regs);
+                       cpm_uart_int_tx(port);
        }
        return (events) ? IRQ_HANDLED : IRQ_NONE;
 }
@@ -379,7 +403,6 @@ static int cpm_uart_startup(struct uart_port *port)
 {
        int retval;
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       int line = pinfo - cpm_uart_ports;
 
        pr_debug("CPM uart[%d]:startup\n", port->line);
 
@@ -390,14 +413,15 @@ static int cpm_uart_startup(struct uart_port *port)
 
        /* Startup rx-int */
        if (IS_SMC(pinfo)) {
-               pinfo->smcp->smc_smcm |= SMCM_RX;
-               pinfo->smcp->smc_smcmr |= SMCMR_REN;
+               setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
+               setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
        } else {
-               pinfo->sccp->scc_sccm |= UART_SCCM_RX;
+               setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
+               setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
        }
 
        if (!(pinfo->flags & FLAG_CONSOLE))
-               cpm_line_cr_cmd(line,CPM_CR_INIT_TRX);
+               cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
        return 0;
 }
 
@@ -413,7 +437,6 @@ inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
 static void cpm_uart_shutdown(struct uart_port *port)
 {
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       int line = pinfo - cpm_uart_ports;
 
        pr_debug("CPM uart[%d]:shutdown\n", port->line);
 
@@ -433,31 +456,39 @@ static void cpm_uart_shutdown(struct uart_port *port)
 
                /* Stop uarts */
                if (IS_SMC(pinfo)) {
-                       volatile smc_t *smcp = pinfo->smcp;
-                       smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
-                       smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
+                       smc_t __iomem *smcp = pinfo->smcp;
+                       clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
+                       clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
                } else {
-                       volatile scc_t *sccp = pinfo->sccp;
-                       sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
-                       sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
+                       scc_t __iomem *sccp = pinfo->sccp;
+                       clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
+                       clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
                }
 
                /* Shut them really down and reinit buffer descriptors */
-               cpm_line_cr_cmd(line, CPM_CR_STOP_TX);
+               if (IS_SMC(pinfo)) {
+                       out_be16(&pinfo->smcup->smc_brkcr, 0);
+                       cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
+               } else {
+                       out_be16(&pinfo->sccup->scc_brkcr, 0);
+                       cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
+               }
+
                cpm_uart_initbd(pinfo);
        }
 }
 
 static void cpm_uart_set_termios(struct uart_port *port,
-                                struct termios *termios, struct termios *old)
+                                 struct ktermios *termios,
+                                 struct ktermios *old)
 {
        int baud;
        unsigned long flags;
        u16 cval, scval, prev_mode;
        int bits, sbits;
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
-       volatile smc_t *smcp = pinfo->smcp;
-       volatile scc_t *sccp = pinfo->sccp;
+       smc_t __iomem *smcp = pinfo->smcp;
+       scc_t __iomem *sccp = pinfo->sccp;
 
        pr_debug("CPM uart[%d]:set_termios\n", port->line);
 
@@ -509,6 +540,11 @@ static void cpm_uart_set_termios(struct uart_port *port,
        }
 
        /*
+        * Update the timeout
+        */
+       uart_update_timeout(port, termios->c_cflag, baud);
+
+       /*
         * Set up parity check flag
         */
 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
@@ -552,16 +588,20 @@ static void cpm_uart_set_termios(struct uart_port *port,
                 * enables, because we want to put them back if they were
                 * present.
                 */
-               prev_mode = smcp->smc_smcmr;
-               smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART;
-               smcp->smc_smcmr |= (prev_mode & (SMCMR_REN | SMCMR_TEN));
+               prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
+               /* Output in *one* operation, so we don't interrupt RX/TX if they
+                * were already enabled. */
+               out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
+                   SMCMR_SM_UART | prev_mode);
        } else {
-               sccp->scc_psmr = (sbits << 12) | scval;
+               out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
        }
 
-       cpm_set_brg(pinfo->brg - 1, baud);
+       if (pinfo->clk)
+               clk_set_rate(pinfo->clk, baud);
+       else
+               cpm_set_brg(pinfo->brg - 1, baud);
        spin_unlock_irqrestore(&port->lock, flags);
-
 }
 
 static const char *cpm_uart_type(struct uart_port *port)
@@ -583,7 +623,7 @@ static int cpm_uart_verify_port(struct uart_port *port,
 
        if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
                ret = -EINVAL;
-       if (ser->irq < 0 || ser->irq >= NR_IRQS)
+       if (ser->irq < 0 || ser->irq >= nr_irqs)
                ret = -EINVAL;
        if (ser->baud_base < 9600)
                ret = -EINVAL;
@@ -595,8 +635,8 @@ static int cpm_uart_verify_port(struct uart_port *port,
  */
 static int cpm_uart_tx_pump(struct uart_port *port)
 {
-       volatile cbd_t *bdp;
-       unsigned char *p;
+       cbd_t __iomem *bdp;
+       u8 *p;
        int count;
        struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
        struct circ_buf *xmit = &port->info->xmit;
@@ -606,13 +646,14 @@ static int cpm_uart_tx_pump(struct uart_port *port)
                /* Pick next descriptor and fill from buffer */
                bdp = pinfo->tx_cur;
 
-               p = cpm2cpu_addr(bdp->cbd_bufaddr);
+               p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
 
                *p++ = port->x_char;
-               bdp->cbd_datlen = 1;
-               bdp->cbd_sc |= BD_SC_READY;
+
+               out_be16(&bdp->cbd_datlen, 1);
+               setbits16(&bdp->cbd_sc, BD_SC_READY);
                /* Get next BD. */
-               if (bdp->cbd_sc & BD_SC_WRAP)
+               if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
                        bdp = pinfo->tx_bd_base;
                else
                        bdp++;
@@ -631,9 +672,10 @@ static int cpm_uart_tx_pump(struct uart_port *port)
        /* Pick next descriptor and fill from buffer */
        bdp = pinfo->tx_cur;
 
-       while (!(bdp->cbd_sc & BD_SC_READY) && (xmit->tail != xmit->head)) {
+       while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
+              xmit->tail != xmit->head) {
                count = 0;
-               p = cpm2cpu_addr(bdp->cbd_bufaddr);
+               p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
                while (count < pinfo->tx_fifosize) {
                        *p++ = xmit->buf[xmit->tail];
                        xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
@@ -642,11 +684,10 @@ static int cpm_uart_tx_pump(struct uart_port *port)
                        if (xmit->head == xmit->tail)
                                break;
                }
-               bdp->cbd_datlen = count;
-               bdp->cbd_sc |= BD_SC_READY;
-               __asm__("eieio");
+               out_be16(&bdp->cbd_datlen, count);
+               setbits16(&bdp->cbd_sc, BD_SC_READY);
                /* Get next BD. */
-               if (bdp->cbd_sc & BD_SC_WRAP)
+               if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
                        bdp = pinfo->tx_bd_base;
                else
                        bdp++;
@@ -671,7 +712,7 @@ static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
 {
        int i;
        u8 *mem_addr;
-       volatile cbd_t *bdp;
+       cbd_t __iomem *bdp;
 
        pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
 
@@ -682,13 +723,13 @@ static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
        mem_addr = pinfo->mem_addr;
        bdp = pinfo->rx_cur = pinfo->rx_bd_base;
        for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
-               bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
-               bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
+               out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
+               out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
                mem_addr += pinfo->rx_fifosize;
        }
 
-       bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
-       bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
+       out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
+       out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
 
        /* Set the physical address of the host memory
         * buffers in the buffer descriptors, and the
@@ -697,20 +738,19 @@ static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
        mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
        bdp = pinfo->tx_cur = pinfo->tx_bd_base;
        for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
-               bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
-               bdp->cbd_sc = BD_SC_INTRPT;
+               out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
+               out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
                mem_addr += pinfo->tx_fifosize;
        }
 
-       bdp->cbd_bufaddr = cpu2cpm_addr(mem_addr);
-       bdp->cbd_sc = BD_SC_WRAP | BD_SC_INTRPT;
+       out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
+       out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
 }
 
 static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
 {
-       int line = pinfo - cpm_uart_ports;
-       volatile scc_t *scp;
-       volatile scc_uart_t *sup;
+       scc_t __iomem *scp;
+       scc_uart_t __iomem *sup;
 
        pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
 
@@ -718,8 +758,10 @@ static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
        sup = pinfo->sccup;
 
        /* Store address */
-       pinfo->sccup->scc_genscc.scc_rbase = (unsigned char *)pinfo->rx_bd_base - DPRAM_BASE;
-       pinfo->sccup->scc_genscc.scc_tbase = (unsigned char *)pinfo->tx_bd_base - DPRAM_BASE;
+       out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
+                (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
+       out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
+                (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
 
        /* Set up the uart parameters in the
         * parameter ram.
@@ -727,51 +769,50 @@ static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
 
        cpm_set_scc_fcr(sup);
 
-       sup->scc_genscc.scc_mrblr = pinfo->rx_fifosize;
-       sup->scc_maxidl = pinfo->rx_fifosize;
-       sup->scc_brkcr = 1;
-       sup->scc_parec = 0;
-       sup->scc_frmec = 0;
-       sup->scc_nosec = 0;
-       sup->scc_brkec = 0;
-       sup->scc_uaddr1 = 0;
-       sup->scc_uaddr2 = 0;
-       sup->scc_toseq = 0;
-       sup->scc_char1 = 0x8000;
-       sup->scc_char2 = 0x8000;
-       sup->scc_char3 = 0x8000;
-       sup->scc_char4 = 0x8000;
-       sup->scc_char5 = 0x8000;
-       sup->scc_char6 = 0x8000;
-       sup->scc_char7 = 0x8000;
-       sup->scc_char8 = 0x8000;
-       sup->scc_rccm = 0xc0ff;
+       out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
+       out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
+       out_be16(&sup->scc_brkcr, 1);
+       out_be16(&sup->scc_parec, 0);
+       out_be16(&sup->scc_frmec, 0);
+       out_be16(&sup->scc_nosec, 0);
+       out_be16(&sup->scc_brkec, 0);
+       out_be16(&sup->scc_uaddr1, 0);
+       out_be16(&sup->scc_uaddr2, 0);
+       out_be16(&sup->scc_toseq, 0);
+       out_be16(&sup->scc_char1, 0x8000);
+       out_be16(&sup->scc_char2, 0x8000);
+       out_be16(&sup->scc_char3, 0x8000);
+       out_be16(&sup->scc_char4, 0x8000);
+       out_be16(&sup->scc_char5, 0x8000);
+       out_be16(&sup->scc_char6, 0x8000);
+       out_be16(&sup->scc_char7, 0x8000);
+       out_be16(&sup->scc_char8, 0x8000);
+       out_be16(&sup->scc_rccm, 0xc0ff);
 
        /* Send the CPM an initialize command.
         */
-       cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
+       cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
 
        /* Set UART mode, 8 bit, no parity, one stop.
         * Enable receive and transmit.
         */
-       scp->scc_gsmrh = 0;
-       scp->scc_gsmrl =
-           (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
+       out_be32(&scp->scc_gsmrh, 0);
+       out_be32(&scp->scc_gsmrl,
+                SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
 
        /* Enable rx interrupts  and clear all pending events.  */
-       scp->scc_sccm = 0;
-       scp->scc_scce = 0xffff;
-       scp->scc_dsr = 0x7e7e;
-       scp->scc_psmr = 0x3000;
+       out_be16(&scp->scc_sccm, 0);
+       out_be16(&scp->scc_scce, 0xffff);
+       out_be16(&scp->scc_dsr, 0x7e7e);
+       out_be16(&scp->scc_psmr, 0x3000);
 
-       scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
+       setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
 }
 
 static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
 {
-       int line = pinfo - cpm_uart_ports;
-       volatile smc_t *sp;
-       volatile smc_uart_t *up;
+       smc_t __iomem *sp;
+       smc_uart_t __iomem *up;
 
        pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
 
@@ -779,19 +820,21 @@ static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
        up = pinfo->smcup;
 
        /* Store address */
-       pinfo->smcup->smc_rbase = (u_char *)pinfo->rx_bd_base - DPRAM_BASE;
-       pinfo->smcup->smc_tbase = (u_char *)pinfo->tx_bd_base - DPRAM_BASE;
+       out_be16(&pinfo->smcup->smc_rbase,
+                (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
+       out_be16(&pinfo->smcup->smc_tbase,
+                (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
 
 /*
  *  In case SMC1 is being relocated...
  */
 #if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
-       up->smc_rbptr = pinfo->smcup->smc_rbase;
-       up->smc_tbptr = pinfo->smcup->smc_tbase;
-       up->smc_rstate = 0;
-       up->smc_tstate = 0;
-       up->smc_brkcr = 1;              /* number of break chars */
-       up->smc_brkec = 0;
+       out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
+       out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
+       out_be32(&up->smc_rstate, 0);
+       out_be32(&up->smc_tstate, 0);
+       out_be16(&up->smc_brkcr, 1);              /* number of break chars */
+       out_be16(&up->smc_brkec, 0);
 #endif
 
        /* Set up the uart parameters in the
@@ -800,24 +843,24 @@ static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
        cpm_set_smc_fcr(up);
 
        /* Using idle charater time requires some additional tuning.  */
-       up->smc_mrblr = pinfo->rx_fifosize;
-       up->smc_maxidl = pinfo->rx_fifosize;
-       up->smc_brklen = 0;
-       up->smc_brkec = 0;
-       up->smc_brkcr = 1;
+       out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
+       out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
+       out_be16(&up->smc_brklen, 0);
+       out_be16(&up->smc_brkec, 0);
+       out_be16(&up->smc_brkcr, 1);
 
-       cpm_line_cr_cmd(line, CPM_CR_INIT_TRX);
+       cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
 
        /* Set UART mode, 8 bit, no parity, one stop.
         * Enable receive and transmit.
         */
-       sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
+       out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
 
        /* Enable only rx interrupts clear all pending events. */
-       sp->smc_smcm = 0;
-       sp->smc_smce = 0xff;
+       out_8(&sp->smc_smcm, 0);
+       out_8(&sp->smc_smce, 0xff);
 
-       sp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
+       setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
 }
 
 /*
@@ -834,20 +877,12 @@ static int cpm_uart_request_port(struct uart_port *port)
        if (pinfo->flags & FLAG_CONSOLE)
                return 0;
 
-       /*
-        * Setup any port IO, connect any baud rate generators,
-        * etc.  This is expected to be handled by board
-        * dependant code
-        */
-       if (pinfo->set_lineif)
-               pinfo->set_lineif(pinfo);
-
        if (IS_SMC(pinfo)) {
-               pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
-               pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
+               clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
+               clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
        } else {
-               pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
-               pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
+               clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
+               clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
        }
 
        ret = cpm_uart_allocbuf(pinfo, 0);
@@ -884,6 +919,80 @@ static void cpm_uart_config_port(struct uart_port *port, int flags)
                cpm_uart_request_port(port);
        }
 }
+
+#ifdef CONFIG_CONSOLE_POLL
+/* Serial polling routines for writing and reading from the uart while
+ * in an interrupt or debug context.
+ */
+
+#define GDB_BUF_SIZE   512     /* power of 2, please */
+
+static char poll_buf[GDB_BUF_SIZE];
+static char *pollp;
+static int poll_chars;
+
+static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
+{
+       u_char          c, *cp;
+       volatile cbd_t  *bdp;
+       int             i;
+
+       /* Get the address of the host memory buffer.
+        */
+       bdp = pinfo->rx_cur;
+       while (bdp->cbd_sc & BD_SC_EMPTY)
+               ;
+
+       /* If the buffer address is in the CPM DPRAM, don't
+        * convert it.
+        */
+       cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
+
+       if (obuf) {
+               i = c = bdp->cbd_datlen;
+               while (i-- > 0)
+                       *obuf++ = *cp++;
+       } else
+               c = *cp;
+       bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
+       bdp->cbd_sc |= BD_SC_EMPTY;
+
+       if (bdp->cbd_sc & BD_SC_WRAP)
+               bdp = pinfo->rx_bd_base;
+       else
+               bdp++;
+       pinfo->rx_cur = (cbd_t *)bdp;
+
+       return (int)c;
+}
+
+static int cpm_get_poll_char(struct uart_port *port)
+{
+       struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
+
+       if (!serial_polled) {
+               serial_polled = 1;
+               poll_chars = 0;
+       }
+       if (poll_chars <= 0) {
+               poll_chars = poll_wait_key(poll_buf, pinfo);
+               pollp = poll_buf;
+       }
+       poll_chars--;
+       return *pollp++;
+}
+
+static void cpm_put_poll_char(struct uart_port *port,
+                        unsigned char c)
+{
+       struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
+       static char ch[2];
+
+       ch[0] = (char)c;
+       cpm_uart_early_write(pinfo->port.line, ch, 1);
+}
+#endif /* CONFIG_CONSOLE_POLL */
+
 static struct uart_ops cpm_uart_pops = {
        .tx_empty       = cpm_uart_tx_empty,
        .set_mctrl      = cpm_uart_set_mctrl,
@@ -901,98 +1010,101 @@ static struct uart_ops cpm_uart_pops = {
        .request_port   = cpm_uart_request_port,
        .config_port    = cpm_uart_config_port,
        .verify_port    = cpm_uart_verify_port,
-};
-
-struct uart_cpm_port cpm_uart_ports[UART_NR] = {
-       [UART_SMC1] = {
-               .port = {
-                       .irq            = SMC1_IRQ,
-                       .ops            = &cpm_uart_pops,
-                       .iotype         = SERIAL_IO_MEM,
-                       .lock           = SPIN_LOCK_UNLOCKED,
-               },
-               .flags = FLAG_SMC,
-               .tx_nrfifos = TX_NUM_FIFO,
-               .tx_fifosize = TX_BUF_SIZE,
-               .rx_nrfifos = RX_NUM_FIFO,
-               .rx_fifosize = RX_BUF_SIZE,
-               .set_lineif = smc1_lineif,
-       },
-       [UART_SMC2] = {
-               .port = {
-                       .irq            = SMC2_IRQ,
-                       .ops            = &cpm_uart_pops,
-                       .iotype         = SERIAL_IO_MEM,
-                       .lock           = SPIN_LOCK_UNLOCKED,
-               },
-               .flags = FLAG_SMC,
-               .tx_nrfifos = TX_NUM_FIFO,
-               .tx_fifosize = TX_BUF_SIZE,
-               .rx_nrfifos = RX_NUM_FIFO,
-               .rx_fifosize = RX_BUF_SIZE,
-               .set_lineif = smc2_lineif,
-#ifdef CONFIG_SERIAL_CPM_ALT_SMC2
-               .is_portb = 1,
+#ifdef CONFIG_CONSOLE_POLL
+       .poll_get_char = cpm_get_poll_char,
+       .poll_put_char = cpm_put_poll_char,
 #endif
-       },
-       [UART_SCC1] = {
-               .port = {
-                       .irq            = SCC1_IRQ,
-                       .ops            = &cpm_uart_pops,
-                       .iotype         = SERIAL_IO_MEM,
-                       .lock           = SPIN_LOCK_UNLOCKED,
-               },
-               .tx_nrfifos = TX_NUM_FIFO,
-               .tx_fifosize = TX_BUF_SIZE,
-               .rx_nrfifos = RX_NUM_FIFO,
-               .rx_fifosize = RX_BUF_SIZE,
-               .set_lineif = scc1_lineif,
-               .wait_closing = SCC_WAIT_CLOSING,
-       },
-       [UART_SCC2] = {
-               .port = {
-                       .irq            = SCC2_IRQ,
-                       .ops            = &cpm_uart_pops,
-                       .iotype         = SERIAL_IO_MEM,
-                       .lock           = SPIN_LOCK_UNLOCKED,
-               },
-               .tx_nrfifos = TX_NUM_FIFO,
-               .tx_fifosize = TX_BUF_SIZE,
-               .rx_nrfifos = RX_NUM_FIFO,
-               .rx_fifosize = RX_BUF_SIZE,
-               .set_lineif = scc2_lineif,
-               .wait_closing = SCC_WAIT_CLOSING,
-       },
-       [UART_SCC3] = {
-               .port = {
-                       .irq            = SCC3_IRQ,
-                       .ops            = &cpm_uart_pops,
-                       .iotype         = SERIAL_IO_MEM,
-                       .lock           = SPIN_LOCK_UNLOCKED,
-               },
-               .tx_nrfifos = TX_NUM_FIFO,
-               .tx_fifosize = TX_BUF_SIZE,
-               .rx_nrfifos = RX_NUM_FIFO,
-               .rx_fifosize = RX_BUF_SIZE,
-               .set_lineif = scc3_lineif,
-               .wait_closing = SCC_WAIT_CLOSING,
-       },
-       [UART_SCC4] = {
-               .port = {
-                       .irq            = SCC4_IRQ,
-                       .ops            = &cpm_uart_pops,
-                       .iotype         = SERIAL_IO_MEM,
-                       .lock           = SPIN_LOCK_UNLOCKED,
-               },
-               .tx_nrfifos = TX_NUM_FIFO,
-               .tx_fifosize = TX_BUF_SIZE,
-               .rx_nrfifos = RX_NUM_FIFO,
-               .rx_fifosize = RX_BUF_SIZE,
-               .set_lineif = scc4_lineif,
-               .wait_closing = SCC_WAIT_CLOSING,
-       },
 };
 
+struct uart_cpm_port cpm_uart_ports[UART_NR];
+
+static int cpm_uart_init_port(struct device_node *np,
+                              struct uart_cpm_port *pinfo)
+{
+       const u32 *data;
+       void __iomem *mem, *pram;
+       int len;
+       int ret;
+       int i;
+
+       data = of_get_property(np, "clock", NULL);
+       if (data) {
+               struct clk *clk = clk_get(NULL, (const char*)data);
+               if (!IS_ERR(clk))
+                       pinfo->clk = clk;
+       }
+       if (!pinfo->clk) {
+               data = of_get_property(np, "fsl,cpm-brg", &len);
+               if (!data || len != 4) {
+                       printk(KERN_ERR "CPM UART %s has no/invalid "
+                                       "fsl,cpm-brg property.\n", np->name);
+                       return -EINVAL;
+               }
+               pinfo->brg = *data;
+       }
+
+       data = of_get_property(np, "fsl,cpm-command", &len);
+       if (!data || len != 4) {
+               printk(KERN_ERR "CPM UART %s has no/invalid "
+                               "fsl,cpm-command property.\n", np->name);
+               return -EINVAL;
+       }
+       pinfo->command = *data;
+
+       mem = of_iomap(np, 0);
+       if (!mem)
+               return -ENOMEM;
+
+       if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
+           of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
+               pinfo->sccp = mem;
+               pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
+       } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
+                  of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
+               pinfo->flags |= FLAG_SMC;
+               pinfo->smcp = mem;
+               pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
+       } else {
+               ret = -ENODEV;
+               goto out_mem;
+       }
+
+       if (!pram) {
+               ret = -ENOMEM;
+               goto out_mem;
+       }
+
+       pinfo->tx_nrfifos = TX_NUM_FIFO;
+       pinfo->tx_fifosize = TX_BUF_SIZE;
+       pinfo->rx_nrfifos = RX_NUM_FIFO;
+       pinfo->rx_fifosize = RX_BUF_SIZE;
+
+       pinfo->port.uartclk = ppc_proc_freq;
+       pinfo->port.mapbase = (unsigned long)mem;
+       pinfo->port.type = PORT_CPM;
+       pinfo->port.ops = &cpm_uart_pops,
+       pinfo->port.iotype = UPIO_MEM;
+       pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
+       spin_lock_init(&pinfo->port.lock);
+
+       pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
+       if (pinfo->port.irq == NO_IRQ) {
+               ret = -EINVAL;
+               goto out_pram;
+       }
+
+       for (i = 0; i < NUM_GPIOS; i++)
+               pinfo->gpios[i] = of_get_gpio(np, i);
+
+       return cpm_uart_request_port(&pinfo->port);
+
+out_pram:
+       cpm_uart_unmap_pram(pinfo, pram);
+out_mem:
+       iounmap(mem);
+       return ret;
+}
+
 #ifdef CONFIG_SERIAL_CPM_CONSOLE
 /*
  *     Print a string to the serial port trying not to disturb
@@ -1003,11 +1115,18 @@ struct uart_cpm_port cpm_uart_ports[UART_NR] = {
 static void cpm_uart_console_write(struct console *co, const char *s,
                                   u_int count)
 {
-       struct uart_cpm_port *pinfo =
-           &cpm_uart_ports[cpm_uart_port_map[co->index]];
+       struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
        unsigned int i;
-       volatile cbd_t *bdp, *bdbase;
-       volatile unsigned char *cp;
+       cbd_t __iomem *bdp, *bdbase;
+       unsigned char *cp;
+       unsigned long flags;
+       int nolock = oops_in_progress;
+
+       if (unlikely(nolock)) {
+               local_irq_save(flags);
+       } else {
+               spin_lock_irqsave(&pinfo->port.lock, flags);
+       }
 
        /* Get the address of the host memory buffer.
         */
@@ -1025,37 +1144,36 @@ static void cpm_uart_console_write(struct console *co, const char *s,
                 * Ready indicates output is ready, and xmt is doing
                 * that, not that it is ready for us to send.
                 */
-               while ((bdp->cbd_sc & BD_SC_READY) != 0)
+               while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
                        ;
 
                /* Send the character out.
                 * If the buffer address is in the CPM DPRAM, don't
                 * convert it.
                 */
-               cp = cpm2cpu_addr(bdp->cbd_bufaddr);
-
+               cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
                *cp = *s;
 
-               bdp->cbd_datlen = 1;
-               bdp->cbd_sc |= BD_SC_READY;
+               out_be16(&bdp->cbd_datlen, 1);
+               setbits16(&bdp->cbd_sc, BD_SC_READY);
 
-               if (bdp->cbd_sc & BD_SC_WRAP)
+               if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
                        bdp = bdbase;
                else
                        bdp++;
 
                /* if a LF, also do CR... */
                if (*s == 10) {
-                       while ((bdp->cbd_sc & BD_SC_READY) != 0)
+                       while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
                                ;
 
-                       cp = cpm2cpu_addr(bdp->cbd_bufaddr);
-
+                       cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
                        *cp = 13;
-                       bdp->cbd_datlen = 1;
-                       bdp->cbd_sc |= BD_SC_READY;
 
-                       if (bdp->cbd_sc & BD_SC_WRAP)
+                       out_be16(&bdp->cbd_datlen, 1);
+                       setbits16(&bdp->cbd_sc, BD_SC_READY);
+
+                       if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
                                bdp = bdbase;
                        else
                                bdp++;
@@ -1066,56 +1184,81 @@ static void cpm_uart_console_write(struct console *co, const char *s,
         * Finally, Wait for transmitter & holding register to empty
         *  and restore the IER
         */
-       while ((bdp->cbd_sc & BD_SC_READY) != 0)
+       while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
                ;
 
-       pinfo->tx_cur = (volatile cbd_t *) bdp;
+       pinfo->tx_cur = bdp;
+
+       if (unlikely(nolock)) {
+               local_irq_restore(flags);
+       } else {
+               spin_unlock_irqrestore(&pinfo->port.lock, flags);
+       }
 }
 
-/*
- * Setup console. Be careful is called early !
- */
+
 static int __init cpm_uart_console_setup(struct console *co, char *options)
 {
-       struct uart_port *port;
-       struct uart_cpm_port *pinfo;
        int baud = 38400;
        int bits = 8;
        int parity = 'n';
        int flow = 'n';
        int ret;
+       struct uart_cpm_port *pinfo;
+       struct uart_port *port;
 
-       port =
-           (struct uart_port *)&cpm_uart_ports[cpm_uart_port_map[co->index]];
-       pinfo = (struct uart_cpm_port *)port;
+       struct device_node *np = NULL;
+       int i = 0;
+
+       if (co->index >= UART_NR) {
+               printk(KERN_ERR "cpm_uart: console index %d too high\n",
+                      co->index);
+               return -ENODEV;
+       }
+
+       do {
+               np = of_find_node_by_type(np, "serial");
+               if (!np)
+                       return -ENODEV;
+
+               if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
+                   !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
+                   !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
+                   !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
+                       i--;
+       } while (i++ != co->index);
+
+       pinfo = &cpm_uart_ports[co->index];
 
        pinfo->flags |= FLAG_CONSOLE;
+       port = &pinfo->port;
+
+       ret = cpm_uart_init_port(np, pinfo);
+       of_node_put(np);
+       if (ret)
+               return ret;
 
        if (options) {
                uart_parse_options(options, &baud, &parity, &bits, &flow);
        } else {
-               bd_t *bd = (bd_t *) __res;
-
-               if (bd->bi_baudrate)
-                       baud = bd->bi_baudrate;
-               else
+               if ((baud = uart_baudrate()) == -1)
                        baud = 9600;
        }
 
-       /*
-        * Setup any port IO, connect any baud rate generators,
-        * etc.  This is expected to be handled by board
-        * dependant code
-        */
-       if (pinfo->set_lineif)
-               pinfo->set_lineif(pinfo);
+#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
+       udbg_putc = NULL;
+#endif
 
        if (IS_SMC(pinfo)) {
-               pinfo->smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
-               pinfo->smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
+               out_be16(&pinfo->smcup->smc_brkcr, 0);
+               cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
+               clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
+               clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
        } else {
-               pinfo->sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
-               pinfo->sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
+               out_be16(&pinfo->sccup->scc_brkcr, 0);
+               cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
+               clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
+               clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
        }
 
        ret = cpm_uart_allocbuf(pinfo, 1);
@@ -1131,6 +1274,7 @@ static int __init cpm_uart_console_setup(struct console *co, char *options)
                cpm_uart_init_scc(pinfo);
 
        uart_set_options(port, co, baud, parity, bits, flow);
+       cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
 
        return 0;
 }
@@ -1146,13 +1290,10 @@ static struct console cpm_scc_uart_console = {
        .data           = &cpm_reg,
 };
 
-int __init cpm_uart_console_init(void)
+static int __init cpm_uart_console_init(void)
 {
-       int ret = cpm_uart_init_portdesc();
-
-       if (!ret)
-               register_console(&cpm_scc_uart_console);
-       return ret;
+       register_console(&cpm_scc_uart_console);
+       return 0;
 }
 
 console_initcall(cpm_uart_console_init);
@@ -1169,45 +1310,77 @@ static struct uart_driver cpm_reg = {
        .major          = SERIAL_CPM_MAJOR,
        .minor          = SERIAL_CPM_MINOR,
        .cons           = CPM_UART_CONSOLE,
+       .nr             = UART_NR,
 };
 
-static int __init cpm_uart_init(void)
+static int probe_index;
+
+static int __devinit cpm_uart_probe(struct of_device *ofdev,
+                                    const struct of_device_id *match)
 {
-       int ret, i;
+       int index = probe_index++;
+       struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
+       int ret;
+
+       pinfo->port.line = index;
+
+       if (index >= UART_NR)
+               return -ENODEV;
 
-       printk(KERN_INFO "Serial: CPM driver $Revision: 0.01 $\n");
+       dev_set_drvdata(&ofdev->dev, pinfo);
 
-#ifndef CONFIG_SERIAL_CPM_CONSOLE
-       ret = cpm_uart_init_portdesc();
+       ret = cpm_uart_init_port(ofdev->node, pinfo);
        if (ret)
                return ret;
-#endif
 
-       cpm_reg.nr = cpm_uart_nr;
-       ret = uart_register_driver(&cpm_reg);
+       return uart_add_one_port(&cpm_reg, &pinfo->port);
+}
+
+static int __devexit cpm_uart_remove(struct of_device *ofdev)
+{
+       struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
+       return uart_remove_one_port(&cpm_reg, &pinfo->port);
+}
+
+static struct of_device_id cpm_uart_match[] = {
+       {
+               .compatible = "fsl,cpm1-smc-uart",
+       },
+       {
+               .compatible = "fsl,cpm1-scc-uart",
+       },
+       {
+               .compatible = "fsl,cpm2-smc-uart",
+       },
+       {
+               .compatible = "fsl,cpm2-scc-uart",
+       },
+       {}
+};
+
+static struct of_platform_driver cpm_uart_driver = {
+       .name = "cpm_uart",
+       .match_table = cpm_uart_match,
+       .probe = cpm_uart_probe,
+       .remove = cpm_uart_remove,
+ };
 
+static int __init cpm_uart_init(void)
+{
+       int ret = uart_register_driver(&cpm_reg);
        if (ret)
                return ret;
 
-       for (i = 0; i < cpm_uart_nr; i++) {
-               int con = cpm_uart_port_map[i];
-               cpm_uart_ports[con].port.line = i;
-               cpm_uart_ports[con].port.flags = UPF_BOOT_AUTOCONF;
-               uart_add_one_port(&cpm_reg, &cpm_uart_ports[con].port);
-       }
+       ret = of_register_platform_driver(&cpm_uart_driver);
+       if (ret)
+               uart_unregister_driver(&cpm_reg);
 
        return ret;
 }
 
 static void __exit cpm_uart_exit(void)
 {
-       int i;
-
-       for (i = 0; i < cpm_uart_nr; i++) {
-               int con = cpm_uart_port_map[i];
-               uart_remove_one_port(&cpm_reg, &cpm_uart_ports[con].port);
-       }
-
+       of_unregister_platform_driver(&cpm_uart_driver);
        uart_unregister_driver(&cpm_reg);
 }