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 dma_addr_t lpuart_dma_datareg_addr(struct lpuart_port *sport)
431 switch (sport->port.iotype) {
433 return sport->port.mapbase + UARTDATA;
435 return sport->port.mapbase + UARTDATA + sizeof(u32) - 1;
437 return sport->port.mapbase + UARTDR;
440 static int lpuart_dma_tx_request(struct uart_port *port)
442 struct lpuart_port *sport = container_of(port,
443 struct lpuart_port, port);
444 struct dma_slave_config dma_tx_sconfig = {};
447 dma_tx_sconfig.dst_addr = lpuart_dma_datareg_addr(sport);
448 dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
449 dma_tx_sconfig.dst_maxburst = 1;
450 dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
451 ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
454 dev_err(sport->port.dev,
455 "DMA slave config failed, err = %d\n", ret);
462 static void lpuart_flush_buffer(struct uart_port *port)
464 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
466 if (sport->lpuart_dma_tx_use) {
467 if (sport->dma_tx_in_progress) {
468 dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
469 sport->dma_tx_nents, DMA_TO_DEVICE);
470 sport->dma_tx_in_progress = false;
472 dmaengine_terminate_all(sport->dma_tx_chan);
476 #if defined(CONFIG_CONSOLE_POLL)
478 static int lpuart_poll_init(struct uart_port *port)
480 struct lpuart_port *sport = container_of(port,
481 struct lpuart_port, port);
485 sport->port.fifosize = 0;
487 spin_lock_irqsave(&sport->port.lock, flags);
488 /* Disable Rx & Tx */
489 writeb(0, sport->port.membase + UARTCR2);
491 temp = readb(sport->port.membase + UARTPFIFO);
492 /* Enable Rx and Tx FIFO */
493 writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
494 sport->port.membase + UARTPFIFO);
496 /* flush Tx and Rx FIFO */
497 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
498 sport->port.membase + UARTCFIFO);
500 /* explicitly clear RDRF */
501 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
502 readb(sport->port.membase + UARTDR);
503 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
506 writeb(0, sport->port.membase + UARTTWFIFO);
507 writeb(1, sport->port.membase + UARTRWFIFO);
509 /* Enable Rx and Tx */
510 writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
511 spin_unlock_irqrestore(&sport->port.lock, flags);
516 static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
519 while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
522 writeb(c, port->membase + UARTDR);
525 static int lpuart_poll_get_char(struct uart_port *port)
527 if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
530 return readb(port->membase + UARTDR);
533 static int lpuart32_poll_init(struct uart_port *port)
536 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
539 sport->port.fifosize = 0;
541 spin_lock_irqsave(&sport->port.lock, flags);
543 /* Disable Rx & Tx */
544 writel(0, sport->port.membase + UARTCTRL);
546 temp = readl(sport->port.membase + UARTFIFO);
548 /* Enable Rx and Tx FIFO */
549 writel(temp | UARTFIFO_RXFE | UARTFIFO_TXFE,
550 sport->port.membase + UARTFIFO);
552 /* flush Tx and Rx FIFO */
553 writel(UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH,
554 sport->port.membase + UARTFIFO);
556 /* explicitly clear RDRF */
557 if (readl(sport->port.membase + UARTSTAT) & UARTSTAT_RDRF) {
558 readl(sport->port.membase + UARTDATA);
559 writel(UARTFIFO_RXUF, sport->port.membase + UARTFIFO);
562 /* Enable Rx and Tx */
563 writel(UARTCTRL_RE | UARTCTRL_TE, sport->port.membase + UARTCTRL);
564 spin_unlock_irqrestore(&sport->port.lock, flags);
569 static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
571 while (!(readl(port->membase + UARTSTAT) & UARTSTAT_TDRE))
574 writel(c, port->membase + UARTDATA);
577 static int lpuart32_poll_get_char(struct uart_port *port)
579 if (!(readl(port->membase + UARTSTAT) & UARTSTAT_RDRF))
582 return readl(port->membase + UARTDATA);
586 static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
588 struct circ_buf *xmit = &sport->port.state->xmit;
590 while (!uart_circ_empty(xmit) &&
591 (readb(sport->port.membase + UARTTCFIFO) < sport->txfifo_size)) {
592 writeb(xmit->buf[xmit->tail], sport->port.membase + UARTDR);
593 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
594 sport->port.icount.tx++;
597 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
598 uart_write_wakeup(&sport->port);
600 if (uart_circ_empty(xmit))
601 lpuart_stop_tx(&sport->port);
604 static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
606 struct circ_buf *xmit = &sport->port.state->xmit;
609 txcnt = lpuart32_read(&sport->port, UARTWATER);
610 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
611 txcnt &= UARTWATER_COUNT_MASK;
612 while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
613 lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
614 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
615 sport->port.icount.tx++;
616 txcnt = lpuart32_read(&sport->port, UARTWATER);
617 txcnt = txcnt >> UARTWATER_TXCNT_OFF;
618 txcnt &= UARTWATER_COUNT_MASK;
621 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
622 uart_write_wakeup(&sport->port);
624 if (uart_circ_empty(xmit))
625 lpuart32_stop_tx(&sport->port);
628 static void lpuart_start_tx(struct uart_port *port)
630 struct lpuart_port *sport = container_of(port,
631 struct lpuart_port, port);
632 struct circ_buf *xmit = &sport->port.state->xmit;
635 temp = readb(port->membase + UARTCR2);
636 writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
638 if (sport->lpuart_dma_tx_use) {
639 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port))
640 lpuart_dma_tx(sport);
642 if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
643 lpuart_transmit_buffer(sport);
647 static void lpuart32_start_tx(struct uart_port *port)
649 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
650 struct circ_buf *xmit = &sport->port.state->xmit;
653 if (sport->lpuart_dma_tx_use) {
654 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port))
655 lpuart_dma_tx(sport);
657 temp = lpuart32_read(port, UARTCTRL);
658 lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
660 if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
661 lpuart32_transmit_buffer(sport);
665 /* return TIOCSER_TEMT when transmitter is not busy */
666 static unsigned int lpuart_tx_empty(struct uart_port *port)
668 struct lpuart_port *sport = container_of(port,
669 struct lpuart_port, port);
670 unsigned char sr1 = readb(port->membase + UARTSR1);
671 unsigned char sfifo = readb(port->membase + UARTSFIFO);
673 if (sport->dma_tx_in_progress)
676 if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
682 static unsigned int lpuart32_tx_empty(struct uart_port *port)
684 struct lpuart_port *sport = container_of(port,
685 struct lpuart_port, port);
686 unsigned long stat = lpuart32_read(port, UARTSTAT);
687 unsigned long sfifo = lpuart32_read(port, UARTFIFO);
689 if (sport->dma_tx_in_progress)
692 if (stat & UARTSTAT_TC && sfifo & UARTFIFO_TXEMPT)
698 static bool lpuart_is_32(struct lpuart_port *sport)
700 return sport->port.iotype == UPIO_MEM32 ||
701 sport->port.iotype == UPIO_MEM32BE;
704 static irqreturn_t lpuart_txint(int irq, void *dev_id)
706 struct lpuart_port *sport = dev_id;
707 struct circ_buf *xmit = &sport->port.state->xmit;
710 spin_lock_irqsave(&sport->port.lock, flags);
711 if (sport->port.x_char) {
712 if (lpuart_is_32(sport))
713 lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
715 writeb(sport->port.x_char, sport->port.membase + UARTDR);
719 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
720 if (lpuart_is_32(sport))
721 lpuart32_stop_tx(&sport->port);
723 lpuart_stop_tx(&sport->port);
727 if (lpuart_is_32(sport))
728 lpuart32_transmit_buffer(sport);
730 lpuart_transmit_buffer(sport);
732 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
733 uart_write_wakeup(&sport->port);
736 spin_unlock_irqrestore(&sport->port.lock, flags);
740 static irqreturn_t lpuart_rxint(int irq, void *dev_id)
742 struct lpuart_port *sport = dev_id;
743 unsigned int flg, ignored = 0;
744 struct tty_port *port = &sport->port.state->port;
746 unsigned char rx, sr;
748 spin_lock_irqsave(&sport->port.lock, flags);
750 while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
752 sport->port.icount.rx++;
754 * to clear the FE, OR, NF, FE, PE flags,
755 * read SR1 then read DR
757 sr = readb(sport->port.membase + UARTSR1);
758 rx = readb(sport->port.membase + UARTDR);
760 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
763 if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
765 sport->port.icount.parity++;
766 else if (sr & UARTSR1_FE)
767 sport->port.icount.frame++;
770 sport->port.icount.overrun++;
772 if (sr & sport->port.ignore_status_mask) {
778 sr &= sport->port.read_status_mask;
782 else if (sr & UARTSR1_FE)
789 sport->port.sysrq = 0;
793 tty_insert_flip_char(port, rx, flg);
797 spin_unlock_irqrestore(&sport->port.lock, flags);
799 tty_flip_buffer_push(port);
803 static irqreturn_t lpuart32_rxint(int irq, void *dev_id)
805 struct lpuart_port *sport = dev_id;
806 unsigned int flg, ignored = 0;
807 struct tty_port *port = &sport->port.state->port;
809 unsigned long rx, sr;
811 spin_lock_irqsave(&sport->port.lock, flags);
813 while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
815 sport->port.icount.rx++;
817 * to clear the FE, OR, NF, FE, PE flags,
818 * read STAT then read DATA reg
820 sr = lpuart32_read(&sport->port, UARTSTAT);
821 rx = lpuart32_read(&sport->port, UARTDATA);
824 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
827 if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
828 if (sr & UARTSTAT_PE)
829 sport->port.icount.parity++;
830 else if (sr & UARTSTAT_FE)
831 sport->port.icount.frame++;
833 if (sr & UARTSTAT_OR)
834 sport->port.icount.overrun++;
836 if (sr & sport->port.ignore_status_mask) {
842 sr &= sport->port.read_status_mask;
844 if (sr & UARTSTAT_PE)
846 else if (sr & UARTSTAT_FE)
849 if (sr & UARTSTAT_OR)
853 sport->port.sysrq = 0;
857 tty_insert_flip_char(port, rx, flg);
861 spin_unlock_irqrestore(&sport->port.lock, flags);
863 tty_flip_buffer_push(port);
867 static irqreturn_t lpuart_int(int irq, void *dev_id)
869 struct lpuart_port *sport = dev_id;
872 sts = readb(sport->port.membase + UARTSR1);
874 if (sts & UARTSR1_RDRF)
875 lpuart_rxint(irq, dev_id);
877 if (sts & UARTSR1_TDRE)
878 lpuart_txint(irq, dev_id);
883 static irqreturn_t lpuart32_int(int irq, void *dev_id)
885 struct lpuart_port *sport = dev_id;
886 unsigned long sts, rxcount;
888 sts = lpuart32_read(&sport->port, UARTSTAT);
889 rxcount = lpuart32_read(&sport->port, UARTWATER);
890 rxcount = rxcount >> UARTWATER_RXCNT_OFF;
892 if ((sts & UARTSTAT_RDRF || rxcount > 0) && !sport->lpuart_dma_rx_use)
893 lpuart32_rxint(irq, dev_id);
895 if ((sts & UARTSTAT_TDRE) && !sport->lpuart_dma_tx_use)
896 lpuart_txint(irq, dev_id);
898 lpuart32_write(&sport->port, sts, UARTSTAT);
902 static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
904 struct tty_port *port = &sport->port.state->port;
905 struct dma_tx_state state;
906 enum dma_status dmastat;
907 struct circ_buf *ring = &sport->rx_ring;
911 if (lpuart_is_32(sport)) {
912 unsigned long sr = lpuart32_read(&sport->port, UARTSTAT);
914 if (sr & (UARTSTAT_PE | UARTSTAT_FE)) {
915 /* Read DR to clear the error flags */
916 lpuart32_read(&sport->port, UARTDATA);
918 if (sr & UARTSTAT_PE)
919 sport->port.icount.parity++;
920 else if (sr & UARTSTAT_FE)
921 sport->port.icount.frame++;
924 unsigned char sr = readb(sport->port.membase + UARTSR1);
926 if (sr & (UARTSR1_PE | UARTSR1_FE)) {
927 /* Read DR to clear the error flags */
928 readb(sport->port.membase + UARTDR);
931 sport->port.icount.parity++;
932 else if (sr & UARTSR1_FE)
933 sport->port.icount.frame++;
937 async_tx_ack(sport->dma_rx_desc);
939 spin_lock_irqsave(&sport->port.lock, flags);
941 dmastat = dmaengine_tx_status(sport->dma_rx_chan,
942 sport->dma_rx_cookie,
945 if (dmastat == DMA_ERROR) {
946 dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
947 spin_unlock_irqrestore(&sport->port.lock, flags);
951 /* CPU claims ownership of RX DMA buffer */
952 dma_sync_sg_for_cpu(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
955 * ring->head points to the end of data already written by the DMA.
956 * ring->tail points to the beginning of data to be read by the
958 * The current transfer size should not be larger than the dma buffer
961 ring->head = sport->rx_sgl.length - state.residue;
962 BUG_ON(ring->head > sport->rx_sgl.length);
964 * At this point ring->head may point to the first byte right after the
965 * last byte of the dma buffer:
966 * 0 <= ring->head <= sport->rx_sgl.length
968 * However ring->tail must always points inside the dma buffer:
969 * 0 <= ring->tail <= sport->rx_sgl.length - 1
971 * Since we use a ring buffer, we have to handle the case
972 * where head is lower than tail. In such a case, we first read from
973 * tail to the end of the buffer then reset tail.
975 if (ring->head < ring->tail) {
976 count = sport->rx_sgl.length - ring->tail;
978 tty_insert_flip_string(port, ring->buf + ring->tail, count);
980 sport->port.icount.rx += count;
983 /* Finally we read data from tail to head */
984 if (ring->tail < ring->head) {
985 count = ring->head - ring->tail;
986 tty_insert_flip_string(port, ring->buf + ring->tail, count);
987 /* Wrap ring->head if needed */
988 if (ring->head >= sport->rx_sgl.length)
990 ring->tail = ring->head;
991 sport->port.icount.rx += count;
994 dma_sync_sg_for_device(sport->port.dev, &sport->rx_sgl, 1,
997 spin_unlock_irqrestore(&sport->port.lock, flags);
999 tty_flip_buffer_push(port);
1000 mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
1003 static void lpuart_dma_rx_complete(void *arg)
1005 struct lpuart_port *sport = arg;
1007 lpuart_copy_rx_to_tty(sport);
1010 static void lpuart_timer_func(struct timer_list *t)
1012 struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
1014 lpuart_copy_rx_to_tty(sport);
1017 static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
1019 struct dma_slave_config dma_rx_sconfig = {};
1020 struct circ_buf *ring = &sport->rx_ring;
1023 struct tty_port *port = &sport->port.state->port;
1024 struct tty_struct *tty = port->tty;
1025 struct ktermios *termios = &tty->termios;
1027 baud = tty_get_baud_rate(tty);
1029 bits = (termios->c_cflag & CSIZE) == CS7 ? 9 : 10;
1030 if (termios->c_cflag & PARENB)
1034 * Calculate length of one DMA buffer size to keep latency below
1035 * 10ms at any baud rate.
1037 sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits / 1000) * 2;
1038 sport->rx_dma_rng_buf_len = (1 << (fls(sport->rx_dma_rng_buf_len) - 1));
1039 if (sport->rx_dma_rng_buf_len < 16)
1040 sport->rx_dma_rng_buf_len = 16;
1042 ring->buf = kmalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
1046 sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1047 sg_set_buf(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
1048 nent = dma_map_sg(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1051 dev_err(sport->port.dev, "DMA Rx mapping error\n");
1055 dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
1056 dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1057 dma_rx_sconfig.src_maxburst = 1;
1058 dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
1059 ret = dmaengine_slave_config(sport->dma_rx_chan, &dma_rx_sconfig);
1062 dev_err(sport->port.dev,
1063 "DMA Rx slave config failed, err = %d\n", ret);
1067 sport->dma_rx_desc = dmaengine_prep_dma_cyclic(sport->dma_rx_chan,
1068 sg_dma_address(&sport->rx_sgl),
1069 sport->rx_sgl.length,
1070 sport->rx_sgl.length / 2,
1072 DMA_PREP_INTERRUPT);
1073 if (!sport->dma_rx_desc) {
1074 dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
1078 sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
1079 sport->dma_rx_desc->callback_param = sport;
1080 sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
1081 dma_async_issue_pending(sport->dma_rx_chan);
1083 if (lpuart_is_32(sport)) {
1084 unsigned long temp = lpuart32_read(&sport->port, UARTBAUD);
1086 lpuart32_write(&sport->port, temp | UARTBAUD_RDMAE, UARTBAUD);
1088 writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
1089 sport->port.membase + UARTCR5);
1095 static void lpuart_dma_rx_free(struct uart_port *port)
1097 struct lpuart_port *sport = container_of(port,
1098 struct lpuart_port, port);
1100 if (sport->dma_rx_chan)
1101 dmaengine_terminate_all(sport->dma_rx_chan);
1103 dma_unmap_sg(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
1104 kfree(sport->rx_ring.buf);
1105 sport->rx_ring.tail = 0;
1106 sport->rx_ring.head = 0;
1107 sport->dma_rx_desc = NULL;
1108 sport->dma_rx_cookie = -EINVAL;
1111 static int lpuart_config_rs485(struct uart_port *port,
1112 struct serial_rs485 *rs485)
1114 struct lpuart_port *sport = container_of(port,
1115 struct lpuart_port, port);
1117 u8 modem = readb(sport->port.membase + UARTMODEM) &
1118 ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
1119 writeb(modem, sport->port.membase + UARTMODEM);
1121 /* clear unsupported configurations */
1122 rs485->delay_rts_before_send = 0;
1123 rs485->delay_rts_after_send = 0;
1124 rs485->flags &= ~SER_RS485_RX_DURING_TX;
1126 if (rs485->flags & SER_RS485_ENABLED) {
1127 /* Enable auto RS-485 RTS mode */
1128 modem |= UARTMODEM_TXRTSE;
1131 * RTS needs to be logic HIGH either during transer _or_ after
1132 * transfer, other variants are not supported by the hardware.
1135 if (!(rs485->flags & (SER_RS485_RTS_ON_SEND |
1136 SER_RS485_RTS_AFTER_SEND)))
1137 rs485->flags |= SER_RS485_RTS_ON_SEND;
1139 if (rs485->flags & SER_RS485_RTS_ON_SEND &&
1140 rs485->flags & SER_RS485_RTS_AFTER_SEND)
1141 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1144 * The hardware defaults to RTS logic HIGH while transfer.
1145 * Switch polarity in case RTS shall be logic HIGH
1147 * Note: UART is assumed to be active high.
1149 if (rs485->flags & SER_RS485_RTS_ON_SEND)
1150 modem &= ~UARTMODEM_TXRTSPOL;
1151 else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
1152 modem |= UARTMODEM_TXRTSPOL;
1155 /* Store the new configuration */
1156 sport->port.rs485 = *rs485;
1158 writeb(modem, sport->port.membase + UARTMODEM);
1162 static unsigned int lpuart_get_mctrl(struct uart_port *port)
1164 unsigned int temp = 0;
1167 reg = readb(port->membase + UARTMODEM);
1168 if (reg & UARTMODEM_TXCTSE)
1171 if (reg & UARTMODEM_RXRTSE)
1177 static unsigned int lpuart32_get_mctrl(struct uart_port *port)
1179 unsigned int temp = 0;
1182 reg = lpuart32_read(port, UARTMODIR);
1183 if (reg & UARTMODIR_TXCTSE)
1186 if (reg & UARTMODIR_RXRTSE)
1192 static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1195 struct lpuart_port *sport = container_of(port,
1196 struct lpuart_port, port);
1198 /* Make sure RXRTSE bit is not set when RS485 is enabled */
1199 if (!(sport->port.rs485.flags & SER_RS485_ENABLED)) {
1200 temp = readb(sport->port.membase + UARTMODEM) &
1201 ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1203 if (mctrl & TIOCM_RTS)
1204 temp |= UARTMODEM_RXRTSE;
1206 if (mctrl & TIOCM_CTS)
1207 temp |= UARTMODEM_TXCTSE;
1209 writeb(temp, port->membase + UARTMODEM);
1213 static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
1217 temp = lpuart32_read(port, UARTMODIR) &
1218 ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
1220 if (mctrl & TIOCM_RTS)
1221 temp |= UARTMODIR_RXRTSE;
1223 if (mctrl & TIOCM_CTS)
1224 temp |= UARTMODIR_TXCTSE;
1226 lpuart32_write(port, temp, UARTMODIR);
1229 static void lpuart_break_ctl(struct uart_port *port, int break_state)
1233 temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
1235 if (break_state != 0)
1236 temp |= UARTCR2_SBK;
1238 writeb(temp, port->membase + UARTCR2);
1241 static void lpuart32_break_ctl(struct uart_port *port, int break_state)
1245 temp = lpuart32_read(port, UARTCTRL) & ~UARTCTRL_SBK;
1247 if (break_state != 0)
1248 temp |= UARTCTRL_SBK;
1250 lpuart32_write(port, temp, UARTCTRL);
1253 static void lpuart_setup_watermark(struct lpuart_port *sport)
1255 unsigned char val, cr2;
1256 unsigned char cr2_saved;
1258 cr2 = readb(sport->port.membase + UARTCR2);
1260 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
1261 UARTCR2_RIE | UARTCR2_RE);
1262 writeb(cr2, sport->port.membase + UARTCR2);
1264 val = readb(sport->port.membase + UARTPFIFO);
1265 writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
1266 sport->port.membase + UARTPFIFO);
1268 /* flush Tx and Rx FIFO */
1269 writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
1270 sport->port.membase + UARTCFIFO);
1272 /* explicitly clear RDRF */
1273 if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
1274 readb(sport->port.membase + UARTDR);
1275 writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
1278 writeb(0, sport->port.membase + UARTTWFIFO);
1279 writeb(1, sport->port.membase + UARTRWFIFO);
1282 writeb(cr2_saved, sport->port.membase + UARTCR2);
1285 static void lpuart32_setup_watermark(struct lpuart_port *sport)
1287 unsigned long val, ctrl;
1288 unsigned long ctrl_saved;
1290 ctrl = lpuart32_read(&sport->port, UARTCTRL);
1292 ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
1293 UARTCTRL_RIE | UARTCTRL_RE);
1294 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1296 /* enable FIFO mode */
1297 val = lpuart32_read(&sport->port, UARTFIFO);
1298 val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
1299 val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
1300 lpuart32_write(&sport->port, val, UARTFIFO);
1302 /* set the watermark */
1303 val = (0x1 << UARTWATER_RXWATER_OFF) | (0x0 << UARTWATER_TXWATER_OFF);
1304 lpuart32_write(&sport->port, val, UARTWATER);
1307 lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
1310 static void rx_dma_timer_init(struct lpuart_port *sport)
1312 timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
1313 sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
1314 add_timer(&sport->lpuart_timer);
1317 static int lpuart_startup(struct uart_port *port)
1319 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1320 unsigned long flags;
1323 /* determine FIFO size and enable FIFO mode */
1324 temp = readb(sport->port.membase + UARTPFIFO);
1326 sport->txfifo_size = 0x1 << (((temp >> UARTPFIFO_TXSIZE_OFF) &
1327 UARTPFIFO_FIFOSIZE_MASK) + 1);
1329 sport->port.fifosize = sport->txfifo_size;
1331 sport->rxfifo_size = 0x1 << (((temp >> UARTPFIFO_RXSIZE_OFF) &
1332 UARTPFIFO_FIFOSIZE_MASK) + 1);
1334 spin_lock_irqsave(&sport->port.lock, flags);
1336 lpuart_setup_watermark(sport);
1338 temp = readb(sport->port.membase + UARTCR2);
1339 temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
1340 writeb(temp, sport->port.membase + UARTCR2);
1342 if (sport->dma_rx_chan && !lpuart_start_rx_dma(sport)) {
1343 /* set Rx DMA timeout */
1344 sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
1345 if (!sport->dma_rx_timeout)
1346 sport->dma_rx_timeout = 1;
1348 sport->lpuart_dma_rx_use = true;
1349 rx_dma_timer_init(sport);
1351 sport->lpuart_dma_rx_use = false;
1354 if (sport->dma_tx_chan && !lpuart_dma_tx_request(port)) {
1355 init_waitqueue_head(&sport->dma_wait);
1356 sport->lpuart_dma_tx_use = true;
1357 temp = readb(port->membase + UARTCR5);
1358 writeb(temp | UARTCR5_TDMAS, port->membase + UARTCR5);
1360 sport->lpuart_dma_tx_use = false;
1363 spin_unlock_irqrestore(&sport->port.lock, flags);
1368 static int lpuart32_startup(struct uart_port *port)
1370 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1371 unsigned long flags;
1374 /* determine FIFO size */
1375 temp = lpuart32_read(&sport->port, UARTFIFO);
1377 sport->txfifo_size = 0x1 << (((temp >> UARTFIFO_TXSIZE_OFF) &
1378 UARTFIFO_FIFOSIZE_MASK) - 1);
1380 sport->port.fifosize = sport->txfifo_size;
1382 sport->rxfifo_size = 0x1 << (((temp >> UARTFIFO_RXSIZE_OFF) &
1383 UARTFIFO_FIFOSIZE_MASK) - 1);
1385 spin_lock_irqsave(&sport->port.lock, flags);
1387 lpuart32_setup_watermark(sport);
1389 temp = lpuart32_read(&sport->port, UARTCTRL);
1390 temp |= UARTCTRL_RE | UARTCTRL_TE | UARTCTRL_ILIE;
1391 lpuart32_write(&sport->port, temp, UARTCTRL);
1393 if (sport->dma_rx_chan && !lpuart_start_rx_dma(sport)) {
1394 /* set Rx DMA timeout */
1395 sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
1396 if (!sport->dma_rx_timeout)
1397 sport->dma_rx_timeout = 1;
1399 sport->lpuart_dma_rx_use = true;
1400 rx_dma_timer_init(sport);
1402 sport->lpuart_dma_rx_use = false;
1405 if (sport->dma_tx_chan && !lpuart_dma_tx_request(port)) {
1406 init_waitqueue_head(&sport->dma_wait);
1407 sport->lpuart_dma_tx_use = true;
1408 temp = lpuart32_read(&sport->port, UARTBAUD);
1409 lpuart32_write(&sport->port, temp | UARTBAUD_TDMAE, UARTBAUD);
1411 sport->lpuart_dma_tx_use = false;
1414 if (sport->lpuart_dma_rx_use) {
1415 /* RXWATER must be 0 */
1416 temp = lpuart32_read(&sport->port, UARTWATER);
1417 temp &= ~(UARTWATER_WATER_MASK << UARTWATER_RXWATER_OFF);
1418 lpuart32_write(&sport->port, temp, UARTWATER);
1420 temp = lpuart32_read(&sport->port, UARTCTRL);
1421 if (!sport->lpuart_dma_rx_use)
1422 temp |= UARTCTRL_RIE;
1423 if (!sport->lpuart_dma_tx_use)
1424 temp |= UARTCTRL_TIE;
1425 lpuart32_write(&sport->port, temp, UARTCTRL);
1427 spin_unlock_irqrestore(&sport->port.lock, flags);
1431 static void lpuart_shutdown(struct uart_port *port)
1433 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1435 unsigned long flags;
1437 spin_lock_irqsave(&port->lock, flags);
1439 /* disable Rx/Tx and interrupts */
1440 temp = readb(port->membase + UARTCR2);
1441 temp &= ~(UARTCR2_TE | UARTCR2_RE |
1442 UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1443 writeb(temp, port->membase + UARTCR2);
1445 spin_unlock_irqrestore(&port->lock, flags);
1447 if (sport->lpuart_dma_rx_use) {
1448 del_timer_sync(&sport->lpuart_timer);
1449 lpuart_dma_rx_free(&sport->port);
1452 if (sport->lpuart_dma_tx_use) {
1453 if (wait_event_interruptible(sport->dma_wait,
1454 !sport->dma_tx_in_progress) != false) {
1455 sport->dma_tx_in_progress = false;
1456 dmaengine_terminate_all(sport->dma_tx_chan);
1459 lpuart_stop_tx(port);
1463 static void lpuart32_shutdown(struct uart_port *port)
1465 struct lpuart_port *sport =
1466 container_of(port, struct lpuart_port, port);
1468 unsigned long flags;
1470 spin_lock_irqsave(&port->lock, flags);
1472 /* disable Rx/Tx and interrupts */
1473 temp = lpuart32_read(port, UARTCTRL);
1474 temp &= ~(UARTCTRL_TE | UARTCTRL_RE |
1475 UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
1476 lpuart32_write(port, temp, UARTCTRL);
1478 spin_unlock_irqrestore(&port->lock, flags);
1480 if (sport->lpuart_dma_rx_use) {
1481 del_timer_sync(&sport->lpuart_timer);
1482 lpuart_dma_rx_free(&sport->port);
1485 if (sport->lpuart_dma_tx_use) {
1486 if (wait_event_interruptible(sport->dma_wait,
1487 !sport->dma_tx_in_progress)) {
1488 sport->dma_tx_in_progress = false;
1489 dmaengine_terminate_all(sport->dma_tx_chan);
1492 lpuart32_stop_tx(port);
1497 lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
1498 struct ktermios *old)
1500 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1501 unsigned long flags;
1502 unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
1504 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1505 unsigned int sbr, brfa;
1507 cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
1508 old_cr2 = readb(sport->port.membase + UARTCR2);
1509 cr3 = readb(sport->port.membase + UARTCR3);
1510 cr4 = readb(sport->port.membase + UARTCR4);
1511 bdh = readb(sport->port.membase + UARTBDH);
1512 modem = readb(sport->port.membase + UARTMODEM);
1514 * only support CS8 and CS7, and for CS7 must enable PE.
1521 while ((termios->c_cflag & CSIZE) != CS8 &&
1522 (termios->c_cflag & CSIZE) != CS7) {
1523 termios->c_cflag &= ~CSIZE;
1524 termios->c_cflag |= old_csize;
1528 if ((termios->c_cflag & CSIZE) == CS8 ||
1529 (termios->c_cflag & CSIZE) == CS7)
1530 cr1 = old_cr1 & ~UARTCR1_M;
1532 if (termios->c_cflag & CMSPAR) {
1533 if ((termios->c_cflag & CSIZE) != CS8) {
1534 termios->c_cflag &= ~CSIZE;
1535 termios->c_cflag |= CS8;
1541 * When auto RS-485 RTS mode is enabled,
1542 * hardware flow control need to be disabled.
1544 if (sport->port.rs485.flags & SER_RS485_ENABLED)
1545 termios->c_cflag &= ~CRTSCTS;
1547 if (termios->c_cflag & CRTSCTS) {
1548 modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1550 termios->c_cflag &= ~CRTSCTS;
1551 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1554 if (termios->c_cflag & CSTOPB)
1555 termios->c_cflag &= ~CSTOPB;
1557 /* parity must be enabled when CS7 to match 8-bits format */
1558 if ((termios->c_cflag & CSIZE) == CS7)
1559 termios->c_cflag |= PARENB;
1561 if ((termios->c_cflag & PARENB)) {
1562 if (termios->c_cflag & CMSPAR) {
1564 if (termios->c_cflag & PARODD)
1570 if ((termios->c_cflag & CSIZE) == CS8)
1572 if (termios->c_cflag & PARODD)
1581 /* ask the core to calculate the divisor */
1582 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1585 * Need to update the Ring buffer length according to the selected
1586 * baud rate and restart Rx DMA path.
1588 * Since timer function acqures sport->port.lock, need to stop before
1589 * acquring same lock because otherwise del_timer_sync() can deadlock.
1591 if (old && sport->lpuart_dma_rx_use) {
1592 del_timer_sync(&sport->lpuart_timer);
1593 lpuart_dma_rx_free(&sport->port);
1596 spin_lock_irqsave(&sport->port.lock, flags);
1598 sport->port.read_status_mask = 0;
1599 if (termios->c_iflag & INPCK)
1600 sport->port.read_status_mask |= (UARTSR1_FE | UARTSR1_PE);
1601 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1602 sport->port.read_status_mask |= UARTSR1_FE;
1604 /* characters to ignore */
1605 sport->port.ignore_status_mask = 0;
1606 if (termios->c_iflag & IGNPAR)
1607 sport->port.ignore_status_mask |= UARTSR1_PE;
1608 if (termios->c_iflag & IGNBRK) {
1609 sport->port.ignore_status_mask |= UARTSR1_FE;
1611 * if we're ignoring parity and break indicators,
1612 * ignore overruns too (for real raw support).
1614 if (termios->c_iflag & IGNPAR)
1615 sport->port.ignore_status_mask |= UARTSR1_OR;
1618 /* update the per-port timeout */
1619 uart_update_timeout(port, termios->c_cflag, baud);
1621 /* wait transmit engin complete */
1622 while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
1625 /* disable transmit and receive */
1626 writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
1627 sport->port.membase + UARTCR2);
1629 sbr = sport->port.uartclk / (16 * baud);
1630 brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
1631 bdh &= ~UARTBDH_SBR_MASK;
1632 bdh |= (sbr >> 8) & 0x1F;
1633 cr4 &= ~UARTCR4_BRFA_MASK;
1634 brfa &= UARTCR4_BRFA_MASK;
1635 writeb(cr4 | brfa, sport->port.membase + UARTCR4);
1636 writeb(bdh, sport->port.membase + UARTBDH);
1637 writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
1638 writeb(cr3, sport->port.membase + UARTCR3);
1639 writeb(cr1, sport->port.membase + UARTCR1);
1640 writeb(modem, sport->port.membase + UARTMODEM);
1642 /* restore control register */
1643 writeb(old_cr2, sport->port.membase + UARTCR2);
1645 if (old && sport->lpuart_dma_rx_use) {
1646 if (!lpuart_start_rx_dma(sport))
1647 rx_dma_timer_init(sport);
1649 sport->lpuart_dma_rx_use = false;
1652 spin_unlock_irqrestore(&sport->port.lock, flags);
1656 lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int baudrate)
1658 u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
1659 u32 clk = sport->port.uartclk;
1662 * The idea is to use the best OSR (over-sampling rate) possible.
1663 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
1664 * Loop to find the best OSR value possible, one that generates minimum
1665 * baud_diff iterate through the rest of the supported values of OSR.
1667 * Calculation Formula:
1668 * Baud Rate = baud clock / ((OSR+1) × SBR)
1670 baud_diff = baudrate;
1674 for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
1675 /* calculate the temporary sbr value */
1676 tmp_sbr = (clk / (baudrate * tmp_osr));
1681 * calculate the baud rate difference based on the temporary
1682 * osr and sbr values
1684 tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
1686 /* select best values between sbr and sbr+1 */
1687 tmp = clk / (tmp_osr * (tmp_sbr + 1));
1688 if (tmp_diff > (baudrate - tmp)) {
1689 tmp_diff = baudrate - tmp;
1693 if (tmp_diff <= baud_diff) {
1694 baud_diff = tmp_diff;
1703 /* handle buadrate outside acceptable rate */
1704 if (baud_diff > ((baudrate / 100) * 3))
1705 dev_warn(sport->port.dev,
1706 "unacceptable baud rate difference of more than 3%%\n");
1708 tmp = lpuart32_read(&sport->port, UARTBAUD);
1710 if ((osr > 3) && (osr < 8))
1711 tmp |= UARTBAUD_BOTHEDGE;
1713 tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
1714 tmp |= (((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT);
1716 tmp &= ~UARTBAUD_SBR_MASK;
1717 tmp |= sbr & UARTBAUD_SBR_MASK;
1719 if (!sport->lpuart_dma_rx_use)
1720 tmp &= ~UARTBAUD_RDMAE;
1721 if (!sport->lpuart_dma_tx_use)
1722 tmp &= ~UARTBAUD_TDMAE;
1724 lpuart32_write(&sport->port, tmp, UARTBAUD);
1728 lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
1729 struct ktermios *old)
1731 struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
1732 unsigned long flags;
1733 unsigned long ctrl, old_ctrl, modem;
1735 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
1737 ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
1738 modem = lpuart32_read(&sport->port, UARTMODIR);
1740 * only support CS8 and CS7, and for CS7 must enable PE.
1747 while ((termios->c_cflag & CSIZE) != CS8 &&
1748 (termios->c_cflag & CSIZE) != CS7) {
1749 termios->c_cflag &= ~CSIZE;
1750 termios->c_cflag |= old_csize;
1754 if ((termios->c_cflag & CSIZE) == CS8 ||
1755 (termios->c_cflag & CSIZE) == CS7)
1756 ctrl = old_ctrl & ~UARTCTRL_M;
1758 if (termios->c_cflag & CMSPAR) {
1759 if ((termios->c_cflag & CSIZE) != CS8) {
1760 termios->c_cflag &= ~CSIZE;
1761 termios->c_cflag |= CS8;
1766 if (termios->c_cflag & CRTSCTS) {
1767 modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1769 termios->c_cflag &= ~CRTSCTS;
1770 modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
1773 if (termios->c_cflag & CSTOPB)
1774 termios->c_cflag &= ~CSTOPB;
1776 /* parity must be enabled when CS7 to match 8-bits format */
1777 if ((termios->c_cflag & CSIZE) == CS7)
1778 termios->c_cflag |= PARENB;
1780 if ((termios->c_cflag & PARENB)) {
1781 if (termios->c_cflag & CMSPAR) {
1782 ctrl &= ~UARTCTRL_PE;
1785 ctrl |= UARTCTRL_PE;
1786 if ((termios->c_cflag & CSIZE) == CS8)
1788 if (termios->c_cflag & PARODD)
1789 ctrl |= UARTCTRL_PT;
1791 ctrl &= ~UARTCTRL_PT;
1794 ctrl &= ~UARTCTRL_PE;
1797 /* ask the core to calculate the divisor */
1798 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
1801 * Need to update the Ring buffer length according to the selected
1802 * baud rate and restart Rx DMA path.
1804 * Since timer function acqures sport->port.lock, need to stop before
1805 * acquring same lock because otherwise del_timer_sync() can deadlock.
1807 if (old && sport->lpuart_dma_rx_use) {
1808 del_timer_sync(&sport->lpuart_timer);
1809 lpuart_dma_rx_free(&sport->port);
1812 spin_lock_irqsave(&sport->port.lock, flags);
1814 sport->port.read_status_mask = 0;
1815 if (termios->c_iflag & INPCK)
1816 sport->port.read_status_mask |= (UARTSTAT_FE | UARTSTAT_PE);
1817 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1818 sport->port.read_status_mask |= UARTSTAT_FE;
1820 /* characters to ignore */
1821 sport->port.ignore_status_mask = 0;
1822 if (termios->c_iflag & IGNPAR)
1823 sport->port.ignore_status_mask |= UARTSTAT_PE;
1824 if (termios->c_iflag & IGNBRK) {
1825 sport->port.ignore_status_mask |= UARTSTAT_FE;
1827 * if we're ignoring parity and break indicators,
1828 * ignore overruns too (for real raw support).
1830 if (termios->c_iflag & IGNPAR)
1831 sport->port.ignore_status_mask |= UARTSTAT_OR;
1834 /* update the per-port timeout */
1835 uart_update_timeout(port, termios->c_cflag, baud);
1837 /* wait transmit engin complete */
1838 while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
1841 /* disable transmit and receive */
1842 lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
1845 lpuart32_serial_setbrg(sport, baud);
1846 lpuart32_write(&sport->port, modem, UARTMODIR);
1847 lpuart32_write(&sport->port, ctrl, UARTCTRL);
1848 /* restore control register */
1850 if (old && sport->lpuart_dma_rx_use) {
1851 if (!lpuart_start_rx_dma(sport))
1852 rx_dma_timer_init(sport);
1854 sport->lpuart_dma_rx_use = false;
1857 spin_unlock_irqrestore(&sport->port.lock, flags);
1860 static const char *lpuart_type(struct uart_port *port)
1862 return "FSL_LPUART";
1865 static void lpuart_release_port(struct uart_port *port)
1870 static int lpuart_request_port(struct uart_port *port)
1875 /* configure/autoconfigure the port */
1876 static void lpuart_config_port(struct uart_port *port, int flags)
1878 if (flags & UART_CONFIG_TYPE)
1879 port->type = PORT_LPUART;
1882 static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
1886 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
1888 if (port->irq != ser->irq)
1890 if (ser->io_type != UPIO_MEM)
1892 if (port->uartclk / 16 != ser->baud_base)
1894 if (port->iobase != ser->port)
1901 static const struct uart_ops lpuart_pops = {
1902 .tx_empty = lpuart_tx_empty,
1903 .set_mctrl = lpuart_set_mctrl,
1904 .get_mctrl = lpuart_get_mctrl,
1905 .stop_tx = lpuart_stop_tx,
1906 .start_tx = lpuart_start_tx,
1907 .stop_rx = lpuart_stop_rx,
1908 .break_ctl = lpuart_break_ctl,
1909 .startup = lpuart_startup,
1910 .shutdown = lpuart_shutdown,
1911 .set_termios = lpuart_set_termios,
1912 .type = lpuart_type,
1913 .request_port = lpuart_request_port,
1914 .release_port = lpuart_release_port,
1915 .config_port = lpuart_config_port,
1916 .verify_port = lpuart_verify_port,
1917 .flush_buffer = lpuart_flush_buffer,
1918 #if defined(CONFIG_CONSOLE_POLL)
1919 .poll_init = lpuart_poll_init,
1920 .poll_get_char = lpuart_poll_get_char,
1921 .poll_put_char = lpuart_poll_put_char,
1925 static const struct uart_ops lpuart32_pops = {
1926 .tx_empty = lpuart32_tx_empty,
1927 .set_mctrl = lpuart32_set_mctrl,
1928 .get_mctrl = lpuart32_get_mctrl,
1929 .stop_tx = lpuart32_stop_tx,
1930 .start_tx = lpuart32_start_tx,
1931 .stop_rx = lpuart32_stop_rx,
1932 .break_ctl = lpuart32_break_ctl,
1933 .startup = lpuart32_startup,
1934 .shutdown = lpuart32_shutdown,
1935 .set_termios = lpuart32_set_termios,
1936 .type = lpuart_type,
1937 .request_port = lpuart_request_port,
1938 .release_port = lpuart_release_port,
1939 .config_port = lpuart_config_port,
1940 .verify_port = lpuart_verify_port,
1941 .flush_buffer = lpuart_flush_buffer,
1942 #if defined(CONFIG_CONSOLE_POLL)
1943 .poll_init = lpuart32_poll_init,
1944 .poll_get_char = lpuart32_poll_get_char,
1945 .poll_put_char = lpuart32_poll_put_char,
1949 static struct lpuart_port *lpuart_ports[UART_NR];
1951 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
1952 static void lpuart_console_putchar(struct uart_port *port, int ch)
1954 while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
1957 writeb(ch, port->membase + UARTDR);
1960 static void lpuart32_console_putchar(struct uart_port *port, int ch)
1962 while (!(lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE))
1965 lpuart32_write(port, ch, UARTDATA);
1969 lpuart_console_write(struct console *co, const char *s, unsigned int count)
1971 struct lpuart_port *sport = lpuart_ports[co->index];
1972 unsigned char old_cr2, cr2;
1973 unsigned long flags;
1976 if (sport->port.sysrq || oops_in_progress)
1977 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1979 spin_lock_irqsave(&sport->port.lock, flags);
1981 /* first save CR2 and then disable interrupts */
1982 cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
1983 cr2 |= (UARTCR2_TE | UARTCR2_RE);
1984 cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
1985 writeb(cr2, sport->port.membase + UARTCR2);
1987 uart_console_write(&sport->port, s, count, lpuart_console_putchar);
1989 /* wait for transmitter finish complete and restore CR2 */
1990 while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
1993 writeb(old_cr2, sport->port.membase + UARTCR2);
1996 spin_unlock_irqrestore(&sport->port.lock, flags);
2000 lpuart32_console_write(struct console *co, const char *s, unsigned int count)
2002 struct lpuart_port *sport = lpuart_ports[co->index];
2003 unsigned long old_cr, cr;
2004 unsigned long flags;
2007 if (sport->port.sysrq || oops_in_progress)
2008 locked = spin_trylock_irqsave(&sport->port.lock, flags);
2010 spin_lock_irqsave(&sport->port.lock, flags);
2012 /* first save CR2 and then disable interrupts */
2013 cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
2014 cr |= (UARTCTRL_TE | UARTCTRL_RE);
2015 cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
2016 lpuart32_write(&sport->port, cr, UARTCTRL);
2018 uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
2020 /* wait for transmitter finish complete and restore CR2 */
2021 while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
2024 lpuart32_write(&sport->port, old_cr, UARTCTRL);
2027 spin_unlock_irqrestore(&sport->port.lock, flags);
2031 * if the port was already initialised (eg, by a boot loader),
2032 * try to determine the current setup.
2035 lpuart_console_get_options(struct lpuart_port *sport, int *baud,
2036 int *parity, int *bits)
2038 unsigned char cr, bdh, bdl, brfa;
2039 unsigned int sbr, uartclk, baud_raw;
2041 cr = readb(sport->port.membase + UARTCR2);
2042 cr &= UARTCR2_TE | UARTCR2_RE;
2046 /* ok, the port was enabled */
2048 cr = readb(sport->port.membase + UARTCR1);
2051 if (cr & UARTCR1_PE) {
2052 if (cr & UARTCR1_PT)
2063 bdh = readb(sport->port.membase + UARTBDH);
2064 bdh &= UARTBDH_SBR_MASK;
2065 bdl = readb(sport->port.membase + UARTBDL);
2069 brfa = readb(sport->port.membase + UARTCR4);
2070 brfa &= UARTCR4_BRFA_MASK;
2072 uartclk = clk_get_rate(sport->clk);
2074 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2076 baud_raw = uartclk / (16 * (sbr + brfa / 32));
2078 if (*baud != baud_raw)
2079 dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2080 "from %d to %d\n", baud_raw, *baud);
2084 lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
2085 int *parity, int *bits)
2087 unsigned long cr, bd;
2088 unsigned int sbr, uartclk, baud_raw;
2090 cr = lpuart32_read(&sport->port, UARTCTRL);
2091 cr &= UARTCTRL_TE | UARTCTRL_RE;
2095 /* ok, the port was enabled */
2097 cr = lpuart32_read(&sport->port, UARTCTRL);
2100 if (cr & UARTCTRL_PE) {
2101 if (cr & UARTCTRL_PT)
2107 if (cr & UARTCTRL_M)
2112 bd = lpuart32_read(&sport->port, UARTBAUD);
2113 bd &= UARTBAUD_SBR_MASK;
2115 uartclk = clk_get_rate(sport->clk);
2117 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2119 baud_raw = uartclk / (16 * sbr);
2121 if (*baud != baud_raw)
2122 dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
2123 "from %d to %d\n", baud_raw, *baud);
2126 static int __init lpuart_console_setup(struct console *co, char *options)
2128 struct lpuart_port *sport;
2135 * check whether an invalid uart number has been specified, and
2136 * if so, search for the first available port that does have
2139 if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
2142 sport = lpuart_ports[co->index];
2147 uart_parse_options(options, &baud, &parity, &bits, &flow);
2149 if (lpuart_is_32(sport))
2150 lpuart32_console_get_options(sport, &baud, &parity, &bits);
2152 lpuart_console_get_options(sport, &baud, &parity, &bits);
2154 if (lpuart_is_32(sport))
2155 lpuart32_setup_watermark(sport);
2157 lpuart_setup_watermark(sport);
2159 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
2162 static struct uart_driver lpuart_reg;
2163 static struct console lpuart_console = {
2165 .write = lpuart_console_write,
2166 .device = uart_console_device,
2167 .setup = lpuart_console_setup,
2168 .flags = CON_PRINTBUFFER,
2170 .data = &lpuart_reg,
2173 static struct console lpuart32_console = {
2175 .write = lpuart32_console_write,
2176 .device = uart_console_device,
2177 .setup = lpuart_console_setup,
2178 .flags = CON_PRINTBUFFER,
2180 .data = &lpuart_reg,
2183 static void lpuart_early_write(struct console *con, const char *s, unsigned n)
2185 struct earlycon_device *dev = con->data;
2187 uart_console_write(&dev->port, s, n, lpuart_console_putchar);
2190 static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
2192 struct earlycon_device *dev = con->data;
2194 uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
2197 static int __init lpuart_early_console_setup(struct earlycon_device *device,
2200 if (!device->port.membase)
2203 device->con->write = lpuart_early_write;
2207 static int __init lpuart32_early_console_setup(struct earlycon_device *device,
2210 if (!device->port.membase)
2213 device->port.iotype = UPIO_MEM32BE;
2214 device->con->write = lpuart32_early_write;
2218 static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
2221 if (!device->port.membase)
2224 device->port.iotype = UPIO_MEM32;
2225 device->port.membase += IMX_REG_OFF;
2226 device->con->write = lpuart32_early_write;
2230 OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
2231 OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
2232 OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
2233 EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
2234 EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
2236 #define LPUART_CONSOLE (&lpuart_console)
2237 #define LPUART32_CONSOLE (&lpuart32_console)
2239 #define LPUART_CONSOLE NULL
2240 #define LPUART32_CONSOLE NULL
2243 static struct uart_driver lpuart_reg = {
2244 .owner = THIS_MODULE,
2245 .driver_name = DRIVER_NAME,
2246 .dev_name = DEV_NAME,
2247 .nr = ARRAY_SIZE(lpuart_ports),
2248 .cons = LPUART_CONSOLE,
2251 static int lpuart_probe(struct platform_device *pdev)
2253 const struct of_device_id *of_id = of_match_device(lpuart_dt_ids,
2255 const struct lpuart_soc_data *sdata = of_id->data;
2256 struct device_node *np = pdev->dev.of_node;
2257 struct lpuart_port *sport;
2258 struct resource *res;
2261 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
2265 pdev->dev.coherent_dma_mask = 0;
2267 ret = of_alias_get_id(np, "serial");
2269 ret = ida_simple_get(&fsl_lpuart_ida, 0, UART_NR, GFP_KERNEL);
2271 dev_err(&pdev->dev, "port line is full, add device failed\n");
2275 if (ret >= ARRAY_SIZE(lpuart_ports)) {
2276 dev_err(&pdev->dev, "serial%d out of range\n", ret);
2279 sport->port.line = ret;
2280 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2281 sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
2282 if (IS_ERR(sport->port.membase))
2283 return PTR_ERR(sport->port.membase);
2285 sport->port.membase += sdata->reg_off;
2286 sport->port.mapbase = res->start;
2287 sport->port.dev = &pdev->dev;
2288 sport->port.type = PORT_LPUART;
2289 ret = platform_get_irq(pdev, 0);
2291 dev_err(&pdev->dev, "cannot obtain irq\n");
2294 sport->port.irq = ret;
2295 sport->port.iotype = sdata->iotype;
2296 if (lpuart_is_32(sport))
2297 sport->port.ops = &lpuart32_pops;
2299 sport->port.ops = &lpuart_pops;
2300 sport->port.flags = UPF_BOOT_AUTOCONF;
2302 sport->port.rs485_config = lpuart_config_rs485;
2304 sport->clk = devm_clk_get(&pdev->dev, "ipg");
2305 if (IS_ERR(sport->clk)) {
2306 ret = PTR_ERR(sport->clk);
2307 dev_err(&pdev->dev, "failed to get uart clk: %d\n", ret);
2311 ret = clk_prepare_enable(sport->clk);
2313 dev_err(&pdev->dev, "failed to enable uart clk: %d\n", ret);
2317 sport->port.uartclk = clk_get_rate(sport->clk);
2319 lpuart_ports[sport->port.line] = sport;
2321 platform_set_drvdata(pdev, &sport->port);
2323 if (lpuart_is_32(sport)) {
2324 lpuart_reg.cons = LPUART32_CONSOLE;
2325 ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart32_int, 0,
2326 DRIVER_NAME, sport);
2328 lpuart_reg.cons = LPUART_CONSOLE;
2329 ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart_int, 0,
2330 DRIVER_NAME, sport);
2334 goto failed_irq_request;
2336 ret = uart_add_one_port(&lpuart_reg, &sport->port);
2338 goto failed_attach_port;
2340 uart_get_rs485_mode(&pdev->dev, &sport->port.rs485);
2342 if (sport->port.rs485.flags & SER_RS485_RX_DURING_TX)
2343 dev_err(&pdev->dev, "driver doesn't support RX during TX\n");
2345 if (sport->port.rs485.delay_rts_before_send ||
2346 sport->port.rs485.delay_rts_after_send)
2347 dev_err(&pdev->dev, "driver doesn't support RTS delays\n");
2349 lpuart_config_rs485(&sport->port, &sport->port.rs485);
2351 sport->dma_tx_chan = dma_request_slave_channel(sport->port.dev, "tx");
2352 if (!sport->dma_tx_chan)
2353 dev_info(sport->port.dev, "DMA tx channel request failed, "
2354 "operating without tx DMA\n");
2356 sport->dma_rx_chan = dma_request_slave_channel(sport->port.dev, "rx");
2357 if (!sport->dma_rx_chan)
2358 dev_info(sport->port.dev, "DMA rx channel request failed, "
2359 "operating without rx DMA\n");
2365 clk_disable_unprepare(sport->clk);
2369 static int lpuart_remove(struct platform_device *pdev)
2371 struct lpuart_port *sport = platform_get_drvdata(pdev);
2373 uart_remove_one_port(&lpuart_reg, &sport->port);
2375 ida_simple_remove(&fsl_lpuart_ida, sport->port.line);
2377 clk_disable_unprepare(sport->clk);
2379 if (sport->dma_tx_chan)
2380 dma_release_channel(sport->dma_tx_chan);
2382 if (sport->dma_rx_chan)
2383 dma_release_channel(sport->dma_rx_chan);
2388 #ifdef CONFIG_PM_SLEEP
2389 static int lpuart_suspend(struct device *dev)
2391 struct lpuart_port *sport = dev_get_drvdata(dev);
2395 if (lpuart_is_32(sport)) {
2396 /* disable Rx/Tx and interrupts */
2397 temp = lpuart32_read(&sport->port, UARTCTRL);
2398 temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
2399 lpuart32_write(&sport->port, temp, UARTCTRL);
2401 /* disable Rx/Tx and interrupts */
2402 temp = readb(sport->port.membase + UARTCR2);
2403 temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
2404 writeb(temp, sport->port.membase + UARTCR2);
2407 uart_suspend_port(&lpuart_reg, &sport->port);
2409 /* uart_suspend_port() might set wakeup flag */
2410 irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2412 if (sport->lpuart_dma_rx_use) {
2414 * EDMA driver during suspend will forcefully release any
2415 * non-idle DMA channels. If port wakeup is enabled or if port
2416 * is console port or 'no_console_suspend' is set the Rx DMA
2417 * cannot resume as as expected, hence gracefully release the
2418 * Rx DMA path before suspend and start Rx DMA path on resume.
2421 del_timer_sync(&sport->lpuart_timer);
2422 lpuart_dma_rx_free(&sport->port);
2425 /* Disable Rx DMA to use UART port as wakeup source */
2426 if (lpuart_is_32(sport)) {
2427 temp = lpuart32_read(&sport->port, UARTBAUD);
2428 lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
2431 writeb(readb(sport->port.membase + UARTCR5) &
2432 ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
2436 if (sport->lpuart_dma_tx_use) {
2437 sport->dma_tx_in_progress = false;
2438 dmaengine_terminate_all(sport->dma_tx_chan);
2441 if (sport->port.suspended && !irq_wake)
2442 clk_disable_unprepare(sport->clk);
2447 static int lpuart_resume(struct device *dev)
2449 struct lpuart_port *sport = dev_get_drvdata(dev);
2450 bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
2453 if (sport->port.suspended && !irq_wake)
2454 clk_prepare_enable(sport->clk);
2456 if (lpuart_is_32(sport)) {
2457 lpuart32_setup_watermark(sport);
2458 temp = lpuart32_read(&sport->port, UARTCTRL);
2459 temp |= UARTCTRL_RE | UARTCTRL_TE | UARTCTRL_ILIE;
2460 lpuart32_write(&sport->port, temp, UARTCTRL);
2462 lpuart_setup_watermark(sport);
2463 temp = readb(sport->port.membase + UARTCR2);
2464 temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
2465 writeb(temp, sport->port.membase + UARTCR2);
2468 if (sport->lpuart_dma_rx_use) {
2470 if (!lpuart_start_rx_dma(sport))
2471 rx_dma_timer_init(sport);
2473 sport->lpuart_dma_rx_use = false;
2477 if (sport->dma_tx_chan && !lpuart_dma_tx_request(&sport->port)) {
2478 init_waitqueue_head(&sport->dma_wait);
2479 sport->lpuart_dma_tx_use = true;
2480 if (lpuart_is_32(sport)) {
2481 temp = lpuart32_read(&sport->port, UARTBAUD);
2482 lpuart32_write(&sport->port,
2483 temp | UARTBAUD_TDMAE, UARTBAUD);
2485 writeb(readb(sport->port.membase + UARTCR5) |
2486 UARTCR5_TDMAS, sport->port.membase + UARTCR5);
2489 sport->lpuart_dma_tx_use = false;
2492 if (lpuart_is_32(sport)) {
2493 if (sport->lpuart_dma_rx_use) {
2494 /* RXWATER must be 0 */
2495 temp = lpuart32_read(&sport->port, UARTWATER);
2496 temp &= ~(UARTWATER_WATER_MASK <<
2497 UARTWATER_RXWATER_OFF);
2498 lpuart32_write(&sport->port, temp, UARTWATER);
2500 temp = lpuart32_read(&sport->port, UARTCTRL);
2501 if (!sport->lpuart_dma_rx_use)
2502 temp |= UARTCTRL_RIE;
2503 if (!sport->lpuart_dma_tx_use)
2504 temp |= UARTCTRL_TIE;
2505 lpuart32_write(&sport->port, temp, UARTCTRL);
2508 uart_resume_port(&lpuart_reg, &sport->port);
2514 static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
2516 static struct platform_driver lpuart_driver = {
2517 .probe = lpuart_probe,
2518 .remove = lpuart_remove,
2520 .name = "fsl-lpuart",
2521 .of_match_table = lpuart_dt_ids,
2522 .pm = &lpuart_pm_ops,
2526 static int __init lpuart_serial_init(void)
2528 int ret = uart_register_driver(&lpuart_reg);
2533 ret = platform_driver_register(&lpuart_driver);
2535 uart_unregister_driver(&lpuart_reg);
2540 static void __exit lpuart_serial_exit(void)
2542 ida_destroy(&fsl_lpuart_ida);
2543 platform_driver_unregister(&lpuart_driver);
2544 uart_unregister_driver(&lpuart_reg);
2547 module_init(lpuart_serial_init);
2548 module_exit(lpuart_serial_exit);
2550 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
2551 MODULE_LICENSE("GPL v2");