xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 48a6092fb41fab5b80064c3fac786f8ec86457a3)
1*48a6092fSMaxime Coquelin /*
2*48a6092fSMaxime Coquelin  * Copyright (C) Maxime Coquelin 2015
3*48a6092fSMaxime Coquelin  * Author:  Maxime Coquelin <mcoquelin.stm32@gmail.com>
4*48a6092fSMaxime Coquelin  * License terms:  GNU General Public License (GPL), version 2
5*48a6092fSMaxime Coquelin  *
6*48a6092fSMaxime Coquelin  * Inspired by st-asc.c from STMicroelectronics (c)
7*48a6092fSMaxime Coquelin  */
8*48a6092fSMaxime Coquelin 
9*48a6092fSMaxime Coquelin #if defined(CONFIG_SERIAL_STM32_USART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
10*48a6092fSMaxime Coquelin #define SUPPORT_SYSRQ
11*48a6092fSMaxime Coquelin #endif
12*48a6092fSMaxime Coquelin 
13*48a6092fSMaxime Coquelin #include <linux/module.h>
14*48a6092fSMaxime Coquelin #include <linux/serial.h>
15*48a6092fSMaxime Coquelin #include <linux/console.h>
16*48a6092fSMaxime Coquelin #include <linux/sysrq.h>
17*48a6092fSMaxime Coquelin #include <linux/platform_device.h>
18*48a6092fSMaxime Coquelin #include <linux/io.h>
19*48a6092fSMaxime Coquelin #include <linux/irq.h>
20*48a6092fSMaxime Coquelin #include <linux/tty.h>
21*48a6092fSMaxime Coquelin #include <linux/tty_flip.h>
22*48a6092fSMaxime Coquelin #include <linux/delay.h>
23*48a6092fSMaxime Coquelin #include <linux/spinlock.h>
24*48a6092fSMaxime Coquelin #include <linux/pm_runtime.h>
25*48a6092fSMaxime Coquelin #include <linux/of.h>
26*48a6092fSMaxime Coquelin #include <linux/of_platform.h>
27*48a6092fSMaxime Coquelin #include <linux/serial_core.h>
28*48a6092fSMaxime Coquelin #include <linux/clk.h>
29*48a6092fSMaxime Coquelin 
30*48a6092fSMaxime Coquelin #define DRIVER_NAME "stm32-usart"
31*48a6092fSMaxime Coquelin 
32*48a6092fSMaxime Coquelin /* Register offsets */
33*48a6092fSMaxime Coquelin #define USART_SR		0x00
34*48a6092fSMaxime Coquelin #define USART_DR		0x04
35*48a6092fSMaxime Coquelin #define USART_BRR		0x08
36*48a6092fSMaxime Coquelin #define USART_CR1		0x0c
37*48a6092fSMaxime Coquelin #define USART_CR2		0x10
38*48a6092fSMaxime Coquelin #define USART_CR3		0x14
39*48a6092fSMaxime Coquelin #define USART_GTPR		0x18
40*48a6092fSMaxime Coquelin 
41*48a6092fSMaxime Coquelin /* USART_SR */
42*48a6092fSMaxime Coquelin #define USART_SR_PE		BIT(0)
43*48a6092fSMaxime Coquelin #define USART_SR_FE		BIT(1)
44*48a6092fSMaxime Coquelin #define USART_SR_NF		BIT(2)
45*48a6092fSMaxime Coquelin #define USART_SR_ORE		BIT(3)
46*48a6092fSMaxime Coquelin #define USART_SR_IDLE		BIT(4)
47*48a6092fSMaxime Coquelin #define USART_SR_RXNE		BIT(5)
48*48a6092fSMaxime Coquelin #define USART_SR_TC		BIT(6)
49*48a6092fSMaxime Coquelin #define USART_SR_TXE		BIT(7)
50*48a6092fSMaxime Coquelin #define USART_SR_LBD		BIT(8)
51*48a6092fSMaxime Coquelin #define USART_SR_CTS		BIT(9)
52*48a6092fSMaxime Coquelin #define USART_SR_ERR_MASK	(USART_SR_LBD | USART_SR_ORE | \
53*48a6092fSMaxime Coquelin 				 USART_SR_FE | USART_SR_PE)
54*48a6092fSMaxime Coquelin /* Dummy bits */
55*48a6092fSMaxime Coquelin #define USART_SR_DUMMY_RX	BIT(16)
56*48a6092fSMaxime Coquelin 
57*48a6092fSMaxime Coquelin /* USART_DR */
58*48a6092fSMaxime Coquelin #define USART_DR_MASK		GENMASK(8, 0)
59*48a6092fSMaxime Coquelin 
60*48a6092fSMaxime Coquelin /* USART_BRR */
61*48a6092fSMaxime Coquelin #define USART_BRR_DIV_F_MASK	GENMASK(3, 0)
62*48a6092fSMaxime Coquelin #define USART_BRR_DIV_M_MASK	GENMASK(15, 4)
63*48a6092fSMaxime Coquelin #define USART_BRR_DIV_M_SHIFT	4
64*48a6092fSMaxime Coquelin 
65*48a6092fSMaxime Coquelin /* USART_CR1 */
66*48a6092fSMaxime Coquelin #define USART_CR1_SBK		BIT(0)
67*48a6092fSMaxime Coquelin #define USART_CR1_RWU		BIT(1)
68*48a6092fSMaxime Coquelin #define USART_CR1_RE		BIT(2)
69*48a6092fSMaxime Coquelin #define USART_CR1_TE		BIT(3)
70*48a6092fSMaxime Coquelin #define USART_CR1_IDLEIE	BIT(4)
71*48a6092fSMaxime Coquelin #define USART_CR1_RXNEIE	BIT(5)
72*48a6092fSMaxime Coquelin #define USART_CR1_TCIE		BIT(6)
73*48a6092fSMaxime Coquelin #define USART_CR1_TXEIE		BIT(7)
74*48a6092fSMaxime Coquelin #define USART_CR1_PEIE		BIT(8)
75*48a6092fSMaxime Coquelin #define USART_CR1_PS		BIT(9)
76*48a6092fSMaxime Coquelin #define USART_CR1_PCE		BIT(10)
77*48a6092fSMaxime Coquelin #define USART_CR1_WAKE		BIT(11)
78*48a6092fSMaxime Coquelin #define USART_CR1_M		BIT(12)
79*48a6092fSMaxime Coquelin #define USART_CR1_UE		BIT(13)
80*48a6092fSMaxime Coquelin #define USART_CR1_OVER8		BIT(15)
81*48a6092fSMaxime Coquelin #define USART_CR1_IE_MASK	GENMASK(8, 4)
82*48a6092fSMaxime Coquelin 
83*48a6092fSMaxime Coquelin /* USART_CR2 */
84*48a6092fSMaxime Coquelin #define USART_CR2_ADD_MASK	GENMASK(3, 0)
85*48a6092fSMaxime Coquelin #define USART_CR2_LBDL		BIT(5)
86*48a6092fSMaxime Coquelin #define USART_CR2_LBDIE		BIT(6)
87*48a6092fSMaxime Coquelin #define USART_CR2_LBCL		BIT(8)
88*48a6092fSMaxime Coquelin #define USART_CR2_CPHA		BIT(9)
89*48a6092fSMaxime Coquelin #define USART_CR2_CPOL		BIT(10)
90*48a6092fSMaxime Coquelin #define USART_CR2_CLKEN		BIT(11)
91*48a6092fSMaxime Coquelin #define USART_CR2_STOP_2B	BIT(13)
92*48a6092fSMaxime Coquelin #define USART_CR2_STOP_MASK	GENMASK(13, 12)
93*48a6092fSMaxime Coquelin #define USART_CR2_LINEN		BIT(14)
94*48a6092fSMaxime Coquelin 
95*48a6092fSMaxime Coquelin /* USART_CR3 */
96*48a6092fSMaxime Coquelin #define USART_CR3_EIE		BIT(0)
97*48a6092fSMaxime Coquelin #define USART_CR3_IREN		BIT(1)
98*48a6092fSMaxime Coquelin #define USART_CR3_IRLP		BIT(2)
99*48a6092fSMaxime Coquelin #define USART_CR3_HDSEL		BIT(3)
100*48a6092fSMaxime Coquelin #define USART_CR3_NACK		BIT(4)
101*48a6092fSMaxime Coquelin #define USART_CR3_SCEN		BIT(5)
102*48a6092fSMaxime Coquelin #define USART_CR3_DMAR		BIT(6)
103*48a6092fSMaxime Coquelin #define USART_CR3_DMAT		BIT(7)
104*48a6092fSMaxime Coquelin #define USART_CR3_RTSE		BIT(8)
105*48a6092fSMaxime Coquelin #define USART_CR3_CTSE		BIT(9)
106*48a6092fSMaxime Coquelin #define USART_CR3_CTSIE		BIT(10)
107*48a6092fSMaxime Coquelin #define USART_CR3_ONEBIT	BIT(11)
108*48a6092fSMaxime Coquelin 
109*48a6092fSMaxime Coquelin /* USART_GTPR */
110*48a6092fSMaxime Coquelin #define USART_GTPR_PSC_MASK	GENMASK(7, 0)
111*48a6092fSMaxime Coquelin #define USART_GTPR_GT_MASK	GENMASK(15, 8)
112*48a6092fSMaxime Coquelin 
113*48a6092fSMaxime Coquelin #define DRIVER_NAME "stm32-usart"
114*48a6092fSMaxime Coquelin #define STM32_SERIAL_NAME "ttyS"
115*48a6092fSMaxime Coquelin #define STM32_MAX_PORTS 6
116*48a6092fSMaxime Coquelin 
117*48a6092fSMaxime Coquelin struct stm32_port {
118*48a6092fSMaxime Coquelin 	struct uart_port port;
119*48a6092fSMaxime Coquelin 	struct clk *clk;
120*48a6092fSMaxime Coquelin 	bool hw_flow_control;
121*48a6092fSMaxime Coquelin };
122*48a6092fSMaxime Coquelin 
123*48a6092fSMaxime Coquelin static struct stm32_port stm32_ports[STM32_MAX_PORTS];
124*48a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver;
125*48a6092fSMaxime Coquelin 
126*48a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port);
127*48a6092fSMaxime Coquelin 
128*48a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
129*48a6092fSMaxime Coquelin {
130*48a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
131*48a6092fSMaxime Coquelin }
132*48a6092fSMaxime Coquelin 
133*48a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
134*48a6092fSMaxime Coquelin {
135*48a6092fSMaxime Coquelin 	u32 val;
136*48a6092fSMaxime Coquelin 
137*48a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
138*48a6092fSMaxime Coquelin 	val |= bits;
139*48a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
140*48a6092fSMaxime Coquelin }
141*48a6092fSMaxime Coquelin 
142*48a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
143*48a6092fSMaxime Coquelin {
144*48a6092fSMaxime Coquelin 	u32 val;
145*48a6092fSMaxime Coquelin 
146*48a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
147*48a6092fSMaxime Coquelin 	val &= ~bits;
148*48a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
149*48a6092fSMaxime Coquelin }
150*48a6092fSMaxime Coquelin 
151*48a6092fSMaxime Coquelin static void stm32_receive_chars(struct uart_port *port)
152*48a6092fSMaxime Coquelin {
153*48a6092fSMaxime Coquelin 	struct tty_port *tport = &port->state->port;
154*48a6092fSMaxime Coquelin 	unsigned long c;
155*48a6092fSMaxime Coquelin 	u32 sr;
156*48a6092fSMaxime Coquelin 	char flag;
157*48a6092fSMaxime Coquelin 
158*48a6092fSMaxime Coquelin 	if (port->irq_wake)
159*48a6092fSMaxime Coquelin 		pm_wakeup_event(tport->tty->dev, 0);
160*48a6092fSMaxime Coquelin 
161*48a6092fSMaxime Coquelin 	while ((sr = readl_relaxed(port->membase + USART_SR)) & USART_SR_RXNE) {
162*48a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
163*48a6092fSMaxime Coquelin 		c = readl_relaxed(port->membase + USART_DR);
164*48a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
165*48a6092fSMaxime Coquelin 		port->icount.rx++;
166*48a6092fSMaxime Coquelin 
167*48a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
168*48a6092fSMaxime Coquelin 			if (sr & USART_SR_LBD) {
169*48a6092fSMaxime Coquelin 				port->icount.brk++;
170*48a6092fSMaxime Coquelin 				if (uart_handle_break(port))
171*48a6092fSMaxime Coquelin 					continue;
172*48a6092fSMaxime Coquelin 			} else if (sr & USART_SR_ORE) {
173*48a6092fSMaxime Coquelin 				port->icount.overrun++;
174*48a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
175*48a6092fSMaxime Coquelin 				port->icount.parity++;
176*48a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
177*48a6092fSMaxime Coquelin 				port->icount.frame++;
178*48a6092fSMaxime Coquelin 			}
179*48a6092fSMaxime Coquelin 
180*48a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
181*48a6092fSMaxime Coquelin 
182*48a6092fSMaxime Coquelin 			if (sr & USART_SR_LBD)
183*48a6092fSMaxime Coquelin 				flag = TTY_BREAK;
184*48a6092fSMaxime Coquelin 			else if (sr & USART_SR_PE)
185*48a6092fSMaxime Coquelin 				flag = TTY_PARITY;
186*48a6092fSMaxime Coquelin 			else if (sr & USART_SR_FE)
187*48a6092fSMaxime Coquelin 				flag = TTY_FRAME;
188*48a6092fSMaxime Coquelin 		}
189*48a6092fSMaxime Coquelin 
190*48a6092fSMaxime Coquelin 		if (uart_handle_sysrq_char(port, c))
191*48a6092fSMaxime Coquelin 			continue;
192*48a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
193*48a6092fSMaxime Coquelin 	}
194*48a6092fSMaxime Coquelin 
195*48a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
196*48a6092fSMaxime Coquelin 	tty_flip_buffer_push(tport);
197*48a6092fSMaxime Coquelin 	spin_lock(&port->lock);
198*48a6092fSMaxime Coquelin }
199*48a6092fSMaxime Coquelin 
200*48a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port)
201*48a6092fSMaxime Coquelin {
202*48a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
203*48a6092fSMaxime Coquelin 
204*48a6092fSMaxime Coquelin 	if (port->x_char) {
205*48a6092fSMaxime Coquelin 		writel_relaxed(port->x_char, port->membase + USART_DR);
206*48a6092fSMaxime Coquelin 		port->x_char = 0;
207*48a6092fSMaxime Coquelin 		port->icount.tx++;
208*48a6092fSMaxime Coquelin 		return;
209*48a6092fSMaxime Coquelin 	}
210*48a6092fSMaxime Coquelin 
211*48a6092fSMaxime Coquelin 	if (uart_tx_stopped(port)) {
212*48a6092fSMaxime Coquelin 		stm32_stop_tx(port);
213*48a6092fSMaxime Coquelin 		return;
214*48a6092fSMaxime Coquelin 	}
215*48a6092fSMaxime Coquelin 
216*48a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit)) {
217*48a6092fSMaxime Coquelin 		stm32_stop_tx(port);
218*48a6092fSMaxime Coquelin 		return;
219*48a6092fSMaxime Coquelin 	}
220*48a6092fSMaxime Coquelin 
221*48a6092fSMaxime Coquelin 	writel_relaxed(xmit->buf[xmit->tail], port->membase + USART_DR);
222*48a6092fSMaxime Coquelin 	xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
223*48a6092fSMaxime Coquelin 	port->icount.tx++;
224*48a6092fSMaxime Coquelin 
225*48a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
226*48a6092fSMaxime Coquelin 		uart_write_wakeup(port);
227*48a6092fSMaxime Coquelin 
228*48a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
229*48a6092fSMaxime Coquelin 		stm32_stop_tx(port);
230*48a6092fSMaxime Coquelin }
231*48a6092fSMaxime Coquelin 
232*48a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr)
233*48a6092fSMaxime Coquelin {
234*48a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
235*48a6092fSMaxime Coquelin 	u32 sr;
236*48a6092fSMaxime Coquelin 
237*48a6092fSMaxime Coquelin 	spin_lock(&port->lock);
238*48a6092fSMaxime Coquelin 
239*48a6092fSMaxime Coquelin 	sr = readl_relaxed(port->membase + USART_SR);
240*48a6092fSMaxime Coquelin 
241*48a6092fSMaxime Coquelin 	if (sr & USART_SR_RXNE)
242*48a6092fSMaxime Coquelin 		stm32_receive_chars(port);
243*48a6092fSMaxime Coquelin 
244*48a6092fSMaxime Coquelin 	if (sr & USART_SR_TXE)
245*48a6092fSMaxime Coquelin 		stm32_transmit_chars(port);
246*48a6092fSMaxime Coquelin 
247*48a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
248*48a6092fSMaxime Coquelin 
249*48a6092fSMaxime Coquelin 	return IRQ_HANDLED;
250*48a6092fSMaxime Coquelin }
251*48a6092fSMaxime Coquelin 
252*48a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port)
253*48a6092fSMaxime Coquelin {
254*48a6092fSMaxime Coquelin 	return readl_relaxed(port->membase + USART_SR) & USART_SR_TXE;
255*48a6092fSMaxime Coquelin }
256*48a6092fSMaxime Coquelin 
257*48a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
258*48a6092fSMaxime Coquelin {
259*48a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
260*48a6092fSMaxime Coquelin 		stm32_set_bits(port, USART_CR3, USART_CR3_RTSE);
261*48a6092fSMaxime Coquelin 	else
262*48a6092fSMaxime Coquelin 		stm32_clr_bits(port, USART_CR3, USART_CR3_RTSE);
263*48a6092fSMaxime Coquelin }
264*48a6092fSMaxime Coquelin 
265*48a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port)
266*48a6092fSMaxime Coquelin {
267*48a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
268*48a6092fSMaxime Coquelin 	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
269*48a6092fSMaxime Coquelin }
270*48a6092fSMaxime Coquelin 
271*48a6092fSMaxime Coquelin /* Transmit stop */
272*48a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port)
273*48a6092fSMaxime Coquelin {
274*48a6092fSMaxime Coquelin 	stm32_clr_bits(port, USART_CR1, USART_CR1_TXEIE);
275*48a6092fSMaxime Coquelin }
276*48a6092fSMaxime Coquelin 
277*48a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
278*48a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port)
279*48a6092fSMaxime Coquelin {
280*48a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
281*48a6092fSMaxime Coquelin 
282*48a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
283*48a6092fSMaxime Coquelin 		return;
284*48a6092fSMaxime Coquelin 
285*48a6092fSMaxime Coquelin 	stm32_set_bits(port, USART_CR1, USART_CR1_TXEIE | USART_CR1_TE);
286*48a6092fSMaxime Coquelin }
287*48a6092fSMaxime Coquelin 
288*48a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
289*48a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port)
290*48a6092fSMaxime Coquelin {
291*48a6092fSMaxime Coquelin 	unsigned long flags;
292*48a6092fSMaxime Coquelin 
293*48a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
294*48a6092fSMaxime Coquelin 	stm32_clr_bits(port, USART_CR1, USART_CR1_RXNEIE);
295*48a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
296*48a6092fSMaxime Coquelin }
297*48a6092fSMaxime Coquelin 
298*48a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
299*48a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port)
300*48a6092fSMaxime Coquelin {
301*48a6092fSMaxime Coquelin 	unsigned long flags;
302*48a6092fSMaxime Coquelin 
303*48a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
304*48a6092fSMaxime Coquelin 	stm32_set_bits(port, USART_CR1, USART_CR1_RXNEIE);
305*48a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
306*48a6092fSMaxime Coquelin }
307*48a6092fSMaxime Coquelin 
308*48a6092fSMaxime Coquelin /* Receive stop */
309*48a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port)
310*48a6092fSMaxime Coquelin {
311*48a6092fSMaxime Coquelin 	stm32_clr_bits(port, USART_CR1, USART_CR1_RXNEIE);
312*48a6092fSMaxime Coquelin }
313*48a6092fSMaxime Coquelin 
314*48a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
315*48a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state)
316*48a6092fSMaxime Coquelin {
317*48a6092fSMaxime Coquelin }
318*48a6092fSMaxime Coquelin 
319*48a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port)
320*48a6092fSMaxime Coquelin {
321*48a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
322*48a6092fSMaxime Coquelin 	u32 val;
323*48a6092fSMaxime Coquelin 	int ret;
324*48a6092fSMaxime Coquelin 
325*48a6092fSMaxime Coquelin 	ret = request_irq(port->irq, stm32_interrupt, IRQF_NO_SUSPEND,
326*48a6092fSMaxime Coquelin 			  name, port);
327*48a6092fSMaxime Coquelin 	if (ret)
328*48a6092fSMaxime Coquelin 		return ret;
329*48a6092fSMaxime Coquelin 
330*48a6092fSMaxime Coquelin 	val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
331*48a6092fSMaxime Coquelin 	stm32_set_bits(port, USART_CR1, val);
332*48a6092fSMaxime Coquelin 
333*48a6092fSMaxime Coquelin 	return 0;
334*48a6092fSMaxime Coquelin }
335*48a6092fSMaxime Coquelin 
336*48a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port)
337*48a6092fSMaxime Coquelin {
338*48a6092fSMaxime Coquelin 	u32 val;
339*48a6092fSMaxime Coquelin 
340*48a6092fSMaxime Coquelin 	val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
341*48a6092fSMaxime Coquelin 	stm32_set_bits(port, USART_CR1, val);
342*48a6092fSMaxime Coquelin 
343*48a6092fSMaxime Coquelin 	free_irq(port->irq, port);
344*48a6092fSMaxime Coquelin }
345*48a6092fSMaxime Coquelin 
346*48a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
347*48a6092fSMaxime Coquelin 			    struct ktermios *old)
348*48a6092fSMaxime Coquelin {
349*48a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
350*48a6092fSMaxime Coquelin 	unsigned int baud;
351*48a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
352*48a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
353*48a6092fSMaxime Coquelin 	u32 cr1, cr2, cr3;
354*48a6092fSMaxime Coquelin 	unsigned long flags;
355*48a6092fSMaxime Coquelin 
356*48a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
357*48a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
358*48a6092fSMaxime Coquelin 
359*48a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
360*48a6092fSMaxime Coquelin 
361*48a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
362*48a6092fSMaxime Coquelin 
363*48a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
364*48a6092fSMaxime Coquelin 	writel_relaxed(0, port->membase + USART_CR1);
365*48a6092fSMaxime Coquelin 
366*48a6092fSMaxime Coquelin 	cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE;
367*48a6092fSMaxime Coquelin 	cr2 = 0;
368*48a6092fSMaxime Coquelin 	cr3 = 0;
369*48a6092fSMaxime Coquelin 
370*48a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
371*48a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
372*48a6092fSMaxime Coquelin 
373*48a6092fSMaxime Coquelin 	if (cflag & PARENB) {
374*48a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
375*48a6092fSMaxime Coquelin 		if ((cflag & CSIZE) == CS8)
376*48a6092fSMaxime Coquelin 			cr1 |= USART_CR1_M;
377*48a6092fSMaxime Coquelin 	}
378*48a6092fSMaxime Coquelin 
379*48a6092fSMaxime Coquelin 	if (cflag & PARODD)
380*48a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
381*48a6092fSMaxime Coquelin 
382*48a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
383*48a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
384*48a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
385*48a6092fSMaxime Coquelin 		cr3 |= USART_CR3_CTSE;
386*48a6092fSMaxime Coquelin 	}
387*48a6092fSMaxime Coquelin 
388*48a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
389*48a6092fSMaxime Coquelin 
390*48a6092fSMaxime Coquelin 	/*
391*48a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
392*48a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
393*48a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
394*48a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
395*48a6092fSMaxime Coquelin 	 */
396*48a6092fSMaxime Coquelin 	if (usartdiv < 16) {
397*48a6092fSMaxime Coquelin 		oversampling = 8;
398*48a6092fSMaxime Coquelin 		stm32_set_bits(port, USART_CR1, USART_CR1_OVER8);
399*48a6092fSMaxime Coquelin 	} else {
400*48a6092fSMaxime Coquelin 		oversampling = 16;
401*48a6092fSMaxime Coquelin 		stm32_clr_bits(port, USART_CR1, USART_CR1_OVER8);
402*48a6092fSMaxime Coquelin 	}
403*48a6092fSMaxime Coquelin 
404*48a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
405*48a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
406*48a6092fSMaxime Coquelin 	writel_relaxed(mantissa | fraction, port->membase + USART_BRR);
407*48a6092fSMaxime Coquelin 
408*48a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
409*48a6092fSMaxime Coquelin 
410*48a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
411*48a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
412*48a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
413*48a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
414*48a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_LBD;
415*48a6092fSMaxime Coquelin 
416*48a6092fSMaxime Coquelin 	/* Characters to ignore */
417*48a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
418*48a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
419*48a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
420*48a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
421*48a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_LBD;
422*48a6092fSMaxime Coquelin 		/*
423*48a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
424*48a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
425*48a6092fSMaxime Coquelin 		 */
426*48a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
427*48a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
428*48a6092fSMaxime Coquelin 	}
429*48a6092fSMaxime Coquelin 
430*48a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
431*48a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
432*48a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
433*48a6092fSMaxime Coquelin 
434*48a6092fSMaxime Coquelin 	writel_relaxed(cr3, port->membase + USART_CR3);
435*48a6092fSMaxime Coquelin 	writel_relaxed(cr2, port->membase + USART_CR2);
436*48a6092fSMaxime Coquelin 	writel_relaxed(cr1, port->membase + USART_CR1);
437*48a6092fSMaxime Coquelin 
438*48a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
439*48a6092fSMaxime Coquelin }
440*48a6092fSMaxime Coquelin 
441*48a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port)
442*48a6092fSMaxime Coquelin {
443*48a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
444*48a6092fSMaxime Coquelin }
445*48a6092fSMaxime Coquelin 
446*48a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port)
447*48a6092fSMaxime Coquelin {
448*48a6092fSMaxime Coquelin }
449*48a6092fSMaxime Coquelin 
450*48a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port)
451*48a6092fSMaxime Coquelin {
452*48a6092fSMaxime Coquelin 	return 0;
453*48a6092fSMaxime Coquelin }
454*48a6092fSMaxime Coquelin 
455*48a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags)
456*48a6092fSMaxime Coquelin {
457*48a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
458*48a6092fSMaxime Coquelin 		port->type = PORT_STM32;
459*48a6092fSMaxime Coquelin }
460*48a6092fSMaxime Coquelin 
461*48a6092fSMaxime Coquelin static int
462*48a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
463*48a6092fSMaxime Coquelin {
464*48a6092fSMaxime Coquelin 	/* No user changeable parameters */
465*48a6092fSMaxime Coquelin 	return -EINVAL;
466*48a6092fSMaxime Coquelin }
467*48a6092fSMaxime Coquelin 
468*48a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state,
469*48a6092fSMaxime Coquelin 		unsigned int oldstate)
470*48a6092fSMaxime Coquelin {
471*48a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
472*48a6092fSMaxime Coquelin 			struct stm32_port, port);
473*48a6092fSMaxime Coquelin 	unsigned long flags = 0;
474*48a6092fSMaxime Coquelin 
475*48a6092fSMaxime Coquelin 	switch (state) {
476*48a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
477*48a6092fSMaxime Coquelin 		clk_prepare_enable(stm32port->clk);
478*48a6092fSMaxime Coquelin 		break;
479*48a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
480*48a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
481*48a6092fSMaxime Coquelin 		stm32_clr_bits(port, USART_CR1, USART_CR1_UE);
482*48a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
483*48a6092fSMaxime Coquelin 		clk_disable_unprepare(stm32port->clk);
484*48a6092fSMaxime Coquelin 		break;
485*48a6092fSMaxime Coquelin 	}
486*48a6092fSMaxime Coquelin }
487*48a6092fSMaxime Coquelin 
488*48a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
489*48a6092fSMaxime Coquelin 	.tx_empty	= stm32_tx_empty,
490*48a6092fSMaxime Coquelin 	.set_mctrl	= stm32_set_mctrl,
491*48a6092fSMaxime Coquelin 	.get_mctrl	= stm32_get_mctrl,
492*48a6092fSMaxime Coquelin 	.stop_tx	= stm32_stop_tx,
493*48a6092fSMaxime Coquelin 	.start_tx	= stm32_start_tx,
494*48a6092fSMaxime Coquelin 	.throttle	= stm32_throttle,
495*48a6092fSMaxime Coquelin 	.unthrottle	= stm32_unthrottle,
496*48a6092fSMaxime Coquelin 	.stop_rx	= stm32_stop_rx,
497*48a6092fSMaxime Coquelin 	.break_ctl	= stm32_break_ctl,
498*48a6092fSMaxime Coquelin 	.startup	= stm32_startup,
499*48a6092fSMaxime Coquelin 	.shutdown	= stm32_shutdown,
500*48a6092fSMaxime Coquelin 	.set_termios	= stm32_set_termios,
501*48a6092fSMaxime Coquelin 	.pm		= stm32_pm,
502*48a6092fSMaxime Coquelin 	.type		= stm32_type,
503*48a6092fSMaxime Coquelin 	.release_port	= stm32_release_port,
504*48a6092fSMaxime Coquelin 	.request_port	= stm32_request_port,
505*48a6092fSMaxime Coquelin 	.config_port	= stm32_config_port,
506*48a6092fSMaxime Coquelin 	.verify_port	= stm32_verify_port,
507*48a6092fSMaxime Coquelin };
508*48a6092fSMaxime Coquelin 
509*48a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port,
510*48a6092fSMaxime Coquelin 			  struct platform_device *pdev)
511*48a6092fSMaxime Coquelin {
512*48a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
513*48a6092fSMaxime Coquelin 	struct resource *res;
514*48a6092fSMaxime Coquelin 	int ret;
515*48a6092fSMaxime Coquelin 
516*48a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
517*48a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
518*48a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
519*48a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
520*48a6092fSMaxime Coquelin 	port->irq	= platform_get_irq(pdev, 0);
521*48a6092fSMaxime Coquelin 
522*48a6092fSMaxime Coquelin 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
523*48a6092fSMaxime Coquelin 	port->membase = devm_ioremap_resource(&pdev->dev, res);
524*48a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
525*48a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
526*48a6092fSMaxime Coquelin 	port->mapbase = res->start;
527*48a6092fSMaxime Coquelin 
528*48a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
529*48a6092fSMaxime Coquelin 
530*48a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
531*48a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
532*48a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
533*48a6092fSMaxime Coquelin 
534*48a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
535*48a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
536*48a6092fSMaxime Coquelin 	if (ret)
537*48a6092fSMaxime Coquelin 		return ret;
538*48a6092fSMaxime Coquelin 
539*48a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
540*48a6092fSMaxime Coquelin 	if (!stm32port->port.uartclk)
541*48a6092fSMaxime Coquelin 		ret = -EINVAL;
542*48a6092fSMaxime Coquelin 
543*48a6092fSMaxime Coquelin 	clk_disable_unprepare(stm32port->clk);
544*48a6092fSMaxime Coquelin 
545*48a6092fSMaxime Coquelin 	return ret;
546*48a6092fSMaxime Coquelin }
547*48a6092fSMaxime Coquelin 
548*48a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
549*48a6092fSMaxime Coquelin {
550*48a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
551*48a6092fSMaxime Coquelin 	int id;
552*48a6092fSMaxime Coquelin 
553*48a6092fSMaxime Coquelin 	if (!np)
554*48a6092fSMaxime Coquelin 		return NULL;
555*48a6092fSMaxime Coquelin 
556*48a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
557*48a6092fSMaxime Coquelin 	if (id < 0)
558*48a6092fSMaxime Coquelin 		id = 0;
559*48a6092fSMaxime Coquelin 
560*48a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
561*48a6092fSMaxime Coquelin 		return NULL;
562*48a6092fSMaxime Coquelin 
563*48a6092fSMaxime Coquelin 	stm32_ports[id].hw_flow_control = of_property_read_bool(np,
564*48a6092fSMaxime Coquelin 							"auto-flow-control");
565*48a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
566*48a6092fSMaxime Coquelin 	return &stm32_ports[id];
567*48a6092fSMaxime Coquelin }
568*48a6092fSMaxime Coquelin 
569*48a6092fSMaxime Coquelin #ifdef CONFIG_OF
570*48a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
571*48a6092fSMaxime Coquelin 	{ .compatible = "st,stm32-usart", },
572*48a6092fSMaxime Coquelin 	{ .compatible = "st,stm32-uart", },
573*48a6092fSMaxime Coquelin 	{},
574*48a6092fSMaxime Coquelin };
575*48a6092fSMaxime Coquelin 
576*48a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
577*48a6092fSMaxime Coquelin #endif
578*48a6092fSMaxime Coquelin 
579*48a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev)
580*48a6092fSMaxime Coquelin {
581*48a6092fSMaxime Coquelin 	int ret;
582*48a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
583*48a6092fSMaxime Coquelin 
584*48a6092fSMaxime Coquelin 	stm32port = stm32_of_get_stm32_port(pdev);
585*48a6092fSMaxime Coquelin 	if (!stm32port)
586*48a6092fSMaxime Coquelin 		return -ENODEV;
587*48a6092fSMaxime Coquelin 
588*48a6092fSMaxime Coquelin 	ret = stm32_init_port(stm32port, pdev);
589*48a6092fSMaxime Coquelin 	if (ret)
590*48a6092fSMaxime Coquelin 		return ret;
591*48a6092fSMaxime Coquelin 
592*48a6092fSMaxime Coquelin 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
593*48a6092fSMaxime Coquelin 	if (ret)
594*48a6092fSMaxime Coquelin 		return ret;
595*48a6092fSMaxime Coquelin 
596*48a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
597*48a6092fSMaxime Coquelin 
598*48a6092fSMaxime Coquelin 	return 0;
599*48a6092fSMaxime Coquelin }
600*48a6092fSMaxime Coquelin 
601*48a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev)
602*48a6092fSMaxime Coquelin {
603*48a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
604*48a6092fSMaxime Coquelin 
605*48a6092fSMaxime Coquelin 	return uart_remove_one_port(&stm32_usart_driver, port);
606*48a6092fSMaxime Coquelin }
607*48a6092fSMaxime Coquelin 
608*48a6092fSMaxime Coquelin 
609*48a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE
610*48a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch)
611*48a6092fSMaxime Coquelin {
612*48a6092fSMaxime Coquelin 	while (!(readl_relaxed(port->membase + USART_SR) & USART_SR_TXE))
613*48a6092fSMaxime Coquelin 		cpu_relax();
614*48a6092fSMaxime Coquelin 
615*48a6092fSMaxime Coquelin 	writel_relaxed(ch, port->membase + USART_DR);
616*48a6092fSMaxime Coquelin }
617*48a6092fSMaxime Coquelin 
618*48a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
619*48a6092fSMaxime Coquelin {
620*48a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
621*48a6092fSMaxime Coquelin 	unsigned long flags;
622*48a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
623*48a6092fSMaxime Coquelin 	int locked = 1;
624*48a6092fSMaxime Coquelin 
625*48a6092fSMaxime Coquelin 	local_irq_save(flags);
626*48a6092fSMaxime Coquelin 	if (port->sysrq)
627*48a6092fSMaxime Coquelin 		locked = 0;
628*48a6092fSMaxime Coquelin 	else if (oops_in_progress)
629*48a6092fSMaxime Coquelin 		locked = spin_trylock(&port->lock);
630*48a6092fSMaxime Coquelin 	else
631*48a6092fSMaxime Coquelin 		spin_lock(&port->lock);
632*48a6092fSMaxime Coquelin 
633*48a6092fSMaxime Coquelin 	/* Save and disable interrupts */
634*48a6092fSMaxime Coquelin 	old_cr1 = readl_relaxed(port->membase + USART_CR1);
635*48a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
636*48a6092fSMaxime Coquelin 	writel_relaxed(new_cr1, port->membase + USART_CR1);
637*48a6092fSMaxime Coquelin 
638*48a6092fSMaxime Coquelin 	uart_console_write(port, s, cnt, stm32_console_putchar);
639*48a6092fSMaxime Coquelin 
640*48a6092fSMaxime Coquelin 	/* Restore interrupt state */
641*48a6092fSMaxime Coquelin 	writel_relaxed(old_cr1, port->membase + USART_CR1);
642*48a6092fSMaxime Coquelin 
643*48a6092fSMaxime Coquelin 	if (locked)
644*48a6092fSMaxime Coquelin 		spin_unlock(&port->lock);
645*48a6092fSMaxime Coquelin 	local_irq_restore(flags);
646*48a6092fSMaxime Coquelin }
647*48a6092fSMaxime Coquelin 
648*48a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options)
649*48a6092fSMaxime Coquelin {
650*48a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
651*48a6092fSMaxime Coquelin 	int baud = 9600;
652*48a6092fSMaxime Coquelin 	int bits = 8;
653*48a6092fSMaxime Coquelin 	int parity = 'n';
654*48a6092fSMaxime Coquelin 	int flow = 'n';
655*48a6092fSMaxime Coquelin 
656*48a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
657*48a6092fSMaxime Coquelin 		return -ENODEV;
658*48a6092fSMaxime Coquelin 
659*48a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
660*48a6092fSMaxime Coquelin 
661*48a6092fSMaxime Coquelin 	/*
662*48a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
663*48a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
664*48a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
665*48a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
666*48a6092fSMaxime Coquelin 	 */
667*48a6092fSMaxime Coquelin 	if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
668*48a6092fSMaxime Coquelin 		return -ENXIO;
669*48a6092fSMaxime Coquelin 
670*48a6092fSMaxime Coquelin 	if (options)
671*48a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
672*48a6092fSMaxime Coquelin 
673*48a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
674*48a6092fSMaxime Coquelin }
675*48a6092fSMaxime Coquelin 
676*48a6092fSMaxime Coquelin static struct console stm32_console = {
677*48a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
678*48a6092fSMaxime Coquelin 	.device		= uart_console_device,
679*48a6092fSMaxime Coquelin 	.write		= stm32_console_write,
680*48a6092fSMaxime Coquelin 	.setup		= stm32_console_setup,
681*48a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
682*48a6092fSMaxime Coquelin 	.index		= -1,
683*48a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
684*48a6092fSMaxime Coquelin };
685*48a6092fSMaxime Coquelin 
686*48a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
687*48a6092fSMaxime Coquelin 
688*48a6092fSMaxime Coquelin #else
689*48a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
690*48a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
691*48a6092fSMaxime Coquelin 
692*48a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
693*48a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
694*48a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
695*48a6092fSMaxime Coquelin 	.major		= 0,
696*48a6092fSMaxime Coquelin 	.minor		= 0,
697*48a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
698*48a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
699*48a6092fSMaxime Coquelin };
700*48a6092fSMaxime Coquelin 
701*48a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
702*48a6092fSMaxime Coquelin 	.probe		= stm32_serial_probe,
703*48a6092fSMaxime Coquelin 	.remove		= stm32_serial_remove,
704*48a6092fSMaxime Coquelin 	.driver	= {
705*48a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
706*48a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
707*48a6092fSMaxime Coquelin 	},
708*48a6092fSMaxime Coquelin };
709*48a6092fSMaxime Coquelin 
710*48a6092fSMaxime Coquelin static int __init usart_init(void)
711*48a6092fSMaxime Coquelin {
712*48a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
713*48a6092fSMaxime Coquelin 	int ret;
714*48a6092fSMaxime Coquelin 
715*48a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
716*48a6092fSMaxime Coquelin 
717*48a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
718*48a6092fSMaxime Coquelin 	if (ret)
719*48a6092fSMaxime Coquelin 		return ret;
720*48a6092fSMaxime Coquelin 
721*48a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
722*48a6092fSMaxime Coquelin 	if (ret)
723*48a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
724*48a6092fSMaxime Coquelin 
725*48a6092fSMaxime Coquelin 	return ret;
726*48a6092fSMaxime Coquelin }
727*48a6092fSMaxime Coquelin 
728*48a6092fSMaxime Coquelin static void __exit usart_exit(void)
729*48a6092fSMaxime Coquelin {
730*48a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
731*48a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
732*48a6092fSMaxime Coquelin }
733*48a6092fSMaxime Coquelin 
734*48a6092fSMaxime Coquelin module_init(usart_init);
735*48a6092fSMaxime Coquelin module_exit(usart_exit);
736*48a6092fSMaxime Coquelin 
737*48a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
738*48a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
739*48a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
740