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>
28 #include <linux/of_device.h>
30 #include <linux/dma-mapping.h>
33 #include <linux/platform_data/dma-imx.h>
35 #include "serial_mctrl_gpio.h"
37 /* Register definitions */
38 #define URXD0 0x0 /* Receiver Register */
39 #define URTX0 0x40 /* Transmitter Register */
40 #define UCR1 0x80 /* Control Register 1 */
41 #define UCR2 0x84 /* Control Register 2 */
42 #define UCR3 0x88 /* Control Register 3 */
43 #define UCR4 0x8c /* Control Register 4 */
44 #define UFCR 0x90 /* FIFO Control Register */
45 #define USR1 0x94 /* Status Register 1 */
46 #define USR2 0x98 /* Status Register 2 */
47 #define UESC 0x9c /* Escape Character Register */
48 #define UTIM 0xa0 /* Escape Timer Register */
49 #define UBIR 0xa4 /* BRM Incremental Register */
50 #define UBMR 0xa8 /* BRM Modulator Register */
51 #define UBRC 0xac /* Baud Rate Count Register */
52 #define IMX21_ONEMS 0xb0 /* One Millisecond register */
53 #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
54 #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
56 /* UART Control Register Bit Fields.*/
57 #define URXD_DUMMY_READ (1<<16)
58 #define URXD_CHARRDY (1<<15)
59 #define URXD_ERR (1<<14)
60 #define URXD_OVRRUN (1<<13)
61 #define URXD_FRMERR (1<<12)
62 #define URXD_BRK (1<<11)
63 #define URXD_PRERR (1<<10)
64 #define URXD_RX_DATA (0xFF<<0)
65 #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
66 #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
67 #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
68 #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
69 #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
70 #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
71 #define UCR1_RXDMAEN (1<<8) /* Recv ready DMA enable */
72 #define UCR1_IREN (1<<7) /* Infrared interface enable */
73 #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
74 #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
75 #define UCR1_SNDBRK (1<<4) /* Send break */
76 #define UCR1_TXDMAEN (1<<3) /* Transmitter ready DMA enable */
77 #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
78 #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
79 #define UCR1_DOZE (1<<1) /* Doze */
80 #define UCR1_UARTEN (1<<0) /* UART enabled */
81 #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
82 #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
83 #define UCR2_CTSC (1<<13) /* CTS pin control */
84 #define UCR2_CTS (1<<12) /* Clear to send */
85 #define UCR2_ESCEN (1<<11) /* Escape enable */
86 #define UCR2_PREN (1<<8) /* Parity enable */
87 #define UCR2_PROE (1<<7) /* Parity odd/even */
88 #define UCR2_STPB (1<<6) /* Stop */
89 #define UCR2_WS (1<<5) /* Word size */
90 #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
91 #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
92 #define UCR2_TXEN (1<<2) /* Transmitter enabled */
93 #define UCR2_RXEN (1<<1) /* Receiver enabled */
94 #define UCR2_SRST (1<<0) /* SW reset */
95 #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
96 #define UCR3_PARERREN (1<<12) /* Parity enable */
97 #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
98 #define UCR3_DSR (1<<10) /* Data set ready */
99 #define UCR3_DCD (1<<9) /* Data carrier detect */
100 #define UCR3_RI (1<<8) /* Ring indicator */
101 #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
102 #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
103 #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
104 #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
105 #define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */
106 #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
107 #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
108 #define UCR3_BPEN (1<<0) /* Preset registers enable */
109 #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
110 #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
111 #define UCR4_INVR (1<<9) /* Inverted infrared reception */
112 #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
113 #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
114 #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
115 #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
116 #define UCR4_IRSC (1<<5) /* IR special case */
117 #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
118 #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
119 #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
120 #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
121 #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
122 #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
123 #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
124 #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
125 #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
126 #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
127 #define USR1_RTSS (1<<14) /* RTS pin status */
128 #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
129 #define USR1_RTSD (1<<12) /* RTS delta */
130 #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
131 #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
132 #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
133 #define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */
134 #define USR1_DTRD (1<<7) /* DTR Delta */
135 #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
136 #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
137 #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
138 #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
139 #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
140 #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
141 #define USR2_IDLE (1<<12) /* Idle condition */
142 #define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */
143 #define USR2_RIIN (1<<9) /* Ring Indicator Input */
144 #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
145 #define USR2_WAKE (1<<7) /* Wake */
146 #define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */
147 #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
148 #define USR2_TXDC (1<<3) /* Transmitter complete */
149 #define USR2_BRCD (1<<2) /* Break condition */
150 #define USR2_ORE (1<<1) /* Overrun error */
151 #define USR2_RDR (1<<0) /* Recv data ready */
152 #define UTS_FRCPERR (1<<13) /* Force parity error */
153 #define UTS_LOOP (1<<12) /* Loop tx and rx */
154 #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
155 #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
156 #define UTS_TXFULL (1<<4) /* TxFIFO full */
157 #define UTS_RXFULL (1<<3) /* RxFIFO full */
158 #define UTS_SOFTRST (1<<0) /* Software reset */
160 /* We've been assigned a range on the "Low-density serial ports" major */
161 #define SERIAL_IMX_MAJOR 207
162 #define MINOR_START 16
163 #define DEV_NAME "ttymxc"
166 * This determines how often we check the modem status signals
167 * for any change. They generally aren't connected to an IRQ
168 * so we have to poll them. We also check immediately before
169 * filling the TX fifo incase CTS has been dropped.
171 #define MCTRL_TIMEOUT (250*HZ/1000)
173 #define DRIVER_NAME "IMX-uart"
177 /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
185 /* device type dependent stuff */
186 struct imx_uart_data {
188 enum imx_uart_type devtype;
199 struct uart_port port;
200 struct timer_list timer;
201 unsigned int old_status;
202 unsigned int have_rtscts:1;
203 unsigned int have_rtsgpio:1;
204 unsigned int dte_mode:1;
205 unsigned int inverted_tx:1;
206 unsigned int inverted_rx:1;
209 const struct imx_uart_data *devdata;
211 struct mctrl_gpios *gpios;
213 /* shadow registers */
221 unsigned int dma_is_enabled:1;
222 unsigned int dma_is_rxing:1;
223 unsigned int dma_is_txing:1;
224 struct dma_chan *dma_chan_rx, *dma_chan_tx;
225 struct scatterlist rx_sgl, tx_sgl[2];
227 struct circ_buf rx_ring;
228 unsigned int rx_buf_size;
229 unsigned int rx_period_length;
230 unsigned int rx_periods;
231 dma_cookie_t rx_cookie;
232 unsigned int tx_bytes;
233 unsigned int dma_tx_nents;
234 unsigned int saved_reg[10];
237 enum imx_tx_state tx_state;
238 struct hrtimer trigger_start_tx;
239 struct hrtimer trigger_stop_tx;
242 struct imx_port_ucrs {
248 static struct imx_uart_data imx_uart_devdata[] = {
251 .devtype = IMX1_UART,
254 .uts_reg = IMX21_UTS,
255 .devtype = IMX21_UART,
258 .uts_reg = IMX21_UTS,
259 .devtype = IMX53_UART,
262 .uts_reg = IMX21_UTS,
263 .devtype = IMX6Q_UART,
267 static const struct of_device_id imx_uart_dt_ids[] = {
268 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
269 { .compatible = "fsl,imx53-uart", .data = &imx_uart_devdata[IMX53_UART], },
270 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
271 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
274 MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
276 static void imx_uart_writel(struct imx_port *sport, u32 val, u32 offset)
297 writel(val, sport->port.membase + offset);
300 static u32 imx_uart_readl(struct imx_port *sport, u32 offset)
308 * UCR2_SRST is the only bit in the cached registers that might
309 * differ from the value that was last written. As it only
310 * automatically becomes one after being cleared, reread
313 if (!(sport->ucr2 & UCR2_SRST))
314 sport->ucr2 = readl(sport->port.membase + offset);
327 return readl(sport->port.membase + offset);
331 static inline unsigned imx_uart_uts_reg(struct imx_port *sport)
333 return sport->devdata->uts_reg;
336 static inline int imx_uart_is_imx1(struct imx_port *sport)
338 return sport->devdata->devtype == IMX1_UART;
341 static inline int imx_uart_is_imx21(struct imx_port *sport)
343 return sport->devdata->devtype == IMX21_UART;
346 static inline int imx_uart_is_imx53(struct imx_port *sport)
348 return sport->devdata->devtype == IMX53_UART;
351 static inline int imx_uart_is_imx6q(struct imx_port *sport)
353 return sport->devdata->devtype == IMX6Q_UART;
356 * Save and restore functions for UCR1, UCR2 and UCR3 registers
358 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
359 static void imx_uart_ucrs_save(struct imx_port *sport,
360 struct imx_port_ucrs *ucr)
362 /* save control registers */
363 ucr->ucr1 = imx_uart_readl(sport, UCR1);
364 ucr->ucr2 = imx_uart_readl(sport, UCR2);
365 ucr->ucr3 = imx_uart_readl(sport, UCR3);
368 static void imx_uart_ucrs_restore(struct imx_port *sport,
369 struct imx_port_ucrs *ucr)
371 /* restore control registers */
372 imx_uart_writel(sport, ucr->ucr1, UCR1);
373 imx_uart_writel(sport, ucr->ucr2, UCR2);
374 imx_uart_writel(sport, ucr->ucr3, UCR3);
378 /* called with port.lock taken and irqs caller dependent */
379 static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2)
381 *ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
383 sport->port.mctrl |= TIOCM_RTS;
384 mctrl_gpio_set(sport->gpios, sport->port.mctrl);
387 /* called with port.lock taken and irqs caller dependent */
388 static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2)
393 sport->port.mctrl &= ~TIOCM_RTS;
394 mctrl_gpio_set(sport->gpios, sport->port.mctrl);
397 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
399 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
402 /* called with port.lock taken and irqs off */
403 static void imx_uart_start_rx(struct uart_port *port)
405 struct imx_port *sport = (struct imx_port *)port;
406 unsigned int ucr1, ucr2;
408 ucr1 = imx_uart_readl(sport, UCR1);
409 ucr2 = imx_uart_readl(sport, UCR2);
413 if (sport->dma_is_enabled) {
414 ucr1 |= UCR1_RXDMAEN | UCR1_ATDMAEN;
420 /* Write UCR2 first as it includes RXEN */
421 imx_uart_writel(sport, ucr2, UCR2);
422 imx_uart_writel(sport, ucr1, UCR1);
425 /* called with port.lock taken and irqs off */
426 static void imx_uart_stop_tx(struct uart_port *port)
428 struct imx_port *sport = (struct imx_port *)port;
429 u32 ucr1, ucr4, usr2;
431 if (sport->tx_state == OFF)
435 * We are maybe in the SMP context, so if the DMA TX thread is running
436 * on other cpu, we have to wait for it to finish.
438 if (sport->dma_is_txing)
441 ucr1 = imx_uart_readl(sport, UCR1);
442 imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
444 usr2 = imx_uart_readl(sport, USR2);
445 if (!(usr2 & USR2_TXDC)) {
446 /* The shifter is still busy, so retry once TC triggers */
450 ucr4 = imx_uart_readl(sport, UCR4);
452 imx_uart_writel(sport, ucr4, UCR4);
454 /* in rs485 mode disable transmitter */
455 if (port->rs485.flags & SER_RS485_ENABLED) {
456 if (sport->tx_state == SEND) {
457 sport->tx_state = WAIT_AFTER_SEND;
459 if (port->rs485.delay_rts_after_send > 0) {
460 start_hrtimer_ms(&sport->trigger_stop_tx,
461 port->rs485.delay_rts_after_send);
465 /* continue without any delay */
468 if (sport->tx_state == WAIT_AFTER_RTS ||
469 sport->tx_state == WAIT_AFTER_SEND) {
472 hrtimer_try_to_cancel(&sport->trigger_start_tx);
474 ucr2 = imx_uart_readl(sport, UCR2);
475 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
476 imx_uart_rts_active(sport, &ucr2);
478 imx_uart_rts_inactive(sport, &ucr2);
479 imx_uart_writel(sport, ucr2, UCR2);
481 imx_uart_start_rx(port);
483 sport->tx_state = OFF;
486 sport->tx_state = OFF;
490 /* called with port.lock taken and irqs off */
491 static void imx_uart_stop_rx(struct uart_port *port)
493 struct imx_port *sport = (struct imx_port *)port;
494 u32 ucr1, ucr2, ucr4;
496 ucr1 = imx_uart_readl(sport, UCR1);
497 ucr2 = imx_uart_readl(sport, UCR2);
498 ucr4 = imx_uart_readl(sport, UCR4);
500 if (sport->dma_is_enabled) {
501 ucr1 &= ~(UCR1_RXDMAEN | UCR1_ATDMAEN);
503 ucr1 &= ~UCR1_RRDYEN;
507 imx_uart_writel(sport, ucr1, UCR1);
508 imx_uart_writel(sport, ucr4, UCR4);
511 imx_uart_writel(sport, ucr2, UCR2);
514 /* called with port.lock taken and irqs off */
515 static void imx_uart_enable_ms(struct uart_port *port)
517 struct imx_port *sport = (struct imx_port *)port;
519 mod_timer(&sport->timer, jiffies);
521 mctrl_gpio_enable_ms(sport->gpios);
524 static void imx_uart_dma_tx(struct imx_port *sport);
526 /* called with port.lock taken and irqs off */
527 static inline void imx_uart_transmit_buffer(struct imx_port *sport)
529 struct circ_buf *xmit = &sport->port.state->xmit;
531 if (sport->port.x_char) {
533 imx_uart_writel(sport, sport->port.x_char, URTX0);
534 sport->port.icount.tx++;
535 sport->port.x_char = 0;
539 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
540 imx_uart_stop_tx(&sport->port);
544 if (sport->dma_is_enabled) {
547 * We've just sent a X-char Ensure the TX DMA is enabled
548 * and the TX IRQ is disabled.
550 ucr1 = imx_uart_readl(sport, UCR1);
551 ucr1 &= ~UCR1_TRDYEN;
552 if (sport->dma_is_txing) {
553 ucr1 |= UCR1_TXDMAEN;
554 imx_uart_writel(sport, ucr1, UCR1);
556 imx_uart_writel(sport, ucr1, UCR1);
557 imx_uart_dma_tx(sport);
563 while (!uart_circ_empty(xmit) &&
564 !(imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)) {
565 /* send xmit->buf[xmit->tail]
566 * out the port here */
567 imx_uart_writel(sport, xmit->buf[xmit->tail], URTX0);
568 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
569 sport->port.icount.tx++;
572 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
573 uart_write_wakeup(&sport->port);
575 if (uart_circ_empty(xmit))
576 imx_uart_stop_tx(&sport->port);
579 static void imx_uart_dma_tx_callback(void *data)
581 struct imx_port *sport = data;
582 struct scatterlist *sgl = &sport->tx_sgl[0];
583 struct circ_buf *xmit = &sport->port.state->xmit;
587 spin_lock_irqsave(&sport->port.lock, flags);
589 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
591 ucr1 = imx_uart_readl(sport, UCR1);
592 ucr1 &= ~UCR1_TXDMAEN;
593 imx_uart_writel(sport, ucr1, UCR1);
595 /* update the stat */
596 xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
597 sport->port.icount.tx += sport->tx_bytes;
599 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
601 sport->dma_is_txing = 0;
603 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
604 uart_write_wakeup(&sport->port);
606 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
607 imx_uart_dma_tx(sport);
608 else if (sport->port.rs485.flags & SER_RS485_ENABLED) {
609 u32 ucr4 = imx_uart_readl(sport, UCR4);
611 imx_uart_writel(sport, ucr4, UCR4);
614 spin_unlock_irqrestore(&sport->port.lock, flags);
617 /* called with port.lock taken and irqs off */
618 static void imx_uart_dma_tx(struct imx_port *sport)
620 struct circ_buf *xmit = &sport->port.state->xmit;
621 struct scatterlist *sgl = sport->tx_sgl;
622 struct dma_async_tx_descriptor *desc;
623 struct dma_chan *chan = sport->dma_chan_tx;
624 struct device *dev = sport->port.dev;
628 if (sport->dma_is_txing)
631 ucr4 = imx_uart_readl(sport, UCR4);
633 imx_uart_writel(sport, ucr4, UCR4);
635 sport->tx_bytes = uart_circ_chars_pending(xmit);
637 if (xmit->tail < xmit->head || xmit->head == 0) {
638 sport->dma_tx_nents = 1;
639 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
641 sport->dma_tx_nents = 2;
642 sg_init_table(sgl, 2);
643 sg_set_buf(sgl, xmit->buf + xmit->tail,
644 UART_XMIT_SIZE - xmit->tail);
645 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
648 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
650 dev_err(dev, "DMA mapping error for TX.\n");
653 desc = dmaengine_prep_slave_sg(chan, sgl, ret,
654 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
656 dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
658 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
661 desc->callback = imx_uart_dma_tx_callback;
662 desc->callback_param = sport;
664 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
665 uart_circ_chars_pending(xmit));
667 ucr1 = imx_uart_readl(sport, UCR1);
668 ucr1 |= UCR1_TXDMAEN;
669 imx_uart_writel(sport, ucr1, UCR1);
672 sport->dma_is_txing = 1;
673 dmaengine_submit(desc);
674 dma_async_issue_pending(chan);
678 /* called with port.lock taken and irqs off */
679 static void imx_uart_start_tx(struct uart_port *port)
681 struct imx_port *sport = (struct imx_port *)port;
684 if (!sport->port.x_char && uart_circ_empty(&port->state->xmit))
688 * We cannot simply do nothing here if sport->tx_state == SEND already
689 * because UCR1_TXMPTYEN might already have been cleared in
690 * imx_uart_stop_tx(), but tx_state is still SEND.
693 if (port->rs485.flags & SER_RS485_ENABLED) {
694 if (sport->tx_state == OFF) {
695 u32 ucr2 = imx_uart_readl(sport, UCR2);
696 if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
697 imx_uart_rts_active(sport, &ucr2);
699 imx_uart_rts_inactive(sport, &ucr2);
700 imx_uart_writel(sport, ucr2, UCR2);
702 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
703 imx_uart_stop_rx(port);
705 sport->tx_state = WAIT_AFTER_RTS;
707 if (port->rs485.delay_rts_before_send > 0) {
708 start_hrtimer_ms(&sport->trigger_start_tx,
709 port->rs485.delay_rts_before_send);
713 /* continue without any delay */
716 if (sport->tx_state == WAIT_AFTER_SEND
717 || sport->tx_state == WAIT_AFTER_RTS) {
719 hrtimer_try_to_cancel(&sport->trigger_stop_tx);
722 * Enable transmitter and shifter empty irq only if DMA
723 * is off. In the DMA case this is done in the
726 if (!sport->dma_is_enabled) {
727 u32 ucr4 = imx_uart_readl(sport, UCR4);
729 imx_uart_writel(sport, ucr4, UCR4);
732 sport->tx_state = SEND;
735 sport->tx_state = SEND;
738 if (!sport->dma_is_enabled) {
739 ucr1 = imx_uart_readl(sport, UCR1);
740 imx_uart_writel(sport, ucr1 | UCR1_TRDYEN, UCR1);
743 if (sport->dma_is_enabled) {
744 if (sport->port.x_char) {
745 /* We have X-char to send, so enable TX IRQ and
746 * disable TX DMA to let TX interrupt to send X-char */
747 ucr1 = imx_uart_readl(sport, UCR1);
748 ucr1 &= ~UCR1_TXDMAEN;
750 imx_uart_writel(sport, ucr1, UCR1);
754 if (!uart_circ_empty(&port->state->xmit) &&
755 !uart_tx_stopped(port))
756 imx_uart_dma_tx(sport);
761 static irqreturn_t __imx_uart_rtsint(int irq, void *dev_id)
763 struct imx_port *sport = dev_id;
766 imx_uart_writel(sport, USR1_RTSD, USR1);
767 usr1 = imx_uart_readl(sport, USR1) & USR1_RTSS;
768 uart_handle_cts_change(&sport->port, !!usr1);
769 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
774 static irqreturn_t imx_uart_rtsint(int irq, void *dev_id)
776 struct imx_port *sport = dev_id;
779 spin_lock(&sport->port.lock);
781 ret = __imx_uart_rtsint(irq, dev_id);
783 spin_unlock(&sport->port.lock);
788 static irqreturn_t imx_uart_txint(int irq, void *dev_id)
790 struct imx_port *sport = dev_id;
792 spin_lock(&sport->port.lock);
793 imx_uart_transmit_buffer(sport);
794 spin_unlock(&sport->port.lock);
798 static irqreturn_t __imx_uart_rxint(int irq, void *dev_id)
800 struct imx_port *sport = dev_id;
801 unsigned int rx, flg, ignored = 0;
802 struct tty_port *port = &sport->port.state->port;
804 while (imx_uart_readl(sport, USR2) & USR2_RDR) {
808 sport->port.icount.rx++;
810 rx = imx_uart_readl(sport, URXD0);
812 usr2 = imx_uart_readl(sport, USR2);
813 if (usr2 & USR2_BRCD) {
814 imx_uart_writel(sport, USR2_BRCD, USR2);
815 if (uart_handle_break(&sport->port))
819 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
822 if (unlikely(rx & URXD_ERR)) {
824 sport->port.icount.brk++;
825 else if (rx & URXD_PRERR)
826 sport->port.icount.parity++;
827 else if (rx & URXD_FRMERR)
828 sport->port.icount.frame++;
829 if (rx & URXD_OVRRUN)
830 sport->port.icount.overrun++;
832 if (rx & sport->port.ignore_status_mask) {
838 rx &= (sport->port.read_status_mask | 0xFF);
842 else if (rx & URXD_PRERR)
844 else if (rx & URXD_FRMERR)
846 if (rx & URXD_OVRRUN)
849 sport->port.sysrq = 0;
852 if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
855 if (tty_insert_flip_char(port, rx, flg) == 0)
856 sport->port.icount.buf_overrun++;
860 tty_flip_buffer_push(port);
865 static irqreturn_t imx_uart_rxint(int irq, void *dev_id)
867 struct imx_port *sport = dev_id;
870 spin_lock(&sport->port.lock);
872 ret = __imx_uart_rxint(irq, dev_id);
874 spin_unlock(&sport->port.lock);
879 static void imx_uart_clear_rx_errors(struct imx_port *sport);
882 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
884 static unsigned int imx_uart_get_hwmctrl(struct imx_port *sport)
886 unsigned int tmp = TIOCM_DSR;
887 unsigned usr1 = imx_uart_readl(sport, USR1);
888 unsigned usr2 = imx_uart_readl(sport, USR2);
890 if (usr1 & USR1_RTSS)
893 /* in DCE mode DCDIN is always 0 */
894 if (!(usr2 & USR2_DCDIN))
898 if (!(imx_uart_readl(sport, USR2) & USR2_RIIN))
905 * Handle any change of modem status signal since we were last called.
907 static void imx_uart_mctrl_check(struct imx_port *sport)
909 unsigned int status, changed;
911 status = imx_uart_get_hwmctrl(sport);
912 changed = status ^ sport->old_status;
917 sport->old_status = status;
919 if (changed & TIOCM_RI && status & TIOCM_RI)
920 sport->port.icount.rng++;
921 if (changed & TIOCM_DSR)
922 sport->port.icount.dsr++;
923 if (changed & TIOCM_CAR)
924 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
925 if (changed & TIOCM_CTS)
926 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
928 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
931 static irqreturn_t imx_uart_int(int irq, void *dev_id)
933 struct imx_port *sport = dev_id;
934 unsigned int usr1, usr2, ucr1, ucr2, ucr3, ucr4;
935 irqreturn_t ret = IRQ_NONE;
937 spin_lock(&sport->port.lock);
939 usr1 = imx_uart_readl(sport, USR1);
940 usr2 = imx_uart_readl(sport, USR2);
941 ucr1 = imx_uart_readl(sport, UCR1);
942 ucr2 = imx_uart_readl(sport, UCR2);
943 ucr3 = imx_uart_readl(sport, UCR3);
944 ucr4 = imx_uart_readl(sport, UCR4);
947 * Even if a condition is true that can trigger an irq only handle it if
948 * the respective irq source is enabled. This prevents some undesired
949 * actions, for example if a character that sits in the RX FIFO and that
950 * should be fetched via DMA is tried to be fetched using PIO. Or the
951 * receiver is currently off and so reading from URXD0 results in an
952 * exception. So just mask the (raw) status bits for disabled irqs.
954 if ((ucr1 & UCR1_RRDYEN) == 0)
956 if ((ucr2 & UCR2_ATEN) == 0)
958 if ((ucr1 & UCR1_TRDYEN) == 0)
960 if ((ucr4 & UCR4_TCEN) == 0)
962 if ((ucr3 & UCR3_DTRDEN) == 0)
964 if ((ucr1 & UCR1_RTSDEN) == 0)
966 if ((ucr3 & UCR3_AWAKEN) == 0)
968 if ((ucr4 & UCR4_OREN) == 0)
971 if (usr1 & (USR1_RRDY | USR1_AGTIM)) {
972 imx_uart_writel(sport, USR1_AGTIM, USR1);
974 __imx_uart_rxint(irq, dev_id);
978 if ((usr1 & USR1_TRDY) || (usr2 & USR2_TXDC)) {
979 imx_uart_transmit_buffer(sport);
983 if (usr1 & USR1_DTRD) {
984 imx_uart_writel(sport, USR1_DTRD, USR1);
986 imx_uart_mctrl_check(sport);
991 if (usr1 & USR1_RTSD) {
992 __imx_uart_rtsint(irq, dev_id);
996 if (usr1 & USR1_AWAKE) {
997 imx_uart_writel(sport, USR1_AWAKE, USR1);
1001 if (usr2 & USR2_ORE) {
1002 sport->port.icount.overrun++;
1003 imx_uart_writel(sport, USR2_ORE, USR2);
1007 spin_unlock(&sport->port.lock);
1013 * Return TIOCSER_TEMT when transmitter is not busy.
1015 static unsigned int imx_uart_tx_empty(struct uart_port *port)
1017 struct imx_port *sport = (struct imx_port *)port;
1020 ret = (imx_uart_readl(sport, USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
1022 /* If the TX DMA is working, return 0. */
1023 if (sport->dma_is_txing)
1029 /* called with port.lock taken and irqs off */
1030 static unsigned int imx_uart_get_mctrl(struct uart_port *port)
1032 struct imx_port *sport = (struct imx_port *)port;
1033 unsigned int ret = imx_uart_get_hwmctrl(sport);
1035 mctrl_gpio_get(sport->gpios, &ret);
1040 /* called with port.lock taken and irqs off */
1041 static void imx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1043 struct imx_port *sport = (struct imx_port *)port;
1046 if (!(port->rs485.flags & SER_RS485_ENABLED)) {
1050 * Turn off autoRTS if RTS is lowered and restore autoRTS
1051 * setting if RTS is raised.
1053 ucr2 = imx_uart_readl(sport, UCR2);
1054 ucr2 &= ~(UCR2_CTS | UCR2_CTSC);
1055 if (mctrl & TIOCM_RTS) {
1058 * UCR2_IRTS is unset if and only if the port is
1059 * configured for CRTSCTS, so we use inverted UCR2_IRTS
1060 * to get the state to restore to.
1062 if (!(ucr2 & UCR2_IRTS))
1065 imx_uart_writel(sport, ucr2, UCR2);
1068 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_DSR;
1069 if (!(mctrl & TIOCM_DTR))
1071 imx_uart_writel(sport, ucr3, UCR3);
1073 uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)) & ~UTS_LOOP;
1074 if (mctrl & TIOCM_LOOP)
1076 imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
1078 mctrl_gpio_set(sport->gpios, mctrl);
1082 * Interrupts always disabled.
1084 static void imx_uart_break_ctl(struct uart_port *port, int break_state)
1086 struct imx_port *sport = (struct imx_port *)port;
1087 unsigned long flags;
1090 spin_lock_irqsave(&sport->port.lock, flags);
1092 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_SNDBRK;
1094 if (break_state != 0)
1095 ucr1 |= UCR1_SNDBRK;
1097 imx_uart_writel(sport, ucr1, UCR1);
1099 spin_unlock_irqrestore(&sport->port.lock, flags);
1103 * This is our per-port timeout handler, for checking the
1104 * modem status signals.
1106 static void imx_uart_timeout(struct timer_list *t)
1108 struct imx_port *sport = from_timer(sport, t, timer);
1109 unsigned long flags;
1111 if (sport->port.state) {
1112 spin_lock_irqsave(&sport->port.lock, flags);
1113 imx_uart_mctrl_check(sport);
1114 spin_unlock_irqrestore(&sport->port.lock, flags);
1116 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
1121 * There are two kinds of RX DMA interrupts(such as in the MX6Q):
1122 * [1] the RX DMA buffer is full.
1123 * [2] the aging timer expires
1125 * Condition [2] is triggered when a character has been sitting in the FIFO
1126 * for at least 8 byte durations.
1128 static void imx_uart_dma_rx_callback(void *data)
1130 struct imx_port *sport = data;
1131 struct dma_chan *chan = sport->dma_chan_rx;
1132 struct scatterlist *sgl = &sport->rx_sgl;
1133 struct tty_port *port = &sport->port.state->port;
1134 struct dma_tx_state state;
1135 struct circ_buf *rx_ring = &sport->rx_ring;
1136 enum dma_status status;
1137 unsigned int w_bytes = 0;
1138 unsigned int r_bytes;
1139 unsigned int bd_size;
1141 status = dmaengine_tx_status(chan, sport->rx_cookie, &state);
1143 if (status == DMA_ERROR) {
1144 imx_uart_clear_rx_errors(sport);
1148 if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
1151 * The state-residue variable represents the empty space
1152 * relative to the entire buffer. Taking this in consideration
1153 * the head is always calculated base on the buffer total
1154 * length - DMA transaction residue. The UART script from the
1155 * SDMA firmware will jump to the next buffer descriptor,
1156 * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4).
1157 * Taking this in consideration the tail is always at the
1158 * beginning of the buffer descriptor that contains the head.
1161 /* Calculate the head */
1162 rx_ring->head = sg_dma_len(sgl) - state.residue;
1164 /* Calculate the tail. */
1165 bd_size = sg_dma_len(sgl) / sport->rx_periods;
1166 rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size;
1168 if (rx_ring->head <= sg_dma_len(sgl) &&
1169 rx_ring->head > rx_ring->tail) {
1171 /* Move data from tail to head */
1172 r_bytes = rx_ring->head - rx_ring->tail;
1174 /* CPU claims ownership of RX DMA buffer */
1175 dma_sync_sg_for_cpu(sport->port.dev, sgl, 1,
1178 w_bytes = tty_insert_flip_string(port,
1179 sport->rx_buf + rx_ring->tail, r_bytes);
1181 /* UART retrieves ownership of RX DMA buffer */
1182 dma_sync_sg_for_device(sport->port.dev, sgl, 1,
1185 if (w_bytes != r_bytes)
1186 sport->port.icount.buf_overrun++;
1188 sport->port.icount.rx += w_bytes;
1190 WARN_ON(rx_ring->head > sg_dma_len(sgl));
1191 WARN_ON(rx_ring->head <= rx_ring->tail);
1196 tty_flip_buffer_push(port);
1197 dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes);
1201 static int imx_uart_start_rx_dma(struct imx_port *sport)
1203 struct scatterlist *sgl = &sport->rx_sgl;
1204 struct dma_chan *chan = sport->dma_chan_rx;
1205 struct device *dev = sport->port.dev;
1206 struct dma_async_tx_descriptor *desc;
1209 sport->rx_ring.head = 0;
1210 sport->rx_ring.tail = 0;
1212 sg_init_one(sgl, sport->rx_buf, sport->rx_buf_size);
1213 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1215 dev_err(dev, "DMA mapping error for RX.\n");
1219 desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl),
1220 sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods,
1221 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
1224 dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1225 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
1228 desc->callback = imx_uart_dma_rx_callback;
1229 desc->callback_param = sport;
1231 dev_dbg(dev, "RX: prepare for the DMA.\n");
1232 sport->dma_is_rxing = 1;
1233 sport->rx_cookie = dmaengine_submit(desc);
1234 dma_async_issue_pending(chan);
1238 static void imx_uart_clear_rx_errors(struct imx_port *sport)
1240 struct tty_port *port = &sport->port.state->port;
1243 usr1 = imx_uart_readl(sport, USR1);
1244 usr2 = imx_uart_readl(sport, USR2);
1246 if (usr2 & USR2_BRCD) {
1247 sport->port.icount.brk++;
1248 imx_uart_writel(sport, USR2_BRCD, USR2);
1249 uart_handle_break(&sport->port);
1250 if (tty_insert_flip_char(port, 0, TTY_BREAK) == 0)
1251 sport->port.icount.buf_overrun++;
1252 tty_flip_buffer_push(port);
1254 if (usr1 & USR1_FRAMERR) {
1255 sport->port.icount.frame++;
1256 imx_uart_writel(sport, USR1_FRAMERR, USR1);
1257 } else if (usr1 & USR1_PARITYERR) {
1258 sport->port.icount.parity++;
1259 imx_uart_writel(sport, USR1_PARITYERR, USR1);
1263 if (usr2 & USR2_ORE) {
1264 sport->port.icount.overrun++;
1265 imx_uart_writel(sport, USR2_ORE, USR2);
1270 #define TXTL_DEFAULT 2 /* reset default */
1271 #define RXTL_DEFAULT 8 /* 8 characters or aging timer */
1272 #define TXTL_DMA 8 /* DMA burst setting */
1273 #define RXTL_DMA 9 /* DMA burst setting */
1275 static void imx_uart_setup_ufcr(struct imx_port *sport,
1276 unsigned char txwl, unsigned char rxwl)
1280 /* set receiver / transmitter trigger level */
1281 val = imx_uart_readl(sport, UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
1282 val |= txwl << UFCR_TXTL_SHF | rxwl;
1283 imx_uart_writel(sport, val, UFCR);
1286 static void imx_uart_dma_exit(struct imx_port *sport)
1288 if (sport->dma_chan_rx) {
1289 dmaengine_terminate_sync(sport->dma_chan_rx);
1290 dma_release_channel(sport->dma_chan_rx);
1291 sport->dma_chan_rx = NULL;
1292 sport->rx_cookie = -EINVAL;
1293 kfree(sport->rx_buf);
1294 sport->rx_buf = NULL;
1297 if (sport->dma_chan_tx) {
1298 dmaengine_terminate_sync(sport->dma_chan_tx);
1299 dma_release_channel(sport->dma_chan_tx);
1300 sport->dma_chan_tx = NULL;
1304 static int imx_uart_dma_init(struct imx_port *sport)
1306 struct dma_slave_config slave_config = {};
1307 struct device *dev = sport->port.dev;
1310 /* Prepare for RX : */
1311 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1312 if (!sport->dma_chan_rx) {
1313 dev_dbg(dev, "cannot get the DMA channel.\n");
1318 slave_config.direction = DMA_DEV_TO_MEM;
1319 slave_config.src_addr = sport->port.mapbase + URXD0;
1320 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1321 /* one byte less than the watermark level to enable the aging timer */
1322 slave_config.src_maxburst = RXTL_DMA - 1;
1323 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
1325 dev_err(dev, "error in RX dma configuration.\n");
1329 sport->rx_buf_size = sport->rx_period_length * sport->rx_periods;
1330 sport->rx_buf = kzalloc(sport->rx_buf_size, GFP_KERNEL);
1331 if (!sport->rx_buf) {
1335 sport->rx_ring.buf = sport->rx_buf;
1337 /* Prepare for TX : */
1338 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1339 if (!sport->dma_chan_tx) {
1340 dev_err(dev, "cannot get the TX DMA channel!\n");
1345 slave_config.direction = DMA_MEM_TO_DEV;
1346 slave_config.dst_addr = sport->port.mapbase + URTX0;
1347 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1348 slave_config.dst_maxburst = TXTL_DMA;
1349 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1351 dev_err(dev, "error in TX dma configuration.");
1357 imx_uart_dma_exit(sport);
1361 static void imx_uart_enable_dma(struct imx_port *sport)
1365 imx_uart_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
1368 ucr1 = imx_uart_readl(sport, UCR1);
1369 ucr1 |= UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN;
1370 imx_uart_writel(sport, ucr1, UCR1);
1372 sport->dma_is_enabled = 1;
1375 static void imx_uart_disable_dma(struct imx_port *sport)
1380 ucr1 = imx_uart_readl(sport, UCR1);
1381 ucr1 &= ~(UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN);
1382 imx_uart_writel(sport, ucr1, UCR1);
1384 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1386 sport->dma_is_enabled = 0;
1389 /* half the RX buffer size */
1392 static int imx_uart_startup(struct uart_port *port)
1394 struct imx_port *sport = (struct imx_port *)port;
1396 unsigned long flags;
1397 int dma_is_inited = 0;
1398 u32 ucr1, ucr2, ucr3, ucr4;
1400 retval = clk_prepare_enable(sport->clk_per);
1403 retval = clk_prepare_enable(sport->clk_ipg);
1405 clk_disable_unprepare(sport->clk_per);
1409 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1411 /* disable the DREN bit (Data Ready interrupt enable) before
1414 ucr4 = imx_uart_readl(sport, UCR4);
1416 /* set the trigger level for CTS */
1417 ucr4 &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
1418 ucr4 |= CTSTL << UCR4_CTSTL_SHF;
1420 imx_uart_writel(sport, ucr4 & ~UCR4_DREN, UCR4);
1422 /* Can we enable the DMA support? */
1423 if (!uart_console(port) && imx_uart_dma_init(sport) == 0)
1426 spin_lock_irqsave(&sport->port.lock, flags);
1427 /* Reset fifo's and state machines */
1430 ucr2 = imx_uart_readl(sport, UCR2);
1432 imx_uart_writel(sport, ucr2, UCR2);
1434 while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
1438 * Finally, clear and enable interrupts
1440 imx_uart_writel(sport, USR1_RTSD | USR1_DTRD, USR1);
1441 imx_uart_writel(sport, USR2_ORE, USR2);
1443 ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_RRDYEN;
1444 ucr1 |= UCR1_UARTEN;
1445 if (sport->have_rtscts)
1446 ucr1 |= UCR1_RTSDEN;
1448 imx_uart_writel(sport, ucr1, UCR1);
1450 ucr4 = imx_uart_readl(sport, UCR4) & ~(UCR4_OREN | UCR4_INVR);
1453 if (sport->inverted_rx)
1455 imx_uart_writel(sport, ucr4, UCR4);
1457 ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_INVT;
1459 * configure tx polarity before enabling tx
1461 if (sport->inverted_tx)
1464 if (!imx_uart_is_imx1(sport)) {
1465 ucr3 |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
1467 if (sport->dte_mode)
1468 /* disable broken interrupts */
1469 ucr3 &= ~(UCR3_RI | UCR3_DCD);
1471 imx_uart_writel(sport, ucr3, UCR3);
1473 ucr2 = imx_uart_readl(sport, UCR2) & ~UCR2_ATEN;
1474 ucr2 |= (UCR2_RXEN | UCR2_TXEN);
1475 if (!sport->have_rtscts)
1478 * make sure the edge sensitive RTS-irq is disabled,
1479 * we're using RTSD instead.
1481 if (!imx_uart_is_imx1(sport))
1482 ucr2 &= ~UCR2_RTSEN;
1483 imx_uart_writel(sport, ucr2, UCR2);
1486 * Enable modem status interrupts
1488 imx_uart_enable_ms(&sport->port);
1490 if (dma_is_inited) {
1491 imx_uart_enable_dma(sport);
1492 imx_uart_start_rx_dma(sport);
1494 ucr1 = imx_uart_readl(sport, UCR1);
1495 ucr1 |= UCR1_RRDYEN;
1496 imx_uart_writel(sport, ucr1, UCR1);
1498 ucr2 = imx_uart_readl(sport, UCR2);
1500 imx_uart_writel(sport, ucr2, UCR2);
1503 spin_unlock_irqrestore(&sport->port.lock, flags);
1508 static void imx_uart_shutdown(struct uart_port *port)
1510 struct imx_port *sport = (struct imx_port *)port;
1511 unsigned long flags;
1512 u32 ucr1, ucr2, ucr4;
1514 if (sport->dma_is_enabled) {
1515 dmaengine_terminate_sync(sport->dma_chan_tx);
1516 if (sport->dma_is_txing) {
1517 dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
1518 sport->dma_tx_nents, DMA_TO_DEVICE);
1519 sport->dma_is_txing = 0;
1521 dmaengine_terminate_sync(sport->dma_chan_rx);
1522 if (sport->dma_is_rxing) {
1523 dma_unmap_sg(sport->port.dev, &sport->rx_sgl,
1524 1, DMA_FROM_DEVICE);
1525 sport->dma_is_rxing = 0;
1528 spin_lock_irqsave(&sport->port.lock, flags);
1529 imx_uart_stop_tx(port);
1530 imx_uart_stop_rx(port);
1531 imx_uart_disable_dma(sport);
1532 spin_unlock_irqrestore(&sport->port.lock, flags);
1533 imx_uart_dma_exit(sport);
1536 mctrl_gpio_disable_ms(sport->gpios);
1538 spin_lock_irqsave(&sport->port.lock, flags);
1539 ucr2 = imx_uart_readl(sport, UCR2);
1540 ucr2 &= ~(UCR2_TXEN | UCR2_ATEN);
1541 imx_uart_writel(sport, ucr2, UCR2);
1542 spin_unlock_irqrestore(&sport->port.lock, flags);
1547 del_timer_sync(&sport->timer);
1550 * Disable all interrupts, port and break condition.
1553 spin_lock_irqsave(&sport->port.lock, flags);
1555 ucr1 = imx_uart_readl(sport, UCR1);
1556 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN | UCR1_RXDMAEN | UCR1_ATDMAEN);
1557 imx_uart_writel(sport, ucr1, UCR1);
1559 ucr4 = imx_uart_readl(sport, UCR4);
1561 imx_uart_writel(sport, ucr4, UCR4);
1563 spin_unlock_irqrestore(&sport->port.lock, flags);
1565 clk_disable_unprepare(sport->clk_per);
1566 clk_disable_unprepare(sport->clk_ipg);
1569 /* called with port.lock taken and irqs off */
1570 static void imx_uart_flush_buffer(struct uart_port *port)
1572 struct imx_port *sport = (struct imx_port *)port;
1573 struct scatterlist *sgl = &sport->tx_sgl[0];
1575 int i = 100, ubir, ubmr, uts;
1577 if (!sport->dma_chan_tx)
1580 sport->tx_bytes = 0;
1581 dmaengine_terminate_all(sport->dma_chan_tx);
1582 if (sport->dma_is_txing) {
1585 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
1587 ucr1 = imx_uart_readl(sport, UCR1);
1588 ucr1 &= ~UCR1_TXDMAEN;
1589 imx_uart_writel(sport, ucr1, UCR1);
1590 sport->dma_is_txing = 0;
1594 * According to the Reference Manual description of the UART SRST bit:
1596 * "Reset the transmit and receive state machines,
1597 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
1600 * We don't need to restore the old values from USR1, USR2, URXD and
1601 * UTXD. UBRC is read only, so only save/restore the other three
1604 ubir = imx_uart_readl(sport, UBIR);
1605 ubmr = imx_uart_readl(sport, UBMR);
1606 uts = imx_uart_readl(sport, IMX21_UTS);
1608 ucr2 = imx_uart_readl(sport, UCR2);
1610 imx_uart_writel(sport, ucr2, UCR2);
1612 while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
1615 /* Restore the registers */
1616 imx_uart_writel(sport, ubir, UBIR);
1617 imx_uart_writel(sport, ubmr, UBMR);
1618 imx_uart_writel(sport, uts, IMX21_UTS);
1622 imx_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1623 struct ktermios *old)
1625 struct imx_port *sport = (struct imx_port *)port;
1626 unsigned long flags;
1627 u32 ucr2, old_ucr2, ufcr;
1628 unsigned int baud, quot;
1629 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1631 unsigned long num, denom, old_ubir, old_ubmr;
1635 * We only support CS7 and CS8.
1637 while ((termios->c_cflag & CSIZE) != CS7 &&
1638 (termios->c_cflag & CSIZE) != CS8) {
1639 termios->c_cflag &= ~CSIZE;
1640 termios->c_cflag |= old_csize;
1644 del_timer_sync(&sport->timer);
1647 * Ask the core to calculate the divisor for us.
1649 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1650 quot = uart_get_divisor(port, baud);
1652 spin_lock_irqsave(&sport->port.lock, flags);
1655 * Read current UCR2 and save it for future use, then clear all the bits
1656 * except those we will or may need to preserve.
1658 old_ucr2 = imx_uart_readl(sport, UCR2);
1659 ucr2 = old_ucr2 & (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN | UCR2_CTS);
1661 ucr2 |= UCR2_SRST | UCR2_IRTS;
1662 if ((termios->c_cflag & CSIZE) == CS8)
1665 if (!sport->have_rtscts)
1666 termios->c_cflag &= ~CRTSCTS;
1668 if (port->rs485.flags & SER_RS485_ENABLED) {
1670 * RTS is mandatory for rs485 operation, so keep
1671 * it under manual control and keep transmitter
1674 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1675 imx_uart_rts_active(sport, &ucr2);
1677 imx_uart_rts_inactive(sport, &ucr2);
1679 } else if (termios->c_cflag & CRTSCTS) {
1681 * Only let receiver control RTS output if we were not requested
1682 * to have RTS inactive (which then should take precedence).
1684 if (ucr2 & UCR2_CTS)
1688 if (termios->c_cflag & CRTSCTS)
1690 if (termios->c_cflag & CSTOPB)
1692 if (termios->c_cflag & PARENB) {
1694 if (termios->c_cflag & PARODD)
1698 sport->port.read_status_mask = 0;
1699 if (termios->c_iflag & INPCK)
1700 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
1701 if (termios->c_iflag & (BRKINT | PARMRK))
1702 sport->port.read_status_mask |= URXD_BRK;
1705 * Characters to ignore
1707 sport->port.ignore_status_mask = 0;
1708 if (termios->c_iflag & IGNPAR)
1709 sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
1710 if (termios->c_iflag & IGNBRK) {
1711 sport->port.ignore_status_mask |= URXD_BRK;
1713 * If we're ignoring parity and break indicators,
1714 * ignore overruns too (for real raw support).
1716 if (termios->c_iflag & IGNPAR)
1717 sport->port.ignore_status_mask |= URXD_OVRRUN;
1720 if ((termios->c_cflag & CREAD) == 0)
1721 sport->port.ignore_status_mask |= URXD_DUMMY_READ;
1724 * Update the per-port timeout.
1726 uart_update_timeout(port, termios->c_cflag, baud);
1728 /* custom-baudrate handling */
1729 div = sport->port.uartclk / (baud * 16);
1730 if (baud == 38400 && quot != div)
1731 baud = sport->port.uartclk / (quot * 16);
1733 div = sport->port.uartclk / (baud * 16);
1739 rational_best_approximation(16 * div * baud, sport->port.uartclk,
1740 1 << 16, 1 << 16, &num, &denom);
1742 tdiv64 = sport->port.uartclk;
1744 do_div(tdiv64, denom * 16 * div);
1745 tty_termios_encode_baud_rate(termios,
1746 (speed_t)tdiv64, (speed_t)tdiv64);
1751 ufcr = imx_uart_readl(sport, UFCR);
1752 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
1753 imx_uart_writel(sport, ufcr, UFCR);
1756 * Two registers below should always be written both and in this
1757 * particular order. One consequence is that we need to check if any of
1758 * them changes and then update both. We do need the check for change
1759 * as even writing the same values seem to "restart"
1760 * transmission/receiving logic in the hardware, that leads to data
1761 * breakage even when rate doesn't in fact change. E.g., user switches
1762 * RTS/CTS handshake and suddenly gets broken bytes.
1764 old_ubir = imx_uart_readl(sport, UBIR);
1765 old_ubmr = imx_uart_readl(sport, UBMR);
1766 if (old_ubir != num || old_ubmr != denom) {
1767 imx_uart_writel(sport, num, UBIR);
1768 imx_uart_writel(sport, denom, UBMR);
1771 if (!imx_uart_is_imx1(sport))
1772 imx_uart_writel(sport, sport->port.uartclk / div / 1000,
1775 imx_uart_writel(sport, ucr2, UCR2);
1777 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1778 imx_uart_enable_ms(&sport->port);
1780 spin_unlock_irqrestore(&sport->port.lock, flags);
1783 static const char *imx_uart_type(struct uart_port *port)
1785 struct imx_port *sport = (struct imx_port *)port;
1787 return sport->port.type == PORT_IMX ? "IMX" : NULL;
1791 * Configure/autoconfigure the port.
1793 static void imx_uart_config_port(struct uart_port *port, int flags)
1795 struct imx_port *sport = (struct imx_port *)port;
1797 if (flags & UART_CONFIG_TYPE)
1798 sport->port.type = PORT_IMX;
1802 * Verify the new serial_struct (for TIOCSSERIAL).
1803 * The only change we allow are to the flags and type, and
1804 * even then only between PORT_IMX and PORT_UNKNOWN
1807 imx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
1809 struct imx_port *sport = (struct imx_port *)port;
1812 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1814 if (sport->port.irq != ser->irq)
1816 if (ser->io_type != UPIO_MEM)
1818 if (sport->port.uartclk / 16 != ser->baud_base)
1820 if (sport->port.mapbase != (unsigned long)ser->iomem_base)
1822 if (sport->port.iobase != ser->port)
1829 #if defined(CONFIG_CONSOLE_POLL)
1831 static int imx_uart_poll_init(struct uart_port *port)
1833 struct imx_port *sport = (struct imx_port *)port;
1834 unsigned long flags;
1838 retval = clk_prepare_enable(sport->clk_ipg);
1841 retval = clk_prepare_enable(sport->clk_per);
1843 clk_disable_unprepare(sport->clk_ipg);
1845 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1847 spin_lock_irqsave(&sport->port.lock, flags);
1850 * Be careful about the order of enabling bits here. First enable the
1851 * receiver (UARTEN + RXEN) and only then the corresponding irqs.
1852 * This prevents that a character that already sits in the RX fifo is
1853 * triggering an irq but the try to fetch it from there results in an
1854 * exception because UARTEN or RXEN is still off.
1856 ucr1 = imx_uart_readl(sport, UCR1);
1857 ucr2 = imx_uart_readl(sport, UCR2);
1859 if (imx_uart_is_imx1(sport))
1860 ucr1 |= IMX1_UCR1_UARTCLKEN;
1862 ucr1 |= UCR1_UARTEN;
1863 ucr1 &= ~(UCR1_TRDYEN | UCR1_RTSDEN | UCR1_RRDYEN);
1865 ucr2 |= UCR2_RXEN | UCR2_TXEN;
1868 imx_uart_writel(sport, ucr1, UCR1);
1869 imx_uart_writel(sport, ucr2, UCR2);
1871 /* now enable irqs */
1872 imx_uart_writel(sport, ucr1 | UCR1_RRDYEN, UCR1);
1873 imx_uart_writel(sport, ucr2 | UCR2_ATEN, UCR2);
1875 spin_unlock_irqrestore(&sport->port.lock, flags);
1880 static int imx_uart_poll_get_char(struct uart_port *port)
1882 struct imx_port *sport = (struct imx_port *)port;
1883 if (!(imx_uart_readl(sport, USR2) & USR2_RDR))
1884 return NO_POLL_CHAR;
1886 return imx_uart_readl(sport, URXD0) & URXD_RX_DATA;
1889 static void imx_uart_poll_put_char(struct uart_port *port, unsigned char c)
1891 struct imx_port *sport = (struct imx_port *)port;
1892 unsigned int status;
1896 status = imx_uart_readl(sport, USR1);
1897 } while (~status & USR1_TRDY);
1900 imx_uart_writel(sport, c, URTX0);
1904 status = imx_uart_readl(sport, USR2);
1905 } while (~status & USR2_TXDC);
1909 /* called with port.lock taken and irqs off or from .probe without locking */
1910 static int imx_uart_rs485_config(struct uart_port *port,
1911 struct serial_rs485 *rs485conf)
1913 struct imx_port *sport = (struct imx_port *)port;
1916 /* RTS is required to control the transmitter */
1917 if (!sport->have_rtscts && !sport->have_rtsgpio)
1918 rs485conf->flags &= ~SER_RS485_ENABLED;
1920 if (rs485conf->flags & SER_RS485_ENABLED) {
1921 /* Enable receiver if low-active RTS signal is requested */
1922 if (sport->have_rtscts && !sport->have_rtsgpio &&
1923 !(rs485conf->flags & SER_RS485_RTS_ON_SEND))
1924 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1926 /* disable transmitter */
1927 ucr2 = imx_uart_readl(sport, UCR2);
1928 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
1929 imx_uart_rts_active(sport, &ucr2);
1931 imx_uart_rts_inactive(sport, &ucr2);
1932 imx_uart_writel(sport, ucr2, UCR2);
1935 /* Make sure Rx is enabled in case Tx is active with Rx disabled */
1936 if (!(rs485conf->flags & SER_RS485_ENABLED) ||
1937 rs485conf->flags & SER_RS485_RX_DURING_TX)
1938 imx_uart_start_rx(port);
1940 port->rs485 = *rs485conf;
1945 static const struct uart_ops imx_uart_pops = {
1946 .tx_empty = imx_uart_tx_empty,
1947 .set_mctrl = imx_uart_set_mctrl,
1948 .get_mctrl = imx_uart_get_mctrl,
1949 .stop_tx = imx_uart_stop_tx,
1950 .start_tx = imx_uart_start_tx,
1951 .stop_rx = imx_uart_stop_rx,
1952 .enable_ms = imx_uart_enable_ms,
1953 .break_ctl = imx_uart_break_ctl,
1954 .startup = imx_uart_startup,
1955 .shutdown = imx_uart_shutdown,
1956 .flush_buffer = imx_uart_flush_buffer,
1957 .set_termios = imx_uart_set_termios,
1958 .type = imx_uart_type,
1959 .config_port = imx_uart_config_port,
1960 .verify_port = imx_uart_verify_port,
1961 #if defined(CONFIG_CONSOLE_POLL)
1962 .poll_init = imx_uart_poll_init,
1963 .poll_get_char = imx_uart_poll_get_char,
1964 .poll_put_char = imx_uart_poll_put_char,
1968 static struct imx_port *imx_uart_ports[UART_NR];
1970 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
1971 static void imx_uart_console_putchar(struct uart_port *port, unsigned char ch)
1973 struct imx_port *sport = (struct imx_port *)port;
1975 while (imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)
1978 imx_uart_writel(sport, ch, URTX0);
1982 * Interrupts are disabled on entering
1985 imx_uart_console_write(struct console *co, const char *s, unsigned int count)
1987 struct imx_port *sport = imx_uart_ports[co->index];
1988 struct imx_port_ucrs old_ucr;
1989 unsigned long flags;
1993 if (sport->port.sysrq)
1995 else if (oops_in_progress)
1996 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1998 spin_lock_irqsave(&sport->port.lock, flags);
2001 * First, save UCR1/2/3 and then disable interrupts
2003 imx_uart_ucrs_save(sport, &old_ucr);
2004 ucr1 = old_ucr.ucr1;
2006 if (imx_uart_is_imx1(sport))
2007 ucr1 |= IMX1_UCR1_UARTCLKEN;
2008 ucr1 |= UCR1_UARTEN;
2009 ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN);
2011 imx_uart_writel(sport, ucr1, UCR1);
2013 imx_uart_writel(sport, old_ucr.ucr2 | UCR2_TXEN, UCR2);
2015 uart_console_write(&sport->port, s, count, imx_uart_console_putchar);
2018 * Finally, wait for transmitter to become empty
2019 * and restore UCR1/2/3
2021 while (!(imx_uart_readl(sport, USR2) & USR2_TXDC));
2023 imx_uart_ucrs_restore(sport, &old_ucr);
2026 spin_unlock_irqrestore(&sport->port.lock, flags);
2030 * If the port was already initialised (eg, by a boot loader),
2031 * try to determine the current setup.
2034 imx_uart_console_get_options(struct imx_port *sport, int *baud,
2035 int *parity, int *bits)
2038 if (imx_uart_readl(sport, UCR1) & UCR1_UARTEN) {
2039 /* ok, the port was enabled */
2040 unsigned int ucr2, ubir, ubmr, uartclk;
2041 unsigned int baud_raw;
2042 unsigned int ucfr_rfdiv;
2044 ucr2 = imx_uart_readl(sport, UCR2);
2047 if (ucr2 & UCR2_PREN) {
2048 if (ucr2 & UCR2_PROE)
2059 ubir = imx_uart_readl(sport, UBIR) & 0xffff;
2060 ubmr = imx_uart_readl(sport, UBMR) & 0xffff;
2062 ucfr_rfdiv = (imx_uart_readl(sport, UFCR) & UFCR_RFDIV) >> 7;
2063 if (ucfr_rfdiv == 6)
2066 ucfr_rfdiv = 6 - ucfr_rfdiv;
2068 uartclk = clk_get_rate(sport->clk_per);
2069 uartclk /= ucfr_rfdiv;
2072 * The next code provides exact computation of
2073 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
2074 * without need of float support or long long division,
2075 * which would be required to prevent 32bit arithmetic overflow
2077 unsigned int mul = ubir + 1;
2078 unsigned int div = 16 * (ubmr + 1);
2079 unsigned int rem = uartclk % div;
2081 baud_raw = (uartclk / div) * mul;
2082 baud_raw += (rem * mul + div / 2) / div;
2083 *baud = (baud_raw + 50) / 100 * 100;
2086 if (*baud != baud_raw)
2087 dev_info(sport->port.dev, "Console IMX rounded baud rate from %d to %d\n",
2093 imx_uart_console_setup(struct console *co, char *options)
2095 struct imx_port *sport;
2103 * Check whether an invalid uart number has been specified, and
2104 * if so, search for the first available port that does have
2107 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_uart_ports))
2109 sport = imx_uart_ports[co->index];
2113 /* For setting the registers, we only need to enable the ipg clock. */
2114 retval = clk_prepare_enable(sport->clk_ipg);
2119 uart_parse_options(options, &baud, &parity, &bits, &flow);
2121 imx_uart_console_get_options(sport, &baud, &parity, &bits);
2123 imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
2125 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
2128 clk_disable_unprepare(sport->clk_ipg);
2132 retval = clk_prepare_enable(sport->clk_per);
2134 clk_disable_unprepare(sport->clk_ipg);
2141 imx_uart_console_exit(struct console *co)
2143 struct imx_port *sport = imx_uart_ports[co->index];
2145 clk_disable_unprepare(sport->clk_per);
2146 clk_disable_unprepare(sport->clk_ipg);
2151 static struct uart_driver imx_uart_uart_driver;
2152 static struct console imx_uart_console = {
2154 .write = imx_uart_console_write,
2155 .device = uart_console_device,
2156 .setup = imx_uart_console_setup,
2157 .exit = imx_uart_console_exit,
2158 .flags = CON_PRINTBUFFER,
2160 .data = &imx_uart_uart_driver,
2163 #define IMX_CONSOLE &imx_uart_console
2166 #define IMX_CONSOLE NULL
2169 static struct uart_driver imx_uart_uart_driver = {
2170 .owner = THIS_MODULE,
2171 .driver_name = DRIVER_NAME,
2172 .dev_name = DEV_NAME,
2173 .major = SERIAL_IMX_MAJOR,
2174 .minor = MINOR_START,
2175 .nr = ARRAY_SIZE(imx_uart_ports),
2176 .cons = IMX_CONSOLE,
2179 static enum hrtimer_restart imx_trigger_start_tx(struct hrtimer *t)
2181 struct imx_port *sport = container_of(t, struct imx_port, trigger_start_tx);
2182 unsigned long flags;
2184 spin_lock_irqsave(&sport->port.lock, flags);
2185 if (sport->tx_state == WAIT_AFTER_RTS)
2186 imx_uart_start_tx(&sport->port);
2187 spin_unlock_irqrestore(&sport->port.lock, flags);
2189 return HRTIMER_NORESTART;
2192 static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t)
2194 struct imx_port *sport = container_of(t, struct imx_port, trigger_stop_tx);
2195 unsigned long flags;
2197 spin_lock_irqsave(&sport->port.lock, flags);
2198 if (sport->tx_state == WAIT_AFTER_SEND)
2199 imx_uart_stop_tx(&sport->port);
2200 spin_unlock_irqrestore(&sport->port.lock, flags);
2202 return HRTIMER_NORESTART;
2205 /* Default RX DMA buffer configuration */
2206 #define RX_DMA_PERIODS 16
2207 #define RX_DMA_PERIOD_LEN (PAGE_SIZE / 4)
2209 static int imx_uart_probe(struct platform_device *pdev)
2211 struct device_node *np = pdev->dev.of_node;
2212 struct imx_port *sport;
2214 u32 dma_buf_conf[2];
2217 struct resource *res;
2218 int txirq, rxirq, rtsirq;
2220 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2224 sport->devdata = of_device_get_match_data(&pdev->dev);
2226 ret = of_alias_get_id(np, "serial");
2228 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2231 sport->port.line = ret;
2233 if (of_get_property(np, "uart-has-rtscts", NULL) ||
2234 of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
2235 sport->have_rtscts = 1;
2237 if (of_get_property(np, "fsl,dte-mode", NULL))
2238 sport->dte_mode = 1;
2240 if (of_get_property(np, "rts-gpios", NULL))
2241 sport->have_rtsgpio = 1;
2243 if (of_get_property(np, "fsl,inverted-tx", NULL))
2244 sport->inverted_tx = 1;
2246 if (of_get_property(np, "fsl,inverted-rx", NULL))
2247 sport->inverted_rx = 1;
2249 if (!of_property_read_u32_array(np, "fsl,dma-info", dma_buf_conf, 2)) {
2250 sport->rx_period_length = dma_buf_conf[0];
2251 sport->rx_periods = dma_buf_conf[1];
2253 sport->rx_period_length = RX_DMA_PERIOD_LEN;
2254 sport->rx_periods = RX_DMA_PERIODS;
2257 if (sport->port.line >= ARRAY_SIZE(imx_uart_ports)) {
2258 dev_err(&pdev->dev, "serial%d out of range\n",
2263 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2264 base = devm_ioremap_resource(&pdev->dev, res);
2266 return PTR_ERR(base);
2268 rxirq = platform_get_irq(pdev, 0);
2271 txirq = platform_get_irq_optional(pdev, 1);
2272 rtsirq = platform_get_irq_optional(pdev, 2);
2274 sport->port.dev = &pdev->dev;
2275 sport->port.mapbase = res->start;
2276 sport->port.membase = base;
2277 sport->port.type = PORT_IMX;
2278 sport->port.iotype = UPIO_MEM;
2279 sport->port.irq = rxirq;
2280 sport->port.fifosize = 32;
2281 sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE);
2282 sport->port.ops = &imx_uart_pops;
2283 sport->port.rs485_config = imx_uart_rs485_config;
2284 sport->port.flags = UPF_BOOT_AUTOCONF;
2285 timer_setup(&sport->timer, imx_uart_timeout, 0);
2287 sport->gpios = mctrl_gpio_init(&sport->port, 0);
2288 if (IS_ERR(sport->gpios))
2289 return PTR_ERR(sport->gpios);
2291 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2292 if (IS_ERR(sport->clk_ipg)) {
2293 ret = PTR_ERR(sport->clk_ipg);
2294 dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
2298 sport->clk_per = devm_clk_get(&pdev->dev, "per");
2299 if (IS_ERR(sport->clk_per)) {
2300 ret = PTR_ERR(sport->clk_per);
2301 dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
2305 sport->port.uartclk = clk_get_rate(sport->clk_per);
2307 /* For register access, we only need to enable the ipg clock. */
2308 ret = clk_prepare_enable(sport->clk_ipg);
2310 dev_err(&pdev->dev, "failed to enable per clk: %d\n", ret);
2314 /* initialize shadow register values */
2315 sport->ucr1 = readl(sport->port.membase + UCR1);
2316 sport->ucr2 = readl(sport->port.membase + UCR2);
2317 sport->ucr3 = readl(sport->port.membase + UCR3);
2318 sport->ucr4 = readl(sport->port.membase + UCR4);
2319 sport->ufcr = readl(sport->port.membase + UFCR);
2321 ret = uart_get_rs485_mode(&sport->port);
2323 clk_disable_unprepare(sport->clk_ipg);
2327 if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2328 (!sport->have_rtscts && !sport->have_rtsgpio))
2329 dev_err(&pdev->dev, "no RTS control, disabling rs485\n");
2332 * If using the i.MX UART RTS/CTS control then the RTS (CTS_B)
2333 * signal cannot be set low during transmission in case the
2334 * receiver is off (limitation of the i.MX UART IP).
2336 if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2337 sport->have_rtscts && !sport->have_rtsgpio &&
2338 (!(sport->port.rs485.flags & SER_RS485_RTS_ON_SEND) &&
2339 !(sport->port.rs485.flags & SER_RS485_RX_DURING_TX)))
2341 "low-active RTS not possible when receiver is off, enabling receiver\n");
2343 imx_uart_rs485_config(&sport->port, &sport->port.rs485);
2345 /* Disable interrupts before requesting them */
2346 ucr1 = imx_uart_readl(sport, UCR1);
2347 ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN);
2348 imx_uart_writel(sport, ucr1, UCR1);
2350 if (!imx_uart_is_imx1(sport) && sport->dte_mode) {
2352 * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI
2353 * and influences if UCR3_RI and UCR3_DCD changes the level of RI
2354 * and DCD (when they are outputs) or enables the respective
2355 * irqs. So set this bit early, i.e. before requesting irqs.
2357 u32 ufcr = imx_uart_readl(sport, UFCR);
2358 if (!(ufcr & UFCR_DCEDTE))
2359 imx_uart_writel(sport, ufcr | UFCR_DCEDTE, UFCR);
2362 * Disable UCR3_RI and UCR3_DCD irqs. They are also not
2363 * enabled later because they cannot be cleared
2364 * (confirmed on i.MX25) which makes them unusable.
2366 imx_uart_writel(sport,
2367 IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR,
2371 u32 ucr3 = UCR3_DSR;
2372 u32 ufcr = imx_uart_readl(sport, UFCR);
2373 if (ufcr & UFCR_DCEDTE)
2374 imx_uart_writel(sport, ufcr & ~UFCR_DCEDTE, UFCR);
2376 if (!imx_uart_is_imx1(sport))
2377 ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
2378 imx_uart_writel(sport, ucr3, UCR3);
2381 clk_disable_unprepare(sport->clk_ipg);
2383 hrtimer_init(&sport->trigger_start_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2384 hrtimer_init(&sport->trigger_stop_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2385 sport->trigger_start_tx.function = imx_trigger_start_tx;
2386 sport->trigger_stop_tx.function = imx_trigger_stop_tx;
2389 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
2390 * chips only have one interrupt.
2393 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_rxint, 0,
2394 dev_name(&pdev->dev), sport);
2396 dev_err(&pdev->dev, "failed to request rx irq: %d\n",
2401 ret = devm_request_irq(&pdev->dev, txirq, imx_uart_txint, 0,
2402 dev_name(&pdev->dev), sport);
2404 dev_err(&pdev->dev, "failed to request tx irq: %d\n",
2409 ret = devm_request_irq(&pdev->dev, rtsirq, imx_uart_rtsint, 0,
2410 dev_name(&pdev->dev), sport);
2412 dev_err(&pdev->dev, "failed to request rts irq: %d\n",
2417 ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_int, 0,
2418 dev_name(&pdev->dev), sport);
2420 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2425 imx_uart_ports[sport->port.line] = sport;
2427 platform_set_drvdata(pdev, sport);
2429 return uart_add_one_port(&imx_uart_uart_driver, &sport->port);
2432 static int imx_uart_remove(struct platform_device *pdev)
2434 struct imx_port *sport = platform_get_drvdata(pdev);
2436 return uart_remove_one_port(&imx_uart_uart_driver, &sport->port);
2439 static void imx_uart_restore_context(struct imx_port *sport)
2441 unsigned long flags;
2443 spin_lock_irqsave(&sport->port.lock, flags);
2444 if (!sport->context_saved) {
2445 spin_unlock_irqrestore(&sport->port.lock, flags);
2449 imx_uart_writel(sport, sport->saved_reg[4], UFCR);
2450 imx_uart_writel(sport, sport->saved_reg[5], UESC);
2451 imx_uart_writel(sport, sport->saved_reg[6], UTIM);
2452 imx_uart_writel(sport, sport->saved_reg[7], UBIR);
2453 imx_uart_writel(sport, sport->saved_reg[8], UBMR);
2454 imx_uart_writel(sport, sport->saved_reg[9], IMX21_UTS);
2455 imx_uart_writel(sport, sport->saved_reg[0], UCR1);
2456 imx_uart_writel(sport, sport->saved_reg[1] | UCR2_SRST, UCR2);
2457 imx_uart_writel(sport, sport->saved_reg[2], UCR3);
2458 imx_uart_writel(sport, sport->saved_reg[3], UCR4);
2459 sport->context_saved = false;
2460 spin_unlock_irqrestore(&sport->port.lock, flags);
2463 static void imx_uart_save_context(struct imx_port *sport)
2465 unsigned long flags;
2467 /* Save necessary regs */
2468 spin_lock_irqsave(&sport->port.lock, flags);
2469 sport->saved_reg[0] = imx_uart_readl(sport, UCR1);
2470 sport->saved_reg[1] = imx_uart_readl(sport, UCR2);
2471 sport->saved_reg[2] = imx_uart_readl(sport, UCR3);
2472 sport->saved_reg[3] = imx_uart_readl(sport, UCR4);
2473 sport->saved_reg[4] = imx_uart_readl(sport, UFCR);
2474 sport->saved_reg[5] = imx_uart_readl(sport, UESC);
2475 sport->saved_reg[6] = imx_uart_readl(sport, UTIM);
2476 sport->saved_reg[7] = imx_uart_readl(sport, UBIR);
2477 sport->saved_reg[8] = imx_uart_readl(sport, UBMR);
2478 sport->saved_reg[9] = imx_uart_readl(sport, IMX21_UTS);
2479 sport->context_saved = true;
2480 spin_unlock_irqrestore(&sport->port.lock, flags);
2483 static void imx_uart_enable_wakeup(struct imx_port *sport, bool on)
2487 ucr3 = imx_uart_readl(sport, UCR3);
2489 imx_uart_writel(sport, USR1_AWAKE, USR1);
2490 ucr3 |= UCR3_AWAKEN;
2492 ucr3 &= ~UCR3_AWAKEN;
2494 imx_uart_writel(sport, ucr3, UCR3);
2496 if (sport->have_rtscts) {
2497 u32 ucr1 = imx_uart_readl(sport, UCR1);
2499 imx_uart_writel(sport, USR1_RTSD, USR1);
2500 ucr1 |= UCR1_RTSDEN;
2502 ucr1 &= ~UCR1_RTSDEN;
2504 imx_uart_writel(sport, ucr1, UCR1);
2508 static int imx_uart_suspend_noirq(struct device *dev)
2510 struct imx_port *sport = dev_get_drvdata(dev);
2512 imx_uart_save_context(sport);
2514 clk_disable(sport->clk_ipg);
2516 pinctrl_pm_select_sleep_state(dev);
2521 static int imx_uart_resume_noirq(struct device *dev)
2523 struct imx_port *sport = dev_get_drvdata(dev);
2526 pinctrl_pm_select_default_state(dev);
2528 ret = clk_enable(sport->clk_ipg);
2532 imx_uart_restore_context(sport);
2537 static int imx_uart_suspend(struct device *dev)
2539 struct imx_port *sport = dev_get_drvdata(dev);
2542 uart_suspend_port(&imx_uart_uart_driver, &sport->port);
2543 disable_irq(sport->port.irq);
2545 ret = clk_prepare_enable(sport->clk_ipg);
2549 /* enable wakeup from i.MX UART */
2550 imx_uart_enable_wakeup(sport, true);
2555 static int imx_uart_resume(struct device *dev)
2557 struct imx_port *sport = dev_get_drvdata(dev);
2559 /* disable wakeup from i.MX UART */
2560 imx_uart_enable_wakeup(sport, false);
2562 uart_resume_port(&imx_uart_uart_driver, &sport->port);
2563 enable_irq(sport->port.irq);
2565 clk_disable_unprepare(sport->clk_ipg);
2570 static int imx_uart_freeze(struct device *dev)
2572 struct imx_port *sport = dev_get_drvdata(dev);
2574 uart_suspend_port(&imx_uart_uart_driver, &sport->port);
2576 return clk_prepare_enable(sport->clk_ipg);
2579 static int imx_uart_thaw(struct device *dev)
2581 struct imx_port *sport = dev_get_drvdata(dev);
2583 uart_resume_port(&imx_uart_uart_driver, &sport->port);
2585 clk_disable_unprepare(sport->clk_ipg);
2590 static const struct dev_pm_ops imx_uart_pm_ops = {
2591 .suspend_noirq = imx_uart_suspend_noirq,
2592 .resume_noirq = imx_uart_resume_noirq,
2593 .freeze_noirq = imx_uart_suspend_noirq,
2594 .restore_noirq = imx_uart_resume_noirq,
2595 .suspend = imx_uart_suspend,
2596 .resume = imx_uart_resume,
2597 .freeze = imx_uart_freeze,
2598 .thaw = imx_uart_thaw,
2599 .restore = imx_uart_thaw,
2602 static struct platform_driver imx_uart_platform_driver = {
2603 .probe = imx_uart_probe,
2604 .remove = imx_uart_remove,
2608 .of_match_table = imx_uart_dt_ids,
2609 .pm = &imx_uart_pm_ops,
2613 static int __init imx_uart_init(void)
2615 int ret = uart_register_driver(&imx_uart_uart_driver);
2620 ret = platform_driver_register(&imx_uart_platform_driver);
2622 uart_unregister_driver(&imx_uart_uart_driver);
2627 static void __exit imx_uart_exit(void)
2629 platform_driver_unregister(&imx_uart_platform_driver);
2630 uart_unregister_driver(&imx_uart_uart_driver);
2633 module_init(imx_uart_init);
2634 module_exit(imx_uart_exit);
2636 MODULE_AUTHOR("Sascha Hauer");
2637 MODULE_DESCRIPTION("IMX generic serial port driver");
2638 MODULE_LICENSE("GPL");
2639 MODULE_ALIAS("platform:imx-uart");