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.
32 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/module.h>
37 #include <linux/ioport.h>
38 #include <linux/init.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/device.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/serial_core.h>
45 #include <linux/serial.h>
46 #include <linux/amba/bus.h>
47 #include <linux/amba/serial.h>
48 #include <linux/clk.h>
49 #include <linux/slab.h>
50 #include <linux/dmaengine.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/scatterlist.h>
53 #include <linux/delay.h>
54 #include <linux/types.h>
56 #include <linux/of_device.h>
57 #include <linux/pinctrl/consumer.h>
58 #include <linux/sizes.h>
64 #define SERIAL_AMBA_MAJOR 204
65 #define SERIAL_AMBA_MINOR 64
66 #define SERIAL_AMBA_NR UART_NR
68 #define AMBA_ISR_PASS_LIMIT 256
70 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
71 #define UART_DUMMY_DR_RX (1 << 16)
73 /* There is by now at least one vendor with differing details, so handle it */
76 unsigned int fifosize
;
80 bool interrupt_may_hang
; /* vendor-specific */
82 bool cts_event_workaround
;
85 static struct vendor_data vendor_arm
= {
86 .ifls
= UART011_IFLS_RX4_8
|UART011_IFLS_TX4_8
,
88 .lcrh_tx
= UART011_LCRH
,
89 .lcrh_rx
= UART011_LCRH
,
90 .oversampling
= false,
91 .dma_threshold
= false,
92 .cts_event_workaround
= false,
95 static struct vendor_data vendor_st
= {
96 .ifls
= UART011_IFLS_RX_HALF
|UART011_IFLS_TX_HALF
,
98 .lcrh_tx
= ST_UART011_LCRH_TX
,
99 .lcrh_rx
= ST_UART011_LCRH_RX
,
100 .oversampling
= true,
101 .interrupt_may_hang
= true,
102 .dma_threshold
= true,
103 .cts_event_workaround
= true,
106 static struct uart_amba_port
*amba_ports
[UART_NR
];
108 /* Deals with DMA transactions */
111 struct scatterlist sg
;
115 struct pl011_dmarx_data
{
116 struct dma_chan
*chan
;
117 struct completion complete
;
119 struct pl011_sgbuf sgbuf_a
;
120 struct pl011_sgbuf sgbuf_b
;
125 struct pl011_dmatx_data
{
126 struct dma_chan
*chan
;
127 struct scatterlist sg
;
133 * We wrap our port structure around the generic uart_port.
135 struct uart_amba_port
{
136 struct uart_port port
;
138 /* Two optional pin states - default & sleep */
139 struct pinctrl
*pinctrl
;
140 struct pinctrl_state
*pins_default
;
141 struct pinctrl_state
*pins_sleep
;
142 const struct vendor_data
*vendor
;
143 unsigned int dmacr
; /* dma control reg */
144 unsigned int im
; /* interrupt mask */
145 unsigned int old_status
;
146 unsigned int fifosize
; /* vendor-specific */
147 unsigned int lcrh_tx
; /* vendor-specific */
148 unsigned int lcrh_rx
; /* vendor-specific */
149 unsigned int old_cr
; /* state during shutdown */
152 bool interrupt_may_hang
; /* vendor-specific */
153 #ifdef CONFIG_DMA_ENGINE
157 struct pl011_dmarx_data dmarx
;
158 struct pl011_dmatx_data dmatx
;
163 * Reads up to 256 characters from the FIFO or until it's empty and
164 * inserts them into the TTY layer. Returns the number of characters
165 * read from the FIFO.
167 static int pl011_fifo_to_tty(struct uart_amba_port
*uap
)
170 unsigned int flag
, max_count
= 256;
173 while (max_count
--) {
174 status
= readw(uap
->port
.membase
+ UART01x_FR
);
175 if (status
& UART01x_FR_RXFE
)
178 /* Take chars from the FIFO and update status */
179 ch
= readw(uap
->port
.membase
+ UART01x_DR
) |
182 uap
->port
.icount
.rx
++;
185 if (unlikely(ch
& UART_DR_ERROR
)) {
186 if (ch
& UART011_DR_BE
) {
187 ch
&= ~(UART011_DR_FE
| UART011_DR_PE
);
188 uap
->port
.icount
.brk
++;
189 if (uart_handle_break(&uap
->port
))
191 } else if (ch
& UART011_DR_PE
)
192 uap
->port
.icount
.parity
++;
193 else if (ch
& UART011_DR_FE
)
194 uap
->port
.icount
.frame
++;
195 if (ch
& UART011_DR_OE
)
196 uap
->port
.icount
.overrun
++;
198 ch
&= uap
->port
.read_status_mask
;
200 if (ch
& UART011_DR_BE
)
202 else if (ch
& UART011_DR_PE
)
204 else if (ch
& UART011_DR_FE
)
208 if (uart_handle_sysrq_char(&uap
->port
, ch
& 255))
211 uart_insert_char(&uap
->port
, ch
, UART011_DR_OE
, ch
, flag
);
219 * All the DMA operation mode stuff goes inside this ifdef.
220 * This assumes that you have a generic DMA device interface,
221 * no custom DMA interfaces are supported.
223 #ifdef CONFIG_DMA_ENGINE
225 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
227 static int pl011_sgbuf_init(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
228 enum dma_data_direction dir
)
230 sg
->buf
= kmalloc(PL011_DMA_BUFFER_SIZE
, GFP_KERNEL
);
234 sg_init_one(&sg
->sg
, sg
->buf
, PL011_DMA_BUFFER_SIZE
);
236 if (dma_map_sg(chan
->device
->dev
, &sg
->sg
, 1, dir
) != 1) {
243 static void pl011_sgbuf_free(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
244 enum dma_data_direction dir
)
247 dma_unmap_sg(chan
->device
->dev
, &sg
->sg
, 1, dir
);
252 static void pl011_dma_probe_initcall(struct uart_amba_port
*uap
)
254 /* DMA is the sole user of the platform data right now */
255 struct amba_pl011_data
*plat
= uap
->port
.dev
->platform_data
;
256 struct dma_slave_config tx_conf
= {
257 .dst_addr
= uap
->port
.mapbase
+ UART01x_DR
,
258 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
259 .direction
= DMA_MEM_TO_DEV
,
260 .dst_maxburst
= uap
->fifosize
>> 1,
263 struct dma_chan
*chan
;
266 /* We need platform data */
267 if (!plat
|| !plat
->dma_filter
) {
268 dev_info(uap
->port
.dev
, "no DMA platform data\n");
272 /* Try to acquire a generic DMA engine slave TX channel */
274 dma_cap_set(DMA_SLAVE
, mask
);
276 chan
= dma_request_channel(mask
, plat
->dma_filter
, plat
->dma_tx_param
);
278 dev_err(uap
->port
.dev
, "no TX DMA channel!\n");
282 dmaengine_slave_config(chan
, &tx_conf
);
283 uap
->dmatx
.chan
= chan
;
285 dev_info(uap
->port
.dev
, "DMA channel TX %s\n",
286 dma_chan_name(uap
->dmatx
.chan
));
288 /* Optionally make use of an RX channel as well */
289 if (plat
->dma_rx_param
) {
290 struct dma_slave_config rx_conf
= {
291 .src_addr
= uap
->port
.mapbase
+ UART01x_DR
,
292 .src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
293 .direction
= DMA_DEV_TO_MEM
,
294 .src_maxburst
= uap
->fifosize
>> 1,
298 chan
= dma_request_channel(mask
, plat
->dma_filter
, plat
->dma_rx_param
);
300 dev_err(uap
->port
.dev
, "no RX DMA channel!\n");
304 dmaengine_slave_config(chan
, &rx_conf
);
305 uap
->dmarx
.chan
= chan
;
307 dev_info(uap
->port
.dev
, "DMA channel RX %s\n",
308 dma_chan_name(uap
->dmarx
.chan
));
314 * Stack up the UARTs and let the above initcall be done at device
315 * initcall time, because the serial driver is called as an arch
316 * initcall, and at this time the DMA subsystem is not yet registered.
317 * At this point the driver will switch over to using DMA where desired.
320 struct list_head node
;
321 struct uart_amba_port
*uap
;
324 static LIST_HEAD(pl011_dma_uarts
);
326 static int __init
pl011_dma_initcall(void)
328 struct list_head
*node
, *tmp
;
330 list_for_each_safe(node
, tmp
, &pl011_dma_uarts
) {
331 struct dma_uap
*dmau
= list_entry(node
, struct dma_uap
, node
);
332 pl011_dma_probe_initcall(dmau
->uap
);
339 device_initcall(pl011_dma_initcall
);
341 static void pl011_dma_probe(struct uart_amba_port
*uap
)
343 struct dma_uap
*dmau
= kzalloc(sizeof(struct dma_uap
), GFP_KERNEL
);
346 list_add_tail(&dmau
->node
, &pl011_dma_uarts
);
350 static void pl011_dma_probe(struct uart_amba_port
*uap
)
352 pl011_dma_probe_initcall(uap
);
356 static void pl011_dma_remove(struct uart_amba_port
*uap
)
358 /* TODO: remove the initcall if it has not yet executed */
360 dma_release_channel(uap
->dmatx
.chan
);
362 dma_release_channel(uap
->dmarx
.chan
);
365 /* Forward declare this for the refill routine */
366 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
);
369 * The current DMA TX buffer has been sent.
370 * Try to queue up another DMA buffer.
372 static void pl011_dma_tx_callback(void *data
)
374 struct uart_amba_port
*uap
= data
;
375 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
379 spin_lock_irqsave(&uap
->port
.lock
, flags
);
380 if (uap
->dmatx
.queued
)
381 dma_unmap_sg(dmatx
->chan
->device
->dev
, &dmatx
->sg
, 1,
385 uap
->dmacr
= dmacr
& ~UART011_TXDMAE
;
386 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
389 * If TX DMA was disabled, it means that we've stopped the DMA for
390 * some reason (eg, XOFF received, or we want to send an X-char.)
392 * Note: we need to be careful here of a potential race between DMA
393 * and the rest of the driver - if the driver disables TX DMA while
394 * a TX buffer completing, we must update the tx queued status to
395 * get further refills (hence we check dmacr).
397 if (!(dmacr
& UART011_TXDMAE
) || uart_tx_stopped(&uap
->port
) ||
398 uart_circ_empty(&uap
->port
.state
->xmit
)) {
399 uap
->dmatx
.queued
= false;
400 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
404 if (pl011_dma_tx_refill(uap
) <= 0) {
406 * We didn't queue a DMA buffer for some reason, but we
407 * have data pending to be sent. Re-enable the TX IRQ.
409 uap
->im
|= UART011_TXIM
;
410 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
412 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
416 * Try to refill the TX DMA buffer.
417 * Locking: called with port lock held and IRQs disabled.
419 * 1 if we queued up a TX DMA buffer.
420 * 0 if we didn't want to handle this by DMA
423 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
)
425 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
426 struct dma_chan
*chan
= dmatx
->chan
;
427 struct dma_device
*dma_dev
= chan
->device
;
428 struct dma_async_tx_descriptor
*desc
;
429 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
433 * Try to avoid the overhead involved in using DMA if the
434 * transaction fits in the first half of the FIFO, by using
435 * the standard interrupt handling. This ensures that we
436 * issue a uart_write_wakeup() at the appropriate time.
438 count
= uart_circ_chars_pending(xmit
);
439 if (count
< (uap
->fifosize
>> 1)) {
440 uap
->dmatx
.queued
= false;
445 * Bodge: don't send the last character by DMA, as this
446 * will prevent XON from notifying us to restart DMA.
450 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
451 if (count
> PL011_DMA_BUFFER_SIZE
)
452 count
= PL011_DMA_BUFFER_SIZE
;
454 if (xmit
->tail
< xmit
->head
)
455 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], count
);
457 size_t first
= UART_XMIT_SIZE
- xmit
->tail
;
458 size_t second
= xmit
->head
;
460 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], first
);
462 memcpy(&dmatx
->buf
[first
], &xmit
->buf
[0], second
);
465 dmatx
->sg
.length
= count
;
467 if (dma_map_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
) != 1) {
468 uap
->dmatx
.queued
= false;
469 dev_dbg(uap
->port
.dev
, "unable to map TX DMA\n");
473 desc
= dmaengine_prep_slave_sg(chan
, &dmatx
->sg
, 1, DMA_MEM_TO_DEV
,
474 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
476 dma_unmap_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
);
477 uap
->dmatx
.queued
= false;
479 * If DMA cannot be used right now, we complete this
480 * transaction via IRQ and let the TTY layer retry.
482 dev_dbg(uap
->port
.dev
, "TX DMA busy\n");
486 /* Some data to go along to the callback */
487 desc
->callback
= pl011_dma_tx_callback
;
488 desc
->callback_param
= uap
;
490 /* All errors should happen at prepare time */
491 dmaengine_submit(desc
);
493 /* Fire the DMA transaction */
494 dma_dev
->device_issue_pending(chan
);
496 uap
->dmacr
|= UART011_TXDMAE
;
497 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
498 uap
->dmatx
.queued
= true;
501 * Now we know that DMA will fire, so advance the ring buffer
502 * with the stuff we just dispatched.
504 xmit
->tail
= (xmit
->tail
+ count
) & (UART_XMIT_SIZE
- 1);
505 uap
->port
.icount
.tx
+= count
;
507 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
508 uart_write_wakeup(&uap
->port
);
514 * We received a transmit interrupt without a pending X-char but with
515 * pending characters.
516 * Locking: called with port lock held and IRQs disabled.
518 * false if we want to use PIO to transmit
519 * true if we queued a DMA buffer
521 static bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
523 if (!uap
->using_tx_dma
)
527 * If we already have a TX buffer queued, but received a
528 * TX interrupt, it will be because we've just sent an X-char.
529 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
531 if (uap
->dmatx
.queued
) {
532 uap
->dmacr
|= UART011_TXDMAE
;
533 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
534 uap
->im
&= ~UART011_TXIM
;
535 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
540 * We don't have a TX buffer queued, so try to queue one.
541 * If we successfully queued a buffer, mask the TX IRQ.
543 if (pl011_dma_tx_refill(uap
) > 0) {
544 uap
->im
&= ~UART011_TXIM
;
545 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
552 * Stop the DMA transmit (eg, due to received XOFF).
553 * Locking: called with port lock held and IRQs disabled.
555 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
557 if (uap
->dmatx
.queued
) {
558 uap
->dmacr
&= ~UART011_TXDMAE
;
559 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
564 * Try to start a DMA transmit, or in the case of an XON/OFF
565 * character queued for send, try to get that character out ASAP.
566 * Locking: called with port lock held and IRQs disabled.
568 * false if we want the TX IRQ to be enabled
569 * true if we have a buffer queued
571 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
575 if (!uap
->using_tx_dma
)
578 if (!uap
->port
.x_char
) {
579 /* no X-char, try to push chars out in DMA mode */
582 if (!uap
->dmatx
.queued
) {
583 if (pl011_dma_tx_refill(uap
) > 0) {
584 uap
->im
&= ~UART011_TXIM
;
587 uap
->im
|= UART011_TXIM
;
590 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
591 } else if (!(uap
->dmacr
& UART011_TXDMAE
)) {
592 uap
->dmacr
|= UART011_TXDMAE
;
594 uap
->port
.membase
+ UART011_DMACR
);
600 * We have an X-char to send. Disable DMA to prevent it loading
601 * the TX fifo, and then see if we can stuff it into the FIFO.
604 uap
->dmacr
&= ~UART011_TXDMAE
;
605 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
607 if (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
) {
609 * No space in the FIFO, so enable the transmit interrupt
610 * so we know when there is space. Note that once we've
611 * loaded the character, we should just re-enable DMA.
616 writew(uap
->port
.x_char
, uap
->port
.membase
+ UART01x_DR
);
617 uap
->port
.icount
.tx
++;
618 uap
->port
.x_char
= 0;
620 /* Success - restore the DMA state */
622 writew(dmacr
, uap
->port
.membase
+ UART011_DMACR
);
628 * Flush the transmit buffer.
629 * Locking: called with port lock held and IRQs disabled.
631 static void pl011_dma_flush_buffer(struct uart_port
*port
)
633 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
635 if (!uap
->using_tx_dma
)
638 /* Avoid deadlock with the DMA engine callback */
639 spin_unlock(&uap
->port
.lock
);
640 dmaengine_terminate_all(uap
->dmatx
.chan
);
641 spin_lock(&uap
->port
.lock
);
642 if (uap
->dmatx
.queued
) {
643 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
645 uap
->dmatx
.queued
= false;
646 uap
->dmacr
&= ~UART011_TXDMAE
;
647 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
651 static void pl011_dma_rx_callback(void *data
);
653 static int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
655 struct dma_chan
*rxchan
= uap
->dmarx
.chan
;
656 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
657 struct dma_async_tx_descriptor
*desc
;
658 struct pl011_sgbuf
*sgbuf
;
663 /* Start the RX DMA job */
664 sgbuf
= uap
->dmarx
.use_buf_b
?
665 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
666 desc
= dmaengine_prep_slave_sg(rxchan
, &sgbuf
->sg
, 1,
668 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
670 * If the DMA engine is busy and cannot prepare a
671 * channel, no big deal, the driver will fall back
672 * to interrupt mode as a result of this error code.
675 uap
->dmarx
.running
= false;
676 dmaengine_terminate_all(rxchan
);
680 /* Some data to go along to the callback */
681 desc
->callback
= pl011_dma_rx_callback
;
682 desc
->callback_param
= uap
;
683 dmarx
->cookie
= dmaengine_submit(desc
);
684 dma_async_issue_pending(rxchan
);
686 uap
->dmacr
|= UART011_RXDMAE
;
687 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
688 uap
->dmarx
.running
= true;
690 uap
->im
&= ~UART011_RXIM
;
691 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
697 * This is called when either the DMA job is complete, or
698 * the FIFO timeout interrupt occurred. This must be called
699 * with the port spinlock uap->port.lock held.
701 static void pl011_dma_rx_chars(struct uart_amba_port
*uap
,
702 u32 pending
, bool use_buf_b
,
705 struct tty_struct
*tty
= uap
->port
.state
->port
.tty
;
706 struct pl011_sgbuf
*sgbuf
= use_buf_b
?
707 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
708 struct device
*dev
= uap
->dmarx
.chan
->device
->dev
;
710 u32 fifotaken
= 0; /* only used for vdbg() */
712 /* Pick everything from the DMA first */
715 dma_sync_sg_for_cpu(dev
, &sgbuf
->sg
, 1, DMA_FROM_DEVICE
);
718 * First take all chars in the DMA pipe, then look in the FIFO.
719 * Note that tty_insert_flip_buf() tries to take as many chars
722 dma_count
= tty_insert_flip_string(uap
->port
.state
->port
.tty
,
723 sgbuf
->buf
, pending
);
725 /* Return buffer to device */
726 dma_sync_sg_for_device(dev
, &sgbuf
->sg
, 1, DMA_FROM_DEVICE
);
728 uap
->port
.icount
.rx
+= dma_count
;
729 if (dma_count
< pending
)
730 dev_warn(uap
->port
.dev
,
731 "couldn't insert all characters (TTY is full?)\n");
735 * Only continue with trying to read the FIFO if all DMA chars have
738 if (dma_count
== pending
&& readfifo
) {
739 /* Clear any error flags */
740 writew(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
| UART011_FEIS
,
741 uap
->port
.membase
+ UART011_ICR
);
744 * If we read all the DMA'd characters, and we had an
745 * incomplete buffer, that could be due to an rx error, or
746 * maybe we just timed out. Read any pending chars and check
749 * Error conditions will only occur in the FIFO, these will
750 * trigger an immediate interrupt and stop the DMA job, so we
751 * will always find the error in the FIFO, never in the DMA
754 fifotaken
= pl011_fifo_to_tty(uap
);
757 spin_unlock(&uap
->port
.lock
);
758 dev_vdbg(uap
->port
.dev
,
759 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
760 dma_count
, fifotaken
);
761 tty_flip_buffer_push(tty
);
762 spin_lock(&uap
->port
.lock
);
765 static void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
767 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
768 struct dma_chan
*rxchan
= dmarx
->chan
;
769 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
770 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
772 struct dma_tx_state state
;
773 enum dma_status dmastat
;
776 * Pause the transfer so we can trust the current counter,
777 * do this before we pause the PL011 block, else we may
780 if (dmaengine_pause(rxchan
))
781 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
782 dmastat
= rxchan
->device
->device_tx_status(rxchan
,
783 dmarx
->cookie
, &state
);
784 if (dmastat
!= DMA_PAUSED
)
785 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
787 /* Disable RX DMA - incoming data will wait in the FIFO */
788 uap
->dmacr
&= ~UART011_RXDMAE
;
789 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
790 uap
->dmarx
.running
= false;
792 pending
= sgbuf
->sg
.length
- state
.residue
;
793 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
794 /* Then we terminate the transfer - we now know our residue */
795 dmaengine_terminate_all(rxchan
);
798 * This will take the chars we have so far and insert
799 * into the framework.
801 pl011_dma_rx_chars(uap
, pending
, dmarx
->use_buf_b
, true);
803 /* Switch buffer & re-trigger DMA job */
804 dmarx
->use_buf_b
= !dmarx
->use_buf_b
;
805 if (pl011_dma_rx_trigger_dma(uap
)) {
806 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
807 "fall back to interrupt mode\n");
808 uap
->im
|= UART011_RXIM
;
809 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
813 static void pl011_dma_rx_callback(void *data
)
815 struct uart_amba_port
*uap
= data
;
816 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
817 struct dma_chan
*rxchan
= dmarx
->chan
;
818 bool lastbuf
= dmarx
->use_buf_b
;
819 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
820 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
822 struct dma_tx_state state
;
826 * This completion interrupt occurs typically when the
827 * RX buffer is totally stuffed but no timeout has yet
828 * occurred. When that happens, we just want the RX
829 * routine to flush out the secondary DMA buffer while
830 * we immediately trigger the next DMA job.
832 spin_lock_irq(&uap
->port
.lock
);
834 * Rx data can be taken by the UART interrupts during
835 * the DMA irq handler. So we check the residue here.
837 rxchan
->device
->device_tx_status(rxchan
, dmarx
->cookie
, &state
);
838 pending
= sgbuf
->sg
.length
- state
.residue
;
839 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
840 /* Then we terminate the transfer - we now know our residue */
841 dmaengine_terminate_all(rxchan
);
843 uap
->dmarx
.running
= false;
844 dmarx
->use_buf_b
= !lastbuf
;
845 ret
= pl011_dma_rx_trigger_dma(uap
);
847 pl011_dma_rx_chars(uap
, pending
, lastbuf
, false);
848 spin_unlock_irq(&uap
->port
.lock
);
850 * Do this check after we picked the DMA chars so we don't
851 * get some IRQ immediately from RX.
854 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
855 "fall back to interrupt mode\n");
856 uap
->im
|= UART011_RXIM
;
857 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
862 * Stop accepting received characters, when we're shutting down or
863 * suspending this port.
864 * Locking: called with port lock held and IRQs disabled.
866 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
868 /* FIXME. Just disable the DMA enable */
869 uap
->dmacr
&= ~UART011_RXDMAE
;
870 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
873 static void pl011_dma_startup(struct uart_amba_port
*uap
)
877 if (!uap
->dmatx
.chan
)
880 uap
->dmatx
.buf
= kmalloc(PL011_DMA_BUFFER_SIZE
, GFP_KERNEL
);
881 if (!uap
->dmatx
.buf
) {
882 dev_err(uap
->port
.dev
, "no memory for DMA TX buffer\n");
883 uap
->port
.fifosize
= uap
->fifosize
;
887 sg_init_one(&uap
->dmatx
.sg
, uap
->dmatx
.buf
, PL011_DMA_BUFFER_SIZE
);
889 /* The DMA buffer is now the FIFO the TTY subsystem can use */
890 uap
->port
.fifosize
= PL011_DMA_BUFFER_SIZE
;
891 uap
->using_tx_dma
= true;
893 if (!uap
->dmarx
.chan
)
896 /* Allocate and map DMA RX buffers */
897 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
900 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
905 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
,
908 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
910 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
915 uap
->using_rx_dma
= true;
918 /* Turn on DMA error (RX/TX will be enabled on demand) */
919 uap
->dmacr
|= UART011_DMAONERR
;
920 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
923 * ST Micro variants has some specific dma burst threshold
924 * compensation. Set this to 16 bytes, so burst will only
925 * be issued above/below 16 bytes.
927 if (uap
->vendor
->dma_threshold
)
928 writew(ST_UART011_DMAWM_RX_16
| ST_UART011_DMAWM_TX_16
,
929 uap
->port
.membase
+ ST_UART011_DMAWM
);
931 if (uap
->using_rx_dma
) {
932 if (pl011_dma_rx_trigger_dma(uap
))
933 dev_dbg(uap
->port
.dev
, "could not trigger initial "
934 "RX DMA job, fall back to interrupt mode\n");
938 static void pl011_dma_shutdown(struct uart_amba_port
*uap
)
940 if (!(uap
->using_tx_dma
|| uap
->using_rx_dma
))
943 /* Disable RX and TX DMA */
944 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_BUSY
)
947 spin_lock_irq(&uap
->port
.lock
);
948 uap
->dmacr
&= ~(UART011_DMAONERR
| UART011_RXDMAE
| UART011_TXDMAE
);
949 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
950 spin_unlock_irq(&uap
->port
.lock
);
952 if (uap
->using_tx_dma
) {
953 /* In theory, this should already be done by pl011_dma_flush_buffer */
954 dmaengine_terminate_all(uap
->dmatx
.chan
);
955 if (uap
->dmatx
.queued
) {
956 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
958 uap
->dmatx
.queued
= false;
961 kfree(uap
->dmatx
.buf
);
962 uap
->using_tx_dma
= false;
965 if (uap
->using_rx_dma
) {
966 dmaengine_terminate_all(uap
->dmarx
.chan
);
967 /* Clean up the RX DMA */
968 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
, DMA_FROM_DEVICE
);
969 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
, DMA_FROM_DEVICE
);
970 uap
->using_rx_dma
= false;
974 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
976 return uap
->using_rx_dma
;
979 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
981 return uap
->using_rx_dma
&& uap
->dmarx
.running
;
986 /* Blank functions if the DMA engine is not available */
987 static inline void pl011_dma_probe(struct uart_amba_port
*uap
)
991 static inline void pl011_dma_remove(struct uart_amba_port
*uap
)
995 static inline void pl011_dma_startup(struct uart_amba_port
*uap
)
999 static inline void pl011_dma_shutdown(struct uart_amba_port
*uap
)
1003 static inline bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
1008 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
1012 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
1017 static inline void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
1021 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
1025 static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
1030 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
1035 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
1040 #define pl011_dma_flush_buffer NULL
1043 static void pl011_stop_tx(struct uart_port
*port
)
1045 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1047 uap
->im
&= ~UART011_TXIM
;
1048 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1049 pl011_dma_tx_stop(uap
);
1052 static void pl011_start_tx(struct uart_port
*port
)
1054 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1056 if (!pl011_dma_tx_start(uap
)) {
1057 uap
->im
|= UART011_TXIM
;
1058 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1062 static void pl011_stop_rx(struct uart_port
*port
)
1064 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1066 uap
->im
&= ~(UART011_RXIM
|UART011_RTIM
|UART011_FEIM
|
1067 UART011_PEIM
|UART011_BEIM
|UART011_OEIM
);
1068 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1070 pl011_dma_rx_stop(uap
);
1073 static void pl011_enable_ms(struct uart_port
*port
)
1075 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1077 uap
->im
|= UART011_RIMIM
|UART011_CTSMIM
|UART011_DCDMIM
|UART011_DSRMIM
;
1078 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1081 static void pl011_rx_chars(struct uart_amba_port
*uap
)
1083 struct tty_struct
*tty
= uap
->port
.state
->port
.tty
;
1085 pl011_fifo_to_tty(uap
);
1087 spin_unlock(&uap
->port
.lock
);
1088 tty_flip_buffer_push(tty
);
1090 * If we were temporarily out of DMA mode for a while,
1091 * attempt to switch back to DMA mode again.
1093 if (pl011_dma_rx_available(uap
)) {
1094 if (pl011_dma_rx_trigger_dma(uap
)) {
1095 dev_dbg(uap
->port
.dev
, "could not trigger RX DMA job "
1096 "fall back to interrupt mode again\n");
1097 uap
->im
|= UART011_RXIM
;
1099 uap
->im
&= ~UART011_RXIM
;
1100 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1102 spin_lock(&uap
->port
.lock
);
1105 static void pl011_tx_chars(struct uart_amba_port
*uap
)
1107 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
1110 if (uap
->port
.x_char
) {
1111 writew(uap
->port
.x_char
, uap
->port
.membase
+ UART01x_DR
);
1112 uap
->port
.icount
.tx
++;
1113 uap
->port
.x_char
= 0;
1116 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uap
->port
)) {
1117 pl011_stop_tx(&uap
->port
);
1121 /* If we are using DMA mode, try to send some characters. */
1122 if (pl011_dma_tx_irq(uap
))
1125 count
= uap
->fifosize
>> 1;
1127 writew(xmit
->buf
[xmit
->tail
], uap
->port
.membase
+ UART01x_DR
);
1128 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
1129 uap
->port
.icount
.tx
++;
1130 if (uart_circ_empty(xmit
))
1132 } while (--count
> 0);
1134 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
1135 uart_write_wakeup(&uap
->port
);
1137 if (uart_circ_empty(xmit
))
1138 pl011_stop_tx(&uap
->port
);
1141 static void pl011_modem_status(struct uart_amba_port
*uap
)
1143 unsigned int status
, delta
;
1145 status
= readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_MODEM_ANY
;
1147 delta
= status
^ uap
->old_status
;
1148 uap
->old_status
= status
;
1153 if (delta
& UART01x_FR_DCD
)
1154 uart_handle_dcd_change(&uap
->port
, status
& UART01x_FR_DCD
);
1156 if (delta
& UART01x_FR_DSR
)
1157 uap
->port
.icount
.dsr
++;
1159 if (delta
& UART01x_FR_CTS
)
1160 uart_handle_cts_change(&uap
->port
, status
& UART01x_FR_CTS
);
1162 wake_up_interruptible(&uap
->port
.state
->port
.delta_msr_wait
);
1165 static irqreturn_t
pl011_int(int irq
, void *dev_id
)
1167 struct uart_amba_port
*uap
= dev_id
;
1168 unsigned long flags
;
1169 unsigned int status
, pass_counter
= AMBA_ISR_PASS_LIMIT
;
1171 unsigned int dummy_read
;
1173 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1175 status
= readw(uap
->port
.membase
+ UART011_MIS
);
1178 if (uap
->vendor
->cts_event_workaround
) {
1179 /* workaround to make sure that all bits are unlocked.. */
1180 writew(0x00, uap
->port
.membase
+ UART011_ICR
);
1183 * WA: introduce 26ns(1 uart clk) delay before W1C;
1184 * single apb access will incur 2 pclk(133.12Mhz) delay,
1185 * so add 2 dummy reads
1187 dummy_read
= readw(uap
->port
.membase
+ UART011_ICR
);
1188 dummy_read
= readw(uap
->port
.membase
+ UART011_ICR
);
1191 writew(status
& ~(UART011_TXIS
|UART011_RTIS
|
1193 uap
->port
.membase
+ UART011_ICR
);
1195 if (status
& (UART011_RTIS
|UART011_RXIS
)) {
1196 if (pl011_dma_rx_running(uap
))
1197 pl011_dma_rx_irq(uap
);
1199 pl011_rx_chars(uap
);
1201 if (status
& (UART011_DSRMIS
|UART011_DCDMIS
|
1202 UART011_CTSMIS
|UART011_RIMIS
))
1203 pl011_modem_status(uap
);
1204 if (status
& UART011_TXIS
)
1205 pl011_tx_chars(uap
);
1207 if (pass_counter
-- == 0)
1210 status
= readw(uap
->port
.membase
+ UART011_MIS
);
1211 } while (status
!= 0);
1215 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1217 return IRQ_RETVAL(handled
);
1220 static unsigned int pl011_tx_empty(struct uart_port
*port
)
1222 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1223 unsigned int status
= readw(uap
->port
.membase
+ UART01x_FR
);
1224 return status
& (UART01x_FR_BUSY
|UART01x_FR_TXFF
) ? 0 : TIOCSER_TEMT
;
1227 static unsigned int pl011_get_mctrl(struct uart_port
*port
)
1229 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1230 unsigned int result
= 0;
1231 unsigned int status
= readw(uap
->port
.membase
+ UART01x_FR
);
1233 #define TIOCMBIT(uartbit, tiocmbit) \
1234 if (status & uartbit) \
1237 TIOCMBIT(UART01x_FR_DCD
, TIOCM_CAR
);
1238 TIOCMBIT(UART01x_FR_DSR
, TIOCM_DSR
);
1239 TIOCMBIT(UART01x_FR_CTS
, TIOCM_CTS
);
1240 TIOCMBIT(UART011_FR_RI
, TIOCM_RNG
);
1245 static void pl011_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1247 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1250 cr
= readw(uap
->port
.membase
+ UART011_CR
);
1252 #define TIOCMBIT(tiocmbit, uartbit) \
1253 if (mctrl & tiocmbit) \
1258 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTS
);
1259 TIOCMBIT(TIOCM_DTR
, UART011_CR_DTR
);
1260 TIOCMBIT(TIOCM_OUT1
, UART011_CR_OUT1
);
1261 TIOCMBIT(TIOCM_OUT2
, UART011_CR_OUT2
);
1262 TIOCMBIT(TIOCM_LOOP
, UART011_CR_LBE
);
1265 /* We need to disable auto-RTS if we want to turn RTS off */
1266 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTSEN
);
1270 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1273 static void pl011_break_ctl(struct uart_port
*port
, int break_state
)
1275 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1276 unsigned long flags
;
1279 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1280 lcr_h
= readw(uap
->port
.membase
+ uap
->lcrh_tx
);
1281 if (break_state
== -1)
1282 lcr_h
|= UART01x_LCRH_BRK
;
1284 lcr_h
&= ~UART01x_LCRH_BRK
;
1285 writew(lcr_h
, uap
->port
.membase
+ uap
->lcrh_tx
);
1286 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1289 #ifdef CONFIG_CONSOLE_POLL
1290 static int pl011_get_poll_char(struct uart_port
*port
)
1292 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1293 unsigned int status
;
1295 status
= readw(uap
->port
.membase
+ UART01x_FR
);
1296 if (status
& UART01x_FR_RXFE
)
1297 return NO_POLL_CHAR
;
1299 return readw(uap
->port
.membase
+ UART01x_DR
);
1302 static void pl011_put_poll_char(struct uart_port
*port
,
1305 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1307 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
1310 writew(ch
, uap
->port
.membase
+ UART01x_DR
);
1313 #endif /* CONFIG_CONSOLE_POLL */
1315 static int pl011_startup(struct uart_port
*port
)
1317 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1321 /* Optionaly enable pins to be muxed in and configured */
1322 if (!IS_ERR(uap
->pins_default
)) {
1323 retval
= pinctrl_select_state(uap
->pinctrl
, uap
->pins_default
);
1326 "could not set default pins\n");
1330 * Try to enable the clock producer.
1332 retval
= clk_prepare_enable(uap
->clk
);
1336 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
1338 /* Clear pending error and receive interrupts */
1339 writew(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
| UART011_FEIS
|
1340 UART011_RTIS
| UART011_RXIS
, uap
->port
.membase
+ UART011_ICR
);
1345 retval
= request_irq(uap
->port
.irq
, pl011_int
, 0, "uart-pl011", uap
);
1349 writew(uap
->vendor
->ifls
, uap
->port
.membase
+ UART011_IFLS
);
1352 * Provoke TX FIFO interrupt into asserting.
1354 cr
= UART01x_CR_UARTEN
| UART011_CR_TXE
| UART011_CR_LBE
;
1355 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1356 writew(0, uap
->port
.membase
+ UART011_FBRD
);
1357 writew(1, uap
->port
.membase
+ UART011_IBRD
);
1358 writew(0, uap
->port
.membase
+ uap
->lcrh_rx
);
1359 if (uap
->lcrh_tx
!= uap
->lcrh_rx
) {
1362 * Wait 10 PCLKs before writing LCRH_TX register,
1363 * to get this delay write read only register 10 times
1365 for (i
= 0; i
< 10; ++i
)
1366 writew(0xff, uap
->port
.membase
+ UART011_MIS
);
1367 writew(0, uap
->port
.membase
+ uap
->lcrh_tx
);
1369 writew(0, uap
->port
.membase
+ UART01x_DR
);
1370 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_BUSY
)
1373 /* restore RTS and DTR */
1374 cr
= uap
->old_cr
& (UART011_CR_RTS
| UART011_CR_DTR
);
1375 cr
|= UART01x_CR_UARTEN
| UART011_CR_RXE
| UART011_CR_TXE
;
1376 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1379 * initialise the old status of the modem signals
1381 uap
->old_status
= readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_MODEM_ANY
;
1384 pl011_dma_startup(uap
);
1387 * Finally, enable interrupts, only timeouts when using DMA
1388 * if initial RX DMA job failed, start in interrupt mode
1391 spin_lock_irq(&uap
->port
.lock
);
1392 /* Clear out any spuriously appearing RX interrupts */
1393 writew(UART011_RTIS
| UART011_RXIS
,
1394 uap
->port
.membase
+ UART011_ICR
);
1395 uap
->im
= UART011_RTIM
;
1396 if (!pl011_dma_rx_running(uap
))
1397 uap
->im
|= UART011_RXIM
;
1398 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1399 spin_unlock_irq(&uap
->port
.lock
);
1401 if (uap
->port
.dev
->platform_data
) {
1402 struct amba_pl011_data
*plat
;
1404 plat
= uap
->port
.dev
->platform_data
;
1412 clk_disable_unprepare(uap
->clk
);
1417 static void pl011_shutdown_channel(struct uart_amba_port
*uap
,
1422 val
= readw(uap
->port
.membase
+ lcrh
);
1423 val
&= ~(UART01x_LCRH_BRK
| UART01x_LCRH_FEN
);
1424 writew(val
, uap
->port
.membase
+ lcrh
);
1427 static void pl011_shutdown(struct uart_port
*port
)
1429 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1434 * disable all interrupts
1436 spin_lock_irq(&uap
->port
.lock
);
1438 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1439 writew(0xffff, uap
->port
.membase
+ UART011_ICR
);
1440 spin_unlock_irq(&uap
->port
.lock
);
1442 pl011_dma_shutdown(uap
);
1445 * Free the interrupt
1447 free_irq(uap
->port
.irq
, uap
);
1451 * disable the port. It should not disable RTS and DTR.
1452 * Also RTS and DTR state should be preserved to restore
1453 * it during startup().
1455 uap
->autorts
= false;
1456 cr
= readw(uap
->port
.membase
+ UART011_CR
);
1458 cr
&= UART011_CR_RTS
| UART011_CR_DTR
;
1459 cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
1460 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1463 * disable break condition and fifos
1465 pl011_shutdown_channel(uap
, uap
->lcrh_rx
);
1466 if (uap
->lcrh_rx
!= uap
->lcrh_tx
)
1467 pl011_shutdown_channel(uap
, uap
->lcrh_tx
);
1470 * Shut down the clock producer
1472 clk_disable_unprepare(uap
->clk
);
1473 /* Optionally let pins go into sleep states */
1474 if (!IS_ERR(uap
->pins_sleep
)) {
1475 retval
= pinctrl_select_state(uap
->pinctrl
, uap
->pins_sleep
);
1478 "could not set pins to sleep state\n");
1482 if (uap
->port
.dev
->platform_data
) {
1483 struct amba_pl011_data
*plat
;
1485 plat
= uap
->port
.dev
->platform_data
;
1493 pl011_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1494 struct ktermios
*old
)
1496 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1497 unsigned int lcr_h
, old_cr
;
1498 unsigned long flags
;
1499 unsigned int baud
, quot
, clkdiv
;
1501 if (uap
->vendor
->oversampling
)
1507 * Ask the core to calculate the divisor for us.
1509 baud
= uart_get_baud_rate(port
, termios
, old
, 0,
1510 port
->uartclk
/ clkdiv
);
1512 if (baud
> port
->uartclk
/16)
1513 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 8, baud
);
1515 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 4, baud
);
1517 switch (termios
->c_cflag
& CSIZE
) {
1519 lcr_h
= UART01x_LCRH_WLEN_5
;
1522 lcr_h
= UART01x_LCRH_WLEN_6
;
1525 lcr_h
= UART01x_LCRH_WLEN_7
;
1528 lcr_h
= UART01x_LCRH_WLEN_8
;
1531 if (termios
->c_cflag
& CSTOPB
)
1532 lcr_h
|= UART01x_LCRH_STP2
;
1533 if (termios
->c_cflag
& PARENB
) {
1534 lcr_h
|= UART01x_LCRH_PEN
;
1535 if (!(termios
->c_cflag
& PARODD
))
1536 lcr_h
|= UART01x_LCRH_EPS
;
1538 if (uap
->fifosize
> 1)
1539 lcr_h
|= UART01x_LCRH_FEN
;
1541 spin_lock_irqsave(&port
->lock
, flags
);
1544 * Update the per-port timeout.
1546 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1548 port
->read_status_mask
= UART011_DR_OE
| 255;
1549 if (termios
->c_iflag
& INPCK
)
1550 port
->read_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1551 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
1552 port
->read_status_mask
|= UART011_DR_BE
;
1555 * Characters to ignore
1557 port
->ignore_status_mask
= 0;
1558 if (termios
->c_iflag
& IGNPAR
)
1559 port
->ignore_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1560 if (termios
->c_iflag
& IGNBRK
) {
1561 port
->ignore_status_mask
|= UART011_DR_BE
;
1563 * If we're ignoring parity and break indicators,
1564 * ignore overruns too (for real raw support).
1566 if (termios
->c_iflag
& IGNPAR
)
1567 port
->ignore_status_mask
|= UART011_DR_OE
;
1571 * Ignore all characters if CREAD is not set.
1573 if ((termios
->c_cflag
& CREAD
) == 0)
1574 port
->ignore_status_mask
|= UART_DUMMY_DR_RX
;
1576 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1577 pl011_enable_ms(port
);
1579 /* first, disable everything */
1580 old_cr
= readw(port
->membase
+ UART011_CR
);
1581 writew(0, port
->membase
+ UART011_CR
);
1583 if (termios
->c_cflag
& CRTSCTS
) {
1584 if (old_cr
& UART011_CR_RTS
)
1585 old_cr
|= UART011_CR_RTSEN
;
1587 old_cr
|= UART011_CR_CTSEN
;
1588 uap
->autorts
= true;
1590 old_cr
&= ~(UART011_CR_CTSEN
| UART011_CR_RTSEN
);
1591 uap
->autorts
= false;
1594 if (uap
->vendor
->oversampling
) {
1595 if (baud
> port
->uartclk
/ 16)
1596 old_cr
|= ST_UART011_CR_OVSFACT
;
1598 old_cr
&= ~ST_UART011_CR_OVSFACT
;
1602 writew(quot
& 0x3f, port
->membase
+ UART011_FBRD
);
1603 writew(quot
>> 6, port
->membase
+ UART011_IBRD
);
1606 * ----------v----------v----------v----------v-----
1607 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1608 * ----------^----------^----------^----------^-----
1610 writew(lcr_h
, port
->membase
+ uap
->lcrh_rx
);
1611 if (uap
->lcrh_rx
!= uap
->lcrh_tx
) {
1614 * Wait 10 PCLKs before writing LCRH_TX register,
1615 * to get this delay write read only register 10 times
1617 for (i
= 0; i
< 10; ++i
)
1618 writew(0xff, uap
->port
.membase
+ UART011_MIS
);
1619 writew(lcr_h
, port
->membase
+ uap
->lcrh_tx
);
1621 writew(old_cr
, port
->membase
+ UART011_CR
);
1623 spin_unlock_irqrestore(&port
->lock
, flags
);
1626 static const char *pl011_type(struct uart_port
*port
)
1628 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1629 return uap
->port
.type
== PORT_AMBA
? uap
->type
: NULL
;
1633 * Release the memory region(s) being used by 'port'
1635 static void pl011_release_port(struct uart_port
*port
)
1637 release_mem_region(port
->mapbase
, SZ_4K
);
1641 * Request the memory region(s) being used by 'port'
1643 static int pl011_request_port(struct uart_port
*port
)
1645 return request_mem_region(port
->mapbase
, SZ_4K
, "uart-pl011")
1646 != NULL
? 0 : -EBUSY
;
1650 * Configure/autoconfigure the port.
1652 static void pl011_config_port(struct uart_port
*port
, int flags
)
1654 if (flags
& UART_CONFIG_TYPE
) {
1655 port
->type
= PORT_AMBA
;
1656 pl011_request_port(port
);
1661 * verify the new serial_struct (for TIOCSSERIAL).
1663 static int pl011_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1666 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_AMBA
)
1668 if (ser
->irq
< 0 || ser
->irq
>= nr_irqs
)
1670 if (ser
->baud_base
< 9600)
1675 static struct uart_ops amba_pl011_pops
= {
1676 .tx_empty
= pl011_tx_empty
,
1677 .set_mctrl
= pl011_set_mctrl
,
1678 .get_mctrl
= pl011_get_mctrl
,
1679 .stop_tx
= pl011_stop_tx
,
1680 .start_tx
= pl011_start_tx
,
1681 .stop_rx
= pl011_stop_rx
,
1682 .enable_ms
= pl011_enable_ms
,
1683 .break_ctl
= pl011_break_ctl
,
1684 .startup
= pl011_startup
,
1685 .shutdown
= pl011_shutdown
,
1686 .flush_buffer
= pl011_dma_flush_buffer
,
1687 .set_termios
= pl011_set_termios
,
1689 .release_port
= pl011_release_port
,
1690 .request_port
= pl011_request_port
,
1691 .config_port
= pl011_config_port
,
1692 .verify_port
= pl011_verify_port
,
1693 #ifdef CONFIG_CONSOLE_POLL
1694 .poll_get_char
= pl011_get_poll_char
,
1695 .poll_put_char
= pl011_put_poll_char
,
1699 static struct uart_amba_port
*amba_ports
[UART_NR
];
1701 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1703 static void pl011_console_putchar(struct uart_port
*port
, int ch
)
1705 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1707 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
1709 writew(ch
, uap
->port
.membase
+ UART01x_DR
);
1713 pl011_console_write(struct console
*co
, const char *s
, unsigned int count
)
1715 struct uart_amba_port
*uap
= amba_ports
[co
->index
];
1716 unsigned int status
, old_cr
, new_cr
;
1717 unsigned long flags
;
1720 clk_enable(uap
->clk
);
1722 local_irq_save(flags
);
1723 if (uap
->port
.sysrq
)
1725 else if (oops_in_progress
)
1726 locked
= spin_trylock(&uap
->port
.lock
);
1728 spin_lock(&uap
->port
.lock
);
1731 * First save the CR then disable the interrupts
1733 old_cr
= readw(uap
->port
.membase
+ UART011_CR
);
1734 new_cr
= old_cr
& ~UART011_CR_CTSEN
;
1735 new_cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
1736 writew(new_cr
, uap
->port
.membase
+ UART011_CR
);
1738 uart_console_write(&uap
->port
, s
, count
, pl011_console_putchar
);
1741 * Finally, wait for transmitter to become empty
1742 * and restore the TCR
1745 status
= readw(uap
->port
.membase
+ UART01x_FR
);
1746 } while (status
& UART01x_FR_BUSY
);
1747 writew(old_cr
, uap
->port
.membase
+ UART011_CR
);
1750 spin_unlock(&uap
->port
.lock
);
1751 local_irq_restore(flags
);
1753 clk_disable(uap
->clk
);
1757 pl011_console_get_options(struct uart_amba_port
*uap
, int *baud
,
1758 int *parity
, int *bits
)
1760 if (readw(uap
->port
.membase
+ UART011_CR
) & UART01x_CR_UARTEN
) {
1761 unsigned int lcr_h
, ibrd
, fbrd
;
1763 lcr_h
= readw(uap
->port
.membase
+ uap
->lcrh_tx
);
1766 if (lcr_h
& UART01x_LCRH_PEN
) {
1767 if (lcr_h
& UART01x_LCRH_EPS
)
1773 if ((lcr_h
& 0x60) == UART01x_LCRH_WLEN_7
)
1778 ibrd
= readw(uap
->port
.membase
+ UART011_IBRD
);
1779 fbrd
= readw(uap
->port
.membase
+ UART011_FBRD
);
1781 *baud
= uap
->port
.uartclk
* 4 / (64 * ibrd
+ fbrd
);
1783 if (uap
->vendor
->oversampling
) {
1784 if (readw(uap
->port
.membase
+ UART011_CR
)
1785 & ST_UART011_CR_OVSFACT
)
1791 static int __init
pl011_console_setup(struct console
*co
, char *options
)
1793 struct uart_amba_port
*uap
;
1801 * Check whether an invalid uart number has been specified, and
1802 * if so, search for the first available port that does have
1805 if (co
->index
>= UART_NR
)
1807 uap
= amba_ports
[co
->index
];
1811 /* Allow pins to be muxed in and configured */
1812 if (!IS_ERR(uap
->pins_default
)) {
1813 ret
= pinctrl_select_state(uap
->pinctrl
, uap
->pins_default
);
1815 dev_err(uap
->port
.dev
,
1816 "could not set default pins\n");
1819 ret
= clk_prepare(uap
->clk
);
1823 if (uap
->port
.dev
->platform_data
) {
1824 struct amba_pl011_data
*plat
;
1826 plat
= uap
->port
.dev
->platform_data
;
1831 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
1834 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1836 pl011_console_get_options(uap
, &baud
, &parity
, &bits
);
1838 return uart_set_options(&uap
->port
, co
, baud
, parity
, bits
, flow
);
1841 static struct uart_driver amba_reg
;
1842 static struct console amba_console
= {
1844 .write
= pl011_console_write
,
1845 .device
= uart_console_device
,
1846 .setup
= pl011_console_setup
,
1847 .flags
= CON_PRINTBUFFER
,
1852 #define AMBA_CONSOLE (&amba_console)
1854 #define AMBA_CONSOLE NULL
1857 static struct uart_driver amba_reg
= {
1858 .owner
= THIS_MODULE
,
1859 .driver_name
= "ttyAMA",
1860 .dev_name
= "ttyAMA",
1861 .major
= SERIAL_AMBA_MAJOR
,
1862 .minor
= SERIAL_AMBA_MINOR
,
1864 .cons
= AMBA_CONSOLE
,
1867 static int pl011_probe_dt_alias(int index
, struct device
*dev
)
1869 struct device_node
*np
;
1870 static bool seen_dev_with_alias
= false;
1871 static bool seen_dev_without_alias
= false;
1874 if (!IS_ENABLED(CONFIG_OF
))
1881 ret
= of_alias_get_id(np
, "serial");
1882 if (IS_ERR_VALUE(ret
)) {
1883 seen_dev_without_alias
= true;
1886 seen_dev_with_alias
= true;
1887 if (ret
>= ARRAY_SIZE(amba_ports
) || amba_ports
[ret
] != NULL
) {
1888 dev_warn(dev
, "requested serial port %d not available.\n", ret
);
1893 if (seen_dev_with_alias
&& seen_dev_without_alias
)
1894 dev_warn(dev
, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
1899 static int pl011_probe(struct amba_device
*dev
, const struct amba_id
*id
)
1901 struct uart_amba_port
*uap
;
1902 struct vendor_data
*vendor
= id
->data
;
1906 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++)
1907 if (amba_ports
[i
] == NULL
)
1910 if (i
== ARRAY_SIZE(amba_ports
)) {
1915 uap
= kzalloc(sizeof(struct uart_amba_port
), GFP_KERNEL
);
1921 i
= pl011_probe_dt_alias(i
, &dev
->dev
);
1923 base
= ioremap(dev
->res
.start
, resource_size(&dev
->res
));
1929 uap
->pinctrl
= devm_pinctrl_get(&dev
->dev
);
1930 if (IS_ERR(uap
->pinctrl
)) {
1931 ret
= PTR_ERR(uap
->pinctrl
);
1934 uap
->pins_default
= pinctrl_lookup_state(uap
->pinctrl
,
1935 PINCTRL_STATE_DEFAULT
);
1936 if (IS_ERR(uap
->pins_default
))
1937 dev_err(&dev
->dev
, "could not get default pinstate\n");
1939 uap
->pins_sleep
= pinctrl_lookup_state(uap
->pinctrl
,
1940 PINCTRL_STATE_SLEEP
);
1941 if (IS_ERR(uap
->pins_sleep
))
1942 dev_dbg(&dev
->dev
, "could not get sleep pinstate\n");
1944 uap
->clk
= clk_get(&dev
->dev
, NULL
);
1945 if (IS_ERR(uap
->clk
)) {
1946 ret
= PTR_ERR(uap
->clk
);
1950 uap
->vendor
= vendor
;
1951 uap
->lcrh_rx
= vendor
->lcrh_rx
;
1952 uap
->lcrh_tx
= vendor
->lcrh_tx
;
1954 uap
->fifosize
= vendor
->fifosize
;
1955 uap
->interrupt_may_hang
= vendor
->interrupt_may_hang
;
1956 uap
->port
.dev
= &dev
->dev
;
1957 uap
->port
.mapbase
= dev
->res
.start
;
1958 uap
->port
.membase
= base
;
1959 uap
->port
.iotype
= UPIO_MEM
;
1960 uap
->port
.irq
= dev
->irq
[0];
1961 uap
->port
.fifosize
= uap
->fifosize
;
1962 uap
->port
.ops
= &amba_pl011_pops
;
1963 uap
->port
.flags
= UPF_BOOT_AUTOCONF
;
1965 pl011_dma_probe(uap
);
1967 /* Ensure interrupts from this UART are masked and cleared */
1968 writew(0, uap
->port
.membase
+ UART011_IMSC
);
1969 writew(0xffff, uap
->port
.membase
+ UART011_ICR
);
1971 snprintf(uap
->type
, sizeof(uap
->type
), "PL011 rev%u", amba_rev(dev
));
1973 amba_ports
[i
] = uap
;
1975 amba_set_drvdata(dev
, uap
);
1976 ret
= uart_add_one_port(&amba_reg
, &uap
->port
);
1978 amba_set_drvdata(dev
, NULL
);
1979 amba_ports
[i
] = NULL
;
1980 pl011_dma_remove(uap
);
1991 static int pl011_remove(struct amba_device
*dev
)
1993 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
1996 amba_set_drvdata(dev
, NULL
);
1998 uart_remove_one_port(&amba_reg
, &uap
->port
);
2000 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++)
2001 if (amba_ports
[i
] == uap
)
2002 amba_ports
[i
] = NULL
;
2004 pl011_dma_remove(uap
);
2005 iounmap(uap
->port
.membase
);
2012 static int pl011_suspend(struct amba_device
*dev
, pm_message_t state
)
2014 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
2019 return uart_suspend_port(&amba_reg
, &uap
->port
);
2022 static int pl011_resume(struct amba_device
*dev
)
2024 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
2029 return uart_resume_port(&amba_reg
, &uap
->port
);
2033 static struct amba_id pl011_ids
[] = {
2037 .data
= &vendor_arm
,
2047 MODULE_DEVICE_TABLE(amba
, pl011_ids
);
2049 static struct amba_driver pl011_driver
= {
2051 .name
= "uart-pl011",
2053 .id_table
= pl011_ids
,
2054 .probe
= pl011_probe
,
2055 .remove
= pl011_remove
,
2057 .suspend
= pl011_suspend
,
2058 .resume
= pl011_resume
,
2062 static int __init
pl011_init(void)
2065 printk(KERN_INFO
"Serial: AMBA PL011 UART driver\n");
2067 ret
= uart_register_driver(&amba_reg
);
2069 ret
= amba_driver_register(&pl011_driver
);
2071 uart_unregister_driver(&amba_reg
);
2076 static void __exit
pl011_exit(void)
2078 amba_driver_unregister(&pl011_driver
);
2079 uart_unregister_driver(&amba_reg
);
2083 * While this can be a module, if builtin it's most likely the console
2084 * So let's leave module_exit but move module_init to an earlier place
2086 arch_initcall(pl011_init
);
2087 module_exit(pl011_exit
);
2089 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2090 MODULE_DESCRIPTION("ARM AMBA serial port driver");
2091 MODULE_LICENSE("GPL");