blob: a12d79cfe60769a78a5a63967b96e7b5121117d8 [file] [log] [blame]
Maxime Coquelin48a60922015-06-10 21:19:36 +02001/*
2 * Copyright (C) Maxime Coquelin 2015
Bich HEMON3e5fcba2017-07-13 15:08:26 +00003 * Copyright (C) STMicroelectronics SA 2017
Alexandre TORGUEada86182016-09-15 18:42:33 +02004 * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
5 * Gerald Baeza <gerald.baeza@st.com>
Maxime Coquelin48a60922015-06-10 21:19:36 +02006 * License terms: GNU General Public License (GPL), version 2
7 *
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>
Maxime Coquelin48a60922015-06-10 21:19:36 +020029#include <linux/serial_core.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020030#include <linux/serial.h>
31#include <linux/spinlock.h>
32#include <linux/sysrq.h>
33#include <linux/tty_flip.h>
34#include <linux/tty.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020035
Alexandre TORGUEbc5a0b52016-09-15 18:42:35 +020036#include "stm32-usart.h"
Maxime Coquelin48a60922015-06-10 21:19:36 +020037
38static void stm32_stop_tx(struct uart_port *port);
Alexandre TORGUE34891872016-09-15 18:42:40 +020039static void stm32_transmit_chars(struct uart_port *port);
Maxime Coquelin48a60922015-06-10 21:19:36 +020040
41static inline struct stm32_port *to_stm32_port(struct uart_port *port)
42{
43 return container_of(port, struct stm32_port, port);
44}
45
46static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
47{
48 u32 val;
49
50 val = readl_relaxed(port->membase + reg);
51 val |= bits;
52 writel_relaxed(val, port->membase + reg);
53}
54
55static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
56{
57 u32 val;
58
59 val = readl_relaxed(port->membase + reg);
60 val &= ~bits;
61 writel_relaxed(val, port->membase + reg);
62}
63
Baoyou Xieb97055b2016-09-26 19:58:56 +080064static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
65 bool threaded)
Alexandre TORGUE34891872016-09-15 18:42:40 +020066{
67 struct stm32_port *stm32_port = to_stm32_port(port);
68 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
69 enum dma_status status;
70 struct dma_tx_state state;
71
72 *sr = readl_relaxed(port->membase + ofs->isr);
73
74 if (threaded && stm32_port->rx_ch) {
75 status = dmaengine_tx_status(stm32_port->rx_ch,
76 stm32_port->rx_ch->cookie,
77 &state);
78 if ((status == DMA_IN_PROGRESS) &&
79 (*last_res != state.residue))
80 return 1;
81 else
82 return 0;
83 } else if (*sr & USART_SR_RXNE) {
84 return 1;
85 }
86 return 0;
87}
88
Baoyou Xieb97055b2016-09-26 19:58:56 +080089static unsigned long
90stm32_get_char(struct uart_port *port, u32 *sr, int *last_res)
Alexandre TORGUE34891872016-09-15 18:42:40 +020091{
92 struct stm32_port *stm32_port = to_stm32_port(port);
93 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
94 unsigned long c;
95
96 if (stm32_port->rx_ch) {
97 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
98 if ((*last_res) == 0)
99 *last_res = RX_BUF_L;
100 return c;
101 } else {
102 return readl_relaxed(port->membase + ofs->rdr);
103 }
104}
105
106static void stm32_receive_chars(struct uart_port *port, bool threaded)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200107{
108 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200109 struct stm32_port *stm32_port = to_stm32_port(port);
110 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200111 unsigned long c;
112 u32 sr;
113 char flag;
114
115 if (port->irq_wake)
116 pm_wakeup_event(tport->tty->dev, 0);
117
Gerald Baezae5707912017-07-13 15:08:27 +0000118 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200119 sr |= USART_SR_DUMMY_RX;
Gerald Baezae5707912017-07-13 15:08:27 +0000120 c = stm32_get_char(port, &sr, &stm32_port->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
296 if (uart_tx_stopped(port)) {
297 stm32_stop_tx(port);
298 return;
299 }
300
301 if (uart_circ_empty(xmit)) {
302 stm32_stop_tx(port);
303 return;
304 }
305
Alexandre TORGUE34891872016-09-15 18:42:40 +0200306 if (stm32_port->tx_ch)
307 stm32_transmit_chars_dma(port);
308 else
309 stm32_transmit_chars_pio(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200310
311 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
312 uart_write_wakeup(port);
313
314 if (uart_circ_empty(xmit))
315 stm32_stop_tx(port);
316}
317
318static irqreturn_t stm32_interrupt(int irq, void *ptr)
319{
320 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200321 struct stm32_port *stm32_port = to_stm32_port(port);
322 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200323 u32 sr;
324
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200325 spin_lock(&port->lock);
326
Alexandre TORGUEada86182016-09-15 18:42:33 +0200327 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200328
Alexandre TORGUE34891872016-09-15 18:42:40 +0200329 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
330 stm32_receive_chars(port, false);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200331
Alexandre TORGUE34891872016-09-15 18:42:40 +0200332 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200333 stm32_transmit_chars(port);
334
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200335 spin_unlock(&port->lock);
336
Alexandre TORGUE34891872016-09-15 18:42:40 +0200337 if (stm32_port->rx_ch)
338 return IRQ_WAKE_THREAD;
339 else
340 return IRQ_HANDLED;
341}
342
343static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
344{
345 struct uart_port *port = ptr;
346 struct stm32_port *stm32_port = to_stm32_port(port);
347
348 spin_lock(&port->lock);
349
350 if (stm32_port->rx_ch)
351 stm32_receive_chars(port, true);
352
Maxime Coquelin48a60922015-06-10 21:19:36 +0200353 spin_unlock(&port->lock);
354
355 return IRQ_HANDLED;
356}
357
358static unsigned int stm32_tx_empty(struct uart_port *port)
359{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200360 struct stm32_port *stm32_port = to_stm32_port(port);
361 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
362
363 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200364}
365
366static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
367{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200368 struct stm32_port *stm32_port = to_stm32_port(port);
369 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
370
Maxime Coquelin48a60922015-06-10 21:19:36 +0200371 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Alexandre TORGUEada86182016-09-15 18:42:33 +0200372 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200373 else
Alexandre TORGUEada86182016-09-15 18:42:33 +0200374 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200375}
376
377static unsigned int stm32_get_mctrl(struct uart_port *port)
378{
379 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
380 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
381}
382
383/* Transmit stop */
384static void stm32_stop_tx(struct uart_port *port)
385{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200386 struct stm32_port *stm32_port = to_stm32_port(port);
387 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
388
389 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200390}
391
392/* There are probably characters waiting to be transmitted. */
393static void stm32_start_tx(struct uart_port *port)
394{
395 struct circ_buf *xmit = &port->state->xmit;
396
397 if (uart_circ_empty(xmit))
398 return;
399
Alexandre TORGUE34891872016-09-15 18:42:40 +0200400 stm32_transmit_chars(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200401}
402
403/* Throttle the remote when input buffer is about to overflow. */
404static void stm32_throttle(struct uart_port *port)
405{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200406 struct stm32_port *stm32_port = to_stm32_port(port);
407 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200408 unsigned long flags;
409
410 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200411 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200412 spin_unlock_irqrestore(&port->lock, flags);
413}
414
415/* Unthrottle the remote, the input buffer can now accept data. */
416static void stm32_unthrottle(struct uart_port *port)
417{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200418 struct stm32_port *stm32_port = to_stm32_port(port);
419 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200420 unsigned long flags;
421
422 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200423 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200424 spin_unlock_irqrestore(&port->lock, flags);
425}
426
427/* Receive stop */
428static void stm32_stop_rx(struct uart_port *port)
429{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200430 struct stm32_port *stm32_port = to_stm32_port(port);
431 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
432
433 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200434}
435
436/* Handle breaks - ignored by us */
437static void stm32_break_ctl(struct uart_port *port, int break_state)
438{
439}
440
441static int stm32_startup(struct uart_port *port)
442{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200443 struct stm32_port *stm32_port = to_stm32_port(port);
444 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200445 const char *name = to_platform_device(port->dev)->name;
446 u32 val;
447 int ret;
448
Alexandre TORGUE34891872016-09-15 18:42:40 +0200449 ret = request_threaded_irq(port->irq, stm32_interrupt,
450 stm32_threaded_interrupt,
451 IRQF_NO_SUSPEND, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200452 if (ret)
453 return ret;
454
455 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200456 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200457
458 return 0;
459}
460
461static void stm32_shutdown(struct uart_port *port)
462{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200463 struct stm32_port *stm32_port = to_stm32_port(port);
464 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200465 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200466 u32 val;
467
468 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200469 val |= BIT(cfg->uart_enable_bit);
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200470 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200471
472 free_irq(port->irq, port);
473}
474
475static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
476 struct ktermios *old)
477{
478 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200479 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
480 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200481 unsigned int baud;
482 u32 usartdiv, mantissa, fraction, oversampling;
483 tcflag_t cflag = termios->c_cflag;
484 u32 cr1, cr2, cr3;
485 unsigned long flags;
486
487 if (!stm32_port->hw_flow_control)
488 cflag &= ~CRTSCTS;
489
490 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
491
492 spin_lock_irqsave(&port->lock, flags);
493
494 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200495 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200496
Alexandre TORGUEada86182016-09-15 18:42:33 +0200497 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
498 cr1 |= BIT(cfg->uart_enable_bit);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200499 cr2 = 0;
500 cr3 = 0;
501
502 if (cflag & CSTOPB)
503 cr2 |= USART_CR2_STOP_2B;
504
505 if (cflag & PARENB) {
506 cr1 |= USART_CR1_PCE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200507 if ((cflag & CSIZE) == CS8) {
508 if (cfg->has_7bits_data)
509 cr1 |= USART_CR1_M0;
510 else
511 cr1 |= USART_CR1_M;
512 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200513 }
514
515 if (cflag & PARODD)
516 cr1 |= USART_CR1_PS;
517
518 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
519 if (cflag & CRTSCTS) {
520 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
521 cr3 |= USART_CR3_CTSE;
522 }
523
524 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
525
526 /*
527 * The USART supports 16 or 8 times oversampling.
528 * By default we prefer 16 times oversampling, so that the receiver
529 * has a better tolerance to clock deviations.
530 * 8 times oversampling is only used to achieve higher speeds.
531 */
532 if (usartdiv < 16) {
533 oversampling = 8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200534 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200535 } else {
536 oversampling = 16;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200537 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200538 }
539
540 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
541 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200542 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200543
544 uart_update_timeout(port, cflag, baud);
545
546 port->read_status_mask = USART_SR_ORE;
547 if (termios->c_iflag & INPCK)
548 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
549 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
550 port->read_status_mask |= USART_SR_LBD;
551
552 /* Characters to ignore */
553 port->ignore_status_mask = 0;
554 if (termios->c_iflag & IGNPAR)
555 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
556 if (termios->c_iflag & IGNBRK) {
557 port->ignore_status_mask |= USART_SR_LBD;
558 /*
559 * If we're ignoring parity and break indicators,
560 * ignore overruns too (for real raw support).
561 */
562 if (termios->c_iflag & IGNPAR)
563 port->ignore_status_mask |= USART_SR_ORE;
564 }
565
566 /* Ignore all characters if CREAD is not set */
567 if ((termios->c_cflag & CREAD) == 0)
568 port->ignore_status_mask |= USART_SR_DUMMY_RX;
569
Alexandre TORGUE34891872016-09-15 18:42:40 +0200570 if (stm32_port->rx_ch)
571 cr3 |= USART_CR3_DMAR;
572
Alexandre TORGUEada86182016-09-15 18:42:33 +0200573 writel_relaxed(cr3, port->membase + ofs->cr3);
574 writel_relaxed(cr2, port->membase + ofs->cr2);
575 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200576
577 spin_unlock_irqrestore(&port->lock, flags);
578}
579
580static const char *stm32_type(struct uart_port *port)
581{
582 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
583}
584
585static void stm32_release_port(struct uart_port *port)
586{
587}
588
589static int stm32_request_port(struct uart_port *port)
590{
591 return 0;
592}
593
594static void stm32_config_port(struct uart_port *port, int flags)
595{
596 if (flags & UART_CONFIG_TYPE)
597 port->type = PORT_STM32;
598}
599
600static int
601stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
602{
603 /* No user changeable parameters */
604 return -EINVAL;
605}
606
607static void stm32_pm(struct uart_port *port, unsigned int state,
608 unsigned int oldstate)
609{
610 struct stm32_port *stm32port = container_of(port,
611 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200612 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
613 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200614 unsigned long flags = 0;
615
616 switch (state) {
617 case UART_PM_STATE_ON:
618 clk_prepare_enable(stm32port->clk);
619 break;
620 case UART_PM_STATE_OFF:
621 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200622 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200623 spin_unlock_irqrestore(&port->lock, flags);
624 clk_disable_unprepare(stm32port->clk);
625 break;
626 }
627}
628
629static const struct uart_ops stm32_uart_ops = {
630 .tx_empty = stm32_tx_empty,
631 .set_mctrl = stm32_set_mctrl,
632 .get_mctrl = stm32_get_mctrl,
633 .stop_tx = stm32_stop_tx,
634 .start_tx = stm32_start_tx,
635 .throttle = stm32_throttle,
636 .unthrottle = stm32_unthrottle,
637 .stop_rx = stm32_stop_rx,
638 .break_ctl = stm32_break_ctl,
639 .startup = stm32_startup,
640 .shutdown = stm32_shutdown,
641 .set_termios = stm32_set_termios,
642 .pm = stm32_pm,
643 .type = stm32_type,
644 .release_port = stm32_release_port,
645 .request_port = stm32_request_port,
646 .config_port = stm32_config_port,
647 .verify_port = stm32_verify_port,
648};
649
650static int stm32_init_port(struct stm32_port *stm32port,
651 struct platform_device *pdev)
652{
653 struct uart_port *port = &stm32port->port;
654 struct resource *res;
655 int ret;
656
657 port->iotype = UPIO_MEM;
658 port->flags = UPF_BOOT_AUTOCONF;
659 port->ops = &stm32_uart_ops;
660 port->dev = &pdev->dev;
661 port->irq = platform_get_irq(pdev, 0);
662
663 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
664 port->membase = devm_ioremap_resource(&pdev->dev, res);
665 if (IS_ERR(port->membase))
666 return PTR_ERR(port->membase);
667 port->mapbase = res->start;
668
669 spin_lock_init(&port->lock);
670
671 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
672 if (IS_ERR(stm32port->clk))
673 return PTR_ERR(stm32port->clk);
674
675 /* Ensure that clk rate is correct by enabling the clk */
676 ret = clk_prepare_enable(stm32port->clk);
677 if (ret)
678 return ret;
679
680 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
681 if (!stm32port->port.uartclk)
682 ret = -EINVAL;
683
Maxime Coquelin48a60922015-06-10 21:19:36 +0200684 return ret;
685}
686
687static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
688{
689 struct device_node *np = pdev->dev.of_node;
690 int id;
691
692 if (!np)
693 return NULL;
694
695 id = of_alias_get_id(np, "serial");
Gerald Baezae5707912017-07-13 15:08:27 +0000696 if (id < 0) {
697 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
698 return NULL;
699 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200700
701 if (WARN_ON(id >= STM32_MAX_PORTS))
702 return NULL;
703
704 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200705 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200706 stm32_ports[id].port.line = id;
Gerald Baezae5707912017-07-13 15:08:27 +0000707 stm32_ports[id].last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200708 return &stm32_ports[id];
709}
710
711#ifdef CONFIG_OF
712static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200713 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
714 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
715 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
716 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200717 {},
718};
719
720MODULE_DEVICE_TABLE(of, stm32_match);
721#endif
722
Alexandre TORGUE34891872016-09-15 18:42:40 +0200723static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
724 struct platform_device *pdev)
725{
726 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
727 struct uart_port *port = &stm32port->port;
728 struct device *dev = &pdev->dev;
729 struct dma_slave_config config;
730 struct dma_async_tx_descriptor *desc = NULL;
731 dma_cookie_t cookie;
732 int ret;
733
734 /* Request DMA RX channel */
735 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
736 if (!stm32port->rx_ch) {
737 dev_info(dev, "rx dma alloc failed\n");
738 return -ENODEV;
739 }
740 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
741 &stm32port->rx_dma_buf,
742 GFP_KERNEL);
743 if (!stm32port->rx_buf) {
744 ret = -ENOMEM;
745 goto alloc_err;
746 }
747
748 /* Configure DMA channel */
749 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200750 config.src_addr = port->mapbase + ofs->rdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200751 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
752
753 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
754 if (ret < 0) {
755 dev_err(dev, "rx dma channel config failed\n");
756 ret = -ENODEV;
757 goto config_err;
758 }
759
760 /* Prepare a DMA cyclic transaction */
761 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
762 stm32port->rx_dma_buf,
763 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
764 DMA_PREP_INTERRUPT);
765 if (!desc) {
766 dev_err(dev, "rx dma prep cyclic failed\n");
767 ret = -ENODEV;
768 goto config_err;
769 }
770
771 /* No callback as dma buffer is drained on usart interrupt */
772 desc->callback = NULL;
773 desc->callback_param = NULL;
774
775 /* Push current DMA transaction in the pending queue */
776 cookie = dmaengine_submit(desc);
777
778 /* Issue pending DMA requests */
779 dma_async_issue_pending(stm32port->rx_ch);
780
781 return 0;
782
783config_err:
784 dma_free_coherent(&pdev->dev,
785 RX_BUF_L, stm32port->rx_buf,
786 stm32port->rx_dma_buf);
787
788alloc_err:
789 dma_release_channel(stm32port->rx_ch);
790 stm32port->rx_ch = NULL;
791
792 return ret;
793}
794
795static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
796 struct platform_device *pdev)
797{
798 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
799 struct uart_port *port = &stm32port->port;
800 struct device *dev = &pdev->dev;
801 struct dma_slave_config config;
802 int ret;
803
804 stm32port->tx_dma_busy = false;
805
806 /* Request DMA TX channel */
807 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
808 if (!stm32port->tx_ch) {
809 dev_info(dev, "tx dma alloc failed\n");
810 return -ENODEV;
811 }
812 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
813 &stm32port->tx_dma_buf,
814 GFP_KERNEL);
815 if (!stm32port->tx_buf) {
816 ret = -ENOMEM;
817 goto alloc_err;
818 }
819
820 /* Configure DMA channel */
821 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200822 config.dst_addr = port->mapbase + ofs->tdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200823 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
824
825 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
826 if (ret < 0) {
827 dev_err(dev, "tx dma channel config failed\n");
828 ret = -ENODEV;
829 goto config_err;
830 }
831
832 return 0;
833
834config_err:
835 dma_free_coherent(&pdev->dev,
836 TX_BUF_L, stm32port->tx_buf,
837 stm32port->tx_dma_buf);
838
839alloc_err:
840 dma_release_channel(stm32port->tx_ch);
841 stm32port->tx_ch = NULL;
842
843 return ret;
844}
845
Maxime Coquelin48a60922015-06-10 21:19:36 +0200846static int stm32_serial_probe(struct platform_device *pdev)
847{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200848 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200849 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200850 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200851
852 stm32port = stm32_of_get_stm32_port(pdev);
853 if (!stm32port)
854 return -ENODEV;
855
Alexandre TORGUEada86182016-09-15 18:42:33 +0200856 match = of_match_device(stm32_match, &pdev->dev);
857 if (match && match->data)
858 stm32port->info = (struct stm32_usart_info *)match->data;
859 else
860 return -EINVAL;
861
Maxime Coquelin48a60922015-06-10 21:19:36 +0200862 ret = stm32_init_port(stm32port, pdev);
863 if (ret)
864 return ret;
865
866 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
867 if (ret)
868 return ret;
869
Alexandre TORGUE34891872016-09-15 18:42:40 +0200870 ret = stm32_of_dma_rx_probe(stm32port, pdev);
871 if (ret)
872 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
873
874 ret = stm32_of_dma_tx_probe(stm32port, pdev);
875 if (ret)
876 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
877
Maxime Coquelin48a60922015-06-10 21:19:36 +0200878 platform_set_drvdata(pdev, &stm32port->port);
879
880 return 0;
881}
882
883static int stm32_serial_remove(struct platform_device *pdev)
884{
885 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200886 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200887 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
888
889 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
890
891 if (stm32_port->rx_ch)
892 dma_release_channel(stm32_port->rx_ch);
893
894 if (stm32_port->rx_dma_buf)
895 dma_free_coherent(&pdev->dev,
896 RX_BUF_L, stm32_port->rx_buf,
897 stm32_port->rx_dma_buf);
898
899 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
900
901 if (stm32_port->tx_ch)
902 dma_release_channel(stm32_port->tx_ch);
903
904 if (stm32_port->tx_dma_buf)
905 dma_free_coherent(&pdev->dev,
906 TX_BUF_L, stm32_port->tx_buf,
907 stm32_port->tx_dma_buf);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200908
909 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200910
911 return uart_remove_one_port(&stm32_usart_driver, port);
912}
913
914
915#ifdef CONFIG_SERIAL_STM32_CONSOLE
916static void stm32_console_putchar(struct uart_port *port, int ch)
917{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200918 struct stm32_port *stm32_port = to_stm32_port(port);
919 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
920
921 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200922 cpu_relax();
923
Alexandre TORGUEada86182016-09-15 18:42:33 +0200924 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200925}
926
927static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
928{
929 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200930 struct stm32_port *stm32_port = to_stm32_port(port);
931 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200932 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200933 unsigned long flags;
934 u32 old_cr1, new_cr1;
935 int locked = 1;
936
937 local_irq_save(flags);
938 if (port->sysrq)
939 locked = 0;
940 else if (oops_in_progress)
941 locked = spin_trylock(&port->lock);
942 else
943 spin_lock(&port->lock);
944
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200945 /* Save and disable interrupts, enable the transmitter */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200946 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200947 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200948 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200949 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200950
951 uart_console_write(port, s, cnt, stm32_console_putchar);
952
953 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200954 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200955
956 if (locked)
957 spin_unlock(&port->lock);
958 local_irq_restore(flags);
959}
960
961static int stm32_console_setup(struct console *co, char *options)
962{
963 struct stm32_port *stm32port;
964 int baud = 9600;
965 int bits = 8;
966 int parity = 'n';
967 int flow = 'n';
968
969 if (co->index >= STM32_MAX_PORTS)
970 return -ENODEV;
971
972 stm32port = &stm32_ports[co->index];
973
974 /*
975 * This driver does not support early console initialization
976 * (use ARM early printk support instead), so we only expect
977 * this to be called during the uart port registration when the
978 * driver gets probed and the port should be mapped at that point.
979 */
980 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
981 return -ENXIO;
982
983 if (options)
984 uart_parse_options(options, &baud, &parity, &bits, &flow);
985
986 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
987}
988
989static struct console stm32_console = {
990 .name = STM32_SERIAL_NAME,
991 .device = uart_console_device,
992 .write = stm32_console_write,
993 .setup = stm32_console_setup,
994 .flags = CON_PRINTBUFFER,
995 .index = -1,
996 .data = &stm32_usart_driver,
997};
998
999#define STM32_SERIAL_CONSOLE (&stm32_console)
1000
1001#else
1002#define STM32_SERIAL_CONSOLE NULL
1003#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1004
1005static struct uart_driver stm32_usart_driver = {
1006 .driver_name = DRIVER_NAME,
1007 .dev_name = STM32_SERIAL_NAME,
1008 .major = 0,
1009 .minor = 0,
1010 .nr = STM32_MAX_PORTS,
1011 .cons = STM32_SERIAL_CONSOLE,
1012};
1013
1014static struct platform_driver stm32_serial_driver = {
1015 .probe = stm32_serial_probe,
1016 .remove = stm32_serial_remove,
1017 .driver = {
1018 .name = DRIVER_NAME,
1019 .of_match_table = of_match_ptr(stm32_match),
1020 },
1021};
1022
1023static int __init usart_init(void)
1024{
1025 static char banner[] __initdata = "STM32 USART driver initialized";
1026 int ret;
1027
1028 pr_info("%s\n", banner);
1029
1030 ret = uart_register_driver(&stm32_usart_driver);
1031 if (ret)
1032 return ret;
1033
1034 ret = platform_driver_register(&stm32_serial_driver);
1035 if (ret)
1036 uart_unregister_driver(&stm32_usart_driver);
1037
1038 return ret;
1039}
1040
1041static void __exit usart_exit(void)
1042{
1043 platform_driver_unregister(&stm32_serial_driver);
1044 uart_unregister_driver(&stm32_usart_driver);
1045}
1046
1047module_init(usart_init);
1048module_exit(usart_exit);
1049
1050MODULE_ALIAS("platform:" DRIVER_NAME);
1051MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1052MODULE_LICENSE("GPL v2");