xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision c845428b7a9157523103100806bc8130d64769c8)
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 
to_stm32_port(struct uart_port * port)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 
stm32_usart_set_bits(struct uart_port * port,u32 reg,u32 bits)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 
stm32_usart_clr_bits(struct uart_port * port,u32 reg,u32 bits)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 
stm32_usart_tx_empty(struct uart_port * port)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 
stm32_usart_rs485_rts_enable(struct uart_port * port)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 
stm32_usart_rs485_rts_disable(struct uart_port * port)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 
stm32_usart_config_reg_rs485(u32 * cr1,u32 * cr3,u32 delay_ADE,u32 delay_DDE,u32 baud)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 
stm32_usart_config_rs485(struct uart_port * port,struct ktermios * termios,struct serial_rs485 * rs485conf)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 
2291bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
2301bcda09dSBich HEMON 		cr1 = readl_relaxed(port->membase + ofs->cr1);
2311bcda09dSBich HEMON 		cr3 = readl_relaxed(port->membase + ofs->cr3);
2321bcda09dSBich HEMON 		usartdiv = readl_relaxed(port->membase + ofs->brr);
2331bcda09dSBich HEMON 		usartdiv = usartdiv & GENMASK(15, 0);
2341bcda09dSBich HEMON 		over8 = cr1 & USART_CR1_OVER8;
2351bcda09dSBich HEMON 
2361bcda09dSBich HEMON 		if (over8)
2371bcda09dSBich HEMON 			usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
2381bcda09dSBich HEMON 				   << USART_BRR_04_R_SHIFT;
2391bcda09dSBich HEMON 
2401bcda09dSBich HEMON 		baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
24156f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
2421bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
24356f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
24456f9a76cSErwan Le Ray 					     baud);
2451bcda09dSBich HEMON 
246f633eb29SLino Sanfilippo 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND)
2471bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
248f633eb29SLino Sanfilippo 		else
2491bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
2501bcda09dSBich HEMON 
2511bcda09dSBich HEMON 		writel_relaxed(cr3, port->membase + ofs->cr3);
2521bcda09dSBich HEMON 		writel_relaxed(cr1, port->membase + ofs->cr1);
25390b8cbd9SLino Sanfilippo 
2543e3578caSLino Sanfilippo 		if (!port->rs485_rx_during_tx_gpio)
25590b8cbd9SLino Sanfilippo 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
2563e3578caSLino Sanfilippo 
2571bcda09dSBich HEMON 	} else {
25856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3,
25956f9a76cSErwan Le Ray 				     USART_CR3_DEM | USART_CR3_DEP);
26056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1,
2611bcda09dSBich HEMON 				     USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
2621bcda09dSBich HEMON 	}
2631bcda09dSBich HEMON 
26456f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
2651bcda09dSBich HEMON 
266adafbbf6SLukas Wunner 	/* Adjust RTS polarity in case it's driven in software */
267adafbbf6SLukas Wunner 	if (stm32_usart_tx_empty(port))
268adafbbf6SLukas Wunner 		stm32_usart_rs485_rts_disable(port);
269adafbbf6SLukas Wunner 	else
270adafbbf6SLukas Wunner 		stm32_usart_rs485_rts_enable(port);
271adafbbf6SLukas Wunner 
2721bcda09dSBich HEMON 	return 0;
2731bcda09dSBich HEMON }
2741bcda09dSBich HEMON 
stm32_usart_init_rs485(struct uart_port * port,struct platform_device * pdev)27556f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port,
2761bcda09dSBich HEMON 				  struct platform_device *pdev)
2771bcda09dSBich HEMON {
2781bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
2791bcda09dSBich HEMON 
2801bcda09dSBich HEMON 	rs485conf->flags = 0;
2811bcda09dSBich HEMON 	rs485conf->delay_rts_before_send = 0;
2821bcda09dSBich HEMON 	rs485conf->delay_rts_after_send = 0;
2831bcda09dSBich HEMON 
2841bcda09dSBich HEMON 	if (!pdev->dev.of_node)
2851bcda09dSBich HEMON 		return -ENODEV;
2861bcda09dSBich HEMON 
287c150c0f3SLukas Wunner 	return uart_get_rs485_mode(port);
2881bcda09dSBich HEMON }
2891bcda09dSBich HEMON 
stm32_usart_rx_dma_started(struct stm32_port * stm32_port)29000d1f9c6SValentin Caron static bool stm32_usart_rx_dma_started(struct stm32_port *stm32_port)
29134891872SAlexandre TORGUE {
2927f28bceaSValentin Caron 	return stm32_port->rx_ch ? stm32_port->rx_dma_busy : false;
2937f28bceaSValentin Caron }
2947f28bceaSValentin Caron 
stm32_usart_rx_dma_terminate(struct stm32_port * stm32_port)2957f28bceaSValentin Caron static void stm32_usart_rx_dma_terminate(struct stm32_port *stm32_port)
2967f28bceaSValentin Caron {
2977f28bceaSValentin Caron 	dmaengine_terminate_async(stm32_port->rx_ch);
2987f28bceaSValentin Caron 	stm32_port->rx_dma_busy = false;
2997f28bceaSValentin Caron }
3007f28bceaSValentin Caron 
stm32_usart_dma_pause_resume(struct stm32_port * stm32_port,struct dma_chan * chan,enum dma_status expected_status,int dmaengine_pause_or_resume (struct dma_chan *),bool stm32_usart_xx_dma_started (struct stm32_port *),void stm32_usart_xx_dma_terminate (struct stm32_port *))3017f28bceaSValentin Caron static int stm32_usart_dma_pause_resume(struct stm32_port *stm32_port,
3027f28bceaSValentin Caron 					struct dma_chan *chan,
3037f28bceaSValentin Caron 					enum dma_status expected_status,
3047f28bceaSValentin Caron 					int dmaengine_pause_or_resume(struct dma_chan *),
3057f28bceaSValentin Caron 					bool stm32_usart_xx_dma_started(struct stm32_port *),
3067f28bceaSValentin Caron 					void stm32_usart_xx_dma_terminate(struct stm32_port *))
3077f28bceaSValentin Caron {
30800d1f9c6SValentin Caron 	struct uart_port *port = &stm32_port->port;
3097f28bceaSValentin Caron 	enum dma_status dma_status;
3107f28bceaSValentin Caron 	int ret;
31133bb2f6aSErwan Le Ray 
3127f28bceaSValentin Caron 	if (!stm32_usart_xx_dma_started(stm32_port))
3137f28bceaSValentin Caron 		return -EPERM;
31433bb2f6aSErwan Le Ray 
3157f28bceaSValentin Caron 	dma_status = dmaengine_tx_status(chan, chan->cookie, NULL);
3167f28bceaSValentin Caron 	if (dma_status != expected_status)
3177f28bceaSValentin Caron 		return -EAGAIN;
3187f28bceaSValentin Caron 
3197f28bceaSValentin Caron 	ret = dmaengine_pause_or_resume(chan);
3207f28bceaSValentin Caron 	if (ret) {
3217f28bceaSValentin Caron 		dev_err(port->dev, "DMA failed with error code: %d\n", ret);
3227f28bceaSValentin Caron 		stm32_usart_xx_dma_terminate(stm32_port);
3237f28bceaSValentin Caron 	}
3247f28bceaSValentin Caron 	return ret;
32533bb2f6aSErwan Le Ray }
32633bb2f6aSErwan Le Ray 
stm32_usart_rx_dma_pause(struct stm32_port * stm32_port)327a01ae50dSValentin Caron static int stm32_usart_rx_dma_pause(struct stm32_port *stm32_port)
328a01ae50dSValentin Caron {
329a01ae50dSValentin Caron 	return stm32_usart_dma_pause_resume(stm32_port, stm32_port->rx_ch,
330a01ae50dSValentin Caron 					    DMA_IN_PROGRESS, dmaengine_pause,
331a01ae50dSValentin Caron 					    stm32_usart_rx_dma_started,
332a01ae50dSValentin Caron 					    stm32_usart_rx_dma_terminate);
333a01ae50dSValentin Caron }
334a01ae50dSValentin Caron 
stm32_usart_rx_dma_resume(struct stm32_port * stm32_port)335a01ae50dSValentin Caron static int stm32_usart_rx_dma_resume(struct stm32_port *stm32_port)
336a01ae50dSValentin Caron {
337a01ae50dSValentin Caron 	return stm32_usart_dma_pause_resume(stm32_port, stm32_port->rx_ch,
338a01ae50dSValentin Caron 					    DMA_PAUSED, dmaengine_resume,
339a01ae50dSValentin Caron 					    stm32_usart_rx_dma_started,
340a01ae50dSValentin Caron 					    stm32_usart_rx_dma_terminate);
341a01ae50dSValentin Caron }
342a01ae50dSValentin Caron 
34333bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */
stm32_usart_pending_rx_pio(struct uart_port * port,u32 * sr)34433bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
34533bb2f6aSErwan Le Ray {
34633bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
34733bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
34834891872SAlexandre TORGUE 
34934891872SAlexandre TORGUE 	*sr = readl_relaxed(port->membase + ofs->isr);
35033bb2f6aSErwan Le Ray 	/* Get pending characters in RDR or FIFO */
35133bb2f6aSErwan Le Ray 	if (*sr & USART_SR_RXNE) {
35233bb2f6aSErwan Le Ray 		/* Get all pending characters from the RDR or the FIFO when using interrupts */
35300d1f9c6SValentin Caron 		if (!stm32_usart_rx_dma_started(stm32_port))
35433bb2f6aSErwan Le Ray 			return true;
35534891872SAlexandre TORGUE 
35633bb2f6aSErwan Le Ray 		/* Handle only RX data errors when using DMA */
35733bb2f6aSErwan Le Ray 		if (*sr & USART_SR_ERR_MASK)
35833bb2f6aSErwan Le Ray 			return true;
35934891872SAlexandre TORGUE 	}
36034891872SAlexandre TORGUE 
36133bb2f6aSErwan Le Ray 	return false;
36233bb2f6aSErwan Le Ray }
36333bb2f6aSErwan Le Ray 
stm32_usart_get_char_pio(struct uart_port * port)364fd2b55f8SJiri Slaby static u8 stm32_usart_get_char_pio(struct uart_port *port)
36534891872SAlexandre TORGUE {
36634891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
367d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
36834891872SAlexandre TORGUE 	unsigned long c;
36934891872SAlexandre TORGUE 
3706c5962f3SErwan Le Ray 	c = readl_relaxed(port->membase + ofs->rdr);
37133bb2f6aSErwan Le Ray 	/* Apply RDR data mask */
3726c5962f3SErwan Le Ray 	c &= stm32_port->rdr_mask;
3736c5962f3SErwan Le Ray 
3746c5962f3SErwan Le Ray 	return c;
37534891872SAlexandre TORGUE }
37634891872SAlexandre TORGUE 
stm32_usart_receive_chars_pio(struct uart_port * port)3776333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
37848a6092fSMaxime Coquelin {
379ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
380d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
3816333a485SErwan Le Ray 	unsigned int size = 0;
38248a6092fSMaxime Coquelin 	u32 sr;
383fd2b55f8SJiri Slaby 	u8 c, flag;
38448a6092fSMaxime Coquelin 
38533bb2f6aSErwan Le Ray 	while (stm32_usart_pending_rx_pio(port, &sr)) {
38648a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
38748a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
38848a6092fSMaxime Coquelin 
3894f01d833SErwan Le Ray 		/*
3904f01d833SErwan Le Ray 		 * Status bits has to be cleared before reading the RDR:
3914f01d833SErwan Le Ray 		 * In FIFO mode, reading the RDR will pop the next data
3924f01d833SErwan Le Ray 		 * (if any) along with its status bits into the SR.
3934f01d833SErwan Le Ray 		 * Not doing so leads to misalignement between RDR and SR,
3944f01d833SErwan Le Ray 		 * and clear status bits of the next rx data.
3954f01d833SErwan Le Ray 		 *
3964f01d833SErwan Le Ray 		 * Clear errors flags for stm32f7 and stm32h7 compatible
3974f01d833SErwan Le Ray 		 * devices. On stm32f4 compatible devices, the error bit is
3984f01d833SErwan Le Ray 		 * cleared by the sequence [read SR - read DR].
3994f01d833SErwan Le Ray 		 */
4004f01d833SErwan Le Ray 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
4011250ed71SFabrice Gasnier 			writel_relaxed(sr & USART_SR_ERR_MASK,
4021250ed71SFabrice Gasnier 				       port->membase + ofs->icr);
4034f01d833SErwan Le Ray 
40433bb2f6aSErwan Le Ray 		c = stm32_usart_get_char_pio(port);
4054f01d833SErwan Le Ray 		port->icount.rx++;
4066333a485SErwan Le Ray 		size++;
40748a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
4084f01d833SErwan Le Ray 			if (sr & USART_SR_ORE) {
40948a6092fSMaxime Coquelin 				port->icount.overrun++;
41048a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
41148a6092fSMaxime Coquelin 				port->icount.parity++;
41248a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
4134f01d833SErwan Le Ray 				/* Break detection if character is null */
4144f01d833SErwan Le Ray 				if (!c) {
4154f01d833SErwan Le Ray 					port->icount.brk++;
4164f01d833SErwan Le Ray 					if (uart_handle_break(port))
4174f01d833SErwan Le Ray 						continue;
4184f01d833SErwan Le Ray 				} else {
41948a6092fSMaxime Coquelin 					port->icount.frame++;
42048a6092fSMaxime Coquelin 				}
4214f01d833SErwan Le Ray 			}
42248a6092fSMaxime Coquelin 
42348a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
42448a6092fSMaxime Coquelin 
4254f01d833SErwan Le Ray 			if (sr & USART_SR_PE) {
42648a6092fSMaxime Coquelin 				flag = TTY_PARITY;
4274f01d833SErwan Le Ray 			} else if (sr & USART_SR_FE) {
4284f01d833SErwan Le Ray 				if (!c)
4294f01d833SErwan Le Ray 					flag = TTY_BREAK;
4304f01d833SErwan Le Ray 				else
43148a6092fSMaxime Coquelin 					flag = TTY_FRAME;
43248a6092fSMaxime Coquelin 			}
4334f01d833SErwan Le Ray 		}
43448a6092fSMaxime Coquelin 
435cea37afdSJohan Hovold 		if (uart_prepare_sysrq_char(port, c))
43648a6092fSMaxime Coquelin 			continue;
43748a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
43848a6092fSMaxime Coquelin 	}
4396333a485SErwan Le Ray 
4406333a485SErwan Le Ray 	return size;
44133bb2f6aSErwan Le Ray }
44233bb2f6aSErwan Le Ray 
stm32_usart_push_buffer_dma(struct uart_port * port,unsigned int dma_size)44333bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
44433bb2f6aSErwan Le Ray {
44533bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
44633bb2f6aSErwan Le Ray 	struct tty_port *ttyport = &stm32_port->port.state->port;
44733bb2f6aSErwan Le Ray 	unsigned char *dma_start;
44833bb2f6aSErwan Le Ray 	int dma_count, i;
44933bb2f6aSErwan Le Ray 
45033bb2f6aSErwan Le Ray 	dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
45133bb2f6aSErwan Le Ray 
45233bb2f6aSErwan Le Ray 	/*
45333bb2f6aSErwan Le Ray 	 * Apply rdr_mask on buffer in order to mask parity bit.
45433bb2f6aSErwan Le Ray 	 * This loop is useless in cs8 mode because DMA copies only
45533bb2f6aSErwan Le Ray 	 * 8 bits and already ignores parity bit.
45633bb2f6aSErwan Le Ray 	 */
45733bb2f6aSErwan Le Ray 	if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
45833bb2f6aSErwan Le Ray 		for (i = 0; i < dma_size; i++)
45933bb2f6aSErwan Le Ray 			*(dma_start + i) &= stm32_port->rdr_mask;
46033bb2f6aSErwan Le Ray 
46133bb2f6aSErwan Le Ray 	dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
46233bb2f6aSErwan Le Ray 	port->icount.rx += dma_count;
46333bb2f6aSErwan Le Ray 	if (dma_count != dma_size)
46433bb2f6aSErwan Le Ray 		port->icount.buf_overrun++;
46533bb2f6aSErwan Le Ray 	stm32_port->last_res -= dma_count;
46633bb2f6aSErwan Le Ray 	if (stm32_port->last_res == 0)
46733bb2f6aSErwan Le Ray 		stm32_port->last_res = RX_BUF_L;
46833bb2f6aSErwan Le Ray }
46933bb2f6aSErwan Le Ray 
stm32_usart_receive_chars_dma(struct uart_port * port)4706333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
47133bb2f6aSErwan Le Ray {
47233bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
4736333a485SErwan Le Ray 	unsigned int dma_size, size = 0;
47433bb2f6aSErwan Le Ray 
47533bb2f6aSErwan Le Ray 	/* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
47633bb2f6aSErwan Le Ray 	if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
47733bb2f6aSErwan Le Ray 		/* Conditional first part: from last_res to end of DMA buffer */
47833bb2f6aSErwan Le Ray 		dma_size = stm32_port->last_res;
47933bb2f6aSErwan Le Ray 		stm32_usart_push_buffer_dma(port, dma_size);
4806333a485SErwan Le Ray 		size = dma_size;
48133bb2f6aSErwan Le Ray 	}
48233bb2f6aSErwan Le Ray 
48333bb2f6aSErwan Le Ray 	dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
48433bb2f6aSErwan Le Ray 	stm32_usart_push_buffer_dma(port, dma_size);
4856333a485SErwan Le Ray 	size += dma_size;
4866333a485SErwan Le Ray 
4876333a485SErwan Le Ray 	return size;
48833bb2f6aSErwan Le Ray }
48933bb2f6aSErwan Le Ray 
stm32_usart_receive_chars(struct uart_port * port,bool force_dma_flush)4906333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
49133bb2f6aSErwan Le Ray {
49233bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
49333bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
49433bb2f6aSErwan Le Ray 	enum dma_status rx_dma_status;
49533bb2f6aSErwan Le Ray 	u32 sr;
4966333a485SErwan Le Ray 	unsigned int size = 0;
49733bb2f6aSErwan Le Ray 
49800d1f9c6SValentin Caron 	if (stm32_usart_rx_dma_started(stm32_port) || force_dma_flush) {
49933bb2f6aSErwan Le Ray 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
50033bb2f6aSErwan Le Ray 						    stm32_port->rx_ch->cookie,
50133bb2f6aSErwan Le Ray 						    &stm32_port->rx_dma_state);
502a01ae50dSValentin Caron 		if (rx_dma_status == DMA_IN_PROGRESS ||
503a01ae50dSValentin Caron 		    rx_dma_status == DMA_PAUSED) {
50433bb2f6aSErwan Le Ray 			/* Empty DMA buffer */
5056333a485SErwan Le Ray 			size = stm32_usart_receive_chars_dma(port);
50633bb2f6aSErwan Le Ray 			sr = readl_relaxed(port->membase + ofs->isr);
50733bb2f6aSErwan Le Ray 			if (sr & USART_SR_ERR_MASK) {
50833bb2f6aSErwan Le Ray 				/* Disable DMA request line */
50933bb2f6aSErwan Le Ray 				stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
51033bb2f6aSErwan Le Ray 
51133bb2f6aSErwan Le Ray 				/* Switch to PIO mode to handle the errors */
5126333a485SErwan Le Ray 				size += stm32_usart_receive_chars_pio(port);
51333bb2f6aSErwan Le Ray 
51433bb2f6aSErwan Le Ray 				/* Switch back to DMA mode */
51533bb2f6aSErwan Le Ray 				stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
51633bb2f6aSErwan Le Ray 			}
51733bb2f6aSErwan Le Ray 		} else {
51833bb2f6aSErwan Le Ray 			/* Disable RX DMA */
5197f28bceaSValentin Caron 			stm32_usart_rx_dma_terminate(stm32_port);
52033bb2f6aSErwan Le Ray 			/* Fall back to interrupt mode */
52133bb2f6aSErwan Le Ray 			dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
5226333a485SErwan Le Ray 			size = stm32_usart_receive_chars_pio(port);
52333bb2f6aSErwan Le Ray 		}
52433bb2f6aSErwan Le Ray 	} else {
5256333a485SErwan Le Ray 		size = stm32_usart_receive_chars_pio(port);
52633bb2f6aSErwan Le Ray 	}
52748a6092fSMaxime Coquelin 
5286333a485SErwan Le Ray 	return size;
52948a6092fSMaxime Coquelin }
53048a6092fSMaxime Coquelin 
stm32_usart_rx_dma_complete(void * arg)531a01ae50dSValentin Caron static void stm32_usart_rx_dma_complete(void *arg)
532a01ae50dSValentin Caron {
533a01ae50dSValentin Caron 	struct uart_port *port = arg;
534a01ae50dSValentin Caron 	struct tty_port *tport = &port->state->port;
535a01ae50dSValentin Caron 	unsigned int size;
536a01ae50dSValentin Caron 	unsigned long flags;
537a01ae50dSValentin Caron 
538a01ae50dSValentin Caron 	spin_lock_irqsave(&port->lock, flags);
539a01ae50dSValentin Caron 	size = stm32_usart_receive_chars(port, false);
540a01ae50dSValentin Caron 	uart_unlock_and_check_sysrq_irqrestore(port, flags);
541a01ae50dSValentin Caron 	if (size)
542a01ae50dSValentin Caron 		tty_flip_buffer_push(tport);
543a01ae50dSValentin Caron }
544a01ae50dSValentin Caron 
stm32_usart_rx_dma_start_or_resume(struct uart_port * port)545a01ae50dSValentin Caron static int stm32_usart_rx_dma_start_or_resume(struct uart_port *port)
546a01ae50dSValentin Caron {
547a01ae50dSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
548a01ae50dSValentin Caron 	struct dma_async_tx_descriptor *desc;
549a01ae50dSValentin Caron 	enum dma_status rx_dma_status;
550a01ae50dSValentin Caron 	int ret;
551a01ae50dSValentin Caron 
552a01ae50dSValentin Caron 	if (stm32_port->throttled)
553a01ae50dSValentin Caron 		return 0;
554a01ae50dSValentin Caron 
555a01ae50dSValentin Caron 	if (stm32_port->rx_dma_busy) {
556a01ae50dSValentin Caron 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
557a01ae50dSValentin Caron 						    stm32_port->rx_ch->cookie,
558a01ae50dSValentin Caron 						    NULL);
559a01ae50dSValentin Caron 		if (rx_dma_status == DMA_IN_PROGRESS)
560a01ae50dSValentin Caron 			return 0;
561a01ae50dSValentin Caron 
562a01ae50dSValentin Caron 		if (rx_dma_status == DMA_PAUSED && !stm32_usart_rx_dma_resume(stm32_port))
563a01ae50dSValentin Caron 			return 0;
564a01ae50dSValentin Caron 
565a01ae50dSValentin Caron 		dev_err(port->dev, "DMA failed : status error.\n");
566a01ae50dSValentin Caron 		stm32_usart_rx_dma_terminate(stm32_port);
567a01ae50dSValentin Caron 	}
568a01ae50dSValentin Caron 
569a01ae50dSValentin Caron 	stm32_port->rx_dma_busy = true;
570a01ae50dSValentin Caron 
571a01ae50dSValentin Caron 	stm32_port->last_res = RX_BUF_L;
572a01ae50dSValentin Caron 	/* Prepare a DMA cyclic transaction */
573a01ae50dSValentin Caron 	desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
574a01ae50dSValentin Caron 					 stm32_port->rx_dma_buf,
575a01ae50dSValentin Caron 					 RX_BUF_L, RX_BUF_P,
576a01ae50dSValentin Caron 					 DMA_DEV_TO_MEM,
577a01ae50dSValentin Caron 					 DMA_PREP_INTERRUPT);
578a01ae50dSValentin Caron 	if (!desc) {
579a01ae50dSValentin Caron 		dev_err(port->dev, "rx dma prep cyclic failed\n");
580a01ae50dSValentin Caron 		stm32_port->rx_dma_busy = false;
581a01ae50dSValentin Caron 		return -ENODEV;
582a01ae50dSValentin Caron 	}
583a01ae50dSValentin Caron 
584a01ae50dSValentin Caron 	desc->callback = stm32_usart_rx_dma_complete;
585a01ae50dSValentin Caron 	desc->callback_param = port;
586a01ae50dSValentin Caron 
587a01ae50dSValentin Caron 	/* Push current DMA transaction in the pending queue */
588a01ae50dSValentin Caron 	ret = dma_submit_error(dmaengine_submit(desc));
589a01ae50dSValentin Caron 	if (ret) {
590a01ae50dSValentin Caron 		dmaengine_terminate_sync(stm32_port->rx_ch);
591a01ae50dSValentin Caron 		stm32_port->rx_dma_busy = false;
592a01ae50dSValentin Caron 		return ret;
593a01ae50dSValentin Caron 	}
594a01ae50dSValentin Caron 
595a01ae50dSValentin Caron 	/* Issue pending DMA requests */
596a01ae50dSValentin Caron 	dma_async_issue_pending(stm32_port->rx_ch);
597a01ae50dSValentin Caron 
598a01ae50dSValentin Caron 	return 0;
599a01ae50dSValentin Caron }
600a01ae50dSValentin Caron 
stm32_usart_tx_dma_terminate(struct stm32_port * stm32_port)6019a135f16SValentin Caron static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
6029a135f16SValentin Caron {
6039a135f16SValentin Caron 	dmaengine_terminate_async(stm32_port->tx_ch);
6049a135f16SValentin Caron 	stm32_port->tx_dma_busy = false;
6059a135f16SValentin Caron }
6069a135f16SValentin Caron 
stm32_usart_tx_dma_started(struct stm32_port * stm32_port)6079a135f16SValentin Caron static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
6089a135f16SValentin Caron {
6099a135f16SValentin Caron 	/*
6109a135f16SValentin Caron 	 * We cannot use the function "dmaengine_tx_status" to know the
6119a135f16SValentin Caron 	 * status of DMA. This function does not show if the "dma complete"
6129a135f16SValentin Caron 	 * callback of the DMA transaction has been called. So we prefer
6139a135f16SValentin Caron 	 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
6149a135f16SValentin Caron 	 * same time.
6159a135f16SValentin Caron 	 */
6169a135f16SValentin Caron 	return stm32_port->tx_dma_busy;
6179a135f16SValentin Caron }
6189a135f16SValentin Caron 
stm32_usart_tx_dma_pause(struct stm32_port * stm32_port)6197f28bceaSValentin Caron static int stm32_usart_tx_dma_pause(struct stm32_port *stm32_port)
6207f28bceaSValentin Caron {
6217f28bceaSValentin Caron 	return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
6227f28bceaSValentin Caron 					    DMA_IN_PROGRESS, dmaengine_pause,
6237f28bceaSValentin Caron 					    stm32_usart_tx_dma_started,
6247f28bceaSValentin Caron 					    stm32_usart_tx_dma_terminate);
6257f28bceaSValentin Caron }
6267f28bceaSValentin Caron 
stm32_usart_tx_dma_resume(struct stm32_port * stm32_port)6277f28bceaSValentin Caron static int stm32_usart_tx_dma_resume(struct stm32_port *stm32_port)
6287f28bceaSValentin Caron {
6297f28bceaSValentin Caron 	return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
6307f28bceaSValentin Caron 					    DMA_PAUSED, dmaengine_resume,
6317f28bceaSValentin Caron 					    stm32_usart_tx_dma_started,
6327f28bceaSValentin Caron 					    stm32_usart_tx_dma_terminate);
6337f28bceaSValentin Caron }
6347f28bceaSValentin Caron 
stm32_usart_tx_dma_complete(void * arg)63556f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg)
63634891872SAlexandre TORGUE {
63734891872SAlexandre TORGUE 	struct uart_port *port = arg;
63834891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
639f16b90c2SErwan Le Ray 	unsigned long flags;
64034891872SAlexandre TORGUE 
6419a135f16SValentin Caron 	stm32_usart_tx_dma_terminate(stm32port);
64234891872SAlexandre TORGUE 
64334891872SAlexandre TORGUE 	/* Let's see if we have pending data to send */
644f16b90c2SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
64556f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
646f16b90c2SErwan Le Ray 	spin_unlock_irqrestore(&port->lock, flags);
64734891872SAlexandre TORGUE }
64834891872SAlexandre TORGUE 
stm32_usart_tx_interrupt_enable(struct uart_port * port)64956f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
650d075719eSErwan Le Ray {
651d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
652d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
653d075719eSErwan Le Ray 
654d075719eSErwan Le Ray 	/*
655d075719eSErwan Le Ray 	 * Enables TX FIFO threashold irq when FIFO is enabled,
656d075719eSErwan Le Ray 	 * or TX empty irq when FIFO is disabled
657d075719eSErwan Le Ray 	 */
6582aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
65956f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
660d075719eSErwan Le Ray 	else
66156f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
662d075719eSErwan Le Ray }
663d075719eSErwan Le Ray 
stm32_usart_tc_interrupt_enable(struct uart_port * port)664d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_enable(struct uart_port *port)
665d7c76716SMarek Vasut {
666d7c76716SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
667d7c76716SMarek Vasut 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
668d7c76716SMarek Vasut 
669d7c76716SMarek Vasut 	stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE);
670d7c76716SMarek Vasut }
671d7c76716SMarek Vasut 
stm32_usart_tx_interrupt_disable(struct uart_port * port)67256f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
673d075719eSErwan Le Ray {
674d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
675d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
676d075719eSErwan Le Ray 
6772aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
67856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
679d075719eSErwan Le Ray 	else
68056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
681d075719eSErwan Le Ray }
682d075719eSErwan Le Ray 
stm32_usart_tc_interrupt_disable(struct uart_port * port)683d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_disable(struct uart_port *port)
684d7c76716SMarek Vasut {
685d7c76716SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
686d7c76716SMarek Vasut 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
687d7c76716SMarek Vasut 
688d7c76716SMarek Vasut 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE);
689d7c76716SMarek Vasut }
690d7c76716SMarek Vasut 
stm32_usart_transmit_chars_pio(struct uart_port * port)69156f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port)
69234891872SAlexandre TORGUE {
69334891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
694d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
69534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
69634891872SAlexandre TORGUE 
6975d9176edSErwan Le Ray 	while (!uart_circ_empty(xmit)) {
6985d9176edSErwan Le Ray 		/* Check that TDR is empty before filling FIFO */
6995d9176edSErwan Le Ray 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
7005d9176edSErwan Le Ray 			break;
70134891872SAlexandre TORGUE 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
70229d8c07bSIlpo Järvinen 		uart_xmit_advance(port, 1);
70334891872SAlexandre TORGUE 	}
70434891872SAlexandre TORGUE 
7055d9176edSErwan Le Ray 	/* rely on TXE irq (mask or unmask) for sending remaining data */
7065d9176edSErwan Le Ray 	if (uart_circ_empty(xmit))
70756f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
7085d9176edSErwan Le Ray 	else
70956f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_enable(port);
7105d9176edSErwan Le Ray }
7115d9176edSErwan Le Ray 
stm32_usart_transmit_chars_dma(struct uart_port * port)71256f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port)
71334891872SAlexandre TORGUE {
71434891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
71534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
71634891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
717195437d1SValentin Caron 	unsigned int count;
718db89728aSValentin Caron 	int ret;
71934891872SAlexandre TORGUE 
7209a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32port)) {
7217f28bceaSValentin Caron 		ret = stm32_usart_tx_dma_resume(stm32port);
7227f28bceaSValentin Caron 		if (ret < 0 && ret != -EAGAIN)
7237f28bceaSValentin Caron 			goto fallback_err;
72434891872SAlexandre TORGUE 		return;
7259a135f16SValentin Caron 	}
72634891872SAlexandre TORGUE 
72734891872SAlexandre TORGUE 	count = uart_circ_chars_pending(xmit);
72834891872SAlexandre TORGUE 
72934891872SAlexandre TORGUE 	if (count > TX_BUF_L)
73034891872SAlexandre TORGUE 		count = TX_BUF_L;
73134891872SAlexandre TORGUE 
73234891872SAlexandre TORGUE 	if (xmit->tail < xmit->head) {
73334891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
73434891872SAlexandre TORGUE 	} else {
73534891872SAlexandre TORGUE 		size_t one = UART_XMIT_SIZE - xmit->tail;
73634891872SAlexandre TORGUE 		size_t two;
73734891872SAlexandre TORGUE 
73834891872SAlexandre TORGUE 		if (one > count)
73934891872SAlexandre TORGUE 			one = count;
74034891872SAlexandre TORGUE 		two = count - one;
74134891872SAlexandre TORGUE 
74234891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
74334891872SAlexandre TORGUE 		if (two)
74434891872SAlexandre TORGUE 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
74534891872SAlexandre TORGUE 	}
74634891872SAlexandre TORGUE 
74734891872SAlexandre TORGUE 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
74834891872SAlexandre TORGUE 					   stm32port->tx_dma_buf,
74934891872SAlexandre TORGUE 					   count,
75034891872SAlexandre TORGUE 					   DMA_MEM_TO_DEV,
75134891872SAlexandre TORGUE 					   DMA_PREP_INTERRUPT);
75234891872SAlexandre TORGUE 
753e7997f7fSErwan Le Ray 	if (!desc)
754e7997f7fSErwan Le Ray 		goto fallback_err;
75534891872SAlexandre TORGUE 
7569a135f16SValentin Caron 	/*
7579a135f16SValentin Caron 	 * Set "tx_dma_busy" flag. This flag will be released when
7589a135f16SValentin Caron 	 * dmaengine_terminate_async will be called. This flag helps
7599a135f16SValentin Caron 	 * transmit_chars_dma not to start another DMA transaction
7609a135f16SValentin Caron 	 * if the callback of the previous is not yet called.
7619a135f16SValentin Caron 	 */
7629a135f16SValentin Caron 	stm32port->tx_dma_busy = true;
7639a135f16SValentin Caron 
76456f9a76cSErwan Le Ray 	desc->callback = stm32_usart_tx_dma_complete;
76534891872SAlexandre TORGUE 	desc->callback_param = port;
76634891872SAlexandre TORGUE 
76734891872SAlexandre TORGUE 	/* Push current DMA TX transaction in the pending queue */
768db89728aSValentin Caron 	/* DMA no yet started, safe to free resources */
7697f28bceaSValentin Caron 	ret = dma_submit_error(dmaengine_submit(desc));
7707f28bceaSValentin Caron 	if (ret) {
7717f28bceaSValentin Caron 		dev_err(port->dev, "DMA failed with error code: %d\n", ret);
7727f28bceaSValentin Caron 		stm32_usart_tx_dma_terminate(stm32port);
7737f28bceaSValentin Caron 		goto fallback_err;
7747f28bceaSValentin Caron 	}
77534891872SAlexandre TORGUE 
77634891872SAlexandre TORGUE 	/* Issue pending DMA TX requests */
77734891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->tx_ch);
77834891872SAlexandre TORGUE 
77929d8c07bSIlpo Järvinen 	uart_xmit_advance(port, count);
78029d8c07bSIlpo Järvinen 
781e7997f7fSErwan Le Ray 	return;
782e7997f7fSErwan Le Ray 
783e7997f7fSErwan Le Ray fallback_err:
78456f9a76cSErwan Le Ray 	stm32_usart_transmit_chars_pio(port);
78534891872SAlexandre TORGUE }
78634891872SAlexandre TORGUE 
stm32_usart_transmit_chars(struct uart_port * port)78756f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port)
78848a6092fSMaxime Coquelin {
789ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
790d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
79148a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
792d3d079bdSValentin Caron 	u32 isr;
793d3d079bdSValentin Caron 	int ret;
79448a6092fSMaxime Coquelin 
795d7c76716SMarek Vasut 	if (!stm32_port->hw_flow_control &&
796c47527cbSMarek Vasut 	    port->rs485.flags & SER_RS485_ENABLED &&
797c47527cbSMarek Vasut 	    (port->x_char ||
798c47527cbSMarek Vasut 	     !(uart_circ_empty(xmit) || uart_tx_stopped(port)))) {
799d7c76716SMarek Vasut 		stm32_usart_tc_interrupt_disable(port);
800d7c76716SMarek Vasut 		stm32_usart_rs485_rts_enable(port);
801d7c76716SMarek Vasut 	}
802d7c76716SMarek Vasut 
80348a6092fSMaxime Coquelin 	if (port->x_char) {
8047f28bceaSValentin Caron 		/* dma terminate may have been called in case of dma pause failure */
8057f28bceaSValentin Caron 		stm32_usart_tx_dma_pause(stm32_port);
8067f28bceaSValentin Caron 
807d3d079bdSValentin Caron 		/* Check that TDR is empty before filling FIFO */
808d3d079bdSValentin Caron 		ret =
809d3d079bdSValentin Caron 		readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
810d3d079bdSValentin Caron 						  isr,
811d3d079bdSValentin Caron 						  (isr & USART_SR_TXE),
812d3d079bdSValentin Caron 						  10, 1000);
813d3d079bdSValentin Caron 		if (ret)
814d3d079bdSValentin Caron 			dev_warn(port->dev, "1 character may be erased\n");
815d3d079bdSValentin Caron 
816ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
81748a6092fSMaxime Coquelin 		port->x_char = 0;
81848a6092fSMaxime Coquelin 		port->icount.tx++;
819db89728aSValentin Caron 
8207f28bceaSValentin Caron 		/* dma terminate may have been called in case of dma resume failure */
8217f28bceaSValentin Caron 		stm32_usart_tx_dma_resume(stm32_port);
82248a6092fSMaxime Coquelin 		return;
82348a6092fSMaxime Coquelin 	}
82448a6092fSMaxime Coquelin 
825b83b957cSErwan Le Ray 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
82656f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
82748a6092fSMaxime Coquelin 		return;
82848a6092fSMaxime Coquelin 	}
82948a6092fSMaxime Coquelin 
83064c32eabSErwan Le Ray 	if (ofs->icr == UNDEF_REG)
83156f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
83264c32eabSErwan Le Ray 	else
8331250ed71SFabrice Gasnier 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
83464c32eabSErwan Le Ray 
83534891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
83656f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_dma(port);
83734891872SAlexandre TORGUE 	else
83856f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_pio(port);
83948a6092fSMaxime Coquelin 
84048a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
84148a6092fSMaxime Coquelin 		uart_write_wakeup(port);
84248a6092fSMaxime Coquelin 
843d7c76716SMarek Vasut 	if (uart_circ_empty(xmit)) {
84456f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
845d7c76716SMarek Vasut 		if (!stm32_port->hw_flow_control &&
846d7c76716SMarek Vasut 		    port->rs485.flags & SER_RS485_ENABLED) {
847d7c76716SMarek Vasut 			stm32_usart_tc_interrupt_enable(port);
848d7c76716SMarek Vasut 		}
849d7c76716SMarek Vasut 	}
85048a6092fSMaxime Coquelin }
85148a6092fSMaxime Coquelin 
stm32_usart_interrupt(int irq,void * ptr)85256f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
85348a6092fSMaxime Coquelin {
85448a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
85512761869SErwan Le Ray 	struct tty_port *tport = &port->state->port;
856ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
857d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
85848a6092fSMaxime Coquelin 	u32 sr;
8596333a485SErwan Le Ray 	unsigned int size;
8609f9be0ecSUwe Kleine-König 	irqreturn_t ret = IRQ_NONE;
86148a6092fSMaxime Coquelin 
862ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
86348a6092fSMaxime Coquelin 
864d7c76716SMarek Vasut 	if (!stm32_port->hw_flow_control &&
865d7c76716SMarek Vasut 	    port->rs485.flags & SER_RS485_ENABLED &&
866d7c76716SMarek Vasut 	    (sr & USART_SR_TC)) {
867d7c76716SMarek Vasut 		stm32_usart_tc_interrupt_disable(port);
868d7c76716SMarek Vasut 		stm32_usart_rs485_rts_disable(port);
8699f9be0ecSUwe Kleine-König 		ret = IRQ_HANDLED;
870d7c76716SMarek Vasut 	}
871d7c76716SMarek Vasut 
8729f9be0ecSUwe Kleine-König 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) {
8734cc0ed62SErwan Le Ray 		writel_relaxed(USART_ICR_RTOCF,
8744cc0ed62SErwan Le Ray 			       port->membase + ofs->icr);
8759f9be0ecSUwe Kleine-König 		ret = IRQ_HANDLED;
8769f9be0ecSUwe Kleine-König 	}
8774cc0ed62SErwan Le Ray 
87812761869SErwan Le Ray 	if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
87912761869SErwan Le Ray 		/* Clear wake up flag and disable wake up interrupt */
880270e5a74SFabrice Gasnier 		writel_relaxed(USART_ICR_WUCF,
881270e5a74SFabrice Gasnier 			       port->membase + ofs->icr);
88212761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
88312761869SErwan Le Ray 		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
88412761869SErwan Le Ray 			pm_wakeup_event(tport->tty->dev, 0);
8859f9be0ecSUwe Kleine-König 		ret = IRQ_HANDLED;
88612761869SErwan Le Ray 	}
887270e5a74SFabrice Gasnier 
88833bb2f6aSErwan Le Ray 	/*
88933bb2f6aSErwan Le Ray 	 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
89033bb2f6aSErwan Le Ray 	 * line has been masked by HW and rx data are stacking in FIFO.
89133bb2f6aSErwan Le Ray 	 */
892d1ec8a2eSErwan Le Ray 	if (!stm32_port->throttled) {
89300d1f9c6SValentin Caron 		if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_started(stm32_port)) ||
89400d1f9c6SValentin Caron 		    ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_started(stm32_port))) {
8956333a485SErwan Le Ray 			spin_lock(&port->lock);
8966333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, false);
8976333a485SErwan Le Ray 			uart_unlock_and_check_sysrq(port);
8986333a485SErwan Le Ray 			if (size)
8996333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
9009f9be0ecSUwe Kleine-König 			ret = IRQ_HANDLED;
901d1ec8a2eSErwan Le Ray 		}
902d1ec8a2eSErwan Le Ray 	}
90348a6092fSMaxime Coquelin 
904ad767681SErwan Le Ray 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
905ad767681SErwan Le Ray 		spin_lock(&port->lock);
90656f9a76cSErwan Le Ray 		stm32_usart_transmit_chars(port);
90701d32d71SAlexandre TORGUE 		spin_unlock(&port->lock);
9089f9be0ecSUwe Kleine-König 		ret = IRQ_HANDLED;
909ad767681SErwan Le Ray 	}
91001d32d71SAlexandre TORGUE 
911cc58d0a3SErwan Le Ray 	/* Receiver timeout irq for DMA RX */
91200d1f9c6SValentin Caron 	if (stm32_usart_rx_dma_started(stm32_port) && !stm32_port->throttled) {
9133f6c02faSMarek Vasut 		spin_lock(&port->lock);
9146333a485SErwan Le Ray 		size = stm32_usart_receive_chars(port, false);
9153f6c02faSMarek Vasut 		uart_unlock_and_check_sysrq(port);
9166333a485SErwan Le Ray 		if (size)
9176333a485SErwan Le Ray 			tty_flip_buffer_push(tport);
9189f9be0ecSUwe Kleine-König 		ret = IRQ_HANDLED;
9196333a485SErwan Le Ray 	}
92034891872SAlexandre TORGUE 
9219f9be0ecSUwe Kleine-König 	return ret;
92248a6092fSMaxime Coquelin }
92348a6092fSMaxime Coquelin 
stm32_usart_set_mctrl(struct uart_port * port,unsigned int mctrl)92456f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
92548a6092fSMaxime Coquelin {
926ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
927d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
928ada8618fSAlexandre TORGUE 
92948a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
93056f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
93148a6092fSMaxime Coquelin 	else
93256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
9336cf61b9bSManivannan Sadhasivam 
9346cf61b9bSManivannan Sadhasivam 	mctrl_gpio_set(stm32_port->gpios, mctrl);
93548a6092fSMaxime Coquelin }
93648a6092fSMaxime Coquelin 
stm32_usart_get_mctrl(struct uart_port * port)93756f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
93848a6092fSMaxime Coquelin {
9396cf61b9bSManivannan Sadhasivam 	struct stm32_port *stm32_port = to_stm32_port(port);
9406cf61b9bSManivannan Sadhasivam 	unsigned int ret;
9416cf61b9bSManivannan Sadhasivam 
94248a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
9436cf61b9bSManivannan Sadhasivam 	ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
9446cf61b9bSManivannan Sadhasivam 
9456cf61b9bSManivannan Sadhasivam 	return mctrl_gpio_get(stm32_port->gpios, &ret);
9466cf61b9bSManivannan Sadhasivam }
9476cf61b9bSManivannan Sadhasivam 
stm32_usart_enable_ms(struct uart_port * port)94856f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port)
9496cf61b9bSManivannan Sadhasivam {
9506cf61b9bSManivannan Sadhasivam 	mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
9516cf61b9bSManivannan Sadhasivam }
9526cf61b9bSManivannan Sadhasivam 
stm32_usart_disable_ms(struct uart_port * port)95356f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port)
9546cf61b9bSManivannan Sadhasivam {
9556cf61b9bSManivannan Sadhasivam 	mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
95648a6092fSMaxime Coquelin }
95748a6092fSMaxime Coquelin 
95848a6092fSMaxime Coquelin /* Transmit stop */
stm32_usart_stop_tx(struct uart_port * port)95956f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port)
96048a6092fSMaxime Coquelin {
961ad0c2748SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
962ad0c2748SMarek Vasut 
96356f9a76cSErwan Le Ray 	stm32_usart_tx_interrupt_disable(port);
9647f28bceaSValentin Caron 
9657f28bceaSValentin Caron 	/* dma terminate may have been called in case of dma pause failure */
9667f28bceaSValentin Caron 	stm32_usart_tx_dma_pause(stm32_port);
967ad0c2748SMarek Vasut 
9683bcea529SMarek Vasut 	stm32_usart_rs485_rts_disable(port);
96948a6092fSMaxime Coquelin }
97048a6092fSMaxime Coquelin 
97148a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
stm32_usart_start_tx(struct uart_port * port)97256f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port)
97348a6092fSMaxime Coquelin {
97448a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
97548a6092fSMaxime Coquelin 
976d7c76716SMarek Vasut 	if (uart_circ_empty(xmit) && !port->x_char) {
977d7c76716SMarek Vasut 		stm32_usart_rs485_rts_disable(port);
97848a6092fSMaxime Coquelin 		return;
979d7c76716SMarek Vasut 	}
98048a6092fSMaxime Coquelin 
9813bcea529SMarek Vasut 	stm32_usart_rs485_rts_enable(port);
982ad0c2748SMarek Vasut 
98356f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
98448a6092fSMaxime Coquelin }
98548a6092fSMaxime Coquelin 
9863d82be8bSErwan Le Ray /* Flush the transmit buffer. */
stm32_usart_flush_buffer(struct uart_port * port)9873d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port)
9883d82be8bSErwan Le Ray {
9893d82be8bSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
9903d82be8bSErwan Le Ray 
991db89728aSValentin Caron 	if (stm32_port->tx_ch)
9929a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
9933d82be8bSErwan Le Ray }
9943d82be8bSErwan Le Ray 
99548a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
stm32_usart_throttle(struct uart_port * port)99656f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port)
99748a6092fSMaxime Coquelin {
998ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
999d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
100048a6092fSMaxime Coquelin 	unsigned long flags;
100148a6092fSMaxime Coquelin 
100248a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
1003d1ec8a2eSErwan Le Ray 
1004d1ec8a2eSErwan Le Ray 	/*
1005a01ae50dSValentin Caron 	 * Pause DMA transfer, so the RX data gets queued into the FIFO.
1006d1ec8a2eSErwan Le Ray 	 * Hardware flow control is triggered when RX FIFO is full.
1007d1ec8a2eSErwan Le Ray 	 */
1008a01ae50dSValentin Caron 	stm32_usart_rx_dma_pause(stm32_port);
1009d1ec8a2eSErwan Le Ray 
101056f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
1011d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
101256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
1013d0a6a7bcSErwan Le Ray 
1014d1ec8a2eSErwan Le Ray 	stm32_port->throttled = true;
101548a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
101648a6092fSMaxime Coquelin }
101748a6092fSMaxime Coquelin 
101848a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
stm32_usart_unthrottle(struct uart_port * port)101956f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port)
102048a6092fSMaxime Coquelin {
1021ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1022d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
102348a6092fSMaxime Coquelin 	unsigned long flags;
102448a6092fSMaxime Coquelin 
102548a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
102656f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
1027d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
102856f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
1029d0a6a7bcSErwan Le Ray 
1030a01ae50dSValentin Caron 	stm32_port->throttled = false;
1031a01ae50dSValentin Caron 
1032d1ec8a2eSErwan Le Ray 	/*
1033a01ae50dSValentin Caron 	 * Switch back to DMA mode (resume DMA).
1034d1ec8a2eSErwan Le Ray 	 * Hardware flow control is stopped when FIFO is not full any more.
1035d1ec8a2eSErwan Le Ray 	 */
1036d1ec8a2eSErwan Le Ray 	if (stm32_port->rx_ch)
1037a01ae50dSValentin Caron 		stm32_usart_rx_dma_start_or_resume(port);
1038d1ec8a2eSErwan Le Ray 
103948a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
104048a6092fSMaxime Coquelin }
104148a6092fSMaxime Coquelin 
104248a6092fSMaxime Coquelin /* Receive stop */
stm32_usart_stop_rx(struct uart_port * port)104356f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port)
104448a6092fSMaxime Coquelin {
1045ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1046d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1047ada8618fSAlexandre TORGUE 
1048e0abc903SErwan Le Ray 	/* Disable DMA request line. */
1049a01ae50dSValentin Caron 	stm32_usart_rx_dma_pause(stm32_port);
1050e0abc903SErwan Le Ray 
105156f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
1052d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
105356f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
105448a6092fSMaxime Coquelin }
105548a6092fSMaxime Coquelin 
105648a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
stm32_usart_break_ctl(struct uart_port * port,int break_state)105756f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
105848a6092fSMaxime Coquelin {
105948a6092fSMaxime Coquelin }
106048a6092fSMaxime Coquelin 
stm32_usart_startup(struct uart_port * port)106156f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port)
106248a6092fSMaxime Coquelin {
1063ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1064d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1065f4518a8aSErwan Le Ray 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
106648a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
106748a6092fSMaxime Coquelin 	u32 val;
106848a6092fSMaxime Coquelin 	int ret;
106948a6092fSMaxime Coquelin 
10703f6c02faSMarek Vasut 	ret = request_irq(port->irq, stm32_usart_interrupt,
10713f6c02faSMarek Vasut 			  IRQF_NO_SUSPEND, name, port);
107248a6092fSMaxime Coquelin 	if (ret)
107348a6092fSMaxime Coquelin 		return ret;
107448a6092fSMaxime Coquelin 
10753cd66593SMartin Devera 	if (stm32_port->swap) {
10763cd66593SMartin Devera 		val = readl_relaxed(port->membase + ofs->cr2);
10773cd66593SMartin Devera 		val |= USART_CR2_SWAP;
10783cd66593SMartin Devera 		writel_relaxed(val, port->membase + ofs->cr2);
10793cd66593SMartin Devera 	}
1080*12e9459dSUwe Kleine-König 	stm32_port->throttled = false;
10813cd66593SMartin Devera 
108284872dc4SErwan Le Ray 	/* RX FIFO Flush */
108384872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
1084315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
108548a6092fSMaxime Coquelin 
1086e0abc903SErwan Le Ray 	if (stm32_port->rx_ch) {
1087a01ae50dSValentin Caron 		ret = stm32_usart_rx_dma_start_or_resume(port);
1088e0abc903SErwan Le Ray 		if (ret) {
10896eeb348cSErwan Le Ray 			free_irq(port->irq, port);
10906eeb348cSErwan Le Ray 			return ret;
1091e0abc903SErwan Le Ray 		}
1092e0abc903SErwan Le Ray 	}
1093d1ec8a2eSErwan Le Ray 
109425a8e761SErwan Le Ray 	/* RX enabling */
1095f4518a8aSErwan Le Ray 	val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
109656f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, val);
109784872dc4SErwan Le Ray 
109848a6092fSMaxime Coquelin 	return 0;
109948a6092fSMaxime Coquelin }
110048a6092fSMaxime Coquelin 
stm32_usart_shutdown(struct uart_port * port)110156f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port)
110248a6092fSMaxime Coquelin {
1103ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1104d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1105d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
110664c32eabSErwan Le Ray 	u32 val, isr;
110764c32eabSErwan Le Ray 	int ret;
110848a6092fSMaxime Coquelin 
11099a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32_port))
11109a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
111156a23f93SValentin Caron 
1112db89728aSValentin Caron 	if (stm32_port->tx_ch)
1113db89728aSValentin Caron 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1114db89728aSValentin Caron 
11156cf61b9bSManivannan Sadhasivam 	/* Disable modem control interrupts */
111656f9a76cSErwan Le Ray 	stm32_usart_disable_ms(port);
11176cf61b9bSManivannan Sadhasivam 
11184cc0ed62SErwan Le Ray 	val = USART_CR1_TXEIE | USART_CR1_TE;
11194cc0ed62SErwan Le Ray 	val |= stm32_port->cr1_irq | USART_CR1_RE;
112087f1f809SAlexandre TORGUE 	val |= BIT(cfg->uart_enable_bit);
1121351a762aSGerald Baeza 	if (stm32_port->fifoen)
1122351a762aSGerald Baeza 		val |= USART_CR1_FIFOEN;
112364c32eabSErwan Le Ray 
112464c32eabSErwan Le Ray 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
112564c32eabSErwan Le Ray 					 isr, (isr & USART_SR_TC),
112664c32eabSErwan Le Ray 					 10, 100000);
112764c32eabSErwan Le Ray 
1128c31c3ea0SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set */
112964c32eabSErwan Le Ray 	if (ret)
1130c31c3ea0SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
113164c32eabSErwan Le Ray 
1132e0abc903SErwan Le Ray 	/* Disable RX DMA. */
11332490a0caSAmelie Delaunay 	if (stm32_port->rx_ch) {
11347f28bceaSValentin Caron 		stm32_usart_rx_dma_terminate(stm32_port);
11352490a0caSAmelie Delaunay 		dmaengine_synchronize(stm32_port->rx_ch);
11362490a0caSAmelie Delaunay 	}
1137e0abc903SErwan Le Ray 
11389f77d192SErwan Le Ray 	/* flush RX & TX FIFO */
11399f77d192SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
11409f77d192SErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
11419f77d192SErwan Le Ray 			       port->membase + ofs->rqr);
11429f77d192SErwan Le Ray 
114356f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, val);
114448a6092fSMaxime Coquelin 
114548a6092fSMaxime Coquelin 	free_irq(port->irq, port);
114648a6092fSMaxime Coquelin }
114748a6092fSMaxime Coquelin 
stm32_usart_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)114856f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port,
114956f9a76cSErwan Le Ray 				    struct ktermios *termios,
1150bec5b814SIlpo Järvinen 				    const struct ktermios *old)
115148a6092fSMaxime Coquelin {
115248a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
1153d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1154d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
11551bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
1156c8a9d043SErwan Le Ray 	unsigned int baud, bits;
115748a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
115848a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
1159f264c6f6SErwan Le Ray 	u32 cr1, cr2, cr3, isr;
116048a6092fSMaxime Coquelin 	unsigned long flags;
1161f264c6f6SErwan Le Ray 	int ret;
116248a6092fSMaxime Coquelin 
116348a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
116448a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
116548a6092fSMaxime Coquelin 
116648a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
116748a6092fSMaxime Coquelin 
116848a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
116948a6092fSMaxime Coquelin 
1170f264c6f6SErwan Le Ray 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
1171f264c6f6SErwan Le Ray 						isr,
1172f264c6f6SErwan Le Ray 						(isr & USART_SR_TC),
1173f264c6f6SErwan Le Ray 						10, 100000);
1174f264c6f6SErwan Le Ray 
1175f264c6f6SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set. */
1176f264c6f6SErwan Le Ray 	if (ret)
1177f264c6f6SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
1178f264c6f6SErwan Le Ray 
117948a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
1180ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
118148a6092fSMaxime Coquelin 
118284872dc4SErwan Le Ray 	/* flush RX & TX FIFO */
118384872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
1184315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1185315e2d8aSErwan Le Ray 			       port->membase + ofs->rqr);
11861bcda09dSBich HEMON 
118784872dc4SErwan Le Ray 	cr1 = USART_CR1_TE | USART_CR1_RE;
1188351a762aSGerald Baeza 	if (stm32_port->fifoen)
1189351a762aSGerald Baeza 		cr1 |= USART_CR1_FIFOEN;
11903cd66593SMartin Devera 	cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
119125a8e761SErwan Le Ray 
119225a8e761SErwan Le Ray 	/* Tx and RX FIFO configuration */
1193d075719eSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
119425a8e761SErwan Le Ray 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
119525a8e761SErwan Le Ray 	if (stm32_port->fifoen) {
11962aa1bbb2SFabrice Gasnier 		if (stm32_port->txftcfg >= 0)
11972aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
11982aa1bbb2SFabrice Gasnier 		if (stm32_port->rxftcfg >= 0)
11992aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
120025a8e761SErwan Le Ray 	}
120148a6092fSMaxime Coquelin 
120248a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
120348a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
120448a6092fSMaxime Coquelin 
12053ec2ff37SJiri Slaby 	bits = tty_get_char_size(cflag);
12066c5962f3SErwan Le Ray 	stm32_port->rdr_mask = (BIT(bits) - 1);
1207c8a9d043SErwan Le Ray 
120848a6092fSMaxime Coquelin 	if (cflag & PARENB) {
1209c8a9d043SErwan Le Ray 		bits++;
121048a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
1211c8a9d043SErwan Le Ray 	}
1212c8a9d043SErwan Le Ray 
1213c8a9d043SErwan Le Ray 	/*
1214c8a9d043SErwan Le Ray 	 * Word length configuration:
1215c8a9d043SErwan Le Ray 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1216c8a9d043SErwan Le Ray 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1217c8a9d043SErwan Le Ray 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1218c8a9d043SErwan Le Ray 	 * M0 and M1 already cleared by cr1 initialization.
1219c8a9d043SErwan Le Ray 	 */
12201deeda8dSIlpo Järvinen 	if (bits == 9) {
1221ada8618fSAlexandre TORGUE 		cr1 |= USART_CR1_M0;
12221deeda8dSIlpo Järvinen 	} else if ((bits == 7) && cfg->has_7bits_data) {
1223c8a9d043SErwan Le Ray 		cr1 |= USART_CR1_M1;
12241deeda8dSIlpo Järvinen 	} else if (bits != 8) {
1225c8a9d043SErwan Le Ray 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1226c8a9d043SErwan Le Ray 			, bits);
12271deeda8dSIlpo Järvinen 		cflag &= ~CSIZE;
12281deeda8dSIlpo Järvinen 		cflag |= CS8;
12291deeda8dSIlpo Järvinen 		termios->c_cflag = cflag;
12301deeda8dSIlpo Järvinen 		bits = 8;
12311deeda8dSIlpo Järvinen 		if (cflag & PARENB) {
12321deeda8dSIlpo Järvinen 			bits++;
12331deeda8dSIlpo Järvinen 			cr1 |= USART_CR1_M0;
12341deeda8dSIlpo Järvinen 		}
12351deeda8dSIlpo Järvinen 	}
123648a6092fSMaxime Coquelin 
12374cc0ed62SErwan Le Ray 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
12382aa1bbb2SFabrice Gasnier 				       (stm32_port->fifoen &&
12392aa1bbb2SFabrice Gasnier 					stm32_port->rxftcfg >= 0))) {
12404cc0ed62SErwan Le Ray 		if (cflag & CSTOPB)
12414cc0ed62SErwan Le Ray 			bits = bits + 3; /* 1 start bit + 2 stop bits */
12424cc0ed62SErwan Le Ray 		else
12434cc0ed62SErwan Le Ray 			bits = bits + 2; /* 1 start bit + 1 stop bit */
12444cc0ed62SErwan Le Ray 
12454cc0ed62SErwan Le Ray 		/* RX timeout irq to occur after last stop bit + bits */
12464cc0ed62SErwan Le Ray 		stm32_port->cr1_irq = USART_CR1_RTOIE;
12474cc0ed62SErwan Le Ray 		writel_relaxed(bits, port->membase + ofs->rtor);
12484cc0ed62SErwan Le Ray 		cr2 |= USART_CR2_RTOEN;
124933bb2f6aSErwan Le Ray 		/*
125033bb2f6aSErwan Le Ray 		 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
125133bb2f6aSErwan Le Ray 		 * wake up over usart, from low power until the DMA gets re-enabled by resume.
125233bb2f6aSErwan Le Ray 		 */
1253d0a6a7bcSErwan Le Ray 		stm32_port->cr3_irq =  USART_CR3_RXFTIE;
12544cc0ed62SErwan Le Ray 	}
12554cc0ed62SErwan Le Ray 
1256d0a6a7bcSErwan Le Ray 	cr1 |= stm32_port->cr1_irq;
1257d0a6a7bcSErwan Le Ray 	cr3 |= stm32_port->cr3_irq;
1258d0a6a7bcSErwan Le Ray 
125948a6092fSMaxime Coquelin 	if (cflag & PARODD)
126048a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
126148a6092fSMaxime Coquelin 
126248a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
126348a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
126448a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
126535abe98fSBich HEMON 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
126648a6092fSMaxime Coquelin 	}
126748a6092fSMaxime Coquelin 
126848a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
126948a6092fSMaxime Coquelin 
127048a6092fSMaxime Coquelin 	/*
127148a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
127248a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
127348a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
127448a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
127548a6092fSMaxime Coquelin 	 */
127648a6092fSMaxime Coquelin 	if (usartdiv < 16) {
127748a6092fSMaxime Coquelin 		oversampling = 8;
12781bcda09dSBich HEMON 		cr1 |= USART_CR1_OVER8;
127956f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
128048a6092fSMaxime Coquelin 	} else {
128148a6092fSMaxime Coquelin 		oversampling = 16;
12821bcda09dSBich HEMON 		cr1 &= ~USART_CR1_OVER8;
128356f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
128448a6092fSMaxime Coquelin 	}
128548a6092fSMaxime Coquelin 
128648a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
128748a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
1288ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
128948a6092fSMaxime Coquelin 
129048a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
129148a6092fSMaxime Coquelin 
129248a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
129348a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
129448a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
129548a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
12964f01d833SErwan Le Ray 		port->read_status_mask |= USART_SR_FE;
129748a6092fSMaxime Coquelin 
129848a6092fSMaxime Coquelin 	/* Characters to ignore */
129948a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
130048a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
130148a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
130248a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
13034f01d833SErwan Le Ray 		port->ignore_status_mask |= USART_SR_FE;
130448a6092fSMaxime Coquelin 		/*
130548a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
130648a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
130748a6092fSMaxime Coquelin 		 */
130848a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
130948a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
131048a6092fSMaxime Coquelin 	}
131148a6092fSMaxime Coquelin 
131248a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
131348a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
131448a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
131548a6092fSMaxime Coquelin 
131633bb2f6aSErwan Le Ray 	if (stm32_port->rx_ch) {
131733bb2f6aSErwan Le Ray 		/*
131833bb2f6aSErwan Le Ray 		 * Setup DMA to collect only valid data and enable error irqs.
131933bb2f6aSErwan Le Ray 		 * This also enables break reception when using DMA.
132033bb2f6aSErwan Le Ray 		 */
132133bb2f6aSErwan Le Ray 		cr1 |= USART_CR1_PEIE;
132233bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_EIE;
132334891872SAlexandre TORGUE 		cr3 |= USART_CR3_DMAR;
132433bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_DDRE;
132533bb2f6aSErwan Le Ray 	}
132634891872SAlexandre TORGUE 
132700bc5e8fSValentin Caron 	if (stm32_port->tx_ch)
132800bc5e8fSValentin Caron 		cr3 |= USART_CR3_DMAT;
132900bc5e8fSValentin Caron 
13301bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
133156f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
13321bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
133356f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
133456f9a76cSErwan Le Ray 					     baud);
13351bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
13361bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
13371bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
13381bcda09dSBich HEMON 		} else {
13391bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
13401bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
13411bcda09dSBich HEMON 		}
13421bcda09dSBich HEMON 
13431bcda09dSBich HEMON 	} else {
13441bcda09dSBich HEMON 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
13451bcda09dSBich HEMON 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
13461bcda09dSBich HEMON 	}
13471bcda09dSBich HEMON 
134812761869SErwan Le Ray 	/* Configure wake up from low power on start bit detection */
13493d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
135012761869SErwan Le Ray 		cr3 &= ~USART_CR3_WUS_MASK;
135112761869SErwan Le Ray 		cr3 |= USART_CR3_WUS_START_BIT;
135212761869SErwan Le Ray 	}
135312761869SErwan Le Ray 
1354ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
1355ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
1356ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
135748a6092fSMaxime Coquelin 
135856f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
135948a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
1360436c9793SErwan Le Ray 
1361436c9793SErwan Le Ray 	/* Handle modem control interrupts */
1362436c9793SErwan Le Ray 	if (UART_ENABLE_MS(port, termios->c_cflag))
1363436c9793SErwan Le Ray 		stm32_usart_enable_ms(port);
1364436c9793SErwan Le Ray 	else
1365436c9793SErwan Le Ray 		stm32_usart_disable_ms(port);
136648a6092fSMaxime Coquelin }
136748a6092fSMaxime Coquelin 
stm32_usart_type(struct uart_port * port)136856f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port)
136948a6092fSMaxime Coquelin {
137048a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
137148a6092fSMaxime Coquelin }
137248a6092fSMaxime Coquelin 
stm32_usart_release_port(struct uart_port * port)137356f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port)
137448a6092fSMaxime Coquelin {
137548a6092fSMaxime Coquelin }
137648a6092fSMaxime Coquelin 
stm32_usart_request_port(struct uart_port * port)137756f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port)
137848a6092fSMaxime Coquelin {
137948a6092fSMaxime Coquelin 	return 0;
138048a6092fSMaxime Coquelin }
138148a6092fSMaxime Coquelin 
stm32_usart_config_port(struct uart_port * port,int flags)138256f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags)
138348a6092fSMaxime Coquelin {
138448a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
138548a6092fSMaxime Coquelin 		port->type = PORT_STM32;
138648a6092fSMaxime Coquelin }
138748a6092fSMaxime Coquelin 
138848a6092fSMaxime Coquelin static int
stm32_usart_verify_port(struct uart_port * port,struct serial_struct * ser)138956f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
139048a6092fSMaxime Coquelin {
139148a6092fSMaxime Coquelin 	/* No user changeable parameters */
139248a6092fSMaxime Coquelin 	return -EINVAL;
139348a6092fSMaxime Coquelin }
139448a6092fSMaxime Coquelin 
stm32_usart_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)139556f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state,
139648a6092fSMaxime Coquelin 			   unsigned int oldstate)
139748a6092fSMaxime Coquelin {
139848a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
139948a6092fSMaxime Coquelin 			struct stm32_port, port);
1400d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1401d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32port->info->cfg;
140218ee37e1SJohan Hovold 	unsigned long flags;
140348a6092fSMaxime Coquelin 
140448a6092fSMaxime Coquelin 	switch (state) {
140548a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
1406fb6dcef6SErwan Le Ray 		pm_runtime_get_sync(port->dev);
140748a6092fSMaxime Coquelin 		break;
140848a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
140948a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
141056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
141148a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
1412fb6dcef6SErwan Le Ray 		pm_runtime_put_sync(port->dev);
141348a6092fSMaxime Coquelin 		break;
141448a6092fSMaxime Coquelin 	}
141548a6092fSMaxime Coquelin }
141648a6092fSMaxime Coquelin 
14171f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL)
14181f507b3aSValentin Caron 
14191f507b3aSValentin Caron  /* Callbacks for characters polling in debug context (i.e. KGDB). */
stm32_usart_poll_init(struct uart_port * port)14201f507b3aSValentin Caron static int stm32_usart_poll_init(struct uart_port *port)
14211f507b3aSValentin Caron {
14221f507b3aSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
14231f507b3aSValentin Caron 
14241f507b3aSValentin Caron 	return clk_prepare_enable(stm32_port->clk);
14251f507b3aSValentin Caron }
14261f507b3aSValentin Caron 
stm32_usart_poll_get_char(struct uart_port * port)14271f507b3aSValentin Caron static int stm32_usart_poll_get_char(struct uart_port *port)
14281f507b3aSValentin Caron {
14291f507b3aSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
14301f507b3aSValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
14311f507b3aSValentin Caron 
14321f507b3aSValentin Caron 	if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE))
14331f507b3aSValentin Caron 		return NO_POLL_CHAR;
14341f507b3aSValentin Caron 
14351f507b3aSValentin Caron 	return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask;
14361f507b3aSValentin Caron }
14371f507b3aSValentin Caron 
stm32_usart_poll_put_char(struct uart_port * port,unsigned char ch)14381f507b3aSValentin Caron static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch)
14391f507b3aSValentin Caron {
14401f507b3aSValentin Caron 	stm32_usart_console_putchar(port, ch);
14411f507b3aSValentin Caron }
14421f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */
14431f507b3aSValentin Caron 
144448a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
144556f9a76cSErwan Le Ray 	.tx_empty	= stm32_usart_tx_empty,
144656f9a76cSErwan Le Ray 	.set_mctrl	= stm32_usart_set_mctrl,
144756f9a76cSErwan Le Ray 	.get_mctrl	= stm32_usart_get_mctrl,
144856f9a76cSErwan Le Ray 	.stop_tx	= stm32_usart_stop_tx,
144956f9a76cSErwan Le Ray 	.start_tx	= stm32_usart_start_tx,
145056f9a76cSErwan Le Ray 	.throttle	= stm32_usart_throttle,
145156f9a76cSErwan Le Ray 	.unthrottle	= stm32_usart_unthrottle,
145256f9a76cSErwan Le Ray 	.stop_rx	= stm32_usart_stop_rx,
145356f9a76cSErwan Le Ray 	.enable_ms	= stm32_usart_enable_ms,
145456f9a76cSErwan Le Ray 	.break_ctl	= stm32_usart_break_ctl,
145556f9a76cSErwan Le Ray 	.startup	= stm32_usart_startup,
145656f9a76cSErwan Le Ray 	.shutdown	= stm32_usart_shutdown,
14573d82be8bSErwan Le Ray 	.flush_buffer	= stm32_usart_flush_buffer,
145856f9a76cSErwan Le Ray 	.set_termios	= stm32_usart_set_termios,
145956f9a76cSErwan Le Ray 	.pm		= stm32_usart_pm,
146056f9a76cSErwan Le Ray 	.type		= stm32_usart_type,
146156f9a76cSErwan Le Ray 	.release_port	= stm32_usart_release_port,
146256f9a76cSErwan Le Ray 	.request_port	= stm32_usart_request_port,
146356f9a76cSErwan Le Ray 	.config_port	= stm32_usart_config_port,
146456f9a76cSErwan Le Ray 	.verify_port	= stm32_usart_verify_port,
14651f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL)
14661f507b3aSValentin Caron 	.poll_init      = stm32_usart_poll_init,
14671f507b3aSValentin Caron 	.poll_get_char	= stm32_usart_poll_get_char,
14681f507b3aSValentin Caron 	.poll_put_char	= stm32_usart_poll_put_char,
14691f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */
147048a6092fSMaxime Coquelin };
147148a6092fSMaxime Coquelin 
14722aa1bbb2SFabrice Gasnier /*
14732aa1bbb2SFabrice Gasnier  * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
14742aa1bbb2SFabrice Gasnier  * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
14752aa1bbb2SFabrice Gasnier  * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
14762aa1bbb2SFabrice Gasnier  * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
14772aa1bbb2SFabrice Gasnier  */
14782aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
14792aa1bbb2SFabrice Gasnier 
stm32_usart_get_ftcfg(struct platform_device * pdev,const char * p,int * ftcfg)14802aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
14812aa1bbb2SFabrice Gasnier 				  int *ftcfg)
14822aa1bbb2SFabrice Gasnier {
14832aa1bbb2SFabrice Gasnier 	u32 bytes, i;
14842aa1bbb2SFabrice Gasnier 
14852aa1bbb2SFabrice Gasnier 	/* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
14862aa1bbb2SFabrice Gasnier 	if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
14872aa1bbb2SFabrice Gasnier 		bytes = 8;
14882aa1bbb2SFabrice Gasnier 
14892aa1bbb2SFabrice Gasnier 	for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
14902aa1bbb2SFabrice Gasnier 		if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
14912aa1bbb2SFabrice Gasnier 			break;
14922aa1bbb2SFabrice Gasnier 	if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
14932aa1bbb2SFabrice Gasnier 		i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
14942aa1bbb2SFabrice Gasnier 
14952aa1bbb2SFabrice Gasnier 	dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
14962aa1bbb2SFabrice Gasnier 		stm32h7_usart_fifo_thresh_cfg[i]);
14972aa1bbb2SFabrice Gasnier 
14982aa1bbb2SFabrice Gasnier 	/* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
14992aa1bbb2SFabrice Gasnier 	if (i)
15002aa1bbb2SFabrice Gasnier 		*ftcfg = i - 1;
15012aa1bbb2SFabrice Gasnier 	else
15022aa1bbb2SFabrice Gasnier 		*ftcfg = -EINVAL;
15032aa1bbb2SFabrice Gasnier }
15042aa1bbb2SFabrice Gasnier 
stm32_usart_deinit_port(struct stm32_port * stm32port)150597f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port)
150697f3a085SErwan Le Ray {
150797f3a085SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
150897f3a085SErwan Le Ray }
150997f3a085SErwan Le Ray 
1510aeae8f22SIlpo Järvinen static const struct serial_rs485 stm32_rs485_supported = {
1511aeae8f22SIlpo Järvinen 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
1512aeae8f22SIlpo Järvinen 		 SER_RS485_RX_DURING_TX,
1513aeae8f22SIlpo Järvinen 	.delay_rts_before_send = 1,
1514aeae8f22SIlpo Järvinen 	.delay_rts_after_send = 1,
1515aeae8f22SIlpo Järvinen };
1516aeae8f22SIlpo Järvinen 
stm32_usart_init_port(struct stm32_port * stm32port,struct platform_device * pdev)151756f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port,
151848a6092fSMaxime Coquelin 				 struct platform_device *pdev)
151948a6092fSMaxime Coquelin {
152048a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
152148a6092fSMaxime Coquelin 	struct resource *res;
1522e0f2a902SErwan Le Ray 	int ret, irq;
152348a6092fSMaxime Coquelin 
1524e0f2a902SErwan Le Ray 	irq = platform_get_irq(pdev, 0);
1525217b04c6STang Bin 	if (irq < 0)
1526217b04c6STang Bin 		return irq;
152792fc0023SErwan Le Ray 
152848a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
152948a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
153048a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
153148a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
1532d075719eSErwan Le Ray 	port->fifosize	= stm32port->info->cfg.fifosize;
15339feedaa7SDmitry Safonov 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1534e0f2a902SErwan Le Ray 	port->irq = irq;
153556f9a76cSErwan Le Ray 	port->rs485_config = stm32_usart_config_rs485;
15360139da50SIlpo Järvinen 	port->rs485_supported = stm32_rs485_supported;
15377d8f6861SBich HEMON 
153856f9a76cSErwan Le Ray 	ret = stm32_usart_init_rs485(port, pdev);
1539c150c0f3SLukas Wunner 	if (ret)
1540c150c0f3SLukas Wunner 		return ret;
15417d8f6861SBich HEMON 
15423d530017SAlexandre Torgue 	stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
15433d530017SAlexandre Torgue 		of_property_read_bool(pdev->dev.of_node, "wakeup-source");
15442c58e560SErwan Le Ray 
15453cd66593SMartin Devera 	stm32port->swap = stm32port->info->cfg.has_swap &&
15463cd66593SMartin Devera 		of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
15473cd66593SMartin Devera 
1548351a762aSGerald Baeza 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
15492aa1bbb2SFabrice Gasnier 	if (stm32port->fifoen) {
15502aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "rx-threshold",
15512aa1bbb2SFabrice Gasnier 				      &stm32port->rxftcfg);
15522aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "tx-threshold",
15532aa1bbb2SFabrice Gasnier 				      &stm32port->txftcfg);
15542aa1bbb2SFabrice Gasnier 	}
155548a6092fSMaxime Coquelin 
15563d881e32STang Bin 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
155748a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
155848a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
155948a6092fSMaxime Coquelin 	port->mapbase = res->start;
156048a6092fSMaxime Coquelin 
156148a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
156248a6092fSMaxime Coquelin 
156348a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
156448a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
156548a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
156648a6092fSMaxime Coquelin 
156748a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
156848a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
156948a6092fSMaxime Coquelin 	if (ret)
157048a6092fSMaxime Coquelin 		return ret;
157148a6092fSMaxime Coquelin 
157248a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1573ada80043SFabrice Gasnier 	if (!stm32port->port.uartclk) {
157448a6092fSMaxime Coquelin 		ret = -EINVAL;
15756cf61b9bSManivannan Sadhasivam 		goto err_clk;
1576ada80043SFabrice Gasnier 	}
157748a6092fSMaxime Coquelin 
15786cf61b9bSManivannan Sadhasivam 	stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
15796cf61b9bSManivannan Sadhasivam 	if (IS_ERR(stm32port->gpios)) {
15806cf61b9bSManivannan Sadhasivam 		ret = PTR_ERR(stm32port->gpios);
15816cf61b9bSManivannan Sadhasivam 		goto err_clk;
15826cf61b9bSManivannan Sadhasivam 	}
15836cf61b9bSManivannan Sadhasivam 
15849359369aSErwan Le Ray 	/*
15859359369aSErwan Le Ray 	 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
15869359369aSErwan Le Ray 	 * properties should not be specified.
15879359369aSErwan Le Ray 	 */
15886cf61b9bSManivannan Sadhasivam 	if (stm32port->hw_flow_control) {
15896cf61b9bSManivannan Sadhasivam 		if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
15906cf61b9bSManivannan Sadhasivam 		    mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
15916cf61b9bSManivannan Sadhasivam 			dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
15926cf61b9bSManivannan Sadhasivam 			ret = -EINVAL;
15936cf61b9bSManivannan Sadhasivam 			goto err_clk;
15946cf61b9bSManivannan Sadhasivam 		}
15956cf61b9bSManivannan Sadhasivam 	}
15966cf61b9bSManivannan Sadhasivam 
15976cf61b9bSManivannan Sadhasivam 	return ret;
15986cf61b9bSManivannan Sadhasivam 
15996cf61b9bSManivannan Sadhasivam err_clk:
16006cf61b9bSManivannan Sadhasivam 	clk_disable_unprepare(stm32port->clk);
16016cf61b9bSManivannan Sadhasivam 
160248a6092fSMaxime Coquelin 	return ret;
160348a6092fSMaxime Coquelin }
160448a6092fSMaxime Coquelin 
stm32_usart_of_get_port(struct platform_device * pdev)160556f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
160648a6092fSMaxime Coquelin {
160748a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
160848a6092fSMaxime Coquelin 	int id;
160948a6092fSMaxime Coquelin 
161048a6092fSMaxime Coquelin 	if (!np)
161148a6092fSMaxime Coquelin 		return NULL;
161248a6092fSMaxime Coquelin 
161348a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
1614e5707915SGerald Baeza 	if (id < 0) {
1615e5707915SGerald Baeza 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1616e5707915SGerald Baeza 		return NULL;
1617e5707915SGerald Baeza 	}
161848a6092fSMaxime Coquelin 
161948a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
162048a6092fSMaxime Coquelin 		return NULL;
162148a6092fSMaxime Coquelin 
16226fd9fffbSErwan Le Ray 	stm32_ports[id].hw_flow_control =
16236fd9fffbSErwan Le Ray 		of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
16246fd9fffbSErwan Le Ray 		of_property_read_bool (np, "uart-has-rtscts");
162548a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
16264cc0ed62SErwan Le Ray 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1627d0a6a7bcSErwan Le Ray 	stm32_ports[id].cr3_irq = 0;
1628e5707915SGerald Baeza 	stm32_ports[id].last_res = RX_BUF_L;
162948a6092fSMaxime Coquelin 	return &stm32_ports[id];
163048a6092fSMaxime Coquelin }
163148a6092fSMaxime Coquelin 
163248a6092fSMaxime Coquelin #ifdef CONFIG_OF
163348a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
1634ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
1635ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1636270e5a74SFabrice Gasnier 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
163748a6092fSMaxime Coquelin 	{},
163848a6092fSMaxime Coquelin };
163948a6092fSMaxime Coquelin 
164048a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
164148a6092fSMaxime Coquelin #endif
164248a6092fSMaxime Coquelin 
stm32_usart_of_dma_rx_remove(struct stm32_port * stm32port,struct platform_device * pdev)1643a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1644a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1645a7770a4bSErwan Le Ray {
1646a7770a4bSErwan Le Ray 	if (stm32port->rx_buf)
1647a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1648a7770a4bSErwan Le Ray 				  stm32port->rx_dma_buf);
1649a7770a4bSErwan Le Ray }
1650a7770a4bSErwan Le Ray 
stm32_usart_of_dma_rx_probe(struct stm32_port * stm32port,struct platform_device * pdev)165156f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
165234891872SAlexandre TORGUE 				       struct platform_device *pdev)
165334891872SAlexandre TORGUE {
1654d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
165534891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
165634891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
165734891872SAlexandre TORGUE 	struct dma_slave_config config;
165834891872SAlexandre TORGUE 	int ret;
165934891872SAlexandre TORGUE 
166059bd4eedSTang Bin 	stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
166134891872SAlexandre TORGUE 					       &stm32port->rx_dma_buf,
166234891872SAlexandre TORGUE 					       GFP_KERNEL);
1663a7770a4bSErwan Le Ray 	if (!stm32port->rx_buf)
1664a7770a4bSErwan Le Ray 		return -ENOMEM;
166534891872SAlexandre TORGUE 
166634891872SAlexandre TORGUE 	/* Configure DMA channel */
166734891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
16688e5481d9SArnd Bergmann 	config.src_addr = port->mapbase + ofs->rdr;
166934891872SAlexandre TORGUE 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
167034891872SAlexandre TORGUE 
167134891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
167234891872SAlexandre TORGUE 	if (ret < 0) {
167334891872SAlexandre TORGUE 		dev_err(dev, "rx dma channel config failed\n");
1674a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
1675a7770a4bSErwan Le Ray 		return ret;
167634891872SAlexandre TORGUE 	}
167734891872SAlexandre TORGUE 
167834891872SAlexandre TORGUE 	return 0;
1679a7770a4bSErwan Le Ray }
168034891872SAlexandre TORGUE 
stm32_usart_of_dma_tx_remove(struct stm32_port * stm32port,struct platform_device * pdev)1681a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1682a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1683a7770a4bSErwan Le Ray {
1684a7770a4bSErwan Le Ray 	if (stm32port->tx_buf)
1685a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1686a7770a4bSErwan Le Ray 				  stm32port->tx_dma_buf);
168734891872SAlexandre TORGUE }
168834891872SAlexandre TORGUE 
stm32_usart_of_dma_tx_probe(struct stm32_port * stm32port,struct platform_device * pdev)168956f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
169034891872SAlexandre TORGUE 				       struct platform_device *pdev)
169134891872SAlexandre TORGUE {
1692d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
169334891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
169434891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
169534891872SAlexandre TORGUE 	struct dma_slave_config config;
169634891872SAlexandre TORGUE 	int ret;
169734891872SAlexandre TORGUE 
169859bd4eedSTang Bin 	stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
169934891872SAlexandre TORGUE 					       &stm32port->tx_dma_buf,
170034891872SAlexandre TORGUE 					       GFP_KERNEL);
1701a7770a4bSErwan Le Ray 	if (!stm32port->tx_buf)
1702a7770a4bSErwan Le Ray 		return -ENOMEM;
170334891872SAlexandre TORGUE 
170434891872SAlexandre TORGUE 	/* Configure DMA channel */
170534891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
17068e5481d9SArnd Bergmann 	config.dst_addr = port->mapbase + ofs->tdr;
170734891872SAlexandre TORGUE 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
170834891872SAlexandre TORGUE 
170934891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
171034891872SAlexandre TORGUE 	if (ret < 0) {
171134891872SAlexandre TORGUE 		dev_err(dev, "tx dma channel config failed\n");
1712a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1713a7770a4bSErwan Le Ray 		return ret;
171434891872SAlexandre TORGUE 	}
171534891872SAlexandre TORGUE 
171634891872SAlexandre TORGUE 	return 0;
171734891872SAlexandre TORGUE }
171834891872SAlexandre TORGUE 
stm32_usart_serial_probe(struct platform_device * pdev)171956f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev)
172048a6092fSMaxime Coquelin {
172148a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
1722ada8618fSAlexandre TORGUE 	int ret;
172348a6092fSMaxime Coquelin 
172456f9a76cSErwan Le Ray 	stm32port = stm32_usart_of_get_port(pdev);
172548a6092fSMaxime Coquelin 	if (!stm32port)
172648a6092fSMaxime Coquelin 		return -ENODEV;
172748a6092fSMaxime Coquelin 
1728d825f0beSStephen Boyd 	stm32port->info = of_device_get_match_data(&pdev->dev);
1729d825f0beSStephen Boyd 	if (!stm32port->info)
1730ada8618fSAlexandre TORGUE 		return -EINVAL;
1731ada8618fSAlexandre TORGUE 
1732a7770a4bSErwan Le Ray 	stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
17330d114e9fSValentin Caron 	if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER)
17340d114e9fSValentin Caron 		return -EPROBE_DEFER;
17350d114e9fSValentin Caron 
1736a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1737a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->rx_ch))
1738a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
173934891872SAlexandre TORGUE 
1740a7770a4bSErwan Le Ray 	stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1741a7770a4bSErwan Le Ray 	if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1742a7770a4bSErwan Le Ray 		ret = -EPROBE_DEFER;
1743a7770a4bSErwan Le Ray 		goto err_dma_rx;
1744a7770a4bSErwan Le Ray 	}
1745a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1746a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->tx_ch))
1747a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1748a7770a4bSErwan Le Ray 
17490d114e9fSValentin Caron 	ret = stm32_usart_init_port(stm32port, pdev);
17500d114e9fSValentin Caron 	if (ret)
17510d114e9fSValentin Caron 		goto err_dma_tx;
17520d114e9fSValentin Caron 
17530d114e9fSValentin Caron 	if (stm32port->wakeup_src) {
17540d114e9fSValentin Caron 		device_set_wakeup_capable(&pdev->dev, true);
17550d114e9fSValentin Caron 		ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
17560d114e9fSValentin Caron 		if (ret)
17570d114e9fSValentin Caron 			goto err_deinit_port;
17580d114e9fSValentin Caron 	}
17590d114e9fSValentin Caron 
1760a7770a4bSErwan Le Ray 	if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1761a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1762a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->rx_ch);
1763a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
1764a7770a4bSErwan Le Ray 	}
1765a7770a4bSErwan Le Ray 
1766a7770a4bSErwan Le Ray 	if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1767a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1768a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->tx_ch);
1769a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1770a7770a4bSErwan Le Ray 	}
1771a7770a4bSErwan Le Ray 
1772a7770a4bSErwan Le Ray 	if (!stm32port->rx_ch)
1773a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1774a7770a4bSErwan Le Ray 	if (!stm32port->tx_ch)
1775a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
177634891872SAlexandre TORGUE 
177748a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
177848a6092fSMaxime Coquelin 
1779fb6dcef6SErwan Le Ray 	pm_runtime_get_noresume(&pdev->dev);
1780fb6dcef6SErwan Le Ray 	pm_runtime_set_active(&pdev->dev);
1781fb6dcef6SErwan Le Ray 	pm_runtime_enable(&pdev->dev);
178287fd0741SErwan Le Ray 
178387fd0741SErwan Le Ray 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
178487fd0741SErwan Le Ray 	if (ret)
178587fd0741SErwan Le Ray 		goto err_port;
178687fd0741SErwan Le Ray 
1787fb6dcef6SErwan Le Ray 	pm_runtime_put_sync(&pdev->dev);
1788fb6dcef6SErwan Le Ray 
178948a6092fSMaxime Coquelin 	return 0;
1790ada80043SFabrice Gasnier 
179187fd0741SErwan Le Ray err_port:
179287fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
179387fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
179487fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
179587fd0741SErwan Le Ray 
17960d114e9fSValentin Caron 	if (stm32port->tx_ch)
1797a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1798a7770a4bSErwan Le Ray 	if (stm32port->rx_ch)
1799a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
180087fd0741SErwan Le Ray 
18013d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
18025297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
18035297f274SErwan Le Ray 
1804a7770a4bSErwan Le Ray err_deinit_port:
18053d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
18063d530017SAlexandre Torgue 		device_set_wakeup_capable(&pdev->dev, false);
1807270e5a74SFabrice Gasnier 
180897f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32port);
1809ada80043SFabrice Gasnier 
18100d114e9fSValentin Caron err_dma_tx:
18110d114e9fSValentin Caron 	if (stm32port->tx_ch)
18120d114e9fSValentin Caron 		dma_release_channel(stm32port->tx_ch);
18130d114e9fSValentin Caron 
18140d114e9fSValentin Caron err_dma_rx:
18150d114e9fSValentin Caron 	if (stm32port->rx_ch)
18160d114e9fSValentin Caron 		dma_release_channel(stm32port->rx_ch);
18170d114e9fSValentin Caron 
1818ada80043SFabrice Gasnier 	return ret;
181948a6092fSMaxime Coquelin }
182048a6092fSMaxime Coquelin 
stm32_usart_serial_remove(struct platform_device * pdev)182156f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev)
182248a6092fSMaxime Coquelin {
182348a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
1824511c7b1bSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1825d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
182633bb2f6aSErwan Le Ray 	u32 cr3;
1827fb6dcef6SErwan Le Ray 
1828fb6dcef6SErwan Le Ray 	pm_runtime_get_sync(&pdev->dev);
18296bd6cd29SUwe Kleine-König 	uart_remove_one_port(&stm32_usart_driver, port);
183087fd0741SErwan Le Ray 
183187fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
183287fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
183387fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
183434891872SAlexandre TORGUE 
183533bb2f6aSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
183634891872SAlexandre TORGUE 
183787fd0741SErwan Le Ray 	if (stm32_port->tx_ch) {
1838a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32_port, pdev);
183934891872SAlexandre TORGUE 		dma_release_channel(stm32_port->tx_ch);
184087fd0741SErwan Le Ray 	}
184134891872SAlexandre TORGUE 
1842a7770a4bSErwan Le Ray 	if (stm32_port->rx_ch) {
1843a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1844a7770a4bSErwan Le Ray 		dma_release_channel(stm32_port->rx_ch);
1845a7770a4bSErwan Le Ray 	}
1846a7770a4bSErwan Le Ray 
1847a01ae50dSValentin Caron 	cr3 = readl_relaxed(port->membase + ofs->cr3);
1848a01ae50dSValentin Caron 	cr3 &= ~USART_CR3_EIE;
1849a01ae50dSValentin Caron 	cr3 &= ~USART_CR3_DMAR;
1850a01ae50dSValentin Caron 	cr3 &= ~USART_CR3_DMAT;
1851a01ae50dSValentin Caron 	cr3 &= ~USART_CR3_DDRE;
1852a01ae50dSValentin Caron 	writel_relaxed(cr3, port->membase + ofs->cr3);
1853511c7b1bSAlexandre TORGUE 
18543d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
18555297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
1856270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
18575297f274SErwan Le Ray 	}
1858270e5a74SFabrice Gasnier 
185997f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32_port);
186048a6092fSMaxime Coquelin 
186187fd0741SErwan Le Ray 	return 0;
186248a6092fSMaxime Coquelin }
186348a6092fSMaxime Coquelin 
stm32_usart_console_putchar(struct uart_port * port,unsigned char ch)18641f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
186548a6092fSMaxime Coquelin {
1866ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1867d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
186828fb1a92SValentin Caron 	u32 isr;
186928fb1a92SValentin Caron 	int ret;
1870ada8618fSAlexandre TORGUE 
187128fb1a92SValentin Caron 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr,
187228fb1a92SValentin Caron 						(isr & USART_SR_TXE), 100,
187328fb1a92SValentin Caron 						STM32_USART_TIMEOUT_USEC);
187428fb1a92SValentin Caron 	if (ret != 0) {
187528fb1a92SValentin Caron 		dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret);
187628fb1a92SValentin Caron 		return;
187728fb1a92SValentin Caron 	}
1878ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
187948a6092fSMaxime Coquelin }
188048a6092fSMaxime Coquelin 
18811f507b3aSValentin Caron #ifdef CONFIG_SERIAL_STM32_CONSOLE
stm32_usart_console_write(struct console * co,const char * s,unsigned int cnt)188256f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s,
188392fc0023SErwan Le Ray 				      unsigned int cnt)
188448a6092fSMaxime Coquelin {
188548a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
1886ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1887d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1888d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
188948a6092fSMaxime Coquelin 	unsigned long flags;
189048a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
189148a6092fSMaxime Coquelin 	int locked = 1;
189248a6092fSMaxime Coquelin 
1893cea37afdSJohan Hovold 	if (oops_in_progress)
1894cea37afdSJohan Hovold 		locked = spin_trylock_irqsave(&port->lock, flags);
189548a6092fSMaxime Coquelin 	else
1896cea37afdSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
189748a6092fSMaxime Coquelin 
189887f1f809SAlexandre TORGUE 	/* Save and disable interrupts, enable the transmitter */
1899ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
190048a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
190187f1f809SAlexandre TORGUE 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1902ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
190348a6092fSMaxime Coquelin 
190456f9a76cSErwan Le Ray 	uart_console_write(port, s, cnt, stm32_usart_console_putchar);
190548a6092fSMaxime Coquelin 
190648a6092fSMaxime Coquelin 	/* Restore interrupt state */
1907ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
190848a6092fSMaxime Coquelin 
190948a6092fSMaxime Coquelin 	if (locked)
1910cea37afdSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
191148a6092fSMaxime Coquelin }
191248a6092fSMaxime Coquelin 
stm32_usart_console_setup(struct console * co,char * options)191356f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options)
191448a6092fSMaxime Coquelin {
191548a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
191648a6092fSMaxime Coquelin 	int baud = 9600;
191748a6092fSMaxime Coquelin 	int bits = 8;
191848a6092fSMaxime Coquelin 	int parity = 'n';
191948a6092fSMaxime Coquelin 	int flow = 'n';
192048a6092fSMaxime Coquelin 
192148a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
192248a6092fSMaxime Coquelin 		return -ENODEV;
192348a6092fSMaxime Coquelin 
192448a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
192548a6092fSMaxime Coquelin 
192648a6092fSMaxime Coquelin 	/*
192748a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
192848a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
192948a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
193048a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
193148a6092fSMaxime Coquelin 	 */
193292fc0023SErwan Le Ray 	if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
193348a6092fSMaxime Coquelin 		return -ENXIO;
193448a6092fSMaxime Coquelin 
193548a6092fSMaxime Coquelin 	if (options)
193648a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
193748a6092fSMaxime Coquelin 
193848a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
193948a6092fSMaxime Coquelin }
194048a6092fSMaxime Coquelin 
194148a6092fSMaxime Coquelin static struct console stm32_console = {
194248a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
194348a6092fSMaxime Coquelin 	.device		= uart_console_device,
194456f9a76cSErwan Le Ray 	.write		= stm32_usart_console_write,
194556f9a76cSErwan Le Ray 	.setup		= stm32_usart_console_setup,
194648a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
194748a6092fSMaxime Coquelin 	.index		= -1,
194848a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
194948a6092fSMaxime Coquelin };
195048a6092fSMaxime Coquelin 
195148a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
195248a6092fSMaxime Coquelin 
195348a6092fSMaxime Coquelin #else
195448a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
195548a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
195648a6092fSMaxime Coquelin 
19578043b16fSValentin Caron #ifdef CONFIG_SERIAL_EARLYCON
early_stm32_usart_console_putchar(struct uart_port * port,unsigned char ch)19588043b16fSValentin Caron static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
19598043b16fSValentin Caron {
19608043b16fSValentin Caron 	struct stm32_usart_info *info = port->private_data;
19618043b16fSValentin Caron 
19628043b16fSValentin Caron 	while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE))
19638043b16fSValentin Caron 		cpu_relax();
19648043b16fSValentin Caron 
19658043b16fSValentin Caron 	writel_relaxed(ch, port->membase + info->ofs.tdr);
19668043b16fSValentin Caron }
19678043b16fSValentin Caron 
early_stm32_serial_write(struct console * console,const char * s,unsigned int count)19688043b16fSValentin Caron static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count)
19698043b16fSValentin Caron {
19708043b16fSValentin Caron 	struct earlycon_device *device = console->data;
19718043b16fSValentin Caron 	struct uart_port *port = &device->port;
19728043b16fSValentin Caron 
19738043b16fSValentin Caron 	uart_console_write(port, s, count, early_stm32_usart_console_putchar);
19748043b16fSValentin Caron }
19758043b16fSValentin Caron 
early_stm32_h7_serial_setup(struct earlycon_device * device,const char * options)19768043b16fSValentin Caron static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
19778043b16fSValentin Caron {
19788043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
19798043b16fSValentin Caron 		return -ENODEV;
19808043b16fSValentin Caron 	device->port.private_data = &stm32h7_info;
19818043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
19828043b16fSValentin Caron 	return 0;
19838043b16fSValentin Caron }
19848043b16fSValentin Caron 
early_stm32_f7_serial_setup(struct earlycon_device * device,const char * options)19858043b16fSValentin Caron static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
19868043b16fSValentin Caron {
19878043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
19888043b16fSValentin Caron 		return -ENODEV;
19898043b16fSValentin Caron 	device->port.private_data = &stm32f7_info;
19908043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
19918043b16fSValentin Caron 	return 0;
19928043b16fSValentin Caron }
19938043b16fSValentin Caron 
early_stm32_f4_serial_setup(struct earlycon_device * device,const char * options)19948043b16fSValentin Caron static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
19958043b16fSValentin Caron {
19968043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
19978043b16fSValentin Caron 		return -ENODEV;
19988043b16fSValentin Caron 	device->port.private_data = &stm32f4_info;
19998043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
20008043b16fSValentin Caron 	return 0;
20018043b16fSValentin Caron }
20028043b16fSValentin Caron 
20038043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup);
20048043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup);
20058043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup);
20068043b16fSValentin Caron #endif /* CONFIG_SERIAL_EARLYCON */
20078043b16fSValentin Caron 
200848a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
200948a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
201048a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
201148a6092fSMaxime Coquelin 	.major		= 0,
201248a6092fSMaxime Coquelin 	.minor		= 0,
201348a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
201448a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
201548a6092fSMaxime Coquelin };
201648a6092fSMaxime Coquelin 
stm32_usart_serial_en_wakeup(struct uart_port * port,bool enable)20176eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
2018fe94347dSErwan Le Ray 						       bool enable)
2019270e5a74SFabrice Gasnier {
2020270e5a74SFabrice Gasnier 	struct stm32_port *stm32_port = to_stm32_port(port);
2021d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
20226eeb348cSErwan Le Ray 	struct tty_port *tport = &port->state->port;
20236eeb348cSErwan Le Ray 	int ret;
2024a01ae50dSValentin Caron 	unsigned int size = 0;
20256333a485SErwan Le Ray 	unsigned long flags;
2026270e5a74SFabrice Gasnier 
20276eeb348cSErwan Le Ray 	if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
20286eeb348cSErwan Le Ray 		return 0;
2029270e5a74SFabrice Gasnier 
203012761869SErwan Le Ray 	/*
203112761869SErwan Le Ray 	 * Enable low-power wake-up and wake-up irq if argument is set to
203212761869SErwan Le Ray 	 * "enable", disable low-power wake-up and wake-up irq otherwise
203312761869SErwan Le Ray 	 */
2034270e5a74SFabrice Gasnier 	if (enable) {
203556f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
203612761869SErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
20377547d9abSErwan Le Ray 		mctrl_gpio_enable_irq_wake(stm32_port->gpios);
20386eeb348cSErwan Le Ray 
20396eeb348cSErwan Le Ray 		/*
20406eeb348cSErwan Le Ray 		 * When DMA is used for reception, it must be disabled before
20416eeb348cSErwan Le Ray 		 * entering low-power mode and re-enabled when exiting from
20426eeb348cSErwan Le Ray 		 * low-power mode.
20436eeb348cSErwan Le Ray 		 */
20446eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
20456333a485SErwan Le Ray 			spin_lock_irqsave(&port->lock, flags);
20466333a485SErwan Le Ray 			/* Poll data from DMA RX buffer if any */
2047a01ae50dSValentin Caron 			if (!stm32_usart_rx_dma_pause(stm32_port))
2048a01ae50dSValentin Caron 				size += stm32_usart_receive_chars(port, true);
20497f28bceaSValentin Caron 			stm32_usart_rx_dma_terminate(stm32_port);
20506333a485SErwan Le Ray 			uart_unlock_and_check_sysrq_irqrestore(port, flags);
20516333a485SErwan Le Ray 			if (size)
20526333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
20536eeb348cSErwan Le Ray 		}
20546eeb348cSErwan Le Ray 
20556eeb348cSErwan Le Ray 		/* Poll data from RX FIFO if any */
20566eeb348cSErwan Le Ray 		stm32_usart_receive_chars(port, false);
2057270e5a74SFabrice Gasnier 	} else {
20586eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
2059a01ae50dSValentin Caron 			ret = stm32_usart_rx_dma_start_or_resume(port);
20606eeb348cSErwan Le Ray 			if (ret)
20616eeb348cSErwan Le Ray 				return ret;
20626eeb348cSErwan Le Ray 		}
20637547d9abSErwan Le Ray 		mctrl_gpio_disable_irq_wake(stm32_port->gpios);
206456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
206512761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
2066270e5a74SFabrice Gasnier 	}
20676eeb348cSErwan Le Ray 
20686eeb348cSErwan Le Ray 	return 0;
2069270e5a74SFabrice Gasnier }
2070270e5a74SFabrice Gasnier 
stm32_usart_serial_suspend(struct device * dev)207156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
2072270e5a74SFabrice Gasnier {
2073270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
20746eeb348cSErwan Le Ray 	int ret;
2075270e5a74SFabrice Gasnier 
2076270e5a74SFabrice Gasnier 	uart_suspend_port(&stm32_usart_driver, port);
2077270e5a74SFabrice Gasnier 
20786eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
20796eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, true);
20806eeb348cSErwan Le Ray 		if (ret)
20816eeb348cSErwan Le Ray 			return ret;
20826eeb348cSErwan Le Ray 	}
2083270e5a74SFabrice Gasnier 
208455484fccSErwan Le Ray 	/*
208555484fccSErwan Le Ray 	 * When "no_console_suspend" is enabled, keep the pinctrl default state
208655484fccSErwan Le Ray 	 * and rely on bootloader stage to restore this state upon resume.
208755484fccSErwan Le Ray 	 * Otherwise, apply the idle or sleep states depending on wakeup
208855484fccSErwan Le Ray 	 * capabilities.
208955484fccSErwan Le Ray 	 */
209055484fccSErwan Le Ray 	if (console_suspend_enabled || !uart_console(port)) {
20911631eeeaSErwan Le Ray 		if (device_may_wakeup(dev) || device_wakeup_path(dev))
209255484fccSErwan Le Ray 			pinctrl_pm_select_idle_state(dev);
209355484fccSErwan Le Ray 		else
209494616d9aSErwan Le Ray 			pinctrl_pm_select_sleep_state(dev);
209555484fccSErwan Le Ray 	}
209694616d9aSErwan Le Ray 
2097270e5a74SFabrice Gasnier 	return 0;
2098270e5a74SFabrice Gasnier }
2099270e5a74SFabrice Gasnier 
stm32_usart_serial_resume(struct device * dev)210056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
2101270e5a74SFabrice Gasnier {
2102270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
21036eeb348cSErwan Le Ray 	int ret;
2104270e5a74SFabrice Gasnier 
210594616d9aSErwan Le Ray 	pinctrl_pm_select_default_state(dev);
210694616d9aSErwan Le Ray 
21076eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
21086eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, false);
21096eeb348cSErwan Le Ray 		if (ret)
21106eeb348cSErwan Le Ray 			return ret;
21116eeb348cSErwan Le Ray 	}
2112270e5a74SFabrice Gasnier 
2113270e5a74SFabrice Gasnier 	return uart_resume_port(&stm32_usart_driver, port);
2114270e5a74SFabrice Gasnier }
2115270e5a74SFabrice Gasnier 
stm32_usart_runtime_suspend(struct device * dev)211656f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
2117fb6dcef6SErwan Le Ray {
2118fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
2119fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
2120fb6dcef6SErwan Le Ray 			struct stm32_port, port);
2121fb6dcef6SErwan Le Ray 
2122fb6dcef6SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
2123fb6dcef6SErwan Le Ray 
2124fb6dcef6SErwan Le Ray 	return 0;
2125fb6dcef6SErwan Le Ray }
2126fb6dcef6SErwan Le Ray 
stm32_usart_runtime_resume(struct device * dev)212756f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
2128fb6dcef6SErwan Le Ray {
2129fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
2130fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
2131fb6dcef6SErwan Le Ray 			struct stm32_port, port);
2132fb6dcef6SErwan Le Ray 
2133fb6dcef6SErwan Le Ray 	return clk_prepare_enable(stm32port->clk);
2134fb6dcef6SErwan Le Ray }
2135fb6dcef6SErwan Le Ray 
2136270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = {
213756f9a76cSErwan Le Ray 	SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
213856f9a76cSErwan Le Ray 			   stm32_usart_runtime_resume, NULL)
213956f9a76cSErwan Le Ray 	SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
214056f9a76cSErwan Le Ray 				stm32_usart_serial_resume)
2141270e5a74SFabrice Gasnier };
2142270e5a74SFabrice Gasnier 
214348a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
214456f9a76cSErwan Le Ray 	.probe		= stm32_usart_serial_probe,
214556f9a76cSErwan Le Ray 	.remove		= stm32_usart_serial_remove,
214648a6092fSMaxime Coquelin 	.driver	= {
214748a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
2148270e5a74SFabrice Gasnier 		.pm	= &stm32_serial_pm_ops,
214948a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
215048a6092fSMaxime Coquelin 	},
215148a6092fSMaxime Coquelin };
215248a6092fSMaxime Coquelin 
stm32_usart_init(void)215356f9a76cSErwan Le Ray static int __init stm32_usart_init(void)
215448a6092fSMaxime Coquelin {
215548a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
215648a6092fSMaxime Coquelin 	int ret;
215748a6092fSMaxime Coquelin 
215848a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
215948a6092fSMaxime Coquelin 
216048a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
216148a6092fSMaxime Coquelin 	if (ret)
216248a6092fSMaxime Coquelin 		return ret;
216348a6092fSMaxime Coquelin 
216448a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
216548a6092fSMaxime Coquelin 	if (ret)
216648a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
216748a6092fSMaxime Coquelin 
216848a6092fSMaxime Coquelin 	return ret;
216948a6092fSMaxime Coquelin }
217048a6092fSMaxime Coquelin 
stm32_usart_exit(void)217156f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void)
217248a6092fSMaxime Coquelin {
217348a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
217448a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
217548a6092fSMaxime Coquelin }
217648a6092fSMaxime Coquelin 
217756f9a76cSErwan Le Ray module_init(stm32_usart_init);
217856f9a76cSErwan Le Ray module_exit(stm32_usart_exit);
217948a6092fSMaxime Coquelin 
218048a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
218148a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
218248a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
2183