Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[safe/jmp/linux-2.6] / drivers / serial / sh-sci.c
index 0251077..42f3333 100644 (file)
 #include <linux/clk.h>
 #include <linux/ctype.h>
 #include <linux/err.h>
+#include <linux/list.h>
 
 #ifdef CONFIG_SUPERH
-#include <asm/clock.h>
 #include <asm/sh_bios.h>
 #endif
 
+#ifdef CONFIG_H8300
+#include <asm/gpio.h>
+#endif
+
 #include "sh-sci.h"
 
 struct sci_port {
@@ -64,10 +68,6 @@ struct sci_port {
        /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
        unsigned int            irqs[SCIx_NR_IRQS];
 
-       /* Port pin configuration */
-       void                    (*init_pins)(struct uart_port *port,
-                                            unsigned int cflag);
-
        /* Port enable callback */
        void                    (*enable)(struct uart_port *port);
 
@@ -78,15 +78,19 @@ struct sci_port {
        struct timer_list       break_timer;
        int                     break_flag;
 
-#ifdef CONFIG_HAVE_CLK
-       /* Port clock */
-       struct clk              *clk;
-#endif
+       /* Interface clock */
+       struct clk              *iclk;
+       /* Data clock */
+       struct clk              *dclk;
+
+       struct list_head        node;
 };
 
-#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
-static struct sci_port *serial_console_port;
-#endif
+struct sh_sci_priv {
+       spinlock_t lock;
+       struct list_head ports;
+       struct notifier_block clk_nb;
+};
 
 /* Function prototypes */
 static void sci_stop_tx(struct uart_port *port);
@@ -103,6 +107,8 @@ to_sci_port(struct uart_port *uart)
 }
 
 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
+
+#ifdef CONFIG_CONSOLE_POLL
 static inline void handle_error(struct uart_port *port)
 {
        /* Clear error flags */
@@ -130,6 +136,7 @@ static int sci_poll_get_char(struct uart_port *port)
 
        return c;
 }
+#endif
 
 static void sci_poll_put_char(struct uart_port *port, unsigned char c)
 {
@@ -139,40 +146,13 @@ static void sci_poll_put_char(struct uart_port *port, unsigned char c)
                status = sci_in(port, SCxSR);
        } while (!(status & SCxSR_TDxE(port)));
 
-       sci_in(port, SCxSR);            /* Dummy read */
-       sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
        sci_out(port, SCxTDR, c);
+       sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
 }
 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
 
-#if defined(__H8300S__)
-enum { sci_disable, sci_enable };
-
-static void h8300_sci_config(struct uart_port *port, unsigned int ctrl)
-{
-       volatile unsigned char *mstpcrl = (volatile unsigned char *)MSTPCRL;
-       int ch = (port->mapbase  - SMR0) >> 3;
-       unsigned char mask = 1 << (ch+1);
-
-       if (ctrl == sci_disable)
-               *mstpcrl |= mask;
-       else
-               *mstpcrl &= ~mask;
-}
-
-static inline void h8300_sci_enable(struct uart_port *port)
-{
-       h8300_sci_config(port, sci_enable);
-}
-
-static inline void h8300_sci_disable(struct uart_port *port)
-{
-       h8300_sci_config(port, sci_disable);
-}
-#endif
-
 #if defined(__H8300H__) || defined(__H8300S__)
-static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
+static void sci_init_pins(struct uart_port *port, unsigned int cflag)
 {
        int ch = (port->mapbase - SMR0) >> 3;
 
@@ -187,146 +167,108 @@ static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
        /* tx mark output*/
        H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
 }
-#else
-#define sci_init_pins_sci NULL
-#endif
-
-#if defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
-static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
+#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
 {
-       unsigned int fcr_val = 0;
-
-       if (cflag & CRTSCTS)
-               fcr_val |= SCFCR_MCE;
-
-       sci_out(port, SCFCR, fcr_val);
-}
-#else
-#define sci_init_pins_irda NULL
-#endif
-
-#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
-static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
-{
-       unsigned int fcr_val = 0;
-
-       set_sh771x_scif_pfc(port);
-       if (cflag & CRTSCTS)
-               fcr_val |= SCFCR_MCE;
-       sci_out(port, SCFCR, fcr_val);
+       if (port->mapbase == 0xA4400000) {
+               __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
+               __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
+       } else if (port->mapbase == 0xA4410000)
+               __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
 }
 #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
