blob: 520e7defc08d43c176049593b0c7e5ff9a754145 [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
14#include <linux/module.h>
15#include <linux/serial.h>
16#include <linux/console.h>
17#include <linux/sysrq.h>
18#include <linux/platform_device.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/delay.h>
24#include <linux/spinlock.h>
25#include <linux/pm_runtime.h>
26#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/serial_core.h>
29#include <linux/clk.h>
30
Alexandre TORGUEbc5a0b52016-09-15 18:42:35 +020031#include "stm32-usart.h"
Maxime Coquelin48a60922015-06-10 21:19:36 +020032
33static void stm32_stop_tx(struct uart_port *port);
34
35static inline struct stm32_port *to_stm32_port(struct uart_port *port)
36{
37 return container_of(port, struct stm32_port, port);
38}
39
40static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
41{
42 u32 val;
43
44 val = readl_relaxed(port->membase + reg);
45 val |= bits;
46 writel_relaxed(val, port->membase + reg);
47}
48
49static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
50{
51 u32 val;
52
53 val = readl_relaxed(port->membase + reg);
54 val &= ~bits;
55 writel_relaxed(val, port->membase + reg);
56}
57
58static void stm32_receive_chars(struct uart_port *port)
59{
60 struct tty_port *tport = &port->state->port;
Alexandre TORGUEada86182016-09-15 18:42:33 +020061 struct stm32_port *stm32_port = to_stm32_port(port);
62 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +020063 unsigned long c;
64 u32 sr;
65 char flag;
66
67 if (port->irq_wake)
68 pm_wakeup_event(tport->tty->dev, 0);
69
Alexandre TORGUEada86182016-09-15 18:42:33 +020070 while ((sr = readl_relaxed(port->membase + ofs->isr)) & USART_SR_RXNE) {
Maxime Coquelin48a60922015-06-10 21:19:36 +020071 sr |= USART_SR_DUMMY_RX;
Alexandre TORGUEada86182016-09-15 18:42:33 +020072 c = readl_relaxed(port->membase + ofs->rdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +020073 flag = TTY_NORMAL;
74 port->icount.rx++;
75
76 if (sr & USART_SR_ERR_MASK) {
77 if (sr & USART_SR_LBD) {
78 port->icount.brk++;
79 if (uart_handle_break(port))
80 continue;
81 } else if (sr & USART_SR_ORE) {
Alexandre TORGUEada86182016-09-15 18:42:33 +020082 if (ofs->icr != UNDEF_REG)
83 writel_relaxed(USART_ICR_ORECF,
84 port->membase +
85 ofs->icr);
Maxime Coquelin48a60922015-06-10 21:19:36 +020086 port->icount.overrun++;
87 } else if (sr & USART_SR_PE) {
88 port->icount.parity++;
89 } else if (sr & USART_SR_FE) {
90 port->icount.frame++;
91 }
92
93 sr &= port->read_status_mask;
94
95 if (sr & USART_SR_LBD)
96 flag = TTY_BREAK;
97 else if (sr & USART_SR_PE)
98 flag = TTY_PARITY;
99 else if (sr & USART_SR_FE)
100 flag = TTY_FRAME;
101 }
102
103 if (uart_handle_sysrq_char(port, c))
104 continue;
105 uart_insert_char(port, sr, USART_SR_ORE, c, flag);
106 }
107
108 spin_unlock(&port->lock);
109 tty_flip_buffer_push(tport);
110 spin_lock(&port->lock);
111}
112
113static void stm32_transmit_chars(struct uart_port *port)
114{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200115 struct stm32_port *stm32_port = to_stm32_port(port);
116 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200117 struct circ_buf *xmit = &port->state->xmit;
118
119 if (port->x_char) {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200120 writel_relaxed(port->x_char, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200121 port->x_char = 0;
122 port->icount.tx++;
123 return;
124 }
125
126 if (uart_tx_stopped(port)) {
127 stm32_stop_tx(port);
128 return;
129 }
130
131 if (uart_circ_empty(xmit)) {
132 stm32_stop_tx(port);
133 return;
134 }
135
Alexandre TORGUEada86182016-09-15 18:42:33 +0200136 writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200137 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
138 port->icount.tx++;
139
140 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
141 uart_write_wakeup(port);
142
143 if (uart_circ_empty(xmit))
144 stm32_stop_tx(port);
145}
146
147static irqreturn_t stm32_interrupt(int irq, void *ptr)
148{
149 struct uart_port *port = ptr;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200150 struct stm32_port *stm32_port = to_stm32_port(port);
151 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200152 u32 sr;
153
154 spin_lock(&port->lock);
155
Alexandre TORGUEada86182016-09-15 18:42:33 +0200156 sr = readl_relaxed(port->membase + ofs->isr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200157
158 if (sr & USART_SR_RXNE)
159 stm32_receive_chars(port);
160
161 if (sr & USART_SR_TXE)
162 stm32_transmit_chars(port);
163
164 spin_unlock(&port->lock);
165
166 return IRQ_HANDLED;
167}
168
169static unsigned int stm32_tx_empty(struct uart_port *port)
170{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200171 struct stm32_port *stm32_port = to_stm32_port(port);
172 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
173
174 return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200175}
176
177static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
178{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200179 struct stm32_port *stm32_port = to_stm32_port(port);
180 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
181
Maxime Coquelin48a60922015-06-10 21:19:36 +0200182 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
Alexandre TORGUEada86182016-09-15 18:42:33 +0200183 stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200184 else
Alexandre TORGUEada86182016-09-15 18:42:33 +0200185 stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200186}
187
188static unsigned int stm32_get_mctrl(struct uart_port *port)
189{
190 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
191 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
192}
193
194/* Transmit stop */
195static void stm32_stop_tx(struct uart_port *port)
196{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200197 struct stm32_port *stm32_port = to_stm32_port(port);
198 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
199
200 stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200201}
202
203/* There are probably characters waiting to be transmitted. */
204static void stm32_start_tx(struct uart_port *port)
205{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200206 struct stm32_port *stm32_port = to_stm32_port(port);
207 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200208 struct circ_buf *xmit = &port->state->xmit;
209
210 if (uart_circ_empty(xmit))
211 return;
212
Alexandre TORGUEada86182016-09-15 18:42:33 +0200213 stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE | USART_CR1_TE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200214}
215
216/* Throttle the remote when input buffer is about to overflow. */
217static void stm32_throttle(struct uart_port *port)
218{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200219 struct stm32_port *stm32_port = to_stm32_port(port);
220 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200221 unsigned long flags;
222
223 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200224 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200225 spin_unlock_irqrestore(&port->lock, flags);
226}
227
228/* Unthrottle the remote, the input buffer can now accept data. */
229static void stm32_unthrottle(struct uart_port *port)
230{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200231 struct stm32_port *stm32_port = to_stm32_port(port);
232 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200233 unsigned long flags;
234
235 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200236 stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200237 spin_unlock_irqrestore(&port->lock, flags);
238}
239
240/* Receive stop */
241static void stm32_stop_rx(struct uart_port *port)
242{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200243 struct stm32_port *stm32_port = to_stm32_port(port);
244 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
245
246 stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200247}
248
249/* Handle breaks - ignored by us */
250static void stm32_break_ctl(struct uart_port *port, int break_state)
251{
252}
253
254static int stm32_startup(struct uart_port *port)
255{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200256 struct stm32_port *stm32_port = to_stm32_port(port);
257 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200258 const char *name = to_platform_device(port->dev)->name;
259 u32 val;
260 int ret;
261
Sudeep Holla616ea8d2015-09-21 16:47:06 +0100262 ret = request_irq(port->irq, stm32_interrupt, 0, name, port);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200263 if (ret)
264 return ret;
265
266 val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200267 stm32_set_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200268
269 return 0;
270}
271
272static void stm32_shutdown(struct uart_port *port)
273{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200274 struct stm32_port *stm32_port = to_stm32_port(port);
275 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200276 u32 val;
277
278 val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
Alexandre TORGUEa14f66a2016-09-15 18:42:36 +0200279 stm32_clr_bits(port, ofs->cr1, val);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200280
281 free_irq(port->irq, port);
282}
283
284static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
285 struct ktermios *old)
286{
287 struct stm32_port *stm32_port = to_stm32_port(port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200288 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
289 struct stm32_usart_config *cfg = &stm32_port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200290 unsigned int baud;
291 u32 usartdiv, mantissa, fraction, oversampling;
292 tcflag_t cflag = termios->c_cflag;
293 u32 cr1, cr2, cr3;
294 unsigned long flags;
295
296 if (!stm32_port->hw_flow_control)
297 cflag &= ~CRTSCTS;
298
299 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
300
301 spin_lock_irqsave(&port->lock, flags);
302
303 /* Stop serial port and reset value */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200304 writel_relaxed(0, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200305
Alexandre TORGUEada86182016-09-15 18:42:33 +0200306 cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
307 cr1 |= BIT(cfg->uart_enable_bit);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200308 cr2 = 0;
309 cr3 = 0;
310
311 if (cflag & CSTOPB)
312 cr2 |= USART_CR2_STOP_2B;
313
314 if (cflag & PARENB) {
315 cr1 |= USART_CR1_PCE;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200316 if ((cflag & CSIZE) == CS8) {
317 if (cfg->has_7bits_data)
318 cr1 |= USART_CR1_M0;
319 else
320 cr1 |= USART_CR1_M;
321 }
Maxime Coquelin48a60922015-06-10 21:19:36 +0200322 }
323
324 if (cflag & PARODD)
325 cr1 |= USART_CR1_PS;
326
327 port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
328 if (cflag & CRTSCTS) {
329 port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
330 cr3 |= USART_CR3_CTSE;
331 }
332
333 usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
334
335 /*
336 * The USART supports 16 or 8 times oversampling.
337 * By default we prefer 16 times oversampling, so that the receiver
338 * has a better tolerance to clock deviations.
339 * 8 times oversampling is only used to achieve higher speeds.
340 */
341 if (usartdiv < 16) {
342 oversampling = 8;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200343 stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200344 } else {
345 oversampling = 16;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200346 stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200347 }
348
349 mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
350 fraction = usartdiv % oversampling;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200351 writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200352
353 uart_update_timeout(port, cflag, baud);
354
355 port->read_status_mask = USART_SR_ORE;
356 if (termios->c_iflag & INPCK)
357 port->read_status_mask |= USART_SR_PE | USART_SR_FE;
358 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
359 port->read_status_mask |= USART_SR_LBD;
360
361 /* Characters to ignore */
362 port->ignore_status_mask = 0;
363 if (termios->c_iflag & IGNPAR)
364 port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
365 if (termios->c_iflag & IGNBRK) {
366 port->ignore_status_mask |= USART_SR_LBD;
367 /*
368 * If we're ignoring parity and break indicators,
369 * ignore overruns too (for real raw support).
370 */
371 if (termios->c_iflag & IGNPAR)
372 port->ignore_status_mask |= USART_SR_ORE;
373 }
374
375 /* Ignore all characters if CREAD is not set */
376 if ((termios->c_cflag & CREAD) == 0)
377 port->ignore_status_mask |= USART_SR_DUMMY_RX;
378
Alexandre TORGUEada86182016-09-15 18:42:33 +0200379 writel_relaxed(cr3, port->membase + ofs->cr3);
380 writel_relaxed(cr2, port->membase + ofs->cr2);
381 writel_relaxed(cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200382
383 spin_unlock_irqrestore(&port->lock, flags);
384}
385
386static const char *stm32_type(struct uart_port *port)
387{
388 return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
389}
390
391static void stm32_release_port(struct uart_port *port)
392{
393}
394
395static int stm32_request_port(struct uart_port *port)
396{
397 return 0;
398}
399
400static void stm32_config_port(struct uart_port *port, int flags)
401{
402 if (flags & UART_CONFIG_TYPE)
403 port->type = PORT_STM32;
404}
405
406static int
407stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
408{
409 /* No user changeable parameters */
410 return -EINVAL;
411}
412
413static void stm32_pm(struct uart_port *port, unsigned int state,
414 unsigned int oldstate)
415{
416 struct stm32_port *stm32port = container_of(port,
417 struct stm32_port, port);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200418 struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
419 struct stm32_usart_config *cfg = &stm32port->info->cfg;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200420 unsigned long flags = 0;
421
422 switch (state) {
423 case UART_PM_STATE_ON:
424 clk_prepare_enable(stm32port->clk);
425 break;
426 case UART_PM_STATE_OFF:
427 spin_lock_irqsave(&port->lock, flags);
Alexandre TORGUEada86182016-09-15 18:42:33 +0200428 stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
Maxime Coquelin48a60922015-06-10 21:19:36 +0200429 spin_unlock_irqrestore(&port->lock, flags);
430 clk_disable_unprepare(stm32port->clk);
431 break;
432 }
433}
434
435static const struct uart_ops stm32_uart_ops = {
436 .tx_empty = stm32_tx_empty,
437 .set_mctrl = stm32_set_mctrl,
438 .get_mctrl = stm32_get_mctrl,
439 .stop_tx = stm32_stop_tx,
440 .start_tx = stm32_start_tx,
441 .throttle = stm32_throttle,
442 .unthrottle = stm32_unthrottle,
443 .stop_rx = stm32_stop_rx,
444 .break_ctl = stm32_break_ctl,
445 .startup = stm32_startup,
446 .shutdown = stm32_shutdown,
447 .set_termios = stm32_set_termios,
448 .pm = stm32_pm,
449 .type = stm32_type,
450 .release_port = stm32_release_port,
451 .request_port = stm32_request_port,
452 .config_port = stm32_config_port,
453 .verify_port = stm32_verify_port,
454};
455
456static int stm32_init_port(struct stm32_port *stm32port,
457 struct platform_device *pdev)
458{
459 struct uart_port *port = &stm32port->port;
460 struct resource *res;
461 int ret;
462
463 port->iotype = UPIO_MEM;
464 port->flags = UPF_BOOT_AUTOCONF;
465 port->ops = &stm32_uart_ops;
466 port->dev = &pdev->dev;
467 port->irq = platform_get_irq(pdev, 0);
468
469 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
470 port->membase = devm_ioremap_resource(&pdev->dev, res);
471 if (IS_ERR(port->membase))
472 return PTR_ERR(port->membase);
473 port->mapbase = res->start;
474
475 spin_lock_init(&port->lock);
476
477 stm32port->clk = devm_clk_get(&pdev->dev, NULL);
478 if (IS_ERR(stm32port->clk))
479 return PTR_ERR(stm32port->clk);
480
481 /* Ensure that clk rate is correct by enabling the clk */
482 ret = clk_prepare_enable(stm32port->clk);
483 if (ret)
484 return ret;
485
486 stm32port->port.uartclk = clk_get_rate(stm32port->clk);
487 if (!stm32port->port.uartclk)
488 ret = -EINVAL;
489
Maxime Coquelin48a60922015-06-10 21:19:36 +0200490 return ret;
491}
492
493static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
494{
495 struct device_node *np = pdev->dev.of_node;
496 int id;
497
498 if (!np)
499 return NULL;
500
501 id = of_alias_get_id(np, "serial");
502 if (id < 0)
503 id = 0;
504
505 if (WARN_ON(id >= STM32_MAX_PORTS))
506 return NULL;
507
508 stm32_ports[id].hw_flow_control = of_property_read_bool(np,
Alexandre TORGUE59bed2d2016-09-15 18:42:37 +0200509 "st,hw-flow-ctrl");
Maxime Coquelin48a60922015-06-10 21:19:36 +0200510 stm32_ports[id].port.line = id;
511 return &stm32_ports[id];
512}
513
514#ifdef CONFIG_OF
515static const struct of_device_id stm32_match[] = {
Alexandre TORGUEada86182016-09-15 18:42:33 +0200516 { .compatible = "st,stm32-usart", .data = &stm32f4_info},
517 { .compatible = "st,stm32-uart", .data = &stm32f4_info},
518 { .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
519 { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
Maxime Coquelin48a60922015-06-10 21:19:36 +0200520 {},
521};
522
523MODULE_DEVICE_TABLE(of, stm32_match);
524#endif
525
526static int stm32_serial_probe(struct platform_device *pdev)
527{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200528 const struct of_device_id *match;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200529 struct stm32_port *stm32port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200530 int ret;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200531
532 stm32port = stm32_of_get_stm32_port(pdev);
533 if (!stm32port)
534 return -ENODEV;
535
Alexandre TORGUEada86182016-09-15 18:42:33 +0200536 match = of_match_device(stm32_match, &pdev->dev);
537 if (match && match->data)
538 stm32port->info = (struct stm32_usart_info *)match->data;
539 else
540 return -EINVAL;
541
Maxime Coquelin48a60922015-06-10 21:19:36 +0200542 ret = stm32_init_port(stm32port, pdev);
543 if (ret)
544 return ret;
545
546 ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
547 if (ret)
548 return ret;
549
550 platform_set_drvdata(pdev, &stm32port->port);
551
552 return 0;
553}
554
555static int stm32_serial_remove(struct platform_device *pdev)
556{
557 struct uart_port *port = platform_get_drvdata(pdev);
Alexandre TORGUE511c7b12016-09-15 18:42:38 +0200558 struct stm32_port *stm32_port = to_stm32_port(port);
559
560 clk_disable_unprepare(stm32_port->clk);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200561
562 return uart_remove_one_port(&stm32_usart_driver, port);
563}
564
565
566#ifdef CONFIG_SERIAL_STM32_CONSOLE
567static void stm32_console_putchar(struct uart_port *port, int ch)
568{
Alexandre TORGUEada86182016-09-15 18:42:33 +0200569 struct stm32_port *stm32_port = to_stm32_port(port);
570 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
571
572 while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
Maxime Coquelin48a60922015-06-10 21:19:36 +0200573 cpu_relax();
574
Alexandre TORGUEada86182016-09-15 18:42:33 +0200575 writel_relaxed(ch, port->membase + ofs->tdr);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200576}
577
578static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
579{
580 struct uart_port *port = &stm32_ports[co->index].port;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200581 struct stm32_port *stm32_port = to_stm32_port(port);
582 struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
Maxime Coquelin48a60922015-06-10 21:19:36 +0200583 unsigned long flags;
584 u32 old_cr1, new_cr1;
585 int locked = 1;
586
587 local_irq_save(flags);
588 if (port->sysrq)
589 locked = 0;
590 else if (oops_in_progress)
591 locked = spin_trylock(&port->lock);
592 else
593 spin_lock(&port->lock);
594
595 /* Save and disable interrupts */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200596 old_cr1 = readl_relaxed(port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200597 new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
Alexandre TORGUEada86182016-09-15 18:42:33 +0200598 writel_relaxed(new_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200599
600 uart_console_write(port, s, cnt, stm32_console_putchar);
601
602 /* Restore interrupt state */
Alexandre TORGUEada86182016-09-15 18:42:33 +0200603 writel_relaxed(old_cr1, port->membase + ofs->cr1);
Maxime Coquelin48a60922015-06-10 21:19:36 +0200604
605 if (locked)
606 spin_unlock(&port->lock);
607 local_irq_restore(flags);
608}
609
610static int stm32_console_setup(struct console *co, char *options)
611{
612 struct stm32_port *stm32port;
613 int baud = 9600;
614 int bits = 8;
615 int parity = 'n';
616 int flow = 'n';
617
618 if (co->index >= STM32_MAX_PORTS)
619 return -ENODEV;
620
621 stm32port = &stm32_ports[co->index];
622
623 /*
624 * This driver does not support early console initialization
625 * (use ARM early printk support instead), so we only expect
626 * this to be called during the uart port registration when the
627 * driver gets probed and the port should be mapped at that point.
628 */
629 if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
630 return -ENXIO;
631
632 if (options)
633 uart_parse_options(options, &baud, &parity, &bits, &flow);
634
635 return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
636}
637
638static struct console stm32_console = {
639 .name = STM32_SERIAL_NAME,
640 .device = uart_console_device,
641 .write = stm32_console_write,
642 .setup = stm32_console_setup,
643 .flags = CON_PRINTBUFFER,
644 .index = -1,
645 .data = &stm32_usart_driver,
646};
647
648#define STM32_SERIAL_CONSOLE (&stm32_console)
649
650#else
651#define STM32_SERIAL_CONSOLE NULL
652#endif /* CONFIG_SERIAL_STM32_CONSOLE */
653
654static struct uart_driver stm32_usart_driver = {
655 .driver_name = DRIVER_NAME,
656 .dev_name = STM32_SERIAL_NAME,
657 .major = 0,
658 .minor = 0,
659 .nr = STM32_MAX_PORTS,
660 .cons = STM32_SERIAL_CONSOLE,
661};
662
663static struct platform_driver stm32_serial_driver = {
664 .probe = stm32_serial_probe,
665 .remove = stm32_serial_remove,
666 .driver = {
667 .name = DRIVER_NAME,
668 .of_match_table = of_match_ptr(stm32_match),
669 },
670};
671
672static int __init usart_init(void)
673{
674 static char banner[] __initdata = "STM32 USART driver initialized";
675 int ret;
676
677 pr_info("%s\n", banner);
678
679 ret = uart_register_driver(&stm32_usart_driver);
680 if (ret)
681 return ret;
682
683 ret = platform_driver_register(&stm32_serial_driver);
684 if (ret)
685 uart_unregister_driver(&stm32_usart_driver);
686
687 return ret;
688}
689
690static void __exit usart_exit(void)
691{
692 platform_driver_unregister(&stm32_serial_driver);
693 uart_unregister_driver(&stm32_usart_driver);
694}
695
696module_init(usart_init);
697module_exit(usart_exit);
698
699MODULE_ALIAS("platform:" DRIVER_NAME);
700MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
701MODULE_LICENSE("GPL v2");