Merge tag 'soc' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[pandora-kernel.git] / drivers / tty / serial / amba-pl011.c
1 /*
2  *  Driver for AMBA serial ports
3  *
4  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5  *
6  *  Copyright 1999 ARM Limited
7  *  Copyright (C) 2000 Deep Blue Solutions Ltd.
8  *  Copyright (C) 2010 ST-Ericsson SA
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * This is a generic driver for ARM AMBA-type serial ports.  They
25  * have a lot of 16550-like features, but are not register compatible.
26  * Note that although they do have CTS, DCD and DSR inputs, they do
27  * not have an RI input, nor do they have DTR or RTS outputs.  If
28  * required, these have to be supplied via some other means (eg, GPIO)
29  * and hooked into this driver.
30  */
31
32 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
33 #define SUPPORT_SYSRQ
34 #endif
35
36 #include <linux/module.h>
37 #include <linux/ioport.h>
38 #include <linux/init.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/device.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/serial_core.h>
45 #include <linux/serial.h>
46 #include <linux/amba/bus.h>
47 #include <linux/amba/serial.h>
48 #include <linux/clk.h>
49 #include <linux/slab.h>
50 #include <linux/dmaengine.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/scatterlist.h>
53 #include <linux/delay.h>
54
55 #include <asm/io.h>
56 #include <asm/sizes.h>
57
58 #define UART_NR                 14
59
60 #define SERIAL_AMBA_MAJOR       204
61 #define SERIAL_AMBA_MINOR       64
62 #define SERIAL_AMBA_NR          UART_NR
63
64 #define AMBA_ISR_PASS_LIMIT     256
65
66 #define UART_DR_ERROR           (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
67 #define UART_DUMMY_DR_RX        (1 << 16)
68
69
70 #define UART_WA_SAVE_NR 14
71
72 static void pl011_lockup_wa(unsigned long data);
73 static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
74         ST_UART011_DMAWM,
75         ST_UART011_TIMEOUT,
76         ST_UART011_LCRH_RX,
77         UART011_IBRD,
78         UART011_FBRD,
79         ST_UART011_LCRH_TX,
80         UART011_IFLS,
81         ST_UART011_XFCR,
82         ST_UART011_XON1,
83         ST_UART011_XON2,
84         ST_UART011_XOFF1,
85         ST_UART011_XOFF2,
86         UART011_CR,
87         UART011_IMSC
88 };
89
90 static u32 uart_wa_regdata[UART_WA_SAVE_NR];
91 static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
92
93 /* There is by now at least one vendor with differing details, so handle it */
94 struct vendor_data {
95         unsigned int            ifls;
96         unsigned int            fifosize;
97         unsigned int            lcrh_tx;
98         unsigned int            lcrh_rx;
99         bool                    oversampling;
100         bool                    interrupt_may_hang;   /* vendor-specific */
101         bool                    dma_threshold;
102 };
103
104 static struct vendor_data vendor_arm = {
105         .ifls                   = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
106         .fifosize               = 16,
107         .lcrh_tx                = UART011_LCRH,
108         .lcrh_rx                = UART011_LCRH,
109         .oversampling           = false,
110         .dma_threshold          = false,
111 };
112
113 static struct vendor_data vendor_st = {
114         .ifls                   = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
115         .fifosize               = 64,
116         .lcrh_tx                = ST_UART011_LCRH_TX,
117         .lcrh_rx                = ST_UART011_LCRH_RX,
118         .oversampling           = true,
119         .interrupt_may_hang     = true,
120         .dma_threshold          = true,
121 };
122
123 static struct uart_amba_port *amba_ports[UART_NR];
124
125 /* Deals with DMA transactions */
126
127 struct pl011_sgbuf {
128         struct scatterlist sg;
129         char *buf;
130 };
131
132 struct pl011_dmarx_data {
133         struct dma_chan         *chan;
134         struct completion       complete;
135         bool                    use_buf_b;
136         struct pl011_sgbuf      sgbuf_a;
137         struct pl011_sgbuf      sgbuf_b;
138         dma_cookie_t            cookie;
139         bool                    running;
140 };
141
142 struct pl011_dmatx_data {
143         struct dma_chan         *chan;
144         struct scatterlist      sg;
145         char                    *buf;
146         bool                    queued;
147 };
148
149 /*
150  * We wrap our port structure around the generic uart_port.
151  */
152 struct uart_amba_port {
153         struct uart_port        port;
154         struct clk              *clk;
155         const struct vendor_data *vendor;
156         unsigned int            dmacr;          /* dma control reg */
157         unsigned int            im;             /* interrupt mask */
158         unsigned int            old_status;
159         unsigned int            fifosize;       /* vendor-specific */
160         unsigned int            lcrh_tx;        /* vendor-specific */
161         unsigned int            lcrh_rx;        /* vendor-specific */
162         unsigned int            old_cr;         /* state during shutdown */
163         bool                    autorts;
164         char                    type[12];
165         bool                    interrupt_may_hang; /* vendor-specific */
166 #ifdef CONFIG_DMA_ENGINE
167         /* DMA stuff */
168         bool                    using_tx_dma;
169         bool                    using_rx_dma;
170         struct pl011_dmarx_data dmarx;
171         struct pl011_dmatx_data dmatx;
172 #endif
173 };
174
175 /*
176  * Reads up to 256 characters from the FIFO or until it's empty and
177  * inserts them into the TTY layer. Returns the number of characters
178  * read from the FIFO.
179  */
180 static int pl011_fifo_to_tty(struct uart_amba_port *uap)
181 {
182         u16 status, ch;
183         unsigned int flag, max_count = 256;
184         int fifotaken = 0;
185
186         while (max_count--) {
187                 status = readw(uap->port.membase + UART01x_FR);
188                 if (status & UART01x_FR_RXFE)
189                         break;
190
191                 /* Take chars from the FIFO and update status */
192                 ch = readw(uap->port.membase + UART01x_DR) |
193                         UART_DUMMY_DR_RX;
194                 flag = TTY_NORMAL;
195                 uap->port.icount.rx++;
196                 fifotaken++;
197
198                 if (unlikely(ch & UART_DR_ERROR)) {
199                         if (ch & UART011_DR_BE) {
200                                 ch &= ~(UART011_DR_FE | UART011_DR_PE);
201                                 uap->port.icount.brk++;
202                                 if (uart_handle_break(&uap->port))
203                                         continue;
204                         } else if (ch & UART011_DR_PE)
205                                 uap->port.icount.parity++;
206                         else if (ch & UART011_DR_FE)
207                                 uap->port.icount.frame++;
208                         if (ch & UART011_DR_OE)
209                                 uap->port.icount.overrun++;
210
211                         ch &= uap->port.read_status_mask;
212
213                         if (ch & UART011_DR_BE)
214                                 flag = TTY_BREAK;
215                         else if (ch & UART011_DR_PE)
216                                 flag = TTY_PARITY;
217                         else if (ch & UART011_DR_FE)
218                                 flag = TTY_FRAME;
219                 }
220
221                 if (uart_handle_sysrq_char(&uap->port, ch & 255))
222                         continue;
223
224                 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
225         }
226
227         return fifotaken;
228 }
229
230
231 /*
232  * All the DMA operation mode stuff goes inside this ifdef.
233  * This assumes that you have a generic DMA device interface,
234  * no custom DMA interfaces are supported.
235  */
236 #ifdef CONFIG_DMA_ENGINE
237
238 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
239
240 static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
241         enum dma_data_direction dir)
242 {
243         sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
244         if (!sg->buf)
245                 return -ENOMEM;
246
247         sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
248
249         if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
250                 kfree(sg->buf);
251                 return -EINVAL;
252         }
253         return 0;
254 }
255
256 static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
257         enum dma_data_direction dir)
258 {
259         if (sg->buf) {
260                 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
261                 kfree(sg->buf);
262         }
263 }
264
265 static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
266 {
267         /* DMA is the sole user of the platform data right now */
268         struct amba_pl011_data *plat = uap->port.dev->platform_data;
269         struct dma_slave_config tx_conf = {
270                 .dst_addr = uap->port.mapbase + UART01x_DR,
271                 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
272                 .direction = DMA_MEM_TO_DEV,
273                 .dst_maxburst = uap->fifosize >> 1,
274         };
275         struct dma_chan *chan;
276         dma_cap_mask_t mask;
277
278         /* We need platform data */
279         if (!plat || !plat->dma_filter) {
280                 dev_info(uap->port.dev, "no DMA platform data\n");
281                 return;
282         }
283
284         /* Try to acquire a generic DMA engine slave TX channel */
285         dma_cap_zero(mask);
286         dma_cap_set(DMA_SLAVE, mask);
287
288         chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
289         if (!chan) {
290                 dev_err(uap->port.dev, "no TX DMA channel!\n");
291                 return;
292         }
293
294         dmaengine_slave_config(chan, &tx_conf);
295         uap->dmatx.chan = chan;
296
297         dev_info(uap->port.dev, "DMA channel TX %s\n",
298                  dma_chan_name(uap->dmatx.chan));
299
300         /* Optionally make use of an RX channel as well */
301         if (plat->dma_rx_param) {
302                 struct dma_slave_config rx_conf = {
303                         .src_addr = uap->port.mapbase + UART01x_DR,
304                         .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
305                         .direction = DMA_DEV_TO_MEM,
306                         .src_maxburst = uap->fifosize >> 1,
307                 };
308
309                 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
310                 if (!chan) {
311                         dev_err(uap->port.dev, "no RX DMA channel!\n");
312                         return;
313                 }
314
315                 dmaengine_slave_config(chan, &rx_conf);
316                 uap->dmarx.chan = chan;
317
318                 dev_info(uap->port.dev, "DMA channel RX %s\n",
319                          dma_chan_name(uap->dmarx.chan));
320         }
321 }
322
323 #ifndef MODULE
324 /*
325  * Stack up the UARTs and let the above initcall be done at device
326  * initcall time, because the serial driver is called as an arch
327  * initcall, and at this time the DMA subsystem is not yet registered.
328  * At this point the driver will switch over to using DMA where desired.
329  */
330 struct dma_uap {
331         struct list_head node;
332         struct uart_amba_port *uap;
333 };
334
335 static LIST_HEAD(pl011_dma_uarts);
336
337 static int __init pl011_dma_initcall(void)
338 {
339         struct list_head *node, *tmp;
340
341         list_for_each_safe(node, tmp, &pl011_dma_uarts) {
342                 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
343                 pl011_dma_probe_initcall(dmau->uap);
344                 list_del(node);
345                 kfree(dmau);
346         }
347         return 0;
348 }
349
350 device_initcall(pl011_dma_initcall);
351
352 static void pl011_dma_probe(struct uart_amba_port *uap)
353 {
354         struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
355         if (dmau) {
356                 dmau->uap = uap;
357                 list_add_tail(&dmau->node, &pl011_dma_uarts);
358         }
359 }
360 #else
361 static void pl011_dma_probe(struct uart_amba_port *uap)
362 {
363         pl011_dma_probe_initcall(uap);
364 }
365 #endif
366
367 static void pl011_dma_remove(struct uart_amba_port *uap)
368 {
369         /* TODO: remove the initcall if it has not yet executed */
370         if (uap->dmatx.chan)
371                 dma_release_channel(uap->dmatx.chan);
372         if (uap->dmarx.chan)
373                 dma_release_channel(uap->dmarx.chan);
374 }
375
376 /* Forward declare this for the refill routine */
377 static int pl011_dma_tx_refill(struct uart_amba_port *uap);
378
379 /*
380  * The current DMA TX buffer has been sent.
381  * Try to queue up another DMA buffer.
382  */
383 static void pl011_dma_tx_callback(void *data)
384 {
385         struct uart_amba_port *uap = data;
386         struct pl011_dmatx_data *dmatx = &uap->dmatx;
387         unsigned long flags;
388         u16 dmacr;
389
390         spin_lock_irqsave(&uap->port.lock, flags);
391         if (uap->dmatx.queued)
392                 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
393                              DMA_TO_DEVICE);
394
395         dmacr = uap->dmacr;
396         uap->dmacr = dmacr & ~UART011_TXDMAE;
397         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
398
399         /*
400          * If TX DMA was disabled, it means that we've stopped the DMA for
401          * some reason (eg, XOFF received, or we want to send an X-char.)
402          *
403          * Note: we need to be careful here of a potential race between DMA
404          * and the rest of the driver - if the driver disables TX DMA while
405          * a TX buffer completing, we must update the tx queued status to
406          * get further refills (hence we check dmacr).
407          */
408         if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
409             uart_circ_empty(&uap->port.state->xmit)) {
410                 uap->dmatx.queued = false;
411                 spin_unlock_irqrestore(&uap->port.lock, flags);
412                 return;
413         }
414
415         if (pl011_dma_tx_refill(uap) <= 0) {
416                 /*
417                  * We didn't queue a DMA buffer for some reason, but we
418                  * have data pending to be sent.  Re-enable the TX IRQ.
419                  */
420                 uap->im |= UART011_TXIM;
421                 writew(uap->im, uap->port.membase + UART011_IMSC);
422         }
423         spin_unlock_irqrestore(&uap->port.lock, flags);
424 }
425
426 /*
427  * Try to refill the TX DMA buffer.
428  * Locking: called with port lock held and IRQs disabled.
429  * Returns:
430  *   1 if we queued up a TX DMA buffer.
431  *   0 if we didn't want to handle this by DMA
432  *  <0 on error
433  */
434 static int pl011_dma_tx_refill(struct uart_amba_port *uap)
435 {
436         struct pl011_dmatx_data *dmatx = &uap->dmatx;
437         struct dma_chan *chan = dmatx->chan;
438         struct dma_device *dma_dev = chan->device;
439         struct dma_async_tx_descriptor *desc;
440         struct circ_buf *xmit = &uap->port.state->xmit;
441         unsigned int count;
442
443         /*
444          * Try to avoid the overhead involved in using DMA if the
445          * transaction fits in the first half of the FIFO, by using
446          * the standard interrupt handling.  This ensures that we
447          * issue a uart_write_wakeup() at the appropriate time.
448          */
449         count = uart_circ_chars_pending(xmit);
450         if (count < (uap->fifosize >> 1)) {
451                 uap->dmatx.queued = false;
452                 return 0;
453         }
454
455         /*
456          * Bodge: don't send the last character by DMA, as this
457          * will prevent XON from notifying us to restart DMA.
458          */
459         count -= 1;
460
461         /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
462         if (count > PL011_DMA_BUFFER_SIZE)
463                 count = PL011_DMA_BUFFER_SIZE;
464
465         if (xmit->tail < xmit->head)
466                 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
467         else {
468                 size_t first = UART_XMIT_SIZE - xmit->tail;
469                 size_t second = xmit->head;
470
471                 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
472                 if (second)
473                         memcpy(&dmatx->buf[first], &xmit->buf[0], second);
474         }
475
476         dmatx->sg.length = count;
477
478         if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
479                 uap->dmatx.queued = false;
480                 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
481                 return -EBUSY;
482         }
483
484         desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
485                                              DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
486         if (!desc) {
487                 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
488                 uap->dmatx.queued = false;
489                 /*
490                  * If DMA cannot be used right now, we complete this
491                  * transaction via IRQ and let the TTY layer retry.
492                  */
493                 dev_dbg(uap->port.dev, "TX DMA busy\n");
494                 return -EBUSY;
495         }
496
497         /* Some data to go along to the callback */
498         desc->callback = pl011_dma_tx_callback;
499         desc->callback_param = uap;
500
501         /* All errors should happen at prepare time */
502         dmaengine_submit(desc);
503
504         /* Fire the DMA transaction */
505         dma_dev->device_issue_pending(chan);
506
507         uap->dmacr |= UART011_TXDMAE;
508         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
509         uap->dmatx.queued = true;
510
511         /*
512          * Now we know that DMA will fire, so advance the ring buffer
513          * with the stuff we just dispatched.
514          */
515         xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
516         uap->port.icount.tx += count;
517
518         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
519                 uart_write_wakeup(&uap->port);
520
521         return 1;
522 }
523
524 /*
525  * We received a transmit interrupt without a pending X-char but with
526  * pending characters.
527  * Locking: called with port lock held and IRQs disabled.
528  * Returns:
529  *   false if we want to use PIO to transmit
530  *   true if we queued a DMA buffer
531  */
532 static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
533 {
534         if (!uap->using_tx_dma)
535                 return false;
536
537         /*
538          * If we already have a TX buffer queued, but received a
539          * TX interrupt, it will be because we've just sent an X-char.
540          * Ensure the TX DMA is enabled and the TX IRQ is disabled.
541          */
542         if (uap->dmatx.queued) {
543                 uap->dmacr |= UART011_TXDMAE;
544                 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
545                 uap->im &= ~UART011_TXIM;
546                 writew(uap->im, uap->port.membase + UART011_IMSC);
547                 return true;
548         }
549
550         /*
551          * We don't have a TX buffer queued, so try to queue one.
552          * If we successfully queued a buffer, mask the TX IRQ.
553          */
554         if (pl011_dma_tx_refill(uap) > 0) {
555                 uap->im &= ~UART011_TXIM;
556                 writew(uap->im, uap->port.membase + UART011_IMSC);
557                 return true;
558         }
559         return false;
560 }
561
562 /*
563  * Stop the DMA transmit (eg, due to received XOFF).
564  * Locking: called with port lock held and IRQs disabled.
565  */
566 static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
567 {
568         if (uap->dmatx.queued) {
569                 uap->dmacr &= ~UART011_TXDMAE;
570                 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
571         }
572 }
573
574 /*
575  * Try to start a DMA transmit, or in the case of an XON/OFF
576  * character queued for send, try to get that character out ASAP.
577  * Locking: called with port lock held and IRQs disabled.
578  * Returns:
579  *   false if we want the TX IRQ to be enabled
580  *   true if we have a buffer queued
581  */
582 static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
583 {
584         u16 dmacr;
585
586         if (!uap->using_tx_dma)
587                 return false;
588
589         if (!uap->port.x_char) {
590                 /* no X-char, try to push chars out in DMA mode */
591                 bool ret = true;
592
593                 if (!uap->dmatx.queued) {
594                         if (pl011_dma_tx_refill(uap) > 0) {
595                                 uap->im &= ~UART011_TXIM;
596                                 ret = true;
597                         } else {
598                                 uap->im |= UART011_TXIM;
599                                 ret = false;
600                         }
601                         writew(uap->im, uap->port.membase + UART011_IMSC);
602                 } else if (!(uap->dmacr & UART011_TXDMAE)) {
603                         uap->dmacr |= UART011_TXDMAE;
604                         writew(uap->dmacr,
605                                        uap->port.membase + UART011_DMACR);
606                 }
607                 return ret;
608         }
609
610         /*
611          * We have an X-char to send.  Disable DMA to prevent it loading
612          * the TX fifo, and then see if we can stuff it into the FIFO.
613          */
614         dmacr = uap->dmacr;
615         uap->dmacr &= ~UART011_TXDMAE;
616         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
617
618         if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
619                 /*
620                  * No space in the FIFO, so enable the transmit interrupt
621                  * so we know when there is space.  Note that once we've
622                  * loaded the character, we should just re-enable DMA.
623                  */
624                 return false;
625         }
626
627         writew(uap->port.x_char, uap->port.membase + UART01x_DR);
628         uap->port.icount.tx++;
629         uap->port.x_char = 0;
630
631         /* Success - restore the DMA state */
632         uap->dmacr = dmacr;
633         writew(dmacr, uap->port.membase + UART011_DMACR);
634
635         return true;
636 }
637
638 /*
639  * Flush the transmit buffer.
640  * Locking: called with port lock held and IRQs disabled.
641  */
642 static void pl011_dma_flush_buffer(struct uart_port *port)
643 {
644         struct uart_amba_port *uap = (struct uart_amba_port *)port;
645
646         if (!uap->using_tx_dma)
647                 return;
648
649         /* Avoid deadlock with the DMA engine callback */
650         spin_unlock(&uap->port.lock);
651         dmaengine_terminate_all(uap->dmatx.chan);
652         spin_lock(&uap->port.lock);
653         if (uap->dmatx.queued) {
654                 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
655                              DMA_TO_DEVICE);
656                 uap->dmatx.queued = false;
657                 uap->dmacr &= ~UART011_TXDMAE;
658                 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
659         }
660 }
661
662 static void pl011_dma_rx_callback(void *data);
663
664 static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
665 {
666         struct dma_chan *rxchan = uap->dmarx.chan;
667         struct dma_device *dma_dev;
668         struct pl011_dmarx_data *dmarx = &uap->dmarx;
669         struct dma_async_tx_descriptor *desc;
670         struct pl011_sgbuf *sgbuf;
671
672         if (!rxchan)
673                 return -EIO;
674
675         /* Start the RX DMA job */
676         sgbuf = uap->dmarx.use_buf_b ?
677                 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
678         dma_dev = rxchan->device;
679         desc = rxchan->device->device_prep_slave_sg(rxchan, &sgbuf->sg, 1,
680                                         DMA_DEV_TO_MEM,
681                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
682         /*
683          * If the DMA engine is busy and cannot prepare a
684          * channel, no big deal, the driver will fall back
685          * to interrupt mode as a result of this error code.
686          */
687         if (!desc) {
688                 uap->dmarx.running = false;
689                 dmaengine_terminate_all(rxchan);
690                 return -EBUSY;
691         }
692
693         /* Some data to go along to the callback */
694         desc->callback = pl011_dma_rx_callback;
695         desc->callback_param = uap;
696         dmarx->cookie = dmaengine_submit(desc);
697         dma_async_issue_pending(rxchan);
698
699         uap->dmacr |= UART011_RXDMAE;
700         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
701         uap->dmarx.running = true;
702
703         uap->im &= ~UART011_RXIM;
704         writew(uap->im, uap->port.membase + UART011_IMSC);
705
706         return 0;
707 }
708
709 /*
710  * This is called when either the DMA job is complete, or
711  * the FIFO timeout interrupt occurred. This must be called
712  * with the port spinlock uap->port.lock held.
713  */
714 static void pl011_dma_rx_chars(struct uart_amba_port *uap,
715                                u32 pending, bool use_buf_b,
716                                bool readfifo)
717 {
718         struct tty_struct *tty = uap->port.state->port.tty;
719         struct pl011_sgbuf *sgbuf = use_buf_b ?
720                 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
721         struct device *dev = uap->dmarx.chan->device->dev;
722         int dma_count = 0;
723         u32 fifotaken = 0; /* only used for vdbg() */
724
725         /* Pick everything from the DMA first */
726         if (pending) {
727                 /* Sync in buffer */
728                 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
729
730                 /*
731                  * First take all chars in the DMA pipe, then look in the FIFO.
732                  * Note that tty_insert_flip_buf() tries to take as many chars
733                  * as it can.
734                  */
735                 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
736                                                    sgbuf->buf, pending);
737
738                 /* Return buffer to device */
739                 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
740
741                 uap->port.icount.rx += dma_count;
742                 if (dma_count < pending)
743                         dev_warn(uap->port.dev,
744                                  "couldn't insert all characters (TTY is full?)\n");
745         }
746
747         /*
748          * Only continue with trying to read the FIFO if all DMA chars have
749          * been taken first.
750          */
751         if (dma_count == pending && readfifo) {
752                 /* Clear any error flags */
753                 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
754                        uap->port.membase + UART011_ICR);
755
756                 /*
757                  * If we read all the DMA'd characters, and we had an
758                  * incomplete buffer, that could be due to an rx error, or
759                  * maybe we just timed out. Read any pending chars and check
760                  * the error status.
761                  *
762                  * Error conditions will only occur in the FIFO, these will
763                  * trigger an immediate interrupt and stop the DMA job, so we
764                  * will always find the error in the FIFO, never in the DMA
765                  * buffer.
766                  */
767                 fifotaken = pl011_fifo_to_tty(uap);
768         }
769
770         spin_unlock(&uap->port.lock);
771         dev_vdbg(uap->port.dev,
772                  "Took %d chars from DMA buffer and %d chars from the FIFO\n",
773                  dma_count, fifotaken);
774         tty_flip_buffer_push(tty);
775         spin_lock(&uap->port.lock);
776 }
777
778 static void pl011_dma_rx_irq(struct uart_amba_port *uap)
779 {
780         struct pl011_dmarx_data *dmarx = &uap->dmarx;
781         struct dma_chan *rxchan = dmarx->chan;
782         struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
783                 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
784         size_t pending;
785         struct dma_tx_state state;
786         enum dma_status dmastat;
787
788         /*
789          * Pause the transfer so we can trust the current counter,
790          * do this before we pause the PL011 block, else we may
791          * overflow the FIFO.
792          */
793         if (dmaengine_pause(rxchan))
794                 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
795         dmastat = rxchan->device->device_tx_status(rxchan,
796                                                    dmarx->cookie, &state);
797         if (dmastat != DMA_PAUSED)
798                 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
799
800         /* Disable RX DMA - incoming data will wait in the FIFO */
801         uap->dmacr &= ~UART011_RXDMAE;
802         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
803         uap->dmarx.running = false;
804
805         pending = sgbuf->sg.length - state.residue;
806         BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
807         /* Then we terminate the transfer - we now know our residue */
808         dmaengine_terminate_all(rxchan);
809
810         /*
811          * This will take the chars we have so far and insert
812          * into the framework.
813          */
814         pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
815
816         /* Switch buffer & re-trigger DMA job */
817         dmarx->use_buf_b = !dmarx->use_buf_b;
818         if (pl011_dma_rx_trigger_dma(uap)) {
819                 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
820                         "fall back to interrupt mode\n");
821                 uap->im |= UART011_RXIM;
822                 writew(uap->im, uap->port.membase + UART011_IMSC);
823         }
824 }
825
826 static void pl011_dma_rx_callback(void *data)
827 {
828         struct uart_amba_port *uap = data;
829         struct pl011_dmarx_data *dmarx = &uap->dmarx;
830         struct dma_chan *rxchan = dmarx->chan;
831         bool lastbuf = dmarx->use_buf_b;
832         struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
833                 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
834         size_t pending;
835         struct dma_tx_state state;
836         int ret;
837
838         /*
839          * This completion interrupt occurs typically when the
840          * RX buffer is totally stuffed but no timeout has yet
841          * occurred. When that happens, we just want the RX
842          * routine to flush out the secondary DMA buffer while
843          * we immediately trigger the next DMA job.
844          */
845         spin_lock_irq(&uap->port.lock);
846         /*
847          * Rx data can be taken by the UART interrupts during
848          * the DMA irq handler. So we check the residue here.
849          */
850         rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
851         pending = sgbuf->sg.length - state.residue;
852         BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
853         /* Then we terminate the transfer - we now know our residue */
854         dmaengine_terminate_all(rxchan);
855
856         uap->dmarx.running = false;
857         dmarx->use_buf_b = !lastbuf;
858         ret = pl011_dma_rx_trigger_dma(uap);
859
860         pl011_dma_rx_chars(uap, pending, lastbuf, false);
861         spin_unlock_irq(&uap->port.lock);
862         /*
863          * Do this check after we picked the DMA chars so we don't
864          * get some IRQ immediately from RX.
865          */
866         if (ret) {
867                 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
868                         "fall back to interrupt mode\n");
869                 uap->im |= UART011_RXIM;
870                 writew(uap->im, uap->port.membase + UART011_IMSC);
871         }
872 }
873
874 /*
875  * Stop accepting received characters, when we're shutting down or
876  * suspending this port.
877  * Locking: called with port lock held and IRQs disabled.
878  */
879 static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
880 {
881         /* FIXME.  Just disable the DMA enable */
882         uap->dmacr &= ~UART011_RXDMAE;
883         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
884 }
885
886 static void pl011_dma_startup(struct uart_amba_port *uap)
887 {
888         int ret;
889
890         if (!uap->dmatx.chan)
891                 return;
892
893         uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
894         if (!uap->dmatx.buf) {
895                 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
896                 uap->port.fifosize = uap->fifosize;
897                 return;
898         }
899
900         sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
901
902         /* The DMA buffer is now the FIFO the TTY subsystem can use */
903         uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
904         uap->using_tx_dma = true;
905
906         if (!uap->dmarx.chan)
907                 goto skip_rx;
908
909         /* Allocate and map DMA RX buffers */
910         ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
911                                DMA_FROM_DEVICE);
912         if (ret) {
913                 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
914                         "RX buffer A", ret);
915                 goto skip_rx;
916         }
917
918         ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
919                                DMA_FROM_DEVICE);
920         if (ret) {
921                 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
922                         "RX buffer B", ret);
923                 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
924                                  DMA_FROM_DEVICE);
925                 goto skip_rx;
926         }
927
928         uap->using_rx_dma = true;
929
930 skip_rx:
931         /* Turn on DMA error (RX/TX will be enabled on demand) */
932         uap->dmacr |= UART011_DMAONERR;
933         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
934
935         /*
936          * ST Micro variants has some specific dma burst threshold
937          * compensation. Set this to 16 bytes, so burst will only
938          * be issued above/below 16 bytes.
939          */
940         if (uap->vendor->dma_threshold)
941                 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
942                                uap->port.membase + ST_UART011_DMAWM);
943
944         if (uap->using_rx_dma) {
945                 if (pl011_dma_rx_trigger_dma(uap))
946                         dev_dbg(uap->port.dev, "could not trigger initial "
947                                 "RX DMA job, fall back to interrupt mode\n");
948         }
949 }
950
951 static void pl011_dma_shutdown(struct uart_amba_port *uap)
952 {
953         if (!(uap->using_tx_dma || uap->using_rx_dma))
954                 return;
955
956         /* Disable RX and TX DMA */
957         while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
958                 barrier();
959
960         spin_lock_irq(&uap->port.lock);
961         uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
962         writew(uap->dmacr, uap->port.membase + UART011_DMACR);
963         spin_unlock_irq(&uap->port.lock);
964
965         if (uap->using_tx_dma) {
966                 /* In theory, this should already be done by pl011_dma_flush_buffer */
967                 dmaengine_terminate_all(uap->dmatx.chan);
968                 if (uap->dmatx.queued) {
969                         dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
970                                      DMA_TO_DEVICE);
971                         uap->dmatx.queued = false;
972                 }
973
974                 kfree(uap->dmatx.buf);
975                 uap->using_tx_dma = false;
976         }
977
978         if (uap->using_rx_dma) {
979                 dmaengine_terminate_all(uap->dmarx.chan);
980                 /* Clean up the RX DMA */
981                 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
982                 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
983                 uap->using_rx_dma = false;
984         }
985 }
986
987 static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
988 {
989         return uap->using_rx_dma;
990 }
991
992 static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
993 {
994         return uap->using_rx_dma && uap->dmarx.running;
995 }
996
997
998 #else
999 /* Blank functions if the DMA engine is not available */
1000 static inline void pl011_dma_probe(struct uart_amba_port *uap)
1001 {
1002 }
1003
1004 static inline void pl011_dma_remove(struct uart_amba_port *uap)
1005 {
1006 }
1007
1008 static inline void pl011_dma_startup(struct uart_amba_port *uap)
1009 {
1010 }
1011
1012 static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1013 {
1014 }
1015
1016 static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1017 {
1018         return false;
1019 }
1020
1021 static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1022 {
1023 }
1024
1025 static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1026 {
1027         return false;
1028 }
1029
1030 static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1031 {
1032 }
1033
1034 static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1035 {
1036 }
1037
1038 static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1039 {
1040         return -EIO;
1041 }
1042
1043 static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1044 {
1045         return false;
1046 }
1047
1048 static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1049 {
1050         return false;
1051 }
1052
1053 #define pl011_dma_flush_buffer  NULL
1054 #endif
1055
1056
1057 /*
1058  * pl011_lockup_wa
1059  * This workaround aims to break the deadlock situation
1060  * when after long transfer over uart in hardware flow
1061  * control, uart interrupt registers cannot be cleared.
1062  * Hence uart transfer gets blocked.
1063  *
1064  * It is seen that during such deadlock condition ICR
1065  * don't get cleared even on multiple write. This leads
1066  * pass_counter to decrease and finally reach zero. This
1067  * can be taken as trigger point to run this UART_BT_WA.
1068  *
1069  */
1070 static void pl011_lockup_wa(unsigned long data)
1071 {
1072         struct uart_amba_port *uap = amba_ports[0];
1073         void __iomem *base = uap->port.membase;
1074         struct circ_buf *xmit = &uap->port.state->xmit;
1075         struct tty_struct *tty = uap->port.state->port.tty;
1076         int buf_empty_retries = 200;
1077         int loop;
1078
1079         /* Stop HCI layer from submitting data for tx */
1080         tty->hw_stopped = 1;
1081         while (!uart_circ_empty(xmit)) {
1082                 if (buf_empty_retries-- == 0)
1083                         break;
1084                 udelay(100);
1085         }
1086
1087         /* Backup registers */
1088         for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1089                 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1090
1091         /* Disable UART so that FIFO data is flushed out */
1092         writew(0x00, uap->port.membase + UART011_CR);
1093
1094         /* Soft reset UART module */
1095         if (uap->port.dev->platform_data) {
1096                 struct amba_pl011_data *plat;
1097
1098                 plat = uap->port.dev->platform_data;
1099                 if (plat->reset)
1100                         plat->reset();
1101         }
1102
1103         /* Restore registers */
1104         for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1105                 writew(uart_wa_regdata[loop] ,
1106                                 uap->port.membase + uart_wa_reg[loop]);
1107
1108         /* Initialise the old status of the modem signals */
1109         uap->old_status = readw(uap->port.membase + UART01x_FR) &
1110                 UART01x_FR_MODEM_ANY;
1111
1112         if (readl(base + UART011_MIS) & 0x2)
1113                 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1114
1115         /* Start Tx/Rx */
1116         tty->hw_stopped = 0;
1117 }
1118
1119 static void pl011_stop_tx(struct uart_port *port)
1120 {
1121         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1122
1123         uap->im &= ~UART011_TXIM;
1124         writew(uap->im, uap->port.membase + UART011_IMSC);
1125         pl011_dma_tx_stop(uap);
1126 }
1127
1128 static void pl011_start_tx(struct uart_port *port)
1129 {
1130         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1131
1132         if (!pl011_dma_tx_start(uap)) {
1133                 uap->im |= UART011_TXIM;
1134                 writew(uap->im, uap->port.membase + UART011_IMSC);
1135         }
1136 }
1137
1138 static void pl011_stop_rx(struct uart_port *port)
1139 {
1140         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1141
1142         uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1143                      UART011_PEIM|UART011_BEIM|UART011_OEIM);
1144         writew(uap->im, uap->port.membase + UART011_IMSC);
1145
1146         pl011_dma_rx_stop(uap);
1147 }
1148
1149 static void pl011_enable_ms(struct uart_port *port)
1150 {
1151         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1152
1153         uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1154         writew(uap->im, uap->port.membase + UART011_IMSC);
1155 }
1156
1157 static void pl011_rx_chars(struct uart_amba_port *uap)
1158 {
1159         struct tty_struct *tty = uap->port.state->port.tty;
1160
1161         pl011_fifo_to_tty(uap);
1162
1163         spin_unlock(&uap->port.lock);
1164         tty_flip_buffer_push(tty);
1165         /*
1166          * If we were temporarily out of DMA mode for a while,
1167          * attempt to switch back to DMA mode again.
1168          */
1169         if (pl011_dma_rx_available(uap)) {
1170                 if (pl011_dma_rx_trigger_dma(uap)) {
1171                         dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1172                                 "fall back to interrupt mode again\n");
1173                         uap->im |= UART011_RXIM;
1174                 } else
1175                         uap->im &= ~UART011_RXIM;
1176                 writew(uap->im, uap->port.membase + UART011_IMSC);
1177         }
1178         spin_lock(&uap->port.lock);
1179 }
1180
1181 static void pl011_tx_chars(struct uart_amba_port *uap)
1182 {
1183         struct circ_buf *xmit = &uap->port.state->xmit;
1184         int count;
1185
1186         if (uap->port.x_char) {
1187                 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1188                 uap->port.icount.tx++;
1189                 uap->port.x_char = 0;
1190                 return;
1191         }
1192         if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
1193                 pl011_stop_tx(&uap->port);
1194                 return;
1195         }
1196
1197         /* If we are using DMA mode, try to send some characters. */
1198         if (pl011_dma_tx_irq(uap))
1199                 return;
1200
1201         count = uap->fifosize >> 1;
1202         do {
1203                 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1204                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1205                 uap->port.icount.tx++;
1206                 if (uart_circ_empty(xmit))
1207                         break;
1208         } while (--count > 0);
1209
1210         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1211                 uart_write_wakeup(&uap->port);
1212
1213         if (uart_circ_empty(xmit))
1214                 pl011_stop_tx(&uap->port);
1215 }
1216
1217 static void pl011_modem_status(struct uart_amba_port *uap)
1218 {
1219         unsigned int status, delta;
1220
1221         status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1222
1223         delta = status ^ uap->old_status;
1224         uap->old_status = status;
1225
1226         if (!delta)
1227                 return;
1228
1229         if (delta & UART01x_FR_DCD)
1230                 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1231
1232         if (delta & UART01x_FR_DSR)
1233                 uap->port.icount.dsr++;
1234
1235         if (delta & UART01x_FR_CTS)
1236                 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1237
1238         wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
1239 }
1240
1241 static irqreturn_t pl011_int(int irq, void *dev_id)
1242 {
1243         struct uart_amba_port *uap = dev_id;
1244         unsigned long flags;
1245         unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1246         int handled = 0;
1247
1248         spin_lock_irqsave(&uap->port.lock, flags);
1249
1250         status = readw(uap->port.membase + UART011_MIS);
1251         if (status) {
1252                 do {
1253                         writew(status & ~(UART011_TXIS|UART011_RTIS|
1254                                           UART011_RXIS),
1255                                uap->port.membase + UART011_ICR);
1256
1257                         if (status & (UART011_RTIS|UART011_RXIS)) {
1258                                 if (pl011_dma_rx_running(uap))
1259                                         pl011_dma_rx_irq(uap);
1260                                 else
1261                                         pl011_rx_chars(uap);
1262                         }
1263                         if (status & (UART011_DSRMIS|UART011_DCDMIS|
1264                                       UART011_CTSMIS|UART011_RIMIS))
1265                                 pl011_modem_status(uap);
1266                         if (status & UART011_TXIS)
1267                                 pl011_tx_chars(uap);
1268
1269                         if (pass_counter-- == 0) {
1270                                 if (uap->interrupt_may_hang)
1271                                         tasklet_schedule(&pl011_lockup_tlet);
1272                                 break;
1273                         }
1274
1275                         status = readw(uap->port.membase + UART011_MIS);
1276                 } while (status != 0);
1277                 handled = 1;
1278         }
1279
1280         spin_unlock_irqrestore(&uap->port.lock, flags);
1281
1282         return IRQ_RETVAL(handled);
1283 }
1284
1285 static unsigned int pl01x_tx_empty(struct uart_port *port)
1286 {
1287         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1288         unsigned int status = readw(uap->port.membase + UART01x_FR);
1289         return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1290 }
1291
1292 static unsigned int pl01x_get_mctrl(struct uart_port *port)
1293 {
1294         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1295         unsigned int result = 0;
1296         unsigned int status = readw(uap->port.membase + UART01x_FR);
1297
1298 #define TIOCMBIT(uartbit, tiocmbit)     \
1299         if (status & uartbit)           \
1300                 result |= tiocmbit
1301
1302         TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1303         TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1304         TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1305         TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1306 #undef TIOCMBIT
1307         return result;
1308 }
1309
1310 static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1311 {
1312         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1313         unsigned int cr;
1314
1315         cr = readw(uap->port.membase + UART011_CR);
1316
1317 #define TIOCMBIT(tiocmbit, uartbit)             \
1318         if (mctrl & tiocmbit)           \
1319                 cr |= uartbit;          \
1320         else                            \
1321                 cr &= ~uartbit
1322
1323         TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1324         TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1325         TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1326         TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1327         TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
1328
1329         if (uap->autorts) {
1330                 /* We need to disable auto-RTS if we want to turn RTS off */
1331                 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1332         }
1333 #undef TIOCMBIT
1334
1335         writew(cr, uap->port.membase + UART011_CR);
1336 }
1337
1338 static void pl011_break_ctl(struct uart_port *port, int break_state)
1339 {
1340         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1341         unsigned long flags;
1342         unsigned int lcr_h;
1343
1344         spin_lock_irqsave(&uap->port.lock, flags);
1345         lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1346         if (break_state == -1)
1347                 lcr_h |= UART01x_LCRH_BRK;
1348         else
1349                 lcr_h &= ~UART01x_LCRH_BRK;
1350         writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1351         spin_unlock_irqrestore(&uap->port.lock, flags);
1352 }
1353
1354 #ifdef CONFIG_CONSOLE_POLL
1355 static int pl010_get_poll_char(struct uart_port *port)
1356 {
1357         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1358         unsigned int status;
1359
1360         status = readw(uap->port.membase + UART01x_FR);
1361         if (status & UART01x_FR_RXFE)
1362                 return NO_POLL_CHAR;
1363
1364         return readw(uap->port.membase + UART01x_DR);
1365 }
1366
1367 static void pl010_put_poll_char(struct uart_port *port,
1368                          unsigned char ch)
1369 {
1370         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1371
1372         while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1373                 barrier();
1374
1375         writew(ch, uap->port.membase + UART01x_DR);
1376 }
1377
1378 #endif /* CONFIG_CONSOLE_POLL */
1379
1380 static int pl011_startup(struct uart_port *port)
1381 {
1382         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1383         unsigned int cr;
1384         int retval;
1385
1386         retval = clk_prepare(uap->clk);
1387         if (retval)
1388                 goto out;
1389
1390         /*
1391          * Try to enable the clock producer.
1392          */
1393         retval = clk_enable(uap->clk);
1394         if (retval)
1395                 goto clk_unprep;
1396
1397         uap->port.uartclk = clk_get_rate(uap->clk);
1398
1399         /* Clear pending error and receive interrupts */
1400         writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1401                UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1402
1403         /*
1404          * Allocate the IRQ
1405          */
1406         retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1407         if (retval)
1408                 goto clk_dis;
1409
1410         writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
1411
1412         /*
1413          * Provoke TX FIFO interrupt into asserting.
1414          */
1415         cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1416         writew(cr, uap->port.membase + UART011_CR);
1417         writew(0, uap->port.membase + UART011_FBRD);
1418         writew(1, uap->port.membase + UART011_IBRD);
1419         writew(0, uap->port.membase + uap->lcrh_rx);
1420         if (uap->lcrh_tx != uap->lcrh_rx) {
1421                 int i;
1422                 /*
1423                  * Wait 10 PCLKs before writing LCRH_TX register,
1424                  * to get this delay write read only register 10 times
1425                  */
1426                 for (i = 0; i < 10; ++i)
1427                         writew(0xff, uap->port.membase + UART011_MIS);
1428                 writew(0, uap->port.membase + uap->lcrh_tx);
1429         }
1430         writew(0, uap->port.membase + UART01x_DR);
1431         while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1432                 barrier();
1433
1434         /* restore RTS and DTR */
1435         cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1436         cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
1437         writew(cr, uap->port.membase + UART011_CR);
1438
1439         /*
1440          * initialise the old status of the modem signals
1441          */
1442         uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1443
1444         /* Startup DMA */
1445         pl011_dma_startup(uap);
1446
1447         /*
1448          * Finally, enable interrupts, only timeouts when using DMA
1449          * if initial RX DMA job failed, start in interrupt mode
1450          * as well.
1451          */
1452         spin_lock_irq(&uap->port.lock);
1453         /* Clear out any spuriously appearing RX interrupts */
1454          writew(UART011_RTIS | UART011_RXIS,
1455                 uap->port.membase + UART011_ICR);
1456         uap->im = UART011_RTIM;
1457         if (!pl011_dma_rx_running(uap))
1458                 uap->im |= UART011_RXIM;
1459         writew(uap->im, uap->port.membase + UART011_IMSC);
1460         spin_unlock_irq(&uap->port.lock);
1461
1462         if (uap->port.dev->platform_data) {
1463                 struct amba_pl011_data *plat;
1464
1465                 plat = uap->port.dev->platform_data;
1466                 if (plat->init)
1467                         plat->init();
1468         }
1469
1470         return 0;
1471
1472  clk_dis:
1473         clk_disable(uap->clk);
1474  clk_unprep:
1475         clk_unprepare(uap->clk);
1476  out:
1477         return retval;
1478 }
1479
1480 static void pl011_shutdown_channel(struct uart_amba_port *uap,
1481                                         unsigned int lcrh)
1482 {
1483       unsigned long val;
1484
1485       val = readw(uap->port.membase + lcrh);
1486       val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1487       writew(val, uap->port.membase + lcrh);
1488 }
1489
1490 static void pl011_shutdown(struct uart_port *port)
1491 {
1492         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1493         unsigned int cr;
1494
1495         /*
1496          * disable all interrupts
1497          */
1498         spin_lock_irq(&uap->port.lock);
1499         uap->im = 0;
1500         writew(uap->im, uap->port.membase + UART011_IMSC);
1501         writew(0xffff, uap->port.membase + UART011_ICR);
1502         spin_unlock_irq(&uap->port.lock);
1503
1504         pl011_dma_shutdown(uap);
1505
1506         /*
1507          * Free the interrupt
1508          */
1509         free_irq(uap->port.irq, uap);
1510
1511         /*
1512          * disable the port
1513          * disable the port. It should not disable RTS and DTR.
1514          * Also RTS and DTR state should be preserved to restore
1515          * it during startup().
1516          */
1517         uap->autorts = false;
1518         cr = readw(uap->port.membase + UART011_CR);
1519         uap->old_cr = cr;
1520         cr &= UART011_CR_RTS | UART011_CR_DTR;
1521         cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1522         writew(cr, uap->port.membase + UART011_CR);
1523
1524         /*
1525          * disable break condition and fifos
1526          */
1527         pl011_shutdown_channel(uap, uap->lcrh_rx);
1528         if (uap->lcrh_rx != uap->lcrh_tx)
1529                 pl011_shutdown_channel(uap, uap->lcrh_tx);
1530
1531         /*
1532          * Shut down the clock producer
1533          */
1534         clk_disable(uap->clk);
1535         clk_unprepare(uap->clk);
1536
1537         if (uap->port.dev->platform_data) {
1538                 struct amba_pl011_data *plat;
1539
1540                 plat = uap->port.dev->platform_data;
1541                 if (plat->exit)
1542                         plat->exit();
1543         }
1544
1545 }
1546
1547 static void
1548 pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1549                      struct ktermios *old)
1550 {
1551         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1552         unsigned int lcr_h, old_cr;
1553         unsigned long flags;
1554         unsigned int baud, quot, clkdiv;
1555
1556         if (uap->vendor->oversampling)
1557                 clkdiv = 8;
1558         else
1559                 clkdiv = 16;
1560
1561         /*
1562          * Ask the core to calculate the divisor for us.
1563          */
1564         baud = uart_get_baud_rate(port, termios, old, 0,
1565                                   port->uartclk / clkdiv);
1566
1567         if (baud > port->uartclk/16)
1568                 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1569         else
1570                 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
1571
1572         switch (termios->c_cflag & CSIZE) {
1573         case CS5:
1574                 lcr_h = UART01x_LCRH_WLEN_5;
1575                 break;
1576         case CS6:
1577                 lcr_h = UART01x_LCRH_WLEN_6;
1578                 break;
1579         case CS7:
1580                 lcr_h = UART01x_LCRH_WLEN_7;
1581                 break;
1582         default: // CS8
1583                 lcr_h = UART01x_LCRH_WLEN_8;
1584                 break;
1585         }
1586         if (termios->c_cflag & CSTOPB)
1587                 lcr_h |= UART01x_LCRH_STP2;
1588         if (termios->c_cflag & PARENB) {
1589                 lcr_h |= UART01x_LCRH_PEN;
1590                 if (!(termios->c_cflag & PARODD))
1591                         lcr_h |= UART01x_LCRH_EPS;
1592         }
1593         if (uap->fifosize > 1)
1594                 lcr_h |= UART01x_LCRH_FEN;
1595
1596         spin_lock_irqsave(&port->lock, flags);
1597
1598         /*
1599          * Update the per-port timeout.
1600          */
1601         uart_update_timeout(port, termios->c_cflag, baud);
1602
1603         port->read_status_mask = UART011_DR_OE | 255;
1604         if (termios->c_iflag & INPCK)
1605                 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1606         if (termios->c_iflag & (BRKINT | PARMRK))
1607                 port->read_status_mask |= UART011_DR_BE;
1608
1609         /*
1610          * Characters to ignore
1611          */
1612         port->ignore_status_mask = 0;
1613         if (termios->c_iflag & IGNPAR)
1614                 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1615         if (termios->c_iflag & IGNBRK) {
1616                 port->ignore_status_mask |= UART011_DR_BE;
1617                 /*
1618                  * If we're ignoring parity and break indicators,
1619                  * ignore overruns too (for real raw support).
1620                  */
1621                 if (termios->c_iflag & IGNPAR)
1622                         port->ignore_status_mask |= UART011_DR_OE;
1623         }
1624
1625         /*
1626          * Ignore all characters if CREAD is not set.
1627          */
1628         if ((termios->c_cflag & CREAD) == 0)
1629                 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1630
1631         if (UART_ENABLE_MS(port, termios->c_cflag))
1632                 pl011_enable_ms(port);
1633
1634         /* first, disable everything */
1635         old_cr = readw(port->membase + UART011_CR);
1636         writew(0, port->membase + UART011_CR);
1637
1638         if (termios->c_cflag & CRTSCTS) {
1639                 if (old_cr & UART011_CR_RTS)
1640                         old_cr |= UART011_CR_RTSEN;
1641
1642                 old_cr |= UART011_CR_CTSEN;
1643                 uap->autorts = true;
1644         } else {
1645                 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1646                 uap->autorts = false;
1647         }
1648
1649         if (uap->vendor->oversampling) {
1650                 if (baud > port->uartclk / 16)
1651                         old_cr |= ST_UART011_CR_OVSFACT;
1652                 else
1653                         old_cr &= ~ST_UART011_CR_OVSFACT;
1654         }
1655
1656         /* Set baud rate */
1657         writew(quot & 0x3f, port->membase + UART011_FBRD);
1658         writew(quot >> 6, port->membase + UART011_IBRD);
1659
1660         /*
1661          * ----------v----------v----------v----------v-----
1662          * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1663          * ----------^----------^----------^----------^-----
1664          */
1665         writew(lcr_h, port->membase + uap->lcrh_rx);
1666         if (uap->lcrh_rx != uap->lcrh_tx) {
1667                 int i;
1668                 /*
1669                  * Wait 10 PCLKs before writing LCRH_TX register,
1670                  * to get this delay write read only register 10 times
1671                  */
1672                 for (i = 0; i < 10; ++i)
1673                         writew(0xff, uap->port.membase + UART011_MIS);
1674                 writew(lcr_h, port->membase + uap->lcrh_tx);
1675         }
1676         writew(old_cr, port->membase + UART011_CR);
1677
1678         spin_unlock_irqrestore(&port->lock, flags);
1679 }
1680
1681 static const char *pl011_type(struct uart_port *port)
1682 {
1683         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1684         return uap->port.type == PORT_AMBA ? uap->type : NULL;
1685 }
1686
1687 /*
1688  * Release the memory region(s) being used by 'port'
1689  */
1690 static void pl010_release_port(struct uart_port *port)
1691 {
1692         release_mem_region(port->mapbase, SZ_4K);
1693 }
1694
1695 /*
1696  * Request the memory region(s) being used by 'port'
1697  */
1698 static int pl010_request_port(struct uart_port *port)
1699 {
1700         return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1701                         != NULL ? 0 : -EBUSY;
1702 }
1703
1704 /*
1705  * Configure/autoconfigure the port.
1706  */
1707 static void pl010_config_port(struct uart_port *port, int flags)
1708 {
1709         if (flags & UART_CONFIG_TYPE) {
1710                 port->type = PORT_AMBA;
1711                 pl010_request_port(port);
1712         }
1713 }
1714
1715 /*
1716  * verify the new serial_struct (for TIOCSSERIAL).
1717  */
1718 static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1719 {
1720         int ret = 0;
1721         if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1722                 ret = -EINVAL;
1723         if (ser->irq < 0 || ser->irq >= nr_irqs)
1724                 ret = -EINVAL;
1725         if (ser->baud_base < 9600)
1726                 ret = -EINVAL;
1727         return ret;
1728 }
1729
1730 static struct uart_ops amba_pl011_pops = {
1731         .tx_empty       = pl01x_tx_empty,
1732         .set_mctrl      = pl011_set_mctrl,
1733         .get_mctrl      = pl01x_get_mctrl,
1734         .stop_tx        = pl011_stop_tx,
1735         .start_tx       = pl011_start_tx,
1736         .stop_rx        = pl011_stop_rx,
1737         .enable_ms      = pl011_enable_ms,
1738         .break_ctl      = pl011_break_ctl,
1739         .startup        = pl011_startup,
1740         .shutdown       = pl011_shutdown,
1741         .flush_buffer   = pl011_dma_flush_buffer,
1742         .set_termios    = pl011_set_termios,
1743         .type           = pl011_type,
1744         .release_port   = pl010_release_port,
1745         .request_port   = pl010_request_port,
1746         .config_port    = pl010_config_port,
1747         .verify_port    = pl010_verify_port,
1748 #ifdef CONFIG_CONSOLE_POLL
1749         .poll_get_char = pl010_get_poll_char,
1750         .poll_put_char = pl010_put_poll_char,
1751 #endif
1752 };
1753
1754 static struct uart_amba_port *amba_ports[UART_NR];
1755
1756 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1757
1758 static void pl011_console_putchar(struct uart_port *port, int ch)
1759 {
1760         struct uart_amba_port *uap = (struct uart_amba_port *)port;
1761
1762         while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1763                 barrier();
1764         writew(ch, uap->port.membase + UART01x_DR);
1765 }
1766
1767 static void
1768 pl011_console_write(struct console *co, const char *s, unsigned int count)
1769 {
1770         struct uart_amba_port *uap = amba_ports[co->index];
1771         unsigned int status, old_cr, new_cr;
1772         unsigned long flags;
1773         int locked = 1;
1774
1775         clk_enable(uap->clk);
1776
1777         local_irq_save(flags);
1778         if (uap->port.sysrq)
1779                 locked = 0;
1780         else if (oops_in_progress)
1781                 locked = spin_trylock(&uap->port.lock);
1782         else
1783                 spin_lock(&uap->port.lock);
1784
1785         /*
1786          *      First save the CR then disable the interrupts
1787          */
1788         old_cr = readw(uap->port.membase + UART011_CR);
1789         new_cr = old_cr & ~UART011_CR_CTSEN;
1790         new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1791         writew(new_cr, uap->port.membase + UART011_CR);
1792
1793         uart_console_write(&uap->port, s, count, pl011_console_putchar);
1794
1795         /*
1796          *      Finally, wait for transmitter to become empty
1797          *      and restore the TCR
1798          */
1799         do {
1800                 status = readw(uap->port.membase + UART01x_FR);
1801         } while (status & UART01x_FR_BUSY);
1802         writew(old_cr, uap->port.membase + UART011_CR);
1803
1804         if (locked)
1805                 spin_unlock(&uap->port.lock);
1806         local_irq_restore(flags);
1807
1808         clk_disable(uap->clk);
1809 }
1810
1811 static void __init
1812 pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1813                              int *parity, int *bits)
1814 {
1815         if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1816                 unsigned int lcr_h, ibrd, fbrd;
1817
1818                 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1819
1820                 *parity = 'n';
1821                 if (lcr_h & UART01x_LCRH_PEN) {
1822                         if (lcr_h & UART01x_LCRH_EPS)
1823                                 *parity = 'e';
1824                         else
1825                                 *parity = 'o';
1826                 }
1827
1828                 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1829                         *bits = 7;
1830                 else
1831                         *bits = 8;
1832
1833                 ibrd = readw(uap->port.membase + UART011_IBRD);
1834                 fbrd = readw(uap->port.membase + UART011_FBRD);
1835
1836                 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
1837
1838                 if (uap->vendor->oversampling) {
1839                         if (readw(uap->port.membase + UART011_CR)
1840                                   & ST_UART011_CR_OVSFACT)
1841                                 *baud *= 2;
1842                 }
1843         }
1844 }
1845
1846 static int __init pl011_console_setup(struct console *co, char *options)
1847 {
1848         struct uart_amba_port *uap;
1849         int baud = 38400;
1850         int bits = 8;
1851         int parity = 'n';
1852         int flow = 'n';
1853         int ret;
1854
1855         /*
1856          * Check whether an invalid uart number has been specified, and
1857          * if so, search for the first available port that does have
1858          * console support.
1859          */
1860         if (co->index >= UART_NR)
1861                 co->index = 0;
1862         uap = amba_ports[co->index];
1863         if (!uap)
1864                 return -ENODEV;
1865
1866         ret = clk_prepare(uap->clk);
1867         if (ret)
1868                 return ret;
1869
1870         if (uap->port.dev->platform_data) {
1871                 struct amba_pl011_data *plat;
1872
1873                 plat = uap->port.dev->platform_data;
1874                 if (plat->init)
1875                         plat->init();
1876         }
1877
1878         uap->port.uartclk = clk_get_rate(uap->clk);
1879
1880         if (options)
1881                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1882         else
1883                 pl011_console_get_options(uap, &baud, &parity, &bits);
1884
1885         return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1886 }
1887
1888 static struct uart_driver amba_reg;
1889 static struct console amba_console = {
1890         .name           = "ttyAMA",
1891         .write          = pl011_console_write,
1892         .device         = uart_console_device,
1893         .setup          = pl011_console_setup,
1894         .flags          = CON_PRINTBUFFER,
1895         .index          = -1,
1896         .data           = &amba_reg,
1897 };
1898
1899 #define AMBA_CONSOLE    (&amba_console)
1900 #else
1901 #define AMBA_CONSOLE    NULL
1902 #endif
1903
1904 static struct uart_driver amba_reg = {
1905         .owner                  = THIS_MODULE,
1906         .driver_name            = "ttyAMA",
1907         .dev_name               = "ttyAMA",
1908         .major                  = SERIAL_AMBA_MAJOR,
1909         .minor                  = SERIAL_AMBA_MINOR,
1910         .nr                     = UART_NR,
1911         .cons                   = AMBA_CONSOLE,
1912 };
1913
1914 static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
1915 {
1916         struct uart_amba_port *uap;
1917         struct vendor_data *vendor = id->data;
1918         void __iomem *base;
1919         int i, ret;
1920
1921         for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1922                 if (amba_ports[i] == NULL)
1923                         break;
1924
1925         if (i == ARRAY_SIZE(amba_ports)) {
1926                 ret = -EBUSY;
1927                 goto out;
1928         }
1929
1930         uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
1931         if (uap == NULL) {
1932                 ret = -ENOMEM;
1933                 goto out;
1934         }
1935
1936         base = ioremap(dev->res.start, resource_size(&dev->res));
1937         if (!base) {
1938                 ret = -ENOMEM;
1939                 goto free;
1940         }
1941
1942         uap->clk = clk_get(&dev->dev, NULL);
1943         if (IS_ERR(uap->clk)) {
1944                 ret = PTR_ERR(uap->clk);
1945                 goto unmap;
1946         }
1947
1948         /* Ensure interrupts from this UART are masked and cleared */
1949         writew(0, uap->port.membase + UART011_IMSC);
1950         writew(0xffff, uap->port.membase + UART011_ICR);
1951
1952         uap->vendor = vendor;
1953         uap->lcrh_rx = vendor->lcrh_rx;
1954         uap->lcrh_tx = vendor->lcrh_tx;
1955         uap->old_cr = 0;
1956         uap->fifosize = vendor->fifosize;
1957         uap->interrupt_may_hang = vendor->interrupt_may_hang;
1958         uap->port.dev = &dev->dev;
1959         uap->port.mapbase = dev->res.start;
1960         uap->port.membase = base;
1961         uap->port.iotype = UPIO_MEM;
1962         uap->port.irq = dev->irq[0];
1963         uap->port.fifosize = uap->fifosize;
1964         uap->port.ops = &amba_pl011_pops;
1965         uap->port.flags = UPF_BOOT_AUTOCONF;
1966         uap->port.line = i;
1967         pl011_dma_probe(uap);
1968
1969         snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1970
1971         amba_ports[i] = uap;
1972
1973         amba_set_drvdata(dev, uap);
1974         ret = uart_add_one_port(&amba_reg, &uap->port);
1975         if (ret) {
1976                 amba_set_drvdata(dev, NULL);
1977                 amba_ports[i] = NULL;
1978                 pl011_dma_remove(uap);
1979                 clk_put(uap->clk);
1980  unmap:
1981                 iounmap(base);
1982  free:
1983                 kfree(uap);
1984         }
1985  out:
1986         return ret;
1987 }
1988
1989 static int pl011_remove(struct amba_device *dev)
1990 {
1991         struct uart_amba_port *uap = amba_get_drvdata(dev);
1992         int i;
1993
1994         amba_set_drvdata(dev, NULL);
1995
1996         uart_remove_one_port(&amba_reg, &uap->port);
1997
1998         for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1999                 if (amba_ports[i] == uap)
2000                         amba_ports[i] = NULL;
2001
2002         pl011_dma_remove(uap);
2003         iounmap(uap->port.membase);
2004         clk_put(uap->clk);
2005         kfree(uap);
2006         return 0;
2007 }
2008
2009 #ifdef CONFIG_PM
2010 static int pl011_suspend(struct amba_device *dev, pm_message_t state)
2011 {
2012         struct uart_amba_port *uap = amba_get_drvdata(dev);
2013
2014         if (!uap)
2015                 return -EINVAL;
2016
2017         return uart_suspend_port(&amba_reg, &uap->port);
2018 }
2019
2020 static int pl011_resume(struct amba_device *dev)
2021 {
2022         struct uart_amba_port *uap = amba_get_drvdata(dev);
2023
2024         if (!uap)
2025                 return -EINVAL;
2026
2027         return uart_resume_port(&amba_reg, &uap->port);
2028 }
2029 #endif
2030
2031 static struct amba_id pl011_ids[] = {
2032         {
2033                 .id     = 0x00041011,
2034                 .mask   = 0x000fffff,
2035                 .data   = &vendor_arm,
2036         },
2037         {
2038                 .id     = 0x00380802,
2039                 .mask   = 0x00ffffff,
2040                 .data   = &vendor_st,
2041         },
2042         { 0, 0 },
2043 };
2044
2045 MODULE_DEVICE_TABLE(amba, pl011_ids);
2046
2047 static struct amba_driver pl011_driver = {
2048         .drv = {
2049                 .name   = "uart-pl011",
2050         },
2051         .id_table       = pl011_ids,
2052         .probe          = pl011_probe,
2053         .remove         = pl011_remove,
2054 #ifdef CONFIG_PM
2055         .suspend        = pl011_suspend,
2056         .resume         = pl011_resume,
2057 #endif
2058 };
2059
2060 static int __init pl011_init(void)
2061 {
2062         int ret;
2063         printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2064
2065         ret = uart_register_driver(&amba_reg);
2066         if (ret == 0) {
2067                 ret = amba_driver_register(&pl011_driver);
2068                 if (ret)
2069                         uart_unregister_driver(&amba_reg);
2070         }
2071         return ret;
2072 }
2073
2074 static void __exit pl011_exit(void)
2075 {
2076         amba_driver_unregister(&pl011_driver);
2077         uart_unregister_driver(&amba_reg);
2078 }
2079
2080 /*
2081  * While this can be a module, if builtin it's most likely the console
2082  * So let's leave module_exit but move module_init to an earlier place
2083  */
2084 arch_initcall(pl011_init);
2085 module_exit(pl011_exit);
2086
2087 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2088 MODULE_DESCRIPTION("ARM AMBA serial port driver");
2089 MODULE_LICENSE("GPL");