-static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
 {
-       unsigned int fcr_val = 0;
        unsigned short data;
 
        if (cflag & CRTSCTS) {
                /* enable RTS/CTS */
                if (port->mapbase == 0xa4430000) { /* SCIF0 */
                        /* Clear PTCR bit 9-2; enable all scif pins but sck */
-                       data = ctrl_inw(PORT_PTCR);
-                       ctrl_outw((data & 0xfc03), PORT_PTCR);
+                       data = __raw_readw(PORT_PTCR);
+                       __raw_writew((data & 0xfc03), PORT_PTCR);
                } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
                        /* Clear PVCR bit 9-2 */
-                       data = ctrl_inw(PORT_PVCR);
-                       ctrl_outw((data & 0xfc03), PORT_PVCR);
+                       data = __raw_readw(PORT_PVCR);
+                       __raw_writew((data & 0xfc03), PORT_PVCR);
                }
-               fcr_val |= SCFCR_MCE;
        } else {
                if (port->mapbase == 0xa4430000) { /* SCIF0 */
                        /* Clear PTCR bit 5-2; enable only tx and rx  */
-                       data = ctrl_inw(PORT_PTCR);
-                       ctrl_outw((data & 0xffc3), PORT_PTCR);
+                       data = __raw_readw(PORT_PTCR);
+                       __raw_writew((data & 0xffc3), PORT_PTCR);
                } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
                        /* Clear PVCR bit 5-2 */
-                       data = ctrl_inw(PORT_PVCR);
-                       ctrl_outw((data & 0xffc3), PORT_PVCR);
+                       data = __raw_readw(PORT_PVCR);
+                       __raw_writew((data & 0xffc3), PORT_PVCR);
                }
        }
-       sci_out(port, SCFCR, fcr_val);
 }
 #elif defined(CONFIG_CPU_SH3)
 /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
