serial: PL011: move interrupt clearing
[linux-flexiantxendom0.git] / drivers / tty / serial / amba-pl011.c
index e76d7d0..6da8cf8 100644 (file)
@@ -1,6 +1,4 @@
 /*
- *  linux/drivers/char/amba.c
- *
  *  Driver for AMBA serial ports
  *
  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
@@ -52,6 +50,7 @@
 #include <linux/dmaengine.h>
 #include <linux/dma-mapping.h>
 #include <linux/scatterlist.h>
+#include <linux/delay.h>
 
 #include <asm/io.h>
 #include <asm/sizes.h>
 #define UART_DR_ERROR          (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
 #define UART_DUMMY_DR_RX       (1 << 16)
 
+
+#define UART_WA_SAVE_NR 14
+
+static void pl011_lockup_wa(unsigned long data);
+static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
+       ST_UART011_DMAWM,
+       ST_UART011_TIMEOUT,
+       ST_UART011_LCRH_RX,
+       UART011_IBRD,
+       UART011_FBRD,
+       ST_UART011_LCRH_TX,
+       UART011_IFLS,
+       ST_UART011_XFCR,
+       ST_UART011_XON1,
+       ST_UART011_XON2,
+       ST_UART011_XOFF1,
+       ST_UART011_XOFF2,
+       UART011_CR,
+       UART011_IMSC
+};
+
+static u32 uart_wa_regdata[UART_WA_SAVE_NR];
+static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
+
 /* There is by now at least one vendor with differing details, so handle it */
 struct vendor_data {
        unsigned int            ifls;
@@ -74,6 +97,7 @@ struct vendor_data {
        unsigned int            lcrh_tx;
        unsigned int            lcrh_rx;
        bool                    oversampling;
+       bool                    interrupt_may_hang;   /* vendor-specific */
        bool                    dma_threshold;
 };
 
@@ -92,10 +116,29 @@ static struct vendor_data vendor_st = {
        .lcrh_tx                = ST_UART011_LCRH_TX,
        .lcrh_rx                = ST_UART011_LCRH_RX,
        .oversampling           = true,
+       .interrupt_may_hang     = true,
        .dma_threshold          = true,
 };
 
+static struct uart_amba_port *amba_ports[UART_NR];
+
 /* Deals with DMA transactions */
+
+struct pl011_sgbuf {
+       struct scatterlist sg;
+       char *buf;
+};
+
+struct pl011_dmarx_data {
+       struct dma_chan         *chan;
+       struct completion       complete;
+       bool                    use_buf_b;
+       struct pl011_sgbuf      sgbuf_a;
+       struct pl011_sgbuf      sgbuf_b;
+       dma_cookie_t            cookie;
+       bool                    running;
+};
+
 struct pl011_dmatx_data {
        struct dma_chan         *chan;
        struct scatterlist      sg;
@@ -118,14 +161,73 @@ struct uart_amba_port {
        unsigned int            lcrh_rx;        /* vendor-specific */
        bool                    autorts;
        char                    type[12];
+       bool                    interrupt_may_hang; /* vendor-specific */
 #ifdef CONFIG_DMA_ENGINE
        /* DMA stuff */
-       bool                    using_dma;
+       bool                    using_tx_dma;
+       bool                    using_rx_dma;
+       struct pl011_dmarx_data dmarx;
        struct pl011_dmatx_data dmatx;
 #endif
 };
 
 /*
+ * Reads up to 256 characters from the FIFO or until it's empty and
+ * inserts them into the TTY layer. Returns the number of characters
+ * read from the FIFO.
+ */
+static int pl011_fifo_to_tty(struct uart_amba_port *uap)
+{
+       u16 status, ch;
+       unsigned int flag, max_count = 256;
+       int fifotaken = 0;
+
+       while (max_count--) {
+               status = readw(uap->port.membase + UART01x_FR);
+               if (status & UART01x_FR_RXFE)
+                       break;
+
+               /* Take chars from the FIFO and update status */
+               ch = readw(uap->port.membase + UART01x_DR) |
+                       UART_DUMMY_DR_RX;
+               flag = TTY_NORMAL;
+               uap->port.icount.rx++;
+               fifotaken++;
+
+               if (unlikely(ch & UART_DR_ERROR)) {
+                       if (ch & UART011_DR_BE) {
+                               ch &= ~(UART011_DR_FE | UART011_DR_PE);
+                               uap->port.icount.brk++;
+                               if (uart_handle_break(&uap->port))
+                                       continue;
+                       } else if (ch & UART011_DR_PE)
+                               uap->port.icount.parity++;
+                       else if (ch & UART011_DR_FE)
+                               uap->port.icount.frame++;
+                       if (ch & UART011_DR_OE)
+                               uap->port.icount.overrun++;
+
+                       ch &= uap->port.read_status_mask;
+
+                       if (ch & UART011_DR_BE)
+                               flag = TTY_BREAK;
+                       else if (ch & UART011_DR_PE)
+                               flag = TTY_PARITY;
+                       else if (ch & UART011_DR_FE)
+                               flag = TTY_FRAME;
+               }
+
+               if (uart_handle_sysrq_char(&uap->port, ch & 255))
+                       continue;
+
+               uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
+       }
+
+       return fifotaken;
+}
+
+
+/*
  * All the DMA operation mode stuff goes inside this ifdef.
  * This assumes that you have a generic DMA device interface,
  * no custom DMA interfaces are supported.
@@ -134,6 +236,31 @@ struct uart_amba_port {
 
 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
 
+static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
+       enum dma_data_direction dir)
+{
+       sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
+       if (!sg->buf)
+               return -ENOMEM;
+
+       sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
+
+       if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
+               kfree(sg->buf);
+               return -EINVAL;
+       }
+       return 0;
+}
+
+static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
+       enum dma_data_direction dir)
+{
+       if (sg->buf) {
+               dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
+               kfree(sg->buf);
+       }
+}
+
 static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
 {
        /* DMA is the sole user of the platform data right now */
@@ -153,7 +280,7 @@ static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
                return;
        }
 
-       /* Try to acquire a generic DMA engine slave channel */
+       /* Try to acquire a generic DMA engine slave TX channel */
        dma_cap_zero(mask);
        dma_cap_set(DMA_SLAVE, mask);
 
@@ -168,6 +295,28 @@ static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
 
        dev_info(uap->port.dev, "DMA channel TX %s\n",
                 dma_chan_name(uap->dmatx.chan));
+
+       /* Optionally make use of an RX channel as well */
+       if (plat->dma_rx_param) {
+               struct dma_slave_config rx_conf = {
+                       .src_addr = uap->port.mapbase + UART01x_DR,
+                       .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
+                       .direction = DMA_FROM_DEVICE,
+                       .src_maxburst = uap->fifosize >> 1,
+               };
+
+               chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
+               if (!chan) {
+                       dev_err(uap->port.dev, "no RX DMA channel!\n");
+                       return;
+               }
+
+               dmaengine_slave_config(chan, &rx_conf);
+               uap->dmarx.chan = chan;
+
+               dev_info(uap->port.dev, "DMA channel RX %s\n",
+                        dma_chan_name(uap->dmarx.chan));
+       }
 }
 
 #ifndef MODULE
