2 * Driver for AMBA serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 * Copyright (C) 2010 ST-Ericsson SA
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
33 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
37 #include <linux/module.h>
38 #include <linux/ioport.h>
39 #include <linux/init.h>
40 #include <linux/console.h>
41 #include <linux/sysrq.h>
42 #include <linux/device.h>
43 #include <linux/tty.h>
44 #include <linux/tty_flip.h>
45 #include <linux/serial_core.h>
46 #include <linux/serial.h>
47 #include <linux/amba/bus.h>
48 #include <linux/amba/serial.h>
49 #include <linux/clk.h>
50 #include <linux/slab.h>
51 #include <linux/dmaengine.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/scatterlist.h>
54 #include <linux/delay.h>
55 #include <linux/types.h>
57 #include <linux/of_device.h>
58 #include <linux/pinctrl/consumer.h>
59 #include <linux/sizes.h>
61 #include <linux/acpi.h>
65 #define SERIAL_AMBA_MAJOR 204
66 #define SERIAL_AMBA_MINOR 64
67 #define SERIAL_AMBA_NR UART_NR
69 #define AMBA_ISR_PASS_LIMIT 256
71 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
72 #define UART_DUMMY_DR_RX (1 << 16)
74 /* There is by now at least one vendor with differing details, so handle it */
81 bool cts_event_workaround
;
85 unsigned int (*get_fifosize
)(struct amba_device
*dev
);
88 static unsigned int get_fifosize_arm(struct amba_device
*dev
)
90 return amba_rev(dev
) < 3 ? 16 : 32;
93 static struct vendor_data vendor_arm
= {
94 .ifls
= UART011_IFLS_RX4_8
|UART011_IFLS_TX4_8
,
95 .lcrh_tx
= UART011_LCRH
,
96 .lcrh_rx
= UART011_LCRH
,
97 .oversampling
= false,
98 .dma_threshold
= false,
99 .cts_event_workaround
= false,
100 .always_enabled
= false,
101 .fixed_options
= false,
102 .get_fifosize
= get_fifosize_arm
,
105 static struct vendor_data vendor_sbsa
= {
106 .oversampling
= false,
107 .dma_threshold
= false,
108 .cts_event_workaround
= false,
109 .always_enabled
= true,
110 .fixed_options
= true,
113 static unsigned int get_fifosize_st(struct amba_device
*dev
)
118 static struct vendor_data vendor_st
= {
119 .ifls
= UART011_IFLS_RX_HALF
|UART011_IFLS_TX_HALF
,
120 .lcrh_tx
= ST_UART011_LCRH_TX
,
121 .lcrh_rx
= ST_UART011_LCRH_RX
,
122 .oversampling
= true,
123 .dma_threshold
= true,
124 .cts_event_workaround
= true,
125 .always_enabled
= false,
126 .fixed_options
= false,
127 .get_fifosize
= get_fifosize_st
,
130 /* Deals with DMA transactions */
133 struct scatterlist sg
;
137 struct pl011_dmarx_data
{
138 struct dma_chan
*chan
;
139 struct completion complete
;
141 struct pl011_sgbuf sgbuf_a
;
142 struct pl011_sgbuf sgbuf_b
;
145 struct timer_list timer
;
146 unsigned int last_residue
;
147 unsigned long last_jiffies
;
149 unsigned int poll_rate
;
150 unsigned int poll_timeout
;
153 struct pl011_dmatx_data
{
154 struct dma_chan
*chan
;
155 struct scatterlist sg
;
161 * We wrap our port structure around the generic uart_port.
163 struct uart_amba_port
{
164 struct uart_port port
;
166 const struct vendor_data
*vendor
;
167 unsigned int dmacr
; /* dma control reg */
168 unsigned int im
; /* interrupt mask */
169 unsigned int old_status
;
170 unsigned int fifosize
; /* vendor-specific */
171 unsigned int lcrh_tx
; /* vendor-specific */
172 unsigned int lcrh_rx
; /* vendor-specific */
173 unsigned int old_cr
; /* state during shutdown */
175 unsigned int fixed_baud
; /* vendor-set fixed baud rate */
177 #ifdef CONFIG_DMA_ENGINE
181 struct pl011_dmarx_data dmarx
;
182 struct pl011_dmatx_data dmatx
;
188 * Reads up to 256 characters from the FIFO or until it's empty and
189 * inserts them into the TTY layer. Returns the number of characters
190 * read from the FIFO.
192 static int pl011_fifo_to_tty(struct uart_amba_port
*uap
)
195 unsigned int flag
, max_count
= 256;
198 while (max_count
--) {
199 status
= readw(uap
->port
.membase
+ UART01x_FR
);
200 if (status
& UART01x_FR_RXFE
)
203 /* Take chars from the FIFO and update status */
204 ch
= readw(uap
->port
.membase
+ UART01x_DR
) |
207 uap
->port
.icount
.rx
++;
210 if (unlikely(ch
& UART_DR_ERROR
)) {
211 if (ch
& UART011_DR_BE
) {
212 ch
&= ~(UART011_DR_FE
| UART011_DR_PE
);
213 uap
->port
.icount
.brk
++;
214 if (uart_handle_break(&uap
->port
))
216 } else if (ch
& UART011_DR_PE
)
217 uap
->port
.icount
.parity
++;
218 else if (ch
& UART011_DR_FE
)
219 uap
->port
.icount
.frame
++;
220 if (ch
& UART011_DR_OE
)
221 uap
->port
.icount
.overrun
++;
223 ch
&= uap
->port
.read_status_mask
;
225 if (ch
& UART011_DR_BE
)
227 else if (ch
& UART011_DR_PE
)
229 else if (ch
& UART011_DR_FE
)
233 if (uart_handle_sysrq_char(&uap
->port
, ch
& 255))
236 uart_insert_char(&uap
->port
, ch
, UART011_DR_OE
, ch
, flag
);
244 * All the DMA operation mode stuff goes inside this ifdef.
245 * This assumes that you have a generic DMA device interface,
246 * no custom DMA interfaces are supported.
248 #ifdef CONFIG_DMA_ENGINE
250 #define PL011_DMA_BUFFER_SIZE PAGE_SIZE
252 static int pl011_sgbuf_init(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
253 enum dma_data_direction dir
)
257 sg
->buf
= dma_alloc_coherent(chan
->device
->dev
,
258 PL011_DMA_BUFFER_SIZE
, &dma_addr
, GFP_KERNEL
);
262 sg_init_table(&sg
->sg
, 1);
263 sg_set_page(&sg
->sg
, phys_to_page(dma_addr
),
264 PL011_DMA_BUFFER_SIZE
, offset_in_page(dma_addr
));
265 sg_dma_address(&sg
->sg
) = dma_addr
;
266 sg_dma_len(&sg
->sg
) = PL011_DMA_BUFFER_SIZE
;
271 static void pl011_sgbuf_free(struct dma_chan
*chan
, struct pl011_sgbuf
*sg
,
272 enum dma_data_direction dir
)
275 dma_free_coherent(chan
->device
->dev
,
276 PL011_DMA_BUFFER_SIZE
, sg
->buf
,
277 sg_dma_address(&sg
->sg
));
281 static void pl011_dma_probe(struct uart_amba_port
*uap
)
283 /* DMA is the sole user of the platform data right now */
284 struct amba_pl011_data
*plat
= dev_get_platdata(uap
->port
.dev
);
285 struct device
*dev
= uap
->port
.dev
;
286 struct dma_slave_config tx_conf
= {
287 .dst_addr
= uap
->port
.mapbase
+ UART01x_DR
,
288 .dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
289 .direction
= DMA_MEM_TO_DEV
,
290 .dst_maxburst
= uap
->fifosize
>> 1,
293 struct dma_chan
*chan
;
296 uap
->dma_probed
= true;
297 chan
= dma_request_slave_channel_reason(dev
, "tx");
299 if (PTR_ERR(chan
) == -EPROBE_DEFER
) {
300 uap
->dma_probed
= false;
304 /* We need platform data */
305 if (!plat
|| !plat
->dma_filter
) {
306 dev_info(uap
->port
.dev
, "no DMA platform data\n");
310 /* Try to acquire a generic DMA engine slave TX channel */
312 dma_cap_set(DMA_SLAVE
, mask
);
314 chan
= dma_request_channel(mask
, plat
->dma_filter
,
317 dev_err(uap
->port
.dev
, "no TX DMA channel!\n");
322 dmaengine_slave_config(chan
, &tx_conf
);
323 uap
->dmatx
.chan
= chan
;
325 dev_info(uap
->port
.dev
, "DMA channel TX %s\n",
326 dma_chan_name(uap
->dmatx
.chan
));
328 /* Optionally make use of an RX channel as well */
329 chan
= dma_request_slave_channel(dev
, "rx");
331 if (!chan
&& plat
->dma_rx_param
) {
332 chan
= dma_request_channel(mask
, plat
->dma_filter
, plat
->dma_rx_param
);
335 dev_err(uap
->port
.dev
, "no RX DMA channel!\n");
341 struct dma_slave_config rx_conf
= {
342 .src_addr
= uap
->port
.mapbase
+ UART01x_DR
,
343 .src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
,
344 .direction
= DMA_DEV_TO_MEM
,
345 .src_maxburst
= uap
->fifosize
>> 2,
348 struct dma_slave_caps caps
;
351 * Some DMA controllers provide information on their capabilities.
352 * If the controller does, check for suitable residue processing
353 * otherwise assime all is well.
355 if (0 == dma_get_slave_caps(chan
, &caps
)) {
356 if (caps
.residue_granularity
==
357 DMA_RESIDUE_GRANULARITY_DESCRIPTOR
) {
358 dma_release_channel(chan
);
359 dev_info(uap
->port
.dev
,
360 "RX DMA disabled - no residue processing\n");
364 dmaengine_slave_config(chan
, &rx_conf
);
365 uap
->dmarx
.chan
= chan
;
367 uap
->dmarx
.auto_poll_rate
= false;
368 if (plat
&& plat
->dma_rx_poll_enable
) {
369 /* Set poll rate if specified. */
370 if (plat
->dma_rx_poll_rate
) {
371 uap
->dmarx
.auto_poll_rate
= false;
372 uap
->dmarx
.poll_rate
= plat
->dma_rx_poll_rate
;
375 * 100 ms defaults to poll rate if not
376 * specified. This will be adjusted with
377 * the baud rate at set_termios.
379 uap
->dmarx
.auto_poll_rate
= true;
380 uap
->dmarx
.poll_rate
= 100;
382 /* 3 secs defaults poll_timeout if not specified. */
383 if (plat
->dma_rx_poll_timeout
)
384 uap
->dmarx
.poll_timeout
=
385 plat
->dma_rx_poll_timeout
;
387 uap
->dmarx
.poll_timeout
= 3000;
388 } else if (!plat
&& dev
->of_node
) {
389 uap
->dmarx
.auto_poll_rate
= of_property_read_bool(
390 dev
->of_node
, "auto-poll");
391 if (uap
->dmarx
.auto_poll_rate
) {
394 if (0 == of_property_read_u32(dev
->of_node
,
396 uap
->dmarx
.poll_rate
= x
;
398 uap
->dmarx
.poll_rate
= 100;
399 if (0 == of_property_read_u32(dev
->of_node
,
400 "poll-timeout-ms", &x
))
401 uap
->dmarx
.poll_timeout
= x
;
403 uap
->dmarx
.poll_timeout
= 3000;
406 dev_info(uap
->port
.dev
, "DMA channel RX %s\n",
407 dma_chan_name(uap
->dmarx
.chan
));
411 static void pl011_dma_remove(struct uart_amba_port
*uap
)
414 dma_release_channel(uap
->dmatx
.chan
);
416 dma_release_channel(uap
->dmarx
.chan
);
419 /* Forward declare these for the refill routine */
420 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
);
421 static void pl011_start_tx_pio(struct uart_amba_port
*uap
);
424 * The current DMA TX buffer has been sent.
425 * Try to queue up another DMA buffer.
427 static void pl011_dma_tx_callback(void *data
)
429 struct uart_amba_port
*uap
= data
;
430 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
434 spin_lock_irqsave(&uap
->port
.lock
, flags
);
435 if (uap
->dmatx
.queued
)
436 dma_unmap_sg(dmatx
->chan
->device
->dev
, &dmatx
->sg
, 1,
440 uap
->dmacr
= dmacr
& ~UART011_TXDMAE
;
441 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
444 * If TX DMA was disabled, it means that we've stopped the DMA for
445 * some reason (eg, XOFF received, or we want to send an X-char.)
447 * Note: we need to be careful here of a potential race between DMA
448 * and the rest of the driver - if the driver disables TX DMA while
449 * a TX buffer completing, we must update the tx queued status to
450 * get further refills (hence we check dmacr).
452 if (!(dmacr
& UART011_TXDMAE
) || uart_tx_stopped(&uap
->port
) ||
453 uart_circ_empty(&uap
->port
.state
->xmit
)) {
454 uap
->dmatx
.queued
= false;
455 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
459 if (pl011_dma_tx_refill(uap
) <= 0)
461 * We didn't queue a DMA buffer for some reason, but we
462 * have data pending to be sent. Re-enable the TX IRQ.
464 pl011_start_tx_pio(uap
);
466 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
470 * Try to refill the TX DMA buffer.
471 * Locking: called with port lock held and IRQs disabled.
473 * 1 if we queued up a TX DMA buffer.
474 * 0 if we didn't want to handle this by DMA
477 static int pl011_dma_tx_refill(struct uart_amba_port
*uap
)
479 struct pl011_dmatx_data
*dmatx
= &uap
->dmatx
;
480 struct dma_chan
*chan
= dmatx
->chan
;
481 struct dma_device
*dma_dev
= chan
->device
;
482 struct dma_async_tx_descriptor
*desc
;
483 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
487 * Try to avoid the overhead involved in using DMA if the
488 * transaction fits in the first half of the FIFO, by using
489 * the standard interrupt handling. This ensures that we
490 * issue a uart_write_wakeup() at the appropriate time.
492 count
= uart_circ_chars_pending(xmit
);
493 if (count
< (uap
->fifosize
>> 1)) {
494 uap
->dmatx
.queued
= false;
499 * Bodge: don't send the last character by DMA, as this
500 * will prevent XON from notifying us to restart DMA.
504 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
505 if (count
> PL011_DMA_BUFFER_SIZE
)
506 count
= PL011_DMA_BUFFER_SIZE
;
508 if (xmit
->tail
< xmit
->head
)
509 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], count
);
511 size_t first
= UART_XMIT_SIZE
- xmit
->tail
;
516 second
= count
- first
;
518 memcpy(&dmatx
->buf
[0], &xmit
->buf
[xmit
->tail
], first
);
520 memcpy(&dmatx
->buf
[first
], &xmit
->buf
[0], second
);
523 dmatx
->sg
.length
= count
;
525 if (dma_map_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
) != 1) {
526 uap
->dmatx
.queued
= false;
527 dev_dbg(uap
->port
.dev
, "unable to map TX DMA\n");
531 desc
= dmaengine_prep_slave_sg(chan
, &dmatx
->sg
, 1, DMA_MEM_TO_DEV
,
532 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
534 dma_unmap_sg(dma_dev
->dev
, &dmatx
->sg
, 1, DMA_TO_DEVICE
);
535 uap
->dmatx
.queued
= false;
537 * If DMA cannot be used right now, we complete this
538 * transaction via IRQ and let the TTY layer retry.
540 dev_dbg(uap
->port
.dev
, "TX DMA busy\n");
544 /* Some data to go along to the callback */
545 desc
->callback
= pl011_dma_tx_callback
;
546 desc
->callback_param
= uap
;
548 /* All errors should happen at prepare time */
549 dmaengine_submit(desc
);
551 /* Fire the DMA transaction */
552 dma_dev
->device_issue_pending(chan
);
554 uap
->dmacr
|= UART011_TXDMAE
;
555 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
556 uap
->dmatx
.queued
= true;
559 * Now we know that DMA will fire, so advance the ring buffer
560 * with the stuff we just dispatched.
562 xmit
->tail
= (xmit
->tail
+ count
) & (UART_XMIT_SIZE
- 1);
563 uap
->port
.icount
.tx
+= count
;
565 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
566 uart_write_wakeup(&uap
->port
);
572 * We received a transmit interrupt without a pending X-char but with
573 * pending characters.
574 * Locking: called with port lock held and IRQs disabled.
576 * false if we want to use PIO to transmit
577 * true if we queued a DMA buffer
579 static bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
581 if (!uap
->using_tx_dma
)
585 * If we already have a TX buffer queued, but received a
586 * TX interrupt, it will be because we've just sent an X-char.
587 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
589 if (uap
->dmatx
.queued
) {
590 uap
->dmacr
|= UART011_TXDMAE
;
591 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
592 uap
->im
&= ~UART011_TXIM
;
593 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
598 * We don't have a TX buffer queued, so try to queue one.
599 * If we successfully queued a buffer, mask the TX IRQ.
601 if (pl011_dma_tx_refill(uap
) > 0) {
602 uap
->im
&= ~UART011_TXIM
;
603 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
610 * Stop the DMA transmit (eg, due to received XOFF).
611 * Locking: called with port lock held and IRQs disabled.
613 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
615 if (uap
->dmatx
.queued
) {
616 uap
->dmacr
&= ~UART011_TXDMAE
;
617 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
622 * Try to start a DMA transmit, or in the case of an XON/OFF
623 * character queued for send, try to get that character out ASAP.
624 * Locking: called with port lock held and IRQs disabled.
626 * false if we want the TX IRQ to be enabled
627 * true if we have a buffer queued
629 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
633 if (!uap
->using_tx_dma
)
636 if (!uap
->port
.x_char
) {
637 /* no X-char, try to push chars out in DMA mode */
640 if (!uap
->dmatx
.queued
) {
641 if (pl011_dma_tx_refill(uap
) > 0) {
642 uap
->im
&= ~UART011_TXIM
;
643 writew(uap
->im
, uap
->port
.membase
+
647 } else if (!(uap
->dmacr
& UART011_TXDMAE
)) {
648 uap
->dmacr
|= UART011_TXDMAE
;
650 uap
->port
.membase
+ UART011_DMACR
);
656 * We have an X-char to send. Disable DMA to prevent it loading
657 * the TX fifo, and then see if we can stuff it into the FIFO.
660 uap
->dmacr
&= ~UART011_TXDMAE
;
661 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
663 if (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
) {
665 * No space in the FIFO, so enable the transmit interrupt
666 * so we know when there is space. Note that once we've
667 * loaded the character, we should just re-enable DMA.
672 writew(uap
->port
.x_char
, uap
->port
.membase
+ UART01x_DR
);
673 uap
->port
.icount
.tx
++;
674 uap
->port
.x_char
= 0;
676 /* Success - restore the DMA state */
678 writew(dmacr
, uap
->port
.membase
+ UART011_DMACR
);
684 * Flush the transmit buffer.
685 * Locking: called with port lock held and IRQs disabled.
687 static void pl011_dma_flush_buffer(struct uart_port
*port
)
688 __releases(&uap
->port
.lock
)
689 __acquires(&uap
->port
.lock
)
691 struct uart_amba_port
*uap
=
692 container_of(port
, struct uart_amba_port
, port
);
694 if (!uap
->using_tx_dma
)
697 /* Avoid deadlock with the DMA engine callback */
698 spin_unlock(&uap
->port
.lock
);
699 dmaengine_terminate_all(uap
->dmatx
.chan
);
700 spin_lock(&uap
->port
.lock
);
701 if (uap
->dmatx
.queued
) {
702 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
704 uap
->dmatx
.queued
= false;
705 uap
->dmacr
&= ~UART011_TXDMAE
;
706 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
710 static void pl011_dma_rx_callback(void *data
);
712 static int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
714 struct dma_chan
*rxchan
= uap
->dmarx
.chan
;
715 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
716 struct dma_async_tx_descriptor
*desc
;
717 struct pl011_sgbuf
*sgbuf
;
722 /* Start the RX DMA job */
723 sgbuf
= uap
->dmarx
.use_buf_b
?
724 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
725 desc
= dmaengine_prep_slave_sg(rxchan
, &sgbuf
->sg
, 1,
727 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
729 * If the DMA engine is busy and cannot prepare a
730 * channel, no big deal, the driver will fall back
731 * to interrupt mode as a result of this error code.
734 uap
->dmarx
.running
= false;
735 dmaengine_terminate_all(rxchan
);
739 /* Some data to go along to the callback */
740 desc
->callback
= pl011_dma_rx_callback
;
741 desc
->callback_param
= uap
;
742 dmarx
->cookie
= dmaengine_submit(desc
);
743 dma_async_issue_pending(rxchan
);
745 uap
->dmacr
|= UART011_RXDMAE
;
746 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
747 uap
->dmarx
.running
= true;
749 uap
->im
&= ~UART011_RXIM
;
750 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
756 * This is called when either the DMA job is complete, or
757 * the FIFO timeout interrupt occurred. This must be called
758 * with the port spinlock uap->port.lock held.
760 static void pl011_dma_rx_chars(struct uart_amba_port
*uap
,
761 u32 pending
, bool use_buf_b
,
764 struct tty_port
*port
= &uap
->port
.state
->port
;
765 struct pl011_sgbuf
*sgbuf
= use_buf_b
?
766 &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
768 u32 fifotaken
= 0; /* only used for vdbg() */
770 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
773 if (uap
->dmarx
.poll_rate
) {
774 /* The data can be taken by polling */
775 dmataken
= sgbuf
->sg
.length
- dmarx
->last_residue
;
776 /* Recalculate the pending size */
777 if (pending
>= dmataken
)
781 /* Pick the remain data from the DMA */
785 * First take all chars in the DMA pipe, then look in the FIFO.
786 * Note that tty_insert_flip_buf() tries to take as many chars
789 dma_count
= tty_insert_flip_string(port
, sgbuf
->buf
+ dmataken
,
792 uap
->port
.icount
.rx
+= dma_count
;
793 if (dma_count
< pending
)
794 dev_warn(uap
->port
.dev
,
795 "couldn't insert all characters (TTY is full?)\n");
798 /* Reset the last_residue for Rx DMA poll */
799 if (uap
->dmarx
.poll_rate
)
800 dmarx
->last_residue
= sgbuf
->sg
.length
;
803 * Only continue with trying to read the FIFO if all DMA chars have
806 if (dma_count
== pending
&& readfifo
) {
807 /* Clear any error flags */
808 writew(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
| UART011_FEIS
,
809 uap
->port
.membase
+ UART011_ICR
);
812 * If we read all the DMA'd characters, and we had an
813 * incomplete buffer, that could be due to an rx error, or
814 * maybe we just timed out. Read any pending chars and check
817 * Error conditions will only occur in the FIFO, these will
818 * trigger an immediate interrupt and stop the DMA job, so we
819 * will always find the error in the FIFO, never in the DMA
822 fifotaken
= pl011_fifo_to_tty(uap
);
825 spin_unlock(&uap
->port
.lock
);
826 dev_vdbg(uap
->port
.dev
,
827 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
828 dma_count
, fifotaken
);
829 tty_flip_buffer_push(port
);
830 spin_lock(&uap
->port
.lock
);
833 static void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
835 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
836 struct dma_chan
*rxchan
= dmarx
->chan
;
837 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
838 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
840 struct dma_tx_state state
;
841 enum dma_status dmastat
;
844 * Pause the transfer so we can trust the current counter,
845 * do this before we pause the PL011 block, else we may
848 if (dmaengine_pause(rxchan
))
849 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
850 dmastat
= rxchan
->device
->device_tx_status(rxchan
,
851 dmarx
->cookie
, &state
);
852 if (dmastat
!= DMA_PAUSED
)
853 dev_err(uap
->port
.dev
, "unable to pause DMA transfer\n");
855 /* Disable RX DMA - incoming data will wait in the FIFO */
856 uap
->dmacr
&= ~UART011_RXDMAE
;
857 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
858 uap
->dmarx
.running
= false;
860 pending
= sgbuf
->sg
.length
- state
.residue
;
861 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
862 /* Then we terminate the transfer - we now know our residue */
863 dmaengine_terminate_all(rxchan
);
866 * This will take the chars we have so far and insert
867 * into the framework.
869 pl011_dma_rx_chars(uap
, pending
, dmarx
->use_buf_b
, true);
871 /* Switch buffer & re-trigger DMA job */
872 dmarx
->use_buf_b
= !dmarx
->use_buf_b
;
873 if (pl011_dma_rx_trigger_dma(uap
)) {
874 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
875 "fall back to interrupt mode\n");
876 uap
->im
|= UART011_RXIM
;
877 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
881 static void pl011_dma_rx_callback(void *data
)
883 struct uart_amba_port
*uap
= data
;
884 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
885 struct dma_chan
*rxchan
= dmarx
->chan
;
886 bool lastbuf
= dmarx
->use_buf_b
;
887 struct pl011_sgbuf
*sgbuf
= dmarx
->use_buf_b
?
888 &dmarx
->sgbuf_b
: &dmarx
->sgbuf_a
;
890 struct dma_tx_state state
;
894 * This completion interrupt occurs typically when the
895 * RX buffer is totally stuffed but no timeout has yet
896 * occurred. When that happens, we just want the RX
897 * routine to flush out the secondary DMA buffer while
898 * we immediately trigger the next DMA job.
900 spin_lock_irq(&uap
->port
.lock
);
902 * Rx data can be taken by the UART interrupts during
903 * the DMA irq handler. So we check the residue here.
905 rxchan
->device
->device_tx_status(rxchan
, dmarx
->cookie
, &state
);
906 pending
= sgbuf
->sg
.length
- state
.residue
;
907 BUG_ON(pending
> PL011_DMA_BUFFER_SIZE
);
908 /* Then we terminate the transfer - we now know our residue */
909 dmaengine_terminate_all(rxchan
);
911 uap
->dmarx
.running
= false;
912 dmarx
->use_buf_b
= !lastbuf
;
913 ret
= pl011_dma_rx_trigger_dma(uap
);
915 pl011_dma_rx_chars(uap
, pending
, lastbuf
, false);
916 spin_unlock_irq(&uap
->port
.lock
);
918 * Do this check after we picked the DMA chars so we don't
919 * get some IRQ immediately from RX.
922 dev_dbg(uap
->port
.dev
, "could not retrigger RX DMA job "
923 "fall back to interrupt mode\n");
924 uap
->im
|= UART011_RXIM
;
925 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
930 * Stop accepting received characters, when we're shutting down or
931 * suspending this port.
932 * Locking: called with port lock held and IRQs disabled.
934 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
936 /* FIXME. Just disable the DMA enable */
937 uap
->dmacr
&= ~UART011_RXDMAE
;
938 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
942 * Timer handler for Rx DMA polling.
943 * Every polling, It checks the residue in the dma buffer and transfer
944 * data to the tty. Also, last_residue is updated for the next polling.
946 static void pl011_dma_rx_poll(unsigned long args
)
948 struct uart_amba_port
*uap
= (struct uart_amba_port
*)args
;
949 struct tty_port
*port
= &uap
->port
.state
->port
;
950 struct pl011_dmarx_data
*dmarx
= &uap
->dmarx
;
951 struct dma_chan
*rxchan
= uap
->dmarx
.chan
;
952 unsigned long flags
= 0;
953 unsigned int dmataken
= 0;
954 unsigned int size
= 0;
955 struct pl011_sgbuf
*sgbuf
;
957 struct dma_tx_state state
;
959 sgbuf
= dmarx
->use_buf_b
? &uap
->dmarx
.sgbuf_b
: &uap
->dmarx
.sgbuf_a
;
960 rxchan
->device
->device_tx_status(rxchan
, dmarx
->cookie
, &state
);
961 if (likely(state
.residue
< dmarx
->last_residue
)) {
962 dmataken
= sgbuf
->sg
.length
- dmarx
->last_residue
;
963 size
= dmarx
->last_residue
- state
.residue
;
964 dma_count
= tty_insert_flip_string(port
, sgbuf
->buf
+ dmataken
,
966 if (dma_count
== size
)
967 dmarx
->last_residue
= state
.residue
;
968 dmarx
->last_jiffies
= jiffies
;
970 tty_flip_buffer_push(port
);
973 * If no data is received in poll_timeout, the driver will fall back
974 * to interrupt mode. We will retrigger DMA at the first interrupt.
976 if (jiffies_to_msecs(jiffies
- dmarx
->last_jiffies
)
977 > uap
->dmarx
.poll_timeout
) {
979 spin_lock_irqsave(&uap
->port
.lock
, flags
);
980 pl011_dma_rx_stop(uap
);
981 uap
->im
|= UART011_RXIM
;
982 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
983 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
985 uap
->dmarx
.running
= false;
986 dmaengine_terminate_all(rxchan
);
987 del_timer(&uap
->dmarx
.timer
);
989 mod_timer(&uap
->dmarx
.timer
,
990 jiffies
+ msecs_to_jiffies(uap
->dmarx
.poll_rate
));
994 static void pl011_dma_startup(struct uart_amba_port
*uap
)
998 if (!uap
->dma_probed
)
999 pl011_dma_probe(uap
);
1001 if (!uap
->dmatx
.chan
)
1004 uap
->dmatx
.buf
= kmalloc(PL011_DMA_BUFFER_SIZE
, GFP_KERNEL
| __GFP_DMA
);
1005 if (!uap
->dmatx
.buf
) {
1006 dev_err(uap
->port
.dev
, "no memory for DMA TX buffer\n");
1007 uap
->port
.fifosize
= uap
->fifosize
;
1011 sg_init_one(&uap
->dmatx
.sg
, uap
->dmatx
.buf
, PL011_DMA_BUFFER_SIZE
);
1013 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1014 uap
->port
.fifosize
= PL011_DMA_BUFFER_SIZE
;
1015 uap
->using_tx_dma
= true;
1017 if (!uap
->dmarx
.chan
)
1020 /* Allocate and map DMA RX buffers */
1021 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
1024 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
1025 "RX buffer A", ret
);
1029 ret
= pl011_sgbuf_init(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
,
1032 dev_err(uap
->port
.dev
, "failed to init DMA %s: %d\n",
1033 "RX buffer B", ret
);
1034 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
,
1039 uap
->using_rx_dma
= true;
1042 /* Turn on DMA error (RX/TX will be enabled on demand) */
1043 uap
->dmacr
|= UART011_DMAONERR
;
1044 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
1047 * ST Micro variants has some specific dma burst threshold
1048 * compensation. Set this to 16 bytes, so burst will only
1049 * be issued above/below 16 bytes.
1051 if (uap
->vendor
->dma_threshold
)
1052 writew(ST_UART011_DMAWM_RX_16
| ST_UART011_DMAWM_TX_16
,
1053 uap
->port
.membase
+ ST_UART011_DMAWM
);
1055 if (uap
->using_rx_dma
) {
1056 if (pl011_dma_rx_trigger_dma(uap
))
1057 dev_dbg(uap
->port
.dev
, "could not trigger initial "
1058 "RX DMA job, fall back to interrupt mode\n");
1059 if (uap
->dmarx
.poll_rate
) {
1060 init_timer(&(uap
->dmarx
.timer
));
1061 uap
->dmarx
.timer
.function
= pl011_dma_rx_poll
;
1062 uap
->dmarx
.timer
.data
= (unsigned long)uap
;
1063 mod_timer(&uap
->dmarx
.timer
,
1065 msecs_to_jiffies(uap
->dmarx
.poll_rate
));
1066 uap
->dmarx
.last_residue
= PL011_DMA_BUFFER_SIZE
;
1067 uap
->dmarx
.last_jiffies
= jiffies
;
1072 static void pl011_dma_shutdown(struct uart_amba_port
*uap
)
1074 if (!(uap
->using_tx_dma
|| uap
->using_rx_dma
))
1077 /* Disable RX and TX DMA */
1078 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_BUSY
)
1081 spin_lock_irq(&uap
->port
.lock
);
1082 uap
->dmacr
&= ~(UART011_DMAONERR
| UART011_RXDMAE
| UART011_TXDMAE
);
1083 writew(uap
->dmacr
, uap
->port
.membase
+ UART011_DMACR
);
1084 spin_unlock_irq(&uap
->port
.lock
);
1086 if (uap
->using_tx_dma
) {
1087 /* In theory, this should already be done by pl011_dma_flush_buffer */
1088 dmaengine_terminate_all(uap
->dmatx
.chan
);
1089 if (uap
->dmatx
.queued
) {
1090 dma_unmap_sg(uap
->dmatx
.chan
->device
->dev
, &uap
->dmatx
.sg
, 1,
1092 uap
->dmatx
.queued
= false;
1095 kfree(uap
->dmatx
.buf
);
1096 uap
->using_tx_dma
= false;
1099 if (uap
->using_rx_dma
) {
1100 dmaengine_terminate_all(uap
->dmarx
.chan
);
1101 /* Clean up the RX DMA */
1102 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_a
, DMA_FROM_DEVICE
);
1103 pl011_sgbuf_free(uap
->dmarx
.chan
, &uap
->dmarx
.sgbuf_b
, DMA_FROM_DEVICE
);
1104 if (uap
->dmarx
.poll_rate
)
1105 del_timer_sync(&uap
->dmarx
.timer
);
1106 uap
->using_rx_dma
= false;
1110 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
1112 return uap
->using_rx_dma
;
1115 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
1117 return uap
->using_rx_dma
&& uap
->dmarx
.running
;
1121 /* Blank functions if the DMA engine is not available */
1122 static inline void pl011_dma_probe(struct uart_amba_port
*uap
)
1126 static inline void pl011_dma_remove(struct uart_amba_port
*uap
)
1130 static inline void pl011_dma_startup(struct uart_amba_port
*uap
)
1134 static inline void pl011_dma_shutdown(struct uart_amba_port
*uap
)
1138 static inline bool pl011_dma_tx_irq(struct uart_amba_port
*uap
)
1143 static inline void pl011_dma_tx_stop(struct uart_amba_port
*uap
)
1147 static inline bool pl011_dma_tx_start(struct uart_amba_port
*uap
)
1152 static inline void pl011_dma_rx_irq(struct uart_amba_port
*uap
)
1156 static inline void pl011_dma_rx_stop(struct uart_amba_port
*uap
)
1160 static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port
*uap
)
1165 static inline bool pl011_dma_rx_available(struct uart_amba_port
*uap
)
1170 static inline bool pl011_dma_rx_running(struct uart_amba_port
*uap
)
1175 #define pl011_dma_flush_buffer NULL
1178 static void pl011_stop_tx(struct uart_port
*port
)
1180 struct uart_amba_port
*uap
=
1181 container_of(port
, struct uart_amba_port
, port
);
1183 uap
->im
&= ~UART011_TXIM
;
1184 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1185 pl011_dma_tx_stop(uap
);
1188 static void pl011_tx_chars(struct uart_amba_port
*uap
, bool from_irq
);
1190 /* Start TX with programmed I/O only (no DMA) */
1191 static void pl011_start_tx_pio(struct uart_amba_port
*uap
)
1193 uap
->im
|= UART011_TXIM
;
1194 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1195 pl011_tx_chars(uap
, false);
1198 static void pl011_start_tx(struct uart_port
*port
)
1200 struct uart_amba_port
*uap
=
1201 container_of(port
, struct uart_amba_port
, port
);
1203 if (!pl011_dma_tx_start(uap
))
1204 pl011_start_tx_pio(uap
);
1207 static void pl011_stop_rx(struct uart_port
*port
)
1209 struct uart_amba_port
*uap
=
1210 container_of(port
, struct uart_amba_port
, port
);
1212 uap
->im
&= ~(UART011_RXIM
|UART011_RTIM
|UART011_FEIM
|
1213 UART011_PEIM
|UART011_BEIM
|UART011_OEIM
);
1214 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1216 pl011_dma_rx_stop(uap
);
1219 static void pl011_enable_ms(struct uart_port
*port
)
1221 struct uart_amba_port
*uap
=
1222 container_of(port
, struct uart_amba_port
, port
);
1224 uap
->im
|= UART011_RIMIM
|UART011_CTSMIM
|UART011_DCDMIM
|UART011_DSRMIM
;
1225 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1228 static void pl011_rx_chars(struct uart_amba_port
*uap
)
1229 __releases(&uap
->port
.lock
)
1230 __acquires(&uap
->port
.lock
)
1232 pl011_fifo_to_tty(uap
);
1234 spin_unlock(&uap
->port
.lock
);
1235 tty_flip_buffer_push(&uap
->port
.state
->port
);
1237 * If we were temporarily out of DMA mode for a while,
1238 * attempt to switch back to DMA mode again.
1240 if (pl011_dma_rx_available(uap
)) {
1241 if (pl011_dma_rx_trigger_dma(uap
)) {
1242 dev_dbg(uap
->port
.dev
, "could not trigger RX DMA job "
1243 "fall back to interrupt mode again\n");
1244 uap
->im
|= UART011_RXIM
;
1245 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1247 #ifdef CONFIG_DMA_ENGINE
1248 /* Start Rx DMA poll */
1249 if (uap
->dmarx
.poll_rate
) {
1250 uap
->dmarx
.last_jiffies
= jiffies
;
1251 uap
->dmarx
.last_residue
= PL011_DMA_BUFFER_SIZE
;
1252 mod_timer(&uap
->dmarx
.timer
,
1254 msecs_to_jiffies(uap
->dmarx
.poll_rate
));
1259 spin_lock(&uap
->port
.lock
);
1262 static bool pl011_tx_char(struct uart_amba_port
*uap
, unsigned char c
,
1265 if (unlikely(!from_irq
) &&
1266 readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
1267 return false; /* unable to transmit character */
1269 writew(c
, uap
->port
.membase
+ UART01x_DR
);
1270 uap
->port
.icount
.tx
++;
1275 static void pl011_tx_chars(struct uart_amba_port
*uap
, bool from_irq
)
1277 struct circ_buf
*xmit
= &uap
->port
.state
->xmit
;
1278 int count
= uap
->fifosize
>> 1;
1280 if (uap
->port
.x_char
) {
1281 if (!pl011_tx_char(uap
, uap
->port
.x_char
, from_irq
))
1283 uap
->port
.x_char
= 0;
1286 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uap
->port
)) {
1287 pl011_stop_tx(&uap
->port
);
1291 /* If we are using DMA mode, try to send some characters. */
1292 if (pl011_dma_tx_irq(uap
))
1296 if (likely(from_irq
) && count
-- == 0)
1299 if (!pl011_tx_char(uap
, xmit
->buf
[xmit
->tail
], from_irq
))
1302 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
1303 } while (!uart_circ_empty(xmit
));
1305 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
1306 uart_write_wakeup(&uap
->port
);
1308 if (uart_circ_empty(xmit
))
1309 pl011_stop_tx(&uap
->port
);
1312 static void pl011_modem_status(struct uart_amba_port
*uap
)
1314 unsigned int status
, delta
;
1316 status
= readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_MODEM_ANY
;
1318 delta
= status
^ uap
->old_status
;
1319 uap
->old_status
= status
;
1324 if (delta
& UART01x_FR_DCD
)
1325 uart_handle_dcd_change(&uap
->port
, status
& UART01x_FR_DCD
);
1327 if (delta
& UART01x_FR_DSR
)
1328 uap
->port
.icount
.dsr
++;
1330 if (delta
& UART01x_FR_CTS
)
1331 uart_handle_cts_change(&uap
->port
, status
& UART01x_FR_CTS
);
1333 wake_up_interruptible(&uap
->port
.state
->port
.delta_msr_wait
);
1336 static void check_apply_cts_event_workaround(struct uart_amba_port
*uap
)
1338 unsigned int dummy_read
;
1340 if (!uap
->vendor
->cts_event_workaround
)
1343 /* workaround to make sure that all bits are unlocked.. */
1344 writew(0x00, uap
->port
.membase
+ UART011_ICR
);
1347 * WA: introduce 26ns(1 uart clk) delay before W1C;
1348 * single apb access will incur 2 pclk(133.12Mhz) delay,
1349 * so add 2 dummy reads
1351 dummy_read
= readw(uap
->port
.membase
+ UART011_ICR
);
1352 dummy_read
= readw(uap
->port
.membase
+ UART011_ICR
);
1355 static irqreturn_t
pl011_int(int irq
, void *dev_id
)
1357 struct uart_amba_port
*uap
= dev_id
;
1358 unsigned long flags
;
1359 unsigned int status
, pass_counter
= AMBA_ISR_PASS_LIMIT
;
1363 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1364 imsc
= readw(uap
->port
.membase
+ UART011_IMSC
);
1365 status
= readw(uap
->port
.membase
+ UART011_RIS
) & imsc
;
1368 check_apply_cts_event_workaround(uap
);
1370 writew(status
& ~(UART011_TXIS
|UART011_RTIS
|
1372 uap
->port
.membase
+ UART011_ICR
);
1374 if (status
& (UART011_RTIS
|UART011_RXIS
)) {
1375 if (pl011_dma_rx_running(uap
))
1376 pl011_dma_rx_irq(uap
);
1378 pl011_rx_chars(uap
);
1380 if (status
& (UART011_DSRMIS
|UART011_DCDMIS
|
1381 UART011_CTSMIS
|UART011_RIMIS
))
1382 pl011_modem_status(uap
);
1383 if (status
& UART011_TXIS
)
1384 pl011_tx_chars(uap
, true);
1386 if (pass_counter
-- == 0)
1389 status
= readw(uap
->port
.membase
+ UART011_RIS
) & imsc
;
1390 } while (status
!= 0);
1394 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1396 return IRQ_RETVAL(handled
);
1399 static unsigned int pl011_tx_empty(struct uart_port
*port
)
1401 struct uart_amba_port
*uap
=
1402 container_of(port
, struct uart_amba_port
, port
);
1403 unsigned int status
= readw(uap
->port
.membase
+ UART01x_FR
);
1404 return status
& (UART01x_FR_BUSY
|UART01x_FR_TXFF
) ? 0 : TIOCSER_TEMT
;
1407 static unsigned int pl011_get_mctrl(struct uart_port
*port
)
1409 struct uart_amba_port
*uap
=
1410 container_of(port
, struct uart_amba_port
, port
);
1411 unsigned int result
= 0;
1412 unsigned int status
= readw(uap
->port
.membase
+ UART01x_FR
);
1414 #define TIOCMBIT(uartbit, tiocmbit) \
1415 if (status & uartbit) \
1418 TIOCMBIT(UART01x_FR_DCD
, TIOCM_CAR
);
1419 TIOCMBIT(UART01x_FR_DSR
, TIOCM_DSR
);
1420 TIOCMBIT(UART01x_FR_CTS
, TIOCM_CTS
);
1421 TIOCMBIT(UART011_FR_RI
, TIOCM_RNG
);
1426 static void pl011_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1428 struct uart_amba_port
*uap
=
1429 container_of(port
, struct uart_amba_port
, port
);
1432 cr
= readw(uap
->port
.membase
+ UART011_CR
);
1434 #define TIOCMBIT(tiocmbit, uartbit) \
1435 if (mctrl & tiocmbit) \
1440 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTS
);
1441 TIOCMBIT(TIOCM_DTR
, UART011_CR_DTR
);
1442 TIOCMBIT(TIOCM_OUT1
, UART011_CR_OUT1
);
1443 TIOCMBIT(TIOCM_OUT2
, UART011_CR_OUT2
);
1444 TIOCMBIT(TIOCM_LOOP
, UART011_CR_LBE
);
1447 /* We need to disable auto-RTS if we want to turn RTS off */
1448 TIOCMBIT(TIOCM_RTS
, UART011_CR_RTSEN
);
1452 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1455 static void pl011_break_ctl(struct uart_port
*port
, int break_state
)
1457 struct uart_amba_port
*uap
=
1458 container_of(port
, struct uart_amba_port
, port
);
1459 unsigned long flags
;
1462 spin_lock_irqsave(&uap
->port
.lock
, flags
);
1463 lcr_h
= readw(uap
->port
.membase
+ uap
->lcrh_tx
);
1464 if (break_state
== -1)
1465 lcr_h
|= UART01x_LCRH_BRK
;
1467 lcr_h
&= ~UART01x_LCRH_BRK
;
1468 writew(lcr_h
, uap
->port
.membase
+ uap
->lcrh_tx
);
1469 spin_unlock_irqrestore(&uap
->port
.lock
, flags
);
1472 #ifdef CONFIG_CONSOLE_POLL
1474 static void pl011_quiesce_irqs(struct uart_port
*port
)
1476 struct uart_amba_port
*uap
=
1477 container_of(port
, struct uart_amba_port
, port
);
1478 unsigned char __iomem
*regs
= uap
->port
.membase
;
1480 writew(readw(regs
+ UART011_MIS
), regs
+ UART011_ICR
);
1482 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1483 * we simply mask it. start_tx() will unmask it.
1485 * Note we can race with start_tx(), and if the race happens, the
1486 * polling user might get another interrupt just after we clear it.
1487 * But it should be OK and can happen even w/o the race, e.g.
1488 * controller immediately got some new data and raised the IRQ.
1490 * And whoever uses polling routines assumes that it manages the device
1491 * (including tx queue), so we're also fine with start_tx()'s caller
1494 writew(readw(regs
+ UART011_IMSC
) & ~UART011_TXIM
, regs
+ UART011_IMSC
);
1497 static int pl011_get_poll_char(struct uart_port
*port
)
1499 struct uart_amba_port
*uap
=
1500 container_of(port
, struct uart_amba_port
, port
);
1501 unsigned int status
;
1504 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1507 pl011_quiesce_irqs(port
);
1509 status
= readw(uap
->port
.membase
+ UART01x_FR
);
1510 if (status
& UART01x_FR_RXFE
)
1511 return NO_POLL_CHAR
;
1513 return readw(uap
->port
.membase
+ UART01x_DR
);
1516 static void pl011_put_poll_char(struct uart_port
*port
,
1519 struct uart_amba_port
*uap
=
1520 container_of(port
, struct uart_amba_port
, port
);
1522 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
1525 writew(ch
, uap
->port
.membase
+ UART01x_DR
);
1528 #endif /* CONFIG_CONSOLE_POLL */
1530 static int pl011_hwinit(struct uart_port
*port
)
1532 struct uart_amba_port
*uap
=
1533 container_of(port
, struct uart_amba_port
, port
);
1536 /* Optionaly enable pins to be muxed in and configured */
1537 pinctrl_pm_select_default_state(port
->dev
);
1540 * Try to enable the clock producer.
1542 retval
= clk_prepare_enable(uap
->clk
);
1546 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
1548 /* Clear pending error and receive interrupts */
1549 writew(UART011_OEIS
| UART011_BEIS
| UART011_PEIS
| UART011_FEIS
|
1550 UART011_RTIS
| UART011_RXIS
, uap
->port
.membase
+ UART011_ICR
);
1553 * Save interrupts enable mask, and enable RX interrupts in case if
1554 * the interrupt is used for NMI entry.
1556 uap
->im
= readw(uap
->port
.membase
+ UART011_IMSC
);
1557 writew(UART011_RTIM
| UART011_RXIM
, uap
->port
.membase
+ UART011_IMSC
);
1559 if (dev_get_platdata(uap
->port
.dev
)) {
1560 struct amba_pl011_data
*plat
;
1562 plat
= dev_get_platdata(uap
->port
.dev
);
1569 static void pl011_write_lcr_h(struct uart_amba_port
*uap
, unsigned int lcr_h
)
1571 writew(lcr_h
, uap
->port
.membase
+ uap
->lcrh_rx
);
1572 if (uap
->lcrh_rx
!= uap
->lcrh_tx
) {
1575 * Wait 10 PCLKs before writing LCRH_TX register,
1576 * to get this delay write read only register 10 times
1578 for (i
= 0; i
< 10; ++i
)
1579 writew(0xff, uap
->port
.membase
+ UART011_MIS
);
1580 writew(lcr_h
, uap
->port
.membase
+ uap
->lcrh_tx
);
1584 static int pl011_allocate_irq(struct uart_amba_port
*uap
)
1586 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1588 return request_irq(uap
->port
.irq
, pl011_int
, 0, "uart-pl011", uap
);
1592 * Enable interrupts, only timeouts when using DMA
1593 * if initial RX DMA job failed, start in interrupt mode
1596 static void pl011_enable_interrupts(struct uart_amba_port
*uap
)
1598 spin_lock_irq(&uap
->port
.lock
);
1600 /* Clear out any spuriously appearing RX interrupts */
1601 writew(UART011_RTIS
| UART011_RXIS
,
1602 uap
->port
.membase
+ UART011_ICR
);
1603 uap
->im
= UART011_RTIM
;
1604 if (!pl011_dma_rx_running(uap
))
1605 uap
->im
|= UART011_RXIM
;
1606 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1607 spin_unlock_irq(&uap
->port
.lock
);
1610 static int pl011_startup(struct uart_port
*port
)
1612 struct uart_amba_port
*uap
=
1613 container_of(port
, struct uart_amba_port
, port
);
1617 retval
= pl011_hwinit(port
);
1621 retval
= pl011_allocate_irq(uap
);
1625 writew(uap
->vendor
->ifls
, uap
->port
.membase
+ UART011_IFLS
);
1627 spin_lock_irq(&uap
->port
.lock
);
1629 /* restore RTS and DTR */
1630 cr
= uap
->old_cr
& (UART011_CR_RTS
| UART011_CR_DTR
);
1631 cr
|= UART01x_CR_UARTEN
| UART011_CR_RXE
| UART011_CR_TXE
;
1632 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1634 spin_unlock_irq(&uap
->port
.lock
);
1637 * initialise the old status of the modem signals
1639 uap
->old_status
= readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_MODEM_ANY
;
1642 pl011_dma_startup(uap
);
1644 pl011_enable_interrupts(uap
);
1649 clk_disable_unprepare(uap
->clk
);
1653 static int sbsa_uart_startup(struct uart_port
*port
)
1655 struct uart_amba_port
*uap
=
1656 container_of(port
, struct uart_amba_port
, port
);
1659 retval
= pl011_hwinit(port
);
1663 retval
= pl011_allocate_irq(uap
);
1667 /* The SBSA UART does not support any modem status lines. */
1668 uap
->old_status
= 0;
1670 pl011_enable_interrupts(uap
);
1675 static void pl011_shutdown_channel(struct uart_amba_port
*uap
,
1680 val
= readw(uap
->port
.membase
+ lcrh
);
1681 val
&= ~(UART01x_LCRH_BRK
| UART01x_LCRH_FEN
);
1682 writew(val
, uap
->port
.membase
+ lcrh
);
1686 * disable the port. It should not disable RTS and DTR.
1687 * Also RTS and DTR state should be preserved to restore
1688 * it during startup().
1690 static void pl011_disable_uart(struct uart_amba_port
*uap
)
1694 uap
->autorts
= false;
1695 spin_lock_irq(&uap
->port
.lock
);
1696 cr
= readw(uap
->port
.membase
+ UART011_CR
);
1698 cr
&= UART011_CR_RTS
| UART011_CR_DTR
;
1699 cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
1700 writew(cr
, uap
->port
.membase
+ UART011_CR
);
1701 spin_unlock_irq(&uap
->port
.lock
);
1704 * disable break condition and fifos
1706 pl011_shutdown_channel(uap
, uap
->lcrh_rx
);
1707 if (uap
->lcrh_rx
!= uap
->lcrh_tx
)
1708 pl011_shutdown_channel(uap
, uap
->lcrh_tx
);
1711 static void pl011_disable_interrupts(struct uart_amba_port
*uap
)
1713 spin_lock_irq(&uap
->port
.lock
);
1715 /* mask all interrupts and clear all pending ones */
1717 writew(uap
->im
, uap
->port
.membase
+ UART011_IMSC
);
1718 writew(0xffff, uap
->port
.membase
+ UART011_ICR
);
1720 spin_unlock_irq(&uap
->port
.lock
);
1723 static void pl011_shutdown(struct uart_port
*port
)
1725 struct uart_amba_port
*uap
=
1726 container_of(port
, struct uart_amba_port
, port
);
1728 pl011_disable_interrupts(uap
);
1730 pl011_dma_shutdown(uap
);
1732 free_irq(uap
->port
.irq
, uap
);
1734 pl011_disable_uart(uap
);
1737 * Shut down the clock producer
1739 clk_disable_unprepare(uap
->clk
);
1740 /* Optionally let pins go into sleep states */
1741 pinctrl_pm_select_sleep_state(port
->dev
);
1743 if (dev_get_platdata(uap
->port
.dev
)) {
1744 struct amba_pl011_data
*plat
;
1746 plat
= dev_get_platdata(uap
->port
.dev
);
1751 if (uap
->port
.ops
->flush_buffer
)
1752 uap
->port
.ops
->flush_buffer(port
);
1755 static void sbsa_uart_shutdown(struct uart_port
*port
)
1757 struct uart_amba_port
*uap
=
1758 container_of(port
, struct uart_amba_port
, port
);
1760 pl011_disable_interrupts(uap
);
1762 free_irq(uap
->port
.irq
, uap
);
1764 if (uap
->port
.ops
->flush_buffer
)
1765 uap
->port
.ops
->flush_buffer(port
);
1769 pl011_setup_status_masks(struct uart_port
*port
, struct ktermios
*termios
)
1771 port
->read_status_mask
= UART011_DR_OE
| 255;
1772 if (termios
->c_iflag
& INPCK
)
1773 port
->read_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1774 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1775 port
->read_status_mask
|= UART011_DR_BE
;
1778 * Characters to ignore
1780 port
->ignore_status_mask
= 0;
1781 if (termios
->c_iflag
& IGNPAR
)
1782 port
->ignore_status_mask
|= UART011_DR_FE
| UART011_DR_PE
;
1783 if (termios
->c_iflag
& IGNBRK
) {
1784 port
->ignore_status_mask
|= UART011_DR_BE
;
1786 * If we're ignoring parity and break indicators,
1787 * ignore overruns too (for real raw support).
1789 if (termios
->c_iflag
& IGNPAR
)
1790 port
->ignore_status_mask
|= UART011_DR_OE
;
1794 * Ignore all characters if CREAD is not set.
1796 if ((termios
->c_cflag
& CREAD
) == 0)
1797 port
->ignore_status_mask
|= UART_DUMMY_DR_RX
;
1801 pl011_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1802 struct ktermios
*old
)
1804 struct uart_amba_port
*uap
=
1805 container_of(port
, struct uart_amba_port
, port
);
1806 unsigned int lcr_h
, old_cr
;
1807 unsigned long flags
;
1808 unsigned int baud
, quot
, clkdiv
;
1810 if (uap
->vendor
->oversampling
)
1816 * Ask the core to calculate the divisor for us.
1818 baud
= uart_get_baud_rate(port
, termios
, old
, 0,
1819 port
->uartclk
/ clkdiv
);
1820 #ifdef CONFIG_DMA_ENGINE
1822 * Adjust RX DMA polling rate with baud rate if not specified.
1824 if (uap
->dmarx
.auto_poll_rate
)
1825 uap
->dmarx
.poll_rate
= DIV_ROUND_UP(10000000, baud
);
1828 if (baud
> port
->uartclk
/16)
1829 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 8, baud
);
1831 quot
= DIV_ROUND_CLOSEST(port
->uartclk
* 4, baud
);
1833 switch (termios
->c_cflag
& CSIZE
) {
1835 lcr_h
= UART01x_LCRH_WLEN_5
;
1838 lcr_h
= UART01x_LCRH_WLEN_6
;
1841 lcr_h
= UART01x_LCRH_WLEN_7
;
1844 lcr_h
= UART01x_LCRH_WLEN_8
;
1847 if (termios
->c_cflag
& CSTOPB
)
1848 lcr_h
|= UART01x_LCRH_STP2
;
1849 if (termios
->c_cflag
& PARENB
) {
1850 lcr_h
|= UART01x_LCRH_PEN
;
1851 if (!(termios
->c_cflag
& PARODD
))
1852 lcr_h
|= UART01x_LCRH_EPS
;
1854 if (uap
->fifosize
> 1)
1855 lcr_h
|= UART01x_LCRH_FEN
;
1857 spin_lock_irqsave(&port
->lock
, flags
);
1860 * Update the per-port timeout.
1862 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1864 pl011_setup_status_masks(port
, termios
);
1866 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1867 pl011_enable_ms(port
);
1869 /* first, disable everything */
1870 old_cr
= readw(port
->membase
+ UART011_CR
);
1871 writew(0, port
->membase
+ UART011_CR
);
1873 if (termios
->c_cflag
& CRTSCTS
) {
1874 if (old_cr
& UART011_CR_RTS
)
1875 old_cr
|= UART011_CR_RTSEN
;
1877 old_cr
|= UART011_CR_CTSEN
;
1878 uap
->autorts
= true;
1880 old_cr
&= ~(UART011_CR_CTSEN
| UART011_CR_RTSEN
);
1881 uap
->autorts
= false;
1884 if (uap
->vendor
->oversampling
) {
1885 if (baud
> port
->uartclk
/ 16)
1886 old_cr
|= ST_UART011_CR_OVSFACT
;
1888 old_cr
&= ~ST_UART011_CR_OVSFACT
;
1892 * Workaround for the ST Micro oversampling variants to
1893 * increase the bitrate slightly, by lowering the divisor,
1894 * to avoid delayed sampling of start bit at high speeds,
1895 * else we see data corruption.
1897 if (uap
->vendor
->oversampling
) {
1898 if ((baud
>= 3000000) && (baud
< 3250000) && (quot
> 1))
1900 else if ((baud
> 3250000) && (quot
> 2))
1904 writew(quot
& 0x3f, port
->membase
+ UART011_FBRD
);
1905 writew(quot
>> 6, port
->membase
+ UART011_IBRD
);
1908 * ----------v----------v----------v----------v-----
1909 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1910 * UART011_FBRD & UART011_IBRD.
1911 * ----------^----------^----------^----------^-----
1913 pl011_write_lcr_h(uap
, lcr_h
);
1914 writew(old_cr
, port
->membase
+ UART011_CR
);
1916 spin_unlock_irqrestore(&port
->lock
, flags
);
1920 sbsa_uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1921 struct ktermios
*old
)
1923 struct uart_amba_port
*uap
=
1924 container_of(port
, struct uart_amba_port
, port
);
1925 unsigned long flags
;
1927 tty_termios_encode_baud_rate(termios
, uap
->fixed_baud
, uap
->fixed_baud
);
1929 /* The SBSA UART only supports 8n1 without hardware flow control. */
1930 termios
->c_cflag
&= ~(CSIZE
| CSTOPB
| PARENB
| PARODD
);
1931 termios
->c_cflag
&= ~(CMSPAR
| CRTSCTS
);
1932 termios
->c_cflag
|= CS8
| CLOCAL
;
1934 spin_lock_irqsave(&port
->lock
, flags
);
1935 uart_update_timeout(port
, CS8
, uap
->fixed_baud
);
1936 pl011_setup_status_masks(port
, termios
);
1937 spin_unlock_irqrestore(&port
->lock
, flags
);
1940 static const char *pl011_type(struct uart_port
*port
)
1942 struct uart_amba_port
*uap
=
1943 container_of(port
, struct uart_amba_port
, port
);
1944 return uap
->port
.type
== PORT_AMBA
? uap
->type
: NULL
;
1948 * Release the memory region(s) being used by 'port'
1950 static void pl011_release_port(struct uart_port
*port
)
1952 release_mem_region(port
->mapbase
, SZ_4K
);
1956 * Request the memory region(s) being used by 'port'
1958 static int pl011_request_port(struct uart_port
*port
)
1960 return request_mem_region(port
->mapbase
, SZ_4K
, "uart-pl011")
1961 != NULL
? 0 : -EBUSY
;
1965 * Configure/autoconfigure the port.
1967 static void pl011_config_port(struct uart_port
*port
, int flags
)
1969 if (flags
& UART_CONFIG_TYPE
) {
1970 port
->type
= PORT_AMBA
;
1971 pl011_request_port(port
);
1976 * verify the new serial_struct (for TIOCSSERIAL).
1978 static int pl011_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1981 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_AMBA
)
1983 if (ser
->irq
< 0 || ser
->irq
>= nr_irqs
)
1985 if (ser
->baud_base
< 9600)
1990 static struct uart_ops amba_pl011_pops
= {
1991 .tx_empty
= pl011_tx_empty
,
1992 .set_mctrl
= pl011_set_mctrl
,
1993 .get_mctrl
= pl011_get_mctrl
,
1994 .stop_tx
= pl011_stop_tx
,
1995 .start_tx
= pl011_start_tx
,
1996 .stop_rx
= pl011_stop_rx
,
1997 .enable_ms
= pl011_enable_ms
,
1998 .break_ctl
= pl011_break_ctl
,
1999 .startup
= pl011_startup
,
2000 .shutdown
= pl011_shutdown
,
2001 .flush_buffer
= pl011_dma_flush_buffer
,
2002 .set_termios
= pl011_set_termios
,
2004 .release_port
= pl011_release_port
,
2005 .request_port
= pl011_request_port
,
2006 .config_port
= pl011_config_port
,
2007 .verify_port
= pl011_verify_port
,
2008 #ifdef CONFIG_CONSOLE_POLL
2009 .poll_init
= pl011_hwinit
,
2010 .poll_get_char
= pl011_get_poll_char
,
2011 .poll_put_char
= pl011_put_poll_char
,
2015 static void sbsa_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
2019 static unsigned int sbsa_uart_get_mctrl(struct uart_port
*port
)
2024 static const struct uart_ops sbsa_uart_pops
= {
2025 .tx_empty
= pl011_tx_empty
,
2026 .set_mctrl
= sbsa_uart_set_mctrl
,
2027 .get_mctrl
= sbsa_uart_get_mctrl
,
2028 .stop_tx
= pl011_stop_tx
,
2029 .start_tx
= pl011_start_tx
,
2030 .stop_rx
= pl011_stop_rx
,
2031 .startup
= sbsa_uart_startup
,
2032 .shutdown
= sbsa_uart_shutdown
,
2033 .set_termios
= sbsa_uart_set_termios
,
2035 .release_port
= pl011_release_port
,
2036 .request_port
= pl011_request_port
,
2037 .config_port
= pl011_config_port
,
2038 .verify_port
= pl011_verify_port
,
2039 #ifdef CONFIG_CONSOLE_POLL
2040 .poll_init
= pl011_hwinit
,
2041 .poll_get_char
= pl011_get_poll_char
,
2042 .poll_put_char
= pl011_put_poll_char
,
2046 static struct uart_amba_port
*amba_ports
[UART_NR
];
2048 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2050 static void pl011_console_putchar(struct uart_port
*port
, int ch
)
2052 struct uart_amba_port
*uap
=
2053 container_of(port
, struct uart_amba_port
, port
);
2055 while (readw(uap
->port
.membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
2057 writew(ch
, uap
->port
.membase
+ UART01x_DR
);
2061 pl011_console_write(struct console
*co
, const char *s
, unsigned int count
)
2063 struct uart_amba_port
*uap
= amba_ports
[co
->index
];
2064 unsigned int status
, old_cr
= 0, new_cr
;
2065 unsigned long flags
;
2068 clk_enable(uap
->clk
);
2070 local_irq_save(flags
);
2071 if (uap
->port
.sysrq
)
2073 else if (oops_in_progress
)
2074 locked
= spin_trylock(&uap
->port
.lock
);
2076 spin_lock(&uap
->port
.lock
);
2079 * First save the CR then disable the interrupts
2081 if (!uap
->vendor
->always_enabled
) {
2082 old_cr
= readw(uap
->port
.membase
+ UART011_CR
);
2083 new_cr
= old_cr
& ~UART011_CR_CTSEN
;
2084 new_cr
|= UART01x_CR_UARTEN
| UART011_CR_TXE
;
2085 writew(new_cr
, uap
->port
.membase
+ UART011_CR
);
2088 uart_console_write(&uap
->port
, s
, count
, pl011_console_putchar
);
2091 * Finally, wait for transmitter to become empty
2092 * and restore the TCR
2095 status
= readw(uap
->port
.membase
+ UART01x_FR
);
2096 } while (status
& UART01x_FR_BUSY
);
2097 if (!uap
->vendor
->always_enabled
)
2098 writew(old_cr
, uap
->port
.membase
+ UART011_CR
);
2101 spin_unlock(&uap
->port
.lock
);
2102 local_irq_restore(flags
);
2104 clk_disable(uap
->clk
);
2108 pl011_console_get_options(struct uart_amba_port
*uap
, int *baud
,
2109 int *parity
, int *bits
)
2111 if (readw(uap
->port
.membase
+ UART011_CR
) & UART01x_CR_UARTEN
) {
2112 unsigned int lcr_h
, ibrd
, fbrd
;
2114 lcr_h
= readw(uap
->port
.membase
+ uap
->lcrh_tx
);
2117 if (lcr_h
& UART01x_LCRH_PEN
) {
2118 if (lcr_h
& UART01x_LCRH_EPS
)
2124 if ((lcr_h
& 0x60) == UART01x_LCRH_WLEN_7
)
2129 ibrd
= readw(uap
->port
.membase
+ UART011_IBRD
);
2130 fbrd
= readw(uap
->port
.membase
+ UART011_FBRD
);
2132 *baud
= uap
->port
.uartclk
* 4 / (64 * ibrd
+ fbrd
);
2134 if (uap
->vendor
->oversampling
) {
2135 if (readw(uap
->port
.membase
+ UART011_CR
)
2136 & ST_UART011_CR_OVSFACT
)
2142 static int __init
pl011_console_setup(struct console
*co
, char *options
)
2144 struct uart_amba_port
*uap
;
2152 * Check whether an invalid uart number has been specified, and
2153 * if so, search for the first available port that does have
2156 if (co
->index
>= UART_NR
)
2158 uap
= amba_ports
[co
->index
];
2162 /* Allow pins to be muxed in and configured */
2163 pinctrl_pm_select_default_state(uap
->port
.dev
);
2165 ret
= clk_prepare(uap
->clk
);
2169 if (dev_get_platdata(uap
->port
.dev
)) {
2170 struct amba_pl011_data
*plat
;
2172 plat
= dev_get_platdata(uap
->port
.dev
);
2177 uap
->port
.uartclk
= clk_get_rate(uap
->clk
);
2179 if (uap
->vendor
->fixed_options
) {
2180 baud
= uap
->fixed_baud
;
2183 uart_parse_options(options
,
2184 &baud
, &parity
, &bits
, &flow
);
2186 pl011_console_get_options(uap
, &baud
, &parity
, &bits
);
2189 return uart_set_options(&uap
->port
, co
, baud
, parity
, bits
, flow
);
2192 static struct uart_driver amba_reg
;
2193 static struct console amba_console
= {
2195 .write
= pl011_console_write
,
2196 .device
= uart_console_device
,
2197 .setup
= pl011_console_setup
,
2198 .flags
= CON_PRINTBUFFER
,
2203 #define AMBA_CONSOLE (&amba_console)
2205 static void pl011_putc(struct uart_port
*port
, int c
)
2207 while (readl(port
->membase
+ UART01x_FR
) & UART01x_FR_TXFF
)
2209 writeb(c
, port
->membase
+ UART01x_DR
);
2210 while (readl(port
->membase
+ UART01x_FR
) & UART01x_FR_BUSY
)
2214 static void pl011_early_write(struct console
*con
, const char *s
, unsigned n
)
2216 struct earlycon_device
*dev
= con
->data
;
2218 uart_console_write(&dev
->port
, s
, n
, pl011_putc
);
2221 static int __init
pl011_early_console_setup(struct earlycon_device
*device
,
2224 if (!device
->port
.membase
)
2227 device
->con
->write
= pl011_early_write
;
2230 EARLYCON_DECLARE(pl011
, pl011_early_console_setup
);
2231 OF_EARLYCON_DECLARE(pl011
, "arm,pl011", pl011_early_console_setup
);
2234 #define AMBA_CONSOLE NULL
2237 static struct uart_driver amba_reg
= {
2238 .owner
= THIS_MODULE
,
2239 .driver_name
= "ttyAMA",
2240 .dev_name
= "ttyAMA",
2241 .major
= SERIAL_AMBA_MAJOR
,
2242 .minor
= SERIAL_AMBA_MINOR
,
2244 .cons
= AMBA_CONSOLE
,
2247 static int pl011_probe_dt_alias(int index
, struct device
*dev
)
2249 struct device_node
*np
;
2250 static bool seen_dev_with_alias
= false;
2251 static bool seen_dev_without_alias
= false;
2254 if (!IS_ENABLED(CONFIG_OF
))
2261 ret
= of_alias_get_id(np
, "serial");
2262 if (IS_ERR_VALUE(ret
)) {
2263 seen_dev_without_alias
= true;
2266 seen_dev_with_alias
= true;
2267 if (ret
>= ARRAY_SIZE(amba_ports
) || amba_ports
[ret
] != NULL
) {
2268 dev_warn(dev
, "requested serial port %d not available.\n", ret
);
2273 if (seen_dev_with_alias
&& seen_dev_without_alias
)
2274 dev_warn(dev
, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2279 /* unregisters the driver also if no more ports are left */
2280 static void pl011_unregister_port(struct uart_amba_port
*uap
)
2285 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++) {
2286 if (amba_ports
[i
] == uap
)
2287 amba_ports
[i
] = NULL
;
2288 else if (amba_ports
[i
])
2291 pl011_dma_remove(uap
);
2293 uart_unregister_driver(&amba_reg
);
2296 static int pl011_find_free_port(void)
2300 for (i
= 0; i
< ARRAY_SIZE(amba_ports
); i
++)
2301 if (amba_ports
[i
] == NULL
)
2307 static int pl011_setup_port(struct device
*dev
, struct uart_amba_port
*uap
,
2308 struct resource
*mmiobase
, int index
)
2312 base
= devm_ioremap_resource(dev
, mmiobase
);
2314 return PTR_ERR(base
);
2316 index
= pl011_probe_dt_alias(index
, dev
);
2319 uap
->port
.dev
= dev
;
2320 uap
->port
.mapbase
= mmiobase
->start
;
2321 uap
->port
.membase
= base
;
2322 uap
->port
.iotype
= UPIO_MEM
;
2323 uap
->port
.fifosize
= uap
->fifosize
;
2324 uap
->port
.flags
= UPF_BOOT_AUTOCONF
;
2325 uap
->port
.line
= index
;
2327 amba_ports
[index
] = uap
;
2332 static int pl011_register_port(struct uart_amba_port
*uap
)
2336 /* Ensure interrupts from this UART are masked and cleared */
2337 writew(0, uap
->port
.membase
+ UART011_IMSC
);
2338 writew(0xffff, uap
->port
.membase
+ UART011_ICR
);
2340 if (!amba_reg
.state
) {
2341 ret
= uart_register_driver(&amba_reg
);
2343 dev_err(uap
->port
.dev
,
2344 "Failed to register AMBA-PL011 driver\n");
2349 ret
= uart_add_one_port(&amba_reg
, &uap
->port
);
2351 pl011_unregister_port(uap
);
2356 static int pl011_probe(struct amba_device
*dev
, const struct amba_id
*id
)
2358 struct uart_amba_port
*uap
;
2359 struct vendor_data
*vendor
= id
->data
;
2362 portnr
= pl011_find_free_port();
2366 uap
= devm_kzalloc(&dev
->dev
, sizeof(struct uart_amba_port
),
2371 uap
->clk
= devm_clk_get(&dev
->dev
, NULL
);
2372 if (IS_ERR(uap
->clk
))
2373 return PTR_ERR(uap
->clk
);
2375 uap
->vendor
= vendor
;
2376 uap
->lcrh_rx
= vendor
->lcrh_rx
;
2377 uap
->lcrh_tx
= vendor
->lcrh_tx
;
2378 uap
->fifosize
= vendor
->get_fifosize(dev
);
2379 uap
->port
.irq
= dev
->irq
[0];
2380 uap
->port
.ops
= &amba_pl011_pops
;
2382 snprintf(uap
->type
, sizeof(uap
->type
), "PL011 rev%u", amba_rev(dev
));
2384 ret
= pl011_setup_port(&dev
->dev
, uap
, &dev
->res
, portnr
);
2388 amba_set_drvdata(dev
, uap
);
2390 return pl011_register_port(uap
);
2393 static int pl011_remove(struct amba_device
*dev
)
2395 struct uart_amba_port
*uap
= amba_get_drvdata(dev
);
2397 uart_remove_one_port(&amba_reg
, &uap
->port
);
2398 pl011_unregister_port(uap
);
2402 #ifdef CONFIG_PM_SLEEP
2403 static int pl011_suspend(struct device
*dev
)
2405 struct uart_amba_port
*uap
= dev_get_drvdata(dev
);
2410 return uart_suspend_port(&amba_reg
, &uap
->port
);
2413 static int pl011_resume(struct device
*dev
)
2415 struct uart_amba_port
*uap
= dev_get_drvdata(dev
);
2420 return uart_resume_port(&amba_reg
, &uap
->port
);
2424 static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops
, pl011_suspend
, pl011_resume
);
2426 static int sbsa_uart_probe(struct platform_device
*pdev
)
2428 struct uart_amba_port
*uap
;
2434 * Check the mandatory baud rate parameter in the DT node early
2435 * so that we can easily exit with the error.
2437 if (pdev
->dev
.of_node
) {
2438 struct device_node
*np
= pdev
->dev
.of_node
;
2440 ret
= of_property_read_u32(np
, "current-speed", &baudrate
);
2447 portnr
= pl011_find_free_port();
2451 uap
= devm_kzalloc(&pdev
->dev
, sizeof(struct uart_amba_port
),
2456 uap
->vendor
= &vendor_sbsa
;
2458 uap
->port
.irq
= platform_get_irq(pdev
, 0);
2459 uap
->port
.ops
= &sbsa_uart_pops
;
2460 uap
->fixed_baud
= baudrate
;
2462 snprintf(uap
->type
, sizeof(uap
->type
), "SBSA");
2464 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2466 ret
= pl011_setup_port(&pdev
->dev
, uap
, r
, portnr
);
2470 platform_set_drvdata(pdev
, uap
);
2472 return pl011_register_port(uap
);
2475 static int sbsa_uart_remove(struct platform_device
*pdev
)
2477 struct uart_amba_port
*uap
= platform_get_drvdata(pdev
);
2479 uart_remove_one_port(&amba_reg
, &uap
->port
);
2480 pl011_unregister_port(uap
);
2484 static const struct of_device_id sbsa_uart_of_match
[] = {
2485 { .compatible
= "arm,sbsa-uart", },
2488 MODULE_DEVICE_TABLE(of
, sbsa_uart_of_match
);
2490 static const struct acpi_device_id sbsa_uart_acpi_match
[] = {
2494 MODULE_DEVICE_TABLE(acpi
, sbsa_uart_acpi_match
);
2496 static struct platform_driver arm_sbsa_uart_platform_driver
= {
2497 .probe
= sbsa_uart_probe
,
2498 .remove
= sbsa_uart_remove
,
2500 .name
= "sbsa-uart",
2501 .of_match_table
= of_match_ptr(sbsa_uart_of_match
),
2502 .acpi_match_table
= ACPI_PTR(sbsa_uart_acpi_match
),
2506 static struct amba_id pl011_ids
[] = {
2510 .data
= &vendor_arm
,
2520 MODULE_DEVICE_TABLE(amba
, pl011_ids
);
2522 static struct amba_driver pl011_driver
= {
2524 .name
= "uart-pl011",
2525 .pm
= &pl011_dev_pm_ops
,
2527 .id_table
= pl011_ids
,
2528 .probe
= pl011_probe
,
2529 .remove
= pl011_remove
,
2532 static int __init
pl011_init(void)
2534 printk(KERN_INFO
"Serial: AMBA PL011 UART driver\n");
2536 if (platform_driver_register(&arm_sbsa_uart_platform_driver
))
2537 pr_warn("could not register SBSA UART platform driver\n");
2538 return amba_driver_register(&pl011_driver
);
2541 static void __exit
pl011_exit(void)
2543 platform_driver_unregister(&arm_sbsa_uart_platform_driver
);
2544 amba_driver_unregister(&pl011_driver
);
2548 * While this can be a module, if builtin it's most likely the console
2549 * So let's leave module_exit but move module_init to an earlier place
2551 arch_initcall(pl011_init
);
2552 module_exit(pl011_exit
);
2554 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2555 MODULE_DESCRIPTION("ARM AMBA serial port driver");
2556 MODULE_LICENSE("GPL");