1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics SA 2017
9 * Inspired by st-asc.c from STMicroelectronics (c)
12 #include <linux/clk.h>
13 #include <linux/console.h>
14 #include <linux/delay.h>
15 #include <linux/dma-direction.h>
16 #include <linux/dmaengine.h>
17 #include <linux/dma-mapping.h>
19 #include <linux/iopoll.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
23 #include <linux/of_platform.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pm_wakeirq.h>
28 #include <linux/serial_core.h>
29 #include <linux/serial.h>
30 #include <linux/spinlock.h>
31 #include <linux/sysrq.h>
32 #include <linux/tty_flip.h>
33 #include <linux/tty.h>
35 #include "serial_mctrl_gpio.h"
36 #include "stm32-usart.h"
39 /* Register offsets */
40 static struct stm32_usart_info __maybe_unused stm32f4_info = {
55 .uart_enable_bit = 13,
56 .has_7bits_data = false,
61 static struct stm32_usart_info __maybe_unused stm32f7_info = {
77 .has_7bits_data = true,
83 static struct stm32_usart_info __maybe_unused stm32h7_info = {
99 .has_7bits_data = true,
107 static void stm32_usart_stop_tx(struct uart_port *port);
108 static void stm32_usart_transmit_chars(struct uart_port *port);
109 static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch);
111 static inline struct stm32_port *to_stm32_port(struct uart_port *port)
113 return container_of(port, struct stm32_port, port);
116 static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
120 val = readl_relaxed(port->membase + reg);
122 writel_relaxed(val, port->membase + reg);
125 static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
129 val = readl_relaxed(port->membase + reg);
131 writel_relaxed(val, port->membase + reg);
134 static unsigned int stm32_usart_tx_empty(struct uart_port *port)
136 struct stm32_port *stm32_port = to_stm32_port(port);
137 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
139 if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
145 static void stm32_usart_rs485_rts_enable(struct uart_port *port)
147 struct stm32_port *stm32_port = to_stm32_port(port);
148 struct serial_rs485 *rs485conf = &port->rs485;
150 if (stm32_port->hw_flow_control ||
151 !(rs485conf->flags & SER_RS485_ENABLED))
154 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
155 mctrl_gpio_set(stm32_port->gpios,
156 stm32_port->port.mctrl | TIOCM_RTS);
158 mctrl_gpio_set(stm32_port->gpios,
159 stm32_port->port.mctrl & ~TIOCM_RTS);
163 static void stm32_usart_rs485_rts_disable(struct uart_port *port)
165 struct stm32_port *stm32_port = to_stm32_port(port);
166 struct serial_rs485 *rs485conf = &port->rs485;
168 if (stm32_port->hw_flow_control ||
169 !(rs485conf->flags & SER_RS485_ENABLED))
172 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
173 mctrl_gpio_set(stm32_port->gpios,
174 stm32_port->port.mctrl & ~TIOCM_RTS);
176 mctrl_gpio_set(stm32_port->gpios,
177 stm32_port->port.mctrl | TIOCM_RTS);
181 static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
182 u32 delay_DDE, u32 baud)
185 u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
188 *cr3 |= USART_CR3_DEM;
189 over8 = *cr1 & USART_CR1_OVER8;
191 *cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
194 rs485_deat_dedt = delay_ADE * baud * 8;
196 rs485_deat_dedt = delay_ADE * baud * 16;
198 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
199 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
200 rs485_deat_dedt_max : rs485_deat_dedt;
201 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
203 *cr1 |= rs485_deat_dedt;
206 rs485_deat_dedt = delay_DDE * baud * 8;
208 rs485_deat_dedt = delay_DDE * baud * 16;
210 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
211 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
212 rs485_deat_dedt_max : rs485_deat_dedt;
213 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
215 *cr1 |= rs485_deat_dedt;
218 static int stm32_usart_config_rs485(struct uart_port *port, struct ktermios *termios,
219 struct serial_rs485 *rs485conf)
221 struct stm32_port *stm32_port = to_stm32_port(port);
222 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
223 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
224 u32 usartdiv, baud, cr1, cr3;
227 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
229 if (port->rs485_rx_during_tx_gpio)
230 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
231 !!(rs485conf->flags & SER_RS485_RX_DURING_TX));
233 rs485conf->flags |= SER_RS485_RX_DURING_TX;
235 if (rs485conf->flags & SER_RS485_ENABLED) {
236 cr1 = readl_relaxed(port->membase + ofs->cr1);
237 cr3 = readl_relaxed(port->membase + ofs->cr3);
238 usartdiv = readl_relaxed(port->membase + ofs->brr);
239 usartdiv = usartdiv & GENMASK(15, 0);
240 over8 = cr1 & USART_CR1_OVER8;
243 usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
244 << USART_BRR_04_R_SHIFT;
246 baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
247 stm32_usart_config_reg_rs485(&cr1, &cr3,
248 rs485conf->delay_rts_before_send,
249 rs485conf->delay_rts_after_send,
252 if (rs485conf->flags & SER_RS485_RTS_ON_SEND)
253 cr3 &= ~USART_CR3_DEP;
255 cr3 |= USART_CR3_DEP;
257 writel_relaxed(cr3, port->membase + ofs->cr3);
258 writel_relaxed(cr1, port->membase + ofs->cr1);
260 stm32_usart_clr_bits(port, ofs->cr3,
261 USART_CR3_DEM | USART_CR3_DEP);
262 stm32_usart_clr_bits(port, ofs->cr1,
263 USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
266 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
268 /* Adjust RTS polarity in case it's driven in software */
269 if (stm32_usart_tx_empty(port))
270 stm32_usart_rs485_rts_disable(port);
272 stm32_usart_rs485_rts_enable(port);
277 static int stm32_usart_init_rs485(struct uart_port *port,
278 struct platform_device *pdev)
280 struct serial_rs485 *rs485conf = &port->rs485;
282 rs485conf->flags = 0;
283 rs485conf->delay_rts_before_send = 0;
284 rs485conf->delay_rts_after_send = 0;
286 if (!pdev->dev.of_node)
289 return uart_get_rs485_mode(port);
292 static bool stm32_usart_rx_dma_enabled(struct uart_port *port)
294 struct stm32_port *stm32_port = to_stm32_port(port);
295 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
297 if (!stm32_port->rx_ch)
300 return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR);
303 /* Return true when data is pending (in pio mode), and false when no data is pending. */
304 static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
306 struct stm32_port *stm32_port = to_stm32_port(port);
307 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
309 *sr = readl_relaxed(port->membase + ofs->isr);
310 /* Get pending characters in RDR or FIFO */
311 if (*sr & USART_SR_RXNE) {
312 /* Get all pending characters from the RDR or the FIFO when using interrupts */
313 if (!stm32_usart_rx_dma_enabled(port))
316 /* Handle only RX data errors when using DMA */
317 if (*sr & USART_SR_ERR_MASK)
324 static unsigned long stm32_usart_get_char_pio(struct uart_port *port)
326 struct stm32_port *stm32_port = to_stm32_port(port);
327 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
330 c = readl_relaxed(port->membase + ofs->rdr);
331 /* Apply RDR data mask */
332 c &= stm32_port->rdr_mask;
337 static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
339 struct stm32_port *stm32_port = to_stm32_port(port);
340 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
342 unsigned int size = 0;
346 while (stm32_usart_pending_rx_pio(port, &sr)) {
347 sr |= USART_SR_DUMMY_RX;
351 * Status bits has to be cleared before reading the RDR:
352 * In FIFO mode, reading the RDR will pop the next data
353 * (if any) along with its status bits into the SR.
354 * Not doing so leads to misalignement between RDR and SR,
355 * and clear status bits of the next rx data.
357 * Clear errors flags for stm32f7 and stm32h7 compatible
358 * devices. On stm32f4 compatible devices, the error bit is
359 * cleared by the sequence [read SR - read DR].
361 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
362 writel_relaxed(sr & USART_SR_ERR_MASK,
363 port->membase + ofs->icr);
365 c = stm32_usart_get_char_pio(port);
368 if (sr & USART_SR_ERR_MASK) {
369 if (sr & USART_SR_ORE) {
370 port->icount.overrun++;
371 } else if (sr & USART_SR_PE) {
372 port->icount.parity++;
373 } else if (sr & USART_SR_FE) {
374 /* Break detection if character is null */
377 if (uart_handle_break(port))
380 port->icount.frame++;
384 sr &= port->read_status_mask;
386 if (sr & USART_SR_PE) {
388 } else if (sr & USART_SR_FE) {
396 if (uart_prepare_sysrq_char(port, c))
398 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
404 static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
406 struct stm32_port *stm32_port = to_stm32_port(port);
407 struct tty_port *ttyport = &stm32_port->port.state->port;
408 unsigned char *dma_start;
411 dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
414 * Apply rdr_mask on buffer in order to mask parity bit.
415 * This loop is useless in cs8 mode because DMA copies only
416 * 8 bits and already ignores parity bit.
418 if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
419 for (i = 0; i < dma_size; i++)
420 *(dma_start + i) &= stm32_port->rdr_mask;
422 dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
423 port->icount.rx += dma_count;
424 if (dma_count != dma_size)
425 port->icount.buf_overrun++;
426 stm32_port->last_res -= dma_count;
427 if (stm32_port->last_res == 0)
428 stm32_port->last_res = RX_BUF_L;
431 static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
433 struct stm32_port *stm32_port = to_stm32_port(port);
434 unsigned int dma_size, size = 0;
436 /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
437 if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
438 /* Conditional first part: from last_res to end of DMA buffer */
439 dma_size = stm32_port->last_res;
440 stm32_usart_push_buffer_dma(port, dma_size);
444 dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
445 stm32_usart_push_buffer_dma(port, dma_size);
451 static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
453 struct stm32_port *stm32_port = to_stm32_port(port);
454 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
455 enum dma_status rx_dma_status;
457 unsigned int size = 0;
459 if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) {
460 rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
461 stm32_port->rx_ch->cookie,
462 &stm32_port->rx_dma_state);
463 if (rx_dma_status == DMA_IN_PROGRESS) {
464 /* Empty DMA buffer */
465 size = stm32_usart_receive_chars_dma(port);
466 sr = readl_relaxed(port->membase + ofs->isr);
467 if (sr & USART_SR_ERR_MASK) {
468 /* Disable DMA request line */
469 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
471 /* Switch to PIO mode to handle the errors */
472 size += stm32_usart_receive_chars_pio(port);
474 /* Switch back to DMA mode */
475 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
479 dmaengine_terminate_async(stm32_port->rx_ch);
480 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
481 /* Fall back to interrupt mode */
482 dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
483 size = stm32_usart_receive_chars_pio(port);
486 size = stm32_usart_receive_chars_pio(port);
492 static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
494 dmaengine_terminate_async(stm32_port->tx_ch);
495 stm32_port->tx_dma_busy = false;
498 static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
501 * We cannot use the function "dmaengine_tx_status" to know the
502 * status of DMA. This function does not show if the "dma complete"
503 * callback of the DMA transaction has been called. So we prefer
504 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
507 return stm32_port->tx_dma_busy;
510 static bool stm32_usart_tx_dma_enabled(struct stm32_port *stm32_port)
512 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
514 return !!(readl_relaxed(stm32_port->port.membase + ofs->cr3) & USART_CR3_DMAT);
517 static void stm32_usart_tx_dma_complete(void *arg)
519 struct uart_port *port = arg;
520 struct stm32_port *stm32port = to_stm32_port(port);
521 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
524 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
525 stm32_usart_tx_dma_terminate(stm32port);
527 /* Let's see if we have pending data to send */
528 spin_lock_irqsave(&port->lock, flags);
529 stm32_usart_transmit_chars(port);
530 spin_unlock_irqrestore(&port->lock, flags);
533 static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
535 struct stm32_port *stm32_port = to_stm32_port(port);
536 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
539 * Enables TX FIFO threashold irq when FIFO is enabled,
540 * or TX empty irq when FIFO is disabled
542 if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
543 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
545 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
548 static void stm32_usart_tc_interrupt_enable(struct uart_port *port)
550 struct stm32_port *stm32_port = to_stm32_port(port);
551 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
553 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE);
556 static void stm32_usart_rx_dma_complete(void *arg)
558 struct uart_port *port = arg;
559 struct tty_port *tport = &port->state->port;
563 spin_lock_irqsave(&port->lock, flags);
564 size = stm32_usart_receive_chars(port, false);
565 uart_unlock_and_check_sysrq_irqrestore(port, flags);
567 tty_flip_buffer_push(tport);
570 static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
572 struct stm32_port *stm32_port = to_stm32_port(port);
573 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
575 if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
576 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
578 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
581 static void stm32_usart_tc_interrupt_disable(struct uart_port *port)
583 struct stm32_port *stm32_port = to_stm32_port(port);
584 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
586 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE);
589 static void stm32_usart_transmit_chars_pio(struct uart_port *port)
591 struct stm32_port *stm32_port = to_stm32_port(port);
592 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
593 struct circ_buf *xmit = &port->state->xmit;
595 if (stm32_usart_tx_dma_enabled(stm32_port))
596 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
598 while (!uart_circ_empty(xmit)) {
599 /* Check that TDR is empty before filling FIFO */
600 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
602 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
603 uart_xmit_advance(port, 1);
606 /* rely on TXE irq (mask or unmask) for sending remaining data */
607 if (uart_circ_empty(xmit))
608 stm32_usart_tx_interrupt_disable(port);
610 stm32_usart_tx_interrupt_enable(port);
613 static void stm32_usart_transmit_chars_dma(struct uart_port *port)
615 struct stm32_port *stm32port = to_stm32_port(port);
616 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
617 struct circ_buf *xmit = &port->state->xmit;
618 struct dma_async_tx_descriptor *desc = NULL;
621 if (stm32_usart_tx_dma_started(stm32port)) {
622 if (!stm32_usart_tx_dma_enabled(stm32port))
623 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
627 count = uart_circ_chars_pending(xmit);
629 if (count > TX_BUF_L)
632 if (xmit->tail < xmit->head) {
633 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
635 size_t one = UART_XMIT_SIZE - xmit->tail;
642 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
644 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
647 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
648 stm32port->tx_dma_buf,
657 * Set "tx_dma_busy" flag. This flag will be released when
658 * dmaengine_terminate_async will be called. This flag helps
659 * transmit_chars_dma not to start another DMA transaction
660 * if the callback of the previous is not yet called.
662 stm32port->tx_dma_busy = true;
664 desc->callback = stm32_usart_tx_dma_complete;
665 desc->callback_param = port;
667 /* Push current DMA TX transaction in the pending queue */
668 if (dma_submit_error(dmaengine_submit(desc))) {
669 /* dma no yet started, safe to free resources */
670 stm32_usart_tx_dma_terminate(stm32port);
674 /* Issue pending DMA TX requests */
675 dma_async_issue_pending(stm32port->tx_ch);
677 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
679 uart_xmit_advance(port, count);
684 stm32_usart_transmit_chars_pio(port);
687 static void stm32_usart_transmit_chars(struct uart_port *port)
689 struct stm32_port *stm32_port = to_stm32_port(port);
690 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
691 struct circ_buf *xmit = &port->state->xmit;
695 if (!stm32_port->hw_flow_control &&
696 port->rs485.flags & SER_RS485_ENABLED &&
698 !(uart_circ_empty(xmit) || uart_tx_stopped(port)))) {
699 stm32_usart_tc_interrupt_disable(port);
700 stm32_usart_rs485_rts_enable(port);
704 if (stm32_usart_tx_dma_started(stm32_port) &&
705 stm32_usart_tx_dma_enabled(stm32_port))
706 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
708 /* Check that TDR is empty before filling FIFO */
710 readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
712 (isr & USART_SR_TXE),
715 dev_warn(port->dev, "1 character may be erased\n");
717 writel_relaxed(port->x_char, port->membase + ofs->tdr);
720 if (stm32_usart_tx_dma_started(stm32_port))
721 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
725 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
726 stm32_usart_tx_interrupt_disable(port);
730 if (ofs->icr == UNDEF_REG)
731 stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
733 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
735 if (stm32_port->tx_ch)
736 stm32_usart_transmit_chars_dma(port);
738 stm32_usart_transmit_chars_pio(port);
740 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
741 uart_write_wakeup(port);
743 if (uart_circ_empty(xmit)) {
744 stm32_usart_tx_interrupt_disable(port);
745 if (!stm32_port->hw_flow_control &&
746 port->rs485.flags & SER_RS485_ENABLED) {
747 stm32_usart_tc_interrupt_enable(port);
752 static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
754 struct uart_port *port = ptr;
755 struct tty_port *tport = &port->state->port;
756 struct stm32_port *stm32_port = to_stm32_port(port);
757 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
761 sr = readl_relaxed(port->membase + ofs->isr);
763 if (!stm32_port->hw_flow_control &&
764 port->rs485.flags & SER_RS485_ENABLED &&
765 (sr & USART_SR_TC)) {
766 stm32_usart_tc_interrupt_disable(port);
767 stm32_usart_rs485_rts_disable(port);
770 if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
771 writel_relaxed(USART_ICR_RTOCF,
772 port->membase + ofs->icr);
774 if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
775 /* Clear wake up flag and disable wake up interrupt */
776 writel_relaxed(USART_ICR_WUCF,
777 port->membase + ofs->icr);
778 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
779 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
780 pm_wakeup_event(tport->tty->dev, 0);
784 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
785 * line has been masked by HW and rx data are stacking in FIFO.
787 if (!stm32_port->throttled) {
788 if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) ||
789 ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) {
790 spin_lock(&port->lock);
791 size = stm32_usart_receive_chars(port, false);
792 uart_unlock_and_check_sysrq(port);
794 tty_flip_buffer_push(tport);
798 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
799 spin_lock(&port->lock);
800 stm32_usart_transmit_chars(port);
801 spin_unlock(&port->lock);
804 /* Receiver timeout irq for DMA RX */
805 if (stm32_usart_rx_dma_enabled(port) && !stm32_port->throttled) {
806 spin_lock(&port->lock);
807 size = stm32_usart_receive_chars(port, false);
808 uart_unlock_and_check_sysrq(port);
810 tty_flip_buffer_push(tport);
816 static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
818 struct stm32_port *stm32_port = to_stm32_port(port);
819 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
821 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
822 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
824 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
826 mctrl_gpio_set(stm32_port->gpios, mctrl);
829 static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
831 struct stm32_port *stm32_port = to_stm32_port(port);
834 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
835 ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
837 return mctrl_gpio_get(stm32_port->gpios, &ret);
840 static void stm32_usart_enable_ms(struct uart_port *port)
842 mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
845 static void stm32_usart_disable_ms(struct uart_port *port)
847 mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
851 static void stm32_usart_stop_tx(struct uart_port *port)
853 struct stm32_port *stm32_port = to_stm32_port(port);
854 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
856 stm32_usart_tx_interrupt_disable(port);
857 if (stm32_usart_tx_dma_started(stm32_port) && stm32_usart_tx_dma_enabled(stm32_port))
858 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
860 stm32_usart_rs485_rts_disable(port);
863 /* There are probably characters waiting to be transmitted. */
864 static void stm32_usart_start_tx(struct uart_port *port)
866 struct circ_buf *xmit = &port->state->xmit;
868 if (uart_circ_empty(xmit) && !port->x_char) {
869 stm32_usart_rs485_rts_disable(port);
873 stm32_usart_rs485_rts_enable(port);
875 stm32_usart_transmit_chars(port);
878 /* Flush the transmit buffer. */
879 static void stm32_usart_flush_buffer(struct uart_port *port)
881 struct stm32_port *stm32_port = to_stm32_port(port);
882 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
884 if (stm32_port->tx_ch) {
885 stm32_usart_tx_dma_terminate(stm32_port);
886 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
890 /* Throttle the remote when input buffer is about to overflow. */
891 static void stm32_usart_throttle(struct uart_port *port)
893 struct stm32_port *stm32_port = to_stm32_port(port);
894 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
897 spin_lock_irqsave(&port->lock, flags);
900 * Disable DMA request line if enabled, so the RX data gets queued into the FIFO.
901 * Hardware flow control is triggered when RX FIFO is full.
903 if (stm32_usart_rx_dma_enabled(port))
904 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
906 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
907 if (stm32_port->cr3_irq)
908 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
910 stm32_port->throttled = true;
911 spin_unlock_irqrestore(&port->lock, flags);
914 /* Unthrottle the remote, the input buffer can now accept data. */
915 static void stm32_usart_unthrottle(struct uart_port *port)
917 struct stm32_port *stm32_port = to_stm32_port(port);
918 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
921 spin_lock_irqsave(&port->lock, flags);
922 stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
923 if (stm32_port->cr3_irq)
924 stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
927 * Switch back to DMA mode (re-enable DMA request line).
928 * Hardware flow control is stopped when FIFO is not full any more.
930 if (stm32_port->rx_ch)
931 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
933 stm32_port->throttled = false;
934 spin_unlock_irqrestore(&port->lock, flags);
938 static void stm32_usart_stop_rx(struct uart_port *port)
940 struct stm32_port *stm32_port = to_stm32_port(port);
941 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
943 /* Disable DMA request line. */
944 if (stm32_port->rx_ch)
945 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
947 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
948 if (stm32_port->cr3_irq)
949 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
952 /* Handle breaks - ignored by us */
953 static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
957 static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port)
959 struct stm32_port *stm32_port = to_stm32_port(port);
960 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
961 struct dma_async_tx_descriptor *desc;
964 stm32_port->last_res = RX_BUF_L;
965 /* Prepare a DMA cyclic transaction */
966 desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
967 stm32_port->rx_dma_buf,
972 dev_err(port->dev, "rx dma prep cyclic failed\n");
976 desc->callback = stm32_usart_rx_dma_complete;
977 desc->callback_param = port;
979 /* Push current DMA transaction in the pending queue */
980 ret = dma_submit_error(dmaengine_submit(desc));
982 dmaengine_terminate_sync(stm32_port->rx_ch);
986 /* Issue pending DMA requests */
987 dma_async_issue_pending(stm32_port->rx_ch);
990 * DMA request line not re-enabled at resume when port is throttled.
991 * It will be re-enabled by unthrottle ops.
993 if (!stm32_port->throttled)
994 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
999 static int stm32_usart_startup(struct uart_port *port)
1001 struct stm32_port *stm32_port = to_stm32_port(port);
1002 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1003 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1004 const char *name = to_platform_device(port->dev)->name;
1008 ret = request_irq(port->irq, stm32_usart_interrupt,
1009 IRQF_NO_SUSPEND, name, port);
1013 if (stm32_port->swap) {
1014 val = readl_relaxed(port->membase + ofs->cr2);
1015 val |= USART_CR2_SWAP;
1016 writel_relaxed(val, port->membase + ofs->cr2);
1020 if (ofs->rqr != UNDEF_REG)
1021 writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
1023 if (stm32_port->rx_ch) {
1024 ret = stm32_usart_start_rx_dma_cyclic(port);
1026 free_irq(port->irq, port);
1032 val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
1033 stm32_usart_set_bits(port, ofs->cr1, val);
1038 static void stm32_usart_shutdown(struct uart_port *port)
1040 struct stm32_port *stm32_port = to_stm32_port(port);
1041 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1042 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1046 if (stm32_usart_tx_dma_enabled(stm32_port))
1047 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1049 if (stm32_usart_tx_dma_started(stm32_port))
1050 stm32_usart_tx_dma_terminate(stm32_port);
1052 /* Disable modem control interrupts */
1053 stm32_usart_disable_ms(port);
1055 val = USART_CR1_TXEIE | USART_CR1_TE;
1056 val |= stm32_port->cr1_irq | USART_CR1_RE;
1057 val |= BIT(cfg->uart_enable_bit);
1058 if (stm32_port->fifoen)
1059 val |= USART_CR1_FIFOEN;
1061 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
1062 isr, (isr & USART_SR_TC),
1065 /* Send the TC error message only when ISR_TC is not set */
1067 dev_err(port->dev, "Transmission is not complete\n");
1069 /* Disable RX DMA. */
1070 if (stm32_port->rx_ch)
1071 dmaengine_terminate_async(stm32_port->rx_ch);
1073 /* flush RX & TX FIFO */
1074 if (ofs->rqr != UNDEF_REG)
1075 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1076 port->membase + ofs->rqr);
1078 stm32_usart_clr_bits(port, ofs->cr1, val);
1080 free_irq(port->irq, port);
1083 static void stm32_usart_set_termios(struct uart_port *port,
1084 struct ktermios *termios,
1085 const struct ktermios *old)
1087 struct stm32_port *stm32_port = to_stm32_port(port);
1088 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1089 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1090 struct serial_rs485 *rs485conf = &port->rs485;
1091 unsigned int baud, bits;
1092 u32 usartdiv, mantissa, fraction, oversampling;
1093 tcflag_t cflag = termios->c_cflag;
1094 u32 cr1, cr2, cr3, isr;
1095 unsigned long flags;
1098 if (!stm32_port->hw_flow_control)
1101 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
1103 spin_lock_irqsave(&port->lock, flags);
1105 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
1107 (isr & USART_SR_TC),
1110 /* Send the TC error message only when ISR_TC is not set. */
1112 dev_err(port->dev, "Transmission is not complete\n");
1114 /* Stop serial port and reset value */
1115 writel_relaxed(0, port->membase + ofs->cr1);
1117 /* flush RX & TX FIFO */
1118 if (ofs->rqr != UNDEF_REG)
1119 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1120 port->membase + ofs->rqr);
1122 cr1 = USART_CR1_TE | USART_CR1_RE;
1123 if (stm32_port->fifoen)
1124 cr1 |= USART_CR1_FIFOEN;
1125 cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
1127 /* Tx and RX FIFO configuration */
1128 cr3 = readl_relaxed(port->membase + ofs->cr3);
1129 cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
1130 if (stm32_port->fifoen) {
1131 if (stm32_port->txftcfg >= 0)
1132 cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
1133 if (stm32_port->rxftcfg >= 0)
1134 cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
1138 cr2 |= USART_CR2_STOP_2B;
1140 bits = tty_get_char_size(cflag);
1141 stm32_port->rdr_mask = (BIT(bits) - 1);
1143 if (cflag & PARENB) {
1145 cr1 |= USART_CR1_PCE;
1149 * Word length configuration:
1150 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1151 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1152 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1153 * M0 and M1 already cleared by cr1 initialization.
1156 cr1 |= USART_CR1_M0;
1157 } else if ((bits == 7) && cfg->has_7bits_data) {
1158 cr1 |= USART_CR1_M1;
1159 } else if (bits != 8) {
1160 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1164 termios->c_cflag = cflag;
1166 if (cflag & PARENB) {
1168 cr1 |= USART_CR1_M0;
1172 if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
1173 (stm32_port->fifoen &&
1174 stm32_port->rxftcfg >= 0))) {
1176 bits = bits + 3; /* 1 start bit + 2 stop bits */
1178 bits = bits + 2; /* 1 start bit + 1 stop bit */
1180 /* RX timeout irq to occur after last stop bit + bits */
1181 stm32_port->cr1_irq = USART_CR1_RTOIE;
1182 writel_relaxed(bits, port->membase + ofs->rtor);
1183 cr2 |= USART_CR2_RTOEN;
1185 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
1186 * wake up over usart, from low power until the DMA gets re-enabled by resume.
1188 stm32_port->cr3_irq = USART_CR3_RXFTIE;
1191 cr1 |= stm32_port->cr1_irq;
1192 cr3 |= stm32_port->cr3_irq;
1195 cr1 |= USART_CR1_PS;
1197 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
1198 if (cflag & CRTSCTS) {
1199 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
1200 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
1203 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
1206 * The USART supports 16 or 8 times oversampling.
1207 * By default we prefer 16 times oversampling, so that the receiver
1208 * has a better tolerance to clock deviations.
1209 * 8 times oversampling is only used to achieve higher speeds.
1211 if (usartdiv < 16) {
1213 cr1 |= USART_CR1_OVER8;
1214 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
1217 cr1 &= ~USART_CR1_OVER8;
1218 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
1221 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
1222 fraction = usartdiv % oversampling;
1223 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
1225 uart_update_timeout(port, cflag, baud);
1227 port->read_status_mask = USART_SR_ORE;
1228 if (termios->c_iflag & INPCK)
1229 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
1230 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1231 port->read_status_mask |= USART_SR_FE;
1233 /* Characters to ignore */
1234 port->ignore_status_mask = 0;
1235 if (termios->c_iflag & IGNPAR)
1236 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
1237 if (termios->c_iflag & IGNBRK) {
1238 port->ignore_status_mask |= USART_SR_FE;
1240 * If we're ignoring parity and break indicators,
1241 * ignore overruns too (for real raw support).
1243 if (termios->c_iflag & IGNPAR)
1244 port->ignore_status_mask |= USART_SR_ORE;
1247 /* Ignore all characters if CREAD is not set */
1248 if ((termios->c_cflag & CREAD) == 0)
1249 port->ignore_status_mask |= USART_SR_DUMMY_RX;
1251 if (stm32_port->rx_ch) {
1253 * Setup DMA to collect only valid data and enable error irqs.
1254 * This also enables break reception when using DMA.
1256 cr1 |= USART_CR1_PEIE;
1257 cr3 |= USART_CR3_EIE;
1258 cr3 |= USART_CR3_DMAR;
1259 cr3 |= USART_CR3_DDRE;
1262 if (rs485conf->flags & SER_RS485_ENABLED) {
1263 stm32_usart_config_reg_rs485(&cr1, &cr3,
1264 rs485conf->delay_rts_before_send,
1265 rs485conf->delay_rts_after_send,
1267 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
1268 cr3 &= ~USART_CR3_DEP;
1269 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1271 cr3 |= USART_CR3_DEP;
1272 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1276 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
1277 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1280 /* Configure wake up from low power on start bit detection */
1281 if (stm32_port->wakeup_src) {
1282 cr3 &= ~USART_CR3_WUS_MASK;
1283 cr3 |= USART_CR3_WUS_START_BIT;
1286 writel_relaxed(cr3, port->membase + ofs->cr3);
1287 writel_relaxed(cr2, port->membase + ofs->cr2);
1288 writel_relaxed(cr1, port->membase + ofs->cr1);
1290 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1291 spin_unlock_irqrestore(&port->lock, flags);
1293 /* Handle modem control interrupts */
1294 if (UART_ENABLE_MS(port, termios->c_cflag))
1295 stm32_usart_enable_ms(port);
1297 stm32_usart_disable_ms(port);
1300 static const char *stm32_usart_type(struct uart_port *port)
1302 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
1305 static void stm32_usart_release_port(struct uart_port *port)
1309 static int stm32_usart_request_port(struct uart_port *port)
1314 static void stm32_usart_config_port(struct uart_port *port, int flags)
1316 if (flags & UART_CONFIG_TYPE)
1317 port->type = PORT_STM32;
1321 stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
1323 /* No user changeable parameters */
1327 static void stm32_usart_pm(struct uart_port *port, unsigned int state,
1328 unsigned int oldstate)
1330 struct stm32_port *stm32port = container_of(port,
1331 struct stm32_port, port);
1332 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1333 const struct stm32_usart_config *cfg = &stm32port->info->cfg;
1334 unsigned long flags;
1337 case UART_PM_STATE_ON:
1338 pm_runtime_get_sync(port->dev);
1340 case UART_PM_STATE_OFF:
1341 spin_lock_irqsave(&port->lock, flags);
1342 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1343 spin_unlock_irqrestore(&port->lock, flags);
1344 pm_runtime_put_sync(port->dev);
1349 #if defined(CONFIG_CONSOLE_POLL)
1351 /* Callbacks for characters polling in debug context (i.e. KGDB). */
1352 static int stm32_usart_poll_init(struct uart_port *port)
1354 struct stm32_port *stm32_port = to_stm32_port(port);
1356 return clk_prepare_enable(stm32_port->clk);
1359 static int stm32_usart_poll_get_char(struct uart_port *port)
1361 struct stm32_port *stm32_port = to_stm32_port(port);
1362 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1364 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE))
1365 return NO_POLL_CHAR;
1367 return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask;
1370 static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch)
1372 stm32_usart_console_putchar(port, ch);
1374 #endif /* CONFIG_CONSOLE_POLL */
1376 static const struct uart_ops stm32_uart_ops = {
1377 .tx_empty = stm32_usart_tx_empty,
1378 .set_mctrl = stm32_usart_set_mctrl,
1379 .get_mctrl = stm32_usart_get_mctrl,
1380 .stop_tx = stm32_usart_stop_tx,
1381 .start_tx = stm32_usart_start_tx,
1382 .throttle = stm32_usart_throttle,
1383 .unthrottle = stm32_usart_unthrottle,
1384 .stop_rx = stm32_usart_stop_rx,
1385 .enable_ms = stm32_usart_enable_ms,
1386 .break_ctl = stm32_usart_break_ctl,
1387 .startup = stm32_usart_startup,
1388 .shutdown = stm32_usart_shutdown,
1389 .flush_buffer = stm32_usart_flush_buffer,
1390 .set_termios = stm32_usart_set_termios,
1391 .pm = stm32_usart_pm,
1392 .type = stm32_usart_type,
1393 .release_port = stm32_usart_release_port,
1394 .request_port = stm32_usart_request_port,
1395 .config_port = stm32_usart_config_port,
1396 .verify_port = stm32_usart_verify_port,
1397 #if defined(CONFIG_CONSOLE_POLL)
1398 .poll_init = stm32_usart_poll_init,
1399 .poll_get_char = stm32_usart_poll_get_char,
1400 .poll_put_char = stm32_usart_poll_put_char,
1401 #endif /* CONFIG_CONSOLE_POLL */
1405 * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
1406 * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
1407 * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
1408 * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
1410 static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
1412 static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
1417 /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
1418 if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
1421 for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
1422 if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
1424 if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
1425 i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
1427 dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
1428 stm32h7_usart_fifo_thresh_cfg[i]);
1430 /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
1437 static void stm32_usart_deinit_port(struct stm32_port *stm32port)
1439 clk_disable_unprepare(stm32port->clk);
1442 static const struct serial_rs485 stm32_rs485_supported = {
1443 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
1444 SER_RS485_RX_DURING_TX,
1445 .delay_rts_before_send = 1,
1446 .delay_rts_after_send = 1,
1449 static int stm32_usart_init_port(struct stm32_port *stm32port,
1450 struct platform_device *pdev)
1452 struct uart_port *port = &stm32port->port;
1453 struct resource *res;
1456 irq = platform_get_irq(pdev, 0);
1460 port->iotype = UPIO_MEM;
1461 port->flags = UPF_BOOT_AUTOCONF;
1462 port->ops = &stm32_uart_ops;
1463 port->dev = &pdev->dev;
1464 port->fifosize = stm32port->info->cfg.fifosize;
1465 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1467 port->rs485_config = stm32_usart_config_rs485;
1468 port->rs485_supported = stm32_rs485_supported;
1470 ret = stm32_usart_init_rs485(port, pdev);
1474 stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
1475 of_property_read_bool(pdev->dev.of_node, "wakeup-source");
1477 stm32port->swap = stm32port->info->cfg.has_swap &&
1478 of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
1480 stm32port->fifoen = stm32port->info->cfg.has_fifo;
1481 if (stm32port->fifoen) {
1482 stm32_usart_get_ftcfg(pdev, "rx-threshold",
1483 &stm32port->rxftcfg);
1484 stm32_usart_get_ftcfg(pdev, "tx-threshold",
1485 &stm32port->txftcfg);
1488 port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1489 if (IS_ERR(port->membase))
1490 return PTR_ERR(port->membase);
1491 port->mapbase = res->start;
1493 spin_lock_init(&port->lock);
1495 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
1496 if (IS_ERR(stm32port->clk))
1497 return PTR_ERR(stm32port->clk);
1499 /* Ensure that clk rate is correct by enabling the clk */
1500 ret = clk_prepare_enable(stm32port->clk);
1504 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1505 if (!stm32port->port.uartclk) {
1510 stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
1511 if (IS_ERR(stm32port->gpios)) {
1512 ret = PTR_ERR(stm32port->gpios);
1517 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
1518 * properties should not be specified.
1520 if (stm32port->hw_flow_control) {
1521 if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
1522 mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
1523 dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
1532 clk_disable_unprepare(stm32port->clk);
1537 static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
1539 struct device_node *np = pdev->dev.of_node;
1545 id = of_alias_get_id(np, "serial");
1547 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1551 if (WARN_ON(id >= STM32_MAX_PORTS))
1554 stm32_ports[id].hw_flow_control =
1555 of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
1556 of_property_read_bool (np, "uart-has-rtscts");
1557 stm32_ports[id].port.line = id;
1558 stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1559 stm32_ports[id].cr3_irq = 0;
1560 stm32_ports[id].last_res = RX_BUF_L;
1561 return &stm32_ports[id];
1565 static const struct of_device_id stm32_match[] = {
1566 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
1567 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1568 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
1572 MODULE_DEVICE_TABLE(of, stm32_match);
1575 static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1576 struct platform_device *pdev)
1578 if (stm32port->rx_buf)
1579 dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1580 stm32port->rx_dma_buf);
1583 static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
1584 struct platform_device *pdev)
1586 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1587 struct uart_port *port = &stm32port->port;
1588 struct device *dev = &pdev->dev;
1589 struct dma_slave_config config;
1592 stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
1593 &stm32port->rx_dma_buf,
1595 if (!stm32port->rx_buf)
1598 /* Configure DMA channel */
1599 memset(&config, 0, sizeof(config));
1600 config.src_addr = port->mapbase + ofs->rdr;
1601 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1603 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
1605 dev_err(dev, "rx dma channel config failed\n");
1606 stm32_usart_of_dma_rx_remove(stm32port, pdev);
1613 static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1614 struct platform_device *pdev)
1616 if (stm32port->tx_buf)
1617 dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1618 stm32port->tx_dma_buf);
1621 static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
1622 struct platform_device *pdev)
1624 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1625 struct uart_port *port = &stm32port->port;
1626 struct device *dev = &pdev->dev;
1627 struct dma_slave_config config;
1630 stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
1631 &stm32port->tx_dma_buf,
1633 if (!stm32port->tx_buf)
1636 /* Configure DMA channel */
1637 memset(&config, 0, sizeof(config));
1638 config.dst_addr = port->mapbase + ofs->tdr;
1639 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1641 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1643 dev_err(dev, "tx dma channel config failed\n");
1644 stm32_usart_of_dma_tx_remove(stm32port, pdev);
1651 static int stm32_usart_serial_probe(struct platform_device *pdev)
1653 struct stm32_port *stm32port;
1656 stm32port = stm32_usart_of_get_port(pdev);
1660 stm32port->info = of_device_get_match_data(&pdev->dev);
1661 if (!stm32port->info)
1664 stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
1665 if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER)
1666 return -EPROBE_DEFER;
1668 /* Fall back in interrupt mode for any non-deferral error */
1669 if (IS_ERR(stm32port->rx_ch))
1670 stm32port->rx_ch = NULL;
1672 stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1673 if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1674 ret = -EPROBE_DEFER;
1677 /* Fall back in interrupt mode for any non-deferral error */
1678 if (IS_ERR(stm32port->tx_ch))
1679 stm32port->tx_ch = NULL;
1681 ret = stm32_usart_init_port(stm32port, pdev);
1685 if (stm32port->wakeup_src) {
1686 device_set_wakeup_capable(&pdev->dev, true);
1687 ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
1689 goto err_deinit_port;
1692 if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1693 /* Fall back in interrupt mode */
1694 dma_release_channel(stm32port->rx_ch);
1695 stm32port->rx_ch = NULL;
1698 if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1699 /* Fall back in interrupt mode */
1700 dma_release_channel(stm32port->tx_ch);
1701 stm32port->tx_ch = NULL;
1704 if (!stm32port->rx_ch)
1705 dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1706 if (!stm32port->tx_ch)
1707 dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
1709 platform_set_drvdata(pdev, &stm32port->port);
1711 pm_runtime_get_noresume(&pdev->dev);
1712 pm_runtime_set_active(&pdev->dev);
1713 pm_runtime_enable(&pdev->dev);
1715 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1719 pm_runtime_put_sync(&pdev->dev);
1724 pm_runtime_disable(&pdev->dev);
1725 pm_runtime_set_suspended(&pdev->dev);
1726 pm_runtime_put_noidle(&pdev->dev);
1728 if (stm32port->tx_ch)
1729 stm32_usart_of_dma_tx_remove(stm32port, pdev);
1730 if (stm32port->rx_ch)
1731 stm32_usart_of_dma_rx_remove(stm32port, pdev);
1733 if (stm32port->wakeup_src)
1734 dev_pm_clear_wake_irq(&pdev->dev);
1737 if (stm32port->wakeup_src)
1738 device_set_wakeup_capable(&pdev->dev, false);
1740 stm32_usart_deinit_port(stm32port);
1743 if (stm32port->tx_ch)
1744 dma_release_channel(stm32port->tx_ch);
1747 if (stm32port->rx_ch)
1748 dma_release_channel(stm32port->rx_ch);
1753 static int stm32_usart_serial_remove(struct platform_device *pdev)
1755 struct uart_port *port = platform_get_drvdata(pdev);
1756 struct stm32_port *stm32_port = to_stm32_port(port);
1757 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1761 pm_runtime_get_sync(&pdev->dev);
1762 err = uart_remove_one_port(&stm32_usart_driver, port);
1766 pm_runtime_disable(&pdev->dev);
1767 pm_runtime_set_suspended(&pdev->dev);
1768 pm_runtime_put_noidle(&pdev->dev);
1770 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
1771 cr3 = readl_relaxed(port->membase + ofs->cr3);
1772 cr3 &= ~USART_CR3_EIE;
1773 cr3 &= ~USART_CR3_DMAR;
1774 cr3 &= ~USART_CR3_DDRE;
1775 writel_relaxed(cr3, port->membase + ofs->cr3);
1777 if (stm32_port->tx_ch) {
1778 stm32_usart_of_dma_tx_remove(stm32_port, pdev);
1779 dma_release_channel(stm32_port->tx_ch);
1782 if (stm32_port->rx_ch) {
1783 stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1784 dma_release_channel(stm32_port->rx_ch);
1787 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1789 if (stm32_port->wakeup_src) {
1790 dev_pm_clear_wake_irq(&pdev->dev);
1791 device_init_wakeup(&pdev->dev, false);
1794 stm32_usart_deinit_port(stm32_port);
1799 static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
1801 struct stm32_port *stm32_port = to_stm32_port(port);
1802 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1806 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr,
1807 (isr & USART_SR_TXE), 100,
1808 STM32_USART_TIMEOUT_USEC);
1810 dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret);
1813 writel_relaxed(ch, port->membase + ofs->tdr);
1816 #ifdef CONFIG_SERIAL_STM32_CONSOLE
1817 static void stm32_usart_console_write(struct console *co, const char *s,
1820 struct uart_port *port = &stm32_ports[co->index].port;
1821 struct stm32_port *stm32_port = to_stm32_port(port);
1822 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1823 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1824 unsigned long flags;
1825 u32 old_cr1, new_cr1;
1828 if (oops_in_progress)
1829 locked = spin_trylock_irqsave(&port->lock, flags);
1831 spin_lock_irqsave(&port->lock, flags);
1833 /* Save and disable interrupts, enable the transmitter */
1834 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1835 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1836 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
1837 writel_relaxed(new_cr1, port->membase + ofs->cr1);
1839 uart_console_write(port, s, cnt, stm32_usart_console_putchar);
1841 /* Restore interrupt state */
1842 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1845 spin_unlock_irqrestore(&port->lock, flags);
1848 static int stm32_usart_console_setup(struct console *co, char *options)
1850 struct stm32_port *stm32port;
1856 if (co->index >= STM32_MAX_PORTS)
1859 stm32port = &stm32_ports[co->index];
1862 * This driver does not support early console initialization
1863 * (use ARM early printk support instead), so we only expect
1864 * this to be called during the uart port registration when the
1865 * driver gets probed and the port should be mapped at that point.
1867 if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
1871 uart_parse_options(options, &baud, &parity, &bits, &flow);
1873 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1876 static struct console stm32_console = {
1877 .name = STM32_SERIAL_NAME,
1878 .device = uart_console_device,
1879 .write = stm32_usart_console_write,
1880 .setup = stm32_usart_console_setup,
1881 .flags = CON_PRINTBUFFER,
1883 .data = &stm32_usart_driver,
1886 #define STM32_SERIAL_CONSOLE (&stm32_console)
1889 #define STM32_SERIAL_CONSOLE NULL
1890 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
1892 #ifdef CONFIG_SERIAL_EARLYCON
1893 static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
1895 struct stm32_usart_info *info = port->private_data;
1897 while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE))
1900 writel_relaxed(ch, port->membase + info->ofs.tdr);
1903 static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count)
1905 struct earlycon_device *device = console->data;
1906 struct uart_port *port = &device->port;
1908 uart_console_write(port, s, count, early_stm32_usart_console_putchar);
1911 static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
1913 if (!(device->port.membase || device->port.iobase))
1915 device->port.private_data = &stm32h7_info;
1916 device->con->write = early_stm32_serial_write;
1920 static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
1922 if (!(device->port.membase || device->port.iobase))
1924 device->port.private_data = &stm32f7_info;
1925 device->con->write = early_stm32_serial_write;
1929 static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
1931 if (!(device->port.membase || device->port.iobase))
1933 device->port.private_data = &stm32f4_info;
1934 device->con->write = early_stm32_serial_write;
1938 OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup);
1939 OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup);
1940 OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup);
1941 #endif /* CONFIG_SERIAL_EARLYCON */
1943 static struct uart_driver stm32_usart_driver = {
1944 .driver_name = DRIVER_NAME,
1945 .dev_name = STM32_SERIAL_NAME,
1948 .nr = STM32_MAX_PORTS,
1949 .cons = STM32_SERIAL_CONSOLE,
1952 static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1955 struct stm32_port *stm32_port = to_stm32_port(port);
1956 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1957 struct tty_port *tport = &port->state->port;
1960 unsigned long flags;
1962 if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
1966 * Enable low-power wake-up and wake-up irq if argument is set to
1967 * "enable", disable low-power wake-up and wake-up irq otherwise
1970 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
1971 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
1972 mctrl_gpio_enable_irq_wake(stm32_port->gpios);
1975 * When DMA is used for reception, it must be disabled before
1976 * entering low-power mode and re-enabled when exiting from
1979 if (stm32_port->rx_ch) {
1980 spin_lock_irqsave(&port->lock, flags);
1981 /* Avoid race with RX IRQ when DMAR is cleared */
1982 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
1983 /* Poll data from DMA RX buffer if any */
1984 size = stm32_usart_receive_chars(port, true);
1985 dmaengine_terminate_async(stm32_port->rx_ch);
1986 uart_unlock_and_check_sysrq_irqrestore(port, flags);
1988 tty_flip_buffer_push(tport);
1991 /* Poll data from RX FIFO if any */
1992 stm32_usart_receive_chars(port, false);
1994 if (stm32_port->rx_ch) {
1995 ret = stm32_usart_start_rx_dma_cyclic(port);
1999 mctrl_gpio_disable_irq_wake(stm32_port->gpios);
2000 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
2001 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
2007 static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
2009 struct uart_port *port = dev_get_drvdata(dev);
2012 uart_suspend_port(&stm32_usart_driver, port);
2014 if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
2015 ret = stm32_usart_serial_en_wakeup(port, true);
2021 * When "no_console_suspend" is enabled, keep the pinctrl default state
2022 * and rely on bootloader stage to restore this state upon resume.
2023 * Otherwise, apply the idle or sleep states depending on wakeup
2026 if (console_suspend_enabled || !uart_console(port)) {
2027 if (device_may_wakeup(dev) || device_wakeup_path(dev))
2028 pinctrl_pm_select_idle_state(dev);
2030 pinctrl_pm_select_sleep_state(dev);
2036 static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
2038 struct uart_port *port = dev_get_drvdata(dev);
2041 pinctrl_pm_select_default_state(dev);
2043 if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
2044 ret = stm32_usart_serial_en_wakeup(port, false);
2049 return uart_resume_port(&stm32_usart_driver, port);
2052 static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
2054 struct uart_port *port = dev_get_drvdata(dev);
2055 struct stm32_port *stm32port = container_of(port,
2056 struct stm32_port, port);
2058 clk_disable_unprepare(stm32port->clk);
2063 static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
2065 struct uart_port *port = dev_get_drvdata(dev);
2066 struct stm32_port *stm32port = container_of(port,
2067 struct stm32_port, port);
2069 return clk_prepare_enable(stm32port->clk);
2072 static const struct dev_pm_ops stm32_serial_pm_ops = {
2073 SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
2074 stm32_usart_runtime_resume, NULL)
2075 SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
2076 stm32_usart_serial_resume)
2079 static struct platform_driver stm32_serial_driver = {
2080 .probe = stm32_usart_serial_probe,
2081 .remove = stm32_usart_serial_remove,
2083 .name = DRIVER_NAME,
2084 .pm = &stm32_serial_pm_ops,
2085 .of_match_table = of_match_ptr(stm32_match),
2089 static int __init stm32_usart_init(void)
2091 static char banner[] __initdata = "STM32 USART driver initialized";
2094 pr_info("%s\n", banner);
2096 ret = uart_register_driver(&stm32_usart_driver);
2100 ret = platform_driver_register(&stm32_serial_driver);
2102 uart_unregister_driver(&stm32_usart_driver);
2107 static void __exit stm32_usart_exit(void)
2109 platform_driver_unregister(&stm32_serial_driver);
2110 uart_unregister_driver(&stm32_usart_driver);
2113 module_init(stm32_usart_init);
2114 module_exit(stm32_usart_exit);
2116 MODULE_ALIAS("platform:" DRIVER_NAME);
2117 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
2118 MODULE_LICENSE("GPL v2");