xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 59bed2dfe03e9e572bfb5a2d29effc1791eedcbc)
148a6092fSMaxime Coquelin /*
248a6092fSMaxime Coquelin  * Copyright (C) Maxime Coquelin 2015
3ada8618fSAlexandre TORGUE  * Authors:  Maxime Coquelin <mcoquelin.stm32@gmail.com>
4ada8618fSAlexandre TORGUE  *	     Gerald Baeza <gerald.baeza@st.com>
548a6092fSMaxime Coquelin  * License terms:  GNU General Public License (GPL), version 2
648a6092fSMaxime Coquelin  *
748a6092fSMaxime Coquelin  * Inspired by st-asc.c from STMicroelectronics (c)
848a6092fSMaxime Coquelin  */
948a6092fSMaxime Coquelin 
106b596a83SMaxime Coquelin #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
1148a6092fSMaxime Coquelin #define SUPPORT_SYSRQ
1248a6092fSMaxime Coquelin #endif
1348a6092fSMaxime Coquelin 
1448a6092fSMaxime Coquelin #include <linux/module.h>
1548a6092fSMaxime Coquelin #include <linux/serial.h>
1648a6092fSMaxime Coquelin #include <linux/console.h>
1748a6092fSMaxime Coquelin #include <linux/sysrq.h>
1848a6092fSMaxime Coquelin #include <linux/platform_device.h>
1948a6092fSMaxime Coquelin #include <linux/io.h>
2048a6092fSMaxime Coquelin #include <linux/irq.h>
2148a6092fSMaxime Coquelin #include <linux/tty.h>
2248a6092fSMaxime Coquelin #include <linux/tty_flip.h>
2348a6092fSMaxime Coquelin #include <linux/delay.h>
2448a6092fSMaxime Coquelin #include <linux/spinlock.h>
2548a6092fSMaxime Coquelin #include <linux/pm_runtime.h>
2648a6092fSMaxime Coquelin #include <linux/of.h>
2748a6092fSMaxime Coquelin #include <linux/of_platform.h>
2848a6092fSMaxime Coquelin #include <linux/serial_core.h>
2948a6092fSMaxime Coquelin #include <linux/clk.h>
3048a6092fSMaxime Coquelin 
31bc5a0b55SAlexandre TORGUE #include "stm32-usart.h"
3248a6092fSMaxime Coquelin 
3348a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port);
3448a6092fSMaxime Coquelin 
3548a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
3648a6092fSMaxime Coquelin {
3748a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
3848a6092fSMaxime Coquelin }
3948a6092fSMaxime Coquelin 
4048a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
4148a6092fSMaxime Coquelin {
4248a6092fSMaxime Coquelin 	u32 val;
4348a6092fSMaxime Coquelin 
4448a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
4548a6092fSMaxime Coquelin 	val |= bits;
4648a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
4748a6092fSMaxime Coquelin }
4848a6092fSMaxime Coquelin 
4948a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
5048a6092fSMaxime Coquelin {
5148a6092fSMaxime Coquelin 	u32 val;
5248a6092fSMaxime Coquelin 
5348a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
5448a6092fSMaxime Coquelin 	val &= ~bits;
5548a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
5648a6092fSMaxime Coquelin }
5748a6092fSMaxime Coquelin 
5848a6092fSMaxime Coquelin static void stm32_receive_chars(struct uart_port *port)
5948a6092fSMaxime Coquelin {
6048a6092fSMaxime Coquelin 	struct tty_port *tport = &port->state->port;
61ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
62ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
6348a6092fSMaxime Coquelin 	unsigned long c;
6448a6092fSMaxime Coquelin 	u32 sr;
6548a6092fSMaxime Coquelin 	char flag;
6648a6092fSMaxime Coquelin 
6748a6092fSMaxime Coquelin 	if (port->irq_wake)
6848a6092fSMaxime Coquelin 		pm_wakeup_event(tport->tty->dev, 0);
6948a6092fSMaxime Coquelin 
70ada8618fSAlexandre TORGUE 	while ((sr = readl_relaxed(port->membase + ofs->isr)) & USART_SR_RXNE) {
7148a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
72ada8618fSAlexandre TORGUE 		c = readl_relaxed(port->membase + ofs->rdr);
7348a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
7448a6092fSMaxime Coquelin 		port->icount.rx++;
7548a6092fSMaxime Coquelin 
7648a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
7748a6092fSMaxime Coquelin 			if (sr & USART_SR_LBD) {
7848a6092fSMaxime Coquelin 				port->icount.brk++;
7948a6092fSMaxime Coquelin 				if (uart_handle_break(port))
8048a6092fSMaxime Coquelin 					continue;
8148a6092fSMaxime Coquelin 			} else if (sr & USART_SR_ORE) {
82ada8618fSAlexandre TORGUE 				if (ofs->icr != UNDEF_REG)
83ada8618fSAlexandre TORGUE 					writel_relaxed(USART_ICR_ORECF,
84ada8618fSAlexandre TORGUE 						       port->membase +
85ada8618fSAlexandre TORGUE 						       ofs->icr);
8648a6092fSMaxime Coquelin 				port->icount.overrun++;
8748a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
8848a6092fSMaxime Coquelin 				port->icount.parity++;
8948a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
9048a6092fSMaxime Coquelin 				port->icount.frame++;
9148a6092fSMaxime Coquelin 			}
9248a6092fSMaxime Coquelin 
9348a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
9448a6092fSMaxime Coquelin 
9548a6092fSMaxime Coquelin 			if (sr & USART_SR_LBD)
9648a6092fSMaxime Coquelin 				flag = TTY_BREAK;
9748a6092fSMaxime Coquelin 			else if (sr & USART_SR_PE)
9848a6092fSMaxime Coquelin 				flag = TTY_PARITY;
9948a6092fSMaxime Coquelin 			else if (sr & USART_SR_FE)
10048a6092fSMaxime Coquelin 				flag = TTY_FRAME;
10148a6092fSMaxime Coquelin 		}
10248a6092fSMaxime Coquelin 
10348a6092fSMaxime Coquelin 		if (uart_handle_sysrq_char(port, c))
10448a6092fSMaxime Coquelin 			continue;
10548a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
10648a6092fSMaxime Coquelin 	}
10748a6092fSMaxime Coquelin 
10848a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
10948a6092fSMaxime Coquelin 	tty_flip_buffer_push(tport);
11048a6092fSMaxime Coquelin 	spin_lock(&port->lock);
11148a6092fSMaxime Coquelin }
11248a6092fSMaxime Coquelin 
11348a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port)
11448a6092fSMaxime Coquelin {
115ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
116ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
11748a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
11848a6092fSMaxime Coquelin 
11948a6092fSMaxime Coquelin 	if (port->x_char) {
120ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
12148a6092fSMaxime Coquelin 		port->x_char = 0;
12248a6092fSMaxime Coquelin 		port->icount.tx++;
12348a6092fSMaxime Coquelin 		return;
12448a6092fSMaxime Coquelin 	}
12548a6092fSMaxime Coquelin 
12648a6092fSMaxime Coquelin 	if (uart_tx_stopped(port)) {
12748a6092fSMaxime Coquelin 		stm32_stop_tx(port);
12848a6092fSMaxime Coquelin 		return;
12948a6092fSMaxime Coquelin 	}
13048a6092fSMaxime Coquelin 
13148a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit)) {
13248a6092fSMaxime Coquelin 		stm32_stop_tx(port);
13348a6092fSMaxime Coquelin 		return;
13448a6092fSMaxime Coquelin 	}
13548a6092fSMaxime Coquelin 
136ada8618fSAlexandre TORGUE 	writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
13748a6092fSMaxime Coquelin 	xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
13848a6092fSMaxime Coquelin 	port->icount.tx++;
13948a6092fSMaxime Coquelin 
14048a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
14148a6092fSMaxime Coquelin 		uart_write_wakeup(port);
14248a6092fSMaxime Coquelin 
14348a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
14448a6092fSMaxime Coquelin 		stm32_stop_tx(port);
14548a6092fSMaxime Coquelin }
14648a6092fSMaxime Coquelin 
14748a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr)
14848a6092fSMaxime Coquelin {
14948a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
150ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
151ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
15248a6092fSMaxime Coquelin 	u32 sr;
15348a6092fSMaxime Coquelin 
15448a6092fSMaxime Coquelin 	spin_lock(&port->lock);
15548a6092fSMaxime Coquelin 
156ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
15748a6092fSMaxime Coquelin 
15848a6092fSMaxime Coquelin 	if (sr & USART_SR_RXNE)
15948a6092fSMaxime Coquelin 		stm32_receive_chars(port);
16048a6092fSMaxime Coquelin 
16148a6092fSMaxime Coquelin 	if (sr & USART_SR_TXE)
16248a6092fSMaxime Coquelin 		stm32_transmit_chars(port);
16348a6092fSMaxime Coquelin 
16448a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
16548a6092fSMaxime Coquelin 
16648a6092fSMaxime Coquelin 	return IRQ_HANDLED;
16748a6092fSMaxime Coquelin }
16848a6092fSMaxime Coquelin 
16948a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port)
17048a6092fSMaxime Coquelin {
171ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
172ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
173ada8618fSAlexandre TORGUE 
174ada8618fSAlexandre TORGUE 	return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
17548a6092fSMaxime Coquelin }
17648a6092fSMaxime Coquelin 
17748a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
17848a6092fSMaxime Coquelin {
179ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
180ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
181ada8618fSAlexandre TORGUE 
18248a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
183ada8618fSAlexandre TORGUE 		stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
18448a6092fSMaxime Coquelin 	else
185ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
18648a6092fSMaxime Coquelin }
18748a6092fSMaxime Coquelin 
18848a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port)
18948a6092fSMaxime Coquelin {
19048a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
19148a6092fSMaxime Coquelin 	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
19248a6092fSMaxime Coquelin }
19348a6092fSMaxime Coquelin 
19448a6092fSMaxime Coquelin /* Transmit stop */
19548a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port)
19648a6092fSMaxime Coquelin {
197ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
198ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
199ada8618fSAlexandre TORGUE 
200ada8618fSAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
20148a6092fSMaxime Coquelin }
20248a6092fSMaxime Coquelin 
20348a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
20448a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port)
20548a6092fSMaxime Coquelin {
206ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
207ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
20848a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
20948a6092fSMaxime Coquelin 
21048a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
21148a6092fSMaxime Coquelin 		return;
21248a6092fSMaxime Coquelin 
213ada8618fSAlexandre TORGUE 	stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE | USART_CR1_TE);
21448a6092fSMaxime Coquelin }
21548a6092fSMaxime Coquelin 
21648a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
21748a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port)
21848a6092fSMaxime Coquelin {
219ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
220ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
22148a6092fSMaxime Coquelin 	unsigned long flags;
22248a6092fSMaxime Coquelin 
22348a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
224ada8618fSAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
22548a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
22648a6092fSMaxime Coquelin }
22748a6092fSMaxime Coquelin 
22848a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
22948a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port)
23048a6092fSMaxime Coquelin {
231ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
232ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
23348a6092fSMaxime Coquelin 	unsigned long flags;
23448a6092fSMaxime Coquelin 
23548a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
236ada8618fSAlexandre TORGUE 	stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE);
23748a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
23848a6092fSMaxime Coquelin }
23948a6092fSMaxime Coquelin 
24048a6092fSMaxime Coquelin /* Receive stop */
24148a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port)
24248a6092fSMaxime Coquelin {
243ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
244ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
245ada8618fSAlexandre TORGUE 
246ada8618fSAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE);
24748a6092fSMaxime Coquelin }
24848a6092fSMaxime Coquelin 
24948a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
25048a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state)
25148a6092fSMaxime Coquelin {
25248a6092fSMaxime Coquelin }
25348a6092fSMaxime Coquelin 
25448a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port)
25548a6092fSMaxime Coquelin {
256ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
257ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
25848a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
25948a6092fSMaxime Coquelin 	u32 val;
26048a6092fSMaxime Coquelin 	int ret;
26148a6092fSMaxime Coquelin 
262616ea8d2SSudeep Holla 	ret = request_irq(port->irq, stm32_interrupt, 0, name, port);
26348a6092fSMaxime Coquelin 	if (ret)
26448a6092fSMaxime Coquelin 		return ret;
26548a6092fSMaxime Coquelin 
26648a6092fSMaxime Coquelin 	val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
267ada8618fSAlexandre TORGUE 	stm32_set_bits(port, ofs->cr1, val);
26848a6092fSMaxime Coquelin 
26948a6092fSMaxime Coquelin 	return 0;
27048a6092fSMaxime Coquelin }
27148a6092fSMaxime Coquelin 
27248a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port)
27348a6092fSMaxime Coquelin {
274ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
275ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
27648a6092fSMaxime Coquelin 	u32 val;
27748a6092fSMaxime Coquelin 
27848a6092fSMaxime Coquelin 	val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
279a14f66a4SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr1, val);
28048a6092fSMaxime Coquelin 
28148a6092fSMaxime Coquelin 	free_irq(port->irq, port);
28248a6092fSMaxime Coquelin }
28348a6092fSMaxime Coquelin 
28448a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
28548a6092fSMaxime Coquelin 			    struct ktermios *old)
28648a6092fSMaxime Coquelin {
28748a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
288ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
289ada8618fSAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
29048a6092fSMaxime Coquelin 	unsigned int baud;
29148a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
29248a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
29348a6092fSMaxime Coquelin 	u32 cr1, cr2, cr3;
29448a6092fSMaxime Coquelin 	unsigned long flags;
29548a6092fSMaxime Coquelin 
29648a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
29748a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
29848a6092fSMaxime Coquelin 
29948a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
30048a6092fSMaxime Coquelin 
30148a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
30248a6092fSMaxime Coquelin 
30348a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
304ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
30548a6092fSMaxime Coquelin 
306ada8618fSAlexandre TORGUE 	cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
307ada8618fSAlexandre TORGUE 	cr1 |= BIT(cfg->uart_enable_bit);
30848a6092fSMaxime Coquelin 	cr2 = 0;
30948a6092fSMaxime Coquelin 	cr3 = 0;
31048a6092fSMaxime Coquelin 
31148a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
31248a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
31348a6092fSMaxime Coquelin 
31448a6092fSMaxime Coquelin 	if (cflag & PARENB) {
31548a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
316ada8618fSAlexandre TORGUE 		if ((cflag & CSIZE) == CS8) {
317ada8618fSAlexandre TORGUE 			if (cfg->has_7bits_data)
318ada8618fSAlexandre TORGUE 				cr1 |= USART_CR1_M0;
319ada8618fSAlexandre TORGUE 			else
32048a6092fSMaxime Coquelin 				cr1 |= USART_CR1_M;
32148a6092fSMaxime Coquelin 		}
322ada8618fSAlexandre TORGUE 	}
32348a6092fSMaxime Coquelin 
32448a6092fSMaxime Coquelin 	if (cflag & PARODD)
32548a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
32648a6092fSMaxime Coquelin 
32748a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
32848a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
32948a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
33048a6092fSMaxime Coquelin 		cr3 |= USART_CR3_CTSE;
33148a6092fSMaxime Coquelin 	}
33248a6092fSMaxime Coquelin 
33348a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
33448a6092fSMaxime Coquelin 
33548a6092fSMaxime Coquelin 	/*
33648a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
33748a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
33848a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
33948a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
34048a6092fSMaxime Coquelin 	 */
34148a6092fSMaxime Coquelin 	if (usartdiv < 16) {
34248a6092fSMaxime Coquelin 		oversampling = 8;
343ada8618fSAlexandre TORGUE 		stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
34448a6092fSMaxime Coquelin 	} else {
34548a6092fSMaxime Coquelin 		oversampling = 16;
346ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
34748a6092fSMaxime Coquelin 	}
34848a6092fSMaxime Coquelin 
34948a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
35048a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
351ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
35248a6092fSMaxime Coquelin 
35348a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
35448a6092fSMaxime Coquelin 
35548a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
35648a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
35748a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
35848a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
35948a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_LBD;
36048a6092fSMaxime Coquelin 
36148a6092fSMaxime Coquelin 	/* Characters to ignore */
36248a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
36348a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
36448a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
36548a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
36648a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_LBD;
36748a6092fSMaxime Coquelin 		/*
36848a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
36948a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
37048a6092fSMaxime Coquelin 		 */
37148a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
37248a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
37348a6092fSMaxime Coquelin 	}
37448a6092fSMaxime Coquelin 
37548a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
37648a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
37748a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
37848a6092fSMaxime Coquelin 
379ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
380ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
381ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
38248a6092fSMaxime Coquelin 
38348a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
38448a6092fSMaxime Coquelin }
38548a6092fSMaxime Coquelin 
38648a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port)
38748a6092fSMaxime Coquelin {
38848a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
38948a6092fSMaxime Coquelin }
39048a6092fSMaxime Coquelin 
39148a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port)
39248a6092fSMaxime Coquelin {
39348a6092fSMaxime Coquelin }
39448a6092fSMaxime Coquelin 
39548a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port)
39648a6092fSMaxime Coquelin {
39748a6092fSMaxime Coquelin 	return 0;
39848a6092fSMaxime Coquelin }
39948a6092fSMaxime Coquelin 
40048a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags)
40148a6092fSMaxime Coquelin {
40248a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
40348a6092fSMaxime Coquelin 		port->type = PORT_STM32;
40448a6092fSMaxime Coquelin }
40548a6092fSMaxime Coquelin 
40648a6092fSMaxime Coquelin static int
40748a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
40848a6092fSMaxime Coquelin {
40948a6092fSMaxime Coquelin 	/* No user changeable parameters */
41048a6092fSMaxime Coquelin 	return -EINVAL;
41148a6092fSMaxime Coquelin }
41248a6092fSMaxime Coquelin 
41348a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state,
41448a6092fSMaxime Coquelin 		unsigned int oldstate)
41548a6092fSMaxime Coquelin {
41648a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
41748a6092fSMaxime Coquelin 			struct stm32_port, port);
418ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
419ada8618fSAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32port->info->cfg;
42048a6092fSMaxime Coquelin 	unsigned long flags = 0;
42148a6092fSMaxime Coquelin 
42248a6092fSMaxime Coquelin 	switch (state) {
42348a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
42448a6092fSMaxime Coquelin 		clk_prepare_enable(stm32port->clk);
42548a6092fSMaxime Coquelin 		break;
42648a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
42748a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
428ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
42948a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
43048a6092fSMaxime Coquelin 		clk_disable_unprepare(stm32port->clk);
43148a6092fSMaxime Coquelin 		break;
43248a6092fSMaxime Coquelin 	}
43348a6092fSMaxime Coquelin }
43448a6092fSMaxime Coquelin 
43548a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
43648a6092fSMaxime Coquelin 	.tx_empty	= stm32_tx_empty,
43748a6092fSMaxime Coquelin 	.set_mctrl	= stm32_set_mctrl,
43848a6092fSMaxime Coquelin 	.get_mctrl	= stm32_get_mctrl,
43948a6092fSMaxime Coquelin 	.stop_tx	= stm32_stop_tx,
44048a6092fSMaxime Coquelin 	.start_tx	= stm32_start_tx,
44148a6092fSMaxime Coquelin 	.throttle	= stm32_throttle,
44248a6092fSMaxime Coquelin 	.unthrottle	= stm32_unthrottle,
44348a6092fSMaxime Coquelin 	.stop_rx	= stm32_stop_rx,
44448a6092fSMaxime Coquelin 	.break_ctl	= stm32_break_ctl,
44548a6092fSMaxime Coquelin 	.startup	= stm32_startup,
44648a6092fSMaxime Coquelin 	.shutdown	= stm32_shutdown,
44748a6092fSMaxime Coquelin 	.set_termios	= stm32_set_termios,
44848a6092fSMaxime Coquelin 	.pm		= stm32_pm,
44948a6092fSMaxime Coquelin 	.type		= stm32_type,
45048a6092fSMaxime Coquelin 	.release_port	= stm32_release_port,
45148a6092fSMaxime Coquelin 	.request_port	= stm32_request_port,
45248a6092fSMaxime Coquelin 	.config_port	= stm32_config_port,
45348a6092fSMaxime Coquelin 	.verify_port	= stm32_verify_port,
45448a6092fSMaxime Coquelin };
45548a6092fSMaxime Coquelin 
45648a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port,
45748a6092fSMaxime Coquelin 			  struct platform_device *pdev)
45848a6092fSMaxime Coquelin {
45948a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
46048a6092fSMaxime Coquelin 	struct resource *res;
46148a6092fSMaxime Coquelin 	int ret;
46248a6092fSMaxime Coquelin 
46348a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
46448a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
46548a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
46648a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
46748a6092fSMaxime Coquelin 	port->irq	= platform_get_irq(pdev, 0);
46848a6092fSMaxime Coquelin 
46948a6092fSMaxime Coquelin 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
47048a6092fSMaxime Coquelin 	port->membase = devm_ioremap_resource(&pdev->dev, res);
47148a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
47248a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
47348a6092fSMaxime Coquelin 	port->mapbase = res->start;
47448a6092fSMaxime Coquelin 
47548a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
47648a6092fSMaxime Coquelin 
47748a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
47848a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
47948a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
48048a6092fSMaxime Coquelin 
48148a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
48248a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
48348a6092fSMaxime Coquelin 	if (ret)
48448a6092fSMaxime Coquelin 		return ret;
48548a6092fSMaxime Coquelin 
48648a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
48748a6092fSMaxime Coquelin 	if (!stm32port->port.uartclk)
48848a6092fSMaxime Coquelin 		ret = -EINVAL;
48948a6092fSMaxime Coquelin 
49048a6092fSMaxime Coquelin 	clk_disable_unprepare(stm32port->clk);
49148a6092fSMaxime Coquelin 
49248a6092fSMaxime Coquelin 	return ret;
49348a6092fSMaxime Coquelin }
49448a6092fSMaxime Coquelin 
49548a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
49648a6092fSMaxime Coquelin {
49748a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
49848a6092fSMaxime Coquelin 	int id;
49948a6092fSMaxime Coquelin 
50048a6092fSMaxime Coquelin 	if (!np)
50148a6092fSMaxime Coquelin 		return NULL;
50248a6092fSMaxime Coquelin 
50348a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
50448a6092fSMaxime Coquelin 	if (id < 0)
50548a6092fSMaxime Coquelin 		id = 0;
50648a6092fSMaxime Coquelin 
50748a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
50848a6092fSMaxime Coquelin 		return NULL;
50948a6092fSMaxime Coquelin 
51048a6092fSMaxime Coquelin 	stm32_ports[id].hw_flow_control = of_property_read_bool(np,
511*59bed2dfSAlexandre TORGUE 							"st,hw-flow-ctrl");
51248a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
51348a6092fSMaxime Coquelin 	return &stm32_ports[id];
51448a6092fSMaxime Coquelin }
51548a6092fSMaxime Coquelin 
51648a6092fSMaxime Coquelin #ifdef CONFIG_OF
51748a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
518ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-usart", .data = &stm32f4_info},
519ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
520ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-usart", .data = &stm32f7_info},
521ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
52248a6092fSMaxime Coquelin 	{},
52348a6092fSMaxime Coquelin };
52448a6092fSMaxime Coquelin 
52548a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
52648a6092fSMaxime Coquelin #endif
52748a6092fSMaxime Coquelin 
52848a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev)
52948a6092fSMaxime Coquelin {
530ada8618fSAlexandre TORGUE 	const struct of_device_id *match;
53148a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
532ada8618fSAlexandre TORGUE 	int ret;
53348a6092fSMaxime Coquelin 
53448a6092fSMaxime Coquelin 	stm32port = stm32_of_get_stm32_port(pdev);
53548a6092fSMaxime Coquelin 	if (!stm32port)
53648a6092fSMaxime Coquelin 		return -ENODEV;
53748a6092fSMaxime Coquelin 
538ada8618fSAlexandre TORGUE 	match = of_match_device(stm32_match, &pdev->dev);
539ada8618fSAlexandre TORGUE 	if (match && match->data)
540ada8618fSAlexandre TORGUE 		stm32port->info = (struct stm32_usart_info *)match->data;
541ada8618fSAlexandre TORGUE 	else
542ada8618fSAlexandre TORGUE 		return -EINVAL;
543ada8618fSAlexandre TORGUE 
54448a6092fSMaxime Coquelin 	ret = stm32_init_port(stm32port, pdev);
54548a6092fSMaxime Coquelin 	if (ret)
54648a6092fSMaxime Coquelin 		return ret;
54748a6092fSMaxime Coquelin 
54848a6092fSMaxime Coquelin 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
54948a6092fSMaxime Coquelin 	if (ret)
55048a6092fSMaxime Coquelin 		return ret;
55148a6092fSMaxime Coquelin 
55248a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
55348a6092fSMaxime Coquelin 
55448a6092fSMaxime Coquelin 	return 0;
55548a6092fSMaxime Coquelin }
55648a6092fSMaxime Coquelin 
55748a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev)
55848a6092fSMaxime Coquelin {
55948a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
56048a6092fSMaxime Coquelin 
56148a6092fSMaxime Coquelin 	return uart_remove_one_port(&stm32_usart_driver, port);
56248a6092fSMaxime Coquelin }
56348a6092fSMaxime Coquelin 
56448a6092fSMaxime Coquelin 
56548a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE
56648a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch)
56748a6092fSMaxime Coquelin {
568ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
569ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
570ada8618fSAlexandre TORGUE 
571ada8618fSAlexandre TORGUE 	while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
57248a6092fSMaxime Coquelin 		cpu_relax();
57348a6092fSMaxime Coquelin 
574ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
57548a6092fSMaxime Coquelin }
57648a6092fSMaxime Coquelin 
57748a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
57848a6092fSMaxime Coquelin {
57948a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
580ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
581ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
58248a6092fSMaxime Coquelin 	unsigned long flags;
58348a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
58448a6092fSMaxime Coquelin 	int locked = 1;
58548a6092fSMaxime Coquelin 
58648a6092fSMaxime Coquelin 	local_irq_save(flags);
58748a6092fSMaxime Coquelin 	if (port->sysrq)
58848a6092fSMaxime Coquelin 		locked = 0;
58948a6092fSMaxime Coquelin 	else if (oops_in_progress)
59048a6092fSMaxime Coquelin 		locked = spin_trylock(&port->lock);
59148a6092fSMaxime Coquelin 	else
59248a6092fSMaxime Coquelin 		spin_lock(&port->lock);
59348a6092fSMaxime Coquelin 
59448a6092fSMaxime Coquelin 	/* Save and disable interrupts */
595ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
59648a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
597ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
59848a6092fSMaxime Coquelin 
59948a6092fSMaxime Coquelin 	uart_console_write(port, s, cnt, stm32_console_putchar);
60048a6092fSMaxime Coquelin 
60148a6092fSMaxime Coquelin 	/* Restore interrupt state */
602ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
60348a6092fSMaxime Coquelin 
60448a6092fSMaxime Coquelin 	if (locked)
60548a6092fSMaxime Coquelin 		spin_unlock(&port->lock);
60648a6092fSMaxime Coquelin 	local_irq_restore(flags);
60748a6092fSMaxime Coquelin }
60848a6092fSMaxime Coquelin 
60948a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options)
61048a6092fSMaxime Coquelin {
61148a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
61248a6092fSMaxime Coquelin 	int baud = 9600;
61348a6092fSMaxime Coquelin 	int bits = 8;
61448a6092fSMaxime Coquelin 	int parity = 'n';
61548a6092fSMaxime Coquelin 	int flow = 'n';
61648a6092fSMaxime Coquelin 
61748a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
61848a6092fSMaxime Coquelin 		return -ENODEV;
61948a6092fSMaxime Coquelin 
62048a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
62148a6092fSMaxime Coquelin 
62248a6092fSMaxime Coquelin 	/*
62348a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
62448a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
62548a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
62648a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
62748a6092fSMaxime Coquelin 	 */
62848a6092fSMaxime Coquelin 	if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
62948a6092fSMaxime Coquelin 		return -ENXIO;
63048a6092fSMaxime Coquelin 
63148a6092fSMaxime Coquelin 	if (options)
63248a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
63348a6092fSMaxime Coquelin 
63448a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
63548a6092fSMaxime Coquelin }
63648a6092fSMaxime Coquelin 
63748a6092fSMaxime Coquelin static struct console stm32_console = {
63848a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
63948a6092fSMaxime Coquelin 	.device		= uart_console_device,
64048a6092fSMaxime Coquelin 	.write		= stm32_console_write,
64148a6092fSMaxime Coquelin 	.setup		= stm32_console_setup,
64248a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
64348a6092fSMaxime Coquelin 	.index		= -1,
64448a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
64548a6092fSMaxime Coquelin };
64648a6092fSMaxime Coquelin 
64748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
64848a6092fSMaxime Coquelin 
64948a6092fSMaxime Coquelin #else
65048a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
65148a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
65248a6092fSMaxime Coquelin 
65348a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
65448a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
65548a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
65648a6092fSMaxime Coquelin 	.major		= 0,
65748a6092fSMaxime Coquelin 	.minor		= 0,
65848a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
65948a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
66048a6092fSMaxime Coquelin };
66148a6092fSMaxime Coquelin 
66248a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
66348a6092fSMaxime Coquelin 	.probe		= stm32_serial_probe,
66448a6092fSMaxime Coquelin 	.remove		= stm32_serial_remove,
66548a6092fSMaxime Coquelin 	.driver	= {
66648a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
66748a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
66848a6092fSMaxime Coquelin 	},
66948a6092fSMaxime Coquelin };
67048a6092fSMaxime Coquelin 
67148a6092fSMaxime Coquelin static int __init usart_init(void)
67248a6092fSMaxime Coquelin {
67348a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
67448a6092fSMaxime Coquelin 	int ret;
67548a6092fSMaxime Coquelin 
67648a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
67748a6092fSMaxime Coquelin 
67848a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
67948a6092fSMaxime Coquelin 	if (ret)
68048a6092fSMaxime Coquelin 		return ret;
68148a6092fSMaxime Coquelin 
68248a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
68348a6092fSMaxime Coquelin 	if (ret)
68448a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
68548a6092fSMaxime Coquelin 
68648a6092fSMaxime Coquelin 	return ret;
68748a6092fSMaxime Coquelin }
68848a6092fSMaxime Coquelin 
68948a6092fSMaxime Coquelin static void __exit usart_exit(void)
69048a6092fSMaxime Coquelin {
69148a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
69248a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
69348a6092fSMaxime Coquelin }
69448a6092fSMaxime Coquelin 
69548a6092fSMaxime Coquelin module_init(usart_init);
69648a6092fSMaxime Coquelin module_exit(usart_exit);
69748a6092fSMaxime Coquelin 
69848a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
69948a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
70048a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
701