xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 3f8bab174cb26aa5a8053c4457cc733881e3ad88)
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 
3856f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port);
3956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port);
4048a6092fSMaxime Coquelin 
4148a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
4248a6092fSMaxime Coquelin {
4348a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
4448a6092fSMaxime Coquelin }
4548a6092fSMaxime Coquelin 
4656f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
4748a6092fSMaxime Coquelin {
4848a6092fSMaxime Coquelin 	u32 val;
4948a6092fSMaxime Coquelin 
5048a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
5148a6092fSMaxime Coquelin 	val |= bits;
5248a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
5348a6092fSMaxime Coquelin }
5448a6092fSMaxime Coquelin 
5556f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
5648a6092fSMaxime Coquelin {
5748a6092fSMaxime Coquelin 	u32 val;
5848a6092fSMaxime Coquelin 
5948a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
6048a6092fSMaxime Coquelin 	val &= ~bits;
6148a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
6248a6092fSMaxime Coquelin }
6348a6092fSMaxime Coquelin 
6456f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
651bcda09dSBich HEMON 					 u32 delay_DDE, u32 baud)
661bcda09dSBich HEMON {
671bcda09dSBich HEMON 	u32 rs485_deat_dedt;
681bcda09dSBich HEMON 	u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
691bcda09dSBich HEMON 	bool over8;
701bcda09dSBich HEMON 
711bcda09dSBich HEMON 	*cr3 |= USART_CR3_DEM;
721bcda09dSBich HEMON 	over8 = *cr1 & USART_CR1_OVER8;
731bcda09dSBich HEMON 
741bcda09dSBich HEMON 	if (over8)
751bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 8;
761bcda09dSBich HEMON 	else
771bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 16;
781bcda09dSBich HEMON 
791bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
801bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
811bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
821bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
831bcda09dSBich HEMON 			   USART_CR1_DEAT_MASK;
841bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
851bcda09dSBich HEMON 
861bcda09dSBich HEMON 	if (over8)
871bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 8;
881bcda09dSBich HEMON 	else
891bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 16;
901bcda09dSBich HEMON 
911bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
921bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
931bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
941bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
951bcda09dSBich HEMON 			   USART_CR1_DEDT_MASK;
961bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
971bcda09dSBich HEMON }
981bcda09dSBich HEMON 
9956f9a76cSErwan Le Ray static int stm32_usart_config_rs485(struct uart_port *port,
1001bcda09dSBich HEMON 				    struct serial_rs485 *rs485conf)
1011bcda09dSBich HEMON {
1021bcda09dSBich HEMON 	struct stm32_port *stm32_port = to_stm32_port(port);
103d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
104d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1051bcda09dSBich HEMON 	u32 usartdiv, baud, cr1, cr3;
1061bcda09dSBich HEMON 	bool over8;
1071bcda09dSBich HEMON 
10856f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1091bcda09dSBich HEMON 
1101bcda09dSBich HEMON 	port->rs485 = *rs485conf;
1111bcda09dSBich HEMON 
1121bcda09dSBich HEMON 	rs485conf->flags |= SER_RS485_RX_DURING_TX;
1131bcda09dSBich HEMON 
1141bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
1151bcda09dSBich HEMON 		cr1 = readl_relaxed(port->membase + ofs->cr1);
1161bcda09dSBich HEMON 		cr3 = readl_relaxed(port->membase + ofs->cr3);
1171bcda09dSBich HEMON 		usartdiv = readl_relaxed(port->membase + ofs->brr);
1181bcda09dSBich HEMON 		usartdiv = usartdiv & GENMASK(15, 0);
1191bcda09dSBich HEMON 		over8 = cr1 & USART_CR1_OVER8;
1201bcda09dSBich HEMON 
1211bcda09dSBich HEMON 		if (over8)
1221bcda09dSBich HEMON 			usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
1231bcda09dSBich HEMON 				   << USART_BRR_04_R_SHIFT;
1241bcda09dSBich HEMON 
1251bcda09dSBich HEMON 		baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
12656f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
1271bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
12856f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
12956f9a76cSErwan Le Ray 					     baud);
1301bcda09dSBich HEMON 
1311bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
1321bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
1331bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1341bcda09dSBich HEMON 		} else {
1351bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
1361bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1371bcda09dSBich HEMON 		}
1381bcda09dSBich HEMON 
1391bcda09dSBich HEMON 		writel_relaxed(cr3, port->membase + ofs->cr3);
1401bcda09dSBich HEMON 		writel_relaxed(cr1, port->membase + ofs->cr1);
1411bcda09dSBich HEMON 	} else {
14256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3,
14356f9a76cSErwan Le Ray 				     USART_CR3_DEM | USART_CR3_DEP);
14456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1,
1451bcda09dSBich HEMON 				     USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1461bcda09dSBich HEMON 	}
1471bcda09dSBich HEMON 
14856f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1491bcda09dSBich HEMON 
1501bcda09dSBich HEMON 	return 0;
1511bcda09dSBich HEMON }
1521bcda09dSBich HEMON 
15356f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port,
1541bcda09dSBich HEMON 				  struct platform_device *pdev)
1551bcda09dSBich HEMON {
1561bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
1571bcda09dSBich HEMON 
1581bcda09dSBich HEMON 	rs485conf->flags = 0;
1591bcda09dSBich HEMON 	rs485conf->delay_rts_before_send = 0;
1601bcda09dSBich HEMON 	rs485conf->delay_rts_after_send = 0;
1611bcda09dSBich HEMON 
1621bcda09dSBich HEMON 	if (!pdev->dev.of_node)
1631bcda09dSBich HEMON 		return -ENODEV;
1641bcda09dSBich HEMON 
165c150c0f3SLukas Wunner 	return uart_get_rs485_mode(port);
1661bcda09dSBich HEMON }
1671bcda09dSBich HEMON 
16833bb2f6aSErwan Le Ray static bool stm32_usart_rx_dma_enabled(struct uart_port *port)
16934891872SAlexandre TORGUE {
17034891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
171d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
17233bb2f6aSErwan Le Ray 
17333bb2f6aSErwan Le Ray 	if (!stm32_port->rx_ch)
17433bb2f6aSErwan Le Ray 		return false;
17533bb2f6aSErwan Le Ray 
17633bb2f6aSErwan Le Ray 	return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR);
17733bb2f6aSErwan Le Ray }
17833bb2f6aSErwan Le Ray 
17933bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */
18033bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
18133bb2f6aSErwan Le Ray {
18233bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
18333bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
18434891872SAlexandre TORGUE 
18534891872SAlexandre TORGUE 	*sr = readl_relaxed(port->membase + ofs->isr);
18633bb2f6aSErwan Le Ray 	/* Get pending characters in RDR or FIFO */
18733bb2f6aSErwan Le Ray 	if (*sr & USART_SR_RXNE) {
18833bb2f6aSErwan Le Ray 		/* Get all pending characters from the RDR or the FIFO when using interrupts */
18933bb2f6aSErwan Le Ray 		if (!stm32_usart_rx_dma_enabled(port))
19033bb2f6aSErwan Le Ray 			return true;
19134891872SAlexandre TORGUE 
19233bb2f6aSErwan Le Ray 		/* Handle only RX data errors when using DMA */
19333bb2f6aSErwan Le Ray 		if (*sr & USART_SR_ERR_MASK)
19433bb2f6aSErwan Le Ray 			return true;
19534891872SAlexandre TORGUE 	}
19634891872SAlexandre TORGUE 
19733bb2f6aSErwan Le Ray 	return false;
19833bb2f6aSErwan Le Ray }
19933bb2f6aSErwan Le Ray 
20033bb2f6aSErwan Le Ray static unsigned long stm32_usart_get_char_pio(struct uart_port *port)
20134891872SAlexandre TORGUE {
20234891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
203d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
20434891872SAlexandre TORGUE 	unsigned long c;
20534891872SAlexandre TORGUE 
2066c5962f3SErwan Le Ray 	c = readl_relaxed(port->membase + ofs->rdr);
20733bb2f6aSErwan Le Ray 	/* Apply RDR data mask */
2086c5962f3SErwan Le Ray 	c &= stm32_port->rdr_mask;
2096c5962f3SErwan Le Ray 
2106c5962f3SErwan Le Ray 	return c;
21134891872SAlexandre TORGUE }
21234891872SAlexandre TORGUE 
2136333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
21448a6092fSMaxime Coquelin {
215ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
216d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
21733bb2f6aSErwan Le Ray 	unsigned long c;
2186333a485SErwan Le Ray 	unsigned int size = 0;
21948a6092fSMaxime Coquelin 	u32 sr;
22048a6092fSMaxime Coquelin 	char flag;
22148a6092fSMaxime Coquelin 
22233bb2f6aSErwan Le Ray 	while (stm32_usart_pending_rx_pio(port, &sr)) {
22348a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
22448a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
22548a6092fSMaxime Coquelin 
2264f01d833SErwan Le Ray 		/*
2274f01d833SErwan Le Ray 		 * Status bits has to be cleared before reading the RDR:
2284f01d833SErwan Le Ray 		 * In FIFO mode, reading the RDR will pop the next data
2294f01d833SErwan Le Ray 		 * (if any) along with its status bits into the SR.
2304f01d833SErwan Le Ray 		 * Not doing so leads to misalignement between RDR and SR,
2314f01d833SErwan Le Ray 		 * and clear status bits of the next rx data.
2324f01d833SErwan Le Ray 		 *
2334f01d833SErwan Le Ray 		 * Clear errors flags for stm32f7 and stm32h7 compatible
2344f01d833SErwan Le Ray 		 * devices. On stm32f4 compatible devices, the error bit is
2354f01d833SErwan Le Ray 		 * cleared by the sequence [read SR - read DR].
2364f01d833SErwan Le Ray 		 */
2374f01d833SErwan Le Ray 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
2381250ed71SFabrice Gasnier 			writel_relaxed(sr & USART_SR_ERR_MASK,
2391250ed71SFabrice Gasnier 				       port->membase + ofs->icr);
2404f01d833SErwan Le Ray 
24133bb2f6aSErwan Le Ray 		c = stm32_usart_get_char_pio(port);
2424f01d833SErwan Le Ray 		port->icount.rx++;
2436333a485SErwan Le Ray 		size++;
24448a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
2454f01d833SErwan Le Ray 			if (sr & USART_SR_ORE) {
24648a6092fSMaxime Coquelin 				port->icount.overrun++;
24748a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
24848a6092fSMaxime Coquelin 				port->icount.parity++;
24948a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
2504f01d833SErwan Le Ray 				/* Break detection if character is null */
2514f01d833SErwan Le Ray 				if (!c) {
2524f01d833SErwan Le Ray 					port->icount.brk++;
2534f01d833SErwan Le Ray 					if (uart_handle_break(port))
2544f01d833SErwan Le Ray 						continue;
2554f01d833SErwan Le Ray 				} else {
25648a6092fSMaxime Coquelin 					port->icount.frame++;
25748a6092fSMaxime Coquelin 				}
2584f01d833SErwan Le Ray 			}
25948a6092fSMaxime Coquelin 
26048a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
26148a6092fSMaxime Coquelin 
2624f01d833SErwan Le Ray 			if (sr & USART_SR_PE) {
26348a6092fSMaxime Coquelin 				flag = TTY_PARITY;
2644f01d833SErwan Le Ray 			} else if (sr & USART_SR_FE) {
2654f01d833SErwan Le Ray 				if (!c)
2664f01d833SErwan Le Ray 					flag = TTY_BREAK;
2674f01d833SErwan Le Ray 				else
26848a6092fSMaxime Coquelin 					flag = TTY_FRAME;
26948a6092fSMaxime Coquelin 			}
2704f01d833SErwan Le Ray 		}
27148a6092fSMaxime Coquelin 
272cea37afdSJohan Hovold 		if (uart_prepare_sysrq_char(port, c))
27348a6092fSMaxime Coquelin 			continue;
27448a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
27548a6092fSMaxime Coquelin 	}
2766333a485SErwan Le Ray 
2776333a485SErwan Le Ray 	return size;
27833bb2f6aSErwan Le Ray }
27933bb2f6aSErwan Le Ray 
28033bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
28133bb2f6aSErwan Le Ray {
28233bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
28333bb2f6aSErwan Le Ray 	struct tty_port *ttyport = &stm32_port->port.state->port;
28433bb2f6aSErwan Le Ray 	unsigned char *dma_start;
28533bb2f6aSErwan Le Ray 	int dma_count, i;
28633bb2f6aSErwan Le Ray 
28733bb2f6aSErwan Le Ray 	dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
28833bb2f6aSErwan Le Ray 
28933bb2f6aSErwan Le Ray 	/*
29033bb2f6aSErwan Le Ray 	 * Apply rdr_mask on buffer in order to mask parity bit.
29133bb2f6aSErwan Le Ray 	 * This loop is useless in cs8 mode because DMA copies only
29233bb2f6aSErwan Le Ray 	 * 8 bits and already ignores parity bit.
29333bb2f6aSErwan Le Ray 	 */
29433bb2f6aSErwan Le Ray 	if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
29533bb2f6aSErwan Le Ray 		for (i = 0; i < dma_size; i++)
29633bb2f6aSErwan Le Ray 			*(dma_start + i) &= stm32_port->rdr_mask;
29733bb2f6aSErwan Le Ray 
29833bb2f6aSErwan Le Ray 	dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
29933bb2f6aSErwan Le Ray 	port->icount.rx += dma_count;
30033bb2f6aSErwan Le Ray 	if (dma_count != dma_size)
30133bb2f6aSErwan Le Ray 		port->icount.buf_overrun++;
30233bb2f6aSErwan Le Ray 	stm32_port->last_res -= dma_count;
30333bb2f6aSErwan Le Ray 	if (stm32_port->last_res == 0)
30433bb2f6aSErwan Le Ray 		stm32_port->last_res = RX_BUF_L;
30533bb2f6aSErwan Le Ray }
30633bb2f6aSErwan Le Ray 
3076333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
30833bb2f6aSErwan Le Ray {
30933bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
3106333a485SErwan Le Ray 	unsigned int dma_size, size = 0;
31133bb2f6aSErwan Le Ray 
31233bb2f6aSErwan Le Ray 	/* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
31333bb2f6aSErwan Le Ray 	if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
31433bb2f6aSErwan Le Ray 		/* Conditional first part: from last_res to end of DMA buffer */
31533bb2f6aSErwan Le Ray 		dma_size = stm32_port->last_res;
31633bb2f6aSErwan Le Ray 		stm32_usart_push_buffer_dma(port, dma_size);
3176333a485SErwan Le Ray 		size = dma_size;
31833bb2f6aSErwan Le Ray 	}
31933bb2f6aSErwan Le Ray 
32033bb2f6aSErwan Le Ray 	dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
32133bb2f6aSErwan Le Ray 	stm32_usart_push_buffer_dma(port, dma_size);
3226333a485SErwan Le Ray 	size += dma_size;
3236333a485SErwan Le Ray 
3246333a485SErwan Le Ray 	return size;
32533bb2f6aSErwan Le Ray }
32633bb2f6aSErwan Le Ray 
3276333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
32833bb2f6aSErwan Le Ray {
32933bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
33033bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
33133bb2f6aSErwan Le Ray 	enum dma_status rx_dma_status;
33233bb2f6aSErwan Le Ray 	u32 sr;
3336333a485SErwan Le Ray 	unsigned int size = 0;
33433bb2f6aSErwan Le Ray 
3356333a485SErwan Le Ray 	if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) {
33633bb2f6aSErwan Le Ray 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
33733bb2f6aSErwan Le Ray 						    stm32_port->rx_ch->cookie,
33833bb2f6aSErwan Le Ray 						    &stm32_port->rx_dma_state);
33933bb2f6aSErwan Le Ray 		if (rx_dma_status == DMA_IN_PROGRESS) {
34033bb2f6aSErwan Le Ray 			/* Empty DMA buffer */
3416333a485SErwan Le Ray 			size = stm32_usart_receive_chars_dma(port);
34233bb2f6aSErwan Le Ray 			sr = readl_relaxed(port->membase + ofs->isr);
34333bb2f6aSErwan Le Ray 			if (sr & USART_SR_ERR_MASK) {
34433bb2f6aSErwan Le Ray 				/* Disable DMA request line */
34533bb2f6aSErwan Le Ray 				stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
34633bb2f6aSErwan Le Ray 
34733bb2f6aSErwan Le Ray 				/* Switch to PIO mode to handle the errors */
3486333a485SErwan Le Ray 				size += stm32_usart_receive_chars_pio(port);
34933bb2f6aSErwan Le Ray 
35033bb2f6aSErwan Le Ray 				/* Switch back to DMA mode */
35133bb2f6aSErwan Le Ray 				stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
35233bb2f6aSErwan Le Ray 			}
35333bb2f6aSErwan Le Ray 		} else {
35433bb2f6aSErwan Le Ray 			/* Disable RX DMA */
35533bb2f6aSErwan Le Ray 			dmaengine_terminate_async(stm32_port->rx_ch);
35633bb2f6aSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
35733bb2f6aSErwan Le Ray 			/* Fall back to interrupt mode */
35833bb2f6aSErwan Le Ray 			dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
3596333a485SErwan Le Ray 			size = stm32_usart_receive_chars_pio(port);
36033bb2f6aSErwan Le Ray 		}
36133bb2f6aSErwan Le Ray 	} else {
3626333a485SErwan Le Ray 		size = stm32_usart_receive_chars_pio(port);
36333bb2f6aSErwan Le Ray 	}
36448a6092fSMaxime Coquelin 
3656333a485SErwan Le Ray 	return size;
36648a6092fSMaxime Coquelin }
36748a6092fSMaxime Coquelin 
3689a135f16SValentin Caron static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
3699a135f16SValentin Caron {
3709a135f16SValentin Caron 	dmaengine_terminate_async(stm32_port->tx_ch);
3719a135f16SValentin Caron 	stm32_port->tx_dma_busy = false;
3729a135f16SValentin Caron }
3739a135f16SValentin Caron 
3749a135f16SValentin Caron static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
3759a135f16SValentin Caron {
3769a135f16SValentin Caron 	/*
3779a135f16SValentin Caron 	 * We cannot use the function "dmaengine_tx_status" to know the
3789a135f16SValentin Caron 	 * status of DMA. This function does not show if the "dma complete"
3799a135f16SValentin Caron 	 * callback of the DMA transaction has been called. So we prefer
3809a135f16SValentin Caron 	 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
3819a135f16SValentin Caron 	 * same time.
3829a135f16SValentin Caron 	 */
3839a135f16SValentin Caron 	return stm32_port->tx_dma_busy;
3849a135f16SValentin Caron }
3859a135f16SValentin Caron 
3869a135f16SValentin Caron static bool stm32_usart_tx_dma_enabled(struct stm32_port *stm32_port)
3879a135f16SValentin Caron {
3889a135f16SValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
3899a135f16SValentin Caron 
3909a135f16SValentin Caron 	return !!(readl_relaxed(stm32_port->port.membase + ofs->cr3) & USART_CR3_DMAT);
3919a135f16SValentin Caron }
3929a135f16SValentin Caron 
39356f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg)
39434891872SAlexandre TORGUE {
39534891872SAlexandre TORGUE 	struct uart_port *port = arg;
39634891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
397d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
398f16b90c2SErwan Le Ray 	unsigned long flags;
39934891872SAlexandre TORGUE 
40056f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
4019a135f16SValentin Caron 	stm32_usart_tx_dma_terminate(stm32port);
40234891872SAlexandre TORGUE 
40334891872SAlexandre TORGUE 	/* Let's see if we have pending data to send */
404f16b90c2SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
40556f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
406f16b90c2SErwan Le Ray 	spin_unlock_irqrestore(&port->lock, flags);
40734891872SAlexandre TORGUE }
40834891872SAlexandre TORGUE 
40956f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
410d075719eSErwan Le Ray {
411d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
412d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
413d075719eSErwan Le Ray 
414d075719eSErwan Le Ray 	/*
415d075719eSErwan Le Ray 	 * Enables TX FIFO threashold irq when FIFO is enabled,
416d075719eSErwan Le Ray 	 * or TX empty irq when FIFO is disabled
417d075719eSErwan Le Ray 	 */
4182aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
41956f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
420d075719eSErwan Le Ray 	else
42156f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
422d075719eSErwan Le Ray }
423d075719eSErwan Le Ray 
42433bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg)
42533bb2f6aSErwan Le Ray {
42633bb2f6aSErwan Le Ray 	struct uart_port *port = arg;
4276333a485SErwan Le Ray 	struct tty_port *tport = &port->state->port;
4286333a485SErwan Le Ray 	unsigned int size;
4296333a485SErwan Le Ray 	unsigned long flags;
43033bb2f6aSErwan Le Ray 
4316333a485SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
4326333a485SErwan Le Ray 	size = stm32_usart_receive_chars(port, false);
4336333a485SErwan Le Ray 	uart_unlock_and_check_sysrq_irqrestore(port, flags);
4346333a485SErwan Le Ray 	if (size)
4356333a485SErwan Le Ray 		tty_flip_buffer_push(tport);
43633bb2f6aSErwan Le Ray }
43733bb2f6aSErwan Le Ray 
43856f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
439d075719eSErwan Le Ray {
440d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
441d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
442d075719eSErwan Le Ray 
4432aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
44456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
445d075719eSErwan Le Ray 	else
44656f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
447d075719eSErwan Le Ray }
448d075719eSErwan Le Ray 
44956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port)
45034891872SAlexandre TORGUE {
45134891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
452d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
45334891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
45434891872SAlexandre TORGUE 
4559a135f16SValentin Caron 	if (stm32_usart_tx_dma_enabled(stm32_port))
45656f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
45734891872SAlexandre TORGUE 
4585d9176edSErwan Le Ray 	while (!uart_circ_empty(xmit)) {
4595d9176edSErwan Le Ray 		/* Check that TDR is empty before filling FIFO */
4605d9176edSErwan Le Ray 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
4615d9176edSErwan Le Ray 			break;
46234891872SAlexandre TORGUE 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
46334891872SAlexandre TORGUE 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
46434891872SAlexandre TORGUE 		port->icount.tx++;
46534891872SAlexandre TORGUE 	}
46634891872SAlexandre TORGUE 
4675d9176edSErwan Le Ray 	/* rely on TXE irq (mask or unmask) for sending remaining data */
4685d9176edSErwan Le Ray 	if (uart_circ_empty(xmit))
46956f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
4705d9176edSErwan Le Ray 	else
47156f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_enable(port);
4725d9176edSErwan Le Ray }
4735d9176edSErwan Le Ray 
47456f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port)
47534891872SAlexandre TORGUE {
47634891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
477d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
47834891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
47934891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
480195437d1SValentin Caron 	unsigned int count;
48134891872SAlexandre TORGUE 
4829a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32port)) {
4839a135f16SValentin Caron 		if (!stm32_usart_tx_dma_enabled(stm32port))
4849a135f16SValentin Caron 			stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
48534891872SAlexandre TORGUE 		return;
4869a135f16SValentin Caron 	}
48734891872SAlexandre TORGUE 
48834891872SAlexandre TORGUE 	count = uart_circ_chars_pending(xmit);
48934891872SAlexandre TORGUE 
49034891872SAlexandre TORGUE 	if (count > TX_BUF_L)
49134891872SAlexandre TORGUE 		count = TX_BUF_L;
49234891872SAlexandre TORGUE 
49334891872SAlexandre TORGUE 	if (xmit->tail < xmit->head) {
49434891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
49534891872SAlexandre TORGUE 	} else {
49634891872SAlexandre TORGUE 		size_t one = UART_XMIT_SIZE - xmit->tail;
49734891872SAlexandre TORGUE 		size_t two;
49834891872SAlexandre TORGUE 
49934891872SAlexandre TORGUE 		if (one > count)
50034891872SAlexandre TORGUE 			one = count;
50134891872SAlexandre TORGUE 		two = count - one;
50234891872SAlexandre TORGUE 
50334891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
50434891872SAlexandre TORGUE 		if (two)
50534891872SAlexandre TORGUE 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
50634891872SAlexandre TORGUE 	}
50734891872SAlexandre TORGUE 
50834891872SAlexandre TORGUE 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
50934891872SAlexandre TORGUE 					   stm32port->tx_dma_buf,
51034891872SAlexandre TORGUE 					   count,
51134891872SAlexandre TORGUE 					   DMA_MEM_TO_DEV,
51234891872SAlexandre TORGUE 					   DMA_PREP_INTERRUPT);
51334891872SAlexandre TORGUE 
514e7997f7fSErwan Le Ray 	if (!desc)
515e7997f7fSErwan Le Ray 		goto fallback_err;
51634891872SAlexandre TORGUE 
5179a135f16SValentin Caron 	/*
5189a135f16SValentin Caron 	 * Set "tx_dma_busy" flag. This flag will be released when
5199a135f16SValentin Caron 	 * dmaengine_terminate_async will be called. This flag helps
5209a135f16SValentin Caron 	 * transmit_chars_dma not to start another DMA transaction
5219a135f16SValentin Caron 	 * if the callback of the previous is not yet called.
5229a135f16SValentin Caron 	 */
5239a135f16SValentin Caron 	stm32port->tx_dma_busy = true;
5249a135f16SValentin Caron 
52556f9a76cSErwan Le Ray 	desc->callback = stm32_usart_tx_dma_complete;
52634891872SAlexandre TORGUE 	desc->callback_param = port;
52734891872SAlexandre TORGUE 
52834891872SAlexandre TORGUE 	/* Push current DMA TX transaction in the pending queue */
529e7997f7fSErwan Le Ray 	if (dma_submit_error(dmaengine_submit(desc))) {
530e7997f7fSErwan Le Ray 		/* dma no yet started, safe to free resources */
5319a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32port);
532e7997f7fSErwan Le Ray 		goto fallback_err;
533e7997f7fSErwan Le Ray 	}
53434891872SAlexandre TORGUE 
53534891872SAlexandre TORGUE 	/* Issue pending DMA TX requests */
53634891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->tx_ch);
53734891872SAlexandre TORGUE 
53856f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
53934891872SAlexandre TORGUE 
54034891872SAlexandre TORGUE 	xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
54134891872SAlexandre TORGUE 	port->icount.tx += count;
542e7997f7fSErwan Le Ray 	return;
543e7997f7fSErwan Le Ray 
544e7997f7fSErwan Le Ray fallback_err:
54556f9a76cSErwan Le Ray 	stm32_usart_transmit_chars_pio(port);
54634891872SAlexandre TORGUE }
54734891872SAlexandre TORGUE 
54856f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port)
54948a6092fSMaxime Coquelin {
550ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
551d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
55248a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
553d3d079bdSValentin Caron 	u32 isr;
554d3d079bdSValentin Caron 	int ret;
55548a6092fSMaxime Coquelin 
55648a6092fSMaxime Coquelin 	if (port->x_char) {
5579a135f16SValentin Caron 		if (stm32_usart_tx_dma_started(stm32_port) &&
5589a135f16SValentin Caron 		    stm32_usart_tx_dma_enabled(stm32_port))
55956f9a76cSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
560d3d079bdSValentin Caron 
561d3d079bdSValentin Caron 		/* Check that TDR is empty before filling FIFO */
562d3d079bdSValentin Caron 		ret =
563d3d079bdSValentin Caron 		readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
564d3d079bdSValentin Caron 						  isr,
565d3d079bdSValentin Caron 						  (isr & USART_SR_TXE),
566d3d079bdSValentin Caron 						  10, 1000);
567d3d079bdSValentin Caron 		if (ret)
568d3d079bdSValentin Caron 			dev_warn(port->dev, "1 character may be erased\n");
569d3d079bdSValentin Caron 
570ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
57148a6092fSMaxime Coquelin 		port->x_char = 0;
57248a6092fSMaxime Coquelin 		port->icount.tx++;
5739a135f16SValentin Caron 		if (stm32_usart_tx_dma_started(stm32_port))
57456f9a76cSErwan Le Ray 			stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
57548a6092fSMaxime Coquelin 		return;
57648a6092fSMaxime Coquelin 	}
57748a6092fSMaxime Coquelin 
578b83b957cSErwan Le Ray 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
57956f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
58048a6092fSMaxime Coquelin 		return;
58148a6092fSMaxime Coquelin 	}
58248a6092fSMaxime Coquelin 
58364c32eabSErwan Le Ray 	if (ofs->icr == UNDEF_REG)
58456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
58564c32eabSErwan Le Ray 	else
5861250ed71SFabrice Gasnier 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
58764c32eabSErwan Le Ray 
58834891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
58956f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_dma(port);
59034891872SAlexandre TORGUE 	else
59156f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_pio(port);
59248a6092fSMaxime Coquelin 
59348a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
59448a6092fSMaxime Coquelin 		uart_write_wakeup(port);
59548a6092fSMaxime Coquelin 
59648a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
59756f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
59848a6092fSMaxime Coquelin }
59948a6092fSMaxime Coquelin 
60056f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
60148a6092fSMaxime Coquelin {
60248a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
60312761869SErwan Le Ray 	struct tty_port *tport = &port->state->port;
604ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
605d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
60648a6092fSMaxime Coquelin 	u32 sr;
6076333a485SErwan Le Ray 	unsigned int size;
60848a6092fSMaxime Coquelin 
609ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
61048a6092fSMaxime Coquelin 
6114cc0ed62SErwan Le Ray 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
6124cc0ed62SErwan Le Ray 		writel_relaxed(USART_ICR_RTOCF,
6134cc0ed62SErwan Le Ray 			       port->membase + ofs->icr);
6144cc0ed62SErwan Le Ray 
61512761869SErwan Le Ray 	if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
61612761869SErwan Le Ray 		/* Clear wake up flag and disable wake up interrupt */
617270e5a74SFabrice Gasnier 		writel_relaxed(USART_ICR_WUCF,
618270e5a74SFabrice Gasnier 			       port->membase + ofs->icr);
61912761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
62012761869SErwan Le Ray 		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
62112761869SErwan Le Ray 			pm_wakeup_event(tport->tty->dev, 0);
62212761869SErwan Le Ray 	}
623270e5a74SFabrice Gasnier 
62433bb2f6aSErwan Le Ray 	/*
62533bb2f6aSErwan Le Ray 	 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
62633bb2f6aSErwan Le Ray 	 * line has been masked by HW and rx data are stacking in FIFO.
62733bb2f6aSErwan Le Ray 	 */
628d1ec8a2eSErwan Le Ray 	if (!stm32_port->throttled) {
62933bb2f6aSErwan Le Ray 		if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) ||
630d1ec8a2eSErwan Le Ray 		    ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) {
6316333a485SErwan Le Ray 			spin_lock(&port->lock);
6326333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, false);
6336333a485SErwan Le Ray 			uart_unlock_and_check_sysrq(port);
6346333a485SErwan Le Ray 			if (size)
6356333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
636d1ec8a2eSErwan Le Ray 		}
637d1ec8a2eSErwan Le Ray 	}
63848a6092fSMaxime Coquelin 
639ad767681SErwan Le Ray 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
640ad767681SErwan Le Ray 		spin_lock(&port->lock);
64156f9a76cSErwan Le Ray 		stm32_usart_transmit_chars(port);
64201d32d71SAlexandre TORGUE 		spin_unlock(&port->lock);
643ad767681SErwan Le Ray 	}
64401d32d71SAlexandre TORGUE 
64533bb2f6aSErwan Le Ray 	if (stm32_usart_rx_dma_enabled(port))
64634891872SAlexandre TORGUE 		return IRQ_WAKE_THREAD;
64734891872SAlexandre TORGUE 	else
64834891872SAlexandre TORGUE 		return IRQ_HANDLED;
64934891872SAlexandre TORGUE }
65034891872SAlexandre TORGUE 
65156f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr)
65234891872SAlexandre TORGUE {
65334891872SAlexandre TORGUE 	struct uart_port *port = ptr;
6546333a485SErwan Le Ray 	struct tty_port *tport = &port->state->port;
655d1ec8a2eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
6566333a485SErwan Le Ray 	unsigned int size;
6576333a485SErwan Le Ray 	unsigned long flags;
65834891872SAlexandre TORGUE 
659cc58d0a3SErwan Le Ray 	/* Receiver timeout irq for DMA RX */
6606333a485SErwan Le Ray 	if (!stm32_port->throttled) {
6616333a485SErwan Le Ray 		spin_lock_irqsave(&port->lock, flags);
6626333a485SErwan Le Ray 		size = stm32_usart_receive_chars(port, false);
6636333a485SErwan Le Ray 		uart_unlock_and_check_sysrq_irqrestore(port, flags);
6646333a485SErwan Le Ray 		if (size)
6656333a485SErwan Le Ray 			tty_flip_buffer_push(tport);
6666333a485SErwan Le Ray 	}
66734891872SAlexandre TORGUE 
66848a6092fSMaxime Coquelin 	return IRQ_HANDLED;
66948a6092fSMaxime Coquelin }
67048a6092fSMaxime Coquelin 
67156f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port)
67248a6092fSMaxime Coquelin {
673ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
674d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
675ada8618fSAlexandre TORGUE 
6763db1d524SErwan Le Ray 	if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
6773db1d524SErwan Le Ray 		return TIOCSER_TEMT;
6783db1d524SErwan Le Ray 
6793db1d524SErwan Le Ray 	return 0;
68048a6092fSMaxime Coquelin }
68148a6092fSMaxime Coquelin 
68256f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
68348a6092fSMaxime Coquelin {
684ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
685d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
686ada8618fSAlexandre TORGUE 
68748a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
68856f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
68948a6092fSMaxime Coquelin 	else
69056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
6916cf61b9bSManivannan Sadhasivam 
6926cf61b9bSManivannan Sadhasivam 	mctrl_gpio_set(stm32_port->gpios, mctrl);
69348a6092fSMaxime Coquelin }
69448a6092fSMaxime Coquelin 
69556f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
69648a6092fSMaxime Coquelin {
6976cf61b9bSManivannan Sadhasivam 	struct stm32_port *stm32_port = to_stm32_port(port);
6986cf61b9bSManivannan Sadhasivam 	unsigned int ret;
6996cf61b9bSManivannan Sadhasivam 
70048a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
7016cf61b9bSManivannan Sadhasivam 	ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
7026cf61b9bSManivannan Sadhasivam 
7036cf61b9bSManivannan Sadhasivam 	return mctrl_gpio_get(stm32_port->gpios, &ret);
7046cf61b9bSManivannan Sadhasivam }
7056cf61b9bSManivannan Sadhasivam 
70656f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port)
7076cf61b9bSManivannan Sadhasivam {
7086cf61b9bSManivannan Sadhasivam 	mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
7096cf61b9bSManivannan Sadhasivam }
7106cf61b9bSManivannan Sadhasivam 
71156f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port)
7126cf61b9bSManivannan Sadhasivam {
7136cf61b9bSManivannan Sadhasivam 	mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
71448a6092fSMaxime Coquelin }
71548a6092fSMaxime Coquelin 
71648a6092fSMaxime Coquelin /* Transmit stop */
71756f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port)
71848a6092fSMaxime Coquelin {
719ad0c2748SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
720ad0c2748SMarek Vasut 	struct serial_rs485 *rs485conf = &port->rs485;
7212a3bcfe0SValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
722ad0c2748SMarek Vasut 
72356f9a76cSErwan Le Ray 	stm32_usart_tx_interrupt_disable(port);
7242a3bcfe0SValentin Caron 	if (stm32_usart_tx_dma_started(stm32_port) && stm32_usart_tx_dma_enabled(stm32_port))
7252a3bcfe0SValentin Caron 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
726ad0c2748SMarek Vasut 
727ad0c2748SMarek Vasut 	if (rs485conf->flags & SER_RS485_ENABLED) {
728ad0c2748SMarek Vasut 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
729ad0c2748SMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
730ad0c2748SMarek Vasut 					stm32_port->port.mctrl & ~TIOCM_RTS);
731ad0c2748SMarek Vasut 		} else {
732ad0c2748SMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
733ad0c2748SMarek Vasut 					stm32_port->port.mctrl | TIOCM_RTS);
734ad0c2748SMarek Vasut 		}
735ad0c2748SMarek Vasut 	}
73648a6092fSMaxime Coquelin }
73748a6092fSMaxime Coquelin 
73848a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
73956f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port)
74048a6092fSMaxime Coquelin {
741ad0c2748SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
742ad0c2748SMarek Vasut 	struct serial_rs485 *rs485conf = &port->rs485;
74348a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
74448a6092fSMaxime Coquelin 
745037b91ecSValentin Caron 	if (uart_circ_empty(xmit) && !port->x_char)
74648a6092fSMaxime Coquelin 		return;
74748a6092fSMaxime Coquelin 
748ad0c2748SMarek Vasut 	if (rs485conf->flags & SER_RS485_ENABLED) {
749ad0c2748SMarek Vasut 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
750ad0c2748SMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
751ad0c2748SMarek Vasut 					stm32_port->port.mctrl | TIOCM_RTS);
752ad0c2748SMarek Vasut 		} else {
753ad0c2748SMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
754ad0c2748SMarek Vasut 					stm32_port->port.mctrl & ~TIOCM_RTS);
755ad0c2748SMarek Vasut 		}
756ad0c2748SMarek Vasut 	}
757ad0c2748SMarek Vasut 
75856f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
75948a6092fSMaxime Coquelin }
76048a6092fSMaxime Coquelin 
7613d82be8bSErwan Le Ray /* Flush the transmit buffer. */
7623d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port)
7633d82be8bSErwan Le Ray {
7643d82be8bSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
7653d82be8bSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
7663d82be8bSErwan Le Ray 
7673d82be8bSErwan Le Ray 	if (stm32_port->tx_ch) {
7689a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
7693d82be8bSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
7703d82be8bSErwan Le Ray 	}
7713d82be8bSErwan Le Ray }
7723d82be8bSErwan Le Ray 
77348a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
77456f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port)
77548a6092fSMaxime Coquelin {
776ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
777d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
77848a6092fSMaxime Coquelin 	unsigned long flags;
77948a6092fSMaxime Coquelin 
78048a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
781d1ec8a2eSErwan Le Ray 
782d1ec8a2eSErwan Le Ray 	/*
783d1ec8a2eSErwan Le Ray 	 * Disable DMA request line if enabled, so the RX data gets queued into the FIFO.
784d1ec8a2eSErwan Le Ray 	 * Hardware flow control is triggered when RX FIFO is full.
785d1ec8a2eSErwan Le Ray 	 */
786d1ec8a2eSErwan Le Ray 	if (stm32_usart_rx_dma_enabled(port))
787d1ec8a2eSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
788d1ec8a2eSErwan Le Ray 
78956f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
790d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
79156f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
792d0a6a7bcSErwan Le Ray 
793d1ec8a2eSErwan Le Ray 	stm32_port->throttled = true;
79448a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
79548a6092fSMaxime Coquelin }
79648a6092fSMaxime Coquelin 
79748a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
79856f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port)
79948a6092fSMaxime Coquelin {
800ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
801d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
80248a6092fSMaxime Coquelin 	unsigned long flags;
80348a6092fSMaxime Coquelin 
80448a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
80556f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
806d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
80756f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
808d0a6a7bcSErwan Le Ray 
809d1ec8a2eSErwan Le Ray 	/*
810d1ec8a2eSErwan Le Ray 	 * Switch back to DMA mode (re-enable DMA request line).
811d1ec8a2eSErwan Le Ray 	 * Hardware flow control is stopped when FIFO is not full any more.
812d1ec8a2eSErwan Le Ray 	 */
813d1ec8a2eSErwan Le Ray 	if (stm32_port->rx_ch)
814d1ec8a2eSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
815d1ec8a2eSErwan Le Ray 
816d1ec8a2eSErwan Le Ray 	stm32_port->throttled = false;
81748a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
81848a6092fSMaxime Coquelin }
81948a6092fSMaxime Coquelin 
82048a6092fSMaxime Coquelin /* Receive stop */
82156f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port)
82248a6092fSMaxime Coquelin {
823ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
824d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
825ada8618fSAlexandre TORGUE 
826e0abc903SErwan Le Ray 	/* Disable DMA request line. */
827e0abc903SErwan Le Ray 	if (stm32_port->rx_ch)
828e0abc903SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
829e0abc903SErwan Le Ray 
83056f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
831d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
83256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
83348a6092fSMaxime Coquelin }
83448a6092fSMaxime Coquelin 
83548a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
83656f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
83748a6092fSMaxime Coquelin {
83848a6092fSMaxime Coquelin }
83948a6092fSMaxime Coquelin 
8406eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port)
8416eeb348cSErwan Le Ray {
8426eeb348cSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
8436eeb348cSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
8446eeb348cSErwan Le Ray 	struct dma_async_tx_descriptor *desc;
8456eeb348cSErwan Le Ray 	int ret;
8466eeb348cSErwan Le Ray 
8476eeb348cSErwan Le Ray 	stm32_port->last_res = RX_BUF_L;
8486eeb348cSErwan Le Ray 	/* Prepare a DMA cyclic transaction */
8496eeb348cSErwan Le Ray 	desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
8506eeb348cSErwan Le Ray 					 stm32_port->rx_dma_buf,
8516eeb348cSErwan Le Ray 					 RX_BUF_L, RX_BUF_P,
8526eeb348cSErwan Le Ray 					 DMA_DEV_TO_MEM,
8536eeb348cSErwan Le Ray 					 DMA_PREP_INTERRUPT);
8546eeb348cSErwan Le Ray 	if (!desc) {
8556eeb348cSErwan Le Ray 		dev_err(port->dev, "rx dma prep cyclic failed\n");
8566eeb348cSErwan Le Ray 		return -ENODEV;
8576eeb348cSErwan Le Ray 	}
8586eeb348cSErwan Le Ray 
8596eeb348cSErwan Le Ray 	desc->callback = stm32_usart_rx_dma_complete;
8606eeb348cSErwan Le Ray 	desc->callback_param = port;
8616eeb348cSErwan Le Ray 
8626eeb348cSErwan Le Ray 	/* Push current DMA transaction in the pending queue */
8636eeb348cSErwan Le Ray 	ret = dma_submit_error(dmaengine_submit(desc));
8646eeb348cSErwan Le Ray 	if (ret) {
8656eeb348cSErwan Le Ray 		dmaengine_terminate_sync(stm32_port->rx_ch);
8666eeb348cSErwan Le Ray 		return ret;
8676eeb348cSErwan Le Ray 	}
8686eeb348cSErwan Le Ray 
8696eeb348cSErwan Le Ray 	/* Issue pending DMA requests */
8706eeb348cSErwan Le Ray 	dma_async_issue_pending(stm32_port->rx_ch);
8716eeb348cSErwan Le Ray 
8726eeb348cSErwan Le Ray 	/*
8736eeb348cSErwan Le Ray 	 * DMA request line not re-enabled at resume when port is throttled.
8746eeb348cSErwan Le Ray 	 * It will be re-enabled by unthrottle ops.
8756eeb348cSErwan Le Ray 	 */
8766eeb348cSErwan Le Ray 	if (!stm32_port->throttled)
8776eeb348cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
8786eeb348cSErwan Le Ray 
8796eeb348cSErwan Le Ray 	return 0;
8806eeb348cSErwan Le Ray }
8816eeb348cSErwan Le Ray 
88256f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port)
88348a6092fSMaxime Coquelin {
884ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
885d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
886f4518a8aSErwan Le Ray 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
88748a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
88848a6092fSMaxime Coquelin 	u32 val;
88948a6092fSMaxime Coquelin 	int ret;
89048a6092fSMaxime Coquelin 
89156f9a76cSErwan Le Ray 	ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
89256f9a76cSErwan Le Ray 				   stm32_usart_threaded_interrupt,
893e359b441SJohan Hovold 				   IRQF_ONESHOT | IRQF_NO_SUSPEND,
894e359b441SJohan Hovold 				   name, port);
89548a6092fSMaxime Coquelin 	if (ret)
89648a6092fSMaxime Coquelin 		return ret;
89748a6092fSMaxime Coquelin 
8983cd66593SMartin Devera 	if (stm32_port->swap) {
8993cd66593SMartin Devera 		val = readl_relaxed(port->membase + ofs->cr2);
9003cd66593SMartin Devera 		val |= USART_CR2_SWAP;
9013cd66593SMartin Devera 		writel_relaxed(val, port->membase + ofs->cr2);
9023cd66593SMartin Devera 	}
9033cd66593SMartin Devera 
90484872dc4SErwan Le Ray 	/* RX FIFO Flush */
90584872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
906315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
90748a6092fSMaxime Coquelin 
908e0abc903SErwan Le Ray 	if (stm32_port->rx_ch) {
9096eeb348cSErwan Le Ray 		ret = stm32_usart_start_rx_dma_cyclic(port);
910e0abc903SErwan Le Ray 		if (ret) {
9116eeb348cSErwan Le Ray 			free_irq(port->irq, port);
9126eeb348cSErwan Le Ray 			return ret;
913e0abc903SErwan Le Ray 		}
914e0abc903SErwan Le Ray 	}
915d1ec8a2eSErwan Le Ray 
91625a8e761SErwan Le Ray 	/* RX enabling */
917f4518a8aSErwan Le Ray 	val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
91856f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, val);
91984872dc4SErwan Le Ray 
92048a6092fSMaxime Coquelin 	return 0;
92148a6092fSMaxime Coquelin }
92248a6092fSMaxime Coquelin 
92356f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port)
92448a6092fSMaxime Coquelin {
925ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
926d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
927d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
92864c32eabSErwan Le Ray 	u32 val, isr;
92964c32eabSErwan Le Ray 	int ret;
93048a6092fSMaxime Coquelin 
9319a135f16SValentin Caron 	if (stm32_usart_tx_dma_enabled(stm32_port))
93256a23f93SValentin Caron 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
9339a135f16SValentin Caron 
9349a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32_port))
9359a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
93656a23f93SValentin Caron 
9376cf61b9bSManivannan Sadhasivam 	/* Disable modem control interrupts */
93856f9a76cSErwan Le Ray 	stm32_usart_disable_ms(port);
9396cf61b9bSManivannan Sadhasivam 
9404cc0ed62SErwan Le Ray 	val = USART_CR1_TXEIE | USART_CR1_TE;
9414cc0ed62SErwan Le Ray 	val |= stm32_port->cr1_irq | USART_CR1_RE;
94287f1f809SAlexandre TORGUE 	val |= BIT(cfg->uart_enable_bit);
943351a762aSGerald Baeza 	if (stm32_port->fifoen)
944351a762aSGerald Baeza 		val |= USART_CR1_FIFOEN;
94564c32eabSErwan Le Ray 
94664c32eabSErwan Le Ray 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
94764c32eabSErwan Le Ray 					 isr, (isr & USART_SR_TC),
94864c32eabSErwan Le Ray 					 10, 100000);
94964c32eabSErwan Le Ray 
950c31c3ea0SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set */
95164c32eabSErwan Le Ray 	if (ret)
952c31c3ea0SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
95364c32eabSErwan Le Ray 
954e0abc903SErwan Le Ray 	/* Disable RX DMA. */
955e0abc903SErwan Le Ray 	if (stm32_port->rx_ch)
956e0abc903SErwan Le Ray 		dmaengine_terminate_async(stm32_port->rx_ch);
957e0abc903SErwan Le Ray 
9589f77d192SErwan Le Ray 	/* flush RX & TX FIFO */
9599f77d192SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
9609f77d192SErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
9619f77d192SErwan Le Ray 			       port->membase + ofs->rqr);
9629f77d192SErwan Le Ray 
96356f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, val);
96448a6092fSMaxime Coquelin 
96548a6092fSMaxime Coquelin 	free_irq(port->irq, port);
96648a6092fSMaxime Coquelin }
96748a6092fSMaxime Coquelin 
96856f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port,
96956f9a76cSErwan Le Ray 				    struct ktermios *termios,
97048a6092fSMaxime Coquelin 				    struct ktermios *old)
97148a6092fSMaxime Coquelin {
97248a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
973d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
974d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
9751bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
976c8a9d043SErwan Le Ray 	unsigned int baud, bits;
97748a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
97848a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
979f264c6f6SErwan Le Ray 	u32 cr1, cr2, cr3, isr;
98048a6092fSMaxime Coquelin 	unsigned long flags;
981f264c6f6SErwan Le Ray 	int ret;
98248a6092fSMaxime Coquelin 
98348a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
98448a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
98548a6092fSMaxime Coquelin 
98648a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
98748a6092fSMaxime Coquelin 
98848a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
98948a6092fSMaxime Coquelin 
990f264c6f6SErwan Le Ray 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
991f264c6f6SErwan Le Ray 						isr,
992f264c6f6SErwan Le Ray 						(isr & USART_SR_TC),
993f264c6f6SErwan Le Ray 						10, 100000);
994f264c6f6SErwan Le Ray 
995f264c6f6SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set. */
996f264c6f6SErwan Le Ray 	if (ret)
997f264c6f6SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
998f264c6f6SErwan Le Ray 
99948a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
1000ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
100148a6092fSMaxime Coquelin 
100284872dc4SErwan Le Ray 	/* flush RX & TX FIFO */
100384872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
1004315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1005315e2d8aSErwan Le Ray 			       port->membase + ofs->rqr);
10061bcda09dSBich HEMON 
100784872dc4SErwan Le Ray 	cr1 = USART_CR1_TE | USART_CR1_RE;
1008351a762aSGerald Baeza 	if (stm32_port->fifoen)
1009351a762aSGerald Baeza 		cr1 |= USART_CR1_FIFOEN;
10103cd66593SMartin Devera 	cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
101125a8e761SErwan Le Ray 
101225a8e761SErwan Le Ray 	/* Tx and RX FIFO configuration */
1013d075719eSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
101425a8e761SErwan Le Ray 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
101525a8e761SErwan Le Ray 	if (stm32_port->fifoen) {
10162aa1bbb2SFabrice Gasnier 		if (stm32_port->txftcfg >= 0)
10172aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
10182aa1bbb2SFabrice Gasnier 		if (stm32_port->rxftcfg >= 0)
10192aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
102025a8e761SErwan Le Ray 	}
102148a6092fSMaxime Coquelin 
102248a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
102348a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
102448a6092fSMaxime Coquelin 
10253ec2ff37SJiri Slaby 	bits = tty_get_char_size(cflag);
10266c5962f3SErwan Le Ray 	stm32_port->rdr_mask = (BIT(bits) - 1);
1027c8a9d043SErwan Le Ray 
102848a6092fSMaxime Coquelin 	if (cflag & PARENB) {
1029c8a9d043SErwan Le Ray 		bits++;
103048a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
1031c8a9d043SErwan Le Ray 	}
1032c8a9d043SErwan Le Ray 
1033c8a9d043SErwan Le Ray 	/*
1034c8a9d043SErwan Le Ray 	 * Word length configuration:
1035c8a9d043SErwan Le Ray 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1036c8a9d043SErwan Le Ray 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1037c8a9d043SErwan Le Ray 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1038c8a9d043SErwan Le Ray 	 * M0 and M1 already cleared by cr1 initialization.
1039c8a9d043SErwan Le Ray 	 */
1040c8a9d043SErwan Le Ray 	if (bits == 9)
1041ada8618fSAlexandre TORGUE 		cr1 |= USART_CR1_M0;
1042c8a9d043SErwan Le Ray 	else if ((bits == 7) && cfg->has_7bits_data)
1043c8a9d043SErwan Le Ray 		cr1 |= USART_CR1_M1;
1044c8a9d043SErwan Le Ray 	else if (bits != 8)
1045c8a9d043SErwan Le Ray 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1046c8a9d043SErwan Le Ray 			, bits);
104748a6092fSMaxime Coquelin 
10484cc0ed62SErwan Le Ray 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
10492aa1bbb2SFabrice Gasnier 				       (stm32_port->fifoen &&
10502aa1bbb2SFabrice Gasnier 					stm32_port->rxftcfg >= 0))) {
10514cc0ed62SErwan Le Ray 		if (cflag & CSTOPB)
10524cc0ed62SErwan Le Ray 			bits = bits + 3; /* 1 start bit + 2 stop bits */
10534cc0ed62SErwan Le Ray 		else
10544cc0ed62SErwan Le Ray 			bits = bits + 2; /* 1 start bit + 1 stop bit */
10554cc0ed62SErwan Le Ray 
10564cc0ed62SErwan Le Ray 		/* RX timeout irq to occur after last stop bit + bits */
10574cc0ed62SErwan Le Ray 		stm32_port->cr1_irq = USART_CR1_RTOIE;
10584cc0ed62SErwan Le Ray 		writel_relaxed(bits, port->membase + ofs->rtor);
10594cc0ed62SErwan Le Ray 		cr2 |= USART_CR2_RTOEN;
106033bb2f6aSErwan Le Ray 		/*
106133bb2f6aSErwan Le Ray 		 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
106233bb2f6aSErwan Le Ray 		 * wake up over usart, from low power until the DMA gets re-enabled by resume.
106333bb2f6aSErwan Le Ray 		 */
1064d0a6a7bcSErwan Le Ray 		stm32_port->cr3_irq =  USART_CR3_RXFTIE;
10654cc0ed62SErwan Le Ray 	}
10664cc0ed62SErwan Le Ray 
1067d0a6a7bcSErwan Le Ray 	cr1 |= stm32_port->cr1_irq;
1068d0a6a7bcSErwan Le Ray 	cr3 |= stm32_port->cr3_irq;
1069d0a6a7bcSErwan Le Ray 
107048a6092fSMaxime Coquelin 	if (cflag & PARODD)
107148a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
107248a6092fSMaxime Coquelin 
107348a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
107448a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
107548a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
107635abe98fSBich HEMON 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
107748a6092fSMaxime Coquelin 	}
107848a6092fSMaxime Coquelin 
107948a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
108048a6092fSMaxime Coquelin 
108148a6092fSMaxime Coquelin 	/*
108248a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
108348a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
108448a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
108548a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
108648a6092fSMaxime Coquelin 	 */
108748a6092fSMaxime Coquelin 	if (usartdiv < 16) {
108848a6092fSMaxime Coquelin 		oversampling = 8;
10891bcda09dSBich HEMON 		cr1 |= USART_CR1_OVER8;
109056f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
109148a6092fSMaxime Coquelin 	} else {
109248a6092fSMaxime Coquelin 		oversampling = 16;
10931bcda09dSBich HEMON 		cr1 &= ~USART_CR1_OVER8;
109456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
109548a6092fSMaxime Coquelin 	}
109648a6092fSMaxime Coquelin 
109748a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
109848a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
1099ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
110048a6092fSMaxime Coquelin 
110148a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
110248a6092fSMaxime Coquelin 
110348a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
110448a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
110548a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
110648a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
11074f01d833SErwan Le Ray 		port->read_status_mask |= USART_SR_FE;
110848a6092fSMaxime Coquelin 
110948a6092fSMaxime Coquelin 	/* Characters to ignore */
111048a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
111148a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
111248a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
111348a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
11144f01d833SErwan Le Ray 		port->ignore_status_mask |= USART_SR_FE;
111548a6092fSMaxime Coquelin 		/*
111648a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
111748a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
111848a6092fSMaxime Coquelin 		 */
111948a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
112048a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
112148a6092fSMaxime Coquelin 	}
112248a6092fSMaxime Coquelin 
112348a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
112448a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
112548a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
112648a6092fSMaxime Coquelin 
112733bb2f6aSErwan Le Ray 	if (stm32_port->rx_ch) {
112833bb2f6aSErwan Le Ray 		/*
112933bb2f6aSErwan Le Ray 		 * Setup DMA to collect only valid data and enable error irqs.
113033bb2f6aSErwan Le Ray 		 * This also enables break reception when using DMA.
113133bb2f6aSErwan Le Ray 		 */
113233bb2f6aSErwan Le Ray 		cr1 |= USART_CR1_PEIE;
113333bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_EIE;
113434891872SAlexandre TORGUE 		cr3 |= USART_CR3_DMAR;
113533bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_DDRE;
113633bb2f6aSErwan Le Ray 	}
113734891872SAlexandre TORGUE 
11381bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
113956f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
11401bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
114156f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
114256f9a76cSErwan Le Ray 					     baud);
11431bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
11441bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
11451bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
11461bcda09dSBich HEMON 		} else {
11471bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
11481bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
11491bcda09dSBich HEMON 		}
11501bcda09dSBich HEMON 
11511bcda09dSBich HEMON 	} else {
11521bcda09dSBich HEMON 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
11531bcda09dSBich HEMON 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
11541bcda09dSBich HEMON 	}
11551bcda09dSBich HEMON 
115612761869SErwan Le Ray 	/* Configure wake up from low power on start bit detection */
11573d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
115812761869SErwan Le Ray 		cr3 &= ~USART_CR3_WUS_MASK;
115912761869SErwan Le Ray 		cr3 |= USART_CR3_WUS_START_BIT;
116012761869SErwan Le Ray 	}
116112761869SErwan Le Ray 
1162ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
1163ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
1164ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
116548a6092fSMaxime Coquelin 
116656f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
116748a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
1168436c9793SErwan Le Ray 
1169436c9793SErwan Le Ray 	/* Handle modem control interrupts */
1170436c9793SErwan Le Ray 	if (UART_ENABLE_MS(port, termios->c_cflag))
1171436c9793SErwan Le Ray 		stm32_usart_enable_ms(port);
1172436c9793SErwan Le Ray 	else
1173436c9793SErwan Le Ray 		stm32_usart_disable_ms(port);
117448a6092fSMaxime Coquelin }
117548a6092fSMaxime Coquelin 
117656f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port)
117748a6092fSMaxime Coquelin {
117848a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
117948a6092fSMaxime Coquelin }
118048a6092fSMaxime Coquelin 
118156f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port)
118248a6092fSMaxime Coquelin {
118348a6092fSMaxime Coquelin }
118448a6092fSMaxime Coquelin 
118556f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port)
118648a6092fSMaxime Coquelin {
118748a6092fSMaxime Coquelin 	return 0;
118848a6092fSMaxime Coquelin }
118948a6092fSMaxime Coquelin 
119056f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags)
119148a6092fSMaxime Coquelin {
119248a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
119348a6092fSMaxime Coquelin 		port->type = PORT_STM32;
119448a6092fSMaxime Coquelin }
119548a6092fSMaxime Coquelin 
119648a6092fSMaxime Coquelin static int
119756f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
119848a6092fSMaxime Coquelin {
119948a6092fSMaxime Coquelin 	/* No user changeable parameters */
120048a6092fSMaxime Coquelin 	return -EINVAL;
120148a6092fSMaxime Coquelin }
120248a6092fSMaxime Coquelin 
120356f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state,
120448a6092fSMaxime Coquelin 			   unsigned int oldstate)
120548a6092fSMaxime Coquelin {
120648a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
120748a6092fSMaxime Coquelin 			struct stm32_port, port);
1208d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1209d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32port->info->cfg;
121018ee37e1SJohan Hovold 	unsigned long flags;
121148a6092fSMaxime Coquelin 
121248a6092fSMaxime Coquelin 	switch (state) {
121348a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
1214fb6dcef6SErwan Le Ray 		pm_runtime_get_sync(port->dev);
121548a6092fSMaxime Coquelin 		break;
121648a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
121748a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
121856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
121948a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
1220fb6dcef6SErwan Le Ray 		pm_runtime_put_sync(port->dev);
122148a6092fSMaxime Coquelin 		break;
122248a6092fSMaxime Coquelin 	}
122348a6092fSMaxime Coquelin }
122448a6092fSMaxime Coquelin 
122548a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
122656f9a76cSErwan Le Ray 	.tx_empty	= stm32_usart_tx_empty,
122756f9a76cSErwan Le Ray 	.set_mctrl	= stm32_usart_set_mctrl,
122856f9a76cSErwan Le Ray 	.get_mctrl	= stm32_usart_get_mctrl,
122956f9a76cSErwan Le Ray 	.stop_tx	= stm32_usart_stop_tx,
123056f9a76cSErwan Le Ray 	.start_tx	= stm32_usart_start_tx,
123156f9a76cSErwan Le Ray 	.throttle	= stm32_usart_throttle,
123256f9a76cSErwan Le Ray 	.unthrottle	= stm32_usart_unthrottle,
123356f9a76cSErwan Le Ray 	.stop_rx	= stm32_usart_stop_rx,
123456f9a76cSErwan Le Ray 	.enable_ms	= stm32_usart_enable_ms,
123556f9a76cSErwan Le Ray 	.break_ctl	= stm32_usart_break_ctl,
123656f9a76cSErwan Le Ray 	.startup	= stm32_usart_startup,
123756f9a76cSErwan Le Ray 	.shutdown	= stm32_usart_shutdown,
12383d82be8bSErwan Le Ray 	.flush_buffer	= stm32_usart_flush_buffer,
123956f9a76cSErwan Le Ray 	.set_termios	= stm32_usart_set_termios,
124056f9a76cSErwan Le Ray 	.pm		= stm32_usart_pm,
124156f9a76cSErwan Le Ray 	.type		= stm32_usart_type,
124256f9a76cSErwan Le Ray 	.release_port	= stm32_usart_release_port,
124356f9a76cSErwan Le Ray 	.request_port	= stm32_usart_request_port,
124456f9a76cSErwan Le Ray 	.config_port	= stm32_usart_config_port,
124556f9a76cSErwan Le Ray 	.verify_port	= stm32_usart_verify_port,
124648a6092fSMaxime Coquelin };
124748a6092fSMaxime Coquelin 
12482aa1bbb2SFabrice Gasnier /*
12492aa1bbb2SFabrice Gasnier  * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
12502aa1bbb2SFabrice Gasnier  * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
12512aa1bbb2SFabrice Gasnier  * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
12522aa1bbb2SFabrice Gasnier  * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
12532aa1bbb2SFabrice Gasnier  */
12542aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
12552aa1bbb2SFabrice Gasnier 
12562aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
12572aa1bbb2SFabrice Gasnier 				  int *ftcfg)
12582aa1bbb2SFabrice Gasnier {
12592aa1bbb2SFabrice Gasnier 	u32 bytes, i;
12602aa1bbb2SFabrice Gasnier 
12612aa1bbb2SFabrice Gasnier 	/* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
12622aa1bbb2SFabrice Gasnier 	if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
12632aa1bbb2SFabrice Gasnier 		bytes = 8;
12642aa1bbb2SFabrice Gasnier 
12652aa1bbb2SFabrice Gasnier 	for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
12662aa1bbb2SFabrice Gasnier 		if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
12672aa1bbb2SFabrice Gasnier 			break;
12682aa1bbb2SFabrice Gasnier 	if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
12692aa1bbb2SFabrice Gasnier 		i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
12702aa1bbb2SFabrice Gasnier 
12712aa1bbb2SFabrice Gasnier 	dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
12722aa1bbb2SFabrice Gasnier 		stm32h7_usart_fifo_thresh_cfg[i]);
12732aa1bbb2SFabrice Gasnier 
12742aa1bbb2SFabrice Gasnier 	/* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
12752aa1bbb2SFabrice Gasnier 	if (i)
12762aa1bbb2SFabrice Gasnier 		*ftcfg = i - 1;
12772aa1bbb2SFabrice Gasnier 	else
12782aa1bbb2SFabrice Gasnier 		*ftcfg = -EINVAL;
12792aa1bbb2SFabrice Gasnier }
12802aa1bbb2SFabrice Gasnier 
128197f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port)
128297f3a085SErwan Le Ray {
128397f3a085SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
128497f3a085SErwan Le Ray }
128597f3a085SErwan Le Ray 
128656f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port,
128748a6092fSMaxime Coquelin 				 struct platform_device *pdev)
128848a6092fSMaxime Coquelin {
128948a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
129048a6092fSMaxime Coquelin 	struct resource *res;
1291e0f2a902SErwan Le Ray 	int ret, irq;
129248a6092fSMaxime Coquelin 
1293e0f2a902SErwan Le Ray 	irq = platform_get_irq(pdev, 0);
1294217b04c6STang Bin 	if (irq < 0)
1295217b04c6STang Bin 		return irq;
129692fc0023SErwan Le Ray 
129748a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
129848a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
129948a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
130048a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
1301d075719eSErwan Le Ray 	port->fifosize	= stm32port->info->cfg.fifosize;
13029feedaa7SDmitry Safonov 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1303e0f2a902SErwan Le Ray 	port->irq = irq;
130456f9a76cSErwan Le Ray 	port->rs485_config = stm32_usart_config_rs485;
13057d8f6861SBich HEMON 
130656f9a76cSErwan Le Ray 	ret = stm32_usart_init_rs485(port, pdev);
1307c150c0f3SLukas Wunner 	if (ret)
1308c150c0f3SLukas Wunner 		return ret;
13097d8f6861SBich HEMON 
13103d530017SAlexandre Torgue 	stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
13113d530017SAlexandre Torgue 		of_property_read_bool(pdev->dev.of_node, "wakeup-source");
13122c58e560SErwan Le Ray 
13133cd66593SMartin Devera 	stm32port->swap = stm32port->info->cfg.has_swap &&
13143cd66593SMartin Devera 		of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
13153cd66593SMartin Devera 
1316351a762aSGerald Baeza 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
13172aa1bbb2SFabrice Gasnier 	if (stm32port->fifoen) {
13182aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "rx-threshold",
13192aa1bbb2SFabrice Gasnier 				      &stm32port->rxftcfg);
13202aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "tx-threshold",
13212aa1bbb2SFabrice Gasnier 				      &stm32port->txftcfg);
13222aa1bbb2SFabrice Gasnier 	}
132348a6092fSMaxime Coquelin 
13243d881e32STang Bin 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
132548a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
132648a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
132748a6092fSMaxime Coquelin 	port->mapbase = res->start;
132848a6092fSMaxime Coquelin 
132948a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
133048a6092fSMaxime Coquelin 
133148a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
133248a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
133348a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
133448a6092fSMaxime Coquelin 
133548a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
133648a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
133748a6092fSMaxime Coquelin 	if (ret)
133848a6092fSMaxime Coquelin 		return ret;
133948a6092fSMaxime Coquelin 
134048a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1341ada80043SFabrice Gasnier 	if (!stm32port->port.uartclk) {
134248a6092fSMaxime Coquelin 		ret = -EINVAL;
13436cf61b9bSManivannan Sadhasivam 		goto err_clk;
1344ada80043SFabrice Gasnier 	}
134548a6092fSMaxime Coquelin 
13466cf61b9bSManivannan Sadhasivam 	stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
13476cf61b9bSManivannan Sadhasivam 	if (IS_ERR(stm32port->gpios)) {
13486cf61b9bSManivannan Sadhasivam 		ret = PTR_ERR(stm32port->gpios);
13496cf61b9bSManivannan Sadhasivam 		goto err_clk;
13506cf61b9bSManivannan Sadhasivam 	}
13516cf61b9bSManivannan Sadhasivam 
13529359369aSErwan Le Ray 	/*
13539359369aSErwan Le Ray 	 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
13549359369aSErwan Le Ray 	 * properties should not be specified.
13559359369aSErwan Le Ray 	 */
13566cf61b9bSManivannan Sadhasivam 	if (stm32port->hw_flow_control) {
13576cf61b9bSManivannan Sadhasivam 		if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
13586cf61b9bSManivannan Sadhasivam 		    mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
13596cf61b9bSManivannan Sadhasivam 			dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
13606cf61b9bSManivannan Sadhasivam 			ret = -EINVAL;
13616cf61b9bSManivannan Sadhasivam 			goto err_clk;
13626cf61b9bSManivannan Sadhasivam 		}
13636cf61b9bSManivannan Sadhasivam 	}
13646cf61b9bSManivannan Sadhasivam 
13656cf61b9bSManivannan Sadhasivam 	return ret;
13666cf61b9bSManivannan Sadhasivam 
13676cf61b9bSManivannan Sadhasivam err_clk:
13686cf61b9bSManivannan Sadhasivam 	clk_disable_unprepare(stm32port->clk);
13696cf61b9bSManivannan Sadhasivam 
137048a6092fSMaxime Coquelin 	return ret;
137148a6092fSMaxime Coquelin }
137248a6092fSMaxime Coquelin 
137356f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
137448a6092fSMaxime Coquelin {
137548a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
137648a6092fSMaxime Coquelin 	int id;
137748a6092fSMaxime Coquelin 
137848a6092fSMaxime Coquelin 	if (!np)
137948a6092fSMaxime Coquelin 		return NULL;
138048a6092fSMaxime Coquelin 
138148a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
1382e5707915SGerald Baeza 	if (id < 0) {
1383e5707915SGerald Baeza 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1384e5707915SGerald Baeza 		return NULL;
1385e5707915SGerald Baeza 	}
138648a6092fSMaxime Coquelin 
138748a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
138848a6092fSMaxime Coquelin 		return NULL;
138948a6092fSMaxime Coquelin 
13906fd9fffbSErwan Le Ray 	stm32_ports[id].hw_flow_control =
13916fd9fffbSErwan Le Ray 		of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
13926fd9fffbSErwan Le Ray 		of_property_read_bool (np, "uart-has-rtscts");
139348a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
13944cc0ed62SErwan Le Ray 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1395d0a6a7bcSErwan Le Ray 	stm32_ports[id].cr3_irq = 0;
1396e5707915SGerald Baeza 	stm32_ports[id].last_res = RX_BUF_L;
139748a6092fSMaxime Coquelin 	return &stm32_ports[id];
139848a6092fSMaxime Coquelin }
139948a6092fSMaxime Coquelin 
140048a6092fSMaxime Coquelin #ifdef CONFIG_OF
140148a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
1402ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
1403ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1404270e5a74SFabrice Gasnier 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
140548a6092fSMaxime Coquelin 	{},
140648a6092fSMaxime Coquelin };
140748a6092fSMaxime Coquelin 
140848a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
140948a6092fSMaxime Coquelin #endif
141048a6092fSMaxime Coquelin 
1411a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1412a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1413a7770a4bSErwan Le Ray {
1414a7770a4bSErwan Le Ray 	if (stm32port->rx_buf)
1415a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1416a7770a4bSErwan Le Ray 				  stm32port->rx_dma_buf);
1417a7770a4bSErwan Le Ray }
1418a7770a4bSErwan Le Ray 
141956f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
142034891872SAlexandre TORGUE 				       struct platform_device *pdev)
142134891872SAlexandre TORGUE {
1422d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
142334891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
142434891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
142534891872SAlexandre TORGUE 	struct dma_slave_config config;
142634891872SAlexandre TORGUE 	int ret;
142734891872SAlexandre TORGUE 
1428e359b441SJohan Hovold 	/*
1429e359b441SJohan Hovold 	 * Using DMA and threaded handler for the console could lead to
1430e359b441SJohan Hovold 	 * deadlocks.
1431e359b441SJohan Hovold 	 */
1432e359b441SJohan Hovold 	if (uart_console(port))
1433e359b441SJohan Hovold 		return -ENODEV;
1434e359b441SJohan Hovold 
143559bd4eedSTang Bin 	stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
143634891872SAlexandre TORGUE 					       &stm32port->rx_dma_buf,
143734891872SAlexandre TORGUE 					       GFP_KERNEL);
1438a7770a4bSErwan Le Ray 	if (!stm32port->rx_buf)
1439a7770a4bSErwan Le Ray 		return -ENOMEM;
144034891872SAlexandre TORGUE 
144134891872SAlexandre TORGUE 	/* Configure DMA channel */
144234891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
14438e5481d9SArnd Bergmann 	config.src_addr = port->mapbase + ofs->rdr;
144434891872SAlexandre TORGUE 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
144534891872SAlexandre TORGUE 
144634891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
144734891872SAlexandre TORGUE 	if (ret < 0) {
144834891872SAlexandre TORGUE 		dev_err(dev, "rx dma channel config failed\n");
1449a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
1450a7770a4bSErwan Le Ray 		return ret;
145134891872SAlexandre TORGUE 	}
145234891872SAlexandre TORGUE 
145334891872SAlexandre TORGUE 	return 0;
1454a7770a4bSErwan Le Ray }
145534891872SAlexandre TORGUE 
1456a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1457a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1458a7770a4bSErwan Le Ray {
1459a7770a4bSErwan Le Ray 	if (stm32port->tx_buf)
1460a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1461a7770a4bSErwan Le Ray 				  stm32port->tx_dma_buf);
146234891872SAlexandre TORGUE }
146334891872SAlexandre TORGUE 
146456f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
146534891872SAlexandre TORGUE 				       struct platform_device *pdev)
146634891872SAlexandre TORGUE {
1467d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
146834891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
146934891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
147034891872SAlexandre TORGUE 	struct dma_slave_config config;
147134891872SAlexandre TORGUE 	int ret;
147234891872SAlexandre TORGUE 
147359bd4eedSTang Bin 	stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
147434891872SAlexandre TORGUE 					       &stm32port->tx_dma_buf,
147534891872SAlexandre TORGUE 					       GFP_KERNEL);
1476a7770a4bSErwan Le Ray 	if (!stm32port->tx_buf)
1477a7770a4bSErwan Le Ray 		return -ENOMEM;
147834891872SAlexandre TORGUE 
147934891872SAlexandre TORGUE 	/* Configure DMA channel */
148034891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
14818e5481d9SArnd Bergmann 	config.dst_addr = port->mapbase + ofs->tdr;
148234891872SAlexandre TORGUE 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
148334891872SAlexandre TORGUE 
148434891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
148534891872SAlexandre TORGUE 	if (ret < 0) {
148634891872SAlexandre TORGUE 		dev_err(dev, "tx dma channel config failed\n");
1487a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1488a7770a4bSErwan Le Ray 		return ret;
148934891872SAlexandre TORGUE 	}
149034891872SAlexandre TORGUE 
149134891872SAlexandre TORGUE 	return 0;
149234891872SAlexandre TORGUE }
149334891872SAlexandre TORGUE 
149456f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev)
149548a6092fSMaxime Coquelin {
149648a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
1497ada8618fSAlexandre TORGUE 	int ret;
149848a6092fSMaxime Coquelin 
149956f9a76cSErwan Le Ray 	stm32port = stm32_usart_of_get_port(pdev);
150048a6092fSMaxime Coquelin 	if (!stm32port)
150148a6092fSMaxime Coquelin 		return -ENODEV;
150248a6092fSMaxime Coquelin 
1503d825f0beSStephen Boyd 	stm32port->info = of_device_get_match_data(&pdev->dev);
1504d825f0beSStephen Boyd 	if (!stm32port->info)
1505ada8618fSAlexandre TORGUE 		return -EINVAL;
1506ada8618fSAlexandre TORGUE 
150756f9a76cSErwan Le Ray 	ret = stm32_usart_init_port(stm32port, pdev);
150848a6092fSMaxime Coquelin 	if (ret)
150948a6092fSMaxime Coquelin 		return ret;
151048a6092fSMaxime Coquelin 
15113d530017SAlexandre Torgue 	if (stm32port->wakeup_src) {
15123d530017SAlexandre Torgue 		device_set_wakeup_capable(&pdev->dev, true);
15133d530017SAlexandre Torgue 		ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
15145297f274SErwan Le Ray 		if (ret)
1515a7770a4bSErwan Le Ray 			goto err_deinit_port;
1516270e5a74SFabrice Gasnier 	}
1517270e5a74SFabrice Gasnier 
1518a7770a4bSErwan Le Ray 	stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
1519a7770a4bSErwan Le Ray 	if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) {
1520a7770a4bSErwan Le Ray 		ret = -EPROBE_DEFER;
1521a7770a4bSErwan Le Ray 		goto err_wakeirq;
1522a7770a4bSErwan Le Ray 	}
1523a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1524a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->rx_ch))
1525a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
152634891872SAlexandre TORGUE 
1527a7770a4bSErwan Le Ray 	stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1528a7770a4bSErwan Le Ray 	if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1529a7770a4bSErwan Le Ray 		ret = -EPROBE_DEFER;
1530a7770a4bSErwan Le Ray 		goto err_dma_rx;
1531a7770a4bSErwan Le Ray 	}
1532a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1533a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->tx_ch))
1534a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1535a7770a4bSErwan Le Ray 
1536a7770a4bSErwan Le Ray 	if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1537a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1538a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->rx_ch);
1539a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
1540a7770a4bSErwan Le Ray 	}
1541a7770a4bSErwan Le Ray 
1542a7770a4bSErwan Le Ray 	if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1543a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1544a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->tx_ch);
1545a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1546a7770a4bSErwan Le Ray 	}
1547a7770a4bSErwan Le Ray 
1548a7770a4bSErwan Le Ray 	if (!stm32port->rx_ch)
1549a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1550a7770a4bSErwan Le Ray 	if (!stm32port->tx_ch)
1551a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
155234891872SAlexandre TORGUE 
155348a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
155448a6092fSMaxime Coquelin 
1555fb6dcef6SErwan Le Ray 	pm_runtime_get_noresume(&pdev->dev);
1556fb6dcef6SErwan Le Ray 	pm_runtime_set_active(&pdev->dev);
1557fb6dcef6SErwan Le Ray 	pm_runtime_enable(&pdev->dev);
155887fd0741SErwan Le Ray 
155987fd0741SErwan Le Ray 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
156087fd0741SErwan Le Ray 	if (ret)
156187fd0741SErwan Le Ray 		goto err_port;
156287fd0741SErwan Le Ray 
1563fb6dcef6SErwan Le Ray 	pm_runtime_put_sync(&pdev->dev);
1564fb6dcef6SErwan Le Ray 
156548a6092fSMaxime Coquelin 	return 0;
1566ada80043SFabrice Gasnier 
156787fd0741SErwan Le Ray err_port:
156887fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
156987fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
157087fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
157187fd0741SErwan Le Ray 
157287fd0741SErwan Le Ray 	if (stm32port->tx_ch) {
1573a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
157487fd0741SErwan Le Ray 		dma_release_channel(stm32port->tx_ch);
157587fd0741SErwan Le Ray 	}
157687fd0741SErwan Le Ray 
1577a7770a4bSErwan Le Ray 	if (stm32port->rx_ch)
1578a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
157987fd0741SErwan Le Ray 
1580a7770a4bSErwan Le Ray err_dma_rx:
1581a7770a4bSErwan Le Ray 	if (stm32port->rx_ch)
1582a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->rx_ch);
1583a7770a4bSErwan Le Ray 
1584a7770a4bSErwan Le Ray err_wakeirq:
15853d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
15865297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
15875297f274SErwan Le Ray 
1588a7770a4bSErwan Le Ray err_deinit_port:
15893d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
15903d530017SAlexandre Torgue 		device_set_wakeup_capable(&pdev->dev, false);
1591270e5a74SFabrice Gasnier 
159297f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32port);
1593ada80043SFabrice Gasnier 
1594ada80043SFabrice Gasnier 	return ret;
159548a6092fSMaxime Coquelin }
159648a6092fSMaxime Coquelin 
159756f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev)
159848a6092fSMaxime Coquelin {
159948a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
1600511c7b1bSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1601d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1602fb6dcef6SErwan Le Ray 	int err;
160333bb2f6aSErwan Le Ray 	u32 cr3;
1604fb6dcef6SErwan Le Ray 
1605fb6dcef6SErwan Le Ray 	pm_runtime_get_sync(&pdev->dev);
160687fd0741SErwan Le Ray 	err = uart_remove_one_port(&stm32_usart_driver, port);
160787fd0741SErwan Le Ray 	if (err)
160887fd0741SErwan Le Ray 		return(err);
160987fd0741SErwan Le Ray 
161087fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
161187fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
161287fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
161334891872SAlexandre TORGUE 
161433bb2f6aSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
161533bb2f6aSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
161633bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_EIE;
161733bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_DMAR;
161833bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_DDRE;
161933bb2f6aSErwan Le Ray 	writel_relaxed(cr3, port->membase + ofs->cr3);
162034891872SAlexandre TORGUE 
162187fd0741SErwan Le Ray 	if (stm32_port->tx_ch) {
1622a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32_port, pdev);
162334891872SAlexandre TORGUE 		dma_release_channel(stm32_port->tx_ch);
162487fd0741SErwan Le Ray 	}
162534891872SAlexandre TORGUE 
1626a7770a4bSErwan Le Ray 	if (stm32_port->rx_ch) {
1627a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1628a7770a4bSErwan Le Ray 		dma_release_channel(stm32_port->rx_ch);
1629a7770a4bSErwan Le Ray 	}
1630a7770a4bSErwan Le Ray 
1631a7770a4bSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1632511c7b1bSAlexandre TORGUE 
16333d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
16345297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
1635270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
16365297f274SErwan Le Ray 	}
1637270e5a74SFabrice Gasnier 
163897f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32_port);
163948a6092fSMaxime Coquelin 
164087fd0741SErwan Le Ray 	return 0;
164148a6092fSMaxime Coquelin }
164248a6092fSMaxime Coquelin 
164348a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE
1644*3f8bab17SJiri Slaby static void stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
164548a6092fSMaxime Coquelin {
1646ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1647d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1648ada8618fSAlexandre TORGUE 
1649ada8618fSAlexandre TORGUE 	while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
165048a6092fSMaxime Coquelin 		cpu_relax();
165148a6092fSMaxime Coquelin 
1652ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
165348a6092fSMaxime Coquelin }
165448a6092fSMaxime Coquelin 
165556f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s,
165692fc0023SErwan Le Ray 				      unsigned int cnt)
165748a6092fSMaxime Coquelin {
165848a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
1659ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1660d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1661d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
166248a6092fSMaxime Coquelin 	unsigned long flags;
166348a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
166448a6092fSMaxime Coquelin 	int locked = 1;
166548a6092fSMaxime Coquelin 
1666cea37afdSJohan Hovold 	if (oops_in_progress)
1667cea37afdSJohan Hovold 		locked = spin_trylock_irqsave(&port->lock, flags);
166848a6092fSMaxime Coquelin 	else
1669cea37afdSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
167048a6092fSMaxime Coquelin 
167187f1f809SAlexandre TORGUE 	/* Save and disable interrupts, enable the transmitter */
1672ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
167348a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
167487f1f809SAlexandre TORGUE 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1675ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
167648a6092fSMaxime Coquelin 
167756f9a76cSErwan Le Ray 	uart_console_write(port, s, cnt, stm32_usart_console_putchar);
167848a6092fSMaxime Coquelin 
167948a6092fSMaxime Coquelin 	/* Restore interrupt state */
1680ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
168148a6092fSMaxime Coquelin 
168248a6092fSMaxime Coquelin 	if (locked)
1683cea37afdSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
168448a6092fSMaxime Coquelin }
168548a6092fSMaxime Coquelin 
168656f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options)
168748a6092fSMaxime Coquelin {
168848a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
168948a6092fSMaxime Coquelin 	int baud = 9600;
169048a6092fSMaxime Coquelin 	int bits = 8;
169148a6092fSMaxime Coquelin 	int parity = 'n';
169248a6092fSMaxime Coquelin 	int flow = 'n';
169348a6092fSMaxime Coquelin 
169448a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
169548a6092fSMaxime Coquelin 		return -ENODEV;
169648a6092fSMaxime Coquelin 
169748a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
169848a6092fSMaxime Coquelin 
169948a6092fSMaxime Coquelin 	/*
170048a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
170148a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
170248a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
170348a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
170448a6092fSMaxime Coquelin 	 */
170592fc0023SErwan Le Ray 	if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
170648a6092fSMaxime Coquelin 		return -ENXIO;
170748a6092fSMaxime Coquelin 
170848a6092fSMaxime Coquelin 	if (options)
170948a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
171048a6092fSMaxime Coquelin 
171148a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
171248a6092fSMaxime Coquelin }
171348a6092fSMaxime Coquelin 
171448a6092fSMaxime Coquelin static struct console stm32_console = {
171548a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
171648a6092fSMaxime Coquelin 	.device		= uart_console_device,
171756f9a76cSErwan Le Ray 	.write		= stm32_usart_console_write,
171856f9a76cSErwan Le Ray 	.setup		= stm32_usart_console_setup,
171948a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
172048a6092fSMaxime Coquelin 	.index		= -1,
172148a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
172248a6092fSMaxime Coquelin };
172348a6092fSMaxime Coquelin 
172448a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
172548a6092fSMaxime Coquelin 
172648a6092fSMaxime Coquelin #else
172748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
172848a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
172948a6092fSMaxime Coquelin 
173048a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
173148a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
173248a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
173348a6092fSMaxime Coquelin 	.major		= 0,
173448a6092fSMaxime Coquelin 	.minor		= 0,
173548a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
173648a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
173748a6092fSMaxime Coquelin };
173848a6092fSMaxime Coquelin 
17396eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1740fe94347dSErwan Le Ray 						       bool enable)
1741270e5a74SFabrice Gasnier {
1742270e5a74SFabrice Gasnier 	struct stm32_port *stm32_port = to_stm32_port(port);
1743d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
17446eeb348cSErwan Le Ray 	struct tty_port *tport = &port->state->port;
17456eeb348cSErwan Le Ray 	int ret;
17466333a485SErwan Le Ray 	unsigned int size;
17476333a485SErwan Le Ray 	unsigned long flags;
1748270e5a74SFabrice Gasnier 
17496eeb348cSErwan Le Ray 	if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
17506eeb348cSErwan Le Ray 		return 0;
1751270e5a74SFabrice Gasnier 
175212761869SErwan Le Ray 	/*
175312761869SErwan Le Ray 	 * Enable low-power wake-up and wake-up irq if argument is set to
175412761869SErwan Le Ray 	 * "enable", disable low-power wake-up and wake-up irq otherwise
175512761869SErwan Le Ray 	 */
1756270e5a74SFabrice Gasnier 	if (enable) {
175756f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
175812761869SErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
17597547d9abSErwan Le Ray 		mctrl_gpio_enable_irq_wake(stm32_port->gpios);
17606eeb348cSErwan Le Ray 
17616eeb348cSErwan Le Ray 		/*
17626eeb348cSErwan Le Ray 		 * When DMA is used for reception, it must be disabled before
17636eeb348cSErwan Le Ray 		 * entering low-power mode and re-enabled when exiting from
17646eeb348cSErwan Le Ray 		 * low-power mode.
17656eeb348cSErwan Le Ray 		 */
17666eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
17676333a485SErwan Le Ray 			spin_lock_irqsave(&port->lock, flags);
17686333a485SErwan Le Ray 			/* Avoid race with RX IRQ when DMAR is cleared */
17696eeb348cSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
17706333a485SErwan Le Ray 			/* Poll data from DMA RX buffer if any */
17716333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, true);
17726333a485SErwan Le Ray 			dmaengine_terminate_async(stm32_port->rx_ch);
17736333a485SErwan Le Ray 			uart_unlock_and_check_sysrq_irqrestore(port, flags);
17746333a485SErwan Le Ray 			if (size)
17756333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
17766eeb348cSErwan Le Ray 		}
17776eeb348cSErwan Le Ray 
17786eeb348cSErwan Le Ray 		/* Poll data from RX FIFO if any */
17796eeb348cSErwan Le Ray 		stm32_usart_receive_chars(port, false);
1780270e5a74SFabrice Gasnier 	} else {
17816eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
17826eeb348cSErwan Le Ray 			ret = stm32_usart_start_rx_dma_cyclic(port);
17836eeb348cSErwan Le Ray 			if (ret)
17846eeb348cSErwan Le Ray 				return ret;
17856eeb348cSErwan Le Ray 		}
17867547d9abSErwan Le Ray 		mctrl_gpio_disable_irq_wake(stm32_port->gpios);
178756f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
178812761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
1789270e5a74SFabrice Gasnier 	}
17906eeb348cSErwan Le Ray 
17916eeb348cSErwan Le Ray 	return 0;
1792270e5a74SFabrice Gasnier }
1793270e5a74SFabrice Gasnier 
179456f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
1795270e5a74SFabrice Gasnier {
1796270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
17976eeb348cSErwan Le Ray 	int ret;
1798270e5a74SFabrice Gasnier 
1799270e5a74SFabrice Gasnier 	uart_suspend_port(&stm32_usart_driver, port);
1800270e5a74SFabrice Gasnier 
18016eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
18026eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, true);
18036eeb348cSErwan Le Ray 		if (ret)
18046eeb348cSErwan Le Ray 			return ret;
18056eeb348cSErwan Le Ray 	}
1806270e5a74SFabrice Gasnier 
180755484fccSErwan Le Ray 	/*
180855484fccSErwan Le Ray 	 * When "no_console_suspend" is enabled, keep the pinctrl default state
180955484fccSErwan Le Ray 	 * and rely on bootloader stage to restore this state upon resume.
181055484fccSErwan Le Ray 	 * Otherwise, apply the idle or sleep states depending on wakeup
181155484fccSErwan Le Ray 	 * capabilities.
181255484fccSErwan Le Ray 	 */
181355484fccSErwan Le Ray 	if (console_suspend_enabled || !uart_console(port)) {
18141631eeeaSErwan Le Ray 		if (device_may_wakeup(dev) || device_wakeup_path(dev))
181555484fccSErwan Le Ray 			pinctrl_pm_select_idle_state(dev);
181655484fccSErwan Le Ray 		else
181794616d9aSErwan Le Ray 			pinctrl_pm_select_sleep_state(dev);
181855484fccSErwan Le Ray 	}
181994616d9aSErwan Le Ray 
1820270e5a74SFabrice Gasnier 	return 0;
1821270e5a74SFabrice Gasnier }
1822270e5a74SFabrice Gasnier 
182356f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
1824270e5a74SFabrice Gasnier {
1825270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
18266eeb348cSErwan Le Ray 	int ret;
1827270e5a74SFabrice Gasnier 
182894616d9aSErwan Le Ray 	pinctrl_pm_select_default_state(dev);
182994616d9aSErwan Le Ray 
18306eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
18316eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, false);
18326eeb348cSErwan Le Ray 		if (ret)
18336eeb348cSErwan Le Ray 			return ret;
18346eeb348cSErwan Le Ray 	}
1835270e5a74SFabrice Gasnier 
1836270e5a74SFabrice Gasnier 	return uart_resume_port(&stm32_usart_driver, port);
1837270e5a74SFabrice Gasnier }
1838270e5a74SFabrice Gasnier 
183956f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
1840fb6dcef6SErwan Le Ray {
1841fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1842fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1843fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1844fb6dcef6SErwan Le Ray 
1845fb6dcef6SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
1846fb6dcef6SErwan Le Ray 
1847fb6dcef6SErwan Le Ray 	return 0;
1848fb6dcef6SErwan Le Ray }
1849fb6dcef6SErwan Le Ray 
185056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
1851fb6dcef6SErwan Le Ray {
1852fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1853fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1854fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1855fb6dcef6SErwan Le Ray 
1856fb6dcef6SErwan Le Ray 	return clk_prepare_enable(stm32port->clk);
1857fb6dcef6SErwan Le Ray }
1858fb6dcef6SErwan Le Ray 
1859270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = {
186056f9a76cSErwan Le Ray 	SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
186156f9a76cSErwan Le Ray 			   stm32_usart_runtime_resume, NULL)
186256f9a76cSErwan Le Ray 	SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
186356f9a76cSErwan Le Ray 				stm32_usart_serial_resume)
1864270e5a74SFabrice Gasnier };
1865270e5a74SFabrice Gasnier 
186648a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
186756f9a76cSErwan Le Ray 	.probe		= stm32_usart_serial_probe,
186856f9a76cSErwan Le Ray 	.remove		= stm32_usart_serial_remove,
186948a6092fSMaxime Coquelin 	.driver	= {
187048a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
1871270e5a74SFabrice Gasnier 		.pm	= &stm32_serial_pm_ops,
187248a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
187348a6092fSMaxime Coquelin 	},
187448a6092fSMaxime Coquelin };
187548a6092fSMaxime Coquelin 
187656f9a76cSErwan Le Ray static int __init stm32_usart_init(void)
187748a6092fSMaxime Coquelin {
187848a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
187948a6092fSMaxime Coquelin 	int ret;
188048a6092fSMaxime Coquelin 
188148a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
188248a6092fSMaxime Coquelin 
188348a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
188448a6092fSMaxime Coquelin 	if (ret)
188548a6092fSMaxime Coquelin 		return ret;
188648a6092fSMaxime Coquelin 
188748a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
188848a6092fSMaxime Coquelin 	if (ret)
188948a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
189048a6092fSMaxime Coquelin 
189148a6092fSMaxime Coquelin 	return ret;
189248a6092fSMaxime Coquelin }
189348a6092fSMaxime Coquelin 
189456f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void)
189548a6092fSMaxime Coquelin {
189648a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
189748a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
189848a6092fSMaxime Coquelin }
189948a6092fSMaxime Coquelin 
190056f9a76cSErwan Le Ray module_init(stm32_usart_init);
190156f9a76cSErwan Le Ray module_exit(stm32_usart_exit);
190248a6092fSMaxime Coquelin 
190348a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
190448a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
190548a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
1906