blob: ea8b591dd46f48b2edc0658a7af31f2af1eb9c00 [file] [log] [blame]
Maxime Coquelin48a60922015-06-10 21:19:36 +02001/*
2 * Copyright (C) Maxime Coquelin 2015
Alexandre TORGUEada86182016-09-15 18:42:33 +02003 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * Gerald Baeza <gerald.baeza@st.com>
Maxime Coquelin48a60922015-06-10 21:19:36 +02005 * License terms: GNU General Public License (GPL), version 2
6 *
7 * Inspired by st-asc.c from STMicroelectronics (c)
8 */
9
Maxime Coquelin6b596a82015-06-16 11:12:19 +020010#if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Maxime Coquelin48a60922015-06-10 21:19:36 +020011#define SUPPORT_SYSRQ
12#endif
13
Alexandre TORGUE34891872016-09-15 18:42:40 +020014#include <linux/clk.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020015#include <linux/console.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020016#include <linux/delay.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020017#include <linux/dma-direction.h>
18#include <linux/dmaengine.h>
19#include <linux/dma-mapping.h>
20#include <linux/io.h>
21#include <linux/iopoll.h>
22#include <linux/irq.h>
23#include <linux/module.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020024#include <linux/of.h>
25#include <linux/of_platform.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020026#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020028#include <linux/serial_core.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020029#include <linux/serial.h>
30#include <linux/spinlock.h>
31#include <linux/sysrq.h>
32#include <linux/tty_flip.h>
33#include <linux/tty.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020034
Alexandre TORGUEbc5a0b52016-09-15 18:42:35 +020035#include "stm32-usart.h"
Maxime Coquelin48a60922015-06-10 21:19:36 +020036
37static void stm32_stop_tx(struct uart_port *port);
Alexandre TORGUE34891872016-09-15 18:42:40 +020038static void stm32_transmit_chars(struct uart_port *port);
Maxime Coquelin48a60922015-06-10 21:19:36 +020039
40static inline struct stm32_port *to_stm32_port(struct uart_port *port)
41{
42 return container_of(port, struct stm32_port, port);
43}
44
45static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
46{
47 u32 val;
48
49 val = readl_relaxed(port->membase + reg);
50 val |= bits;
51 writel_relaxed(val, port->membase + reg);
52}
53
54static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
55{
56 u32 val;
57
58 val = readl_relaxed(port->membase + reg);
59 val &= ~bits;
60 writel_relaxed(val, port->membase + reg);
61}
62
Baoyou Xieb97055b2016-09-26 19:58:56 +080063static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
64 bool threaded)
Alexandre TORGUE34891872016-09-15 18:42:40 +020065{
66 struct stm32_port *stm32_port = to_stm32_port(port);
67 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
68 enum dma_status status;
69 struct dma_tx_state state;
70
71 *sr = readl_relaxed(port->membase + ofs->isr);
72
73 if (threaded && stm32_port->rx_ch) {
74 status = dmaengine_tx_status(stm32_port->rx_ch,
75 stm32_port->rx_ch->cookie,
76 &state);
77 if ((status == DMA_IN_PROGRESS) &&
78 (*last_res != state.residue))
79 return 1;
80 else
81 return 0;
82 } else if (*sr & USART_SR_RXNE) {
83 return 1;
84 }
85 return 0;
86}
87
Baoyou Xieb97055b2016-09-26 19:58:56 +080088static unsigned long
89stm32_get_char(struct uart_port *port, u32 *sr, int *last_res)
Alexandre TORGUE34891872016-09-15 18:42:40 +020090{
91 struct stm32_port *stm32_port = to_stm32_port(port);
92 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
93 unsigned long c;
94
95 if (stm32_port->rx_ch) {
96 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
97 if ((*last_res) == 0)
98 *last_res = RX_BUF_L;
99 return c;
100 } else {
101 return readl_relaxed(port->membase + ofs->rdr);
102 }
103}
104
105static void stm32_receive_chars(struct uart_port *port, bool threaded)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200106{
107 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200108 struct stm32_port *stm32_port = to_stm32_port(port);
109 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200110 unsigned long c;
111 u32 sr;
112 char flag;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200113 static int last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200114
115 if (port->irq_wake)
116 pm_wakeup_event(tport->tty->dev, 0);
117
Alexandre TORGUE34891872016-09-15 18:42:40 +0200118 while (stm32_pending_rx(port, &sr, &last_res, threaded)) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200119 sr |= USART_SR_DUMMY_RX;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200120 c = stm32_get_char(port, &sr, &last_res);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200121 flag = TTY_NORMAL;
122 port->icount.rx++;
123
124 if (sr & USART_SR_ERR_MASK) {
125 if (sr & USART_SR_LBD) {
126 port->icount.brk++;
127 if (uart_handle_break(port))
128 continue;
129 } else if (sr & USART_SR_ORE) {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200130 if (ofs->icr != UNDEF_REG)
131 writel_relaxed(USART_ICR_ORECF,
132 port->membase +
133 ofs->icr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200134 port->icount.overrun++;
135 } else if (sr & USART_SR_PE) {
136 port->icount.parity++;
137 } else if (sr & USART_SR_FE) {
138 port->icount.frame++;
139 }
140
141 sr &= port->read_status_mask;
142
143 if (sr & USART_SR_LBD)
144 flag = TTY_BREAK;
145 else if (sr & USART_SR_PE)
146 flag = TTY_PARITY;
147 else if (sr & USART_SR_FE)
148 flag = TTY_FRAME;
149 }
150
151 if (uart_handle_sysrq_char(port, c))
152 continue;
153 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
154 }
155
156 spin_unlock(&port->lock);
157 tty_flip_buffer_push(tport);
158 spin_lock(&port->lock);
159}
160
Alexandre TORGUE34891872016-09-15 18:42:40 +0200161static void stm32_tx_dma_complete(void *arg)
162{
163 struct uart_port *port = arg;
164 struct stm32_port *stm32port = to_stm32_port(port);
165 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
166 unsigned int isr;
167 int ret;
168
169 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
170 isr,
171 (isr & USART_SR_TC),
172 10, 100000);
173
174 if (ret)
175 dev_err(port->dev, "terminal count not set\n");
176
177 if (ofs->icr == UNDEF_REG)
178 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
179 else
180 stm32_set_bits(port, ofs->icr, USART_CR_TC);
181
182 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
183 stm32port->tx_dma_busy = false;
184
185 /* Let's see if we have pending data to send */
186 stm32_transmit_chars(port);
187}
188
189static void stm32_transmit_chars_pio(struct uart_port *port)
190{
191 struct stm32_port *stm32_port = to_stm32_port(port);
192 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
193 struct circ_buf *xmit = &port->state->xmit;
194 unsigned int isr;
195 int ret;
196
197 if (stm32_port->tx_dma_busy) {
198 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
199 stm32_port->tx_dma_busy = false;
200 }
201
202 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
203 isr,
204 (isr & USART_SR_TXE),
205 10, 100);
206
207 if (ret)
208 dev_err(port->dev, "tx empty not set\n");
209
210 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
211
212 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
213 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
214 port->icount.tx++;
215}
216
217static void stm32_transmit_chars_dma(struct uart_port *port)
218{
219 struct stm32_port *stm32port = to_stm32_port(port);
220 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
221 struct circ_buf *xmit = &port->state->xmit;
222 struct dma_async_tx_descriptor *desc = NULL;
223 dma_cookie_t cookie;
224 unsigned int count, i;
225
226 if (stm32port->tx_dma_busy)
227 return;
228
229 stm32port->tx_dma_busy = true;
230
231 count = uart_circ_chars_pending(xmit);
232
233 if (count > TX_BUF_L)
234 count = TX_BUF_L;
235
236 if (xmit->tail < xmit->head) {
237 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
238 } else {
239 size_t one = UART_XMIT_SIZE - xmit->tail;
240 size_t two;
241
242 if (one > count)
243 one = count;
244 two = count - one;
245
246 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
247 if (two)
248 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
249 }
250
251 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
252 stm32port->tx_dma_buf,
253 count,
254 DMA_MEM_TO_DEV,
255 DMA_PREP_INTERRUPT);
256
257 if (!desc) {
258 for (i = count; i > 0; i--)
259 stm32_transmit_chars_pio(port);
260 return;
261 }
262
263 desc->callback = stm32_tx_dma_complete;
264 desc->callback_param = port;
265
266 /* Push current DMA TX transaction in the pending queue */
267 cookie = dmaengine_submit(desc);
268
269 /* Issue pending DMA TX requests */
270 dma_async_issue_pending(stm32port->tx_ch);
271
272 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
273 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
274
275 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
276 port->icount.tx += count;
277}
278
Maxime Coquelin48a60922015-06-10 21:19:36 +0200279static void stm32_transmit_chars(struct uart_port *port)
280{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200281 struct stm32_port *stm32_port = to_stm32_port(port);
282 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200283 struct circ_buf *xmit = &port->state->xmit;
284
285 if (port->x_char) {
Alexandre TORGUE34891872016-09-15 18:42:40 +0200286 if (stm32_port->tx_dma_busy)
287 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200288 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200289 port->x_char = 0;
290 port->icount.tx++;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200291 if (stm32_port->tx_dma_busy)
292 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200293 return;
294 }
295
Erwan Le Rayb2af35a2019-05-21 17:45:44 +0200296 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
297 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200298 return;
299 }
300
Alexandre TORGUE34891872016-09-15 18:42:40 +0200301 if (stm32_port->tx_ch)
302 stm32_transmit_chars_dma(port);
303 else
304 stm32_transmit_chars_pio(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200305
306 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
307 uart_write_wakeup(port);
308
309 if (uart_circ_empty(xmit))
Erwan Le Rayb2af35a2019-05-21 17:45:44 +0200310 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200311}
312
313static irqreturn_t stm32_interrupt(int irq, void *ptr)
314{
315 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200316 struct stm32_port *stm32_port = to_stm32_port(port);
317 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200318 u32 sr;
319
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200320 spin_lock(&port->lock);
321
Alexandre TORGUEada86182016-09-15 18:42:33 +0200322 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200323
Alexandre TORGUE34891872016-09-15 18:42:40 +0200324 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
325 stm32_receive_chars(port, false);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200326
Alexandre TORGUE34891872016-09-15 18:42:40 +0200327 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200328 stm32_transmit_chars(port);
329
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200330 spin_unlock(&port->lock);
331
Alexandre TORGUE34891872016-09-15 18:42:40 +0200332 if (stm32_port->rx_ch)
333 return IRQ_WAKE_THREAD;
334 else
335 return IRQ_HANDLED;
336}
337
338static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
339{
340 struct uart_port *port = ptr;
341 struct stm32_port *stm32_port = to_stm32_port(port);
342
343 spin_lock(&port->lock);
344
345 if (stm32_port->rx_ch)
346 stm32_receive_chars(port, true);
347
Maxime Coquelin48a60922015-06-10 21:19:36 +0200348 spin_unlock(&port->lock);
349
350 return IRQ_HANDLED;
351}
352
353static unsigned int stm32_tx_empty(struct uart_port *port)
354{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200355 struct stm32_port *stm32_port = to_stm32_port(port);
356 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
357
358 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200359}
360
361static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
362{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200363 struct stm32_port *stm32_port = to_stm32_port(port);
364 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
365
Maxime Coquelin48a60922015-06-10 21:19:36 +0200366 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Alexandre TORGUEada86182016-09-15 18:42:33 +0200367 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200368 else
Alexandre TORGUEada86182016-09-15 18:42:33 +0200369 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200370}
371
372static unsigned int stm32_get_mctrl(struct uart_port *port)
373{
374 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
375 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
376}
377
378/* Transmit stop */
379static void stm32_stop_tx(struct uart_port *port)
380{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200381 struct stm32_port *stm32_port = to_stm32_port(port);
382 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
383
384 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200385}
386
387/* There are probably characters waiting to be transmitted. */
388static void stm32_start_tx(struct uart_port *port)
389{
390 struct circ_buf *xmit = &port->state->xmit;
391
392 if (uart_circ_empty(xmit))
393 return;
394
Alexandre TORGUE34891872016-09-15 18:42:40 +0200395 stm32_transmit_chars(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200396}
397
398/* Throttle the remote when input buffer is about to overflow. */
399static void stm32_throttle(struct uart_port *port)
400{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200401 struct stm32_port *stm32_port = to_stm32_port(port);
402 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200403 unsigned long flags;
404
405 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200406 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200407 spin_unlock_irqrestore(&port->lock, flags);
408}
409
410/* Unthrottle the remote, the input buffer can now accept data. */
411static void stm32_unthrottle(struct uart_port *port)
412{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200413 struct stm32_port *stm32_port = to_stm32_port(port);
414 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200415 unsigned long flags;
416
417 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200418 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200419 spin_unlock_irqrestore(&port->lock, flags);
420}
421
422/* Receive stop */
423static void stm32_stop_rx(struct uart_port *port)
424{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200425 struct stm32_port *stm32_port = to_stm32_port(port);
426 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
427
428 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200429}
430
431/* Handle breaks - ignored by us */
432static void stm32_break_ctl(struct uart_port *port, int break_state)
433{
434}
435
436static int stm32_startup(struct uart_port *port)
437{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200438 struct stm32_port *stm32_port = to_stm32_port(port);
439 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200440 const char *name = to_platform_device(port->dev)->name;
441 u32 val;
442 int ret;
443
Alexandre TORGUE34891872016-09-15 18:42:40 +0200444 ret = request_threaded_irq(port->irq, stm32_interrupt,
445 stm32_threaded_interrupt,
446 IRQF_NO_SUSPEND, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200447 if (ret)
448 return ret;
449
450 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200451 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200452
453 return 0;
454}
455
456static void stm32_shutdown(struct uart_port *port)
457{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200458 struct stm32_port *stm32_port = to_stm32_port(port);
459 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200460 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200461 u32 val;
462
463 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200464 val |= BIT(cfg->uart_enable_bit);
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200465 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200466
467 free_irq(port->irq, port);
468}
469
470static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
471 struct ktermios *old)
472{
473 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200474 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
475 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200476 unsigned int baud;
477 u32 usartdiv, mantissa, fraction, oversampling;
478 tcflag_t cflag = termios->c_cflag;
479 u32 cr1, cr2, cr3;
480 unsigned long flags;
481
482 if (!stm32_port->hw_flow_control)
483 cflag &= ~CRTSCTS;
484
485 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
486
487 spin_lock_irqsave(&port->lock, flags);
488
489 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200490 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200491
Alexandre TORGUEada86182016-09-15 18:42:33 +0200492 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
493 cr1 |= BIT(cfg->uart_enable_bit);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200494 cr2 = 0;
495 cr3 = 0;
496
497 if (cflag & CSTOPB)
498 cr2 |= USART_CR2_STOP_2B;
499
500 if (cflag & PARENB) {
501 cr1 |= USART_CR1_PCE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200502 if ((cflag & CSIZE) == CS8) {
503 if (cfg->has_7bits_data)
504 cr1 |= USART_CR1_M0;
505 else
506 cr1 |= USART_CR1_M;
507 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200508 }
509
510 if (cflag & PARODD)
511 cr1 |= USART_CR1_PS;
512
513 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
514 if (cflag & CRTSCTS) {
515 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
516 cr3 |= USART_CR3_CTSE;
517 }
518
519 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
520
521 /*
522 * The USART supports 16 or 8 times oversampling.
523 * By default we prefer 16 times oversampling, so that the receiver
524 * has a better tolerance to clock deviations.
525 * 8 times oversampling is only used to achieve higher speeds.
526 */
527 if (usartdiv < 16) {
528 oversampling = 8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200529 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200530 } else {
531 oversampling = 16;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200532 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200533 }
534
535 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
536 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200537 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200538
539 uart_update_timeout(port, cflag, baud);
540
541 port->read_status_mask = USART_SR_ORE;
542 if (termios->c_iflag & INPCK)
543 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
544 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
545 port->read_status_mask |= USART_SR_LBD;
546
547 /* Characters to ignore */
548 port->ignore_status_mask = 0;
549 if (termios->c_iflag & IGNPAR)
550 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
551 if (termios->c_iflag & IGNBRK) {
552 port->ignore_status_mask |= USART_SR_LBD;
553 /*
554 * If we're ignoring parity and break indicators,
555 * ignore overruns too (for real raw support).
556 */
557 if (termios->c_iflag & IGNPAR)
558 port->ignore_status_mask |= USART_SR_ORE;
559 }
560
561 /* Ignore all characters if CREAD is not set */
562 if ((termios->c_cflag & CREAD) == 0)
563 port->ignore_status_mask |= USART_SR_DUMMY_RX;
564
Alexandre TORGUE34891872016-09-15 18:42:40 +0200565 if (stm32_port->rx_ch)
566 cr3 |= USART_CR3_DMAR;
567
Alexandre TORGUEada86182016-09-15 18:42:33 +0200568 writel_relaxed(cr3, port->membase + ofs->cr3);
569 writel_relaxed(cr2, port->membase + ofs->cr2);
570 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200571
572 spin_unlock_irqrestore(&port->lock, flags);
573}
574
575static const char *stm32_type(struct uart_port *port)
576{
577 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
578}
579
580static void stm32_release_port(struct uart_port *port)
581{
582}
583
584static int stm32_request_port(struct uart_port *port)
585{
586 return 0;
587}
588
589static void stm32_config_port(struct uart_port *port, int flags)
590{
591 if (flags & UART_CONFIG_TYPE)
592 port->type = PORT_STM32;
593}
594
595static int
596stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
597{
598 /* No user changeable parameters */
599 return -EINVAL;
600}
601
602static void stm32_pm(struct uart_port *port, unsigned int state,
603 unsigned int oldstate)
604{
605 struct stm32_port *stm32port = container_of(port,
606 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200607 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
608 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200609 unsigned long flags = 0;
610
611 switch (state) {
612 case UART_PM_STATE_ON:
613 clk_prepare_enable(stm32port->clk);
614 break;
615 case UART_PM_STATE_OFF:
616 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200617 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200618 spin_unlock_irqrestore(&port->lock, flags);
619 clk_disable_unprepare(stm32port->clk);
620 break;
621 }
622}
623
624static const struct uart_ops stm32_uart_ops = {
625 .tx_empty = stm32_tx_empty,
626 .set_mctrl = stm32_set_mctrl,
627 .get_mctrl = stm32_get_mctrl,
628 .stop_tx = stm32_stop_tx,
629 .start_tx = stm32_start_tx,
630 .throttle = stm32_throttle,
631 .unthrottle = stm32_unthrottle,
632 .stop_rx = stm32_stop_rx,
633 .break_ctl = stm32_break_ctl,
634 .startup = stm32_startup,
635 .shutdown = stm32_shutdown,
636 .set_termios = stm32_set_termios,
637 .pm = stm32_pm,
638 .type = stm32_type,
639 .release_port = stm32_release_port,
640 .request_port = stm32_request_port,
641 .config_port = stm32_config_port,
642 .verify_port = stm32_verify_port,
643};
644
645static int stm32_init_port(struct stm32_port *stm32port,
646 struct platform_device *pdev)
647{
648 struct uart_port *port = &stm32port->port;
649 struct resource *res;
650 int ret;
651
652 port->iotype = UPIO_MEM;
653 port->flags = UPF_BOOT_AUTOCONF;
654 port->ops = &stm32_uart_ops;
655 port->dev = &pdev->dev;
656 port->irq = platform_get_irq(pdev, 0);
657
658 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
659 port->membase = devm_ioremap_resource(&pdev->dev, res);
660 if (IS_ERR(port->membase))
661 return PTR_ERR(port->membase);
662 port->mapbase = res->start;
663
664 spin_lock_init(&port->lock);
665
666 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
667 if (IS_ERR(stm32port->clk))
668 return PTR_ERR(stm32port->clk);
669
670 /* Ensure that clk rate is correct by enabling the clk */
671 ret = clk_prepare_enable(stm32port->clk);
672 if (ret)
673 return ret;
674
675 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
676 if (!stm32port->port.uartclk)
677 ret = -EINVAL;
678
Maxime Coquelin48a60922015-06-10 21:19:36 +0200679 return ret;
680}
681
682static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
683{
684 struct device_node *np = pdev->dev.of_node;
685 int id;
686
687 if (!np)
688 return NULL;
689
690 id = of_alias_get_id(np, "serial");
691 if (id < 0)
692 id = 0;
693
694 if (WARN_ON(id >= STM32_MAX_PORTS))
695 return NULL;
696
697 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200698 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200699 stm32_ports[id].port.line = id;
700 return &stm32_ports[id];
701}
702
703#ifdef CONFIG_OF
704static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200705 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
706 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
707 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
708 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200709 {},
710};
711
712MODULE_DEVICE_TABLE(of, stm32_match);
713#endif
714
Alexandre TORGUE34891872016-09-15 18:42:40 +0200715static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
716 struct platform_device *pdev)
717{
718 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
719 struct uart_port *port = &stm32port->port;
720 struct device *dev = &pdev->dev;
721 struct dma_slave_config config;
722 struct dma_async_tx_descriptor *desc = NULL;
723 dma_cookie_t cookie;
724 int ret;
725
726 /* Request DMA RX channel */
727 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
728 if (!stm32port->rx_ch) {
729 dev_info(dev, "rx dma alloc failed\n");
730 return -ENODEV;
731 }
732 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
733 &stm32port->rx_dma_buf,
734 GFP_KERNEL);
735 if (!stm32port->rx_buf) {
736 ret = -ENOMEM;
737 goto alloc_err;
738 }
739
740 /* Configure DMA channel */
741 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200742 config.src_addr = port->mapbase + ofs->rdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200743 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
744
745 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
746 if (ret < 0) {
747 dev_err(dev, "rx dma channel config failed\n");
748 ret = -ENODEV;
749 goto config_err;
750 }
751
752 /* Prepare a DMA cyclic transaction */
753 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
754 stm32port->rx_dma_buf,
755 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
756 DMA_PREP_INTERRUPT);
757 if (!desc) {
758 dev_err(dev, "rx dma prep cyclic failed\n");
759 ret = -ENODEV;
760 goto config_err;
761 }
762
763 /* No callback as dma buffer is drained on usart interrupt */
764 desc->callback = NULL;
765 desc->callback_param = NULL;
766
767 /* Push current DMA transaction in the pending queue */
768 cookie = dmaengine_submit(desc);
769
770 /* Issue pending DMA requests */
771 dma_async_issue_pending(stm32port->rx_ch);
772
773 return 0;
774
775config_err:
776 dma_free_coherent(&pdev->dev,
777 RX_BUF_L, stm32port->rx_buf,
778 stm32port->rx_dma_buf);
779
780alloc_err:
781 dma_release_channel(stm32port->rx_ch);
782 stm32port->rx_ch = NULL;
783
784 return ret;
785}
786
787static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
788 struct platform_device *pdev)
789{
790 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
791 struct uart_port *port = &stm32port->port;
792 struct device *dev = &pdev->dev;
793 struct dma_slave_config config;
794 int ret;
795
796 stm32port->tx_dma_busy = false;
797
798 /* Request DMA TX channel */
799 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
800 if (!stm32port->tx_ch) {
801 dev_info(dev, "tx dma alloc failed\n");
802 return -ENODEV;
803 }
804 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
805 &stm32port->tx_dma_buf,
806 GFP_KERNEL);
807 if (!stm32port->tx_buf) {
808 ret = -ENOMEM;
809 goto alloc_err;
810 }
811
812 /* Configure DMA channel */
813 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200814 config.dst_addr = port->mapbase + ofs->tdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200815 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
816
817 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
818 if (ret < 0) {
819 dev_err(dev, "tx dma channel config failed\n");
820 ret = -ENODEV;
821 goto config_err;
822 }
823
824 return 0;
825
826config_err:
827 dma_free_coherent(&pdev->dev,
828 TX_BUF_L, stm32port->tx_buf,
829 stm32port->tx_dma_buf);
830
831alloc_err:
832 dma_release_channel(stm32port->tx_ch);
833 stm32port->tx_ch = NULL;
834
835 return ret;
836}
837
Maxime Coquelin48a60922015-06-10 21:19:36 +0200838static int stm32_serial_probe(struct platform_device *pdev)
839{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200840 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200841 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200842 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200843
844 stm32port = stm32_of_get_stm32_port(pdev);
845 if (!stm32port)
846 return -ENODEV;
847
Alexandre TORGUEada86182016-09-15 18:42:33 +0200848 match = of_match_device(stm32_match, &pdev->dev);
849 if (match && match->data)
850 stm32port->info = (struct stm32_usart_info *)match->data;
851 else
852 return -EINVAL;
853
Maxime Coquelin48a60922015-06-10 21:19:36 +0200854 ret = stm32_init_port(stm32port, pdev);
855 if (ret)
856 return ret;
857
858 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
859 if (ret)
860 return ret;
861
Alexandre TORGUE34891872016-09-15 18:42:40 +0200862 ret = stm32_of_dma_rx_probe(stm32port, pdev);
863 if (ret)
864 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
865
866 ret = stm32_of_dma_tx_probe(stm32port, pdev);
867 if (ret)
868 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
869
Maxime Coquelin48a60922015-06-10 21:19:36 +0200870 platform_set_drvdata(pdev, &stm32port->port);
871
872 return 0;
873}
874
875static int stm32_serial_remove(struct platform_device *pdev)
876{
877 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200878 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200879 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
880
881 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
882
883 if (stm32_port->rx_ch)
884 dma_release_channel(stm32_port->rx_ch);
885
886 if (stm32_port->rx_dma_buf)
887 dma_free_coherent(&pdev->dev,
888 RX_BUF_L, stm32_port->rx_buf,
889 stm32_port->rx_dma_buf);
890
891 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
892
893 if (stm32_port->tx_ch)
894 dma_release_channel(stm32_port->tx_ch);
895
896 if (stm32_port->tx_dma_buf)
897 dma_free_coherent(&pdev->dev,
898 TX_BUF_L, stm32_port->tx_buf,
899 stm32_port->tx_dma_buf);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200900
901 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200902
903 return uart_remove_one_port(&stm32_usart_driver, port);
904}
905
906
907#ifdef CONFIG_SERIAL_STM32_CONSOLE
908static void stm32_console_putchar(struct uart_port *port, int ch)
909{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200910 struct stm32_port *stm32_port = to_stm32_port(port);
911 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
912
913 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200914 cpu_relax();
915
Alexandre TORGUEada86182016-09-15 18:42:33 +0200916 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200917}
918
919static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
920{
921 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200922 struct stm32_port *stm32_port = to_stm32_port(port);
923 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200924 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200925 unsigned long flags;
926 u32 old_cr1, new_cr1;
927 int locked = 1;
928
929 local_irq_save(flags);
930 if (port->sysrq)
931 locked = 0;
932 else if (oops_in_progress)
933 locked = spin_trylock(&port->lock);
934 else
935 spin_lock(&port->lock);
936
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200937 /* Save and disable interrupts, enable the transmitter */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200938 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200939 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200940 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200941 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200942
943 uart_console_write(port, s, cnt, stm32_console_putchar);
944
945 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200946 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200947
948 if (locked)
949 spin_unlock(&port->lock);
950 local_irq_restore(flags);
951}
952
953static int stm32_console_setup(struct console *co, char *options)
954{
955 struct stm32_port *stm32port;
956 int baud = 9600;
957 int bits = 8;
958 int parity = 'n';
959 int flow = 'n';
960
961 if (co->index >= STM32_MAX_PORTS)
962 return -ENODEV;
963
964 stm32port = &stm32_ports[co->index];
965
966 /*
967 * This driver does not support early console initialization
968 * (use ARM early printk support instead), so we only expect
969 * this to be called during the uart port registration when the
970 * driver gets probed and the port should be mapped at that point.
971 */
972 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
973 return -ENXIO;
974
975 if (options)
976 uart_parse_options(options, &baud, &parity, &bits, &flow);
977
978 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
979}
980
981static struct console stm32_console = {
982 .name = STM32_SERIAL_NAME,
983 .device = uart_console_device,
984 .write = stm32_console_write,
985 .setup = stm32_console_setup,
986 .flags = CON_PRINTBUFFER,
987 .index = -1,
988 .data = &stm32_usart_driver,
989};
990
991#define STM32_SERIAL_CONSOLE (&stm32_console)
992
993#else
994#define STM32_SERIAL_CONSOLE NULL
995#endif /* CONFIG_SERIAL_STM32_CONSOLE */
996
997static struct uart_driver stm32_usart_driver = {
998 .driver_name = DRIVER_NAME,
999 .dev_name = STM32_SERIAL_NAME,
1000 .major = 0,
1001 .minor = 0,
1002 .nr = STM32_MAX_PORTS,
1003 .cons = STM32_SERIAL_CONSOLE,
1004};
1005
1006static struct platform_driver stm32_serial_driver = {
1007 .probe = stm32_serial_probe,
1008 .remove = stm32_serial_remove,
1009 .driver = {
1010 .name = DRIVER_NAME,
1011 .of_match_table = of_match_ptr(stm32_match),
1012 },
1013};
1014
1015static int __init usart_init(void)
1016{
1017 static char banner[] __initdata = "STM32 USART driver initialized";
1018 int ret;
1019
1020 pr_info("%s\n", banner);
1021
1022 ret = uart_register_driver(&stm32_usart_driver);
1023 if (ret)
1024 return ret;
1025
1026 ret = platform_driver_register(&stm32_serial_driver);
1027 if (ret)
1028 uart_unregister_driver(&stm32_usart_driver);
1029
1030 return ret;
1031}
1032
1033static void __exit usart_exit(void)
1034{
1035 platform_driver_unregister(&stm32_serial_driver);
1036 uart_unregister_driver(&stm32_usart_driver);
1037}
1038
1039module_init(usart_init);
1040module_exit(usart_exit);
1041
1042MODULE_ALIAS("platform:" DRIVER_NAME);
1043MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1044MODULE_LICENSE("GPL v2");