xref: /openbmc/linux/drivers/tty/serial/stm32-usart.c (revision 2a3bcfe03725472607110507b6860d823e0deb41)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) Maxime Coquelin 2015
4  * Copyright (C) STMicroelectronics SA 2017
5  * Authors:  Maxime Coquelin <mcoquelin.stm32@gmail.com>
6  *	     Gerald Baeza <gerald.baeza@foss.st.com>
7  *	     Erwan Le Ray <erwan.leray@foss.st.com>
8  *
9  * Inspired by st-asc.c from STMicroelectronics (c)
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/console.h>
14 #include <linux/delay.h>
15 #include <linux/dma-direction.h>
16 #include <linux/dmaengine.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/io.h>
19 #include <linux/iopoll.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pm_wakeirq.h>
28 #include <linux/serial_core.h>
29 #include <linux/serial.h>
30 #include <linux/spinlock.h>
31 #include <linux/sysrq.h>
32 #include <linux/tty_flip.h>
33 #include <linux/tty.h>
34 
35 #include "serial_mctrl_gpio.h"
36 #include "stm32-usart.h"
37 
38 static void stm32_usart_stop_tx(struct uart_port *port);
39 static void stm32_usart_transmit_chars(struct uart_port *port);
40 
41 static inline struct stm32_port *to_stm32_port(struct uart_port *port)
42 {
43 	return container_of(port, struct stm32_port, port);
44 }
45 
46 static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
47 {
48 	u32 val;
49 
50 	val = readl_relaxed(port->membase + reg);
51 	val |= bits;
52 	writel_relaxed(val, port->membase + reg);
53 }
54 
55 static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
56 {
57 	u32 val;
58 
59 	val = readl_relaxed(port->membase + reg);
60 	val &= ~bits;
61 	writel_relaxed(val, port->membase + reg);
62 }
63 
64 static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
65 					 u32 delay_DDE, u32 baud)
66 {
67 	u32 rs485_deat_dedt;
68 	u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
69 	bool over8;
70 
71 	*cr3 |= USART_CR3_DEM;
72 	over8 = *cr1 & USART_CR1_OVER8;
73 
74 	if (over8)
75 		rs485_deat_dedt = delay_ADE * baud * 8;
76 	else
77 		rs485_deat_dedt = delay_ADE * baud * 16;
78 
79 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
80 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
81 			  rs485_deat_dedt_max : rs485_deat_dedt;
82 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
83 			   USART_CR1_DEAT_MASK;
84 	*cr1 |= rs485_deat_dedt;
85 
86 	if (over8)
87 		rs485_deat_dedt = delay_DDE * baud * 8;
88 	else
89 		rs485_deat_dedt = delay_DDE * baud * 16;
90 
91 	rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
92 	rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
93 			  rs485_deat_dedt_max : rs485_deat_dedt;
94 	rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
95 			   USART_CR1_DEDT_MASK;
96 	*cr1 |= rs485_deat_dedt;
97 }
98 
99 static int stm32_usart_config_rs485(struct uart_port *port,
100 				    struct serial_rs485 *rs485conf)
101 {
102 	struct stm32_port *stm32_port = to_stm32_port(port);
103 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
104 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
105 	u32 usartdiv, baud, cr1, cr3;
106 	bool over8;
107 
108 	stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
109 
110 	port->rs485 = *rs485conf;
111 
112 	rs485conf->flags |= SER_RS485_RX_DURING_TX;
113 
114 	if (rs485conf->flags & SER_RS485_ENABLED) {
115 		cr1 = readl_relaxed(port->membase + ofs->cr1);
116 		cr3 = readl_relaxed(port->membase + ofs->cr3);
117 		usartdiv = readl_relaxed(port->membase + ofs->brr);
118 		usartdiv = usartdiv & GENMASK(15, 0);
119 		over8 = cr1 & USART_CR1_OVER8;
120 
121 		if (over8)
122 			usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
123 				   << USART_BRR_04_R_SHIFT;
124 
125 		baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
126 		stm32_usart_config_reg_rs485(&cr1, &cr3,
127 					     rs485conf->delay_rts_before_send,
128 					     rs485conf->delay_rts_after_send,
129 					     baud);
130 
131 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
132 			cr3 &= ~USART_CR3_DEP;
133 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
134 		} else {
135 			cr3 |= USART_CR3_DEP;
136 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
137 		}
138 
139 		writel_relaxed(cr3, port->membase + ofs->cr3);
140 		writel_relaxed(cr1, port->membase + ofs->cr1);
141 	} else {
142 		stm32_usart_clr_bits(port, ofs->cr3,
143 				     USART_CR3_DEM | USART_CR3_DEP);
144 		stm32_usart_clr_bits(port, ofs->cr1,
145 				     USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
146 	}
147 
148 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
149 
150 	return 0;
151 }
152 
153 static int stm32_usart_init_rs485(struct uart_port *port,
154 				  struct platform_device *pdev)
155 {
156 	struct serial_rs485 *rs485conf = &port->rs485;
157 
158 	rs485conf->flags = 0;
159 	rs485conf->delay_rts_before_send = 0;
160 	rs485conf->delay_rts_after_send = 0;
161 
162 	if (!pdev->dev.of_node)
163 		return -ENODEV;
164 
165 	return uart_get_rs485_mode(port);
166 }
167 
168 static bool stm32_usart_rx_dma_enabled(struct uart_port *port)
169 {
170 	struct stm32_port *stm32_port = to_stm32_port(port);
171 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
172 
173 	if (!stm32_port->rx_ch)
174 		return false;
175 
176 	return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR);
177 }
178 
179 /* Return true when data is pending (in pio mode), and false when no data is pending. */
180 static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
181 {
182 	struct stm32_port *stm32_port = to_stm32_port(port);
183 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
184 
185 	*sr = readl_relaxed(port->membase + ofs->isr);
186 	/* Get pending characters in RDR or FIFO */
187 	if (*sr & USART_SR_RXNE) {
188 		/* Get all pending characters from the RDR or the FIFO when using interrupts */
189 		if (!stm32_usart_rx_dma_enabled(port))
190 			return true;
191 
192 		/* Handle only RX data errors when using DMA */
193 		if (*sr & USART_SR_ERR_MASK)
194 			return true;
195 	}
196 
197 	return false;
198 }
199 
200 static unsigned long stm32_usart_get_char_pio(struct uart_port *port)
201 {
202 	struct stm32_port *stm32_port = to_stm32_port(port);
203 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
204 	unsigned long c;
205 
206 	c = readl_relaxed(port->membase + ofs->rdr);
207 	/* Apply RDR data mask */
208 	c &= stm32_port->rdr_mask;
209 
210 	return c;
211 }
212 
213 static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
214 {
215 	struct stm32_port *stm32_port = to_stm32_port(port);
216 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
217 	unsigned long c;
218 	unsigned int size = 0;
219 	u32 sr;
220 	char flag;
221 
222 	while (stm32_usart_pending_rx_pio(port, &sr)) {
223 		sr |= USART_SR_DUMMY_RX;
224 		flag = TTY_NORMAL;
225 
226 		/*
227 		 * Status bits has to be cleared before reading the RDR:
228 		 * In FIFO mode, reading the RDR will pop the next data
229 		 * (if any) along with its status bits into the SR.
230 		 * Not doing so leads to misalignement between RDR and SR,
231 		 * and clear status bits of the next rx data.
232 		 *
233 		 * Clear errors flags for stm32f7 and stm32h7 compatible
234 		 * devices. On stm32f4 compatible devices, the error bit is
235 		 * cleared by the sequence [read SR - read DR].
236 		 */
237 		if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
238 			writel_relaxed(sr & USART_SR_ERR_MASK,
239 				       port->membase + ofs->icr);
240 
241 		c = stm32_usart_get_char_pio(port);
242 		port->icount.rx++;
243 		size++;
244 		if (sr & USART_SR_ERR_MASK) {
245 			if (sr & USART_SR_ORE) {
246 				port->icount.overrun++;
247 			} else if (sr & USART_SR_PE) {
248 				port->icount.parity++;
249 			} else if (sr & USART_SR_FE) {
250 				/* Break detection if character is null */
251 				if (!c) {
252 					port->icount.brk++;
253 					if (uart_handle_break(port))
254 						continue;
255 				} else {
256 					port->icount.frame++;
257 				}
258 			}
259 
260 			sr &= port->read_status_mask;
261 
262 			if (sr & USART_SR_PE) {
263 				flag = TTY_PARITY;
264 			} else if (sr & USART_SR_FE) {
265 				if (!c)
266 					flag = TTY_BREAK;
267 				else
268 					flag = TTY_FRAME;
269 			}
270 		}
271 
272 		if (uart_prepare_sysrq_char(port, c))
273 			continue;
274 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
275 	}
276 
277 	return size;
278 }
279 
280 static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
281 {
282 	struct stm32_port *stm32_port = to_stm32_port(port);
283 	struct tty_port *ttyport = &stm32_port->port.state->port;
284 	unsigned char *dma_start;
285 	int dma_count, i;
286 
287 	dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
288 
289 	/*
290 	 * Apply rdr_mask on buffer in order to mask parity bit.
291 	 * This loop is useless in cs8 mode because DMA copies only
292 	 * 8 bits and already ignores parity bit.
293 	 */
294 	if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
295 		for (i = 0; i < dma_size; i++)
296 			*(dma_start + i) &= stm32_port->rdr_mask;
297 
298 	dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
299 	port->icount.rx += dma_count;
300 	if (dma_count != dma_size)
301 		port->icount.buf_overrun++;
302 	stm32_port->last_res -= dma_count;
303 	if (stm32_port->last_res == 0)
304 		stm32_port->last_res = RX_BUF_L;
305 }
306 
307 static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
308 {
309 	struct stm32_port *stm32_port = to_stm32_port(port);
310 	unsigned int dma_size, size = 0;
311 
312 	/* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
313 	if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
314 		/* Conditional first part: from last_res to end of DMA buffer */
315 		dma_size = stm32_port->last_res;
316 		stm32_usart_push_buffer_dma(port, dma_size);
317 		size = dma_size;
318 	}
319 
320 	dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
321 	stm32_usart_push_buffer_dma(port, dma_size);
322 	size += dma_size;
323 
324 	return size;
325 }
326 
327 static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
328 {
329 	struct stm32_port *stm32_port = to_stm32_port(port);
330 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
331 	enum dma_status rx_dma_status;
332 	u32 sr;
333 	unsigned int size = 0;
334 
335 	if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) {
336 		rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
337 						    stm32_port->rx_ch->cookie,
338 						    &stm32_port->rx_dma_state);
339 		if (rx_dma_status == DMA_IN_PROGRESS) {
340 			/* Empty DMA buffer */
341 			size = stm32_usart_receive_chars_dma(port);
342 			sr = readl_relaxed(port->membase + ofs->isr);
343 			if (sr & USART_SR_ERR_MASK) {
344 				/* Disable DMA request line */
345 				stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
346 
347 				/* Switch to PIO mode to handle the errors */
348 				size += stm32_usart_receive_chars_pio(port);
349 
350 				/* Switch back to DMA mode */
351 				stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
352 			}
353 		} else {
354 			/* Disable RX DMA */
355 			dmaengine_terminate_async(stm32_port->rx_ch);
356 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
357 			/* Fall back to interrupt mode */
358 			dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
359 			size = stm32_usart_receive_chars_pio(port);
360 		}
361 	} else {
362 		size = stm32_usart_receive_chars_pio(port);
363 	}
364 
365 	return size;
366 }
367 
368 static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
369 {
370 	dmaengine_terminate_async(stm32_port->tx_ch);
371 	stm32_port->tx_dma_busy = false;
372 }
373 
374 static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
375 {
376 	/*
377 	 * We cannot use the function "dmaengine_tx_status" to know the
378 	 * status of DMA. This function does not show if the "dma complete"
379 	 * callback of the DMA transaction has been called. So we prefer
380 	 * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
381 	 * same time.
382 	 */
383 	return stm32_port->tx_dma_busy;
384 }
385 
386 static bool stm32_usart_tx_dma_enabled(struct stm32_port *stm32_port)
387 {
388 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
389 
390 	return !!(readl_relaxed(stm32_port->port.membase + ofs->cr3) & USART_CR3_DMAT);
391 }
392 
393 static void stm32_usart_tx_dma_complete(void *arg)
394 {
395 	struct uart_port *port = arg;
396 	struct stm32_port *stm32port = to_stm32_port(port);
397 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
398 	unsigned long flags;
399 
400 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
401 	stm32_usart_tx_dma_terminate(stm32port);
402 
403 	/* Let's see if we have pending data to send */
404 	spin_lock_irqsave(&port->lock, flags);
405 	stm32_usart_transmit_chars(port);
406 	spin_unlock_irqrestore(&port->lock, flags);
407 }
408 
409 static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
410 {
411 	struct stm32_port *stm32_port = to_stm32_port(port);
412 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
413 
414 	/*
415 	 * Enables TX FIFO threashold irq when FIFO is enabled,
416 	 * or TX empty irq when FIFO is disabled
417 	 */
418 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
419 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
420 	else
421 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
422 }
423 
424 static void stm32_usart_rx_dma_complete(void *arg)
425 {
426 	struct uart_port *port = arg;
427 	struct tty_port *tport = &port->state->port;
428 	unsigned int size;
429 	unsigned long flags;
430 
431 	spin_lock_irqsave(&port->lock, flags);
432 	size = stm32_usart_receive_chars(port, false);
433 	uart_unlock_and_check_sysrq_irqrestore(port, flags);
434 	if (size)
435 		tty_flip_buffer_push(tport);
436 }
437 
438 static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
439 {
440 	struct stm32_port *stm32_port = to_stm32_port(port);
441 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
442 
443 	if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
444 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
445 	else
446 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
447 }
448 
449 static void stm32_usart_transmit_chars_pio(struct uart_port *port)
450 {
451 	struct stm32_port *stm32_port = to_stm32_port(port);
452 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
453 	struct circ_buf *xmit = &port->state->xmit;
454 
455 	if (stm32_usart_tx_dma_enabled(stm32_port))
456 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
457 
458 	while (!uart_circ_empty(xmit)) {
459 		/* Check that TDR is empty before filling FIFO */
460 		if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
461 			break;
462 		writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
463 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
464 		port->icount.tx++;
465 	}
466 
467 	/* rely on TXE irq (mask or unmask) for sending remaining data */
468 	if (uart_circ_empty(xmit))
469 		stm32_usart_tx_interrupt_disable(port);
470 	else
471 		stm32_usart_tx_interrupt_enable(port);
472 }
473 
474 static void stm32_usart_transmit_chars_dma(struct uart_port *port)
475 {
476 	struct stm32_port *stm32port = to_stm32_port(port);
477 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
478 	struct circ_buf *xmit = &port->state->xmit;
479 	struct dma_async_tx_descriptor *desc = NULL;
480 	unsigned int count, i;
481 
482 	if (stm32_usart_tx_dma_started(stm32port)) {
483 		if (!stm32_usart_tx_dma_enabled(stm32port))
484 			stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
485 		return;
486 	}
487 
488 	count = uart_circ_chars_pending(xmit);
489 
490 	if (count > TX_BUF_L)
491 		count = TX_BUF_L;
492 
493 	if (xmit->tail < xmit->head) {
494 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
495 	} else {
496 		size_t one = UART_XMIT_SIZE - xmit->tail;
497 		size_t two;
498 
499 		if (one > count)
500 			one = count;
501 		two = count - one;
502 
503 		memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
504 		if (two)
505 			memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
506 	}
507 
508 	desc = dmaengine_prep_slave_single(stm32port->tx_ch,
509 					   stm32port->tx_dma_buf,
510 					   count,
511 					   DMA_MEM_TO_DEV,
512 					   DMA_PREP_INTERRUPT);
513 
514 	if (!desc)
515 		goto fallback_err;
516 
517 	/*
518 	 * Set "tx_dma_busy" flag. This flag will be released when
519 	 * dmaengine_terminate_async will be called. This flag helps
520 	 * transmit_chars_dma not to start another DMA transaction
521 	 * if the callback of the previous is not yet called.
522 	 */
523 	stm32port->tx_dma_busy = true;
524 
525 	desc->callback = stm32_usart_tx_dma_complete;
526 	desc->callback_param = port;
527 
528 	/* Push current DMA TX transaction in the pending queue */
529 	if (dma_submit_error(dmaengine_submit(desc))) {
530 		/* dma no yet started, safe to free resources */
531 		stm32_usart_tx_dma_terminate(stm32port);
532 		goto fallback_err;
533 	}
534 
535 	/* Issue pending DMA TX requests */
536 	dma_async_issue_pending(stm32port->tx_ch);
537 
538 	stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
539 
540 	xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
541 	port->icount.tx += count;
542 	return;
543 
544 fallback_err:
545 	for (i = count; i > 0; i--)
546 		stm32_usart_transmit_chars_pio(port);
547 }
548 
549 static void stm32_usart_transmit_chars(struct uart_port *port)
550 {
551 	struct stm32_port *stm32_port = to_stm32_port(port);
552 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
553 	struct circ_buf *xmit = &port->state->xmit;
554 
555 	if (port->x_char) {
556 		if (stm32_usart_tx_dma_started(stm32_port) &&
557 		    stm32_usart_tx_dma_enabled(stm32_port))
558 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
559 		writel_relaxed(port->x_char, port->membase + ofs->tdr);
560 		port->x_char = 0;
561 		port->icount.tx++;
562 		if (stm32_usart_tx_dma_started(stm32_port))
563 			stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT);
564 		return;
565 	}
566 
567 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
568 		stm32_usart_tx_interrupt_disable(port);
569 		return;
570 	}
571 
572 	if (ofs->icr == UNDEF_REG)
573 		stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
574 	else
575 		writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
576 
577 	if (stm32_port->tx_ch)
578 		stm32_usart_transmit_chars_dma(port);
579 	else
580 		stm32_usart_transmit_chars_pio(port);
581 
582 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
583 		uart_write_wakeup(port);
584 
585 	if (uart_circ_empty(xmit))
586 		stm32_usart_tx_interrupt_disable(port);
587 }
588 
589 static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
590 {
591 	struct uart_port *port = ptr;
592 	struct tty_port *tport = &port->state->port;
593 	struct stm32_port *stm32_port = to_stm32_port(port);
594 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
595 	u32 sr;
596 	unsigned int size;
597 
598 	sr = readl_relaxed(port->membase + ofs->isr);
599 
600 	if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
601 		writel_relaxed(USART_ICR_RTOCF,
602 			       port->membase + ofs->icr);
603 
604 	if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
605 		/* Clear wake up flag and disable wake up interrupt */
606 		writel_relaxed(USART_ICR_WUCF,
607 			       port->membase + ofs->icr);
608 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
609 		if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
610 			pm_wakeup_event(tport->tty->dev, 0);
611 	}
612 
613 	/*
614 	 * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
615 	 * line has been masked by HW and rx data are stacking in FIFO.
616 	 */
617 	if (!stm32_port->throttled) {
618 		if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) ||
619 		    ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) {
620 			spin_lock(&port->lock);
621 			size = stm32_usart_receive_chars(port, false);
622 			uart_unlock_and_check_sysrq(port);
623 			if (size)
624 				tty_flip_buffer_push(tport);
625 		}
626 	}
627 
628 	if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
629 		spin_lock(&port->lock);
630 		stm32_usart_transmit_chars(port);
631 		spin_unlock(&port->lock);
632 	}
633 
634 	if (stm32_usart_rx_dma_enabled(port))
635 		return IRQ_WAKE_THREAD;
636 	else
637 		return IRQ_HANDLED;
638 }
639 
640 static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr)
641 {
642 	struct uart_port *port = ptr;
643 	struct tty_port *tport = &port->state->port;
644 	struct stm32_port *stm32_port = to_stm32_port(port);
645 	unsigned int size;
646 	unsigned long flags;
647 
648 	/* Receiver timeout irq for DMA RX */
649 	if (!stm32_port->throttled) {
650 		spin_lock_irqsave(&port->lock, flags);
651 		size = stm32_usart_receive_chars(port, false);
652 		uart_unlock_and_check_sysrq_irqrestore(port, flags);
653 		if (size)
654 			tty_flip_buffer_push(tport);
655 	}
656 
657 	return IRQ_HANDLED;
658 }
659 
660 static unsigned int stm32_usart_tx_empty(struct uart_port *port)
661 {
662 	struct stm32_port *stm32_port = to_stm32_port(port);
663 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
664 
665 	if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
666 		return TIOCSER_TEMT;
667 
668 	return 0;
669 }
670 
671 static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
672 {
673 	struct stm32_port *stm32_port = to_stm32_port(port);
674 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
675 
676 	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
677 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
678 	else
679 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
680 
681 	mctrl_gpio_set(stm32_port->gpios, mctrl);
682 }
683 
684 static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
685 {
686 	struct stm32_port *stm32_port = to_stm32_port(port);
687 	unsigned int ret;
688 
689 	/* This routine is used to get signals of: DCD, DSR, RI, and CTS */
690 	ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
691 
692 	return mctrl_gpio_get(stm32_port->gpios, &ret);
693 }
694 
695 static void stm32_usart_enable_ms(struct uart_port *port)
696 {
697 	mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
698 }
699 
700 static void stm32_usart_disable_ms(struct uart_port *port)
701 {
702 	mctrl_gpio_disable_ms(to_stm32_port(port)->gpios);
703 }
704 
705 /* Transmit stop */
706 static void stm32_usart_stop_tx(struct uart_port *port)
707 {
708 	struct stm32_port *stm32_port = to_stm32_port(port);
709 	struct serial_rs485 *rs485conf = &port->rs485;
710 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
711 
712 	stm32_usart_tx_interrupt_disable(port);
713 	if (stm32_usart_tx_dma_started(stm32_port) && stm32_usart_tx_dma_enabled(stm32_port))
714 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
715 
716 	if (rs485conf->flags & SER_RS485_ENABLED) {
717 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
718 			mctrl_gpio_set(stm32_port->gpios,
719 					stm32_port->port.mctrl & ~TIOCM_RTS);
720 		} else {
721 			mctrl_gpio_set(stm32_port->gpios,
722 					stm32_port->port.mctrl | TIOCM_RTS);
723 		}
724 	}
725 }
726 
727 /* There are probably characters waiting to be transmitted. */
728 static void stm32_usart_start_tx(struct uart_port *port)
729 {
730 	struct stm32_port *stm32_port = to_stm32_port(port);
731 	struct serial_rs485 *rs485conf = &port->rs485;
732 	struct circ_buf *xmit = &port->state->xmit;
733 
734 	if (uart_circ_empty(xmit))
735 		return;
736 
737 	if (rs485conf->flags & SER_RS485_ENABLED) {
738 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
739 			mctrl_gpio_set(stm32_port->gpios,
740 					stm32_port->port.mctrl | TIOCM_RTS);
741 		} else {
742 			mctrl_gpio_set(stm32_port->gpios,
743 					stm32_port->port.mctrl & ~TIOCM_RTS);
744 		}
745 	}
746 
747 	stm32_usart_transmit_chars(port);
748 }
749 
750 /* Flush the transmit buffer. */
751 static void stm32_usart_flush_buffer(struct uart_port *port)
752 {
753 	struct stm32_port *stm32_port = to_stm32_port(port);
754 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
755 
756 	if (stm32_port->tx_ch) {
757 		stm32_usart_tx_dma_terminate(stm32_port);
758 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
759 	}
760 }
761 
762 /* Throttle the remote when input buffer is about to overflow. */
763 static void stm32_usart_throttle(struct uart_port *port)
764 {
765 	struct stm32_port *stm32_port = to_stm32_port(port);
766 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
767 	unsigned long flags;
768 
769 	spin_lock_irqsave(&port->lock, flags);
770 
771 	/*
772 	 * Disable DMA request line if enabled, so the RX data gets queued into the FIFO.
773 	 * Hardware flow control is triggered when RX FIFO is full.
774 	 */
775 	if (stm32_usart_rx_dma_enabled(port))
776 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
777 
778 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
779 	if (stm32_port->cr3_irq)
780 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
781 
782 	stm32_port->throttled = true;
783 	spin_unlock_irqrestore(&port->lock, flags);
784 }
785 
786 /* Unthrottle the remote, the input buffer can now accept data. */
787 static void stm32_usart_unthrottle(struct uart_port *port)
788 {
789 	struct stm32_port *stm32_port = to_stm32_port(port);
790 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
791 	unsigned long flags;
792 
793 	spin_lock_irqsave(&port->lock, flags);
794 	stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
795 	if (stm32_port->cr3_irq)
796 		stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
797 
798 	/*
799 	 * Switch back to DMA mode (re-enable DMA request line).
800 	 * Hardware flow control is stopped when FIFO is not full any more.
801 	 */
802 	if (stm32_port->rx_ch)
803 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
804 
805 	stm32_port->throttled = false;
806 	spin_unlock_irqrestore(&port->lock, flags);
807 }
808 
809 /* Receive stop */
810 static void stm32_usart_stop_rx(struct uart_port *port)
811 {
812 	struct stm32_port *stm32_port = to_stm32_port(port);
813 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
814 
815 	/* Disable DMA request line. */
816 	if (stm32_port->rx_ch)
817 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
818 
819 	stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
820 	if (stm32_port->cr3_irq)
821 		stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
822 }
823 
824 /* Handle breaks - ignored by us */
825 static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
826 {
827 }
828 
829 static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port)
830 {
831 	struct stm32_port *stm32_port = to_stm32_port(port);
832 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
833 	struct dma_async_tx_descriptor *desc;
834 	int ret;
835 
836 	stm32_port->last_res = RX_BUF_L;
837 	/* Prepare a DMA cyclic transaction */
838 	desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
839 					 stm32_port->rx_dma_buf,
840 					 RX_BUF_L, RX_BUF_P,
841 					 DMA_DEV_TO_MEM,
842 					 DMA_PREP_INTERRUPT);
843 	if (!desc) {
844 		dev_err(port->dev, "rx dma prep cyclic failed\n");
845 		return -ENODEV;
846 	}
847 
848 	desc->callback = stm32_usart_rx_dma_complete;
849 	desc->callback_param = port;
850 
851 	/* Push current DMA transaction in the pending queue */
852 	ret = dma_submit_error(dmaengine_submit(desc));
853 	if (ret) {
854 		dmaengine_terminate_sync(stm32_port->rx_ch);
855 		return ret;
856 	}
857 
858 	/* Issue pending DMA requests */
859 	dma_async_issue_pending(stm32_port->rx_ch);
860 
861 	/*
862 	 * DMA request line not re-enabled at resume when port is throttled.
863 	 * It will be re-enabled by unthrottle ops.
864 	 */
865 	if (!stm32_port->throttled)
866 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
867 
868 	return 0;
869 }
870 
871 static int stm32_usart_startup(struct uart_port *port)
872 {
873 	struct stm32_port *stm32_port = to_stm32_port(port);
874 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
875 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
876 	const char *name = to_platform_device(port->dev)->name;
877 	u32 val;
878 	int ret;
879 
880 	ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
881 				   stm32_usart_threaded_interrupt,
882 				   IRQF_ONESHOT | IRQF_NO_SUSPEND,
883 				   name, port);
884 	if (ret)
885 		return ret;
886 
887 	if (stm32_port->swap) {
888 		val = readl_relaxed(port->membase + ofs->cr2);
889 		val |= USART_CR2_SWAP;
890 		writel_relaxed(val, port->membase + ofs->cr2);
891 	}
892 
893 	/* RX FIFO Flush */
894 	if (ofs->rqr != UNDEF_REG)
895 		writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
896 
897 	if (stm32_port->rx_ch) {
898 		ret = stm32_usart_start_rx_dma_cyclic(port);
899 		if (ret) {
900 			free_irq(port->irq, port);
901 			return ret;
902 		}
903 	}
904 
905 	/* RX enabling */
906 	val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
907 	stm32_usart_set_bits(port, ofs->cr1, val);
908 
909 	return 0;
910 }
911 
912 static void stm32_usart_shutdown(struct uart_port *port)
913 {
914 	struct stm32_port *stm32_port = to_stm32_port(port);
915 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
916 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
917 	u32 val, isr;
918 	int ret;
919 
920 	if (stm32_usart_tx_dma_enabled(stm32_port))
921 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
922 
923 	if (stm32_usart_tx_dma_started(stm32_port))
924 		stm32_usart_tx_dma_terminate(stm32_port);
925 
926 	/* Disable modem control interrupts */
927 	stm32_usart_disable_ms(port);
928 
929 	val = USART_CR1_TXEIE | USART_CR1_TE;
930 	val |= stm32_port->cr1_irq | USART_CR1_RE;
931 	val |= BIT(cfg->uart_enable_bit);
932 	if (stm32_port->fifoen)
933 		val |= USART_CR1_FIFOEN;
934 
935 	ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
936 					 isr, (isr & USART_SR_TC),
937 					 10, 100000);
938 
939 	/* Send the TC error message only when ISR_TC is not set */
940 	if (ret)
941 		dev_err(port->dev, "Transmission is not complete\n");
942 
943 	/* Disable RX DMA. */
944 	if (stm32_port->rx_ch)
945 		dmaengine_terminate_async(stm32_port->rx_ch);
946 
947 	/* flush RX & TX FIFO */
948 	if (ofs->rqr != UNDEF_REG)
949 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
950 			       port->membase + ofs->rqr);
951 
952 	stm32_usart_clr_bits(port, ofs->cr1, val);
953 
954 	free_irq(port->irq, port);
955 }
956 
957 static void stm32_usart_set_termios(struct uart_port *port,
958 				    struct ktermios *termios,
959 				    struct ktermios *old)
960 {
961 	struct stm32_port *stm32_port = to_stm32_port(port);
962 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
963 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
964 	struct serial_rs485 *rs485conf = &port->rs485;
965 	unsigned int baud, bits;
966 	u32 usartdiv, mantissa, fraction, oversampling;
967 	tcflag_t cflag = termios->c_cflag;
968 	u32 cr1, cr2, cr3, isr;
969 	unsigned long flags;
970 	int ret;
971 
972 	if (!stm32_port->hw_flow_control)
973 		cflag &= ~CRTSCTS;
974 
975 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
976 
977 	spin_lock_irqsave(&port->lock, flags);
978 
979 	ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
980 						isr,
981 						(isr & USART_SR_TC),
982 						10, 100000);
983 
984 	/* Send the TC error message only when ISR_TC is not set. */
985 	if (ret)
986 		dev_err(port->dev, "Transmission is not complete\n");
987 
988 	/* Stop serial port and reset value */
989 	writel_relaxed(0, port->membase + ofs->cr1);
990 
991 	/* flush RX & TX FIFO */
992 	if (ofs->rqr != UNDEF_REG)
993 		writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
994 			       port->membase + ofs->rqr);
995 
996 	cr1 = USART_CR1_TE | USART_CR1_RE;
997 	if (stm32_port->fifoen)
998 		cr1 |= USART_CR1_FIFOEN;
999 	cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
1000 
1001 	/* Tx and RX FIFO configuration */
1002 	cr3 = readl_relaxed(port->membase + ofs->cr3);
1003 	cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
1004 	if (stm32_port->fifoen) {
1005 		if (stm32_port->txftcfg >= 0)
1006 			cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
1007 		if (stm32_port->rxftcfg >= 0)
1008 			cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
1009 	}
1010 
1011 	if (cflag & CSTOPB)
1012 		cr2 |= USART_CR2_STOP_2B;
1013 
1014 	bits = tty_get_char_size(cflag);
1015 	stm32_port->rdr_mask = (BIT(bits) - 1);
1016 
1017 	if (cflag & PARENB) {
1018 		bits++;
1019 		cr1 |= USART_CR1_PCE;
1020 	}
1021 
1022 	/*
1023 	 * Word length configuration:
1024 	 * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
1025 	 * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
1026 	 * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
1027 	 * M0 and M1 already cleared by cr1 initialization.
1028 	 */
1029 	if (bits == 9)
1030 		cr1 |= USART_CR1_M0;
1031 	else if ((bits == 7) && cfg->has_7bits_data)
1032 		cr1 |= USART_CR1_M1;
1033 	else if (bits != 8)
1034 		dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
1035 			, bits);
1036 
1037 	if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
1038 				       (stm32_port->fifoen &&
1039 					stm32_port->rxftcfg >= 0))) {
1040 		if (cflag & CSTOPB)
1041 			bits = bits + 3; /* 1 start bit + 2 stop bits */
1042 		else
1043 			bits = bits + 2; /* 1 start bit + 1 stop bit */
1044 
1045 		/* RX timeout irq to occur after last stop bit + bits */
1046 		stm32_port->cr1_irq = USART_CR1_RTOIE;
1047 		writel_relaxed(bits, port->membase + ofs->rtor);
1048 		cr2 |= USART_CR2_RTOEN;
1049 		/*
1050 		 * Enable fifo threshold irq in two cases, either when there is no DMA, or when
1051 		 * wake up over usart, from low power until the DMA gets re-enabled by resume.
1052 		 */
1053 		stm32_port->cr3_irq =  USART_CR3_RXFTIE;
1054 	}
1055 
1056 	cr1 |= stm32_port->cr1_irq;
1057 	cr3 |= stm32_port->cr3_irq;
1058 
1059 	if (cflag & PARODD)
1060 		cr1 |= USART_CR1_PS;
1061 
1062 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
1063 	if (cflag & CRTSCTS) {
1064 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
1065 		cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
1066 	}
1067 
1068 	usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
1069 
1070 	/*
1071 	 * The USART supports 16 or 8 times oversampling.
1072 	 * By default we prefer 16 times oversampling, so that the receiver
1073 	 * has a better tolerance to clock deviations.
1074 	 * 8 times oversampling is only used to achieve higher speeds.
1075 	 */
1076 	if (usartdiv < 16) {
1077 		oversampling = 8;
1078 		cr1 |= USART_CR1_OVER8;
1079 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
1080 	} else {
1081 		oversampling = 16;
1082 		cr1 &= ~USART_CR1_OVER8;
1083 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
1084 	}
1085 
1086 	mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
1087 	fraction = usartdiv % oversampling;
1088 	writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
1089 
1090 	uart_update_timeout(port, cflag, baud);
1091 
1092 	port->read_status_mask = USART_SR_ORE;
1093 	if (termios->c_iflag & INPCK)
1094 		port->read_status_mask |= USART_SR_PE | USART_SR_FE;
1095 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1096 		port->read_status_mask |= USART_SR_FE;
1097 
1098 	/* Characters to ignore */
1099 	port->ignore_status_mask = 0;
1100 	if (termios->c_iflag & IGNPAR)
1101 		port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
1102 	if (termios->c_iflag & IGNBRK) {
1103 		port->ignore_status_mask |= USART_SR_FE;
1104 		/*
1105 		 * If we're ignoring parity and break indicators,
1106 		 * ignore overruns too (for real raw support).
1107 		 */
1108 		if (termios->c_iflag & IGNPAR)
1109 			port->ignore_status_mask |= USART_SR_ORE;
1110 	}
1111 
1112 	/* Ignore all characters if CREAD is not set */
1113 	if ((termios->c_cflag & CREAD) == 0)
1114 		port->ignore_status_mask |= USART_SR_DUMMY_RX;
1115 
1116 	if (stm32_port->rx_ch) {
1117 		/*
1118 		 * Setup DMA to collect only valid data and enable error irqs.
1119 		 * This also enables break reception when using DMA.
1120 		 */
1121 		cr1 |= USART_CR1_PEIE;
1122 		cr3 |= USART_CR3_EIE;
1123 		cr3 |= USART_CR3_DMAR;
1124 		cr3 |= USART_CR3_DDRE;
1125 	}
1126 
1127 	if (rs485conf->flags & SER_RS485_ENABLED) {
1128 		stm32_usart_config_reg_rs485(&cr1, &cr3,
1129 					     rs485conf->delay_rts_before_send,
1130 					     rs485conf->delay_rts_after_send,
1131 					     baud);
1132 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
1133 			cr3 &= ~USART_CR3_DEP;
1134 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
1135 		} else {
1136 			cr3 |= USART_CR3_DEP;
1137 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
1138 		}
1139 
1140 	} else {
1141 		cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
1142 		cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
1143 	}
1144 
1145 	/* Configure wake up from low power on start bit detection */
1146 	if (stm32_port->wakeup_src) {
1147 		cr3 &= ~USART_CR3_WUS_MASK;
1148 		cr3 |= USART_CR3_WUS_START_BIT;
1149 	}
1150 
1151 	writel_relaxed(cr3, port->membase + ofs->cr3);
1152 	writel_relaxed(cr2, port->membase + ofs->cr2);
1153 	writel_relaxed(cr1, port->membase + ofs->cr1);
1154 
1155 	stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1156 	spin_unlock_irqrestore(&port->lock, flags);
1157 
1158 	/* Handle modem control interrupts */
1159 	if (UART_ENABLE_MS(port, termios->c_cflag))
1160 		stm32_usart_enable_ms(port);
1161 	else
1162 		stm32_usart_disable_ms(port);
1163 }
1164 
1165 static const char *stm32_usart_type(struct uart_port *port)
1166 {
1167 	return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
1168 }
1169 
1170 static void stm32_usart_release_port(struct uart_port *port)
1171 {
1172 }
1173 
1174 static int stm32_usart_request_port(struct uart_port *port)
1175 {
1176 	return 0;
1177 }
1178 
1179 static void stm32_usart_config_port(struct uart_port *port, int flags)
1180 {
1181 	if (flags & UART_CONFIG_TYPE)
1182 		port->type = PORT_STM32;
1183 }
1184 
1185 static int
1186 stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
1187 {
1188 	/* No user changeable parameters */
1189 	return -EINVAL;
1190 }
1191 
1192 static void stm32_usart_pm(struct uart_port *port, unsigned int state,
1193 			   unsigned int oldstate)
1194 {
1195 	struct stm32_port *stm32port = container_of(port,
1196 			struct stm32_port, port);
1197 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1198 	const struct stm32_usart_config *cfg = &stm32port->info->cfg;
1199 	unsigned long flags;
1200 
1201 	switch (state) {
1202 	case UART_PM_STATE_ON:
1203 		pm_runtime_get_sync(port->dev);
1204 		break;
1205 	case UART_PM_STATE_OFF:
1206 		spin_lock_irqsave(&port->lock, flags);
1207 		stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1208 		spin_unlock_irqrestore(&port->lock, flags);
1209 		pm_runtime_put_sync(port->dev);
1210 		break;
1211 	}
1212 }
1213 
1214 static const struct uart_ops stm32_uart_ops = {
1215 	.tx_empty	= stm32_usart_tx_empty,
1216 	.set_mctrl	= stm32_usart_set_mctrl,
1217 	.get_mctrl	= stm32_usart_get_mctrl,
1218 	.stop_tx	= stm32_usart_stop_tx,
1219 	.start_tx	= stm32_usart_start_tx,
1220 	.throttle	= stm32_usart_throttle,
1221 	.unthrottle	= stm32_usart_unthrottle,
1222 	.stop_rx	= stm32_usart_stop_rx,
1223 	.enable_ms	= stm32_usart_enable_ms,
1224 	.break_ctl	= stm32_usart_break_ctl,
1225 	.startup	= stm32_usart_startup,
1226 	.shutdown	= stm32_usart_shutdown,
1227 	.flush_buffer	= stm32_usart_flush_buffer,
1228 	.set_termios	= stm32_usart_set_termios,
1229 	.pm		= stm32_usart_pm,
1230 	.type		= stm32_usart_type,
1231 	.release_port	= stm32_usart_release_port,
1232 	.request_port	= stm32_usart_request_port,
1233 	.config_port	= stm32_usart_config_port,
1234 	.verify_port	= stm32_usart_verify_port,
1235 };
1236 
1237 /*
1238  * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG)
1239  * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case,
1240  * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE.
1241  * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1.
1242  */
1243 static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 };
1244 
1245 static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p,
1246 				  int *ftcfg)
1247 {
1248 	u32 bytes, i;
1249 
1250 	/* DT option to get RX & TX FIFO threshold (default to 8 bytes) */
1251 	if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
1252 		bytes = 8;
1253 
1254 	for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++)
1255 		if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes)
1256 			break;
1257 	if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
1258 		i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
1259 
1260 	dev_dbg(&pdev->dev, "%s set to %d bytes\n", p,
1261 		stm32h7_usart_fifo_thresh_cfg[i]);
1262 
1263 	/* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */
1264 	if (i)
1265 		*ftcfg = i - 1;
1266 	else
1267 		*ftcfg = -EINVAL;
1268 }
1269 
1270 static void stm32_usart_deinit_port(struct stm32_port *stm32port)
1271 {
1272 	clk_disable_unprepare(stm32port->clk);
1273 }
1274 
1275 static int stm32_usart_init_port(struct stm32_port *stm32port,
1276 				 struct platform_device *pdev)
1277 {
1278 	struct uart_port *port = &stm32port->port;
1279 	struct resource *res;
1280 	int ret, irq;
1281 
1282 	irq = platform_get_irq(pdev, 0);
1283 	if (irq < 0)
1284 		return irq;
1285 
1286 	port->iotype	= UPIO_MEM;
1287 	port->flags	= UPF_BOOT_AUTOCONF;
1288 	port->ops	= &stm32_uart_ops;
1289 	port->dev	= &pdev->dev;
1290 	port->fifosize	= stm32port->info->cfg.fifosize;
1291 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
1292 	port->irq = irq;
1293 	port->rs485_config = stm32_usart_config_rs485;
1294 
1295 	ret = stm32_usart_init_rs485(port, pdev);
1296 	if (ret)
1297 		return ret;
1298 
1299 	stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
1300 		of_property_read_bool(pdev->dev.of_node, "wakeup-source");
1301 
1302 	stm32port->swap = stm32port->info->cfg.has_swap &&
1303 		of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
1304 
1305 	stm32port->fifoen = stm32port->info->cfg.has_fifo;
1306 	if (stm32port->fifoen) {
1307 		stm32_usart_get_ftcfg(pdev, "rx-threshold",
1308 				      &stm32port->rxftcfg);
1309 		stm32_usart_get_ftcfg(pdev, "tx-threshold",
1310 				      &stm32port->txftcfg);
1311 	}
1312 
1313 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1314 	if (IS_ERR(port->membase))
1315 		return PTR_ERR(port->membase);
1316 	port->mapbase = res->start;
1317 
1318 	spin_lock_init(&port->lock);
1319 
1320 	stm32port->clk = devm_clk_get(&pdev->dev, NULL);
1321 	if (IS_ERR(stm32port->clk))
1322 		return PTR_ERR(stm32port->clk);
1323 
1324 	/* Ensure that clk rate is correct by enabling the clk */
1325 	ret = clk_prepare_enable(stm32port->clk);
1326 	if (ret)
1327 		return ret;
1328 
1329 	stm32port->port.uartclk = clk_get_rate(stm32port->clk);
1330 	if (!stm32port->port.uartclk) {
1331 		ret = -EINVAL;
1332 		goto err_clk;
1333 	}
1334 
1335 	stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
1336 	if (IS_ERR(stm32port->gpios)) {
1337 		ret = PTR_ERR(stm32port->gpios);
1338 		goto err_clk;
1339 	}
1340 
1341 	/*
1342 	 * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
1343 	 * properties should not be specified.
1344 	 */
1345 	if (stm32port->hw_flow_control) {
1346 		if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
1347 		    mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
1348 			dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
1349 			ret = -EINVAL;
1350 			goto err_clk;
1351 		}
1352 	}
1353 
1354 	return ret;
1355 
1356 err_clk:
1357 	clk_disable_unprepare(stm32port->clk);
1358 
1359 	return ret;
1360 }
1361 
1362 static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
1363 {
1364 	struct device_node *np = pdev->dev.of_node;
1365 	int id;
1366 
1367 	if (!np)
1368 		return NULL;
1369 
1370 	id = of_alias_get_id(np, "serial");
1371 	if (id < 0) {
1372 		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
1373 		return NULL;
1374 	}
1375 
1376 	if (WARN_ON(id >= STM32_MAX_PORTS))
1377 		return NULL;
1378 
1379 	stm32_ports[id].hw_flow_control =
1380 		of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
1381 		of_property_read_bool (np, "uart-has-rtscts");
1382 	stm32_ports[id].port.line = id;
1383 	stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
1384 	stm32_ports[id].cr3_irq = 0;
1385 	stm32_ports[id].last_res = RX_BUF_L;
1386 	return &stm32_ports[id];
1387 }
1388 
1389 #ifdef CONFIG_OF
1390 static const struct of_device_id stm32_match[] = {
1391 	{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
1392 	{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
1393 	{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
1394 	{},
1395 };
1396 
1397 MODULE_DEVICE_TABLE(of, stm32_match);
1398 #endif
1399 
1400 static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
1401 					 struct platform_device *pdev)
1402 {
1403 	if (stm32port->rx_buf)
1404 		dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
1405 				  stm32port->rx_dma_buf);
1406 }
1407 
1408 static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
1409 				       struct platform_device *pdev)
1410 {
1411 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1412 	struct uart_port *port = &stm32port->port;
1413 	struct device *dev = &pdev->dev;
1414 	struct dma_slave_config config;
1415 	int ret;
1416 
1417 	/*
1418 	 * Using DMA and threaded handler for the console could lead to
1419 	 * deadlocks.
1420 	 */
1421 	if (uart_console(port))
1422 		return -ENODEV;
1423 
1424 	stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
1425 					       &stm32port->rx_dma_buf,
1426 					       GFP_KERNEL);
1427 	if (!stm32port->rx_buf)
1428 		return -ENOMEM;
1429 
1430 	/* Configure DMA channel */
1431 	memset(&config, 0, sizeof(config));
1432 	config.src_addr = port->mapbase + ofs->rdr;
1433 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1434 
1435 	ret = dmaengine_slave_config(stm32port->rx_ch, &config);
1436 	if (ret < 0) {
1437 		dev_err(dev, "rx dma channel config failed\n");
1438 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
1439 		return ret;
1440 	}
1441 
1442 	return 0;
1443 }
1444 
1445 static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
1446 					 struct platform_device *pdev)
1447 {
1448 	if (stm32port->tx_buf)
1449 		dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
1450 				  stm32port->tx_dma_buf);
1451 }
1452 
1453 static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
1454 				       struct platform_device *pdev)
1455 {
1456 	const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
1457 	struct uart_port *port = &stm32port->port;
1458 	struct device *dev = &pdev->dev;
1459 	struct dma_slave_config config;
1460 	int ret;
1461 
1462 	stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
1463 					       &stm32port->tx_dma_buf,
1464 					       GFP_KERNEL);
1465 	if (!stm32port->tx_buf)
1466 		return -ENOMEM;
1467 
1468 	/* Configure DMA channel */
1469 	memset(&config, 0, sizeof(config));
1470 	config.dst_addr = port->mapbase + ofs->tdr;
1471 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1472 
1473 	ret = dmaengine_slave_config(stm32port->tx_ch, &config);
1474 	if (ret < 0) {
1475 		dev_err(dev, "tx dma channel config failed\n");
1476 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1477 		return ret;
1478 	}
1479 
1480 	return 0;
1481 }
1482 
1483 static int stm32_usart_serial_probe(struct platform_device *pdev)
1484 {
1485 	struct stm32_port *stm32port;
1486 	int ret;
1487 
1488 	stm32port = stm32_usart_of_get_port(pdev);
1489 	if (!stm32port)
1490 		return -ENODEV;
1491 
1492 	stm32port->info = of_device_get_match_data(&pdev->dev);
1493 	if (!stm32port->info)
1494 		return -EINVAL;
1495 
1496 	ret = stm32_usart_init_port(stm32port, pdev);
1497 	if (ret)
1498 		return ret;
1499 
1500 	if (stm32port->wakeup_src) {
1501 		device_set_wakeup_capable(&pdev->dev, true);
1502 		ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
1503 		if (ret)
1504 			goto err_deinit_port;
1505 	}
1506 
1507 	stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
1508 	if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) {
1509 		ret = -EPROBE_DEFER;
1510 		goto err_wakeirq;
1511 	}
1512 	/* Fall back in interrupt mode for any non-deferral error */
1513 	if (IS_ERR(stm32port->rx_ch))
1514 		stm32port->rx_ch = NULL;
1515 
1516 	stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
1517 	if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
1518 		ret = -EPROBE_DEFER;
1519 		goto err_dma_rx;
1520 	}
1521 	/* Fall back in interrupt mode for any non-deferral error */
1522 	if (IS_ERR(stm32port->tx_ch))
1523 		stm32port->tx_ch = NULL;
1524 
1525 	if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
1526 		/* Fall back in interrupt mode */
1527 		dma_release_channel(stm32port->rx_ch);
1528 		stm32port->rx_ch = NULL;
1529 	}
1530 
1531 	if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
1532 		/* Fall back in interrupt mode */
1533 		dma_release_channel(stm32port->tx_ch);
1534 		stm32port->tx_ch = NULL;
1535 	}
1536 
1537 	if (!stm32port->rx_ch)
1538 		dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
1539 	if (!stm32port->tx_ch)
1540 		dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
1541 
1542 	platform_set_drvdata(pdev, &stm32port->port);
1543 
1544 	pm_runtime_get_noresume(&pdev->dev);
1545 	pm_runtime_set_active(&pdev->dev);
1546 	pm_runtime_enable(&pdev->dev);
1547 
1548 	ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
1549 	if (ret)
1550 		goto err_port;
1551 
1552 	pm_runtime_put_sync(&pdev->dev);
1553 
1554 	return 0;
1555 
1556 err_port:
1557 	pm_runtime_disable(&pdev->dev);
1558 	pm_runtime_set_suspended(&pdev->dev);
1559 	pm_runtime_put_noidle(&pdev->dev);
1560 
1561 	if (stm32port->tx_ch) {
1562 		stm32_usart_of_dma_tx_remove(stm32port, pdev);
1563 		dma_release_channel(stm32port->tx_ch);
1564 	}
1565 
1566 	if (stm32port->rx_ch)
1567 		stm32_usart_of_dma_rx_remove(stm32port, pdev);
1568 
1569 err_dma_rx:
1570 	if (stm32port->rx_ch)
1571 		dma_release_channel(stm32port->rx_ch);
1572 
1573 err_wakeirq:
1574 	if (stm32port->wakeup_src)
1575 		dev_pm_clear_wake_irq(&pdev->dev);
1576 
1577 err_deinit_port:
1578 	if (stm32port->wakeup_src)
1579 		device_set_wakeup_capable(&pdev->dev, false);
1580 
1581 	stm32_usart_deinit_port(stm32port);
1582 
1583 	return ret;
1584 }
1585 
1586 static int stm32_usart_serial_remove(struct platform_device *pdev)
1587 {
1588 	struct uart_port *port = platform_get_drvdata(pdev);
1589 	struct stm32_port *stm32_port = to_stm32_port(port);
1590 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1591 	int err;
1592 	u32 cr3;
1593 
1594 	pm_runtime_get_sync(&pdev->dev);
1595 	err = uart_remove_one_port(&stm32_usart_driver, port);
1596 	if (err)
1597 		return(err);
1598 
1599 	pm_runtime_disable(&pdev->dev);
1600 	pm_runtime_set_suspended(&pdev->dev);
1601 	pm_runtime_put_noidle(&pdev->dev);
1602 
1603 	stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
1604 	cr3 = readl_relaxed(port->membase + ofs->cr3);
1605 	cr3 &= ~USART_CR3_EIE;
1606 	cr3 &= ~USART_CR3_DMAR;
1607 	cr3 &= ~USART_CR3_DDRE;
1608 	writel_relaxed(cr3, port->membase + ofs->cr3);
1609 
1610 	if (stm32_port->tx_ch) {
1611 		stm32_usart_of_dma_tx_remove(stm32_port, pdev);
1612 		dma_release_channel(stm32_port->tx_ch);
1613 	}
1614 
1615 	if (stm32_port->rx_ch) {
1616 		stm32_usart_of_dma_rx_remove(stm32_port, pdev);
1617 		dma_release_channel(stm32_port->rx_ch);
1618 	}
1619 
1620 	stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
1621 
1622 	if (stm32_port->wakeup_src) {
1623 		dev_pm_clear_wake_irq(&pdev->dev);
1624 		device_init_wakeup(&pdev->dev, false);
1625 	}
1626 
1627 	stm32_usart_deinit_port(stm32_port);
1628 
1629 	return 0;
1630 }
1631 
1632 #ifdef CONFIG_SERIAL_STM32_CONSOLE
1633 static void stm32_usart_console_putchar(struct uart_port *port, int ch)
1634 {
1635 	struct stm32_port *stm32_port = to_stm32_port(port);
1636 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1637 
1638 	while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
1639 		cpu_relax();
1640 
1641 	writel_relaxed(ch, port->membase + ofs->tdr);
1642 }
1643 
1644 static void stm32_usart_console_write(struct console *co, const char *s,
1645 				      unsigned int cnt)
1646 {
1647 	struct uart_port *port = &stm32_ports[co->index].port;
1648 	struct stm32_port *stm32_port = to_stm32_port(port);
1649 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1650 	const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
1651 	unsigned long flags;
1652 	u32 old_cr1, new_cr1;
1653 	int locked = 1;
1654 
1655 	if (oops_in_progress)
1656 		locked = spin_trylock_irqsave(&port->lock, flags);
1657 	else
1658 		spin_lock_irqsave(&port->lock, flags);
1659 
1660 	/* Save and disable interrupts, enable the transmitter */
1661 	old_cr1 = readl_relaxed(port->membase + ofs->cr1);
1662 	new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
1663 	new_cr1 |=  USART_CR1_TE | BIT(cfg->uart_enable_bit);
1664 	writel_relaxed(new_cr1, port->membase + ofs->cr1);
1665 
1666 	uart_console_write(port, s, cnt, stm32_usart_console_putchar);
1667 
1668 	/* Restore interrupt state */
1669 	writel_relaxed(old_cr1, port->membase + ofs->cr1);
1670 
1671 	if (locked)
1672 		spin_unlock_irqrestore(&port->lock, flags);
1673 }
1674 
1675 static int stm32_usart_console_setup(struct console *co, char *options)
1676 {
1677 	struct stm32_port *stm32port;
1678 	int baud = 9600;
1679 	int bits = 8;
1680 	int parity = 'n';
1681 	int flow = 'n';
1682 
1683 	if (co->index >= STM32_MAX_PORTS)
1684 		return -ENODEV;
1685 
1686 	stm32port = &stm32_ports[co->index];
1687 
1688 	/*
1689 	 * This driver does not support early console initialization
1690 	 * (use ARM early printk support instead), so we only expect
1691 	 * this to be called during the uart port registration when the
1692 	 * driver gets probed and the port should be mapped at that point.
1693 	 */
1694 	if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
1695 		return -ENXIO;
1696 
1697 	if (options)
1698 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1699 
1700 	return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
1701 }
1702 
1703 static struct console stm32_console = {
1704 	.name		= STM32_SERIAL_NAME,
1705 	.device		= uart_console_device,
1706 	.write		= stm32_usart_console_write,
1707 	.setup		= stm32_usart_console_setup,
1708 	.flags		= CON_PRINTBUFFER,
1709 	.index		= -1,
1710 	.data		= &stm32_usart_driver,
1711 };
1712 
1713 #define STM32_SERIAL_CONSOLE (&stm32_console)
1714 
1715 #else
1716 #define STM32_SERIAL_CONSOLE NULL
1717 #endif /* CONFIG_SERIAL_STM32_CONSOLE */
1718 
1719 static struct uart_driver stm32_usart_driver = {
1720 	.driver_name	= DRIVER_NAME,
1721 	.dev_name	= STM32_SERIAL_NAME,
1722 	.major		= 0,
1723 	.minor		= 0,
1724 	.nr		= STM32_MAX_PORTS,
1725 	.cons		= STM32_SERIAL_CONSOLE,
1726 };
1727 
1728 static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
1729 						       bool enable)
1730 {
1731 	struct stm32_port *stm32_port = to_stm32_port(port);
1732 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
1733 	struct tty_port *tport = &port->state->port;
1734 	int ret;
1735 	unsigned int size;
1736 	unsigned long flags;
1737 
1738 	if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
1739 		return 0;
1740 
1741 	/*
1742 	 * Enable low-power wake-up and wake-up irq if argument is set to
1743 	 * "enable", disable low-power wake-up and wake-up irq otherwise
1744 	 */
1745 	if (enable) {
1746 		stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
1747 		stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
1748 
1749 		/*
1750 		 * When DMA is used for reception, it must be disabled before
1751 		 * entering low-power mode and re-enabled when exiting from
1752 		 * low-power mode.
1753 		 */
1754 		if (stm32_port->rx_ch) {
1755 			spin_lock_irqsave(&port->lock, flags);
1756 			/* Avoid race with RX IRQ when DMAR is cleared */
1757 			stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
1758 			/* Poll data from DMA RX buffer if any */
1759 			size = stm32_usart_receive_chars(port, true);
1760 			dmaengine_terminate_async(stm32_port->rx_ch);
1761 			uart_unlock_and_check_sysrq_irqrestore(port, flags);
1762 			if (size)
1763 				tty_flip_buffer_push(tport);
1764 		}
1765 
1766 		/* Poll data from RX FIFO if any */
1767 		stm32_usart_receive_chars(port, false);
1768 	} else {
1769 		if (stm32_port->rx_ch) {
1770 			ret = stm32_usart_start_rx_dma_cyclic(port);
1771 			if (ret)
1772 				return ret;
1773 		}
1774 
1775 		stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
1776 		stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
1777 	}
1778 
1779 	return 0;
1780 }
1781 
1782 static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
1783 {
1784 	struct uart_port *port = dev_get_drvdata(dev);
1785 	int ret;
1786 
1787 	uart_suspend_port(&stm32_usart_driver, port);
1788 
1789 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
1790 		ret = stm32_usart_serial_en_wakeup(port, true);
1791 		if (ret)
1792 			return ret;
1793 	}
1794 
1795 	/*
1796 	 * When "no_console_suspend" is enabled, keep the pinctrl default state
1797 	 * and rely on bootloader stage to restore this state upon resume.
1798 	 * Otherwise, apply the idle or sleep states depending on wakeup
1799 	 * capabilities.
1800 	 */
1801 	if (console_suspend_enabled || !uart_console(port)) {
1802 		if (device_may_wakeup(dev) || device_wakeup_path(dev))
1803 			pinctrl_pm_select_idle_state(dev);
1804 		else
1805 			pinctrl_pm_select_sleep_state(dev);
1806 	}
1807 
1808 	return 0;
1809 }
1810 
1811 static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
1812 {
1813 	struct uart_port *port = dev_get_drvdata(dev);
1814 	int ret;
1815 
1816 	pinctrl_pm_select_default_state(dev);
1817 
1818 	if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
1819 		ret = stm32_usart_serial_en_wakeup(port, false);
1820 		if (ret)
1821 			return ret;
1822 	}
1823 
1824 	return uart_resume_port(&stm32_usart_driver, port);
1825 }
1826 
1827 static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
1828 {
1829 	struct uart_port *port = dev_get_drvdata(dev);
1830 	struct stm32_port *stm32port = container_of(port,
1831 			struct stm32_port, port);
1832 
1833 	clk_disable_unprepare(stm32port->clk);
1834 
1835 	return 0;
1836 }
1837 
1838 static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
1839 {
1840 	struct uart_port *port = dev_get_drvdata(dev);
1841 	struct stm32_port *stm32port = container_of(port,
1842 			struct stm32_port, port);
1843 
1844 	return clk_prepare_enable(stm32port->clk);
1845 }
1846 
1847 static const struct dev_pm_ops stm32_serial_pm_ops = {
1848 	SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
1849 			   stm32_usart_runtime_resume, NULL)
1850 	SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
1851 				stm32_usart_serial_resume)
1852 };
1853 
1854 static struct platform_driver stm32_serial_driver = {
1855 	.probe		= stm32_usart_serial_probe,
1856 	.remove		= stm32_usart_serial_remove,
1857 	.driver	= {
1858 		.name	= DRIVER_NAME,
1859 		.pm	= &stm32_serial_pm_ops,
1860 		.of_match_table = of_match_ptr(stm32_match),
1861 	},
1862 };
1863 
1864 static int __init stm32_usart_init(void)
1865 {
1866 	static char banner[] __initdata = "STM32 USART driver initialized";
1867 	int ret;
1868 
1869 	pr_info("%s\n", banner);
1870 
1871 	ret = uart_register_driver(&stm32_usart_driver);
1872 	if (ret)
1873 		return ret;
1874 
1875 	ret = platform_driver_register(&stm32_serial_driver);
1876 	if (ret)
1877 		uart_unregister_driver(&stm32_usart_driver);
1878 
1879 	return ret;
1880 }
1881 
1882 static void __exit stm32_usart_exit(void)
1883 {
1884 	platform_driver_unregister(&stm32_serial_driver);
1885 	uart_unregister_driver(&stm32_usart_driver);
1886 }
1887 
1888 module_init(stm32_usart_init);
1889 module_exit(stm32_usart_exit);
1890 
1891 MODULE_ALIAS("platform:" DRIVER_NAME);
1892 MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
1893 MODULE_LICENSE("GPL v2");
1894