2 * Driver for AMBA serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 * Copyright (C) 2010 ST-Ericsson SA
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.
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.
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
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.
33 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
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>
57 #include <linux/of_device.h>
58 #include <linux/pinctrl/consumer.h>
59 #include <linux/sizes.h>
61 #include <linux/acpi.h>
63 #include "amba-pl011.h"
67 #define SERIAL_AMBA_MAJOR 204
68 #define SERIAL_AMBA_MINOR 64
69 #define SERIAL_AMBA_NR UART_NR
71 #define AMBA_ISR_PASS_LIMIT 256
73 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
74 #define UART_DUMMY_DR_RX (1 << 16)
76 static u16 pl011_std_offsets
[REG_ARRAY_SIZE
] = {
77 [REG_DR
] = UART01x_DR
,
78 [REG_ST_DMAWM
] = ST_UART011_DMAWM
,
79 [REG_ST_TIMEOUT
] = ST_UART011_TIMEOUT
,
80 [REG_FR
] = UART01x_FR
,
81 [REG_ST_LCRH_RX
] = ST_UART011_LCRH_RX
,
82 [REG_IBRD
] = UART011_IBRD
,
83 [REG_FBRD
] = UART011_FBRD
,
84 [REG_LCRH
] = UART011_LCRH
,
85 [REG_ST_LCRH_TX
] = ST_UART011_LCRH_TX
,
86 [REG_CR
] = UART011_CR
,
87 [REG_IFLS
] = UART011_IFLS
,
88 [REG_IMSC
] = UART011_IMSC
,
89 [REG_RIS
] = UART011_RIS
,
90 [REG_MIS
] = UART011_MIS
,
91 [REG_ICR
] = UART011_ICR
,
92 [REG_DMACR
] = UART011_DMACR
,
93 [REG_ST_XFCR
] = ST_UART011_XFCR
,
94 [REG_ST_XON1
] = ST_UART011_XON1
,
95 [REG_ST_XON2
] = ST_UART011_XON2
,
96 [REG_ST_XOFF1
] = ST_UART011_XOFF1
,
97 [REG_ST_XOFF2
] = ST_UART011_XOFF2
,
98 [REG_ST_ITCR
] = ST_UART011_ITCR
,
99 [REG_ST_ITIP
] = ST_UART011_ITIP
,
100 [REG_ST_ABCR
] = ST_UART011_ABCR
,
101 [REG_ST_ABIMSC
] = ST_UART011_ABIMSC
,
104 /* There is by now at least one vendor with differing details, so handle it */
107 unsigned int lcrh_tx
;
108 unsigned int lcrh_rx
;
111 bool cts_event_workaround
;
115 unsigned int (*get_fifosize
)(struct amba_device
*dev
);
118 static unsigned int get_fifosize_arm(struct amba_device
*dev
)
120 return amba_rev(dev
) < 3 ? 16 : 32;
123 static struct vendor_data vendor_arm
= {
124 .ifls
= UART011_IFLS_RX4_8
|UART011_IFLS_TX4_8
,
127 .oversampling
= false,
128 .dma_threshold
= false,
129 .cts_event_workaround
= false,
130 .always_enabled
= false,
131 .fixed_options
= false,
132 .get_fifosize
= get_fifosize_arm
,
135 static struct vendor_data vendor_sbsa
= {
136 .oversampling
= false,
137 .dma_threshold
= false,
138 .cts_event_workaround
= false,
139 .always_enabled
= true,
140 .fixed_options
= true,
143 static unsigned int get_fifosize_st(struct amba_device
*dev
)
148 static struct vendor_data vendor_st
= {
149 .ifls
= UART011_IFLS_RX_HALF
|UART011_IFLS_TX_HALF
,
150 .lcrh_tx
= REG_ST_LCRH_TX
,
151 .lcrh_rx
= REG_ST_LCRH_RX
,
152 .oversampling
= true,
153 .dma_threshold
= true,
154 .cts_event_workaround
= true,
155 .always_enabled
= false,
156 .fixed_options
= false,
157 .get_fifosize
= get_fifosize_st
,
160 /* Deals with DMA transactions */
163 struct scatterlist sg
;
167 struct pl011_dmarx_data
{
168 struct dma_chan
*chan
;
169 struct completion complete
;
171 struct pl011_sgbuf sgbuf_a
;
172 struct pl011_sgbuf sgbuf_b
;
175 struct timer_list timer
;
176 unsigned int last_residue
;
177 unsigned long last_jiffies
;
179 unsigned int poll_rate
;
180 unsigned int poll_timeout
;
183 struct pl011_dmatx_data
{
184 struct dma_chan
*chan
;
185 struct scatterlist sg
;
191 * We wrap our port structure around the generic uart_port.
193 struct uart_amba_port
{
194 struct uart_port port
;
195 const u16
*reg_offset
;
197 const struct vendor_data
*vendor
;
198 unsigned int dmacr
; /* dma control reg */
199 unsigned int im
; /* interrupt mask */
200 unsigned int old_status
;
201 unsigned int fifosize
; /* vendor-specific */
202 unsigned int lcrh_tx
; /* vendor-specific */
203 unsigned int lcrh_rx
; /* vendor-specific */
204 unsigned int old_cr
; /* state during shutdown */
206 unsigned int fixed_baud
; /* vendor-set fixed baud rate */
208 #ifdef CONFIG_DMA_ENGINE
212 struct pl011_dmarx_data dmarx
;
213 struct pl011_dmatx_data dmatx
;
218 static unsigned int pl011_reg_to_offset(const struct uart_amba_port
*uap
,
221 return uap
->reg_offset
[reg
];
224 static unsigned int pl011_read(const struct uart_amba_port
*uap
,
227 return readw(uap
->port
.membase
+ pl011_reg_to_offset(uap
, reg
));
230 static void pl011_write(unsigned int val
, const struct uart_amba_port
*uap
,
233 writew(val
, uap
->port
.membase
+ pl011_reg_to_offset(uap
, reg
));
237 * Reads up to 256 characters from the FIFO or until it's empty and
238 * inserts them into the TTY layer. Returns the number of characters
239 * read from the FIFO.
241 static int pl011_fifo_to_tty(struct uart_amba_port
*uap
)
244 unsigned int ch
, flag
, max_count
= 256;
247 while (max_count
--) {
248 status
= pl011_read(uap
, REG_FR
);
249 if (status
& UART01x_FR_RXFE
)
252 /* Take chars from the FIFO and update status */
253 ch
= pl011_read(uap
, REG_DR
) | UART_DUMMY_DR_RX
;
255 uap
->port
.icount
.rx
++;
258 if (unlikely(ch
& UART_DR_ERROR
)) {
259 if (ch
& UART011_DR_BE
) {
260 ch
&= ~(UART011_DR_FE
| UART011_DR_PE
);
261 uap
->port
.icount
.brk
++;
262 if (uart_handle_break(&uap
->port
))
264 } else if (ch
& UART011_DR_PE
)
265 uap
->port
.icount
.parity
++;
266 else if (ch
& UART011_DR_FE
)
267 uap
->port
.icount
.frame
++;
268 if (ch
& UART011_DR_OE
)
269 uap
->port
.icount
.overrun
++;
271 ch
&= uap
->port
.read_status_mask
;
273 if (ch
& UART011_DR_BE
)
275 else if (ch
& UART011_DR_PE
)
277 else if (ch
& UART011_DR_FE
)
281 if (uart_handle_sysrq_char(&uap
->port
, ch
& 255))
284 uart_insert_char(&uap
->port
, ch
, UART011_DR_OE
, ch
, flag
);
292 * All the DMA operation mode stuff goes inside this ifdef.
293 * This assumes that you have a generic DMA device interface,
294 * no custom DMA interfaces are supported.
296 #ifdef CONFIG_DMA_ENGINE
298 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
300 static int pl011_sgbuf_init(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
301 enum dma_data_direction dir
)
305 sg
->buf
= dma_alloc_coherent(chan
->device
->dev
,
306 PL011_DMA_BUFFER_SIZE
, &dma_addr
, GFP_KERNEL
);
310 sg_init_table(&sg
->sg
, 1);
311 sg_set_page(&sg
->sg
, phys_to_page(dma_addr
),
312 PL011_DMA_BUFFER_SIZE
, offset_in_page(dma_addr
));
313 sg_dma_address(&sg
->sg
) = dma_addr
;
314 sg_dma_len(&sg
->sg
) = PL011_DMA_BUFFER_SIZE
;
319 static void pl011_sgbuf_free(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
320 enum dma_data_direction dir
)
323 dma_free_coherent(chan
->device
->dev
,
324 PL011_DMA_BUFFER_SIZE
, sg
->buf
,
325 sg_dma_address(&sg
->sg
));
329 static void pl011_dma_probe(struct uart_amba_port
*uap
)
331 /* DMA is the sole user of the platform data right now */
332 struct amba_pl011_data
*plat
= dev_get_platdata(uap
->port
.dev
);
333 struct device
*dev
= uap
->port
.dev
;
334 struct dma_slave_config tx_conf
= {
335 .dst_addr
= uap
->port
.mapbase
+
336 pl011_reg_to_offset(uap
, REG_DR
),
337 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
338 .direction
= DMA_MEM_TO_DEV
,
339 .dst_maxburst
= uap
->fifosize
>> 1,
342 struct dma_chan
*chan
;
345 uap
->dma_probed
= true;
346 chan
= dma_request_slave_channel_reason(dev
, "tx");
348 if (PTR_ERR(chan
) == -EPROBE_DEFER
) {
349 uap
->dma_probed
= false;
353 /* We need platform data */
354 if (!plat
|| !plat
->dma_filter
) {
355 dev_info(uap
->port
.dev
, "no DMA platform data\n");
359 /* Try to acquire a generic DMA engine slave TX channel */
361 dma_cap_set(DMA_SLAVE
, mask
);
363 chan
= dma_request_channel(mask
, plat
->dma_filter
,
366 dev_err(uap
->port
.dev
, "no TX DMA channel!\n");
371 dmaengine_slave_config(chan
, &tx_conf
);
372 uap
->dmatx
.chan
= chan
;
374 dev_info(uap
->port
.dev
, "DMA channel TX %s\n",
375 dma_chan_name(uap
->dmatx
.chan
));
377 /* Optionally make use of an RX channel as well */
378 chan
= dma_request_slave_channel(dev
, "rx");
380 if (!chan
&& plat
->dma_rx_param
) {
381 chan
= dma_request_channel(mask
, plat
->dma_filter
, plat
->dma_rx_param
);
384 dev_err(uap
->port
.dev
, "no RX DMA channel!\n");
390 struct dma_slave_config rx_conf
= {
391 .src_addr
= uap
->port
.mapbase
+
392 pl011_reg_to_offset(uap
, REG_DR
),
393 .src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
394 .direction
= DMA_DEV_TO_MEM
,
395 .src_maxburst
= uap
->fifosize
>> 2,
398 struct dma_slave_caps caps
;
401 * Some DMA controllers provide information on their capabilities.
402 * If the controller does, check for suitable residue processing
403 * otherwise assime all is well.
405 if (0 == dma_get_slave_caps(chan
, &caps
)) {
406 if (caps
.residue_granularity
==
407 DMA_RESIDUE_GRANULARITY_DESCRIPTOR
) {
408 dma_release_channel(chan
);
409 dev_info(uap
->port
.dev
,
410 "RX DMA disabled - no residue processing\n");
414 dmaengine_slave_config(chan
, &rx_conf
);
415 uap
->dmarx
.chan
= chan
;
417 uap
->dmarx
.auto_poll_rate
= false;
418 if (plat
&& plat
->dma_rx_poll_enable
) {
419 /* Set poll rate if specified. */
420 if (plat
->dma_rx_poll_rate
) {
421 uap
->dmarx
.auto_poll_rate
= false;
422 uap
->dmarx
.poll_rate
= plat
->dma_rx_poll_rate
;
425 * 100 ms defaults to poll rate if not
426 * specified. This will be adjusted with
427 * the baud rate at set_termios.
429 uap
->dmarx
.auto_poll_rate
= true;
430 uap
->dmarx
.poll_rate
= 100;
432 /* 3 secs defaults poll_timeout if not specified. */
433 if (plat
->dma_rx_poll_timeout
)
434 uap
->dmarx
.poll_timeout
=
435 plat
->dma_rx_poll_timeout
;
437 uap
->dmarx
.poll_timeout
= 3000;
438 } else if (!plat
&& dev
->of_node
) {
439 uap
->dmarx
.auto_poll_rate
= of_property_read_bool(
440 dev
->of_node
, "auto-poll");
441 if (uap
->dmarx
.auto_poll_rate
) {
444 if (0 == of_property_read_u32(dev
->of_node
,
446 uap
->dmarx
.poll_rate
= x
;
448 uap
->dmarx
.poll_rate
= 100;
449 if (0 == of_property_read_u32(dev
->of_node
,
450 "poll-timeout-ms", &x
))
451 uap
->dmarx
.poll_timeout
= x
;
453 uap
->dmarx
.poll_timeout
= 3000;
456 dev_info(uap
->port
.dev
, "DMA channel RX %s\n",
457 dma_chan_name(uap
->dmarx
.chan
));
461 static void pl011_dma_remove(struct uart_amba_port
*uap
)
464 dma_release_channel(uap
->dmatx
.chan
);
466 dma_release_channel(uap
->dmarx
.chan
);
469 /* Forward declare these for the refill routine */
470 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
);
471 static void pl011_start_tx_pio(struct uart_amba_port
*uap
);
474 * The current DMA TX buffer has been sent.
475 * Try to queue up another DMA buffer.
477 static void pl011_dma_tx_callback(void *data
)
479 struct uart_amba_port
*uap
= data
;
480 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
484 spin_lock_irqsave(&uap
->port
.lock
, flags
);
485 if (uap
->dmatx
.queued
)
486 dma_unmap_sg(dmatx
->chan
->device
->dev
, &dmatx
->sg
, 1,
490 uap
->dmacr
= dmacr
& ~UART011_TXDMAE
;
491 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
494 * If TX DMA was disabled, it means that we've stopped the DMA for
495 * some reason (eg, XOFF received, or we want to send an X-char.)
497 * Note: we need to be careful here of a potential race between DMA
498 * and the rest of the driver - if the driver disables TX DMA while
499 * a TX buffer completing, we must update the tx queued status to
500 * get further refills (hence we check dmacr).
502 if (!(dmacr
& UART011_TXDMAE
) || uart_tx_stopped(&uap
->port
) ||
503 uart_circ_empty(&uap
->port
.state
->xmit
)) {
504 uap
->dmatx
.queued
= false;
505 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
509 if (pl011_dma_tx_refill(uap
) <= 0)
511 * We didn't queue a DMA buffer for some reason, but we
512 * have data pending to be sent. Re-enable the TX IRQ.
514 pl011_start_tx_pio(uap
);
516 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
520 * Try to refill the TX DMA buffer.
521 * Locking: called with port lock held and IRQs disabled.
523 * 1 if we queued up a TX DMA buffer.
524 * 0 if we didn't want to handle this by DMA
527 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
)
529 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
530 struct dma_chan
*chan
= dmatx
->chan
;
531 struct dma_device
*dma_dev
= chan
->device
;
532 struct dma_async_tx_descriptor
*desc
;
533 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
537 * Try to avoid the overhead involved in using DMA if the
538 * transaction fits in the first half of the FIFO, by using
539 * the standard interrupt handling. This ensures that we
540 * issue a uart_write_wakeup() at the appropriate time.
542 count
= uart_circ_chars_pending(xmit
);
543 if (count
< (uap
->fifosize
>> 1)) {
544 uap
->dmatx
.queued
= false;
549 * Bodge: don't send the last character by DMA, as this
550 * will prevent XON from notifying us to restart DMA.
554 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
555 if (count
> PL011_DMA_BUFFER_SIZE
)
556 count
= PL011_DMA_BUFFER_SIZE
;
558 if (xmit
->tail
< xmit
->head
)
559 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], count
);
561 size_t first
= UART_XMIT_SIZE
- xmit
->tail
;
566 second
= count
- first
;
568 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], first
);
570 memcpy(&dmatx
->buf
[first
], &xmit
->buf
[0], second
);
573 dmatx
->sg
.length
= count
;
575 if (dma_map_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
) != 1) {
576 uap
->dmatx
.queued
= false;
577 dev_dbg(uap
->port
.dev
, "unable to map TX DMA\n");
581 desc
= dmaengine_prep_slave_sg(chan
, &dmatx
->sg
, 1, DMA_MEM_TO_DEV
,
582 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
584 dma_unmap_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
);
585 uap
->dmatx
.queued
= false;
587 * If DMA cannot be used right now, we complete this
588 * transaction via IRQ and let the TTY layer retry.
590 dev_dbg(uap
->port
.dev
, "TX DMA busy\n");
594 /* Some data to go along to the callback */
595 desc
->callback
= pl011_dma_tx_callback
;
596 desc
->callback_param
= uap
;
598 /* All errors should happen at prepare time */
599 dmaengine_submit(desc
);
601 /* Fire the DMA transaction */
602 dma_dev
->device_issue_pending(chan
);
604 uap
->dmacr
|= UART011_TXDMAE
;
605 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
606 uap
->dmatx
.queued
= true;
609 * Now we know that DMA will fire, so advance the ring buffer
610 * with the stuff we just dispatched.
612 xmit
->tail
= (xmit
->tail
+ count
) & (UART_XMIT_SIZE
- 1);
613 uap
->port
.icount
.tx
+= count
;
615 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
616 uart_write_wakeup(&uap
->port
);
622 * We received a transmit interrupt without a pending X-char but with
623 * pending characters.
624 * Locking: called with port lock held and IRQs disabled.
626 * false if we want to use PIO to transmit
627 * true if we queued a DMA buffer
629 static bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
631 if (!uap
->using_tx_dma
)
635 * If we already have a TX buffer queued, but received a
636 * TX interrupt, it will be because we've just sent an X-char.
637 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
639 if (uap
->dmatx
.queued
) {
640 uap
->dmacr
|= UART011_TXDMAE
;
641 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
642 uap
->im
&= ~UART011_TXIM
;
643 pl011_write(uap
->im
, uap
, REG_IMSC
);
648 * We don't have a TX buffer queued, so try to queue one.
649 * If we successfully queued a buffer, mask the TX IRQ.
651 if (pl011_dma_tx_refill(uap
) > 0) {
652 uap
->im
&= ~UART011_TXIM
;
653 pl011_write(uap
->im
, uap
, REG_IMSC
);
660 * Stop the DMA transmit (eg, due to received XOFF).
661 * Locking: called with port lock held and IRQs disabled.
663 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
665 if (uap
->dmatx
.queued
) {
666 uap
->dmacr
&= ~UART011_TXDMAE
;
667 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
672 * Try to start a DMA transmit, or in the case of an XON/OFF
673 * character queued for send, try to get that character out ASAP.
674 * Locking: called with port lock held and IRQs disabled.
676 * false if we want the TX IRQ to be enabled
677 * true if we have a buffer queued
679 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
683 if (!uap
->using_tx_dma
)
686 if (!uap
->port
.x_char
) {
687 /* no X-char, try to push chars out in DMA mode */
690 if (!uap
->dmatx
.queued
) {
691 if (pl011_dma_tx_refill(uap
) > 0) {
692 uap
->im
&= ~UART011_TXIM
;
693 pl011_write(uap
->im
, uap
, REG_IMSC
);
696 } else if (!(uap
->dmacr
& UART011_TXDMAE
)) {
697 uap
->dmacr
|= UART011_TXDMAE
;
698 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
704 * We have an X-char to send. Disable DMA to prevent it loading
705 * the TX fifo, and then see if we can stuff it into the FIFO.
708 uap
->dmacr
&= ~UART011_TXDMAE
;
709 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
711 if (pl011_read(uap
, REG_FR
) & UART01x_FR_TXFF
) {
713 * No space in the FIFO, so enable the transmit interrupt
714 * so we know when there is space. Note that once we've
715 * loaded the character, we should just re-enable DMA.
720 pl011_write(uap
->port
.x_char
, uap
, REG_DR
);
721 uap
->port
.icount
.tx
++;
722 uap
->port
.x_char
= 0;
724 /* Success - restore the DMA state */
726 pl011_write(dmacr
, uap
, REG_DMACR
);
732 * Flush the transmit buffer.
733 * Locking: called with port lock held and IRQs disabled.
735 static void pl011_dma_flush_buffer(struct uart_port
*port
)
736 __releases(&uap
->port
.lock
)
737 __acquires(&uap
->port
.lock
)
739 struct uart_amba_port
*uap
=
740 container_of(port
, struct uart_amba_port
, port
);
742 if (!uap
->using_tx_dma
)
745 /* Avoid deadlock with the DMA engine callback */
746 spin_unlock(&uap
->port
.lock
);
747 dmaengine_terminate_all(uap
->dmatx
.chan
);
748 spin_lock(&uap
->port
.lock
);
749 if (uap
->dmatx
.queued
) {
750 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
752 uap
->dmatx
.queued
= false;
753 uap
->dmacr
&= ~UART011_TXDMAE
;
754 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
758 static void pl011_dma_rx_callback(void *data
);
760 static int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
762 struct dma_chan
*rxchan
= uap
->dmarx
.chan
;
763 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
764 struct dma_async_tx_descriptor
*desc
;
765 struct pl011_sgbuf
*sgbuf
;
770 /* Start the RX DMA job */
771 sgbuf
= uap
->dmarx
.use_buf_b
?
772 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
773 desc
= dmaengine_prep_slave_sg(rxchan
, &sgbuf
->sg
, 1,
775 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
777 * If the DMA engine is busy and cannot prepare a
778 * channel, no big deal, the driver will fall back
779 * to interrupt mode as a result of this error code.
782 uap
->dmarx
.running
= false;
783 dmaengine_terminate_all(rxchan
);
787 /* Some data to go along to the callback */
788 desc
->callback
= pl011_dma_rx_callback
;
789 desc
->callback_param
= uap
;
790 dmarx
->cookie
= dmaengine_submit(desc
);
791 dma_async_issue_pending(rxchan
);
793 uap
->dmacr
|= UART011_RXDMAE
;
794 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
795 uap
->dmarx
.running
= true;
797 uap
->im
&= ~UART011_RXIM
;
798 pl011_write(uap
->im
, uap
, REG_IMSC
);
804 * This is called when either the DMA job is complete, or
805 * the FIFO timeout interrupt occurred. This must be called
806 * with the port spinlock uap->port.lock held.
808 static void pl011_dma_rx_chars(struct uart_amba_port
*uap
,
809 u32 pending
, bool use_buf_b
,
812 struct tty_port
*port
= &uap
->port
.state
->port
;
813 struct pl011_sgbuf
*sgbuf
= use_buf_b
?
814 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
816 u32 fifotaken
= 0; /* only used for vdbg() */
818 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
821 if (uap
->dmarx
.poll_rate
) {
822 /* The data can be taken by polling */
823 dmataken
= sgbuf
->sg
.length
- dmarx
->last_residue
;
824 /* Recalculate the pending size */
825 if (pending
>= dmataken
)
829 /* Pick the remain data from the DMA */
833 * First take all chars in the DMA pipe, then look in the FIFO.
834 * Note that tty_insert_flip_buf() tries to take as many chars
837 dma_count
= tty_insert_flip_string(port
, sgbuf
->buf
+ dmataken
,
840 uap
->port
.icount
.rx
+= dma_count
;
841 if (dma_count
< pending
)
842 dev_warn(uap
->port
.dev
,
843 "couldn't insert all characters (TTY is full?)\n");
846 /* Reset the last_residue for Rx DMA poll */
847 if (uap
->dmarx
.poll_rate
)
848 dmarx
->last_residue
= sgbuf
->sg
.length
;
851 * Only continue with trying to read the FIFO if all DMA chars have
854 if (dma_count
== pending
&& readfifo
) {
855 /* Clear any error flags */
856 pl011_write(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
|
857 UART011_FEIS
, uap
, REG_ICR
);
860 * If we read all the DMA'd characters, and we had an
861 * incomplete buffer, that could be due to an rx error, or
862 * maybe we just timed out. Read any pending chars and check
865 * Error conditions will only occur in the FIFO, these will
866 * trigger an immediate interrupt and stop the DMA job, so we
867 * will always find the error in the FIFO, never in the DMA
870 fifotaken
= pl011_fifo_to_tty(uap
);
873 spin_unlock(&uap
->port
.lock
);
874 dev_vdbg(uap
->port
.dev
,
875 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
876 dma_count
, fifotaken
);
877 tty_flip_buffer_push(port
);
878 spin_lock(&uap
->port
.lock
);
881 static void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
883 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
884 struct dma_chan
*rxchan
= dmarx
->chan
;
885 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
886 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
888 struct dma_tx_state state
;
889 enum dma_status dmastat
;
892 * Pause the transfer so we can trust the current counter,
893 * do this before we pause the PL011 block, else we may
896 if (dmaengine_pause(rxchan
))
897 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
898 dmastat
= rxchan
->device
->device_tx_status(rxchan
,
899 dmarx
->cookie
, &state
);
900 if (dmastat
!= DMA_PAUSED
)
901 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
903 /* Disable RX DMA - incoming data will wait in the FIFO */
904 uap
->dmacr
&= ~UART011_RXDMAE
;
905 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
906 uap
->dmarx
.running
= false;
908 pending
= sgbuf
->sg
.length
- state
.residue
;
909 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
910 /* Then we terminate the transfer - we now know our residue */
911 dmaengine_terminate_all(rxchan
);
914 * This will take the chars we have so far and insert
915 * into the framework.
917 pl011_dma_rx_chars(uap
, pending
, dmarx
->use_buf_b
, true);
919 /* Switch buffer & re-trigger DMA job */
920 dmarx
->use_buf_b
= !dmarx
->use_buf_b
;
921 if (pl011_dma_rx_trigger_dma(uap
)) {
922 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
923 "fall back to interrupt mode\n");
924 uap
->im
|= UART011_RXIM
;
925 pl011_write(uap
->im
, uap
, REG_IMSC
);
929 static void pl011_dma_rx_callback(void *data
)
931 struct uart_amba_port
*uap
= data
;
932 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
933 struct dma_chan
*rxchan
= dmarx
->chan
;
934 bool lastbuf
= dmarx
->use_buf_b
;
935 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
936 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
938 struct dma_tx_state state
;
942 * This completion interrupt occurs typically when the
943 * RX buffer is totally stuffed but no timeout has yet
944 * occurred. When that happens, we just want the RX
945 * routine to flush out the secondary DMA buffer while
946 * we immediately trigger the next DMA job.
948 spin_lock_irq(&uap
->port
.lock
);
950 * Rx data can be taken by the UART interrupts during
951 * the DMA irq handler. So we check the residue here.
953 rxchan
->device
->device_tx_status(rxchan
, dmarx
->cookie
, &state
);
954 pending
= sgbuf
->sg
.length
- state
.residue
;
955 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
956 /* Then we terminate the transfer - we now know our residue */
957 dmaengine_terminate_all(rxchan
);
959 uap
->dmarx
.running
= false;
960 dmarx
->use_buf_b
= !lastbuf
;
961 ret
= pl011_dma_rx_trigger_dma(uap
);
963 pl011_dma_rx_chars(uap
, pending
, lastbuf
, false);
964 spin_unlock_irq(&uap
->port
.lock
);
966 * Do this check after we picked the DMA chars so we don't
967 * get some IRQ immediately from RX.
970 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
971 "fall back to interrupt mode\n");
972 uap
->im
|= UART011_RXIM
;
973 pl011_write(uap
->im
, uap
, REG_IMSC
);
978 * Stop accepting received characters, when we're shutting down or
979 * suspending this port.
980 * Locking: called with port lock held and IRQs disabled.
982 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
984 /* FIXME. Just disable the DMA enable */
985 uap
->dmacr
&= ~UART011_RXDMAE
;
986 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
990 * Timer handler for Rx DMA polling.
991 * Every polling, It checks the residue in the dma buffer and transfer
992 * data to the tty. Also, last_residue is updated for the next polling.
994 static void pl011_dma_rx_poll(unsigned long args
)
996 struct uart_amba_port
*uap
= (struct uart_amba_port
*)args
;
997 struct tty_port
*port
= &uap
->port
.state
->port
;
998 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
999 struct dma_chan
*rxchan
= uap
->dmarx
.chan
;
1000 unsigned long flags
= 0;
1001 unsigned int dmataken
= 0;
1002 unsigned int size
= 0;
1003 struct pl011_sgbuf
*sgbuf
;
1005 struct dma_tx_state state
;
1007 sgbuf
= dmarx
->use_buf_b
? &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
1008 rxchan
->device
->device_tx_status(rxchan
, dmarx
->cookie
, &state
);
1009 if (likely(state
.residue
< dmarx
->last_residue
)) {
1010 dmataken
= sgbuf
->sg
.length
- dmarx
->last_residue
;
1011 size
= dmarx
->last_residue
- state
.residue
;
1012 dma_count
= tty_insert_flip_string(port
, sgbuf
->buf
+ dmataken
,
1014 if (dma_count
== size
)
1015 dmarx
->last_residue
= state
.residue
;
1016 dmarx
->last_jiffies
= jiffies
;
1018 tty_flip_buffer_push(port
);
1021 * If no data is received in poll_timeout, the driver will fall back
1022 * to interrupt mode. We will retrigger DMA at the first interrupt.
1024 if (jiffies_to_msecs(jiffies
- dmarx
->last_jiffies
)
1025 > uap
->dmarx
.poll_timeout
) {
1027 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1028 pl011_dma_rx_stop(uap
);
1029 uap
->im
|= UART011_RXIM
;
1030 pl011_write(uap
->im
, uap
, REG_IMSC
);
1031 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1033 uap
->dmarx
.running
= false;
1034 dmaengine_terminate_all(rxchan
);
1035 del_timer(&uap
->dmarx
.timer
);
1037 mod_timer(&uap
->dmarx
.timer
,
1038 jiffies
+ msecs_to_jiffies(uap
->dmarx
.poll_rate
));
1042 static void pl011_dma_startup(struct uart_amba_port
*uap
)
1046 if (!uap
->dma_probed
)
1047 pl011_dma_probe(uap
);
1049 if (!uap
->dmatx
.chan
)
1052 uap
->dmatx
.buf
= kmalloc(PL011_DMA_BUFFER_SIZE
, GFP_KERNEL
| __GFP_DMA
);
1053 if (!uap
->dmatx
.buf
) {
1054 dev_err(uap
->port
.dev
, "no memory for DMA TX buffer\n");
1055 uap
->port
.fifosize
= uap
->fifosize
;
1059 sg_init_one(&uap
->dmatx
.sg
, uap
->dmatx
.buf
, PL011_DMA_BUFFER_SIZE
);
1061 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1062 uap
->port
.fifosize
= PL011_DMA_BUFFER_SIZE
;
1063 uap
->using_tx_dma
= true;
1065 if (!uap
->dmarx
.chan
)
1068 /* Allocate and map DMA RX buffers */
1069 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
1072 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
1073 "RX buffer A", ret
);
1077 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
,
1080 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
1081 "RX buffer B", ret
);
1082 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
1087 uap
->using_rx_dma
= true;
1090 /* Turn on DMA error (RX/TX will be enabled on demand) */
1091 uap
->dmacr
|= UART011_DMAONERR
;
1092 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
1095 * ST Micro variants has some specific dma burst threshold
1096 * compensation. Set this to 16 bytes, so burst will only
1097 * be issued above/below 16 bytes.
1099 if (uap
->vendor
->dma_threshold
)
1100 pl011_write(ST_UART011_DMAWM_RX_16
| ST_UART011_DMAWM_TX_16
,
1103 if (uap
->using_rx_dma
) {
1104 if (pl011_dma_rx_trigger_dma(uap
))
1105 dev_dbg(uap
->port
.dev
, "could not trigger initial "
1106 "RX DMA job, fall back to interrupt mode\n");
1107 if (uap
->dmarx
.poll_rate
) {
1108 init_timer(&(uap
->dmarx
.timer
));
1109 uap
->dmarx
.timer
.function
= pl011_dma_rx_poll
;
1110 uap
->dmarx
.timer
.data
= (unsigned long)uap
;
1111 mod_timer(&uap
->dmarx
.timer
,
1113 msecs_to_jiffies(uap
->dmarx
.poll_rate
));
1114 uap
->dmarx
.last_residue
= PL011_DMA_BUFFER_SIZE
;
1115 uap
->dmarx
.last_jiffies
= jiffies
;
1120 static void pl011_dma_shutdown(struct uart_amba_port
*uap
)
1122 if (!(uap
->using_tx_dma
|| uap
->using_rx_dma
))
1125 /* Disable RX and TX DMA */
1126 while (pl011_read(uap
, REG_FR
) & UART01x_FR_BUSY
)
1129 spin_lock_irq(&uap
->port
.lock
);
1130 uap
->dmacr
&= ~(UART011_DMAONERR
| UART011_RXDMAE
| UART011_TXDMAE
);
1131 pl011_write(uap
->dmacr
, uap
, REG_DMACR
);
1132 spin_unlock_irq(&uap
->port
.lock
);
1134 if (uap
->using_tx_dma
) {
1135 /* In theory, this should already be done by pl011_dma_flush_buffer */
1136 dmaengine_terminate_all(uap
->dmatx
.chan
);
1137 if (uap
->dmatx
.queued
) {
1138 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
1140 uap
->dmatx
.queued
= false;
1143 kfree(uap
->dmatx
.buf
);
1144 uap
->using_tx_dma
= false;
1147 if (uap
->using_rx_dma
) {
1148 dmaengine_terminate_all(uap
->dmarx
.chan
);
1149 /* Clean up the RX DMA */
1150 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
, DMA_FROM_DEVICE
);
1151 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
, DMA_FROM_DEVICE
);
1152 if (uap
->dmarx
.poll_rate
)
1153 del_timer_sync(&uap
->dmarx
.timer
);
1154 uap
->using_rx_dma
= false;
1158 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
1160 return uap
->using_rx_dma
;
1163 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
1165 return uap
->using_rx_dma
&& uap
->dmarx
.running
;
1169 /* Blank functions if the DMA engine is not available */
1170 static inline void pl011_dma_probe(struct uart_amba_port
*uap
)
1174 static inline void pl011_dma_remove(struct uart_amba_port
*uap
)
1178 static inline void pl011_dma_startup(struct uart_amba_port
*uap
)
1182 static inline void pl011_dma_shutdown(struct uart_amba_port
*uap
)
1186 static inline bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
1191 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
1195 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
1200 static inline void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
1204 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
1208 static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
1213 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
1218 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
1223 #define pl011_dma_flush_buffer NULL
1226 static void pl011_stop_tx(struct uart_port
*port
)
1228 struct uart_amba_port
*uap
=
1229 container_of(port
, struct uart_amba_port
, port
);
1231 uap
->im
&= ~UART011_TXIM
;
1232 pl011_write(uap
->im
, uap
, REG_IMSC
);
1233 pl011_dma_tx_stop(uap
);
1236 static void pl011_tx_chars(struct uart_amba_port
*uap
, bool from_irq
);
1238 /* Start TX with programmed I/O only (no DMA) */
1239 static void pl011_start_tx_pio(struct uart_amba_port
*uap
)
1241 uap
->im
|= UART011_TXIM
;
1242 pl011_write(uap
->im
, uap
, REG_IMSC
);
1243 pl011_tx_chars(uap
, false);
1246 static void pl011_start_tx(struct uart_port
*port
)
1248 struct uart_amba_port
*uap
=
1249 container_of(port
, struct uart_amba_port
, port
);
1251 if (!pl011_dma_tx_start(uap
))
1252 pl011_start_tx_pio(uap
);
1255 static void pl011_stop_rx(struct uart_port
*port
)
1257 struct uart_amba_port
*uap
=
1258 container_of(port
, struct uart_amba_port
, port
);
1260 uap
->im
&= ~(UART011_RXIM
|UART011_RTIM
|UART011_FEIM
|
1261 UART011_PEIM
|UART011_BEIM
|UART011_OEIM
);
1262 pl011_write(uap
->im
, uap
, REG_IMSC
);
1264 pl011_dma_rx_stop(uap
);
1267 static void pl011_enable_ms(struct uart_port
*port
)
1269 struct uart_amba_port
*uap
=
1270 container_of(port
, struct uart_amba_port
, port
);
1272 uap
->im
|= UART011_RIMIM
|UART011_CTSMIM
|UART011_DCDMIM
|UART011_DSRMIM
;
1273 pl011_write(uap
->im
, uap
, REG_IMSC
);
1276 static void pl011_rx_chars(struct uart_amba_port
*uap
)
1277 __releases(&uap
->port
.lock
)
1278 __acquires(&uap
->port
.lock
)
1280 pl011_fifo_to_tty(uap
);
1282 spin_unlock(&uap
->port
.lock
);
1283 tty_flip_buffer_push(&uap
->port
.state
->port
);
1285 * If we were temporarily out of DMA mode for a while,
1286 * attempt to switch back to DMA mode again.
1288 if (pl011_dma_rx_available(uap
)) {
1289 if (pl011_dma_rx_trigger_dma(uap
)) {
1290 dev_dbg(uap
->port
.dev
, "could not trigger RX DMA job "
1291 "fall back to interrupt mode again\n");
1292 uap
->im
|= UART011_RXIM
;
1293 pl011_write(uap
->im
, uap
, REG_IMSC
);
1295 #ifdef CONFIG_DMA_ENGINE
1296 /* Start Rx DMA poll */
1297 if (uap
->dmarx
.poll_rate
) {
1298 uap
->dmarx
.last_jiffies
= jiffies
;
1299 uap
->dmarx
.last_residue
= PL011_DMA_BUFFER_SIZE
;
1300 mod_timer(&uap
->dmarx
.timer
,
1302 msecs_to_jiffies(uap
->dmarx
.poll_rate
));
1307 spin_lock(&uap
->port
.lock
);
1310 static bool pl011_tx_char(struct uart_amba_port
*uap
, unsigned char c
,
1313 if (unlikely(!from_irq
) &&
1314 pl011_read(uap
, REG_FR
) & UART01x_FR_TXFF
)
1315 return false; /* unable to transmit character */
1317 pl011_write(c
, uap
, REG_DR
);
1318 uap
->port
.icount
.tx
++;
1323 static void pl011_tx_chars(struct uart_amba_port
*uap
, bool from_irq
)
1325 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
1326 int count
= uap
->fifosize
>> 1;
1328 if (uap
->port
.x_char
) {
1329 if (!pl011_tx_char(uap
, uap
->port
.x_char
, from_irq
))
1331 uap
->port
.x_char
= 0;
1334 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uap
->port
)) {
1335 pl011_stop_tx(&uap
->port
);
1339 /* If we are using DMA mode, try to send some characters. */
1340 if (pl011_dma_tx_irq(uap
))
1344 if (likely(from_irq
) && count
-- == 0)
1347 if (!pl011_tx_char(uap
, xmit
->buf
[xmit
->tail
], from_irq
))
1350 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
1351 } while (!uart_circ_empty(xmit
));
1353 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
1354 uart_write_wakeup(&uap
->port
);
1356 if (uart_circ_empty(xmit
))
1357 pl011_stop_tx(&uap
->port
);
1360 static void pl011_modem_status(struct uart_amba_port
*uap
)
1362 unsigned int status
, delta
;
1364 status
= pl011_read(uap
, REG_FR
) & UART01x_FR_MODEM_ANY
;
1366 delta
= status
^ uap
->old_status
;
1367 uap
->old_status
= status
;
1372 if (delta
& UART01x_FR_DCD
)
1373 uart_handle_dcd_change(&uap
->port
, status
& UART01x_FR_DCD
);
1375 if (delta
& UART01x_FR_DSR
)
1376 uap
->port
.icount
.dsr
++;
1378 if (delta
& UART01x_FR_CTS
)
1379 uart_handle_cts_change(&uap
->port
, status
& UART01x_FR_CTS
);
1381 wake_up_interruptible(&uap
->port
.state
->port
.delta_msr_wait
);
1384 static void check_apply_cts_event_workaround(struct uart_amba_port
*uap
)
1386 unsigned int dummy_read
;
1388 if (!uap
->vendor
->cts_event_workaround
)
1391 /* workaround to make sure that all bits are unlocked.. */
1392 pl011_write(0x00, uap
, REG_ICR
);
1395 * WA: introduce 26ns(1 uart clk) delay before W1C;
1396 * single apb access will incur 2 pclk(133.12Mhz) delay,
1397 * so add 2 dummy reads
1399 dummy_read
= pl011_read(uap
, REG_ICR
);
1400 dummy_read
= pl011_read(uap
, REG_ICR
);
1403 static irqreturn_t
pl011_int(int irq
, void *dev_id
)
1405 struct uart_amba_port
*uap
= dev_id
;
1406 unsigned long flags
;
1407 unsigned int status
, pass_counter
= AMBA_ISR_PASS_LIMIT
;
1411 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1412 imsc
= pl011_read(uap
, REG_IMSC
);
1413 status
= pl011_read(uap
, REG_RIS
) & imsc
;
1416 check_apply_cts_event_workaround(uap
);
1418 pl011_write(status
& ~(UART011_TXIS
|UART011_RTIS
|
1422 if (status
& (UART011_RTIS
|UART011_RXIS
)) {
1423 if (pl011_dma_rx_running(uap
))
1424 pl011_dma_rx_irq(uap
);
1426 pl011_rx_chars(uap
);
1428 if (status
& (UART011_DSRMIS
|UART011_DCDMIS
|
1429 UART011_CTSMIS
|UART011_RIMIS
))
1430 pl011_modem_status(uap
);
1431 if (status
& UART011_TXIS
)
1432 pl011_tx_chars(uap
, true);
1434 if (pass_counter
-- == 0)
1437 status
= pl011_read(uap
, REG_RIS
) & imsc
;
1438 } while (status
!= 0);
1442 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1444 return IRQ_RETVAL(handled
);
1447 static unsigned int pl011_tx_empty(struct uart_port
*port
)
1449 struct uart_amba_port
*uap
=
1450 container_of(port
, struct uart_amba_port
, port
);
1451 unsigned int status
= pl011_read(uap
, REG_FR
);
1452 return status
& (UART01x_FR_BUSY
|UART01x_FR_TXFF
) ? 0 : TIOCSER_TEMT
;
1455 static unsigned int pl011_get_mctrl(struct uart_port
*port
)
1457 struct uart_amba_port
*uap
=
1458 container_of(port
, struct uart_amba_port
, port
);
1459 unsigned int result
= 0;
1460 unsigned int status
= pl011_read(uap
, REG_FR
);
1462 #define TIOCMBIT(uartbit, tiocmbit) \
1463 if (status & uartbit) \
1466 TIOCMBIT(UART01x_FR_DCD
, TIOCM_CAR
);
1467 TIOCMBIT(UART01x_FR_DSR
, TIOCM_DSR
);
1468 TIOCMBIT(UART01x_FR_CTS
, TIOCM_CTS
);
1469 TIOCMBIT(UART011_FR_RI
, TIOCM_RNG
);
1474 static void pl011_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1476 struct uart_amba_port
*uap
=
1477 container_of(port
, struct uart_amba_port
, port
);
1480 cr
= pl011_read(uap
, REG_CR
);
1482 #define TIOCMBIT(tiocmbit, uartbit) \
1483 if (mctrl & tiocmbit) \
1488 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTS
);
1489 TIOCMBIT(TIOCM_DTR
, UART011_CR_DTR
);
1490 TIOCMBIT(TIOCM_OUT1
, UART011_CR_OUT1
);
1491 TIOCMBIT(TIOCM_OUT2
, UART011_CR_OUT2
);
1492 TIOCMBIT(TIOCM_LOOP
, UART011_CR_LBE
);
1495 /* We need to disable auto-RTS if we want to turn RTS off */
1496 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTSEN
);
1500 pl011_write(cr
, uap
, REG_CR
);
1503 static void pl011_break_ctl(struct uart_port
*port
, int break_state
)
1505 struct uart_amba_port
*uap
=
1506 container_of(port
, struct uart_amba_port
, port
);
1507 unsigned long flags
;
1510 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1511 lcr_h
= pl011_read(uap
, uap
->lcrh_tx
);
1512 if (break_state
== -1)
1513 lcr_h
|= UART01x_LCRH_BRK
;
1515 lcr_h
&= ~UART01x_LCRH_BRK
;
1516 pl011_write(lcr_h
, uap
, uap
->lcrh_tx
);
1517 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1520 #ifdef CONFIG_CONSOLE_POLL
1522 static void pl011_quiesce_irqs(struct uart_port
*port
)
1524 struct uart_amba_port
*uap
=
1525 container_of(port
, struct uart_amba_port
, port
);
1527 pl011_write(pl011_read(uap
, REG_MIS
), uap
, REG_ICR
);
1529 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1530 * we simply mask it. start_tx() will unmask it.
1532 * Note we can race with start_tx(), and if the race happens, the
1533 * polling user might get another interrupt just after we clear it.
1534 * But it should be OK and can happen even w/o the race, e.g.
1535 * controller immediately got some new data and raised the IRQ.
1537 * And whoever uses polling routines assumes that it manages the device
1538 * (including tx queue), so we're also fine with start_tx()'s caller
1541 pl011_write(pl011_read(uap
, REG_IMSC
) & ~UART011_TXIM
, uap
,
1545 static int pl011_get_poll_char(struct uart_port
*port
)
1547 struct uart_amba_port
*uap
=
1548 container_of(port
, struct uart_amba_port
, port
);
1549 unsigned int status
;
1552 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1555 pl011_quiesce_irqs(port
);
1557 status
= pl011_read(uap
, REG_FR
);
1558 if (status
& UART01x_FR_RXFE
)
1559 return NO_POLL_CHAR
;
1561 return pl011_read(uap
, REG_DR
);
1564 static void pl011_put_poll_char(struct uart_port
*port
,
1567 struct uart_amba_port
*uap
=
1568 container_of(port
, struct uart_amba_port
, port
);
1570 while (pl011_read(uap
, REG_FR
) & UART01x_FR_TXFF
)
1573 pl011_write(ch
, uap
, REG_DR
);
1576 #endif /* CONFIG_CONSOLE_POLL */
1578 static int pl011_hwinit(struct uart_port
*port
)
1580 struct uart_amba_port
*uap
=
1581 container_of(port
, struct uart_amba_port
, port
);
1584 /* Optionaly enable pins to be muxed in and configured */
1585 pinctrl_pm_select_default_state(port
->dev
);
1588 * Try to enable the clock producer.
1590 retval
= clk_prepare_enable(uap
->clk
);
1594 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
1596 /* Clear pending error and receive interrupts */
1597 pl011_write(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
|
1598 UART011_FEIS
| UART011_RTIS
| UART011_RXIS
,
1602 * Save interrupts enable mask, and enable RX interrupts in case if
1603 * the interrupt is used for NMI entry.
1605 uap
->im
= pl011_read(uap
, REG_IMSC
);
1606 pl011_write(UART011_RTIM
| UART011_RXIM
, uap
, REG_IMSC
);
1608 if (dev_get_platdata(uap
->port
.dev
)) {
1609 struct amba_pl011_data
*plat
;
1611 plat
= dev_get_platdata(uap
->port
.dev
);
1618 static bool pl011_split_lcrh(const struct uart_amba_port
*uap
)
1620 return pl011_reg_to_offset(uap
, uap
->lcrh_rx
) !=
1621 pl011_reg_to_offset(uap
, uap
->lcrh_tx
);
1624 static void pl011_write_lcr_h(struct uart_amba_port
*uap
, unsigned int lcr_h
)
1626 pl011_write(lcr_h
, uap
, uap
->lcrh_rx
);
1627 if (pl011_split_lcrh(uap
)) {
1630 * Wait 10 PCLKs before writing LCRH_TX register,
1631 * to get this delay write read only register 10 times
1633 for (i
= 0; i
< 10; ++i
)
1634 pl011_write(0xff, uap
, REG_MIS
);
1635 pl011_write(lcr_h
, uap
, uap
->lcrh_tx
);
1639 static int pl011_allocate_irq(struct uart_amba_port
*uap
)
1641 pl011_write(uap
->im
, uap
, REG_IMSC
);
1643 return request_irq(uap
->port
.irq
, pl011_int
, 0, "uart-pl011", uap
);
1647 * Enable interrupts, only timeouts when using DMA
1648 * if initial RX DMA job failed, start in interrupt mode
1651 static void pl011_enable_interrupts(struct uart_amba_port
*uap
)
1653 spin_lock_irq(&uap
->port
.lock
);
1655 /* Clear out any spuriously appearing RX interrupts */
1656 pl011_write(UART011_RTIS
| UART011_RXIS
, uap
, REG_ICR
);
1657 uap
->im
= UART011_RTIM
;
1658 if (!pl011_dma_rx_running(uap
))
1659 uap
->im
|= UART011_RXIM
;
1660 pl011_write(uap
->im
, uap
, REG_IMSC
);
1661 spin_unlock_irq(&uap
->port
.lock
);
1664 static int pl011_startup(struct uart_port
*port
)
1666 struct uart_amba_port
*uap
=
1667 container_of(port
, struct uart_amba_port
, port
);
1671 retval
= pl011_hwinit(port
);
1675 retval
= pl011_allocate_irq(uap
);
1679 pl011_write(uap
->vendor
->ifls
, uap
, REG_IFLS
);
1681 spin_lock_irq(&uap
->port
.lock
);
1683 /* restore RTS and DTR */
1684 cr
= uap
->old_cr
& (UART011_CR_RTS
| UART011_CR_DTR
);
1685 cr
|= UART01x_CR_UARTEN
| UART011_CR_RXE
| UART011_CR_TXE
;
1686 pl011_write(cr
, uap
, REG_CR
);
1688 spin_unlock_irq(&uap
->port
.lock
);
1691 * initialise the old status of the modem signals
1693 uap
->old_status
= pl011_read(uap
, REG_FR
) & UART01x_FR_MODEM_ANY
;
1696 pl011_dma_startup(uap
);
1698 pl011_enable_interrupts(uap
);
1703 clk_disable_unprepare(uap
->clk
);
1707 static int sbsa_uart_startup(struct uart_port
*port
)
1709 struct uart_amba_port
*uap
=
1710 container_of(port
, struct uart_amba_port
, port
);
1713 retval
= pl011_hwinit(port
);
1717 retval
= pl011_allocate_irq(uap
);
1721 /* The SBSA UART does not support any modem status lines. */
1722 uap
->old_status
= 0;
1724 pl011_enable_interrupts(uap
);
1729 static void pl011_shutdown_channel(struct uart_amba_port
*uap
,
1734 val
= pl011_read(uap
, lcrh
);
1735 val
&= ~(UART01x_LCRH_BRK
| UART01x_LCRH_FEN
);
1736 pl011_write(val
, uap
, lcrh
);
1740 * disable the port. It should not disable RTS and DTR.
1741 * Also RTS and DTR state should be preserved to restore
1742 * it during startup().
1744 static void pl011_disable_uart(struct uart_amba_port
*uap
)
1748 uap
->autorts
= false;
1749 spin_lock_irq(&uap
->port
.lock
);
1750 cr
= pl011_read(uap
, REG_CR
);
1752 cr
&= UART011_CR_RTS
| UART011_CR_DTR
;
1753 cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
1754 pl011_write(cr
, uap
, REG_CR
);
1755 spin_unlock_irq(&uap
->port
.lock
);
1758 * disable break condition and fifos
1760 pl011_shutdown_channel(uap
, uap
->lcrh_rx
);
1761 if (pl011_split_lcrh(uap
))
1762 pl011_shutdown_channel(uap
, uap
->lcrh_tx
);
1765 static void pl011_disable_interrupts(struct uart_amba_port
*uap
)
1767 spin_lock_irq(&uap
->port
.lock
);
1769 /* mask all interrupts and clear all pending ones */
1771 pl011_write(uap
->im
, uap
, REG_IMSC
);
1772 pl011_write(0xffff, uap
, REG_ICR
);
1774 spin_unlock_irq(&uap
->port
.lock
);
1777 static void pl011_shutdown(struct uart_port
*port
)
1779 struct uart_amba_port
*uap
=
1780 container_of(port
, struct uart_amba_port
, port
);
1782 pl011_disable_interrupts(uap
);
1784 pl011_dma_shutdown(uap
);
1786 free_irq(uap
->port
.irq
, uap
);
1788 pl011_disable_uart(uap
);
1791 * Shut down the clock producer
1793 clk_disable_unprepare(uap
->clk
);
1794 /* Optionally let pins go into sleep states */
1795 pinctrl_pm_select_sleep_state(port
->dev
);
1797 if (dev_get_platdata(uap
->port
.dev
)) {
1798 struct amba_pl011_data
*plat
;
1800 plat
= dev_get_platdata(uap
->port
.dev
);
1805 if (uap
->port
.ops
->flush_buffer
)
1806 uap
->port
.ops
->flush_buffer(port
);
1809 static void sbsa_uart_shutdown(struct uart_port
*port
)
1811 struct uart_amba_port
*uap
=
1812 container_of(port
, struct uart_amba_port
, port
);
1814 pl011_disable_interrupts(uap
);
1816 free_irq(uap
->port
.irq
, uap
);
1818 if (uap
->port
.ops
->flush_buffer
)
1819 uap
->port
.ops
->flush_buffer(port
);
1823 pl011_setup_status_masks(struct uart_port
*port
, struct ktermios
*termios
)
1825 port
->read_status_mask
= UART011_DR_OE
| 255;
1826 if (termios
->c_iflag
& INPCK
)
1827 port
->read_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1828 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1829 port
->read_status_mask
|= UART011_DR_BE
;
1832 * Characters to ignore
1834 port
->ignore_status_mask
= 0;
1835 if (termios
->c_iflag
& IGNPAR
)
1836 port
->ignore_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1837 if (termios
->c_iflag
& IGNBRK
) {
1838 port
->ignore_status_mask
|= UART011_DR_BE
;
1840 * If we're ignoring parity and break indicators,
1841 * ignore overruns too (for real raw support).
1843 if (termios
->c_iflag
& IGNPAR
)
1844 port
->ignore_status_mask
|= UART011_DR_OE
;
1848 * Ignore all characters if CREAD is not set.
1850 if ((termios
->c_cflag
& CREAD
) == 0)
1851 port
->ignore_status_mask
|= UART_DUMMY_DR_RX
;
1855 pl011_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1856 struct ktermios
*old
)
1858 struct uart_amba_port
*uap
=
1859 container_of(port
, struct uart_amba_port
, port
);
1860 unsigned int lcr_h
, old_cr
;
1861 unsigned long flags
;
1862 unsigned int baud
, quot
, clkdiv
;
1864 if (uap
->vendor
->oversampling
)
1870 * Ask the core to calculate the divisor for us.
1872 baud
= uart_get_baud_rate(port
, termios
, old
, 0,
1873 port
->uartclk
/ clkdiv
);
1874 #ifdef CONFIG_DMA_ENGINE
1876 * Adjust RX DMA polling rate with baud rate if not specified.
1878 if (uap
->dmarx
.auto_poll_rate
)
1879 uap
->dmarx
.poll_rate
= DIV_ROUND_UP(10000000, baud
);
1882 if (baud
> port
->uartclk
/16)
1883 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 8, baud
);
1885 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 4, baud
);
1887 switch (termios
->c_cflag
& CSIZE
) {
1889 lcr_h
= UART01x_LCRH_WLEN_5
;
1892 lcr_h
= UART01x_LCRH_WLEN_6
;
1895 lcr_h
= UART01x_LCRH_WLEN_7
;
1898 lcr_h
= UART01x_LCRH_WLEN_8
;
1901 if (termios
->c_cflag
& CSTOPB
)
1902 lcr_h
|= UART01x_LCRH_STP2
;
1903 if (termios
->c_cflag
& PARENB
) {
1904 lcr_h
|= UART01x_LCRH_PEN
;
1905 if (!(termios
->c_cflag
& PARODD
))
1906 lcr_h
|= UART01x_LCRH_EPS
;
1908 if (uap
->fifosize
> 1)
1909 lcr_h
|= UART01x_LCRH_FEN
;
1911 spin_lock_irqsave(&port
->lock
, flags
);
1914 * Update the per-port timeout.
1916 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1918 pl011_setup_status_masks(port
, termios
);
1920 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1921 pl011_enable_ms(port
);
1923 /* first, disable everything */
1924 old_cr
= pl011_read(uap
, REG_CR
);
1925 pl011_write(0, uap
, REG_CR
);
1927 if (termios
->c_cflag
& CRTSCTS
) {
1928 if (old_cr
& UART011_CR_RTS
)
1929 old_cr
|= UART011_CR_RTSEN
;
1931 old_cr
|= UART011_CR_CTSEN
;
1932 uap
->autorts
= true;
1934 old_cr
&= ~(UART011_CR_CTSEN
| UART011_CR_RTSEN
);
1935 uap
->autorts
= false;
1938 if (uap
->vendor
->oversampling
) {
1939 if (baud
> port
->uartclk
/ 16)
1940 old_cr
|= ST_UART011_CR_OVSFACT
;
1942 old_cr
&= ~ST_UART011_CR_OVSFACT
;
1946 * Workaround for the ST Micro oversampling variants to
1947 * increase the bitrate slightly, by lowering the divisor,
1948 * to avoid delayed sampling of start bit at high speeds,
1949 * else we see data corruption.
1951 if (uap
->vendor
->oversampling
) {
1952 if ((baud
>= 3000000) && (baud
< 3250000) && (quot
> 1))
1954 else if ((baud
> 3250000) && (quot
> 2))
1958 pl011_write(quot
& 0x3f, uap
, REG_FBRD
);
1959 pl011_write(quot
>> 6, uap
, REG_IBRD
);
1962 * ----------v----------v----------v----------v-----
1963 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1964 * REG_FBRD & REG_IBRD.
1965 * ----------^----------^----------^----------^-----
1967 pl011_write_lcr_h(uap
, lcr_h
);
1968 pl011_write(old_cr
, uap
, REG_CR
);
1970 spin_unlock_irqrestore(&port
->lock
, flags
);
1974 sbsa_uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1975 struct ktermios
*old
)
1977 struct uart_amba_port
*uap
=
1978 container_of(port
, struct uart_amba_port
, port
);
1979 unsigned long flags
;
1981 tty_termios_encode_baud_rate(termios
, uap
->fixed_baud
, uap
->fixed_baud
);
1983 /* The SBSA UART only supports 8n1 without hardware flow control. */
1984 termios
->c_cflag
&= ~(CSIZE
| CSTOPB
| PARENB
| PARODD
);
1985 termios
->c_cflag
&= ~(CMSPAR
| CRTSCTS
);
1986 termios
->c_cflag
|= CS8
| CLOCAL
;
1988 spin_lock_irqsave(&port
->lock
, flags
);
1989 uart_update_timeout(port
, CS8
, uap
->fixed_baud
);
1990 pl011_setup_status_masks(port
, termios
);
1991 spin_unlock_irqrestore(&port
->lock
, flags
);
1994 static const char *pl011_type(struct uart_port
*port
)
1996 struct uart_amba_port
*uap
=
1997 container_of(port
, struct uart_amba_port
, port
);
1998 return uap
->port
.type
== PORT_AMBA
? uap
->type
: NULL
;
2002 * Release the memory region(s) being used by 'port'
2004 static void pl011_release_port(struct uart_port
*port
)
2006 release_mem_region(port
->mapbase
, SZ_4K
);
2010 * Request the memory region(s) being used by 'port'
2012 static int pl011_request_port(struct uart_port
*port
)
2014 return request_mem_region(port
->mapbase
, SZ_4K
, "uart-pl011")
2015 != NULL
? 0 : -EBUSY
;
2019 * Configure/autoconfigure the port.
2021 static void pl011_config_port(struct uart_port
*port
, int flags
)
2023 if (flags
& UART_CONFIG_TYPE
) {
2024 port
->type
= PORT_AMBA
;
2025 pl011_request_port(port
);
2030 * verify the new serial_struct (for TIOCSSERIAL).
2032 static int pl011_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
2035 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_AMBA
)
2037 if (ser
->irq
< 0 || ser
->irq
>= nr_irqs
)
2039 if (ser
->baud_base
< 9600)
2044 static struct uart_ops amba_pl011_pops
= {
2045 .tx_empty
= pl011_tx_empty
,
2046 .set_mctrl
= pl011_set_mctrl
,
2047 .get_mctrl
= pl011_get_mctrl
,
2048 .stop_tx
= pl011_stop_tx
,
2049 .start_tx
= pl011_start_tx
,
2050 .stop_rx
= pl011_stop_rx
,
2051 .enable_ms
= pl011_enable_ms
,
2052 .break_ctl
= pl011_break_ctl
,
2053 .startup
= pl011_startup
,
2054 .shutdown
= pl011_shutdown
,
2055 .flush_buffer
= pl011_dma_flush_buffer
,
2056 .set_termios
= pl011_set_termios
,
2058 .release_port
= pl011_release_port
,
2059 .request_port
= pl011_request_port
,
2060 .config_port
= pl011_config_port
,
2061 .verify_port
= pl011_verify_port
,
2062 #ifdef CONFIG_CONSOLE_POLL
2063 .poll_init
= pl011_hwinit
,
2064 .poll_get_char
= pl011_get_poll_char
,
2065 .poll_put_char
= pl011_put_poll_char
,
2069 static void sbsa_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
2073 static unsigned int sbsa_uart_get_mctrl(struct uart_port
*port
)
2078 static const struct uart_ops sbsa_uart_pops
= {
2079 .tx_empty
= pl011_tx_empty
,
2080 .set_mctrl
= sbsa_uart_set_mctrl
,
2081 .get_mctrl
= sbsa_uart_get_mctrl
,
2082 .stop_tx
= pl011_stop_tx
,
2083 .start_tx
= pl011_start_tx
,
2084 .stop_rx
= pl011_stop_rx
,
2085 .startup
= sbsa_uart_startup
,
2086 .shutdown
= sbsa_uart_shutdown
,
2087 .set_termios
= sbsa_uart_set_termios
,
2089 .release_port
= pl011_release_port
,
2090 .request_port
= pl011_request_port
,
2091 .config_port
= pl011_config_port
,
2092 .verify_port
= pl011_verify_port
,
2093 #ifdef CONFIG_CONSOLE_POLL
2094 .poll_init
= pl011_hwinit
,
2095 .poll_get_char
= pl011_get_poll_char
,
2096 .poll_put_char
= pl011_put_poll_char
,
2100 static struct uart_amba_port
*amba_ports
[UART_NR
];
2102 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2104 static void pl011_console_putchar(struct uart_port
*port
, int ch
)
2106 struct uart_amba_port
*uap
=
2107 container_of(port
, struct uart_amba_port
, port
);
2109 while (pl011_read(uap
, REG_FR
) & UART01x_FR_TXFF
)
2111 pl011_write(ch
, uap
, REG_DR
);
2115 pl011_console_write(struct console
*co
, const char *s
, unsigned int count
)
2117 struct uart_amba_port
*uap
= amba_ports
[co
->index
];
2118 unsigned int status
, old_cr
= 0, new_cr
;
2119 unsigned long flags
;
2122 clk_enable(uap
->clk
);
2124 local_irq_save(flags
);
2125 if (uap
->port
.sysrq
)
2127 else if (oops_in_progress
)
2128 locked
= spin_trylock(&uap
->port
.lock
);
2130 spin_lock(&uap
->port
.lock
);
2133 * First save the CR then disable the interrupts
2135 if (!uap
->vendor
->always_enabled
) {
2136 old_cr
= pl011_read(uap
, REG_CR
);
2137 new_cr
= old_cr
& ~UART011_CR_CTSEN
;
2138 new_cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
2139 pl011_write(new_cr
, uap
, REG_CR
);
2142 uart_console_write(&uap
->port
, s
, count
, pl011_console_putchar
);
2145 * Finally, wait for transmitter to become empty
2146 * and restore the TCR
2149 status
= pl011_read(uap
, REG_FR
);
2150 } while (status
& UART01x_FR_BUSY
);
2151 if (!uap
->vendor
->always_enabled
)
2152 pl011_write(old_cr
, uap
, REG_CR
);
2155 spin_unlock(&uap
->port
.lock
);
2156 local_irq_restore(flags
);
2158 clk_disable(uap
->clk
);
2162 pl011_console_get_options(struct uart_amba_port
*uap
, int *baud
,
2163 int *parity
, int *bits
)
2165 if (pl011_read(uap
, REG_CR
) & UART01x_CR_UARTEN
) {
2166 unsigned int lcr_h
, ibrd
, fbrd
;
2168 lcr_h
= pl011_read(uap
, uap
->lcrh_tx
);
2171 if (lcr_h
& UART01x_LCRH_PEN
) {
2172 if (lcr_h
& UART01x_LCRH_EPS
)
2178 if ((lcr_h
& 0x60) == UART01x_LCRH_WLEN_7
)
2183 ibrd
= pl011_read(uap
, REG_IBRD
);
2184 fbrd
= pl011_read(uap
, REG_FBRD
);
2186 *baud
= uap
->port
.uartclk
* 4 / (64 * ibrd
+ fbrd
);
2188 if (uap
->vendor
->oversampling
) {
2189 if (pl011_read(uap
, REG_CR
)
2190 & ST_UART011_CR_OVSFACT
)
2196 static int __init
pl011_console_setup(struct console
*co
, char *options
)
2198 struct uart_amba_port
*uap
;
2206 * Check whether an invalid uart number has been specified, and
2207 * if so, search for the first available port that does have
2210 if (co
->index
>= UART_NR
)
2212 uap
= amba_ports
[co
->index
];
2216 /* Allow pins to be muxed in and configured */
2217 pinctrl_pm_select_default_state(uap
->port
.dev
);
2219 ret
= clk_prepare(uap
->clk
);
2223 if (dev_get_platdata(uap
->port
.dev
)) {
2224 struct amba_pl011_data
*plat
;
2226 plat
= dev_get_platdata(uap
->port
.dev
);
2231 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
2233 if (uap
->vendor
->fixed_options
) {
2234 baud
= uap
->fixed_baud
;
2237 uart_parse_options(options
,
2238 &baud
, &parity
, &bits
, &flow
);
2240 pl011_console_get_options(uap
, &baud
, &parity
, &bits
);
2243 return uart_set_options(&uap
->port
, co
, baud
, parity
, bits
, flow
);
2246 static struct uart_driver amba_reg
;
2247 static struct console amba_console
= {
2249 .write
= pl011_console_write
,
2250 .device
= uart_console_device
,
2251 .setup
= pl011_console_setup
,
2252 .flags
= CON_PRINTBUFFER
,
2257 #define AMBA_CONSOLE (&amba_console)
2259 static void pl011_putc(struct uart_port
*port
, int c
)
2261 while (readl(port
->membase
+ REG_FR
) & UART01x_FR_TXFF
)
2263 writeb(c
, port
->membase
+ REG_DR
);
2264 while (readl(port
->membase
+ REG_FR
) & UART01x_FR_BUSY
)
2268 static void pl011_early_write(struct console
*con
, const char *s
, unsigned n
)
2270 struct earlycon_device
*dev
= con
->data
;
2272 uart_console_write(&dev
->port
, s
, n
, pl011_putc
);
2275 static int __init
pl011_early_console_setup(struct earlycon_device
*device
,
2278 if (!device
->port
.membase
)
2281 device
->con
->write
= pl011_early_write
;
2284 EARLYCON_DECLARE(pl011
, pl011_early_console_setup
);
2285 OF_EARLYCON_DECLARE(pl011
, "arm,pl011", pl011_early_console_setup
);
2288 #define AMBA_CONSOLE NULL
2291 static struct uart_driver amba_reg
= {
2292 .owner
= THIS_MODULE
,
2293 .driver_name
= "ttyAMA",
2294 .dev_name
= "ttyAMA",
2295 .major
= SERIAL_AMBA_MAJOR
,
2296 .minor
= SERIAL_AMBA_MINOR
,
2298 .cons
= AMBA_CONSOLE
,
2301 static int pl011_probe_dt_alias(int index
, struct device
*dev
)
2303 struct device_node
*np
;
2304 static bool seen_dev_with_alias
= false;
2305 static bool seen_dev_without_alias
= false;
2308 if (!IS_ENABLED(CONFIG_OF
))
2315 ret
= of_alias_get_id(np
, "serial");
2316 if (IS_ERR_VALUE(ret
)) {
2317 seen_dev_without_alias
= true;
2320 seen_dev_with_alias
= true;
2321 if (ret
>= ARRAY_SIZE(amba_ports
) || amba_ports
[ret
] != NULL
) {
2322 dev_warn(dev
, "requested serial port %d not available.\n", ret
);
2327 if (seen_dev_with_alias
&& seen_dev_without_alias
)
2328 dev_warn(dev
, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2333 /* unregisters the driver also if no more ports are left */
2334 static void pl011_unregister_port(struct uart_amba_port
*uap
)
2339 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++) {
2340 if (amba_ports
[i
] == uap
)
2341 amba_ports
[i
] = NULL
;
2342 else if (amba_ports
[i
])
2345 pl011_dma_remove(uap
);
2347 uart_unregister_driver(&amba_reg
);
2350 static int pl011_find_free_port(void)
2354 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++)
2355 if (amba_ports
[i
] == NULL
)
2361 static int pl011_setup_port(struct device
*dev
, struct uart_amba_port
*uap
,
2362 struct resource
*mmiobase
, int index
)
2366 base
= devm_ioremap_resource(dev
, mmiobase
);
2368 return PTR_ERR(base
);
2370 index
= pl011_probe_dt_alias(index
, dev
);
2373 uap
->port
.dev
= dev
;
2374 uap
->port
.mapbase
= mmiobase
->start
;
2375 uap
->port
.membase
= base
;
2376 uap
->port
.iotype
= UPIO_MEM
;
2377 uap
->port
.fifosize
= uap
->fifosize
;
2378 uap
->port
.flags
= UPF_BOOT_AUTOCONF
;
2379 uap
->port
.line
= index
;
2381 amba_ports
[index
] = uap
;
2386 static int pl011_register_port(struct uart_amba_port
*uap
)
2390 /* Ensure interrupts from this UART are masked and cleared */
2391 pl011_write(0, uap
, REG_IMSC
);
2392 pl011_write(0xffff, uap
, REG_ICR
);
2394 if (!amba_reg
.state
) {
2395 ret
= uart_register_driver(&amba_reg
);
2397 dev_err(uap
->port
.dev
,
2398 "Failed to register AMBA-PL011 driver\n");
2403 ret
= uart_add_one_port(&amba_reg
, &uap
->port
);
2405 pl011_unregister_port(uap
);
2410 static int pl011_probe(struct amba_device
*dev
, const struct amba_id
*id
)
2412 struct uart_amba_port
*uap
;
2413 struct vendor_data
*vendor
= id
->data
;
2416 portnr
= pl011_find_free_port();
2420 uap
= devm_kzalloc(&dev
->dev
, sizeof(struct uart_amba_port
),
2425 uap
->clk
= devm_clk_get(&dev
->dev
, NULL
);
2426 if (IS_ERR(uap
->clk
))
2427 return PTR_ERR(uap
->clk
);
2429 uap
->reg_offset
= pl011_std_offsets
;
2430 uap
->vendor
= vendor
;
2431 uap
->lcrh_rx
= vendor
->lcrh_rx
;
2432 uap
->lcrh_tx
= vendor
->lcrh_tx
;
2433 uap
->fifosize
= vendor
->get_fifosize(dev
);
2434 uap
->port
.irq
= dev
->irq
[0];
2435 uap
->port
.ops
= &amba_pl011_pops
;
2437 snprintf(uap
->type
, sizeof(uap
->type
), "PL011 rev%u", amba_rev(dev
));
2439 ret
= pl011_setup_port(&dev
->dev
, uap
, &dev
->res
, portnr
);
2443 amba_set_drvdata(dev
, uap
);
2445 return pl011_register_port(uap
);
2448 static int pl011_remove(struct amba_device
*dev
)
2450 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
2452 uart_remove_one_port(&amba_reg
, &uap
->port
);
2453 pl011_unregister_port(uap
);
2457 #ifdef CONFIG_PM_SLEEP
2458 static int pl011_suspend(struct device
*dev
)
2460 struct uart_amba_port
*uap
= dev_get_drvdata(dev
);
2465 return uart_suspend_port(&amba_reg
, &uap
->port
);
2468 static int pl011_resume(struct device
*dev
)
2470 struct uart_amba_port
*uap
= dev_get_drvdata(dev
);
2475 return uart_resume_port(&amba_reg
, &uap
->port
);
2479 static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops
, pl011_suspend
, pl011_resume
);
2481 static int sbsa_uart_probe(struct platform_device
*pdev
)
2483 struct uart_amba_port
*uap
;
2489 * Check the mandatory baud rate parameter in the DT node early
2490 * so that we can easily exit with the error.
2492 if (pdev
->dev
.of_node
) {
2493 struct device_node
*np
= pdev
->dev
.of_node
;
2495 ret
= of_property_read_u32(np
, "current-speed", &baudrate
);
2502 portnr
= pl011_find_free_port();
2506 uap
= devm_kzalloc(&pdev
->dev
, sizeof(struct uart_amba_port
),
2511 uap
->reg_offset
= pl011_std_offsets
;
2512 uap
->vendor
= &vendor_sbsa
;
2514 uap
->port
.irq
= platform_get_irq(pdev
, 0);
2515 uap
->port
.ops
= &sbsa_uart_pops
;
2516 uap
->fixed_baud
= baudrate
;
2518 snprintf(uap
->type
, sizeof(uap
->type
), "SBSA");
2520 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2522 ret
= pl011_setup_port(&pdev
->dev
, uap
, r
, portnr
);
2526 platform_set_drvdata(pdev
, uap
);
2528 return pl011_register_port(uap
);
2531 static int sbsa_uart_remove(struct platform_device
*pdev
)
2533 struct uart_amba_port
*uap
= platform_get_drvdata(pdev
);
2535 uart_remove_one_port(&amba_reg
, &uap
->port
);
2536 pl011_unregister_port(uap
);
2540 static const struct of_device_id sbsa_uart_of_match
[] = {
2541 { .compatible
= "arm,sbsa-uart", },
2544 MODULE_DEVICE_TABLE(of
, sbsa_uart_of_match
);
2546 static const struct acpi_device_id sbsa_uart_acpi_match
[] = {
2550 MODULE_DEVICE_TABLE(acpi
, sbsa_uart_acpi_match
);
2552 static struct platform_driver arm_sbsa_uart_platform_driver
= {
2553 .probe
= sbsa_uart_probe
,
2554 .remove
= sbsa_uart_remove
,
2556 .name
= "sbsa-uart",
2557 .of_match_table
= of_match_ptr(sbsa_uart_of_match
),
2558 .acpi_match_table
= ACPI_PTR(sbsa_uart_acpi_match
),
2562 static struct amba_id pl011_ids
[] = {
2566 .data
= &vendor_arm
,
2576 MODULE_DEVICE_TABLE(amba
, pl011_ids
);
2578 static struct amba_driver pl011_driver
= {
2580 .name
= "uart-pl011",
2581 .pm
= &pl011_dev_pm_ops
,
2583 .id_table
= pl011_ids
,
2584 .probe
= pl011_probe
,
2585 .remove
= pl011_remove
,
2588 static int __init
pl011_init(void)
2590 printk(KERN_INFO
"Serial: AMBA PL011 UART driver\n");
2592 if (platform_driver_register(&arm_sbsa_uart_platform_driver
))
2593 pr_warn("could not register SBSA UART platform driver\n");
2594 return amba_driver_register(&pl011_driver
);
2597 static void __exit
pl011_exit(void)
2599 platform_driver_unregister(&arm_sbsa_uart_platform_driver
);
2600 amba_driver_unregister(&pl011_driver
);
2604 * While this can be a module, if builtin it's most likely the console
2605 * So let's leave module_exit but move module_init to an earlier place
2607 arch_initcall(pl011_init
);
2608 module_exit(pl011_exit
);
2610 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2611 MODULE_DESCRIPTION("ARM AMBA serial port driver");
2612 MODULE_LICENSE("GPL");