1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for AMBA serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000 Deep Blue Solutions Ltd.
9 * Copyright (C) 2010 ST-Ericsson SA
11 * This is a generic driver for ARM AMBA-type serial ports. They
12 * have a lot of 16550-like features, but are not register compatible.
13 * Note that although they do have CTS, DCD and DSR inputs, they do
14 * not have an RI input, nor do they have DTR or RTS outputs. If
15 * required, these have to be supplied via some other means (eg, GPIO)
16 * and hooked into this driver.
19 #include <linux/module.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/console.h>
23 #include <linux/platform_device.h>
24 #include <linux/sysrq.h>
25 #include <linux/device.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial_core.h>
29 #include <linux/serial.h>
30 #include <linux/amba/bus.h>
31 #include <linux/amba/serial.h>
32 #include <linux/clk.h>
33 #include <linux/slab.h>
34 #include <linux/dmaengine.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/scatterlist.h>
37 #include <linux/delay.h>
38 #include <linux/types.h>
40 #include <linux/pinctrl/consumer.h>
41 #include <linux/sizes.h>
43 #include <linux/acpi.h>
47 #define SERIAL_AMBA_MAJOR 204
48 #define SERIAL_AMBA_MINOR 64
49 #define SERIAL_AMBA_NR UART_NR
51 #define AMBA_ISR_PASS_LIMIT 256
53 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
54 #define UART_DUMMY_DR_RX (1 << 16)
82 /* The size of the array - must be last */
86 static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
87 [REG_DR] = UART01x_DR,
88 [REG_FR] = UART01x_FR,
89 [REG_LCRH_RX] = UART011_LCRH,
90 [REG_LCRH_TX] = UART011_LCRH,
91 [REG_IBRD] = UART011_IBRD,
92 [REG_FBRD] = UART011_FBRD,
93 [REG_CR] = UART011_CR,
94 [REG_IFLS] = UART011_IFLS,
95 [REG_IMSC] = UART011_IMSC,
96 [REG_RIS] = UART011_RIS,
97 [REG_MIS] = UART011_MIS,
98 [REG_ICR] = UART011_ICR,
99 [REG_DMACR] = UART011_DMACR,
102 /* There is by now at least one vendor with differing details, so handle it */
104 const u16 *reg_offset;
106 unsigned int fr_busy;
114 bool cts_event_workaround;
118 unsigned int (*get_fifosize)(struct amba_device *dev);
121 static unsigned int get_fifosize_arm(struct amba_device *dev)
123 return amba_rev(dev) < 3 ? 16 : 32;
126 static struct vendor_data vendor_arm = {
127 .reg_offset = pl011_std_offsets,
128 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
129 .fr_busy = UART01x_FR_BUSY,
130 .fr_dsr = UART01x_FR_DSR,
131 .fr_cts = UART01x_FR_CTS,
132 .fr_ri = UART011_FR_RI,
133 .oversampling = false,
134 .dma_threshold = false,
135 .cts_event_workaround = false,
136 .always_enabled = false,
137 .fixed_options = false,
138 .get_fifosize = get_fifosize_arm,
141 static const struct vendor_data vendor_sbsa = {
142 .reg_offset = pl011_std_offsets,
143 .fr_busy = UART01x_FR_BUSY,
144 .fr_dsr = UART01x_FR_DSR,
145 .fr_cts = UART01x_FR_CTS,
146 .fr_ri = UART011_FR_RI,
148 .oversampling = false,
149 .dma_threshold = false,
150 .cts_event_workaround = false,
151 .always_enabled = true,
152 .fixed_options = true,
155 #ifdef CONFIG_ACPI_SPCR_TABLE
156 static const struct vendor_data vendor_qdt_qdf2400_e44 = {
157 .reg_offset = pl011_std_offsets,
158 .fr_busy = UART011_FR_TXFE,
159 .fr_dsr = UART01x_FR_DSR,
160 .fr_cts = UART01x_FR_CTS,
161 .fr_ri = UART011_FR_RI,
162 .inv_fr = UART011_FR_TXFE,
164 .oversampling = false,
165 .dma_threshold = false,
166 .cts_event_workaround = false,
167 .always_enabled = true,
168 .fixed_options = true,
172 static u16 pl011_st_offsets[REG_ARRAY_SIZE] = {
173 [REG_DR] = UART01x_DR,
174 [REG_ST_DMAWM] = ST_UART011_DMAWM,
175 [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
176 [REG_FR] = UART01x_FR,
177 [REG_LCRH_RX] = ST_UART011_LCRH_RX,
178 [REG_LCRH_TX] = ST_UART011_LCRH_TX,
179 [REG_IBRD] = UART011_IBRD,
180 [REG_FBRD] = UART011_FBRD,
181 [REG_CR] = UART011_CR,
182 [REG_IFLS] = UART011_IFLS,
183 [REG_IMSC] = UART011_IMSC,
184 [REG_RIS] = UART011_RIS,
185 [REG_MIS] = UART011_MIS,
186 [REG_ICR] = UART011_ICR,
187 [REG_DMACR] = UART011_DMACR,
188 [REG_ST_XFCR] = ST_UART011_XFCR,
189 [REG_ST_XON1] = ST_UART011_XON1,
190 [REG_ST_XON2] = ST_UART011_XON2,
191 [REG_ST_XOFF1] = ST_UART011_XOFF1,
192 [REG_ST_XOFF2] = ST_UART011_XOFF2,
193 [REG_ST_ITCR] = ST_UART011_ITCR,
194 [REG_ST_ITIP] = ST_UART011_ITIP,
195 [REG_ST_ABCR] = ST_UART011_ABCR,
196 [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
199 static unsigned int get_fifosize_st(struct amba_device *dev)
204 static struct vendor_data vendor_st = {
205 .reg_offset = pl011_st_offsets,
206 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
207 .fr_busy = UART01x_FR_BUSY,
208 .fr_dsr = UART01x_FR_DSR,
209 .fr_cts = UART01x_FR_CTS,
210 .fr_ri = UART011_FR_RI,
211 .oversampling = true,
212 .dma_threshold = true,
213 .cts_event_workaround = true,
214 .always_enabled = false,
215 .fixed_options = false,
216 .get_fifosize = get_fifosize_st,
219 /* Deals with DMA transactions */
221 struct pl011_dmabuf {
227 struct pl011_dmarx_data {
228 struct dma_chan *chan;
229 struct completion complete;
231 struct pl011_dmabuf dbuf_a;
232 struct pl011_dmabuf dbuf_b;
235 struct timer_list timer;
236 unsigned int last_residue;
237 unsigned long last_jiffies;
239 unsigned int poll_rate;
240 unsigned int poll_timeout;
243 struct pl011_dmatx_data {
244 struct dma_chan *chan;
252 * We wrap our port structure around the generic uart_port.
254 struct uart_amba_port {
255 struct uart_port port;
256 const u16 *reg_offset;
258 const struct vendor_data *vendor;
259 unsigned int dmacr; /* dma control reg */
260 unsigned int im; /* interrupt mask */
261 unsigned int old_status;
262 unsigned int fifosize; /* vendor-specific */
263 unsigned int fixed_baud; /* vendor-set fixed baud rate */
265 bool rs485_tx_started;
266 unsigned int rs485_tx_drain_interval; /* usecs */
267 #ifdef CONFIG_DMA_ENGINE
271 struct pl011_dmarx_data dmarx;
272 struct pl011_dmatx_data dmatx;
277 static unsigned int pl011_tx_empty(struct uart_port *port);
279 static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
282 return uap->reg_offset[reg];
285 static unsigned int pl011_read(const struct uart_amba_port *uap,
288 void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
290 return (uap->port.iotype == UPIO_MEM32) ?
291 readl_relaxed(addr) : readw_relaxed(addr);
294 static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
297 void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
299 if (uap->port.iotype == UPIO_MEM32)
300 writel_relaxed(val, addr);
302 writew_relaxed(val, addr);
306 * Reads up to 256 characters from the FIFO or until it's empty and
307 * inserts them into the TTY layer. Returns the number of characters
308 * read from the FIFO.
310 static int pl011_fifo_to_tty(struct uart_amba_port *uap)
312 unsigned int ch, fifotaken;
317 for (fifotaken = 0; fifotaken != 256; fifotaken++) {
318 status = pl011_read(uap, REG_FR);
319 if (status & UART01x_FR_RXFE)
322 /* Take chars from the FIFO and update status */
323 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
325 uap->port.icount.rx++;
327 if (unlikely(ch & UART_DR_ERROR)) {
328 if (ch & UART011_DR_BE) {
329 ch &= ~(UART011_DR_FE | UART011_DR_PE);
330 uap->port.icount.brk++;
331 if (uart_handle_break(&uap->port))
333 } else if (ch & UART011_DR_PE)
334 uap->port.icount.parity++;
335 else if (ch & UART011_DR_FE)
336 uap->port.icount.frame++;
337 if (ch & UART011_DR_OE)
338 uap->port.icount.overrun++;
340 ch &= uap->port.read_status_mask;
342 if (ch & UART011_DR_BE)
344 else if (ch & UART011_DR_PE)
346 else if (ch & UART011_DR_FE)
350 spin_unlock(&uap->port.lock);
351 sysrq = uart_handle_sysrq_char(&uap->port, ch & 255);
352 spin_lock(&uap->port.lock);
355 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
363 * All the DMA operation mode stuff goes inside this ifdef.
364 * This assumes that you have a generic DMA device interface,
365 * no custom DMA interfaces are supported.
367 #ifdef CONFIG_DMA_ENGINE
369 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
371 static int pl011_dmabuf_init(struct dma_chan *chan, struct pl011_dmabuf *db,
372 enum dma_data_direction dir)
374 db->buf = dma_alloc_coherent(chan->device->dev, PL011_DMA_BUFFER_SIZE,
375 &db->dma, GFP_KERNEL);
378 db->len = PL011_DMA_BUFFER_SIZE;
383 static void pl011_dmabuf_free(struct dma_chan *chan, struct pl011_dmabuf *db,
384 enum dma_data_direction dir)
387 dma_free_coherent(chan->device->dev,
388 PL011_DMA_BUFFER_SIZE, db->buf, db->dma);
392 static void pl011_dma_probe(struct uart_amba_port *uap)
394 /* DMA is the sole user of the platform data right now */
395 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
396 struct device *dev = uap->port.dev;
397 struct dma_slave_config tx_conf = {
398 .dst_addr = uap->port.mapbase +
399 pl011_reg_to_offset(uap, REG_DR),
400 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
401 .direction = DMA_MEM_TO_DEV,
402 .dst_maxburst = uap->fifosize >> 1,
405 struct dma_chan *chan;
408 uap->dma_probed = true;
409 chan = dma_request_chan(dev, "tx");
411 if (PTR_ERR(chan) == -EPROBE_DEFER) {
412 uap->dma_probed = false;
416 /* We need platform data */
417 if (!plat || !plat->dma_filter) {
418 dev_info(uap->port.dev, "no DMA platform data\n");
422 /* Try to acquire a generic DMA engine slave TX channel */
424 dma_cap_set(DMA_SLAVE, mask);
426 chan = dma_request_channel(mask, plat->dma_filter,
429 dev_err(uap->port.dev, "no TX DMA channel!\n");
434 dmaengine_slave_config(chan, &tx_conf);
435 uap->dmatx.chan = chan;
437 dev_info(uap->port.dev, "DMA channel TX %s\n",
438 dma_chan_name(uap->dmatx.chan));
440 /* Optionally make use of an RX channel as well */
441 chan = dma_request_slave_channel(dev, "rx");
443 if (!chan && plat && plat->dma_rx_param) {
444 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
447 dev_err(uap->port.dev, "no RX DMA channel!\n");
453 struct dma_slave_config rx_conf = {
454 .src_addr = uap->port.mapbase +
455 pl011_reg_to_offset(uap, REG_DR),
456 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
457 .direction = DMA_DEV_TO_MEM,
458 .src_maxburst = uap->fifosize >> 2,
461 struct dma_slave_caps caps;
464 * Some DMA controllers provide information on their capabilities.
465 * If the controller does, check for suitable residue processing
466 * otherwise assime all is well.
468 if (0 == dma_get_slave_caps(chan, &caps)) {
469 if (caps.residue_granularity ==
470 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
471 dma_release_channel(chan);
472 dev_info(uap->port.dev,
473 "RX DMA disabled - no residue processing\n");
477 dmaengine_slave_config(chan, &rx_conf);
478 uap->dmarx.chan = chan;
480 uap->dmarx.auto_poll_rate = false;
481 if (plat && plat->dma_rx_poll_enable) {
482 /* Set poll rate if specified. */
483 if (plat->dma_rx_poll_rate) {
484 uap->dmarx.auto_poll_rate = false;
485 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
488 * 100 ms defaults to poll rate if not
489 * specified. This will be adjusted with
490 * the baud rate at set_termios.
492 uap->dmarx.auto_poll_rate = true;
493 uap->dmarx.poll_rate = 100;
495 /* 3 secs defaults poll_timeout if not specified. */
496 if (plat->dma_rx_poll_timeout)
497 uap->dmarx.poll_timeout =
498 plat->dma_rx_poll_timeout;
500 uap->dmarx.poll_timeout = 3000;
501 } else if (!plat && dev->of_node) {
502 uap->dmarx.auto_poll_rate = of_property_read_bool(
503 dev->of_node, "auto-poll");
504 if (uap->dmarx.auto_poll_rate) {
507 if (0 == of_property_read_u32(dev->of_node,
509 uap->dmarx.poll_rate = x;
511 uap->dmarx.poll_rate = 100;
512 if (0 == of_property_read_u32(dev->of_node,
513 "poll-timeout-ms", &x))
514 uap->dmarx.poll_timeout = x;
516 uap->dmarx.poll_timeout = 3000;
519 dev_info(uap->port.dev, "DMA channel RX %s\n",
520 dma_chan_name(uap->dmarx.chan));
524 static void pl011_dma_remove(struct uart_amba_port *uap)
527 dma_release_channel(uap->dmatx.chan);
529 dma_release_channel(uap->dmarx.chan);
532 /* Forward declare these for the refill routine */
533 static int pl011_dma_tx_refill(struct uart_amba_port *uap);
534 static void pl011_start_tx_pio(struct uart_amba_port *uap);
537 * The current DMA TX buffer has been sent.
538 * Try to queue up another DMA buffer.
540 static void pl011_dma_tx_callback(void *data)
542 struct uart_amba_port *uap = data;
543 struct pl011_dmatx_data *dmatx = &uap->dmatx;
547 spin_lock_irqsave(&uap->port.lock, flags);
548 if (uap->dmatx.queued)
549 dma_unmap_single(dmatx->chan->device->dev, dmatx->dma,
550 dmatx->len, DMA_TO_DEVICE);
553 uap->dmacr = dmacr & ~UART011_TXDMAE;
554 pl011_write(uap->dmacr, uap, REG_DMACR);
557 * If TX DMA was disabled, it means that we've stopped the DMA for
558 * some reason (eg, XOFF received, or we want to send an X-char.)
560 * Note: we need to be careful here of a potential race between DMA
561 * and the rest of the driver - if the driver disables TX DMA while
562 * a TX buffer completing, we must update the tx queued status to
563 * get further refills (hence we check dmacr).
565 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
566 uart_circ_empty(&uap->port.state->xmit)) {
567 uap->dmatx.queued = false;
568 spin_unlock_irqrestore(&uap->port.lock, flags);
572 if (pl011_dma_tx_refill(uap) <= 0)
574 * We didn't queue a DMA buffer for some reason, but we
575 * have data pending to be sent. Re-enable the TX IRQ.
577 pl011_start_tx_pio(uap);
579 spin_unlock_irqrestore(&uap->port.lock, flags);
583 * Try to refill the TX DMA buffer.
584 * Locking: called with port lock held and IRQs disabled.
586 * 1 if we queued up a TX DMA buffer.
587 * 0 if we didn't want to handle this by DMA
590 static int pl011_dma_tx_refill(struct uart_amba_port *uap)
592 struct pl011_dmatx_data *dmatx = &uap->dmatx;
593 struct dma_chan *chan = dmatx->chan;
594 struct dma_device *dma_dev = chan->device;
595 struct dma_async_tx_descriptor *desc;
596 struct circ_buf *xmit = &uap->port.state->xmit;
600 * Try to avoid the overhead involved in using DMA if the
601 * transaction fits in the first half of the FIFO, by using
602 * the standard interrupt handling. This ensures that we
603 * issue a uart_write_wakeup() at the appropriate time.
605 count = uart_circ_chars_pending(xmit);
606 if (count < (uap->fifosize >> 1)) {
607 uap->dmatx.queued = false;
612 * Bodge: don't send the last character by DMA, as this
613 * will prevent XON from notifying us to restart DMA.
617 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
618 if (count > PL011_DMA_BUFFER_SIZE)
619 count = PL011_DMA_BUFFER_SIZE;
621 if (xmit->tail < xmit->head)
622 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
624 size_t first = UART_XMIT_SIZE - xmit->tail;
629 second = count - first;
631 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
633 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
637 dmatx->dma = dma_map_single(dma_dev->dev, dmatx->buf, count,
639 if (dmatx->dma == DMA_MAPPING_ERROR) {
640 uap->dmatx.queued = false;
641 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
645 desc = dmaengine_prep_slave_single(chan, dmatx->dma, dmatx->len, DMA_MEM_TO_DEV,
646 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
648 dma_unmap_single(dma_dev->dev, dmatx->dma, dmatx->len, DMA_TO_DEVICE);
649 uap->dmatx.queued = false;
651 * If DMA cannot be used right now, we complete this
652 * transaction via IRQ and let the TTY layer retry.
654 dev_dbg(uap->port.dev, "TX DMA busy\n");
658 /* Some data to go along to the callback */
659 desc->callback = pl011_dma_tx_callback;
660 desc->callback_param = uap;
662 /* All errors should happen at prepare time */
663 dmaengine_submit(desc);
665 /* Fire the DMA transaction */
666 dma_dev->device_issue_pending(chan);
668 uap->dmacr |= UART011_TXDMAE;
669 pl011_write(uap->dmacr, uap, REG_DMACR);
670 uap->dmatx.queued = true;
673 * Now we know that DMA will fire, so advance the ring buffer
674 * with the stuff we just dispatched.
676 uart_xmit_advance(&uap->port, count);
678 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
679 uart_write_wakeup(&uap->port);
685 * We received a transmit interrupt without a pending X-char but with
686 * pending characters.
687 * Locking: called with port lock held and IRQs disabled.
689 * false if we want to use PIO to transmit
690 * true if we queued a DMA buffer
692 static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
694 if (!uap->using_tx_dma)
698 * If we already have a TX buffer queued, but received a
699 * TX interrupt, it will be because we've just sent an X-char.
700 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
702 if (uap->dmatx.queued) {
703 uap->dmacr |= UART011_TXDMAE;
704 pl011_write(uap->dmacr, uap, REG_DMACR);
705 uap->im &= ~UART011_TXIM;
706 pl011_write(uap->im, uap, REG_IMSC);
711 * We don't have a TX buffer queued, so try to queue one.
712 * If we successfully queued a buffer, mask the TX IRQ.
714 if (pl011_dma_tx_refill(uap) > 0) {
715 uap->im &= ~UART011_TXIM;
716 pl011_write(uap->im, uap, REG_IMSC);
723 * Stop the DMA transmit (eg, due to received XOFF).
724 * Locking: called with port lock held and IRQs disabled.
726 static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
728 if (uap->dmatx.queued) {
729 uap->dmacr &= ~UART011_TXDMAE;
730 pl011_write(uap->dmacr, uap, REG_DMACR);
735 * Try to start a DMA transmit, or in the case of an XON/OFF
736 * character queued for send, try to get that character out ASAP.
737 * Locking: called with port lock held and IRQs disabled.
739 * false if we want the TX IRQ to be enabled
740 * true if we have a buffer queued
742 static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
746 if (!uap->using_tx_dma)
749 if (!uap->port.x_char) {
750 /* no X-char, try to push chars out in DMA mode */
753 if (!uap->dmatx.queued) {
754 if (pl011_dma_tx_refill(uap) > 0) {
755 uap->im &= ~UART011_TXIM;
756 pl011_write(uap->im, uap, REG_IMSC);
759 } else if (!(uap->dmacr & UART011_TXDMAE)) {
760 uap->dmacr |= UART011_TXDMAE;
761 pl011_write(uap->dmacr, uap, REG_DMACR);
767 * We have an X-char to send. Disable DMA to prevent it loading
768 * the TX fifo, and then see if we can stuff it into the FIFO.
771 uap->dmacr &= ~UART011_TXDMAE;
772 pl011_write(uap->dmacr, uap, REG_DMACR);
774 if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
776 * No space in the FIFO, so enable the transmit interrupt
777 * so we know when there is space. Note that once we've
778 * loaded the character, we should just re-enable DMA.
783 pl011_write(uap->port.x_char, uap, REG_DR);
784 uap->port.icount.tx++;
785 uap->port.x_char = 0;
787 /* Success - restore the DMA state */
789 pl011_write(dmacr, uap, REG_DMACR);
795 * Flush the transmit buffer.
796 * Locking: called with port lock held and IRQs disabled.
798 static void pl011_dma_flush_buffer(struct uart_port *port)
799 __releases(&uap->port.lock)
800 __acquires(&uap->port.lock)
802 struct uart_amba_port *uap =
803 container_of(port, struct uart_amba_port, port);
805 if (!uap->using_tx_dma)
808 dmaengine_terminate_async(uap->dmatx.chan);
810 if (uap->dmatx.queued) {
811 dma_unmap_single(uap->dmatx.chan->device->dev, uap->dmatx.dma,
812 uap->dmatx.len, DMA_TO_DEVICE);
813 uap->dmatx.queued = false;
814 uap->dmacr &= ~UART011_TXDMAE;
815 pl011_write(uap->dmacr, uap, REG_DMACR);
819 static void pl011_dma_rx_callback(void *data);
821 static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
823 struct dma_chan *rxchan = uap->dmarx.chan;
824 struct pl011_dmarx_data *dmarx = &uap->dmarx;
825 struct dma_async_tx_descriptor *desc;
826 struct pl011_dmabuf *dbuf;
831 /* Start the RX DMA job */
832 dbuf = uap->dmarx.use_buf_b ?
833 &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
834 desc = dmaengine_prep_slave_single(rxchan, dbuf->dma, dbuf->len,
836 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
838 * If the DMA engine is busy and cannot prepare a
839 * channel, no big deal, the driver will fall back
840 * to interrupt mode as a result of this error code.
843 uap->dmarx.running = false;
844 dmaengine_terminate_all(rxchan);
848 /* Some data to go along to the callback */
849 desc->callback = pl011_dma_rx_callback;
850 desc->callback_param = uap;
851 dmarx->cookie = dmaengine_submit(desc);
852 dma_async_issue_pending(rxchan);
854 uap->dmacr |= UART011_RXDMAE;
855 pl011_write(uap->dmacr, uap, REG_DMACR);
856 uap->dmarx.running = true;
858 uap->im &= ~UART011_RXIM;
859 pl011_write(uap->im, uap, REG_IMSC);
865 * This is called when either the DMA job is complete, or
866 * the FIFO timeout interrupt occurred. This must be called
867 * with the port spinlock uap->port.lock held.
869 static void pl011_dma_rx_chars(struct uart_amba_port *uap,
870 u32 pending, bool use_buf_b,
873 struct tty_port *port = &uap->port.state->port;
874 struct pl011_dmabuf *dbuf = use_buf_b ?
875 &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
877 u32 fifotaken = 0; /* only used for vdbg() */
879 struct pl011_dmarx_data *dmarx = &uap->dmarx;
882 if (uap->dmarx.poll_rate) {
883 /* The data can be taken by polling */
884 dmataken = dbuf->len - dmarx->last_residue;
885 /* Recalculate the pending size */
886 if (pending >= dmataken)
890 /* Pick the remain data from the DMA */
894 * First take all chars in the DMA pipe, then look in the FIFO.
895 * Note that tty_insert_flip_buf() tries to take as many chars
898 dma_count = tty_insert_flip_string(port, dbuf->buf + dmataken,
901 uap->port.icount.rx += dma_count;
902 if (dma_count < pending)
903 dev_warn(uap->port.dev,
904 "couldn't insert all characters (TTY is full?)\n");
907 /* Reset the last_residue for Rx DMA poll */
908 if (uap->dmarx.poll_rate)
909 dmarx->last_residue = dbuf->len;
912 * Only continue with trying to read the FIFO if all DMA chars have
915 if (dma_count == pending && readfifo) {
916 /* Clear any error flags */
917 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
918 UART011_FEIS, uap, REG_ICR);
921 * If we read all the DMA'd characters, and we had an
922 * incomplete buffer, that could be due to an rx error, or
923 * maybe we just timed out. Read any pending chars and check
926 * Error conditions will only occur in the FIFO, these will
927 * trigger an immediate interrupt and stop the DMA job, so we
928 * will always find the error in the FIFO, never in the DMA
931 fifotaken = pl011_fifo_to_tty(uap);
934 dev_vdbg(uap->port.dev,
935 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
936 dma_count, fifotaken);
937 tty_flip_buffer_push(port);
940 static void pl011_dma_rx_irq(struct uart_amba_port *uap)
942 struct pl011_dmarx_data *dmarx = &uap->dmarx;
943 struct dma_chan *rxchan = dmarx->chan;
944 struct pl011_dmabuf *dbuf = dmarx->use_buf_b ?
945 &dmarx->dbuf_b : &dmarx->dbuf_a;
947 struct dma_tx_state state;
948 enum dma_status dmastat;
951 * Pause the transfer so we can trust the current counter,
952 * do this before we pause the PL011 block, else we may
955 if (dmaengine_pause(rxchan))
956 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
957 dmastat = rxchan->device->device_tx_status(rxchan,
958 dmarx->cookie, &state);
959 if (dmastat != DMA_PAUSED)
960 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
962 /* Disable RX DMA - incoming data will wait in the FIFO */
963 uap->dmacr &= ~UART011_RXDMAE;
964 pl011_write(uap->dmacr, uap, REG_DMACR);
965 uap->dmarx.running = false;
967 pending = dbuf->len - state.residue;
968 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
969 /* Then we terminate the transfer - we now know our residue */
970 dmaengine_terminate_all(rxchan);
973 * This will take the chars we have so far and insert
974 * into the framework.
976 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
978 /* Switch buffer & re-trigger DMA job */
979 dmarx->use_buf_b = !dmarx->use_buf_b;
980 if (pl011_dma_rx_trigger_dma(uap)) {
981 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
982 "fall back to interrupt mode\n");
983 uap->im |= UART011_RXIM;
984 pl011_write(uap->im, uap, REG_IMSC);
988 static void pl011_dma_rx_callback(void *data)
990 struct uart_amba_port *uap = data;
991 struct pl011_dmarx_data *dmarx = &uap->dmarx;
992 struct dma_chan *rxchan = dmarx->chan;
993 bool lastbuf = dmarx->use_buf_b;
994 struct pl011_dmabuf *dbuf = dmarx->use_buf_b ?
995 &dmarx->dbuf_b : &dmarx->dbuf_a;
997 struct dma_tx_state state;
1001 * This completion interrupt occurs typically when the
1002 * RX buffer is totally stuffed but no timeout has yet
1003 * occurred. When that happens, we just want the RX
1004 * routine to flush out the secondary DMA buffer while
1005 * we immediately trigger the next DMA job.
1007 spin_lock_irq(&uap->port.lock);
1009 * Rx data can be taken by the UART interrupts during
1010 * the DMA irq handler. So we check the residue here.
1012 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1013 pending = dbuf->len - state.residue;
1014 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
1015 /* Then we terminate the transfer - we now know our residue */
1016 dmaengine_terminate_all(rxchan);
1018 uap->dmarx.running = false;
1019 dmarx->use_buf_b = !lastbuf;
1020 ret = pl011_dma_rx_trigger_dma(uap);
1022 pl011_dma_rx_chars(uap, pending, lastbuf, false);
1023 spin_unlock_irq(&uap->port.lock);
1025 * Do this check after we picked the DMA chars so we don't
1026 * get some IRQ immediately from RX.
1029 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
1030 "fall back to interrupt mode\n");
1031 uap->im |= UART011_RXIM;
1032 pl011_write(uap->im, uap, REG_IMSC);
1037 * Stop accepting received characters, when we're shutting down or
1038 * suspending this port.
1039 * Locking: called with port lock held and IRQs disabled.
1041 static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1043 if (!uap->using_rx_dma)
1046 /* FIXME. Just disable the DMA enable */
1047 uap->dmacr &= ~UART011_RXDMAE;
1048 pl011_write(uap->dmacr, uap, REG_DMACR);
1052 * Timer handler for Rx DMA polling.
1053 * Every polling, It checks the residue in the dma buffer and transfer
1054 * data to the tty. Also, last_residue is updated for the next polling.
1056 static void pl011_dma_rx_poll(struct timer_list *t)
1058 struct uart_amba_port *uap = from_timer(uap, t, dmarx.timer);
1059 struct tty_port *port = &uap->port.state->port;
1060 struct pl011_dmarx_data *dmarx = &uap->dmarx;
1061 struct dma_chan *rxchan = uap->dmarx.chan;
1062 unsigned long flags;
1063 unsigned int dmataken = 0;
1064 unsigned int size = 0;
1065 struct pl011_dmabuf *dbuf;
1067 struct dma_tx_state state;
1069 dbuf = dmarx->use_buf_b ? &uap->dmarx.dbuf_b : &uap->dmarx.dbuf_a;
1070 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1071 if (likely(state.residue < dmarx->last_residue)) {
1072 dmataken = dbuf->len - dmarx->last_residue;
1073 size = dmarx->last_residue - state.residue;
1074 dma_count = tty_insert_flip_string(port, dbuf->buf + dmataken,
1076 if (dma_count == size)
1077 dmarx->last_residue = state.residue;
1078 dmarx->last_jiffies = jiffies;
1080 tty_flip_buffer_push(port);
1083 * If no data is received in poll_timeout, the driver will fall back
1084 * to interrupt mode. We will retrigger DMA at the first interrupt.
1086 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1087 > uap->dmarx.poll_timeout) {
1089 spin_lock_irqsave(&uap->port.lock, flags);
1090 pl011_dma_rx_stop(uap);
1091 uap->im |= UART011_RXIM;
1092 pl011_write(uap->im, uap, REG_IMSC);
1093 spin_unlock_irqrestore(&uap->port.lock, flags);
1095 uap->dmarx.running = false;
1096 dmaengine_terminate_all(rxchan);
1097 del_timer(&uap->dmarx.timer);
1099 mod_timer(&uap->dmarx.timer,
1100 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1104 static void pl011_dma_startup(struct uart_amba_port *uap)
1108 if (!uap->dma_probed)
1109 pl011_dma_probe(uap);
1111 if (!uap->dmatx.chan)
1114 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
1115 if (!uap->dmatx.buf) {
1116 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1117 uap->port.fifosize = uap->fifosize;
1121 uap->dmatx.len = PL011_DMA_BUFFER_SIZE;
1123 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1124 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
1125 uap->using_tx_dma = true;
1127 if (!uap->dmarx.chan)
1130 /* Allocate and map DMA RX buffers */
1131 ret = pl011_dmabuf_init(uap->dmarx.chan, &uap->dmarx.dbuf_a,
1134 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1135 "RX buffer A", ret);
1139 ret = pl011_dmabuf_init(uap->dmarx.chan, &uap->dmarx.dbuf_b,
1142 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1143 "RX buffer B", ret);
1144 pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_a,
1149 uap->using_rx_dma = true;
1152 /* Turn on DMA error (RX/TX will be enabled on demand) */
1153 uap->dmacr |= UART011_DMAONERR;
1154 pl011_write(uap->dmacr, uap, REG_DMACR);
1157 * ST Micro variants has some specific dma burst threshold
1158 * compensation. Set this to 16 bytes, so burst will only
1159 * be issued above/below 16 bytes.
1161 if (uap->vendor->dma_threshold)
1162 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1165 if (uap->using_rx_dma) {
1166 if (pl011_dma_rx_trigger_dma(uap))
1167 dev_dbg(uap->port.dev, "could not trigger initial "
1168 "RX DMA job, fall back to interrupt mode\n");
1169 if (uap->dmarx.poll_rate) {
1170 timer_setup(&uap->dmarx.timer, pl011_dma_rx_poll, 0);
1171 mod_timer(&uap->dmarx.timer,
1173 msecs_to_jiffies(uap->dmarx.poll_rate));
1174 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1175 uap->dmarx.last_jiffies = jiffies;
1180 static void pl011_dma_shutdown(struct uart_amba_port *uap)
1182 if (!(uap->using_tx_dma || uap->using_rx_dma))
1185 /* Disable RX and TX DMA */
1186 while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
1189 spin_lock_irq(&uap->port.lock);
1190 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1191 pl011_write(uap->dmacr, uap, REG_DMACR);
1192 spin_unlock_irq(&uap->port.lock);
1194 if (uap->using_tx_dma) {
1195 /* In theory, this should already be done by pl011_dma_flush_buffer */
1196 dmaengine_terminate_all(uap->dmatx.chan);
1197 if (uap->dmatx.queued) {
1198 dma_unmap_single(uap->dmatx.chan->device->dev,
1199 uap->dmatx.dma, uap->dmatx.len,
1201 uap->dmatx.queued = false;
1204 kfree(uap->dmatx.buf);
1205 uap->using_tx_dma = false;
1208 if (uap->using_rx_dma) {
1209 dmaengine_terminate_all(uap->dmarx.chan);
1210 /* Clean up the RX DMA */
1211 pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_a, DMA_FROM_DEVICE);
1212 pl011_dmabuf_free(uap->dmarx.chan, &uap->dmarx.dbuf_b, DMA_FROM_DEVICE);
1213 if (uap->dmarx.poll_rate)
1214 del_timer_sync(&uap->dmarx.timer);
1215 uap->using_rx_dma = false;
1219 static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1221 return uap->using_rx_dma;
1224 static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1226 return uap->using_rx_dma && uap->dmarx.running;
1230 /* Blank functions if the DMA engine is not available */
1231 static inline void pl011_dma_remove(struct uart_amba_port *uap)
1235 static inline void pl011_dma_startup(struct uart_amba_port *uap)
1239 static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1243 static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1248 static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1252 static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1257 static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1261 static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1265 static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1270 static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1275 static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1280 #define pl011_dma_flush_buffer NULL
1283 static void pl011_rs485_tx_stop(struct uart_amba_port *uap)
1286 * To be on the safe side only time out after twice as many iterations
1289 const int MAX_TX_DRAIN_ITERS = uap->port.fifosize * 2;
1290 struct uart_port *port = &uap->port;
1294 /* Wait until hardware tx queue is empty */
1295 while (!pl011_tx_empty(port)) {
1296 if (i > MAX_TX_DRAIN_ITERS) {
1298 "timeout while draining hardware tx queue\n");
1302 udelay(uap->rs485_tx_drain_interval);
1306 if (port->rs485.delay_rts_after_send)
1307 mdelay(port->rs485.delay_rts_after_send);
1309 cr = pl011_read(uap, REG_CR);
1311 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1312 cr &= ~UART011_CR_RTS;
1314 cr |= UART011_CR_RTS;
1316 /* Disable the transmitter and reenable the transceiver */
1317 cr &= ~UART011_CR_TXE;
1318 cr |= UART011_CR_RXE;
1319 pl011_write(cr, uap, REG_CR);
1321 uap->rs485_tx_started = false;
1324 static void pl011_stop_tx(struct uart_port *port)
1326 struct uart_amba_port *uap =
1327 container_of(port, struct uart_amba_port, port);
1329 uap->im &= ~UART011_TXIM;
1330 pl011_write(uap->im, uap, REG_IMSC);
1331 pl011_dma_tx_stop(uap);
1333 if ((port->rs485.flags & SER_RS485_ENABLED) && uap->rs485_tx_started)
1334 pl011_rs485_tx_stop(uap);
1337 static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
1339 /* Start TX with programmed I/O only (no DMA) */
1340 static void pl011_start_tx_pio(struct uart_amba_port *uap)
1342 if (pl011_tx_chars(uap, false)) {
1343 uap->im |= UART011_TXIM;
1344 pl011_write(uap->im, uap, REG_IMSC);
1348 static void pl011_start_tx(struct uart_port *port)
1350 struct uart_amba_port *uap =
1351 container_of(port, struct uart_amba_port, port);
1353 if (!pl011_dma_tx_start(uap))
1354 pl011_start_tx_pio(uap);
1357 static void pl011_stop_rx(struct uart_port *port)
1359 struct uart_amba_port *uap =
1360 container_of(port, struct uart_amba_port, port);
1362 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1363 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1364 pl011_write(uap->im, uap, REG_IMSC);
1366 pl011_dma_rx_stop(uap);
1369 static void pl011_throttle_rx(struct uart_port *port)
1371 unsigned long flags;
1373 spin_lock_irqsave(&port->lock, flags);
1374 pl011_stop_rx(port);
1375 spin_unlock_irqrestore(&port->lock, flags);
1378 static void pl011_enable_ms(struct uart_port *port)
1380 struct uart_amba_port *uap =
1381 container_of(port, struct uart_amba_port, port);
1383 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1384 pl011_write(uap->im, uap, REG_IMSC);
1387 static void pl011_rx_chars(struct uart_amba_port *uap)
1388 __releases(&uap->port.lock)
1389 __acquires(&uap->port.lock)
1391 pl011_fifo_to_tty(uap);
1393 spin_unlock(&uap->port.lock);
1394 tty_flip_buffer_push(&uap->port.state->port);
1396 * If we were temporarily out of DMA mode for a while,
1397 * attempt to switch back to DMA mode again.
1399 if (pl011_dma_rx_available(uap)) {
1400 if (pl011_dma_rx_trigger_dma(uap)) {
1401 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1402 "fall back to interrupt mode again\n");
1403 uap->im |= UART011_RXIM;
1404 pl011_write(uap->im, uap, REG_IMSC);
1406 #ifdef CONFIG_DMA_ENGINE
1407 /* Start Rx DMA poll */
1408 if (uap->dmarx.poll_rate) {
1409 uap->dmarx.last_jiffies = jiffies;
1410 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1411 mod_timer(&uap->dmarx.timer,
1413 msecs_to_jiffies(uap->dmarx.poll_rate));
1418 spin_lock(&uap->port.lock);
1421 static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1424 if (unlikely(!from_irq) &&
1425 pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
1426 return false; /* unable to transmit character */
1428 pl011_write(c, uap, REG_DR);
1429 uap->port.icount.tx++;
1434 static void pl011_rs485_tx_start(struct uart_amba_port *uap)
1436 struct uart_port *port = &uap->port;
1439 /* Enable transmitter */
1440 cr = pl011_read(uap, REG_CR);
1441 cr |= UART011_CR_TXE;
1443 /* Disable receiver if half-duplex */
1444 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
1445 cr &= ~UART011_CR_RXE;
1447 if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
1448 cr &= ~UART011_CR_RTS;
1450 cr |= UART011_CR_RTS;
1452 pl011_write(cr, uap, REG_CR);
1454 if (port->rs485.delay_rts_before_send)
1455 mdelay(port->rs485.delay_rts_before_send);
1457 uap->rs485_tx_started = true;
1460 /* Returns true if tx interrupts have to be (kept) enabled */
1461 static bool pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
1463 struct circ_buf *xmit = &uap->port.state->xmit;
1464 int count = uap->fifosize >> 1;
1466 if ((uap->port.rs485.flags & SER_RS485_ENABLED) &&
1467 !uap->rs485_tx_started)
1468 pl011_rs485_tx_start(uap);
1470 if (uap->port.x_char) {
1471 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1473 uap->port.x_char = 0;
1476 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
1477 pl011_stop_tx(&uap->port);
1481 /* If we are using DMA mode, try to send some characters. */
1482 if (pl011_dma_tx_irq(uap))
1486 if (likely(from_irq) && count-- == 0)
1489 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1492 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1493 } while (!uart_circ_empty(xmit));
1495 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1496 uart_write_wakeup(&uap->port);
1498 if (uart_circ_empty(xmit)) {
1499 pl011_stop_tx(&uap->port);
1505 static void pl011_modem_status(struct uart_amba_port *uap)
1507 unsigned int status, delta;
1509 status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
1511 delta = status ^ uap->old_status;
1512 uap->old_status = status;
1517 if (delta & UART01x_FR_DCD)
1518 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1520 if (delta & uap->vendor->fr_dsr)
1521 uap->port.icount.dsr++;
1523 if (delta & uap->vendor->fr_cts)
1524 uart_handle_cts_change(&uap->port,
1525 status & uap->vendor->fr_cts);
1527 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
1530 static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1532 if (!uap->vendor->cts_event_workaround)
1535 /* workaround to make sure that all bits are unlocked.. */
1536 pl011_write(0x00, uap, REG_ICR);
1539 * WA: introduce 26ns(1 uart clk) delay before W1C;
1540 * single apb access will incur 2 pclk(133.12Mhz) delay,
1541 * so add 2 dummy reads
1543 pl011_read(uap, REG_ICR);
1544 pl011_read(uap, REG_ICR);
1547 static irqreturn_t pl011_int(int irq, void *dev_id)
1549 struct uart_amba_port *uap = dev_id;
1550 unsigned long flags;
1551 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1554 spin_lock_irqsave(&uap->port.lock, flags);
1555 status = pl011_read(uap, REG_RIS) & uap->im;
1558 check_apply_cts_event_workaround(uap);
1560 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1564 if (status & (UART011_RTIS|UART011_RXIS)) {
1565 if (pl011_dma_rx_running(uap))
1566 pl011_dma_rx_irq(uap);
1568 pl011_rx_chars(uap);
1570 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1571 UART011_CTSMIS|UART011_RIMIS))
1572 pl011_modem_status(uap);
1573 if (status & UART011_TXIS)
1574 pl011_tx_chars(uap, true);
1576 if (pass_counter-- == 0)
1579 status = pl011_read(uap, REG_RIS) & uap->im;
1580 } while (status != 0);
1584 spin_unlock_irqrestore(&uap->port.lock, flags);
1586 return IRQ_RETVAL(handled);
1589 static unsigned int pl011_tx_empty(struct uart_port *port)
1591 struct uart_amba_port *uap =
1592 container_of(port, struct uart_amba_port, port);
1594 /* Allow feature register bits to be inverted to work around errata */
1595 unsigned int status = pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr;
1597 return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
1601 static unsigned int pl011_get_mctrl(struct uart_port *port)
1603 struct uart_amba_port *uap =
1604 container_of(port, struct uart_amba_port, port);
1605 unsigned int result = 0;
1606 unsigned int status = pl011_read(uap, REG_FR);
1608 #define TIOCMBIT(uartbit, tiocmbit) \
1609 if (status & uartbit) \
1612 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1613 TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
1614 TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
1615 TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
1620 static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1622 struct uart_amba_port *uap =
1623 container_of(port, struct uart_amba_port, port);
1626 cr = pl011_read(uap, REG_CR);
1628 #define TIOCMBIT(tiocmbit, uartbit) \
1629 if (mctrl & tiocmbit) \
1634 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1635 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1636 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1637 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1638 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
1640 if (port->status & UPSTAT_AUTORTS) {
1641 /* We need to disable auto-RTS if we want to turn RTS off */
1642 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1646 pl011_write(cr, uap, REG_CR);
1649 static void pl011_break_ctl(struct uart_port *port, int break_state)
1651 struct uart_amba_port *uap =
1652 container_of(port, struct uart_amba_port, port);
1653 unsigned long flags;
1656 spin_lock_irqsave(&uap->port.lock, flags);
1657 lcr_h = pl011_read(uap, REG_LCRH_TX);
1658 if (break_state == -1)
1659 lcr_h |= UART01x_LCRH_BRK;
1661 lcr_h &= ~UART01x_LCRH_BRK;
1662 pl011_write(lcr_h, uap, REG_LCRH_TX);
1663 spin_unlock_irqrestore(&uap->port.lock, flags);
1666 #ifdef CONFIG_CONSOLE_POLL
1668 static void pl011_quiesce_irqs(struct uart_port *port)
1670 struct uart_amba_port *uap =
1671 container_of(port, struct uart_amba_port, port);
1673 pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
1675 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1676 * we simply mask it. start_tx() will unmask it.
1678 * Note we can race with start_tx(), and if the race happens, the
1679 * polling user might get another interrupt just after we clear it.
1680 * But it should be OK and can happen even w/o the race, e.g.
1681 * controller immediately got some new data and raised the IRQ.
1683 * And whoever uses polling routines assumes that it manages the device
1684 * (including tx queue), so we're also fine with start_tx()'s caller
1687 pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
1691 static int pl011_get_poll_char(struct uart_port *port)
1693 struct uart_amba_port *uap =
1694 container_of(port, struct uart_amba_port, port);
1695 unsigned int status;
1698 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1701 pl011_quiesce_irqs(port);
1703 status = pl011_read(uap, REG_FR);
1704 if (status & UART01x_FR_RXFE)
1705 return NO_POLL_CHAR;
1707 return pl011_read(uap, REG_DR);
1710 static void pl011_put_poll_char(struct uart_port *port,
1713 struct uart_amba_port *uap =
1714 container_of(port, struct uart_amba_port, port);
1716 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
1719 pl011_write(ch, uap, REG_DR);
1722 #endif /* CONFIG_CONSOLE_POLL */
1724 static int pl011_hwinit(struct uart_port *port)
1726 struct uart_amba_port *uap =
1727 container_of(port, struct uart_amba_port, port);
1730 /* Optionaly enable pins to be muxed in and configured */
1731 pinctrl_pm_select_default_state(port->dev);
1734 * Try to enable the clock producer.
1736 retval = clk_prepare_enable(uap->clk);
1740 uap->port.uartclk = clk_get_rate(uap->clk);
1742 /* Clear pending error and receive interrupts */
1743 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1744 UART011_FEIS | UART011_RTIS | UART011_RXIS,
1748 * Save interrupts enable mask, and enable RX interrupts in case if
1749 * the interrupt is used for NMI entry.
1751 uap->im = pl011_read(uap, REG_IMSC);
1752 pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
1754 if (dev_get_platdata(uap->port.dev)) {
1755 struct amba_pl011_data *plat;
1757 plat = dev_get_platdata(uap->port.dev);
1764 static bool pl011_split_lcrh(const struct uart_amba_port *uap)
1766 return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
1767 pl011_reg_to_offset(uap, REG_LCRH_TX);
1770 static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1772 pl011_write(lcr_h, uap, REG_LCRH_RX);
1773 if (pl011_split_lcrh(uap)) {
1776 * Wait 10 PCLKs before writing LCRH_TX register,
1777 * to get this delay write read only register 10 times
1779 for (i = 0; i < 10; ++i)
1780 pl011_write(0xff, uap, REG_MIS);
1781 pl011_write(lcr_h, uap, REG_LCRH_TX);
1785 static int pl011_allocate_irq(struct uart_amba_port *uap)
1787 pl011_write(uap->im, uap, REG_IMSC);
1789 return request_irq(uap->port.irq, pl011_int, IRQF_SHARED, "uart-pl011", uap);
1793 * Enable interrupts, only timeouts when using DMA
1794 * if initial RX DMA job failed, start in interrupt mode
1797 static void pl011_enable_interrupts(struct uart_amba_port *uap)
1799 unsigned long flags;
1802 spin_lock_irqsave(&uap->port.lock, flags);
1804 /* Clear out any spuriously appearing RX interrupts */
1805 pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
1808 * RXIS is asserted only when the RX FIFO transitions from below
1809 * to above the trigger threshold. If the RX FIFO is already
1810 * full to the threshold this can't happen and RXIS will now be
1811 * stuck off. Drain the RX FIFO explicitly to fix this:
1813 for (i = 0; i < uap->fifosize * 2; ++i) {
1814 if (pl011_read(uap, REG_FR) & UART01x_FR_RXFE)
1817 pl011_read(uap, REG_DR);
1820 uap->im = UART011_RTIM;
1821 if (!pl011_dma_rx_running(uap))
1822 uap->im |= UART011_RXIM;
1823 pl011_write(uap->im, uap, REG_IMSC);
1824 spin_unlock_irqrestore(&uap->port.lock, flags);
1827 static void pl011_unthrottle_rx(struct uart_port *port)
1829 struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port);
1830 unsigned long flags;
1832 spin_lock_irqsave(&uap->port.lock, flags);
1834 uap->im = UART011_RTIM;
1835 if (!pl011_dma_rx_running(uap))
1836 uap->im |= UART011_RXIM;
1838 pl011_write(uap->im, uap, REG_IMSC);
1840 spin_unlock_irqrestore(&uap->port.lock, flags);
1843 static int pl011_startup(struct uart_port *port)
1845 struct uart_amba_port *uap =
1846 container_of(port, struct uart_amba_port, port);
1850 retval = pl011_hwinit(port);
1854 retval = pl011_allocate_irq(uap);
1858 pl011_write(uap->vendor->ifls, uap, REG_IFLS);
1860 spin_lock_irq(&uap->port.lock);
1862 cr = pl011_read(uap, REG_CR);
1863 cr &= UART011_CR_RTS | UART011_CR_DTR;
1864 cr |= UART01x_CR_UARTEN | UART011_CR_RXE;
1866 if (!(port->rs485.flags & SER_RS485_ENABLED))
1867 cr |= UART011_CR_TXE;
1869 pl011_write(cr, uap, REG_CR);
1871 spin_unlock_irq(&uap->port.lock);
1874 * initialise the old status of the modem signals
1876 uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
1879 pl011_dma_startup(uap);
1881 pl011_enable_interrupts(uap);
1886 clk_disable_unprepare(uap->clk);
1890 static int sbsa_uart_startup(struct uart_port *port)
1892 struct uart_amba_port *uap =
1893 container_of(port, struct uart_amba_port, port);
1896 retval = pl011_hwinit(port);
1900 retval = pl011_allocate_irq(uap);
1904 /* The SBSA UART does not support any modem status lines. */
1905 uap->old_status = 0;
1907 pl011_enable_interrupts(uap);
1912 static void pl011_shutdown_channel(struct uart_amba_port *uap,
1917 val = pl011_read(uap, lcrh);
1918 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1919 pl011_write(val, uap, lcrh);
1923 * disable the port. It should not disable RTS and DTR.
1924 * Also RTS and DTR state should be preserved to restore
1925 * it during startup().
1927 static void pl011_disable_uart(struct uart_amba_port *uap)
1931 uap->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
1932 spin_lock_irq(&uap->port.lock);
1933 cr = pl011_read(uap, REG_CR);
1934 cr &= UART011_CR_RTS | UART011_CR_DTR;
1935 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1936 pl011_write(cr, uap, REG_CR);
1937 spin_unlock_irq(&uap->port.lock);
1940 * disable break condition and fifos
1942 pl011_shutdown_channel(uap, REG_LCRH_RX);
1943 if (pl011_split_lcrh(uap))
1944 pl011_shutdown_channel(uap, REG_LCRH_TX);
1947 static void pl011_disable_interrupts(struct uart_amba_port *uap)
1949 spin_lock_irq(&uap->port.lock);
1951 /* mask all interrupts and clear all pending ones */
1953 pl011_write(uap->im, uap, REG_IMSC);
1954 pl011_write(0xffff, uap, REG_ICR);
1956 spin_unlock_irq(&uap->port.lock);
1959 static void pl011_shutdown(struct uart_port *port)
1961 struct uart_amba_port *uap =
1962 container_of(port, struct uart_amba_port, port);
1964 pl011_disable_interrupts(uap);
1966 pl011_dma_shutdown(uap);
1968 if ((port->rs485.flags & SER_RS485_ENABLED) && uap->rs485_tx_started)
1969 pl011_rs485_tx_stop(uap);
1971 free_irq(uap->port.irq, uap);
1973 pl011_disable_uart(uap);
1976 * Shut down the clock producer
1978 clk_disable_unprepare(uap->clk);
1979 /* Optionally let pins go into sleep states */
1980 pinctrl_pm_select_sleep_state(port->dev);
1982 if (dev_get_platdata(uap->port.dev)) {
1983 struct amba_pl011_data *plat;
1985 plat = dev_get_platdata(uap->port.dev);
1990 if (uap->port.ops->flush_buffer)
1991 uap->port.ops->flush_buffer(port);
1994 static void sbsa_uart_shutdown(struct uart_port *port)
1996 struct uart_amba_port *uap =
1997 container_of(port, struct uart_amba_port, port);
1999 pl011_disable_interrupts(uap);
2001 free_irq(uap->port.irq, uap);
2003 if (uap->port.ops->flush_buffer)
2004 uap->port.ops->flush_buffer(port);
2008 pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
2010 port->read_status_mask = UART011_DR_OE | 255;
2011 if (termios->c_iflag & INPCK)
2012 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
2013 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2014 port->read_status_mask |= UART011_DR_BE;
2017 * Characters to ignore
2019 port->ignore_status_mask = 0;
2020 if (termios->c_iflag & IGNPAR)
2021 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
2022 if (termios->c_iflag & IGNBRK) {
2023 port->ignore_status_mask |= UART011_DR_BE;
2025 * If we're ignoring parity and break indicators,
2026 * ignore overruns too (for real raw support).
2028 if (termios->c_iflag & IGNPAR)
2029 port->ignore_status_mask |= UART011_DR_OE;
2033 * Ignore all characters if CREAD is not set.
2035 if ((termios->c_cflag & CREAD) == 0)
2036 port->ignore_status_mask |= UART_DUMMY_DR_RX;
2040 pl011_set_termios(struct uart_port *port, struct ktermios *termios,
2041 const struct ktermios *old)
2043 struct uart_amba_port *uap =
2044 container_of(port, struct uart_amba_port, port);
2045 unsigned int lcr_h, old_cr;
2046 unsigned long flags;
2047 unsigned int baud, quot, clkdiv;
2050 if (uap->vendor->oversampling)
2056 * Ask the core to calculate the divisor for us.
2058 baud = uart_get_baud_rate(port, termios, old, 0,
2059 port->uartclk / clkdiv);
2060 #ifdef CONFIG_DMA_ENGINE
2062 * Adjust RX DMA polling rate with baud rate if not specified.
2064 if (uap->dmarx.auto_poll_rate)
2065 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
2068 if (baud > port->uartclk/16)
2069 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
2071 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
2073 switch (termios->c_cflag & CSIZE) {
2075 lcr_h = UART01x_LCRH_WLEN_5;
2078 lcr_h = UART01x_LCRH_WLEN_6;
2081 lcr_h = UART01x_LCRH_WLEN_7;
2084 lcr_h = UART01x_LCRH_WLEN_8;
2087 if (termios->c_cflag & CSTOPB)
2088 lcr_h |= UART01x_LCRH_STP2;
2089 if (termios->c_cflag & PARENB) {
2090 lcr_h |= UART01x_LCRH_PEN;
2091 if (!(termios->c_cflag & PARODD))
2092 lcr_h |= UART01x_LCRH_EPS;
2093 if (termios->c_cflag & CMSPAR)
2094 lcr_h |= UART011_LCRH_SPS;
2096 if (uap->fifosize > 1)
2097 lcr_h |= UART01x_LCRH_FEN;
2099 bits = tty_get_frame_size(termios->c_cflag);
2101 spin_lock_irqsave(&port->lock, flags);
2104 * Update the per-port timeout.
2106 uart_update_timeout(port, termios->c_cflag, baud);
2109 * Calculate the approximated time it takes to transmit one character
2110 * with the given baud rate. We use this as the poll interval when we
2111 * wait for the tx queue to empty.
2113 uap->rs485_tx_drain_interval = DIV_ROUND_UP(bits * 1000 * 1000, baud);
2115 pl011_setup_status_masks(port, termios);
2117 if (UART_ENABLE_MS(port, termios->c_cflag))
2118 pl011_enable_ms(port);
2120 if (port->rs485.flags & SER_RS485_ENABLED)
2121 termios->c_cflag &= ~CRTSCTS;
2123 old_cr = pl011_read(uap, REG_CR);
2125 if (termios->c_cflag & CRTSCTS) {
2126 if (old_cr & UART011_CR_RTS)
2127 old_cr |= UART011_CR_RTSEN;
2129 old_cr |= UART011_CR_CTSEN;
2130 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
2132 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
2133 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
2136 if (uap->vendor->oversampling) {
2137 if (baud > port->uartclk / 16)
2138 old_cr |= ST_UART011_CR_OVSFACT;
2140 old_cr &= ~ST_UART011_CR_OVSFACT;
2144 * Workaround for the ST Micro oversampling variants to
2145 * increase the bitrate slightly, by lowering the divisor,
2146 * to avoid delayed sampling of start bit at high speeds,
2147 * else we see data corruption.
2149 if (uap->vendor->oversampling) {
2150 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
2152 else if ((baud > 3250000) && (quot > 2))
2156 pl011_write(quot & 0x3f, uap, REG_FBRD);
2157 pl011_write(quot >> 6, uap, REG_IBRD);
2160 * ----------v----------v----------v----------v-----
2161 * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
2162 * REG_FBRD & REG_IBRD.
2163 * ----------^----------^----------^----------^-----
2165 pl011_write_lcr_h(uap, lcr_h);
2168 * Receive was disabled by pl011_disable_uart during shutdown.
2169 * Need to reenable receive if you need to use a tty_driver
2170 * returns from tty_find_polling_driver() after a port shutdown.
2172 old_cr |= UART011_CR_RXE;
2173 pl011_write(old_cr, uap, REG_CR);
2175 spin_unlock_irqrestore(&port->lock, flags);
2179 sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
2180 const struct ktermios *old)
2182 struct uart_amba_port *uap =
2183 container_of(port, struct uart_amba_port, port);
2184 unsigned long flags;
2186 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
2188 /* The SBSA UART only supports 8n1 without hardware flow control. */
2189 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
2190 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
2191 termios->c_cflag |= CS8 | CLOCAL;
2193 spin_lock_irqsave(&port->lock, flags);
2194 uart_update_timeout(port, CS8, uap->fixed_baud);
2195 pl011_setup_status_masks(port, termios);
2196 spin_unlock_irqrestore(&port->lock, flags);
2199 static const char *pl011_type(struct uart_port *port)
2201 struct uart_amba_port *uap =
2202 container_of(port, struct uart_amba_port, port);
2203 return uap->port.type == PORT_AMBA ? uap->type : NULL;
2207 * Configure/autoconfigure the port.
2209 static void pl011_config_port(struct uart_port *port, int flags)
2211 if (flags & UART_CONFIG_TYPE)
2212 port->type = PORT_AMBA;
2216 * verify the new serial_struct (for TIOCSSERIAL).
2218 static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
2221 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2223 if (ser->irq < 0 || ser->irq >= nr_irqs)
2225 if (ser->baud_base < 9600)
2227 if (port->mapbase != (unsigned long) ser->iomem_base)
2232 static int pl011_rs485_config(struct uart_port *port, struct ktermios *termios,
2233 struct serial_rs485 *rs485)
2235 struct uart_amba_port *uap =
2236 container_of(port, struct uart_amba_port, port);
2238 if (port->rs485.flags & SER_RS485_ENABLED)
2239 pl011_rs485_tx_stop(uap);
2241 /* Make sure auto RTS is disabled */
2242 if (rs485->flags & SER_RS485_ENABLED) {
2243 u32 cr = pl011_read(uap, REG_CR);
2245 cr &= ~UART011_CR_RTSEN;
2246 pl011_write(cr, uap, REG_CR);
2247 port->status &= ~UPSTAT_AUTORTS;
2253 static const struct uart_ops amba_pl011_pops = {
2254 .tx_empty = pl011_tx_empty,
2255 .set_mctrl = pl011_set_mctrl,
2256 .get_mctrl = pl011_get_mctrl,
2257 .stop_tx = pl011_stop_tx,
2258 .start_tx = pl011_start_tx,
2259 .stop_rx = pl011_stop_rx,
2260 .throttle = pl011_throttle_rx,
2261 .unthrottle = pl011_unthrottle_rx,
2262 .enable_ms = pl011_enable_ms,
2263 .break_ctl = pl011_break_ctl,
2264 .startup = pl011_startup,
2265 .shutdown = pl011_shutdown,
2266 .flush_buffer = pl011_dma_flush_buffer,
2267 .set_termios = pl011_set_termios,
2269 .config_port = pl011_config_port,
2270 .verify_port = pl011_verify_port,
2271 #ifdef CONFIG_CONSOLE_POLL
2272 .poll_init = pl011_hwinit,
2273 .poll_get_char = pl011_get_poll_char,
2274 .poll_put_char = pl011_put_poll_char,
2278 static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2282 static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2287 static const struct uart_ops sbsa_uart_pops = {
2288 .tx_empty = pl011_tx_empty,
2289 .set_mctrl = sbsa_uart_set_mctrl,
2290 .get_mctrl = sbsa_uart_get_mctrl,
2291 .stop_tx = pl011_stop_tx,
2292 .start_tx = pl011_start_tx,
2293 .stop_rx = pl011_stop_rx,
2294 .startup = sbsa_uart_startup,
2295 .shutdown = sbsa_uart_shutdown,
2296 .set_termios = sbsa_uart_set_termios,
2298 .config_port = pl011_config_port,
2299 .verify_port = pl011_verify_port,
2300 #ifdef CONFIG_CONSOLE_POLL
2301 .poll_init = pl011_hwinit,
2302 .poll_get_char = pl011_get_poll_char,
2303 .poll_put_char = pl011_put_poll_char,
2307 static struct uart_amba_port *amba_ports[UART_NR];
2309 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2311 static void pl011_console_putchar(struct uart_port *port, unsigned char ch)
2313 struct uart_amba_port *uap =
2314 container_of(port, struct uart_amba_port, port);
2316 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
2318 pl011_write(ch, uap, REG_DR);
2322 pl011_console_write(struct console *co, const char *s, unsigned int count)
2324 struct uart_amba_port *uap = amba_ports[co->index];
2325 unsigned int old_cr = 0, new_cr;
2326 unsigned long flags;
2329 clk_enable(uap->clk);
2331 local_irq_save(flags);
2332 if (uap->port.sysrq)
2334 else if (oops_in_progress)
2335 locked = spin_trylock(&uap->port.lock);
2337 spin_lock(&uap->port.lock);
2340 * First save the CR then disable the interrupts
2342 if (!uap->vendor->always_enabled) {
2343 old_cr = pl011_read(uap, REG_CR);
2344 new_cr = old_cr & ~UART011_CR_CTSEN;
2345 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
2346 pl011_write(new_cr, uap, REG_CR);
2349 uart_console_write(&uap->port, s, count, pl011_console_putchar);
2352 * Finally, wait for transmitter to become empty and restore the
2353 * TCR. Allow feature register bits to be inverted to work around
2356 while ((pl011_read(uap, REG_FR) ^ uap->vendor->inv_fr)
2357 & uap->vendor->fr_busy)
2359 if (!uap->vendor->always_enabled)
2360 pl011_write(old_cr, uap, REG_CR);
2363 spin_unlock(&uap->port.lock);
2364 local_irq_restore(flags);
2366 clk_disable(uap->clk);
2369 static void pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2370 int *parity, int *bits)
2372 if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
2373 unsigned int lcr_h, ibrd, fbrd;
2375 lcr_h = pl011_read(uap, REG_LCRH_TX);
2378 if (lcr_h & UART01x_LCRH_PEN) {
2379 if (lcr_h & UART01x_LCRH_EPS)
2385 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2390 ibrd = pl011_read(uap, REG_IBRD);
2391 fbrd = pl011_read(uap, REG_FBRD);
2393 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
2395 if (uap->vendor->oversampling) {
2396 if (pl011_read(uap, REG_CR)
2397 & ST_UART011_CR_OVSFACT)
2403 static int pl011_console_setup(struct console *co, char *options)
2405 struct uart_amba_port *uap;
2413 * Check whether an invalid uart number has been specified, and
2414 * if so, search for the first available port that does have
2417 if (co->index >= UART_NR)
2419 uap = amba_ports[co->index];
2423 /* Allow pins to be muxed in and configured */
2424 pinctrl_pm_select_default_state(uap->port.dev);
2426 ret = clk_prepare(uap->clk);
2430 if (dev_get_platdata(uap->port.dev)) {
2431 struct amba_pl011_data *plat;
2433 plat = dev_get_platdata(uap->port.dev);
2438 uap->port.uartclk = clk_get_rate(uap->clk);
2440 if (uap->vendor->fixed_options) {
2441 baud = uap->fixed_baud;
2444 uart_parse_options(options,
2445 &baud, &parity, &bits, &flow);
2447 pl011_console_get_options(uap, &baud, &parity, &bits);
2450 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2454 * pl011_console_match - non-standard console matching
2455 * @co: registering console
2456 * @name: name from console command line
2457 * @idx: index from console command line
2458 * @options: ptr to option string from console command line
2460 * Only attempts to match console command lines of the form:
2461 * console=pl011,mmio|mmio32,<addr>[,<options>]
2462 * console=pl011,0x<addr>[,<options>]
2463 * This form is used to register an initial earlycon boot console and
2464 * replace it with the amba_console at pl011 driver init.
2466 * Performs console setup for a match (as required by interface)
2467 * If no <options> are specified, then assume the h/w is already setup.
2469 * Returns 0 if console matches; otherwise non-zero to use default matching
2471 static int pl011_console_match(struct console *co, char *name, int idx,
2474 unsigned char iotype;
2475 resource_size_t addr;
2479 * Systems affected by the Qualcomm Technologies QDF2400 E44 erratum
2480 * have a distinct console name, so make sure we check for that.
2481 * The actual implementation of the erratum occurs in the probe
2484 if ((strcmp(name, "qdf2400_e44") != 0) && (strcmp(name, "pl011") != 0))
2487 if (uart_parse_earlycon(options, &iotype, &addr, &options))
2490 if (iotype != UPIO_MEM && iotype != UPIO_MEM32)
2493 /* try to match the port specified on the command line */
2494 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2495 struct uart_port *port;
2500 port = &amba_ports[i]->port;
2502 if (port->mapbase != addr)
2507 return pl011_console_setup(co, options);
2513 static struct uart_driver amba_reg;
2514 static struct console amba_console = {
2516 .write = pl011_console_write,
2517 .device = uart_console_device,
2518 .setup = pl011_console_setup,
2519 .match = pl011_console_match,
2520 .flags = CON_PRINTBUFFER | CON_ANYTIME,
2525 #define AMBA_CONSOLE (&amba_console)
2527 static void qdf2400_e44_putc(struct uart_port *port, unsigned char c)
2529 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
2531 writel(c, port->membase + UART01x_DR);
2532 while (!(readl(port->membase + UART01x_FR) & UART011_FR_TXFE))
2536 static void qdf2400_e44_early_write(struct console *con, const char *s, unsigned n)
2538 struct earlycon_device *dev = con->data;
2540 uart_console_write(&dev->port, s, n, qdf2400_e44_putc);
2543 static void pl011_putc(struct uart_port *port, unsigned char c)
2545 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
2547 if (port->iotype == UPIO_MEM32)
2548 writel(c, port->membase + UART01x_DR);
2550 writeb(c, port->membase + UART01x_DR);
2551 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
2555 static void pl011_early_write(struct console *con, const char *s, unsigned n)
2557 struct earlycon_device *dev = con->data;
2559 uart_console_write(&dev->port, s, n, pl011_putc);
2562 #ifdef CONFIG_CONSOLE_POLL
2563 static int pl011_getc(struct uart_port *port)
2565 if (readl(port->membase + UART01x_FR) & UART01x_FR_RXFE)
2566 return NO_POLL_CHAR;
2568 if (port->iotype == UPIO_MEM32)
2569 return readl(port->membase + UART01x_DR);
2571 return readb(port->membase + UART01x_DR);
2574 static int pl011_early_read(struct console *con, char *s, unsigned int n)
2576 struct earlycon_device *dev = con->data;
2577 int ch, num_read = 0;
2579 while (num_read < n) {
2580 ch = pl011_getc(&dev->port);
2581 if (ch == NO_POLL_CHAR)
2590 #define pl011_early_read NULL
2594 * On non-ACPI systems, earlycon is enabled by specifying
2595 * "earlycon=pl011,<address>" on the kernel command line.
2597 * On ACPI ARM64 systems, an "early" console is enabled via the SPCR table,
2598 * by specifying only "earlycon" on the command line. Because it requires
2599 * SPCR, the console starts after ACPI is parsed, which is later than a
2600 * traditional early console.
2602 * To get the traditional early console that starts before ACPI is parsed,
2603 * specify the full "earlycon=pl011,<address>" option.
2605 static int __init pl011_early_console_setup(struct earlycon_device *device,
2608 if (!device->port.membase)
2611 device->con->write = pl011_early_write;
2612 device->con->read = pl011_early_read;
2616 OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
2617 OF_EARLYCON_DECLARE(pl011, "arm,sbsa-uart", pl011_early_console_setup);
2620 * On Qualcomm Datacenter Technologies QDF2400 SOCs affected by
2621 * Erratum 44, traditional earlycon can be enabled by specifying
2622 * "earlycon=qdf2400_e44,<address>". Any options are ignored.
2624 * Alternatively, you can just specify "earlycon", and the early console
2625 * will be enabled with the information from the SPCR table. In this
2626 * case, the SPCR code will detect the need for the E44 work-around,
2627 * and set the console name to "qdf2400_e44".
2630 qdf2400_e44_early_console_setup(struct earlycon_device *device,
2633 if (!device->port.membase)
2636 device->con->write = qdf2400_e44_early_write;
2639 EARLYCON_DECLARE(qdf2400_e44, qdf2400_e44_early_console_setup);
2642 #define AMBA_CONSOLE NULL
2645 static struct uart_driver amba_reg = {
2646 .owner = THIS_MODULE,
2647 .driver_name = "ttyAMA",
2648 .dev_name = "ttyAMA",
2649 .major = SERIAL_AMBA_MAJOR,
2650 .minor = SERIAL_AMBA_MINOR,
2652 .cons = AMBA_CONSOLE,
2655 static int pl011_probe_dt_alias(int index, struct device *dev)
2657 struct device_node *np;
2658 static bool seen_dev_with_alias = false;
2659 static bool seen_dev_without_alias = false;
2662 if (!IS_ENABLED(CONFIG_OF))
2669 ret = of_alias_get_id(np, "serial");
2671 seen_dev_without_alias = true;
2674 seen_dev_with_alias = true;
2675 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2676 dev_warn(dev, "requested serial port %d not available.\n", ret);
2681 if (seen_dev_with_alias && seen_dev_without_alias)
2682 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2687 /* unregisters the driver also if no more ports are left */
2688 static void pl011_unregister_port(struct uart_amba_port *uap)
2693 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2694 if (amba_ports[i] == uap)
2695 amba_ports[i] = NULL;
2696 else if (amba_ports[i])
2699 pl011_dma_remove(uap);
2701 uart_unregister_driver(&amba_reg);
2704 static int pl011_find_free_port(void)
2708 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2709 if (amba_ports[i] == NULL)
2715 static int pl011_get_rs485_mode(struct uart_amba_port *uap)
2717 struct uart_port *port = &uap->port;
2720 ret = uart_get_rs485_mode(port);
2727 static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2728 struct resource *mmiobase, int index)
2733 base = devm_ioremap_resource(dev, mmiobase);
2735 return PTR_ERR(base);
2737 index = pl011_probe_dt_alias(index, dev);
2739 uap->port.dev = dev;
2740 uap->port.mapbase = mmiobase->start;
2741 uap->port.membase = base;
2742 uap->port.fifosize = uap->fifosize;
2743 uap->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_AMBA_PL011_CONSOLE);
2744 uap->port.flags = UPF_BOOT_AUTOCONF;
2745 uap->port.line = index;
2747 ret = pl011_get_rs485_mode(uap);
2751 amba_ports[index] = uap;
2756 static int pl011_register_port(struct uart_amba_port *uap)
2760 /* Ensure interrupts from this UART are masked and cleared */
2761 pl011_write(0, uap, REG_IMSC);
2762 pl011_write(0xffff, uap, REG_ICR);
2764 if (!amba_reg.state) {
2765 ret = uart_register_driver(&amba_reg);
2767 dev_err(uap->port.dev,
2768 "Failed to register AMBA-PL011 driver\n");
2769 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2770 if (amba_ports[i] == uap)
2771 amba_ports[i] = NULL;
2776 ret = uart_add_one_port(&amba_reg, &uap->port);
2778 pl011_unregister_port(uap);
2783 static const struct serial_rs485 pl011_rs485_supported = {
2784 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
2785 SER_RS485_RX_DURING_TX,
2786 .delay_rts_before_send = 1,
2787 .delay_rts_after_send = 1,
2790 static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2792 struct uart_amba_port *uap;
2793 struct vendor_data *vendor = id->data;
2797 portnr = pl011_find_free_port();
2801 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2806 uap->clk = devm_clk_get(&dev->dev, NULL);
2807 if (IS_ERR(uap->clk))
2808 return PTR_ERR(uap->clk);
2810 uap->reg_offset = vendor->reg_offset;
2811 uap->vendor = vendor;
2812 uap->fifosize = vendor->get_fifosize(dev);
2813 uap->port.iotype = vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
2814 uap->port.irq = dev->irq[0];
2815 uap->port.ops = &amba_pl011_pops;
2816 uap->port.rs485_config = pl011_rs485_config;
2817 uap->port.rs485_supported = pl011_rs485_supported;
2818 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2820 if (device_property_read_u32(&dev->dev, "reg-io-width", &val) == 0) {
2823 uap->port.iotype = UPIO_MEM;
2826 uap->port.iotype = UPIO_MEM32;
2829 dev_warn(&dev->dev, "unsupported reg-io-width (%d)\n",
2835 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2839 amba_set_drvdata(dev, uap);
2841 return pl011_register_port(uap);
2844 static void pl011_remove(struct amba_device *dev)
2846 struct uart_amba_port *uap = amba_get_drvdata(dev);
2848 uart_remove_one_port(&amba_reg, &uap->port);
2849 pl011_unregister_port(uap);
2852 #ifdef CONFIG_PM_SLEEP
2853 static int pl011_suspend(struct device *dev)
2855 struct uart_amba_port *uap = dev_get_drvdata(dev);
2860 return uart_suspend_port(&amba_reg, &uap->port);
2863 static int pl011_resume(struct device *dev)
2865 struct uart_amba_port *uap = dev_get_drvdata(dev);
2870 return uart_resume_port(&amba_reg, &uap->port);
2874 static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2876 static int sbsa_uart_probe(struct platform_device *pdev)
2878 struct uart_amba_port *uap;
2884 * Check the mandatory baud rate parameter in the DT node early
2885 * so that we can easily exit with the error.
2887 if (pdev->dev.of_node) {
2888 struct device_node *np = pdev->dev.of_node;
2890 ret = of_property_read_u32(np, "current-speed", &baudrate);
2897 portnr = pl011_find_free_port();
2901 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2906 ret = platform_get_irq(pdev, 0);
2909 uap->port.irq = ret;
2911 #ifdef CONFIG_ACPI_SPCR_TABLE
2912 if (qdf2400_e44_present) {
2913 dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n");
2914 uap->vendor = &vendor_qdt_qdf2400_e44;
2917 uap->vendor = &vendor_sbsa;
2919 uap->reg_offset = uap->vendor->reg_offset;
2921 uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
2922 uap->port.ops = &sbsa_uart_pops;
2923 uap->fixed_baud = baudrate;
2925 snprintf(uap->type, sizeof(uap->type), "SBSA");
2927 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2929 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2933 platform_set_drvdata(pdev, uap);
2935 return pl011_register_port(uap);
2938 static int sbsa_uart_remove(struct platform_device *pdev)
2940 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2942 uart_remove_one_port(&amba_reg, &uap->port);
2943 pl011_unregister_port(uap);
2947 static const struct of_device_id sbsa_uart_of_match[] = {
2948 { .compatible = "arm,sbsa-uart", },
2951 MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2953 static const struct acpi_device_id __maybe_unused sbsa_uart_acpi_match[] = {
2958 MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2960 static struct platform_driver arm_sbsa_uart_platform_driver = {
2961 .probe = sbsa_uart_probe,
2962 .remove = sbsa_uart_remove,
2964 .name = "sbsa-uart",
2965 .pm = &pl011_dev_pm_ops,
2966 .of_match_table = of_match_ptr(sbsa_uart_of_match),
2967 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
2968 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
2972 static const struct amba_id pl011_ids[] = {
2976 .data = &vendor_arm,
2986 MODULE_DEVICE_TABLE(amba, pl011_ids);
2988 static struct amba_driver pl011_driver = {
2990 .name = "uart-pl011",
2991 .pm = &pl011_dev_pm_ops,
2992 .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_AMBA_PL011),
2994 .id_table = pl011_ids,
2995 .probe = pl011_probe,
2996 .remove = pl011_remove,
2999 static int __init pl011_init(void)
3001 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
3003 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
3004 pr_warn("could not register SBSA UART platform driver\n");
3005 return amba_driver_register(&pl011_driver);
3008 static void __exit pl011_exit(void)
3010 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
3011 amba_driver_unregister(&pl011_driver);
3015 * While this can be a module, if builtin it's most likely the console
3016 * So let's leave module_exit but move module_init to an earlier place
3018 arch_initcall(pl011_init);
3019 module_exit(pl011_exit);
3021 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
3022 MODULE_DESCRIPTION("ARM AMBA serial port driver");
3023 MODULE_LICENSE("GPL");