blob: 5e0b9f783cacb21ac0b8aaa6300aa3010297c749 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/pinctrl/consumer.h>
28#include <linux/platform_device.h>
29#include <linux/pm_runtime.h>
30#include <linux/pm_wakeirq.h>
31#include <linux/serial_core.h>
32#include <linux/serial.h>
33#include <linux/spinlock.h>
34#include <linux/sysrq.h>
35#include <linux/tty_flip.h>
36#include <linux/tty.h>
37
38#include "stm32-usart.h"
39
40static void stm32_stop_tx(struct uart_port *port);
41static void stm32_transmit_chars(struct uart_port *port);
42
43static inline struct stm32_port *to_stm32_port(struct uart_port *port)
44{
45 return container_of(port, struct stm32_port, port);
46}
47
48static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
49{
50 u32 val;
51
52 val = readl_relaxed(port->membase + reg);
53 val |= bits;
54 writel_relaxed(val, port->membase + reg);
55}
56
57static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
58{
59 u32 val;
60
61 val = readl_relaxed(port->membase + reg);
62 val &= ~bits;
63 writel_relaxed(val, port->membase + reg);
64}
65
66static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
67 u32 delay_DDE, u32 baud)
68{
69 u32 rs485_deat_dedt;
70 u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
71 bool over8;
72
73 *cr3 |= USART_CR3_DEM;
74 over8 = *cr1 & USART_CR1_OVER8;
75
76 *cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
77
78 if (over8)
79 rs485_deat_dedt = delay_ADE * baud * 8;
80 else
81 rs485_deat_dedt = delay_ADE * baud * 16;
82
83 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
84 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
85 rs485_deat_dedt_max : rs485_deat_dedt;
86 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
87 USART_CR1_DEAT_MASK;
88 *cr1 |= rs485_deat_dedt;
89
90 if (over8)
91 rs485_deat_dedt = delay_DDE * baud * 8;
92 else
93 rs485_deat_dedt = delay_DDE * baud * 16;
94
95 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
96 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
97 rs485_deat_dedt_max : rs485_deat_dedt;
98 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
99 USART_CR1_DEDT_MASK;
100 *cr1 |= rs485_deat_dedt;
101}
102
103static int stm32_config_rs485(struct uart_port *port,
104 struct serial_rs485 *rs485conf)
105{
106 struct stm32_port *stm32_port = to_stm32_port(port);
107 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
108 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
109 u32 usartdiv, baud, cr1, cr3;
110 bool over8;
111
112 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
113
114 port->rs485 = *rs485conf;
115
116 rs485conf->flags |= SER_RS485_RX_DURING_TX;
117
118 if (rs485conf->flags & SER_RS485_ENABLED) {
119 cr1 = readl_relaxed(port->membase + ofs->cr1);
120 cr3 = readl_relaxed(port->membase + ofs->cr3);
121 usartdiv = readl_relaxed(port->membase + ofs->brr);
122 usartdiv = usartdiv & GENMASK(15, 0);
123 over8 = cr1 & USART_CR1_OVER8;
124
125 if (over8)
126 usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
127 << USART_BRR_04_R_SHIFT;
128
129 baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
130 stm32_config_reg_rs485(&cr1, &cr3,
131 rs485conf->delay_rts_before_send,
132 rs485conf->delay_rts_after_send, baud);
133
134 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
135 cr3 &= ~USART_CR3_DEP;
136 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
137 } else {
138 cr3 |= USART_CR3_DEP;
139 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
140 }
141
142 writel_relaxed(cr3, port->membase + ofs->cr3);
143 writel_relaxed(cr1, port->membase + ofs->cr1);
144 } else {
145 stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP);
146 stm32_clr_bits(port, ofs->cr1,
147 USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
148 }
149
150 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
151
152 return 0;
153}
154
155static int stm32_init_rs485(struct uart_port *port,
156 struct platform_device *pdev)
157{
158 struct serial_rs485 *rs485conf = &port->rs485;
159
160 rs485conf->flags = 0;
161 rs485conf->delay_rts_before_send = 0;
162 rs485conf->delay_rts_after_send = 0;
163
164 if (!pdev->dev.of_node)
165 return -ENODEV;
166
167 uart_get_rs485_mode(&pdev->dev, rs485conf);
168
169 return 0;
170}
171
172static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
173 bool threaded)
174{
175 struct stm32_port *stm32_port = to_stm32_port(port);
176 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
177 enum dma_status status;
178 struct dma_tx_state state;
179
180 *sr = readl_relaxed(port->membase + ofs->isr);
181
182 if (threaded && stm32_port->rx_ch) {
183 status = dmaengine_tx_status(stm32_port->rx_ch,
184 stm32_port->rx_ch->cookie,
185 &state);
186 if ((status == DMA_IN_PROGRESS) &&
187 (*last_res != state.residue))
188 return 1;
189 else
190 return 0;
191 } else if (*sr & USART_SR_RXNE) {
192 return 1;
193 }
194 return 0;
195}
196
197static unsigned long stm32_get_char(struct uart_port *port, u32 *sr,
198 int *last_res)
199{
200 struct stm32_port *stm32_port = to_stm32_port(port);
201 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
202 unsigned long c;
203
204 if (stm32_port->rx_ch) {
205 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
206 if ((*last_res) == 0)
207 *last_res = RX_BUF_L;
208 } else {
209 c = readl_relaxed(port->membase + ofs->rdr);
210 /* apply RDR data mask */
211 c &= stm32_port->rdr_mask;
212 }
213
214 return c;
215}
216
217static void stm32_receive_chars(struct uart_port *port, bool threaded)
218{
219 struct tty_port *tport = &port->state->port;
220 struct stm32_port *stm32_port = to_stm32_port(port);
221 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
222 unsigned long c;
223 u32 sr;
224 char flag;
225
226 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
227 pm_wakeup_event(tport->tty->dev, 0);
228
229 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
230 sr |= USART_SR_DUMMY_RX;
231 flag = TTY_NORMAL;
232
233 /*
234 * Status bits has to be cleared before reading the RDR:
235 * In FIFO mode, reading the RDR will pop the next data
236 * (if any) along with its status bits into the SR.
237 * Not doing so leads to misalignement between RDR and SR,
238 * and clear status bits of the next rx data.
239 *
240 * Clear errors flags for stm32f7 and stm32h7 compatible
241 * devices. On stm32f4 compatible devices, the error bit is
242 * cleared by the sequence [read SR - read DR].
243 */
244 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
245 writel_relaxed(sr & USART_SR_ERR_MASK,
246 port->membase + ofs->icr);
247
248 c = stm32_get_char(port, &sr, &stm32_port->last_res);
249 port->icount.rx++;
250 if (sr & USART_SR_ERR_MASK) {
251 if (sr & USART_SR_ORE) {
252 port->icount.overrun++;
253 } else if (sr & USART_SR_PE) {
254 port->icount.parity++;
255 } else if (sr & USART_SR_FE) {
256 /* Break detection if character is null */
257 if (!c) {
258 port->icount.brk++;
259 if (uart_handle_break(port))
260 continue;
261 } else {
262 port->icount.frame++;
263 }
264 }
265
266 sr &= port->read_status_mask;
267
268 if (sr & USART_SR_PE) {
269 flag = TTY_PARITY;
270 } else if (sr & USART_SR_FE) {
271 if (!c)
272 flag = TTY_BREAK;
273 else
274 flag = TTY_FRAME;
275 }
276 }
277
278 if (uart_handle_sysrq_char(port, c))
279 continue;
280 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
281 }
282
283 spin_unlock(&port->lock);
284 tty_flip_buffer_push(tport);
285 spin_lock(&port->lock);
286}
287
288static void stm32_tx_dma_complete(void *arg)
289{
290 struct uart_port *port = arg;
291 struct stm32_port *stm32port = to_stm32_port(port);
292 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
293
294 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
295 stm32port->tx_dma_busy = false;
296
297 /* Let's see if we have pending data to send */
298 stm32_transmit_chars(port);
299}
300
301static void stm32_tx_interrupt_enable(struct uart_port *port)
302{
303 struct stm32_port *stm32_port = to_stm32_port(port);
304 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
305
306 /*
307 * Enables TX FIFO threashold irq when FIFO is enabled,
308 * or TX empty irq when FIFO is disabled
309 */
310 if (stm32_port->fifoen)
311 stm32_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
312 else
313 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
314}
315
316static void stm32_tx_interrupt_disable(struct uart_port *port)
317{
318 struct stm32_port *stm32_port = to_stm32_port(port);
319 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
320
321 if (stm32_port->fifoen)
322 stm32_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
323 else
324 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
325}
326
327static void stm32_transmit_chars_pio(struct uart_port *port)
328{
329 struct stm32_port *stm32_port = to_stm32_port(port);
330 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
331 struct circ_buf *xmit = &port->state->xmit;
332
333 if (stm32_port->tx_dma_busy) {
334 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
335 stm32_port->tx_dma_busy = false;
336 }
337
338 while (!uart_circ_empty(xmit)) {
339 /* Check that TDR is empty before filling FIFO */
340 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
341 break;
342 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
343 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
344 port->icount.tx++;
345 }
346
347 /* rely on TXE irq (mask or unmask) for sending remaining data */
348 if (uart_circ_empty(xmit))
349 stm32_tx_interrupt_disable(port);
350 else
351 stm32_tx_interrupt_enable(port);
352}
353
354static void stm32_transmit_chars_dma(struct uart_port *port)
355{
356 struct stm32_port *stm32port = to_stm32_port(port);
357 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
358 struct circ_buf *xmit = &port->state->xmit;
359 struct dma_async_tx_descriptor *desc = NULL;
360 dma_cookie_t cookie;
361 unsigned int count, i;
362
363 if (stm32port->tx_dma_busy)
364 return;
365
366 stm32port->tx_dma_busy = true;
367
368 count = uart_circ_chars_pending(xmit);
369
370 if (count > TX_BUF_L)
371 count = TX_BUF_L;
372
373 if (xmit->tail < xmit->head) {
374 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
375 } else {
376 size_t one = UART_XMIT_SIZE - xmit->tail;
377 size_t two;
378
379 if (one > count)
380 one = count;
381 two = count - one;
382
383 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
384 if (two)
385 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
386 }
387
388 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
389 stm32port->tx_dma_buf,
390 count,
391 DMA_MEM_TO_DEV,
392 DMA_PREP_INTERRUPT);
393
394 if (!desc) {
395 for (i = count; i > 0; i--)
396 stm32_transmit_chars_pio(port);
397 return;
398 }
399
400 desc->callback = stm32_tx_dma_complete;
401 desc->callback_param = port;
402
403 /* Push current DMA TX transaction in the pending queue */
404 cookie = dmaengine_submit(desc);
405
406 /* Issue pending DMA TX requests */
407 dma_async_issue_pending(stm32port->tx_ch);
408
409 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
410
411 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
412 port->icount.tx += count;
413}
414
415static void stm32_transmit_chars(struct uart_port *port)
416{
417 struct stm32_port *stm32_port = to_stm32_port(port);
418 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
419 struct circ_buf *xmit = &port->state->xmit;
420
421 if (port->x_char) {
422 if (stm32_port->tx_dma_busy)
423 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
424 writel_relaxed(port->x_char, port->membase + ofs->tdr);
425 port->x_char = 0;
426 port->icount.tx++;
427 if (stm32_port->tx_dma_busy)
428 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
429 return;
430 }
431
432 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
433 stm32_tx_interrupt_disable(port);
434 return;
435 }
436
437 if (ofs->icr == UNDEF_REG)
438 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
439 else
440 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
441
442 if (stm32_port->tx_ch)
443 stm32_transmit_chars_dma(port);
444 else
445 stm32_transmit_chars_pio(port);
446
447 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
448 uart_write_wakeup(port);
449
450 if (uart_circ_empty(xmit))
451 stm32_tx_interrupt_disable(port);
452}
453
454static irqreturn_t stm32_interrupt(int irq, void *ptr)
455{
456 struct uart_port *port = ptr;
457 struct stm32_port *stm32_port = to_stm32_port(port);
458 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
459 u32 sr;
460
461 spin_lock(&port->lock);
462
463 sr = readl_relaxed(port->membase + ofs->isr);
464
465 if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
466 writel_relaxed(USART_ICR_RTOCF,
467 port->membase + ofs->icr);
468
469 if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
470 writel_relaxed(USART_ICR_WUCF,
471 port->membase + ofs->icr);
472
473 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
474 stm32_receive_chars(port, false);
475
476 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
477 stm32_transmit_chars(port);
478
479 spin_unlock(&port->lock);
480
481 if (stm32_port->rx_ch)
482 return IRQ_WAKE_THREAD;
483 else
484 return IRQ_HANDLED;
485}
486
487static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
488{
489 struct uart_port *port = ptr;
490 struct stm32_port *stm32_port = to_stm32_port(port);
491
492 spin_lock(&port->lock);
493
494 if (stm32_port->rx_ch)
495 stm32_receive_chars(port, true);
496
497 spin_unlock(&port->lock);
498
499 return IRQ_HANDLED;
500}
501
502static unsigned int stm32_tx_empty(struct uart_port *port)
503{
504 struct stm32_port *stm32_port = to_stm32_port(port);
505 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
506
507 if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
508 return TIOCSER_TEMT;
509
510 return 0;
511}
512
513static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
514{
515 struct stm32_port *stm32_port = to_stm32_port(port);
516 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
517
518 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
519 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
520 else
521 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
522}
523
524static unsigned int stm32_get_mctrl(struct uart_port *port)
525{
526 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
527 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
528}
529
530/* Transmit stop */
531static void stm32_stop_tx(struct uart_port *port)
532{
533 stm32_tx_interrupt_disable(port);
534}
535
536/* There are probably characters waiting to be transmitted. */
537static void stm32_start_tx(struct uart_port *port)
538{
539 struct circ_buf *xmit = &port->state->xmit;
540
541 if (uart_circ_empty(xmit) && !port->x_char)
542 return;
543
544 stm32_transmit_chars(port);
545}
546
547/* Throttle the remote when input buffer is about to overflow. */
548static void stm32_throttle(struct uart_port *port)
549{
550 struct stm32_port *stm32_port = to_stm32_port(port);
551 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
552 unsigned long flags;
553
554 spin_lock_irqsave(&port->lock, flags);
555 stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
556 if (stm32_port->cr3_irq)
557 stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
558
559 spin_unlock_irqrestore(&port->lock, flags);
560}
561
562/* Unthrottle the remote, the input buffer can now accept data. */
563static void stm32_unthrottle(struct uart_port *port)
564{
565 struct stm32_port *stm32_port = to_stm32_port(port);
566 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
567 unsigned long flags;
568
569 spin_lock_irqsave(&port->lock, flags);
570 stm32_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
571 if (stm32_port->cr3_irq)
572 stm32_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
573
574 spin_unlock_irqrestore(&port->lock, flags);
575}
576
577/* Receive stop */
578static void stm32_stop_rx(struct uart_port *port)
579{
580 struct stm32_port *stm32_port = to_stm32_port(port);
581 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
582
583 stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
584 if (stm32_port->cr3_irq)
585 stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
586
587}
588
589/* Handle breaks - ignored by us */
590static void stm32_break_ctl(struct uart_port *port, int break_state)
591{
592}
593
594static int stm32_startup(struct uart_port *port)
595{
596 struct stm32_port *stm32_port = to_stm32_port(port);
597 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
598 const char *name = to_platform_device(port->dev)->name;
599 u32 val;
600 int ret;
601
602 ret = request_threaded_irq(port->irq, stm32_interrupt,
603 stm32_threaded_interrupt,
604 IRQF_NO_SUSPEND, name, port);
605 if (ret)
606 return ret;
607
608 /* RX FIFO Flush */
609 if (ofs->rqr != UNDEF_REG)
610 stm32_set_bits(port, ofs->rqr, USART_RQR_RXFRQ);
611
612 /* Tx and RX FIFO configuration */
613 if (stm32_port->fifoen) {
614 val = readl_relaxed(port->membase + ofs->cr3);
615 val &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK);
616 val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT;
617 val |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT;
618 writel_relaxed(val, port->membase + ofs->cr3);
619 }
620
621 /* RX FIFO enabling */
622 val = stm32_port->cr1_irq | USART_CR1_RE;
623 if (stm32_port->fifoen)
624 val |= USART_CR1_FIFOEN;
625 stm32_set_bits(port, ofs->cr1, val);
626
627 return 0;
628}
629
630static void stm32_shutdown(struct uart_port *port)
631{
632 struct stm32_port *stm32_port = to_stm32_port(port);
633 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
634 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
635 u32 val, isr;
636 int ret;
637
638 val = USART_CR1_TXEIE | USART_CR1_TE;
639 val |= stm32_port->cr1_irq | USART_CR1_RE;
640 val |= BIT(cfg->uart_enable_bit);
641 if (stm32_port->fifoen)
642 val |= USART_CR1_FIFOEN;
643
644 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
645 isr, (isr & USART_SR_TC),
646 10, 100000);
647
648 if (ret)
649 dev_err(port->dev, "transmission complete not set\n");
650
651 stm32_clr_bits(port, ofs->cr1, val);
652
653 free_irq(port->irq, port);
654}
655
656static unsigned int stm32_get_databits(struct ktermios *termios)
657{
658 unsigned int bits;
659
660 tcflag_t cflag = termios->c_cflag;
661
662 switch (cflag & CSIZE) {
663 /*
664 * CSIZE settings are not necessarily supported in hardware.
665 * CSIZE unsupported configurations are handled here to set word length
666 * to 8 bits word as default configuration and to print debug message.
667 */
668 case CS5:
669 bits = 5;
670 break;
671 case CS6:
672 bits = 6;
673 break;
674 case CS7:
675 bits = 7;
676 break;
677 /* default including CS8 */
678 default:
679 bits = 8;
680 break;
681 }
682
683 return bits;
684}
685
686static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
687 struct ktermios *old)
688{
689 struct stm32_port *stm32_port = to_stm32_port(port);
690 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
691 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
692 struct serial_rs485 *rs485conf = &port->rs485;
693 unsigned int baud, bits;
694 u32 usartdiv, mantissa, fraction, oversampling;
695 tcflag_t cflag = termios->c_cflag;
696 u32 cr1, cr2, cr3, isr;
697 unsigned long flags;
698 int ret;
699
700 if (!stm32_port->hw_flow_control)
701 cflag &= ~CRTSCTS;
702
703 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
704
705 spin_lock_irqsave(&port->lock, flags);
706
707 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
708 isr,
709 (isr & USART_SR_TC),
710 10, 100000);
711
712 /* Send the TC error message only when ISR_TC is not set. */
713 if (ret)
714 dev_err(port->dev, "Transmission is not complete\n");
715
716 /* Stop serial port and reset value */
717 writel_relaxed(0, port->membase + ofs->cr1);
718
719 /* flush RX & TX FIFO */
720 if (ofs->rqr != UNDEF_REG)
721 stm32_set_bits(port, ofs->rqr,
722 USART_RQR_TXFRQ | USART_RQR_RXFRQ);
723
724 cr1 = USART_CR1_TE | USART_CR1_RE;
725 if (stm32_port->fifoen)
726 cr1 |= USART_CR1_FIFOEN;
727 cr2 = 0;
728 cr3 = readl_relaxed(port->membase + ofs->cr3);
729 cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG_MASK | USART_CR3_RXFTIE
730 | USART_CR3_TXFTCFG_MASK;
731
732 if (cflag & CSTOPB)
733 cr2 |= USART_CR2_STOP_2B;
734
735 bits = stm32_get_databits(termios);
736 stm32_port->rdr_mask = (BIT(bits) - 1);
737
738 if (cflag & PARENB) {
739 bits++;
740 cr1 |= USART_CR1_PCE;
741 }
742
743 /*
744 * Word length configuration:
745 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
746 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
747 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
748 * M0 and M1 already cleared by cr1 initialization.
749 */
750 if (bits == 9) {
751 cr1 |= USART_CR1_M0;
752 } else if ((bits == 7) && cfg->has_7bits_data) {
753 cr1 |= USART_CR1_M1;
754 } else if (bits != 8) {
755 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
756 , bits);
757 cflag &= ~CSIZE;
758 cflag |= CS8;
759 termios->c_cflag = cflag;
760 bits = 8;
761 if (cflag & PARENB) {
762 bits++;
763 cr1 |= USART_CR1_M0;
764 }
765 }
766
767 if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
768 stm32_port->fifoen)) {
769 if (cflag & CSTOPB)
770 bits = bits + 3; /* 1 start bit + 2 stop bits */
771 else
772 bits = bits + 2; /* 1 start bit + 1 stop bit */
773
774 /* RX timeout irq to occur after last stop bit + bits */
775 stm32_port->cr1_irq = USART_CR1_RTOIE;
776 writel_relaxed(bits, port->membase + ofs->rtor);
777 cr2 |= USART_CR2_RTOEN;
778 /* Not using dma, enable fifo threshold irq */
779 if (!stm32_port->rx_ch)
780 stm32_port->cr3_irq = USART_CR3_RXFTIE;
781 }
782
783 cr1 |= stm32_port->cr1_irq;
784 cr3 |= stm32_port->cr3_irq;
785
786 if (cflag & PARODD)
787 cr1 |= USART_CR1_PS;
788
789 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
790 if (cflag & CRTSCTS) {
791 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
792 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
793 }
794
795 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
796
797 /*
798 * The USART supports 16 or 8 times oversampling.
799 * By default we prefer 16 times oversampling, so that the receiver
800 * has a better tolerance to clock deviations.
801 * 8 times oversampling is only used to achieve higher speeds.
802 */
803 if (usartdiv < 16) {
804 oversampling = 8;
805 cr1 |= USART_CR1_OVER8;
806 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
807 } else {
808 oversampling = 16;
809 cr1 &= ~USART_CR1_OVER8;
810 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
811 }
812
813 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
814 fraction = usartdiv % oversampling;
815 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
816
817 uart_update_timeout(port, cflag, baud);
818
819 port->read_status_mask = USART_SR_ORE;
820 if (termios->c_iflag & INPCK)
821 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
822 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
823 port->read_status_mask |= USART_SR_FE;
824
825 /* Characters to ignore */
826 port->ignore_status_mask = 0;
827 if (termios->c_iflag & IGNPAR)
828 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
829 if (termios->c_iflag & IGNBRK) {
830 port->ignore_status_mask |= USART_SR_FE;
831 /*
832 * If we're ignoring parity and break indicators,
833 * ignore overruns too (for real raw support).
834 */
835 if (termios->c_iflag & IGNPAR)
836 port->ignore_status_mask |= USART_SR_ORE;
837 }
838
839 /* Ignore all characters if CREAD is not set */
840 if ((termios->c_cflag & CREAD) == 0)
841 port->ignore_status_mask |= USART_SR_DUMMY_RX;
842
843 if (stm32_port->rx_ch)
844 cr3 |= USART_CR3_DMAR;
845
846 if (rs485conf->flags & SER_RS485_ENABLED) {
847 stm32_config_reg_rs485(&cr1, &cr3,
848 rs485conf->delay_rts_before_send,
849 rs485conf->delay_rts_after_send, baud);
850 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
851 cr3 &= ~USART_CR3_DEP;
852 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
853 } else {
854 cr3 |= USART_CR3_DEP;
855 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
856 }
857
858 } else {
859 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
860 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
861 }
862
863 writel_relaxed(cr3, port->membase + ofs->cr3);
864 writel_relaxed(cr2, port->membase + ofs->cr2);
865 writel_relaxed(cr1, port->membase + ofs->cr1);
866
867 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
868 spin_unlock_irqrestore(&port->lock, flags);
869}
870
871static const char *stm32_type(struct uart_port *port)
872{
873 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
874}
875
876static void stm32_release_port(struct uart_port *port)
877{
878}
879
880static int stm32_request_port(struct uart_port *port)
881{
882 return 0;
883}
884
885static void stm32_config_port(struct uart_port *port, int flags)
886{
887 if (flags & UART_CONFIG_TYPE)
888 port->type = PORT_STM32;
889}
890
891static int
892stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
893{
894 /* No user changeable parameters */
895 return -EINVAL;
896}
897
898static void stm32_pm(struct uart_port *port, unsigned int state,
899 unsigned int oldstate)
900{
901 struct stm32_port *stm32port = container_of(port,
902 struct stm32_port, port);
903 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
904 struct stm32_usart_config *cfg = &stm32port->info->cfg;
905 unsigned long flags = 0;
906
907 switch (state) {
908 case UART_PM_STATE_ON:
909 pm_runtime_get_sync(port->dev);
910 break;
911 case UART_PM_STATE_OFF:
912 spin_lock_irqsave(&port->lock, flags);
913 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
914 spin_unlock_irqrestore(&port->lock, flags);
915 pm_runtime_put_sync(port->dev);
916 break;
917 }
918}
919
920static const struct uart_ops stm32_uart_ops = {
921 .tx_empty = stm32_tx_empty,
922 .set_mctrl = stm32_set_mctrl,
923 .get_mctrl = stm32_get_mctrl,
924 .stop_tx = stm32_stop_tx,
925 .start_tx = stm32_start_tx,
926 .throttle = stm32_throttle,
927 .unthrottle = stm32_unthrottle,
928 .stop_rx = stm32_stop_rx,
929 .break_ctl = stm32_break_ctl,
930 .startup = stm32_startup,
931 .shutdown = stm32_shutdown,
932 .set_termios = stm32_set_termios,
933 .pm = stm32_pm,
934 .type = stm32_type,
935 .release_port = stm32_release_port,
936 .request_port = stm32_request_port,
937 .config_port = stm32_config_port,
938 .verify_port = stm32_verify_port,
939};
940
941static int stm32_init_port(struct stm32_port *stm32port,
942 struct platform_device *pdev)
943{
944 struct uart_port *port = &stm32port->port;
945 struct resource *res;
946 int ret;
947
948 port->iotype = UPIO_MEM;
949 port->flags = UPF_BOOT_AUTOCONF;
950 port->ops = &stm32_uart_ops;
951 port->dev = &pdev->dev;
952 port->fifosize = stm32port->info->cfg.fifosize;
953
954 ret = platform_get_irq(pdev, 0);
955 if (ret <= 0)
956 return ret ? : -ENODEV;
957 port->irq = ret;
958
959 port->rs485_config = stm32_config_rs485;
960
961 stm32_init_rs485(port, pdev);
962
963 if (stm32port->info->cfg.has_wakeup) {
964 stm32port->wakeirq = platform_get_irq_optional(pdev, 1);
965 if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO)
966 return stm32port->wakeirq ? : -ENODEV;
967 }
968
969 stm32port->fifoen = stm32port->info->cfg.has_fifo;
970
971 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
972 port->membase = devm_ioremap_resource(&pdev->dev, res);
973 if (IS_ERR(port->membase))
974 return PTR_ERR(port->membase);
975 port->mapbase = res->start;
976
977 spin_lock_init(&port->lock);
978
979 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
980 if (IS_ERR(stm32port->clk))
981 return PTR_ERR(stm32port->clk);
982
983 /* Ensure that clk rate is correct by enabling the clk */
984 ret = clk_prepare_enable(stm32port->clk);
985 if (ret)
986 return ret;
987
988 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
989 if (!stm32port->port.uartclk) {
990 clk_disable_unprepare(stm32port->clk);
991 ret = -EINVAL;
992 }
993
994 return ret;
995}
996
997static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
998{
999 struct device_node *np = pdev->dev.of_node;
1000 int id;
1001
1002 if (!np)
1003 return NULL;
1004
1005 id = of_alias_get_id(np, "serial");
1006 if (id < 0) {
1007 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1008 return NULL;
1009 }
1010
1011 if (WARN_ON(id >= STM32_MAX_PORTS))
1012 return NULL;
1013
1014 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
1015 "st,hw-flow-ctrl");
1016 stm32_ports[id].port.line = id;
1017 stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1018 stm32_ports[id].cr3_irq = 0;
1019 stm32_ports[id].last_res = RX_BUF_L;
1020 return &stm32_ports[id];
1021}
1022
1023#ifdef CONFIG_OF
1024static const struct of_device_id stm32_match[] = {
1025 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
1026 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1027 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
1028 {},
1029};
1030
1031MODULE_DEVICE_TABLE(of, stm32_match);
1032#endif
1033
1034static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
1035 struct platform_device *pdev)
1036{
1037 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1038 struct uart_port *port = &stm32port->port;
1039 struct device *dev = &pdev->dev;
1040 struct dma_slave_config config;
1041 struct dma_async_tx_descriptor *desc = NULL;
1042 dma_cookie_t cookie;
1043 int ret;
1044
1045 /* Request DMA RX channel */
1046 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
1047 if (!stm32port->rx_ch) {
1048 dev_info(dev, "rx dma alloc failed\n");
1049 return -ENODEV;
1050 }
1051 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
1052 &stm32port->rx_dma_buf,
1053 GFP_KERNEL);
1054 if (!stm32port->rx_buf) {
1055 ret = -ENOMEM;
1056 goto alloc_err;
1057 }
1058
1059 /* Configure DMA channel */
1060 memset(&config, 0, sizeof(config));
1061 config.src_addr = port->mapbase + ofs->rdr;
1062 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1063
1064 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
1065 if (ret < 0) {
1066 dev_err(dev, "rx dma channel config failed\n");
1067 ret = -ENODEV;
1068 goto config_err;
1069 }
1070
1071 /* Prepare a DMA cyclic transaction */
1072 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
1073 stm32port->rx_dma_buf,
1074 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
1075 DMA_PREP_INTERRUPT);
1076 if (!desc) {
1077 dev_err(dev, "rx dma prep cyclic failed\n");
1078 ret = -ENODEV;
1079 goto config_err;
1080 }
1081
1082 /* No callback as dma buffer is drained on usart interrupt */
1083 desc->callback = NULL;
1084 desc->callback_param = NULL;
1085
1086 /* Push current DMA transaction in the pending queue */
1087 cookie = dmaengine_submit(desc);
1088
1089 /* Issue pending DMA requests */
1090 dma_async_issue_pending(stm32port->rx_ch);
1091
1092 return 0;
1093
1094config_err:
1095 dma_free_coherent(&pdev->dev,
1096 RX_BUF_L, stm32port->rx_buf,
1097 stm32port->rx_dma_buf);
1098
1099alloc_err:
1100 dma_release_channel(stm32port->rx_ch);
1101 stm32port->rx_ch = NULL;
1102
1103 return ret;
1104}
1105
1106static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
1107 struct platform_device *pdev)
1108{
1109 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1110 struct uart_port *port = &stm32port->port;
1111 struct device *dev = &pdev->dev;
1112 struct dma_slave_config config;
1113 int ret;
1114
1115 stm32port->tx_dma_busy = false;
1116
1117 /* Request DMA TX channel */
1118 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
1119 if (!stm32port->tx_ch) {
1120 dev_info(dev, "tx dma alloc failed\n");
1121 return -ENODEV;
1122 }
1123 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
1124 &stm32port->tx_dma_buf,
1125 GFP_KERNEL);
1126 if (!stm32port->tx_buf) {
1127 ret = -ENOMEM;
1128 goto alloc_err;
1129 }
1130
1131 /* Configure DMA channel */
1132 memset(&config, 0, sizeof(config));
1133 config.dst_addr = port->mapbase + ofs->tdr;
1134 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1135
1136 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1137 if (ret < 0) {
1138 dev_err(dev, "tx dma channel config failed\n");
1139 ret = -ENODEV;
1140 goto config_err;
1141 }
1142
1143 return 0;
1144
1145config_err:
1146 dma_free_coherent(&pdev->dev,
1147 TX_BUF_L, stm32port->tx_buf,
1148 stm32port->tx_dma_buf);
1149
1150alloc_err:
1151 dma_release_channel(stm32port->tx_ch);
1152 stm32port->tx_ch = NULL;
1153
1154 return ret;
1155}
1156
1157static int stm32_serial_probe(struct platform_device *pdev)
1158{
1159 const struct of_device_id *match;
1160 struct stm32_port *stm32port;
1161 int ret;
1162
1163 stm32port = stm32_of_get_stm32_port(pdev);
1164 if (!stm32port)
1165 return -ENODEV;
1166
1167 match = of_match_device(stm32_match, &pdev->dev);
1168 if (match && match->data)
1169 stm32port->info = (struct stm32_usart_info *)match->data;
1170 else
1171 return -EINVAL;
1172
1173 ret = stm32_init_port(stm32port, pdev);
1174 if (ret)
1175 return ret;
1176
1177 if (stm32port->wakeirq > 0) {
1178 ret = device_init_wakeup(&pdev->dev, true);
1179 if (ret)
1180 goto err_uninit;
1181
1182 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1183 stm32port->wakeirq);
1184 if (ret)
1185 goto err_nowup;
1186
1187 device_set_wakeup_enable(&pdev->dev, false);
1188 }
1189
1190 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1191 if (ret)
1192 goto err_wirq;
1193
1194 ret = stm32_of_dma_rx_probe(stm32port, pdev);
1195 if (ret)
1196 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
1197
1198 ret = stm32_of_dma_tx_probe(stm32port, pdev);
1199 if (ret)
1200 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
1201
1202 platform_set_drvdata(pdev, &stm32port->port);
1203
1204 pm_runtime_get_noresume(&pdev->dev);
1205 pm_runtime_set_active(&pdev->dev);
1206 pm_runtime_enable(&pdev->dev);
1207 pm_runtime_put_sync(&pdev->dev);
1208
1209 return 0;
1210
1211err_wirq:
1212 if (stm32port->wakeirq > 0)
1213 dev_pm_clear_wake_irq(&pdev->dev);
1214
1215err_nowup:
1216 if (stm32port->wakeirq > 0)
1217 device_init_wakeup(&pdev->dev, false);
1218
1219err_uninit:
1220 clk_disable_unprepare(stm32port->clk);
1221
1222 return ret;
1223}
1224
1225static int stm32_serial_remove(struct platform_device *pdev)
1226{
1227 struct uart_port *port = platform_get_drvdata(pdev);
1228 struct stm32_port *stm32_port = to_stm32_port(port);
1229 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1230 int err;
1231
1232 pm_runtime_get_sync(&pdev->dev);
1233
1234 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
1235
1236 if (stm32_port->rx_ch)
1237 dma_release_channel(stm32_port->rx_ch);
1238
1239 if (stm32_port->rx_dma_buf)
1240 dma_free_coherent(&pdev->dev,
1241 RX_BUF_L, stm32_port->rx_buf,
1242 stm32_port->rx_dma_buf);
1243
1244 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1245
1246 if (stm32_port->tx_ch)
1247 dma_release_channel(stm32_port->tx_ch);
1248
1249 if (stm32_port->tx_dma_buf)
1250 dma_free_coherent(&pdev->dev,
1251 TX_BUF_L, stm32_port->tx_buf,
1252 stm32_port->tx_dma_buf);
1253
1254 if (stm32_port->wakeirq > 0) {
1255 dev_pm_clear_wake_irq(&pdev->dev);
1256 device_init_wakeup(&pdev->dev, false);
1257 }
1258
1259 clk_disable_unprepare(stm32_port->clk);
1260
1261 err = uart_remove_one_port(&stm32_usart_driver, port);
1262
1263 pm_runtime_disable(&pdev->dev);
1264 pm_runtime_put_noidle(&pdev->dev);
1265
1266 return err;
1267}
1268
1269
1270#ifdef CONFIG_SERIAL_STM32_CONSOLE
1271static void stm32_console_putchar(struct uart_port *port, int ch)
1272{
1273 struct stm32_port *stm32_port = to_stm32_port(port);
1274 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1275
1276 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
1277 cpu_relax();
1278
1279 writel_relaxed(ch, port->membase + ofs->tdr);
1280}
1281
1282static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
1283{
1284 struct uart_port *port = &stm32_ports[co->index].port;
1285 struct stm32_port *stm32_port = to_stm32_port(port);
1286 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1287 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1288 unsigned long flags;
1289 u32 old_cr1, new_cr1;
1290 int locked = 1;
1291
1292 local_irq_save(flags);
1293 if (port->sysrq)
1294 locked = 0;
1295 else if (oops_in_progress)
1296 locked = spin_trylock(&port->lock);
1297 else
1298 spin_lock(&port->lock);
1299
1300 /* Save and disable interrupts, enable the transmitter */
1301 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1302 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1303 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
1304 writel_relaxed(new_cr1, port->membase + ofs->cr1);
1305
1306 uart_console_write(port, s, cnt, stm32_console_putchar);
1307
1308 /* Restore interrupt state */
1309 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1310
1311 if (locked)
1312 spin_unlock(&port->lock);
1313 local_irq_restore(flags);
1314}
1315
1316static int stm32_console_setup(struct console *co, char *options)
1317{
1318 struct stm32_port *stm32port;
1319 int baud = 9600;
1320 int bits = 8;
1321 int parity = 'n';
1322 int flow = 'n';
1323
1324 if (co->index >= STM32_MAX_PORTS)
1325 return -ENODEV;
1326
1327 stm32port = &stm32_ports[co->index];
1328
1329 /*
1330 * This driver does not support early console initialization
1331 * (use ARM early printk support instead), so we only expect
1332 * this to be called during the uart port registration when the
1333 * driver gets probed and the port should be mapped at that point.
1334 */
1335 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1336 return -ENXIO;
1337
1338 if (options)
1339 uart_parse_options(options, &baud, &parity, &bits, &flow);
1340
1341 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1342}
1343
1344static struct console stm32_console = {
1345 .name = STM32_SERIAL_NAME,
1346 .device = uart_console_device,
1347 .write = stm32_console_write,
1348 .setup = stm32_console_setup,
1349 .flags = CON_PRINTBUFFER,
1350 .index = -1,
1351 .data = &stm32_usart_driver,
1352};
1353
1354#define STM32_SERIAL_CONSOLE (&stm32_console)
1355
1356#else
1357#define STM32_SERIAL_CONSOLE NULL
1358#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1359
1360static struct uart_driver stm32_usart_driver = {
1361 .driver_name = DRIVER_NAME,
1362 .dev_name = STM32_SERIAL_NAME,
1363 .major = 0,
1364 .minor = 0,
1365 .nr = STM32_MAX_PORTS,
1366 .cons = STM32_SERIAL_CONSOLE,
1367};
1368
1369static void __maybe_unused stm32_serial_enable_wakeup(struct uart_port *port,
1370 bool enable)
1371{
1372 struct stm32_port *stm32_port = to_stm32_port(port);
1373 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1374 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1375 u32 val;
1376
1377 if (stm32_port->wakeirq <= 0)
1378 return;
1379
1380 if (enable) {
1381 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1382 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1383 val = readl_relaxed(port->membase + ofs->cr3);
1384 val &= ~USART_CR3_WUS_MASK;
1385 /* Enable Wake up interrupt from low power on start bit */
1386 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1387 writel_relaxed(val, port->membase + ofs->cr3);
1388 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1389 } else {
1390 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1391 }
1392}
1393
1394static int __maybe_unused stm32_serial_suspend(struct device *dev)
1395{
1396 struct uart_port *port = dev_get_drvdata(dev);
1397
1398 uart_suspend_port(&stm32_usart_driver, port);
1399
1400 if (device_may_wakeup(dev))
1401 stm32_serial_enable_wakeup(port, true);
1402 else
1403 stm32_serial_enable_wakeup(port, false);
1404
1405 pinctrl_pm_select_sleep_state(dev);
1406
1407 return 0;
1408}
1409
1410static int __maybe_unused stm32_serial_resume(struct device *dev)
1411{
1412 struct uart_port *port = dev_get_drvdata(dev);
1413
1414 pinctrl_pm_select_default_state(dev);
1415
1416 if (device_may_wakeup(dev))
1417 stm32_serial_enable_wakeup(port, false);
1418
1419 return uart_resume_port(&stm32_usart_driver, port);
1420}
1421
1422static int __maybe_unused stm32_serial_runtime_suspend(struct device *dev)
1423{
1424 struct uart_port *port = dev_get_drvdata(dev);
1425 struct stm32_port *stm32port = container_of(port,
1426 struct stm32_port, port);
1427
1428 clk_disable_unprepare(stm32port->clk);
1429
1430 return 0;
1431}
1432
1433static int __maybe_unused stm32_serial_runtime_resume(struct device *dev)
1434{
1435 struct uart_port *port = dev_get_drvdata(dev);
1436 struct stm32_port *stm32port = container_of(port,
1437 struct stm32_port, port);
1438
1439 return clk_prepare_enable(stm32port->clk);
1440}
1441
1442static const struct dev_pm_ops stm32_serial_pm_ops = {
1443 SET_RUNTIME_PM_OPS(stm32_serial_runtime_suspend,
1444 stm32_serial_runtime_resume, NULL)
1445 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1446};
1447
1448static struct platform_driver stm32_serial_driver = {
1449 .probe = stm32_serial_probe,
1450 .remove = stm32_serial_remove,
1451 .driver = {
1452 .name = DRIVER_NAME,
1453 .pm = &stm32_serial_pm_ops,
1454 .of_match_table = of_match_ptr(stm32_match),
1455 },
1456};
1457
1458static int __init usart_init(void)
1459{
1460 static char banner[] __initdata = "STM32 USART driver initialized";
1461 int ret;
1462
1463 pr_info("%s\n", banner);
1464
1465 ret = uart_register_driver(&stm32_usart_driver);
1466 if (ret)
1467 return ret;
1468
1469 ret = platform_driver_register(&stm32_serial_driver);
1470 if (ret)
1471 uart_unregister_driver(&stm32_usart_driver);
1472
1473 return ret;
1474}
1475
1476static void __exit usart_exit(void)
1477{
1478 platform_driver_unregister(&stm32_serial_driver);
1479 uart_unregister_driver(&stm32_usart_driver);
1480}
1481
1482module_init(usart_init);
1483module_exit(usart_exit);
1484
1485MODULE_ALIAS("platform:" DRIVER_NAME);
1486MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1487MODULE_LICENSE("GPL v2");