1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Maxime Coquelin 2015
4 * Copyright (C) STMicroelectronics SA 2017
5 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
6 * Gerald Baeza <gerald.baeza@st.com>
8 * Inspired by st-asc.c from STMicroelectronics (c)
11 #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #include <linux/clk.h>
16 #include <linux/console.h>
17 #include <linux/delay.h>
18 #include <linux/dma-direction.h>
19 #include <linux/dmaengine.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/iopoll.h>
23 #include <linux/irq.h>
24 #include <linux/module.h>
26 #include <linux/of_platform.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/pm_wakeirq.h>
30 #include <linux/serial_core.h>
31 #include <linux/serial.h>
32 #include <linux/spinlock.h>
33 #include <linux/sysrq.h>
34 #include <linux/tty_flip.h>
35 #include <linux/tty.h>
37 #include "stm32-usart.h"
39 static void stm32_stop_tx(struct uart_port *port);
40 static void stm32_transmit_chars(struct uart_port *port);
42 static inline struct stm32_port *to_stm32_port(struct uart_port *port)
44 return container_of(port, struct stm32_port, port);
47 static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
51 val = readl_relaxed(port->membase + reg);
53 writel_relaxed(val, port->membase + reg);
56 static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
60 val = readl_relaxed(port->membase + reg);
62 writel_relaxed(val, port->membase + reg);
65 static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
66 u32 delay_DDE, u32 baud)
69 u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
72 *cr3 |= USART_CR3_DEM;
73 over8 = *cr1 & USART_CR1_OVER8;
75 *cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
78 rs485_deat_dedt = delay_ADE * baud * 8;
80 rs485_deat_dedt = delay_ADE * baud * 16;
82 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
83 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
84 rs485_deat_dedt_max : rs485_deat_dedt;
85 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
87 *cr1 |= rs485_deat_dedt;
90 rs485_deat_dedt = delay_DDE * baud * 8;
92 rs485_deat_dedt = delay_DDE * baud * 16;
94 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
95 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
96 rs485_deat_dedt_max : rs485_deat_dedt;
97 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
99 *cr1 |= rs485_deat_dedt;
102 static int stm32_config_rs485(struct uart_port *port,
103 struct serial_rs485 *rs485conf)
105 struct stm32_port *stm32_port = to_stm32_port(port);
106 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
107 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
108 u32 usartdiv, baud, cr1, cr3;
111 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
113 port->rs485 = *rs485conf;
115 rs485conf->flags |= SER_RS485_RX_DURING_TX;
117 if (rs485conf->flags & SER_RS485_ENABLED) {
118 cr1 = readl_relaxed(port->membase + ofs->cr1);
119 cr3 = readl_relaxed(port->membase + ofs->cr3);
120 usartdiv = readl_relaxed(port->membase + ofs->brr);
121 usartdiv = usartdiv & GENMASK(15, 0);
122 over8 = cr1 & USART_CR1_OVER8;
125 usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
126 << USART_BRR_04_R_SHIFT;
128 baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
129 stm32_config_reg_rs485(&cr1, &cr3,
130 rs485conf->delay_rts_before_send,
131 rs485conf->delay_rts_after_send, baud);
133 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
134 cr3 &= ~USART_CR3_DEP;
135 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
137 cr3 |= USART_CR3_DEP;
138 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
141 writel_relaxed(cr3, port->membase + ofs->cr3);
142 writel_relaxed(cr1, port->membase + ofs->cr1);
144 stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP);
145 stm32_clr_bits(port, ofs->cr1,
146 USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
149 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
154 static int stm32_init_rs485(struct uart_port *port,
155 struct platform_device *pdev)
157 struct serial_rs485 *rs485conf = &port->rs485;
159 rs485conf->flags = 0;
160 rs485conf->delay_rts_before_send = 0;
161 rs485conf->delay_rts_after_send = 0;
163 if (!pdev->dev.of_node)
166 uart_get_rs485_mode(&pdev->dev, rs485conf);
171 static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
174 struct stm32_port *stm32_port = to_stm32_port(port);
175 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
176 enum dma_status status;
177 struct dma_tx_state state;
179 *sr = readl_relaxed(port->membase + ofs->isr);
181 if (threaded && stm32_port->rx_ch) {
182 status = dmaengine_tx_status(stm32_port->rx_ch,
183 stm32_port->rx_ch->cookie,
185 if ((status == DMA_IN_PROGRESS) &&
186 (*last_res != state.residue))
190 } else if (*sr & USART_SR_RXNE) {
196 static unsigned long stm32_get_char(struct uart_port *port, u32 *sr,
199 struct stm32_port *stm32_port = to_stm32_port(port);
200 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
203 if (stm32_port->rx_ch) {
204 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
205 if ((*last_res) == 0)
206 *last_res = RX_BUF_L;
208 c = readl_relaxed(port->membase + ofs->rdr);
209 /* apply RDR data mask */
210 c &= stm32_port->rdr_mask;
216 static void stm32_receive_chars(struct uart_port *port, bool threaded)
218 struct tty_port *tport = &port->state->port;
219 struct stm32_port *stm32_port = to_stm32_port(port);
220 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
225 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
226 pm_wakeup_event(tport->tty->dev, 0);
228 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
229 sr |= USART_SR_DUMMY_RX;
233 * Status bits has to be cleared before reading the RDR:
234 * In FIFO mode, reading the RDR will pop the next data
235 * (if any) along with its status bits into the SR.
236 * Not doing so leads to misalignement between RDR and SR,
237 * and clear status bits of the next rx data.
239 * Clear errors flags for stm32f7 and stm32h7 compatible
240 * devices. On stm32f4 compatible devices, the error bit is
241 * cleared by the sequence [read SR - read DR].
243 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
244 writel_relaxed(sr & USART_SR_ERR_MASK,
245 port->membase + ofs->icr);
247 c = stm32_get_char(port, &sr, &stm32_port->last_res);
249 if (sr & USART_SR_ERR_MASK) {
250 if (sr & USART_SR_ORE) {
251 port->icount.overrun++;
252 } else if (sr & USART_SR_PE) {
253 port->icount.parity++;
254 } else if (sr & USART_SR_FE) {
255 /* Break detection if character is null */
258 if (uart_handle_break(port))
261 port->icount.frame++;
265 sr &= port->read_status_mask;
267 if (sr & USART_SR_PE) {
269 } else if (sr & USART_SR_FE) {
277 if (uart_handle_sysrq_char(port, c))
279 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
282 spin_unlock(&port->lock);
283 tty_flip_buffer_push(tport);
284 spin_lock(&port->lock);
287 static void stm32_tx_dma_complete(void *arg)
289 struct uart_port *port = arg;
290 struct stm32_port *stm32port = to_stm32_port(port);
291 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
293 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
294 stm32port->tx_dma_busy = false;
296 /* Let's see if we have pending data to send */
297 stm32_transmit_chars(port);
300 static void stm32_transmit_chars_pio(struct uart_port *port)
302 struct stm32_port *stm32_port = to_stm32_port(port);
303 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
304 struct circ_buf *xmit = &port->state->xmit;
308 if (stm32_port->tx_dma_busy) {
309 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
310 stm32_port->tx_dma_busy = false;
313 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
315 (isr & USART_SR_TXE),
319 dev_err(port->dev, "tx empty not set\n");
321 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
323 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
324 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
328 static void stm32_transmit_chars_dma(struct uart_port *port)
330 struct stm32_port *stm32port = to_stm32_port(port);
331 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
332 struct circ_buf *xmit = &port->state->xmit;
333 struct dma_async_tx_descriptor *desc = NULL;
335 unsigned int count, i;
337 if (stm32port->tx_dma_busy)
340 stm32port->tx_dma_busy = true;
342 count = uart_circ_chars_pending(xmit);
344 if (count > TX_BUF_L)
347 if (xmit->tail < xmit->head) {
348 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
350 size_t one = UART_XMIT_SIZE - xmit->tail;
357 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
359 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
362 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
363 stm32port->tx_dma_buf,
369 for (i = count; i > 0; i--)
370 stm32_transmit_chars_pio(port);
374 desc->callback = stm32_tx_dma_complete;
375 desc->callback_param = port;
377 /* Push current DMA TX transaction in the pending queue */
378 cookie = dmaengine_submit(desc);
380 /* Issue pending DMA TX requests */
381 dma_async_issue_pending(stm32port->tx_ch);
383 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
385 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
386 port->icount.tx += count;
389 static void stm32_transmit_chars(struct uart_port *port)
391 struct stm32_port *stm32_port = to_stm32_port(port);
392 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
393 struct circ_buf *xmit = &port->state->xmit;
396 if (stm32_port->tx_dma_busy)
397 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
398 writel_relaxed(port->x_char, port->membase + ofs->tdr);
401 if (stm32_port->tx_dma_busy)
402 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
406 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
407 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
411 if (ofs->icr == UNDEF_REG)
412 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
414 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
416 if (stm32_port->tx_ch)
417 stm32_transmit_chars_dma(port);
419 stm32_transmit_chars_pio(port);
421 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
422 uart_write_wakeup(port);
424 if (uart_circ_empty(xmit))
425 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
428 static irqreturn_t stm32_interrupt(int irq, void *ptr)
430 struct uart_port *port = ptr;
431 struct stm32_port *stm32_port = to_stm32_port(port);
432 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
435 spin_lock(&port->lock);
437 sr = readl_relaxed(port->membase + ofs->isr);
439 if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
440 writel_relaxed(USART_ICR_WUCF,
441 port->membase + ofs->icr);
443 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
444 stm32_receive_chars(port, false);
446 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
447 stm32_transmit_chars(port);
449 spin_unlock(&port->lock);
451 if (stm32_port->rx_ch)
452 return IRQ_WAKE_THREAD;
457 static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
459 struct uart_port *port = ptr;
460 struct stm32_port *stm32_port = to_stm32_port(port);
462 spin_lock(&port->lock);
464 if (stm32_port->rx_ch)
465 stm32_receive_chars(port, true);
467 spin_unlock(&port->lock);
472 static unsigned int stm32_tx_empty(struct uart_port *port)
474 struct stm32_port *stm32_port = to_stm32_port(port);
475 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
477 if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
483 static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
485 struct stm32_port *stm32_port = to_stm32_port(port);
486 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
488 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
489 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
491 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
494 static unsigned int stm32_get_mctrl(struct uart_port *port)
496 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
497 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
501 static void stm32_stop_tx(struct uart_port *port)
503 struct stm32_port *stm32_port = to_stm32_port(port);
504 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
506 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
509 /* There are probably characters waiting to be transmitted. */
510 static void stm32_start_tx(struct uart_port *port)
512 struct circ_buf *xmit = &port->state->xmit;
514 if (uart_circ_empty(xmit) && !port->x_char)
517 stm32_transmit_chars(port);
520 /* Throttle the remote when input buffer is about to overflow. */
521 static void stm32_throttle(struct uart_port *port)
523 struct stm32_port *stm32_port = to_stm32_port(port);
524 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
527 spin_lock_irqsave(&port->lock, flags);
528 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
529 spin_unlock_irqrestore(&port->lock, flags);
532 /* Unthrottle the remote, the input buffer can now accept data. */
533 static void stm32_unthrottle(struct uart_port *port)
535 struct stm32_port *stm32_port = to_stm32_port(port);
536 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
539 spin_lock_irqsave(&port->lock, flags);
540 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
541 spin_unlock_irqrestore(&port->lock, flags);
545 static void stm32_stop_rx(struct uart_port *port)
547 struct stm32_port *stm32_port = to_stm32_port(port);
548 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
550 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
553 /* Handle breaks - ignored by us */
554 static void stm32_break_ctl(struct uart_port *port, int break_state)
558 static int stm32_startup(struct uart_port *port)
560 struct stm32_port *stm32_port = to_stm32_port(port);
561 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
562 const char *name = to_platform_device(port->dev)->name;
566 ret = request_threaded_irq(port->irq, stm32_interrupt,
567 stm32_threaded_interrupt,
568 IRQF_NO_SUSPEND, name, port);
572 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
573 if (stm32_port->fifoen)
574 val |= USART_CR1_FIFOEN;
575 stm32_set_bits(port, ofs->cr1, val);
580 static void stm32_shutdown(struct uart_port *port)
582 struct stm32_port *stm32_port = to_stm32_port(port);
583 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
584 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
588 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
589 val |= BIT(cfg->uart_enable_bit);
590 if (stm32_port->fifoen)
591 val |= USART_CR1_FIFOEN;
593 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
594 isr, (isr & USART_SR_TC),
598 dev_err(port->dev, "transmission complete not set\n");
600 stm32_clr_bits(port, ofs->cr1, val);
602 free_irq(port->irq, port);
605 unsigned int stm32_get_databits(struct ktermios *termios)
609 tcflag_t cflag = termios->c_cflag;
611 switch (cflag & CSIZE) {
613 * CSIZE settings are not necessarily supported in hardware.
614 * CSIZE unsupported configurations are handled here to set word length
615 * to 8 bits word as default configuration and to print debug message.
626 /* default including CS8 */
635 static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
636 struct ktermios *old)
638 struct stm32_port *stm32_port = to_stm32_port(port);
639 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
640 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
641 struct serial_rs485 *rs485conf = &port->rs485;
642 unsigned int baud, bits;
643 u32 usartdiv, mantissa, fraction, oversampling;
644 tcflag_t cflag = termios->c_cflag;
645 u32 cr1, cr2, cr3, isr;
649 if (!stm32_port->hw_flow_control)
652 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
654 spin_lock_irqsave(&port->lock, flags);
656 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
661 /* Send the TC error message only when ISR_TC is not set. */
663 dev_err(port->dev, "Transmission is not complete\n");
665 /* Stop serial port and reset value */
666 writel_relaxed(0, port->membase + ofs->cr1);
668 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
670 if (stm32_port->fifoen)
671 cr1 |= USART_CR1_FIFOEN;
676 cr2 |= USART_CR2_STOP_2B;
678 bits = stm32_get_databits(termios);
679 stm32_port->rdr_mask = (BIT(bits) - 1);
681 if (cflag & PARENB) {
683 cr1 |= USART_CR1_PCE;
687 * Word length configuration:
688 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
689 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
690 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
691 * M0 and M1 already cleared by cr1 initialization.
695 } else if ((bits == 7) && cfg->has_7bits_data) {
697 } else if (bits != 8) {
698 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
702 termios->c_cflag = cflag;
704 if (cflag & PARENB) {
713 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
714 if (cflag & CRTSCTS) {
715 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
716 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
719 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
722 * The USART supports 16 or 8 times oversampling.
723 * By default we prefer 16 times oversampling, so that the receiver
724 * has a better tolerance to clock deviations.
725 * 8 times oversampling is only used to achieve higher speeds.
729 cr1 |= USART_CR1_OVER8;
730 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
733 cr1 &= ~USART_CR1_OVER8;
734 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
737 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
738 fraction = usartdiv % oversampling;
739 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
741 uart_update_timeout(port, cflag, baud);
743 port->read_status_mask = USART_SR_ORE;
744 if (termios->c_iflag & INPCK)
745 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
746 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
747 port->read_status_mask |= USART_SR_FE;
749 /* Characters to ignore */
750 port->ignore_status_mask = 0;
751 if (termios->c_iflag & IGNPAR)
752 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
753 if (termios->c_iflag & IGNBRK) {
754 port->ignore_status_mask |= USART_SR_FE;
756 * If we're ignoring parity and break indicators,
757 * ignore overruns too (for real raw support).
759 if (termios->c_iflag & IGNPAR)
760 port->ignore_status_mask |= USART_SR_ORE;
763 /* Ignore all characters if CREAD is not set */
764 if ((termios->c_cflag & CREAD) == 0)
765 port->ignore_status_mask |= USART_SR_DUMMY_RX;
767 if (stm32_port->rx_ch)
768 cr3 |= USART_CR3_DMAR;
770 if (rs485conf->flags & SER_RS485_ENABLED) {
771 stm32_config_reg_rs485(&cr1, &cr3,
772 rs485conf->delay_rts_before_send,
773 rs485conf->delay_rts_after_send, baud);
774 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
775 cr3 &= ~USART_CR3_DEP;
776 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
778 cr3 |= USART_CR3_DEP;
779 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
783 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
784 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
787 writel_relaxed(cr3, port->membase + ofs->cr3);
788 writel_relaxed(cr2, port->membase + ofs->cr2);
789 writel_relaxed(cr1, port->membase + ofs->cr1);
791 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
792 spin_unlock_irqrestore(&port->lock, flags);
795 static const char *stm32_type(struct uart_port *port)
797 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
800 static void stm32_release_port(struct uart_port *port)
804 static int stm32_request_port(struct uart_port *port)
809 static void stm32_config_port(struct uart_port *port, int flags)
811 if (flags & UART_CONFIG_TYPE)
812 port->type = PORT_STM32;
816 stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
818 /* No user changeable parameters */
822 static void stm32_pm(struct uart_port *port, unsigned int state,
823 unsigned int oldstate)
825 struct stm32_port *stm32port = container_of(port,
826 struct stm32_port, port);
827 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
828 struct stm32_usart_config *cfg = &stm32port->info->cfg;
829 unsigned long flags = 0;
832 case UART_PM_STATE_ON:
833 clk_prepare_enable(stm32port->clk);
835 case UART_PM_STATE_OFF:
836 spin_lock_irqsave(&port->lock, flags);
837 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
838 spin_unlock_irqrestore(&port->lock, flags);
839 clk_disable_unprepare(stm32port->clk);
844 static const struct uart_ops stm32_uart_ops = {
845 .tx_empty = stm32_tx_empty,
846 .set_mctrl = stm32_set_mctrl,
847 .get_mctrl = stm32_get_mctrl,
848 .stop_tx = stm32_stop_tx,
849 .start_tx = stm32_start_tx,
850 .throttle = stm32_throttle,
851 .unthrottle = stm32_unthrottle,
852 .stop_rx = stm32_stop_rx,
853 .break_ctl = stm32_break_ctl,
854 .startup = stm32_startup,
855 .shutdown = stm32_shutdown,
856 .set_termios = stm32_set_termios,
859 .release_port = stm32_release_port,
860 .request_port = stm32_request_port,
861 .config_port = stm32_config_port,
862 .verify_port = stm32_verify_port,
865 static int stm32_init_port(struct stm32_port *stm32port,
866 struct platform_device *pdev)
868 struct uart_port *port = &stm32port->port;
869 struct resource *res;
872 port->iotype = UPIO_MEM;
873 port->flags = UPF_BOOT_AUTOCONF;
874 port->ops = &stm32_uart_ops;
875 port->dev = &pdev->dev;
876 port->irq = platform_get_irq(pdev, 0);
877 port->rs485_config = stm32_config_rs485;
879 stm32_init_rs485(port, pdev);
881 stm32port->wakeirq = platform_get_irq(pdev, 1);
882 stm32port->fifoen = stm32port->info->cfg.has_fifo;
884 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
885 port->membase = devm_ioremap_resource(&pdev->dev, res);
886 if (IS_ERR(port->membase))
887 return PTR_ERR(port->membase);
888 port->mapbase = res->start;
890 spin_lock_init(&port->lock);
892 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
893 if (IS_ERR(stm32port->clk))
894 return PTR_ERR(stm32port->clk);
896 /* Ensure that clk rate is correct by enabling the clk */
897 ret = clk_prepare_enable(stm32port->clk);
901 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
902 if (!stm32port->port.uartclk) {
903 clk_disable_unprepare(stm32port->clk);
910 static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
912 struct device_node *np = pdev->dev.of_node;
918 id = of_alias_get_id(np, "serial");
920 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
924 if (WARN_ON(id >= STM32_MAX_PORTS))
927 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
929 stm32_ports[id].port.line = id;
930 stm32_ports[id].last_res = RX_BUF_L;
931 return &stm32_ports[id];
935 static const struct of_device_id stm32_match[] = {
936 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
937 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
938 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
942 MODULE_DEVICE_TABLE(of, stm32_match);
945 static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
946 struct platform_device *pdev)
948 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
949 struct uart_port *port = &stm32port->port;
950 struct device *dev = &pdev->dev;
951 struct dma_slave_config config;
952 struct dma_async_tx_descriptor *desc = NULL;
956 /* Request DMA RX channel */
957 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
958 if (!stm32port->rx_ch) {
959 dev_info(dev, "rx dma alloc failed\n");
962 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
963 &stm32port->rx_dma_buf,
965 if (!stm32port->rx_buf) {
970 /* Configure DMA channel */
971 memset(&config, 0, sizeof(config));
972 config.src_addr = port->mapbase + ofs->rdr;
973 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
975 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
977 dev_err(dev, "rx dma channel config failed\n");
982 /* Prepare a DMA cyclic transaction */
983 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
984 stm32port->rx_dma_buf,
985 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
988 dev_err(dev, "rx dma prep cyclic failed\n");
993 /* No callback as dma buffer is drained on usart interrupt */
994 desc->callback = NULL;
995 desc->callback_param = NULL;
997 /* Push current DMA transaction in the pending queue */
998 cookie = dmaengine_submit(desc);
1000 /* Issue pending DMA requests */
1001 dma_async_issue_pending(stm32port->rx_ch);
1006 dma_free_coherent(&pdev->dev,
1007 RX_BUF_L, stm32port->rx_buf,
1008 stm32port->rx_dma_buf);
1011 dma_release_channel(stm32port->rx_ch);
1012 stm32port->rx_ch = NULL;
1017 static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
1018 struct platform_device *pdev)
1020 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1021 struct uart_port *port = &stm32port->port;
1022 struct device *dev = &pdev->dev;
1023 struct dma_slave_config config;
1026 stm32port->tx_dma_busy = false;
1028 /* Request DMA TX channel */
1029 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
1030 if (!stm32port->tx_ch) {
1031 dev_info(dev, "tx dma alloc failed\n");
1034 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
1035 &stm32port->tx_dma_buf,
1037 if (!stm32port->tx_buf) {
1042 /* Configure DMA channel */
1043 memset(&config, 0, sizeof(config));
1044 config.dst_addr = port->mapbase + ofs->tdr;
1045 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1047 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1049 dev_err(dev, "tx dma channel config failed\n");
1057 dma_free_coherent(&pdev->dev,
1058 TX_BUF_L, stm32port->tx_buf,
1059 stm32port->tx_dma_buf);
1062 dma_release_channel(stm32port->tx_ch);
1063 stm32port->tx_ch = NULL;
1068 static int stm32_serial_probe(struct platform_device *pdev)
1070 const struct of_device_id *match;
1071 struct stm32_port *stm32port;
1074 stm32port = stm32_of_get_stm32_port(pdev);
1078 match = of_match_device(stm32_match, &pdev->dev);
1079 if (match && match->data)
1080 stm32port->info = (struct stm32_usart_info *)match->data;
1084 ret = stm32_init_port(stm32port, pdev);
1088 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) {
1089 ret = device_init_wakeup(&pdev->dev, true);
1093 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1094 stm32port->wakeirq);
1098 device_set_wakeup_enable(&pdev->dev, false);
1101 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1105 ret = stm32_of_dma_rx_probe(stm32port, pdev);
1107 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
1109 ret = stm32_of_dma_tx_probe(stm32port, pdev);
1111 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
1113 platform_set_drvdata(pdev, &stm32port->port);
1118 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
1119 dev_pm_clear_wake_irq(&pdev->dev);
1122 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
1123 device_init_wakeup(&pdev->dev, false);
1126 clk_disable_unprepare(stm32port->clk);
1131 static int stm32_serial_remove(struct platform_device *pdev)
1133 struct uart_port *port = platform_get_drvdata(pdev);
1134 struct stm32_port *stm32_port = to_stm32_port(port);
1135 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1136 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1138 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
1140 if (stm32_port->rx_ch)
1141 dma_release_channel(stm32_port->rx_ch);
1143 if (stm32_port->rx_dma_buf)
1144 dma_free_coherent(&pdev->dev,
1145 RX_BUF_L, stm32_port->rx_buf,
1146 stm32_port->rx_dma_buf);
1148 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1150 if (stm32_port->tx_ch)
1151 dma_release_channel(stm32_port->tx_ch);
1153 if (stm32_port->tx_dma_buf)
1154 dma_free_coherent(&pdev->dev,
1155 TX_BUF_L, stm32_port->tx_buf,
1156 stm32_port->tx_dma_buf);
1158 if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
1159 dev_pm_clear_wake_irq(&pdev->dev);
1160 device_init_wakeup(&pdev->dev, false);
1163 clk_disable_unprepare(stm32_port->clk);
1165 return uart_remove_one_port(&stm32_usart_driver, port);
1169 #ifdef CONFIG_SERIAL_STM32_CONSOLE
1170 static void stm32_console_putchar(struct uart_port *port, int ch)
1172 struct stm32_port *stm32_port = to_stm32_port(port);
1173 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1175 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
1178 writel_relaxed(ch, port->membase + ofs->tdr);
1181 static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
1183 struct uart_port *port = &stm32_ports[co->index].port;
1184 struct stm32_port *stm32_port = to_stm32_port(port);
1185 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1186 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1187 unsigned long flags;
1188 u32 old_cr1, new_cr1;
1191 local_irq_save(flags);
1194 else if (oops_in_progress)
1195 locked = spin_trylock(&port->lock);
1197 spin_lock(&port->lock);
1199 /* Save and disable interrupts, enable the transmitter */
1200 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1201 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1202 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
1203 writel_relaxed(new_cr1, port->membase + ofs->cr1);
1205 uart_console_write(port, s, cnt, stm32_console_putchar);
1207 /* Restore interrupt state */
1208 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1211 spin_unlock(&port->lock);
1212 local_irq_restore(flags);
1215 static int stm32_console_setup(struct console *co, char *options)
1217 struct stm32_port *stm32port;
1223 if (co->index >= STM32_MAX_PORTS)
1226 stm32port = &stm32_ports[co->index];
1229 * This driver does not support early console initialization
1230 * (use ARM early printk support instead), so we only expect
1231 * this to be called during the uart port registration when the
1232 * driver gets probed and the port should be mapped at that point.
1234 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1238 uart_parse_options(options, &baud, &parity, &bits, &flow);
1240 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1243 static struct console stm32_console = {
1244 .name = STM32_SERIAL_NAME,
1245 .device = uart_console_device,
1246 .write = stm32_console_write,
1247 .setup = stm32_console_setup,
1248 .flags = CON_PRINTBUFFER,
1250 .data = &stm32_usart_driver,
1253 #define STM32_SERIAL_CONSOLE (&stm32_console)
1256 #define STM32_SERIAL_CONSOLE NULL
1257 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
1259 static struct uart_driver stm32_usart_driver = {
1260 .driver_name = DRIVER_NAME,
1261 .dev_name = STM32_SERIAL_NAME,
1264 .nr = STM32_MAX_PORTS,
1265 .cons = STM32_SERIAL_CONSOLE,
1268 #ifdef CONFIG_PM_SLEEP
1269 static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable)
1271 struct stm32_port *stm32_port = to_stm32_port(port);
1272 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1273 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1276 if (!cfg->has_wakeup || stm32_port->wakeirq < 0)
1280 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1281 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1282 val = readl_relaxed(port->membase + ofs->cr3);
1283 val &= ~USART_CR3_WUS_MASK;
1284 /* Enable Wake up interrupt from low power on start bit */
1285 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1286 writel_relaxed(val, port->membase + ofs->cr3);
1287 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1289 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1293 static int stm32_serial_suspend(struct device *dev)
1295 struct uart_port *port = dev_get_drvdata(dev);
1297 uart_suspend_port(&stm32_usart_driver, port);
1299 if (device_may_wakeup(dev))
1300 stm32_serial_enable_wakeup(port, true);
1302 stm32_serial_enable_wakeup(port, false);
1307 static int stm32_serial_resume(struct device *dev)
1309 struct uart_port *port = dev_get_drvdata(dev);
1311 if (device_may_wakeup(dev))
1312 stm32_serial_enable_wakeup(port, false);
1314 return uart_resume_port(&stm32_usart_driver, port);
1316 #endif /* CONFIG_PM_SLEEP */
1318 static const struct dev_pm_ops stm32_serial_pm_ops = {
1319 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1322 static struct platform_driver stm32_serial_driver = {
1323 .probe = stm32_serial_probe,
1324 .remove = stm32_serial_remove,
1326 .name = DRIVER_NAME,
1327 .pm = &stm32_serial_pm_ops,
1328 .of_match_table = of_match_ptr(stm32_match),
1332 static int __init usart_init(void)
1334 static char banner[] __initdata = "STM32 USART driver initialized";
1337 pr_info("%s\n", banner);
1339 ret = uart_register_driver(&stm32_usart_driver);
1343 ret = platform_driver_register(&stm32_serial_driver);
1345 uart_unregister_driver(&stm32_usart_driver);
1350 static void __exit usart_exit(void)
1352 platform_driver_unregister(&stm32_serial_driver);
1353 uart_unregister_driver(&stm32_usart_driver);
1356 module_init(usart_init);
1357 module_exit(usart_exit);
1359 MODULE_ALIAS("platform:" DRIVER_NAME);
1360 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1361 MODULE_LICENSE("GPL v2");