2 * Freescale STMP37XX/STMP378X Application UART driver
4 * Author: dmitry pervushin <dimka@embeddedalley.com>
6 * Copyright 2008-2010 Freescale Semiconductor, Inc.
7 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 * The code contained herein is licensed under the GNU General Public
10 * License. You may obtain a copy of the GNU General Public License
11 * Version 2 or later at the following locations:
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/console.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
26 #include <linux/tty_driver.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial.h>
29 #include <linux/serial_core.h>
30 #include <linux/platform_device.h>
31 #include <linux/device.h>
32 #include <linux/clk.h>
33 #include <linux/delay.h>
35 #include <linux/of_device.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/dmaengine.h>
39 #include <asm/cacheflush.h>
41 #define MXS_AUART_PORTS 5
43 #define AUART_CTRL0 0x00000000
44 #define AUART_CTRL0_SET 0x00000004
45 #define AUART_CTRL0_CLR 0x00000008
46 #define AUART_CTRL0_TOG 0x0000000c
47 #define AUART_CTRL1 0x00000010
48 #define AUART_CTRL1_SET 0x00000014
49 #define AUART_CTRL1_CLR 0x00000018
50 #define AUART_CTRL1_TOG 0x0000001c
51 #define AUART_CTRL2 0x00000020
52 #define AUART_CTRL2_SET 0x00000024
53 #define AUART_CTRL2_CLR 0x00000028
54 #define AUART_CTRL2_TOG 0x0000002c
55 #define AUART_LINECTRL 0x00000030
56 #define AUART_LINECTRL_SET 0x00000034
57 #define AUART_LINECTRL_CLR 0x00000038
58 #define AUART_LINECTRL_TOG 0x0000003c
59 #define AUART_LINECTRL2 0x00000040
60 #define AUART_LINECTRL2_SET 0x00000044
61 #define AUART_LINECTRL2_CLR 0x00000048
62 #define AUART_LINECTRL2_TOG 0x0000004c
63 #define AUART_INTR 0x00000050
64 #define AUART_INTR_SET 0x00000054
65 #define AUART_INTR_CLR 0x00000058
66 #define AUART_INTR_TOG 0x0000005c
67 #define AUART_DATA 0x00000060
68 #define AUART_STAT 0x00000070
69 #define AUART_DEBUG 0x00000080
70 #define AUART_VERSION 0x00000090
71 #define AUART_AUTOBAUD 0x000000a0
73 #define AUART_CTRL0_SFTRST (1 << 31)
74 #define AUART_CTRL0_CLKGATE (1 << 30)
75 #define AUART_CTRL0_RXTO_ENABLE (1 << 27)
76 #define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
77 #define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
79 #define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
81 #define AUART_CTRL2_DMAONERR (1 << 26)
82 #define AUART_CTRL2_TXDMAE (1 << 25)
83 #define AUART_CTRL2_RXDMAE (1 << 24)
85 #define AUART_CTRL2_CTSEN (1 << 15)
86 #define AUART_CTRL2_RTSEN (1 << 14)
87 #define AUART_CTRL2_RTS (1 << 11)
88 #define AUART_CTRL2_RXE (1 << 9)
89 #define AUART_CTRL2_TXE (1 << 8)
90 #define AUART_CTRL2_UARTEN (1 << 0)
92 #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
93 #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
94 #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
95 #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
96 #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
97 #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
98 #define AUART_LINECTRL_WLEN_MASK 0x00000060
99 #define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
100 #define AUART_LINECTRL_FEN (1 << 4)
101 #define AUART_LINECTRL_STP2 (1 << 3)
102 #define AUART_LINECTRL_EPS (1 << 2)
103 #define AUART_LINECTRL_PEN (1 << 1)
104 #define AUART_LINECTRL_BRK (1 << 0)
106 #define AUART_INTR_RTIEN (1 << 22)
107 #define AUART_INTR_TXIEN (1 << 21)
108 #define AUART_INTR_RXIEN (1 << 20)
109 #define AUART_INTR_CTSMIEN (1 << 17)
110 #define AUART_INTR_RTIS (1 << 6)
111 #define AUART_INTR_TXIS (1 << 5)
112 #define AUART_INTR_RXIS (1 << 4)
113 #define AUART_INTR_CTSMIS (1 << 1)
115 #define AUART_STAT_BUSY (1 << 29)
116 #define AUART_STAT_CTS (1 << 28)
117 #define AUART_STAT_TXFE (1 << 27)
118 #define AUART_STAT_TXFF (1 << 25)
119 #define AUART_STAT_RXFE (1 << 24)
120 #define AUART_STAT_OERR (1 << 19)
121 #define AUART_STAT_BERR (1 << 18)
122 #define AUART_STAT_PERR (1 << 17)
123 #define AUART_STAT_FERR (1 << 16)
124 #define AUART_STAT_RXCOUNT_MASK 0xffff
126 static struct uart_driver auart_driver
;
128 enum mxs_auart_type
{
133 struct mxs_auart_port
{
134 struct uart_port port
;
136 #define MXS_AUART_DMA_ENABLED 0x2
137 #define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
138 #define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
139 #define MXS_AUART_RTSCTS 4 /* bit 4 */
142 enum mxs_auart_type devtype
;
150 struct scatterlist tx_sgl
;
151 struct dma_chan
*tx_dma_chan
;
154 struct scatterlist rx_sgl
;
155 struct dma_chan
*rx_dma_chan
;
159 static struct platform_device_id mxs_auart_devtype
[] = {
160 { .name
= "mxs-auart-imx23", .driver_data
= IMX23_AUART
},
161 { .name
= "mxs-auart-imx28", .driver_data
= IMX28_AUART
},
164 MODULE_DEVICE_TABLE(platform
, mxs_auart_devtype
);
166 static struct of_device_id mxs_auart_dt_ids
[] = {
168 .compatible
= "fsl,imx28-auart",
169 .data
= &mxs_auart_devtype
[IMX28_AUART
]
171 .compatible
= "fsl,imx23-auart",
172 .data
= &mxs_auart_devtype
[IMX23_AUART
]
173 }, { /* sentinel */ }
175 MODULE_DEVICE_TABLE(of
, mxs_auart_dt_ids
);
177 static inline int is_imx28_auart(struct mxs_auart_port
*s
)
179 return s
->devtype
== IMX28_AUART
;
182 static inline bool auart_dma_enabled(struct mxs_auart_port
*s
)
184 return s
->flags
& MXS_AUART_DMA_ENABLED
;
187 static void mxs_auart_stop_tx(struct uart_port
*u
);
189 #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
191 static void mxs_auart_tx_chars(struct mxs_auart_port
*s
);
193 static void dma_tx_callback(void *param
)
195 struct mxs_auart_port
*s
= param
;
196 struct circ_buf
*xmit
= &s
->port
.state
->xmit
;
198 dma_unmap_sg(s
->dev
, &s
->tx_sgl
, 1, DMA_TO_DEVICE
);
200 /* clear the bit used to serialize the DMA tx. */
201 clear_bit(MXS_AUART_DMA_TX_SYNC
, &s
->flags
);
202 smp_mb__after_clear_bit();
204 /* wake up the possible processes. */
205 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
206 uart_write_wakeup(&s
->port
);
208 mxs_auart_tx_chars(s
);
211 static int mxs_auart_dma_tx(struct mxs_auart_port
*s
, int size
)
213 struct dma_async_tx_descriptor
*desc
;
214 struct scatterlist
*sgl
= &s
->tx_sgl
;
215 struct dma_chan
*channel
= s
->tx_dma_chan
;
218 /* [1] : send PIO. Note, the first pio word is CTRL1. */
219 pio
= AUART_CTRL1_XFER_COUNT(size
);
220 desc
= dmaengine_prep_slave_sg(channel
, (struct scatterlist
*)&pio
,
221 1, DMA_TRANS_NONE
, 0);
223 dev_err(s
->dev
, "step 1 error\n");
227 /* [2] : set DMA buffer. */
228 sg_init_one(sgl
, s
->tx_dma_buf
, size
);
229 dma_map_sg(s
->dev
, sgl
, 1, DMA_TO_DEVICE
);
230 desc
= dmaengine_prep_slave_sg(channel
, sgl
,
231 1, DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
233 dev_err(s
->dev
, "step 2 error\n");
237 /* [3] : submit the DMA */
238 desc
->callback
= dma_tx_callback
;
239 desc
->callback_param
= s
;
240 dmaengine_submit(desc
);
241 dma_async_issue_pending(channel
);
245 static void mxs_auart_tx_chars(struct mxs_auart_port
*s
)
247 struct circ_buf
*xmit
= &s
->port
.state
->xmit
;
249 if (auart_dma_enabled(s
)) {
252 void *buffer
= s
->tx_dma_buf
;
254 if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC
, &s
->flags
))
257 while (!uart_circ_empty(xmit
) && !uart_tx_stopped(&s
->port
)) {
258 size
= min_t(u32
, UART_XMIT_SIZE
- i
,
259 CIRC_CNT_TO_END(xmit
->head
,
262 memcpy(buffer
+ i
, xmit
->buf
+ xmit
->tail
, size
);
263 xmit
->tail
= (xmit
->tail
+ size
) & (UART_XMIT_SIZE
- 1);
266 if (i
>= UART_XMIT_SIZE
)
270 if (uart_tx_stopped(&s
->port
))
271 mxs_auart_stop_tx(&s
->port
);
274 mxs_auart_dma_tx(s
, i
);
276 clear_bit(MXS_AUART_DMA_TX_SYNC
, &s
->flags
);
277 smp_mb__after_clear_bit();
283 while (!(readl(s
->port
.membase
+ AUART_STAT
) &
285 if (s
->port
.x_char
) {
287 writel(s
->port
.x_char
,
288 s
->port
.membase
+ AUART_DATA
);
292 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(&s
->port
)) {
294 writel(xmit
->buf
[xmit
->tail
],
295 s
->port
.membase
+ AUART_DATA
);
296 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
300 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
301 uart_write_wakeup(&s
->port
);
303 if (uart_circ_empty(&(s
->port
.state
->xmit
)))
304 writel(AUART_INTR_TXIEN
,
305 s
->port
.membase
+ AUART_INTR_CLR
);
307 writel(AUART_INTR_TXIEN
,
308 s
->port
.membase
+ AUART_INTR_SET
);
310 if (uart_tx_stopped(&s
->port
))
311 mxs_auart_stop_tx(&s
->port
);
314 static void mxs_auart_rx_char(struct mxs_auart_port
*s
)
320 c
= readl(s
->port
.membase
+ AUART_DATA
);
321 stat
= readl(s
->port
.membase
+ AUART_STAT
);
326 if (stat
& AUART_STAT_BERR
) {
327 s
->port
.icount
.brk
++;
328 if (uart_handle_break(&s
->port
))
330 } else if (stat
& AUART_STAT_PERR
) {
331 s
->port
.icount
.parity
++;
332 } else if (stat
& AUART_STAT_FERR
) {
333 s
->port
.icount
.frame
++;
337 * Mask off conditions which should be ingored.
339 stat
&= s
->port
.read_status_mask
;
341 if (stat
& AUART_STAT_BERR
) {
343 } else if (stat
& AUART_STAT_PERR
)
345 else if (stat
& AUART_STAT_FERR
)
348 if (stat
& AUART_STAT_OERR
)
349 s
->port
.icount
.overrun
++;
351 if (uart_handle_sysrq_char(&s
->port
, c
))
354 uart_insert_char(&s
->port
, stat
, AUART_STAT_OERR
, c
, flag
);
356 writel(stat
, s
->port
.membase
+ AUART_STAT
);
359 static void mxs_auart_rx_chars(struct mxs_auart_port
*s
)
364 stat
= readl(s
->port
.membase
+ AUART_STAT
);
365 if (stat
& AUART_STAT_RXFE
)
367 mxs_auart_rx_char(s
);
370 writel(stat
, s
->port
.membase
+ AUART_STAT
);
371 tty_flip_buffer_push(&s
->port
.state
->port
);
374 static int mxs_auart_request_port(struct uart_port
*u
)
379 static int mxs_auart_verify_port(struct uart_port
*u
,
380 struct serial_struct
*ser
)
382 if (u
->type
!= PORT_UNKNOWN
&& u
->type
!= PORT_IMX
)
387 static void mxs_auart_config_port(struct uart_port
*u
, int flags
)
391 static const char *mxs_auart_type(struct uart_port
*u
)
393 struct mxs_auart_port
*s
= to_auart_port(u
);
395 return dev_name(s
->dev
);
398 static void mxs_auart_release_port(struct uart_port
*u
)
402 static void mxs_auart_set_mctrl(struct uart_port
*u
, unsigned mctrl
)
404 struct mxs_auart_port
*s
= to_auart_port(u
);
406 u32 ctrl
= readl(u
->membase
+ AUART_CTRL2
);
408 ctrl
&= ~(AUART_CTRL2_RTSEN
| AUART_CTRL2_RTS
);
409 if (mctrl
& TIOCM_RTS
) {
410 if (tty_port_cts_enabled(&u
->state
->port
))
411 ctrl
|= AUART_CTRL2_RTSEN
;
413 ctrl
|= AUART_CTRL2_RTS
;
417 writel(ctrl
, u
->membase
+ AUART_CTRL2
);
420 static u32
mxs_auart_get_mctrl(struct uart_port
*u
)
422 struct mxs_auart_port
*s
= to_auart_port(u
);
423 u32 stat
= readl(u
->membase
+ AUART_STAT
);
424 int ctrl2
= readl(u
->membase
+ AUART_CTRL2
);
428 if (stat
& AUART_STAT_CTS
)
431 if (ctrl2
& AUART_CTRL2_RTS
)
437 static int mxs_auart_dma_prep_rx(struct mxs_auart_port
*s
);
438 static void dma_rx_callback(void *arg
)
440 struct mxs_auart_port
*s
= (struct mxs_auart_port
*) arg
;
441 struct tty_port
*port
= &s
->port
.state
->port
;
445 dma_unmap_sg(s
->dev
, &s
->rx_sgl
, 1, DMA_FROM_DEVICE
);
447 stat
= readl(s
->port
.membase
+ AUART_STAT
);
448 stat
&= ~(AUART_STAT_OERR
| AUART_STAT_BERR
|
449 AUART_STAT_PERR
| AUART_STAT_FERR
);
451 count
= stat
& AUART_STAT_RXCOUNT_MASK
;
452 tty_insert_flip_string(port
, s
->rx_dma_buf
, count
);
454 writel(stat
, s
->port
.membase
+ AUART_STAT
);
455 tty_flip_buffer_push(port
);
457 /* start the next DMA for RX. */
458 mxs_auart_dma_prep_rx(s
);
461 static int mxs_auart_dma_prep_rx(struct mxs_auart_port
*s
)
463 struct dma_async_tx_descriptor
*desc
;
464 struct scatterlist
*sgl
= &s
->rx_sgl
;
465 struct dma_chan
*channel
= s
->rx_dma_chan
;
469 pio
[0] = AUART_CTRL0_RXTO_ENABLE
470 | AUART_CTRL0_RXTIMEOUT(0x80)
471 | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE
);
472 desc
= dmaengine_prep_slave_sg(channel
, (struct scatterlist
*)pio
,
473 1, DMA_TRANS_NONE
, 0);
475 dev_err(s
->dev
, "step 1 error\n");
479 /* [2] : send DMA request */
480 sg_init_one(sgl
, s
->rx_dma_buf
, UART_XMIT_SIZE
);
481 dma_map_sg(s
->dev
, sgl
, 1, DMA_FROM_DEVICE
);
482 desc
= dmaengine_prep_slave_sg(channel
, sgl
, 1, DMA_DEV_TO_MEM
,
483 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
485 dev_err(s
->dev
, "step 2 error\n");
489 /* [3] : submit the DMA, but do not issue it. */
490 desc
->callback
= dma_rx_callback
;
491 desc
->callback_param
= s
;
492 dmaengine_submit(desc
);
493 dma_async_issue_pending(channel
);
497 static void mxs_auart_dma_exit_channel(struct mxs_auart_port
*s
)
499 if (s
->tx_dma_chan
) {
500 dma_release_channel(s
->tx_dma_chan
);
501 s
->tx_dma_chan
= NULL
;
503 if (s
->rx_dma_chan
) {
504 dma_release_channel(s
->rx_dma_chan
);
505 s
->rx_dma_chan
= NULL
;
508 kfree(s
->tx_dma_buf
);
509 kfree(s
->rx_dma_buf
);
510 s
->tx_dma_buf
= NULL
;
511 s
->rx_dma_buf
= NULL
;
514 static void mxs_auart_dma_exit(struct mxs_auart_port
*s
)
517 writel(AUART_CTRL2_TXDMAE
| AUART_CTRL2_RXDMAE
| AUART_CTRL2_DMAONERR
,
518 s
->port
.membase
+ AUART_CTRL2_CLR
);
520 mxs_auart_dma_exit_channel(s
);
521 s
->flags
&= ~MXS_AUART_DMA_ENABLED
;
522 clear_bit(MXS_AUART_DMA_TX_SYNC
, &s
->flags
);
523 clear_bit(MXS_AUART_DMA_RX_READY
, &s
->flags
);
526 static int mxs_auart_dma_init(struct mxs_auart_port
*s
)
528 if (auart_dma_enabled(s
))
532 s
->rx_dma_chan
= dma_request_slave_channel(s
->dev
, "rx");
535 s
->rx_dma_buf
= kzalloc(UART_XMIT_SIZE
, GFP_KERNEL
| GFP_DMA
);
540 s
->tx_dma_chan
= dma_request_slave_channel(s
->dev
, "tx");
543 s
->tx_dma_buf
= kzalloc(UART_XMIT_SIZE
, GFP_KERNEL
| GFP_DMA
);
548 s
->flags
|= MXS_AUART_DMA_ENABLED
;
549 dev_dbg(s
->dev
, "enabled the DMA support.");
554 mxs_auart_dma_exit_channel(s
);
559 static void mxs_auart_settermios(struct uart_port
*u
,
560 struct ktermios
*termios
,
561 struct ktermios
*old
)
563 struct mxs_auart_port
*s
= to_auart_port(u
);
564 u32 bm
, ctrl
, ctrl2
, div
;
565 unsigned int cflag
, baud
;
567 cflag
= termios
->c_cflag
;
569 ctrl
= AUART_LINECTRL_FEN
;
570 ctrl2
= readl(u
->membase
+ AUART_CTRL2
);
573 switch (cflag
& CSIZE
) {
590 ctrl
|= AUART_LINECTRL_WLEN(bm
);
593 if (cflag
& PARENB
) {
594 ctrl
|= AUART_LINECTRL_PEN
;
595 if ((cflag
& PARODD
) == 0)
596 ctrl
|= AUART_LINECTRL_EPS
;
599 u
->read_status_mask
= 0;
601 if (termios
->c_iflag
& INPCK
)
602 u
->read_status_mask
|= AUART_STAT_PERR
;
603 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
604 u
->read_status_mask
|= AUART_STAT_BERR
;
607 * Characters to ignore
609 u
->ignore_status_mask
= 0;
610 if (termios
->c_iflag
& IGNPAR
)
611 u
->ignore_status_mask
|= AUART_STAT_PERR
;
612 if (termios
->c_iflag
& IGNBRK
) {
613 u
->ignore_status_mask
|= AUART_STAT_BERR
;
615 * If we're ignoring parity and break indicators,
616 * ignore overruns too (for real raw support).
618 if (termios
->c_iflag
& IGNPAR
)
619 u
->ignore_status_mask
|= AUART_STAT_OERR
;
623 * ignore all characters if CREAD is not set
626 ctrl2
|= AUART_CTRL2_RXE
;
628 ctrl2
&= ~AUART_CTRL2_RXE
;
630 /* figure out the stop bits requested */
632 ctrl
|= AUART_LINECTRL_STP2
;
634 /* figure out the hardware flow control settings */
635 if (cflag
& CRTSCTS
) {
637 * The DMA has a bug(see errata:2836) in mx23.
638 * So we can not implement the DMA for auart in mx23,
639 * we can only implement the DMA support for auart
642 if (is_imx28_auart(s
)
643 && test_bit(MXS_AUART_RTSCTS
, &s
->flags
)) {
644 if (!mxs_auart_dma_init(s
))
645 /* enable DMA tranfer */
646 ctrl2
|= AUART_CTRL2_TXDMAE
| AUART_CTRL2_RXDMAE
647 | AUART_CTRL2_DMAONERR
;
649 ctrl2
|= AUART_CTRL2_CTSEN
| AUART_CTRL2_RTSEN
;
651 ctrl2
&= ~(AUART_CTRL2_CTSEN
| AUART_CTRL2_RTSEN
);
655 baud
= uart_get_baud_rate(u
, termios
, old
, 0, u
->uartclk
);
656 div
= u
->uartclk
* 32 / baud
;
657 ctrl
|= AUART_LINECTRL_BAUD_DIVFRAC(div
& 0x3F);
658 ctrl
|= AUART_LINECTRL_BAUD_DIVINT(div
>> 6);
660 writel(ctrl
, u
->membase
+ AUART_LINECTRL
);
661 writel(ctrl2
, u
->membase
+ AUART_CTRL2
);
663 uart_update_timeout(u
, termios
->c_cflag
, baud
);
665 /* prepare for the DMA RX. */
666 if (auart_dma_enabled(s
) &&
667 !test_and_set_bit(MXS_AUART_DMA_RX_READY
, &s
->flags
)) {
668 if (!mxs_auart_dma_prep_rx(s
)) {
669 /* Disable the normal RX interrupt. */
670 writel(AUART_INTR_RXIEN
| AUART_INTR_RTIEN
,
671 u
->membase
+ AUART_INTR_CLR
);
673 mxs_auart_dma_exit(s
);
674 dev_err(s
->dev
, "We can not start up the DMA.\n");
679 static irqreturn_t
mxs_auart_irq_handle(int irq
, void *context
)
682 struct mxs_auart_port
*s
= context
;
683 u32 stat
= readl(s
->port
.membase
+ AUART_STAT
);
685 istat
= readl(s
->port
.membase
+ AUART_INTR
);
688 writel(istat
& (AUART_INTR_RTIS
691 | AUART_INTR_CTSMIS
),
692 s
->port
.membase
+ AUART_INTR_CLR
);
694 if (istat
& AUART_INTR_CTSMIS
) {
695 uart_handle_cts_change(&s
->port
, stat
& AUART_STAT_CTS
);
696 writel(AUART_INTR_CTSMIS
,
697 s
->port
.membase
+ AUART_INTR_CLR
);
698 istat
&= ~AUART_INTR_CTSMIS
;
701 if (istat
& (AUART_INTR_RTIS
| AUART_INTR_RXIS
)) {
702 if (!auart_dma_enabled(s
))
703 mxs_auart_rx_chars(s
);
704 istat
&= ~(AUART_INTR_RTIS
| AUART_INTR_RXIS
);
707 if (istat
& AUART_INTR_TXIS
) {
708 mxs_auart_tx_chars(s
);
709 istat
&= ~AUART_INTR_TXIS
;
715 static void mxs_auart_reset(struct uart_port
*u
)
720 writel(AUART_CTRL0_SFTRST
, u
->membase
+ AUART_CTRL0_CLR
);
722 for (i
= 0; i
< 10000; i
++) {
723 reg
= readl(u
->membase
+ AUART_CTRL0
);
724 if (!(reg
& AUART_CTRL0_SFTRST
))
728 writel(AUART_CTRL0_CLKGATE
, u
->membase
+ AUART_CTRL0_CLR
);
731 static int mxs_auart_startup(struct uart_port
*u
)
733 struct mxs_auart_port
*s
= to_auart_port(u
);
735 clk_prepare_enable(s
->clk
);
737 writel(AUART_CTRL0_CLKGATE
, u
->membase
+ AUART_CTRL0_CLR
);
739 writel(AUART_CTRL2_UARTEN
, u
->membase
+ AUART_CTRL2_SET
);
741 writel(AUART_INTR_RXIEN
| AUART_INTR_RTIEN
| AUART_INTR_CTSMIEN
,
742 u
->membase
+ AUART_INTR
);
745 * Enable fifo so all four bytes of a DMA word are written to
746 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
748 writel(AUART_LINECTRL_FEN
, u
->membase
+ AUART_LINECTRL_SET
);
753 static void mxs_auart_shutdown(struct uart_port
*u
)
755 struct mxs_auart_port
*s
= to_auart_port(u
);
757 if (auart_dma_enabled(s
))
758 mxs_auart_dma_exit(s
);
760 writel(AUART_CTRL2_UARTEN
, u
->membase
+ AUART_CTRL2_CLR
);
762 writel(AUART_INTR_RXIEN
| AUART_INTR_RTIEN
| AUART_INTR_CTSMIEN
,
763 u
->membase
+ AUART_INTR_CLR
);
765 writel(AUART_CTRL0_CLKGATE
, u
->membase
+ AUART_CTRL0_SET
);
767 clk_disable_unprepare(s
->clk
);
770 static unsigned int mxs_auart_tx_empty(struct uart_port
*u
)
772 if (readl(u
->membase
+ AUART_STAT
) & AUART_STAT_TXFE
)
778 static void mxs_auart_start_tx(struct uart_port
*u
)
780 struct mxs_auart_port
*s
= to_auart_port(u
);
782 /* enable transmitter */
783 writel(AUART_CTRL2_TXE
, u
->membase
+ AUART_CTRL2_SET
);
785 mxs_auart_tx_chars(s
);
788 static void mxs_auart_stop_tx(struct uart_port
*u
)
790 writel(AUART_CTRL2_TXE
, u
->membase
+ AUART_CTRL2_CLR
);
793 static void mxs_auart_stop_rx(struct uart_port
*u
)
795 writel(AUART_CTRL2_RXE
, u
->membase
+ AUART_CTRL2_CLR
);
798 static void mxs_auart_break_ctl(struct uart_port
*u
, int ctl
)
801 writel(AUART_LINECTRL_BRK
,
802 u
->membase
+ AUART_LINECTRL_SET
);
804 writel(AUART_LINECTRL_BRK
,
805 u
->membase
+ AUART_LINECTRL_CLR
);
808 static void mxs_auart_enable_ms(struct uart_port
*port
)
813 static struct uart_ops mxs_auart_ops
= {
814 .tx_empty
= mxs_auart_tx_empty
,
815 .start_tx
= mxs_auart_start_tx
,
816 .stop_tx
= mxs_auart_stop_tx
,
817 .stop_rx
= mxs_auart_stop_rx
,
818 .enable_ms
= mxs_auart_enable_ms
,
819 .break_ctl
= mxs_auart_break_ctl
,
820 .set_mctrl
= mxs_auart_set_mctrl
,
821 .get_mctrl
= mxs_auart_get_mctrl
,
822 .startup
= mxs_auart_startup
,
823 .shutdown
= mxs_auart_shutdown
,
824 .set_termios
= mxs_auart_settermios
,
825 .type
= mxs_auart_type
,
826 .release_port
= mxs_auart_release_port
,
827 .request_port
= mxs_auart_request_port
,
828 .config_port
= mxs_auart_config_port
,
829 .verify_port
= mxs_auart_verify_port
,
832 static struct mxs_auart_port
*auart_port
[MXS_AUART_PORTS
];
834 #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
835 static void mxs_auart_console_putchar(struct uart_port
*port
, int ch
)
837 unsigned int to
= 1000;
839 while (readl(port
->membase
+ AUART_STAT
) & AUART_STAT_TXFF
) {
845 writel(ch
, port
->membase
+ AUART_DATA
);
849 auart_console_write(struct console
*co
, const char *str
, unsigned int count
)
851 struct mxs_auart_port
*s
;
852 struct uart_port
*port
;
853 unsigned int old_ctrl0
, old_ctrl2
;
854 unsigned int to
= 20000;
856 if (co
->index
>= MXS_AUART_PORTS
|| co
->index
< 0)
859 s
= auart_port
[co
->index
];
864 /* First save the CR then disable the interrupts */
865 old_ctrl2
= readl(port
->membase
+ AUART_CTRL2
);
866 old_ctrl0
= readl(port
->membase
+ AUART_CTRL0
);
868 writel(AUART_CTRL0_CLKGATE
,
869 port
->membase
+ AUART_CTRL0_CLR
);
870 writel(AUART_CTRL2_UARTEN
| AUART_CTRL2_TXE
,
871 port
->membase
+ AUART_CTRL2_SET
);
873 uart_console_write(port
, str
, count
, mxs_auart_console_putchar
);
875 /* Finally, wait for transmitter to become empty ... */
876 while (readl(port
->membase
+ AUART_STAT
) & AUART_STAT_BUSY
) {
883 * ... and restore the TCR if we waited long enough for the transmitter
884 * to be idle. This might keep the transmitter enabled although it is
885 * unused, but that is better than to disable it while it is still
888 if (!(readl(port
->membase
+ AUART_STAT
) & AUART_STAT_BUSY
)) {
889 writel(old_ctrl0
, port
->membase
+ AUART_CTRL0
);
890 writel(old_ctrl2
, port
->membase
+ AUART_CTRL2
);
897 auart_console_get_options(struct uart_port
*port
, int *baud
,
898 int *parity
, int *bits
)
900 unsigned int lcr_h
, quot
;
902 if (!(readl(port
->membase
+ AUART_CTRL2
) & AUART_CTRL2_UARTEN
))
905 lcr_h
= readl(port
->membase
+ AUART_LINECTRL
);
908 if (lcr_h
& AUART_LINECTRL_PEN
) {
909 if (lcr_h
& AUART_LINECTRL_EPS
)
915 if ((lcr_h
& AUART_LINECTRL_WLEN_MASK
) == AUART_LINECTRL_WLEN(2))
920 quot
= ((readl(port
->membase
+ AUART_LINECTRL
)
921 & AUART_LINECTRL_BAUD_DIVINT_MASK
))
922 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT
- 6);
923 quot
|= ((readl(port
->membase
+ AUART_LINECTRL
)
924 & AUART_LINECTRL_BAUD_DIVFRAC_MASK
))
925 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT
;
929 *baud
= (port
->uartclk
<< 2) / quot
;
933 auart_console_setup(struct console
*co
, char *options
)
935 struct mxs_auart_port
*s
;
943 * Check whether an invalid uart number has been specified, and
944 * if so, search for the first available port that does have
947 if (co
->index
== -1 || co
->index
>= ARRAY_SIZE(auart_port
))
949 s
= auart_port
[co
->index
];
953 clk_prepare_enable(s
->clk
);
956 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
958 auart_console_get_options(&s
->port
, &baud
, &parity
, &bits
);
960 ret
= uart_set_options(&s
->port
, co
, baud
, parity
, bits
, flow
);
962 clk_disable_unprepare(s
->clk
);
967 static struct console auart_console
= {
969 .write
= auart_console_write
,
970 .device
= uart_console_device
,
971 .setup
= auart_console_setup
,
972 .flags
= CON_PRINTBUFFER
,
974 .data
= &auart_driver
,
978 static struct uart_driver auart_driver
= {
979 .owner
= THIS_MODULE
,
980 .driver_name
= "ttyAPP",
981 .dev_name
= "ttyAPP",
984 .nr
= MXS_AUART_PORTS
,
985 #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
986 .cons
= &auart_console
,
991 * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
992 * could successfully get all information from dt or a negative errno.
994 static int serial_mxs_probe_dt(struct mxs_auart_port
*s
,
995 struct platform_device
*pdev
)
997 struct device_node
*np
= pdev
->dev
.of_node
;
1001 /* no device tree device */
1004 ret
= of_alias_get_id(np
, "serial");
1006 dev_err(&pdev
->dev
, "failed to get alias id: %d\n", ret
);
1011 if (of_get_property(np
, "fsl,uart-has-rtscts", NULL
))
1012 set_bit(MXS_AUART_RTSCTS
, &s
->flags
);
1017 static int mxs_auart_probe(struct platform_device
*pdev
)
1019 const struct of_device_id
*of_id
=
1020 of_match_device(mxs_auart_dt_ids
, &pdev
->dev
);
1021 struct mxs_auart_port
*s
;
1026 s
= kzalloc(sizeof(struct mxs_auart_port
), GFP_KERNEL
);
1032 ret
= serial_mxs_probe_dt(s
, pdev
);
1034 s
->port
.line
= pdev
->id
< 0 ? 0 : pdev
->id
;
1039 pdev
->id_entry
= of_id
->data
;
1040 s
->devtype
= pdev
->id_entry
->driver_data
;
1043 s
->clk
= clk_get(&pdev
->dev
, NULL
);
1044 if (IS_ERR(s
->clk
)) {
1045 ret
= PTR_ERR(s
->clk
);
1049 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1055 s
->port
.mapbase
= r
->start
;
1056 s
->port
.membase
= ioremap(r
->start
, resource_size(r
));
1057 s
->port
.ops
= &mxs_auart_ops
;
1058 s
->port
.iotype
= UPIO_MEM
;
1059 s
->port
.fifosize
= 16;
1060 s
->port
.uartclk
= clk_get_rate(s
->clk
);
1061 s
->port
.type
= PORT_IMX
;
1062 s
->port
.dev
= s
->dev
= &pdev
->dev
;
1066 s
->irq
= platform_get_irq(pdev
, 0);
1067 s
->port
.irq
= s
->irq
;
1068 ret
= request_irq(s
->irq
, mxs_auart_irq_handle
, 0, dev_name(&pdev
->dev
), s
);
1072 platform_set_drvdata(pdev
, s
);
1074 auart_port
[s
->port
.line
] = s
;
1076 mxs_auart_reset(&s
->port
);
1078 ret
= uart_add_one_port(&auart_driver
, &s
->port
);
1082 version
= readl(s
->port
.membase
+ AUART_VERSION
);
1083 dev_info(&pdev
->dev
, "Found APPUART %d.%d.%d\n",
1084 (version
>> 24) & 0xff,
1085 (version
>> 16) & 0xff, version
& 0xffff);
1090 auart_port
[pdev
->id
] = NULL
;
1091 free_irq(s
->irq
, s
);
1100 static int mxs_auart_remove(struct platform_device
*pdev
)
1102 struct mxs_auart_port
*s
= platform_get_drvdata(pdev
);
1104 uart_remove_one_port(&auart_driver
, &s
->port
);
1106 auart_port
[pdev
->id
] = NULL
;
1109 free_irq(s
->irq
, s
);
1115 static struct platform_driver mxs_auart_driver
= {
1116 .probe
= mxs_auart_probe
,
1117 .remove
= mxs_auart_remove
,
1119 .name
= "mxs-auart",
1120 .owner
= THIS_MODULE
,
1121 .of_match_table
= mxs_auart_dt_ids
,
1125 static int __init
mxs_auart_init(void)
1129 r
= uart_register_driver(&auart_driver
);
1133 r
= platform_driver_register(&mxs_auart_driver
);
1139 uart_unregister_driver(&auart_driver
);
1144 static void __exit
mxs_auart_exit(void)
1146 platform_driver_unregister(&mxs_auart_driver
);
1147 uart_unregister_driver(&auart_driver
);
1150 module_init(mxs_auart_init
);
1151 module_exit(mxs_auart_exit
);
1152 MODULE_LICENSE("GPL");
1153 MODULE_DESCRIPTION("Freescale MXS application uart driver");
1154 MODULE_ALIAS("platform:mxs-auart");