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