xref: /linux/drivers/tty/serial/8250/8250_omap.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 8250-core based driver for the OMAP internal UART
4  *
5  * based on omap-serial.c, Copyright (C) 2010 Texas Instruments.
6  *
7  * Copyright (C) 2014 Sebastian Andrzej Siewior
8  *
9  */
10 
11 #include <linux/atomic.h>
12 #include <linux/clk.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/serial_8250.h>
18 #include <linux/serial_reg.h>
19 #include <linux/tty_flip.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/of_irq.h>
24 #include <linux/delay.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/console.h>
27 #include <linux/pm_qos.h>
28 #include <linux/pm_wakeirq.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/sys_soc.h>
31 #include <linux/reboot.h>
32 #include <linux/pinctrl/consumer.h>
33 
34 #include "8250.h"
35 
36 #define DEFAULT_CLK_SPEED	48000000
37 #define OMAP_UART_REGSHIFT	2
38 
39 #define UART_ERRATA_i202_MDR1_ACCESS	(1 << 0)
40 #define OMAP_UART_WER_HAS_TX_WAKEUP	(1 << 1)
41 #define OMAP_DMA_TX_KICK		(1 << 2)
42 /*
43  * See Advisory 21 in AM437x errata SPRZ408B, updated April 2015.
44  * The same errata is applicable to AM335x and DRA7x processors too.
45  */
46 #define UART_ERRATA_CLOCK_DISABLE	(1 << 3)
47 #define	UART_HAS_EFR2			BIT(4)
48 #define UART_HAS_RHR_IT_DIS		BIT(5)
49 #define UART_RX_TIMEOUT_QUIRK		BIT(6)
50 #define UART_HAS_NATIVE_RS485		BIT(7)
51 
52 #define OMAP_UART_FCR_RX_TRIG		6
53 #define OMAP_UART_FCR_TX_TRIG		4
54 
55 /* SCR register bitmasks */
56 #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK	(1 << 7)
57 #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK	(1 << 6)
58 #define OMAP_UART_SCR_TX_EMPTY			(1 << 3)
59 #define OMAP_UART_SCR_DMAMODE_MASK		(3 << 1)
60 #define OMAP_UART_SCR_DMAMODE_1			(1 << 1)
61 #define OMAP_UART_SCR_DMAMODE_CTL		(1 << 0)
62 
63 /* MVR register bitmasks */
64 #define OMAP_UART_MVR_SCHEME_SHIFT	30
65 #define OMAP_UART_LEGACY_MVR_MAJ_MASK	0xf0
66 #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT	4
67 #define OMAP_UART_LEGACY_MVR_MIN_MASK	0x0f
68 #define OMAP_UART_MVR_MAJ_MASK		0x700
69 #define OMAP_UART_MVR_MAJ_SHIFT		8
70 #define OMAP_UART_MVR_MIN_MASK		0x3f
71 
72 /* SYSC register bitmasks */
73 #define OMAP_UART_SYSC_SOFTRESET	(1 << 1)
74 
75 /* SYSS register bitmasks */
76 #define OMAP_UART_SYSS_RESETDONE	(1 << 0)
77 
78 #define UART_TI752_TLR_TX	0
79 #define UART_TI752_TLR_RX	4
80 
81 #define TRIGGER_TLR_MASK(x)	((x & 0x3c) >> 2)
82 #define TRIGGER_FCR_MASK(x)	(x & 3)
83 
84 /* Enable XON/XOFF flow control on output */
85 #define OMAP_UART_SW_TX		0x08
86 /* Enable XON/XOFF flow control on input */
87 #define OMAP_UART_SW_RX		0x02
88 
89 #define OMAP_UART_WER_MOD_WKUP	0x7f
90 #define OMAP_UART_TX_WAKEUP_EN	(1 << 7)
91 
92 #define TX_TRIGGER	1
93 #define RX_TRIGGER	48
94 
95 #define OMAP_UART_TCR_RESTORE(x)	((x / 4) << 4)
96 #define OMAP_UART_TCR_HALT(x)		((x / 4) << 0)
97 
98 #define UART_BUILD_REVISION(x, y)	(((x) << 8) | (y))
99 
100 #define OMAP_UART_REV_46 0x0406
101 #define OMAP_UART_REV_52 0x0502
102 #define OMAP_UART_REV_63 0x0603
103 
104 /* Resume register */
105 #define UART_OMAP_RESUME		0x0B
106 
107 /* Interrupt Enable Register 2 */
108 #define UART_OMAP_IER2			0x1B
109 #define UART_OMAP_IER2_RHR_IT_DIS	BIT(2)
110 
111 /* Mode Definition Register 3 */
112 #define UART_OMAP_MDR3			0x20
113 #define UART_OMAP_MDR3_DIR_POL		BIT(3)
114 #define UART_OMAP_MDR3_DIR_EN		BIT(4)
115 
116 /* Enhanced features register 2 */
117 #define UART_OMAP_EFR2			0x23
118 #define UART_OMAP_EFR2_TIMEOUT_BEHAVE	BIT(6)
119 
120 /* RX FIFO occupancy indicator */
121 #define UART_OMAP_RX_LVL		0x19
122 
123 /* Timeout low and High */
124 #define UART_OMAP_TO_L                 0x26
125 #define UART_OMAP_TO_H                 0x27
126 struct omap8250_priv {
127 	void __iomem *membase;
128 	struct uart_8250_port *uport;
129 	u8 habit;
130 	u8 mdr1;
131 	u8 mdr3;
132 	u8 efr;
133 	u8 scr;
134 	u8 wer;
135 	u8 xon;
136 	u8 xoff;
137 	u8 delayed_restore;
138 	u16 quot;
139 
140 	u8 tx_trigger;
141 	u8 rx_trigger;
142 	atomic_t active;
143 	bool is_suspending;
144 	int wakeirq;
145 	u32 latency;
146 	u32 calc_latency;
147 	struct pm_qos_request pm_qos_request;
148 	struct work_struct qos_work;
149 	struct uart_8250_dma omap8250_dma;
150 	spinlock_t rx_dma_lock;
151 	bool rx_dma_broken;
152 	bool throttled;
153 
154 	struct pinctrl *pinctrl;
155 	struct pinctrl_state *pinctrl_wakeup;
156 };
157 
158 struct omap8250_dma_params {
159 	u32 rx_size;
160 	u8 rx_trigger;
161 	u8 tx_trigger;
162 };
163 
164 struct omap8250_platdata {
165 	struct omap8250_dma_params *dma_params;
166 	u8 habit;
167 };
168 
169 #ifdef CONFIG_SERIAL_8250_DMA
170 static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
171 #else
172 static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
173 #endif
174 
175 static u32 uart_read(struct omap8250_priv *priv, u32 reg)
176 {
177 	return readl(priv->membase + (reg << OMAP_UART_REGSHIFT));
178 }
179 
180 /*
181  * Called on runtime PM resume path from omap8250_restore_regs(), and
182  * omap8250_set_mctrl().
183  */
184 static void __omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
185 {
186 	struct uart_8250_port *up = up_to_u8250p(port);
187 	struct omap8250_priv *priv = port->private_data;
188 	u8 lcr;
189 
190 	serial8250_do_set_mctrl(port, mctrl);
191 
192 	if (!mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS)) {
193 		/*
194 		 * Turn off autoRTS if RTS is lowered and restore autoRTS
195 		 * setting if RTS is raised
196 		 */
197 		lcr = serial_in(up, UART_LCR);
198 		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
199 		if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
200 			priv->efr |= UART_EFR_RTS;
201 		else
202 			priv->efr &= ~UART_EFR_RTS;
203 		serial_out(up, UART_EFR, priv->efr);
204 		serial_out(up, UART_LCR, lcr);
205 	}
206 }
207 
208 static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
209 {
210 	int err;
211 
212 	err = pm_runtime_resume_and_get(port->dev);
213 	if (err)
214 		return;
215 
216 	__omap8250_set_mctrl(port, mctrl);
217 
218 	pm_runtime_mark_last_busy(port->dev);
219 	pm_runtime_put_autosuspend(port->dev);
220 }
221 
222 /*
223  * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
224  * The access to uart register after MDR1 Access
225  * causes UART to corrupt data.
226  *
227  * Need a delay =
228  * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
229  * give 10 times as much
230  */
231 static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
232 				     struct omap8250_priv *priv)
233 {
234 	serial_out(up, UART_OMAP_MDR1, priv->mdr1);
235 	udelay(2);
236 	serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
237 			UART_FCR_CLEAR_RCVR);
238 }
239 
240 static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
241 				  struct omap8250_priv *priv)
242 {
243 	unsigned int uartclk = port->uartclk;
244 	unsigned int div_13, div_16;
245 	unsigned int abs_d13, abs_d16;
246 
247 	div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
248 	div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
249 
250 	if (!div_13)
251 		div_13 = 1;
252 	if (!div_16)
253 		div_16 = 1;
254 
255 	abs_d13 = abs(baud - uartclk / 13 / div_13);
256 	abs_d16 = abs(baud - uartclk / 16 / div_16);
257 
258 	if (abs_d13 >= abs_d16) {
259 		priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
260 		priv->quot = div_16;
261 	} else {
262 		priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
263 		priv->quot = div_13;
264 	}
265 }
266 
267 static void omap8250_update_scr(struct uart_8250_port *up,
268 				struct omap8250_priv *priv)
269 {
270 	u8 old_scr;
271 
272 	old_scr = serial_in(up, UART_OMAP_SCR);
273 	if (old_scr == priv->scr)
274 		return;
275 
276 	/*
277 	 * The manual recommends not to enable the DMA mode selector in the SCR
278 	 * (instead of the FCR) register _and_ selecting the DMA mode as one
279 	 * register write because this may lead to malfunction.
280 	 */
281 	if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
282 		serial_out(up, UART_OMAP_SCR,
283 			   priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
284 	serial_out(up, UART_OMAP_SCR, priv->scr);
285 }
286 
287 static void omap8250_update_mdr1(struct uart_8250_port *up,
288 				 struct omap8250_priv *priv)
289 {
290 	if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
291 		omap_8250_mdr1_errataset(up, priv);
292 	else
293 		serial_out(up, UART_OMAP_MDR1, priv->mdr1);
294 }
295 
296 static void omap8250_restore_regs(struct uart_8250_port *up)
297 {
298 	struct uart_port *port = &up->port;
299 	struct omap8250_priv *priv = port->private_data;
300 	struct uart_8250_dma	*dma = up->dma;
301 	u8 mcr = serial8250_in_MCR(up);
302 
303 	/* Port locked to synchronize UART_IER access against the console. */
304 	lockdep_assert_held_once(&port->lock);
305 
306 	if (dma && dma->tx_running) {
307 		/*
308 		 * TCSANOW requests the change to occur immediately however if
309 		 * we have a TX-DMA operation in progress then it has been
310 		 * observed that it might stall and never complete. Therefore we
311 		 * delay DMA completes to prevent this hang from happen.
312 		 */
313 		priv->delayed_restore = 1;
314 		return;
315 	}
316 
317 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
318 	serial_out(up, UART_EFR, UART_EFR_ECB);
319 
320 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
321 	serial8250_out_MCR(up, mcr | UART_MCR_TCRTLR);
322 	serial_out(up, UART_FCR, up->fcr);
323 
324 	omap8250_update_scr(up, priv);
325 
326 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
327 
328 	serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
329 			OMAP_UART_TCR_HALT(52));
330 	serial_out(up, UART_TI752_TLR,
331 		   TRIGGER_TLR_MASK(priv->tx_trigger) << UART_TI752_TLR_TX |
332 		   TRIGGER_TLR_MASK(priv->rx_trigger) << UART_TI752_TLR_RX);
333 
334 	serial_out(up, UART_LCR, 0);
335 
336 	/* drop TCR + TLR access, we setup XON/XOFF later */
337 	serial8250_out_MCR(up, mcr);
338 
339 	serial_out(up, UART_IER, up->ier);
340 
341 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
342 	serial_dl_write(up, priv->quot);
343 
344 	serial_out(up, UART_EFR, priv->efr);
345 
346 	/* Configure flow control */
347 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
348 	serial_out(up, UART_XON1, priv->xon);
349 	serial_out(up, UART_XOFF1, priv->xoff);
350 
351 	serial_out(up, UART_LCR, up->lcr);
352 
353 	omap8250_update_mdr1(up, priv);
354 
355 	__omap8250_set_mctrl(port, port->mctrl);
356 
357 	serial_out(up, UART_OMAP_MDR3, priv->mdr3);
358 
359 	if (port->rs485.flags & SER_RS485_ENABLED &&
360 	    port->rs485_config == serial8250_em485_config)
361 		serial8250_em485_stop_tx(up, true);
362 }
363 
364 static void omap_8250_set_termios_atomic(struct uart_port *port, struct ktermios *termios,
365 					 const struct ktermios *old, unsigned int baud)
366 {
367 	struct uart_8250_port *up = up_to_u8250p(port);
368 	struct omap8250_priv *priv = port->private_data;
369 	u8 cval;
370 
371 	cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
372 
373 	if (termios->c_cflag & CSTOPB)
374 		cval |= UART_LCR_STOP;
375 	if (termios->c_cflag & PARENB)
376 		cval |= UART_LCR_PARITY;
377 	if (!(termios->c_cflag & PARODD))
378 		cval |= UART_LCR_EPAR;
379 	if (termios->c_cflag & CMSPAR)
380 		cval |= UART_LCR_SPAR;
381 
382 	omap_8250_get_divisor(port, baud, priv);
383 
384 	/*
385 	 * Ok, we're now changing the port state. Do it with
386 	 * interrupts disabled.
387 	 */
388 	guard(serial8250_rpm)(up);
389 	guard(uart_port_lock_irq)(port);
390 
391 	/*
392 	 * Update the per-port timeout.
393 	 */
394 	uart_update_timeout(port, termios->c_cflag, baud);
395 
396 	/*
397 	 * Specify which conditions may be considered for error
398 	 * handling and the ignoring of characters. The actual
399 	 * ignoring of characters only occurs if the bit is set
400 	 * in @ignore_status_mask as well.
401 	 */
402 	port->read_status_mask = UART_LSR_OE | UART_LSR_DR;
403 	if (termios->c_iflag & INPCK)
404 		port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
405 	if (termios->c_iflag & (IGNBRK | PARMRK))
406 		port->read_status_mask |= UART_LSR_BI;
407 
408 	/*
409 	 * Characters to ignore
410 	 */
411 	port->ignore_status_mask = 0;
412 	if (termios->c_iflag & IGNPAR)
413 		port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
414 	if (termios->c_iflag & IGNBRK) {
415 		port->ignore_status_mask |= UART_LSR_BI;
416 		/*
417 		 * If we're ignoring parity and break indicators,
418 		 * ignore overruns too (for real raw support).
419 		 */
420 		if (termios->c_iflag & IGNPAR)
421 			port->ignore_status_mask |= UART_LSR_OE;
422 	}
423 
424 	/*
425 	 * ignore all characters if CREAD is not set
426 	 */
427 	if ((termios->c_cflag & CREAD) == 0)
428 		port->ignore_status_mask |= UART_LSR_DR;
429 
430 	/*
431 	 * Modem status interrupts
432 	 */
433 	up->ier &= ~UART_IER_MSI;
434 	if (UART_ENABLE_MS(port, termios->c_cflag))
435 		up->ier |= UART_IER_MSI;
436 
437 	up->lcr = cval;
438 	/* Up to here it was mostly serial8250_do_set_termios() */
439 
440 	/*
441 	 * We enable TRIG_GRANU for RX and TX and additionally we set
442 	 * SCR_TX_EMPTY bit. The result is the following:
443 	 * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
444 	 * - less than RX_TRIGGER number of bytes will also cause an interrupt
445 	 *   once the UART decides that there no new bytes arriving.
446 	 * - Once THRE is enabled, the interrupt will be fired once the FIFO is
447 	 *   empty - the trigger level is ignored here.
448 	 *
449 	 * Once DMA is enabled:
450 	 * - UART will assert the TX DMA line once there is room for TX_TRIGGER
451 	 *   bytes in the TX FIFO. On each assert the DMA engine will move
452 	 *   TX_TRIGGER bytes into the FIFO.
453 	 * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
454 	 *   the FIFO and move RX_TRIGGER bytes.
455 	 * This is because threshold and trigger values are the same.
456 	 */
457 	up->fcr = UART_FCR_ENABLE_FIFO;
458 	up->fcr |= TRIGGER_FCR_MASK(priv->tx_trigger) << OMAP_UART_FCR_TX_TRIG;
459 	up->fcr |= TRIGGER_FCR_MASK(priv->rx_trigger) << OMAP_UART_FCR_RX_TRIG;
460 
461 	priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
462 		OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
463 
464 	if (up->dma)
465 		priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
466 			OMAP_UART_SCR_DMAMODE_CTL;
467 
468 	priv->xon = termios->c_cc[VSTART];
469 	priv->xoff = termios->c_cc[VSTOP];
470 
471 	priv->efr = 0;
472 	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
473 
474 	if (termios->c_cflag & CRTSCTS && port->flags & UPF_HARD_FLOW &&
475 	    !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) &&
476 	    !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_CTS)) {
477 		/* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
478 		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
479 		priv->efr |= UART_EFR_CTS;
480 	} else	if (port->flags & UPF_SOFT_FLOW) {
481 		/*
482 		 * OMAP rx s/w flow control is borked; the transmitter remains
483 		 * stuck off even if rx flow control is subsequently disabled
484 		 */
485 
486 		/*
487 		 * IXOFF Flag:
488 		 * Enable XON/XOFF flow control on output.
489 		 * Transmit XON1, XOFF1
490 		 */
491 		if (termios->c_iflag & IXOFF) {
492 			port->status |= UPSTAT_AUTOXOFF;
493 			priv->efr |= OMAP_UART_SW_TX;
494 		}
495 	}
496 	omap8250_restore_regs(up);
497 }
498 
499 /*
500  * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
501  * some differences in how we want to handle flow control.
502  */
503 static void omap_8250_set_termios(struct uart_port *port,
504 				  struct ktermios *termios,
505 				  const struct ktermios *old)
506 {
507 	struct omap8250_priv *priv = port->private_data;
508 	unsigned int baud;
509 
510 	/*
511 	 * Ask the core to calculate the divisor for us.
512 	 */
513 	baud = uart_get_baud_rate(port, termios, old,
514 				  port->uartclk / 16 / UART_DIV_MAX,
515 				  port->uartclk / 13);
516 
517 	omap_8250_set_termios_atomic(port, termios, old, baud);
518 
519 	/* calculate wakeup latency constraint */
520 	priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
521 	priv->latency = priv->calc_latency;
522 
523 	schedule_work(&priv->qos_work);
524 
525 	/* Don't rewrite B0 */
526 	if (tty_termios_baud_rate(termios))
527 		tty_termios_encode_baud_rate(termios, baud, baud);
528 }
529 
530 /* same as 8250 except that we may have extra flow bits set in EFR */
531 static void omap_8250_pm(struct uart_port *port, unsigned int state,
532 			 unsigned int oldstate)
533 {
534 	struct uart_8250_port *up = up_to_u8250p(port);
535 	u8 efr;
536 
537 	guard(serial8250_rpm)(up);
538 	/* Synchronize UART_IER access against the console. */
539 	guard(uart_port_lock_irq)(port);
540 
541 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
542 	efr = serial_in(up, UART_EFR);
543 	serial_out(up, UART_EFR, efr | UART_EFR_ECB);
544 	serial_out(up, UART_LCR, 0);
545 
546 	serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
547 	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
548 	serial_out(up, UART_EFR, efr);
549 	serial_out(up, UART_LCR, 0);
550 }
551 
552 static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
553 					      struct omap8250_priv *priv)
554 {
555 	static const struct soc_device_attribute k3_soc_devices[] = {
556 		{ .family = "AM65X",  },
557 		{ .family = "J721E", .revision = "SR1.0" },
558 		{ /* sentinel */ }
559 	};
560 	u32 mvr, scheme;
561 	u16 revision, major, minor;
562 
563 	mvr = uart_read(priv, UART_OMAP_MVER);
564 
565 	/* Check revision register scheme */
566 	scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
567 
568 	switch (scheme) {
569 	case 0: /* Legacy Scheme: OMAP2/3 */
570 		/* MINOR_REV[0:4], MAJOR_REV[4:7] */
571 		major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
572 			OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
573 		minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
574 		break;
575 	case 1:
576 		/* New Scheme: OMAP4+ */
577 		/* MINOR_REV[0:5], MAJOR_REV[8:10] */
578 		major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
579 			OMAP_UART_MVR_MAJ_SHIFT;
580 		minor = (mvr & OMAP_UART_MVR_MIN_MASK);
581 		break;
582 	default:
583 		dev_warn(up->port.dev,
584 			 "Unknown revision, defaulting to highest\n");
585 		/* highest possible revision */
586 		major = 0xff;
587 		minor = 0xff;
588 	}
589 	/* normalize revision for the driver */
590 	revision = UART_BUILD_REVISION(major, minor);
591 
592 	switch (revision) {
593 	case OMAP_UART_REV_46:
594 		priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
595 		break;
596 	case OMAP_UART_REV_52:
597 		priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
598 				OMAP_UART_WER_HAS_TX_WAKEUP;
599 		break;
600 	case OMAP_UART_REV_63:
601 		priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
602 			OMAP_UART_WER_HAS_TX_WAKEUP;
603 		break;
604 	default:
605 		break;
606 	}
607 
608 	/*
609 	 * AM65x SR1.0, AM65x SR2.0 and J721e SR1.0 don't
610 	 * don't have RHR_IT_DIS bit in IER2 register. So drop to flag
611 	 * to enable errata workaround.
612 	 */
613 	if (soc_device_match(k3_soc_devices))
614 		priv->habit &= ~UART_HAS_RHR_IT_DIS;
615 }
616 
617 static void omap8250_uart_qos_work(struct work_struct *work)
618 {
619 	struct omap8250_priv *priv;
620 
621 	priv = container_of(work, struct omap8250_priv, qos_work);
622 	cpu_latency_qos_update_request(&priv->pm_qos_request, priv->latency);
623 }
624 
625 #ifdef CONFIG_SERIAL_8250_DMA
626 static int omap_8250_dma_handle_irq(struct uart_port *port);
627 #endif
628 
629 static irqreturn_t omap8250_irq(int irq, void *dev_id)
630 {
631 	struct omap8250_priv *priv = dev_id;
632 	struct uart_8250_port *up = priv->uport;
633 	struct uart_port *port = &up->port;
634 	unsigned int iir, lsr;
635 	int ret;
636 
637 	pm_runtime_get_noresume(port->dev);
638 
639 	/* Shallow idle state wake-up to an IO interrupt? */
640 	if (atomic_add_unless(&priv->active, 1, 1)) {
641 		priv->latency = priv->calc_latency;
642 		schedule_work(&priv->qos_work);
643 	}
644 
645 #ifdef CONFIG_SERIAL_8250_DMA
646 	if (up->dma) {
647 		ret = omap_8250_dma_handle_irq(port);
648 		pm_runtime_mark_last_busy(port->dev);
649 		pm_runtime_put(port->dev);
650 		return IRQ_RETVAL(ret);
651 	}
652 #endif
653 
654 	lsr = serial_port_in(port, UART_LSR);
655 	iir = serial_port_in(port, UART_IIR);
656 	ret = serial8250_handle_irq(port, iir);
657 
658 	/*
659 	 * On K3 SoCs, it is observed that RX TIMEOUT is signalled after
660 	 * FIFO has been drained or erroneously.
661 	 * So apply solution of Errata i2310 as mentioned in
662 	 * https://www.ti.com/lit/pdf/sprz536
663 	 */
664 	if (priv->habit & UART_RX_TIMEOUT_QUIRK &&
665 	    (iir & UART_IIR_RX_TIMEOUT) == UART_IIR_RX_TIMEOUT &&
666 	    serial_port_in(port, UART_OMAP_RX_LVL) == 0) {
667 		unsigned char efr2, timeout_h, timeout_l;
668 
669 		efr2 = serial_in(up, UART_OMAP_EFR2);
670 		timeout_h = serial_in(up, UART_OMAP_TO_H);
671 		timeout_l = serial_in(up, UART_OMAP_TO_L);
672 		serial_out(up, UART_OMAP_TO_H, 0xFF);
673 		serial_out(up, UART_OMAP_TO_L, 0xFF);
674 		serial_out(up, UART_OMAP_EFR2, UART_OMAP_EFR2_TIMEOUT_BEHAVE);
675 		serial_in(up, UART_IIR);
676 		serial_out(up, UART_OMAP_EFR2, efr2);
677 		serial_out(up, UART_OMAP_TO_H, timeout_h);
678 		serial_out(up, UART_OMAP_TO_L, timeout_l);
679 	}
680 
681 	/* Stop processing interrupts on input overrun */
682 	if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
683 		unsigned long delay;
684 
685 		/* Synchronize UART_IER access against the console. */
686 		uart_port_lock(port);
687 		up->ier = serial_port_in(port, UART_IER);
688 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
689 			port->ops->stop_rx(port);
690 		} else {
691 			/* Keep restarting the timer until
692 			 * the input overrun subsides.
693 			 */
694 			cancel_delayed_work(&up->overrun_backoff);
695 		}
696 		uart_port_unlock(port);
697 
698 		delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
699 		schedule_delayed_work(&up->overrun_backoff, delay);
700 	}
701 
702 	pm_runtime_mark_last_busy(port->dev);
703 	pm_runtime_put(port->dev);
704 
705 	return IRQ_RETVAL(ret);
706 }
707 
708 static int omap_8250_startup(struct uart_port *port)
709 {
710 	struct uart_8250_port *up = up_to_u8250p(port);
711 	struct omap8250_priv *priv = port->private_data;
712 	struct uart_8250_dma *dma = &priv->omap8250_dma;
713 	int ret;
714 
715 	/* ISR and runtime PM need the uport; probe sets it only after registration */
716 	priv->uport = up;
717 
718 #ifdef CONFIG_PM
719 	up->capabilities |= UART_CAP_RPM;
720 #endif
721 
722 	guard(serial8250_rpm)(up);
723 
724 	serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
725 
726 	serial_out(up, UART_LCR, UART_LCR_WLEN8);
727 
728 	up->lsr_saved_flags = 0;
729 	up->msr_saved_flags = 0;
730 
731 	/* Disable DMA for console UART */
732 	if (dma->fn && !uart_console(port)) {
733 		up->dma = &priv->omap8250_dma;
734 		ret = serial8250_request_dma(up);
735 		if (ret) {
736 			dev_warn_ratelimited(port->dev,
737 					     "failed to request DMA\n");
738 			up->dma = NULL;
739 		}
740 	} else {
741 		up->dma = NULL;
742 	}
743 
744 	/* Synchronize UART_IER access against the console. */
745 	scoped_guard(uart_port_lock_irq, port) {
746 		up->ier = UART_IER_RLSI | UART_IER_RDI;
747 		serial_out(up, UART_IER, up->ier);
748 	}
749 
750 	/* Enable module level wake up */
751 	priv->wer = OMAP_UART_WER_MOD_WKUP;
752 	if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
753 		priv->wer |= OMAP_UART_TX_WAKEUP_EN;
754 	serial_out(up, UART_OMAP_WER, priv->wer);
755 
756 	if (up->dma && !(priv->habit & UART_HAS_EFR2)) {
757 		guard(uart_port_lock_irq)(port);
758 		up->dma->rx_dma(up);
759 	}
760 
761 	enable_irq(port->irq);
762 
763 	return 0;
764 }
765 
766 static void omap_8250_shutdown(struct uart_port *port)
767 {
768 	struct uart_8250_port *up = up_to_u8250p(port);
769 	struct omap8250_priv *priv = port->private_data;
770 
771 	guard(serial8250_rpm)(up);
772 
773 	flush_work(&priv->qos_work);
774 	if (up->dma)
775 		omap_8250_rx_dma_flush(up);
776 
777 	serial_out(up, UART_OMAP_WER, 0);
778 	if (priv->habit & UART_HAS_EFR2)
779 		serial_out(up, UART_OMAP_EFR2, 0x0);
780 
781 	/* Synchronize UART_IER access against the console. */
782 	scoped_guard(uart_port_lock_irq, port) {
783 		up->ier = 0;
784 		serial_out(up, UART_IER, 0);
785 	}
786 
787 	disable_irq_nosync(port->irq);
788 
789 	serial8250_release_dma(up);
790 	up->dma = NULL;
791 
792 	/*
793 	 * Disable break condition and FIFOs
794 	 */
795 	if (up->lcr & UART_LCR_SBC)
796 		serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
797 	serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
798 }
799 
800 static void omap_8250_throttle(struct uart_port *port)
801 {
802 	struct omap8250_priv *priv = port->private_data;
803 
804 	guard(serial8250_rpm)(up_to_u8250p(port));
805 	guard(uart_port_lock_irqsave)(port);
806 
807 	port->ops->stop_rx(port);
808 	priv->throttled = true;
809 }
810 
811 static void omap_8250_unthrottle(struct uart_port *port)
812 {
813 	struct omap8250_priv *priv = port->private_data;
814 	struct uart_8250_port *up = up_to_u8250p(port);
815 
816 	guard(serial8250_rpm)(up);
817 	/* Synchronize UART_IER access against the console. */
818 	guard(uart_port_lock_irqsave)(port);
819 
820 	priv->throttled = false;
821 	if (up->dma)
822 		up->dma->rx_dma(up);
823 	up->ier |= UART_IER_RLSI | UART_IER_RDI;
824 	serial_out(up, UART_IER, up->ier);
825 }
826 
827 static int omap8250_rs485_config(struct uart_port *port,
828 				 struct ktermios *termios,
829 				 struct serial_rs485 *rs485)
830 {
831 	struct omap8250_priv *priv = port->private_data;
832 	struct uart_8250_port *up = up_to_u8250p(port);
833 	u32 fixed_delay_rts_before_send = 0;
834 	u32 fixed_delay_rts_after_send = 0;
835 	unsigned int baud;
836 
837 	/*
838 	 * There is a fixed delay of 3 bit clock cycles after the TX shift
839 	 * register is going empty to allow time for the stop bit to transition
840 	 * through the transceiver before direction is changed to receive.
841 	 *
842 	 * Additionally there appears to be a 1 bit clock delay between writing
843 	 * to the THR register and transmission of the start bit, per page 8783
844 	 * of the AM65 TRM:  https://www.ti.com/lit/ug/spruid7e/spruid7e.pdf
845 	 */
846 	if (priv->quot) {
847 		if (priv->mdr1 == UART_OMAP_MDR1_16X_MODE)
848 			baud = port->uartclk / (16 * priv->quot);
849 		else
850 			baud = port->uartclk / (13 * priv->quot);
851 
852 		fixed_delay_rts_after_send  = 3 * MSEC_PER_SEC / baud;
853 		fixed_delay_rts_before_send = 1 * MSEC_PER_SEC / baud;
854 	}
855 
856 	/*
857 	 * Fall back to RS485 software emulation if the UART is missing
858 	 * hardware support, if the device tree specifies an mctrl_gpio
859 	 * (indicates that RTS is unavailable due to a pinmux conflict)
860 	 * or if the requested delays exceed the fixed hardware delays.
861 	 */
862 	if (!(priv->habit & UART_HAS_NATIVE_RS485) ||
863 	    mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) ||
864 	    rs485->delay_rts_after_send  > fixed_delay_rts_after_send ||
865 	    rs485->delay_rts_before_send > fixed_delay_rts_before_send) {
866 		priv->mdr3 &= ~UART_OMAP_MDR3_DIR_EN;
867 		serial_out(up, UART_OMAP_MDR3, priv->mdr3);
868 
869 		port->rs485_config = serial8250_em485_config;
870 		return serial8250_em485_config(port, termios, rs485);
871 	}
872 
873 	rs485->delay_rts_after_send  = fixed_delay_rts_after_send;
874 	rs485->delay_rts_before_send = fixed_delay_rts_before_send;
875 
876 	if (rs485->flags & SER_RS485_ENABLED)
877 		priv->mdr3 |= UART_OMAP_MDR3_DIR_EN;
878 	else
879 		priv->mdr3 &= ~UART_OMAP_MDR3_DIR_EN;
880 
881 	/*
882 	 * Retain same polarity semantics as RS485 software emulation,
883 	 * i.e. SER_RS485_RTS_ON_SEND means driving RTS low on send.
884 	 */
885 	if (rs485->flags & SER_RS485_RTS_ON_SEND)
886 		priv->mdr3 &= ~UART_OMAP_MDR3_DIR_POL;
887 	else
888 		priv->mdr3 |= UART_OMAP_MDR3_DIR_POL;
889 
890 	serial_out(up, UART_OMAP_MDR3, priv->mdr3);
891 
892 	return 0;
893 }
894 
895 #ifdef CONFIG_SERIAL_8250_DMA
896 static int omap_8250_rx_dma(struct uart_8250_port *p);
897 
898 /* Must be called while priv->rx_dma_lock is held */
899 static void __dma_rx_do_complete(struct uart_8250_port *p)
900 {
901 	struct uart_8250_dma    *dma = p->dma;
902 	struct tty_port         *tty_port = &p->port.state->port;
903 	struct omap8250_priv	*priv = p->port.private_data;
904 	struct dma_chan		*rxchan = dma->rxchan;
905 	dma_cookie_t		cookie;
906 	struct dma_tx_state     state;
907 	int                     count;
908 	int			ret;
909 	u32			reg;
910 
911 	if (!dma->rx_running)
912 		goto out;
913 
914 	cookie = dma->rx_cookie;
915 
916 	/* Re-enable RX FIFO interrupt now that transfer is complete */
917 	if (priv->habit & UART_HAS_RHR_IT_DIS) {
918 		reg = serial_in(p, UART_OMAP_IER2);
919 		reg &= ~UART_OMAP_IER2_RHR_IT_DIS;
920 		serial_out(p, UART_OMAP_IER2, reg);
921 	}
922 
923 	dmaengine_tx_status(rxchan, cookie, &state);
924 
925 	count = dma->rx_size - state.residue + state.in_flight_bytes;
926 	if (count < dma->rx_size) {
927 		dmaengine_terminate_async(rxchan);
928 
929 		/*
930 		 * Poll for teardown to complete which guarantees in
931 		 * flight data is drained.
932 		 */
933 		if (state.in_flight_bytes) {
934 			int poll_count = 25;
935 
936 			while (dmaengine_tx_status(rxchan, cookie, NULL) &&
937 			       poll_count--)
938 				cpu_relax();
939 
940 			if (poll_count == -1)
941 				dev_err(p->port.dev, "teardown incomplete\n");
942 		}
943 	}
944 
945 	dma->rx_running = 0;
946 	if (!count)
947 		goto out;
948 	ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
949 
950 	p->port.icount.rx += ret;
951 	p->port.icount.buf_overrun += count - ret;
952 out:
953 
954 	tty_flip_buffer_push(tty_port);
955 }
956 
957 static void __dma_rx_complete(void *param)
958 {
959 	struct uart_8250_port *p = param;
960 	struct omap8250_priv *priv = p->port.private_data;
961 	struct uart_8250_dma *dma = p->dma;
962 	struct dma_tx_state     state;
963 
964 	/* Synchronize UART_IER access against the console. */
965 	guard(uart_port_lock_irqsave)(&p->port);
966 
967 	/*
968 	 * If the tx status is not DMA_COMPLETE, then this is a delayed
969 	 * completion callback. A previous RX timeout flush would have
970 	 * already pushed the data, so exit.
971 	 */
972 	if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) != DMA_COMPLETE)
973 		return;
974 
975 	__dma_rx_do_complete(p);
976 	if (priv->throttled)
977 		return;
978 
979 	p->ier |= UART_IER_RLSI | UART_IER_RDI;
980 	serial_out(p, UART_IER, p->ier);
981 	if (!(priv->habit & UART_HAS_EFR2))
982 		omap_8250_rx_dma(p);
983 }
984 
985 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
986 {
987 	struct omap8250_priv	*priv = p->port.private_data;
988 	struct uart_8250_dma	*dma = p->dma;
989 	struct dma_tx_state     state;
990 	unsigned long		flags;
991 	int ret;
992 
993 	spin_lock_irqsave(&priv->rx_dma_lock, flags);
994 
995 	if (!dma->rx_running) {
996 		spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
997 		return;
998 	}
999 
1000 	ret = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
1001 	if (ret == DMA_IN_PROGRESS) {
1002 		ret = dmaengine_pause(dma->rxchan);
1003 		if (WARN_ON_ONCE(ret))
1004 			priv->rx_dma_broken = true;
1005 	}
1006 	__dma_rx_do_complete(p);
1007 	spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
1008 }
1009 
1010 static int omap_8250_rx_dma(struct uart_8250_port *p)
1011 {
1012 	struct omap8250_priv		*priv = p->port.private_data;
1013 	struct uart_8250_dma            *dma = p->dma;
1014 	int				err = 0;
1015 	struct dma_async_tx_descriptor  *desc;
1016 	unsigned long			flags;
1017 	u32				reg;
1018 
1019 	/* Port locked to synchronize UART_IER access against the console. */
1020 	lockdep_assert_held_once(&p->port.lock);
1021 
1022 	if (priv->rx_dma_broken)
1023 		return -EINVAL;
1024 
1025 	spin_lock_irqsave(&priv->rx_dma_lock, flags);
1026 
1027 	if (dma->rx_running) {
1028 		enum dma_status state;
1029 
1030 		state = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, NULL);
1031 		if (state == DMA_COMPLETE) {
1032 			/*
1033 			 * Disable RX interrupts to allow RX DMA completion
1034 			 * callback to run.
1035 			 */
1036 			p->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1037 			serial_out(p, UART_IER, p->ier);
1038 		}
1039 		goto out;
1040 	}
1041 
1042 	desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
1043 					   dma->rx_size, DMA_DEV_TO_MEM,
1044 					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1045 	if (!desc) {
1046 		err = -EBUSY;
1047 		goto out;
1048 	}
1049 
1050 	dma->rx_running = 1;
1051 	desc->callback = __dma_rx_complete;
1052 	desc->callback_param = p;
1053 
1054 	dma->rx_cookie = dmaengine_submit(desc);
1055 
1056 	/*
1057 	 * Disable RX FIFO interrupt while RX DMA is enabled, else
1058 	 * spurious interrupt may be raised when data is in the RX FIFO
1059 	 * but is yet to be drained by DMA.
1060 	 */
1061 	if (priv->habit & UART_HAS_RHR_IT_DIS) {
1062 		reg = serial_in(p, UART_OMAP_IER2);
1063 		reg |= UART_OMAP_IER2_RHR_IT_DIS;
1064 		serial_out(p, UART_OMAP_IER2, reg);
1065 	}
1066 
1067 	dma_async_issue_pending(dma->rxchan);
1068 out:
1069 	spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
1070 	return err;
1071 }
1072 
1073 static int omap_8250_tx_dma(struct uart_8250_port *p);
1074 
1075 static void omap_8250_dma_tx_complete(void *param)
1076 {
1077 	struct uart_8250_port	*p = param;
1078 	struct uart_8250_dma	*dma = p->dma;
1079 	struct tty_port		*tport = &p->port.state->port;
1080 	bool			en_thri = false;
1081 	struct omap8250_priv	*priv = p->port.private_data;
1082 
1083 	dma_sync_single_for_cpu(dmaengine_get_dma_device(dma->txchan), dma->tx_addr,
1084 				UART_XMIT_SIZE, DMA_TO_DEVICE);
1085 
1086 	guard(uart_port_lock_irqsave)(&p->port);
1087 
1088 	dma->tx_running = 0;
1089 
1090 	uart_xmit_advance(&p->port, dma->tx_size);
1091 
1092 	if (priv->delayed_restore) {
1093 		priv->delayed_restore = 0;
1094 		omap8250_restore_regs(p);
1095 	}
1096 
1097 	if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1098 		uart_write_wakeup(&p->port);
1099 
1100 	if (!kfifo_is_empty(&tport->xmit_fifo) && !uart_tx_stopped(&p->port)) {
1101 		int ret;
1102 
1103 		ret = omap_8250_tx_dma(p);
1104 		if (ret)
1105 			en_thri = true;
1106 	} else if (p->capabilities & UART_CAP_RPM) {
1107 		en_thri = true;
1108 	}
1109 
1110 	if (en_thri) {
1111 		dma->tx_err = 1;
1112 		serial8250_set_THRI(p);
1113 	}
1114 }
1115 
1116 static int omap_8250_tx_dma(struct uart_8250_port *p)
1117 {
1118 	struct uart_8250_dma		*dma = p->dma;
1119 	struct omap8250_priv		*priv = p->port.private_data;
1120 	struct tty_port			*tport = &p->port.state->port;
1121 	struct dma_async_tx_descriptor	*desc;
1122 	struct scatterlist sg;
1123 	int skip_byte = -1;
1124 	int ret;
1125 
1126 	if (dma->tx_running)
1127 		return 0;
1128 	if (uart_tx_stopped(&p->port) || kfifo_is_empty(&tport->xmit_fifo)) {
1129 
1130 		/*
1131 		 * Even if no data, we need to return an error for the two cases
1132 		 * below so serial8250_tx_chars() is invoked and properly clears
1133 		 * THRI and/or runtime suspend.
1134 		 */
1135 		if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
1136 			ret = -EBUSY;
1137 			goto err;
1138 		}
1139 		serial8250_clear_THRI(p);
1140 		return 0;
1141 	}
1142 
1143 	if (priv->habit & OMAP_DMA_TX_KICK) {
1144 		unsigned char c;
1145 		u8 tx_lvl;
1146 
1147 		/*
1148 		 * We need to put the first byte into the FIFO in order to start
1149 		 * the DMA transfer. For transfers smaller than four bytes we
1150 		 * don't bother doing DMA at all. It seem not matter if there
1151 		 * are still bytes in the FIFO from the last transfer (in case
1152 		 * we got here directly from omap_8250_dma_tx_complete()). Bytes
1153 		 * leaving the FIFO seem not to trigger the DMA transfer. It is
1154 		 * really the byte that we put into the FIFO.
1155 		 * If the FIFO is already full then we most likely got here from
1156 		 * omap_8250_dma_tx_complete(). And this means the DMA engine
1157 		 * just completed its work. We don't have to wait the complete
1158 		 * 86us at 115200,8n1 but around 60us (not to mention lower
1159 		 * baudrates). So in that case we take the interrupt and try
1160 		 * again with an empty FIFO.
1161 		 */
1162 		tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
1163 		if (tx_lvl == p->tx_loadsz) {
1164 			ret = -EBUSY;
1165 			goto err;
1166 		}
1167 		if (kfifo_len(&tport->xmit_fifo) < 4) {
1168 			ret = -EINVAL;
1169 			goto err;
1170 		}
1171 		if (!uart_fifo_out(&p->port, &c, 1)) {
1172 			ret = -EINVAL;
1173 			goto err;
1174 		}
1175 		skip_byte = c;
1176 	}
1177 
1178 	sg_init_table(&sg, 1);
1179 	ret = kfifo_dma_out_prepare_mapped(&tport->xmit_fifo, &sg, 1, UART_XMIT_SIZE, dma->tx_addr);
1180 	if (ret != 1) {
1181 		ret = -EINVAL;
1182 		goto err;
1183 	}
1184 
1185 	desc = dmaengine_prep_slave_sg(dma->txchan, &sg, 1, DMA_MEM_TO_DEV,
1186 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1187 	if (!desc) {
1188 		ret = -EBUSY;
1189 		goto err;
1190 	}
1191 
1192 	dma->tx_size = sg_dma_len(&sg);
1193 	dma->tx_running = 1;
1194 
1195 	desc->callback = omap_8250_dma_tx_complete;
1196 	desc->callback_param = p;
1197 
1198 	dma->tx_cookie = dmaengine_submit(desc);
1199 
1200 	dma_sync_single_for_device(dmaengine_get_dma_device(dma->txchan), dma->tx_addr,
1201 				   UART_XMIT_SIZE, DMA_TO_DEVICE);
1202 
1203 	dma_async_issue_pending(dma->txchan);
1204 	if (dma->tx_err)
1205 		dma->tx_err = 0;
1206 
1207 	serial8250_clear_THRI(p);
1208 	ret = 0;
1209 	goto out_skip;
1210 err:
1211 	dma->tx_err = 1;
1212 out_skip:
1213 	if (skip_byte >= 0)
1214 		serial_out(p, UART_TX, skip_byte);
1215 	return ret;
1216 }
1217 
1218 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1219 {
1220 	switch (iir & 0x3f) {
1221 	case UART_IIR_RLSI:
1222 	case UART_IIR_RX_TIMEOUT:
1223 	case UART_IIR_RDI:
1224 		omap_8250_rx_dma_flush(up);
1225 		return true;
1226 	}
1227 	return omap_8250_rx_dma(up);
1228 }
1229 
1230 static u16 omap_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir, u16 status)
1231 {
1232 	if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
1233 	    (iir & UART_IIR_RDI)) {
1234 		if (handle_rx_dma(up, iir)) {
1235 			status = serial8250_rx_chars(up, status);
1236 			omap_8250_rx_dma(up);
1237 		}
1238 	}
1239 
1240 	return status;
1241 }
1242 
1243 static void am654_8250_handle_uart_errors(struct uart_8250_port *up, u8 iir, u16 status)
1244 {
1245 	if (status & UART_LSR_OE) {
1246 		serial8250_clear_and_reinit_fifos(up);
1247 		serial_in(up, UART_LSR);
1248 		serial_in(up, UART_OMAP_RESUME);
1249 	} else {
1250 		if (status & (UART_LSR_FE | UART_LSR_PE | UART_LSR_BI))
1251 			serial_in(up, UART_RX);
1252 		if (iir & UART_IIR_XOFF)
1253 			serial_in(up, UART_IIR);
1254 	}
1255 }
1256 
1257 static void am654_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir,
1258 				     u16 status)
1259 {
1260 	/* Port locked to synchronize UART_IER access against the console. */
1261 	lockdep_assert_held_once(&up->port.lock);
1262 
1263 	/*
1264 	 * Queue a new transfer if FIFO has data.
1265 	 */
1266 	if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
1267 	    (up->ier & UART_IER_RDI) && !(status & UART_LSR_OE)) {
1268 		am654_8250_handle_uart_errors(up, iir, status);
1269 		omap_8250_rx_dma(up);
1270 		serial_out(up, UART_OMAP_EFR2, UART_OMAP_EFR2_TIMEOUT_BEHAVE);
1271 	} else if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
1272 		/*
1273 		 * Disable RX timeout, read IIR to clear
1274 		 * current timeout condition, clear EFR2 to
1275 		 * periodic timeouts, re-enable interrupts.
1276 		 */
1277 		up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1278 		serial_out(up, UART_IER, up->ier);
1279 		omap_8250_rx_dma_flush(up);
1280 		serial_in(up, UART_IIR);
1281 		serial_out(up, UART_OMAP_EFR2, 0x0);
1282 		up->ier |= UART_IER_RLSI | UART_IER_RDI;
1283 		serial_out(up, UART_IER, up->ier);
1284 	} else {
1285 		am654_8250_handle_uart_errors(up, iir, status);
1286 	}
1287 }
1288 
1289 /*
1290  * This is mostly serial8250_handle_irq(). We have a slightly different DMA
1291  * hook for RX/TX and need different logic for them in the ISR. Therefore we
1292  * use the default routine in the non-DMA case and this one for with DMA.
1293  */
1294 static int omap_8250_dma_handle_irq(struct uart_port *port)
1295 {
1296 	struct uart_8250_port *up = up_to_u8250p(port);
1297 	struct omap8250_priv *priv = port->private_data;
1298 	u16 status;
1299 	u8 iir;
1300 
1301 	iir = serial_port_in(port, UART_IIR);
1302 	if (iir & UART_IIR_NO_INT) {
1303 		return IRQ_HANDLED;
1304 	}
1305 
1306 	uart_port_lock(port);
1307 
1308 	status = serial_port_in(port, UART_LSR);
1309 
1310 	if ((iir & 0x3f) != UART_IIR_THRI) {
1311 		if (priv->habit & UART_HAS_EFR2)
1312 			am654_8250_handle_rx_dma(up, iir, status);
1313 		else
1314 			status = omap_8250_handle_rx_dma(up, iir, status);
1315 	}
1316 
1317 	serial8250_modem_status(up);
1318 	if (status & UART_LSR_THRE && up->dma->tx_err) {
1319 		if (uart_tx_stopped(port) ||
1320 		    kfifo_is_empty(&port->state->port.xmit_fifo)) {
1321 			up->dma->tx_err = 0;
1322 			serial8250_tx_chars(up);
1323 		} else  {
1324 			/*
1325 			 * try again due to an earlier failure which
1326 			 * might have been resolved by now.
1327 			 */
1328 			if (omap_8250_tx_dma(up))
1329 				serial8250_tx_chars(up);
1330 		}
1331 	}
1332 
1333 	uart_unlock_and_check_sysrq(port);
1334 
1335 	return 1;
1336 }
1337 
1338 static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1339 {
1340 	return false;
1341 }
1342 
1343 #else
1344 
1345 static inline int omap_8250_rx_dma(struct uart_8250_port *p)
1346 {
1347 	return -EINVAL;
1348 }
1349 #endif
1350 
1351 static int omap8250_no_handle_irq(struct uart_port *port)
1352 {
1353 	/* IRQ has not been requested but handling irq? */
1354 	WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1355 	return 0;
1356 }
1357 
1358 static int omap8250_select_wakeup_pinctrl(struct device *dev,
1359 					  struct omap8250_priv *priv)
1360 {
1361 	if (IS_ERR_OR_NULL(priv->pinctrl_wakeup))
1362 		return 0;
1363 
1364 	if (!device_may_wakeup(dev))
1365 		return 0;
1366 
1367 	device_set_out_band_wakeup(dev);
1368 
1369 	return pinctrl_select_state(priv->pinctrl, priv->pinctrl_wakeup);
1370 }
1371 
1372 static struct omap8250_dma_params am654_dma = {
1373 	.rx_size = SZ_2K,
1374 	.rx_trigger = 1,
1375 	.tx_trigger = TX_TRIGGER,
1376 };
1377 
1378 static struct omap8250_dma_params am33xx_dma = {
1379 	.rx_size = RX_TRIGGER,
1380 	.rx_trigger = RX_TRIGGER,
1381 	.tx_trigger = TX_TRIGGER,
1382 };
1383 
1384 static struct omap8250_platdata am654_platdata = {
1385 	.dma_params	= &am654_dma,
1386 	.habit		= UART_HAS_EFR2 | UART_HAS_RHR_IT_DIS |
1387 			  UART_RX_TIMEOUT_QUIRK | UART_HAS_NATIVE_RS485,
1388 };
1389 
1390 static struct omap8250_platdata am33xx_platdata = {
1391 	.dma_params	= &am33xx_dma,
1392 	.habit		= OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE,
1393 };
1394 
1395 static struct omap8250_platdata omap4_platdata = {
1396 	.dma_params	= &am33xx_dma,
1397 	.habit		= UART_ERRATA_CLOCK_DISABLE,
1398 };
1399 
1400 static const struct of_device_id omap8250_dt_ids[] = {
1401 	{ .compatible = "ti,am654-uart", .data = &am654_platdata, },
1402 	{ .compatible = "ti,omap2-uart" },
1403 	{ .compatible = "ti,omap3-uart" },
1404 	{ .compatible = "ti,omap4-uart", .data = &omap4_platdata, },
1405 	{ .compatible = "ti,am3352-uart", .data = &am33xx_platdata, },
1406 	{ .compatible = "ti,am4372-uart", .data = &am33xx_platdata, },
1407 	{ .compatible = "ti,dra742-uart", .data = &omap4_platdata, },
1408 	{},
1409 };
1410 MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1411 
1412 static int omap8250_probe(struct platform_device *pdev)
1413 {
1414 	struct device_node *np = pdev->dev.of_node;
1415 	struct omap8250_priv *priv;
1416 	const struct omap8250_platdata *pdata;
1417 	struct uart_8250_port up;
1418 	struct resource *regs;
1419 	void __iomem *membase;
1420 	int ret;
1421 
1422 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1423 	if (!regs) {
1424 		dev_err(&pdev->dev, "missing registers\n");
1425 		return -EINVAL;
1426 	}
1427 
1428 	priv = devm_kzalloc_obj(&pdev->dev, *priv);
1429 	if (!priv)
1430 		return -ENOMEM;
1431 
1432 	membase = devm_ioremap(&pdev->dev, regs->start,
1433 				       resource_size(regs));
1434 	if (!membase)
1435 		return -ENODEV;
1436 
1437 	memset(&up, 0, sizeof(up));
1438 	up.port.dev = &pdev->dev;
1439 	up.port.mapbase = regs->start;
1440 	up.port.membase = membase;
1441 	/*
1442 	 * It claims to be 16C750 compatible however it is a little different.
1443 	 * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1444 	 * have) is enabled via EFR instead of MCR. The type is set here 8250
1445 	 * just to get things going. UNKNOWN does not work for a few reasons and
1446 	 * we don't need our own type since we don't use 8250's set_termios()
1447 	 * or pm callback.
1448 	 */
1449 	up.port.type = PORT_8250;
1450 	up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW | UPF_HARD_FLOW;
1451 	up.port.private_data = priv;
1452 
1453 	up.tx_loadsz = 64;
1454 	up.capabilities = UART_CAP_FIFO;
1455 #ifdef CONFIG_PM
1456 	/*
1457 	 * Runtime PM is mostly transparent. However to do it right we need to a
1458 	 * TX empty interrupt before we can put the device to auto idle. So if
1459 	 * PM is not enabled we don't add that flag and can spare that one extra
1460 	 * interrupt in the TX path.
1461 	 */
1462 	up.capabilities |= UART_CAP_RPM;
1463 #endif
1464 	up.port.set_termios = omap_8250_set_termios;
1465 	up.port.set_mctrl = omap8250_set_mctrl;
1466 	up.port.pm = omap_8250_pm;
1467 	up.port.startup = omap_8250_startup;
1468 	up.port.shutdown = omap_8250_shutdown;
1469 	up.port.throttle = omap_8250_throttle;
1470 	up.port.unthrottle = omap_8250_unthrottle;
1471 	up.port.rs485_config = omap8250_rs485_config;
1472 	/* same rs485_supported for software emulation and native RS485 */
1473 	up.port.rs485_supported = serial8250_em485_supported;
1474 	up.rs485_start_tx = serial8250_em485_start_tx;
1475 	up.rs485_stop_tx = serial8250_em485_stop_tx;
1476 	up.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
1477 
1478 	ret = uart_read_port_properties(&up.port);
1479 	if (ret)
1480 		return ret;
1481 
1482 	up.port.regshift = OMAP_UART_REGSHIFT;
1483 	up.port.fifosize = 64;
1484 
1485 	if (!up.port.uartclk) {
1486 		struct clk *clk;
1487 
1488 		clk = devm_clk_get(&pdev->dev, NULL);
1489 		if (IS_ERR(clk)) {
1490 			if (PTR_ERR(clk) == -EPROBE_DEFER)
1491 				return -EPROBE_DEFER;
1492 		} else {
1493 			up.port.uartclk = clk_get_rate(clk);
1494 		}
1495 	}
1496 
1497 	if (of_property_read_u32(np, "overrun-throttle-ms",
1498 				 &up.overrun_backoff_time_ms) != 0)
1499 		up.overrun_backoff_time_ms = 0;
1500 
1501 	pdata = of_device_get_match_data(&pdev->dev);
1502 	if (pdata)
1503 		priv->habit |= pdata->habit;
1504 
1505 	if (!up.port.uartclk) {
1506 		up.port.uartclk = DEFAULT_CLK_SPEED;
1507 		dev_warn(&pdev->dev,
1508 			 "No clock speed specified: using default: %d\n",
1509 			 DEFAULT_CLK_SPEED);
1510 	}
1511 
1512 	priv->membase = membase;
1513 	priv->uport = NULL;
1514 	priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1515 	priv->calc_latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1516 	cpu_latency_qos_add_request(&priv->pm_qos_request, priv->latency);
1517 	INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1518 
1519 	spin_lock_init(&priv->rx_dma_lock);
1520 
1521 	platform_set_drvdata(pdev, priv);
1522 
1523 	device_set_wakeup_capable(&pdev->dev, true);
1524 	if (of_property_read_bool(np, "wakeup-source"))
1525 		device_set_wakeup_enable(&pdev->dev, true);
1526 
1527 	pm_runtime_enable(&pdev->dev);
1528 	pm_runtime_use_autosuspend(&pdev->dev);
1529 
1530 	/*
1531 	 * Disable runtime PM until autosuspend delay unless specifically
1532 	 * enabled by the user via sysfs. This is the historic way to
1533 	 * prevent an unsafe default policy with lossy characters on wake-up.
1534 	 * For serdev devices this is not needed, the policy can be managed by
1535 	 * the serdev driver.
1536 	 */
1537 	if (!of_get_available_child_count(pdev->dev.of_node))
1538 		pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1539 
1540 	pm_runtime_get_sync(&pdev->dev);
1541 
1542 	omap_serial_fill_features_erratas(&up, priv);
1543 	up.port.handle_irq = omap8250_no_handle_irq;
1544 	priv->rx_trigger = RX_TRIGGER;
1545 	priv->tx_trigger = TX_TRIGGER;
1546 #ifdef CONFIG_SERIAL_8250_DMA
1547 	/*
1548 	 * Oh DMA support. If there are no DMA properties in the DT then
1549 	 * we will fall back to a generic DMA channel which does not
1550 	 * really work here. To ensure that we do not get a generic DMA
1551 	 * channel assigned, we have the the_no_dma_filter_fn() here.
1552 	 * To avoid "failed to request DMA" messages we check for DMA
1553 	 * properties in DT.
1554 	 */
1555 	ret = of_property_count_strings(np, "dma-names");
1556 	if (ret == 2) {
1557 		struct omap8250_dma_params *dma_params = NULL;
1558 		struct uart_8250_dma *dma = &priv->omap8250_dma;
1559 
1560 		dma->fn = the_no_dma_filter_fn;
1561 		dma->tx_dma = omap_8250_tx_dma;
1562 		dma->rx_dma = omap_8250_rx_dma;
1563 		if (pdata)
1564 			dma_params = pdata->dma_params;
1565 
1566 		if (dma_params) {
1567 			dma->rx_size = dma_params->rx_size;
1568 			dma->rxconf.src_maxburst = dma_params->rx_trigger;
1569 			dma->txconf.dst_maxburst = dma_params->tx_trigger;
1570 			priv->rx_trigger = dma_params->rx_trigger;
1571 			priv->tx_trigger = dma_params->tx_trigger;
1572 		} else {
1573 			dma->rx_size = RX_TRIGGER;
1574 			dma->rxconf.src_maxburst = RX_TRIGGER;
1575 			dma->txconf.dst_maxburst = TX_TRIGGER;
1576 		}
1577 	}
1578 #endif
1579 
1580 	irq_set_status_flags(up.port.irq, IRQ_NOAUTOEN);
1581 	ret = devm_request_irq(&pdev->dev, up.port.irq, omap8250_irq, 0,
1582 			       dev_name(&pdev->dev), priv);
1583 	if (ret < 0)
1584 		goto err;
1585 
1586 	priv->wakeirq = irq_of_parse_and_map(np, 1);
1587 
1588 	if (priv->wakeirq) {
1589 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev, priv->wakeirq);
1590 		if (ret)
1591 			goto err;
1592 	}
1593 
1594 	struct uart_8250_port *uport = serial8250_register_8250_port(&up);
1595 	if (IS_ERR(uport)) {
1596 		ret = PTR_ERR(uport);
1597 		dev_err(&pdev->dev, "unable to register 8250 port\n");
1598 		goto err;
1599 	}
1600 	priv->uport = uport;
1601 	pm_runtime_mark_last_busy(&pdev->dev);
1602 	pm_runtime_put_autosuspend(&pdev->dev);
1603 
1604 	priv->pinctrl = devm_pinctrl_get(&pdev->dev);
1605 	if (!IS_ERR_OR_NULL(priv->pinctrl))
1606 		priv->pinctrl_wakeup = pinctrl_lookup_state(priv->pinctrl, "wakeup");
1607 
1608 	return 0;
1609 err:
1610 	dev_pm_clear_wake_irq(&pdev->dev);
1611 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1612 	pm_runtime_put_sync(&pdev->dev);
1613 	flush_work(&priv->qos_work);
1614 	pm_runtime_disable(&pdev->dev);
1615 	cpu_latency_qos_remove_request(&priv->pm_qos_request);
1616 	return ret;
1617 }
1618 
1619 static void omap8250_remove(struct platform_device *pdev)
1620 {
1621 	struct omap8250_priv *priv = platform_get_drvdata(pdev);
1622 	struct uart_8250_port *up;
1623 	int err;
1624 
1625 	err = pm_runtime_resume_and_get(&pdev->dev);
1626 	if (err)
1627 		dev_err(&pdev->dev, "Failed to resume hardware\n");
1628 
1629 	up = priv->uport;
1630 	omap_8250_shutdown(&up->port);
1631 	serial8250_unregister_port(up);
1632 	priv->uport = NULL;
1633 	dev_pm_clear_wake_irq(&pdev->dev);
1634 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1635 	pm_runtime_put_sync(&pdev->dev);
1636 	flush_work(&priv->qos_work);
1637 	pm_runtime_disable(&pdev->dev);
1638 	cpu_latency_qos_remove_request(&priv->pm_qos_request);
1639 	device_set_wakeup_capable(&pdev->dev, false);
1640 }
1641 
1642 static int omap8250_prepare(struct device *dev)
1643 {
1644 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1645 
1646 	if (!priv)
1647 		return 0;
1648 	priv->is_suspending = true;
1649 	return 0;
1650 }
1651 
1652 static void omap8250_complete(struct device *dev)
1653 {
1654 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1655 
1656 	if (!priv)
1657 		return;
1658 	priv->is_suspending = false;
1659 }
1660 
1661 static int omap8250_suspend(struct device *dev)
1662 {
1663 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1664 	struct uart_8250_port *up = priv->uport;
1665 	int err = 0;
1666 
1667 	err = omap8250_select_wakeup_pinctrl(dev, priv);
1668 	if (err) {
1669 		dev_err(dev, "Failed to select wakeup pinctrl, aborting suspend %pe\n",
1670 			ERR_PTR(err));
1671 		return err;
1672 	}
1673 
1674 	serial8250_suspend_port(up);
1675 
1676 	err = pm_runtime_resume_and_get(dev);
1677 	if (err)
1678 		return err;
1679 	if (!device_may_wakeup(dev))
1680 		priv->wer = 0;
1681 	serial_out(up, UART_OMAP_WER, priv->wer);
1682 	if (uart_console(&up->port) && console_suspend_enabled)
1683 		err = pm_runtime_force_suspend(dev);
1684 	flush_work(&priv->qos_work);
1685 
1686 	return err;
1687 }
1688 
1689 static int omap8250_resume(struct device *dev)
1690 {
1691 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1692 	struct uart_8250_port *up = priv->uport;
1693 	int err;
1694 
1695 	err = pinctrl_select_default_state(dev);
1696 	if (err) {
1697 		dev_err(dev, "Failed to select default pinctrl state on resume: %pe\n",
1698 			ERR_PTR(err));
1699 		return err;
1700 	}
1701 
1702 	if (uart_console(&up->port) && console_suspend_enabled) {
1703 		err = pm_runtime_force_resume(dev);
1704 		if (err)
1705 			return err;
1706 	}
1707 
1708 	serial8250_resume_port(up);
1709 	/* Paired with pm_runtime_resume_and_get() in omap8250_suspend() */
1710 	pm_runtime_mark_last_busy(dev);
1711 	pm_runtime_put_autosuspend(dev);
1712 
1713 	return 0;
1714 }
1715 
1716 static int omap8250_lost_context(struct uart_8250_port *up)
1717 {
1718 	u32 val;
1719 
1720 	val = serial_in(up, UART_OMAP_SCR);
1721 	/*
1722 	 * If we lose context, then SCR is set to its reset value of zero.
1723 	 * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
1724 	 * among other bits, to never set the register back to zero again.
1725 	 */
1726 	if (!val)
1727 		return 1;
1728 	return 0;
1729 }
1730 
1731 static void uart_write(struct omap8250_priv *priv, u32 reg, u32 val)
1732 {
1733 	writel(val, priv->membase + (reg << OMAP_UART_REGSHIFT));
1734 }
1735 
1736 /* TODO: in future, this should happen via API in drivers/reset/ */
1737 static int omap8250_soft_reset(struct device *dev)
1738 {
1739 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1740 	int timeout = 100;
1741 	int sysc;
1742 	int syss;
1743 
1744 	/*
1745 	 * At least on omap4, unused uarts may not idle after reset without
1746 	 * a basic scr dma configuration even with no dma in use. The
1747 	 * module clkctrl status bits will be 1 instead of 3 blocking idle
1748 	 * for the whole clockdomain. The softreset below will clear scr,
1749 	 * and we restore it on resume so this is safe to do on all SoCs
1750 	 * needing omap8250_soft_reset() quirk. Do it in two writes as
1751 	 * recommended in the comment for omap8250_update_scr().
1752 	 */
1753 	uart_write(priv, UART_OMAP_SCR, OMAP_UART_SCR_DMAMODE_1);
1754 	uart_write(priv, UART_OMAP_SCR,
1755 		   OMAP_UART_SCR_DMAMODE_1 | OMAP_UART_SCR_DMAMODE_CTL);
1756 
1757 	sysc = uart_read(priv, UART_OMAP_SYSC);
1758 
1759 	/* softreset the UART */
1760 	sysc |= OMAP_UART_SYSC_SOFTRESET;
1761 	uart_write(priv, UART_OMAP_SYSC, sysc);
1762 
1763 	/* By experiments, 1us enough for reset complete on AM335x */
1764 	do {
1765 		udelay(1);
1766 		syss = uart_read(priv, UART_OMAP_SYSS);
1767 	} while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
1768 
1769 	if (!timeout) {
1770 		dev_err(dev, "timed out waiting for reset done\n");
1771 		return -ETIMEDOUT;
1772 	}
1773 
1774 	return 0;
1775 }
1776 
1777 static int omap8250_runtime_suspend(struct device *dev)
1778 {
1779 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1780 	struct uart_8250_port *up = priv->uport;
1781 
1782 	if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
1783 		int ret;
1784 
1785 		ret = omap8250_soft_reset(dev);
1786 		if (ret)
1787 			return ret;
1788 
1789 		if (up) {
1790 			/* Restore to UART mode after reset (for wakeup) */
1791 			omap8250_update_mdr1(up, priv);
1792 			/* Restore wakeup enable register */
1793 			serial_out(up, UART_OMAP_WER, priv->wer);
1794 		}
1795 	}
1796 
1797 	if (up && up->dma && up->dma->rxchan)
1798 		omap_8250_rx_dma_flush(up);
1799 
1800 	priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1801 	schedule_work(&priv->qos_work);
1802 	atomic_set(&priv->active, 0);
1803 
1804 	return 0;
1805 }
1806 
1807 static int omap8250_runtime_resume(struct device *dev)
1808 {
1809 	struct omap8250_priv *priv = dev_get_drvdata(dev);
1810 	struct uart_8250_port *up = priv->uport;
1811 
1812 	/* Did the hardware wake to a device IO interrupt before a wakeirq? */
1813 	if (atomic_read(&priv->active))
1814 		return 0;
1815 
1816 	if (up && omap8250_lost_context(up)) {
1817 		guard(uart_port_lock_irq)(&up->port);
1818 		omap8250_restore_regs(up);
1819 	}
1820 
1821 	if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2)) {
1822 		guard(uart_port_lock_irq)(&up->port);
1823 		omap_8250_rx_dma(up);
1824 	}
1825 
1826 	atomic_set(&priv->active, 1);
1827 	priv->latency = priv->calc_latency;
1828 	schedule_work(&priv->qos_work);
1829 
1830 	return 0;
1831 }
1832 
1833 #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1834 static int __init omap8250_console_fixup(void)
1835 {
1836 	char *omap_str;
1837 	char *options;
1838 	u8 idx;
1839 
1840 	if (strstr(boot_command_line, "console=ttyS"))
1841 		/* user set a ttyS based name for the console */
1842 		return 0;
1843 
1844 	omap_str = strstr(boot_command_line, "console=ttyO");
1845 	if (!omap_str)
1846 		/* user did not set ttyO based console, so we don't care */
1847 		return 0;
1848 
1849 	omap_str += 12;
1850 	if ('0' <= *omap_str && *omap_str <= '9')
1851 		idx = *omap_str - '0';
1852 	else
1853 		return 0;
1854 
1855 	omap_str++;
1856 	if (omap_str[0] == ',') {
1857 		omap_str++;
1858 		options = omap_str;
1859 	} else {
1860 		options = NULL;
1861 	}
1862 
1863 	add_preferred_console("ttyS", idx, options);
1864 	pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1865 	       idx, idx);
1866 	pr_err("This ensures that you still see kernel messages. Please\n");
1867 	pr_err("update your kernel commandline.\n");
1868 	return 0;
1869 }
1870 console_initcall(omap8250_console_fixup);
1871 #endif
1872 
1873 static const struct dev_pm_ops omap8250_dev_pm_ops = {
1874 	SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1875 	RUNTIME_PM_OPS(omap8250_runtime_suspend,
1876 			   omap8250_runtime_resume, NULL)
1877 	.prepare        = pm_sleep_ptr(omap8250_prepare),
1878 	.complete       = pm_sleep_ptr(omap8250_complete),
1879 };
1880 
1881 static struct platform_driver omap8250_platform_driver = {
1882 	.driver = {
1883 		.name		= "omap8250",
1884 		.pm		= pm_ptr(&omap8250_dev_pm_ops),
1885 		.of_match_table = omap8250_dt_ids,
1886 	},
1887 	.probe			= omap8250_probe,
1888 	.remove			= omap8250_remove,
1889 };
1890 module_platform_driver(omap8250_platform_driver);
1891 
1892 MODULE_AUTHOR("Sebastian Andrzej Siewior");
1893 MODULE_DESCRIPTION("OMAP 8250 Driver");
1894 MODULE_LICENSE("GPL v2");
1895