xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision fdf16d78941b4f380753053d229955baddd00712)
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>
6ada8618fSAlexandre TORGUE  *	     Gerald Baeza <gerald.baeza@st.com>
748a6092fSMaxime Coquelin  *
848a6092fSMaxime Coquelin  * Inspired by st-asc.c from STMicroelectronics (c)
948a6092fSMaxime Coquelin  */
1048a6092fSMaxime Coquelin 
1134891872SAlexandre TORGUE #include <linux/clk.h>
1248a6092fSMaxime Coquelin #include <linux/console.h>
1348a6092fSMaxime Coquelin #include <linux/delay.h>
1434891872SAlexandre TORGUE #include <linux/dma-direction.h>
1534891872SAlexandre TORGUE #include <linux/dmaengine.h>
1634891872SAlexandre TORGUE #include <linux/dma-mapping.h>
1734891872SAlexandre TORGUE #include <linux/io.h>
1834891872SAlexandre TORGUE #include <linux/iopoll.h>
1934891872SAlexandre TORGUE #include <linux/irq.h>
2034891872SAlexandre TORGUE #include <linux/module.h>
2148a6092fSMaxime Coquelin #include <linux/of.h>
2248a6092fSMaxime Coquelin #include <linux/of_platform.h>
2394616d9aSErwan Le Ray #include <linux/pinctrl/consumer.h>
2434891872SAlexandre TORGUE #include <linux/platform_device.h>
2534891872SAlexandre TORGUE #include <linux/pm_runtime.h>
26270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h>
2748a6092fSMaxime Coquelin #include <linux/serial_core.h>
2834891872SAlexandre TORGUE #include <linux/serial.h>
2934891872SAlexandre TORGUE #include <linux/spinlock.h>
3034891872SAlexandre TORGUE #include <linux/sysrq.h>
3134891872SAlexandre TORGUE #include <linux/tty_flip.h>
3234891872SAlexandre TORGUE #include <linux/tty.h>
3348a6092fSMaxime Coquelin 
346cf61b9bSManivannan Sadhasivam #include "serial_mctrl_gpio.h"
35bc5a0b55SAlexandre TORGUE #include "stm32-usart.h"
3648a6092fSMaxime Coquelin 
3748a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port);
3834891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port);
3948a6092fSMaxime Coquelin 
4048a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
4148a6092fSMaxime Coquelin {
4248a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
4348a6092fSMaxime Coquelin }
4448a6092fSMaxime Coquelin 
4548a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
4648a6092fSMaxime Coquelin {
4748a6092fSMaxime Coquelin 	u32 val;
4848a6092fSMaxime Coquelin 
4948a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
5048a6092fSMaxime Coquelin 	val |= bits;
5148a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
5248a6092fSMaxime Coquelin }
5348a6092fSMaxime Coquelin 
5448a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
5548a6092fSMaxime Coquelin {
5648a6092fSMaxime Coquelin 	u32 val;
5748a6092fSMaxime Coquelin 
5848a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
5948a6092fSMaxime Coquelin 	val &= ~bits;
6048a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
6148a6092fSMaxime Coquelin }
6248a6092fSMaxime Coquelin 
631bcda09dSBich HEMON static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
641bcda09dSBich HEMON 				   u32 delay_DDE, u32 baud)
651bcda09dSBich HEMON {
661bcda09dSBich HEMON 	u32 rs485_deat_dedt;
671bcda09dSBich HEMON 	u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
681bcda09dSBich HEMON 	bool over8;
691bcda09dSBich HEMON 
701bcda09dSBich HEMON 	*cr3 |= USART_CR3_DEM;
711bcda09dSBich HEMON 	over8 = *cr1 & USART_CR1_OVER8;
721bcda09dSBich HEMON 
731bcda09dSBich HEMON 	if (over8)
741bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 8;
751bcda09dSBich HEMON 	else
761bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 16;
771bcda09dSBich HEMON 
781bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
791bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
801bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
811bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
821bcda09dSBich HEMON 			   USART_CR1_DEAT_MASK;
831bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
841bcda09dSBich HEMON 
851bcda09dSBich HEMON 	if (over8)
861bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 8;
871bcda09dSBich HEMON 	else
881bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 16;
891bcda09dSBich HEMON 
901bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
911bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
921bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
931bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
941bcda09dSBich HEMON 			   USART_CR1_DEDT_MASK;
951bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
961bcda09dSBich HEMON }
971bcda09dSBich HEMON 
981bcda09dSBich HEMON static int stm32_config_rs485(struct uart_port *port,
991bcda09dSBich HEMON 			      struct serial_rs485 *rs485conf)
1001bcda09dSBich HEMON {
1011bcda09dSBich HEMON 	struct stm32_port *stm32_port = to_stm32_port(port);
1021bcda09dSBich HEMON 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1031bcda09dSBich HEMON 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1041bcda09dSBich HEMON 	u32 usartdiv, baud, cr1, cr3;
1051bcda09dSBich HEMON 	bool over8;
1061bcda09dSBich HEMON 
1071bcda09dSBich HEMON 	stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1081bcda09dSBich HEMON 
1091bcda09dSBich HEMON 	port->rs485 = *rs485conf;
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);
1251bcda09dSBich HEMON 		stm32_config_reg_rs485(&cr1, &cr3,
1261bcda09dSBich HEMON 				       rs485conf->delay_rts_before_send,
1271bcda09dSBich HEMON 				       rs485conf->delay_rts_after_send, baud);
1281bcda09dSBich HEMON 
1291bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
1301bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
1311bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1327df5081cSMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
1337df5081cSMarek Vasut 					stm32_port->port.mctrl & ~TIOCM_RTS);
1341bcda09dSBich HEMON 		} else {
1351bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
1361bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1377df5081cSMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
1387df5081cSMarek Vasut 					stm32_port->port.mctrl | TIOCM_RTS);
1391bcda09dSBich HEMON 		}
1401bcda09dSBich HEMON 
1411bcda09dSBich HEMON 		writel_relaxed(cr3, port->membase + ofs->cr3);
1421bcda09dSBich HEMON 		writel_relaxed(cr1, port->membase + ofs->cr1);
1431bcda09dSBich HEMON 	} else {
1441bcda09dSBich HEMON 		stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP);
1451bcda09dSBich HEMON 		stm32_clr_bits(port, ofs->cr1,
1461bcda09dSBich HEMON 			       USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1471bcda09dSBich HEMON 	}
1481bcda09dSBich HEMON 
1491bcda09dSBich HEMON 	stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1501bcda09dSBich HEMON 
1511bcda09dSBich HEMON 	return 0;
1521bcda09dSBich HEMON }
1531bcda09dSBich HEMON 
1541bcda09dSBich HEMON static int stm32_init_rs485(struct uart_port *port,
1551bcda09dSBich HEMON 			    struct platform_device *pdev)
1561bcda09dSBich HEMON {
1571bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
1581bcda09dSBich HEMON 
1591bcda09dSBich HEMON 	rs485conf->flags = 0;
1601bcda09dSBich HEMON 	rs485conf->delay_rts_before_send = 0;
1611bcda09dSBich HEMON 	rs485conf->delay_rts_after_send = 0;
1621bcda09dSBich HEMON 
1631bcda09dSBich HEMON 	if (!pdev->dev.of_node)
1641bcda09dSBich HEMON 		return -ENODEV;
1651bcda09dSBich HEMON 
166c150c0f3SLukas Wunner 	return uart_get_rs485_mode(port);
1671bcda09dSBich HEMON }
1681bcda09dSBich HEMON 
169b97055bcSBaoyou Xie static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
17034891872SAlexandre TORGUE 			    bool threaded)
17134891872SAlexandre TORGUE {
17234891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
17334891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
17434891872SAlexandre TORGUE 	enum dma_status status;
17534891872SAlexandre TORGUE 	struct dma_tx_state state;
17634891872SAlexandre TORGUE 
17734891872SAlexandre TORGUE 	*sr = readl_relaxed(port->membase + ofs->isr);
17834891872SAlexandre TORGUE 
17934891872SAlexandre TORGUE 	if (threaded && stm32_port->rx_ch) {
18034891872SAlexandre TORGUE 		status = dmaengine_tx_status(stm32_port->rx_ch,
18134891872SAlexandre TORGUE 					     stm32_port->rx_ch->cookie,
18234891872SAlexandre TORGUE 					     &state);
18334891872SAlexandre TORGUE 		if ((status == DMA_IN_PROGRESS) &&
18434891872SAlexandre TORGUE 		    (*last_res != state.residue))
18534891872SAlexandre TORGUE 			return 1;
18634891872SAlexandre TORGUE 		else
18734891872SAlexandre TORGUE 			return 0;
18834891872SAlexandre TORGUE 	} else if (*sr & USART_SR_RXNE) {
18934891872SAlexandre TORGUE 		return 1;
19034891872SAlexandre TORGUE 	}
19134891872SAlexandre TORGUE 	return 0;
19234891872SAlexandre TORGUE }
19334891872SAlexandre TORGUE 
1946c5962f3SErwan Le Ray static unsigned long stm32_get_char(struct uart_port *port, u32 *sr,
1956c5962f3SErwan Le Ray 				    int *last_res)
19634891872SAlexandre TORGUE {
19734891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
19834891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
19934891872SAlexandre TORGUE 	unsigned long c;
20034891872SAlexandre TORGUE 
20134891872SAlexandre TORGUE 	if (stm32_port->rx_ch) {
20234891872SAlexandre TORGUE 		c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
20334891872SAlexandre TORGUE 		if ((*last_res) == 0)
20434891872SAlexandre TORGUE 			*last_res = RX_BUF_L;
20534891872SAlexandre TORGUE 	} else {
2066c5962f3SErwan Le Ray 		c = readl_relaxed(port->membase + ofs->rdr);
2076c5962f3SErwan Le Ray 		/* apply RDR data mask */
2086c5962f3SErwan Le Ray 		c &= stm32_port->rdr_mask;
20934891872SAlexandre TORGUE 	}
2106c5962f3SErwan Le Ray 
2116c5962f3SErwan Le Ray 	return c;
21234891872SAlexandre TORGUE }
21334891872SAlexandre TORGUE 
21434891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded)
21548a6092fSMaxime Coquelin {
21648a6092fSMaxime Coquelin 	struct tty_port *tport = &port->state->port;
217ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
218ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
21948a6092fSMaxime Coquelin 	unsigned long c;
22048a6092fSMaxime Coquelin 	u32 sr;
22148a6092fSMaxime Coquelin 	char flag;
22248a6092fSMaxime Coquelin 
22329d60981SAndy Shevchenko 	if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
22448a6092fSMaxime Coquelin 		pm_wakeup_event(tport->tty->dev, 0);
22548a6092fSMaxime Coquelin 
226e5707915SGerald Baeza 	while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
22748a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
22848a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
22948a6092fSMaxime Coquelin 
2304f01d833SErwan Le Ray 		/*
2314f01d833SErwan Le Ray 		 * Status bits has to be cleared before reading the RDR:
2324f01d833SErwan Le Ray 		 * In FIFO mode, reading the RDR will pop the next data
2334f01d833SErwan Le Ray 		 * (if any) along with its status bits into the SR.
2344f01d833SErwan Le Ray 		 * Not doing so leads to misalignement between RDR and SR,
2354f01d833SErwan Le Ray 		 * and clear status bits of the next rx data.
2364f01d833SErwan Le Ray 		 *
2374f01d833SErwan Le Ray 		 * Clear errors flags for stm32f7 and stm32h7 compatible
2384f01d833SErwan Le Ray 		 * devices. On stm32f4 compatible devices, the error bit is
2394f01d833SErwan Le Ray 		 * cleared by the sequence [read SR - read DR].
2404f01d833SErwan Le Ray 		 */
2414f01d833SErwan Le Ray 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
2421250ed71SFabrice Gasnier 			writel_relaxed(sr & USART_SR_ERR_MASK,
2431250ed71SFabrice Gasnier 				       port->membase + ofs->icr);
2444f01d833SErwan Le Ray 
2454f01d833SErwan Le Ray 		c = stm32_get_char(port, &sr, &stm32_port->last_res);
2464f01d833SErwan Le Ray 		port->icount.rx++;
24748a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
2484f01d833SErwan Le Ray 			if (sr & USART_SR_ORE) {
24948a6092fSMaxime Coquelin 				port->icount.overrun++;
25048a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
25148a6092fSMaxime Coquelin 				port->icount.parity++;
25248a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
2534f01d833SErwan Le Ray 				/* Break detection if character is null */
2544f01d833SErwan Le Ray 				if (!c) {
2554f01d833SErwan Le Ray 					port->icount.brk++;
2564f01d833SErwan Le Ray 					if (uart_handle_break(port))
2574f01d833SErwan Le Ray 						continue;
2584f01d833SErwan Le Ray 				} else {
25948a6092fSMaxime Coquelin 					port->icount.frame++;
26048a6092fSMaxime Coquelin 				}
2614f01d833SErwan Le Ray 			}
26248a6092fSMaxime Coquelin 
26348a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
26448a6092fSMaxime Coquelin 
2654f01d833SErwan Le Ray 			if (sr & USART_SR_PE) {
26648a6092fSMaxime Coquelin 				flag = TTY_PARITY;
2674f01d833SErwan Le Ray 			} else if (sr & USART_SR_FE) {
2684f01d833SErwan Le Ray 				if (!c)
2694f01d833SErwan Le Ray 					flag = TTY_BREAK;
2704f01d833SErwan Le Ray 				else
27148a6092fSMaxime Coquelin 					flag = TTY_FRAME;
27248a6092fSMaxime Coquelin 			}
2734f01d833SErwan Le Ray 		}
27448a6092fSMaxime Coquelin 
27548a6092fSMaxime Coquelin 		if (uart_handle_sysrq_char(port, c))
27648a6092fSMaxime Coquelin 			continue;
27748a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
27848a6092fSMaxime Coquelin 	}
27948a6092fSMaxime Coquelin 
28048a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
28148a6092fSMaxime Coquelin 	tty_flip_buffer_push(tport);
28248a6092fSMaxime Coquelin 	spin_lock(&port->lock);
28348a6092fSMaxime Coquelin }
28448a6092fSMaxime Coquelin 
28534891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg)
28634891872SAlexandre TORGUE {
28734891872SAlexandre TORGUE 	struct uart_port *port = arg;
28834891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
28934891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
29034891872SAlexandre TORGUE 
29134891872SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
29234891872SAlexandre TORGUE 	stm32port->tx_dma_busy = false;
29334891872SAlexandre TORGUE 
29434891872SAlexandre TORGUE 	/* Let's see if we have pending data to send */
29534891872SAlexandre TORGUE 	stm32_transmit_chars(port);
29634891872SAlexandre TORGUE }
29734891872SAlexandre TORGUE 
298d075719eSErwan Le Ray static void stm32_tx_interrupt_enable(struct uart_port *port)
299d075719eSErwan Le Ray {
300d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
301d075719eSErwan Le Ray 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
302d075719eSErwan Le Ray 
303d075719eSErwan Le Ray 	/*
304d075719eSErwan Le Ray 	 * Enables TX FIFO threashold irq when FIFO is enabled,
305d075719eSErwan Le Ray 	 * or TX empty irq when FIFO is disabled
306d075719eSErwan Le Ray 	 */
307d075719eSErwan Le Ray 	if (stm32_port->fifoen)
308d075719eSErwan Le Ray 		stm32_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
309d075719eSErwan Le Ray 	else
310d075719eSErwan Le Ray 		stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
311d075719eSErwan Le Ray }
312d075719eSErwan Le Ray 
313d075719eSErwan Le Ray static void stm32_tx_interrupt_disable(struct uart_port *port)
314d075719eSErwan Le Ray {
315d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
316d075719eSErwan Le Ray 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
317d075719eSErwan Le Ray 
318d075719eSErwan Le Ray 	if (stm32_port->fifoen)
319d075719eSErwan Le Ray 		stm32_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
320d075719eSErwan Le Ray 	else
321d075719eSErwan Le Ray 		stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
322d075719eSErwan Le Ray }
323d075719eSErwan Le Ray 
32434891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port)
32534891872SAlexandre TORGUE {
32634891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
32734891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
32834891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
32934891872SAlexandre TORGUE 
33034891872SAlexandre TORGUE 	if (stm32_port->tx_dma_busy) {
33134891872SAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
33234891872SAlexandre TORGUE 		stm32_port->tx_dma_busy = false;
33334891872SAlexandre TORGUE 	}
33434891872SAlexandre TORGUE 
3355d9176edSErwan Le Ray 	while (!uart_circ_empty(xmit)) {
3365d9176edSErwan Le Ray 		/* Check that TDR is empty before filling FIFO */
3375d9176edSErwan Le Ray 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
3385d9176edSErwan Le Ray 			break;
33934891872SAlexandre TORGUE 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
34034891872SAlexandre TORGUE 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
34134891872SAlexandre TORGUE 		port->icount.tx++;
34234891872SAlexandre TORGUE 	}
34334891872SAlexandre TORGUE 
3445d9176edSErwan Le Ray 	/* rely on TXE irq (mask or unmask) for sending remaining data */
3455d9176edSErwan Le Ray 	if (uart_circ_empty(xmit))
346d075719eSErwan Le Ray 		stm32_tx_interrupt_disable(port);
3475d9176edSErwan Le Ray 	else
348d075719eSErwan Le Ray 		stm32_tx_interrupt_enable(port);
3495d9176edSErwan Le Ray }
3505d9176edSErwan Le Ray 
35134891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port)
35234891872SAlexandre TORGUE {
35334891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
35434891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
35534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
35634891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
35734891872SAlexandre TORGUE 	dma_cookie_t cookie;
35834891872SAlexandre TORGUE 	unsigned int count, i;
35934891872SAlexandre TORGUE 
36034891872SAlexandre TORGUE 	if (stm32port->tx_dma_busy)
36134891872SAlexandre TORGUE 		return;
36234891872SAlexandre TORGUE 
36334891872SAlexandre TORGUE 	stm32port->tx_dma_busy = true;
36434891872SAlexandre TORGUE 
36534891872SAlexandre TORGUE 	count = uart_circ_chars_pending(xmit);
36634891872SAlexandre TORGUE 
36734891872SAlexandre TORGUE 	if (count > TX_BUF_L)
36834891872SAlexandre TORGUE 		count = TX_BUF_L;
36934891872SAlexandre TORGUE 
37034891872SAlexandre TORGUE 	if (xmit->tail < xmit->head) {
37134891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
37234891872SAlexandre TORGUE 	} else {
37334891872SAlexandre TORGUE 		size_t one = UART_XMIT_SIZE - xmit->tail;
37434891872SAlexandre TORGUE 		size_t two;
37534891872SAlexandre TORGUE 
37634891872SAlexandre TORGUE 		if (one > count)
37734891872SAlexandre TORGUE 			one = count;
37834891872SAlexandre TORGUE 		two = count - one;
37934891872SAlexandre TORGUE 
38034891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
38134891872SAlexandre TORGUE 		if (two)
38234891872SAlexandre TORGUE 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
38334891872SAlexandre TORGUE 	}
38434891872SAlexandre TORGUE 
38534891872SAlexandre TORGUE 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
38634891872SAlexandre TORGUE 					   stm32port->tx_dma_buf,
38734891872SAlexandre TORGUE 					   count,
38834891872SAlexandre TORGUE 					   DMA_MEM_TO_DEV,
38934891872SAlexandre TORGUE 					   DMA_PREP_INTERRUPT);
39034891872SAlexandre TORGUE 
39134891872SAlexandre TORGUE 	if (!desc) {
39234891872SAlexandre TORGUE 		for (i = count; i > 0; i--)
39334891872SAlexandre TORGUE 			stm32_transmit_chars_pio(port);
39434891872SAlexandre TORGUE 		return;
39534891872SAlexandre TORGUE 	}
39634891872SAlexandre TORGUE 
39734891872SAlexandre TORGUE 	desc->callback = stm32_tx_dma_complete;
39834891872SAlexandre TORGUE 	desc->callback_param = port;
39934891872SAlexandre TORGUE 
40034891872SAlexandre TORGUE 	/* Push current DMA TX transaction in the pending queue */
40134891872SAlexandre TORGUE 	cookie = dmaengine_submit(desc);
40234891872SAlexandre TORGUE 
40334891872SAlexandre TORGUE 	/* Issue pending DMA TX requests */
40434891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->tx_ch);
40534891872SAlexandre TORGUE 
40634891872SAlexandre TORGUE 	stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
40734891872SAlexandre TORGUE 
40834891872SAlexandre TORGUE 	xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
40934891872SAlexandre TORGUE 	port->icount.tx += count;
41034891872SAlexandre TORGUE }
41134891872SAlexandre TORGUE 
41248a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port)
41348a6092fSMaxime Coquelin {
414ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
415ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
41648a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
41748a6092fSMaxime Coquelin 
41848a6092fSMaxime Coquelin 	if (port->x_char) {
41934891872SAlexandre TORGUE 		if (stm32_port->tx_dma_busy)
42034891872SAlexandre TORGUE 			stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
421ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
42248a6092fSMaxime Coquelin 		port->x_char = 0;
42348a6092fSMaxime Coquelin 		port->icount.tx++;
42434891872SAlexandre TORGUE 		if (stm32_port->tx_dma_busy)
42534891872SAlexandre TORGUE 			stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
42648a6092fSMaxime Coquelin 		return;
42748a6092fSMaxime Coquelin 	}
42848a6092fSMaxime Coquelin 
429b83b957cSErwan Le Ray 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
430d075719eSErwan Le Ray 		stm32_tx_interrupt_disable(port);
43148a6092fSMaxime Coquelin 		return;
43248a6092fSMaxime Coquelin 	}
43348a6092fSMaxime Coquelin 
43464c32eabSErwan Le Ray 	if (ofs->icr == UNDEF_REG)
43564c32eabSErwan Le Ray 		stm32_clr_bits(port, ofs->isr, USART_SR_TC);
43664c32eabSErwan Le Ray 	else
4371250ed71SFabrice Gasnier 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
43864c32eabSErwan Le Ray 
43934891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
44034891872SAlexandre TORGUE 		stm32_transmit_chars_dma(port);
44134891872SAlexandre TORGUE 	else
44234891872SAlexandre TORGUE 		stm32_transmit_chars_pio(port);
44348a6092fSMaxime Coquelin 
44448a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
44548a6092fSMaxime Coquelin 		uart_write_wakeup(port);
44648a6092fSMaxime Coquelin 
44748a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
448d075719eSErwan Le Ray 		stm32_tx_interrupt_disable(port);
44948a6092fSMaxime Coquelin }
45048a6092fSMaxime Coquelin 
45148a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr)
45248a6092fSMaxime Coquelin {
45348a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
454ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
455ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
45648a6092fSMaxime Coquelin 	u32 sr;
45748a6092fSMaxime Coquelin 
45801d32d71SAlexandre TORGUE 	spin_lock(&port->lock);
45901d32d71SAlexandre TORGUE 
460ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
46148a6092fSMaxime Coquelin 
4624cc0ed62SErwan Le Ray 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
4634cc0ed62SErwan Le Ray 		writel_relaxed(USART_ICR_RTOCF,
4644cc0ed62SErwan Le Ray 			       port->membase + ofs->icr);
4654cc0ed62SErwan Le Ray 
466270e5a74SFabrice Gasnier 	if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
467270e5a74SFabrice Gasnier 		writel_relaxed(USART_ICR_WUCF,
468270e5a74SFabrice Gasnier 			       port->membase + ofs->icr);
469270e5a74SFabrice Gasnier 
47034891872SAlexandre TORGUE 	if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
47134891872SAlexandre TORGUE 		stm32_receive_chars(port, false);
47248a6092fSMaxime Coquelin 
47334891872SAlexandre TORGUE 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
47448a6092fSMaxime Coquelin 		stm32_transmit_chars(port);
47548a6092fSMaxime Coquelin 
47601d32d71SAlexandre TORGUE 	spin_unlock(&port->lock);
47701d32d71SAlexandre TORGUE 
47834891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
47934891872SAlexandre TORGUE 		return IRQ_WAKE_THREAD;
48034891872SAlexandre TORGUE 	else
48134891872SAlexandre TORGUE 		return IRQ_HANDLED;
48234891872SAlexandre TORGUE }
48334891872SAlexandre TORGUE 
48434891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
48534891872SAlexandre TORGUE {
48634891872SAlexandre TORGUE 	struct uart_port *port = ptr;
48734891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
48834891872SAlexandre TORGUE 
48934891872SAlexandre TORGUE 	spin_lock(&port->lock);
49034891872SAlexandre TORGUE 
49134891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
49234891872SAlexandre TORGUE 		stm32_receive_chars(port, true);
49334891872SAlexandre TORGUE 
49448a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
49548a6092fSMaxime Coquelin 
49648a6092fSMaxime Coquelin 	return IRQ_HANDLED;
49748a6092fSMaxime Coquelin }
49848a6092fSMaxime Coquelin 
49948a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port)
50048a6092fSMaxime Coquelin {
501ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
502ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
503ada8618fSAlexandre TORGUE 
504ada8618fSAlexandre TORGUE 	return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
50548a6092fSMaxime Coquelin }
50648a6092fSMaxime Coquelin 
50748a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
50848a6092fSMaxime Coquelin {
509ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
510ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
511ada8618fSAlexandre TORGUE 
51248a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
513ada8618fSAlexandre TORGUE 		stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
51448a6092fSMaxime Coquelin 	else
515ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
5166cf61b9bSManivannan Sadhasivam 
5176cf61b9bSManivannan Sadhasivam 	mctrl_gpio_set(stm32_port->gpios, mctrl);
51848a6092fSMaxime Coquelin }
51948a6092fSMaxime Coquelin 
52048a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port)
52148a6092fSMaxime Coquelin {
5226cf61b9bSManivannan Sadhasivam 	struct stm32_port *stm32_port = to_stm32_port(port);
5236cf61b9bSManivannan Sadhasivam 	unsigned int ret;
5246cf61b9bSManivannan Sadhasivam 
52548a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
5266cf61b9bSManivannan Sadhasivam 	ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
5276cf61b9bSManivannan Sadhasivam 
5286cf61b9bSManivannan Sadhasivam 	return mctrl_gpio_get(stm32_port->gpios, &ret);
5296cf61b9bSManivannan Sadhasivam }
5306cf61b9bSManivannan Sadhasivam 
5316cf61b9bSManivannan Sadhasivam static void stm32_enable_ms(struct uart_port *port)
5326cf61b9bSManivannan Sadhasivam {
5336cf61b9bSManivannan Sadhasivam 	mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
5346cf61b9bSManivannan Sadhasivam }
5356cf61b9bSManivannan Sadhasivam 
5366cf61b9bSManivannan Sadhasivam static void stm32_disable_ms(struct uart_port *port)
5376cf61b9bSManivannan Sadhasivam {
5386cf61b9bSManivannan Sadhasivam 	mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
53948a6092fSMaxime Coquelin }
54048a6092fSMaxime Coquelin 
54148a6092fSMaxime Coquelin /* Transmit stop */
54248a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port)
54348a6092fSMaxime Coquelin {
544d075719eSErwan Le Ray 	stm32_tx_interrupt_disable(port);
54548a6092fSMaxime Coquelin }
54648a6092fSMaxime Coquelin 
54748a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
54848a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port)
54948a6092fSMaxime Coquelin {
55048a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
55148a6092fSMaxime Coquelin 
55248a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
55348a6092fSMaxime Coquelin 		return;
55448a6092fSMaxime Coquelin 
55534891872SAlexandre TORGUE 	stm32_transmit_chars(port);
55648a6092fSMaxime Coquelin }
55748a6092fSMaxime Coquelin 
55848a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
55948a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port)
56048a6092fSMaxime Coquelin {
561ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
562ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
56348a6092fSMaxime Coquelin 	unsigned long flags;
56448a6092fSMaxime Coquelin 
56548a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
5664cc0ed62SErwan Le Ray 	stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
567d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
568d0a6a7bcSErwan Le Ray 		stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
569d0a6a7bcSErwan Le Ray 
57048a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
57148a6092fSMaxime Coquelin }
57248a6092fSMaxime Coquelin 
57348a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
57448a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port)
57548a6092fSMaxime Coquelin {
576ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
577ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
57848a6092fSMaxime Coquelin 	unsigned long flags;
57948a6092fSMaxime Coquelin 
58048a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
5814cc0ed62SErwan Le Ray 	stm32_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
582d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
583d0a6a7bcSErwan Le Ray 		stm32_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
584d0a6a7bcSErwan Le Ray 
58548a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
58648a6092fSMaxime Coquelin }
58748a6092fSMaxime Coquelin 
58848a6092fSMaxime Coquelin /* Receive stop */
58948a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port)
59048a6092fSMaxime Coquelin {
591ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
592ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
593ada8618fSAlexandre TORGUE 
5944cc0ed62SErwan Le Ray 	stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
595d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
596d0a6a7bcSErwan Le Ray 		stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
597d0a6a7bcSErwan Le Ray 
59848a6092fSMaxime Coquelin }
59948a6092fSMaxime Coquelin 
60048a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
60148a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state)
60248a6092fSMaxime Coquelin {
60348a6092fSMaxime Coquelin }
60448a6092fSMaxime Coquelin 
60548a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port)
60648a6092fSMaxime Coquelin {
607ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
608ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
60948a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
61048a6092fSMaxime Coquelin 	u32 val;
61148a6092fSMaxime Coquelin 	int ret;
61248a6092fSMaxime Coquelin 
61334891872SAlexandre TORGUE 	ret = request_threaded_irq(port->irq, stm32_interrupt,
61434891872SAlexandre TORGUE 				   stm32_threaded_interrupt,
61534891872SAlexandre TORGUE 				   IRQF_NO_SUSPEND, name, port);
61648a6092fSMaxime Coquelin 	if (ret)
61748a6092fSMaxime Coquelin 		return ret;
61848a6092fSMaxime Coquelin 
61984872dc4SErwan Le Ray 	/* RX FIFO Flush */
62084872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
62184872dc4SErwan Le Ray 		stm32_set_bits(port, ofs->rqr, USART_RQR_RXFRQ);
62248a6092fSMaxime Coquelin 
62384872dc4SErwan Le Ray 	/* Tx and RX FIFO configuration */
624d075719eSErwan Le Ray 	if (stm32_port->fifoen) {
625d075719eSErwan Le Ray 		val = readl_relaxed(port->membase + ofs->cr3);
626d0a6a7bcSErwan Le Ray 		val &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK);
627d075719eSErwan Le Ray 		val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT;
628d0a6a7bcSErwan Le Ray 		val |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT;
629d075719eSErwan Le Ray 		writel_relaxed(val, port->membase + ofs->cr3);
630d075719eSErwan Le Ray 	}
631d075719eSErwan Le Ray 
63284872dc4SErwan Le Ray 	/* RX FIFO enabling */
63384872dc4SErwan Le Ray 	val = stm32_port->cr1_irq | USART_CR1_RE;
63484872dc4SErwan Le Ray 	if (stm32_port->fifoen)
63584872dc4SErwan Le Ray 		val |= USART_CR1_FIFOEN;
63684872dc4SErwan Le Ray 	stm32_set_bits(port, ofs->cr1, val);
63784872dc4SErwan Le Ray 
63848a6092fSMaxime Coquelin 	return 0;
63948a6092fSMaxime Coquelin }
64048a6092fSMaxime Coquelin 
64148a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port)
64248a6092fSMaxime Coquelin {
643ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
644ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
64587f1f809SAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
64664c32eabSErwan Le Ray 	u32 val, isr;
64764c32eabSErwan Le Ray 	int ret;
64848a6092fSMaxime Coquelin 
6496cf61b9bSManivannan Sadhasivam 	/* Disable modem control interrupts */
6506cf61b9bSManivannan Sadhasivam 	stm32_disable_ms(port);
6516cf61b9bSManivannan Sadhasivam 
6524cc0ed62SErwan Le Ray 	val = USART_CR1_TXEIE | USART_CR1_TE;
6534cc0ed62SErwan Le Ray 	val |= stm32_port->cr1_irq | USART_CR1_RE;
65487f1f809SAlexandre TORGUE 	val |= BIT(cfg->uart_enable_bit);
655351a762aSGerald Baeza 	if (stm32_port->fifoen)
656351a762aSGerald Baeza 		val |= USART_CR1_FIFOEN;
65764c32eabSErwan Le Ray 
65864c32eabSErwan Le Ray 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
65964c32eabSErwan Le Ray 					 isr, (isr & USART_SR_TC),
66064c32eabSErwan Le Ray 					 10, 100000);
66164c32eabSErwan Le Ray 
66264c32eabSErwan Le Ray 	if (ret)
66364c32eabSErwan Le Ray 		dev_err(port->dev, "transmission complete not set\n");
66464c32eabSErwan Le Ray 
665a14f66a4SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr1, val);
66648a6092fSMaxime Coquelin 
66748a6092fSMaxime Coquelin 	free_irq(port->irq, port);
66848a6092fSMaxime Coquelin }
66948a6092fSMaxime Coquelin 
670929ffa4aSYueHaibing static unsigned int stm32_get_databits(struct ktermios *termios)
671c8a9d043SErwan Le Ray {
672c8a9d043SErwan Le Ray 	unsigned int bits;
673c8a9d043SErwan Le Ray 
674c8a9d043SErwan Le Ray 	tcflag_t cflag = termios->c_cflag;
675c8a9d043SErwan Le Ray 
676c8a9d043SErwan Le Ray 	switch (cflag & CSIZE) {
677c8a9d043SErwan Le Ray 	/*
678c8a9d043SErwan Le Ray 	 * CSIZE settings are not necessarily supported in hardware.
679c8a9d043SErwan Le Ray 	 * CSIZE unsupported configurations are handled here to set word length
680c8a9d043SErwan Le Ray 	 * to 8 bits word as default configuration and to print debug message.
681c8a9d043SErwan Le Ray 	 */
682c8a9d043SErwan Le Ray 	case CS5:
683c8a9d043SErwan Le Ray 		bits = 5;
684c8a9d043SErwan Le Ray 		break;
685c8a9d043SErwan Le Ray 	case CS6:
686c8a9d043SErwan Le Ray 		bits = 6;
687c8a9d043SErwan Le Ray 		break;
688c8a9d043SErwan Le Ray 	case CS7:
689c8a9d043SErwan Le Ray 		bits = 7;
690c8a9d043SErwan Le Ray 		break;
691c8a9d043SErwan Le Ray 	/* default including CS8 */
692c8a9d043SErwan Le Ray 	default:
693c8a9d043SErwan Le Ray 		bits = 8;
694c8a9d043SErwan Le Ray 		break;
695c8a9d043SErwan Le Ray 	}
696c8a9d043SErwan Le Ray 
697c8a9d043SErwan Le Ray 	return bits;
698c8a9d043SErwan Le Ray }
699c8a9d043SErwan Le Ray 
70048a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
70148a6092fSMaxime Coquelin 			    struct ktermios *old)
70248a6092fSMaxime Coquelin {
70348a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
704ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
705ada8618fSAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
7061bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
707c8a9d043SErwan Le Ray 	unsigned int baud, bits;
70848a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
70948a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
71048a6092fSMaxime Coquelin 	u32 cr1, cr2, cr3;
71148a6092fSMaxime Coquelin 	unsigned long flags;
71248a6092fSMaxime Coquelin 
71348a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
71448a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
71548a6092fSMaxime Coquelin 
71648a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
71748a6092fSMaxime Coquelin 
71848a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
71948a6092fSMaxime Coquelin 
72048a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
721ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
72248a6092fSMaxime Coquelin 
72384872dc4SErwan Le Ray 	/* flush RX & TX FIFO */
72484872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
72584872dc4SErwan Le Ray 		stm32_set_bits(port, ofs->rqr,
72684872dc4SErwan Le Ray 			       USART_RQR_TXFRQ | USART_RQR_RXFRQ);
7271bcda09dSBich HEMON 
72884872dc4SErwan Le Ray 	cr1 = USART_CR1_TE | USART_CR1_RE;
729351a762aSGerald Baeza 	if (stm32_port->fifoen)
730351a762aSGerald Baeza 		cr1 |= USART_CR1_FIFOEN;
73148a6092fSMaxime Coquelin 	cr2 = 0;
732d075719eSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
733d0a6a7bcSErwan Le Ray 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG_MASK | USART_CR3_RXFTIE
734d075719eSErwan Le Ray 		| USART_CR3_TXFTCFG_MASK;
73548a6092fSMaxime Coquelin 
73648a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
73748a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
73848a6092fSMaxime Coquelin 
739c8a9d043SErwan Le Ray 	bits = stm32_get_databits(termios);
7406c5962f3SErwan Le Ray 	stm32_port->rdr_mask = (BIT(bits) - 1);
741c8a9d043SErwan Le Ray 
74248a6092fSMaxime Coquelin 	if (cflag & PARENB) {
743c8a9d043SErwan Le Ray 		bits++;
74448a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
745c8a9d043SErwan Le Ray 	}
746c8a9d043SErwan Le Ray 
747c8a9d043SErwan Le Ray 	/*
748c8a9d043SErwan Le Ray 	 * Word length configuration:
749c8a9d043SErwan Le Ray 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
750c8a9d043SErwan Le Ray 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
751c8a9d043SErwan Le Ray 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
752c8a9d043SErwan Le Ray 	 * M0 and M1 already cleared by cr1 initialization.
753c8a9d043SErwan Le Ray 	 */
754c8a9d043SErwan Le Ray 	if (bits == 9)
755ada8618fSAlexandre TORGUE 		cr1 |= USART_CR1_M0;
756c8a9d043SErwan Le Ray 	else if ((bits == 7) && cfg->has_7bits_data)
757c8a9d043SErwan Le Ray 		cr1 |= USART_CR1_M1;
758c8a9d043SErwan Le Ray 	else if (bits != 8)
759c8a9d043SErwan Le Ray 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
760c8a9d043SErwan Le Ray 			, bits);
76148a6092fSMaxime Coquelin 
7624cc0ed62SErwan Le Ray 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
7634cc0ed62SErwan Le Ray 				       stm32_port->fifoen)) {
7644cc0ed62SErwan Le Ray 		if (cflag & CSTOPB)
7654cc0ed62SErwan Le Ray 			bits = bits + 3; /* 1 start bit + 2 stop bits */
7664cc0ed62SErwan Le Ray 		else
7674cc0ed62SErwan Le Ray 			bits = bits + 2; /* 1 start bit + 1 stop bit */
7684cc0ed62SErwan Le Ray 
7694cc0ed62SErwan Le Ray 		/* RX timeout irq to occur after last stop bit + bits */
7704cc0ed62SErwan Le Ray 		stm32_port->cr1_irq = USART_CR1_RTOIE;
7714cc0ed62SErwan Le Ray 		writel_relaxed(bits, port->membase + ofs->rtor);
7724cc0ed62SErwan Le Ray 		cr2 |= USART_CR2_RTOEN;
773d0a6a7bcSErwan Le Ray 		/* Not using dma, enable fifo threshold irq */
774d0a6a7bcSErwan Le Ray 		if (!stm32_port->rx_ch)
775d0a6a7bcSErwan Le Ray 			stm32_port->cr3_irq =  USART_CR3_RXFTIE;
7764cc0ed62SErwan Le Ray 	}
7774cc0ed62SErwan Le Ray 
778d0a6a7bcSErwan Le Ray 	cr1 |= stm32_port->cr1_irq;
779d0a6a7bcSErwan Le Ray 	cr3 |= stm32_port->cr3_irq;
780d0a6a7bcSErwan Le Ray 
78148a6092fSMaxime Coquelin 	if (cflag & PARODD)
78248a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
78348a6092fSMaxime Coquelin 
78448a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
78548a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
78648a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
78735abe98fSBich HEMON 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
78848a6092fSMaxime Coquelin 	}
78948a6092fSMaxime Coquelin 
7906cf61b9bSManivannan Sadhasivam 	/* Handle modem control interrupts */
7916cf61b9bSManivannan Sadhasivam 	if (UART_ENABLE_MS(port, termios->c_cflag))
7926cf61b9bSManivannan Sadhasivam 		stm32_enable_ms(port);
7936cf61b9bSManivannan Sadhasivam 	else
7946cf61b9bSManivannan Sadhasivam 		stm32_disable_ms(port);
7956cf61b9bSManivannan Sadhasivam 
79648a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
79748a6092fSMaxime Coquelin 
79848a6092fSMaxime Coquelin 	/*
79948a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
80048a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
80148a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
80248a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
80348a6092fSMaxime Coquelin 	 */
80448a6092fSMaxime Coquelin 	if (usartdiv < 16) {
80548a6092fSMaxime Coquelin 		oversampling = 8;
8061bcda09dSBich HEMON 		cr1 |= USART_CR1_OVER8;
807ada8618fSAlexandre TORGUE 		stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
80848a6092fSMaxime Coquelin 	} else {
80948a6092fSMaxime Coquelin 		oversampling = 16;
8101bcda09dSBich HEMON 		cr1 &= ~USART_CR1_OVER8;
811ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
81248a6092fSMaxime Coquelin 	}
81348a6092fSMaxime Coquelin 
81448a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
81548a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
816ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
81748a6092fSMaxime Coquelin 
81848a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
81948a6092fSMaxime Coquelin 
82048a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
82148a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
82248a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
82348a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
8244f01d833SErwan Le Ray 		port->read_status_mask |= USART_SR_FE;
82548a6092fSMaxime Coquelin 
82648a6092fSMaxime Coquelin 	/* Characters to ignore */
82748a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
82848a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
82948a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
83048a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
8314f01d833SErwan Le Ray 		port->ignore_status_mask |= USART_SR_FE;
83248a6092fSMaxime Coquelin 		/*
83348a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
83448a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
83548a6092fSMaxime Coquelin 		 */
83648a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
83748a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
83848a6092fSMaxime Coquelin 	}
83948a6092fSMaxime Coquelin 
84048a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
84148a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
84248a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
84348a6092fSMaxime Coquelin 
84434891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
84534891872SAlexandre TORGUE 		cr3 |= USART_CR3_DMAR;
84634891872SAlexandre TORGUE 
8471bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
8481bcda09dSBich HEMON 		stm32_config_reg_rs485(&cr1, &cr3,
8491bcda09dSBich HEMON 				       rs485conf->delay_rts_before_send,
8501bcda09dSBich HEMON 				       rs485conf->delay_rts_after_send, baud);
8511bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
8521bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
8531bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
8547df5081cSMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
8557df5081cSMarek Vasut 					stm32_port->port.mctrl & ~TIOCM_RTS);
8561bcda09dSBich HEMON 		} else {
8571bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
8581bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
8597df5081cSMarek Vasut 			mctrl_gpio_set(stm32_port->gpios,
8607df5081cSMarek Vasut 					stm32_port->port.mctrl | TIOCM_RTS);
8611bcda09dSBich HEMON 		}
8621bcda09dSBich HEMON 
8631bcda09dSBich HEMON 	} else {
8641bcda09dSBich HEMON 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
8651bcda09dSBich HEMON 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
8661bcda09dSBich HEMON 	}
8671bcda09dSBich HEMON 
868ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
869ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
870ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
87148a6092fSMaxime Coquelin 
8721bcda09dSBich HEMON 	stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
87348a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
87448a6092fSMaxime Coquelin }
87548a6092fSMaxime Coquelin 
87648a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port)
87748a6092fSMaxime Coquelin {
87848a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
87948a6092fSMaxime Coquelin }
88048a6092fSMaxime Coquelin 
88148a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port)
88248a6092fSMaxime Coquelin {
88348a6092fSMaxime Coquelin }
88448a6092fSMaxime Coquelin 
88548a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port)
88648a6092fSMaxime Coquelin {
88748a6092fSMaxime Coquelin 	return 0;
88848a6092fSMaxime Coquelin }
88948a6092fSMaxime Coquelin 
89048a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags)
89148a6092fSMaxime Coquelin {
89248a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
89348a6092fSMaxime Coquelin 		port->type = PORT_STM32;
89448a6092fSMaxime Coquelin }
89548a6092fSMaxime Coquelin 
89648a6092fSMaxime Coquelin static int
89748a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
89848a6092fSMaxime Coquelin {
89948a6092fSMaxime Coquelin 	/* No user changeable parameters */
90048a6092fSMaxime Coquelin 	return -EINVAL;
90148a6092fSMaxime Coquelin }
90248a6092fSMaxime Coquelin 
90348a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state,
90448a6092fSMaxime Coquelin 		unsigned int oldstate)
90548a6092fSMaxime Coquelin {
90648a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
90748a6092fSMaxime Coquelin 			struct stm32_port, port);
908ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
909ada8618fSAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32port->info->cfg;
91048a6092fSMaxime Coquelin 	unsigned long flags = 0;
91148a6092fSMaxime Coquelin 
91248a6092fSMaxime Coquelin 	switch (state) {
91348a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
914fb6dcef6SErwan Le Ray 		pm_runtime_get_sync(port->dev);
91548a6092fSMaxime Coquelin 		break;
91648a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
91748a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
918ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
91948a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
920fb6dcef6SErwan Le Ray 		pm_runtime_put_sync(port->dev);
92148a6092fSMaxime Coquelin 		break;
92248a6092fSMaxime Coquelin 	}
92348a6092fSMaxime Coquelin }
92448a6092fSMaxime Coquelin 
92548a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
92648a6092fSMaxime Coquelin 	.tx_empty	= stm32_tx_empty,
92748a6092fSMaxime Coquelin 	.set_mctrl	= stm32_set_mctrl,
92848a6092fSMaxime Coquelin 	.get_mctrl	= stm32_get_mctrl,
92948a6092fSMaxime Coquelin 	.stop_tx	= stm32_stop_tx,
93048a6092fSMaxime Coquelin 	.start_tx	= stm32_start_tx,
93148a6092fSMaxime Coquelin 	.throttle	= stm32_throttle,
93248a6092fSMaxime Coquelin 	.unthrottle	= stm32_unthrottle,
93348a6092fSMaxime Coquelin 	.stop_rx	= stm32_stop_rx,
9346cf61b9bSManivannan Sadhasivam 	.enable_ms	= stm32_enable_ms,
93548a6092fSMaxime Coquelin 	.break_ctl	= stm32_break_ctl,
93648a6092fSMaxime Coquelin 	.startup	= stm32_startup,
93748a6092fSMaxime Coquelin 	.shutdown	= stm32_shutdown,
93848a6092fSMaxime Coquelin 	.set_termios	= stm32_set_termios,
93948a6092fSMaxime Coquelin 	.pm		= stm32_pm,
94048a6092fSMaxime Coquelin 	.type		= stm32_type,
94148a6092fSMaxime Coquelin 	.release_port	= stm32_release_port,
94248a6092fSMaxime Coquelin 	.request_port	= stm32_request_port,
94348a6092fSMaxime Coquelin 	.config_port	= stm32_config_port,
94448a6092fSMaxime Coquelin 	.verify_port	= stm32_verify_port,
94548a6092fSMaxime Coquelin };
94648a6092fSMaxime Coquelin 
94748a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port,
94848a6092fSMaxime Coquelin 			  struct platform_device *pdev)
94948a6092fSMaxime Coquelin {
95048a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
95148a6092fSMaxime Coquelin 	struct resource *res;
95248a6092fSMaxime Coquelin 	int ret;
95348a6092fSMaxime Coquelin 
95448a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
95548a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
95648a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
95748a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
958d075719eSErwan Le Ray 	port->fifosize	= stm32port->info->cfg.fifosize;
9599feedaa7SDmitry Safonov 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
9602c58e560SErwan Le Ray 
9612c58e560SErwan Le Ray 	ret = platform_get_irq(pdev, 0);
9621df21786SStephen Boyd 	if (ret <= 0)
9631df21786SStephen Boyd 		return ret ? : -ENODEV;
9642c58e560SErwan Le Ray 	port->irq = ret;
9652c58e560SErwan Le Ray 
9667d8f6861SBich HEMON 	port->rs485_config = stm32_config_rs485;
9677d8f6861SBich HEMON 
968c150c0f3SLukas Wunner 	ret = stm32_init_rs485(port, pdev);
969c150c0f3SLukas Wunner 	if (ret)
970c150c0f3SLukas Wunner 		return ret;
9717d8f6861SBich HEMON 
9722c58e560SErwan Le Ray 	if (stm32port->info->cfg.has_wakeup) {
973*fdf16d78SHolger Assmann 		stm32port->wakeirq = platform_get_irq_optional(pdev, 1);
9741df21786SStephen Boyd 		if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO)
9751df21786SStephen Boyd 			return stm32port->wakeirq ? : -ENODEV;
9762c58e560SErwan Le Ray 	}
9772c58e560SErwan Le Ray 
978351a762aSGerald Baeza 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
97948a6092fSMaxime Coquelin 
98048a6092fSMaxime Coquelin 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
98148a6092fSMaxime Coquelin 	port->membase = devm_ioremap_resource(&pdev->dev, res);
98248a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
98348a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
98448a6092fSMaxime Coquelin 	port->mapbase = res->start;
98548a6092fSMaxime Coquelin 
98648a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
98748a6092fSMaxime Coquelin 
98848a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
98948a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
99048a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
99148a6092fSMaxime Coquelin 
99248a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
99348a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
99448a6092fSMaxime Coquelin 	if (ret)
99548a6092fSMaxime Coquelin 		return ret;
99648a6092fSMaxime Coquelin 
99748a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
998ada80043SFabrice Gasnier 	if (!stm32port->port.uartclk) {
99948a6092fSMaxime Coquelin 		ret = -EINVAL;
10006cf61b9bSManivannan Sadhasivam 		goto err_clk;
1001ada80043SFabrice Gasnier 	}
100248a6092fSMaxime Coquelin 
10036cf61b9bSManivannan Sadhasivam 	stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
10046cf61b9bSManivannan Sadhasivam 	if (IS_ERR(stm32port->gpios)) {
10056cf61b9bSManivannan Sadhasivam 		ret = PTR_ERR(stm32port->gpios);
10066cf61b9bSManivannan Sadhasivam 		goto err_clk;
10076cf61b9bSManivannan Sadhasivam 	}
10086cf61b9bSManivannan Sadhasivam 
10096cf61b9bSManivannan Sadhasivam 	/* Both CTS/RTS gpios and "st,hw-flow-ctrl" should not be specified */
10106cf61b9bSManivannan Sadhasivam 	if (stm32port->hw_flow_control) {
10116cf61b9bSManivannan Sadhasivam 		if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
10126cf61b9bSManivannan Sadhasivam 		    mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
10136cf61b9bSManivannan Sadhasivam 			dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
10146cf61b9bSManivannan Sadhasivam 			ret = -EINVAL;
10156cf61b9bSManivannan Sadhasivam 			goto err_clk;
10166cf61b9bSManivannan Sadhasivam 		}
10176cf61b9bSManivannan Sadhasivam 	}
10186cf61b9bSManivannan Sadhasivam 
10196cf61b9bSManivannan Sadhasivam 	return ret;
10206cf61b9bSManivannan Sadhasivam 
10216cf61b9bSManivannan Sadhasivam err_clk:
10226cf61b9bSManivannan Sadhasivam 	clk_disable_unprepare(stm32port->clk);
10236cf61b9bSManivannan Sadhasivam 
102448a6092fSMaxime Coquelin 	return ret;
102548a6092fSMaxime Coquelin }
102648a6092fSMaxime Coquelin 
102748a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
102848a6092fSMaxime Coquelin {
102948a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
103048a6092fSMaxime Coquelin 	int id;
103148a6092fSMaxime Coquelin 
103248a6092fSMaxime Coquelin 	if (!np)
103348a6092fSMaxime Coquelin 		return NULL;
103448a6092fSMaxime Coquelin 
103548a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
1036e5707915SGerald Baeza 	if (id < 0) {
1037e5707915SGerald Baeza 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1038e5707915SGerald Baeza 		return NULL;
1039e5707915SGerald Baeza 	}
104048a6092fSMaxime Coquelin 
104148a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
104248a6092fSMaxime Coquelin 		return NULL;
104348a6092fSMaxime Coquelin 
10446fd9fffbSErwan Le Ray 	stm32_ports[id].hw_flow_control =
10456fd9fffbSErwan Le Ray 		of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
10466fd9fffbSErwan Le Ray 		of_property_read_bool (np, "uart-has-rtscts");
104748a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
10484cc0ed62SErwan Le Ray 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1049d0a6a7bcSErwan Le Ray 	stm32_ports[id].cr3_irq = 0;
1050e5707915SGerald Baeza 	stm32_ports[id].last_res = RX_BUF_L;
105148a6092fSMaxime Coquelin 	return &stm32_ports[id];
105248a6092fSMaxime Coquelin }
105348a6092fSMaxime Coquelin 
105448a6092fSMaxime Coquelin #ifdef CONFIG_OF
105548a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
1056ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
1057ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1058270e5a74SFabrice Gasnier 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
105948a6092fSMaxime Coquelin 	{},
106048a6092fSMaxime Coquelin };
106148a6092fSMaxime Coquelin 
106248a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
106348a6092fSMaxime Coquelin #endif
106448a6092fSMaxime Coquelin 
106534891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
106634891872SAlexandre TORGUE 				 struct platform_device *pdev)
106734891872SAlexandre TORGUE {
106834891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
106934891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
107034891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
107134891872SAlexandre TORGUE 	struct dma_slave_config config;
107234891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
107334891872SAlexandre TORGUE 	dma_cookie_t cookie;
107434891872SAlexandre TORGUE 	int ret;
107534891872SAlexandre TORGUE 
107634891872SAlexandre TORGUE 	/* Request DMA RX channel */
107734891872SAlexandre TORGUE 	stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
107834891872SAlexandre TORGUE 	if (!stm32port->rx_ch) {
107934891872SAlexandre TORGUE 		dev_info(dev, "rx dma alloc failed\n");
108034891872SAlexandre TORGUE 		return -ENODEV;
108134891872SAlexandre TORGUE 	}
108234891872SAlexandre TORGUE 	stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
108334891872SAlexandre TORGUE 						 &stm32port->rx_dma_buf,
108434891872SAlexandre TORGUE 						 GFP_KERNEL);
108534891872SAlexandre TORGUE 	if (!stm32port->rx_buf) {
108634891872SAlexandre TORGUE 		ret = -ENOMEM;
108734891872SAlexandre TORGUE 		goto alloc_err;
108834891872SAlexandre TORGUE 	}
108934891872SAlexandre TORGUE 
109034891872SAlexandre TORGUE 	/* Configure DMA channel */
109134891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
10928e5481d9SArnd Bergmann 	config.src_addr = port->mapbase + ofs->rdr;
109334891872SAlexandre TORGUE 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
109434891872SAlexandre TORGUE 
109534891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
109634891872SAlexandre TORGUE 	if (ret < 0) {
109734891872SAlexandre TORGUE 		dev_err(dev, "rx dma channel config failed\n");
109834891872SAlexandre TORGUE 		ret = -ENODEV;
109934891872SAlexandre TORGUE 		goto config_err;
110034891872SAlexandre TORGUE 	}
110134891872SAlexandre TORGUE 
110234891872SAlexandre TORGUE 	/* Prepare a DMA cyclic transaction */
110334891872SAlexandre TORGUE 	desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
110434891872SAlexandre TORGUE 					 stm32port->rx_dma_buf,
110534891872SAlexandre TORGUE 					 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
110634891872SAlexandre TORGUE 					 DMA_PREP_INTERRUPT);
110734891872SAlexandre TORGUE 	if (!desc) {
110834891872SAlexandre TORGUE 		dev_err(dev, "rx dma prep cyclic failed\n");
110934891872SAlexandre TORGUE 		ret = -ENODEV;
111034891872SAlexandre TORGUE 		goto config_err;
111134891872SAlexandre TORGUE 	}
111234891872SAlexandre TORGUE 
111334891872SAlexandre TORGUE 	/* No callback as dma buffer is drained on usart interrupt */
111434891872SAlexandre TORGUE 	desc->callback = NULL;
111534891872SAlexandre TORGUE 	desc->callback_param = NULL;
111634891872SAlexandre TORGUE 
111734891872SAlexandre TORGUE 	/* Push current DMA transaction in the pending queue */
111834891872SAlexandre TORGUE 	cookie = dmaengine_submit(desc);
111934891872SAlexandre TORGUE 
112034891872SAlexandre TORGUE 	/* Issue pending DMA requests */
112134891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->rx_ch);
112234891872SAlexandre TORGUE 
112334891872SAlexandre TORGUE 	return 0;
112434891872SAlexandre TORGUE 
112534891872SAlexandre TORGUE config_err:
112634891872SAlexandre TORGUE 	dma_free_coherent(&pdev->dev,
112734891872SAlexandre TORGUE 			  RX_BUF_L, stm32port->rx_buf,
112834891872SAlexandre TORGUE 			  stm32port->rx_dma_buf);
112934891872SAlexandre TORGUE 
113034891872SAlexandre TORGUE alloc_err:
113134891872SAlexandre TORGUE 	dma_release_channel(stm32port->rx_ch);
113234891872SAlexandre TORGUE 	stm32port->rx_ch = NULL;
113334891872SAlexandre TORGUE 
113434891872SAlexandre TORGUE 	return ret;
113534891872SAlexandre TORGUE }
113634891872SAlexandre TORGUE 
113734891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
113834891872SAlexandre TORGUE 				 struct platform_device *pdev)
113934891872SAlexandre TORGUE {
114034891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
114134891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
114234891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
114334891872SAlexandre TORGUE 	struct dma_slave_config config;
114434891872SAlexandre TORGUE 	int ret;
114534891872SAlexandre TORGUE 
114634891872SAlexandre TORGUE 	stm32port->tx_dma_busy = false;
114734891872SAlexandre TORGUE 
114834891872SAlexandre TORGUE 	/* Request DMA TX channel */
114934891872SAlexandre TORGUE 	stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
115034891872SAlexandre TORGUE 	if (!stm32port->tx_ch) {
115134891872SAlexandre TORGUE 		dev_info(dev, "tx dma alloc failed\n");
115234891872SAlexandre TORGUE 		return -ENODEV;
115334891872SAlexandre TORGUE 	}
115434891872SAlexandre TORGUE 	stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
115534891872SAlexandre TORGUE 						 &stm32port->tx_dma_buf,
115634891872SAlexandre TORGUE 						 GFP_KERNEL);
115734891872SAlexandre TORGUE 	if (!stm32port->tx_buf) {
115834891872SAlexandre TORGUE 		ret = -ENOMEM;
115934891872SAlexandre TORGUE 		goto alloc_err;
116034891872SAlexandre TORGUE 	}
116134891872SAlexandre TORGUE 
116234891872SAlexandre TORGUE 	/* Configure DMA channel */
116334891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
11648e5481d9SArnd Bergmann 	config.dst_addr = port->mapbase + ofs->tdr;
116534891872SAlexandre TORGUE 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
116634891872SAlexandre TORGUE 
116734891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
116834891872SAlexandre TORGUE 	if (ret < 0) {
116934891872SAlexandre TORGUE 		dev_err(dev, "tx dma channel config failed\n");
117034891872SAlexandre TORGUE 		ret = -ENODEV;
117134891872SAlexandre TORGUE 		goto config_err;
117234891872SAlexandre TORGUE 	}
117334891872SAlexandre TORGUE 
117434891872SAlexandre TORGUE 	return 0;
117534891872SAlexandre TORGUE 
117634891872SAlexandre TORGUE config_err:
117734891872SAlexandre TORGUE 	dma_free_coherent(&pdev->dev,
117834891872SAlexandre TORGUE 			  TX_BUF_L, stm32port->tx_buf,
117934891872SAlexandre TORGUE 			  stm32port->tx_dma_buf);
118034891872SAlexandre TORGUE 
118134891872SAlexandre TORGUE alloc_err:
118234891872SAlexandre TORGUE 	dma_release_channel(stm32port->tx_ch);
118334891872SAlexandre TORGUE 	stm32port->tx_ch = NULL;
118434891872SAlexandre TORGUE 
118534891872SAlexandre TORGUE 	return ret;
118634891872SAlexandre TORGUE }
118734891872SAlexandre TORGUE 
118848a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev)
118948a6092fSMaxime Coquelin {
1190ada8618fSAlexandre TORGUE 	const struct of_device_id *match;
119148a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
1192ada8618fSAlexandre TORGUE 	int ret;
119348a6092fSMaxime Coquelin 
119448a6092fSMaxime Coquelin 	stm32port = stm32_of_get_stm32_port(pdev);
119548a6092fSMaxime Coquelin 	if (!stm32port)
119648a6092fSMaxime Coquelin 		return -ENODEV;
119748a6092fSMaxime Coquelin 
1198ada8618fSAlexandre TORGUE 	match = of_match_device(stm32_match, &pdev->dev);
1199ada8618fSAlexandre TORGUE 	if (match && match->data)
1200ada8618fSAlexandre TORGUE 		stm32port->info = (struct stm32_usart_info *)match->data;
1201ada8618fSAlexandre TORGUE 	else
1202ada8618fSAlexandre TORGUE 		return -EINVAL;
1203ada8618fSAlexandre TORGUE 
120448a6092fSMaxime Coquelin 	ret = stm32_init_port(stm32port, pdev);
120548a6092fSMaxime Coquelin 	if (ret)
120648a6092fSMaxime Coquelin 		return ret;
120748a6092fSMaxime Coquelin 
12082c58e560SErwan Le Ray 	if (stm32port->wakeirq > 0) {
1209270e5a74SFabrice Gasnier 		ret = device_init_wakeup(&pdev->dev, true);
121048a6092fSMaxime Coquelin 		if (ret)
1211ada80043SFabrice Gasnier 			goto err_uninit;
12125297f274SErwan Le Ray 
12135297f274SErwan Le Ray 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
12145297f274SErwan Le Ray 						    stm32port->wakeirq);
12155297f274SErwan Le Ray 		if (ret)
12165297f274SErwan Le Ray 			goto err_nowup;
12175297f274SErwan Le Ray 
12185297f274SErwan Le Ray 		device_set_wakeup_enable(&pdev->dev, false);
1219270e5a74SFabrice Gasnier 	}
1220270e5a74SFabrice Gasnier 
1221270e5a74SFabrice Gasnier 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1222270e5a74SFabrice Gasnier 	if (ret)
12235297f274SErwan Le Ray 		goto err_wirq;
122448a6092fSMaxime Coquelin 
122534891872SAlexandre TORGUE 	ret = stm32_of_dma_rx_probe(stm32port, pdev);
122634891872SAlexandre TORGUE 	if (ret)
122734891872SAlexandre TORGUE 		dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
122834891872SAlexandre TORGUE 
122934891872SAlexandre TORGUE 	ret = stm32_of_dma_tx_probe(stm32port, pdev);
123034891872SAlexandre TORGUE 	if (ret)
123134891872SAlexandre TORGUE 		dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
123234891872SAlexandre TORGUE 
123348a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
123448a6092fSMaxime Coquelin 
1235fb6dcef6SErwan Le Ray 	pm_runtime_get_noresume(&pdev->dev);
1236fb6dcef6SErwan Le Ray 	pm_runtime_set_active(&pdev->dev);
1237fb6dcef6SErwan Le Ray 	pm_runtime_enable(&pdev->dev);
1238fb6dcef6SErwan Le Ray 	pm_runtime_put_sync(&pdev->dev);
1239fb6dcef6SErwan Le Ray 
124048a6092fSMaxime Coquelin 	return 0;
1241ada80043SFabrice Gasnier 
12425297f274SErwan Le Ray err_wirq:
12432c58e560SErwan Le Ray 	if (stm32port->wakeirq > 0)
12445297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
12455297f274SErwan Le Ray 
1246270e5a74SFabrice Gasnier err_nowup:
12472c58e560SErwan Le Ray 	if (stm32port->wakeirq > 0)
1248270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
1249270e5a74SFabrice Gasnier 
1250ada80043SFabrice Gasnier err_uninit:
1251ada80043SFabrice Gasnier 	clk_disable_unprepare(stm32port->clk);
1252ada80043SFabrice Gasnier 
1253ada80043SFabrice Gasnier 	return ret;
125448a6092fSMaxime Coquelin }
125548a6092fSMaxime Coquelin 
125648a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev)
125748a6092fSMaxime Coquelin {
125848a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
1259511c7b1bSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
126034891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1261fb6dcef6SErwan Le Ray 	int err;
1262fb6dcef6SErwan Le Ray 
1263fb6dcef6SErwan Le Ray 	pm_runtime_get_sync(&pdev->dev);
126434891872SAlexandre TORGUE 
126534891872SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
126634891872SAlexandre TORGUE 
126734891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
126834891872SAlexandre TORGUE 		dma_release_channel(stm32_port->rx_ch);
126934891872SAlexandre TORGUE 
127034891872SAlexandre TORGUE 	if (stm32_port->rx_dma_buf)
127134891872SAlexandre TORGUE 		dma_free_coherent(&pdev->dev,
127234891872SAlexandre TORGUE 				  RX_BUF_L, stm32_port->rx_buf,
127334891872SAlexandre TORGUE 				  stm32_port->rx_dma_buf);
127434891872SAlexandre TORGUE 
127534891872SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
127634891872SAlexandre TORGUE 
127734891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
127834891872SAlexandre TORGUE 		dma_release_channel(stm32_port->tx_ch);
127934891872SAlexandre TORGUE 
128034891872SAlexandre TORGUE 	if (stm32_port->tx_dma_buf)
128134891872SAlexandre TORGUE 		dma_free_coherent(&pdev->dev,
128234891872SAlexandre TORGUE 				  TX_BUF_L, stm32_port->tx_buf,
128334891872SAlexandre TORGUE 				  stm32_port->tx_dma_buf);
1284511c7b1bSAlexandre TORGUE 
12852c58e560SErwan Le Ray 	if (stm32_port->wakeirq > 0) {
12865297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
1287270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
12885297f274SErwan Le Ray 	}
1289270e5a74SFabrice Gasnier 
1290511c7b1bSAlexandre TORGUE 	clk_disable_unprepare(stm32_port->clk);
129148a6092fSMaxime Coquelin 
1292fb6dcef6SErwan Le Ray 	err = uart_remove_one_port(&stm32_usart_driver, port);
1293fb6dcef6SErwan Le Ray 
1294fb6dcef6SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
1295fb6dcef6SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
1296fb6dcef6SErwan Le Ray 
1297fb6dcef6SErwan Le Ray 	return err;
129848a6092fSMaxime Coquelin }
129948a6092fSMaxime Coquelin 
130048a6092fSMaxime Coquelin 
130148a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE
130248a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch)
130348a6092fSMaxime Coquelin {
1304ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1305ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1306ada8618fSAlexandre TORGUE 
1307ada8618fSAlexandre TORGUE 	while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
130848a6092fSMaxime Coquelin 		cpu_relax();
130948a6092fSMaxime Coquelin 
1310ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
131148a6092fSMaxime Coquelin }
131248a6092fSMaxime Coquelin 
131348a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
131448a6092fSMaxime Coquelin {
131548a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
1316ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1317ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
131887f1f809SAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
131948a6092fSMaxime Coquelin 	unsigned long flags;
132048a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
132148a6092fSMaxime Coquelin 	int locked = 1;
132248a6092fSMaxime Coquelin 
132348a6092fSMaxime Coquelin 	local_irq_save(flags);
132448a6092fSMaxime Coquelin 	if (port->sysrq)
132548a6092fSMaxime Coquelin 		locked = 0;
132648a6092fSMaxime Coquelin 	else if (oops_in_progress)
132748a6092fSMaxime Coquelin 		locked = spin_trylock(&port->lock);
132848a6092fSMaxime Coquelin 	else
132948a6092fSMaxime Coquelin 		spin_lock(&port->lock);
133048a6092fSMaxime Coquelin 
133187f1f809SAlexandre TORGUE 	/* Save and disable interrupts, enable the transmitter */
1332ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
133348a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
133487f1f809SAlexandre TORGUE 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1335ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
133648a6092fSMaxime Coquelin 
133748a6092fSMaxime Coquelin 	uart_console_write(port, s, cnt, stm32_console_putchar);
133848a6092fSMaxime Coquelin 
133948a6092fSMaxime Coquelin 	/* Restore interrupt state */
1340ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
134148a6092fSMaxime Coquelin 
134248a6092fSMaxime Coquelin 	if (locked)
134348a6092fSMaxime Coquelin 		spin_unlock(&port->lock);
134448a6092fSMaxime Coquelin 	local_irq_restore(flags);
134548a6092fSMaxime Coquelin }
134648a6092fSMaxime Coquelin 
134748a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options)
134848a6092fSMaxime Coquelin {
134948a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
135048a6092fSMaxime Coquelin 	int baud = 9600;
135148a6092fSMaxime Coquelin 	int bits = 8;
135248a6092fSMaxime Coquelin 	int parity = 'n';
135348a6092fSMaxime Coquelin 	int flow = 'n';
135448a6092fSMaxime Coquelin 
135548a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
135648a6092fSMaxime Coquelin 		return -ENODEV;
135748a6092fSMaxime Coquelin 
135848a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
135948a6092fSMaxime Coquelin 
136048a6092fSMaxime Coquelin 	/*
136148a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
136248a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
136348a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
136448a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
136548a6092fSMaxime Coquelin 	 */
136648a6092fSMaxime Coquelin 	if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
136748a6092fSMaxime Coquelin 		return -ENXIO;
136848a6092fSMaxime Coquelin 
136948a6092fSMaxime Coquelin 	if (options)
137048a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
137148a6092fSMaxime Coquelin 
137248a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
137348a6092fSMaxime Coquelin }
137448a6092fSMaxime Coquelin 
137548a6092fSMaxime Coquelin static struct console stm32_console = {
137648a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
137748a6092fSMaxime Coquelin 	.device		= uart_console_device,
137848a6092fSMaxime Coquelin 	.write		= stm32_console_write,
137948a6092fSMaxime Coquelin 	.setup		= stm32_console_setup,
138048a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
138148a6092fSMaxime Coquelin 	.index		= -1,
138248a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
138348a6092fSMaxime Coquelin };
138448a6092fSMaxime Coquelin 
138548a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
138648a6092fSMaxime Coquelin 
138748a6092fSMaxime Coquelin #else
138848a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
138948a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
139048a6092fSMaxime Coquelin 
139148a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
139248a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
139348a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
139448a6092fSMaxime Coquelin 	.major		= 0,
139548a6092fSMaxime Coquelin 	.minor		= 0,
139648a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
139748a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
139848a6092fSMaxime Coquelin };
139948a6092fSMaxime Coquelin 
1400fe94347dSErwan Le Ray static void __maybe_unused stm32_serial_enable_wakeup(struct uart_port *port,
1401fe94347dSErwan Le Ray 						      bool enable)
1402270e5a74SFabrice Gasnier {
1403270e5a74SFabrice Gasnier 	struct stm32_port *stm32_port = to_stm32_port(port);
1404270e5a74SFabrice Gasnier 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1405270e5a74SFabrice Gasnier 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1406270e5a74SFabrice Gasnier 	u32 val;
1407270e5a74SFabrice Gasnier 
14082c58e560SErwan Le Ray 	if (stm32_port->wakeirq <= 0)
1409270e5a74SFabrice Gasnier 		return;
1410270e5a74SFabrice Gasnier 
1411270e5a74SFabrice Gasnier 	if (enable) {
1412270e5a74SFabrice Gasnier 		stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1413270e5a74SFabrice Gasnier 		stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1414270e5a74SFabrice Gasnier 		val = readl_relaxed(port->membase + ofs->cr3);
1415270e5a74SFabrice Gasnier 		val &= ~USART_CR3_WUS_MASK;
1416270e5a74SFabrice Gasnier 		/* Enable Wake up interrupt from low power on start bit */
1417270e5a74SFabrice Gasnier 		val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1418270e5a74SFabrice Gasnier 		writel_relaxed(val, port->membase + ofs->cr3);
1419270e5a74SFabrice Gasnier 		stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1420270e5a74SFabrice Gasnier 	} else {
1421270e5a74SFabrice Gasnier 		stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1422270e5a74SFabrice Gasnier 	}
1423270e5a74SFabrice Gasnier }
1424270e5a74SFabrice Gasnier 
1425fe94347dSErwan Le Ray static int __maybe_unused stm32_serial_suspend(struct device *dev)
1426270e5a74SFabrice Gasnier {
1427270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
1428270e5a74SFabrice Gasnier 
1429270e5a74SFabrice Gasnier 	uart_suspend_port(&stm32_usart_driver, port);
1430270e5a74SFabrice Gasnier 
1431270e5a74SFabrice Gasnier 	if (device_may_wakeup(dev))
1432270e5a74SFabrice Gasnier 		stm32_serial_enable_wakeup(port, true);
1433270e5a74SFabrice Gasnier 	else
1434270e5a74SFabrice Gasnier 		stm32_serial_enable_wakeup(port, false);
1435270e5a74SFabrice Gasnier 
143655484fccSErwan Le Ray 	/*
143755484fccSErwan Le Ray 	 * When "no_console_suspend" is enabled, keep the pinctrl default state
143855484fccSErwan Le Ray 	 * and rely on bootloader stage to restore this state upon resume.
143955484fccSErwan Le Ray 	 * Otherwise, apply the idle or sleep states depending on wakeup
144055484fccSErwan Le Ray 	 * capabilities.
144155484fccSErwan Le Ray 	 */
144255484fccSErwan Le Ray 	if (console_suspend_enabled || !uart_console(port)) {
144355484fccSErwan Le Ray 		if (device_may_wakeup(dev))
144455484fccSErwan Le Ray 			pinctrl_pm_select_idle_state(dev);
144555484fccSErwan Le Ray 		else
144694616d9aSErwan Le Ray 			pinctrl_pm_select_sleep_state(dev);
144755484fccSErwan Le Ray 	}
144894616d9aSErwan Le Ray 
1449270e5a74SFabrice Gasnier 	return 0;
1450270e5a74SFabrice Gasnier }
1451270e5a74SFabrice Gasnier 
1452fe94347dSErwan Le Ray static int __maybe_unused stm32_serial_resume(struct device *dev)
1453270e5a74SFabrice Gasnier {
1454270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
1455270e5a74SFabrice Gasnier 
145694616d9aSErwan Le Ray 	pinctrl_pm_select_default_state(dev);
145794616d9aSErwan Le Ray 
1458270e5a74SFabrice Gasnier 	if (device_may_wakeup(dev))
1459270e5a74SFabrice Gasnier 		stm32_serial_enable_wakeup(port, false);
1460270e5a74SFabrice Gasnier 
1461270e5a74SFabrice Gasnier 	return uart_resume_port(&stm32_usart_driver, port);
1462270e5a74SFabrice Gasnier }
1463270e5a74SFabrice Gasnier 
1464fb6dcef6SErwan Le Ray static int __maybe_unused stm32_serial_runtime_suspend(struct device *dev)
1465fb6dcef6SErwan Le Ray {
1466fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1467fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1468fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1469fb6dcef6SErwan Le Ray 
1470fb6dcef6SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
1471fb6dcef6SErwan Le Ray 
1472fb6dcef6SErwan Le Ray 	return 0;
1473fb6dcef6SErwan Le Ray }
1474fb6dcef6SErwan Le Ray 
1475fb6dcef6SErwan Le Ray static int __maybe_unused stm32_serial_runtime_resume(struct device *dev)
1476fb6dcef6SErwan Le Ray {
1477fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1478fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1479fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1480fb6dcef6SErwan Le Ray 
1481fb6dcef6SErwan Le Ray 	return clk_prepare_enable(stm32port->clk);
1482fb6dcef6SErwan Le Ray }
1483fb6dcef6SErwan Le Ray 
1484270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = {
1485fb6dcef6SErwan Le Ray 	SET_RUNTIME_PM_OPS(stm32_serial_runtime_suspend,
1486fb6dcef6SErwan Le Ray 			   stm32_serial_runtime_resume, NULL)
1487270e5a74SFabrice Gasnier 	SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1488270e5a74SFabrice Gasnier };
1489270e5a74SFabrice Gasnier 
149048a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
149148a6092fSMaxime Coquelin 	.probe		= stm32_serial_probe,
149248a6092fSMaxime Coquelin 	.remove		= stm32_serial_remove,
149348a6092fSMaxime Coquelin 	.driver	= {
149448a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
1495270e5a74SFabrice Gasnier 		.pm	= &stm32_serial_pm_ops,
149648a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
149748a6092fSMaxime Coquelin 	},
149848a6092fSMaxime Coquelin };
149948a6092fSMaxime Coquelin 
150048a6092fSMaxime Coquelin static int __init usart_init(void)
150148a6092fSMaxime Coquelin {
150248a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
150348a6092fSMaxime Coquelin 	int ret;
150448a6092fSMaxime Coquelin 
150548a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
150648a6092fSMaxime Coquelin 
150748a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
150848a6092fSMaxime Coquelin 	if (ret)
150948a6092fSMaxime Coquelin 		return ret;
151048a6092fSMaxime Coquelin 
151148a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
151248a6092fSMaxime Coquelin 	if (ret)
151348a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
151448a6092fSMaxime Coquelin 
151548a6092fSMaxime Coquelin 	return ret;
151648a6092fSMaxime Coquelin }
151748a6092fSMaxime Coquelin 
151848a6092fSMaxime Coquelin static void __exit usart_exit(void)
151948a6092fSMaxime Coquelin {
152048a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
152148a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
152248a6092fSMaxime Coquelin }
152348a6092fSMaxime Coquelin 
152448a6092fSMaxime Coquelin module_init(usart_init);
152548a6092fSMaxime Coquelin module_exit(usart_exit);
152648a6092fSMaxime Coquelin 
152748a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
152848a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
152948a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
1530