GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / tty / serial / 8250 / 8250_omap.c
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/device.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/serial_8250.h>
15 #include <linux/serial_reg.h>
16 #include <linux/tty_flip.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/of_gpio.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
30 #include "8250.h"
31
32 #define DEFAULT_CLK_SPEED       48000000
33
34 #define UART_ERRATA_i202_MDR1_ACCESS    (1 << 0)
35 #define OMAP_UART_WER_HAS_TX_WAKEUP     (1 << 1)
36 #define OMAP_DMA_TX_KICK                (1 << 2)
37 /*
38  * See Advisory 21 in AM437x errata SPRZ408B, updated April 2015.
39  * The same errata is applicable to AM335x and DRA7x processors too.
40  */
41 #define UART_ERRATA_CLOCK_DISABLE       (1 << 3)
42
43 #define OMAP_UART_FCR_RX_TRIG           6
44 #define OMAP_UART_FCR_TX_TRIG           4
45
46 /* SCR register bitmasks */
47 #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK       (1 << 7)
48 #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK       (1 << 6)
49 #define OMAP_UART_SCR_TX_EMPTY                  (1 << 3)
50 #define OMAP_UART_SCR_DMAMODE_MASK              (3 << 1)
51 #define OMAP_UART_SCR_DMAMODE_1                 (1 << 1)
52 #define OMAP_UART_SCR_DMAMODE_CTL               (1 << 0)
53
54 /* MVR register bitmasks */
55 #define OMAP_UART_MVR_SCHEME_SHIFT      30
56 #define OMAP_UART_LEGACY_MVR_MAJ_MASK   0xf0
57 #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT  4
58 #define OMAP_UART_LEGACY_MVR_MIN_MASK   0x0f
59 #define OMAP_UART_MVR_MAJ_MASK          0x700
60 #define OMAP_UART_MVR_MAJ_SHIFT         8
61 #define OMAP_UART_MVR_MIN_MASK          0x3f
62
63 /* SYSC register bitmasks */
64 #define OMAP_UART_SYSC_SOFTRESET        (1 << 1)
65
66 /* SYSS register bitmasks */
67 #define OMAP_UART_SYSS_RESETDONE        (1 << 0)
68
69 #define UART_TI752_TLR_TX       0
70 #define UART_TI752_TLR_RX       4
71
72 #define TRIGGER_TLR_MASK(x)     ((x & 0x3c) >> 2)
73 #define TRIGGER_FCR_MASK(x)     (x & 3)
74
75 /* Enable XON/XOFF flow control on output */
76 #define OMAP_UART_SW_TX         0x08
77 /* Enable XON/XOFF flow control on input */
78 #define OMAP_UART_SW_RX         0x02
79
80 #define OMAP_UART_WER_MOD_WKUP  0x7f
81 #define OMAP_UART_TX_WAKEUP_EN  (1 << 7)
82
83 #define TX_TRIGGER      1
84 #define RX_TRIGGER      48
85
86 #define OMAP_UART_TCR_RESTORE(x)        ((x / 4) << 4)
87 #define OMAP_UART_TCR_HALT(x)           ((x / 4) << 0)
88
89 #define UART_BUILD_REVISION(x, y)       (((x) << 8) | (y))
90
91 #define OMAP_UART_REV_46 0x0406
92 #define OMAP_UART_REV_52 0x0502
93 #define OMAP_UART_REV_63 0x0603
94
95 struct omap8250_priv {
96         int line;
97         u8 habit;
98         u8 mdr1;
99         u8 efr;
100         u8 scr;
101         u8 wer;
102         u8 xon;
103         u8 xoff;
104         u8 delayed_restore;
105         u16 quot;
106
107         bool is_suspending;
108         int wakeirq;
109         int wakeups_enabled;
110         u32 latency;
111         u32 calc_latency;
112         struct pm_qos_request pm_qos_request;
113         struct work_struct qos_work;
114         struct uart_8250_dma omap8250_dma;
115         spinlock_t rx_dma_lock;
116         bool rx_dma_broken;
117         bool throttled;
118 };
119
120 #ifdef CONFIG_SERIAL_8250_DMA
121 static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
122 #else
123 static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
124 #endif
125
126 static u32 uart_read(struct uart_8250_port *up, u32 reg)
127 {
128         return readl(up->port.membase + (reg << up->port.regshift));
129 }
130
131 static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
132 {
133         struct uart_8250_port *up = up_to_u8250p(port);
134         struct omap8250_priv *priv = up->port.private_data;
135         u8 lcr;
136
137         serial8250_do_set_mctrl(port, mctrl);
138
139         /*
140          * Turn off autoRTS if RTS is lowered and restore autoRTS setting
141          * if RTS is raised
142          */
143         lcr = serial_in(up, UART_LCR);
144         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
145         if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
146                 priv->efr |= UART_EFR_RTS;
147         else
148                 priv->efr &= ~UART_EFR_RTS;
149         serial_out(up, UART_EFR, priv->efr);
150         serial_out(up, UART_LCR, lcr);
151 }
152
153 /*
154  * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
155  * The access to uart register after MDR1 Access
156  * causes UART to corrupt data.
157  *
158  * Need a delay =
159  * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
160  * give 10 times as much
161  */
162 static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
163                                      struct omap8250_priv *priv)
164 {
165         serial_out(up, UART_OMAP_MDR1, priv->mdr1);
166         udelay(2);
167         serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
168                         UART_FCR_CLEAR_RCVR);
169 }
170
171 static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
172                                   struct omap8250_priv *priv)
173 {
174         unsigned int uartclk = port->uartclk;
175         unsigned int div_13, div_16;
176         unsigned int abs_d13, abs_d16;
177
178         /*
179          * Old custom speed handling.
180          */
181         if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
182                 priv->quot = port->custom_divisor & UART_DIV_MAX;
183                 /*
184                  * I assume that nobody is using this. But hey, if somebody
185                  * would like to specify the divisor _and_ the mode then the
186                  * driver is ready and waiting for it.
187                  */
188                 if (port->custom_divisor & (1 << 16))
189                         priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
190                 else
191                         priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
192                 return;
193         }
194         div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
195         div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
196
197         if (!div_13)
198                 div_13 = 1;
199         if (!div_16)
200                 div_16 = 1;
201
202         abs_d13 = abs(baud - uartclk / 13 / div_13);
203         abs_d16 = abs(baud - uartclk / 16 / div_16);
204
205         if (abs_d13 >= abs_d16) {
206                 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
207                 priv->quot = div_16;
208         } else {
209                 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
210                 priv->quot = div_13;
211         }
212 }
213
214 static void omap8250_update_scr(struct uart_8250_port *up,
215                                 struct omap8250_priv *priv)
216 {
217         u8 old_scr;
218
219         old_scr = serial_in(up, UART_OMAP_SCR);
220         if (old_scr == priv->scr)
221                 return;
222
223         /*
224          * The manual recommends not to enable the DMA mode selector in the SCR
225          * (instead of the FCR) register _and_ selecting the DMA mode as one
226          * register write because this may lead to malfunction.
227          */
228         if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
229                 serial_out(up, UART_OMAP_SCR,
230                            priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
231         serial_out(up, UART_OMAP_SCR, priv->scr);
232 }
233
234 static void omap8250_update_mdr1(struct uart_8250_port *up,
235                                  struct omap8250_priv *priv)
236 {
237         if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
238                 omap_8250_mdr1_errataset(up, priv);
239         else
240                 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
241 }
242
243 static void omap8250_restore_regs(struct uart_8250_port *up)
244 {
245         struct omap8250_priv *priv = up->port.private_data;
246         struct uart_8250_dma    *dma = up->dma;
247         u8 mcr = serial8250_in_MCR(up);
248
249         if (dma && dma->tx_running) {
250                 /*
251                  * TCSANOW requests the change to occur immediately however if
252                  * we have a TX-DMA operation in progress then it has been
253                  * observed that it might stall and never complete. Therefore we
254                  * delay DMA completes to prevent this hang from happen.
255                  */
256                 priv->delayed_restore = 1;
257                 return;
258         }
259
260         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
261         serial_out(up, UART_EFR, UART_EFR_ECB);
262
263         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
264         serial8250_out_MCR(up, mcr | UART_MCR_TCRTLR);
265         serial_out(up, UART_FCR, up->fcr);
266
267         omap8250_update_scr(up, priv);
268
269         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
270
271         serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
272                         OMAP_UART_TCR_HALT(52));
273         serial_out(up, UART_TI752_TLR,
274                    TRIGGER_TLR_MASK(TX_TRIGGER) << UART_TI752_TLR_TX |
275                    TRIGGER_TLR_MASK(RX_TRIGGER) << UART_TI752_TLR_RX);
276
277         serial_out(up, UART_LCR, 0);
278
279         /* drop TCR + TLR access, we setup XON/XOFF later */
280         serial8250_out_MCR(up, mcr);
281
282         serial_out(up, UART_IER, up->ier);
283
284         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
285         serial_dl_write(up, priv->quot);
286
287         serial_out(up, UART_EFR, priv->efr);
288
289         /* Configure flow control */
290         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
291         serial_out(up, UART_XON1, priv->xon);
292         serial_out(up, UART_XOFF1, priv->xoff);
293
294         serial_out(up, UART_LCR, up->lcr);
295
296         omap8250_update_mdr1(up, priv);
297
298         up->port.ops->set_mctrl(&up->port, up->port.mctrl);
299 }
300
301 /*
302  * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
303  * some differences in how we want to handle flow control.
304  */
305 static void omap_8250_set_termios(struct uart_port *port,
306                                   struct ktermios *termios,
307                                   struct ktermios *old)
308 {
309         struct uart_8250_port *up = up_to_u8250p(port);
310         struct omap8250_priv *priv = up->port.private_data;
311         unsigned char cval = 0;
312         unsigned int baud;
313
314         switch (termios->c_cflag & CSIZE) {
315         case CS5:
316                 cval = UART_LCR_WLEN5;
317                 break;
318         case CS6:
319                 cval = UART_LCR_WLEN6;
320                 break;
321         case CS7:
322                 cval = UART_LCR_WLEN7;
323                 break;
324         default:
325         case CS8:
326                 cval = UART_LCR_WLEN8;
327                 break;
328         }
329
330         if (termios->c_cflag & CSTOPB)
331                 cval |= UART_LCR_STOP;
332         if (termios->c_cflag & PARENB)
333                 cval |= UART_LCR_PARITY;
334         if (!(termios->c_cflag & PARODD))
335                 cval |= UART_LCR_EPAR;
336         if (termios->c_cflag & CMSPAR)
337                 cval |= UART_LCR_SPAR;
338
339         /*
340          * Ask the core to calculate the divisor for us.
341          */
342         baud = uart_get_baud_rate(port, termios, old,
343                                   port->uartclk / 16 / UART_DIV_MAX,
344                                   port->uartclk / 13);
345         omap_8250_get_divisor(port, baud, priv);
346
347         /*
348          * Ok, we're now changing the port state. Do it with
349          * interrupts disabled.
350          */
351         pm_runtime_get_sync(port->dev);
352         spin_lock_irq(&port->lock);
353
354         /*
355          * Update the per-port timeout.
356          */
357         uart_update_timeout(port, termios->c_cflag, baud);
358
359         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
360         if (termios->c_iflag & INPCK)
361                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
362         if (termios->c_iflag & (IGNBRK | PARMRK))
363                 up->port.read_status_mask |= UART_LSR_BI;
364
365         /*
366          * Characters to ignore
367          */
368         up->port.ignore_status_mask = 0;
369         if (termios->c_iflag & IGNPAR)
370                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
371         if (termios->c_iflag & IGNBRK) {
372                 up->port.ignore_status_mask |= UART_LSR_BI;
373                 /*
374                  * If we're ignoring parity and break indicators,
375                  * ignore overruns too (for real raw support).
376                  */
377                 if (termios->c_iflag & IGNPAR)
378                         up->port.ignore_status_mask |= UART_LSR_OE;
379         }
380
381         /*
382          * ignore all characters if CREAD is not set
383          */
384         if ((termios->c_cflag & CREAD) == 0)
385                 up->port.ignore_status_mask |= UART_LSR_DR;
386
387         /*
388          * Modem status interrupts
389          */
390         up->ier &= ~UART_IER_MSI;
391         if (UART_ENABLE_MS(&up->port, termios->c_cflag))
392                 up->ier |= UART_IER_MSI;
393
394         up->lcr = cval;
395         /* Up to here it was mostly serial8250_do_set_termios() */
396
397         /*
398          * We enable TRIG_GRANU for RX and TX and additionally we set
399          * SCR_TX_EMPTY bit. The result is the following:
400          * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
401          * - less than RX_TRIGGER number of bytes will also cause an interrupt
402          *   once the UART decides that there no new bytes arriving.
403          * - Once THRE is enabled, the interrupt will be fired once the FIFO is
404          *   empty - the trigger level is ignored here.
405          *
406          * Once DMA is enabled:
407          * - UART will assert the TX DMA line once there is room for TX_TRIGGER
408          *   bytes in the TX FIFO. On each assert the DMA engine will move
409          *   TX_TRIGGER bytes into the FIFO.
410          * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
411          *   the FIFO and move RX_TRIGGER bytes.
412          * This is because threshold and trigger values are the same.
413          */
414         up->fcr = UART_FCR_ENABLE_FIFO;
415         up->fcr |= TRIGGER_FCR_MASK(TX_TRIGGER) << OMAP_UART_FCR_TX_TRIG;
416         up->fcr |= TRIGGER_FCR_MASK(RX_TRIGGER) << OMAP_UART_FCR_RX_TRIG;
417
418         priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
419                 OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
420
421         if (up->dma)
422                 priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
423                         OMAP_UART_SCR_DMAMODE_CTL;
424
425         priv->xon = termios->c_cc[VSTART];
426         priv->xoff = termios->c_cc[VSTOP];
427
428         priv->efr = 0;
429         up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
430
431         if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
432                 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
433                 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
434                 priv->efr |= UART_EFR_CTS;
435         } else  if (up->port.flags & UPF_SOFT_FLOW) {
436                 /*
437                  * OMAP rx s/w flow control is borked; the transmitter remains
438                  * stuck off even if rx flow control is subsequently disabled
439                  */
440
441                 /*
442                  * IXOFF Flag:
443                  * Enable XON/XOFF flow control on output.
444                  * Transmit XON1, XOFF1
445                  */
446                 if (termios->c_iflag & IXOFF) {
447                         up->port.status |= UPSTAT_AUTOXOFF;
448                         priv->efr |= OMAP_UART_SW_TX;
449                 }
450         }
451         omap8250_restore_regs(up);
452
453         spin_unlock_irq(&up->port.lock);
454         pm_runtime_mark_last_busy(port->dev);
455         pm_runtime_put_autosuspend(port->dev);
456
457         /* calculate wakeup latency constraint */
458         priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
459         priv->latency = priv->calc_latency;
460
461         schedule_work(&priv->qos_work);
462
463         /* Don't rewrite B0 */
464         if (tty_termios_baud_rate(termios))
465                 tty_termios_encode_baud_rate(termios, baud, baud);
466 }
467
468 /* same as 8250 except that we may have extra flow bits set in EFR */
469 static void omap_8250_pm(struct uart_port *port, unsigned int state,
470                          unsigned int oldstate)
471 {
472         struct uart_8250_port *up = up_to_u8250p(port);
473         u8 efr;
474
475         pm_runtime_get_sync(port->dev);
476         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
477         efr = serial_in(up, UART_EFR);
478         serial_out(up, UART_EFR, efr | UART_EFR_ECB);
479         serial_out(up, UART_LCR, 0);
480
481         serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
482         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
483         serial_out(up, UART_EFR, efr);
484         serial_out(up, UART_LCR, 0);
485
486         pm_runtime_mark_last_busy(port->dev);
487         pm_runtime_put_autosuspend(port->dev);
488 }
489
490 static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
491                                               struct omap8250_priv *priv)
492 {
493         u32 mvr, scheme;
494         u16 revision, major, minor;
495
496         mvr = uart_read(up, UART_OMAP_MVER);
497
498         /* Check revision register scheme */
499         scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
500
501         switch (scheme) {
502         case 0: /* Legacy Scheme: OMAP2/3 */
503                 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
504                 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
505                         OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
506                 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
507                 break;
508         case 1:
509                 /* New Scheme: OMAP4+ */
510                 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
511                 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
512                         OMAP_UART_MVR_MAJ_SHIFT;
513                 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
514                 break;
515         default:
516                 dev_warn(up->port.dev,
517                          "Unknown revision, defaulting to highest\n");
518                 /* highest possible revision */
519                 major = 0xff;
520                 minor = 0xff;
521         }
522         /* normalize revision for the driver */
523         revision = UART_BUILD_REVISION(major, minor);
524
525         switch (revision) {
526         case OMAP_UART_REV_46:
527                 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
528                 break;
529         case OMAP_UART_REV_52:
530                 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
531                                 OMAP_UART_WER_HAS_TX_WAKEUP;
532                 break;
533         case OMAP_UART_REV_63:
534                 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
535                         OMAP_UART_WER_HAS_TX_WAKEUP;
536                 break;
537         default:
538                 break;
539         }
540 }
541
542 static void omap8250_uart_qos_work(struct work_struct *work)
543 {
544         struct omap8250_priv *priv;
545
546         priv = container_of(work, struct omap8250_priv, qos_work);
547         pm_qos_update_request(&priv->pm_qos_request, priv->latency);
548 }
549
550 #ifdef CONFIG_SERIAL_8250_DMA
551 static int omap_8250_dma_handle_irq(struct uart_port *port);
552 #endif
553
554 static irqreturn_t omap8250_irq(int irq, void *dev_id)
555 {
556         struct uart_port *port = dev_id;
557         struct uart_8250_port *up = up_to_u8250p(port);
558         unsigned int iir;
559         int ret;
560
561 #ifdef CONFIG_SERIAL_8250_DMA
562         if (up->dma) {
563                 ret = omap_8250_dma_handle_irq(port);
564                 return IRQ_RETVAL(ret);
565         }
566 #endif
567
568         serial8250_rpm_get(up);
569         iir = serial_port_in(port, UART_IIR);
570         ret = serial8250_handle_irq(port, iir);
571         serial8250_rpm_put(up);
572
573         return IRQ_RETVAL(ret);
574 }
575
576 static int omap_8250_startup(struct uart_port *port)
577 {
578         struct uart_8250_port *up = up_to_u8250p(port);
579         struct omap8250_priv *priv = port->private_data;
580         int ret;
581
582         if (priv->wakeirq) {
583                 ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
584                 if (ret)
585                         return ret;
586         }
587
588         pm_runtime_get_sync(port->dev);
589
590         serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
591
592         serial_out(up, UART_LCR, UART_LCR_WLEN8);
593
594         up->lsr_saved_flags = 0;
595         up->msr_saved_flags = 0;
596
597         /* Disable DMA for console UART */
598         if (uart_console(port))
599                 up->dma = NULL;
600
601         if (up->dma) {
602                 ret = serial8250_request_dma(up);
603                 if (ret) {
604                         dev_warn_ratelimited(port->dev,
605                                              "failed to request DMA\n");
606                         up->dma = NULL;
607                 }
608         }
609
610         ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
611                           dev_name(port->dev), port);
612         if (ret < 0)
613                 goto err;
614
615         up->ier = UART_IER_RLSI | UART_IER_RDI;
616         serial_out(up, UART_IER, up->ier);
617
618 #ifdef CONFIG_PM
619         up->capabilities |= UART_CAP_RPM;
620 #endif
621
622         /* Enable module level wake up */
623         priv->wer = OMAP_UART_WER_MOD_WKUP;
624         if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
625                 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
626         serial_out(up, UART_OMAP_WER, priv->wer);
627
628         if (up->dma)
629                 up->dma->rx_dma(up);
630
631         pm_runtime_mark_last_busy(port->dev);
632         pm_runtime_put_autosuspend(port->dev);
633         return 0;
634 err:
635         pm_runtime_mark_last_busy(port->dev);
636         pm_runtime_put_autosuspend(port->dev);
637         dev_pm_clear_wake_irq(port->dev);
638         return ret;
639 }
640
641 static void omap_8250_shutdown(struct uart_port *port)
642 {
643         struct uart_8250_port *up = up_to_u8250p(port);
644         struct omap8250_priv *priv = port->private_data;
645
646         flush_work(&priv->qos_work);
647         if (up->dma)
648                 omap_8250_rx_dma_flush(up);
649
650         pm_runtime_get_sync(port->dev);
651
652         serial_out(up, UART_OMAP_WER, 0);
653
654         up->ier = 0;
655         serial_out(up, UART_IER, 0);
656
657         if (up->dma)
658                 serial8250_release_dma(up);
659
660         /*
661          * Disable break condition and FIFOs
662          */
663         if (up->lcr & UART_LCR_SBC)
664                 serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
665         serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
666
667         pm_runtime_mark_last_busy(port->dev);
668         pm_runtime_put_autosuspend(port->dev);
669         free_irq(port->irq, port);
670         dev_pm_clear_wake_irq(port->dev);
671 }
672
673 static void omap_8250_throttle(struct uart_port *port)
674 {
675         struct omap8250_priv *priv = port->private_data;
676         struct uart_8250_port *up = up_to_u8250p(port);
677         unsigned long flags;
678
679         pm_runtime_get_sync(port->dev);
680
681         spin_lock_irqsave(&port->lock, flags);
682         up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
683         serial_out(up, UART_IER, up->ier);
684         priv->throttled = true;
685         spin_unlock_irqrestore(&port->lock, flags);
686
687         pm_runtime_mark_last_busy(port->dev);
688         pm_runtime_put_autosuspend(port->dev);
689 }
690
691 static int omap_8250_rs485_config(struct uart_port *port,
692                                   struct serial_rs485 *rs485)
693 {
694         struct uart_8250_port *up = up_to_u8250p(port);
695
696         /* Clamp the delays to [0, 100ms] */
697         rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
698         rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send, 100U);
699
700         port->rs485 = *rs485;
701
702         /*
703          * Both serial8250_em485_init and serial8250_em485_destroy
704          * are idempotent
705          */
706         if (rs485->flags & SER_RS485_ENABLED) {
707                 int ret = serial8250_em485_init(up);
708
709                 if (ret) {
710                         rs485->flags &= ~SER_RS485_ENABLED;
711                         port->rs485.flags &= ~SER_RS485_ENABLED;
712                 }
713                 return ret;
714         }
715
716         serial8250_em485_destroy(up);
717
718         return 0;
719 }
720
721 static void omap_8250_unthrottle(struct uart_port *port)
722 {
723         struct omap8250_priv *priv = port->private_data;
724         struct uart_8250_port *up = up_to_u8250p(port);
725         unsigned long flags;
726
727         pm_runtime_get_sync(port->dev);
728
729         spin_lock_irqsave(&port->lock, flags);
730         priv->throttled = false;
731         if (up->dma)
732                 up->dma->rx_dma(up);
733         up->ier |= UART_IER_RLSI | UART_IER_RDI;
734         serial_out(up, UART_IER, up->ier);
735         spin_unlock_irqrestore(&port->lock, flags);
736
737         pm_runtime_mark_last_busy(port->dev);
738         pm_runtime_put_autosuspend(port->dev);
739 }
740
741 #ifdef CONFIG_SERIAL_8250_DMA
742 static int omap_8250_rx_dma(struct uart_8250_port *p);
743
744 static void __dma_rx_do_complete(struct uart_8250_port *p)
745 {
746         struct omap8250_priv    *priv = p->port.private_data;
747         struct uart_8250_dma    *dma = p->dma;
748         struct tty_port         *tty_port = &p->port.state->port;
749         struct dma_tx_state     state;
750         int                     count;
751         unsigned long           flags;
752         int                     ret;
753
754         spin_lock_irqsave(&priv->rx_dma_lock, flags);
755
756         if (!dma->rx_running)
757                 goto unlock;
758
759         dma->rx_running = 0;
760         dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
761
762         count = dma->rx_size - state.residue;
763         if (count < dma->rx_size)
764                 dmaengine_terminate_async(dma->rxchan);
765         if (!count)
766                 goto unlock;
767         ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
768
769         p->port.icount.rx += ret;
770         p->port.icount.buf_overrun += count - ret;
771 unlock:
772         spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
773
774         tty_flip_buffer_push(tty_port);
775 }
776
777 static void __dma_rx_complete(void *param)
778 {
779         struct uart_8250_port *p = param;
780         struct omap8250_priv *priv = p->port.private_data;
781         struct uart_8250_dma *dma = p->dma;
782         struct dma_tx_state     state;
783         unsigned long flags;
784
785         spin_lock_irqsave(&p->port.lock, flags);
786
787         /*
788          * If the tx status is not DMA_COMPLETE, then this is a delayed
789          * completion callback. A previous RX timeout flush would have
790          * already pushed the data, so exit.
791          */
792         if (dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state) !=
793                         DMA_COMPLETE) {
794                 spin_unlock_irqrestore(&p->port.lock, flags);
795                 return;
796         }
797         __dma_rx_do_complete(p);
798         if (!priv->throttled)
799                 omap_8250_rx_dma(p);
800
801         spin_unlock_irqrestore(&p->port.lock, flags);
802 }
803
804 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
805 {
806         struct omap8250_priv    *priv = p->port.private_data;
807         struct uart_8250_dma    *dma = p->dma;
808         struct dma_tx_state     state;
809         unsigned long           flags;
810         int ret;
811
812         spin_lock_irqsave(&priv->rx_dma_lock, flags);
813
814         if (!dma->rx_running) {
815                 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
816                 return;
817         }
818
819         ret = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
820         if (ret == DMA_IN_PROGRESS) {
821                 ret = dmaengine_pause(dma->rxchan);
822                 if (WARN_ON_ONCE(ret))
823                         priv->rx_dma_broken = true;
824         }
825         spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
826
827         __dma_rx_do_complete(p);
828 }
829
830 static int omap_8250_rx_dma(struct uart_8250_port *p)
831 {
832         struct omap8250_priv            *priv = p->port.private_data;
833         struct uart_8250_dma            *dma = p->dma;
834         int                             err = 0;
835         struct dma_async_tx_descriptor  *desc;
836         unsigned long                   flags;
837
838         if (priv->rx_dma_broken)
839                 return -EINVAL;
840
841         spin_lock_irqsave(&priv->rx_dma_lock, flags);
842
843         if (dma->rx_running)
844                 goto out;
845
846         desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
847                                            dma->rx_size, DMA_DEV_TO_MEM,
848                                            DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
849         if (!desc) {
850                 err = -EBUSY;
851                 goto out;
852         }
853
854         dma->rx_running = 1;
855         desc->callback = __dma_rx_complete;
856         desc->callback_param = p;
857
858         dma->rx_cookie = dmaengine_submit(desc);
859
860         dma_async_issue_pending(dma->rxchan);
861 out:
862         spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
863         return err;
864 }
865
866 static int omap_8250_tx_dma(struct uart_8250_port *p);
867
868 static void omap_8250_dma_tx_complete(void *param)
869 {
870         struct uart_8250_port   *p = param;
871         struct uart_8250_dma    *dma = p->dma;
872         struct circ_buf         *xmit = &p->port.state->xmit;
873         unsigned long           flags;
874         bool                    en_thri = false;
875         struct omap8250_priv    *priv = p->port.private_data;
876
877         dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
878                                 UART_XMIT_SIZE, DMA_TO_DEVICE);
879
880         spin_lock_irqsave(&p->port.lock, flags);
881
882         dma->tx_running = 0;
883
884         xmit->tail += dma->tx_size;
885         xmit->tail &= UART_XMIT_SIZE - 1;
886         p->port.icount.tx += dma->tx_size;
887
888         if (priv->delayed_restore) {
889                 priv->delayed_restore = 0;
890                 omap8250_restore_regs(p);
891         }
892
893         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
894                 uart_write_wakeup(&p->port);
895
896         if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
897                 int ret;
898
899                 ret = omap_8250_tx_dma(p);
900                 if (ret)
901                         en_thri = true;
902
903         } else if (p->capabilities & UART_CAP_RPM) {
904                 en_thri = true;
905         }
906
907         if (en_thri) {
908                 dma->tx_err = 1;
909                 p->ier |= UART_IER_THRI;
910                 serial_port_out(&p->port, UART_IER, p->ier);
911         }
912
913         spin_unlock_irqrestore(&p->port.lock, flags);
914 }
915
916 static int omap_8250_tx_dma(struct uart_8250_port *p)
917 {
918         struct uart_8250_dma            *dma = p->dma;
919         struct omap8250_priv            *priv = p->port.private_data;
920         struct circ_buf                 *xmit = &p->port.state->xmit;
921         struct dma_async_tx_descriptor  *desc;
922         unsigned int    skip_byte = 0;
923         int ret;
924
925         if (dma->tx_running)
926                 return 0;
927         if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
928
929                 /*
930                  * Even if no data, we need to return an error for the two cases
931                  * below so serial8250_tx_chars() is invoked and properly clears
932                  * THRI and/or runtime suspend.
933                  */
934                 if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
935                         ret = -EBUSY;
936                         goto err;
937                 }
938                 if (p->ier & UART_IER_THRI) {
939                         p->ier &= ~UART_IER_THRI;
940                         serial_out(p, UART_IER, p->ier);
941                 }
942                 return 0;
943         }
944
945         dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
946         if (priv->habit & OMAP_DMA_TX_KICK) {
947                 u8 tx_lvl;
948
949                 /*
950                  * We need to put the first byte into the FIFO in order to start
951                  * the DMA transfer. For transfers smaller than four bytes we
952                  * don't bother doing DMA at all. It seem not matter if there
953                  * are still bytes in the FIFO from the last transfer (in case
954                  * we got here directly from omap_8250_dma_tx_complete()). Bytes
955                  * leaving the FIFO seem not to trigger the DMA transfer. It is
956                  * really the byte that we put into the FIFO.
957                  * If the FIFO is already full then we most likely got here from
958                  * omap_8250_dma_tx_complete(). And this means the DMA engine
959                  * just completed its work. We don't have to wait the complete
960                  * 86us at 115200,8n1 but around 60us (not to mention lower
961                  * baudrates). So in that case we take the interrupt and try
962                  * again with an empty FIFO.
963                  */
964                 tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
965                 if (tx_lvl == p->tx_loadsz) {
966                         ret = -EBUSY;
967                         goto err;
968                 }
969                 if (dma->tx_size < 4) {
970                         ret = -EINVAL;
971                         goto err;
972                 }
973                 skip_byte = 1;
974         }
975
976         desc = dmaengine_prep_slave_single(dma->txchan,
977                         dma->tx_addr + xmit->tail + skip_byte,
978                         dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
979                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
980         if (!desc) {
981                 ret = -EBUSY;
982                 goto err;
983         }
984
985         dma->tx_running = 1;
986
987         desc->callback = omap_8250_dma_tx_complete;
988         desc->callback_param = p;
989
990         dma->tx_cookie = dmaengine_submit(desc);
991
992         dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
993                                    UART_XMIT_SIZE, DMA_TO_DEVICE);
994
995         dma_async_issue_pending(dma->txchan);
996         if (dma->tx_err)
997                 dma->tx_err = 0;
998
999         if (p->ier & UART_IER_THRI) {
1000                 p->ier &= ~UART_IER_THRI;
1001                 serial_out(p, UART_IER, p->ier);
1002         }
1003         if (skip_byte)
1004                 serial_out(p, UART_TX, xmit->buf[xmit->tail]);
1005         return 0;
1006 err:
1007         dma->tx_err = 1;
1008         return ret;
1009 }
1010
1011 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1012 {
1013         switch (iir & 0x3f) {
1014         case UART_IIR_RLSI:
1015         case UART_IIR_RX_TIMEOUT:
1016         case UART_IIR_RDI:
1017                 omap_8250_rx_dma_flush(up);
1018                 return true;
1019         }
1020         return omap_8250_rx_dma(up);
1021 }
1022
1023 /*
1024  * This is mostly serial8250_handle_irq(). We have a slightly different DMA
1025  * hoook for RX/TX and need different logic for them in the ISR. Therefore we
1026  * use the default routine in the non-DMA case and this one for with DMA.
1027  */
1028 static int omap_8250_dma_handle_irq(struct uart_port *port)
1029 {
1030         struct uart_8250_port *up = up_to_u8250p(port);
1031         unsigned char status;
1032         unsigned long flags;
1033         u8 iir;
1034
1035         serial8250_rpm_get(up);
1036
1037         iir = serial_port_in(port, UART_IIR);
1038         if (iir & UART_IIR_NO_INT) {
1039                 serial8250_rpm_put(up);
1040                 return 0;
1041         }
1042
1043         spin_lock_irqsave(&port->lock, flags);
1044
1045         status = serial_port_in(port, UART_LSR);
1046
1047         if (status & (UART_LSR_DR | UART_LSR_BI)) {
1048                 if (handle_rx_dma(up, iir)) {
1049                         status = serial8250_rx_chars(up, status);
1050                         omap_8250_rx_dma(up);
1051                 }
1052         }
1053         serial8250_modem_status(up);
1054         if (status & UART_LSR_THRE && up->dma->tx_err) {
1055                 if (uart_tx_stopped(&up->port) ||
1056                     uart_circ_empty(&up->port.state->xmit)) {
1057                         up->dma->tx_err = 0;
1058                         serial8250_tx_chars(up);
1059                 } else  {
1060                         /*
1061                          * try again due to an earlier failer which
1062                          * might have been resolved by now.
1063                          */
1064                         if (omap_8250_tx_dma(up))
1065                                 serial8250_tx_chars(up);
1066                 }
1067         }
1068
1069         spin_unlock_irqrestore(&port->lock, flags);
1070         serial8250_rpm_put(up);
1071         return 1;
1072 }
1073
1074 static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1075 {
1076         return false;
1077 }
1078
1079 #else
1080
1081 static inline int omap_8250_rx_dma(struct uart_8250_port *p)
1082 {
1083         return -EINVAL;
1084 }
1085 #endif
1086
1087 static int omap8250_no_handle_irq(struct uart_port *port)
1088 {
1089         /* IRQ has not been requested but handling irq? */
1090         WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1091         return 0;
1092 }
1093
1094 static const u8 omap4_habit = UART_ERRATA_CLOCK_DISABLE;
1095 static const u8 am3352_habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE;
1096 static const u8 dra742_habit = UART_ERRATA_CLOCK_DISABLE;
1097
1098 static const struct of_device_id omap8250_dt_ids[] = {
1099         { .compatible = "ti,am654-uart" },
1100         { .compatible = "ti,omap2-uart" },
1101         { .compatible = "ti,omap3-uart" },
1102         { .compatible = "ti,omap4-uart", .data = &omap4_habit, },
1103         { .compatible = "ti,am3352-uart", .data = &am3352_habit, },
1104         { .compatible = "ti,am4372-uart", .data = &am3352_habit, },
1105         { .compatible = "ti,dra742-uart", .data = &dra742_habit, },
1106         {},
1107 };
1108 MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1109
1110 static int omap8250_probe(struct platform_device *pdev)
1111 {
1112         struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1113         struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1114         struct omap8250_priv *priv;
1115         struct uart_8250_port up;
1116         int ret;
1117         void __iomem *membase;
1118
1119         if (!regs || !irq) {
1120                 dev_err(&pdev->dev, "missing registers or irq\n");
1121                 return -EINVAL;
1122         }
1123
1124         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1125         if (!priv)
1126                 return -ENOMEM;
1127
1128         membase = devm_ioremap_nocache(&pdev->dev, regs->start,
1129                                        resource_size(regs));
1130         if (!membase)
1131                 return -ENODEV;
1132
1133         memset(&up, 0, sizeof(up));
1134         up.port.dev = &pdev->dev;
1135         up.port.mapbase = regs->start;
1136         up.port.membase = membase;
1137         up.port.irq = irq->start;
1138         /*
1139          * It claims to be 16C750 compatible however it is a little different.
1140          * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1141          * have) is enabled via EFR instead of MCR. The type is set here 8250
1142          * just to get things going. UNKNOWN does not work for a few reasons and
1143          * we don't need our own type since we don't use 8250's set_termios()
1144          * or pm callback.
1145          */
1146         up.port.type = PORT_8250;
1147         up.port.iotype = UPIO_MEM;
1148         up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
1149                 UPF_HARD_FLOW;
1150         up.port.private_data = priv;
1151
1152         up.port.regshift = 2;
1153         up.port.fifosize = 64;
1154         up.tx_loadsz = 64;
1155         up.capabilities = UART_CAP_FIFO;
1156 #ifdef CONFIG_PM
1157         /*
1158          * Runtime PM is mostly transparent. However to do it right we need to a
1159          * TX empty interrupt before we can put the device to auto idle. So if
1160          * PM is not enabled we don't add that flag and can spare that one extra
1161          * interrupt in the TX path.
1162          */
1163         up.capabilities |= UART_CAP_RPM;
1164 #endif
1165         up.port.set_termios = omap_8250_set_termios;
1166         up.port.set_mctrl = omap8250_set_mctrl;
1167         up.port.pm = omap_8250_pm;
1168         up.port.startup = omap_8250_startup;
1169         up.port.shutdown = omap_8250_shutdown;
1170         up.port.throttle = omap_8250_throttle;
1171         up.port.unthrottle = omap_8250_unthrottle;
1172         up.port.rs485_config = omap_8250_rs485_config;
1173
1174         if (pdev->dev.of_node) {
1175                 const struct of_device_id *id;
1176
1177                 ret = of_alias_get_id(pdev->dev.of_node, "serial");
1178
1179                 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1180                                      &up.port.uartclk);
1181                 priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
1182
1183                 id = of_match_device(of_match_ptr(omap8250_dt_ids), &pdev->dev);
1184                 if (id && id->data)
1185                         priv->habit |= *(u8 *)id->data;
1186         } else {
1187                 ret = pdev->id;
1188         }
1189         if (ret < 0) {
1190                 dev_err(&pdev->dev, "failed to get alias/pdev id\n");
1191                 return ret;
1192         }
1193         up.port.line = ret;
1194
1195         if (!up.port.uartclk) {
1196                 up.port.uartclk = DEFAULT_CLK_SPEED;
1197                 dev_warn(&pdev->dev,
1198                          "No clock speed specified: using default: %d\n",
1199                          DEFAULT_CLK_SPEED);
1200         }
1201
1202         priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1203         priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1204         pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
1205                            priv->latency);
1206         INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1207
1208         spin_lock_init(&priv->rx_dma_lock);
1209
1210         device_init_wakeup(&pdev->dev, true);
1211         pm_runtime_enable(&pdev->dev);
1212         pm_runtime_use_autosuspend(&pdev->dev);
1213         pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1214
1215         pm_runtime_irq_safe(&pdev->dev);
1216
1217         pm_runtime_get_sync(&pdev->dev);
1218
1219         omap_serial_fill_features_erratas(&up, priv);
1220         up.port.handle_irq = omap8250_no_handle_irq;
1221 #ifdef CONFIG_SERIAL_8250_DMA
1222         if (pdev->dev.of_node) {
1223                 /*
1224                  * Oh DMA support. If there are no DMA properties in the DT then
1225                  * we will fall back to a generic DMA channel which does not
1226                  * really work here. To ensure that we do not get a generic DMA
1227                  * channel assigned, we have the the_no_dma_filter_fn() here.
1228                  * To avoid "failed to request DMA" messages we check for DMA
1229                  * properties in DT.
1230                  */
1231                 ret = of_property_count_strings(pdev->dev.of_node, "dma-names");
1232                 if (ret == 2) {
1233                         up.dma = &priv->omap8250_dma;
1234                         priv->omap8250_dma.fn = the_no_dma_filter_fn;
1235                         priv->omap8250_dma.tx_dma = omap_8250_tx_dma;
1236                         priv->omap8250_dma.rx_dma = omap_8250_rx_dma;
1237                         priv->omap8250_dma.rx_size = RX_TRIGGER;
1238                         priv->omap8250_dma.rxconf.src_maxburst = RX_TRIGGER;
1239                         priv->omap8250_dma.txconf.dst_maxburst = TX_TRIGGER;
1240                 }
1241         }
1242 #endif
1243         ret = serial8250_register_8250_port(&up);
1244         if (ret < 0) {
1245                 dev_err(&pdev->dev, "unable to register 8250 port\n");
1246                 goto err;
1247         }
1248         priv->line = ret;
1249         platform_set_drvdata(pdev, priv);
1250         pm_runtime_mark_last_busy(&pdev->dev);
1251         pm_runtime_put_autosuspend(&pdev->dev);
1252         return 0;
1253 err:
1254         pm_runtime_dont_use_autosuspend(&pdev->dev);
1255         pm_runtime_put_sync(&pdev->dev);
1256         pm_runtime_disable(&pdev->dev);
1257         return ret;
1258 }
1259
1260 static int omap8250_remove(struct platform_device *pdev)
1261 {
1262         struct omap8250_priv *priv = platform_get_drvdata(pdev);
1263
1264         pm_runtime_dont_use_autosuspend(&pdev->dev);
1265         pm_runtime_put_sync(&pdev->dev);
1266         flush_work(&priv->qos_work);
1267         pm_runtime_disable(&pdev->dev);
1268         serial8250_unregister_port(priv->line);
1269         pm_qos_remove_request(&priv->pm_qos_request);
1270         device_init_wakeup(&pdev->dev, false);
1271         return 0;
1272 }
1273
1274 #ifdef CONFIG_PM_SLEEP
1275 static int omap8250_prepare(struct device *dev)
1276 {
1277         struct omap8250_priv *priv = dev_get_drvdata(dev);
1278
1279         if (!priv)
1280                 return 0;
1281         priv->is_suspending = true;
1282         return 0;
1283 }
1284
1285 static void omap8250_complete(struct device *dev)
1286 {
1287         struct omap8250_priv *priv = dev_get_drvdata(dev);
1288
1289         if (!priv)
1290                 return;
1291         priv->is_suspending = false;
1292 }
1293
1294 static int omap8250_suspend(struct device *dev)
1295 {
1296         struct omap8250_priv *priv = dev_get_drvdata(dev);
1297         struct uart_8250_port *up = serial8250_get_port(priv->line);
1298
1299         serial8250_suspend_port(priv->line);
1300
1301         pm_runtime_get_sync(dev);
1302         if (!device_may_wakeup(dev))
1303                 priv->wer = 0;
1304         serial_out(up, UART_OMAP_WER, priv->wer);
1305         pm_runtime_mark_last_busy(dev);
1306         pm_runtime_put_autosuspend(dev);
1307
1308         flush_work(&priv->qos_work);
1309         return 0;
1310 }
1311
1312 static int omap8250_resume(struct device *dev)
1313 {
1314         struct omap8250_priv *priv = dev_get_drvdata(dev);
1315
1316         serial8250_resume_port(priv->line);
1317         return 0;
1318 }
1319 #else
1320 #define omap8250_prepare NULL
1321 #define omap8250_complete NULL
1322 #endif
1323
1324 #ifdef CONFIG_PM
1325 static int omap8250_lost_context(struct uart_8250_port *up)
1326 {
1327         u32 val;
1328
1329         val = serial_in(up, UART_OMAP_SCR);
1330         /*
1331          * If we lose context, then SCR is set to its reset value of zero.
1332          * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
1333          * among other bits, to never set the register back to zero again.
1334          */
1335         if (!val)
1336                 return 1;
1337         return 0;
1338 }
1339
1340 /* TODO: in future, this should happen via API in drivers/reset/ */
1341 static int omap8250_soft_reset(struct device *dev)
1342 {
1343         struct omap8250_priv *priv = dev_get_drvdata(dev);
1344         struct uart_8250_port *up = serial8250_get_port(priv->line);
1345         int timeout = 100;
1346         int sysc;
1347         int syss;
1348
1349         /*
1350          * At least on omap4, unused uarts may not idle after reset without
1351          * a basic scr dma configuration even with no dma in use. The
1352          * module clkctrl status bits will be 1 instead of 3 blocking idle
1353          * for the whole clockdomain. The softreset below will clear scr,
1354          * and we restore it on resume so this is safe to do on all SoCs
1355          * needing omap8250_soft_reset() quirk. Do it in two writes as
1356          * recommended in the comment for omap8250_update_scr().
1357          */
1358         serial_out(up, UART_OMAP_SCR, OMAP_UART_SCR_DMAMODE_1);
1359         serial_out(up, UART_OMAP_SCR,
1360                    OMAP_UART_SCR_DMAMODE_1 | OMAP_UART_SCR_DMAMODE_CTL);
1361
1362         sysc = serial_in(up, UART_OMAP_SYSC);
1363
1364         /* softreset the UART */
1365         sysc |= OMAP_UART_SYSC_SOFTRESET;
1366         serial_out(up, UART_OMAP_SYSC, sysc);
1367
1368         /* By experiments, 1us enough for reset complete on AM335x */
1369         do {
1370                 udelay(1);
1371                 syss = serial_in(up, UART_OMAP_SYSS);
1372         } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
1373
1374         if (!timeout) {
1375                 dev_err(dev, "timed out waiting for reset done\n");
1376                 return -ETIMEDOUT;
1377         }
1378
1379         return 0;
1380 }
1381
1382 static int omap8250_runtime_suspend(struct device *dev)
1383 {
1384         struct omap8250_priv *priv = dev_get_drvdata(dev);
1385         struct uart_8250_port *up;
1386
1387         /* In case runtime-pm tries this before we are setup */
1388         if (!priv)
1389                 return 0;
1390
1391         up = serial8250_get_port(priv->line);
1392         /*
1393          * When using 'no_console_suspend', the console UART must not be
1394          * suspended. Since driver suspend is managed by runtime suspend,
1395          * preventing runtime suspend (by returning error) will keep device
1396          * active during suspend.
1397          */
1398         if (priv->is_suspending && !console_suspend_enabled) {
1399                 if (uart_console(&up->port))
1400                         return -EBUSY;
1401         }
1402
1403         if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
1404                 int ret;
1405
1406                 ret = omap8250_soft_reset(dev);
1407                 if (ret)
1408                         return ret;
1409
1410                 /* Restore to UART mode after reset (for wakeup) */
1411                 omap8250_update_mdr1(up, priv);
1412                 /* Restore wakeup enable register */
1413                 serial_out(up, UART_OMAP_WER, priv->wer);
1414         }
1415
1416         if (up->dma && up->dma->rxchan)
1417                 omap_8250_rx_dma_flush(up);
1418
1419         priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1420         schedule_work(&priv->qos_work);
1421
1422         return 0;
1423 }
1424
1425 static int omap8250_runtime_resume(struct device *dev)
1426 {
1427         struct omap8250_priv *priv = dev_get_drvdata(dev);
1428         struct uart_8250_port *up;
1429
1430         /* In case runtime-pm tries this before we are setup */
1431         if (!priv)
1432                 return 0;
1433
1434         up = serial8250_get_port(priv->line);
1435
1436         if (omap8250_lost_context(up))
1437                 omap8250_restore_regs(up);
1438
1439         if (up->dma && up->dma->rxchan)
1440                 omap_8250_rx_dma(up);
1441
1442         priv->latency = priv->calc_latency;
1443         schedule_work(&priv->qos_work);
1444         return 0;
1445 }
1446 #endif
1447
1448 #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1449 static int __init omap8250_console_fixup(void)
1450 {
1451         char *omap_str;
1452         char *options;
1453         u8 idx;
1454
1455         if (strstr(boot_command_line, "console=ttyS"))
1456                 /* user set a ttyS based name for the console */
1457                 return 0;
1458
1459         omap_str = strstr(boot_command_line, "console=ttyO");
1460         if (!omap_str)
1461                 /* user did not set ttyO based console, so we don't care */
1462                 return 0;
1463
1464         omap_str += 12;
1465         if ('0' <= *omap_str && *omap_str <= '9')
1466                 idx = *omap_str - '0';
1467         else
1468                 return 0;
1469
1470         omap_str++;
1471         if (omap_str[0] == ',') {
1472                 omap_str++;
1473                 options = omap_str;
1474         } else {
1475                 options = NULL;
1476         }
1477
1478         add_preferred_console("ttyS", idx, options);
1479         pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1480                idx, idx);
1481         pr_err("This ensures that you still see kernel messages. Please\n");
1482         pr_err("update your kernel commandline.\n");
1483         return 0;
1484 }
1485 console_initcall(omap8250_console_fixup);
1486 #endif
1487
1488 static const struct dev_pm_ops omap8250_dev_pm_ops = {
1489         SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1490         SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
1491                            omap8250_runtime_resume, NULL)
1492         .prepare        = omap8250_prepare,
1493         .complete       = omap8250_complete,
1494 };
1495
1496 static struct platform_driver omap8250_platform_driver = {
1497         .driver = {
1498                 .name           = "omap8250",
1499                 .pm             = &omap8250_dev_pm_ops,
1500                 .of_match_table = omap8250_dt_ids,
1501         },
1502         .probe                  = omap8250_probe,
1503         .remove                 = omap8250_remove,
1504 };
1505 module_platform_driver(omap8250_platform_driver);
1506
1507 MODULE_AUTHOR("Sebastian Andrzej Siewior");
1508 MODULE_DESCRIPTION("OMAP 8250 Driver");
1509 MODULE_LICENSE("GPL v2");