@@ -219,9 +368,10 @@ static void pl011_dma_remove(struct uart_amba_port *uap)
        /* TODO: remove the initcall if it has not yet executed */
        if (uap->dmatx.chan)
                dma_release_channel(uap->dmatx.chan);
+       if (uap->dmarx.chan)
+               dma_release_channel(uap->dmarx.chan);
 }
 
-
 /* Forward declare this for the refill routine */
 static int pl011_dma_tx_refill(struct uart_amba_port *uap);
 
@@ -380,7 +530,7 @@ static int pl011_dma_tx_refill(struct uart_amba_port *uap)
  */
 static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
 {
-       if (!uap->using_dma)
+       if (!uap->using_tx_dma)
                return false;
 
        /*
@@ -398,7 +548,7 @@ static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
 
        /*
         * We don't have a TX buffer queued, so try to queue one.
-        * If we succesfully queued a buffer, mask the TX IRQ.
+        * If we successfully queued a buffer, mask the TX IRQ.
         */
        if (pl011_dma_tx_refill(uap) > 0) {
                uap->im &= ~UART011_TXIM;
@@ -432,7 +582,7 @@ static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
 {
        u16 dmacr;
 
-       if (!uap->using_dma)
+       if (!uap->using_tx_dma)
                return false;
 
        if (!uap->port.x_char) {
@@ -492,7 +642,7 @@ static void pl011_dma_flush_buffer(struct uart_port *port)
 {
        struct uart_amba_port *uap = (struct uart_amba_port *)port;
 
-       if (!uap->using_dma)
+       if (!uap->using_tx_dma)
                return;
 
        /* Avoid deadlock with the DMA engine callback */
@@ -508,9 +658,219 @@ static void pl011_dma_flush_buffer(struct uart_port *port)
        }
 }
 
+static void pl011_dma_rx_callback(void *data);
+
+static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
+{
+       struct dma_chan *rxchan = uap->dmarx.chan;
+       struct dma_device *dma_dev;
+       struct pl011_dmarx_data *dmarx = &uap->dmarx;
+       struct dma_async_tx_descriptor *desc;
+       struct pl011_sgbuf *sgbuf;
+
+       if (!rxchan)
+               return -EIO;
+
+       /* Start the RX DMA job */
+       sgbuf = uap->dmarx.use_buf_b ?
+               &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
+       dma_dev = rxchan->device;
+       desc = rxchan->device->device_prep_slave_sg(rxchan, &sgbuf->sg, 1,
+                                       DMA_FROM_DEVICE,
+                                       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+       /*
+        * If the DMA engine is busy and cannot prepare a
+        * channel, no big deal, the driver will fall back
+        * to interrupt mode as a result of this error code.
+        */
+       if (!desc) {
+               uap->dmarx.running = false;
+               dmaengine_terminate_all(rxchan);
+               return -EBUSY;
+       }
+
+       /* Some data to go along to the callback */
+       desc->callback = pl011_dma_rx_callback;
+       desc->callback_param = uap;
+       dmarx->cookie = dmaengine_submit(desc);
+       dma_async_issue_pending(rxchan);
+
+       uap->dmacr |= UART011_RXDMAE;
+       writew(uap->dmacr, uap->port.membase + UART011_DMACR);
+       uap->dmarx.running = true;
+
+       uap->im &= ~UART011_RXIM;
+       writew(uap->im, uap->port.membase + UART011_IMSC);
+
+       return 0;
+}
+
+/*
+ * This is called when either the DMA job is complete, or
+ * the FIFO timeout interrupt occurred. This must be called
+ * with the port spinlock uap->port.lock held.
+ */
+static void pl011_dma_rx_chars(struct uart_amba_port *uap,
+                              u32 pending, bool use_buf_b,
+                              bool readfifo)
+{
+       struct tty_struct *tty = uap->port.state->port.tty;
+       struct pl011_sgbuf *sgbuf = use_buf_b ?
+               &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
+       struct device *dev = uap->dmarx.chan->device->dev;
+       int dma_count = 0;
+       u32 fifotaken = 0; /* only used for vdbg() */
+
+       /* Pick everything from the DMA first */
+       if (pending) {
+               /* Sync in buffer */
+               dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
+
+               /*
+                * First take all chars in the DMA pipe, then look in the FIFO.
+                * Note that tty_insert_flip_buf() tries to take as many chars
+                * as it can.
+                */
+               dma_count = tty_insert_flip_string(uap->port.state->port.tty,
+                                                  sgbuf->buf, pending);
+
+               /* Return buffer to device */
+               dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
+
+               uap->port.icount.rx += dma_count;
+               if (dma_count < pending)
+                       dev_warn(uap->port.dev,
+                                "couldn't insert all characters (TTY is full?)\n");
+       }
+
+       /*
+        * Only continue with trying to read the FIFO if all DMA chars have
+        * been taken first.
+        */
+       if (dma_count == pending && readfifo) {
+               /* Clear any error flags */
+               writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
+                      uap->port.membase + UART011_ICR);
+
+               /*
+                * If we read all the DMA'd characters, and we had an
+                * incomplete buffer, that could be due to an rx error, or
+                * maybe we just timed out. Read any pending chars and check
+                * the error status.
+                *
+                * Error conditions will only occur in the FIFO, these will
+                * trigger an immediate interrupt and stop the DMA job, so we
+                * will always find the error in the FIFO, never in the DMA
+                * buffer.
+                */
+               fifotaken = pl011_fifo_to_tty(uap);
+       }
+
+       spin_unlock(&uap->port.lock);
+       dev_vdbg(uap->port.dev,
+                "Took %d chars from DMA buffer and %d chars from the FIFO\n",
+                dma_count, fifotaken);
+       tty_flip_buffer_push(tty);
+       spin_lock(&uap->port.lock);
+}
+
+static void pl011_dma_rx_irq(struct uart_amba_port *uap)
+{
+       struct pl011_dmarx_data *dmarx = &uap->dmarx;
+       struct dma_chan *rxchan = dmarx->chan;
+       struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
+               &dmarx->sgbuf_b : &dmarx->sgbuf_a;
+       size_t pending;
+       struct dma_tx_state state;
+       enum dma_status dmastat;
+
+       /*
+        * Pause the transfer so we can trust the current counter,
+        * do this before we pause the PL011 block, else we may
+        * overflow the FIFO.
+        */
+       if (dmaengine_pause(rxchan))
+               dev_err(uap->port.dev, "unable to pause DMA transfer\n");
+       dmastat = rxchan->device->device_tx_status(rxchan,
+                                                  dmarx->cookie, &state);
+       if (dmastat != DMA_PAUSED)
+               dev_err(uap->port.dev, "unable to pause DMA transfer\n");
+
+       /* Disable RX DMA - incoming data will wait in the FIFO */
+       uap->dmacr &= ~UART011_RXDMAE;
+       writew(uap->dmacr, uap->port.membase + UART011_DMACR);
+       uap->dmarx.running = false;
+
+       pending = sgbuf->sg.length - state.residue;
+       BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
+       /* Then we terminate the transfer - we now know our residue */
+       dmaengine_terminate_all(rxchan);
+
+       /*
+        * This will take the chars we have so far and insert
+        * into the framework.
+        */
+       pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
+
+       /* Switch buffer & re-trigger DMA job */
+       dmarx->use_buf_b = !dmarx->use_buf_b;
+       if (pl011_dma_rx_trigger_dma(uap)) {
+               dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
+                       "fall back to interrupt mode\n");
+               uap->im |= UART011_RXIM;
+               writew(uap->im, uap->port.membase + UART011_IMSC);
+       }
+}
+
+static void pl011_dma_rx_callback(void *data)
+{
+       struct uart_amba_port *uap = data;
+       struct pl011_dmarx_data *dmarx = &uap->dmarx;
+       bool lastbuf = dmarx->use_buf_b;
+       int ret;
+
+       /*
+        * This completion interrupt occurs typically when the
+        * RX buffer is totally stuffed but no timeout has yet
+        * occurred. When that happens, we just want the RX
+        * routine to flush out the secondary DMA buffer while
+        * we immediately trigger the next DMA job.
+        */
+       spin_lock_irq(&uap->port.lock);
+       uap->dmarx.running = false;
+       dmarx->use_buf_b = !lastbuf;
+       ret = pl011_dma_rx_trigger_dma(uap);
+
+       pl011_dma_rx_chars(uap, PL011_DMA_BUFFER_SIZE, lastbuf, false);
+       spin_unlock_irq(&uap->port.lock);
+       /*
+        * Do this check after we picked the DMA chars so we don't
+        * get some IRQ immediately from RX.
+        */
+       if (ret) {
+               dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
+                       "fall back to interrupt mode\n");
+               uap->im |= UART011_RXIM;
+               writew(uap->im, uap->port.membase + UART011_IMSC);
+       }
+}
+
+/*
+ * Stop accepting received characters, when we're shutting down or
+ * suspending this port.
+ * Locking: called with port lock held and IRQs disabled.
+ */
+static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
+{
+       /* FIXME.  Just disable the DMA enable */
+       uap->dmacr &= ~UART011_RXDMAE;
+       writew(uap->dmacr, uap->port.membase + UART011_DMACR);
+}
 
 static void pl011_dma_startup(struct uart_amba_port *uap)
 {
+       int ret;
+
        if (!uap->dmatx.chan)
                return;
 
@@ -525,8 +885,33 @@ static void pl011_dma_startup(struct uart_amba_port *uap)
 
        /* The DMA buffer is now the FIFO the TTY subsystem can use */
        uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
-       uap->using_dma = true;
+       uap->using_tx_dma = true;
 
+       if (!uap->dmarx.chan)
+               goto skip_rx;
+
+       /* Allocate and map DMA RX buffers */
+       ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
+                              DMA_FROM_DEVICE);
+       if (ret) {
+               dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
+                       "RX buffer A", ret);
+               goto skip_rx;
+       }
+
+       ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
+                              DMA_FROM_DEVICE);
+       if (ret) {
+               dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
+                       "RX buffer B", ret);
+               pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
+                                DMA_FROM_DEVICE);
+               goto skip_rx;
+       }
+
+       uap->using_rx_dma = true;
+
+skip_rx:
        /* Turn on DMA error (RX/TX will be enabled on demand) */
        uap->dmacr |= UART011_DMAONERR;
        writew(uap->dmacr, uap->port.membase + UART011_DMACR);
