xref: /openbmc/linux/drivers/tty/serial/imx.c (revision f0931824)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Motorola/Freescale IMX serial ports
4  *
5  * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6  *
7  * Author: Sascha Hauer <sascha@saschahauer.de>
8  * Copyright (C) 2004 Pengutronix
9  */
10 
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>
27 #include <linux/of.h>
28 #include <linux/io.h>
29 #include <linux/dma-mapping.h>
30 
31 #include <asm/irq.h>
32 #include <linux/dma/imx-dma.h>
33 
34 #include "serial_mctrl_gpio.h"
35 
36 /* Register definitions */
37 #define URXD0 0x0  /* Receiver Register */
38 #define URTX0 0x40 /* Transmitter Register */
39 #define UCR1  0x80 /* Control Register 1 */
40 #define UCR2  0x84 /* Control Register 2 */
41 #define UCR3  0x88 /* Control Register 3 */
42 #define UCR4  0x8c /* Control Register 4 */
43 #define UFCR  0x90 /* FIFO Control Register */
44 #define USR1  0x94 /* Status Register 1 */
45 #define USR2  0x98 /* Status Register 2 */
46 #define UESC  0x9c /* Escape Character Register */
47 #define UTIM  0xa0 /* Escape Timer Register */
48 #define UBIR  0xa4 /* BRM Incremental Register */
49 #define UBMR  0xa8 /* BRM Modulator Register */
50 #define UBRC  0xac /* Baud Rate Count Register */
51 #define IMX21_ONEMS 0xb0 /* One Millisecond register */
52 #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
53 #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
54 
55 /* UART Control Register Bit Fields.*/
56 #define URXD_DUMMY_READ (1<<16)
57 #define URXD_CHARRDY	(1<<15)
58 #define URXD_ERR	(1<<14)
59 #define URXD_OVRRUN	(1<<13)
60 #define URXD_FRMERR	(1<<12)
61 #define URXD_BRK	(1<<11)
62 #define URXD_PRERR	(1<<10)
63 #define URXD_RX_DATA	(0xFF<<0)
64 #define UCR1_ADEN	(1<<15) /* Auto detect interrupt */
65 #define UCR1_ADBR	(1<<14) /* Auto detect baud rate */
66 #define UCR1_TRDYEN	(1<<13) /* Transmitter ready interrupt enable */
67 #define UCR1_IDEN	(1<<12) /* Idle condition interrupt */
68 #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
69 #define UCR1_RRDYEN	(1<<9)	/* Recv ready interrupt enable */
70 #define UCR1_RXDMAEN	(1<<8)	/* Recv ready DMA enable */
71 #define UCR1_IREN	(1<<7)	/* Infrared interface enable */
72 #define UCR1_TXMPTYEN	(1<<6)	/* Transimitter empty interrupt enable */
73 #define UCR1_RTSDEN	(1<<5)	/* RTS delta interrupt enable */
74 #define UCR1_SNDBRK	(1<<4)	/* Send break */
75 #define UCR1_TXDMAEN	(1<<3)	/* Transmitter ready DMA enable */
76 #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
77 #define UCR1_ATDMAEN    (1<<2)  /* Aging DMA Timer Enable */
78 #define UCR1_DOZE	(1<<1)	/* Doze */
79 #define UCR1_UARTEN	(1<<0)	/* UART enabled */
80 #define UCR2_ESCI	(1<<15)	/* Escape seq interrupt enable */
81 #define UCR2_IRTS	(1<<14)	/* Ignore RTS pin */
82 #define UCR2_CTSC	(1<<13)	/* CTS pin control */
83 #define UCR2_CTS	(1<<12)	/* Clear to send */
84 #define UCR2_ESCEN	(1<<11)	/* Escape enable */
85 #define UCR2_PREN	(1<<8)	/* Parity enable */
86 #define UCR2_PROE	(1<<7)	/* Parity odd/even */
87 #define UCR2_STPB	(1<<6)	/* Stop */
88 #define UCR2_WS		(1<<5)	/* Word size */
89 #define UCR2_RTSEN	(1<<4)	/* Request to send interrupt enable */
90 #define UCR2_ATEN	(1<<3)	/* Aging Timer Enable */
91 #define UCR2_TXEN	(1<<2)	/* Transmitter enabled */
92 #define UCR2_RXEN	(1<<1)	/* Receiver enabled */
93 #define UCR2_SRST	(1<<0)	/* SW reset */
94 #define UCR3_DTREN	(1<<13) /* DTR interrupt enable */
95 #define UCR3_PARERREN	(1<<12) /* Parity enable */
96 #define UCR3_FRAERREN	(1<<11) /* Frame error interrupt enable */
97 #define UCR3_DSR	(1<<10) /* Data set ready */
98 #define UCR3_DCD	(1<<9)	/* Data carrier detect */
99 #define UCR3_RI		(1<<8)	/* Ring indicator */
100 #define UCR3_ADNIMP	(1<<7)	/* Autobaud Detection Not Improved */
101 #define UCR3_RXDSEN	(1<<6)	/* Receive status interrupt enable */
102 #define UCR3_AIRINTEN	(1<<5)	/* Async IR wake interrupt enable */
103 #define UCR3_AWAKEN	(1<<4)	/* Async wake interrupt enable */
104 #define UCR3_DTRDEN	(1<<3)	/* Data Terminal Ready Delta Enable. */
105 #define IMX21_UCR3_RXDMUXSEL	(1<<2)	/* RXD Muxed Input Select */
106 #define UCR3_INVT	(1<<1)	/* Inverted Infrared transmission */
107 #define UCR3_BPEN	(1<<0)	/* Preset registers enable */
108 #define UCR4_CTSTL_SHF	10	/* CTS trigger level shift */
109 #define UCR4_CTSTL_MASK	0x3F	/* CTS trigger is 6 bits wide */
110 #define UCR4_INVR	(1<<9)	/* Inverted infrared reception */
111 #define UCR4_ENIRI	(1<<8)	/* Serial infrared interrupt enable */
112 #define UCR4_WKEN	(1<<7)	/* Wake interrupt enable */
113 #define UCR4_REF16	(1<<6)	/* Ref freq 16 MHz */
114 #define UCR4_IDDMAEN    (1<<6)  /* DMA IDLE Condition Detected */
115 #define UCR4_IRSC	(1<<5)	/* IR special case */
116 #define UCR4_TCEN	(1<<3)	/* Transmit complete interrupt enable */
117 #define UCR4_BKEN	(1<<2)	/* Break condition interrupt enable */
118 #define UCR4_OREN	(1<<1)	/* Receiver overrun interrupt enable */
119 #define UCR4_DREN	(1<<0)	/* Recv data ready interrupt enable */
120 #define UFCR_RXTL_SHF	0	/* Receiver trigger level shift */
121 #define UFCR_DCEDTE	(1<<6)	/* DCE/DTE mode select */
122 #define UFCR_RFDIV	(7<<7)	/* Reference freq divider mask */
123 #define UFCR_RFDIV_REG(x)	(((x) < 7 ? 6 - (x) : 6) << 7)
124 #define UFCR_TXTL_SHF	10	/* Transmitter trigger level shift */
125 #define USR1_PARITYERR	(1<<15) /* Parity error interrupt flag */
126 #define USR1_RTSS	(1<<14) /* RTS pin status */
127 #define USR1_TRDY	(1<<13) /* Transmitter ready interrupt/dma flag */
128 #define USR1_RTSD	(1<<12) /* RTS delta */
129 #define USR1_ESCF	(1<<11) /* Escape seq interrupt flag */
130 #define USR1_FRAMERR	(1<<10) /* Frame error interrupt flag */
131 #define USR1_RRDY	(1<<9)	 /* Receiver ready interrupt/dma flag */
132 #define USR1_AGTIM	(1<<8)	 /* Ageing timer interrupt flag */
133 #define USR1_DTRD	(1<<7)	 /* DTR Delta */
134 #define USR1_RXDS	 (1<<6)	 /* Receiver idle interrupt flag */
135 #define USR1_AIRINT	 (1<<5)	 /* Async IR wake interrupt flag */
136 #define USR1_AWAKE	 (1<<4)	 /* Aysnc wake interrupt flag */
137 #define USR2_ADET	 (1<<15) /* Auto baud rate detect complete */
138 #define USR2_TXFE	 (1<<14) /* Transmit buffer FIFO empty */
139 #define USR2_DTRF	 (1<<13) /* DTR edge interrupt flag */
140 #define USR2_IDLE	 (1<<12) /* Idle condition */
141 #define USR2_RIDELT	 (1<<10) /* Ring Interrupt Delta */
142 #define USR2_RIIN	 (1<<9)	 /* Ring Indicator Input */
143 #define USR2_IRINT	 (1<<8)	 /* Serial infrared interrupt flag */
144 #define USR2_WAKE	 (1<<7)	 /* Wake */
145 #define USR2_DCDIN	 (1<<5)	 /* Data Carrier Detect Input */
146 #define USR2_RTSF	 (1<<4)	 /* RTS edge interrupt flag */
147 #define USR2_TXDC	 (1<<3)	 /* Transmitter complete */
148 #define USR2_BRCD	 (1<<2)	 /* Break condition */
149 #define USR2_ORE	(1<<1)	 /* Overrun error */
150 #define USR2_RDR	(1<<0)	 /* Recv data ready */
151 #define UTS_FRCPERR	(1<<13) /* Force parity error */
152 #define UTS_LOOP	(1<<12)	 /* Loop tx and rx */
153 #define UTS_TXEMPTY	 (1<<6)	 /* TxFIFO empty */
154 #define UTS_RXEMPTY	 (1<<5)	 /* RxFIFO empty */
155 #define UTS_TXFULL	 (1<<4)	 /* TxFIFO full */
156 #define UTS_RXFULL	 (1<<3)	 /* RxFIFO full */
157 #define UTS_SOFTRST	 (1<<0)	 /* Software reset */
158 
159 /* We've been assigned a range on the "Low-density serial ports" major */
160 #define SERIAL_IMX_MAJOR	207
161 #define MINOR_START		16
162 #define DEV_NAME		"ttymxc"
163 
164 /*
165  * This determines how often we check the modem status signals
166  * for any change.  They generally aren't connected to an IRQ
167  * so we have to poll them.  We also check immediately before
168  * filling the TX fifo incase CTS has been dropped.
169  */
170 #define MCTRL_TIMEOUT	(250*HZ/1000)
171 
172 #define DRIVER_NAME "IMX-uart"
173 
174 #define UART_NR 8
175 
176 /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
177 enum imx_uart_type {
178 	IMX1_UART,
179 	IMX21_UART,
180 	IMX53_UART,
181 	IMX6Q_UART,
182 };
183 
184 /* device type dependent stuff */
185 struct imx_uart_data {
186 	unsigned uts_reg;
187 	enum imx_uart_type devtype;
188 };
189 
190 enum imx_tx_state {
191 	OFF,
192 	WAIT_AFTER_RTS,
193 	SEND,
194 	WAIT_AFTER_SEND,
195 };
196 
197 struct imx_port {
198 	struct uart_port	port;
199 	struct timer_list	timer;
200 	unsigned int		old_status;
201 	unsigned int		have_rtscts:1;
202 	unsigned int		have_rtsgpio:1;
203 	unsigned int		dte_mode:1;
204 	unsigned int		inverted_tx:1;
205 	unsigned int		inverted_rx:1;
206 	struct clk		*clk_ipg;
207 	struct clk		*clk_per;
208 	const struct imx_uart_data *devdata;
209 
210 	struct mctrl_gpios *gpios;
211 
212 	/* counter to stop 0xff flood */
213 	int idle_counter;
214 
215 	/* DMA fields */
216 	unsigned int		dma_is_enabled:1;
217 	unsigned int		dma_is_rxing:1;
218 	unsigned int		dma_is_txing:1;
219 	struct dma_chan		*dma_chan_rx, *dma_chan_tx;
220 	struct scatterlist	rx_sgl, tx_sgl[2];
221 	void			*rx_buf;
222 	struct circ_buf		rx_ring;
223 	unsigned int		rx_buf_size;
224 	unsigned int		rx_period_length;
225 	unsigned int		rx_periods;
226 	dma_cookie_t		rx_cookie;
227 	unsigned int		tx_bytes;
228 	unsigned int		dma_tx_nents;
229 	unsigned int            saved_reg[10];
230 	bool			context_saved;
231 
232 	enum imx_tx_state	tx_state;
233 	struct hrtimer		trigger_start_tx;
234 	struct hrtimer		trigger_stop_tx;
235 };
236 
237 struct imx_port_ucrs {
238 	unsigned int	ucr1;
239 	unsigned int	ucr2;
240 	unsigned int	ucr3;
241 };
242 
243 static struct imx_uart_data imx_uart_devdata[] = {
244 	[IMX1_UART] = {
245 		.uts_reg = IMX1_UTS,
246 		.devtype = IMX1_UART,
247 	},
248 	[IMX21_UART] = {
249 		.uts_reg = IMX21_UTS,
250 		.devtype = IMX21_UART,
251 	},
252 	[IMX53_UART] = {
253 		.uts_reg = IMX21_UTS,
254 		.devtype = IMX53_UART,
255 	},
256 	[IMX6Q_UART] = {
257 		.uts_reg = IMX21_UTS,
258 		.devtype = IMX6Q_UART,
259 	},
260 };
261 
262 static const struct of_device_id imx_uart_dt_ids[] = {
263 	{ .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
264 	{ .compatible = "fsl,imx53-uart", .data = &imx_uart_devdata[IMX53_UART], },
265 	{ .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
266 	{ .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
267 	{ /* sentinel */ }
268 };
269 MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
270 
271 static inline void imx_uart_writel(struct imx_port *sport, u32 val, u32 offset)
272 {
273 	writel(val, sport->port.membase + offset);
274 }
275 
276 static inline u32 imx_uart_readl(struct imx_port *sport, u32 offset)
277 {
278 	return readl(sport->port.membase + offset);
279 }
280 
281 static inline unsigned imx_uart_uts_reg(struct imx_port *sport)
282 {
283 	return sport->devdata->uts_reg;
284 }
285 
286 static inline int imx_uart_is_imx1(struct imx_port *sport)
287 {
288 	return sport->devdata->devtype == IMX1_UART;
289 }
290 
291 /*
292  * Save and restore functions for UCR1, UCR2 and UCR3 registers
293  */
294 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
295 static void imx_uart_ucrs_save(struct imx_port *sport,
296 			       struct imx_port_ucrs *ucr)
297 {
298 	/* save control registers */
299 	ucr->ucr1 = imx_uart_readl(sport, UCR1);
300 	ucr->ucr2 = imx_uart_readl(sport, UCR2);
301 	ucr->ucr3 = imx_uart_readl(sport, UCR3);
302 }
303 
304 static void imx_uart_ucrs_restore(struct imx_port *sport,
305 				  struct imx_port_ucrs *ucr)
306 {
307 	/* restore control registers */
308 	imx_uart_writel(sport, ucr->ucr1, UCR1);
309 	imx_uart_writel(sport, ucr->ucr2, UCR2);
310 	imx_uart_writel(sport, ucr->ucr3, UCR3);
311 }
312 #endif
313 
314 /* called with port.lock taken and irqs caller dependent */
315 static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2)
316 {
317 	*ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
318 
319 	mctrl_gpio_set(sport->gpios, sport->port.mctrl | TIOCM_RTS);
320 }
321 
322 /* called with port.lock taken and irqs caller dependent */
323 static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2)
324 {
325 	*ucr2 &= ~UCR2_CTSC;
326 	*ucr2 |= UCR2_CTS;
327 
328 	mctrl_gpio_set(sport->gpios, sport->port.mctrl & ~TIOCM_RTS);
329 }
330 
331 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
332 {
333        hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
334 }
335 
336 /* called with port.lock taken and irqs off */
337 static void imx_uart_soft_reset(struct imx_port *sport)
338 {
339 	int i = 10;
340 	u32 ucr2, ubir, ubmr, uts;
341 
342 	/*
343 	 * According to the Reference Manual description of the UART SRST bit:
344 	 *
345 	 * "Reset the transmit and receive state machines,
346 	 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
347 	 * and UTS[6-3]".
348 	 *
349 	 * We don't need to restore the old values from USR1, USR2, URXD and
350 	 * UTXD. UBRC is read only, so only save/restore the other three
351 	 * registers.
352 	 */
353 	ubir = imx_uart_readl(sport, UBIR);
354 	ubmr = imx_uart_readl(sport, UBMR);
355 	uts = imx_uart_readl(sport, IMX21_UTS);
356 
357 	ucr2 = imx_uart_readl(sport, UCR2);
358 	imx_uart_writel(sport, ucr2 & ~UCR2_SRST, UCR2);
359 
360 	while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
361 		udelay(1);
362 
363 	/* Restore the registers */
364 	imx_uart_writel(sport, ubir, UBIR);
365 	imx_uart_writel(sport, ubmr, UBMR);
366 	imx_uart_writel(sport, uts, IMX21_UTS);
367 
368 	sport->idle_counter = 0;
369 }
370 
371 static void imx_uart_disable_loopback_rs485(struct imx_port *sport)
372 {
373 	unsigned int uts;
374 
375 	/* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
376 	uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
377 	uts &= ~UTS_LOOP;
378 	imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
379 }
380 
381 /* called with port.lock taken and irqs off */
382 static void imx_uart_start_rx(struct uart_port *port)
383 {
384 	struct imx_port *sport = (struct imx_port *)port;
385 	unsigned int ucr1, ucr2;
386 
387 	ucr1 = imx_uart_readl(sport, UCR1);
388 	ucr2 = imx_uart_readl(sport, UCR2);
389 
390 	ucr2 |= UCR2_RXEN;
391 
392 	if (sport->dma_is_enabled) {
393 		ucr1 |= UCR1_RXDMAEN | UCR1_ATDMAEN;
394 	} else {
395 		ucr1 |= UCR1_RRDYEN;
396 		ucr2 |= UCR2_ATEN;
397 	}
398 
399 	/* Write UCR2 first as it includes RXEN */
400 	imx_uart_writel(sport, ucr2, UCR2);
401 	imx_uart_writel(sport, ucr1, UCR1);
402 	imx_uart_disable_loopback_rs485(sport);
403 }
404 
405 /* called with port.lock taken and irqs off */
406 static void imx_uart_stop_tx(struct uart_port *port)
407 {
408 	struct imx_port *sport = (struct imx_port *)port;
409 	u32 ucr1, ucr4, usr2;
410 
411 	if (sport->tx_state == OFF)
412 		return;
413 
414 	/*
415 	 * We are maybe in the SMP context, so if the DMA TX thread is running
416 	 * on other cpu, we have to wait for it to finish.
417 	 */
418 	if (sport->dma_is_txing)
419 		return;
420 
421 	ucr1 = imx_uart_readl(sport, UCR1);
422 	imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
423 
424 	ucr4 = imx_uart_readl(sport, UCR4);
425 	usr2 = imx_uart_readl(sport, USR2);
426 	if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
427 		/* The shifter is still busy, so retry once TC triggers */
428 		return;
429 	}
430 
431 	ucr4 &= ~UCR4_TCEN;
432 	imx_uart_writel(sport, ucr4, UCR4);
433 
434 	/* in rs485 mode disable transmitter */
435 	if (port->rs485.flags & SER_RS485_ENABLED) {
436 		if (sport->tx_state == SEND) {
437 			sport->tx_state = WAIT_AFTER_SEND;
438 
439 			if (port->rs485.delay_rts_after_send > 0) {
440 				start_hrtimer_ms(&sport->trigger_stop_tx,
441 					 port->rs485.delay_rts_after_send);
442 				return;
443 			}
444 
445 			/* continue without any delay */
446 		}
447 
448 		if (sport->tx_state == WAIT_AFTER_RTS ||
449 		    sport->tx_state == WAIT_AFTER_SEND) {
450 			u32 ucr2;
451 
452 			hrtimer_try_to_cancel(&sport->trigger_start_tx);
453 
454 			ucr2 = imx_uart_readl(sport, UCR2);
455 			if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
456 				imx_uart_rts_active(sport, &ucr2);
457 			else
458 				imx_uart_rts_inactive(sport, &ucr2);
459 			imx_uart_writel(sport, ucr2, UCR2);
460 
461 			if (!port->rs485_rx_during_tx_gpio)
462 				imx_uart_start_rx(port);
463 
464 			sport->tx_state = OFF;
465 		}
466 	} else {
467 		sport->tx_state = OFF;
468 	}
469 }
470 
471 static void imx_uart_stop_rx_with_loopback_ctrl(struct uart_port *port, bool loopback)
472 {
473 	struct imx_port *sport = (struct imx_port *)port;
474 	u32 ucr1, ucr2, ucr4, uts;
475 
476 	ucr1 = imx_uart_readl(sport, UCR1);
477 	ucr2 = imx_uart_readl(sport, UCR2);
478 	ucr4 = imx_uart_readl(sport, UCR4);
479 
480 	if (sport->dma_is_enabled) {
481 		ucr1 &= ~(UCR1_RXDMAEN | UCR1_ATDMAEN);
482 	} else {
483 		ucr1 &= ~UCR1_RRDYEN;
484 		ucr2 &= ~UCR2_ATEN;
485 		ucr4 &= ~UCR4_OREN;
486 	}
487 	imx_uart_writel(sport, ucr1, UCR1);
488 	imx_uart_writel(sport, ucr4, UCR4);
489 
490 	/* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
491 	if (port->rs485.flags & SER_RS485_ENABLED &&
492 	    port->rs485.flags & SER_RS485_RTS_ON_SEND &&
493 	    sport->have_rtscts && !sport->have_rtsgpio && loopback) {
494 		uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
495 		uts |= UTS_LOOP;
496 		imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
497 		ucr2 |= UCR2_RXEN;
498 	} else {
499 		ucr2 &= ~UCR2_RXEN;
500 	}
501 
502 	imx_uart_writel(sport, ucr2, UCR2);
503 }
504 
505 /* called with port.lock taken and irqs off */
506 static void imx_uart_stop_rx(struct uart_port *port)
507 {
508 	/*
509 	 * Stop RX and enable loopback in order to make sure RS485 bus
510 	 * is not blocked. Se comment in imx_uart_probe().
511 	 */
512 	imx_uart_stop_rx_with_loopback_ctrl(port, true);
513 }
514 
515 /* called with port.lock taken and irqs off */
516 static void imx_uart_enable_ms(struct uart_port *port)
517 {
518 	struct imx_port *sport = (struct imx_port *)port;
519 
520 	mod_timer(&sport->timer, jiffies);
521 
522 	mctrl_gpio_enable_ms(sport->gpios);
523 }
524 
525 static void imx_uart_dma_tx(struct imx_port *sport);
526 
527 /* called with port.lock taken and irqs off */
528 static inline void imx_uart_transmit_buffer(struct imx_port *sport)
529 {
530 	struct circ_buf *xmit = &sport->port.state->xmit;
531 
532 	if (sport->port.x_char) {
533 		/* Send next char */
534 		imx_uart_writel(sport, sport->port.x_char, URTX0);
535 		sport->port.icount.tx++;
536 		sport->port.x_char = 0;
537 		return;
538 	}
539 
540 	if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
541 		imx_uart_stop_tx(&sport->port);
542 		return;
543 	}
544 
545 	if (sport->dma_is_enabled) {
546 		u32 ucr1;
547 		/*
548 		 * We've just sent a X-char Ensure the TX DMA is enabled
549 		 * and the TX IRQ is disabled.
550 		 **/
551 		ucr1 = imx_uart_readl(sport, UCR1);
552 		ucr1 &= ~UCR1_TRDYEN;
553 		if (sport->dma_is_txing) {
554 			ucr1 |= UCR1_TXDMAEN;
555 			imx_uart_writel(sport, ucr1, UCR1);
556 		} else {
557 			imx_uart_writel(sport, ucr1, UCR1);
558 			imx_uart_dma_tx(sport);
559 		}
560 
561 		return;
562 	}
563 
564 	while (!uart_circ_empty(xmit) &&
565 	       !(imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)) {
566 		/* send xmit->buf[xmit->tail]
567 		 * out the port here */
568 		imx_uart_writel(sport, xmit->buf[xmit->tail], URTX0);
569 		uart_xmit_advance(&sport->port, 1);
570 	}
571 
572 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
573 		uart_write_wakeup(&sport->port);
574 
575 	if (uart_circ_empty(xmit))
576 		imx_uart_stop_tx(&sport->port);
577 }
578 
579 static void imx_uart_dma_tx_callback(void *data)
580 {
581 	struct imx_port *sport = data;
582 	struct scatterlist *sgl = &sport->tx_sgl[0];
583 	struct circ_buf *xmit = &sport->port.state->xmit;
584 	unsigned long flags;
585 	u32 ucr1;
586 
587 	spin_lock_irqsave(&sport->port.lock, flags);
588 
589 	dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
590 
591 	ucr1 = imx_uart_readl(sport, UCR1);
592 	ucr1 &= ~UCR1_TXDMAEN;
593 	imx_uart_writel(sport, ucr1, UCR1);
594 
595 	uart_xmit_advance(&sport->port, sport->tx_bytes);
596 
597 	dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
598 
599 	sport->dma_is_txing = 0;
600 
601 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
602 		uart_write_wakeup(&sport->port);
603 
604 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
605 		imx_uart_dma_tx(sport);
606 	else if (sport->port.rs485.flags & SER_RS485_ENABLED) {
607 		u32 ucr4 = imx_uart_readl(sport, UCR4);
608 		ucr4 |= UCR4_TCEN;
609 		imx_uart_writel(sport, ucr4, UCR4);
610 	}
611 
612 	spin_unlock_irqrestore(&sport->port.lock, flags);
613 }
614 
615 /* called with port.lock taken and irqs off */
616 static void imx_uart_dma_tx(struct imx_port *sport)
617 {
618 	struct circ_buf *xmit = &sport->port.state->xmit;
619 	struct scatterlist *sgl = sport->tx_sgl;
620 	struct dma_async_tx_descriptor *desc;
621 	struct dma_chan	*chan = sport->dma_chan_tx;
622 	struct device *dev = sport->port.dev;
623 	u32 ucr1, ucr4;
624 	int ret;
625 
626 	if (sport->dma_is_txing)
627 		return;
628 
629 	ucr4 = imx_uart_readl(sport, UCR4);
630 	ucr4 &= ~UCR4_TCEN;
631 	imx_uart_writel(sport, ucr4, UCR4);
632 
633 	sport->tx_bytes = uart_circ_chars_pending(xmit);
634 
635 	if (xmit->tail < xmit->head || xmit->head == 0) {
636 		sport->dma_tx_nents = 1;
637 		sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
638 	} else {
639 		sport->dma_tx_nents = 2;
640 		sg_init_table(sgl, 2);
641 		sg_set_buf(sgl, xmit->buf + xmit->tail,
642 				UART_XMIT_SIZE - xmit->tail);
643 		sg_set_buf(sgl + 1, xmit->buf, xmit->head);
644 	}
645 
646 	ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
647 	if (ret == 0) {
648 		dev_err(dev, "DMA mapping error for TX.\n");
649 		return;
650 	}
651 	desc = dmaengine_prep_slave_sg(chan, sgl, ret,
652 					DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
653 	if (!desc) {
654 		dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
655 			     DMA_TO_DEVICE);
656 		dev_err(dev, "We cannot prepare for the TX slave dma!\n");
657 		return;
658 	}
659 	desc->callback = imx_uart_dma_tx_callback;
660 	desc->callback_param = sport;
661 
662 	dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
663 			uart_circ_chars_pending(xmit));
664 
665 	ucr1 = imx_uart_readl(sport, UCR1);
666 	ucr1 |= UCR1_TXDMAEN;
667 	imx_uart_writel(sport, ucr1, UCR1);
668 
669 	/* fire it */
670 	sport->dma_is_txing = 1;
671 	dmaengine_submit(desc);
672 	dma_async_issue_pending(chan);
673 	return;
674 }
675 
676 /* called with port.lock taken and irqs off */
677 static void imx_uart_start_tx(struct uart_port *port)
678 {
679 	struct imx_port *sport = (struct imx_port *)port;
680 	u32 ucr1;
681 
682 	if (!sport->port.x_char && uart_circ_empty(&port->state->xmit))
683 		return;
684 
685 	/*
686 	 * We cannot simply do nothing here if sport->tx_state == SEND already
687 	 * because UCR1_TXMPTYEN might already have been cleared in
688 	 * imx_uart_stop_tx(), but tx_state is still SEND.
689 	 */
690 
691 	if (port->rs485.flags & SER_RS485_ENABLED) {
692 		if (sport->tx_state == OFF) {
693 			u32 ucr2 = imx_uart_readl(sport, UCR2);
694 			if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
695 				imx_uart_rts_active(sport, &ucr2);
696 			else
697 				imx_uart_rts_inactive(sport, &ucr2);
698 			imx_uart_writel(sport, ucr2, UCR2);
699 
700 			/*
701 			 * Since we are about to transmit we can not stop RX
702 			 * with loopback enabled because that will make our
703 			 * transmitted data being just looped to RX.
704 			 */
705 			if (!(port->rs485.flags & SER_RS485_RX_DURING_TX) &&
706 			    !port->rs485_rx_during_tx_gpio)
707 				imx_uart_stop_rx_with_loopback_ctrl(port, false);
708 
709 			sport->tx_state = WAIT_AFTER_RTS;
710 
711 			if (port->rs485.delay_rts_before_send > 0) {
712 				start_hrtimer_ms(&sport->trigger_start_tx,
713 					 port->rs485.delay_rts_before_send);
714 				return;
715 			}
716 
717 			/* continue without any delay */
718 		}
719 
720 		if (sport->tx_state == WAIT_AFTER_SEND
721 		    || sport->tx_state == WAIT_AFTER_RTS) {
722 
723 			hrtimer_try_to_cancel(&sport->trigger_stop_tx);
724 
725 			/*
726 			 * Enable transmitter and shifter empty irq only if DMA
727 			 * is off.  In the DMA case this is done in the
728 			 * tx-callback.
729 			 */
730 			if (!sport->dma_is_enabled) {
731 				u32 ucr4 = imx_uart_readl(sport, UCR4);
732 				ucr4 |= UCR4_TCEN;
733 				imx_uart_writel(sport, ucr4, UCR4);
734 			}
735 
736 			sport->tx_state = SEND;
737 		}
738 	} else {
739 		sport->tx_state = SEND;
740 	}
741 
742 	if (!sport->dma_is_enabled) {
743 		ucr1 = imx_uart_readl(sport, UCR1);
744 		imx_uart_writel(sport, ucr1 | UCR1_TRDYEN, UCR1);
745 	}
746 
747 	if (sport->dma_is_enabled) {
748 		if (sport->port.x_char) {
749 			/* We have X-char to send, so enable TX IRQ and
750 			 * disable TX DMA to let TX interrupt to send X-char */
751 			ucr1 = imx_uart_readl(sport, UCR1);
752 			ucr1 &= ~UCR1_TXDMAEN;
753 			ucr1 |= UCR1_TRDYEN;
754 			imx_uart_writel(sport, ucr1, UCR1);
755 			return;
756 		}
757 
758 		if (!uart_circ_empty(&port->state->xmit) &&
759 		    !uart_tx_stopped(port))
760 			imx_uart_dma_tx(sport);
761 		return;
762 	}
763 }
764 
765 static irqreturn_t __imx_uart_rtsint(int irq, void *dev_id)
766 {
767 	struct imx_port *sport = dev_id;
768 	u32 usr1;
769 
770 	imx_uart_writel(sport, USR1_RTSD, USR1);
771 	usr1 = imx_uart_readl(sport, USR1) & USR1_RTSS;
772 	uart_handle_cts_change(&sport->port, usr1);
773 	wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
774 
775 	return IRQ_HANDLED;
776 }
777 
778 static irqreturn_t imx_uart_rtsint(int irq, void *dev_id)
779 {
780 	struct imx_port *sport = dev_id;
781 	irqreturn_t ret;
782 
783 	spin_lock(&sport->port.lock);
784 
785 	ret = __imx_uart_rtsint(irq, dev_id);
786 
787 	spin_unlock(&sport->port.lock);
788 
789 	return ret;
790 }
791 
792 static irqreturn_t imx_uart_txint(int irq, void *dev_id)
793 {
794 	struct imx_port *sport = dev_id;
795 
796 	spin_lock(&sport->port.lock);
797 	imx_uart_transmit_buffer(sport);
798 	spin_unlock(&sport->port.lock);
799 	return IRQ_HANDLED;
800 }
801 
802 /* Check if hardware Rx flood is in progress, and issue soft reset to stop it.
803  * This is to be called from Rx ISRs only when some bytes were actually
804  * received.
805  *
806  * A way to reproduce the flood (checked on iMX6SX) is: open iMX UART at 9600
807  * 8N1, and from external source send 0xf0 char at 115200 8N1. In about 90% of
808  * cases this starts a flood of "receiving" of 0xff characters by the iMX6 UART
809  * that is terminated by any activity on RxD line, or could be stopped by
810  * issuing soft reset to the UART (just stop/start of RX does not help). Note
811  * that what we do here is sending isolated start bit about 2.4 times shorter
812  * than it is to be on UART configured baud rate.
813  */
814 static void imx_uart_check_flood(struct imx_port *sport, u32 usr2)
815 {
816 	/* To detect hardware 0xff flood we monitor RxD line between RX
817 	 * interrupts to isolate "receiving" of char(s) with no activity
818 	 * on RxD line, that'd never happen on actual data transfers.
819 	 *
820 	 * We use USR2_WAKE bit to check for activity on RxD line, but we have a
821 	 * race here if we clear USR2_WAKE when receiving of a char is in
822 	 * progress, so we might get RX interrupt later with USR2_WAKE bit
823 	 * cleared. Note though that as we don't try to clear USR2_WAKE when we
824 	 * detected no activity, this race may hide actual activity only once.
825 	 *
826 	 * Yet another case where receive interrupt may occur without RxD
827 	 * activity is expiration of aging timer, so we consider this as well.
828 	 *
829 	 * We use 'idle_counter' to ensure that we got at least so many RX
830 	 * interrupts without any detected activity on RxD line. 2 cases
831 	 * described plus 1 to be on the safe side gives us a margin of 3,
832 	 * below. In practice I was not able to produce a false positive to
833 	 * induce soft reset at regular data transfers even using 1 as the
834 	 * margin, so 3 is actually very strong.
835 	 *
836 	 * We count interrupts, not chars in 'idle-counter' for simplicity.
837 	 */
838 
839 	if (usr2 & USR2_WAKE) {
840 		imx_uart_writel(sport, USR2_WAKE, USR2);
841 		sport->idle_counter = 0;
842 	} else if (++sport->idle_counter > 3) {
843 		dev_warn(sport->port.dev, "RX flood detected: soft reset.");
844 		imx_uart_soft_reset(sport); /* also clears 'sport->idle_counter' */
845 	}
846 }
847 
848 static irqreturn_t __imx_uart_rxint(int irq, void *dev_id)
849 {
850 	struct imx_port *sport = dev_id;
851 	struct tty_port *port = &sport->port.state->port;
852 	u32 usr2, rx;
853 
854 	/* If we received something, check for 0xff flood */
855 	usr2 = imx_uart_readl(sport, USR2);
856 	if (usr2 & USR2_RDR)
857 		imx_uart_check_flood(sport, usr2);
858 
859 	while ((rx = imx_uart_readl(sport, URXD0)) & URXD_CHARRDY) {
860 		unsigned int flg = TTY_NORMAL;
861 		sport->port.icount.rx++;
862 
863 		if (unlikely(rx & URXD_ERR)) {
864 			if (rx & URXD_BRK) {
865 				sport->port.icount.brk++;
866 				if (uart_handle_break(&sport->port))
867 					continue;
868 			}
869 			else if (rx & URXD_PRERR)
870 				sport->port.icount.parity++;
871 			else if (rx & URXD_FRMERR)
872 				sport->port.icount.frame++;
873 			if (rx & URXD_OVRRUN)
874 				sport->port.icount.overrun++;
875 
876 			if (rx & sport->port.ignore_status_mask)
877 				continue;
878 
879 			rx &= (sport->port.read_status_mask | 0xFF);
880 
881 			if (rx & URXD_BRK)
882 				flg = TTY_BREAK;
883 			else if (rx & URXD_PRERR)
884 				flg = TTY_PARITY;
885 			else if (rx & URXD_FRMERR)
886 				flg = TTY_FRAME;
887 			if (rx & URXD_OVRRUN)
888 				flg = TTY_OVERRUN;
889 
890 			sport->port.sysrq = 0;
891 		} else if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx)) {
892 			continue;
893 		}
894 
895 		if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
896 			continue;
897 
898 		if (tty_insert_flip_char(port, rx, flg) == 0)
899 			sport->port.icount.buf_overrun++;
900 	}
901 
902 	tty_flip_buffer_push(port);
903 
904 	return IRQ_HANDLED;
905 }
906 
907 static irqreturn_t imx_uart_rxint(int irq, void *dev_id)
908 {
909 	struct imx_port *sport = dev_id;
910 	irqreturn_t ret;
911 
912 	spin_lock(&sport->port.lock);
913 
914 	ret = __imx_uart_rxint(irq, dev_id);
915 
916 	spin_unlock(&sport->port.lock);
917 
918 	return ret;
919 }
920 
921 static void imx_uart_clear_rx_errors(struct imx_port *sport);
922 
923 /*
924  * We have a modem side uart, so the meanings of RTS and CTS are inverted.
925  */
926 static unsigned int imx_uart_get_hwmctrl(struct imx_port *sport)
927 {
928 	unsigned int tmp = TIOCM_DSR;
929 	unsigned usr1 = imx_uart_readl(sport, USR1);
930 	unsigned usr2 = imx_uart_readl(sport, USR2);
931 
932 	if (usr1 & USR1_RTSS)
933 		tmp |= TIOCM_CTS;
934 
935 	/* in DCE mode DCDIN is always 0 */
936 	if (!(usr2 & USR2_DCDIN))
937 		tmp |= TIOCM_CAR;
938 
939 	if (sport->dte_mode)
940 		if (!(imx_uart_readl(sport, USR2) & USR2_RIIN))
941 			tmp |= TIOCM_RI;
942 
943 	return tmp;
944 }
945 
946 /*
947  * Handle any change of modem status signal since we were last called.
948  */
949 static void imx_uart_mctrl_check(struct imx_port *sport)
950 {
951 	unsigned int status, changed;
952 
953 	status = imx_uart_get_hwmctrl(sport);
954 	changed = status ^ sport->old_status;
955 
956 	if (changed == 0)
957 		return;
958 
959 	sport->old_status = status;
960 
961 	if (changed & TIOCM_RI && status & TIOCM_RI)
962 		sport->port.icount.rng++;
963 	if (changed & TIOCM_DSR)
964 		sport->port.icount.dsr++;
965 	if (changed & TIOCM_CAR)
966 		uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
967 	if (changed & TIOCM_CTS)
968 		uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
969 
970 	wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
971 }
972 
973 static irqreturn_t imx_uart_int(int irq, void *dev_id)
974 {
975 	struct imx_port *sport = dev_id;
976 	unsigned int usr1, usr2, ucr1, ucr2, ucr3, ucr4;
977 	irqreturn_t ret = IRQ_NONE;
978 
979 	spin_lock(&sport->port.lock);
980 
981 	usr1 = imx_uart_readl(sport, USR1);
982 	usr2 = imx_uart_readl(sport, USR2);
983 	ucr1 = imx_uart_readl(sport, UCR1);
984 	ucr2 = imx_uart_readl(sport, UCR2);
985 	ucr3 = imx_uart_readl(sport, UCR3);
986 	ucr4 = imx_uart_readl(sport, UCR4);
987 
988 	/*
989 	 * Even if a condition is true that can trigger an irq only handle it if
990 	 * the respective irq source is enabled. This prevents some undesired
991 	 * actions, for example if a character that sits in the RX FIFO and that
992 	 * should be fetched via DMA is tried to be fetched using PIO. Or the
993 	 * receiver is currently off and so reading from URXD0 results in an
994 	 * exception. So just mask the (raw) status bits for disabled irqs.
995 	 */
996 	if ((ucr1 & UCR1_RRDYEN) == 0)
997 		usr1 &= ~USR1_RRDY;
998 	if ((ucr2 & UCR2_ATEN) == 0)
999 		usr1 &= ~USR1_AGTIM;
1000 	if ((ucr1 & UCR1_TRDYEN) == 0)
1001 		usr1 &= ~USR1_TRDY;
1002 	if ((ucr4 & UCR4_TCEN) == 0)
1003 		usr2 &= ~USR2_TXDC;
1004 	if ((ucr3 & UCR3_DTRDEN) == 0)
1005 		usr1 &= ~USR1_DTRD;
1006 	if ((ucr1 & UCR1_RTSDEN) == 0)
1007 		usr1 &= ~USR1_RTSD;
1008 	if ((ucr3 & UCR3_AWAKEN) == 0)
1009 		usr1 &= ~USR1_AWAKE;
1010 	if ((ucr4 & UCR4_OREN) == 0)
1011 		usr2 &= ~USR2_ORE;
1012 
1013 	if (usr1 & (USR1_RRDY | USR1_AGTIM)) {
1014 		imx_uart_writel(sport, USR1_AGTIM, USR1);
1015 
1016 		__imx_uart_rxint(irq, dev_id);
1017 		ret = IRQ_HANDLED;
1018 	}
1019 
1020 	if ((usr1 & USR1_TRDY) || (usr2 & USR2_TXDC)) {
1021 		imx_uart_transmit_buffer(sport);
1022 		ret = IRQ_HANDLED;
1023 	}
1024 
1025 	if (usr1 & USR1_DTRD) {
1026 		imx_uart_writel(sport, USR1_DTRD, USR1);
1027 
1028 		imx_uart_mctrl_check(sport);
1029 
1030 		ret = IRQ_HANDLED;
1031 	}
1032 
1033 	if (usr1 & USR1_RTSD) {
1034 		__imx_uart_rtsint(irq, dev_id);
1035 		ret = IRQ_HANDLED;
1036 	}
1037 
1038 	if (usr1 & USR1_AWAKE) {
1039 		imx_uart_writel(sport, USR1_AWAKE, USR1);
1040 		ret = IRQ_HANDLED;
1041 	}
1042 
1043 	if (usr2 & USR2_ORE) {
1044 		sport->port.icount.overrun++;
1045 		imx_uart_writel(sport, USR2_ORE, USR2);
1046 		ret = IRQ_HANDLED;
1047 	}
1048 
1049 	spin_unlock(&sport->port.lock);
1050 
1051 	return ret;
1052 }
1053 
1054 /*
1055  * Return TIOCSER_TEMT when transmitter is not busy.
1056  */
1057 static unsigned int imx_uart_tx_empty(struct uart_port *port)
1058 {
1059 	struct imx_port *sport = (struct imx_port *)port;
1060 	unsigned int ret;
1061 
1062 	ret = (imx_uart_readl(sport, USR2) & USR2_TXDC) ?  TIOCSER_TEMT : 0;
1063 
1064 	/* If the TX DMA is working, return 0. */
1065 	if (sport->dma_is_txing)
1066 		ret = 0;
1067 
1068 	return ret;
1069 }
1070 
1071 /* called with port.lock taken and irqs off */
1072 static unsigned int imx_uart_get_mctrl(struct uart_port *port)
1073 {
1074 	struct imx_port *sport = (struct imx_port *)port;
1075 	unsigned int ret = imx_uart_get_hwmctrl(sport);
1076 
1077 	mctrl_gpio_get(sport->gpios, &ret);
1078 
1079 	return ret;
1080 }
1081 
1082 /* called with port.lock taken and irqs off */
1083 static void imx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1084 {
1085 	struct imx_port *sport = (struct imx_port *)port;
1086 	u32 ucr3, uts;
1087 
1088 	if (!(port->rs485.flags & SER_RS485_ENABLED)) {
1089 		u32 ucr2;
1090 
1091 		/*
1092 		 * Turn off autoRTS if RTS is lowered and restore autoRTS
1093 		 * setting if RTS is raised.
1094 		 */
1095 		ucr2 = imx_uart_readl(sport, UCR2);
1096 		ucr2 &= ~(UCR2_CTS | UCR2_CTSC);
1097 		if (mctrl & TIOCM_RTS) {
1098 			ucr2 |= UCR2_CTS;
1099 			/*
1100 			 * UCR2_IRTS is unset if and only if the port is
1101 			 * configured for CRTSCTS, so we use inverted UCR2_IRTS
1102 			 * to get the state to restore to.
1103 			 */
1104 			if (!(ucr2 & UCR2_IRTS))
1105 				ucr2 |= UCR2_CTSC;
1106 		}
1107 		imx_uart_writel(sport, ucr2, UCR2);
1108 	}
1109 
1110 	ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_DSR;
1111 	if (!(mctrl & TIOCM_DTR))
1112 		ucr3 |= UCR3_DSR;
1113 	imx_uart_writel(sport, ucr3, UCR3);
1114 
1115 	uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)) & ~UTS_LOOP;
1116 	if (mctrl & TIOCM_LOOP)
1117 		uts |= UTS_LOOP;
1118 	imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
1119 
1120 	mctrl_gpio_set(sport->gpios, mctrl);
1121 }
1122 
1123 /*
1124  * Interrupts always disabled.
1125  */
1126 static void imx_uart_break_ctl(struct uart_port *port, int break_state)
1127 {
1128 	struct imx_port *sport = (struct imx_port *)port;
1129 	unsigned long flags;
1130 	u32 ucr1;
1131 
1132 	spin_lock_irqsave(&sport->port.lock, flags);
1133 
1134 	ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_SNDBRK;
1135 
1136 	if (break_state != 0)
1137 		ucr1 |= UCR1_SNDBRK;
1138 
1139 	imx_uart_writel(sport, ucr1, UCR1);
1140 
1141 	spin_unlock_irqrestore(&sport->port.lock, flags);
1142 }
1143 
1144 /*
1145  * This is our per-port timeout handler, for checking the
1146  * modem status signals.
1147  */
1148 static void imx_uart_timeout(struct timer_list *t)
1149 {
1150 	struct imx_port *sport = from_timer(sport, t, timer);
1151 	unsigned long flags;
1152 
1153 	if (sport->port.state) {
1154 		spin_lock_irqsave(&sport->port.lock, flags);
1155 		imx_uart_mctrl_check(sport);
1156 		spin_unlock_irqrestore(&sport->port.lock, flags);
1157 
1158 		mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
1159 	}
1160 }
1161 
1162 /*
1163  * There are two kinds of RX DMA interrupts(such as in the MX6Q):
1164  *   [1] the RX DMA buffer is full.
1165  *   [2] the aging timer expires
1166  *
1167  * Condition [2] is triggered when a character has been sitting in the FIFO
1168  * for at least 8 byte durations.
1169  */
1170 static void imx_uart_dma_rx_callback(void *data)
1171 {
1172 	struct imx_port *sport = data;
1173 	struct dma_chan	*chan = sport->dma_chan_rx;
1174 	struct scatterlist *sgl = &sport->rx_sgl;
1175 	struct tty_port *port = &sport->port.state->port;
1176 	struct dma_tx_state state;
1177 	struct circ_buf *rx_ring = &sport->rx_ring;
1178 	enum dma_status status;
1179 	unsigned int w_bytes = 0;
1180 	unsigned int r_bytes;
1181 	unsigned int bd_size;
1182 
1183 	status = dmaengine_tx_status(chan, sport->rx_cookie, &state);
1184 
1185 	if (status == DMA_ERROR) {
1186 		spin_lock(&sport->port.lock);
1187 		imx_uart_clear_rx_errors(sport);
1188 		spin_unlock(&sport->port.lock);
1189 		return;
1190 	}
1191 
1192 	/*
1193 	 * The state-residue variable represents the empty space
1194 	 * relative to the entire buffer. Taking this in consideration
1195 	 * the head is always calculated base on the buffer total
1196 	 * length - DMA transaction residue. The UART script from the
1197 	 * SDMA firmware will jump to the next buffer descriptor,
1198 	 * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4).
1199 	 * Taking this in consideration the tail is always at the
1200 	 * beginning of the buffer descriptor that contains the head.
1201 	 */
1202 
1203 	/* Calculate the head */
1204 	rx_ring->head = sg_dma_len(sgl) - state.residue;
1205 
1206 	/* Calculate the tail. */
1207 	bd_size = sg_dma_len(sgl) / sport->rx_periods;
1208 	rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size;
1209 
1210 	if (rx_ring->head <= sg_dma_len(sgl) &&
1211 	    rx_ring->head > rx_ring->tail) {
1212 
1213 		/* Move data from tail to head */
1214 		r_bytes = rx_ring->head - rx_ring->tail;
1215 
1216 		/* If we received something, check for 0xff flood */
1217 		spin_lock(&sport->port.lock);
1218 		imx_uart_check_flood(sport, imx_uart_readl(sport, USR2));
1219 		spin_unlock(&sport->port.lock);
1220 
1221 		if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
1222 
1223 			/* CPU claims ownership of RX DMA buffer */
1224 			dma_sync_sg_for_cpu(sport->port.dev, sgl, 1,
1225 					    DMA_FROM_DEVICE);
1226 
1227 			w_bytes = tty_insert_flip_string(port,
1228 							 sport->rx_buf + rx_ring->tail, r_bytes);
1229 
1230 			/* UART retrieves ownership of RX DMA buffer */
1231 			dma_sync_sg_for_device(sport->port.dev, sgl, 1,
1232 					       DMA_FROM_DEVICE);
1233 
1234 			if (w_bytes != r_bytes)
1235 				sport->port.icount.buf_overrun++;
1236 
1237 			sport->port.icount.rx += w_bytes;
1238 		}
1239 	} else	{
1240 		WARN_ON(rx_ring->head > sg_dma_len(sgl));
1241 		WARN_ON(rx_ring->head <= rx_ring->tail);
1242 	}
1243 
1244 	if (w_bytes) {
1245 		tty_flip_buffer_push(port);
1246 		dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes);
1247 	}
1248 }
1249 
1250 static int imx_uart_start_rx_dma(struct imx_port *sport)
1251 {
1252 	struct scatterlist *sgl = &sport->rx_sgl;
1253 	struct dma_chan	*chan = sport->dma_chan_rx;
1254 	struct device *dev = sport->port.dev;
1255 	struct dma_async_tx_descriptor *desc;
1256 	int ret;
1257 
1258 	sport->rx_ring.head = 0;
1259 	sport->rx_ring.tail = 0;
1260 
1261 	sg_init_one(sgl, sport->rx_buf, sport->rx_buf_size);
1262 	ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1263 	if (ret == 0) {
1264 		dev_err(dev, "DMA mapping error for RX.\n");
1265 		return -EINVAL;
1266 	}
1267 
1268 	desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl),
1269 		sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods,
1270 		DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
1271 
1272 	if (!desc) {
1273 		dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1274 		dev_err(dev, "We cannot prepare for the RX slave dma!\n");
1275 		return -EINVAL;
1276 	}
1277 	desc->callback = imx_uart_dma_rx_callback;
1278 	desc->callback_param = sport;
1279 
1280 	dev_dbg(dev, "RX: prepare for the DMA.\n");
1281 	sport->dma_is_rxing = 1;
1282 	sport->rx_cookie = dmaengine_submit(desc);
1283 	dma_async_issue_pending(chan);
1284 	return 0;
1285 }
1286 
1287 static void imx_uart_clear_rx_errors(struct imx_port *sport)
1288 {
1289 	struct tty_port *port = &sport->port.state->port;
1290 	u32 usr1, usr2;
1291 
1292 	usr1 = imx_uart_readl(sport, USR1);
1293 	usr2 = imx_uart_readl(sport, USR2);
1294 
1295 	if (usr2 & USR2_BRCD) {
1296 		sport->port.icount.brk++;
1297 		imx_uart_writel(sport, USR2_BRCD, USR2);
1298 		uart_handle_break(&sport->port);
1299 		if (tty_insert_flip_char(port, 0, TTY_BREAK) == 0)
1300 			sport->port.icount.buf_overrun++;
1301 		tty_flip_buffer_push(port);
1302 	} else {
1303 		if (usr1 & USR1_FRAMERR) {
1304 			sport->port.icount.frame++;
1305 			imx_uart_writel(sport, USR1_FRAMERR, USR1);
1306 		} else if (usr1 & USR1_PARITYERR) {
1307 			sport->port.icount.parity++;
1308 			imx_uart_writel(sport, USR1_PARITYERR, USR1);
1309 		}
1310 	}
1311 
1312 	if (usr2 & USR2_ORE) {
1313 		sport->port.icount.overrun++;
1314 		imx_uart_writel(sport, USR2_ORE, USR2);
1315 	}
1316 
1317 	sport->idle_counter = 0;
1318 
1319 }
1320 
1321 #define TXTL_DEFAULT 2 /* reset default */
1322 #define RXTL_DEFAULT 8 /* 8 characters or aging timer */
1323 #define TXTL_DMA 8 /* DMA burst setting */
1324 #define RXTL_DMA 9 /* DMA burst setting */
1325 
1326 static void imx_uart_setup_ufcr(struct imx_port *sport,
1327 				unsigned char txwl, unsigned char rxwl)
1328 {
1329 	unsigned int val;
1330 
1331 	/* set receiver / transmitter trigger level */
1332 	val = imx_uart_readl(sport, UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
1333 	val |= txwl << UFCR_TXTL_SHF | rxwl;
1334 	imx_uart_writel(sport, val, UFCR);
1335 }
1336 
1337 static void imx_uart_dma_exit(struct imx_port *sport)
1338 {
1339 	if (sport->dma_chan_rx) {
1340 		dmaengine_terminate_sync(sport->dma_chan_rx);
1341 		dma_release_channel(sport->dma_chan_rx);
1342 		sport->dma_chan_rx = NULL;
1343 		sport->rx_cookie = -EINVAL;
1344 		kfree(sport->rx_buf);
1345 		sport->rx_buf = NULL;
1346 	}
1347 
1348 	if (sport->dma_chan_tx) {
1349 		dmaengine_terminate_sync(sport->dma_chan_tx);
1350 		dma_release_channel(sport->dma_chan_tx);
1351 		sport->dma_chan_tx = NULL;
1352 	}
1353 }
1354 
1355 static int imx_uart_dma_init(struct imx_port *sport)
1356 {
1357 	struct dma_slave_config slave_config = {};
1358 	struct device *dev = sport->port.dev;
1359 	int ret;
1360 
1361 	/* Prepare for RX : */
1362 	sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1363 	if (!sport->dma_chan_rx) {
1364 		dev_dbg(dev, "cannot get the DMA channel.\n");
1365 		ret = -EINVAL;
1366 		goto err;
1367 	}
1368 
1369 	slave_config.direction = DMA_DEV_TO_MEM;
1370 	slave_config.src_addr = sport->port.mapbase + URXD0;
1371 	slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1372 	/* one byte less than the watermark level to enable the aging timer */
1373 	slave_config.src_maxburst = RXTL_DMA - 1;
1374 	ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
1375 	if (ret) {
1376 		dev_err(dev, "error in RX dma configuration.\n");
1377 		goto err;
1378 	}
1379 
1380 	sport->rx_buf_size = sport->rx_period_length * sport->rx_periods;
1381 	sport->rx_buf = kzalloc(sport->rx_buf_size, GFP_KERNEL);
1382 	if (!sport->rx_buf) {
1383 		ret = -ENOMEM;
1384 		goto err;
1385 	}
1386 	sport->rx_ring.buf = sport->rx_buf;
1387 
1388 	/* Prepare for TX : */
1389 	sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1390 	if (!sport->dma_chan_tx) {
1391 		dev_err(dev, "cannot get the TX DMA channel!\n");
1392 		ret = -EINVAL;
1393 		goto err;
1394 	}
1395 
1396 	slave_config.direction = DMA_MEM_TO_DEV;
1397 	slave_config.dst_addr = sport->port.mapbase + URTX0;
1398 	slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1399 	slave_config.dst_maxburst = TXTL_DMA;
1400 	ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1401 	if (ret) {
1402 		dev_err(dev, "error in TX dma configuration.");
1403 		goto err;
1404 	}
1405 
1406 	return 0;
1407 err:
1408 	imx_uart_dma_exit(sport);
1409 	return ret;
1410 }
1411 
1412 static void imx_uart_enable_dma(struct imx_port *sport)
1413 {
1414 	u32 ucr1;
1415 
1416 	imx_uart_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
1417 
1418 	/* set UCR1 */
1419 	ucr1 = imx_uart_readl(sport, UCR1);
1420 	ucr1 |= UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN;
1421 	imx_uart_writel(sport, ucr1, UCR1);
1422 
1423 	sport->dma_is_enabled = 1;
1424 }
1425 
1426 static void imx_uart_disable_dma(struct imx_port *sport)
1427 {
1428 	u32 ucr1;
1429 
1430 	/* clear UCR1 */
1431 	ucr1 = imx_uart_readl(sport, UCR1);
1432 	ucr1 &= ~(UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN);
1433 	imx_uart_writel(sport, ucr1, UCR1);
1434 
1435 	imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1436 
1437 	sport->dma_is_enabled = 0;
1438 }
1439 
1440 /* half the RX buffer size */
1441 #define CTSTL 16
1442 
1443 static int imx_uart_startup(struct uart_port *port)
1444 {
1445 	struct imx_port *sport = (struct imx_port *)port;
1446 	int retval;
1447 	unsigned long flags;
1448 	int dma_is_inited = 0;
1449 	u32 ucr1, ucr2, ucr3, ucr4;
1450 
1451 	retval = clk_prepare_enable(sport->clk_per);
1452 	if (retval)
1453 		return retval;
1454 	retval = clk_prepare_enable(sport->clk_ipg);
1455 	if (retval) {
1456 		clk_disable_unprepare(sport->clk_per);
1457 		return retval;
1458 	}
1459 
1460 	imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1461 
1462 	/* disable the DREN bit (Data Ready interrupt enable) before
1463 	 * requesting IRQs
1464 	 */
1465 	ucr4 = imx_uart_readl(sport, UCR4);
1466 
1467 	/* set the trigger level for CTS */
1468 	ucr4 &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
1469 	ucr4 |= CTSTL << UCR4_CTSTL_SHF;
1470 
1471 	imx_uart_writel(sport, ucr4 & ~UCR4_DREN, UCR4);
1472 
1473 	/* Can we enable the DMA support? */
1474 	if (!uart_console(port) && imx_uart_dma_init(sport) == 0)
1475 		dma_is_inited = 1;
1476 
1477 	spin_lock_irqsave(&sport->port.lock, flags);
1478 
1479 	/* Reset fifo's and state machines */
1480 	imx_uart_soft_reset(sport);
1481 
1482 	/*
1483 	 * Finally, clear and enable interrupts
1484 	 */
1485 	imx_uart_writel(sport, USR1_RTSD | USR1_DTRD, USR1);
1486 	imx_uart_writel(sport, USR2_ORE, USR2);
1487 
1488 	ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_RRDYEN;
1489 	ucr1 |= UCR1_UARTEN;
1490 	if (sport->have_rtscts)
1491 		ucr1 |= UCR1_RTSDEN;
1492 
1493 	imx_uart_writel(sport, ucr1, UCR1);
1494 
1495 	ucr4 = imx_uart_readl(sport, UCR4) & ~(UCR4_OREN | UCR4_INVR);
1496 	if (!dma_is_inited)
1497 		ucr4 |= UCR4_OREN;
1498 	if (sport->inverted_rx)
1499 		ucr4 |= UCR4_INVR;
1500 	imx_uart_writel(sport, ucr4, UCR4);
1501 
1502 	ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_INVT;
1503 	/*
1504 	 * configure tx polarity before enabling tx
1505 	 */
1506 	if (sport->inverted_tx)
1507 		ucr3 |= UCR3_INVT;
1508 
1509 	if (!imx_uart_is_imx1(sport)) {
1510 		ucr3 |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
1511 
1512 		if (sport->dte_mode)
1513 			/* disable broken interrupts */
1514 			ucr3 &= ~(UCR3_RI | UCR3_DCD);
1515 	}
1516 	imx_uart_writel(sport, ucr3, UCR3);
1517 
1518 	ucr2 = imx_uart_readl(sport, UCR2) & ~UCR2_ATEN;
1519 	ucr2 |= (UCR2_RXEN | UCR2_TXEN);
1520 	if (!sport->have_rtscts)
1521 		ucr2 |= UCR2_IRTS;
1522 	/*
1523 	 * make sure the edge sensitive RTS-irq is disabled,
1524 	 * we're using RTSD instead.
1525 	 */
1526 	if (!imx_uart_is_imx1(sport))
1527 		ucr2 &= ~UCR2_RTSEN;
1528 	imx_uart_writel(sport, ucr2, UCR2);
1529 
1530 	/*
1531 	 * Enable modem status interrupts
1532 	 */
1533 	imx_uart_enable_ms(&sport->port);
1534 
1535 	if (dma_is_inited) {
1536 		imx_uart_enable_dma(sport);
1537 		imx_uart_start_rx_dma(sport);
1538 	} else {
1539 		ucr1 = imx_uart_readl(sport, UCR1);
1540 		ucr1 |= UCR1_RRDYEN;
1541 		imx_uart_writel(sport, ucr1, UCR1);
1542 
1543 		ucr2 = imx_uart_readl(sport, UCR2);
1544 		ucr2 |= UCR2_ATEN;
1545 		imx_uart_writel(sport, ucr2, UCR2);
1546 	}
1547 
1548 	imx_uart_disable_loopback_rs485(sport);
1549 
1550 	spin_unlock_irqrestore(&sport->port.lock, flags);
1551 
1552 	return 0;
1553 }
1554 
1555 static void imx_uart_shutdown(struct uart_port *port)
1556 {
1557 	struct imx_port *sport = (struct imx_port *)port;
1558 	unsigned long flags;
1559 	u32 ucr1, ucr2, ucr4, uts;
1560 
1561 	if (sport->dma_is_enabled) {
1562 		dmaengine_terminate_sync(sport->dma_chan_tx);
1563 		if (sport->dma_is_txing) {
1564 			dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
1565 				     sport->dma_tx_nents, DMA_TO_DEVICE);
1566 			sport->dma_is_txing = 0;
1567 		}
1568 		dmaengine_terminate_sync(sport->dma_chan_rx);
1569 		if (sport->dma_is_rxing) {
1570 			dma_unmap_sg(sport->port.dev, &sport->rx_sgl,
1571 				     1, DMA_FROM_DEVICE);
1572 			sport->dma_is_rxing = 0;
1573 		}
1574 
1575 		spin_lock_irqsave(&sport->port.lock, flags);
1576 		imx_uart_stop_tx(port);
1577 		imx_uart_stop_rx(port);
1578 		imx_uart_disable_dma(sport);
1579 		spin_unlock_irqrestore(&sport->port.lock, flags);
1580 		imx_uart_dma_exit(sport);
1581 	}
1582 
1583 	mctrl_gpio_disable_ms(sport->gpios);
1584 
1585 	spin_lock_irqsave(&sport->port.lock, flags);
1586 	ucr2 = imx_uart_readl(sport, UCR2);
1587 	ucr2 &= ~(UCR2_TXEN | UCR2_ATEN);
1588 	imx_uart_writel(sport, ucr2, UCR2);
1589 	spin_unlock_irqrestore(&sport->port.lock, flags);
1590 
1591 	/*
1592 	 * Stop our timer.
1593 	 */
1594 	del_timer_sync(&sport->timer);
1595 
1596 	/*
1597 	 * Disable all interrupts, port and break condition.
1598 	 */
1599 
1600 	spin_lock_irqsave(&sport->port.lock, flags);
1601 
1602 	ucr1 = imx_uart_readl(sport, UCR1);
1603 	ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_RXDMAEN |
1604 		  UCR1_ATDMAEN | UCR1_SNDBRK);
1605 	/* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
1606 	if (port->rs485.flags & SER_RS485_ENABLED &&
1607 	    port->rs485.flags & SER_RS485_RTS_ON_SEND &&
1608 	    sport->have_rtscts && !sport->have_rtsgpio) {
1609 		uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
1610 		uts |= UTS_LOOP;
1611 		imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
1612 		ucr1 |= UCR1_UARTEN;
1613 	} else {
1614 		ucr1 &= ~UCR1_UARTEN;
1615 	}
1616 	imx_uart_writel(sport, ucr1, UCR1);
1617 
1618 	ucr4 = imx_uart_readl(sport, UCR4);
1619 	ucr4 &= ~UCR4_TCEN;
1620 	imx_uart_writel(sport, ucr4, UCR4);
1621 
1622 	spin_unlock_irqrestore(&sport->port.lock, flags);
1623 
1624 	clk_disable_unprepare(sport->clk_per);
1625 	clk_disable_unprepare(sport->clk_ipg);
1626 }
1627 
1628 /* called with port.lock taken and irqs off */
1629 static void imx_uart_flush_buffer(struct uart_port *port)
1630 {
1631 	struct imx_port *sport = (struct imx_port *)port;
1632 	struct scatterlist *sgl = &sport->tx_sgl[0];
1633 
1634 	if (!sport->dma_chan_tx)
1635 		return;
1636 
1637 	sport->tx_bytes = 0;
1638 	dmaengine_terminate_all(sport->dma_chan_tx);
1639 	if (sport->dma_is_txing) {
1640 		u32 ucr1;
1641 
1642 		dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
1643 			     DMA_TO_DEVICE);
1644 		ucr1 = imx_uart_readl(sport, UCR1);
1645 		ucr1 &= ~UCR1_TXDMAEN;
1646 		imx_uart_writel(sport, ucr1, UCR1);
1647 		sport->dma_is_txing = 0;
1648 	}
1649 
1650 	imx_uart_soft_reset(sport);
1651 
1652 }
1653 
1654 static void
1655 imx_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1656 		     const struct ktermios *old)
1657 {
1658 	struct imx_port *sport = (struct imx_port *)port;
1659 	unsigned long flags;
1660 	u32 ucr2, old_ucr2, ufcr;
1661 	unsigned int baud, quot;
1662 	unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1663 	unsigned long div;
1664 	unsigned long num, denom, old_ubir, old_ubmr;
1665 	uint64_t tdiv64;
1666 
1667 	/*
1668 	 * We only support CS7 and CS8.
1669 	 */
1670 	while ((termios->c_cflag & CSIZE) != CS7 &&
1671 	       (termios->c_cflag & CSIZE) != CS8) {
1672 		termios->c_cflag &= ~CSIZE;
1673 		termios->c_cflag |= old_csize;
1674 		old_csize = CS8;
1675 	}
1676 
1677 	del_timer_sync(&sport->timer);
1678 
1679 	/*
1680 	 * Ask the core to calculate the divisor for us.
1681 	 */
1682 	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1683 	quot = uart_get_divisor(port, baud);
1684 
1685 	spin_lock_irqsave(&sport->port.lock, flags);
1686 
1687 	/*
1688 	 * Read current UCR2 and save it for future use, then clear all the bits
1689 	 * except those we will or may need to preserve.
1690 	 */
1691 	old_ucr2 = imx_uart_readl(sport, UCR2);
1692 	ucr2 = old_ucr2 & (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN | UCR2_CTS);
1693 
1694 	ucr2 |= UCR2_SRST | UCR2_IRTS;
1695 	if ((termios->c_cflag & CSIZE) == CS8)
1696 		ucr2 |= UCR2_WS;
1697 
1698 	if (!sport->have_rtscts)
1699 		termios->c_cflag &= ~CRTSCTS;
1700 
1701 	if (port->rs485.flags & SER_RS485_ENABLED) {
1702 		/*
1703 		 * RTS is mandatory for rs485 operation, so keep
1704 		 * it under manual control and keep transmitter
1705 		 * disabled.
1706 		 */
1707 		if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1708 			imx_uart_rts_active(sport, &ucr2);
1709 		else
1710 			imx_uart_rts_inactive(sport, &ucr2);
1711 
1712 	} else if (termios->c_cflag & CRTSCTS) {
1713 		/*
1714 		 * Only let receiver control RTS output if we were not requested
1715 		 * to have RTS inactive (which then should take precedence).
1716 		 */
1717 		if (ucr2 & UCR2_CTS)
1718 			ucr2 |= UCR2_CTSC;
1719 	}
1720 
1721 	if (termios->c_cflag & CRTSCTS)
1722 		ucr2 &= ~UCR2_IRTS;
1723 	if (termios->c_cflag & CSTOPB)
1724 		ucr2 |= UCR2_STPB;
1725 	if (termios->c_cflag & PARENB) {
1726 		ucr2 |= UCR2_PREN;
1727 		if (termios->c_cflag & PARODD)
1728 			ucr2 |= UCR2_PROE;
1729 	}
1730 
1731 	sport->port.read_status_mask = 0;
1732 	if (termios->c_iflag & INPCK)
1733 		sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
1734 	if (termios->c_iflag & (BRKINT | PARMRK))
1735 		sport->port.read_status_mask |= URXD_BRK;
1736 
1737 	/*
1738 	 * Characters to ignore
1739 	 */
1740 	sport->port.ignore_status_mask = 0;
1741 	if (termios->c_iflag & IGNPAR)
1742 		sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
1743 	if (termios->c_iflag & IGNBRK) {
1744 		sport->port.ignore_status_mask |= URXD_BRK;
1745 		/*
1746 		 * If we're ignoring parity and break indicators,
1747 		 * ignore overruns too (for real raw support).
1748 		 */
1749 		if (termios->c_iflag & IGNPAR)
1750 			sport->port.ignore_status_mask |= URXD_OVRRUN;
1751 	}
1752 
1753 	if ((termios->c_cflag & CREAD) == 0)
1754 		sport->port.ignore_status_mask |= URXD_DUMMY_READ;
1755 
1756 	/*
1757 	 * Update the per-port timeout.
1758 	 */
1759 	uart_update_timeout(port, termios->c_cflag, baud);
1760 
1761 	/* custom-baudrate handling */
1762 	div = sport->port.uartclk / (baud * 16);
1763 	if (baud == 38400 && quot != div)
1764 		baud = sport->port.uartclk / (quot * 16);
1765 
1766 	div = sport->port.uartclk / (baud * 16);
1767 	if (div > 7)
1768 		div = 7;
1769 	if (!div)
1770 		div = 1;
1771 
1772 	rational_best_approximation(16 * div * baud, sport->port.uartclk,
1773 		1 << 16, 1 << 16, &num, &denom);
1774 
1775 	tdiv64 = sport->port.uartclk;
1776 	tdiv64 *= num;
1777 	do_div(tdiv64, denom * 16 * div);
1778 	tty_termios_encode_baud_rate(termios,
1779 				(speed_t)tdiv64, (speed_t)tdiv64);
1780 
1781 	num -= 1;
1782 	denom -= 1;
1783 
1784 	ufcr = imx_uart_readl(sport, UFCR);
1785 	ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
1786 	imx_uart_writel(sport, ufcr, UFCR);
1787 
1788 	/*
1789 	 *  Two registers below should always be written both and in this
1790 	 *  particular order. One consequence is that we need to check if any of
1791 	 *  them changes and then update both. We do need the check for change
1792 	 *  as even writing the same values seem to "restart"
1793 	 *  transmission/receiving logic in the hardware, that leads to data
1794 	 *  breakage even when rate doesn't in fact change. E.g., user switches
1795 	 *  RTS/CTS handshake and suddenly gets broken bytes.
1796 	 */
1797 	old_ubir = imx_uart_readl(sport, UBIR);
1798 	old_ubmr = imx_uart_readl(sport, UBMR);
1799 	if (old_ubir != num || old_ubmr != denom) {
1800 		imx_uart_writel(sport, num, UBIR);
1801 		imx_uart_writel(sport, denom, UBMR);
1802 	}
1803 
1804 	if (!imx_uart_is_imx1(sport))
1805 		imx_uart_writel(sport, sport->port.uartclk / div / 1000,
1806 				IMX21_ONEMS);
1807 
1808 	imx_uart_writel(sport, ucr2, UCR2);
1809 
1810 	if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1811 		imx_uart_enable_ms(&sport->port);
1812 
1813 	spin_unlock_irqrestore(&sport->port.lock, flags);
1814 }
1815 
1816 static const char *imx_uart_type(struct uart_port *port)
1817 {
1818 	return port->type == PORT_IMX ? "IMX" : NULL;
1819 }
1820 
1821 /*
1822  * Configure/autoconfigure the port.
1823  */
1824 static void imx_uart_config_port(struct uart_port *port, int flags)
1825 {
1826 	if (flags & UART_CONFIG_TYPE)
1827 		port->type = PORT_IMX;
1828 }
1829 
1830 /*
1831  * Verify the new serial_struct (for TIOCSSERIAL).
1832  * The only change we allow are to the flags and type, and
1833  * even then only between PORT_IMX and PORT_UNKNOWN
1834  */
1835 static int
1836 imx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
1837 {
1838 	int ret = 0;
1839 
1840 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1841 		ret = -EINVAL;
1842 	if (port->irq != ser->irq)
1843 		ret = -EINVAL;
1844 	if (ser->io_type != UPIO_MEM)
1845 		ret = -EINVAL;
1846 	if (port->uartclk / 16 != ser->baud_base)
1847 		ret = -EINVAL;
1848 	if (port->mapbase != (unsigned long)ser->iomem_base)
1849 		ret = -EINVAL;
1850 	if (port->iobase != ser->port)
1851 		ret = -EINVAL;
1852 	if (ser->hub6 != 0)
1853 		ret = -EINVAL;
1854 	return ret;
1855 }
1856 
1857 #if defined(CONFIG_CONSOLE_POLL)
1858 
1859 static int imx_uart_poll_init(struct uart_port *port)
1860 {
1861 	struct imx_port *sport = (struct imx_port *)port;
1862 	unsigned long flags;
1863 	u32 ucr1, ucr2;
1864 	int retval;
1865 
1866 	retval = clk_prepare_enable(sport->clk_ipg);
1867 	if (retval)
1868 		return retval;
1869 	retval = clk_prepare_enable(sport->clk_per);
1870 	if (retval)
1871 		clk_disable_unprepare(sport->clk_ipg);
1872 
1873 	imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1874 
1875 	spin_lock_irqsave(&sport->port.lock, flags);
1876 
1877 	/*
1878 	 * Be careful about the order of enabling bits here. First enable the
1879 	 * receiver (UARTEN + RXEN) and only then the corresponding irqs.
1880 	 * This prevents that a character that already sits in the RX fifo is
1881 	 * triggering an irq but the try to fetch it from there results in an
1882 	 * exception because UARTEN or RXEN is still off.
1883 	 */
1884 	ucr1 = imx_uart_readl(sport, UCR1);
1885 	ucr2 = imx_uart_readl(sport, UCR2);
1886 
1887 	if (imx_uart_is_imx1(sport))
1888 		ucr1 |= IMX1_UCR1_UARTCLKEN;
1889 
1890 	ucr1 |= UCR1_UARTEN;
1891 	ucr1 &= ~(UCR1_TRDYEN | UCR1_RTSDEN | UCR1_RRDYEN);
1892 
1893 	ucr2 |= UCR2_RXEN | UCR2_TXEN;
1894 	ucr2 &= ~UCR2_ATEN;
1895 
1896 	imx_uart_writel(sport, ucr1, UCR1);
1897 	imx_uart_writel(sport, ucr2, UCR2);
1898 
1899 	/* now enable irqs */
1900 	imx_uart_writel(sport, ucr1 | UCR1_RRDYEN, UCR1);
1901 	imx_uart_writel(sport, ucr2 | UCR2_ATEN, UCR2);
1902 
1903 	spin_unlock_irqrestore(&sport->port.lock, flags);
1904 
1905 	return 0;
1906 }
1907 
1908 static int imx_uart_poll_get_char(struct uart_port *port)
1909 {
1910 	struct imx_port *sport = (struct imx_port *)port;
1911 	if (!(imx_uart_readl(sport, USR2) & USR2_RDR))
1912 		return NO_POLL_CHAR;
1913 
1914 	return imx_uart_readl(sport, URXD0) & URXD_RX_DATA;
1915 }
1916 
1917 static void imx_uart_poll_put_char(struct uart_port *port, unsigned char c)
1918 {
1919 	struct imx_port *sport = (struct imx_port *)port;
1920 	unsigned int status;
1921 
1922 	/* drain */
1923 	do {
1924 		status = imx_uart_readl(sport, USR1);
1925 	} while (~status & USR1_TRDY);
1926 
1927 	/* write */
1928 	imx_uart_writel(sport, c, URTX0);
1929 
1930 	/* flush */
1931 	do {
1932 		status = imx_uart_readl(sport, USR2);
1933 	} while (~status & USR2_TXDC);
1934 }
1935 #endif
1936 
1937 /* called with port.lock taken and irqs off or from .probe without locking */
1938 static int imx_uart_rs485_config(struct uart_port *port, struct ktermios *termios,
1939 				 struct serial_rs485 *rs485conf)
1940 {
1941 	struct imx_port *sport = (struct imx_port *)port;
1942 	u32 ucr2;
1943 
1944 	if (rs485conf->flags & SER_RS485_ENABLED) {
1945 		/* Enable receiver if low-active RTS signal is requested */
1946 		if (sport->have_rtscts &&  !sport->have_rtsgpio &&
1947 		    !(rs485conf->flags & SER_RS485_RTS_ON_SEND))
1948 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
1949 
1950 		/* disable transmitter */
1951 		ucr2 = imx_uart_readl(sport, UCR2);
1952 		if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
1953 			imx_uart_rts_active(sport, &ucr2);
1954 		else
1955 			imx_uart_rts_inactive(sport, &ucr2);
1956 		imx_uart_writel(sport, ucr2, UCR2);
1957 	}
1958 
1959 	/* Make sure Rx is enabled in case Tx is active with Rx disabled */
1960 	if (!(rs485conf->flags & SER_RS485_ENABLED) ||
1961 	    rs485conf->flags & SER_RS485_RX_DURING_TX)
1962 		imx_uart_start_rx(port);
1963 
1964 	return 0;
1965 }
1966 
1967 static const struct uart_ops imx_uart_pops = {
1968 	.tx_empty	= imx_uart_tx_empty,
1969 	.set_mctrl	= imx_uart_set_mctrl,
1970 	.get_mctrl	= imx_uart_get_mctrl,
1971 	.stop_tx	= imx_uart_stop_tx,
1972 	.start_tx	= imx_uart_start_tx,
1973 	.stop_rx	= imx_uart_stop_rx,
1974 	.enable_ms	= imx_uart_enable_ms,
1975 	.break_ctl	= imx_uart_break_ctl,
1976 	.startup	= imx_uart_startup,
1977 	.shutdown	= imx_uart_shutdown,
1978 	.flush_buffer	= imx_uart_flush_buffer,
1979 	.set_termios	= imx_uart_set_termios,
1980 	.type		= imx_uart_type,
1981 	.config_port	= imx_uart_config_port,
1982 	.verify_port	= imx_uart_verify_port,
1983 #if defined(CONFIG_CONSOLE_POLL)
1984 	.poll_init      = imx_uart_poll_init,
1985 	.poll_get_char  = imx_uart_poll_get_char,
1986 	.poll_put_char  = imx_uart_poll_put_char,
1987 #endif
1988 };
1989 
1990 static struct imx_port *imx_uart_ports[UART_NR];
1991 
1992 #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
1993 static void imx_uart_console_putchar(struct uart_port *port, unsigned char ch)
1994 {
1995 	struct imx_port *sport = (struct imx_port *)port;
1996 
1997 	while (imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)
1998 		barrier();
1999 
2000 	imx_uart_writel(sport, ch, URTX0);
2001 }
2002 
2003 /*
2004  * Interrupts are disabled on entering
2005  */
2006 static void
2007 imx_uart_console_write(struct console *co, const char *s, unsigned int count)
2008 {
2009 	struct imx_port *sport = imx_uart_ports[co->index];
2010 	struct imx_port_ucrs old_ucr;
2011 	unsigned long flags;
2012 	unsigned int ucr1;
2013 	int locked = 1;
2014 
2015 	if (sport->port.sysrq)
2016 		locked = 0;
2017 	else if (oops_in_progress)
2018 		locked = spin_trylock_irqsave(&sport->port.lock, flags);
2019 	else
2020 		spin_lock_irqsave(&sport->port.lock, flags);
2021 
2022 	/*
2023 	 *	First, save UCR1/2/3 and then disable interrupts
2024 	 */
2025 	imx_uart_ucrs_save(sport, &old_ucr);
2026 	ucr1 = old_ucr.ucr1;
2027 
2028 	if (imx_uart_is_imx1(sport))
2029 		ucr1 |= IMX1_UCR1_UARTCLKEN;
2030 	ucr1 |= UCR1_UARTEN;
2031 	ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN);
2032 
2033 	imx_uart_writel(sport, ucr1, UCR1);
2034 
2035 	imx_uart_writel(sport, old_ucr.ucr2 | UCR2_TXEN, UCR2);
2036 
2037 	uart_console_write(&sport->port, s, count, imx_uart_console_putchar);
2038 
2039 	/*
2040 	 *	Finally, wait for transmitter to become empty
2041 	 *	and restore UCR1/2/3
2042 	 */
2043 	while (!(imx_uart_readl(sport, USR2) & USR2_TXDC));
2044 
2045 	imx_uart_ucrs_restore(sport, &old_ucr);
2046 
2047 	if (locked)
2048 		spin_unlock_irqrestore(&sport->port.lock, flags);
2049 }
2050 
2051 /*
2052  * If the port was already initialised (eg, by a boot loader),
2053  * try to determine the current setup.
2054  */
2055 static void
2056 imx_uart_console_get_options(struct imx_port *sport, int *baud,
2057 			     int *parity, int *bits)
2058 {
2059 
2060 	if (imx_uart_readl(sport, UCR1) & UCR1_UARTEN) {
2061 		/* ok, the port was enabled */
2062 		unsigned int ucr2, ubir, ubmr, uartclk;
2063 		unsigned int baud_raw;
2064 		unsigned int ucfr_rfdiv;
2065 
2066 		ucr2 = imx_uart_readl(sport, UCR2);
2067 
2068 		*parity = 'n';
2069 		if (ucr2 & UCR2_PREN) {
2070 			if (ucr2 & UCR2_PROE)
2071 				*parity = 'o';
2072 			else
2073 				*parity = 'e';
2074 		}
2075 
2076 		if (ucr2 & UCR2_WS)
2077 			*bits = 8;
2078 		else
2079 			*bits = 7;
2080 
2081 		ubir = imx_uart_readl(sport, UBIR) & 0xffff;
2082 		ubmr = imx_uart_readl(sport, UBMR) & 0xffff;
2083 
2084 		ucfr_rfdiv = (imx_uart_readl(sport, UFCR) & UFCR_RFDIV) >> 7;
2085 		if (ucfr_rfdiv == 6)
2086 			ucfr_rfdiv = 7;
2087 		else
2088 			ucfr_rfdiv = 6 - ucfr_rfdiv;
2089 
2090 		uartclk = clk_get_rate(sport->clk_per);
2091 		uartclk /= ucfr_rfdiv;
2092 
2093 		{	/*
2094 			 * The next code provides exact computation of
2095 			 *   baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
2096 			 * without need of float support or long long division,
2097 			 * which would be required to prevent 32bit arithmetic overflow
2098 			 */
2099 			unsigned int mul = ubir + 1;
2100 			unsigned int div = 16 * (ubmr + 1);
2101 			unsigned int rem = uartclk % div;
2102 
2103 			baud_raw = (uartclk / div) * mul;
2104 			baud_raw += (rem * mul + div / 2) / div;
2105 			*baud = (baud_raw + 50) / 100 * 100;
2106 		}
2107 
2108 		if (*baud != baud_raw)
2109 			dev_info(sport->port.dev, "Console IMX rounded baud rate from %d to %d\n",
2110 				baud_raw, *baud);
2111 	}
2112 }
2113 
2114 static int
2115 imx_uart_console_setup(struct console *co, char *options)
2116 {
2117 	struct imx_port *sport;
2118 	int baud = 9600;
2119 	int bits = 8;
2120 	int parity = 'n';
2121 	int flow = 'n';
2122 	int retval;
2123 
2124 	/*
2125 	 * Check whether an invalid uart number has been specified, and
2126 	 * if so, search for the first available port that does have
2127 	 * console support.
2128 	 */
2129 	if (co->index == -1 || co->index >= ARRAY_SIZE(imx_uart_ports))
2130 		co->index = 0;
2131 	sport = imx_uart_ports[co->index];
2132 	if (sport == NULL)
2133 		return -ENODEV;
2134 
2135 	/* For setting the registers, we only need to enable the ipg clock. */
2136 	retval = clk_prepare_enable(sport->clk_ipg);
2137 	if (retval)
2138 		goto error_console;
2139 
2140 	if (options)
2141 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2142 	else
2143 		imx_uart_console_get_options(sport, &baud, &parity, &bits);
2144 
2145 	imx_uart_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
2146 
2147 	retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
2148 
2149 	if (retval) {
2150 		clk_disable_unprepare(sport->clk_ipg);
2151 		goto error_console;
2152 	}
2153 
2154 	retval = clk_prepare_enable(sport->clk_per);
2155 	if (retval)
2156 		clk_disable_unprepare(sport->clk_ipg);
2157 
2158 error_console:
2159 	return retval;
2160 }
2161 
2162 static int
2163 imx_uart_console_exit(struct console *co)
2164 {
2165 	struct imx_port *sport = imx_uart_ports[co->index];
2166 
2167 	clk_disable_unprepare(sport->clk_per);
2168 	clk_disable_unprepare(sport->clk_ipg);
2169 
2170 	return 0;
2171 }
2172 
2173 static struct uart_driver imx_uart_uart_driver;
2174 static struct console imx_uart_console = {
2175 	.name		= DEV_NAME,
2176 	.write		= imx_uart_console_write,
2177 	.device		= uart_console_device,
2178 	.setup		= imx_uart_console_setup,
2179 	.exit		= imx_uart_console_exit,
2180 	.flags		= CON_PRINTBUFFER,
2181 	.index		= -1,
2182 	.data		= &imx_uart_uart_driver,
2183 };
2184 
2185 #define IMX_CONSOLE	&imx_uart_console
2186 
2187 #else
2188 #define IMX_CONSOLE	NULL
2189 #endif
2190 
2191 static struct uart_driver imx_uart_uart_driver = {
2192 	.owner          = THIS_MODULE,
2193 	.driver_name    = DRIVER_NAME,
2194 	.dev_name       = DEV_NAME,
2195 	.major          = SERIAL_IMX_MAJOR,
2196 	.minor          = MINOR_START,
2197 	.nr             = ARRAY_SIZE(imx_uart_ports),
2198 	.cons           = IMX_CONSOLE,
2199 };
2200 
2201 static enum hrtimer_restart imx_trigger_start_tx(struct hrtimer *t)
2202 {
2203 	struct imx_port *sport = container_of(t, struct imx_port, trigger_start_tx);
2204 	unsigned long flags;
2205 
2206 	spin_lock_irqsave(&sport->port.lock, flags);
2207 	if (sport->tx_state == WAIT_AFTER_RTS)
2208 		imx_uart_start_tx(&sport->port);
2209 	spin_unlock_irqrestore(&sport->port.lock, flags);
2210 
2211 	return HRTIMER_NORESTART;
2212 }
2213 
2214 static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t)
2215 {
2216 	struct imx_port *sport = container_of(t, struct imx_port, trigger_stop_tx);
2217 	unsigned long flags;
2218 
2219 	spin_lock_irqsave(&sport->port.lock, flags);
2220 	if (sport->tx_state == WAIT_AFTER_SEND)
2221 		imx_uart_stop_tx(&sport->port);
2222 	spin_unlock_irqrestore(&sport->port.lock, flags);
2223 
2224 	return HRTIMER_NORESTART;
2225 }
2226 
2227 static const struct serial_rs485 imx_rs485_supported = {
2228 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
2229 		 SER_RS485_RX_DURING_TX,
2230 	.delay_rts_before_send = 1,
2231 	.delay_rts_after_send = 1,
2232 };
2233 
2234 /* Default RX DMA buffer configuration */
2235 #define RX_DMA_PERIODS		16
2236 #define RX_DMA_PERIOD_LEN	(PAGE_SIZE / 4)
2237 
2238 static int imx_uart_probe(struct platform_device *pdev)
2239 {
2240 	struct device_node *np = pdev->dev.of_node;
2241 	struct imx_port *sport;
2242 	void __iomem *base;
2243 	u32 dma_buf_conf[2];
2244 	int ret = 0;
2245 	u32 ucr1, ucr2, uts;
2246 	struct resource *res;
2247 	int txirq, rxirq, rtsirq;
2248 
2249 	sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2250 	if (!sport)
2251 		return -ENOMEM;
2252 
2253 	sport->devdata = of_device_get_match_data(&pdev->dev);
2254 
2255 	ret = of_alias_get_id(np, "serial");
2256 	if (ret < 0) {
2257 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2258 		return ret;
2259 	}
2260 	sport->port.line = ret;
2261 
2262 	sport->have_rtscts = of_property_read_bool(np, "uart-has-rtscts") ||
2263 		of_property_read_bool(np, "fsl,uart-has-rtscts"); /* deprecated */
2264 
2265 	sport->dte_mode = of_property_read_bool(np, "fsl,dte-mode");
2266 
2267 	sport->have_rtsgpio = of_property_present(np, "rts-gpios");
2268 
2269 	sport->inverted_tx = of_property_read_bool(np, "fsl,inverted-tx");
2270 
2271 	sport->inverted_rx = of_property_read_bool(np, "fsl,inverted-rx");
2272 
2273 	if (!of_property_read_u32_array(np, "fsl,dma-info", dma_buf_conf, 2)) {
2274 		sport->rx_period_length = dma_buf_conf[0];
2275 		sport->rx_periods = dma_buf_conf[1];
2276 	} else {
2277 		sport->rx_period_length = RX_DMA_PERIOD_LEN;
2278 		sport->rx_periods = RX_DMA_PERIODS;
2279 	}
2280 
2281 	if (sport->port.line >= ARRAY_SIZE(imx_uart_ports)) {
2282 		dev_err(&pdev->dev, "serial%d out of range\n",
2283 			sport->port.line);
2284 		return -EINVAL;
2285 	}
2286 
2287 	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2288 	if (IS_ERR(base))
2289 		return PTR_ERR(base);
2290 
2291 	rxirq = platform_get_irq(pdev, 0);
2292 	if (rxirq < 0)
2293 		return rxirq;
2294 	txirq = platform_get_irq_optional(pdev, 1);
2295 	rtsirq = platform_get_irq_optional(pdev, 2);
2296 
2297 	sport->port.dev = &pdev->dev;
2298 	sport->port.mapbase = res->start;
2299 	sport->port.membase = base;
2300 	sport->port.type = PORT_IMX;
2301 	sport->port.iotype = UPIO_MEM;
2302 	sport->port.irq = rxirq;
2303 	sport->port.fifosize = 32;
2304 	sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE);
2305 	sport->port.ops = &imx_uart_pops;
2306 	sport->port.rs485_config = imx_uart_rs485_config;
2307 	/* RTS is required to control the RS485 transmitter */
2308 	if (sport->have_rtscts || sport->have_rtsgpio)
2309 		sport->port.rs485_supported = imx_rs485_supported;
2310 	sport->port.flags = UPF_BOOT_AUTOCONF;
2311 	timer_setup(&sport->timer, imx_uart_timeout, 0);
2312 
2313 	sport->gpios = mctrl_gpio_init(&sport->port, 0);
2314 	if (IS_ERR(sport->gpios))
2315 		return PTR_ERR(sport->gpios);
2316 
2317 	sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2318 	if (IS_ERR(sport->clk_ipg)) {
2319 		ret = PTR_ERR(sport->clk_ipg);
2320 		dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
2321 		return ret;
2322 	}
2323 
2324 	sport->clk_per = devm_clk_get(&pdev->dev, "per");
2325 	if (IS_ERR(sport->clk_per)) {
2326 		ret = PTR_ERR(sport->clk_per);
2327 		dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
2328 		return ret;
2329 	}
2330 
2331 	sport->port.uartclk = clk_get_rate(sport->clk_per);
2332 
2333 	/* For register access, we only need to enable the ipg clock. */
2334 	ret = clk_prepare_enable(sport->clk_ipg);
2335 	if (ret) {
2336 		dev_err(&pdev->dev, "failed to enable ipg clk: %d\n", ret);
2337 		return ret;
2338 	}
2339 
2340 	ret = uart_get_rs485_mode(&sport->port);
2341 	if (ret)
2342 		goto err_clk;
2343 
2344 	/*
2345 	 * If using the i.MX UART RTS/CTS control then the RTS (CTS_B)
2346 	 * signal cannot be set low during transmission in case the
2347 	 * receiver is off (limitation of the i.MX UART IP).
2348 	 */
2349 	if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2350 	    sport->have_rtscts && !sport->have_rtsgpio &&
2351 	    (!(sport->port.rs485.flags & SER_RS485_RTS_ON_SEND) &&
2352 	     !(sport->port.rs485.flags & SER_RS485_RX_DURING_TX)))
2353 		dev_err(&pdev->dev,
2354 			"low-active RTS not possible when receiver is off, enabling receiver\n");
2355 
2356 	/* Disable interrupts before requesting them */
2357 	ucr1 = imx_uart_readl(sport, UCR1);
2358 	ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN);
2359 	imx_uart_writel(sport, ucr1, UCR1);
2360 
2361 	/* Disable Ageing Timer interrupt */
2362 	ucr2 = imx_uart_readl(sport, UCR2);
2363 	ucr2 &= ~UCR2_ATEN;
2364 	imx_uart_writel(sport, ucr2, UCR2);
2365 
2366 	/*
2367 	 * In case RS485 is enabled without GPIO RTS control, the UART IP
2368 	 * is used to control CTS signal. Keep both the UART and Receiver
2369 	 * enabled, otherwise the UART IP pulls CTS signal always HIGH no
2370 	 * matter how the UCR2 CTSC and CTS bits are set. To prevent any
2371 	 * data from being fed into the RX FIFO, enable loopback mode in
2372 	 * UTS register, which disconnects the RX path from external RXD
2373 	 * pin and connects it to the Transceiver, which is disabled, so
2374 	 * no data can be fed to the RX FIFO that way.
2375 	 */
2376 	if (sport->port.rs485.flags & SER_RS485_ENABLED &&
2377 	    sport->have_rtscts && !sport->have_rtsgpio) {
2378 		uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
2379 		uts |= UTS_LOOP;
2380 		imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
2381 
2382 		ucr1 = imx_uart_readl(sport, UCR1);
2383 		ucr1 |= UCR1_UARTEN;
2384 		imx_uart_writel(sport, ucr1, UCR1);
2385 
2386 		ucr2 = imx_uart_readl(sport, UCR2);
2387 		ucr2 |= UCR2_RXEN;
2388 		imx_uart_writel(sport, ucr2, UCR2);
2389 	}
2390 
2391 	if (!imx_uart_is_imx1(sport) && sport->dte_mode) {
2392 		/*
2393 		 * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI
2394 		 * and influences if UCR3_RI and UCR3_DCD changes the level of RI
2395 		 * and DCD (when they are outputs) or enables the respective
2396 		 * irqs. So set this bit early, i.e. before requesting irqs.
2397 		 */
2398 		u32 ufcr = imx_uart_readl(sport, UFCR);
2399 		if (!(ufcr & UFCR_DCEDTE))
2400 			imx_uart_writel(sport, ufcr | UFCR_DCEDTE, UFCR);
2401 
2402 		/*
2403 		 * Disable UCR3_RI and UCR3_DCD irqs. They are also not
2404 		 * enabled later because they cannot be cleared
2405 		 * (confirmed on i.MX25) which makes them unusable.
2406 		 */
2407 		imx_uart_writel(sport,
2408 				IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR,
2409 				UCR3);
2410 
2411 	} else {
2412 		u32 ucr3 = UCR3_DSR;
2413 		u32 ufcr = imx_uart_readl(sport, UFCR);
2414 		if (ufcr & UFCR_DCEDTE)
2415 			imx_uart_writel(sport, ufcr & ~UFCR_DCEDTE, UFCR);
2416 
2417 		if (!imx_uart_is_imx1(sport))
2418 			ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
2419 		imx_uart_writel(sport, ucr3, UCR3);
2420 	}
2421 
2422 	hrtimer_init(&sport->trigger_start_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2423 	hrtimer_init(&sport->trigger_stop_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2424 	sport->trigger_start_tx.function = imx_trigger_start_tx;
2425 	sport->trigger_stop_tx.function = imx_trigger_stop_tx;
2426 
2427 	/*
2428 	 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
2429 	 * chips only have one interrupt.
2430 	 */
2431 	if (txirq > 0) {
2432 		ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_rxint, 0,
2433 				       dev_name(&pdev->dev), sport);
2434 		if (ret) {
2435 			dev_err(&pdev->dev, "failed to request rx irq: %d\n",
2436 				ret);
2437 			goto err_clk;
2438 		}
2439 
2440 		ret = devm_request_irq(&pdev->dev, txirq, imx_uart_txint, 0,
2441 				       dev_name(&pdev->dev), sport);
2442 		if (ret) {
2443 			dev_err(&pdev->dev, "failed to request tx irq: %d\n",
2444 				ret);
2445 			goto err_clk;
2446 		}
2447 
2448 		ret = devm_request_irq(&pdev->dev, rtsirq, imx_uart_rtsint, 0,
2449 				       dev_name(&pdev->dev), sport);
2450 		if (ret) {
2451 			dev_err(&pdev->dev, "failed to request rts irq: %d\n",
2452 				ret);
2453 			goto err_clk;
2454 		}
2455 	} else {
2456 		ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_int, 0,
2457 				       dev_name(&pdev->dev), sport);
2458 		if (ret) {
2459 			dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2460 			goto err_clk;
2461 		}
2462 	}
2463 
2464 	imx_uart_ports[sport->port.line] = sport;
2465 
2466 	platform_set_drvdata(pdev, sport);
2467 
2468 	ret = uart_add_one_port(&imx_uart_uart_driver, &sport->port);
2469 
2470 err_clk:
2471 	clk_disable_unprepare(sport->clk_ipg);
2472 
2473 	return ret;
2474 }
2475 
2476 static int imx_uart_remove(struct platform_device *pdev)
2477 {
2478 	struct imx_port *sport = platform_get_drvdata(pdev);
2479 
2480 	uart_remove_one_port(&imx_uart_uart_driver, &sport->port);
2481 
2482 	return 0;
2483 }
2484 
2485 static void imx_uart_restore_context(struct imx_port *sport)
2486 {
2487 	unsigned long flags;
2488 
2489 	spin_lock_irqsave(&sport->port.lock, flags);
2490 	if (!sport->context_saved) {
2491 		spin_unlock_irqrestore(&sport->port.lock, flags);
2492 		return;
2493 	}
2494 
2495 	imx_uart_writel(sport, sport->saved_reg[4], UFCR);
2496 	imx_uart_writel(sport, sport->saved_reg[5], UESC);
2497 	imx_uart_writel(sport, sport->saved_reg[6], UTIM);
2498 	imx_uart_writel(sport, sport->saved_reg[7], UBIR);
2499 	imx_uart_writel(sport, sport->saved_reg[8], UBMR);
2500 	imx_uart_writel(sport, sport->saved_reg[9], IMX21_UTS);
2501 	imx_uart_writel(sport, sport->saved_reg[0], UCR1);
2502 	imx_uart_writel(sport, sport->saved_reg[1] | UCR2_SRST, UCR2);
2503 	imx_uart_writel(sport, sport->saved_reg[2], UCR3);
2504 	imx_uart_writel(sport, sport->saved_reg[3], UCR4);
2505 	sport->context_saved = false;
2506 	spin_unlock_irqrestore(&sport->port.lock, flags);
2507 }
2508 
2509 static void imx_uart_save_context(struct imx_port *sport)
2510 {
2511 	unsigned long flags;
2512 
2513 	/* Save necessary regs */
2514 	spin_lock_irqsave(&sport->port.lock, flags);
2515 	sport->saved_reg[0] = imx_uart_readl(sport, UCR1);
2516 	sport->saved_reg[1] = imx_uart_readl(sport, UCR2);
2517 	sport->saved_reg[2] = imx_uart_readl(sport, UCR3);
2518 	sport->saved_reg[3] = imx_uart_readl(sport, UCR4);
2519 	sport->saved_reg[4] = imx_uart_readl(sport, UFCR);
2520 	sport->saved_reg[5] = imx_uart_readl(sport, UESC);
2521 	sport->saved_reg[6] = imx_uart_readl(sport, UTIM);
2522 	sport->saved_reg[7] = imx_uart_readl(sport, UBIR);
2523 	sport->saved_reg[8] = imx_uart_readl(sport, UBMR);
2524 	sport->saved_reg[9] = imx_uart_readl(sport, IMX21_UTS);
2525 	sport->context_saved = true;
2526 	spin_unlock_irqrestore(&sport->port.lock, flags);
2527 }
2528 
2529 static void imx_uart_enable_wakeup(struct imx_port *sport, bool on)
2530 {
2531 	u32 ucr3;
2532 
2533 	ucr3 = imx_uart_readl(sport, UCR3);
2534 	if (on) {
2535 		imx_uart_writel(sport, USR1_AWAKE, USR1);
2536 		ucr3 |= UCR3_AWAKEN;
2537 	} else {
2538 		ucr3 &= ~UCR3_AWAKEN;
2539 	}
2540 	imx_uart_writel(sport, ucr3, UCR3);
2541 
2542 	if (sport->have_rtscts) {
2543 		u32 ucr1 = imx_uart_readl(sport, UCR1);
2544 		if (on) {
2545 			imx_uart_writel(sport, USR1_RTSD, USR1);
2546 			ucr1 |= UCR1_RTSDEN;
2547 		} else {
2548 			ucr1 &= ~UCR1_RTSDEN;
2549 		}
2550 		imx_uart_writel(sport, ucr1, UCR1);
2551 	}
2552 }
2553 
2554 static int imx_uart_suspend_noirq(struct device *dev)
2555 {
2556 	struct imx_port *sport = dev_get_drvdata(dev);
2557 
2558 	imx_uart_save_context(sport);
2559 
2560 	clk_disable(sport->clk_ipg);
2561 
2562 	pinctrl_pm_select_sleep_state(dev);
2563 
2564 	return 0;
2565 }
2566 
2567 static int imx_uart_resume_noirq(struct device *dev)
2568 {
2569 	struct imx_port *sport = dev_get_drvdata(dev);
2570 	int ret;
2571 
2572 	pinctrl_pm_select_default_state(dev);
2573 
2574 	ret = clk_enable(sport->clk_ipg);
2575 	if (ret)
2576 		return ret;
2577 
2578 	imx_uart_restore_context(sport);
2579 
2580 	return 0;
2581 }
2582 
2583 static int imx_uart_suspend(struct device *dev)
2584 {
2585 	struct imx_port *sport = dev_get_drvdata(dev);
2586 	int ret;
2587 
2588 	uart_suspend_port(&imx_uart_uart_driver, &sport->port);
2589 	disable_irq(sport->port.irq);
2590 
2591 	ret = clk_prepare_enable(sport->clk_ipg);
2592 	if (ret)
2593 		return ret;
2594 
2595 	/* enable wakeup from i.MX UART */
2596 	imx_uart_enable_wakeup(sport, true);
2597 
2598 	return 0;
2599 }
2600 
2601 static int imx_uart_resume(struct device *dev)
2602 {
2603 	struct imx_port *sport = dev_get_drvdata(dev);
2604 
2605 	/* disable wakeup from i.MX UART */
2606 	imx_uart_enable_wakeup(sport, false);
2607 
2608 	uart_resume_port(&imx_uart_uart_driver, &sport->port);
2609 	enable_irq(sport->port.irq);
2610 
2611 	clk_disable_unprepare(sport->clk_ipg);
2612 
2613 	return 0;
2614 }
2615 
2616 static int imx_uart_freeze(struct device *dev)
2617 {
2618 	struct imx_port *sport = dev_get_drvdata(dev);
2619 
2620 	uart_suspend_port(&imx_uart_uart_driver, &sport->port);
2621 
2622 	return clk_prepare_enable(sport->clk_ipg);
2623 }
2624 
2625 static int imx_uart_thaw(struct device *dev)
2626 {
2627 	struct imx_port *sport = dev_get_drvdata(dev);
2628 
2629 	uart_resume_port(&imx_uart_uart_driver, &sport->port);
2630 
2631 	clk_disable_unprepare(sport->clk_ipg);
2632 
2633 	return 0;
2634 }
2635 
2636 static const struct dev_pm_ops imx_uart_pm_ops = {
2637 	.suspend_noirq = imx_uart_suspend_noirq,
2638 	.resume_noirq = imx_uart_resume_noirq,
2639 	.freeze_noirq = imx_uart_suspend_noirq,
2640 	.thaw_noirq = imx_uart_resume_noirq,
2641 	.restore_noirq = imx_uart_resume_noirq,
2642 	.suspend = imx_uart_suspend,
2643 	.resume = imx_uart_resume,
2644 	.freeze = imx_uart_freeze,
2645 	.thaw = imx_uart_thaw,
2646 	.restore = imx_uart_thaw,
2647 };
2648 
2649 static struct platform_driver imx_uart_platform_driver = {
2650 	.probe = imx_uart_probe,
2651 	.remove = imx_uart_remove,
2652 
2653 	.driver = {
2654 		.name = "imx-uart",
2655 		.of_match_table = imx_uart_dt_ids,
2656 		.pm = &imx_uart_pm_ops,
2657 	},
2658 };
2659 
2660 static int __init imx_uart_init(void)
2661 {
2662 	int ret = uart_register_driver(&imx_uart_uart_driver);
2663 
2664 	if (ret)
2665 		return ret;
2666 
2667 	ret = platform_driver_register(&imx_uart_platform_driver);
2668 	if (ret != 0)
2669 		uart_unregister_driver(&imx_uart_uart_driver);
2670 
2671 	return ret;
2672 }
2673 
2674 static void __exit imx_uart_exit(void)
2675 {
2676 	platform_driver_unregister(&imx_uart_platform_driver);
2677 	uart_unregister_driver(&imx_uart_uart_driver);
2678 }
2679 
2680 module_init(imx_uart_init);
2681 module_exit(imx_uart_exit);
2682 
2683 MODULE_AUTHOR("Sascha Hauer");
2684 MODULE_DESCRIPTION("IMX generic serial port driver");
2685 MODULE_LICENSE("GPL");
2686 MODULE_ALIAS("platform:imx-uart");
2687