Linux 4.16.11
[linux/fpc-iii.git] / drivers / tty / serial / stm32-usart.c
blob0fa735b60f2dcb2e65d3b8cee7442a0fa0418673
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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)
9 */
11 #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12 #define SUPPORT_SYSRQ
13 #endif
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>
21 #include <linux/io.h>
22 #include <linux/iopoll.h>
23 #include <linux/irq.h>
24 #include <linux/module.h>
25 #include <linux/of.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)
49 u32 val;
51 val = readl_relaxed(port->membase + reg);
52 val |= bits;
53 writel_relaxed(val, port->membase + reg);
56 static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
58 u32 val;
60 val = readl_relaxed(port->membase + reg);
61 val &= ~bits;
62 writel_relaxed(val, port->membase + reg);
65 static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
66 bool threaded)
68 struct stm32_port *stm32_port = to_stm32_port(port);
69 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
70 enum dma_status status;
71 struct dma_tx_state state;
73 *sr = readl_relaxed(port->membase + ofs->isr);
75 if (threaded && stm32_port->rx_ch) {
76 status = dmaengine_tx_status(stm32_port->rx_ch,
77 stm32_port->rx_ch->cookie,
78 &state);
79 if ((status == DMA_IN_PROGRESS) &&
80 (*last_res != state.residue))
81 return 1;
82 else
83 return 0;
84 } else if (*sr & USART_SR_RXNE) {
85 return 1;
87 return 0;
90 static unsigned long
91 stm32_get_char(struct uart_port *port, u32 *sr, int *last_res)
93 struct stm32_port *stm32_port = to_stm32_port(port);
94 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
95 unsigned long c;
97 if (stm32_port->rx_ch) {
98 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
99 if ((*last_res) == 0)
100 *last_res = RX_BUF_L;
101 return c;
102 } else {
103 return readl_relaxed(port->membase + ofs->rdr);
107 static void stm32_receive_chars(struct uart_port *port, bool threaded)
109 struct tty_port *tport = &port->state->port;
110 struct stm32_port *stm32_port = to_stm32_port(port);
111 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
112 unsigned long c;
113 u32 sr;
114 char flag;
116 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
117 pm_wakeup_event(tport->tty->dev, 0);
119 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
120 sr |= USART_SR_DUMMY_RX;
121 c = stm32_get_char(port, &sr, &stm32_port->last_res);
122 flag = TTY_NORMAL;
123 port->icount.rx++;
125 if (sr & USART_SR_ERR_MASK) {
126 if (sr & USART_SR_LBD) {
127 port->icount.brk++;
128 if (uart_handle_break(port))
129 continue;
130 } else if (sr & USART_SR_ORE) {
131 if (ofs->icr != UNDEF_REG)
132 writel_relaxed(USART_ICR_ORECF,
133 port->membase +
134 ofs->icr);
135 port->icount.overrun++;
136 } else if (sr & USART_SR_PE) {
137 port->icount.parity++;
138 } else if (sr & USART_SR_FE) {
139 port->icount.frame++;
142 sr &= port->read_status_mask;
144 if (sr & USART_SR_LBD)
145 flag = TTY_BREAK;
146 else if (sr & USART_SR_PE)
147 flag = TTY_PARITY;
148 else if (sr & USART_SR_FE)
149 flag = TTY_FRAME;
152 if (uart_handle_sysrq_char(port, c))
153 continue;
154 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
157 spin_unlock(&port->lock);
158 tty_flip_buffer_push(tport);
159 spin_lock(&port->lock);
162 static void stm32_tx_dma_complete(void *arg)
164 struct uart_port *port = arg;
165 struct stm32_port *stm32port = to_stm32_port(port);
166 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
167 unsigned int isr;
168 int ret;
170 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
171 isr,
172 (isr & USART_SR_TC),
173 10, 100000);
175 if (ret)
176 dev_err(port->dev, "terminal count not set\n");
178 if (ofs->icr == UNDEF_REG)
179 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
180 else
181 stm32_set_bits(port, ofs->icr, USART_CR_TC);
183 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
184 stm32port->tx_dma_busy = false;
186 /* Let's see if we have pending data to send */
187 stm32_transmit_chars(port);
190 static void stm32_transmit_chars_pio(struct uart_port *port)
192 struct stm32_port *stm32_port = to_stm32_port(port);
193 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
194 struct circ_buf *xmit = &port->state->xmit;
195 unsigned int isr;
196 int ret;
198 if (stm32_port->tx_dma_busy) {
199 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
200 stm32_port->tx_dma_busy = false;
203 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
204 isr,
205 (isr & USART_SR_TXE),
206 10, 100000);
208 if (ret)
209 dev_err(port->dev, "tx empty not set\n");
211 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
213 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
214 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
215 port->icount.tx++;
218 static void stm32_transmit_chars_dma(struct uart_port *port)
220 struct stm32_port *stm32port = to_stm32_port(port);
221 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
222 struct circ_buf *xmit = &port->state->xmit;
223 struct dma_async_tx_descriptor *desc = NULL;
224 dma_cookie_t cookie;
225 unsigned int count, i;
227 if (stm32port->tx_dma_busy)
228 return;
230 stm32port->tx_dma_busy = true;
232 count = uart_circ_chars_pending(xmit);
234 if (count > TX_BUF_L)
235 count = TX_BUF_L;
237 if (xmit->tail < xmit->head) {
238 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
239 } else {
240 size_t one = UART_XMIT_SIZE - xmit->tail;
241 size_t two;
243 if (one > count)
244 one = count;
245 two = count - one;
247 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
248 if (two)
249 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
252 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
253 stm32port->tx_dma_buf,
254 count,
255 DMA_MEM_TO_DEV,
256 DMA_PREP_INTERRUPT);
258 if (!desc) {
259 for (i = count; i > 0; i--)
260 stm32_transmit_chars_pio(port);
261 return;
264 desc->callback = stm32_tx_dma_complete;
265 desc->callback_param = port;
267 /* Push current DMA TX transaction in the pending queue */
268 cookie = dmaengine_submit(desc);
270 /* Issue pending DMA TX requests */
271 dma_async_issue_pending(stm32port->tx_ch);
273 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
274 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
276 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
277 port->icount.tx += count;
280 static void stm32_transmit_chars(struct uart_port *port)
282 struct stm32_port *stm32_port = to_stm32_port(port);
283 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
284 struct circ_buf *xmit = &port->state->xmit;
286 if (port->x_char) {
287 if (stm32_port->tx_dma_busy)
288 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
289 writel_relaxed(port->x_char, port->membase + ofs->tdr);
290 port->x_char = 0;
291 port->icount.tx++;
292 if (stm32_port->tx_dma_busy)
293 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
294 return;
297 if (uart_tx_stopped(port)) {
298 stm32_stop_tx(port);
299 return;
302 if (uart_circ_empty(xmit)) {
303 stm32_stop_tx(port);
304 return;
307 if (stm32_port->tx_ch)
308 stm32_transmit_chars_dma(port);
309 else
310 stm32_transmit_chars_pio(port);
312 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
313 uart_write_wakeup(port);
315 if (uart_circ_empty(xmit))
316 stm32_stop_tx(port);
319 static irqreturn_t stm32_interrupt(int irq, void *ptr)
321 struct uart_port *port = ptr;
322 struct stm32_port *stm32_port = to_stm32_port(port);
323 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
324 u32 sr;
326 spin_lock(&port->lock);
328 sr = readl_relaxed(port->membase + ofs->isr);
330 if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
331 writel_relaxed(USART_ICR_WUCF,
332 port->membase + ofs->icr);
334 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
335 stm32_receive_chars(port, false);
337 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
338 stm32_transmit_chars(port);
340 spin_unlock(&port->lock);
342 if (stm32_port->rx_ch)
343 return IRQ_WAKE_THREAD;
344 else
345 return IRQ_HANDLED;
348 static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
350 struct uart_port *port = ptr;
351 struct stm32_port *stm32_port = to_stm32_port(port);
353 spin_lock(&port->lock);
355 if (stm32_port->rx_ch)
356 stm32_receive_chars(port, true);
358 spin_unlock(&port->lock);
360 return IRQ_HANDLED;
363 static unsigned int stm32_tx_empty(struct uart_port *port)
365 struct stm32_port *stm32_port = to_stm32_port(port);
366 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
368 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
371 static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
373 struct stm32_port *stm32_port = to_stm32_port(port);
374 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
376 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
377 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
378 else
379 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
382 static unsigned int stm32_get_mctrl(struct uart_port *port)
384 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
385 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
388 /* Transmit stop */
389 static void stm32_stop_tx(struct uart_port *port)
391 struct stm32_port *stm32_port = to_stm32_port(port);
392 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
394 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
397 /* There are probably characters waiting to be transmitted. */
398 static void stm32_start_tx(struct uart_port *port)
400 struct circ_buf *xmit = &port->state->xmit;
402 if (uart_circ_empty(xmit))
403 return;
405 stm32_transmit_chars(port);
408 /* Throttle the remote when input buffer is about to overflow. */
409 static void stm32_throttle(struct uart_port *port)
411 struct stm32_port *stm32_port = to_stm32_port(port);
412 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
413 unsigned long flags;
415 spin_lock_irqsave(&port->lock, flags);
416 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
417 spin_unlock_irqrestore(&port->lock, flags);
420 /* Unthrottle the remote, the input buffer can now accept data. */
421 static void stm32_unthrottle(struct uart_port *port)
423 struct stm32_port *stm32_port = to_stm32_port(port);
424 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
425 unsigned long flags;
427 spin_lock_irqsave(&port->lock, flags);
428 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
429 spin_unlock_irqrestore(&port->lock, flags);
432 /* Receive stop */
433 static void stm32_stop_rx(struct uart_port *port)
435 struct stm32_port *stm32_port = to_stm32_port(port);
436 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
438 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
441 /* Handle breaks - ignored by us */
442 static void stm32_break_ctl(struct uart_port *port, int break_state)
446 static int stm32_startup(struct uart_port *port)
448 struct stm32_port *stm32_port = to_stm32_port(port);
449 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
450 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
451 const char *name = to_platform_device(port->dev)->name;
452 u32 val;
453 int ret;
455 ret = request_threaded_irq(port->irq, stm32_interrupt,
456 stm32_threaded_interrupt,
457 IRQF_NO_SUSPEND, name, port);
458 if (ret)
459 return ret;
461 if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
462 ret = dev_pm_set_dedicated_wake_irq(port->dev,
463 stm32_port->wakeirq);
464 if (ret) {
465 free_irq(port->irq, port);
466 return ret;
470 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
471 if (stm32_port->fifoen)
472 val |= USART_CR1_FIFOEN;
473 stm32_set_bits(port, ofs->cr1, val);
475 return 0;
478 static void stm32_shutdown(struct uart_port *port)
480 struct stm32_port *stm32_port = to_stm32_port(port);
481 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
482 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
483 u32 val;
485 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
486 val |= BIT(cfg->uart_enable_bit);
487 if (stm32_port->fifoen)
488 val |= USART_CR1_FIFOEN;
489 stm32_clr_bits(port, ofs->cr1, val);
491 dev_pm_clear_wake_irq(port->dev);
492 free_irq(port->irq, port);
495 static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
496 struct ktermios *old)
498 struct stm32_port *stm32_port = to_stm32_port(port);
499 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
500 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
501 unsigned int baud;
502 u32 usartdiv, mantissa, fraction, oversampling;
503 tcflag_t cflag = termios->c_cflag;
504 u32 cr1, cr2, cr3;
505 unsigned long flags;
507 if (!stm32_port->hw_flow_control)
508 cflag &= ~CRTSCTS;
510 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
512 spin_lock_irqsave(&port->lock, flags);
514 /* Stop serial port and reset value */
515 writel_relaxed(0, port->membase + ofs->cr1);
517 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
518 cr1 |= BIT(cfg->uart_enable_bit);
519 if (stm32_port->fifoen)
520 cr1 |= USART_CR1_FIFOEN;
521 cr2 = 0;
522 cr3 = 0;
524 if (cflag & CSTOPB)
525 cr2 |= USART_CR2_STOP_2B;
527 if (cflag & PARENB) {
528 cr1 |= USART_CR1_PCE;
529 if ((cflag & CSIZE) == CS8) {
530 if (cfg->has_7bits_data)
531 cr1 |= USART_CR1_M0;
532 else
533 cr1 |= USART_CR1_M;
537 if (cflag & PARODD)
538 cr1 |= USART_CR1_PS;
540 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
541 if (cflag & CRTSCTS) {
542 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
543 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
546 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
549 * The USART supports 16 or 8 times oversampling.
550 * By default we prefer 16 times oversampling, so that the receiver
551 * has a better tolerance to clock deviations.
552 * 8 times oversampling is only used to achieve higher speeds.
554 if (usartdiv < 16) {
555 oversampling = 8;
556 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
557 } else {
558 oversampling = 16;
559 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
562 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
563 fraction = usartdiv % oversampling;
564 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
566 uart_update_timeout(port, cflag, baud);
568 port->read_status_mask = USART_SR_ORE;
569 if (termios->c_iflag & INPCK)
570 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
571 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
572 port->read_status_mask |= USART_SR_LBD;
574 /* Characters to ignore */
575 port->ignore_status_mask = 0;
576 if (termios->c_iflag & IGNPAR)
577 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
578 if (termios->c_iflag & IGNBRK) {
579 port->ignore_status_mask |= USART_SR_LBD;
581 * If we're ignoring parity and break indicators,
582 * ignore overruns too (for real raw support).
584 if (termios->c_iflag & IGNPAR)
585 port->ignore_status_mask |= USART_SR_ORE;
588 /* Ignore all characters if CREAD is not set */
589 if ((termios->c_cflag & CREAD) == 0)
590 port->ignore_status_mask |= USART_SR_DUMMY_RX;
592 if (stm32_port->rx_ch)
593 cr3 |= USART_CR3_DMAR;
595 writel_relaxed(cr3, port->membase + ofs->cr3);
596 writel_relaxed(cr2, port->membase + ofs->cr2);
597 writel_relaxed(cr1, port->membase + ofs->cr1);
599 spin_unlock_irqrestore(&port->lock, flags);
602 static const char *stm32_type(struct uart_port *port)
604 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
607 static void stm32_release_port(struct uart_port *port)
611 static int stm32_request_port(struct uart_port *port)
613 return 0;
616 static void stm32_config_port(struct uart_port *port, int flags)
618 if (flags & UART_CONFIG_TYPE)
619 port->type = PORT_STM32;
622 static int
623 stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
625 /* No user changeable parameters */
626 return -EINVAL;
629 static void stm32_pm(struct uart_port *port, unsigned int state,
630 unsigned int oldstate)
632 struct stm32_port *stm32port = container_of(port,
633 struct stm32_port, port);
634 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
635 struct stm32_usart_config *cfg = &stm32port->info->cfg;
636 unsigned long flags = 0;
638 switch (state) {
639 case UART_PM_STATE_ON:
640 clk_prepare_enable(stm32port->clk);
641 break;
642 case UART_PM_STATE_OFF:
643 spin_lock_irqsave(&port->lock, flags);
644 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
645 spin_unlock_irqrestore(&port->lock, flags);
646 clk_disable_unprepare(stm32port->clk);
647 break;
651 static const struct uart_ops stm32_uart_ops = {
652 .tx_empty = stm32_tx_empty,
653 .set_mctrl = stm32_set_mctrl,
654 .get_mctrl = stm32_get_mctrl,
655 .stop_tx = stm32_stop_tx,
656 .start_tx = stm32_start_tx,
657 .throttle = stm32_throttle,
658 .unthrottle = stm32_unthrottle,
659 .stop_rx = stm32_stop_rx,
660 .break_ctl = stm32_break_ctl,
661 .startup = stm32_startup,
662 .shutdown = stm32_shutdown,
663 .set_termios = stm32_set_termios,
664 .pm = stm32_pm,
665 .type = stm32_type,
666 .release_port = stm32_release_port,
667 .request_port = stm32_request_port,
668 .config_port = stm32_config_port,
669 .verify_port = stm32_verify_port,
672 static int stm32_init_port(struct stm32_port *stm32port,
673 struct platform_device *pdev)
675 struct uart_port *port = &stm32port->port;
676 struct resource *res;
677 int ret;
679 port->iotype = UPIO_MEM;
680 port->flags = UPF_BOOT_AUTOCONF;
681 port->ops = &stm32_uart_ops;
682 port->dev = &pdev->dev;
683 port->irq = platform_get_irq(pdev, 0);
684 stm32port->wakeirq = platform_get_irq(pdev, 1);
685 stm32port->fifoen = stm32port->info->cfg.has_fifo;
687 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
688 port->membase = devm_ioremap_resource(&pdev->dev, res);
689 if (IS_ERR(port->membase))
690 return PTR_ERR(port->membase);
691 port->mapbase = res->start;
693 spin_lock_init(&port->lock);
695 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
696 if (IS_ERR(stm32port->clk))
697 return PTR_ERR(stm32port->clk);
699 /* Ensure that clk rate is correct by enabling the clk */
700 ret = clk_prepare_enable(stm32port->clk);
701 if (ret)
702 return ret;
704 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
705 if (!stm32port->port.uartclk) {
706 clk_disable_unprepare(stm32port->clk);
707 ret = -EINVAL;
710 return ret;
713 static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
715 struct device_node *np = pdev->dev.of_node;
716 int id;
718 if (!np)
719 return NULL;
721 id = of_alias_get_id(np, "serial");
722 if (id < 0) {
723 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
724 return NULL;
727 if (WARN_ON(id >= STM32_MAX_PORTS))
728 return NULL;
730 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
731 "st,hw-flow-ctrl");
732 stm32_ports[id].port.line = id;
733 stm32_ports[id].last_res = RX_BUF_L;
734 return &stm32_ports[id];
737 #ifdef CONFIG_OF
738 static const struct of_device_id stm32_match[] = {
739 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
740 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
741 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
745 MODULE_DEVICE_TABLE(of, stm32_match);
746 #endif
748 static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
749 struct platform_device *pdev)
751 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
752 struct uart_port *port = &stm32port->port;
753 struct device *dev = &pdev->dev;
754 struct dma_slave_config config;
755 struct dma_async_tx_descriptor *desc = NULL;
756 dma_cookie_t cookie;
757 int ret;
759 /* Request DMA RX channel */
760 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
761 if (!stm32port->rx_ch) {
762 dev_info(dev, "rx dma alloc failed\n");
763 return -ENODEV;
765 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
766 &stm32port->rx_dma_buf,
767 GFP_KERNEL);
768 if (!stm32port->rx_buf) {
769 ret = -ENOMEM;
770 goto alloc_err;
773 /* Configure DMA channel */
774 memset(&config, 0, sizeof(config));
775 config.src_addr = port->mapbase + ofs->rdr;
776 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
778 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
779 if (ret < 0) {
780 dev_err(dev, "rx dma channel config failed\n");
781 ret = -ENODEV;
782 goto config_err;
785 /* Prepare a DMA cyclic transaction */
786 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
787 stm32port->rx_dma_buf,
788 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
789 DMA_PREP_INTERRUPT);
790 if (!desc) {
791 dev_err(dev, "rx dma prep cyclic failed\n");
792 ret = -ENODEV;
793 goto config_err;
796 /* No callback as dma buffer is drained on usart interrupt */
797 desc->callback = NULL;
798 desc->callback_param = NULL;
800 /* Push current DMA transaction in the pending queue */
801 cookie = dmaengine_submit(desc);
803 /* Issue pending DMA requests */
804 dma_async_issue_pending(stm32port->rx_ch);
806 return 0;
808 config_err:
809 dma_free_coherent(&pdev->dev,
810 RX_BUF_L, stm32port->rx_buf,
811 stm32port->rx_dma_buf);
813 alloc_err:
814 dma_release_channel(stm32port->rx_ch);
815 stm32port->rx_ch = NULL;
817 return ret;
820 static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
821 struct platform_device *pdev)
823 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
824 struct uart_port *port = &stm32port->port;
825 struct device *dev = &pdev->dev;
826 struct dma_slave_config config;
827 int ret;
829 stm32port->tx_dma_busy = false;
831 /* Request DMA TX channel */
832 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
833 if (!stm32port->tx_ch) {
834 dev_info(dev, "tx dma alloc failed\n");
835 return -ENODEV;
837 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
838 &stm32port->tx_dma_buf,
839 GFP_KERNEL);
840 if (!stm32port->tx_buf) {
841 ret = -ENOMEM;
842 goto alloc_err;
845 /* Configure DMA channel */
846 memset(&config, 0, sizeof(config));
847 config.dst_addr = port->mapbase + ofs->tdr;
848 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
850 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
851 if (ret < 0) {
852 dev_err(dev, "tx dma channel config failed\n");
853 ret = -ENODEV;
854 goto config_err;
857 return 0;
859 config_err:
860 dma_free_coherent(&pdev->dev,
861 TX_BUF_L, stm32port->tx_buf,
862 stm32port->tx_dma_buf);
864 alloc_err:
865 dma_release_channel(stm32port->tx_ch);
866 stm32port->tx_ch = NULL;
868 return ret;
871 static int stm32_serial_probe(struct platform_device *pdev)
873 const struct of_device_id *match;
874 struct stm32_port *stm32port;
875 int ret;
877 stm32port = stm32_of_get_stm32_port(pdev);
878 if (!stm32port)
879 return -ENODEV;
881 match = of_match_device(stm32_match, &pdev->dev);
882 if (match && match->data)
883 stm32port->info = (struct stm32_usart_info *)match->data;
884 else
885 return -EINVAL;
887 ret = stm32_init_port(stm32port, pdev);
888 if (ret)
889 return ret;
891 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) {
892 ret = device_init_wakeup(&pdev->dev, true);
893 if (ret)
894 goto err_uninit;
897 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
898 if (ret)
899 goto err_nowup;
901 ret = stm32_of_dma_rx_probe(stm32port, pdev);
902 if (ret)
903 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
905 ret = stm32_of_dma_tx_probe(stm32port, pdev);
906 if (ret)
907 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
909 platform_set_drvdata(pdev, &stm32port->port);
911 return 0;
913 err_nowup:
914 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
915 device_init_wakeup(&pdev->dev, false);
917 err_uninit:
918 clk_disable_unprepare(stm32port->clk);
920 return ret;
923 static int stm32_serial_remove(struct platform_device *pdev)
925 struct uart_port *port = platform_get_drvdata(pdev);
926 struct stm32_port *stm32_port = to_stm32_port(port);
927 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
928 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
930 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
932 if (stm32_port->rx_ch)
933 dma_release_channel(stm32_port->rx_ch);
935 if (stm32_port->rx_dma_buf)
936 dma_free_coherent(&pdev->dev,
937 RX_BUF_L, stm32_port->rx_buf,
938 stm32_port->rx_dma_buf);
940 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
942 if (stm32_port->tx_ch)
943 dma_release_channel(stm32_port->tx_ch);
945 if (stm32_port->tx_dma_buf)
946 dma_free_coherent(&pdev->dev,
947 TX_BUF_L, stm32_port->tx_buf,
948 stm32_port->tx_dma_buf);
950 if (cfg->has_wakeup && stm32_port->wakeirq >= 0)
951 device_init_wakeup(&pdev->dev, false);
953 clk_disable_unprepare(stm32_port->clk);
955 return uart_remove_one_port(&stm32_usart_driver, port);
959 #ifdef CONFIG_SERIAL_STM32_CONSOLE
960 static void stm32_console_putchar(struct uart_port *port, int ch)
962 struct stm32_port *stm32_port = to_stm32_port(port);
963 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
965 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
966 cpu_relax();
968 writel_relaxed(ch, port->membase + ofs->tdr);
971 static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
973 struct uart_port *port = &stm32_ports[co->index].port;
974 struct stm32_port *stm32_port = to_stm32_port(port);
975 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
976 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
977 unsigned long flags;
978 u32 old_cr1, new_cr1;
979 int locked = 1;
981 local_irq_save(flags);
982 if (port->sysrq)
983 locked = 0;
984 else if (oops_in_progress)
985 locked = spin_trylock(&port->lock);
986 else
987 spin_lock(&port->lock);
989 /* Save and disable interrupts, enable the transmitter */
990 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
991 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
992 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
993 writel_relaxed(new_cr1, port->membase + ofs->cr1);
995 uart_console_write(port, s, cnt, stm32_console_putchar);
997 /* Restore interrupt state */
998 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1000 if (locked)
1001 spin_unlock(&port->lock);
1002 local_irq_restore(flags);
1005 static int stm32_console_setup(struct console *co, char *options)
1007 struct stm32_port *stm32port;
1008 int baud = 9600;
1009 int bits = 8;
1010 int parity = 'n';
1011 int flow = 'n';
1013 if (co->index >= STM32_MAX_PORTS)
1014 return -ENODEV;
1016 stm32port = &stm32_ports[co->index];
1019 * This driver does not support early console initialization
1020 * (use ARM early printk support instead), so we only expect
1021 * this to be called during the uart port registration when the
1022 * driver gets probed and the port should be mapped at that point.
1024 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1025 return -ENXIO;
1027 if (options)
1028 uart_parse_options(options, &baud, &parity, &bits, &flow);
1030 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1033 static struct console stm32_console = {
1034 .name = STM32_SERIAL_NAME,
1035 .device = uart_console_device,
1036 .write = stm32_console_write,
1037 .setup = stm32_console_setup,
1038 .flags = CON_PRINTBUFFER,
1039 .index = -1,
1040 .data = &stm32_usart_driver,
1043 #define STM32_SERIAL_CONSOLE (&stm32_console)
1045 #else
1046 #define STM32_SERIAL_CONSOLE NULL
1047 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
1049 static struct uart_driver stm32_usart_driver = {
1050 .driver_name = DRIVER_NAME,
1051 .dev_name = STM32_SERIAL_NAME,
1052 .major = 0,
1053 .minor = 0,
1054 .nr = STM32_MAX_PORTS,
1055 .cons = STM32_SERIAL_CONSOLE,
1058 #ifdef CONFIG_PM_SLEEP
1059 static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable)
1061 struct stm32_port *stm32_port = to_stm32_port(port);
1062 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1063 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1064 u32 val;
1066 if (!cfg->has_wakeup || stm32_port->wakeirq < 0)
1067 return;
1069 if (enable) {
1070 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1071 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1072 val = readl_relaxed(port->membase + ofs->cr3);
1073 val &= ~USART_CR3_WUS_MASK;
1074 /* Enable Wake up interrupt from low power on start bit */
1075 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1076 writel_relaxed(val, port->membase + ofs->cr3);
1077 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1078 } else {
1079 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1083 static int stm32_serial_suspend(struct device *dev)
1085 struct uart_port *port = dev_get_drvdata(dev);
1087 uart_suspend_port(&stm32_usart_driver, port);
1089 if (device_may_wakeup(dev))
1090 stm32_serial_enable_wakeup(port, true);
1091 else
1092 stm32_serial_enable_wakeup(port, false);
1094 return 0;
1097 static int stm32_serial_resume(struct device *dev)
1099 struct uart_port *port = dev_get_drvdata(dev);
1101 if (device_may_wakeup(dev))
1102 stm32_serial_enable_wakeup(port, false);
1104 return uart_resume_port(&stm32_usart_driver, port);
1106 #endif /* CONFIG_PM_SLEEP */
1108 static const struct dev_pm_ops stm32_serial_pm_ops = {
1109 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1112 static struct platform_driver stm32_serial_driver = {
1113 .probe = stm32_serial_probe,
1114 .remove = stm32_serial_remove,
1115 .driver = {
1116 .name = DRIVER_NAME,
1117 .pm = &stm32_serial_pm_ops,
1118 .of_match_table = of_match_ptr(stm32_match),
1122 static int __init usart_init(void)
1124 static char banner[] __initdata = "STM32 USART driver initialized";
1125 int ret;
1127 pr_info("%s\n", banner);
1129 ret = uart_register_driver(&stm32_usart_driver);
1130 if (ret)
1131 return ret;
1133 ret = platform_driver_register(&stm32_serial_driver);
1134 if (ret)
1135 uart_unregister_driver(&stm32_usart_driver);
1137 return ret;
1140 static void __exit usart_exit(void)
1142 platform_driver_unregister(&stm32_serial_driver);
1143 uart_unregister_driver(&stm32_usart_driver);
1146 module_init(usart_init);
1147 module_exit(usart_exit);
1149 MODULE_ALIAS("platform:" DRIVER_NAME);
1150 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1151 MODULE_LICENSE("GPL v2");