-static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
 {
-       unsigned int fcr_val = 0;
        unsigned short data;
 
        /* We need to set SCPCR to enable RTS/CTS */
-       data = ctrl_inw(SCPCR);
+       data = __raw_readw(SCPCR);
        /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
-       ctrl_outw(data & 0x0fcf, SCPCR);
+       __raw_writew(data & 0x0fcf, SCPCR);
 
-       if (cflag & CRTSCTS)
-               fcr_val |= SCFCR_MCE;
-       else {
+       if (!(cflag & CRTSCTS)) {
                /* We need to set SCPCR to enable RTS/CTS */
-               data = ctrl_inw(SCPCR);
+               data = __raw_readw(SCPCR);
                /* Clear out SCP7MD1,0, SCP4MD1,0,
                   Set SCP6MD1,0 = {01} (output)  */
-               ctrl_outw((data & 0x0fcf) | 0x1000, SCPCR);
+               __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
 
-               data = ctrl_inb(SCPDR);
+               data = __raw_readb(SCPDR);
                /* Set /RTS2 (bit6) = 0 */
-               ctrl_outb(data & 0xbf, SCPDR);
+               __raw_writeb(data & 0xbf, SCPDR);
        }
-
-       sci_out(port, SCFCR, fcr_val);
 }
 #elif defined(CONFIG_CPU_SUBTYPE_SH7722)
-static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
 {
-       unsigned int fcr_val = 0;
        unsigned short data;
 
        if (port->mapbase == 0xffe00000) {
-               data = ctrl_inw(PSCR);
+               data = __raw_readw(PSCR);
                data &= ~0x03cf;
-               if (cflag & CRTSCTS)
-                       fcr_val |= SCFCR_MCE;
-               else
+               if (!(cflag & CRTSCTS))
                        data |= 0x0340;
 
-               ctrl_outw(data, PSCR);
+               __raw_writew(data, PSCR);
        }
-       /* SCIF1 and SCIF2 should be setup by board code */
-
-       sci_out(port, SCFCR, fcr_val);
-}
-#elif defined(CONFIG_CPU_SUBTYPE_SH7723)
-static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
-{
-       /* Nothing to do here.. */
-       sci_out(port, SCFCR, 0);
 }
-#else
-/* For SH7750 */
-static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
-{
-       unsigned int fcr_val = 0;
-
-       if (cflag & CRTSCTS) {
-               fcr_val |= SCFCR_MCE;
-       } else {
-#if defined(CONFIG_CPU_SUBTYPE_SH7343) || defined(CONFIG_CPU_SUBTYPE_SH7366)
-               /* Nothing */
-#elif defined(CONFIG_CPU_SUBTYPE_SH7763) || \
+#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
+      defined(CONFIG_CPU_SUBTYPE_SH7763) || \
       defined(CONFIG_CPU_SUBTYPE_SH7780) || \
       defined(CONFIG_CPU_SUBTYPE_SH7785) || \
+      defined(CONFIG_CPU_SUBTYPE_SH7786) || \
       defined(CONFIG_CPU_SUBTYPE_SHX3)
-               ctrl_outw(0x0080, SCSPTR0); /* Set RTS = 1 */
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
+{
+       if (!(cflag & CRTSCTS))
+               __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
+}
+#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
+{
+       if (!(cflag & CRTSCTS))
+               __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
+}
 #else
-               ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
-#endif
-       }
-       sci_out(port, SCFCR, fcr_val);
+static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
+{
+       /* Nothing to do */
 }
 #endif
 
 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
     defined(CONFIG_CPU_SUBTYPE_SH7780) || \
-    defined(CONFIG_CPU_SUBTYPE_SH7785)
+    defined(CONFIG_CPU_SUBTYPE_SH7785) || \
+    defined(CONFIG_CPU_SUBTYPE_SH7786)
 static inline int scif_txroom(struct uart_port *port)
 {
        return SCIF_TXROOM_MAX - (sci_in(port, SCTFDR) & 0xff);
@@ -388,7 +330,7 @@ static inline int sci_rxroom(struct uart_port *port)
 
 static void sci_transmit_chars(struct uart_port *port)
 {
-       struct circ_buf *xmit = &port->info->xmit;
+       struct circ_buf *xmit = &port->state->xmit;
        unsigned int stopped = uart_tx_stopped(port);
        unsigned short status;
        unsigned short ctrl;
@@ -453,7 +395,7 @@ static void sci_transmit_chars(struct uart_port *port)
 static inline void sci_receive_chars(struct uart_port *port)
 {
        struct sci_port *sci_port = to_sci_port(port);
-       struct tty_struct *tty = port->info->port.tty;
+       struct tty_struct *tty = port->state->port.tty;
        int i, count, copied = 0;
        unsigned short status;
        unsigned char flag;
@@ -573,7 +515,7 @@ static inline int sci_handle_errors(struct uart_port *port)
 {
        int copied = 0;
        unsigned short status = sci_in(port, SCxSR);
-       struct tty_struct *tty = port->info->port.tty;
+       struct tty_struct *tty = port->state->port.tty;
 
        if (status & SCxSR_ORER(port)) {
                /* overrun error */
@@ -625,12 +567,33 @@ static inline int sci_handle_errors(struct uart_port *port)
        return copied;
 }
 
+static inline int sci_handle_fifo_overrun(struct uart_port *port)
+{
+       struct tty_struct *tty = port->state->port.tty;
+       int copied = 0;
+
+       if (port->type != PORT_SCIF)
+               return 0;
+
+       if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
+               sci_out(port, SCLSR, 0);
+
+               tty_insert_flip_char(tty, 0, TTY_OVERRUN);
+               tty_flip_buffer_push(tty);
+
+               dev_notice(port->dev, "overrun error\n");
+               copied++;
+       }
+
+       return copied;
+}
+
 static inline int sci_handle_breaks(struct uart_port *port)
 {
        int copied = 0;
        unsigned short status = sci_in(port, SCxSR);
-       struct tty_struct *tty = port->info->port.tty;
-       struct sci_port *s = &sci_ports[port->line];
+       struct tty_struct *tty = port->state->port.tty;
+       struct sci_port *s = to_sci_port(port);
 
        if (uart_handle_break(port))
                return 0;
@@ -647,20 +610,11 @@ static inline int sci_handle_breaks(struct uart_port *port)
                dev_dbg(port->dev, "BREAK detected\n");
        }
 
-#if defined(SCIF_ORER)
-       /* XXX: Handle SCIF overrun error */
-       if (port->type != PORT_SCI && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
-               sci_out(port, SCLSR, 0);
-               if (tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
-                       copied++;
-                       dev_notice(port->dev, "overrun error\n");
-               }
-       }
-#endif
-
        if (copied)
                tty_flip_buffer_push(tty);
 
+       copied += sci_handle_fifo_overrun(port);
+
        return copied;
 }
 
@@ -678,10 +632,11 @@ static irqreturn_t sci_rx_interrupt(int irq, void *port)
 static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
 {
        struct uart_port *port = ptr;
+       unsigned long flags;
 
-       spin_lock_irq(&port->lock);
+       spin_lock_irqsave(&port->lock, flags);
        sci_transmit_chars(port);
-       spin_unlock_irq(&port->lock);
+       spin_unlock_irqrestore(&port->lock, flags);
 
        return IRQ_HANDLED;
 }
@@ -698,16 +653,7 @@ static irqreturn_t sci_er_interrupt(int irq, void *ptr)
                        sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
                }
        } else {
-#if defined(SCIF_ORER)
-               if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
-                       struct tty_struct *tty = port->info->port.tty;
-
-                       sci_out(port, SCLSR, 0);
-                       tty_insert_flip_char(tty, 0, TTY_OVERRUN);
-                       tty_flip_buffer_push(tty);
-                       dev_notice(port->dev, "overrun error\n");
-               }
-#endif
+               sci_handle_fifo_overrun(port);
                sci_rx_interrupt(irq, ptr);
        }
 
@@ -732,30 +678,30 @@ static irqreturn_t sci_br_interrupt(int irq, void *ptr)
 
 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
 {
-       unsigned short ssr_status, scr_status;
+       unsigned short ssr_status, scr_status, err_enabled;
        struct uart_port *port = ptr;
        irqreturn_t ret = IRQ_NONE;
 
        ssr_status = sci_in(port, SCxSR);
        scr_status = sci_in(port, SCSCR);
+       err_enabled = scr_status & (SCI_CTRL_FLAGS_REIE | SCI_CTRL_FLAGS_RIE);
 
        /* Tx Interrupt */
-       if ((ssr_status & 0x0020) && (scr_status & SCI_CTRL_FLAGS_TIE))
+       if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCI_CTRL_FLAGS_TIE))
                ret = sci_tx_interrupt(irq, ptr);
        /* Rx Interrupt */
