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)
1486 /* ask the core to calculate the divisor */
1487 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1490 * Need to update the Ring buffer length according to the selected
1491 * baud rate and restart Rx DMA path.
1493 * Since timer function acqures sport->port.lock, need to stop before
1494 * acquring same lock because otherwise del_timer_sync() can deadlock.
1496 if (old && sport->lpuart_dma_rx_use) {
1497 del_timer_sync(&sport->lpuart_timer);
1498 lpuart_dma_rx_free(&sport->port);
1501 spin_lock_irqsave(&sport->port.lock, flags);
1503 sport->port.read_status_mask = 0;
1504 if (termios->c_iflag & INPCK)
1505 sport->port.read_status_mask |= (UARTSR1_FE | UARTSR1_PE);
1506 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1507 sport->port.read_status_mask |= UARTSR1_FE;
1509 /* characters to ignore */
1510 sport->port.ignore_status_mask = 0;
1511 if (termios->c_iflag & IGNPAR)
1512 sport->port.ignore_status_mask |= UARTSR1_PE;
1513 if (termios->c_iflag & IGNBRK) {
1514 sport->port.ignore_status_mask |= UARTSR1_FE;
1516 * if we're ignoring parity and break indicators,
1517 * ignore overruns too (for real raw support).
1519 if (termios->c_iflag & IGNPAR)
1520 sport->port.ignore_status_mask |= UARTSR1_OR;
1523 /* update the per-port timeout */
1524 uart_update_timeout(port, termios->c_cflag, baud);
1526 /* wait transmit engin complete */
1527 while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
1530 /* disable transmit and receive */
1531 writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
1532 sport->port.membase + UARTCR2);
1534 sbr = sport->port.uartclk / (16 * baud);
1535 brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
1536 bdh &= ~UARTBDH_SBR_MASK;
1537 bdh |= (sbr >> 8) & 0x1F;
1538 cr4 &= ~UARTCR4_BRFA_MASK;
1539 brfa &= UARTCR4_BRFA_MASK;
1540 writeb(cr4 | brfa, sport->port.membase + UARTCR4);
1541 writeb(bdh, sport->port.membase + UARTBDH);
1542 writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
1543 writeb(cr3, sport->port.membase + UARTCR3);
1544 writeb(cr1, sport->port.membase + UARTCR1);
1545 writeb(modem, sport->port.membase + UARTMODEM);
1547 /* restore control register */
1548 writeb(old_cr2, sport->port.membase + UARTCR2);
1550 if (old && sport->lpuart_dma_rx_use) {
1551 if (!lpuart_start_rx_dma(sport))
1552 rx_dma_timer_init(sport);
1554 sport->lpuart_dma_rx_use = false;
1557 spin_unlock_irqrestore(&sport->port.lock, flags);
1561 lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int baudrate)
1563 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
1564 u32 clk = sport->port.uartclk;
1567 * The idea is to use the best OSR (over-sampling rate) possible.
1568 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
1569 * Loop to find the best OSR value possible, one that generates minimum
1570 * baud_diff iterate through the rest of the supported values of OSR.
1572 * Calculation Formula:
1573 * Baud Rate = baud clock / ((OSR+1) × SBR)
1575 baud_diff = baudrate;
1579 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
1580 /* calculate the temporary sbr value */
1581 tmp_sbr = (clk / (baudrate * tmp_osr));
1586 * calculate the baud rate difference based on the temporary
1587 * osr and sbr values
1589 tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
1591 /* select best values between sbr and sbr+1 */
1592 tmp = clk / (tmp_osr * (tmp_sbr + 1));
1593 if (tmp_diff > (baudrate - tmp)) {
1594 tmp_diff = baudrate - tmp;
1598 if (tmp_diff <= baud_diff) {
1599 baud_diff = tmp_diff;
1608 /* handle buadrate outside acceptable rate */
1609 if (baud_diff > ((baudrate / 100) * 3))
1610 dev_warn(sport->port.dev,
1611 "unacceptable baud rate difference of more than 3%%\n");
1613 tmp = lpuart32_read(&sport->port, UARTBAUD);
1615 if ((osr > 3) && (osr < 8))
1616 tmp |= UARTBAUD_BOTHEDGE;
1618 tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
1619 tmp |= (((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT);
1621 tmp &= ~UARTBAUD_SBR_MASK;
1622 tmp |= sbr & UARTBAUD_SBR_MASK;
1624 tmp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
1626 lpuart32_write(&sport->port, tmp, UARTBAUD);
1630 lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
1631 struct ktermios *old)
1633 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1634 unsigned long flags;
1635 unsigned long ctrl, old_ctrl, modem;
1637 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1639 ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
1640 modem = lpuart32_read(&sport->port, UARTMODIR);
1642 * only support CS8 and CS7, and for CS7 must enable PE.
1649 while ((termios->c_cflag & CSIZE) != CS8 &&
1650 (termios->c_cflag & CSIZE) != CS7) {
1651 termios->c_cflag &= ~CSIZE;
1652 termios->c_cflag |= old_csize;
1656 if ((termios->c_cflag & CSIZE) == CS8 ||
1657 (termios->c_cflag & CSIZE) == CS7)
1658 ctrl = old_ctrl & ~UARTCTRL_M;
1660 if (termios->c_cflag & CMSPAR) {
1661 if ((termios->c_cflag & CSIZE) != CS8) {
1662 termios->c_cflag &= ~CSIZE;
1663 termios->c_cflag |= CS8;
1668 if (termios->c_cflag & CRTSCTS) {
1669 modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1671 termios->c_cflag &= ~CRTSCTS;
1672 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1675 if (termios->c_cflag & CSTOPB)
1676 termios->c_cflag &= ~CSTOPB;
1678 /* parity must be enabled when CS7 to match 8-bits format */
1679 if ((termios->c_cflag & CSIZE) == CS7)
1680 termios->c_cflag |= PARENB;
1682 if ((termios->c_cflag & PARENB)) {
1683 if (termios->c_cflag & CMSPAR) {
1684 ctrl &= ~UARTCTRL_PE;
1687 ctrl |= UARTCTRL_PE;
1688 if ((termios->c_cflag & CSIZE) == CS8)
1690 if (termios->c_cflag & PARODD)
1691 ctrl |= UARTCTRL_PT;
1693 ctrl &= ~UARTCTRL_PT;
1696 ctrl &= ~UARTCTRL_PE;
1699 /* ask the core to calculate the divisor */
1700 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
1702 spin_lock_irqsave(&sport->port.lock, flags);
1704 sport->port.read_status_mask = 0;
1705 if (termios->c_iflag & INPCK)
1706 sport->port.read_status_mask |= (UARTSTAT_FE | UARTSTAT_PE);
1707 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1708 sport->port.read_status_mask |= UARTSTAT_FE;
1710 /* characters to ignore */
1711 sport->port.ignore_status_mask = 0;
1712 if (termios->c_iflag & IGNPAR)
1713 sport->port.ignore_status_mask |= UARTSTAT_PE;
1714 if (termios->c_iflag & IGNBRK) {
1715 sport->port.ignore_status_mask |= UARTSTAT_FE;
1717 * if we're ignoring parity and break indicators,
1718 * ignore overruns too (for real raw support).
1720 if (termios->c_iflag & IGNPAR)
1721 sport->port.ignore_status_mask |= UARTSTAT_OR;
1724 /* update the per-port timeout */
1725 uart_update_timeout(port, termios->c_cflag, baud);
1727 /* wait transmit engin complete */
1728 while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
1731 /* disable transmit and receive */
1732 lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
1735 lpuart32_serial_setbrg(sport, baud);
1736 lpuart32_write(&sport->port, modem, UARTMODIR);
1737 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1738 /* restore control register */
1740 spin_unlock_irqrestore(&sport->port.lock, flags);
1743 static const char *lpuart_type(struct uart_port *port)
1745 return "FSL_LPUART";
1748 static void lpuart_release_port(struct uart_port *port)
1753 static int lpuart_request_port(struct uart_port *port)
1758 /* configure/autoconfigure the port */
1759 static void lpuart_config_port(struct uart_port *port, int flags)
1761 if (flags & UART_CONFIG_TYPE)
1762 port->type = PORT_LPUART;
1765 static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
1769 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
1771 if (port->irq != ser->irq)
1773 if (ser->io_type != UPIO_MEM)
1775 if (port->uartclk / 16 != ser->baud_base)
1777 if (port->iobase != ser->port)
1784 static const struct uart_ops lpuart_pops = {
1785 .tx_empty = lpuart_tx_empty,
1786 .set_mctrl = lpuart_set_mctrl,
1787 .get_mctrl = lpuart_get_mctrl,
1788 .stop_tx = lpuart_stop_tx,
1789 .start_tx = lpuart_start_tx,
1790 .stop_rx = lpuart_stop_rx,
1791 .break_ctl = lpuart_break_ctl,
1792 .startup = lpuart_startup,
1793 .shutdown = lpuart_shutdown,
1794 .set_termios = lpuart_set_termios,
1795 .type = lpuart_type,
1796 .request_port = lpuart_request_port,
1797 .release_port = lpuart_release_port,
1798 .config_port = lpuart_config_port,
1799 .verify_port = lpuart_verify_port,
1800 .flush_buffer = lpuart_flush_buffer,
1801 #if defined(CONFIG_CONSOLE_POLL)
1802 .poll_init = lpuart_poll_init,
1803 .poll_get_char = lpuart_poll_get_char,
1804 .poll_put_char = lpuart_poll_put_char,
1808 static const struct uart_ops lpuart32_pops = {
1809 .tx_empty = lpuart32_tx_empty,
1810 .set_mctrl = lpuart32_set_mctrl,
1811 .get_mctrl = lpuart32_get_mctrl,
1812 .stop_tx = lpuart32_stop_tx,
1813 .start_tx = lpuart32_start_tx,
1814 .stop_rx = lpuart32_stop_rx,
1815 .break_ctl = lpuart32_break_ctl,
1816 .startup = lpuart32_startup,
1817 .shutdown = lpuart32_shutdown,
1818 .set_termios = lpuart32_set_termios,
1819 .type = lpuart_type,
1820 .request_port = lpuart_request_port,
1821 .release_port = lpuart_release_port,
1822 .config_port = lpuart_config_port,
1823 .verify_port = lpuart_verify_port,
1824 .flush_buffer = lpuart_flush_buffer,
1825 #if defined(CONFIG_CONSOLE_POLL)
1826 .poll_init = lpuart32_poll_init,
1827 .poll_get_char = lpuart32_poll_get_char,
1828 .poll_put_char = lpuart32_poll_put_char,
1832 static struct lpuart_port *lpuart_ports[UART_NR];
1834 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
1835 static void lpuart_console_putchar(struct uart_port *port, int ch)
1837 while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
1840 writeb(ch, port->membase + UARTDR);
1843 static void lpuart32_console_putchar(struct uart_port *port, int ch)
1845 while (!(lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE))
1848 lpuart32_write(port, ch, UARTDATA);
1852 lpuart_console_write(struct console *co, const char *s, unsigned int count)
1854 struct lpuart_port *sport = lpuart_ports[co->index];
1855 unsigned char old_cr2, cr2;
1856 unsigned long flags;
1859 if (sport->port.sysrq || oops_in_progress)
1860 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1862 spin_lock_irqsave(&sport->port.lock, flags);
1864 /* first save CR2 and then disable interrupts */
1865 cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
1866 cr2 |= (UARTCR2_TE | UARTCR2_RE);
1867 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1868 writeb(cr2, sport->port.membase + UARTCR2);
1870 uart_console_write(&sport->port, s, count, lpuart_console_putchar);
1872 /* wait for transmitter finish complete and restore CR2 */
1873 while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
1876 writeb(old_cr2, sport->port.membase + UARTCR2);
1879 spin_unlock_irqrestore(&sport->port.lock, flags);
1883 lpuart32_console_write(struct console *co, const char *s, unsigned int count)
1885 struct lpuart_port *sport = lpuart_ports[co->index];
1886 unsigned long old_cr, cr;
1887 unsigned long flags;
1890 if (sport->port.sysrq || oops_in_progress)
1891 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1893 spin_lock_irqsave(&sport->port.lock, flags);
1895 /* first save CR2 and then disable interrupts */
1896 cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
1897 cr |= (UARTCTRL_TE | UARTCTRL_RE);
1898 cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
1899 lpuart32_write(&sport->port, cr, UARTCTRL);
1901 uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
1903 /* wait for transmitter finish complete and restore CR2 */
1904 while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
1907 lpuart32_write(&sport->port, old_cr, UARTCTRL);
1910 spin_unlock_irqrestore(&sport->port.lock, flags);
1914 * if the port was already initialised (eg, by a boot loader),
1915 * try to determine the current setup.
1918 lpuart_console_get_options(struct lpuart_port *sport, int *baud,
1919 int *parity, int *bits)
1921 unsigned char cr, bdh, bdl, brfa;
1922 unsigned int sbr, uartclk, baud_raw;
1924 cr = readb(sport->port.membase + UARTCR2);
1925 cr &= UARTCR2_TE | UARTCR2_RE;
1929 /* ok, the port was enabled */
1931 cr = readb(sport->port.membase + UARTCR1);
1934 if (cr & UARTCR1_PE) {
1935 if (cr & UARTCR1_PT)
1946 bdh = readb(sport->port.membase + UARTBDH);
1947 bdh &= UARTBDH_SBR_MASK;
1948 bdl = readb(sport->port.membase + UARTBDL);
1952 brfa = readb(sport->port.membase + UARTCR4);
1953 brfa &= UARTCR4_BRFA_MASK;
1955 uartclk = clk_get_rate(sport->clk);
1957 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
1959 baud_raw = uartclk / (16 * (sbr + brfa / 32));
1961 if (*baud != baud_raw)
1962 printk(KERN_INFO "Serial: Console lpuart rounded baud rate"
1963 "from %d to %d\n", baud_raw, *baud);
1967 lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
1968 int *parity, int *bits)
1970 unsigned long cr, bd;
1971 unsigned int sbr, uartclk, baud_raw;
1973 cr = lpuart32_read(&sport->port, UARTCTRL);
1974 cr &= UARTCTRL_TE | UARTCTRL_RE;
1978 /* ok, the port was enabled */
1980 cr = lpuart32_read(&sport->port, UARTCTRL);
1983 if (cr & UARTCTRL_PE) {
1984 if (cr & UARTCTRL_PT)
1990 if (cr & UARTCTRL_M)
1995 bd = lpuart32_read(&sport->port, UARTBAUD);
1996 bd &= UARTBAUD_SBR_MASK;
1998 uartclk = clk_get_rate(sport->clk);
2000 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2002 baud_raw = uartclk / (16 * sbr);
2004 if (*baud != baud_raw)
2005 printk(KERN_INFO "Serial: Console lpuart rounded baud rate"
2006 "from %d to %d\n", baud_raw, *baud);
2009 static int __init lpuart_console_setup(struct console *co, char *options)
2011 struct lpuart_port *sport;
2018 * check whether an invalid uart number has been specified, and
2019 * if so, search for the first available port that does have
2022 if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
2025 sport = lpuart_ports[co->index];
2030 uart_parse_options(options, &baud, &parity, &bits, &flow);
2032 if (lpuart_is_32(sport))
2033 lpuart32_console_get_options(sport, &baud, &parity, &bits);
2035 lpuart_console_get_options(sport, &baud, &parity, &bits);
2037 if (lpuart_is_32(sport))
2038 lpuart32_setup_watermark(sport);
2040 lpuart_setup_watermark(sport);
2042 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
2045 static struct uart_driver lpuart_reg;
2046 static struct console lpuart_console = {
2048 .write = lpuart_console_write,
2049 .device = uart_console_device,
2050 .setup = lpuart_console_setup,
2051 .flags = CON_PRINTBUFFER,
2053 .data = &lpuart_reg,
2056 static struct console lpuart32_console = {
2058 .write = lpuart32_console_write,
2059 .device = uart_console_device,
2060 .setup = lpuart_console_setup,
2061 .flags = CON_PRINTBUFFER,
2063 .data = &lpuart_reg,
2066 static void lpuart_early_write(struct console *con, const char *s, unsigned n)
2068 struct earlycon_device *dev = con->data;
2070 uart_console_write(&dev->port, s, n, lpuart_console_putchar);
2073 static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
2075 struct earlycon_device *dev = con->data;
2077 uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
2080 static int __init lpuart_early_console_setup(struct earlycon_device *device,
2083 if (!device->port.membase)
2086 device->con->write = lpuart_early_write;
2090 static int __init lpuart32_early_console_setup(struct earlycon_device *device,
2093 if (!device->port.membase)
2096 device->port.iotype = UPIO_MEM32BE;
2097 device->con->write = lpuart32_early_write;
2101 static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
2104 if (!device->port.membase)
2107 device->port.iotype = UPIO_MEM32;
2108 device->port.membase += IMX_REG_OFF;
2109 device->con->write = lpuart32_early_write;
2113 OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
2114 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2115 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2116 EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
2117 EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2119 #define LPUART_CONSOLE (&lpuart_console)
2120 #define LPUART32_CONSOLE (&lpuart32_console)
2122 #define LPUART_CONSOLE NULL
2123 #define LPUART32_CONSOLE NULL
2126 static struct uart_driver lpuart_reg = {
2127 .owner = THIS_MODULE,
2128 .driver_name = DRIVER_NAME,
2129 .dev_name = DEV_NAME,
2130 .nr = ARRAY_SIZE(lpuart_ports),
2131 .cons = LPUART_CONSOLE,
2134 static int lpuart_probe(struct platform_device *pdev)
2136 const struct of_device_id *of_id = of_match_device(lpuart_dt_ids,
2138 const struct lpuart_soc_data *sdata = of_id->data;
2139 struct device_node *np = pdev->dev.of_node;
2140 struct lpuart_port *sport;
2141 struct resource *res;
2144 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2148 pdev->dev.coherent_dma_mask = 0;
2150 ret = of_alias_get_id(np, "serial");
2152 ret = ida_simple_get(&fsl_lpuart_ida, 0, UART_NR, GFP_KERNEL);
2154 dev_err(&pdev->dev, "port line is full, add device failed\n");
2158 if (ret >= ARRAY_SIZE(lpuart_ports)) {
2159 dev_err(&pdev->dev, "serial%d out of range\n", ret);
2162 sport->port.line = ret;
2163 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2164 sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
2165 if (IS_ERR(sport->port.membase))
2166 return PTR_ERR(sport->port.membase);
2168 sport->port.membase += sdata->reg_off;
2169 sport->port.mapbase = res->start;
2170 sport->port.dev = &pdev->dev;
2171 sport->port.type = PORT_LPUART;
2172 ret = platform_get_irq(pdev, 0);
2174 dev_err(&pdev->dev, "cannot obtain irq\n");
2177 sport->port.irq = ret;
2178 sport->port.iotype = sdata->iotype;
2179 if (lpuart_is_32(sport))
2180 sport->port.ops = &lpuart32_pops;
2182 sport->port.ops = &lpuart_pops;
2183 sport->port.flags = UPF_BOOT_AUTOCONF;
2185 sport->port.rs485_config = lpuart_config_rs485;
2187 sport->clk = devm_clk_get(&pdev->dev, "ipg");
2188 if (IS_ERR(sport->clk)) {
2189 ret = PTR_ERR(sport->clk);
2190 dev_err(&pdev->dev, "failed to get uart clk: %d\n", ret);
2194 ret = clk_prepare_enable(sport->clk);
2196 dev_err(&pdev->dev, "failed to enable uart clk: %d\n", ret);
2200 sport->port.uartclk = clk_get_rate(sport->clk);
2202 lpuart_ports[sport->port.line] = sport;
2204 platform_set_drvdata(pdev, &sport->port);
2206 if (lpuart_is_32(sport)) {
2207 lpuart_reg.cons = LPUART32_CONSOLE;
2208 ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart32_int, 0,
2209 DRIVER_NAME, sport);
2211 lpuart_reg.cons = LPUART_CONSOLE;
2212 ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart_int, 0,
2213 DRIVER_NAME, sport);
2217 goto failed_irq_request;
2219 ret = uart_add_one_port(&lpuart_reg, &sport->port);
2221 goto failed_attach_port;
2223 uart_get_rs485_mode(&pdev->dev, &sport->port.rs485);
2225 if (sport->port.rs485.flags & SER_RS485_RX_DURING_TX)
2226 dev_err(&pdev->dev, "driver doesn't support RX during TX\n");
2228 if (sport->port.rs485.delay_rts_before_send ||
2229 sport->port.rs485.delay_rts_after_send)
2230 dev_err(&pdev->dev, "driver doesn't support RTS delays\n");
2232 lpuart_config_rs485(&sport->port, &sport->port.rs485);
2234 sport->dma_tx_chan = dma_request_slave_channel(sport->port.dev, "tx");
2235 if (!sport->dma_tx_chan)
2236 dev_info(sport->port.dev, "DMA tx channel request failed, "
2237 "operating without tx DMA\n");
2239 sport->dma_rx_chan = dma_request_slave_channel(sport->port.dev, "rx");
2240 if (!sport->dma_rx_chan)
2241 dev_info(sport->port.dev, "DMA rx channel request failed, "
2242 "operating without rx DMA\n");
2248 clk_disable_unprepare(sport->clk);
2252 static int lpuart_remove(struct platform_device *pdev)
2254 struct lpuart_port *sport = platform_get_drvdata(pdev);
2256 uart_remove_one_port(&lpuart_reg, &sport->port);
2258 ida_simple_remove(&fsl_lpuart_ida, sport->port.line);
2260 clk_disable_unprepare(sport->clk);
2262 if (sport->dma_tx_chan)
2263 dma_release_channel(sport->dma_tx_chan);
2265 if (sport->dma_rx_chan)
2266 dma_release_channel(sport->dma_rx_chan);
2271 #ifdef CONFIG_PM_SLEEP
2272 static int lpuart_suspend(struct device *dev)
2274 struct lpuart_port *sport = dev_get_drvdata(dev);
2278 if (lpuart_is_32(sport)) {
2279 /* disable Rx/Tx and interrupts */
2280 temp = lpuart32_read(&sport->port, UARTCTRL);
2281 temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
2282 lpuart32_write(&sport->port, temp, UARTCTRL);
2284 /* disable Rx/Tx and interrupts */
2285 temp = readb(sport->port.membase + UARTCR2);
2286 temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
2287 writeb(temp, sport->port.membase + UARTCR2);
2290 uart_suspend_port(&lpuart_reg, &sport->port);
2292 /* uart_suspend_port() might set wakeup flag */
2293 irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2295 if (sport->lpuart_dma_rx_use) {
2297 * EDMA driver during suspend will forcefully release any
2298 * non-idle DMA channels. If port wakeup is enabled or if port
2299 * is console port or 'no_console_suspend' is set the Rx DMA
2300 * cannot resume as as expected, hence gracefully release the
2301 * Rx DMA path before suspend and start Rx DMA path on resume.
2304 del_timer_sync(&sport->lpuart_timer);
2305 lpuart_dma_rx_free(&sport->port);
2308 /* Disable Rx DMA to use UART port as wakeup source */
2309 writeb(readb(sport->port.membase + UARTCR5) & ~UARTCR5_RDMAS,
2310 sport->port.membase + UARTCR5);
2313 if (sport->lpuart_dma_tx_use) {
2314 sport->dma_tx_in_progress = false;
2315 dmaengine_terminate_all(sport->dma_tx_chan);
2318 if (sport->port.suspended && !irq_wake)
2319 clk_disable_unprepare(sport->clk);
2324 static int lpuart_resume(struct device *dev)
2326 struct lpuart_port *sport = dev_get_drvdata(dev);
2327 bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2330 if (sport->port.suspended && !irq_wake)
2331 clk_prepare_enable(sport->clk);
2333 if (lpuart_is_32(sport)) {
2334 lpuart32_setup_watermark(sport);
2335 temp = lpuart32_read(&sport->port, UARTCTRL);
2336 temp |= (UARTCTRL_RIE | UARTCTRL_TIE | UARTCTRL_RE |
2337 UARTCTRL_TE | UARTCTRL_ILIE);
2338 lpuart32_write(&sport->port, temp, UARTCTRL);
2340 lpuart_setup_watermark(sport);
2341 temp = readb(sport->port.membase + UARTCR2);
2342 temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
2343 writeb(temp, sport->port.membase + UARTCR2);
2346 if (sport->lpuart_dma_rx_use) {
2348 if (!lpuart_start_rx_dma(sport))
2349 rx_dma_timer_init(sport);
2351 sport->lpuart_dma_rx_use = false;
2355 if (sport->dma_tx_chan && !lpuart_dma_tx_request(&sport->port)) {
2356 init_waitqueue_head(&sport->dma_wait);
2357 sport->lpuart_dma_tx_use = true;
2358 writeb(readb(sport->port.membase + UARTCR5) |
2359 UARTCR5_TDMAS, sport->port.membase + UARTCR5);
2361 sport->lpuart_dma_tx_use = false;
2364 uart_resume_port(&lpuart_reg, &sport->port);
2370 static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
2372 static struct platform_driver lpuart_driver = {
2373 .probe = lpuart_probe,
2374 .remove = lpuart_remove,
2376 .name = "fsl-lpuart",
2377 .of_match_table = lpuart_dt_ids,
2378 .pm = &lpuart_pm_ops,
2382 static int __init lpuart_serial_init(void)
2384 int ret = uart_register_driver(&lpuart_reg);
2389 ret = platform_driver_register(&lpuart_driver);
2391 uart_unregister_driver(&lpuart_reg);
2396 static void __exit lpuart_serial_exit(void)
2398 ida_destroy(&fsl_lpuart_ida);
2399 platform_driver_unregister(&lpuart_driver);
2400 uart_unregister_driver(&lpuart_reg);
2403 module_init(lpuart_serial_init);
2404 module_exit(lpuart_serial_exit);
2406 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
2407 MODULE_LICENSE("GPL v2");