1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale lpuart serial port driver
5 * Copyright 2012-2014 Freescale Semiconductor, Inc.
9 #include <linux/console.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dmapool.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/of_dma.h>
19 #include <linux/serial_core.h>
20 #include <linux/slab.h>
21 #include <linux/tty_flip.h>
23 /* All registers are 8-bit width */
33 #define UARTMODEM 0x0d
34 #define UARTPFIFO 0x10
35 #define UARTCFIFO 0x11
36 #define UARTSFIFO 0x12
37 #define UARTTWFIFO 0x13
38 #define UARTTCFIFO 0x14
39 #define UARTRWFIFO 0x15
41 #define UARTBDH_LBKDIE 0x80
42 #define UARTBDH_RXEDGIE 0x40
43 #define UARTBDH_SBR_MASK 0x1f
45 #define UARTCR1_LOOPS 0x80
46 #define UARTCR1_RSRC 0x20
47 #define UARTCR1_M 0x10
48 #define UARTCR1_WAKE 0x08
49 #define UARTCR1_ILT 0x04
50 #define UARTCR1_PE 0x02
51 #define UARTCR1_PT 0x01
53 #define UARTCR2_TIE 0x80
54 #define UARTCR2_TCIE 0x40
55 #define UARTCR2_RIE 0x20
56 #define UARTCR2_ILIE 0x10
57 #define UARTCR2_TE 0x08
58 #define UARTCR2_RE 0x04
59 #define UARTCR2_RWU 0x02
60 #define UARTCR2_SBK 0x01
62 #define UARTSR1_TDRE 0x80
63 #define UARTSR1_TC 0x40
64 #define UARTSR1_RDRF 0x20
65 #define UARTSR1_IDLE 0x10
66 #define UARTSR1_OR 0x08
67 #define UARTSR1_NF 0x04
68 #define UARTSR1_FE 0x02
69 #define UARTSR1_PE 0x01
71 #define UARTCR3_R8 0x80
72 #define UARTCR3_T8 0x40
73 #define UARTCR3_TXDIR 0x20
74 #define UARTCR3_TXINV 0x10
75 #define UARTCR3_ORIE 0x08
76 #define UARTCR3_NEIE 0x04
77 #define UARTCR3_FEIE 0x02
78 #define UARTCR3_PEIE 0x01
80 #define UARTCR4_MAEN1 0x80
81 #define UARTCR4_MAEN2 0x40
82 #define UARTCR4_M10 0x20
83 #define UARTCR4_BRFA_MASK 0x1f
84 #define UARTCR4_BRFA_OFF 0
86 #define UARTCR5_TDMAS 0x80
87 #define UARTCR5_RDMAS 0x20
89 #define UARTMODEM_RXRTSE 0x08
90 #define UARTMODEM_TXRTSPOL 0x04
91 #define UARTMODEM_TXRTSE 0x02
92 #define UARTMODEM_TXCTSE 0x01
94 #define UARTPFIFO_TXFE 0x80
95 #define UARTPFIFO_FIFOSIZE_MASK 0x7
96 #define UARTPFIFO_TXSIZE_OFF 4
97 #define UARTPFIFO_RXFE 0x08
98 #define UARTPFIFO_RXSIZE_OFF 0
100 #define UARTCFIFO_TXFLUSH 0x80
101 #define UARTCFIFO_RXFLUSH 0x40
102 #define UARTCFIFO_RXOFE 0x04
103 #define UARTCFIFO_TXOFE 0x02
104 #define UARTCFIFO_RXUFE 0x01
106 #define UARTSFIFO_TXEMPT 0x80
107 #define UARTSFIFO_RXEMPT 0x40
108 #define UARTSFIFO_RXOF 0x04
109 #define UARTSFIFO_TXOF 0x02
110 #define UARTSFIFO_RXUF 0x01
112 /* 32-bit register definition */
113 #define UARTBAUD 0x00
114 #define UARTSTAT 0x04
115 #define UARTCTRL 0x08
116 #define UARTDATA 0x0C
117 #define UARTMATCH 0x10
118 #define UARTMODIR 0x14
119 #define UARTFIFO 0x18
120 #define UARTWATER 0x1c
122 #define UARTBAUD_MAEN1 0x80000000
123 #define UARTBAUD_MAEN2 0x40000000
124 #define UARTBAUD_M10 0x20000000
125 #define UARTBAUD_TDMAE 0x00800000
126 #define UARTBAUD_RDMAE 0x00200000
127 #define UARTBAUD_MATCFG 0x00400000
128 #define UARTBAUD_BOTHEDGE 0x00020000
129 #define UARTBAUD_RESYNCDIS 0x00010000
130 #define UARTBAUD_LBKDIE 0x00008000
131 #define UARTBAUD_RXEDGIE 0x00004000
132 #define UARTBAUD_SBNS 0x00002000
133 #define UARTBAUD_SBR 0x00000000
134 #define UARTBAUD_SBR_MASK 0x1fff
135 #define UARTBAUD_OSR_MASK 0x1f
136 #define UARTBAUD_OSR_SHIFT 24
138 #define UARTSTAT_LBKDIF 0x80000000
139 #define UARTSTAT_RXEDGIF 0x40000000
140 #define UARTSTAT_MSBF 0x20000000
141 #define UARTSTAT_RXINV 0x10000000
142 #define UARTSTAT_RWUID 0x08000000
143 #define UARTSTAT_BRK13 0x04000000
144 #define UARTSTAT_LBKDE 0x02000000
145 #define UARTSTAT_RAF 0x01000000
146 #define UARTSTAT_TDRE 0x00800000
147 #define UARTSTAT_TC 0x00400000
148 #define UARTSTAT_RDRF 0x00200000
149 #define UARTSTAT_IDLE 0x00100000
150 #define UARTSTAT_OR 0x00080000
151 #define UARTSTAT_NF 0x00040000
152 #define UARTSTAT_FE 0x00020000
153 #define UARTSTAT_PE 0x00010000
154 #define UARTSTAT_MA1F 0x00008000
155 #define UARTSTAT_M21F 0x00004000
157 #define UARTCTRL_R8T9 0x80000000
158 #define UARTCTRL_R9T8 0x40000000
159 #define UARTCTRL_TXDIR 0x20000000
160 #define UARTCTRL_TXINV 0x10000000
161 #define UARTCTRL_ORIE 0x08000000
162 #define UARTCTRL_NEIE 0x04000000
163 #define UARTCTRL_FEIE 0x02000000
164 #define UARTCTRL_PEIE 0x01000000
165 #define UARTCTRL_TIE 0x00800000
166 #define UARTCTRL_TCIE 0x00400000
167 #define UARTCTRL_RIE 0x00200000
168 #define UARTCTRL_ILIE 0x00100000
169 #define UARTCTRL_TE 0x00080000
170 #define UARTCTRL_RE 0x00040000
171 #define UARTCTRL_RWU 0x00020000
172 #define UARTCTRL_SBK 0x00010000
173 #define UARTCTRL_MA1IE 0x00008000
174 #define UARTCTRL_MA2IE 0x00004000
175 #define UARTCTRL_IDLECFG 0x00000100
176 #define UARTCTRL_LOOPS 0x00000080
177 #define UARTCTRL_DOZEEN 0x00000040
178 #define UARTCTRL_RSRC 0x00000020
179 #define UARTCTRL_M 0x00000010
180 #define UARTCTRL_WAKE 0x00000008
181 #define UARTCTRL_ILT 0x00000004
182 #define UARTCTRL_PE 0x00000002
183 #define UARTCTRL_PT 0x00000001
185 #define UARTDATA_NOISY 0x00008000
186 #define UARTDATA_PARITYE 0x00004000
187 #define UARTDATA_FRETSC 0x00002000
188 #define UARTDATA_RXEMPT 0x00001000
189 #define UARTDATA_IDLINE 0x00000800
190 #define UARTDATA_MASK 0x3ff
192 #define UARTMODIR_IREN 0x00020000
193 #define UARTMODIR_TXCTSSRC 0x00000020
194 #define UARTMODIR_TXCTSC 0x00000010
195 #define UARTMODIR_RXRTSE 0x00000008
196 #define UARTMODIR_TXRTSPOL 0x00000004
197 #define UARTMODIR_TXRTSE 0x00000002
198 #define UARTMODIR_TXCTSE 0x00000001
200 #define UARTFIFO_TXEMPT 0x00800000
201 #define UARTFIFO_RXEMPT 0x00400000
202 #define UARTFIFO_TXOF 0x00020000
203 #define UARTFIFO_RXUF 0x00010000
204 #define UARTFIFO_TXFLUSH 0x00008000
205 #define UARTFIFO_RXFLUSH 0x00004000
206 #define UARTFIFO_TXOFE 0x00000200
207 #define UARTFIFO_RXUFE 0x00000100
208 #define UARTFIFO_TXFE 0x00000080
209 #define UARTFIFO_FIFOSIZE_MASK 0x7
210 #define UARTFIFO_TXSIZE_OFF 4
211 #define UARTFIFO_RXFE 0x00000008
212 #define UARTFIFO_RXSIZE_OFF 0
213 #define UARTFIFO_DEPTH(x) (0x1 << ((x) ? ((x) + 1) : 0))
215 #define UARTWATER_COUNT_MASK 0xff
216 #define UARTWATER_TXCNT_OFF 8
217 #define UARTWATER_RXCNT_OFF 24
218 #define UARTWATER_WATER_MASK 0xff
219 #define UARTWATER_TXWATER_OFF 0
220 #define UARTWATER_RXWATER_OFF 16
222 /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
223 #define DMA_RX_TIMEOUT (10)
225 #define DRIVER_NAME "fsl-lpuart"
226 #define DEV_NAME "ttyLP"
229 /* IMX lpuart has four extra unused regs located at the beginning */
230 #define IMX_REG_OFF 0x10
232 static DEFINE_IDA(fsl_lpuart_ida
);
243 struct uart_port port
;
244 enum lpuart_type devtype
;
246 struct clk
*baud_clk
;
247 unsigned int txfifo_size
;
248 unsigned int rxfifo_size
;
250 bool lpuart_dma_tx_use
;
251 bool lpuart_dma_rx_use
;
252 struct dma_chan
*dma_tx_chan
;
253 struct dma_chan
*dma_rx_chan
;
254 struct dma_async_tx_descriptor
*dma_tx_desc
;
255 struct dma_async_tx_descriptor
*dma_rx_desc
;
256 dma_cookie_t dma_tx_cookie
;
257 dma_cookie_t dma_rx_cookie
;
258 unsigned int dma_tx_bytes
;
259 unsigned int dma_rx_bytes
;
260 bool dma_tx_in_progress
;
261 unsigned int dma_rx_timeout
;
262 struct timer_list lpuart_timer
;
263 struct scatterlist rx_sgl
, tx_sgl
[2];
264 struct circ_buf rx_ring
;
265 int rx_dma_rng_buf_len
;
266 unsigned int dma_tx_nents
;
267 wait_queue_head_t dma_wait
;
271 struct lpuart_soc_data
{
272 enum lpuart_type devtype
;
277 static const struct lpuart_soc_data vf_data
= {
278 .devtype
= VF610_LPUART
,
282 static const struct lpuart_soc_data ls1021a_data
= {
283 .devtype
= LS1021A_LPUART
,
284 .iotype
= UPIO_MEM32BE
,
287 static const struct lpuart_soc_data ls1028a_data
= {
288 .devtype
= LS1028A_LPUART
,
289 .iotype
= UPIO_MEM32
,
292 static struct lpuart_soc_data imx7ulp_data
= {
293 .devtype
= IMX7ULP_LPUART
,
294 .iotype
= UPIO_MEM32
,
295 .reg_off
= IMX_REG_OFF
,
298 static struct lpuart_soc_data imx8qxp_data
= {
299 .devtype
= IMX8QXP_LPUART
,
300 .iotype
= UPIO_MEM32
,
301 .reg_off
= IMX_REG_OFF
,
304 static const struct of_device_id lpuart_dt_ids
[] = {
305 { .compatible
= "fsl,vf610-lpuart", .data
= &vf_data
, },
306 { .compatible
= "fsl,ls1021a-lpuart", .data
= &ls1021a_data
, },
307 { .compatible
= "fsl,ls1028a-lpuart", .data
= &ls1028a_data
, },
308 { .compatible
= "fsl,imx7ulp-lpuart", .data
= &imx7ulp_data
, },
309 { .compatible
= "fsl,imx8qxp-lpuart", .data
= &imx8qxp_data
, },
312 MODULE_DEVICE_TABLE(of
, lpuart_dt_ids
);
314 /* Forward declare this for the dma callbacks*/
315 static void lpuart_dma_tx_complete(void *arg
);
317 static inline bool is_ls1028a_lpuart(struct lpuart_port
*sport
)
319 return sport
->devtype
== LS1028A_LPUART
;
322 static inline bool is_imx8qxp_lpuart(struct lpuart_port
*sport
)
324 return sport
->devtype
== IMX8QXP_LPUART
;
327 static inline u32
lpuart32_read(struct uart_port
*port
, u32 off
)
329 switch (port
->iotype
) {
331 return readl(port
->membase
+ off
);
333 return ioread32be(port
->membase
+ off
);
339 static inline void lpuart32_write(struct uart_port
*port
, u32 val
,
342 switch (port
->iotype
) {
344 writel(val
, port
->membase
+ off
);
347 iowrite32be(val
, port
->membase
+ off
);
352 static int __lpuart_enable_clks(struct lpuart_port
*sport
, bool is_en
)
357 ret
= clk_prepare_enable(sport
->ipg_clk
);
361 ret
= clk_prepare_enable(sport
->baud_clk
);
363 clk_disable_unprepare(sport
->ipg_clk
);
367 clk_disable_unprepare(sport
->baud_clk
);
368 clk_disable_unprepare(sport
->ipg_clk
);
374 static unsigned int lpuart_get_baud_clk_rate(struct lpuart_port
*sport
)
376 if (is_imx8qxp_lpuart(sport
))
377 return clk_get_rate(sport
->baud_clk
);
379 return clk_get_rate(sport
->ipg_clk
);
382 #define lpuart_enable_clks(x) __lpuart_enable_clks(x, true)
383 #define lpuart_disable_clks(x) __lpuart_enable_clks(x, false)
385 static void lpuart_stop_tx(struct uart_port
*port
)
389 temp
= readb(port
->membase
+ UARTCR2
);
390 temp
&= ~(UARTCR2_TIE
| UARTCR2_TCIE
);
391 writeb(temp
, port
->membase
+ UARTCR2
);
394 static void lpuart32_stop_tx(struct uart_port
*port
)
398 temp
= lpuart32_read(port
, UARTCTRL
);
399 temp
&= ~(UARTCTRL_TIE
| UARTCTRL_TCIE
);
400 lpuart32_write(port
, temp
, UARTCTRL
);
403 static void lpuart_stop_rx(struct uart_port
*port
)
407 temp
= readb(port
->membase
+ UARTCR2
);
408 writeb(temp
& ~UARTCR2_RE
, port
->membase
+ UARTCR2
);
411 static void lpuart32_stop_rx(struct uart_port
*port
)
415 temp
= lpuart32_read(port
, UARTCTRL
);
416 lpuart32_write(port
, temp
& ~UARTCTRL_RE
, UARTCTRL
);
419 static void lpuart_dma_tx(struct lpuart_port
*sport
)
421 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
422 struct scatterlist
*sgl
= sport
->tx_sgl
;
423 struct device
*dev
= sport
->port
.dev
;
424 struct dma_chan
*chan
= sport
->dma_tx_chan
;
427 if (sport
->dma_tx_in_progress
)
430 sport
->dma_tx_bytes
= uart_circ_chars_pending(xmit
);
432 if (xmit
->tail
< xmit
->head
|| xmit
->head
== 0) {
433 sport
->dma_tx_nents
= 1;
434 sg_init_one(sgl
, xmit
->buf
+ xmit
->tail
, sport
->dma_tx_bytes
);
436 sport
->dma_tx_nents
= 2;
437 sg_init_table(sgl
, 2);
438 sg_set_buf(sgl
, xmit
->buf
+ xmit
->tail
,
439 UART_XMIT_SIZE
- xmit
->tail
);
440 sg_set_buf(sgl
+ 1, xmit
->buf
, xmit
->head
);
443 ret
= dma_map_sg(chan
->device
->dev
, sgl
, sport
->dma_tx_nents
,
446 dev_err(dev
, "DMA mapping error for TX.\n");
450 sport
->dma_tx_desc
= dmaengine_prep_slave_sg(chan
, sgl
,
453 if (!sport
->dma_tx_desc
) {
454 dma_unmap_sg(chan
->device
->dev
, sgl
, sport
->dma_tx_nents
,
456 dev_err(dev
, "Cannot prepare TX slave DMA!\n");
460 sport
->dma_tx_desc
->callback
= lpuart_dma_tx_complete
;
461 sport
->dma_tx_desc
->callback_param
= sport
;
462 sport
->dma_tx_in_progress
= true;
463 sport
->dma_tx_cookie
= dmaengine_submit(sport
->dma_tx_desc
);
464 dma_async_issue_pending(chan
);
467 static bool lpuart_stopped_or_empty(struct uart_port
*port
)
469 return uart_circ_empty(&port
->state
->xmit
) || uart_tx_stopped(port
);
472 static void lpuart_dma_tx_complete(void *arg
)
474 struct lpuart_port
*sport
= arg
;
475 struct scatterlist
*sgl
= &sport
->tx_sgl
[0];
476 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
477 struct dma_chan
*chan
= sport
->dma_tx_chan
;
480 spin_lock_irqsave(&sport
->port
.lock
, flags
);
482 dma_unmap_sg(chan
->device
->dev
, sgl
, sport
->dma_tx_nents
,
485 xmit
->tail
= (xmit
->tail
+ sport
->dma_tx_bytes
) & (UART_XMIT_SIZE
- 1);
487 sport
->port
.icount
.tx
+= sport
->dma_tx_bytes
;
488 sport
->dma_tx_in_progress
= false;
489 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
491 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
492 uart_write_wakeup(&sport
->port
);
494 if (waitqueue_active(&sport
->dma_wait
)) {
495 wake_up(&sport
->dma_wait
);
499 spin_lock_irqsave(&sport
->port
.lock
, flags
);
501 if (!lpuart_stopped_or_empty(&sport
->port
))
502 lpuart_dma_tx(sport
);
504 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
507 static dma_addr_t
lpuart_dma_datareg_addr(struct lpuart_port
*sport
)
509 switch (sport
->port
.iotype
) {
511 return sport
->port
.mapbase
+ UARTDATA
;
513 return sport
->port
.mapbase
+ UARTDATA
+ sizeof(u32
) - 1;
515 return sport
->port
.mapbase
+ UARTDR
;
518 static int lpuart_dma_tx_request(struct uart_port
*port
)
520 struct lpuart_port
*sport
= container_of(port
,
521 struct lpuart_port
, port
);
522 struct dma_slave_config dma_tx_sconfig
= {};
525 dma_tx_sconfig
.dst_addr
= lpuart_dma_datareg_addr(sport
);
526 dma_tx_sconfig
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
527 dma_tx_sconfig
.dst_maxburst
= 1;
528 dma_tx_sconfig
.direction
= DMA_MEM_TO_DEV
;
529 ret
= dmaengine_slave_config(sport
->dma_tx_chan
, &dma_tx_sconfig
);
532 dev_err(sport
->port
.dev
,
533 "DMA slave config failed, err = %d\n", ret
);
540 static bool lpuart_is_32(struct lpuart_port
*sport
)
542 return sport
->port
.iotype
== UPIO_MEM32
||
543 sport
->port
.iotype
== UPIO_MEM32BE
;
546 static void lpuart_flush_buffer(struct uart_port
*port
)
548 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
549 struct dma_chan
*chan
= sport
->dma_tx_chan
;
552 if (sport
->lpuart_dma_tx_use
) {
553 if (sport
->dma_tx_in_progress
) {
554 dma_unmap_sg(chan
->device
->dev
, &sport
->tx_sgl
[0],
555 sport
->dma_tx_nents
, DMA_TO_DEVICE
);
556 sport
->dma_tx_in_progress
= false;
558 dmaengine_terminate_all(chan
);
561 if (lpuart_is_32(sport
)) {
562 val
= lpuart32_read(&sport
->port
, UARTFIFO
);
563 val
|= UARTFIFO_TXFLUSH
| UARTFIFO_RXFLUSH
;
564 lpuart32_write(&sport
->port
, val
, UARTFIFO
);
566 val
= readb(sport
->port
.membase
+ UARTCFIFO
);
567 val
|= UARTCFIFO_TXFLUSH
| UARTCFIFO_RXFLUSH
;
568 writeb(val
, sport
->port
.membase
+ UARTCFIFO
);
572 static void lpuart_wait_bit_set(struct uart_port
*port
, unsigned int offset
,
575 while (!(readb(port
->membase
+ offset
) & bit
))
579 static void lpuart32_wait_bit_set(struct uart_port
*port
, unsigned int offset
,
582 while (!(lpuart32_read(port
, offset
) & bit
))
586 #if defined(CONFIG_CONSOLE_POLL)
588 static int lpuart_poll_init(struct uart_port
*port
)
590 struct lpuart_port
*sport
= container_of(port
,
591 struct lpuart_port
, port
);
595 sport
->port
.fifosize
= 0;
597 spin_lock_irqsave(&sport
->port
.lock
, flags
);
598 /* Disable Rx & Tx */
599 writeb(0, sport
->port
.membase
+ UARTCR2
);
601 temp
= readb(sport
->port
.membase
+ UARTPFIFO
);
602 /* Enable Rx and Tx FIFO */
603 writeb(temp
| UARTPFIFO_RXFE
| UARTPFIFO_TXFE
,
604 sport
->port
.membase
+ UARTPFIFO
);
606 /* flush Tx and Rx FIFO */
607 writeb(UARTCFIFO_TXFLUSH
| UARTCFIFO_RXFLUSH
,
608 sport
->port
.membase
+ UARTCFIFO
);
610 /* explicitly clear RDRF */
611 if (readb(sport
->port
.membase
+ UARTSR1
) & UARTSR1_RDRF
) {
612 readb(sport
->port
.membase
+ UARTDR
);
613 writeb(UARTSFIFO_RXUF
, sport
->port
.membase
+ UARTSFIFO
);
616 writeb(0, sport
->port
.membase
+ UARTTWFIFO
);
617 writeb(1, sport
->port
.membase
+ UARTRWFIFO
);
619 /* Enable Rx and Tx */
620 writeb(UARTCR2_RE
| UARTCR2_TE
, sport
->port
.membase
+ UARTCR2
);
621 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
626 static void lpuart_poll_put_char(struct uart_port
*port
, unsigned char c
)
629 lpuart_wait_bit_set(port
, UARTSR1
, UARTSR1_TDRE
);
630 writeb(c
, port
->membase
+ UARTDR
);
633 static int lpuart_poll_get_char(struct uart_port
*port
)
635 if (!(readb(port
->membase
+ UARTSR1
) & UARTSR1_RDRF
))
638 return readb(port
->membase
+ UARTDR
);
641 static int lpuart32_poll_init(struct uart_port
*port
)
644 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
647 sport
->port
.fifosize
= 0;
649 spin_lock_irqsave(&sport
->port
.lock
, flags
);
651 /* Disable Rx & Tx */
652 lpuart32_write(&sport
->port
, UARTCTRL
, 0);
654 temp
= lpuart32_read(&sport
->port
, UARTFIFO
);
656 /* Enable Rx and Tx FIFO */
657 lpuart32_write(&sport
->port
, UARTFIFO
,
658 temp
| UARTFIFO_RXFE
| UARTFIFO_TXFE
);
660 /* flush Tx and Rx FIFO */
661 lpuart32_write(&sport
->port
, UARTFIFO
,
662 UARTFIFO_TXFLUSH
| UARTFIFO_RXFLUSH
);
664 /* explicitly clear RDRF */
665 if (lpuart32_read(&sport
->port
, UARTSTAT
) & UARTSTAT_RDRF
) {
666 lpuart32_read(&sport
->port
, UARTDATA
);
667 lpuart32_write(&sport
->port
, UARTFIFO
, UARTFIFO_RXUF
);
670 /* Enable Rx and Tx */
671 lpuart32_write(&sport
->port
, UARTCTRL
, UARTCTRL_RE
| UARTCTRL_TE
);
672 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
677 static void lpuart32_poll_put_char(struct uart_port
*port
, unsigned char c
)
679 lpuart32_wait_bit_set(port
, UARTSTAT
, UARTSTAT_TDRE
);
680 lpuart32_write(port
, UARTDATA
, c
);
683 static int lpuart32_poll_get_char(struct uart_port
*port
)
685 if (!(lpuart32_read(port
, UARTSTAT
) & UARTSTAT_RDRF
))
688 return lpuart32_read(port
, UARTDATA
);
692 static inline void lpuart_transmit_buffer(struct lpuart_port
*sport
)
694 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
696 if (sport
->port
.x_char
) {
697 writeb(sport
->port
.x_char
, sport
->port
.membase
+ UARTDR
);
698 sport
->port
.icount
.tx
++;
699 sport
->port
.x_char
= 0;
703 if (lpuart_stopped_or_empty(&sport
->port
)) {
704 lpuart_stop_tx(&sport
->port
);
708 while (!uart_circ_empty(xmit
) &&
709 (readb(sport
->port
.membase
+ UARTTCFIFO
) < sport
->txfifo_size
)) {
710 writeb(xmit
->buf
[xmit
->tail
], sport
->port
.membase
+ UARTDR
);
711 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
712 sport
->port
.icount
.tx
++;
715 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
716 uart_write_wakeup(&sport
->port
);
718 if (uart_circ_empty(xmit
))
719 lpuart_stop_tx(&sport
->port
);
722 static inline void lpuart32_transmit_buffer(struct lpuart_port
*sport
)
724 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
727 if (sport
->port
.x_char
) {
728 lpuart32_write(&sport
->port
, sport
->port
.x_char
, UARTDATA
);
729 sport
->port
.icount
.tx
++;
730 sport
->port
.x_char
= 0;
734 if (lpuart_stopped_or_empty(&sport
->port
)) {
735 lpuart32_stop_tx(&sport
->port
);
739 txcnt
= lpuart32_read(&sport
->port
, UARTWATER
);
740 txcnt
= txcnt
>> UARTWATER_TXCNT_OFF
;
741 txcnt
&= UARTWATER_COUNT_MASK
;
742 while (!uart_circ_empty(xmit
) && (txcnt
< sport
->txfifo_size
)) {
743 lpuart32_write(&sport
->port
, xmit
->buf
[xmit
->tail
], UARTDATA
);
744 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
745 sport
->port
.icount
.tx
++;
746 txcnt
= lpuart32_read(&sport
->port
, UARTWATER
);
747 txcnt
= txcnt
>> UARTWATER_TXCNT_OFF
;
748 txcnt
&= UARTWATER_COUNT_MASK
;
751 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
752 uart_write_wakeup(&sport
->port
);
754 if (uart_circ_empty(xmit
))
755 lpuart32_stop_tx(&sport
->port
);
758 static void lpuart_start_tx(struct uart_port
*port
)
760 struct lpuart_port
*sport
= container_of(port
,
761 struct lpuart_port
, port
);
764 temp
= readb(port
->membase
+ UARTCR2
);
765 writeb(temp
| UARTCR2_TIE
, port
->membase
+ UARTCR2
);
767 if (sport
->lpuart_dma_tx_use
) {
768 if (!lpuart_stopped_or_empty(port
))
769 lpuart_dma_tx(sport
);
771 if (readb(port
->membase
+ UARTSR1
) & UARTSR1_TDRE
)
772 lpuart_transmit_buffer(sport
);
776 static void lpuart32_start_tx(struct uart_port
*port
)
778 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
781 if (sport
->lpuart_dma_tx_use
) {
782 if (!lpuart_stopped_or_empty(port
))
783 lpuart_dma_tx(sport
);
785 temp
= lpuart32_read(port
, UARTCTRL
);
786 lpuart32_write(port
, temp
| UARTCTRL_TIE
, UARTCTRL
);
788 if (lpuart32_read(port
, UARTSTAT
) & UARTSTAT_TDRE
)
789 lpuart32_transmit_buffer(sport
);
793 /* return TIOCSER_TEMT when transmitter is not busy */
794 static unsigned int lpuart_tx_empty(struct uart_port
*port
)
796 struct lpuart_port
*sport
= container_of(port
,
797 struct lpuart_port
, port
);
798 unsigned char sr1
= readb(port
->membase
+ UARTSR1
);
799 unsigned char sfifo
= readb(port
->membase
+ UARTSFIFO
);
801 if (sport
->dma_tx_in_progress
)
804 if (sr1
& UARTSR1_TC
&& sfifo
& UARTSFIFO_TXEMPT
)
810 static unsigned int lpuart32_tx_empty(struct uart_port
*port
)
812 struct lpuart_port
*sport
= container_of(port
,
813 struct lpuart_port
, port
);
814 unsigned long stat
= lpuart32_read(port
, UARTSTAT
);
815 unsigned long sfifo
= lpuart32_read(port
, UARTFIFO
);
817 if (sport
->dma_tx_in_progress
)
820 if (stat
& UARTSTAT_TC
&& sfifo
& UARTFIFO_TXEMPT
)
826 static void lpuart_txint(struct lpuart_port
*sport
)
830 spin_lock_irqsave(&sport
->port
.lock
, flags
);
831 lpuart_transmit_buffer(sport
);
832 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
835 static void lpuart_rxint(struct lpuart_port
*sport
)
837 unsigned int flg
, ignored
= 0, overrun
= 0;
838 struct tty_port
*port
= &sport
->port
.state
->port
;
840 unsigned char rx
, sr
;
842 spin_lock_irqsave(&sport
->port
.lock
, flags
);
844 while (!(readb(sport
->port
.membase
+ UARTSFIFO
) & UARTSFIFO_RXEMPT
)) {
846 sport
->port
.icount
.rx
++;
848 * to clear the FE, OR, NF, FE, PE flags,
849 * read SR1 then read DR
851 sr
= readb(sport
->port
.membase
+ UARTSR1
);
852 rx
= readb(sport
->port
.membase
+ UARTDR
);
854 if (uart_handle_sysrq_char(&sport
->port
, (unsigned char)rx
))
857 if (sr
& (UARTSR1_PE
| UARTSR1_OR
| UARTSR1_FE
)) {
859 sport
->port
.icount
.parity
++;
860 else if (sr
& UARTSR1_FE
)
861 sport
->port
.icount
.frame
++;
866 if (sr
& sport
->port
.ignore_status_mask
) {
872 sr
&= sport
->port
.read_status_mask
;
876 else if (sr
& UARTSR1_FE
)
882 sport
->port
.sysrq
= 0;
885 tty_insert_flip_char(port
, rx
, flg
);
890 sport
->port
.icount
.overrun
+= overrun
;
893 * Overruns cause FIFO pointers to become missaligned.
894 * Flushing the receive FIFO reinitializes the pointers.
896 writeb(UARTCFIFO_RXFLUSH
, sport
->port
.membase
+ UARTCFIFO
);
897 writeb(UARTSFIFO_RXOF
, sport
->port
.membase
+ UARTSFIFO
);
900 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
902 tty_flip_buffer_push(port
);
905 static void lpuart32_txint(struct lpuart_port
*sport
)
909 spin_lock_irqsave(&sport
->port
.lock
, flags
);
910 lpuart32_transmit_buffer(sport
);
911 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
914 static void lpuart32_rxint(struct lpuart_port
*sport
)
916 unsigned int flg
, ignored
= 0;
917 struct tty_port
*port
= &sport
->port
.state
->port
;
919 unsigned long rx
, sr
;
921 spin_lock_irqsave(&sport
->port
.lock
, flags
);
923 while (!(lpuart32_read(&sport
->port
, UARTFIFO
) & UARTFIFO_RXEMPT
)) {
925 sport
->port
.icount
.rx
++;
927 * to clear the FE, OR, NF, FE, PE flags,
928 * read STAT then read DATA reg
930 sr
= lpuart32_read(&sport
->port
, UARTSTAT
);
931 rx
= lpuart32_read(&sport
->port
, UARTDATA
);
934 if (uart_handle_sysrq_char(&sport
->port
, (unsigned char)rx
))
937 if (sr
& (UARTSTAT_PE
| UARTSTAT_OR
| UARTSTAT_FE
)) {
938 if (sr
& UARTSTAT_PE
)
939 sport
->port
.icount
.parity
++;
940 else if (sr
& UARTSTAT_FE
)
941 sport
->port
.icount
.frame
++;
943 if (sr
& UARTSTAT_OR
)
944 sport
->port
.icount
.overrun
++;
946 if (sr
& sport
->port
.ignore_status_mask
) {
952 sr
&= sport
->port
.read_status_mask
;
954 if (sr
& UARTSTAT_PE
)
956 else if (sr
& UARTSTAT_FE
)
959 if (sr
& UARTSTAT_OR
)
962 sport
->port
.sysrq
= 0;
965 tty_insert_flip_char(port
, rx
, flg
);
969 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
971 tty_flip_buffer_push(port
);
974 static irqreturn_t
lpuart_int(int irq
, void *dev_id
)
976 struct lpuart_port
*sport
= dev_id
;
979 sts
= readb(sport
->port
.membase
+ UARTSR1
);
981 if (sts
& UARTSR1_RDRF
&& !sport
->lpuart_dma_rx_use
)
984 if (sts
& UARTSR1_TDRE
&& !sport
->lpuart_dma_tx_use
)
990 static irqreturn_t
lpuart32_int(int irq
, void *dev_id
)
992 struct lpuart_port
*sport
= dev_id
;
993 unsigned long sts
, rxcount
;
995 sts
= lpuart32_read(&sport
->port
, UARTSTAT
);
996 rxcount
= lpuart32_read(&sport
->port
, UARTWATER
);
997 rxcount
= rxcount
>> UARTWATER_RXCNT_OFF
;
999 if ((sts
& UARTSTAT_RDRF
|| rxcount
> 0) && !sport
->lpuart_dma_rx_use
)
1000 lpuart32_rxint(sport
);
1002 if ((sts
& UARTSTAT_TDRE
) && !sport
->lpuart_dma_tx_use
)
1003 lpuart32_txint(sport
);
1005 lpuart32_write(&sport
->port
, sts
, UARTSTAT
);
1009 static void lpuart_copy_rx_to_tty(struct lpuart_port
*sport
)
1011 struct tty_port
*port
= &sport
->port
.state
->port
;
1012 struct dma_tx_state state
;
1013 enum dma_status dmastat
;
1014 struct dma_chan
*chan
= sport
->dma_rx_chan
;
1015 struct circ_buf
*ring
= &sport
->rx_ring
;
1016 unsigned long flags
;
1019 if (lpuart_is_32(sport
)) {
1020 unsigned long sr
= lpuart32_read(&sport
->port
, UARTSTAT
);
1022 if (sr
& (UARTSTAT_PE
| UARTSTAT_FE
)) {
1023 /* Read DR to clear the error flags */
1024 lpuart32_read(&sport
->port
, UARTDATA
);
1026 if (sr
& UARTSTAT_PE
)
1027 sport
->port
.icount
.parity
++;
1028 else if (sr
& UARTSTAT_FE
)
1029 sport
->port
.icount
.frame
++;
1032 unsigned char sr
= readb(sport
->port
.membase
+ UARTSR1
);
1034 if (sr
& (UARTSR1_PE
| UARTSR1_FE
)) {
1037 /* Disable receiver during this operation... */
1038 cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1040 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
1042 /* Read DR to clear the error flags */
1043 readb(sport
->port
.membase
+ UARTDR
);
1045 if (sr
& UARTSR1_PE
)
1046 sport
->port
.icount
.parity
++;
1047 else if (sr
& UARTSR1_FE
)
1048 sport
->port
.icount
.frame
++;
1050 * At this point parity/framing error is
1051 * cleared However, since the DMA already read
1052 * the data register and we had to read it
1053 * again after reading the status register to
1054 * properly clear the flags, the FIFO actually
1055 * underflowed... This requires a clearing of
1058 if (readb(sport
->port
.membase
+ UARTSFIFO
) &
1060 writeb(UARTSFIFO_RXUF
,
1061 sport
->port
.membase
+ UARTSFIFO
);
1062 writeb(UARTCFIFO_RXFLUSH
,
1063 sport
->port
.membase
+ UARTCFIFO
);
1067 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
1071 async_tx_ack(sport
->dma_rx_desc
);
1073 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1075 dmastat
= dmaengine_tx_status(chan
, sport
->dma_rx_cookie
, &state
);
1076 if (dmastat
== DMA_ERROR
) {
1077 dev_err(sport
->port
.dev
, "Rx DMA transfer failed!\n");
1078 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1082 /* CPU claims ownership of RX DMA buffer */
1083 dma_sync_sg_for_cpu(chan
->device
->dev
, &sport
->rx_sgl
, 1,
1087 * ring->head points to the end of data already written by the DMA.
1088 * ring->tail points to the beginning of data to be read by the
1090 * The current transfer size should not be larger than the dma buffer
1093 ring
->head
= sport
->rx_sgl
.length
- state
.residue
;
1094 BUG_ON(ring
->head
> sport
->rx_sgl
.length
);
1096 * At this point ring->head may point to the first byte right after the
1097 * last byte of the dma buffer:
1098 * 0 <= ring->head <= sport->rx_sgl.length
1100 * However ring->tail must always points inside the dma buffer:
1101 * 0 <= ring->tail <= sport->rx_sgl.length - 1
1103 * Since we use a ring buffer, we have to handle the case
1104 * where head is lower than tail. In such a case, we first read from
1105 * tail to the end of the buffer then reset tail.
1107 if (ring
->head
< ring
->tail
) {
1108 count
= sport
->rx_sgl
.length
- ring
->tail
;
1110 tty_insert_flip_string(port
, ring
->buf
+ ring
->tail
, count
);
1112 sport
->port
.icount
.rx
+= count
;
1115 /* Finally we read data from tail to head */
1116 if (ring
->tail
< ring
->head
) {
1117 count
= ring
->head
- ring
->tail
;
1118 tty_insert_flip_string(port
, ring
->buf
+ ring
->tail
, count
);
1119 /* Wrap ring->head if needed */
1120 if (ring
->head
>= sport
->rx_sgl
.length
)
1122 ring
->tail
= ring
->head
;
1123 sport
->port
.icount
.rx
+= count
;
1126 dma_sync_sg_for_device(chan
->device
->dev
, &sport
->rx_sgl
, 1,
1129 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1131 tty_flip_buffer_push(port
);
1132 mod_timer(&sport
->lpuart_timer
, jiffies
+ sport
->dma_rx_timeout
);
1135 static void lpuart_dma_rx_complete(void *arg
)
1137 struct lpuart_port
*sport
= arg
;
1139 lpuart_copy_rx_to_tty(sport
);
1142 static void lpuart_timer_func(struct timer_list
*t
)
1144 struct lpuart_port
*sport
= from_timer(sport
, t
, lpuart_timer
);
1146 lpuart_copy_rx_to_tty(sport
);
1149 static inline int lpuart_start_rx_dma(struct lpuart_port
*sport
)
1151 struct dma_slave_config dma_rx_sconfig
= {};
1152 struct circ_buf
*ring
= &sport
->rx_ring
;
1155 struct tty_port
*port
= &sport
->port
.state
->port
;
1156 struct tty_struct
*tty
= port
->tty
;
1157 struct ktermios
*termios
= &tty
->termios
;
1158 struct dma_chan
*chan
= sport
->dma_rx_chan
;
1160 baud
= tty_get_baud_rate(tty
);
1162 bits
= (termios
->c_cflag
& CSIZE
) == CS7
? 9 : 10;
1163 if (termios
->c_cflag
& PARENB
)
1167 * Calculate length of one DMA buffer size to keep latency below
1168 * 10ms at any baud rate.
1170 sport
->rx_dma_rng_buf_len
= (DMA_RX_TIMEOUT
* baud
/ bits
/ 1000) * 2;
1171 sport
->rx_dma_rng_buf_len
= (1 << (fls(sport
->rx_dma_rng_buf_len
) - 1));
1172 if (sport
->rx_dma_rng_buf_len
< 16)
1173 sport
->rx_dma_rng_buf_len
= 16;
1175 ring
->buf
= kzalloc(sport
->rx_dma_rng_buf_len
, GFP_ATOMIC
);
1179 sg_init_one(&sport
->rx_sgl
, ring
->buf
, sport
->rx_dma_rng_buf_len
);
1180 nent
= dma_map_sg(chan
->device
->dev
, &sport
->rx_sgl
, 1,
1184 dev_err(sport
->port
.dev
, "DMA Rx mapping error\n");
1188 dma_rx_sconfig
.src_addr
= lpuart_dma_datareg_addr(sport
);
1189 dma_rx_sconfig
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
1190 dma_rx_sconfig
.src_maxburst
= 1;
1191 dma_rx_sconfig
.direction
= DMA_DEV_TO_MEM
;
1192 ret
= dmaengine_slave_config(chan
, &dma_rx_sconfig
);
1195 dev_err(sport
->port
.dev
,
1196 "DMA Rx slave config failed, err = %d\n", ret
);
1200 sport
->dma_rx_desc
= dmaengine_prep_dma_cyclic(chan
,
1201 sg_dma_address(&sport
->rx_sgl
),
1202 sport
->rx_sgl
.length
,
1203 sport
->rx_sgl
.length
/ 2,
1205 DMA_PREP_INTERRUPT
);
1206 if (!sport
->dma_rx_desc
) {
1207 dev_err(sport
->port
.dev
, "Cannot prepare cyclic DMA\n");
1211 sport
->dma_rx_desc
->callback
= lpuart_dma_rx_complete
;
1212 sport
->dma_rx_desc
->callback_param
= sport
;
1213 sport
->dma_rx_cookie
= dmaengine_submit(sport
->dma_rx_desc
);
1214 dma_async_issue_pending(chan
);
1216 if (lpuart_is_32(sport
)) {
1217 unsigned long temp
= lpuart32_read(&sport
->port
, UARTBAUD
);
1219 lpuart32_write(&sport
->port
, temp
| UARTBAUD_RDMAE
, UARTBAUD
);
1221 writeb(readb(sport
->port
.membase
+ UARTCR5
) | UARTCR5_RDMAS
,
1222 sport
->port
.membase
+ UARTCR5
);
1228 static void lpuart_dma_rx_free(struct uart_port
*port
)
1230 struct lpuart_port
*sport
= container_of(port
,
1231 struct lpuart_port
, port
);
1232 struct dma_chan
*chan
= sport
->dma_rx_chan
;
1235 dmaengine_terminate_all(chan
);
1237 dma_unmap_sg(chan
->device
->dev
, &sport
->rx_sgl
, 1, DMA_FROM_DEVICE
);
1238 kfree(sport
->rx_ring
.buf
);
1239 sport
->rx_ring
.tail
= 0;
1240 sport
->rx_ring
.head
= 0;
1241 sport
->dma_rx_desc
= NULL
;
1242 sport
->dma_rx_cookie
= -EINVAL
;
1245 static int lpuart_config_rs485(struct uart_port
*port
,
1246 struct serial_rs485
*rs485
)
1248 struct lpuart_port
*sport
= container_of(port
,
1249 struct lpuart_port
, port
);
1251 u8 modem
= readb(sport
->port
.membase
+ UARTMODEM
) &
1252 ~(UARTMODEM_TXRTSPOL
| UARTMODEM_TXRTSE
);
1253 writeb(modem
, sport
->port
.membase
+ UARTMODEM
);
1255 /* clear unsupported configurations */
1256 rs485
->delay_rts_before_send
= 0;
1257 rs485
->delay_rts_after_send
= 0;
1258 rs485
->flags
&= ~SER_RS485_RX_DURING_TX
;
1260 if (rs485
->flags
& SER_RS485_ENABLED
) {
1261 /* Enable auto RS-485 RTS mode */
1262 modem
|= UARTMODEM_TXRTSE
;
1265 * RTS needs to be logic HIGH either during transer _or_ after
1266 * transfer, other variants are not supported by the hardware.
1269 if (!(rs485
->flags
& (SER_RS485_RTS_ON_SEND
|
1270 SER_RS485_RTS_AFTER_SEND
)))
1271 rs485
->flags
|= SER_RS485_RTS_ON_SEND
;
1273 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
&&
1274 rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)
1275 rs485
->flags
&= ~SER_RS485_RTS_AFTER_SEND
;
1278 * The hardware defaults to RTS logic HIGH while transfer.
1279 * Switch polarity in case RTS shall be logic HIGH
1281 * Note: UART is assumed to be active high.
1283 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
)
1284 modem
&= ~UARTMODEM_TXRTSPOL
;
1285 else if (rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)
1286 modem
|= UARTMODEM_TXRTSPOL
;
1289 /* Store the new configuration */
1290 sport
->port
.rs485
= *rs485
;
1292 writeb(modem
, sport
->port
.membase
+ UARTMODEM
);
1296 static int lpuart32_config_rs485(struct uart_port
*port
,
1297 struct serial_rs485
*rs485
)
1299 struct lpuart_port
*sport
= container_of(port
,
1300 struct lpuart_port
, port
);
1302 unsigned long modem
= lpuart32_read(&sport
->port
, UARTMODIR
)
1303 & ~(UARTMODEM_TXRTSPOL
| UARTMODEM_TXRTSE
);
1304 lpuart32_write(&sport
->port
, modem
, UARTMODIR
);
1306 /* clear unsupported configurations */
1307 rs485
->delay_rts_before_send
= 0;
1308 rs485
->delay_rts_after_send
= 0;
1309 rs485
->flags
&= ~SER_RS485_RX_DURING_TX
;
1311 if (rs485
->flags
& SER_RS485_ENABLED
) {
1312 /* Enable auto RS-485 RTS mode */
1313 modem
|= UARTMODEM_TXRTSE
;
1316 * RTS needs to be logic HIGH either during transer _or_ after
1317 * transfer, other variants are not supported by the hardware.
1320 if (!(rs485
->flags
& (SER_RS485_RTS_ON_SEND
|
1321 SER_RS485_RTS_AFTER_SEND
)))
1322 rs485
->flags
|= SER_RS485_RTS_ON_SEND
;
1324 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
&&
1325 rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)
1326 rs485
->flags
&= ~SER_RS485_RTS_AFTER_SEND
;
1329 * The hardware defaults to RTS logic HIGH while transfer.
1330 * Switch polarity in case RTS shall be logic HIGH
1332 * Note: UART is assumed to be active high.
1334 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
)
1335 modem
&= ~UARTMODEM_TXRTSPOL
;
1336 else if (rs485
->flags
& SER_RS485_RTS_AFTER_SEND
)
1337 modem
|= UARTMODEM_TXRTSPOL
;
1340 /* Store the new configuration */
1341 sport
->port
.rs485
= *rs485
;
1343 lpuart32_write(&sport
->port
, modem
, UARTMODIR
);
1347 static unsigned int lpuart_get_mctrl(struct uart_port
*port
)
1349 unsigned int temp
= 0;
1352 reg
= readb(port
->membase
+ UARTMODEM
);
1353 if (reg
& UARTMODEM_TXCTSE
)
1356 if (reg
& UARTMODEM_RXRTSE
)
1362 static unsigned int lpuart32_get_mctrl(struct uart_port
*port
)
1364 unsigned int temp
= 0;
1367 reg
= lpuart32_read(port
, UARTMODIR
);
1368 if (reg
& UARTMODIR_TXCTSE
)
1371 if (reg
& UARTMODIR_RXRTSE
)
1377 static void lpuart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1380 struct lpuart_port
*sport
= container_of(port
,
1381 struct lpuart_port
, port
);
1383 /* Make sure RXRTSE bit is not set when RS485 is enabled */
1384 if (!(sport
->port
.rs485
.flags
& SER_RS485_ENABLED
)) {
1385 temp
= readb(sport
->port
.membase
+ UARTMODEM
) &
1386 ~(UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1388 if (mctrl
& TIOCM_RTS
)
1389 temp
|= UARTMODEM_RXRTSE
;
1391 if (mctrl
& TIOCM_CTS
)
1392 temp
|= UARTMODEM_TXCTSE
;
1394 writeb(temp
, port
->membase
+ UARTMODEM
);
1398 static void lpuart32_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
1403 static void lpuart_break_ctl(struct uart_port
*port
, int break_state
)
1407 temp
= readb(port
->membase
+ UARTCR2
) & ~UARTCR2_SBK
;
1409 if (break_state
!= 0)
1410 temp
|= UARTCR2_SBK
;
1412 writeb(temp
, port
->membase
+ UARTCR2
);
1415 static void lpuart32_break_ctl(struct uart_port
*port
, int break_state
)
1419 temp
= lpuart32_read(port
, UARTCTRL
) & ~UARTCTRL_SBK
;
1421 if (break_state
!= 0)
1422 temp
|= UARTCTRL_SBK
;
1424 lpuart32_write(port
, temp
, UARTCTRL
);
1427 static void lpuart_setup_watermark(struct lpuart_port
*sport
)
1429 unsigned char val
, cr2
;
1430 unsigned char cr2_saved
;
1432 cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1434 cr2
&= ~(UARTCR2_TIE
| UARTCR2_TCIE
| UARTCR2_TE
|
1435 UARTCR2_RIE
| UARTCR2_RE
);
1436 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
1438 val
= readb(sport
->port
.membase
+ UARTPFIFO
);
1439 writeb(val
| UARTPFIFO_TXFE
| UARTPFIFO_RXFE
,
1440 sport
->port
.membase
+ UARTPFIFO
);
1442 /* flush Tx and Rx FIFO */
1443 writeb(UARTCFIFO_TXFLUSH
| UARTCFIFO_RXFLUSH
,
1444 sport
->port
.membase
+ UARTCFIFO
);
1446 /* explicitly clear RDRF */
1447 if (readb(sport
->port
.membase
+ UARTSR1
) & UARTSR1_RDRF
) {
1448 readb(sport
->port
.membase
+ UARTDR
);
1449 writeb(UARTSFIFO_RXUF
, sport
->port
.membase
+ UARTSFIFO
);
1452 writeb(0, sport
->port
.membase
+ UARTTWFIFO
);
1453 writeb(1, sport
->port
.membase
+ UARTRWFIFO
);
1456 writeb(cr2_saved
, sport
->port
.membase
+ UARTCR2
);
1459 static void lpuart_setup_watermark_enable(struct lpuart_port
*sport
)
1463 lpuart_setup_watermark(sport
);
1465 cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1466 cr2
|= UARTCR2_RIE
| UARTCR2_RE
| UARTCR2_TE
;
1467 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
1470 static void lpuart32_setup_watermark(struct lpuart_port
*sport
)
1472 unsigned long val
, ctrl
;
1473 unsigned long ctrl_saved
;
1475 ctrl
= lpuart32_read(&sport
->port
, UARTCTRL
);
1477 ctrl
&= ~(UARTCTRL_TIE
| UARTCTRL_TCIE
| UARTCTRL_TE
|
1478 UARTCTRL_RIE
| UARTCTRL_RE
);
1479 lpuart32_write(&sport
->port
, ctrl
, UARTCTRL
);
1481 /* enable FIFO mode */
1482 val
= lpuart32_read(&sport
->port
, UARTFIFO
);
1483 val
|= UARTFIFO_TXFE
| UARTFIFO_RXFE
;
1484 val
|= UARTFIFO_TXFLUSH
| UARTFIFO_RXFLUSH
;
1485 lpuart32_write(&sport
->port
, val
, UARTFIFO
);
1487 /* set the watermark */
1488 val
= (0x1 << UARTWATER_RXWATER_OFF
) | (0x0 << UARTWATER_TXWATER_OFF
);
1489 lpuart32_write(&sport
->port
, val
, UARTWATER
);
1492 lpuart32_write(&sport
->port
, ctrl_saved
, UARTCTRL
);
1495 static void lpuart32_setup_watermark_enable(struct lpuart_port
*sport
)
1499 lpuart32_setup_watermark(sport
);
1501 temp
= lpuart32_read(&sport
->port
, UARTCTRL
);
1502 temp
|= UARTCTRL_RE
| UARTCTRL_TE
| UARTCTRL_ILIE
;
1503 lpuart32_write(&sport
->port
, temp
, UARTCTRL
);
1506 static void rx_dma_timer_init(struct lpuart_port
*sport
)
1508 timer_setup(&sport
->lpuart_timer
, lpuart_timer_func
, 0);
1509 sport
->lpuart_timer
.expires
= jiffies
+ sport
->dma_rx_timeout
;
1510 add_timer(&sport
->lpuart_timer
);
1513 static void lpuart_request_dma(struct lpuart_port
*sport
)
1515 sport
->dma_tx_chan
= dma_request_chan(sport
->port
.dev
, "tx");
1516 if (IS_ERR(sport
->dma_tx_chan
)) {
1517 dev_info_once(sport
->port
.dev
,
1518 "DMA tx channel request failed, operating without tx DMA (%ld)\n",
1519 PTR_ERR(sport
->dma_tx_chan
));
1520 sport
->dma_tx_chan
= NULL
;
1523 sport
->dma_rx_chan
= dma_request_chan(sport
->port
.dev
, "rx");
1524 if (IS_ERR(sport
->dma_rx_chan
)) {
1525 dev_info_once(sport
->port
.dev
,
1526 "DMA rx channel request failed, operating without rx DMA (%ld)\n",
1527 PTR_ERR(sport
->dma_rx_chan
));
1528 sport
->dma_rx_chan
= NULL
;
1532 static void lpuart_tx_dma_startup(struct lpuart_port
*sport
)
1537 if (!sport
->dma_tx_chan
)
1540 ret
= lpuart_dma_tx_request(&sport
->port
);
1544 init_waitqueue_head(&sport
->dma_wait
);
1545 sport
->lpuart_dma_tx_use
= true;
1546 if (lpuart_is_32(sport
)) {
1547 uartbaud
= lpuart32_read(&sport
->port
, UARTBAUD
);
1548 lpuart32_write(&sport
->port
,
1549 uartbaud
| UARTBAUD_TDMAE
, UARTBAUD
);
1551 writeb(readb(sport
->port
.membase
+ UARTCR5
) |
1552 UARTCR5_TDMAS
, sport
->port
.membase
+ UARTCR5
);
1558 sport
->lpuart_dma_tx_use
= false;
1561 static void lpuart_rx_dma_startup(struct lpuart_port
*sport
)
1565 if (!sport
->dma_rx_chan
)
1568 ret
= lpuart_start_rx_dma(sport
);
1572 /* set Rx DMA timeout */
1573 sport
->dma_rx_timeout
= msecs_to_jiffies(DMA_RX_TIMEOUT
);
1574 if (!sport
->dma_rx_timeout
)
1575 sport
->dma_rx_timeout
= 1;
1577 sport
->lpuart_dma_rx_use
= true;
1578 rx_dma_timer_init(sport
);
1583 sport
->lpuart_dma_rx_use
= false;
1586 static int lpuart_startup(struct uart_port
*port
)
1588 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1589 unsigned long flags
;
1592 /* determine FIFO size and enable FIFO mode */
1593 temp
= readb(sport
->port
.membase
+ UARTPFIFO
);
1595 sport
->txfifo_size
= UARTFIFO_DEPTH((temp
>> UARTPFIFO_TXSIZE_OFF
) &
1596 UARTPFIFO_FIFOSIZE_MASK
);
1597 sport
->port
.fifosize
= sport
->txfifo_size
;
1599 sport
->rxfifo_size
= UARTFIFO_DEPTH((temp
>> UARTPFIFO_RXSIZE_OFF
) &
1600 UARTPFIFO_FIFOSIZE_MASK
);
1602 lpuart_request_dma(sport
);
1604 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1606 lpuart_setup_watermark_enable(sport
);
1608 lpuart_rx_dma_startup(sport
);
1609 lpuart_tx_dma_startup(sport
);
1611 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1616 static void lpuart32_configure(struct lpuart_port
*sport
)
1620 if (sport
->lpuart_dma_rx_use
) {
1621 /* RXWATER must be 0 */
1622 temp
= lpuart32_read(&sport
->port
, UARTWATER
);
1623 temp
&= ~(UARTWATER_WATER_MASK
<< UARTWATER_RXWATER_OFF
);
1624 lpuart32_write(&sport
->port
, temp
, UARTWATER
);
1626 temp
= lpuart32_read(&sport
->port
, UARTCTRL
);
1627 if (!sport
->lpuart_dma_rx_use
)
1628 temp
|= UARTCTRL_RIE
;
1629 if (!sport
->lpuart_dma_tx_use
)
1630 temp
|= UARTCTRL_TIE
;
1631 lpuart32_write(&sport
->port
, temp
, UARTCTRL
);
1634 static int lpuart32_startup(struct uart_port
*port
)
1636 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1637 unsigned long flags
;
1640 /* determine FIFO size */
1641 temp
= lpuart32_read(&sport
->port
, UARTFIFO
);
1643 sport
->txfifo_size
= UARTFIFO_DEPTH((temp
>> UARTFIFO_TXSIZE_OFF
) &
1644 UARTFIFO_FIFOSIZE_MASK
);
1645 sport
->port
.fifosize
= sport
->txfifo_size
;
1647 sport
->rxfifo_size
= UARTFIFO_DEPTH((temp
>> UARTFIFO_RXSIZE_OFF
) &
1648 UARTFIFO_FIFOSIZE_MASK
);
1651 * The LS1028A has a fixed length of 16 words. Although it supports the
1652 * RX/TXSIZE fields their encoding is different. Eg the reference manual
1653 * states 0b101 is 16 words.
1655 if (is_ls1028a_lpuart(sport
)) {
1656 sport
->rxfifo_size
= 16;
1657 sport
->txfifo_size
= 16;
1658 sport
->port
.fifosize
= sport
->txfifo_size
;
1661 lpuart_request_dma(sport
);
1663 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1665 lpuart32_setup_watermark_enable(sport
);
1667 lpuart_rx_dma_startup(sport
);
1668 lpuart_tx_dma_startup(sport
);
1670 lpuart32_configure(sport
);
1672 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1676 static void lpuart_dma_shutdown(struct lpuart_port
*sport
)
1678 if (sport
->lpuart_dma_rx_use
) {
1679 del_timer_sync(&sport
->lpuart_timer
);
1680 lpuart_dma_rx_free(&sport
->port
);
1683 if (sport
->lpuart_dma_tx_use
) {
1684 if (wait_event_interruptible(sport
->dma_wait
,
1685 !sport
->dma_tx_in_progress
) != false) {
1686 sport
->dma_tx_in_progress
= false;
1687 dmaengine_terminate_all(sport
->dma_tx_chan
);
1691 if (sport
->dma_tx_chan
)
1692 dma_release_channel(sport
->dma_tx_chan
);
1693 if (sport
->dma_rx_chan
)
1694 dma_release_channel(sport
->dma_rx_chan
);
1697 static void lpuart_shutdown(struct uart_port
*port
)
1699 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1701 unsigned long flags
;
1703 spin_lock_irqsave(&port
->lock
, flags
);
1705 /* disable Rx/Tx and interrupts */
1706 temp
= readb(port
->membase
+ UARTCR2
);
1707 temp
&= ~(UARTCR2_TE
| UARTCR2_RE
|
1708 UARTCR2_TIE
| UARTCR2_TCIE
| UARTCR2_RIE
);
1709 writeb(temp
, port
->membase
+ UARTCR2
);
1711 spin_unlock_irqrestore(&port
->lock
, flags
);
1713 lpuart_dma_shutdown(sport
);
1716 static void lpuart32_shutdown(struct uart_port
*port
)
1718 struct lpuart_port
*sport
=
1719 container_of(port
, struct lpuart_port
, port
);
1721 unsigned long flags
;
1723 spin_lock_irqsave(&port
->lock
, flags
);
1725 /* disable Rx/Tx and interrupts */
1726 temp
= lpuart32_read(port
, UARTCTRL
);
1727 temp
&= ~(UARTCTRL_TE
| UARTCTRL_RE
|
1728 UARTCTRL_TIE
| UARTCTRL_TCIE
| UARTCTRL_RIE
);
1729 lpuart32_write(port
, temp
, UARTCTRL
);
1731 spin_unlock_irqrestore(&port
->lock
, flags
);
1733 lpuart_dma_shutdown(sport
);
1737 lpuart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1738 struct ktermios
*old
)
1740 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1741 unsigned long flags
;
1742 unsigned char cr1
, old_cr1
, old_cr2
, cr3
, cr4
, bdh
, modem
;
1744 unsigned int old_csize
= old
? old
->c_cflag
& CSIZE
: CS8
;
1745 unsigned int sbr
, brfa
;
1747 cr1
= old_cr1
= readb(sport
->port
.membase
+ UARTCR1
);
1748 old_cr2
= readb(sport
->port
.membase
+ UARTCR2
);
1749 cr3
= readb(sport
->port
.membase
+ UARTCR3
);
1750 cr4
= readb(sport
->port
.membase
+ UARTCR4
);
1751 bdh
= readb(sport
->port
.membase
+ UARTBDH
);
1752 modem
= readb(sport
->port
.membase
+ UARTMODEM
);
1754 * only support CS8 and CS7, and for CS7 must enable PE.
1761 while ((termios
->c_cflag
& CSIZE
) != CS8
&&
1762 (termios
->c_cflag
& CSIZE
) != CS7
) {
1763 termios
->c_cflag
&= ~CSIZE
;
1764 termios
->c_cflag
|= old_csize
;
1768 if ((termios
->c_cflag
& CSIZE
) == CS8
||
1769 (termios
->c_cflag
& CSIZE
) == CS7
)
1770 cr1
= old_cr1
& ~UARTCR1_M
;
1772 if (termios
->c_cflag
& CMSPAR
) {
1773 if ((termios
->c_cflag
& CSIZE
) != CS8
) {
1774 termios
->c_cflag
&= ~CSIZE
;
1775 termios
->c_cflag
|= CS8
;
1781 * When auto RS-485 RTS mode is enabled,
1782 * hardware flow control need to be disabled.
1784 if (sport
->port
.rs485
.flags
& SER_RS485_ENABLED
)
1785 termios
->c_cflag
&= ~CRTSCTS
;
1787 if (termios
->c_cflag
& CRTSCTS
)
1788 modem
|= UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
;
1790 modem
&= ~(UARTMODEM_RXRTSE
| UARTMODEM_TXCTSE
);
1792 termios
->c_cflag
&= ~CSTOPB
;
1794 /* parity must be enabled when CS7 to match 8-bits format */
1795 if ((termios
->c_cflag
& CSIZE
) == CS7
)
1796 termios
->c_cflag
|= PARENB
;
1798 if (termios
->c_cflag
& PARENB
) {
1799 if (termios
->c_cflag
& CMSPAR
) {
1801 if (termios
->c_cflag
& PARODD
)
1807 if ((termios
->c_cflag
& CSIZE
) == CS8
)
1809 if (termios
->c_cflag
& PARODD
)
1818 /* ask the core to calculate the divisor */
1819 baud
= uart_get_baud_rate(port
, termios
, old
, 50, port
->uartclk
/ 16);
1822 * Need to update the Ring buffer length according to the selected
1823 * baud rate and restart Rx DMA path.
1825 * Since timer function acqures sport->port.lock, need to stop before
1826 * acquring same lock because otherwise del_timer_sync() can deadlock.
1828 if (old
&& sport
->lpuart_dma_rx_use
) {
1829 del_timer_sync(&sport
->lpuart_timer
);
1830 lpuart_dma_rx_free(&sport
->port
);
1833 spin_lock_irqsave(&sport
->port
.lock
, flags
);
1835 sport
->port
.read_status_mask
= 0;
1836 if (termios
->c_iflag
& INPCK
)
1837 sport
->port
.read_status_mask
|= UARTSR1_FE
| UARTSR1_PE
;
1838 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1839 sport
->port
.read_status_mask
|= UARTSR1_FE
;
1841 /* characters to ignore */
1842 sport
->port
.ignore_status_mask
= 0;
1843 if (termios
->c_iflag
& IGNPAR
)
1844 sport
->port
.ignore_status_mask
|= UARTSR1_PE
;
1845 if (termios
->c_iflag
& IGNBRK
) {
1846 sport
->port
.ignore_status_mask
|= UARTSR1_FE
;
1848 * if we're ignoring parity and break indicators,
1849 * ignore overruns too (for real raw support).
1851 if (termios
->c_iflag
& IGNPAR
)
1852 sport
->port
.ignore_status_mask
|= UARTSR1_OR
;
1855 /* update the per-port timeout */
1856 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1858 /* wait transmit engin complete */
1859 lpuart_wait_bit_set(&sport
->port
, UARTSR1
, UARTSR1_TC
);
1861 /* disable transmit and receive */
1862 writeb(old_cr2
& ~(UARTCR2_TE
| UARTCR2_RE
),
1863 sport
->port
.membase
+ UARTCR2
);
1865 sbr
= sport
->port
.uartclk
/ (16 * baud
);
1866 brfa
= ((sport
->port
.uartclk
- (16 * sbr
* baud
)) * 2) / baud
;
1867 bdh
&= ~UARTBDH_SBR_MASK
;
1868 bdh
|= (sbr
>> 8) & 0x1F;
1869 cr4
&= ~UARTCR4_BRFA_MASK
;
1870 brfa
&= UARTCR4_BRFA_MASK
;
1871 writeb(cr4
| brfa
, sport
->port
.membase
+ UARTCR4
);
1872 writeb(bdh
, sport
->port
.membase
+ UARTBDH
);
1873 writeb(sbr
& 0xFF, sport
->port
.membase
+ UARTBDL
);
1874 writeb(cr3
, sport
->port
.membase
+ UARTCR3
);
1875 writeb(cr1
, sport
->port
.membase
+ UARTCR1
);
1876 writeb(modem
, sport
->port
.membase
+ UARTMODEM
);
1878 /* restore control register */
1879 writeb(old_cr2
, sport
->port
.membase
+ UARTCR2
);
1881 if (old
&& sport
->lpuart_dma_rx_use
) {
1882 if (!lpuart_start_rx_dma(sport
))
1883 rx_dma_timer_init(sport
);
1885 sport
->lpuart_dma_rx_use
= false;
1888 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
1891 static void __lpuart32_serial_setbrg(struct uart_port
*port
,
1892 unsigned int baudrate
, bool use_rx_dma
,
1895 u32 sbr
, osr
, baud_diff
, tmp_osr
, tmp_sbr
, tmp_diff
, tmp
;
1896 u32 clk
= port
->uartclk
;
1899 * The idea is to use the best OSR (over-sampling rate) possible.
1900 * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
1901 * Loop to find the best OSR value possible, one that generates minimum
1902 * baud_diff iterate through the rest of the supported values of OSR.
1904 * Calculation Formula:
1905 * Baud Rate = baud clock / ((OSR+1) × SBR)
1907 baud_diff
= baudrate
;
1911 for (tmp_osr
= 4; tmp_osr
<= 32; tmp_osr
++) {
1912 /* calculate the temporary sbr value */
1913 tmp_sbr
= (clk
/ (baudrate
* tmp_osr
));
1918 * calculate the baud rate difference based on the temporary
1919 * osr and sbr values
1921 tmp_diff
= clk
/ (tmp_osr
* tmp_sbr
) - baudrate
;
1923 /* select best values between sbr and sbr+1 */
1924 tmp
= clk
/ (tmp_osr
* (tmp_sbr
+ 1));
1925 if (tmp_diff
> (baudrate
- tmp
)) {
1926 tmp_diff
= baudrate
- tmp
;
1930 if (tmp_diff
<= baud_diff
) {
1931 baud_diff
= tmp_diff
;
1940 /* handle buadrate outside acceptable rate */
1941 if (baud_diff
> ((baudrate
/ 100) * 3))
1943 "unacceptable baud rate difference of more than 3%%\n");
1945 tmp
= lpuart32_read(port
, UARTBAUD
);
1947 if ((osr
> 3) && (osr
< 8))
1948 tmp
|= UARTBAUD_BOTHEDGE
;
1950 tmp
&= ~(UARTBAUD_OSR_MASK
<< UARTBAUD_OSR_SHIFT
);
1951 tmp
|= ((osr
-1) & UARTBAUD_OSR_MASK
) << UARTBAUD_OSR_SHIFT
;
1953 tmp
&= ~UARTBAUD_SBR_MASK
;
1954 tmp
|= sbr
& UARTBAUD_SBR_MASK
;
1957 tmp
&= ~UARTBAUD_RDMAE
;
1959 tmp
&= ~UARTBAUD_TDMAE
;
1961 lpuart32_write(port
, tmp
, UARTBAUD
);
1964 static void lpuart32_serial_setbrg(struct lpuart_port
*sport
,
1965 unsigned int baudrate
)
1967 __lpuart32_serial_setbrg(&sport
->port
, baudrate
,
1968 sport
->lpuart_dma_rx_use
,
1969 sport
->lpuart_dma_tx_use
);
1974 lpuart32_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1975 struct ktermios
*old
)
1977 struct lpuart_port
*sport
= container_of(port
, struct lpuart_port
, port
);
1978 unsigned long flags
;
1979 unsigned long ctrl
, old_ctrl
, modem
;
1981 unsigned int old_csize
= old
? old
->c_cflag
& CSIZE
: CS8
;
1983 ctrl
= old_ctrl
= lpuart32_read(&sport
->port
, UARTCTRL
);
1984 modem
= lpuart32_read(&sport
->port
, UARTMODIR
);
1986 * only support CS8 and CS7, and for CS7 must enable PE.
1993 while ((termios
->c_cflag
& CSIZE
) != CS8
&&
1994 (termios
->c_cflag
& CSIZE
) != CS7
) {
1995 termios
->c_cflag
&= ~CSIZE
;
1996 termios
->c_cflag
|= old_csize
;
2000 if ((termios
->c_cflag
& CSIZE
) == CS8
||
2001 (termios
->c_cflag
& CSIZE
) == CS7
)
2002 ctrl
= old_ctrl
& ~UARTCTRL_M
;
2004 if (termios
->c_cflag
& CMSPAR
) {
2005 if ((termios
->c_cflag
& CSIZE
) != CS8
) {
2006 termios
->c_cflag
&= ~CSIZE
;
2007 termios
->c_cflag
|= CS8
;
2013 * When auto RS-485 RTS mode is enabled,
2014 * hardware flow control need to be disabled.
2016 if (sport
->port
.rs485
.flags
& SER_RS485_ENABLED
)
2017 termios
->c_cflag
&= ~CRTSCTS
;
2019 if (termios
->c_cflag
& CRTSCTS
) {
2020 modem
|= (UARTMODIR_RXRTSE
| UARTMODIR_TXCTSE
);
2022 termios
->c_cflag
&= ~CRTSCTS
;
2023 modem
&= ~(UARTMODIR_RXRTSE
| UARTMODIR_TXCTSE
);
2026 if (termios
->c_cflag
& CSTOPB
)
2027 termios
->c_cflag
&= ~CSTOPB
;
2029 /* parity must be enabled when CS7 to match 8-bits format */
2030 if ((termios
->c_cflag
& CSIZE
) == CS7
)
2031 termios
->c_cflag
|= PARENB
;
2033 if ((termios
->c_cflag
& PARENB
)) {
2034 if (termios
->c_cflag
& CMSPAR
) {
2035 ctrl
&= ~UARTCTRL_PE
;
2038 ctrl
|= UARTCTRL_PE
;
2039 if ((termios
->c_cflag
& CSIZE
) == CS8
)
2041 if (termios
->c_cflag
& PARODD
)
2042 ctrl
|= UARTCTRL_PT
;
2044 ctrl
&= ~UARTCTRL_PT
;
2047 ctrl
&= ~UARTCTRL_PE
;
2050 /* ask the core to calculate the divisor */
2051 baud
= uart_get_baud_rate(port
, termios
, old
, 50, port
->uartclk
/ 4);
2054 * Need to update the Ring buffer length according to the selected
2055 * baud rate and restart Rx DMA path.
2057 * Since timer function acqures sport->port.lock, need to stop before
2058 * acquring same lock because otherwise del_timer_sync() can deadlock.
2060 if (old
&& sport
->lpuart_dma_rx_use
) {
2061 del_timer_sync(&sport
->lpuart_timer
);
2062 lpuart_dma_rx_free(&sport
->port
);
2065 spin_lock_irqsave(&sport
->port
.lock
, flags
);
2067 sport
->port
.read_status_mask
= 0;
2068 if (termios
->c_iflag
& INPCK
)
2069 sport
->port
.read_status_mask
|= UARTSTAT_FE
| UARTSTAT_PE
;
2070 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
2071 sport
->port
.read_status_mask
|= UARTSTAT_FE
;
2073 /* characters to ignore */
2074 sport
->port
.ignore_status_mask
= 0;
2075 if (termios
->c_iflag
& IGNPAR
)
2076 sport
->port
.ignore_status_mask
|= UARTSTAT_PE
;
2077 if (termios
->c_iflag
& IGNBRK
) {
2078 sport
->port
.ignore_status_mask
|= UARTSTAT_FE
;
2080 * if we're ignoring parity and break indicators,
2081 * ignore overruns too (for real raw support).
2083 if (termios
->c_iflag
& IGNPAR
)
2084 sport
->port
.ignore_status_mask
|= UARTSTAT_OR
;
2087 /* update the per-port timeout */
2088 uart_update_timeout(port
, termios
->c_cflag
, baud
);
2090 /* wait transmit engin complete */
2091 lpuart32_wait_bit_set(&sport
->port
, UARTSTAT
, UARTSTAT_TC
);
2093 /* disable transmit and receive */
2094 lpuart32_write(&sport
->port
, old_ctrl
& ~(UARTCTRL_TE
| UARTCTRL_RE
),
2097 lpuart32_serial_setbrg(sport
, baud
);
2098 lpuart32_write(&sport
->port
, modem
, UARTMODIR
);
2099 lpuart32_write(&sport
->port
, ctrl
, UARTCTRL
);
2100 /* restore control register */
2102 if (old
&& sport
->lpuart_dma_rx_use
) {
2103 if (!lpuart_start_rx_dma(sport
))
2104 rx_dma_timer_init(sport
);
2106 sport
->lpuart_dma_rx_use
= false;
2109 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
2112 static const char *lpuart_type(struct uart_port
*port
)
2114 return "FSL_LPUART";
2117 static void lpuart_release_port(struct uart_port
*port
)
2122 static int lpuart_request_port(struct uart_port
*port
)
2127 /* configure/autoconfigure the port */
2128 static void lpuart_config_port(struct uart_port
*port
, int flags
)
2130 if (flags
& UART_CONFIG_TYPE
)
2131 port
->type
= PORT_LPUART
;
2134 static int lpuart_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
2138 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_LPUART
)
2140 if (port
->irq
!= ser
->irq
)
2142 if (ser
->io_type
!= UPIO_MEM
)
2144 if (port
->uartclk
/ 16 != ser
->baud_base
)
2146 if (port
->iobase
!= ser
->port
)
2153 static const struct uart_ops lpuart_pops
= {
2154 .tx_empty
= lpuart_tx_empty
,
2155 .set_mctrl
= lpuart_set_mctrl
,
2156 .get_mctrl
= lpuart_get_mctrl
,
2157 .stop_tx
= lpuart_stop_tx
,
2158 .start_tx
= lpuart_start_tx
,
2159 .stop_rx
= lpuart_stop_rx
,
2160 .break_ctl
= lpuart_break_ctl
,
2161 .startup
= lpuart_startup
,
2162 .shutdown
= lpuart_shutdown
,
2163 .set_termios
= lpuart_set_termios
,
2164 .type
= lpuart_type
,
2165 .request_port
= lpuart_request_port
,
2166 .release_port
= lpuart_release_port
,
2167 .config_port
= lpuart_config_port
,
2168 .verify_port
= lpuart_verify_port
,
2169 .flush_buffer
= lpuart_flush_buffer
,
2170 #if defined(CONFIG_CONSOLE_POLL)
2171 .poll_init
= lpuart_poll_init
,
2172 .poll_get_char
= lpuart_poll_get_char
,
2173 .poll_put_char
= lpuart_poll_put_char
,
2177 static const struct uart_ops lpuart32_pops
= {
2178 .tx_empty
= lpuart32_tx_empty
,
2179 .set_mctrl
= lpuart32_set_mctrl
,
2180 .get_mctrl
= lpuart32_get_mctrl
,
2181 .stop_tx
= lpuart32_stop_tx
,
2182 .start_tx
= lpuart32_start_tx
,
2183 .stop_rx
= lpuart32_stop_rx
,
2184 .break_ctl
= lpuart32_break_ctl
,
2185 .startup
= lpuart32_startup
,
2186 .shutdown
= lpuart32_shutdown
,
2187 .set_termios
= lpuart32_set_termios
,
2188 .type
= lpuart_type
,
2189 .request_port
= lpuart_request_port
,
2190 .release_port
= lpuart_release_port
,
2191 .config_port
= lpuart_config_port
,
2192 .verify_port
= lpuart_verify_port
,
2193 .flush_buffer
= lpuart_flush_buffer
,
2194 #if defined(CONFIG_CONSOLE_POLL)
2195 .poll_init
= lpuart32_poll_init
,
2196 .poll_get_char
= lpuart32_poll_get_char
,
2197 .poll_put_char
= lpuart32_poll_put_char
,
2201 static struct lpuart_port
*lpuart_ports
[UART_NR
];
2203 #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
2204 static void lpuart_console_putchar(struct uart_port
*port
, int ch
)
2206 lpuart_wait_bit_set(port
, UARTSR1
, UARTSR1_TDRE
);
2207 writeb(ch
, port
->membase
+ UARTDR
);
2210 static void lpuart32_console_putchar(struct uart_port
*port
, int ch
)
2212 lpuart32_wait_bit_set(port
, UARTSTAT
, UARTSTAT_TDRE
);
2213 lpuart32_write(port
, ch
, UARTDATA
);
2217 lpuart_console_write(struct console
*co
, const char *s
, unsigned int count
)
2219 struct lpuart_port
*sport
= lpuart_ports
[co
->index
];
2220 unsigned char old_cr2
, cr2
;
2221 unsigned long flags
;
2224 if (sport
->port
.sysrq
|| oops_in_progress
)
2225 locked
= spin_trylock_irqsave(&sport
->port
.lock
, flags
);
2227 spin_lock_irqsave(&sport
->port
.lock
, flags
);
2229 /* first save CR2 and then disable interrupts */
2230 cr2
= old_cr2
= readb(sport
->port
.membase
+ UARTCR2
);
2231 cr2
|= UARTCR2_TE
| UARTCR2_RE
;
2232 cr2
&= ~(UARTCR2_TIE
| UARTCR2_TCIE
| UARTCR2_RIE
);
2233 writeb(cr2
, sport
->port
.membase
+ UARTCR2
);
2235 uart_console_write(&sport
->port
, s
, count
, lpuart_console_putchar
);
2237 /* wait for transmitter finish complete and restore CR2 */
2238 lpuart_wait_bit_set(&sport
->port
, UARTSR1
, UARTSR1_TC
);
2240 writeb(old_cr2
, sport
->port
.membase
+ UARTCR2
);
2243 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
2247 lpuart32_console_write(struct console
*co
, const char *s
, unsigned int count
)
2249 struct lpuart_port
*sport
= lpuart_ports
[co
->index
];
2250 unsigned long old_cr
, cr
;
2251 unsigned long flags
;
2254 if (sport
->port
.sysrq
|| oops_in_progress
)
2255 locked
= spin_trylock_irqsave(&sport
->port
.lock
, flags
);
2257 spin_lock_irqsave(&sport
->port
.lock
, flags
);
2259 /* first save CR2 and then disable interrupts */
2260 cr
= old_cr
= lpuart32_read(&sport
->port
, UARTCTRL
);
2261 cr
|= UARTCTRL_TE
| UARTCTRL_RE
;
2262 cr
&= ~(UARTCTRL_TIE
| UARTCTRL_TCIE
| UARTCTRL_RIE
);
2263 lpuart32_write(&sport
->port
, cr
, UARTCTRL
);
2265 uart_console_write(&sport
->port
, s
, count
, lpuart32_console_putchar
);
2267 /* wait for transmitter finish complete and restore CR2 */
2268 lpuart32_wait_bit_set(&sport
->port
, UARTSTAT
, UARTSTAT_TC
);
2270 lpuart32_write(&sport
->port
, old_cr
, UARTCTRL
);
2273 spin_unlock_irqrestore(&sport
->port
.lock
, flags
);
2277 * if the port was already initialised (eg, by a boot loader),
2278 * try to determine the current setup.
2281 lpuart_console_get_options(struct lpuart_port
*sport
, int *baud
,
2282 int *parity
, int *bits
)
2284 unsigned char cr
, bdh
, bdl
, brfa
;
2285 unsigned int sbr
, uartclk
, baud_raw
;
2287 cr
= readb(sport
->port
.membase
+ UARTCR2
);
2288 cr
&= UARTCR2_TE
| UARTCR2_RE
;
2292 /* ok, the port was enabled */
2294 cr
= readb(sport
->port
.membase
+ UARTCR1
);
2297 if (cr
& UARTCR1_PE
) {
2298 if (cr
& UARTCR1_PT
)
2309 bdh
= readb(sport
->port
.membase
+ UARTBDH
);
2310 bdh
&= UARTBDH_SBR_MASK
;
2311 bdl
= readb(sport
->port
.membase
+ UARTBDL
);
2315 brfa
= readb(sport
->port
.membase
+ UARTCR4
);
2316 brfa
&= UARTCR4_BRFA_MASK
;
2318 uartclk
= lpuart_get_baud_clk_rate(sport
);
2320 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2322 baud_raw
= uartclk
/ (16 * (sbr
+ brfa
/ 32));
2324 if (*baud
!= baud_raw
)
2325 dev_info(sport
->port
.dev
, "Serial: Console lpuart rounded baud rate"
2326 "from %d to %d\n", baud_raw
, *baud
);
2330 lpuart32_console_get_options(struct lpuart_port
*sport
, int *baud
,
2331 int *parity
, int *bits
)
2333 unsigned long cr
, bd
;
2334 unsigned int sbr
, uartclk
, baud_raw
;
2336 cr
= lpuart32_read(&sport
->port
, UARTCTRL
);
2337 cr
&= UARTCTRL_TE
| UARTCTRL_RE
;
2341 /* ok, the port was enabled */
2343 cr
= lpuart32_read(&sport
->port
, UARTCTRL
);
2346 if (cr
& UARTCTRL_PE
) {
2347 if (cr
& UARTCTRL_PT
)
2353 if (cr
& UARTCTRL_M
)
2358 bd
= lpuart32_read(&sport
->port
, UARTBAUD
);
2359 bd
&= UARTBAUD_SBR_MASK
;
2361 uartclk
= lpuart_get_baud_clk_rate(sport
);
2363 * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
2365 baud_raw
= uartclk
/ (16 * sbr
);
2367 if (*baud
!= baud_raw
)
2368 dev_info(sport
->port
.dev
, "Serial: Console lpuart rounded baud rate"
2369 "from %d to %d\n", baud_raw
, *baud
);
2372 static int __init
lpuart_console_setup(struct console
*co
, char *options
)
2374 struct lpuart_port
*sport
;
2381 * check whether an invalid uart number has been specified, and
2382 * if so, search for the first available port that does have
2385 if (co
->index
== -1 || co
->index
>= ARRAY_SIZE(lpuart_ports
))
2388 sport
= lpuart_ports
[co
->index
];
2393 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2395 if (lpuart_is_32(sport
))
2396 lpuart32_console_get_options(sport
, &baud
, &parity
, &bits
);
2398 lpuart_console_get_options(sport
, &baud
, &parity
, &bits
);
2400 if (lpuart_is_32(sport
))
2401 lpuart32_setup_watermark(sport
);
2403 lpuart_setup_watermark(sport
);
2405 return uart_set_options(&sport
->port
, co
, baud
, parity
, bits
, flow
);
2408 static struct uart_driver lpuart_reg
;
2409 static struct console lpuart_console
= {
2411 .write
= lpuart_console_write
,
2412 .device
= uart_console_device
,
2413 .setup
= lpuart_console_setup
,
2414 .flags
= CON_PRINTBUFFER
,
2416 .data
= &lpuart_reg
,
2419 static struct console lpuart32_console
= {
2421 .write
= lpuart32_console_write
,
2422 .device
= uart_console_device
,
2423 .setup
= lpuart_console_setup
,
2424 .flags
= CON_PRINTBUFFER
,
2426 .data
= &lpuart_reg
,
2429 static void lpuart_early_write(struct console
*con
, const char *s
, unsigned n
)
2431 struct earlycon_device
*dev
= con
->data
;
2433 uart_console_write(&dev
->port
, s
, n
, lpuart_console_putchar
);
2436 static void lpuart32_early_write(struct console
*con
, const char *s
, unsigned n
)
2438 struct earlycon_device
*dev
= con
->data
;
2440 uart_console_write(&dev
->port
, s
, n
, lpuart32_console_putchar
);
2443 static int __init
lpuart_early_console_setup(struct earlycon_device
*device
,
2446 if (!device
->port
.membase
)
2449 device
->con
->write
= lpuart_early_write
;
2453 static int __init
lpuart32_early_console_setup(struct earlycon_device
*device
,
2456 if (!device
->port
.membase
)
2459 if (device
->port
.iotype
!= UPIO_MEM32
)
2460 device
->port
.iotype
= UPIO_MEM32BE
;
2462 device
->con
->write
= lpuart32_early_write
;
2466 static int __init
ls1028a_early_console_setup(struct earlycon_device
*device
,
2471 if (!device
->port
.membase
)
2474 device
->port
.iotype
= UPIO_MEM32
;
2475 device
->con
->write
= lpuart32_early_write
;
2477 /* set the baudrate */
2478 if (device
->port
.uartclk
&& device
->baud
)
2479 __lpuart32_serial_setbrg(&device
->port
, device
->baud
,
2482 /* enable transmitter */
2483 cr
= lpuart32_read(&device
->port
, UARTCTRL
);
2485 lpuart32_write(&device
->port
, cr
, UARTCTRL
);
2490 static int __init
lpuart32_imx_early_console_setup(struct earlycon_device
*device
,
2493 if (!device
->port
.membase
)
2496 device
->port
.iotype
= UPIO_MEM32
;
2497 device
->port
.membase
+= IMX_REG_OFF
;
2498 device
->con
->write
= lpuart32_early_write
;
2502 OF_EARLYCON_DECLARE(lpuart
, "fsl,vf610-lpuart", lpuart_early_console_setup
);
2503 OF_EARLYCON_DECLARE(lpuart32
, "fsl,ls1021a-lpuart", lpuart32_early_console_setup
);
2504 OF_EARLYCON_DECLARE(lpuart32
, "fsl,ls1028a-lpuart", ls1028a_early_console_setup
);
2505 OF_EARLYCON_DECLARE(lpuart32
, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup
);
2506 EARLYCON_DECLARE(lpuart
, lpuart_early_console_setup
);
2507 EARLYCON_DECLARE(lpuart32
, lpuart32_early_console_setup
);
2509 #define LPUART_CONSOLE (&lpuart_console)
2510 #define LPUART32_CONSOLE (&lpuart32_console)
2512 #define LPUART_CONSOLE NULL
2513 #define LPUART32_CONSOLE NULL
2516 static struct uart_driver lpuart_reg
= {
2517 .owner
= THIS_MODULE
,
2518 .driver_name
= DRIVER_NAME
,
2519 .dev_name
= DEV_NAME
,
2520 .nr
= ARRAY_SIZE(lpuart_ports
),
2521 .cons
= LPUART_CONSOLE
,
2524 static int lpuart_probe(struct platform_device
*pdev
)
2526 const struct of_device_id
*of_id
= of_match_device(lpuart_dt_ids
,
2528 const struct lpuart_soc_data
*sdata
= of_id
->data
;
2529 struct device_node
*np
= pdev
->dev
.of_node
;
2530 struct lpuart_port
*sport
;
2531 struct resource
*res
;
2534 sport
= devm_kzalloc(&pdev
->dev
, sizeof(*sport
), GFP_KERNEL
);
2538 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2539 sport
->port
.membase
= devm_ioremap_resource(&pdev
->dev
, res
);
2540 if (IS_ERR(sport
->port
.membase
))
2541 return PTR_ERR(sport
->port
.membase
);
2543 sport
->port
.membase
+= sdata
->reg_off
;
2544 sport
->port
.mapbase
= res
->start
;
2545 sport
->port
.dev
= &pdev
->dev
;
2546 sport
->port
.type
= PORT_LPUART
;
2547 sport
->devtype
= sdata
->devtype
;
2548 ret
= platform_get_irq(pdev
, 0);
2551 sport
->port
.irq
= ret
;
2552 sport
->port
.iotype
= sdata
->iotype
;
2553 if (lpuart_is_32(sport
))
2554 sport
->port
.ops
= &lpuart32_pops
;
2556 sport
->port
.ops
= &lpuart_pops
;
2557 sport
->port
.has_sysrq
= IS_ENABLED(CONFIG_SERIAL_FSL_LPUART_CONSOLE
);
2558 sport
->port
.flags
= UPF_BOOT_AUTOCONF
;
2560 if (lpuart_is_32(sport
))
2561 sport
->port
.rs485_config
= lpuart32_config_rs485
;
2563 sport
->port
.rs485_config
= lpuart_config_rs485
;
2565 sport
->ipg_clk
= devm_clk_get(&pdev
->dev
, "ipg");
2566 if (IS_ERR(sport
->ipg_clk
)) {
2567 ret
= PTR_ERR(sport
->ipg_clk
);
2568 dev_err(&pdev
->dev
, "failed to get uart ipg clk: %d\n", ret
);
2572 sport
->baud_clk
= NULL
;
2573 if (is_imx8qxp_lpuart(sport
)) {
2574 sport
->baud_clk
= devm_clk_get(&pdev
->dev
, "baud");
2575 if (IS_ERR(sport
->baud_clk
)) {
2576 ret
= PTR_ERR(sport
->baud_clk
);
2577 dev_err(&pdev
->dev
, "failed to get uart baud clk: %d\n", ret
);
2582 ret
= of_alias_get_id(np
, "serial");
2584 ret
= ida_simple_get(&fsl_lpuart_ida
, 0, UART_NR
, GFP_KERNEL
);
2586 dev_err(&pdev
->dev
, "port line is full, add device failed\n");
2589 sport
->id_allocated
= true;
2591 if (ret
>= ARRAY_SIZE(lpuart_ports
)) {
2592 dev_err(&pdev
->dev
, "serial%d out of range\n", ret
);
2594 goto failed_out_of_range
;
2596 sport
->port
.line
= ret
;
2598 ret
= lpuart_enable_clks(sport
);
2600 goto failed_clock_enable
;
2601 sport
->port
.uartclk
= lpuart_get_baud_clk_rate(sport
);
2603 lpuart_ports
[sport
->port
.line
] = sport
;
2605 platform_set_drvdata(pdev
, &sport
->port
);
2607 if (lpuart_is_32(sport
)) {
2608 lpuart_reg
.cons
= LPUART32_CONSOLE
;
2609 ret
= devm_request_irq(&pdev
->dev
, sport
->port
.irq
, lpuart32_int
, 0,
2610 DRIVER_NAME
, sport
);
2612 lpuart_reg
.cons
= LPUART_CONSOLE
;
2613 ret
= devm_request_irq(&pdev
->dev
, sport
->port
.irq
, lpuart_int
, 0,
2614 DRIVER_NAME
, sport
);
2618 goto failed_irq_request
;
2620 ret
= uart_add_one_port(&lpuart_reg
, &sport
->port
);
2622 goto failed_attach_port
;
2624 uart_get_rs485_mode(&pdev
->dev
, &sport
->port
.rs485
);
2626 if (sport
->port
.rs485
.flags
& SER_RS485_RX_DURING_TX
)
2627 dev_err(&pdev
->dev
, "driver doesn't support RX during TX\n");
2629 if (sport
->port
.rs485
.delay_rts_before_send
||
2630 sport
->port
.rs485
.delay_rts_after_send
)
2631 dev_err(&pdev
->dev
, "driver doesn't support RTS delays\n");
2633 sport
->port
.rs485_config(&sport
->port
, &sport
->port
.rs485
);
2639 lpuart_disable_clks(sport
);
2640 failed_clock_enable
:
2641 failed_out_of_range
:
2642 if (sport
->id_allocated
)
2643 ida_simple_remove(&fsl_lpuart_ida
, sport
->port
.line
);
2647 static int lpuart_remove(struct platform_device
*pdev
)
2649 struct lpuart_port
*sport
= platform_get_drvdata(pdev
);
2651 uart_remove_one_port(&lpuart_reg
, &sport
->port
);
2653 if (sport
->id_allocated
)
2654 ida_simple_remove(&fsl_lpuart_ida
, sport
->port
.line
);
2656 lpuart_disable_clks(sport
);
2658 if (sport
->dma_tx_chan
)
2659 dma_release_channel(sport
->dma_tx_chan
);
2661 if (sport
->dma_rx_chan
)
2662 dma_release_channel(sport
->dma_rx_chan
);
2667 #ifdef CONFIG_PM_SLEEP
2668 static int lpuart_suspend(struct device
*dev
)
2670 struct lpuart_port
*sport
= dev_get_drvdata(dev
);
2674 if (lpuart_is_32(sport
)) {
2675 /* disable Rx/Tx and interrupts */
2676 temp
= lpuart32_read(&sport
->port
, UARTCTRL
);
2677 temp
&= ~(UARTCTRL_TE
| UARTCTRL_TIE
| UARTCTRL_TCIE
);
2678 lpuart32_write(&sport
->port
, temp
, UARTCTRL
);
2680 /* disable Rx/Tx and interrupts */
2681 temp
= readb(sport
->port
.membase
+ UARTCR2
);
2682 temp
&= ~(UARTCR2_TE
| UARTCR2_TIE
| UARTCR2_TCIE
);
2683 writeb(temp
, sport
->port
.membase
+ UARTCR2
);
2686 uart_suspend_port(&lpuart_reg
, &sport
->port
);
2688 /* uart_suspend_port() might set wakeup flag */
2689 irq_wake
= irqd_is_wakeup_set(irq_get_irq_data(sport
->port
.irq
));
2691 if (sport
->lpuart_dma_rx_use
) {
2693 * EDMA driver during suspend will forcefully release any
2694 * non-idle DMA channels. If port wakeup is enabled or if port
2695 * is console port or 'no_console_suspend' is set the Rx DMA
2696 * cannot resume as as expected, hence gracefully release the
2697 * Rx DMA path before suspend and start Rx DMA path on resume.
2700 del_timer_sync(&sport
->lpuart_timer
);
2701 lpuart_dma_rx_free(&sport
->port
);
2704 /* Disable Rx DMA to use UART port as wakeup source */
2705 if (lpuart_is_32(sport
)) {
2706 temp
= lpuart32_read(&sport
->port
, UARTBAUD
);
2707 lpuart32_write(&sport
->port
, temp
& ~UARTBAUD_RDMAE
,
2710 writeb(readb(sport
->port
.membase
+ UARTCR5
) &
2711 ~UARTCR5_RDMAS
, sport
->port
.membase
+ UARTCR5
);
2715 if (sport
->lpuart_dma_tx_use
) {
2716 sport
->dma_tx_in_progress
= false;
2717 dmaengine_terminate_all(sport
->dma_tx_chan
);
2720 if (sport
->port
.suspended
&& !irq_wake
)
2721 lpuart_disable_clks(sport
);
2726 static int lpuart_resume(struct device
*dev
)
2728 struct lpuart_port
*sport
= dev_get_drvdata(dev
);
2729 bool irq_wake
= irqd_is_wakeup_set(irq_get_irq_data(sport
->port
.irq
));
2731 if (sport
->port
.suspended
&& !irq_wake
)
2732 lpuart_enable_clks(sport
);
2734 if (lpuart_is_32(sport
))
2735 lpuart32_setup_watermark_enable(sport
);
2737 lpuart_setup_watermark_enable(sport
);
2739 if (sport
->lpuart_dma_rx_use
) {
2741 if (!lpuart_start_rx_dma(sport
))
2742 rx_dma_timer_init(sport
);
2744 sport
->lpuart_dma_rx_use
= false;
2748 lpuart_tx_dma_startup(sport
);
2750 if (lpuart_is_32(sport
))
2751 lpuart32_configure(sport
);
2753 uart_resume_port(&lpuart_reg
, &sport
->port
);
2759 static SIMPLE_DEV_PM_OPS(lpuart_pm_ops
, lpuart_suspend
, lpuart_resume
);
2761 static struct platform_driver lpuart_driver
= {
2762 .probe
= lpuart_probe
,
2763 .remove
= lpuart_remove
,
2765 .name
= "fsl-lpuart",
2766 .of_match_table
= lpuart_dt_ids
,
2767 .pm
= &lpuart_pm_ops
,
2771 static int __init
lpuart_serial_init(void)
2773 int ret
= uart_register_driver(&lpuart_reg
);
2778 ret
= platform_driver_register(&lpuart_driver
);
2780 uart_unregister_driver(&lpuart_reg
);
2785 static void __exit
lpuart_serial_exit(void)
2787 ida_destroy(&fsl_lpuart_ida
);
2788 platform_driver_unregister(&lpuart_driver
);
2789 uart_unregister_driver(&lpuart_reg
);
2792 module_init(lpuart_serial_init
);
2793 module_exit(lpuart_serial_exit
);
2795 MODULE_DESCRIPTION("Freescale lpuart serial port driver");
2796 MODULE_LICENSE("GPL v2");