1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Motorola/Freescale IMX serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Author: Sascha Hauer <sascha@saschahauer.de>
8 * Copyright (C) 2004 Pengutronix
11 #include <linux/module.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/console.h>
15 #include <linux/sysrq.h>
16 #include <linux/platform_device.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial_core.h>
20 #include <linux/serial.h>
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/ktime.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/rational.h>
26 #include <linux/slab.h>
29 #include <linux/dma-mapping.h>
32 #include <linux/dma/imx-dma.h>
34 #include "serial_mctrl_gpio.h"
36 /* Register definitions */
37 #define URXD0 0x0 /* Receiver Register */
38 #define URTX0 0x40 /* Transmitter Register */
39 #define UCR1 0x80 /* Control Register 1 */
40 #define UCR2 0x84 /* Control Register 2 */
41 #define UCR3 0x88 /* Control Register 3 */
42 #define UCR4 0x8c /* Control Register 4 */
43 #define UFCR 0x90 /* FIFO Control Register */
44 #define USR1 0x94 /* Status Register 1 */
45 #define USR2 0x98 /* Status Register 2 */
46 #define UESC 0x9c /* Escape Character Register */
47 #define UTIM 0xa0 /* Escape Timer Register */
48 #define UBIR 0xa4 /* BRM Incremental Register */
49 #define UBMR 0xa8 /* BRM Modulator Register */
50 #define UBRC 0xac /* Baud Rate Count Register */
51 #define IMX21_ONEMS 0xb0 /* One Millisecond register */
52 #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
53 #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
55 /* UART Control Register Bit Fields.*/
56 #define URXD_DUMMY_READ (1<<16)
57 #define URXD_CHARRDY (1<<15)
58 #define URXD_ERR (1<<14)
59 #define URXD_OVRRUN (1<<13)
60 #define URXD_FRMERR (1<<12)
61 #define URXD_BRK (1<<11)
62 #define URXD_PRERR (1<<10)
63 #define URXD_RX_DATA (0xFF<<0)
64 #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
65 #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
66 #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
67 #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
68 #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
69 #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
70 #define UCR1_RXDMAEN (1<<8) /* Recv ready DMA enable */
71 #define UCR1_IREN (1<<7) /* Infrared interface enable */
72 #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
73 #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
74 #define UCR1_SNDBRK (1<<4) /* Send break */
75 #define UCR1_TXDMAEN (1<<3) /* Transmitter ready DMA enable */
76 #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
77 #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
78 #define UCR1_DOZE (1<<1) /* Doze */
79 #define UCR1_UARTEN (1<<0) /* UART enabled */
80 #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
81 #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
82 #define UCR2_CTSC (1<<13) /* CTS pin control */
83 #define UCR2_CTS (1<<12) /* Clear to send */
84 #define UCR2_ESCEN (1<<11) /* Escape enable */
85 #define UCR2_PREN (1<<8) /* Parity enable */
86 #define UCR2_PROE (1<<7) /* Parity odd/even */
87 #define UCR2_STPB (1<<6) /* Stop */
88 #define UCR2_WS (1<<5) /* Word size */
89 #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
90 #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
91 #define UCR2_TXEN (1<<2) /* Transmitter enabled */
92 #define UCR2_RXEN (1<<1) /* Receiver enabled */
93 #define UCR2_SRST (1<<0) /* SW reset */
94 #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
95 #define UCR3_PARERREN (1<<12) /* Parity enable */
96 #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
97 #define UCR3_DSR (1<<10) /* Data set ready */
98 #define UCR3_DCD (1<<9) /* Data carrier detect */
99 #define UCR3_RI (1<<8) /* Ring indicator */
100 #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
101 #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
102 #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
103 #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
104 #define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */
105 #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
106 #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
107 #define UCR3_BPEN (1<<0) /* Preset registers enable */
108 #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
109 #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
110 #define UCR4_INVR (1<<9) /* Inverted infrared reception */
111 #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
112 #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
113 #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
114 #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
115 #define UCR4_IRSC (1<<5) /* IR special case */
116 #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
117 #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
118 #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
119 #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
120 #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
121 #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
122 #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
123 #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
124 #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
125 #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
126 #define USR1_RTSS (1<<14) /* RTS pin status */
127 #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
128 #define USR1_RTSD (1<<12) /* RTS delta */
129 #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
130 #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
131 #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
132 #define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */
133 #define USR1_DTRD (1<<7) /* DTR Delta */
134 #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
135 #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
136 #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
137 #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
138 #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
139 #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
140 #define USR2_IDLE (1<<12) /* Idle condition */
141 #define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */
142 #define USR2_RIIN (1<<9) /* Ring Indicator Input */
143 #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
144 #define USR2_WAKE (1<<7) /* Wake */
145 #define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */
146 #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
147 #define USR2_TXDC (1<<3) /* Transmitter complete */
148 #define USR2_BRCD (1<<2) /* Break condition */
149 #define USR2_ORE (1<<1) /* Overrun error */
150 #define USR2_RDR (1<<0) /* Recv data ready */
151 #define UTS_FRCPERR (1<<13) /* Force parity error */
152 #define UTS_LOOP (1<<12) /* Loop tx and rx */
153 #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
154 #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
155 #define UTS_TXFULL (1<<4) /* TxFIFO full */
156 #define UTS_RXFULL (1<<3) /* RxFIFO full */
157 #define UTS_SOFTRST (1<<0) /* Software reset */
159 /* We've been assigned a range on the "Low-density serial ports" major */
160 #define SERIAL_IMX_MAJOR 207
161 #define MINOR_START 16
162 #define DEV_NAME "ttymxc"
165 * This determines how often we check the modem status signals
166 * for any change. They generally aren't connected to an IRQ
167 * so we have to poll them. We also check immediately before
168 * filling the TX fifo incase CTS has been dropped.
170 #define MCTRL_TIMEOUT (250*HZ/1000)
172 #define DRIVER_NAME "IMX-uart"
176 /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
184 /* device type dependent stuff */
185 struct imx_uart_data {
187 enum imx_uart_type devtype;
198 struct uart_port port;
199 struct timer_list timer;
200 unsigned int old_status;
201 unsigned int have_rtscts:1;
202 unsigned int have_rtsgpio:1;
203 unsigned int dte_mode:1;
204 unsigned int inverted_tx:1;
205 unsigned int inverted_rx:1;
208 const struct imx_uart_data *devdata;
210 struct mctrl_gpios *gpios;
212 /* counter to stop 0xff flood */
216 unsigned int dma_is_enabled:1;
217 unsigned int dma_is_rxing:1;
218 unsigned int dma_is_txing:1;
219 struct dma_chan *dma_chan_rx, *dma_chan_tx;
220 struct scatterlist rx_sgl, tx_sgl[2];
222 struct circ_buf rx_ring;
223 unsigned int rx_buf_size;
224 unsigned int rx_period_length;
225 unsigned int rx_periods;
226 dma_cookie_t rx_cookie;
227 unsigned int tx_bytes;
228 unsigned int dma_tx_nents;
229 unsigned int saved_reg[10];
232 enum imx_tx_state tx_state;
233 struct hrtimer trigger_start_tx;
234 struct hrtimer trigger_stop_tx;
237 struct imx_port_ucrs {
243 static struct imx_uart_data imx_uart_devdata[] = {
246 .devtype = IMX1_UART,
249 .uts_reg = IMX21_UTS,
250 .devtype = IMX21_UART,
253 .uts_reg = IMX21_UTS,
254 .devtype = IMX53_UART,
257 .uts_reg = IMX21_UTS,
258 .devtype = IMX6Q_UART,
262 static const struct of_device_id imx_uart_dt_ids[] = {
263 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
264 { .compatible = "fsl,imx53-uart", .data = &imx_uart_devdata[IMX53_UART], },
265 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
266 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
269 MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
271 static inline void imx_uart_writel(struct imx_port *sport, u32 val, u32 offset)
273 writel(val, sport->port.membase + offset);
276 static inline u32 imx_uart_readl(struct imx_port *sport, u32 offset)
278 return readl(sport->port.membase + offset);
281 static inline unsigned imx_uart_uts_reg(struct imx_port *sport)
283 return sport->devdata->uts_reg;
286 static inline int imx_uart_is_imx1(struct imx_port *sport)
288 return sport->devdata->devtype == IMX1_UART;
292 * Save and restore functions for UCR1, UCR2 and UCR3 registers
294 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
295 static void imx_uart_ucrs_save(struct imx_port *sport,
296 struct imx_port_ucrs *ucr)
298 /* save control registers */
299 ucr->ucr1 = imx_uart_readl(sport, UCR1);
300 ucr->ucr2 = imx_uart_readl(sport, UCR2);
301 ucr->ucr3 = imx_uart_readl(sport, UCR3);
304 static void imx_uart_ucrs_restore(struct imx_port *sport,
305 struct imx_port_ucrs *ucr)
307 /* restore control registers */
308 imx_uart_writel(sport, ucr->ucr1, UCR1);
309 imx_uart_writel(sport, ucr->ucr2, UCR2);
310 imx_uart_writel(sport, ucr->ucr3, UCR3);
314 /* called with port.lock taken and irqs caller dependent */
315 static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2)
317 *ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
319 mctrl_gpio_set(sport->gpios, sport->port.mctrl | TIOCM_RTS);
322 /* called with port.lock taken and irqs caller dependent */
323 static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2)
328 mctrl_gpio_set(sport->gpios, sport->port.mctrl & ~TIOCM_RTS);
331 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
333 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
336 /* called with port.lock taken and irqs off */
337 static void imx_uart_soft_reset(struct imx_port *sport)
340 u32 ucr2, ubir, ubmr, uts;
343 * According to the Reference Manual description of the UART SRST bit:
345 * "Reset the transmit and receive state machines,
346 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
349 * We don't need to restore the old values from USR1, USR2, URXD and
350 * UTXD. UBRC is read only, so only save/restore the other three
353 ubir = imx_uart_readl(sport, UBIR);
354 ubmr = imx_uart_readl(sport, UBMR);
355 uts = imx_uart_readl(sport, IMX21_UTS);
357 ucr2 = imx_uart_readl(sport, UCR2);
358 imx_uart_writel(sport, ucr2 & ~UCR2_SRST, UCR2);
360 while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
363 /* Restore the registers */
364 imx_uart_writel(sport, ubir, UBIR);
365 imx_uart_writel(sport, ubmr, UBMR);
366 imx_uart_writel(sport, uts, IMX21_UTS);
368 sport->idle_counter = 0;
371 static void imx_uart_disable_loopback_rs485(struct imx_port *sport)
375 /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
376 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
378 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
381 /* called with port.lock taken and irqs off */
382 static void imx_uart_start_rx(struct uart_port *port)
384 struct imx_port *sport = (struct imx_port *)port;
385 unsigned int ucr1, ucr2;
387 ucr1 = imx_uart_readl(sport, UCR1);
388 ucr2 = imx_uart_readl(sport, UCR2);
392 if (sport->dma_is_enabled) {
393 ucr1 |= UCR1_RXDMAEN | UCR1_ATDMAEN;
399 /* Write UCR2 first as it includes RXEN */
400 imx_uart_writel(sport, ucr2, UCR2);
401 imx_uart_writel(sport, ucr1, UCR1);
402 imx_uart_disable_loopback_rs485(sport);
405 /* called with port.lock taken and irqs off */
406 static void imx_uart_stop_tx(struct uart_port *port)
408 struct imx_port *sport = (struct imx_port *)port;
409 u32 ucr1, ucr4, usr2;
411 if (sport->tx_state == OFF)
415 * We are maybe in the SMP context, so if the DMA TX thread is running
416 * on other cpu, we have to wait for it to finish.
418 if (sport->dma_is_txing)
421 ucr1 = imx_uart_readl(sport, UCR1);
422 imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
424 ucr4 = imx_uart_readl(sport, UCR4);
425 usr2 = imx_uart_readl(sport, USR2);
426 if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
427 /* The shifter is still busy, so retry once TC triggers */
432 imx_uart_writel(sport, ucr4, UCR4);
434 /* in rs485 mode disable transmitter */
435 if (port->rs485.flags & SER_RS485_ENABLED) {
436 if (sport->tx_state == SEND) {
437 sport->tx_state = WAIT_AFTER_SEND;
439 if (port->rs485.delay_rts_after_send > 0) {
440 start_hrtimer_ms(&sport->trigger_stop_tx,
441 port->rs485.delay_rts_after_send);
445 /* continue without any delay */
448 if (sport->tx_state == WAIT_AFTER_RTS ||
449 sport->tx_state == WAIT_AFTER_SEND) {
452 hrtimer_try_to_cancel(&sport->trigger_start_tx);
454 ucr2 = imx_uart_readl(sport, UCR2);
455 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
456 imx_uart_rts_active(sport, &ucr2);
458 imx_uart_rts_inactive(sport, &ucr2);
459 imx_uart_writel(sport, ucr2, UCR2);
461 if (!port->rs485_rx_during_tx_gpio)
462 imx_uart_start_rx(port);
464 sport->tx_state = OFF;
467 sport->tx_state = OFF;
471 /* called with port.lock taken and irqs off */
472 static void imx_uart_stop_rx(struct uart_port *port)
474 struct imx_port *sport = (struct imx_port *)port;
475 u32 ucr1, ucr2, ucr4, uts;
477 ucr1 = imx_uart_readl(sport, UCR1);
478 ucr2 = imx_uart_readl(sport, UCR2);
479 ucr4 = imx_uart_readl(sport, UCR4);
481 if (sport->dma_is_enabled) {
482 ucr1 &= ~(UCR1_RXDMAEN | UCR1_ATDMAEN);
484 ucr1 &= ~UCR1_RRDYEN;
488 imx_uart_writel(sport, ucr1, UCR1);
489 imx_uart_writel(sport, ucr4, UCR4);
491 /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
492 if (port->rs485.flags & SER_RS485_ENABLED &&
493 port->rs485.flags & SER_RS485_RTS_ON_SEND &&
494 sport->have_rtscts && !sport->have_rtsgpio) {
495 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
497 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
503 imx_uart_writel(sport, ucr2, UCR2);
506 /* called with port.lock taken and irqs off */
507 static void imx_uart_enable_ms(struct uart_port *port)
509 struct imx_port *sport = (struct imx_port *)port;
511 mod_timer(&sport->timer, jiffies);
513 mctrl_gpio_enable_ms(sport->gpios);
516 static void imx_uart_dma_tx(struct imx_port *sport);
518 /* called with port.lock taken and irqs off */
519 static inline void imx_uart_transmit_buffer(struct imx_port *sport)
521 struct circ_buf *xmit = &sport->port.state->xmit;
523 if (sport->port.x_char) {
525 imx_uart_writel(sport, sport->port.x_char, URTX0);
526 sport->port.icount.tx++;
527 sport->port.x_char = 0;
531 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
532 imx_uart_stop_tx(&sport->port);
536 if (sport->dma_is_enabled) {
539 * We've just sent a X-char Ensure the TX DMA is enabled
540 * and the TX IRQ is disabled.
542 ucr1 = imx_uart_readl(sport, UCR1);
543 ucr1 &= ~UCR1_TRDYEN;
544 if (sport->dma_is_txing) {
545 ucr1 |= UCR1_TXDMAEN;
546 imx_uart_writel(sport, ucr1, UCR1);
548 imx_uart_writel(sport, ucr1, UCR1);
549 imx_uart_dma_tx(sport);
555 while (!uart_circ_empty(xmit) &&
556 !(imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)) {
557 /* send xmit->buf[xmit->tail]
558 * out the port here */
559 imx_uart_writel(sport, xmit->buf[xmit->tail], URTX0);
560 uart_xmit_advance(&sport->port, 1);
563 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
564 uart_write_wakeup(&sport->port);
566 if (uart_circ_empty(xmit))
567 imx_uart_stop_tx(&sport->port);
570 static void imx_uart_dma_tx_callback(void *data)
572 struct imx_port *sport = data;
573 struct scatterlist *sgl = &sport->tx_sgl[0];
574 struct circ_buf *xmit = &sport->port.state->xmit;
578 spin_lock_irqsave(&sport->port.lock, flags);
580 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
582 ucr1 = imx_uart_readl(sport, UCR1);
583 ucr1 &= ~UCR1_TXDMAEN;
584 imx_uart_writel(sport, ucr1, UCR1);
586 uart_xmit_advance(&sport->port, sport->tx_bytes);
588 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
590 sport->dma_is_txing = 0;
592 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
593 uart_write_wakeup(&sport->port);
595 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
596 imx_uart_dma_tx(sport);
597 else if (sport->port.rs485.flags & SER_RS485_ENABLED) {
598 u32 ucr4 = imx_uart_readl(sport, UCR4);
600 imx_uart_writel(sport, ucr4, UCR4);
603 spin_unlock_irqrestore(&sport->port.lock, flags);
606 /* called with port.lock taken and irqs off */
607 static void imx_uart_dma_tx(struct imx_port *sport)
609 struct circ_buf *xmit = &sport->port.state->xmit;
610 struct scatterlist *sgl = sport->tx_sgl;
611 struct dma_async_tx_descriptor *desc;
612 struct dma_chan *chan = sport->dma_chan_tx;
613 struct device *dev = sport->port.dev;
617 if (sport->dma_is_txing)
620 ucr4 = imx_uart_readl(sport, UCR4);
622 imx_uart_writel(sport, ucr4, UCR4);
624 sport->tx_bytes = uart_circ_chars_pending(xmit);
626 if (xmit->tail < xmit->head || xmit->head == 0) {
627 sport->dma_tx_nents = 1;
628 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
630 sport->dma_tx_nents = 2;
631 sg_init_table(sgl, 2);
632 sg_set_buf(sgl, xmit->buf + xmit->tail,
633 UART_XMIT_SIZE - xmit->tail);
634 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
637 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
639 dev_err(dev, "DMA mapping error for TX.\n");
642 desc = dmaengine_prep_slave_sg(chan, sgl, ret,
643 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
645 dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
647 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
650 desc->callback = imx_uart_dma_tx_callback;
651 desc->callback_param = sport;
653 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
654 uart_circ_chars_pending(xmit));
656 ucr1 = imx_uart_readl(sport, UCR1);
657 ucr1 |= UCR1_TXDMAEN;
658 imx_uart_writel(sport, ucr1, UCR1);
661 sport->dma_is_txing = 1;
662 dmaengine_submit(desc);
663 dma_async_issue_pending(chan);
667 /* called with port.lock taken and irqs off */
668 static void imx_uart_start_tx(struct uart_port *port)
670 struct imx_port *sport = (struct imx_port *)port;
673 if (!sport->port.x_char && uart_circ_empty(&port->state->xmit))
677 * We cannot simply do nothing here if sport->tx_state == SEND already
678 * because UCR1_TXMPTYEN might already have been cleared in
679 * imx_uart_stop_tx(), but tx_state is still SEND.
682 if (port->rs485.flags & SER_RS485_ENABLED) {
683 if (sport->tx_state == OFF) {
684 u32 ucr2 = imx_uart_readl(sport, UCR2);
685 if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
686 imx_uart_rts_active(sport, &ucr2);
688 imx_uart_rts_inactive(sport, &ucr2);
689 imx_uart_writel(sport, ucr2, UCR2);
691 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX) &&
692 !port->rs485_rx_during_tx_gpio)
693 imx_uart_stop_rx(port);
695 sport->tx_state = WAIT_AFTER_RTS;
697 if (port->rs485.delay_rts_before_send > 0) {
698 start_hrtimer_ms(&sport->trigger_start_tx,
699 port->rs485.delay_rts_before_send);
703 /* continue without any delay */
706 if (sport->tx_state == WAIT_AFTER_SEND
707 || sport->tx_state == WAIT_AFTER_RTS) {
709 hrtimer_try_to_cancel(&sport->trigger_stop_tx);
712 * Enable transmitter and shifter empty irq only if DMA
713 * is off. In the DMA case this is done in the
716 if (!sport->dma_is_enabled) {
717 u32 ucr4 = imx_uart_readl(sport, UCR4);
719 imx_uart_writel(sport, ucr4, UCR4);
722 sport->tx_state = SEND;
725 sport->tx_state = SEND;
728 if (!sport->dma_is_enabled) {
729 ucr1 = imx_uart_readl(sport, UCR1);
730 imx_uart_writel(sport, ucr1 | UCR1_TRDYEN, UCR1);
733 if (sport->dma_is_enabled) {
734 if (sport->port.x_char) {
735 /* We have X-char to send, so enable TX IRQ and
736 * disable TX DMA to let TX interrupt to send X-char */
737 ucr1 = imx_uart_readl(sport, UCR1);
738 ucr1 &= ~UCR1_TXDMAEN;
740 imx_uart_writel(sport, ucr1, UCR1);
744 if (!uart_circ_empty(&port->state->xmit) &&
745 !uart_tx_stopped(port))
746 imx_uart_dma_tx(sport);
751 static irqreturn_t __imx_uart_rtsint(int irq, void *dev_id)
753 struct imx_port *sport = dev_id;
756 imx_uart_writel(sport, USR1_RTSD, USR1);
757 usr1 = imx_uart_readl(sport, USR1) & USR1_RTSS;
758 uart_handle_cts_change(&sport->port, usr1);
759 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
764 static irqreturn_t imx_uart_rtsint(int irq, void *dev_id)
766 struct imx_port *sport = dev_id;
769 spin_lock(&sport->port.lock);
771 ret = __imx_uart_rtsint(irq, dev_id);
773 spin_unlock(&sport->port.lock);
778 static irqreturn_t imx_uart_txint(int irq, void *dev_id)
780 struct imx_port *sport = dev_id;
782 spin_lock(&sport->port.lock);
783 imx_uart_transmit_buffer(sport);
784 spin_unlock(&sport->port.lock);
788 /* Check if hardware Rx flood is in progress, and issue soft reset to stop it.
789 * This is to be called from Rx ISRs only when some bytes were actually
792 * A way to reproduce the flood (checked on iMX6SX) is: open iMX UART at 9600
793 * 8N1, and from external source send 0xf0 char at 115200 8N1. In about 90% of
794 * cases this starts a flood of "receiving" of 0xff characters by the iMX6 UART
795 * that is terminated by any activity on RxD line, or could be stopped by
796 * issuing soft reset to the UART (just stop/start of RX does not help). Note
797 * that what we do here is sending isolated start bit about 2.4 times shorter
798 * than it is to be on UART configured baud rate.
800 static void imx_uart_check_flood(struct imx_port *sport, u32 usr2)
802 /* To detect hardware 0xff flood we monitor RxD line between RX
803 * interrupts to isolate "receiving" of char(s) with no activity
804 * on RxD line, that'd never happen on actual data transfers.
806 * We use USR2_WAKE bit to check for activity on RxD line, but we have a
807 * race here if we clear USR2_WAKE when receiving of a char is in
808 * progress, so we might get RX interrupt later with USR2_WAKE bit
809 * cleared. Note though that as we don't try to clear USR2_WAKE when we
810 * detected no activity, this race may hide actual activity only once.
812 * Yet another case where receive interrupt may occur without RxD
813 * activity is expiration of aging timer, so we consider this as well.
815 * We use 'idle_counter' to ensure that we got at least so many RX
816 * interrupts without any detected activity on RxD line. 2 cases
817 * described plus 1 to be on the safe side gives us a margin of 3,
818 * below. In practice I was not able to produce a false positive to
819 * induce soft reset at regular data transfers even using 1 as the
820 * margin, so 3 is actually very strong.
822 * We count interrupts, not chars in 'idle-counter' for simplicity.
825 if (usr2 & USR2_WAKE) {
826 imx_uart_writel(sport, USR2_WAKE, USR2);
827 sport->idle_counter = 0;
828 } else if (++sport->idle_counter > 3) {
829 dev_warn(sport->port.dev, "RX flood detected: soft reset.");
830 imx_uart_soft_reset(sport); /* also clears 'sport->idle_counter' */
834 static irqreturn_t __imx_uart_rxint(int irq, void *dev_id)
836 struct imx_port *sport = dev_id;
837 struct tty_port *port = &sport->port.state->port;
840 /* If we received something, check for 0xff flood */
841 usr2 = imx_uart_readl(sport, USR2);
843 imx_uart_check_flood(sport, usr2);
845 while ((rx = imx_uart_readl(sport, URXD0)) & URXD_CHARRDY) {
846 unsigned int flg = TTY_NORMAL;
847 sport->port.icount.rx++;
849 if (unlikely(rx & URXD_ERR)) {
851 sport->port.icount.brk++;
852 if (uart_handle_break(&sport->port))
855 else if (rx & URXD_PRERR)
856 sport->port.icount.parity++;
857 else if (rx & URXD_FRMERR)
858 sport->port.icount.frame++;
859 if (rx & URXD_OVRRUN)
860 sport->port.icount.overrun++;
862 if (rx & sport->port.ignore_status_mask)
865 rx &= (sport->port.read_status_mask | 0xFF);
869 else if (rx & URXD_PRERR)
871 else if (rx & URXD_FRMERR)
873 if (rx & URXD_OVRRUN)
876 sport->port.sysrq = 0;
877 } else if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx)) {
881 if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
884 if (tty_insert_flip_char(port, rx, flg) == 0)
885 sport->port.icount.buf_overrun++;
888 tty_flip_buffer_push(port);
893 static irqreturn_t imx_uart_rxint(int irq, void *dev_id)
895 struct imx_port *sport = dev_id;
898 spin_lock(&sport->port.lock);
900 ret = __imx_uart_rxint(irq, dev_id);
902 spin_unlock(&sport->port.lock);
907 static void imx_uart_clear_rx_errors(struct imx_port *sport);
910 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
912 static unsigned int imx_uart_get_hwmctrl(struct imx_port *sport)
914 unsigned int tmp = TIOCM_DSR;
915 unsigned usr1 = imx_uart_readl(sport, USR1);
916 unsigned usr2 = imx_uart_readl(sport, USR2);
918 if (usr1 & USR1_RTSS)
921 /* in DCE mode DCDIN is always 0 */
922 if (!(usr2 & USR2_DCDIN))
926 if (!(imx_uart_readl(sport, USR2) & USR2_RIIN))
933 * Handle any change of modem status signal since we were last called.
935 static void imx_uart_mctrl_check(struct imx_port *sport)
937 unsigned int status, changed;
939 status = imx_uart_get_hwmctrl(sport);
940 changed = status ^ sport->old_status;
945 sport->old_status = status;
947 if (changed & TIOCM_RI && status & TIOCM_RI)
948 sport->port.icount.rng++;
949 if (changed & TIOCM_DSR)
950 sport->port.icount.dsr++;
951 if (changed & TIOCM_CAR)
952 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
953 if (changed & TIOCM_CTS)
954 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
956 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
959 static irqreturn_t imx_uart_int(int irq, void *dev_id)
961 struct imx_port *sport = dev_id;
962 unsigned int usr1, usr2, ucr1, ucr2, ucr3, ucr4;
963 irqreturn_t ret = IRQ_NONE;
965 spin_lock(&sport->port.lock);
967 usr1 = imx_uart_readl(sport, USR1);
968 usr2 = imx_uart_readl(sport, USR2);
969 ucr1 = imx_uart_readl(sport, UCR1);
970 ucr2 = imx_uart_readl(sport, UCR2);
971 ucr3 = imx_uart_readl(sport, UCR3);
972 ucr4 = imx_uart_readl(sport, UCR4);
975 * Even if a condition is true that can trigger an irq only handle it if
976 * the respective irq source is enabled. This prevents some undesired
977 * actions, for example if a character that sits in the RX FIFO and that
978 * should be fetched via DMA is tried to be fetched using PIO. Or the
979 * receiver is currently off and so reading from URXD0 results in an
980 * exception. So just mask the (raw) status bits for disabled irqs.
982 if ((ucr1 & UCR1_RRDYEN) == 0)
984 if ((ucr2 & UCR2_ATEN) == 0)
986 if ((ucr1 & UCR1_TRDYEN) == 0)
988 if ((ucr4 & UCR4_TCEN) == 0)
990 if ((ucr3 & UCR3_DTRDEN) == 0)
992 if ((ucr1 & UCR1_RTSDEN) == 0)
994 if ((ucr3 & UCR3_AWAKEN) == 0)
996 if ((ucr4 & UCR4_OREN) == 0)
999 if (usr1 & (USR1_RRDY | USR1_AGTIM)) {
1000 imx_uart_writel(sport, USR1_AGTIM, USR1);
1002 __imx_uart_rxint(irq, dev_id);
1006 if ((usr1 & USR1_TRDY) || (usr2 & USR2_TXDC)) {
1007 imx_uart_transmit_buffer(sport);
1011 if (usr1 & USR1_DTRD) {
1012 imx_uart_writel(sport, USR1_DTRD, USR1);
1014 imx_uart_mctrl_check(sport);
1019 if (usr1 & USR1_RTSD) {
1020 __imx_uart_rtsint(irq, dev_id);
1024 if (usr1 & USR1_AWAKE) {
1025 imx_uart_writel(sport, USR1_AWAKE, USR1);
1029 if (usr2 & USR2_ORE) {
1030 sport->port.icount.overrun++;
1031 imx_uart_writel(sport, USR2_ORE, USR2);
1035 spin_unlock(&sport->port.lock);
1041 * Return TIOCSER_TEMT when transmitter is not busy.
1043 static unsigned int imx_uart_tx_empty(struct uart_port *port)
1045 struct imx_port *sport = (struct imx_port *)port;
1048 ret = (imx_uart_readl(sport, USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
1050 /* If the TX DMA is working, return 0. */
1051 if (sport->dma_is_txing)
1057 /* called with port.lock taken and irqs off */
1058 static unsigned int imx_uart_get_mctrl(struct uart_port *port)
1060 struct imx_port *sport = (struct imx_port *)port;
1061 unsigned int ret = imx_uart_get_hwmctrl(sport);
1063 mctrl_gpio_get(sport->gpios, &ret);
1068 /* called with port.lock taken and irqs off */
1069 static void imx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1071 struct imx_port *sport = (struct imx_port *)port;
1074 if (!(port->rs485.flags & SER_RS485_ENABLED)) {
1078 * Turn off autoRTS if RTS is lowered and restore autoRTS
1079 * setting if RTS is raised.
1081 ucr2 = imx_uart_readl(sport, UCR2);
1082 ucr2 &= ~(UCR2_CTS | UCR2_CTSC);
1083 if (mctrl & TIOCM_RTS) {
1086 * UCR2_IRTS is unset if and only if the port is
1087 * configured for CRTSCTS, so we use inverted UCR2_IRTS
1088 * to get the state to restore to.
1090 if (!(ucr2 & UCR2_IRTS))
1093 imx_uart_writel(sport, ucr2, UCR2);
1096 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_DSR;
1097 if (!(mctrl & TIOCM_DTR))
1099 imx_uart_writel(sport, ucr3, UCR3);
1101 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)) & ~UTS_LOOP;
1102 if (mctrl & TIOCM_LOOP)
1104 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
1106 mctrl_gpio_set(sport->gpios, mctrl);
1110 * Interrupts always disabled.
1112 static void imx_uart_break_ctl(struct uart_port *port, int break_state)
1114 struct imx_port *sport = (struct imx_port *)port;
1115 unsigned long flags;
1118 spin_lock_irqsave(&sport->port.lock, flags);
1120 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_SNDBRK;
1122 if (break_state != 0)
1123 ucr1 |= UCR1_SNDBRK;
1125 imx_uart_writel(sport, ucr1, UCR1);
1127 spin_unlock_irqrestore(&sport->port.lock, flags);
1131 * This is our per-port timeout handler, for checking the
1132 * modem status signals.
1134 static void imx_uart_timeout(struct timer_list *t)
1136 struct imx_port *sport = from_timer(sport, t, timer);
1137 unsigned long flags;
1139 if (sport->port.state) {
1140 spin_lock_irqsave(&sport->port.lock, flags);
1141 imx_uart_mctrl_check(sport);
1142 spin_unlock_irqrestore(&sport->port.lock, flags);
1144 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
1149 * There are two kinds of RX DMA interrupts(such as in the MX6Q):
1150 * [1] the RX DMA buffer is full.
1151 * [2] the aging timer expires
1153 * Condition [2] is triggered when a character has been sitting in the FIFO
1154 * for at least 8 byte durations.
1156 static void imx_uart_dma_rx_callback(void *data)
1158 struct imx_port *sport = data;
1159 struct dma_chan *chan = sport->dma_chan_rx;
1160 struct scatterlist *sgl = &sport->rx_sgl;
1161 struct tty_port *port = &sport->port.state->port;
1162 struct dma_tx_state state;
1163 struct circ_buf *rx_ring = &sport->rx_ring;
1164 enum dma_status status;
1165 unsigned int w_bytes = 0;
1166 unsigned int r_bytes;
1167 unsigned int bd_size;
1169 status = dmaengine_tx_status(chan, sport->rx_cookie, &state);
1171 if (status == DMA_ERROR) {
1172 spin_lock(&sport->port.lock);
1173 imx_uart_clear_rx_errors(sport);
1174 spin_unlock(&sport->port.lock);
1179 * The state-residue variable represents the empty space
1180 * relative to the entire buffer. Taking this in consideration
1181 * the head is always calculated base on the buffer total
1182 * length - DMA transaction residue. The UART script from the
1183 * SDMA firmware will jump to the next buffer descriptor,
1184 * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4).
1185 * Taking this in consideration the tail is always at the
1186 * beginning of the buffer descriptor that contains the head.
1189 /* Calculate the head */
1190 rx_ring->head = sg_dma_len(sgl) - state.residue;
1192 /* Calculate the tail. */
1193 bd_size = sg_dma_len(sgl) / sport->rx_periods;
1194 rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size;
1196 if (rx_ring->head <= sg_dma_len(sgl) &&
1197 rx_ring->head > rx_ring->tail) {
1199 /* Move data from tail to head */
1200 r_bytes = rx_ring->head - rx_ring->tail;
1202 /* If we received something, check for 0xff flood */
1203 spin_lock(&sport->port.lock);
1204 imx_uart_check_flood(sport, imx_uart_readl(sport, USR2));
1205 spin_unlock(&sport->port.lock);
1207 if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
1209 /* CPU claims ownership of RX DMA buffer */
1210 dma_sync_sg_for_cpu(sport->port.dev, sgl, 1,
1213 w_bytes = tty_insert_flip_string(port,
1214 sport->rx_buf + rx_ring->tail, r_bytes);
1216 /* UART retrieves ownership of RX DMA buffer */
1217 dma_sync_sg_for_device(sport->port.dev, sgl, 1,
1220 if (w_bytes != r_bytes)
1221 sport->port.icount.buf_overrun++;
1223 sport->port.icount.rx += w_bytes;
1226 WARN_ON(rx_ring->head > sg_dma_len(sgl));
1227 WARN_ON(rx_ring->head <= rx_ring->tail);
1231 tty_flip_buffer_push(port);
1232 dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes);
1236 static int imx_uart_start_rx_dma(struct imx_port *sport)
1238 struct scatterlist *sgl = &sport->rx_sgl;
1239 struct dma_chan *chan = sport->dma_chan_rx;
1240 struct device *dev = sport->port.dev;
1241 struct dma_async_tx_descriptor *desc;
1244 sport->rx_ring.head = 0;
1245 sport->rx_ring.tail = 0;
1247 sg_init_one(sgl, sport->rx_buf, sport->rx_buf_size);
1248 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1250 dev_err(dev, "DMA mapping error for RX.\n");
1254 desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl),
1255 sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods,
1256 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
1259 dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1260 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
1263 desc->callback = imx_uart_dma_rx_callback;
1264 desc->callback_param = sport;
1266 dev_dbg(dev, "RX: prepare for the DMA.\n");
1267 sport->dma_is_rxing = 1;
1268 sport->rx_cookie = dmaengine_submit(desc);
1269 dma_async_issue_pending(chan);
1273 static void imx_uart_clear_rx_errors(struct imx_port *sport)
1275 struct tty_port *port = &sport->port.state->port;
1278 usr1 = imx_uart_readl(sport, USR1);
1279 usr2 = imx_uart_readl(sport, USR2);
1281 if (usr2 & USR2_BRCD) {
1282 sport->port.icount.brk++;
1283 imx_uart_writel(sport, USR2_BRCD, USR2);
1284 uart_handle_break(&sport->port);
1285 if (tty_insert_flip_char(port, 0, TTY_BREAK) == 0)
1286 sport->port.icount.buf_overrun++;
1287 tty_flip_buffer_push(port);
1289 if (usr1 & USR1_FRAMERR) {
1290 sport->port.icount.frame++;
1291 imx_uart_writel(sport, USR1_FRAMERR, USR1);
1292 } else if (usr1 & USR1_PARITYERR) {
1293 sport->port.icount.parity++;
1294 imx_uart_writel(sport, USR1_PARITYERR, USR1);
1298 if (usr2 & USR2_ORE) {
1299 sport->port.icount.overrun++;
1300 imx_uart_writel(sport, USR2_ORE, USR2);
1303 sport->idle_counter = 0;
1307 #define TXTL_DEFAULT 2 /* reset default */
1308 #define RXTL_DEFAULT 8 /* 8 characters or aging timer */
1309 #define TXTL_DMA 8 /* DMA burst setting */
1310 #define RXTL_DMA 9 /* DMA burst setting */
1312 static void imx_uart_setup_ufcr(struct imx_port *sport,
1313 unsigned char txwl, unsigned char rxwl)
1317 /* set receiver / transmitter trigger level */
1318 val = imx_uart_readl(sport, UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
1319 val |= txwl << UFCR_TXTL_SHF | rxwl;
1320 imx_uart_writel(sport, val, UFCR);
1323 static void imx_uart_dma_exit(struct imx_port *sport)
1325 if (sport->dma_chan_rx) {
1326 dmaengine_terminate_sync(sport->dma_chan_rx);
1327 dma_release_channel(sport->dma_chan_rx);
1328 sport->dma_chan_rx = NULL;
1329 sport->rx_cookie = -EINVAL;
1330 kfree(sport->rx_buf);
1331 sport->rx_buf = NULL;
1334 if (sport->dma_chan_tx) {
1335 dmaengine_terminate_sync(sport->dma_chan_tx);
1336 dma_release_channel(sport->dma_chan_tx);
1337 sport->dma_chan_tx = NULL;
1341 static int imx_uart_dma_init(struct imx_port *sport)
1343 struct dma_slave_config slave_config = {};
1344 struct device *dev = sport->port.dev;
1347 /* Prepare for RX : */
1348 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1349 if (!sport->dma_chan_rx) {
1350 dev_dbg(dev, "cannot get the DMA channel.\n");
1355 slave_config.direction = DMA_DEV_TO_MEM;
1356 slave_config.src_addr = sport->port.mapbase + URXD0;
1357 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1358 /* one byte less than the watermark level to enable the aging timer */
1359 slave_config.src_maxburst = RXTL_DMA - 1;
1360 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
1362 dev_err(dev, "error in RX dma configuration.\n");
1366 sport->rx_buf_size = sport->rx_period_length * sport->rx_periods;
1367 sport->rx_buf = kzalloc(sport->rx_buf_size, GFP_KERNEL);
1368 if (!sport->rx_buf) {
1372 sport->rx_ring.buf = sport->rx_buf;
1374 /* Prepare for TX : */
1375 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1376 if (!sport->dma_chan_tx) {
1377 dev_err(dev, "cannot get the TX DMA channel!\n");
1382 slave_config.direction = DMA_MEM_TO_DEV;
1383 slave_config.dst_addr = sport->port.mapbase + URTX0;
1384 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1385 slave_config.dst_maxburst = TXTL_DMA;
1386 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1388 dev_err(dev, "error in TX dma configuration.");
1394 imx_uart_dma_exit(sport);
1398 static void imx_uart_enable_dma(struct imx_port *sport)
1402 imx_uart_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
1405 ucr1 = imx_uart_readl(sport, UCR1);
1406 ucr1 |= UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN;
1407 imx_uart_writel(sport, ucr1, UCR1);
1409 sport->dma_is_enabled = 1;
1412 static void imx_uart_disable_dma(struct imx_port *sport)
1417 ucr1 = imx_uart_readl(sport, UCR1);
1418 ucr1 &= ~(UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN);
1419 imx_uart_writel(sport, ucr1, UCR1);
1421 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1423 sport->dma_is_enabled = 0;
1426 /* half the RX buffer size */
1429 static int imx_uart_startup(struct uart_port *port)
1431 struct imx_port *sport = (struct imx_port *)port;
1433 unsigned long flags;
1434 int dma_is_inited = 0;
1435 u32 ucr1, ucr2, ucr3, ucr4;
1437 retval = clk_prepare_enable(sport->clk_per);
1440 retval = clk_prepare_enable(sport->clk_ipg);
1442 clk_disable_unprepare(sport->clk_per);
1446 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1448 /* disable the DREN bit (Data Ready interrupt enable) before
1451 ucr4 = imx_uart_readl(sport, UCR4);
1453 /* set the trigger level for CTS */
1454 ucr4 &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
1455 ucr4 |= CTSTL << UCR4_CTSTL_SHF;
1457 imx_uart_writel(sport, ucr4 & ~UCR4_DREN, UCR4);
1459 /* Can we enable the DMA support? */
1460 if (!uart_console(port) && imx_uart_dma_init(sport) == 0)
1463 spin_lock_irqsave(&sport->port.lock, flags);
1465 /* Reset fifo's and state machines */
1466 imx_uart_soft_reset(sport);
1469 * Finally, clear and enable interrupts
1471 imx_uart_writel(sport, USR1_RTSD | USR1_DTRD, USR1);
1472 imx_uart_writel(sport, USR2_ORE, USR2);
1474 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_RRDYEN;
1475 ucr1 |= UCR1_UARTEN;
1476 if (sport->have_rtscts)
1477 ucr1 |= UCR1_RTSDEN;
1479 imx_uart_writel(sport, ucr1, UCR1);
1481 ucr4 = imx_uart_readl(sport, UCR4) & ~(UCR4_OREN | UCR4_INVR);
1484 if (sport->inverted_rx)
1486 imx_uart_writel(sport, ucr4, UCR4);
1488 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_INVT;
1490 * configure tx polarity before enabling tx
1492 if (sport->inverted_tx)
1495 if (!imx_uart_is_imx1(sport)) {
1496 ucr3 |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
1498 if (sport->dte_mode)
1499 /* disable broken interrupts */
1500 ucr3 &= ~(UCR3_RI | UCR3_DCD);
1502 imx_uart_writel(sport, ucr3, UCR3);
1504 ucr2 = imx_uart_readl(sport, UCR2) & ~UCR2_ATEN;
1505 ucr2 |= (UCR2_RXEN | UCR2_TXEN);
1506 if (!sport->have_rtscts)
1509 * make sure the edge sensitive RTS-irq is disabled,
1510 * we're using RTSD instead.
1512 if (!imx_uart_is_imx1(sport))
1513 ucr2 &= ~UCR2_RTSEN;
1514 imx_uart_writel(sport, ucr2, UCR2);
1517 * Enable modem status interrupts
1519 imx_uart_enable_ms(&sport->port);
1521 if (dma_is_inited) {
1522 imx_uart_enable_dma(sport);
1523 imx_uart_start_rx_dma(sport);
1525 ucr1 = imx_uart_readl(sport, UCR1);
1526 ucr1 |= UCR1_RRDYEN;
1527 imx_uart_writel(sport, ucr1, UCR1);
1529 ucr2 = imx_uart_readl(sport, UCR2);
1531 imx_uart_writel(sport, ucr2, UCR2);
1534 imx_uart_disable_loopback_rs485(sport);
1536 spin_unlock_irqrestore(&sport->port.lock, flags);
1541 static void imx_uart_shutdown(struct uart_port *port)
1543 struct imx_port *sport = (struct imx_port *)port;
1544 unsigned long flags;
1545 u32 ucr1, ucr2, ucr4, uts;
1547 if (sport->dma_is_enabled) {
1548 dmaengine_terminate_sync(sport->dma_chan_tx);
1549 if (sport->dma_is_txing) {
1550 dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
1551 sport->dma_tx_nents, DMA_TO_DEVICE);
1552 sport->dma_is_txing = 0;
1554 dmaengine_terminate_sync(sport->dma_chan_rx);
1555 if (sport->dma_is_rxing) {
1556 dma_unmap_sg(sport->port.dev, &sport->rx_sgl,
1557 1, DMA_FROM_DEVICE);
1558 sport->dma_is_rxing = 0;
1561 spin_lock_irqsave(&sport->port.lock, flags);
1562 imx_uart_stop_tx(port);
1563 imx_uart_stop_rx(port);
1564 imx_uart_disable_dma(sport);
1565 spin_unlock_irqrestore(&sport->port.lock, flags);
1566 imx_uart_dma_exit(sport);
1569 mctrl_gpio_disable_ms(sport->gpios);
1571 spin_lock_irqsave(&sport->port.lock, flags);
1572 ucr2 = imx_uart_readl(sport, UCR2);
1573 ucr2 &= ~(UCR2_TXEN | UCR2_ATEN);
1574 imx_uart_writel(sport, ucr2, UCR2);
1575 spin_unlock_irqrestore(&sport->port.lock, flags);
1580 del_timer_sync(&sport->timer);
1583 * Disable all interrupts, port and break condition.
1586 spin_lock_irqsave(&sport->port.lock, flags);
1588 ucr1 = imx_uart_readl(sport, UCR1);
1589 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_RXDMAEN |
1590 UCR1_ATDMAEN | UCR1_SNDBRK);
1591 /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
1592 if (port->rs485.flags & SER_RS485_ENABLED &&
1593 port->rs485.flags & SER_RS485_RTS_ON_SEND &&
1594 sport->have_rtscts && !sport->have_rtsgpio) {
1595 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
1597 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
1598 ucr1 |= UCR1_UARTEN;
1600 ucr1 &= ~UCR1_UARTEN;
1602 imx_uart_writel(sport, ucr1, UCR1);
1604 ucr4 = imx_uart_readl(sport, UCR4);
1606 imx_uart_writel(sport, ucr4, UCR4);
1608 spin_unlock_irqrestore(&sport->port.lock, flags);
1610 clk_disable_unprepare(sport->clk_per);
1611 clk_disable_unprepare(sport->clk_ipg);
1614 /* called with port.lock taken and irqs off */
1615 static void imx_uart_flush_buffer(struct uart_port *port)
1617 struct imx_port *sport = (struct imx_port *)port;
1618 struct scatterlist *sgl = &sport->tx_sgl[0];
1620 if (!sport->dma_chan_tx)
1623 sport->tx_bytes = 0;
1624 dmaengine_terminate_all(sport->dma_chan_tx);
1625 if (sport->dma_is_txing) {
1628 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
1630 ucr1 = imx_uart_readl(sport, UCR1);
1631 ucr1 &= ~UCR1_TXDMAEN;
1632 imx_uart_writel(sport, ucr1, UCR1);
1633 sport->dma_is_txing = 0;
1636 imx_uart_soft_reset(sport);
1641 imx_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1642 const struct ktermios *old)
1644 struct imx_port *sport = (struct imx_port *)port;
1645 unsigned long flags;
1646 u32 ucr2, old_ucr2, ufcr;
1647 unsigned int baud, quot;
1648 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1650 unsigned long num, denom, old_ubir, old_ubmr;
1654 * We only support CS7 and CS8.
1656 while ((termios->c_cflag & CSIZE) != CS7 &&
1657 (termios->c_cflag & CSIZE) != CS8) {
1658 termios->c_cflag &= ~CSIZE;
1659 termios->c_cflag |= old_csize;
1663 del_timer_sync(&sport->timer);
1666 * Ask the core to calculate the divisor for us.
1668 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1669 quot = uart_get_divisor(port, baud);
1671 spin_lock_irqsave(&sport->port.lock, flags);
1674 * Read current UCR2 and save it for future use, then clear all the bits
1675 * except those we will or may need to preserve.
1677 old_ucr2 = imx_uart_readl(sport, UCR2);
1678 ucr2 = old_ucr2 & (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN | UCR2_CTS);
1680 ucr2 |= UCR2_SRST | UCR2_IRTS;
1681 if ((termios->c_cflag & CSIZE) == CS8)
1684 if (!sport->have_rtscts)
1685 termios->c_cflag &= ~CRTSCTS;
1687 if (port->rs485.flags & SER_RS485_ENABLED) {
1689 * RTS is mandatory for rs485 operation, so keep
1690 * it under manual control and keep transmitter
1693 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1694 imx_uart_rts_active(sport, &ucr2);
1696 imx_uart_rts_inactive(sport, &ucr2);
1698 } else if (termios->c_cflag & CRTSCTS) {
1700 * Only let receiver control RTS output if we were not requested
1701 * to have RTS inactive (which then should take precedence).
1703 if (ucr2 & UCR2_CTS)
1707 if (termios->c_cflag & CRTSCTS)
1709 if (termios->c_cflag & CSTOPB)
1711 if (termios->c_cflag & PARENB) {
1713 if (termios->c_cflag & PARODD)
1717 sport->port.read_status_mask = 0;
1718 if (termios->c_iflag & INPCK)
1719 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
1720 if (termios->c_iflag & (BRKINT | PARMRK))
1721 sport->port.read_status_mask |= URXD_BRK;
1724 * Characters to ignore
1726 sport->port.ignore_status_mask = 0;
1727 if (termios->c_iflag & IGNPAR)
1728 sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
1729 if (termios->c_iflag & IGNBRK) {
1730 sport->port.ignore_status_mask |= URXD_BRK;
1732 * If we're ignoring parity and break indicators,
1733 * ignore overruns too (for real raw support).
1735 if (termios->c_iflag & IGNPAR)
1736 sport->port.ignore_status_mask |= URXD_OVRRUN;
1739 if ((termios->c_cflag & CREAD) == 0)
1740 sport->port.ignore_status_mask |= URXD_DUMMY_READ;
1743 * Update the per-port timeout.
1745 uart_update_timeout(port, termios->c_cflag, baud);
1747 /* custom-baudrate handling */
1748 div = sport->port.uartclk / (baud * 16);
1749 if (baud == 38400 && quot != div)
1750 baud = sport->port.uartclk / (quot * 16);
1752 div = sport->port.uartclk / (baud * 16);
1758 rational_best_approximation(16 * div * baud, sport->port.uartclk,
1759 1 << 16, 1 << 16, &num, &denom);
1761 tdiv64 = sport->port.uartclk;
1763 do_div(tdiv64, denom * 16 * div);
1764 tty_termios_encode_baud_rate(termios,
1765 (speed_t)tdiv64, (speed_t)tdiv64);
1770 ufcr = imx_uart_readl(sport, UFCR);
1771 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
1772 imx_uart_writel(sport, ufcr, UFCR);
1775 * Two registers below should always be written both and in this
1776 * particular order. One consequence is that we need to check if any of
1777 * them changes and then update both. We do need the check for change
1778 * as even writing the same values seem to "restart"
1779 * transmission/receiving logic in the hardware, that leads to data
1780 * breakage even when rate doesn't in fact change. E.g., user switches
1781 * RTS/CTS handshake and suddenly gets broken bytes.
1783 old_ubir = imx_uart_readl(sport, UBIR);
1784 old_ubmr = imx_uart_readl(sport, UBMR);
1785 if (old_ubir != num || old_ubmr != denom) {
1786 imx_uart_writel(sport, num, UBIR);
1787 imx_uart_writel(sport, denom, UBMR);
1790 if (!imx_uart_is_imx1(sport))
1791 imx_uart_writel(sport, sport->port.uartclk / div / 1000,
1794 imx_uart_writel(sport, ucr2, UCR2);
1796 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1797 imx_uart_enable_ms(&sport->port);
1799 spin_unlock_irqrestore(&sport->port.lock, flags);
1802 static const char *imx_uart_type(struct uart_port *port)
1804 return port->type == PORT_IMX ? "IMX" : NULL;
1808 * Configure/autoconfigure the port.
1810 static void imx_uart_config_port(struct uart_port *port, int flags)
1812 if (flags & UART_CONFIG_TYPE)
1813 port->type = PORT_IMX;
1817 * Verify the new serial_struct (for TIOCSSERIAL).
1818 * The only change we allow are to the flags and type, and
1819 * even then only between PORT_IMX and PORT_UNKNOWN
1822 imx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
1826 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1828 if (port->irq != ser->irq)
1830 if (ser->io_type != UPIO_MEM)
1832 if (port->uartclk / 16 != ser->baud_base)
1834 if (port->mapbase != (unsigned long)ser->iomem_base)
1836 if (port->iobase != ser->port)
1843 #if defined(CONFIG_CONSOLE_POLL)
1845 static int imx_uart_poll_init(struct uart_port *port)
1847 struct imx_port *sport = (struct imx_port *)port;
1848 unsigned long flags;
1852 retval = clk_prepare_enable(sport->clk_ipg);
1855 retval = clk_prepare_enable(sport->clk_per);
1857 clk_disable_unprepare(sport->clk_ipg);
1859 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1861 spin_lock_irqsave(&sport->port.lock, flags);
1864 * Be careful about the order of enabling bits here. First enable the
1865 * receiver (UARTEN + RXEN) and only then the corresponding irqs.
1866 * This prevents that a character that already sits in the RX fifo is
1867 * triggering an irq but the try to fetch it from there results in an
1868 * exception because UARTEN or RXEN is still off.
1870 ucr1 = imx_uart_readl(sport, UCR1);
1871 ucr2 = imx_uart_readl(sport, UCR2);
1873 if (imx_uart_is_imx1(sport))
1874 ucr1 |= IMX1_UCR1_UARTCLKEN;
1876 ucr1 |= UCR1_UARTEN;
1877 ucr1 &= ~(UCR1_TRDYEN | UCR1_RTSDEN | UCR1_RRDYEN);
1879 ucr2 |= UCR2_RXEN | UCR2_TXEN;
1882 imx_uart_writel(sport, ucr1, UCR1);
1883 imx_uart_writel(sport, ucr2, UCR2);
1885 /* now enable irqs */
1886 imx_uart_writel(sport, ucr1 | UCR1_RRDYEN, UCR1);
1887 imx_uart_writel(sport, ucr2 | UCR2_ATEN, UCR2);
1889 spin_unlock_irqrestore(&sport->port.lock, flags);
1894 static int imx_uart_poll_get_char(struct uart_port *port)
1896 struct imx_port *sport = (struct imx_port *)port;
1897 if (!(imx_uart_readl(sport, USR2) & USR2_RDR))
1898 return NO_POLL_CHAR;
1900 return imx_uart_readl(sport, URXD0) & URXD_RX_DATA;
1903 static void imx_uart_poll_put_char(struct uart_port *port, unsigned char c)
1905 struct imx_port *sport = (struct imx_port *)port;
1906 unsigned int status;
1910 status = imx_uart_readl(sport, USR1);
1911 } while (~status & USR1_TRDY);
1914 imx_uart_writel(sport, c, URTX0);
1918 status = imx_uart_readl(sport, USR2);
1919 } while (~status & USR2_TXDC);
1923 /* called with port.lock taken and irqs off or from .probe without locking */
1924 static int imx_uart_rs485_config(struct uart_port *port, struct ktermios *termios,
1925 struct serial_rs485 *rs485conf)
1927 struct imx_port *sport = (struct imx_port *)port;
1930 if (rs485conf->flags & SER_RS485_ENABLED) {
1931 /* Enable receiver if low-active RTS signal is requested */
1932 if (sport->have_rtscts && !sport->have_rtsgpio &&
1933 !(rs485conf->flags & SER_RS485_RTS_ON_SEND))
1934 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1936 /* disable transmitter */
1937 ucr2 = imx_uart_readl(sport, UCR2);
1938 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
1939 imx_uart_rts_active(sport, &ucr2);
1941 imx_uart_rts_inactive(sport, &ucr2);
1942 imx_uart_writel(sport, ucr2, UCR2);
1945 /* Make sure Rx is enabled in case Tx is active with Rx disabled */
1946 if (!(rs485conf->flags & SER_RS485_ENABLED) ||
1947 rs485conf->flags & SER_RS485_RX_DURING_TX)
1948 imx_uart_start_rx(port);
1953 static const struct uart_ops imx_uart_pops = {
1954 .tx_empty = imx_uart_tx_empty,
1955 .set_mctrl = imx_uart_set_mctrl,
1956 .get_mctrl = imx_uart_get_mctrl,
1957 .stop_tx = imx_uart_stop_tx,
1958 .start_tx = imx_uart_start_tx,
1959 .stop_rx = imx_uart_stop_rx,
1960 .enable_ms = imx_uart_enable_ms,
1961 .break_ctl = imx_uart_break_ctl,
1962 .startup = imx_uart_startup,
1963 .shutdown = imx_uart_shutdown,
1964 .flush_buffer = imx_uart_flush_buffer,
1965 .set_termios = imx_uart_set_termios,
1966 .type = imx_uart_type,
1967 .config_port = imx_uart_config_port,
1968 .verify_port = imx_uart_verify_port,
1969 #if defined(CONFIG_CONSOLE_POLL)
1970 .poll_init = imx_uart_poll_init,
1971 .poll_get_char = imx_uart_poll_get_char,
1972 .poll_put_char = imx_uart_poll_put_char,
1976 static struct imx_port *imx_uart_ports[UART_NR];
1978 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
1979 static void imx_uart_console_putchar(struct uart_port *port, unsigned char ch)
1981 struct imx_port *sport = (struct imx_port *)port;
1983 while (imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)
1986 imx_uart_writel(sport, ch, URTX0);
1990 * Interrupts are disabled on entering
1993 imx_uart_console_write(struct console *co, const char *s, unsigned int count)
1995 struct imx_port *sport = imx_uart_ports[co->index];
1996 struct imx_port_ucrs old_ucr;
1997 unsigned long flags;
2001 if (sport->port.sysrq)
2003 else if (oops_in_progress)
2004 locked = spin_trylock_irqsave(&sport->port.lock, flags);
2006 spin_lock_irqsave(&sport->port.lock, flags);
2009 * First, save UCR1/2/3 and then disable interrupts
2011 imx_uart_ucrs_save(sport, &old_ucr);
2012 ucr1 = old_ucr.ucr1;
2014 if (imx_uart_is_imx1(sport))
2015 ucr1 |= IMX1_UCR1_UARTCLKEN;
2016 ucr1 |= UCR1_UARTEN;
2017 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN);
2019 imx_uart_writel(sport, ucr1, UCR1);
2021 imx_uart_writel(sport, old_ucr.ucr2 | UCR2_TXEN, UCR2);
2023 uart_console_write(&sport->port, s, count, imx_uart_console_putchar);
2026 * Finally, wait for transmitter to become empty
2027 * and restore UCR1/2/3
2029 while (!(imx_uart_readl(sport, USR2) & USR2_TXDC));
2031 imx_uart_ucrs_restore(sport, &old_ucr);
2034 spin_unlock_irqrestore(&sport->port.lock, flags);
2038 * If the port was already initialised (eg, by a boot loader),
2039 * try to determine the current setup.
2042 imx_uart_console_get_options(struct imx_port *sport, int *baud,
2043 int *parity, int *bits)
2046 if (imx_uart_readl(sport, UCR1) & UCR1_UARTEN) {
2047 /* ok, the port was enabled */
2048 unsigned int ucr2, ubir, ubmr, uartclk;
2049 unsigned int baud_raw;
2050 unsigned int ucfr_rfdiv;
2052 ucr2 = imx_uart_readl(sport, UCR2);
2055 if (ucr2 & UCR2_PREN) {
2056 if (ucr2 & UCR2_PROE)
2067 ubir = imx_uart_readl(sport, UBIR) & 0xffff;
2068 ubmr = imx_uart_readl(sport, UBMR) & 0xffff;
2070 ucfr_rfdiv = (imx_uart_readl(sport, UFCR) & UFCR_RFDIV) >> 7;
2071 if (ucfr_rfdiv == 6)
2074 ucfr_rfdiv = 6 - ucfr_rfdiv;
2076 uartclk = clk_get_rate(sport->clk_per);
2077 uartclk /= ucfr_rfdiv;
2080 * The next code provides exact computation of
2081 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
2082 * without need of float support or long long division,
2083 * which would be required to prevent 32bit arithmetic overflow
2085 unsigned int mul = ubir + 1;
2086 unsigned int div = 16 * (ubmr + 1);
2087 unsigned int rem = uartclk % div;
2089 baud_raw = (uartclk / div) * mul;
2090 baud_raw += (rem * mul + div / 2) / div;
2091 *baud = (baud_raw + 50) / 100 * 100;
2094 if (*baud != baud_raw)
2095 dev_info(sport->port.dev, "Console IMX rounded baud rate from %d to %d\n",
2101 imx_uart_console_setup(struct console *co, char *options)
2103 struct imx_port *sport;
2111 * Check whether an invalid uart number has been specified, and
2112 * if so, search for the first available port that does have
2115 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_uart_ports))
2117 sport = imx_uart_ports[co->index];
2121 /* For setting the registers, we only need to enable the ipg clock. */
2122 retval = clk_prepare_enable(sport->clk_ipg);
2127 uart_parse_options(options, &baud, &parity, &bits, &flow);
2129 imx_uart_console_get_options(sport, &baud, &parity, &bits);
2131 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
2133 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
2136 clk_disable_unprepare(sport->clk_ipg);
2140 retval = clk_prepare_enable(sport->clk_per);
2142 clk_disable_unprepare(sport->clk_ipg);
2149 imx_uart_console_exit(struct console *co)
2151 struct imx_port *sport = imx_uart_ports[co->index];
2153 clk_disable_unprepare(sport->clk_per);
2154 clk_disable_unprepare(sport->clk_ipg);
2159 static struct uart_driver imx_uart_uart_driver;
2160 static struct console imx_uart_console = {
2162 .write = imx_uart_console_write,
2163 .device = uart_console_device,
2164 .setup = imx_uart_console_setup,
2165 .exit = imx_uart_console_exit,
2166 .flags = CON_PRINTBUFFER,
2168 .data = &imx_uart_uart_driver,
2171 #define IMX_CONSOLE &imx_uart_console
2174 #define IMX_CONSOLE NULL
2177 static struct uart_driver imx_uart_uart_driver = {
2178 .owner = THIS_MODULE,
2179 .driver_name = DRIVER_NAME,
2180 .dev_name = DEV_NAME,
2181 .major = SERIAL_IMX_MAJOR,
2182 .minor = MINOR_START,
2183 .nr = ARRAY_SIZE(imx_uart_ports),
2184 .cons = IMX_CONSOLE,
2187 static enum hrtimer_restart imx_trigger_start_tx(struct hrtimer *t)
2189 struct imx_port *sport = container_of(t, struct imx_port, trigger_start_tx);
2190 unsigned long flags;
2192 spin_lock_irqsave(&sport->port.lock, flags);
2193 if (sport->tx_state == WAIT_AFTER_RTS)
2194 imx_uart_start_tx(&sport->port);
2195 spin_unlock_irqrestore(&sport->port.lock, flags);
2197 return HRTIMER_NORESTART;
2200 static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t)
2202 struct imx_port *sport = container_of(t, struct imx_port, trigger_stop_tx);
2203 unsigned long flags;
2205 spin_lock_irqsave(&sport->port.lock, flags);
2206 if (sport->tx_state == WAIT_AFTER_SEND)
2207 imx_uart_stop_tx(&sport->port);
2208 spin_unlock_irqrestore(&sport->port.lock, flags);
2210 return HRTIMER_NORESTART;
2213 static const struct serial_rs485 imx_rs485_supported = {
2214 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
2215 SER_RS485_RX_DURING_TX,
2216 .delay_rts_before_send = 1,
2217 .delay_rts_after_send = 1,
2220 /* Default RX DMA buffer configuration */
2221 #define RX_DMA_PERIODS 16
2222 #define RX_DMA_PERIOD_LEN (PAGE_SIZE / 4)
2224 static int imx_uart_probe(struct platform_device *pdev)
2226 struct device_node *np = pdev->dev.of_node;
2227 struct imx_port *sport;
2229 u32 dma_buf_conf[2];
2231 u32 ucr1, ucr2, uts;
2232 struct resource *res;
2233 int txirq, rxirq, rtsirq;
2235 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2239 sport->devdata = of_device_get_match_data(&pdev->dev);
2241 ret = of_alias_get_id(np, "serial");
2243 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2246 sport->port.line = ret;
2248 sport->have_rtscts = of_property_read_bool(np, "uart-has-rtscts") ||
2249 of_property_read_bool(np, "fsl,uart-has-rtscts"); /* deprecated */
2251 sport->dte_mode = of_property_read_bool(np, "fsl,dte-mode");
2253 sport->have_rtsgpio = of_property_present(np, "rts-gpios");
2255 sport->inverted_tx = of_property_read_bool(np, "fsl,inverted-tx");
2257 sport->inverted_rx = of_property_read_bool(np, "fsl,inverted-rx");
2259 if (!of_property_read_u32_array(np, "fsl,dma-info", dma_buf_conf, 2)) {
2260 sport->rx_period_length = dma_buf_conf[0];
2261 sport->rx_periods = dma_buf_conf[1];
2263 sport->rx_period_length = RX_DMA_PERIOD_LEN;
2264 sport->rx_periods = RX_DMA_PERIODS;
2267 if (sport->port.line >= ARRAY_SIZE(imx_uart_ports)) {
2268 dev_err(&pdev->dev, "serial%d out of range\n",
2273 base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2275 return PTR_ERR(base);
2277 rxirq = platform_get_irq(pdev, 0);
2280 txirq = platform_get_irq_optional(pdev, 1);
2281 rtsirq = platform_get_irq_optional(pdev, 2);
2283 sport->port.dev = &pdev->dev;
2284 sport->port.mapbase = res->start;
2285 sport->port.membase = base;
2286 sport->port.type = PORT_IMX;
2287 sport->port.iotype = UPIO_MEM;
2288 sport->port.irq = rxirq;
2289 sport->port.fifosize = 32;
2290 sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE);
2291 sport->port.ops = &imx_uart_pops;
2292 sport->port.rs485_config = imx_uart_rs485_config;
2293 /* RTS is required to control the RS485 transmitter */
2294 if (sport->have_rtscts || sport->have_rtsgpio)
2295 sport->port.rs485_supported = imx_rs485_supported;
2296 sport->port.flags = UPF_BOOT_AUTOCONF;
2297 timer_setup(&sport->timer, imx_uart_timeout, 0);
2299 sport->gpios = mctrl_gpio_init(&sport->port, 0);
2300 if (IS_ERR(sport->gpios))
2301 return PTR_ERR(sport->gpios);
2303 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2304 if (IS_ERR(sport->clk_ipg)) {
2305 ret = PTR_ERR(sport->clk_ipg);
2306 dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
2310 sport->clk_per = devm_clk_get(&pdev->dev, "per");
2311 if (IS_ERR(sport->clk_per)) {
2312 ret = PTR_ERR(sport->clk_per);
2313 dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
2317 sport->port.uartclk = clk_get_rate(sport->clk_per);
2319 /* For register access, we only need to enable the ipg clock. */
2320 ret = clk_prepare_enable(sport->clk_ipg);
2322 dev_err(&pdev->dev, "failed to enable ipg clk: %d\n", ret);
2326 ret = uart_get_rs485_mode(&sport->port);
2331 * If using the i.MX UART RTS/CTS control then the RTS (CTS_B)
2332 * signal cannot be set low during transmission in case the
2333 * receiver is off (limitation of the i.MX UART IP).
2335 if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2336 sport->have_rtscts && !sport->have_rtsgpio &&
2337 (!(sport->port.rs485.flags & SER_RS485_RTS_ON_SEND) &&
2338 !(sport->port.rs485.flags & SER_RS485_RX_DURING_TX)))
2340 "low-active RTS not possible when receiver is off, enabling receiver\n");
2342 /* Disable interrupts before requesting them */
2343 ucr1 = imx_uart_readl(sport, UCR1);
2344 ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN);
2345 imx_uart_writel(sport, ucr1, UCR1);
2347 /* Disable Ageing Timer interrupt */
2348 ucr2 = imx_uart_readl(sport, UCR2);
2350 imx_uart_writel(sport, ucr2, UCR2);
2353 * In case RS485 is enabled without GPIO RTS control, the UART IP
2354 * is used to control CTS signal. Keep both the UART and Receiver
2355 * enabled, otherwise the UART IP pulls CTS signal always HIGH no
2356 * matter how the UCR2 CTSC and CTS bits are set. To prevent any
2357 * data from being fed into the RX FIFO, enable loopback mode in
2358 * UTS register, which disconnects the RX path from external RXD
2359 * pin and connects it to the Transceiver, which is disabled, so
2360 * no data can be fed to the RX FIFO that way.
2362 if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2363 sport->have_rtscts && !sport->have_rtsgpio) {
2364 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
2366 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
2368 ucr1 = imx_uart_readl(sport, UCR1);
2369 ucr1 |= UCR1_UARTEN;
2370 imx_uart_writel(sport, ucr1, UCR1);
2372 ucr2 = imx_uart_readl(sport, UCR2);
2374 imx_uart_writel(sport, ucr2, UCR2);
2377 if (!imx_uart_is_imx1(sport) && sport->dte_mode) {
2379 * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI
2380 * and influences if UCR3_RI and UCR3_DCD changes the level of RI
2381 * and DCD (when they are outputs) or enables the respective
2382 * irqs. So set this bit early, i.e. before requesting irqs.
2384 u32 ufcr = imx_uart_readl(sport, UFCR);
2385 if (!(ufcr & UFCR_DCEDTE))
2386 imx_uart_writel(sport, ufcr | UFCR_DCEDTE, UFCR);
2389 * Disable UCR3_RI and UCR3_DCD irqs. They are also not
2390 * enabled later because they cannot be cleared
2391 * (confirmed on i.MX25) which makes them unusable.
2393 imx_uart_writel(sport,
2394 IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR,
2398 u32 ucr3 = UCR3_DSR;
2399 u32 ufcr = imx_uart_readl(sport, UFCR);
2400 if (ufcr & UFCR_DCEDTE)
2401 imx_uart_writel(sport, ufcr & ~UFCR_DCEDTE, UFCR);
2403 if (!imx_uart_is_imx1(sport))
2404 ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
2405 imx_uart_writel(sport, ucr3, UCR3);
2408 hrtimer_init(&sport->trigger_start_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2409 hrtimer_init(&sport->trigger_stop_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2410 sport->trigger_start_tx.function = imx_trigger_start_tx;
2411 sport->trigger_stop_tx.function = imx_trigger_stop_tx;
2414 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
2415 * chips only have one interrupt.
2418 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_rxint, 0,
2419 dev_name(&pdev->dev), sport);
2421 dev_err(&pdev->dev, "failed to request rx irq: %d\n",
2426 ret = devm_request_irq(&pdev->dev, txirq, imx_uart_txint, 0,
2427 dev_name(&pdev->dev), sport);
2429 dev_err(&pdev->dev, "failed to request tx irq: %d\n",
2434 ret = devm_request_irq(&pdev->dev, rtsirq, imx_uart_rtsint, 0,
2435 dev_name(&pdev->dev), sport);
2437 dev_err(&pdev->dev, "failed to request rts irq: %d\n",
2442 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_int, 0,
2443 dev_name(&pdev->dev), sport);
2445 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2450 imx_uart_ports[sport->port.line] = sport;
2452 platform_set_drvdata(pdev, sport);
2454 ret = uart_add_one_port(&imx_uart_uart_driver, &sport->port);
2457 clk_disable_unprepare(sport->clk_ipg);
2462 static int imx_uart_remove(struct platform_device *pdev)
2464 struct imx_port *sport = platform_get_drvdata(pdev);
2466 uart_remove_one_port(&imx_uart_uart_driver, &sport->port);
2471 static void imx_uart_restore_context(struct imx_port *sport)
2473 unsigned long flags;
2475 spin_lock_irqsave(&sport->port.lock, flags);
2476 if (!sport->context_saved) {
2477 spin_unlock_irqrestore(&sport->port.lock, flags);
2481 imx_uart_writel(sport, sport->saved_reg[4], UFCR);
2482 imx_uart_writel(sport, sport->saved_reg[5], UESC);
2483 imx_uart_writel(sport, sport->saved_reg[6], UTIM);
2484 imx_uart_writel(sport, sport->saved_reg[7], UBIR);
2485 imx_uart_writel(sport, sport->saved_reg[8], UBMR);
2486 imx_uart_writel(sport, sport->saved_reg[9], IMX21_UTS);
2487 imx_uart_writel(sport, sport->saved_reg[0], UCR1);
2488 imx_uart_writel(sport, sport->saved_reg[1] | UCR2_SRST, UCR2);
2489 imx_uart_writel(sport, sport->saved_reg[2], UCR3);
2490 imx_uart_writel(sport, sport->saved_reg[3], UCR4);
2491 sport->context_saved = false;
2492 spin_unlock_irqrestore(&sport->port.lock, flags);
2495 static void imx_uart_save_context(struct imx_port *sport)
2497 unsigned long flags;
2499 /* Save necessary regs */
2500 spin_lock_irqsave(&sport->port.lock, flags);
2501 sport->saved_reg[0] = imx_uart_readl(sport, UCR1);
2502 sport->saved_reg[1] = imx_uart_readl(sport, UCR2);
2503 sport->saved_reg[2] = imx_uart_readl(sport, UCR3);
2504 sport->saved_reg[3] = imx_uart_readl(sport, UCR4);
2505 sport->saved_reg[4] = imx_uart_readl(sport, UFCR);
2506 sport->saved_reg[5] = imx_uart_readl(sport, UESC);
2507 sport->saved_reg[6] = imx_uart_readl(sport, UTIM);
2508 sport->saved_reg[7] = imx_uart_readl(sport, UBIR);
2509 sport->saved_reg[8] = imx_uart_readl(sport, UBMR);
2510 sport->saved_reg[9] = imx_uart_readl(sport, IMX21_UTS);
2511 sport->context_saved = true;
2512 spin_unlock_irqrestore(&sport->port.lock, flags);
2515 static void imx_uart_enable_wakeup(struct imx_port *sport, bool on)
2519 ucr3 = imx_uart_readl(sport, UCR3);
2521 imx_uart_writel(sport, USR1_AWAKE, USR1);
2522 ucr3 |= UCR3_AWAKEN;
2524 ucr3 &= ~UCR3_AWAKEN;
2526 imx_uart_writel(sport, ucr3, UCR3);
2528 if (sport->have_rtscts) {
2529 u32 ucr1 = imx_uart_readl(sport, UCR1);
2531 imx_uart_writel(sport, USR1_RTSD, USR1);
2532 ucr1 |= UCR1_RTSDEN;
2534 ucr1 &= ~UCR1_RTSDEN;
2536 imx_uart_writel(sport, ucr1, UCR1);
2540 static int imx_uart_suspend_noirq(struct device *dev)
2542 struct imx_port *sport = dev_get_drvdata(dev);
2544 imx_uart_save_context(sport);
2546 clk_disable(sport->clk_ipg);
2548 pinctrl_pm_select_sleep_state(dev);
2553 static int imx_uart_resume_noirq(struct device *dev)
2555 struct imx_port *sport = dev_get_drvdata(dev);
2558 pinctrl_pm_select_default_state(dev);
2560 ret = clk_enable(sport->clk_ipg);
2564 imx_uart_restore_context(sport);
2569 static int imx_uart_suspend(struct device *dev)
2571 struct imx_port *sport = dev_get_drvdata(dev);
2574 uart_suspend_port(&imx_uart_uart_driver, &sport->port);
2575 disable_irq(sport->port.irq);
2577 ret = clk_prepare_enable(sport->clk_ipg);
2581 /* enable wakeup from i.MX UART */
2582 imx_uart_enable_wakeup(sport, true);
2587 static int imx_uart_resume(struct device *dev)
2589 struct imx_port *sport = dev_get_drvdata(dev);
2591 /* disable wakeup from i.MX UART */
2592 imx_uart_enable_wakeup(sport, false);
2594 uart_resume_port(&imx_uart_uart_driver, &sport->port);
2595 enable_irq(sport->port.irq);
2597 clk_disable_unprepare(sport->clk_ipg);
2602 static int imx_uart_freeze(struct device *dev)
2604 struct imx_port *sport = dev_get_drvdata(dev);
2606 uart_suspend_port(&imx_uart_uart_driver, &sport->port);
2608 return clk_prepare_enable(sport->clk_ipg);
2611 static int imx_uart_thaw(struct device *dev)
2613 struct imx_port *sport = dev_get_drvdata(dev);
2615 uart_resume_port(&imx_uart_uart_driver, &sport->port);
2617 clk_disable_unprepare(sport->clk_ipg);
2622 static const struct dev_pm_ops imx_uart_pm_ops = {
2623 .suspend_noirq = imx_uart_suspend_noirq,
2624 .resume_noirq = imx_uart_resume_noirq,
2625 .freeze_noirq = imx_uart_suspend_noirq,
2626 .thaw_noirq = imx_uart_resume_noirq,
2627 .restore_noirq = imx_uart_resume_noirq,
2628 .suspend = imx_uart_suspend,
2629 .resume = imx_uart_resume,
2630 .freeze = imx_uart_freeze,
2631 .thaw = imx_uart_thaw,
2632 .restore = imx_uart_thaw,
2635 static struct platform_driver imx_uart_platform_driver = {
2636 .probe = imx_uart_probe,
2637 .remove = imx_uart_remove,
2641 .of_match_table = imx_uart_dt_ids,
2642 .pm = &imx_uart_pm_ops,
2646 static int __init imx_uart_init(void)
2648 int ret = uart_register_driver(&imx_uart_uart_driver);
2653 ret = platform_driver_register(&imx_uart_platform_driver);
2655 uart_unregister_driver(&imx_uart_uart_driver);
2660 static void __exit imx_uart_exit(void)
2662 platform_driver_unregister(&imx_uart_platform_driver);
2663 uart_unregister_driver(&imx_uart_uart_driver);
2666 module_init(imx_uart_init);
2667 module_exit(imx_uart_exit);
2669 MODULE_AUTHOR("Sascha Hauer");
2670 MODULE_DESCRIPTION("IMX generic serial port driver");
2671 MODULE_LICENSE("GPL");
2672 MODULE_ALIAS("platform:imx-uart");