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