blob: 11656b6b7c0f0090e93a6e55fda09e4ba7d11672 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Maxime Coquelin48a60922015-06-10 21:19:36 +02002/*
3 * Copyright (C) Maxime Coquelin 2015
Bich HEMON3e5fcba2017-07-13 15:08:26 +00004 * Copyright (C) STMicroelectronics SA 2017
Alexandre TORGUEada86182016-09-15 18:42:33 +02005 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Erwan Le Ray8ebd9662021-01-06 17:21:59 +01006 * Gerald Baeza <gerald.baeza@foss.st.com>
7 * Erwan Le Ray <erwan.leray@foss.st.com>
Maxime Coquelin48a60922015-06-10 21:19:36 +02008 *
9 * Inspired by st-asc.c from STMicroelectronics (c)
10 */
11
Alexandre TORGUE34891872016-09-15 18:42:40 +020012#include <linux/clk.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020013#include <linux/console.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020014#include <linux/delay.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020015#include <linux/dma-direction.h>
16#include <linux/dmaengine.h>
17#include <linux/dma-mapping.h>
18#include <linux/io.h>
19#include <linux/iopoll.h>
20#include <linux/irq.h>
21#include <linux/module.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020022#include <linux/of.h>
23#include <linux/of_platform.h>
Erwan Le Ray94616d92019-06-13 15:49:53 +020024#include <linux/pinctrl/consumer.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020025#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
Fabrice Gasnier270e5a72017-07-13 15:08:30 +000027#include <linux/pm_wakeirq.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020028#include <linux/serial_core.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020029#include <linux/serial.h>
30#include <linux/spinlock.h>
31#include <linux/sysrq.h>
32#include <linux/tty_flip.h>
33#include <linux/tty.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020034
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +053035#include "serial_mctrl_gpio.h"
Alexandre TORGUEbc5a0b52016-09-15 18:42:35 +020036#include "stm32-usart.h"
Maxime Coquelin48a60922015-06-10 21:19:36 +020037
Erwan Le Ray56f9a762021-01-06 17:21:58 +010038static void stm32_usart_stop_tx(struct uart_port *port);
39static void stm32_usart_transmit_chars(struct uart_port *port);
Maxime Coquelin48a60922015-06-10 21:19:36 +020040
41static inline struct stm32_port *to_stm32_port(struct uart_port *port)
42{
43 return container_of(port, struct stm32_port, port);
44}
45
Erwan Le Ray56f9a762021-01-06 17:21:58 +010046static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
Maxime Coquelin48a60922015-06-10 21:19:36 +020047{
48 u32 val;
49
50 val = readl_relaxed(port->membase + reg);
51 val |= bits;
52 writel_relaxed(val, port->membase + reg);
53}
54
Erwan Le Ray56f9a762021-01-06 17:21:58 +010055static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
Maxime Coquelin48a60922015-06-10 21:19:36 +020056{
57 u32 val;
58
59 val = readl_relaxed(port->membase + reg);
60 val &= ~bits;
61 writel_relaxed(val, port->membase + reg);
62}
63
Erwan Le Ray56f9a762021-01-06 17:21:58 +010064static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
65 u32 delay_DDE, u32 baud)
Bich HEMON1bcda092018-03-12 09:50:05 +000066{
67 u32 rs485_deat_dedt;
68 u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
69 bool over8;
70
71 *cr3 |= USART_CR3_DEM;
72 over8 = *cr1 & USART_CR1_OVER8;
73
74 if (over8)
75 rs485_deat_dedt = delay_ADE * baud * 8;
76 else
77 rs485_deat_dedt = delay_ADE * baud * 16;
78
79 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
80 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
81 rs485_deat_dedt_max : rs485_deat_dedt;
82 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
83 USART_CR1_DEAT_MASK;
84 *cr1 |= rs485_deat_dedt;
85
86 if (over8)
87 rs485_deat_dedt = delay_DDE * baud * 8;
88 else
89 rs485_deat_dedt = delay_DDE * baud * 16;
90
91 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
92 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
93 rs485_deat_dedt_max : rs485_deat_dedt;
94 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
95 USART_CR1_DEDT_MASK;
96 *cr1 |= rs485_deat_dedt;
97}
98
Erwan Le Ray56f9a762021-01-06 17:21:58 +010099static int stm32_usart_config_rs485(struct uart_port *port,
100 struct serial_rs485 *rs485conf)
Bich HEMON1bcda092018-03-12 09:50:05 +0000101{
102 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800103 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
104 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Bich HEMON1bcda092018-03-12 09:50:05 +0000105 u32 usartdiv, baud, cr1, cr3;
106 bool over8;
Bich HEMON1bcda092018-03-12 09:50:05 +0000107
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100108 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Bich HEMON1bcda092018-03-12 09:50:05 +0000109
110 port->rs485 = *rs485conf;
111
112 rs485conf->flags |= SER_RS485_RX_DURING_TX;
113
114 if (rs485conf->flags & SER_RS485_ENABLED) {
115 cr1 = readl_relaxed(port->membase + ofs->cr1);
116 cr3 = readl_relaxed(port->membase + ofs->cr3);
117 usartdiv = readl_relaxed(port->membase + ofs->brr);
118 usartdiv = usartdiv & GENMASK(15, 0);
119 over8 = cr1 & USART_CR1_OVER8;
120
121 if (over8)
122 usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
123 << USART_BRR_04_R_SHIFT;
124
125 baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100126 stm32_usart_config_reg_rs485(&cr1, &cr3,
127 rs485conf->delay_rts_before_send,
128 rs485conf->delay_rts_after_send,
129 baud);
Bich HEMON1bcda092018-03-12 09:50:05 +0000130
131 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
132 cr3 &= ~USART_CR3_DEP;
133 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
134 } else {
135 cr3 |= USART_CR3_DEP;
136 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
137 }
138
139 writel_relaxed(cr3, port->membase + ofs->cr3);
140 writel_relaxed(cr1, port->membase + ofs->cr1);
141 } else {
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100142 stm32_usart_clr_bits(port, ofs->cr3,
143 USART_CR3_DEM | USART_CR3_DEP);
144 stm32_usart_clr_bits(port, ofs->cr1,
145 USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
Bich HEMON1bcda092018-03-12 09:50:05 +0000146 }
147
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100148 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Bich HEMON1bcda092018-03-12 09:50:05 +0000149
150 return 0;
151}
152
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100153static int stm32_usart_init_rs485(struct uart_port *port,
154 struct platform_device *pdev)
Bich HEMON1bcda092018-03-12 09:50:05 +0000155{
156 struct serial_rs485 *rs485conf = &port->rs485;
157
158 rs485conf->flags = 0;
159 rs485conf->delay_rts_before_send = 0;
160 rs485conf->delay_rts_after_send = 0;
161
162 if (!pdev->dev.of_node)
163 return -ENODEV;
164
Lukas Wunnerc150c0f2020-05-12 14:40:02 +0200165 return uart_get_rs485_mode(port);
Bich HEMON1bcda092018-03-12 09:50:05 +0000166}
167
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100168static int stm32_usart_pending_rx(struct uart_port *port, u32 *sr,
169 int *last_res, bool threaded)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200170{
171 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800172 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200173 enum dma_status status;
174 struct dma_tx_state state;
175
176 *sr = readl_relaxed(port->membase + ofs->isr);
177
178 if (threaded && stm32_port->rx_ch) {
179 status = dmaengine_tx_status(stm32_port->rx_ch,
180 stm32_port->rx_ch->cookie,
181 &state);
Erwan Le Ray92fc0022021-01-06 17:21:57 +0100182 if (status == DMA_IN_PROGRESS && (*last_res != state.residue))
Alexandre TORGUE34891872016-09-15 18:42:40 +0200183 return 1;
184 else
185 return 0;
186 } else if (*sr & USART_SR_RXNE) {
187 return 1;
188 }
189 return 0;
190}
191
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100192static unsigned long stm32_usart_get_char(struct uart_port *port, u32 *sr,
193 int *last_res)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200194{
195 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800196 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200197 unsigned long c;
198
199 if (stm32_port->rx_ch) {
200 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
201 if ((*last_res) == 0)
202 *last_res = RX_BUF_L;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200203 } else {
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200204 c = readl_relaxed(port->membase + ofs->rdr);
205 /* apply RDR data mask */
206 c &= stm32_port->rdr_mask;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200207 }
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200208
209 return c;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200210}
211
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100212static void stm32_usart_receive_chars(struct uart_port *port, bool threaded)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200213{
214 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200215 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800216 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Erwan Le Rayad767682021-03-04 17:23:00 +0100217 unsigned long c, flags;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200218 u32 sr;
219 char flag;
220
Erwan Le Rayad767682021-03-04 17:23:00 +0100221 if (threaded)
222 spin_lock_irqsave(&port->lock, flags);
223 else
224 spin_lock(&port->lock);
225
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100226 while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res,
227 threaded)) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200228 sr |= USART_SR_DUMMY_RX;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200229 flag = TTY_NORMAL;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200230
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200231 /*
232 * Status bits has to be cleared before reading the RDR:
233 * In FIFO mode, reading the RDR will pop the next data
234 * (if any) along with its status bits into the SR.
235 * Not doing so leads to misalignement between RDR and SR,
236 * and clear status bits of the next rx data.
237 *
238 * Clear errors flags for stm32f7 and stm32h7 compatible
239 * devices. On stm32f4 compatible devices, the error bit is
240 * cleared by the sequence [read SR - read DR].
241 */
242 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
Fabrice Gasnier1250ed72019-11-21 09:10:49 +0100243 writel_relaxed(sr & USART_SR_ERR_MASK,
244 port->membase + ofs->icr);
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200245
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100246 c = stm32_usart_get_char(port, &sr, &stm32_port->last_res);
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200247 port->icount.rx++;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200248 if (sr & USART_SR_ERR_MASK) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200249 if (sr & USART_SR_ORE) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200250 port->icount.overrun++;
251 } else if (sr & USART_SR_PE) {
252 port->icount.parity++;
253 } else if (sr & USART_SR_FE) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200254 /* Break detection if character is null */
255 if (!c) {
256 port->icount.brk++;
257 if (uart_handle_break(port))
258 continue;
259 } else {
260 port->icount.frame++;
261 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200262 }
263
264 sr &= port->read_status_mask;
265
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200266 if (sr & USART_SR_PE) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200267 flag = TTY_PARITY;
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200268 } else if (sr & USART_SR_FE) {
269 if (!c)
270 flag = TTY_BREAK;
271 else
272 flag = TTY_FRAME;
273 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200274 }
275
276 if (uart_handle_sysrq_char(port, c))
277 continue;
278 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
279 }
280
Erwan Le Rayad767682021-03-04 17:23:00 +0100281 if (threaded)
282 spin_unlock_irqrestore(&port->lock, flags);
283 else
284 spin_unlock(&port->lock);
285
Maxime Coquelin48a60922015-06-10 21:19:36 +0200286 tty_flip_buffer_push(tport);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200287}
288
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100289static void stm32_usart_tx_dma_complete(void *arg)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200290{
291 struct uart_port *port = arg;
292 struct stm32_port *stm32port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800293 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Erwan Le Rayf16b90c2021-03-04 17:23:04 +0100294 unsigned long flags;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200295
Erwan Le Rayfb4f2e02021-03-04 17:23:03 +0100296 dmaengine_terminate_async(stm32port->tx_ch);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100297 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200298 stm32port->tx_dma_busy = false;
299
300 /* Let's see if we have pending data to send */
Erwan Le Rayf16b90c2021-03-04 17:23:04 +0100301 spin_lock_irqsave(&port->lock, flags);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100302 stm32_usart_transmit_chars(port);
Erwan Le Rayf16b90c2021-03-04 17:23:04 +0100303 spin_unlock_irqrestore(&port->lock, flags);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200304}
305
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100306static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
Erwan Le Rayd0757192019-06-18 12:02:24 +0200307{
308 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800309 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Erwan Le Rayd0757192019-06-18 12:02:24 +0200310
311 /*
312 * Enables TX FIFO threashold irq when FIFO is enabled,
313 * or TX empty irq when FIFO is disabled
314 */
315 if (stm32_port->fifoen)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100316 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
Erwan Le Rayd0757192019-06-18 12:02:24 +0200317 else
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100318 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
Erwan Le Rayd0757192019-06-18 12:02:24 +0200319}
320
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100321static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
Erwan Le Rayd0757192019-06-18 12:02:24 +0200322{
323 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800324 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Erwan Le Rayd0757192019-06-18 12:02:24 +0200325
326 if (stm32_port->fifoen)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100327 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
Erwan Le Rayd0757192019-06-18 12:02:24 +0200328 else
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100329 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Erwan Le Rayd0757192019-06-18 12:02:24 +0200330}
331
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100332static void stm32_usart_transmit_chars_pio(struct uart_port *port)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200333{
334 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800335 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200336 struct circ_buf *xmit = &port->state->xmit;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200337
338 if (stm32_port->tx_dma_busy) {
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100339 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200340 stm32_port->tx_dma_busy = false;
341 }
342
Erwan Le Ray5d9176e2019-06-18 12:02:23 +0200343 while (!uart_circ_empty(xmit)) {
344 /* Check that TDR is empty before filling FIFO */
345 if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
346 break;
347 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
348 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
349 port->icount.tx++;
350 }
Alexandre TORGUE34891872016-09-15 18:42:40 +0200351
Erwan Le Ray5d9176e2019-06-18 12:02:23 +0200352 /* rely on TXE irq (mask or unmask) for sending remaining data */
353 if (uart_circ_empty(xmit))
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100354 stm32_usart_tx_interrupt_disable(port);
Erwan Le Ray5d9176e2019-06-18 12:02:23 +0200355 else
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100356 stm32_usart_tx_interrupt_enable(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200357}
358
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100359static void stm32_usart_transmit_chars_dma(struct uart_port *port)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200360{
361 struct stm32_port *stm32port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800362 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200363 struct circ_buf *xmit = &port->state->xmit;
364 struct dma_async_tx_descriptor *desc = NULL;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200365 unsigned int count, i;
366
367 if (stm32port->tx_dma_busy)
368 return;
369
370 stm32port->tx_dma_busy = true;
371
372 count = uart_circ_chars_pending(xmit);
373
374 if (count > TX_BUF_L)
375 count = TX_BUF_L;
376
377 if (xmit->tail < xmit->head) {
378 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
379 } else {
380 size_t one = UART_XMIT_SIZE - xmit->tail;
381 size_t two;
382
383 if (one > count)
384 one = count;
385 two = count - one;
386
387 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
388 if (two)
389 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
390 }
391
392 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
393 stm32port->tx_dma_buf,
394 count,
395 DMA_MEM_TO_DEV,
396 DMA_PREP_INTERRUPT);
397
Erwan Le Raye7997f72021-01-06 17:21:56 +0100398 if (!desc)
399 goto fallback_err;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200400
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100401 desc->callback = stm32_usart_tx_dma_complete;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200402 desc->callback_param = port;
403
404 /* Push current DMA TX transaction in the pending queue */
Erwan Le Raye7997f72021-01-06 17:21:56 +0100405 if (dma_submit_error(dmaengine_submit(desc))) {
406 /* dma no yet started, safe to free resources */
407 dmaengine_terminate_async(stm32port->tx_ch);
408 goto fallback_err;
409 }
Alexandre TORGUE34891872016-09-15 18:42:40 +0200410
411 /* Issue pending DMA TX requests */
412 dma_async_issue_pending(stm32port->tx_ch);
413
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100414 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200415
416 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
417 port->icount.tx += count;
Erwan Le Raye7997f72021-01-06 17:21:56 +0100418 return;
419
420fallback_err:
421 for (i = count; i > 0; i--)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100422 stm32_usart_transmit_chars_pio(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200423}
424
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100425static void stm32_usart_transmit_chars(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200426{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200427 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800428 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200429 struct circ_buf *xmit = &port->state->xmit;
430
431 if (port->x_char) {
Alexandre TORGUE34891872016-09-15 18:42:40 +0200432 if (stm32_port->tx_dma_busy)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100433 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200434 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200435 port->x_char = 0;
436 port->icount.tx++;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200437 if (stm32_port->tx_dma_busy)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100438 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200439 return;
440 }
441
Erwan Le Rayb83b9572019-05-21 17:45:44 +0200442 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100443 stm32_usart_tx_interrupt_disable(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200444 return;
445 }
446
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200447 if (ofs->icr == UNDEF_REG)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100448 stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200449 else
Fabrice Gasnier1250ed72019-11-21 09:10:49 +0100450 writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200451
Alexandre TORGUE34891872016-09-15 18:42:40 +0200452 if (stm32_port->tx_ch)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100453 stm32_usart_transmit_chars_dma(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200454 else
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100455 stm32_usart_transmit_chars_pio(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200456
457 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
458 uart_write_wakeup(port);
459
460 if (uart_circ_empty(xmit))
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100461 stm32_usart_tx_interrupt_disable(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200462}
463
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100464static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200465{
466 struct uart_port *port = ptr;
Erwan Le Ray12761862021-03-04 17:23:01 +0100467 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200468 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800469 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200470 u32 sr;
471
Alexandre TORGUEada86182016-09-15 18:42:33 +0200472 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200473
Erwan Le Ray4cc0ed62019-06-18 12:02:22 +0200474 if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
475 writel_relaxed(USART_ICR_RTOCF,
476 port->membase + ofs->icr);
477
Erwan Le Ray12761862021-03-04 17:23:01 +0100478 if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
479 /* Clear wake up flag and disable wake up interrupt */
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000480 writel_relaxed(USART_ICR_WUCF,
481 port->membase + ofs->icr);
Erwan Le Ray12761862021-03-04 17:23:01 +0100482 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
483 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
484 pm_wakeup_event(tport->tty->dev, 0);
485 }
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000486
Alexandre TORGUE34891872016-09-15 18:42:40 +0200487 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100488 stm32_usart_receive_chars(port, false);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200489
Erwan Le Rayad767682021-03-04 17:23:00 +0100490 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
491 spin_lock(&port->lock);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100492 stm32_usart_transmit_chars(port);
Erwan Le Rayad767682021-03-04 17:23:00 +0100493 spin_unlock(&port->lock);
494 }
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200495
Alexandre TORGUE34891872016-09-15 18:42:40 +0200496 if (stm32_port->rx_ch)
497 return IRQ_WAKE_THREAD;
498 else
499 return IRQ_HANDLED;
500}
501
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100502static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200503{
504 struct uart_port *port = ptr;
505 struct stm32_port *stm32_port = to_stm32_port(port);
506
Alexandre TORGUE34891872016-09-15 18:42:40 +0200507 if (stm32_port->rx_ch)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100508 stm32_usart_receive_chars(port, true);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200509
Maxime Coquelin48a60922015-06-10 21:19:36 +0200510 return IRQ_HANDLED;
511}
512
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100513static unsigned int stm32_usart_tx_empty(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200514{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200515 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800516 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200517
Erwan Le Ray3db1d522021-03-04 17:23:07 +0100518 if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
519 return TIOCSER_TEMT;
520
521 return 0;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200522}
523
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100524static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200525{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200526 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800527 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200528
Maxime Coquelin48a60922015-06-10 21:19:36 +0200529 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100530 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200531 else
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100532 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530533
534 mctrl_gpio_set(stm32_port->gpios, mctrl);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200535}
536
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100537static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200538{
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530539 struct stm32_port *stm32_port = to_stm32_port(port);
540 unsigned int ret;
541
Maxime Coquelin48a60922015-06-10 21:19:36 +0200542 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530543 ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
544
545 return mctrl_gpio_get(stm32_port->gpios, &ret);
546}
547
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100548static void stm32_usart_enable_ms(struct uart_port *port)
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530549{
550 mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
551}
552
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100553static void stm32_usart_disable_ms(struct uart_port *port)
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530554{
555 mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200556}
557
558/* Transmit stop */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100559static void stm32_usart_stop_tx(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200560{
Marek Vasutad0c2742020-08-31 19:10:45 +0200561 struct stm32_port *stm32_port = to_stm32_port(port);
562 struct serial_rs485 *rs485conf = &port->rs485;
563
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100564 stm32_usart_tx_interrupt_disable(port);
Marek Vasutad0c2742020-08-31 19:10:45 +0200565
566 if (rs485conf->flags & SER_RS485_ENABLED) {
567 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
568 mctrl_gpio_set(stm32_port->gpios,
569 stm32_port->port.mctrl & ~TIOCM_RTS);
570 } else {
571 mctrl_gpio_set(stm32_port->gpios,
572 stm32_port->port.mctrl | TIOCM_RTS);
573 }
574 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200575}
576
577/* There are probably characters waiting to be transmitted. */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100578static void stm32_usart_start_tx(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200579{
Marek Vasutad0c2742020-08-31 19:10:45 +0200580 struct stm32_port *stm32_port = to_stm32_port(port);
581 struct serial_rs485 *rs485conf = &port->rs485;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200582 struct circ_buf *xmit = &port->state->xmit;
583
584 if (uart_circ_empty(xmit))
585 return;
586
Marek Vasutad0c2742020-08-31 19:10:45 +0200587 if (rs485conf->flags & SER_RS485_ENABLED) {
588 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
589 mctrl_gpio_set(stm32_port->gpios,
590 stm32_port->port.mctrl | TIOCM_RTS);
591 } else {
592 mctrl_gpio_set(stm32_port->gpios,
593 stm32_port->port.mctrl & ~TIOCM_RTS);
594 }
595 }
596
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100597 stm32_usart_transmit_chars(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200598}
599
Erwan Le Ray3d82be82021-03-04 17:23:08 +0100600/* Flush the transmit buffer. */
601static void stm32_usart_flush_buffer(struct uart_port *port)
602{
603 struct stm32_port *stm32_port = to_stm32_port(port);
604 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
605
606 if (stm32_port->tx_ch) {
607 dmaengine_terminate_async(stm32_port->tx_ch);
608 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
609 stm32_port->tx_dma_busy = false;
610 }
611}
612
Maxime Coquelin48a60922015-06-10 21:19:36 +0200613/* Throttle the remote when input buffer is about to overflow. */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100614static void stm32_usart_throttle(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200615{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200616 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800617 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200618 unsigned long flags;
619
620 spin_lock_irqsave(&port->lock, flags);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100621 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200622 if (stm32_port->cr3_irq)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100623 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200624
Maxime Coquelin48a60922015-06-10 21:19:36 +0200625 spin_unlock_irqrestore(&port->lock, flags);
626}
627
628/* Unthrottle the remote, the input buffer can now accept data. */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100629static void stm32_usart_unthrottle(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200630{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200631 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800632 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200633 unsigned long flags;
634
635 spin_lock_irqsave(&port->lock, flags);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100636 stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200637 if (stm32_port->cr3_irq)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100638 stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200639
Maxime Coquelin48a60922015-06-10 21:19:36 +0200640 spin_unlock_irqrestore(&port->lock, flags);
641}
642
643/* Receive stop */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100644static void stm32_usart_stop_rx(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200645{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200646 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800647 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200648
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100649 stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200650 if (stm32_port->cr3_irq)
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100651 stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200652}
653
654/* Handle breaks - ignored by us */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100655static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200656{
657}
658
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100659static int stm32_usart_startup(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200660{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200661 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800662 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Erwan Le Rayf4518a82021-03-04 17:22:57 +0100663 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200664 const char *name = to_platform_device(port->dev)->name;
665 u32 val;
666 int ret;
667
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100668 ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
669 stm32_usart_threaded_interrupt,
Alexandre TORGUE34891872016-09-15 18:42:40 +0200670 IRQF_NO_SUSPEND, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200671 if (ret)
672 return ret;
673
Erwan Le Ray84872dc2019-06-18 12:02:26 +0200674 /* RX FIFO Flush */
675 if (ofs->rqr != UNDEF_REG)
Erwan Le Ray315e2d82021-03-04 17:23:05 +0100676 writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200677
Erwan Le Ray25a8e762021-03-04 17:22:59 +0100678 /* RX enabling */
Erwan Le Rayf4518a82021-03-04 17:22:57 +0100679 val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100680 stm32_usart_set_bits(port, ofs->cr1, val);
Erwan Le Ray84872dc2019-06-18 12:02:26 +0200681
Maxime Coquelin48a60922015-06-10 21:19:36 +0200682 return 0;
683}
684
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100685static void stm32_usart_shutdown(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200686{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200687 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800688 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
689 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200690 u32 val, isr;
691 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200692
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530693 /* Disable modem control interrupts */
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100694 stm32_usart_disable_ms(port);
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +0530695
Erwan Le Ray4cc0ed62019-06-18 12:02:22 +0200696 val = USART_CR1_TXEIE | USART_CR1_TE;
697 val |= stm32_port->cr1_irq | USART_CR1_RE;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200698 val |= BIT(cfg->uart_enable_bit);
Gerald Baeza351a7622017-07-13 15:08:30 +0000699 if (stm32_port->fifoen)
700 val |= USART_CR1_FIFOEN;
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200701
702 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
703 isr, (isr & USART_SR_TC),
704 10, 100000);
705
Erwan Le Rayc31c3ea2021-01-06 17:22:03 +0100706 /* Send the TC error message only when ISR_TC is not set */
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200707 if (ret)
Erwan Le Rayc31c3ea2021-01-06 17:22:03 +0100708 dev_err(port->dev, "Transmission is not complete\n");
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200709
Erwan Le Ray9f77d192021-03-04 17:23:06 +0100710 /* flush RX & TX FIFO */
711 if (ofs->rqr != UNDEF_REG)
712 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
713 port->membase + ofs->rqr);
714
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100715 stm32_usart_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200716
717 free_irq(port->irq, port);
718}
719
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100720static unsigned int stm32_usart_get_databits(struct ktermios *termios)
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200721{
722 unsigned int bits;
723
724 tcflag_t cflag = termios->c_cflag;
725
726 switch (cflag & CSIZE) {
727 /*
728 * CSIZE settings are not necessarily supported in hardware.
729 * CSIZE unsupported configurations are handled here to set word length
730 * to 8 bits word as default configuration and to print debug message.
731 */
732 case CS5:
733 bits = 5;
734 break;
735 case CS6:
736 bits = 6;
737 break;
738 case CS7:
739 bits = 7;
740 break;
741 /* default including CS8 */
742 default:
743 bits = 8;
744 break;
745 }
746
747 return bits;
748}
749
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100750static void stm32_usart_set_termios(struct uart_port *port,
751 struct ktermios *termios,
752 struct ktermios *old)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200753{
754 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800755 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
756 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Bich HEMON1bcda092018-03-12 09:50:05 +0000757 struct serial_rs485 *rs485conf = &port->rs485;
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200758 unsigned int baud, bits;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200759 u32 usartdiv, mantissa, fraction, oversampling;
760 tcflag_t cflag = termios->c_cflag;
Erwan Le Rayf264c6f2021-03-04 17:22:58 +0100761 u32 cr1, cr2, cr3, isr;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200762 unsigned long flags;
Erwan Le Rayf264c6f2021-03-04 17:22:58 +0100763 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200764
765 if (!stm32_port->hw_flow_control)
766 cflag &= ~CRTSCTS;
767
768 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
769
770 spin_lock_irqsave(&port->lock, flags);
771
Erwan Le Rayf264c6f2021-03-04 17:22:58 +0100772 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
773 isr,
774 (isr & USART_SR_TC),
775 10, 100000);
776
777 /* Send the TC error message only when ISR_TC is not set. */
778 if (ret)
779 dev_err(port->dev, "Transmission is not complete\n");
780
Maxime Coquelin48a60922015-06-10 21:19:36 +0200781 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200782 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200783
Erwan Le Ray84872dc2019-06-18 12:02:26 +0200784 /* flush RX & TX FIFO */
785 if (ofs->rqr != UNDEF_REG)
Erwan Le Ray315e2d82021-03-04 17:23:05 +0100786 writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
787 port->membase + ofs->rqr);
Bich HEMON1bcda092018-03-12 09:50:05 +0000788
Erwan Le Ray84872dc2019-06-18 12:02:26 +0200789 cr1 = USART_CR1_TE | USART_CR1_RE;
Gerald Baeza351a7622017-07-13 15:08:30 +0000790 if (stm32_port->fifoen)
791 cr1 |= USART_CR1_FIFOEN;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200792 cr2 = 0;
Erwan Le Ray25a8e762021-03-04 17:22:59 +0100793
794 /* Tx and RX FIFO configuration */
Erwan Le Rayd0757192019-06-18 12:02:24 +0200795 cr3 = readl_relaxed(port->membase + ofs->cr3);
Erwan Le Ray25a8e762021-03-04 17:22:59 +0100796 cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
797 if (stm32_port->fifoen) {
798 cr3 &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK);
799 cr3 |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT;
800 cr3 |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT;
801 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200802
803 if (cflag & CSTOPB)
804 cr2 |= USART_CR2_STOP_2B;
805
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100806 bits = stm32_usart_get_databits(termios);
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200807 stm32_port->rdr_mask = (BIT(bits) - 1);
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200808
Maxime Coquelin48a60922015-06-10 21:19:36 +0200809 if (cflag & PARENB) {
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200810 bits++;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200811 cr1 |= USART_CR1_PCE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200812 }
813
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200814 /*
815 * Word length configuration:
816 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
817 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
818 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
819 * M0 and M1 already cleared by cr1 initialization.
820 */
821 if (bits == 9)
822 cr1 |= USART_CR1_M0;
823 else if ((bits == 7) && cfg->has_7bits_data)
824 cr1 |= USART_CR1_M1;
825 else if (bits != 8)
826 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
827 , bits);
828
Erwan Le Ray4cc0ed62019-06-18 12:02:22 +0200829 if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
830 stm32_port->fifoen)) {
831 if (cflag & CSTOPB)
832 bits = bits + 3; /* 1 start bit + 2 stop bits */
833 else
834 bits = bits + 2; /* 1 start bit + 1 stop bit */
835
836 /* RX timeout irq to occur after last stop bit + bits */
837 stm32_port->cr1_irq = USART_CR1_RTOIE;
838 writel_relaxed(bits, port->membase + ofs->rtor);
839 cr2 |= USART_CR2_RTOEN;
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200840 /* Not using dma, enable fifo threshold irq */
841 if (!stm32_port->rx_ch)
842 stm32_port->cr3_irq = USART_CR3_RXFTIE;
Erwan Le Ray4cc0ed62019-06-18 12:02:22 +0200843 }
844
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +0200845 cr1 |= stm32_port->cr1_irq;
846 cr3 |= stm32_port->cr3_irq;
847
Maxime Coquelin48a60922015-06-10 21:19:36 +0200848 if (cflag & PARODD)
849 cr1 |= USART_CR1_PS;
850
851 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
852 if (cflag & CRTSCTS) {
853 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Bich HEMON35abe982017-07-13 15:08:28 +0000854 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200855 }
856
857 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
858
859 /*
860 * The USART supports 16 or 8 times oversampling.
861 * By default we prefer 16 times oversampling, so that the receiver
862 * has a better tolerance to clock deviations.
863 * 8 times oversampling is only used to achieve higher speeds.
864 */
865 if (usartdiv < 16) {
866 oversampling = 8;
Bich HEMON1bcda092018-03-12 09:50:05 +0000867 cr1 |= USART_CR1_OVER8;
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100868 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200869 } else {
870 oversampling = 16;
Bich HEMON1bcda092018-03-12 09:50:05 +0000871 cr1 &= ~USART_CR1_OVER8;
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100872 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200873 }
874
875 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
876 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200877 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200878
879 uart_update_timeout(port, cflag, baud);
880
881 port->read_status_mask = USART_SR_ORE;
882 if (termios->c_iflag & INPCK)
883 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
884 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200885 port->read_status_mask |= USART_SR_FE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200886
887 /* Characters to ignore */
888 port->ignore_status_mask = 0;
889 if (termios->c_iflag & IGNPAR)
890 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
891 if (termios->c_iflag & IGNBRK) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200892 port->ignore_status_mask |= USART_SR_FE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200893 /*
894 * If we're ignoring parity and break indicators,
895 * ignore overruns too (for real raw support).
896 */
897 if (termios->c_iflag & IGNPAR)
898 port->ignore_status_mask |= USART_SR_ORE;
899 }
900
901 /* Ignore all characters if CREAD is not set */
902 if ((termios->c_cflag & CREAD) == 0)
903 port->ignore_status_mask |= USART_SR_DUMMY_RX;
904
Alexandre TORGUE34891872016-09-15 18:42:40 +0200905 if (stm32_port->rx_ch)
906 cr3 |= USART_CR3_DMAR;
907
Bich HEMON1bcda092018-03-12 09:50:05 +0000908 if (rs485conf->flags & SER_RS485_ENABLED) {
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100909 stm32_usart_config_reg_rs485(&cr1, &cr3,
910 rs485conf->delay_rts_before_send,
911 rs485conf->delay_rts_after_send,
912 baud);
Bich HEMON1bcda092018-03-12 09:50:05 +0000913 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
914 cr3 &= ~USART_CR3_DEP;
915 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
916 } else {
917 cr3 |= USART_CR3_DEP;
918 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
919 }
920
921 } else {
922 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
923 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
924 }
925
Erwan Le Ray12761862021-03-04 17:23:01 +0100926 /* Configure wake up from low power on start bit detection */
927 if (stm32_port->wakeirq > 0) {
928 cr3 &= ~USART_CR3_WUS_MASK;
929 cr3 |= USART_CR3_WUS_START_BIT;
930 }
931
Alexandre TORGUEada86182016-09-15 18:42:33 +0200932 writel_relaxed(cr3, port->membase + ofs->cr3);
933 writel_relaxed(cr2, port->membase + ofs->cr2);
934 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200935
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100936 stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200937 spin_unlock_irqrestore(&port->lock, flags);
Erwan Le Ray436c9792021-03-04 17:23:02 +0100938
939 /* Handle modem control interrupts */
940 if (UART_ENABLE_MS(port, termios->c_cflag))
941 stm32_usart_enable_ms(port);
942 else
943 stm32_usart_disable_ms(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200944}
945
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100946static const char *stm32_usart_type(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200947{
948 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
949}
950
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100951static void stm32_usart_release_port(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200952{
953}
954
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100955static int stm32_usart_request_port(struct uart_port *port)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200956{
957 return 0;
958}
959
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100960static void stm32_usart_config_port(struct uart_port *port, int flags)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200961{
962 if (flags & UART_CONFIG_TYPE)
963 port->type = PORT_STM32;
964}
965
966static int
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100967stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200968{
969 /* No user changeable parameters */
970 return -EINVAL;
971}
972
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100973static void stm32_usart_pm(struct uart_port *port, unsigned int state,
974 unsigned int oldstate)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200975{
976 struct stm32_port *stm32port = container_of(port,
977 struct stm32_port, port);
Stephen Boydd825f0b2021-01-22 19:44:25 -0800978 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
979 const struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200980 unsigned long flags = 0;
981
982 switch (state) {
983 case UART_PM_STATE_ON:
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +0200984 pm_runtime_get_sync(port->dev);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200985 break;
986 case UART_PM_STATE_OFF:
987 spin_lock_irqsave(&port->lock, flags);
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100988 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200989 spin_unlock_irqrestore(&port->lock, flags);
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +0200990 pm_runtime_put_sync(port->dev);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200991 break;
992 }
993}
994
995static const struct uart_ops stm32_uart_ops = {
Erwan Le Ray56f9a762021-01-06 17:21:58 +0100996 .tx_empty = stm32_usart_tx_empty,
997 .set_mctrl = stm32_usart_set_mctrl,
998 .get_mctrl = stm32_usart_get_mctrl,
999 .stop_tx = stm32_usart_stop_tx,
1000 .start_tx = stm32_usart_start_tx,
1001 .throttle = stm32_usart_throttle,
1002 .unthrottle = stm32_usart_unthrottle,
1003 .stop_rx = stm32_usart_stop_rx,
1004 .enable_ms = stm32_usart_enable_ms,
1005 .break_ctl = stm32_usart_break_ctl,
1006 .startup = stm32_usart_startup,
1007 .shutdown = stm32_usart_shutdown,
Erwan Le Ray3d82be82021-03-04 17:23:08 +01001008 .flush_buffer = stm32_usart_flush_buffer,
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001009 .set_termios = stm32_usart_set_termios,
1010 .pm = stm32_usart_pm,
1011 .type = stm32_usart_type,
1012 .release_port = stm32_usart_release_port,
1013 .request_port = stm32_usart_request_port,
1014 .config_port = stm32_usart_config_port,
1015 .verify_port = stm32_usart_verify_port,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001016};
1017
Erwan Le Ray97f3a082021-01-06 17:22:02 +01001018static void stm32_usart_deinit_port(struct stm32_port *stm32port)
1019{
1020 clk_disable_unprepare(stm32port->clk);
1021}
1022
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001023static int stm32_usart_init_port(struct stm32_port *stm32port,
1024 struct platform_device *pdev)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001025{
1026 struct uart_port *port = &stm32port->port;
1027 struct resource *res;
Erwan Le Raye0f2a902021-01-21 15:23:09 +01001028 int ret, irq;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001029
Erwan Le Raye0f2a902021-01-21 15:23:09 +01001030 irq = platform_get_irq(pdev, 0);
1031 if (irq <= 0)
1032 return irq ? : -ENODEV;
Erwan Le Ray92fc0022021-01-06 17:21:57 +01001033
Maxime Coquelin48a60922015-06-10 21:19:36 +02001034 port->iotype = UPIO_MEM;
1035 port->flags = UPF_BOOT_AUTOCONF;
1036 port->ops = &stm32_uart_ops;
1037 port->dev = &pdev->dev;
Erwan Le Rayd0757192019-06-18 12:02:24 +02001038 port->fifosize = stm32port->info->cfg.fifosize;
Dmitry Safonov9feedaa2019-12-13 00:06:43 +00001039 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
Erwan Le Raye0f2a902021-01-21 15:23:09 +01001040 port->irq = irq;
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001041 port->rs485_config = stm32_usart_config_rs485;
Bich HEMON7d8f6862018-03-15 08:44:46 +00001042
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001043 ret = stm32_usart_init_rs485(port, pdev);
Lukas Wunnerc150c0f2020-05-12 14:40:02 +02001044 if (ret)
1045 return ret;
Bich HEMON7d8f6862018-03-15 08:44:46 +00001046
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001047 if (stm32port->info->cfg.has_wakeup) {
Holger Assmannfdf16d72020-08-13 17:27:57 +02001048 stm32port->wakeirq = platform_get_irq_optional(pdev, 1);
Stephen Boyd1df21782019-07-30 11:15:44 -07001049 if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO)
1050 return stm32port->wakeirq ? : -ENODEV;
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001051 }
1052
Gerald Baeza351a7622017-07-13 15:08:30 +00001053 stm32port->fifoen = stm32port->info->cfg.has_fifo;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001054
1055 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1056 port->membase = devm_ioremap_resource(&pdev->dev, res);
1057 if (IS_ERR(port->membase))
1058 return PTR_ERR(port->membase);
1059 port->mapbase = res->start;
1060
1061 spin_lock_init(&port->lock);
1062
1063 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
1064 if (IS_ERR(stm32port->clk))
1065 return PTR_ERR(stm32port->clk);
1066
1067 /* Ensure that clk rate is correct by enabling the clk */
1068 ret = clk_prepare_enable(stm32port->clk);
1069 if (ret)
1070 return ret;
1071
1072 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
Fabrice Gasnierada80042017-07-13 15:08:29 +00001073 if (!stm32port->port.uartclk) {
Maxime Coquelin48a60922015-06-10 21:19:36 +02001074 ret = -EINVAL;
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +05301075 goto err_clk;
Fabrice Gasnierada80042017-07-13 15:08:29 +00001076 }
Maxime Coquelin48a60922015-06-10 21:19:36 +02001077
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +05301078 stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
1079 if (IS_ERR(stm32port->gpios)) {
1080 ret = PTR_ERR(stm32port->gpios);
1081 goto err_clk;
1082 }
1083
Erwan Le Ray93593692021-01-06 17:22:01 +01001084 /*
1085 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
1086 * properties should not be specified.
1087 */
Manivannan Sadhasivam6cf61b92020-04-20 22:32:04 +05301088 if (stm32port->hw_flow_control) {
1089 if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
1090 mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
1091 dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
1092 ret = -EINVAL;
1093 goto err_clk;
1094 }
1095 }
1096
1097 return ret;
1098
1099err_clk:
1100 clk_disable_unprepare(stm32port->clk);
1101
Maxime Coquelin48a60922015-06-10 21:19:36 +02001102 return ret;
1103}
1104
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001105static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001106{
1107 struct device_node *np = pdev->dev.of_node;
1108 int id;
1109
1110 if (!np)
1111 return NULL;
1112
1113 id = of_alias_get_id(np, "serial");
Gerald Baezae5707912017-07-13 15:08:27 +00001114 if (id < 0) {
1115 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1116 return NULL;
1117 }
Maxime Coquelin48a60922015-06-10 21:19:36 +02001118
1119 if (WARN_ON(id >= STM32_MAX_PORTS))
1120 return NULL;
1121
Erwan Le Ray6fd9fff2020-05-20 15:39:32 +02001122 stm32_ports[id].hw_flow_control =
1123 of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
1124 of_property_read_bool (np, "uart-has-rtscts");
Maxime Coquelin48a60922015-06-10 21:19:36 +02001125 stm32_ports[id].port.line = id;
Erwan Le Ray4cc0ed62019-06-18 12:02:22 +02001126 stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
Erwan Le Rayd0a6a7b2019-06-18 12:02:25 +02001127 stm32_ports[id].cr3_irq = 0;
Gerald Baezae5707912017-07-13 15:08:27 +00001128 stm32_ports[id].last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001129 return &stm32_ports[id];
1130}
1131
1132#ifdef CONFIG_OF
1133static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +02001134 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
Alexandre TORGUEada86182016-09-15 18:42:33 +02001135 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001136 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +02001137 {},
1138};
1139
1140MODULE_DEVICE_TABLE(of, stm32_match);
1141#endif
1142
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001143static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
1144 struct platform_device *pdev)
Alexandre TORGUE34891872016-09-15 18:42:40 +02001145{
Stephen Boydd825f0b2021-01-22 19:44:25 -08001146 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001147 struct uart_port *port = &stm32port->port;
1148 struct device *dev = &pdev->dev;
1149 struct dma_slave_config config;
1150 struct dma_async_tx_descriptor *desc = NULL;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001151 int ret;
1152
1153 /* Request DMA RX channel */
1154 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
1155 if (!stm32port->rx_ch) {
1156 dev_info(dev, "rx dma alloc failed\n");
1157 return -ENODEV;
1158 }
1159 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
Erwan Le Ray92fc0022021-01-06 17:21:57 +01001160 &stm32port->rx_dma_buf,
1161 GFP_KERNEL);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001162 if (!stm32port->rx_buf) {
1163 ret = -ENOMEM;
1164 goto alloc_err;
1165 }
1166
1167 /* Configure DMA channel */
1168 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +02001169 config.src_addr = port->mapbase + ofs->rdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001170 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1171
1172 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
1173 if (ret < 0) {
1174 dev_err(dev, "rx dma channel config failed\n");
1175 ret = -ENODEV;
1176 goto config_err;
1177 }
1178
1179 /* Prepare a DMA cyclic transaction */
1180 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
1181 stm32port->rx_dma_buf,
1182 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
1183 DMA_PREP_INTERRUPT);
1184 if (!desc) {
1185 dev_err(dev, "rx dma prep cyclic failed\n");
1186 ret = -ENODEV;
1187 goto config_err;
1188 }
1189
1190 /* No callback as dma buffer is drained on usart interrupt */
1191 desc->callback = NULL;
1192 desc->callback_param = NULL;
1193
1194 /* Push current DMA transaction in the pending queue */
Erwan Le Raye7997f72021-01-06 17:21:56 +01001195 ret = dma_submit_error(dmaengine_submit(desc));
1196 if (ret) {
1197 dmaengine_terminate_sync(stm32port->rx_ch);
1198 goto config_err;
1199 }
Alexandre TORGUE34891872016-09-15 18:42:40 +02001200
1201 /* Issue pending DMA requests */
1202 dma_async_issue_pending(stm32port->rx_ch);
1203
1204 return 0;
1205
1206config_err:
1207 dma_free_coherent(&pdev->dev,
1208 RX_BUF_L, stm32port->rx_buf,
1209 stm32port->rx_dma_buf);
1210
1211alloc_err:
1212 dma_release_channel(stm32port->rx_ch);
1213 stm32port->rx_ch = NULL;
1214
1215 return ret;
1216}
1217
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001218static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
1219 struct platform_device *pdev)
Alexandre TORGUE34891872016-09-15 18:42:40 +02001220{
Stephen Boydd825f0b2021-01-22 19:44:25 -08001221 const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001222 struct uart_port *port = &stm32port->port;
1223 struct device *dev = &pdev->dev;
1224 struct dma_slave_config config;
1225 int ret;
1226
1227 stm32port->tx_dma_busy = false;
1228
1229 /* Request DMA TX channel */
1230 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
1231 if (!stm32port->tx_ch) {
1232 dev_info(dev, "tx dma alloc failed\n");
1233 return -ENODEV;
1234 }
1235 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
Erwan Le Ray92fc0022021-01-06 17:21:57 +01001236 &stm32port->tx_dma_buf,
1237 GFP_KERNEL);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001238 if (!stm32port->tx_buf) {
1239 ret = -ENOMEM;
1240 goto alloc_err;
1241 }
1242
1243 /* Configure DMA channel */
1244 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +02001245 config.dst_addr = port->mapbase + ofs->tdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001246 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1247
1248 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1249 if (ret < 0) {
1250 dev_err(dev, "tx dma channel config failed\n");
1251 ret = -ENODEV;
1252 goto config_err;
1253 }
1254
1255 return 0;
1256
1257config_err:
1258 dma_free_coherent(&pdev->dev,
1259 TX_BUF_L, stm32port->tx_buf,
1260 stm32port->tx_dma_buf);
1261
1262alloc_err:
1263 dma_release_channel(stm32port->tx_ch);
1264 stm32port->tx_ch = NULL;
1265
1266 return ret;
1267}
1268
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001269static int stm32_usart_serial_probe(struct platform_device *pdev)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001270{
Maxime Coquelin48a60922015-06-10 21:19:36 +02001271 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001272 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001273
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001274 stm32port = stm32_usart_of_get_port(pdev);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001275 if (!stm32port)
1276 return -ENODEV;
1277
Stephen Boydd825f0b2021-01-22 19:44:25 -08001278 stm32port->info = of_device_get_match_data(&pdev->dev);
1279 if (!stm32port->info)
Alexandre TORGUEada86182016-09-15 18:42:33 +02001280 return -EINVAL;
1281
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001282 ret = stm32_usart_init_port(stm32port, pdev);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001283 if (ret)
1284 return ret;
1285
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001286 if (stm32port->wakeirq > 0) {
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001287 ret = device_init_wakeup(&pdev->dev, true);
1288 if (ret)
1289 goto err_uninit;
Erwan Le Ray5297f272019-05-21 17:45:46 +02001290
1291 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1292 stm32port->wakeirq);
1293 if (ret)
1294 goto err_nowup;
1295
1296 device_set_wakeup_enable(&pdev->dev, false);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001297 }
1298
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001299 ret = stm32_usart_of_dma_rx_probe(stm32port, pdev);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001300 if (ret)
1301 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
1302
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001303 ret = stm32_usart_of_dma_tx_probe(stm32port, pdev);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001304 if (ret)
1305 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
1306
Maxime Coquelin48a60922015-06-10 21:19:36 +02001307 platform_set_drvdata(pdev, &stm32port->port);
1308
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +02001309 pm_runtime_get_noresume(&pdev->dev);
1310 pm_runtime_set_active(&pdev->dev);
1311 pm_runtime_enable(&pdev->dev);
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001312
1313 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1314 if (ret)
1315 goto err_port;
1316
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +02001317 pm_runtime_put_sync(&pdev->dev);
1318
Maxime Coquelin48a60922015-06-10 21:19:36 +02001319 return 0;
Fabrice Gasnierada80042017-07-13 15:08:29 +00001320
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001321err_port:
1322 pm_runtime_disable(&pdev->dev);
1323 pm_runtime_set_suspended(&pdev->dev);
1324 pm_runtime_put_noidle(&pdev->dev);
1325
1326 if (stm32port->rx_ch) {
1327 dmaengine_terminate_async(stm32port->rx_ch);
1328 dma_release_channel(stm32port->rx_ch);
1329 }
1330
1331 if (stm32port->rx_dma_buf)
1332 dma_free_coherent(&pdev->dev,
1333 RX_BUF_L, stm32port->rx_buf,
1334 stm32port->rx_dma_buf);
1335
1336 if (stm32port->tx_ch) {
1337 dmaengine_terminate_async(stm32port->tx_ch);
1338 dma_release_channel(stm32port->tx_ch);
1339 }
1340
1341 if (stm32port->tx_dma_buf)
1342 dma_free_coherent(&pdev->dev,
1343 TX_BUF_L, stm32port->tx_buf,
1344 stm32port->tx_dma_buf);
1345
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001346 if (stm32port->wakeirq > 0)
Erwan Le Ray5297f272019-05-21 17:45:46 +02001347 dev_pm_clear_wake_irq(&pdev->dev);
1348
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001349err_nowup:
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001350 if (stm32port->wakeirq > 0)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001351 device_init_wakeup(&pdev->dev, false);
1352
Fabrice Gasnierada80042017-07-13 15:08:29 +00001353err_uninit:
Erwan Le Ray97f3a082021-01-06 17:22:02 +01001354 stm32_usart_deinit_port(stm32port);
Fabrice Gasnierada80042017-07-13 15:08:29 +00001355
1356 return ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001357}
1358
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001359static int stm32_usart_serial_remove(struct platform_device *pdev)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001360{
1361 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +02001362 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -08001363 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +02001364 int err;
1365
1366 pm_runtime_get_sync(&pdev->dev);
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001367 err = uart_remove_one_port(&stm32_usart_driver, port);
1368 if (err)
1369 return(err);
1370
1371 pm_runtime_disable(&pdev->dev);
1372 pm_runtime_set_suspended(&pdev->dev);
1373 pm_runtime_put_noidle(&pdev->dev);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001374
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001375 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001376
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001377 if (stm32_port->rx_ch) {
1378 dmaengine_terminate_async(stm32_port->rx_ch);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001379 dma_release_channel(stm32_port->rx_ch);
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001380 }
Alexandre TORGUE34891872016-09-15 18:42:40 +02001381
1382 if (stm32_port->rx_dma_buf)
1383 dma_free_coherent(&pdev->dev,
1384 RX_BUF_L, stm32_port->rx_buf,
1385 stm32_port->rx_dma_buf);
1386
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001387 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001388
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001389 if (stm32_port->tx_ch) {
1390 dmaengine_terminate_async(stm32_port->tx_ch);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001391 dma_release_channel(stm32_port->tx_ch);
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001392 }
Alexandre TORGUE34891872016-09-15 18:42:40 +02001393
1394 if (stm32_port->tx_dma_buf)
1395 dma_free_coherent(&pdev->dev,
1396 TX_BUF_L, stm32_port->tx_buf,
1397 stm32_port->tx_dma_buf);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +02001398
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001399 if (stm32_port->wakeirq > 0) {
Erwan Le Ray5297f272019-05-21 17:45:46 +02001400 dev_pm_clear_wake_irq(&pdev->dev);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001401 device_init_wakeup(&pdev->dev, false);
Erwan Le Ray5297f272019-05-21 17:45:46 +02001402 }
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001403
Erwan Le Ray97f3a082021-01-06 17:22:02 +01001404 stm32_usart_deinit_port(stm32_port);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001405
Erwan Le Ray87fd0742021-03-04 17:22:56 +01001406 return 0;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001407}
1408
Maxime Coquelin48a60922015-06-10 21:19:36 +02001409#ifdef CONFIG_SERIAL_STM32_CONSOLE
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001410static void stm32_usart_console_putchar(struct uart_port *port, int ch)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001411{
Alexandre TORGUEada86182016-09-15 18:42:33 +02001412 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -08001413 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001414
1415 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +02001416 cpu_relax();
1417
Alexandre TORGUEada86182016-09-15 18:42:33 +02001418 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001419}
1420
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001421static void stm32_usart_console_write(struct console *co, const char *s,
1422 unsigned int cnt)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001423{
1424 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001425 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -08001426 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1427 const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001428 unsigned long flags;
1429 u32 old_cr1, new_cr1;
1430 int locked = 1;
1431
1432 local_irq_save(flags);
1433 if (port->sysrq)
1434 locked = 0;
1435 else if (oops_in_progress)
1436 locked = spin_trylock(&port->lock);
1437 else
1438 spin_lock(&port->lock);
1439
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001440 /* Save and disable interrupts, enable the transmitter */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001441 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001442 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001443 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
Alexandre TORGUEada86182016-09-15 18:42:33 +02001444 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001445
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001446 uart_console_write(port, s, cnt, stm32_usart_console_putchar);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001447
1448 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001449 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001450
1451 if (locked)
1452 spin_unlock(&port->lock);
1453 local_irq_restore(flags);
1454}
1455
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001456static int stm32_usart_console_setup(struct console *co, char *options)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001457{
1458 struct stm32_port *stm32port;
1459 int baud = 9600;
1460 int bits = 8;
1461 int parity = 'n';
1462 int flow = 'n';
1463
1464 if (co->index >= STM32_MAX_PORTS)
1465 return -ENODEV;
1466
1467 stm32port = &stm32_ports[co->index];
1468
1469 /*
1470 * This driver does not support early console initialization
1471 * (use ARM early printk support instead), so we only expect
1472 * this to be called during the uart port registration when the
1473 * driver gets probed and the port should be mapped at that point.
1474 */
Erwan Le Ray92fc0022021-01-06 17:21:57 +01001475 if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001476 return -ENXIO;
1477
1478 if (options)
1479 uart_parse_options(options, &baud, &parity, &bits, &flow);
1480
1481 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1482}
1483
1484static struct console stm32_console = {
1485 .name = STM32_SERIAL_NAME,
1486 .device = uart_console_device,
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001487 .write = stm32_usart_console_write,
1488 .setup = stm32_usart_console_setup,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001489 .flags = CON_PRINTBUFFER,
1490 .index = -1,
1491 .data = &stm32_usart_driver,
1492};
1493
1494#define STM32_SERIAL_CONSOLE (&stm32_console)
1495
1496#else
1497#define STM32_SERIAL_CONSOLE NULL
1498#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1499
1500static struct uart_driver stm32_usart_driver = {
1501 .driver_name = DRIVER_NAME,
1502 .dev_name = STM32_SERIAL_NAME,
1503 .major = 0,
1504 .minor = 0,
1505 .nr = STM32_MAX_PORTS,
1506 .cons = STM32_SERIAL_CONSOLE,
1507};
1508
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001509static void __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1510 bool enable)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001511{
1512 struct stm32_port *stm32_port = to_stm32_port(port);
Stephen Boydd825f0b2021-01-22 19:44:25 -08001513 const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001514
Erwan Le Ray2c58e562019-05-21 17:45:47 +02001515 if (stm32_port->wakeirq <= 0)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001516 return;
1517
Erwan Le Ray12761862021-03-04 17:23:01 +01001518 /*
1519 * Enable low-power wake-up and wake-up irq if argument is set to
1520 * "enable", disable low-power wake-up and wake-up irq otherwise
1521 */
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001522 if (enable) {
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001523 stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
Erwan Le Ray12761862021-03-04 17:23:01 +01001524 stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001525 } else {
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001526 stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
Erwan Le Ray12761862021-03-04 17:23:01 +01001527 stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001528 }
1529}
1530
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001531static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001532{
1533 struct uart_port *port = dev_get_drvdata(dev);
1534
1535 uart_suspend_port(&stm32_usart_driver, port);
1536
Erwan Le Ray1631eee2021-03-19 19:42:49 +01001537 if (device_may_wakeup(dev) || device_wakeup_path(dev))
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001538 stm32_usart_serial_en_wakeup(port, true);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001539 else
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001540 stm32_usart_serial_en_wakeup(port, false);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001541
Erwan Le Ray55484fc2020-05-19 11:41:04 +02001542 /*
1543 * When "no_console_suspend" is enabled, keep the pinctrl default state
1544 * and rely on bootloader stage to restore this state upon resume.
1545 * Otherwise, apply the idle or sleep states depending on wakeup
1546 * capabilities.
1547 */
1548 if (console_suspend_enabled || !uart_console(port)) {
Erwan Le Ray1631eee2021-03-19 19:42:49 +01001549 if (device_may_wakeup(dev) || device_wakeup_path(dev))
Erwan Le Ray55484fc2020-05-19 11:41:04 +02001550 pinctrl_pm_select_idle_state(dev);
1551 else
1552 pinctrl_pm_select_sleep_state(dev);
1553 }
Erwan Le Ray94616d92019-06-13 15:49:53 +02001554
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001555 return 0;
1556}
1557
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001558static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001559{
1560 struct uart_port *port = dev_get_drvdata(dev);
1561
Erwan Le Ray94616d92019-06-13 15:49:53 +02001562 pinctrl_pm_select_default_state(dev);
1563
Erwan Le Ray1631eee2021-03-19 19:42:49 +01001564 if (device_may_wakeup(dev) || device_wakeup_path(dev))
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001565 stm32_usart_serial_en_wakeup(port, false);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001566
1567 return uart_resume_port(&stm32_usart_driver, port);
1568}
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001569
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001570static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +02001571{
1572 struct uart_port *port = dev_get_drvdata(dev);
1573 struct stm32_port *stm32port = container_of(port,
1574 struct stm32_port, port);
1575
1576 clk_disable_unprepare(stm32port->clk);
1577
1578 return 0;
1579}
1580
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001581static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
Erwan Le Rayfb6dcef2019-06-13 15:49:54 +02001582{
1583 struct uart_port *port = dev_get_drvdata(dev);
1584 struct stm32_port *stm32port = container_of(port,
1585 struct stm32_port, port);
1586
1587 return clk_prepare_enable(stm32port->clk);
1588}
1589
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001590static const struct dev_pm_ops stm32_serial_pm_ops = {
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001591 SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
1592 stm32_usart_runtime_resume, NULL)
1593 SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
1594 stm32_usart_serial_resume)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001595};
1596
Maxime Coquelin48a60922015-06-10 21:19:36 +02001597static struct platform_driver stm32_serial_driver = {
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001598 .probe = stm32_usart_serial_probe,
1599 .remove = stm32_usart_serial_remove,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001600 .driver = {
1601 .name = DRIVER_NAME,
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001602 .pm = &stm32_serial_pm_ops,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001603 .of_match_table = of_match_ptr(stm32_match),
1604 },
1605};
1606
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001607static int __init stm32_usart_init(void)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001608{
1609 static char banner[] __initdata = "STM32 USART driver initialized";
1610 int ret;
1611
1612 pr_info("%s\n", banner);
1613
1614 ret = uart_register_driver(&stm32_usart_driver);
1615 if (ret)
1616 return ret;
1617
1618 ret = platform_driver_register(&stm32_serial_driver);
1619 if (ret)
1620 uart_unregister_driver(&stm32_usart_driver);
1621
1622 return ret;
1623}
1624
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001625static void __exit stm32_usart_exit(void)
Maxime Coquelin48a60922015-06-10 21:19:36 +02001626{
1627 platform_driver_unregister(&stm32_serial_driver);
1628 uart_unregister_driver(&stm32_usart_driver);
1629}
1630
Erwan Le Ray56f9a762021-01-06 17:21:58 +01001631module_init(stm32_usart_init);
1632module_exit(stm32_usart_exit);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001633
1634MODULE_ALIAS("platform:" DRIVER_NAME);
1635MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1636MODULE_LICENSE("GPL v2");