blob: 24c4d821472a71414e28e22704de1a1a40b4b2eb [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
Alexandre TORGUE34891872016-09-15 18:42:40 +020063int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
64 bool threaded)
65{
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
88unsigned long stm32_get_char(struct uart_port *port, u32 *sr, int *last_res)
89{
90 struct stm32_port *stm32_port = to_stm32_port(port);
91 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
92 unsigned long c;
93
94 if (stm32_port->rx_ch) {
95 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
96 if ((*last_res) == 0)
97 *last_res = RX_BUF_L;
98 return c;
99 } else {
100 return readl_relaxed(port->membase + ofs->rdr);
101 }
102}
103
104static void stm32_receive_chars(struct uart_port *port, bool threaded)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200105{
106 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200107 struct stm32_port *stm32_port = to_stm32_port(port);
108 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200109 unsigned long c;
110 u32 sr;
111 char flag;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200112 static int last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200113
114 if (port->irq_wake)
115 pm_wakeup_event(tport->tty->dev, 0);
116
Alexandre TORGUE34891872016-09-15 18:42:40 +0200117 while (stm32_pending_rx(port, &sr, &last_res, threaded)) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200118 sr |= USART_SR_DUMMY_RX;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200119 c = stm32_get_char(port, &sr, &last_res);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200120 flag = TTY_NORMAL;
121 port->icount.rx++;
122
123 if (sr & USART_SR_ERR_MASK) {
124 if (sr & USART_SR_LBD) {
125 port->icount.brk++;
126 if (uart_handle_break(port))
127 continue;
128 } else if (sr & USART_SR_ORE) {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200129 if (ofs->icr != UNDEF_REG)
130 writel_relaxed(USART_ICR_ORECF,
131 port->membase +
132 ofs->icr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200133 port->icount.overrun++;
134 } else if (sr & USART_SR_PE) {
135 port->icount.parity++;
136 } else if (sr & USART_SR_FE) {
137 port->icount.frame++;
138 }
139
140 sr &= port->read_status_mask;
141
142 if (sr & USART_SR_LBD)
143 flag = TTY_BREAK;
144 else if (sr & USART_SR_PE)
145 flag = TTY_PARITY;
146 else if (sr & USART_SR_FE)
147 flag = TTY_FRAME;
148 }
149
150 if (uart_handle_sysrq_char(port, c))
151 continue;
152 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
153 }
154
155 spin_unlock(&port->lock);
156 tty_flip_buffer_push(tport);
157 spin_lock(&port->lock);
158}
159
Alexandre TORGUE34891872016-09-15 18:42:40 +0200160static void stm32_tx_dma_complete(void *arg)
161{
162 struct uart_port *port = arg;
163 struct stm32_port *stm32port = to_stm32_port(port);
164 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
165 unsigned int isr;
166 int ret;
167
168 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
169 isr,
170 (isr & USART_SR_TC),
171 10, 100000);
172
173 if (ret)
174 dev_err(port->dev, "terminal count not set\n");
175
176 if (ofs->icr == UNDEF_REG)
177 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
178 else
179 stm32_set_bits(port, ofs->icr, USART_CR_TC);
180
181 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
182 stm32port->tx_dma_busy = false;
183
184 /* Let's see if we have pending data to send */
185 stm32_transmit_chars(port);
186}
187
188static void stm32_transmit_chars_pio(struct uart_port *port)
189{
190 struct stm32_port *stm32_port = to_stm32_port(port);
191 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
192 struct circ_buf *xmit = &port->state->xmit;
193 unsigned int isr;
194 int ret;
195
196 if (stm32_port->tx_dma_busy) {
197 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
198 stm32_port->tx_dma_busy = false;
199 }
200
201 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
202 isr,
203 (isr & USART_SR_TXE),
204 10, 100);
205
206 if (ret)
207 dev_err(port->dev, "tx empty not set\n");
208
209 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
210
211 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
212 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
213 port->icount.tx++;
214}
215
216static void stm32_transmit_chars_dma(struct uart_port *port)
217{
218 struct stm32_port *stm32port = to_stm32_port(port);
219 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
220 struct circ_buf *xmit = &port->state->xmit;
221 struct dma_async_tx_descriptor *desc = NULL;
222 dma_cookie_t cookie;
223 unsigned int count, i;
224
225 if (stm32port->tx_dma_busy)
226 return;
227
228 stm32port->tx_dma_busy = true;
229
230 count = uart_circ_chars_pending(xmit);
231
232 if (count > TX_BUF_L)
233 count = TX_BUF_L;
234
235 if (xmit->tail < xmit->head) {
236 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
237 } else {
238 size_t one = UART_XMIT_SIZE - xmit->tail;
239 size_t two;
240
241 if (one > count)
242 one = count;
243 two = count - one;
244
245 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
246 if (two)
247 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
248 }
249
250 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
251 stm32port->tx_dma_buf,
252 count,
253 DMA_MEM_TO_DEV,
254 DMA_PREP_INTERRUPT);
255
256 if (!desc) {
257 for (i = count; i > 0; i--)
258 stm32_transmit_chars_pio(port);
259 return;
260 }
261
262 desc->callback = stm32_tx_dma_complete;
263 desc->callback_param = port;
264
265 /* Push current DMA TX transaction in the pending queue */
266 cookie = dmaengine_submit(desc);
267
268 /* Issue pending DMA TX requests */
269 dma_async_issue_pending(stm32port->tx_ch);
270
271 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
272 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
273
274 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
275 port->icount.tx += count;
276}
277
Maxime Coquelin48a60922015-06-10 21:19:36 +0200278static void stm32_transmit_chars(struct uart_port *port)
279{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200280 struct stm32_port *stm32_port = to_stm32_port(port);
281 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200282 struct circ_buf *xmit = &port->state->xmit;
283
284 if (port->x_char) {
Alexandre TORGUE34891872016-09-15 18:42:40 +0200285 if (stm32_port->tx_dma_busy)
286 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200287 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200288 port->x_char = 0;
289 port->icount.tx++;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200290 if (stm32_port->tx_dma_busy)
291 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200292 return;
293 }
294
295 if (uart_tx_stopped(port)) {
296 stm32_stop_tx(port);
297 return;
298 }
299
300 if (uart_circ_empty(xmit)) {
301 stm32_stop_tx(port);
302 return;
303 }
304
Alexandre TORGUE34891872016-09-15 18:42:40 +0200305 if (stm32_port->tx_ch)
306 stm32_transmit_chars_dma(port);
307 else
308 stm32_transmit_chars_pio(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200309
310 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
311 uart_write_wakeup(port);
312
313 if (uart_circ_empty(xmit))
314 stm32_stop_tx(port);
315}
316
317static irqreturn_t stm32_interrupt(int irq, void *ptr)
318{
319 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200320 struct stm32_port *stm32_port = to_stm32_port(port);
321 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200322 u32 sr;
323
Alexandre TORGUEada86182016-09-15 18:42:33 +0200324 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200325
Alexandre TORGUE34891872016-09-15 18:42:40 +0200326 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
327 stm32_receive_chars(port, false);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200328
Alexandre TORGUE34891872016-09-15 18:42:40 +0200329 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200330 stm32_transmit_chars(port);
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;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200460 u32 val;
461
462 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200463 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200464
465 free_irq(port->irq, port);
466}
467
468static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
469 struct ktermios *old)
470{
471 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200472 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
473 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200474 unsigned int baud;
475 u32 usartdiv, mantissa, fraction, oversampling;
476 tcflag_t cflag = termios->c_cflag;
477 u32 cr1, cr2, cr3;
478 unsigned long flags;
479
480 if (!stm32_port->hw_flow_control)
481 cflag &= ~CRTSCTS;
482
483 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
484
485 spin_lock_irqsave(&port->lock, flags);
486
487 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200488 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200489
Alexandre TORGUEada86182016-09-15 18:42:33 +0200490 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
491 cr1 |= BIT(cfg->uart_enable_bit);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200492 cr2 = 0;
493 cr3 = 0;
494
495 if (cflag & CSTOPB)
496 cr2 |= USART_CR2_STOP_2B;
497
498 if (cflag & PARENB) {
499 cr1 |= USART_CR1_PCE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200500 if ((cflag & CSIZE) == CS8) {
501 if (cfg->has_7bits_data)
502 cr1 |= USART_CR1_M0;
503 else
504 cr1 |= USART_CR1_M;
505 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200506 }
507
508 if (cflag & PARODD)
509 cr1 |= USART_CR1_PS;
510
511 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
512 if (cflag & CRTSCTS) {
513 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
514 cr3 |= USART_CR3_CTSE;
515 }
516
517 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
518
519 /*
520 * The USART supports 16 or 8 times oversampling.
521 * By default we prefer 16 times oversampling, so that the receiver
522 * has a better tolerance to clock deviations.
523 * 8 times oversampling is only used to achieve higher speeds.
524 */
525 if (usartdiv < 16) {
526 oversampling = 8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200527 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200528 } else {
529 oversampling = 16;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200530 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200531 }
532
533 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
534 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200535 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200536
537 uart_update_timeout(port, cflag, baud);
538
539 port->read_status_mask = USART_SR_ORE;
540 if (termios->c_iflag & INPCK)
541 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
542 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
543 port->read_status_mask |= USART_SR_LBD;
544
545 /* Characters to ignore */
546 port->ignore_status_mask = 0;
547 if (termios->c_iflag & IGNPAR)
548 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
549 if (termios->c_iflag & IGNBRK) {
550 port->ignore_status_mask |= USART_SR_LBD;
551 /*
552 * If we're ignoring parity and break indicators,
553 * ignore overruns too (for real raw support).
554 */
555 if (termios->c_iflag & IGNPAR)
556 port->ignore_status_mask |= USART_SR_ORE;
557 }
558
559 /* Ignore all characters if CREAD is not set */
560 if ((termios->c_cflag & CREAD) == 0)
561 port->ignore_status_mask |= USART_SR_DUMMY_RX;
562
Alexandre TORGUE34891872016-09-15 18:42:40 +0200563 if (stm32_port->rx_ch)
564 cr3 |= USART_CR3_DMAR;
565
Alexandre TORGUEada86182016-09-15 18:42:33 +0200566 writel_relaxed(cr3, port->membase + ofs->cr3);
567 writel_relaxed(cr2, port->membase + ofs->cr2);
568 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200569
570 spin_unlock_irqrestore(&port->lock, flags);
571}
572
573static const char *stm32_type(struct uart_port *port)
574{
575 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
576}
577
578static void stm32_release_port(struct uart_port *port)
579{
580}
581
582static int stm32_request_port(struct uart_port *port)
583{
584 return 0;
585}
586
587static void stm32_config_port(struct uart_port *port, int flags)
588{
589 if (flags & UART_CONFIG_TYPE)
590 port->type = PORT_STM32;
591}
592
593static int
594stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
595{
596 /* No user changeable parameters */
597 return -EINVAL;
598}
599
600static void stm32_pm(struct uart_port *port, unsigned int state,
601 unsigned int oldstate)
602{
603 struct stm32_port *stm32port = container_of(port,
604 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200605 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
606 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200607 unsigned long flags = 0;
608
609 switch (state) {
610 case UART_PM_STATE_ON:
611 clk_prepare_enable(stm32port->clk);
612 break;
613 case UART_PM_STATE_OFF:
614 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200615 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200616 spin_unlock_irqrestore(&port->lock, flags);
617 clk_disable_unprepare(stm32port->clk);
618 break;
619 }
620}
621
622static const struct uart_ops stm32_uart_ops = {
623 .tx_empty = stm32_tx_empty,
624 .set_mctrl = stm32_set_mctrl,
625 .get_mctrl = stm32_get_mctrl,
626 .stop_tx = stm32_stop_tx,
627 .start_tx = stm32_start_tx,
628 .throttle = stm32_throttle,
629 .unthrottle = stm32_unthrottle,
630 .stop_rx = stm32_stop_rx,
631 .break_ctl = stm32_break_ctl,
632 .startup = stm32_startup,
633 .shutdown = stm32_shutdown,
634 .set_termios = stm32_set_termios,
635 .pm = stm32_pm,
636 .type = stm32_type,
637 .release_port = stm32_release_port,
638 .request_port = stm32_request_port,
639 .config_port = stm32_config_port,
640 .verify_port = stm32_verify_port,
641};
642
643static int stm32_init_port(struct stm32_port *stm32port,
644 struct platform_device *pdev)
645{
646 struct uart_port *port = &stm32port->port;
647 struct resource *res;
648 int ret;
649
650 port->iotype = UPIO_MEM;
651 port->flags = UPF_BOOT_AUTOCONF;
652 port->ops = &stm32_uart_ops;
653 port->dev = &pdev->dev;
654 port->irq = platform_get_irq(pdev, 0);
655
656 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
657 port->membase = devm_ioremap_resource(&pdev->dev, res);
658 if (IS_ERR(port->membase))
659 return PTR_ERR(port->membase);
660 port->mapbase = res->start;
661
662 spin_lock_init(&port->lock);
663
664 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
665 if (IS_ERR(stm32port->clk))
666 return PTR_ERR(stm32port->clk);
667
668 /* Ensure that clk rate is correct by enabling the clk */
669 ret = clk_prepare_enable(stm32port->clk);
670 if (ret)
671 return ret;
672
673 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
674 if (!stm32port->port.uartclk)
675 ret = -EINVAL;
676
Maxime Coquelin48a60922015-06-10 21:19:36 +0200677 return ret;
678}
679
680static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
681{
682 struct device_node *np = pdev->dev.of_node;
683 int id;
684
685 if (!np)
686 return NULL;
687
688 id = of_alias_get_id(np, "serial");
689 if (id < 0)
690 id = 0;
691
692 if (WARN_ON(id >= STM32_MAX_PORTS))
693 return NULL;
694
695 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200696 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200697 stm32_ports[id].port.line = id;
698 return &stm32_ports[id];
699}
700
701#ifdef CONFIG_OF
702static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200703 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
704 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
705 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
706 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200707 {},
708};
709
710MODULE_DEVICE_TABLE(of, stm32_match);
711#endif
712
Alexandre TORGUE34891872016-09-15 18:42:40 +0200713static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
714 struct platform_device *pdev)
715{
716 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
717 struct uart_port *port = &stm32port->port;
718 struct device *dev = &pdev->dev;
719 struct dma_slave_config config;
720 struct dma_async_tx_descriptor *desc = NULL;
721 dma_cookie_t cookie;
722 int ret;
723
724 /* Request DMA RX channel */
725 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
726 if (!stm32port->rx_ch) {
727 dev_info(dev, "rx dma alloc failed\n");
728 return -ENODEV;
729 }
730 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
731 &stm32port->rx_dma_buf,
732 GFP_KERNEL);
733 if (!stm32port->rx_buf) {
734 ret = -ENOMEM;
735 goto alloc_err;
736 }
737
738 /* Configure DMA channel */
739 memset(&config, 0, sizeof(config));
740 config.src_addr = (dma_addr_t)port->membase + ofs->rdr;
741 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
742
743 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
744 if (ret < 0) {
745 dev_err(dev, "rx dma channel config failed\n");
746 ret = -ENODEV;
747 goto config_err;
748 }
749
750 /* Prepare a DMA cyclic transaction */
751 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
752 stm32port->rx_dma_buf,
753 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
754 DMA_PREP_INTERRUPT);
755 if (!desc) {
756 dev_err(dev, "rx dma prep cyclic failed\n");
757 ret = -ENODEV;
758 goto config_err;
759 }
760
761 /* No callback as dma buffer is drained on usart interrupt */
762 desc->callback = NULL;
763 desc->callback_param = NULL;
764
765 /* Push current DMA transaction in the pending queue */
766 cookie = dmaengine_submit(desc);
767
768 /* Issue pending DMA requests */
769 dma_async_issue_pending(stm32port->rx_ch);
770
771 return 0;
772
773config_err:
774 dma_free_coherent(&pdev->dev,
775 RX_BUF_L, stm32port->rx_buf,
776 stm32port->rx_dma_buf);
777
778alloc_err:
779 dma_release_channel(stm32port->rx_ch);
780 stm32port->rx_ch = NULL;
781
782 return ret;
783}
784
785static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
786 struct platform_device *pdev)
787{
788 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
789 struct uart_port *port = &stm32port->port;
790 struct device *dev = &pdev->dev;
791 struct dma_slave_config config;
792 int ret;
793
794 stm32port->tx_dma_busy = false;
795
796 /* Request DMA TX channel */
797 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
798 if (!stm32port->tx_ch) {
799 dev_info(dev, "tx dma alloc failed\n");
800 return -ENODEV;
801 }
802 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
803 &stm32port->tx_dma_buf,
804 GFP_KERNEL);
805 if (!stm32port->tx_buf) {
806 ret = -ENOMEM;
807 goto alloc_err;
808 }
809
810 /* Configure DMA channel */
811 memset(&config, 0, sizeof(config));
812 config.dst_addr = (dma_addr_t)port->membase + ofs->tdr;
813 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
814
815 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
816 if (ret < 0) {
817 dev_err(dev, "tx dma channel config failed\n");
818 ret = -ENODEV;
819 goto config_err;
820 }
821
822 return 0;
823
824config_err:
825 dma_free_coherent(&pdev->dev,
826 TX_BUF_L, stm32port->tx_buf,
827 stm32port->tx_dma_buf);
828
829alloc_err:
830 dma_release_channel(stm32port->tx_ch);
831 stm32port->tx_ch = NULL;
832
833 return ret;
834}
835
Maxime Coquelin48a60922015-06-10 21:19:36 +0200836static int stm32_serial_probe(struct platform_device *pdev)
837{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200838 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200839 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200840 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200841
842 stm32port = stm32_of_get_stm32_port(pdev);
843 if (!stm32port)
844 return -ENODEV;
845
Alexandre TORGUEada86182016-09-15 18:42:33 +0200846 match = of_match_device(stm32_match, &pdev->dev);
847 if (match && match->data)
848 stm32port->info = (struct stm32_usart_info *)match->data;
849 else
850 return -EINVAL;
851
Maxime Coquelin48a60922015-06-10 21:19:36 +0200852 ret = stm32_init_port(stm32port, pdev);
853 if (ret)
854 return ret;
855
856 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
857 if (ret)
858 return ret;
859
Alexandre TORGUE34891872016-09-15 18:42:40 +0200860 ret = stm32_of_dma_rx_probe(stm32port, pdev);
861 if (ret)
862 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
863
864 ret = stm32_of_dma_tx_probe(stm32port, pdev);
865 if (ret)
866 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
867
Maxime Coquelin48a60922015-06-10 21:19:36 +0200868 platform_set_drvdata(pdev, &stm32port->port);
869
870 return 0;
871}
872
873static int stm32_serial_remove(struct platform_device *pdev)
874{
875 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200876 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200877 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
878
879 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
880
881 if (stm32_port->rx_ch)
882 dma_release_channel(stm32_port->rx_ch);
883
884 if (stm32_port->rx_dma_buf)
885 dma_free_coherent(&pdev->dev,
886 RX_BUF_L, stm32_port->rx_buf,
887 stm32_port->rx_dma_buf);
888
889 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
890
891 if (stm32_port->tx_ch)
892 dma_release_channel(stm32_port->tx_ch);
893
894 if (stm32_port->tx_dma_buf)
895 dma_free_coherent(&pdev->dev,
896 TX_BUF_L, stm32_port->tx_buf,
897 stm32_port->tx_dma_buf);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200898
899 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200900
901 return uart_remove_one_port(&stm32_usart_driver, port);
902}
903
904
905#ifdef CONFIG_SERIAL_STM32_CONSOLE
906static void stm32_console_putchar(struct uart_port *port, int ch)
907{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200908 struct stm32_port *stm32_port = to_stm32_port(port);
909 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
910
911 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200912 cpu_relax();
913
Alexandre TORGUEada86182016-09-15 18:42:33 +0200914 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200915}
916
917static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
918{
919 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200920 struct stm32_port *stm32_port = to_stm32_port(port);
921 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200922 unsigned long flags;
923 u32 old_cr1, new_cr1;
924 int locked = 1;
925
926 local_irq_save(flags);
927 if (port->sysrq)
928 locked = 0;
929 else if (oops_in_progress)
930 locked = spin_trylock(&port->lock);
931 else
932 spin_lock(&port->lock);
933
934 /* Save and disable interrupts */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200935 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200936 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200937 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200938
939 uart_console_write(port, s, cnt, stm32_console_putchar);
940
941 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200942 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200943
944 if (locked)
945 spin_unlock(&port->lock);
946 local_irq_restore(flags);
947}
948
949static int stm32_console_setup(struct console *co, char *options)
950{
951 struct stm32_port *stm32port;
952 int baud = 9600;
953 int bits = 8;
954 int parity = 'n';
955 int flow = 'n';
956
957 if (co->index >= STM32_MAX_PORTS)
958 return -ENODEV;
959
960 stm32port = &stm32_ports[co->index];
961
962 /*
963 * This driver does not support early console initialization
964 * (use ARM early printk support instead), so we only expect
965 * this to be called during the uart port registration when the
966 * driver gets probed and the port should be mapped at that point.
967 */
968 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
969 return -ENXIO;
970
971 if (options)
972 uart_parse_options(options, &baud, &parity, &bits, &flow);
973
974 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
975}
976
977static struct console stm32_console = {
978 .name = STM32_SERIAL_NAME,
979 .device = uart_console_device,
980 .write = stm32_console_write,
981 .setup = stm32_console_setup,
982 .flags = CON_PRINTBUFFER,
983 .index = -1,
984 .data = &stm32_usart_driver,
985};
986
987#define STM32_SERIAL_CONSOLE (&stm32_console)
988
989#else
990#define STM32_SERIAL_CONSOLE NULL
991#endif /* CONFIG_SERIAL_STM32_CONSOLE */
992
993static struct uart_driver stm32_usart_driver = {
994 .driver_name = DRIVER_NAME,
995 .dev_name = STM32_SERIAL_NAME,
996 .major = 0,
997 .minor = 0,
998 .nr = STM32_MAX_PORTS,
999 .cons = STM32_SERIAL_CONSOLE,
1000};
1001
1002static struct platform_driver stm32_serial_driver = {
1003 .probe = stm32_serial_probe,
1004 .remove = stm32_serial_remove,
1005 .driver = {
1006 .name = DRIVER_NAME,
1007 .of_match_table = of_match_ptr(stm32_match),
1008 },
1009};
1010
1011static int __init usart_init(void)
1012{
1013 static char banner[] __initdata = "STM32 USART driver initialized";
1014 int ret;
1015
1016 pr_info("%s\n", banner);
1017
1018 ret = uart_register_driver(&stm32_usart_driver);
1019 if (ret)
1020 return ret;
1021
1022 ret = platform_driver_register(&stm32_serial_driver);
1023 if (ret)
1024 uart_unregister_driver(&stm32_usart_driver);
1025
1026 return ret;
1027}
1028
1029static void __exit usart_exit(void)
1030{
1031 platform_driver_unregister(&stm32_serial_driver);
1032 uart_unregister_driver(&stm32_usart_driver);
1033}
1034
1035module_init(usart_init);
1036module_exit(usart_exit);
1037
1038MODULE_ALIAS("platform:" DRIVER_NAME);
1039MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1040MODULE_LICENSE("GPL v2");