1 // SPDX-License-Identifier: GPL-2.0
3 * 8250-core based driver for the OMAP internal UART
5 * based on omap-serial.c, Copyright (C) 2010 Texas Instruments.
7 * Copyright (C) 2014 Sebastian Andrzej Siewior
11 #include <linux/clk.h>
12 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/serial_8250.h>
16 #include <linux/serial_reg.h>
17 #include <linux/tty_flip.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
21 #include <linux/of_device.h>
22 #include <linux/of_gpio.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>
34 #define DEFAULT_CLK_SPEED 48000000
35 #define OMAP_UART_REGSHIFT 2
37 #define UART_ERRATA_i202_MDR1_ACCESS (1 << 0)
38 #define OMAP_UART_WER_HAS_TX_WAKEUP (1 << 1)
39 #define OMAP_DMA_TX_KICK (1 << 2)
41 * See Advisory 21 in AM437x errata SPRZ408B, updated April 2015.
42 * The same errata is applicable to AM335x and DRA7x processors too.
44 #define UART_ERRATA_CLOCK_DISABLE (1 << 3)
45 #define UART_HAS_EFR2 BIT(4)
46 #define UART_HAS_RHR_IT_DIS BIT(5)
47 #define UART_RX_TIMEOUT_QUIRK BIT(6)
49 #define OMAP_UART_FCR_RX_TRIG 6
50 #define OMAP_UART_FCR_TX_TRIG 4
52 /* SCR register bitmasks */
53 #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
54 #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
55 #define OMAP_UART_SCR_TX_EMPTY (1 << 3)
56 #define OMAP_UART_SCR_DMAMODE_MASK (3 << 1)
57 #define OMAP_UART_SCR_DMAMODE_1 (1 << 1)
58 #define OMAP_UART_SCR_DMAMODE_CTL (1 << 0)
60 /* MVR register bitmasks */
61 #define OMAP_UART_MVR_SCHEME_SHIFT 30
62 #define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
63 #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
64 #define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
65 #define OMAP_UART_MVR_MAJ_MASK 0x700
66 #define OMAP_UART_MVR_MAJ_SHIFT 8
67 #define OMAP_UART_MVR_MIN_MASK 0x3f
69 /* SYSC register bitmasks */
70 #define OMAP_UART_SYSC_SOFTRESET (1 << 1)
72 /* SYSS register bitmasks */
73 #define OMAP_UART_SYSS_RESETDONE (1 << 0)
75 #define UART_TI752_TLR_TX 0
76 #define UART_TI752_TLR_RX 4
78 #define TRIGGER_TLR_MASK(x) ((x & 0x3c) >> 2)
79 #define TRIGGER_FCR_MASK(x) (x & 3)
81 /* Enable XON/XOFF flow control on output */
82 #define OMAP_UART_SW_TX 0x08
83 /* Enable XON/XOFF flow control on input */
84 #define OMAP_UART_SW_RX 0x02
86 #define OMAP_UART_WER_MOD_WKUP 0x7f
87 #define OMAP_UART_TX_WAKEUP_EN (1 << 7)
92 #define OMAP_UART_TCR_RESTORE(x) ((x / 4) << 4)
93 #define OMAP_UART_TCR_HALT(x) ((x / 4) << 0)
95 #define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
97 #define OMAP_UART_REV_46 0x0406
98 #define OMAP_UART_REV_52 0x0502
99 #define OMAP_UART_REV_63 0x0603
101 /* Interrupt Enable Register 2 */
102 #define UART_OMAP_IER2 0x1B
103 #define UART_OMAP_IER2_RHR_IT_DIS BIT(2)
105 /* Enhanced features register 2 */
106 #define UART_OMAP_EFR2 0x23
107 #define UART_OMAP_EFR2_TIMEOUT_BEHAVE BIT(6)
109 /* RX FIFO occupancy indicator */
110 #define UART_OMAP_RX_LVL 0x19
112 struct omap8250_priv {
113 void __iomem *membase;
132 struct pm_qos_request pm_qos_request;
133 struct work_struct qos_work;
134 struct uart_8250_dma omap8250_dma;
135 spinlock_t rx_dma_lock;
140 struct omap8250_dma_params {
146 struct omap8250_platdata {
147 struct omap8250_dma_params *dma_params;
151 #ifdef CONFIG_SERIAL_8250_DMA
152 static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
154 static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
157 static u32 uart_read(struct omap8250_priv *priv, u32 reg)
159 return readl(priv->membase + (reg << OMAP_UART_REGSHIFT));
163 * Called on runtime PM resume path from omap8250_restore_regs(), and
164 * omap8250_set_mctrl().
166 static void __omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
168 struct uart_8250_port *up = up_to_u8250p(port);
169 struct omap8250_priv *priv = up->port.private_data;
172 serial8250_do_set_mctrl(port, mctrl);
174 if (!mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS)) {
176 * Turn off autoRTS if RTS is lowered and restore autoRTS
177 * setting if RTS is raised
179 lcr = serial_in(up, UART_LCR);
180 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
181 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
182 priv->efr |= UART_EFR_RTS;
184 priv->efr &= ~UART_EFR_RTS;
185 serial_out(up, UART_EFR, priv->efr);
186 serial_out(up, UART_LCR, lcr);
190 static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
194 err = pm_runtime_resume_and_get(port->dev);
198 __omap8250_set_mctrl(port, mctrl);
200 pm_runtime_mark_last_busy(port->dev);
201 pm_runtime_put_autosuspend(port->dev);
205 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
206 * The access to uart register after MDR1 Access
207 * causes UART to corrupt data.
210 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
211 * give 10 times as much
213 static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
214 struct omap8250_priv *priv)
216 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
218 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
219 UART_FCR_CLEAR_RCVR);
222 static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
223 struct omap8250_priv *priv)
225 unsigned int uartclk = port->uartclk;
226 unsigned int div_13, div_16;
227 unsigned int abs_d13, abs_d16;
230 * Old custom speed handling.
232 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
233 priv->quot = port->custom_divisor & UART_DIV_MAX;
235 * I assume that nobody is using this. But hey, if somebody
236 * would like to specify the divisor _and_ the mode then the
237 * driver is ready and waiting for it.
239 if (port->custom_divisor & (1 << 16))
240 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
242 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
245 div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
246 div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
253 abs_d13 = abs(baud - uartclk / 13 / div_13);
254 abs_d16 = abs(baud - uartclk / 16 / div_16);
256 if (abs_d13 >= abs_d16) {
257 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
260 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
265 static void omap8250_update_scr(struct uart_8250_port *up,
266 struct omap8250_priv *priv)
270 old_scr = serial_in(up, UART_OMAP_SCR);
271 if (old_scr == priv->scr)
275 * The manual recommends not to enable the DMA mode selector in the SCR
276 * (instead of the FCR) register _and_ selecting the DMA mode as one
277 * register write because this may lead to malfunction.
279 if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
280 serial_out(up, UART_OMAP_SCR,
281 priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
282 serial_out(up, UART_OMAP_SCR, priv->scr);
285 static void omap8250_update_mdr1(struct uart_8250_port *up,
286 struct omap8250_priv *priv)
288 if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
289 omap_8250_mdr1_errataset(up, priv);
291 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
294 static void omap8250_restore_regs(struct uart_8250_port *up)
296 struct omap8250_priv *priv = up->port.private_data;
297 struct uart_8250_dma *dma = up->dma;
298 u8 mcr = serial8250_in_MCR(up);
300 if (dma && dma->tx_running) {
302 * TCSANOW requests the change to occur immediately however if
303 * we have a TX-DMA operation in progress then it has been
304 * observed that it might stall and never complete. Therefore we
305 * delay DMA completes to prevent this hang from happen.
307 priv->delayed_restore = 1;
311 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
312 serial_out(up, UART_EFR, UART_EFR_ECB);
314 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
315 serial8250_out_MCR(up, mcr | UART_MCR_TCRTLR);
316 serial_out(up, UART_FCR, up->fcr);
318 omap8250_update_scr(up, priv);
320 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
322 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
323 OMAP_UART_TCR_HALT(52));
324 serial_out(up, UART_TI752_TLR,
325 TRIGGER_TLR_MASK(priv->tx_trigger) << UART_TI752_TLR_TX |
326 TRIGGER_TLR_MASK(priv->rx_trigger) << UART_TI752_TLR_RX);
328 serial_out(up, UART_LCR, 0);
330 /* drop TCR + TLR access, we setup XON/XOFF later */
331 serial8250_out_MCR(up, mcr);
333 serial_out(up, UART_IER, up->ier);
335 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
336 serial_dl_write(up, priv->quot);
338 serial_out(up, UART_EFR, priv->efr);
340 /* Configure flow control */
341 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
342 serial_out(up, UART_XON1, priv->xon);
343 serial_out(up, UART_XOFF1, priv->xoff);
345 serial_out(up, UART_LCR, up->lcr);
347 omap8250_update_mdr1(up, priv);
349 __omap8250_set_mctrl(&up->port, up->port.mctrl);
351 if (up->port.rs485.flags & SER_RS485_ENABLED)
352 serial8250_em485_stop_tx(up);
356 * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
357 * some differences in how we want to handle flow control.
359 static void omap_8250_set_termios(struct uart_port *port,
360 struct ktermios *termios,
361 const struct ktermios *old)
363 struct uart_8250_port *up = up_to_u8250p(port);
364 struct omap8250_priv *priv = up->port.private_data;
365 unsigned char cval = 0;
368 cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
370 if (termios->c_cflag & CSTOPB)
371 cval |= UART_LCR_STOP;
372 if (termios->c_cflag & PARENB)
373 cval |= UART_LCR_PARITY;
374 if (!(termios->c_cflag & PARODD))
375 cval |= UART_LCR_EPAR;
376 if (termios->c_cflag & CMSPAR)
377 cval |= UART_LCR_SPAR;
380 * Ask the core to calculate the divisor for us.
382 baud = uart_get_baud_rate(port, termios, old,
383 port->uartclk / 16 / UART_DIV_MAX,
385 omap_8250_get_divisor(port, baud, priv);
388 * Ok, we're now changing the port state. Do it with
389 * interrupts disabled.
391 pm_runtime_get_sync(port->dev);
392 spin_lock_irq(&port->lock);
395 * Update the per-port timeout.
397 uart_update_timeout(port, termios->c_cflag, baud);
399 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
400 if (termios->c_iflag & INPCK)
401 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
402 if (termios->c_iflag & (IGNBRK | PARMRK))
403 up->port.read_status_mask |= UART_LSR_BI;
406 * Characters to ignore
408 up->port.ignore_status_mask = 0;
409 if (termios->c_iflag & IGNPAR)
410 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
411 if (termios->c_iflag & IGNBRK) {
412 up->port.ignore_status_mask |= UART_LSR_BI;
414 * If we're ignoring parity and break indicators,
415 * ignore overruns too (for real raw support).
417 if (termios->c_iflag & IGNPAR)
418 up->port.ignore_status_mask |= UART_LSR_OE;
422 * ignore all characters if CREAD is not set
424 if ((termios->c_cflag & CREAD) == 0)
425 up->port.ignore_status_mask |= UART_LSR_DR;
428 * Modem status interrupts
430 up->ier &= ~UART_IER_MSI;
431 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
432 up->ier |= UART_IER_MSI;
435 /* Up to here it was mostly serial8250_do_set_termios() */
438 * We enable TRIG_GRANU for RX and TX and additionally we set
439 * SCR_TX_EMPTY bit. The result is the following:
440 * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
441 * - less than RX_TRIGGER number of bytes will also cause an interrupt
442 * once the UART decides that there no new bytes arriving.
443 * - Once THRE is enabled, the interrupt will be fired once the FIFO is
444 * empty - the trigger level is ignored here.
446 * Once DMA is enabled:
447 * - UART will assert the TX DMA line once there is room for TX_TRIGGER
448 * bytes in the TX FIFO. On each assert the DMA engine will move
449 * TX_TRIGGER bytes into the FIFO.
450 * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
451 * the FIFO and move RX_TRIGGER bytes.
452 * This is because threshold and trigger values are the same.
454 up->fcr = UART_FCR_ENABLE_FIFO;
455 up->fcr |= TRIGGER_FCR_MASK(priv->tx_trigger) << OMAP_UART_FCR_TX_TRIG;
456 up->fcr |= TRIGGER_FCR_MASK(priv->rx_trigger) << OMAP_UART_FCR_RX_TRIG;
458 priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
459 OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
462 priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
463 OMAP_UART_SCR_DMAMODE_CTL;
465 priv->xon = termios->c_cc[VSTART];
466 priv->xoff = termios->c_cc[VSTOP];
469 up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
471 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW &&
472 !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) &&
473 !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_CTS)) {
474 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
475 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
476 priv->efr |= UART_EFR_CTS;
477 } else if (up->port.flags & UPF_SOFT_FLOW) {
479 * OMAP rx s/w flow control is borked; the transmitter remains
480 * stuck off even if rx flow control is subsequently disabled
485 * Enable XON/XOFF flow control on output.
486 * Transmit XON1, XOFF1
488 if (termios->c_iflag & IXOFF) {
489 up->port.status |= UPSTAT_AUTOXOFF;
490 priv->efr |= OMAP_UART_SW_TX;
493 omap8250_restore_regs(up);
495 spin_unlock_irq(&up->port.lock);
496 pm_runtime_mark_last_busy(port->dev);
497 pm_runtime_put_autosuspend(port->dev);
499 /* calculate wakeup latency constraint */
500 priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
501 priv->latency = priv->calc_latency;
503 schedule_work(&priv->qos_work);
505 /* Don't rewrite B0 */
506 if (tty_termios_baud_rate(termios))
507 tty_termios_encode_baud_rate(termios, baud, baud);
510 /* same as 8250 except that we may have extra flow bits set in EFR */
511 static void omap_8250_pm(struct uart_port *port, unsigned int state,
512 unsigned int oldstate)
514 struct uart_8250_port *up = up_to_u8250p(port);
517 pm_runtime_get_sync(port->dev);
518 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
519 efr = serial_in(up, UART_EFR);
520 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
521 serial_out(up, UART_LCR, 0);
523 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
524 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
525 serial_out(up, UART_EFR, efr);
526 serial_out(up, UART_LCR, 0);
528 pm_runtime_mark_last_busy(port->dev);
529 pm_runtime_put_autosuspend(port->dev);
532 static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
533 struct omap8250_priv *priv)
535 static const struct soc_device_attribute k3_soc_devices[] = {
536 { .family = "AM65X", },
537 { .family = "J721E", .revision = "SR1.0" },
541 u16 revision, major, minor;
543 mvr = uart_read(priv, UART_OMAP_MVER);
545 /* Check revision register scheme */
546 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
549 case 0: /* Legacy Scheme: OMAP2/3 */
550 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
551 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
552 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
553 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
556 /* New Scheme: OMAP4+ */
557 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
558 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
559 OMAP_UART_MVR_MAJ_SHIFT;
560 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
563 dev_warn(up->port.dev,
564 "Unknown revision, defaulting to highest\n");
565 /* highest possible revision */
569 /* normalize revision for the driver */
570 revision = UART_BUILD_REVISION(major, minor);
573 case OMAP_UART_REV_46:
574 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
576 case OMAP_UART_REV_52:
577 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
578 OMAP_UART_WER_HAS_TX_WAKEUP;
580 case OMAP_UART_REV_63:
581 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
582 OMAP_UART_WER_HAS_TX_WAKEUP;
589 * AM65x SR1.0, AM65x SR2.0 and J721e SR1.0 don't
590 * don't have RHR_IT_DIS bit in IER2 register. So drop to flag
591 * to enable errata workaround.
593 if (soc_device_match(k3_soc_devices))
594 priv->habit &= ~UART_HAS_RHR_IT_DIS;
597 static void omap8250_uart_qos_work(struct work_struct *work)
599 struct omap8250_priv *priv;
601 priv = container_of(work, struct omap8250_priv, qos_work);
602 cpu_latency_qos_update_request(&priv->pm_qos_request, priv->latency);
605 #ifdef CONFIG_SERIAL_8250_DMA
606 static int omap_8250_dma_handle_irq(struct uart_port *port);
609 static irqreturn_t omap8250_irq(int irq, void *dev_id)
611 struct uart_port *port = dev_id;
612 struct omap8250_priv *priv = port->private_data;
613 struct uart_8250_port *up = up_to_u8250p(port);
614 unsigned int iir, lsr;
617 #ifdef CONFIG_SERIAL_8250_DMA
619 ret = omap_8250_dma_handle_irq(port);
620 return IRQ_RETVAL(ret);
624 serial8250_rpm_get(up);
625 lsr = serial_port_in(port, UART_LSR);
626 iir = serial_port_in(port, UART_IIR);
627 ret = serial8250_handle_irq(port, iir);
630 * On K3 SoCs, it is observed that RX TIMEOUT is signalled after
631 * FIFO has been drained, in which case a dummy read of RX FIFO
632 * is required to clear RX TIMEOUT condition.
634 if (priv->habit & UART_RX_TIMEOUT_QUIRK &&
635 (iir & UART_IIR_RX_TIMEOUT) == UART_IIR_RX_TIMEOUT &&
636 serial_port_in(port, UART_OMAP_RX_LVL) == 0) {
637 serial_port_in(port, UART_RX);
640 /* Stop processing interrupts on input overrun */
641 if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
644 /* Synchronize UART_IER access against the console. */
645 spin_lock(&port->lock);
646 up->ier = port->serial_in(port, UART_IER);
647 if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
648 port->ops->stop_rx(port);
650 /* Keep restarting the timer until
651 * the input overrun subsides.
653 cancel_delayed_work(&up->overrun_backoff);
655 spin_unlock(&port->lock);
657 delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
658 schedule_delayed_work(&up->overrun_backoff, delay);
661 serial8250_rpm_put(up);
663 return IRQ_RETVAL(ret);
666 static int omap_8250_startup(struct uart_port *port)
668 struct uart_8250_port *up = up_to_u8250p(port);
669 struct omap8250_priv *priv = port->private_data;
673 ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
678 pm_runtime_get_sync(port->dev);
680 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
682 serial_out(up, UART_LCR, UART_LCR_WLEN8);
684 up->lsr_saved_flags = 0;
685 up->msr_saved_flags = 0;
687 /* Disable DMA for console UART */
688 if (uart_console(port))
692 ret = serial8250_request_dma(up);
694 dev_warn_ratelimited(port->dev,
695 "failed to request DMA\n");
700 ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
701 dev_name(port->dev), port);
705 up->ier = UART_IER_RLSI | UART_IER_RDI;
706 serial_out(up, UART_IER, up->ier);
709 up->capabilities |= UART_CAP_RPM;
712 /* Enable module level wake up */
713 priv->wer = OMAP_UART_WER_MOD_WKUP;
714 if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
715 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
716 serial_out(up, UART_OMAP_WER, priv->wer);
718 if (up->dma && !(priv->habit & UART_HAS_EFR2))
721 pm_runtime_mark_last_busy(port->dev);
722 pm_runtime_put_autosuspend(port->dev);
725 pm_runtime_mark_last_busy(port->dev);
726 pm_runtime_put_autosuspend(port->dev);
727 dev_pm_clear_wake_irq(port->dev);
731 static void omap_8250_shutdown(struct uart_port *port)
733 struct uart_8250_port *up = up_to_u8250p(port);
734 struct omap8250_priv *priv = port->private_data;
736 flush_work(&priv->qos_work);
738 omap_8250_rx_dma_flush(up);
740 pm_runtime_get_sync(port->dev);
742 serial_out(up, UART_OMAP_WER, 0);
743 if (priv->habit & UART_HAS_EFR2)
744 serial_out(up, UART_OMAP_EFR2, 0x0);
747 serial_out(up, UART_IER, 0);
750 serial8250_release_dma(up);
753 * Disable break condition and FIFOs
755 if (up->lcr & UART_LCR_SBC)
756 serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
757 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
759 pm_runtime_mark_last_busy(port->dev);
760 pm_runtime_put_autosuspend(port->dev);
761 free_irq(port->irq, port);
762 dev_pm_clear_wake_irq(port->dev);
765 static void omap_8250_throttle(struct uart_port *port)
767 struct omap8250_priv *priv = port->private_data;
770 pm_runtime_get_sync(port->dev);
772 spin_lock_irqsave(&port->lock, flags);
773 port->ops->stop_rx(port);
774 priv->throttled = true;
775 spin_unlock_irqrestore(&port->lock, flags);
777 pm_runtime_mark_last_busy(port->dev);
778 pm_runtime_put_autosuspend(port->dev);
781 static void omap_8250_unthrottle(struct uart_port *port)
783 struct omap8250_priv *priv = port->private_data;
784 struct uart_8250_port *up = up_to_u8250p(port);
787 pm_runtime_get_sync(port->dev);
789 spin_lock_irqsave(&port->lock, flags);
790 priv->throttled = false;
793 up->ier |= UART_IER_RLSI | UART_IER_RDI;
794 port->read_status_mask |= UART_LSR_DR;
795 serial_out(up, UART_IER, up->ier);
796 spin_unlock_irqrestore(&port->lock, flags);
798 pm_runtime_mark_last_busy(port->dev);
799 pm_runtime_put_autosuspend(port->dev);
802 #ifdef CONFIG_SERIAL_8250_DMA
803 static int omap_8250_rx_dma(struct uart_8250_port *p);
805 /* Must be called while priv->rx_dma_lock is held */
806 static void __dma_rx_do_complete(struct uart_8250_port *p)
808 struct uart_8250_dma *dma = p->dma;
809 struct tty_port *tty_port = &p->port.state->port;
810 struct omap8250_priv *priv = p->port.private_data;
811 struct dma_chan *rxchan = dma->rxchan;
813 struct dma_tx_state state;
818 if (!dma->rx_running)
821 cookie = dma->rx_cookie;
824 /* Re-enable RX FIFO interrupt now that transfer is complete */
825 if (priv->habit & UART_HAS_RHR_IT_DIS) {
826 reg = serial_in(p, UART_OMAP_IER2);
827 reg &= ~UART_OMAP_IER2_RHR_IT_DIS;
828 serial_out(p, UART_OMAP_IER2, reg);
831 dmaengine_tx_status(rxchan, cookie, &state);
833 count = dma->rx_size - state.residue + state.in_flight_bytes;
834 if (count < dma->rx_size) {
835 dmaengine_terminate_async(rxchan);
838 * Poll for teardown to complete which guarantees in
839 * flight data is drained.
841 if (state.in_flight_bytes) {
844 while (dmaengine_tx_status(rxchan, cookie, NULL) &&
848 if (poll_count == -1)
849 dev_err(p->port.dev, "teardown incomplete\n");
854 ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
856 p->port.icount.rx += ret;
857 p->port.icount.buf_overrun += count - ret;
860 tty_flip_buffer_push(tty_port);
863 static void __dma_rx_complete(void *param)
865 struct uart_8250_port *p = param;
866 struct omap8250_priv *priv = p->port.private_data;
867 struct uart_8250_dma *dma = p->dma;
868 struct dma_tx_state state;
871 spin_lock_irqsave(&p->port.lock, flags);
874 * If the tx status is not DMA_COMPLETE, then this is a delayed
875 * completion callback. A previous RX timeout flush would have
876 * already pushed the data, so exit.
878 if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) !=
880 spin_unlock_irqrestore(&p->port.lock, flags);
883 __dma_rx_do_complete(p);
884 if (!priv->throttled) {
885 p->ier |= UART_IER_RLSI | UART_IER_RDI;
886 serial_out(p, UART_IER, p->ier);
887 if (!(priv->habit & UART_HAS_EFR2))
891 spin_unlock_irqrestore(&p->port.lock, flags);
894 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
896 struct omap8250_priv *priv = p->port.private_data;
897 struct uart_8250_dma *dma = p->dma;
898 struct dma_tx_state state;
902 spin_lock_irqsave(&priv->rx_dma_lock, flags);
904 if (!dma->rx_running) {
905 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
909 ret = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
910 if (ret == DMA_IN_PROGRESS) {
911 ret = dmaengine_pause(dma->rxchan);
912 if (WARN_ON_ONCE(ret))
913 priv->rx_dma_broken = true;
915 __dma_rx_do_complete(p);
916 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
919 static int omap_8250_rx_dma(struct uart_8250_port *p)
921 struct omap8250_priv *priv = p->port.private_data;
922 struct uart_8250_dma *dma = p->dma;
924 struct dma_async_tx_descriptor *desc;
928 if (priv->rx_dma_broken)
931 spin_lock_irqsave(&priv->rx_dma_lock, flags);
933 if (dma->rx_running) {
934 enum dma_status state;
936 state = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, NULL);
937 if (state == DMA_COMPLETE) {
939 * Disable RX interrupts to allow RX DMA completion
942 p->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
943 serial_out(p, UART_IER, p->ier);
948 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
949 dma->rx_size, DMA_DEV_TO_MEM,
950 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
957 desc->callback = __dma_rx_complete;
958 desc->callback_param = p;
960 dma->rx_cookie = dmaengine_submit(desc);
963 * Disable RX FIFO interrupt while RX DMA is enabled, else
964 * spurious interrupt may be raised when data is in the RX FIFO
965 * but is yet to be drained by DMA.
967 if (priv->habit & UART_HAS_RHR_IT_DIS) {
968 reg = serial_in(p, UART_OMAP_IER2);
969 reg |= UART_OMAP_IER2_RHR_IT_DIS;
970 serial_out(p, UART_OMAP_IER2, reg);
973 dma_async_issue_pending(dma->rxchan);
975 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
979 static int omap_8250_tx_dma(struct uart_8250_port *p);
981 static void omap_8250_dma_tx_complete(void *param)
983 struct uart_8250_port *p = param;
984 struct uart_8250_dma *dma = p->dma;
985 struct circ_buf *xmit = &p->port.state->xmit;
987 bool en_thri = false;
988 struct omap8250_priv *priv = p->port.private_data;
990 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
991 UART_XMIT_SIZE, DMA_TO_DEVICE);
993 spin_lock_irqsave(&p->port.lock, flags);
997 uart_xmit_advance(&p->port, dma->tx_size);
999 if (priv->delayed_restore) {
1000 priv->delayed_restore = 0;
1001 omap8250_restore_regs(p);
1004 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1005 uart_write_wakeup(&p->port);
1007 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
1010 ret = omap_8250_tx_dma(p);
1013 } else if (p->capabilities & UART_CAP_RPM) {
1019 serial8250_set_THRI(p);
1022 spin_unlock_irqrestore(&p->port.lock, flags);
1025 static int omap_8250_tx_dma(struct uart_8250_port *p)
1027 struct uart_8250_dma *dma = p->dma;
1028 struct omap8250_priv *priv = p->port.private_data;
1029 struct circ_buf *xmit = &p->port.state->xmit;
1030 struct dma_async_tx_descriptor *desc;
1031 unsigned int skip_byte = 0;
1034 if (dma->tx_running)
1036 if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
1039 * Even if no data, we need to return an error for the two cases
1040 * below so serial8250_tx_chars() is invoked and properly clears
1041 * THRI and/or runtime suspend.
1043 if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
1047 serial8250_clear_THRI(p);
1051 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1052 if (priv->habit & OMAP_DMA_TX_KICK) {
1056 * We need to put the first byte into the FIFO in order to start
1057 * the DMA transfer. For transfers smaller than four bytes we
1058 * don't bother doing DMA at all. It seem not matter if there
1059 * are still bytes in the FIFO from the last transfer (in case
1060 * we got here directly from omap_8250_dma_tx_complete()). Bytes
1061 * leaving the FIFO seem not to trigger the DMA transfer. It is
1062 * really the byte that we put into the FIFO.
1063 * If the FIFO is already full then we most likely got here from
1064 * omap_8250_dma_tx_complete(). And this means the DMA engine
1065 * just completed its work. We don't have to wait the complete
1066 * 86us at 115200,8n1 but around 60us (not to mention lower
1067 * baudrates). So in that case we take the interrupt and try
1068 * again with an empty FIFO.
1070 tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
1071 if (tx_lvl == p->tx_loadsz) {
1075 if (dma->tx_size < 4) {
1082 desc = dmaengine_prep_slave_single(dma->txchan,
1083 dma->tx_addr + xmit->tail + skip_byte,
1084 dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
1085 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1091 dma->tx_running = 1;
1093 desc->callback = omap_8250_dma_tx_complete;
1094 desc->callback_param = p;
1096 dma->tx_cookie = dmaengine_submit(desc);
1098 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
1099 UART_XMIT_SIZE, DMA_TO_DEVICE);
1101 dma_async_issue_pending(dma->txchan);
1105 serial8250_clear_THRI(p);
1107 serial_out(p, UART_TX, xmit->buf[xmit->tail]);
1114 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1116 switch (iir & 0x3f) {
1118 case UART_IIR_RX_TIMEOUT:
1120 omap_8250_rx_dma_flush(up);
1123 return omap_8250_rx_dma(up);
1126 static u16 omap_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir, u16 status)
1128 if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
1129 (iir & UART_IIR_RDI)) {
1130 if (handle_rx_dma(up, iir)) {
1131 status = serial8250_rx_chars(up, status);
1132 omap_8250_rx_dma(up);
1139 static void am654_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir,
1143 * Queue a new transfer if FIFO has data.
1145 if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
1146 (up->ier & UART_IER_RDI)) {
1147 omap_8250_rx_dma(up);
1148 serial_out(up, UART_OMAP_EFR2, UART_OMAP_EFR2_TIMEOUT_BEHAVE);
1149 } else if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
1151 * Disable RX timeout, read IIR to clear
1152 * current timeout condition, clear EFR2 to
1153 * periodic timeouts, re-enable interrupts.
1155 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1156 serial_out(up, UART_IER, up->ier);
1157 omap_8250_rx_dma_flush(up);
1158 serial_in(up, UART_IIR);
1159 serial_out(up, UART_OMAP_EFR2, 0x0);
1160 up->ier |= UART_IER_RLSI | UART_IER_RDI;
1161 serial_out(up, UART_IER, up->ier);
1166 * This is mostly serial8250_handle_irq(). We have a slightly different DMA
1167 * hoook for RX/TX and need different logic for them in the ISR. Therefore we
1168 * use the default routine in the non-DMA case and this one for with DMA.
1170 static int omap_8250_dma_handle_irq(struct uart_port *port)
1172 struct uart_8250_port *up = up_to_u8250p(port);
1173 struct omap8250_priv *priv = up->port.private_data;
1177 serial8250_rpm_get(up);
1179 iir = serial_port_in(port, UART_IIR);
1180 if (iir & UART_IIR_NO_INT) {
1181 serial8250_rpm_put(up);
1185 spin_lock(&port->lock);
1187 status = serial_port_in(port, UART_LSR);
1189 if ((iir & 0x3f) != UART_IIR_THRI) {
1190 if (priv->habit & UART_HAS_EFR2)
1191 am654_8250_handle_rx_dma(up, iir, status);
1193 status = omap_8250_handle_rx_dma(up, iir, status);
1196 serial8250_modem_status(up);
1197 if (status & UART_LSR_THRE && up->dma->tx_err) {
1198 if (uart_tx_stopped(&up->port) ||
1199 uart_circ_empty(&up->port.state->xmit)) {
1200 up->dma->tx_err = 0;
1201 serial8250_tx_chars(up);
1204 * try again due to an earlier failer which
1205 * might have been resolved by now.
1207 if (omap_8250_tx_dma(up))
1208 serial8250_tx_chars(up);
1212 uart_unlock_and_check_sysrq(port);
1214 serial8250_rpm_put(up);
1218 static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1225 static inline int omap_8250_rx_dma(struct uart_8250_port *p)
1231 static int omap8250_no_handle_irq(struct uart_port *port)
1233 /* IRQ has not been requested but handling irq? */
1234 WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1238 static struct omap8250_dma_params am654_dma = {
1241 .tx_trigger = TX_TRIGGER,
1244 static struct omap8250_dma_params am33xx_dma = {
1245 .rx_size = RX_TRIGGER,
1246 .rx_trigger = RX_TRIGGER,
1247 .tx_trigger = TX_TRIGGER,
1250 static struct omap8250_platdata am654_platdata = {
1251 .dma_params = &am654_dma,
1252 .habit = UART_HAS_EFR2 | UART_HAS_RHR_IT_DIS |
1253 UART_RX_TIMEOUT_QUIRK,
1256 static struct omap8250_platdata am33xx_platdata = {
1257 .dma_params = &am33xx_dma,
1258 .habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE,
1261 static struct omap8250_platdata omap4_platdata = {
1262 .dma_params = &am33xx_dma,
1263 .habit = UART_ERRATA_CLOCK_DISABLE,
1266 static const struct of_device_id omap8250_dt_ids[] = {
1267 { .compatible = "ti,am654-uart", .data = &am654_platdata, },
1268 { .compatible = "ti,omap2-uart" },
1269 { .compatible = "ti,omap3-uart" },
1270 { .compatible = "ti,omap4-uart", .data = &omap4_platdata, },
1271 { .compatible = "ti,am3352-uart", .data = &am33xx_platdata, },
1272 { .compatible = "ti,am4372-uart", .data = &am33xx_platdata, },
1273 { .compatible = "ti,dra742-uart", .data = &omap4_platdata, },
1276 MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1278 static int omap8250_probe(struct platform_device *pdev)
1280 struct device_node *np = pdev->dev.of_node;
1281 struct omap8250_priv *priv;
1282 const struct omap8250_platdata *pdata;
1283 struct uart_8250_port up;
1284 struct resource *regs;
1285 void __iomem *membase;
1288 irq = platform_get_irq(pdev, 0);
1292 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1294 dev_err(&pdev->dev, "missing registers\n");
1298 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1302 membase = devm_ioremap(&pdev->dev, regs->start,
1303 resource_size(regs));
1307 memset(&up, 0, sizeof(up));
1308 up.port.dev = &pdev->dev;
1309 up.port.mapbase = regs->start;
1310 up.port.membase = membase;
1313 * It claims to be 16C750 compatible however it is a little different.
1314 * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1315 * have) is enabled via EFR instead of MCR. The type is set here 8250
1316 * just to get things going. UNKNOWN does not work for a few reasons and
1317 * we don't need our own type since we don't use 8250's set_termios()
1320 up.port.type = PORT_8250;
1321 up.port.iotype = UPIO_MEM;
1322 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
1324 up.port.private_data = priv;
1326 up.port.regshift = OMAP_UART_REGSHIFT;
1327 up.port.fifosize = 64;
1329 up.capabilities = UART_CAP_FIFO;
1332 * Runtime PM is mostly transparent. However to do it right we need to a
1333 * TX empty interrupt before we can put the device to auto idle. So if
1334 * PM is not enabled we don't add that flag and can spare that one extra
1335 * interrupt in the TX path.
1337 up.capabilities |= UART_CAP_RPM;
1339 up.port.set_termios = omap_8250_set_termios;
1340 up.port.set_mctrl = omap8250_set_mctrl;
1341 up.port.pm = omap_8250_pm;
1342 up.port.startup = omap_8250_startup;
1343 up.port.shutdown = omap_8250_shutdown;
1344 up.port.throttle = omap_8250_throttle;
1345 up.port.unthrottle = omap_8250_unthrottle;
1346 up.port.rs485_config = serial8250_em485_config;
1347 up.port.rs485_supported = serial8250_em485_supported;
1348 up.rs485_start_tx = serial8250_em485_start_tx;
1349 up.rs485_stop_tx = serial8250_em485_stop_tx;
1350 up.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
1352 ret = of_alias_get_id(np, "serial");
1354 dev_err(&pdev->dev, "failed to get alias\n");
1359 if (of_property_read_u32(np, "clock-frequency", &up.port.uartclk)) {
1362 clk = devm_clk_get(&pdev->dev, NULL);
1364 if (PTR_ERR(clk) == -EPROBE_DEFER)
1365 return -EPROBE_DEFER;
1367 up.port.uartclk = clk_get_rate(clk);
1371 if (of_property_read_u32(np, "overrun-throttle-ms",
1372 &up.overrun_backoff_time_ms) != 0)
1373 up.overrun_backoff_time_ms = 0;
1375 priv->wakeirq = irq_of_parse_and_map(np, 1);
1377 pdata = of_device_get_match_data(&pdev->dev);
1379 priv->habit |= pdata->habit;
1381 if (!up.port.uartclk) {
1382 up.port.uartclk = DEFAULT_CLK_SPEED;
1383 dev_warn(&pdev->dev,
1384 "No clock speed specified: using default: %d\n",
1388 priv->membase = membase;
1389 priv->line = -ENODEV;
1390 priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1391 priv->calc_latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1392 cpu_latency_qos_add_request(&priv->pm_qos_request, priv->latency);
1393 INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1395 spin_lock_init(&priv->rx_dma_lock);
1397 platform_set_drvdata(pdev, priv);
1399 device_init_wakeup(&pdev->dev, true);
1400 pm_runtime_enable(&pdev->dev);
1401 pm_runtime_use_autosuspend(&pdev->dev);
1404 * Disable runtime PM until autosuspend delay unless specifically
1405 * enabled by the user via sysfs. This is the historic way to
1406 * prevent an unsafe default policy with lossy characters on wake-up.
1407 * For serdev devices this is not needed, the policy can be managed by
1408 * the serdev driver.
1410 if (!of_get_available_child_count(pdev->dev.of_node))
1411 pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1413 pm_runtime_irq_safe(&pdev->dev);
1415 pm_runtime_get_sync(&pdev->dev);
1417 omap_serial_fill_features_erratas(&up, priv);
1418 up.port.handle_irq = omap8250_no_handle_irq;
1419 priv->rx_trigger = RX_TRIGGER;
1420 priv->tx_trigger = TX_TRIGGER;
1421 #ifdef CONFIG_SERIAL_8250_DMA
1423 * Oh DMA support. If there are no DMA properties in the DT then
1424 * we will fall back to a generic DMA channel which does not
1425 * really work here. To ensure that we do not get a generic DMA
1426 * channel assigned, we have the the_no_dma_filter_fn() here.
1427 * To avoid "failed to request DMA" messages we check for DMA
1430 ret = of_property_count_strings(np, "dma-names");
1432 struct omap8250_dma_params *dma_params = NULL;
1434 up.dma = &priv->omap8250_dma;
1435 up.dma->fn = the_no_dma_filter_fn;
1436 up.dma->tx_dma = omap_8250_tx_dma;
1437 up.dma->rx_dma = omap_8250_rx_dma;
1439 dma_params = pdata->dma_params;
1442 up.dma->rx_size = dma_params->rx_size;
1443 up.dma->rxconf.src_maxburst = dma_params->rx_trigger;
1444 up.dma->txconf.dst_maxburst = dma_params->tx_trigger;
1445 priv->rx_trigger = dma_params->rx_trigger;
1446 priv->tx_trigger = dma_params->tx_trigger;
1448 up.dma->rx_size = RX_TRIGGER;
1449 up.dma->rxconf.src_maxburst = RX_TRIGGER;
1450 up.dma->txconf.dst_maxburst = TX_TRIGGER;
1454 ret = serial8250_register_8250_port(&up);
1456 dev_err(&pdev->dev, "unable to register 8250 port\n");
1460 pm_runtime_mark_last_busy(&pdev->dev);
1461 pm_runtime_put_autosuspend(&pdev->dev);
1464 pm_runtime_dont_use_autosuspend(&pdev->dev);
1465 pm_runtime_put_sync(&pdev->dev);
1466 flush_work(&priv->qos_work);
1467 pm_runtime_disable(&pdev->dev);
1468 cpu_latency_qos_remove_request(&priv->pm_qos_request);
1472 static int omap8250_remove(struct platform_device *pdev)
1474 struct omap8250_priv *priv = platform_get_drvdata(pdev);
1477 err = pm_runtime_resume_and_get(&pdev->dev);
1481 serial8250_unregister_port(priv->line);
1482 priv->line = -ENODEV;
1483 pm_runtime_dont_use_autosuspend(&pdev->dev);
1484 pm_runtime_put_sync(&pdev->dev);
1485 flush_work(&priv->qos_work);
1486 pm_runtime_disable(&pdev->dev);
1487 cpu_latency_qos_remove_request(&priv->pm_qos_request);
1488 device_init_wakeup(&pdev->dev, false);
1492 static int omap8250_prepare(struct device *dev)
1494 struct omap8250_priv *priv = dev_get_drvdata(dev);
1498 priv->is_suspending = true;
1502 static void omap8250_complete(struct device *dev)
1504 struct omap8250_priv *priv = dev_get_drvdata(dev);
1508 priv->is_suspending = false;
1511 static int omap8250_suspend(struct device *dev)
1513 struct omap8250_priv *priv = dev_get_drvdata(dev);
1514 struct uart_8250_port *up = serial8250_get_port(priv->line);
1517 serial8250_suspend_port(priv->line);
1519 err = pm_runtime_resume_and_get(dev);
1522 if (!device_may_wakeup(dev))
1524 serial_out(up, UART_OMAP_WER, priv->wer);
1525 if (uart_console(&up->port) && console_suspend_enabled)
1526 err = pm_runtime_force_suspend(dev);
1527 flush_work(&priv->qos_work);
1532 static int omap8250_resume(struct device *dev)
1534 struct omap8250_priv *priv = dev_get_drvdata(dev);
1535 struct uart_8250_port *up = serial8250_get_port(priv->line);
1538 if (uart_console(&up->port) && console_suspend_enabled) {
1539 err = pm_runtime_force_resume(dev);
1544 serial8250_resume_port(priv->line);
1545 /* Paired with pm_runtime_resume_and_get() in omap8250_suspend() */
1546 pm_runtime_mark_last_busy(dev);
1547 pm_runtime_put_autosuspend(dev);
1552 static int omap8250_lost_context(struct uart_8250_port *up)
1556 val = serial_in(up, UART_OMAP_SCR);
1558 * If we lose context, then SCR is set to its reset value of zero.
1559 * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
1560 * among other bits, to never set the register back to zero again.
1567 static void uart_write(struct omap8250_priv *priv, u32 reg, u32 val)
1569 writel(val, priv->membase + (reg << OMAP_UART_REGSHIFT));
1572 /* TODO: in future, this should happen via API in drivers/reset/ */
1573 static int omap8250_soft_reset(struct device *dev)
1575 struct omap8250_priv *priv = dev_get_drvdata(dev);
1581 * At least on omap4, unused uarts may not idle after reset without
1582 * a basic scr dma configuration even with no dma in use. The
1583 * module clkctrl status bits will be 1 instead of 3 blocking idle
1584 * for the whole clockdomain. The softreset below will clear scr,
1585 * and we restore it on resume so this is safe to do on all SoCs
1586 * needing omap8250_soft_reset() quirk. Do it in two writes as
1587 * recommended in the comment for omap8250_update_scr().
1589 uart_write(priv, UART_OMAP_SCR, OMAP_UART_SCR_DMAMODE_1);
1590 uart_write(priv, UART_OMAP_SCR,
1591 OMAP_UART_SCR_DMAMODE_1 | OMAP_UART_SCR_DMAMODE_CTL);
1593 sysc = uart_read(priv, UART_OMAP_SYSC);
1595 /* softreset the UART */
1596 sysc |= OMAP_UART_SYSC_SOFTRESET;
1597 uart_write(priv, UART_OMAP_SYSC, sysc);
1599 /* By experiments, 1us enough for reset complete on AM335x */
1602 syss = uart_read(priv, UART_OMAP_SYSS);
1603 } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
1606 dev_err(dev, "timed out waiting for reset done\n");
1613 static int omap8250_runtime_suspend(struct device *dev)
1615 struct omap8250_priv *priv = dev_get_drvdata(dev);
1616 struct uart_8250_port *up = NULL;
1618 if (priv->line >= 0)
1619 up = serial8250_get_port(priv->line);
1621 if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
1624 ret = omap8250_soft_reset(dev);
1629 /* Restore to UART mode after reset (for wakeup) */
1630 omap8250_update_mdr1(up, priv);
1631 /* Restore wakeup enable register */
1632 serial_out(up, UART_OMAP_WER, priv->wer);
1636 if (up && up->dma && up->dma->rxchan)
1637 omap_8250_rx_dma_flush(up);
1639 priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1640 schedule_work(&priv->qos_work);
1645 static int omap8250_runtime_resume(struct device *dev)
1647 struct omap8250_priv *priv = dev_get_drvdata(dev);
1648 struct uart_8250_port *up = NULL;
1650 if (priv->line >= 0)
1651 up = serial8250_get_port(priv->line);
1653 if (up && omap8250_lost_context(up))
1654 omap8250_restore_regs(up);
1656 if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2))
1657 omap_8250_rx_dma(up);
1659 priv->latency = priv->calc_latency;
1660 schedule_work(&priv->qos_work);
1664 #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1665 static int __init omap8250_console_fixup(void)
1671 if (strstr(boot_command_line, "console=ttyS"))
1672 /* user set a ttyS based name for the console */
1675 omap_str = strstr(boot_command_line, "console=ttyO");
1677 /* user did not set ttyO based console, so we don't care */
1681 if ('0' <= *omap_str && *omap_str <= '9')
1682 idx = *omap_str - '0';
1687 if (omap_str[0] == ',') {
1694 add_preferred_console("ttyS", idx, options);
1695 pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1697 pr_err("This ensures that you still see kernel messages. Please\n");
1698 pr_err("update your kernel commandline.\n");
1701 console_initcall(omap8250_console_fixup);
1704 static const struct dev_pm_ops omap8250_dev_pm_ops = {
1705 SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1706 RUNTIME_PM_OPS(omap8250_runtime_suspend,
1707 omap8250_runtime_resume, NULL)
1708 .prepare = pm_sleep_ptr(omap8250_prepare),
1709 .complete = pm_sleep_ptr(omap8250_complete),
1712 static struct platform_driver omap8250_platform_driver = {
1715 .pm = pm_ptr(&omap8250_dev_pm_ops),
1716 .of_match_table = omap8250_dt_ids,
1718 .probe = omap8250_probe,
1719 .remove = omap8250_remove,
1721 module_platform_driver(omap8250_platform_driver);
1723 MODULE_AUTHOR("Sebastian Andrzej Siewior");
1724 MODULE_DESCRIPTION("OMAP 8250 Driver");
1725 MODULE_LICENSE("GPL v2");