xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 3bcea529b295a993b1b05db63f245ae8030c5acf)
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);
401f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch);
4148a6092fSMaxime Coquelin 
4248a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
4348a6092fSMaxime Coquelin {
4448a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
4548a6092fSMaxime Coquelin }
4648a6092fSMaxime Coquelin 
4756f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
4848a6092fSMaxime Coquelin {
4948a6092fSMaxime Coquelin 	u32 val;
5048a6092fSMaxime Coquelin 
5148a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
5248a6092fSMaxime Coquelin 	val |= bits;
5348a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
5448a6092fSMaxime Coquelin }
5548a6092fSMaxime Coquelin 
5656f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
5748a6092fSMaxime Coquelin {
5848a6092fSMaxime Coquelin 	u32 val;
5948a6092fSMaxime Coquelin 
6048a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
6148a6092fSMaxime Coquelin 	val &= ~bits;
6248a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
6348a6092fSMaxime Coquelin }
6448a6092fSMaxime Coquelin 
6556f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
661bcda09dSBich HEMON 					 u32 delay_DDE, u32 baud)
671bcda09dSBich HEMON {
681bcda09dSBich HEMON 	u32 rs485_deat_dedt;
691bcda09dSBich HEMON 	u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
701bcda09dSBich HEMON 	bool over8;
711bcda09dSBich HEMON 
721bcda09dSBich HEMON 	*cr3 |= USART_CR3_DEM;
731bcda09dSBich HEMON 	over8 = *cr1 & USART_CR1_OVER8;
741bcda09dSBich HEMON 
751bcda09dSBich HEMON 	if (over8)
761bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 8;
771bcda09dSBich HEMON 	else
781bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 16;
791bcda09dSBich HEMON 
801bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
811bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
821bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
831bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
841bcda09dSBich HEMON 			   USART_CR1_DEAT_MASK;
851bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
861bcda09dSBich HEMON 
871bcda09dSBich HEMON 	if (over8)
881bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 8;
891bcda09dSBich HEMON 	else
901bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 16;
911bcda09dSBich HEMON 
921bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
931bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
941bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
951bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
961bcda09dSBich HEMON 			   USART_CR1_DEDT_MASK;
971bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
981bcda09dSBich HEMON }
991bcda09dSBich HEMON 
10056f9a76cSErwan Le Ray static int stm32_usart_config_rs485(struct uart_port *port,
1011bcda09dSBich HEMON 				    struct serial_rs485 *rs485conf)
1021bcda09dSBich HEMON {
1031bcda09dSBich HEMON 	struct stm32_port *stm32_port = to_stm32_port(port);
104d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
105d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1061bcda09dSBich HEMON 	u32 usartdiv, baud, cr1, cr3;
1071bcda09dSBich HEMON 	bool over8;
1081bcda09dSBich HEMON 
10956f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1101bcda09dSBich HEMON 
1111bcda09dSBich HEMON 	rs485conf->flags |= SER_RS485_RX_DURING_TX;
1121bcda09dSBich HEMON 
1131bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
1141bcda09dSBich HEMON 		cr1 = readl_relaxed(port->membase + ofs->cr1);
1151bcda09dSBich HEMON 		cr3 = readl_relaxed(port->membase + ofs->cr3);
1161bcda09dSBich HEMON 		usartdiv = readl_relaxed(port->membase + ofs->brr);
1171bcda09dSBich HEMON 		usartdiv = usartdiv & GENMASK(15, 0);
1181bcda09dSBich HEMON 		over8 = cr1 & USART_CR1_OVER8;
1191bcda09dSBich HEMON 
1201bcda09dSBich HEMON 		if (over8)
1211bcda09dSBich HEMON 			usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
1221bcda09dSBich HEMON 				   << USART_BRR_04_R_SHIFT;
1231bcda09dSBich HEMON 
1241bcda09dSBich HEMON 		baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
12556f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
1261bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
12756f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
12856f9a76cSErwan Le Ray 					     baud);
1291bcda09dSBich HEMON 
130f633eb29SLino Sanfilippo 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND)
1311bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
132f633eb29SLino Sanfilippo 		else
1331bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
1341bcda09dSBich HEMON 
1351bcda09dSBich HEMON 		writel_relaxed(cr3, port->membase + ofs->cr3);
1361bcda09dSBich HEMON 		writel_relaxed(cr1, port->membase + ofs->cr1);
1371bcda09dSBich HEMON 	} else {
13856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3,
13956f9a76cSErwan Le Ray 				     USART_CR3_DEM | USART_CR3_DEP);
14056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1,
1411bcda09dSBich HEMON 				     USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1421bcda09dSBich HEMON 	}
1431bcda09dSBich HEMON 
14456f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1451bcda09dSBich HEMON 
1461bcda09dSBich HEMON 	return 0;
1471bcda09dSBich HEMON }
1481bcda09dSBich HEMON 
14956f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port,
1501bcda09dSBich HEMON 				  struct platform_device *pdev)
1511bcda09dSBich HEMON {
1521bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
1531bcda09dSBich HEMON 
1541bcda09dSBich HEMON 	rs485conf->flags = 0;
1551bcda09dSBich HEMON 	rs485conf->delay_rts_before_send = 0;
1561bcda09dSBich HEMON 	rs485conf->delay_rts_after_send = 0;
1571bcda09dSBich HEMON 
1581bcda09dSBich HEMON 	if (!pdev->dev.of_node)
1591bcda09dSBich HEMON 		return -ENODEV;
1601bcda09dSBich HEMON 
161c150c0f3SLukas Wunner 	return uart_get_rs485_mode(port);
1621bcda09dSBich HEMON }
1631bcda09dSBich HEMON 
16433bb2f6aSErwan Le Ray static bool stm32_usart_rx_dma_enabled(struct uart_port *port)
16534891872SAlexandre TORGUE {
16634891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
167d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
16833bb2f6aSErwan Le Ray 
16933bb2f6aSErwan Le Ray 	if (!stm32_port->rx_ch)
17033bb2f6aSErwan Le Ray 		return false;
17133bb2f6aSErwan Le Ray 
17233bb2f6aSErwan Le Ray 	return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR);
17333bb2f6aSErwan Le Ray }
17433bb2f6aSErwan Le Ray 
17533bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */
17633bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
17733bb2f6aSErwan Le Ray {
17833bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
17933bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
18034891872SAlexandre TORGUE 
18134891872SAlexandre TORGUE 	*sr = readl_relaxed(port->membase + ofs->isr);
18233bb2f6aSErwan Le Ray 	/* Get pending characters in RDR or FIFO */
18333bb2f6aSErwan Le Ray 	if (*sr & USART_SR_RXNE) {
18433bb2f6aSErwan Le Ray 		/* Get all pending characters from the RDR or the FIFO when using interrupts */
18533bb2f6aSErwan Le Ray 		if (!stm32_usart_rx_dma_enabled(port))
18633bb2f6aSErwan Le Ray 			return true;
18734891872SAlexandre TORGUE 
18833bb2f6aSErwan Le Ray 		/* Handle only RX data errors when using DMA */
18933bb2f6aSErwan Le Ray 		if (*sr & USART_SR_ERR_MASK)
19033bb2f6aSErwan Le Ray 			return true;
19134891872SAlexandre TORGUE 	}
19234891872SAlexandre TORGUE 
19333bb2f6aSErwan Le Ray 	return false;
19433bb2f6aSErwan Le Ray }
19533bb2f6aSErwan Le Ray 
19633bb2f6aSErwan Le Ray static unsigned long stm32_usart_get_char_pio(struct uart_port *port)
19734891872SAlexandre TORGUE {
19834891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
199d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
20034891872SAlexandre TORGUE 	unsigned long c;
20134891872SAlexandre TORGUE 
2026c5962f3SErwan Le Ray 	c = readl_relaxed(port->membase + ofs->rdr);
20333bb2f6aSErwan Le Ray 	/* Apply RDR data mask */
2046c5962f3SErwan Le Ray 	c &= stm32_port->rdr_mask;
2056c5962f3SErwan Le Ray 
2066c5962f3SErwan Le Ray 	return c;
20734891872SAlexandre TORGUE }
20834891872SAlexandre TORGUE 
2096333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
21048a6092fSMaxime Coquelin {
211ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
212d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
21333bb2f6aSErwan Le Ray 	unsigned long c;
2146333a485SErwan Le Ray 	unsigned int size = 0;
21548a6092fSMaxime Coquelin 	u32 sr;
21648a6092fSMaxime Coquelin 	char flag;
21748a6092fSMaxime Coquelin 
21833bb2f6aSErwan Le Ray 	while (stm32_usart_pending_rx_pio(port, &sr)) {
21948a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
22048a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
22148a6092fSMaxime Coquelin 
2224f01d833SErwan Le Ray 		/*
2234f01d833SErwan Le Ray 		 * Status bits has to be cleared before reading the RDR:
2244f01d833SErwan Le Ray 		 * In FIFO mode, reading the RDR will pop the next data
2254f01d833SErwan Le Ray 		 * (if any) along with its status bits into the SR.
2264f01d833SErwan Le Ray 		 * Not doing so leads to misalignement between RDR and SR,
2274f01d833SErwan Le Ray 		 * and clear status bits of the next rx data.
2284f01d833SErwan Le Ray 		 *
2294f01d833SErwan Le Ray 		 * Clear errors flags for stm32f7 and stm32h7 compatible
2304f01d833SErwan Le Ray 		 * devices. On stm32f4 compatible devices, the error bit is
2314f01d833SErwan Le Ray 		 * cleared by the sequence [read SR - read DR].
2324f01d833SErwan Le Ray 		 */
2334f01d833SErwan Le Ray 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
2341250ed71SFabrice Gasnier 			writel_relaxed(sr & USART_SR_ERR_MASK,
2351250ed71SFabrice Gasnier 				       port->membase + ofs->icr);
2364f01d833SErwan Le Ray 
23733bb2f6aSErwan Le Ray 		c = stm32_usart_get_char_pio(port);
2384f01d833SErwan Le Ray 		port->icount.rx++;
2396333a485SErwan Le Ray 		size++;
24048a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
2414f01d833SErwan Le Ray 			if (sr & USART_SR_ORE) {
24248a6092fSMaxime Coquelin 				port->icount.overrun++;
24348a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
24448a6092fSMaxime Coquelin 				port->icount.parity++;
24548a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
2464f01d833SErwan Le Ray 				/* Break detection if character is null */
2474f01d833SErwan Le Ray 				if (!c) {
2484f01d833SErwan Le Ray 					port->icount.brk++;
2494f01d833SErwan Le Ray 					if (uart_handle_break(port))
2504f01d833SErwan Le Ray 						continue;
2514f01d833SErwan Le Ray 				} else {
25248a6092fSMaxime Coquelin 					port->icount.frame++;
25348a6092fSMaxime Coquelin 				}
2544f01d833SErwan Le Ray 			}
25548a6092fSMaxime Coquelin 
25648a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
25748a6092fSMaxime Coquelin 
2584f01d833SErwan Le Ray 			if (sr & USART_SR_PE) {
25948a6092fSMaxime Coquelin 				flag = TTY_PARITY;
2604f01d833SErwan Le Ray 			} else if (sr & USART_SR_FE) {
2614f01d833SErwan Le Ray 				if (!c)
2624f01d833SErwan Le Ray 					flag = TTY_BREAK;
2634f01d833SErwan Le Ray 				else
26448a6092fSMaxime Coquelin 					flag = TTY_FRAME;
26548a6092fSMaxime Coquelin 			}
2664f01d833SErwan Le Ray 		}
26748a6092fSMaxime Coquelin 
268cea37afdSJohan Hovold 		if (uart_prepare_sysrq_char(port, c))
26948a6092fSMaxime Coquelin 			continue;
27048a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
27148a6092fSMaxime Coquelin 	}
2726333a485SErwan Le Ray 
2736333a485SErwan Le Ray 	return size;
27433bb2f6aSErwan Le Ray }
27533bb2f6aSErwan Le Ray 
27633bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
27733bb2f6aSErwan Le Ray {
27833bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
27933bb2f6aSErwan Le Ray 	struct tty_port *ttyport = &stm32_port->port.state->port;
28033bb2f6aSErwan Le Ray 	unsigned char *dma_start;
28133bb2f6aSErwan Le Ray 	int dma_count, i;
28233bb2f6aSErwan Le Ray 
28333bb2f6aSErwan Le Ray 	dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
28433bb2f6aSErwan Le Ray 
28533bb2f6aSErwan Le Ray 	/*
28633bb2f6aSErwan Le Ray 	 * Apply rdr_mask on buffer in order to mask parity bit.
28733bb2f6aSErwan Le Ray 	 * This loop is useless in cs8 mode because DMA copies only
28833bb2f6aSErwan Le Ray 	 * 8 bits and already ignores parity bit.
28933bb2f6aSErwan Le Ray 	 */
29033bb2f6aSErwan Le Ray 	if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
29133bb2f6aSErwan Le Ray 		for (i = 0; i < dma_size; i++)
29233bb2f6aSErwan Le Ray 			*(dma_start + i) &= stm32_port->rdr_mask;
29333bb2f6aSErwan Le Ray 
29433bb2f6aSErwan Le Ray 	dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
29533bb2f6aSErwan Le Ray 	port->icount.rx += dma_count;
29633bb2f6aSErwan Le Ray 	if (dma_count != dma_size)
29733bb2f6aSErwan Le Ray 		port->icount.buf_overrun++;
29833bb2f6aSErwan Le Ray 	stm32_port->last_res -= dma_count;
29933bb2f6aSErwan Le Ray 	if (stm32_port->last_res == 0)
30033bb2f6aSErwan Le Ray 		stm32_port->last_res = RX_BUF_L;
30133bb2f6aSErwan Le Ray }
30233bb2f6aSErwan Le Ray 
3036333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
30433bb2f6aSErwan Le Ray {
30533bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
3066333a485SErwan Le Ray 	unsigned int dma_size, size = 0;
30733bb2f6aSErwan Le Ray 
30833bb2f6aSErwan Le Ray 	/* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
30933bb2f6aSErwan Le Ray 	if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
31033bb2f6aSErwan Le Ray 		/* Conditional first part: from last_res to end of DMA buffer */
31133bb2f6aSErwan Le Ray 		dma_size = stm32_port->last_res;
31233bb2f6aSErwan Le Ray 		stm32_usart_push_buffer_dma(port, dma_size);
3136333a485SErwan Le Ray 		size = dma_size;
31433bb2f6aSErwan Le Ray 	}
31533bb2f6aSErwan Le Ray 
31633bb2f6aSErwan Le Ray 	dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
31733bb2f6aSErwan Le Ray 	stm32_usart_push_buffer_dma(port, dma_size);
3186333a485SErwan Le Ray 	size += dma_size;
3196333a485SErwan Le Ray 
3206333a485SErwan Le Ray 	return size;
32133bb2f6aSErwan Le Ray }
32233bb2f6aSErwan Le Ray 
3236333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
32433bb2f6aSErwan Le Ray {
32533bb2f6aSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
32633bb2f6aSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
32733bb2f6aSErwan Le Ray 	enum dma_status rx_dma_status;
32833bb2f6aSErwan Le Ray 	u32 sr;
3296333a485SErwan Le Ray 	unsigned int size = 0;
33033bb2f6aSErwan Le Ray 
3316333a485SErwan Le Ray 	if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) {
33233bb2f6aSErwan Le Ray 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
33333bb2f6aSErwan Le Ray 						    stm32_port->rx_ch->cookie,
33433bb2f6aSErwan Le Ray 						    &stm32_port->rx_dma_state);
33533bb2f6aSErwan Le Ray 		if (rx_dma_status == DMA_IN_PROGRESS) {
33633bb2f6aSErwan Le Ray 			/* Empty DMA buffer */
3376333a485SErwan Le Ray 			size = stm32_usart_receive_chars_dma(port);
33833bb2f6aSErwan Le Ray 			sr = readl_relaxed(port->membase + ofs->isr);
33933bb2f6aSErwan Le Ray 			if (sr & USART_SR_ERR_MASK) {
34033bb2f6aSErwan Le Ray 				/* Disable DMA request line */
34133bb2f6aSErwan Le Ray 				stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
34233bb2f6aSErwan Le Ray 
34333bb2f6aSErwan Le Ray 				/* Switch to PIO mode to handle the errors */
3446333a485SErwan Le Ray 				size += stm32_usart_receive_chars_pio(port);
34533bb2f6aSErwan Le Ray 
34633bb2f6aSErwan Le Ray 				/* Switch back to DMA mode */
34733bb2f6aSErwan Le Ray 				stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
34833bb2f6aSErwan Le Ray 			}
34933bb2f6aSErwan Le Ray 		} else {
35033bb2f6aSErwan Le Ray 			/* Disable RX DMA */
35133bb2f6aSErwan Le Ray 			dmaengine_terminate_async(stm32_port->rx_ch);
35233bb2f6aSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
35333bb2f6aSErwan Le Ray 			/* Fall back to interrupt mode */
35433bb2f6aSErwan Le Ray 			dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
3556333a485SErwan Le Ray 			size = stm32_usart_receive_chars_pio(port);
35633bb2f6aSErwan Le Ray 		}
35733bb2f6aSErwan Le Ray 	} else {
3586333a485SErwan Le Ray 		size = stm32_usart_receive_chars_pio(port);
35933bb2f6aSErwan Le Ray 	}
36048a6092fSMaxime Coquelin 
3616333a485SErwan Le Ray 	return size;
36248a6092fSMaxime Coquelin }
36348a6092fSMaxime Coquelin 
3649a135f16SValentin Caron static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
3659a135f16SValentin Caron {
3669a135f16SValentin Caron 	dmaengine_terminate_async(stm32_port->tx_ch);
3679a135f16SValentin Caron 	stm32_port->tx_dma_busy = false;
3689a135f16SValentin Caron }
3699a135f16SValentin Caron 
3709a135f16SValentin Caron static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
3719a135f16SValentin Caron {
3729a135f16SValentin Caron 	/*
3739a135f16SValentin Caron 	 * We cannot use the function "dmaengine_tx_status" to know the
3749a135f16SValentin Caron 	 * status of DMA. This function does not show if the "dma complete"
3759a135f16SValentin Caron 	 * callback of the DMA transaction has been called. So we prefer
3769a135f16SValentin Caron 	 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
3779a135f16SValentin Caron 	 * same time.
3789a135f16SValentin Caron 	 */
3799a135f16SValentin Caron 	return stm32_port->tx_dma_busy;
3809a135f16SValentin Caron }
3819a135f16SValentin Caron 
3829a135f16SValentin Caron static bool stm32_usart_tx_dma_enabled(struct stm32_port *stm32_port)
3839a135f16SValentin Caron {
3849a135f16SValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
3859a135f16SValentin Caron 
3869a135f16SValentin Caron 	return !!(readl_relaxed(stm32_port->port.membase + ofs->cr3) & USART_CR3_DMAT);
3879a135f16SValentin Caron }
3889a135f16SValentin Caron 
38956f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg)
39034891872SAlexandre TORGUE {
39134891872SAlexandre TORGUE 	struct uart_port *port = arg;
39234891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
393d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
394f16b90c2SErwan Le Ray 	unsigned long flags;
39534891872SAlexandre TORGUE 
39656f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
3979a135f16SValentin Caron 	stm32_usart_tx_dma_terminate(stm32port);
39834891872SAlexandre TORGUE 
39934891872SAlexandre TORGUE 	/* Let's see if we have pending data to send */
400f16b90c2SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
40156f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
402f16b90c2SErwan Le Ray 	spin_unlock_irqrestore(&port->lock, flags);
40334891872SAlexandre TORGUE }
40434891872SAlexandre TORGUE 
40556f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
406d075719eSErwan Le Ray {
407d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
408d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
409d075719eSErwan Le Ray 
410d075719eSErwan Le Ray 	/*
411d075719eSErwan Le Ray 	 * Enables TX FIFO threashold irq when FIFO is enabled,
412d075719eSErwan Le Ray 	 * or TX empty irq when FIFO is disabled
413d075719eSErwan Le Ray 	 */
4142aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
41556f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
416d075719eSErwan Le Ray 	else
41756f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
418d075719eSErwan Le Ray }
419d075719eSErwan Le Ray 
42033bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg)
42133bb2f6aSErwan Le Ray {
42233bb2f6aSErwan Le Ray 	struct uart_port *port = arg;
4236333a485SErwan Le Ray 	struct tty_port *tport = &port->state->port;
4246333a485SErwan Le Ray 	unsigned int size;
4256333a485SErwan Le Ray 	unsigned long flags;
42633bb2f6aSErwan Le Ray 
4276333a485SErwan Le Ray 	spin_lock_irqsave(&port->lock, flags);
4286333a485SErwan Le Ray 	size = stm32_usart_receive_chars(port, false);
4296333a485SErwan Le Ray 	uart_unlock_and_check_sysrq_irqrestore(port, flags);
4306333a485SErwan Le Ray 	if (size)
4316333a485SErwan Le Ray 		tty_flip_buffer_push(tport);
43233bb2f6aSErwan Le Ray }
43333bb2f6aSErwan Le Ray 
43456f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
435d075719eSErwan Le Ray {
436d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
437d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
438d075719eSErwan Le Ray 
4392aa1bbb2SFabrice Gasnier 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
44056f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
441d075719eSErwan Le Ray 	else
44256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
443d075719eSErwan Le Ray }
444d075719eSErwan Le Ray 
445*3bcea529SMarek Vasut static void stm32_usart_rs485_rts_enable(struct uart_port *port)
446*3bcea529SMarek Vasut {
447*3bcea529SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
448*3bcea529SMarek Vasut 	struct serial_rs485 *rs485conf = &port->rs485;
449*3bcea529SMarek Vasut 
450*3bcea529SMarek Vasut 	if (stm32_port->hw_flow_control ||
451*3bcea529SMarek Vasut 	    !(rs485conf->flags & SER_RS485_ENABLED))
452*3bcea529SMarek Vasut 		return;
453*3bcea529SMarek Vasut 
454*3bcea529SMarek Vasut 	if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
455*3bcea529SMarek Vasut 		mctrl_gpio_set(stm32_port->gpios,
456*3bcea529SMarek Vasut 			       stm32_port->port.mctrl | TIOCM_RTS);
457*3bcea529SMarek Vasut 	} else {
458*3bcea529SMarek Vasut 		mctrl_gpio_set(stm32_port->gpios,
459*3bcea529SMarek Vasut 			       stm32_port->port.mctrl & ~TIOCM_RTS);
460*3bcea529SMarek Vasut 	}
461*3bcea529SMarek Vasut }
462*3bcea529SMarek Vasut 
463*3bcea529SMarek Vasut static void stm32_usart_rs485_rts_disable(struct uart_port *port)
464*3bcea529SMarek Vasut {
465*3bcea529SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
466*3bcea529SMarek Vasut 	struct serial_rs485 *rs485conf = &port->rs485;
467*3bcea529SMarek Vasut 
468*3bcea529SMarek Vasut 	if (stm32_port->hw_flow_control ||
469*3bcea529SMarek Vasut 	    !(rs485conf->flags & SER_RS485_ENABLED))
470*3bcea529SMarek Vasut 		return;
471*3bcea529SMarek Vasut 
472*3bcea529SMarek Vasut 	if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
473*3bcea529SMarek Vasut 		mctrl_gpio_set(stm32_port->gpios,
474*3bcea529SMarek Vasut 			       stm32_port->port.mctrl & ~TIOCM_RTS);
475*3bcea529SMarek Vasut 	} else {
476*3bcea529SMarek Vasut 		mctrl_gpio_set(stm32_port->gpios,
477*3bcea529SMarek Vasut 			       stm32_port->port.mctrl | TIOCM_RTS);
478*3bcea529SMarek Vasut 	}
479*3bcea529SMarek Vasut }
480*3bcea529SMarek Vasut 
48156f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port)
48234891872SAlexandre TORGUE {
48334891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
484d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
48534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
48634891872SAlexandre TORGUE 
4879a135f16SValentin Caron 	if (stm32_usart_tx_dma_enabled(stm32_port))
48856f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
48934891872SAlexandre TORGUE 
4905d9176edSErwan Le Ray 	while (!uart_circ_empty(xmit)) {
4915d9176edSErwan Le Ray 		/* Check that TDR is empty before filling FIFO */
4925d9176edSErwan Le Ray 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
4935d9176edSErwan Le Ray 			break;
49434891872SAlexandre TORGUE 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
49534891872SAlexandre TORGUE 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
49634891872SAlexandre TORGUE 		port->icount.tx++;
49734891872SAlexandre TORGUE 	}
49834891872SAlexandre TORGUE 
4995d9176edSErwan Le Ray 	/* rely on TXE irq (mask or unmask) for sending remaining data */
5005d9176edSErwan Le Ray 	if (uart_circ_empty(xmit))
50156f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
5025d9176edSErwan Le Ray 	else
50356f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_enable(port);
5045d9176edSErwan Le Ray }
5055d9176edSErwan Le Ray 
50656f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port)
50734891872SAlexandre TORGUE {
50834891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
509d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
51034891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
51134891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
512195437d1SValentin Caron 	unsigned int count;
51334891872SAlexandre TORGUE 
5149a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32port)) {
5159a135f16SValentin Caron 		if (!stm32_usart_tx_dma_enabled(stm32port))
5169a135f16SValentin Caron 			stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
51734891872SAlexandre TORGUE 		return;
5189a135f16SValentin Caron 	}
51934891872SAlexandre TORGUE 
52034891872SAlexandre TORGUE 	count = uart_circ_chars_pending(xmit);
52134891872SAlexandre TORGUE 
52234891872SAlexandre TORGUE 	if (count > TX_BUF_L)
52334891872SAlexandre TORGUE 		count = TX_BUF_L;
52434891872SAlexandre TORGUE 
52534891872SAlexandre TORGUE 	if (xmit->tail < xmit->head) {
52634891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
52734891872SAlexandre TORGUE 	} else {
52834891872SAlexandre TORGUE 		size_t one = UART_XMIT_SIZE - xmit->tail;
52934891872SAlexandre TORGUE 		size_t two;
53034891872SAlexandre TORGUE 
53134891872SAlexandre TORGUE 		if (one > count)
53234891872SAlexandre TORGUE 			one = count;
53334891872SAlexandre TORGUE 		two = count - one;
53434891872SAlexandre TORGUE 
53534891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
53634891872SAlexandre TORGUE 		if (two)
53734891872SAlexandre TORGUE 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
53834891872SAlexandre TORGUE 	}
53934891872SAlexandre TORGUE 
54034891872SAlexandre TORGUE 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
54134891872SAlexandre TORGUE 					   stm32port->tx_dma_buf,
54234891872SAlexandre TORGUE 					   count,
54334891872SAlexandre TORGUE 					   DMA_MEM_TO_DEV,
54434891872SAlexandre TORGUE 					   DMA_PREP_INTERRUPT);
54534891872SAlexandre TORGUE 
546e7997f7fSErwan Le Ray 	if (!desc)
547e7997f7fSErwan Le Ray 		goto fallback_err;
54834891872SAlexandre TORGUE 
5499a135f16SValentin Caron 	/*
5509a135f16SValentin Caron 	 * Set "tx_dma_busy" flag. This flag will be released when
5519a135f16SValentin Caron 	 * dmaengine_terminate_async will be called. This flag helps
5529a135f16SValentin Caron 	 * transmit_chars_dma not to start another DMA transaction
5539a135f16SValentin Caron 	 * if the callback of the previous is not yet called.
5549a135f16SValentin Caron 	 */
5559a135f16SValentin Caron 	stm32port->tx_dma_busy = true;
5569a135f16SValentin Caron 
55756f9a76cSErwan Le Ray 	desc->callback = stm32_usart_tx_dma_complete;
55834891872SAlexandre TORGUE 	desc->callback_param = port;
55934891872SAlexandre TORGUE 
56034891872SAlexandre TORGUE 	/* Push current DMA TX transaction in the pending queue */
561e7997f7fSErwan Le Ray 	if (dma_submit_error(dmaengine_submit(desc))) {
562e7997f7fSErwan Le Ray 		/* dma no yet started, safe to free resources */
5639a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32port);
564e7997f7fSErwan Le Ray 		goto fallback_err;
565e7997f7fSErwan Le Ray 	}
56634891872SAlexandre TORGUE 
56734891872SAlexandre TORGUE 	/* Issue pending DMA TX requests */
56834891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->tx_ch);
56934891872SAlexandre TORGUE 
57056f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
57134891872SAlexandre TORGUE 
57234891872SAlexandre TORGUE 	xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
57334891872SAlexandre TORGUE 	port->icount.tx += count;
574e7997f7fSErwan Le Ray 	return;
575e7997f7fSErwan Le Ray 
576e7997f7fSErwan Le Ray fallback_err:
57756f9a76cSErwan Le Ray 	stm32_usart_transmit_chars_pio(port);
57834891872SAlexandre TORGUE }
57934891872SAlexandre TORGUE 
58056f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port)
58148a6092fSMaxime Coquelin {
582ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
583d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
58448a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
585d3d079bdSValentin Caron 	u32 isr;
586d3d079bdSValentin Caron 	int ret;
58748a6092fSMaxime Coquelin 
58848a6092fSMaxime Coquelin 	if (port->x_char) {
5899a135f16SValentin Caron 		if (stm32_usart_tx_dma_started(stm32_port) &&
5909a135f16SValentin Caron 		    stm32_usart_tx_dma_enabled(stm32_port))
59156f9a76cSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
592d3d079bdSValentin Caron 
593d3d079bdSValentin Caron 		/* Check that TDR is empty before filling FIFO */
594d3d079bdSValentin Caron 		ret =
595d3d079bdSValentin Caron 		readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
596d3d079bdSValentin Caron 						  isr,
597d3d079bdSValentin Caron 						  (isr & USART_SR_TXE),
598d3d079bdSValentin Caron 						  10, 1000);
599d3d079bdSValentin Caron 		if (ret)
600d3d079bdSValentin Caron 			dev_warn(port->dev, "1 character may be erased\n");
601d3d079bdSValentin Caron 
602ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
60348a6092fSMaxime Coquelin 		port->x_char = 0;
60448a6092fSMaxime Coquelin 		port->icount.tx++;
6059a135f16SValentin Caron 		if (stm32_usart_tx_dma_started(stm32_port))
60656f9a76cSErwan Le Ray 			stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
60748a6092fSMaxime Coquelin 		return;
60848a6092fSMaxime Coquelin 	}
60948a6092fSMaxime Coquelin 
610b83b957cSErwan Le Ray 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
61156f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
61248a6092fSMaxime Coquelin 		return;
61348a6092fSMaxime Coquelin 	}
61448a6092fSMaxime Coquelin 
61564c32eabSErwan Le Ray 	if (ofs->icr == UNDEF_REG)
61656f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
61764c32eabSErwan Le Ray 	else
6181250ed71SFabrice Gasnier 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
61964c32eabSErwan Le Ray 
62034891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
62156f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_dma(port);
62234891872SAlexandre TORGUE 	else
62356f9a76cSErwan Le Ray 		stm32_usart_transmit_chars_pio(port);
62448a6092fSMaxime Coquelin 
62548a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
62648a6092fSMaxime Coquelin 		uart_write_wakeup(port);
62748a6092fSMaxime Coquelin 
62848a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
62956f9a76cSErwan Le Ray 		stm32_usart_tx_interrupt_disable(port);
63048a6092fSMaxime Coquelin }
63148a6092fSMaxime Coquelin 
63256f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
63348a6092fSMaxime Coquelin {
63448a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
63512761869SErwan Le Ray 	struct tty_port *tport = &port->state->port;
636ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
637d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
63848a6092fSMaxime Coquelin 	u32 sr;
6396333a485SErwan Le Ray 	unsigned int size;
64048a6092fSMaxime Coquelin 
641ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
64248a6092fSMaxime Coquelin 
6434cc0ed62SErwan Le Ray 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
6444cc0ed62SErwan Le Ray 		writel_relaxed(USART_ICR_RTOCF,
6454cc0ed62SErwan Le Ray 			       port->membase + ofs->icr);
6464cc0ed62SErwan Le Ray 
64712761869SErwan Le Ray 	if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
64812761869SErwan Le Ray 		/* Clear wake up flag and disable wake up interrupt */
649270e5a74SFabrice Gasnier 		writel_relaxed(USART_ICR_WUCF,
650270e5a74SFabrice Gasnier 			       port->membase + ofs->icr);
65112761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
65212761869SErwan Le Ray 		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
65312761869SErwan Le Ray 			pm_wakeup_event(tport->tty->dev, 0);
65412761869SErwan Le Ray 	}
655270e5a74SFabrice Gasnier 
65633bb2f6aSErwan Le Ray 	/*
65733bb2f6aSErwan Le Ray 	 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
65833bb2f6aSErwan Le Ray 	 * line has been masked by HW and rx data are stacking in FIFO.
65933bb2f6aSErwan Le Ray 	 */
660d1ec8a2eSErwan Le Ray 	if (!stm32_port->throttled) {
66133bb2f6aSErwan Le Ray 		if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) ||
662d1ec8a2eSErwan Le Ray 		    ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) {
6636333a485SErwan Le Ray 			spin_lock(&port->lock);
6646333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, false);
6656333a485SErwan Le Ray 			uart_unlock_and_check_sysrq(port);
6666333a485SErwan Le Ray 			if (size)
6676333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
668d1ec8a2eSErwan Le Ray 		}
669d1ec8a2eSErwan Le Ray 	}
67048a6092fSMaxime Coquelin 
671ad767681SErwan Le Ray 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
672ad767681SErwan Le Ray 		spin_lock(&port->lock);
67356f9a76cSErwan Le Ray 		stm32_usart_transmit_chars(port);
67401d32d71SAlexandre TORGUE 		spin_unlock(&port->lock);
675ad767681SErwan Le Ray 	}
67601d32d71SAlexandre TORGUE 
67733bb2f6aSErwan Le Ray 	if (stm32_usart_rx_dma_enabled(port))
67834891872SAlexandre TORGUE 		return IRQ_WAKE_THREAD;
67934891872SAlexandre TORGUE 	else
68034891872SAlexandre TORGUE 		return IRQ_HANDLED;
68134891872SAlexandre TORGUE }
68234891872SAlexandre TORGUE 
68356f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr)
68434891872SAlexandre TORGUE {
68534891872SAlexandre TORGUE 	struct uart_port *port = ptr;
6866333a485SErwan Le Ray 	struct tty_port *tport = &port->state->port;
687d1ec8a2eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
6886333a485SErwan Le Ray 	unsigned int size;
6896333a485SErwan Le Ray 	unsigned long flags;
69034891872SAlexandre TORGUE 
691cc58d0a3SErwan Le Ray 	/* Receiver timeout irq for DMA RX */
6926333a485SErwan Le Ray 	if (!stm32_port->throttled) {
6936333a485SErwan Le Ray 		spin_lock_irqsave(&port->lock, flags);
6946333a485SErwan Le Ray 		size = stm32_usart_receive_chars(port, false);
6956333a485SErwan Le Ray 		uart_unlock_and_check_sysrq_irqrestore(port, flags);
6966333a485SErwan Le Ray 		if (size)
6976333a485SErwan Le Ray 			tty_flip_buffer_push(tport);
6986333a485SErwan Le Ray 	}
69934891872SAlexandre TORGUE 
70048a6092fSMaxime Coquelin 	return IRQ_HANDLED;
70148a6092fSMaxime Coquelin }
70248a6092fSMaxime Coquelin 
70356f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port)
70448a6092fSMaxime Coquelin {
705ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
706d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
707ada8618fSAlexandre TORGUE 
7083db1d524SErwan Le Ray 	if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
7093db1d524SErwan Le Ray 		return TIOCSER_TEMT;
7103db1d524SErwan Le Ray 
7113db1d524SErwan Le Ray 	return 0;
71248a6092fSMaxime Coquelin }
71348a6092fSMaxime Coquelin 
71456f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
71548a6092fSMaxime Coquelin {
716ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
717d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
718ada8618fSAlexandre TORGUE 
71948a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
72056f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
72148a6092fSMaxime Coquelin 	else
72256f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
7236cf61b9bSManivannan Sadhasivam 
7246cf61b9bSManivannan Sadhasivam 	mctrl_gpio_set(stm32_port->gpios, mctrl);
72548a6092fSMaxime Coquelin }
72648a6092fSMaxime Coquelin 
72756f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
72848a6092fSMaxime Coquelin {
7296cf61b9bSManivannan Sadhasivam 	struct stm32_port *stm32_port = to_stm32_port(port);
7306cf61b9bSManivannan Sadhasivam 	unsigned int ret;
7316cf61b9bSManivannan Sadhasivam 
73248a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
7336cf61b9bSManivannan Sadhasivam 	ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
7346cf61b9bSManivannan Sadhasivam 
7356cf61b9bSManivannan Sadhasivam 	return mctrl_gpio_get(stm32_port->gpios, &ret);
7366cf61b9bSManivannan Sadhasivam }
7376cf61b9bSManivannan Sadhasivam 
73856f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port)
7396cf61b9bSManivannan Sadhasivam {
7406cf61b9bSManivannan Sadhasivam 	mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
7416cf61b9bSManivannan Sadhasivam }
7426cf61b9bSManivannan Sadhasivam 
74356f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port)
7446cf61b9bSManivannan Sadhasivam {
7456cf61b9bSManivannan Sadhasivam 	mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
74648a6092fSMaxime Coquelin }
74748a6092fSMaxime Coquelin 
74848a6092fSMaxime Coquelin /* Transmit stop */
74956f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port)
75048a6092fSMaxime Coquelin {
751ad0c2748SMarek Vasut 	struct stm32_port *stm32_port = to_stm32_port(port);
7522a3bcfe0SValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
753ad0c2748SMarek Vasut 
75456f9a76cSErwan Le Ray 	stm32_usart_tx_interrupt_disable(port);
7552a3bcfe0SValentin Caron 	if (stm32_usart_tx_dma_started(stm32_port) && stm32_usart_tx_dma_enabled(stm32_port))
7562a3bcfe0SValentin Caron 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
757ad0c2748SMarek Vasut 
758*3bcea529SMarek Vasut 	stm32_usart_rs485_rts_disable(port);
75948a6092fSMaxime Coquelin }
76048a6092fSMaxime Coquelin 
76148a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
76256f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port)
76348a6092fSMaxime Coquelin {
76448a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
76548a6092fSMaxime Coquelin 
766037b91ecSValentin Caron 	if (uart_circ_empty(xmit) && !port->x_char)
76748a6092fSMaxime Coquelin 		return;
76848a6092fSMaxime Coquelin 
769*3bcea529SMarek Vasut 	stm32_usart_rs485_rts_enable(port);
770ad0c2748SMarek Vasut 
77156f9a76cSErwan Le Ray 	stm32_usart_transmit_chars(port);
77248a6092fSMaxime Coquelin }
77348a6092fSMaxime Coquelin 
7743d82be8bSErwan Le Ray /* Flush the transmit buffer. */
7753d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port)
7763d82be8bSErwan Le Ray {
7773d82be8bSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
7783d82be8bSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
7793d82be8bSErwan Le Ray 
7803d82be8bSErwan Le Ray 	if (stm32_port->tx_ch) {
7819a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
7823d82be8bSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
7833d82be8bSErwan Le Ray 	}
7843d82be8bSErwan Le Ray }
7853d82be8bSErwan Le Ray 
78648a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
78756f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port)
78848a6092fSMaxime Coquelin {
789ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
790d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
79148a6092fSMaxime Coquelin 	unsigned long flags;
79248a6092fSMaxime Coquelin 
79348a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
794d1ec8a2eSErwan Le Ray 
795d1ec8a2eSErwan Le Ray 	/*
796d1ec8a2eSErwan Le Ray 	 * Disable DMA request line if enabled, so the RX data gets queued into the FIFO.
797d1ec8a2eSErwan Le Ray 	 * Hardware flow control is triggered when RX FIFO is full.
798d1ec8a2eSErwan Le Ray 	 */
799d1ec8a2eSErwan Le Ray 	if (stm32_usart_rx_dma_enabled(port))
800d1ec8a2eSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
801d1ec8a2eSErwan Le Ray 
80256f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
803d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
80456f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
805d0a6a7bcSErwan Le Ray 
806d1ec8a2eSErwan Le Ray 	stm32_port->throttled = true;
80748a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
80848a6092fSMaxime Coquelin }
80948a6092fSMaxime Coquelin 
81048a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
81156f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port)
81248a6092fSMaxime Coquelin {
813ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
814d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
81548a6092fSMaxime Coquelin 	unsigned long flags;
81648a6092fSMaxime Coquelin 
81748a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
81856f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
819d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
82056f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
821d0a6a7bcSErwan Le Ray 
822d1ec8a2eSErwan Le Ray 	/*
823d1ec8a2eSErwan Le Ray 	 * Switch back to DMA mode (re-enable DMA request line).
824d1ec8a2eSErwan Le Ray 	 * Hardware flow control is stopped when FIFO is not full any more.
825d1ec8a2eSErwan Le Ray 	 */
826d1ec8a2eSErwan Le Ray 	if (stm32_port->rx_ch)
827d1ec8a2eSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
828d1ec8a2eSErwan Le Ray 
829d1ec8a2eSErwan Le Ray 	stm32_port->throttled = false;
83048a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
83148a6092fSMaxime Coquelin }
83248a6092fSMaxime Coquelin 
83348a6092fSMaxime Coquelin /* Receive stop */
83456f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port)
83548a6092fSMaxime Coquelin {
836ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
837d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
838ada8618fSAlexandre TORGUE 
839e0abc903SErwan Le Ray 	/* Disable DMA request line. */
840e0abc903SErwan Le Ray 	if (stm32_port->rx_ch)
841e0abc903SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
842e0abc903SErwan Le Ray 
84356f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
844d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
84556f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
84648a6092fSMaxime Coquelin }
84748a6092fSMaxime Coquelin 
84848a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
84956f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
85048a6092fSMaxime Coquelin {
85148a6092fSMaxime Coquelin }
85248a6092fSMaxime Coquelin 
8536eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port)
8546eeb348cSErwan Le Ray {
8556eeb348cSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
8566eeb348cSErwan Le Ray 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
8576eeb348cSErwan Le Ray 	struct dma_async_tx_descriptor *desc;
8586eeb348cSErwan Le Ray 	int ret;
8596eeb348cSErwan Le Ray 
8606eeb348cSErwan Le Ray 	stm32_port->last_res = RX_BUF_L;
8616eeb348cSErwan Le Ray 	/* Prepare a DMA cyclic transaction */
8626eeb348cSErwan Le Ray 	desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
8636eeb348cSErwan Le Ray 					 stm32_port->rx_dma_buf,
8646eeb348cSErwan Le Ray 					 RX_BUF_L, RX_BUF_P,
8656eeb348cSErwan Le Ray 					 DMA_DEV_TO_MEM,
8666eeb348cSErwan Le Ray 					 DMA_PREP_INTERRUPT);
8676eeb348cSErwan Le Ray 	if (!desc) {
8686eeb348cSErwan Le Ray 		dev_err(port->dev, "rx dma prep cyclic failed\n");
8696eeb348cSErwan Le Ray 		return -ENODEV;
8706eeb348cSErwan Le Ray 	}
8716eeb348cSErwan Le Ray 
8726eeb348cSErwan Le Ray 	desc->callback = stm32_usart_rx_dma_complete;
8736eeb348cSErwan Le Ray 	desc->callback_param = port;
8746eeb348cSErwan Le Ray 
8756eeb348cSErwan Le Ray 	/* Push current DMA transaction in the pending queue */
8766eeb348cSErwan Le Ray 	ret = dma_submit_error(dmaengine_submit(desc));
8776eeb348cSErwan Le Ray 	if (ret) {
8786eeb348cSErwan Le Ray 		dmaengine_terminate_sync(stm32_port->rx_ch);
8796eeb348cSErwan Le Ray 		return ret;
8806eeb348cSErwan Le Ray 	}
8816eeb348cSErwan Le Ray 
8826eeb348cSErwan Le Ray 	/* Issue pending DMA requests */
8836eeb348cSErwan Le Ray 	dma_async_issue_pending(stm32_port->rx_ch);
8846eeb348cSErwan Le Ray 
8856eeb348cSErwan Le Ray 	/*
8866eeb348cSErwan Le Ray 	 * DMA request line not re-enabled at resume when port is throttled.
8876eeb348cSErwan Le Ray 	 * It will be re-enabled by unthrottle ops.
8886eeb348cSErwan Le Ray 	 */
8896eeb348cSErwan Le Ray 	if (!stm32_port->throttled)
8906eeb348cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
8916eeb348cSErwan Le Ray 
8926eeb348cSErwan Le Ray 	return 0;
8936eeb348cSErwan Le Ray }
8946eeb348cSErwan Le Ray 
89556f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port)
89648a6092fSMaxime Coquelin {
897ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
898d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
899f4518a8aSErwan Le Ray 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
90048a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
90148a6092fSMaxime Coquelin 	u32 val;
90248a6092fSMaxime Coquelin 	int ret;
90348a6092fSMaxime Coquelin 
90456f9a76cSErwan Le Ray 	ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
90556f9a76cSErwan Le Ray 				   stm32_usart_threaded_interrupt,
906e359b441SJohan Hovold 				   IRQF_ONESHOT | IRQF_NO_SUSPEND,
907e359b441SJohan Hovold 				   name, port);
90848a6092fSMaxime Coquelin 	if (ret)
90948a6092fSMaxime Coquelin 		return ret;
91048a6092fSMaxime Coquelin 
9113cd66593SMartin Devera 	if (stm32_port->swap) {
9123cd66593SMartin Devera 		val = readl_relaxed(port->membase + ofs->cr2);
9133cd66593SMartin Devera 		val |= USART_CR2_SWAP;
9143cd66593SMartin Devera 		writel_relaxed(val, port->membase + ofs->cr2);
9153cd66593SMartin Devera 	}
9163cd66593SMartin Devera 
91784872dc4SErwan Le Ray 	/* RX FIFO Flush */
91884872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
919315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
92048a6092fSMaxime Coquelin 
921e0abc903SErwan Le Ray 	if (stm32_port->rx_ch) {
9226eeb348cSErwan Le Ray 		ret = stm32_usart_start_rx_dma_cyclic(port);
923e0abc903SErwan Le Ray 		if (ret) {
9246eeb348cSErwan Le Ray 			free_irq(port->irq, port);
9256eeb348cSErwan Le Ray 			return ret;
926e0abc903SErwan Le Ray 		}
927e0abc903SErwan Le Ray 	}
928d1ec8a2eSErwan Le Ray 
92925a8e761SErwan Le Ray 	/* RX enabling */
930f4518a8aSErwan Le Ray 	val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
93156f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, val);
93284872dc4SErwan Le Ray 
93348a6092fSMaxime Coquelin 	return 0;
93448a6092fSMaxime Coquelin }
93548a6092fSMaxime Coquelin 
93656f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port)
93748a6092fSMaxime Coquelin {
938ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
939d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
940d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
94164c32eabSErwan Le Ray 	u32 val, isr;
94264c32eabSErwan Le Ray 	int ret;
94348a6092fSMaxime Coquelin 
9449a135f16SValentin Caron 	if (stm32_usart_tx_dma_enabled(stm32_port))
94556a23f93SValentin Caron 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
9469a135f16SValentin Caron 
9479a135f16SValentin Caron 	if (stm32_usart_tx_dma_started(stm32_port))
9489a135f16SValentin Caron 		stm32_usart_tx_dma_terminate(stm32_port);
94956a23f93SValentin Caron 
9506cf61b9bSManivannan Sadhasivam 	/* Disable modem control interrupts */
95156f9a76cSErwan Le Ray 	stm32_usart_disable_ms(port);
9526cf61b9bSManivannan Sadhasivam 
9534cc0ed62SErwan Le Ray 	val = USART_CR1_TXEIE | USART_CR1_TE;
9544cc0ed62SErwan Le Ray 	val |= stm32_port->cr1_irq | USART_CR1_RE;
95587f1f809SAlexandre TORGUE 	val |= BIT(cfg->uart_enable_bit);
956351a762aSGerald Baeza 	if (stm32_port->fifoen)
957351a762aSGerald Baeza 		val |= USART_CR1_FIFOEN;
95864c32eabSErwan Le Ray 
95964c32eabSErwan Le Ray 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
96064c32eabSErwan Le Ray 					 isr, (isr & USART_SR_TC),
96164c32eabSErwan Le Ray 					 10, 100000);
96264c32eabSErwan Le Ray 
963c31c3ea0SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set */
96464c32eabSErwan Le Ray 	if (ret)
965c31c3ea0SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
96664c32eabSErwan Le Ray 
967e0abc903SErwan Le Ray 	/* Disable RX DMA. */
968e0abc903SErwan Le Ray 	if (stm32_port->rx_ch)
969e0abc903SErwan Le Ray 		dmaengine_terminate_async(stm32_port->rx_ch);
970e0abc903SErwan Le Ray 
9719f77d192SErwan Le Ray 	/* flush RX & TX FIFO */
9729f77d192SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
9739f77d192SErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
9749f77d192SErwan Le Ray 			       port->membase + ofs->rqr);
9759f77d192SErwan Le Ray 
97656f9a76cSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, val);
97748a6092fSMaxime Coquelin 
97848a6092fSMaxime Coquelin 	free_irq(port->irq, port);
97948a6092fSMaxime Coquelin }
98048a6092fSMaxime Coquelin 
98156f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port,
98256f9a76cSErwan Le Ray 				    struct ktermios *termios,
98348a6092fSMaxime Coquelin 				    struct ktermios *old)
98448a6092fSMaxime Coquelin {
98548a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
986d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
987d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
9881bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
989c8a9d043SErwan Le Ray 	unsigned int baud, bits;
99048a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
99148a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
992f264c6f6SErwan Le Ray 	u32 cr1, cr2, cr3, isr;
99348a6092fSMaxime Coquelin 	unsigned long flags;
994f264c6f6SErwan Le Ray 	int ret;
99548a6092fSMaxime Coquelin 
99648a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
99748a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
99848a6092fSMaxime Coquelin 
99948a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
100048a6092fSMaxime Coquelin 
100148a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
100248a6092fSMaxime Coquelin 
1003f264c6f6SErwan Le Ray 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
1004f264c6f6SErwan Le Ray 						isr,
1005f264c6f6SErwan Le Ray 						(isr & USART_SR_TC),
1006f264c6f6SErwan Le Ray 						10, 100000);
1007f264c6f6SErwan Le Ray 
1008f264c6f6SErwan Le Ray 	/* Send the TC error message only when ISR_TC is not set. */
1009f264c6f6SErwan Le Ray 	if (ret)
1010f264c6f6SErwan Le Ray 		dev_err(port->dev, "Transmission is not complete\n");
1011f264c6f6SErwan Le Ray 
101248a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
1013ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
101448a6092fSMaxime Coquelin 
101584872dc4SErwan Le Ray 	/* flush RX & TX FIFO */
101684872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
1017315e2d8aSErwan Le Ray 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
1018315e2d8aSErwan Le Ray 			       port->membase + ofs->rqr);
10191bcda09dSBich HEMON 
102084872dc4SErwan Le Ray 	cr1 = USART_CR1_TE | USART_CR1_RE;
1021351a762aSGerald Baeza 	if (stm32_port->fifoen)
1022351a762aSGerald Baeza 		cr1 |= USART_CR1_FIFOEN;
10233cd66593SMartin Devera 	cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
102425a8e761SErwan Le Ray 
102525a8e761SErwan Le Ray 	/* Tx and RX FIFO configuration */
1026d075719eSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
102725a8e761SErwan Le Ray 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
102825a8e761SErwan Le Ray 	if (stm32_port->fifoen) {
10292aa1bbb2SFabrice Gasnier 		if (stm32_port->txftcfg >= 0)
10302aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
10312aa1bbb2SFabrice Gasnier 		if (stm32_port->rxftcfg >= 0)
10322aa1bbb2SFabrice Gasnier 			cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
103325a8e761SErwan Le Ray 	}
103448a6092fSMaxime Coquelin 
103548a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
103648a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
103748a6092fSMaxime Coquelin 
10383ec2ff37SJiri Slaby 	bits = tty_get_char_size(cflag);
10396c5962f3SErwan Le Ray 	stm32_port->rdr_mask = (BIT(bits) - 1);
1040c8a9d043SErwan Le Ray 
104148a6092fSMaxime Coquelin 	if (cflag & PARENB) {
1042c8a9d043SErwan Le Ray 		bits++;
104348a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
1044c8a9d043SErwan Le Ray 	}
1045c8a9d043SErwan Le Ray 
1046c8a9d043SErwan Le Ray 	/*
1047c8a9d043SErwan Le Ray 	 * Word length configuration:
1048c8a9d043SErwan Le Ray 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1049c8a9d043SErwan Le Ray 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1050c8a9d043SErwan Le Ray 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1051c8a9d043SErwan Le Ray 	 * M0 and M1 already cleared by cr1 initialization.
1052c8a9d043SErwan Le Ray 	 */
1053c8a9d043SErwan Le Ray 	if (bits == 9)
1054ada8618fSAlexandre TORGUE 		cr1 |= USART_CR1_M0;
1055c8a9d043SErwan Le Ray 	else if ((bits == 7) && cfg->has_7bits_data)
1056c8a9d043SErwan Le Ray 		cr1 |= USART_CR1_M1;
1057c8a9d043SErwan Le Ray 	else if (bits != 8)
1058c8a9d043SErwan Le Ray 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1059c8a9d043SErwan Le Ray 			, bits);
106048a6092fSMaxime Coquelin 
10614cc0ed62SErwan Le Ray 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
10622aa1bbb2SFabrice Gasnier 				       (stm32_port->fifoen &&
10632aa1bbb2SFabrice Gasnier 					stm32_port->rxftcfg >= 0))) {
10644cc0ed62SErwan Le Ray 		if (cflag & CSTOPB)
10654cc0ed62SErwan Le Ray 			bits = bits + 3; /* 1 start bit + 2 stop bits */
10664cc0ed62SErwan Le Ray 		else
10674cc0ed62SErwan Le Ray 			bits = bits + 2; /* 1 start bit + 1 stop bit */
10684cc0ed62SErwan Le Ray 
10694cc0ed62SErwan Le Ray 		/* RX timeout irq to occur after last stop bit + bits */
10704cc0ed62SErwan Le Ray 		stm32_port->cr1_irq = USART_CR1_RTOIE;
10714cc0ed62SErwan Le Ray 		writel_relaxed(bits, port->membase + ofs->rtor);
10724cc0ed62SErwan Le Ray 		cr2 |= USART_CR2_RTOEN;
107333bb2f6aSErwan Le Ray 		/*
107433bb2f6aSErwan Le Ray 		 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
107533bb2f6aSErwan Le Ray 		 * wake up over usart, from low power until the DMA gets re-enabled by resume.
107633bb2f6aSErwan Le Ray 		 */
1077d0a6a7bcSErwan Le Ray 		stm32_port->cr3_irq =  USART_CR3_RXFTIE;
10784cc0ed62SErwan Le Ray 	}
10794cc0ed62SErwan Le Ray 
1080d0a6a7bcSErwan Le Ray 	cr1 |= stm32_port->cr1_irq;
1081d0a6a7bcSErwan Le Ray 	cr3 |= stm32_port->cr3_irq;
1082d0a6a7bcSErwan Le Ray 
108348a6092fSMaxime Coquelin 	if (cflag & PARODD)
108448a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
108548a6092fSMaxime Coquelin 
108648a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
108748a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
108848a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
108935abe98fSBich HEMON 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
109048a6092fSMaxime Coquelin 	}
109148a6092fSMaxime Coquelin 
109248a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
109348a6092fSMaxime Coquelin 
109448a6092fSMaxime Coquelin 	/*
109548a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
109648a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
109748a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
109848a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
109948a6092fSMaxime Coquelin 	 */
110048a6092fSMaxime Coquelin 	if (usartdiv < 16) {
110148a6092fSMaxime Coquelin 		oversampling = 8;
11021bcda09dSBich HEMON 		cr1 |= USART_CR1_OVER8;
110356f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
110448a6092fSMaxime Coquelin 	} else {
110548a6092fSMaxime Coquelin 		oversampling = 16;
11061bcda09dSBich HEMON 		cr1 &= ~USART_CR1_OVER8;
110756f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
110848a6092fSMaxime Coquelin 	}
110948a6092fSMaxime Coquelin 
111048a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
111148a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
1112ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
111348a6092fSMaxime Coquelin 
111448a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
111548a6092fSMaxime Coquelin 
111648a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
111748a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
111848a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
111948a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
11204f01d833SErwan Le Ray 		port->read_status_mask |= USART_SR_FE;
112148a6092fSMaxime Coquelin 
112248a6092fSMaxime Coquelin 	/* Characters to ignore */
112348a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
112448a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
112548a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
112648a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
11274f01d833SErwan Le Ray 		port->ignore_status_mask |= USART_SR_FE;
112848a6092fSMaxime Coquelin 		/*
112948a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
113048a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
113148a6092fSMaxime Coquelin 		 */
113248a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
113348a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
113448a6092fSMaxime Coquelin 	}
113548a6092fSMaxime Coquelin 
113648a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
113748a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
113848a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
113948a6092fSMaxime Coquelin 
114033bb2f6aSErwan Le Ray 	if (stm32_port->rx_ch) {
114133bb2f6aSErwan Le Ray 		/*
114233bb2f6aSErwan Le Ray 		 * Setup DMA to collect only valid data and enable error irqs.
114333bb2f6aSErwan Le Ray 		 * This also enables break reception when using DMA.
114433bb2f6aSErwan Le Ray 		 */
114533bb2f6aSErwan Le Ray 		cr1 |= USART_CR1_PEIE;
114633bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_EIE;
114734891872SAlexandre TORGUE 		cr3 |= USART_CR3_DMAR;
114833bb2f6aSErwan Le Ray 		cr3 |= USART_CR3_DDRE;
114933bb2f6aSErwan Le Ray 	}
115034891872SAlexandre TORGUE 
11511bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
115256f9a76cSErwan Le Ray 		stm32_usart_config_reg_rs485(&cr1, &cr3,
11531bcda09dSBich HEMON 					     rs485conf->delay_rts_before_send,
115456f9a76cSErwan Le Ray 					     rs485conf->delay_rts_after_send,
115556f9a76cSErwan Le Ray 					     baud);
11561bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
11571bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
11581bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
11591bcda09dSBich HEMON 		} else {
11601bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
11611bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
11621bcda09dSBich HEMON 		}
11631bcda09dSBich HEMON 
11641bcda09dSBich HEMON 	} else {
11651bcda09dSBich HEMON 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
11661bcda09dSBich HEMON 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
11671bcda09dSBich HEMON 	}
11681bcda09dSBich HEMON 
116912761869SErwan Le Ray 	/* Configure wake up from low power on start bit detection */
11703d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
117112761869SErwan Le Ray 		cr3 &= ~USART_CR3_WUS_MASK;
117212761869SErwan Le Ray 		cr3 |= USART_CR3_WUS_START_BIT;
117312761869SErwan Le Ray 	}
117412761869SErwan Le Ray 
1175ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
1176ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
1177ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
117848a6092fSMaxime Coquelin 
117956f9a76cSErwan Le Ray 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
118048a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
1181436c9793SErwan Le Ray 
1182436c9793SErwan Le Ray 	/* Handle modem control interrupts */
1183436c9793SErwan Le Ray 	if (UART_ENABLE_MS(port, termios->c_cflag))
1184436c9793SErwan Le Ray 		stm32_usart_enable_ms(port);
1185436c9793SErwan Le Ray 	else
1186436c9793SErwan Le Ray 		stm32_usart_disable_ms(port);
118748a6092fSMaxime Coquelin }
118848a6092fSMaxime Coquelin 
118956f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port)
119048a6092fSMaxime Coquelin {
119148a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
119248a6092fSMaxime Coquelin }
119348a6092fSMaxime Coquelin 
119456f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port)
119548a6092fSMaxime Coquelin {
119648a6092fSMaxime Coquelin }
119748a6092fSMaxime Coquelin 
119856f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port)
119948a6092fSMaxime Coquelin {
120048a6092fSMaxime Coquelin 	return 0;
120148a6092fSMaxime Coquelin }
120248a6092fSMaxime Coquelin 
120356f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags)
120448a6092fSMaxime Coquelin {
120548a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
120648a6092fSMaxime Coquelin 		port->type = PORT_STM32;
120748a6092fSMaxime Coquelin }
120848a6092fSMaxime Coquelin 
120948a6092fSMaxime Coquelin static int
121056f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
121148a6092fSMaxime Coquelin {
121248a6092fSMaxime Coquelin 	/* No user changeable parameters */
121348a6092fSMaxime Coquelin 	return -EINVAL;
121448a6092fSMaxime Coquelin }
121548a6092fSMaxime Coquelin 
121656f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state,
121748a6092fSMaxime Coquelin 			   unsigned int oldstate)
121848a6092fSMaxime Coquelin {
121948a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
122048a6092fSMaxime Coquelin 			struct stm32_port, port);
1221d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1222d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32port->info->cfg;
122318ee37e1SJohan Hovold 	unsigned long flags;
122448a6092fSMaxime Coquelin 
122548a6092fSMaxime Coquelin 	switch (state) {
122648a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
1227fb6dcef6SErwan Le Ray 		pm_runtime_get_sync(port->dev);
122848a6092fSMaxime Coquelin 		break;
122948a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
123048a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
123156f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
123248a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
1233fb6dcef6SErwan Le Ray 		pm_runtime_put_sync(port->dev);
123448a6092fSMaxime Coquelin 		break;
123548a6092fSMaxime Coquelin 	}
123648a6092fSMaxime Coquelin }
123748a6092fSMaxime Coquelin 
12381f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL)
12391f507b3aSValentin Caron 
12401f507b3aSValentin Caron  /* Callbacks for characters polling in debug context (i.e. KGDB). */
12411f507b3aSValentin Caron static int stm32_usart_poll_init(struct uart_port *port)
12421f507b3aSValentin Caron {
12431f507b3aSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
12441f507b3aSValentin Caron 
12451f507b3aSValentin Caron 	return clk_prepare_enable(stm32_port->clk);
12461f507b3aSValentin Caron }
12471f507b3aSValentin Caron 
12481f507b3aSValentin Caron static int stm32_usart_poll_get_char(struct uart_port *port)
12491f507b3aSValentin Caron {
12501f507b3aSValentin Caron 	struct stm32_port *stm32_port = to_stm32_port(port);
12511f507b3aSValentin Caron 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
12521f507b3aSValentin Caron 
12531f507b3aSValentin Caron 	if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE))
12541f507b3aSValentin Caron 		return NO_POLL_CHAR;
12551f507b3aSValentin Caron 
12561f507b3aSValentin Caron 	return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask;
12571f507b3aSValentin Caron }
12581f507b3aSValentin Caron 
12591f507b3aSValentin Caron static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch)
12601f507b3aSValentin Caron {
12611f507b3aSValentin Caron 	stm32_usart_console_putchar(port, ch);
12621f507b3aSValentin Caron }
12631f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */
12641f507b3aSValentin Caron 
126548a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
126656f9a76cSErwan Le Ray 	.tx_empty	= stm32_usart_tx_empty,
126756f9a76cSErwan Le Ray 	.set_mctrl	= stm32_usart_set_mctrl,
126856f9a76cSErwan Le Ray 	.get_mctrl	= stm32_usart_get_mctrl,
126956f9a76cSErwan Le Ray 	.stop_tx	= stm32_usart_stop_tx,
127056f9a76cSErwan Le Ray 	.start_tx	= stm32_usart_start_tx,
127156f9a76cSErwan Le Ray 	.throttle	= stm32_usart_throttle,
127256f9a76cSErwan Le Ray 	.unthrottle	= stm32_usart_unthrottle,
127356f9a76cSErwan Le Ray 	.stop_rx	= stm32_usart_stop_rx,
127456f9a76cSErwan Le Ray 	.enable_ms	= stm32_usart_enable_ms,
127556f9a76cSErwan Le Ray 	.break_ctl	= stm32_usart_break_ctl,
127656f9a76cSErwan Le Ray 	.startup	= stm32_usart_startup,
127756f9a76cSErwan Le Ray 	.shutdown	= stm32_usart_shutdown,
12783d82be8bSErwan Le Ray 	.flush_buffer	= stm32_usart_flush_buffer,
127956f9a76cSErwan Le Ray 	.set_termios	= stm32_usart_set_termios,
128056f9a76cSErwan Le Ray 	.pm		= stm32_usart_pm,
128156f9a76cSErwan Le Ray 	.type		= stm32_usart_type,
128256f9a76cSErwan Le Ray 	.release_port	= stm32_usart_release_port,
128356f9a76cSErwan Le Ray 	.request_port	= stm32_usart_request_port,
128456f9a76cSErwan Le Ray 	.config_port	= stm32_usart_config_port,
128556f9a76cSErwan Le Ray 	.verify_port	= stm32_usart_verify_port,
12861f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL)
12871f507b3aSValentin Caron 	.poll_init      = stm32_usart_poll_init,
12881f507b3aSValentin Caron 	.poll_get_char	= stm32_usart_poll_get_char,
12891f507b3aSValentin Caron 	.poll_put_char	= stm32_usart_poll_put_char,
12901f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */
129148a6092fSMaxime Coquelin };
129248a6092fSMaxime Coquelin 
12932aa1bbb2SFabrice Gasnier /*
12942aa1bbb2SFabrice Gasnier  * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
12952aa1bbb2SFabrice Gasnier  * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
12962aa1bbb2SFabrice Gasnier  * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
12972aa1bbb2SFabrice Gasnier  * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
12982aa1bbb2SFabrice Gasnier  */
12992aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
13002aa1bbb2SFabrice Gasnier 
13012aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
13022aa1bbb2SFabrice Gasnier 				  int *ftcfg)
13032aa1bbb2SFabrice Gasnier {
13042aa1bbb2SFabrice Gasnier 	u32 bytes, i;
13052aa1bbb2SFabrice Gasnier 
13062aa1bbb2SFabrice Gasnier 	/* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
13072aa1bbb2SFabrice Gasnier 	if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
13082aa1bbb2SFabrice Gasnier 		bytes = 8;
13092aa1bbb2SFabrice Gasnier 
13102aa1bbb2SFabrice Gasnier 	for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
13112aa1bbb2SFabrice Gasnier 		if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
13122aa1bbb2SFabrice Gasnier 			break;
13132aa1bbb2SFabrice Gasnier 	if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
13142aa1bbb2SFabrice Gasnier 		i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
13152aa1bbb2SFabrice Gasnier 
13162aa1bbb2SFabrice Gasnier 	dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
13172aa1bbb2SFabrice Gasnier 		stm32h7_usart_fifo_thresh_cfg[i]);
13182aa1bbb2SFabrice Gasnier 
13192aa1bbb2SFabrice Gasnier 	/* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
13202aa1bbb2SFabrice Gasnier 	if (i)
13212aa1bbb2SFabrice Gasnier 		*ftcfg = i - 1;
13222aa1bbb2SFabrice Gasnier 	else
13232aa1bbb2SFabrice Gasnier 		*ftcfg = -EINVAL;
13242aa1bbb2SFabrice Gasnier }
13252aa1bbb2SFabrice Gasnier 
132697f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port)
132797f3a085SErwan Le Ray {
132897f3a085SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
132997f3a085SErwan Le Ray }
133097f3a085SErwan Le Ray 
133156f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port,
133248a6092fSMaxime Coquelin 				 struct platform_device *pdev)
133348a6092fSMaxime Coquelin {
133448a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
133548a6092fSMaxime Coquelin 	struct resource *res;
1336e0f2a902SErwan Le Ray 	int ret, irq;
133748a6092fSMaxime Coquelin 
1338e0f2a902SErwan Le Ray 	irq = platform_get_irq(pdev, 0);
1339217b04c6STang Bin 	if (irq < 0)
1340217b04c6STang Bin 		return irq;
134192fc0023SErwan Le Ray 
134248a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
134348a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
134448a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
134548a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
1346d075719eSErwan Le Ray 	port->fifosize	= stm32port->info->cfg.fifosize;
13479feedaa7SDmitry Safonov 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1348e0f2a902SErwan Le Ray 	port->irq = irq;
134956f9a76cSErwan Le Ray 	port->rs485_config = stm32_usart_config_rs485;
13507d8f6861SBich HEMON 
135156f9a76cSErwan Le Ray 	ret = stm32_usart_init_rs485(port, pdev);
1352c150c0f3SLukas Wunner 	if (ret)
1353c150c0f3SLukas Wunner 		return ret;
13547d8f6861SBich HEMON 
13553d530017SAlexandre Torgue 	stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
13563d530017SAlexandre Torgue 		of_property_read_bool(pdev->dev.of_node, "wakeup-source");
13572c58e560SErwan Le Ray 
13583cd66593SMartin Devera 	stm32port->swap = stm32port->info->cfg.has_swap &&
13593cd66593SMartin Devera 		of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
13603cd66593SMartin Devera 
1361351a762aSGerald Baeza 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
13622aa1bbb2SFabrice Gasnier 	if (stm32port->fifoen) {
13632aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "rx-threshold",
13642aa1bbb2SFabrice Gasnier 				      &stm32port->rxftcfg);
13652aa1bbb2SFabrice Gasnier 		stm32_usart_get_ftcfg(pdev, "tx-threshold",
13662aa1bbb2SFabrice Gasnier 				      &stm32port->txftcfg);
13672aa1bbb2SFabrice Gasnier 	}
136848a6092fSMaxime Coquelin 
13693d881e32STang Bin 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
137048a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
137148a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
137248a6092fSMaxime Coquelin 	port->mapbase = res->start;
137348a6092fSMaxime Coquelin 
137448a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
137548a6092fSMaxime Coquelin 
137648a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
137748a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
137848a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
137948a6092fSMaxime Coquelin 
138048a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
138148a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
138248a6092fSMaxime Coquelin 	if (ret)
138348a6092fSMaxime Coquelin 		return ret;
138448a6092fSMaxime Coquelin 
138548a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1386ada80043SFabrice Gasnier 	if (!stm32port->port.uartclk) {
138748a6092fSMaxime Coquelin 		ret = -EINVAL;
13886cf61b9bSManivannan Sadhasivam 		goto err_clk;
1389ada80043SFabrice Gasnier 	}
139048a6092fSMaxime Coquelin 
13916cf61b9bSManivannan Sadhasivam 	stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
13926cf61b9bSManivannan Sadhasivam 	if (IS_ERR(stm32port->gpios)) {
13936cf61b9bSManivannan Sadhasivam 		ret = PTR_ERR(stm32port->gpios);
13946cf61b9bSManivannan Sadhasivam 		goto err_clk;
13956cf61b9bSManivannan Sadhasivam 	}
13966cf61b9bSManivannan Sadhasivam 
13979359369aSErwan Le Ray 	/*
13989359369aSErwan Le Ray 	 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
13999359369aSErwan Le Ray 	 * properties should not be specified.
14009359369aSErwan Le Ray 	 */
14016cf61b9bSManivannan Sadhasivam 	if (stm32port->hw_flow_control) {
14026cf61b9bSManivannan Sadhasivam 		if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
14036cf61b9bSManivannan Sadhasivam 		    mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
14046cf61b9bSManivannan Sadhasivam 			dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
14056cf61b9bSManivannan Sadhasivam 			ret = -EINVAL;
14066cf61b9bSManivannan Sadhasivam 			goto err_clk;
14076cf61b9bSManivannan Sadhasivam 		}
14086cf61b9bSManivannan Sadhasivam 	}
14096cf61b9bSManivannan Sadhasivam 
14106cf61b9bSManivannan Sadhasivam 	return ret;
14116cf61b9bSManivannan Sadhasivam 
14126cf61b9bSManivannan Sadhasivam err_clk:
14136cf61b9bSManivannan Sadhasivam 	clk_disable_unprepare(stm32port->clk);
14146cf61b9bSManivannan Sadhasivam 
141548a6092fSMaxime Coquelin 	return ret;
141648a6092fSMaxime Coquelin }
141748a6092fSMaxime Coquelin 
141856f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
141948a6092fSMaxime Coquelin {
142048a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
142148a6092fSMaxime Coquelin 	int id;
142248a6092fSMaxime Coquelin 
142348a6092fSMaxime Coquelin 	if (!np)
142448a6092fSMaxime Coquelin 		return NULL;
142548a6092fSMaxime Coquelin 
142648a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
1427e5707915SGerald Baeza 	if (id < 0) {
1428e5707915SGerald Baeza 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1429e5707915SGerald Baeza 		return NULL;
1430e5707915SGerald Baeza 	}
143148a6092fSMaxime Coquelin 
143248a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
143348a6092fSMaxime Coquelin 		return NULL;
143448a6092fSMaxime Coquelin 
14356fd9fffbSErwan Le Ray 	stm32_ports[id].hw_flow_control =
14366fd9fffbSErwan Le Ray 		of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
14376fd9fffbSErwan Le Ray 		of_property_read_bool (np, "uart-has-rtscts");
143848a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
14394cc0ed62SErwan Le Ray 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1440d0a6a7bcSErwan Le Ray 	stm32_ports[id].cr3_irq = 0;
1441e5707915SGerald Baeza 	stm32_ports[id].last_res = RX_BUF_L;
144248a6092fSMaxime Coquelin 	return &stm32_ports[id];
144348a6092fSMaxime Coquelin }
144448a6092fSMaxime Coquelin 
144548a6092fSMaxime Coquelin #ifdef CONFIG_OF
144648a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
1447ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
1448ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1449270e5a74SFabrice Gasnier 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
145048a6092fSMaxime Coquelin 	{},
145148a6092fSMaxime Coquelin };
145248a6092fSMaxime Coquelin 
145348a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
145448a6092fSMaxime Coquelin #endif
145548a6092fSMaxime Coquelin 
1456a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1457a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1458a7770a4bSErwan Le Ray {
1459a7770a4bSErwan Le Ray 	if (stm32port->rx_buf)
1460a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1461a7770a4bSErwan Le Ray 				  stm32port->rx_dma_buf);
1462a7770a4bSErwan Le Ray }
1463a7770a4bSErwan Le Ray 
146456f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_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 
1473e359b441SJohan Hovold 	/*
1474e359b441SJohan Hovold 	 * Using DMA and threaded handler for the console could lead to
1475e359b441SJohan Hovold 	 * deadlocks.
1476e359b441SJohan Hovold 	 */
1477e359b441SJohan Hovold 	if (uart_console(port))
1478e359b441SJohan Hovold 		return -ENODEV;
1479e359b441SJohan Hovold 
148059bd4eedSTang Bin 	stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
148134891872SAlexandre TORGUE 					       &stm32port->rx_dma_buf,
148234891872SAlexandre TORGUE 					       GFP_KERNEL);
1483a7770a4bSErwan Le Ray 	if (!stm32port->rx_buf)
1484a7770a4bSErwan Le Ray 		return -ENOMEM;
148534891872SAlexandre TORGUE 
148634891872SAlexandre TORGUE 	/* Configure DMA channel */
148734891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
14888e5481d9SArnd Bergmann 	config.src_addr = port->mapbase + ofs->rdr;
148934891872SAlexandre TORGUE 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
149034891872SAlexandre TORGUE 
149134891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
149234891872SAlexandre TORGUE 	if (ret < 0) {
149334891872SAlexandre TORGUE 		dev_err(dev, "rx dma channel config failed\n");
1494a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
1495a7770a4bSErwan Le Ray 		return ret;
149634891872SAlexandre TORGUE 	}
149734891872SAlexandre TORGUE 
149834891872SAlexandre TORGUE 	return 0;
1499a7770a4bSErwan Le Ray }
150034891872SAlexandre TORGUE 
1501a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1502a7770a4bSErwan Le Ray 					 struct platform_device *pdev)
1503a7770a4bSErwan Le Ray {
1504a7770a4bSErwan Le Ray 	if (stm32port->tx_buf)
1505a7770a4bSErwan Le Ray 		dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1506a7770a4bSErwan Le Ray 				  stm32port->tx_dma_buf);
150734891872SAlexandre TORGUE }
150834891872SAlexandre TORGUE 
150956f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
151034891872SAlexandre TORGUE 				       struct platform_device *pdev)
151134891872SAlexandre TORGUE {
1512d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
151334891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
151434891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
151534891872SAlexandre TORGUE 	struct dma_slave_config config;
151634891872SAlexandre TORGUE 	int ret;
151734891872SAlexandre TORGUE 
151859bd4eedSTang Bin 	stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
151934891872SAlexandre TORGUE 					       &stm32port->tx_dma_buf,
152034891872SAlexandre TORGUE 					       GFP_KERNEL);
1521a7770a4bSErwan Le Ray 	if (!stm32port->tx_buf)
1522a7770a4bSErwan Le Ray 		return -ENOMEM;
152334891872SAlexandre TORGUE 
152434891872SAlexandre TORGUE 	/* Configure DMA channel */
152534891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
15268e5481d9SArnd Bergmann 	config.dst_addr = port->mapbase + ofs->tdr;
152734891872SAlexandre TORGUE 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
152834891872SAlexandre TORGUE 
152934891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
153034891872SAlexandre TORGUE 	if (ret < 0) {
153134891872SAlexandre TORGUE 		dev_err(dev, "tx dma channel config failed\n");
1532a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1533a7770a4bSErwan Le Ray 		return ret;
153434891872SAlexandre TORGUE 	}
153534891872SAlexandre TORGUE 
153634891872SAlexandre TORGUE 	return 0;
153734891872SAlexandre TORGUE }
153834891872SAlexandre TORGUE 
153956f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev)
154048a6092fSMaxime Coquelin {
154148a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
1542ada8618fSAlexandre TORGUE 	int ret;
154348a6092fSMaxime Coquelin 
154456f9a76cSErwan Le Ray 	stm32port = stm32_usart_of_get_port(pdev);
154548a6092fSMaxime Coquelin 	if (!stm32port)
154648a6092fSMaxime Coquelin 		return -ENODEV;
154748a6092fSMaxime Coquelin 
1548d825f0beSStephen Boyd 	stm32port->info = of_device_get_match_data(&pdev->dev);
1549d825f0beSStephen Boyd 	if (!stm32port->info)
1550ada8618fSAlexandre TORGUE 		return -EINVAL;
1551ada8618fSAlexandre TORGUE 
155256f9a76cSErwan Le Ray 	ret = stm32_usart_init_port(stm32port, pdev);
155348a6092fSMaxime Coquelin 	if (ret)
155448a6092fSMaxime Coquelin 		return ret;
155548a6092fSMaxime Coquelin 
15563d530017SAlexandre Torgue 	if (stm32port->wakeup_src) {
15573d530017SAlexandre Torgue 		device_set_wakeup_capable(&pdev->dev, true);
15583d530017SAlexandre Torgue 		ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
15595297f274SErwan Le Ray 		if (ret)
1560a7770a4bSErwan Le Ray 			goto err_deinit_port;
1561270e5a74SFabrice Gasnier 	}
1562270e5a74SFabrice Gasnier 
1563a7770a4bSErwan Le Ray 	stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
1564a7770a4bSErwan Le Ray 	if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) {
1565a7770a4bSErwan Le Ray 		ret = -EPROBE_DEFER;
1566a7770a4bSErwan Le Ray 		goto err_wakeirq;
1567a7770a4bSErwan Le Ray 	}
1568a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1569a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->rx_ch))
1570a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
157134891872SAlexandre TORGUE 
1572a7770a4bSErwan Le Ray 	stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1573a7770a4bSErwan Le Ray 	if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1574a7770a4bSErwan Le Ray 		ret = -EPROBE_DEFER;
1575a7770a4bSErwan Le Ray 		goto err_dma_rx;
1576a7770a4bSErwan Le Ray 	}
1577a7770a4bSErwan Le Ray 	/* Fall back in interrupt mode for any non-deferral error */
1578a7770a4bSErwan Le Ray 	if (IS_ERR(stm32port->tx_ch))
1579a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1580a7770a4bSErwan Le Ray 
1581a7770a4bSErwan Le Ray 	if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1582a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1583a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->rx_ch);
1584a7770a4bSErwan Le Ray 		stm32port->rx_ch = NULL;
1585a7770a4bSErwan Le Ray 	}
1586a7770a4bSErwan Le Ray 
1587a7770a4bSErwan Le Ray 	if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1588a7770a4bSErwan Le Ray 		/* Fall back in interrupt mode */
1589a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->tx_ch);
1590a7770a4bSErwan Le Ray 		stm32port->tx_ch = NULL;
1591a7770a4bSErwan Le Ray 	}
1592a7770a4bSErwan Le Ray 
1593a7770a4bSErwan Le Ray 	if (!stm32port->rx_ch)
1594a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1595a7770a4bSErwan Le Ray 	if (!stm32port->tx_ch)
1596a7770a4bSErwan Le Ray 		dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
159734891872SAlexandre TORGUE 
159848a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
159948a6092fSMaxime Coquelin 
1600fb6dcef6SErwan Le Ray 	pm_runtime_get_noresume(&pdev->dev);
1601fb6dcef6SErwan Le Ray 	pm_runtime_set_active(&pdev->dev);
1602fb6dcef6SErwan Le Ray 	pm_runtime_enable(&pdev->dev);
160387fd0741SErwan Le Ray 
160487fd0741SErwan Le Ray 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
160587fd0741SErwan Le Ray 	if (ret)
160687fd0741SErwan Le Ray 		goto err_port;
160787fd0741SErwan Le Ray 
1608fb6dcef6SErwan Le Ray 	pm_runtime_put_sync(&pdev->dev);
1609fb6dcef6SErwan Le Ray 
161048a6092fSMaxime Coquelin 	return 0;
1611ada80043SFabrice Gasnier 
161287fd0741SErwan Le Ray err_port:
161387fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
161487fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
161587fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
161687fd0741SErwan Le Ray 
161787fd0741SErwan Le Ray 	if (stm32port->tx_ch) {
1618a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
161987fd0741SErwan Le Ray 		dma_release_channel(stm32port->tx_ch);
162087fd0741SErwan Le Ray 	}
162187fd0741SErwan Le Ray 
1622a7770a4bSErwan Le Ray 	if (stm32port->rx_ch)
1623a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
162487fd0741SErwan Le Ray 
1625a7770a4bSErwan Le Ray err_dma_rx:
1626a7770a4bSErwan Le Ray 	if (stm32port->rx_ch)
1627a7770a4bSErwan Le Ray 		dma_release_channel(stm32port->rx_ch);
1628a7770a4bSErwan Le Ray 
1629a7770a4bSErwan Le Ray err_wakeirq:
16303d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
16315297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
16325297f274SErwan Le Ray 
1633a7770a4bSErwan Le Ray err_deinit_port:
16343d530017SAlexandre Torgue 	if (stm32port->wakeup_src)
16353d530017SAlexandre Torgue 		device_set_wakeup_capable(&pdev->dev, false);
1636270e5a74SFabrice Gasnier 
163797f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32port);
1638ada80043SFabrice Gasnier 
1639ada80043SFabrice Gasnier 	return ret;
164048a6092fSMaxime Coquelin }
164148a6092fSMaxime Coquelin 
164256f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev)
164348a6092fSMaxime Coquelin {
164448a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
1645511c7b1bSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1646d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1647fb6dcef6SErwan Le Ray 	int err;
164833bb2f6aSErwan Le Ray 	u32 cr3;
1649fb6dcef6SErwan Le Ray 
1650fb6dcef6SErwan Le Ray 	pm_runtime_get_sync(&pdev->dev);
165187fd0741SErwan Le Ray 	err = uart_remove_one_port(&stm32_usart_driver, port);
165287fd0741SErwan Le Ray 	if (err)
165387fd0741SErwan Le Ray 		return(err);
165487fd0741SErwan Le Ray 
165587fd0741SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
165687fd0741SErwan Le Ray 	pm_runtime_set_suspended(&pdev->dev);
165787fd0741SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
165834891872SAlexandre TORGUE 
165933bb2f6aSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
166033bb2f6aSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
166133bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_EIE;
166233bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_DMAR;
166333bb2f6aSErwan Le Ray 	cr3 &= ~USART_CR3_DDRE;
166433bb2f6aSErwan Le Ray 	writel_relaxed(cr3, port->membase + ofs->cr3);
166534891872SAlexandre TORGUE 
166687fd0741SErwan Le Ray 	if (stm32_port->tx_ch) {
1667a7770a4bSErwan Le Ray 		stm32_usart_of_dma_tx_remove(stm32_port, pdev);
166834891872SAlexandre TORGUE 		dma_release_channel(stm32_port->tx_ch);
166987fd0741SErwan Le Ray 	}
167034891872SAlexandre TORGUE 
1671a7770a4bSErwan Le Ray 	if (stm32_port->rx_ch) {
1672a7770a4bSErwan Le Ray 		stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1673a7770a4bSErwan Le Ray 		dma_release_channel(stm32_port->rx_ch);
1674a7770a4bSErwan Le Ray 	}
1675a7770a4bSErwan Le Ray 
1676a7770a4bSErwan Le Ray 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1677511c7b1bSAlexandre TORGUE 
16783d530017SAlexandre Torgue 	if (stm32_port->wakeup_src) {
16795297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
1680270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
16815297f274SErwan Le Ray 	}
1682270e5a74SFabrice Gasnier 
168397f3a085SErwan Le Ray 	stm32_usart_deinit_port(stm32_port);
168448a6092fSMaxime Coquelin 
168587fd0741SErwan Le Ray 	return 0;
168648a6092fSMaxime Coquelin }
168748a6092fSMaxime Coquelin 
16881f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
168948a6092fSMaxime Coquelin {
1690ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1691d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
169228fb1a92SValentin Caron 	u32 isr;
169328fb1a92SValentin Caron 	int ret;
1694ada8618fSAlexandre TORGUE 
169528fb1a92SValentin Caron 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr,
169628fb1a92SValentin Caron 						(isr & USART_SR_TXE), 100,
169728fb1a92SValentin Caron 						STM32_USART_TIMEOUT_USEC);
169828fb1a92SValentin Caron 	if (ret != 0) {
169928fb1a92SValentin Caron 		dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret);
170028fb1a92SValentin Caron 		return;
170128fb1a92SValentin Caron 	}
1702ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
170348a6092fSMaxime Coquelin }
170448a6092fSMaxime Coquelin 
17051f507b3aSValentin Caron #ifdef CONFIG_SERIAL_STM32_CONSOLE
170656f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s,
170792fc0023SErwan Le Ray 				      unsigned int cnt)
170848a6092fSMaxime Coquelin {
170948a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
1710ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1711d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1712d825f0beSStephen Boyd 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
171348a6092fSMaxime Coquelin 	unsigned long flags;
171448a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
171548a6092fSMaxime Coquelin 	int locked = 1;
171648a6092fSMaxime Coquelin 
1717cea37afdSJohan Hovold 	if (oops_in_progress)
1718cea37afdSJohan Hovold 		locked = spin_trylock_irqsave(&port->lock, flags);
171948a6092fSMaxime Coquelin 	else
1720cea37afdSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
172148a6092fSMaxime Coquelin 
172287f1f809SAlexandre TORGUE 	/* Save and disable interrupts, enable the transmitter */
1723ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
172448a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
172587f1f809SAlexandre TORGUE 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1726ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
172748a6092fSMaxime Coquelin 
172856f9a76cSErwan Le Ray 	uart_console_write(port, s, cnt, stm32_usart_console_putchar);
172948a6092fSMaxime Coquelin 
173048a6092fSMaxime Coquelin 	/* Restore interrupt state */
1731ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
173248a6092fSMaxime Coquelin 
173348a6092fSMaxime Coquelin 	if (locked)
1734cea37afdSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
173548a6092fSMaxime Coquelin }
173648a6092fSMaxime Coquelin 
173756f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options)
173848a6092fSMaxime Coquelin {
173948a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
174048a6092fSMaxime Coquelin 	int baud = 9600;
174148a6092fSMaxime Coquelin 	int bits = 8;
174248a6092fSMaxime Coquelin 	int parity = 'n';
174348a6092fSMaxime Coquelin 	int flow = 'n';
174448a6092fSMaxime Coquelin 
174548a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
174648a6092fSMaxime Coquelin 		return -ENODEV;
174748a6092fSMaxime Coquelin 
174848a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
174948a6092fSMaxime Coquelin 
175048a6092fSMaxime Coquelin 	/*
175148a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
175248a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
175348a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
175448a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
175548a6092fSMaxime Coquelin 	 */
175692fc0023SErwan Le Ray 	if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
175748a6092fSMaxime Coquelin 		return -ENXIO;
175848a6092fSMaxime Coquelin 
175948a6092fSMaxime Coquelin 	if (options)
176048a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
176148a6092fSMaxime Coquelin 
176248a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
176348a6092fSMaxime Coquelin }
176448a6092fSMaxime Coquelin 
176548a6092fSMaxime Coquelin static struct console stm32_console = {
176648a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
176748a6092fSMaxime Coquelin 	.device		= uart_console_device,
176856f9a76cSErwan Le Ray 	.write		= stm32_usart_console_write,
176956f9a76cSErwan Le Ray 	.setup		= stm32_usart_console_setup,
177048a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
177148a6092fSMaxime Coquelin 	.index		= -1,
177248a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
177348a6092fSMaxime Coquelin };
177448a6092fSMaxime Coquelin 
177548a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
177648a6092fSMaxime Coquelin 
177748a6092fSMaxime Coquelin #else
177848a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
177948a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
178048a6092fSMaxime Coquelin 
17818043b16fSValentin Caron #ifdef CONFIG_SERIAL_EARLYCON
17828043b16fSValentin Caron static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
17838043b16fSValentin Caron {
17848043b16fSValentin Caron 	struct stm32_usart_info *info = port->private_data;
17858043b16fSValentin Caron 
17868043b16fSValentin Caron 	while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE))
17878043b16fSValentin Caron 		cpu_relax();
17888043b16fSValentin Caron 
17898043b16fSValentin Caron 	writel_relaxed(ch, port->membase + info->ofs.tdr);
17908043b16fSValentin Caron }
17918043b16fSValentin Caron 
17928043b16fSValentin Caron static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count)
17938043b16fSValentin Caron {
17948043b16fSValentin Caron 	struct earlycon_device *device = console->data;
17958043b16fSValentin Caron 	struct uart_port *port = &device->port;
17968043b16fSValentin Caron 
17978043b16fSValentin Caron 	uart_console_write(port, s, count, early_stm32_usart_console_putchar);
17988043b16fSValentin Caron }
17998043b16fSValentin Caron 
18008043b16fSValentin Caron static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
18018043b16fSValentin Caron {
18028043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
18038043b16fSValentin Caron 		return -ENODEV;
18048043b16fSValentin Caron 	device->port.private_data = &stm32h7_info;
18058043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
18068043b16fSValentin Caron 	return 0;
18078043b16fSValentin Caron }
18088043b16fSValentin Caron 
18098043b16fSValentin Caron static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
18108043b16fSValentin Caron {
18118043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
18128043b16fSValentin Caron 		return -ENODEV;
18138043b16fSValentin Caron 	device->port.private_data = &stm32f7_info;
18148043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
18158043b16fSValentin Caron 	return 0;
18168043b16fSValentin Caron }
18178043b16fSValentin Caron 
18188043b16fSValentin Caron static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
18198043b16fSValentin Caron {
18208043b16fSValentin Caron 	if (!(device->port.membase || device->port.iobase))
18218043b16fSValentin Caron 		return -ENODEV;
18228043b16fSValentin Caron 	device->port.private_data = &stm32f4_info;
18238043b16fSValentin Caron 	device->con->write = early_stm32_serial_write;
18248043b16fSValentin Caron 	return 0;
18258043b16fSValentin Caron }
18268043b16fSValentin Caron 
18278043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup);
18288043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup);
18298043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup);
18308043b16fSValentin Caron #endif /* CONFIG_SERIAL_EARLYCON */
18318043b16fSValentin Caron 
183248a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
183348a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
183448a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
183548a6092fSMaxime Coquelin 	.major		= 0,
183648a6092fSMaxime Coquelin 	.minor		= 0,
183748a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
183848a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
183948a6092fSMaxime Coquelin };
184048a6092fSMaxime Coquelin 
18416eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1842fe94347dSErwan Le Ray 						       bool enable)
1843270e5a74SFabrice Gasnier {
1844270e5a74SFabrice Gasnier 	struct stm32_port *stm32_port = to_stm32_port(port);
1845d825f0beSStephen Boyd 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
18466eeb348cSErwan Le Ray 	struct tty_port *tport = &port->state->port;
18476eeb348cSErwan Le Ray 	int ret;
18486333a485SErwan Le Ray 	unsigned int size;
18496333a485SErwan Le Ray 	unsigned long flags;
1850270e5a74SFabrice Gasnier 
18516eeb348cSErwan Le Ray 	if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
18526eeb348cSErwan Le Ray 		return 0;
1853270e5a74SFabrice Gasnier 
185412761869SErwan Le Ray 	/*
185512761869SErwan Le Ray 	 * Enable low-power wake-up and wake-up irq if argument is set to
185612761869SErwan Le Ray 	 * "enable", disable low-power wake-up and wake-up irq otherwise
185712761869SErwan Le Ray 	 */
1858270e5a74SFabrice Gasnier 	if (enable) {
185956f9a76cSErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
186012761869SErwan Le Ray 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
18617547d9abSErwan Le Ray 		mctrl_gpio_enable_irq_wake(stm32_port->gpios);
18626eeb348cSErwan Le Ray 
18636eeb348cSErwan Le Ray 		/*
18646eeb348cSErwan Le Ray 		 * When DMA is used for reception, it must be disabled before
18656eeb348cSErwan Le Ray 		 * entering low-power mode and re-enabled when exiting from
18666eeb348cSErwan Le Ray 		 * low-power mode.
18676eeb348cSErwan Le Ray 		 */
18686eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
18696333a485SErwan Le Ray 			spin_lock_irqsave(&port->lock, flags);
18706333a485SErwan Le Ray 			/* Avoid race with RX IRQ when DMAR is cleared */
18716eeb348cSErwan Le Ray 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
18726333a485SErwan Le Ray 			/* Poll data from DMA RX buffer if any */
18736333a485SErwan Le Ray 			size = stm32_usart_receive_chars(port, true);
18746333a485SErwan Le Ray 			dmaengine_terminate_async(stm32_port->rx_ch);
18756333a485SErwan Le Ray 			uart_unlock_and_check_sysrq_irqrestore(port, flags);
18766333a485SErwan Le Ray 			if (size)
18776333a485SErwan Le Ray 				tty_flip_buffer_push(tport);
18786eeb348cSErwan Le Ray 		}
18796eeb348cSErwan Le Ray 
18806eeb348cSErwan Le Ray 		/* Poll data from RX FIFO if any */
18816eeb348cSErwan Le Ray 		stm32_usart_receive_chars(port, false);
1882270e5a74SFabrice Gasnier 	} else {
18836eeb348cSErwan Le Ray 		if (stm32_port->rx_ch) {
18846eeb348cSErwan Le Ray 			ret = stm32_usart_start_rx_dma_cyclic(port);
18856eeb348cSErwan Le Ray 			if (ret)
18866eeb348cSErwan Le Ray 				return ret;
18876eeb348cSErwan Le Ray 		}
18887547d9abSErwan Le Ray 		mctrl_gpio_disable_irq_wake(stm32_port->gpios);
188956f9a76cSErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
189012761869SErwan Le Ray 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
1891270e5a74SFabrice Gasnier 	}
18926eeb348cSErwan Le Ray 
18936eeb348cSErwan Le Ray 	return 0;
1894270e5a74SFabrice Gasnier }
1895270e5a74SFabrice Gasnier 
189656f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
1897270e5a74SFabrice Gasnier {
1898270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
18996eeb348cSErwan Le Ray 	int ret;
1900270e5a74SFabrice Gasnier 
1901270e5a74SFabrice Gasnier 	uart_suspend_port(&stm32_usart_driver, port);
1902270e5a74SFabrice Gasnier 
19036eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
19046eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, true);
19056eeb348cSErwan Le Ray 		if (ret)
19066eeb348cSErwan Le Ray 			return ret;
19076eeb348cSErwan Le Ray 	}
1908270e5a74SFabrice Gasnier 
190955484fccSErwan Le Ray 	/*
191055484fccSErwan Le Ray 	 * When "no_console_suspend" is enabled, keep the pinctrl default state
191155484fccSErwan Le Ray 	 * and rely on bootloader stage to restore this state upon resume.
191255484fccSErwan Le Ray 	 * Otherwise, apply the idle or sleep states depending on wakeup
191355484fccSErwan Le Ray 	 * capabilities.
191455484fccSErwan Le Ray 	 */
191555484fccSErwan Le Ray 	if (console_suspend_enabled || !uart_console(port)) {
19161631eeeaSErwan Le Ray 		if (device_may_wakeup(dev) || device_wakeup_path(dev))
191755484fccSErwan Le Ray 			pinctrl_pm_select_idle_state(dev);
191855484fccSErwan Le Ray 		else
191994616d9aSErwan Le Ray 			pinctrl_pm_select_sleep_state(dev);
192055484fccSErwan Le Ray 	}
192194616d9aSErwan Le Ray 
1922270e5a74SFabrice Gasnier 	return 0;
1923270e5a74SFabrice Gasnier }
1924270e5a74SFabrice Gasnier 
192556f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
1926270e5a74SFabrice Gasnier {
1927270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
19286eeb348cSErwan Le Ray 	int ret;
1929270e5a74SFabrice Gasnier 
193094616d9aSErwan Le Ray 	pinctrl_pm_select_default_state(dev);
193194616d9aSErwan Le Ray 
19326eeb348cSErwan Le Ray 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
19336eeb348cSErwan Le Ray 		ret = stm32_usart_serial_en_wakeup(port, false);
19346eeb348cSErwan Le Ray 		if (ret)
19356eeb348cSErwan Le Ray 			return ret;
19366eeb348cSErwan Le Ray 	}
1937270e5a74SFabrice Gasnier 
1938270e5a74SFabrice Gasnier 	return uart_resume_port(&stm32_usart_driver, port);
1939270e5a74SFabrice Gasnier }
1940270e5a74SFabrice Gasnier 
194156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
1942fb6dcef6SErwan Le Ray {
1943fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1944fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1945fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1946fb6dcef6SErwan Le Ray 
1947fb6dcef6SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
1948fb6dcef6SErwan Le Ray 
1949fb6dcef6SErwan Le Ray 	return 0;
1950fb6dcef6SErwan Le Ray }
1951fb6dcef6SErwan Le Ray 
195256f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
1953fb6dcef6SErwan Le Ray {
1954fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1955fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1956fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1957fb6dcef6SErwan Le Ray 
1958fb6dcef6SErwan Le Ray 	return clk_prepare_enable(stm32port->clk);
1959fb6dcef6SErwan Le Ray }
1960fb6dcef6SErwan Le Ray 
1961270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = {
196256f9a76cSErwan Le Ray 	SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
196356f9a76cSErwan Le Ray 			   stm32_usart_runtime_resume, NULL)
196456f9a76cSErwan Le Ray 	SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
196556f9a76cSErwan Le Ray 				stm32_usart_serial_resume)
1966270e5a74SFabrice Gasnier };
1967270e5a74SFabrice Gasnier 
196848a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
196956f9a76cSErwan Le Ray 	.probe		= stm32_usart_serial_probe,
197056f9a76cSErwan Le Ray 	.remove		= stm32_usart_serial_remove,
197148a6092fSMaxime Coquelin 	.driver	= {
197248a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
1973270e5a74SFabrice Gasnier 		.pm	= &stm32_serial_pm_ops,
197448a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
197548a6092fSMaxime Coquelin 	},
197648a6092fSMaxime Coquelin };
197748a6092fSMaxime Coquelin 
197856f9a76cSErwan Le Ray static int __init stm32_usart_init(void)
197948a6092fSMaxime Coquelin {
198048a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
198148a6092fSMaxime Coquelin 	int ret;
198248a6092fSMaxime Coquelin 
198348a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
198448a6092fSMaxime Coquelin 
198548a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
198648a6092fSMaxime Coquelin 	if (ret)
198748a6092fSMaxime Coquelin 		return ret;
198848a6092fSMaxime Coquelin 
198948a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
199048a6092fSMaxime Coquelin 	if (ret)
199148a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
199248a6092fSMaxime Coquelin 
199348a6092fSMaxime Coquelin 	return ret;
199448a6092fSMaxime Coquelin }
199548a6092fSMaxime Coquelin 
199656f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void)
199748a6092fSMaxime Coquelin {
199848a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
199948a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
200048a6092fSMaxime Coquelin }
200148a6092fSMaxime Coquelin 
200256f9a76cSErwan Le Ray module_init(stm32_usart_init);
200356f9a76cSErwan Le Ray module_exit(stm32_usart_exit);
200448a6092fSMaxime Coquelin 
200548a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
200648a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
200748a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
2008