@@ -539,11 +924,17 @@ static void pl011_dma_startup(struct uart_amba_port *uap)
        if (uap->vendor->dma_threshold)
                writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
                               uap->port.membase + ST_UART011_DMAWM);
+
+       if (uap->using_rx_dma) {
+               if (pl011_dma_rx_trigger_dma(uap))
+                       dev_dbg(uap->port.dev, "could not trigger initial "
+                               "RX DMA job, fall back to interrupt mode\n");
+       }
 }
 
 static void pl011_dma_shutdown(struct uart_amba_port *uap)
 {
-       if (!uap->using_dma)
+       if (!(uap->using_tx_dma || uap->using_rx_dma))
                return;
 
        /* Disable RX and TX DMA */
@@ -555,19 +946,39 @@ static void pl011_dma_shutdown(struct uart_amba_port *uap)
        writew(uap->dmacr, uap->port.membase + UART011_DMACR);
        spin_unlock_irq(&uap->port.lock);
 
-       /* In theory, this should already be done by pl011_dma_flush_buffer */
-       dmaengine_terminate_all(uap->dmatx.chan);
-       if (uap->dmatx.queued) {
-               dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
-                            DMA_TO_DEVICE);
-               uap->dmatx.queued = false;
+       if (uap->using_tx_dma) {
+               /* In theory, this should already be done by pl011_dma_flush_buffer */
+               dmaengine_terminate_all(uap->dmatx.chan);
+               if (uap->dmatx.queued) {
+                       dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
+                                    DMA_TO_DEVICE);
+                       uap->dmatx.queued = false;
+               }
+
+               kfree(uap->dmatx.buf);
+               uap->using_tx_dma = false;
+       }
+
+       if (uap->using_rx_dma) {
+               dmaengine_terminate_all(uap->dmarx.chan);
+               /* Clean up the RX DMA */
+               pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
+               pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
+               uap->using_rx_dma = false;
        }
