serial: imx: Only wakeup via RTSDEN bit if the system has RTS/CTS
[linux/fpc-iii.git] / drivers / tty / serial / imx.c
blobe9145ed0d6e77be9852dd70fa92ecfb40bac5956
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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
9 */
11 #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12 #define SUPPORT_SYSRQ
13 #endif
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/console.h>
19 #include <linux/sysrq.h>
20 #include <linux/platform_device.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial_core.h>
24 #include <linux/serial.h>
25 #include <linux/clk.h>
26 #include <linux/delay.h>
27 #include <linux/rational.h>
28 #include <linux/slab.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/io.h>
32 #include <linux/dma-mapping.h>
34 #include <asm/irq.h>
35 #include <linux/platform_data/serial-imx.h>
36 #include <linux/platform_data/dma-imx.h>
38 #include "serial_mctrl_gpio.h"
40 /* Register definitions */
41 #define URXD0 0x0 /* Receiver Register */
42 #define URTX0 0x40 /* Transmitter Register */
43 #define UCR1 0x80 /* Control Register 1 */
44 #define UCR2 0x84 /* Control Register 2 */
45 #define UCR3 0x88 /* Control Register 3 */
46 #define UCR4 0x8c /* Control Register 4 */
47 #define UFCR 0x90 /* FIFO Control Register */
48 #define USR1 0x94 /* Status Register 1 */
49 #define USR2 0x98 /* Status Register 2 */
50 #define UESC 0x9c /* Escape Character Register */
51 #define UTIM 0xa0 /* Escape Timer Register */
52 #define UBIR 0xa4 /* BRM Incremental Register */
53 #define UBMR 0xa8 /* BRM Modulator Register */
54 #define UBRC 0xac /* Baud Rate Count Register */
55 #define IMX21_ONEMS 0xb0 /* One Millisecond register */
56 #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
57 #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
59 /* UART Control Register Bit Fields.*/
60 #define URXD_DUMMY_READ (1<<16)
61 #define URXD_CHARRDY (1<<15)
62 #define URXD_ERR (1<<14)
63 #define URXD_OVRRUN (1<<13)
64 #define URXD_FRMERR (1<<12)
65 #define URXD_BRK (1<<11)
66 #define URXD_PRERR (1<<10)
67 #define URXD_RX_DATA (0xFF<<0)
68 #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
69 #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
70 #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
71 #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
72 #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
73 #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
74 #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
75 #define UCR1_IREN (1<<7) /* Infrared interface enable */
76 #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
77 #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
78 #define UCR1_SNDBRK (1<<4) /* Send break */
79 #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
80 #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
81 #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
82 #define UCR1_DOZE (1<<1) /* Doze */
83 #define UCR1_UARTEN (1<<0) /* UART enabled */
84 #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
85 #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
86 #define UCR2_CTSC (1<<13) /* CTS pin control */
87 #define UCR2_CTS (1<<12) /* Clear to send */
88 #define UCR2_ESCEN (1<<11) /* Escape enable */
89 #define UCR2_PREN (1<<8) /* Parity enable */
90 #define UCR2_PROE (1<<7) /* Parity odd/even */
91 #define UCR2_STPB (1<<6) /* Stop */
92 #define UCR2_WS (1<<5) /* Word size */
93 #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
94 #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
95 #define UCR2_TXEN (1<<2) /* Transmitter enabled */
96 #define UCR2_RXEN (1<<1) /* Receiver enabled */
97 #define UCR2_SRST (1<<0) /* SW reset */
98 #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
99 #define UCR3_PARERREN (1<<12) /* Parity enable */
100 #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
101 #define UCR3_DSR (1<<10) /* Data set ready */
102 #define UCR3_DCD (1<<9) /* Data carrier detect */
103 #define UCR3_RI (1<<8) /* Ring indicator */
104 #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
105 #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
106 #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
107 #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
108 #define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */
109 #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
110 #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
111 #define UCR3_BPEN (1<<0) /* Preset registers enable */
112 #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
113 #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
114 #define UCR4_INVR (1<<9) /* Inverted infrared reception */
115 #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
116 #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
117 #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
118 #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
119 #define UCR4_IRSC (1<<5) /* IR special case */
120 #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
121 #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
122 #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
123 #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
124 #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
125 #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
126 #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
127 #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
128 #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
129 #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
130 #define USR1_RTSS (1<<14) /* RTS pin status */
131 #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
132 #define USR1_RTSD (1<<12) /* RTS delta */
133 #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
134 #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
135 #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
136 #define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */
137 #define USR1_DTRD (1<<7) /* DTR Delta */
138 #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
139 #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
140 #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
141 #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
142 #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
143 #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
144 #define USR2_IDLE (1<<12) /* Idle condition */
145 #define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */
146 #define USR2_RIIN (1<<9) /* Ring Indicator Input */
147 #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
148 #define USR2_WAKE (1<<7) /* Wake */
149 #define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */
150 #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
151 #define USR2_TXDC (1<<3) /* Transmitter complete */
152 #define USR2_BRCD (1<<2) /* Break condition */
153 #define USR2_ORE (1<<1) /* Overrun error */
154 #define USR2_RDR (1<<0) /* Recv data ready */
155 #define UTS_FRCPERR (1<<13) /* Force parity error */
156 #define UTS_LOOP (1<<12) /* Loop tx and rx */
157 #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
158 #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
159 #define UTS_TXFULL (1<<4) /* TxFIFO full */
160 #define UTS_RXFULL (1<<3) /* RxFIFO full */
161 #define UTS_SOFTRST (1<<0) /* Software reset */
163 /* We've been assigned a range on the "Low-density serial ports" major */
164 #define SERIAL_IMX_MAJOR 207
165 #define MINOR_START 16
166 #define DEV_NAME "ttymxc"
169 * This determines how often we check the modem status signals
170 * for any change. They generally aren't connected to an IRQ
171 * so we have to poll them. We also check immediately before
172 * filling the TX fifo incase CTS has been dropped.
174 #define MCTRL_TIMEOUT (250*HZ/1000)
176 #define DRIVER_NAME "IMX-uart"
178 #define UART_NR 8
180 /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
181 enum imx_uart_type {
182 IMX1_UART,
183 IMX21_UART,
184 IMX53_UART,
185 IMX6Q_UART,
188 /* device type dependent stuff */
189 struct imx_uart_data {
190 unsigned uts_reg;
191 enum imx_uart_type devtype;
194 struct imx_port {
195 struct uart_port port;
196 struct timer_list timer;
197 unsigned int old_status;
198 unsigned int have_rtscts:1;
199 unsigned int have_rtsgpio:1;
200 unsigned int dte_mode:1;
201 struct clk *clk_ipg;
202 struct clk *clk_per;
203 const struct imx_uart_data *devdata;
205 struct mctrl_gpios *gpios;
207 /* DMA fields */
208 unsigned int dma_is_inited:1;
209 unsigned int dma_is_enabled:1;
210 unsigned int dma_is_rxing:1;
211 unsigned int dma_is_txing:1;
212 struct dma_chan *dma_chan_rx, *dma_chan_tx;
213 struct scatterlist rx_sgl, tx_sgl[2];
214 void *rx_buf;
215 struct circ_buf rx_ring;
216 unsigned int rx_periods;
217 dma_cookie_t rx_cookie;
218 unsigned int tx_bytes;
219 unsigned int dma_tx_nents;
220 unsigned int saved_reg[10];
221 bool context_saved;
224 struct imx_port_ucrs {
225 unsigned int ucr1;
226 unsigned int ucr2;
227 unsigned int ucr3;
230 static struct imx_uart_data imx_uart_devdata[] = {
231 [IMX1_UART] = {
232 .uts_reg = IMX1_UTS,
233 .devtype = IMX1_UART,
235 [IMX21_UART] = {
236 .uts_reg = IMX21_UTS,
237 .devtype = IMX21_UART,
239 [IMX53_UART] = {
240 .uts_reg = IMX21_UTS,
241 .devtype = IMX53_UART,
243 [IMX6Q_UART] = {
244 .uts_reg = IMX21_UTS,
245 .devtype = IMX6Q_UART,
249 static const struct platform_device_id imx_uart_devtype[] = {
251 .name = "imx1-uart",
252 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX1_UART],
253 }, {
254 .name = "imx21-uart",
255 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
256 }, {
257 .name = "imx53-uart",
258 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX53_UART],
259 }, {
260 .name = "imx6q-uart",
261 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX6Q_UART],
262 }, {
263 /* sentinel */
266 MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
268 static const struct of_device_id imx_uart_dt_ids[] = {
269 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
270 { .compatible = "fsl,imx53-uart", .data = &imx_uart_devdata[IMX53_UART], },
271 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
272 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
273 { /* sentinel */ }
275 MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
277 static inline unsigned uts_reg(struct imx_port *sport)
279 return sport->devdata->uts_reg;
282 static inline int is_imx1_uart(struct imx_port *sport)
284 return sport->devdata->devtype == IMX1_UART;
287 static inline int is_imx21_uart(struct imx_port *sport)
289 return sport->devdata->devtype == IMX21_UART;
292 static inline int is_imx53_uart(struct imx_port *sport)
294 return sport->devdata->devtype == IMX53_UART;
297 static inline int is_imx6q_uart(struct imx_port *sport)
299 return sport->devdata->devtype == IMX6Q_UART;
302 * Save and restore functions for UCR1, UCR2 and UCR3 registers
304 #if defined(CONFIG_SERIAL_IMX_CONSOLE)
305 static void imx_port_ucrs_save(struct uart_port *port,
306 struct imx_port_ucrs *ucr)
308 /* save control registers */
309 ucr->ucr1 = readl(port->membase + UCR1);
310 ucr->ucr2 = readl(port->membase + UCR2);
311 ucr->ucr3 = readl(port->membase + UCR3);
314 static void imx_port_ucrs_restore(struct uart_port *port,
315 struct imx_port_ucrs *ucr)
317 /* restore control registers */
318 writel(ucr->ucr1, port->membase + UCR1);
319 writel(ucr->ucr2, port->membase + UCR2);
320 writel(ucr->ucr3, port->membase + UCR3);
322 #endif
324 static void imx_port_rts_active(struct imx_port *sport, unsigned long *ucr2)
326 *ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
328 sport->port.mctrl |= TIOCM_RTS;
329 mctrl_gpio_set(sport->gpios, sport->port.mctrl);
332 static void imx_port_rts_inactive(struct imx_port *sport, unsigned long *ucr2)
334 *ucr2 &= ~UCR2_CTSC;
335 *ucr2 |= UCR2_CTS;
337 sport->port.mctrl &= ~TIOCM_RTS;
338 mctrl_gpio_set(sport->gpios, sport->port.mctrl);
341 static void imx_port_rts_auto(struct imx_port *sport, unsigned long *ucr2)
343 *ucr2 |= UCR2_CTSC;
347 * interrupts disabled on entry
349 static void imx_stop_tx(struct uart_port *port)
351 struct imx_port *sport = (struct imx_port *)port;
352 unsigned long temp;
355 * We are maybe in the SMP context, so if the DMA TX thread is running
356 * on other cpu, we have to wait for it to finish.
358 if (sport->dma_is_enabled && sport->dma_is_txing)
359 return;
361 temp = readl(port->membase + UCR1);
362 writel(temp & ~UCR1_TXMPTYEN, port->membase + UCR1);
364 /* in rs485 mode disable transmitter if shifter is empty */
365 if (port->rs485.flags & SER_RS485_ENABLED &&
366 readl(port->membase + USR2) & USR2_TXDC) {
367 temp = readl(port->membase + UCR2);
368 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
369 imx_port_rts_active(sport, &temp);
370 else
371 imx_port_rts_inactive(sport, &temp);
372 temp |= UCR2_RXEN;
373 writel(temp, port->membase + UCR2);
375 temp = readl(port->membase + UCR4);
376 temp &= ~UCR4_TCEN;
377 writel(temp, port->membase + UCR4);
382 * interrupts disabled on entry
384 static void imx_stop_rx(struct uart_port *port)
386 struct imx_port *sport = (struct imx_port *)port;
387 unsigned long temp;
389 if (sport->dma_is_enabled && sport->dma_is_rxing) {
390 if (sport->port.suspended) {
391 dmaengine_terminate_all(sport->dma_chan_rx);
392 sport->dma_is_rxing = 0;
393 } else {
394 return;
398 temp = readl(sport->port.membase + UCR2);
399 writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
401 /* disable the `Receiver Ready Interrrupt` */
402 temp = readl(sport->port.membase + UCR1);
403 writel(temp & ~UCR1_RRDYEN, sport->port.membase + UCR1);
407 * Set the modem control timer to fire immediately.
409 static void imx_enable_ms(struct uart_port *port)
411 struct imx_port *sport = (struct imx_port *)port;
413 mod_timer(&sport->timer, jiffies);
415 mctrl_gpio_enable_ms(sport->gpios);
418 static void imx_dma_tx(struct imx_port *sport);
419 static inline void imx_transmit_buffer(struct imx_port *sport)
421 struct circ_buf *xmit = &sport->port.state->xmit;
422 unsigned long temp;
424 if (sport->port.x_char) {
425 /* Send next char */
426 writel(sport->port.x_char, sport->port.membase + URTX0);
427 sport->port.icount.tx++;
428 sport->port.x_char = 0;
429 return;
432 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
433 imx_stop_tx(&sport->port);
434 return;
437 if (sport->dma_is_enabled) {
439 * We've just sent a X-char Ensure the TX DMA is enabled
440 * and the TX IRQ is disabled.
442 temp = readl(sport->port.membase + UCR1);
443 temp &= ~UCR1_TXMPTYEN;
444 if (sport->dma_is_txing) {
445 temp |= UCR1_TDMAEN;
446 writel(temp, sport->port.membase + UCR1);
447 } else {
448 writel(temp, sport->port.membase + UCR1);
449 imx_dma_tx(sport);
453 if (sport->dma_is_txing)
454 return;
456 while (!uart_circ_empty(xmit) &&
457 !(readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)) {
458 /* send xmit->buf[xmit->tail]
459 * out the port here */
460 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
461 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
462 sport->port.icount.tx++;
465 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
466 uart_write_wakeup(&sport->port);
468 if (uart_circ_empty(xmit))
469 imx_stop_tx(&sport->port);
472 static void dma_tx_callback(void *data)
474 struct imx_port *sport = data;
475 struct scatterlist *sgl = &sport->tx_sgl[0];
476 struct circ_buf *xmit = &sport->port.state->xmit;
477 unsigned long flags;
478 unsigned long temp;
480 spin_lock_irqsave(&sport->port.lock, flags);
482 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
484 temp = readl(sport->port.membase + UCR1);
485 temp &= ~UCR1_TDMAEN;
486 writel(temp, sport->port.membase + UCR1);
488 /* update the stat */
489 xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
490 sport->port.icount.tx += sport->tx_bytes;
492 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
494 sport->dma_is_txing = 0;
496 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
497 uart_write_wakeup(&sport->port);
499 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
500 imx_dma_tx(sport);
502 spin_unlock_irqrestore(&sport->port.lock, flags);
505 static void imx_dma_tx(struct imx_port *sport)
507 struct circ_buf *xmit = &sport->port.state->xmit;
508 struct scatterlist *sgl = sport->tx_sgl;
509 struct dma_async_tx_descriptor *desc;
510 struct dma_chan *chan = sport->dma_chan_tx;
511 struct device *dev = sport->port.dev;
512 unsigned long temp;
513 int ret;
515 if (sport->dma_is_txing)
516 return;
518 sport->tx_bytes = uart_circ_chars_pending(xmit);
520 if (xmit->tail < xmit->head) {
521 sport->dma_tx_nents = 1;
522 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
523 } else {
524 sport->dma_tx_nents = 2;
525 sg_init_table(sgl, 2);
526 sg_set_buf(sgl, xmit->buf + xmit->tail,
527 UART_XMIT_SIZE - xmit->tail);
528 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
531 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
532 if (ret == 0) {
533 dev_err(dev, "DMA mapping error for TX.\n");
534 return;
536 desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
537 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
538 if (!desc) {
539 dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
540 DMA_TO_DEVICE);
541 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
542 return;
544 desc->callback = dma_tx_callback;
545 desc->callback_param = sport;
547 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
548 uart_circ_chars_pending(xmit));
550 temp = readl(sport->port.membase + UCR1);
551 temp |= UCR1_TDMAEN;
552 writel(temp, sport->port.membase + UCR1);
554 /* fire it */
555 sport->dma_is_txing = 1;
556 dmaengine_submit(desc);
557 dma_async_issue_pending(chan);
558 return;
562 * interrupts disabled on entry
564 static void imx_start_tx(struct uart_port *port)
566 struct imx_port *sport = (struct imx_port *)port;
567 unsigned long temp;
569 if (port->rs485.flags & SER_RS485_ENABLED) {
570 temp = readl(port->membase + UCR2);
571 if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
572 imx_port_rts_active(sport, &temp);
573 else
574 imx_port_rts_inactive(sport, &temp);
575 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
576 temp &= ~UCR2_RXEN;
577 writel(temp, port->membase + UCR2);
579 /* enable transmitter and shifter empty irq */
580 temp = readl(port->membase + UCR4);
581 temp |= UCR4_TCEN;
582 writel(temp, port->membase + UCR4);
585 if (!sport->dma_is_enabled) {
586 temp = readl(sport->port.membase + UCR1);
587 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
590 if (sport->dma_is_enabled) {
591 if (sport->port.x_char) {
592 /* We have X-char to send, so enable TX IRQ and
593 * disable TX DMA to let TX interrupt to send X-char */
594 temp = readl(sport->port.membase + UCR1);
595 temp &= ~UCR1_TDMAEN;
596 temp |= UCR1_TXMPTYEN;
597 writel(temp, sport->port.membase + UCR1);
598 return;
601 if (!uart_circ_empty(&port->state->xmit) &&
602 !uart_tx_stopped(port))
603 imx_dma_tx(sport);
604 return;
608 static irqreturn_t imx_rtsint(int irq, void *dev_id)
610 struct imx_port *sport = dev_id;
611 unsigned int val;
612 unsigned long flags;
614 spin_lock_irqsave(&sport->port.lock, flags);
616 writel(USR1_RTSD, sport->port.membase + USR1);
617 val = readl(sport->port.membase + USR1) & USR1_RTSS;
618 uart_handle_cts_change(&sport->port, !!val);
619 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
621 spin_unlock_irqrestore(&sport->port.lock, flags);
622 return IRQ_HANDLED;
625 static irqreturn_t imx_txint(int irq, void *dev_id)
627 struct imx_port *sport = dev_id;
628 unsigned long flags;
630 spin_lock_irqsave(&sport->port.lock, flags);
631 imx_transmit_buffer(sport);
632 spin_unlock_irqrestore(&sport->port.lock, flags);
633 return IRQ_HANDLED;
636 static irqreturn_t imx_rxint(int irq, void *dev_id)
638 struct imx_port *sport = dev_id;
639 unsigned int rx, flg, ignored = 0;
640 struct tty_port *port = &sport->port.state->port;
641 unsigned long flags, temp;
643 spin_lock_irqsave(&sport->port.lock, flags);
645 while (readl(sport->port.membase + USR2) & USR2_RDR) {
646 flg = TTY_NORMAL;
647 sport->port.icount.rx++;
649 rx = readl(sport->port.membase + URXD0);
651 temp = readl(sport->port.membase + USR2);
652 if (temp & USR2_BRCD) {
653 writel(USR2_BRCD, sport->port.membase + USR2);
654 if (uart_handle_break(&sport->port))
655 continue;
658 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
659 continue;
661 if (unlikely(rx & URXD_ERR)) {
662 if (rx & URXD_BRK)
663 sport->port.icount.brk++;
664 else if (rx & URXD_PRERR)
665 sport->port.icount.parity++;
666 else if (rx & URXD_FRMERR)
667 sport->port.icount.frame++;
668 if (rx & URXD_OVRRUN)
669 sport->port.icount.overrun++;
671 if (rx & sport->port.ignore_status_mask) {
672 if (++ignored > 100)
673 goto out;
674 continue;
677 rx &= (sport->port.read_status_mask | 0xFF);
679 if (rx & URXD_BRK)
680 flg = TTY_BREAK;
681 else if (rx & URXD_PRERR)
682 flg = TTY_PARITY;
683 else if (rx & URXD_FRMERR)
684 flg = TTY_FRAME;
685 if (rx & URXD_OVRRUN)
686 flg = TTY_OVERRUN;
688 #ifdef SUPPORT_SYSRQ
689 sport->port.sysrq = 0;
690 #endif
693 if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
694 goto out;
696 if (tty_insert_flip_char(port, rx, flg) == 0)
697 sport->port.icount.buf_overrun++;
700 out:
701 spin_unlock_irqrestore(&sport->port.lock, flags);
702 tty_flip_buffer_push(port);
703 return IRQ_HANDLED;
706 static void imx_disable_rx_int(struct imx_port *sport)
708 unsigned long temp;
710 /* disable the receiver ready and aging timer interrupts */
711 temp = readl(sport->port.membase + UCR1);
712 temp &= ~(UCR1_RRDYEN);
713 writel(temp, sport->port.membase + UCR1);
715 temp = readl(sport->port.membase + UCR2);
716 temp &= ~(UCR2_ATEN);
717 writel(temp, sport->port.membase + UCR2);
719 /* disable the rx errors interrupts */
720 temp = readl(sport->port.membase + UCR4);
721 temp &= ~UCR4_OREN;
722 writel(temp, sport->port.membase + UCR4);
725 static void clear_rx_errors(struct imx_port *sport);
728 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
730 static unsigned int imx_get_hwmctrl(struct imx_port *sport)
732 unsigned int tmp = TIOCM_DSR;
733 unsigned usr1 = readl(sport->port.membase + USR1);
734 unsigned usr2 = readl(sport->port.membase + USR2);
736 if (usr1 & USR1_RTSS)
737 tmp |= TIOCM_CTS;
739 /* in DCE mode DCDIN is always 0 */
740 if (!(usr2 & USR2_DCDIN))
741 tmp |= TIOCM_CAR;
743 if (sport->dte_mode)
744 if (!(readl(sport->port.membase + USR2) & USR2_RIIN))
745 tmp |= TIOCM_RI;
747 return tmp;
751 * Handle any change of modem status signal since we were last called.
753 static void imx_mctrl_check(struct imx_port *sport)
755 unsigned int status, changed;
757 status = imx_get_hwmctrl(sport);
758 changed = status ^ sport->old_status;
760 if (changed == 0)
761 return;
763 sport->old_status = status;
765 if (changed & TIOCM_RI && status & TIOCM_RI)
766 sport->port.icount.rng++;
767 if (changed & TIOCM_DSR)
768 sport->port.icount.dsr++;
769 if (changed & TIOCM_CAR)
770 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
771 if (changed & TIOCM_CTS)
772 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
774 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
777 static irqreturn_t imx_int(int irq, void *dev_id)
779 struct imx_port *sport = dev_id;
780 unsigned int sts;
781 unsigned int sts2;
782 irqreturn_t ret = IRQ_NONE;
784 sts = readl(sport->port.membase + USR1);
785 sts2 = readl(sport->port.membase + USR2);
787 if (!sport->dma_is_enabled && (sts & (USR1_RRDY | USR1_AGTIM))) {
788 imx_rxint(irq, dev_id);
789 ret = IRQ_HANDLED;
792 if ((sts & USR1_TRDY &&
793 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN) ||
794 (sts2 & USR2_TXDC &&
795 readl(sport->port.membase + UCR4) & UCR4_TCEN)) {
796 imx_txint(irq, dev_id);
797 ret = IRQ_HANDLED;
800 if (sts & USR1_DTRD) {
801 unsigned long flags;
803 if (sts & USR1_DTRD)
804 writel(USR1_DTRD, sport->port.membase + USR1);
806 spin_lock_irqsave(&sport->port.lock, flags);
807 imx_mctrl_check(sport);
808 spin_unlock_irqrestore(&sport->port.lock, flags);
810 ret = IRQ_HANDLED;
813 if (sts & USR1_RTSD) {
814 imx_rtsint(irq, dev_id);
815 ret = IRQ_HANDLED;
818 if (sts & USR1_AWAKE) {
819 writel(USR1_AWAKE, sport->port.membase + USR1);
820 ret = IRQ_HANDLED;
823 if (sts2 & USR2_ORE) {
824 sport->port.icount.overrun++;
825 writel(USR2_ORE, sport->port.membase + USR2);
826 ret = IRQ_HANDLED;
829 return ret;
833 * Return TIOCSER_TEMT when transmitter is not busy.
835 static unsigned int imx_tx_empty(struct uart_port *port)
837 struct imx_port *sport = (struct imx_port *)port;
838 unsigned int ret;
840 ret = (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
842 /* If the TX DMA is working, return 0. */
843 if (sport->dma_is_enabled && sport->dma_is_txing)
844 ret = 0;
846 return ret;
849 static unsigned int imx_get_mctrl(struct uart_port *port)
851 struct imx_port *sport = (struct imx_port *)port;
852 unsigned int ret = imx_get_hwmctrl(sport);
854 mctrl_gpio_get(sport->gpios, &ret);
856 return ret;
859 static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
861 struct imx_port *sport = (struct imx_port *)port;
862 unsigned long temp;
864 if (!(port->rs485.flags & SER_RS485_ENABLED)) {
865 temp = readl(sport->port.membase + UCR2);
866 temp &= ~(UCR2_CTS | UCR2_CTSC);
867 if (mctrl & TIOCM_RTS)
868 temp |= UCR2_CTS | UCR2_CTSC;
869 writel(temp, sport->port.membase + UCR2);
872 temp = readl(sport->port.membase + UCR3) & ~UCR3_DSR;
873 if (!(mctrl & TIOCM_DTR))
874 temp |= UCR3_DSR;
875 writel(temp, sport->port.membase + UCR3);
877 temp = readl(sport->port.membase + uts_reg(sport)) & ~UTS_LOOP;
878 if (mctrl & TIOCM_LOOP)
879 temp |= UTS_LOOP;
880 writel(temp, sport->port.membase + uts_reg(sport));
882 mctrl_gpio_set(sport->gpios, mctrl);
886 * Interrupts always disabled.
888 static void imx_break_ctl(struct uart_port *port, int break_state)
890 struct imx_port *sport = (struct imx_port *)port;
891 unsigned long flags, temp;
893 spin_lock_irqsave(&sport->port.lock, flags);
895 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
897 if (break_state != 0)
898 temp |= UCR1_SNDBRK;
900 writel(temp, sport->port.membase + UCR1);
902 spin_unlock_irqrestore(&sport->port.lock, flags);
906 * This is our per-port timeout handler, for checking the
907 * modem status signals.
909 static void imx_timeout(struct timer_list *t)
911 struct imx_port *sport = from_timer(sport, t, timer);
912 unsigned long flags;
914 if (sport->port.state) {
915 spin_lock_irqsave(&sport->port.lock, flags);
916 imx_mctrl_check(sport);
917 spin_unlock_irqrestore(&sport->port.lock, flags);
919 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
923 #define RX_BUF_SIZE (PAGE_SIZE)
926 * There are two kinds of RX DMA interrupts(such as in the MX6Q):
927 * [1] the RX DMA buffer is full.
928 * [2] the aging timer expires
930 * Condition [2] is triggered when a character has been sitting in the FIFO
931 * for at least 8 byte durations.
933 static void dma_rx_callback(void *data)
935 struct imx_port *sport = data;
936 struct dma_chan *chan = sport->dma_chan_rx;
937 struct scatterlist *sgl = &sport->rx_sgl;
938 struct tty_port *port = &sport->port.state->port;
939 struct dma_tx_state state;
940 struct circ_buf *rx_ring = &sport->rx_ring;
941 enum dma_status status;
942 unsigned int w_bytes = 0;
943 unsigned int r_bytes;
944 unsigned int bd_size;
946 status = dmaengine_tx_status(chan, (dma_cookie_t)0, &state);
948 if (status == DMA_ERROR) {
949 dev_err(sport->port.dev, "DMA transaction error.\n");
950 clear_rx_errors(sport);
951 return;
954 if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
957 * The state-residue variable represents the empty space
958 * relative to the entire buffer. Taking this in consideration
959 * the head is always calculated base on the buffer total
960 * length - DMA transaction residue. The UART script from the
961 * SDMA firmware will jump to the next buffer descriptor,
962 * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4).
963 * Taking this in consideration the tail is always at the
964 * beginning of the buffer descriptor that contains the head.
967 /* Calculate the head */
968 rx_ring->head = sg_dma_len(sgl) - state.residue;
970 /* Calculate the tail. */
971 bd_size = sg_dma_len(sgl) / sport->rx_periods;
972 rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size;
974 if (rx_ring->head <= sg_dma_len(sgl) &&
975 rx_ring->head > rx_ring->tail) {
977 /* Move data from tail to head */
978 r_bytes = rx_ring->head - rx_ring->tail;
980 /* CPU claims ownership of RX DMA buffer */
981 dma_sync_sg_for_cpu(sport->port.dev, sgl, 1,
982 DMA_FROM_DEVICE);
984 w_bytes = tty_insert_flip_string(port,
985 sport->rx_buf + rx_ring->tail, r_bytes);
987 /* UART retrieves ownership of RX DMA buffer */
988 dma_sync_sg_for_device(sport->port.dev, sgl, 1,
989 DMA_FROM_DEVICE);
991 if (w_bytes != r_bytes)
992 sport->port.icount.buf_overrun++;
994 sport->port.icount.rx += w_bytes;
995 } else {
996 WARN_ON(rx_ring->head > sg_dma_len(sgl));
997 WARN_ON(rx_ring->head <= rx_ring->tail);
1001 if (w_bytes) {
1002 tty_flip_buffer_push(port);
1003 dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes);
1007 /* RX DMA buffer periods */
1008 #define RX_DMA_PERIODS 4
1010 static int start_rx_dma(struct imx_port *sport)
1012 struct scatterlist *sgl = &sport->rx_sgl;
1013 struct dma_chan *chan = sport->dma_chan_rx;
1014 struct device *dev = sport->port.dev;
1015 struct dma_async_tx_descriptor *desc;
1016 int ret;
1018 sport->rx_ring.head = 0;
1019 sport->rx_ring.tail = 0;
1020 sport->rx_periods = RX_DMA_PERIODS;
1022 sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
1023 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1024 if (ret == 0) {
1025 dev_err(dev, "DMA mapping error for RX.\n");
1026 return -EINVAL;
1029 desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl),
1030 sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods,
1031 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
1033 if (!desc) {
1034 dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1035 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
1036 return -EINVAL;
1038 desc->callback = dma_rx_callback;
1039 desc->callback_param = sport;
1041 dev_dbg(dev, "RX: prepare for the DMA.\n");
1042 sport->dma_is_rxing = 1;
1043 sport->rx_cookie = dmaengine_submit(desc);
1044 dma_async_issue_pending(chan);
1045 return 0;
1048 static void clear_rx_errors(struct imx_port *sport)
1050 unsigned int status_usr1, status_usr2;
1052 status_usr1 = readl(sport->port.membase + USR1);
1053 status_usr2 = readl(sport->port.membase + USR2);
1055 if (status_usr2 & USR2_BRCD) {
1056 sport->port.icount.brk++;
1057 writel(USR2_BRCD, sport->port.membase + USR2);
1058 } else if (status_usr1 & USR1_FRAMERR) {
1059 sport->port.icount.frame++;
1060 writel(USR1_FRAMERR, sport->port.membase + USR1);
1061 } else if (status_usr1 & USR1_PARITYERR) {
1062 sport->port.icount.parity++;
1063 writel(USR1_PARITYERR, sport->port.membase + USR1);
1066 if (status_usr2 & USR2_ORE) {
1067 sport->port.icount.overrun++;
1068 writel(USR2_ORE, sport->port.membase + USR2);
1073 #define TXTL_DEFAULT 2 /* reset default */
1074 #define RXTL_DEFAULT 1 /* reset default */
1075 #define TXTL_DMA 8 /* DMA burst setting */
1076 #define RXTL_DMA 9 /* DMA burst setting */
1078 static void imx_setup_ufcr(struct imx_port *sport,
1079 unsigned char txwl, unsigned char rxwl)
1081 unsigned int val;
1083 /* set receiver / transmitter trigger level */
1084 val = readl(sport->port.membase + UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
1085 val |= txwl << UFCR_TXTL_SHF | rxwl;
1086 writel(val, sport->port.membase + UFCR);
1089 static void imx_uart_dma_exit(struct imx_port *sport)
1091 if (sport->dma_chan_rx) {
1092 dmaengine_terminate_sync(sport->dma_chan_rx);
1093 dma_release_channel(sport->dma_chan_rx);
1094 sport->dma_chan_rx = NULL;
1095 sport->rx_cookie = -EINVAL;
1096 kfree(sport->rx_buf);
1097 sport->rx_buf = NULL;
1100 if (sport->dma_chan_tx) {
1101 dmaengine_terminate_sync(sport->dma_chan_tx);
1102 dma_release_channel(sport->dma_chan_tx);
1103 sport->dma_chan_tx = NULL;
1106 sport->dma_is_inited = 0;
1109 static int imx_uart_dma_init(struct imx_port *sport)
1111 struct dma_slave_config slave_config = {};
1112 struct device *dev = sport->port.dev;
1113 int ret;
1115 /* Prepare for RX : */
1116 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1117 if (!sport->dma_chan_rx) {
1118 dev_dbg(dev, "cannot get the DMA channel.\n");
1119 ret = -EINVAL;
1120 goto err;
1123 slave_config.direction = DMA_DEV_TO_MEM;
1124 slave_config.src_addr = sport->port.mapbase + URXD0;
1125 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1126 /* one byte less than the watermark level to enable the aging timer */
1127 slave_config.src_maxburst = RXTL_DMA - 1;
1128 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
1129 if (ret) {
1130 dev_err(dev, "error in RX dma configuration.\n");
1131 goto err;
1134 sport->rx_buf = kzalloc(RX_BUF_SIZE, GFP_KERNEL);
1135 if (!sport->rx_buf) {
1136 ret = -ENOMEM;
1137 goto err;
1139 sport->rx_ring.buf = sport->rx_buf;
1141 /* Prepare for TX : */
1142 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1143 if (!sport->dma_chan_tx) {
1144 dev_err(dev, "cannot get the TX DMA channel!\n");
1145 ret = -EINVAL;
1146 goto err;
1149 slave_config.direction = DMA_MEM_TO_DEV;
1150 slave_config.dst_addr = sport->port.mapbase + URTX0;
1151 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1152 slave_config.dst_maxburst = TXTL_DMA;
1153 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1154 if (ret) {
1155 dev_err(dev, "error in TX dma configuration.");
1156 goto err;
1159 sport->dma_is_inited = 1;
1161 return 0;
1162 err:
1163 imx_uart_dma_exit(sport);
1164 return ret;
1167 static void imx_enable_dma(struct imx_port *sport)
1169 unsigned long temp;
1171 /* set UCR1 */
1172 temp = readl(sport->port.membase + UCR1);
1173 temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN;
1174 writel(temp, sport->port.membase + UCR1);
1176 imx_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
1178 sport->dma_is_enabled = 1;
1181 static void imx_disable_dma(struct imx_port *sport)
1183 unsigned long temp;
1185 /* clear UCR1 */
1186 temp = readl(sport->port.membase + UCR1);
1187 temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
1188 writel(temp, sport->port.membase + UCR1);
1190 /* clear UCR2 */
1191 temp = readl(sport->port.membase + UCR2);
1192 temp &= ~(UCR2_CTSC | UCR2_CTS | UCR2_ATEN);
1193 writel(temp, sport->port.membase + UCR2);
1195 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1197 sport->dma_is_enabled = 0;
1200 /* half the RX buffer size */
1201 #define CTSTL 16
1203 static int imx_startup(struct uart_port *port)
1205 struct imx_port *sport = (struct imx_port *)port;
1206 int retval, i;
1207 unsigned long flags, temp;
1209 retval = clk_prepare_enable(sport->clk_per);
1210 if (retval)
1211 return retval;
1212 retval = clk_prepare_enable(sport->clk_ipg);
1213 if (retval) {
1214 clk_disable_unprepare(sport->clk_per);
1215 return retval;
1218 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1220 /* disable the DREN bit (Data Ready interrupt enable) before
1221 * requesting IRQs
1223 temp = readl(sport->port.membase + UCR4);
1225 /* set the trigger level for CTS */
1226 temp &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
1227 temp |= CTSTL << UCR4_CTSTL_SHF;
1229 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
1231 /* Can we enable the DMA support? */
1232 if (!uart_console(port) && !sport->dma_is_inited)
1233 imx_uart_dma_init(sport);
1235 spin_lock_irqsave(&sport->port.lock, flags);
1236 /* Reset fifo's and state machines */
1237 i = 100;
1239 temp = readl(sport->port.membase + UCR2);
1240 temp &= ~UCR2_SRST;
1241 writel(temp, sport->port.membase + UCR2);
1243 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
1244 udelay(1);
1247 * Finally, clear and enable interrupts
1249 writel(USR1_RTSD | USR1_DTRD, sport->port.membase + USR1);
1250 writel(USR2_ORE, sport->port.membase + USR2);
1252 if (sport->dma_is_inited && !sport->dma_is_enabled)
1253 imx_enable_dma(sport);
1255 temp = readl(sport->port.membase + UCR1);
1256 temp |= UCR1_RRDYEN | UCR1_UARTEN;
1257 if (sport->have_rtscts)
1258 temp |= UCR1_RTSDEN;
1260 writel(temp, sport->port.membase + UCR1);
1262 temp = readl(sport->port.membase + UCR4);
1263 temp |= UCR4_OREN;
1264 writel(temp, sport->port.membase + UCR4);
1266 temp = readl(sport->port.membase + UCR2);
1267 temp |= (UCR2_RXEN | UCR2_TXEN);
1268 if (!sport->have_rtscts)
1269 temp |= UCR2_IRTS;
1271 * make sure the edge sensitive RTS-irq is disabled,
1272 * we're using RTSD instead.
1274 if (!is_imx1_uart(sport))
1275 temp &= ~UCR2_RTSEN;
1276 writel(temp, sport->port.membase + UCR2);
1278 if (!is_imx1_uart(sport)) {
1279 temp = readl(sport->port.membase + UCR3);
1281 temp |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
1283 if (sport->dte_mode)
1284 /* disable broken interrupts */
1285 temp &= ~(UCR3_RI | UCR3_DCD);
1287 writel(temp, sport->port.membase + UCR3);
1291 * Enable modem status interrupts
1293 imx_enable_ms(&sport->port);
1296 * Start RX DMA immediately instead of waiting for RX FIFO interrupts.
1297 * In our iMX53 the average delay for the first reception dropped from
1298 * approximately 35000 microseconds to 1000 microseconds.
1300 if (sport->dma_is_enabled) {
1301 imx_disable_rx_int(sport);
1302 start_rx_dma(sport);
1305 spin_unlock_irqrestore(&sport->port.lock, flags);
1307 return 0;
1310 static void imx_shutdown(struct uart_port *port)
1312 struct imx_port *sport = (struct imx_port *)port;
1313 unsigned long temp;
1314 unsigned long flags;
1316 if (sport->dma_is_enabled) {
1317 sport->dma_is_rxing = 0;
1318 sport->dma_is_txing = 0;
1319 dmaengine_terminate_sync(sport->dma_chan_tx);
1320 dmaengine_terminate_sync(sport->dma_chan_rx);
1322 spin_lock_irqsave(&sport->port.lock, flags);
1323 imx_stop_tx(port);
1324 imx_stop_rx(port);
1325 imx_disable_dma(sport);
1326 spin_unlock_irqrestore(&sport->port.lock, flags);
1327 imx_uart_dma_exit(sport);
1330 mctrl_gpio_disable_ms(sport->gpios);
1332 spin_lock_irqsave(&sport->port.lock, flags);
1333 temp = readl(sport->port.membase + UCR2);
1334 temp &= ~(UCR2_TXEN);
1335 writel(temp, sport->port.membase + UCR2);
1336 spin_unlock_irqrestore(&sport->port.lock, flags);
1339 * Stop our timer.
1341 del_timer_sync(&sport->timer);
1344 * Disable all interrupts, port and break condition.
1347 spin_lock_irqsave(&sport->port.lock, flags);
1348 temp = readl(sport->port.membase + UCR1);
1349 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
1351 writel(temp, sport->port.membase + UCR1);
1352 spin_unlock_irqrestore(&sport->port.lock, flags);
1354 clk_disable_unprepare(sport->clk_per);
1355 clk_disable_unprepare(sport->clk_ipg);
1358 static void imx_flush_buffer(struct uart_port *port)
1360 struct imx_port *sport = (struct imx_port *)port;
1361 struct scatterlist *sgl = &sport->tx_sgl[0];
1362 unsigned long temp;
1363 int i = 100, ubir, ubmr, uts;
1365 if (!sport->dma_chan_tx)
1366 return;
1368 sport->tx_bytes = 0;
1369 dmaengine_terminate_all(sport->dma_chan_tx);
1370 if (sport->dma_is_txing) {
1371 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
1372 DMA_TO_DEVICE);
1373 temp = readl(sport->port.membase + UCR1);
1374 temp &= ~UCR1_TDMAEN;
1375 writel(temp, sport->port.membase + UCR1);
1376 sport->dma_is_txing = 0;
1380 * According to the Reference Manual description of the UART SRST bit:
1382 * "Reset the transmit and receive state machines,
1383 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
1384 * and UTS[6-3]".
1386 * We don't need to restore the old values from USR1, USR2, URXD and
1387 * UTXD. UBRC is read only, so only save/restore the other three
1388 * registers.
1390 ubir = readl(sport->port.membase + UBIR);
1391 ubmr = readl(sport->port.membase + UBMR);
1392 uts = readl(sport->port.membase + IMX21_UTS);
1394 temp = readl(sport->port.membase + UCR2);
1395 temp &= ~UCR2_SRST;
1396 writel(temp, sport->port.membase + UCR2);
1398 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
1399 udelay(1);
1401 /* Restore the registers */
1402 writel(ubir, sport->port.membase + UBIR);
1403 writel(ubmr, sport->port.membase + UBMR);
1404 writel(uts, sport->port.membase + IMX21_UTS);
1407 static void
1408 imx_set_termios(struct uart_port *port, struct ktermios *termios,
1409 struct ktermios *old)
1411 struct imx_port *sport = (struct imx_port *)port;
1412 unsigned long flags;
1413 unsigned long ucr2, old_ucr1, old_ucr2;
1414 unsigned int baud, quot;
1415 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1416 unsigned long div, ufcr;
1417 unsigned long num, denom;
1418 uint64_t tdiv64;
1421 * We only support CS7 and CS8.
1423 while ((termios->c_cflag & CSIZE) != CS7 &&
1424 (termios->c_cflag & CSIZE) != CS8) {
1425 termios->c_cflag &= ~CSIZE;
1426 termios->c_cflag |= old_csize;
1427 old_csize = CS8;
1430 if ((termios->c_cflag & CSIZE) == CS8)
1431 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
1432 else
1433 ucr2 = UCR2_SRST | UCR2_IRTS;
1435 if (termios->c_cflag & CRTSCTS) {
1436 if (sport->have_rtscts) {
1437 ucr2 &= ~UCR2_IRTS;
1439 if (port->rs485.flags & SER_RS485_ENABLED) {
1441 * RTS is mandatory for rs485 operation, so keep
1442 * it under manual control and keep transmitter
1443 * disabled.
1445 if (port->rs485.flags &
1446 SER_RS485_RTS_AFTER_SEND)
1447 imx_port_rts_active(sport, &ucr2);
1448 else
1449 imx_port_rts_inactive(sport, &ucr2);
1450 } else {
1451 imx_port_rts_auto(sport, &ucr2);
1453 } else {
1454 termios->c_cflag &= ~CRTSCTS;
1456 } else if (port->rs485.flags & SER_RS485_ENABLED) {
1457 /* disable transmitter */
1458 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1459 imx_port_rts_active(sport, &ucr2);
1460 else
1461 imx_port_rts_inactive(sport, &ucr2);
1465 if (termios->c_cflag & CSTOPB)
1466 ucr2 |= UCR2_STPB;
1467 if (termios->c_cflag & PARENB) {
1468 ucr2 |= UCR2_PREN;
1469 if (termios->c_cflag & PARODD)
1470 ucr2 |= UCR2_PROE;
1473 del_timer_sync(&sport->timer);
1476 * Ask the core to calculate the divisor for us.
1478 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1479 quot = uart_get_divisor(port, baud);
1481 spin_lock_irqsave(&sport->port.lock, flags);
1483 sport->port.read_status_mask = 0;
1484 if (termios->c_iflag & INPCK)
1485 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
1486 if (termios->c_iflag & (BRKINT | PARMRK))
1487 sport->port.read_status_mask |= URXD_BRK;
1490 * Characters to ignore
1492 sport->port.ignore_status_mask = 0;
1493 if (termios->c_iflag & IGNPAR)
1494 sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
1495 if (termios->c_iflag & IGNBRK) {
1496 sport->port.ignore_status_mask |= URXD_BRK;
1498 * If we're ignoring parity and break indicators,
1499 * ignore overruns too (for real raw support).
1501 if (termios->c_iflag & IGNPAR)
1502 sport->port.ignore_status_mask |= URXD_OVRRUN;
1505 if ((termios->c_cflag & CREAD) == 0)
1506 sport->port.ignore_status_mask |= URXD_DUMMY_READ;
1509 * Update the per-port timeout.
1511 uart_update_timeout(port, termios->c_cflag, baud);
1514 * disable interrupts and drain transmitter
1516 old_ucr1 = readl(sport->port.membase + UCR1);
1517 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
1518 sport->port.membase + UCR1);
1520 while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
1521 barrier();
1523 /* then, disable everything */
1524 old_ucr2 = readl(sport->port.membase + UCR2);
1525 writel(old_ucr2 & ~(UCR2_TXEN | UCR2_RXEN),
1526 sport->port.membase + UCR2);
1527 old_ucr2 &= (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN);
1529 /* custom-baudrate handling */
1530 div = sport->port.uartclk / (baud * 16);
1531 if (baud == 38400 && quot != div)
1532 baud = sport->port.uartclk / (quot * 16);
1534 div = sport->port.uartclk / (baud * 16);
1535 if (div > 7)
1536 div = 7;
1537 if (!div)
1538 div = 1;
1540 rational_best_approximation(16 * div * baud, sport->port.uartclk,
1541 1 << 16, 1 << 16, &num, &denom);
1543 tdiv64 = sport->port.uartclk;
1544 tdiv64 *= num;
1545 do_div(tdiv64, denom * 16 * div);
1546 tty_termios_encode_baud_rate(termios,
1547 (speed_t)tdiv64, (speed_t)tdiv64);
1549 num -= 1;
1550 denom -= 1;
1552 ufcr = readl(sport->port.membase + UFCR);
1553 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
1554 writel(ufcr, sport->port.membase + UFCR);
1556 writel(num, sport->port.membase + UBIR);
1557 writel(denom, sport->port.membase + UBMR);
1559 if (!is_imx1_uart(sport))
1560 writel(sport->port.uartclk / div / 1000,
1561 sport->port.membase + IMX21_ONEMS);
1563 writel(old_ucr1, sport->port.membase + UCR1);
1565 /* set the parity, stop bits and data size */
1566 writel(ucr2 | old_ucr2, sport->port.membase + UCR2);
1568 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1569 imx_enable_ms(&sport->port);
1571 spin_unlock_irqrestore(&sport->port.lock, flags);
1574 static const char *imx_type(struct uart_port *port)
1576 struct imx_port *sport = (struct imx_port *)port;
1578 return sport->port.type == PORT_IMX ? "IMX" : NULL;
1582 * Configure/autoconfigure the port.
1584 static void imx_config_port(struct uart_port *port, int flags)
1586 struct imx_port *sport = (struct imx_port *)port;
1588 if (flags & UART_CONFIG_TYPE)
1589 sport->port.type = PORT_IMX;
1593 * Verify the new serial_struct (for TIOCSSERIAL).
1594 * The only change we allow are to the flags and type, and
1595 * even then only between PORT_IMX and PORT_UNKNOWN
1597 static int
1598 imx_verify_port(struct uart_port *port, struct serial_struct *ser)
1600 struct imx_port *sport = (struct imx_port *)port;
1601 int ret = 0;
1603 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1604 ret = -EINVAL;
1605 if (sport->port.irq != ser->irq)
1606 ret = -EINVAL;
1607 if (ser->io_type != UPIO_MEM)
1608 ret = -EINVAL;
1609 if (sport->port.uartclk / 16 != ser->baud_base)
1610 ret = -EINVAL;
1611 if (sport->port.mapbase != (unsigned long)ser->iomem_base)
1612 ret = -EINVAL;
1613 if (sport->port.iobase != ser->port)
1614 ret = -EINVAL;
1615 if (ser->hub6 != 0)
1616 ret = -EINVAL;
1617 return ret;
1620 #if defined(CONFIG_CONSOLE_POLL)
1622 static int imx_poll_init(struct uart_port *port)
1624 struct imx_port *sport = (struct imx_port *)port;
1625 unsigned long flags;
1626 unsigned long temp;
1627 int retval;
1629 retval = clk_prepare_enable(sport->clk_ipg);
1630 if (retval)
1631 return retval;
1632 retval = clk_prepare_enable(sport->clk_per);
1633 if (retval)
1634 clk_disable_unprepare(sport->clk_ipg);
1636 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1638 spin_lock_irqsave(&sport->port.lock, flags);
1640 temp = readl(sport->port.membase + UCR1);
1641 if (is_imx1_uart(sport))
1642 temp |= IMX1_UCR1_UARTCLKEN;
1643 temp |= UCR1_UARTEN | UCR1_RRDYEN;
1644 temp &= ~(UCR1_TXMPTYEN | UCR1_RTSDEN);
1645 writel(temp, sport->port.membase + UCR1);
1647 temp = readl(sport->port.membase + UCR2);
1648 temp |= UCR2_RXEN;
1649 writel(temp, sport->port.membase + UCR2);
1651 spin_unlock_irqrestore(&sport->port.lock, flags);
1653 return 0;
1656 static int imx_poll_get_char(struct uart_port *port)
1658 if (!(readl_relaxed(port->membase + USR2) & USR2_RDR))
1659 return NO_POLL_CHAR;
1661 return readl_relaxed(port->membase + URXD0) & URXD_RX_DATA;
1664 static void imx_poll_put_char(struct uart_port *port, unsigned char c)
1666 unsigned int status;
1668 /* drain */
1669 do {
1670 status = readl_relaxed(port->membase + USR1);
1671 } while (~status & USR1_TRDY);
1673 /* write */
1674 writel_relaxed(c, port->membase + URTX0);
1676 /* flush */
1677 do {
1678 status = readl_relaxed(port->membase + USR2);
1679 } while (~status & USR2_TXDC);
1681 #endif
1683 static int imx_rs485_config(struct uart_port *port,
1684 struct serial_rs485 *rs485conf)
1686 struct imx_port *sport = (struct imx_port *)port;
1687 unsigned long temp;
1689 /* unimplemented */
1690 rs485conf->delay_rts_before_send = 0;
1691 rs485conf->delay_rts_after_send = 0;
1693 /* RTS is required to control the transmitter */
1694 if (!sport->have_rtscts && !sport->have_rtsgpio)
1695 rs485conf->flags &= ~SER_RS485_ENABLED;
1697 if (rs485conf->flags & SER_RS485_ENABLED) {
1698 /* disable transmitter */
1699 temp = readl(sport->port.membase + UCR2);
1700 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
1701 imx_port_rts_active(sport, &temp);
1702 else
1703 imx_port_rts_inactive(sport, &temp);
1704 writel(temp, sport->port.membase + UCR2);
1707 /* Make sure Rx is enabled in case Tx is active with Rx disabled */
1708 if (!(rs485conf->flags & SER_RS485_ENABLED) ||
1709 rs485conf->flags & SER_RS485_RX_DURING_TX) {
1710 temp = readl(sport->port.membase + UCR2);
1711 temp |= UCR2_RXEN;
1712 writel(temp, sport->port.membase + UCR2);
1715 port->rs485 = *rs485conf;
1717 return 0;
1720 static const struct uart_ops imx_pops = {
1721 .tx_empty = imx_tx_empty,
1722 .set_mctrl = imx_set_mctrl,
1723 .get_mctrl = imx_get_mctrl,
1724 .stop_tx = imx_stop_tx,
1725 .start_tx = imx_start_tx,
1726 .stop_rx = imx_stop_rx,
1727 .enable_ms = imx_enable_ms,
1728 .break_ctl = imx_break_ctl,
1729 .startup = imx_startup,
1730 .shutdown = imx_shutdown,
1731 .flush_buffer = imx_flush_buffer,
1732 .set_termios = imx_set_termios,
1733 .type = imx_type,
1734 .config_port = imx_config_port,
1735 .verify_port = imx_verify_port,
1736 #if defined(CONFIG_CONSOLE_POLL)
1737 .poll_init = imx_poll_init,
1738 .poll_get_char = imx_poll_get_char,
1739 .poll_put_char = imx_poll_put_char,
1740 #endif
1743 static struct imx_port *imx_ports[UART_NR];
1745 #ifdef CONFIG_SERIAL_IMX_CONSOLE
1746 static void imx_console_putchar(struct uart_port *port, int ch)
1748 struct imx_port *sport = (struct imx_port *)port;
1750 while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
1751 barrier();
1753 writel(ch, sport->port.membase + URTX0);
1757 * Interrupts are disabled on entering
1759 static void
1760 imx_console_write(struct console *co, const char *s, unsigned int count)
1762 struct imx_port *sport = imx_ports[co->index];
1763 struct imx_port_ucrs old_ucr;
1764 unsigned int ucr1;
1765 unsigned long flags = 0;
1766 int locked = 1;
1767 int retval;
1769 retval = clk_enable(sport->clk_per);
1770 if (retval)
1771 return;
1772 retval = clk_enable(sport->clk_ipg);
1773 if (retval) {
1774 clk_disable(sport->clk_per);
1775 return;
1778 if (sport->port.sysrq)
1779 locked = 0;
1780 else if (oops_in_progress)
1781 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1782 else
1783 spin_lock_irqsave(&sport->port.lock, flags);
1786 * First, save UCR1/2/3 and then disable interrupts
1788 imx_port_ucrs_save(&sport->port, &old_ucr);
1789 ucr1 = old_ucr.ucr1;
1791 if (is_imx1_uart(sport))
1792 ucr1 |= IMX1_UCR1_UARTCLKEN;
1793 ucr1 |= UCR1_UARTEN;
1794 ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
1796 writel(ucr1, sport->port.membase + UCR1);
1798 writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
1800 uart_console_write(&sport->port, s, count, imx_console_putchar);
1803 * Finally, wait for transmitter to become empty
1804 * and restore UCR1/2/3
1806 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
1808 imx_port_ucrs_restore(&sport->port, &old_ucr);
1810 if (locked)
1811 spin_unlock_irqrestore(&sport->port.lock, flags);
1813 clk_disable(sport->clk_ipg);
1814 clk_disable(sport->clk_per);
1818 * If the port was already initialised (eg, by a boot loader),
1819 * try to determine the current setup.
1821 static void __init
1822 imx_console_get_options(struct imx_port *sport, int *baud,
1823 int *parity, int *bits)
1826 if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
1827 /* ok, the port was enabled */
1828 unsigned int ucr2, ubir, ubmr, uartclk;
1829 unsigned int baud_raw;
1830 unsigned int ucfr_rfdiv;
1832 ucr2 = readl(sport->port.membase + UCR2);
1834 *parity = 'n';
1835 if (ucr2 & UCR2_PREN) {
1836 if (ucr2 & UCR2_PROE)
1837 *parity = 'o';
1838 else
1839 *parity = 'e';
1842 if (ucr2 & UCR2_WS)
1843 *bits = 8;
1844 else
1845 *bits = 7;
1847 ubir = readl(sport->port.membase + UBIR) & 0xffff;
1848 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
1850 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
1851 if (ucfr_rfdiv == 6)
1852 ucfr_rfdiv = 7;
1853 else
1854 ucfr_rfdiv = 6 - ucfr_rfdiv;
1856 uartclk = clk_get_rate(sport->clk_per);
1857 uartclk /= ucfr_rfdiv;
1859 { /*
1860 * The next code provides exact computation of
1861 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1862 * without need of float support or long long division,
1863 * which would be required to prevent 32bit arithmetic overflow
1865 unsigned int mul = ubir + 1;
1866 unsigned int div = 16 * (ubmr + 1);
1867 unsigned int rem = uartclk % div;
1869 baud_raw = (uartclk / div) * mul;
1870 baud_raw += (rem * mul + div / 2) / div;
1871 *baud = (baud_raw + 50) / 100 * 100;
1874 if (*baud != baud_raw)
1875 pr_info("Console IMX rounded baud rate from %d to %d\n",
1876 baud_raw, *baud);
1880 static int __init
1881 imx_console_setup(struct console *co, char *options)
1883 struct imx_port *sport;
1884 int baud = 9600;
1885 int bits = 8;
1886 int parity = 'n';
1887 int flow = 'n';
1888 int retval;
1891 * Check whether an invalid uart number has been specified, and
1892 * if so, search for the first available port that does have
1893 * console support.
1895 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1896 co->index = 0;
1897 sport = imx_ports[co->index];
1898 if (sport == NULL)
1899 return -ENODEV;
1901 /* For setting the registers, we only need to enable the ipg clock. */
1902 retval = clk_prepare_enable(sport->clk_ipg);
1903 if (retval)
1904 goto error_console;
1906 if (options)
1907 uart_parse_options(options, &baud, &parity, &bits, &flow);
1908 else
1909 imx_console_get_options(sport, &baud, &parity, &bits);
1911 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1913 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
1915 clk_disable(sport->clk_ipg);
1916 if (retval) {
1917 clk_unprepare(sport->clk_ipg);
1918 goto error_console;
1921 retval = clk_prepare(sport->clk_per);
1922 if (retval)
1923 clk_disable_unprepare(sport->clk_ipg);
1925 error_console:
1926 return retval;
1929 static struct uart_driver imx_reg;
1930 static struct console imx_console = {
1931 .name = DEV_NAME,
1932 .write = imx_console_write,
1933 .device = uart_console_device,
1934 .setup = imx_console_setup,
1935 .flags = CON_PRINTBUFFER,
1936 .index = -1,
1937 .data = &imx_reg,
1940 #define IMX_CONSOLE &imx_console
1942 #ifdef CONFIG_OF
1943 static void imx_console_early_putchar(struct uart_port *port, int ch)
1945 while (readl_relaxed(port->membase + IMX21_UTS) & UTS_TXFULL)
1946 cpu_relax();
1948 writel_relaxed(ch, port->membase + URTX0);
1951 static void imx_console_early_write(struct console *con, const char *s,
1952 unsigned count)
1954 struct earlycon_device *dev = con->data;
1956 uart_console_write(&dev->port, s, count, imx_console_early_putchar);
1959 static int __init
1960 imx_console_early_setup(struct earlycon_device *dev, const char *opt)
1962 if (!dev->port.membase)
1963 return -ENODEV;
1965 dev->con->write = imx_console_early_write;
1967 return 0;
1969 OF_EARLYCON_DECLARE(ec_imx6q, "fsl,imx6q-uart", imx_console_early_setup);
1970 OF_EARLYCON_DECLARE(ec_imx21, "fsl,imx21-uart", imx_console_early_setup);
1971 #endif
1973 #else
1974 #define IMX_CONSOLE NULL
1975 #endif
1977 static struct uart_driver imx_reg = {
1978 .owner = THIS_MODULE,
1979 .driver_name = DRIVER_NAME,
1980 .dev_name = DEV_NAME,
1981 .major = SERIAL_IMX_MAJOR,
1982 .minor = MINOR_START,
1983 .nr = ARRAY_SIZE(imx_ports),
1984 .cons = IMX_CONSOLE,
1987 #ifdef CONFIG_OF
1989 * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
1990 * could successfully get all information from dt or a negative errno.
1992 static int serial_imx_probe_dt(struct imx_port *sport,
1993 struct platform_device *pdev)
1995 struct device_node *np = pdev->dev.of_node;
1996 int ret;
1998 sport->devdata = of_device_get_match_data(&pdev->dev);
1999 if (!sport->devdata)
2000 /* no device tree device */
2001 return 1;
2003 ret = of_alias_get_id(np, "serial");
2004 if (ret < 0) {
2005 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2006 return ret;
2008 sport->port.line = ret;
2010 if (of_get_property(np, "uart-has-rtscts", NULL) ||
2011 of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
2012 sport->have_rtscts = 1;
2014 if (of_get_property(np, "fsl,dte-mode", NULL))
2015 sport->dte_mode = 1;
2017 if (of_get_property(np, "rts-gpios", NULL))
2018 sport->have_rtsgpio = 1;
2020 of_get_rs485_mode(np, &sport->port.rs485);
2022 return 0;
2024 #else
2025 static inline int serial_imx_probe_dt(struct imx_port *sport,
2026 struct platform_device *pdev)
2028 return 1;
2030 #endif
2032 static void serial_imx_probe_pdata(struct imx_port *sport,
2033 struct platform_device *pdev)
2035 struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
2037 sport->port.line = pdev->id;
2038 sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
2040 if (!pdata)
2041 return;
2043 if (pdata->flags & IMXUART_HAVE_RTSCTS)
2044 sport->have_rtscts = 1;
2047 static int serial_imx_probe(struct platform_device *pdev)
2049 struct imx_port *sport;
2050 void __iomem *base;
2051 int ret = 0, reg;
2052 struct resource *res;
2053 int txirq, rxirq, rtsirq;
2055 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2056 if (!sport)
2057 return -ENOMEM;
2059 ret = serial_imx_probe_dt(sport, pdev);
2060 if (ret > 0)
2061 serial_imx_probe_pdata(sport, pdev);
2062 else if (ret < 0)
2063 return ret;
2065 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2066 base = devm_ioremap_resource(&pdev->dev, res);
2067 if (IS_ERR(base))
2068 return PTR_ERR(base);
2070 rxirq = platform_get_irq(pdev, 0);
2071 txirq = platform_get_irq(pdev, 1);
2072 rtsirq = platform_get_irq(pdev, 2);
2074 sport->port.dev = &pdev->dev;
2075 sport->port.mapbase = res->start;
2076 sport->port.membase = base;
2077 sport->port.type = PORT_IMX,
2078 sport->port.iotype = UPIO_MEM;
2079 sport->port.irq = rxirq;
2080 sport->port.fifosize = 32;
2081 sport->port.ops = &imx_pops;
2082 sport->port.rs485_config = imx_rs485_config;
2083 sport->port.rs485.flags |= SER_RS485_RTS_ON_SEND;
2084 sport->port.flags = UPF_BOOT_AUTOCONF;
2085 timer_setup(&sport->timer, imx_timeout, 0);
2087 sport->gpios = mctrl_gpio_init(&sport->port, 0);
2088 if (IS_ERR(sport->gpios))
2089 return PTR_ERR(sport->gpios);
2091 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2092 if (IS_ERR(sport->clk_ipg)) {
2093 ret = PTR_ERR(sport->clk_ipg);
2094 dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
2095 return ret;
2098 sport->clk_per = devm_clk_get(&pdev->dev, "per");
2099 if (IS_ERR(sport->clk_per)) {
2100 ret = PTR_ERR(sport->clk_per);
2101 dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
2102 return ret;
2105 sport->port.uartclk = clk_get_rate(sport->clk_per);
2107 /* For register access, we only need to enable the ipg clock. */
2108 ret = clk_prepare_enable(sport->clk_ipg);
2109 if (ret) {
2110 dev_err(&pdev->dev, "failed to enable per clk: %d\n", ret);
2111 return ret;
2114 /* Disable interrupts before requesting them */
2115 reg = readl_relaxed(sport->port.membase + UCR1);
2116 reg &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN |
2117 UCR1_TXMPTYEN | UCR1_RTSDEN);
2118 writel_relaxed(reg, sport->port.membase + UCR1);
2120 if (!is_imx1_uart(sport) && sport->dte_mode) {
2122 * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI
2123 * and influences if UCR3_RI and UCR3_DCD changes the level of RI
2124 * and DCD (when they are outputs) or enables the respective
2125 * irqs. So set this bit early, i.e. before requesting irqs.
2127 reg = readl(sport->port.membase + UFCR);
2128 if (!(reg & UFCR_DCEDTE))
2129 writel(reg | UFCR_DCEDTE, sport->port.membase + UFCR);
2132 * Disable UCR3_RI and UCR3_DCD irqs. They are also not
2133 * enabled later because they cannot be cleared
2134 * (confirmed on i.MX25) which makes them unusable.
2136 writel(IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR,
2137 sport->port.membase + UCR3);
2139 } else {
2140 unsigned long ucr3 = UCR3_DSR;
2142 reg = readl(sport->port.membase + UFCR);
2143 if (reg & UFCR_DCEDTE)
2144 writel(reg & ~UFCR_DCEDTE, sport->port.membase + UFCR);
2146 if (!is_imx1_uart(sport))
2147 ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
2148 writel(ucr3, sport->port.membase + UCR3);
2151 clk_disable_unprepare(sport->clk_ipg);
2154 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
2155 * chips only have one interrupt.
2157 if (txirq > 0) {
2158 ret = devm_request_irq(&pdev->dev, rxirq, imx_rxint, 0,
2159 dev_name(&pdev->dev), sport);
2160 if (ret) {
2161 dev_err(&pdev->dev, "failed to request rx irq: %d\n",
2162 ret);
2163 return ret;
2166 ret = devm_request_irq(&pdev->dev, txirq, imx_txint, 0,
2167 dev_name(&pdev->dev), sport);
2168 if (ret) {
2169 dev_err(&pdev->dev, "failed to request tx irq: %d\n",
2170 ret);
2171 return ret;
2173 } else {
2174 ret = devm_request_irq(&pdev->dev, rxirq, imx_int, 0,
2175 dev_name(&pdev->dev), sport);
2176 if (ret) {
2177 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2178 return ret;
2182 imx_ports[sport->port.line] = sport;
2184 platform_set_drvdata(pdev, sport);
2186 return uart_add_one_port(&imx_reg, &sport->port);
2189 static int serial_imx_remove(struct platform_device *pdev)
2191 struct imx_port *sport = platform_get_drvdata(pdev);
2193 return uart_remove_one_port(&imx_reg, &sport->port);
2196 static void serial_imx_restore_context(struct imx_port *sport)
2198 if (!sport->context_saved)
2199 return;
2201 writel(sport->saved_reg[4], sport->port.membase + UFCR);
2202 writel(sport->saved_reg[5], sport->port.membase + UESC);
2203 writel(sport->saved_reg[6], sport->port.membase + UTIM);
2204 writel(sport->saved_reg[7], sport->port.membase + UBIR);
2205 writel(sport->saved_reg[8], sport->port.membase + UBMR);
2206 writel(sport->saved_reg[9], sport->port.membase + IMX21_UTS);
2207 writel(sport->saved_reg[0], sport->port.membase + UCR1);
2208 writel(sport->saved_reg[1] | UCR2_SRST, sport->port.membase + UCR2);
2209 writel(sport->saved_reg[2], sport->port.membase + UCR3);
2210 writel(sport->saved_reg[3], sport->port.membase + UCR4);
2211 sport->context_saved = false;
2214 static void serial_imx_save_context(struct imx_port *sport)
2216 /* Save necessary regs */
2217 sport->saved_reg[0] = readl(sport->port.membase + UCR1);
2218 sport->saved_reg[1] = readl(sport->port.membase + UCR2);
2219 sport->saved_reg[2] = readl(sport->port.membase + UCR3);
2220 sport->saved_reg[3] = readl(sport->port.membase + UCR4);
2221 sport->saved_reg[4] = readl(sport->port.membase + UFCR);
2222 sport->saved_reg[5] = readl(sport->port.membase + UESC);
2223 sport->saved_reg[6] = readl(sport->port.membase + UTIM);
2224 sport->saved_reg[7] = readl(sport->port.membase + UBIR);
2225 sport->saved_reg[8] = readl(sport->port.membase + UBMR);
2226 sport->saved_reg[9] = readl(sport->port.membase + IMX21_UTS);
2227 sport->context_saved = true;
2230 static void serial_imx_enable_wakeup(struct imx_port *sport, bool on)
2232 unsigned int val;
2234 val = readl(sport->port.membase + UCR3);
2235 if (on)
2236 val |= UCR3_AWAKEN;
2237 else
2238 val &= ~UCR3_AWAKEN;
2239 writel(val, sport->port.membase + UCR3);
2241 if (sport->have_rtscts) {
2242 val = readl(sport->port.membase + UCR1);
2243 if (on)
2244 val |= UCR1_RTSDEN;
2245 else
2246 val &= ~UCR1_RTSDEN;
2247 writel(val, sport->port.membase + UCR1);
2251 static int imx_serial_port_suspend_noirq(struct device *dev)
2253 struct platform_device *pdev = to_platform_device(dev);
2254 struct imx_port *sport = platform_get_drvdata(pdev);
2255 int ret;
2257 ret = clk_enable(sport->clk_ipg);
2258 if (ret)
2259 return ret;
2261 serial_imx_save_context(sport);
2263 clk_disable(sport->clk_ipg);
2265 return 0;
2268 static int imx_serial_port_resume_noirq(struct device *dev)
2270 struct platform_device *pdev = to_platform_device(dev);
2271 struct imx_port *sport = platform_get_drvdata(pdev);
2272 int ret;
2274 ret = clk_enable(sport->clk_ipg);
2275 if (ret)
2276 return ret;
2278 serial_imx_restore_context(sport);
2280 clk_disable(sport->clk_ipg);
2282 return 0;
2285 static int imx_serial_port_suspend(struct device *dev)
2287 struct platform_device *pdev = to_platform_device(dev);
2288 struct imx_port *sport = platform_get_drvdata(pdev);
2290 /* enable wakeup from i.MX UART */
2291 serial_imx_enable_wakeup(sport, true);
2293 uart_suspend_port(&imx_reg, &sport->port);
2294 disable_irq(sport->port.irq);
2296 /* Needed to enable clock in suspend_noirq */
2297 return clk_prepare(sport->clk_ipg);
2300 static int imx_serial_port_resume(struct device *dev)
2302 struct platform_device *pdev = to_platform_device(dev);
2303 struct imx_port *sport = platform_get_drvdata(pdev);
2305 /* disable wakeup from i.MX UART */
2306 serial_imx_enable_wakeup(sport, false);
2308 uart_resume_port(&imx_reg, &sport->port);
2309 enable_irq(sport->port.irq);
2311 clk_unprepare(sport->clk_ipg);
2313 return 0;
2316 static int imx_serial_port_freeze(struct device *dev)
2318 struct platform_device *pdev = to_platform_device(dev);
2319 struct imx_port *sport = platform_get_drvdata(pdev);
2321 uart_suspend_port(&imx_reg, &sport->port);
2323 /* Needed to enable clock in suspend_noirq */
2324 return clk_prepare(sport->clk_ipg);
2327 static int imx_serial_port_thaw(struct device *dev)
2329 struct platform_device *pdev = to_platform_device(dev);
2330 struct imx_port *sport = platform_get_drvdata(pdev);
2332 uart_resume_port(&imx_reg, &sport->port);
2334 clk_unprepare(sport->clk_ipg);
2336 return 0;
2339 static const struct dev_pm_ops imx_serial_port_pm_ops = {
2340 .suspend_noirq = imx_serial_port_suspend_noirq,
2341 .resume_noirq = imx_serial_port_resume_noirq,
2342 .freeze_noirq = imx_serial_port_suspend_noirq,
2343 .restore_noirq = imx_serial_port_resume_noirq,
2344 .suspend = imx_serial_port_suspend,
2345 .resume = imx_serial_port_resume,
2346 .freeze = imx_serial_port_freeze,
2347 .thaw = imx_serial_port_thaw,
2348 .restore = imx_serial_port_thaw,
2351 static struct platform_driver serial_imx_driver = {
2352 .probe = serial_imx_probe,
2353 .remove = serial_imx_remove,
2355 .id_table = imx_uart_devtype,
2356 .driver = {
2357 .name = "imx-uart",
2358 .of_match_table = imx_uart_dt_ids,
2359 .pm = &imx_serial_port_pm_ops,
2363 static int __init imx_serial_init(void)
2365 int ret = uart_register_driver(&imx_reg);
2367 if (ret)
2368 return ret;
2370 ret = platform_driver_register(&serial_imx_driver);
2371 if (ret != 0)
2372 uart_unregister_driver(&imx_reg);
2374 return ret;
2377 static void __exit imx_serial_exit(void)
2379 platform_driver_unregister(&serial_imx_driver);
2380 uart_unregister_driver(&imx_reg);
2383 module_init(imx_serial_init);
2384 module_exit(imx_serial_exit);
2386 MODULE_AUTHOR("Sascha Hauer");
2387 MODULE_DESCRIPTION("IMX generic serial port driver");
2388 MODULE_LICENSE("GPL");
2389 MODULE_ALIAS("platform:imx-uart");