xref: /openbmc/linux/drivers/tty/serial/fsl_lpuart.c (revision 9cb31a2824f9399930ef0c9c449cec258bac66e4)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Freescale lpuart serial port driver
4  *
5  *  Copyright 2012-2014 Freescale Semiconductor, Inc.
6  */
7 
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmaengine.h>
15 #include <linux/dmapool.h>
16 #include <linux/io.h>
17 #include <linux/iopoll.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_dma.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/serial_core.h>
26 #include <linux/slab.h>
27 #include <linux/tty_flip.h>
28 
29 /* All registers are 8-bit width */
30 #define UARTBDH			0x00
31 #define UARTBDL			0x01
32 #define UARTCR1			0x02
33 #define UARTCR2			0x03
34 #define UARTSR1			0x04
35 #define UARTCR3			0x06
36 #define UARTDR			0x07
37 #define UARTCR4			0x0a
38 #define UARTCR5			0x0b
39 #define UARTMODEM		0x0d
40 #define UARTPFIFO		0x10
41 #define UARTCFIFO		0x11
42 #define UARTSFIFO		0x12
43 #define UARTTWFIFO		0x13
44 #define UARTTCFIFO		0x14
45 #define UARTRWFIFO		0x15
46 
47 #define UARTBDH_LBKDIE		0x80
48 #define UARTBDH_RXEDGIE		0x40
49 #define UARTBDH_SBR_MASK	0x1f
50 
51 #define UARTCR1_LOOPS		0x80
52 #define UARTCR1_RSRC		0x20
53 #define UARTCR1_M		0x10
54 #define UARTCR1_WAKE		0x08
55 #define UARTCR1_ILT		0x04
56 #define UARTCR1_PE		0x02
57 #define UARTCR1_PT		0x01
58 
59 #define UARTCR2_TIE		0x80
60 #define UARTCR2_TCIE		0x40
61 #define UARTCR2_RIE		0x20
62 #define UARTCR2_ILIE		0x10
63 #define UARTCR2_TE		0x08
64 #define UARTCR2_RE		0x04
65 #define UARTCR2_RWU		0x02
66 #define UARTCR2_SBK		0x01
67 
68 #define UARTSR1_TDRE		0x80
69 #define UARTSR1_TC		0x40
70 #define UARTSR1_RDRF		0x20
71 #define UARTSR1_IDLE		0x10
72 #define UARTSR1_OR		0x08
73 #define UARTSR1_NF		0x04
74 #define UARTSR1_FE		0x02
75 #define UARTSR1_PE		0x01
76 
77 #define UARTCR3_R8		0x80
78 #define UARTCR3_T8		0x40
79 #define UARTCR3_TXDIR		0x20
80 #define UARTCR3_TXINV		0x10
81 #define UARTCR3_ORIE		0x08
82 #define UARTCR3_NEIE		0x04
83 #define UARTCR3_FEIE		0x02
84 #define UARTCR3_PEIE		0x01
85 
86 #define UARTCR4_MAEN1		0x80
87 #define UARTCR4_MAEN2		0x40
88 #define UARTCR4_M10		0x20
89 #define UARTCR4_BRFA_MASK	0x1f
90 #define UARTCR4_BRFA_OFF	0
91 
92 #define UARTCR5_TDMAS		0x80
93 #define UARTCR5_RDMAS		0x20
94 
95 #define UARTMODEM_RXRTSE	0x08
96 #define UARTMODEM_TXRTSPOL	0x04
97 #define UARTMODEM_TXRTSE	0x02
98 #define UARTMODEM_TXCTSE	0x01
99 
100 #define UARTPFIFO_TXFE		0x80
101 #define UARTPFIFO_FIFOSIZE_MASK	0x7
102 #define UARTPFIFO_TXSIZE_OFF	4
103 #define UARTPFIFO_RXFE		0x08
104 #define UARTPFIFO_RXSIZE_OFF	0
105 
106 #define UARTCFIFO_TXFLUSH	0x80
107 #define UARTCFIFO_RXFLUSH	0x40
108 #define UARTCFIFO_RXOFE		0x04
109 #define UARTCFIFO_TXOFE		0x02
110 #define UARTCFIFO_RXUFE		0x01
111 
112 #define UARTSFIFO_TXEMPT	0x80
113 #define UARTSFIFO_RXEMPT	0x40
114 #define UARTSFIFO_RXOF		0x04
115 #define UARTSFIFO_TXOF		0x02
116 #define UARTSFIFO_RXUF		0x01
117 
118 /* 32-bit global registers only for i.MX7ULP/i.MX8x
119  * Used to reset all internal logic and registers, except the Global Register.
120  */
121 #define UART_GLOBAL		0x8
122 
123 /* 32-bit register definition */
124 #define UARTBAUD		0x00
125 #define UARTSTAT		0x04
126 #define UARTCTRL		0x08
127 #define UARTDATA		0x0C
128 #define UARTMATCH		0x10
129 #define UARTMODIR		0x14
130 #define UARTFIFO		0x18
131 #define UARTWATER		0x1c
132 
133 #define UARTBAUD_MAEN1		0x80000000
134 #define UARTBAUD_MAEN2		0x40000000
135 #define UARTBAUD_M10		0x20000000
136 #define UARTBAUD_TDMAE		0x00800000
137 #define UARTBAUD_RDMAE		0x00200000
138 #define UARTBAUD_MATCFG		0x00400000
139 #define UARTBAUD_BOTHEDGE	0x00020000
140 #define UARTBAUD_RESYNCDIS	0x00010000
141 #define UARTBAUD_LBKDIE		0x00008000
142 #define UARTBAUD_RXEDGIE	0x00004000
143 #define UARTBAUD_SBNS		0x00002000
144 #define UARTBAUD_SBR		0x00000000
145 #define UARTBAUD_SBR_MASK	0x1fff
146 #define UARTBAUD_OSR_MASK       0x1f
147 #define UARTBAUD_OSR_SHIFT      24
148 
149 #define UARTSTAT_LBKDIF		0x80000000
150 #define UARTSTAT_RXEDGIF	0x40000000
151 #define UARTSTAT_MSBF		0x20000000
152 #define UARTSTAT_RXINV		0x10000000
153 #define UARTSTAT_RWUID		0x08000000
154 #define UARTSTAT_BRK13		0x04000000
155 #define UARTSTAT_LBKDE		0x02000000
156 #define UARTSTAT_RAF		0x01000000
157 #define UARTSTAT_TDRE		0x00800000
158 #define UARTSTAT_TC		0x00400000
159 #define UARTSTAT_RDRF		0x00200000
160 #define UARTSTAT_IDLE		0x00100000
161 #define UARTSTAT_OR		0x00080000
162 #define UARTSTAT_NF		0x00040000
163 #define UARTSTAT_FE		0x00020000
164 #define UARTSTAT_PE		0x00010000
165 #define UARTSTAT_MA1F		0x00008000
166 #define UARTSTAT_M21F		0x00004000
167 
168 #define UARTCTRL_R8T9		0x80000000
169 #define UARTCTRL_R9T8		0x40000000
170 #define UARTCTRL_TXDIR		0x20000000
171 #define UARTCTRL_TXINV		0x10000000
172 #define UARTCTRL_ORIE		0x08000000
173 #define UARTCTRL_NEIE		0x04000000
174 #define UARTCTRL_FEIE		0x02000000
175 #define UARTCTRL_PEIE		0x01000000
176 #define UARTCTRL_TIE		0x00800000
177 #define UARTCTRL_TCIE		0x00400000
178 #define UARTCTRL_RIE		0x00200000
179 #define UARTCTRL_ILIE		0x00100000
180 #define UARTCTRL_TE		0x00080000
181 #define UARTCTRL_RE		0x00040000
182 #define UARTCTRL_RWU		0x00020000
183 #define UARTCTRL_SBK		0x00010000
184 #define UARTCTRL_MA1IE		0x00008000
185 #define UARTCTRL_MA2IE		0x00004000
186 #define UARTCTRL_IDLECFG	GENMASK(10, 8)
187 #define UARTCTRL_LOOPS		0x00000080
188 #define UARTCTRL_DOZEEN		0x00000040
189 #define UARTCTRL_RSRC		0x00000020
190 #define UARTCTRL_M		0x00000010
191 #define UARTCTRL_WAKE		0x00000008
192 #define UARTCTRL_ILT		0x00000004
193 #define UARTCTRL_PE		0x00000002
194 #define UARTCTRL_PT		0x00000001
195 
196 #define UARTDATA_NOISY		0x00008000
197 #define UARTDATA_PARITYE	0x00004000
198 #define UARTDATA_FRETSC		0x00002000
199 #define UARTDATA_RXEMPT		0x00001000
200 #define UARTDATA_IDLINE		0x00000800
201 #define UARTDATA_MASK		0x3ff
202 
203 #define UARTMODIR_IREN		0x00020000
204 #define UARTMODIR_RTSWATER	GENMASK(10, 8)
205 #define UARTMODIR_TXCTSSRC	0x00000020
206 #define UARTMODIR_TXCTSC	0x00000010
207 #define UARTMODIR_RXRTSE	0x00000008
208 #define UARTMODIR_TXRTSPOL	0x00000004
209 #define UARTMODIR_TXRTSE	0x00000002
210 #define UARTMODIR_TXCTSE	0x00000001
211 
212 #define UARTFIFO_TXEMPT		0x00800000
213 #define UARTFIFO_RXEMPT		0x00400000
214 #define UARTFIFO_TXOF		0x00020000
215 #define UARTFIFO_RXUF		0x00010000
216 #define UARTFIFO_TXFLUSH	0x00008000
217 #define UARTFIFO_RXFLUSH	0x00004000
218 #define UARTFIFO_RXIDEN	GENMASK(12, 10)
219 #define UARTFIFO_TXOFE		0x00000200
220 #define UARTFIFO_RXUFE		0x00000100
221 #define UARTFIFO_TXFE		0x00000080
222 #define UARTFIFO_FIFOSIZE_MASK	0x7
223 #define UARTFIFO_TXSIZE_OFF	4
224 #define UARTFIFO_RXFE		0x00000008
225 #define UARTFIFO_RXSIZE_OFF	0
226 #define UARTFIFO_DEPTH(x)	(0x1 << ((x) ? ((x) + 1) : 0))
227 
228 #define UARTWATER_COUNT_MASK	0xff
229 #define UARTWATER_TXCNT_OFF	8
230 #define UARTWATER_RXCNT_OFF	24
231 #define UARTWATER_WATER_MASK	0xff
232 #define UARTWATER_TXWATER_OFF	0
233 #define UARTWATER_RXWATER_OFF	16
234 
235 #define UART_GLOBAL_RST	0x2
236 #define GLOBAL_RST_MIN_US	20
237 #define GLOBAL_RST_MAX_US	40
238 
239 /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
240 #define DMA_RX_TIMEOUT		(10)
241 #define DMA_RX_IDLE_CHARS	8
242 #define UART_AUTOSUSPEND_TIMEOUT	3000
243 
244 #define DRIVER_NAME	"fsl-lpuart"
245 #define DEV_NAME	"ttyLP"
246 #define UART_NR		8
247 
248 /* IMX lpuart has four extra unused regs located at the beginning */
249 #define IMX_REG_OFF	0x10
250 
251 enum lpuart_type {
252 	VF610_LPUART,
253 	LS1021A_LPUART,
254 	LS1028A_LPUART,
255 	IMX7ULP_LPUART,
256 	IMX8ULP_LPUART,
257 	IMX8QXP_LPUART,
258 	IMXRT1050_LPUART,
259 };
260 
261 struct lpuart_port {
262 	struct uart_port	port;
263 	enum lpuart_type	devtype;
264 	struct clk		*ipg_clk;
265 	struct clk		*baud_clk;
266 	unsigned int		txfifo_size;
267 	unsigned int		rxfifo_size;
268 
269 	u8			rx_watermark;
270 	bool			lpuart_dma_tx_use;
271 	bool			lpuart_dma_rx_use;
272 	struct dma_chan		*dma_tx_chan;
273 	struct dma_chan		*dma_rx_chan;
274 	struct dma_async_tx_descriptor  *dma_tx_desc;
275 	struct dma_async_tx_descriptor  *dma_rx_desc;
276 	dma_cookie_t		dma_tx_cookie;
277 	dma_cookie_t		dma_rx_cookie;
278 	unsigned int		dma_tx_bytes;
279 	unsigned int		dma_rx_bytes;
280 	bool			dma_tx_in_progress;
281 	unsigned int		dma_rx_timeout;
282 	struct timer_list	lpuart_timer;
283 	struct scatterlist	rx_sgl, tx_sgl[2];
284 	struct circ_buf		rx_ring;
285 	int			rx_dma_rng_buf_len;
286 	int                     last_residue;
287 	unsigned int		dma_tx_nents;
288 	wait_queue_head_t	dma_wait;
289 	bool			is_cs7; /* Set to true when character size is 7 */
290 					/* and the parity is enabled		*/
291 };
292 
293 struct lpuart_soc_data {
294 	enum lpuart_type devtype;
295 	char iotype;
296 	u8 reg_off;
297 	u8 rx_watermark;
298 };
299 
300 static const struct lpuart_soc_data vf_data = {
301 	.devtype = VF610_LPUART,
302 	.iotype = UPIO_MEM,
303 	.rx_watermark = 1,
304 };
305 
306 static const struct lpuart_soc_data ls1021a_data = {
307 	.devtype = LS1021A_LPUART,
308 	.iotype = UPIO_MEM32BE,
309 	.rx_watermark = 1,
310 };
311 
312 static const struct lpuart_soc_data ls1028a_data = {
313 	.devtype = LS1028A_LPUART,
314 	.iotype = UPIO_MEM32,
315 	.rx_watermark = 0,
316 };
317 
318 static struct lpuart_soc_data imx7ulp_data = {
319 	.devtype = IMX7ULP_LPUART,
320 	.iotype = UPIO_MEM32,
321 	.reg_off = IMX_REG_OFF,
322 	.rx_watermark = 1,
323 };
324 
325 static struct lpuart_soc_data imx8ulp_data = {
326 	.devtype = IMX8ULP_LPUART,
327 	.iotype = UPIO_MEM32,
328 	.reg_off = IMX_REG_OFF,
329 	.rx_watermark = 3,
330 };
331 
332 static struct lpuart_soc_data imx8qxp_data = {
333 	.devtype = IMX8QXP_LPUART,
334 	.iotype = UPIO_MEM32,
335 	.reg_off = IMX_REG_OFF,
336 	.rx_watermark = 7, /* A lower watermark is ideal for low baud rates. */
337 };
338 static struct lpuart_soc_data imxrt1050_data = {
339 	.devtype = IMXRT1050_LPUART,
340 	.iotype = UPIO_MEM32,
341 	.reg_off = IMX_REG_OFF,
342 	.rx_watermark = 1,
343 };
344 
345 static const struct of_device_id lpuart_dt_ids[] = {
346 	{ .compatible = "fsl,vf610-lpuart",	.data = &vf_data, },
347 	{ .compatible = "fsl,ls1021a-lpuart",	.data = &ls1021a_data, },
348 	{ .compatible = "fsl,ls1028a-lpuart",	.data = &ls1028a_data, },
349 	{ .compatible = "fsl,imx7ulp-lpuart",	.data = &imx7ulp_data, },
350 	{ .compatible = "fsl,imx8ulp-lpuart",	.data = &imx8ulp_data, },
351 	{ .compatible = "fsl,imx8qxp-lpuart",	.data = &imx8qxp_data, },
352 	{ .compatible = "fsl,imxrt1050-lpuart",	.data = &imxrt1050_data},
353 	{ /* sentinel */ }
354 };
355 MODULE_DEVICE_TABLE(of, lpuart_dt_ids);
356 
357 /* Forward declare this for the dma callbacks*/
358 static void lpuart_dma_tx_complete(void *arg);
359 
360 static inline bool is_layerscape_lpuart(struct lpuart_port *sport)
361 {
362 	return (sport->devtype == LS1021A_LPUART ||
363 		sport->devtype == LS1028A_LPUART);
364 }
365 
366 static inline bool is_imx7ulp_lpuart(struct lpuart_port *sport)
367 {
368 	return sport->devtype == IMX7ULP_LPUART;
369 }
370 
371 static inline bool is_imx8ulp_lpuart(struct lpuart_port *sport)
372 {
373 	return sport->devtype == IMX8ULP_LPUART;
374 }
375 
376 static inline bool is_imx8qxp_lpuart(struct lpuart_port *sport)
377 {
378 	return sport->devtype == IMX8QXP_LPUART;
379 }
380 
381 static inline u32 lpuart32_read(struct uart_port *port, u32 off)
382 {
383 	switch (port->iotype) {
384 	case UPIO_MEM32:
385 		return readl(port->membase + off);
386 	case UPIO_MEM32BE:
387 		return ioread32be(port->membase + off);
388 	default:
389 		return 0;
390 	}
391 }
392 
393 static inline void lpuart32_write(struct uart_port *port, u32 val,
394 				  u32 off)
395 {
396 	switch (port->iotype) {
397 	case UPIO_MEM32:
398 		writel(val, port->membase + off);
399 		break;
400 	case UPIO_MEM32BE:
401 		iowrite32be(val, port->membase + off);
402 		break;
403 	}
404 }
405 
406 static int __lpuart_enable_clks(struct lpuart_port *sport, bool is_en)
407 {
408 	int ret = 0;
409 
410 	if (is_en) {
411 		ret = clk_prepare_enable(sport->ipg_clk);
412 		if (ret)
413 			return ret;
414 
415 		ret = clk_prepare_enable(sport->baud_clk);
416 		if (ret) {
417 			clk_disable_unprepare(sport->ipg_clk);
418 			return ret;
419 		}
420 	} else {
421 		clk_disable_unprepare(sport->baud_clk);
422 		clk_disable_unprepare(sport->ipg_clk);
423 	}
424 
425 	return 0;
426 }
427 
428 static unsigned int lpuart_get_baud_clk_rate(struct lpuart_port *sport)
429 {
430 	if (is_imx8qxp_lpuart(sport))
431 		return clk_get_rate(sport->baud_clk);
432 
433 	return clk_get_rate(sport->ipg_clk);
434 }
435 
436 #define lpuart_enable_clks(x)	__lpuart_enable_clks(x, true)
437 #define lpuart_disable_clks(x)	__lpuart_enable_clks(x, false)
438 
439 static void lpuart_stop_tx(struct uart_port *port)
440 {
441 	unsigned char temp;
442 
443 	temp = readb(port->membase + UARTCR2);
444 	temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
445 	writeb(temp, port->membase + UARTCR2);
446 }
447 
448 static void lpuart32_stop_tx(struct uart_port *port)
449 {
450 	unsigned long temp;
451 
452 	temp = lpuart32_read(port, UARTCTRL);
453 	temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
454 	lpuart32_write(port, temp, UARTCTRL);
455 }
456 
457 static void lpuart_stop_rx(struct uart_port *port)
458 {
459 	unsigned char temp;
460 
461 	temp = readb(port->membase + UARTCR2);
462 	writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
463 }
464 
465 static void lpuart32_stop_rx(struct uart_port *port)
466 {
467 	unsigned long temp;
468 
469 	temp = lpuart32_read(port, UARTCTRL);
470 	lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
471 }
472 
473 static void lpuart_dma_tx(struct lpuart_port *sport)
474 {
475 	struct circ_buf *xmit = &sport->port.state->xmit;
476 	struct scatterlist *sgl = sport->tx_sgl;
477 	struct device *dev = sport->port.dev;
478 	struct dma_chan *chan = sport->dma_tx_chan;
479 	int ret;
480 
481 	if (sport->dma_tx_in_progress)
482 		return;
483 
484 	sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
485 
486 	if (xmit->tail < xmit->head || xmit->head == 0) {
487 		sport->dma_tx_nents = 1;
488 		sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
489 	} else {
490 		sport->dma_tx_nents = 2;
491 		sg_init_table(sgl, 2);
492 		sg_set_buf(sgl, xmit->buf + xmit->tail,
493 				UART_XMIT_SIZE - xmit->tail);
494 		sg_set_buf(sgl + 1, xmit->buf, xmit->head);
495 	}
496 
497 	ret = dma_map_sg(chan->device->dev, sgl, sport->dma_tx_nents,
498 			 DMA_TO_DEVICE);
499 	if (!ret) {
500 		dev_err(dev, "DMA mapping error for TX.\n");
501 		return;
502 	}
503 
504 	sport->dma_tx_desc = dmaengine_prep_slave_sg(chan, sgl,
505 					ret, DMA_MEM_TO_DEV,
506 					DMA_PREP_INTERRUPT);
507 	if (!sport->dma_tx_desc) {
508 		dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
509 			      DMA_TO_DEVICE);
510 		dev_err(dev, "Cannot prepare TX slave DMA!\n");
511 		return;
512 	}
513 
514 	sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
515 	sport->dma_tx_desc->callback_param = sport;
516 	sport->dma_tx_in_progress = true;
517 	sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
518 	dma_async_issue_pending(chan);
519 }
520 
521 static bool lpuart_stopped_or_empty(struct uart_port *port)
522 {
523 	return uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port);
524 }
525 
526 static void lpuart_dma_tx_complete(void *arg)
527 {
528 	struct lpuart_port *sport = arg;
529 	struct scatterlist *sgl = &sport->tx_sgl[0];
530 	struct circ_buf *xmit = &sport->port.state->xmit;
531 	struct dma_chan *chan = sport->dma_tx_chan;
532 	unsigned long flags;
533 
534 	spin_lock_irqsave(&sport->port.lock, flags);
535 	if (!sport->dma_tx_in_progress) {
536 		spin_unlock_irqrestore(&sport->port.lock, flags);
537 		return;
538 	}
539 
540 	dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
541 		     DMA_TO_DEVICE);
542 
543 	uart_xmit_advance(&sport->port, sport->dma_tx_bytes);
544 	sport->dma_tx_in_progress = false;
545 	spin_unlock_irqrestore(&sport->port.lock, flags);
546 
547 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
548 		uart_write_wakeup(&sport->port);
549 
550 	if (waitqueue_active(&sport->dma_wait)) {
551 		wake_up(&sport->dma_wait);
552 		return;
553 	}
554 
555 	spin_lock_irqsave(&sport->port.lock, flags);
556 
557 	if (!lpuart_stopped_or_empty(&sport->port))
558 		lpuart_dma_tx(sport);
559 
560 	spin_unlock_irqrestore(&sport->port.lock, flags);
561 }
562 
563 static dma_addr_t lpuart_dma_datareg_addr(struct lpuart_port *sport)
564 {
565 	switch (sport->port.iotype) {
566 	case UPIO_MEM32:
567 		return sport->port.mapbase + UARTDATA;
568 	case UPIO_MEM32BE:
569 		return sport->port.mapbase + UARTDATA + sizeof(u32) - 1;
570 	}
571 	return sport->port.mapbase + UARTDR;
572 }
573 
574 static int lpuart_dma_tx_request(struct uart_port *port)
575 {
576 	struct lpuart_port *sport = container_of(port,
577 					struct lpuart_port, port);
578 	struct dma_slave_config dma_tx_sconfig = {};
579 	int ret;
580 
581 	dma_tx_sconfig.dst_addr = lpuart_dma_datareg_addr(sport);
582 	dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
583 	dma_tx_sconfig.dst_maxburst = 1;
584 	dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
585 	ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
586 
587 	if (ret) {
588 		dev_err(sport->port.dev,
589 				"DMA slave config failed, err = %d\n", ret);
590 		return ret;
591 	}
592 
593 	return 0;
594 }
595 
596 static bool lpuart_is_32(struct lpuart_port *sport)
597 {
598 	return sport->port.iotype == UPIO_MEM32 ||
599 	       sport->port.iotype ==  UPIO_MEM32BE;
600 }
601 
602 static void lpuart_flush_buffer(struct uart_port *port)
603 {
604 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
605 	struct dma_chan *chan = sport->dma_tx_chan;
606 	u32 val;
607 
608 	if (sport->lpuart_dma_tx_use) {
609 		if (sport->dma_tx_in_progress) {
610 			dma_unmap_sg(chan->device->dev, &sport->tx_sgl[0],
611 				sport->dma_tx_nents, DMA_TO_DEVICE);
612 			sport->dma_tx_in_progress = false;
613 		}
614 		dmaengine_terminate_async(chan);
615 	}
616 
617 	if (lpuart_is_32(sport)) {
618 		val = lpuart32_read(&sport->port, UARTFIFO);
619 		val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
620 		lpuart32_write(&sport->port, val, UARTFIFO);
621 	} else {
622 		val = readb(sport->port.membase + UARTCFIFO);
623 		val |= UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH;
624 		writeb(val, sport->port.membase + UARTCFIFO);
625 	}
626 }
627 
628 static void lpuart_wait_bit_set(struct uart_port *port, unsigned int offset,
629 				u8 bit)
630 {
631 	while (!(readb(port->membase + offset) & bit))
632 		cpu_relax();
633 }
634 
635 static void lpuart32_wait_bit_set(struct uart_port *port, unsigned int offset,
636 				  u32 bit)
637 {
638 	while (!(lpuart32_read(port, offset) & bit))
639 		cpu_relax();
640 }
641 
642 #if defined(CONFIG_CONSOLE_POLL)
643 
644 static int lpuart_poll_init(struct uart_port *port)
645 {
646 	struct lpuart_port *sport = container_of(port,
647 					struct lpuart_port, port);
648 	unsigned long flags;
649 	unsigned char temp;
650 
651 	sport->port.fifosize = 0;
652 
653 	spin_lock_irqsave(&sport->port.lock, flags);
654 	/* Disable Rx & Tx */
655 	writeb(0, sport->port.membase + UARTCR2);
656 
657 	temp = readb(sport->port.membase + UARTPFIFO);
658 	/* Enable Rx and Tx FIFO */
659 	writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
660 			sport->port.membase + UARTPFIFO);
661 
662 	/* flush Tx and Rx FIFO */
663 	writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
664 			sport->port.membase + UARTCFIFO);
665 
666 	/* explicitly clear RDRF */
667 	if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
668 		readb(sport->port.membase + UARTDR);
669 		writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
670 	}
671 
672 	writeb(0, sport->port.membase + UARTTWFIFO);
673 	writeb(1, sport->port.membase + UARTRWFIFO);
674 
675 	/* Enable Rx and Tx */
676 	writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
677 	spin_unlock_irqrestore(&sport->port.lock, flags);
678 
679 	return 0;
680 }
681 
682 static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
683 {
684 	/* drain */
685 	lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
686 	writeb(c, port->membase + UARTDR);
687 }
688 
689 static int lpuart_poll_get_char(struct uart_port *port)
690 {
691 	if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
692 		return NO_POLL_CHAR;
693 
694 	return readb(port->membase + UARTDR);
695 }
696 
697 static int lpuart32_poll_init(struct uart_port *port)
698 {
699 	unsigned long flags;
700 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
701 	u32 temp;
702 
703 	sport->port.fifosize = 0;
704 
705 	spin_lock_irqsave(&sport->port.lock, flags);
706 
707 	/* Disable Rx & Tx */
708 	lpuart32_write(&sport->port, 0, UARTCTRL);
709 
710 	temp = lpuart32_read(&sport->port, UARTFIFO);
711 
712 	/* Enable Rx and Tx FIFO */
713 	lpuart32_write(&sport->port, temp | UARTFIFO_RXFE | UARTFIFO_TXFE, UARTFIFO);
714 
715 	/* flush Tx and Rx FIFO */
716 	lpuart32_write(&sport->port, UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH, UARTFIFO);
717 
718 	/* explicitly clear RDRF */
719 	if (lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_RDRF) {
720 		lpuart32_read(&sport->port, UARTDATA);
721 		lpuart32_write(&sport->port, UARTFIFO_RXUF, UARTFIFO);
722 	}
723 
724 	/* Enable Rx and Tx */
725 	lpuart32_write(&sport->port, UARTCTRL_RE | UARTCTRL_TE, UARTCTRL);
726 	spin_unlock_irqrestore(&sport->port.lock, flags);
727 
728 	return 0;
729 }
730 
731 static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
732 {
733 	lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
734 	lpuart32_write(port, c, UARTDATA);
735 }
736 
737 static int lpuart32_poll_get_char(struct uart_port *port)
738 {
739 	if (!(lpuart32_read(port, UARTWATER) >> UARTWATER_RXCNT_OFF))
740 		return NO_POLL_CHAR;
741 
742 	return lpuart32_read(port, UARTDATA);
743 }
744 #endif
745 
746 static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
747 {
748 	struct uart_port *port = &sport->port;
749 	u8 ch;
750 
751 	uart_port_tx(port, ch,
752 		readb(port->membase + UARTTCFIFO) < sport->txfifo_size,
753 		writeb(ch, port->membase + UARTDR));
754 }
755 
756 static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
757 {
758 	struct circ_buf *xmit = &sport->port.state->xmit;
759 	unsigned long txcnt;
760 
761 	if (sport->port.x_char) {
762 		lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
763 		sport->port.icount.tx++;
764 		sport->port.x_char = 0;
765 		return;
766 	}
767 
768 	if (lpuart_stopped_or_empty(&sport->port)) {
769 		lpuart32_stop_tx(&sport->port);
770 		return;
771 	}
772 
773 	txcnt = lpuart32_read(&sport->port, UARTWATER);
774 	txcnt = txcnt >> UARTWATER_TXCNT_OFF;
775 	txcnt &= UARTWATER_COUNT_MASK;
776 	while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
777 		lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
778 		uart_xmit_advance(&sport->port, 1);
779 		txcnt = lpuart32_read(&sport->port, UARTWATER);
780 		txcnt = txcnt >> UARTWATER_TXCNT_OFF;
781 		txcnt &= UARTWATER_COUNT_MASK;
782 	}
783 
784 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
785 		uart_write_wakeup(&sport->port);
786 
787 	if (uart_circ_empty(xmit))
788 		lpuart32_stop_tx(&sport->port);
789 }
790 
791 static void lpuart_start_tx(struct uart_port *port)
792 {
793 	struct lpuart_port *sport = container_of(port,
794 			struct lpuart_port, port);
795 	unsigned char temp;
796 
797 	temp = readb(port->membase + UARTCR2);
798 	writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
799 
800 	if (sport->lpuart_dma_tx_use) {
801 		if (!lpuart_stopped_or_empty(port))
802 			lpuart_dma_tx(sport);
803 	} else {
804 		if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
805 			lpuart_transmit_buffer(sport);
806 	}
807 }
808 
809 static void lpuart32_start_tx(struct uart_port *port)
810 {
811 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
812 	unsigned long temp;
813 
814 	if (sport->lpuart_dma_tx_use) {
815 		if (!lpuart_stopped_or_empty(port))
816 			lpuart_dma_tx(sport);
817 	} else {
818 		temp = lpuart32_read(port, UARTCTRL);
819 		lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
820 
821 		if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
822 			lpuart32_transmit_buffer(sport);
823 	}
824 }
825 
826 static void
827 lpuart_uart_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
828 {
829 	switch (state) {
830 	case UART_PM_STATE_OFF:
831 		pm_runtime_mark_last_busy(port->dev);
832 		pm_runtime_put_autosuspend(port->dev);
833 		break;
834 	default:
835 		pm_runtime_get_sync(port->dev);
836 		break;
837 	}
838 }
839 
840 /* return TIOCSER_TEMT when transmitter is not busy */
841 static unsigned int lpuart_tx_empty(struct uart_port *port)
842 {
843 	struct lpuart_port *sport = container_of(port,
844 			struct lpuart_port, port);
845 	unsigned char sr1 = readb(port->membase + UARTSR1);
846 	unsigned char sfifo = readb(port->membase + UARTSFIFO);
847 
848 	if (sport->dma_tx_in_progress)
849 		return 0;
850 
851 	if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
852 		return TIOCSER_TEMT;
853 
854 	return 0;
855 }
856 
857 static unsigned int lpuart32_tx_empty(struct uart_port *port)
858 {
859 	struct lpuart_port *sport = container_of(port,
860 			struct lpuart_port, port);
861 	unsigned long stat = lpuart32_read(port, UARTSTAT);
862 	unsigned long sfifo = lpuart32_read(port, UARTFIFO);
863 	unsigned long ctrl = lpuart32_read(port, UARTCTRL);
864 
865 	if (sport->dma_tx_in_progress)
866 		return 0;
867 
868 	/*
869 	 * LPUART Transmission Complete Flag may never be set while queuing a break
870 	 * character, so avoid checking for transmission complete when UARTCTRL_SBK
871 	 * is asserted.
872 	 */
873 	if ((stat & UARTSTAT_TC && sfifo & UARTFIFO_TXEMPT) || ctrl & UARTCTRL_SBK)
874 		return TIOCSER_TEMT;
875 
876 	return 0;
877 }
878 
879 static void lpuart_txint(struct lpuart_port *sport)
880 {
881 	spin_lock(&sport->port.lock);
882 	lpuart_transmit_buffer(sport);
883 	spin_unlock(&sport->port.lock);
884 }
885 
886 static void lpuart_rxint(struct lpuart_port *sport)
887 {
888 	unsigned int flg, ignored = 0, overrun = 0;
889 	struct tty_port *port = &sport->port.state->port;
890 	unsigned char rx, sr;
891 
892 	spin_lock(&sport->port.lock);
893 
894 	while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
895 		flg = TTY_NORMAL;
896 		sport->port.icount.rx++;
897 		/*
898 		 * to clear the FE, OR, NF, FE, PE flags,
899 		 * read SR1 then read DR
900 		 */
901 		sr = readb(sport->port.membase + UARTSR1);
902 		rx = readb(sport->port.membase + UARTDR);
903 
904 		if (uart_prepare_sysrq_char(&sport->port, rx))
905 			continue;
906 
907 		if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
908 			if (sr & UARTSR1_PE)
909 				sport->port.icount.parity++;
910 			else if (sr & UARTSR1_FE)
911 				sport->port.icount.frame++;
912 
913 			if (sr & UARTSR1_OR)
914 				overrun++;
915 
916 			if (sr & sport->port.ignore_status_mask) {
917 				if (++ignored > 100)
918 					goto out;
919 				continue;
920 			}
921 
922 			sr &= sport->port.read_status_mask;
923 
924 			if (sr & UARTSR1_PE)
925 				flg = TTY_PARITY;
926 			else if (sr & UARTSR1_FE)
927 				flg = TTY_FRAME;
928 
929 			if (sr & UARTSR1_OR)
930 				flg = TTY_OVERRUN;
931 
932 			sport->port.sysrq = 0;
933 		}
934 
935 		if (tty_insert_flip_char(port, rx, flg) == 0)
936 			sport->port.icount.buf_overrun++;
937 	}
938 
939 out:
940 	if (overrun) {
941 		sport->port.icount.overrun += overrun;
942 
943 		/*
944 		 * Overruns cause FIFO pointers to become missaligned.
945 		 * Flushing the receive FIFO reinitializes the pointers.
946 		 */
947 		writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
948 		writeb(UARTSFIFO_RXOF, sport->port.membase + UARTSFIFO);
949 	}
950 
951 	uart_unlock_and_check_sysrq(&sport->port);
952 
953 	tty_flip_buffer_push(port);
954 }
955 
956 static void lpuart32_txint(struct lpuart_port *sport)
957 {
958 	spin_lock(&sport->port.lock);
959 	lpuart32_transmit_buffer(sport);
960 	spin_unlock(&sport->port.lock);
961 }
962 
963 static void lpuart32_rxint(struct lpuart_port *sport)
964 {
965 	unsigned int flg, ignored = 0;
966 	struct tty_port *port = &sport->port.state->port;
967 	unsigned long rx, sr;
968 	bool is_break;
969 
970 	spin_lock(&sport->port.lock);
971 
972 	while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
973 		flg = TTY_NORMAL;
974 		sport->port.icount.rx++;
975 		/*
976 		 * to clear the FE, OR, NF, FE, PE flags,
977 		 * read STAT then read DATA reg
978 		 */
979 		sr = lpuart32_read(&sport->port, UARTSTAT);
980 		rx = lpuart32_read(&sport->port, UARTDATA);
981 		rx &= UARTDATA_MASK;
982 
983 		/*
984 		 * The LPUART can't distinguish between a break and a framing error,
985 		 * thus we assume it is a break if the received data is zero.
986 		 */
987 		is_break = (sr & UARTSTAT_FE) && !rx;
988 
989 		if (is_break && uart_handle_break(&sport->port))
990 			continue;
991 
992 		if (uart_prepare_sysrq_char(&sport->port, rx))
993 			continue;
994 
995 		if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
996 			if (sr & UARTSTAT_PE) {
997 				sport->port.icount.parity++;
998 			} else if (sr & UARTSTAT_FE) {
999 				if (is_break)
1000 					sport->port.icount.brk++;
1001 				else
1002 					sport->port.icount.frame++;
1003 			}
1004 
1005 			if (sr & UARTSTAT_OR)
1006 				sport->port.icount.overrun++;
1007 
1008 			if (sr & sport->port.ignore_status_mask) {
1009 				if (++ignored > 100)
1010 					goto out;
1011 				continue;
1012 			}
1013 
1014 			sr &= sport->port.read_status_mask;
1015 
1016 			if (sr & UARTSTAT_PE) {
1017 				flg = TTY_PARITY;
1018 			} else if (sr & UARTSTAT_FE) {
1019 				if (is_break)
1020 					flg = TTY_BREAK;
1021 				else
1022 					flg = TTY_FRAME;
1023 			}
1024 
1025 			if (sr & UARTSTAT_OR)
1026 				flg = TTY_OVERRUN;
1027 		}
1028 
1029 		if (sport->is_cs7)
1030 			rx &= 0x7F;
1031 
1032 		if (tty_insert_flip_char(port, rx, flg) == 0)
1033 			sport->port.icount.buf_overrun++;
1034 	}
1035 
1036 out:
1037 	uart_unlock_and_check_sysrq(&sport->port);
1038 
1039 	tty_flip_buffer_push(port);
1040 }
1041 
1042 static irqreturn_t lpuart_int(int irq, void *dev_id)
1043 {
1044 	struct lpuart_port *sport = dev_id;
1045 	unsigned char sts;
1046 
1047 	sts = readb(sport->port.membase + UARTSR1);
1048 
1049 	/* SysRq, using dma, check for linebreak by framing err. */
1050 	if (sts & UARTSR1_FE && sport->lpuart_dma_rx_use) {
1051 		readb(sport->port.membase + UARTDR);
1052 		uart_handle_break(&sport->port);
1053 		/* linebreak produces some garbage, removing it */
1054 		writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
1055 		return IRQ_HANDLED;
1056 	}
1057 
1058 	if (sts & UARTSR1_RDRF && !sport->lpuart_dma_rx_use)
1059 		lpuart_rxint(sport);
1060 
1061 	if (sts & UARTSR1_TDRE && !sport->lpuart_dma_tx_use)
1062 		lpuart_txint(sport);
1063 
1064 	return IRQ_HANDLED;
1065 }
1066 
1067 static inline void lpuart_handle_sysrq_chars(struct uart_port *port,
1068 					     unsigned char *p, int count)
1069 {
1070 	while (count--) {
1071 		if (*p && uart_handle_sysrq_char(port, *p))
1072 			return;
1073 		p++;
1074 	}
1075 }
1076 
1077 static void lpuart_handle_sysrq(struct lpuart_port *sport)
1078 {
1079 	struct circ_buf *ring = &sport->rx_ring;
1080 	int count;
1081 
1082 	if (ring->head < ring->tail) {
1083 		count = sport->rx_sgl.length - ring->tail;
1084 		lpuart_handle_sysrq_chars(&sport->port,
1085 					  ring->buf + ring->tail, count);
1086 		ring->tail = 0;
1087 	}
1088 
1089 	if (ring->head > ring->tail) {
1090 		count = ring->head - ring->tail;
1091 		lpuart_handle_sysrq_chars(&sport->port,
1092 					  ring->buf + ring->tail, count);
1093 		ring->tail = ring->head;
1094 	}
1095 }
1096 
1097 static int lpuart_tty_insert_flip_string(struct tty_port *port,
1098 	unsigned char *chars, size_t size, bool is_cs7)
1099 {
1100 	int i;
1101 
1102 	if (is_cs7)
1103 		for (i = 0; i < size; i++)
1104 			chars[i] &= 0x7F;
1105 	return tty_insert_flip_string(port, chars, size);
1106 }
1107 
1108 static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
1109 {
1110 	struct tty_port *port = &sport->port.state->port;
1111 	struct dma_tx_state state;
1112 	enum dma_status dmastat;
1113 	struct dma_chan *chan = sport->dma_rx_chan;
1114 	struct circ_buf *ring = &sport->rx_ring;
1115 	unsigned long flags;
1116 	int count, copied;
1117 
1118 	if (lpuart_is_32(sport)) {
1119 		unsigned long sr = lpuart32_read(&sport->port, UARTSTAT);
1120 
1121 		if (sr & (UARTSTAT_PE | UARTSTAT_FE)) {
1122 			/* Read DR to clear the error flags */
1123 			lpuart32_read(&sport->port, UARTDATA);
1124 
1125 			if (sr & UARTSTAT_PE)
1126 				sport->port.icount.parity++;
1127 			else if (sr & UARTSTAT_FE)
1128 				sport->port.icount.frame++;
1129 		}
1130 	} else {
1131 		unsigned char sr = readb(sport->port.membase + UARTSR1);
1132 
1133 		if (sr & (UARTSR1_PE | UARTSR1_FE)) {
1134 			unsigned char cr2;
1135 
1136 			/* Disable receiver during this operation... */
1137 			cr2 = readb(sport->port.membase + UARTCR2);
1138 			cr2 &= ~UARTCR2_RE;
1139 			writeb(cr2, sport->port.membase + UARTCR2);
1140 
1141 			/* Read DR to clear the error flags */
1142 			readb(sport->port.membase + UARTDR);
1143 
1144 			if (sr & UARTSR1_PE)
1145 				sport->port.icount.parity++;
1146 			else if (sr & UARTSR1_FE)
1147 				sport->port.icount.frame++;
1148 			/*
1149 			 * At this point parity/framing error is
1150 			 * cleared However, since the DMA already read
1151 			 * the data register and we had to read it
1152 			 * again after reading the status register to
1153 			 * properly clear the flags, the FIFO actually
1154 			 * underflowed... This requires a clearing of
1155 			 * the FIFO...
1156 			 */
1157 			if (readb(sport->port.membase + UARTSFIFO) &
1158 			    UARTSFIFO_RXUF) {
1159 				writeb(UARTSFIFO_RXUF,
1160 				       sport->port.membase + UARTSFIFO);
1161 				writeb(UARTCFIFO_RXFLUSH,
1162 				       sport->port.membase + UARTCFIFO);
1163 			}
1164 
1165 			cr2 |= UARTCR2_RE;
1166 			writeb(cr2, sport->port.membase + UARTCR2);
1167 		}
1168 	}
1169 
1170 	async_tx_ack(sport->dma_rx_desc);
1171 
1172 	spin_lock_irqsave(&sport->port.lock, flags);
1173 
1174 	dmastat = dmaengine_tx_status(chan, sport->dma_rx_cookie, &state);
1175 	if (dmastat == DMA_ERROR) {
1176 		dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
1177 		spin_unlock_irqrestore(&sport->port.lock, flags);
1178 		return;
1179 	}
1180 
1181 	/* CPU claims ownership of RX DMA buffer */
1182 	dma_sync_sg_for_cpu(chan->device->dev, &sport->rx_sgl, 1,
1183 			    DMA_FROM_DEVICE);
1184 
1185 	/*
1186 	 * ring->head points to the end of data already written by the DMA.
1187 	 * ring->tail points to the beginning of data to be read by the
1188 	 * framework.
1189 	 * The current transfer size should not be larger than the dma buffer
1190 	 * length.
1191 	 */
1192 	ring->head = sport->rx_sgl.length - state.residue;
1193 	BUG_ON(ring->head > sport->rx_sgl.length);
1194 
1195 	/*
1196 	 * Silent handling of keys pressed in the sysrq timeframe
1197 	 */
1198 	if (sport->port.sysrq) {
1199 		lpuart_handle_sysrq(sport);
1200 		goto exit;
1201 	}
1202 
1203 	/*
1204 	 * At this point ring->head may point to the first byte right after the
1205 	 * last byte of the dma buffer:
1206 	 * 0 <= ring->head <= sport->rx_sgl.length
1207 	 *
1208 	 * However ring->tail must always points inside the dma buffer:
1209 	 * 0 <= ring->tail <= sport->rx_sgl.length - 1
1210 	 *
1211 	 * Since we use a ring buffer, we have to handle the case
1212 	 * where head is lower than tail. In such a case, we first read from
1213 	 * tail to the end of the buffer then reset tail.
1214 	 */
1215 	if (ring->head < ring->tail) {
1216 		count = sport->rx_sgl.length - ring->tail;
1217 
1218 		copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
1219 					count, sport->is_cs7);
1220 		if (copied != count)
1221 			sport->port.icount.buf_overrun++;
1222 		ring->tail = 0;
1223 		sport->port.icount.rx += copied;
1224 	}
1225 
1226 	/* Finally we read data from tail to head */
1227 	if (ring->tail < ring->head) {
1228 		count = ring->head - ring->tail;
1229 		copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
1230 					count, sport->is_cs7);
1231 		if (copied != count)
1232 			sport->port.icount.buf_overrun++;
1233 		/* Wrap ring->head if needed */
1234 		if (ring->head >= sport->rx_sgl.length)
1235 			ring->head = 0;
1236 		ring->tail = ring->head;
1237 		sport->port.icount.rx += copied;
1238 	}
1239 
1240 	sport->last_residue = state.residue;
1241 
1242 exit:
1243 	dma_sync_sg_for_device(chan->device->dev, &sport->rx_sgl, 1,
1244 			       DMA_FROM_DEVICE);
1245 
1246 	spin_unlock_irqrestore(&sport->port.lock, flags);
1247 
1248 	tty_flip_buffer_push(port);
1249 	mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
1250 }
1251 
1252 static void lpuart_dma_rx_complete(void *arg)
1253 {
1254 	struct lpuart_port *sport = arg;
1255 
1256 	lpuart_copy_rx_to_tty(sport);
1257 }
1258 
1259 static irqreturn_t lpuart32_int(int irq, void *dev_id)
1260 {
1261 	struct lpuart_port *sport = dev_id;
1262 	unsigned long sts, rxcount;
1263 
1264 	sts = lpuart32_read(&sport->port, UARTSTAT);
1265 	rxcount = lpuart32_read(&sport->port, UARTWATER);
1266 	rxcount = rxcount >> UARTWATER_RXCNT_OFF;
1267 
1268 	if ((sts & UARTSTAT_RDRF || rxcount > 0) && !sport->lpuart_dma_rx_use)
1269 		lpuart32_rxint(sport);
1270 
1271 	if ((sts & UARTSTAT_TDRE) && !sport->lpuart_dma_tx_use)
1272 		lpuart32_txint(sport);
1273 
1274 	lpuart32_write(&sport->port, sts, UARTSTAT);
1275 	return IRQ_HANDLED;
1276 }
1277 
1278 /*
1279  * Timer function to simulate the hardware EOP (End Of Package) event.
1280  * The timer callback is to check for new RX data and copy to TTY buffer.
1281  * If no new data are received since last interval, the EOP condition is
1282  * met, complete the DMA transfer by copying the data. Otherwise, just
1283  * restart timer.
1284  */
1285 static void lpuart_timer_func(struct timer_list *t)
1286 {
1287 	struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
1288 	enum dma_status dmastat;
1289 	struct dma_chan *chan = sport->dma_rx_chan;
1290 	struct circ_buf *ring = &sport->rx_ring;
1291 	struct dma_tx_state state;
1292 	unsigned long flags;
1293 	int count;
1294 
1295 	dmastat = dmaengine_tx_status(chan, sport->dma_rx_cookie, &state);
1296 	if (dmastat == DMA_ERROR) {
1297 		dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
1298 		return;
1299 	}
1300 
1301 	ring->head = sport->rx_sgl.length - state.residue;
1302 	count = CIRC_CNT(ring->head, ring->tail, sport->rx_sgl.length);
1303 
1304 	/* Check if new data received before copying */
1305 	if ((count != 0) && (sport->last_residue == state.residue))
1306 		lpuart_copy_rx_to_tty(sport);
1307 	else
1308 		mod_timer(&sport->lpuart_timer,
1309 			  jiffies + sport->dma_rx_timeout);
1310 
1311 	if (spin_trylock_irqsave(&sport->port.lock, flags)) {
1312 		sport->last_residue = state.residue;
1313 		spin_unlock_irqrestore(&sport->port.lock, flags);
1314 	}
1315 }
1316 
1317 static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
1318 {
1319 	struct dma_slave_config dma_rx_sconfig = {};
1320 	struct circ_buf *ring = &sport->rx_ring;
1321 	int ret, nent;
1322 	struct tty_port *port = &sport->port.state->port;
1323 	struct tty_struct *tty = port->tty;
1324 	struct ktermios *termios = &tty->termios;
1325 	struct dma_chan *chan = sport->dma_rx_chan;
1326 	unsigned int bits = tty_get_frame_size(termios->c_cflag);
1327 	unsigned int baud = tty_get_baud_rate(tty);
1328 
1329 	/*
1330 	 * Calculate length of one DMA buffer size to keep latency below
1331 	 * 10ms at any baud rate.
1332 	 */
1333 	sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud /  bits / 1000) * 2;
1334 	sport->rx_dma_rng_buf_len = (1 << fls(sport->rx_dma_rng_buf_len));
1335 	sport->rx_dma_rng_buf_len = max_t(int,
1336 					  sport->rxfifo_size * 2,
1337 					  sport->rx_dma_rng_buf_len);
1338 	/*
1339 	 * Keep this condition check in case rxfifo_size is unavailable
1340 	 * for some SoCs.
1341 	 */
1342 	if (sport->rx_dma_rng_buf_len < 16)
1343 		sport->rx_dma_rng_buf_len = 16;
1344 
1345 	sport->last_residue = 0;
1346 	sport->dma_rx_timeout = max(nsecs_to_jiffies(
1347 		sport->port.frame_time * DMA_RX_IDLE_CHARS), 1UL);
1348 
1349 	ring->buf = kzalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
1350 	if (!ring->buf)
1351 		return -ENOMEM;
1352 
1353 	sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1354 	nent = dma_map_sg(chan->device->dev, &sport->rx_sgl, 1,
1355 			  DMA_FROM_DEVICE);
1356 
1357 	if (!nent) {
1358 		dev_err(sport->port.dev, "DMA Rx mapping error\n");
1359 		return -EINVAL;
1360 	}
1361 
1362 	dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
1363 	dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1364 	dma_rx_sconfig.src_maxburst = 1;
1365 	dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
1366 	ret = dmaengine_slave_config(chan, &dma_rx_sconfig);
1367 
1368 	if (ret < 0) {
1369 		dev_err(sport->port.dev,
1370 				"DMA Rx slave config failed, err = %d\n", ret);
1371 		return ret;
1372 	}
1373 
1374 	sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
1375 				 sg_dma_address(&sport->rx_sgl),
1376 				 sport->rx_sgl.length,
1377 				 sport->rx_sgl.length / 2,
1378 				 DMA_DEV_TO_MEM,
1379 				 DMA_PREP_INTERRUPT);
1380 	if (!sport->dma_rx_desc) {
1381 		dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
1382 		return -EFAULT;
1383 	}
1384 
1385 	sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
1386 	sport->dma_rx_desc->callback_param = sport;
1387 	sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
1388 	dma_async_issue_pending(chan);
1389 
1390 	if (lpuart_is_32(sport)) {
1391 		unsigned long temp = lpuart32_read(&sport->port, UARTBAUD);
1392 
1393 		lpuart32_write(&sport->port, temp | UARTBAUD_RDMAE, UARTBAUD);
1394 	} else {
1395 		writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
1396 		       sport->port.membase + UARTCR5);
1397 	}
1398 
1399 	return 0;
1400 }
1401 
1402 static void lpuart_dma_rx_free(struct uart_port *port)
1403 {
1404 	struct lpuart_port *sport = container_of(port,
1405 					struct lpuart_port, port);
1406 	struct dma_chan *chan = sport->dma_rx_chan;
1407 
1408 	dmaengine_terminate_sync(chan);
1409 	del_timer_sync(&sport->lpuart_timer);
1410 	dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1411 	kfree(sport->rx_ring.buf);
1412 	sport->rx_ring.tail = 0;
1413 	sport->rx_ring.head = 0;
1414 	sport->dma_rx_desc = NULL;
1415 	sport->dma_rx_cookie = -EINVAL;
1416 }
1417 
1418 static int lpuart_config_rs485(struct uart_port *port, struct ktermios *termios,
1419 			struct serial_rs485 *rs485)
1420 {
1421 	struct lpuart_port *sport = container_of(port,
1422 			struct lpuart_port, port);
1423 
1424 	u8 modem = readb(sport->port.membase + UARTMODEM) &
1425 		~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
1426 	writeb(modem, sport->port.membase + UARTMODEM);
1427 
1428 	if (rs485->flags & SER_RS485_ENABLED) {
1429 		/* Enable auto RS-485 RTS mode */
1430 		modem |= UARTMODEM_TXRTSE;
1431 
1432 		/*
1433 		 * The hardware defaults to RTS logic HIGH while transfer.
1434 		 * Switch polarity in case RTS shall be logic HIGH
1435 		 * after transfer.
1436 		 * Note: UART is assumed to be active high.
1437 		 */
1438 		if (rs485->flags & SER_RS485_RTS_ON_SEND)
1439 			modem |= UARTMODEM_TXRTSPOL;
1440 		else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1441 			modem &= ~UARTMODEM_TXRTSPOL;
1442 	}
1443 
1444 	writeb(modem, sport->port.membase + UARTMODEM);
1445 	return 0;
1446 }
1447 
1448 static int lpuart32_config_rs485(struct uart_port *port, struct ktermios *termios,
1449 			struct serial_rs485 *rs485)
1450 {
1451 	struct lpuart_port *sport = container_of(port,
1452 			struct lpuart_port, port);
1453 
1454 	unsigned long modem = lpuart32_read(&sport->port, UARTMODIR)
1455 				& ~(UARTMODIR_TXRTSPOL | UARTMODIR_TXRTSE);
1456 	lpuart32_write(&sport->port, modem, UARTMODIR);
1457 
1458 	if (rs485->flags & SER_RS485_ENABLED) {
1459 		/* Enable auto RS-485 RTS mode */
1460 		modem |= UARTMODIR_TXRTSE;
1461 
1462 		/*
1463 		 * The hardware defaults to RTS logic HIGH while transfer.
1464 		 * Switch polarity in case RTS shall be logic HIGH
1465 		 * after transfer.
1466 		 * Note: UART is assumed to be active high.
1467 		 */
1468 		if (rs485->flags & SER_RS485_RTS_ON_SEND)
1469 			modem |= UARTMODIR_TXRTSPOL;
1470 		else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1471 			modem &= ~UARTMODIR_TXRTSPOL;
1472 	}
1473 
1474 	lpuart32_write(&sport->port, modem, UARTMODIR);
1475 	return 0;
1476 }
1477 
1478 static unsigned int lpuart_get_mctrl(struct uart_port *port)
1479 {
1480 	unsigned int mctrl = 0;
1481 	u8 reg;
1482 
1483 	reg = readb(port->membase + UARTCR1);
1484 	if (reg & UARTCR1_LOOPS)
1485 		mctrl |= TIOCM_LOOP;
1486 
1487 	return mctrl;
1488 }
1489 
1490 static unsigned int lpuart32_get_mctrl(struct uart_port *port)
1491 {
1492 	unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1493 	u32 reg;
1494 
1495 	reg = lpuart32_read(port, UARTCTRL);
1496 	if (reg & UARTCTRL_LOOPS)
1497 		mctrl |= TIOCM_LOOP;
1498 
1499 	return mctrl;
1500 }
1501 
1502 static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1503 {
1504 	u8 reg;
1505 
1506 	reg = readb(port->membase + UARTCR1);
1507 
1508 	/* for internal loopback we need LOOPS=1 and RSRC=0 */
1509 	reg &= ~(UARTCR1_LOOPS | UARTCR1_RSRC);
1510 	if (mctrl & TIOCM_LOOP)
1511 		reg |= UARTCR1_LOOPS;
1512 
1513 	writeb(reg, port->membase + UARTCR1);
1514 }
1515 
1516 static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
1517 {
1518 	u32 reg;
1519 
1520 	reg = lpuart32_read(port, UARTCTRL);
1521 
1522 	/* for internal loopback we need LOOPS=1 and RSRC=0 */
1523 	reg &= ~(UARTCTRL_LOOPS | UARTCTRL_RSRC);
1524 	if (mctrl & TIOCM_LOOP)
1525 		reg |= UARTCTRL_LOOPS;
1526 
1527 	lpuart32_write(port, reg, UARTCTRL);
1528 }
1529 
1530 static void lpuart_break_ctl(struct uart_port *port, int break_state)
1531 {
1532 	unsigned char temp;
1533 
1534 	temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
1535 
1536 	if (break_state != 0)
1537 		temp |= UARTCR2_SBK;
1538 
1539 	writeb(temp, port->membase + UARTCR2);
1540 }
1541 
1542 static void lpuart32_break_ctl(struct uart_port *port, int break_state)
1543 {
1544 	unsigned long temp;
1545 
1546 	temp = lpuart32_read(port, UARTCTRL);
1547 
1548 	/*
1549 	 * LPUART IP now has two known bugs, one is CTS has higher priority than the
1550 	 * break signal, which causes the break signal sending through UARTCTRL_SBK
1551 	 * may impacted by the CTS input if the HW flow control is enabled. It
1552 	 * exists on all platforms we support in this driver.
1553 	 * Another bug is i.MX8QM LPUART may have an additional break character
1554 	 * being sent after SBK was cleared.
1555 	 * To avoid above two bugs, we use Transmit Data Inversion function to send
1556 	 * the break signal instead of UARTCTRL_SBK.
1557 	 */
1558 	if (break_state != 0) {
1559 		/*
1560 		 * Disable the transmitter to prevent any data from being sent out
1561 		 * during break, then invert the TX line to send break.
1562 		 */
1563 		temp &= ~UARTCTRL_TE;
1564 		lpuart32_write(port, temp, UARTCTRL);
1565 		temp |= UARTCTRL_TXINV;
1566 		lpuart32_write(port, temp, UARTCTRL);
1567 	} else {
1568 		/* Disable the TXINV to turn off break and re-enable transmitter. */
1569 		temp &= ~UARTCTRL_TXINV;
1570 		lpuart32_write(port, temp, UARTCTRL);
1571 		temp |= UARTCTRL_TE;
1572 		lpuart32_write(port, temp, UARTCTRL);
1573 	}
1574 }
1575 
1576 static void lpuart_setup_watermark(struct lpuart_port *sport)
1577 {
1578 	unsigned char val, cr2;
1579 	unsigned char cr2_saved;
1580 
1581 	cr2 = readb(sport->port.membase + UARTCR2);
1582 	cr2_saved = cr2;
1583 	cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
1584 			UARTCR2_RIE | UARTCR2_RE);
1585 	writeb(cr2, sport->port.membase + UARTCR2);
1586 
1587 	val = readb(sport->port.membase + UARTPFIFO);
1588 	writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
1589 			sport->port.membase + UARTPFIFO);
1590 
1591 	/* flush Tx and Rx FIFO */
1592 	writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
1593 			sport->port.membase + UARTCFIFO);
1594 
1595 	/* explicitly clear RDRF */
1596 	if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
1597 		readb(sport->port.membase + UARTDR);
1598 		writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
1599 	}
1600 
1601 	if (uart_console(&sport->port))
1602 		sport->rx_watermark = 1;
1603 	writeb(0, sport->port.membase + UARTTWFIFO);
1604 	writeb(sport->rx_watermark, sport->port.membase + UARTRWFIFO);
1605 
1606 	/* Restore cr2 */
1607 	writeb(cr2_saved, sport->port.membase + UARTCR2);
1608 }
1609 
1610 static void lpuart_setup_watermark_enable(struct lpuart_port *sport)
1611 {
1612 	unsigned char cr2;
1613 
1614 	lpuart_setup_watermark(sport);
1615 
1616 	cr2 = readb(sport->port.membase + UARTCR2);
1617 	cr2 |= UARTCR2_RIE | UARTCR2_RE | UARTCR2_TE;
1618 	writeb(cr2, sport->port.membase + UARTCR2);
1619 }
1620 
1621 static void lpuart32_setup_watermark(struct lpuart_port *sport)
1622 {
1623 	unsigned long val, ctrl;
1624 	unsigned long ctrl_saved;
1625 
1626 	ctrl = lpuart32_read(&sport->port, UARTCTRL);
1627 	ctrl_saved = ctrl;
1628 	ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
1629 			UARTCTRL_RIE | UARTCTRL_RE | UARTCTRL_ILIE);
1630 	lpuart32_write(&sport->port, ctrl, UARTCTRL);
1631 
1632 	/* enable FIFO mode */
1633 	val = lpuart32_read(&sport->port, UARTFIFO);
1634 	val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
1635 	val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
1636 	val |= FIELD_PREP(UARTFIFO_RXIDEN, 0x3);
1637 	lpuart32_write(&sport->port, val, UARTFIFO);
1638 
1639 	/* set the watermark */
1640 	if (uart_console(&sport->port))
1641 		sport->rx_watermark = 1;
1642 	val = (sport->rx_watermark << UARTWATER_RXWATER_OFF) |
1643 	      (0x0 << UARTWATER_TXWATER_OFF);
1644 	lpuart32_write(&sport->port, val, UARTWATER);
1645 
1646 	/* set RTS watermark */
1647 	if (!uart_console(&sport->port)) {
1648 		val = lpuart32_read(&sport->port, UARTMODIR);
1649 		val |= FIELD_PREP(UARTMODIR_RTSWATER, sport->rxfifo_size >> 1);
1650 		lpuart32_write(&sport->port, val, UARTMODIR);
1651 	}
1652 
1653 	/* Restore cr2 */
1654 	lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
1655 }
1656 
1657 static void lpuart32_setup_watermark_enable(struct lpuart_port *sport)
1658 {
1659 	u32 temp;
1660 
1661 	lpuart32_setup_watermark(sport);
1662 
1663 	temp = lpuart32_read(&sport->port, UARTCTRL);
1664 	temp |= UARTCTRL_RE | UARTCTRL_TE;
1665 	temp |= FIELD_PREP(UARTCTRL_IDLECFG, 0x7);
1666 	lpuart32_write(&sport->port, temp, UARTCTRL);
1667 }
1668 
1669 static void rx_dma_timer_init(struct lpuart_port *sport)
1670 {
1671 	timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
1672 	sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
1673 	add_timer(&sport->lpuart_timer);
1674 }
1675 
1676 static void lpuart_request_dma(struct lpuart_port *sport)
1677 {
1678 	sport->dma_tx_chan = dma_request_chan(sport->port.dev, "tx");
1679 	if (IS_ERR(sport->dma_tx_chan)) {
1680 		dev_dbg_once(sport->port.dev,
1681 			     "DMA tx channel request failed, operating without tx DMA (%ld)\n",
1682 			     PTR_ERR(sport->dma_tx_chan));
1683 		sport->dma_tx_chan = NULL;
1684 	}
1685 
1686 	sport->dma_rx_chan = dma_request_chan(sport->port.dev, "rx");
1687 	if (IS_ERR(sport->dma_rx_chan)) {
1688 		dev_dbg_once(sport->port.dev,
1689 			     "DMA rx channel request failed, operating without rx DMA (%ld)\n",
1690 			     PTR_ERR(sport->dma_rx_chan));
1691 		sport->dma_rx_chan = NULL;
1692 	}
1693 }
1694 
1695 static void lpuart_tx_dma_startup(struct lpuart_port *sport)
1696 {
1697 	u32 uartbaud;
1698 	int ret;
1699 
1700 	if (uart_console(&sport->port))
1701 		goto err;
1702 
1703 	if (!sport->dma_tx_chan)
1704 		goto err;
1705 
1706 	ret = lpuart_dma_tx_request(&sport->port);
1707 	if (ret)
1708 		goto err;
1709 
1710 	init_waitqueue_head(&sport->dma_wait);
1711 	sport->lpuart_dma_tx_use = true;
1712 	if (lpuart_is_32(sport)) {
1713 		uartbaud = lpuart32_read(&sport->port, UARTBAUD);
1714 		lpuart32_write(&sport->port,
1715 			       uartbaud | UARTBAUD_TDMAE, UARTBAUD);
1716 	} else {
1717 		writeb(readb(sport->port.membase + UARTCR5) |
1718 		       UARTCR5_TDMAS, sport->port.membase + UARTCR5);
1719 	}
1720 
1721 	return;
1722 
1723 err:
1724 	sport->lpuart_dma_tx_use = false;
1725 }
1726 
1727 static void lpuart_rx_dma_startup(struct lpuart_port *sport)
1728 {
1729 	int ret;
1730 	unsigned char cr3;
1731 
1732 	if (uart_console(&sport->port))
1733 		goto err;
1734 
1735 	if (!sport->dma_rx_chan)
1736 		goto err;
1737 
1738 	/* set default Rx DMA timeout */
1739 	sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
1740 
1741 	ret = lpuart_start_rx_dma(sport);
1742 	if (ret)
1743 		goto err;
1744 
1745 	if (!sport->dma_rx_timeout)
1746 		sport->dma_rx_timeout = 1;
1747 
1748 	sport->lpuart_dma_rx_use = true;
1749 	rx_dma_timer_init(sport);
1750 
1751 	if (sport->port.has_sysrq && !lpuart_is_32(sport)) {
1752 		cr3 = readb(sport->port.membase + UARTCR3);
1753 		cr3 |= UARTCR3_FEIE;
1754 		writeb(cr3, sport->port.membase + UARTCR3);
1755 	}
1756 
1757 	return;
1758 
1759 err:
1760 	sport->lpuart_dma_rx_use = false;
1761 }
1762 
1763 static void lpuart_hw_setup(struct lpuart_port *sport)
1764 {
1765 	unsigned long flags;
1766 
1767 	spin_lock_irqsave(&sport->port.lock, flags);
1768 
1769 	lpuart_setup_watermark_enable(sport);
1770 
1771 	lpuart_rx_dma_startup(sport);
1772 	lpuart_tx_dma_startup(sport);
1773 
1774 	spin_unlock_irqrestore(&sport->port.lock, flags);
1775 }
1776 
1777 static int lpuart_startup(struct uart_port *port)
1778 {
1779 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1780 	unsigned char temp;
1781 
1782 	/* determine FIFO size and enable FIFO mode */
1783 	temp = readb(sport->port.membase + UARTPFIFO);
1784 
1785 	sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_TXSIZE_OFF) &
1786 					    UARTPFIFO_FIFOSIZE_MASK);
1787 	sport->port.fifosize = sport->txfifo_size;
1788 
1789 	sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
1790 					    UARTPFIFO_FIFOSIZE_MASK);
1791 
1792 	lpuart_request_dma(sport);
1793 	lpuart_hw_setup(sport);
1794 
1795 	return 0;
1796 }
1797 
1798 static void lpuart32_hw_disable(struct lpuart_port *sport)
1799 {
1800 	unsigned long temp;
1801 
1802 	temp = lpuart32_read(&sport->port, UARTCTRL);
1803 	temp &= ~(UARTCTRL_RIE | UARTCTRL_ILIE | UARTCTRL_RE |
1804 		  UARTCTRL_TIE | UARTCTRL_TE);
1805 	lpuart32_write(&sport->port, temp, UARTCTRL);
1806 }
1807 
1808 static void lpuart32_configure(struct lpuart_port *sport)
1809 {
1810 	unsigned long temp;
1811 
1812 	temp = lpuart32_read(&sport->port, UARTCTRL);
1813 	if (!sport->lpuart_dma_rx_use)
1814 		temp |= UARTCTRL_RIE | UARTCTRL_ILIE;
1815 	if (!sport->lpuart_dma_tx_use)
1816 		temp |= UARTCTRL_TIE;
1817 	lpuart32_write(&sport->port, temp, UARTCTRL);
1818 }
1819 
1820 static void lpuart32_hw_setup(struct lpuart_port *sport)
1821 {
1822 	unsigned long flags;
1823 
1824 	spin_lock_irqsave(&sport->port.lock, flags);
1825 
1826 	lpuart32_hw_disable(sport);
1827 
1828 	lpuart_rx_dma_startup(sport);
1829 	lpuart_tx_dma_startup(sport);
1830 
1831 	lpuart32_setup_watermark_enable(sport);
1832 	lpuart32_configure(sport);
1833 
1834 	spin_unlock_irqrestore(&sport->port.lock, flags);
1835 }
1836 
1837 static int lpuart32_startup(struct uart_port *port)
1838 {
1839 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1840 	unsigned long temp;
1841 
1842 	/* determine FIFO size */
1843 	temp = lpuart32_read(&sport->port, UARTFIFO);
1844 
1845 	sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_TXSIZE_OFF) &
1846 					    UARTFIFO_FIFOSIZE_MASK);
1847 	sport->port.fifosize = sport->txfifo_size;
1848 
1849 	sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_RXSIZE_OFF) &
1850 					    UARTFIFO_FIFOSIZE_MASK);
1851 
1852 	/*
1853 	 * The LS1021A and LS1028A have a fixed FIFO depth of 16 words.
1854 	 * Although they support the RX/TXSIZE fields, their encoding is
1855 	 * different. Eg the reference manual states 0b101 is 16 words.
1856 	 */
1857 	if (is_layerscape_lpuart(sport)) {
1858 		sport->rxfifo_size = 16;
1859 		sport->txfifo_size = 16;
1860 		sport->port.fifosize = sport->txfifo_size;
1861 	}
1862 
1863 	lpuart_request_dma(sport);
1864 	lpuart32_hw_setup(sport);
1865 
1866 	return 0;
1867 }
1868 
1869 static void lpuart_dma_shutdown(struct lpuart_port *sport)
1870 {
1871 	if (sport->lpuart_dma_rx_use) {
1872 		lpuart_dma_rx_free(&sport->port);
1873 		sport->lpuart_dma_rx_use = false;
1874 	}
1875 
1876 	if (sport->lpuart_dma_tx_use) {
1877 		if (wait_event_interruptible_timeout(sport->dma_wait,
1878 			!sport->dma_tx_in_progress, msecs_to_jiffies(300)) <= 0) {
1879 			sport->dma_tx_in_progress = false;
1880 			dmaengine_terminate_sync(sport->dma_tx_chan);
1881 		}
1882 		sport->lpuart_dma_tx_use = false;
1883 	}
1884 
1885 	if (sport->dma_tx_chan)
1886 		dma_release_channel(sport->dma_tx_chan);
1887 	if (sport->dma_rx_chan)
1888 		dma_release_channel(sport->dma_rx_chan);
1889 }
1890 
1891 static void lpuart_shutdown(struct uart_port *port)
1892 {
1893 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1894 	unsigned char temp;
1895 	unsigned long flags;
1896 
1897 	spin_lock_irqsave(&port->lock, flags);
1898 
1899 	/* disable Rx/Tx and interrupts */
1900 	temp = readb(port->membase + UARTCR2);
1901 	temp &= ~(UARTCR2_TE | UARTCR2_RE |
1902 			UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1903 	writeb(temp, port->membase + UARTCR2);
1904 
1905 	spin_unlock_irqrestore(&port->lock, flags);
1906 
1907 	lpuart_dma_shutdown(sport);
1908 }
1909 
1910 static void lpuart32_shutdown(struct uart_port *port)
1911 {
1912 	struct lpuart_port *sport =
1913 		container_of(port, struct lpuart_port, port);
1914 	unsigned long temp;
1915 	unsigned long flags;
1916 
1917 	spin_lock_irqsave(&port->lock, flags);
1918 
1919 	/* clear status */
1920 	temp = lpuart32_read(&sport->port, UARTSTAT);
1921 	lpuart32_write(&sport->port, temp, UARTSTAT);
1922 
1923 	/* disable Rx/Tx DMA */
1924 	temp = lpuart32_read(port, UARTBAUD);
1925 	temp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
1926 	lpuart32_write(port, temp, UARTBAUD);
1927 
1928 	/* disable Rx/Tx and interrupts and break condition */
1929 	temp = lpuart32_read(port, UARTCTRL);
1930 	temp &= ~(UARTCTRL_TE | UARTCTRL_RE | UARTCTRL_ILIE |
1931 			UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE | UARTCTRL_SBK);
1932 	lpuart32_write(port, temp, UARTCTRL);
1933 
1934 	spin_unlock_irqrestore(&port->lock, flags);
1935 
1936 	lpuart_dma_shutdown(sport);
1937 }
1938 
1939 static void
1940 lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
1941 		   const struct ktermios *old)
1942 {
1943 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1944 	unsigned long flags;
1945 	unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
1946 	unsigned int  baud;
1947 	unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1948 	unsigned int sbr, brfa;
1949 
1950 	cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
1951 	old_cr2 = readb(sport->port.membase + UARTCR2);
1952 	cr3 = readb(sport->port.membase + UARTCR3);
1953 	cr4 = readb(sport->port.membase + UARTCR4);
1954 	bdh = readb(sport->port.membase + UARTBDH);
1955 	modem = readb(sport->port.membase + UARTMODEM);
1956 	/*
1957 	 * only support CS8 and CS7, and for CS7 must enable PE.
1958 	 * supported mode:
1959 	 *  - (7,e/o,1)
1960 	 *  - (8,n,1)
1961 	 *  - (8,m/s,1)
1962 	 *  - (8,e/o,1)
1963 	 */
1964 	while ((termios->c_cflag & CSIZE) != CS8 &&
1965 		(termios->c_cflag & CSIZE) != CS7) {
1966 		termios->c_cflag &= ~CSIZE;
1967 		termios->c_cflag |= old_csize;
1968 		old_csize = CS8;
1969 	}
1970 
1971 	if ((termios->c_cflag & CSIZE) == CS8 ||
1972 		(termios->c_cflag & CSIZE) == CS7)
1973 		cr1 = old_cr1 & ~UARTCR1_M;
1974 
1975 	if (termios->c_cflag & CMSPAR) {
1976 		if ((termios->c_cflag & CSIZE) != CS8) {
1977 			termios->c_cflag &= ~CSIZE;
1978 			termios->c_cflag |= CS8;
1979 		}
1980 		cr1 |= UARTCR1_M;
1981 	}
1982 
1983 	/*
1984 	 * When auto RS-485 RTS mode is enabled,
1985 	 * hardware flow control need to be disabled.
1986 	 */
1987 	if (sport->port.rs485.flags & SER_RS485_ENABLED)
1988 		termios->c_cflag &= ~CRTSCTS;
1989 
1990 	if (termios->c_cflag & CRTSCTS)
1991 		modem |= UARTMODEM_RXRTSE | UARTMODEM_TXCTSE;
1992 	else
1993 		modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1994 
1995 	termios->c_cflag &= ~CSTOPB;
1996 
1997 	/* parity must be enabled when CS7 to match 8-bits format */
1998 	if ((termios->c_cflag & CSIZE) == CS7)
1999 		termios->c_cflag |= PARENB;
2000 
2001 	if (termios->c_cflag & PARENB) {
2002 		if (termios->c_cflag & CMSPAR) {
2003 			cr1 &= ~UARTCR1_PE;
2004 			if (termios->c_cflag & PARODD)
2005 				cr3 |= UARTCR3_T8;
2006 			else
2007 				cr3 &= ~UARTCR3_T8;
2008 		} else {
2009 			cr1 |= UARTCR1_PE;
2010 			if ((termios->c_cflag & CSIZE) == CS8)
2011 				cr1 |= UARTCR1_M;
2012 			if (termios->c_cflag & PARODD)
2013 				cr1 |= UARTCR1_PT;
2014 			else
2015 				cr1 &= ~UARTCR1_PT;
2016 		}
2017 	} else {
2018 		cr1 &= ~UARTCR1_PE;
2019 	}
2020 
2021 	/* ask the core to calculate the divisor */
2022 	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
2023 
2024 	/*
2025 	 * Need to update the Ring buffer length according to the selected
2026 	 * baud rate and restart Rx DMA path.
2027 	 *
2028 	 * Since timer function acqures sport->port.lock, need to stop before
2029 	 * acquring same lock because otherwise del_timer_sync() can deadlock.
2030 	 */
2031 	if (old && sport->lpuart_dma_rx_use)
2032 		lpuart_dma_rx_free(&sport->port);
2033 
2034 	spin_lock_irqsave(&sport->port.lock, flags);
2035 
2036 	sport->port.read_status_mask = 0;
2037 	if (termios->c_iflag & INPCK)
2038 		sport->port.read_status_mask |= UARTSR1_FE | UARTSR1_PE;
2039 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2040 		sport->port.read_status_mask |= UARTSR1_FE;
2041 
2042 	/* characters to ignore */
2043 	sport->port.ignore_status_mask = 0;
2044 	if (termios->c_iflag & IGNPAR)
2045 		sport->port.ignore_status_mask |= UARTSR1_PE;
2046 	if (termios->c_iflag & IGNBRK) {
2047 		sport->port.ignore_status_mask |= UARTSR1_FE;
2048 		/*
2049 		 * if we're ignoring parity and break indicators,
2050 		 * ignore overruns too (for real raw support).
2051 		 */
2052 		if (termios->c_iflag & IGNPAR)
2053 			sport->port.ignore_status_mask |= UARTSR1_OR;
2054 	}
2055 
2056 	/* update the per-port timeout */
2057 	uart_update_timeout(port, termios->c_cflag, baud);
2058 
2059 	/* wait transmit engin complete */
2060 	lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
2061 
2062 	/* disable transmit and receive */
2063 	writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
2064 			sport->port.membase + UARTCR2);
2065 
2066 	sbr = sport->port.uartclk / (16 * baud);
2067 	brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
2068 	bdh &= ~UARTBDH_SBR_MASK;
2069 	bdh |= (sbr >> 8) & 0x1F;
2070 	cr4 &= ~UARTCR4_BRFA_MASK;
2071 	brfa &= UARTCR4_BRFA_MASK;
2072 	writeb(cr4 | brfa, sport->port.membase + UARTCR4);
2073 	writeb(bdh, sport->port.membase + UARTBDH);
2074 	writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
2075 	writeb(cr3, sport->port.membase + UARTCR3);
2076 	writeb(cr1, sport->port.membase + UARTCR1);
2077 	writeb(modem, sport->port.membase + UARTMODEM);
2078 
2079 	/* restore control register */
2080 	writeb(old_cr2, sport->port.membase + UARTCR2);
2081 
2082 	if (old && sport->lpuart_dma_rx_use) {
2083 		if (!lpuart_start_rx_dma(sport))
2084 			rx_dma_timer_init(sport);
2085 		else
2086 			sport->lpuart_dma_rx_use = false;
2087 	}
2088 
2089 	spin_unlock_irqrestore(&sport->port.lock, flags);
2090 }
2091 
2092 static void __lpuart32_serial_setbrg(struct uart_port *port,
2093 				     unsigned int baudrate, bool use_rx_dma,
2094 				     bool use_tx_dma)
2095 {
2096 	u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
2097 	u32 clk = port->uartclk;
2098 
2099 	/*
2100 	 * The idea is to use the best OSR (over-sampling rate) possible.
2101 	 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
2102 	 * Loop to find the best OSR value possible, one that generates minimum
2103 	 * baud_diff iterate through the rest of the supported values of OSR.
2104 	 *
2105 	 * Calculation Formula:
2106 	 *  Baud Rate = baud clock / ((OSR+1) × SBR)
2107 	 */
2108 	baud_diff = baudrate;
2109 	osr = 0;
2110 	sbr = 0;
2111 
2112 	for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
2113 		/* calculate the temporary sbr value  */
2114 		tmp_sbr = (clk / (baudrate * tmp_osr));
2115 		if (tmp_sbr == 0)
2116 			tmp_sbr = 1;
2117 
2118 		/*
2119 		 * calculate the baud rate difference based on the temporary
2120 		 * osr and sbr values
2121 		 */
2122 		tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
2123 
2124 		/* select best values between sbr and sbr+1 */
2125 		tmp = clk / (tmp_osr * (tmp_sbr + 1));
2126 		if (tmp_diff > (baudrate - tmp)) {
2127 			tmp_diff = baudrate - tmp;
2128 			tmp_sbr++;
2129 		}
2130 
2131 		if (tmp_sbr > UARTBAUD_SBR_MASK)
2132 			continue;
2133 
2134 		if (tmp_diff <= baud_diff) {
2135 			baud_diff = tmp_diff;
2136 			osr = tmp_osr;
2137 			sbr = tmp_sbr;
2138 
2139 			if (!baud_diff)
2140 				break;
2141 		}
2142 	}
2143 
2144 	/* handle buadrate outside acceptable rate */
2145 	if (baud_diff > ((baudrate / 100) * 3))
2146 		dev_warn(port->dev,
2147 			 "unacceptable baud rate difference of more than 3%%\n");
2148 
2149 	tmp = lpuart32_read(port, UARTBAUD);
2150 
2151 	if ((osr > 3) && (osr < 8))
2152 		tmp |= UARTBAUD_BOTHEDGE;
2153 
2154 	tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
2155 	tmp |= ((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT;
2156 
2157 	tmp &= ~UARTBAUD_SBR_MASK;
2158 	tmp |= sbr & UARTBAUD_SBR_MASK;
2159 
2160 	if (!use_rx_dma)
2161 		tmp &= ~UARTBAUD_RDMAE;
2162 	if (!use_tx_dma)
2163 		tmp &= ~UARTBAUD_TDMAE;
2164 
2165 	lpuart32_write(port, tmp, UARTBAUD);
2166 }
2167 
2168 static void lpuart32_serial_setbrg(struct lpuart_port *sport,
2169 				   unsigned int baudrate)
2170 {
2171 	__lpuart32_serial_setbrg(&sport->port, baudrate,
2172 				 sport->lpuart_dma_rx_use,
2173 				 sport->lpuart_dma_tx_use);
2174 }
2175 
2176 
2177 static void
2178 lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
2179 		     const struct ktermios *old)
2180 {
2181 	struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
2182 	unsigned long flags;
2183 	unsigned long ctrl, old_ctrl, bd, modem;
2184 	unsigned int  baud;
2185 	unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
2186 
2187 	ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
2188 	bd = lpuart32_read(&sport->port, UARTBAUD);
2189 	modem = lpuart32_read(&sport->port, UARTMODIR);
2190 	sport->is_cs7 = false;
2191 	/*
2192 	 * only support CS8 and CS7, and for CS7 must enable PE.
2193 	 * supported mode:
2194 	 *  - (7,e/o,1)
2195 	 *  - (8,n,1)
2196 	 *  - (8,m/s,1)
2197 	 *  - (8,e/o,1)
2198 	 */
2199 	while ((termios->c_cflag & CSIZE) != CS8 &&
2200 		(termios->c_cflag & CSIZE) != CS7) {
2201 		termios->c_cflag &= ~CSIZE;
2202 		termios->c_cflag |= old_csize;
2203 		old_csize = CS8;
2204 	}
2205 
2206 	if ((termios->c_cflag & CSIZE) == CS8 ||
2207 		(termios->c_cflag & CSIZE) == CS7)
2208 		ctrl = old_ctrl & ~UARTCTRL_M;
2209 
2210 	if (termios->c_cflag & CMSPAR) {
2211 		if ((termios->c_cflag & CSIZE) != CS8) {
2212 			termios->c_cflag &= ~CSIZE;
2213 			termios->c_cflag |= CS8;
2214 		}
2215 		ctrl |= UARTCTRL_M;
2216 	}
2217 
2218 	/*
2219 	 * When auto RS-485 RTS mode is enabled,
2220 	 * hardware flow control need to be disabled.
2221 	 */
2222 	if (sport->port.rs485.flags & SER_RS485_ENABLED)
2223 		termios->c_cflag &= ~CRTSCTS;
2224 
2225 	if (termios->c_cflag & CRTSCTS)
2226 		modem |= UARTMODIR_RXRTSE | UARTMODIR_TXCTSE;
2227 	else
2228 		modem &= ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
2229 
2230 	if (termios->c_cflag & CSTOPB)
2231 		bd |= UARTBAUD_SBNS;
2232 	else
2233 		bd &= ~UARTBAUD_SBNS;
2234 
2235 	/* parity must be enabled when CS7 to match 8-bits format */
2236 	if ((termios->c_cflag & CSIZE) == CS7)
2237 		termios->c_cflag |= PARENB;
2238 
2239 	if ((termios->c_cflag & PARENB)) {
2240 		if (termios->c_cflag & CMSPAR) {
2241 			ctrl &= ~UARTCTRL_PE;
2242 			ctrl |= UARTCTRL_M;
2243 		} else {
2244 			ctrl |= UARTCTRL_PE;
2245 			if ((termios->c_cflag & CSIZE) == CS8)
2246 				ctrl |= UARTCTRL_M;
2247 			if (termios->c_cflag & PARODD)
2248 				ctrl |= UARTCTRL_PT;
2249 			else
2250 				ctrl &= ~UARTCTRL_PT;
2251 		}
2252 	} else {
2253 		ctrl &= ~UARTCTRL_PE;
2254 	}
2255 
2256 	/* ask the core to calculate the divisor */
2257 	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
2258 
2259 	/*
2260 	 * Need to update the Ring buffer length according to the selected
2261 	 * baud rate and restart Rx DMA path.
2262 	 *
2263 	 * Since timer function acqures sport->port.lock, need to stop before
2264 	 * acquring same lock because otherwise del_timer_sync() can deadlock.
2265 	 */
2266 	if (old && sport->lpuart_dma_rx_use)
2267 		lpuart_dma_rx_free(&sport->port);
2268 
2269 	spin_lock_irqsave(&sport->port.lock, flags);
2270 
2271 	sport->port.read_status_mask = 0;
2272 	if (termios->c_iflag & INPCK)
2273 		sport->port.read_status_mask |= UARTSTAT_FE | UARTSTAT_PE;
2274 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2275 		sport->port.read_status_mask |= UARTSTAT_FE;
2276 
2277 	/* characters to ignore */
2278 	sport->port.ignore_status_mask = 0;
2279 	if (termios->c_iflag & IGNPAR)
2280 		sport->port.ignore_status_mask |= UARTSTAT_PE;
2281 	if (termios->c_iflag & IGNBRK) {
2282 		sport->port.ignore_status_mask |= UARTSTAT_FE;
2283 		/*
2284 		 * if we're ignoring parity and break indicators,
2285 		 * ignore overruns too (for real raw support).
2286 		 */
2287 		if (termios->c_iflag & IGNPAR)
2288 			sport->port.ignore_status_mask |= UARTSTAT_OR;
2289 	}
2290 
2291 	/* update the per-port timeout */
2292 	uart_update_timeout(port, termios->c_cflag, baud);
2293 
2294 	/*
2295 	 * LPUART Transmission Complete Flag may never be set while queuing a break
2296 	 * character, so skip waiting for transmission complete when UARTCTRL_SBK is
2297 	 * asserted.
2298 	 */
2299 	if (!(old_ctrl & UARTCTRL_SBK)) {
2300 		lpuart32_write(&sport->port, 0, UARTMODIR);
2301 		lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2302 	}
2303 
2304 	/* disable transmit and receive */
2305 	lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
2306 		       UARTCTRL);
2307 
2308 	lpuart32_write(&sport->port, bd, UARTBAUD);
2309 	lpuart32_serial_setbrg(sport, baud);
2310 	lpuart32_write(&sport->port, modem, UARTMODIR);
2311 	lpuart32_write(&sport->port, ctrl, UARTCTRL);
2312 	/* restore control register */
2313 
2314 	if ((ctrl & (UARTCTRL_PE | UARTCTRL_M)) == UARTCTRL_PE)
2315 		sport->is_cs7 = true;
2316 
2317 	if (old && sport->lpuart_dma_rx_use) {
2318 		if (!lpuart_start_rx_dma(sport))
2319 			rx_dma_timer_init(sport);
2320 		else
2321 			sport->lpuart_dma_rx_use = false;
2322 	}
2323 
2324 	spin_unlock_irqrestore(&sport->port.lock, flags);
2325 }
2326 
2327 static const char *lpuart_type(struct uart_port *port)
2328 {
2329 	return "FSL_LPUART";
2330 }
2331 
2332 static void lpuart_release_port(struct uart_port *port)
2333 {
2334 	/* nothing to do */
2335 }
2336 
2337 static int lpuart_request_port(struct uart_port *port)
2338 {
2339 	return  0;
2340 }
2341 
2342 /* configure/autoconfigure the port */
2343 static void lpuart_config_port(struct uart_port *port, int flags)
2344 {
2345 	if (flags & UART_CONFIG_TYPE)
2346 		port->type = PORT_LPUART;
2347 }
2348 
2349 static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
2350 {
2351 	int ret = 0;
2352 
2353 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
2354 		ret = -EINVAL;
2355 	if (port->irq != ser->irq)
2356 		ret = -EINVAL;
2357 	if (ser->io_type != UPIO_MEM)
2358 		ret = -EINVAL;
2359 	if (port->uartclk / 16 != ser->baud_base)
2360 		ret = -EINVAL;
2361 	if (port->iobase != ser->port)
2362 		ret = -EINVAL;
2363 	if (ser->hub6 != 0)
2364 		ret = -EINVAL;
2365 	return ret;
2366 }
2367 
2368 static const struct uart_ops lpuart_pops = {
2369 	.tx_empty	= lpuart_tx_empty,
2370 	.set_mctrl	= lpuart_set_mctrl,
2371 	.get_mctrl	= lpuart_get_mctrl,
2372 	.stop_tx	= lpuart_stop_tx,
2373 	.start_tx	= lpuart_start_tx,
2374 	.stop_rx	= lpuart_stop_rx,
2375 	.break_ctl	= lpuart_break_ctl,
2376 	.startup	= lpuart_startup,
2377 	.shutdown	= lpuart_shutdown,
2378 	.set_termios	= lpuart_set_termios,
2379 	.pm		= lpuart_uart_pm,
2380 	.type		= lpuart_type,
2381 	.request_port	= lpuart_request_port,
2382 	.release_port	= lpuart_release_port,
2383 	.config_port	= lpuart_config_port,
2384 	.verify_port	= lpuart_verify_port,
2385 	.flush_buffer	= lpuart_flush_buffer,
2386 #if defined(CONFIG_CONSOLE_POLL)
2387 	.poll_init	= lpuart_poll_init,
2388 	.poll_get_char	= lpuart_poll_get_char,
2389 	.poll_put_char	= lpuart_poll_put_char,
2390 #endif
2391 };
2392 
2393 static const struct uart_ops lpuart32_pops = {
2394 	.tx_empty	= lpuart32_tx_empty,
2395 	.set_mctrl	= lpuart32_set_mctrl,
2396 	.get_mctrl	= lpuart32_get_mctrl,
2397 	.stop_tx	= lpuart32_stop_tx,
2398 	.start_tx	= lpuart32_start_tx,
2399 	.stop_rx	= lpuart32_stop_rx,
2400 	.break_ctl	= lpuart32_break_ctl,
2401 	.startup	= lpuart32_startup,
2402 	.shutdown	= lpuart32_shutdown,
2403 	.set_termios	= lpuart32_set_termios,
2404 	.pm		= lpuart_uart_pm,
2405 	.type		= lpuart_type,
2406 	.request_port	= lpuart_request_port,
2407 	.release_port	= lpuart_release_port,
2408 	.config_port	= lpuart_config_port,
2409 	.verify_port	= lpuart_verify_port,
2410 	.flush_buffer	= lpuart_flush_buffer,
2411 #if defined(CONFIG_CONSOLE_POLL)
2412 	.poll_init	= lpuart32_poll_init,
2413 	.poll_get_char	= lpuart32_poll_get_char,
2414 	.poll_put_char	= lpuart32_poll_put_char,
2415 #endif
2416 };
2417 
2418 static struct lpuart_port *lpuart_ports[UART_NR];
2419 
2420 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
2421 static void lpuart_console_putchar(struct uart_port *port, unsigned char ch)
2422 {
2423 	lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
2424 	writeb(ch, port->membase + UARTDR);
2425 }
2426 
2427 static void lpuart32_console_putchar(struct uart_port *port, unsigned char ch)
2428 {
2429 	lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
2430 	lpuart32_write(port, ch, UARTDATA);
2431 }
2432 
2433 static void
2434 lpuart_console_write(struct console *co, const char *s, unsigned int count)
2435 {
2436 	struct lpuart_port *sport = lpuart_ports[co->index];
2437 	unsigned char  old_cr2, cr2;
2438 	unsigned long flags;
2439 	int locked = 1;
2440 
2441 	if (oops_in_progress)
2442 		locked = spin_trylock_irqsave(&sport->port.lock, flags);
2443 	else
2444 		spin_lock_irqsave(&sport->port.lock, flags);
2445 
2446 	/* first save CR2 and then disable interrupts */
2447 	cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
2448 	cr2 |= UARTCR2_TE | UARTCR2_RE;
2449 	cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
2450 	writeb(cr2, sport->port.membase + UARTCR2);
2451 
2452 	uart_console_write(&sport->port, s, count, lpuart_console_putchar);
2453 
2454 	/* wait for transmitter finish complete and restore CR2 */
2455 	lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
2456 
2457 	writeb(old_cr2, sport->port.membase + UARTCR2);
2458 
2459 	if (locked)
2460 		spin_unlock_irqrestore(&sport->port.lock, flags);
2461 }
2462 
2463 static void
2464 lpuart32_console_write(struct console *co, const char *s, unsigned int count)
2465 {
2466 	struct lpuart_port *sport = lpuart_ports[co->index];
2467 	unsigned long  old_cr, cr;
2468 	unsigned long flags;
2469 	int locked = 1;
2470 
2471 	if (oops_in_progress)
2472 		locked = spin_trylock_irqsave(&sport->port.lock, flags);
2473 	else
2474 		spin_lock_irqsave(&sport->port.lock, flags);
2475 
2476 	/* first save CR2 and then disable interrupts */
2477 	cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
2478 	cr |= UARTCTRL_TE | UARTCTRL_RE;
2479 	cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
2480 	lpuart32_write(&sport->port, cr, UARTCTRL);
2481 
2482 	uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
2483 
2484 	/* wait for transmitter finish complete and restore CR2 */
2485 	lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2486 
2487 	lpuart32_write(&sport->port, old_cr, UARTCTRL);
2488 
2489 	if (locked)
2490 		spin_unlock_irqrestore(&sport->port.lock, flags);
2491 }
2492 
2493 /*
2494  * if the port was already initialised (eg, by a boot loader),
2495  * try to determine the current setup.
2496  */
2497 static void __init
2498 lpuart_console_get_options(struct lpuart_port *sport, int *baud,
2499 			   int *parity, int *bits)
2500 {
2501 	unsigned char cr, bdh, bdl, brfa;
2502 	unsigned int sbr, uartclk, baud_raw;
2503 
2504 	cr = readb(sport->port.membase + UARTCR2);
2505 	cr &= UARTCR2_TE | UARTCR2_RE;
2506 	if (!cr)
2507 		return;
2508 
2509 	/* ok, the port was enabled */
2510 
2511 	cr = readb(sport->port.membase + UARTCR1);
2512 
2513 	*parity = 'n';
2514 	if (cr & UARTCR1_PE) {
2515 		if (cr & UARTCR1_PT)
2516 			*parity = 'o';
2517 		else
2518 			*parity = 'e';
2519 	}
2520 
2521 	if (cr & UARTCR1_M)
2522 		*bits = 9;
2523 	else
2524 		*bits = 8;
2525 
2526 	bdh = readb(sport->port.membase + UARTBDH);
2527 	bdh &= UARTBDH_SBR_MASK;
2528 	bdl = readb(sport->port.membase + UARTBDL);
2529 	sbr = bdh;
2530 	sbr <<= 8;
2531 	sbr |= bdl;
2532 	brfa = readb(sport->port.membase + UARTCR4);
2533 	brfa &= UARTCR4_BRFA_MASK;
2534 
2535 	uartclk = lpuart_get_baud_clk_rate(sport);
2536 	/*
2537 	 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2538 	 */
2539 	baud_raw = uartclk / (16 * (sbr + brfa / 32));
2540 
2541 	if (*baud != baud_raw)
2542 		dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2543 				"from %d to %d\n", baud_raw, *baud);
2544 }
2545 
2546 static void __init
2547 lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
2548 			   int *parity, int *bits)
2549 {
2550 	unsigned long cr, bd;
2551 	unsigned int sbr, uartclk, baud_raw;
2552 
2553 	cr = lpuart32_read(&sport->port, UARTCTRL);
2554 	cr &= UARTCTRL_TE | UARTCTRL_RE;
2555 	if (!cr)
2556 		return;
2557 
2558 	/* ok, the port was enabled */
2559 
2560 	cr = lpuart32_read(&sport->port, UARTCTRL);
2561 
2562 	*parity = 'n';
2563 	if (cr & UARTCTRL_PE) {
2564 		if (cr & UARTCTRL_PT)
2565 			*parity = 'o';
2566 		else
2567 			*parity = 'e';
2568 	}
2569 
2570 	if (cr & UARTCTRL_M)
2571 		*bits = 9;
2572 	else
2573 		*bits = 8;
2574 
2575 	bd = lpuart32_read(&sport->port, UARTBAUD);
2576 	bd &= UARTBAUD_SBR_MASK;
2577 	if (!bd)
2578 		return;
2579 
2580 	sbr = bd;
2581 	uartclk = lpuart_get_baud_clk_rate(sport);
2582 	/*
2583 	 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2584 	 */
2585 	baud_raw = uartclk / (16 * sbr);
2586 
2587 	if (*baud != baud_raw)
2588 		dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2589 				"from %d to %d\n", baud_raw, *baud);
2590 }
2591 
2592 static int __init lpuart_console_setup(struct console *co, char *options)
2593 {
2594 	struct lpuart_port *sport;
2595 	int baud = 115200;
2596 	int bits = 8;
2597 	int parity = 'n';
2598 	int flow = 'n';
2599 
2600 	/*
2601 	 * check whether an invalid uart number has been specified, and
2602 	 * if so, search for the first available port that does have
2603 	 * console support.
2604 	 */
2605 	if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
2606 		co->index = 0;
2607 
2608 	sport = lpuart_ports[co->index];
2609 	if (sport == NULL)
2610 		return -ENODEV;
2611 
2612 	if (options)
2613 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2614 	else
2615 		if (lpuart_is_32(sport))
2616 			lpuart32_console_get_options(sport, &baud, &parity, &bits);
2617 		else
2618 			lpuart_console_get_options(sport, &baud, &parity, &bits);
2619 
2620 	if (lpuart_is_32(sport))
2621 		lpuart32_setup_watermark(sport);
2622 	else
2623 		lpuart_setup_watermark(sport);
2624 
2625 	return uart_set_options(&sport->port, co, baud, parity, bits, flow);
2626 }
2627 
2628 static struct uart_driver lpuart_reg;
2629 static struct console lpuart_console = {
2630 	.name		= DEV_NAME,
2631 	.write		= lpuart_console_write,
2632 	.device		= uart_console_device,
2633 	.setup		= lpuart_console_setup,
2634 	.flags		= CON_PRINTBUFFER,
2635 	.index		= -1,
2636 	.data		= &lpuart_reg,
2637 };
2638 
2639 static struct console lpuart32_console = {
2640 	.name		= DEV_NAME,
2641 	.write		= lpuart32_console_write,
2642 	.device		= uart_console_device,
2643 	.setup		= lpuart_console_setup,
2644 	.flags		= CON_PRINTBUFFER,
2645 	.index		= -1,
2646 	.data		= &lpuart_reg,
2647 };
2648 
2649 static void lpuart_early_write(struct console *con, const char *s, unsigned n)
2650 {
2651 	struct earlycon_device *dev = con->data;
2652 
2653 	uart_console_write(&dev->port, s, n, lpuart_console_putchar);
2654 }
2655 
2656 static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
2657 {
2658 	struct earlycon_device *dev = con->data;
2659 
2660 	uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
2661 }
2662 
2663 static int __init lpuart_early_console_setup(struct earlycon_device *device,
2664 					  const char *opt)
2665 {
2666 	if (!device->port.membase)
2667 		return -ENODEV;
2668 
2669 	device->con->write = lpuart_early_write;
2670 	return 0;
2671 }
2672 
2673 static int __init lpuart32_early_console_setup(struct earlycon_device *device,
2674 					  const char *opt)
2675 {
2676 	if (!device->port.membase)
2677 		return -ENODEV;
2678 
2679 	if (device->port.iotype != UPIO_MEM32)
2680 		device->port.iotype = UPIO_MEM32BE;
2681 
2682 	device->con->write = lpuart32_early_write;
2683 	return 0;
2684 }
2685 
2686 static int __init ls1028a_early_console_setup(struct earlycon_device *device,
2687 					      const char *opt)
2688 {
2689 	u32 cr;
2690 
2691 	if (!device->port.membase)
2692 		return -ENODEV;
2693 
2694 	device->port.iotype = UPIO_MEM32;
2695 	device->con->write = lpuart32_early_write;
2696 
2697 	/* set the baudrate */
2698 	if (device->port.uartclk && device->baud)
2699 		__lpuart32_serial_setbrg(&device->port, device->baud,
2700 					 false, false);
2701 
2702 	/* enable transmitter */
2703 	cr = lpuart32_read(&device->port, UARTCTRL);
2704 	cr |= UARTCTRL_TE;
2705 	lpuart32_write(&device->port, cr, UARTCTRL);
2706 
2707 	return 0;
2708 }
2709 
2710 static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
2711 						   const char *opt)
2712 {
2713 	if (!device->port.membase)
2714 		return -ENODEV;
2715 
2716 	device->port.iotype = UPIO_MEM32;
2717 	device->port.membase += IMX_REG_OFF;
2718 	device->con->write = lpuart32_early_write;
2719 
2720 	return 0;
2721 }
2722 OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
2723 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2724 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1028a-lpuart", ls1028a_early_console_setup);
2725 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2726 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8ulp-lpuart", lpuart32_imx_early_console_setup);
2727 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8qxp-lpuart", lpuart32_imx_early_console_setup);
2728 OF_EARLYCON_DECLARE(lpuart32, "fsl,imxrt1050-lpuart", lpuart32_imx_early_console_setup);
2729 EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
2730 EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2731 
2732 #define LPUART_CONSOLE	(&lpuart_console)
2733 #define LPUART32_CONSOLE	(&lpuart32_console)
2734 #else
2735 #define LPUART_CONSOLE	NULL
2736 #define LPUART32_CONSOLE	NULL
2737 #endif
2738 
2739 static struct uart_driver lpuart_reg = {
2740 	.owner		= THIS_MODULE,
2741 	.driver_name	= DRIVER_NAME,
2742 	.dev_name	= DEV_NAME,
2743 	.nr		= ARRAY_SIZE(lpuart_ports),
2744 	.cons		= LPUART_CONSOLE,
2745 };
2746 
2747 static const struct serial_rs485 lpuart_rs485_supported = {
2748 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
2749 	/* delay_rts_* and RX_DURING_TX are not supported */
2750 };
2751 
2752 static int lpuart_global_reset(struct lpuart_port *sport)
2753 {
2754 	struct uart_port *port = &sport->port;
2755 	void __iomem *global_addr;
2756 	unsigned long ctrl, bd;
2757 	unsigned int val = 0;
2758 	int ret;
2759 
2760 	ret = clk_prepare_enable(sport->ipg_clk);
2761 	if (ret) {
2762 		dev_err(sport->port.dev, "failed to enable uart ipg clk: %d\n", ret);
2763 		return ret;
2764 	}
2765 
2766 	if (is_imx7ulp_lpuart(sport) || is_imx8ulp_lpuart(sport) || is_imx8qxp_lpuart(sport)) {
2767 		/*
2768 		 * If the transmitter is used by earlycon, wait for transmit engine to
2769 		 * complete and then reset.
2770 		 */
2771 		ctrl = lpuart32_read(port, UARTCTRL);
2772 		if (ctrl & UARTCTRL_TE) {
2773 			bd = lpuart32_read(&sport->port, UARTBAUD);
2774 			if (read_poll_timeout(lpuart32_tx_empty, val, val, 1, 100000, false,
2775 					      port)) {
2776 				dev_warn(sport->port.dev,
2777 					 "timeout waiting for transmit engine to complete\n");
2778 				clk_disable_unprepare(sport->ipg_clk);
2779 				return 0;
2780 			}
2781 		}
2782 
2783 		global_addr = port->membase + UART_GLOBAL - IMX_REG_OFF;
2784 		writel(UART_GLOBAL_RST, global_addr);
2785 		usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
2786 		writel(0, global_addr);
2787 		usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
2788 
2789 		/* Recover the transmitter for earlycon. */
2790 		if (ctrl & UARTCTRL_TE) {
2791 			lpuart32_write(port, bd, UARTBAUD);
2792 			lpuart32_write(port, ctrl, UARTCTRL);
2793 		}
2794 	}
2795 
2796 	clk_disable_unprepare(sport->ipg_clk);
2797 	return 0;
2798 }
2799 
2800 static int lpuart_probe(struct platform_device *pdev)
2801 {
2802 	const struct lpuart_soc_data *sdata = of_device_get_match_data(&pdev->dev);
2803 	struct device_node *np = pdev->dev.of_node;
2804 	struct lpuart_port *sport;
2805 	struct resource *res;
2806 	irq_handler_t handler;
2807 	int ret;
2808 
2809 	sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2810 	if (!sport)
2811 		return -ENOMEM;
2812 
2813 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2814 	sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
2815 	if (IS_ERR(sport->port.membase))
2816 		return PTR_ERR(sport->port.membase);
2817 
2818 	sport->port.membase += sdata->reg_off;
2819 	sport->port.mapbase = res->start + sdata->reg_off;
2820 	sport->port.dev = &pdev->dev;
2821 	sport->port.type = PORT_LPUART;
2822 	sport->devtype = sdata->devtype;
2823 	sport->rx_watermark = sdata->rx_watermark;
2824 	ret = platform_get_irq(pdev, 0);
2825 	if (ret < 0)
2826 		return ret;
2827 	sport->port.irq = ret;
2828 	sport->port.iotype = sdata->iotype;
2829 	if (lpuart_is_32(sport))
2830 		sport->port.ops = &lpuart32_pops;
2831 	else
2832 		sport->port.ops = &lpuart_pops;
2833 	sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_FSL_LPUART_CONSOLE);
2834 	sport->port.flags = UPF_BOOT_AUTOCONF;
2835 
2836 	if (lpuart_is_32(sport))
2837 		sport->port.rs485_config = lpuart32_config_rs485;
2838 	else
2839 		sport->port.rs485_config = lpuart_config_rs485;
2840 	sport->port.rs485_supported = lpuart_rs485_supported;
2841 
2842 	sport->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
2843 	if (IS_ERR(sport->ipg_clk)) {
2844 		ret = PTR_ERR(sport->ipg_clk);
2845 		dev_err(&pdev->dev, "failed to get uart ipg clk: %d\n", ret);
2846 		return ret;
2847 	}
2848 
2849 	sport->baud_clk = NULL;
2850 	if (is_imx8qxp_lpuart(sport)) {
2851 		sport->baud_clk = devm_clk_get(&pdev->dev, "baud");
2852 		if (IS_ERR(sport->baud_clk)) {
2853 			ret = PTR_ERR(sport->baud_clk);
2854 			dev_err(&pdev->dev, "failed to get uart baud clk: %d\n", ret);
2855 			return ret;
2856 		}
2857 	}
2858 
2859 	ret = of_alias_get_id(np, "serial");
2860 	if (ret < 0) {
2861 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2862 		return ret;
2863 	}
2864 	if (ret >= ARRAY_SIZE(lpuart_ports)) {
2865 		dev_err(&pdev->dev, "serial%d out of range\n", ret);
2866 		return -EINVAL;
2867 	}
2868 	sport->port.line = ret;
2869 
2870 	ret = lpuart_enable_clks(sport);
2871 	if (ret)
2872 		return ret;
2873 	sport->port.uartclk = lpuart_get_baud_clk_rate(sport);
2874 
2875 	lpuart_ports[sport->port.line] = sport;
2876 
2877 	platform_set_drvdata(pdev, &sport->port);
2878 
2879 	if (lpuart_is_32(sport)) {
2880 		lpuart_reg.cons = LPUART32_CONSOLE;
2881 		handler = lpuart32_int;
2882 	} else {
2883 		lpuart_reg.cons = LPUART_CONSOLE;
2884 		handler = lpuart_int;
2885 	}
2886 
2887 	pm_runtime_use_autosuspend(&pdev->dev);
2888 	pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
2889 	pm_runtime_set_active(&pdev->dev);
2890 	pm_runtime_enable(&pdev->dev);
2891 
2892 	ret = lpuart_global_reset(sport);
2893 	if (ret)
2894 		goto failed_reset;
2895 
2896 	ret = uart_get_rs485_mode(&sport->port);
2897 	if (ret)
2898 		goto failed_get_rs485;
2899 
2900 	ret = uart_add_one_port(&lpuart_reg, &sport->port);
2901 	if (ret)
2902 		goto failed_attach_port;
2903 
2904 	ret = devm_request_irq(&pdev->dev, sport->port.irq, handler, 0,
2905 				DRIVER_NAME, sport);
2906 	if (ret)
2907 		goto failed_irq_request;
2908 
2909 	return 0;
2910 
2911 failed_irq_request:
2912 	uart_remove_one_port(&lpuart_reg, &sport->port);
2913 failed_attach_port:
2914 failed_get_rs485:
2915 failed_reset:
2916 	pm_runtime_disable(&pdev->dev);
2917 	pm_runtime_set_suspended(&pdev->dev);
2918 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2919 	lpuart_disable_clks(sport);
2920 	return ret;
2921 }
2922 
2923 static int lpuart_remove(struct platform_device *pdev)
2924 {
2925 	struct lpuart_port *sport = platform_get_drvdata(pdev);
2926 
2927 	uart_remove_one_port(&lpuart_reg, &sport->port);
2928 
2929 	lpuart_disable_clks(sport);
2930 
2931 	if (sport->dma_tx_chan)
2932 		dma_release_channel(sport->dma_tx_chan);
2933 
2934 	if (sport->dma_rx_chan)
2935 		dma_release_channel(sport->dma_rx_chan);
2936 
2937 	pm_runtime_disable(&pdev->dev);
2938 	pm_runtime_set_suspended(&pdev->dev);
2939 	pm_runtime_dont_use_autosuspend(&pdev->dev);
2940 	return 0;
2941 }
2942 
2943 static int lpuart_runtime_suspend(struct device *dev)
2944 {
2945 	struct platform_device *pdev = to_platform_device(dev);
2946 	struct lpuart_port *sport = platform_get_drvdata(pdev);
2947 
2948 	lpuart_disable_clks(sport);
2949 
2950 	return 0;
2951 };
2952 
2953 static int lpuart_runtime_resume(struct device *dev)
2954 {
2955 	struct platform_device *pdev = to_platform_device(dev);
2956 	struct lpuart_port *sport = platform_get_drvdata(pdev);
2957 
2958 	return lpuart_enable_clks(sport);
2959 };
2960 
2961 static void serial_lpuart_enable_wakeup(struct lpuart_port *sport, bool on)
2962 {
2963 	unsigned int val, baud;
2964 
2965 	if (lpuart_is_32(sport)) {
2966 		val = lpuart32_read(&sport->port, UARTCTRL);
2967 		baud = lpuart32_read(&sport->port, UARTBAUD);
2968 		if (on) {
2969 			/* set rx_watermark to 0 in wakeup source mode */
2970 			lpuart32_write(&sport->port, 0, UARTWATER);
2971 			val |= UARTCTRL_RIE;
2972 			/* clear RXEDGIF flag before enable RXEDGIE interrupt */
2973 			lpuart32_write(&sport->port, UARTSTAT_RXEDGIF, UARTSTAT);
2974 			baud |= UARTBAUD_RXEDGIE;
2975 		} else {
2976 			val &= ~UARTCTRL_RIE;
2977 			baud &= ~UARTBAUD_RXEDGIE;
2978 		}
2979 		lpuart32_write(&sport->port, val, UARTCTRL);
2980 		lpuart32_write(&sport->port, baud, UARTBAUD);
2981 	} else {
2982 		val = readb(sport->port.membase + UARTCR2);
2983 		if (on)
2984 			val |= UARTCR2_RIE;
2985 		else
2986 			val &= ~UARTCR2_RIE;
2987 		writeb(val, sport->port.membase + UARTCR2);
2988 	}
2989 }
2990 
2991 static bool lpuart_uport_is_active(struct lpuart_port *sport)
2992 {
2993 	struct tty_port *port = &sport->port.state->port;
2994 	struct tty_struct *tty;
2995 	struct device *tty_dev;
2996 	int may_wake = 0;
2997 
2998 	tty = tty_port_tty_get(port);
2999 	if (tty) {
3000 		tty_dev = tty->dev;
3001 		may_wake = tty_dev && device_may_wakeup(tty_dev);
3002 		tty_kref_put(tty);
3003 	}
3004 
3005 	if ((tty_port_initialized(port) && may_wake) ||
3006 	    (!console_suspend_enabled && uart_console(&sport->port)))
3007 		return true;
3008 
3009 	return false;
3010 }
3011 
3012 static int lpuart_suspend_noirq(struct device *dev)
3013 {
3014 	struct lpuart_port *sport = dev_get_drvdata(dev);
3015 	bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
3016 
3017 	if (lpuart_uport_is_active(sport))
3018 		serial_lpuart_enable_wakeup(sport, !!irq_wake);
3019 
3020 	pinctrl_pm_select_sleep_state(dev);
3021 
3022 	return 0;
3023 }
3024 
3025 static int lpuart_resume_noirq(struct device *dev)
3026 {
3027 	struct lpuart_port *sport = dev_get_drvdata(dev);
3028 	unsigned int val;
3029 
3030 	pinctrl_pm_select_default_state(dev);
3031 
3032 	if (lpuart_uport_is_active(sport)) {
3033 		serial_lpuart_enable_wakeup(sport, false);
3034 
3035 		/* clear the wakeup flags */
3036 		if (lpuart_is_32(sport)) {
3037 			val = lpuart32_read(&sport->port, UARTSTAT);
3038 			lpuart32_write(&sport->port, val, UARTSTAT);
3039 		}
3040 	}
3041 
3042 	return 0;
3043 }
3044 
3045 static int lpuart_suspend(struct device *dev)
3046 {
3047 	struct lpuart_port *sport = dev_get_drvdata(dev);
3048 	unsigned long temp, flags;
3049 
3050 	uart_suspend_port(&lpuart_reg, &sport->port);
3051 
3052 	if (lpuart_uport_is_active(sport)) {
3053 		spin_lock_irqsave(&sport->port.lock, flags);
3054 		if (lpuart_is_32(sport)) {
3055 			/* disable Rx/Tx and interrupts */
3056 			temp = lpuart32_read(&sport->port, UARTCTRL);
3057 			temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
3058 			lpuart32_write(&sport->port, temp, UARTCTRL);
3059 		} else {
3060 			/* disable Rx/Tx and interrupts */
3061 			temp = readb(sport->port.membase + UARTCR2);
3062 			temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
3063 			writeb(temp, sport->port.membase + UARTCR2);
3064 		}
3065 		spin_unlock_irqrestore(&sport->port.lock, flags);
3066 
3067 		if (sport->lpuart_dma_rx_use) {
3068 			/*
3069 			 * EDMA driver during suspend will forcefully release any
3070 			 * non-idle DMA channels. If port wakeup is enabled or if port
3071 			 * is console port or 'no_console_suspend' is set the Rx DMA
3072 			 * cannot resume as expected, hence gracefully release the
3073 			 * Rx DMA path before suspend and start Rx DMA path on resume.
3074 			 */
3075 			lpuart_dma_rx_free(&sport->port);
3076 
3077 			/* Disable Rx DMA to use UART port as wakeup source */
3078 			spin_lock_irqsave(&sport->port.lock, flags);
3079 			if (lpuart_is_32(sport)) {
3080 				temp = lpuart32_read(&sport->port, UARTBAUD);
3081 				lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
3082 					       UARTBAUD);
3083 			} else {
3084 				writeb(readb(sport->port.membase + UARTCR5) &
3085 				       ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
3086 			}
3087 			spin_unlock_irqrestore(&sport->port.lock, flags);
3088 		}
3089 
3090 		if (sport->lpuart_dma_tx_use) {
3091 			spin_lock_irqsave(&sport->port.lock, flags);
3092 			if (lpuart_is_32(sport)) {
3093 				temp = lpuart32_read(&sport->port, UARTBAUD);
3094 				temp &= ~UARTBAUD_TDMAE;
3095 				lpuart32_write(&sport->port, temp, UARTBAUD);
3096 			} else {
3097 				temp = readb(sport->port.membase + UARTCR5);
3098 				temp &= ~UARTCR5_TDMAS;
3099 				writeb(temp, sport->port.membase + UARTCR5);
3100 			}
3101 			spin_unlock_irqrestore(&sport->port.lock, flags);
3102 			sport->dma_tx_in_progress = false;
3103 			dmaengine_terminate_sync(sport->dma_tx_chan);
3104 		}
3105 	} else if (pm_runtime_active(sport->port.dev)) {
3106 		lpuart_disable_clks(sport);
3107 		pm_runtime_disable(sport->port.dev);
3108 		pm_runtime_set_suspended(sport->port.dev);
3109 	}
3110 
3111 	return 0;
3112 }
3113 
3114 static void lpuart_console_fixup(struct lpuart_port *sport)
3115 {
3116 	struct tty_port *port = &sport->port.state->port;
3117 	struct uart_port *uport = &sport->port;
3118 	struct ktermios termios;
3119 
3120 	/* i.MX7ULP enter VLLS mode that lpuart module power off and registers
3121 	 * all lost no matter the port is wakeup source.
3122 	 * For console port, console baud rate setting lost and print messy
3123 	 * log when enable the console port as wakeup source. To avoid the
3124 	 * issue happen, user should not enable uart port as wakeup source
3125 	 * in VLLS mode, or restore console setting here.
3126 	 */
3127 	if (is_imx7ulp_lpuart(sport) && lpuart_uport_is_active(sport) &&
3128 	    console_suspend_enabled && uart_console(&sport->port)) {
3129 
3130 		mutex_lock(&port->mutex);
3131 		memset(&termios, 0, sizeof(struct ktermios));
3132 		termios.c_cflag = uport->cons->cflag;
3133 		if (port->tty && termios.c_cflag == 0)
3134 			termios = port->tty->termios;
3135 		uport->ops->set_termios(uport, &termios, NULL);
3136 		mutex_unlock(&port->mutex);
3137 	}
3138 }
3139 
3140 static int lpuart_resume(struct device *dev)
3141 {
3142 	struct lpuart_port *sport = dev_get_drvdata(dev);
3143 	int ret;
3144 
3145 	if (lpuart_uport_is_active(sport)) {
3146 		if (lpuart_is_32(sport))
3147 			lpuart32_hw_setup(sport);
3148 		else
3149 			lpuart_hw_setup(sport);
3150 	} else if (pm_runtime_active(sport->port.dev)) {
3151 		ret = lpuart_enable_clks(sport);
3152 		if (ret)
3153 			return ret;
3154 		pm_runtime_set_active(sport->port.dev);
3155 		pm_runtime_enable(sport->port.dev);
3156 	}
3157 
3158 	lpuart_console_fixup(sport);
3159 	uart_resume_port(&lpuart_reg, &sport->port);
3160 
3161 	return 0;
3162 }
3163 
3164 static const struct dev_pm_ops lpuart_pm_ops = {
3165 	RUNTIME_PM_OPS(lpuart_runtime_suspend,
3166 			   lpuart_runtime_resume, NULL)
3167 	NOIRQ_SYSTEM_SLEEP_PM_OPS(lpuart_suspend_noirq,
3168 				      lpuart_resume_noirq)
3169 	SYSTEM_SLEEP_PM_OPS(lpuart_suspend, lpuart_resume)
3170 };
3171 
3172 static struct platform_driver lpuart_driver = {
3173 	.probe		= lpuart_probe,
3174 	.remove		= lpuart_remove,
3175 	.driver		= {
3176 		.name	= "fsl-lpuart",
3177 		.of_match_table = lpuart_dt_ids,
3178 		.pm	= pm_ptr(&lpuart_pm_ops),
3179 	},
3180 };
3181 
3182 static int __init lpuart_serial_init(void)
3183 {
3184 	int ret = uart_register_driver(&lpuart_reg);
3185 
3186 	if (ret)
3187 		return ret;
3188 
3189 	ret = platform_driver_register(&lpuart_driver);
3190 	if (ret)
3191 		uart_unregister_driver(&lpuart_reg);
3192 
3193 	return ret;
3194 }
3195 
3196 static void __exit lpuart_serial_exit(void)
3197 {
3198 	platform_driver_unregister(&lpuart_driver);
3199 	uart_unregister_driver(&lpuart_reg);
3200 }
3201 
3202 module_init(lpuart_serial_init);
3203 module_exit(lpuart_serial_exit);
3204 
3205 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
3206 MODULE_LICENSE("GPL v2");
3207