blob: d603be9669a9691c255cea0b28c61990e8af2645 [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;
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000560 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200561 const char *name = to_platform_device(port->dev)->name;
562 u32 val;
563 int ret;
564
Alexandre TORGUE34891872016-09-15 18:42:40 +0200565 ret = request_threaded_irq(port->irq, stm32_interrupt,
566 stm32_threaded_interrupt,
567 IRQF_NO_SUSPEND, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200568 if (ret)
569 return ret;
570
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000571 if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
572 ret = dev_pm_set_dedicated_wake_irq(port->dev,
573 stm32_port->wakeirq);
574 if (ret) {
575 free_irq(port->irq, port);
576 return ret;
577 }
578 }
579
Maxime Coquelin48a60922015-06-10 21:19:36 +0200580 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Gerald Baeza351a7622017-07-13 15:08:30 +0000581 if (stm32_port->fifoen)
582 val |= USART_CR1_FIFOEN;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200583 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200584
585 return 0;
586}
587
588static void stm32_shutdown(struct uart_port *port)
589{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200590 struct stm32_port *stm32_port = to_stm32_port(port);
591 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200592 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200593 u32 val, isr;
594 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200595
596 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200597 val |= BIT(cfg->uart_enable_bit);
Gerald Baeza351a7622017-07-13 15:08:30 +0000598 if (stm32_port->fifoen)
599 val |= USART_CR1_FIFOEN;
Erwan Le Ray64c32ea2019-05-21 17:45:45 +0200600
601 ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
602 isr, (isr & USART_SR_TC),
603 10, 100000);
604
605 if (ret)
606 dev_err(port->dev, "transmission complete not set\n");
607
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200608 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200609
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000610 dev_pm_clear_wake_irq(port->dev);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200611 free_irq(port->irq, port);
612}
613
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200614unsigned int stm32_get_databits(struct ktermios *termios)
615{
616 unsigned int bits;
617
618 tcflag_t cflag = termios->c_cflag;
619
620 switch (cflag & CSIZE) {
621 /*
622 * CSIZE settings are not necessarily supported in hardware.
623 * CSIZE unsupported configurations are handled here to set word length
624 * to 8 bits word as default configuration and to print debug message.
625 */
626 case CS5:
627 bits = 5;
628 break;
629 case CS6:
630 bits = 6;
631 break;
632 case CS7:
633 bits = 7;
634 break;
635 /* default including CS8 */
636 default:
637 bits = 8;
638 break;
639 }
640
641 return bits;
642}
643
Maxime Coquelin48a60922015-06-10 21:19:36 +0200644static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
645 struct ktermios *old)
646{
647 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200648 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
649 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Bich HEMON1bcda092018-03-12 09:50:05 +0000650 struct serial_rs485 *rs485conf = &port->rs485;
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200651 unsigned int baud, bits;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200652 u32 usartdiv, mantissa, fraction, oversampling;
653 tcflag_t cflag = termios->c_cflag;
654 u32 cr1, cr2, cr3;
655 unsigned long flags;
656
657 if (!stm32_port->hw_flow_control)
658 cflag &= ~CRTSCTS;
659
660 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
661
662 spin_lock_irqsave(&port->lock, flags);
663
664 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200665 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200666
Alexandre TORGUEada86182016-09-15 18:42:33 +0200667 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
Bich HEMON1bcda092018-03-12 09:50:05 +0000668
Gerald Baeza351a7622017-07-13 15:08:30 +0000669 if (stm32_port->fifoen)
670 cr1 |= USART_CR1_FIFOEN;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200671 cr2 = 0;
672 cr3 = 0;
673
674 if (cflag & CSTOPB)
675 cr2 |= USART_CR2_STOP_2B;
676
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200677 bits = stm32_get_databits(termios);
Erwan Le Ray6c5962f2019-05-21 17:45:43 +0200678 stm32_port->rdr_mask = (BIT(bits) - 1);
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200679
Maxime Coquelin48a60922015-06-10 21:19:36 +0200680 if (cflag & PARENB) {
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200681 bits++;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200682 cr1 |= USART_CR1_PCE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200683 }
684
Erwan Le Rayc8a9d042019-05-21 17:45:41 +0200685 /*
686 * Word length configuration:
687 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
688 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
689 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
690 * M0 and M1 already cleared by cr1 initialization.
691 */
692 if (bits == 9)
693 cr1 |= USART_CR1_M0;
694 else if ((bits == 7) && cfg->has_7bits_data)
695 cr1 |= USART_CR1_M1;
696 else if (bits != 8)
697 dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
698 , bits);
699
Maxime Coquelin48a60922015-06-10 21:19:36 +0200700 if (cflag & PARODD)
701 cr1 |= USART_CR1_PS;
702
703 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
704 if (cflag & CRTSCTS) {
705 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Bich HEMON35abe982017-07-13 15:08:28 +0000706 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200707 }
708
709 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
710
711 /*
712 * The USART supports 16 or 8 times oversampling.
713 * By default we prefer 16 times oversampling, so that the receiver
714 * has a better tolerance to clock deviations.
715 * 8 times oversampling is only used to achieve higher speeds.
716 */
717 if (usartdiv < 16) {
718 oversampling = 8;
Bich HEMON1bcda092018-03-12 09:50:05 +0000719 cr1 |= USART_CR1_OVER8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200720 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200721 } else {
722 oversampling = 16;
Bich HEMON1bcda092018-03-12 09:50:05 +0000723 cr1 &= ~USART_CR1_OVER8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200724 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200725 }
726
727 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
728 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200729 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200730
731 uart_update_timeout(port, cflag, baud);
732
733 port->read_status_mask = USART_SR_ORE;
734 if (termios->c_iflag & INPCK)
735 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
736 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200737 port->read_status_mask |= USART_SR_FE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200738
739 /* Characters to ignore */
740 port->ignore_status_mask = 0;
741 if (termios->c_iflag & IGNPAR)
742 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
743 if (termios->c_iflag & IGNBRK) {
Erwan Le Ray4f01d832019-05-21 17:45:42 +0200744 port->ignore_status_mask |= USART_SR_FE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200745 /*
746 * If we're ignoring parity and break indicators,
747 * ignore overruns too (for real raw support).
748 */
749 if (termios->c_iflag & IGNPAR)
750 port->ignore_status_mask |= USART_SR_ORE;
751 }
752
753 /* Ignore all characters if CREAD is not set */
754 if ((termios->c_cflag & CREAD) == 0)
755 port->ignore_status_mask |= USART_SR_DUMMY_RX;
756
Alexandre TORGUE34891872016-09-15 18:42:40 +0200757 if (stm32_port->rx_ch)
758 cr3 |= USART_CR3_DMAR;
759
Bich HEMON1bcda092018-03-12 09:50:05 +0000760 if (rs485conf->flags & SER_RS485_ENABLED) {
761 stm32_config_reg_rs485(&cr1, &cr3,
762 rs485conf->delay_rts_before_send,
763 rs485conf->delay_rts_after_send, baud);
764 if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
765 cr3 &= ~USART_CR3_DEP;
766 rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
767 } else {
768 cr3 |= USART_CR3_DEP;
769 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
770 }
771
772 } else {
773 cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
774 cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
775 }
776
Alexandre TORGUEada86182016-09-15 18:42:33 +0200777 writel_relaxed(cr3, port->membase + ofs->cr3);
778 writel_relaxed(cr2, port->membase + ofs->cr2);
779 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200780
Bich HEMON1bcda092018-03-12 09:50:05 +0000781 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200782 spin_unlock_irqrestore(&port->lock, flags);
783}
784
785static const char *stm32_type(struct uart_port *port)
786{
787 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
788}
789
790static void stm32_release_port(struct uart_port *port)
791{
792}
793
794static int stm32_request_port(struct uart_port *port)
795{
796 return 0;
797}
798
799static void stm32_config_port(struct uart_port *port, int flags)
800{
801 if (flags & UART_CONFIG_TYPE)
802 port->type = PORT_STM32;
803}
804
805static int
806stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
807{
808 /* No user changeable parameters */
809 return -EINVAL;
810}
811
812static void stm32_pm(struct uart_port *port, unsigned int state,
813 unsigned int oldstate)
814{
815 struct stm32_port *stm32port = container_of(port,
816 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200817 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
818 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200819 unsigned long flags = 0;
820
821 switch (state) {
822 case UART_PM_STATE_ON:
823 clk_prepare_enable(stm32port->clk);
824 break;
825 case UART_PM_STATE_OFF:
826 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200827 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200828 spin_unlock_irqrestore(&port->lock, flags);
829 clk_disable_unprepare(stm32port->clk);
830 break;
831 }
832}
833
834static const struct uart_ops stm32_uart_ops = {
835 .tx_empty = stm32_tx_empty,
836 .set_mctrl = stm32_set_mctrl,
837 .get_mctrl = stm32_get_mctrl,
838 .stop_tx = stm32_stop_tx,
839 .start_tx = stm32_start_tx,
840 .throttle = stm32_throttle,
841 .unthrottle = stm32_unthrottle,
842 .stop_rx = stm32_stop_rx,
843 .break_ctl = stm32_break_ctl,
844 .startup = stm32_startup,
845 .shutdown = stm32_shutdown,
846 .set_termios = stm32_set_termios,
847 .pm = stm32_pm,
848 .type = stm32_type,
849 .release_port = stm32_release_port,
850 .request_port = stm32_request_port,
851 .config_port = stm32_config_port,
852 .verify_port = stm32_verify_port,
853};
854
855static int stm32_init_port(struct stm32_port *stm32port,
856 struct platform_device *pdev)
857{
858 struct uart_port *port = &stm32port->port;
859 struct resource *res;
860 int ret;
861
862 port->iotype = UPIO_MEM;
863 port->flags = UPF_BOOT_AUTOCONF;
864 port->ops = &stm32_uart_ops;
865 port->dev = &pdev->dev;
866 port->irq = platform_get_irq(pdev, 0);
Bich HEMON7d8f6862018-03-15 08:44:46 +0000867 port->rs485_config = stm32_config_rs485;
868
869 stm32_init_rs485(port, pdev);
870
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000871 stm32port->wakeirq = platform_get_irq(pdev, 1);
Gerald Baeza351a7622017-07-13 15:08:30 +0000872 stm32port->fifoen = stm32port->info->cfg.has_fifo;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200873
874 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
875 port->membase = devm_ioremap_resource(&pdev->dev, res);
876 if (IS_ERR(port->membase))
877 return PTR_ERR(port->membase);
878 port->mapbase = res->start;
879
880 spin_lock_init(&port->lock);
881
882 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
883 if (IS_ERR(stm32port->clk))
884 return PTR_ERR(stm32port->clk);
885
886 /* Ensure that clk rate is correct by enabling the clk */
887 ret = clk_prepare_enable(stm32port->clk);
888 if (ret)
889 return ret;
890
891 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
Fabrice Gasnierada80042017-07-13 15:08:29 +0000892 if (!stm32port->port.uartclk) {
893 clk_disable_unprepare(stm32port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200894 ret = -EINVAL;
Fabrice Gasnierada80042017-07-13 15:08:29 +0000895 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200896
Maxime Coquelin48a60922015-06-10 21:19:36 +0200897 return ret;
898}
899
900static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
901{
902 struct device_node *np = pdev->dev.of_node;
903 int id;
904
905 if (!np)
906 return NULL;
907
908 id = of_alias_get_id(np, "serial");
Gerald Baezae5707912017-07-13 15:08:27 +0000909 if (id < 0) {
910 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
911 return NULL;
912 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200913
914 if (WARN_ON(id >= STM32_MAX_PORTS))
915 return NULL;
916
917 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200918 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200919 stm32_ports[id].port.line = id;
Gerald Baezae5707912017-07-13 15:08:27 +0000920 stm32_ports[id].last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200921 return &stm32_ports[id];
922}
923
924#ifdef CONFIG_OF
925static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200926 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
Alexandre TORGUEada86182016-09-15 18:42:33 +0200927 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000928 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200929 {},
930};
931
932MODULE_DEVICE_TABLE(of, stm32_match);
933#endif
934
Alexandre TORGUE34891872016-09-15 18:42:40 +0200935static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
936 struct platform_device *pdev)
937{
938 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
939 struct uart_port *port = &stm32port->port;
940 struct device *dev = &pdev->dev;
941 struct dma_slave_config config;
942 struct dma_async_tx_descriptor *desc = NULL;
943 dma_cookie_t cookie;
944 int ret;
945
946 /* Request DMA RX channel */
947 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
948 if (!stm32port->rx_ch) {
949 dev_info(dev, "rx dma alloc failed\n");
950 return -ENODEV;
951 }
952 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
953 &stm32port->rx_dma_buf,
954 GFP_KERNEL);
955 if (!stm32port->rx_buf) {
956 ret = -ENOMEM;
957 goto alloc_err;
958 }
959
960 /* Configure DMA channel */
961 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200962 config.src_addr = port->mapbase + ofs->rdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200963 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
964
965 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
966 if (ret < 0) {
967 dev_err(dev, "rx dma channel config failed\n");
968 ret = -ENODEV;
969 goto config_err;
970 }
971
972 /* Prepare a DMA cyclic transaction */
973 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
974 stm32port->rx_dma_buf,
975 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
976 DMA_PREP_INTERRUPT);
977 if (!desc) {
978 dev_err(dev, "rx dma prep cyclic failed\n");
979 ret = -ENODEV;
980 goto config_err;
981 }
982
983 /* No callback as dma buffer is drained on usart interrupt */
984 desc->callback = NULL;
985 desc->callback_param = NULL;
986
987 /* Push current DMA transaction in the pending queue */
988 cookie = dmaengine_submit(desc);
989
990 /* Issue pending DMA requests */
991 dma_async_issue_pending(stm32port->rx_ch);
992
993 return 0;
994
995config_err:
996 dma_free_coherent(&pdev->dev,
997 RX_BUF_L, stm32port->rx_buf,
998 stm32port->rx_dma_buf);
999
1000alloc_err:
1001 dma_release_channel(stm32port->rx_ch);
1002 stm32port->rx_ch = NULL;
1003
1004 return ret;
1005}
1006
1007static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
1008 struct platform_device *pdev)
1009{
1010 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1011 struct uart_port *port = &stm32port->port;
1012 struct device *dev = &pdev->dev;
1013 struct dma_slave_config config;
1014 int ret;
1015
1016 stm32port->tx_dma_busy = false;
1017
1018 /* Request DMA TX channel */
1019 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
1020 if (!stm32port->tx_ch) {
1021 dev_info(dev, "tx dma alloc failed\n");
1022 return -ENODEV;
1023 }
1024 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
1025 &stm32port->tx_dma_buf,
1026 GFP_KERNEL);
1027 if (!stm32port->tx_buf) {
1028 ret = -ENOMEM;
1029 goto alloc_err;
1030 }
1031
1032 /* Configure DMA channel */
1033 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +02001034 config.dst_addr = port->mapbase + ofs->tdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +02001035 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1036
1037 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1038 if (ret < 0) {
1039 dev_err(dev, "tx dma channel config failed\n");
1040 ret = -ENODEV;
1041 goto config_err;
1042 }
1043
1044 return 0;
1045
1046config_err:
1047 dma_free_coherent(&pdev->dev,
1048 TX_BUF_L, stm32port->tx_buf,
1049 stm32port->tx_dma_buf);
1050
1051alloc_err:
1052 dma_release_channel(stm32port->tx_ch);
1053 stm32port->tx_ch = NULL;
1054
1055 return ret;
1056}
1057
Maxime Coquelin48a60922015-06-10 21:19:36 +02001058static int stm32_serial_probe(struct platform_device *pdev)
1059{
Alexandre TORGUEada86182016-09-15 18:42:33 +02001060 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001061 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001062 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001063
1064 stm32port = stm32_of_get_stm32_port(pdev);
1065 if (!stm32port)
1066 return -ENODEV;
1067
Alexandre TORGUEada86182016-09-15 18:42:33 +02001068 match = of_match_device(stm32_match, &pdev->dev);
1069 if (match && match->data)
1070 stm32port->info = (struct stm32_usart_info *)match->data;
1071 else
1072 return -EINVAL;
1073
Maxime Coquelin48a60922015-06-10 21:19:36 +02001074 ret = stm32_init_port(stm32port, pdev);
1075 if (ret)
1076 return ret;
1077
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001078 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) {
1079 ret = device_init_wakeup(&pdev->dev, true);
1080 if (ret)
1081 goto err_uninit;
1082 }
1083
Maxime Coquelin48a60922015-06-10 21:19:36 +02001084 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1085 if (ret)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001086 goto err_nowup;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001087
Alexandre TORGUE34891872016-09-15 18:42:40 +02001088 ret = stm32_of_dma_rx_probe(stm32port, pdev);
1089 if (ret)
1090 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
1091
1092 ret = stm32_of_dma_tx_probe(stm32port, pdev);
1093 if (ret)
1094 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
1095
Maxime Coquelin48a60922015-06-10 21:19:36 +02001096 platform_set_drvdata(pdev, &stm32port->port);
1097
1098 return 0;
Fabrice Gasnierada80042017-07-13 15:08:29 +00001099
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
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001137 if (cfg->has_wakeup && stm32_port->wakeirq >= 0)
1138 device_init_wakeup(&pdev->dev, false);
1139
Alexandre TORGUE511c7b12016-09-15 18:42:38 +02001140 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001141
1142 return uart_remove_one_port(&stm32_usart_driver, port);
1143}
1144
1145
1146#ifdef CONFIG_SERIAL_STM32_CONSOLE
1147static void stm32_console_putchar(struct uart_port *port, int ch)
1148{
Alexandre TORGUEada86182016-09-15 18:42:33 +02001149 struct stm32_port *stm32_port = to_stm32_port(port);
1150 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1151
1152 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +02001153 cpu_relax();
1154
Alexandre TORGUEada86182016-09-15 18:42:33 +02001155 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001156}
1157
1158static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
1159{
1160 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +02001161 struct stm32_port *stm32_port = to_stm32_port(port);
1162 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001163 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +02001164 unsigned long flags;
1165 u32 old_cr1, new_cr1;
1166 int locked = 1;
1167
1168 local_irq_save(flags);
1169 if (port->sysrq)
1170 locked = 0;
1171 else if (oops_in_progress)
1172 locked = spin_trylock(&port->lock);
1173 else
1174 spin_lock(&port->lock);
1175
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001176 /* Save and disable interrupts, enable the transmitter */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001177 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001178 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +02001179 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
Alexandre TORGUEada86182016-09-15 18:42:33 +02001180 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001181
1182 uart_console_write(port, s, cnt, stm32_console_putchar);
1183
1184 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001185 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001186
1187 if (locked)
1188 spin_unlock(&port->lock);
1189 local_irq_restore(flags);
1190}
1191
1192static int stm32_console_setup(struct console *co, char *options)
1193{
1194 struct stm32_port *stm32port;
1195 int baud = 9600;
1196 int bits = 8;
1197 int parity = 'n';
1198 int flow = 'n';
1199
1200 if (co->index >= STM32_MAX_PORTS)
1201 return -ENODEV;
1202
1203 stm32port = &stm32_ports[co->index];
1204
1205 /*
1206 * This driver does not support early console initialization
1207 * (use ARM early printk support instead), so we only expect
1208 * this to be called during the uart port registration when the
1209 * driver gets probed and the port should be mapped at that point.
1210 */
1211 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1212 return -ENXIO;
1213
1214 if (options)
1215 uart_parse_options(options, &baud, &parity, &bits, &flow);
1216
1217 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1218}
1219
1220static struct console stm32_console = {
1221 .name = STM32_SERIAL_NAME,
1222 .device = uart_console_device,
1223 .write = stm32_console_write,
1224 .setup = stm32_console_setup,
1225 .flags = CON_PRINTBUFFER,
1226 .index = -1,
1227 .data = &stm32_usart_driver,
1228};
1229
1230#define STM32_SERIAL_CONSOLE (&stm32_console)
1231
1232#else
1233#define STM32_SERIAL_CONSOLE NULL
1234#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1235
1236static struct uart_driver stm32_usart_driver = {
1237 .driver_name = DRIVER_NAME,
1238 .dev_name = STM32_SERIAL_NAME,
1239 .major = 0,
1240 .minor = 0,
1241 .nr = STM32_MAX_PORTS,
1242 .cons = STM32_SERIAL_CONSOLE,
1243};
1244
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001245#ifdef CONFIG_PM_SLEEP
1246static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable)
1247{
1248 struct stm32_port *stm32_port = to_stm32_port(port);
1249 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1250 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1251 u32 val;
1252
1253 if (!cfg->has_wakeup || stm32_port->wakeirq < 0)
1254 return;
1255
1256 if (enable) {
1257 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1258 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1259 val = readl_relaxed(port->membase + ofs->cr3);
1260 val &= ~USART_CR3_WUS_MASK;
1261 /* Enable Wake up interrupt from low power on start bit */
1262 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1263 writel_relaxed(val, port->membase + ofs->cr3);
1264 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1265 } else {
1266 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1267 }
1268}
1269
1270static int stm32_serial_suspend(struct device *dev)
1271{
1272 struct uart_port *port = dev_get_drvdata(dev);
1273
1274 uart_suspend_port(&stm32_usart_driver, port);
1275
1276 if (device_may_wakeup(dev))
1277 stm32_serial_enable_wakeup(port, true);
1278 else
1279 stm32_serial_enable_wakeup(port, false);
1280
1281 return 0;
1282}
1283
1284static int stm32_serial_resume(struct device *dev)
1285{
1286 struct uart_port *port = dev_get_drvdata(dev);
1287
1288 if (device_may_wakeup(dev))
1289 stm32_serial_enable_wakeup(port, false);
1290
1291 return uart_resume_port(&stm32_usart_driver, port);
1292}
1293#endif /* CONFIG_PM_SLEEP */
1294
1295static const struct dev_pm_ops stm32_serial_pm_ops = {
1296 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1297};
1298
Maxime Coquelin48a60922015-06-10 21:19:36 +02001299static struct platform_driver stm32_serial_driver = {
1300 .probe = stm32_serial_probe,
1301 .remove = stm32_serial_remove,
1302 .driver = {
1303 .name = DRIVER_NAME,
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001304 .pm = &stm32_serial_pm_ops,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001305 .of_match_table = of_match_ptr(stm32_match),
1306 },
1307};
1308
1309static int __init usart_init(void)
1310{
1311 static char banner[] __initdata = "STM32 USART driver initialized";
1312 int ret;
1313
1314 pr_info("%s\n", banner);
1315
1316 ret = uart_register_driver(&stm32_usart_driver);
1317 if (ret)
1318 return ret;
1319
1320 ret = platform_driver_register(&stm32_serial_driver);
1321 if (ret)
1322 uart_unregister_driver(&stm32_usart_driver);
1323
1324 return ret;
1325}
1326
1327static void __exit usart_exit(void)
1328{
1329 platform_driver_unregister(&stm32_serial_driver);
1330 uart_unregister_driver(&stm32_usart_driver);
1331}
1332
1333module_init(usart_init);
1334module_exit(usart_exit);
1335
1336MODULE_ALIAS("platform:" DRIVER_NAME);
1337MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1338MODULE_LICENSE("GPL v2");