blob: 6afae051ba8d117cc5170c03b556db514d60a4a2 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#include <linux/clk.h>
12#include <linux/console.h>
13#include <linux/delay.h>
14#include <linux/dma-direction.h>
15#include <linux/dmaengine.h>
16#include <linux/dma-mapping.h>
17#include <linux/io.h>
18#include <linux/iopoll.h>
19#include <linux/irq.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_platform.h>
David Brazdil0f672f62019-12-10 10:32:29 +000023#include <linux/pinctrl/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/pm_wakeirq.h>
27#include <linux/serial_core.h>
28#include <linux/serial.h>
29#include <linux/spinlock.h>
30#include <linux/sysrq.h>
31#include <linux/tty_flip.h>
32#include <linux/tty.h>
33
Olivier Deprez157378f2022-04-04 15:47:50 +020034#include "serial_mctrl_gpio.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035#include "stm32-usart.h"
36
Olivier Deprez157378f2022-04-04 15:47:50 +020037static void stm32_usart_stop_tx(struct uart_port *port);
38static void stm32_usart_transmit_chars(struct uart_port *port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039
40static inline struct stm32_port *to_stm32_port(struct uart_port *port)
41{
42 return container_of(port, struct stm32_port, port);
43}
44
Olivier Deprez157378f2022-04-04 15:47:50 +020045static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046{
47 u32 val;
48
49 val = readl_relaxed(port->membase + reg);
50 val |= bits;
51 writel_relaxed(val, port->membase + reg);
52}
53
Olivier Deprez157378f2022-04-04 15:47:50 +020054static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055{
56 u32 val;
57
58 val = readl_relaxed(port->membase + reg);
59 val &= ~bits;
60 writel_relaxed(val, port->membase + reg);
61}
62
Olivier Deprez157378f2022-04-04 15:47:50 +020063static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
64 u32 delay_DDE, u32 baud)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065{
66 u32 rs485_deat_dedt;
67 u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
68 bool over8;
69
70 *cr3 |= USART_CR3_DEM;
71 over8 = *cr1 & USART_CR1_OVER8;
72
73 if (over8)
74 rs485_deat_dedt = delay_ADE * baud * 8;
75 else
76 rs485_deat_dedt = delay_ADE * baud * 16;
77
78 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
79 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
80 rs485_deat_dedt_max : rs485_deat_dedt;
81 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
82 USART_CR1_DEAT_MASK;
83 *cr1 |= rs485_deat_dedt;
84
85 if (over8)
86 rs485_deat_dedt = delay_DDE * baud * 8;
87 else
88 rs485_deat_dedt = delay_DDE * baud * 16;
89
90 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
91 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
92 rs485_deat_dedt_max : rs485_deat_dedt;
93 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
94 USART_CR1_DEDT_MASK;
95 *cr1 |= rs485_deat_dedt;
96}
97
Olivier Deprez157378f2022-04-04 15:47:50 +020098static int stm32_usart_config_rs485(struct uart_port *port,
99 struct serial_rs485 *rs485conf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100{
101 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200102 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
103 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 u32 usartdiv, baud, cr1, cr3;
105 bool over8;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106
Olivier Deprez157378f2022-04-04 15:47:50 +0200107 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108
109 port->rs485 = *rs485conf;
110
111 rs485conf->flags |= SER_RS485_RX_DURING_TX;
112
113 if (rs485conf->flags & SER_RS485_ENABLED) {
114 cr1 = readl_relaxed(port->membase + ofs->cr1);
115 cr3 = readl_relaxed(port->membase + ofs->cr3);
116 usartdiv = readl_relaxed(port->membase + ofs->brr);
117 usartdiv = usartdiv & GENMASK(15, 0);
118 over8 = cr1 & USART_CR1_OVER8;
119
120 if (over8)
121 usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
122 << USART_BRR_04_R_SHIFT;
123
124 baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
Olivier Deprez157378f2022-04-04 15:47:50 +0200125 stm32_usart_config_reg_rs485(&cr1, &cr3,
126 rs485conf->delay_rts_before_send,
127 rs485conf->delay_rts_after_send,
128 baud);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129
130 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
131 cr3 &= ~USART_CR3_DEP;
132 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
133 } else {
134 cr3 |= USART_CR3_DEP;
135 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
136 }
137
138 writel_relaxed(cr3, port->membase + ofs->cr3);
139 writel_relaxed(cr1, port->membase + ofs->cr1);
140 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +0200141 stm32_usart_clr_bits(port, ofs->cr3,
142 USART_CR3_DEM | USART_CR3_DEP);
143 stm32_usart_clr_bits(port, ofs->cr1,
144 USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145 }
146
Olivier Deprez157378f2022-04-04 15:47:50 +0200147 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148
149 return 0;
150}
151
Olivier Deprez157378f2022-04-04 15:47:50 +0200152static int stm32_usart_init_rs485(struct uart_port *port,
153 struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000154{
155 struct serial_rs485 *rs485conf = &port->rs485;
156
157 rs485conf->flags = 0;
158 rs485conf->delay_rts_before_send = 0;
159 rs485conf->delay_rts_after_send = 0;
160
161 if (!pdev->dev.of_node)
162 return -ENODEV;
163
Olivier Deprez157378f2022-04-04 15:47:50 +0200164 return uart_get_rs485_mode(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165}
166
Olivier Deprez157378f2022-04-04 15:47:50 +0200167static int stm32_usart_pending_rx(struct uart_port *port, u32 *sr,
168 int *last_res, bool threaded)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169{
170 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200171 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172 enum dma_status status;
173 struct dma_tx_state state;
174
175 *sr = readl_relaxed(port->membase + ofs->isr);
176
177 if (threaded && stm32_port->rx_ch) {
178 status = dmaengine_tx_status(stm32_port->rx_ch,
179 stm32_port->rx_ch->cookie,
180 &state);
Olivier Deprez157378f2022-04-04 15:47:50 +0200181 if (status == DMA_IN_PROGRESS && (*last_res != state.residue))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182 return 1;
183 else
184 return 0;
185 } else if (*sr & USART_SR_RXNE) {
186 return 1;
187 }
188 return 0;
189}
190
Olivier Deprez157378f2022-04-04 15:47:50 +0200191static unsigned long stm32_usart_get_char(struct uart_port *port, u32 *sr,
192 int *last_res)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193{
194 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200195 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000196 unsigned long c;
197
198 if (stm32_port->rx_ch) {
199 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
200 if ((*last_res) == 0)
201 *last_res = RX_BUF_L;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000203 c = readl_relaxed(port->membase + ofs->rdr);
204 /* apply RDR data mask */
205 c &= stm32_port->rdr_mask;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 }
David Brazdil0f672f62019-12-10 10:32:29 +0000207
208 return c;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000209}
210
Olivier Deprez157378f2022-04-04 15:47:50 +0200211static void stm32_usart_receive_chars(struct uart_port *port, bool threaded)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212{
213 struct tty_port *tport = &port->state->port;
214 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200215 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216 unsigned long c;
217 u32 sr;
218 char flag;
219
Olivier Deprez157378f2022-04-04 15:47:50 +0200220 spin_lock(&port->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221
Olivier Deprez157378f2022-04-04 15:47:50 +0200222 while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res,
223 threaded)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224 sr |= USART_SR_DUMMY_RX;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 flag = TTY_NORMAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226
David Brazdil0f672f62019-12-10 10:32:29 +0000227 /*
228 * Status bits has to be cleared before reading the RDR:
229 * In FIFO mode, reading the RDR will pop the next data
230 * (if any) along with its status bits into the SR.
231 * Not doing so leads to misalignement between RDR and SR,
232 * and clear status bits of the next rx data.
233 *
234 * Clear errors flags for stm32f7 and stm32h7 compatible
235 * devices. On stm32f4 compatible devices, the error bit is
236 * cleared by the sequence [read SR - read DR].
237 */
238 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
Olivier Deprez0e641232021-09-23 10:07:05 +0200239 writel_relaxed(sr & USART_SR_ERR_MASK,
240 port->membase + ofs->icr);
David Brazdil0f672f62019-12-10 10:32:29 +0000241
Olivier Deprez157378f2022-04-04 15:47:50 +0200242 c = stm32_usart_get_char(port, &sr, &stm32_port->last_res);
David Brazdil0f672f62019-12-10 10:32:29 +0000243 port->icount.rx++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244 if (sr & USART_SR_ERR_MASK) {
David Brazdil0f672f62019-12-10 10:32:29 +0000245 if (sr & USART_SR_ORE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246 port->icount.overrun++;
247 } else if (sr & USART_SR_PE) {
248 port->icount.parity++;
249 } else if (sr & USART_SR_FE) {
David Brazdil0f672f62019-12-10 10:32:29 +0000250 /* Break detection if character is null */
251 if (!c) {
252 port->icount.brk++;
253 if (uart_handle_break(port))
254 continue;
255 } else {
256 port->icount.frame++;
257 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000258 }
259
260 sr &= port->read_status_mask;
261
David Brazdil0f672f62019-12-10 10:32:29 +0000262 if (sr & USART_SR_PE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263 flag = TTY_PARITY;
David Brazdil0f672f62019-12-10 10:32:29 +0000264 } else if (sr & USART_SR_FE) {
265 if (!c)
266 flag = TTY_BREAK;
267 else
268 flag = TTY_FRAME;
269 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270 }
271
272 if (uart_handle_sysrq_char(port, c))
273 continue;
274 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
275 }
276
277 spin_unlock(&port->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200278
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 tty_flip_buffer_push(tport);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280}
281
Olivier Deprez157378f2022-04-04 15:47:50 +0200282static void stm32_usart_tx_dma_complete(void *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283{
284 struct uart_port *port = arg;
285 struct stm32_port *stm32port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200286 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
287 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288
Olivier Deprez157378f2022-04-04 15:47:50 +0200289 dmaengine_terminate_async(stm32port->tx_ch);
290 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291 stm32port->tx_dma_busy = false;
292
293 /* Let's see if we have pending data to send */
Olivier Deprez157378f2022-04-04 15:47:50 +0200294 spin_lock_irqsave(&port->lock, flags);
295 stm32_usart_transmit_chars(port);
296 spin_unlock_irqrestore(&port->lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297}
298
Olivier Deprez157378f2022-04-04 15:47:50 +0200299static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
David Brazdil0f672f62019-12-10 10:32:29 +0000300{
301 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200302 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
David Brazdil0f672f62019-12-10 10:32:29 +0000303
304 /*
305 * Enables TX FIFO threashold irq when FIFO is enabled,
306 * or TX empty irq when FIFO is disabled
307 */
308 if (stm32_port->fifoen)
Olivier Deprez157378f2022-04-04 15:47:50 +0200309 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
David Brazdil0f672f62019-12-10 10:32:29 +0000310 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200311 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
David Brazdil0f672f62019-12-10 10:32:29 +0000312}
313
Olivier Deprez157378f2022-04-04 15:47:50 +0200314static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
David Brazdil0f672f62019-12-10 10:32:29 +0000315{
316 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200317 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
David Brazdil0f672f62019-12-10 10:32:29 +0000318
319 if (stm32_port->fifoen)
Olivier Deprez157378f2022-04-04 15:47:50 +0200320 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
David Brazdil0f672f62019-12-10 10:32:29 +0000321 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200322 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
David Brazdil0f672f62019-12-10 10:32:29 +0000323}
324
Olivier Deprez157378f2022-04-04 15:47:50 +0200325static void stm32_usart_transmit_chars_pio(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326{
327 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200328 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329 struct circ_buf *xmit = &port->state->xmit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330
331 if (stm32_port->tx_dma_busy) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200332 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333 stm32_port->tx_dma_busy = false;
334 }
335
David Brazdil0f672f62019-12-10 10:32:29 +0000336 while (!uart_circ_empty(xmit)) {
337 /* Check that TDR is empty before filling FIFO */
338 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
339 break;
340 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
341 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
342 port->icount.tx++;
343 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344
David Brazdil0f672f62019-12-10 10:32:29 +0000345 /* rely on TXE irq (mask or unmask) for sending remaining data */
346 if (uart_circ_empty(xmit))
Olivier Deprez157378f2022-04-04 15:47:50 +0200347 stm32_usart_tx_interrupt_disable(port);
David Brazdil0f672f62019-12-10 10:32:29 +0000348 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200349 stm32_usart_tx_interrupt_enable(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350}
351
Olivier Deprez157378f2022-04-04 15:47:50 +0200352static void stm32_usart_transmit_chars_dma(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353{
354 struct stm32_port *stm32port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200355 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 struct circ_buf *xmit = &port->state->xmit;
357 struct dma_async_tx_descriptor *desc = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 unsigned int count, i;
359
360 if (stm32port->tx_dma_busy)
361 return;
362
363 stm32port->tx_dma_busy = true;
364
365 count = uart_circ_chars_pending(xmit);
366
367 if (count > TX_BUF_L)
368 count = TX_BUF_L;
369
370 if (xmit->tail < xmit->head) {
371 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
372 } else {
373 size_t one = UART_XMIT_SIZE - xmit->tail;
374 size_t two;
375
376 if (one > count)
377 one = count;
378 two = count - one;
379
380 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
381 if (two)
382 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
383 }
384
385 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
386 stm32port->tx_dma_buf,
387 count,
388 DMA_MEM_TO_DEV,
389 DMA_PREP_INTERRUPT);
390
Olivier Deprez157378f2022-04-04 15:47:50 +0200391 if (!desc)
392 goto fallback_err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393
Olivier Deprez157378f2022-04-04 15:47:50 +0200394 desc->callback = stm32_usart_tx_dma_complete;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395 desc->callback_param = port;
396
397 /* Push current DMA TX transaction in the pending queue */
Olivier Deprez157378f2022-04-04 15:47:50 +0200398 if (dma_submit_error(dmaengine_submit(desc))) {
399 /* dma no yet started, safe to free resources */
400 dmaengine_terminate_async(stm32port->tx_ch);
401 goto fallback_err;
402 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403
404 /* Issue pending DMA TX requests */
405 dma_async_issue_pending(stm32port->tx_ch);
406
Olivier Deprez157378f2022-04-04 15:47:50 +0200407 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408
409 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
410 port->icount.tx += count;
Olivier Deprez157378f2022-04-04 15:47:50 +0200411 return;
412
413fallback_err:
414 for (i = count; i > 0; i--)
415 stm32_usart_transmit_chars_pio(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416}
417
Olivier Deprez157378f2022-04-04 15:47:50 +0200418static void stm32_usart_transmit_chars(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419{
420 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200421 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422 struct circ_buf *xmit = &port->state->xmit;
Olivier Deprez157378f2022-04-04 15:47:50 +0200423 u32 isr;
424 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425
426 if (port->x_char) {
427 if (stm32_port->tx_dma_busy)
Olivier Deprez157378f2022-04-04 15:47:50 +0200428 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
429
430 /* Check that TDR is empty before filling FIFO */
431 ret =
432 readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
433 isr,
434 (isr & USART_SR_TXE),
435 10, 1000);
436 if (ret)
437 dev_warn(port->dev, "1 character may be erased\n");
438
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439 writel_relaxed(port->x_char, port->membase + ofs->tdr);
440 port->x_char = 0;
441 port->icount.tx++;
442 if (stm32_port->tx_dma_busy)
Olivier Deprez157378f2022-04-04 15:47:50 +0200443 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444 return;
445 }
446
David Brazdil0f672f62019-12-10 10:32:29 +0000447 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200448 stm32_usart_tx_interrupt_disable(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 return;
450 }
451
David Brazdil0f672f62019-12-10 10:32:29 +0000452 if (ofs->icr == UNDEF_REG)
Olivier Deprez157378f2022-04-04 15:47:50 +0200453 stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
David Brazdil0f672f62019-12-10 10:32:29 +0000454 else
Olivier Deprez0e641232021-09-23 10:07:05 +0200455 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000456
457 if (stm32_port->tx_ch)
Olivier Deprez157378f2022-04-04 15:47:50 +0200458 stm32_usart_transmit_chars_dma(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200460 stm32_usart_transmit_chars_pio(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461
462 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
463 uart_write_wakeup(port);
464
465 if (uart_circ_empty(xmit))
Olivier Deprez157378f2022-04-04 15:47:50 +0200466 stm32_usart_tx_interrupt_disable(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467}
468
Olivier Deprez157378f2022-04-04 15:47:50 +0200469static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000470{
471 struct uart_port *port = ptr;
Olivier Deprez157378f2022-04-04 15:47:50 +0200472 struct tty_port *tport = &port->state->port;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200474 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000475 u32 sr;
476
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477 sr = readl_relaxed(port->membase + ofs->isr);
478
David Brazdil0f672f62019-12-10 10:32:29 +0000479 if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
480 writel_relaxed(USART_ICR_RTOCF,
481 port->membase + ofs->icr);
482
Olivier Deprez157378f2022-04-04 15:47:50 +0200483 if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
484 /* Clear wake up flag and disable wake up interrupt */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485 writel_relaxed(USART_ICR_WUCF,
486 port->membase + ofs->icr);
Olivier Deprez157378f2022-04-04 15:47:50 +0200487 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
488 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
489 pm_wakeup_event(tport->tty->dev, 0);
490 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491
492 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
Olivier Deprez157378f2022-04-04 15:47:50 +0200493 stm32_usart_receive_chars(port, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494
Olivier Deprez157378f2022-04-04 15:47:50 +0200495 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
496 spin_lock(&port->lock);
497 stm32_usart_transmit_chars(port);
498 spin_unlock(&port->lock);
499 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
501 if (stm32_port->rx_ch)
502 return IRQ_WAKE_THREAD;
503 else
504 return IRQ_HANDLED;
505}
506
Olivier Deprez157378f2022-04-04 15:47:50 +0200507static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000508{
509 struct uart_port *port = ptr;
510 struct stm32_port *stm32_port = to_stm32_port(port);
511
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512 if (stm32_port->rx_ch)
Olivier Deprez157378f2022-04-04 15:47:50 +0200513 stm32_usart_receive_chars(port, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514
515 return IRQ_HANDLED;
516}
517
Olivier Deprez157378f2022-04-04 15:47:50 +0200518static unsigned int stm32_usart_tx_empty(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000519{
520 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200521 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522
Olivier Deprez0e641232021-09-23 10:07:05 +0200523 if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
524 return TIOCSER_TEMT;
525
526 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000527}
528
Olivier Deprez157378f2022-04-04 15:47:50 +0200529static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000530{
531 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200532 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533
534 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Olivier Deprez157378f2022-04-04 15:47:50 +0200535 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000536 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200537 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
538
539 mctrl_gpio_set(stm32_port->gpios, mctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000540}
541
Olivier Deprez157378f2022-04-04 15:47:50 +0200542static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000543{
Olivier Deprez157378f2022-04-04 15:47:50 +0200544 struct stm32_port *stm32_port = to_stm32_port(port);
545 unsigned int ret;
546
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
Olivier Deprez157378f2022-04-04 15:47:50 +0200548 ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
549
550 return mctrl_gpio_get(stm32_port->gpios, &ret);
551}
552
553static void stm32_usart_enable_ms(struct uart_port *port)
554{
555 mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
556}
557
558static void stm32_usart_disable_ms(struct uart_port *port)
559{
560 mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000561}
562
563/* Transmit stop */
Olivier Deprez157378f2022-04-04 15:47:50 +0200564static void stm32_usart_stop_tx(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565{
Olivier Deprez157378f2022-04-04 15:47:50 +0200566 struct stm32_port *stm32_port = to_stm32_port(port);
567 struct serial_rs485 *rs485conf = &port->rs485;
568
569 stm32_usart_tx_interrupt_disable(port);
570
571 if (rs485conf->flags & SER_RS485_ENABLED) {
572 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
573 mctrl_gpio_set(stm32_port->gpios,
574 stm32_port->port.mctrl & ~TIOCM_RTS);
575 } else {
576 mctrl_gpio_set(stm32_port->gpios,
577 stm32_port->port.mctrl | TIOCM_RTS);
578 }
579 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580}
581
582/* There are probably characters waiting to be transmitted. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200583static void stm32_usart_start_tx(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584{
Olivier Deprez157378f2022-04-04 15:47:50 +0200585 struct stm32_port *stm32_port = to_stm32_port(port);
586 struct serial_rs485 *rs485conf = &port->rs485;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587 struct circ_buf *xmit = &port->state->xmit;
588
Olivier Deprez157378f2022-04-04 15:47:50 +0200589 if (uart_circ_empty(xmit) && !port->x_char)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590 return;
591
Olivier Deprez157378f2022-04-04 15:47:50 +0200592 if (rs485conf->flags & SER_RS485_ENABLED) {
593 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
594 mctrl_gpio_set(stm32_port->gpios,
595 stm32_port->port.mctrl | TIOCM_RTS);
596 } else {
597 mctrl_gpio_set(stm32_port->gpios,
598 stm32_port->port.mctrl & ~TIOCM_RTS);
599 }
600 }
601
602 stm32_usart_transmit_chars(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603}
604
605/* Throttle the remote when input buffer is about to overflow. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200606static void stm32_usart_throttle(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000607{
608 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200609 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610 unsigned long flags;
611
612 spin_lock_irqsave(&port->lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +0200613 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
David Brazdil0f672f62019-12-10 10:32:29 +0000614 if (stm32_port->cr3_irq)
Olivier Deprez157378f2022-04-04 15:47:50 +0200615 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
David Brazdil0f672f62019-12-10 10:32:29 +0000616
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000617 spin_unlock_irqrestore(&port->lock, flags);
618}
619
620/* Unthrottle the remote, the input buffer can now accept data. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200621static void stm32_usart_unthrottle(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622{
623 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200624 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000625 unsigned long flags;
626
627 spin_lock_irqsave(&port->lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +0200628 stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
David Brazdil0f672f62019-12-10 10:32:29 +0000629 if (stm32_port->cr3_irq)
Olivier Deprez157378f2022-04-04 15:47:50 +0200630 stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
David Brazdil0f672f62019-12-10 10:32:29 +0000631
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000632 spin_unlock_irqrestore(&port->lock, flags);
633}
634
635/* Receive stop */
Olivier Deprez157378f2022-04-04 15:47:50 +0200636static void stm32_usart_stop_rx(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000637{
638 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200639 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000640
Olivier Deprez157378f2022-04-04 15:47:50 +0200641 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
David Brazdil0f672f62019-12-10 10:32:29 +0000642 if (stm32_port->cr3_irq)
Olivier Deprez157378f2022-04-04 15:47:50 +0200643 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644}
645
646/* Handle breaks - ignored by us */
Olivier Deprez157378f2022-04-04 15:47:50 +0200647static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000648{
649}
650
Olivier Deprez157378f2022-04-04 15:47:50 +0200651static int stm32_usart_startup(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000652{
653 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200654 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
655 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656 const char *name = to_platform_device(port->dev)->name;
657 u32 val;
658 int ret;
659
Olivier Deprez157378f2022-04-04 15:47:50 +0200660 ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
661 stm32_usart_threaded_interrupt,
662 IRQF_ONESHOT | IRQF_NO_SUSPEND,
663 name, port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000664 if (ret)
665 return ret;
666
David Brazdil0f672f62019-12-10 10:32:29 +0000667 /* RX FIFO Flush */
668 if (ofs->rqr != UNDEF_REG)
Olivier Deprez157378f2022-04-04 15:47:50 +0200669 writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
David Brazdil0f672f62019-12-10 10:32:29 +0000670
Olivier Deprez157378f2022-04-04 15:47:50 +0200671 /* RX enabling */
672 val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
673 stm32_usart_set_bits(port, ofs->cr1, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000674
675 return 0;
676}
677
Olivier Deprez157378f2022-04-04 15:47:50 +0200678static void stm32_usart_shutdown(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679{
680 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200681 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
682 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
David Brazdil0f672f62019-12-10 10:32:29 +0000683 u32 val, isr;
684 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685
Olivier Deprez157378f2022-04-04 15:47:50 +0200686 /* Disable modem control interrupts */
687 stm32_usart_disable_ms(port);
688
David Brazdil0f672f62019-12-10 10:32:29 +0000689 val = USART_CR1_TXEIE | USART_CR1_TE;
690 val |= stm32_port->cr1_irq | USART_CR1_RE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000691 val |= BIT(cfg->uart_enable_bit);
692 if (stm32_port->fifoen)
693 val |= USART_CR1_FIFOEN;
David Brazdil0f672f62019-12-10 10:32:29 +0000694
695 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
696 isr, (isr & USART_SR_TC),
697 10, 100000);
698
699 if (ret)
700 dev_err(port->dev, "transmission complete not set\n");
701
Olivier Deprez157378f2022-04-04 15:47:50 +0200702 /* flush RX & TX FIFO */
703 if (ofs->rqr != UNDEF_REG)
704 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
705 port->membase + ofs->rqr);
706
707 stm32_usart_clr_bits(port, ofs->cr1, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709 free_irq(port->irq, port);
710}
711
Olivier Deprez157378f2022-04-04 15:47:50 +0200712static unsigned int stm32_usart_get_databits(struct ktermios *termios)
David Brazdil0f672f62019-12-10 10:32:29 +0000713{
714 unsigned int bits;
715
716 tcflag_t cflag = termios->c_cflag;
717
718 switch (cflag & CSIZE) {
719 /*
720 * CSIZE settings are not necessarily supported in hardware.
721 * CSIZE unsupported configurations are handled here to set word length
722 * to 8 bits word as default configuration and to print debug message.
723 */
724 case CS5:
725 bits = 5;
726 break;
727 case CS6:
728 bits = 6;
729 break;
730 case CS7:
731 bits = 7;
732 break;
733 /* default including CS8 */
734 default:
735 bits = 8;
736 break;
737 }
738
739 return bits;
740}
741
Olivier Deprez157378f2022-04-04 15:47:50 +0200742static void stm32_usart_set_termios(struct uart_port *port,
743 struct ktermios *termios,
744 struct ktermios *old)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745{
746 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200747 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
748 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749 struct serial_rs485 *rs485conf = &port->rs485;
David Brazdil0f672f62019-12-10 10:32:29 +0000750 unsigned int baud, bits;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000751 u32 usartdiv, mantissa, fraction, oversampling;
752 tcflag_t cflag = termios->c_cflag;
Olivier Deprez0e641232021-09-23 10:07:05 +0200753 u32 cr1, cr2, cr3, isr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000754 unsigned long flags;
Olivier Deprez0e641232021-09-23 10:07:05 +0200755 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000756
757 if (!stm32_port->hw_flow_control)
758 cflag &= ~CRTSCTS;
759
760 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
761
762 spin_lock_irqsave(&port->lock, flags);
763
Olivier Deprez0e641232021-09-23 10:07:05 +0200764 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
765 isr,
766 (isr & USART_SR_TC),
767 10, 100000);
768
769 /* Send the TC error message only when ISR_TC is not set. */
770 if (ret)
771 dev_err(port->dev, "Transmission is not complete\n");
772
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773 /* Stop serial port and reset value */
774 writel_relaxed(0, port->membase + ofs->cr1);
775
David Brazdil0f672f62019-12-10 10:32:29 +0000776 /* flush RX & TX FIFO */
777 if (ofs->rqr != UNDEF_REG)
Olivier Deprez157378f2022-04-04 15:47:50 +0200778 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
779 port->membase + ofs->rqr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000780
David Brazdil0f672f62019-12-10 10:32:29 +0000781 cr1 = USART_CR1_TE | USART_CR1_RE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782 if (stm32_port->fifoen)
783 cr1 |= USART_CR1_FIFOEN;
784 cr2 = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200785
786 /* Tx and RX FIFO configuration */
David Brazdil0f672f62019-12-10 10:32:29 +0000787 cr3 = readl_relaxed(port->membase + ofs->cr3);
Olivier Deprez157378f2022-04-04 15:47:50 +0200788 cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
789 if (stm32_port->fifoen) {
790 cr3 &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK);
791 cr3 |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT;
792 cr3 |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT;
793 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794
795 if (cflag & CSTOPB)
796 cr2 |= USART_CR2_STOP_2B;
797
Olivier Deprez157378f2022-04-04 15:47:50 +0200798 bits = stm32_usart_get_databits(termios);
David Brazdil0f672f62019-12-10 10:32:29 +0000799 stm32_port->rdr_mask = (BIT(bits) - 1);
800
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801 if (cflag & PARENB) {
David Brazdil0f672f62019-12-10 10:32:29 +0000802 bits++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000803 cr1 |= USART_CR1_PCE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804 }
805
David Brazdil0f672f62019-12-10 10:32:29 +0000806 /*
807 * Word length configuration:
808 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
809 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
810 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
811 * M0 and M1 already cleared by cr1 initialization.
812 */
813 if (bits == 9)
814 cr1 |= USART_CR1_M0;
815 else if ((bits == 7) && cfg->has_7bits_data)
816 cr1 |= USART_CR1_M1;
817 else if (bits != 8)
818 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
819 , bits);
820
821 if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
822 stm32_port->fifoen)) {
823 if (cflag & CSTOPB)
824 bits = bits + 3; /* 1 start bit + 2 stop bits */
825 else
826 bits = bits + 2; /* 1 start bit + 1 stop bit */
827
828 /* RX timeout irq to occur after last stop bit + bits */
829 stm32_port->cr1_irq = USART_CR1_RTOIE;
830 writel_relaxed(bits, port->membase + ofs->rtor);
831 cr2 |= USART_CR2_RTOEN;
832 /* Not using dma, enable fifo threshold irq */
833 if (!stm32_port->rx_ch)
834 stm32_port->cr3_irq = USART_CR3_RXFTIE;
835 }
836
837 cr1 |= stm32_port->cr1_irq;
838 cr3 |= stm32_port->cr3_irq;
839
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000840 if (cflag & PARODD)
841 cr1 |= USART_CR1_PS;
842
843 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
844 if (cflag & CRTSCTS) {
845 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
846 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
847 }
848
849 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
850
851 /*
852 * The USART supports 16 or 8 times oversampling.
853 * By default we prefer 16 times oversampling, so that the receiver
854 * has a better tolerance to clock deviations.
855 * 8 times oversampling is only used to achieve higher speeds.
856 */
857 if (usartdiv < 16) {
858 oversampling = 8;
859 cr1 |= USART_CR1_OVER8;
Olivier Deprez157378f2022-04-04 15:47:50 +0200860 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861 } else {
862 oversampling = 16;
863 cr1 &= ~USART_CR1_OVER8;
Olivier Deprez157378f2022-04-04 15:47:50 +0200864 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000865 }
866
867 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
868 fraction = usartdiv % oversampling;
869 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
870
871 uart_update_timeout(port, cflag, baud);
872
873 port->read_status_mask = USART_SR_ORE;
874 if (termios->c_iflag & INPCK)
875 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
876 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
David Brazdil0f672f62019-12-10 10:32:29 +0000877 port->read_status_mask |= USART_SR_FE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000878
879 /* Characters to ignore */
880 port->ignore_status_mask = 0;
881 if (termios->c_iflag & IGNPAR)
882 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
883 if (termios->c_iflag & IGNBRK) {
David Brazdil0f672f62019-12-10 10:32:29 +0000884 port->ignore_status_mask |= USART_SR_FE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885 /*
886 * If we're ignoring parity and break indicators,
887 * ignore overruns too (for real raw support).
888 */
889 if (termios->c_iflag & IGNPAR)
890 port->ignore_status_mask |= USART_SR_ORE;
891 }
892
893 /* Ignore all characters if CREAD is not set */
894 if ((termios->c_cflag & CREAD) == 0)
895 port->ignore_status_mask |= USART_SR_DUMMY_RX;
896
897 if (stm32_port->rx_ch)
898 cr3 |= USART_CR3_DMAR;
899
900 if (rs485conf->flags & SER_RS485_ENABLED) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200901 stm32_usart_config_reg_rs485(&cr1, &cr3,
902 rs485conf->delay_rts_before_send,
903 rs485conf->delay_rts_after_send,
904 baud);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000905 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
906 cr3 &= ~USART_CR3_DEP;
907 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
908 } else {
909 cr3 |= USART_CR3_DEP;
910 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
911 }
912
913 } else {
914 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
915 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
916 }
917
Olivier Deprez157378f2022-04-04 15:47:50 +0200918 /* Configure wake up from low power on start bit detection */
919 if (stm32_port->wakeirq > 0) {
920 cr3 &= ~USART_CR3_WUS_MASK;
921 cr3 |= USART_CR3_WUS_START_BIT;
922 }
923
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000924 writel_relaxed(cr3, port->membase + ofs->cr3);
925 writel_relaxed(cr2, port->membase + ofs->cr2);
926 writel_relaxed(cr1, port->membase + ofs->cr1);
927
Olivier Deprez157378f2022-04-04 15:47:50 +0200928 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000929 spin_unlock_irqrestore(&port->lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +0200930
931 /* Handle modem control interrupts */
932 if (UART_ENABLE_MS(port, termios->c_cflag))
933 stm32_usart_enable_ms(port);
934 else
935 stm32_usart_disable_ms(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936}
937
Olivier Deprez157378f2022-04-04 15:47:50 +0200938static const char *stm32_usart_type(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939{
940 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
941}
942
Olivier Deprez157378f2022-04-04 15:47:50 +0200943static void stm32_usart_release_port(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944{
945}
946
Olivier Deprez157378f2022-04-04 15:47:50 +0200947static int stm32_usart_request_port(struct uart_port *port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948{
949 return 0;
950}
951
Olivier Deprez157378f2022-04-04 15:47:50 +0200952static void stm32_usart_config_port(struct uart_port *port, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000953{
954 if (flags & UART_CONFIG_TYPE)
955 port->type = PORT_STM32;
956}
957
958static int
Olivier Deprez157378f2022-04-04 15:47:50 +0200959stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960{
961 /* No user changeable parameters */
962 return -EINVAL;
963}
964
Olivier Deprez157378f2022-04-04 15:47:50 +0200965static void stm32_usart_pm(struct uart_port *port, unsigned int state,
966 unsigned int oldstate)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000967{
968 struct stm32_port *stm32port = container_of(port,
969 struct stm32_port, port);
Olivier Deprez157378f2022-04-04 15:47:50 +0200970 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
971 const struct stm32_usart_config *cfg = &stm32port->info->cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972 unsigned long flags = 0;
973
974 switch (state) {
975 case UART_PM_STATE_ON:
David Brazdil0f672f62019-12-10 10:32:29 +0000976 pm_runtime_get_sync(port->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000977 break;
978 case UART_PM_STATE_OFF:
979 spin_lock_irqsave(&port->lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +0200980 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000981 spin_unlock_irqrestore(&port->lock, flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000982 pm_runtime_put_sync(port->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000983 break;
984 }
985}
986
987static const struct uart_ops stm32_uart_ops = {
Olivier Deprez157378f2022-04-04 15:47:50 +0200988 .tx_empty = stm32_usart_tx_empty,
989 .set_mctrl = stm32_usart_set_mctrl,
990 .get_mctrl = stm32_usart_get_mctrl,
991 .stop_tx = stm32_usart_stop_tx,
992 .start_tx = stm32_usart_start_tx,
993 .throttle = stm32_usart_throttle,
994 .unthrottle = stm32_usart_unthrottle,
995 .stop_rx = stm32_usart_stop_rx,
996 .enable_ms = stm32_usart_enable_ms,
997 .break_ctl = stm32_usart_break_ctl,
998 .startup = stm32_usart_startup,
999 .shutdown = stm32_usart_shutdown,
1000 .set_termios = stm32_usart_set_termios,
1001 .pm = stm32_usart_pm,
1002 .type = stm32_usart_type,
1003 .release_port = stm32_usart_release_port,
1004 .request_port = stm32_usart_request_port,
1005 .config_port = stm32_usart_config_port,
1006 .verify_port = stm32_usart_verify_port,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007};
1008
Olivier Deprez157378f2022-04-04 15:47:50 +02001009static int stm32_usart_init_port(struct stm32_port *stm32port,
1010 struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011{
1012 struct uart_port *port = &stm32port->port;
1013 struct resource *res;
1014 int ret;
1015
Olivier Deprez157378f2022-04-04 15:47:50 +02001016 ret = platform_get_irq(pdev, 0);
1017 if (ret <= 0)
1018 return ret ? : -ENODEV;
1019
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001020 port->iotype = UPIO_MEM;
1021 port->flags = UPF_BOOT_AUTOCONF;
1022 port->ops = &stm32_uart_ops;
1023 port->dev = &pdev->dev;
David Brazdil0f672f62019-12-10 10:32:29 +00001024 port->fifosize = stm32port->info->cfg.fifosize;
Olivier Deprez157378f2022-04-04 15:47:50 +02001025 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
David Brazdil0f672f62019-12-10 10:32:29 +00001026 port->irq = ret;
Olivier Deprez157378f2022-04-04 15:47:50 +02001027 port->rs485_config = stm32_usart_config_rs485;
David Brazdil0f672f62019-12-10 10:32:29 +00001028
Olivier Deprez157378f2022-04-04 15:47:50 +02001029 ret = stm32_usart_init_rs485(port, pdev);
1030 if (ret)
1031 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032
David Brazdil0f672f62019-12-10 10:32:29 +00001033 if (stm32port->info->cfg.has_wakeup) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001034 stm32port->wakeirq = platform_get_irq_optional(pdev, 1);
David Brazdil0f672f62019-12-10 10:32:29 +00001035 if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO)
1036 return stm32port->wakeirq ? : -ENODEV;
1037 }
1038
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001039 stm32port->fifoen = stm32port->info->cfg.has_fifo;
1040
1041 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1042 port->membase = devm_ioremap_resource(&pdev->dev, res);
1043 if (IS_ERR(port->membase))
1044 return PTR_ERR(port->membase);
1045 port->mapbase = res->start;
1046
1047 spin_lock_init(&port->lock);
1048
1049 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
1050 if (IS_ERR(stm32port->clk))
1051 return PTR_ERR(stm32port->clk);
1052
1053 /* Ensure that clk rate is correct by enabling the clk */
1054 ret = clk_prepare_enable(stm32port->clk);
1055 if (ret)
1056 return ret;
1057
1058 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1059 if (!stm32port->port.uartclk) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001060 ret = -EINVAL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001061 goto err_clk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001062 }
1063
Olivier Deprez157378f2022-04-04 15:47:50 +02001064 stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
1065 if (IS_ERR(stm32port->gpios)) {
1066 ret = PTR_ERR(stm32port->gpios);
1067 goto err_clk;
1068 }
1069
1070 /* Both CTS/RTS gpios and "st,hw-flow-ctrl" should not be specified */
1071 if (stm32port->hw_flow_control) {
1072 if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
1073 mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
1074 dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
1075 ret = -EINVAL;
1076 goto err_clk;
1077 }
1078 }
1079
1080 return ret;
1081
1082err_clk:
1083 clk_disable_unprepare(stm32port->clk);
1084
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001085 return ret;
1086}
1087
Olivier Deprez157378f2022-04-04 15:47:50 +02001088static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089{
1090 struct device_node *np = pdev->dev.of_node;
1091 int id;
1092
1093 if (!np)
1094 return NULL;
1095
1096 id = of_alias_get_id(np, "serial");
1097 if (id < 0) {
1098 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1099 return NULL;
1100 }
1101
1102 if (WARN_ON(id >= STM32_MAX_PORTS))
1103 return NULL;
1104
Olivier Deprez157378f2022-04-04 15:47:50 +02001105 stm32_ports[id].hw_flow_control =
1106 of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
1107 of_property_read_bool (np, "uart-has-rtscts");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108 stm32_ports[id].port.line = id;
David Brazdil0f672f62019-12-10 10:32:29 +00001109 stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1110 stm32_ports[id].cr3_irq = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001111 stm32_ports[id].last_res = RX_BUF_L;
1112 return &stm32_ports[id];
1113}
1114
1115#ifdef CONFIG_OF
1116static const struct of_device_id stm32_match[] = {
1117 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
1118 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1119 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
1120 {},
1121};
1122
1123MODULE_DEVICE_TABLE(of, stm32_match);
1124#endif
1125
Olivier Deprez157378f2022-04-04 15:47:50 +02001126static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
1127 struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001128{
Olivier Deprez157378f2022-04-04 15:47:50 +02001129 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001130 struct uart_port *port = &stm32port->port;
1131 struct device *dev = &pdev->dev;
1132 struct dma_slave_config config;
1133 struct dma_async_tx_descriptor *desc = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001134 int ret;
1135
Olivier Deprez157378f2022-04-04 15:47:50 +02001136 /*
1137 * Using DMA and threaded handler for the console could lead to
1138 * deadlocks.
1139 */
1140 if (uart_console(port))
1141 return -ENODEV;
1142
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143 /* Request DMA RX channel */
1144 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
1145 if (!stm32port->rx_ch) {
1146 dev_info(dev, "rx dma alloc failed\n");
1147 return -ENODEV;
1148 }
1149 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
Olivier Deprez157378f2022-04-04 15:47:50 +02001150 &stm32port->rx_dma_buf,
1151 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001152 if (!stm32port->rx_buf) {
1153 ret = -ENOMEM;
1154 goto alloc_err;
1155 }
1156
1157 /* Configure DMA channel */
1158 memset(&config, 0, sizeof(config));
1159 config.src_addr = port->mapbase + ofs->rdr;
1160 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1161
1162 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
1163 if (ret < 0) {
1164 dev_err(dev, "rx dma channel config failed\n");
1165 ret = -ENODEV;
1166 goto config_err;
1167 }
1168
1169 /* Prepare a DMA cyclic transaction */
1170 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
1171 stm32port->rx_dma_buf,
1172 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
1173 DMA_PREP_INTERRUPT);
1174 if (!desc) {
1175 dev_err(dev, "rx dma prep cyclic failed\n");
1176 ret = -ENODEV;
1177 goto config_err;
1178 }
1179
1180 /* No callback as dma buffer is drained on usart interrupt */
1181 desc->callback = NULL;
1182 desc->callback_param = NULL;
1183
1184 /* Push current DMA transaction in the pending queue */
Olivier Deprez157378f2022-04-04 15:47:50 +02001185 ret = dma_submit_error(dmaengine_submit(desc));
1186 if (ret) {
1187 dmaengine_terminate_sync(stm32port->rx_ch);
1188 goto config_err;
1189 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001190
1191 /* Issue pending DMA requests */
1192 dma_async_issue_pending(stm32port->rx_ch);
1193
1194 return 0;
1195
1196config_err:
1197 dma_free_coherent(&pdev->dev,
1198 RX_BUF_L, stm32port->rx_buf,
1199 stm32port->rx_dma_buf);
1200
1201alloc_err:
1202 dma_release_channel(stm32port->rx_ch);
1203 stm32port->rx_ch = NULL;
1204
1205 return ret;
1206}
1207
Olivier Deprez157378f2022-04-04 15:47:50 +02001208static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
1209 struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001210{
Olivier Deprez157378f2022-04-04 15:47:50 +02001211 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001212 struct uart_port *port = &stm32port->port;
1213 struct device *dev = &pdev->dev;
1214 struct dma_slave_config config;
1215 int ret;
1216
1217 stm32port->tx_dma_busy = false;
1218
1219 /* Request DMA TX channel */
1220 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
1221 if (!stm32port->tx_ch) {
1222 dev_info(dev, "tx dma alloc failed\n");
1223 return -ENODEV;
1224 }
1225 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
Olivier Deprez157378f2022-04-04 15:47:50 +02001226 &stm32port->tx_dma_buf,
1227 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001228 if (!stm32port->tx_buf) {
1229 ret = -ENOMEM;
1230 goto alloc_err;
1231 }
1232
1233 /* Configure DMA channel */
1234 memset(&config, 0, sizeof(config));
1235 config.dst_addr = port->mapbase + ofs->tdr;
1236 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1237
1238 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1239 if (ret < 0) {
1240 dev_err(dev, "tx dma channel config failed\n");
1241 ret = -ENODEV;
1242 goto config_err;
1243 }
1244
1245 return 0;
1246
1247config_err:
1248 dma_free_coherent(&pdev->dev,
1249 TX_BUF_L, stm32port->tx_buf,
1250 stm32port->tx_dma_buf);
1251
1252alloc_err:
1253 dma_release_channel(stm32port->tx_ch);
1254 stm32port->tx_ch = NULL;
1255
1256 return ret;
1257}
1258
Olivier Deprez157378f2022-04-04 15:47:50 +02001259static int stm32_usart_serial_probe(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001260{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001261 struct stm32_port *stm32port;
1262 int ret;
1263
Olivier Deprez157378f2022-04-04 15:47:50 +02001264 stm32port = stm32_usart_of_get_port(pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265 if (!stm32port)
1266 return -ENODEV;
1267
Olivier Deprez157378f2022-04-04 15:47:50 +02001268 stm32port->info = of_device_get_match_data(&pdev->dev);
1269 if (!stm32port->info)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001270 return -EINVAL;
1271
Olivier Deprez157378f2022-04-04 15:47:50 +02001272 ret = stm32_usart_init_port(stm32port, pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001273 if (ret)
1274 return ret;
1275
David Brazdil0f672f62019-12-10 10:32:29 +00001276 if (stm32port->wakeirq > 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001277 ret = device_init_wakeup(&pdev->dev, true);
1278 if (ret)
1279 goto err_uninit;
David Brazdil0f672f62019-12-10 10:32:29 +00001280
1281 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1282 stm32port->wakeirq);
1283 if (ret)
1284 goto err_nowup;
1285
1286 device_set_wakeup_enable(&pdev->dev, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001287 }
1288
Olivier Deprez157378f2022-04-04 15:47:50 +02001289 ret = stm32_usart_of_dma_rx_probe(stm32port, pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001290 if (ret)
1291 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
1292
Olivier Deprez157378f2022-04-04 15:47:50 +02001293 ret = stm32_usart_of_dma_tx_probe(stm32port, pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001294 if (ret)
1295 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
1296
1297 platform_set_drvdata(pdev, &stm32port->port);
1298
David Brazdil0f672f62019-12-10 10:32:29 +00001299 pm_runtime_get_noresume(&pdev->dev);
1300 pm_runtime_set_active(&pdev->dev);
1301 pm_runtime_enable(&pdev->dev);
Olivier Deprez157378f2022-04-04 15:47:50 +02001302
1303 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1304 if (ret)
1305 goto err_port;
1306
David Brazdil0f672f62019-12-10 10:32:29 +00001307 pm_runtime_put_sync(&pdev->dev);
1308
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001309 return 0;
1310
Olivier Deprez157378f2022-04-04 15:47:50 +02001311err_port:
1312 pm_runtime_disable(&pdev->dev);
1313 pm_runtime_set_suspended(&pdev->dev);
1314 pm_runtime_put_noidle(&pdev->dev);
1315
1316 if (stm32port->rx_ch) {
1317 dmaengine_terminate_async(stm32port->rx_ch);
1318 dma_release_channel(stm32port->rx_ch);
1319 }
1320
1321 if (stm32port->rx_dma_buf)
1322 dma_free_coherent(&pdev->dev,
1323 RX_BUF_L, stm32port->rx_buf,
1324 stm32port->rx_dma_buf);
1325
1326 if (stm32port->tx_ch) {
1327 dmaengine_terminate_async(stm32port->tx_ch);
1328 dma_release_channel(stm32port->tx_ch);
1329 }
1330
1331 if (stm32port->tx_dma_buf)
1332 dma_free_coherent(&pdev->dev,
1333 TX_BUF_L, stm32port->tx_buf,
1334 stm32port->tx_dma_buf);
1335
David Brazdil0f672f62019-12-10 10:32:29 +00001336 if (stm32port->wakeirq > 0)
1337 dev_pm_clear_wake_irq(&pdev->dev);
1338
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001339err_nowup:
David Brazdil0f672f62019-12-10 10:32:29 +00001340 if (stm32port->wakeirq > 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001341 device_init_wakeup(&pdev->dev, false);
1342
1343err_uninit:
1344 clk_disable_unprepare(stm32port->clk);
1345
1346 return ret;
1347}
1348
Olivier Deprez157378f2022-04-04 15:47:50 +02001349static int stm32_usart_serial_remove(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001350{
1351 struct uart_port *port = platform_get_drvdata(pdev);
1352 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +02001353 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
David Brazdil0f672f62019-12-10 10:32:29 +00001354 int err;
1355
1356 pm_runtime_get_sync(&pdev->dev);
Olivier Deprez157378f2022-04-04 15:47:50 +02001357 err = uart_remove_one_port(&stm32_usart_driver, port);
1358 if (err)
1359 return(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001360
Olivier Deprez157378f2022-04-04 15:47:50 +02001361 pm_runtime_disable(&pdev->dev);
1362 pm_runtime_set_suspended(&pdev->dev);
1363 pm_runtime_put_noidle(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001364
Olivier Deprez157378f2022-04-04 15:47:50 +02001365 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
1366
1367 if (stm32_port->rx_ch) {
1368 dmaengine_terminate_async(stm32_port->rx_ch);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001369 dma_release_channel(stm32_port->rx_ch);
Olivier Deprez157378f2022-04-04 15:47:50 +02001370 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001371
1372 if (stm32_port->rx_dma_buf)
1373 dma_free_coherent(&pdev->dev,
1374 RX_BUF_L, stm32_port->rx_buf,
1375 stm32_port->rx_dma_buf);
1376
Olivier Deprez157378f2022-04-04 15:47:50 +02001377 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378
Olivier Deprez157378f2022-04-04 15:47:50 +02001379 if (stm32_port->tx_ch) {
1380 dmaengine_terminate_async(stm32_port->tx_ch);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001381 dma_release_channel(stm32_port->tx_ch);
Olivier Deprez157378f2022-04-04 15:47:50 +02001382 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001383
1384 if (stm32_port->tx_dma_buf)
1385 dma_free_coherent(&pdev->dev,
1386 TX_BUF_L, stm32_port->tx_buf,
1387 stm32_port->tx_dma_buf);
1388
David Brazdil0f672f62019-12-10 10:32:29 +00001389 if (stm32_port->wakeirq > 0) {
1390 dev_pm_clear_wake_irq(&pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001391 device_init_wakeup(&pdev->dev, false);
David Brazdil0f672f62019-12-10 10:32:29 +00001392 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393
1394 clk_disable_unprepare(stm32_port->clk);
1395
Olivier Deprez157378f2022-04-04 15:47:50 +02001396 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001397}
1398
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001399#ifdef CONFIG_SERIAL_STM32_CONSOLE
Olivier Deprez157378f2022-04-04 15:47:50 +02001400static void stm32_usart_console_putchar(struct uart_port *port, int ch)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401{
1402 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +02001403 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001404
1405 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
1406 cpu_relax();
1407
1408 writel_relaxed(ch, port->membase + ofs->tdr);
1409}
1410
Olivier Deprez157378f2022-04-04 15:47:50 +02001411static void stm32_usart_console_write(struct console *co, const char *s,
1412 unsigned int cnt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001413{
1414 struct uart_port *port = &stm32_ports[co->index].port;
1415 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +02001416 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1417 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418 unsigned long flags;
1419 u32 old_cr1, new_cr1;
1420 int locked = 1;
1421
1422 local_irq_save(flags);
1423 if (port->sysrq)
1424 locked = 0;
1425 else if (oops_in_progress)
1426 locked = spin_trylock(&port->lock);
1427 else
1428 spin_lock(&port->lock);
1429
1430 /* Save and disable interrupts, enable the transmitter */
1431 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1432 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1433 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
1434 writel_relaxed(new_cr1, port->membase + ofs->cr1);
1435
Olivier Deprez157378f2022-04-04 15:47:50 +02001436 uart_console_write(port, s, cnt, stm32_usart_console_putchar);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001437
1438 /* Restore interrupt state */
1439 writel_relaxed(old_cr1, port->membase + ofs->cr1);
1440
1441 if (locked)
1442 spin_unlock(&port->lock);
1443 local_irq_restore(flags);
1444}
1445
Olivier Deprez157378f2022-04-04 15:47:50 +02001446static int stm32_usart_console_setup(struct console *co, char *options)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001447{
1448 struct stm32_port *stm32port;
1449 int baud = 9600;
1450 int bits = 8;
1451 int parity = 'n';
1452 int flow = 'n';
1453
1454 if (co->index >= STM32_MAX_PORTS)
1455 return -ENODEV;
1456
1457 stm32port = &stm32_ports[co->index];
1458
1459 /*
1460 * This driver does not support early console initialization
1461 * (use ARM early printk support instead), so we only expect
1462 * this to be called during the uart port registration when the
1463 * driver gets probed and the port should be mapped at that point.
1464 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001465 if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001466 return -ENXIO;
1467
1468 if (options)
1469 uart_parse_options(options, &baud, &parity, &bits, &flow);
1470
1471 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1472}
1473
1474static struct console stm32_console = {
1475 .name = STM32_SERIAL_NAME,
1476 .device = uart_console_device,
Olivier Deprez157378f2022-04-04 15:47:50 +02001477 .write = stm32_usart_console_write,
1478 .setup = stm32_usart_console_setup,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479 .flags = CON_PRINTBUFFER,
1480 .index = -1,
1481 .data = &stm32_usart_driver,
1482};
1483
1484#define STM32_SERIAL_CONSOLE (&stm32_console)
1485
1486#else
1487#define STM32_SERIAL_CONSOLE NULL
1488#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1489
1490static struct uart_driver stm32_usart_driver = {
1491 .driver_name = DRIVER_NAME,
1492 .dev_name = STM32_SERIAL_NAME,
1493 .major = 0,
1494 .minor = 0,
1495 .nr = STM32_MAX_PORTS,
1496 .cons = STM32_SERIAL_CONSOLE,
1497};
1498
Olivier Deprez157378f2022-04-04 15:47:50 +02001499static void __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1500 bool enable)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001501{
1502 struct stm32_port *stm32_port = to_stm32_port(port);
Olivier Deprez157378f2022-04-04 15:47:50 +02001503 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504
David Brazdil0f672f62019-12-10 10:32:29 +00001505 if (stm32_port->wakeirq <= 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001506 return;
1507
Olivier Deprez157378f2022-04-04 15:47:50 +02001508 /*
1509 * Enable low-power wake-up and wake-up irq if argument is set to
1510 * "enable", disable low-power wake-up and wake-up irq otherwise
1511 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001512 if (enable) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001513 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
1514 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02001516 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1517 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001518 }
1519}
1520
Olivier Deprez157378f2022-04-04 15:47:50 +02001521static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001522{
1523 struct uart_port *port = dev_get_drvdata(dev);
1524
1525 uart_suspend_port(&stm32_usart_driver, port);
1526
1527 if (device_may_wakeup(dev))
Olivier Deprez157378f2022-04-04 15:47:50 +02001528 stm32_usart_serial_en_wakeup(port, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001529 else
Olivier Deprez157378f2022-04-04 15:47:50 +02001530 stm32_usart_serial_en_wakeup(port, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001531
Olivier Deprez157378f2022-04-04 15:47:50 +02001532 /*
1533 * When "no_console_suspend" is enabled, keep the pinctrl default state
1534 * and rely on bootloader stage to restore this state upon resume.
1535 * Otherwise, apply the idle or sleep states depending on wakeup
1536 * capabilities.
1537 */
1538 if (console_suspend_enabled || !uart_console(port)) {
1539 if (device_may_wakeup(dev))
1540 pinctrl_pm_select_idle_state(dev);
1541 else
1542 pinctrl_pm_select_sleep_state(dev);
1543 }
David Brazdil0f672f62019-12-10 10:32:29 +00001544
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001545 return 0;
1546}
1547
Olivier Deprez157378f2022-04-04 15:47:50 +02001548static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001549{
1550 struct uart_port *port = dev_get_drvdata(dev);
1551
David Brazdil0f672f62019-12-10 10:32:29 +00001552 pinctrl_pm_select_default_state(dev);
1553
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554 if (device_may_wakeup(dev))
Olivier Deprez157378f2022-04-04 15:47:50 +02001555 stm32_usart_serial_en_wakeup(port, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001556
1557 return uart_resume_port(&stm32_usart_driver, port);
1558}
David Brazdil0f672f62019-12-10 10:32:29 +00001559
Olivier Deprez157378f2022-04-04 15:47:50 +02001560static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
David Brazdil0f672f62019-12-10 10:32:29 +00001561{
1562 struct uart_port *port = dev_get_drvdata(dev);
1563 struct stm32_port *stm32port = container_of(port,
1564 struct stm32_port, port);
1565
1566 clk_disable_unprepare(stm32port->clk);
1567
1568 return 0;
1569}
1570
Olivier Deprez157378f2022-04-04 15:47:50 +02001571static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
David Brazdil0f672f62019-12-10 10:32:29 +00001572{
1573 struct uart_port *port = dev_get_drvdata(dev);
1574 struct stm32_port *stm32port = container_of(port,
1575 struct stm32_port, port);
1576
1577 return clk_prepare_enable(stm32port->clk);
1578}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001579
1580static const struct dev_pm_ops stm32_serial_pm_ops = {
Olivier Deprez157378f2022-04-04 15:47:50 +02001581 SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
1582 stm32_usart_runtime_resume, NULL)
1583 SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
1584 stm32_usart_serial_resume)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001585};
1586
1587static struct platform_driver stm32_serial_driver = {
Olivier Deprez157378f2022-04-04 15:47:50 +02001588 .probe = stm32_usart_serial_probe,
1589 .remove = stm32_usart_serial_remove,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001590 .driver = {
1591 .name = DRIVER_NAME,
1592 .pm = &stm32_serial_pm_ops,
1593 .of_match_table = of_match_ptr(stm32_match),
1594 },
1595};
1596
Olivier Deprez157378f2022-04-04 15:47:50 +02001597static int __init stm32_usart_init(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001598{
1599 static char banner[] __initdata = "STM32 USART driver initialized";
1600 int ret;
1601
1602 pr_info("%s\n", banner);
1603
1604 ret = uart_register_driver(&stm32_usart_driver);
1605 if (ret)
1606 return ret;
1607
1608 ret = platform_driver_register(&stm32_serial_driver);
1609 if (ret)
1610 uart_unregister_driver(&stm32_usart_driver);
1611
1612 return ret;
1613}
1614
Olivier Deprez157378f2022-04-04 15:47:50 +02001615static void __exit stm32_usart_exit(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001616{
1617 platform_driver_unregister(&stm32_serial_driver);
1618 uart_unregister_driver(&stm32_usart_driver);
1619}
1620
Olivier Deprez157378f2022-04-04 15:47:50 +02001621module_init(stm32_usart_init);
1622module_exit(stm32_usart_exit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001623
1624MODULE_ALIAS("platform:" DRIVER_NAME);
1625MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1626MODULE_LICENSE("GPL v2");