blob: 1334e42939776a15bc7aefa3ec7aa25036734895 [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>
6 * Gerald Baeza <gerald.baeza@st.com>
Maxime Coquelin48a60922015-06-10 21:19:36 +02007 *
8 * Inspired by st-asc.c from STMicroelectronics (c)
9 */
10
Maxime Coquelin6b596a82015-06-16 11:12:19 +020011#if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Maxime Coquelin48a60922015-06-10 21:19:36 +020012#define SUPPORT_SYSRQ
13#endif
14
Alexandre TORGUE34891872016-09-15 18:42:40 +020015#include <linux/clk.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020016#include <linux/console.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020017#include <linux/delay.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020018#include <linux/dma-direction.h>
19#include <linux/dmaengine.h>
20#include <linux/dma-mapping.h>
21#include <linux/io.h>
22#include <linux/iopoll.h>
23#include <linux/irq.h>
24#include <linux/module.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020025#include <linux/of.h>
26#include <linux/of_platform.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020027#include <linux/platform_device.h>
28#include <linux/pm_runtime.h>
Fabrice Gasnier270e5a72017-07-13 15:08:30 +000029#include <linux/pm_wakeirq.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020030#include <linux/serial_core.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020031#include <linux/serial.h>
32#include <linux/spinlock.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
35#include <linux/tty.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020036
Alexandre TORGUEbc5a0b52016-09-15 18:42:35 +020037#include "stm32-usart.h"
Maxime Coquelin48a60922015-06-10 21:19:36 +020038
39static void stm32_stop_tx(struct uart_port *port);
Alexandre TORGUE34891872016-09-15 18:42:40 +020040static void stm32_transmit_chars(struct uart_port *port);
Maxime Coquelin48a60922015-06-10 21:19:36 +020041
42static inline struct stm32_port *to_stm32_port(struct uart_port *port)
43{
44 return container_of(port, struct stm32_port, port);
45}
46
47static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
48{
49 u32 val;
50
51 val = readl_relaxed(port->membase + reg);
52 val |= bits;
53 writel_relaxed(val, port->membase + reg);
54}
55
56static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
57{
58 u32 val;
59
60 val = readl_relaxed(port->membase + reg);
61 val &= ~bits;
62 writel_relaxed(val, port->membase + reg);
63}
64
Bich HEMON1bcda092018-03-12 09:50:05 +000065static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
66 u32 delay_DDE, u32 baud)
67{
68 u32 rs485_deat_dedt;
69 u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
70 bool over8;
71
72 *cr3 |= USART_CR3_DEM;
73 over8 = *cr1 & USART_CR1_OVER8;
74
75 if (over8)
76 rs485_deat_dedt = delay_ADE * baud * 8;
77 else
78 rs485_deat_dedt = delay_ADE * baud * 16;
79
80 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
81 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
82 rs485_deat_dedt_max : rs485_deat_dedt;
83 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
84 USART_CR1_DEAT_MASK;
85 *cr1 |= rs485_deat_dedt;
86
87 if (over8)
88 rs485_deat_dedt = delay_DDE * baud * 8;
89 else
90 rs485_deat_dedt = delay_DDE * baud * 16;
91
92 rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
93 rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
94 rs485_deat_dedt_max : rs485_deat_dedt;
95 rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
96 USART_CR1_DEDT_MASK;
97 *cr1 |= rs485_deat_dedt;
98}
99
100static int stm32_config_rs485(struct uart_port *port,
101 struct serial_rs485 *rs485conf)
102{
103 struct stm32_port *stm32_port = to_stm32_port(port);
104 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
105 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
106 u32 usartdiv, baud, cr1, cr3;
107 bool over8;
108 unsigned long flags;
109
110 spin_lock_irqsave(&port->lock, flags);
111 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
112
113 port->rs485 = *rs485conf;
114
115 rs485conf->flags |= SER_RS485_RX_DURING_TX;
116
117 if (rs485conf->flags & SER_RS485_ENABLED) {
118 cr1 = readl_relaxed(port->membase + ofs->cr1);
119 cr3 = readl_relaxed(port->membase + ofs->cr3);
120 usartdiv = readl_relaxed(port->membase + ofs->brr);
121 usartdiv = usartdiv & GENMASK(15, 0);
122 over8 = cr1 & USART_CR1_OVER8;
123
124 if (over8)
125 usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
126 << USART_BRR_04_R_SHIFT;
127
128 baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
129 stm32_config_reg_rs485(&cr1, &cr3,
130 rs485conf->delay_rts_before_send,
131 rs485conf->delay_rts_after_send, baud);
132
133 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
134 cr3 &= ~USART_CR3_DEP;
135 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
136 } else {
137 cr3 |= USART_CR3_DEP;
138 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
139 }
140
141 writel_relaxed(cr3, port->membase + ofs->cr3);
142 writel_relaxed(cr1, port->membase + ofs->cr1);
143 } else {
144 stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP);
145 stm32_clr_bits(port, ofs->cr1,
146 USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
147 }
148
149 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
150 spin_unlock_irqrestore(&port->lock, flags);
151
152 return 0;
153}
154
155static int stm32_init_rs485(struct uart_port *port,
156 struct platform_device *pdev)
157{
158 struct serial_rs485 *rs485conf = &port->rs485;
159
160 rs485conf->flags = 0;
161 rs485conf->delay_rts_before_send = 0;
162 rs485conf->delay_rts_after_send = 0;
163
164 if (!pdev->dev.of_node)
165 return -ENODEV;
166
167 uart_get_rs485_mode(&pdev->dev, rs485conf);
168
169 return 0;
170}
171
Baoyou Xieb97055b2016-09-26 19:58:56 +0800172static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
173 bool threaded)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200174{
175 struct stm32_port *stm32_port = to_stm32_port(port);
176 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
177 enum dma_status status;
178 struct dma_tx_state state;
179
180 *sr = readl_relaxed(port->membase + ofs->isr);
181
182 if (threaded && stm32_port->rx_ch) {
183 status = dmaengine_tx_status(stm32_port->rx_ch,
184 stm32_port->rx_ch->cookie,
185 &state);
186 if ((status == DMA_IN_PROGRESS) &&
187 (*last_res != state.residue))
188 return 1;
189 else
190 return 0;
191 } else if (*sr & USART_SR_RXNE) {
192 return 1;
193 }
194 return 0;
195}
196
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200197static unsigned long stm32_get_char(struct uart_port *port, u32 *sr,
198 int *last_res)
Alexandre TORGUE34891872016-09-15 18:42:40 +0200199{
200 struct stm32_port *stm32_port = to_stm32_port(port);
201 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
202 unsigned long c;
203
204 if (stm32_port->rx_ch) {
205 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
206 if ((*last_res) == 0)
207 *last_res = RX_BUF_L;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200208 } else {
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200209 c = readl_relaxed(port->membase + ofs->rdr);
210 /* apply RDR data mask */
211 c &= stm32_port->rdr_mask;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200212 }
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200213
214 return c;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200215}
216
217static void stm32_receive_chars(struct uart_port *port, bool threaded)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200218{
219 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200220 struct stm32_port *stm32_port = to_stm32_port(port);
221 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200222 unsigned long c;
223 u32 sr;
224 char flag;
225
Andy Shevchenko29d60982017-08-13 17:47:41 +0300226 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200227 pm_wakeup_event(tport->tty->dev, 0);
228
Gerald Baezae5707912017-07-13 15:08:27 +0000229 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200230 sr |= USART_SR_DUMMY_RX;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200231 flag = TTY_NORMAL;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200232
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200233 /*
234 * Status bits has to be cleared before reading the RDR:
235 * In FIFO mode, reading the RDR will pop the next data
236 * (if any) along with its status bits into the SR.
237 * Not doing so leads to misalignement between RDR and SR,
238 * and clear status bits of the next rx data.
239 *
240 * Clear errors flags for stm32f7 and stm32h7 compatible
241 * devices. On stm32f4 compatible devices, the error bit is
242 * cleared by the sequence [read SR - read DR].
243 */
244 if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
245 stm32_clr_bits(port, ofs->icr, USART_ICR_ORECF |
246 USART_ICR_PECF | USART_ICR_FECF);
247
248 c = stm32_get_char(port, &sr, &stm32_port->last_res);
249 port->icount.rx++;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200250 if (sr & USART_SR_ERR_MASK) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200251 if (sr & USART_SR_ORE) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200252 port->icount.overrun++;
253 } else if (sr & USART_SR_PE) {
254 port->icount.parity++;
255 } else if (sr & USART_SR_FE) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200256 /* Break detection if character is null */
257 if (!c) {
258 port->icount.brk++;
259 if (uart_handle_break(port))
260 continue;
261 } else {
262 port->icount.frame++;
263 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200264 }
265
266 sr &= port->read_status_mask;
267
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200268 if (sr & USART_SR_PE) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200269 flag = TTY_PARITY;
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200270 } else if (sr & USART_SR_FE) {
271 if (!c)
272 flag = TTY_BREAK;
273 else
274 flag = TTY_FRAME;
275 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200276 }
277
278 if (uart_handle_sysrq_char(port, c))
279 continue;
280 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
281 }
282
283 spin_unlock(&port->lock);
284 tty_flip_buffer_push(tport);
285 spin_lock(&port->lock);
286}
287
Alexandre TORGUE34891872016-09-15 18:42:40 +0200288static void stm32_tx_dma_complete(void *arg)
289{
290 struct uart_port *port = arg;
291 struct stm32_port *stm32port = to_stm32_port(port);
292 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200293
294 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
295 stm32port->tx_dma_busy = false;
296
297 /* Let's see if we have pending data to send */
298 stm32_transmit_chars(port);
299}
300
301static void stm32_transmit_chars_pio(struct uart_port *port)
302{
303 struct stm32_port *stm32_port = to_stm32_port(port);
304 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
305 struct circ_buf *xmit = &port->state->xmit;
306 unsigned int isr;
307 int ret;
308
309 if (stm32_port->tx_dma_busy) {
310 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
311 stm32_port->tx_dma_busy = false;
312 }
313
314 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
315 isr,
316 (isr & USART_SR_TXE),
Gerald Baezaa61d9e62017-07-31 09:31:52 +0000317 10, 100000);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200318
319 if (ret)
320 dev_err(port->dev, "tx empty not set\n");
321
322 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
323
324 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
325 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
326 port->icount.tx++;
327}
328
329static void stm32_transmit_chars_dma(struct uart_port *port)
330{
331 struct stm32_port *stm32port = to_stm32_port(port);
332 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
333 struct circ_buf *xmit = &port->state->xmit;
334 struct dma_async_tx_descriptor *desc = NULL;
335 dma_cookie_t cookie;
336 unsigned int count, i;
337
338 if (stm32port->tx_dma_busy)
339 return;
340
341 stm32port->tx_dma_busy = true;
342
343 count = uart_circ_chars_pending(xmit);
344
345 if (count > TX_BUF_L)
346 count = TX_BUF_L;
347
348 if (xmit->tail < xmit->head) {
349 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
350 } else {
351 size_t one = UART_XMIT_SIZE - xmit->tail;
352 size_t two;
353
354 if (one > count)
355 one = count;
356 two = count - one;
357
358 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
359 if (two)
360 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
361 }
362
363 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
364 stm32port->tx_dma_buf,
365 count,
366 DMA_MEM_TO_DEV,
367 DMA_PREP_INTERRUPT);
368
369 if (!desc) {
370 for (i = count; i > 0; i--)
371 stm32_transmit_chars_pio(port);
372 return;
373 }
374
375 desc->callback = stm32_tx_dma_complete;
376 desc->callback_param = port;
377
378 /* Push current DMA TX transaction in the pending queue */
379 cookie = dmaengine_submit(desc);
380
381 /* Issue pending DMA TX requests */
382 dma_async_issue_pending(stm32port->tx_ch);
383
Alexandre TORGUE34891872016-09-15 18:42:40 +0200384 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
385
386 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
387 port->icount.tx += count;
388}
389
Maxime Coquelin48a60922015-06-10 21:19:36 +0200390static void stm32_transmit_chars(struct uart_port *port)
391{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200392 struct stm32_port *stm32_port = to_stm32_port(port);
393 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200394 struct circ_buf *xmit = &port->state->xmit;
395
396 if (port->x_char) {
Alexandre TORGUE34891872016-09-15 18:42:40 +0200397 if (stm32_port->tx_dma_busy)
398 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200399 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200400 port->x_char = 0;
401 port->icount.tx++;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200402 if (stm32_port->tx_dma_busy)
403 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200404 return;
405 }
406
Erwan Le Rayb83b9572019-05-21 17:45:44 +0200407 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
408 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200409 return;
410 }
411
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200412 if (ofs->icr == UNDEF_REG)
413 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
414 else
415 stm32_set_bits(port, ofs->icr, USART_ICR_TCCF);
416
Alexandre TORGUE34891872016-09-15 18:42:40 +0200417 if (stm32_port->tx_ch)
418 stm32_transmit_chars_dma(port);
419 else
420 stm32_transmit_chars_pio(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200421
422 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
423 uart_write_wakeup(port);
424
425 if (uart_circ_empty(xmit))
Erwan Le Rayb83b9572019-05-21 17:45:44 +0200426 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200427}
428
429static irqreturn_t stm32_interrupt(int irq, void *ptr)
430{
431 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200432 struct stm32_port *stm32_port = to_stm32_port(port);
433 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200434 u32 sr;
435
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200436 spin_lock(&port->lock);
437
Alexandre TORGUEada86182016-09-15 18:42:33 +0200438 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200439
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000440 if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
441 writel_relaxed(USART_ICR_WUCF,
442 port->membase + ofs->icr);
443
Alexandre TORGUE34891872016-09-15 18:42:40 +0200444 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
445 stm32_receive_chars(port, false);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200446
Alexandre TORGUE34891872016-09-15 18:42:40 +0200447 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200448 stm32_transmit_chars(port);
449
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200450 spin_unlock(&port->lock);
451
Alexandre TORGUE34891872016-09-15 18:42:40 +0200452 if (stm32_port->rx_ch)
453 return IRQ_WAKE_THREAD;
454 else
455 return IRQ_HANDLED;
456}
457
458static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
459{
460 struct uart_port *port = ptr;
461 struct stm32_port *stm32_port = to_stm32_port(port);
462
463 spin_lock(&port->lock);
464
465 if (stm32_port->rx_ch)
466 stm32_receive_chars(port, true);
467
Maxime Coquelin48a60922015-06-10 21:19:36 +0200468 spin_unlock(&port->lock);
469
470 return IRQ_HANDLED;
471}
472
473static unsigned int stm32_tx_empty(struct uart_port *port)
474{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200475 struct stm32_port *stm32_port = to_stm32_port(port);
476 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
477
478 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200479}
480
481static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
482{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200483 struct stm32_port *stm32_port = to_stm32_port(port);
484 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
485
Maxime Coquelin48a60922015-06-10 21:19:36 +0200486 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Alexandre TORGUEada86182016-09-15 18:42:33 +0200487 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200488 else
Alexandre TORGUEada86182016-09-15 18:42:33 +0200489 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200490}
491
492static unsigned int stm32_get_mctrl(struct uart_port *port)
493{
494 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
495 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
496}
497
498/* Transmit stop */
499static void stm32_stop_tx(struct uart_port *port)
500{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200501 struct stm32_port *stm32_port = to_stm32_port(port);
502 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
503
504 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200505}
506
507/* There are probably characters waiting to be transmitted. */
508static void stm32_start_tx(struct uart_port *port)
509{
510 struct circ_buf *xmit = &port->state->xmit;
511
512 if (uart_circ_empty(xmit))
513 return;
514
Alexandre TORGUE34891872016-09-15 18:42:40 +0200515 stm32_transmit_chars(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200516}
517
518/* Throttle the remote when input buffer is about to overflow. */
519static void stm32_throttle(struct uart_port *port)
520{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200521 struct stm32_port *stm32_port = to_stm32_port(port);
522 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200523 unsigned long flags;
524
525 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200526 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200527 spin_unlock_irqrestore(&port->lock, flags);
528}
529
530/* Unthrottle the remote, the input buffer can now accept data. */
531static void stm32_unthrottle(struct uart_port *port)
532{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200533 struct stm32_port *stm32_port = to_stm32_port(port);
534 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200535 unsigned long flags;
536
537 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200538 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200539 spin_unlock_irqrestore(&port->lock, flags);
540}
541
542/* Receive stop */
543static void stm32_stop_rx(struct uart_port *port)
544{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200545 struct stm32_port *stm32_port = to_stm32_port(port);
546 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
547
548 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200549}
550
551/* Handle breaks - ignored by us */
552static void stm32_break_ctl(struct uart_port *port, int break_state)
553{
554}
555
556static int stm32_startup(struct uart_port *port)
557{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200558 struct stm32_port *stm32_port = to_stm32_port(port);
559 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200560 const char *name = to_platform_device(port->dev)->name;
561 u32 val;
562 int ret;
563
Alexandre TORGUE34891872016-09-15 18:42:40 +0200564 ret = request_threaded_irq(port->irq, stm32_interrupt,
565 stm32_threaded_interrupt,
566 IRQF_NO_SUSPEND, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200567 if (ret)
568 return ret;
569
570 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Gerald Baeza351a7622017-07-13 15:08:30 +0000571 if (stm32_port->fifoen)
572 val |= USART_CR1_FIFOEN;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200573 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200574
575 return 0;
576}
577
578static void stm32_shutdown(struct uart_port *port)
579{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200580 struct stm32_port *stm32_port = to_stm32_port(port);
581 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200582 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200583 u32 val, isr;
584 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200585
586 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200587 val |= BIT(cfg->uart_enable_bit);
Gerald Baeza351a7622017-07-13 15:08:30 +0000588 if (stm32_port->fifoen)
589 val |= USART_CR1_FIFOEN;
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200590
591 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
592 isr, (isr & USART_SR_TC),
593 10, 100000);
594
595 if (ret)
596 dev_err(port->dev, "transmission complete not set\n");
597
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200598 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200599
600 free_irq(port->irq, port);
601}
602
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200603unsigned int stm32_get_databits(struct ktermios *termios)
604{
605 unsigned int bits;
606
607 tcflag_t cflag = termios->c_cflag;
608
609 switch (cflag & CSIZE) {
610 /*
611 * CSIZE settings are not necessarily supported in hardware.
612 * CSIZE unsupported configurations are handled here to set word length
613 * to 8 bits word as default configuration and to print debug message.
614 */
615 case CS5:
616 bits = 5;
617 break;
618 case CS6:
619 bits = 6;
620 break;
621 case CS7:
622 bits = 7;
623 break;
624 /* default including CS8 */
625 default:
626 bits = 8;
627 break;
628 }
629
630 return bits;
631}
632
Maxime Coquelin48a60922015-06-10 21:19:36 +0200633static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
634 struct ktermios *old)
635{
636 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200637 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
638 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Bich HEMON1bcda092018-03-12 09:50:05 +0000639 struct serial_rs485 *rs485conf = &port->rs485;
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200640 unsigned int baud, bits;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200641 u32 usartdiv, mantissa, fraction, oversampling;
642 tcflag_t cflag = termios->c_cflag;
643 u32 cr1, cr2, cr3;
644 unsigned long flags;
645
646 if (!stm32_port->hw_flow_control)
647 cflag &= ~CRTSCTS;
648
649 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
650
651 spin_lock_irqsave(&port->lock, flags);
652
653 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200654 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200655
Alexandre TORGUEada86182016-09-15 18:42:33 +0200656 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
Bich HEMON1bcda092018-03-12 09:50:05 +0000657
Gerald Baeza351a7622017-07-13 15:08:30 +0000658 if (stm32_port->fifoen)
659 cr1 |= USART_CR1_FIFOEN;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200660 cr2 = 0;
661 cr3 = 0;
662
663 if (cflag & CSTOPB)
664 cr2 |= USART_CR2_STOP_2B;
665
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200666 bits = stm32_get_databits(termios);
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200667 stm32_port->rdr_mask = (BIT(bits) - 1);
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200668
Maxime Coquelin48a60922015-06-10 21:19:36 +0200669 if (cflag & PARENB) {
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200670 bits++;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200671 cr1 |= USART_CR1_PCE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200672 }
673
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200674 /*
675 * Word length configuration:
676 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
677 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
678 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
679 * M0 and M1 already cleared by cr1 initialization.
680 */
681 if (bits == 9)
682 cr1 |= USART_CR1_M0;
683 else if ((bits == 7) && cfg->has_7bits_data)
684 cr1 |= USART_CR1_M1;
685 else if (bits != 8)
686 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
687 , bits);
688
Maxime Coquelin48a60922015-06-10 21:19:36 +0200689 if (cflag & PARODD)
690 cr1 |= USART_CR1_PS;
691
692 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
693 if (cflag & CRTSCTS) {
694 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Bich HEMON35abe982017-07-13 15:08:28 +0000695 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200696 }
697
698 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
699
700 /*
701 * The USART supports 16 or 8 times oversampling.
702 * By default we prefer 16 times oversampling, so that the receiver
703 * has a better tolerance to clock deviations.
704 * 8 times oversampling is only used to achieve higher speeds.
705 */
706 if (usartdiv < 16) {
707 oversampling = 8;
Bich HEMON1bcda092018-03-12 09:50:05 +0000708 cr1 |= USART_CR1_OVER8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200709 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200710 } else {
711 oversampling = 16;
Bich HEMON1bcda092018-03-12 09:50:05 +0000712 cr1 &= ~USART_CR1_OVER8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200713 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200714 }
715
716 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
717 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200718 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200719
720 uart_update_timeout(port, cflag, baud);
721
722 port->read_status_mask = USART_SR_ORE;
723 if (termios->c_iflag & INPCK)
724 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
725 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200726 port->read_status_mask |= USART_SR_FE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200727
728 /* Characters to ignore */
729 port->ignore_status_mask = 0;
730 if (termios->c_iflag & IGNPAR)
731 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
732 if (termios->c_iflag & IGNBRK) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200733 port->ignore_status_mask |= USART_SR_FE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200734 /*
735 * If we're ignoring parity and break indicators,
736 * ignore overruns too (for real raw support).
737 */
738 if (termios->c_iflag & IGNPAR)
739 port->ignore_status_mask |= USART_SR_ORE;
740 }
741
742 /* Ignore all characters if CREAD is not set */
743 if ((termios->c_cflag & CREAD) == 0)
744 port->ignore_status_mask |= USART_SR_DUMMY_RX;
745
Alexandre TORGUE34891872016-09-15 18:42:40 +0200746 if (stm32_port->rx_ch)
747 cr3 |= USART_CR3_DMAR;
748
Bich HEMON1bcda092018-03-12 09:50:05 +0000749 if (rs485conf->flags & SER_RS485_ENABLED) {
750 stm32_config_reg_rs485(&cr1, &cr3,
751 rs485conf->delay_rts_before_send,
752 rs485conf->delay_rts_after_send, baud);
753 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
754 cr3 &= ~USART_CR3_DEP;
755 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
756 } else {
757 cr3 |= USART_CR3_DEP;
758 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
759 }
760
761 } else {
762 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
763 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
764 }
765
Alexandre TORGUEada86182016-09-15 18:42:33 +0200766 writel_relaxed(cr3, port->membase + ofs->cr3);
767 writel_relaxed(cr2, port->membase + ofs->cr2);
768 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200769
Bich HEMON1bcda092018-03-12 09:50:05 +0000770 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200771 spin_unlock_irqrestore(&port->lock, flags);
772}
773
774static const char *stm32_type(struct uart_port *port)
775{
776 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
777}
778
779static void stm32_release_port(struct uart_port *port)
780{
781}
782
783static int stm32_request_port(struct uart_port *port)
784{
785 return 0;
786}
787
788static void stm32_config_port(struct uart_port *port, int flags)
789{
790 if (flags & UART_CONFIG_TYPE)
791 port->type = PORT_STM32;
792}
793
794static int
795stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
796{
797 /* No user changeable parameters */
798 return -EINVAL;
799}
800
801static void stm32_pm(struct uart_port *port, unsigned int state,
802 unsigned int oldstate)
803{
804 struct stm32_port *stm32port = container_of(port,
805 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200806 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
807 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200808 unsigned long flags = 0;
809
810 switch (state) {
811 case UART_PM_STATE_ON:
812 clk_prepare_enable(stm32port->clk);
813 break;
814 case UART_PM_STATE_OFF:
815 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200816 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200817 spin_unlock_irqrestore(&port->lock, flags);
818 clk_disable_unprepare(stm32port->clk);
819 break;
820 }
821}
822
823static const struct uart_ops stm32_uart_ops = {
824 .tx_empty = stm32_tx_empty,
825 .set_mctrl = stm32_set_mctrl,
826 .get_mctrl = stm32_get_mctrl,
827 .stop_tx = stm32_stop_tx,
828 .start_tx = stm32_start_tx,
829 .throttle = stm32_throttle,
830 .unthrottle = stm32_unthrottle,
831 .stop_rx = stm32_stop_rx,
832 .break_ctl = stm32_break_ctl,
833 .startup = stm32_startup,
834 .shutdown = stm32_shutdown,
835 .set_termios = stm32_set_termios,
836 .pm = stm32_pm,
837 .type = stm32_type,
838 .release_port = stm32_release_port,
839 .request_port = stm32_request_port,
840 .config_port = stm32_config_port,
841 .verify_port = stm32_verify_port,
842};
843
844static int stm32_init_port(struct stm32_port *stm32port,
845 struct platform_device *pdev)
846{
847 struct uart_port *port = &stm32port->port;
848 struct resource *res;
849 int ret;
850
851 port->iotype = UPIO_MEM;
852 port->flags = UPF_BOOT_AUTOCONF;
853 port->ops = &stm32_uart_ops;
854 port->dev = &pdev->dev;
855 port->irq = platform_get_irq(pdev, 0);
Bich HEMON7d8f6862018-03-15 08:44:46 +0000856 port->rs485_config = stm32_config_rs485;
857
858 stm32_init_rs485(port, pdev);
859
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000860 stm32port->wakeirq = platform_get_irq(pdev, 1);
Gerald Baeza351a7622017-07-13 15:08:30 +0000861 stm32port->fifoen = stm32port->info->cfg.has_fifo;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200862
863 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
864 port->membase = devm_ioremap_resource(&pdev->dev, res);
865 if (IS_ERR(port->membase))
866 return PTR_ERR(port->membase);
867 port->mapbase = res->start;
868
869 spin_lock_init(&port->lock);
870
871 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
872 if (IS_ERR(stm32port->clk))
873 return PTR_ERR(stm32port->clk);
874
875 /* Ensure that clk rate is correct by enabling the clk */
876 ret = clk_prepare_enable(stm32port->clk);
877 if (ret)
878 return ret;
879
880 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
Fabrice Gasnierada80042017-07-13 15:08:29 +0000881 if (!stm32port->port.uartclk) {
882 clk_disable_unprepare(stm32port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200883 ret = -EINVAL;
Fabrice Gasnierada80042017-07-13 15:08:29 +0000884 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200885
Maxime Coquelin48a60922015-06-10 21:19:36 +0200886 return ret;
887}
888
889static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
890{
891 struct device_node *np = pdev->dev.of_node;
892 int id;
893
894 if (!np)
895 return NULL;
896
897 id = of_alias_get_id(np, "serial");
Gerald Baezae5707912017-07-13 15:08:27 +0000898 if (id < 0) {
899 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
900 return NULL;
901 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200902
903 if (WARN_ON(id >= STM32_MAX_PORTS))
904 return NULL;
905
906 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200907 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200908 stm32_ports[id].port.line = id;
Gerald Baezae5707912017-07-13 15:08:27 +0000909 stm32_ports[id].last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200910 return &stm32_ports[id];
911}
912
913#ifdef CONFIG_OF
914static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200915 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
Alexandre TORGUEada86182016-09-15 18:42:33 +0200916 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000917 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200918 {},
919};
920
921MODULE_DEVICE_TABLE(of, stm32_match);
922#endif
923
Alexandre TORGUE34891872016-09-15 18:42:40 +0200924static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
925 struct platform_device *pdev)
926{
927 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
928 struct uart_port *port = &stm32port->port;
929 struct device *dev = &pdev->dev;
930 struct dma_slave_config config;
931 struct dma_async_tx_descriptor *desc = NULL;
932 dma_cookie_t cookie;
933 int ret;
934
935 /* Request DMA RX channel */
936 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
937 if (!stm32port->rx_ch) {
938 dev_info(dev, "rx dma alloc failed\n");
939 return -ENODEV;
940 }
941 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
942 &stm32port->rx_dma_buf,
943 GFP_KERNEL);
944 if (!stm32port->rx_buf) {
945 ret = -ENOMEM;
946 goto alloc_err;
947 }
948
949 /* Configure DMA channel */
950 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200951 config.src_addr = port->mapbase + ofs->rdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200952 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
953
954 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
955 if (ret < 0) {
956 dev_err(dev, "rx dma channel config failed\n");
957 ret = -ENODEV;
958 goto config_err;
959 }
960
961 /* Prepare a DMA cyclic transaction */
962 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
963 stm32port->rx_dma_buf,
964 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
965 DMA_PREP_INTERRUPT);
966 if (!desc) {
967 dev_err(dev, "rx dma prep cyclic failed\n");
968 ret = -ENODEV;
969 goto config_err;
970 }
971
972 /* No callback as dma buffer is drained on usart interrupt */
973 desc->callback = NULL;
974 desc->callback_param = NULL;
975
976 /* Push current DMA transaction in the pending queue */
977 cookie = dmaengine_submit(desc);
978
979 /* Issue pending DMA requests */
980 dma_async_issue_pending(stm32port->rx_ch);
981
982 return 0;
983
984config_err:
985 dma_free_coherent(&pdev->dev,
986 RX_BUF_L, stm32port->rx_buf,
987 stm32port->rx_dma_buf);
988
989alloc_err:
990 dma_release_channel(stm32port->rx_ch);
991 stm32port->rx_ch = NULL;
992
993 return ret;
994}
995
996static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
997 struct platform_device *pdev)
998{
999 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1000 struct uart_port *port = &stm32port->port;
1001 struct device *dev = &pdev->dev;
1002 struct dma_slave_config config;
1003 int ret;
1004
1005 stm32port->tx_dma_busy = false;
1006
1007 /* Request DMA TX channel */
1008 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
1009 if (!stm32port->tx_ch) {
1010 dev_info(dev, "tx dma alloc failed\n");
1011 return -ENODEV;
1012 }
1013 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
1014 &stm32port->tx_dma_buf,
1015 GFP_KERNEL);
1016 if (!stm32port->tx_buf) {
1017 ret = -ENOMEM;
1018 goto alloc_err;
1019 }
1020
1021 /* Configure DMA channel */
1022 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +02001023 config.dst_addr = port->mapbase + ofs->tdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001024 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1025
1026 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1027 if (ret < 0) {
1028 dev_err(dev, "tx dma channel config failed\n");
1029 ret = -ENODEV;
1030 goto config_err;
1031 }
1032
1033 return 0;
1034
1035config_err:
1036 dma_free_coherent(&pdev->dev,
1037 TX_BUF_L, stm32port->tx_buf,
1038 stm32port->tx_dma_buf);
1039
1040alloc_err:
1041 dma_release_channel(stm32port->tx_ch);
1042 stm32port->tx_ch = NULL;
1043
1044 return ret;
1045}
1046
Maxime Coquelin48a60922015-06-10 21:19:36 +02001047static int stm32_serial_probe(struct platform_device *pdev)
1048{
Alexandre TORGUEada86182016-09-15 18:42:33 +02001049 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001050 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001051 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001052
1053 stm32port = stm32_of_get_stm32_port(pdev);
1054 if (!stm32port)
1055 return -ENODEV;
1056
Alexandre TORGUEada86182016-09-15 18:42:33 +02001057 match = of_match_device(stm32_match, &pdev->dev);
1058 if (match && match->data)
1059 stm32port->info = (struct stm32_usart_info *)match->data;
1060 else
1061 return -EINVAL;
1062
Maxime Coquelin48a60922015-06-10 21:19:36 +02001063 ret = stm32_init_port(stm32port, pdev);
1064 if (ret)
1065 return ret;
1066
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001067 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) {
1068 ret = device_init_wakeup(&pdev->dev, true);
1069 if (ret)
1070 goto err_uninit;
Erwan Le Ray5297f272019-05-21 17:45:46 +02001071
1072 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1073 stm32port->wakeirq);
1074 if (ret)
1075 goto err_nowup;
1076
1077 device_set_wakeup_enable(&pdev->dev, false);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001078 }
1079
Maxime Coquelin48a60922015-06-10 21:19:36 +02001080 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1081 if (ret)
Erwan Le Ray5297f272019-05-21 17:45:46 +02001082 goto err_wirq;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001083
Alexandre TORGUE34891872016-09-15 18:42:40 +02001084 ret = stm32_of_dma_rx_probe(stm32port, pdev);
1085 if (ret)
1086 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
1087
1088 ret = stm32_of_dma_tx_probe(stm32port, pdev);
1089 if (ret)
1090 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
1091
Maxime Coquelin48a60922015-06-10 21:19:36 +02001092 platform_set_drvdata(pdev, &stm32port->port);
1093
1094 return 0;
Fabrice Gasnierada80042017-07-13 15:08:29 +00001095
Erwan Le Ray5297f272019-05-21 17:45:46 +02001096err_wirq:
1097 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
1098 dev_pm_clear_wake_irq(&pdev->dev);
1099
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001100err_nowup:
1101 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
1102 device_init_wakeup(&pdev->dev, false);
1103
Fabrice Gasnierada80042017-07-13 15:08:29 +00001104err_uninit:
1105 clk_disable_unprepare(stm32port->clk);
1106
1107 return ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001108}
1109
1110static int stm32_serial_remove(struct platform_device *pdev)
1111{
1112 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +02001113 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +02001114 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001115 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001116
1117 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
1118
1119 if (stm32_port->rx_ch)
1120 dma_release_channel(stm32_port->rx_ch);
1121
1122 if (stm32_port->rx_dma_buf)
1123 dma_free_coherent(&pdev->dev,
1124 RX_BUF_L, stm32_port->rx_buf,
1125 stm32_port->rx_dma_buf);
1126
1127 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1128
1129 if (stm32_port->tx_ch)
1130 dma_release_channel(stm32_port->tx_ch);
1131
1132 if (stm32_port->tx_dma_buf)
1133 dma_free_coherent(&pdev->dev,
1134 TX_BUF_L, stm32_port->tx_buf,
1135 stm32_port->tx_dma_buf);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +02001136
Erwan Le Ray5297f272019-05-21 17:45:46 +02001137 if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
1138 dev_pm_clear_wake_irq(&pdev->dev);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001139 device_init_wakeup(&pdev->dev, false);
Erwan Le Ray5297f272019-05-21 17:45:46 +02001140 }
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001141
Alexandre TORGUE511c7b12016-09-15 18:42:38 +02001142 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001143
1144 return uart_remove_one_port(&stm32_usart_driver, port);
1145}
1146
1147
1148#ifdef CONFIG_SERIAL_STM32_CONSOLE
1149static void stm32_console_putchar(struct uart_port *port, int ch)
1150{
Alexandre TORGUEada86182016-09-15 18:42:33 +02001151 struct stm32_port *stm32_port = to_stm32_port(port);
1152 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1153
1154 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +02001155 cpu_relax();
1156
Alexandre TORGUEada86182016-09-15 18:42:33 +02001157 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001158}
1159
1160static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
1161{
1162 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001163 struct stm32_port *stm32_port = to_stm32_port(port);
1164 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001165 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001166 unsigned long flags;
1167 u32 old_cr1, new_cr1;
1168 int locked = 1;
1169
1170 local_irq_save(flags);
1171 if (port->sysrq)
1172 locked = 0;
1173 else if (oops_in_progress)
1174 locked = spin_trylock(&port->lock);
1175 else
1176 spin_lock(&port->lock);
1177
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001178 /* Save and disable interrupts, enable the transmitter */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001179 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001180 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001181 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
Alexandre TORGUEada86182016-09-15 18:42:33 +02001182 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001183
1184 uart_console_write(port, s, cnt, stm32_console_putchar);
1185
1186 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001187 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001188
1189 if (locked)
1190 spin_unlock(&port->lock);
1191 local_irq_restore(flags);
1192}
1193
1194static int stm32_console_setup(struct console *co, char *options)
1195{
1196 struct stm32_port *stm32port;
1197 int baud = 9600;
1198 int bits = 8;
1199 int parity = 'n';
1200 int flow = 'n';
1201
1202 if (co->index >= STM32_MAX_PORTS)
1203 return -ENODEV;
1204
1205 stm32port = &stm32_ports[co->index];
1206
1207 /*
1208 * This driver does not support early console initialization
1209 * (use ARM early printk support instead), so we only expect
1210 * this to be called during the uart port registration when the
1211 * driver gets probed and the port should be mapped at that point.
1212 */
1213 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1214 return -ENXIO;
1215
1216 if (options)
1217 uart_parse_options(options, &baud, &parity, &bits, &flow);
1218
1219 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1220}
1221
1222static struct console stm32_console = {
1223 .name = STM32_SERIAL_NAME,
1224 .device = uart_console_device,
1225 .write = stm32_console_write,
1226 .setup = stm32_console_setup,
1227 .flags = CON_PRINTBUFFER,
1228 .index = -1,
1229 .data = &stm32_usart_driver,
1230};
1231
1232#define STM32_SERIAL_CONSOLE (&stm32_console)
1233
1234#else
1235#define STM32_SERIAL_CONSOLE NULL
1236#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1237
1238static struct uart_driver stm32_usart_driver = {
1239 .driver_name = DRIVER_NAME,
1240 .dev_name = STM32_SERIAL_NAME,
1241 .major = 0,
1242 .minor = 0,
1243 .nr = STM32_MAX_PORTS,
1244 .cons = STM32_SERIAL_CONSOLE,
1245};
1246
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001247#ifdef CONFIG_PM_SLEEP
1248static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable)
1249{
1250 struct stm32_port *stm32_port = to_stm32_port(port);
1251 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1252 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1253 u32 val;
1254
1255 if (!cfg->has_wakeup || stm32_port->wakeirq < 0)
1256 return;
1257
1258 if (enable) {
1259 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1260 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1261 val = readl_relaxed(port->membase + ofs->cr3);
1262 val &= ~USART_CR3_WUS_MASK;
1263 /* Enable Wake up interrupt from low power on start bit */
1264 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1265 writel_relaxed(val, port->membase + ofs->cr3);
1266 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1267 } else {
1268 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1269 }
1270}
1271
1272static int stm32_serial_suspend(struct device *dev)
1273{
1274 struct uart_port *port = dev_get_drvdata(dev);
1275
1276 uart_suspend_port(&stm32_usart_driver, port);
1277
1278 if (device_may_wakeup(dev))
1279 stm32_serial_enable_wakeup(port, true);
1280 else
1281 stm32_serial_enable_wakeup(port, false);
1282
1283 return 0;
1284}
1285
1286static int stm32_serial_resume(struct device *dev)
1287{
1288 struct uart_port *port = dev_get_drvdata(dev);
1289
1290 if (device_may_wakeup(dev))
1291 stm32_serial_enable_wakeup(port, false);
1292
1293 return uart_resume_port(&stm32_usart_driver, port);
1294}
1295#endif /* CONFIG_PM_SLEEP */
1296
1297static const struct dev_pm_ops stm32_serial_pm_ops = {
1298 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1299};
1300
Maxime Coquelin48a60922015-06-10 21:19:36 +02001301static struct platform_driver stm32_serial_driver = {
1302 .probe = stm32_serial_probe,
1303 .remove = stm32_serial_remove,
1304 .driver = {
1305 .name = DRIVER_NAME,
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001306 .pm = &stm32_serial_pm_ops,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001307 .of_match_table = of_match_ptr(stm32_match),
1308 },
1309};
1310
1311static int __init usart_init(void)
1312{
1313 static char banner[] __initdata = "STM32 USART driver initialized";
1314 int ret;
1315
1316 pr_info("%s\n", banner);
1317
1318 ret = uart_register_driver(&stm32_usart_driver);
1319 if (ret)
1320 return ret;
1321
1322 ret = platform_driver_register(&stm32_serial_driver);
1323 if (ret)
1324 uart_unregister_driver(&stm32_usart_driver);
1325
1326 return ret;
1327}
1328
1329static void __exit usart_exit(void)
1330{
1331 platform_driver_unregister(&stm32_serial_driver);
1332 uart_unregister_driver(&stm32_usart_driver);
1333}
1334
1335module_init(usart_init);
1336module_exit(usart_exit);
1337
1338MODULE_ALIAS("platform:" DRIVER_NAME);
1339MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1340MODULE_LICENSE("GPL v2");