blob: 72c0ec145527ac5d4b75d06e067c8eadb4706a9d [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>
Fabrice Gasnier270e5a72017-07-13 15:08:30 +000029#include <linux/pm_wakeirq.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020030#include <linux/serial_core.h>
Alexandre TORGUE34891872016-09-15 18:42:40 +020031#include <linux/serial.h>
32#include <linux/spinlock.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
35#include <linux/tty.h>
Maxime Coquelin48a60922015-06-10 21:19:36 +020036
Alexandre TORGUEbc5a0b52016-09-15 18:42:35 +020037#include "stm32-usart.h"
Maxime Coquelin48a60922015-06-10 21:19:36 +020038
39static void stm32_stop_tx(struct uart_port *port);
Alexandre TORGUE34891872016-09-15 18:42:40 +020040static void stm32_transmit_chars(struct uart_port *port);
Maxime Coquelin48a60922015-06-10 21:19:36 +020041
42static inline struct stm32_port *to_stm32_port(struct uart_port *port)
43{
44 return container_of(port, struct stm32_port, port);
45}
46
47static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
48{
49 u32 val;
50
51 val = readl_relaxed(port->membase + reg);
52 val |= bits;
53 writel_relaxed(val, port->membase + reg);
54}
55
56static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
57{
58 u32 val;
59
60 val = readl_relaxed(port->membase + reg);
61 val &= ~bits;
62 writel_relaxed(val, port->membase + reg);
63}
64
Baoyou Xieb97055b2016-09-26 19:58:56 +080065static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
66 bool threaded)
Alexandre TORGUE34891872016-09-15 18:42:40 +020067{
68 struct stm32_port *stm32_port = to_stm32_port(port);
69 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
70 enum dma_status status;
71 struct dma_tx_state state;
72
73 *sr = readl_relaxed(port->membase + ofs->isr);
74
75 if (threaded && stm32_port->rx_ch) {
76 status = dmaengine_tx_status(stm32_port->rx_ch,
77 stm32_port->rx_ch->cookie,
78 &state);
79 if ((status == DMA_IN_PROGRESS) &&
80 (*last_res != state.residue))
81 return 1;
82 else
83 return 0;
84 } else if (*sr & USART_SR_RXNE) {
85 return 1;
86 }
87 return 0;
88}
89
Baoyou Xieb97055b2016-09-26 19:58:56 +080090static unsigned long
91stm32_get_char(struct uart_port *port, u32 *sr, int *last_res)
Alexandre TORGUE34891872016-09-15 18:42:40 +020092{
93 struct stm32_port *stm32_port = to_stm32_port(port);
94 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
95 unsigned long c;
96
97 if (stm32_port->rx_ch) {
98 c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
99 if ((*last_res) == 0)
100 *last_res = RX_BUF_L;
101 return c;
102 } else {
103 return readl_relaxed(port->membase + ofs->rdr);
104 }
105}
106
107static void stm32_receive_chars(struct uart_port *port, bool threaded)
Maxime Coquelin48a60922015-06-10 21:19:36 +0200108{
109 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200110 struct stm32_port *stm32_port = to_stm32_port(port);
111 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200112 unsigned long c;
113 u32 sr;
114 char flag;
115
116 if (port->irq_wake)
117 pm_wakeup_event(tport->tty->dev, 0);
118
Gerald Baezae5707912017-07-13 15:08:27 +0000119 while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
Maxime Coquelin48a60922015-06-10 21:19:36 +0200120 sr |= USART_SR_DUMMY_RX;
Gerald Baezae5707912017-07-13 15:08:27 +0000121 c = stm32_get_char(port, &sr, &stm32_port->last_res);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200122 flag = TTY_NORMAL;
123 port->icount.rx++;
124
125 if (sr & USART_SR_ERR_MASK) {
126 if (sr & USART_SR_LBD) {
127 port->icount.brk++;
128 if (uart_handle_break(port))
129 continue;
130 } else if (sr & USART_SR_ORE) {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200131 if (ofs->icr != UNDEF_REG)
132 writel_relaxed(USART_ICR_ORECF,
133 port->membase +
134 ofs->icr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200135 port->icount.overrun++;
136 } else if (sr & USART_SR_PE) {
137 port->icount.parity++;
138 } else if (sr & USART_SR_FE) {
139 port->icount.frame++;
140 }
141
142 sr &= port->read_status_mask;
143
144 if (sr & USART_SR_LBD)
145 flag = TTY_BREAK;
146 else if (sr & USART_SR_PE)
147 flag = TTY_PARITY;
148 else if (sr & USART_SR_FE)
149 flag = TTY_FRAME;
150 }
151
152 if (uart_handle_sysrq_char(port, c))
153 continue;
154 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
155 }
156
157 spin_unlock(&port->lock);
158 tty_flip_buffer_push(tport);
159 spin_lock(&port->lock);
160}
161
Alexandre TORGUE34891872016-09-15 18:42:40 +0200162static void stm32_tx_dma_complete(void *arg)
163{
164 struct uart_port *port = arg;
165 struct stm32_port *stm32port = to_stm32_port(port);
166 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
167 unsigned int isr;
168 int ret;
169
170 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
171 isr,
172 (isr & USART_SR_TC),
173 10, 100000);
174
175 if (ret)
176 dev_err(port->dev, "terminal count not set\n");
177
178 if (ofs->icr == UNDEF_REG)
179 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
180 else
181 stm32_set_bits(port, ofs->icr, USART_CR_TC);
182
183 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
184 stm32port->tx_dma_busy = false;
185
186 /* Let's see if we have pending data to send */
187 stm32_transmit_chars(port);
188}
189
190static void stm32_transmit_chars_pio(struct uart_port *port)
191{
192 struct stm32_port *stm32_port = to_stm32_port(port);
193 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
194 struct circ_buf *xmit = &port->state->xmit;
195 unsigned int isr;
196 int ret;
197
198 if (stm32_port->tx_dma_busy) {
199 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
200 stm32_port->tx_dma_busy = false;
201 }
202
203 ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
204 isr,
205 (isr & USART_SR_TXE),
206 10, 100);
207
208 if (ret)
209 dev_err(port->dev, "tx empty not set\n");
210
211 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
212
213 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
214 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
215 port->icount.tx++;
216}
217
218static void stm32_transmit_chars_dma(struct uart_port *port)
219{
220 struct stm32_port *stm32port = to_stm32_port(port);
221 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
222 struct circ_buf *xmit = &port->state->xmit;
223 struct dma_async_tx_descriptor *desc = NULL;
224 dma_cookie_t cookie;
225 unsigned int count, i;
226
227 if (stm32port->tx_dma_busy)
228 return;
229
230 stm32port->tx_dma_busy = true;
231
232 count = uart_circ_chars_pending(xmit);
233
234 if (count > TX_BUF_L)
235 count = TX_BUF_L;
236
237 if (xmit->tail < xmit->head) {
238 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
239 } else {
240 size_t one = UART_XMIT_SIZE - xmit->tail;
241 size_t two;
242
243 if (one > count)
244 one = count;
245 two = count - one;
246
247 memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
248 if (two)
249 memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
250 }
251
252 desc = dmaengine_prep_slave_single(stm32port->tx_ch,
253 stm32port->tx_dma_buf,
254 count,
255 DMA_MEM_TO_DEV,
256 DMA_PREP_INTERRUPT);
257
258 if (!desc) {
259 for (i = count; i > 0; i--)
260 stm32_transmit_chars_pio(port);
261 return;
262 }
263
264 desc->callback = stm32_tx_dma_complete;
265 desc->callback_param = port;
266
267 /* Push current DMA TX transaction in the pending queue */
268 cookie = dmaengine_submit(desc);
269
270 /* Issue pending DMA TX requests */
271 dma_async_issue_pending(stm32port->tx_ch);
272
273 stm32_clr_bits(port, ofs->isr, USART_SR_TC);
274 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
275
276 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
277 port->icount.tx += count;
278}
279
Maxime Coquelin48a60922015-06-10 21:19:36 +0200280static void stm32_transmit_chars(struct uart_port *port)
281{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200282 struct stm32_port *stm32_port = to_stm32_port(port);
283 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200284 struct circ_buf *xmit = &port->state->xmit;
285
286 if (port->x_char) {
Alexandre TORGUE34891872016-09-15 18:42:40 +0200287 if (stm32_port->tx_dma_busy)
288 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200289 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200290 port->x_char = 0;
291 port->icount.tx++;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200292 if (stm32_port->tx_dma_busy)
293 stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200294 return;
295 }
296
297 if (uart_tx_stopped(port)) {
298 stm32_stop_tx(port);
299 return;
300 }
301
302 if (uart_circ_empty(xmit)) {
303 stm32_stop_tx(port);
304 return;
305 }
306
Alexandre TORGUE34891872016-09-15 18:42:40 +0200307 if (stm32_port->tx_ch)
308 stm32_transmit_chars_dma(port);
309 else
310 stm32_transmit_chars_pio(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200311
312 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
313 uart_write_wakeup(port);
314
315 if (uart_circ_empty(xmit))
316 stm32_stop_tx(port);
317}
318
319static irqreturn_t stm32_interrupt(int irq, void *ptr)
320{
321 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200322 struct stm32_port *stm32_port = to_stm32_port(port);
323 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200324 u32 sr;
325
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200326 spin_lock(&port->lock);
327
Alexandre TORGUEada86182016-09-15 18:42:33 +0200328 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200329
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000330 if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
331 writel_relaxed(USART_ICR_WUCF,
332 port->membase + ofs->icr);
333
Alexandre TORGUE34891872016-09-15 18:42:40 +0200334 if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
335 stm32_receive_chars(port, false);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200336
Alexandre TORGUE34891872016-09-15 18:42:40 +0200337 if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200338 stm32_transmit_chars(port);
339
Alexandre TORGUE01d32d72016-09-15 18:42:41 +0200340 spin_unlock(&port->lock);
341
Alexandre TORGUE34891872016-09-15 18:42:40 +0200342 if (stm32_port->rx_ch)
343 return IRQ_WAKE_THREAD;
344 else
345 return IRQ_HANDLED;
346}
347
348static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
349{
350 struct uart_port *port = ptr;
351 struct stm32_port *stm32_port = to_stm32_port(port);
352
353 spin_lock(&port->lock);
354
355 if (stm32_port->rx_ch)
356 stm32_receive_chars(port, true);
357
Maxime Coquelin48a60922015-06-10 21:19:36 +0200358 spin_unlock(&port->lock);
359
360 return IRQ_HANDLED;
361}
362
363static unsigned int stm32_tx_empty(struct uart_port *port)
364{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200365 struct stm32_port *stm32_port = to_stm32_port(port);
366 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
367
368 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200369}
370
371static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
372{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200373 struct stm32_port *stm32_port = to_stm32_port(port);
374 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
375
Maxime Coquelin48a60922015-06-10 21:19:36 +0200376 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Alexandre TORGUEada86182016-09-15 18:42:33 +0200377 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200378 else
Alexandre TORGUEada86182016-09-15 18:42:33 +0200379 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200380}
381
382static unsigned int stm32_get_mctrl(struct uart_port *port)
383{
384 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
385 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
386}
387
388/* Transmit stop */
389static void stm32_stop_tx(struct uart_port *port)
390{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200391 struct stm32_port *stm32_port = to_stm32_port(port);
392 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
393
394 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200395}
396
397/* There are probably characters waiting to be transmitted. */
398static void stm32_start_tx(struct uart_port *port)
399{
400 struct circ_buf *xmit = &port->state->xmit;
401
402 if (uart_circ_empty(xmit))
403 return;
404
Alexandre TORGUE34891872016-09-15 18:42:40 +0200405 stm32_transmit_chars(port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200406}
407
408/* Throttle the remote when input buffer is about to overflow. */
409static void stm32_throttle(struct uart_port *port)
410{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200411 struct stm32_port *stm32_port = to_stm32_port(port);
412 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200413 unsigned long flags;
414
415 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200416 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200417 spin_unlock_irqrestore(&port->lock, flags);
418}
419
420/* Unthrottle the remote, the input buffer can now accept data. */
421static void stm32_unthrottle(struct uart_port *port)
422{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200423 struct stm32_port *stm32_port = to_stm32_port(port);
424 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200425 unsigned long flags;
426
427 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200428 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200429 spin_unlock_irqrestore(&port->lock, flags);
430}
431
432/* Receive stop */
433static void stm32_stop_rx(struct uart_port *port)
434{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200435 struct stm32_port *stm32_port = to_stm32_port(port);
436 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
437
438 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200439}
440
441/* Handle breaks - ignored by us */
442static void stm32_break_ctl(struct uart_port *port, int break_state)
443{
444}
445
446static int stm32_startup(struct uart_port *port)
447{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200448 struct stm32_port *stm32_port = to_stm32_port(port);
449 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000450 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200451 const char *name = to_platform_device(port->dev)->name;
452 u32 val;
453 int ret;
454
Alexandre TORGUE34891872016-09-15 18:42:40 +0200455 ret = request_threaded_irq(port->irq, stm32_interrupt,
456 stm32_threaded_interrupt,
457 IRQF_NO_SUSPEND, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200458 if (ret)
459 return ret;
460
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000461 if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
462 ret = dev_pm_set_dedicated_wake_irq(port->dev,
463 stm32_port->wakeirq);
464 if (ret) {
465 free_irq(port->irq, port);
466 return ret;
467 }
468 }
469
Maxime Coquelin48a60922015-06-10 21:19:36 +0200470 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Gerald Baeza351a7622017-07-13 15:08:30 +0000471 if (stm32_port->fifoen)
472 val |= USART_CR1_FIFOEN;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200473 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200474
475 return 0;
476}
477
478static void stm32_shutdown(struct uart_port *port)
479{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200480 struct stm32_port *stm32_port = to_stm32_port(port);
481 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200482 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200483 u32 val;
484
485 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200486 val |= BIT(cfg->uart_enable_bit);
Gerald Baeza351a7622017-07-13 15:08:30 +0000487 if (stm32_port->fifoen)
488 val |= USART_CR1_FIFOEN;
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200489 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200490
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000491 dev_pm_clear_wake_irq(port->dev);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200492 free_irq(port->irq, port);
493}
494
495static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
496 struct ktermios *old)
497{
498 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200499 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
500 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200501 unsigned int baud;
502 u32 usartdiv, mantissa, fraction, oversampling;
503 tcflag_t cflag = termios->c_cflag;
504 u32 cr1, cr2, cr3;
505 unsigned long flags;
506
507 if (!stm32_port->hw_flow_control)
508 cflag &= ~CRTSCTS;
509
510 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
511
512 spin_lock_irqsave(&port->lock, flags);
513
514 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200515 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200516
Alexandre TORGUEada86182016-09-15 18:42:33 +0200517 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
518 cr1 |= BIT(cfg->uart_enable_bit);
Gerald Baeza351a7622017-07-13 15:08:30 +0000519 if (stm32_port->fifoen)
520 cr1 |= USART_CR1_FIFOEN;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200521 cr2 = 0;
522 cr3 = 0;
523
524 if (cflag & CSTOPB)
525 cr2 |= USART_CR2_STOP_2B;
526
527 if (cflag & PARENB) {
528 cr1 |= USART_CR1_PCE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200529 if ((cflag & CSIZE) == CS8) {
530 if (cfg->has_7bits_data)
531 cr1 |= USART_CR1_M0;
532 else
533 cr1 |= USART_CR1_M;
534 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200535 }
536
537 if (cflag & PARODD)
538 cr1 |= USART_CR1_PS;
539
540 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
541 if (cflag & CRTSCTS) {
542 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Bich HEMON35abe982017-07-13 15:08:28 +0000543 cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200544 }
545
546 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
547
548 /*
549 * The USART supports 16 or 8 times oversampling.
550 * By default we prefer 16 times oversampling, so that the receiver
551 * has a better tolerance to clock deviations.
552 * 8 times oversampling is only used to achieve higher speeds.
553 */
554 if (usartdiv < 16) {
555 oversampling = 8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200556 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200557 } else {
558 oversampling = 16;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200559 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200560 }
561
562 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
563 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200564 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200565
566 uart_update_timeout(port, cflag, baud);
567
568 port->read_status_mask = USART_SR_ORE;
569 if (termios->c_iflag & INPCK)
570 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
571 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
572 port->read_status_mask |= USART_SR_LBD;
573
574 /* Characters to ignore */
575 port->ignore_status_mask = 0;
576 if (termios->c_iflag & IGNPAR)
577 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
578 if (termios->c_iflag & IGNBRK) {
579 port->ignore_status_mask |= USART_SR_LBD;
580 /*
581 * If we're ignoring parity and break indicators,
582 * ignore overruns too (for real raw support).
583 */
584 if (termios->c_iflag & IGNPAR)
585 port->ignore_status_mask |= USART_SR_ORE;
586 }
587
588 /* Ignore all characters if CREAD is not set */
589 if ((termios->c_cflag & CREAD) == 0)
590 port->ignore_status_mask |= USART_SR_DUMMY_RX;
591
Alexandre TORGUE34891872016-09-15 18:42:40 +0200592 if (stm32_port->rx_ch)
593 cr3 |= USART_CR3_DMAR;
594
Alexandre TORGUEada86182016-09-15 18:42:33 +0200595 writel_relaxed(cr3, port->membase + ofs->cr3);
596 writel_relaxed(cr2, port->membase + ofs->cr2);
597 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200598
599 spin_unlock_irqrestore(&port->lock, flags);
600}
601
602static const char *stm32_type(struct uart_port *port)
603{
604 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
605}
606
607static void stm32_release_port(struct uart_port *port)
608{
609}
610
611static int stm32_request_port(struct uart_port *port)
612{
613 return 0;
614}
615
616static void stm32_config_port(struct uart_port *port, int flags)
617{
618 if (flags & UART_CONFIG_TYPE)
619 port->type = PORT_STM32;
620}
621
622static int
623stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
624{
625 /* No user changeable parameters */
626 return -EINVAL;
627}
628
629static void stm32_pm(struct uart_port *port, unsigned int state,
630 unsigned int oldstate)
631{
632 struct stm32_port *stm32port = container_of(port,
633 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200634 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
635 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200636 unsigned long flags = 0;
637
638 switch (state) {
639 case UART_PM_STATE_ON:
640 clk_prepare_enable(stm32port->clk);
641 break;
642 case UART_PM_STATE_OFF:
643 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200644 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200645 spin_unlock_irqrestore(&port->lock, flags);
646 clk_disable_unprepare(stm32port->clk);
647 break;
648 }
649}
650
651static const struct uart_ops stm32_uart_ops = {
652 .tx_empty = stm32_tx_empty,
653 .set_mctrl = stm32_set_mctrl,
654 .get_mctrl = stm32_get_mctrl,
655 .stop_tx = stm32_stop_tx,
656 .start_tx = stm32_start_tx,
657 .throttle = stm32_throttle,
658 .unthrottle = stm32_unthrottle,
659 .stop_rx = stm32_stop_rx,
660 .break_ctl = stm32_break_ctl,
661 .startup = stm32_startup,
662 .shutdown = stm32_shutdown,
663 .set_termios = stm32_set_termios,
664 .pm = stm32_pm,
665 .type = stm32_type,
666 .release_port = stm32_release_port,
667 .request_port = stm32_request_port,
668 .config_port = stm32_config_port,
669 .verify_port = stm32_verify_port,
670};
671
672static int stm32_init_port(struct stm32_port *stm32port,
673 struct platform_device *pdev)
674{
675 struct uart_port *port = &stm32port->port;
676 struct resource *res;
677 int ret;
678
679 port->iotype = UPIO_MEM;
680 port->flags = UPF_BOOT_AUTOCONF;
681 port->ops = &stm32_uart_ops;
682 port->dev = &pdev->dev;
683 port->irq = platform_get_irq(pdev, 0);
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000684 stm32port->wakeirq = platform_get_irq(pdev, 1);
Gerald Baeza351a7622017-07-13 15:08:30 +0000685 stm32port->fifoen = stm32port->info->cfg.has_fifo;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200686
687 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
688 port->membase = devm_ioremap_resource(&pdev->dev, res);
689 if (IS_ERR(port->membase))
690 return PTR_ERR(port->membase);
691 port->mapbase = res->start;
692
693 spin_lock_init(&port->lock);
694
695 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
696 if (IS_ERR(stm32port->clk))
697 return PTR_ERR(stm32port->clk);
698
699 /* Ensure that clk rate is correct by enabling the clk */
700 ret = clk_prepare_enable(stm32port->clk);
701 if (ret)
702 return ret;
703
704 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
Fabrice Gasnierada80042017-07-13 15:08:29 +0000705 if (!stm32port->port.uartclk) {
706 clk_disable_unprepare(stm32port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200707 ret = -EINVAL;
Fabrice Gasnierada80042017-07-13 15:08:29 +0000708 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200709
Maxime Coquelin48a60922015-06-10 21:19:36 +0200710 return ret;
711}
712
713static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
714{
715 struct device_node *np = pdev->dev.of_node;
716 int id;
717
718 if (!np)
719 return NULL;
720
721 id = of_alias_get_id(np, "serial");
Gerald Baezae5707912017-07-13 15:08:27 +0000722 if (id < 0) {
723 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
724 return NULL;
725 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200726
727 if (WARN_ON(id >= STM32_MAX_PORTS))
728 return NULL;
729
730 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200731 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200732 stm32_ports[id].port.line = id;
Gerald Baezae5707912017-07-13 15:08:27 +0000733 stm32_ports[id].last_res = RX_BUF_L;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200734 return &stm32_ports[id];
735}
736
737#ifdef CONFIG_OF
738static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200739 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
740 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
741 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
742 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000743 { .compatible = "st,stm32h7-usart", .data = &stm32h7_info},
744 { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200745 {},
746};
747
748MODULE_DEVICE_TABLE(of, stm32_match);
749#endif
750
Alexandre TORGUE34891872016-09-15 18:42:40 +0200751static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
752 struct platform_device *pdev)
753{
754 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
755 struct uart_port *port = &stm32port->port;
756 struct device *dev = &pdev->dev;
757 struct dma_slave_config config;
758 struct dma_async_tx_descriptor *desc = NULL;
759 dma_cookie_t cookie;
760 int ret;
761
762 /* Request DMA RX channel */
763 stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
764 if (!stm32port->rx_ch) {
765 dev_info(dev, "rx dma alloc failed\n");
766 return -ENODEV;
767 }
768 stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
769 &stm32port->rx_dma_buf,
770 GFP_KERNEL);
771 if (!stm32port->rx_buf) {
772 ret = -ENOMEM;
773 goto alloc_err;
774 }
775
776 /* Configure DMA channel */
777 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200778 config.src_addr = port->mapbase + ofs->rdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200779 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
780
781 ret = dmaengine_slave_config(stm32port->rx_ch, &config);
782 if (ret < 0) {
783 dev_err(dev, "rx dma channel config failed\n");
784 ret = -ENODEV;
785 goto config_err;
786 }
787
788 /* Prepare a DMA cyclic transaction */
789 desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
790 stm32port->rx_dma_buf,
791 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
792 DMA_PREP_INTERRUPT);
793 if (!desc) {
794 dev_err(dev, "rx dma prep cyclic failed\n");
795 ret = -ENODEV;
796 goto config_err;
797 }
798
799 /* No callback as dma buffer is drained on usart interrupt */
800 desc->callback = NULL;
801 desc->callback_param = NULL;
802
803 /* Push current DMA transaction in the pending queue */
804 cookie = dmaengine_submit(desc);
805
806 /* Issue pending DMA requests */
807 dma_async_issue_pending(stm32port->rx_ch);
808
809 return 0;
810
811config_err:
812 dma_free_coherent(&pdev->dev,
813 RX_BUF_L, stm32port->rx_buf,
814 stm32port->rx_dma_buf);
815
816alloc_err:
817 dma_release_channel(stm32port->rx_ch);
818 stm32port->rx_ch = NULL;
819
820 return ret;
821}
822
823static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
824 struct platform_device *pdev)
825{
826 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
827 struct uart_port *port = &stm32port->port;
828 struct device *dev = &pdev->dev;
829 struct dma_slave_config config;
830 int ret;
831
832 stm32port->tx_dma_busy = false;
833
834 /* Request DMA TX channel */
835 stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
836 if (!stm32port->tx_ch) {
837 dev_info(dev, "tx dma alloc failed\n");
838 return -ENODEV;
839 }
840 stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
841 &stm32port->tx_dma_buf,
842 GFP_KERNEL);
843 if (!stm32port->tx_buf) {
844 ret = -ENOMEM;
845 goto alloc_err;
846 }
847
848 /* Configure DMA channel */
849 memset(&config, 0, sizeof(config));
Arnd Bergmann8e5481d2016-09-23 21:38:51 +0200850 config.dst_addr = port->mapbase + ofs->tdr;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200851 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
852
853 ret = dmaengine_slave_config(stm32port->tx_ch, &config);
854 if (ret < 0) {
855 dev_err(dev, "tx dma channel config failed\n");
856 ret = -ENODEV;
857 goto config_err;
858 }
859
860 return 0;
861
862config_err:
863 dma_free_coherent(&pdev->dev,
864 TX_BUF_L, stm32port->tx_buf,
865 stm32port->tx_dma_buf);
866
867alloc_err:
868 dma_release_channel(stm32port->tx_ch);
869 stm32port->tx_ch = NULL;
870
871 return ret;
872}
873
Maxime Coquelin48a60922015-06-10 21:19:36 +0200874static int stm32_serial_probe(struct platform_device *pdev)
875{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200876 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200877 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200878 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200879
880 stm32port = stm32_of_get_stm32_port(pdev);
881 if (!stm32port)
882 return -ENODEV;
883
Alexandre TORGUEada86182016-09-15 18:42:33 +0200884 match = of_match_device(stm32_match, &pdev->dev);
885 if (match && match->data)
886 stm32port->info = (struct stm32_usart_info *)match->data;
887 else
888 return -EINVAL;
889
Maxime Coquelin48a60922015-06-10 21:19:36 +0200890 ret = stm32_init_port(stm32port, pdev);
891 if (ret)
892 return ret;
893
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000894 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) {
895 ret = device_init_wakeup(&pdev->dev, true);
896 if (ret)
897 goto err_uninit;
898 }
899
Maxime Coquelin48a60922015-06-10 21:19:36 +0200900 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
901 if (ret)
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000902 goto err_nowup;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200903
Alexandre TORGUE34891872016-09-15 18:42:40 +0200904 ret = stm32_of_dma_rx_probe(stm32port, pdev);
905 if (ret)
906 dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
907
908 ret = stm32_of_dma_tx_probe(stm32port, pdev);
909 if (ret)
910 dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
911
Maxime Coquelin48a60922015-06-10 21:19:36 +0200912 platform_set_drvdata(pdev, &stm32port->port);
913
914 return 0;
Fabrice Gasnierada80042017-07-13 15:08:29 +0000915
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000916err_nowup:
917 if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
918 device_init_wakeup(&pdev->dev, false);
919
Fabrice Gasnierada80042017-07-13 15:08:29 +0000920err_uninit:
921 clk_disable_unprepare(stm32port->clk);
922
923 return ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200924}
925
926static int stm32_serial_remove(struct platform_device *pdev)
927{
928 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200929 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUE34891872016-09-15 18:42:40 +0200930 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000931 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Alexandre TORGUE34891872016-09-15 18:42:40 +0200932
933 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
934
935 if (stm32_port->rx_ch)
936 dma_release_channel(stm32_port->rx_ch);
937
938 if (stm32_port->rx_dma_buf)
939 dma_free_coherent(&pdev->dev,
940 RX_BUF_L, stm32_port->rx_buf,
941 stm32_port->rx_dma_buf);
942
943 stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
944
945 if (stm32_port->tx_ch)
946 dma_release_channel(stm32_port->tx_ch);
947
948 if (stm32_port->tx_dma_buf)
949 dma_free_coherent(&pdev->dev,
950 TX_BUF_L, stm32_port->tx_buf,
951 stm32_port->tx_dma_buf);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200952
Fabrice Gasnier270e5a72017-07-13 15:08:30 +0000953 if (cfg->has_wakeup && stm32_port->wakeirq >= 0)
954 device_init_wakeup(&pdev->dev, false);
955
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200956 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200957
958 return uart_remove_one_port(&stm32_usart_driver, port);
959}
960
961
962#ifdef CONFIG_SERIAL_STM32_CONSOLE
963static void stm32_console_putchar(struct uart_port *port, int ch)
964{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200965 struct stm32_port *stm32_port = to_stm32_port(port);
966 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
967
968 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200969 cpu_relax();
970
Alexandre TORGUEada86182016-09-15 18:42:33 +0200971 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200972}
973
974static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
975{
976 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200977 struct stm32_port *stm32_port = to_stm32_port(port);
978 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200979 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200980 unsigned long flags;
981 u32 old_cr1, new_cr1;
982 int locked = 1;
983
984 local_irq_save(flags);
985 if (port->sysrq)
986 locked = 0;
987 else if (oops_in_progress)
988 locked = spin_trylock(&port->lock);
989 else
990 spin_lock(&port->lock);
991
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200992 /* Save and disable interrupts, enable the transmitter */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200993 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200994 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUE87f1f802016-09-15 18:42:42 +0200995 new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200996 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200997
998 uart_console_write(port, s, cnt, stm32_console_putchar);
999
1000 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +02001001 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +02001002
1003 if (locked)
1004 spin_unlock(&port->lock);
1005 local_irq_restore(flags);
1006}
1007
1008static int stm32_console_setup(struct console *co, char *options)
1009{
1010 struct stm32_port *stm32port;
1011 int baud = 9600;
1012 int bits = 8;
1013 int parity = 'n';
1014 int flow = 'n';
1015
1016 if (co->index >= STM32_MAX_PORTS)
1017 return -ENODEV;
1018
1019 stm32port = &stm32_ports[co->index];
1020
1021 /*
1022 * This driver does not support early console initialization
1023 * (use ARM early printk support instead), so we only expect
1024 * this to be called during the uart port registration when the
1025 * driver gets probed and the port should be mapped at that point.
1026 */
1027 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
1028 return -ENXIO;
1029
1030 if (options)
1031 uart_parse_options(options, &baud, &parity, &bits, &flow);
1032
1033 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1034}
1035
1036static struct console stm32_console = {
1037 .name = STM32_SERIAL_NAME,
1038 .device = uart_console_device,
1039 .write = stm32_console_write,
1040 .setup = stm32_console_setup,
1041 .flags = CON_PRINTBUFFER,
1042 .index = -1,
1043 .data = &stm32_usart_driver,
1044};
1045
1046#define STM32_SERIAL_CONSOLE (&stm32_console)
1047
1048#else
1049#define STM32_SERIAL_CONSOLE NULL
1050#endif /* CONFIG_SERIAL_STM32_CONSOLE */
1051
1052static struct uart_driver stm32_usart_driver = {
1053 .driver_name = DRIVER_NAME,
1054 .dev_name = STM32_SERIAL_NAME,
1055 .major = 0,
1056 .minor = 0,
1057 .nr = STM32_MAX_PORTS,
1058 .cons = STM32_SERIAL_CONSOLE,
1059};
1060
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001061#ifdef CONFIG_PM_SLEEP
1062static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable)
1063{
1064 struct stm32_port *stm32_port = to_stm32_port(port);
1065 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1066 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1067 u32 val;
1068
1069 if (!cfg->has_wakeup || stm32_port->wakeirq < 0)
1070 return;
1071
1072 if (enable) {
1073 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1074 stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1075 val = readl_relaxed(port->membase + ofs->cr3);
1076 val &= ~USART_CR3_WUS_MASK;
1077 /* Enable Wake up interrupt from low power on start bit */
1078 val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1079 writel_relaxed(val, port->membase + ofs->cr3);
1080 stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1081 } else {
1082 stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1083 }
1084}
1085
1086static int stm32_serial_suspend(struct device *dev)
1087{
1088 struct uart_port *port = dev_get_drvdata(dev);
1089
1090 uart_suspend_port(&stm32_usart_driver, port);
1091
1092 if (device_may_wakeup(dev))
1093 stm32_serial_enable_wakeup(port, true);
1094 else
1095 stm32_serial_enable_wakeup(port, false);
1096
1097 return 0;
1098}
1099
1100static int stm32_serial_resume(struct device *dev)
1101{
1102 struct uart_port *port = dev_get_drvdata(dev);
1103
1104 if (device_may_wakeup(dev))
1105 stm32_serial_enable_wakeup(port, false);
1106
1107 return uart_resume_port(&stm32_usart_driver, port);
1108}
1109#endif /* CONFIG_PM_SLEEP */
1110
1111static const struct dev_pm_ops stm32_serial_pm_ops = {
1112 SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1113};
1114
Maxime Coquelin48a60922015-06-10 21:19:36 +02001115static struct platform_driver stm32_serial_driver = {
1116 .probe = stm32_serial_probe,
1117 .remove = stm32_serial_remove,
1118 .driver = {
1119 .name = DRIVER_NAME,
Fabrice Gasnier270e5a72017-07-13 15:08:30 +00001120 .pm = &stm32_serial_pm_ops,
Maxime Coquelin48a60922015-06-10 21:19:36 +02001121 .of_match_table = of_match_ptr(stm32_match),
1122 },
1123};
1124
1125static int __init usart_init(void)
1126{
1127 static char banner[] __initdata = "STM32 USART driver initialized";
1128 int ret;
1129
1130 pr_info("%s\n", banner);
1131
1132 ret = uart_register_driver(&stm32_usart_driver);
1133 if (ret)
1134 return ret;
1135
1136 ret = platform_driver_register(&stm32_serial_driver);
1137 if (ret)
1138 uart_unregister_driver(&stm32_usart_driver);
1139
1140 return ret;
1141}
1142
1143static void __exit usart_exit(void)
1144{
1145 platform_driver_unregister(&stm32_serial_driver);
1146 uart_unregister_driver(&stm32_usart_driver);
1147}
1148
1149module_init(usart_init);
1150module_exit(usart_exit);
1151
1152MODULE_ALIAS("platform:" DRIVER_NAME);
1153MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1154MODULE_LICENSE("GPL v2");