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