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)
48 #define UART_HAS_NATIVE_RS485 BIT(7)
50 #define OMAP_UART_FCR_RX_TRIG 6
51 #define OMAP_UART_FCR_TX_TRIG 4
53 /* SCR register bitmasks */
54 #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
55 #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
56 #define OMAP_UART_SCR_TX_EMPTY (1 << 3)
57 #define OMAP_UART_SCR_DMAMODE_MASK (3 << 1)
58 #define OMAP_UART_SCR_DMAMODE_1 (1 << 1)
59 #define OMAP_UART_SCR_DMAMODE_CTL (1 << 0)
61 /* MVR register bitmasks */
62 #define OMAP_UART_MVR_SCHEME_SHIFT 30
63 #define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
64 #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
65 #define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
66 #define OMAP_UART_MVR_MAJ_MASK 0x700
67 #define OMAP_UART_MVR_MAJ_SHIFT 8
68 #define OMAP_UART_MVR_MIN_MASK 0x3f
70 /* SYSC register bitmasks */
71 #define OMAP_UART_SYSC_SOFTRESET (1 << 1)
73 /* SYSS register bitmasks */
74 #define OMAP_UART_SYSS_RESETDONE (1 << 0)
76 #define UART_TI752_TLR_TX 0
77 #define UART_TI752_TLR_RX 4
79 #define TRIGGER_TLR_MASK(x) ((x & 0x3c) >> 2)
80 #define TRIGGER_FCR_MASK(x) (x & 3)
82 /* Enable XON/XOFF flow control on output */
83 #define OMAP_UART_SW_TX 0x08
84 /* Enable XON/XOFF flow control on input */
85 #define OMAP_UART_SW_RX 0x02
87 #define OMAP_UART_WER_MOD_WKUP 0x7f
88 #define OMAP_UART_TX_WAKEUP_EN (1 << 7)
93 #define OMAP_UART_TCR_RESTORE(x) ((x / 4) << 4)
94 #define OMAP_UART_TCR_HALT(x) ((x / 4) << 0)
96 #define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
98 #define OMAP_UART_REV_46 0x0406
99 #define OMAP_UART_REV_52 0x0502
100 #define OMAP_UART_REV_63 0x0603
102 /* Interrupt Enable Register 2 */
103 #define UART_OMAP_IER2 0x1B
104 #define UART_OMAP_IER2_RHR_IT_DIS BIT(2)
106 /* Mode Definition Register 3 */
107 #define UART_OMAP_MDR3 0x20
108 #define UART_OMAP_MDR3_DIR_POL BIT(3)
109 #define UART_OMAP_MDR3_DIR_EN BIT(4)
111 /* Enhanced features register 2 */
112 #define UART_OMAP_EFR2 0x23
113 #define UART_OMAP_EFR2_TIMEOUT_BEHAVE BIT(6)
115 /* RX FIFO occupancy indicator */
116 #define UART_OMAP_RX_LVL 0x19
118 struct omap8250_priv {
119 void __iomem *membase;
139 struct pm_qos_request pm_qos_request;
140 struct work_struct qos_work;
141 struct uart_8250_dma omap8250_dma;
142 spinlock_t rx_dma_lock;
147 struct omap8250_dma_params {
153 struct omap8250_platdata {
154 struct omap8250_dma_params *dma_params;
158 #ifdef CONFIG_SERIAL_8250_DMA
159 static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
161 static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
164 static u32 uart_read(struct omap8250_priv *priv, u32 reg)
166 return readl(priv->membase + (reg << OMAP_UART_REGSHIFT));
169 static void uart_write(struct omap8250_priv *priv, u32 reg, u32 val)
171 writel(val, priv->membase + (reg << OMAP_UART_REGSHIFT));
175 * Called on runtime PM resume path from omap8250_restore_regs(), and
176 * omap8250_set_mctrl().
178 static void __omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
180 struct uart_8250_port *up = up_to_u8250p(port);
181 struct omap8250_priv *priv = up->port.private_data;
184 serial8250_do_set_mctrl(port, mctrl);
186 if (!mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS)) {
188 * Turn off autoRTS if RTS is lowered and restore autoRTS
189 * setting if RTS is raised
191 lcr = serial_in(up, UART_LCR);
192 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
193 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
194 priv->efr |= UART_EFR_RTS;
196 priv->efr &= ~UART_EFR_RTS;
197 serial_out(up, UART_EFR, priv->efr);
198 serial_out(up, UART_LCR, lcr);
202 static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
206 err = pm_runtime_resume_and_get(port->dev);
210 __omap8250_set_mctrl(port, mctrl);
212 pm_runtime_mark_last_busy(port->dev);
213 pm_runtime_put_autosuspend(port->dev);
217 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
218 * The access to uart register after MDR1 Access
219 * causes UART to corrupt data.
222 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
223 * give 10 times as much
225 static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
226 struct omap8250_priv *priv)
228 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
230 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
231 UART_FCR_CLEAR_RCVR);
234 static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
235 struct omap8250_priv *priv)
237 unsigned int uartclk = port->uartclk;
238 unsigned int div_13, div_16;
239 unsigned int abs_d13, abs_d16;
242 * Old custom speed handling.
244 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
245 priv->quot = port->custom_divisor & UART_DIV_MAX;
247 * I assume that nobody is using this. But hey, if somebody
248 * would like to specify the divisor _and_ the mode then the
249 * driver is ready and waiting for it.
251 if (port->custom_divisor & (1 << 16))
252 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
254 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
257 div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
258 div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
265 abs_d13 = abs(baud - uartclk / 13 / div_13);
266 abs_d16 = abs(baud - uartclk / 16 / div_16);
268 if (abs_d13 >= abs_d16) {
269 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
272 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
277 static void omap8250_update_scr(struct uart_8250_port *up,
278 struct omap8250_priv *priv)
282 old_scr = serial_in(up, UART_OMAP_SCR);
283 if (old_scr == priv->scr)
287 * The manual recommends not to enable the DMA mode selector in the SCR
288 * (instead of the FCR) register _and_ selecting the DMA mode as one
289 * register write because this may lead to malfunction.
291 if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
292 serial_out(up, UART_OMAP_SCR,
293 priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
294 serial_out(up, UART_OMAP_SCR, priv->scr);
297 static void omap8250_update_mdr1(struct uart_8250_port *up,
298 struct omap8250_priv *priv)
300 if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
301 omap_8250_mdr1_errataset(up, priv);
303 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
306 static void omap8250_restore_regs(struct uart_8250_port *up)
308 struct omap8250_priv *priv = up->port.private_data;
309 struct uart_8250_dma *dma = up->dma;
310 u8 mcr = serial8250_in_MCR(up);
312 if (dma && dma->tx_running) {
314 * TCSANOW requests the change to occur immediately however if
315 * we have a TX-DMA operation in progress then it has been
316 * observed that it might stall and never complete. Therefore we
317 * delay DMA completes to prevent this hang from happen.
319 priv->delayed_restore = 1;
323 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
324 serial_out(up, UART_EFR, UART_EFR_ECB);
326 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
327 serial8250_out_MCR(up, mcr | UART_MCR_TCRTLR);
328 serial_out(up, UART_FCR, up->fcr);
330 omap8250_update_scr(up, priv);
332 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
334 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
335 OMAP_UART_TCR_HALT(52));
336 serial_out(up, UART_TI752_TLR,
337 TRIGGER_TLR_MASK(priv->tx_trigger) << UART_TI752_TLR_TX |
338 TRIGGER_TLR_MASK(priv->rx_trigger) << UART_TI752_TLR_RX);
340 serial_out(up, UART_LCR, 0);
342 /* drop TCR + TLR access, we setup XON/XOFF later */
343 serial8250_out_MCR(up, mcr);
345 serial_out(up, UART_IER, up->ier);
347 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
348 serial_dl_write(up, priv->quot);
350 serial_out(up, UART_EFR, priv->efr);
352 /* Configure flow control */
353 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
354 serial_out(up, UART_XON1, priv->xon);
355 serial_out(up, UART_XOFF1, priv->xoff);
357 serial_out(up, UART_LCR, up->lcr);
359 omap8250_update_mdr1(up, priv);
361 __omap8250_set_mctrl(&up->port, up->port.mctrl);
363 serial_out(up, UART_OMAP_MDR3, priv->mdr3);
365 if (up->port.rs485.flags & SER_RS485_ENABLED &&
366 up->port.rs485_config == serial8250_em485_config)
367 serial8250_em485_stop_tx(up);
371 * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
372 * some differences in how we want to handle flow control.
374 static void omap_8250_set_termios(struct uart_port *port,
375 struct ktermios *termios,
376 const struct ktermios *old)
378 struct uart_8250_port *up = up_to_u8250p(port);
379 struct omap8250_priv *priv = up->port.private_data;
380 unsigned char cval = 0;
383 cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
385 if (termios->c_cflag & CSTOPB)
386 cval |= UART_LCR_STOP;
387 if (termios->c_cflag & PARENB)
388 cval |= UART_LCR_PARITY;
389 if (!(termios->c_cflag & PARODD))
390 cval |= UART_LCR_EPAR;
391 if (termios->c_cflag & CMSPAR)
392 cval |= UART_LCR_SPAR;
395 * Ask the core to calculate the divisor for us.
397 baud = uart_get_baud_rate(port, termios, old,
398 port->uartclk / 16 / UART_DIV_MAX,
400 omap_8250_get_divisor(port, baud, priv);
403 * Ok, we're now changing the port state. Do it with
404 * interrupts disabled.
406 pm_runtime_get_sync(port->dev);
407 spin_lock_irq(&port->lock);
410 * Update the per-port timeout.
412 uart_update_timeout(port, termios->c_cflag, baud);
414 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
415 if (termios->c_iflag & INPCK)
416 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
417 if (termios->c_iflag & (IGNBRK | PARMRK))
418 up->port.read_status_mask |= UART_LSR_BI;
421 * Characters to ignore
423 up->port.ignore_status_mask = 0;
424 if (termios->c_iflag & IGNPAR)
425 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
426 if (termios->c_iflag & IGNBRK) {
427 up->port.ignore_status_mask |= UART_LSR_BI;
429 * If we're ignoring parity and break indicators,
430 * ignore overruns too (for real raw support).
432 if (termios->c_iflag & IGNPAR)
433 up->port.ignore_status_mask |= UART_LSR_OE;
437 * ignore all characters if CREAD is not set
439 if ((termios->c_cflag & CREAD) == 0)
440 up->port.ignore_status_mask |= UART_LSR_DR;
443 * Modem status interrupts
445 up->ier &= ~UART_IER_MSI;
446 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
447 up->ier |= UART_IER_MSI;
450 /* Up to here it was mostly serial8250_do_set_termios() */
453 * We enable TRIG_GRANU for RX and TX and additionally we set
454 * SCR_TX_EMPTY bit. The result is the following:
455 * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
456 * - less than RX_TRIGGER number of bytes will also cause an interrupt
457 * once the UART decides that there no new bytes arriving.
458 * - Once THRE is enabled, the interrupt will be fired once the FIFO is
459 * empty - the trigger level is ignored here.
461 * Once DMA is enabled:
462 * - UART will assert the TX DMA line once there is room for TX_TRIGGER
463 * bytes in the TX FIFO. On each assert the DMA engine will move
464 * TX_TRIGGER bytes into the FIFO.
465 * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
466 * the FIFO and move RX_TRIGGER bytes.
467 * This is because threshold and trigger values are the same.
469 up->fcr = UART_FCR_ENABLE_FIFO;
470 up->fcr |= TRIGGER_FCR_MASK(priv->tx_trigger) << OMAP_UART_FCR_TX_TRIG;
471 up->fcr |= TRIGGER_FCR_MASK(priv->rx_trigger) << OMAP_UART_FCR_RX_TRIG;
473 priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
474 OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
477 priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
478 OMAP_UART_SCR_DMAMODE_CTL;
480 priv->xon = termios->c_cc[VSTART];
481 priv->xoff = termios->c_cc[VSTOP];
484 up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
486 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW &&
487 !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) &&
488 !mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_CTS)) {
489 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
490 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
491 priv->efr |= UART_EFR_CTS;
492 } else if (up->port.flags & UPF_SOFT_FLOW) {
494 * OMAP rx s/w flow control is borked; the transmitter remains
495 * stuck off even if rx flow control is subsequently disabled
500 * Enable XON/XOFF flow control on output.
501 * Transmit XON1, XOFF1
503 if (termios->c_iflag & IXOFF) {
504 up->port.status |= UPSTAT_AUTOXOFF;
505 priv->efr |= OMAP_UART_SW_TX;
508 omap8250_restore_regs(up);
510 spin_unlock_irq(&up->port.lock);
511 pm_runtime_mark_last_busy(port->dev);
512 pm_runtime_put_autosuspend(port->dev);
514 /* calculate wakeup latency constraint */
515 priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
516 priv->latency = priv->calc_latency;
518 schedule_work(&priv->qos_work);
520 /* Don't rewrite B0 */
521 if (tty_termios_baud_rate(termios))
522 tty_termios_encode_baud_rate(termios, baud, baud);
525 /* same as 8250 except that we may have extra flow bits set in EFR */
526 static void omap_8250_pm(struct uart_port *port, unsigned int state,
527 unsigned int oldstate)
529 struct uart_8250_port *up = up_to_u8250p(port);
532 pm_runtime_get_sync(port->dev);
533 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
534 efr = serial_in(up, UART_EFR);
535 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
536 serial_out(up, UART_LCR, 0);
538 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
539 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
540 serial_out(up, UART_EFR, efr);
541 serial_out(up, UART_LCR, 0);
543 pm_runtime_mark_last_busy(port->dev);
544 pm_runtime_put_autosuspend(port->dev);
547 static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
548 struct omap8250_priv *priv)
550 static const struct soc_device_attribute k3_soc_devices[] = {
551 { .family = "AM65X", },
552 { .family = "J721E", .revision = "SR1.0" },
556 u16 revision, major, minor;
558 mvr = uart_read(priv, UART_OMAP_MVER);
560 /* Check revision register scheme */
561 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
564 case 0: /* Legacy Scheme: OMAP2/3 */
565 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
566 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
567 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
568 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
571 /* New Scheme: OMAP4+ */
572 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
573 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
574 OMAP_UART_MVR_MAJ_SHIFT;
575 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
578 dev_warn(up->port.dev,
579 "Unknown revision, defaulting to highest\n");
580 /* highest possible revision */
584 /* normalize revision for the driver */
585 revision = UART_BUILD_REVISION(major, minor);
588 case OMAP_UART_REV_46:
589 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
591 case OMAP_UART_REV_52:
592 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
593 OMAP_UART_WER_HAS_TX_WAKEUP;
595 case OMAP_UART_REV_63:
596 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
597 OMAP_UART_WER_HAS_TX_WAKEUP;
604 * AM65x SR1.0, AM65x SR2.0 and J721e SR1.0 don't
605 * don't have RHR_IT_DIS bit in IER2 register. So drop to flag
606 * to enable errata workaround.
608 if (soc_device_match(k3_soc_devices))
609 priv->habit &= ~UART_HAS_RHR_IT_DIS;
612 static void omap8250_uart_qos_work(struct work_struct *work)
614 struct omap8250_priv *priv;
616 priv = container_of(work, struct omap8250_priv, qos_work);
617 cpu_latency_qos_update_request(&priv->pm_qos_request, priv->latency);
620 #ifdef CONFIG_SERIAL_8250_DMA
621 static int omap_8250_dma_handle_irq(struct uart_port *port);
624 static irqreturn_t omap8250_irq(int irq, void *dev_id)
626 struct omap8250_priv *priv = dev_id;
627 struct uart_8250_port *up = serial8250_get_port(priv->line);
628 struct uart_port *port = &up->port;
629 unsigned int iir, lsr;
632 #ifdef CONFIG_SERIAL_8250_DMA
634 ret = omap_8250_dma_handle_irq(port);
635 return IRQ_RETVAL(ret);
639 serial8250_rpm_get(up);
640 lsr = serial_port_in(port, UART_LSR);
641 iir = serial_port_in(port, UART_IIR);
642 ret = serial8250_handle_irq(port, iir);
645 * On K3 SoCs, it is observed that RX TIMEOUT is signalled after
646 * FIFO has been drained, in which case a dummy read of RX FIFO
647 * is required to clear RX TIMEOUT condition.
649 if (priv->habit & UART_RX_TIMEOUT_QUIRK &&
650 (iir & UART_IIR_RX_TIMEOUT) == UART_IIR_RX_TIMEOUT &&
651 serial_port_in(port, UART_OMAP_RX_LVL) == 0) {
652 serial_port_in(port, UART_RX);
655 /* Stop processing interrupts on input overrun */
656 if ((lsr & UART_LSR_OE) && up->overrun_backoff_time_ms > 0) {
659 up->ier = port->serial_in(port, UART_IER);
660 if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
661 port->ops->stop_rx(port);
663 /* Keep restarting the timer until
664 * the input overrun subsides.
666 cancel_delayed_work(&up->overrun_backoff);
669 delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
670 schedule_delayed_work(&up->overrun_backoff, delay);
673 serial8250_rpm_put(up);
675 return IRQ_RETVAL(ret);
678 static int omap_8250_startup(struct uart_port *port)
680 struct uart_8250_port *up = up_to_u8250p(port);
681 struct omap8250_priv *priv = port->private_data;
685 ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
690 pm_runtime_get_sync(port->dev);
692 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
694 serial_out(up, UART_LCR, UART_LCR_WLEN8);
696 up->lsr_saved_flags = 0;
697 up->msr_saved_flags = 0;
699 /* Disable DMA for console UART */
700 if (uart_console(port))
704 ret = serial8250_request_dma(up);
706 dev_warn_ratelimited(port->dev,
707 "failed to request DMA\n");
712 up->ier = UART_IER_RLSI | UART_IER_RDI;
713 serial_out(up, UART_IER, up->ier);
716 up->capabilities |= UART_CAP_RPM;
719 /* Enable module level wake up */
720 priv->wer = OMAP_UART_WER_MOD_WKUP;
721 if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
722 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
723 serial_out(up, UART_OMAP_WER, priv->wer);
725 if (up->dma && !(priv->habit & UART_HAS_EFR2))
728 enable_irq(up->port.irq);
730 pm_runtime_mark_last_busy(port->dev);
731 pm_runtime_put_autosuspend(port->dev);
735 static void omap_8250_shutdown(struct uart_port *port)
737 struct uart_8250_port *up = up_to_u8250p(port);
738 struct omap8250_priv *priv = port->private_data;
740 flush_work(&priv->qos_work);
742 omap_8250_rx_dma_flush(up);
744 pm_runtime_get_sync(port->dev);
746 serial_out(up, UART_OMAP_WER, 0);
747 if (priv->habit & UART_HAS_EFR2)
748 serial_out(up, UART_OMAP_EFR2, 0x0);
751 serial_out(up, UART_IER, 0);
752 disable_irq_nosync(up->port.irq);
753 dev_pm_clear_wake_irq(port->dev);
756 serial8250_release_dma(up);
759 * Disable break condition and FIFOs
761 if (up->lcr & UART_LCR_SBC)
762 serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
763 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
765 pm_runtime_mark_last_busy(port->dev);
766 pm_runtime_put_autosuspend(port->dev);
769 static void omap_8250_throttle(struct uart_port *port)
771 struct omap8250_priv *priv = port->private_data;
774 pm_runtime_get_sync(port->dev);
776 spin_lock_irqsave(&port->lock, flags);
777 port->ops->stop_rx(port);
778 priv->throttled = true;
779 spin_unlock_irqrestore(&port->lock, flags);
781 pm_runtime_mark_last_busy(port->dev);
782 pm_runtime_put_autosuspend(port->dev);
785 static void omap_8250_unthrottle(struct uart_port *port)
787 struct omap8250_priv *priv = port->private_data;
788 struct uart_8250_port *up = up_to_u8250p(port);
791 pm_runtime_get_sync(port->dev);
793 spin_lock_irqsave(&port->lock, flags);
794 priv->throttled = false;
797 up->ier |= UART_IER_RLSI | UART_IER_RDI;
798 port->read_status_mask |= UART_LSR_DR;
799 serial_out(up, UART_IER, up->ier);
800 spin_unlock_irqrestore(&port->lock, flags);
802 pm_runtime_mark_last_busy(port->dev);
803 pm_runtime_put_autosuspend(port->dev);
806 static int omap8250_rs485_config(struct uart_port *port,
807 struct ktermios *termios,
808 struct serial_rs485 *rs485)
810 struct omap8250_priv *priv = port->private_data;
811 struct uart_8250_port *up = up_to_u8250p(port);
812 u32 fixed_delay_rts_before_send = 0;
813 u32 fixed_delay_rts_after_send = 0;
817 * There is a fixed delay of 3 bit clock cycles after the TX shift
818 * register is going empty to allow time for the stop bit to transition
819 * through the transceiver before direction is changed to receive.
821 * Additionally there appears to be a 1 bit clock delay between writing
822 * to the THR register and transmission of the start bit, per page 8783
823 * of the AM65 TRM: https://www.ti.com/lit/ug/spruid7e/spruid7e.pdf
826 if (priv->mdr1 == UART_OMAP_MDR1_16X_MODE)
827 baud = port->uartclk / (16 * priv->quot);
829 baud = port->uartclk / (13 * priv->quot);
831 fixed_delay_rts_after_send = 3 * MSEC_PER_SEC / baud;
832 fixed_delay_rts_before_send = 1 * MSEC_PER_SEC / baud;
836 * Fall back to RS485 software emulation if the UART is missing
837 * hardware support, if the device tree specifies an mctrl_gpio
838 * (indicates that RTS is unavailable due to a pinmux conflict)
839 * or if the requested delays exceed the fixed hardware delays.
841 if (!(priv->habit & UART_HAS_NATIVE_RS485) ||
842 mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS) ||
843 rs485->delay_rts_after_send > fixed_delay_rts_after_send ||
844 rs485->delay_rts_before_send > fixed_delay_rts_before_send) {
845 priv->mdr3 &= ~UART_OMAP_MDR3_DIR_EN;
846 serial_out(up, UART_OMAP_MDR3, priv->mdr3);
848 port->rs485_config = serial8250_em485_config;
849 return serial8250_em485_config(port, termios, rs485);
852 rs485->delay_rts_after_send = fixed_delay_rts_after_send;
853 rs485->delay_rts_before_send = fixed_delay_rts_before_send;
855 if (rs485->flags & SER_RS485_ENABLED)
856 priv->mdr3 |= UART_OMAP_MDR3_DIR_EN;
858 priv->mdr3 &= ~UART_OMAP_MDR3_DIR_EN;
861 * Retain same polarity semantics as RS485 software emulation,
862 * i.e. SER_RS485_RTS_ON_SEND means driving RTS low on send.
864 if (rs485->flags & SER_RS485_RTS_ON_SEND)
865 priv->mdr3 &= ~UART_OMAP_MDR3_DIR_POL;
867 priv->mdr3 |= UART_OMAP_MDR3_DIR_POL;
869 serial_out(up, UART_OMAP_MDR3, priv->mdr3);
874 #ifdef CONFIG_SERIAL_8250_DMA
875 static int omap_8250_rx_dma(struct uart_8250_port *p);
877 /* Must be called while priv->rx_dma_lock is held */
878 static void __dma_rx_do_complete(struct uart_8250_port *p)
880 struct uart_8250_dma *dma = p->dma;
881 struct tty_port *tty_port = &p->port.state->port;
882 struct omap8250_priv *priv = p->port.private_data;
883 struct dma_chan *rxchan = dma->rxchan;
885 struct dma_tx_state state;
890 if (!dma->rx_running)
893 cookie = dma->rx_cookie;
896 /* Re-enable RX FIFO interrupt now that transfer is complete */
897 if (priv->habit & UART_HAS_RHR_IT_DIS) {
898 reg = serial_in(p, UART_OMAP_IER2);
899 reg &= ~UART_OMAP_IER2_RHR_IT_DIS;
900 serial_out(p, UART_OMAP_IER2, UART_OMAP_IER2_RHR_IT_DIS);
903 dmaengine_tx_status(rxchan, cookie, &state);
905 count = dma->rx_size - state.residue + state.in_flight_bytes;
906 if (count < dma->rx_size) {
907 dmaengine_terminate_async(rxchan);
910 * Poll for teardown to complete which guarantees in
911 * flight data is drained.
913 if (state.in_flight_bytes) {
916 while (dmaengine_tx_status(rxchan, cookie, NULL) &&
920 if (poll_count == -1)
921 dev_err(p->port.dev, "teardown incomplete\n");
926 ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
928 p->port.icount.rx += ret;
929 p->port.icount.buf_overrun += count - ret;
932 tty_flip_buffer_push(tty_port);
935 static void __dma_rx_complete(void *param)
937 struct uart_8250_port *p = param;
938 struct omap8250_priv *priv = p->port.private_data;
939 struct uart_8250_dma *dma = p->dma;
940 struct dma_tx_state state;
943 spin_lock_irqsave(&p->port.lock, flags);
946 * If the tx status is not DMA_COMPLETE, then this is a delayed
947 * completion callback. A previous RX timeout flush would have
948 * already pushed the data, so exit.
950 if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) !=
952 spin_unlock_irqrestore(&p->port.lock, flags);
955 __dma_rx_do_complete(p);
956 if (!priv->throttled) {
957 p->ier |= UART_IER_RLSI | UART_IER_RDI;
958 serial_out(p, UART_IER, p->ier);
959 if (!(priv->habit & UART_HAS_EFR2))
963 spin_unlock_irqrestore(&p->port.lock, flags);
966 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
968 struct omap8250_priv *priv = p->port.private_data;
969 struct uart_8250_dma *dma = p->dma;
970 struct dma_tx_state state;
974 spin_lock_irqsave(&priv->rx_dma_lock, flags);
976 if (!dma->rx_running) {
977 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
981 ret = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
982 if (ret == DMA_IN_PROGRESS) {
983 ret = dmaengine_pause(dma->rxchan);
984 if (WARN_ON_ONCE(ret))
985 priv->rx_dma_broken = true;
987 __dma_rx_do_complete(p);
988 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
991 static int omap_8250_rx_dma(struct uart_8250_port *p)
993 struct omap8250_priv *priv = p->port.private_data;
994 struct uart_8250_dma *dma = p->dma;
996 struct dma_async_tx_descriptor *desc;
1000 if (priv->rx_dma_broken)
1003 spin_lock_irqsave(&priv->rx_dma_lock, flags);
1005 if (dma->rx_running) {
1006 enum dma_status state;
1008 state = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, NULL);
1009 if (state == DMA_COMPLETE) {
1011 * Disable RX interrupts to allow RX DMA completion
1014 p->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1015 serial_out(p, UART_IER, p->ier);
1020 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
1021 dma->rx_size, DMA_DEV_TO_MEM,
1022 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1028 dma->rx_running = 1;
1029 desc->callback = __dma_rx_complete;
1030 desc->callback_param = p;
1032 dma->rx_cookie = dmaengine_submit(desc);
1035 * Disable RX FIFO interrupt while RX DMA is enabled, else
1036 * spurious interrupt may be raised when data is in the RX FIFO
1037 * but is yet to be drained by DMA.
1039 if (priv->habit & UART_HAS_RHR_IT_DIS) {
1040 reg = serial_in(p, UART_OMAP_IER2);
1041 reg |= UART_OMAP_IER2_RHR_IT_DIS;
1042 serial_out(p, UART_OMAP_IER2, UART_OMAP_IER2_RHR_IT_DIS);
1045 dma_async_issue_pending(dma->rxchan);
1047 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
1051 static int omap_8250_tx_dma(struct uart_8250_port *p);
1053 static void omap_8250_dma_tx_complete(void *param)
1055 struct uart_8250_port *p = param;
1056 struct uart_8250_dma *dma = p->dma;
1057 struct circ_buf *xmit = &p->port.state->xmit;
1058 unsigned long flags;
1059 bool en_thri = false;
1060 struct omap8250_priv *priv = p->port.private_data;
1062 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
1063 UART_XMIT_SIZE, DMA_TO_DEVICE);
1065 spin_lock_irqsave(&p->port.lock, flags);
1067 dma->tx_running = 0;
1069 uart_xmit_advance(&p->port, dma->tx_size);
1071 if (priv->delayed_restore) {
1072 priv->delayed_restore = 0;
1073 omap8250_restore_regs(p);
1076 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1077 uart_write_wakeup(&p->port);
1079 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
1082 ret = omap_8250_tx_dma(p);
1085 } else if (p->capabilities & UART_CAP_RPM) {
1091 serial8250_set_THRI(p);
1094 spin_unlock_irqrestore(&p->port.lock, flags);
1097 static int omap_8250_tx_dma(struct uart_8250_port *p)
1099 struct uart_8250_dma *dma = p->dma;
1100 struct omap8250_priv *priv = p->port.private_data;
1101 struct circ_buf *xmit = &p->port.state->xmit;
1102 struct dma_async_tx_descriptor *desc;
1103 unsigned int skip_byte = 0;
1106 if (dma->tx_running)
1108 if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
1111 * Even if no data, we need to return an error for the two cases
1112 * below so serial8250_tx_chars() is invoked and properly clears
1113 * THRI and/or runtime suspend.
1115 if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
1119 serial8250_clear_THRI(p);
1123 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1124 if (priv->habit & OMAP_DMA_TX_KICK) {
1128 * We need to put the first byte into the FIFO in order to start
1129 * the DMA transfer. For transfers smaller than four bytes we
1130 * don't bother doing DMA at all. It seem not matter if there
1131 * are still bytes in the FIFO from the last transfer (in case
1132 * we got here directly from omap_8250_dma_tx_complete()). Bytes
1133 * leaving the FIFO seem not to trigger the DMA transfer. It is
1134 * really the byte that we put into the FIFO.
1135 * If the FIFO is already full then we most likely got here from
1136 * omap_8250_dma_tx_complete(). And this means the DMA engine
1137 * just completed its work. We don't have to wait the complete
1138 * 86us at 115200,8n1 but around 60us (not to mention lower
1139 * baudrates). So in that case we take the interrupt and try
1140 * again with an empty FIFO.
1142 tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
1143 if (tx_lvl == p->tx_loadsz) {
1147 if (dma->tx_size < 4) {
1154 desc = dmaengine_prep_slave_single(dma->txchan,
1155 dma->tx_addr + xmit->tail + skip_byte,
1156 dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
1157 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1163 dma->tx_running = 1;
1165 desc->callback = omap_8250_dma_tx_complete;
1166 desc->callback_param = p;
1168 dma->tx_cookie = dmaengine_submit(desc);
1170 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
1171 UART_XMIT_SIZE, DMA_TO_DEVICE);
1173 dma_async_issue_pending(dma->txchan);
1177 serial8250_clear_THRI(p);
1179 serial_out(p, UART_TX, xmit->buf[xmit->tail]);
1186 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1188 switch (iir & 0x3f) {
1190 case UART_IIR_RX_TIMEOUT:
1192 omap_8250_rx_dma_flush(up);
1195 return omap_8250_rx_dma(up);
1198 static u16 omap_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir, u16 status)
1200 if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
1201 (iir & UART_IIR_RDI)) {
1202 if (handle_rx_dma(up, iir)) {
1203 status = serial8250_rx_chars(up, status);
1204 omap_8250_rx_dma(up);
1211 static void am654_8250_handle_rx_dma(struct uart_8250_port *up, u8 iir,
1215 * Queue a new transfer if FIFO has data.
1217 if ((status & (UART_LSR_DR | UART_LSR_BI)) &&
1218 (up->ier & UART_IER_RDI)) {
1219 omap_8250_rx_dma(up);
1220 serial_out(up, UART_OMAP_EFR2, UART_OMAP_EFR2_TIMEOUT_BEHAVE);
1221 } else if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
1223 * Disable RX timeout, read IIR to clear
1224 * current timeout condition, clear EFR2 to
1225 * periodic timeouts, re-enable interrupts.
1227 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1228 serial_out(up, UART_IER, up->ier);
1229 omap_8250_rx_dma_flush(up);
1230 serial_in(up, UART_IIR);
1231 serial_out(up, UART_OMAP_EFR2, 0x0);
1232 up->ier |= UART_IER_RLSI | UART_IER_RDI;
1233 serial_out(up, UART_IER, up->ier);
1238 * This is mostly serial8250_handle_irq(). We have a slightly different DMA
1239 * hoook for RX/TX and need different logic for them in the ISR. Therefore we
1240 * use the default routine in the non-DMA case and this one for with DMA.
1242 static int omap_8250_dma_handle_irq(struct uart_port *port)
1244 struct uart_8250_port *up = up_to_u8250p(port);
1245 struct omap8250_priv *priv = up->port.private_data;
1249 serial8250_rpm_get(up);
1251 iir = serial_port_in(port, UART_IIR);
1252 if (iir & UART_IIR_NO_INT) {
1253 serial8250_rpm_put(up);
1257 spin_lock(&port->lock);
1259 status = serial_port_in(port, UART_LSR);
1261 if (priv->habit & UART_HAS_EFR2)
1262 am654_8250_handle_rx_dma(up, iir, status);
1264 status = omap_8250_handle_rx_dma(up, iir, status);
1266 serial8250_modem_status(up);
1267 if (status & UART_LSR_THRE && up->dma->tx_err) {
1268 if (uart_tx_stopped(&up->port) ||
1269 uart_circ_empty(&up->port.state->xmit)) {
1270 up->dma->tx_err = 0;
1271 serial8250_tx_chars(up);
1274 * try again due to an earlier failer which
1275 * might have been resolved by now.
1277 if (omap_8250_tx_dma(up))
1278 serial8250_tx_chars(up);
1282 uart_unlock_and_check_sysrq(port);
1284 serial8250_rpm_put(up);
1288 static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1295 static inline int omap_8250_rx_dma(struct uart_8250_port *p)
1301 static int omap8250_no_handle_irq(struct uart_port *port)
1303 /* IRQ has not been requested but handling irq? */
1304 WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1308 static struct omap8250_dma_params am654_dma = {
1311 .tx_trigger = TX_TRIGGER,
1314 static struct omap8250_dma_params am33xx_dma = {
1315 .rx_size = RX_TRIGGER,
1316 .rx_trigger = RX_TRIGGER,
1317 .tx_trigger = TX_TRIGGER,
1320 static struct omap8250_platdata am654_platdata = {
1321 .dma_params = &am654_dma,
1322 .habit = UART_HAS_EFR2 | UART_HAS_RHR_IT_DIS |
1323 UART_RX_TIMEOUT_QUIRK | UART_HAS_NATIVE_RS485,
1326 static struct omap8250_platdata am33xx_platdata = {
1327 .dma_params = &am33xx_dma,
1328 .habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE,
1331 static struct omap8250_platdata omap4_platdata = {
1332 .dma_params = &am33xx_dma,
1333 .habit = UART_ERRATA_CLOCK_DISABLE,
1336 static const struct of_device_id omap8250_dt_ids[] = {
1337 { .compatible = "ti,am654-uart", .data = &am654_platdata, },
1338 { .compatible = "ti,omap2-uart" },
1339 { .compatible = "ti,omap3-uart" },
1340 { .compatible = "ti,omap4-uart", .data = &omap4_platdata, },
1341 { .compatible = "ti,am3352-uart", .data = &am33xx_platdata, },
1342 { .compatible = "ti,am4372-uart", .data = &am33xx_platdata, },
1343 { .compatible = "ti,dra742-uart", .data = &omap4_platdata, },
1346 MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1348 static int omap8250_probe(struct platform_device *pdev)
1350 struct device_node *np = pdev->dev.of_node;
1351 struct omap8250_priv *priv;
1352 const struct omap8250_platdata *pdata;
1353 struct uart_8250_port up;
1354 struct resource *regs;
1355 void __iomem *membase;
1358 irq = platform_get_irq(pdev, 0);
1362 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1364 dev_err(&pdev->dev, "missing registers\n");
1368 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1372 membase = devm_ioremap(&pdev->dev, regs->start,
1373 resource_size(regs));
1377 memset(&up, 0, sizeof(up));
1378 up.port.dev = &pdev->dev;
1379 up.port.mapbase = regs->start;
1380 up.port.membase = membase;
1383 * It claims to be 16C750 compatible however it is a little different.
1384 * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1385 * have) is enabled via EFR instead of MCR. The type is set here 8250
1386 * just to get things going. UNKNOWN does not work for a few reasons and
1387 * we don't need our own type since we don't use 8250's set_termios()
1390 up.port.type = PORT_8250;
1391 up.port.iotype = UPIO_MEM;
1392 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
1394 up.port.private_data = priv;
1396 up.port.regshift = OMAP_UART_REGSHIFT;
1397 up.port.fifosize = 64;
1399 up.capabilities = UART_CAP_FIFO;
1402 * Runtime PM is mostly transparent. However to do it right we need to a
1403 * TX empty interrupt before we can put the device to auto idle. So if
1404 * PM is not enabled we don't add that flag and can spare that one extra
1405 * interrupt in the TX path.
1407 up.capabilities |= UART_CAP_RPM;
1409 up.port.set_termios = omap_8250_set_termios;
1410 up.port.set_mctrl = omap8250_set_mctrl;
1411 up.port.pm = omap_8250_pm;
1412 up.port.startup = omap_8250_startup;
1413 up.port.shutdown = omap_8250_shutdown;
1414 up.port.throttle = omap_8250_throttle;
1415 up.port.unthrottle = omap_8250_unthrottle;
1416 up.port.rs485_config = omap8250_rs485_config;
1417 /* same rs485_supported for software emulation and native RS485 */
1418 up.port.rs485_supported = serial8250_em485_supported;
1419 up.rs485_start_tx = serial8250_em485_start_tx;
1420 up.rs485_stop_tx = serial8250_em485_stop_tx;
1421 up.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
1423 ret = of_alias_get_id(np, "serial");
1425 dev_err(&pdev->dev, "failed to get alias\n");
1430 if (of_property_read_u32(np, "clock-frequency", &up.port.uartclk)) {
1433 clk = devm_clk_get(&pdev->dev, NULL);
1435 if (PTR_ERR(clk) == -EPROBE_DEFER)
1436 return -EPROBE_DEFER;
1438 up.port.uartclk = clk_get_rate(clk);
1442 if (of_property_read_u32(np, "overrun-throttle-ms",
1443 &up.overrun_backoff_time_ms) != 0)
1444 up.overrun_backoff_time_ms = 0;
1446 pdata = of_device_get_match_data(&pdev->dev);
1448 priv->habit |= pdata->habit;
1450 if (!up.port.uartclk) {
1451 up.port.uartclk = DEFAULT_CLK_SPEED;
1452 dev_warn(&pdev->dev,
1453 "No clock speed specified: using default: %d\n",
1457 priv->membase = membase;
1458 priv->line = -ENODEV;
1459 priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1460 priv->calc_latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1461 cpu_latency_qos_add_request(&priv->pm_qos_request, priv->latency);
1462 INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1464 spin_lock_init(&priv->rx_dma_lock);
1466 platform_set_drvdata(pdev, priv);
1468 device_init_wakeup(&pdev->dev, true);
1469 pm_runtime_enable(&pdev->dev);
1470 pm_runtime_use_autosuspend(&pdev->dev);
1473 * Disable runtime PM until autosuspend delay unless specifically
1474 * enabled by the user via sysfs. This is the historic way to
1475 * prevent an unsafe default policy with lossy characters on wake-up.
1476 * For serdev devices this is not needed, the policy can be managed by
1477 * the serdev driver.
1479 if (!of_get_available_child_count(pdev->dev.of_node))
1480 pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1482 pm_runtime_irq_safe(&pdev->dev);
1484 pm_runtime_get_sync(&pdev->dev);
1486 omap_serial_fill_features_erratas(&up, priv);
1487 up.port.handle_irq = omap8250_no_handle_irq;
1488 priv->rx_trigger = RX_TRIGGER;
1489 priv->tx_trigger = TX_TRIGGER;
1490 #ifdef CONFIG_SERIAL_8250_DMA
1492 * Oh DMA support. If there are no DMA properties in the DT then
1493 * we will fall back to a generic DMA channel which does not
1494 * really work here. To ensure that we do not get a generic DMA
1495 * channel assigned, we have the the_no_dma_filter_fn() here.
1496 * To avoid "failed to request DMA" messages we check for DMA
1499 ret = of_property_count_strings(np, "dma-names");
1501 struct omap8250_dma_params *dma_params = NULL;
1503 up.dma = &priv->omap8250_dma;
1504 up.dma->fn = the_no_dma_filter_fn;
1505 up.dma->tx_dma = omap_8250_tx_dma;
1506 up.dma->rx_dma = omap_8250_rx_dma;
1508 dma_params = pdata->dma_params;
1511 up.dma->rx_size = dma_params->rx_size;
1512 up.dma->rxconf.src_maxburst = dma_params->rx_trigger;
1513 up.dma->txconf.dst_maxburst = dma_params->tx_trigger;
1514 priv->rx_trigger = dma_params->rx_trigger;
1515 priv->tx_trigger = dma_params->tx_trigger;
1517 up.dma->rx_size = RX_TRIGGER;
1518 up.dma->rxconf.src_maxburst = RX_TRIGGER;
1519 up.dma->txconf.dst_maxburst = TX_TRIGGER;
1524 irq_set_status_flags(irq, IRQ_NOAUTOEN);
1525 ret = devm_request_irq(&pdev->dev, irq, omap8250_irq, 0,
1526 dev_name(&pdev->dev), priv);
1530 priv->wakeirq = irq_of_parse_and_map(np, 1);
1532 ret = serial8250_register_8250_port(&up);
1534 dev_err(&pdev->dev, "unable to register 8250 port\n");
1538 pm_runtime_mark_last_busy(&pdev->dev);
1539 pm_runtime_put_autosuspend(&pdev->dev);
1542 pm_runtime_dont_use_autosuspend(&pdev->dev);
1543 pm_runtime_put_sync(&pdev->dev);
1544 flush_work(&priv->qos_work);
1545 pm_runtime_disable(&pdev->dev);
1546 cpu_latency_qos_remove_request(&priv->pm_qos_request);
1550 static int omap8250_remove(struct platform_device *pdev)
1552 struct omap8250_priv *priv = platform_get_drvdata(pdev);
1555 err = pm_runtime_resume_and_get(&pdev->dev);
1559 serial8250_unregister_port(priv->line);
1560 priv->line = -ENODEV;
1561 pm_runtime_dont_use_autosuspend(&pdev->dev);
1562 pm_runtime_put_sync(&pdev->dev);
1563 flush_work(&priv->qos_work);
1564 pm_runtime_disable(&pdev->dev);
1565 cpu_latency_qos_remove_request(&priv->pm_qos_request);
1566 device_init_wakeup(&pdev->dev, false);
1570 #ifdef CONFIG_PM_SLEEP
1571 static int omap8250_prepare(struct device *dev)
1573 struct omap8250_priv *priv = dev_get_drvdata(dev);
1577 priv->is_suspending = true;
1581 static void omap8250_complete(struct device *dev)
1583 struct omap8250_priv *priv = dev_get_drvdata(dev);
1587 priv->is_suspending = false;
1590 static int omap8250_suspend(struct device *dev)
1592 struct omap8250_priv *priv = dev_get_drvdata(dev);
1593 struct uart_8250_port *up = serial8250_get_port(priv->line);
1595 serial8250_suspend_port(priv->line);
1597 pm_runtime_get_sync(dev);
1598 if (!device_may_wakeup(dev))
1600 serial_out(up, UART_OMAP_WER, priv->wer);
1601 pm_runtime_mark_last_busy(dev);
1602 pm_runtime_put_autosuspend(dev);
1604 flush_work(&priv->qos_work);
1608 static int omap8250_resume(struct device *dev)
1610 struct omap8250_priv *priv = dev_get_drvdata(dev);
1612 serial8250_resume_port(priv->line);
1616 #define omap8250_prepare NULL
1617 #define omap8250_complete NULL
1621 static int omap8250_lost_context(struct uart_8250_port *up)
1625 val = serial_in(up, UART_OMAP_SCR);
1627 * If we lose context, then SCR is set to its reset value of zero.
1628 * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
1629 * among other bits, to never set the register back to zero again.
1636 /* TODO: in future, this should happen via API in drivers/reset/ */
1637 static int omap8250_soft_reset(struct device *dev)
1639 struct omap8250_priv *priv = dev_get_drvdata(dev);
1645 * At least on omap4, unused uarts may not idle after reset without
1646 * a basic scr dma configuration even with no dma in use. The
1647 * module clkctrl status bits will be 1 instead of 3 blocking idle
1648 * for the whole clockdomain. The softreset below will clear scr,
1649 * and we restore it on resume so this is safe to do on all SoCs
1650 * needing omap8250_soft_reset() quirk. Do it in two writes as
1651 * recommended in the comment for omap8250_update_scr().
1653 uart_write(priv, UART_OMAP_SCR, OMAP_UART_SCR_DMAMODE_1);
1654 uart_write(priv, UART_OMAP_SCR,
1655 OMAP_UART_SCR_DMAMODE_1 | OMAP_UART_SCR_DMAMODE_CTL);
1657 sysc = uart_read(priv, UART_OMAP_SYSC);
1659 /* softreset the UART */
1660 sysc |= OMAP_UART_SYSC_SOFTRESET;
1661 uart_write(priv, UART_OMAP_SYSC, sysc);
1663 /* By experiments, 1us enough for reset complete on AM335x */
1666 syss = uart_read(priv, UART_OMAP_SYSS);
1667 } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
1670 dev_err(dev, "timed out waiting for reset done\n");
1677 static int omap8250_runtime_suspend(struct device *dev)
1679 struct omap8250_priv *priv = dev_get_drvdata(dev);
1680 struct uart_8250_port *up = NULL;
1682 if (priv->line >= 0)
1683 up = serial8250_get_port(priv->line);
1685 * When using 'no_console_suspend', the console UART must not be
1686 * suspended. Since driver suspend is managed by runtime suspend,
1687 * preventing runtime suspend (by returning error) will keep device
1688 * active during suspend.
1690 if (priv->is_suspending && !console_suspend_enabled) {
1691 if (up && uart_console(&up->port))
1695 if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
1698 ret = omap8250_soft_reset(dev);
1703 /* Restore to UART mode after reset (for wakeup) */
1704 omap8250_update_mdr1(up, priv);
1705 /* Restore wakeup enable register */
1706 serial_out(up, UART_OMAP_WER, priv->wer);
1710 if (up && up->dma && up->dma->rxchan)
1711 omap_8250_rx_dma_flush(up);
1713 priv->latency = PM_QOS_CPU_LATENCY_DEFAULT_VALUE;
1714 schedule_work(&priv->qos_work);
1719 static int omap8250_runtime_resume(struct device *dev)
1721 struct omap8250_priv *priv = dev_get_drvdata(dev);
1722 struct uart_8250_port *up = NULL;
1724 if (priv->line >= 0)
1725 up = serial8250_get_port(priv->line);
1727 if (up && omap8250_lost_context(up))
1728 omap8250_restore_regs(up);
1730 if (up && up->dma && up->dma->rxchan && !(priv->habit & UART_HAS_EFR2))
1731 omap_8250_rx_dma(up);
1733 priv->latency = priv->calc_latency;
1734 schedule_work(&priv->qos_work);
1739 #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1740 static int __init omap8250_console_fixup(void)
1746 if (strstr(boot_command_line, "console=ttyS"))
1747 /* user set a ttyS based name for the console */
1750 omap_str = strstr(boot_command_line, "console=ttyO");
1752 /* user did not set ttyO based console, so we don't care */
1756 if ('0' <= *omap_str && *omap_str <= '9')
1757 idx = *omap_str - '0';
1762 if (omap_str[0] == ',') {
1769 add_preferred_console("ttyS", idx, options);
1770 pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1772 pr_err("This ensures that you still see kernel messages. Please\n");
1773 pr_err("update your kernel commandline.\n");
1776 console_initcall(omap8250_console_fixup);
1779 static const struct dev_pm_ops omap8250_dev_pm_ops = {
1780 SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1781 SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
1782 omap8250_runtime_resume, NULL)
1783 .prepare = omap8250_prepare,
1784 .complete = omap8250_complete,
1787 static struct platform_driver omap8250_platform_driver = {
1790 .pm = &omap8250_dev_pm_ops,
1791 .of_match_table = omap8250_dt_ids,
1793 .probe = omap8250_probe,
1794 .remove = omap8250_remove,
1796 module_platform_driver(omap8250_platform_driver);
1798 MODULE_AUTHOR("Sebastian Andrzej Siewior");
1799 MODULE_DESCRIPTION("OMAP 8250 Driver");
1800 MODULE_LICENSE("GPL v2");