1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale lpuart serial port driver
5 * Copyright 2012-2014 Freescale Semiconductor, Inc.
9 #include <linux/console.h>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dmapool.h>
15 #include <linux/iopoll.h>
16 #include <linux/irq.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_dma.h>
21 #include <linux/serial_core.h>
22 #include <linux/slab.h>
23 #include <linux/tty_flip.h>
25 /* All registers are 8-bit width */
35 #define UARTMODEM 0x0d
36 #define UARTPFIFO 0x10
37 #define UARTCFIFO 0x11
38 #define UARTSFIFO 0x12
39 #define UARTTWFIFO 0x13
40 #define UARTTCFIFO 0x14
41 #define UARTRWFIFO 0x15
43 #define UARTBDH_LBKDIE 0x80
44 #define UARTBDH_RXEDGIE 0x40
45 #define UARTBDH_SBR_MASK 0x1f
47 #define UARTCR1_LOOPS 0x80
48 #define UARTCR1_RSRC 0x20
49 #define UARTCR1_M 0x10
50 #define UARTCR1_WAKE 0x08
51 #define UARTCR1_ILT 0x04
52 #define UARTCR1_PE 0x02
53 #define UARTCR1_PT 0x01
55 #define UARTCR2_TIE 0x80
56 #define UARTCR2_TCIE 0x40
57 #define UARTCR2_RIE 0x20
58 #define UARTCR2_ILIE 0x10
59 #define UARTCR2_TE 0x08
60 #define UARTCR2_RE 0x04
61 #define UARTCR2_RWU 0x02
62 #define UARTCR2_SBK 0x01
64 #define UARTSR1_TDRE 0x80
65 #define UARTSR1_TC 0x40
66 #define UARTSR1_RDRF 0x20
67 #define UARTSR1_IDLE 0x10
68 #define UARTSR1_OR 0x08
69 #define UARTSR1_NF 0x04
70 #define UARTSR1_FE 0x02
71 #define UARTSR1_PE 0x01
73 #define UARTCR3_R8 0x80
74 #define UARTCR3_T8 0x40
75 #define UARTCR3_TXDIR 0x20
76 #define UARTCR3_TXINV 0x10
77 #define UARTCR3_ORIE 0x08
78 #define UARTCR3_NEIE 0x04
79 #define UARTCR3_FEIE 0x02
80 #define UARTCR3_PEIE 0x01
82 #define UARTCR4_MAEN1 0x80
83 #define UARTCR4_MAEN2 0x40
84 #define UARTCR4_M10 0x20
85 #define UARTCR4_BRFA_MASK 0x1f
86 #define UARTCR4_BRFA_OFF 0
88 #define UARTCR5_TDMAS 0x80
89 #define UARTCR5_RDMAS 0x20
91 #define UARTMODEM_RXRTSE 0x08
92 #define UARTMODEM_TXRTSPOL 0x04
93 #define UARTMODEM_TXRTSE 0x02
94 #define UARTMODEM_TXCTSE 0x01
96 #define UARTPFIFO_TXFE 0x80
97 #define UARTPFIFO_FIFOSIZE_MASK 0x7
98 #define UARTPFIFO_TXSIZE_OFF 4
99 #define UARTPFIFO_RXFE 0x08
100 #define UARTPFIFO_RXSIZE_OFF 0
102 #define UARTCFIFO_TXFLUSH 0x80
103 #define UARTCFIFO_RXFLUSH 0x40
104 #define UARTCFIFO_RXOFE 0x04
105 #define UARTCFIFO_TXOFE 0x02
106 #define UARTCFIFO_RXUFE 0x01
108 #define UARTSFIFO_TXEMPT 0x80
109 #define UARTSFIFO_RXEMPT 0x40
110 #define UARTSFIFO_RXOF 0x04
111 #define UARTSFIFO_TXOF 0x02
112 #define UARTSFIFO_RXUF 0x01
114 /* 32-bit global registers only for i.MX7ULP/i.MX8x
115 * Used to reset all internal logic and registers, except the Global Register.
117 #define UART_GLOBAL 0x8
119 /* 32-bit register definition */
120 #define UARTBAUD 0x00
121 #define UARTSTAT 0x04
122 #define UARTCTRL 0x08
123 #define UARTDATA 0x0C
124 #define UARTMATCH 0x10
125 #define UARTMODIR 0x14
126 #define UARTFIFO 0x18
127 #define UARTWATER 0x1c
129 #define UARTBAUD_MAEN1 0x80000000
130 #define UARTBAUD_MAEN2 0x40000000
131 #define UARTBAUD_M10 0x20000000
132 #define UARTBAUD_TDMAE 0x00800000
133 #define UARTBAUD_RDMAE 0x00200000
134 #define UARTBAUD_MATCFG 0x00400000
135 #define UARTBAUD_BOTHEDGE 0x00020000
136 #define UARTBAUD_RESYNCDIS 0x00010000
137 #define UARTBAUD_LBKDIE 0x00008000
138 #define UARTBAUD_RXEDGIE 0x00004000
139 #define UARTBAUD_SBNS 0x00002000
140 #define UARTBAUD_SBR 0x00000000
141 #define UARTBAUD_SBR_MASK 0x1fff
142 #define UARTBAUD_OSR_MASK 0x1f
143 #define UARTBAUD_OSR_SHIFT 24
145 #define UARTSTAT_LBKDIF 0x80000000
146 #define UARTSTAT_RXEDGIF 0x40000000
147 #define UARTSTAT_MSBF 0x20000000
148 #define UARTSTAT_RXINV 0x10000000
149 #define UARTSTAT_RWUID 0x08000000
150 #define UARTSTAT_BRK13 0x04000000
151 #define UARTSTAT_LBKDE 0x02000000
152 #define UARTSTAT_RAF 0x01000000
153 #define UARTSTAT_TDRE 0x00800000
154 #define UARTSTAT_TC 0x00400000
155 #define UARTSTAT_RDRF 0x00200000
156 #define UARTSTAT_IDLE 0x00100000
157 #define UARTSTAT_OR 0x00080000
158 #define UARTSTAT_NF 0x00040000
159 #define UARTSTAT_FE 0x00020000
160 #define UARTSTAT_PE 0x00010000
161 #define UARTSTAT_MA1F 0x00008000
162 #define UARTSTAT_M21F 0x00004000
164 #define UARTCTRL_R8T9 0x80000000
165 #define UARTCTRL_R9T8 0x40000000
166 #define UARTCTRL_TXDIR 0x20000000
167 #define UARTCTRL_TXINV 0x10000000
168 #define UARTCTRL_ORIE 0x08000000
169 #define UARTCTRL_NEIE 0x04000000
170 #define UARTCTRL_FEIE 0x02000000
171 #define UARTCTRL_PEIE 0x01000000
172 #define UARTCTRL_TIE 0x00800000
173 #define UARTCTRL_TCIE 0x00400000
174 #define UARTCTRL_RIE 0x00200000
175 #define UARTCTRL_ILIE 0x00100000
176 #define UARTCTRL_TE 0x00080000
177 #define UARTCTRL_RE 0x00040000
178 #define UARTCTRL_RWU 0x00020000
179 #define UARTCTRL_SBK 0x00010000
180 #define UARTCTRL_MA1IE 0x00008000
181 #define UARTCTRL_MA2IE 0x00004000
182 #define UARTCTRL_IDLECFG 0x00000100
183 #define UARTCTRL_LOOPS 0x00000080
184 #define UARTCTRL_DOZEEN 0x00000040
185 #define UARTCTRL_RSRC 0x00000020
186 #define UARTCTRL_M 0x00000010
187 #define UARTCTRL_WAKE 0x00000008
188 #define UARTCTRL_ILT 0x00000004
189 #define UARTCTRL_PE 0x00000002
190 #define UARTCTRL_PT 0x00000001
192 #define UARTDATA_NOISY 0x00008000
193 #define UARTDATA_PARITYE 0x00004000
194 #define UARTDATA_FRETSC 0x00002000
195 #define UARTDATA_RXEMPT 0x00001000
196 #define UARTDATA_IDLINE 0x00000800
197 #define UARTDATA_MASK 0x3ff
199 #define UARTMODIR_IREN 0x00020000
200 #define UARTMODIR_TXCTSSRC 0x00000020
201 #define UARTMODIR_TXCTSC 0x00000010
202 #define UARTMODIR_RXRTSE 0x00000008
203 #define UARTMODIR_TXRTSPOL 0x00000004
204 #define UARTMODIR_TXRTSE 0x00000002
205 #define UARTMODIR_TXCTSE 0x00000001
207 #define UARTFIFO_TXEMPT 0x00800000
208 #define UARTFIFO_RXEMPT 0x00400000
209 #define UARTFIFO_TXOF 0x00020000
210 #define UARTFIFO_RXUF 0x00010000
211 #define UARTFIFO_TXFLUSH 0x00008000
212 #define UARTFIFO_RXFLUSH 0x00004000
213 #define UARTFIFO_TXOFE 0x00000200
214 #define UARTFIFO_RXUFE 0x00000100
215 #define UARTFIFO_TXFE 0x00000080
216 #define UARTFIFO_FIFOSIZE_MASK 0x7
217 #define UARTFIFO_TXSIZE_OFF 4
218 #define UARTFIFO_RXFE 0x00000008
219 #define UARTFIFO_RXSIZE_OFF 0
220 #define UARTFIFO_DEPTH(x) (0x1 << ((x) ? ((x) + 1) : 0))
222 #define UARTWATER_COUNT_MASK 0xff
223 #define UARTWATER_TXCNT_OFF 8
224 #define UARTWATER_RXCNT_OFF 24
225 #define UARTWATER_WATER_MASK 0xff
226 #define UARTWATER_TXWATER_OFF 0
227 #define UARTWATER_RXWATER_OFF 16
229 #define UART_GLOBAL_RST 0x2
230 #define GLOBAL_RST_MIN_US 20
231 #define GLOBAL_RST_MAX_US 40
233 /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
234 #define DMA_RX_TIMEOUT (10)
236 #define DRIVER_NAME "fsl-lpuart"
237 #define DEV_NAME "ttyLP"
240 /* IMX lpuart has four extra unused regs located at the beginning */
241 #define IMX_REG_OFF 0x10
253 struct uart_port port;
254 enum lpuart_type devtype;
256 struct clk *baud_clk;
257 unsigned int txfifo_size;
258 unsigned int rxfifo_size;
260 bool lpuart_dma_tx_use;
261 bool lpuart_dma_rx_use;
262 struct dma_chan *dma_tx_chan;
263 struct dma_chan *dma_rx_chan;
264 struct dma_async_tx_descriptor *dma_tx_desc;
265 struct dma_async_tx_descriptor *dma_rx_desc;
266 dma_cookie_t dma_tx_cookie;
267 dma_cookie_t dma_rx_cookie;
268 unsigned int dma_tx_bytes;
269 unsigned int dma_rx_bytes;
270 bool dma_tx_in_progress;
271 unsigned int dma_rx_timeout;
272 struct timer_list lpuart_timer;
273 struct scatterlist rx_sgl, tx_sgl[2];
274 struct circ_buf rx_ring;
275 int rx_dma_rng_buf_len;
276 unsigned int dma_tx_nents;
277 wait_queue_head_t dma_wait;
278 bool is_cs7; /* Set to true when character size is 7 */
279 /* and the parity is enabled */
282 struct lpuart_soc_data {
283 enum lpuart_type devtype;
288 static const struct lpuart_soc_data vf_data = {
289 .devtype = VF610_LPUART,
293 static const struct lpuart_soc_data ls1021a_data = {
294 .devtype = LS1021A_LPUART,
295 .iotype = UPIO_MEM32BE,
298 static const struct lpuart_soc_data ls1028a_data = {
299 .devtype = LS1028A_LPUART,
300 .iotype = UPIO_MEM32,
303 static struct lpuart_soc_data imx7ulp_data = {
304 .devtype = IMX7ULP_LPUART,
305 .iotype = UPIO_MEM32,
306 .reg_off = IMX_REG_OFF,
309 static struct lpuart_soc_data imx8qxp_data = {
310 .devtype = IMX8QXP_LPUART,
311 .iotype = UPIO_MEM32,
312 .reg_off = IMX_REG_OFF,
314 static struct lpuart_soc_data imxrt1050_data = {
315 .devtype = IMXRT1050_LPUART,
316 .iotype = UPIO_MEM32,
317 .reg_off = IMX_REG_OFF,
320 static const struct of_device_id lpuart_dt_ids[] = {
321 { .compatible = "fsl,vf610-lpuart", .data = &vf_data, },
322 { .compatible = "fsl,ls1021a-lpuart", .data = &ls1021a_data, },
323 { .compatible = "fsl,ls1028a-lpuart", .data = &ls1028a_data, },
324 { .compatible = "fsl,imx7ulp-lpuart", .data = &imx7ulp_data, },
325 { .compatible = "fsl,imx8qxp-lpuart", .data = &imx8qxp_data, },
326 { .compatible = "fsl,imxrt1050-lpuart", .data = &imxrt1050_data},
329 MODULE_DEVICE_TABLE(of, lpuart_dt_ids);
331 /* Forward declare this for the dma callbacks*/
332 static void lpuart_dma_tx_complete(void *arg);
334 static inline bool is_layerscape_lpuart(struct lpuart_port *sport)
336 return (sport->devtype == LS1021A_LPUART ||
337 sport->devtype == LS1028A_LPUART);
340 static inline bool is_imx7ulp_lpuart(struct lpuart_port *sport)
342 return sport->devtype == IMX7ULP_LPUART;
345 static inline bool is_imx8qxp_lpuart(struct lpuart_port *sport)
347 return sport->devtype == IMX8QXP_LPUART;
350 static inline u32 lpuart32_read(struct uart_port *port, u32 off)
352 switch (port->iotype) {
354 return readl(port->membase + off);
356 return ioread32be(port->membase + off);
362 static inline void lpuart32_write(struct uart_port *port, u32 val,
365 switch (port->iotype) {
367 writel(val, port->membase + off);
370 iowrite32be(val, port->membase + off);
375 static int __lpuart_enable_clks(struct lpuart_port *sport, bool is_en)
380 ret = clk_prepare_enable(sport->ipg_clk);
384 ret = clk_prepare_enable(sport->baud_clk);
386 clk_disable_unprepare(sport->ipg_clk);
390 clk_disable_unprepare(sport->baud_clk);
391 clk_disable_unprepare(sport->ipg_clk);
397 static unsigned int lpuart_get_baud_clk_rate(struct lpuart_port *sport)
399 if (is_imx8qxp_lpuart(sport))
400 return clk_get_rate(sport->baud_clk);
402 return clk_get_rate(sport->ipg_clk);
405 #define lpuart_enable_clks(x) __lpuart_enable_clks(x, true)
406 #define lpuart_disable_clks(x) __lpuart_enable_clks(x, false)
408 static void lpuart_stop_tx(struct uart_port *port)
412 temp = readb(port->membase + UARTCR2);
413 temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
414 writeb(temp, port->membase + UARTCR2);
417 static void lpuart32_stop_tx(struct uart_port *port)
421 temp = lpuart32_read(port, UARTCTRL);
422 temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
423 lpuart32_write(port, temp, UARTCTRL);
426 static void lpuart_stop_rx(struct uart_port *port)
430 temp = readb(port->membase + UARTCR2);
431 writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
434 static void lpuart32_stop_rx(struct uart_port *port)
438 temp = lpuart32_read(port, UARTCTRL);
439 lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
442 static void lpuart_dma_tx(struct lpuart_port *sport)
444 struct circ_buf *xmit = &sport->port.state->xmit;
445 struct scatterlist *sgl = sport->tx_sgl;
446 struct device *dev = sport->port.dev;
447 struct dma_chan *chan = sport->dma_tx_chan;
450 if (sport->dma_tx_in_progress)
453 sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
455 if (xmit->tail < xmit->head || xmit->head == 0) {
456 sport->dma_tx_nents = 1;
457 sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
459 sport->dma_tx_nents = 2;
460 sg_init_table(sgl, 2);
461 sg_set_buf(sgl, xmit->buf + xmit->tail,
462 UART_XMIT_SIZE - xmit->tail);
463 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
466 ret = dma_map_sg(chan->device->dev, sgl, sport->dma_tx_nents,
469 dev_err(dev, "DMA mapping error for TX.\n");
473 sport->dma_tx_desc = dmaengine_prep_slave_sg(chan, sgl,
476 if (!sport->dma_tx_desc) {
477 dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
479 dev_err(dev, "Cannot prepare TX slave DMA!\n");
483 sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
484 sport->dma_tx_desc->callback_param = sport;
485 sport->dma_tx_in_progress = true;
486 sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
487 dma_async_issue_pending(chan);
490 static bool lpuart_stopped_or_empty(struct uart_port *port)
492 return uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port);
495 static void lpuart_dma_tx_complete(void *arg)
497 struct lpuart_port *sport = arg;
498 struct scatterlist *sgl = &sport->tx_sgl[0];
499 struct circ_buf *xmit = &sport->port.state->xmit;
500 struct dma_chan *chan = sport->dma_tx_chan;
503 spin_lock_irqsave(&sport->port.lock, flags);
504 if (!sport->dma_tx_in_progress) {
505 spin_unlock_irqrestore(&sport->port.lock, flags);
509 dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
512 xmit->tail = (xmit->tail + sport->dma_tx_bytes) & (UART_XMIT_SIZE - 1);
514 sport->port.icount.tx += sport->dma_tx_bytes;
515 sport->dma_tx_in_progress = false;
516 spin_unlock_irqrestore(&sport->port.lock, flags);
518 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
519 uart_write_wakeup(&sport->port);
521 if (waitqueue_active(&sport->dma_wait)) {
522 wake_up(&sport->dma_wait);
526 spin_lock_irqsave(&sport->port.lock, flags);
528 if (!lpuart_stopped_or_empty(&sport->port))
529 lpuart_dma_tx(sport);
531 spin_unlock_irqrestore(&sport->port.lock, flags);
534 static dma_addr_t lpuart_dma_datareg_addr(struct lpuart_port *sport)
536 switch (sport->port.iotype) {
538 return sport->port.mapbase + UARTDATA;
540 return sport->port.mapbase + UARTDATA + sizeof(u32) - 1;
542 return sport->port.mapbase + UARTDR;
545 static int lpuart_dma_tx_request(struct uart_port *port)
547 struct lpuart_port *sport = container_of(port,
548 struct lpuart_port, port);
549 struct dma_slave_config dma_tx_sconfig = {};
552 dma_tx_sconfig.dst_addr = lpuart_dma_datareg_addr(sport);
553 dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
554 dma_tx_sconfig.dst_maxburst = 1;
555 dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
556 ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
559 dev_err(sport->port.dev,
560 "DMA slave config failed, err = %d\n", ret);
567 static bool lpuart_is_32(struct lpuart_port *sport)
569 return sport->port.iotype == UPIO_MEM32 ||
570 sport->port.iotype == UPIO_MEM32BE;
573 static void lpuart_flush_buffer(struct uart_port *port)
575 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
576 struct dma_chan *chan = sport->dma_tx_chan;
579 if (sport->lpuart_dma_tx_use) {
580 if (sport->dma_tx_in_progress) {
581 dma_unmap_sg(chan->device->dev, &sport->tx_sgl[0],
582 sport->dma_tx_nents, DMA_TO_DEVICE);
583 sport->dma_tx_in_progress = false;
585 dmaengine_terminate_all(chan);
588 if (lpuart_is_32(sport)) {
589 val = lpuart32_read(&sport->port, UARTFIFO);
590 val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
591 lpuart32_write(&sport->port, val, UARTFIFO);
593 val = readb(sport->port.membase + UARTCFIFO);
594 val |= UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH;
595 writeb(val, sport->port.membase + UARTCFIFO);
599 static void lpuart_wait_bit_set(struct uart_port *port, unsigned int offset,
602 while (!(readb(port->membase + offset) & bit))
606 static void lpuart32_wait_bit_set(struct uart_port *port, unsigned int offset,
609 while (!(lpuart32_read(port, offset) & bit))
613 #if defined(CONFIG_CONSOLE_POLL)
615 static int lpuart_poll_init(struct uart_port *port)
617 struct lpuart_port *sport = container_of(port,
618 struct lpuart_port, port);
622 sport->port.fifosize = 0;
624 spin_lock_irqsave(&sport->port.lock, flags);
625 /* Disable Rx & Tx */
626 writeb(0, sport->port.membase + UARTCR2);
628 temp = readb(sport->port.membase + UARTPFIFO);
629 /* Enable Rx and Tx FIFO */
630 writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
631 sport->port.membase + UARTPFIFO);
633 /* flush Tx and Rx FIFO */
634 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
635 sport->port.membase + UARTCFIFO);
637 /* explicitly clear RDRF */
638 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
639 readb(sport->port.membase + UARTDR);
640 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
643 writeb(0, sport->port.membase + UARTTWFIFO);
644 writeb(1, sport->port.membase + UARTRWFIFO);
646 /* Enable Rx and Tx */
647 writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
648 spin_unlock_irqrestore(&sport->port.lock, flags);
653 static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
656 lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
657 writeb(c, port->membase + UARTDR);
660 static int lpuart_poll_get_char(struct uart_port *port)
662 if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
665 return readb(port->membase + UARTDR);
668 static int lpuart32_poll_init(struct uart_port *port)
671 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
674 sport->port.fifosize = 0;
676 spin_lock_irqsave(&sport->port.lock, flags);
678 /* Disable Rx & Tx */
679 lpuart32_write(&sport->port, 0, UARTCTRL);
681 temp = lpuart32_read(&sport->port, UARTFIFO);
683 /* Enable Rx and Tx FIFO */
684 lpuart32_write(&sport->port, temp | UARTFIFO_RXFE | UARTFIFO_TXFE, UARTFIFO);
686 /* flush Tx and Rx FIFO */
687 lpuart32_write(&sport->port, UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH, UARTFIFO);
689 /* explicitly clear RDRF */
690 if (lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_RDRF) {
691 lpuart32_read(&sport->port, UARTDATA);
692 lpuart32_write(&sport->port, UARTFIFO_RXUF, UARTFIFO);
695 /* Enable Rx and Tx */
696 lpuart32_write(&sport->port, UARTCTRL_RE | UARTCTRL_TE, UARTCTRL);
697 spin_unlock_irqrestore(&sport->port.lock, flags);
702 static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
704 lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
705 lpuart32_write(port, c, UARTDATA);
708 static int lpuart32_poll_get_char(struct uart_port *port)
710 if (!(lpuart32_read(port, UARTWATER) >> UARTWATER_RXCNT_OFF))
713 return lpuart32_read(port, UARTDATA);
717 static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
719 struct circ_buf *xmit = &sport->port.state->xmit;
721 if (sport->port.x_char) {
722 writeb(sport->port.x_char, sport->port.membase + UARTDR);
723 sport->port.icount.tx++;
724 sport->port.x_char = 0;
728 if (lpuart_stopped_or_empty(&sport->port)) {
729 lpuart_stop_tx(&sport->port);
733 while (!uart_circ_empty(xmit) &&
734 (readb(sport->port.membase + UARTTCFIFO) < sport->txfifo_size)) {
735 writeb(xmit->buf[xmit->tail], sport->port.membase + UARTDR);
736 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
737 sport->port.icount.tx++;
740 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
741 uart_write_wakeup(&sport->port);
743 if (uart_circ_empty(xmit))
744 lpuart_stop_tx(&sport->port);
747 static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
749 struct circ_buf *xmit = &sport->port.state->xmit;
752 if (sport->port.x_char) {
753 lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
754 sport->port.icount.tx++;
755 sport->port.x_char = 0;
759 if (lpuart_stopped_or_empty(&sport->port)) {
760 lpuart32_stop_tx(&sport->port);
764 txcnt = lpuart32_read(&sport->port, UARTWATER);
765 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
766 txcnt &= UARTWATER_COUNT_MASK;
767 while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
768 lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
769 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
770 sport->port.icount.tx++;
771 txcnt = lpuart32_read(&sport->port, UARTWATER);
772 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
773 txcnt &= UARTWATER_COUNT_MASK;
776 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
777 uart_write_wakeup(&sport->port);
779 if (uart_circ_empty(xmit))
780 lpuart32_stop_tx(&sport->port);
783 static void lpuart_start_tx(struct uart_port *port)
785 struct lpuart_port *sport = container_of(port,
786 struct lpuart_port, port);
789 temp = readb(port->membase + UARTCR2);
790 writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
792 if (sport->lpuart_dma_tx_use) {
793 if (!lpuart_stopped_or_empty(port))
794 lpuart_dma_tx(sport);
796 if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
797 lpuart_transmit_buffer(sport);
801 static void lpuart32_start_tx(struct uart_port *port)
803 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
806 if (sport->lpuart_dma_tx_use) {
807 if (!lpuart_stopped_or_empty(port))
808 lpuart_dma_tx(sport);
810 temp = lpuart32_read(port, UARTCTRL);
811 lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
813 if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
814 lpuart32_transmit_buffer(sport);
818 /* return TIOCSER_TEMT when transmitter is not busy */
819 static unsigned int lpuart_tx_empty(struct uart_port *port)
821 struct lpuart_port *sport = container_of(port,
822 struct lpuart_port, port);
823 unsigned char sr1 = readb(port->membase + UARTSR1);
824 unsigned char sfifo = readb(port->membase + UARTSFIFO);
826 if (sport->dma_tx_in_progress)
829 if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
835 static unsigned int lpuart32_tx_empty(struct uart_port *port)
837 struct lpuart_port *sport = container_of(port,
838 struct lpuart_port, port);
839 unsigned long stat = lpuart32_read(port, UARTSTAT);
840 unsigned long sfifo = lpuart32_read(port, UARTFIFO);
842 if (sport->dma_tx_in_progress)
845 if (stat & UARTSTAT_TC && sfifo & UARTFIFO_TXEMPT)
851 static void lpuart_txint(struct lpuart_port *sport)
853 spin_lock(&sport->port.lock);
854 lpuart_transmit_buffer(sport);
855 spin_unlock(&sport->port.lock);
858 static void lpuart_rxint(struct lpuart_port *sport)
860 unsigned int flg, ignored = 0, overrun = 0;
861 struct tty_port *port = &sport->port.state->port;
862 unsigned char rx, sr;
864 spin_lock(&sport->port.lock);
866 while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
868 sport->port.icount.rx++;
870 * to clear the FE, OR, NF, FE, PE flags,
871 * read SR1 then read DR
873 sr = readb(sport->port.membase + UARTSR1);
874 rx = readb(sport->port.membase + UARTDR);
876 if (uart_prepare_sysrq_char(&sport->port, rx))
879 if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
881 sport->port.icount.parity++;
882 else if (sr & UARTSR1_FE)
883 sport->port.icount.frame++;
888 if (sr & sport->port.ignore_status_mask) {
894 sr &= sport->port.read_status_mask;
898 else if (sr & UARTSR1_FE)
904 sport->port.sysrq = 0;
907 if (tty_insert_flip_char(port, rx, flg) == 0)
908 sport->port.icount.buf_overrun++;
913 sport->port.icount.overrun += overrun;
916 * Overruns cause FIFO pointers to become missaligned.
917 * Flushing the receive FIFO reinitializes the pointers.
919 writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
920 writeb(UARTSFIFO_RXOF, sport->port.membase + UARTSFIFO);
923 uart_unlock_and_check_sysrq(&sport->port);
925 tty_flip_buffer_push(port);
928 static void lpuart32_txint(struct lpuart_port *sport)
930 spin_lock(&sport->port.lock);
931 lpuart32_transmit_buffer(sport);
932 spin_unlock(&sport->port.lock);
935 static void lpuart32_rxint(struct lpuart_port *sport)
937 unsigned int flg, ignored = 0;
938 struct tty_port *port = &sport->port.state->port;
939 unsigned long rx, sr;
942 spin_lock(&sport->port.lock);
944 while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
946 sport->port.icount.rx++;
948 * to clear the FE, OR, NF, FE, PE flags,
949 * read STAT then read DATA reg
951 sr = lpuart32_read(&sport->port, UARTSTAT);
952 rx = lpuart32_read(&sport->port, UARTDATA);
956 * The LPUART can't distinguish between a break and a framing error,
957 * thus we assume it is a break if the received data is zero.
959 is_break = (sr & UARTSTAT_FE) && !rx;
961 if (is_break && uart_handle_break(&sport->port))
964 if (uart_prepare_sysrq_char(&sport->port, rx))
967 if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
968 if (sr & UARTSTAT_PE) {
969 sport->port.icount.parity++;
970 } else if (sr & UARTSTAT_FE) {
972 sport->port.icount.brk++;
974 sport->port.icount.frame++;
977 if (sr & UARTSTAT_OR)
978 sport->port.icount.overrun++;
980 if (sr & sport->port.ignore_status_mask) {
986 sr &= sport->port.read_status_mask;
988 if (sr & UARTSTAT_PE) {
990 } else if (sr & UARTSTAT_FE) {
997 if (sr & UARTSTAT_OR)
1004 if (tty_insert_flip_char(port, rx, flg) == 0)
1005 sport->port.icount.buf_overrun++;
1009 uart_unlock_and_check_sysrq(&sport->port);
1011 tty_flip_buffer_push(port);
1014 static irqreturn_t lpuart_int(int irq, void *dev_id)
1016 struct lpuart_port *sport = dev_id;
1019 sts = readb(sport->port.membase + UARTSR1);
1021 /* SysRq, using dma, check for linebreak by framing err. */
1022 if (sts & UARTSR1_FE && sport->lpuart_dma_rx_use) {
1023 readb(sport->port.membase + UARTDR);
1024 uart_handle_break(&sport->port);
1025 /* linebreak produces some garbage, removing it */
1026 writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
1030 if (sts & UARTSR1_RDRF && !sport->lpuart_dma_rx_use)
1031 lpuart_rxint(sport);
1033 if (sts & UARTSR1_TDRE && !sport->lpuart_dma_tx_use)
1034 lpuart_txint(sport);
1039 static irqreturn_t lpuart32_int(int irq, void *dev_id)
1041 struct lpuart_port *sport = dev_id;
1042 unsigned long sts, rxcount;
1044 sts = lpuart32_read(&sport->port, UARTSTAT);
1045 rxcount = lpuart32_read(&sport->port, UARTWATER);
1046 rxcount = rxcount >> UARTWATER_RXCNT_OFF;
1048 if ((sts & UARTSTAT_RDRF || rxcount > 0) && !sport->lpuart_dma_rx_use)
1049 lpuart32_rxint(sport);
1051 if ((sts & UARTSTAT_TDRE) && !sport->lpuart_dma_tx_use)
1052 lpuart32_txint(sport);
1054 lpuart32_write(&sport->port, sts, UARTSTAT);
1059 static inline void lpuart_handle_sysrq_chars(struct uart_port *port,
1060 unsigned char *p, int count)
1063 if (*p && uart_handle_sysrq_char(port, *p))
1069 static void lpuart_handle_sysrq(struct lpuart_port *sport)
1071 struct circ_buf *ring = &sport->rx_ring;
1074 if (ring->head < ring->tail) {
1075 count = sport->rx_sgl.length - ring->tail;
1076 lpuart_handle_sysrq_chars(&sport->port,
1077 ring->buf + ring->tail, count);
1081 if (ring->head > ring->tail) {
1082 count = ring->head - ring->tail;
1083 lpuart_handle_sysrq_chars(&sport->port,
1084 ring->buf + ring->tail, count);
1085 ring->tail = ring->head;
1089 static int lpuart_tty_insert_flip_string(struct tty_port *port,
1090 unsigned char *chars, size_t size, bool is_cs7)
1095 for (i = 0; i < size; i++)
1097 return tty_insert_flip_string(port, chars, size);
1100 static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
1102 struct tty_port *port = &sport->port.state->port;
1103 struct dma_tx_state state;
1104 enum dma_status dmastat;
1105 struct dma_chan *chan = sport->dma_rx_chan;
1106 struct circ_buf *ring = &sport->rx_ring;
1107 unsigned long flags;
1110 if (lpuart_is_32(sport)) {
1111 unsigned long sr = lpuart32_read(&sport->port, UARTSTAT);
1113 if (sr & (UARTSTAT_PE | UARTSTAT_FE)) {
1114 /* Read DR to clear the error flags */
1115 lpuart32_read(&sport->port, UARTDATA);
1117 if (sr & UARTSTAT_PE)
1118 sport->port.icount.parity++;
1119 else if (sr & UARTSTAT_FE)
1120 sport->port.icount.frame++;
1123 unsigned char sr = readb(sport->port.membase + UARTSR1);
1125 if (sr & (UARTSR1_PE | UARTSR1_FE)) {
1128 /* Disable receiver during this operation... */
1129 cr2 = readb(sport->port.membase + UARTCR2);
1131 writeb(cr2, sport->port.membase + UARTCR2);
1133 /* Read DR to clear the error flags */
1134 readb(sport->port.membase + UARTDR);
1136 if (sr & UARTSR1_PE)
1137 sport->port.icount.parity++;
1138 else if (sr & UARTSR1_FE)
1139 sport->port.icount.frame++;
1141 * At this point parity/framing error is
1142 * cleared However, since the DMA already read
1143 * the data register and we had to read it
1144 * again after reading the status register to
1145 * properly clear the flags, the FIFO actually
1146 * underflowed... This requires a clearing of
1149 if (readb(sport->port.membase + UARTSFIFO) &
1151 writeb(UARTSFIFO_RXUF,
1152 sport->port.membase + UARTSFIFO);
1153 writeb(UARTCFIFO_RXFLUSH,
1154 sport->port.membase + UARTCFIFO);
1158 writeb(cr2, sport->port.membase + UARTCR2);
1162 async_tx_ack(sport->dma_rx_desc);
1164 spin_lock_irqsave(&sport->port.lock, flags);
1166 dmastat = dmaengine_tx_status(chan, sport->dma_rx_cookie, &state);
1167 if (dmastat == DMA_ERROR) {
1168 dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
1169 spin_unlock_irqrestore(&sport->port.lock, flags);
1173 /* CPU claims ownership of RX DMA buffer */
1174 dma_sync_sg_for_cpu(chan->device->dev, &sport->rx_sgl, 1,
1178 * ring->head points to the end of data already written by the DMA.
1179 * ring->tail points to the beginning of data to be read by the
1181 * The current transfer size should not be larger than the dma buffer
1184 ring->head = sport->rx_sgl.length - state.residue;
1185 BUG_ON(ring->head > sport->rx_sgl.length);
1188 * Silent handling of keys pressed in the sysrq timeframe
1190 if (sport->port.sysrq) {
1191 lpuart_handle_sysrq(sport);
1196 * At this point ring->head may point to the first byte right after the
1197 * last byte of the dma buffer:
1198 * 0 <= ring->head <= sport->rx_sgl.length
1200 * However ring->tail must always points inside the dma buffer:
1201 * 0 <= ring->tail <= sport->rx_sgl.length - 1
1203 * Since we use a ring buffer, we have to handle the case
1204 * where head is lower than tail. In such a case, we first read from
1205 * tail to the end of the buffer then reset tail.
1207 if (ring->head < ring->tail) {
1208 count = sport->rx_sgl.length - ring->tail;
1210 copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
1211 count, sport->is_cs7);
1212 if (copied != count)
1213 sport->port.icount.buf_overrun++;
1215 sport->port.icount.rx += copied;
1218 /* Finally we read data from tail to head */
1219 if (ring->tail < ring->head) {
1220 count = ring->head - ring->tail;
1221 copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
1222 count, sport->is_cs7);
1223 if (copied != count)
1224 sport->port.icount.buf_overrun++;
1225 /* Wrap ring->head if needed */
1226 if (ring->head >= sport->rx_sgl.length)
1228 ring->tail = ring->head;
1229 sport->port.icount.rx += copied;
1233 dma_sync_sg_for_device(chan->device->dev, &sport->rx_sgl, 1,
1236 spin_unlock_irqrestore(&sport->port.lock, flags);
1238 tty_flip_buffer_push(port);
1239 mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
1242 static void lpuart_dma_rx_complete(void *arg)
1244 struct lpuart_port *sport = arg;
1246 lpuart_copy_rx_to_tty(sport);
1249 static void lpuart_timer_func(struct timer_list *t)
1251 struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
1253 lpuart_copy_rx_to_tty(sport);
1256 static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
1258 struct dma_slave_config dma_rx_sconfig = {};
1259 struct circ_buf *ring = &sport->rx_ring;
1261 struct tty_port *port = &sport->port.state->port;
1262 struct tty_struct *tty = port->tty;
1263 struct ktermios *termios = &tty->termios;
1264 struct dma_chan *chan = sport->dma_rx_chan;
1265 unsigned int bits = tty_get_frame_size(termios->c_cflag);
1266 unsigned int baud = tty_get_baud_rate(tty);
1269 * Calculate length of one DMA buffer size to keep latency below
1270 * 10ms at any baud rate.
1272 sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits / 1000) * 2;
1273 sport->rx_dma_rng_buf_len = (1 << (fls(sport->rx_dma_rng_buf_len) - 1));
1274 if (sport->rx_dma_rng_buf_len < 16)
1275 sport->rx_dma_rng_buf_len = 16;
1277 ring->buf = kzalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
1281 sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1282 nent = dma_map_sg(chan->device->dev, &sport->rx_sgl, 1,
1286 dev_err(sport->port.dev, "DMA Rx mapping error\n");
1290 dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
1291 dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1292 dma_rx_sconfig.src_maxburst = 1;
1293 dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
1294 ret = dmaengine_slave_config(chan, &dma_rx_sconfig);
1297 dev_err(sport->port.dev,
1298 "DMA Rx slave config failed, err = %d\n", ret);
1302 sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
1303 sg_dma_address(&sport->rx_sgl),
1304 sport->rx_sgl.length,
1305 sport->rx_sgl.length / 2,
1307 DMA_PREP_INTERRUPT);
1308 if (!sport->dma_rx_desc) {
1309 dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
1313 sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
1314 sport->dma_rx_desc->callback_param = sport;
1315 sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
1316 dma_async_issue_pending(chan);
1318 if (lpuart_is_32(sport)) {
1319 unsigned long temp = lpuart32_read(&sport->port, UARTBAUD);
1321 lpuart32_write(&sport->port, temp | UARTBAUD_RDMAE, UARTBAUD);
1323 writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
1324 sport->port.membase + UARTCR5);
1330 static void lpuart_dma_rx_free(struct uart_port *port)
1332 struct lpuart_port *sport = container_of(port,
1333 struct lpuart_port, port);
1334 struct dma_chan *chan = sport->dma_rx_chan;
1336 dmaengine_terminate_all(chan);
1337 dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1338 kfree(sport->rx_ring.buf);
1339 sport->rx_ring.tail = 0;
1340 sport->rx_ring.head = 0;
1341 sport->dma_rx_desc = NULL;
1342 sport->dma_rx_cookie = -EINVAL;
1345 static int lpuart_config_rs485(struct uart_port *port, struct ktermios *termios,
1346 struct serial_rs485 *rs485)
1348 struct lpuart_port *sport = container_of(port,
1349 struct lpuart_port, port);
1351 u8 modem = readb(sport->port.membase + UARTMODEM) &
1352 ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
1353 writeb(modem, sport->port.membase + UARTMODEM);
1355 if (rs485->flags & SER_RS485_ENABLED) {
1356 /* Enable auto RS-485 RTS mode */
1357 modem |= UARTMODEM_TXRTSE;
1360 * The hardware defaults to RTS logic HIGH while transfer.
1361 * Switch polarity in case RTS shall be logic HIGH
1363 * Note: UART is assumed to be active high.
1365 if (rs485->flags & SER_RS485_RTS_ON_SEND)
1366 modem |= UARTMODEM_TXRTSPOL;
1367 else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1368 modem &= ~UARTMODEM_TXRTSPOL;
1371 writeb(modem, sport->port.membase + UARTMODEM);
1375 static int lpuart32_config_rs485(struct uart_port *port, struct ktermios *termios,
1376 struct serial_rs485 *rs485)
1378 struct lpuart_port *sport = container_of(port,
1379 struct lpuart_port, port);
1381 unsigned long modem = lpuart32_read(&sport->port, UARTMODIR)
1382 & ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
1383 lpuart32_write(&sport->port, modem, UARTMODIR);
1385 if (rs485->flags & SER_RS485_ENABLED) {
1386 /* Enable auto RS-485 RTS mode */
1387 modem |= UARTMODEM_TXRTSE;
1390 * The hardware defaults to RTS logic HIGH while transfer.
1391 * Switch polarity in case RTS shall be logic HIGH
1393 * Note: UART is assumed to be active high.
1395 if (rs485->flags & SER_RS485_RTS_ON_SEND)
1396 modem &= ~UARTMODEM_TXRTSPOL;
1397 else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1398 modem |= UARTMODEM_TXRTSPOL;
1401 lpuart32_write(&sport->port, modem, UARTMODIR);
1405 static unsigned int lpuart_get_mctrl(struct uart_port *port)
1407 unsigned int mctrl = 0;
1410 reg = readb(port->membase + UARTCR1);
1411 if (reg & UARTCR1_LOOPS)
1412 mctrl |= TIOCM_LOOP;
1417 static unsigned int lpuart32_get_mctrl(struct uart_port *port)
1419 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
1422 reg = lpuart32_read(port, UARTCTRL);
1423 if (reg & UARTCTRL_LOOPS)
1424 mctrl |= TIOCM_LOOP;
1429 static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1433 reg = readb(port->membase + UARTCR1);
1435 /* for internal loopback we need LOOPS=1 and RSRC=0 */
1436 reg &= ~(UARTCR1_LOOPS | UARTCR1_RSRC);
1437 if (mctrl & TIOCM_LOOP)
1438 reg |= UARTCR1_LOOPS;
1440 writeb(reg, port->membase + UARTCR1);
1443 static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
1447 reg = lpuart32_read(port, UARTCTRL);
1449 /* for internal loopback we need LOOPS=1 and RSRC=0 */
1450 reg &= ~(UARTCTRL_LOOPS | UARTCTRL_RSRC);
1451 if (mctrl & TIOCM_LOOP)
1452 reg |= UARTCTRL_LOOPS;
1454 lpuart32_write(port, reg, UARTCTRL);
1457 static void lpuart_break_ctl(struct uart_port *port, int break_state)
1461 temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
1463 if (break_state != 0)
1464 temp |= UARTCR2_SBK;
1466 writeb(temp, port->membase + UARTCR2);
1469 static void lpuart32_break_ctl(struct uart_port *port, int break_state)
1473 temp = lpuart32_read(port, UARTCTRL) & ~UARTCTRL_SBK;
1475 if (break_state != 0)
1476 temp |= UARTCTRL_SBK;
1478 lpuart32_write(port, temp, UARTCTRL);
1481 static void lpuart_setup_watermark(struct lpuart_port *sport)
1483 unsigned char val, cr2;
1484 unsigned char cr2_saved;
1486 cr2 = readb(sport->port.membase + UARTCR2);
1488 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
1489 UARTCR2_RIE | UARTCR2_RE);
1490 writeb(cr2, sport->port.membase + UARTCR2);
1492 val = readb(sport->port.membase + UARTPFIFO);
1493 writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
1494 sport->port.membase + UARTPFIFO);
1496 /* flush Tx and Rx FIFO */
1497 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
1498 sport->port.membase + UARTCFIFO);
1500 /* explicitly clear RDRF */
1501 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
1502 readb(sport->port.membase + UARTDR);
1503 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
1506 writeb(0, sport->port.membase + UARTTWFIFO);
1507 writeb(1, sport->port.membase + UARTRWFIFO);
1510 writeb(cr2_saved, sport->port.membase + UARTCR2);
1513 static void lpuart_setup_watermark_enable(struct lpuart_port *sport)
1517 lpuart_setup_watermark(sport);
1519 cr2 = readb(sport->port.membase + UARTCR2);
1520 cr2 |= UARTCR2_RIE | UARTCR2_RE | UARTCR2_TE;
1521 writeb(cr2, sport->port.membase + UARTCR2);
1524 static void lpuart32_setup_watermark(struct lpuart_port *sport)
1526 unsigned long val, ctrl;
1527 unsigned long ctrl_saved;
1529 ctrl = lpuart32_read(&sport->port, UARTCTRL);
1531 ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
1532 UARTCTRL_RIE | UARTCTRL_RE);
1533 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1535 /* enable FIFO mode */
1536 val = lpuart32_read(&sport->port, UARTFIFO);
1537 val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
1538 val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
1539 lpuart32_write(&sport->port, val, UARTFIFO);
1541 /* set the watermark */
1542 val = (0x1 << UARTWATER_RXWATER_OFF) | (0x0 << UARTWATER_TXWATER_OFF);
1543 lpuart32_write(&sport->port, val, UARTWATER);
1546 lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
1549 static void lpuart32_setup_watermark_enable(struct lpuart_port *sport)
1553 lpuart32_setup_watermark(sport);
1555 temp = lpuart32_read(&sport->port, UARTCTRL);
1556 temp |= UARTCTRL_RE | UARTCTRL_TE | UARTCTRL_ILIE;
1557 lpuart32_write(&sport->port, temp, UARTCTRL);
1560 static void rx_dma_timer_init(struct lpuart_port *sport)
1562 timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
1563 sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
1564 add_timer(&sport->lpuart_timer);
1567 static void lpuart_request_dma(struct lpuart_port *sport)
1569 sport->dma_tx_chan = dma_request_chan(sport->port.dev, "tx");
1570 if (IS_ERR(sport->dma_tx_chan)) {
1571 dev_dbg_once(sport->port.dev,
1572 "DMA tx channel request failed, operating without tx DMA (%ld)\n",
1573 PTR_ERR(sport->dma_tx_chan));
1574 sport->dma_tx_chan = NULL;
1577 sport->dma_rx_chan = dma_request_chan(sport->port.dev, "rx");
1578 if (IS_ERR(sport->dma_rx_chan)) {
1579 dev_dbg_once(sport->port.dev,
1580 "DMA rx channel request failed, operating without rx DMA (%ld)\n",
1581 PTR_ERR(sport->dma_rx_chan));
1582 sport->dma_rx_chan = NULL;
1586 static void lpuart_tx_dma_startup(struct lpuart_port *sport)
1591 if (uart_console(&sport->port))
1594 if (!sport->dma_tx_chan)
1597 ret = lpuart_dma_tx_request(&sport->port);
1601 init_waitqueue_head(&sport->dma_wait);
1602 sport->lpuart_dma_tx_use = true;
1603 if (lpuart_is_32(sport)) {
1604 uartbaud = lpuart32_read(&sport->port, UARTBAUD);
1605 lpuart32_write(&sport->port,
1606 uartbaud | UARTBAUD_TDMAE, UARTBAUD);
1608 writeb(readb(sport->port.membase + UARTCR5) |
1609 UARTCR5_TDMAS, sport->port.membase + UARTCR5);
1615 sport->lpuart_dma_tx_use = false;
1618 static void lpuart_rx_dma_startup(struct lpuart_port *sport)
1623 if (uart_console(&sport->port))
1626 if (!sport->dma_rx_chan)
1629 ret = lpuart_start_rx_dma(sport);
1633 /* set Rx DMA timeout */
1634 sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
1635 if (!sport->dma_rx_timeout)
1636 sport->dma_rx_timeout = 1;
1638 sport->lpuart_dma_rx_use = true;
1639 rx_dma_timer_init(sport);
1641 if (sport->port.has_sysrq && !lpuart_is_32(sport)) {
1642 cr3 = readb(sport->port.membase + UARTCR3);
1643 cr3 |= UARTCR3_FEIE;
1644 writeb(cr3, sport->port.membase + UARTCR3);
1650 sport->lpuart_dma_rx_use = false;
1653 static int lpuart_startup(struct uart_port *port)
1655 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1656 unsigned long flags;
1659 /* determine FIFO size and enable FIFO mode */
1660 temp = readb(sport->port.membase + UARTPFIFO);
1662 sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_TXSIZE_OFF) &
1663 UARTPFIFO_FIFOSIZE_MASK);
1664 sport->port.fifosize = sport->txfifo_size;
1666 sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
1667 UARTPFIFO_FIFOSIZE_MASK);
1669 lpuart_request_dma(sport);
1671 spin_lock_irqsave(&sport->port.lock, flags);
1673 lpuart_setup_watermark_enable(sport);
1675 lpuart_rx_dma_startup(sport);
1676 lpuart_tx_dma_startup(sport);
1678 spin_unlock_irqrestore(&sport->port.lock, flags);
1683 static void lpuart32_configure(struct lpuart_port *sport)
1687 if (sport->lpuart_dma_rx_use) {
1688 /* RXWATER must be 0 */
1689 temp = lpuart32_read(&sport->port, UARTWATER);
1690 temp &= ~(UARTWATER_WATER_MASK << UARTWATER_RXWATER_OFF);
1691 lpuart32_write(&sport->port, temp, UARTWATER);
1693 temp = lpuart32_read(&sport->port, UARTCTRL);
1694 if (!sport->lpuart_dma_rx_use)
1695 temp |= UARTCTRL_RIE;
1696 if (!sport->lpuart_dma_tx_use)
1697 temp |= UARTCTRL_TIE;
1698 lpuart32_write(&sport->port, temp, UARTCTRL);
1701 static int lpuart32_startup(struct uart_port *port)
1703 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1704 unsigned long flags;
1707 /* determine FIFO size */
1708 temp = lpuart32_read(&sport->port, UARTFIFO);
1710 sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_TXSIZE_OFF) &
1711 UARTFIFO_FIFOSIZE_MASK);
1712 sport->port.fifosize = sport->txfifo_size;
1714 sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_RXSIZE_OFF) &
1715 UARTFIFO_FIFOSIZE_MASK);
1718 * The LS1021A and LS1028A have a fixed FIFO depth of 16 words.
1719 * Although they support the RX/TXSIZE fields, their encoding is
1720 * different. Eg the reference manual states 0b101 is 16 words.
1722 if (is_layerscape_lpuart(sport)) {
1723 sport->rxfifo_size = 16;
1724 sport->txfifo_size = 16;
1725 sport->port.fifosize = sport->txfifo_size;
1728 lpuart_request_dma(sport);
1730 spin_lock_irqsave(&sport->port.lock, flags);
1732 lpuart32_setup_watermark_enable(sport);
1734 lpuart_rx_dma_startup(sport);
1735 lpuart_tx_dma_startup(sport);
1737 lpuart32_configure(sport);
1739 spin_unlock_irqrestore(&sport->port.lock, flags);
1743 static void lpuart_dma_shutdown(struct lpuart_port *sport)
1745 if (sport->lpuart_dma_rx_use) {
1746 del_timer_sync(&sport->lpuart_timer);
1747 lpuart_dma_rx_free(&sport->port);
1748 sport->lpuart_dma_rx_use = false;
1751 if (sport->lpuart_dma_tx_use) {
1752 if (wait_event_interruptible_timeout(sport->dma_wait,
1753 !sport->dma_tx_in_progress, msecs_to_jiffies(300)) <= 0) {
1754 sport->dma_tx_in_progress = false;
1755 dmaengine_terminate_all(sport->dma_tx_chan);
1757 sport->lpuart_dma_tx_use = false;
1760 if (sport->dma_tx_chan)
1761 dma_release_channel(sport->dma_tx_chan);
1762 if (sport->dma_rx_chan)
1763 dma_release_channel(sport->dma_rx_chan);
1766 static void lpuart_shutdown(struct uart_port *port)
1768 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1770 unsigned long flags;
1772 spin_lock_irqsave(&port->lock, flags);
1774 /* disable Rx/Tx and interrupts */
1775 temp = readb(port->membase + UARTCR2);
1776 temp &= ~(UARTCR2_TE | UARTCR2_RE |
1777 UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1778 writeb(temp, port->membase + UARTCR2);
1780 spin_unlock_irqrestore(&port->lock, flags);
1782 lpuart_dma_shutdown(sport);
1785 static void lpuart32_shutdown(struct uart_port *port)
1787 struct lpuart_port *sport =
1788 container_of(port, struct lpuart_port, port);
1790 unsigned long flags;
1792 spin_lock_irqsave(&port->lock, flags);
1794 /* disable Rx/Tx and interrupts */
1795 temp = lpuart32_read(port, UARTCTRL);
1796 temp &= ~(UARTCTRL_TE | UARTCTRL_RE |
1797 UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
1798 lpuart32_write(port, temp, UARTCTRL);
1800 spin_unlock_irqrestore(&port->lock, flags);
1802 lpuart_dma_shutdown(sport);
1806 lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
1807 const struct ktermios *old)
1809 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1810 unsigned long flags;
1811 unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
1813 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1814 unsigned int sbr, brfa;
1816 cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
1817 old_cr2 = readb(sport->port.membase + UARTCR2);
1818 cr3 = readb(sport->port.membase + UARTCR3);
1819 cr4 = readb(sport->port.membase + UARTCR4);
1820 bdh = readb(sport->port.membase + UARTBDH);
1821 modem = readb(sport->port.membase + UARTMODEM);
1823 * only support CS8 and CS7, and for CS7 must enable PE.
1830 while ((termios->c_cflag & CSIZE) != CS8 &&
1831 (termios->c_cflag & CSIZE) != CS7) {
1832 termios->c_cflag &= ~CSIZE;
1833 termios->c_cflag |= old_csize;
1837 if ((termios->c_cflag & CSIZE) == CS8 ||
1838 (termios->c_cflag & CSIZE) == CS7)
1839 cr1 = old_cr1 & ~UARTCR1_M;
1841 if (termios->c_cflag & CMSPAR) {
1842 if ((termios->c_cflag & CSIZE) != CS8) {
1843 termios->c_cflag &= ~CSIZE;
1844 termios->c_cflag |= CS8;
1850 * When auto RS-485 RTS mode is enabled,
1851 * hardware flow control need to be disabled.
1853 if (sport->port.rs485.flags & SER_RS485_ENABLED)
1854 termios->c_cflag &= ~CRTSCTS;
1856 if (termios->c_cflag & CRTSCTS)
1857 modem |= UARTMODEM_RXRTSE | UARTMODEM_TXCTSE;
1859 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1861 termios->c_cflag &= ~CSTOPB;
1863 /* parity must be enabled when CS7 to match 8-bits format */
1864 if ((termios->c_cflag & CSIZE) == CS7)
1865 termios->c_cflag |= PARENB;
1867 if (termios->c_cflag & PARENB) {
1868 if (termios->c_cflag & CMSPAR) {
1870 if (termios->c_cflag & PARODD)
1876 if ((termios->c_cflag & CSIZE) == CS8)
1878 if (termios->c_cflag & PARODD)
1887 /* ask the core to calculate the divisor */
1888 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1891 * Need to update the Ring buffer length according to the selected
1892 * baud rate and restart Rx DMA path.
1894 * Since timer function acqures sport->port.lock, need to stop before
1895 * acquring same lock because otherwise del_timer_sync() can deadlock.
1897 if (old && sport->lpuart_dma_rx_use) {
1898 del_timer_sync(&sport->lpuart_timer);
1899 lpuart_dma_rx_free(&sport->port);
1902 spin_lock_irqsave(&sport->port.lock, flags);
1904 sport->port.read_status_mask = 0;
1905 if (termios->c_iflag & INPCK)
1906 sport->port.read_status_mask |= UARTSR1_FE | UARTSR1_PE;
1907 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1908 sport->port.read_status_mask |= UARTSR1_FE;
1910 /* characters to ignore */
1911 sport->port.ignore_status_mask = 0;
1912 if (termios->c_iflag & IGNPAR)
1913 sport->port.ignore_status_mask |= UARTSR1_PE;
1914 if (termios->c_iflag & IGNBRK) {
1915 sport->port.ignore_status_mask |= UARTSR1_FE;
1917 * if we're ignoring parity and break indicators,
1918 * ignore overruns too (for real raw support).
1920 if (termios->c_iflag & IGNPAR)
1921 sport->port.ignore_status_mask |= UARTSR1_OR;
1924 /* update the per-port timeout */
1925 uart_update_timeout(port, termios->c_cflag, baud);
1927 /* wait transmit engin complete */
1928 lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
1930 /* disable transmit and receive */
1931 writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
1932 sport->port.membase + UARTCR2);
1934 sbr = sport->port.uartclk / (16 * baud);
1935 brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
1936 bdh &= ~UARTBDH_SBR_MASK;
1937 bdh |= (sbr >> 8) & 0x1F;
1938 cr4 &= ~UARTCR4_BRFA_MASK;
1939 brfa &= UARTCR4_BRFA_MASK;
1940 writeb(cr4 | brfa, sport->port.membase + UARTCR4);
1941 writeb(bdh, sport->port.membase + UARTBDH);
1942 writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
1943 writeb(cr3, sport->port.membase + UARTCR3);
1944 writeb(cr1, sport->port.membase + UARTCR1);
1945 writeb(modem, sport->port.membase + UARTMODEM);
1947 /* restore control register */
1948 writeb(old_cr2, sport->port.membase + UARTCR2);
1950 if (old && sport->lpuart_dma_rx_use) {
1951 if (!lpuart_start_rx_dma(sport))
1952 rx_dma_timer_init(sport);
1954 sport->lpuart_dma_rx_use = false;
1957 spin_unlock_irqrestore(&sport->port.lock, flags);
1960 static void __lpuart32_serial_setbrg(struct uart_port *port,
1961 unsigned int baudrate, bool use_rx_dma,
1964 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
1965 u32 clk = port->uartclk;
1968 * The idea is to use the best OSR (over-sampling rate) possible.
1969 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
1970 * Loop to find the best OSR value possible, one that generates minimum
1971 * baud_diff iterate through the rest of the supported values of OSR.
1973 * Calculation Formula:
1974 * Baud Rate = baud clock / ((OSR+1) × SBR)
1976 baud_diff = baudrate;
1980 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
1981 /* calculate the temporary sbr value */
1982 tmp_sbr = (clk / (baudrate * tmp_osr));
1987 * calculate the baud rate difference based on the temporary
1988 * osr and sbr values
1990 tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
1992 /* select best values between sbr and sbr+1 */
1993 tmp = clk / (tmp_osr * (tmp_sbr + 1));
1994 if (tmp_diff > (baudrate - tmp)) {
1995 tmp_diff = baudrate - tmp;
1999 if (tmp_sbr > UARTBAUD_SBR_MASK)
2002 if (tmp_diff <= baud_diff) {
2003 baud_diff = tmp_diff;
2012 /* handle buadrate outside acceptable rate */
2013 if (baud_diff > ((baudrate / 100) * 3))
2015 "unacceptable baud rate difference of more than 3%%\n");
2017 tmp = lpuart32_read(port, UARTBAUD);
2019 if ((osr > 3) && (osr < 8))
2020 tmp |= UARTBAUD_BOTHEDGE;
2022 tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
2023 tmp |= ((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT;
2025 tmp &= ~UARTBAUD_SBR_MASK;
2026 tmp |= sbr & UARTBAUD_SBR_MASK;
2029 tmp &= ~UARTBAUD_RDMAE;
2031 tmp &= ~UARTBAUD_TDMAE;
2033 lpuart32_write(port, tmp, UARTBAUD);
2036 static void lpuart32_serial_setbrg(struct lpuart_port *sport,
2037 unsigned int baudrate)
2039 __lpuart32_serial_setbrg(&sport->port, baudrate,
2040 sport->lpuart_dma_rx_use,
2041 sport->lpuart_dma_tx_use);
2046 lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
2047 const struct ktermios *old)
2049 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
2050 unsigned long flags;
2051 unsigned long ctrl, old_ctrl, bd, modem;
2053 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
2055 ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
2056 bd = lpuart32_read(&sport->port, UARTBAUD);
2057 modem = lpuart32_read(&sport->port, UARTMODIR);
2058 sport->is_cs7 = false;
2060 * only support CS8 and CS7, and for CS7 must enable PE.
2067 while ((termios->c_cflag & CSIZE) != CS8 &&
2068 (termios->c_cflag & CSIZE) != CS7) {
2069 termios->c_cflag &= ~CSIZE;
2070 termios->c_cflag |= old_csize;
2074 if ((termios->c_cflag & CSIZE) == CS8 ||
2075 (termios->c_cflag & CSIZE) == CS7)
2076 ctrl = old_ctrl & ~UARTCTRL_M;
2078 if (termios->c_cflag & CMSPAR) {
2079 if ((termios->c_cflag & CSIZE) != CS8) {
2080 termios->c_cflag &= ~CSIZE;
2081 termios->c_cflag |= CS8;
2087 * When auto RS-485 RTS mode is enabled,
2088 * hardware flow control need to be disabled.
2090 if (sport->port.rs485.flags & SER_RS485_ENABLED)
2091 termios->c_cflag &= ~CRTSCTS;
2093 if (termios->c_cflag & CRTSCTS)
2094 modem |= UARTMODIR_RXRTSE | UARTMODIR_TXCTSE;
2096 modem &= ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
2098 if (termios->c_cflag & CSTOPB)
2099 bd |= UARTBAUD_SBNS;
2101 bd &= ~UARTBAUD_SBNS;
2103 /* parity must be enabled when CS7 to match 8-bits format */
2104 if ((termios->c_cflag & CSIZE) == CS7)
2105 termios->c_cflag |= PARENB;
2107 if ((termios->c_cflag & PARENB)) {
2108 if (termios->c_cflag & CMSPAR) {
2109 ctrl &= ~UARTCTRL_PE;
2112 ctrl |= UARTCTRL_PE;
2113 if ((termios->c_cflag & CSIZE) == CS8)
2115 if (termios->c_cflag & PARODD)
2116 ctrl |= UARTCTRL_PT;
2118 ctrl &= ~UARTCTRL_PT;
2121 ctrl &= ~UARTCTRL_PE;
2124 /* ask the core to calculate the divisor */
2125 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
2128 * Need to update the Ring buffer length according to the selected
2129 * baud rate and restart Rx DMA path.
2131 * Since timer function acqures sport->port.lock, need to stop before
2132 * acquring same lock because otherwise del_timer_sync() can deadlock.
2134 if (old && sport->lpuart_dma_rx_use) {
2135 del_timer_sync(&sport->lpuart_timer);
2136 lpuart_dma_rx_free(&sport->port);
2139 spin_lock_irqsave(&sport->port.lock, flags);
2141 sport->port.read_status_mask = 0;
2142 if (termios->c_iflag & INPCK)
2143 sport->port.read_status_mask |= UARTSTAT_FE | UARTSTAT_PE;
2144 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2145 sport->port.read_status_mask |= UARTSTAT_FE;
2147 /* characters to ignore */
2148 sport->port.ignore_status_mask = 0;
2149 if (termios->c_iflag & IGNPAR)
2150 sport->port.ignore_status_mask |= UARTSTAT_PE;
2151 if (termios->c_iflag & IGNBRK) {
2152 sport->port.ignore_status_mask |= UARTSTAT_FE;
2154 * if we're ignoring parity and break indicators,
2155 * ignore overruns too (for real raw support).
2157 if (termios->c_iflag & IGNPAR)
2158 sport->port.ignore_status_mask |= UARTSTAT_OR;
2161 /* update the per-port timeout */
2162 uart_update_timeout(port, termios->c_cflag, baud);
2164 /* wait transmit engin complete */
2165 lpuart32_write(&sport->port, 0, UARTMODIR);
2166 lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2168 /* disable transmit and receive */
2169 lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
2172 lpuart32_write(&sport->port, bd, UARTBAUD);
2173 lpuart32_serial_setbrg(sport, baud);
2174 lpuart32_write(&sport->port, modem, UARTMODIR);
2175 lpuart32_write(&sport->port, ctrl, UARTCTRL);
2176 /* restore control register */
2178 if ((ctrl & (UARTCTRL_PE | UARTCTRL_M)) == UARTCTRL_PE)
2179 sport->is_cs7 = true;
2181 if (old && sport->lpuart_dma_rx_use) {
2182 if (!lpuart_start_rx_dma(sport))
2183 rx_dma_timer_init(sport);
2185 sport->lpuart_dma_rx_use = false;
2188 spin_unlock_irqrestore(&sport->port.lock, flags);
2191 static const char *lpuart_type(struct uart_port *port)
2193 return "FSL_LPUART";
2196 static void lpuart_release_port(struct uart_port *port)
2201 static int lpuart_request_port(struct uart_port *port)
2206 /* configure/autoconfigure the port */
2207 static void lpuart_config_port(struct uart_port *port, int flags)
2209 if (flags & UART_CONFIG_TYPE)
2210 port->type = PORT_LPUART;
2213 static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
2217 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
2219 if (port->irq != ser->irq)
2221 if (ser->io_type != UPIO_MEM)
2223 if (port->uartclk / 16 != ser->baud_base)
2225 if (port->iobase != ser->port)
2232 static const struct uart_ops lpuart_pops = {
2233 .tx_empty = lpuart_tx_empty,
2234 .set_mctrl = lpuart_set_mctrl,
2235 .get_mctrl = lpuart_get_mctrl,
2236 .stop_tx = lpuart_stop_tx,
2237 .start_tx = lpuart_start_tx,
2238 .stop_rx = lpuart_stop_rx,
2239 .break_ctl = lpuart_break_ctl,
2240 .startup = lpuart_startup,
2241 .shutdown = lpuart_shutdown,
2242 .set_termios = lpuart_set_termios,
2243 .type = lpuart_type,
2244 .request_port = lpuart_request_port,
2245 .release_port = lpuart_release_port,
2246 .config_port = lpuart_config_port,
2247 .verify_port = lpuart_verify_port,
2248 .flush_buffer = lpuart_flush_buffer,
2249 #if defined(CONFIG_CONSOLE_POLL)
2250 .poll_init = lpuart_poll_init,
2251 .poll_get_char = lpuart_poll_get_char,
2252 .poll_put_char = lpuart_poll_put_char,
2256 static const struct uart_ops lpuart32_pops = {
2257 .tx_empty = lpuart32_tx_empty,
2258 .set_mctrl = lpuart32_set_mctrl,
2259 .get_mctrl = lpuart32_get_mctrl,
2260 .stop_tx = lpuart32_stop_tx,
2261 .start_tx = lpuart32_start_tx,
2262 .stop_rx = lpuart32_stop_rx,
2263 .break_ctl = lpuart32_break_ctl,
2264 .startup = lpuart32_startup,
2265 .shutdown = lpuart32_shutdown,
2266 .set_termios = lpuart32_set_termios,
2267 .type = lpuart_type,
2268 .request_port = lpuart_request_port,
2269 .release_port = lpuart_release_port,
2270 .config_port = lpuart_config_port,
2271 .verify_port = lpuart_verify_port,
2272 .flush_buffer = lpuart_flush_buffer,
2273 #if defined(CONFIG_CONSOLE_POLL)
2274 .poll_init = lpuart32_poll_init,
2275 .poll_get_char = lpuart32_poll_get_char,
2276 .poll_put_char = lpuart32_poll_put_char,
2280 static struct lpuart_port *lpuart_ports[UART_NR];
2282 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
2283 static void lpuart_console_putchar(struct uart_port *port, unsigned char ch)
2285 lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
2286 writeb(ch, port->membase + UARTDR);
2289 static void lpuart32_console_putchar(struct uart_port *port, unsigned char ch)
2291 lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
2292 lpuart32_write(port, ch, UARTDATA);
2296 lpuart_console_write(struct console *co, const char *s, unsigned int count)
2298 struct lpuart_port *sport = lpuart_ports[co->index];
2299 unsigned char old_cr2, cr2;
2300 unsigned long flags;
2303 if (oops_in_progress)
2304 locked = spin_trylock_irqsave(&sport->port.lock, flags);
2306 spin_lock_irqsave(&sport->port.lock, flags);
2308 /* first save CR2 and then disable interrupts */
2309 cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
2310 cr2 |= UARTCR2_TE | UARTCR2_RE;
2311 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
2312 writeb(cr2, sport->port.membase + UARTCR2);
2314 uart_console_write(&sport->port, s, count, lpuart_console_putchar);
2316 /* wait for transmitter finish complete and restore CR2 */
2317 lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
2319 writeb(old_cr2, sport->port.membase + UARTCR2);
2322 spin_unlock_irqrestore(&sport->port.lock, flags);
2326 lpuart32_console_write(struct console *co, const char *s, unsigned int count)
2328 struct lpuart_port *sport = lpuart_ports[co->index];
2329 unsigned long old_cr, cr;
2330 unsigned long flags;
2333 if (oops_in_progress)
2334 locked = spin_trylock_irqsave(&sport->port.lock, flags);
2336 spin_lock_irqsave(&sport->port.lock, flags);
2338 /* first save CR2 and then disable interrupts */
2339 cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
2340 cr |= UARTCTRL_TE | UARTCTRL_RE;
2341 cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
2342 lpuart32_write(&sport->port, cr, UARTCTRL);
2344 uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
2346 /* wait for transmitter finish complete and restore CR2 */
2347 lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
2349 lpuart32_write(&sport->port, old_cr, UARTCTRL);
2352 spin_unlock_irqrestore(&sport->port.lock, flags);
2356 * if the port was already initialised (eg, by a boot loader),
2357 * try to determine the current setup.
2360 lpuart_console_get_options(struct lpuart_port *sport, int *baud,
2361 int *parity, int *bits)
2363 unsigned char cr, bdh, bdl, brfa;
2364 unsigned int sbr, uartclk, baud_raw;
2366 cr = readb(sport->port.membase + UARTCR2);
2367 cr &= UARTCR2_TE | UARTCR2_RE;
2371 /* ok, the port was enabled */
2373 cr = readb(sport->port.membase + UARTCR1);
2376 if (cr & UARTCR1_PE) {
2377 if (cr & UARTCR1_PT)
2388 bdh = readb(sport->port.membase + UARTBDH);
2389 bdh &= UARTBDH_SBR_MASK;
2390 bdl = readb(sport->port.membase + UARTBDL);
2394 brfa = readb(sport->port.membase + UARTCR4);
2395 brfa &= UARTCR4_BRFA_MASK;
2397 uartclk = lpuart_get_baud_clk_rate(sport);
2399 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2401 baud_raw = uartclk / (16 * (sbr + brfa / 32));
2403 if (*baud != baud_raw)
2404 dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2405 "from %d to %d\n", baud_raw, *baud);
2409 lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
2410 int *parity, int *bits)
2412 unsigned long cr, bd;
2413 unsigned int sbr, uartclk, baud_raw;
2415 cr = lpuart32_read(&sport->port, UARTCTRL);
2416 cr &= UARTCTRL_TE | UARTCTRL_RE;
2420 /* ok, the port was enabled */
2422 cr = lpuart32_read(&sport->port, UARTCTRL);
2425 if (cr & UARTCTRL_PE) {
2426 if (cr & UARTCTRL_PT)
2432 if (cr & UARTCTRL_M)
2437 bd = lpuart32_read(&sport->port, UARTBAUD);
2438 bd &= UARTBAUD_SBR_MASK;
2443 uartclk = lpuart_get_baud_clk_rate(sport);
2445 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2447 baud_raw = uartclk / (16 * sbr);
2449 if (*baud != baud_raw)
2450 dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2451 "from %d to %d\n", baud_raw, *baud);
2454 static int __init lpuart_console_setup(struct console *co, char *options)
2456 struct lpuart_port *sport;
2463 * check whether an invalid uart number has been specified, and
2464 * if so, search for the first available port that does have
2467 if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
2470 sport = lpuart_ports[co->index];
2475 uart_parse_options(options, &baud, &parity, &bits, &flow);
2477 if (lpuart_is_32(sport))
2478 lpuart32_console_get_options(sport, &baud, &parity, &bits);
2480 lpuart_console_get_options(sport, &baud, &parity, &bits);
2482 if (lpuart_is_32(sport))
2483 lpuart32_setup_watermark(sport);
2485 lpuart_setup_watermark(sport);
2487 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
2490 static struct uart_driver lpuart_reg;
2491 static struct console lpuart_console = {
2493 .write = lpuart_console_write,
2494 .device = uart_console_device,
2495 .setup = lpuart_console_setup,
2496 .flags = CON_PRINTBUFFER,
2498 .data = &lpuart_reg,
2501 static struct console lpuart32_console = {
2503 .write = lpuart32_console_write,
2504 .device = uart_console_device,
2505 .setup = lpuart_console_setup,
2506 .flags = CON_PRINTBUFFER,
2508 .data = &lpuart_reg,
2511 static void lpuart_early_write(struct console *con, const char *s, unsigned n)
2513 struct earlycon_device *dev = con->data;
2515 uart_console_write(&dev->port, s, n, lpuart_console_putchar);
2518 static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
2520 struct earlycon_device *dev = con->data;
2522 uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
2525 static int __init lpuart_early_console_setup(struct earlycon_device *device,
2528 if (!device->port.membase)
2531 device->con->write = lpuart_early_write;
2535 static int __init lpuart32_early_console_setup(struct earlycon_device *device,
2538 if (!device->port.membase)
2541 if (device->port.iotype != UPIO_MEM32)
2542 device->port.iotype = UPIO_MEM32BE;
2544 device->con->write = lpuart32_early_write;
2548 static int __init ls1028a_early_console_setup(struct earlycon_device *device,
2553 if (!device->port.membase)
2556 device->port.iotype = UPIO_MEM32;
2557 device->con->write = lpuart32_early_write;
2559 /* set the baudrate */
2560 if (device->port.uartclk && device->baud)
2561 __lpuart32_serial_setbrg(&device->port, device->baud,
2564 /* enable transmitter */
2565 cr = lpuart32_read(&device->port, UARTCTRL);
2567 lpuart32_write(&device->port, cr, UARTCTRL);
2572 static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
2575 if (!device->port.membase)
2578 device->port.iotype = UPIO_MEM32;
2579 device->port.membase += IMX_REG_OFF;
2580 device->con->write = lpuart32_early_write;
2584 OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
2585 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2586 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1028a-lpuart", ls1028a_early_console_setup);
2587 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2588 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8qxp-lpuart", lpuart32_imx_early_console_setup);
2589 OF_EARLYCON_DECLARE(lpuart32, "fsl,imxrt1050-lpuart", lpuart32_imx_early_console_setup);
2590 EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
2591 EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2593 #define LPUART_CONSOLE (&lpuart_console)
2594 #define LPUART32_CONSOLE (&lpuart32_console)
2596 #define LPUART_CONSOLE NULL
2597 #define LPUART32_CONSOLE NULL
2600 static struct uart_driver lpuart_reg = {
2601 .owner = THIS_MODULE,
2602 .driver_name = DRIVER_NAME,
2603 .dev_name = DEV_NAME,
2604 .nr = ARRAY_SIZE(lpuart_ports),
2605 .cons = LPUART_CONSOLE,
2608 static const struct serial_rs485 lpuart_rs485_supported = {
2609 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
2610 /* delay_rts_* and RX_DURING_TX are not supported */
2613 static int lpuart_global_reset(struct lpuart_port *sport)
2615 struct uart_port *port = &sport->port;
2616 void __iomem *global_addr;
2617 unsigned long ctrl, bd;
2618 unsigned int val = 0;
2621 ret = clk_prepare_enable(sport->ipg_clk);
2623 dev_err(sport->port.dev, "failed to enable uart ipg clk: %d\n", ret);
2627 if (is_imx7ulp_lpuart(sport) || is_imx8qxp_lpuart(sport)) {
2629 * If the transmitter is used by earlycon, wait for transmit engine to
2630 * complete and then reset.
2632 ctrl = lpuart32_read(port, UARTCTRL);
2633 if (ctrl & UARTCTRL_TE) {
2634 bd = lpuart32_read(&sport->port, UARTBAUD);
2635 if (read_poll_timeout(lpuart32_tx_empty, val, val, 1, 100000, false,
2637 dev_warn(sport->port.dev,
2638 "timeout waiting for transmit engine to complete\n");
2639 clk_disable_unprepare(sport->ipg_clk);
2644 global_addr = port->membase + UART_GLOBAL - IMX_REG_OFF;
2645 writel(UART_GLOBAL_RST, global_addr);
2646 usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
2647 writel(0, global_addr);
2648 usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
2650 /* Recover the transmitter for earlycon. */
2651 if (ctrl & UARTCTRL_TE) {
2652 lpuart32_write(port, bd, UARTBAUD);
2653 lpuart32_write(port, ctrl, UARTCTRL);
2657 clk_disable_unprepare(sport->ipg_clk);
2661 static int lpuart_probe(struct platform_device *pdev)
2663 const struct lpuart_soc_data *sdata = of_device_get_match_data(&pdev->dev);
2664 struct device_node *np = pdev->dev.of_node;
2665 struct lpuart_port *sport;
2666 struct resource *res;
2667 irq_handler_t handler;
2670 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2674 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2675 sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
2676 if (IS_ERR(sport->port.membase))
2677 return PTR_ERR(sport->port.membase);
2679 sport->port.membase += sdata->reg_off;
2680 sport->port.mapbase = res->start + sdata->reg_off;
2681 sport->port.dev = &pdev->dev;
2682 sport->port.type = PORT_LPUART;
2683 sport->devtype = sdata->devtype;
2684 ret = platform_get_irq(pdev, 0);
2687 sport->port.irq = ret;
2688 sport->port.iotype = sdata->iotype;
2689 if (lpuart_is_32(sport))
2690 sport->port.ops = &lpuart32_pops;
2692 sport->port.ops = &lpuart_pops;
2693 sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_FSL_LPUART_CONSOLE);
2694 sport->port.flags = UPF_BOOT_AUTOCONF;
2696 if (lpuart_is_32(sport))
2697 sport->port.rs485_config = lpuart32_config_rs485;
2699 sport->port.rs485_config = lpuart_config_rs485;
2700 sport->port.rs485_supported = lpuart_rs485_supported;
2702 sport->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
2703 if (IS_ERR(sport->ipg_clk)) {
2704 ret = PTR_ERR(sport->ipg_clk);
2705 dev_err(&pdev->dev, "failed to get uart ipg clk: %d\n", ret);
2709 sport->baud_clk = NULL;
2710 if (is_imx8qxp_lpuart(sport)) {
2711 sport->baud_clk = devm_clk_get(&pdev->dev, "baud");
2712 if (IS_ERR(sport->baud_clk)) {
2713 ret = PTR_ERR(sport->baud_clk);
2714 dev_err(&pdev->dev, "failed to get uart baud clk: %d\n", ret);
2719 ret = of_alias_get_id(np, "serial");
2721 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
2724 if (ret >= ARRAY_SIZE(lpuart_ports)) {
2725 dev_err(&pdev->dev, "serial%d out of range\n", ret);
2728 sport->port.line = ret;
2730 ret = lpuart_enable_clks(sport);
2733 sport->port.uartclk = lpuart_get_baud_clk_rate(sport);
2735 lpuart_ports[sport->port.line] = sport;
2737 platform_set_drvdata(pdev, &sport->port);
2739 if (lpuart_is_32(sport)) {
2740 lpuart_reg.cons = LPUART32_CONSOLE;
2741 handler = lpuart32_int;
2743 lpuart_reg.cons = LPUART_CONSOLE;
2744 handler = lpuart_int;
2747 ret = lpuart_global_reset(sport);
2751 ret = uart_get_rs485_mode(&sport->port);
2753 goto failed_get_rs485;
2755 ret = uart_add_one_port(&lpuart_reg, &sport->port);
2757 goto failed_attach_port;
2759 ret = devm_request_irq(&pdev->dev, sport->port.irq, handler, 0,
2760 DRIVER_NAME, sport);
2762 goto failed_irq_request;
2767 uart_remove_one_port(&lpuart_reg, &sport->port);
2771 lpuart_disable_clks(sport);
2775 static int lpuart_remove(struct platform_device *pdev)
2777 struct lpuart_port *sport = platform_get_drvdata(pdev);
2779 uart_remove_one_port(&lpuart_reg, &sport->port);
2781 lpuart_disable_clks(sport);
2783 if (sport->dma_tx_chan)
2784 dma_release_channel(sport->dma_tx_chan);
2786 if (sport->dma_rx_chan)
2787 dma_release_channel(sport->dma_rx_chan);
2792 static int __maybe_unused lpuart_suspend(struct device *dev)
2794 struct lpuart_port *sport = dev_get_drvdata(dev);
2798 if (lpuart_is_32(sport)) {
2799 /* disable Rx/Tx and interrupts */
2800 temp = lpuart32_read(&sport->port, UARTCTRL);
2801 temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
2802 lpuart32_write(&sport->port, temp, UARTCTRL);
2804 /* disable Rx/Tx and interrupts */
2805 temp = readb(sport->port.membase + UARTCR2);
2806 temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
2807 writeb(temp, sport->port.membase + UARTCR2);
2810 uart_suspend_port(&lpuart_reg, &sport->port);
2812 /* uart_suspend_port() might set wakeup flag */
2813 irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2815 if (sport->lpuart_dma_rx_use) {
2817 * EDMA driver during suspend will forcefully release any
2818 * non-idle DMA channels. If port wakeup is enabled or if port
2819 * is console port or 'no_console_suspend' is set the Rx DMA
2820 * cannot resume as expected, hence gracefully release the
2821 * Rx DMA path before suspend and start Rx DMA path on resume.
2824 del_timer_sync(&sport->lpuart_timer);
2825 lpuart_dma_rx_free(&sport->port);
2828 /* Disable Rx DMA to use UART port as wakeup source */
2829 if (lpuart_is_32(sport)) {
2830 temp = lpuart32_read(&sport->port, UARTBAUD);
2831 lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
2834 writeb(readb(sport->port.membase + UARTCR5) &
2835 ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
2839 if (sport->lpuart_dma_tx_use) {
2840 sport->dma_tx_in_progress = false;
2841 dmaengine_terminate_all(sport->dma_tx_chan);
2844 if (sport->port.suspended && !irq_wake)
2845 lpuart_disable_clks(sport);
2850 static int __maybe_unused lpuart_resume(struct device *dev)
2852 struct lpuart_port *sport = dev_get_drvdata(dev);
2853 bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2855 if (sport->port.suspended && !irq_wake)
2856 lpuart_enable_clks(sport);
2858 if (lpuart_is_32(sport))
2859 lpuart32_setup_watermark_enable(sport);
2861 lpuart_setup_watermark_enable(sport);
2863 if (sport->lpuart_dma_rx_use) {
2865 if (!lpuart_start_rx_dma(sport))
2866 rx_dma_timer_init(sport);
2868 sport->lpuart_dma_rx_use = false;
2872 lpuart_tx_dma_startup(sport);
2874 if (lpuart_is_32(sport))
2875 lpuart32_configure(sport);
2877 uart_resume_port(&lpuart_reg, &sport->port);
2882 static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
2884 static struct platform_driver lpuart_driver = {
2885 .probe = lpuart_probe,
2886 .remove = lpuart_remove,
2888 .name = "fsl-lpuart",
2889 .of_match_table = lpuart_dt_ids,
2890 .pm = &lpuart_pm_ops,
2894 static int __init lpuart_serial_init(void)
2896 int ret = uart_register_driver(&lpuart_reg);
2901 ret = platform_driver_register(&lpuart_driver);
2903 uart_unregister_driver(&lpuart_reg);
2908 static void __exit lpuart_serial_exit(void)
2910 platform_driver_unregister(&lpuart_driver);
2911 uart_unregister_driver(&lpuart_reg);
2914 module_init(lpuart_serial_init);
2915 module_exit(lpuart_serial_exit);
2917 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
2918 MODULE_LICENSE("GPL v2");