blob: 1e854e1851fbbf52a08a0c667694d0a94420cceb [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) Maxime Coquelin 2015
3 * Copyright (C) STMicroelectronics SA 2017
4 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
5 * Gerald Baeza <gerald.baeza@st.com>
6 * License terms: GNU General Public License (GPL), version 2
7 *
8 * Inspired by st-asc.c from STMicroelectronics (c)
9 */
10
11#if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12#define SUPPORT_SYSRQ
13#endif
14
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>
36
37#include "stm32-usart.h"
38
39static void stm32_stop_tx(struct uart_port *port);
40static void stm32_transmit_chars(struct uart_port *port);
41
42static inline struct stm32_port *to_stm32_port(struct uart_port *port)
43{
44 return container_of(port, struct stm32_port, port);
45}
46
47static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
48{
49 u32 val;
50
51 val = readl_relaxed(port->membase + reg);
52 val |= bits;
53 writel_relaxed(val, port->membase + reg);
54}
55
56static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
57{
58 u32 val;
59
60 val = readl_relaxed(port->membase + reg);
61 val &= ~bits;
62 writel_relaxed(val, port->membase + reg);
63}
64
65static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
66 bool threaded)
67{
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;
72
73 *sr = readl_relaxed(port->membase + ofs->isr);
74
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;
86 }
87 return 0;
88}
89
90static unsigned long
91stm32_get_char(struct uart_port *port, u32 *sr, int *last_res)
92{
93 struct stm32_port *stm32_port = to_stm32_port(port);
94 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
95 unsigned long c;
96
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);
104 }
105}
106
107static void stm32_receive_chars(struct uart_port *port, bool threaded)
108{
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;
115
116 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
117 pm_wakeup_event(tport->tty->dev, 0);
118
119 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
120 sr |= USART_SR_DUMMY_RX;
121 flag = TTY_NORMAL;
122
123 /*
124 * Status bits has to be cleared before reading the RDR:
125 * In FIFO mode, reading the RDR will pop the next data
126 * (if any) along with its status bits into the SR.
127 * Not doing so leads to misalignement between RDR and SR,
128 * and clear status bits of the next rx data.
129 *
130 * Clear errors flags for stm32f7 and stm32h7 compatible
131 * devices. On stm32f4 compatible devices, the error bit is
132 * cleared by the sequence [read SR - read DR].
133 */
134 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
135 writel_relaxed(sr & USART_SR_ERR_MASK,
136 port->membase + ofs->icr);
137
138 c = stm32_get_char(port, &sr, &stm32_port->last_res);
139 port->icount.rx++;
140 if (sr & USART_SR_ERR_MASK) {
141 if (sr & USART_SR_ORE) {
142 port->icount.overrun++;
143 } else if (sr & USART_SR_PE) {
144 port->icount.parity++;
145 } else if (sr & USART_SR_FE) {
146 /* Break detection if character is null */
147 if (!c) {
148 port->icount.brk++;
149 if (uart_handle_break(port))
150 continue;
151 } else {
152 port->icount.frame++;
153 }
154 }
155
156 sr &= port->read_status_mask;
157
158 if (sr & USART_SR_PE) {
159 flag = TTY_PARITY;
160 } else if (sr & USART_SR_FE) {
161 if (!c)
162 flag = TTY_BREAK;
163 else
164 flag = TTY_FRAME;
165 }
166 }
167
168 if (uart_handle_sysrq_char(port, c))
169 continue;
170 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
171 }
172
173 spin_unlock(&port->lock);
174 tty_flip_buffer_push(tport);
175 spin_lock(&port->lock);
176}
177
178static void stm32_tx_dma_complete(void *arg)
179{
180 struct uart_port *port = arg;
181 struct stm32_port *stm32port = to_stm32_port(port);
182 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
183
184 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
185 stm32port->tx_dma_busy = false;
186
187 /* Let's see if we have pending data to send */
188 stm32_transmit_chars(port);
189}
190
191static void stm32_transmit_chars_pio(struct uart_port *port)
192{
193 struct stm32_port *stm32_port = to_stm32_port(port);
194 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
195 struct circ_buf *xmit = &port->state->xmit;
196 unsigned int isr;
197 int ret;
198
199 if (stm32_port->tx_dma_busy) {
200 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
201 stm32_port->tx_dma_busy = false;
202 }
203
204 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
205 isr,
206 (isr & USART_SR_TXE),
207 10, 100000);
208
209 if (ret)
210 dev_err(port->dev, "tx empty not set\n");
211
212 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
213
214 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
215 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
216 port->icount.tx++;
217}
218
219static void stm32_transmit_chars_dma(struct uart_port *port)
220{
221 struct stm32_port *stm32port = to_stm32_port(port);
222 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
223 struct circ_buf *xmit = &port->state->xmit;
224 struct dma_async_tx_descriptor *desc = NULL;
225 dma_cookie_t cookie;
226 unsigned int count, i;
227
228 if (stm32port->tx_dma_busy)
229 return;
230
231 stm32port->tx_dma_busy = true;
232
233 count = uart_circ_chars_pending(xmit);
234
235 if (count > TX_BUF_L)
236 count = TX_BUF_L;
237
238 if (xmit->tail < xmit->head) {
239 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
240 } else {
241 size_t one = UART_XMIT_SIZE - xmit->tail;
242 size_t two;
243
244 if (one > count)
245 one = count;
246 two = count - one;
247
248 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
249 if (two)
250 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
251 }
252
253 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
254 stm32port->tx_dma_buf,
255 count,
256 DMA_MEM_TO_DEV,
257 DMA_PREP_INTERRUPT);
258
259 if (!desc) {
260 for (i = count; i > 0; i--)
261 stm32_transmit_chars_pio(port);
262 return;
263 }
264
265 desc->callback = stm32_tx_dma_complete;
266 desc->callback_param = port;
267
268 /* Push current DMA TX transaction in the pending queue */
269 cookie = dmaengine_submit(desc);
270
271 /* Issue pending DMA TX requests */
272 dma_async_issue_pending(stm32port->tx_ch);
273
274 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
275
276 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
277 port->icount.tx += count;
278}
279
280static void stm32_transmit_chars(struct uart_port *port)
281{
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;
285
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;
295 }
296
297 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
298 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
299 return;
300 }
301
302 if (ofs->icr == UNDEF_REG)
303 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
304 else
305 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
306
307 if (stm32_port->tx_ch)
308 stm32_transmit_chars_dma(port);
309 else
310 stm32_transmit_chars_pio(port);
311
312 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
313 uart_write_wakeup(port);
314
315 if (uart_circ_empty(xmit))
316 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
317}
318
319static irqreturn_t stm32_interrupt(int irq, void *ptr)
320{
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;
325
326 spin_lock(&port->lock);
327
328 sr = readl_relaxed(port->membase + ofs->isr);
329
330 if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
331 writel_relaxed(USART_ICR_WUCF,
332 port->membase + ofs->icr);
333
334 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
335 stm32_receive_chars(port, false);
336
337 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
338 stm32_transmit_chars(port);
339
340 spin_unlock(&port->lock);
341
342 if (stm32_port->rx_ch)
343 return IRQ_WAKE_THREAD;
344 else
345 return IRQ_HANDLED;
346}
347
348static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
349{
350 struct uart_port *port = ptr;
351 struct stm32_port *stm32_port = to_stm32_port(port);
352
353 spin_lock(&port->lock);
354
355 if (stm32_port->rx_ch)
356 stm32_receive_chars(port, true);
357
358 spin_unlock(&port->lock);
359
360 return IRQ_HANDLED;
361}
362
363static unsigned int stm32_tx_empty(struct uart_port *port)
364{
365 struct stm32_port *stm32_port = to_stm32_port(port);
366 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
367
368 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
369}
370
371static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
372{
373 struct stm32_port *stm32_port = to_stm32_port(port);
374 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
375
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);
380}
381
382static unsigned int stm32_get_mctrl(struct uart_port *port)
383{
384 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
385 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
386}
387
388/* Transmit stop */
389static void stm32_stop_tx(struct uart_port *port)
390{
391 struct stm32_port *stm32_port = to_stm32_port(port);
392 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
393
394 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
395}
396
397/* There are probably characters waiting to be transmitted. */
398static void stm32_start_tx(struct uart_port *port)
399{
400 struct circ_buf *xmit = &port->state->xmit;
401
402 if (uart_circ_empty(xmit))
403 return;
404
405 stm32_transmit_chars(port);
406}
407
408/* Throttle the remote when input buffer is about to overflow. */
409static void stm32_throttle(struct uart_port *port)
410{
411 struct stm32_port *stm32_port = to_stm32_port(port);
412 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
413 unsigned long flags;
414
415 spin_lock_irqsave(&port->lock, flags);
416 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
417 spin_unlock_irqrestore(&port->lock, flags);
418}
419
420/* Unthrottle the remote, the input buffer can now accept data. */
421static void stm32_unthrottle(struct uart_port *port)
422{
423 struct stm32_port *stm32_port = to_stm32_port(port);
424 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
425 unsigned long flags;
426
427 spin_lock_irqsave(&port->lock, flags);
428 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
429 spin_unlock_irqrestore(&port->lock, flags);
430}
431
432/* Receive stop */
433static void stm32_stop_rx(struct uart_port *port)
434{
435 struct stm32_port *stm32_port = to_stm32_port(port);
436 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
437
438 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
439}
440
441/* Handle breaks - ignored by us */
442static void stm32_break_ctl(struct uart_port *port, int break_state)
443{
444}
445
446static int stm32_startup(struct uart_port *port)
447{
448 struct stm32_port *stm32_port = to_stm32_port(port);
449 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
450 const char *name = to_platform_device(port->dev)->name;
451 u32 val;
452 int ret;
453
454 ret = request_threaded_irq(port->irq, stm32_interrupt,
455 stm32_threaded_interrupt,
456 IRQF_NO_SUSPEND, name, port);
457 if (ret)
458 return ret;
459
460 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
461 if (stm32_port->fifoen)
462 val |= USART_CR1_FIFOEN;
463 stm32_set_bits(port, ofs->cr1, val);
464
465 return 0;
466}
467
468static void stm32_shutdown(struct uart_port *port)
469{
470 struct stm32_port *stm32_port = to_stm32_port(port);
471 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
472 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
473 u32 val, isr;
474 int ret;
475
476 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
477 val |= BIT(cfg->uart_enable_bit);
478 if (stm32_port->fifoen)
479 val |= USART_CR1_FIFOEN;
480
481 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
482 isr, (isr & USART_SR_TC),
483 10, 100000);
484
485 if (ret)
486 dev_err(port->dev, "transmission complete not set\n");
487
488 stm32_clr_bits(port, ofs->cr1, val);
489
490 free_irq(port->irq, port);
491}
492
493static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
494 struct ktermios *old)
495{
496 struct stm32_port *stm32_port = to_stm32_port(port);
497 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
498 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
499 unsigned int baud;
500 u32 usartdiv, mantissa, fraction, oversampling;
501 tcflag_t cflag = termios->c_cflag;
502 u32 cr1, cr2, cr3;
503 unsigned long flags;
504
505 if (!stm32_port->hw_flow_control)
506 cflag &= ~CRTSCTS;
507
508 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
509
510 spin_lock_irqsave(&port->lock, flags);
511
512 /* Stop serial port and reset value */
513 writel_relaxed(0, port->membase + ofs->cr1);
514
515 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
516 cr1 |= BIT(cfg->uart_enable_bit);
517 if (stm32_port->fifoen)
518 cr1 |= USART_CR1_FIFOEN;
519 cr2 = 0;
520 cr3 = 0;
521
522 if (cflag & CSTOPB)
523 cr2 |= USART_CR2_STOP_2B;
524
525 if (cflag & PARENB) {
526 cr1 |= USART_CR1_PCE;
527 if ((cflag & CSIZE) == CS8) {
528 if (cfg->has_7bits_data)
529 cr1 |= USART_CR1_M0;
530 else
531 cr1 |= USART_CR1_M;
532 }
533 }
534
535 if (cflag & PARODD)
536 cr1 |= USART_CR1_PS;
537
538 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
539 if (cflag & CRTSCTS) {
540 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
541 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
542 }
543
544 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
545
546 /*
547 * The USART supports 16 or 8 times oversampling.
548 * By default we prefer 16 times oversampling, so that the receiver
549 * has a better tolerance to clock deviations.
550 * 8 times oversampling is only used to achieve higher speeds.
551 */
552 if (usartdiv < 16) {
553 oversampling = 8;
554 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
555 } else {
556 oversampling = 16;
557 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
558 }
559
560 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
561 fraction = usartdiv % oversampling;
562 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
563
564 uart_update_timeout(port, cflag, baud);
565
566 port->read_status_mask = USART_SR_ORE;
567 if (termios->c_iflag & INPCK)
568 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
569 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
570 port->read_status_mask |= USART_SR_FE;
571
572 /* Characters to ignore */
573 port->ignore_status_mask = 0;
574 if (termios->c_iflag & IGNPAR)
575 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
576 if (termios->c_iflag & IGNBRK) {
577 port->ignore_status_mask |= USART_SR_FE;
578 /*
579 * If we're ignoring parity and break indicators,
580 * ignore overruns too (for real raw support).
581 */
582 if (termios->c_iflag & IGNPAR)
583 port->ignore_status_mask |= USART_SR_ORE;
584 }
585
586 /* Ignore all characters if CREAD is not set */
587 if ((termios->c_cflag & CREAD) == 0)
588 port->ignore_status_mask |= USART_SR_DUMMY_RX;
589
590 if (stm32_port->rx_ch)
591 cr3 |= USART_CR3_DMAR;
592
593 writel_relaxed(cr3, port->membase + ofs->cr3);
594 writel_relaxed(cr2, port->membase + ofs->cr2);
595 writel_relaxed(cr1, port->membase + ofs->cr1);
596
597 spin_unlock_irqrestore(&port->lock, flags);
598}
599
600static const char *stm32_type(struct uart_port *port)
601{
602 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
603}
604
605static void stm32_release_port(struct uart_port *port)
606{
607}
608
609static int stm32_request_port(struct uart_port *port)
610{
611 return 0;
612}
613
614static void stm32_config_port(struct uart_port *port, int flags)
615{
616 if (flags & UART_CONFIG_TYPE)
617 port->type = PORT_STM32;
618}
619
620static int
621stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
622{
623 /* No user changeable parameters */
624 return -EINVAL;
625}
626
627static void stm32_pm(struct uart_port *port, unsigned int state,
628 unsigned int oldstate)
629{
630 struct stm32_port *stm32port = container_of(port,
631 struct stm32_port, port);
632 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
633 struct stm32_usart_config *cfg = &stm32port->info->cfg;
634 unsigned long flags = 0;
635
636 switch (state) {
637 case UART_PM_STATE_ON:
638 clk_prepare_enable(stm32port->clk);
639 break;
640 case UART_PM_STATE_OFF:
641 spin_lock_irqsave(&port->lock, flags);
642 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
643 spin_unlock_irqrestore(&port->lock, flags);
644 clk_disable_unprepare(stm32port->clk);
645 break;
646 }
647}
648
649static const struct uart_ops stm32_uart_ops = {
650 .tx_empty = stm32_tx_empty,
651 .set_mctrl = stm32_set_mctrl,
652 .get_mctrl = stm32_get_mctrl,
653 .stop_tx = stm32_stop_tx,
654 .start_tx = stm32_start_tx,
655 .throttle = stm32_throttle,
656 .unthrottle = stm32_unthrottle,
657 .stop_rx = stm32_stop_rx,
658 .break_ctl = stm32_break_ctl,
659 .startup = stm32_startup,
660 .shutdown = stm32_shutdown,
661 .set_termios = stm32_set_termios,
662 .pm = stm32_pm,
663 .type = stm32_type,
664 .release_port = stm32_release_port,
665 .request_port = stm32_request_port,
666 .config_port = stm32_config_port,
667 .verify_port = stm32_verify_port,
668};
669
670static int stm32_init_port(struct stm32_port *stm32port,
671 struct platform_device *pdev)
672{
673 struct uart_port *port = &stm32port->port;
674 struct resource *res;
675 int ret;
676
677 port->iotype = UPIO_MEM;
678 port->flags = UPF_BOOT_AUTOCONF;
679 port->ops = &stm32_uart_ops;
680 port->dev = &pdev->dev;
681 port->irq = platform_get_irq(pdev, 0);
682 stm32port->wakeirq = platform_get_irq(pdev, 1);
683 stm32port->fifoen = stm32port->info->cfg.has_fifo;
684
685 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
686 port->membase = devm_ioremap_resource(&pdev->dev, res);
687 if (IS_ERR(port->membase))
688 return PTR_ERR(port->membase);
689 port->mapbase = res->start;
690
691 spin_lock_init(&port->lock);
692
693 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
694 if (IS_ERR(stm32port->clk))
695 return PTR_ERR(stm32port->clk);
696
697 /* Ensure that clk rate is correct by enabling the clk */
698 ret = clk_prepare_enable(stm32port->clk);
699 if (ret)
700 return ret;
701
702 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
703 if (!stm32port->port.uartclk) {
704 clk_disable_unprepare(stm32port->clk);
705 ret = -EINVAL;
706 }
707
708 return ret;
709}
710
711static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
712{
713 struct device_node *np = pdev->dev.of_node;
714 int id;
715
716 if (!np)
717 return NULL;
718
719 id = of_alias_get_id(np, "serial");
720 if (id < 0) {
721 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
722 return NULL;
723 }
724
725 if (WARN_ON(id >= STM32_MAX_PORTS))
726 return NULL;
727
728 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
729 "st,hw-flow-ctrl");
730 stm32_ports[id].port.line = id;
731 stm32_ports[id].last_res = RX_BUF_L;
732 return &stm32_ports[id];
733}
734
735#ifdef CONFIG_OF
736static const struct of_device_id stm32_match[] = {
737 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
738 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
739 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
740 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
741 { .compatible = "st,stm32h7-usart", .data = &stm32h7_info},
742 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
743 {},
744};
745
746MODULE_DEVICE_TABLE(of, stm32_match);
747#endif
748
749static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
750 struct platform_device *pdev)
751{
752 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
753 struct uart_port *port = &stm32port->port;
754 struct device *dev = &pdev->dev;
755 struct dma_slave_config config;
756 struct dma_async_tx_descriptor *desc = NULL;
757 dma_cookie_t cookie;
758 int ret;
759
760 /* Request DMA RX channel */
761 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
762 if (!stm32port->rx_ch) {
763 dev_info(dev, "rx dma alloc failed\n");
764 return -ENODEV;
765 }
766 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
767 &stm32port->rx_dma_buf,
768 GFP_KERNEL);
769 if (!stm32port->rx_buf) {
770 ret = -ENOMEM;
771 goto alloc_err;
772 }
773
774 /* Configure DMA channel */
775 memset(&config, 0, sizeof(config));
776 config.src_addr = port->mapbase + ofs->rdr;
777 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
778
779 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
780 if (ret < 0) {
781 dev_err(dev, "rx dma channel config failed\n");
782 ret = -ENODEV;
783 goto config_err;
784 }
785
786 /* Prepare a DMA cyclic transaction */
787 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
788 stm32port->rx_dma_buf,
789 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
790 DMA_PREP_INTERRUPT);
791 if (!desc) {
792 dev_err(dev, "rx dma prep cyclic failed\n");
793 ret = -ENODEV;
794 goto config_err;
795 }
796
797 /* No callback as dma buffer is drained on usart interrupt */
798 desc->callback = NULL;
799 desc->callback_param = NULL;
800
801 /* Push current DMA transaction in the pending queue */
802 cookie = dmaengine_submit(desc);
803
804 /* Issue pending DMA requests */
805 dma_async_issue_pending(stm32port->rx_ch);
806
807 return 0;
808
809config_err:
810 dma_free_coherent(&pdev->dev,
811 RX_BUF_L, stm32port->rx_buf,
812 stm32port->rx_dma_buf);
813
814alloc_err:
815 dma_release_channel(stm32port->rx_ch);
816 stm32port->rx_ch = NULL;
817
818 return ret;
819}
820
821static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
822 struct platform_device *pdev)
823{
824 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
825 struct uart_port *port = &stm32port->port;
826 struct device *dev = &pdev->dev;
827 struct dma_slave_config config;
828 int ret;
829
830 stm32port->tx_dma_busy = false;
831
832 /* Request DMA TX channel */
833 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
834 if (!stm32port->tx_ch) {
835 dev_info(dev, "tx dma alloc failed\n");
836 return -ENODEV;
837 }
838 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
839 &stm32port->tx_dma_buf,
840 GFP_KERNEL);
841 if (!stm32port->tx_buf) {
842 ret = -ENOMEM;
843 goto alloc_err;
844 }
845
846 /* Configure DMA channel */
847 memset(&config, 0, sizeof(config));
848 config.dst_addr = port->mapbase + ofs->tdr;
849 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
850
851 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
852 if (ret < 0) {
853 dev_err(dev, "tx dma channel config failed\n");
854 ret = -ENODEV;
855 goto config_err;
856 }
857
858 return 0;
859
860config_err:
861 dma_free_coherent(&pdev->dev,
862 TX_BUF_L, stm32port->tx_buf,
863 stm32port->tx_dma_buf);
864
865alloc_err:
866 dma_release_channel(stm32port->tx_ch);
867 stm32port->tx_ch = NULL;
868
869 return ret;
870}
871
872static int stm32_serial_probe(struct platform_device *pdev)
873{
874 const struct of_device_id *match;
875 struct stm32_port *stm32port;
876 int ret;
877
878 stm32port = stm32_of_get_stm32_port(pdev);
879 if (!stm32port)
880 return -ENODEV;
881
882 match = of_match_device(stm32_match, &pdev->dev);
883 if (match && match->data)
884 stm32port->info = (struct stm32_usart_info *)match->data;
885 else
886 return -EINVAL;
887
888 ret = stm32_init_port(stm32port, pdev);
889 if (ret)
890 return ret;
891
892 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) {
893 ret = device_init_wakeup(&pdev->dev, true);
894 if (ret)
895 goto err_uninit;
896
897 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
898 stm32port->wakeirq);
899 if (ret)
900 goto err_nowup;
901
902 device_set_wakeup_enable(&pdev->dev, false);
903 }
904
905 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
906 if (ret)
907 goto err_wirq;
908
909 ret = stm32_of_dma_rx_probe(stm32port, pdev);
910 if (ret)
911 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
912
913 ret = stm32_of_dma_tx_probe(stm32port, pdev);
914 if (ret)
915 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
916
917 platform_set_drvdata(pdev, &stm32port->port);
918
919 return 0;
920
921err_wirq:
922 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
923 dev_pm_clear_wake_irq(&pdev->dev);
924
925err_nowup:
926 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
927 device_init_wakeup(&pdev->dev, false);
928
929err_uninit:
930 clk_disable_unprepare(stm32port->clk);
931
932 return ret;
933}
934
935static int stm32_serial_remove(struct platform_device *pdev)
936{
937 struct uart_port *port = platform_get_drvdata(pdev);
938 struct stm32_port *stm32_port = to_stm32_port(port);
939 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
940 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
941
942 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
943
944 if (stm32_port->rx_ch)
945 dma_release_channel(stm32_port->rx_ch);
946
947 if (stm32_port->rx_dma_buf)
948 dma_free_coherent(&pdev->dev,
949 RX_BUF_L, stm32_port->rx_buf,
950 stm32_port->rx_dma_buf);
951
952 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
953
954 if (stm32_port->tx_ch)
955 dma_release_channel(stm32_port->tx_ch);
956
957 if (stm32_port->tx_dma_buf)
958 dma_free_coherent(&pdev->dev,
959 TX_BUF_L, stm32_port->tx_buf,
960 stm32_port->tx_dma_buf);
961
962 if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
963 dev_pm_clear_wake_irq(&pdev->dev);
964 device_init_wakeup(&pdev->dev, false);
965 }
966
967 clk_disable_unprepare(stm32_port->clk);
968
969 return uart_remove_one_port(&stm32_usart_driver, port);
970}
971
972
973#ifdef CONFIG_SERIAL_STM32_CONSOLE
974static void stm32_console_putchar(struct uart_port *port, int ch)
975{
976 struct stm32_port *stm32_port = to_stm32_port(port);
977 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
978
979 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
980 cpu_relax();
981
982 writel_relaxed(ch, port->membase + ofs->tdr);
983}
984
985static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
986{
987 struct uart_port *port = &stm32_ports[co->index].port;
988 struct stm32_port *stm32_port = to_stm32_port(port);
989 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
990 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
991 unsigned long flags;
992 u32 old_cr1, new_cr1;
993 int locked = 1;
994
995 local_irq_save(flags);
996 if (port->sysrq)
997 locked = 0;
998 else if (oops_in_progress)
999 locked = spin_trylock(&port->lock);
1000 else
1001 spin_lock(&port->lock);
1002
1003 /* Save and disable interrupts, enable the transmitter */
1004 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1005 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1006 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
1007 writel_relaxed(new_cr1, port->membase + ofs->cr1);
1008
1009 uart_console_write(port, s, cnt, stm32_console_putchar);
1010
1011 /* Restore interrupt state */
1012 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1013
1014 if (locked)
1015 spin_unlock(&port->lock);
1016 local_irq_restore(flags);
1017}
1018
1019static int stm32_console_setup(struct console *co, char *options)
1020{
1021 struct stm32_port *stm32port;
1022 int baud = 9600;
1023 int bits = 8;
1024 int parity = 'n';
1025 int flow = 'n';
1026
1027 if (co->index >= STM32_MAX_PORTS)
1028 return -ENODEV;
1029
1030 stm32port = &stm32_ports[co->index];
1031
1032 /*
1033 * This driver does not support early console initialization
1034 * (use ARM early printk support instead), so we only expect
1035 * this to be called during the uart port registration when the
1036 * driver gets probed and the port should be mapped at that point.
1037 */
1038 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1039 return -ENXIO;
1040
1041 if (options)
1042 uart_parse_options(options, &baud, &parity, &bits, &flow);
1043
1044 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1045}
1046
1047static struct console stm32_console = {
1048 .name = STM32_SERIAL_NAME,
1049 .device = uart_console_device,
1050 .write = stm32_console_write,
1051 .setup = stm32_console_setup,
1052 .flags = CON_PRINTBUFFER,
1053 .index = -1,
1054 .data = &stm32_usart_driver,
1055};
1056
1057#define STM32_SERIAL_CONSOLE (&stm32_console)
1058
1059#else
1060#define STM32_SERIAL_CONSOLE NULL
1061#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1062
1063static struct uart_driver stm32_usart_driver = {
1064 .driver_name = DRIVER_NAME,
1065 .dev_name = STM32_SERIAL_NAME,
1066 .major = 0,
1067 .minor = 0,
1068 .nr = STM32_MAX_PORTS,
1069 .cons = STM32_SERIAL_CONSOLE,
1070};
1071
1072#ifdef CONFIG_PM_SLEEP
1073static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable)
1074{
1075 struct stm32_port *stm32_port = to_stm32_port(port);
1076 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1077 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1078 u32 val;
1079
1080 if (!cfg->has_wakeup || stm32_port->wakeirq < 0)
1081 return;
1082
1083 if (enable) {
1084 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1085 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1086 val = readl_relaxed(port->membase + ofs->cr3);
1087 val &= ~USART_CR3_WUS_MASK;
1088 /* Enable Wake up interrupt from low power on start bit */
1089 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1090 writel_relaxed(val, port->membase + ofs->cr3);
1091 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1092 } else {
1093 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1094 }
1095}
1096
1097static int stm32_serial_suspend(struct device *dev)
1098{
1099 struct uart_port *port = dev_get_drvdata(dev);
1100
1101 uart_suspend_port(&stm32_usart_driver, port);
1102
1103 if (device_may_wakeup(dev))
1104 stm32_serial_enable_wakeup(port, true);
1105 else
1106 stm32_serial_enable_wakeup(port, false);
1107
1108 return 0;
1109}
1110
1111static int stm32_serial_resume(struct device *dev)
1112{
1113 struct uart_port *port = dev_get_drvdata(dev);
1114
1115 if (device_may_wakeup(dev))
1116 stm32_serial_enable_wakeup(port, false);
1117
1118 return uart_resume_port(&stm32_usart_driver, port);
1119}
1120#endif /* CONFIG_PM_SLEEP */
1121
1122static const struct dev_pm_ops stm32_serial_pm_ops = {
1123 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1124};
1125
1126static struct platform_driver stm32_serial_driver = {
1127 .probe = stm32_serial_probe,
1128 .remove = stm32_serial_remove,
1129 .driver = {
1130 .name = DRIVER_NAME,
1131 .pm = &stm32_serial_pm_ops,
1132 .of_match_table = of_match_ptr(stm32_match),
1133 },
1134};
1135
1136static int __init usart_init(void)
1137{
1138 static char banner[] __initdata = "STM32 USART driver initialized";
1139 int ret;
1140
1141 pr_info("%s\n", banner);
1142
1143 ret = uart_register_driver(&stm32_usart_driver);
1144 if (ret)
1145 return ret;
1146
1147 ret = platform_driver_register(&stm32_serial_driver);
1148 if (ret)
1149 uart_unregister_driver(&stm32_usart_driver);
1150
1151 return ret;
1152}
1153
1154static void __exit usart_exit(void)
1155{
1156 platform_driver_unregister(&stm32_serial_driver);
1157 uart_unregister_driver(&stm32_usart_driver);
1158}
1159
1160module_init(usart_init);
1161module_exit(usart_exit);
1162
1163MODULE_ALIAS("platform:" DRIVER_NAME);
1164MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1165MODULE_LICENSE("GPL v2");