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