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