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