+}
 
-       kfree(uap->dmatx.buf);
+static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
+{
+       return uap->using_rx_dma;
+}
 
-       uap->using_dma = false;
+static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
+{
+       return uap->using_rx_dma && uap->dmarx.running;
 }
 
+
 #else
 /* Blank functions if the DMA engine is not available */
 static inline void pl011_dma_probe(struct uart_amba_port *uap)
@@ -600,10 +1011,95 @@ static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
        return false;
 }
 
+static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
+{
+}
+
+static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
+{
+}
+
+static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
+{
+       return -EIO;
+}
+
+static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
+{
+       return false;
+}
+
+static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
+{
+       return false;
+}
+
 #define pl011_dma_flush_buffer NULL
 #endif
 
 
+/*
+ * pl011_lockup_wa
+ * This workaround aims to break the deadlock situation
+ * when after long transfer over uart in hardware flow
+ * control, uart interrupt registers cannot be cleared.
+ * Hence uart transfer gets blocked.
+ *
+ * It is seen that during such deadlock condition ICR
+ * don't get cleared even on multiple write. This leads
+ * pass_counter to decrease and finally reach zero. This
+ * can be taken as trigger point to run this UART_BT_WA.
+ *
+ */
+static void pl011_lockup_wa(unsigned long data)
+{
+       struct uart_amba_port *uap = amba_ports[0];
+       void __iomem *base = uap->port.membase;
+       struct circ_buf *xmit = &uap->port.state->xmit;
+       struct tty_struct *tty = uap->port.state->port.tty;
+       int buf_empty_retries = 200;
+       int loop;
+
+       /* Stop HCI layer from submitting data for tx */
+       tty->hw_stopped = 1;
+       while (!uart_circ_empty(xmit)) {
+               if (buf_empty_retries-- == 0)
+                       break;
+               udelay(100);
+       }
+
+       /* Backup registers */
+       for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
+               uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
+
+       /* Disable UART so that FIFO data is flushed out */
+       writew(0x00, uap->port.membase + UART011_CR);
+
+       /* Soft reset UART module */
+       if (uap->port.dev->platform_data) {
+               struct amba_pl011_data *plat;
+
+               plat = uap->port.dev->platform_data;
+               if (plat->reset)
+                       plat->reset();
+       }
+
+       /* Restore registers */
+       for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
+               writew(uart_wa_regdata[loop] ,
+                               uap->port.membase + uart_wa_reg[loop]);
+
+       /* Initialise the old status of the modem signals */
+       uap->old_status = readw(uap->port.membase + UART01x_FR) &
+               UART01x_FR_MODEM_ANY;
+
+       if (readl(base + UART011_MIS) & 0x2)
+               printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
+
+       /* Start Tx/Rx */
+       tty->hw_stopped = 0;
+}
+
 static void pl011_stop_tx(struct uart_port *port)
 {
        struct uart_amba_port *uap = (struct uart_amba_port *)port;
@@ -630,6 +1126,8 @@ static void pl011_stop_rx(struct uart_port *port)
        uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
                     UART011_PEIM|UART011_BEIM|UART011_OEIM);
        writew(uap->im, uap->port.membase + UART011_IMSC);
