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
236 struct uart_port port
;
238 unsigned int txfifo_size
;
239 unsigned int rxfifo_size
;
241 bool lpuart_dma_tx_use
;
242 bool lpuart_dma_rx_use
;
243 struct dma_chan
*dma_tx_chan
;
244 struct dma_chan
*dma_rx_chan
;
245 struct dma_async_tx_descriptor
*dma_tx_desc
;
246 struct dma_async_tx_descriptor
*dma_rx_desc
;
247 dma_cookie_t dma_tx_cookie
;
248 dma_cookie_t dma_rx_cookie
;
249 unsigned int dma_tx_bytes
;
250 unsigned int dma_rx_bytes
;
251 bool dma_tx_in_progress
;
252 unsigned int dma_rx_timeout
;
253 struct timer_list lpuart_timer
;
254 struct scatterlist rx_sgl
, tx_sgl
[2];
255 struct circ_buf rx_ring
;
256 int rx_dma_rng_buf_len
;
257 unsigned int dma_tx_nents
;
258 wait_queue_head_t dma_wait
;
261 struct lpuart_soc_data
{
266 static const struct lpuart_soc_data vf_data
= {
270 static const struct lpuart_soc_data ls_data
= {
271 .iotype
= UPIO_MEM32BE
,
274 static struct lpuart_soc_data imx_data
= {
275 .iotype
= UPIO_MEM32
,
276 .reg_off
= IMX_REG_OFF
,
279 static const struct of_device_id lpuart_dt_ids
[] = {
280 { .compatible
= "fsl,vf610-lpuart", .data
= &vf_data
, },
281 { .compatible
= "fsl,ls1021a-lpuart", .data
= &ls_data
, },
282 { .compatible
= "fsl,imx7ulp-lpuart", .data
= &imx_data
, },
285 MODULE_DEVICE_TABLE(of
, lpuart_dt_ids
);
287 /* Forward declare this for the dma callbacks*/
288 static void lpuart_dma_tx_complete(void *arg
);
290 static inline u32
lpuart32_read(struct uart_port
*port
, u32 off
)
292 switch (port
->iotype
) {
294 return readl(port
->membase
+ off
);
296 return ioread32be(port
->membase
+ off
);
302 static inline void lpuart32_write(struct uart_port
*port
, u32 val
,
305 switch (port
->iotype
) {
307 writel(val
, port
->membase
+ off
);
310 iowrite32be(val
, port
->membase
+ off
);
315 static void lpuart_stop_tx(struct uart_port
*port
)
319 temp
= readb(port
->membase
+ UARTCR2
);
320 temp
&= ~(UARTCR2_TIE
| UARTCR2_TCIE
);
321 writeb(temp
, port
->membase
+ UARTCR2
);
324 static void lpuart32_stop_tx(struct uart_port
*port
)
328 temp
= lpuart32_read(port
, UARTCTRL
);
329 temp
&= ~(UARTCTRL_TIE
| UARTCTRL_TCIE
);
330 lpuart32_write(port
, temp
, UARTCTRL
);
333 static void lpuart_stop_rx(struct uart_port
*port
)
337 temp
= readb(port
->membase
+ UARTCR2
);
338 writeb(temp
& ~UARTCR2_RE
, port
->membase
+ UARTCR2
);
341 static void lpuart32_stop_rx(struct uart_port
*port
)
345 temp
= lpuart32_read(port
, UARTCTRL
);
346 lpuart32_write(port
, temp
& ~UARTCTRL_RE
, UARTCTRL
);
349 static void lpuart_dma_tx(struct lpuart_port
*sport
)
351 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
352 struct scatterlist
*sgl
= sport
->tx_sgl
;
353 struct device
*dev
= sport
->port
.dev
;
356 if (sport
->dma_tx_in_progress
)
359 sport
->dma_tx_bytes
= uart_circ_chars_pending(xmit
);
361 if (xmit
->tail
< xmit
->head
|| xmit
->head
== 0) {
362 sport
->dma_tx_nents
= 1;
363 sg_init_one(sgl
, xmit
->buf
+ xmit
->tail
, sport
->dma_tx_bytes
);
365 sport
->dma_tx_nents
= 2;
366 sg_init_table(sgl
, 2);
367 sg_set_buf(sgl
, xmit
->buf
+ xmit
->tail
,
368 UART_XMIT_SIZE
- xmit
->tail
);
369 sg_set_buf(sgl
+ 1, xmit
->buf
, xmit
->head
);
372 ret
= dma_map_sg(dev
, sgl
, sport
->dma_tx_nents
, DMA_TO_DEVICE
);
374 dev_err(dev
, "DMA mapping error for TX.\n");
378 sport
->dma_tx_desc
= dmaengine_prep_slave_sg(sport
->dma_tx_chan
, sgl
,
380 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
);
381 if (!sport
->dma_tx_desc
) {
382 dma_unmap_sg(dev
, sgl
, sport
->dma_tx_nents
, DMA_TO_DEVICE
);
383 dev_err(dev
, "Cannot prepare TX slave DMA!\n");
387 sport
->dma_tx_desc
->callback
= lpuart_dma_tx_complete
;
388 sport
->dma_tx_desc
->callback_param
= sport
;
389 sport
->dma_tx_in_progress
= true;
390 sport
->dma_tx_cookie
= dmaengine_submit(sport
->dma_tx_desc
);
391 dma_async_issue_pending(sport
->dma_tx_chan
);
394 static void lpuart_dma_tx_complete(void *arg
)
396 struct lpuart_port
*sport
= arg
;
397 struct scatterlist
*sgl
= &sport
->tx_sgl
[0];
398 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
401 spin_lock_irqsave(&sport
->port
.lock
, flags
);
403 dma_unmap_sg(sport
->port
.dev
, sgl
, sport
->dma_tx_nents
, DMA_TO_DEVICE
);
405 xmit
->tail
= (xmit
->tail
+ sport
->dma_tx_bytes
) & (UART_XMIT_SIZE
- 1);
407 sport
->port
.icount
.tx
+= sport
->dma_tx_bytes
;
408 sport
->dma_tx_in_progress
= false;
409 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
411 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
412 uart_write_wakeup(&sport
->port
);
414 if (waitqueue_active(&sport
->dma_wait
)) {
415 wake_up(&sport
->dma_wait
);
419 spin_lock_irqsave(&sport
->port
.lock
, flags
);
421 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(&sport
->port
))
422 lpuart_dma_tx(sport
);
424 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
427 static int lpuart_dma_tx_request(struct uart_port
*port
)
429 struct lpuart_port
*sport
= container_of(port
,
430 struct lpuart_port
, port
);
431 struct dma_slave_config dma_tx_sconfig
= {};
434 dma_tx_sconfig
.dst_addr
= sport
->port
.mapbase
+ UARTDR
;
435 dma_tx_sconfig
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
436 dma_tx_sconfig
.dst_maxburst
= 1;
437 dma_tx_sconfig
.direction
= DMA_MEM_TO_DEV
;
438 ret
= dmaengine_slave_config(sport
->dma_tx_chan
, &dma_tx_sconfig
);
441 dev_err(sport
->port
.dev
,
442 "DMA slave config failed, err = %d\n", ret
);
449 static void lpuart_flush_buffer(struct uart_port
*port
)
451 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
453 if (sport
->lpuart_dma_tx_use
) {
454 if (sport
->dma_tx_in_progress
) {
455 dma_unmap_sg(sport
->port
.dev
, &sport
->tx_sgl
[0],
456 sport
->dma_tx_nents
, DMA_TO_DEVICE
);
457 sport
->dma_tx_in_progress
= false;
459 dmaengine_terminate_all(sport
->dma_tx_chan
);
463 #if defined(CONFIG_CONSOLE_POLL)
465 static int lpuart_poll_init(struct uart_port
*port
)
467 struct lpuart_port
*sport
= container_of(port
,
468 struct lpuart_port
, port
);
472 sport
->port
.fifosize
= 0;
474 spin_lock_irqsave(&sport
->port
.lock
, flags
);
475 /* Disable Rx & Tx */
476 writeb(0, sport
->port
.membase
+ UARTCR2
);
478 temp
= readb(sport
->port
.membase
+ UARTPFIFO
);
479 /* Enable Rx and Tx FIFO */
480 writeb(temp
| UARTPFIFO_RXFE
| UARTPFIFO_TXFE
,
481 sport
->port
.membase
+ UARTPFIFO
);
483 /* flush Tx and Rx FIFO */
484 writeb(UARTCFIFO_TXFLUSH
| UARTCFIFO_RXFLUSH
,
485 sport
->port
.membase
+ UARTCFIFO
);
487 /* explicitly clear RDRF */
488 if (readb(sport
->port
.membase
+ UARTSR1
) & UARTSR1_RDRF
) {
489 readb(sport
->port
.membase
+ UARTDR
);
490 writeb(UARTSFIFO_RXUF
, sport
->port
.membase
+ UARTSFIFO
);
493 writeb(0, sport
->port
.membase
+ UARTTWFIFO
);
494 writeb(1, sport
->port
.membase
+ UARTRWFIFO
);
496 /* Enable Rx and Tx */
497 writeb(UARTCR2_RE
| UARTCR2_TE
, sport
->port
.membase
+ UARTCR2
);
498 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
503 static void lpuart_poll_put_char(struct uart_port
*port
, unsigned char c
)
506 while (!(readb(port
->membase
+ UARTSR1
) & UARTSR1_TDRE
))
509 writeb(c
, port
->membase
+ UARTDR
);
512 static int lpuart_poll_get_char(struct uart_port
*port
)
514 if (!(readb(port
->membase
+ UARTSR1
) & UARTSR1_RDRF
))
517 return readb(port
->membase
+ UARTDR
);
520 static int lpuart32_poll_init(struct uart_port
*port
)
523 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
526 sport
->port
.fifosize
= 0;
528 spin_lock_irqsave(&sport
->port
.lock
, flags
);
530 /* Disable Rx & Tx */
531 writel(0, sport
->port
.membase
+ UARTCTRL
);
533 temp
= readl(sport
->port
.membase
+ UARTFIFO
);
535 /* Enable Rx and Tx FIFO */
536 writel(temp
| UARTFIFO_RXFE
| UARTFIFO_TXFE
,
537 sport
->port
.membase
+ UARTFIFO
);
539 /* flush Tx and Rx FIFO */
540 writel(UARTFIFO_TXFLUSH
| UARTFIFO_RXFLUSH
,
541 sport
->port
.membase
+ UARTFIFO
);
543 /* explicitly clear RDRF */
544 if (readl(sport
->port
.membase
+ UARTSTAT
) & UARTSTAT_RDRF
) {
545 readl(sport
->port
.membase
+ UARTDATA
);
546 writel(UARTFIFO_RXUF
, sport
->port
.membase
+ UARTFIFO
);
549 /* Enable Rx and Tx */
550 writel(UARTCTRL_RE
| UARTCTRL_TE
, sport
->port
.membase
+ UARTCTRL
);
551 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
556 static void lpuart32_poll_put_char(struct uart_port
*port
, unsigned char c
)
558 while (!(readl(port
->membase
+ UARTSTAT
) & UARTSTAT_TDRE
))
561 writel(c
, port
->membase
+ UARTDATA
);
564 static int lpuart32_poll_get_char(struct uart_port
*port
)
566 if (!(readl(port
->membase
+ UARTSTAT
) & UARTSTAT_RDRF
))
569 return readl(port
->membase
+ UARTDATA
);
573 static inline void lpuart_transmit_buffer(struct lpuart_port
*sport
)
575 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
577 while (!uart_circ_empty(xmit
) &&
578 (readb(sport
->port
.membase
+ UARTTCFIFO
) < sport
->txfifo_size
)) {
579 writeb(xmit
->buf
[xmit
->tail
], sport
->port
.membase
+ UARTDR
);
580 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
581 sport
->port
.icount
.tx
++;
584 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
585 uart_write_wakeup(&sport
->port
);
587 if (uart_circ_empty(xmit
))
588 lpuart_stop_tx(&sport
->port
);
591 static inline void lpuart32_transmit_buffer(struct lpuart_port
*sport
)
593 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
596 txcnt
= lpuart32_read(&sport
->port
, UARTWATER
);
597 txcnt
= txcnt
>> UARTWATER_TXCNT_OFF
;
598 txcnt
&= UARTWATER_COUNT_MASK
;
599 while (!uart_circ_empty(xmit
) && (txcnt
< sport
->txfifo_size
)) {
600 lpuart32_write(&sport
->port
, xmit
->buf
[xmit
->tail
], UARTDATA
);
601 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
602 sport
->port
.icount
.tx
++;
603 txcnt
= lpuart32_read(&sport
->port
, UARTWATER
);
604 txcnt
= txcnt
>> UARTWATER_TXCNT_OFF
;
605 txcnt
&= UARTWATER_COUNT_MASK
;
608 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
609 uart_write_wakeup(&sport
->port
);
611 if (uart_circ_empty(xmit
))
612 lpuart32_stop_tx(&sport
->port
);
615 static void lpuart_start_tx(struct uart_port
*port
)
617 struct lpuart_port
*sport
= container_of(port
,
618 struct lpuart_port
, port
);
619 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
622 temp
= readb(port
->membase
+ UARTCR2
);
623 writeb(temp
| UARTCR2_TIE
, port
->membase
+ UARTCR2
);
625 if (sport
->lpuart_dma_tx_use
) {
626 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(port
))
627 lpuart_dma_tx(sport
);
629 if (readb(port
->membase
+ UARTSR1
) & UARTSR1_TDRE
)
630 lpuart_transmit_buffer(sport
);
634 static void lpuart32_start_tx(struct uart_port
*port
)
636 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
639 temp
= lpuart32_read(port
, UARTCTRL
);
640 lpuart32_write(port
, temp
| UARTCTRL_TIE
, UARTCTRL
);
642 if (lpuart32_read(port
, UARTSTAT
) & UARTSTAT_TDRE
)
643 lpuart32_transmit_buffer(sport
);
646 /* return TIOCSER_TEMT when transmitter is not busy */
647 static unsigned int lpuart_tx_empty(struct uart_port
*port
)
649 struct lpuart_port
*sport
= container_of(port
,
650 struct lpuart_port
, port
);
651 unsigned char sr1
= readb(port
->membase
+ UARTSR1
);
652 unsigned char sfifo
= readb(port
->membase
+ UARTSFIFO
);
654 if (sport
->dma_tx_in_progress
)
657 if (sr1
& UARTSR1_TC
&& sfifo
& UARTSFIFO_TXEMPT
)
663 static unsigned int lpuart32_tx_empty(struct uart_port
*port
)
665 return (lpuart32_read(port
, UARTSTAT
) & UARTSTAT_TC
) ?
669 static bool lpuart_is_32(struct lpuart_port
*sport
)
671 return sport
->port
.iotype
== UPIO_MEM32
||
672 sport
->port
.iotype
== UPIO_MEM32BE
;
675 static irqreturn_t
lpuart_txint(int irq
, void *dev_id
)
677 struct lpuart_port
*sport
= dev_id
;
678 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
681 spin_lock_irqsave(&sport
->port
.lock
, flags
);
682 if (sport
->port
.x_char
) {
683 if (lpuart_is_32(sport
))
684 lpuart32_write(&sport
->port
, sport
->port
.x_char
, UARTDATA
);
686 writeb(sport
->port
.x_char
, sport
->port
.membase
+ UARTDR
);
690 if (uart_circ_empty(xmit
) || uart_tx_stopped(&sport
->port
)) {
691 if (lpuart_is_32(sport
))
692 lpuart32_stop_tx(&sport
->port
);
694 lpuart_stop_tx(&sport
->port
);
698 if (lpuart_is_32(sport
))
699 lpuart32_transmit_buffer(sport
);
701 lpuart_transmit_buffer(sport
);
703 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
704 uart_write_wakeup(&sport
->port
);
707 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
711 static irqreturn_t
lpuart_rxint(int irq
, void *dev_id
)
713 struct lpuart_port
*sport
= dev_id
;
714 unsigned int flg
, ignored
= 0;
715 struct tty_port
*port
= &sport
->port
.state
->port
;
717 unsigned char rx
, sr
;
719 spin_lock_irqsave(&sport
->port
.lock
, flags
);
721 while (!(readb(sport
->port
.membase
+ UARTSFIFO
) & UARTSFIFO_RXEMPT
)) {
723 sport
->port
.icount
.rx
++;
725 * to clear the FE, OR, NF, FE, PE flags,
726 * read SR1 then read DR
728 sr
= readb(sport
->port
.membase
+ UARTSR1
);
729 rx
= readb(sport
->port
.membase
+ UARTDR
);
731 if (uart_handle_sysrq_char(&sport
->port
, (unsigned char)rx
))
734 if (sr
& (UARTSR1_PE
| UARTSR1_OR
| UARTSR1_FE
)) {
736 sport
->port
.icount
.parity
++;
737 else if (sr
& UARTSR1_FE
)
738 sport
->port
.icount
.frame
++;
741 sport
->port
.icount
.overrun
++;
743 if (sr
& sport
->port
.ignore_status_mask
) {
749 sr
&= sport
->port
.read_status_mask
;
753 else if (sr
& UARTSR1_FE
)
760 sport
->port
.sysrq
= 0;
764 tty_insert_flip_char(port
, rx
, flg
);
768 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
770 tty_flip_buffer_push(port
);
774 static irqreturn_t
lpuart32_rxint(int irq
, void *dev_id
)
776 struct lpuart_port
*sport
= dev_id
;
777 unsigned int flg
, ignored
= 0;
778 struct tty_port
*port
= &sport
->port
.state
->port
;
780 unsigned long rx
, sr
;
782 spin_lock_irqsave(&sport
->port
.lock
, flags
);
784 while (!(lpuart32_read(&sport
->port
, UARTFIFO
) & UARTFIFO_RXEMPT
)) {
786 sport
->port
.icount
.rx
++;
788 * to clear the FE, OR, NF, FE, PE flags,
789 * read STAT then read DATA reg
791 sr
= lpuart32_read(&sport
->port
, UARTSTAT
);
792 rx
= lpuart32_read(&sport
->port
, UARTDATA
);
795 if (uart_handle_sysrq_char(&sport
->port
, (unsigned char)rx
))
798 if (sr
& (UARTSTAT_PE
| UARTSTAT_OR
| UARTSTAT_FE
)) {
799 if (sr
& UARTSTAT_PE
)
800 sport
->port
.icount
.parity
++;
801 else if (sr
& UARTSTAT_FE
)
802 sport
->port
.icount
.frame
++;
804 if (sr
& UARTSTAT_OR
)
805 sport
->port
.icount
.overrun
++;
807 if (sr
& sport
->port
.ignore_status_mask
) {
813 sr
&= sport
->port
.read_status_mask
;
815 if (sr
& UARTSTAT_PE
)
817 else if (sr
& UARTSTAT_FE
)
820 if (sr
& UARTSTAT_OR
)
824 sport
->port
.sysrq
= 0;
828 tty_insert_flip_char(port
, rx
, flg
);
832 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
834 tty_flip_buffer_push(port
);
838 static irqreturn_t
lpuart_int(int irq
, void *dev_id
)
840 struct lpuart_port
*sport
= dev_id
;
843 sts
= readb(sport
->port
.membase
+ UARTSR1
);
845 if (sts
& UARTSR1_RDRF
)
846 lpuart_rxint(irq
, dev_id
);
848 if (sts
& UARTSR1_TDRE
)
849 lpuart_txint(irq
, dev_id
);
854 static irqreturn_t
lpuart32_int(int irq
, void *dev_id
)
856 struct lpuart_port
*sport
= dev_id
;
857 unsigned long sts
, rxcount
;
859 sts
= lpuart32_read(&sport
->port
, UARTSTAT
);
860 rxcount
= lpuart32_read(&sport
->port
, UARTWATER
);
861 rxcount
= rxcount
>> UARTWATER_RXCNT_OFF
;
863 if (sts
& UARTSTAT_RDRF
|| rxcount
> 0)
864 lpuart32_rxint(irq
, dev_id
);
866 if ((sts
& UARTSTAT_TDRE
) &&
867 !(lpuart32_read(&sport
->port
, UARTBAUD
) & UARTBAUD_TDMAE
))
868 lpuart_txint(irq
, dev_id
);
870 lpuart32_write(&sport
->port
, sts
, UARTSTAT
);
874 static void lpuart_copy_rx_to_tty(struct lpuart_port
*sport
)
876 struct tty_port
*port
= &sport
->port
.state
->port
;
877 struct dma_tx_state state
;
878 enum dma_status dmastat
;
879 struct circ_buf
*ring
= &sport
->rx_ring
;
884 sr
= readb(sport
->port
.membase
+ UARTSR1
);
886 if (sr
& (UARTSR1_PE
| UARTSR1_FE
)) {
887 /* Read DR to clear the error flags */
888 readb(sport
->port
.membase
+ UARTDR
);
891 sport
->port
.icount
.parity
++;
892 else if (sr
& UARTSR1_FE
)
893 sport
->port
.icount
.frame
++;
896 async_tx_ack(sport
->dma_rx_desc
);
898 spin_lock_irqsave(&sport
->port
.lock
, flags
);
900 dmastat
= dmaengine_tx_status(sport
->dma_rx_chan
,
901 sport
->dma_rx_cookie
,
904 if (dmastat
== DMA_ERROR
) {
905 dev_err(sport
->port
.dev
, "Rx DMA transfer failed!\n");
906 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
910 /* CPU claims ownership of RX DMA buffer */
911 dma_sync_sg_for_cpu(sport
->port
.dev
, &sport
->rx_sgl
, 1, DMA_FROM_DEVICE
);
914 * ring->head points to the end of data already written by the DMA.
915 * ring->tail points to the beginning of data to be read by the
917 * The current transfer size should not be larger than the dma buffer
920 ring
->head
= sport
->rx_sgl
.length
- state
.residue
;
921 BUG_ON(ring
->head
> sport
->rx_sgl
.length
);
923 * At this point ring->head may point to the first byte right after the
924 * last byte of the dma buffer:
925 * 0 <= ring->head <= sport->rx_sgl.length
927 * However ring->tail must always points inside the dma buffer:
928 * 0 <= ring->tail <= sport->rx_sgl.length - 1
930 * Since we use a ring buffer, we have to handle the case
931 * where head is lower than tail. In such a case, we first read from
932 * tail to the end of the buffer then reset tail.
934 if (ring
->head
< ring
->tail
) {
935 count
= sport
->rx_sgl
.length
- ring
->tail
;
937 tty_insert_flip_string(port
, ring
->buf
+ ring
->tail
, count
);
939 sport
->port
.icount
.rx
+= count
;
942 /* Finally we read data from tail to head */
943 if (ring
->tail
< ring
->head
) {
944 count
= ring
->head
- ring
->tail
;
945 tty_insert_flip_string(port
, ring
->buf
+ ring
->tail
, count
);
946 /* Wrap ring->head if needed */
947 if (ring
->head
>= sport
->rx_sgl
.length
)
949 ring
->tail
= ring
->head
;
950 sport
->port
.icount
.rx
+= count
;
953 dma_sync_sg_for_device(sport
->port
.dev
, &sport
->rx_sgl
, 1,
956 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
958 tty_flip_buffer_push(port
);
959 mod_timer(&sport
->lpuart_timer
, jiffies
+ sport
->dma_rx_timeout
);
962 static void lpuart_dma_rx_complete(void *arg
)
964 struct lpuart_port
*sport
= arg
;
966 lpuart_copy_rx_to_tty(sport
);
969 static void lpuart_timer_func(struct timer_list
*t
)
971 struct lpuart_port
*sport
= from_timer(sport
, t
, lpuart_timer
);
973 lpuart_copy_rx_to_tty(sport
);
976 static inline int lpuart_start_rx_dma(struct lpuart_port
*sport
)
978 struct dma_slave_config dma_rx_sconfig
= {};
979 struct circ_buf
*ring
= &sport
->rx_ring
;
982 struct tty_struct
*tty
= tty_port_tty_get(&sport
->port
.state
->port
);
983 struct ktermios
*termios
= &tty
->termios
;
985 baud
= tty_get_baud_rate(tty
);
987 bits
= (termios
->c_cflag
& CSIZE
) == CS7
? 9 : 10;
988 if (termios
->c_cflag
& PARENB
)
992 * Calculate length of one DMA buffer size to keep latency below
993 * 10ms at any baud rate.
995 sport
->rx_dma_rng_buf_len
= (DMA_RX_TIMEOUT
* baud
/ bits
/ 1000) * 2;
996 sport
->rx_dma_rng_buf_len
= (1 << (fls(sport
->rx_dma_rng_buf_len
) - 1));
997 if (sport
->rx_dma_rng_buf_len
< 16)
998 sport
->rx_dma_rng_buf_len
= 16;
1000 ring
->buf
= kmalloc(sport
->rx_dma_rng_buf_len
, GFP_ATOMIC
);
1002 dev_err(sport
->port
.dev
, "Ring buf alloc failed\n");
1006 sg_init_one(&sport
->rx_sgl
, ring
->buf
, sport
->rx_dma_rng_buf_len
);
1007 sg_set_buf(&sport
->rx_sgl
, ring
->buf
, sport
->rx_dma_rng_buf_len
);
1008 nent
= dma_map_sg(sport
->port
.dev
, &sport
->rx_sgl
, 1, DMA_FROM_DEVICE
);
1011 dev_err(sport
->port
.dev
, "DMA Rx mapping error\n");
1015 dma_rx_sconfig
.src_addr
= sport
->port
.mapbase
+ UARTDR
;
1016 dma_rx_sconfig
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
1017 dma_rx_sconfig
.src_maxburst
= 1;
1018 dma_rx_sconfig
.direction
= DMA_DEV_TO_MEM
;
1019 ret
= dmaengine_slave_config(sport
->dma_rx_chan
, &dma_rx_sconfig
);
1022 dev_err(sport
->port
.dev
,
1023 "DMA Rx slave config failed, err = %d\n", ret
);
1027 sport
->dma_rx_desc
= dmaengine_prep_dma_cyclic(sport
->dma_rx_chan
,
1028 sg_dma_address(&sport
->rx_sgl
),
1029 sport
->rx_sgl
.length
,
1030 sport
->rx_sgl
.length
/ 2,
1032 DMA_PREP_INTERRUPT
);
1033 if (!sport
->dma_rx_desc
) {
1034 dev_err(sport
->port
.dev
, "Cannot prepare cyclic DMA\n");
1038 sport
->dma_rx_desc
->callback
= lpuart_dma_rx_complete
;
1039 sport
->dma_rx_desc
->callback_param
= sport
;
1040 sport
->dma_rx_cookie
= dmaengine_submit(sport
->dma_rx_desc
);
1041 dma_async_issue_pending(sport
->dma_rx_chan
);
1043 writeb(readb(sport
->port
.membase
+ UARTCR5
) | UARTCR5_RDMAS
,
1044 sport
->port
.membase
+ UARTCR5
);
1049 static void lpuart_dma_rx_free(struct uart_port
*port
)
1051 struct lpuart_port
*sport
= container_of(port
,
1052 struct lpuart_port
, port
);
1054 if (sport
->dma_rx_chan
)
1055 dmaengine_terminate_all(sport
->dma_rx_chan
);
1057 dma_unmap_sg(sport
->port
.dev
, &sport
->rx_sgl
, 1, DMA_FROM_DEVICE
);
1058 kfree(sport
->rx_ring
.buf
);
1059 sport
->rx_ring
.tail
= 0;
1060 sport
->rx_ring
.head
= 0;
1061 sport
->dma_rx_desc
= NULL
;
1062 sport
->dma_rx_cookie
= -EINVAL
;
1065 static int lpuart_config_rs485(struct uart_port
*port
,
1066 struct serial_rs485
*rs485
)
1068 struct lpuart_port
*sport
= container_of(port
,
1069 struct lpuart_port
, port
);
1071 u8 modem
= readb(sport
->port
.membase
+ UARTMODEM
) &
1072 ~(UARTMODEM_TXRTSPOL
| UARTMODEM_TXRTSE
);
1073 writeb(modem
, sport
->port
.membase
+ UARTMODEM
);
1075 /* clear unsupported configurations */
1076 rs485
->delay_rts_before_send
= 0;
1077 rs485
->delay_rts_after_send
= 0;
1078 rs485
->flags
&= ~SER_RS485_RX_DURING_TX
;
1080 if (rs485
->flags
& SER_RS485_ENABLED
) {
1081 /* Enable auto RS-485 RTS mode */
1082 modem
|= UARTMODEM_TXRTSE
;
1085 * RTS needs to be logic HIGH either during transer _or_ after
1086 * transfer, other variants are not supported by the hardware.
1089 if (!(rs485
->flags
& (SER_RS485_RTS_ON_SEND
|
1090 SER_RS485_RTS_AFTER_SEND
)))
1091 rs485
->flags
|= SER_RS485_RTS_ON_SEND
;
1093 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
&&
1094 rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)
1095 rs485
->flags
&= ~SER_RS485_RTS_AFTER_SEND
;
1098 * The hardware defaults to RTS logic HIGH while transfer.
1099 * Switch polarity in case RTS shall be logic HIGH
1101 * Note: UART is assumed to be active high.
1103 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
)
1104 modem
&= ~UARTMODEM_TXRTSPOL
;
1105 else if (rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)
1106 modem
|= UARTMODEM_TXRTSPOL
;
1109 /* Store the new configuration */
1110 sport
->port
.rs485
= *rs485
;
1112 writeb(modem
, sport
->port
.membase
+ UARTMODEM
);
1116 static unsigned int lpuart_get_mctrl(struct uart_port
*port
)
1118 unsigned int temp
= 0;
1121 reg
= readb(port
->membase
+ UARTMODEM
);
1122 if (reg
& UARTMODEM_TXCTSE
)
1125 if (reg
& UARTMODEM_RXRTSE
)
1131 static unsigned int lpuart32_get_mctrl(struct uart_port
*port
)
1133 unsigned int temp
= 0;
1136 reg
= lpuart32_read(port
, UARTMODIR
);
1137 if (reg
& UARTMODIR_TXCTSE
)
1140 if (reg
& UARTMODIR_RXRTSE
)
1146 static void lpuart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1149 struct lpuart_port
*sport
= container_of(port
,
1150 struct lpuart_port
, port
);
1152 /* Make sure RXRTSE bit is not set when RS485 is enabled */
1153 if (!(sport
->port
.rs485
.flags
& SER_RS485_ENABLED
)) {
1154 temp
= readb(sport
->port
.membase
+ UARTMODEM
) &
1155 ~(UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1157 if (mctrl
& TIOCM_RTS
)
1158 temp
|= UARTMODEM_RXRTSE
;
1160 if (mctrl
& TIOCM_CTS
)
1161 temp
|= UARTMODEM_TXCTSE
;
1163 writeb(temp
, port
->membase
+ UARTMODEM
);
1167 static void lpuart32_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1171 temp
= lpuart32_read(port
, UARTMODIR
) &
1172 ~(UARTMODIR_RXRTSE
| UARTMODIR_TXCTSE
);
1174 if (mctrl
& TIOCM_RTS
)
1175 temp
|= UARTMODIR_RXRTSE
;
1177 if (mctrl
& TIOCM_CTS
)
1178 temp
|= UARTMODIR_TXCTSE
;
1180 lpuart32_write(port
, temp
, UARTMODIR
);
1183 static void lpuart_break_ctl(struct uart_port
*port
, int break_state
)
1187 temp
= readb(port
->membase
+ UARTCR2
) & ~UARTCR2_SBK
;
1189 if (break_state
!= 0)
1190 temp
|= UARTCR2_SBK
;
1192 writeb(temp
, port
->membase
+ UARTCR2
);
1195 static void lpuart32_break_ctl(struct uart_port
*port
, int break_state
)
1199 temp
= lpuart32_read(port
, UARTCTRL
) & ~UARTCTRL_SBK
;
1201 if (break_state
!= 0)
1202 temp
|= UARTCTRL_SBK
;
1204 lpuart32_write(port
, temp
, UARTCTRL
);
1207 static void lpuart_setup_watermark(struct lpuart_port
*sport
)
1209 unsigned char val
, cr2
;
1210 unsigned char cr2_saved
;
1212 cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1214 cr2
&= ~(UARTCR2_TIE
| UARTCR2_TCIE
| UARTCR2_TE
|
1215 UARTCR2_RIE
| UARTCR2_RE
);
1216 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
1218 val
= readb(sport
->port
.membase
+ UARTPFIFO
);
1219 writeb(val
| UARTPFIFO_TXFE
| UARTPFIFO_RXFE
,
1220 sport
->port
.membase
+ UARTPFIFO
);
1222 /* flush Tx and Rx FIFO */
1223 writeb(UARTCFIFO_TXFLUSH
| UARTCFIFO_RXFLUSH
,
1224 sport
->port
.membase
+ UARTCFIFO
);
1226 /* explicitly clear RDRF */
1227 if (readb(sport
->port
.membase
+ UARTSR1
) & UARTSR1_RDRF
) {
1228 readb(sport
->port
.membase
+ UARTDR
);
1229 writeb(UARTSFIFO_RXUF
, sport
->port
.membase
+ UARTSFIFO
);
1232 writeb(0, sport
->port
.membase
+ UARTTWFIFO
);
1233 writeb(1, sport
->port
.membase
+ UARTRWFIFO
);
1236 writeb(cr2_saved
, sport
->port
.membase
+ UARTCR2
);
1239 static void lpuart32_setup_watermark(struct lpuart_port
*sport
)
1241 unsigned long val
, ctrl
;
1242 unsigned long ctrl_saved
;
1244 ctrl
= lpuart32_read(&sport
->port
, UARTCTRL
);
1246 ctrl
&= ~(UARTCTRL_TIE
| UARTCTRL_TCIE
| UARTCTRL_TE
|
1247 UARTCTRL_RIE
| UARTCTRL_RE
);
1248 lpuart32_write(&sport
->port
, ctrl
, UARTCTRL
);
1250 /* enable FIFO mode */
1251 val
= lpuart32_read(&sport
->port
, UARTFIFO
);
1252 val
|= UARTFIFO_TXFE
| UARTFIFO_RXFE
;
1253 val
|= UARTFIFO_TXFLUSH
| UARTFIFO_RXFLUSH
;
1254 lpuart32_write(&sport
->port
, val
, UARTFIFO
);
1256 /* set the watermark */
1257 val
= (0x1 << UARTWATER_RXWATER_OFF
) | (0x0 << UARTWATER_TXWATER_OFF
);
1258 lpuart32_write(&sport
->port
, val
, UARTWATER
);
1261 lpuart32_write(&sport
->port
, ctrl_saved
, UARTCTRL
);
1264 static void rx_dma_timer_init(struct lpuart_port
*sport
)
1266 timer_setup(&sport
->lpuart_timer
, lpuart_timer_func
, 0);
1267 sport
->lpuart_timer
.expires
= jiffies
+ sport
->dma_rx_timeout
;
1268 add_timer(&sport
->lpuart_timer
);
1271 static int lpuart_startup(struct uart_port
*port
)
1273 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1274 unsigned long flags
;
1277 /* determine FIFO size and enable FIFO mode */
1278 temp
= readb(sport
->port
.membase
+ UARTPFIFO
);
1280 sport
->txfifo_size
= 0x1 << (((temp
>> UARTPFIFO_TXSIZE_OFF
) &
1281 UARTPFIFO_FIFOSIZE_MASK
) + 1);
1283 sport
->port
.fifosize
= sport
->txfifo_size
;
1285 sport
->rxfifo_size
= 0x1 << (((temp
>> UARTPFIFO_RXSIZE_OFF
) &
1286 UARTPFIFO_FIFOSIZE_MASK
) + 1);
1288 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1290 lpuart_setup_watermark(sport
);
1292 temp
= readb(sport
->port
.membase
+ UARTCR2
);
1293 temp
|= (UARTCR2_RIE
| UARTCR2_TIE
| UARTCR2_RE
| UARTCR2_TE
);
1294 writeb(temp
, sport
->port
.membase
+ UARTCR2
);
1296 if (sport
->dma_rx_chan
&& !lpuart_start_rx_dma(sport
)) {
1297 /* set Rx DMA timeout */
1298 sport
->dma_rx_timeout
= msecs_to_jiffies(DMA_RX_TIMEOUT
);
1299 if (!sport
->dma_rx_timeout
)
1300 sport
->dma_rx_timeout
= 1;
1302 sport
->lpuart_dma_rx_use
= true;
1303 rx_dma_timer_init(sport
);
1305 sport
->lpuart_dma_rx_use
= false;
1308 if (sport
->dma_tx_chan
&& !lpuart_dma_tx_request(port
)) {
1309 init_waitqueue_head(&sport
->dma_wait
);
1310 sport
->lpuart_dma_tx_use
= true;
1311 temp
= readb(port
->membase
+ UARTCR5
);
1312 writeb(temp
| UARTCR5_TDMAS
, port
->membase
+ UARTCR5
);
1314 sport
->lpuart_dma_tx_use
= false;
1317 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1322 static int lpuart32_startup(struct uart_port
*port
)
1324 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1325 unsigned long flags
;
1328 /* determine FIFO size */
1329 temp
= lpuart32_read(&sport
->port
, UARTFIFO
);
1331 sport
->txfifo_size
= 0x1 << (((temp
>> UARTFIFO_TXSIZE_OFF
) &
1332 UARTFIFO_FIFOSIZE_MASK
) - 1);
1334 sport
->rxfifo_size
= 0x1 << (((temp
>> UARTFIFO_RXSIZE_OFF
) &
1335 UARTFIFO_FIFOSIZE_MASK
) - 1);
1337 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1339 lpuart32_setup_watermark(sport
);
1341 temp
= lpuart32_read(&sport
->port
, UARTCTRL
);
1342 temp
|= (UARTCTRL_RIE
| UARTCTRL_TIE
| UARTCTRL_RE
| UARTCTRL_TE
);
1343 temp
|= UARTCTRL_ILIE
;
1344 lpuart32_write(&sport
->port
, temp
, UARTCTRL
);
1346 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1350 static void lpuart_shutdown(struct uart_port
*port
)
1352 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1354 unsigned long flags
;
1356 spin_lock_irqsave(&port
->lock
, flags
);
1358 /* disable Rx/Tx and interrupts */
1359 temp
= readb(port
->membase
+ UARTCR2
);
1360 temp
&= ~(UARTCR2_TE
| UARTCR2_RE
|
1361 UARTCR2_TIE
| UARTCR2_TCIE
| UARTCR2_RIE
);
1362 writeb(temp
, port
->membase
+ UARTCR2
);
1364 spin_unlock_irqrestore(&port
->lock
, flags
);
1366 if (sport
->lpuart_dma_rx_use
) {
1367 del_timer_sync(&sport
->lpuart_timer
);
1368 lpuart_dma_rx_free(&sport
->port
);
1371 if (sport
->lpuart_dma_tx_use
) {
1372 if (wait_event_interruptible(sport
->dma_wait
,
1373 !sport
->dma_tx_in_progress
) != false) {
1374 sport
->dma_tx_in_progress
= false;
1375 dmaengine_terminate_all(sport
->dma_tx_chan
);
1378 lpuart_stop_tx(port
);
1382 static void lpuart32_shutdown(struct uart_port
*port
)
1385 unsigned long flags
;
1387 spin_lock_irqsave(&port
->lock
, flags
);
1389 /* disable Rx/Tx and interrupts */
1390 temp
= lpuart32_read(port
, UARTCTRL
);
1391 temp
&= ~(UARTCTRL_TE
| UARTCTRL_RE
|
1392 UARTCTRL_TIE
| UARTCTRL_TCIE
| UARTCTRL_RIE
);
1393 lpuart32_write(port
, temp
, UARTCTRL
);
1395 spin_unlock_irqrestore(&port
->lock
, flags
);
1399 lpuart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1400 struct ktermios
*old
)
1402 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1403 unsigned long flags
;
1404 unsigned char cr1
, old_cr1
, old_cr2
, cr3
, cr4
, bdh
, modem
;
1406 unsigned int old_csize
= old
? old
->c_cflag
& CSIZE
: CS8
;
1407 unsigned int sbr
, brfa
;
1409 cr1
= old_cr1
= readb(sport
->port
.membase
+ UARTCR1
);
1410 old_cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1411 cr3
= readb(sport
->port
.membase
+ UARTCR3
);
1412 cr4
= readb(sport
->port
.membase
+ UARTCR4
);
1413 bdh
= readb(sport
->port
.membase
+ UARTBDH
);
1414 modem
= readb(sport
->port
.membase
+ UARTMODEM
);
1416 * only support CS8 and CS7, and for CS7 must enable PE.
1423 while ((termios
->c_cflag
& CSIZE
) != CS8
&&
1424 (termios
->c_cflag
& CSIZE
) != CS7
) {
1425 termios
->c_cflag
&= ~CSIZE
;
1426 termios
->c_cflag
|= old_csize
;
1430 if ((termios
->c_cflag
& CSIZE
) == CS8
||
1431 (termios
->c_cflag
& CSIZE
) == CS7
)
1432 cr1
= old_cr1
& ~UARTCR1_M
;
1434 if (termios
->c_cflag
& CMSPAR
) {
1435 if ((termios
->c_cflag
& CSIZE
) != CS8
) {
1436 termios
->c_cflag
&= ~CSIZE
;
1437 termios
->c_cflag
|= CS8
;
1443 * When auto RS-485 RTS mode is enabled,
1444 * hardware flow control need to be disabled.
1446 if (sport
->port
.rs485
.flags
& SER_RS485_ENABLED
)
1447 termios
->c_cflag
&= ~CRTSCTS
;
1449 if (termios
->c_cflag
& CRTSCTS
) {
1450 modem
|= (UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1452 termios
->c_cflag
&= ~CRTSCTS
;
1453 modem
&= ~(UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1456 if (termios
->c_cflag
& CSTOPB
)
1457 termios
->c_cflag
&= ~CSTOPB
;
1459 /* parity must be enabled when CS7 to match 8-bits format */
1460 if ((termios
->c_cflag
& CSIZE
) == CS7
)
1461 termios
->c_cflag
|= PARENB
;
1463 if ((termios
->c_cflag
& PARENB
)) {
1464 if (termios
->c_cflag
& CMSPAR
) {
1466 if (termios
->c_cflag
& PARODD
)
1472 if ((termios
->c_cflag
& CSIZE
) == CS8
)
1474 if (termios
->c_cflag
& PARODD
)
1481 /* ask the core to calculate the divisor */
1482 baud
= uart_get_baud_rate(port
, termios
, old
, 50, port
->uartclk
/ 16);
1485 * Need to update the Ring buffer length according to the selected
1486 * baud rate and restart Rx DMA path.
1488 * Since timer function acqures sport->port.lock, need to stop before
1489 * acquring same lock because otherwise del_timer_sync() can deadlock.
1491 if (old
&& sport
->lpuart_dma_rx_use
) {
1492 del_timer_sync(&sport
->lpuart_timer
);
1493 lpuart_dma_rx_free(&sport
->port
);
1496 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1498 sport
->port
.read_status_mask
= 0;
1499 if (termios
->c_iflag
& INPCK
)
1500 sport
->port
.read_status_mask
|= (UARTSR1_FE
| UARTSR1_PE
);
1501 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1502 sport
->port
.read_status_mask
|= UARTSR1_FE
;
1504 /* characters to ignore */
1505 sport
->port
.ignore_status_mask
= 0;
1506 if (termios
->c_iflag
& IGNPAR
)
1507 sport
->port
.ignore_status_mask
|= UARTSR1_PE
;
1508 if (termios
->c_iflag
& IGNBRK
) {
1509 sport
->port
.ignore_status_mask
|= UARTSR1_FE
;
1511 * if we're ignoring parity and break indicators,
1512 * ignore overruns too (for real raw support).
1514 if (termios
->c_iflag
& IGNPAR
)
1515 sport
->port
.ignore_status_mask
|= UARTSR1_OR
;
1518 /* update the per-port timeout */
1519 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1521 /* wait transmit engin complete */
1522 while (!(readb(sport
->port
.membase
+ UARTSR1
) & UARTSR1_TC
))
1525 /* disable transmit and receive */
1526 writeb(old_cr2
& ~(UARTCR2_TE
| UARTCR2_RE
),
1527 sport
->port
.membase
+ UARTCR2
);
1529 sbr
= sport
->port
.uartclk
/ (16 * baud
);
1530 brfa
= ((sport
->port
.uartclk
- (16 * sbr
* baud
)) * 2) / baud
;
1531 bdh
&= ~UARTBDH_SBR_MASK
;
1532 bdh
|= (sbr
>> 8) & 0x1F;
1533 cr4
&= ~UARTCR4_BRFA_MASK
;
1534 brfa
&= UARTCR4_BRFA_MASK
;
1535 writeb(cr4
| brfa
, sport
->port
.membase
+ UARTCR4
);
1536 writeb(bdh
, sport
->port
.membase
+ UARTBDH
);
1537 writeb(sbr
& 0xFF, sport
->port
.membase
+ UARTBDL
);
1538 writeb(cr3
, sport
->port
.membase
+ UARTCR3
);
1539 writeb(cr1
, sport
->port
.membase
+ UARTCR1
);
1540 writeb(modem
, sport
->port
.membase
+ UARTMODEM
);
1542 /* restore control register */
1543 writeb(old_cr2
, sport
->port
.membase
+ UARTCR2
);
1545 if (old
&& sport
->lpuart_dma_rx_use
) {
1546 if (!lpuart_start_rx_dma(sport
))
1547 rx_dma_timer_init(sport
);
1549 sport
->lpuart_dma_rx_use
= false;
1552 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1556 lpuart32_serial_setbrg(struct lpuart_port
*sport
, unsigned int baudrate
)
1558 u32 sbr
, osr
, baud_diff
, tmp_osr
, tmp_sbr
, tmp_diff
, tmp
;
1559 u32 clk
= sport
->port
.uartclk
;
1562 * The idea is to use the best OSR (over-sampling rate) possible.
1563 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
1564 * Loop to find the best OSR value possible, one that generates minimum
1565 * baud_diff iterate through the rest of the supported values of OSR.
1567 * Calculation Formula:
1568 * Baud Rate = baud clock / ((OSR+1) × SBR)
1570 baud_diff
= baudrate
;
1574 for (tmp_osr
= 4; tmp_osr
<= 32; tmp_osr
++) {
1575 /* calculate the temporary sbr value */
1576 tmp_sbr
= (clk
/ (baudrate
* tmp_osr
));
1581 * calculate the baud rate difference based on the temporary
1582 * osr and sbr values
1584 tmp_diff
= clk
/ (tmp_osr
* tmp_sbr
) - baudrate
;
1586 /* select best values between sbr and sbr+1 */
1587 tmp
= clk
/ (tmp_osr
* (tmp_sbr
+ 1));
1588 if (tmp_diff
> (baudrate
- tmp
)) {
1589 tmp_diff
= baudrate
- tmp
;
1593 if (tmp_diff
<= baud_diff
) {
1594 baud_diff
= tmp_diff
;
1603 /* handle buadrate outside acceptable rate */
1604 if (baud_diff
> ((baudrate
/ 100) * 3))
1605 dev_warn(sport
->port
.dev
,
1606 "unacceptable baud rate difference of more than 3%%\n");
1608 tmp
= lpuart32_read(&sport
->port
, UARTBAUD
);
1610 if ((osr
> 3) && (osr
< 8))
1611 tmp
|= UARTBAUD_BOTHEDGE
;
1613 tmp
&= ~(UARTBAUD_OSR_MASK
<< UARTBAUD_OSR_SHIFT
);
1614 tmp
|= (((osr
-1) & UARTBAUD_OSR_MASK
) << UARTBAUD_OSR_SHIFT
);
1616 tmp
&= ~UARTBAUD_SBR_MASK
;
1617 tmp
|= sbr
& UARTBAUD_SBR_MASK
;
1619 tmp
&= ~(UARTBAUD_TDMAE
| UARTBAUD_RDMAE
);
1621 lpuart32_write(&sport
->port
, tmp
, UARTBAUD
);
1625 lpuart32_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1626 struct ktermios
*old
)
1628 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1629 unsigned long flags
;
1630 unsigned long ctrl
, old_ctrl
, modem
;
1632 unsigned int old_csize
= old
? old
->c_cflag
& CSIZE
: CS8
;
1634 ctrl
= old_ctrl
= lpuart32_read(&sport
->port
, UARTCTRL
);
1635 modem
= lpuart32_read(&sport
->port
, UARTMODIR
);
1637 * only support CS8 and CS7, and for CS7 must enable PE.
1644 while ((termios
->c_cflag
& CSIZE
) != CS8
&&
1645 (termios
->c_cflag
& CSIZE
) != CS7
) {
1646 termios
->c_cflag
&= ~CSIZE
;
1647 termios
->c_cflag
|= old_csize
;
1651 if ((termios
->c_cflag
& CSIZE
) == CS8
||
1652 (termios
->c_cflag
& CSIZE
) == CS7
)
1653 ctrl
= old_ctrl
& ~UARTCTRL_M
;
1655 if (termios
->c_cflag
& CMSPAR
) {
1656 if ((termios
->c_cflag
& CSIZE
) != CS8
) {
1657 termios
->c_cflag
&= ~CSIZE
;
1658 termios
->c_cflag
|= CS8
;
1663 if (termios
->c_cflag
& CRTSCTS
) {
1664 modem
|= (UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1666 termios
->c_cflag
&= ~CRTSCTS
;
1667 modem
&= ~(UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1670 if (termios
->c_cflag
& CSTOPB
)
1671 termios
->c_cflag
&= ~CSTOPB
;
1673 /* parity must be enabled when CS7 to match 8-bits format */
1674 if ((termios
->c_cflag
& CSIZE
) == CS7
)
1675 termios
->c_cflag
|= PARENB
;
1677 if ((termios
->c_cflag
& PARENB
)) {
1678 if (termios
->c_cflag
& CMSPAR
) {
1679 ctrl
&= ~UARTCTRL_PE
;
1683 if ((termios
->c_cflag
& CSIZE
) == CS8
)
1685 if (termios
->c_cflag
& PARODD
)
1686 ctrl
|= UARTCTRL_PT
;
1688 ctrl
&= ~UARTCTRL_PT
;
1692 /* ask the core to calculate the divisor */
1693 baud
= uart_get_baud_rate(port
, termios
, old
, 50, port
->uartclk
/ 16);
1695 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1697 sport
->port
.read_status_mask
= 0;
1698 if (termios
->c_iflag
& INPCK
)
1699 sport
->port
.read_status_mask
|= (UARTSTAT_FE
| UARTSTAT_PE
);
1700 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1701 sport
->port
.read_status_mask
|= UARTSTAT_FE
;
1703 /* characters to ignore */
1704 sport
->port
.ignore_status_mask
= 0;
1705 if (termios
->c_iflag
& IGNPAR
)
1706 sport
->port
.ignore_status_mask
|= UARTSTAT_PE
;
1707 if (termios
->c_iflag
& IGNBRK
) {
1708 sport
->port
.ignore_status_mask
|= UARTSTAT_FE
;
1710 * if we're ignoring parity and break indicators,
1711 * ignore overruns too (for real raw support).
1713 if (termios
->c_iflag
& IGNPAR
)
1714 sport
->port
.ignore_status_mask
|= UARTSTAT_OR
;
1717 /* update the per-port timeout */
1718 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1720 /* wait transmit engin complete */
1721 while (!(lpuart32_read(&sport
->port
, UARTSTAT
) & UARTSTAT_TC
))
1724 /* disable transmit and receive */
1725 lpuart32_write(&sport
->port
, old_ctrl
& ~(UARTCTRL_TE
| UARTCTRL_RE
),
1728 lpuart32_serial_setbrg(sport
, baud
);
1729 lpuart32_write(&sport
->port
, modem
, UARTMODIR
);
1730 lpuart32_write(&sport
->port
, ctrl
, UARTCTRL
);
1731 /* restore control register */
1733 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1736 static const char *lpuart_type(struct uart_port
*port
)
1738 return "FSL_LPUART";
1741 static void lpuart_release_port(struct uart_port
*port
)
1746 static int lpuart_request_port(struct uart_port
*port
)
1751 /* configure/autoconfigure the port */
1752 static void lpuart_config_port(struct uart_port
*port
, int flags
)
1754 if (flags
& UART_CONFIG_TYPE
)
1755 port
->type
= PORT_LPUART
;
1758 static int lpuart_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1762 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_LPUART
)
1764 if (port
->irq
!= ser
->irq
)
1766 if (ser
->io_type
!= UPIO_MEM
)
1768 if (port
->uartclk
/ 16 != ser
->baud_base
)
1770 if (port
->iobase
!= ser
->port
)
1777 static const struct uart_ops lpuart_pops
= {
1778 .tx_empty
= lpuart_tx_empty
,
1779 .set_mctrl
= lpuart_set_mctrl
,
1780 .get_mctrl
= lpuart_get_mctrl
,
1781 .stop_tx
= lpuart_stop_tx
,
1782 .start_tx
= lpuart_start_tx
,
1783 .stop_rx
= lpuart_stop_rx
,
1784 .break_ctl
= lpuart_break_ctl
,
1785 .startup
= lpuart_startup
,
1786 .shutdown
= lpuart_shutdown
,
1787 .set_termios
= lpuart_set_termios
,
1788 .type
= lpuart_type
,
1789 .request_port
= lpuart_request_port
,
1790 .release_port
= lpuart_release_port
,
1791 .config_port
= lpuart_config_port
,
1792 .verify_port
= lpuart_verify_port
,
1793 .flush_buffer
= lpuart_flush_buffer
,
1794 #if defined(CONFIG_CONSOLE_POLL)
1795 .poll_init
= lpuart_poll_init
,
1796 .poll_get_char
= lpuart_poll_get_char
,
1797 .poll_put_char
= lpuart_poll_put_char
,
1801 static const struct uart_ops lpuart32_pops
= {
1802 .tx_empty
= lpuart32_tx_empty
,
1803 .set_mctrl
= lpuart32_set_mctrl
,
1804 .get_mctrl
= lpuart32_get_mctrl
,
1805 .stop_tx
= lpuart32_stop_tx
,
1806 .start_tx
= lpuart32_start_tx
,
1807 .stop_rx
= lpuart32_stop_rx
,
1808 .break_ctl
= lpuart32_break_ctl
,
1809 .startup
= lpuart32_startup
,
1810 .shutdown
= lpuart32_shutdown
,
1811 .set_termios
= lpuart32_set_termios
,
1812 .type
= lpuart_type
,
1813 .request_port
= lpuart_request_port
,
1814 .release_port
= lpuart_release_port
,
1815 .config_port
= lpuart_config_port
,
1816 .verify_port
= lpuart_verify_port
,
1817 .flush_buffer
= lpuart_flush_buffer
,
1818 #if defined(CONFIG_CONSOLE_POLL)
1819 .poll_init
= lpuart32_poll_init
,
1820 .poll_get_char
= lpuart32_poll_get_char
,
1821 .poll_put_char
= lpuart32_poll_put_char
,
1825 static struct lpuart_port
*lpuart_ports
[UART_NR
];
1827 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
1828 static void lpuart_console_putchar(struct uart_port
*port
, int ch
)
1830 while (!(readb(port
->membase
+ UARTSR1
) & UARTSR1_TDRE
))
1833 writeb(ch
, port
->membase
+ UARTDR
);
1836 static void lpuart32_console_putchar(struct uart_port
*port
, int ch
)
1838 while (!(lpuart32_read(port
, UARTSTAT
) & UARTSTAT_TDRE
))
1841 lpuart32_write(port
, ch
, UARTDATA
);
1845 lpuart_console_write(struct console
*co
, const char *s
, unsigned int count
)
1847 struct lpuart_port
*sport
= lpuart_ports
[co
->index
];
1848 unsigned char old_cr2
, cr2
;
1849 unsigned long flags
;
1852 if (sport
->port
.sysrq
|| oops_in_progress
)
1853 locked
= spin_trylock_irqsave(&sport
->port
.lock
, flags
);
1855 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1857 /* first save CR2 and then disable interrupts */
1858 cr2
= old_cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1859 cr2
|= (UARTCR2_TE
| UARTCR2_RE
);
1860 cr2
&= ~(UARTCR2_TIE
| UARTCR2_TCIE
| UARTCR2_RIE
);
1861 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
1863 uart_console_write(&sport
->port
, s
, count
, lpuart_console_putchar
);
1865 /* wait for transmitter finish complete and restore CR2 */
1866 while (!(readb(sport
->port
.membase
+ UARTSR1
) & UARTSR1_TC
))
1869 writeb(old_cr2
, sport
->port
.membase
+ UARTCR2
);
1872 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1876 lpuart32_console_write(struct console
*co
, const char *s
, unsigned int count
)
1878 struct lpuart_port
*sport
= lpuart_ports
[co
->index
];
1879 unsigned long old_cr
, cr
;
1880 unsigned long flags
;
1883 if (sport
->port
.sysrq
|| oops_in_progress
)
1884 locked
= spin_trylock_irqsave(&sport
->port
.lock
, flags
);
1886 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1888 /* first save CR2 and then disable interrupts */
1889 cr
= old_cr
= lpuart32_read(&sport
->port
, UARTCTRL
);
1890 cr
|= (UARTCTRL_TE
| UARTCTRL_RE
);
1891 cr
&= ~(UARTCTRL_TIE
| UARTCTRL_TCIE
| UARTCTRL_RIE
);
1892 lpuart32_write(&sport
->port
, cr
, UARTCTRL
);
1894 uart_console_write(&sport
->port
, s
, count
, lpuart32_console_putchar
);
1896 /* wait for transmitter finish complete and restore CR2 */
1897 while (!(lpuart32_read(&sport
->port
, UARTSTAT
) & UARTSTAT_TC
))
1900 lpuart32_write(&sport
->port
, old_cr
, UARTCTRL
);
1903 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1907 * if the port was already initialised (eg, by a boot loader),
1908 * try to determine the current setup.
1911 lpuart_console_get_options(struct lpuart_port
*sport
, int *baud
,
1912 int *parity
, int *bits
)
1914 unsigned char cr
, bdh
, bdl
, brfa
;
1915 unsigned int sbr
, uartclk
, baud_raw
;
1917 cr
= readb(sport
->port
.membase
+ UARTCR2
);
1918 cr
&= UARTCR2_TE
| UARTCR2_RE
;
1922 /* ok, the port was enabled */
1924 cr
= readb(sport
->port
.membase
+ UARTCR1
);
1927 if (cr
& UARTCR1_PE
) {
1928 if (cr
& UARTCR1_PT
)
1939 bdh
= readb(sport
->port
.membase
+ UARTBDH
);
1940 bdh
&= UARTBDH_SBR_MASK
;
1941 bdl
= readb(sport
->port
.membase
+ UARTBDL
);
1945 brfa
= readb(sport
->port
.membase
+ UARTCR4
);
1946 brfa
&= UARTCR4_BRFA_MASK
;
1948 uartclk
= clk_get_rate(sport
->clk
);
1950 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
1952 baud_raw
= uartclk
/ (16 * (sbr
+ brfa
/ 32));
1954 if (*baud
!= baud_raw
)
1955 printk(KERN_INFO
"Serial: Console lpuart rounded baud rate"
1956 "from %d to %d\n", baud_raw
, *baud
);
1960 lpuart32_console_get_options(struct lpuart_port
*sport
, int *baud
,
1961 int *parity
, int *bits
)
1963 unsigned long cr
, bd
;
1964 unsigned int sbr
, uartclk
, baud_raw
;
1966 cr
= lpuart32_read(&sport
->port
, UARTCTRL
);
1967 cr
&= UARTCTRL_TE
| UARTCTRL_RE
;
1971 /* ok, the port was enabled */
1973 cr
= lpuart32_read(&sport
->port
, UARTCTRL
);
1976 if (cr
& UARTCTRL_PE
) {
1977 if (cr
& UARTCTRL_PT
)
1983 if (cr
& UARTCTRL_M
)
1988 bd
= lpuart32_read(&sport
->port
, UARTBAUD
);
1989 bd
&= UARTBAUD_SBR_MASK
;
1991 uartclk
= clk_get_rate(sport
->clk
);
1993 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
1995 baud_raw
= uartclk
/ (16 * sbr
);
1997 if (*baud
!= baud_raw
)
1998 printk(KERN_INFO
"Serial: Console lpuart rounded baud rate"
1999 "from %d to %d\n", baud_raw
, *baud
);
2002 static int __init
lpuart_console_setup(struct console
*co
, char *options
)
2004 struct lpuart_port
*sport
;
2011 * check whether an invalid uart number has been specified, and
2012 * if so, search for the first available port that does have
2015 if (co
->index
== -1 || co
->index
>= ARRAY_SIZE(lpuart_ports
))
2018 sport
= lpuart_ports
[co
->index
];
2023 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2025 if (lpuart_is_32(sport
))
2026 lpuart32_console_get_options(sport
, &baud
, &parity
, &bits
);
2028 lpuart_console_get_options(sport
, &baud
, &parity
, &bits
);
2030 if (lpuart_is_32(sport
))
2031 lpuart32_setup_watermark(sport
);
2033 lpuart_setup_watermark(sport
);
2035 return uart_set_options(&sport
->port
, co
, baud
, parity
, bits
, flow
);
2038 static struct uart_driver lpuart_reg
;
2039 static struct console lpuart_console
= {
2041 .write
= lpuart_console_write
,
2042 .device
= uart_console_device
,
2043 .setup
= lpuart_console_setup
,
2044 .flags
= CON_PRINTBUFFER
,
2046 .data
= &lpuart_reg
,
2049 static struct console lpuart32_console
= {
2051 .write
= lpuart32_console_write
,
2052 .device
= uart_console_device
,
2053 .setup
= lpuart_console_setup
,
2054 .flags
= CON_PRINTBUFFER
,
2056 .data
= &lpuart_reg
,
2059 static void lpuart_early_write(struct console
*con
, const char *s
, unsigned n
)
2061 struct earlycon_device
*dev
= con
->data
;
2063 uart_console_write(&dev
->port
, s
, n
, lpuart_console_putchar
);
2066 static void lpuart32_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
, lpuart32_console_putchar
);
2073 static int __init
lpuart_early_console_setup(struct earlycon_device
*device
,
2076 if (!device
->port
.membase
)
2079 device
->con
->write
= lpuart_early_write
;
2083 static int __init
lpuart32_early_console_setup(struct earlycon_device
*device
,
2086 if (!device
->port
.membase
)
2089 device
->port
.iotype
= UPIO_MEM32BE
;
2090 device
->con
->write
= lpuart32_early_write
;
2094 static int __init
lpuart32_imx_early_console_setup(struct earlycon_device
*device
,
2097 if (!device
->port
.membase
)
2100 device
->port
.iotype
= UPIO_MEM32
;
2101 device
->port
.membase
+= IMX_REG_OFF
;
2102 device
->con
->write
= lpuart32_early_write
;
2106 OF_EARLYCON_DECLARE(lpuart
, "fsl,vf610-lpuart", lpuart_early_console_setup
);
2107 OF_EARLYCON_DECLARE(lpuart32
, "fsl,ls1021a-lpuart", lpuart32_early_console_setup
);
2108 OF_EARLYCON_DECLARE(lpuart32
, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup
);
2109 EARLYCON_DECLARE(lpuart
, lpuart_early_console_setup
);
2110 EARLYCON_DECLARE(lpuart32
, lpuart32_early_console_setup
);
2112 #define LPUART_CONSOLE (&lpuart_console)
2113 #define LPUART32_CONSOLE (&lpuart32_console)
2115 #define LPUART_CONSOLE NULL
2116 #define LPUART32_CONSOLE NULL
2119 static struct uart_driver lpuart_reg
= {
2120 .owner
= THIS_MODULE
,
2121 .driver_name
= DRIVER_NAME
,
2122 .dev_name
= DEV_NAME
,
2123 .nr
= ARRAY_SIZE(lpuart_ports
),
2124 .cons
= LPUART_CONSOLE
,
2127 static int lpuart_probe(struct platform_device
*pdev
)
2129 const struct of_device_id
*of_id
= of_match_device(lpuart_dt_ids
,
2131 const struct lpuart_soc_data
*sdata
= of_id
->data
;
2132 struct device_node
*np
= pdev
->dev
.of_node
;
2133 struct lpuart_port
*sport
;
2134 struct resource
*res
;
2137 sport
= devm_kzalloc(&pdev
->dev
, sizeof(*sport
), GFP_KERNEL
);
2141 pdev
->dev
.coherent_dma_mask
= 0;
2143 ret
= of_alias_get_id(np
, "serial");
2145 dev_err(&pdev
->dev
, "failed to get alias id, errno %d\n", ret
);
2148 sport
->port
.line
= ret
;
2149 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2150 sport
->port
.membase
= devm_ioremap_resource(&pdev
->dev
, res
);
2151 if (IS_ERR(sport
->port
.membase
))
2152 return PTR_ERR(sport
->port
.membase
);
2154 sport
->port
.membase
+= sdata
->reg_off
;
2155 sport
->port
.mapbase
= res
->start
;
2156 sport
->port
.dev
= &pdev
->dev
;
2157 sport
->port
.type
= PORT_LPUART
;
2158 ret
= platform_get_irq(pdev
, 0);
2160 dev_err(&pdev
->dev
, "cannot obtain irq\n");
2163 sport
->port
.irq
= ret
;
2164 sport
->port
.iotype
= sdata
->iotype
;
2165 if (lpuart_is_32(sport
))
2166 sport
->port
.ops
= &lpuart32_pops
;
2168 sport
->port
.ops
= &lpuart_pops
;
2169 sport
->port
.flags
= UPF_BOOT_AUTOCONF
;
2171 sport
->port
.rs485_config
= lpuart_config_rs485
;
2173 sport
->clk
= devm_clk_get(&pdev
->dev
, "ipg");
2174 if (IS_ERR(sport
->clk
)) {
2175 ret
= PTR_ERR(sport
->clk
);
2176 dev_err(&pdev
->dev
, "failed to get uart clk: %d\n", ret
);
2180 ret
= clk_prepare_enable(sport
->clk
);
2182 dev_err(&pdev
->dev
, "failed to enable uart clk: %d\n", ret
);
2186 sport
->port
.uartclk
= clk_get_rate(sport
->clk
);
2188 lpuart_ports
[sport
->port
.line
] = sport
;
2190 platform_set_drvdata(pdev
, &sport
->port
);
2192 if (lpuart_is_32(sport
)) {
2193 lpuart_reg
.cons
= LPUART32_CONSOLE
;
2194 ret
= devm_request_irq(&pdev
->dev
, sport
->port
.irq
, lpuart32_int
, 0,
2195 DRIVER_NAME
, sport
);
2197 lpuart_reg
.cons
= LPUART_CONSOLE
;
2198 ret
= devm_request_irq(&pdev
->dev
, sport
->port
.irq
, lpuart_int
, 0,
2199 DRIVER_NAME
, sport
);
2203 goto failed_irq_request
;
2205 ret
= uart_add_one_port(&lpuart_reg
, &sport
->port
);
2207 goto failed_attach_port
;
2209 uart_get_rs485_mode(&pdev
->dev
, &sport
->port
.rs485
);
2211 if (sport
->port
.rs485
.flags
& SER_RS485_RX_DURING_TX
)
2212 dev_err(&pdev
->dev
, "driver doesn't support RX during TX\n");
2214 if (sport
->port
.rs485
.delay_rts_before_send
||
2215 sport
->port
.rs485
.delay_rts_after_send
)
2216 dev_err(&pdev
->dev
, "driver doesn't support RTS delays\n");
2218 lpuart_config_rs485(&sport
->port
, &sport
->port
.rs485
);
2220 sport
->dma_tx_chan
= dma_request_slave_channel(sport
->port
.dev
, "tx");
2221 if (!sport
->dma_tx_chan
)
2222 dev_info(sport
->port
.dev
, "DMA tx channel request failed, "
2223 "operating without tx DMA\n");
2225 sport
->dma_rx_chan
= dma_request_slave_channel(sport
->port
.dev
, "rx");
2226 if (!sport
->dma_rx_chan
)
2227 dev_info(sport
->port
.dev
, "DMA rx channel request failed, "
2228 "operating without rx DMA\n");
2234 clk_disable_unprepare(sport
->clk
);
2238 static int lpuart_remove(struct platform_device
*pdev
)
2240 struct lpuart_port
*sport
= platform_get_drvdata(pdev
);
2242 uart_remove_one_port(&lpuart_reg
, &sport
->port
);
2244 clk_disable_unprepare(sport
->clk
);
2246 if (sport
->dma_tx_chan
)
2247 dma_release_channel(sport
->dma_tx_chan
);
2249 if (sport
->dma_rx_chan
)
2250 dma_release_channel(sport
->dma_rx_chan
);
2255 #ifdef CONFIG_PM_SLEEP
2256 static int lpuart_suspend(struct device
*dev
)
2258 struct lpuart_port
*sport
= dev_get_drvdata(dev
);
2262 if (lpuart_is_32(sport
)) {
2263 /* disable Rx/Tx and interrupts */
2264 temp
= lpuart32_read(&sport
->port
, UARTCTRL
);
2265 temp
&= ~(UARTCTRL_TE
| UARTCTRL_TIE
| UARTCTRL_TCIE
);
2266 lpuart32_write(&sport
->port
, temp
, UARTCTRL
);
2268 /* disable Rx/Tx and interrupts */
2269 temp
= readb(sport
->port
.membase
+ UARTCR2
);
2270 temp
&= ~(UARTCR2_TE
| UARTCR2_TIE
| UARTCR2_TCIE
);
2271 writeb(temp
, sport
->port
.membase
+ UARTCR2
);
2274 uart_suspend_port(&lpuart_reg
, &sport
->port
);
2276 /* uart_suspend_port() might set wakeup flag */
2277 irq_wake
= irqd_is_wakeup_set(irq_get_irq_data(sport
->port
.irq
));
2279 if (sport
->lpuart_dma_rx_use
) {
2281 * EDMA driver during suspend will forcefully release any
2282 * non-idle DMA channels. If port wakeup is enabled or if port
2283 * is console port or 'no_console_suspend' is set the Rx DMA
2284 * cannot resume as as expected, hence gracefully release the
2285 * Rx DMA path before suspend and start Rx DMA path on resume.
2288 del_timer_sync(&sport
->lpuart_timer
);
2289 lpuart_dma_rx_free(&sport
->port
);
2292 /* Disable Rx DMA to use UART port as wakeup source */
2293 writeb(readb(sport
->port
.membase
+ UARTCR5
) & ~UARTCR5_RDMAS
,
2294 sport
->port
.membase
+ UARTCR5
);
2297 if (sport
->lpuart_dma_tx_use
) {
2298 sport
->dma_tx_in_progress
= false;
2299 dmaengine_terminate_all(sport
->dma_tx_chan
);
2302 if (sport
->port
.suspended
&& !irq_wake
)
2303 clk_disable_unprepare(sport
->clk
);
2308 static int lpuart_resume(struct device
*dev
)
2310 struct lpuart_port
*sport
= dev_get_drvdata(dev
);
2311 bool irq_wake
= irqd_is_wakeup_set(irq_get_irq_data(sport
->port
.irq
));
2314 if (sport
->port
.suspended
&& !irq_wake
)
2315 clk_prepare_enable(sport
->clk
);
2317 if (lpuart_is_32(sport
)) {
2318 lpuart32_setup_watermark(sport
);
2319 temp
= lpuart32_read(&sport
->port
, UARTCTRL
);
2320 temp
|= (UARTCTRL_RIE
| UARTCTRL_TIE
| UARTCTRL_RE
|
2321 UARTCTRL_TE
| UARTCTRL_ILIE
);
2322 lpuart32_write(&sport
->port
, temp
, UARTCTRL
);
2324 lpuart_setup_watermark(sport
);
2325 temp
= readb(sport
->port
.membase
+ UARTCR2
);
2326 temp
|= (UARTCR2_RIE
| UARTCR2_TIE
| UARTCR2_RE
| UARTCR2_TE
);
2327 writeb(temp
, sport
->port
.membase
+ UARTCR2
);
2330 if (sport
->lpuart_dma_rx_use
) {
2332 if (!lpuart_start_rx_dma(sport
))
2333 rx_dma_timer_init(sport
);
2335 sport
->lpuart_dma_rx_use
= false;
2339 if (sport
->dma_tx_chan
&& !lpuart_dma_tx_request(&sport
->port
)) {
2340 init_waitqueue_head(&sport
->dma_wait
);
2341 sport
->lpuart_dma_tx_use
= true;
2342 writeb(readb(sport
->port
.membase
+ UARTCR5
) |
2343 UARTCR5_TDMAS
, sport
->port
.membase
+ UARTCR5
);
2345 sport
->lpuart_dma_tx_use
= false;
2348 uart_resume_port(&lpuart_reg
, &sport
->port
);
2354 static SIMPLE_DEV_PM_OPS(lpuart_pm_ops
, lpuart_suspend
, lpuart_resume
);
2356 static struct platform_driver lpuart_driver
= {
2357 .probe
= lpuart_probe
,
2358 .remove
= lpuart_remove
,
2360 .name
= "fsl-lpuart",
2361 .of_match_table
= lpuart_dt_ids
,
2362 .pm
= &lpuart_pm_ops
,
2366 static int __init
lpuart_serial_init(void)
2368 int ret
= uart_register_driver(&lpuart_reg
);
2373 ret
= platform_driver_register(&lpuart_driver
);
2375 uart_unregister_driver(&lpuart_reg
);
2380 static void __exit
lpuart_serial_exit(void)
2382 platform_driver_unregister(&lpuart_driver
);
2383 uart_unregister_driver(&lpuart_reg
);
2386 module_init(lpuart_serial_init
);
2387 module_exit(lpuart_serial_exit
);
2389 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
2390 MODULE_LICENSE("GPL v2");