2 * Copyright (C) Maxime Coquelin 2015
3 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * License terms: GNU General Public License (GPL), version 2
6 * Inspired by st-asc.c from STMicroelectronics (c)
9 #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
13 #include <linux/module.h>
14 #include <linux/serial.h>
15 #include <linux/console.h>
16 #include <linux/sysrq.h>
17 #include <linux/platform_device.h>
19 #include <linux/irq.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/pm_runtime.h>
26 #include <linux/of_platform.h>
27 #include <linux/serial_core.h>
28 #include <linux/clk.h>
30 #define DRIVER_NAME "stm32-usart"
32 /* Register offsets */
35 #define USART_BRR 0x08
36 #define USART_CR1 0x0c
37 #define USART_CR2 0x10
38 #define USART_CR3 0x14
39 #define USART_GTPR 0x18
42 #define USART_SR_PE BIT(0)
43 #define USART_SR_FE BIT(1)
44 #define USART_SR_NF BIT(2)
45 #define USART_SR_ORE BIT(3)
46 #define USART_SR_IDLE BIT(4)
47 #define USART_SR_RXNE BIT(5)
48 #define USART_SR_TC BIT(6)
49 #define USART_SR_TXE BIT(7)
50 #define USART_SR_LBD BIT(8)
51 #define USART_SR_CTS BIT(9)
52 #define USART_SR_ERR_MASK (USART_SR_LBD | USART_SR_ORE | \
53 USART_SR_FE | USART_SR_PE)
55 #define USART_SR_DUMMY_RX BIT(16)
58 #define USART_DR_MASK GENMASK(8, 0)
61 #define USART_BRR_DIV_F_MASK GENMASK(3, 0)
62 #define USART_BRR_DIV_M_MASK GENMASK(15, 4)
63 #define USART_BRR_DIV_M_SHIFT 4
66 #define USART_CR1_SBK BIT(0)
67 #define USART_CR1_RWU BIT(1)
68 #define USART_CR1_RE BIT(2)
69 #define USART_CR1_TE BIT(3)
70 #define USART_CR1_IDLEIE BIT(4)
71 #define USART_CR1_RXNEIE BIT(5)
72 #define USART_CR1_TCIE BIT(6)
73 #define USART_CR1_TXEIE BIT(7)
74 #define USART_CR1_PEIE BIT(8)
75 #define USART_CR1_PS BIT(9)
76 #define USART_CR1_PCE BIT(10)
77 #define USART_CR1_WAKE BIT(11)
78 #define USART_CR1_M BIT(12)
79 #define USART_CR1_UE BIT(13)
80 #define USART_CR1_OVER8 BIT(15)
81 #define USART_CR1_IE_MASK GENMASK(8, 4)
84 #define USART_CR2_ADD_MASK GENMASK(3, 0)
85 #define USART_CR2_LBDL BIT(5)
86 #define USART_CR2_LBDIE BIT(6)
87 #define USART_CR2_LBCL BIT(8)
88 #define USART_CR2_CPHA BIT(9)
89 #define USART_CR2_CPOL BIT(10)
90 #define USART_CR2_CLKEN BIT(11)
91 #define USART_CR2_STOP_2B BIT(13)
92 #define USART_CR2_STOP_MASK GENMASK(13, 12)
93 #define USART_CR2_LINEN BIT(14)
96 #define USART_CR3_EIE BIT(0)
97 #define USART_CR3_IREN BIT(1)
98 #define USART_CR3_IRLP BIT(2)
99 #define USART_CR3_HDSEL BIT(3)
100 #define USART_CR3_NACK BIT(4)
101 #define USART_CR3_SCEN BIT(5)
102 #define USART_CR3_DMAR BIT(6)
103 #define USART_CR3_DMAT BIT(7)
104 #define USART_CR3_RTSE BIT(8)
105 #define USART_CR3_CTSE BIT(9)
106 #define USART_CR3_CTSIE BIT(10)
107 #define USART_CR3_ONEBIT BIT(11)
110 #define USART_GTPR_PSC_MASK GENMASK(7, 0)
111 #define USART_GTPR_GT_MASK GENMASK(15, 8)
113 #define DRIVER_NAME "stm32-usart"
114 #define STM32_SERIAL_NAME "ttyS"
115 #define STM32_MAX_PORTS 6
118 struct uart_port port
;
120 bool hw_flow_control
;
123 static struct stm32_port stm32_ports
[STM32_MAX_PORTS
];
124 static struct uart_driver stm32_usart_driver
;
126 static void stm32_stop_tx(struct uart_port
*port
);
128 static inline struct stm32_port
*to_stm32_port(struct uart_port
*port
)
130 return container_of(port
, struct stm32_port
, port
);
133 static void stm32_set_bits(struct uart_port
*port
, u32 reg
, u32 bits
)
137 val
= readl_relaxed(port
->membase
+ reg
);
139 writel_relaxed(val
, port
->membase
+ reg
);
142 static void stm32_clr_bits(struct uart_port
*port
, u32 reg
, u32 bits
)
146 val
= readl_relaxed(port
->membase
+ reg
);
148 writel_relaxed(val
, port
->membase
+ reg
);
151 static void stm32_receive_chars(struct uart_port
*port
)
153 struct tty_port
*tport
= &port
->state
->port
;
159 pm_wakeup_event(tport
->tty
->dev
, 0);
161 while ((sr
= readl_relaxed(port
->membase
+ USART_SR
)) & USART_SR_RXNE
) {
162 sr
|= USART_SR_DUMMY_RX
;
163 c
= readl_relaxed(port
->membase
+ USART_DR
);
167 if (sr
& USART_SR_ERR_MASK
) {
168 if (sr
& USART_SR_LBD
) {
170 if (uart_handle_break(port
))
172 } else if (sr
& USART_SR_ORE
) {
173 port
->icount
.overrun
++;
174 } else if (sr
& USART_SR_PE
) {
175 port
->icount
.parity
++;
176 } else if (sr
& USART_SR_FE
) {
177 port
->icount
.frame
++;
180 sr
&= port
->read_status_mask
;
182 if (sr
& USART_SR_LBD
)
184 else if (sr
& USART_SR_PE
)
186 else if (sr
& USART_SR_FE
)
190 if (uart_handle_sysrq_char(port
, c
))
192 uart_insert_char(port
, sr
, USART_SR_ORE
, c
, flag
);
195 spin_unlock(&port
->lock
);
196 tty_flip_buffer_push(tport
);
197 spin_lock(&port
->lock
);
200 static void stm32_transmit_chars(struct uart_port
*port
)
202 struct circ_buf
*xmit
= &port
->state
->xmit
;
205 writel_relaxed(port
->x_char
, port
->membase
+ USART_DR
);
211 if (uart_tx_stopped(port
)) {
216 if (uart_circ_empty(xmit
)) {
221 writel_relaxed(xmit
->buf
[xmit
->tail
], port
->membase
+ USART_DR
);
222 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
225 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
226 uart_write_wakeup(port
);
228 if (uart_circ_empty(xmit
))
232 static irqreturn_t
stm32_interrupt(int irq
, void *ptr
)
234 struct uart_port
*port
= ptr
;
237 spin_lock(&port
->lock
);
239 sr
= readl_relaxed(port
->membase
+ USART_SR
);
241 if (sr
& USART_SR_RXNE
)
242 stm32_receive_chars(port
);
244 if (sr
& USART_SR_TXE
)
245 stm32_transmit_chars(port
);
247 spin_unlock(&port
->lock
);
252 static unsigned int stm32_tx_empty(struct uart_port
*port
)
254 return readl_relaxed(port
->membase
+ USART_SR
) & USART_SR_TXE
;
257 static void stm32_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
259 if ((mctrl
& TIOCM_RTS
) && (port
->status
& UPSTAT_AUTORTS
))
260 stm32_set_bits(port
, USART_CR3
, USART_CR3_RTSE
);
262 stm32_clr_bits(port
, USART_CR3
, USART_CR3_RTSE
);
265 static unsigned int stm32_get_mctrl(struct uart_port
*port
)
267 /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
268 return TIOCM_CAR
| TIOCM_DSR
| TIOCM_CTS
;
272 static void stm32_stop_tx(struct uart_port
*port
)
274 stm32_clr_bits(port
, USART_CR1
, USART_CR1_TXEIE
);
277 /* There are probably characters waiting to be transmitted. */
278 static void stm32_start_tx(struct uart_port
*port
)
280 struct circ_buf
*xmit
= &port
->state
->xmit
;
282 if (uart_circ_empty(xmit
))
285 stm32_set_bits(port
, USART_CR1
, USART_CR1_TXEIE
| USART_CR1_TE
);
288 /* Throttle the remote when input buffer is about to overflow. */
289 static void stm32_throttle(struct uart_port
*port
)
293 spin_lock_irqsave(&port
->lock
, flags
);
294 stm32_clr_bits(port
, USART_CR1
, USART_CR1_RXNEIE
);
295 spin_unlock_irqrestore(&port
->lock
, flags
);
298 /* Unthrottle the remote, the input buffer can now accept data. */
299 static void stm32_unthrottle(struct uart_port
*port
)
303 spin_lock_irqsave(&port
->lock
, flags
);
304 stm32_set_bits(port
, USART_CR1
, USART_CR1_RXNEIE
);
305 spin_unlock_irqrestore(&port
->lock
, flags
);
309 static void stm32_stop_rx(struct uart_port
*port
)
311 stm32_clr_bits(port
, USART_CR1
, USART_CR1_RXNEIE
);
314 /* Handle breaks - ignored by us */
315 static void stm32_break_ctl(struct uart_port
*port
, int break_state
)
319 static int stm32_startup(struct uart_port
*port
)
321 const char *name
= to_platform_device(port
->dev
)->name
;
325 ret
= request_irq(port
->irq
, stm32_interrupt
, 0, name
, port
);
329 val
= USART_CR1_RXNEIE
| USART_CR1_TE
| USART_CR1_RE
;
330 stm32_set_bits(port
, USART_CR1
, val
);
335 static void stm32_shutdown(struct uart_port
*port
)
339 val
= USART_CR1_TXEIE
| USART_CR1_RXNEIE
| USART_CR1_TE
| USART_CR1_RE
;
340 stm32_set_bits(port
, USART_CR1
, val
);
342 free_irq(port
->irq
, port
);
345 static void stm32_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
346 struct ktermios
*old
)
348 struct stm32_port
*stm32_port
= to_stm32_port(port
);
350 u32 usartdiv
, mantissa
, fraction
, oversampling
;
351 tcflag_t cflag
= termios
->c_cflag
;
355 if (!stm32_port
->hw_flow_control
)
358 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 8);
360 spin_lock_irqsave(&port
->lock
, flags
);
362 /* Stop serial port and reset value */
363 writel_relaxed(0, port
->membase
+ USART_CR1
);
365 cr1
= USART_CR1_TE
| USART_CR1_RE
| USART_CR1_UE
| USART_CR1_RXNEIE
;
370 cr2
|= USART_CR2_STOP_2B
;
372 if (cflag
& PARENB
) {
373 cr1
|= USART_CR1_PCE
;
374 if ((cflag
& CSIZE
) == CS8
)
381 port
->status
&= ~(UPSTAT_AUTOCTS
| UPSTAT_AUTORTS
);
382 if (cflag
& CRTSCTS
) {
383 port
->status
|= UPSTAT_AUTOCTS
| UPSTAT_AUTORTS
;
384 cr3
|= USART_CR3_CTSE
;
387 usartdiv
= DIV_ROUND_CLOSEST(port
->uartclk
, baud
);
390 * The USART supports 16 or 8 times oversampling.
391 * By default we prefer 16 times oversampling, so that the receiver
392 * has a better tolerance to clock deviations.
393 * 8 times oversampling is only used to achieve higher speeds.
397 stm32_set_bits(port
, USART_CR1
, USART_CR1_OVER8
);
400 stm32_clr_bits(port
, USART_CR1
, USART_CR1_OVER8
);
403 mantissa
= (usartdiv
/ oversampling
) << USART_BRR_DIV_M_SHIFT
;
404 fraction
= usartdiv
% oversampling
;
405 writel_relaxed(mantissa
| fraction
, port
->membase
+ USART_BRR
);
407 uart_update_timeout(port
, cflag
, baud
);
409 port
->read_status_mask
= USART_SR_ORE
;
410 if (termios
->c_iflag
& INPCK
)
411 port
->read_status_mask
|= USART_SR_PE
| USART_SR_FE
;
412 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
413 port
->read_status_mask
|= USART_SR_LBD
;
415 /* Characters to ignore */
416 port
->ignore_status_mask
= 0;
417 if (termios
->c_iflag
& IGNPAR
)
418 port
->ignore_status_mask
= USART_SR_PE
| USART_SR_FE
;
419 if (termios
->c_iflag
& IGNBRK
) {
420 port
->ignore_status_mask
|= USART_SR_LBD
;
422 * If we're ignoring parity and break indicators,
423 * ignore overruns too (for real raw support).
425 if (termios
->c_iflag
& IGNPAR
)
426 port
->ignore_status_mask
|= USART_SR_ORE
;
429 /* Ignore all characters if CREAD is not set */
430 if ((termios
->c_cflag
& CREAD
) == 0)
431 port
->ignore_status_mask
|= USART_SR_DUMMY_RX
;
433 writel_relaxed(cr3
, port
->membase
+ USART_CR3
);
434 writel_relaxed(cr2
, port
->membase
+ USART_CR2
);
435 writel_relaxed(cr1
, port
->membase
+ USART_CR1
);
437 spin_unlock_irqrestore(&port
->lock
, flags
);
440 static const char *stm32_type(struct uart_port
*port
)
442 return (port
->type
== PORT_STM32
) ? DRIVER_NAME
: NULL
;
445 static void stm32_release_port(struct uart_port
*port
)
449 static int stm32_request_port(struct uart_port
*port
)
454 static void stm32_config_port(struct uart_port
*port
, int flags
)
456 if (flags
& UART_CONFIG_TYPE
)
457 port
->type
= PORT_STM32
;
461 stm32_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
463 /* No user changeable parameters */
467 static void stm32_pm(struct uart_port
*port
, unsigned int state
,
468 unsigned int oldstate
)
470 struct stm32_port
*stm32port
= container_of(port
,
471 struct stm32_port
, port
);
472 unsigned long flags
= 0;
475 case UART_PM_STATE_ON
:
476 clk_prepare_enable(stm32port
->clk
);
478 case UART_PM_STATE_OFF
:
479 spin_lock_irqsave(&port
->lock
, flags
);
480 stm32_clr_bits(port
, USART_CR1
, USART_CR1_UE
);
481 spin_unlock_irqrestore(&port
->lock
, flags
);
482 clk_disable_unprepare(stm32port
->clk
);
487 static const struct uart_ops stm32_uart_ops
= {
488 .tx_empty
= stm32_tx_empty
,
489 .set_mctrl
= stm32_set_mctrl
,
490 .get_mctrl
= stm32_get_mctrl
,
491 .stop_tx
= stm32_stop_tx
,
492 .start_tx
= stm32_start_tx
,
493 .throttle
= stm32_throttle
,
494 .unthrottle
= stm32_unthrottle
,
495 .stop_rx
= stm32_stop_rx
,
496 .break_ctl
= stm32_break_ctl
,
497 .startup
= stm32_startup
,
498 .shutdown
= stm32_shutdown
,
499 .set_termios
= stm32_set_termios
,
502 .release_port
= stm32_release_port
,
503 .request_port
= stm32_request_port
,
504 .config_port
= stm32_config_port
,
505 .verify_port
= stm32_verify_port
,
508 static int stm32_init_port(struct stm32_port
*stm32port
,
509 struct platform_device
*pdev
)
511 struct uart_port
*port
= &stm32port
->port
;
512 struct resource
*res
;
515 port
->iotype
= UPIO_MEM
;
516 port
->flags
= UPF_BOOT_AUTOCONF
;
517 port
->ops
= &stm32_uart_ops
;
518 port
->dev
= &pdev
->dev
;
519 port
->irq
= platform_get_irq(pdev
, 0);
521 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
522 port
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
523 if (IS_ERR(port
->membase
))
524 return PTR_ERR(port
->membase
);
525 port
->mapbase
= res
->start
;
527 spin_lock_init(&port
->lock
);
529 stm32port
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
530 if (IS_ERR(stm32port
->clk
))
531 return PTR_ERR(stm32port
->clk
);
533 /* Ensure that clk rate is correct by enabling the clk */
534 ret
= clk_prepare_enable(stm32port
->clk
);
538 stm32port
->port
.uartclk
= clk_get_rate(stm32port
->clk
);
539 if (!stm32port
->port
.uartclk
)
542 clk_disable_unprepare(stm32port
->clk
);
547 static struct stm32_port
*stm32_of_get_stm32_port(struct platform_device
*pdev
)
549 struct device_node
*np
= pdev
->dev
.of_node
;
555 id
= of_alias_get_id(np
, "serial");
559 if (WARN_ON(id
>= STM32_MAX_PORTS
))
562 stm32_ports
[id
].hw_flow_control
= of_property_read_bool(np
,
563 "auto-flow-control");
564 stm32_ports
[id
].port
.line
= id
;
565 return &stm32_ports
[id
];
569 static const struct of_device_id stm32_match
[] = {
570 { .compatible
= "st,stm32-usart", },
571 { .compatible
= "st,stm32-uart", },
575 MODULE_DEVICE_TABLE(of
, stm32_match
);
578 static int stm32_serial_probe(struct platform_device
*pdev
)
581 struct stm32_port
*stm32port
;
583 stm32port
= stm32_of_get_stm32_port(pdev
);
587 ret
= stm32_init_port(stm32port
, pdev
);
591 ret
= uart_add_one_port(&stm32_usart_driver
, &stm32port
->port
);
595 platform_set_drvdata(pdev
, &stm32port
->port
);
600 static int stm32_serial_remove(struct platform_device
*pdev
)
602 struct uart_port
*port
= platform_get_drvdata(pdev
);
604 return uart_remove_one_port(&stm32_usart_driver
, port
);
608 #ifdef CONFIG_SERIAL_STM32_CONSOLE
609 static void stm32_console_putchar(struct uart_port
*port
, int ch
)
611 while (!(readl_relaxed(port
->membase
+ USART_SR
) & USART_SR_TXE
))
614 writel_relaxed(ch
, port
->membase
+ USART_DR
);
617 static void stm32_console_write(struct console
*co
, const char *s
, unsigned cnt
)
619 struct uart_port
*port
= &stm32_ports
[co
->index
].port
;
621 u32 old_cr1
, new_cr1
;
624 local_irq_save(flags
);
627 else if (oops_in_progress
)
628 locked
= spin_trylock(&port
->lock
);
630 spin_lock(&port
->lock
);
632 /* Save and disable interrupts */
633 old_cr1
= readl_relaxed(port
->membase
+ USART_CR1
);
634 new_cr1
= old_cr1
& ~USART_CR1_IE_MASK
;
635 writel_relaxed(new_cr1
, port
->membase
+ USART_CR1
);
637 uart_console_write(port
, s
, cnt
, stm32_console_putchar
);
639 /* Restore interrupt state */
640 writel_relaxed(old_cr1
, port
->membase
+ USART_CR1
);
643 spin_unlock(&port
->lock
);
644 local_irq_restore(flags
);
647 static int stm32_console_setup(struct console
*co
, char *options
)
649 struct stm32_port
*stm32port
;
655 if (co
->index
>= STM32_MAX_PORTS
)
658 stm32port
= &stm32_ports
[co
->index
];
661 * This driver does not support early console initialization
662 * (use ARM early printk support instead), so we only expect
663 * this to be called during the uart port registration when the
664 * driver gets probed and the port should be mapped at that point.
666 if (stm32port
->port
.mapbase
== 0 || stm32port
->port
.membase
== NULL
)
670 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
672 return uart_set_options(&stm32port
->port
, co
, baud
, parity
, bits
, flow
);
675 static struct console stm32_console
= {
676 .name
= STM32_SERIAL_NAME
,
677 .device
= uart_console_device
,
678 .write
= stm32_console_write
,
679 .setup
= stm32_console_setup
,
680 .flags
= CON_PRINTBUFFER
,
682 .data
= &stm32_usart_driver
,
685 #define STM32_SERIAL_CONSOLE (&stm32_console)
688 #define STM32_SERIAL_CONSOLE NULL
689 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
691 static struct uart_driver stm32_usart_driver
= {
692 .driver_name
= DRIVER_NAME
,
693 .dev_name
= STM32_SERIAL_NAME
,
696 .nr
= STM32_MAX_PORTS
,
697 .cons
= STM32_SERIAL_CONSOLE
,
700 static struct platform_driver stm32_serial_driver
= {
701 .probe
= stm32_serial_probe
,
702 .remove
= stm32_serial_remove
,
705 .of_match_table
= of_match_ptr(stm32_match
),
709 static int __init
usart_init(void)
711 static char banner
[] __initdata
= "STM32 USART driver initialized";
714 pr_info("%s\n", banner
);
716 ret
= uart_register_driver(&stm32_usart_driver
);
720 ret
= platform_driver_register(&stm32_serial_driver
);
722 uart_unregister_driver(&stm32_usart_driver
);
727 static void __exit
usart_exit(void)
729 platform_driver_unregister(&stm32_serial_driver
);
730 uart_unregister_driver(&stm32_usart_driver
);
733 module_init(usart_init
);
734 module_exit(usart_exit
);
736 MODULE_ALIAS("platform:" DRIVER_NAME
);
737 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
738 MODULE_LICENSE("GPL v2");