xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 9feedaa7f37b2734536562eb1bc8e2d55e9a38ee)
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 
34bc5a0b55SAlexandre TORGUE #include "stm32-usart.h"
3548a6092fSMaxime Coquelin 
3648a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port);
3734891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port);
3848a6092fSMaxime Coquelin 
3948a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port)
4048a6092fSMaxime Coquelin {
4148a6092fSMaxime Coquelin 	return container_of(port, struct stm32_port, port);
4248a6092fSMaxime Coquelin }
4348a6092fSMaxime Coquelin 
4448a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
4548a6092fSMaxime Coquelin {
4648a6092fSMaxime Coquelin 	u32 val;
4748a6092fSMaxime Coquelin 
4848a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
4948a6092fSMaxime Coquelin 	val |= bits;
5048a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
5148a6092fSMaxime Coquelin }
5248a6092fSMaxime Coquelin 
5348a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
5448a6092fSMaxime Coquelin {
5548a6092fSMaxime Coquelin 	u32 val;
5648a6092fSMaxime Coquelin 
5748a6092fSMaxime Coquelin 	val = readl_relaxed(port->membase + reg);
5848a6092fSMaxime Coquelin 	val &= ~bits;
5948a6092fSMaxime Coquelin 	writel_relaxed(val, port->membase + reg);
6048a6092fSMaxime Coquelin }
6148a6092fSMaxime Coquelin 
621bcda09dSBich HEMON static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
631bcda09dSBich HEMON 				   u32 delay_DDE, u32 baud)
641bcda09dSBich HEMON {
651bcda09dSBich HEMON 	u32 rs485_deat_dedt;
661bcda09dSBich HEMON 	u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
671bcda09dSBich HEMON 	bool over8;
681bcda09dSBich HEMON 
691bcda09dSBich HEMON 	*cr3 |= USART_CR3_DEM;
701bcda09dSBich HEMON 	over8 = *cr1 & USART_CR1_OVER8;
711bcda09dSBich HEMON 
721bcda09dSBich HEMON 	if (over8)
731bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 8;
741bcda09dSBich HEMON 	else
751bcda09dSBich HEMON 		rs485_deat_dedt = delay_ADE * baud * 16;
761bcda09dSBich HEMON 
771bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
781bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
791bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
801bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
811bcda09dSBich HEMON 			   USART_CR1_DEAT_MASK;
821bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
831bcda09dSBich HEMON 
841bcda09dSBich HEMON 	if (over8)
851bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 8;
861bcda09dSBich HEMON 	else
871bcda09dSBich HEMON 		rs485_deat_dedt = delay_DDE * baud * 16;
881bcda09dSBich HEMON 
891bcda09dSBich HEMON 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
901bcda09dSBich HEMON 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
911bcda09dSBich HEMON 			  rs485_deat_dedt_max : rs485_deat_dedt;
921bcda09dSBich HEMON 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
931bcda09dSBich HEMON 			   USART_CR1_DEDT_MASK;
941bcda09dSBich HEMON 	*cr1 |= rs485_deat_dedt;
951bcda09dSBich HEMON }
961bcda09dSBich HEMON 
971bcda09dSBich HEMON static int stm32_config_rs485(struct uart_port *port,
981bcda09dSBich HEMON 			      struct serial_rs485 *rs485conf)
991bcda09dSBich HEMON {
1001bcda09dSBich HEMON 	struct stm32_port *stm32_port = to_stm32_port(port);
1011bcda09dSBich HEMON 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1021bcda09dSBich HEMON 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1031bcda09dSBich HEMON 	u32 usartdiv, baud, cr1, cr3;
1041bcda09dSBich HEMON 	bool over8;
1051bcda09dSBich HEMON 
1061bcda09dSBich HEMON 	stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1071bcda09dSBich HEMON 
1081bcda09dSBich HEMON 	port->rs485 = *rs485conf;
1091bcda09dSBich HEMON 
1101bcda09dSBich HEMON 	rs485conf->flags |= SER_RS485_RX_DURING_TX;
1111bcda09dSBich HEMON 
1121bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
1131bcda09dSBich HEMON 		cr1 = readl_relaxed(port->membase + ofs->cr1);
1141bcda09dSBich HEMON 		cr3 = readl_relaxed(port->membase + ofs->cr3);
1151bcda09dSBich HEMON 		usartdiv = readl_relaxed(port->membase + ofs->brr);
1161bcda09dSBich HEMON 		usartdiv = usartdiv & GENMASK(15, 0);
1171bcda09dSBich HEMON 		over8 = cr1 & USART_CR1_OVER8;
1181bcda09dSBich HEMON 
1191bcda09dSBich HEMON 		if (over8)
1201bcda09dSBich HEMON 			usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
1211bcda09dSBich HEMON 				   << USART_BRR_04_R_SHIFT;
1221bcda09dSBich HEMON 
1231bcda09dSBich HEMON 		baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
1241bcda09dSBich HEMON 		stm32_config_reg_rs485(&cr1, &cr3,
1251bcda09dSBich HEMON 				       rs485conf->delay_rts_before_send,
1261bcda09dSBich HEMON 				       rs485conf->delay_rts_after_send, baud);
1271bcda09dSBich HEMON 
1281bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
1291bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
1301bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1311bcda09dSBich HEMON 		} else {
1321bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
1331bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1341bcda09dSBich HEMON 		}
1351bcda09dSBich HEMON 
1361bcda09dSBich HEMON 		writel_relaxed(cr3, port->membase + ofs->cr3);
1371bcda09dSBich HEMON 		writel_relaxed(cr1, port->membase + ofs->cr1);
1381bcda09dSBich HEMON 	} else {
1391bcda09dSBich HEMON 		stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP);
1401bcda09dSBich HEMON 		stm32_clr_bits(port, ofs->cr1,
1411bcda09dSBich HEMON 			       USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1421bcda09dSBich HEMON 	}
1431bcda09dSBich HEMON 
1441bcda09dSBich HEMON 	stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1451bcda09dSBich HEMON 
1461bcda09dSBich HEMON 	return 0;
1471bcda09dSBich HEMON }
1481bcda09dSBich HEMON 
1491bcda09dSBich HEMON static int stm32_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 
1611bcda09dSBich HEMON 	uart_get_rs485_mode(&pdev->dev, rs485conf);
1621bcda09dSBich HEMON 
1631bcda09dSBich HEMON 	return 0;
1641bcda09dSBich HEMON }
1651bcda09dSBich HEMON 
166b97055bcSBaoyou Xie static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
16734891872SAlexandre TORGUE 			    bool threaded)
16834891872SAlexandre TORGUE {
16934891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
17034891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
17134891872SAlexandre TORGUE 	enum dma_status status;
17234891872SAlexandre TORGUE 	struct dma_tx_state state;
17334891872SAlexandre TORGUE 
17434891872SAlexandre TORGUE 	*sr = readl_relaxed(port->membase + ofs->isr);
17534891872SAlexandre TORGUE 
17634891872SAlexandre TORGUE 	if (threaded && stm32_port->rx_ch) {
17734891872SAlexandre TORGUE 		status = dmaengine_tx_status(stm32_port->rx_ch,
17834891872SAlexandre TORGUE 					     stm32_port->rx_ch->cookie,
17934891872SAlexandre TORGUE 					     &state);
18034891872SAlexandre TORGUE 		if ((status == DMA_IN_PROGRESS) &&
18134891872SAlexandre TORGUE 		    (*last_res != state.residue))
18234891872SAlexandre TORGUE 			return 1;
18334891872SAlexandre TORGUE 		else
18434891872SAlexandre TORGUE 			return 0;
18534891872SAlexandre TORGUE 	} else if (*sr & USART_SR_RXNE) {
18634891872SAlexandre TORGUE 		return 1;
18734891872SAlexandre TORGUE 	}
18834891872SAlexandre TORGUE 	return 0;
18934891872SAlexandre TORGUE }
19034891872SAlexandre TORGUE 
1916c5962f3SErwan Le Ray static unsigned long stm32_get_char(struct uart_port *port, u32 *sr,
1926c5962f3SErwan Le Ray 				    int *last_res)
19334891872SAlexandre TORGUE {
19434891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
19534891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
19634891872SAlexandre TORGUE 	unsigned long c;
19734891872SAlexandre TORGUE 
19834891872SAlexandre TORGUE 	if (stm32_port->rx_ch) {
19934891872SAlexandre TORGUE 		c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
20034891872SAlexandre TORGUE 		if ((*last_res) == 0)
20134891872SAlexandre TORGUE 			*last_res = RX_BUF_L;
20234891872SAlexandre TORGUE 	} else {
2036c5962f3SErwan Le Ray 		c = readl_relaxed(port->membase + ofs->rdr);
2046c5962f3SErwan Le Ray 		/* apply RDR data mask */
2056c5962f3SErwan Le Ray 		c &= stm32_port->rdr_mask;
20634891872SAlexandre TORGUE 	}
2076c5962f3SErwan Le Ray 
2086c5962f3SErwan Le Ray 	return c;
20934891872SAlexandre TORGUE }
21034891872SAlexandre TORGUE 
21134891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded)
21248a6092fSMaxime Coquelin {
21348a6092fSMaxime Coquelin 	struct tty_port *tport = &port->state->port;
214ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
215ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
21648a6092fSMaxime Coquelin 	unsigned long c;
21748a6092fSMaxime Coquelin 	u32 sr;
21848a6092fSMaxime Coquelin 	char flag;
21948a6092fSMaxime Coquelin 
22029d60981SAndy Shevchenko 	if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
22148a6092fSMaxime Coquelin 		pm_wakeup_event(tport->tty->dev, 0);
22248a6092fSMaxime Coquelin 
223e5707915SGerald Baeza 	while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
22448a6092fSMaxime Coquelin 		sr |= USART_SR_DUMMY_RX;
22548a6092fSMaxime Coquelin 		flag = TTY_NORMAL;
22648a6092fSMaxime Coquelin 
2274f01d833SErwan Le Ray 		/*
2284f01d833SErwan Le Ray 		 * Status bits has to be cleared before reading the RDR:
2294f01d833SErwan Le Ray 		 * In FIFO mode, reading the RDR will pop the next data
2304f01d833SErwan Le Ray 		 * (if any) along with its status bits into the SR.
2314f01d833SErwan Le Ray 		 * Not doing so leads to misalignement between RDR and SR,
2324f01d833SErwan Le Ray 		 * and clear status bits of the next rx data.
2334f01d833SErwan Le Ray 		 *
2344f01d833SErwan Le Ray 		 * Clear errors flags for stm32f7 and stm32h7 compatible
2354f01d833SErwan Le Ray 		 * devices. On stm32f4 compatible devices, the error bit is
2364f01d833SErwan Le Ray 		 * cleared by the sequence [read SR - read DR].
2374f01d833SErwan Le Ray 		 */
2384f01d833SErwan Le Ray 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
2391250ed71SFabrice Gasnier 			writel_relaxed(sr & USART_SR_ERR_MASK,
2401250ed71SFabrice Gasnier 				       port->membase + ofs->icr);
2414f01d833SErwan Le Ray 
2424f01d833SErwan Le Ray 		c = stm32_get_char(port, &sr, &stm32_port->last_res);
2434f01d833SErwan Le Ray 		port->icount.rx++;
24448a6092fSMaxime Coquelin 		if (sr & USART_SR_ERR_MASK) {
2454f01d833SErwan Le Ray 			if (sr & USART_SR_ORE) {
24648a6092fSMaxime Coquelin 				port->icount.overrun++;
24748a6092fSMaxime Coquelin 			} else if (sr & USART_SR_PE) {
24848a6092fSMaxime Coquelin 				port->icount.parity++;
24948a6092fSMaxime Coquelin 			} else if (sr & USART_SR_FE) {
2504f01d833SErwan Le Ray 				/* Break detection if character is null */
2514f01d833SErwan Le Ray 				if (!c) {
2524f01d833SErwan Le Ray 					port->icount.brk++;
2534f01d833SErwan Le Ray 					if (uart_handle_break(port))
2544f01d833SErwan Le Ray 						continue;
2554f01d833SErwan Le Ray 				} else {
25648a6092fSMaxime Coquelin 					port->icount.frame++;
25748a6092fSMaxime Coquelin 				}
2584f01d833SErwan Le Ray 			}
25948a6092fSMaxime Coquelin 
26048a6092fSMaxime Coquelin 			sr &= port->read_status_mask;
26148a6092fSMaxime Coquelin 
2624f01d833SErwan Le Ray 			if (sr & USART_SR_PE) {
26348a6092fSMaxime Coquelin 				flag = TTY_PARITY;
2644f01d833SErwan Le Ray 			} else if (sr & USART_SR_FE) {
2654f01d833SErwan Le Ray 				if (!c)
2664f01d833SErwan Le Ray 					flag = TTY_BREAK;
2674f01d833SErwan Le Ray 				else
26848a6092fSMaxime Coquelin 					flag = TTY_FRAME;
26948a6092fSMaxime Coquelin 			}
2704f01d833SErwan Le Ray 		}
27148a6092fSMaxime Coquelin 
27248a6092fSMaxime Coquelin 		if (uart_handle_sysrq_char(port, c))
27348a6092fSMaxime Coquelin 			continue;
27448a6092fSMaxime Coquelin 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
27548a6092fSMaxime Coquelin 	}
27648a6092fSMaxime Coquelin 
27748a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
27848a6092fSMaxime Coquelin 	tty_flip_buffer_push(tport);
27948a6092fSMaxime Coquelin 	spin_lock(&port->lock);
28048a6092fSMaxime Coquelin }
28148a6092fSMaxime Coquelin 
28234891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg)
28334891872SAlexandre TORGUE {
28434891872SAlexandre TORGUE 	struct uart_port *port = arg;
28534891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
28634891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
28734891872SAlexandre TORGUE 
28834891872SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
28934891872SAlexandre TORGUE 	stm32port->tx_dma_busy = false;
29034891872SAlexandre TORGUE 
29134891872SAlexandre TORGUE 	/* Let's see if we have pending data to send */
29234891872SAlexandre TORGUE 	stm32_transmit_chars(port);
29334891872SAlexandre TORGUE }
29434891872SAlexandre TORGUE 
295d075719eSErwan Le Ray static void stm32_tx_interrupt_enable(struct uart_port *port)
296d075719eSErwan Le Ray {
297d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
298d075719eSErwan Le Ray 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
299d075719eSErwan Le Ray 
300d075719eSErwan Le Ray 	/*
301d075719eSErwan Le Ray 	 * Enables TX FIFO threashold irq when FIFO is enabled,
302d075719eSErwan Le Ray 	 * or TX empty irq when FIFO is disabled
303d075719eSErwan Le Ray 	 */
304d075719eSErwan Le Ray 	if (stm32_port->fifoen)
305d075719eSErwan Le Ray 		stm32_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
306d075719eSErwan Le Ray 	else
307d075719eSErwan Le Ray 		stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
308d075719eSErwan Le Ray }
309d075719eSErwan Le Ray 
310d075719eSErwan Le Ray static void stm32_tx_interrupt_disable(struct uart_port *port)
311d075719eSErwan Le Ray {
312d075719eSErwan Le Ray 	struct stm32_port *stm32_port = to_stm32_port(port);
313d075719eSErwan Le Ray 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
314d075719eSErwan Le Ray 
315d075719eSErwan Le Ray 	if (stm32_port->fifoen)
316d075719eSErwan Le Ray 		stm32_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
317d075719eSErwan Le Ray 	else
318d075719eSErwan Le Ray 		stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
319d075719eSErwan Le Ray }
320d075719eSErwan Le Ray 
32134891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port)
32234891872SAlexandre TORGUE {
32334891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
32434891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
32534891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
32634891872SAlexandre TORGUE 
32734891872SAlexandre TORGUE 	if (stm32_port->tx_dma_busy) {
32834891872SAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
32934891872SAlexandre TORGUE 		stm32_port->tx_dma_busy = false;
33034891872SAlexandre TORGUE 	}
33134891872SAlexandre TORGUE 
3325d9176edSErwan Le Ray 	while (!uart_circ_empty(xmit)) {
3335d9176edSErwan Le Ray 		/* Check that TDR is empty before filling FIFO */
3345d9176edSErwan Le Ray 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
3355d9176edSErwan Le Ray 			break;
33634891872SAlexandre TORGUE 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
33734891872SAlexandre TORGUE 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
33834891872SAlexandre TORGUE 		port->icount.tx++;
33934891872SAlexandre TORGUE 	}
34034891872SAlexandre TORGUE 
3415d9176edSErwan Le Ray 	/* rely on TXE irq (mask or unmask) for sending remaining data */
3425d9176edSErwan Le Ray 	if (uart_circ_empty(xmit))
343d075719eSErwan Le Ray 		stm32_tx_interrupt_disable(port);
3445d9176edSErwan Le Ray 	else
345d075719eSErwan Le Ray 		stm32_tx_interrupt_enable(port);
3465d9176edSErwan Le Ray }
3475d9176edSErwan Le Ray 
34834891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port)
34934891872SAlexandre TORGUE {
35034891872SAlexandre TORGUE 	struct stm32_port *stm32port = to_stm32_port(port);
35134891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
35234891872SAlexandre TORGUE 	struct circ_buf *xmit = &port->state->xmit;
35334891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
35434891872SAlexandre TORGUE 	dma_cookie_t cookie;
35534891872SAlexandre TORGUE 	unsigned int count, i;
35634891872SAlexandre TORGUE 
35734891872SAlexandre TORGUE 	if (stm32port->tx_dma_busy)
35834891872SAlexandre TORGUE 		return;
35934891872SAlexandre TORGUE 
36034891872SAlexandre TORGUE 	stm32port->tx_dma_busy = true;
36134891872SAlexandre TORGUE 
36234891872SAlexandre TORGUE 	count = uart_circ_chars_pending(xmit);
36334891872SAlexandre TORGUE 
36434891872SAlexandre TORGUE 	if (count > TX_BUF_L)
36534891872SAlexandre TORGUE 		count = TX_BUF_L;
36634891872SAlexandre TORGUE 
36734891872SAlexandre TORGUE 	if (xmit->tail < xmit->head) {
36834891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
36934891872SAlexandre TORGUE 	} else {
37034891872SAlexandre TORGUE 		size_t one = UART_XMIT_SIZE - xmit->tail;
37134891872SAlexandre TORGUE 		size_t two;
37234891872SAlexandre TORGUE 
37334891872SAlexandre TORGUE 		if (one > count)
37434891872SAlexandre TORGUE 			one = count;
37534891872SAlexandre TORGUE 		two = count - one;
37634891872SAlexandre TORGUE 
37734891872SAlexandre TORGUE 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
37834891872SAlexandre TORGUE 		if (two)
37934891872SAlexandre TORGUE 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
38034891872SAlexandre TORGUE 	}
38134891872SAlexandre TORGUE 
38234891872SAlexandre TORGUE 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
38334891872SAlexandre TORGUE 					   stm32port->tx_dma_buf,
38434891872SAlexandre TORGUE 					   count,
38534891872SAlexandre TORGUE 					   DMA_MEM_TO_DEV,
38634891872SAlexandre TORGUE 					   DMA_PREP_INTERRUPT);
38734891872SAlexandre TORGUE 
38834891872SAlexandre TORGUE 	if (!desc) {
38934891872SAlexandre TORGUE 		for (i = count; i > 0; i--)
39034891872SAlexandre TORGUE 			stm32_transmit_chars_pio(port);
39134891872SAlexandre TORGUE 		return;
39234891872SAlexandre TORGUE 	}
39334891872SAlexandre TORGUE 
39434891872SAlexandre TORGUE 	desc->callback = stm32_tx_dma_complete;
39534891872SAlexandre TORGUE 	desc->callback_param = port;
39634891872SAlexandre TORGUE 
39734891872SAlexandre TORGUE 	/* Push current DMA TX transaction in the pending queue */
39834891872SAlexandre TORGUE 	cookie = dmaengine_submit(desc);
39934891872SAlexandre TORGUE 
40034891872SAlexandre TORGUE 	/* Issue pending DMA TX requests */
40134891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->tx_ch);
40234891872SAlexandre TORGUE 
40334891872SAlexandre TORGUE 	stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
40434891872SAlexandre TORGUE 
40534891872SAlexandre TORGUE 	xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
40634891872SAlexandre TORGUE 	port->icount.tx += count;
40734891872SAlexandre TORGUE }
40834891872SAlexandre TORGUE 
40948a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port)
41048a6092fSMaxime Coquelin {
411ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
412ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
41348a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
41448a6092fSMaxime Coquelin 
41548a6092fSMaxime Coquelin 	if (port->x_char) {
41634891872SAlexandre TORGUE 		if (stm32_port->tx_dma_busy)
41734891872SAlexandre TORGUE 			stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
418ada8618fSAlexandre TORGUE 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
41948a6092fSMaxime Coquelin 		port->x_char = 0;
42048a6092fSMaxime Coquelin 		port->icount.tx++;
42134891872SAlexandre TORGUE 		if (stm32_port->tx_dma_busy)
42234891872SAlexandre TORGUE 			stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
42348a6092fSMaxime Coquelin 		return;
42448a6092fSMaxime Coquelin 	}
42548a6092fSMaxime Coquelin 
426b83b957cSErwan Le Ray 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
427d075719eSErwan Le Ray 		stm32_tx_interrupt_disable(port);
42848a6092fSMaxime Coquelin 		return;
42948a6092fSMaxime Coquelin 	}
43048a6092fSMaxime Coquelin 
43164c32eabSErwan Le Ray 	if (ofs->icr == UNDEF_REG)
43264c32eabSErwan Le Ray 		stm32_clr_bits(port, ofs->isr, USART_SR_TC);
43364c32eabSErwan Le Ray 	else
4341250ed71SFabrice Gasnier 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
43564c32eabSErwan Le Ray 
43634891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
43734891872SAlexandre TORGUE 		stm32_transmit_chars_dma(port);
43834891872SAlexandre TORGUE 	else
43934891872SAlexandre TORGUE 		stm32_transmit_chars_pio(port);
44048a6092fSMaxime Coquelin 
44148a6092fSMaxime Coquelin 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
44248a6092fSMaxime Coquelin 		uart_write_wakeup(port);
44348a6092fSMaxime Coquelin 
44448a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
445d075719eSErwan Le Ray 		stm32_tx_interrupt_disable(port);
44648a6092fSMaxime Coquelin }
44748a6092fSMaxime Coquelin 
44848a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr)
44948a6092fSMaxime Coquelin {
45048a6092fSMaxime Coquelin 	struct uart_port *port = ptr;
451ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
452ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
45348a6092fSMaxime Coquelin 	u32 sr;
45448a6092fSMaxime Coquelin 
45501d32d71SAlexandre TORGUE 	spin_lock(&port->lock);
45601d32d71SAlexandre TORGUE 
457ada8618fSAlexandre TORGUE 	sr = readl_relaxed(port->membase + ofs->isr);
45848a6092fSMaxime Coquelin 
4594cc0ed62SErwan Le Ray 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
4604cc0ed62SErwan Le Ray 		writel_relaxed(USART_ICR_RTOCF,
4614cc0ed62SErwan Le Ray 			       port->membase + ofs->icr);
4624cc0ed62SErwan Le Ray 
463270e5a74SFabrice Gasnier 	if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
464270e5a74SFabrice Gasnier 		writel_relaxed(USART_ICR_WUCF,
465270e5a74SFabrice Gasnier 			       port->membase + ofs->icr);
466270e5a74SFabrice Gasnier 
46734891872SAlexandre TORGUE 	if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
46834891872SAlexandre TORGUE 		stm32_receive_chars(port, false);
46948a6092fSMaxime Coquelin 
47034891872SAlexandre TORGUE 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
47148a6092fSMaxime Coquelin 		stm32_transmit_chars(port);
47248a6092fSMaxime Coquelin 
47301d32d71SAlexandre TORGUE 	spin_unlock(&port->lock);
47401d32d71SAlexandre TORGUE 
47534891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
47634891872SAlexandre TORGUE 		return IRQ_WAKE_THREAD;
47734891872SAlexandre TORGUE 	else
47834891872SAlexandre TORGUE 		return IRQ_HANDLED;
47934891872SAlexandre TORGUE }
48034891872SAlexandre TORGUE 
48134891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
48234891872SAlexandre TORGUE {
48334891872SAlexandre TORGUE 	struct uart_port *port = ptr;
48434891872SAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
48534891872SAlexandre TORGUE 
48634891872SAlexandre TORGUE 	spin_lock(&port->lock);
48734891872SAlexandre TORGUE 
48834891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
48934891872SAlexandre TORGUE 		stm32_receive_chars(port, true);
49034891872SAlexandre TORGUE 
49148a6092fSMaxime Coquelin 	spin_unlock(&port->lock);
49248a6092fSMaxime Coquelin 
49348a6092fSMaxime Coquelin 	return IRQ_HANDLED;
49448a6092fSMaxime Coquelin }
49548a6092fSMaxime Coquelin 
49648a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port)
49748a6092fSMaxime Coquelin {
498ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
499ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
500ada8618fSAlexandre TORGUE 
501ada8618fSAlexandre TORGUE 	return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE;
50248a6092fSMaxime Coquelin }
50348a6092fSMaxime Coquelin 
50448a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
50548a6092fSMaxime Coquelin {
506ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
507ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
508ada8618fSAlexandre TORGUE 
50948a6092fSMaxime Coquelin 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
510ada8618fSAlexandre TORGUE 		stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
51148a6092fSMaxime Coquelin 	else
512ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
51348a6092fSMaxime Coquelin }
51448a6092fSMaxime Coquelin 
51548a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port)
51648a6092fSMaxime Coquelin {
51748a6092fSMaxime Coquelin 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
51848a6092fSMaxime Coquelin 	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
51948a6092fSMaxime Coquelin }
52048a6092fSMaxime Coquelin 
52148a6092fSMaxime Coquelin /* Transmit stop */
52248a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port)
52348a6092fSMaxime Coquelin {
524d075719eSErwan Le Ray 	stm32_tx_interrupt_disable(port);
52548a6092fSMaxime Coquelin }
52648a6092fSMaxime Coquelin 
52748a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */
52848a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port)
52948a6092fSMaxime Coquelin {
53048a6092fSMaxime Coquelin 	struct circ_buf *xmit = &port->state->xmit;
53148a6092fSMaxime Coquelin 
53248a6092fSMaxime Coquelin 	if (uart_circ_empty(xmit))
53348a6092fSMaxime Coquelin 		return;
53448a6092fSMaxime Coquelin 
53534891872SAlexandre TORGUE 	stm32_transmit_chars(port);
53648a6092fSMaxime Coquelin }
53748a6092fSMaxime Coquelin 
53848a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */
53948a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port)
54048a6092fSMaxime Coquelin {
541ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
542ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
54348a6092fSMaxime Coquelin 	unsigned long flags;
54448a6092fSMaxime Coquelin 
54548a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
5464cc0ed62SErwan Le Ray 	stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
547d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
548d0a6a7bcSErwan Le Ray 		stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
549d0a6a7bcSErwan Le Ray 
55048a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
55148a6092fSMaxime Coquelin }
55248a6092fSMaxime Coquelin 
55348a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */
55448a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port)
55548a6092fSMaxime Coquelin {
556ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
557ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
55848a6092fSMaxime Coquelin 	unsigned long flags;
55948a6092fSMaxime Coquelin 
56048a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
5614cc0ed62SErwan Le Ray 	stm32_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
562d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
563d0a6a7bcSErwan Le Ray 		stm32_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
564d0a6a7bcSErwan Le Ray 
56548a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
56648a6092fSMaxime Coquelin }
56748a6092fSMaxime Coquelin 
56848a6092fSMaxime Coquelin /* Receive stop */
56948a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port)
57048a6092fSMaxime Coquelin {
571ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
572ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
573ada8618fSAlexandre TORGUE 
5744cc0ed62SErwan Le Ray 	stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
575d0a6a7bcSErwan Le Ray 	if (stm32_port->cr3_irq)
576d0a6a7bcSErwan Le Ray 		stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
577d0a6a7bcSErwan Le Ray 
57848a6092fSMaxime Coquelin }
57948a6092fSMaxime Coquelin 
58048a6092fSMaxime Coquelin /* Handle breaks - ignored by us */
58148a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state)
58248a6092fSMaxime Coquelin {
58348a6092fSMaxime Coquelin }
58448a6092fSMaxime Coquelin 
58548a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port)
58648a6092fSMaxime Coquelin {
587ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
588ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
58948a6092fSMaxime Coquelin 	const char *name = to_platform_device(port->dev)->name;
59048a6092fSMaxime Coquelin 	u32 val;
59148a6092fSMaxime Coquelin 	int ret;
59248a6092fSMaxime Coquelin 
59334891872SAlexandre TORGUE 	ret = request_threaded_irq(port->irq, stm32_interrupt,
59434891872SAlexandre TORGUE 				   stm32_threaded_interrupt,
59534891872SAlexandre TORGUE 				   IRQF_NO_SUSPEND, name, port);
59648a6092fSMaxime Coquelin 	if (ret)
59748a6092fSMaxime Coquelin 		return ret;
59848a6092fSMaxime Coquelin 
59984872dc4SErwan Le Ray 	/* RX FIFO Flush */
60084872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
60184872dc4SErwan Le Ray 		stm32_set_bits(port, ofs->rqr, USART_RQR_RXFRQ);
60248a6092fSMaxime Coquelin 
60384872dc4SErwan Le Ray 	/* Tx and RX FIFO configuration */
604d075719eSErwan Le Ray 	if (stm32_port->fifoen) {
605d075719eSErwan Le Ray 		val = readl_relaxed(port->membase + ofs->cr3);
606d0a6a7bcSErwan Le Ray 		val &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK);
607d075719eSErwan Le Ray 		val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT;
608d0a6a7bcSErwan Le Ray 		val |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT;
609d075719eSErwan Le Ray 		writel_relaxed(val, port->membase + ofs->cr3);
610d075719eSErwan Le Ray 	}
611d075719eSErwan Le Ray 
61284872dc4SErwan Le Ray 	/* RX FIFO enabling */
61384872dc4SErwan Le Ray 	val = stm32_port->cr1_irq | USART_CR1_RE;
61484872dc4SErwan Le Ray 	if (stm32_port->fifoen)
61584872dc4SErwan Le Ray 		val |= USART_CR1_FIFOEN;
61684872dc4SErwan Le Ray 	stm32_set_bits(port, ofs->cr1, val);
61784872dc4SErwan Le Ray 
61848a6092fSMaxime Coquelin 	return 0;
61948a6092fSMaxime Coquelin }
62048a6092fSMaxime Coquelin 
62148a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port)
62248a6092fSMaxime Coquelin {
623ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
624ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
62587f1f809SAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
62664c32eabSErwan Le Ray 	u32 val, isr;
62764c32eabSErwan Le Ray 	int ret;
62848a6092fSMaxime Coquelin 
6294cc0ed62SErwan Le Ray 	val = USART_CR1_TXEIE | USART_CR1_TE;
6304cc0ed62SErwan Le Ray 	val |= stm32_port->cr1_irq | USART_CR1_RE;
63187f1f809SAlexandre TORGUE 	val |= BIT(cfg->uart_enable_bit);
632351a762aSGerald Baeza 	if (stm32_port->fifoen)
633351a762aSGerald Baeza 		val |= USART_CR1_FIFOEN;
63464c32eabSErwan Le Ray 
63564c32eabSErwan Le Ray 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
63664c32eabSErwan Le Ray 					 isr, (isr & USART_SR_TC),
63764c32eabSErwan Le Ray 					 10, 100000);
63864c32eabSErwan Le Ray 
63964c32eabSErwan Le Ray 	if (ret)
64064c32eabSErwan Le Ray 		dev_err(port->dev, "transmission complete not set\n");
64164c32eabSErwan Le Ray 
642a14f66a4SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr1, val);
64348a6092fSMaxime Coquelin 
64448a6092fSMaxime Coquelin 	free_irq(port->irq, port);
64548a6092fSMaxime Coquelin }
64648a6092fSMaxime Coquelin 
647929ffa4aSYueHaibing static unsigned int stm32_get_databits(struct ktermios *termios)
648c8a9d043SErwan Le Ray {
649c8a9d043SErwan Le Ray 	unsigned int bits;
650c8a9d043SErwan Le Ray 
651c8a9d043SErwan Le Ray 	tcflag_t cflag = termios->c_cflag;
652c8a9d043SErwan Le Ray 
653c8a9d043SErwan Le Ray 	switch (cflag & CSIZE) {
654c8a9d043SErwan Le Ray 	/*
655c8a9d043SErwan Le Ray 	 * CSIZE settings are not necessarily supported in hardware.
656c8a9d043SErwan Le Ray 	 * CSIZE unsupported configurations are handled here to set word length
657c8a9d043SErwan Le Ray 	 * to 8 bits word as default configuration and to print debug message.
658c8a9d043SErwan Le Ray 	 */
659c8a9d043SErwan Le Ray 	case CS5:
660c8a9d043SErwan Le Ray 		bits = 5;
661c8a9d043SErwan Le Ray 		break;
662c8a9d043SErwan Le Ray 	case CS6:
663c8a9d043SErwan Le Ray 		bits = 6;
664c8a9d043SErwan Le Ray 		break;
665c8a9d043SErwan Le Ray 	case CS7:
666c8a9d043SErwan Le Ray 		bits = 7;
667c8a9d043SErwan Le Ray 		break;
668c8a9d043SErwan Le Ray 	/* default including CS8 */
669c8a9d043SErwan Le Ray 	default:
670c8a9d043SErwan Le Ray 		bits = 8;
671c8a9d043SErwan Le Ray 		break;
672c8a9d043SErwan Le Ray 	}
673c8a9d043SErwan Le Ray 
674c8a9d043SErwan Le Ray 	return bits;
675c8a9d043SErwan Le Ray }
676c8a9d043SErwan Le Ray 
67748a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
67848a6092fSMaxime Coquelin 			    struct ktermios *old)
67948a6092fSMaxime Coquelin {
68048a6092fSMaxime Coquelin 	struct stm32_port *stm32_port = to_stm32_port(port);
681ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
682ada8618fSAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
6831bcda09dSBich HEMON 	struct serial_rs485 *rs485conf = &port->rs485;
684c8a9d043SErwan Le Ray 	unsigned int baud, bits;
68548a6092fSMaxime Coquelin 	u32 usartdiv, mantissa, fraction, oversampling;
68648a6092fSMaxime Coquelin 	tcflag_t cflag = termios->c_cflag;
68748a6092fSMaxime Coquelin 	u32 cr1, cr2, cr3;
68848a6092fSMaxime Coquelin 	unsigned long flags;
68948a6092fSMaxime Coquelin 
69048a6092fSMaxime Coquelin 	if (!stm32_port->hw_flow_control)
69148a6092fSMaxime Coquelin 		cflag &= ~CRTSCTS;
69248a6092fSMaxime Coquelin 
69348a6092fSMaxime Coquelin 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
69448a6092fSMaxime Coquelin 
69548a6092fSMaxime Coquelin 	spin_lock_irqsave(&port->lock, flags);
69648a6092fSMaxime Coquelin 
69748a6092fSMaxime Coquelin 	/* Stop serial port and reset value */
698ada8618fSAlexandre TORGUE 	writel_relaxed(0, port->membase + ofs->cr1);
69948a6092fSMaxime Coquelin 
70084872dc4SErwan Le Ray 	/* flush RX & TX FIFO */
70184872dc4SErwan Le Ray 	if (ofs->rqr != UNDEF_REG)
70284872dc4SErwan Le Ray 		stm32_set_bits(port, ofs->rqr,
70384872dc4SErwan Le Ray 			       USART_RQR_TXFRQ | USART_RQR_RXFRQ);
7041bcda09dSBich HEMON 
70584872dc4SErwan Le Ray 	cr1 = USART_CR1_TE | USART_CR1_RE;
706351a762aSGerald Baeza 	if (stm32_port->fifoen)
707351a762aSGerald Baeza 		cr1 |= USART_CR1_FIFOEN;
70848a6092fSMaxime Coquelin 	cr2 = 0;
709d075719eSErwan Le Ray 	cr3 = readl_relaxed(port->membase + ofs->cr3);
710d0a6a7bcSErwan Le Ray 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG_MASK | USART_CR3_RXFTIE
711d075719eSErwan Le Ray 		| USART_CR3_TXFTCFG_MASK;
71248a6092fSMaxime Coquelin 
71348a6092fSMaxime Coquelin 	if (cflag & CSTOPB)
71448a6092fSMaxime Coquelin 		cr2 |= USART_CR2_STOP_2B;
71548a6092fSMaxime Coquelin 
716c8a9d043SErwan Le Ray 	bits = stm32_get_databits(termios);
7176c5962f3SErwan Le Ray 	stm32_port->rdr_mask = (BIT(bits) - 1);
718c8a9d043SErwan Le Ray 
71948a6092fSMaxime Coquelin 	if (cflag & PARENB) {
720c8a9d043SErwan Le Ray 		bits++;
72148a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PCE;
722c8a9d043SErwan Le Ray 	}
723c8a9d043SErwan Le Ray 
724c8a9d043SErwan Le Ray 	/*
725c8a9d043SErwan Le Ray 	 * Word length configuration:
726c8a9d043SErwan Le Ray 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
727c8a9d043SErwan Le Ray 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
728c8a9d043SErwan Le Ray 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
729c8a9d043SErwan Le Ray 	 * M0 and M1 already cleared by cr1 initialization.
730c8a9d043SErwan Le Ray 	 */
731c8a9d043SErwan Le Ray 	if (bits == 9)
732ada8618fSAlexandre TORGUE 		cr1 |= USART_CR1_M0;
733c8a9d043SErwan Le Ray 	else if ((bits == 7) && cfg->has_7bits_data)
734c8a9d043SErwan Le Ray 		cr1 |= USART_CR1_M1;
735c8a9d043SErwan Le Ray 	else if (bits != 8)
736c8a9d043SErwan Le Ray 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
737c8a9d043SErwan Le Ray 			, bits);
73848a6092fSMaxime Coquelin 
7394cc0ed62SErwan Le Ray 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
7404cc0ed62SErwan Le Ray 				       stm32_port->fifoen)) {
7414cc0ed62SErwan Le Ray 		if (cflag & CSTOPB)
7424cc0ed62SErwan Le Ray 			bits = bits + 3; /* 1 start bit + 2 stop bits */
7434cc0ed62SErwan Le Ray 		else
7444cc0ed62SErwan Le Ray 			bits = bits + 2; /* 1 start bit + 1 stop bit */
7454cc0ed62SErwan Le Ray 
7464cc0ed62SErwan Le Ray 		/* RX timeout irq to occur after last stop bit + bits */
7474cc0ed62SErwan Le Ray 		stm32_port->cr1_irq = USART_CR1_RTOIE;
7484cc0ed62SErwan Le Ray 		writel_relaxed(bits, port->membase + ofs->rtor);
7494cc0ed62SErwan Le Ray 		cr2 |= USART_CR2_RTOEN;
750d0a6a7bcSErwan Le Ray 		/* Not using dma, enable fifo threshold irq */
751d0a6a7bcSErwan Le Ray 		if (!stm32_port->rx_ch)
752d0a6a7bcSErwan Le Ray 			stm32_port->cr3_irq =  USART_CR3_RXFTIE;
7534cc0ed62SErwan Le Ray 	}
7544cc0ed62SErwan Le Ray 
755d0a6a7bcSErwan Le Ray 	cr1 |= stm32_port->cr1_irq;
756d0a6a7bcSErwan Le Ray 	cr3 |= stm32_port->cr3_irq;
757d0a6a7bcSErwan Le Ray 
75848a6092fSMaxime Coquelin 	if (cflag & PARODD)
75948a6092fSMaxime Coquelin 		cr1 |= USART_CR1_PS;
76048a6092fSMaxime Coquelin 
76148a6092fSMaxime Coquelin 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
76248a6092fSMaxime Coquelin 	if (cflag & CRTSCTS) {
76348a6092fSMaxime Coquelin 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
76435abe98fSBich HEMON 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
76548a6092fSMaxime Coquelin 	}
76648a6092fSMaxime Coquelin 
76748a6092fSMaxime Coquelin 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
76848a6092fSMaxime Coquelin 
76948a6092fSMaxime Coquelin 	/*
77048a6092fSMaxime Coquelin 	 * The USART supports 16 or 8 times oversampling.
77148a6092fSMaxime Coquelin 	 * By default we prefer 16 times oversampling, so that the receiver
77248a6092fSMaxime Coquelin 	 * has a better tolerance to clock deviations.
77348a6092fSMaxime Coquelin 	 * 8 times oversampling is only used to achieve higher speeds.
77448a6092fSMaxime Coquelin 	 */
77548a6092fSMaxime Coquelin 	if (usartdiv < 16) {
77648a6092fSMaxime Coquelin 		oversampling = 8;
7771bcda09dSBich HEMON 		cr1 |= USART_CR1_OVER8;
778ada8618fSAlexandre TORGUE 		stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
77948a6092fSMaxime Coquelin 	} else {
78048a6092fSMaxime Coquelin 		oversampling = 16;
7811bcda09dSBich HEMON 		cr1 &= ~USART_CR1_OVER8;
782ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
78348a6092fSMaxime Coquelin 	}
78448a6092fSMaxime Coquelin 
78548a6092fSMaxime Coquelin 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
78648a6092fSMaxime Coquelin 	fraction = usartdiv % oversampling;
787ada8618fSAlexandre TORGUE 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
78848a6092fSMaxime Coquelin 
78948a6092fSMaxime Coquelin 	uart_update_timeout(port, cflag, baud);
79048a6092fSMaxime Coquelin 
79148a6092fSMaxime Coquelin 	port->read_status_mask = USART_SR_ORE;
79248a6092fSMaxime Coquelin 	if (termios->c_iflag & INPCK)
79348a6092fSMaxime Coquelin 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
79448a6092fSMaxime Coquelin 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
7954f01d833SErwan Le Ray 		port->read_status_mask |= USART_SR_FE;
79648a6092fSMaxime Coquelin 
79748a6092fSMaxime Coquelin 	/* Characters to ignore */
79848a6092fSMaxime Coquelin 	port->ignore_status_mask = 0;
79948a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNPAR)
80048a6092fSMaxime Coquelin 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
80148a6092fSMaxime Coquelin 	if (termios->c_iflag & IGNBRK) {
8024f01d833SErwan Le Ray 		port->ignore_status_mask |= USART_SR_FE;
80348a6092fSMaxime Coquelin 		/*
80448a6092fSMaxime Coquelin 		 * If we're ignoring parity and break indicators,
80548a6092fSMaxime Coquelin 		 * ignore overruns too (for real raw support).
80648a6092fSMaxime Coquelin 		 */
80748a6092fSMaxime Coquelin 		if (termios->c_iflag & IGNPAR)
80848a6092fSMaxime Coquelin 			port->ignore_status_mask |= USART_SR_ORE;
80948a6092fSMaxime Coquelin 	}
81048a6092fSMaxime Coquelin 
81148a6092fSMaxime Coquelin 	/* Ignore all characters if CREAD is not set */
81248a6092fSMaxime Coquelin 	if ((termios->c_cflag & CREAD) == 0)
81348a6092fSMaxime Coquelin 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
81448a6092fSMaxime Coquelin 
81534891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
81634891872SAlexandre TORGUE 		cr3 |= USART_CR3_DMAR;
81734891872SAlexandre TORGUE 
8181bcda09dSBich HEMON 	if (rs485conf->flags & SER_RS485_ENABLED) {
8191bcda09dSBich HEMON 		stm32_config_reg_rs485(&cr1, &cr3,
8201bcda09dSBich HEMON 				       rs485conf->delay_rts_before_send,
8211bcda09dSBich HEMON 				       rs485conf->delay_rts_after_send, baud);
8221bcda09dSBich HEMON 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
8231bcda09dSBich HEMON 			cr3 &= ~USART_CR3_DEP;
8241bcda09dSBich HEMON 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
8251bcda09dSBich HEMON 		} else {
8261bcda09dSBich HEMON 			cr3 |= USART_CR3_DEP;
8271bcda09dSBich HEMON 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
8281bcda09dSBich HEMON 		}
8291bcda09dSBich HEMON 
8301bcda09dSBich HEMON 	} else {
8311bcda09dSBich HEMON 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
8321bcda09dSBich HEMON 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
8331bcda09dSBich HEMON 	}
8341bcda09dSBich HEMON 
835ada8618fSAlexandre TORGUE 	writel_relaxed(cr3, port->membase + ofs->cr3);
836ada8618fSAlexandre TORGUE 	writel_relaxed(cr2, port->membase + ofs->cr2);
837ada8618fSAlexandre TORGUE 	writel_relaxed(cr1, port->membase + ofs->cr1);
83848a6092fSMaxime Coquelin 
8391bcda09dSBich HEMON 	stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
84048a6092fSMaxime Coquelin 	spin_unlock_irqrestore(&port->lock, flags);
84148a6092fSMaxime Coquelin }
84248a6092fSMaxime Coquelin 
84348a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port)
84448a6092fSMaxime Coquelin {
84548a6092fSMaxime Coquelin 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
84648a6092fSMaxime Coquelin }
84748a6092fSMaxime Coquelin 
84848a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port)
84948a6092fSMaxime Coquelin {
85048a6092fSMaxime Coquelin }
85148a6092fSMaxime Coquelin 
85248a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port)
85348a6092fSMaxime Coquelin {
85448a6092fSMaxime Coquelin 	return 0;
85548a6092fSMaxime Coquelin }
85648a6092fSMaxime Coquelin 
85748a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags)
85848a6092fSMaxime Coquelin {
85948a6092fSMaxime Coquelin 	if (flags & UART_CONFIG_TYPE)
86048a6092fSMaxime Coquelin 		port->type = PORT_STM32;
86148a6092fSMaxime Coquelin }
86248a6092fSMaxime Coquelin 
86348a6092fSMaxime Coquelin static int
86448a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
86548a6092fSMaxime Coquelin {
86648a6092fSMaxime Coquelin 	/* No user changeable parameters */
86748a6092fSMaxime Coquelin 	return -EINVAL;
86848a6092fSMaxime Coquelin }
86948a6092fSMaxime Coquelin 
87048a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state,
87148a6092fSMaxime Coquelin 		unsigned int oldstate)
87248a6092fSMaxime Coquelin {
87348a6092fSMaxime Coquelin 	struct stm32_port *stm32port = container_of(port,
87448a6092fSMaxime Coquelin 			struct stm32_port, port);
875ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
876ada8618fSAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32port->info->cfg;
87748a6092fSMaxime Coquelin 	unsigned long flags = 0;
87848a6092fSMaxime Coquelin 
87948a6092fSMaxime Coquelin 	switch (state) {
88048a6092fSMaxime Coquelin 	case UART_PM_STATE_ON:
881fb6dcef6SErwan Le Ray 		pm_runtime_get_sync(port->dev);
88248a6092fSMaxime Coquelin 		break;
88348a6092fSMaxime Coquelin 	case UART_PM_STATE_OFF:
88448a6092fSMaxime Coquelin 		spin_lock_irqsave(&port->lock, flags);
885ada8618fSAlexandre TORGUE 		stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
88648a6092fSMaxime Coquelin 		spin_unlock_irqrestore(&port->lock, flags);
887fb6dcef6SErwan Le Ray 		pm_runtime_put_sync(port->dev);
88848a6092fSMaxime Coquelin 		break;
88948a6092fSMaxime Coquelin 	}
89048a6092fSMaxime Coquelin }
89148a6092fSMaxime Coquelin 
89248a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = {
89348a6092fSMaxime Coquelin 	.tx_empty	= stm32_tx_empty,
89448a6092fSMaxime Coquelin 	.set_mctrl	= stm32_set_mctrl,
89548a6092fSMaxime Coquelin 	.get_mctrl	= stm32_get_mctrl,
89648a6092fSMaxime Coquelin 	.stop_tx	= stm32_stop_tx,
89748a6092fSMaxime Coquelin 	.start_tx	= stm32_start_tx,
89848a6092fSMaxime Coquelin 	.throttle	= stm32_throttle,
89948a6092fSMaxime Coquelin 	.unthrottle	= stm32_unthrottle,
90048a6092fSMaxime Coquelin 	.stop_rx	= stm32_stop_rx,
90148a6092fSMaxime Coquelin 	.break_ctl	= stm32_break_ctl,
90248a6092fSMaxime Coquelin 	.startup	= stm32_startup,
90348a6092fSMaxime Coquelin 	.shutdown	= stm32_shutdown,
90448a6092fSMaxime Coquelin 	.set_termios	= stm32_set_termios,
90548a6092fSMaxime Coquelin 	.pm		= stm32_pm,
90648a6092fSMaxime Coquelin 	.type		= stm32_type,
90748a6092fSMaxime Coquelin 	.release_port	= stm32_release_port,
90848a6092fSMaxime Coquelin 	.request_port	= stm32_request_port,
90948a6092fSMaxime Coquelin 	.config_port	= stm32_config_port,
91048a6092fSMaxime Coquelin 	.verify_port	= stm32_verify_port,
91148a6092fSMaxime Coquelin };
91248a6092fSMaxime Coquelin 
91348a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port,
91448a6092fSMaxime Coquelin 			  struct platform_device *pdev)
91548a6092fSMaxime Coquelin {
91648a6092fSMaxime Coquelin 	struct uart_port *port = &stm32port->port;
91748a6092fSMaxime Coquelin 	struct resource *res;
91848a6092fSMaxime Coquelin 	int ret;
91948a6092fSMaxime Coquelin 
92048a6092fSMaxime Coquelin 	port->iotype	= UPIO_MEM;
92148a6092fSMaxime Coquelin 	port->flags	= UPF_BOOT_AUTOCONF;
92248a6092fSMaxime Coquelin 	port->ops	= &stm32_uart_ops;
92348a6092fSMaxime Coquelin 	port->dev	= &pdev->dev;
924d075719eSErwan Le Ray 	port->fifosize	= stm32port->info->cfg.fifosize;
925*9feedaa7SDmitry Safonov 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
9262c58e560SErwan Le Ray 
9272c58e560SErwan Le Ray 	ret = platform_get_irq(pdev, 0);
9281df21786SStephen Boyd 	if (ret <= 0)
9291df21786SStephen Boyd 		return ret ? : -ENODEV;
9302c58e560SErwan Le Ray 	port->irq = ret;
9312c58e560SErwan Le Ray 
9327d8f6861SBich HEMON 	port->rs485_config = stm32_config_rs485;
9337d8f6861SBich HEMON 
9347d8f6861SBich HEMON 	stm32_init_rs485(port, pdev);
9357d8f6861SBich HEMON 
9362c58e560SErwan Le Ray 	if (stm32port->info->cfg.has_wakeup) {
937270e5a74SFabrice Gasnier 		stm32port->wakeirq = platform_get_irq(pdev, 1);
9381df21786SStephen Boyd 		if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO)
9391df21786SStephen Boyd 			return stm32port->wakeirq ? : -ENODEV;
9402c58e560SErwan Le Ray 	}
9412c58e560SErwan Le Ray 
942351a762aSGerald Baeza 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
94348a6092fSMaxime Coquelin 
94448a6092fSMaxime Coquelin 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94548a6092fSMaxime Coquelin 	port->membase = devm_ioremap_resource(&pdev->dev, res);
94648a6092fSMaxime Coquelin 	if (IS_ERR(port->membase))
94748a6092fSMaxime Coquelin 		return PTR_ERR(port->membase);
94848a6092fSMaxime Coquelin 	port->mapbase = res->start;
94948a6092fSMaxime Coquelin 
95048a6092fSMaxime Coquelin 	spin_lock_init(&port->lock);
95148a6092fSMaxime Coquelin 
95248a6092fSMaxime Coquelin 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
95348a6092fSMaxime Coquelin 	if (IS_ERR(stm32port->clk))
95448a6092fSMaxime Coquelin 		return PTR_ERR(stm32port->clk);
95548a6092fSMaxime Coquelin 
95648a6092fSMaxime Coquelin 	/* Ensure that clk rate is correct by enabling the clk */
95748a6092fSMaxime Coquelin 	ret = clk_prepare_enable(stm32port->clk);
95848a6092fSMaxime Coquelin 	if (ret)
95948a6092fSMaxime Coquelin 		return ret;
96048a6092fSMaxime Coquelin 
96148a6092fSMaxime Coquelin 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
962ada80043SFabrice Gasnier 	if (!stm32port->port.uartclk) {
963ada80043SFabrice Gasnier 		clk_disable_unprepare(stm32port->clk);
96448a6092fSMaxime Coquelin 		ret = -EINVAL;
965ada80043SFabrice Gasnier 	}
96648a6092fSMaxime Coquelin 
96748a6092fSMaxime Coquelin 	return ret;
96848a6092fSMaxime Coquelin }
96948a6092fSMaxime Coquelin 
97048a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
97148a6092fSMaxime Coquelin {
97248a6092fSMaxime Coquelin 	struct device_node *np = pdev->dev.of_node;
97348a6092fSMaxime Coquelin 	int id;
97448a6092fSMaxime Coquelin 
97548a6092fSMaxime Coquelin 	if (!np)
97648a6092fSMaxime Coquelin 		return NULL;
97748a6092fSMaxime Coquelin 
97848a6092fSMaxime Coquelin 	id = of_alias_get_id(np, "serial");
979e5707915SGerald Baeza 	if (id < 0) {
980e5707915SGerald Baeza 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
981e5707915SGerald Baeza 		return NULL;
982e5707915SGerald Baeza 	}
98348a6092fSMaxime Coquelin 
98448a6092fSMaxime Coquelin 	if (WARN_ON(id >= STM32_MAX_PORTS))
98548a6092fSMaxime Coquelin 		return NULL;
98648a6092fSMaxime Coquelin 
98748a6092fSMaxime Coquelin 	stm32_ports[id].hw_flow_control = of_property_read_bool(np,
98859bed2dfSAlexandre TORGUE 							"st,hw-flow-ctrl");
98948a6092fSMaxime Coquelin 	stm32_ports[id].port.line = id;
9904cc0ed62SErwan Le Ray 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
991d0a6a7bcSErwan Le Ray 	stm32_ports[id].cr3_irq = 0;
992e5707915SGerald Baeza 	stm32_ports[id].last_res = RX_BUF_L;
99348a6092fSMaxime Coquelin 	return &stm32_ports[id];
99448a6092fSMaxime Coquelin }
99548a6092fSMaxime Coquelin 
99648a6092fSMaxime Coquelin #ifdef CONFIG_OF
99748a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = {
998ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
999ada8618fSAlexandre TORGUE 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1000270e5a74SFabrice Gasnier 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
100148a6092fSMaxime Coquelin 	{},
100248a6092fSMaxime Coquelin };
100348a6092fSMaxime Coquelin 
100448a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match);
100548a6092fSMaxime Coquelin #endif
100648a6092fSMaxime Coquelin 
100734891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
100834891872SAlexandre TORGUE 				 struct platform_device *pdev)
100934891872SAlexandre TORGUE {
101034891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
101134891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
101234891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
101334891872SAlexandre TORGUE 	struct dma_slave_config config;
101434891872SAlexandre TORGUE 	struct dma_async_tx_descriptor *desc = NULL;
101534891872SAlexandre TORGUE 	dma_cookie_t cookie;
101634891872SAlexandre TORGUE 	int ret;
101734891872SAlexandre TORGUE 
101834891872SAlexandre TORGUE 	/* Request DMA RX channel */
101934891872SAlexandre TORGUE 	stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
102034891872SAlexandre TORGUE 	if (!stm32port->rx_ch) {
102134891872SAlexandre TORGUE 		dev_info(dev, "rx dma alloc failed\n");
102234891872SAlexandre TORGUE 		return -ENODEV;
102334891872SAlexandre TORGUE 	}
102434891872SAlexandre TORGUE 	stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
102534891872SAlexandre TORGUE 						 &stm32port->rx_dma_buf,
102634891872SAlexandre TORGUE 						 GFP_KERNEL);
102734891872SAlexandre TORGUE 	if (!stm32port->rx_buf) {
102834891872SAlexandre TORGUE 		ret = -ENOMEM;
102934891872SAlexandre TORGUE 		goto alloc_err;
103034891872SAlexandre TORGUE 	}
103134891872SAlexandre TORGUE 
103234891872SAlexandre TORGUE 	/* Configure DMA channel */
103334891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
10348e5481d9SArnd Bergmann 	config.src_addr = port->mapbase + ofs->rdr;
103534891872SAlexandre TORGUE 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
103634891872SAlexandre TORGUE 
103734891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
103834891872SAlexandre TORGUE 	if (ret < 0) {
103934891872SAlexandre TORGUE 		dev_err(dev, "rx dma channel config failed\n");
104034891872SAlexandre TORGUE 		ret = -ENODEV;
104134891872SAlexandre TORGUE 		goto config_err;
104234891872SAlexandre TORGUE 	}
104334891872SAlexandre TORGUE 
104434891872SAlexandre TORGUE 	/* Prepare a DMA cyclic transaction */
104534891872SAlexandre TORGUE 	desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
104634891872SAlexandre TORGUE 					 stm32port->rx_dma_buf,
104734891872SAlexandre TORGUE 					 RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
104834891872SAlexandre TORGUE 					 DMA_PREP_INTERRUPT);
104934891872SAlexandre TORGUE 	if (!desc) {
105034891872SAlexandre TORGUE 		dev_err(dev, "rx dma prep cyclic failed\n");
105134891872SAlexandre TORGUE 		ret = -ENODEV;
105234891872SAlexandre TORGUE 		goto config_err;
105334891872SAlexandre TORGUE 	}
105434891872SAlexandre TORGUE 
105534891872SAlexandre TORGUE 	/* No callback as dma buffer is drained on usart interrupt */
105634891872SAlexandre TORGUE 	desc->callback = NULL;
105734891872SAlexandre TORGUE 	desc->callback_param = NULL;
105834891872SAlexandre TORGUE 
105934891872SAlexandre TORGUE 	/* Push current DMA transaction in the pending queue */
106034891872SAlexandre TORGUE 	cookie = dmaengine_submit(desc);
106134891872SAlexandre TORGUE 
106234891872SAlexandre TORGUE 	/* Issue pending DMA requests */
106334891872SAlexandre TORGUE 	dma_async_issue_pending(stm32port->rx_ch);
106434891872SAlexandre TORGUE 
106534891872SAlexandre TORGUE 	return 0;
106634891872SAlexandre TORGUE 
106734891872SAlexandre TORGUE config_err:
106834891872SAlexandre TORGUE 	dma_free_coherent(&pdev->dev,
106934891872SAlexandre TORGUE 			  RX_BUF_L, stm32port->rx_buf,
107034891872SAlexandre TORGUE 			  stm32port->rx_dma_buf);
107134891872SAlexandre TORGUE 
107234891872SAlexandre TORGUE alloc_err:
107334891872SAlexandre TORGUE 	dma_release_channel(stm32port->rx_ch);
107434891872SAlexandre TORGUE 	stm32port->rx_ch = NULL;
107534891872SAlexandre TORGUE 
107634891872SAlexandre TORGUE 	return ret;
107734891872SAlexandre TORGUE }
107834891872SAlexandre TORGUE 
107934891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
108034891872SAlexandre TORGUE 				 struct platform_device *pdev)
108134891872SAlexandre TORGUE {
108234891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
108334891872SAlexandre TORGUE 	struct uart_port *port = &stm32port->port;
108434891872SAlexandre TORGUE 	struct device *dev = &pdev->dev;
108534891872SAlexandre TORGUE 	struct dma_slave_config config;
108634891872SAlexandre TORGUE 	int ret;
108734891872SAlexandre TORGUE 
108834891872SAlexandre TORGUE 	stm32port->tx_dma_busy = false;
108934891872SAlexandre TORGUE 
109034891872SAlexandre TORGUE 	/* Request DMA TX channel */
109134891872SAlexandre TORGUE 	stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
109234891872SAlexandre TORGUE 	if (!stm32port->tx_ch) {
109334891872SAlexandre TORGUE 		dev_info(dev, "tx dma alloc failed\n");
109434891872SAlexandre TORGUE 		return -ENODEV;
109534891872SAlexandre TORGUE 	}
109634891872SAlexandre TORGUE 	stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
109734891872SAlexandre TORGUE 						 &stm32port->tx_dma_buf,
109834891872SAlexandre TORGUE 						 GFP_KERNEL);
109934891872SAlexandre TORGUE 	if (!stm32port->tx_buf) {
110034891872SAlexandre TORGUE 		ret = -ENOMEM;
110134891872SAlexandre TORGUE 		goto alloc_err;
110234891872SAlexandre TORGUE 	}
110334891872SAlexandre TORGUE 
110434891872SAlexandre TORGUE 	/* Configure DMA channel */
110534891872SAlexandre TORGUE 	memset(&config, 0, sizeof(config));
11068e5481d9SArnd Bergmann 	config.dst_addr = port->mapbase + ofs->tdr;
110734891872SAlexandre TORGUE 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
110834891872SAlexandre TORGUE 
110934891872SAlexandre TORGUE 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
111034891872SAlexandre TORGUE 	if (ret < 0) {
111134891872SAlexandre TORGUE 		dev_err(dev, "tx dma channel config failed\n");
111234891872SAlexandre TORGUE 		ret = -ENODEV;
111334891872SAlexandre TORGUE 		goto config_err;
111434891872SAlexandre TORGUE 	}
111534891872SAlexandre TORGUE 
111634891872SAlexandre TORGUE 	return 0;
111734891872SAlexandre TORGUE 
111834891872SAlexandre TORGUE config_err:
111934891872SAlexandre TORGUE 	dma_free_coherent(&pdev->dev,
112034891872SAlexandre TORGUE 			  TX_BUF_L, stm32port->tx_buf,
112134891872SAlexandre TORGUE 			  stm32port->tx_dma_buf);
112234891872SAlexandre TORGUE 
112334891872SAlexandre TORGUE alloc_err:
112434891872SAlexandre TORGUE 	dma_release_channel(stm32port->tx_ch);
112534891872SAlexandre TORGUE 	stm32port->tx_ch = NULL;
112634891872SAlexandre TORGUE 
112734891872SAlexandre TORGUE 	return ret;
112834891872SAlexandre TORGUE }
112934891872SAlexandre TORGUE 
113048a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev)
113148a6092fSMaxime Coquelin {
1132ada8618fSAlexandre TORGUE 	const struct of_device_id *match;
113348a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
1134ada8618fSAlexandre TORGUE 	int ret;
113548a6092fSMaxime Coquelin 
113648a6092fSMaxime Coquelin 	stm32port = stm32_of_get_stm32_port(pdev);
113748a6092fSMaxime Coquelin 	if (!stm32port)
113848a6092fSMaxime Coquelin 		return -ENODEV;
113948a6092fSMaxime Coquelin 
1140ada8618fSAlexandre TORGUE 	match = of_match_device(stm32_match, &pdev->dev);
1141ada8618fSAlexandre TORGUE 	if (match && match->data)
1142ada8618fSAlexandre TORGUE 		stm32port->info = (struct stm32_usart_info *)match->data;
1143ada8618fSAlexandre TORGUE 	else
1144ada8618fSAlexandre TORGUE 		return -EINVAL;
1145ada8618fSAlexandre TORGUE 
114648a6092fSMaxime Coquelin 	ret = stm32_init_port(stm32port, pdev);
114748a6092fSMaxime Coquelin 	if (ret)
114848a6092fSMaxime Coquelin 		return ret;
114948a6092fSMaxime Coquelin 
11502c58e560SErwan Le Ray 	if (stm32port->wakeirq > 0) {
1151270e5a74SFabrice Gasnier 		ret = device_init_wakeup(&pdev->dev, true);
115248a6092fSMaxime Coquelin 		if (ret)
1153ada80043SFabrice Gasnier 			goto err_uninit;
11545297f274SErwan Le Ray 
11555297f274SErwan Le Ray 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
11565297f274SErwan Le Ray 						    stm32port->wakeirq);
11575297f274SErwan Le Ray 		if (ret)
11585297f274SErwan Le Ray 			goto err_nowup;
11595297f274SErwan Le Ray 
11605297f274SErwan Le Ray 		device_set_wakeup_enable(&pdev->dev, false);
1161270e5a74SFabrice Gasnier 	}
1162270e5a74SFabrice Gasnier 
1163270e5a74SFabrice Gasnier 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1164270e5a74SFabrice Gasnier 	if (ret)
11655297f274SErwan Le Ray 		goto err_wirq;
116648a6092fSMaxime Coquelin 
116734891872SAlexandre TORGUE 	ret = stm32_of_dma_rx_probe(stm32port, pdev);
116834891872SAlexandre TORGUE 	if (ret)
116934891872SAlexandre TORGUE 		dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
117034891872SAlexandre TORGUE 
117134891872SAlexandre TORGUE 	ret = stm32_of_dma_tx_probe(stm32port, pdev);
117234891872SAlexandre TORGUE 	if (ret)
117334891872SAlexandre TORGUE 		dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
117434891872SAlexandre TORGUE 
117548a6092fSMaxime Coquelin 	platform_set_drvdata(pdev, &stm32port->port);
117648a6092fSMaxime Coquelin 
1177fb6dcef6SErwan Le Ray 	pm_runtime_get_noresume(&pdev->dev);
1178fb6dcef6SErwan Le Ray 	pm_runtime_set_active(&pdev->dev);
1179fb6dcef6SErwan Le Ray 	pm_runtime_enable(&pdev->dev);
1180fb6dcef6SErwan Le Ray 	pm_runtime_put_sync(&pdev->dev);
1181fb6dcef6SErwan Le Ray 
118248a6092fSMaxime Coquelin 	return 0;
1183ada80043SFabrice Gasnier 
11845297f274SErwan Le Ray err_wirq:
11852c58e560SErwan Le Ray 	if (stm32port->wakeirq > 0)
11865297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
11875297f274SErwan Le Ray 
1188270e5a74SFabrice Gasnier err_nowup:
11892c58e560SErwan Le Ray 	if (stm32port->wakeirq > 0)
1190270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
1191270e5a74SFabrice Gasnier 
1192ada80043SFabrice Gasnier err_uninit:
1193ada80043SFabrice Gasnier 	clk_disable_unprepare(stm32port->clk);
1194ada80043SFabrice Gasnier 
1195ada80043SFabrice Gasnier 	return ret;
119648a6092fSMaxime Coquelin }
119748a6092fSMaxime Coquelin 
119848a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev)
119948a6092fSMaxime Coquelin {
120048a6092fSMaxime Coquelin 	struct uart_port *port = platform_get_drvdata(pdev);
1201511c7b1bSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
120234891872SAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1203fb6dcef6SErwan Le Ray 	int err;
1204fb6dcef6SErwan Le Ray 
1205fb6dcef6SErwan Le Ray 	pm_runtime_get_sync(&pdev->dev);
120634891872SAlexandre TORGUE 
120734891872SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
120834891872SAlexandre TORGUE 
120934891872SAlexandre TORGUE 	if (stm32_port->rx_ch)
121034891872SAlexandre TORGUE 		dma_release_channel(stm32_port->rx_ch);
121134891872SAlexandre TORGUE 
121234891872SAlexandre TORGUE 	if (stm32_port->rx_dma_buf)
121334891872SAlexandre TORGUE 		dma_free_coherent(&pdev->dev,
121434891872SAlexandre TORGUE 				  RX_BUF_L, stm32_port->rx_buf,
121534891872SAlexandre TORGUE 				  stm32_port->rx_dma_buf);
121634891872SAlexandre TORGUE 
121734891872SAlexandre TORGUE 	stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
121834891872SAlexandre TORGUE 
121934891872SAlexandre TORGUE 	if (stm32_port->tx_ch)
122034891872SAlexandre TORGUE 		dma_release_channel(stm32_port->tx_ch);
122134891872SAlexandre TORGUE 
122234891872SAlexandre TORGUE 	if (stm32_port->tx_dma_buf)
122334891872SAlexandre TORGUE 		dma_free_coherent(&pdev->dev,
122434891872SAlexandre TORGUE 				  TX_BUF_L, stm32_port->tx_buf,
122534891872SAlexandre TORGUE 				  stm32_port->tx_dma_buf);
1226511c7b1bSAlexandre TORGUE 
12272c58e560SErwan Le Ray 	if (stm32_port->wakeirq > 0) {
12285297f274SErwan Le Ray 		dev_pm_clear_wake_irq(&pdev->dev);
1229270e5a74SFabrice Gasnier 		device_init_wakeup(&pdev->dev, false);
12305297f274SErwan Le Ray 	}
1231270e5a74SFabrice Gasnier 
1232511c7b1bSAlexandre TORGUE 	clk_disable_unprepare(stm32_port->clk);
123348a6092fSMaxime Coquelin 
1234fb6dcef6SErwan Le Ray 	err = uart_remove_one_port(&stm32_usart_driver, port);
1235fb6dcef6SErwan Le Ray 
1236fb6dcef6SErwan Le Ray 	pm_runtime_disable(&pdev->dev);
1237fb6dcef6SErwan Le Ray 	pm_runtime_put_noidle(&pdev->dev);
1238fb6dcef6SErwan Le Ray 
1239fb6dcef6SErwan Le Ray 	return err;
124048a6092fSMaxime Coquelin }
124148a6092fSMaxime Coquelin 
124248a6092fSMaxime Coquelin 
124348a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE
124448a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch)
124548a6092fSMaxime Coquelin {
1246ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1247ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1248ada8618fSAlexandre TORGUE 
1249ada8618fSAlexandre TORGUE 	while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
125048a6092fSMaxime Coquelin 		cpu_relax();
125148a6092fSMaxime Coquelin 
1252ada8618fSAlexandre TORGUE 	writel_relaxed(ch, port->membase + ofs->tdr);
125348a6092fSMaxime Coquelin }
125448a6092fSMaxime Coquelin 
125548a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
125648a6092fSMaxime Coquelin {
125748a6092fSMaxime Coquelin 	struct uart_port *port = &stm32_ports[co->index].port;
1258ada8618fSAlexandre TORGUE 	struct stm32_port *stm32_port = to_stm32_port(port);
1259ada8618fSAlexandre TORGUE 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
126087f1f809SAlexandre TORGUE 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
126148a6092fSMaxime Coquelin 	unsigned long flags;
126248a6092fSMaxime Coquelin 	u32 old_cr1, new_cr1;
126348a6092fSMaxime Coquelin 	int locked = 1;
126448a6092fSMaxime Coquelin 
126548a6092fSMaxime Coquelin 	local_irq_save(flags);
126648a6092fSMaxime Coquelin 	if (port->sysrq)
126748a6092fSMaxime Coquelin 		locked = 0;
126848a6092fSMaxime Coquelin 	else if (oops_in_progress)
126948a6092fSMaxime Coquelin 		locked = spin_trylock(&port->lock);
127048a6092fSMaxime Coquelin 	else
127148a6092fSMaxime Coquelin 		spin_lock(&port->lock);
127248a6092fSMaxime Coquelin 
127387f1f809SAlexandre TORGUE 	/* Save and disable interrupts, enable the transmitter */
1274ada8618fSAlexandre TORGUE 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
127548a6092fSMaxime Coquelin 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
127687f1f809SAlexandre TORGUE 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1277ada8618fSAlexandre TORGUE 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
127848a6092fSMaxime Coquelin 
127948a6092fSMaxime Coquelin 	uart_console_write(port, s, cnt, stm32_console_putchar);
128048a6092fSMaxime Coquelin 
128148a6092fSMaxime Coquelin 	/* Restore interrupt state */
1282ada8618fSAlexandre TORGUE 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
128348a6092fSMaxime Coquelin 
128448a6092fSMaxime Coquelin 	if (locked)
128548a6092fSMaxime Coquelin 		spin_unlock(&port->lock);
128648a6092fSMaxime Coquelin 	local_irq_restore(flags);
128748a6092fSMaxime Coquelin }
128848a6092fSMaxime Coquelin 
128948a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options)
129048a6092fSMaxime Coquelin {
129148a6092fSMaxime Coquelin 	struct stm32_port *stm32port;
129248a6092fSMaxime Coquelin 	int baud = 9600;
129348a6092fSMaxime Coquelin 	int bits = 8;
129448a6092fSMaxime Coquelin 	int parity = 'n';
129548a6092fSMaxime Coquelin 	int flow = 'n';
129648a6092fSMaxime Coquelin 
129748a6092fSMaxime Coquelin 	if (co->index >= STM32_MAX_PORTS)
129848a6092fSMaxime Coquelin 		return -ENODEV;
129948a6092fSMaxime Coquelin 
130048a6092fSMaxime Coquelin 	stm32port = &stm32_ports[co->index];
130148a6092fSMaxime Coquelin 
130248a6092fSMaxime Coquelin 	/*
130348a6092fSMaxime Coquelin 	 * This driver does not support early console initialization
130448a6092fSMaxime Coquelin 	 * (use ARM early printk support instead), so we only expect
130548a6092fSMaxime Coquelin 	 * this to be called during the uart port registration when the
130648a6092fSMaxime Coquelin 	 * driver gets probed and the port should be mapped at that point.
130748a6092fSMaxime Coquelin 	 */
130848a6092fSMaxime Coquelin 	if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
130948a6092fSMaxime Coquelin 		return -ENXIO;
131048a6092fSMaxime Coquelin 
131148a6092fSMaxime Coquelin 	if (options)
131248a6092fSMaxime Coquelin 		uart_parse_options(options, &baud, &parity, &bits, &flow);
131348a6092fSMaxime Coquelin 
131448a6092fSMaxime Coquelin 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
131548a6092fSMaxime Coquelin }
131648a6092fSMaxime Coquelin 
131748a6092fSMaxime Coquelin static struct console stm32_console = {
131848a6092fSMaxime Coquelin 	.name		= STM32_SERIAL_NAME,
131948a6092fSMaxime Coquelin 	.device		= uart_console_device,
132048a6092fSMaxime Coquelin 	.write		= stm32_console_write,
132148a6092fSMaxime Coquelin 	.setup		= stm32_console_setup,
132248a6092fSMaxime Coquelin 	.flags		= CON_PRINTBUFFER,
132348a6092fSMaxime Coquelin 	.index		= -1,
132448a6092fSMaxime Coquelin 	.data		= &stm32_usart_driver,
132548a6092fSMaxime Coquelin };
132648a6092fSMaxime Coquelin 
132748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console)
132848a6092fSMaxime Coquelin 
132948a6092fSMaxime Coquelin #else
133048a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL
133148a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */
133248a6092fSMaxime Coquelin 
133348a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = {
133448a6092fSMaxime Coquelin 	.driver_name	= DRIVER_NAME,
133548a6092fSMaxime Coquelin 	.dev_name	= STM32_SERIAL_NAME,
133648a6092fSMaxime Coquelin 	.major		= 0,
133748a6092fSMaxime Coquelin 	.minor		= 0,
133848a6092fSMaxime Coquelin 	.nr		= STM32_MAX_PORTS,
133948a6092fSMaxime Coquelin 	.cons		= STM32_SERIAL_CONSOLE,
134048a6092fSMaxime Coquelin };
134148a6092fSMaxime Coquelin 
1342fe94347dSErwan Le Ray static void __maybe_unused stm32_serial_enable_wakeup(struct uart_port *port,
1343fe94347dSErwan Le Ray 						      bool enable)
1344270e5a74SFabrice Gasnier {
1345270e5a74SFabrice Gasnier 	struct stm32_port *stm32_port = to_stm32_port(port);
1346270e5a74SFabrice Gasnier 	struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1347270e5a74SFabrice Gasnier 	struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1348270e5a74SFabrice Gasnier 	u32 val;
1349270e5a74SFabrice Gasnier 
13502c58e560SErwan Le Ray 	if (stm32_port->wakeirq <= 0)
1351270e5a74SFabrice Gasnier 		return;
1352270e5a74SFabrice Gasnier 
1353270e5a74SFabrice Gasnier 	if (enable) {
1354270e5a74SFabrice Gasnier 		stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1355270e5a74SFabrice Gasnier 		stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
1356270e5a74SFabrice Gasnier 		val = readl_relaxed(port->membase + ofs->cr3);
1357270e5a74SFabrice Gasnier 		val &= ~USART_CR3_WUS_MASK;
1358270e5a74SFabrice Gasnier 		/* Enable Wake up interrupt from low power on start bit */
1359270e5a74SFabrice Gasnier 		val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
1360270e5a74SFabrice Gasnier 		writel_relaxed(val, port->membase + ofs->cr3);
1361270e5a74SFabrice Gasnier 		stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1362270e5a74SFabrice Gasnier 	} else {
1363270e5a74SFabrice Gasnier 		stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1364270e5a74SFabrice Gasnier 	}
1365270e5a74SFabrice Gasnier }
1366270e5a74SFabrice Gasnier 
1367fe94347dSErwan Le Ray static int __maybe_unused stm32_serial_suspend(struct device *dev)
1368270e5a74SFabrice Gasnier {
1369270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
1370270e5a74SFabrice Gasnier 
1371270e5a74SFabrice Gasnier 	uart_suspend_port(&stm32_usart_driver, port);
1372270e5a74SFabrice Gasnier 
1373270e5a74SFabrice Gasnier 	if (device_may_wakeup(dev))
1374270e5a74SFabrice Gasnier 		stm32_serial_enable_wakeup(port, true);
1375270e5a74SFabrice Gasnier 	else
1376270e5a74SFabrice Gasnier 		stm32_serial_enable_wakeup(port, false);
1377270e5a74SFabrice Gasnier 
137894616d9aSErwan Le Ray 	pinctrl_pm_select_sleep_state(dev);
137994616d9aSErwan Le Ray 
1380270e5a74SFabrice Gasnier 	return 0;
1381270e5a74SFabrice Gasnier }
1382270e5a74SFabrice Gasnier 
1383fe94347dSErwan Le Ray static int __maybe_unused stm32_serial_resume(struct device *dev)
1384270e5a74SFabrice Gasnier {
1385270e5a74SFabrice Gasnier 	struct uart_port *port = dev_get_drvdata(dev);
1386270e5a74SFabrice Gasnier 
138794616d9aSErwan Le Ray 	pinctrl_pm_select_default_state(dev);
138894616d9aSErwan Le Ray 
1389270e5a74SFabrice Gasnier 	if (device_may_wakeup(dev))
1390270e5a74SFabrice Gasnier 		stm32_serial_enable_wakeup(port, false);
1391270e5a74SFabrice Gasnier 
1392270e5a74SFabrice Gasnier 	return uart_resume_port(&stm32_usart_driver, port);
1393270e5a74SFabrice Gasnier }
1394270e5a74SFabrice Gasnier 
1395fb6dcef6SErwan Le Ray static int __maybe_unused stm32_serial_runtime_suspend(struct device *dev)
1396fb6dcef6SErwan Le Ray {
1397fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1398fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1399fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1400fb6dcef6SErwan Le Ray 
1401fb6dcef6SErwan Le Ray 	clk_disable_unprepare(stm32port->clk);
1402fb6dcef6SErwan Le Ray 
1403fb6dcef6SErwan Le Ray 	return 0;
1404fb6dcef6SErwan Le Ray }
1405fb6dcef6SErwan Le Ray 
1406fb6dcef6SErwan Le Ray static int __maybe_unused stm32_serial_runtime_resume(struct device *dev)
1407fb6dcef6SErwan Le Ray {
1408fb6dcef6SErwan Le Ray 	struct uart_port *port = dev_get_drvdata(dev);
1409fb6dcef6SErwan Le Ray 	struct stm32_port *stm32port = container_of(port,
1410fb6dcef6SErwan Le Ray 			struct stm32_port, port);
1411fb6dcef6SErwan Le Ray 
1412fb6dcef6SErwan Le Ray 	return clk_prepare_enable(stm32port->clk);
1413fb6dcef6SErwan Le Ray }
1414fb6dcef6SErwan Le Ray 
1415270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = {
1416fb6dcef6SErwan Le Ray 	SET_RUNTIME_PM_OPS(stm32_serial_runtime_suspend,
1417fb6dcef6SErwan Le Ray 			   stm32_serial_runtime_resume, NULL)
1418270e5a74SFabrice Gasnier 	SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
1419270e5a74SFabrice Gasnier };
1420270e5a74SFabrice Gasnier 
142148a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = {
142248a6092fSMaxime Coquelin 	.probe		= stm32_serial_probe,
142348a6092fSMaxime Coquelin 	.remove		= stm32_serial_remove,
142448a6092fSMaxime Coquelin 	.driver	= {
142548a6092fSMaxime Coquelin 		.name	= DRIVER_NAME,
1426270e5a74SFabrice Gasnier 		.pm	= &stm32_serial_pm_ops,
142748a6092fSMaxime Coquelin 		.of_match_table = of_match_ptr(stm32_match),
142848a6092fSMaxime Coquelin 	},
142948a6092fSMaxime Coquelin };
143048a6092fSMaxime Coquelin 
143148a6092fSMaxime Coquelin static int __init usart_init(void)
143248a6092fSMaxime Coquelin {
143348a6092fSMaxime Coquelin 	static char banner[] __initdata = "STM32 USART driver initialized";
143448a6092fSMaxime Coquelin 	int ret;
143548a6092fSMaxime Coquelin 
143648a6092fSMaxime Coquelin 	pr_info("%s\n", banner);
143748a6092fSMaxime Coquelin 
143848a6092fSMaxime Coquelin 	ret = uart_register_driver(&stm32_usart_driver);
143948a6092fSMaxime Coquelin 	if (ret)
144048a6092fSMaxime Coquelin 		return ret;
144148a6092fSMaxime Coquelin 
144248a6092fSMaxime Coquelin 	ret = platform_driver_register(&stm32_serial_driver);
144348a6092fSMaxime Coquelin 	if (ret)
144448a6092fSMaxime Coquelin 		uart_unregister_driver(&stm32_usart_driver);
144548a6092fSMaxime Coquelin 
144648a6092fSMaxime Coquelin 	return ret;
144748a6092fSMaxime Coquelin }
144848a6092fSMaxime Coquelin 
144948a6092fSMaxime Coquelin static void __exit usart_exit(void)
145048a6092fSMaxime Coquelin {
145148a6092fSMaxime Coquelin 	platform_driver_unregister(&stm32_serial_driver);
145248a6092fSMaxime Coquelin 	uart_unregister_driver(&stm32_usart_driver);
145348a6092fSMaxime Coquelin }
145448a6092fSMaxime Coquelin 
145548a6092fSMaxime Coquelin module_init(usart_init);
145648a6092fSMaxime Coquelin module_exit(usart_exit);
145748a6092fSMaxime Coquelin 
145848a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME);
145948a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
146048a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2");
1461