-       if ((ssr_status & 0x0002) && (scr_status & SCI_CTRL_FLAGS_RIE))
+       if ((ssr_status & SCxSR_RDxF(port)) && (scr_status & SCI_CTRL_FLAGS_RIE))
                ret = sci_rx_interrupt(irq, ptr);
        /* Error Interrupt */
-       if ((ssr_status & 0x0080) && (scr_status & SCI_CTRL_FLAGS_REIE))
+       if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
                ret = sci_er_interrupt(irq, ptr);
        /* Break Interrupt */
-       if ((ssr_status & 0x0010) && (scr_status & SCI_CTRL_FLAGS_REIE))
+       if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
                ret = sci_br_interrupt(irq, ptr);
 
        return ret;
 }
 
-#ifdef CONFIG_HAVE_CLK
 /*
  * Here we define a transistion notifier so that we can update all of our
  * ports' baud rate when the peripheral clock changes.
@@ -763,20 +709,42 @@ static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
 static int sci_notifier(struct notifier_block *self,
                        unsigned long phase, void *p)
 {
-       int i;
+       struct sh_sci_priv *priv = container_of(self,
+                                               struct sh_sci_priv, clk_nb);
+       struct sci_port *sci_port;
+       unsigned long flags;
 
        if ((phase == CPUFREQ_POSTCHANGE) ||
-           (phase == CPUFREQ_RESUMECHANGE))
-               for (i = 0; i < SCI_NPORTS; i++) {
-                       struct sci_port *s = &sci_ports[i];
-                       s->port.uartclk = clk_get_rate(s->clk);
-               }
+           (phase == CPUFREQ_RESUMECHANGE)) {
+               spin_lock_irqsave(&priv->lock, flags);
+               list_for_each_entry(sci_port, &priv->ports, node)
+                       sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
+               spin_unlock_irqrestore(&priv->lock, flags);
+       }
 
        return NOTIFY_OK;
 }
 
-static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
-#endif
+static void sci_clk_enable(struct uart_port *port)
+{
+       struct sci_port *sci_port = to_sci_port(port);
+
+       clk_enable(sci_port->dclk);
+       sci_port->port.uartclk = clk_get_rate(sci_port->dclk);
+
+       if (sci_port->iclk)
+               clk_enable(sci_port->iclk);
+}
+
+static void sci_clk_disable(struct uart_port *port)
+{
+       struct sci_port *sci_port = to_sci_port(port);
+
+       if (sci_port->iclk)
+               clk_disable(sci_port->iclk);
+
+       clk_disable(sci_port->dclk);
+}
 
 static int sci_request_irq(struct sci_port *port)
 {
@@ -831,8 +799,8 @@ static void sci_free_irq(struct sci_port *port)
 
 static unsigned int sci_tx_empty(struct uart_port *port)
 {
-       /* Can't detect */
-       return TIOCSER_TEMT;
+       unsigned short status = sci_in(port, SCxSR);
+       return status & SCxSR_TEND(port) ? TIOCSER_TEMT : 0;
 }
 
 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
