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