1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale lpuart serial port driver
5 * Copyright 2012-2014 Freescale Semiconductor, Inc.
8 #if defined(CONFIG_SERIAL_FSL_LPUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12 #include <linux/clk.h>
13 #include <linux/console.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmaengine.h>
16 #include <linux/dmapool.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/serial_core.h>
24 #include <linux/slab.h>
25 #include <linux/tty_flip.h>
27 /* All registers are 8-bit width */
37 #define UARTMODEM 0x0d
38 #define UARTPFIFO 0x10
39 #define UARTCFIFO 0x11
40 #define UARTSFIFO 0x12
41 #define UARTTWFIFO 0x13
42 #define UARTTCFIFO 0x14
43 #define UARTRWFIFO 0x15
45 #define UARTBDH_LBKDIE 0x80
46 #define UARTBDH_RXEDGIE 0x40
47 #define UARTBDH_SBR_MASK 0x1f
49 #define UARTCR1_LOOPS 0x80
50 #define UARTCR1_RSRC 0x20
51 #define UARTCR1_M 0x10
52 #define UARTCR1_WAKE 0x08
53 #define UARTCR1_ILT 0x04
54 #define UARTCR1_PE 0x02
55 #define UARTCR1_PT 0x01
57 #define UARTCR2_TIE 0x80
58 #define UARTCR2_TCIE 0x40
59 #define UARTCR2_RIE 0x20
60 #define UARTCR2_ILIE 0x10
61 #define UARTCR2_TE 0x08
62 #define UARTCR2_RE 0x04
63 #define UARTCR2_RWU 0x02
64 #define UARTCR2_SBK 0x01
66 #define UARTSR1_TDRE 0x80
67 #define UARTSR1_TC 0x40
68 #define UARTSR1_RDRF 0x20
69 #define UARTSR1_IDLE 0x10
70 #define UARTSR1_OR 0x08
71 #define UARTSR1_NF 0x04
72 #define UARTSR1_FE 0x02
73 #define UARTSR1_PE 0x01
75 #define UARTCR3_R8 0x80
76 #define UARTCR3_T8 0x40
77 #define UARTCR3_TXDIR 0x20
78 #define UARTCR3_TXINV 0x10
79 #define UARTCR3_ORIE 0x08
80 #define UARTCR3_NEIE 0x04
81 #define UARTCR3_FEIE 0x02
82 #define UARTCR3_PEIE 0x01
84 #define UARTCR4_MAEN1 0x80
85 #define UARTCR4_MAEN2 0x40
86 #define UARTCR4_M10 0x20
87 #define UARTCR4_BRFA_MASK 0x1f
88 #define UARTCR4_BRFA_OFF 0
90 #define UARTCR5_TDMAS 0x80
91 #define UARTCR5_RDMAS 0x20
93 #define UARTMODEM_RXRTSE 0x08
94 #define UARTMODEM_TXRTSPOL 0x04
95 #define UARTMODEM_TXRTSE 0x02
96 #define UARTMODEM_TXCTSE 0x01
98 #define UARTPFIFO_TXFE 0x80
99 #define UARTPFIFO_FIFOSIZE_MASK 0x7
100 #define UARTPFIFO_TXSIZE_OFF 4
101 #define UARTPFIFO_RXFE 0x08
102 #define UARTPFIFO_RXSIZE_OFF 0
104 #define UARTCFIFO_TXFLUSH 0x80
105 #define UARTCFIFO_RXFLUSH 0x40
106 #define UARTCFIFO_RXOFE 0x04
107 #define UARTCFIFO_TXOFE 0x02
108 #define UARTCFIFO_RXUFE 0x01
110 #define UARTSFIFO_TXEMPT 0x80
111 #define UARTSFIFO_RXEMPT 0x40
112 #define UARTSFIFO_RXOF 0x04
113 #define UARTSFIFO_TXOF 0x02
114 #define UARTSFIFO_RXUF 0x01
116 /* 32-bit register definition */
117 #define UARTBAUD 0x00
118 #define UARTSTAT 0x04
119 #define UARTCTRL 0x08
120 #define UARTDATA 0x0C
121 #define UARTMATCH 0x10
122 #define UARTMODIR 0x14
123 #define UARTFIFO 0x18
124 #define UARTWATER 0x1c
126 #define UARTBAUD_MAEN1 0x80000000
127 #define UARTBAUD_MAEN2 0x40000000
128 #define UARTBAUD_M10 0x20000000
129 #define UARTBAUD_TDMAE 0x00800000
130 #define UARTBAUD_RDMAE 0x00200000
131 #define UARTBAUD_MATCFG 0x00400000
132 #define UARTBAUD_BOTHEDGE 0x00020000
133 #define UARTBAUD_RESYNCDIS 0x00010000
134 #define UARTBAUD_LBKDIE 0x00008000
135 #define UARTBAUD_RXEDGIE 0x00004000
136 #define UARTBAUD_SBNS 0x00002000
137 #define UARTBAUD_SBR 0x00000000
138 #define UARTBAUD_SBR_MASK 0x1fff
139 #define UARTBAUD_OSR_MASK 0x1f
140 #define UARTBAUD_OSR_SHIFT 24
142 #define UARTSTAT_LBKDIF 0x80000000
143 #define UARTSTAT_RXEDGIF 0x40000000
144 #define UARTSTAT_MSBF 0x20000000
145 #define UARTSTAT_RXINV 0x10000000
146 #define UARTSTAT_RWUID 0x08000000
147 #define UARTSTAT_BRK13 0x04000000
148 #define UARTSTAT_LBKDE 0x02000000
149 #define UARTSTAT_RAF 0x01000000
150 #define UARTSTAT_TDRE 0x00800000
151 #define UARTSTAT_TC 0x00400000
152 #define UARTSTAT_RDRF 0x00200000
153 #define UARTSTAT_IDLE 0x00100000
154 #define UARTSTAT_OR 0x00080000
155 #define UARTSTAT_NF 0x00040000
156 #define UARTSTAT_FE 0x00020000
157 #define UARTSTAT_PE 0x00010000
158 #define UARTSTAT_MA1F 0x00008000
159 #define UARTSTAT_M21F 0x00004000
161 #define UARTCTRL_R8T9 0x80000000
162 #define UARTCTRL_R9T8 0x40000000
163 #define UARTCTRL_TXDIR 0x20000000
164 #define UARTCTRL_TXINV 0x10000000
165 #define UARTCTRL_ORIE 0x08000000
166 #define UARTCTRL_NEIE 0x04000000
167 #define UARTCTRL_FEIE 0x02000000
168 #define UARTCTRL_PEIE 0x01000000
169 #define UARTCTRL_TIE 0x00800000
170 #define UARTCTRL_TCIE 0x00400000
171 #define UARTCTRL_RIE 0x00200000
172 #define UARTCTRL_ILIE 0x00100000
173 #define UARTCTRL_TE 0x00080000
174 #define UARTCTRL_RE 0x00040000
175 #define UARTCTRL_RWU 0x00020000
176 #define UARTCTRL_SBK 0x00010000
177 #define UARTCTRL_MA1IE 0x00008000
178 #define UARTCTRL_MA2IE 0x00004000
179 #define UARTCTRL_IDLECFG 0x00000100
180 #define UARTCTRL_LOOPS 0x00000080
181 #define UARTCTRL_DOZEEN 0x00000040
182 #define UARTCTRL_RSRC 0x00000020
183 #define UARTCTRL_M 0x00000010
184 #define UARTCTRL_WAKE 0x00000008
185 #define UARTCTRL_ILT 0x00000004
186 #define UARTCTRL_PE 0x00000002
187 #define UARTCTRL_PT 0x00000001
189 #define UARTDATA_NOISY 0x00008000
190 #define UARTDATA_PARITYE 0x00004000
191 #define UARTDATA_FRETSC 0x00002000
192 #define UARTDATA_RXEMPT 0x00001000
193 #define UARTDATA_IDLINE 0x00000800
194 #define UARTDATA_MASK 0x3ff
196 #define UARTMODIR_IREN 0x00020000
197 #define UARTMODIR_TXCTSSRC 0x00000020
198 #define UARTMODIR_TXCTSC 0x00000010
199 #define UARTMODIR_RXRTSE 0x00000008
200 #define UARTMODIR_TXRTSPOL 0x00000004
201 #define UARTMODIR_TXRTSE 0x00000002
202 #define UARTMODIR_TXCTSE 0x00000001
204 #define UARTFIFO_TXEMPT 0x00800000
205 #define UARTFIFO_RXEMPT 0x00400000
206 #define UARTFIFO_TXOF 0x00020000
207 #define UARTFIFO_RXUF 0x00010000
208 #define UARTFIFO_TXFLUSH 0x00008000
209 #define UARTFIFO_RXFLUSH 0x00004000
210 #define UARTFIFO_TXOFE 0x00000200
211 #define UARTFIFO_RXUFE 0x00000100
212 #define UARTFIFO_TXFE 0x00000080
213 #define UARTFIFO_FIFOSIZE_MASK 0x7
214 #define UARTFIFO_TXSIZE_OFF 4
215 #define UARTFIFO_RXFE 0x00000008
216 #define UARTFIFO_RXSIZE_OFF 0
218 #define UARTWATER_COUNT_MASK 0xff
219 #define UARTWATER_TXCNT_OFF 8
220 #define UARTWATER_RXCNT_OFF 24
221 #define UARTWATER_WATER_MASK 0xff
222 #define UARTWATER_TXWATER_OFF 0
223 #define UARTWATER_RXWATER_OFF 16
225 /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
226 #define DMA_RX_TIMEOUT (10)
228 #define DRIVER_NAME "fsl-lpuart"
229 #define DEV_NAME "ttyLP"
232 /* IMX lpuart has four extra unused regs located at the beginning */
233 #define IMX_REG_OFF 0x10
235 static DEFINE_IDA(fsl_lpuart_ida);
238 struct uart_port port;
240 unsigned int txfifo_size;
241 unsigned int rxfifo_size;
243 bool lpuart_dma_tx_use;
244 bool lpuart_dma_rx_use;
245 struct dma_chan *dma_tx_chan;
246 struct dma_chan *dma_rx_chan;
247 struct dma_async_tx_descriptor *dma_tx_desc;
248 struct dma_async_tx_descriptor *dma_rx_desc;
249 dma_cookie_t dma_tx_cookie;
250 dma_cookie_t dma_rx_cookie;
251 unsigned int dma_tx_bytes;
252 unsigned int dma_rx_bytes;
253 bool dma_tx_in_progress;
254 unsigned int dma_rx_timeout;
255 struct timer_list lpuart_timer;
256 struct scatterlist rx_sgl, tx_sgl[2];
257 struct circ_buf rx_ring;
258 int rx_dma_rng_buf_len;
259 unsigned int dma_tx_nents;
260 wait_queue_head_t dma_wait;
263 struct lpuart_soc_data {
268 static const struct lpuart_soc_data vf_data = {
272 static const struct lpuart_soc_data ls_data = {
273 .iotype = UPIO_MEM32BE,
276 static struct lpuart_soc_data imx_data = {
277 .iotype = UPIO_MEM32,
278 .reg_off = IMX_REG_OFF,
281 static const struct of_device_id lpuart_dt_ids[] = {
282 { .compatible = "fsl,vf610-lpuart", .data = &vf_data, },
283 { .compatible = "fsl,ls1021a-lpuart", .data = &ls_data, },
284 { .compatible = "fsl,imx7ulp-lpuart", .data = &imx_data, },
287 MODULE_DEVICE_TABLE(of, lpuart_dt_ids);
289 /* Forward declare this for the dma callbacks*/
290 static void lpuart_dma_tx_complete(void *arg);
292 static inline u32 lpuart32_read(struct uart_port *port, u32 off)
294 switch (port->iotype) {
296 return readl(port->membase + off);
298 return ioread32be(port->membase + off);
304 static inline void lpuart32_write(struct uart_port *port, u32 val,
307 switch (port->iotype) {
309 writel(val, port->membase + off);
312 iowrite32be(val, port->membase + off);
317 static void lpuart_stop_tx(struct uart_port *port)
321 temp = readb(port->membase + UARTCR2);
322 temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
323 writeb(temp, port->membase + UARTCR2);
326 static void lpuart32_stop_tx(struct uart_port *port)
330 temp = lpuart32_read(port, UARTCTRL);
331 temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
332 lpuart32_write(port, temp, UARTCTRL);
335 static void lpuart_stop_rx(struct uart_port *port)
339 temp = readb(port->membase + UARTCR2);
340 writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
343 static void lpuart32_stop_rx(struct uart_port *port)
347 temp = lpuart32_read(port, UARTCTRL);
348 lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
351 static void lpuart_dma_tx(struct lpuart_port *sport)
353 struct circ_buf *xmit = &sport->port.state->xmit;
354 struct scatterlist *sgl = sport->tx_sgl;
355 struct device *dev = sport->port.dev;
358 if (sport->dma_tx_in_progress)
361 sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
363 if (xmit->tail < xmit->head || xmit->head == 0) {
364 sport->dma_tx_nents = 1;
365 sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
367 sport->dma_tx_nents = 2;
368 sg_init_table(sgl, 2);
369 sg_set_buf(sgl, xmit->buf + xmit->tail,
370 UART_XMIT_SIZE - xmit->tail);
371 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
374 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
376 dev_err(dev, "DMA mapping error for TX.\n");
380 sport->dma_tx_desc = dmaengine_prep_slave_sg(sport->dma_tx_chan, sgl,
382 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
383 if (!sport->dma_tx_desc) {
384 dma_unmap_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
385 dev_err(dev, "Cannot prepare TX slave DMA!\n");
389 sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
390 sport->dma_tx_desc->callback_param = sport;
391 sport->dma_tx_in_progress = true;
392 sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
393 dma_async_issue_pending(sport->dma_tx_chan);
396 static void lpuart_dma_tx_complete(void *arg)
398 struct lpuart_port *sport = arg;
399 struct scatterlist *sgl = &sport->tx_sgl[0];
400 struct circ_buf *xmit = &sport->port.state->xmit;
403 spin_lock_irqsave(&sport->port.lock, flags);
405 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
407 xmit->tail = (xmit->tail + sport->dma_tx_bytes) & (UART_XMIT_SIZE - 1);
409 sport->port.icount.tx += sport->dma_tx_bytes;
410 sport->dma_tx_in_progress = false;
411 spin_unlock_irqrestore(&sport->port.lock, flags);
413 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
414 uart_write_wakeup(&sport->port);
416 if (waitqueue_active(&sport->dma_wait)) {
417 wake_up(&sport->dma_wait);
421 spin_lock_irqsave(&sport->port.lock, flags);
423 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
424 lpuart_dma_tx(sport);
426 spin_unlock_irqrestore(&sport->port.lock, flags);
429 static int lpuart_dma_tx_request(struct uart_port *port)
431 struct lpuart_port *sport = container_of(port,
432 struct lpuart_port, port);
433 struct dma_slave_config dma_tx_sconfig = {};
436 dma_tx_sconfig.dst_addr = sport->port.mapbase + UARTDR;
437 dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
438 dma_tx_sconfig.dst_maxburst = 1;
439 dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
440 ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
443 dev_err(sport->port.dev,
444 "DMA slave config failed, err = %d\n", ret);
451 static void lpuart_flush_buffer(struct uart_port *port)
453 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
455 if (sport->lpuart_dma_tx_use) {
456 if (sport->dma_tx_in_progress) {
457 dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
458 sport->dma_tx_nents, DMA_TO_DEVICE);
459 sport->dma_tx_in_progress = false;
461 dmaengine_terminate_all(sport->dma_tx_chan);
465 #if defined(CONFIG_CONSOLE_POLL)
467 static int lpuart_poll_init(struct uart_port *port)
469 struct lpuart_port *sport = container_of(port,
470 struct lpuart_port, port);
474 sport->port.fifosize = 0;
476 spin_lock_irqsave(&sport->port.lock, flags);
477 /* Disable Rx & Tx */
478 writeb(0, sport->port.membase + UARTCR2);
480 temp = readb(sport->port.membase + UARTPFIFO);
481 /* Enable Rx and Tx FIFO */
482 writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
483 sport->port.membase + UARTPFIFO);
485 /* flush Tx and Rx FIFO */
486 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
487 sport->port.membase + UARTCFIFO);
489 /* explicitly clear RDRF */
490 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
491 readb(sport->port.membase + UARTDR);
492 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
495 writeb(0, sport->port.membase + UARTTWFIFO);
496 writeb(1, sport->port.membase + UARTRWFIFO);
498 /* Enable Rx and Tx */
499 writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
500 spin_unlock_irqrestore(&sport->port.lock, flags);
505 static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
508 while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
511 writeb(c, port->membase + UARTDR);
514 static int lpuart_poll_get_char(struct uart_port *port)
516 if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
519 return readb(port->membase + UARTDR);
522 static int lpuart32_poll_init(struct uart_port *port)
525 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
528 sport->port.fifosize = 0;
530 spin_lock_irqsave(&sport->port.lock, flags);
532 /* Disable Rx & Tx */
533 writel(0, sport->port.membase + UARTCTRL);
535 temp = readl(sport->port.membase + UARTFIFO);
537 /* Enable Rx and Tx FIFO */
538 writel(temp | UARTFIFO_RXFE | UARTFIFO_TXFE,
539 sport->port.membase + UARTFIFO);
541 /* flush Tx and Rx FIFO */
542 writel(UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH,
543 sport->port.membase + UARTFIFO);
545 /* explicitly clear RDRF */
546 if (readl(sport->port.membase + UARTSTAT) & UARTSTAT_RDRF) {
547 readl(sport->port.membase + UARTDATA);
548 writel(UARTFIFO_RXUF, sport->port.membase + UARTFIFO);
551 /* Enable Rx and Tx */
552 writel(UARTCTRL_RE | UARTCTRL_TE, sport->port.membase + UARTCTRL);
553 spin_unlock_irqrestore(&sport->port.lock, flags);
558 static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
560 while (!(readl(port->membase + UARTSTAT) & UARTSTAT_TDRE))
563 writel(c, port->membase + UARTDATA);
566 static int lpuart32_poll_get_char(struct uart_port *port)
568 if (!(readl(port->membase + UARTSTAT) & UARTSTAT_RDRF))
571 return readl(port->membase + UARTDATA);
575 static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
577 struct circ_buf *xmit = &sport->port.state->xmit;
579 while (!uart_circ_empty(xmit) &&
580 (readb(sport->port.membase + UARTTCFIFO) < sport->txfifo_size)) {
581 writeb(xmit->buf[xmit->tail], sport->port.membase + UARTDR);
582 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
583 sport->port.icount.tx++;
586 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
587 uart_write_wakeup(&sport->port);
589 if (uart_circ_empty(xmit))
590 lpuart_stop_tx(&sport->port);
593 static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
595 struct circ_buf *xmit = &sport->port.state->xmit;
598 txcnt = lpuart32_read(&sport->port, UARTWATER);
599 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
600 txcnt &= UARTWATER_COUNT_MASK;
601 while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
602 lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
603 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
604 sport->port.icount.tx++;
605 txcnt = lpuart32_read(&sport->port, UARTWATER);
606 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
607 txcnt &= UARTWATER_COUNT_MASK;
610 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
611 uart_write_wakeup(&sport->port);
613 if (uart_circ_empty(xmit))
614 lpuart32_stop_tx(&sport->port);
617 static void lpuart_start_tx(struct uart_port *port)
619 struct lpuart_port *sport = container_of(port,
620 struct lpuart_port, port);
621 struct circ_buf *xmit = &sport->port.state->xmit;
624 temp = readb(port->membase + UARTCR2);
625 writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
627 if (sport->lpuart_dma_tx_use) {
628 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port))
629 lpuart_dma_tx(sport);
631 if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
632 lpuart_transmit_buffer(sport);
636 static void lpuart32_start_tx(struct uart_port *port)
638 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
641 temp = lpuart32_read(port, UARTCTRL);
642 lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
644 if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
645 lpuart32_transmit_buffer(sport);
648 /* return TIOCSER_TEMT when transmitter is not busy */
649 static unsigned int lpuart_tx_empty(struct uart_port *port)
651 struct lpuart_port *sport = container_of(port,
652 struct lpuart_port, port);
653 unsigned char sr1 = readb(port->membase + UARTSR1);
654 unsigned char sfifo = readb(port->membase + UARTSFIFO);
656 if (sport->dma_tx_in_progress)
659 if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
665 static unsigned int lpuart32_tx_empty(struct uart_port *port)
667 return (lpuart32_read(port, UARTSTAT) & UARTSTAT_TC) ?
671 static bool lpuart_is_32(struct lpuart_port *sport)
673 return sport->port.iotype == UPIO_MEM32 ||
674 sport->port.iotype == UPIO_MEM32BE;
677 static irqreturn_t lpuart_txint(int irq, void *dev_id)
679 struct lpuart_port *sport = dev_id;
680 struct circ_buf *xmit = &sport->port.state->xmit;
683 spin_lock_irqsave(&sport->port.lock, flags);
684 if (sport->port.x_char) {
685 if (lpuart_is_32(sport))
686 lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
688 writeb(sport->port.x_char, sport->port.membase + UARTDR);
692 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
693 if (lpuart_is_32(sport))
694 lpuart32_stop_tx(&sport->port);
696 lpuart_stop_tx(&sport->port);
700 if (lpuart_is_32(sport))
701 lpuart32_transmit_buffer(sport);
703 lpuart_transmit_buffer(sport);
705 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
706 uart_write_wakeup(&sport->port);
709 spin_unlock_irqrestore(&sport->port.lock, flags);
713 static irqreturn_t lpuart_rxint(int irq, void *dev_id)
715 struct lpuart_port *sport = dev_id;
716 unsigned int flg, ignored = 0;
717 struct tty_port *port = &sport->port.state->port;
719 unsigned char rx, sr;
721 spin_lock_irqsave(&sport->port.lock, flags);
723 while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
725 sport->port.icount.rx++;
727 * to clear the FE, OR, NF, FE, PE flags,
728 * read SR1 then read DR
730 sr = readb(sport->port.membase + UARTSR1);
731 rx = readb(sport->port.membase + UARTDR);
733 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
736 if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
738 sport->port.icount.parity++;
739 else if (sr & UARTSR1_FE)
740 sport->port.icount.frame++;
743 sport->port.icount.overrun++;
745 if (sr & sport->port.ignore_status_mask) {
751 sr &= sport->port.read_status_mask;
755 else if (sr & UARTSR1_FE)
762 sport->port.sysrq = 0;
766 tty_insert_flip_char(port, rx, flg);
770 spin_unlock_irqrestore(&sport->port.lock, flags);
772 tty_flip_buffer_push(port);
776 static irqreturn_t lpuart32_rxint(int irq, void *dev_id)
778 struct lpuart_port *sport = dev_id;
779 unsigned int flg, ignored = 0;
780 struct tty_port *port = &sport->port.state->port;
782 unsigned long rx, sr;
784 spin_lock_irqsave(&sport->port.lock, flags);
786 while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
788 sport->port.icount.rx++;
790 * to clear the FE, OR, NF, FE, PE flags,
791 * read STAT then read DATA reg
793 sr = lpuart32_read(&sport->port, UARTSTAT);
794 rx = lpuart32_read(&sport->port, UARTDATA);
797 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
800 if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
801 if (sr & UARTSTAT_PE)
802 sport->port.icount.parity++;
803 else if (sr & UARTSTAT_FE)
804 sport->port.icount.frame++;
806 if (sr & UARTSTAT_OR)
807 sport->port.icount.overrun++;
809 if (sr & sport->port.ignore_status_mask) {
815 sr &= sport->port.read_status_mask;
817 if (sr & UARTSTAT_PE)
819 else if (sr & UARTSTAT_FE)
822 if (sr & UARTSTAT_OR)
826 sport->port.sysrq = 0;
830 tty_insert_flip_char(port, rx, flg);
834 spin_unlock_irqrestore(&sport->port.lock, flags);
836 tty_flip_buffer_push(port);
840 static irqreturn_t lpuart_int(int irq, void *dev_id)
842 struct lpuart_port *sport = dev_id;
845 sts = readb(sport->port.membase + UARTSR1);
847 if (sts & UARTSR1_RDRF)
848 lpuart_rxint(irq, dev_id);
850 if (sts & UARTSR1_TDRE)
851 lpuart_txint(irq, dev_id);
856 static irqreturn_t lpuart32_int(int irq, void *dev_id)
858 struct lpuart_port *sport = dev_id;
859 unsigned long sts, rxcount;
861 sts = lpuart32_read(&sport->port, UARTSTAT);
862 rxcount = lpuart32_read(&sport->port, UARTWATER);
863 rxcount = rxcount >> UARTWATER_RXCNT_OFF;
865 if (sts & UARTSTAT_RDRF || rxcount > 0)
866 lpuart32_rxint(irq, dev_id);
868 if ((sts & UARTSTAT_TDRE) &&
869 !(lpuart32_read(&sport->port, UARTBAUD) & UARTBAUD_TDMAE))
870 lpuart_txint(irq, dev_id);
872 lpuart32_write(&sport->port, sts, UARTSTAT);
876 static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
878 struct tty_port *port = &sport->port.state->port;
879 struct dma_tx_state state;
880 enum dma_status dmastat;
881 struct circ_buf *ring = &sport->rx_ring;
886 sr = readb(sport->port.membase + UARTSR1);
888 if (sr & (UARTSR1_PE | UARTSR1_FE)) {
889 /* Read DR to clear the error flags */
890 readb(sport->port.membase + UARTDR);
893 sport->port.icount.parity++;
894 else if (sr & UARTSR1_FE)
895 sport->port.icount.frame++;
898 async_tx_ack(sport->dma_rx_desc);
900 spin_lock_irqsave(&sport->port.lock, flags);
902 dmastat = dmaengine_tx_status(sport->dma_rx_chan,
903 sport->dma_rx_cookie,
906 if (dmastat == DMA_ERROR) {
907 dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
908 spin_unlock_irqrestore(&sport->port.lock, flags);
912 /* CPU claims ownership of RX DMA buffer */
913 dma_sync_sg_for_cpu(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
916 * ring->head points to the end of data already written by the DMA.
917 * ring->tail points to the beginning of data to be read by the
919 * The current transfer size should not be larger than the dma buffer
922 ring->head = sport->rx_sgl.length - state.residue;
923 BUG_ON(ring->head > sport->rx_sgl.length);
925 * At this point ring->head may point to the first byte right after the
926 * last byte of the dma buffer:
927 * 0 <= ring->head <= sport->rx_sgl.length
929 * However ring->tail must always points inside the dma buffer:
930 * 0 <= ring->tail <= sport->rx_sgl.length - 1
932 * Since we use a ring buffer, we have to handle the case
933 * where head is lower than tail. In such a case, we first read from
934 * tail to the end of the buffer then reset tail.
936 if (ring->head < ring->tail) {
937 count = sport->rx_sgl.length - ring->tail;
939 tty_insert_flip_string(port, ring->buf + ring->tail, count);
941 sport->port.icount.rx += count;
944 /* Finally we read data from tail to head */
945 if (ring->tail < ring->head) {
946 count = ring->head - ring->tail;
947 tty_insert_flip_string(port, ring->buf + ring->tail, count);
948 /* Wrap ring->head if needed */
949 if (ring->head >= sport->rx_sgl.length)
951 ring->tail = ring->head;
952 sport->port.icount.rx += count;
955 dma_sync_sg_for_device(sport->port.dev, &sport->rx_sgl, 1,
958 spin_unlock_irqrestore(&sport->port.lock, flags);
960 tty_flip_buffer_push(port);
961 mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
964 static void lpuart_dma_rx_complete(void *arg)
966 struct lpuart_port *sport = arg;
968 lpuart_copy_rx_to_tty(sport);
971 static void lpuart_timer_func(struct timer_list *t)
973 struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
975 lpuart_copy_rx_to_tty(sport);
978 static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
980 struct dma_slave_config dma_rx_sconfig = {};
981 struct circ_buf *ring = &sport->rx_ring;
984 struct tty_port *port = &sport->port.state->port;
985 struct tty_struct *tty = port->tty;
986 struct ktermios *termios = &tty->termios;
988 baud = tty_get_baud_rate(tty);
990 bits = (termios->c_cflag & CSIZE) == CS7 ? 9 : 10;
991 if (termios->c_cflag & PARENB)
995 * Calculate length of one DMA buffer size to keep latency below
996 * 10ms at any baud rate.
998 sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits / 1000) * 2;
999 sport->rx_dma_rng_buf_len = (1 << (fls(sport->rx_dma_rng_buf_len) - 1));
1000 if (sport->rx_dma_rng_buf_len < 16)
1001 sport->rx_dma_rng_buf_len = 16;
1003 ring->buf = kmalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
1005 dev_err(sport->port.dev, "Ring buf alloc failed\n");
1009 sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1010 sg_set_buf(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1011 nent = dma_map_sg(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1014 dev_err(sport->port.dev, "DMA Rx mapping error\n");
1018 dma_rx_sconfig.src_addr = sport->port.mapbase + UARTDR;
1019 dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1020 dma_rx_sconfig.src_maxburst = 1;
1021 dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
1022 ret = dmaengine_slave_config(sport->dma_rx_chan, &dma_rx_sconfig);
1025 dev_err(sport->port.dev,
1026 "DMA Rx slave config failed, err = %d\n", ret);
1030 sport->dma_rx_desc = dmaengine_prep_dma_cyclic(sport->dma_rx_chan,
1031 sg_dma_address(&sport->rx_sgl),
1032 sport->rx_sgl.length,
1033 sport->rx_sgl.length / 2,
1035 DMA_PREP_INTERRUPT);
1036 if (!sport->dma_rx_desc) {
1037 dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
1041 sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
1042 sport->dma_rx_desc->callback_param = sport;
1043 sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
1044 dma_async_issue_pending(sport->dma_rx_chan);
1046 writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
1047 sport->port.membase + UARTCR5);
1052 static void lpuart_dma_rx_free(struct uart_port *port)
1054 struct lpuart_port *sport = container_of(port,
1055 struct lpuart_port, port);
1057 if (sport->dma_rx_chan)
1058 dmaengine_terminate_all(sport->dma_rx_chan);
1060 dma_unmap_sg(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1061 kfree(sport->rx_ring.buf);
1062 sport->rx_ring.tail = 0;
1063 sport->rx_ring.head = 0;
1064 sport->dma_rx_desc = NULL;
1065 sport->dma_rx_cookie = -EINVAL;
1068 static int lpuart_config_rs485(struct uart_port *port,
1069 struct serial_rs485 *rs485)
1071 struct lpuart_port *sport = container_of(port,
1072 struct lpuart_port, port);
1074 u8 modem = readb(sport->port.membase + UARTMODEM) &
1075 ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
1076 writeb(modem, sport->port.membase + UARTMODEM);
1078 /* clear unsupported configurations */
1079 rs485->delay_rts_before_send = 0;
1080 rs485->delay_rts_after_send = 0;
1081 rs485->flags &= ~SER_RS485_RX_DURING_TX;
1083 if (rs485->flags & SER_RS485_ENABLED) {
1084 /* Enable auto RS-485 RTS mode */
1085 modem |= UARTMODEM_TXRTSE;
1088 * RTS needs to be logic HIGH either during transer _or_ after
1089 * transfer, other variants are not supported by the hardware.
1092 if (!(rs485->flags & (SER_RS485_RTS_ON_SEND |
1093 SER_RS485_RTS_AFTER_SEND)))
1094 rs485->flags |= SER_RS485_RTS_ON_SEND;
1096 if (rs485->flags & SER_RS485_RTS_ON_SEND &&
1097 rs485->flags & SER_RS485_RTS_AFTER_SEND)
1098 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1101 * The hardware defaults to RTS logic HIGH while transfer.
1102 * Switch polarity in case RTS shall be logic HIGH
1104 * Note: UART is assumed to be active high.
1106 if (rs485->flags & SER_RS485_RTS_ON_SEND)
1107 modem &= ~UARTMODEM_TXRTSPOL;
1108 else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1109 modem |= UARTMODEM_TXRTSPOL;
1112 /* Store the new configuration */
1113 sport->port.rs485 = *rs485;
1115 writeb(modem, sport->port.membase + UARTMODEM);
1119 static unsigned int lpuart_get_mctrl(struct uart_port *port)
1121 unsigned int temp = 0;
1124 reg = readb(port->membase + UARTMODEM);
1125 if (reg & UARTMODEM_TXCTSE)
1128 if (reg & UARTMODEM_RXRTSE)
1134 static unsigned int lpuart32_get_mctrl(struct uart_port *port)
1136 unsigned int temp = 0;
1139 reg = lpuart32_read(port, UARTMODIR);
1140 if (reg & UARTMODIR_TXCTSE)
1143 if (reg & UARTMODIR_RXRTSE)
1149 static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1152 struct lpuart_port *sport = container_of(port,
1153 struct lpuart_port, port);
1155 /* Make sure RXRTSE bit is not set when RS485 is enabled */
1156 if (!(sport->port.rs485.flags & SER_RS485_ENABLED)) {
1157 temp = readb(sport->port.membase + UARTMODEM) &
1158 ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1160 if (mctrl & TIOCM_RTS)
1161 temp |= UARTMODEM_RXRTSE;
1163 if (mctrl & TIOCM_CTS)
1164 temp |= UARTMODEM_TXCTSE;
1166 writeb(temp, port->membase + UARTMODEM);
1170 static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
1174 temp = lpuart32_read(port, UARTMODIR) &
1175 ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
1177 if (mctrl & TIOCM_RTS)
1178 temp |= UARTMODIR_RXRTSE;
1180 if (mctrl & TIOCM_CTS)
1181 temp |= UARTMODIR_TXCTSE;
1183 lpuart32_write(port, temp, UARTMODIR);
1186 static void lpuart_break_ctl(struct uart_port *port, int break_state)
1190 temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
1192 if (break_state != 0)
1193 temp |= UARTCR2_SBK;
1195 writeb(temp, port->membase + UARTCR2);
1198 static void lpuart32_break_ctl(struct uart_port *port, int break_state)
1202 temp = lpuart32_read(port, UARTCTRL) & ~UARTCTRL_SBK;
1204 if (break_state != 0)
1205 temp |= UARTCTRL_SBK;
1207 lpuart32_write(port, temp, UARTCTRL);
1210 static void lpuart_setup_watermark(struct lpuart_port *sport)
1212 unsigned char val, cr2;
1213 unsigned char cr2_saved;
1215 cr2 = readb(sport->port.membase + UARTCR2);
1217 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
1218 UARTCR2_RIE | UARTCR2_RE);
1219 writeb(cr2, sport->port.membase + UARTCR2);
1221 val = readb(sport->port.membase + UARTPFIFO);
1222 writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
1223 sport->port.membase + UARTPFIFO);
1225 /* flush Tx and Rx FIFO */
1226 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
1227 sport->port.membase + UARTCFIFO);
1229 /* explicitly clear RDRF */
1230 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
1231 readb(sport->port.membase + UARTDR);
1232 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
1235 writeb(0, sport->port.membase + UARTTWFIFO);
1236 writeb(1, sport->port.membase + UARTRWFIFO);
1239 writeb(cr2_saved, sport->port.membase + UARTCR2);
1242 static void lpuart32_setup_watermark(struct lpuart_port *sport)
1244 unsigned long val, ctrl;
1245 unsigned long ctrl_saved;
1247 ctrl = lpuart32_read(&sport->port, UARTCTRL);
1249 ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
1250 UARTCTRL_RIE | UARTCTRL_RE);
1251 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1253 /* enable FIFO mode */
1254 val = lpuart32_read(&sport->port, UARTFIFO);
1255 val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
1256 val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
1257 lpuart32_write(&sport->port, val, UARTFIFO);
1259 /* set the watermark */
1260 val = (0x1 << UARTWATER_RXWATER_OFF) | (0x0 << UARTWATER_TXWATER_OFF);
1261 lpuart32_write(&sport->port, val, UARTWATER);
1264 lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
1267 static void rx_dma_timer_init(struct lpuart_port *sport)
1269 timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
1270 sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
1271 add_timer(&sport->lpuart_timer);
1274 static int lpuart_startup(struct uart_port *port)
1276 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1277 unsigned long flags;
1280 /* determine FIFO size and enable FIFO mode */
1281 temp = readb(sport->port.membase + UARTPFIFO);
1283 sport->txfifo_size = 0x1 << (((temp >> UARTPFIFO_TXSIZE_OFF) &
1284 UARTPFIFO_FIFOSIZE_MASK) + 1);
1286 sport->port.fifosize = sport->txfifo_size;
1288 sport->rxfifo_size = 0x1 << (((temp >> UARTPFIFO_RXSIZE_OFF) &
1289 UARTPFIFO_FIFOSIZE_MASK) + 1);
1291 spin_lock_irqsave(&sport->port.lock, flags);
1293 lpuart_setup_watermark(sport);
1295 temp = readb(sport->port.membase + UARTCR2);
1296 temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
1297 writeb(temp, sport->port.membase + UARTCR2);
1299 if (sport->dma_rx_chan && !lpuart_start_rx_dma(sport)) {
1300 /* set Rx DMA timeout */
1301 sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
1302 if (!sport->dma_rx_timeout)
1303 sport->dma_rx_timeout = 1;
1305 sport->lpuart_dma_rx_use = true;
1306 rx_dma_timer_init(sport);
1308 sport->lpuart_dma_rx_use = false;
1311 if (sport->dma_tx_chan && !lpuart_dma_tx_request(port)) {
1312 init_waitqueue_head(&sport->dma_wait);
1313 sport->lpuart_dma_tx_use = true;
1314 temp = readb(port->membase + UARTCR5);
1315 writeb(temp | UARTCR5_TDMAS, port->membase + UARTCR5);
1317 sport->lpuart_dma_tx_use = false;
1320 spin_unlock_irqrestore(&sport->port.lock, flags);
1325 static int lpuart32_startup(struct uart_port *port)
1327 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1328 unsigned long flags;
1331 /* determine FIFO size */
1332 temp = lpuart32_read(&sport->port, UARTFIFO);
1334 sport->txfifo_size = 0x1 << (((temp >> UARTFIFO_TXSIZE_OFF) &
1335 UARTFIFO_FIFOSIZE_MASK) - 1);
1337 sport->rxfifo_size = 0x1 << (((temp >> UARTFIFO_RXSIZE_OFF) &
1338 UARTFIFO_FIFOSIZE_MASK) - 1);
1340 spin_lock_irqsave(&sport->port.lock, flags);
1342 lpuart32_setup_watermark(sport);
1344 temp = lpuart32_read(&sport->port, UARTCTRL);
1345 temp |= (UARTCTRL_RIE | UARTCTRL_TIE | UARTCTRL_RE | UARTCTRL_TE);
1346 temp |= UARTCTRL_ILIE;
1347 lpuart32_write(&sport->port, temp, UARTCTRL);
1349 spin_unlock_irqrestore(&sport->port.lock, flags);
1353 static void lpuart_shutdown(struct uart_port *port)
1355 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1357 unsigned long flags;
1359 spin_lock_irqsave(&port->lock, flags);
1361 /* disable Rx/Tx and interrupts */
1362 temp = readb(port->membase + UARTCR2);
1363 temp &= ~(UARTCR2_TE | UARTCR2_RE |
1364 UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1365 writeb(temp, port->membase + UARTCR2);
1367 spin_unlock_irqrestore(&port->lock, flags);
1369 if (sport->lpuart_dma_rx_use) {
1370 del_timer_sync(&sport->lpuart_timer);
1371 lpuart_dma_rx_free(&sport->port);
1374 if (sport->lpuart_dma_tx_use) {
1375 if (wait_event_interruptible(sport->dma_wait,
1376 !sport->dma_tx_in_progress) != false) {
1377 sport->dma_tx_in_progress = false;
1378 dmaengine_terminate_all(sport->dma_tx_chan);
1381 lpuart_stop_tx(port);
1385 static void lpuart32_shutdown(struct uart_port *port)
1388 unsigned long flags;
1390 spin_lock_irqsave(&port->lock, flags);
1392 /* disable Rx/Tx and interrupts */
1393 temp = lpuart32_read(port, UARTCTRL);
1394 temp &= ~(UARTCTRL_TE | UARTCTRL_RE |
1395 UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
1396 lpuart32_write(port, temp, UARTCTRL);
1398 spin_unlock_irqrestore(&port->lock, flags);
1402 lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
1403 struct ktermios *old)
1405 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1406 unsigned long flags;
1407 unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
1409 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1410 unsigned int sbr, brfa;
1412 cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
1413 old_cr2 = readb(sport->port.membase + UARTCR2);
1414 cr3 = readb(sport->port.membase + UARTCR3);
1415 cr4 = readb(sport->port.membase + UARTCR4);
1416 bdh = readb(sport->port.membase + UARTBDH);
1417 modem = readb(sport->port.membase + UARTMODEM);
1419 * only support CS8 and CS7, and for CS7 must enable PE.
1426 while ((termios->c_cflag & CSIZE) != CS8 &&
1427 (termios->c_cflag & CSIZE) != CS7) {
1428 termios->c_cflag &= ~CSIZE;
1429 termios->c_cflag |= old_csize;
1433 if ((termios->c_cflag & CSIZE) == CS8 ||
1434 (termios->c_cflag & CSIZE) == CS7)
1435 cr1 = old_cr1 & ~UARTCR1_M;
1437 if (termios->c_cflag & CMSPAR) {
1438 if ((termios->c_cflag & CSIZE) != CS8) {
1439 termios->c_cflag &= ~CSIZE;
1440 termios->c_cflag |= CS8;
1446 * When auto RS-485 RTS mode is enabled,
1447 * hardware flow control need to be disabled.
1449 if (sport->port.rs485.flags & SER_RS485_ENABLED)
1450 termios->c_cflag &= ~CRTSCTS;
1452 if (termios->c_cflag & CRTSCTS) {
1453 modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1455 termios->c_cflag &= ~CRTSCTS;
1456 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1459 if (termios->c_cflag & CSTOPB)
1460 termios->c_cflag &= ~CSTOPB;
1462 /* parity must be enabled when CS7 to match 8-bits format */
1463 if ((termios->c_cflag & CSIZE) == CS7)
1464 termios->c_cflag |= PARENB;
1466 if ((termios->c_cflag & PARENB)) {
1467 if (termios->c_cflag & CMSPAR) {
1469 if (termios->c_cflag & PARODD)
1475 if ((termios->c_cflag & CSIZE) == CS8)
1477 if (termios->c_cflag & PARODD)
1484 /* ask the core to calculate the divisor */
1485 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1488 * Need to update the Ring buffer length according to the selected
1489 * baud rate and restart Rx DMA path.
1491 * Since timer function acqures sport->port.lock, need to stop before
1492 * acquring same lock because otherwise del_timer_sync() can deadlock.
1494 if (old && sport->lpuart_dma_rx_use) {
1495 del_timer_sync(&sport->lpuart_timer);
1496 lpuart_dma_rx_free(&sport->port);
1499 spin_lock_irqsave(&sport->port.lock, flags);
1501 sport->port.read_status_mask = 0;
1502 if (termios->c_iflag & INPCK)
1503 sport->port.read_status_mask |= (UARTSR1_FE | UARTSR1_PE);
1504 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1505 sport->port.read_status_mask |= UARTSR1_FE;
1507 /* characters to ignore */
1508 sport->port.ignore_status_mask = 0;
1509 if (termios->c_iflag & IGNPAR)
1510 sport->port.ignore_status_mask |= UARTSR1_PE;
1511 if (termios->c_iflag & IGNBRK) {
1512 sport->port.ignore_status_mask |= UARTSR1_FE;
1514 * if we're ignoring parity and break indicators,
1515 * ignore overruns too (for real raw support).
1517 if (termios->c_iflag & IGNPAR)
1518 sport->port.ignore_status_mask |= UARTSR1_OR;
1521 /* update the per-port timeout */
1522 uart_update_timeout(port, termios->c_cflag, baud);
1524 /* wait transmit engin complete */
1525 while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
1528 /* disable transmit and receive */
1529 writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
1530 sport->port.membase + UARTCR2);
1532 sbr = sport->port.uartclk / (16 * baud);
1533 brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
1534 bdh &= ~UARTBDH_SBR_MASK;
1535 bdh |= (sbr >> 8) & 0x1F;
1536 cr4 &= ~UARTCR4_BRFA_MASK;
1537 brfa &= UARTCR4_BRFA_MASK;
1538 writeb(cr4 | brfa, sport->port.membase + UARTCR4);
1539 writeb(bdh, sport->port.membase + UARTBDH);
1540 writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
1541 writeb(cr3, sport->port.membase + UARTCR3);
1542 writeb(cr1, sport->port.membase + UARTCR1);
1543 writeb(modem, sport->port.membase + UARTMODEM);
1545 /* restore control register */
1546 writeb(old_cr2, sport->port.membase + UARTCR2);
1548 if (old && sport->lpuart_dma_rx_use) {
1549 if (!lpuart_start_rx_dma(sport))
1550 rx_dma_timer_init(sport);
1552 sport->lpuart_dma_rx_use = false;
1555 spin_unlock_irqrestore(&sport->port.lock, flags);
1559 lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int baudrate)
1561 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
1562 u32 clk = sport->port.uartclk;
1565 * The idea is to use the best OSR (over-sampling rate) possible.
1566 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
1567 * Loop to find the best OSR value possible, one that generates minimum
1568 * baud_diff iterate through the rest of the supported values of OSR.
1570 * Calculation Formula:
1571 * Baud Rate = baud clock / ((OSR+1) × SBR)
1573 baud_diff = baudrate;
1577 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
1578 /* calculate the temporary sbr value */
1579 tmp_sbr = (clk / (baudrate * tmp_osr));
1584 * calculate the baud rate difference based on the temporary
1585 * osr and sbr values
1587 tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
1589 /* select best values between sbr and sbr+1 */
1590 tmp = clk / (tmp_osr * (tmp_sbr + 1));
1591 if (tmp_diff > (baudrate - tmp)) {
1592 tmp_diff = baudrate - tmp;
1596 if (tmp_diff <= baud_diff) {
1597 baud_diff = tmp_diff;
1606 /* handle buadrate outside acceptable rate */
1607 if (baud_diff > ((baudrate / 100) * 3))
1608 dev_warn(sport->port.dev,
1609 "unacceptable baud rate difference of more than 3%%\n");
1611 tmp = lpuart32_read(&sport->port, UARTBAUD);
1613 if ((osr > 3) && (osr < 8))
1614 tmp |= UARTBAUD_BOTHEDGE;
1616 tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
1617 tmp |= (((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT);
1619 tmp &= ~UARTBAUD_SBR_MASK;
1620 tmp |= sbr & UARTBAUD_SBR_MASK;
1622 tmp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
1624 lpuart32_write(&sport->port, tmp, UARTBAUD);
1628 lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
1629 struct ktermios *old)
1631 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1632 unsigned long flags;
1633 unsigned long ctrl, old_ctrl, modem;
1635 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1637 ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
1638 modem = lpuart32_read(&sport->port, UARTMODIR);
1640 * only support CS8 and CS7, and for CS7 must enable PE.
1647 while ((termios->c_cflag & CSIZE) != CS8 &&
1648 (termios->c_cflag & CSIZE) != CS7) {
1649 termios->c_cflag &= ~CSIZE;
1650 termios->c_cflag |= old_csize;
1654 if ((termios->c_cflag & CSIZE) == CS8 ||
1655 (termios->c_cflag & CSIZE) == CS7)
1656 ctrl = old_ctrl & ~UARTCTRL_M;
1658 if (termios->c_cflag & CMSPAR) {
1659 if ((termios->c_cflag & CSIZE) != CS8) {
1660 termios->c_cflag &= ~CSIZE;
1661 termios->c_cflag |= CS8;
1666 if (termios->c_cflag & CRTSCTS) {
1667 modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1669 termios->c_cflag &= ~CRTSCTS;
1670 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1673 if (termios->c_cflag & CSTOPB)
1674 termios->c_cflag &= ~CSTOPB;
1676 /* parity must be enabled when CS7 to match 8-bits format */
1677 if ((termios->c_cflag & CSIZE) == CS7)
1678 termios->c_cflag |= PARENB;
1680 if ((termios->c_cflag & PARENB)) {
1681 if (termios->c_cflag & CMSPAR) {
1682 ctrl &= ~UARTCTRL_PE;
1686 if ((termios->c_cflag & CSIZE) == CS8)
1688 if (termios->c_cflag & PARODD)
1689 ctrl |= UARTCTRL_PT;
1691 ctrl &= ~UARTCTRL_PT;
1695 /* ask the core to calculate the divisor */
1696 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1698 spin_lock_irqsave(&sport->port.lock, flags);
1700 sport->port.read_status_mask = 0;
1701 if (termios->c_iflag & INPCK)
1702 sport->port.read_status_mask |= (UARTSTAT_FE | UARTSTAT_PE);
1703 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1704 sport->port.read_status_mask |= UARTSTAT_FE;
1706 /* characters to ignore */
1707 sport->port.ignore_status_mask = 0;
1708 if (termios->c_iflag & IGNPAR)
1709 sport->port.ignore_status_mask |= UARTSTAT_PE;
1710 if (termios->c_iflag & IGNBRK) {
1711 sport->port.ignore_status_mask |= UARTSTAT_FE;
1713 * if we're ignoring parity and break indicators,
1714 * ignore overruns too (for real raw support).
1716 if (termios->c_iflag & IGNPAR)
1717 sport->port.ignore_status_mask |= UARTSTAT_OR;
1720 /* update the per-port timeout */
1721 uart_update_timeout(port, termios->c_cflag, baud);
1723 /* wait transmit engin complete */
1724 while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
1727 /* disable transmit and receive */
1728 lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
1731 lpuart32_serial_setbrg(sport, baud);
1732 lpuart32_write(&sport->port, modem, UARTMODIR);
1733 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1734 /* restore control register */
1736 spin_unlock_irqrestore(&sport->port.lock, flags);
1739 static const char *lpuart_type(struct uart_port *port)
1741 return "FSL_LPUART";
1744 static void lpuart_release_port(struct uart_port *port)
1749 static int lpuart_request_port(struct uart_port *port)
1754 /* configure/autoconfigure the port */
1755 static void lpuart_config_port(struct uart_port *port, int flags)
1757 if (flags & UART_CONFIG_TYPE)
1758 port->type = PORT_LPUART;
1761 static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
1765 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
1767 if (port->irq != ser->irq)
1769 if (ser->io_type != UPIO_MEM)
1771 if (port->uartclk / 16 != ser->baud_base)
1773 if (port->iobase != ser->port)
1780 static const struct uart_ops lpuart_pops = {
1781 .tx_empty = lpuart_tx_empty,
1782 .set_mctrl = lpuart_set_mctrl,
1783 .get_mctrl = lpuart_get_mctrl,
1784 .stop_tx = lpuart_stop_tx,
1785 .start_tx = lpuart_start_tx,
1786 .stop_rx = lpuart_stop_rx,
1787 .break_ctl = lpuart_break_ctl,
1788 .startup = lpuart_startup,
1789 .shutdown = lpuart_shutdown,
1790 .set_termios = lpuart_set_termios,
1791 .type = lpuart_type,
1792 .request_port = lpuart_request_port,
1793 .release_port = lpuart_release_port,
1794 .config_port = lpuart_config_port,
1795 .verify_port = lpuart_verify_port,
1796 .flush_buffer = lpuart_flush_buffer,
1797 #if defined(CONFIG_CONSOLE_POLL)
1798 .poll_init = lpuart_poll_init,
1799 .poll_get_char = lpuart_poll_get_char,
1800 .poll_put_char = lpuart_poll_put_char,
1804 static const struct uart_ops lpuart32_pops = {
1805 .tx_empty = lpuart32_tx_empty,
1806 .set_mctrl = lpuart32_set_mctrl,
1807 .get_mctrl = lpuart32_get_mctrl,
1808 .stop_tx = lpuart32_stop_tx,
1809 .start_tx = lpuart32_start_tx,
1810 .stop_rx = lpuart32_stop_rx,
1811 .break_ctl = lpuart32_break_ctl,
1812 .startup = lpuart32_startup,
1813 .shutdown = lpuart32_shutdown,
1814 .set_termios = lpuart32_set_termios,
1815 .type = lpuart_type,
1816 .request_port = lpuart_request_port,
1817 .release_port = lpuart_release_port,
1818 .config_port = lpuart_config_port,
1819 .verify_port = lpuart_verify_port,
1820 .flush_buffer = lpuart_flush_buffer,
1821 #if defined(CONFIG_CONSOLE_POLL)
1822 .poll_init = lpuart32_poll_init,
1823 .poll_get_char = lpuart32_poll_get_char,
1824 .poll_put_char = lpuart32_poll_put_char,
1828 static struct lpuart_port *lpuart_ports[UART_NR];
1830 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
1831 static void lpuart_console_putchar(struct uart_port *port, int ch)
1833 while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
1836 writeb(ch, port->membase + UARTDR);
1839 static void lpuart32_console_putchar(struct uart_port *port, int ch)
1841 while (!(lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE))
1844 lpuart32_write(port, ch, UARTDATA);
1848 lpuart_console_write(struct console *co, const char *s, unsigned int count)
1850 struct lpuart_port *sport = lpuart_ports[co->index];
1851 unsigned char old_cr2, cr2;
1852 unsigned long flags;
1855 if (sport->port.sysrq || oops_in_progress)
1856 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1858 spin_lock_irqsave(&sport->port.lock, flags);
1860 /* first save CR2 and then disable interrupts */
1861 cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
1862 cr2 |= (UARTCR2_TE | UARTCR2_RE);
1863 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1864 writeb(cr2, sport->port.membase + UARTCR2);
1866 uart_console_write(&sport->port, s, count, lpuart_console_putchar);
1868 /* wait for transmitter finish complete and restore CR2 */
1869 while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
1872 writeb(old_cr2, sport->port.membase + UARTCR2);
1875 spin_unlock_irqrestore(&sport->port.lock, flags);
1879 lpuart32_console_write(struct console *co, const char *s, unsigned int count)
1881 struct lpuart_port *sport = lpuart_ports[co->index];
1882 unsigned long old_cr, cr;
1883 unsigned long flags;
1886 if (sport->port.sysrq || oops_in_progress)
1887 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1889 spin_lock_irqsave(&sport->port.lock, flags);
1891 /* first save CR2 and then disable interrupts */
1892 cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
1893 cr |= (UARTCTRL_TE | UARTCTRL_RE);
1894 cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
1895 lpuart32_write(&sport->port, cr, UARTCTRL);
1897 uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
1899 /* wait for transmitter finish complete and restore CR2 */
1900 while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
1903 lpuart32_write(&sport->port, old_cr, UARTCTRL);
1906 spin_unlock_irqrestore(&sport->port.lock, flags);
1910 * if the port was already initialised (eg, by a boot loader),
1911 * try to determine the current setup.
1914 lpuart_console_get_options(struct lpuart_port *sport, int *baud,
1915 int *parity, int *bits)
1917 unsigned char cr, bdh, bdl, brfa;
1918 unsigned int sbr, uartclk, baud_raw;
1920 cr = readb(sport->port.membase + UARTCR2);
1921 cr &= UARTCR2_TE | UARTCR2_RE;
1925 /* ok, the port was enabled */
1927 cr = readb(sport->port.membase + UARTCR1);
1930 if (cr & UARTCR1_PE) {
1931 if (cr & UARTCR1_PT)
1942 bdh = readb(sport->port.membase + UARTBDH);
1943 bdh &= UARTBDH_SBR_MASK;
1944 bdl = readb(sport->port.membase + UARTBDL);
1948 brfa = readb(sport->port.membase + UARTCR4);
1949 brfa &= UARTCR4_BRFA_MASK;
1951 uartclk = clk_get_rate(sport->clk);
1953 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
1955 baud_raw = uartclk / (16 * (sbr + brfa / 32));
1957 if (*baud != baud_raw)
1958 printk(KERN_INFO "Serial: Console lpuart rounded baud rate"
1959 "from %d to %d\n", baud_raw, *baud);
1963 lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
1964 int *parity, int *bits)
1966 unsigned long cr, bd;
1967 unsigned int sbr, uartclk, baud_raw;
1969 cr = lpuart32_read(&sport->port, UARTCTRL);
1970 cr &= UARTCTRL_TE | UARTCTRL_RE;
1974 /* ok, the port was enabled */
1976 cr = lpuart32_read(&sport->port, UARTCTRL);
1979 if (cr & UARTCTRL_PE) {
1980 if (cr & UARTCTRL_PT)
1986 if (cr & UARTCTRL_M)
1991 bd = lpuart32_read(&sport->port, UARTBAUD);
1992 bd &= UARTBAUD_SBR_MASK;
1994 uartclk = clk_get_rate(sport->clk);
1996 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
1998 baud_raw = uartclk / (16 * sbr);
2000 if (*baud != baud_raw)
2001 printk(KERN_INFO "Serial: Console lpuart rounded baud rate"
2002 "from %d to %d\n", baud_raw, *baud);
2005 static int __init lpuart_console_setup(struct console *co, char *options)
2007 struct lpuart_port *sport;
2014 * check whether an invalid uart number has been specified, and
2015 * if so, search for the first available port that does have
2018 if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
2021 sport = lpuart_ports[co->index];
2026 uart_parse_options(options, &baud, &parity, &bits, &flow);
2028 if (lpuart_is_32(sport))
2029 lpuart32_console_get_options(sport, &baud, &parity, &bits);
2031 lpuart_console_get_options(sport, &baud, &parity, &bits);
2033 if (lpuart_is_32(sport))
2034 lpuart32_setup_watermark(sport);
2036 lpuart_setup_watermark(sport);
2038 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
2041 static struct uart_driver lpuart_reg;
2042 static struct console lpuart_console = {
2044 .write = lpuart_console_write,
2045 .device = uart_console_device,
2046 .setup = lpuart_console_setup,
2047 .flags = CON_PRINTBUFFER,
2049 .data = &lpuart_reg,
2052 static struct console lpuart32_console = {
2054 .write = lpuart32_console_write,
2055 .device = uart_console_device,
2056 .setup = lpuart_console_setup,
2057 .flags = CON_PRINTBUFFER,
2059 .data = &lpuart_reg,
2062 static void lpuart_early_write(struct console *con, const char *s, unsigned n)
2064 struct earlycon_device *dev = con->data;
2066 uart_console_write(&dev->port, s, n, lpuart_console_putchar);
2069 static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
2071 struct earlycon_device *dev = con->data;
2073 uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
2076 static int __init lpuart_early_console_setup(struct earlycon_device *device,
2079 if (!device->port.membase)
2082 device->con->write = lpuart_early_write;
2086 static int __init lpuart32_early_console_setup(struct earlycon_device *device,
2089 if (!device->port.membase)
2092 device->port.iotype = UPIO_MEM32BE;
2093 device->con->write = lpuart32_early_write;
2097 static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
2100 if (!device->port.membase)
2103 device->port.iotype = UPIO_MEM32;
2104 device->port.membase += IMX_REG_OFF;
2105 device->con->write = lpuart32_early_write;
2109 OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
2110 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2111 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2112 EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
2113 EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2115 #define LPUART_CONSOLE (&lpuart_console)
2116 #define LPUART32_CONSOLE (&lpuart32_console)
2118 #define LPUART_CONSOLE NULL
2119 #define LPUART32_CONSOLE NULL
2122 static struct uart_driver lpuart_reg = {
2123 .owner = THIS_MODULE,
2124 .driver_name = DRIVER_NAME,
2125 .dev_name = DEV_NAME,
2126 .nr = ARRAY_SIZE(lpuart_ports),
2127 .cons = LPUART_CONSOLE,
2130 static int lpuart_probe(struct platform_device *pdev)
2132 const struct of_device_id *of_id = of_match_device(lpuart_dt_ids,
2134 const struct lpuart_soc_data *sdata = of_id->data;
2135 struct device_node *np = pdev->dev.of_node;
2136 struct lpuart_port *sport;
2137 struct resource *res;
2140 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2144 pdev->dev.coherent_dma_mask = 0;
2146 ret = of_alias_get_id(np, "serial");
2148 ret = ida_simple_get(&fsl_lpuart_ida, 0, UART_NR, GFP_KERNEL);
2150 dev_err(&pdev->dev, "port line is full, add device failed\n");
2154 if (ret >= ARRAY_SIZE(lpuart_ports)) {
2155 dev_err(&pdev->dev, "serial%d out of range\n", ret);
2158 sport->port.line = ret;
2159 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2160 sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
2161 if (IS_ERR(sport->port.membase))
2162 return PTR_ERR(sport->port.membase);
2164 sport->port.membase += sdata->reg_off;
2165 sport->port.mapbase = res->start;
2166 sport->port.dev = &pdev->dev;
2167 sport->port.type = PORT_LPUART;
2168 ret = platform_get_irq(pdev, 0);
2170 dev_err(&pdev->dev, "cannot obtain irq\n");
2173 sport->port.irq = ret;
2174 sport->port.iotype = sdata->iotype;
2175 if (lpuart_is_32(sport))
2176 sport->port.ops = &lpuart32_pops;
2178 sport->port.ops = &lpuart_pops;
2179 sport->port.flags = UPF_BOOT_AUTOCONF;
2181 sport->port.rs485_config = lpuart_config_rs485;
2183 sport->clk = devm_clk_get(&pdev->dev, "ipg");
2184 if (IS_ERR(sport->clk)) {
2185 ret = PTR_ERR(sport->clk);
2186 dev_err(&pdev->dev, "failed to get uart clk: %d\n", ret);
2190 ret = clk_prepare_enable(sport->clk);
2192 dev_err(&pdev->dev, "failed to enable uart clk: %d\n", ret);
2196 sport->port.uartclk = clk_get_rate(sport->clk);
2198 lpuart_ports[sport->port.line] = sport;
2200 platform_set_drvdata(pdev, &sport->port);
2202 if (lpuart_is_32(sport)) {
2203 lpuart_reg.cons = LPUART32_CONSOLE;
2204 ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart32_int, 0,
2205 DRIVER_NAME, sport);
2207 lpuart_reg.cons = LPUART_CONSOLE;
2208 ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart_int, 0,
2209 DRIVER_NAME, sport);
2213 goto failed_irq_request;
2215 ret = uart_add_one_port(&lpuart_reg, &sport->port);
2217 goto failed_attach_port;
2219 uart_get_rs485_mode(&pdev->dev, &sport->port.rs485);
2221 if (sport->port.rs485.flags & SER_RS485_RX_DURING_TX)
2222 dev_err(&pdev->dev, "driver doesn't support RX during TX\n");
2224 if (sport->port.rs485.delay_rts_before_send ||
2225 sport->port.rs485.delay_rts_after_send)
2226 dev_err(&pdev->dev, "driver doesn't support RTS delays\n");
2228 lpuart_config_rs485(&sport->port, &sport->port.rs485);
2230 sport->dma_tx_chan = dma_request_slave_channel(sport->port.dev, "tx");
2231 if (!sport->dma_tx_chan)
2232 dev_info(sport->port.dev, "DMA tx channel request failed, "
2233 "operating without tx DMA\n");
2235 sport->dma_rx_chan = dma_request_slave_channel(sport->port.dev, "rx");
2236 if (!sport->dma_rx_chan)
2237 dev_info(sport->port.dev, "DMA rx channel request failed, "
2238 "operating without rx DMA\n");
2244 clk_disable_unprepare(sport->clk);
2248 static int lpuart_remove(struct platform_device *pdev)
2250 struct lpuart_port *sport = platform_get_drvdata(pdev);
2252 uart_remove_one_port(&lpuart_reg, &sport->port);
2254 ida_simple_remove(&fsl_lpuart_ida, sport->port.line);
2256 clk_disable_unprepare(sport->clk);
2258 if (sport->dma_tx_chan)
2259 dma_release_channel(sport->dma_tx_chan);
2261 if (sport->dma_rx_chan)
2262 dma_release_channel(sport->dma_rx_chan);
2267 #ifdef CONFIG_PM_SLEEP
2268 static int lpuart_suspend(struct device *dev)
2270 struct lpuart_port *sport = dev_get_drvdata(dev);
2274 if (lpuart_is_32(sport)) {
2275 /* disable Rx/Tx and interrupts */
2276 temp = lpuart32_read(&sport->port, UARTCTRL);
2277 temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
2278 lpuart32_write(&sport->port, temp, UARTCTRL);
2280 /* disable Rx/Tx and interrupts */
2281 temp = readb(sport->port.membase + UARTCR2);
2282 temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
2283 writeb(temp, sport->port.membase + UARTCR2);
2286 uart_suspend_port(&lpuart_reg, &sport->port);
2288 /* uart_suspend_port() might set wakeup flag */
2289 irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2291 if (sport->lpuart_dma_rx_use) {
2293 * EDMA driver during suspend will forcefully release any
2294 * non-idle DMA channels. If port wakeup is enabled or if port
2295 * is console port or 'no_console_suspend' is set the Rx DMA
2296 * cannot resume as as expected, hence gracefully release the
2297 * Rx DMA path before suspend and start Rx DMA path on resume.
2300 del_timer_sync(&sport->lpuart_timer);
2301 lpuart_dma_rx_free(&sport->port);
2304 /* Disable Rx DMA to use UART port as wakeup source */
2305 writeb(readb(sport->port.membase + UARTCR5) & ~UARTCR5_RDMAS,
2306 sport->port.membase + UARTCR5);
2309 if (sport->lpuart_dma_tx_use) {
2310 sport->dma_tx_in_progress = false;
2311 dmaengine_terminate_all(sport->dma_tx_chan);
2314 if (sport->port.suspended && !irq_wake)
2315 clk_disable_unprepare(sport->clk);
2320 static int lpuart_resume(struct device *dev)
2322 struct lpuart_port *sport = dev_get_drvdata(dev);
2323 bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2326 if (sport->port.suspended && !irq_wake)
2327 clk_prepare_enable(sport->clk);
2329 if (lpuart_is_32(sport)) {
2330 lpuart32_setup_watermark(sport);
2331 temp = lpuart32_read(&sport->port, UARTCTRL);
2332 temp |= (UARTCTRL_RIE | UARTCTRL_TIE | UARTCTRL_RE |
2333 UARTCTRL_TE | UARTCTRL_ILIE);
2334 lpuart32_write(&sport->port, temp, UARTCTRL);
2336 lpuart_setup_watermark(sport);
2337 temp = readb(sport->port.membase + UARTCR2);
2338 temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
2339 writeb(temp, sport->port.membase + UARTCR2);
2342 if (sport->lpuart_dma_rx_use) {
2344 if (!lpuart_start_rx_dma(sport))
2345 rx_dma_timer_init(sport);
2347 sport->lpuart_dma_rx_use = false;
2351 if (sport->dma_tx_chan && !lpuart_dma_tx_request(&sport->port)) {
2352 init_waitqueue_head(&sport->dma_wait);
2353 sport->lpuart_dma_tx_use = true;
2354 writeb(readb(sport->port.membase + UARTCR5) |
2355 UARTCR5_TDMAS, sport->port.membase + UARTCR5);
2357 sport->lpuart_dma_tx_use = false;
2360 uart_resume_port(&lpuart_reg, &sport->port);
2366 static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
2368 static struct platform_driver lpuart_driver = {
2369 .probe = lpuart_probe,
2370 .remove = lpuart_remove,
2372 .name = "fsl-lpuart",
2373 .of_match_table = lpuart_dt_ids,
2374 .pm = &lpuart_pm_ops,
2378 static int __init lpuart_serial_init(void)
2380 int ret = uart_register_driver(&lpuart_reg);
2385 ret = platform_driver_register(&lpuart_driver);
2387 uart_unregister_driver(&lpuart_reg);
2392 static void __exit lpuart_serial_exit(void)
2394 ida_destroy(&fsl_lpuart_ida);
2395 platform_driver_unregister(&lpuart_driver);
2396 uart_unregister_driver(&lpuart_reg);
2399 module_init(lpuart_serial_init);
2400 module_exit(lpuart_serial_exit);
2402 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
2403 MODULE_LICENSE("GPL v2");