Merge branch 'next' of git://git.infradead.org/users/vkoul/slave-dma
[linux-2.6-block.git] / drivers / tty / serial / amba-pl011.c
index f9dcb5379b92c5cda3ff32a9463bcae1283ef3b6..0c65c9e669867bf70ed3c86e61e06b3846bf5b60 100644 (file)
@@ -160,6 +160,7 @@ struct uart_amba_port {
        unsigned int            fifosize;       /* vendor-specific */
        unsigned int            lcrh_tx;        /* vendor-specific */
        unsigned int            lcrh_rx;        /* vendor-specific */
+       unsigned int            old_cr;         /* state during shutdown */
        bool                    autorts;
        char                    type[12];
        bool                    interrupt_may_hang; /* vendor-specific */
@@ -827,7 +828,12 @@ static void pl011_dma_rx_callback(void *data)
 {
        struct uart_amba_port *uap = data;
        struct pl011_dmarx_data *dmarx = &uap->dmarx;
+       struct dma_chan *rxchan = dmarx->chan;
        bool lastbuf = dmarx->use_buf_b;
+       struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
+               &dmarx->sgbuf_b : &dmarx->sgbuf_a;
+       size_t pending;
+       struct dma_tx_state state;
        int ret;
 
        /*
@@ -838,11 +844,21 @@ static void pl011_dma_rx_callback(void *data)
         * we immediately trigger the next DMA job.
         */
        spin_lock_irq(&uap->port.lock);
+       /*
+        * Rx data can be taken by the UART interrupts during
+        * the DMA irq handler. So we check the residue here.
+        */
+       rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
+       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);
+
        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);
+       pl011_dma_rx_chars(uap, pending, lastbuf, false);
        spin_unlock_irq(&uap->port.lock);
        /*
         * Do this check after we picked the DMA chars so we don't
@@ -1381,6 +1397,10 @@ static int pl011_startup(struct uart_port *port)
 
        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
         */
@@ -1412,13 +1432,11 @@ static int pl011_startup(struct uart_port *port)
        while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
                barrier();
 
-       cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
+       /* restore RTS and DTR */
+       cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
+       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
         */
@@ -1433,6 +1451,9 @@ static int pl011_startup(struct uart_port *port)
         * as well.
         */
        spin_lock_irq(&uap->port.lock);
+       /* 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;
@@ -1470,6 +1491,7 @@ static void pl011_shutdown_channel(struct uart_amba_port *uap,
 static void pl011_shutdown(struct uart_port *port)
 {
        struct uart_amba_port *uap = (struct uart_amba_port *)port;
+       unsigned int cr;
 
        /*
         * disable all interrupts
@@ -1489,9 +1511,16 @@ static void pl011_shutdown(struct uart_port *port)
 
        /*
         * disable the port
+        * disable the port. It should not disable RTS and DTR.
+        * Also RTS and DTR state should be preserved to restore
+        * it during startup().
         */
        uap->autorts = false;
-       writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
+       cr = readw(uap->port.membase + UART011_CR);
+       uap->old_cr = cr;
+       cr &= UART011_CR_RTS | UART011_CR_DTR;
+       cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
+       writew(cr, uap->port.membase + UART011_CR);
 
        /*
         * disable break condition and fifos
@@ -1741,9 +1770,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
         */
@@ -1763,6 +1802,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);
 }
 
@@ -1903,9 +1946,14 @@ static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
                goto unmap;
        }
 
+       /* Ensure interrupts from this UART are masked and cleared */
+       writew(0, uap->port.membase + UART011_IMSC);
+       writew(0xffff, uap->port.membase + UART011_ICR);
+
        uap->vendor = vendor;
        uap->lcrh_rx = vendor->lcrh_rx;
        uap->lcrh_tx = vendor->lcrh_tx;
+       uap->old_cr = 0;
        uap->fifosize = vendor->fifosize;
        uap->interrupt_may_hang = vendor->interrupt_may_hang;
        uap->port.dev = &dev->dev;