2 * linux/drivers/char/amba.c
4 * Driver for AMBA serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 * Copyright (C) 2010 ST-Ericsson SA
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This is a generic driver for ARM AMBA-type serial ports. They
27 * have a lot of 16550-like features, but are not register compatible.
28 * Note that although they do have CTS, DCD and DSR inputs, they do
29 * not have an RI input, nor do they have DTR or RTS outputs. If
30 * required, these have to be supplied via some other means (eg, GPIO)
31 * and hooked into this driver.
34 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
38 #include <linux/module.h>
39 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/console.h>
42 #include <linux/sysrq.h>
43 #include <linux/device.h>
44 #include <linux/tty.h>
45 #include <linux/tty_flip.h>
46 #include <linux/serial_core.h>
47 #include <linux/serial.h>
48 #include <linux/amba/bus.h>
49 #include <linux/amba/serial.h>
50 #include <linux/clk.h>
51 #include <linux/slab.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/scatterlist.h>
57 #include <asm/sizes.h>
61 #define SERIAL_AMBA_MAJOR 204
62 #define SERIAL_AMBA_MINOR 64
63 #define SERIAL_AMBA_NR UART_NR
65 #define AMBA_ISR_PASS_LIMIT 256
67 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
68 #define UART_DUMMY_DR_RX (1 << 16)
70 /* There is by now at least one vendor with differing details, so handle it */
73 unsigned int fifosize
;
80 static struct vendor_data vendor_arm
= {
81 .ifls
= UART011_IFLS_RX4_8
|UART011_IFLS_TX4_8
,
83 .lcrh_tx
= UART011_LCRH
,
84 .lcrh_rx
= UART011_LCRH
,
85 .oversampling
= false,
86 .dma_threshold
= false,
89 static struct vendor_data vendor_st
= {
90 .ifls
= UART011_IFLS_RX_HALF
|UART011_IFLS_TX_HALF
,
92 .lcrh_tx
= ST_UART011_LCRH_TX
,
93 .lcrh_rx
= ST_UART011_LCRH_RX
,
95 .dma_threshold
= true,
98 /* Deals with DMA transactions */
101 struct scatterlist sg
;
105 struct pl011_dmarx_data
{
106 struct dma_chan
*chan
;
107 struct completion complete
;
109 struct pl011_sgbuf sgbuf_a
;
110 struct pl011_sgbuf sgbuf_b
;
115 struct pl011_dmatx_data
{
116 struct dma_chan
*chan
;
117 struct scatterlist sg
;
123 * We wrap our port structure around the generic uart_port.
125 struct uart_amba_port
{
126 struct uart_port port
;
128 const struct vendor_data
*vendor
;
129 unsigned int dmacr
; /* dma control reg */
130 unsigned int im
; /* interrupt mask */
131 unsigned int old_status
;
132 unsigned int fifosize
; /* vendor-specific */
133 unsigned int lcrh_tx
; /* vendor-specific */
134 unsigned int lcrh_rx
; /* vendor-specific */
137 #ifdef CONFIG_DMA_ENGINE
141 struct pl011_dmarx_data dmarx
;
142 struct pl011_dmatx_data dmatx
;
147 * Reads up to 256 characters from the FIFO or until it's empty and
148 * inserts them into the TTY layer. Returns the number of characters
149 * read from the FIFO.
151 static int pl011_fifo_to_tty(struct uart_amba_port
*uap
)
154 unsigned int flag
, max_count
= 256;
157 while (max_count
--) {
158 status
= readw(uap
->port
.membase
+ UART01x_FR
);
159 if (status
& UART01x_FR_RXFE
)
162 /* Take chars from the FIFO and update status */
163 ch
= readw(uap
->port
.membase
+ UART01x_DR
) |
166 uap
->port
.icount
.rx
++;
169 if (unlikely(ch
& UART_DR_ERROR
)) {
170 if (ch
& UART011_DR_BE
) {
171 ch
&= ~(UART011_DR_FE
| UART011_DR_PE
);
172 uap
->port
.icount
.brk
++;
173 if (uart_handle_break(&uap
->port
))
175 } else if (ch
& UART011_DR_PE
)
176 uap
->port
.icount
.parity
++;
177 else if (ch
& UART011_DR_FE
)
178 uap
->port
.icount
.frame
++;
179 if (ch
& UART011_DR_OE
)
180 uap
->port
.icount
.overrun
++;
182 ch
&= uap
->port
.read_status_mask
;
184 if (ch
& UART011_DR_BE
)
186 else if (ch
& UART011_DR_PE
)
188 else if (ch
& UART011_DR_FE
)
192 if (uart_handle_sysrq_char(&uap
->port
, ch
& 255))
195 uart_insert_char(&uap
->port
, ch
, UART011_DR_OE
, ch
, flag
);
203 * All the DMA operation mode stuff goes inside this ifdef.
204 * This assumes that you have a generic DMA device interface,
205 * no custom DMA interfaces are supported.
207 #ifdef CONFIG_DMA_ENGINE
209 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
211 static int pl011_sgbuf_init(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
212 enum dma_data_direction dir
)
214 sg
->buf
= kmalloc(PL011_DMA_BUFFER_SIZE
, GFP_KERNEL
);
218 sg_init_one(&sg
->sg
, sg
->buf
, PL011_DMA_BUFFER_SIZE
);
220 if (dma_map_sg(chan
->device
->dev
, &sg
->sg
, 1, dir
) != 1) {
227 static void pl011_sgbuf_free(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
228 enum dma_data_direction dir
)
231 dma_unmap_sg(chan
->device
->dev
, &sg
->sg
, 1, dir
);
236 static void pl011_dma_probe_initcall(struct uart_amba_port
*uap
)
238 /* DMA is the sole user of the platform data right now */
239 struct amba_pl011_data
*plat
= uap
->port
.dev
->platform_data
;
240 struct dma_slave_config tx_conf
= {
241 .dst_addr
= uap
->port
.mapbase
+ UART01x_DR
,
242 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
243 .direction
= DMA_TO_DEVICE
,
244 .dst_maxburst
= uap
->fifosize
>> 1,
246 struct dma_chan
*chan
;
249 /* We need platform data */
250 if (!plat
|| !plat
->dma_filter
) {
251 dev_info(uap
->port
.dev
, "no DMA platform data\n");
255 /* Try to acquire a generic DMA engine slave TX channel */
257 dma_cap_set(DMA_SLAVE
, mask
);
259 chan
= dma_request_channel(mask
, plat
->dma_filter
, plat
->dma_tx_param
);
261 dev_err(uap
->port
.dev
, "no TX DMA channel!\n");
265 dmaengine_slave_config(chan
, &tx_conf
);
266 uap
->dmatx
.chan
= chan
;
268 dev_info(uap
->port
.dev
, "DMA channel TX %s\n",
269 dma_chan_name(uap
->dmatx
.chan
));
271 /* Optionally make use of an RX channel as well */
272 if (plat
->dma_rx_param
) {
273 struct dma_slave_config rx_conf
= {
274 .src_addr
= uap
->port
.mapbase
+ UART01x_DR
,
275 .src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
276 .direction
= DMA_FROM_DEVICE
,
277 .src_maxburst
= uap
->fifosize
>> 1,
280 chan
= dma_request_channel(mask
, plat
->dma_filter
, plat
->dma_rx_param
);
282 dev_err(uap
->port
.dev
, "no RX DMA channel!\n");
286 dmaengine_slave_config(chan
, &rx_conf
);
287 uap
->dmarx
.chan
= chan
;
289 dev_info(uap
->port
.dev
, "DMA channel RX %s\n",
290 dma_chan_name(uap
->dmarx
.chan
));
296 * Stack up the UARTs and let the above initcall be done at device
297 * initcall time, because the serial driver is called as an arch
298 * initcall, and at this time the DMA subsystem is not yet registered.
299 * At this point the driver will switch over to using DMA where desired.
302 struct list_head node
;
303 struct uart_amba_port
*uap
;
306 static LIST_HEAD(pl011_dma_uarts
);
308 static int __init
pl011_dma_initcall(void)
310 struct list_head
*node
, *tmp
;
312 list_for_each_safe(node
, tmp
, &pl011_dma_uarts
) {
313 struct dma_uap
*dmau
= list_entry(node
, struct dma_uap
, node
);
314 pl011_dma_probe_initcall(dmau
->uap
);
321 device_initcall(pl011_dma_initcall
);
323 static void pl011_dma_probe(struct uart_amba_port
*uap
)
325 struct dma_uap
*dmau
= kzalloc(sizeof(struct dma_uap
), GFP_KERNEL
);
328 list_add_tail(&dmau
->node
, &pl011_dma_uarts
);
332 static void pl011_dma_probe(struct uart_amba_port
*uap
)
334 pl011_dma_probe_initcall(uap
);
338 static void pl011_dma_remove(struct uart_amba_port
*uap
)
340 /* TODO: remove the initcall if it has not yet executed */
342 dma_release_channel(uap
->dmatx
.chan
);
344 dma_release_channel(uap
->dmarx
.chan
);
347 /* Forward declare this for the refill routine */
348 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
);
351 * The current DMA TX buffer has been sent.
352 * Try to queue up another DMA buffer.
354 static void pl011_dma_tx_callback(void *data
)
356 struct uart_amba_port
*uap
= data
;
357 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
361 spin_lock_irqsave(&uap
->port
.lock
, flags
);
362 if (uap
->dmatx
.queued
)
363 dma_unmap_sg(dmatx
->chan
->device
->dev
, &dmatx
->sg
, 1,
367 uap
->dmacr
= dmacr
& ~UART011_TXDMAE
;
368 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
371 * If TX DMA was disabled, it means that we've stopped the DMA for
372 * some reason (eg, XOFF received, or we want to send an X-char.)
374 * Note: we need to be careful here of a potential race between DMA
375 * and the rest of the driver - if the driver disables TX DMA while
376 * a TX buffer completing, we must update the tx queued status to
377 * get further refills (hence we check dmacr).
379 if (!(dmacr
& UART011_TXDMAE
) || uart_tx_stopped(&uap
->port
) ||
380 uart_circ_empty(&uap
->port
.state
->xmit
)) {
381 uap
->dmatx
.queued
= false;
382 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
386 if (pl011_dma_tx_refill(uap
) <= 0) {
388 * We didn't queue a DMA buffer for some reason, but we
389 * have data pending to be sent. Re-enable the TX IRQ.
391 uap
->im
|= UART011_TXIM
;
392 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
394 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
398 * Try to refill the TX DMA buffer.
399 * Locking: called with port lock held and IRQs disabled.
401 * 1 if we queued up a TX DMA buffer.
402 * 0 if we didn't want to handle this by DMA
405 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
)
407 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
408 struct dma_chan
*chan
= dmatx
->chan
;
409 struct dma_device
*dma_dev
= chan
->device
;
410 struct dma_async_tx_descriptor
*desc
;
411 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
415 * Try to avoid the overhead involved in using DMA if the
416 * transaction fits in the first half of the FIFO, by using
417 * the standard interrupt handling. This ensures that we
418 * issue a uart_write_wakeup() at the appropriate time.
420 count
= uart_circ_chars_pending(xmit
);
421 if (count
< (uap
->fifosize
>> 1)) {
422 uap
->dmatx
.queued
= false;
427 * Bodge: don't send the last character by DMA, as this
428 * will prevent XON from notifying us to restart DMA.
432 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
433 if (count
> PL011_DMA_BUFFER_SIZE
)
434 count
= PL011_DMA_BUFFER_SIZE
;
436 if (xmit
->tail
< xmit
->head
)
437 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], count
);
439 size_t first
= UART_XMIT_SIZE
- xmit
->tail
;
440 size_t second
= xmit
->head
;
442 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], first
);
444 memcpy(&dmatx
->buf
[first
], &xmit
->buf
[0], second
);
447 dmatx
->sg
.length
= count
;
449 if (dma_map_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
) != 1) {
450 uap
->dmatx
.queued
= false;
451 dev_dbg(uap
->port
.dev
, "unable to map TX DMA\n");
455 desc
= dma_dev
->device_prep_slave_sg(chan
, &dmatx
->sg
, 1, DMA_TO_DEVICE
,
456 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
458 dma_unmap_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
);
459 uap
->dmatx
.queued
= false;
461 * If DMA cannot be used right now, we complete this
462 * transaction via IRQ and let the TTY layer retry.
464 dev_dbg(uap
->port
.dev
, "TX DMA busy\n");
468 /* Some data to go along to the callback */
469 desc
->callback
= pl011_dma_tx_callback
;
470 desc
->callback_param
= uap
;
472 /* All errors should happen at prepare time */
473 dmaengine_submit(desc
);
475 /* Fire the DMA transaction */
476 dma_dev
->device_issue_pending(chan
);
478 uap
->dmacr
|= UART011_TXDMAE
;
479 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
480 uap
->dmatx
.queued
= true;
483 * Now we know that DMA will fire, so advance the ring buffer
484 * with the stuff we just dispatched.
486 xmit
->tail
= (xmit
->tail
+ count
) & (UART_XMIT_SIZE
- 1);
487 uap
->port
.icount
.tx
+= count
;
489 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
490 uart_write_wakeup(&uap
->port
);
496 * We received a transmit interrupt without a pending X-char but with
497 * pending characters.
498 * Locking: called with port lock held and IRQs disabled.
500 * false if we want to use PIO to transmit
501 * true if we queued a DMA buffer
503 static bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
505 if (!uap
->using_tx_dma
)
509 * If we already have a TX buffer queued, but received a
510 * TX interrupt, it will be because we've just sent an X-char.
511 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
513 if (uap
->dmatx
.queued
) {
514 uap
->dmacr
|= UART011_TXDMAE
;
515 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
516 uap
->im
&= ~UART011_TXIM
;
517 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
522 * We don't have a TX buffer queued, so try to queue one.
523 * If we succesfully queued a buffer, mask the TX IRQ.
525 if (pl011_dma_tx_refill(uap
) > 0) {
526 uap
->im
&= ~UART011_TXIM
;
527 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
534 * Stop the DMA transmit (eg, due to received XOFF).
535 * Locking: called with port lock held and IRQs disabled.
537 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
539 if (uap
->dmatx
.queued
) {
540 uap
->dmacr
&= ~UART011_TXDMAE
;
541 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
546 * Try to start a DMA transmit, or in the case of an XON/OFF
547 * character queued for send, try to get that character out ASAP.
548 * Locking: called with port lock held and IRQs disabled.
550 * false if we want the TX IRQ to be enabled
551 * true if we have a buffer queued
553 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
557 if (!uap
->using_tx_dma
)
560 if (!uap
->port
.x_char
) {
561 /* no X-char, try to push chars out in DMA mode */
564 if (!uap
->dmatx
.queued
) {
565 if (pl011_dma_tx_refill(uap
) > 0) {
566 uap
->im
&= ~UART011_TXIM
;
569 uap
->im
|= UART011_TXIM
;
572 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
573 } else if (!(uap
->dmacr
& UART011_TXDMAE
)) {
574 uap
->dmacr
|= UART011_TXDMAE
;
576 uap
->port
.membase
+ UART011_DMACR
);
582 * We have an X-char to send. Disable DMA to prevent it loading
583 * the TX fifo, and then see if we can stuff it into the FIFO.
586 uap
->dmacr
&= ~UART011_TXDMAE
;
587 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
589 if (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
) {
591 * No space in the FIFO, so enable the transmit interrupt
592 * so we know when there is space. Note that once we've
593 * loaded the character, we should just re-enable DMA.
598 writew(uap
->port
.x_char
, uap
->port
.membase
+ UART01x_DR
);
599 uap
->port
.icount
.tx
++;
600 uap
->port
.x_char
= 0;
602 /* Success - restore the DMA state */
604 writew(dmacr
, uap
->port
.membase
+ UART011_DMACR
);
610 * Flush the transmit buffer.
611 * Locking: called with port lock held and IRQs disabled.
613 static void pl011_dma_flush_buffer(struct uart_port
*port
)
615 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
617 if (!uap
->using_tx_dma
)
620 /* Avoid deadlock with the DMA engine callback */
621 spin_unlock(&uap
->port
.lock
);
622 dmaengine_terminate_all(uap
->dmatx
.chan
);
623 spin_lock(&uap
->port
.lock
);
624 if (uap
->dmatx
.queued
) {
625 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
627 uap
->dmatx
.queued
= false;
628 uap
->dmacr
&= ~UART011_TXDMAE
;
629 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
633 static void pl011_dma_rx_callback(void *data
);
635 static int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
637 struct dma_chan
*rxchan
= uap
->dmarx
.chan
;
638 struct dma_device
*dma_dev
;
639 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
640 struct dma_async_tx_descriptor
*desc
;
641 struct pl011_sgbuf
*sgbuf
;
646 /* Start the RX DMA job */
647 sgbuf
= uap
->dmarx
.use_buf_b
?
648 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
649 dma_dev
= rxchan
->device
;
650 desc
= rxchan
->device
->device_prep_slave_sg(rxchan
, &sgbuf
->sg
, 1,
652 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
654 * If the DMA engine is busy and cannot prepare a
655 * channel, no big deal, the driver will fall back
656 * to interrupt mode as a result of this error code.
659 uap
->dmarx
.running
= false;
660 dmaengine_terminate_all(rxchan
);
664 /* Some data to go along to the callback */
665 desc
->callback
= pl011_dma_rx_callback
;
666 desc
->callback_param
= uap
;
667 dmarx
->cookie
= dmaengine_submit(desc
);
668 dma_async_issue_pending(rxchan
);
670 uap
->dmacr
|= UART011_RXDMAE
;
671 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
672 uap
->dmarx
.running
= true;
674 uap
->im
&= ~UART011_RXIM
;
675 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
681 * This is called when either the DMA job is complete, or
682 * the FIFO timeout interrupt occurred. This must be called
683 * with the port spinlock uap->port.lock held.
685 static void pl011_dma_rx_chars(struct uart_amba_port
*uap
,
686 u32 pending
, bool use_buf_b
,
689 struct tty_struct
*tty
= uap
->port
.state
->port
.tty
;
690 struct pl011_sgbuf
*sgbuf
= use_buf_b
?
691 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
692 struct device
*dev
= uap
->dmarx
.chan
->device
->dev
;
694 u32 fifotaken
= 0; /* only used for vdbg() */
696 /* Pick everything from the DMA first */
699 dma_sync_sg_for_cpu(dev
, &sgbuf
->sg
, 1, DMA_FROM_DEVICE
);
702 * First take all chars in the DMA pipe, then look in the FIFO.
703 * Note that tty_insert_flip_buf() tries to take as many chars
706 dma_count
= tty_insert_flip_string(uap
->port
.state
->port
.tty
,
707 sgbuf
->buf
, pending
);
709 /* Return buffer to device */
710 dma_sync_sg_for_device(dev
, &sgbuf
->sg
, 1, DMA_FROM_DEVICE
);
712 uap
->port
.icount
.rx
+= dma_count
;
713 if (dma_count
< pending
)
714 dev_warn(uap
->port
.dev
,
715 "couldn't insert all characters (TTY is full?)\n");
719 * Only continue with trying to read the FIFO if all DMA chars have
722 if (dma_count
== pending
&& readfifo
) {
723 /* Clear any error flags */
724 writew(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
| UART011_FEIS
,
725 uap
->port
.membase
+ UART011_ICR
);
728 * If we read all the DMA'd characters, and we had an
729 * incomplete buffer, that could be due to an rx error, or
730 * maybe we just timed out. Read any pending chars and check
733 * Error conditions will only occur in the FIFO, these will
734 * trigger an immediate interrupt and stop the DMA job, so we
735 * will always find the error in the FIFO, never in the DMA
738 fifotaken
= pl011_fifo_to_tty(uap
);
741 spin_unlock(&uap
->port
.lock
);
742 dev_vdbg(uap
->port
.dev
,
743 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
744 dma_count
, fifotaken
);
745 tty_flip_buffer_push(tty
);
746 spin_lock(&uap
->port
.lock
);
749 static void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
751 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
752 struct dma_chan
*rxchan
= dmarx
->chan
;
753 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
754 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
756 struct dma_tx_state state
;
757 enum dma_status dmastat
;
760 * Pause the transfer so we can trust the current counter,
761 * do this before we pause the PL011 block, else we may
764 if (dmaengine_pause(rxchan
))
765 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
766 dmastat
= rxchan
->device
->device_tx_status(rxchan
,
767 dmarx
->cookie
, &state
);
768 if (dmastat
!= DMA_PAUSED
)
769 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
771 /* Disable RX DMA - incoming data will wait in the FIFO */
772 uap
->dmacr
&= ~UART011_RXDMAE
;
773 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
774 uap
->dmarx
.running
= false;
776 pending
= sgbuf
->sg
.length
- state
.residue
;
777 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
778 /* Then we terminate the transfer - we now know our residue */
779 dmaengine_terminate_all(rxchan
);
782 * This will take the chars we have so far and insert
783 * into the framework.
785 pl011_dma_rx_chars(uap
, pending
, dmarx
->use_buf_b
, true);
787 /* Switch buffer & re-trigger DMA job */
788 dmarx
->use_buf_b
= !dmarx
->use_buf_b
;
789 if (pl011_dma_rx_trigger_dma(uap
)) {
790 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
791 "fall back to interrupt mode\n");
792 uap
->im
|= UART011_RXIM
;
793 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
797 static void pl011_dma_rx_callback(void *data
)
799 struct uart_amba_port
*uap
= data
;
800 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
801 bool lastbuf
= dmarx
->use_buf_b
;
805 * This completion interrupt occurs typically when the
806 * RX buffer is totally stuffed but no timeout has yet
807 * occurred. When that happens, we just want the RX
808 * routine to flush out the secondary DMA buffer while
809 * we immediately trigger the next DMA job.
811 spin_lock_irq(&uap
->port
.lock
);
812 uap
->dmarx
.running
= false;
813 dmarx
->use_buf_b
= !lastbuf
;
814 ret
= pl011_dma_rx_trigger_dma(uap
);
816 pl011_dma_rx_chars(uap
, PL011_DMA_BUFFER_SIZE
, lastbuf
, false);
817 spin_unlock_irq(&uap
->port
.lock
);
819 * Do this check after we picked the DMA chars so we don't
820 * get some IRQ immediately from RX.
823 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
824 "fall back to interrupt mode\n");
825 uap
->im
|= UART011_RXIM
;
826 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
831 * Stop accepting received characters, when we're shutting down or
832 * suspending this port.
833 * Locking: called with port lock held and IRQs disabled.
835 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
837 /* FIXME. Just disable the DMA enable */
838 uap
->dmacr
&= ~UART011_RXDMAE
;
839 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
842 static void pl011_dma_startup(struct uart_amba_port
*uap
)
846 if (!uap
->dmatx
.chan
)
849 uap
->dmatx
.buf
= kmalloc(PL011_DMA_BUFFER_SIZE
, GFP_KERNEL
);
850 if (!uap
->dmatx
.buf
) {
851 dev_err(uap
->port
.dev
, "no memory for DMA TX buffer\n");
852 uap
->port
.fifosize
= uap
->fifosize
;
856 sg_init_one(&uap
->dmatx
.sg
, uap
->dmatx
.buf
, PL011_DMA_BUFFER_SIZE
);
858 /* The DMA buffer is now the FIFO the TTY subsystem can use */
859 uap
->port
.fifosize
= PL011_DMA_BUFFER_SIZE
;
860 uap
->using_tx_dma
= true;
862 if (!uap
->dmarx
.chan
)
865 /* Allocate and map DMA RX buffers */
866 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
869 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
874 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
,
877 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
879 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
884 uap
->using_rx_dma
= true;
887 /* Turn on DMA error (RX/TX will be enabled on demand) */
888 uap
->dmacr
|= UART011_DMAONERR
;
889 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
892 * ST Micro variants has some specific dma burst threshold
893 * compensation. Set this to 16 bytes, so burst will only
894 * be issued above/below 16 bytes.
896 if (uap
->vendor
->dma_threshold
)
897 writew(ST_UART011_DMAWM_RX_16
| ST_UART011_DMAWM_TX_16
,
898 uap
->port
.membase
+ ST_UART011_DMAWM
);
900 if (uap
->using_rx_dma
) {
901 if (pl011_dma_rx_trigger_dma(uap
))
902 dev_dbg(uap
->port
.dev
, "could not trigger initial "
903 "RX DMA job, fall back to interrupt mode\n");
907 static void pl011_dma_shutdown(struct uart_amba_port
*uap
)
909 if (!(uap
->using_tx_dma
|| uap
->using_rx_dma
))
912 /* Disable RX and TX DMA */
913 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_BUSY
)
916 spin_lock_irq(&uap
->port
.lock
);
917 uap
->dmacr
&= ~(UART011_DMAONERR
| UART011_RXDMAE
| UART011_TXDMAE
);
918 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
919 spin_unlock_irq(&uap
->port
.lock
);
921 if (uap
->using_tx_dma
) {
922 /* In theory, this should already be done by pl011_dma_flush_buffer */
923 dmaengine_terminate_all(uap
->dmatx
.chan
);
924 if (uap
->dmatx
.queued
) {
925 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
927 uap
->dmatx
.queued
= false;
930 kfree(uap
->dmatx
.buf
);
931 uap
->using_tx_dma
= false;
934 if (uap
->using_rx_dma
) {
935 dmaengine_terminate_all(uap
->dmarx
.chan
);
936 /* Clean up the RX DMA */
937 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
, DMA_FROM_DEVICE
);
938 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
, DMA_FROM_DEVICE
);
939 uap
->using_rx_dma
= false;
943 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
945 return uap
->using_rx_dma
;
948 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
950 return uap
->using_rx_dma
&& uap
->dmarx
.running
;
955 /* Blank functions if the DMA engine is not available */
956 static inline void pl011_dma_probe(struct uart_amba_port
*uap
)
960 static inline void pl011_dma_remove(struct uart_amba_port
*uap
)
964 static inline void pl011_dma_startup(struct uart_amba_port
*uap
)
968 static inline void pl011_dma_shutdown(struct uart_amba_port
*uap
)
972 static inline bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
977 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
981 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
986 static inline void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
990 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
994 static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
999 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
1004 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
1009 #define pl011_dma_flush_buffer NULL
1013 static void pl011_stop_tx(struct uart_port
*port
)
1015 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1017 uap
->im
&= ~UART011_TXIM
;
1018 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1019 pl011_dma_tx_stop(uap
);
1022 static void pl011_start_tx(struct uart_port
*port
)
1024 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1026 if (!pl011_dma_tx_start(uap
)) {
1027 uap
->im
|= UART011_TXIM
;
1028 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1032 static void pl011_stop_rx(struct uart_port
*port
)
1034 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1036 uap
->im
&= ~(UART011_RXIM
|UART011_RTIM
|UART011_FEIM
|
1037 UART011_PEIM
|UART011_BEIM
|UART011_OEIM
);
1038 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1040 pl011_dma_rx_stop(uap
);
1043 static void pl011_enable_ms(struct uart_port
*port
)
1045 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1047 uap
->im
|= UART011_RIMIM
|UART011_CTSMIM
|UART011_DCDMIM
|UART011_DSRMIM
;
1048 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1051 static void pl011_rx_chars(struct uart_amba_port
*uap
)
1053 struct tty_struct
*tty
= uap
->port
.state
->port
.tty
;
1055 pl011_fifo_to_tty(uap
);
1057 spin_unlock(&uap
->port
.lock
);
1058 tty_flip_buffer_push(tty
);
1060 * If we were temporarily out of DMA mode for a while,
1061 * attempt to switch back to DMA mode again.
1063 if (pl011_dma_rx_available(uap
)) {
1064 if (pl011_dma_rx_trigger_dma(uap
)) {
1065 dev_dbg(uap
->port
.dev
, "could not trigger RX DMA job "
1066 "fall back to interrupt mode again\n");
1067 uap
->im
|= UART011_RXIM
;
1069 uap
->im
&= ~UART011_RXIM
;
1070 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1072 spin_lock(&uap
->port
.lock
);
1075 static void pl011_tx_chars(struct uart_amba_port
*uap
)
1077 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
1080 if (uap
->port
.x_char
) {
1081 writew(uap
->port
.x_char
, uap
->port
.membase
+ UART01x_DR
);
1082 uap
->port
.icount
.tx
++;
1083 uap
->port
.x_char
= 0;
1086 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uap
->port
)) {
1087 pl011_stop_tx(&uap
->port
);
1091 /* If we are using DMA mode, try to send some characters. */
1092 if (pl011_dma_tx_irq(uap
))
1095 count
= uap
->fifosize
>> 1;
1097 writew(xmit
->buf
[xmit
->tail
], uap
->port
.membase
+ UART01x_DR
);
1098 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
1099 uap
->port
.icount
.tx
++;
1100 if (uart_circ_empty(xmit
))
1102 } while (--count
> 0);
1104 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
1105 uart_write_wakeup(&uap
->port
);
1107 if (uart_circ_empty(xmit
))
1108 pl011_stop_tx(&uap
->port
);
1111 static void pl011_modem_status(struct uart_amba_port
*uap
)
1113 unsigned int status
, delta
;
1115 status
= readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_MODEM_ANY
;
1117 delta
= status
^ uap
->old_status
;
1118 uap
->old_status
= status
;
1123 if (delta
& UART01x_FR_DCD
)
1124 uart_handle_dcd_change(&uap
->port
, status
& UART01x_FR_DCD
);
1126 if (delta
& UART01x_FR_DSR
)
1127 uap
->port
.icount
.dsr
++;
1129 if (delta
& UART01x_FR_CTS
)
1130 uart_handle_cts_change(&uap
->port
, status
& UART01x_FR_CTS
);
1132 wake_up_interruptible(&uap
->port
.state
->port
.delta_msr_wait
);
1135 static irqreturn_t
pl011_int(int irq
, void *dev_id
)
1137 struct uart_amba_port
*uap
= dev_id
;
1138 unsigned long flags
;
1139 unsigned int status
, pass_counter
= AMBA_ISR_PASS_LIMIT
;
1142 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1144 status
= readw(uap
->port
.membase
+ UART011_MIS
);
1147 writew(status
& ~(UART011_TXIS
|UART011_RTIS
|
1149 uap
->port
.membase
+ UART011_ICR
);
1151 if (status
& (UART011_RTIS
|UART011_RXIS
)) {
1152 if (pl011_dma_rx_running(uap
))
1153 pl011_dma_rx_irq(uap
);
1155 pl011_rx_chars(uap
);
1157 if (status
& (UART011_DSRMIS
|UART011_DCDMIS
|
1158 UART011_CTSMIS
|UART011_RIMIS
))
1159 pl011_modem_status(uap
);
1160 if (status
& UART011_TXIS
)
1161 pl011_tx_chars(uap
);
1163 if (pass_counter
-- == 0)
1166 status
= readw(uap
->port
.membase
+ UART011_MIS
);
1167 } while (status
!= 0);
1171 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1173 return IRQ_RETVAL(handled
);
1176 static unsigned int pl01x_tx_empty(struct uart_port
*port
)
1178 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1179 unsigned int status
= readw(uap
->port
.membase
+ UART01x_FR
);
1180 return status
& (UART01x_FR_BUSY
|UART01x_FR_TXFF
) ? 0 : TIOCSER_TEMT
;
1183 static unsigned int pl01x_get_mctrl(struct uart_port
*port
)
1185 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1186 unsigned int result
= 0;
1187 unsigned int status
= readw(uap
->port
.membase
+ UART01x_FR
);
1189 #define TIOCMBIT(uartbit, tiocmbit) \
1190 if (status & uartbit) \
1193 TIOCMBIT(UART01x_FR_DCD
, TIOCM_CAR
);
1194 TIOCMBIT(UART01x_FR_DSR
, TIOCM_DSR
);
1195 TIOCMBIT(UART01x_FR_CTS
, TIOCM_CTS
);
1196 TIOCMBIT(UART011_FR_RI
, TIOCM_RNG
);
1201 static void pl011_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1203 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1206 cr
= readw(uap
->port
.membase
+ UART011_CR
);
1208 #define TIOCMBIT(tiocmbit, uartbit) \
1209 if (mctrl & tiocmbit) \
1214 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTS
);
1215 TIOCMBIT(TIOCM_DTR
, UART011_CR_DTR
);
1216 TIOCMBIT(TIOCM_OUT1
, UART011_CR_OUT1
);
1217 TIOCMBIT(TIOCM_OUT2
, UART011_CR_OUT2
);
1218 TIOCMBIT(TIOCM_LOOP
, UART011_CR_LBE
);
1221 /* We need to disable auto-RTS if we want to turn RTS off */
1222 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTSEN
);
1226 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1229 static void pl011_break_ctl(struct uart_port
*port
, int break_state
)
1231 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1232 unsigned long flags
;
1235 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1236 lcr_h
= readw(uap
->port
.membase
+ uap
->lcrh_tx
);
1237 if (break_state
== -1)
1238 lcr_h
|= UART01x_LCRH_BRK
;
1240 lcr_h
&= ~UART01x_LCRH_BRK
;
1241 writew(lcr_h
, uap
->port
.membase
+ uap
->lcrh_tx
);
1242 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1245 #ifdef CONFIG_CONSOLE_POLL
1246 static int pl010_get_poll_char(struct uart_port
*port
)
1248 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1249 unsigned int status
;
1251 status
= readw(uap
->port
.membase
+ UART01x_FR
);
1252 if (status
& UART01x_FR_RXFE
)
1253 return NO_POLL_CHAR
;
1255 return readw(uap
->port
.membase
+ UART01x_DR
);
1258 static void pl010_put_poll_char(struct uart_port
*port
,
1261 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1263 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
1266 writew(ch
, uap
->port
.membase
+ UART01x_DR
);
1269 #endif /* CONFIG_CONSOLE_POLL */
1271 static int pl011_startup(struct uart_port
*port
)
1273 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1278 * Try to enable the clock producer.
1280 retval
= clk_enable(uap
->clk
);
1284 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
1289 retval
= request_irq(uap
->port
.irq
, pl011_int
, 0, "uart-pl011", uap
);
1293 writew(uap
->vendor
->ifls
, uap
->port
.membase
+ UART011_IFLS
);
1296 * Provoke TX FIFO interrupt into asserting.
1298 cr
= UART01x_CR_UARTEN
| UART011_CR_TXE
| UART011_CR_LBE
;
1299 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1300 writew(0, uap
->port
.membase
+ UART011_FBRD
);
1301 writew(1, uap
->port
.membase
+ UART011_IBRD
);
1302 writew(0, uap
->port
.membase
+ uap
->lcrh_rx
);
1303 if (uap
->lcrh_tx
!= uap
->lcrh_rx
) {
1306 * Wait 10 PCLKs before writing LCRH_TX register,
1307 * to get this delay write read only register 10 times
1309 for (i
= 0; i
< 10; ++i
)
1310 writew(0xff, uap
->port
.membase
+ UART011_MIS
);
1311 writew(0, uap
->port
.membase
+ uap
->lcrh_tx
);
1313 writew(0, uap
->port
.membase
+ UART01x_DR
);
1314 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_BUSY
)
1317 cr
= UART01x_CR_UARTEN
| UART011_CR_RXE
| UART011_CR_TXE
;
1318 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1320 /* Clear pending error interrupts */
1321 writew(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
| UART011_FEIS
,
1322 uap
->port
.membase
+ UART011_ICR
);
1325 * initialise the old status of the modem signals
1327 uap
->old_status
= readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_MODEM_ANY
;
1330 pl011_dma_startup(uap
);
1333 * Finally, enable interrupts, only timeouts when using DMA
1334 * if initial RX DMA job failed, start in interrupt mode
1337 spin_lock_irq(&uap
->port
.lock
);
1338 uap
->im
= UART011_RTIM
;
1339 if (!pl011_dma_rx_running(uap
))
1340 uap
->im
|= UART011_RXIM
;
1341 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1342 spin_unlock_irq(&uap
->port
.lock
);
1347 clk_disable(uap
->clk
);
1352 static void pl011_shutdown_channel(struct uart_amba_port
*uap
,
1357 val
= readw(uap
->port
.membase
+ lcrh
);
1358 val
&= ~(UART01x_LCRH_BRK
| UART01x_LCRH_FEN
);
1359 writew(val
, uap
->port
.membase
+ lcrh
);
1362 static void pl011_shutdown(struct uart_port
*port
)
1364 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1367 * disable all interrupts
1369 spin_lock_irq(&uap
->port
.lock
);
1371 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1372 writew(0xffff, uap
->port
.membase
+ UART011_ICR
);
1373 spin_unlock_irq(&uap
->port
.lock
);
1375 pl011_dma_shutdown(uap
);
1378 * Free the interrupt
1380 free_irq(uap
->port
.irq
, uap
);
1385 uap
->autorts
= false;
1386 writew(UART01x_CR_UARTEN
| UART011_CR_TXE
, uap
->port
.membase
+ UART011_CR
);
1389 * disable break condition and fifos
1391 pl011_shutdown_channel(uap
, uap
->lcrh_rx
);
1392 if (uap
->lcrh_rx
!= uap
->lcrh_tx
)
1393 pl011_shutdown_channel(uap
, uap
->lcrh_tx
);
1396 * Shut down the clock producer
1398 clk_disable(uap
->clk
);
1402 pl011_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1403 struct ktermios
*old
)
1405 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1406 unsigned int lcr_h
, old_cr
;
1407 unsigned long flags
;
1408 unsigned int baud
, quot
, clkdiv
;
1410 if (uap
->vendor
->oversampling
)
1416 * Ask the core to calculate the divisor for us.
1418 baud
= uart_get_baud_rate(port
, termios
, old
, 0,
1419 port
->uartclk
/ clkdiv
);
1421 if (baud
> port
->uartclk
/16)
1422 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 8, baud
);
1424 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 4, baud
);
1426 switch (termios
->c_cflag
& CSIZE
) {
1428 lcr_h
= UART01x_LCRH_WLEN_5
;
1431 lcr_h
= UART01x_LCRH_WLEN_6
;
1434 lcr_h
= UART01x_LCRH_WLEN_7
;
1437 lcr_h
= UART01x_LCRH_WLEN_8
;
1440 if (termios
->c_cflag
& CSTOPB
)
1441 lcr_h
|= UART01x_LCRH_STP2
;
1442 if (termios
->c_cflag
& PARENB
) {
1443 lcr_h
|= UART01x_LCRH_PEN
;
1444 if (!(termios
->c_cflag
& PARODD
))
1445 lcr_h
|= UART01x_LCRH_EPS
;
1447 if (uap
->fifosize
> 1)
1448 lcr_h
|= UART01x_LCRH_FEN
;
1450 spin_lock_irqsave(&port
->lock
, flags
);
1453 * Update the per-port timeout.
1455 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1457 port
->read_status_mask
= UART011_DR_OE
| 255;
1458 if (termios
->c_iflag
& INPCK
)
1459 port
->read_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1460 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
1461 port
->read_status_mask
|= UART011_DR_BE
;
1464 * Characters to ignore
1466 port
->ignore_status_mask
= 0;
1467 if (termios
->c_iflag
& IGNPAR
)
1468 port
->ignore_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1469 if (termios
->c_iflag
& IGNBRK
) {
1470 port
->ignore_status_mask
|= UART011_DR_BE
;
1472 * If we're ignoring parity and break indicators,
1473 * ignore overruns too (for real raw support).
1475 if (termios
->c_iflag
& IGNPAR
)
1476 port
->ignore_status_mask
|= UART011_DR_OE
;
1480 * Ignore all characters if CREAD is not set.
1482 if ((termios
->c_cflag
& CREAD
) == 0)
1483 port
->ignore_status_mask
|= UART_DUMMY_DR_RX
;
1485 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1486 pl011_enable_ms(port
);
1488 /* first, disable everything */
1489 old_cr
= readw(port
->membase
+ UART011_CR
);
1490 writew(0, port
->membase
+ UART011_CR
);
1492 if (termios
->c_cflag
& CRTSCTS
) {
1493 if (old_cr
& UART011_CR_RTS
)
1494 old_cr
|= UART011_CR_RTSEN
;
1496 old_cr
|= UART011_CR_CTSEN
;
1497 uap
->autorts
= true;
1499 old_cr
&= ~(UART011_CR_CTSEN
| UART011_CR_RTSEN
);
1500 uap
->autorts
= false;
1503 if (uap
->vendor
->oversampling
) {
1504 if (baud
> port
->uartclk
/ 16)
1505 old_cr
|= ST_UART011_CR_OVSFACT
;
1507 old_cr
&= ~ST_UART011_CR_OVSFACT
;
1511 writew(quot
& 0x3f, port
->membase
+ UART011_FBRD
);
1512 writew(quot
>> 6, port
->membase
+ UART011_IBRD
);
1515 * ----------v----------v----------v----------v-----
1516 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1517 * ----------^----------^----------^----------^-----
1519 writew(lcr_h
, port
->membase
+ uap
->lcrh_rx
);
1520 if (uap
->lcrh_rx
!= uap
->lcrh_tx
) {
1523 * Wait 10 PCLKs before writing LCRH_TX register,
1524 * to get this delay write read only register 10 times
1526 for (i
= 0; i
< 10; ++i
)
1527 writew(0xff, uap
->port
.membase
+ UART011_MIS
);
1528 writew(lcr_h
, port
->membase
+ uap
->lcrh_tx
);
1530 writew(old_cr
, port
->membase
+ UART011_CR
);
1532 spin_unlock_irqrestore(&port
->lock
, flags
);
1535 static const char *pl011_type(struct uart_port
*port
)
1537 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1538 return uap
->port
.type
== PORT_AMBA
? uap
->type
: NULL
;
1542 * Release the memory region(s) being used by 'port'
1544 static void pl010_release_port(struct uart_port
*port
)
1546 release_mem_region(port
->mapbase
, SZ_4K
);
1550 * Request the memory region(s) being used by 'port'
1552 static int pl010_request_port(struct uart_port
*port
)
1554 return request_mem_region(port
->mapbase
, SZ_4K
, "uart-pl011")
1555 != NULL
? 0 : -EBUSY
;
1559 * Configure/autoconfigure the port.
1561 static void pl010_config_port(struct uart_port
*port
, int flags
)
1563 if (flags
& UART_CONFIG_TYPE
) {
1564 port
->type
= PORT_AMBA
;
1565 pl010_request_port(port
);
1570 * verify the new serial_struct (for TIOCSSERIAL).
1572 static int pl010_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1575 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_AMBA
)
1577 if (ser
->irq
< 0 || ser
->irq
>= nr_irqs
)
1579 if (ser
->baud_base
< 9600)
1584 static struct uart_ops amba_pl011_pops
= {
1585 .tx_empty
= pl01x_tx_empty
,
1586 .set_mctrl
= pl011_set_mctrl
,
1587 .get_mctrl
= pl01x_get_mctrl
,
1588 .stop_tx
= pl011_stop_tx
,
1589 .start_tx
= pl011_start_tx
,
1590 .stop_rx
= pl011_stop_rx
,
1591 .enable_ms
= pl011_enable_ms
,
1592 .break_ctl
= pl011_break_ctl
,
1593 .startup
= pl011_startup
,
1594 .shutdown
= pl011_shutdown
,
1595 .flush_buffer
= pl011_dma_flush_buffer
,
1596 .set_termios
= pl011_set_termios
,
1598 .release_port
= pl010_release_port
,
1599 .request_port
= pl010_request_port
,
1600 .config_port
= pl010_config_port
,
1601 .verify_port
= pl010_verify_port
,
1602 #ifdef CONFIG_CONSOLE_POLL
1603 .poll_get_char
= pl010_get_poll_char
,
1604 .poll_put_char
= pl010_put_poll_char
,
1608 static struct uart_amba_port
*amba_ports
[UART_NR
];
1610 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1612 static void pl011_console_putchar(struct uart_port
*port
, int ch
)
1614 struct uart_amba_port
*uap
= (struct uart_amba_port
*)port
;
1616 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
1618 writew(ch
, uap
->port
.membase
+ UART01x_DR
);
1622 pl011_console_write(struct console
*co
, const char *s
, unsigned int count
)
1624 struct uart_amba_port
*uap
= amba_ports
[co
->index
];
1625 unsigned int status
, old_cr
, new_cr
;
1627 clk_enable(uap
->clk
);
1630 * First save the CR then disable the interrupts
1632 old_cr
= readw(uap
->port
.membase
+ UART011_CR
);
1633 new_cr
= old_cr
& ~UART011_CR_CTSEN
;
1634 new_cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
1635 writew(new_cr
, uap
->port
.membase
+ UART011_CR
);
1637 uart_console_write(&uap
->port
, s
, count
, pl011_console_putchar
);
1640 * Finally, wait for transmitter to become empty
1641 * and restore the TCR
1644 status
= readw(uap
->port
.membase
+ UART01x_FR
);
1645 } while (status
& UART01x_FR_BUSY
);
1646 writew(old_cr
, uap
->port
.membase
+ UART011_CR
);
1648 clk_disable(uap
->clk
);
1652 pl011_console_get_options(struct uart_amba_port
*uap
, int *baud
,
1653 int *parity
, int *bits
)
1655 if (readw(uap
->port
.membase
+ UART011_CR
) & UART01x_CR_UARTEN
) {
1656 unsigned int lcr_h
, ibrd
, fbrd
;
1658 lcr_h
= readw(uap
->port
.membase
+ uap
->lcrh_tx
);
1661 if (lcr_h
& UART01x_LCRH_PEN
) {
1662 if (lcr_h
& UART01x_LCRH_EPS
)
1668 if ((lcr_h
& 0x60) == UART01x_LCRH_WLEN_7
)
1673 ibrd
= readw(uap
->port
.membase
+ UART011_IBRD
);
1674 fbrd
= readw(uap
->port
.membase
+ UART011_FBRD
);
1676 *baud
= uap
->port
.uartclk
* 4 / (64 * ibrd
+ fbrd
);
1678 if (uap
->vendor
->oversampling
) {
1679 if (readw(uap
->port
.membase
+ UART011_CR
)
1680 & ST_UART011_CR_OVSFACT
)
1686 static int __init
pl011_console_setup(struct console
*co
, char *options
)
1688 struct uart_amba_port
*uap
;
1695 * Check whether an invalid uart number has been specified, and
1696 * if so, search for the first available port that does have
1699 if (co
->index
>= UART_NR
)
1701 uap
= amba_ports
[co
->index
];
1705 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
1708 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1710 pl011_console_get_options(uap
, &baud
, &parity
, &bits
);
1712 return uart_set_options(&uap
->port
, co
, baud
, parity
, bits
, flow
);
1715 static struct uart_driver amba_reg
;
1716 static struct console amba_console
= {
1718 .write
= pl011_console_write
,
1719 .device
= uart_console_device
,
1720 .setup
= pl011_console_setup
,
1721 .flags
= CON_PRINTBUFFER
,
1726 #define AMBA_CONSOLE (&amba_console)
1728 #define AMBA_CONSOLE NULL
1731 static struct uart_driver amba_reg
= {
1732 .owner
= THIS_MODULE
,
1733 .driver_name
= "ttyAMA",
1734 .dev_name
= "ttyAMA",
1735 .major
= SERIAL_AMBA_MAJOR
,
1736 .minor
= SERIAL_AMBA_MINOR
,
1738 .cons
= AMBA_CONSOLE
,
1741 static int pl011_probe(struct amba_device
*dev
, const struct amba_id
*id
)
1743 struct uart_amba_port
*uap
;
1744 struct vendor_data
*vendor
= id
->data
;
1748 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++)
1749 if (amba_ports
[i
] == NULL
)
1752 if (i
== ARRAY_SIZE(amba_ports
)) {
1757 uap
= kzalloc(sizeof(struct uart_amba_port
), GFP_KERNEL
);
1763 base
= ioremap(dev
->res
.start
, resource_size(&dev
->res
));
1769 uap
->clk
= clk_get(&dev
->dev
, NULL
);
1770 if (IS_ERR(uap
->clk
)) {
1771 ret
= PTR_ERR(uap
->clk
);
1775 uap
->vendor
= vendor
;
1776 uap
->lcrh_rx
= vendor
->lcrh_rx
;
1777 uap
->lcrh_tx
= vendor
->lcrh_tx
;
1778 uap
->fifosize
= vendor
->fifosize
;
1779 uap
->port
.dev
= &dev
->dev
;
1780 uap
->port
.mapbase
= dev
->res
.start
;
1781 uap
->port
.membase
= base
;
1782 uap
->port
.iotype
= UPIO_MEM
;
1783 uap
->port
.irq
= dev
->irq
[0];
1784 uap
->port
.fifosize
= uap
->fifosize
;
1785 uap
->port
.ops
= &amba_pl011_pops
;
1786 uap
->port
.flags
= UPF_BOOT_AUTOCONF
;
1788 pl011_dma_probe(uap
);
1790 snprintf(uap
->type
, sizeof(uap
->type
), "PL011 rev%u", amba_rev(dev
));
1792 amba_ports
[i
] = uap
;
1794 amba_set_drvdata(dev
, uap
);
1795 ret
= uart_add_one_port(&amba_reg
, &uap
->port
);
1797 amba_set_drvdata(dev
, NULL
);
1798 amba_ports
[i
] = NULL
;
1799 pl011_dma_remove(uap
);
1810 static int pl011_remove(struct amba_device
*dev
)
1812 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
1815 amba_set_drvdata(dev
, NULL
);
1817 uart_remove_one_port(&amba_reg
, &uap
->port
);
1819 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++)
1820 if (amba_ports
[i
] == uap
)
1821 amba_ports
[i
] = NULL
;
1823 pl011_dma_remove(uap
);
1824 iounmap(uap
->port
.membase
);
1831 static int pl011_suspend(struct amba_device
*dev
, pm_message_t state
)
1833 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
1838 return uart_suspend_port(&amba_reg
, &uap
->port
);
1841 static int pl011_resume(struct amba_device
*dev
)
1843 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
1848 return uart_resume_port(&amba_reg
, &uap
->port
);
1852 static struct amba_id pl011_ids
[] = {
1856 .data
= &vendor_arm
,
1866 static struct amba_driver pl011_driver
= {
1868 .name
= "uart-pl011",
1870 .id_table
= pl011_ids
,
1871 .probe
= pl011_probe
,
1872 .remove
= pl011_remove
,
1874 .suspend
= pl011_suspend
,
1875 .resume
= pl011_resume
,
1879 static int __init
pl011_init(void)
1882 printk(KERN_INFO
"Serial: AMBA PL011 UART driver\n");
1884 ret
= uart_register_driver(&amba_reg
);
1886 ret
= amba_driver_register(&pl011_driver
);
1888 uart_unregister_driver(&amba_reg
);
1893 static void __exit
pl011_exit(void)
1895 amba_driver_unregister(&pl011_driver
);
1896 uart_unregister_driver(&amba_reg
);
1900 * While this can be a module, if builtin it's most likely the console
1901 * So let's leave module_exit but move module_init to an earlier place
1903 arch_initcall(pl011_init
);
1904 module_exit(pl011_exit
);
1906 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
1907 MODULE_DESCRIPTION("ARM AMBA serial port driver");
1908 MODULE_LICENSE("GPL");