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_started(struct stm32_port *stm32_port)
294 return stm32_port->rx_ch ? stm32_port->rx_dma_busy : false;
297 static void stm32_usart_rx_dma_terminate(struct stm32_port *stm32_port)
299 dmaengine_terminate_async(stm32_port->rx_ch);
300 stm32_port->rx_dma_busy = false;
303 static int stm32_usart_dma_pause_resume(struct stm32_port *stm32_port,
304 struct dma_chan *chan,
305 enum dma_status expected_status,
306 int dmaengine_pause_or_resume(struct dma_chan *),
307 bool stm32_usart_xx_dma_started(struct stm32_port *),
308 void stm32_usart_xx_dma_terminate(struct stm32_port *))
310 struct uart_port *port = &stm32_port->port;
311 enum dma_status dma_status;
314 if (!stm32_usart_xx_dma_started(stm32_port))
317 dma_status = dmaengine_tx_status(chan, chan->cookie, NULL);
318 if (dma_status != expected_status)
321 ret = dmaengine_pause_or_resume(chan);
323 dev_err(port->dev, "DMA failed with error code: %d\n", ret);
324 stm32_usart_xx_dma_terminate(stm32_port);
329 static int stm32_usart_rx_dma_pause(struct stm32_port *stm32_port)
331 return stm32_usart_dma_pause_resume(stm32_port, stm32_port->rx_ch,
332 DMA_IN_PROGRESS, dmaengine_pause,
333 stm32_usart_rx_dma_started,
334 stm32_usart_rx_dma_terminate);
337 static int stm32_usart_rx_dma_resume(struct stm32_port *stm32_port)
339 return stm32_usart_dma_pause_resume(stm32_port, stm32_port->rx_ch,
340 DMA_PAUSED, dmaengine_resume,
341 stm32_usart_rx_dma_started,
342 stm32_usart_rx_dma_terminate);
345 /* Return true when data is pending (in pio mode), and false when no data is pending. */
346 static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
348 struct stm32_port *stm32_port = to_stm32_port(port);
349 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
351 *sr = readl_relaxed(port->membase + ofs->isr);
352 /* Get pending characters in RDR or FIFO */
353 if (*sr & USART_SR_RXNE) {
354 /* Get all pending characters from the RDR or the FIFO when using interrupts */
355 if (!stm32_usart_rx_dma_started(stm32_port))
358 /* Handle only RX data errors when using DMA */
359 if (*sr & USART_SR_ERR_MASK)
366 static u8 stm32_usart_get_char_pio(struct uart_port *port)
368 struct stm32_port *stm32_port = to_stm32_port(port);
369 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
372 c = readl_relaxed(port->membase + ofs->rdr);
373 /* Apply RDR data mask */
374 c &= stm32_port->rdr_mask;
379 static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
381 struct stm32_port *stm32_port = to_stm32_port(port);
382 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
383 unsigned int size = 0;
387 while (stm32_usart_pending_rx_pio(port, &sr)) {
388 sr |= USART_SR_DUMMY_RX;
392 * Status bits has to be cleared before reading the RDR:
393 * In FIFO mode, reading the RDR will pop the next data
394 * (if any) along with its status bits into the SR.
395 * Not doing so leads to misalignement between RDR and SR,
396 * and clear status bits of the next rx data.
398 * Clear errors flags for stm32f7 and stm32h7 compatible
399 * devices. On stm32f4 compatible devices, the error bit is
400 * cleared by the sequence [read SR - read DR].
402 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
403 writel_relaxed(sr & USART_SR_ERR_MASK,
404 port->membase + ofs->icr);
406 c = stm32_usart_get_char_pio(port);
409 if (sr & USART_SR_ERR_MASK) {
410 if (sr & USART_SR_ORE) {
411 port->icount.overrun++;
412 } else if (sr & USART_SR_PE) {
413 port->icount.parity++;
414 } else if (sr & USART_SR_FE) {
415 /* Break detection if character is null */
418 if (uart_handle_break(port))
421 port->icount.frame++;
425 sr &= port->read_status_mask;
427 if (sr & USART_SR_PE) {
429 } else if (sr & USART_SR_FE) {
437 if (uart_prepare_sysrq_char(port, c))
439 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
445 static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
447 struct stm32_port *stm32_port = to_stm32_port(port);
448 struct tty_port *ttyport = &stm32_port->port.state->port;
449 unsigned char *dma_start;
452 dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
455 * Apply rdr_mask on buffer in order to mask parity bit.
456 * This loop is useless in cs8 mode because DMA copies only
457 * 8 bits and already ignores parity bit.
459 if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
460 for (i = 0; i < dma_size; i++)
461 *(dma_start + i) &= stm32_port->rdr_mask;
463 dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
464 port->icount.rx += dma_count;
465 if (dma_count != dma_size)
466 port->icount.buf_overrun++;
467 stm32_port->last_res -= dma_count;
468 if (stm32_port->last_res == 0)
469 stm32_port->last_res = RX_BUF_L;
472 static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
474 struct stm32_port *stm32_port = to_stm32_port(port);
475 unsigned int dma_size, size = 0;
477 /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
478 if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
479 /* Conditional first part: from last_res to end of DMA buffer */
480 dma_size = stm32_port->last_res;
481 stm32_usart_push_buffer_dma(port, dma_size);
485 dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
486 stm32_usart_push_buffer_dma(port, dma_size);
492 static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
494 struct stm32_port *stm32_port = to_stm32_port(port);
495 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
496 enum dma_status rx_dma_status;
498 unsigned int size = 0;
500 if (stm32_usart_rx_dma_started(stm32_port) || force_dma_flush) {
501 rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
502 stm32_port->rx_ch->cookie,
503 &stm32_port->rx_dma_state);
504 if (rx_dma_status == DMA_IN_PROGRESS ||
505 rx_dma_status == DMA_PAUSED) {
506 /* Empty DMA buffer */
507 size = stm32_usart_receive_chars_dma(port);
508 sr = readl_relaxed(port->membase + ofs->isr);
509 if (sr & USART_SR_ERR_MASK) {
510 /* Disable DMA request line */
511 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
513 /* Switch to PIO mode to handle the errors */
514 size += stm32_usart_receive_chars_pio(port);
516 /* Switch back to DMA mode */
517 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
521 stm32_usart_rx_dma_terminate(stm32_port);
522 /* Fall back to interrupt mode */
523 dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
524 size = stm32_usart_receive_chars_pio(port);
527 size = stm32_usart_receive_chars_pio(port);
533 static void stm32_usart_rx_dma_complete(void *arg)
535 struct uart_port *port = arg;
536 struct tty_port *tport = &port->state->port;
540 uart_port_lock_irqsave(port, &flags);
541 size = stm32_usart_receive_chars(port, false);
542 uart_unlock_and_check_sysrq_irqrestore(port, flags);
544 tty_flip_buffer_push(tport);
547 static int stm32_usart_rx_dma_start_or_resume(struct uart_port *port)
549 struct stm32_port *stm32_port = to_stm32_port(port);
550 struct dma_async_tx_descriptor *desc;
551 enum dma_status rx_dma_status;
554 if (stm32_port->throttled)
557 if (stm32_port->rx_dma_busy) {
558 rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
559 stm32_port->rx_ch->cookie,
561 if (rx_dma_status == DMA_IN_PROGRESS)
564 if (rx_dma_status == DMA_PAUSED && !stm32_usart_rx_dma_resume(stm32_port))
567 dev_err(port->dev, "DMA failed : status error.\n");
568 stm32_usart_rx_dma_terminate(stm32_port);
571 stm32_port->rx_dma_busy = true;
573 stm32_port->last_res = RX_BUF_L;
574 /* Prepare a DMA cyclic transaction */
575 desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
576 stm32_port->rx_dma_buf,
581 dev_err(port->dev, "rx dma prep cyclic failed\n");
582 stm32_port->rx_dma_busy = false;
586 desc->callback = stm32_usart_rx_dma_complete;
587 desc->callback_param = port;
589 /* Push current DMA transaction in the pending queue */
590 ret = dma_submit_error(dmaengine_submit(desc));
592 dmaengine_terminate_sync(stm32_port->rx_ch);
593 stm32_port->rx_dma_busy = false;
597 /* Issue pending DMA requests */
598 dma_async_issue_pending(stm32_port->rx_ch);
603 static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
605 dmaengine_terminate_async(stm32_port->tx_ch);
606 stm32_port->tx_dma_busy = false;
609 static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
612 * We cannot use the function "dmaengine_tx_status" to know the
613 * status of DMA. This function does not show if the "dma complete"
614 * callback of the DMA transaction has been called. So we prefer
615 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
618 return stm32_port->tx_dma_busy;
621 static int stm32_usart_tx_dma_pause(struct stm32_port *stm32_port)
623 return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
624 DMA_IN_PROGRESS, dmaengine_pause,
625 stm32_usart_tx_dma_started,
626 stm32_usart_tx_dma_terminate);
629 static int stm32_usart_tx_dma_resume(struct stm32_port *stm32_port)
631 return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
632 DMA_PAUSED, dmaengine_resume,
633 stm32_usart_tx_dma_started,
634 stm32_usart_tx_dma_terminate);
637 static void stm32_usart_tx_dma_complete(void *arg)
639 struct uart_port *port = arg;
640 struct stm32_port *stm32port = to_stm32_port(port);
643 stm32_usart_tx_dma_terminate(stm32port);
645 /* Let's see if we have pending data to send */
646 uart_port_lock_irqsave(port, &flags);
647 stm32_usart_transmit_chars(port);
648 uart_port_unlock_irqrestore(port, flags);
651 static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
653 struct stm32_port *stm32_port = to_stm32_port(port);
654 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
657 * Enables TX FIFO threashold irq when FIFO is enabled,
658 * or TX empty irq when FIFO is disabled
660 if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
661 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
663 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
666 static void stm32_usart_tc_interrupt_enable(struct uart_port *port)
668 struct stm32_port *stm32_port = to_stm32_port(port);
669 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
671 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE);
674 static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
676 struct stm32_port *stm32_port = to_stm32_port(port);
677 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
679 if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
680 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
682 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
685 static void stm32_usart_tc_interrupt_disable(struct uart_port *port)
687 struct stm32_port *stm32_port = to_stm32_port(port);
688 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
690 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE);
693 static void stm32_usart_transmit_chars_pio(struct uart_port *port)
695 struct stm32_port *stm32_port = to_stm32_port(port);
696 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
697 struct circ_buf *xmit = &port->state->xmit;
699 while (!uart_circ_empty(xmit)) {
700 /* Check that TDR is empty before filling FIFO */
701 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
703 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
704 uart_xmit_advance(port, 1);
707 /* rely on TXE irq (mask or unmask) for sending remaining data */
708 if (uart_circ_empty(xmit))
709 stm32_usart_tx_interrupt_disable(port);
711 stm32_usart_tx_interrupt_enable(port);
714 static void stm32_usart_transmit_chars_dma(struct uart_port *port)
716 struct stm32_port *stm32port = to_stm32_port(port);
717 struct circ_buf *xmit = &port->state->xmit;
718 struct dma_async_tx_descriptor *desc = NULL;
722 if (stm32_usart_tx_dma_started(stm32port)) {
723 ret = stm32_usart_tx_dma_resume(stm32port);
724 if (ret < 0 && ret != -EAGAIN)
729 count = uart_circ_chars_pending(xmit);
731 if (count > TX_BUF_L)
734 if (xmit->tail < xmit->head) {
735 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
737 size_t one = UART_XMIT_SIZE - xmit->tail;
744 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
746 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
749 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
750 stm32port->tx_dma_buf,
759 * Set "tx_dma_busy" flag. This flag will be released when
760 * dmaengine_terminate_async will be called. This flag helps
761 * transmit_chars_dma not to start another DMA transaction
762 * if the callback of the previous is not yet called.
764 stm32port->tx_dma_busy = true;
766 desc->callback = stm32_usart_tx_dma_complete;
767 desc->callback_param = port;
769 /* Push current DMA TX transaction in the pending queue */
770 /* DMA no yet started, safe to free resources */
771 ret = dma_submit_error(dmaengine_submit(desc));
773 dev_err(port->dev, "DMA failed with error code: %d\n", ret);
774 stm32_usart_tx_dma_terminate(stm32port);
778 /* Issue pending DMA TX requests */
779 dma_async_issue_pending(stm32port->tx_ch);
781 uart_xmit_advance(port, count);
786 stm32_usart_transmit_chars_pio(port);
789 static void stm32_usart_transmit_chars(struct uart_port *port)
791 struct stm32_port *stm32_port = to_stm32_port(port);
792 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
793 struct circ_buf *xmit = &port->state->xmit;
797 if (!stm32_port->hw_flow_control &&
798 port->rs485.flags & SER_RS485_ENABLED &&
800 !(uart_circ_empty(xmit) || uart_tx_stopped(port)))) {
801 stm32_usart_tc_interrupt_disable(port);
802 stm32_usart_rs485_rts_enable(port);
806 /* dma terminate may have been called in case of dma pause failure */
807 stm32_usart_tx_dma_pause(stm32_port);
809 /* Check that TDR is empty before filling FIFO */
811 readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
813 (isr & USART_SR_TXE),
816 dev_warn(port->dev, "1 character may be erased\n");
818 writel_relaxed(port->x_char, port->membase + ofs->tdr);
822 /* dma terminate may have been called in case of dma resume failure */
823 stm32_usart_tx_dma_resume(stm32_port);
827 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
828 stm32_usart_tx_interrupt_disable(port);
832 if (ofs->icr == UNDEF_REG)
833 stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
835 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
837 if (stm32_port->tx_ch)
838 stm32_usart_transmit_chars_dma(port);
840 stm32_usart_transmit_chars_pio(port);
842 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
843 uart_write_wakeup(port);
845 if (uart_circ_empty(xmit)) {
846 stm32_usart_tx_interrupt_disable(port);
847 if (!stm32_port->hw_flow_control &&
848 port->rs485.flags & SER_RS485_ENABLED) {
849 stm32_usart_tc_interrupt_enable(port);
854 static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
856 struct uart_port *port = ptr;
857 struct tty_port *tport = &port->state->port;
858 struct stm32_port *stm32_port = to_stm32_port(port);
859 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
863 sr = readl_relaxed(port->membase + ofs->isr);
865 if (!stm32_port->hw_flow_control &&
866 port->rs485.flags & SER_RS485_ENABLED &&
867 (sr & USART_SR_TC)) {
868 stm32_usart_tc_interrupt_disable(port);
869 stm32_usart_rs485_rts_disable(port);
872 if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
873 writel_relaxed(USART_ICR_RTOCF,
874 port->membase + ofs->icr);
876 if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
877 /* Clear wake up flag and disable wake up interrupt */
878 writel_relaxed(USART_ICR_WUCF,
879 port->membase + ofs->icr);
880 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
881 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
882 pm_wakeup_event(tport->tty->dev, 0);
886 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
887 * line has been masked by HW and rx data are stacking in FIFO.
889 if (!stm32_port->throttled) {
890 if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_started(stm32_port)) ||
891 ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_started(stm32_port))) {
892 uart_port_lock(port);
893 size = stm32_usart_receive_chars(port, false);
894 uart_unlock_and_check_sysrq(port);
896 tty_flip_buffer_push(tport);
900 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
901 uart_port_lock(port);
902 stm32_usart_transmit_chars(port);
903 uart_port_unlock(port);
906 /* Receiver timeout irq for DMA RX */
907 if (stm32_usart_rx_dma_started(stm32_port) && !stm32_port->throttled) {
908 uart_port_lock(port);
909 size = stm32_usart_receive_chars(port, false);
910 uart_unlock_and_check_sysrq(port);
912 tty_flip_buffer_push(tport);
918 static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
920 struct stm32_port *stm32_port = to_stm32_port(port);
921 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
923 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
924 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
926 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
928 mctrl_gpio_set(stm32_port->gpios, mctrl);
931 static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
933 struct stm32_port *stm32_port = to_stm32_port(port);
936 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
937 ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
939 return mctrl_gpio_get(stm32_port->gpios, &ret);
942 static void stm32_usart_enable_ms(struct uart_port *port)
944 mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
947 static void stm32_usart_disable_ms(struct uart_port *port)
949 mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
953 static void stm32_usart_stop_tx(struct uart_port *port)
955 struct stm32_port *stm32_port = to_stm32_port(port);
957 stm32_usart_tx_interrupt_disable(port);
959 /* dma terminate may have been called in case of dma pause failure */
960 stm32_usart_tx_dma_pause(stm32_port);
962 stm32_usart_rs485_rts_disable(port);
965 /* There are probably characters waiting to be transmitted. */
966 static void stm32_usart_start_tx(struct uart_port *port)
968 struct circ_buf *xmit = &port->state->xmit;
970 if (uart_circ_empty(xmit) && !port->x_char) {
971 stm32_usart_rs485_rts_disable(port);
975 stm32_usart_rs485_rts_enable(port);
977 stm32_usart_transmit_chars(port);
980 /* Flush the transmit buffer. */
981 static void stm32_usart_flush_buffer(struct uart_port *port)
983 struct stm32_port *stm32_port = to_stm32_port(port);
985 if (stm32_port->tx_ch)
986 stm32_usart_tx_dma_terminate(stm32_port);
989 /* Throttle the remote when input buffer is about to overflow. */
990 static void stm32_usart_throttle(struct uart_port *port)
992 struct stm32_port *stm32_port = to_stm32_port(port);
993 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
996 uart_port_lock_irqsave(port, &flags);
999 * Pause DMA transfer, so the RX data gets queued into the FIFO.
1000 * Hardware flow control is triggered when RX FIFO is full.
1002 stm32_usart_rx_dma_pause(stm32_port);
1004 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
1005 if (stm32_port->cr3_irq)
1006 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
1008 stm32_port->throttled = true;
1009 uart_port_unlock_irqrestore(port, flags);
1012 /* Unthrottle the remote, the input buffer can now accept data. */
1013 static void stm32_usart_unthrottle(struct uart_port *port)
1015 struct stm32_port *stm32_port = to_stm32_port(port);
1016 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1017 unsigned long flags;
1019 uart_port_lock_irqsave(port, &flags);
1020 stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
1021 if (stm32_port->cr3_irq)
1022 stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
1024 stm32_port->throttled = false;
1027 * Switch back to DMA mode (resume DMA).
1028 * Hardware flow control is stopped when FIFO is not full any more.
1030 if (stm32_port->rx_ch)
1031 stm32_usart_rx_dma_start_or_resume(port);
1033 uart_port_unlock_irqrestore(port, flags);
1037 static void stm32_usart_stop_rx(struct uart_port *port)
1039 struct stm32_port *stm32_port = to_stm32_port(port);
1040 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1042 /* Disable DMA request line. */
1043 stm32_usart_rx_dma_pause(stm32_port);
1045 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
1046 if (stm32_port->cr3_irq)
1047 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
1050 static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
1052 struct stm32_port *stm32_port = to_stm32_port(port);
1053 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1054 unsigned long flags;
1056 spin_lock_irqsave(&port->lock, flags);
1059 stm32_usart_set_bits(port, ofs->rqr, USART_RQR_SBKRQ);
1061 stm32_usart_clr_bits(port, ofs->rqr, USART_RQR_SBKRQ);
1063 spin_unlock_irqrestore(&port->lock, flags);
1066 static int stm32_usart_startup(struct uart_port *port)
1068 struct stm32_port *stm32_port = to_stm32_port(port);
1069 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1070 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1071 const char *name = to_platform_device(port->dev)->name;
1075 ret = request_irq(port->irq, stm32_usart_interrupt,
1076 IRQF_NO_SUSPEND, name, port);
1080 if (stm32_port->swap) {
1081 val = readl_relaxed(port->membase + ofs->cr2);
1082 val |= USART_CR2_SWAP;
1083 writel_relaxed(val, port->membase + ofs->cr2);
1087 if (ofs->rqr != UNDEF_REG)
1088 writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
1090 if (stm32_port->rx_ch) {
1091 ret = stm32_usart_rx_dma_start_or_resume(port);
1093 free_irq(port->irq, port);
1099 val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
1100 stm32_usart_set_bits(port, ofs->cr1, val);
1105 static void stm32_usart_shutdown(struct uart_port *port)
1107 struct stm32_port *stm32_port = to_stm32_port(port);
1108 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1109 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1113 if (stm32_usart_tx_dma_started(stm32_port))
1114 stm32_usart_tx_dma_terminate(stm32_port);
1116 if (stm32_port->tx_ch)
1117 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1119 /* Disable modem control interrupts */
1120 stm32_usart_disable_ms(port);
1122 val = USART_CR1_TXEIE | USART_CR1_TE;
1123 val |= stm32_port->cr1_irq | USART_CR1_RE;
1124 val |= BIT(cfg->uart_enable_bit);
1125 if (stm32_port->fifoen)
1126 val |= USART_CR1_FIFOEN;
1128 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
1129 isr, (isr & USART_SR_TC),
1132 /* Send the TC error message only when ISR_TC is not set */
1134 dev_err(port->dev, "Transmission is not complete\n");
1136 /* Disable RX DMA. */
1137 if (stm32_port->rx_ch) {
1138 stm32_usart_rx_dma_terminate(stm32_port);
1139 dmaengine_synchronize(stm32_port->rx_ch);
1142 /* flush RX & TX FIFO */
1143 if (ofs->rqr != UNDEF_REG)
1144 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1145 port->membase + ofs->rqr);
1147 stm32_usart_clr_bits(port, ofs->cr1, val);
1149 free_irq(port->irq, port);
1152 static void stm32_usart_set_termios(struct uart_port *port,
1153 struct ktermios *termios,
1154 const struct ktermios *old)
1156 struct stm32_port *stm32_port = to_stm32_port(port);
1157 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1158 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1159 struct serial_rs485 *rs485conf = &port->rs485;
1160 unsigned int baud, bits;
1161 u32 usartdiv, mantissa, fraction, oversampling;
1162 tcflag_t cflag = termios->c_cflag;
1163 u32 cr1, cr2, cr3, isr;
1164 unsigned long flags;
1167 if (!stm32_port->hw_flow_control)
1170 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
1172 uart_port_lock_irqsave(port, &flags);
1174 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
1176 (isr & USART_SR_TC),
1179 /* Send the TC error message only when ISR_TC is not set. */
1181 dev_err(port->dev, "Transmission is not complete\n");
1183 /* Stop serial port and reset value */
1184 writel_relaxed(0, port->membase + ofs->cr1);
1186 /* flush RX & TX FIFO */
1187 if (ofs->rqr != UNDEF_REG)
1188 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1189 port->membase + ofs->rqr);
1191 cr1 = USART_CR1_TE | USART_CR1_RE;
1192 if (stm32_port->fifoen)
1193 cr1 |= USART_CR1_FIFOEN;
1194 cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
1196 /* Tx and RX FIFO configuration */
1197 cr3 = readl_relaxed(port->membase + ofs->cr3);
1198 cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
1199 if (stm32_port->fifoen) {
1200 if (stm32_port->txftcfg >= 0)
1201 cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
1202 if (stm32_port->rxftcfg >= 0)
1203 cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
1207 cr2 |= USART_CR2_STOP_2B;
1209 bits = tty_get_char_size(cflag);
1210 stm32_port->rdr_mask = (BIT(bits) - 1);
1212 if (cflag & PARENB) {
1214 cr1 |= USART_CR1_PCE;
1218 * Word length configuration:
1219 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1220 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1221 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1222 * M0 and M1 already cleared by cr1 initialization.
1225 cr1 |= USART_CR1_M0;
1226 } else if ((bits == 7) && cfg->has_7bits_data) {
1227 cr1 |= USART_CR1_M1;
1228 } else if (bits != 8) {
1229 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1233 termios->c_cflag = cflag;
1235 if (cflag & PARENB) {
1237 cr1 |= USART_CR1_M0;
1241 if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
1242 (stm32_port->fifoen &&
1243 stm32_port->rxftcfg >= 0))) {
1245 bits = bits + 3; /* 1 start bit + 2 stop bits */
1247 bits = bits + 2; /* 1 start bit + 1 stop bit */
1249 /* RX timeout irq to occur after last stop bit + bits */
1250 stm32_port->cr1_irq = USART_CR1_RTOIE;
1251 writel_relaxed(bits, port->membase + ofs->rtor);
1252 cr2 |= USART_CR2_RTOEN;
1254 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
1255 * wake up over usart, from low power until the DMA gets re-enabled by resume.
1257 stm32_port->cr3_irq = USART_CR3_RXFTIE;
1260 cr1 |= stm32_port->cr1_irq;
1261 cr3 |= stm32_port->cr3_irq;
1264 cr1 |= USART_CR1_PS;
1266 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
1267 if (cflag & CRTSCTS) {
1268 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
1269 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
1272 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
1275 * The USART supports 16 or 8 times oversampling.
1276 * By default we prefer 16 times oversampling, so that the receiver
1277 * has a better tolerance to clock deviations.
1278 * 8 times oversampling is only used to achieve higher speeds.
1280 if (usartdiv < 16) {
1282 cr1 |= USART_CR1_OVER8;
1283 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
1286 cr1 &= ~USART_CR1_OVER8;
1287 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
1290 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
1291 fraction = usartdiv % oversampling;
1292 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
1294 uart_update_timeout(port, cflag, baud);
1296 port->read_status_mask = USART_SR_ORE;
1297 if (termios->c_iflag & INPCK)
1298 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
1299 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1300 port->read_status_mask |= USART_SR_FE;
1302 /* Characters to ignore */
1303 port->ignore_status_mask = 0;
1304 if (termios->c_iflag & IGNPAR)
1305 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
1306 if (termios->c_iflag & IGNBRK) {
1307 port->ignore_status_mask |= USART_SR_FE;
1309 * If we're ignoring parity and break indicators,
1310 * ignore overruns too (for real raw support).
1312 if (termios->c_iflag & IGNPAR)
1313 port->ignore_status_mask |= USART_SR_ORE;
1316 /* Ignore all characters if CREAD is not set */
1317 if ((termios->c_cflag & CREAD) == 0)
1318 port->ignore_status_mask |= USART_SR_DUMMY_RX;
1320 if (stm32_port->rx_ch) {
1322 * Setup DMA to collect only valid data and enable error irqs.
1323 * This also enables break reception when using DMA.
1325 cr1 |= USART_CR1_PEIE;
1326 cr3 |= USART_CR3_EIE;
1327 cr3 |= USART_CR3_DMAR;
1328 cr3 |= USART_CR3_DDRE;
1331 if (stm32_port->tx_ch)
1332 cr3 |= USART_CR3_DMAT;
1334 if (rs485conf->flags & SER_RS485_ENABLED) {
1335 stm32_usart_config_reg_rs485(&cr1, &cr3,
1336 rs485conf->delay_rts_before_send,
1337 rs485conf->delay_rts_after_send,
1339 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
1340 cr3 &= ~USART_CR3_DEP;
1341 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1343 cr3 |= USART_CR3_DEP;
1344 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1348 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
1349 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1352 /* Configure wake up from low power on start bit detection */
1353 if (stm32_port->wakeup_src) {
1354 cr3 &= ~USART_CR3_WUS_MASK;
1355 cr3 |= USART_CR3_WUS_START_BIT;
1358 writel_relaxed(cr3, port->membase + ofs->cr3);
1359 writel_relaxed(cr2, port->membase + ofs->cr2);
1360 writel_relaxed(cr1, port->membase + ofs->cr1);
1362 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1363 uart_port_unlock_irqrestore(port, flags);
1365 /* Handle modem control interrupts */
1366 if (UART_ENABLE_MS(port, termios->c_cflag))
1367 stm32_usart_enable_ms(port);
1369 stm32_usart_disable_ms(port);
1372 static const char *stm32_usart_type(struct uart_port *port)
1374 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
1377 static void stm32_usart_release_port(struct uart_port *port)
1381 static int stm32_usart_request_port(struct uart_port *port)
1386 static void stm32_usart_config_port(struct uart_port *port, int flags)
1388 if (flags & UART_CONFIG_TYPE)
1389 port->type = PORT_STM32;
1393 stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
1395 /* No user changeable parameters */
1399 static void stm32_usart_pm(struct uart_port *port, unsigned int state,
1400 unsigned int oldstate)
1402 struct stm32_port *stm32port = container_of(port,
1403 struct stm32_port, port);
1404 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1405 const struct stm32_usart_config *cfg = &stm32port->info->cfg;
1406 unsigned long flags;
1409 case UART_PM_STATE_ON:
1410 pm_runtime_get_sync(port->dev);
1412 case UART_PM_STATE_OFF:
1413 uart_port_lock_irqsave(port, &flags);
1414 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1415 uart_port_unlock_irqrestore(port, flags);
1416 pm_runtime_put_sync(port->dev);
1421 #if defined(CONFIG_CONSOLE_POLL)
1423 /* Callbacks for characters polling in debug context (i.e. KGDB). */
1424 static int stm32_usart_poll_init(struct uart_port *port)
1426 struct stm32_port *stm32_port = to_stm32_port(port);
1428 return clk_prepare_enable(stm32_port->clk);
1431 static int stm32_usart_poll_get_char(struct uart_port *port)
1433 struct stm32_port *stm32_port = to_stm32_port(port);
1434 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1436 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE))
1437 return NO_POLL_CHAR;
1439 return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask;
1442 static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch)
1444 stm32_usart_console_putchar(port, ch);
1446 #endif /* CONFIG_CONSOLE_POLL */
1448 static const struct uart_ops stm32_uart_ops = {
1449 .tx_empty = stm32_usart_tx_empty,
1450 .set_mctrl = stm32_usart_set_mctrl,
1451 .get_mctrl = stm32_usart_get_mctrl,
1452 .stop_tx = stm32_usart_stop_tx,
1453 .start_tx = stm32_usart_start_tx,
1454 .throttle = stm32_usart_throttle,
1455 .unthrottle = stm32_usart_unthrottle,
1456 .stop_rx = stm32_usart_stop_rx,
1457 .enable_ms = stm32_usart_enable_ms,
1458 .break_ctl = stm32_usart_break_ctl,
1459 .startup = stm32_usart_startup,
1460 .shutdown = stm32_usart_shutdown,
1461 .flush_buffer = stm32_usart_flush_buffer,
1462 .set_termios = stm32_usart_set_termios,
1463 .pm = stm32_usart_pm,
1464 .type = stm32_usart_type,
1465 .release_port = stm32_usart_release_port,
1466 .request_port = stm32_usart_request_port,
1467 .config_port = stm32_usart_config_port,
1468 .verify_port = stm32_usart_verify_port,
1469 #if defined(CONFIG_CONSOLE_POLL)
1470 .poll_init = stm32_usart_poll_init,
1471 .poll_get_char = stm32_usart_poll_get_char,
1472 .poll_put_char = stm32_usart_poll_put_char,
1473 #endif /* CONFIG_CONSOLE_POLL */
1477 * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
1478 * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
1479 * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
1480 * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
1482 static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
1484 static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
1489 /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
1490 if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
1493 for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
1494 if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
1496 if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
1497 i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
1499 dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
1500 stm32h7_usart_fifo_thresh_cfg[i]);
1502 /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
1509 static void stm32_usart_deinit_port(struct stm32_port *stm32port)
1511 clk_disable_unprepare(stm32port->clk);
1514 static const struct serial_rs485 stm32_rs485_supported = {
1515 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
1516 SER_RS485_RX_DURING_TX,
1517 .delay_rts_before_send = 1,
1518 .delay_rts_after_send = 1,
1521 static int stm32_usart_init_port(struct stm32_port *stm32port,
1522 struct platform_device *pdev)
1524 struct uart_port *port = &stm32port->port;
1525 struct resource *res;
1528 irq = platform_get_irq(pdev, 0);
1532 port->iotype = UPIO_MEM;
1533 port->flags = UPF_BOOT_AUTOCONF;
1534 port->ops = &stm32_uart_ops;
1535 port->dev = &pdev->dev;
1536 port->fifosize = stm32port->info->cfg.fifosize;
1537 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1539 port->rs485_config = stm32_usart_config_rs485;
1540 port->rs485_supported = stm32_rs485_supported;
1542 ret = stm32_usart_init_rs485(port, pdev);
1546 stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
1547 of_property_read_bool(pdev->dev.of_node, "wakeup-source");
1549 stm32port->swap = stm32port->info->cfg.has_swap &&
1550 of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
1552 stm32port->fifoen = stm32port->info->cfg.has_fifo;
1553 if (stm32port->fifoen) {
1554 stm32_usart_get_ftcfg(pdev, "rx-threshold",
1555 &stm32port->rxftcfg);
1556 stm32_usart_get_ftcfg(pdev, "tx-threshold",
1557 &stm32port->txftcfg);
1560 port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1561 if (IS_ERR(port->membase))
1562 return PTR_ERR(port->membase);
1563 port->mapbase = res->start;
1565 spin_lock_init(&port->lock);
1567 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
1568 if (IS_ERR(stm32port->clk))
1569 return PTR_ERR(stm32port->clk);
1571 /* Ensure that clk rate is correct by enabling the clk */
1572 ret = clk_prepare_enable(stm32port->clk);
1576 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1577 if (!stm32port->port.uartclk) {
1582 stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
1583 if (IS_ERR(stm32port->gpios)) {
1584 ret = PTR_ERR(stm32port->gpios);
1589 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
1590 * properties should not be specified.
1592 if (stm32port->hw_flow_control) {
1593 if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
1594 mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
1595 dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
1604 clk_disable_unprepare(stm32port->clk);
1609 static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
1611 struct device_node *np = pdev->dev.of_node;
1617 id = of_alias_get_id(np, "serial");
1619 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1623 if (WARN_ON(id >= STM32_MAX_PORTS))
1626 stm32_ports[id].hw_flow_control =
1627 of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
1628 of_property_read_bool (np, "uart-has-rtscts");
1629 stm32_ports[id].port.line = id;
1630 stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1631 stm32_ports[id].cr3_irq = 0;
1632 stm32_ports[id].last_res = RX_BUF_L;
1633 return &stm32_ports[id];
1637 static const struct of_device_id stm32_match[] = {
1638 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
1639 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1640 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
1644 MODULE_DEVICE_TABLE(of, stm32_match);
1647 static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1648 struct platform_device *pdev)
1650 if (stm32port->rx_buf)
1651 dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1652 stm32port->rx_dma_buf);
1655 static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
1656 struct platform_device *pdev)
1658 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1659 struct uart_port *port = &stm32port->port;
1660 struct device *dev = &pdev->dev;
1661 struct dma_slave_config config;
1664 stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
1665 &stm32port->rx_dma_buf,
1667 if (!stm32port->rx_buf)
1670 /* Configure DMA channel */
1671 memset(&config, 0, sizeof(config));
1672 config.src_addr = port->mapbase + ofs->rdr;
1673 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1675 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
1677 dev_err(dev, "rx dma channel config failed\n");
1678 stm32_usart_of_dma_rx_remove(stm32port, pdev);
1685 static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1686 struct platform_device *pdev)
1688 if (stm32port->tx_buf)
1689 dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1690 stm32port->tx_dma_buf);
1693 static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
1694 struct platform_device *pdev)
1696 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1697 struct uart_port *port = &stm32port->port;
1698 struct device *dev = &pdev->dev;
1699 struct dma_slave_config config;
1702 stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
1703 &stm32port->tx_dma_buf,
1705 if (!stm32port->tx_buf)
1708 /* Configure DMA channel */
1709 memset(&config, 0, sizeof(config));
1710 config.dst_addr = port->mapbase + ofs->tdr;
1711 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1713 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1715 dev_err(dev, "tx dma channel config failed\n");
1716 stm32_usart_of_dma_tx_remove(stm32port, pdev);
1723 static int stm32_usart_serial_probe(struct platform_device *pdev)
1725 struct stm32_port *stm32port;
1728 stm32port = stm32_usart_of_get_port(pdev);
1732 stm32port->info = of_device_get_match_data(&pdev->dev);
1733 if (!stm32port->info)
1736 stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
1737 if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER)
1738 return -EPROBE_DEFER;
1740 /* Fall back in interrupt mode for any non-deferral error */
1741 if (IS_ERR(stm32port->rx_ch))
1742 stm32port->rx_ch = NULL;
1744 stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1745 if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1746 ret = -EPROBE_DEFER;
1749 /* Fall back in interrupt mode for any non-deferral error */
1750 if (IS_ERR(stm32port->tx_ch))
1751 stm32port->tx_ch = NULL;
1753 ret = stm32_usart_init_port(stm32port, pdev);
1757 if (stm32port->wakeup_src) {
1758 device_set_wakeup_capable(&pdev->dev, true);
1759 ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
1761 goto err_deinit_port;
1764 if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1765 /* Fall back in interrupt mode */
1766 dma_release_channel(stm32port->rx_ch);
1767 stm32port->rx_ch = NULL;
1770 if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1771 /* Fall back in interrupt mode */
1772 dma_release_channel(stm32port->tx_ch);
1773 stm32port->tx_ch = NULL;
1776 if (!stm32port->rx_ch)
1777 dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1778 if (!stm32port->tx_ch)
1779 dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
1781 platform_set_drvdata(pdev, &stm32port->port);
1783 pm_runtime_get_noresume(&pdev->dev);
1784 pm_runtime_set_active(&pdev->dev);
1785 pm_runtime_enable(&pdev->dev);
1787 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1791 pm_runtime_put_sync(&pdev->dev);
1796 pm_runtime_disable(&pdev->dev);
1797 pm_runtime_set_suspended(&pdev->dev);
1798 pm_runtime_put_noidle(&pdev->dev);
1800 if (stm32port->tx_ch)
1801 stm32_usart_of_dma_tx_remove(stm32port, pdev);
1802 if (stm32port->rx_ch)
1803 stm32_usart_of_dma_rx_remove(stm32port, pdev);
1805 if (stm32port->wakeup_src)
1806 dev_pm_clear_wake_irq(&pdev->dev);
1809 if (stm32port->wakeup_src)
1810 device_set_wakeup_capable(&pdev->dev, false);
1812 stm32_usart_deinit_port(stm32port);
1815 if (stm32port->tx_ch)
1816 dma_release_channel(stm32port->tx_ch);
1819 if (stm32port->rx_ch)
1820 dma_release_channel(stm32port->rx_ch);
1825 static int stm32_usart_serial_remove(struct platform_device *pdev)
1827 struct uart_port *port = platform_get_drvdata(pdev);
1828 struct stm32_port *stm32_port = to_stm32_port(port);
1829 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1832 pm_runtime_get_sync(&pdev->dev);
1833 uart_remove_one_port(&stm32_usart_driver, port);
1835 pm_runtime_disable(&pdev->dev);
1836 pm_runtime_set_suspended(&pdev->dev);
1837 pm_runtime_put_noidle(&pdev->dev);
1839 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
1841 if (stm32_port->tx_ch) {
1842 stm32_usart_of_dma_tx_remove(stm32_port, pdev);
1843 dma_release_channel(stm32_port->tx_ch);
1846 if (stm32_port->rx_ch) {
1847 stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1848 dma_release_channel(stm32_port->rx_ch);
1851 cr3 = readl_relaxed(port->membase + ofs->cr3);
1852 cr3 &= ~USART_CR3_EIE;
1853 cr3 &= ~USART_CR3_DMAR;
1854 cr3 &= ~USART_CR3_DMAT;
1855 cr3 &= ~USART_CR3_DDRE;
1856 writel_relaxed(cr3, port->membase + ofs->cr3);
1858 if (stm32_port->wakeup_src) {
1859 dev_pm_clear_wake_irq(&pdev->dev);
1860 device_init_wakeup(&pdev->dev, false);
1863 stm32_usart_deinit_port(stm32_port);
1868 static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
1870 struct stm32_port *stm32_port = to_stm32_port(port);
1871 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1875 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr,
1876 (isr & USART_SR_TXE), 100,
1877 STM32_USART_TIMEOUT_USEC);
1879 dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret);
1882 writel_relaxed(ch, port->membase + ofs->tdr);
1885 #ifdef CONFIG_SERIAL_STM32_CONSOLE
1886 static void stm32_usart_console_write(struct console *co, const char *s,
1889 struct uart_port *port = &stm32_ports[co->index].port;
1890 struct stm32_port *stm32_port = to_stm32_port(port);
1891 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1892 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1893 unsigned long flags;
1894 u32 old_cr1, new_cr1;
1897 if (oops_in_progress)
1898 locked = uart_port_trylock_irqsave(port, &flags);
1900 uart_port_lock_irqsave(port, &flags);
1902 /* Save and disable interrupts, enable the transmitter */
1903 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1904 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1905 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
1906 writel_relaxed(new_cr1, port->membase + ofs->cr1);
1908 uart_console_write(port, s, cnt, stm32_usart_console_putchar);
1910 /* Restore interrupt state */
1911 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1914 uart_port_unlock_irqrestore(port, flags);
1917 static int stm32_usart_console_setup(struct console *co, char *options)
1919 struct stm32_port *stm32port;
1925 if (co->index >= STM32_MAX_PORTS)
1928 stm32port = &stm32_ports[co->index];
1931 * This driver does not support early console initialization
1932 * (use ARM early printk support instead), so we only expect
1933 * this to be called during the uart port registration when the
1934 * driver gets probed and the port should be mapped at that point.
1936 if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
1940 uart_parse_options(options, &baud, &parity, &bits, &flow);
1942 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1945 static struct console stm32_console = {
1946 .name = STM32_SERIAL_NAME,
1947 .device = uart_console_device,
1948 .write = stm32_usart_console_write,
1949 .setup = stm32_usart_console_setup,
1950 .flags = CON_PRINTBUFFER,
1952 .data = &stm32_usart_driver,
1955 #define STM32_SERIAL_CONSOLE (&stm32_console)
1958 #define STM32_SERIAL_CONSOLE NULL
1959 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
1961 #ifdef CONFIG_SERIAL_EARLYCON
1962 static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
1964 struct stm32_usart_info *info = port->private_data;
1966 while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE))
1969 writel_relaxed(ch, port->membase + info->ofs.tdr);
1972 static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count)
1974 struct earlycon_device *device = console->data;
1975 struct uart_port *port = &device->port;
1977 uart_console_write(port, s, count, early_stm32_usart_console_putchar);
1980 static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
1982 if (!(device->port.membase || device->port.iobase))
1984 device->port.private_data = &stm32h7_info;
1985 device->con->write = early_stm32_serial_write;
1989 static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
1991 if (!(device->port.membase || device->port.iobase))
1993 device->port.private_data = &stm32f7_info;
1994 device->con->write = early_stm32_serial_write;
1998 static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
2000 if (!(device->port.membase || device->port.iobase))
2002 device->port.private_data = &stm32f4_info;
2003 device->con->write = early_stm32_serial_write;
2007 OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup);
2008 OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup);
2009 OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup);
2010 #endif /* CONFIG_SERIAL_EARLYCON */
2012 static struct uart_driver stm32_usart_driver = {
2013 .driver_name = DRIVER_NAME,
2014 .dev_name = STM32_SERIAL_NAME,
2017 .nr = STM32_MAX_PORTS,
2018 .cons = STM32_SERIAL_CONSOLE,
2021 static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
2024 struct stm32_port *stm32_port = to_stm32_port(port);
2025 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
2026 struct tty_port *tport = &port->state->port;
2028 unsigned int size = 0;
2029 unsigned long flags;
2031 if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
2035 * Enable low-power wake-up and wake-up irq if argument is set to
2036 * "enable", disable low-power wake-up and wake-up irq otherwise
2039 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
2040 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
2041 mctrl_gpio_enable_irq_wake(stm32_port->gpios);
2044 * When DMA is used for reception, it must be disabled before
2045 * entering low-power mode and re-enabled when exiting from
2048 if (stm32_port->rx_ch) {
2049 uart_port_lock_irqsave(port, &flags);
2050 /* Poll data from DMA RX buffer if any */
2051 if (!stm32_usart_rx_dma_pause(stm32_port))
2052 size += stm32_usart_receive_chars(port, true);
2053 stm32_usart_rx_dma_terminate(stm32_port);
2054 uart_unlock_and_check_sysrq_irqrestore(port, flags);
2056 tty_flip_buffer_push(tport);
2059 /* Poll data from RX FIFO if any */
2060 stm32_usart_receive_chars(port, false);
2062 if (stm32_port->rx_ch) {
2063 ret = stm32_usart_rx_dma_start_or_resume(port);
2067 mctrl_gpio_disable_irq_wake(stm32_port->gpios);
2068 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
2069 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
2075 static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
2077 struct uart_port *port = dev_get_drvdata(dev);
2080 uart_suspend_port(&stm32_usart_driver, port);
2082 if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
2083 ret = stm32_usart_serial_en_wakeup(port, true);
2089 * When "no_console_suspend" is enabled, keep the pinctrl default state
2090 * and rely on bootloader stage to restore this state upon resume.
2091 * Otherwise, apply the idle or sleep states depending on wakeup
2094 if (console_suspend_enabled || !uart_console(port)) {
2095 if (device_may_wakeup(dev) || device_wakeup_path(dev))
2096 pinctrl_pm_select_idle_state(dev);
2098 pinctrl_pm_select_sleep_state(dev);
2104 static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
2106 struct uart_port *port = dev_get_drvdata(dev);
2109 pinctrl_pm_select_default_state(dev);
2111 if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
2112 ret = stm32_usart_serial_en_wakeup(port, false);
2117 return uart_resume_port(&stm32_usart_driver, port);
2120 static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
2122 struct uart_port *port = dev_get_drvdata(dev);
2123 struct stm32_port *stm32port = container_of(port,
2124 struct stm32_port, port);
2126 clk_disable_unprepare(stm32port->clk);
2131 static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
2133 struct uart_port *port = dev_get_drvdata(dev);
2134 struct stm32_port *stm32port = container_of(port,
2135 struct stm32_port, port);
2137 return clk_prepare_enable(stm32port->clk);
2140 static const struct dev_pm_ops stm32_serial_pm_ops = {
2141 SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
2142 stm32_usart_runtime_resume, NULL)
2143 SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
2144 stm32_usart_serial_resume)
2147 static struct platform_driver stm32_serial_driver = {
2148 .probe = stm32_usart_serial_probe,
2149 .remove = stm32_usart_serial_remove,
2151 .name = DRIVER_NAME,
2152 .pm = &stm32_serial_pm_ops,
2153 .of_match_table = of_match_ptr(stm32_match),
2157 static int __init stm32_usart_init(void)
2159 static char banner[] __initdata = "STM32 USART driver initialized";
2162 pr_info("%s\n", banner);
2164 ret = uart_register_driver(&stm32_usart_driver);
2168 ret = platform_driver_register(&stm32_serial_driver);
2170 uart_unregister_driver(&stm32_usart_driver);
2175 static void __exit stm32_usart_exit(void)
2177 platform_driver_unregister(&stm32_serial_driver);
2178 uart_unregister_driver(&stm32_usart_driver);
2181 module_init(stm32_usart_init);
2182 module_exit(stm32_usart_exit);
2184 MODULE_ALIAS("platform:" DRIVER_NAME);
2185 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
2186 MODULE_LICENSE("GPL v2");