1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale lpuart serial port driver
5 * Copyright 2012-2014 Freescale Semiconductor, Inc.
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>
17 #include <linux/iopoll.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/of_dma.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/serial_core.h>
26 #include <linux/slab.h>
27 #include <linux/tty_flip.h>
29 /* All registers are 8-bit width */
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
47 #define UARTBDH_LBKDIE 0x80
48 #define UARTBDH_RXEDGIE 0x40
49 #define UARTBDH_SBR_MASK 0x1f
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
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
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
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
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
92 #define UARTCR5_TDMAS 0x80
93 #define UARTCR5_RDMAS 0x20
95 #define UARTMODEM_RXRTSE 0x08
96 #define UARTMODEM_TXRTSPOL 0x04
97 #define UARTMODEM_TXRTSE 0x02
98 #define UARTMODEM_TXCTSE 0x01
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
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
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
118 /* 32-bit global registers only for i.MX7ULP/i.MX8x
119 * Used to reset all internal logic and registers, except the Global Register.
121 #define UART_GLOBAL 0x8
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
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
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
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
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
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
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))
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
235 #define UART_GLOBAL_RST 0x2
236 #define GLOBAL_RST_MIN_US 20
237 #define GLOBAL_RST_MAX_US 40
239 /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
240 #define DMA_RX_TIMEOUT (10)
241 #define UART_AUTOSUSPEND_TIMEOUT 3000
243 #define DRIVER_NAME "fsl-lpuart"
244 #define DEV_NAME "ttyLP"
247 /* IMX lpuart has four extra unused regs located at the beginning */
248 #define IMX_REG_OFF 0x10
261 struct uart_port port;
262 enum lpuart_type devtype;
264 struct clk *baud_clk;
265 unsigned int txfifo_size;
266 unsigned int rxfifo_size;
269 bool lpuart_dma_tx_use;
270 bool lpuart_dma_rx_use;
271 struct dma_chan *dma_tx_chan;
272 struct dma_chan *dma_rx_chan;
273 struct dma_async_tx_descriptor *dma_tx_desc;
274 struct dma_async_tx_descriptor *dma_rx_desc;
275 dma_cookie_t dma_tx_cookie;
276 dma_cookie_t dma_rx_cookie;
277 unsigned int dma_tx_bytes;
278 unsigned int dma_rx_bytes;
279 bool dma_tx_in_progress;
280 unsigned int dma_rx_timeout;
281 struct timer_list lpuart_timer;
282 struct scatterlist rx_sgl, tx_sgl[2];
283 struct circ_buf rx_ring;
284 int rx_dma_rng_buf_len;
285 unsigned int dma_tx_nents;
286 wait_queue_head_t dma_wait;
287 bool is_cs7; /* Set to true when character size is 7 */
288 /* and the parity is enabled */
291 struct lpuart_soc_data {
292 enum lpuart_type devtype;
298 static const struct lpuart_soc_data vf_data = {
299 .devtype = VF610_LPUART,
304 static const struct lpuart_soc_data ls1021a_data = {
305 .devtype = LS1021A_LPUART,
306 .iotype = UPIO_MEM32BE,
310 static const struct lpuart_soc_data ls1028a_data = {
311 .devtype = LS1028A_LPUART,
312 .iotype = UPIO_MEM32,
316 static struct lpuart_soc_data imx7ulp_data = {
317 .devtype = IMX7ULP_LPUART,
318 .iotype = UPIO_MEM32,
319 .reg_off = IMX_REG_OFF,
323 static struct lpuart_soc_data imx8ulp_data = {
324 .devtype = IMX8ULP_LPUART,
325 .iotype = UPIO_MEM32,
326 .reg_off = IMX_REG_OFF,
330 static struct lpuart_soc_data imx8qxp_data = {
331 .devtype = IMX8QXP_LPUART,
332 .iotype = UPIO_MEM32,
333 .reg_off = IMX_REG_OFF,
336 static struct lpuart_soc_data imxrt1050_data = {
337 .devtype = IMXRT1050_LPUART,
338 .iotype = UPIO_MEM32,
339 .reg_off = IMX_REG_OFF,
343 static const struct of_device_id lpuart_dt_ids[] = {
344 { .compatible = "fsl,vf610-lpuart", .data = &vf_data, },
345 { .compatible = "fsl,ls1021a-lpuart", .data = &ls1021a_data, },
346 { .compatible = "fsl,ls1028a-lpuart", .data = &ls1028a_data, },
347 { .compatible = "fsl,imx7ulp-lpuart", .data = &imx7ulp_data, },
348 { .compatible = "fsl,imx8ulp-lpuart", .data = &imx8ulp_data, },
349 { .compatible = "fsl,imx8qxp-lpuart", .data = &imx8qxp_data, },
350 { .compatible = "fsl,imxrt1050-lpuart", .data = &imxrt1050_data},
353 MODULE_DEVICE_TABLE(of, lpuart_dt_ids);
355 /* Forward declare this for the dma callbacks*/
356 static void lpuart_dma_tx_complete(void *arg);
358 static inline bool is_layerscape_lpuart(struct lpuart_port *sport)
360 return (sport->devtype == LS1021A_LPUART ||
361 sport->devtype == LS1028A_LPUART);
364 static inline bool is_imx7ulp_lpuart(struct lpuart_port *sport)
366 return sport->devtype == IMX7ULP_LPUART;
369 static inline bool is_imx8ulp_lpuart(struct lpuart_port *sport)
371 return sport->devtype == IMX8ULP_LPUART;
374 static inline bool is_imx8qxp_lpuart(struct lpuart_port *sport)
376 return sport->devtype == IMX8QXP_LPUART;
379 static inline u32 lpuart32_read(struct uart_port *port, u32 off)
381 switch (port->iotype) {
383 return readl(port->membase + off);
385 return ioread32be(port->membase + off);
391 static inline void lpuart32_write(struct uart_port *port, u32 val,
394 switch (port->iotype) {
396 writel(val, port->membase + off);
399 iowrite32be(val, port->membase + off);
404 static int __lpuart_enable_clks(struct lpuart_port *sport, bool is_en)
409 ret = clk_prepare_enable(sport->ipg_clk);
413 ret = clk_prepare_enable(sport->baud_clk);
415 clk_disable_unprepare(sport->ipg_clk);
419 clk_disable_unprepare(sport->baud_clk);
420 clk_disable_unprepare(sport->ipg_clk);
426 static unsigned int lpuart_get_baud_clk_rate(struct lpuart_port *sport)
428 if (is_imx8qxp_lpuart(sport))
429 return clk_get_rate(sport->baud_clk);
431 return clk_get_rate(sport->ipg_clk);
434 #define lpuart_enable_clks(x) __lpuart_enable_clks(x, true)
435 #define lpuart_disable_clks(x) __lpuart_enable_clks(x, false)
437 static void lpuart_stop_tx(struct uart_port *port)
441 temp = readb(port->membase + UARTCR2);
442 temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
443 writeb(temp, port->membase + UARTCR2);
446 static void lpuart32_stop_tx(struct uart_port *port)
450 temp = lpuart32_read(port, UARTCTRL);
451 temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
452 lpuart32_write(port, temp, UARTCTRL);
455 static void lpuart_stop_rx(struct uart_port *port)
459 temp = readb(port->membase + UARTCR2);
460 writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
463 static void lpuart32_stop_rx(struct uart_port *port)
467 temp = lpuart32_read(port, UARTCTRL);
468 lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
471 static void lpuart_dma_tx(struct lpuart_port *sport)
473 struct circ_buf *xmit = &sport->port.state->xmit;
474 struct scatterlist *sgl = sport->tx_sgl;
475 struct device *dev = sport->port.dev;
476 struct dma_chan *chan = sport->dma_tx_chan;
479 if (sport->dma_tx_in_progress)
482 sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
484 if (xmit->tail < xmit->head || xmit->head == 0) {
485 sport->dma_tx_nents = 1;
486 sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
488 sport->dma_tx_nents = 2;
489 sg_init_table(sgl, 2);
490 sg_set_buf(sgl, xmit->buf + xmit->tail,
491 UART_XMIT_SIZE - xmit->tail);
492 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
495 ret = dma_map_sg(chan->device->dev, sgl, sport->dma_tx_nents,
498 dev_err(dev, "DMA mapping error for TX.\n");
502 sport->dma_tx_desc = dmaengine_prep_slave_sg(chan, sgl,
505 if (!sport->dma_tx_desc) {
506 dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
508 dev_err(dev, "Cannot prepare TX slave DMA!\n");
512 sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
513 sport->dma_tx_desc->callback_param = sport;
514 sport->dma_tx_in_progress = true;
515 sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
516 dma_async_issue_pending(chan);
519 static bool lpuart_stopped_or_empty(struct uart_port *port)
521 return uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port);
524 static void lpuart_dma_tx_complete(void *arg)
526 struct lpuart_port *sport = arg;
527 struct scatterlist *sgl = &sport->tx_sgl[0];
528 struct circ_buf *xmit = &sport->port.state->xmit;
529 struct dma_chan *chan = sport->dma_tx_chan;
532 spin_lock_irqsave(&sport->port.lock, flags);
533 if (!sport->dma_tx_in_progress) {
534 spin_unlock_irqrestore(&sport->port.lock, flags);
538 dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
541 uart_xmit_advance(&sport->port, sport->dma_tx_bytes);
542 sport->dma_tx_in_progress = false;
543 spin_unlock_irqrestore(&sport->port.lock, flags);
545 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
546 uart_write_wakeup(&sport->port);
548 if (waitqueue_active(&sport->dma_wait)) {
549 wake_up(&sport->dma_wait);
553 spin_lock_irqsave(&sport->port.lock, flags);
555 if (!lpuart_stopped_or_empty(&sport->port))
556 lpuart_dma_tx(sport);
558 spin_unlock_irqrestore(&sport->port.lock, flags);
561 static dma_addr_t lpuart_dma_datareg_addr(struct lpuart_port *sport)
563 switch (sport->port.iotype) {
565 return sport->port.mapbase + UARTDATA;
567 return sport->port.mapbase + UARTDATA + sizeof(u32) - 1;
569 return sport->port.mapbase + UARTDR;
572 static int lpuart_dma_tx_request(struct uart_port *port)
574 struct lpuart_port *sport = container_of(port,
575 struct lpuart_port, port);
576 struct dma_slave_config dma_tx_sconfig = {};
579 dma_tx_sconfig.dst_addr = lpuart_dma_datareg_addr(sport);
580 dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
581 dma_tx_sconfig.dst_maxburst = 1;
582 dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
583 ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
586 dev_err(sport->port.dev,
587 "DMA slave config failed, err = %d\n", ret);
594 static bool lpuart_is_32(struct lpuart_port *sport)
596 return sport->port.iotype == UPIO_MEM32 ||
597 sport->port.iotype == UPIO_MEM32BE;
600 static void lpuart_flush_buffer(struct uart_port *port)
602 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
603 struct dma_chan *chan = sport->dma_tx_chan;
606 if (sport->lpuart_dma_tx_use) {
607 if (sport->dma_tx_in_progress) {
608 dma_unmap_sg(chan->device->dev, &sport->tx_sgl[0],
609 sport->dma_tx_nents, DMA_TO_DEVICE);
610 sport->dma_tx_in_progress = false;
612 dmaengine_terminate_async(chan);
615 if (lpuart_is_32(sport)) {
616 val = lpuart32_read(&sport->port, UARTFIFO);
617 val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
618 lpuart32_write(&sport->port, val, UARTFIFO);
620 val = readb(sport->port.membase + UARTCFIFO);
621 val |= UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH;
622 writeb(val, sport->port.membase + UARTCFIFO);
626 static void lpuart_wait_bit_set(struct uart_port *port, unsigned int offset,
629 while (!(readb(port->membase + offset) & bit))
633 static void lpuart32_wait_bit_set(struct uart_port *port, unsigned int offset,
636 while (!(lpuart32_read(port, offset) & bit))
640 #if defined(CONFIG_CONSOLE_POLL)
642 static int lpuart_poll_init(struct uart_port *port)
644 struct lpuart_port *sport = container_of(port,
645 struct lpuart_port, port);
649 sport->port.fifosize = 0;
651 spin_lock_irqsave(&sport->port.lock, flags);
652 /* Disable Rx & Tx */
653 writeb(0, sport->port.membase + UARTCR2);
655 temp = readb(sport->port.membase + UARTPFIFO);
656 /* Enable Rx and Tx FIFO */
657 writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
658 sport->port.membase + UARTPFIFO);
660 /* flush Tx and Rx FIFO */
661 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
662 sport->port.membase + UARTCFIFO);
664 /* explicitly clear RDRF */
665 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
666 readb(sport->port.membase + UARTDR);
667 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
670 writeb(0, sport->port.membase + UARTTWFIFO);
671 writeb(1, sport->port.membase + UARTRWFIFO);
673 /* Enable Rx and Tx */
674 writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
675 spin_unlock_irqrestore(&sport->port.lock, flags);
680 static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
683 lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
684 writeb(c, port->membase + UARTDR);
687 static int lpuart_poll_get_char(struct uart_port *port)
689 if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
692 return readb(port->membase + UARTDR);
695 static int lpuart32_poll_init(struct uart_port *port)
698 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
701 sport->port.fifosize = 0;
703 spin_lock_irqsave(&sport->port.lock, flags);
705 /* Disable Rx & Tx */
706 lpuart32_write(&sport->port, 0, UARTCTRL);
708 temp = lpuart32_read(&sport->port, UARTFIFO);
710 /* Enable Rx and Tx FIFO */
711 lpuart32_write(&sport->port, temp | UARTFIFO_RXFE | UARTFIFO_TXFE, UARTFIFO);
713 /* flush Tx and Rx FIFO */
714 lpuart32_write(&sport->port, UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH, UARTFIFO);
716 /* explicitly clear RDRF */
717 if (lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_RDRF) {
718 lpuart32_read(&sport->port, UARTDATA);
719 lpuart32_write(&sport->port, UARTFIFO_RXUF, UARTFIFO);
722 /* Enable Rx and Tx */
723 lpuart32_write(&sport->port, UARTCTRL_RE | UARTCTRL_TE, UARTCTRL);
724 spin_unlock_irqrestore(&sport->port.lock, flags);
729 static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
731 lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
732 lpuart32_write(port, c, UARTDATA);
735 static int lpuart32_poll_get_char(struct uart_port *port)
737 if (!(lpuart32_read(port, UARTWATER) >> UARTWATER_RXCNT_OFF))
740 return lpuart32_read(port, UARTDATA);
744 static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
746 struct uart_port *port = &sport->port;
749 uart_port_tx(port, ch,
750 readb(port->membase + UARTTCFIFO) < sport->txfifo_size,
751 writeb(ch, port->membase + UARTDR));
754 static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
756 struct circ_buf *xmit = &sport->port.state->xmit;
759 if (sport->port.x_char) {
760 lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
761 sport->port.icount.tx++;
762 sport->port.x_char = 0;
766 if (lpuart_stopped_or_empty(&sport->port)) {
767 lpuart32_stop_tx(&sport->port);
771 txcnt = lpuart32_read(&sport->port, UARTWATER);
772 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
773 txcnt &= UARTWATER_COUNT_MASK;
774 while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
775 lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
776 uart_xmit_advance(&sport->port, 1);
777 txcnt = lpuart32_read(&sport->port, UARTWATER);
778 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
779 txcnt &= UARTWATER_COUNT_MASK;
782 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
783 uart_write_wakeup(&sport->port);
785 if (uart_circ_empty(xmit))
786 lpuart32_stop_tx(&sport->port);
789 static void lpuart_start_tx(struct uart_port *port)
791 struct lpuart_port *sport = container_of(port,
792 struct lpuart_port, port);
795 temp = readb(port->membase + UARTCR2);
796 writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
798 if (sport->lpuart_dma_tx_use) {
799 if (!lpuart_stopped_or_empty(port))
800 lpuart_dma_tx(sport);
802 if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
803 lpuart_transmit_buffer(sport);
807 static void lpuart32_start_tx(struct uart_port *port)
809 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
812 if (sport->lpuart_dma_tx_use) {
813 if (!lpuart_stopped_or_empty(port))
814 lpuart_dma_tx(sport);
816 temp = lpuart32_read(port, UARTCTRL);
817 lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
819 if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
820 lpuart32_transmit_buffer(sport);
825 lpuart_uart_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
828 case UART_PM_STATE_OFF:
829 pm_runtime_mark_last_busy(port->dev);
830 pm_runtime_put_autosuspend(port->dev);
833 pm_runtime_get_sync(port->dev);
838 /* return TIOCSER_TEMT when transmitter is not busy */
839 static unsigned int lpuart_tx_empty(struct uart_port *port)
841 struct lpuart_port *sport = container_of(port,
842 struct lpuart_port, port);
843 unsigned char sr1 = readb(port->membase + UARTSR1);
844 unsigned char sfifo = readb(port->membase + UARTSFIFO);
846 if (sport->dma_tx_in_progress)
849 if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
855 static unsigned int lpuart32_tx_empty(struct uart_port *port)
857 struct lpuart_port *sport = container_of(port,
858 struct lpuart_port, port);
859 unsigned long stat = lpuart32_read(port, UARTSTAT);
860 unsigned long sfifo = lpuart32_read(port, UARTFIFO);
861 unsigned long ctrl = lpuart32_read(port, UARTCTRL);
863 if (sport->dma_tx_in_progress)
867 * LPUART Transmission Complete Flag may never be set while queuing a break
868 * character, so avoid checking for transmission complete when UARTCTRL_SBK
871 if ((stat & UARTSTAT_TC && sfifo & UARTFIFO_TXEMPT) || ctrl & UARTCTRL_SBK)
877 static void lpuart_txint(struct lpuart_port *sport)
879 spin_lock(&sport->port.lock);
880 lpuart_transmit_buffer(sport);
881 spin_unlock(&sport->port.lock);
884 static void lpuart_rxint(struct lpuart_port *sport)
886 unsigned int flg, ignored = 0, overrun = 0;
887 struct tty_port *port = &sport->port.state->port;
888 unsigned char rx, sr;
890 spin_lock(&sport->port.lock);
892 while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
894 sport->port.icount.rx++;
896 * to clear the FE, OR, NF, FE, PE flags,
897 * read SR1 then read DR
899 sr = readb(sport->port.membase + UARTSR1);
900 rx = readb(sport->port.membase + UARTDR);
902 if (uart_prepare_sysrq_char(&sport->port, rx))
905 if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
907 sport->port.icount.parity++;
908 else if (sr & UARTSR1_FE)
909 sport->port.icount.frame++;
914 if (sr & sport->port.ignore_status_mask) {
920 sr &= sport->port.read_status_mask;
924 else if (sr & UARTSR1_FE)
930 sport->port.sysrq = 0;
933 if (tty_insert_flip_char(port, rx, flg) == 0)
934 sport->port.icount.buf_overrun++;
939 sport->port.icount.overrun += overrun;
942 * Overruns cause FIFO pointers to become missaligned.
943 * Flushing the receive FIFO reinitializes the pointers.
945 writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
946 writeb(UARTSFIFO_RXOF, sport->port.membase + UARTSFIFO);
949 uart_unlock_and_check_sysrq(&sport->port);
951 tty_flip_buffer_push(port);
954 static void lpuart32_txint(struct lpuart_port *sport)
956 spin_lock(&sport->port.lock);
957 lpuart32_transmit_buffer(sport);
958 spin_unlock(&sport->port.lock);
961 static void lpuart32_rxint(struct lpuart_port *sport)
963 unsigned int flg, ignored = 0;
964 struct tty_port *port = &sport->port.state->port;
965 unsigned long rx, sr;
968 spin_lock(&sport->port.lock);
970 while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
972 sport->port.icount.rx++;
974 * to clear the FE, OR, NF, FE, PE flags,
975 * read STAT then read DATA reg
977 sr = lpuart32_read(&sport->port, UARTSTAT);
978 rx = lpuart32_read(&sport->port, UARTDATA);
982 * The LPUART can't distinguish between a break and a framing error,
983 * thus we assume it is a break if the received data is zero.
985 is_break = (sr & UARTSTAT_FE) && !rx;
987 if (is_break && uart_handle_break(&sport->port))
990 if (uart_prepare_sysrq_char(&sport->port, rx))
993 if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
994 if (sr & UARTSTAT_PE) {
995 sport->port.icount.parity++;
996 } else if (sr & UARTSTAT_FE) {
998 sport->port.icount.brk++;
1000 sport->port.icount.frame++;
1003 if (sr & UARTSTAT_OR)
1004 sport->port.icount.overrun++;
1006 if (sr & sport->port.ignore_status_mask) {
1007 if (++ignored > 100)
1012 sr &= sport->port.read_status_mask;
1014 if (sr & UARTSTAT_PE) {
1016 } else if (sr & UARTSTAT_FE) {
1023 if (sr & UARTSTAT_OR)
1030 if (tty_insert_flip_char(port, rx, flg) == 0)
1031 sport->port.icount.buf_overrun++;
1035 uart_unlock_and_check_sysrq(&sport->port);
1037 tty_flip_buffer_push(port);
1040 static irqreturn_t lpuart_int(int irq, void *dev_id)
1042 struct lpuart_port *sport = dev_id;
1045 sts = readb(sport->port.membase + UARTSR1);
1047 /* SysRq, using dma, check for linebreak by framing err. */
1048 if (sts & UARTSR1_FE && sport->lpuart_dma_rx_use) {
1049 readb(sport->port.membase + UARTDR);
1050 uart_handle_break(&sport->port);
1051 /* linebreak produces some garbage, removing it */
1052 writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
1056 if (sts & UARTSR1_RDRF && !sport->lpuart_dma_rx_use)
1057 lpuart_rxint(sport);
1059 if (sts & UARTSR1_TDRE && !sport->lpuart_dma_tx_use)
1060 lpuart_txint(sport);
1065 static irqreturn_t lpuart32_int(int irq, void *dev_id)
1067 struct lpuart_port *sport = dev_id;
1068 unsigned long sts, rxcount;
1070 sts = lpuart32_read(&sport->port, UARTSTAT);
1071 rxcount = lpuart32_read(&sport->port, UARTWATER);
1072 rxcount = rxcount >> UARTWATER_RXCNT_OFF;
1074 if ((sts & UARTSTAT_RDRF || rxcount > 0) && !sport->lpuart_dma_rx_use)
1075 lpuart32_rxint(sport);
1077 if ((sts & UARTSTAT_TDRE) && !sport->lpuart_dma_tx_use)
1078 lpuart32_txint(sport);
1080 lpuart32_write(&sport->port, sts, UARTSTAT);
1085 static inline void lpuart_handle_sysrq_chars(struct uart_port *port,
1086 unsigned char *p, int count)
1089 if (*p && uart_handle_sysrq_char(port, *p))
1095 static void lpuart_handle_sysrq(struct lpuart_port *sport)
1097 struct circ_buf *ring = &sport->rx_ring;
1100 if (ring->head < ring->tail) {
1101 count = sport->rx_sgl.length - ring->tail;
1102 lpuart_handle_sysrq_chars(&sport->port,
1103 ring->buf + ring->tail, count);
1107 if (ring->head > ring->tail) {
1108 count = ring->head - ring->tail;
1109 lpuart_handle_sysrq_chars(&sport->port,
1110 ring->buf + ring->tail, count);
1111 ring->tail = ring->head;
1115 static int lpuart_tty_insert_flip_string(struct tty_port *port,
1116 unsigned char *chars, size_t size, bool is_cs7)
1121 for (i = 0; i < size; i++)
1123 return tty_insert_flip_string(port, chars, size);
1126 static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
1128 struct tty_port *port = &sport->port.state->port;
1129 struct dma_tx_state state;
1130 enum dma_status dmastat;
1131 struct dma_chan *chan = sport->dma_rx_chan;
1132 struct circ_buf *ring = &sport->rx_ring;
1133 unsigned long flags;
1136 if (lpuart_is_32(sport)) {
1137 unsigned long sr = lpuart32_read(&sport->port, UARTSTAT);
1139 if (sr & (UARTSTAT_PE | UARTSTAT_FE)) {
1140 /* Read DR to clear the error flags */
1141 lpuart32_read(&sport->port, UARTDATA);
1143 if (sr & UARTSTAT_PE)
1144 sport->port.icount.parity++;
1145 else if (sr & UARTSTAT_FE)
1146 sport->port.icount.frame++;
1149 unsigned char sr = readb(sport->port.membase + UARTSR1);
1151 if (sr & (UARTSR1_PE | UARTSR1_FE)) {
1154 /* Disable receiver during this operation... */
1155 cr2 = readb(sport->port.membase + UARTCR2);
1157 writeb(cr2, sport->port.membase + UARTCR2);
1159 /* Read DR to clear the error flags */
1160 readb(sport->port.membase + UARTDR);
1162 if (sr & UARTSR1_PE)
1163 sport->port.icount.parity++;
1164 else if (sr & UARTSR1_FE)
1165 sport->port.icount.frame++;
1167 * At this point parity/framing error is
1168 * cleared However, since the DMA already read
1169 * the data register and we had to read it
1170 * again after reading the status register to
1171 * properly clear the flags, the FIFO actually
1172 * underflowed... This requires a clearing of
1175 if (readb(sport->port.membase + UARTSFIFO) &
1177 writeb(UARTSFIFO_RXUF,
1178 sport->port.membase + UARTSFIFO);
1179 writeb(UARTCFIFO_RXFLUSH,
1180 sport->port.membase + UARTCFIFO);
1184 writeb(cr2, sport->port.membase + UARTCR2);
1188 async_tx_ack(sport->dma_rx_desc);
1190 spin_lock_irqsave(&sport->port.lock, flags);
1192 dmastat = dmaengine_tx_status(chan, sport->dma_rx_cookie, &state);
1193 if (dmastat == DMA_ERROR) {
1194 dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
1195 spin_unlock_irqrestore(&sport->port.lock, flags);
1199 /* CPU claims ownership of RX DMA buffer */
1200 dma_sync_sg_for_cpu(chan->device->dev, &sport->rx_sgl, 1,
1204 * ring->head points to the end of data already written by the DMA.
1205 * ring->tail points to the beginning of data to be read by the
1207 * The current transfer size should not be larger than the dma buffer
1210 ring->head = sport->rx_sgl.length - state.residue;
1211 BUG_ON(ring->head > sport->rx_sgl.length);
1214 * Silent handling of keys pressed in the sysrq timeframe
1216 if (sport->port.sysrq) {
1217 lpuart_handle_sysrq(sport);
1222 * At this point ring->head may point to the first byte right after the
1223 * last byte of the dma buffer:
1224 * 0 <= ring->head <= sport->rx_sgl.length
1226 * However ring->tail must always points inside the dma buffer:
1227 * 0 <= ring->tail <= sport->rx_sgl.length - 1
1229 * Since we use a ring buffer, we have to handle the case
1230 * where head is lower than tail. In such a case, we first read from
1231 * tail to the end of the buffer then reset tail.
1233 if (ring->head < ring->tail) {
1234 count = sport->rx_sgl.length - ring->tail;
1236 copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
1237 count, sport->is_cs7);
1238 if (copied != count)
1239 sport->port.icount.buf_overrun++;
1241 sport->port.icount.rx += copied;
1244 /* Finally we read data from tail to head */
1245 if (ring->tail < ring->head) {
1246 count = ring->head - ring->tail;
1247 copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
1248 count, sport->is_cs7);
1249 if (copied != count)
1250 sport->port.icount.buf_overrun++;
1251 /* Wrap ring->head if needed */
1252 if (ring->head >= sport->rx_sgl.length)
1254 ring->tail = ring->head;
1255 sport->port.icount.rx += copied;
1259 dma_sync_sg_for_device(chan->device->dev, &sport->rx_sgl, 1,
1262 spin_unlock_irqrestore(&sport->port.lock, flags);
1264 tty_flip_buffer_push(port);
1265 mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
1268 static void lpuart_dma_rx_complete(void *arg)
1270 struct lpuart_port *sport = arg;
1272 lpuart_copy_rx_to_tty(sport);
1275 static void lpuart_timer_func(struct timer_list *t)
1277 struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
1279 lpuart_copy_rx_to_tty(sport);
1282 static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
1284 struct dma_slave_config dma_rx_sconfig = {};
1285 struct circ_buf *ring = &sport->rx_ring;
1287 struct tty_port *port = &sport->port.state->port;
1288 struct tty_struct *tty = port->tty;
1289 struct ktermios *termios = &tty->termios;
1290 struct dma_chan *chan = sport->dma_rx_chan;
1291 unsigned int bits = tty_get_frame_size(termios->c_cflag);
1292 unsigned int baud = tty_get_baud_rate(tty);
1295 * Calculate length of one DMA buffer size to keep latency below
1296 * 10ms at any baud rate.
1298 sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits / 1000) * 2;
1299 sport->rx_dma_rng_buf_len = (1 << fls(sport->rx_dma_rng_buf_len));
1300 if (sport->rx_dma_rng_buf_len < 16)
1301 sport->rx_dma_rng_buf_len = 16;
1303 ring->buf = kzalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
1307 sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1308 nent = dma_map_sg(chan->device->dev, &sport->rx_sgl, 1,
1312 dev_err(sport->port.dev, "DMA Rx mapping error\n");
1316 dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
1317 dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1318 dma_rx_sconfig.src_maxburst = 1;
1319 dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
1320 ret = dmaengine_slave_config(chan, &dma_rx_sconfig);
1323 dev_err(sport->port.dev,
1324 "DMA Rx slave config failed, err = %d\n", ret);
1328 sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
1329 sg_dma_address(&sport->rx_sgl),
1330 sport->rx_sgl.length,
1331 sport->rx_sgl.length / 2,
1333 DMA_PREP_INTERRUPT);
1334 if (!sport->dma_rx_desc) {
1335 dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
1339 sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
1340 sport->dma_rx_desc->callback_param = sport;
1341 sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
1342 dma_async_issue_pending(chan);
1344 if (lpuart_is_32(sport)) {
1345 unsigned long temp = lpuart32_read(&sport->port, UARTBAUD);
1347 lpuart32_write(&sport->port, temp | UARTBAUD_RDMAE, UARTBAUD);
1349 writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
1350 sport->port.membase + UARTCR5);
1356 static void lpuart_dma_rx_free(struct uart_port *port)
1358 struct lpuart_port *sport = container_of(port,
1359 struct lpuart_port, port);
1360 struct dma_chan *chan = sport->dma_rx_chan;
1362 dmaengine_terminate_sync(chan);
1363 del_timer_sync(&sport->lpuart_timer);
1364 dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1365 kfree(sport->rx_ring.buf);
1366 sport->rx_ring.tail = 0;
1367 sport->rx_ring.head = 0;
1368 sport->dma_rx_desc = NULL;
1369 sport->dma_rx_cookie = -EINVAL;
1372 static int lpuart_config_rs485(struct uart_port *port, struct ktermios *termios,
1373 struct serial_rs485 *rs485)
1375 struct lpuart_port *sport = container_of(port,
1376 struct lpuart_port, port);
1378 u8 modem = readb(sport->port.membase + UARTMODEM) &
1379 ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
1380 writeb(modem, sport->port.membase + UARTMODEM);
1382 if (rs485->flags & SER_RS485_ENABLED) {
1383 /* Enable auto RS-485 RTS mode */
1384 modem |= UARTMODEM_TXRTSE;
1387 * The hardware defaults to RTS logic HIGH while transfer.
1388 * Switch polarity in case RTS shall be logic HIGH
1390 * Note: UART is assumed to be active high.
1392 if (rs485->flags & SER_RS485_RTS_ON_SEND)
1393 modem |= UARTMODEM_TXRTSPOL;
1394 else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1395 modem &= ~UARTMODEM_TXRTSPOL;
1398 writeb(modem, sport->port.membase + UARTMODEM);
1402 static int lpuart32_config_rs485(struct uart_port *port, struct ktermios *termios,
1403 struct serial_rs485 *rs485)
1405 struct lpuart_port *sport = container_of(port,
1406 struct lpuart_port, port);
1408 unsigned long modem = lpuart32_read(&sport->port, UARTMODIR)
1409 & ~(UARTMODIR_TXRTSPOL | UARTMODIR_TXRTSE);
1410 lpuart32_write(&sport->port, modem, UARTMODIR);
1412 if (rs485->flags & SER_RS485_ENABLED) {
1413 /* Enable auto RS-485 RTS mode */
1414 modem |= UARTMODIR_TXRTSE;
1417 * The hardware defaults to RTS logic HIGH while transfer.
1418 * Switch polarity in case RTS shall be logic HIGH
1420 * Note: UART is assumed to be active high.
1422 if (rs485->flags & SER_RS485_RTS_ON_SEND)
1423 modem |= UARTMODIR_TXRTSPOL;
1424 else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1425 modem &= ~UARTMODIR_TXRTSPOL;
1428 lpuart32_write(&sport->port, modem, UARTMODIR);
1432 static unsigned int lpuart_get_mctrl(struct uart_port *port)
1434 unsigned int mctrl = 0;
1437 reg = readb(port->membase + UARTCR1);
1438 if (reg & UARTCR1_LOOPS)
1439 mctrl |= TIOCM_LOOP;
1444 static unsigned int lpuart32_get_mctrl(struct uart_port *port)
1446 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1449 reg = lpuart32_read(port, UARTCTRL);
1450 if (reg & UARTCTRL_LOOPS)
1451 mctrl |= TIOCM_LOOP;
1456 static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1460 reg = readb(port->membase + UARTCR1);
1462 /* for internal loopback we need LOOPS=1 and RSRC=0 */
1463 reg &= ~(UARTCR1_LOOPS | UARTCR1_RSRC);
1464 if (mctrl & TIOCM_LOOP)
1465 reg |= UARTCR1_LOOPS;
1467 writeb(reg, port->membase + UARTCR1);
1470 static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
1474 reg = lpuart32_read(port, UARTCTRL);
1476 /* for internal loopback we need LOOPS=1 and RSRC=0 */
1477 reg &= ~(UARTCTRL_LOOPS | UARTCTRL_RSRC);
1478 if (mctrl & TIOCM_LOOP)
1479 reg |= UARTCTRL_LOOPS;
1481 lpuart32_write(port, reg, UARTCTRL);
1484 static void lpuart_break_ctl(struct uart_port *port, int break_state)
1488 temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
1490 if (break_state != 0)
1491 temp |= UARTCR2_SBK;
1493 writeb(temp, port->membase + UARTCR2);
1496 static void lpuart32_break_ctl(struct uart_port *port, int break_state)
1500 temp = lpuart32_read(port, UARTCTRL);
1503 * LPUART IP now has two known bugs, one is CTS has higher priority than the
1504 * break signal, which causes the break signal sending through UARTCTRL_SBK
1505 * may impacted by the CTS input if the HW flow control is enabled. It
1506 * exists on all platforms we support in this driver.
1507 * Another bug is i.MX8QM LPUART may have an additional break character
1508 * being sent after SBK was cleared.
1509 * To avoid above two bugs, we use Transmit Data Inversion function to send
1510 * the break signal instead of UARTCTRL_SBK.
1512 if (break_state != 0) {
1514 * Disable the transmitter to prevent any data from being sent out
1515 * during break, then invert the TX line to send break.
1517 temp &= ~UARTCTRL_TE;
1518 lpuart32_write(port, temp, UARTCTRL);
1519 temp |= UARTCTRL_TXINV;
1520 lpuart32_write(port, temp, UARTCTRL);
1522 /* Disable the TXINV to turn off break and re-enable transmitter. */
1523 temp &= ~UARTCTRL_TXINV;
1524 lpuart32_write(port, temp, UARTCTRL);
1525 temp |= UARTCTRL_TE;
1526 lpuart32_write(port, temp, UARTCTRL);
1530 static void lpuart_setup_watermark(struct lpuart_port *sport)
1532 unsigned char val, cr2;
1533 unsigned char cr2_saved;
1535 cr2 = readb(sport->port.membase + UARTCR2);
1537 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
1538 UARTCR2_RIE | UARTCR2_RE);
1539 writeb(cr2, sport->port.membase + UARTCR2);
1541 val = readb(sport->port.membase + UARTPFIFO);
1542 writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
1543 sport->port.membase + UARTPFIFO);
1545 /* flush Tx and Rx FIFO */
1546 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
1547 sport->port.membase + UARTCFIFO);
1549 /* explicitly clear RDRF */
1550 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
1551 readb(sport->port.membase + UARTDR);
1552 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
1555 if (uart_console(&sport->port))
1556 sport->rx_watermark = 1;
1557 writeb(0, sport->port.membase + UARTTWFIFO);
1558 writeb(sport->rx_watermark, sport->port.membase + UARTRWFIFO);
1561 writeb(cr2_saved, sport->port.membase + UARTCR2);
1564 static void lpuart_setup_watermark_enable(struct lpuart_port *sport)
1568 lpuart_setup_watermark(sport);
1570 cr2 = readb(sport->port.membase + UARTCR2);
1571 cr2 |= UARTCR2_RIE | UARTCR2_RE | UARTCR2_TE;
1572 writeb(cr2, sport->port.membase + UARTCR2);
1575 static void lpuart32_setup_watermark(struct lpuart_port *sport)
1577 unsigned long val, ctrl;
1578 unsigned long ctrl_saved;
1580 ctrl = lpuart32_read(&sport->port, UARTCTRL);
1582 ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
1583 UARTCTRL_RIE | UARTCTRL_RE | UARTCTRL_ILIE);
1584 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1586 /* enable FIFO mode */
1587 val = lpuart32_read(&sport->port, UARTFIFO);
1588 val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
1589 val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
1590 val |= FIELD_PREP(UARTFIFO_RXIDEN, 0x3);
1591 lpuart32_write(&sport->port, val, UARTFIFO);
1593 /* set the watermark */
1594 if (uart_console(&sport->port))
1595 sport->rx_watermark = 1;
1596 val = (sport->rx_watermark << UARTWATER_RXWATER_OFF) |
1597 (0x0 << UARTWATER_TXWATER_OFF);
1598 lpuart32_write(&sport->port, val, UARTWATER);
1600 /* set RTS watermark */
1601 if (!uart_console(&sport->port)) {
1602 val = lpuart32_read(&sport->port, UARTMODIR);
1603 val |= FIELD_PREP(UARTMODIR_RTSWATER, sport->rxfifo_size >> 1);
1604 lpuart32_write(&sport->port, val, UARTMODIR);
1608 lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
1611 static void lpuart32_setup_watermark_enable(struct lpuart_port *sport)
1615 lpuart32_setup_watermark(sport);
1617 temp = lpuart32_read(&sport->port, UARTCTRL);
1618 temp |= UARTCTRL_RE | UARTCTRL_TE;
1619 temp |= FIELD_PREP(UARTCTRL_IDLECFG, 0x7);
1620 lpuart32_write(&sport->port, temp, UARTCTRL);
1623 static void rx_dma_timer_init(struct lpuart_port *sport)
1625 timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
1626 sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
1627 add_timer(&sport->lpuart_timer);
1630 static void lpuart_request_dma(struct lpuart_port *sport)
1632 sport->dma_tx_chan = dma_request_chan(sport->port.dev, "tx");
1633 if (IS_ERR(sport->dma_tx_chan)) {
1634 dev_dbg_once(sport->port.dev,
1635 "DMA tx channel request failed, operating without tx DMA (%ld)\n",
1636 PTR_ERR(sport->dma_tx_chan));
1637 sport->dma_tx_chan = NULL;
1640 sport->dma_rx_chan = dma_request_chan(sport->port.dev, "rx");
1641 if (IS_ERR(sport->dma_rx_chan)) {
1642 dev_dbg_once(sport->port.dev,
1643 "DMA rx channel request failed, operating without rx DMA (%ld)\n",
1644 PTR_ERR(sport->dma_rx_chan));
1645 sport->dma_rx_chan = NULL;
1649 static void lpuart_tx_dma_startup(struct lpuart_port *sport)
1654 if (uart_console(&sport->port))
1657 if (!sport->dma_tx_chan)
1660 ret = lpuart_dma_tx_request(&sport->port);
1664 init_waitqueue_head(&sport->dma_wait);
1665 sport->lpuart_dma_tx_use = true;
1666 if (lpuart_is_32(sport)) {
1667 uartbaud = lpuart32_read(&sport->port, UARTBAUD);
1668 lpuart32_write(&sport->port,
1669 uartbaud | UARTBAUD_TDMAE, UARTBAUD);
1671 writeb(readb(sport->port.membase + UARTCR5) |
1672 UARTCR5_TDMAS, sport->port.membase + UARTCR5);
1678 sport->lpuart_dma_tx_use = false;
1681 static void lpuart_rx_dma_startup(struct lpuart_port *sport)
1686 if (uart_console(&sport->port))
1689 if (!sport->dma_rx_chan)
1692 ret = lpuart_start_rx_dma(sport);
1696 /* set Rx DMA timeout */
1697 sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
1698 if (!sport->dma_rx_timeout)
1699 sport->dma_rx_timeout = 1;
1701 sport->lpuart_dma_rx_use = true;
1702 rx_dma_timer_init(sport);
1704 if (sport->port.has_sysrq && !lpuart_is_32(sport)) {
1705 cr3 = readb(sport->port.membase + UARTCR3);
1706 cr3 |= UARTCR3_FEIE;
1707 writeb(cr3, sport->port.membase + UARTCR3);
1713 sport->lpuart_dma_rx_use = false;
1716 static void lpuart_hw_setup(struct lpuart_port *sport)
1718 unsigned long flags;
1720 spin_lock_irqsave(&sport->port.lock, flags);
1722 lpuart_setup_watermark_enable(sport);
1724 lpuart_rx_dma_startup(sport);
1725 lpuart_tx_dma_startup(sport);
1727 spin_unlock_irqrestore(&sport->port.lock, flags);
1730 static int lpuart_startup(struct uart_port *port)
1732 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1735 /* determine FIFO size and enable FIFO mode */
1736 temp = readb(sport->port.membase + UARTPFIFO);
1738 sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_TXSIZE_OFF) &
1739 UARTPFIFO_FIFOSIZE_MASK);
1740 sport->port.fifosize = sport->txfifo_size;
1742 sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
1743 UARTPFIFO_FIFOSIZE_MASK);
1745 lpuart_request_dma(sport);
1746 lpuart_hw_setup(sport);
1751 static void lpuart32_hw_disable(struct lpuart_port *sport)
1755 temp = lpuart32_read(&sport->port, UARTCTRL);
1756 temp &= ~(UARTCTRL_RIE | UARTCTRL_ILIE | UARTCTRL_RE |
1757 UARTCTRL_TIE | UARTCTRL_TE);
1758 lpuart32_write(&sport->port, temp, UARTCTRL);
1761 static void lpuart32_configure(struct lpuart_port *sport)
1765 temp = lpuart32_read(&sport->port, UARTCTRL);
1766 if (!sport->lpuart_dma_rx_use)
1767 temp |= UARTCTRL_RIE | UARTCTRL_ILIE;
1768 if (!sport->lpuart_dma_tx_use)
1769 temp |= UARTCTRL_TIE;
1770 lpuart32_write(&sport->port, temp, UARTCTRL);
1773 static void lpuart32_hw_setup(struct lpuart_port *sport)
1775 unsigned long flags;
1777 spin_lock_irqsave(&sport->port.lock, flags);
1779 lpuart32_hw_disable(sport);
1781 lpuart_rx_dma_startup(sport);
1782 lpuart_tx_dma_startup(sport);
1784 lpuart32_setup_watermark_enable(sport);
1785 lpuart32_configure(sport);
1787 spin_unlock_irqrestore(&sport->port.lock, flags);
1790 static int lpuart32_startup(struct uart_port *port)
1792 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1795 /* determine FIFO size */
1796 temp = lpuart32_read(&sport->port, UARTFIFO);
1798 sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_TXSIZE_OFF) &
1799 UARTFIFO_FIFOSIZE_MASK);
1800 sport->port.fifosize = sport->txfifo_size;
1802 sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_RXSIZE_OFF) &
1803 UARTFIFO_FIFOSIZE_MASK);
1806 * The LS1021A and LS1028A have a fixed FIFO depth of 16 words.
1807 * Although they support the RX/TXSIZE fields, their encoding is
1808 * different. Eg the reference manual states 0b101 is 16 words.
1810 if (is_layerscape_lpuart(sport)) {
1811 sport->rxfifo_size = 16;
1812 sport->txfifo_size = 16;
1813 sport->port.fifosize = sport->txfifo_size;
1816 lpuart_request_dma(sport);
1817 lpuart32_hw_setup(sport);
1822 static void lpuart_dma_shutdown(struct lpuart_port *sport)
1824 if (sport->lpuart_dma_rx_use) {
1825 lpuart_dma_rx_free(&sport->port);
1826 sport->lpuart_dma_rx_use = false;
1829 if (sport->lpuart_dma_tx_use) {
1830 if (wait_event_interruptible_timeout(sport->dma_wait,
1831 !sport->dma_tx_in_progress, msecs_to_jiffies(300)) <= 0) {
1832 sport->dma_tx_in_progress = false;
1833 dmaengine_terminate_sync(sport->dma_tx_chan);
1835 sport->lpuart_dma_tx_use = false;
1838 if (sport->dma_tx_chan)
1839 dma_release_channel(sport->dma_tx_chan);
1840 if (sport->dma_rx_chan)
1841 dma_release_channel(sport->dma_rx_chan);
1844 static void lpuart_shutdown(struct uart_port *port)
1846 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1848 unsigned long flags;
1850 spin_lock_irqsave(&port->lock, flags);
1852 /* disable Rx/Tx and interrupts */
1853 temp = readb(port->membase + UARTCR2);
1854 temp &= ~(UARTCR2_TE | UARTCR2_RE |
1855 UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1856 writeb(temp, port->membase + UARTCR2);
1858 spin_unlock_irqrestore(&port->lock, flags);
1860 lpuart_dma_shutdown(sport);
1863 static void lpuart32_shutdown(struct uart_port *port)
1865 struct lpuart_port *sport =
1866 container_of(port, struct lpuart_port, port);
1868 unsigned long flags;
1870 spin_lock_irqsave(&port->lock, flags);
1873 temp = lpuart32_read(&sport->port, UARTSTAT);
1874 lpuart32_write(&sport->port, temp, UARTSTAT);
1876 /* disable Rx/Tx DMA */
1877 temp = lpuart32_read(port, UARTBAUD);
1878 temp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
1879 lpuart32_write(port, temp, UARTBAUD);
1881 /* disable Rx/Tx and interrupts and break condition */
1882 temp = lpuart32_read(port, UARTCTRL);
1883 temp &= ~(UARTCTRL_TE | UARTCTRL_RE | UARTCTRL_ILIE |
1884 UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE | UARTCTRL_SBK);
1885 lpuart32_write(port, temp, UARTCTRL);
1887 spin_unlock_irqrestore(&port->lock, flags);
1889 lpuart_dma_shutdown(sport);
1893 lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
1894 const struct ktermios *old)
1896 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1897 unsigned long flags;
1898 unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
1900 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1901 unsigned int sbr, brfa;
1903 cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
1904 old_cr2 = readb(sport->port.membase + UARTCR2);
1905 cr3 = readb(sport->port.membase + UARTCR3);
1906 cr4 = readb(sport->port.membase + UARTCR4);
1907 bdh = readb(sport->port.membase + UARTBDH);
1908 modem = readb(sport->port.membase + UARTMODEM);
1910 * only support CS8 and CS7, and for CS7 must enable PE.
1917 while ((termios->c_cflag & CSIZE) != CS8 &&
1918 (termios->c_cflag & CSIZE) != CS7) {
1919 termios->c_cflag &= ~CSIZE;
1920 termios->c_cflag |= old_csize;
1924 if ((termios->c_cflag & CSIZE) == CS8 ||
1925 (termios->c_cflag & CSIZE) == CS7)
1926 cr1 = old_cr1 & ~UARTCR1_M;
1928 if (termios->c_cflag & CMSPAR) {
1929 if ((termios->c_cflag & CSIZE) != CS8) {
1930 termios->c_cflag &= ~CSIZE;
1931 termios->c_cflag |= CS8;
1937 * When auto RS-485 RTS mode is enabled,
1938 * hardware flow control need to be disabled.
1940 if (sport->port.rs485.flags & SER_RS485_ENABLED)
1941 termios->c_cflag &= ~CRTSCTS;
1943 if (termios->c_cflag & CRTSCTS)
1944 modem |= UARTMODEM_RXRTSE | UARTMODEM_TXCTSE;
1946 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1948 termios->c_cflag &= ~CSTOPB;
1950 /* parity must be enabled when CS7 to match 8-bits format */
1951 if ((termios->c_cflag & CSIZE) == CS7)
1952 termios->c_cflag |= PARENB;
1954 if (termios->c_cflag & PARENB) {
1955 if (termios->c_cflag & CMSPAR) {
1957 if (termios->c_cflag & PARODD)
1963 if ((termios->c_cflag & CSIZE) == CS8)
1965 if (termios->c_cflag & PARODD)
1974 /* ask the core to calculate the divisor */
1975 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1978 * Need to update the Ring buffer length according to the selected
1979 * baud rate and restart Rx DMA path.
1981 * Since timer function acqures sport->port.lock, need to stop before
1982 * acquring same lock because otherwise del_timer_sync() can deadlock.
1984 if (old && sport->lpuart_dma_rx_use)
1985 lpuart_dma_rx_free(&sport->port);
1987 spin_lock_irqsave(&sport->port.lock, flags);
1989 sport->port.read_status_mask = 0;
1990 if (termios->c_iflag & INPCK)
1991 sport->port.read_status_mask |= UARTSR1_FE | UARTSR1_PE;
1992 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1993 sport->port.read_status_mask |= UARTSR1_FE;
1995 /* characters to ignore */
1996 sport->port.ignore_status_mask = 0;
1997 if (termios->c_iflag & IGNPAR)
1998 sport->port.ignore_status_mask |= UARTSR1_PE;
1999 if (termios->c_iflag & IGNBRK) {
2000 sport->port.ignore_status_mask |= UARTSR1_FE;
2002 * if we're ignoring parity and break indicators,
2003 * ignore overruns too (for real raw support).
2005 if (termios->c_iflag & IGNPAR)
2006 sport->port.ignore_status_mask |= UARTSR1_OR;
2009 /* update the per-port timeout */
2010 uart_update_timeout(port, termios->c_cflag, baud);
2012 /* wait transmit engin complete */
2013 lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
2015 /* disable transmit and receive */
2016 writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
2017 sport->port.membase + UARTCR2);
2019 sbr = sport->port.uartclk / (16 * baud);
2020 brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
2021 bdh &= ~UARTBDH_SBR_MASK;
2022 bdh |= (sbr >> 8) & 0x1F;
2023 cr4 &= ~UARTCR4_BRFA_MASK;
2024 brfa &= UARTCR4_BRFA_MASK;
2025 writeb(cr4 | brfa, sport->port.membase + UARTCR4);
2026 writeb(bdh, sport->port.membase + UARTBDH);
2027 writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
2028 writeb(cr3, sport->port.membase + UARTCR3);
2029 writeb(cr1, sport->port.membase + UARTCR1);
2030 writeb(modem, sport->port.membase + UARTMODEM);
2032 /* restore control register */
2033 writeb(old_cr2, sport->port.membase + UARTCR2);
2035 if (old && sport->lpuart_dma_rx_use) {
2036 if (!lpuart_start_rx_dma(sport))
2037 rx_dma_timer_init(sport);
2039 sport->lpuart_dma_rx_use = false;
2042 spin_unlock_irqrestore(&sport->port.lock, flags);
2045 static void __lpuart32_serial_setbrg(struct uart_port *port,
2046 unsigned int baudrate, bool use_rx_dma,
2049 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
2050 u32 clk = port->uartclk;
2053 * The idea is to use the best OSR (over-sampling rate) possible.
2054 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
2055 * Loop to find the best OSR value possible, one that generates minimum
2056 * baud_diff iterate through the rest of the supported values of OSR.
2058 * Calculation Formula:
2059 * Baud Rate = baud clock / ((OSR+1) × SBR)
2061 baud_diff = baudrate;
2065 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
2066 /* calculate the temporary sbr value */
2067 tmp_sbr = (clk / (baudrate * tmp_osr));
2072 * calculate the baud rate difference based on the temporary
2073 * osr and sbr values
2075 tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
2077 /* select best values between sbr and sbr+1 */
2078 tmp = clk / (tmp_osr * (tmp_sbr + 1));
2079 if (tmp_diff > (baudrate - tmp)) {
2080 tmp_diff = baudrate - tmp;
2084 if (tmp_sbr > UARTBAUD_SBR_MASK)
2087 if (tmp_diff <= baud_diff) {
2088 baud_diff = tmp_diff;
2097 /* handle buadrate outside acceptable rate */
2098 if (baud_diff > ((baudrate / 100) * 3))
2100 "unacceptable baud rate difference of more than 3%%\n");
2102 tmp = lpuart32_read(port, UARTBAUD);
2104 if ((osr > 3) && (osr < 8))
2105 tmp |= UARTBAUD_BOTHEDGE;
2107 tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
2108 tmp |= ((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT;
2110 tmp &= ~UARTBAUD_SBR_MASK;
2111 tmp |= sbr & UARTBAUD_SBR_MASK;
2114 tmp &= ~UARTBAUD_RDMAE;
2116 tmp &= ~UARTBAUD_TDMAE;
2118 lpuart32_write(port, tmp, UARTBAUD);
2121 static void lpuart32_serial_setbrg(struct lpuart_port *sport,
2122 unsigned int baudrate)
2124 __lpuart32_serial_setbrg(&sport->port, baudrate,
2125 sport->lpuart_dma_rx_use,
2126 sport->lpuart_dma_tx_use);
2131 lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
2132 const struct ktermios *old)
2134 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
2135 unsigned long flags;
2136 unsigned long ctrl, old_ctrl, bd, modem;
2138 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
2140 ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
2141 bd = lpuart32_read(&sport->port, UARTBAUD);
2142 modem = lpuart32_read(&sport->port, UARTMODIR);
2143 sport->is_cs7 = false;
2145 * only support CS8 and CS7, and for CS7 must enable PE.
2152 while ((termios->c_cflag & CSIZE) != CS8 &&
2153 (termios->c_cflag & CSIZE) != CS7) {
2154 termios->c_cflag &= ~CSIZE;
2155 termios->c_cflag |= old_csize;
2159 if ((termios->c_cflag & CSIZE) == CS8 ||
2160 (termios->c_cflag & CSIZE) == CS7)
2161 ctrl = old_ctrl & ~UARTCTRL_M;
2163 if (termios->c_cflag & CMSPAR) {
2164 if ((termios->c_cflag & CSIZE) != CS8) {
2165 termios->c_cflag &= ~CSIZE;
2166 termios->c_cflag |= CS8;
2172 * When auto RS-485 RTS mode is enabled,
2173 * hardware flow control need to be disabled.
2175 if (sport->port.rs485.flags & SER_RS485_ENABLED)
2176 termios->c_cflag &= ~CRTSCTS;
2178 if (termios->c_cflag & CRTSCTS)
2179 modem |= UARTMODIR_RXRTSE | UARTMODIR_TXCTSE;
2181 modem &= ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
2183 if (termios->c_cflag & CSTOPB)
2184 bd |= UARTBAUD_SBNS;
2186 bd &= ~UARTBAUD_SBNS;
2188 /* parity must be enabled when CS7 to match 8-bits format */
2189 if ((termios->c_cflag & CSIZE) == CS7)
2190 termios->c_cflag |= PARENB;
2192 if ((termios->c_cflag & PARENB)) {
2193 if (termios->c_cflag & CMSPAR) {
2194 ctrl &= ~UARTCTRL_PE;
2197 ctrl |= UARTCTRL_PE;
2198 if ((termios->c_cflag & CSIZE) == CS8)
2200 if (termios->c_cflag & PARODD)
2201 ctrl |= UARTCTRL_PT;
2203 ctrl &= ~UARTCTRL_PT;
2206 ctrl &= ~UARTCTRL_PE;
2209 /* ask the core to calculate the divisor */
2210 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
2213 * Need to update the Ring buffer length according to the selected
2214 * baud rate and restart Rx DMA path.
2216 * Since timer function acqures sport->port.lock, need to stop before
2217 * acquring same lock because otherwise del_timer_sync() can deadlock.
2219 if (old && sport->lpuart_dma_rx_use)
2220 lpuart_dma_rx_free(&sport->port);
2222 spin_lock_irqsave(&sport->port.lock, flags);
2224 sport->port.read_status_mask = 0;
2225 if (termios->c_iflag & INPCK)
2226 sport->port.read_status_mask |= UARTSTAT_FE | UARTSTAT_PE;
2227 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2228 sport->port.read_status_mask |= UARTSTAT_FE;
2230 /* characters to ignore */
2231 sport->port.ignore_status_mask = 0;
2232 if (termios->c_iflag & IGNPAR)
2233 sport->port.ignore_status_mask |= UARTSTAT_PE;
2234 if (termios->c_iflag & IGNBRK) {
2235 sport->port.ignore_status_mask |= UARTSTAT_FE;
2237 * if we're ignoring parity and break indicators,
2238 * ignore overruns too (for real raw support).
2240 if (termios->c_iflag & IGNPAR)
2241 sport->port.ignore_status_mask |= UARTSTAT_OR;
2244 /* update the per-port timeout */
2245 uart_update_timeout(port, termios->c_cflag, baud);
2248 * LPUART Transmission Complete Flag may never be set while queuing a break
2249 * character, so skip waiting for transmission complete when UARTCTRL_SBK is
2252 if (!(old_ctrl & UARTCTRL_SBK)) {
2253 lpuart32_write(&sport->port, 0, UARTMODIR);
2254 lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2257 /* disable transmit and receive */
2258 lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
2261 lpuart32_write(&sport->port, bd, UARTBAUD);
2262 lpuart32_serial_setbrg(sport, baud);
2263 lpuart32_write(&sport->port, modem, UARTMODIR);
2264 lpuart32_write(&sport->port, ctrl, UARTCTRL);
2265 /* restore control register */
2267 if ((ctrl & (UARTCTRL_PE | UARTCTRL_M)) == UARTCTRL_PE)
2268 sport->is_cs7 = true;
2270 if (old && sport->lpuart_dma_rx_use) {
2271 if (!lpuart_start_rx_dma(sport))
2272 rx_dma_timer_init(sport);
2274 sport->lpuart_dma_rx_use = false;
2277 spin_unlock_irqrestore(&sport->port.lock, flags);
2280 static const char *lpuart_type(struct uart_port *port)
2282 return "FSL_LPUART";
2285 static void lpuart_release_port(struct uart_port *port)
2290 static int lpuart_request_port(struct uart_port *port)
2295 /* configure/autoconfigure the port */
2296 static void lpuart_config_port(struct uart_port *port, int flags)
2298 if (flags & UART_CONFIG_TYPE)
2299 port->type = PORT_LPUART;
2302 static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
2306 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
2308 if (port->irq != ser->irq)
2310 if (ser->io_type != UPIO_MEM)
2312 if (port->uartclk / 16 != ser->baud_base)
2314 if (port->iobase != ser->port)
2321 static const struct uart_ops lpuart_pops = {
2322 .tx_empty = lpuart_tx_empty,
2323 .set_mctrl = lpuart_set_mctrl,
2324 .get_mctrl = lpuart_get_mctrl,
2325 .stop_tx = lpuart_stop_tx,
2326 .start_tx = lpuart_start_tx,
2327 .stop_rx = lpuart_stop_rx,
2328 .break_ctl = lpuart_break_ctl,
2329 .startup = lpuart_startup,
2330 .shutdown = lpuart_shutdown,
2331 .set_termios = lpuart_set_termios,
2332 .pm = lpuart_uart_pm,
2333 .type = lpuart_type,
2334 .request_port = lpuart_request_port,
2335 .release_port = lpuart_release_port,
2336 .config_port = lpuart_config_port,
2337 .verify_port = lpuart_verify_port,
2338 .flush_buffer = lpuart_flush_buffer,
2339 #if defined(CONFIG_CONSOLE_POLL)
2340 .poll_init = lpuart_poll_init,
2341 .poll_get_char = lpuart_poll_get_char,
2342 .poll_put_char = lpuart_poll_put_char,
2346 static const struct uart_ops lpuart32_pops = {
2347 .tx_empty = lpuart32_tx_empty,
2348 .set_mctrl = lpuart32_set_mctrl,
2349 .get_mctrl = lpuart32_get_mctrl,
2350 .stop_tx = lpuart32_stop_tx,
2351 .start_tx = lpuart32_start_tx,
2352 .stop_rx = lpuart32_stop_rx,
2353 .break_ctl = lpuart32_break_ctl,
2354 .startup = lpuart32_startup,
2355 .shutdown = lpuart32_shutdown,
2356 .set_termios = lpuart32_set_termios,
2357 .pm = lpuart_uart_pm,
2358 .type = lpuart_type,
2359 .request_port = lpuart_request_port,
2360 .release_port = lpuart_release_port,
2361 .config_port = lpuart_config_port,
2362 .verify_port = lpuart_verify_port,
2363 .flush_buffer = lpuart_flush_buffer,
2364 #if defined(CONFIG_CONSOLE_POLL)
2365 .poll_init = lpuart32_poll_init,
2366 .poll_get_char = lpuart32_poll_get_char,
2367 .poll_put_char = lpuart32_poll_put_char,
2371 static struct lpuart_port *lpuart_ports[UART_NR];
2373 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
2374 static void lpuart_console_putchar(struct uart_port *port, unsigned char ch)
2376 lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
2377 writeb(ch, port->membase + UARTDR);
2380 static void lpuart32_console_putchar(struct uart_port *port, unsigned char ch)
2382 lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
2383 lpuart32_write(port, ch, UARTDATA);
2387 lpuart_console_write(struct console *co, const char *s, unsigned int count)
2389 struct lpuart_port *sport = lpuart_ports[co->index];
2390 unsigned char old_cr2, cr2;
2391 unsigned long flags;
2394 if (oops_in_progress)
2395 locked = spin_trylock_irqsave(&sport->port.lock, flags);
2397 spin_lock_irqsave(&sport->port.lock, flags);
2399 /* first save CR2 and then disable interrupts */
2400 cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
2401 cr2 |= UARTCR2_TE | UARTCR2_RE;
2402 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
2403 writeb(cr2, sport->port.membase + UARTCR2);
2405 uart_console_write(&sport->port, s, count, lpuart_console_putchar);
2407 /* wait for transmitter finish complete and restore CR2 */
2408 lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
2410 writeb(old_cr2, sport->port.membase + UARTCR2);
2413 spin_unlock_irqrestore(&sport->port.lock, flags);
2417 lpuart32_console_write(struct console *co, const char *s, unsigned int count)
2419 struct lpuart_port *sport = lpuart_ports[co->index];
2420 unsigned long old_cr, cr;
2421 unsigned long flags;
2424 if (oops_in_progress)
2425 locked = spin_trylock_irqsave(&sport->port.lock, flags);
2427 spin_lock_irqsave(&sport->port.lock, flags);
2429 /* first save CR2 and then disable interrupts */
2430 cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
2431 cr |= UARTCTRL_TE | UARTCTRL_RE;
2432 cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
2433 lpuart32_write(&sport->port, cr, UARTCTRL);
2435 uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
2437 /* wait for transmitter finish complete and restore CR2 */
2438 lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2440 lpuart32_write(&sport->port, old_cr, UARTCTRL);
2443 spin_unlock_irqrestore(&sport->port.lock, flags);
2447 * if the port was already initialised (eg, by a boot loader),
2448 * try to determine the current setup.
2451 lpuart_console_get_options(struct lpuart_port *sport, int *baud,
2452 int *parity, int *bits)
2454 unsigned char cr, bdh, bdl, brfa;
2455 unsigned int sbr, uartclk, baud_raw;
2457 cr = readb(sport->port.membase + UARTCR2);
2458 cr &= UARTCR2_TE | UARTCR2_RE;
2462 /* ok, the port was enabled */
2464 cr = readb(sport->port.membase + UARTCR1);
2467 if (cr & UARTCR1_PE) {
2468 if (cr & UARTCR1_PT)
2479 bdh = readb(sport->port.membase + UARTBDH);
2480 bdh &= UARTBDH_SBR_MASK;
2481 bdl = readb(sport->port.membase + UARTBDL);
2485 brfa = readb(sport->port.membase + UARTCR4);
2486 brfa &= UARTCR4_BRFA_MASK;
2488 uartclk = lpuart_get_baud_clk_rate(sport);
2490 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2492 baud_raw = uartclk / (16 * (sbr + brfa / 32));
2494 if (*baud != baud_raw)
2495 dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2496 "from %d to %d\n", baud_raw, *baud);
2500 lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
2501 int *parity, int *bits)
2503 unsigned long cr, bd;
2504 unsigned int sbr, uartclk, baud_raw;
2506 cr = lpuart32_read(&sport->port, UARTCTRL);
2507 cr &= UARTCTRL_TE | UARTCTRL_RE;
2511 /* ok, the port was enabled */
2513 cr = lpuart32_read(&sport->port, UARTCTRL);
2516 if (cr & UARTCTRL_PE) {
2517 if (cr & UARTCTRL_PT)
2523 if (cr & UARTCTRL_M)
2528 bd = lpuart32_read(&sport->port, UARTBAUD);
2529 bd &= UARTBAUD_SBR_MASK;
2534 uartclk = lpuart_get_baud_clk_rate(sport);
2536 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2538 baud_raw = uartclk / (16 * sbr);
2540 if (*baud != baud_raw)
2541 dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2542 "from %d to %d\n", baud_raw, *baud);
2545 static int __init lpuart_console_setup(struct console *co, char *options)
2547 struct lpuart_port *sport;
2554 * check whether an invalid uart number has been specified, and
2555 * if so, search for the first available port that does have
2558 if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
2561 sport = lpuart_ports[co->index];
2566 uart_parse_options(options, &baud, &parity, &bits, &flow);
2568 if (lpuart_is_32(sport))
2569 lpuart32_console_get_options(sport, &baud, &parity, &bits);
2571 lpuart_console_get_options(sport, &baud, &parity, &bits);
2573 if (lpuart_is_32(sport))
2574 lpuart32_setup_watermark(sport);
2576 lpuart_setup_watermark(sport);
2578 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
2581 static struct uart_driver lpuart_reg;
2582 static struct console lpuart_console = {
2584 .write = lpuart_console_write,
2585 .device = uart_console_device,
2586 .setup = lpuart_console_setup,
2587 .flags = CON_PRINTBUFFER,
2589 .data = &lpuart_reg,
2592 static struct console lpuart32_console = {
2594 .write = lpuart32_console_write,
2595 .device = uart_console_device,
2596 .setup = lpuart_console_setup,
2597 .flags = CON_PRINTBUFFER,
2599 .data = &lpuart_reg,
2602 static void lpuart_early_write(struct console *con, const char *s, unsigned n)
2604 struct earlycon_device *dev = con->data;
2606 uart_console_write(&dev->port, s, n, lpuart_console_putchar);
2609 static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
2611 struct earlycon_device *dev = con->data;
2613 uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
2616 static int __init lpuart_early_console_setup(struct earlycon_device *device,
2619 if (!device->port.membase)
2622 device->con->write = lpuart_early_write;
2626 static int __init lpuart32_early_console_setup(struct earlycon_device *device,
2629 if (!device->port.membase)
2632 if (device->port.iotype != UPIO_MEM32)
2633 device->port.iotype = UPIO_MEM32BE;
2635 device->con->write = lpuart32_early_write;
2639 static int __init ls1028a_early_console_setup(struct earlycon_device *device,
2644 if (!device->port.membase)
2647 device->port.iotype = UPIO_MEM32;
2648 device->con->write = lpuart32_early_write;
2650 /* set the baudrate */
2651 if (device->port.uartclk && device->baud)
2652 __lpuart32_serial_setbrg(&device->port, device->baud,
2655 /* enable transmitter */
2656 cr = lpuart32_read(&device->port, UARTCTRL);
2658 lpuart32_write(&device->port, cr, UARTCTRL);
2663 static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
2666 if (!device->port.membase)
2669 device->port.iotype = UPIO_MEM32;
2670 device->port.membase += IMX_REG_OFF;
2671 device->con->write = lpuart32_early_write;
2675 OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
2676 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2677 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1028a-lpuart", ls1028a_early_console_setup);
2678 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2679 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8qxp-lpuart", lpuart32_imx_early_console_setup);
2680 OF_EARLYCON_DECLARE(lpuart32, "fsl,imxrt1050-lpuart", lpuart32_imx_early_console_setup);
2681 EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
2682 EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2684 #define LPUART_CONSOLE (&lpuart_console)
2685 #define LPUART32_CONSOLE (&lpuart32_console)
2687 #define LPUART_CONSOLE NULL
2688 #define LPUART32_CONSOLE NULL
2691 static struct uart_driver lpuart_reg = {
2692 .owner = THIS_MODULE,
2693 .driver_name = DRIVER_NAME,
2694 .dev_name = DEV_NAME,
2695 .nr = ARRAY_SIZE(lpuart_ports),
2696 .cons = LPUART_CONSOLE,
2699 static const struct serial_rs485 lpuart_rs485_supported = {
2700 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
2701 /* delay_rts_* and RX_DURING_TX are not supported */
2704 static int lpuart_global_reset(struct lpuart_port *sport)
2706 struct uart_port *port = &sport->port;
2707 void __iomem *global_addr;
2708 unsigned long ctrl, bd;
2709 unsigned int val = 0;
2712 ret = clk_prepare_enable(sport->ipg_clk);
2714 dev_err(sport->port.dev, "failed to enable uart ipg clk: %d\n", ret);
2718 if (is_imx7ulp_lpuart(sport) || is_imx8ulp_lpuart(sport) || is_imx8qxp_lpuart(sport)) {
2720 * If the transmitter is used by earlycon, wait for transmit engine to
2721 * complete and then reset.
2723 ctrl = lpuart32_read(port, UARTCTRL);
2724 if (ctrl & UARTCTRL_TE) {
2725 bd = lpuart32_read(&sport->port, UARTBAUD);
2726 if (read_poll_timeout(lpuart32_tx_empty, val, val, 1, 100000, false,
2728 dev_warn(sport->port.dev,
2729 "timeout waiting for transmit engine to complete\n");
2730 clk_disable_unprepare(sport->ipg_clk);
2735 global_addr = port->membase + UART_GLOBAL - IMX_REG_OFF;
2736 writel(UART_GLOBAL_RST, global_addr);
2737 usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
2738 writel(0, global_addr);
2739 usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
2741 /* Recover the transmitter for earlycon. */
2742 if (ctrl & UARTCTRL_TE) {
2743 lpuart32_write(port, bd, UARTBAUD);
2744 lpuart32_write(port, ctrl, UARTCTRL);
2748 clk_disable_unprepare(sport->ipg_clk);
2752 static int lpuart_probe(struct platform_device *pdev)
2754 const struct lpuart_soc_data *sdata = of_device_get_match_data(&pdev->dev);
2755 struct device_node *np = pdev->dev.of_node;
2756 struct lpuart_port *sport;
2757 struct resource *res;
2758 irq_handler_t handler;
2761 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2765 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2766 sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
2767 if (IS_ERR(sport->port.membase))
2768 return PTR_ERR(sport->port.membase);
2770 sport->port.membase += sdata->reg_off;
2771 sport->port.mapbase = res->start + sdata->reg_off;
2772 sport->port.dev = &pdev->dev;
2773 sport->port.type = PORT_LPUART;
2774 sport->devtype = sdata->devtype;
2775 sport->rx_watermark = sdata->rx_watermark;
2776 ret = platform_get_irq(pdev, 0);
2779 sport->port.irq = ret;
2780 sport->port.iotype = sdata->iotype;
2781 if (lpuart_is_32(sport))
2782 sport->port.ops = &lpuart32_pops;
2784 sport->port.ops = &lpuart_pops;
2785 sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_FSL_LPUART_CONSOLE);
2786 sport->port.flags = UPF_BOOT_AUTOCONF;
2788 if (lpuart_is_32(sport))
2789 sport->port.rs485_config = lpuart32_config_rs485;
2791 sport->port.rs485_config = lpuart_config_rs485;
2792 sport->port.rs485_supported = lpuart_rs485_supported;
2794 sport->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
2795 if (IS_ERR(sport->ipg_clk)) {
2796 ret = PTR_ERR(sport->ipg_clk);
2797 dev_err(&pdev->dev, "failed to get uart ipg clk: %d\n", ret);
2801 sport->baud_clk = NULL;
2802 if (is_imx8qxp_lpuart(sport)) {
2803 sport->baud_clk = devm_clk_get(&pdev->dev, "baud");
2804 if (IS_ERR(sport->baud_clk)) {
2805 ret = PTR_ERR(sport->baud_clk);
2806 dev_err(&pdev->dev, "failed to get uart baud clk: %d\n", ret);
2811 ret = of_alias_get_id(np, "serial");
2813 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2816 if (ret >= ARRAY_SIZE(lpuart_ports)) {
2817 dev_err(&pdev->dev, "serial%d out of range\n", ret);
2820 sport->port.line = ret;
2822 ret = lpuart_enable_clks(sport);
2825 sport->port.uartclk = lpuart_get_baud_clk_rate(sport);
2827 lpuart_ports[sport->port.line] = sport;
2829 platform_set_drvdata(pdev, &sport->port);
2831 if (lpuart_is_32(sport)) {
2832 lpuart_reg.cons = LPUART32_CONSOLE;
2833 handler = lpuart32_int;
2835 lpuart_reg.cons = LPUART_CONSOLE;
2836 handler = lpuart_int;
2839 pm_runtime_use_autosuspend(&pdev->dev);
2840 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
2841 pm_runtime_set_active(&pdev->dev);
2842 pm_runtime_enable(&pdev->dev);
2844 ret = lpuart_global_reset(sport);
2848 ret = uart_get_rs485_mode(&sport->port);
2850 goto failed_get_rs485;
2852 ret = uart_add_one_port(&lpuart_reg, &sport->port);
2854 goto failed_attach_port;
2856 ret = devm_request_irq(&pdev->dev, sport->port.irq, handler, 0,
2857 DRIVER_NAME, sport);
2859 goto failed_irq_request;
2864 uart_remove_one_port(&lpuart_reg, &sport->port);
2868 pm_runtime_disable(&pdev->dev);
2869 pm_runtime_set_suspended(&pdev->dev);
2870 pm_runtime_dont_use_autosuspend(&pdev->dev);
2871 lpuart_disable_clks(sport);
2875 static int lpuart_remove(struct platform_device *pdev)
2877 struct lpuart_port *sport = platform_get_drvdata(pdev);
2879 uart_remove_one_port(&lpuart_reg, &sport->port);
2881 lpuart_disable_clks(sport);
2883 if (sport->dma_tx_chan)
2884 dma_release_channel(sport->dma_tx_chan);
2886 if (sport->dma_rx_chan)
2887 dma_release_channel(sport->dma_rx_chan);
2889 pm_runtime_disable(&pdev->dev);
2890 pm_runtime_set_suspended(&pdev->dev);
2891 pm_runtime_dont_use_autosuspend(&pdev->dev);
2895 static int lpuart_runtime_suspend(struct device *dev)
2897 struct platform_device *pdev = to_platform_device(dev);
2898 struct lpuart_port *sport = platform_get_drvdata(pdev);
2900 lpuart_disable_clks(sport);
2905 static int lpuart_runtime_resume(struct device *dev)
2907 struct platform_device *pdev = to_platform_device(dev);
2908 struct lpuart_port *sport = platform_get_drvdata(pdev);
2910 return lpuart_enable_clks(sport);
2913 static void serial_lpuart_enable_wakeup(struct lpuart_port *sport, bool on)
2915 unsigned int val, baud;
2917 if (lpuart_is_32(sport)) {
2918 val = lpuart32_read(&sport->port, UARTCTRL);
2919 baud = lpuart32_read(&sport->port, UARTBAUD);
2921 /* set rx_watermark to 0 in wakeup source mode */
2922 lpuart32_write(&sport->port, 0, UARTWATER);
2923 val |= UARTCTRL_RIE;
2924 /* clear RXEDGIF flag before enable RXEDGIE interrupt */
2925 lpuart32_write(&sport->port, UARTSTAT_RXEDGIF, UARTSTAT);
2926 baud |= UARTBAUD_RXEDGIE;
2928 val &= ~UARTCTRL_RIE;
2929 baud &= ~UARTBAUD_RXEDGIE;
2931 lpuart32_write(&sport->port, val, UARTCTRL);
2932 lpuart32_write(&sport->port, baud, UARTBAUD);
2934 val = readb(sport->port.membase + UARTCR2);
2938 val &= ~UARTCR2_RIE;
2939 writeb(val, sport->port.membase + UARTCR2);
2943 static bool lpuart_uport_is_active(struct lpuart_port *sport)
2945 struct tty_port *port = &sport->port.state->port;
2946 struct tty_struct *tty;
2947 struct device *tty_dev;
2950 tty = tty_port_tty_get(port);
2953 may_wake = tty_dev && device_may_wakeup(tty_dev);
2957 if ((tty_port_initialized(port) && may_wake) ||
2958 (!console_suspend_enabled && uart_console(&sport->port)))
2964 static int lpuart_suspend_noirq(struct device *dev)
2966 struct lpuart_port *sport = dev_get_drvdata(dev);
2967 bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2969 if (lpuart_uport_is_active(sport))
2970 serial_lpuart_enable_wakeup(sport, !!irq_wake);
2972 pinctrl_pm_select_sleep_state(dev);
2977 static int lpuart_resume_noirq(struct device *dev)
2979 struct lpuart_port *sport = dev_get_drvdata(dev);
2982 pinctrl_pm_select_default_state(dev);
2984 if (lpuart_uport_is_active(sport)) {
2985 serial_lpuart_enable_wakeup(sport, false);
2987 /* clear the wakeup flags */
2988 if (lpuart_is_32(sport)) {
2989 val = lpuart32_read(&sport->port, UARTSTAT);
2990 lpuart32_write(&sport->port, val, UARTSTAT);
2997 static int lpuart_suspend(struct device *dev)
2999 struct lpuart_port *sport = dev_get_drvdata(dev);
3000 unsigned long temp, flags;
3002 uart_suspend_port(&lpuart_reg, &sport->port);
3004 if (lpuart_uport_is_active(sport)) {
3005 spin_lock_irqsave(&sport->port.lock, flags);
3006 if (lpuart_is_32(sport)) {
3007 /* disable Rx/Tx and interrupts */
3008 temp = lpuart32_read(&sport->port, UARTCTRL);
3009 temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
3010 lpuart32_write(&sport->port, temp, UARTCTRL);
3012 /* disable Rx/Tx and interrupts */
3013 temp = readb(sport->port.membase + UARTCR2);
3014 temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
3015 writeb(temp, sport->port.membase + UARTCR2);
3017 spin_unlock_irqrestore(&sport->port.lock, flags);
3019 if (sport->lpuart_dma_rx_use) {
3021 * EDMA driver during suspend will forcefully release any
3022 * non-idle DMA channels. If port wakeup is enabled or if port
3023 * is console port or 'no_console_suspend' is set the Rx DMA
3024 * cannot resume as expected, hence gracefully release the
3025 * Rx DMA path before suspend and start Rx DMA path on resume.
3027 lpuart_dma_rx_free(&sport->port);
3029 /* Disable Rx DMA to use UART port as wakeup source */
3030 spin_lock_irqsave(&sport->port.lock, flags);
3031 if (lpuart_is_32(sport)) {
3032 temp = lpuart32_read(&sport->port, UARTBAUD);
3033 lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
3036 writeb(readb(sport->port.membase + UARTCR5) &
3037 ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
3039 spin_unlock_irqrestore(&sport->port.lock, flags);
3042 if (sport->lpuart_dma_tx_use) {
3043 spin_lock_irqsave(&sport->port.lock, flags);
3044 if (lpuart_is_32(sport)) {
3045 temp = lpuart32_read(&sport->port, UARTBAUD);
3046 temp &= ~UARTBAUD_TDMAE;
3047 lpuart32_write(&sport->port, temp, UARTBAUD);
3049 temp = readb(sport->port.membase + UARTCR5);
3050 temp &= ~UARTCR5_TDMAS;
3051 writeb(temp, sport->port.membase + UARTCR5);
3053 spin_unlock_irqrestore(&sport->port.lock, flags);
3054 sport->dma_tx_in_progress = false;
3055 dmaengine_terminate_sync(sport->dma_tx_chan);
3057 } else if (pm_runtime_active(sport->port.dev)) {
3058 lpuart_disable_clks(sport);
3059 pm_runtime_disable(sport->port.dev);
3060 pm_runtime_set_suspended(sport->port.dev);
3066 static void lpuart_console_fixup(struct lpuart_port *sport)
3068 struct tty_port *port = &sport->port.state->port;
3069 struct uart_port *uport = &sport->port;
3070 struct ktermios termios;
3072 /* i.MX7ULP enter VLLS mode that lpuart module power off and registers
3073 * all lost no matter the port is wakeup source.
3074 * For console port, console baud rate setting lost and print messy
3075 * log when enable the console port as wakeup source. To avoid the
3076 * issue happen, user should not enable uart port as wakeup source
3077 * in VLLS mode, or restore console setting here.
3079 if (is_imx7ulp_lpuart(sport) && lpuart_uport_is_active(sport) &&
3080 console_suspend_enabled && uart_console(&sport->port)) {
3082 mutex_lock(&port->mutex);
3083 memset(&termios, 0, sizeof(struct ktermios));
3084 termios.c_cflag = uport->cons->cflag;
3085 if (port->tty && termios.c_cflag == 0)
3086 termios = port->tty->termios;
3087 uport->ops->set_termios(uport, &termios, NULL);
3088 mutex_unlock(&port->mutex);
3092 static int lpuart_resume(struct device *dev)
3094 struct lpuart_port *sport = dev_get_drvdata(dev);
3097 if (lpuart_uport_is_active(sport)) {
3098 if (lpuart_is_32(sport))
3099 lpuart32_hw_setup(sport);
3101 lpuart_hw_setup(sport);
3102 } else if (pm_runtime_active(sport->port.dev)) {
3103 ret = lpuart_enable_clks(sport);
3106 pm_runtime_set_active(sport->port.dev);
3107 pm_runtime_enable(sport->port.dev);
3110 lpuart_console_fixup(sport);
3111 uart_resume_port(&lpuart_reg, &sport->port);
3116 static const struct dev_pm_ops lpuart_pm_ops = {
3117 RUNTIME_PM_OPS(lpuart_runtime_suspend,
3118 lpuart_runtime_resume, NULL)
3119 NOIRQ_SYSTEM_SLEEP_PM_OPS(lpuart_suspend_noirq,
3120 lpuart_resume_noirq)
3121 SYSTEM_SLEEP_PM_OPS(lpuart_suspend, lpuart_resume)
3124 static struct platform_driver lpuart_driver = {
3125 .probe = lpuart_probe,
3126 .remove = lpuart_remove,
3128 .name = "fsl-lpuart",
3129 .of_match_table = lpuart_dt_ids,
3130 .pm = pm_ptr(&lpuart_pm_ops),
3134 static int __init lpuart_serial_init(void)
3136 int ret = uart_register_driver(&lpuart_reg);
3141 ret = platform_driver_register(&lpuart_driver);
3143 uart_unregister_driver(&lpuart_reg);
3148 static void __exit lpuart_serial_exit(void)
3150 platform_driver_unregister(&lpuart_driver);
3151 uart_unregister_driver(&lpuart_reg);
3154 module_init(lpuart_serial_init);
3155 module_exit(lpuart_serial_exit);
3157 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
3158 MODULE_LICENSE("GPL v2");