xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 7f28bcea824e4fb192214c6121161053efe738a8)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
248a6092fSMaxime Coquelin /*
348a6092fSMaxime Coquelin  * Copyright (C) Maxime Coquelin 2015
43e5fcbacSBich HEMON  * Copyright (C) STMicroelectronics SA 2017
5ada8618fSAlexandre TORGUE  * Authors:  Maxime Coquelin <mcoquelin.stm32@gmail.com>
68ebd9665SErwan Le Ray  *	     Gerald Baeza <gerald.baeza@foss.st.com>
78ebd9665SErwan Le Ray  *	     Erwan Le Ray <erwan.leray@foss.st.com>
848a6092fSMaxime Coquelin  *
948a6092fSMaxime Coquelin  * Inspired by st-asc.c from STMicroelectronics (c)
1048a6092fSMaxime Coquelin  */
1148a6092fSMaxime Coquelin 
1234891872SAlexandre TORGUE #include <linux/clk.h>
1348a6092fSMaxime Coquelin #include <linux/console.h>
1448a6092fSMaxime Coquelin #include <linux/delay.h>
1534891872SAlexandre TORGUE #include <linux/dma-direction.h>
1634891872SAlexandre TORGUE #include <linux/dmaengine.h>
1734891872SAlexandre TORGUE #include <linux/dma-mapping.h>
1834891872SAlexandre TORGUE #include <linux/io.h>
1934891872SAlexandre TORGUE #include <linux/iopoll.h>
2034891872SAlexandre TORGUE #include <linux/irq.h>
2134891872SAlexandre TORGUE #include <linux/module.h>
2248a6092fSMaxime Coquelin #include <linux/of.h>
2348a6092fSMaxime Coquelin #include <linux/of_platform.h>
2494616d9aSErwan Le Ray #include <linux/pinctrl/consumer.h>
2534891872SAlexandre TORGUE #include <linux/platform_device.h>
2634891872SAlexandre TORGUE #include <linux/pm_runtime.h>
27270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h>
2848a6092fSMaxime Coquelin #include <linux/serial_core.h>
2934891872SAlexandre TORGUE #include <linux/serial.h>
3034891872SAlexandre TORGUE #include <linux/spinlock.h>
3134891872SAlexandre TORGUE #include <linux/sysrq.h>
3234891872SAlexandre TORGUE #include <linux/tty_flip.h>
3334891872SAlexandre TORGUE #include <linux/tty.h>
3448a6092fSMaxime Coquelin 
356cf61b9bSManivannan Sadhasivam #include "serial_mctrl_gpio.h"
36bc5a0b55SAlexandre TORGUE #include "stm32-usart.h"
3748a6092fSMaxime Coquelin 
38c7039ce9SBen Dooks 
39c7039ce9SBen Dooks /* Register offsets */
40dfdabd38SRen Zhijie static struct stm32_usart_info __maybe_unused stm32f4_info = {
41c7039ce9SBen Dooks 	.ofs = {
42c7039ce9SBen Dooks 		.isr	= 0x00,
43c7039ce9SBen Dooks 		.rdr	= 0x04,
44c7039ce9SBen Dooks 		.tdr	= 0x04,
45c7039ce9SBen Dooks 		.brr	= 0x08,
46c7039ce9SBen Dooks 		.cr1	= 0x0c,
47c7039ce9SBen Dooks 		.cr2	= 0x10,
48c7039ce9SBen Dooks 		.cr3	= 0x14,
49c7039ce9SBen Dooks 		.gtpr	= 0x18,
50c7039ce9SBen Dooks 		.rtor	= UNDEF_REG,
51c7039ce9SBen Dooks 		.rqr	= UNDEF_REG,
52c7039ce9SBen Dooks 		.icr	= UNDEF_REG,
53c7039ce9SBen Dooks 	},
54c7039ce9SBen Dooks 	.cfg = {
55c7039ce9SBen Dooks 		.uart_enable_bit = 13,
56c7039ce9SBen Dooks 		.has_7bits_data = false,
57c7039ce9SBen Dooks 		.fifosize = 1,
58c7039ce9SBen Dooks 	}
59c7039ce9SBen Dooks };
60c7039ce9SBen Dooks 
61dfdabd38SRen Zhijie static struct stm32_usart_info __maybe_unused stm32f7_info = {
62c7039ce9SBen Dooks 	.ofs = {
63c7039ce9SBen Dooks 		.cr1	= 0x00,
64c7039ce9SBen Dooks 		.cr2	= 0x04,
65c7039ce9SBen Dooks 		.cr3	= 0x08,
66c7039ce9SBen Dooks 		.brr	= 0x0c,
67c7039ce9SBen Dooks 		.gtpr	= 0x10,
68c7039ce9SBen Dooks 		.rtor	= 0x14,
69c7039ce9SBen Dooks 		.rqr	= 0x18,
70c7039ce9SBen Dooks 		.isr	= 0x1c,
71c7039ce9SBen Dooks 		.icr	= 0x20,
72c7039ce9SBen Dooks 		.rdr	= 0x24,
73c7039ce9SBen Dooks 		.tdr	= 0x28,
74c7039ce9SBen Dooks 	},
75c7039ce9SBen Dooks 	.cfg = {
76c7039ce9SBen Dooks 		.uart_enable_bit = 0,
77c7039ce9SBen Dooks 		.has_7bits_data = true,
78c7039ce9SBen Dooks 		.has_swap = true,
79c7039ce9SBen Dooks 		.fifosize = 1,
80c7039ce9SBen Dooks 	}
81c7039ce9SBen Dooks };
82c7039ce9SBen Dooks 
83dfdabd38SRen Zhijie static struct stm32_usart_info __maybe_unused stm32h7_info = {
84c7039ce9SBen Dooks 	.ofs = {
85c7039ce9SBen Dooks 		.cr1	= 0x00,
86c7039ce9SBen Dooks 		.cr2	= 0x04,
87c7039ce9SBen Dooks 		.cr3	= 0x08,
88c7039ce9SBen Dooks 		.brr	= 0x0c,
89c7039ce9SBen Dooks 		.gtpr	= 0x10,
90c7039ce9SBen Dooks 		.rtor	= 0x14,
91c7039ce9SBen Dooks 		.rqr	= 0x18,
92c7039ce9SBen Dooks 		.isr	= 0x1c,
93c7039ce9SBen Dooks 		.icr	= 0x20,
94c7039ce9SBen Dooks 		.rdr	= 0x24,
95c7039ce9SBen Dooks 		.tdr	= 0x28,
96c7039ce9SBen Dooks 	},
97c7039ce9SBen Dooks 	.cfg = {
98c7039ce9SBen Dooks 		.uart_enable_bit = 0,
99c7039ce9SBen Dooks 		.has_7bits_data = true,
100c7039ce9SBen Dooks 		.has_swap = true,
101c7039ce9SBen Dooks 		.has_wakeup = true,
102c7039ce9SBen Dooks 		.has_fifo = true,
103c7039ce9SBen Dooks 		.fifosize = 16,
104c7039ce9SBen Dooks 	}
105c7039ce9SBen Dooks };
106c7039ce9SBen Dooks 
10756f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port);
10856f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port);
1091f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch);
11048a6092fSMaxime Coquelin 
11148a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
11248a6092fSMaxime Coquelin {
11348a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
11448a6092fSMaxime Coquelin }
11548a6092fSMaxime Coquelin 
11656f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
11748a6092fSMaxime Coquelin {
11848a6092fSMaxime Coquelin 	u32 val;
11948a6092fSMaxime Coquelin 
12048a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
12148a6092fSMaxime Coquelin 	val |= bits;
12248a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
12348a6092fSMaxime Coquelin }
12448a6092fSMaxime Coquelin 
12556f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
12648a6092fSMaxime Coquelin {
12748a6092fSMaxime Coquelin 	u32 val;
12848a6092fSMaxime Coquelin 
12948a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
13048a6092fSMaxime Coquelin 	val &= ~bits;
13148a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
13248a6092fSMaxime Coquelin }
13348a6092fSMaxime Coquelin 
134adafbbf6SLukas Wunner static unsigned int stm32_usart_tx_empty(struct uart_port *port)
135adafbbf6SLukas Wunner {
136adafbbf6SLukas Wunner 	struct stm32_port *stm32_port = to_stm32_port(port);
137adafbbf6SLukas Wunner 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
138adafbbf6SLukas Wunner 
139adafbbf6SLukas Wunner 	if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
140adafbbf6SLukas Wunner 		return TIOCSER_TEMT;
141adafbbf6SLukas Wunner 
142adafbbf6SLukas Wunner 	return 0;
143adafbbf6SLukas Wunner }
144adafbbf6SLukas Wunner 
145adafbbf6SLukas Wunner static void stm32_usart_rs485_rts_enable(struct uart_port *port)
146adafbbf6SLukas Wunner {
147adafbbf6SLukas Wunner 	struct stm32_port *stm32_port = to_stm32_port(port);
148adafbbf6SLukas Wunner 	struct serial_rs485 *rs485conf = &port->rs485;
149adafbbf6SLukas Wunner 
150adafbbf6SLukas Wunner 	if (stm32_port->hw_flow_control ||
151adafbbf6SLukas Wunner 	    !(rs485conf->flags & SER_RS485_ENABLED))
152adafbbf6SLukas Wunner 		return;
153adafbbf6SLukas Wunner 
154adafbbf6SLukas Wunner 	if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
155adafbbf6SLukas Wunner 		mctrl_gpio_set(stm32_port->gpios,
156adafbbf6SLukas Wunner 			       stm32_port->port.mctrl | TIOCM_RTS);
157adafbbf6SLukas Wunner 	} else {
158adafbbf6SLukas Wunner 		mctrl_gpio_set(stm32_port->gpios,
159adafbbf6SLukas Wunner 			       stm32_port->port.mctrl & ~TIOCM_RTS);
160adafbbf6SLukas Wunner 	}
161adafbbf6SLukas Wunner }
162adafbbf6SLukas Wunner 
163adafbbf6SLukas Wunner static void stm32_usart_rs485_rts_disable(struct uart_port *port)
164adafbbf6SLukas Wunner {
165adafbbf6SLukas Wunner 	struct stm32_port *stm32_port = to_stm32_port(port);
166adafbbf6SLukas Wunner 	struct serial_rs485 *rs485conf = &port->rs485;
167adafbbf6SLukas Wunner 
168adafbbf6SLukas Wunner 	if (stm32_port->hw_flow_control ||
169adafbbf6SLukas Wunner 	    !(rs485conf->flags & SER_RS485_ENABLED))
170adafbbf6SLukas Wunner 		return;
171adafbbf6SLukas Wunner 
172adafbbf6SLukas Wunner 	if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
173adafbbf6SLukas Wunner 		mctrl_gpio_set(stm32_port->gpios,
174adafbbf6SLukas Wunner 			       stm32_port->port.mctrl & ~TIOCM_RTS);
175adafbbf6SLukas Wunner 	} else {
176adafbbf6SLukas Wunner 		mctrl_gpio_set(stm32_port->gpios,
177adafbbf6SLukas Wunner 			       stm32_port->port.mctrl | TIOCM_RTS);
178adafbbf6SLukas Wunner 	}
179adafbbf6SLukas Wunner }
180adafbbf6SLukas Wunner 
18156f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
1821bcda09dSBich HEMON 					 u32 delay_DDE, u32 baud)
1831bcda09dSBich HEMON {
1841bcda09dSBich HEMON 	u32 rs485_deat_dedt;
1851bcda09dSBich HEMON 	u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
1861bcda09dSBich HEMON 	bool over8;
1871bcda09dSBich HEMON 
1881bcda09dSBich HEMON 	*cr3 |= USART_CR3_DEM;
1891bcda09dSBich HEMON 	over8 = *cr1 & USART_CR1_OVER8;
1901bcda09dSBich HEMON 
1915c5f44e3SIlpo Järvinen 	*cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1925c5f44e3SIlpo Järvinen 
1931bcda09dSBich HEMON 	if (over8)
1941bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 8;
1951bcda09dSBich HEMON 	else
1961bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 16;
1971bcda09dSBich HEMON 
1981bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
1991bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
2001bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
2011bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
2021bcda09dSBich HEMON 			   USART_CR1_DEAT_MASK;
2031bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
2041bcda09dSBich HEMON 
2051bcda09dSBich HEMON 	if (over8)
2061bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 8;
2071bcda09dSBich HEMON 	else
2081bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 16;
2091bcda09dSBich HEMON 
2101bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
2111bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
2121bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
2131bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
2141bcda09dSBich HEMON 			   USART_CR1_DEDT_MASK;
2151bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
2161bcda09dSBich HEMON }
2171bcda09dSBich HEMON 
218ae50bb27SIlpo Järvinen static int stm32_usart_config_rs485(struct uart_port *port, struct ktermios *termios,
2191bcda09dSBich HEMON 				    struct serial_rs485 *rs485conf)
2201bcda09dSBich HEMON {
2211bcda09dSBich HEMON 	struct stm32_port *stm32_port = to_stm32_port(port);
222d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
223d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
2241bcda09dSBich HEMON 	u32 usartdiv, baud, cr1, cr3;
2251bcda09dSBich HEMON 	bool over8;
2261bcda09dSBich HEMON 
22756f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
2281bcda09dSBich HEMON 
229c54d4854SChristoph Niedermaier 	if (port->rs485_rx_during_tx_gpio)
230c54d4854SChristoph Niedermaier 		gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
231c54d4854SChristoph Niedermaier 					 !!(rs485conf->flags & SER_RS485_RX_DURING_TX));
232c54d4854SChristoph Niedermaier 	else
2331bcda09dSBich HEMON 		rs485conf->flags |= SER_RS485_RX_DURING_TX;
2341bcda09dSBich HEMON 
2351bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
2361bcda09dSBich HEMON 		cr1 = readl_relaxed(port->membase + ofs->cr1);
2371bcda09dSBich HEMON 		cr3 = readl_relaxed(port->membase + ofs->cr3);
2381bcda09dSBich HEMON 		usartdiv = readl_relaxed(port->membase + ofs->brr);
2391bcda09dSBich HEMON 		usartdiv = usartdiv & GENMASK(15, 0);
2401bcda09dSBich HEMON 		over8 = cr1 & USART_CR1_OVER8;
2411bcda09dSBich HEMON 
2421bcda09dSBich HEMON 		if (over8)
2431bcda09dSBich HEMON 			usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
2441bcda09dSBich HEMON 				   << USART_BRR_04_R_SHIFT;
2451bcda09dSBich HEMON 
2461bcda09dSBich HEMON 		baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
24756f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
2481bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
24956f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
25056f9a76cSErwan Le Ray 					     baud);
2511bcda09dSBich HEMON 
252f633eb29SLino Sanfilippo 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND)
2531bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
254f633eb29SLino Sanfilippo 		else
2551bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
2561bcda09dSBich HEMON 
2571bcda09dSBich HEMON 		writel_relaxed(cr3, port->membase + ofs->cr3);
2581bcda09dSBich HEMON 		writel_relaxed(cr1, port->membase + ofs->cr1);
2591bcda09dSBich HEMON 	} else {
26056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3,
26156f9a76cSErwan Le Ray 				     USART_CR3_DEM | USART_CR3_DEP);
26256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1,
2631bcda09dSBich HEMON 				     USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
2641bcda09dSBich HEMON 	}
2651bcda09dSBich HEMON 
26656f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
2671bcda09dSBich HEMON 
268adafbbf6SLukas Wunner 	/* Adjust RTS polarity in case it's driven in software */
269adafbbf6SLukas Wunner 	if (stm32_usart_tx_empty(port))
270adafbbf6SLukas Wunner 		stm32_usart_rs485_rts_disable(port);
271adafbbf6SLukas Wunner 	else
272adafbbf6SLukas Wunner 		stm32_usart_rs485_rts_enable(port);
273adafbbf6SLukas Wunner 
2741bcda09dSBich HEMON 	return 0;
2751bcda09dSBich HEMON }
2761bcda09dSBich HEMON 
27756f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port,
2781bcda09dSBich HEMON 				  struct platform_device *pdev)
2791bcda09dSBich HEMON {
2801bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
2811bcda09dSBich HEMON 
2821bcda09dSBich HEMON 	rs485conf->flags = 0;
2831bcda09dSBich HEMON 	rs485conf->delay_rts_before_send = 0;
2841bcda09dSBich HEMON 	rs485conf->delay_rts_after_send = 0;
2851bcda09dSBich HEMON 
2861bcda09dSBich HEMON 	if (!pdev->dev.of_node)
2871bcda09dSBich HEMON 		return -ENODEV;
2881bcda09dSBich HEMON 
289c150c0f3SLukas Wunner 	return uart_get_rs485_mode(port);
2901bcda09dSBich HEMON }
2911bcda09dSBich HEMON 
29200d1f9c6SValentin Caron static bool stm32_usart_rx_dma_started(struct stm32_port *stm32_port)
29334891872SAlexandre TORGUE {
294*7f28bceaSValentin Caron 	return stm32_port->rx_ch ? stm32_port->rx_dma_busy : false;
295*7f28bceaSValentin Caron }
296*7f28bceaSValentin Caron 
297*7f28bceaSValentin Caron static void stm32_usart_rx_dma_terminate(struct stm32_port *stm32_port)
298*7f28bceaSValentin Caron {
299*7f28bceaSValentin Caron 	dmaengine_terminate_async(stm32_port->rx_ch);
300*7f28bceaSValentin Caron 	stm32_port->rx_dma_busy = false;
301*7f28bceaSValentin Caron }
302*7f28bceaSValentin Caron 
303*7f28bceaSValentin Caron static int stm32_usart_dma_pause_resume(struct stm32_port *stm32_port,
304*7f28bceaSValentin Caron 					struct dma_chan *chan,
305*7f28bceaSValentin Caron 					enum dma_status expected_status,
306*7f28bceaSValentin Caron 					int dmaengine_pause_or_resume(struct dma_chan *),
307*7f28bceaSValentin Caron 					bool stm32_usart_xx_dma_started(struct stm32_port *),
308*7f28bceaSValentin Caron 					void stm32_usart_xx_dma_terminate(struct stm32_port *))
309*7f28bceaSValentin Caron {
31000d1f9c6SValentin Caron 	struct uart_port *port = &stm32_port->port;
311*7f28bceaSValentin Caron 	enum dma_status dma_status;
312*7f28bceaSValentin Caron 	int ret;
31333bb2f6aSErwan Le Ray 
314*7f28bceaSValentin Caron 	if (!stm32_usart_xx_dma_started(stm32_port))
315*7f28bceaSValentin Caron 		return -EPERM;
31633bb2f6aSErwan Le Ray 
317*7f28bceaSValentin Caron 	dma_status = dmaengine_tx_status(chan, chan->cookie, NULL);
318*7f28bceaSValentin Caron 	if (dma_status != expected_status)
319*7f28bceaSValentin Caron 		return -EAGAIN;
320*7f28bceaSValentin Caron 
321*7f28bceaSValentin Caron 	ret = dmaengine_pause_or_resume(chan);
322*7f28bceaSValentin Caron 	if (ret) {
323*7f28bceaSValentin Caron 		dev_err(port->dev, "DMA failed with error code: %d\n", ret);
324*7f28bceaSValentin Caron 		stm32_usart_xx_dma_terminate(stm32_port);
325*7f28bceaSValentin Caron 	}
326*7f28bceaSValentin Caron 	return ret;
32733bb2f6aSErwan Le Ray }
32833bb2f6aSErwan Le Ray 
32933bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */
33033bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
33133bb2f6aSErwan Le Ray {
33233bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
33333bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
33434891872SAlexandre TORGUE 
33534891872SAlexandre TORGUE 	*sr = readl_relaxed(port->membase + ofs->isr);
33633bb2f6aSErwan Le Ray 	/* Get pending characters in RDR or FIFO */
33733bb2f6aSErwan Le Ray 	if (*sr & USART_SR_RXNE) {
33833bb2f6aSErwan Le Ray 		/* Get all pending characters from the RDR or the FIFO when using interrupts */
33900d1f9c6SValentin Caron 		if (!stm32_usart_rx_dma_started(stm32_port))
34033bb2f6aSErwan Le Ray 			return true;
34134891872SAlexandre TORGUE 
34233bb2f6aSErwan Le Ray 		/* Handle only RX data errors when using DMA */
34333bb2f6aSErwan Le Ray 		if (*sr & USART_SR_ERR_MASK)
34433bb2f6aSErwan Le Ray 			return true;
34534891872SAlexandre TORGUE 	}
34634891872SAlexandre TORGUE 
34733bb2f6aSErwan Le Ray 	return false;
34833bb2f6aSErwan Le Ray }
34933bb2f6aSErwan Le Ray 
350fd2b55f8SJiri Slaby static u8 stm32_usart_get_char_pio(struct uart_port *port)
35134891872SAlexandre TORGUE {
35234891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
353d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
35434891872SAlexandre TORGUE 	unsigned long c;
35534891872SAlexandre TORGUE 
3566c5962f3SErwan Le Ray 	c = readl_relaxed(port->membase + ofs->rdr);
35733bb2f6aSErwan Le Ray 	/* Apply RDR data mask */
3586c5962f3SErwan Le Ray 	c &= stm32_port->rdr_mask;
3596c5962f3SErwan Le Ray 
3606c5962f3SErwan Le Ray 	return c;
36134891872SAlexandre TORGUE }
36234891872SAlexandre TORGUE 
3636333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
36448a6092fSMaxime Coquelin {
365ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
366d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
3676333a485SErwan Le Ray 	unsigned int size = 0;
36848a6092fSMaxime Coquelin 	u32 sr;
369fd2b55f8SJiri Slaby 	u8 c, flag;
37048a6092fSMaxime Coquelin 
37133bb2f6aSErwan Le Ray 	while (stm32_usart_pending_rx_pio(port, &sr)) {
37248a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
37348a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
37448a6092fSMaxime Coquelin 
3754f01d833SErwan Le Ray 		/*
3764f01d833SErwan Le Ray 		 * Status bits has to be cleared before reading the RDR:
3774f01d833SErwan Le Ray 		 * In FIFO mode, reading the RDR will pop the next data
3784f01d833SErwan Le Ray 		 * (if any) along with its status bits into the SR.
3794f01d833SErwan Le Ray 		 * Not doing so leads to misalignement between RDR and SR,
3804f01d833SErwan Le Ray 		 * and clear status bits of the next rx data.
3814f01d833SErwan Le Ray 		 *
3824f01d833SErwan Le Ray 		 * Clear errors flags for stm32f7 and stm32h7 compatible
3834f01d833SErwan Le Ray 		 * devices. On stm32f4 compatible devices, the error bit is
3844f01d833SErwan Le Ray 		 * cleared by the sequence [read SR - read DR].
3854f01d833SErwan Le Ray 		 */
3864f01d833SErwan Le Ray 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
3871250ed71SFabrice Gasnier 			writel_relaxed(sr & USART_SR_ERR_MASK,
3881250ed71SFabrice Gasnier 				       port->membase + ofs->icr);
3894f01d833SErwan Le Ray 
39033bb2f6aSErwan Le Ray 		c = stm32_usart_get_char_pio(port);
3914f01d833SErwan Le Ray 		port->icount.rx++;
3926333a485SErwan Le Ray 		size++;
39348a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
3944f01d833SErwan Le Ray 			if (sr & USART_SR_ORE) {
39548a6092fSMaxime Coquelin 				port->icount.overrun++;
39648a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
39748a6092fSMaxime Coquelin 				port->icount.parity++;
39848a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
3994f01d833SErwan Le Ray 				/* Break detection if character is null */
4004f01d833SErwan Le Ray 				if (!c) {
4014f01d833SErwan Le Ray 					port->icount.brk++;
4024f01d833SErwan Le Ray 					if (uart_handle_break(port))
4034f01d833SErwan Le Ray 						continue;
4044f01d833SErwan Le Ray 				} else {
40548a6092fSMaxime Coquelin 					port->icount.frame++;
40648a6092fSMaxime Coquelin 				}
4074f01d833SErwan Le Ray 			}
40848a6092fSMaxime Coquelin 
40948a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
41048a6092fSMaxime Coquelin 
4114f01d833SErwan Le Ray 			if (sr & USART_SR_PE) {
41248a6092fSMaxime Coquelin 				flag = TTY_PARITY;
4134f01d833SErwan Le Ray 			} else if (sr & USART_SR_FE) {
4144f01d833SErwan Le Ray 				if (!c)
4154f01d833SErwan Le Ray 					flag = TTY_BREAK;
4164f01d833SErwan Le Ray 				else
41748a6092fSMaxime Coquelin 					flag = TTY_FRAME;
41848a6092fSMaxime Coquelin 			}
4194f01d833SErwan Le Ray 		}
42048a6092fSMaxime Coquelin 
421cea37afdSJohan Hovold 		if (uart_prepare_sysrq_char(port, c))
42248a6092fSMaxime Coquelin 			continue;
42348a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
42448a6092fSMaxime Coquelin 	}
4256333a485SErwan Le Ray 
4266333a485SErwan Le Ray 	return size;
42733bb2f6aSErwan Le Ray }
42833bb2f6aSErwan Le Ray 
42933bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
43033bb2f6aSErwan Le Ray {
43133bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
43233bb2f6aSErwan Le Ray 	struct tty_port *ttyport = &stm32_port->port.state->port;
43333bb2f6aSErwan Le Ray 	unsigned char *dma_start;
43433bb2f6aSErwan Le Ray 	int dma_count, i;
43533bb2f6aSErwan Le Ray 
43633bb2f6aSErwan Le Ray 	dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
43733bb2f6aSErwan Le Ray 
43833bb2f6aSErwan Le Ray 	/*
43933bb2f6aSErwan Le Ray 	 * Apply rdr_mask on buffer in order to mask parity bit.
44033bb2f6aSErwan Le Ray 	 * This loop is useless in cs8 mode because DMA copies only
44133bb2f6aSErwan Le Ray 	 * 8 bits and already ignores parity bit.
44233bb2f6aSErwan Le Ray 	 */
44333bb2f6aSErwan Le Ray 	if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
44433bb2f6aSErwan Le Ray 		for (i = 0; i < dma_size; i++)
44533bb2f6aSErwan Le Ray 			*(dma_start + i) &= stm32_port->rdr_mask;
44633bb2f6aSErwan Le Ray 
44733bb2f6aSErwan Le Ray 	dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
44833bb2f6aSErwan Le Ray 	port->icount.rx += dma_count;
44933bb2f6aSErwan Le Ray 	if (dma_count != dma_size)
45033bb2f6aSErwan Le Ray 		port->icount.buf_overrun++;
45133bb2f6aSErwan Le Ray 	stm32_port->last_res -= dma_count;
45233bb2f6aSErwan Le Ray 	if (stm32_port->last_res == 0)
45333bb2f6aSErwan Le Ray 		stm32_port->last_res = RX_BUF_L;
45433bb2f6aSErwan Le Ray }
45533bb2f6aSErwan Le Ray 
4566333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
45733bb2f6aSErwan Le Ray {
45833bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
4596333a485SErwan Le Ray 	unsigned int dma_size, size = 0;
46033bb2f6aSErwan Le Ray 
46133bb2f6aSErwan Le Ray 	/* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
46233bb2f6aSErwan Le Ray 	if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
46333bb2f6aSErwan Le Ray 		/* Conditional first part: from last_res to end of DMA buffer */
46433bb2f6aSErwan Le Ray 		dma_size = stm32_port->last_res;
46533bb2f6aSErwan Le Ray 		stm32_usart_push_buffer_dma(port, dma_size);
4666333a485SErwan Le Ray 		size = dma_size;
46733bb2f6aSErwan Le Ray 	}
46833bb2f6aSErwan Le Ray 
46933bb2f6aSErwan Le Ray 	dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
47033bb2f6aSErwan Le Ray 	stm32_usart_push_buffer_dma(port, dma_size);
4716333a485SErwan Le Ray 	size += dma_size;
4726333a485SErwan Le Ray 
4736333a485SErwan Le Ray 	return size;
47433bb2f6aSErwan Le Ray }
47533bb2f6aSErwan Le Ray 
4766333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
47733bb2f6aSErwan Le Ray {
47833bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
47933bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
48033bb2f6aSErwan Le Ray 	enum dma_status rx_dma_status;
48133bb2f6aSErwan Le Ray 	u32 sr;
4826333a485SErwan Le Ray 	unsigned int size = 0;
48333bb2f6aSErwan Le Ray 
48400d1f9c6SValentin Caron 	if (stm32_usart_rx_dma_started(stm32_port) || force_dma_flush) {
48533bb2f6aSErwan Le Ray 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
48633bb2f6aSErwan Le Ray 						    stm32_port->rx_ch->cookie,
48733bb2f6aSErwan Le Ray 						    &stm32_port->rx_dma_state);
48833bb2f6aSErwan Le Ray 		if (rx_dma_status == DMA_IN_PROGRESS) {
48933bb2f6aSErwan Le Ray 			/* Empty DMA buffer */
4906333a485SErwan Le Ray 			size = stm32_usart_receive_chars_dma(port);
49133bb2f6aSErwan Le Ray 			sr = readl_relaxed(port->membase + ofs->isr);
49233bb2f6aSErwan Le Ray 			if (sr & USART_SR_ERR_MASK) {
49333bb2f6aSErwan Le Ray 				/* Disable DMA request line */
49433bb2f6aSErwan Le Ray 				stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
49533bb2f6aSErwan Le Ray 
49633bb2f6aSErwan Le Ray 				/* Switch to PIO mode to handle the errors */
4976333a485SErwan Le Ray 				size += stm32_usart_receive_chars_pio(port);
49833bb2f6aSErwan Le Ray 
49933bb2f6aSErwan Le Ray 				/* Switch back to DMA mode */
50033bb2f6aSErwan Le Ray 				stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
50133bb2f6aSErwan Le Ray 			}
50233bb2f6aSErwan Le Ray 		} else {
50333bb2f6aSErwan Le Ray 			/* Disable RX DMA */
504*7f28bceaSValentin Caron 			stm32_usart_rx_dma_terminate(stm32_port);
50533bb2f6aSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
50633bb2f6aSErwan Le Ray 			/* Fall back to interrupt mode */
50733bb2f6aSErwan Le Ray 			dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
5086333a485SErwan Le Ray 			size = stm32_usart_receive_chars_pio(port);
50933bb2f6aSErwan Le Ray 		}
51033bb2f6aSErwan Le Ray 	} else {
5116333a485SErwan Le Ray 		size = stm32_usart_receive_chars_pio(port);
51233bb2f6aSErwan Le Ray 	}
51348a6092fSMaxime Coquelin 
5146333a485SErwan Le Ray 	return size;
51548a6092fSMaxime Coquelin }
51648a6092fSMaxime Coquelin 
5179a135f16SValentin Caron static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
5189a135f16SValentin Caron {
5199a135f16SValentin Caron 	dmaengine_terminate_async(stm32_port->tx_ch);
5209a135f16SValentin Caron 	stm32_port->tx_dma_busy = false;
5219a135f16SValentin Caron }
5229a135f16SValentin Caron 
5239a135f16SValentin Caron static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
5249a135f16SValentin Caron {
5259a135f16SValentin Caron 	/*
5269a135f16SValentin Caron 	 * We cannot use the function "dmaengine_tx_status" to know the
5279a135f16SValentin Caron 	 * status of DMA. This function does not show if the "dma complete"
5289a135f16SValentin Caron 	 * callback of the DMA transaction has been called. So we prefer
5299a135f16SValentin Caron 	 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
5309a135f16SValentin Caron 	 * same time.
5319a135f16SValentin Caron 	 */
5329a135f16SValentin Caron 	return stm32_port->tx_dma_busy;
5339a135f16SValentin Caron }
5349a135f16SValentin Caron 
535*7f28bceaSValentin Caron static int stm32_usart_tx_dma_pause(struct stm32_port *stm32_port)
536*7f28bceaSValentin Caron {
537*7f28bceaSValentin Caron 	return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
538*7f28bceaSValentin Caron 					    DMA_IN_PROGRESS, dmaengine_pause,
539*7f28bceaSValentin Caron 					    stm32_usart_tx_dma_started,
540*7f28bceaSValentin Caron 					    stm32_usart_tx_dma_terminate);
541*7f28bceaSValentin Caron }
542*7f28bceaSValentin Caron 
543*7f28bceaSValentin Caron static int stm32_usart_tx_dma_resume(struct stm32_port *stm32_port)
544*7f28bceaSValentin Caron {
545*7f28bceaSValentin Caron 	return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
546*7f28bceaSValentin Caron 					    DMA_PAUSED, dmaengine_resume,
547*7f28bceaSValentin Caron 					    stm32_usart_tx_dma_started,
548*7f28bceaSValentin Caron 					    stm32_usart_tx_dma_terminate);
549*7f28bceaSValentin Caron }
550*7f28bceaSValentin Caron 
55156f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg)
55234891872SAlexandre TORGUE {
55334891872SAlexandre TORGUE 	struct uart_port *port = arg;
55434891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
555f16b90c2SErwan Le Ray 	unsigned long flags;
55634891872SAlexandre TORGUE 
5579a135f16SValentin Caron 	stm32_usart_tx_dma_terminate(stm32port);
55834891872SAlexandre TORGUE 
55934891872SAlexandre TORGUE 	/* Let's see if we have pending data to send */
560f16b90c2SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
56156f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
562f16b90c2SErwan Le Ray 	spin_unlock_irqrestore(&port->lock, flags);
56334891872SAlexandre TORGUE }
56434891872SAlexandre TORGUE 
56556f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
566d075719eSErwan Le Ray {
567d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
568d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
569d075719eSErwan Le Ray 
570d075719eSErwan Le Ray 	/*
571d075719eSErwan Le Ray 	 * Enables TX FIFO threashold irq when FIFO is enabled,
572d075719eSErwan Le Ray 	 * or TX empty irq when FIFO is disabled
573d075719eSErwan Le Ray 	 */
5742aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
57556f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
576d075719eSErwan Le Ray 	else
57756f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
578d075719eSErwan Le Ray }
579d075719eSErwan Le Ray 
580d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_enable(struct uart_port *port)
581d7c76716SMarek Vasut {
582d7c76716SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
583d7c76716SMarek Vasut 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
584d7c76716SMarek Vasut 
585d7c76716SMarek Vasut 	stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE);
586d7c76716SMarek Vasut }
587d7c76716SMarek Vasut 
58833bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg)
58933bb2f6aSErwan Le Ray {
59033bb2f6aSErwan Le Ray 	struct uart_port *port = arg;
5916333a485SErwan Le Ray 	struct tty_port *tport = &port->state->port;
5926333a485SErwan Le Ray 	unsigned int size;
5936333a485SErwan Le Ray 	unsigned long flags;
59433bb2f6aSErwan Le Ray 
5956333a485SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
5966333a485SErwan Le Ray 	size = stm32_usart_receive_chars(port, false);
5976333a485SErwan Le Ray 	uart_unlock_and_check_sysrq_irqrestore(port, flags);
5986333a485SErwan Le Ray 	if (size)
5996333a485SErwan Le Ray 		tty_flip_buffer_push(tport);
60033bb2f6aSErwan Le Ray }
60133bb2f6aSErwan Le Ray 
60256f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
603d075719eSErwan Le Ray {
604d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
605d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
606d075719eSErwan Le Ray 
6072aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
60856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
609d075719eSErwan Le Ray 	else
61056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
611d075719eSErwan Le Ray }
612d075719eSErwan Le Ray 
613d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_disable(struct uart_port *port)
614d7c76716SMarek Vasut {
615d7c76716SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
616d7c76716SMarek Vasut 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
617d7c76716SMarek Vasut 
618d7c76716SMarek Vasut 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE);
619d7c76716SMarek Vasut }
620d7c76716SMarek Vasut 
62156f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port)
62234891872SAlexandre TORGUE {
62334891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
624d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
62534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
62634891872SAlexandre TORGUE 
6275d9176edSErwan Le Ray 	while (!uart_circ_empty(xmit)) {
6285d9176edSErwan Le Ray 		/* Check that TDR is empty before filling FIFO */
6295d9176edSErwan Le Ray 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
6305d9176edSErwan Le Ray 			break;
63134891872SAlexandre TORGUE 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
63229d8c07bSIlpo Järvinen 		uart_xmit_advance(port, 1);
63334891872SAlexandre TORGUE 	}
63434891872SAlexandre TORGUE 
6355d9176edSErwan Le Ray 	/* rely on TXE irq (mask or unmask) for sending remaining data */
6365d9176edSErwan Le Ray 	if (uart_circ_empty(xmit))
63756f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
6385d9176edSErwan Le Ray 	else
63956f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_enable(port);
6405d9176edSErwan Le Ray }
6415d9176edSErwan Le Ray 
64256f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port)
64334891872SAlexandre TORGUE {
64434891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
64534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
64634891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
647195437d1SValentin Caron 	unsigned int count;
648db89728aSValentin Caron 	int ret;
64934891872SAlexandre TORGUE 
6509a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32port)) {
651*7f28bceaSValentin Caron 		ret = stm32_usart_tx_dma_resume(stm32port);
652*7f28bceaSValentin Caron 		if (ret < 0 && ret != -EAGAIN)
653*7f28bceaSValentin Caron 			goto fallback_err;
65434891872SAlexandre TORGUE 		return;
6559a135f16SValentin Caron 	}
65634891872SAlexandre TORGUE 
65734891872SAlexandre TORGUE 	count = uart_circ_chars_pending(xmit);
65834891872SAlexandre TORGUE 
65934891872SAlexandre TORGUE 	if (count > TX_BUF_L)
66034891872SAlexandre TORGUE 		count = TX_BUF_L;
66134891872SAlexandre TORGUE 
66234891872SAlexandre TORGUE 	if (xmit->tail < xmit->head) {
66334891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
66434891872SAlexandre TORGUE 	} else {
66534891872SAlexandre TORGUE 		size_t one = UART_XMIT_SIZE - xmit->tail;
66634891872SAlexandre TORGUE 		size_t two;
66734891872SAlexandre TORGUE 
66834891872SAlexandre TORGUE 		if (one > count)
66934891872SAlexandre TORGUE 			one = count;
67034891872SAlexandre TORGUE 		two = count - one;
67134891872SAlexandre TORGUE 
67234891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
67334891872SAlexandre TORGUE 		if (two)
67434891872SAlexandre TORGUE 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
67534891872SAlexandre TORGUE 	}
67634891872SAlexandre TORGUE 
67734891872SAlexandre TORGUE 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
67834891872SAlexandre TORGUE 					   stm32port->tx_dma_buf,
67934891872SAlexandre TORGUE 					   count,
68034891872SAlexandre TORGUE 					   DMA_MEM_TO_DEV,
68134891872SAlexandre TORGUE 					   DMA_PREP_INTERRUPT);
68234891872SAlexandre TORGUE 
683e7997f7fSErwan Le Ray 	if (!desc)
684e7997f7fSErwan Le Ray 		goto fallback_err;
68534891872SAlexandre TORGUE 
6869a135f16SValentin Caron 	/*
6879a135f16SValentin Caron 	 * Set "tx_dma_busy" flag. This flag will be released when
6889a135f16SValentin Caron 	 * dmaengine_terminate_async will be called. This flag helps
6899a135f16SValentin Caron 	 * transmit_chars_dma not to start another DMA transaction
6909a135f16SValentin Caron 	 * if the callback of the previous is not yet called.
6919a135f16SValentin Caron 	 */
6929a135f16SValentin Caron 	stm32port->tx_dma_busy = true;
6939a135f16SValentin Caron 
69456f9a76cSErwan Le Ray 	desc->callback = stm32_usart_tx_dma_complete;
69534891872SAlexandre TORGUE 	desc->callback_param = port;
69634891872SAlexandre TORGUE 
69734891872SAlexandre TORGUE 	/* Push current DMA TX transaction in the pending queue */
698db89728aSValentin Caron 	/* DMA no yet started, safe to free resources */
699*7f28bceaSValentin Caron 	ret = dma_submit_error(dmaengine_submit(desc));
700*7f28bceaSValentin Caron 	if (ret) {
701*7f28bceaSValentin Caron 		dev_err(port->dev, "DMA failed with error code: %d\n", ret);
702*7f28bceaSValentin Caron 		stm32_usart_tx_dma_terminate(stm32port);
703*7f28bceaSValentin Caron 		goto fallback_err;
704*7f28bceaSValentin Caron 	}
70534891872SAlexandre TORGUE 
70634891872SAlexandre TORGUE 	/* Issue pending DMA TX requests */
70734891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->tx_ch);
70834891872SAlexandre TORGUE 
70929d8c07bSIlpo Järvinen 	uart_xmit_advance(port, count);
71029d8c07bSIlpo Järvinen 
711e7997f7fSErwan Le Ray 	return;
712e7997f7fSErwan Le Ray 
713e7997f7fSErwan Le Ray fallback_err:
71456f9a76cSErwan Le Ray 	stm32_usart_transmit_chars_pio(port);
71534891872SAlexandre TORGUE }
71634891872SAlexandre TORGUE 
71756f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port)
71848a6092fSMaxime Coquelin {
719ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
720d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
72148a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
722d3d079bdSValentin Caron 	u32 isr;
723d3d079bdSValentin Caron 	int ret;
72448a6092fSMaxime Coquelin 
725d7c76716SMarek Vasut 	if (!stm32_port->hw_flow_control &&
726c47527cbSMarek Vasut 	    port->rs485.flags & SER_RS485_ENABLED &&
727c47527cbSMarek Vasut 	    (port->x_char ||
728c47527cbSMarek Vasut 	     !(uart_circ_empty(xmit) || uart_tx_stopped(port)))) {
729d7c76716SMarek Vasut 		stm32_usart_tc_interrupt_disable(port);
730d7c76716SMarek Vasut 		stm32_usart_rs485_rts_enable(port);
731d7c76716SMarek Vasut 	}
732d7c76716SMarek Vasut 
73348a6092fSMaxime Coquelin 	if (port->x_char) {
734*7f28bceaSValentin Caron 		/* dma terminate may have been called in case of dma pause failure */
735*7f28bceaSValentin Caron 		stm32_usart_tx_dma_pause(stm32_port);
736*7f28bceaSValentin Caron 
737d3d079bdSValentin Caron 		/* Check that TDR is empty before filling FIFO */
738d3d079bdSValentin Caron 		ret =
739d3d079bdSValentin Caron 		readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
740d3d079bdSValentin Caron 						  isr,
741d3d079bdSValentin Caron 						  (isr & USART_SR_TXE),
742d3d079bdSValentin Caron 						  10, 1000);
743d3d079bdSValentin Caron 		if (ret)
744d3d079bdSValentin Caron 			dev_warn(port->dev, "1 character may be erased\n");
745d3d079bdSValentin Caron 
746ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
74748a6092fSMaxime Coquelin 		port->x_char = 0;
74848a6092fSMaxime Coquelin 		port->icount.tx++;
749db89728aSValentin Caron 
750*7f28bceaSValentin Caron 		/* dma terminate may have been called in case of dma resume failure */
751*7f28bceaSValentin Caron 		stm32_usart_tx_dma_resume(stm32_port);
75248a6092fSMaxime Coquelin 		return;
75348a6092fSMaxime Coquelin 	}
75448a6092fSMaxime Coquelin 
755b83b957cSErwan Le Ray 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
75656f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
75748a6092fSMaxime Coquelin 		return;
75848a6092fSMaxime Coquelin 	}
75948a6092fSMaxime Coquelin 
76064c32eabSErwan Le Ray 	if (ofs->icr == UNDEF_REG)
76156f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
76264c32eabSErwan Le Ray 	else
7631250ed71SFabrice Gasnier 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
76464c32eabSErwan Le Ray 
76534891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
76656f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_dma(port);
76734891872SAlexandre TORGUE 	else
76856f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_pio(port);
76948a6092fSMaxime Coquelin 
77048a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
77148a6092fSMaxime Coquelin 		uart_write_wakeup(port);
77248a6092fSMaxime Coquelin 
773d7c76716SMarek Vasut 	if (uart_circ_empty(xmit)) {
77456f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
775d7c76716SMarek Vasut 		if (!stm32_port->hw_flow_control &&
776d7c76716SMarek Vasut 		    port->rs485.flags & SER_RS485_ENABLED) {
777d7c76716SMarek Vasut 			stm32_usart_tc_interrupt_enable(port);
778d7c76716SMarek Vasut 		}
779d7c76716SMarek Vasut 	}
78048a6092fSMaxime Coquelin }
78148a6092fSMaxime Coquelin 
78256f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
78348a6092fSMaxime Coquelin {
78448a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
78512761869SErwan Le Ray 	struct tty_port *tport = &port->state->port;
786ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
787d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
78848a6092fSMaxime Coquelin 	u32 sr;
7896333a485SErwan Le Ray 	unsigned int size;
79048a6092fSMaxime Coquelin 
791ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
79248a6092fSMaxime Coquelin 
793d7c76716SMarek Vasut 	if (!stm32_port->hw_flow_control &&
794d7c76716SMarek Vasut 	    port->rs485.flags & SER_RS485_ENABLED &&
795d7c76716SMarek Vasut 	    (sr & USART_SR_TC)) {
796d7c76716SMarek Vasut 		stm32_usart_tc_interrupt_disable(port);
797d7c76716SMarek Vasut 		stm32_usart_rs485_rts_disable(port);
798d7c76716SMarek Vasut 	}
799d7c76716SMarek Vasut 
8004cc0ed62SErwan Le Ray 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
8014cc0ed62SErwan Le Ray 		writel_relaxed(USART_ICR_RTOCF,
8024cc0ed62SErwan Le Ray 			       port->membase + ofs->icr);
8034cc0ed62SErwan Le Ray 
80412761869SErwan Le Ray 	if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
80512761869SErwan Le Ray 		/* Clear wake up flag and disable wake up interrupt */
806270e5a74SFabrice Gasnier 		writel_relaxed(USART_ICR_WUCF,
807270e5a74SFabrice Gasnier 			       port->membase + ofs->icr);
80812761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
80912761869SErwan Le Ray 		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
81012761869SErwan Le Ray 			pm_wakeup_event(tport->tty->dev, 0);
81112761869SErwan Le Ray 	}
812270e5a74SFabrice Gasnier 
81333bb2f6aSErwan Le Ray 	/*
81433bb2f6aSErwan Le Ray 	 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
81533bb2f6aSErwan Le Ray 	 * line has been masked by HW and rx data are stacking in FIFO.
81633bb2f6aSErwan Le Ray 	 */
817d1ec8a2eSErwan Le Ray 	if (!stm32_port->throttled) {
81800d1f9c6SValentin Caron 		if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_started(stm32_port)) ||
81900d1f9c6SValentin Caron 		    ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_started(stm32_port))) {
8206333a485SErwan Le Ray 			spin_lock(&port->lock);
8216333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, false);
8226333a485SErwan Le Ray 			uart_unlock_and_check_sysrq(port);
8236333a485SErwan Le Ray 			if (size)
8246333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
825d1ec8a2eSErwan Le Ray 		}
826d1ec8a2eSErwan Le Ray 	}
82748a6092fSMaxime Coquelin 
828ad767681SErwan Le Ray 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
829ad767681SErwan Le Ray 		spin_lock(&port->lock);
83056f9a76cSErwan Le Ray 		stm32_usart_transmit_chars(port);
83101d32d71SAlexandre TORGUE 		spin_unlock(&port->lock);
832ad767681SErwan Le Ray 	}
83301d32d71SAlexandre TORGUE 
834cc58d0a3SErwan Le Ray 	/* Receiver timeout irq for DMA RX */
83500d1f9c6SValentin Caron 	if (stm32_usart_rx_dma_started(stm32_port) && !stm32_port->throttled) {
8363f6c02faSMarek Vasut 		spin_lock(&port->lock);
8376333a485SErwan Le Ray 		size = stm32_usart_receive_chars(port, false);
8383f6c02faSMarek Vasut 		uart_unlock_and_check_sysrq(port);
8396333a485SErwan Le Ray 		if (size)
8406333a485SErwan Le Ray 			tty_flip_buffer_push(tport);
8416333a485SErwan Le Ray 	}
84234891872SAlexandre TORGUE 
84348a6092fSMaxime Coquelin 	return IRQ_HANDLED;
84448a6092fSMaxime Coquelin }
84548a6092fSMaxime Coquelin 
84656f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
84748a6092fSMaxime Coquelin {
848ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
849d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
850ada8618fSAlexandre TORGUE 
85148a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
85256f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
85348a6092fSMaxime Coquelin 	else
85456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
8556cf61b9bSManivannan Sadhasivam 
8566cf61b9bSManivannan Sadhasivam 	mctrl_gpio_set(stm32_port->gpios, mctrl);
85748a6092fSMaxime Coquelin }
85848a6092fSMaxime Coquelin 
85956f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
86048a6092fSMaxime Coquelin {
8616cf61b9bSManivannan Sadhasivam 	struct stm32_port *stm32_port = to_stm32_port(port);
8626cf61b9bSManivannan Sadhasivam 	unsigned int ret;
8636cf61b9bSManivannan Sadhasivam 
86448a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
8656cf61b9bSManivannan Sadhasivam 	ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
8666cf61b9bSManivannan Sadhasivam 
8676cf61b9bSManivannan Sadhasivam 	return mctrl_gpio_get(stm32_port->gpios, &ret);
8686cf61b9bSManivannan Sadhasivam }
8696cf61b9bSManivannan Sadhasivam 
87056f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port)
8716cf61b9bSManivannan Sadhasivam {
8726cf61b9bSManivannan Sadhasivam 	mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
8736cf61b9bSManivannan Sadhasivam }
8746cf61b9bSManivannan Sadhasivam 
87556f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port)
8766cf61b9bSManivannan Sadhasivam {
8776cf61b9bSManivannan Sadhasivam 	mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
87848a6092fSMaxime Coquelin }
87948a6092fSMaxime Coquelin 
88048a6092fSMaxime Coquelin /* Transmit stop */
88156f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port)
88248a6092fSMaxime Coquelin {
883ad0c2748SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
884ad0c2748SMarek Vasut 
88556f9a76cSErwan Le Ray 	stm32_usart_tx_interrupt_disable(port);
886*7f28bceaSValentin Caron 
887*7f28bceaSValentin Caron 	/* dma terminate may have been called in case of dma pause failure */
888*7f28bceaSValentin Caron 	stm32_usart_tx_dma_pause(stm32_port);
889ad0c2748SMarek Vasut 
8903bcea529SMarek Vasut 	stm32_usart_rs485_rts_disable(port);
89148a6092fSMaxime Coquelin }
89248a6092fSMaxime Coquelin 
89348a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
89456f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port)
89548a6092fSMaxime Coquelin {
89648a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
89748a6092fSMaxime Coquelin 
898d7c76716SMarek Vasut 	if (uart_circ_empty(xmit) && !port->x_char) {
899d7c76716SMarek Vasut 		stm32_usart_rs485_rts_disable(port);
90048a6092fSMaxime Coquelin 		return;
901d7c76716SMarek Vasut 	}
90248a6092fSMaxime Coquelin 
9033bcea529SMarek Vasut 	stm32_usart_rs485_rts_enable(port);
904ad0c2748SMarek Vasut 
90556f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
90648a6092fSMaxime Coquelin }
90748a6092fSMaxime Coquelin 
9083d82be8bSErwan Le Ray /* Flush the transmit buffer. */
9093d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port)
9103d82be8bSErwan Le Ray {
9113d82be8bSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
9123d82be8bSErwan Le Ray 
913db89728aSValentin Caron 	if (stm32_port->tx_ch)
9149a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
9153d82be8bSErwan Le Ray }
9163d82be8bSErwan Le Ray 
91748a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
91856f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port)
91948a6092fSMaxime Coquelin {
920ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
921d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
92248a6092fSMaxime Coquelin 	unsigned long flags;
92348a6092fSMaxime Coquelin 
92448a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
925d1ec8a2eSErwan Le Ray 
926d1ec8a2eSErwan Le Ray 	/*
927d1ec8a2eSErwan Le Ray 	 * Disable DMA request line if enabled, so the RX data gets queued into the FIFO.
928d1ec8a2eSErwan Le Ray 	 * Hardware flow control is triggered when RX FIFO is full.
929d1ec8a2eSErwan Le Ray 	 */
93000d1f9c6SValentin Caron 	if (stm32_usart_rx_dma_started(stm32_port))
931d1ec8a2eSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
932d1ec8a2eSErwan Le Ray 
93356f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
934d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
93556f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
936d0a6a7bcSErwan Le Ray 
937d1ec8a2eSErwan Le Ray 	stm32_port->throttled = true;
93848a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
93948a6092fSMaxime Coquelin }
94048a6092fSMaxime Coquelin 
94148a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
94256f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port)
94348a6092fSMaxime Coquelin {
944ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
945d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
94648a6092fSMaxime Coquelin 	unsigned long flags;
94748a6092fSMaxime Coquelin 
94848a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
94956f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
950d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
95156f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
952d0a6a7bcSErwan Le Ray 
953d1ec8a2eSErwan Le Ray 	/*
954d1ec8a2eSErwan Le Ray 	 * Switch back to DMA mode (re-enable DMA request line).
955d1ec8a2eSErwan Le Ray 	 * Hardware flow control is stopped when FIFO is not full any more.
956d1ec8a2eSErwan Le Ray 	 */
957d1ec8a2eSErwan Le Ray 	if (stm32_port->rx_ch)
958d1ec8a2eSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
959d1ec8a2eSErwan Le Ray 
960d1ec8a2eSErwan Le Ray 	stm32_port->throttled = false;
96148a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
96248a6092fSMaxime Coquelin }
96348a6092fSMaxime Coquelin 
96448a6092fSMaxime Coquelin /* Receive stop */
96556f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port)
96648a6092fSMaxime Coquelin {
967ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
968d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
969ada8618fSAlexandre TORGUE 
970e0abc903SErwan Le Ray 	/* Disable DMA request line. */
971e0abc903SErwan Le Ray 	if (stm32_port->rx_ch)
972e0abc903SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
973e0abc903SErwan Le Ray 
97456f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
975d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
97656f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
97748a6092fSMaxime Coquelin }
97848a6092fSMaxime Coquelin 
97948a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
98056f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
98148a6092fSMaxime Coquelin {
98248a6092fSMaxime Coquelin }
98348a6092fSMaxime Coquelin 
9846eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port)
9856eeb348cSErwan Le Ray {
9866eeb348cSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
9876eeb348cSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
9886eeb348cSErwan Le Ray 	struct dma_async_tx_descriptor *desc;
989*7f28bceaSValentin Caron 	enum dma_status rx_dma_status;
9906eeb348cSErwan Le Ray 	int ret;
9916eeb348cSErwan Le Ray 
992*7f28bceaSValentin Caron 	if (stm32_port->rx_dma_busy) {
993*7f28bceaSValentin Caron 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
994*7f28bceaSValentin Caron 						    stm32_port->rx_ch->cookie,
995*7f28bceaSValentin Caron 						    NULL);
996*7f28bceaSValentin Caron 		if (rx_dma_status == DMA_IN_PROGRESS)
997*7f28bceaSValentin Caron 			return 0;
998*7f28bceaSValentin Caron 
999*7f28bceaSValentin Caron 		dev_err(port->dev, "DMA failed : status error.\n");
1000*7f28bceaSValentin Caron 		stm32_usart_rx_dma_terminate(stm32_port);
1001*7f28bceaSValentin Caron 	}
1002*7f28bceaSValentin Caron 
1003*7f28bceaSValentin Caron 	stm32_port->rx_dma_busy = true;
1004*7f28bceaSValentin Caron 
10056eeb348cSErwan Le Ray 	stm32_port->last_res = RX_BUF_L;
10066eeb348cSErwan Le Ray 	/* Prepare a DMA cyclic transaction */
10076eeb348cSErwan Le Ray 	desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
10086eeb348cSErwan Le Ray 					 stm32_port->rx_dma_buf,
10096eeb348cSErwan Le Ray 					 RX_BUF_L, RX_BUF_P,
10106eeb348cSErwan Le Ray 					 DMA_DEV_TO_MEM,
10116eeb348cSErwan Le Ray 					 DMA_PREP_INTERRUPT);
10126eeb348cSErwan Le Ray 	if (!desc) {
10136eeb348cSErwan Le Ray 		dev_err(port->dev, "rx dma prep cyclic failed\n");
1014*7f28bceaSValentin Caron 		stm32_port->rx_dma_busy = false;
10156eeb348cSErwan Le Ray 		return -ENODEV;
10166eeb348cSErwan Le Ray 	}
10176eeb348cSErwan Le Ray 
10186eeb348cSErwan Le Ray 	desc->callback = stm32_usart_rx_dma_complete;
10196eeb348cSErwan Le Ray 	desc->callback_param = port;
10206eeb348cSErwan Le Ray 
10216eeb348cSErwan Le Ray 	/* Push current DMA transaction in the pending queue */
10226eeb348cSErwan Le Ray 	ret = dma_submit_error(dmaengine_submit(desc));
10236eeb348cSErwan Le Ray 	if (ret) {
10246eeb348cSErwan Le Ray 		dmaengine_terminate_sync(stm32_port->rx_ch);
1025*7f28bceaSValentin Caron 		stm32_port->rx_dma_busy = false;
10266eeb348cSErwan Le Ray 		return ret;
10276eeb348cSErwan Le Ray 	}
10286eeb348cSErwan Le Ray 
10296eeb348cSErwan Le Ray 	/* Issue pending DMA requests */
10306eeb348cSErwan Le Ray 	dma_async_issue_pending(stm32_port->rx_ch);
10316eeb348cSErwan Le Ray 
10326eeb348cSErwan Le Ray 	/*
10336eeb348cSErwan Le Ray 	 * DMA request line not re-enabled at resume when port is throttled.
10346eeb348cSErwan Le Ray 	 * It will be re-enabled by unthrottle ops.
10356eeb348cSErwan Le Ray 	 */
10366eeb348cSErwan Le Ray 	if (!stm32_port->throttled)
10376eeb348cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
10386eeb348cSErwan Le Ray 
10396eeb348cSErwan Le Ray 	return 0;
10406eeb348cSErwan Le Ray }
10416eeb348cSErwan Le Ray 
104256f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port)
104348a6092fSMaxime Coquelin {
1044ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1045d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1046f4518a8aSErwan Le Ray 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
104748a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
104848a6092fSMaxime Coquelin 	u32 val;
104948a6092fSMaxime Coquelin 	int ret;
105048a6092fSMaxime Coquelin 
10513f6c02faSMarek Vasut 	ret = request_irq(port->irq, stm32_usart_interrupt,
10523f6c02faSMarek Vasut 			  IRQF_NO_SUSPEND, name, port);
105348a6092fSMaxime Coquelin 	if (ret)
105448a6092fSMaxime Coquelin 		return ret;
105548a6092fSMaxime Coquelin 
10563cd66593SMartin Devera 	if (stm32_port->swap) {
10573cd66593SMartin Devera 		val = readl_relaxed(port->membase + ofs->cr2);
10583cd66593SMartin Devera 		val |= USART_CR2_SWAP;
10593cd66593SMartin Devera 		writel_relaxed(val, port->membase + ofs->cr2);
10603cd66593SMartin Devera 	}
10613cd66593SMartin Devera 
106284872dc4SErwan Le Ray 	/* RX FIFO Flush */
106384872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
1064315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
106548a6092fSMaxime Coquelin 
1066e0abc903SErwan Le Ray 	if (stm32_port->rx_ch) {
10676eeb348cSErwan Le Ray 		ret = stm32_usart_start_rx_dma_cyclic(port);
1068e0abc903SErwan Le Ray 		if (ret) {
10696eeb348cSErwan Le Ray 			free_irq(port->irq, port);
10706eeb348cSErwan Le Ray 			return ret;
1071e0abc903SErwan Le Ray 		}
1072e0abc903SErwan Le Ray 	}
1073d1ec8a2eSErwan Le Ray 
107425a8e761SErwan Le Ray 	/* RX enabling */
1075f4518a8aSErwan Le Ray 	val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
107656f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, val);
107784872dc4SErwan Le Ray 
107848a6092fSMaxime Coquelin 	return 0;
107948a6092fSMaxime Coquelin }
108048a6092fSMaxime Coquelin 
108156f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port)
108248a6092fSMaxime Coquelin {
1083ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1084d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1085d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
108664c32eabSErwan Le Ray 	u32 val, isr;
108764c32eabSErwan Le Ray 	int ret;
108848a6092fSMaxime Coquelin 
10899a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32_port))
10909a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
109156a23f93SValentin Caron 
1092db89728aSValentin Caron 	if (stm32_port->tx_ch)
1093db89728aSValentin Caron 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1094db89728aSValentin Caron 
10956cf61b9bSManivannan Sadhasivam 	/* Disable modem control interrupts */
109656f9a76cSErwan Le Ray 	stm32_usart_disable_ms(port);
10976cf61b9bSManivannan Sadhasivam 
10984cc0ed62SErwan Le Ray 	val = USART_CR1_TXEIE | USART_CR1_TE;
10994cc0ed62SErwan Le Ray 	val |= stm32_port->cr1_irq | USART_CR1_RE;
110087f1f809SAlexandre TORGUE 	val |= BIT(cfg->uart_enable_bit);
1101351a762aSGerald Baeza 	if (stm32_port->fifoen)
1102351a762aSGerald Baeza 		val |= USART_CR1_FIFOEN;
110364c32eabSErwan Le Ray 
110464c32eabSErwan Le Ray 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
110564c32eabSErwan Le Ray 					 isr, (isr & USART_SR_TC),
110664c32eabSErwan Le Ray 					 10, 100000);
110764c32eabSErwan Le Ray 
1108c31c3ea0SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set */
110964c32eabSErwan Le Ray 	if (ret)
1110c31c3ea0SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
111164c32eabSErwan Le Ray 
1112e0abc903SErwan Le Ray 	/* Disable RX DMA. */
1113e0abc903SErwan Le Ray 	if (stm32_port->rx_ch)
1114*7f28bceaSValentin Caron 		stm32_usart_rx_dma_terminate(stm32_port);
1115e0abc903SErwan Le Ray 
11169f77d192SErwan Le Ray 	/* flush RX & TX FIFO */
11179f77d192SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
11189f77d192SErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
11199f77d192SErwan Le Ray 			       port->membase + ofs->rqr);
11209f77d192SErwan Le Ray 
112156f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, val);
112248a6092fSMaxime Coquelin 
112348a6092fSMaxime Coquelin 	free_irq(port->irq, port);
112448a6092fSMaxime Coquelin }
112548a6092fSMaxime Coquelin 
112656f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port,
112756f9a76cSErwan Le Ray 				    struct ktermios *termios,
1128bec5b814SIlpo Järvinen 				    const struct ktermios *old)
112948a6092fSMaxime Coquelin {
113048a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
1131d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1132d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
11331bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
1134c8a9d043SErwan Le Ray 	unsigned int baud, bits;
113548a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
113648a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
1137f264c6f6SErwan Le Ray 	u32 cr1, cr2, cr3, isr;
113848a6092fSMaxime Coquelin 	unsigned long flags;
1139f264c6f6SErwan Le Ray 	int ret;
114048a6092fSMaxime Coquelin 
114148a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
114248a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
114348a6092fSMaxime Coquelin 
114448a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
114548a6092fSMaxime Coquelin 
114648a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
114748a6092fSMaxime Coquelin 
1148f264c6f6SErwan Le Ray 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
1149f264c6f6SErwan Le Ray 						isr,
1150f264c6f6SErwan Le Ray 						(isr & USART_SR_TC),
1151f264c6f6SErwan Le Ray 						10, 100000);
1152f264c6f6SErwan Le Ray 
1153f264c6f6SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set. */
1154f264c6f6SErwan Le Ray 	if (ret)
1155f264c6f6SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
1156f264c6f6SErwan Le Ray 
115748a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
1158ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
115948a6092fSMaxime Coquelin 
116084872dc4SErwan Le Ray 	/* flush RX & TX FIFO */
116184872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
1162315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1163315e2d8aSErwan Le Ray 			       port->membase + ofs->rqr);
11641bcda09dSBich HEMON 
116584872dc4SErwan Le Ray 	cr1 = USART_CR1_TE | USART_CR1_RE;
1166351a762aSGerald Baeza 	if (stm32_port->fifoen)
1167351a762aSGerald Baeza 		cr1 |= USART_CR1_FIFOEN;
11683cd66593SMartin Devera 	cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
116925a8e761SErwan Le Ray 
117025a8e761SErwan Le Ray 	/* Tx and RX FIFO configuration */
1171d075719eSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
117225a8e761SErwan Le Ray 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
117325a8e761SErwan Le Ray 	if (stm32_port->fifoen) {
11742aa1bbb2SFabrice Gasnier 		if (stm32_port->txftcfg >= 0)
11752aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
11762aa1bbb2SFabrice Gasnier 		if (stm32_port->rxftcfg >= 0)
11772aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
117825a8e761SErwan Le Ray 	}
117948a6092fSMaxime Coquelin 
118048a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
118148a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
118248a6092fSMaxime Coquelin 
11833ec2ff37SJiri Slaby 	bits = tty_get_char_size(cflag);
11846c5962f3SErwan Le Ray 	stm32_port->rdr_mask = (BIT(bits) - 1);
1185c8a9d043SErwan Le Ray 
118648a6092fSMaxime Coquelin 	if (cflag & PARENB) {
1187c8a9d043SErwan Le Ray 		bits++;
118848a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
1189c8a9d043SErwan Le Ray 	}
1190c8a9d043SErwan Le Ray 
1191c8a9d043SErwan Le Ray 	/*
1192c8a9d043SErwan Le Ray 	 * Word length configuration:
1193c8a9d043SErwan Le Ray 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1194c8a9d043SErwan Le Ray 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1195c8a9d043SErwan Le Ray 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1196c8a9d043SErwan Le Ray 	 * M0 and M1 already cleared by cr1 initialization.
1197c8a9d043SErwan Le Ray 	 */
11981deeda8dSIlpo Järvinen 	if (bits == 9) {
1199ada8618fSAlexandre TORGUE 		cr1 |= USART_CR1_M0;
12001deeda8dSIlpo Järvinen 	} else if ((bits == 7) && cfg->has_7bits_data) {
1201c8a9d043SErwan Le Ray 		cr1 |= USART_CR1_M1;
12021deeda8dSIlpo Järvinen 	} else if (bits != 8) {
1203c8a9d043SErwan Le Ray 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1204c8a9d043SErwan Le Ray 			, bits);
12051deeda8dSIlpo Järvinen 		cflag &= ~CSIZE;
12061deeda8dSIlpo Järvinen 		cflag |= CS8;
12071deeda8dSIlpo Järvinen 		termios->c_cflag = cflag;
12081deeda8dSIlpo Järvinen 		bits = 8;
12091deeda8dSIlpo Järvinen 		if (cflag & PARENB) {
12101deeda8dSIlpo Järvinen 			bits++;
12111deeda8dSIlpo Järvinen 			cr1 |= USART_CR1_M0;
12121deeda8dSIlpo Järvinen 		}
12131deeda8dSIlpo Järvinen 	}
121448a6092fSMaxime Coquelin 
12154cc0ed62SErwan Le Ray 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
12162aa1bbb2SFabrice Gasnier 				       (stm32_port->fifoen &&
12172aa1bbb2SFabrice Gasnier 					stm32_port->rxftcfg >= 0))) {
12184cc0ed62SErwan Le Ray 		if (cflag & CSTOPB)
12194cc0ed62SErwan Le Ray 			bits = bits + 3; /* 1 start bit + 2 stop bits */
12204cc0ed62SErwan Le Ray 		else
12214cc0ed62SErwan Le Ray 			bits = bits + 2; /* 1 start bit + 1 stop bit */
12224cc0ed62SErwan Le Ray 
12234cc0ed62SErwan Le Ray 		/* RX timeout irq to occur after last stop bit + bits */
12244cc0ed62SErwan Le Ray 		stm32_port->cr1_irq = USART_CR1_RTOIE;
12254cc0ed62SErwan Le Ray 		writel_relaxed(bits, port->membase + ofs->rtor);
12264cc0ed62SErwan Le Ray 		cr2 |= USART_CR2_RTOEN;
122733bb2f6aSErwan Le Ray 		/*
122833bb2f6aSErwan Le Ray 		 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
122933bb2f6aSErwan Le Ray 		 * wake up over usart, from low power until the DMA gets re-enabled by resume.
123033bb2f6aSErwan Le Ray 		 */
1231d0a6a7bcSErwan Le Ray 		stm32_port->cr3_irq =  USART_CR3_RXFTIE;
12324cc0ed62SErwan Le Ray 	}
12334cc0ed62SErwan Le Ray 
1234d0a6a7bcSErwan Le Ray 	cr1 |= stm32_port->cr1_irq;
1235d0a6a7bcSErwan Le Ray 	cr3 |= stm32_port->cr3_irq;
1236d0a6a7bcSErwan Le Ray 
123748a6092fSMaxime Coquelin 	if (cflag & PARODD)
123848a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
123948a6092fSMaxime Coquelin 
124048a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
124148a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
124248a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
124335abe98fSBich HEMON 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
124448a6092fSMaxime Coquelin 	}
124548a6092fSMaxime Coquelin 
124648a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
124748a6092fSMaxime Coquelin 
124848a6092fSMaxime Coquelin 	/*
124948a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
125048a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
125148a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
125248a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
125348a6092fSMaxime Coquelin 	 */
125448a6092fSMaxime Coquelin 	if (usartdiv < 16) {
125548a6092fSMaxime Coquelin 		oversampling = 8;
12561bcda09dSBich HEMON 		cr1 |= USART_CR1_OVER8;
125756f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
125848a6092fSMaxime Coquelin 	} else {
125948a6092fSMaxime Coquelin 		oversampling = 16;
12601bcda09dSBich HEMON 		cr1 &= ~USART_CR1_OVER8;
126156f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
126248a6092fSMaxime Coquelin 	}
126348a6092fSMaxime Coquelin 
126448a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
126548a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
1266ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
126748a6092fSMaxime Coquelin 
126848a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
126948a6092fSMaxime Coquelin 
127048a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
127148a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
127248a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
127348a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
12744f01d833SErwan Le Ray 		port->read_status_mask |= USART_SR_FE;
127548a6092fSMaxime Coquelin 
127648a6092fSMaxime Coquelin 	/* Characters to ignore */
127748a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
127848a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
127948a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
128048a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
12814f01d833SErwan Le Ray 		port->ignore_status_mask |= USART_SR_FE;
128248a6092fSMaxime Coquelin 		/*
128348a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
128448a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
128548a6092fSMaxime Coquelin 		 */
128648a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
128748a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
128848a6092fSMaxime Coquelin 	}
128948a6092fSMaxime Coquelin 
129048a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
129148a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
129248a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
129348a6092fSMaxime Coquelin 
129433bb2f6aSErwan Le Ray 	if (stm32_port->rx_ch) {
129533bb2f6aSErwan Le Ray 		/*
129633bb2f6aSErwan Le Ray 		 * Setup DMA to collect only valid data and enable error irqs.
129733bb2f6aSErwan Le Ray 		 * This also enables break reception when using DMA.
129833bb2f6aSErwan Le Ray 		 */
129933bb2f6aSErwan Le Ray 		cr1 |= USART_CR1_PEIE;
130033bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_EIE;
130134891872SAlexandre TORGUE 		cr3 |= USART_CR3_DMAR;
130233bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_DDRE;
130333bb2f6aSErwan Le Ray 	}
130434891872SAlexandre TORGUE 
130500bc5e8fSValentin Caron 	if (stm32_port->tx_ch)
130600bc5e8fSValentin Caron 		cr3 |= USART_CR3_DMAT;
130700bc5e8fSValentin Caron 
13081bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
130956f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
13101bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
131156f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
131256f9a76cSErwan Le Ray 					     baud);
13131bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
13141bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
13151bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
13161bcda09dSBich HEMON 		} else {
13171bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
13181bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
13191bcda09dSBich HEMON 		}
13201bcda09dSBich HEMON 
13211bcda09dSBich HEMON 	} else {
13221bcda09dSBich HEMON 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
13231bcda09dSBich HEMON 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
13241bcda09dSBich HEMON 	}
13251bcda09dSBich HEMON 
132612761869SErwan Le Ray 	/* Configure wake up from low power on start bit detection */
13273d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
132812761869SErwan Le Ray 		cr3 &= ~USART_CR3_WUS_MASK;
132912761869SErwan Le Ray 		cr3 |= USART_CR3_WUS_START_BIT;
133012761869SErwan Le Ray 	}
133112761869SErwan Le Ray 
1332ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
1333ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
1334ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
133548a6092fSMaxime Coquelin 
133656f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
133748a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
1338436c9793SErwan Le Ray 
1339436c9793SErwan Le Ray 	/* Handle modem control interrupts */
1340436c9793SErwan Le Ray 	if (UART_ENABLE_MS(port, termios->c_cflag))
1341436c9793SErwan Le Ray 		stm32_usart_enable_ms(port);
1342436c9793SErwan Le Ray 	else
1343436c9793SErwan Le Ray 		stm32_usart_disable_ms(port);
134448a6092fSMaxime Coquelin }
134548a6092fSMaxime Coquelin 
134656f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port)
134748a6092fSMaxime Coquelin {
134848a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
134948a6092fSMaxime Coquelin }
135048a6092fSMaxime Coquelin 
135156f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port)
135248a6092fSMaxime Coquelin {
135348a6092fSMaxime Coquelin }
135448a6092fSMaxime Coquelin 
135556f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port)
135648a6092fSMaxime Coquelin {
135748a6092fSMaxime Coquelin 	return 0;
135848a6092fSMaxime Coquelin }
135948a6092fSMaxime Coquelin 
136056f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags)
136148a6092fSMaxime Coquelin {
136248a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
136348a6092fSMaxime Coquelin 		port->type = PORT_STM32;
136448a6092fSMaxime Coquelin }
136548a6092fSMaxime Coquelin 
136648a6092fSMaxime Coquelin static int
136756f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
136848a6092fSMaxime Coquelin {
136948a6092fSMaxime Coquelin 	/* No user changeable parameters */
137048a6092fSMaxime Coquelin 	return -EINVAL;
137148a6092fSMaxime Coquelin }
137248a6092fSMaxime Coquelin 
137356f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state,
137448a6092fSMaxime Coquelin 			   unsigned int oldstate)
137548a6092fSMaxime Coquelin {
137648a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
137748a6092fSMaxime Coquelin 			struct stm32_port, port);
1378d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1379d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32port->info->cfg;
138018ee37e1SJohan Hovold 	unsigned long flags;
138148a6092fSMaxime Coquelin 
138248a6092fSMaxime Coquelin 	switch (state) {
138348a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
1384fb6dcef6SErwan Le Ray 		pm_runtime_get_sync(port->dev);
138548a6092fSMaxime Coquelin 		break;
138648a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
138748a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
138856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
138948a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
1390fb6dcef6SErwan Le Ray 		pm_runtime_put_sync(port->dev);
139148a6092fSMaxime Coquelin 		break;
139248a6092fSMaxime Coquelin 	}
139348a6092fSMaxime Coquelin }
139448a6092fSMaxime Coquelin 
13951f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL)
13961f507b3aSValentin Caron 
13971f507b3aSValentin Caron  /* Callbacks for characters polling in debug context (i.e. KGDB). */
13981f507b3aSValentin Caron static int stm32_usart_poll_init(struct uart_port *port)
13991f507b3aSValentin Caron {
14001f507b3aSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
14011f507b3aSValentin Caron 
14021f507b3aSValentin Caron 	return clk_prepare_enable(stm32_port->clk);
14031f507b3aSValentin Caron }
14041f507b3aSValentin Caron 
14051f507b3aSValentin Caron static int stm32_usart_poll_get_char(struct uart_port *port)
14061f507b3aSValentin Caron {
14071f507b3aSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
14081f507b3aSValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
14091f507b3aSValentin Caron 
14101f507b3aSValentin Caron 	if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE))
14111f507b3aSValentin Caron 		return NO_POLL_CHAR;
14121f507b3aSValentin Caron 
14131f507b3aSValentin Caron 	return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask;
14141f507b3aSValentin Caron }
14151f507b3aSValentin Caron 
14161f507b3aSValentin Caron static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch)
14171f507b3aSValentin Caron {
14181f507b3aSValentin Caron 	stm32_usart_console_putchar(port, ch);
14191f507b3aSValentin Caron }
14201f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */
14211f507b3aSValentin Caron 
142248a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
142356f9a76cSErwan Le Ray 	.tx_empty	= stm32_usart_tx_empty,
142456f9a76cSErwan Le Ray 	.set_mctrl	= stm32_usart_set_mctrl,
142556f9a76cSErwan Le Ray 	.get_mctrl	= stm32_usart_get_mctrl,
142656f9a76cSErwan Le Ray 	.stop_tx	= stm32_usart_stop_tx,
142756f9a76cSErwan Le Ray 	.start_tx	= stm32_usart_start_tx,
142856f9a76cSErwan Le Ray 	.throttle	= stm32_usart_throttle,
142956f9a76cSErwan Le Ray 	.unthrottle	= stm32_usart_unthrottle,
143056f9a76cSErwan Le Ray 	.stop_rx	= stm32_usart_stop_rx,
143156f9a76cSErwan Le Ray 	.enable_ms	= stm32_usart_enable_ms,
143256f9a76cSErwan Le Ray 	.break_ctl	= stm32_usart_break_ctl,
143356f9a76cSErwan Le Ray 	.startup	= stm32_usart_startup,
143456f9a76cSErwan Le Ray 	.shutdown	= stm32_usart_shutdown,
14353d82be8bSErwan Le Ray 	.flush_buffer	= stm32_usart_flush_buffer,
143656f9a76cSErwan Le Ray 	.set_termios	= stm32_usart_set_termios,
143756f9a76cSErwan Le Ray 	.pm		= stm32_usart_pm,
143856f9a76cSErwan Le Ray 	.type		= stm32_usart_type,
143956f9a76cSErwan Le Ray 	.release_port	= stm32_usart_release_port,
144056f9a76cSErwan Le Ray 	.request_port	= stm32_usart_request_port,
144156f9a76cSErwan Le Ray 	.config_port	= stm32_usart_config_port,
144256f9a76cSErwan Le Ray 	.verify_port	= stm32_usart_verify_port,
14431f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL)
14441f507b3aSValentin Caron 	.poll_init      = stm32_usart_poll_init,
14451f507b3aSValentin Caron 	.poll_get_char	= stm32_usart_poll_get_char,
14461f507b3aSValentin Caron 	.poll_put_char	= stm32_usart_poll_put_char,
14471f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */
144848a6092fSMaxime Coquelin };
144948a6092fSMaxime Coquelin 
14502aa1bbb2SFabrice Gasnier /*
14512aa1bbb2SFabrice Gasnier  * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
14522aa1bbb2SFabrice Gasnier  * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
14532aa1bbb2SFabrice Gasnier  * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
14542aa1bbb2SFabrice Gasnier  * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
14552aa1bbb2SFabrice Gasnier  */
14562aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
14572aa1bbb2SFabrice Gasnier 
14582aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
14592aa1bbb2SFabrice Gasnier 				  int *ftcfg)
14602aa1bbb2SFabrice Gasnier {
14612aa1bbb2SFabrice Gasnier 	u32 bytes, i;
14622aa1bbb2SFabrice Gasnier 
14632aa1bbb2SFabrice Gasnier 	/* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
14642aa1bbb2SFabrice Gasnier 	if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
14652aa1bbb2SFabrice Gasnier 		bytes = 8;
14662aa1bbb2SFabrice Gasnier 
14672aa1bbb2SFabrice Gasnier 	for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
14682aa1bbb2SFabrice Gasnier 		if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
14692aa1bbb2SFabrice Gasnier 			break;
14702aa1bbb2SFabrice Gasnier 	if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
14712aa1bbb2SFabrice Gasnier 		i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
14722aa1bbb2SFabrice Gasnier 
14732aa1bbb2SFabrice Gasnier 	dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
14742aa1bbb2SFabrice Gasnier 		stm32h7_usart_fifo_thresh_cfg[i]);
14752aa1bbb2SFabrice Gasnier 
14762aa1bbb2SFabrice Gasnier 	/* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
14772aa1bbb2SFabrice Gasnier 	if (i)
14782aa1bbb2SFabrice Gasnier 		*ftcfg = i - 1;
14792aa1bbb2SFabrice Gasnier 	else
14802aa1bbb2SFabrice Gasnier 		*ftcfg = -EINVAL;
14812aa1bbb2SFabrice Gasnier }
14822aa1bbb2SFabrice Gasnier 
148397f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port)
148497f3a085SErwan Le Ray {
148597f3a085SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
148697f3a085SErwan Le Ray }
148797f3a085SErwan Le Ray 
1488aeae8f22SIlpo Järvinen static const struct serial_rs485 stm32_rs485_supported = {
1489aeae8f22SIlpo Järvinen 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
1490aeae8f22SIlpo Järvinen 		 SER_RS485_RX_DURING_TX,
1491aeae8f22SIlpo Järvinen 	.delay_rts_before_send = 1,
1492aeae8f22SIlpo Järvinen 	.delay_rts_after_send = 1,
1493aeae8f22SIlpo Järvinen };
1494aeae8f22SIlpo Järvinen 
149556f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port,
149648a6092fSMaxime Coquelin 				 struct platform_device *pdev)
149748a6092fSMaxime Coquelin {
149848a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
149948a6092fSMaxime Coquelin 	struct resource *res;
1500e0f2a902SErwan Le Ray 	int ret, irq;
150148a6092fSMaxime Coquelin 
1502e0f2a902SErwan Le Ray 	irq = platform_get_irq(pdev, 0);
1503217b04c6STang Bin 	if (irq < 0)
1504217b04c6STang Bin 		return irq;
150592fc0023SErwan Le Ray 
150648a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
150748a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
150848a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
150948a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
1510d075719eSErwan Le Ray 	port->fifosize	= stm32port->info->cfg.fifosize;
15119feedaa7SDmitry Safonov 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1512e0f2a902SErwan Le Ray 	port->irq = irq;
151356f9a76cSErwan Le Ray 	port->rs485_config = stm32_usart_config_rs485;
15140139da50SIlpo Järvinen 	port->rs485_supported = stm32_rs485_supported;
15157d8f6861SBich HEMON 
151656f9a76cSErwan Le Ray 	ret = stm32_usart_init_rs485(port, pdev);
1517c150c0f3SLukas Wunner 	if (ret)
1518c150c0f3SLukas Wunner 		return ret;
15197d8f6861SBich HEMON 
15203d530017SAlexandre Torgue 	stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
15213d530017SAlexandre Torgue 		of_property_read_bool(pdev->dev.of_node, "wakeup-source");
15222c58e560SErwan Le Ray 
15233cd66593SMartin Devera 	stm32port->swap = stm32port->info->cfg.has_swap &&
15243cd66593SMartin Devera 		of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
15253cd66593SMartin Devera 
1526351a762aSGerald Baeza 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
15272aa1bbb2SFabrice Gasnier 	if (stm32port->fifoen) {
15282aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "rx-threshold",
15292aa1bbb2SFabrice Gasnier 				      &stm32port->rxftcfg);
15302aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "tx-threshold",
15312aa1bbb2SFabrice Gasnier 				      &stm32port->txftcfg);
15322aa1bbb2SFabrice Gasnier 	}
153348a6092fSMaxime Coquelin 
15343d881e32STang Bin 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
153548a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
153648a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
153748a6092fSMaxime Coquelin 	port->mapbase = res->start;
153848a6092fSMaxime Coquelin 
153948a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
154048a6092fSMaxime Coquelin 
154148a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
154248a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
154348a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
154448a6092fSMaxime Coquelin 
154548a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
154648a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
154748a6092fSMaxime Coquelin 	if (ret)
154848a6092fSMaxime Coquelin 		return ret;
154948a6092fSMaxime Coquelin 
155048a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1551ada80043SFabrice Gasnier 	if (!stm32port->port.uartclk) {
155248a6092fSMaxime Coquelin 		ret = -EINVAL;
15536cf61b9bSManivannan Sadhasivam 		goto err_clk;
1554ada80043SFabrice Gasnier 	}
155548a6092fSMaxime Coquelin 
15566cf61b9bSManivannan Sadhasivam 	stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
15576cf61b9bSManivannan Sadhasivam 	if (IS_ERR(stm32port->gpios)) {
15586cf61b9bSManivannan Sadhasivam 		ret = PTR_ERR(stm32port->gpios);
15596cf61b9bSManivannan Sadhasivam 		goto err_clk;
15606cf61b9bSManivannan Sadhasivam 	}
15616cf61b9bSManivannan Sadhasivam 
15629359369aSErwan Le Ray 	/*
15639359369aSErwan Le Ray 	 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
15649359369aSErwan Le Ray 	 * properties should not be specified.
15659359369aSErwan Le Ray 	 */
15666cf61b9bSManivannan Sadhasivam 	if (stm32port->hw_flow_control) {
15676cf61b9bSManivannan Sadhasivam 		if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
15686cf61b9bSManivannan Sadhasivam 		    mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
15696cf61b9bSManivannan Sadhasivam 			dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
15706cf61b9bSManivannan Sadhasivam 			ret = -EINVAL;
15716cf61b9bSManivannan Sadhasivam 			goto err_clk;
15726cf61b9bSManivannan Sadhasivam 		}
15736cf61b9bSManivannan Sadhasivam 	}
15746cf61b9bSManivannan Sadhasivam 
15756cf61b9bSManivannan Sadhasivam 	return ret;
15766cf61b9bSManivannan Sadhasivam 
15776cf61b9bSManivannan Sadhasivam err_clk:
15786cf61b9bSManivannan Sadhasivam 	clk_disable_unprepare(stm32port->clk);
15796cf61b9bSManivannan Sadhasivam 
158048a6092fSMaxime Coquelin 	return ret;
158148a6092fSMaxime Coquelin }
158248a6092fSMaxime Coquelin 
158356f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
158448a6092fSMaxime Coquelin {
158548a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
158648a6092fSMaxime Coquelin 	int id;
158748a6092fSMaxime Coquelin 
158848a6092fSMaxime Coquelin 	if (!np)
158948a6092fSMaxime Coquelin 		return NULL;
159048a6092fSMaxime Coquelin 
159148a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
1592e5707915SGerald Baeza 	if (id < 0) {
1593e5707915SGerald Baeza 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1594e5707915SGerald Baeza 		return NULL;
1595e5707915SGerald Baeza 	}
159648a6092fSMaxime Coquelin 
159748a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
159848a6092fSMaxime Coquelin 		return NULL;
159948a6092fSMaxime Coquelin 
16006fd9fffbSErwan Le Ray 	stm32_ports[id].hw_flow_control =
16016fd9fffbSErwan Le Ray 		of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
16026fd9fffbSErwan Le Ray 		of_property_read_bool (np, "uart-has-rtscts");
160348a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
16044cc0ed62SErwan Le Ray 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1605d0a6a7bcSErwan Le Ray 	stm32_ports[id].cr3_irq = 0;
1606e5707915SGerald Baeza 	stm32_ports[id].last_res = RX_BUF_L;
160748a6092fSMaxime Coquelin 	return &stm32_ports[id];
160848a6092fSMaxime Coquelin }
160948a6092fSMaxime Coquelin 
161048a6092fSMaxime Coquelin #ifdef CONFIG_OF
161148a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
1612ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
1613ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1614270e5a74SFabrice Gasnier 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
161548a6092fSMaxime Coquelin 	{},
161648a6092fSMaxime Coquelin };
161748a6092fSMaxime Coquelin 
161848a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
161948a6092fSMaxime Coquelin #endif
162048a6092fSMaxime Coquelin 
1621a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1622a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1623a7770a4bSErwan Le Ray {
1624a7770a4bSErwan Le Ray 	if (stm32port->rx_buf)
1625a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1626a7770a4bSErwan Le Ray 				  stm32port->rx_dma_buf);
1627a7770a4bSErwan Le Ray }
1628a7770a4bSErwan Le Ray 
162956f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
163034891872SAlexandre TORGUE 				       struct platform_device *pdev)
163134891872SAlexandre TORGUE {
1632d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
163334891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
163434891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
163534891872SAlexandre TORGUE 	struct dma_slave_config config;
163634891872SAlexandre TORGUE 	int ret;
163734891872SAlexandre TORGUE 
163859bd4eedSTang Bin 	stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
163934891872SAlexandre TORGUE 					       &stm32port->rx_dma_buf,
164034891872SAlexandre TORGUE 					       GFP_KERNEL);
1641a7770a4bSErwan Le Ray 	if (!stm32port->rx_buf)
1642a7770a4bSErwan Le Ray 		return -ENOMEM;
164334891872SAlexandre TORGUE 
164434891872SAlexandre TORGUE 	/* Configure DMA channel */
164534891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
16468e5481d9SArnd Bergmann 	config.src_addr = port->mapbase + ofs->rdr;
164734891872SAlexandre TORGUE 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
164834891872SAlexandre TORGUE 
164934891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
165034891872SAlexandre TORGUE 	if (ret < 0) {
165134891872SAlexandre TORGUE 		dev_err(dev, "rx dma channel config failed\n");
1652a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
1653a7770a4bSErwan Le Ray 		return ret;
165434891872SAlexandre TORGUE 	}
165534891872SAlexandre TORGUE 
165634891872SAlexandre TORGUE 	return 0;
1657a7770a4bSErwan Le Ray }
165834891872SAlexandre TORGUE 
1659a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1660a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1661a7770a4bSErwan Le Ray {
1662a7770a4bSErwan Le Ray 	if (stm32port->tx_buf)
1663a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1664a7770a4bSErwan Le Ray 				  stm32port->tx_dma_buf);
166534891872SAlexandre TORGUE }
166634891872SAlexandre TORGUE 
166756f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
166834891872SAlexandre TORGUE 				       struct platform_device *pdev)
166934891872SAlexandre TORGUE {
1670d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
167134891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
167234891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
167334891872SAlexandre TORGUE 	struct dma_slave_config config;
167434891872SAlexandre TORGUE 	int ret;
167534891872SAlexandre TORGUE 
167659bd4eedSTang Bin 	stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
167734891872SAlexandre TORGUE 					       &stm32port->tx_dma_buf,
167834891872SAlexandre TORGUE 					       GFP_KERNEL);
1679a7770a4bSErwan Le Ray 	if (!stm32port->tx_buf)
1680a7770a4bSErwan Le Ray 		return -ENOMEM;
168134891872SAlexandre TORGUE 
168234891872SAlexandre TORGUE 	/* Configure DMA channel */
168334891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
16848e5481d9SArnd Bergmann 	config.dst_addr = port->mapbase + ofs->tdr;
168534891872SAlexandre TORGUE 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
168634891872SAlexandre TORGUE 
168734891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
168834891872SAlexandre TORGUE 	if (ret < 0) {
168934891872SAlexandre TORGUE 		dev_err(dev, "tx dma channel config failed\n");
1690a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1691a7770a4bSErwan Le Ray 		return ret;
169234891872SAlexandre TORGUE 	}
169334891872SAlexandre TORGUE 
169434891872SAlexandre TORGUE 	return 0;
169534891872SAlexandre TORGUE }
169634891872SAlexandre TORGUE 
169756f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev)
169848a6092fSMaxime Coquelin {
169948a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
1700ada8618fSAlexandre TORGUE 	int ret;
170148a6092fSMaxime Coquelin 
170256f9a76cSErwan Le Ray 	stm32port = stm32_usart_of_get_port(pdev);
170348a6092fSMaxime Coquelin 	if (!stm32port)
170448a6092fSMaxime Coquelin 		return -ENODEV;
170548a6092fSMaxime Coquelin 
1706d825f0beSStephen Boyd 	stm32port->info = of_device_get_match_data(&pdev->dev);
1707d825f0beSStephen Boyd 	if (!stm32port->info)
1708ada8618fSAlexandre TORGUE 		return -EINVAL;
1709ada8618fSAlexandre TORGUE 
1710a7770a4bSErwan Le Ray 	stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
17110d114e9fSValentin Caron 	if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER)
17120d114e9fSValentin Caron 		return -EPROBE_DEFER;
17130d114e9fSValentin Caron 
1714a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1715a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->rx_ch))
1716a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
171734891872SAlexandre TORGUE 
1718a7770a4bSErwan Le Ray 	stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1719a7770a4bSErwan Le Ray 	if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1720a7770a4bSErwan Le Ray 		ret = -EPROBE_DEFER;
1721a7770a4bSErwan Le Ray 		goto err_dma_rx;
1722a7770a4bSErwan Le Ray 	}
1723a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1724a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->tx_ch))
1725a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1726a7770a4bSErwan Le Ray 
17270d114e9fSValentin Caron 	ret = stm32_usart_init_port(stm32port, pdev);
17280d114e9fSValentin Caron 	if (ret)
17290d114e9fSValentin Caron 		goto err_dma_tx;
17300d114e9fSValentin Caron 
17310d114e9fSValentin Caron 	if (stm32port->wakeup_src) {
17320d114e9fSValentin Caron 		device_set_wakeup_capable(&pdev->dev, true);
17330d114e9fSValentin Caron 		ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
17340d114e9fSValentin Caron 		if (ret)
17350d114e9fSValentin Caron 			goto err_deinit_port;
17360d114e9fSValentin Caron 	}
17370d114e9fSValentin Caron 
1738a7770a4bSErwan Le Ray 	if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1739a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1740a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->rx_ch);
1741a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
1742a7770a4bSErwan Le Ray 	}
1743a7770a4bSErwan Le Ray 
1744a7770a4bSErwan Le Ray 	if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1745a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1746a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->tx_ch);
1747a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1748a7770a4bSErwan Le Ray 	}
1749a7770a4bSErwan Le Ray 
1750a7770a4bSErwan Le Ray 	if (!stm32port->rx_ch)
1751a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1752a7770a4bSErwan Le Ray 	if (!stm32port->tx_ch)
1753a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
175434891872SAlexandre TORGUE 
175548a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
175648a6092fSMaxime Coquelin 
1757fb6dcef6SErwan Le Ray 	pm_runtime_get_noresume(&pdev->dev);
1758fb6dcef6SErwan Le Ray 	pm_runtime_set_active(&pdev->dev);
1759fb6dcef6SErwan Le Ray 	pm_runtime_enable(&pdev->dev);
176087fd0741SErwan Le Ray 
176187fd0741SErwan Le Ray 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
176287fd0741SErwan Le Ray 	if (ret)
176387fd0741SErwan Le Ray 		goto err_port;
176487fd0741SErwan Le Ray 
1765fb6dcef6SErwan Le Ray 	pm_runtime_put_sync(&pdev->dev);
1766fb6dcef6SErwan Le Ray 
176748a6092fSMaxime Coquelin 	return 0;
1768ada80043SFabrice Gasnier 
176987fd0741SErwan Le Ray err_port:
177087fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
177187fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
177287fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
177387fd0741SErwan Le Ray 
17740d114e9fSValentin Caron 	if (stm32port->tx_ch)
1775a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1776a7770a4bSErwan Le Ray 	if (stm32port->rx_ch)
1777a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
177887fd0741SErwan Le Ray 
17793d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
17805297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
17815297f274SErwan Le Ray 
1782a7770a4bSErwan Le Ray err_deinit_port:
17833d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
17843d530017SAlexandre Torgue 		device_set_wakeup_capable(&pdev->dev, false);
1785270e5a74SFabrice Gasnier 
178697f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32port);
1787ada80043SFabrice Gasnier 
17880d114e9fSValentin Caron err_dma_tx:
17890d114e9fSValentin Caron 	if (stm32port->tx_ch)
17900d114e9fSValentin Caron 		dma_release_channel(stm32port->tx_ch);
17910d114e9fSValentin Caron 
17920d114e9fSValentin Caron err_dma_rx:
17930d114e9fSValentin Caron 	if (stm32port->rx_ch)
17940d114e9fSValentin Caron 		dma_release_channel(stm32port->rx_ch);
17950d114e9fSValentin Caron 
1796ada80043SFabrice Gasnier 	return ret;
179748a6092fSMaxime Coquelin }
179848a6092fSMaxime Coquelin 
179956f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev)
180048a6092fSMaxime Coquelin {
180148a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
1802511c7b1bSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1803d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
180433bb2f6aSErwan Le Ray 	u32 cr3;
1805fb6dcef6SErwan Le Ray 
1806fb6dcef6SErwan Le Ray 	pm_runtime_get_sync(&pdev->dev);
18076bd6cd29SUwe Kleine-König 	uart_remove_one_port(&stm32_usart_driver, port);
180887fd0741SErwan Le Ray 
180987fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
181087fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
181187fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
181234891872SAlexandre TORGUE 
181333bb2f6aSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
181433bb2f6aSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
181533bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_EIE;
181633bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_DMAR;
181733bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_DDRE;
181833bb2f6aSErwan Le Ray 	writel_relaxed(cr3, port->membase + ofs->cr3);
181934891872SAlexandre TORGUE 
182087fd0741SErwan Le Ray 	if (stm32_port->tx_ch) {
1821a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32_port, pdev);
182234891872SAlexandre TORGUE 		dma_release_channel(stm32_port->tx_ch);
182387fd0741SErwan Le Ray 	}
182434891872SAlexandre TORGUE 
1825a7770a4bSErwan Le Ray 	if (stm32_port->rx_ch) {
1826a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1827a7770a4bSErwan Le Ray 		dma_release_channel(stm32_port->rx_ch);
1828a7770a4bSErwan Le Ray 	}
1829a7770a4bSErwan Le Ray 
1830a7770a4bSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1831511c7b1bSAlexandre TORGUE 
18323d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
18335297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
1834270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
18355297f274SErwan Le Ray 	}
1836270e5a74SFabrice Gasnier 
183797f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32_port);
183848a6092fSMaxime Coquelin 
183987fd0741SErwan Le Ray 	return 0;
184048a6092fSMaxime Coquelin }
184148a6092fSMaxime Coquelin 
18421f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
184348a6092fSMaxime Coquelin {
1844ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1845d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
184628fb1a92SValentin Caron 	u32 isr;
184728fb1a92SValentin Caron 	int ret;
1848ada8618fSAlexandre TORGUE 
184928fb1a92SValentin Caron 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr,
185028fb1a92SValentin Caron 						(isr & USART_SR_TXE), 100,
185128fb1a92SValentin Caron 						STM32_USART_TIMEOUT_USEC);
185228fb1a92SValentin Caron 	if (ret != 0) {
185328fb1a92SValentin Caron 		dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret);
185428fb1a92SValentin Caron 		return;
185528fb1a92SValentin Caron 	}
1856ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
185748a6092fSMaxime Coquelin }
185848a6092fSMaxime Coquelin 
18591f507b3aSValentin Caron #ifdef CONFIG_SERIAL_STM32_CONSOLE
186056f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s,
186192fc0023SErwan Le Ray 				      unsigned int cnt)
186248a6092fSMaxime Coquelin {
186348a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
1864ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1865d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1866d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
186748a6092fSMaxime Coquelin 	unsigned long flags;
186848a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
186948a6092fSMaxime Coquelin 	int locked = 1;
187048a6092fSMaxime Coquelin 
1871cea37afdSJohan Hovold 	if (oops_in_progress)
1872cea37afdSJohan Hovold 		locked = spin_trylock_irqsave(&port->lock, flags);
187348a6092fSMaxime Coquelin 	else
1874cea37afdSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
187548a6092fSMaxime Coquelin 
187687f1f809SAlexandre TORGUE 	/* Save and disable interrupts, enable the transmitter */
1877ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
187848a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
187987f1f809SAlexandre TORGUE 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1880ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
188148a6092fSMaxime Coquelin 
188256f9a76cSErwan Le Ray 	uart_console_write(port, s, cnt, stm32_usart_console_putchar);
188348a6092fSMaxime Coquelin 
188448a6092fSMaxime Coquelin 	/* Restore interrupt state */
1885ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
188648a6092fSMaxime Coquelin 
188748a6092fSMaxime Coquelin 	if (locked)
1888cea37afdSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
188948a6092fSMaxime Coquelin }
189048a6092fSMaxime Coquelin 
189156f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options)
189248a6092fSMaxime Coquelin {
189348a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
189448a6092fSMaxime Coquelin 	int baud = 9600;
189548a6092fSMaxime Coquelin 	int bits = 8;
189648a6092fSMaxime Coquelin 	int parity = 'n';
189748a6092fSMaxime Coquelin 	int flow = 'n';
189848a6092fSMaxime Coquelin 
189948a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
190048a6092fSMaxime Coquelin 		return -ENODEV;
190148a6092fSMaxime Coquelin 
190248a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
190348a6092fSMaxime Coquelin 
190448a6092fSMaxime Coquelin 	/*
190548a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
190648a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
190748a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
190848a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
190948a6092fSMaxime Coquelin 	 */
191092fc0023SErwan Le Ray 	if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
191148a6092fSMaxime Coquelin 		return -ENXIO;
191248a6092fSMaxime Coquelin 
191348a6092fSMaxime Coquelin 	if (options)
191448a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
191548a6092fSMaxime Coquelin 
191648a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
191748a6092fSMaxime Coquelin }
191848a6092fSMaxime Coquelin 
191948a6092fSMaxime Coquelin static struct console stm32_console = {
192048a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
192148a6092fSMaxime Coquelin 	.device		= uart_console_device,
192256f9a76cSErwan Le Ray 	.write		= stm32_usart_console_write,
192356f9a76cSErwan Le Ray 	.setup		= stm32_usart_console_setup,
192448a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
192548a6092fSMaxime Coquelin 	.index		= -1,
192648a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
192748a6092fSMaxime Coquelin };
192848a6092fSMaxime Coquelin 
192948a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
193048a6092fSMaxime Coquelin 
193148a6092fSMaxime Coquelin #else
193248a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
193348a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
193448a6092fSMaxime Coquelin 
19358043b16fSValentin Caron #ifdef CONFIG_SERIAL_EARLYCON
19368043b16fSValentin Caron static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
19378043b16fSValentin Caron {
19388043b16fSValentin Caron 	struct stm32_usart_info *info = port->private_data;
19398043b16fSValentin Caron 
19408043b16fSValentin Caron 	while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE))
19418043b16fSValentin Caron 		cpu_relax();
19428043b16fSValentin Caron 
19438043b16fSValentin Caron 	writel_relaxed(ch, port->membase + info->ofs.tdr);
19448043b16fSValentin Caron }
19458043b16fSValentin Caron 
19468043b16fSValentin Caron static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count)
19478043b16fSValentin Caron {
19488043b16fSValentin Caron 	struct earlycon_device *device = console->data;
19498043b16fSValentin Caron 	struct uart_port *port = &device->port;
19508043b16fSValentin Caron 
19518043b16fSValentin Caron 	uart_console_write(port, s, count, early_stm32_usart_console_putchar);
19528043b16fSValentin Caron }
19538043b16fSValentin Caron 
19548043b16fSValentin Caron static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
19558043b16fSValentin Caron {
19568043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
19578043b16fSValentin Caron 		return -ENODEV;
19588043b16fSValentin Caron 	device->port.private_data = &stm32h7_info;
19598043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
19608043b16fSValentin Caron 	return 0;
19618043b16fSValentin Caron }
19628043b16fSValentin Caron 
19638043b16fSValentin Caron static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
19648043b16fSValentin Caron {
19658043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
19668043b16fSValentin Caron 		return -ENODEV;
19678043b16fSValentin Caron 	device->port.private_data = &stm32f7_info;
19688043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
19698043b16fSValentin Caron 	return 0;
19708043b16fSValentin Caron }
19718043b16fSValentin Caron 
19728043b16fSValentin Caron static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
19738043b16fSValentin Caron {
19748043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
19758043b16fSValentin Caron 		return -ENODEV;
19768043b16fSValentin Caron 	device->port.private_data = &stm32f4_info;
19778043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
19788043b16fSValentin Caron 	return 0;
19798043b16fSValentin Caron }
19808043b16fSValentin Caron 
19818043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup);
19828043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup);
19838043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup);
19848043b16fSValentin Caron #endif /* CONFIG_SERIAL_EARLYCON */
19858043b16fSValentin Caron 
198648a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
198748a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
198848a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
198948a6092fSMaxime Coquelin 	.major		= 0,
199048a6092fSMaxime Coquelin 	.minor		= 0,
199148a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
199248a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
199348a6092fSMaxime Coquelin };
199448a6092fSMaxime Coquelin 
19956eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1996fe94347dSErwan Le Ray 						       bool enable)
1997270e5a74SFabrice Gasnier {
1998270e5a74SFabrice Gasnier 	struct stm32_port *stm32_port = to_stm32_port(port);
1999d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
20006eeb348cSErwan Le Ray 	struct tty_port *tport = &port->state->port;
20016eeb348cSErwan Le Ray 	int ret;
20026333a485SErwan Le Ray 	unsigned int size;
20036333a485SErwan Le Ray 	unsigned long flags;
2004270e5a74SFabrice Gasnier 
20056eeb348cSErwan Le Ray 	if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
20066eeb348cSErwan Le Ray 		return 0;
2007270e5a74SFabrice Gasnier 
200812761869SErwan Le Ray 	/*
200912761869SErwan Le Ray 	 * Enable low-power wake-up and wake-up irq if argument is set to
201012761869SErwan Le Ray 	 * "enable", disable low-power wake-up and wake-up irq otherwise
201112761869SErwan Le Ray 	 */
2012270e5a74SFabrice Gasnier 	if (enable) {
201356f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
201412761869SErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
20157547d9abSErwan Le Ray 		mctrl_gpio_enable_irq_wake(stm32_port->gpios);
20166eeb348cSErwan Le Ray 
20176eeb348cSErwan Le Ray 		/*
20186eeb348cSErwan Le Ray 		 * When DMA is used for reception, it must be disabled before
20196eeb348cSErwan Le Ray 		 * entering low-power mode and re-enabled when exiting from
20206eeb348cSErwan Le Ray 		 * low-power mode.
20216eeb348cSErwan Le Ray 		 */
20226eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
20236333a485SErwan Le Ray 			spin_lock_irqsave(&port->lock, flags);
20246333a485SErwan Le Ray 			/* Avoid race with RX IRQ when DMAR is cleared */
20256eeb348cSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
20266333a485SErwan Le Ray 			/* Poll data from DMA RX buffer if any */
20276333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, true);
2028*7f28bceaSValentin Caron 			stm32_usart_rx_dma_terminate(stm32_port);
20296333a485SErwan Le Ray 			uart_unlock_and_check_sysrq_irqrestore(port, flags);
20306333a485SErwan Le Ray 			if (size)
20316333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
20326eeb348cSErwan Le Ray 		}
20336eeb348cSErwan Le Ray 
20346eeb348cSErwan Le Ray 		/* Poll data from RX FIFO if any */
20356eeb348cSErwan Le Ray 		stm32_usart_receive_chars(port, false);
2036270e5a74SFabrice Gasnier 	} else {
20376eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
20386eeb348cSErwan Le Ray 			ret = stm32_usart_start_rx_dma_cyclic(port);
20396eeb348cSErwan Le Ray 			if (ret)
20406eeb348cSErwan Le Ray 				return ret;
20416eeb348cSErwan Le Ray 		}
20427547d9abSErwan Le Ray 		mctrl_gpio_disable_irq_wake(stm32_port->gpios);
204356f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
204412761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
2045270e5a74SFabrice Gasnier 	}
20466eeb348cSErwan Le Ray 
20476eeb348cSErwan Le Ray 	return 0;
2048270e5a74SFabrice Gasnier }
2049270e5a74SFabrice Gasnier 
205056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
2051270e5a74SFabrice Gasnier {
2052270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
20536eeb348cSErwan Le Ray 	int ret;
2054270e5a74SFabrice Gasnier 
2055270e5a74SFabrice Gasnier 	uart_suspend_port(&stm32_usart_driver, port);
2056270e5a74SFabrice Gasnier 
20576eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
20586eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, true);
20596eeb348cSErwan Le Ray 		if (ret)
20606eeb348cSErwan Le Ray 			return ret;
20616eeb348cSErwan Le Ray 	}
2062270e5a74SFabrice Gasnier 
206355484fccSErwan Le Ray 	/*
206455484fccSErwan Le Ray 	 * When "no_console_suspend" is enabled, keep the pinctrl default state
206555484fccSErwan Le Ray 	 * and rely on bootloader stage to restore this state upon resume.
206655484fccSErwan Le Ray 	 * Otherwise, apply the idle or sleep states depending on wakeup
206755484fccSErwan Le Ray 	 * capabilities.
206855484fccSErwan Le Ray 	 */
206955484fccSErwan Le Ray 	if (console_suspend_enabled || !uart_console(port)) {
20701631eeeaSErwan Le Ray 		if (device_may_wakeup(dev) || device_wakeup_path(dev))
207155484fccSErwan Le Ray 			pinctrl_pm_select_idle_state(dev);
207255484fccSErwan Le Ray 		else
207394616d9aSErwan Le Ray 			pinctrl_pm_select_sleep_state(dev);
207455484fccSErwan Le Ray 	}
207594616d9aSErwan Le Ray 
2076270e5a74SFabrice Gasnier 	return 0;
2077270e5a74SFabrice Gasnier }
2078270e5a74SFabrice Gasnier 
207956f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
2080270e5a74SFabrice Gasnier {
2081270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
20826eeb348cSErwan Le Ray 	int ret;
2083270e5a74SFabrice Gasnier 
208494616d9aSErwan Le Ray 	pinctrl_pm_select_default_state(dev);
208594616d9aSErwan Le Ray 
20866eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
20876eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, false);
20886eeb348cSErwan Le Ray 		if (ret)
20896eeb348cSErwan Le Ray 			return ret;
20906eeb348cSErwan Le Ray 	}
2091270e5a74SFabrice Gasnier 
2092270e5a74SFabrice Gasnier 	return uart_resume_port(&stm32_usart_driver, port);
2093270e5a74SFabrice Gasnier }
2094270e5a74SFabrice Gasnier 
209556f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
2096fb6dcef6SErwan Le Ray {
2097fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
2098fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
2099fb6dcef6SErwan Le Ray 			struct stm32_port, port);
2100fb6dcef6SErwan Le Ray 
2101fb6dcef6SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
2102fb6dcef6SErwan Le Ray 
2103fb6dcef6SErwan Le Ray 	return 0;
2104fb6dcef6SErwan Le Ray }
2105fb6dcef6SErwan Le Ray 
210656f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
2107fb6dcef6SErwan Le Ray {
2108fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
2109fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
2110fb6dcef6SErwan Le Ray 			struct stm32_port, port);
2111fb6dcef6SErwan Le Ray 
2112fb6dcef6SErwan Le Ray 	return clk_prepare_enable(stm32port->clk);
2113fb6dcef6SErwan Le Ray }
2114fb6dcef6SErwan Le Ray 
2115270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = {
211656f9a76cSErwan Le Ray 	SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
211756f9a76cSErwan Le Ray 			   stm32_usart_runtime_resume, NULL)
211856f9a76cSErwan Le Ray 	SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
211956f9a76cSErwan Le Ray 				stm32_usart_serial_resume)
2120270e5a74SFabrice Gasnier };
2121270e5a74SFabrice Gasnier 
212248a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
212356f9a76cSErwan Le Ray 	.probe		= stm32_usart_serial_probe,
212456f9a76cSErwan Le Ray 	.remove		= stm32_usart_serial_remove,
212548a6092fSMaxime Coquelin 	.driver	= {
212648a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
2127270e5a74SFabrice Gasnier 		.pm	= &stm32_serial_pm_ops,
212848a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
212948a6092fSMaxime Coquelin 	},
213048a6092fSMaxime Coquelin };
213148a6092fSMaxime Coquelin 
213256f9a76cSErwan Le Ray static int __init stm32_usart_init(void)
213348a6092fSMaxime Coquelin {
213448a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
213548a6092fSMaxime Coquelin 	int ret;
213648a6092fSMaxime Coquelin 
213748a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
213848a6092fSMaxime Coquelin 
213948a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
214048a6092fSMaxime Coquelin 	if (ret)
214148a6092fSMaxime Coquelin 		return ret;
214248a6092fSMaxime Coquelin 
214348a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
214448a6092fSMaxime Coquelin 	if (ret)
214548a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
214648a6092fSMaxime Coquelin 
214748a6092fSMaxime Coquelin 	return ret;
214848a6092fSMaxime Coquelin }
214948a6092fSMaxime Coquelin 
215056f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void)
215148a6092fSMaxime Coquelin {
215248a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
215348a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
215448a6092fSMaxime Coquelin }
215548a6092fSMaxime Coquelin 
215656f9a76cSErwan Le Ray module_init(stm32_usart_init);
215756f9a76cSErwan Le Ray module_exit(stm32_usart_exit);
215848a6092fSMaxime Coquelin 
215948a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
216048a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
216148a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
2162