1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics SA 2017
5 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
6 * Gerald Baeza <gerald.baeza@st.com>
8 * Inspired by st-asc.c from STMicroelectronics (c)
11 #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #include <linux/clk.h>
16 #include <linux/console.h>
17 #include <linux/delay.h>
18 #include <linux/dma-direction.h>
19 #include <linux/dmaengine.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/iopoll.h>
23 #include <linux/irq.h>
24 #include <linux/module.h>
26 #include <linux/of_platform.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/pm_wakeirq.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial.h>
32 #include <linux/spinlock.h>
33 #include <linux/sysrq.h>
34 #include <linux/tty_flip.h>
35 #include <linux/tty.h>
37 #include "stm32-usart.h"
39 static void stm32_stop_tx(struct uart_port
*port
);
40 static void stm32_transmit_chars(struct uart_port
*port
);
42 static inline struct stm32_port
*to_stm32_port(struct uart_port
*port
)
44 return container_of(port
, struct stm32_port
, port
);
47 static void stm32_set_bits(struct uart_port
*port
, u32 reg
, u32 bits
)
51 val
= readl_relaxed(port
->membase
+ reg
);
53 writel_relaxed(val
, port
->membase
+ reg
);
56 static void stm32_clr_bits(struct uart_port
*port
, u32 reg
, u32 bits
)
60 val
= readl_relaxed(port
->membase
+ reg
);
62 writel_relaxed(val
, port
->membase
+ reg
);
65 static int stm32_pending_rx(struct uart_port
*port
, u32
*sr
, int *last_res
,
68 struct stm32_port
*stm32_port
= to_stm32_port(port
);
69 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
70 enum dma_status status
;
71 struct dma_tx_state state
;
73 *sr
= readl_relaxed(port
->membase
+ ofs
->isr
);
75 if (threaded
&& stm32_port
->rx_ch
) {
76 status
= dmaengine_tx_status(stm32_port
->rx_ch
,
77 stm32_port
->rx_ch
->cookie
,
79 if ((status
== DMA_IN_PROGRESS
) &&
80 (*last_res
!= state
.residue
))
84 } else if (*sr
& USART_SR_RXNE
) {
91 stm32_get_char(struct uart_port
*port
, u32
*sr
, int *last_res
)
93 struct stm32_port
*stm32_port
= to_stm32_port(port
);
94 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
97 if (stm32_port
->rx_ch
) {
98 c
= stm32_port
->rx_buf
[RX_BUF_L
- (*last_res
)--];
100 *last_res
= RX_BUF_L
;
103 return readl_relaxed(port
->membase
+ ofs
->rdr
);
107 static void stm32_receive_chars(struct uart_port
*port
, bool threaded
)
109 struct tty_port
*tport
= &port
->state
->port
;
110 struct stm32_port
*stm32_port
= to_stm32_port(port
);
111 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
116 if (irqd_is_wakeup_set(irq_get_irq_data(port
->irq
)))
117 pm_wakeup_event(tport
->tty
->dev
, 0);
119 while (stm32_pending_rx(port
, &sr
, &stm32_port
->last_res
, threaded
)) {
120 sr
|= USART_SR_DUMMY_RX
;
124 * Status bits has to be cleared before reading the RDR:
125 * In FIFO mode, reading the RDR will pop the next data
126 * (if any) along with its status bits into the SR.
127 * Not doing so leads to misalignement between RDR and SR,
128 * and clear status bits of the next rx data.
130 * Clear errors flags for stm32f7 and stm32h7 compatible
131 * devices. On stm32f4 compatible devices, the error bit is
132 * cleared by the sequence [read SR - read DR].
134 if ((sr
& USART_SR_ERR_MASK
) && ofs
->icr
!= UNDEF_REG
)
135 stm32_clr_bits(port
, ofs
->icr
, USART_ICR_ORECF
|
136 USART_ICR_PECF
| USART_ICR_FECF
);
138 c
= stm32_get_char(port
, &sr
, &stm32_port
->last_res
);
140 if (sr
& USART_SR_ERR_MASK
) {
141 if (sr
& USART_SR_ORE
) {
142 port
->icount
.overrun
++;
143 } else if (sr
& USART_SR_PE
) {
144 port
->icount
.parity
++;
145 } else if (sr
& USART_SR_FE
) {
146 /* Break detection if character is null */
149 if (uart_handle_break(port
))
152 port
->icount
.frame
++;
156 sr
&= port
->read_status_mask
;
158 if (sr
& USART_SR_PE
) {
160 } else if (sr
& USART_SR_FE
) {
168 if (uart_handle_sysrq_char(port
, c
))
170 uart_insert_char(port
, sr
, USART_SR_ORE
, c
, flag
);
173 spin_unlock(&port
->lock
);
174 tty_flip_buffer_push(tport
);
175 spin_lock(&port
->lock
);
178 static void stm32_tx_dma_complete(void *arg
)
180 struct uart_port
*port
= arg
;
181 struct stm32_port
*stm32port
= to_stm32_port(port
);
182 struct stm32_usart_offsets
*ofs
= &stm32port
->info
->ofs
;
186 ret
= readl_relaxed_poll_timeout_atomic(port
->membase
+ ofs
->isr
,
192 dev_err(port
->dev
, "terminal count not set\n");
194 if (ofs
->icr
== UNDEF_REG
)
195 stm32_clr_bits(port
, ofs
->isr
, USART_SR_TC
);
197 stm32_set_bits(port
, ofs
->icr
, USART_CR_TC
);
199 stm32_clr_bits(port
, ofs
->cr3
, USART_CR3_DMAT
);
200 stm32port
->tx_dma_busy
= false;
202 /* Let's see if we have pending data to send */
203 stm32_transmit_chars(port
);
206 static void stm32_transmit_chars_pio(struct uart_port
*port
)
208 struct stm32_port
*stm32_port
= to_stm32_port(port
);
209 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
210 struct circ_buf
*xmit
= &port
->state
->xmit
;
214 if (stm32_port
->tx_dma_busy
) {
215 stm32_clr_bits(port
, ofs
->cr3
, USART_CR3_DMAT
);
216 stm32_port
->tx_dma_busy
= false;
219 ret
= readl_relaxed_poll_timeout_atomic(port
->membase
+ ofs
->isr
,
221 (isr
& USART_SR_TXE
),
225 dev_err(port
->dev
, "tx empty not set\n");
227 stm32_set_bits(port
, ofs
->cr1
, USART_CR1_TXEIE
);
229 writel_relaxed(xmit
->buf
[xmit
->tail
], port
->membase
+ ofs
->tdr
);
230 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
234 static void stm32_transmit_chars_dma(struct uart_port
*port
)
236 struct stm32_port
*stm32port
= to_stm32_port(port
);
237 struct stm32_usart_offsets
*ofs
= &stm32port
->info
->ofs
;
238 struct circ_buf
*xmit
= &port
->state
->xmit
;
239 struct dma_async_tx_descriptor
*desc
= NULL
;
241 unsigned int count
, i
;
243 if (stm32port
->tx_dma_busy
)
246 stm32port
->tx_dma_busy
= true;
248 count
= uart_circ_chars_pending(xmit
);
250 if (count
> TX_BUF_L
)
253 if (xmit
->tail
< xmit
->head
) {
254 memcpy(&stm32port
->tx_buf
[0], &xmit
->buf
[xmit
->tail
], count
);
256 size_t one
= UART_XMIT_SIZE
- xmit
->tail
;
263 memcpy(&stm32port
->tx_buf
[0], &xmit
->buf
[xmit
->tail
], one
);
265 memcpy(&stm32port
->tx_buf
[one
], &xmit
->buf
[0], two
);
268 desc
= dmaengine_prep_slave_single(stm32port
->tx_ch
,
269 stm32port
->tx_dma_buf
,
275 for (i
= count
; i
> 0; i
--)
276 stm32_transmit_chars_pio(port
);
280 desc
->callback
= stm32_tx_dma_complete
;
281 desc
->callback_param
= port
;
283 /* Push current DMA TX transaction in the pending queue */
284 cookie
= dmaengine_submit(desc
);
286 /* Issue pending DMA TX requests */
287 dma_async_issue_pending(stm32port
->tx_ch
);
289 stm32_clr_bits(port
, ofs
->isr
, USART_SR_TC
);
290 stm32_set_bits(port
, ofs
->cr3
, USART_CR3_DMAT
);
292 xmit
->tail
= (xmit
->tail
+ count
) & (UART_XMIT_SIZE
- 1);
293 port
->icount
.tx
+= count
;
296 static void stm32_transmit_chars(struct uart_port
*port
)
298 struct stm32_port
*stm32_port
= to_stm32_port(port
);
299 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
300 struct circ_buf
*xmit
= &port
->state
->xmit
;
303 if (stm32_port
->tx_dma_busy
)
304 stm32_clr_bits(port
, ofs
->cr3
, USART_CR3_DMAT
);
305 writel_relaxed(port
->x_char
, port
->membase
+ ofs
->tdr
);
308 if (stm32_port
->tx_dma_busy
)
309 stm32_set_bits(port
, ofs
->cr3
, USART_CR3_DMAT
);
313 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
314 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_TXEIE
);
318 if (stm32_port
->tx_ch
)
319 stm32_transmit_chars_dma(port
);
321 stm32_transmit_chars_pio(port
);
323 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
324 uart_write_wakeup(port
);
326 if (uart_circ_empty(xmit
))
327 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_TXEIE
);
330 static irqreturn_t
stm32_interrupt(int irq
, void *ptr
)
332 struct uart_port
*port
= ptr
;
333 struct stm32_port
*stm32_port
= to_stm32_port(port
);
334 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
337 spin_lock(&port
->lock
);
339 sr
= readl_relaxed(port
->membase
+ ofs
->isr
);
341 if ((sr
& USART_SR_WUF
) && (ofs
->icr
!= UNDEF_REG
))
342 writel_relaxed(USART_ICR_WUCF
,
343 port
->membase
+ ofs
->icr
);
345 if ((sr
& USART_SR_RXNE
) && !(stm32_port
->rx_ch
))
346 stm32_receive_chars(port
, false);
348 if ((sr
& USART_SR_TXE
) && !(stm32_port
->tx_ch
))
349 stm32_transmit_chars(port
);
351 spin_unlock(&port
->lock
);
353 if (stm32_port
->rx_ch
)
354 return IRQ_WAKE_THREAD
;
359 static irqreturn_t
stm32_threaded_interrupt(int irq
, void *ptr
)
361 struct uart_port
*port
= ptr
;
362 struct stm32_port
*stm32_port
= to_stm32_port(port
);
364 spin_lock(&port
->lock
);
366 if (stm32_port
->rx_ch
)
367 stm32_receive_chars(port
, true);
369 spin_unlock(&port
->lock
);
374 static unsigned int stm32_tx_empty(struct uart_port
*port
)
376 struct stm32_port
*stm32_port
= to_stm32_port(port
);
377 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
379 return readl_relaxed(port
->membase
+ ofs
->isr
) & USART_SR_TXE
;
382 static void stm32_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
384 struct stm32_port
*stm32_port
= to_stm32_port(port
);
385 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
387 if ((mctrl
& TIOCM_RTS
) && (port
->status
& UPSTAT_AUTORTS
))
388 stm32_set_bits(port
, ofs
->cr3
, USART_CR3_RTSE
);
390 stm32_clr_bits(port
, ofs
->cr3
, USART_CR3_RTSE
);
393 static unsigned int stm32_get_mctrl(struct uart_port
*port
)
395 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
396 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
400 static void stm32_stop_tx(struct uart_port
*port
)
402 struct stm32_port
*stm32_port
= to_stm32_port(port
);
403 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
405 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_TXEIE
);
408 /* There are probably characters waiting to be transmitted. */
409 static void stm32_start_tx(struct uart_port
*port
)
411 struct circ_buf
*xmit
= &port
->state
->xmit
;
413 if (uart_circ_empty(xmit
))
416 stm32_transmit_chars(port
);
419 /* Throttle the remote when input buffer is about to overflow. */
420 static void stm32_throttle(struct uart_port
*port
)
422 struct stm32_port
*stm32_port
= to_stm32_port(port
);
423 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
426 spin_lock_irqsave(&port
->lock
, flags
);
427 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_RXNEIE
);
428 spin_unlock_irqrestore(&port
->lock
, flags
);
431 /* Unthrottle the remote, the input buffer can now accept data. */
432 static void stm32_unthrottle(struct uart_port
*port
)
434 struct stm32_port
*stm32_port
= to_stm32_port(port
);
435 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
438 spin_lock_irqsave(&port
->lock
, flags
);
439 stm32_set_bits(port
, ofs
->cr1
, USART_CR1_RXNEIE
);
440 spin_unlock_irqrestore(&port
->lock
, flags
);
444 static void stm32_stop_rx(struct uart_port
*port
)
446 struct stm32_port
*stm32_port
= to_stm32_port(port
);
447 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
449 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_RXNEIE
);
452 /* Handle breaks - ignored by us */
453 static void stm32_break_ctl(struct uart_port
*port
, int break_state
)
457 static int stm32_startup(struct uart_port
*port
)
459 struct stm32_port
*stm32_port
= to_stm32_port(port
);
460 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
461 struct stm32_usart_config
*cfg
= &stm32_port
->info
->cfg
;
462 const char *name
= to_platform_device(port
->dev
)->name
;
466 ret
= request_threaded_irq(port
->irq
, stm32_interrupt
,
467 stm32_threaded_interrupt
,
468 IRQF_NO_SUSPEND
, name
, port
);
472 if (cfg
->has_wakeup
&& stm32_port
->wakeirq
>= 0) {
473 ret
= dev_pm_set_dedicated_wake_irq(port
->dev
,
474 stm32_port
->wakeirq
);
476 free_irq(port
->irq
, port
);
481 val
= USART_CR1_RXNEIE
| USART_CR1_TE
| USART_CR1_RE
;
482 if (stm32_port
->fifoen
)
483 val
|= USART_CR1_FIFOEN
;
484 stm32_set_bits(port
, ofs
->cr1
, val
);
489 static void stm32_shutdown(struct uart_port
*port
)
491 struct stm32_port
*stm32_port
= to_stm32_port(port
);
492 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
493 struct stm32_usart_config
*cfg
= &stm32_port
->info
->cfg
;
496 val
= USART_CR1_TXEIE
| USART_CR1_RXNEIE
| USART_CR1_TE
| USART_CR1_RE
;
497 val
|= BIT(cfg
->uart_enable_bit
);
498 if (stm32_port
->fifoen
)
499 val
|= USART_CR1_FIFOEN
;
500 stm32_clr_bits(port
, ofs
->cr1
, val
);
502 dev_pm_clear_wake_irq(port
->dev
);
503 free_irq(port
->irq
, port
);
506 static void stm32_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
507 struct ktermios
*old
)
509 struct stm32_port
*stm32_port
= to_stm32_port(port
);
510 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
511 struct stm32_usart_config
*cfg
= &stm32_port
->info
->cfg
;
513 u32 usartdiv
, mantissa
, fraction
, oversampling
;
514 tcflag_t cflag
= termios
->c_cflag
;
518 if (!stm32_port
->hw_flow_control
)
521 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 8);
523 spin_lock_irqsave(&port
->lock
, flags
);
525 /* Stop serial port and reset value */
526 writel_relaxed(0, port
->membase
+ ofs
->cr1
);
528 cr1
= USART_CR1_TE
| USART_CR1_RE
| USART_CR1_RXNEIE
;
529 cr1
|= BIT(cfg
->uart_enable_bit
);
530 if (stm32_port
->fifoen
)
531 cr1
|= USART_CR1_FIFOEN
;
536 cr2
|= USART_CR2_STOP_2B
;
538 if (cflag
& PARENB
) {
539 cr1
|= USART_CR1_PCE
;
540 if ((cflag
& CSIZE
) == CS8
) {
541 if (cfg
->has_7bits_data
)
551 port
->status
&= ~(UPSTAT_AUTOCTS
| UPSTAT_AUTORTS
);
552 if (cflag
& CRTSCTS
) {
553 port
->status
|= UPSTAT_AUTOCTS
| UPSTAT_AUTORTS
;
554 cr3
|= USART_CR3_CTSE
| USART_CR3_RTSE
;
557 usartdiv
= DIV_ROUND_CLOSEST(port
->uartclk
, baud
);
560 * The USART supports 16 or 8 times oversampling.
561 * By default we prefer 16 times oversampling, so that the receiver
562 * has a better tolerance to clock deviations.
563 * 8 times oversampling is only used to achieve higher speeds.
567 stm32_set_bits(port
, ofs
->cr1
, USART_CR1_OVER8
);
570 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_OVER8
);
573 mantissa
= (usartdiv
/ oversampling
) << USART_BRR_DIV_M_SHIFT
;
574 fraction
= usartdiv
% oversampling
;
575 writel_relaxed(mantissa
| fraction
, port
->membase
+ ofs
->brr
);
577 uart_update_timeout(port
, cflag
, baud
);
579 port
->read_status_mask
= USART_SR_ORE
;
580 if (termios
->c_iflag
& INPCK
)
581 port
->read_status_mask
|= USART_SR_PE
| USART_SR_FE
;
582 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
583 port
->read_status_mask
|= USART_SR_FE
;
585 /* Characters to ignore */
586 port
->ignore_status_mask
= 0;
587 if (termios
->c_iflag
& IGNPAR
)
588 port
->ignore_status_mask
= USART_SR_PE
| USART_SR_FE
;
589 if (termios
->c_iflag
& IGNBRK
) {
590 port
->ignore_status_mask
|= USART_SR_FE
;
592 * If we're ignoring parity and break indicators,
593 * ignore overruns too (for real raw support).
595 if (termios
->c_iflag
& IGNPAR
)
596 port
->ignore_status_mask
|= USART_SR_ORE
;
599 /* Ignore all characters if CREAD is not set */
600 if ((termios
->c_cflag
& CREAD
) == 0)
601 port
->ignore_status_mask
|= USART_SR_DUMMY_RX
;
603 if (stm32_port
->rx_ch
)
604 cr3
|= USART_CR3_DMAR
;
606 writel_relaxed(cr3
, port
->membase
+ ofs
->cr3
);
607 writel_relaxed(cr2
, port
->membase
+ ofs
->cr2
);
608 writel_relaxed(cr1
, port
->membase
+ ofs
->cr1
);
610 spin_unlock_irqrestore(&port
->lock
, flags
);
613 static const char *stm32_type(struct uart_port
*port
)
615 return (port
->type
== PORT_STM32
) ? DRIVER_NAME
: NULL
;
618 static void stm32_release_port(struct uart_port
*port
)
622 static int stm32_request_port(struct uart_port
*port
)
627 static void stm32_config_port(struct uart_port
*port
, int flags
)
629 if (flags
& UART_CONFIG_TYPE
)
630 port
->type
= PORT_STM32
;
634 stm32_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
636 /* No user changeable parameters */
640 static void stm32_pm(struct uart_port
*port
, unsigned int state
,
641 unsigned int oldstate
)
643 struct stm32_port
*stm32port
= container_of(port
,
644 struct stm32_port
, port
);
645 struct stm32_usart_offsets
*ofs
= &stm32port
->info
->ofs
;
646 struct stm32_usart_config
*cfg
= &stm32port
->info
->cfg
;
647 unsigned long flags
= 0;
650 case UART_PM_STATE_ON
:
651 clk_prepare_enable(stm32port
->clk
);
653 case UART_PM_STATE_OFF
:
654 spin_lock_irqsave(&port
->lock
, flags
);
655 stm32_clr_bits(port
, ofs
->cr1
, BIT(cfg
->uart_enable_bit
));
656 spin_unlock_irqrestore(&port
->lock
, flags
);
657 clk_disable_unprepare(stm32port
->clk
);
662 static const struct uart_ops stm32_uart_ops
= {
663 .tx_empty
= stm32_tx_empty
,
664 .set_mctrl
= stm32_set_mctrl
,
665 .get_mctrl
= stm32_get_mctrl
,
666 .stop_tx
= stm32_stop_tx
,
667 .start_tx
= stm32_start_tx
,
668 .throttle
= stm32_throttle
,
669 .unthrottle
= stm32_unthrottle
,
670 .stop_rx
= stm32_stop_rx
,
671 .break_ctl
= stm32_break_ctl
,
672 .startup
= stm32_startup
,
673 .shutdown
= stm32_shutdown
,
674 .set_termios
= stm32_set_termios
,
677 .release_port
= stm32_release_port
,
678 .request_port
= stm32_request_port
,
679 .config_port
= stm32_config_port
,
680 .verify_port
= stm32_verify_port
,
683 static int stm32_init_port(struct stm32_port
*stm32port
,
684 struct platform_device
*pdev
)
686 struct uart_port
*port
= &stm32port
->port
;
687 struct resource
*res
;
690 port
->iotype
= UPIO_MEM
;
691 port
->flags
= UPF_BOOT_AUTOCONF
;
692 port
->ops
= &stm32_uart_ops
;
693 port
->dev
= &pdev
->dev
;
694 port
->irq
= platform_get_irq(pdev
, 0);
695 stm32port
->wakeirq
= platform_get_irq(pdev
, 1);
696 stm32port
->fifoen
= stm32port
->info
->cfg
.has_fifo
;
698 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
699 port
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
700 if (IS_ERR(port
->membase
))
701 return PTR_ERR(port
->membase
);
702 port
->mapbase
= res
->start
;
704 spin_lock_init(&port
->lock
);
706 stm32port
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
707 if (IS_ERR(stm32port
->clk
))
708 return PTR_ERR(stm32port
->clk
);
710 /* Ensure that clk rate is correct by enabling the clk */
711 ret
= clk_prepare_enable(stm32port
->clk
);
715 stm32port
->port
.uartclk
= clk_get_rate(stm32port
->clk
);
716 if (!stm32port
->port
.uartclk
) {
717 clk_disable_unprepare(stm32port
->clk
);
724 static struct stm32_port
*stm32_of_get_stm32_port(struct platform_device
*pdev
)
726 struct device_node
*np
= pdev
->dev
.of_node
;
732 id
= of_alias_get_id(np
, "serial");
734 dev_err(&pdev
->dev
, "failed to get alias id, errno %d\n", id
);
738 if (WARN_ON(id
>= STM32_MAX_PORTS
))
741 stm32_ports
[id
].hw_flow_control
= of_property_read_bool(np
,
743 stm32_ports
[id
].port
.line
= id
;
744 stm32_ports
[id
].last_res
= RX_BUF_L
;
745 return &stm32_ports
[id
];
749 static const struct of_device_id stm32_match
[] = {
750 { .compatible
= "st,stm32-uart", .data
= &stm32f4_info
},
751 { .compatible
= "st,stm32f7-uart", .data
= &stm32f7_info
},
752 { .compatible
= "st,stm32h7-uart", .data
= &stm32h7_info
},
756 MODULE_DEVICE_TABLE(of
, stm32_match
);
759 static int stm32_of_dma_rx_probe(struct stm32_port
*stm32port
,
760 struct platform_device
*pdev
)
762 struct stm32_usart_offsets
*ofs
= &stm32port
->info
->ofs
;
763 struct uart_port
*port
= &stm32port
->port
;
764 struct device
*dev
= &pdev
->dev
;
765 struct dma_slave_config config
;
766 struct dma_async_tx_descriptor
*desc
= NULL
;
770 /* Request DMA RX channel */
771 stm32port
->rx_ch
= dma_request_slave_channel(dev
, "rx");
772 if (!stm32port
->rx_ch
) {
773 dev_info(dev
, "rx dma alloc failed\n");
776 stm32port
->rx_buf
= dma_alloc_coherent(&pdev
->dev
, RX_BUF_L
,
777 &stm32port
->rx_dma_buf
,
779 if (!stm32port
->rx_buf
) {
784 /* Configure DMA channel */
785 memset(&config
, 0, sizeof(config
));
786 config
.src_addr
= port
->mapbase
+ ofs
->rdr
;
787 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
789 ret
= dmaengine_slave_config(stm32port
->rx_ch
, &config
);
791 dev_err(dev
, "rx dma channel config failed\n");
796 /* Prepare a DMA cyclic transaction */
797 desc
= dmaengine_prep_dma_cyclic(stm32port
->rx_ch
,
798 stm32port
->rx_dma_buf
,
799 RX_BUF_L
, RX_BUF_P
, DMA_DEV_TO_MEM
,
802 dev_err(dev
, "rx dma prep cyclic failed\n");
807 /* No callback as dma buffer is drained on usart interrupt */
808 desc
->callback
= NULL
;
809 desc
->callback_param
= NULL
;
811 /* Push current DMA transaction in the pending queue */
812 cookie
= dmaengine_submit(desc
);
814 /* Issue pending DMA requests */
815 dma_async_issue_pending(stm32port
->rx_ch
);
820 dma_free_coherent(&pdev
->dev
,
821 RX_BUF_L
, stm32port
->rx_buf
,
822 stm32port
->rx_dma_buf
);
825 dma_release_channel(stm32port
->rx_ch
);
826 stm32port
->rx_ch
= NULL
;
831 static int stm32_of_dma_tx_probe(struct stm32_port
*stm32port
,
832 struct platform_device
*pdev
)
834 struct stm32_usart_offsets
*ofs
= &stm32port
->info
->ofs
;
835 struct uart_port
*port
= &stm32port
->port
;
836 struct device
*dev
= &pdev
->dev
;
837 struct dma_slave_config config
;
840 stm32port
->tx_dma_busy
= false;
842 /* Request DMA TX channel */
843 stm32port
->tx_ch
= dma_request_slave_channel(dev
, "tx");
844 if (!stm32port
->tx_ch
) {
845 dev_info(dev
, "tx dma alloc failed\n");
848 stm32port
->tx_buf
= dma_alloc_coherent(&pdev
->dev
, TX_BUF_L
,
849 &stm32port
->tx_dma_buf
,
851 if (!stm32port
->tx_buf
) {
856 /* Configure DMA channel */
857 memset(&config
, 0, sizeof(config
));
858 config
.dst_addr
= port
->mapbase
+ ofs
->tdr
;
859 config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
861 ret
= dmaengine_slave_config(stm32port
->tx_ch
, &config
);
863 dev_err(dev
, "tx dma channel config failed\n");
871 dma_free_coherent(&pdev
->dev
,
872 TX_BUF_L
, stm32port
->tx_buf
,
873 stm32port
->tx_dma_buf
);
876 dma_release_channel(stm32port
->tx_ch
);
877 stm32port
->tx_ch
= NULL
;
882 static int stm32_serial_probe(struct platform_device
*pdev
)
884 const struct of_device_id
*match
;
885 struct stm32_port
*stm32port
;
888 stm32port
= stm32_of_get_stm32_port(pdev
);
892 match
= of_match_device(stm32_match
, &pdev
->dev
);
893 if (match
&& match
->data
)
894 stm32port
->info
= (struct stm32_usart_info
*)match
->data
;
898 ret
= stm32_init_port(stm32port
, pdev
);
902 if (stm32port
->info
->cfg
.has_wakeup
&& stm32port
->wakeirq
>= 0) {
903 ret
= device_init_wakeup(&pdev
->dev
, true);
908 ret
= uart_add_one_port(&stm32_usart_driver
, &stm32port
->port
);
912 ret
= stm32_of_dma_rx_probe(stm32port
, pdev
);
914 dev_info(&pdev
->dev
, "interrupt mode used for rx (no dma)\n");
916 ret
= stm32_of_dma_tx_probe(stm32port
, pdev
);
918 dev_info(&pdev
->dev
, "interrupt mode used for tx (no dma)\n");
920 platform_set_drvdata(pdev
, &stm32port
->port
);
925 if (stm32port
->info
->cfg
.has_wakeup
&& stm32port
->wakeirq
>= 0)
926 device_init_wakeup(&pdev
->dev
, false);
929 clk_disable_unprepare(stm32port
->clk
);
934 static int stm32_serial_remove(struct platform_device
*pdev
)
936 struct uart_port
*port
= platform_get_drvdata(pdev
);
937 struct stm32_port
*stm32_port
= to_stm32_port(port
);
938 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
939 struct stm32_usart_config
*cfg
= &stm32_port
->info
->cfg
;
941 stm32_clr_bits(port
, ofs
->cr3
, USART_CR3_DMAR
);
943 if (stm32_port
->rx_ch
)
944 dma_release_channel(stm32_port
->rx_ch
);
946 if (stm32_port
->rx_dma_buf
)
947 dma_free_coherent(&pdev
->dev
,
948 RX_BUF_L
, stm32_port
->rx_buf
,
949 stm32_port
->rx_dma_buf
);
951 stm32_clr_bits(port
, ofs
->cr3
, USART_CR3_DMAT
);
953 if (stm32_port
->tx_ch
)
954 dma_release_channel(stm32_port
->tx_ch
);
956 if (stm32_port
->tx_dma_buf
)
957 dma_free_coherent(&pdev
->dev
,
958 TX_BUF_L
, stm32_port
->tx_buf
,
959 stm32_port
->tx_dma_buf
);
961 if (cfg
->has_wakeup
&& stm32_port
->wakeirq
>= 0)
962 device_init_wakeup(&pdev
->dev
, false);
964 clk_disable_unprepare(stm32_port
->clk
);
966 return uart_remove_one_port(&stm32_usart_driver
, port
);
970 #ifdef CONFIG_SERIAL_STM32_CONSOLE
971 static void stm32_console_putchar(struct uart_port
*port
, int ch
)
973 struct stm32_port
*stm32_port
= to_stm32_port(port
);
974 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
976 while (!(readl_relaxed(port
->membase
+ ofs
->isr
) & USART_SR_TXE
))
979 writel_relaxed(ch
, port
->membase
+ ofs
->tdr
);
982 static void stm32_console_write(struct console
*co
, const char *s
, unsigned cnt
)
984 struct uart_port
*port
= &stm32_ports
[co
->index
].port
;
985 struct stm32_port
*stm32_port
= to_stm32_port(port
);
986 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
987 struct stm32_usart_config
*cfg
= &stm32_port
->info
->cfg
;
989 u32 old_cr1
, new_cr1
;
992 local_irq_save(flags
);
995 else if (oops_in_progress
)
996 locked
= spin_trylock(&port
->lock
);
998 spin_lock(&port
->lock
);
1000 /* Save and disable interrupts, enable the transmitter */
1001 old_cr1
= readl_relaxed(port
->membase
+ ofs
->cr1
);
1002 new_cr1
= old_cr1
& ~USART_CR1_IE_MASK
;
1003 new_cr1
|= USART_CR1_TE
| BIT(cfg
->uart_enable_bit
);
1004 writel_relaxed(new_cr1
, port
->membase
+ ofs
->cr1
);
1006 uart_console_write(port
, s
, cnt
, stm32_console_putchar
);
1008 /* Restore interrupt state */
1009 writel_relaxed(old_cr1
, port
->membase
+ ofs
->cr1
);
1012 spin_unlock(&port
->lock
);
1013 local_irq_restore(flags
);
1016 static int stm32_console_setup(struct console
*co
, char *options
)
1018 struct stm32_port
*stm32port
;
1024 if (co
->index
>= STM32_MAX_PORTS
)
1027 stm32port
= &stm32_ports
[co
->index
];
1030 * This driver does not support early console initialization
1031 * (use ARM early printk support instead), so we only expect
1032 * this to be called during the uart port registration when the
1033 * driver gets probed and the port should be mapped at that point.
1035 if (stm32port
->port
.mapbase
== 0 || stm32port
->port
.membase
== NULL
)
1039 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1041 return uart_set_options(&stm32port
->port
, co
, baud
, parity
, bits
, flow
);
1044 static struct console stm32_console
= {
1045 .name
= STM32_SERIAL_NAME
,
1046 .device
= uart_console_device
,
1047 .write
= stm32_console_write
,
1048 .setup
= stm32_console_setup
,
1049 .flags
= CON_PRINTBUFFER
,
1051 .data
= &stm32_usart_driver
,
1054 #define STM32_SERIAL_CONSOLE (&stm32_console)
1057 #define STM32_SERIAL_CONSOLE NULL
1058 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
1060 static struct uart_driver stm32_usart_driver
= {
1061 .driver_name
= DRIVER_NAME
,
1062 .dev_name
= STM32_SERIAL_NAME
,
1065 .nr
= STM32_MAX_PORTS
,
1066 .cons
= STM32_SERIAL_CONSOLE
,
1069 #ifdef CONFIG_PM_SLEEP
1070 static void stm32_serial_enable_wakeup(struct uart_port
*port
, bool enable
)
1072 struct stm32_port
*stm32_port
= to_stm32_port(port
);
1073 struct stm32_usart_offsets
*ofs
= &stm32_port
->info
->ofs
;
1074 struct stm32_usart_config
*cfg
= &stm32_port
->info
->cfg
;
1077 if (!cfg
->has_wakeup
|| stm32_port
->wakeirq
< 0)
1081 stm32_clr_bits(port
, ofs
->cr1
, BIT(cfg
->uart_enable_bit
));
1082 stm32_set_bits(port
, ofs
->cr1
, USART_CR1_UESM
);
1083 val
= readl_relaxed(port
->membase
+ ofs
->cr3
);
1084 val
&= ~USART_CR3_WUS_MASK
;
1085 /* Enable Wake up interrupt from low power on start bit */
1086 val
|= USART_CR3_WUS_START_BIT
| USART_CR3_WUFIE
;
1087 writel_relaxed(val
, port
->membase
+ ofs
->cr3
);
1088 stm32_set_bits(port
, ofs
->cr1
, BIT(cfg
->uart_enable_bit
));
1090 stm32_clr_bits(port
, ofs
->cr1
, USART_CR1_UESM
);
1094 static int stm32_serial_suspend(struct device
*dev
)
1096 struct uart_port
*port
= dev_get_drvdata(dev
);
1098 uart_suspend_port(&stm32_usart_driver
, port
);
1100 if (device_may_wakeup(dev
))
1101 stm32_serial_enable_wakeup(port
, true);
1103 stm32_serial_enable_wakeup(port
, false);
1108 static int stm32_serial_resume(struct device
*dev
)
1110 struct uart_port
*port
= dev_get_drvdata(dev
);
1112 if (device_may_wakeup(dev
))
1113 stm32_serial_enable_wakeup(port
, false);
1115 return uart_resume_port(&stm32_usart_driver
, port
);
1117 #endif /* CONFIG_PM_SLEEP */
1119 static const struct dev_pm_ops stm32_serial_pm_ops
= {
1120 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend
, stm32_serial_resume
)
1123 static struct platform_driver stm32_serial_driver
= {
1124 .probe
= stm32_serial_probe
,
1125 .remove
= stm32_serial_remove
,
1127 .name
= DRIVER_NAME
,
1128 .pm
= &stm32_serial_pm_ops
,
1129 .of_match_table
= of_match_ptr(stm32_match
),
1133 static int __init
usart_init(void)
1135 static char banner
[] __initdata
= "STM32 USART driver initialized";
1138 pr_info("%s\n", banner
);
1140 ret
= uart_register_driver(&stm32_usart_driver
);
1144 ret
= platform_driver_register(&stm32_serial_driver
);
1146 uart_unregister_driver(&stm32_usart_driver
);
1151 static void __exit
usart_exit(void)
1153 platform_driver_unregister(&stm32_serial_driver
);
1154 uart_unregister_driver(&stm32_usart_driver
);
1157 module_init(usart_init
);
1158 module_exit(usart_exit
);
1160 MODULE_ALIAS("platform:" DRIVER_NAME
);
1161 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1162 MODULE_LICENSE("GPL v2");