+
+       pl011_dma_rx_stop(uap);
 }
 
 static void pl011_enable_ms(struct uart_port *port)
@@ -643,51 +1141,24 @@ static void pl011_enable_ms(struct uart_port *port)
 static void pl011_rx_chars(struct uart_amba_port *uap)
 {
        struct tty_struct *tty = uap->port.state->port.tty;
-       unsigned int status, ch, flag, max_count = 256;
 
-       status = readw(uap->port.membase + UART01x_FR);
-       while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
-               ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
-               flag = TTY_NORMAL;
-               uap->port.icount.rx++;
+       pl011_fifo_to_tty(uap);
 
-               /*
-                * Note that the error handling code is
-                * out of the main execution path
-                */
-               if (unlikely(ch & UART_DR_ERROR)) {
-                       if (ch & UART011_DR_BE) {
-                               ch &= ~(UART011_DR_FE | UART011_DR_PE);
-                               uap->port.icount.brk++;
-                               if (uart_handle_break(&uap->port))
-                                       goto ignore_char;
-                       } else if (ch & UART011_DR_PE)
-                               uap->port.icount.parity++;
-                       else if (ch & UART011_DR_FE)
-                               uap->port.icount.frame++;
-                       if (ch & UART011_DR_OE)
-                               uap->port.icount.overrun++;
-
-                       ch &= uap->port.read_status_mask;
-
-                       if (ch & UART011_DR_BE)
-                               flag = TTY_BREAK;
-                       else if (ch & UART011_DR_PE)
-                               flag = TTY_PARITY;
-                       else if (ch & UART011_DR_FE)
-                               flag = TTY_FRAME;
-               }
-
-               if (uart_handle_sysrq_char(&uap->port, ch & 255))
-                       goto ignore_char;
-
-               uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
-
-       ignore_char:
-               status = readw(uap->port.membase + UART01x_FR);
-       }
        spin_unlock(&uap->port.lock);
        tty_flip_buffer_push(tty);