@@ -902,15 +870,11 @@ static void sci_break_ctl(struct uart_port *port, int break_state)
 
 static int sci_startup(struct uart_port *port)
 {
-       struct sci_port *s = &sci_ports[port->line];
+       struct sci_port *s = to_sci_port(port);
 
        if (s->enable)
                s->enable(port);
 
-#ifdef CONFIG_HAVE_CLK
-       s->clk = clk_get(NULL, "module_clk");
-#endif
-
        sci_request_irq(s);
        sci_start_tx(port);
        sci_start_rx(port, 1);
@@ -920,7 +884,7 @@ static int sci_startup(struct uart_port *port)
 
 static void sci_shutdown(struct uart_port *port)
 {
-       struct sci_port *s = &sci_ports[port->line];
+       struct sci_port *s = to_sci_port(port);
 
        sci_stop_rx(port);
        sci_stop_tx(port);
@@ -928,22 +892,26 @@ static void sci_shutdown(struct uart_port *port)
 
        if (s->disable)
                s->disable(port);
-
-#ifdef CONFIG_HAVE_CLK
-       clk_put(s->clk);
-       s->clk = NULL;
-#endif
 }
 
 static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
                            struct ktermios *old)
 {
-       struct sci_port *s = &sci_ports[port->line];
-       unsigned int status, baud, smr_val;
+       unsigned int status, baud, smr_val, max_baud;
        int t = -1;
 
-       baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
-       if (likely(baud))
+       /*
+        * earlyprintk comes here early on with port->uartclk set to zero.
+        * the clock framework is not up and running at this point so here
+        * we assume that 115200 is the maximum baud rate. please note that
+        * the baud rate is not programmed during earlyprintk - it is assumed
+        * that the previous boot loader has enabled required clocks and
+        * setup the baud rate generator hardware for us already.
+        */
+       max_baud = port->uartclk ? port->uartclk / 16 : 115200;
+
+       baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
+       if (likely(baud && port->uartclk))
                t = SCBRR_VALUE(baud, port->uartclk);
 
        do {
@@ -980,8 +948,8 @@ static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
                udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
        }
 
-       if (likely(s->init_pins))
-               s->init_pins(port, termios->c_cflag);
+       sci_init_pins(port, termios->c_cflag);
+       sci_out(port, SCFCR, (termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0);
 
        sci_out(port, SCSCR, SCSCR_INIT(port));
 
@@ -1018,38 +986,31 @@ static int sci_request_port(struct uart_port *port)
 
 static void sci_config_port(struct uart_port *port, int flags)
 {
-       struct sci_port *s = &sci_ports[port->line];
+       struct sci_port *s = to_sci_port(port);
 
        port->type = s->type;
 
-       switch (port->type) {
-       case PORT_SCI:
-               s->init_pins = sci_init_pins_sci;
-               break;
-       case PORT_SCIF:
-       case PORT_SCIFA:
-               s->init_pins = sci_init_pins_scif;
-               break;
-       case PORT_IRDA:
-               s->init_pins = sci_init_pins_irda;
-               break;
-       }
+       if (port->membase)
+               return;
 
-       if (port->flags & UPF_IOREMAP && !port->membase) {
-#if defined(CONFIG_SUPERH64)
-               port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
-               port->membase = (void __iomem *)port->mapbase;
-#else
+       if (port->flags & UPF_IOREMAP) {
                port->membase = ioremap_nocache(port->mapbase, 0x40);
-#endif
 
-               dev_err(port->dev, "can't remap port#%d\n", port->line);
+               if (IS_ERR(port->membase))
+                       dev_err(port->dev, "can't remap port#%d\n", port->line);
+       } else {
+               /*
+                * For the simple (and majority of) cases where we don't
+                * need to do any remapping, just cast the cookie
+                * directly.
+                */
+               port->membase = (void __iomem *)port->mapbase;
        }
 }
 
 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
 {
-       struct sci_port *s = &sci_ports[port->line];
+       struct sci_port *s = to_sci_port(port);
 
        if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
                return -EINVAL;
@@ -1083,63 +1044,62 @@ static struct uart_ops sci_uart_ops = {
 #endif
 };
 
-static void __init sci_init_ports(void)
+static void __devinit sci_init_single(struct platform_device *dev,
+                                     struct sci_port *sci_port,
+                                     unsigned int index,
+                                     struct plat_sci_port *p)
 {
-       static int first = 1;
-       int i;
+       sci_port->port.ops      = &sci_uart_ops;
+       sci_port->port.iotype   = UPIO_MEM;
+       sci_port->port.line     = index;
 
-       if (!first)
-               return;
+       switch (p->type) {
+       case PORT_SCIFA:
+               sci_port->port.fifosize = 64;
+               break;
+       case PORT_SCIF:
+               sci_port->port.fifosize = 16;
+               break;
+       default:
+               sci_port->port.fifosize = 1;
+               break;
+       }
 
-       first = 0;
+       if (dev) {
+               sci_port->iclk = p->clk ? clk_get(&dev->dev, p->clk) : NULL;
+               sci_port->dclk = clk_get(&dev->dev, "peripheral_clk");
+               sci_port->enable = sci_clk_enable;
+               sci_port->disable = sci_clk_disable;
+               sci_port->port.dev = &dev->dev;
+       }
 
-       for (i = 0; i < SCI_NPORTS; i++) {
-               sci_ports[i].port.ops           = &sci_uart_ops;
-               sci_ports[i].port.iotype        = UPIO_MEM;
-               sci_ports[i].port.line          = i;
-               sci_ports[i].port.fifosize      = 1;
+       sci_port->break_timer.data = (unsigned long)sci_port;
+       sci_port->break_timer.function = sci_break_timer;
+       init_timer(&sci_port->break_timer);
 
-#if defined(__H8300H__) || defined(__H8300S__)
-#ifdef __H8300S__
-               sci_ports[i].enable     = h8300_sci_enable;
-               sci_ports[i].disable    = h8300_sci_disable;
-#endif
-               sci_ports[i].port.uartclk = CONFIG_CPU_CLOCK;
-#elif defined(CONFIG_HAVE_CLK)
-               /*
-                * XXX: We should use a proper SCI/SCIF clock
-                */
-               {
-                       struct clk *clk = clk_get(NULL, "module_clk");
-                       sci_ports[i].port.uartclk = clk_get_rate(clk);
-                       clk_put(clk);
-               }
-#else
-#error "Need a valid uartclk"
-#endif
+       sci_port->port.mapbase  = p->mapbase;
+       sci_port->port.membase  = p->membase;
 
-               sci_ports[i].break_timer.data = (unsigned long)&sci_ports[i];
-               sci_ports[i].break_timer.function = sci_break_timer;
+       sci_port->port.irq      = p->irqs[SCIx_TXI_IRQ];
+       sci_port->port.flags    = p->flags;
+       sci_port->type          = sci_port->port.type = p->type;
 
-               init_timer(&sci_ports[i].break_timer);
-       }
+       memcpy(&sci_port->irqs, &p->irqs, sizeof(p->irqs));
 }
 
-int __init early_sci_setup(struct uart_port *port)
+#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
+static struct tty_driver *serial_console_device(struct console *co, int *index)
 {
-       if (unlikely(port->line > SCI_NPORTS))
-               return -ENODEV;
-
-       sci_init_ports();
-
-       sci_ports[port->line].port.membase      = port->membase;
-       sci_ports[port->line].port.mapbase      = port->mapbase;
-       sci_ports[port->line].port.type         = port->type;
+       struct uart_driver *p = &sci_uart_driver;
+       *index = co->index;
+       return p->tty_driver;
+}
 
-       return 0;
+static void serial_console_putchar(struct uart_port *port, int ch)
+{
+       sci_poll_put_char(port, ch);
 }
 
-#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
 /*
  *     Print a string to the serial port trying not to disturb
  *     any possible real use of the port...
@@ -1147,19 +1107,27 @@ int __init early_sci_setup(struct uart_port *port)
 static void serial_console_write(struct console *co, const char *s,
                                 unsigned count)
 {
-       struct uart_port *port = &serial_console_port->port;
-       int i;
+       struct uart_port *port = co->data;
+       struct sci_port *sci_port = to_sci_port(port);
+       unsigned short bits;
 
-       for (i = 0; i < count; i++) {
-               if (*s == 10)
-                       sci_poll_put_char(port, '\r');
+       if (sci_port->enable)
+               sci_port->enable(port);
 
-               sci_poll_put_char(port, *s++);
-       }
+       uart_console_write(port, s, count, serial_console_putchar);
+
+       /* wait until fifo is empty and last bit has been transmitted */
+       bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
+       while ((sci_in(port, SCxSR) & bits) != bits)
+               cpu_relax();
+
+       if (sci_port->disable)
+               sci_port->disable(port);
 }
 
-static int __init serial_console_setup(struct console *co, char *options)
+static int __devinit serial_console_setup(struct console *co, char *options)
 {
+       struct sci_port *sci_port;
        struct uart_port *port;
        int baud = 115200;
        int bits = 8;
@@ -1175,8 +1143,14 @@ static int __init serial_console_setup(struct console *co, char *options)
        if (co->index >= SCI_NPORTS)
                co->index = 0;
 
-       serial_console_port = &sci_ports[co->index];
-       port = &serial_console_port->port;
+       if (co->data) {
+               port = co->data;
+               sci_port = to_sci_port(port);
+       } else {
+               sci_port = &sci_ports[co->index];
+               port = &sci_port->port;
+               co->data = port;
+       }
 
        /*
         * Also need to check port->type, we don't actually have any
@@ -1186,21 +1160,11 @@ static int __init serial_console_setup(struct console *co, char *options)
         */
        if (!port->type)
                return -ENODEV;
-       if (!port->membase || !port->mapbase)
-               return -ENODEV;
 
-       port->type = serial_console_port->type;
+       sci_config_port(port, 0);
 
-#ifdef CONFIG_HAVE_CLK
-       if (!serial_console_port->clk)
-               serial_console_port->clk = clk_get(NULL, "module_clk");
-#endif
-
-       if (port->flags & UPF_IOREMAP)
-               sci_config_port(port, 0);
-
-       if (serial_console_port->enable)
-               serial_console_port->enable(port);
+       if (sci_port->enable)
+               sci_port->enable(port);
 
        if (options)
                uart_parse_options(options, &baud, &parity, &bits, &flow);
@@ -1211,26 +1175,34 @@ static int __init serial_console_setup(struct console *co, char *options)
        if (ret == 0)
                sci_stop_rx(port);
 #endif
+       /* TODO: disable clock */
        return ret;
 }
 
 static struct console serial_console = {
        .name           = "ttySC",
-       .device         = uart_console_device,
+       .device         = serial_console_device,
        .write          = serial_console_write,
        .setup          = serial_console_setup,
        .flags          = CON_PRINTBUFFER,
        .index          = -1,
-       .data           = &sci_uart_driver,
 };
 
 static int __init sci_console_init(void)
 {
-       sci_init_ports();
        register_console(&serial_console);
        return 0;
 }
 console_initcall(sci_console_init);
+
+static struct sci_port early_serial_port;
+static struct console early_serial_console = {
+       .name           = "early_ttySC",
+       .write          = serial_console_write,
+       .flags          = CON_PRINTBUFFER,
+};
+static char early_serial_buf[32];
+
 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
 
 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
@@ -1252,6 +1224,58 @@ static struct uart_driver sci_uart_driver = {
        .cons           = SCI_CONSOLE,
 };
 
+
+static int sci_remove(struct platform_device *dev)
+{
+       struct sh_sci_priv *priv = platform_get_drvdata(dev);
+       struct sci_port *p;
+       unsigned long flags;
+
+       cpufreq_unregister_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
+
+       spin_lock_irqsave(&priv->lock, flags);
+       list_for_each_entry(p, &priv->ports, node)
+               uart_remove_one_port(&sci_uart_driver, &p->port);
+       spin_unlock_irqrestore(&priv->lock, flags);
+
+       kfree(priv);
+       return 0;
+}
+
+static int __devinit sci_probe_single(struct platform_device *dev,
+                                     unsigned int index,
+                                     struct plat_sci_port *p,
+                                     struct sci_port *sciport)
+{
+       struct sh_sci_priv *priv = platform_get_drvdata(dev);
+       unsigned long flags;
+       int ret;
+
+       /* Sanity check */
+       if (unlikely(index >= SCI_NPORTS)) {
+               dev_notice(&dev->dev, "Attempting to register port "
+                          "%d when only %d are available.\n",
+                          index+1, SCI_NPORTS);
+               dev_notice(&dev->dev, "Consider bumping "
+                          "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
+               return 0;
+       }
+
+       sci_init_single(dev, sciport, index, p);
+
+       ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
+       if (ret)
+               return ret;
+
+       INIT_LIST_HEAD(&sciport->node);
+
+       spin_lock_irqsave(&priv->lock, flags);
+       list_add(&sciport->node, &priv->ports);
+       spin_unlock_irqrestore(&priv->lock, flags);
+
+       return 0;
+}
+
 /*
  * Register a set of serial devices attached to a platform device.  The
  * list is terminated with a zero flags entry, which means we expect
@@ -1261,57 +1285,47 @@ static struct uart_driver sci_uart_driver = {
 static int __devinit sci_probe(struct platform_device *dev)
 {
        struct plat_sci_port *p = dev->dev.platform_data;
+       struct sh_sci_priv *priv;
        int i, ret = -EINVAL;
 
-       for (i = 0; p && p->flags != 0; p++, i++) {
-               struct sci_port *sciport = &sci_ports[i];
-
-               /* Sanity check */
-               if (unlikely(i == SCI_NPORTS)) {
-                       dev_notice(&dev->dev, "Attempting to register port "
-                                  "%d when only %d are available.\n",
-                                  i+1, SCI_NPORTS);
-                       dev_notice(&dev->dev, "Consider bumping "
-                                  "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
-                       break;
-               }
-
-               sciport->port.mapbase   = p->mapbase;
-
-               if (p->mapbase && !p->membase) {
-                       if (p->flags & UPF_IOREMAP) {
-                               p->membase = ioremap_nocache(p->mapbase, 0x40);
-                               if (IS_ERR(p->membase)) {
-                                       ret = PTR_ERR(p->membase);
-                                       goto err_unreg;
-                               }
-                       } else {
-                               /*
-                                * For the simple (and majority of) cases
-                                * where we don't need to do any remapping,
-                                * just cast the cookie directly.
-                                */
-                               p->membase = (void __iomem *)p->mapbase;
-                       }
-               }
-
-               sciport->port.membase   = p->membase;
+#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
+       if (is_early_platform_device(dev)) {
+               if (dev->id == -1)
+                       return -ENOTSUPP;
+               early_serial_console.index = dev->id;
+               early_serial_console.data = &early_serial_port.port;
+               sci_init_single(NULL, &early_serial_port, dev->id, p);
+               serial_console_setup(&early_serial_console, early_serial_buf);
+               if (!strstr(early_serial_buf, "keep"))
+                       early_serial_console.flags |= CON_BOOT;
+               register_console(&early_serial_console);
+               return 0;
+       }
+#endif
 
-               sciport->port.irq       = p->irqs[SCIx_TXI_IRQ];
-               sciport->port.flags     = p->flags;
-               sciport->port.dev       = &dev->dev;
+       priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+       if (!priv)
+               return -ENOMEM;
 
-               sciport->type           = sciport->port.type = p->type;
+       INIT_LIST_HEAD(&priv->ports);
+       spin_lock_init(&priv->lock);
+       platform_set_drvdata(dev, priv);
 
-               memcpy(&sciport->irqs, &p->irqs, sizeof(p->irqs));
+       priv->clk_nb.notifier_call = sci_notifier;
+       cpufreq_register_notifier(&priv->clk_nb, CPUFREQ_TRANSITION_NOTIFIER);
 
-               uart_add_one_port(&sci_uart_driver, &sciport->port);
+       if (dev->id != -1) {
+               ret = sci_probe_single(dev, dev->id, p, &sci_ports[dev->id]);
+               if (ret)
+                       goto err_unreg;
+       } else {
+               for (i = 0; p && p->flags != 0; p++, i++) {
+                       ret = sci_probe_single(dev, i, p, &sci_ports[i]);
+                       if (ret)
+                               goto err_unreg;
+               }
        }
 
-#ifdef CONFIG_HAVE_CLK
-       cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
-#endif
-
 #ifdef CONFIG_SH_STANDARD_BIOS
        sh_bios_gdb_detach();
 #endif
@@ -1319,62 +1333,50 @@ static int __devinit sci_probe(struct platform_device *dev)
        return 0;
 
 err_unreg:
-       for (i = i - 1; i >= 0; i--)
-               uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
-
+       sci_remove(dev);
        return ret;
 }
 
-static int __devexit sci_remove(struct platform_device *dev)
+static int sci_suspend(struct device *dev)
 {
-       int i;
-
-#ifdef CONFIG_HAVE_CLK
-       cpufreq_unregister_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
-#endif
+       struct sh_sci_priv *priv = dev_get_drvdata(dev);
+       struct sci_port *p;
+       unsigned long flags;
 
-       for (i = 0; i < SCI_NPORTS; i++)
-               uart_remove_one_port(&sci_uart_driver, &sci_ports[i].port);
+       spin_lock_irqsave(&priv->lock, flags);
+       list_for_each_entry(p, &priv->ports, node)
+               uart_suspend_port(&sci_uart_driver, &p->port);
+       spin_unlock_irqrestore(&priv->lock, flags);
 
        return 0;
 }
 
-static int sci_suspend(struct platform_device *dev, pm_message_t state)
+static int sci_resume(struct device *dev)
 {
-       int i;
+       struct sh_sci_priv *priv = dev_get_drvdata(dev);
+       struct sci_port *p;
+       unsigned long flags;
 
-       for (i = 0; i < SCI_NPORTS; i++) {
-               struct sci_port *p = &sci_ports[i];
-
-               if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
-                       uart_suspend_port(&sci_uart_driver, &p->port);
-       }
+       spin_lock_irqsave(&priv->lock, flags);
+       list_for_each_entry(p, &priv->ports, node)
+               uart_resume_port(&sci_uart_driver, &p->port);
+       spin_unlock_irqrestore(&priv->lock, flags);
 
        return 0;
 }
 
-static int sci_resume(struct platform_device *dev)
-{
-       int i;
-
-       for (i = 0; i < SCI_NPORTS; i++) {
-               struct sci_port *p = &sci_ports[i];
-
-               if (p->type != PORT_UNKNOWN && p->port.dev == &dev->dev)
-                       uart_resume_port(&sci_uart_driver, &p->port);
-       }
-
-       return 0;
-}
+static const struct dev_pm_ops sci_dev_pm_ops = {
+       .suspend        = sci_suspend,
+       .resume         = sci_resume,
+};
 
 static struct platform_driver sci_driver = {
        .probe          = sci_probe,
-       .remove         = __devexit_p(sci_remove),
-       .suspend        = sci_suspend,
-       .resume         = sci_resume,
+       .remove         = sci_remove,
        .driver         = {
                .name   = "sh-sci",
                .owner  = THIS_MODULE,
+               .pm     = &sci_dev_pm_ops,
        },
 };
 
@@ -1384,8 +1386,6 @@ static int __init sci_init(void)
 
        printk(banner);
 
-       sci_init_ports();
-
        ret = uart_register_driver(&sci_uart_driver);
        if (likely(ret == 0)) {
                ret = platform_driver_register(&sci_driver);
@@ -1402,6 +1402,10 @@ static void __exit sci_exit(void)
        uart_unregister_driver(&sci_uart_driver);
 }
 
+#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
+early_platform_init_buffer("earlyprintk", &sci_driver,
+                          early_serial_buf, ARRAY_SIZE(early_serial_buf));
+#endif
 module_init(sci_init);
 module_exit(sci_exit);