30 #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34 #include <linux/module.h>
40 #include <linux/tty.h>
42 #include <linux/serial_core.h>
43 #include <linux/serial.h>
47 #include <linux/slab.h>
71 #define IMX21_ONEMS 0xb0
73 #define IMX21_UTS 0xb4
76 #define URXD_CHARRDY (1<<15)
77 #define URXD_ERR (1<<14)
78 #define URXD_OVRRUN (1<<13)
79 #define URXD_FRMERR (1<<12)
80 #define URXD_BRK (1<<11)
81 #define URXD_PRERR (1<<10)
82 #define UCR1_ADEN (1<<15)
83 #define UCR1_ADBR (1<<14)
84 #define UCR1_TRDYEN (1<<13)
85 #define UCR1_IDEN (1<<12)
86 #define UCR1_RRDYEN (1<<9)
87 #define UCR1_RDMAEN (1<<8)
88 #define UCR1_IREN (1<<7)
89 #define UCR1_TXMPTYEN (1<<6)
90 #define UCR1_RTSDEN (1<<5)
91 #define UCR1_SNDBRK (1<<4)
92 #define UCR1_TDMAEN (1<<3)
93 #define IMX1_UCR1_UARTCLKEN (1<<2)
94 #define UCR1_DOZE (1<<1)
95 #define UCR1_UARTEN (1<<0)
96 #define UCR2_ESCI (1<<15)
97 #define UCR2_IRTS (1<<14)
98 #define UCR2_CTSC (1<<13)
99 #define UCR2_CTS (1<<12)
100 #define UCR2_ESCEN (1<<11)
101 #define UCR2_PREN (1<<8)
102 #define UCR2_PROE (1<<7)
103 #define UCR2_STPB (1<<6)
104 #define UCR2_WS (1<<5)
105 #define UCR2_RTSEN (1<<4)
106 #define UCR2_ATEN (1<<3)
107 #define UCR2_TXEN (1<<2)
108 #define UCR2_RXEN (1<<1)
109 #define UCR2_SRST (1<<0)
110 #define UCR3_DTREN (1<<13)
111 #define UCR3_PARERREN (1<<12)
112 #define UCR3_FRAERREN (1<<11)
113 #define UCR3_DSR (1<<10)
114 #define UCR3_DCD (1<<9)
115 #define UCR3_RI (1<<8)
116 #define UCR3_TIMEOUTEN (1<<7)
117 #define UCR3_RXDSEN (1<<6)
118 #define UCR3_AIRINTEN (1<<5)
119 #define UCR3_AWAKEN (1<<4)
120 #define IMX21_UCR3_RXDMUXSEL (1<<2)
121 #define UCR3_INVT (1<<1)
122 #define UCR3_BPEN (1<<0)
123 #define UCR4_CTSTL_SHF 10
124 #define UCR4_CTSTL_MASK 0x3F
125 #define UCR4_INVR (1<<9)
126 #define UCR4_ENIRI (1<<8)
127 #define UCR4_WKEN (1<<7)
128 #define UCR4_REF16 (1<<6)
129 #define UCR4_IRSC (1<<5)
130 #define UCR4_TCEN (1<<3)
131 #define UCR4_BKEN (1<<2)
132 #define UCR4_OREN (1<<1)
133 #define UCR4_DREN (1<<0)
134 #define UFCR_RXTL_SHF 0
135 #define UFCR_DCEDTE (1<<6)
136 #define UFCR_RFDIV (7<<7)
137 #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
138 #define UFCR_TXTL_SHF 10
139 #define USR1_PARITYERR (1<<15)
140 #define USR1_RTSS (1<<14)
141 #define USR1_TRDY (1<<13)
142 #define USR1_RTSD (1<<12)
143 #define USR1_ESCF (1<<11)
144 #define USR1_FRAMERR (1<<10)
145 #define USR1_RRDY (1<<9)
146 #define USR1_TIMEOUT (1<<7)
147 #define USR1_RXDS (1<<6)
148 #define USR1_AIRINT (1<<5)
149 #define USR1_AWAKE (1<<4)
150 #define USR2_ADET (1<<15)
151 #define USR2_TXFE (1<<14)
152 #define USR2_DTRF (1<<13)
153 #define USR2_IDLE (1<<12)
154 #define USR2_IRINT (1<<8)
155 #define USR2_WAKE (1<<7)
156 #define USR2_RTSF (1<<4)
157 #define USR2_TXDC (1<<3)
158 #define USR2_BRCD (1<<2)
159 #define USR2_ORE (1<<1)
160 #define USR2_RDR (1<<0)
161 #define UTS_FRCPERR (1<<13)
162 #define UTS_LOOP (1<<12)
163 #define UTS_TXEMPTY (1<<6)
164 #define UTS_RXEMPTY (1<<5)
165 #define UTS_TXFULL (1<<4)
166 #define UTS_RXFULL (1<<3)
167 #define UTS_SOFTRST (1<<0)
170 #define SERIAL_IMX_MAJOR 207
171 #define MINOR_START 16
172 #define DEV_NAME "ttymxc"
180 #define MCTRL_TIMEOUT (250*HZ/1000)
182 #define DRIVER_NAME "IMX-uart"
220 #define USE_IRDA(sport) ((sport)->use_irda)
222 #define USE_IRDA(sport) (0)
241 .name =
"imx21-uart",
250 { .compatible =
"fsl,imx1-uart", .data = &imx_uart_devdata[
IMX1_UART], },
251 { .compatible =
"fsl,imx21-uart", .data = &imx_uart_devdata[
IMX21_UART], },
256 static inline unsigned uts_reg(
struct imx_port *sport)
258 return sport->
devdata->uts_reg;
261 static inline int is_imx1_uart(
struct imx_port *sport)
266 static inline int is_imx21_uart(
struct imx_port *sport)
295 static void imx_mctrl_check(
struct imx_port *sport)
299 status = sport->
port.ops->get_mctrl(&sport->
port);
308 sport->
port.icount.rng++;
310 sport->
port.icount.dsr++;
323 static void imx_timeout(
unsigned long data)
328 if (sport->
port.state) {
330 imx_mctrl_check(sport);
331 spin_unlock_irqrestore(&sport->
port.lock, flags);
394 static void imx_stop_rx(
struct uart_port *port)
406 static void imx_enable_ms(
struct uart_port *port)
413 static inline void imx_transmit_buffer(
struct imx_port *sport)
418 !(
readl(sport->
port.membase + uts_reg(sport))
424 sport->
port.icount.tx++;
431 imx_stop_tx(&sport->
port);
437 static void imx_start_tx(
struct uart_port *port)
467 imx_transmit_buffer(sport);
483 spin_unlock_irqrestore(&sport->
port.lock, flags);
494 if (sport->
port.x_char)
502 imx_stop_tx(&sport->
port);
506 imx_transmit_buffer(sport);
512 spin_unlock_irqrestore(&sport->
port.lock,flags);
516 static irqreturn_t imx_rxint(
int irq,
void *dev_id)
527 sport->
port.icount.rx++;
534 if (uart_handle_break(&sport->
port))
543 sport->
port.icount.brk++;
545 sport->
port.icount.parity++;
547 sport->
port.icount.frame++;
549 sport->
port.icount.overrun++;
551 if (rx & sport->
port.ignore_status_mask) {
557 rx &= sport->
port.read_status_mask;
561 else if (rx & URXD_PRERR)
563 else if (rx & URXD_FRMERR)
565 if (rx & URXD_OVRRUN)
569 sport->
port.sysrq = 0;
573 tty_insert_flip_char(tty, rx, flg);
577 spin_unlock_irqrestore(&sport->
port.lock,flags);
590 imx_rxint(irq, dev_id);
594 imx_txint(irq, dev_id);
597 imx_rtsint(irq, dev_id);
608 static unsigned int imx_tx_empty(
struct uart_port *port)
618 static unsigned int imx_get_mctrl(
struct uart_port *port)
632 static void imx_set_mctrl(
struct uart_port *port,
unsigned int mctrl)
648 static void imx_break_ctl(
struct uart_port *port,
int break_state)
657 if ( break_state != 0 )
662 spin_unlock_irqrestore(&sport->
port.lock, flags);
668 static int imx_setup_ufcr(
struct imx_port *sport,
unsigned int mode)
682 static int imx_startup(
struct uart_port *port)
688 imx_setup_ufcr(sport, 0);
720 if (sport->
txirq > 0) {
776 if (is_imx21_uart(sport)) {
801 imx_enable_ms(&sport->
port);
802 spin_unlock_irqrestore(&sport->
port.lock,flags);
806 pdata = sport->
port.dev->platform_data;
826 static void imx_shutdown(
struct uart_port *port)
836 spin_unlock_irqrestore(&sport->
port.lock, flags);
840 pdata = sport->
port.dev->platform_data;
853 if (sport->
txirq > 0) {
872 spin_unlock_irqrestore(&sport->
port.lock, flags);
881 unsigned int ucr2, old_ucr1, old_txrxen,
baud, quot;
883 unsigned int div, ufcr;
884 unsigned long num, denom;
938 sport->
port.read_status_mask = 0;
942 sport->
port.read_status_mask |= URXD_BRK;
947 sport->
port.ignore_status_mask = 0;
988 div = sport->
port.uartclk / (baud * 16);
996 1 << 16, 1 << 16, &num, &denom);
998 tdiv64 = sport->
port.uartclk;
1000 do_div(tdiv64, denom * 16 * div);
1014 if (is_imx21_uart(sport))
1024 imx_enable_ms(&sport->
port);
1026 spin_unlock_irqrestore(&sport->
port.lock, flags);
1029 static const char *imx_type(
struct uart_port *port)
1039 static void imx_release_port(
struct uart_port *port)
1051 static int imx_request_port(
struct uart_port *port)
1063 return ret ? 0 : -
EBUSY;
1069 static void imx_config_port(
struct uart_port *port,
int flags)
1074 imx_request_port(&sport->
port) == 0)
1091 if (sport->
port.irq != ser->
irq)
1099 if (sport->
port.iobase != ser->
port)
1106 #if defined(CONFIG_CONSOLE_POLL)
1107 static int imx_poll_get_char(
struct uart_port *port)
1114 imx_port_ucrs_save(port, &old_ucr);
1126 }
while (~status & USR2_RDR);
1132 imx_port_ucrs_restore(port, &old_ucr);
1137 static void imx_poll_put_char(
struct uart_port *port,
unsigned char c)
1143 imx_port_ucrs_save(port, &old_ucr);
1163 }
while (~status & USR2_TXDC);
1166 imx_port_ucrs_restore(port, &old_ucr);
1170 static struct uart_ops imx_pops = {
1171 .tx_empty = imx_tx_empty,
1172 .set_mctrl = imx_set_mctrl,
1173 .get_mctrl = imx_get_mctrl,
1174 .stop_tx = imx_stop_tx,
1175 .start_tx = imx_start_tx,
1176 .stop_rx = imx_stop_rx,
1177 .enable_ms = imx_enable_ms,
1178 .break_ctl = imx_break_ctl,
1179 .startup = imx_startup,
1180 .shutdown = imx_shutdown,
1181 .set_termios = imx_set_termios,
1183 .release_port = imx_release_port,
1184 .request_port = imx_request_port,
1185 .config_port = imx_config_port,
1186 .verify_port = imx_verify_port,
1187 #if defined(CONFIG_CONSOLE_POLL)
1188 .poll_get_char = imx_poll_get_char,
1189 .poll_put_char = imx_poll_put_char,
1195 #ifdef CONFIG_SERIAL_IMX_CONSOLE
1196 static void imx_console_putchar(
struct uart_port *port,
int ch)
1210 imx_console_write(
struct console *co,
const char *
s,
unsigned int count)
1215 unsigned long flags;
1222 imx_port_ucrs_save(&sport->
port, &old_ucr);
1223 ucr1 = old_ucr.ucr1;
1225 if (is_imx1_uart(sport))
1242 imx_port_ucrs_restore(&sport->
port, &old_ucr);
1244 spin_unlock_irqrestore(&sport->
port.lock, flags);
1252 imx_console_get_options(
struct imx_port *sport,
int *baud,
1253 int *parity,
int *
bits)
1258 unsigned int ucr2, ubir,ubmr, uartclk;
1259 unsigned int baud_raw;
1260 unsigned int ucfr_rfdiv;
1281 if (ucfr_rfdiv == 6)
1284 ucfr_rfdiv = 6 - ucfr_rfdiv;
1287 uartclk /= ucfr_rfdiv;
1295 unsigned int mul = ubir + 1;
1296 unsigned int div = 16 * (ubmr + 1);
1297 unsigned int rem = uartclk %
div;
1299 baud_raw = (uartclk /
div) * mul;
1300 baud_raw += (rem * mul + div / 2) / div;
1301 *baud = (baud_raw + 50) / 100 * 100;
1304 if(*baud != baud_raw)
1305 printk(
KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
1326 sport = imx_ports[co->
index];
1333 imx_console_get_options(sport, &baud, &parity, &bits);
1335 imx_setup_ufcr(sport, 0);
1341 static struct console imx_console = {
1343 .write = imx_console_write,
1345 .setup = imx_console_setup,
1351 #define IMX_CONSOLE &imx_console
1353 #define IMX_CONSOLE NULL
1368 struct imx_port *sport = platform_get_drvdata(dev);
1383 struct imx_port *sport = platform_get_drvdata(dev);
1401 static int serial_imx_probe_dt(
struct imx_port *sport,
1415 dev_err(&pdev->
dev,
"failed to get alias id, errno %d\n", ret);
1431 static inline int serial_imx_probe_dt(
struct imx_port *sport,
1438 static void serial_imx_probe_pdata(
struct imx_port *sport,
1443 sport->
port.line = pdev->
id;
1469 ret = serial_imx_probe_dt(sport, pdev);
1471 serial_imx_probe_pdata(sport, pdev);
1489 sport->
port.membase = base;
1496 sport->
port.fifosize = 32;
1497 sport->
port.ops = &imx_pops;
1500 sport->
timer.function = imx_timeout;
1501 sport->
timer.data = (
unsigned long)sport;
1503 pinctrl = devm_pinctrl_get_select_default(&pdev->
dev);
1504 if (IS_ERR(pinctrl)) {
1505 ret = PTR_ERR(pinctrl);
1506 dev_err(&pdev->
dev,
"failed to get default pinctrl: %d\n", ret);
1512 ret = PTR_ERR(sport->
clk_ipg);
1513 dev_err(&pdev->
dev,
"failed to get ipg clk: %d\n", ret);
1519 ret = PTR_ERR(sport->
clk_per);
1520 dev_err(&pdev->
dev,
"failed to get per clk: %d\n", ret);
1524 clk_prepare_enable(sport->
clk_per);
1525 clk_prepare_enable(sport->
clk_ipg);
1529 imx_ports[sport->
port.line] = sport;
1531 pdata = pdev->
dev.platform_data;
1532 if (pdata && pdata->
init) {
1533 ret = pdata->
init(pdev);
1541 platform_set_drvdata(pdev, sport);
1545 if (pdata && pdata->
exit)
1548 clk_disable_unprepare(sport->
clk_per);
1549 clk_disable_unprepare(sport->
clk_ipg);
1561 struct imx_port *sport = platform_get_drvdata(pdev);
1563 pdata = pdev->
dev.platform_data;
1565 platform_set_drvdata(pdev,
NULL);
1569 clk_disable_unprepare(sport->
clk_per);
1570 clk_disable_unprepare(sport->
clk_ipg);
1572 if (pdata && pdata->
exit)
1582 .probe = serial_imx_probe,
1583 .remove = serial_imx_remove,
1585 .suspend = serial_imx_suspend,
1586 .resume = serial_imx_resume,
1587 .id_table = imx_uart_devtype,
1591 .of_match_table = imx_uart_dt_ids,
1595 static int __init imx_serial_init(
void)
1612 static void __exit imx_serial_exit(
void)