+       /*
+        * If we were temporarily out of DMA mode for a while,
+        * attempt to switch back to DMA mode again.
+        */
+       if (pl011_dma_rx_available(uap)) {
+               if (pl011_dma_rx_trigger_dma(uap)) {
+                       dev_dbg(uap->port.dev, "could not trigger RX DMA job "
+                               "fall back to interrupt mode again\n");
+                       uap->im |= UART011_RXIM;
+               } else
+                       uap->im &= ~UART011_RXIM;
+               writew(uap->im, uap->port.membase + UART011_IMSC);
+       }
        spin_lock(&uap->port.lock);
 }
 
@@ -767,16 +1238,23 @@ static irqreturn_t pl011_int(int irq, void *dev_id)
                                          UART011_RXIS),
                               uap->port.membase + UART011_ICR);
 
-                       if (status & (UART011_RTIS|UART011_RXIS))
-                               pl011_rx_chars(uap);
+                       if (status & (UART011_RTIS|UART011_RXIS)) {
+                               if (pl011_dma_rx_running(uap))
+                                       pl011_dma_rx_irq(uap);
+                               else
+                                       pl011_rx_chars(uap);
+                       }
                        if (status & (UART011_DSRMIS|UART011_DCDMIS|
                                      UART011_CTSMIS|UART011_RIMIS))
                                pl011_modem_status(uap);
                        if (status & UART011_TXIS)
                                pl011_tx_chars(uap);
 
