17 #include <linux/kernel.h>
18 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/wait.h>
25 #include <linux/tty.h>
28 #include <linux/serial.h>
29 #include <linux/serial_core.h>
31 #include <linux/device.h>
38 #include <asm/cacheflush.h>
40 #define MXS_AUART_PORTS 5
42 #define AUART_CTRL0 0x00000000
43 #define AUART_CTRL0_SET 0x00000004
44 #define AUART_CTRL0_CLR 0x00000008
45 #define AUART_CTRL0_TOG 0x0000000c
46 #define AUART_CTRL1 0x00000010
47 #define AUART_CTRL1_SET 0x00000014
48 #define AUART_CTRL1_CLR 0x00000018
49 #define AUART_CTRL1_TOG 0x0000001c
50 #define AUART_CTRL2 0x00000020
51 #define AUART_CTRL2_SET 0x00000024
52 #define AUART_CTRL2_CLR 0x00000028
53 #define AUART_CTRL2_TOG 0x0000002c
54 #define AUART_LINECTRL 0x00000030
55 #define AUART_LINECTRL_SET 0x00000034
56 #define AUART_LINECTRL_CLR 0x00000038
57 #define AUART_LINECTRL_TOG 0x0000003c
58 #define AUART_LINECTRL2 0x00000040
59 #define AUART_LINECTRL2_SET 0x00000044
60 #define AUART_LINECTRL2_CLR 0x00000048
61 #define AUART_LINECTRL2_TOG 0x0000004c
62 #define AUART_INTR 0x00000050
63 #define AUART_INTR_SET 0x00000054
64 #define AUART_INTR_CLR 0x00000058
65 #define AUART_INTR_TOG 0x0000005c
66 #define AUART_DATA 0x00000060
67 #define AUART_STAT 0x00000070
68 #define AUART_DEBUG 0x00000080
69 #define AUART_VERSION 0x00000090
70 #define AUART_AUTOBAUD 0x000000a0
72 #define AUART_CTRL0_SFTRST (1 << 31)
73 #define AUART_CTRL0_CLKGATE (1 << 30)
75 #define AUART_CTRL2_CTSEN (1 << 15)
76 #define AUART_CTRL2_RTSEN (1 << 14)
77 #define AUART_CTRL2_RTS (1 << 11)
78 #define AUART_CTRL2_RXE (1 << 9)
79 #define AUART_CTRL2_TXE (1 << 8)
80 #define AUART_CTRL2_UARTEN (1 << 0)
82 #define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
83 #define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
84 #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
85 #define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
86 #define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
87 #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
88 #define AUART_LINECTRL_WLEN_MASK 0x00000060
89 #define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
90 #define AUART_LINECTRL_FEN (1 << 4)
91 #define AUART_LINECTRL_STP2 (1 << 3)
92 #define AUART_LINECTRL_EPS (1 << 2)
93 #define AUART_LINECTRL_PEN (1 << 1)
94 #define AUART_LINECTRL_BRK (1 << 0)
96 #define AUART_INTR_RTIEN (1 << 22)
97 #define AUART_INTR_TXIEN (1 << 21)
98 #define AUART_INTR_RXIEN (1 << 20)
99 #define AUART_INTR_CTSMIEN (1 << 17)
100 #define AUART_INTR_RTIS (1 << 6)
101 #define AUART_INTR_TXIS (1 << 5)
102 #define AUART_INTR_RXIS (1 << 4)
103 #define AUART_INTR_CTSMIS (1 << 1)
105 #define AUART_STAT_BUSY (1 << 29)
106 #define AUART_STAT_CTS (1 << 28)
107 #define AUART_STAT_TXFE (1 << 27)
108 #define AUART_STAT_TXFF (1 << 25)
109 #define AUART_STAT_RXFE (1 << 24)
110 #define AUART_STAT_OERR (1 << 19)
111 #define AUART_STAT_BERR (1 << 18)
112 #define AUART_STAT_PERR (1 << 17)
113 #define AUART_STAT_FERR (1 << 16)
129 static void mxs_auart_stop_tx(
struct uart_port *
u);
131 #define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
139 if (s->
port.x_char) {
164 if (uart_tx_stopped(&s->
port))
165 mxs_auart_stop_tx(&s->
port);
181 s->
port.icount.brk++;
182 if (uart_handle_break(&s->
port))
185 s->
port.icount.parity++;
187 s->
port.icount.frame++;
193 stat &= s->
port.read_status_mask;
195 if (stat & AUART_STAT_BERR) {
197 }
else if (stat & AUART_STAT_PERR)
199 else if (stat & AUART_STAT_FERR)
203 s->
port.icount.overrun++;
222 mxs_auart_rx_char(s);
229 static int mxs_auart_request_port(
struct uart_port *
u)
234 static int mxs_auart_verify_port(
struct uart_port *
u,
246 static const char *mxs_auart_type(
struct uart_port *u)
250 return dev_name(s->
dev);
253 static void mxs_auart_release_port(
struct uart_port *u)
257 static void mxs_auart_set_mctrl(
struct uart_port *u,
unsigned mctrl)
265 if (tty_port_cts_enabled(&u->
state->port))
290 static void mxs_auart_settermios(
struct uart_port *u,
303 switch (cflag &
CSIZE) {
325 if ((cflag &
PARODD) == 0)
394 istat &= ~AUART_INTR_CTSMIS;
398 mxs_auart_rx_chars(s);
403 mxs_auart_tx_chars(s);
404 istat &= ~AUART_INTR_TXIS;
410 | AUART_INTR_CTSMIS),
416 static void mxs_auart_reset(
struct uart_port *u)
423 for (i = 0; i < 10000; i++) {
432 static int mxs_auart_startup(
struct uart_port *u)
436 clk_prepare_enable(s->
clk);
454 static void mxs_auart_shutdown(
struct uart_port *u)
465 clk_disable_unprepare(s->
clk);
468 static unsigned int mxs_auart_tx_empty(
struct uart_port *u)
476 static void mxs_auart_start_tx(
struct uart_port *u)
483 mxs_auart_tx_chars(s);
486 static void mxs_auart_stop_tx(
struct uart_port *u)
491 static void mxs_auart_stop_rx(
struct uart_port *u)
496 static void mxs_auart_break_ctl(
struct uart_port *u,
int ctl)
511 static struct uart_ops mxs_auart_ops = {
512 .tx_empty = mxs_auart_tx_empty,
513 .start_tx = mxs_auart_start_tx,
514 .stop_tx = mxs_auart_stop_tx,
515 .stop_rx = mxs_auart_stop_rx,
516 .enable_ms = mxs_auart_enable_ms,
517 .break_ctl = mxs_auart_break_ctl,
518 .set_mctrl = mxs_auart_set_mctrl,
519 .get_mctrl = mxs_auart_get_mctrl,
520 .startup = mxs_auart_startup,
521 .shutdown = mxs_auart_shutdown,
522 .set_termios = mxs_auart_settermios,
523 .type = mxs_auart_type,
524 .release_port = mxs_auart_release_port,
525 .request_port = mxs_auart_request_port,
526 .config_port = mxs_auart_config_port,
527 .verify_port = mxs_auart_verify_port,
532 #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
533 static void mxs_auart_console_putchar(
struct uart_port *
port,
int ch)
535 unsigned int to = 1000;
547 auart_console_write(
struct console *co,
const char *
str,
unsigned int count)
551 unsigned int old_ctrl0, old_ctrl2;
552 unsigned int to = 1000;
557 s = auart_port[co->
index];
590 auart_console_get_options(
struct uart_port *port,
int *baud,
591 int *parity,
int *
bits)
593 unsigned int lcr_h, quot;
622 *baud = (port->
uartclk << 2) / quot;
642 s = auart_port[co->
index];
646 clk_prepare_enable(s->
clk);
651 auart_console_get_options(&s->
port, &baud, &parity, &bits);
655 clk_disable_unprepare(s->
clk);
660 static struct console auart_console = {
662 .write = auart_console_write,
664 .setup = auart_console_setup,
667 .data = &auart_driver,
673 .driver_name =
"ttyAPP",
674 .dev_name =
"ttyAPP",
678 #ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
679 .cons = &auart_console,
699 dev_err(&pdev->
dev,
"failed to get alias id: %d\n", ret);
721 ret = serial_mxs_probe_dt(s, pdev);
723 s->
port.line = pdev->
id < 0 ? 0 : pdev->
id;
727 pinctrl = devm_pinctrl_get_select_default(&pdev->
dev);
728 if (IS_ERR(pinctrl)) {
729 ret = PTR_ERR(pinctrl);
734 if (IS_ERR(s->
clk)) {
735 ret = PTR_ERR(s->
clk);
747 s->
port.ops = &mxs_auart_ops;
749 s->
port.fifosize = 16;
763 platform_set_drvdata(pdev, s);
765 auart_port[s->
port.line] =
s;
767 mxs_auart_reset(&s->
port);
775 (version >> 24) & 0xff,
776 (version >> 16) & 0xff, version & 0xffff);
781 auart_port[pdev->
id] =
NULL;
798 auart_port[pdev->
id] =
NULL;
809 { .compatible =
"fsl,imx23-auart", },
815 .probe = mxs_auart_probe,
820 .of_match_table = mxs_auart_dt_ids,
824 static int __init mxs_auart_init(
void)
843 static void __exit mxs_auart_exit(
void)