-                       if (pass_counter-- == 0)
+                       if (pass_counter-- == 0) {
+                               if (uap->interrupt_may_hang)
+                                       tasklet_schedule(&pl011_lockup_tlet);
                                break;
+                       }
 
                        status = readw(uap->port.membase + UART011_MIS);
                } while (status != 0);
@@ -889,15 +1367,23 @@ static int pl011_startup(struct uart_port *port)
        unsigned int cr;
        int retval;
 
+       retval = clk_prepare(uap->clk);
+       if (retval)
+               goto out;
+
        /*
         * Try to enable the clock producer.
         */
        retval = clk_enable(uap->clk);
        if (retval)
-               goto out;
+               goto clk_unprep;
 
        uap->port.uartclk = clk_get_rate(uap->clk);
 
+       /* Clear pending error and receive interrupts */
+       writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
+              UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
+
        /*
         * Allocate the IRQ
         */
@@ -932,10 +1418,6 @@ static int pl011_startup(struct uart_port *port)
        cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
        writew(cr, uap->port.membase + UART011_CR);
 
-       /* Clear pending error interrupts */
-       writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
-              uap->port.membase + UART011_ICR);
-
        /*
         * initialise the old status of the modem signals
         */
@@ -945,17 +1427,34 @@ static int pl011_startup(struct uart_port *port)
        pl011_dma_startup(uap);
 
        /*
-        * Finally, enable interrupts
+        * Finally, enable interrupts, only timeouts when using DMA
+        * if initial RX DMA job failed, start in interrupt mode
+        * as well.
         */
        spin_lock_irq(&uap->port.lock);
-       uap->im = UART011_RXIM | UART011_RTIM;
+       /* Clear out any spuriously appearing RX interrupts */
+        writew(UART011_RTIS | UART011_RXIS,
+               uap->port.membase + UART011_ICR);
+       uap->im = UART011_RTIM;
+       if (!pl011_dma_rx_running(uap))
+               uap->im |= UART011_RXIM;
        writew(uap->im, uap->port.membase + UART011_IMSC);
        spin_unlock_irq(&uap->port.lock);
 
+       if (uap->port.dev->platform_data) {
+               struct amba_pl011_data *plat;
+
+               plat = uap->port.dev->platform_data;
+               if (plat->init)
+                       plat->init();
+       }
+
        return 0;
 
  clk_dis:
        clk_disable(uap->clk);
+ clk_unprep:
+       clk_unprepare(uap->clk);
  out:
        return retval;
 }
@@ -1007,6 +1506,16 @@ static void pl011_shutdown(struct uart_port *port)
         * Shut down the clock producer
         */
        clk_disable(uap->clk);
+       clk_unprepare(uap->clk);
+
+       if (uap->port.dev->platform_data) {
+               struct amba_pl011_data *plat;
+
+               plat = uap->port.dev->platform_data;
+               if (plat->exit)
+                       plat->exit();
+       }
+
 }
 
 static void
@@ -1234,9 +1743,19 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
 {
        struct uart_amba_port *uap = amba_ports[co->index];
        unsigned int status, old_cr, new_cr;
+       unsigned long flags;
+       int locked = 1;
 
        clk_enable(uap->clk);
 
+       local_irq_save(flags);
+       if (uap->port.sysrq)
+               locked = 0;
+       else if (oops_in_progress)
+               locked = spin_trylock(&uap->port.lock);
+       else
+               spin_lock(&uap->port.lock);
+
        /*
         *      First save the CR then disable the interrupts
         */
@@ -1256,6 +1775,10 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
        } while (status & UART01x_FR_BUSY);
        writew(old_cr, uap->port.membase + UART011_CR);
 
+       if (locked)
+               spin_unlock(&uap->port.lock);
+       local_irq_restore(flags);
+
        clk_disable(uap->clk);
 }
 
@@ -1301,6 +1824,7 @@ static int __init pl011_console_setup(struct console *co, char *options)
        int bits = 8;
        int parity = 'n';
        int flow = 'n';
+       int ret;
 
        /*
         * Check whether an invalid uart number has been specified, and
@@ -1313,6 +1837,18 @@ static int __init pl011_console_setup(struct console *co, char *options)
        if (!uap)
                return -ENODEV;
 
+       ret = clk_prepare(uap->clk);
+       if (ret)
+               return ret;
+
+       if (uap->port.dev->platform_data) {
+               struct amba_pl011_data *plat;
+
+               plat = uap->port.dev->platform_data;
+               if (plat->init)
+                       plat->init();
+       }
+
        uap->port.uartclk = clk_get_rate(uap->clk);
 
        if (options)
@@ -1349,7 +1885,7 @@ static struct uart_driver amba_reg = {
        .cons                   = AMBA_CONSOLE,
 };
 
-static int pl011_probe(struct amba_device *dev, struct amba_id *id)
+static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
 {
        struct uart_amba_port *uap;
        struct vendor_data *vendor = id->data;
@@ -1387,6 +1923,7 @@ static int pl011_probe(struct amba_device *dev, struct amba_id *id)
        uap->lcrh_rx = vendor->lcrh_rx;
        uap->lcrh_tx = vendor->lcrh_tx;
        uap->fifosize = vendor->fifosize;
+       uap->interrupt_may_hang = vendor->interrupt_may_hang;
        uap->port.dev = &dev->dev;
        uap->port.mapbase = dev->res.start;
        uap->port.membase = base;
@@ -1398,6 +1935,10 @@ static int pl011_probe(struct amba_device *dev, struct amba_id *id)
        uap->port.line = i;
        pl011_dma_probe(uap);
 
+       /* Ensure interrupts from this UART are masked and cleared */
+       writew(0, uap->port.membase + UART011_IMSC);
+       writew(0xffff, uap->port.membase + UART011_ICR);
+
        snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
 
        amba_ports[i] = uap;