]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/tty/serial/8250/8250_omap.c
serial: 8250_omap: refactor mdr1 update
[karo-tx-linux.git] / drivers / tty / serial / 8250 / 8250_omap.c
1 /*
2  * 8250-core based driver for the OMAP internal UART
3  *
4  * based on omap-serial.c, Copyright (C) 2010 Texas Instruments.
5  *
6  * Copyright (C) 2014 Sebastian Andrzej Siewior
7  *
8  */
9
10 #include <linux/device.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/serial_8250.h>
14 #include <linux/serial_reg.h>
15 #include <linux/tty_flip.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/of.h>
19 #include <linux/of_gpio.h>
20 #include <linux/of_irq.h>
21 #include <linux/delay.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/console.h>
24 #include <linux/pm_qos.h>
25 #include <linux/pm_wakeirq.h>
26 #include <linux/dma-mapping.h>
27
28 #include "8250.h"
29
30 #define DEFAULT_CLK_SPEED       48000000
31
32 #define UART_ERRATA_i202_MDR1_ACCESS    (1 << 0)
33 #define OMAP_UART_WER_HAS_TX_WAKEUP     (1 << 1)
34 #define OMAP_DMA_TX_KICK                (1 << 2)
35
36 #define OMAP_UART_FCR_RX_TRIG           6
37 #define OMAP_UART_FCR_TX_TRIG           4
38
39 /* SCR register bitmasks */
40 #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK       (1 << 7)
41 #define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK       (1 << 6)
42 #define OMAP_UART_SCR_TX_EMPTY                  (1 << 3)
43 #define OMAP_UART_SCR_DMAMODE_MASK              (3 << 1)
44 #define OMAP_UART_SCR_DMAMODE_1                 (1 << 1)
45 #define OMAP_UART_SCR_DMAMODE_CTL               (1 << 0)
46
47 /* MVR register bitmasks */
48 #define OMAP_UART_MVR_SCHEME_SHIFT      30
49 #define OMAP_UART_LEGACY_MVR_MAJ_MASK   0xf0
50 #define OMAP_UART_LEGACY_MVR_MAJ_SHIFT  4
51 #define OMAP_UART_LEGACY_MVR_MIN_MASK   0x0f
52 #define OMAP_UART_MVR_MAJ_MASK          0x700
53 #define OMAP_UART_MVR_MAJ_SHIFT         8
54 #define OMAP_UART_MVR_MIN_MASK          0x3f
55
56 #define UART_TI752_TLR_TX       0
57 #define UART_TI752_TLR_RX       4
58
59 #define TRIGGER_TLR_MASK(x)     ((x & 0x3c) >> 2)
60 #define TRIGGER_FCR_MASK(x)     (x & 3)
61
62 /* Enable XON/XOFF flow control on output */
63 #define OMAP_UART_SW_TX         0x08
64 /* Enable XON/XOFF flow control on input */
65 #define OMAP_UART_SW_RX         0x02
66
67 #define OMAP_UART_WER_MOD_WKUP  0x7f
68 #define OMAP_UART_TX_WAKEUP_EN  (1 << 7)
69
70 #define TX_TRIGGER      1
71 #define RX_TRIGGER      48
72
73 #define OMAP_UART_TCR_RESTORE(x)        ((x / 4) << 4)
74 #define OMAP_UART_TCR_HALT(x)           ((x / 4) << 0)
75
76 #define UART_BUILD_REVISION(x, y)       (((x) << 8) | (y))
77
78 #define OMAP_UART_REV_46 0x0406
79 #define OMAP_UART_REV_52 0x0502
80 #define OMAP_UART_REV_63 0x0603
81
82 struct omap8250_priv {
83         int line;
84         u8 habit;
85         u8 mdr1;
86         u8 efr;
87         u8 scr;
88         u8 wer;
89         u8 xon;
90         u8 xoff;
91         u8 delayed_restore;
92         u16 quot;
93
94         bool is_suspending;
95         int wakeirq;
96         int wakeups_enabled;
97         u32 latency;
98         u32 calc_latency;
99         struct pm_qos_request pm_qos_request;
100         struct work_struct qos_work;
101         struct uart_8250_dma omap8250_dma;
102         spinlock_t rx_dma_lock;
103 };
104
105 static u32 uart_read(struct uart_8250_port *up, u32 reg)
106 {
107         return readl(up->port.membase + (reg << up->port.regshift));
108 }
109
110 static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
111 {
112         struct uart_8250_port *up = up_to_u8250p(port);
113         struct omap8250_priv *priv = up->port.private_data;
114         u8 lcr;
115
116         serial8250_do_set_mctrl(port, mctrl);
117
118         /*
119          * Turn off autoRTS if RTS is lowered and restore autoRTS setting
120          * if RTS is raised
121          */
122         lcr = serial_in(up, UART_LCR);
123         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
124         if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
125                 priv->efr |= UART_EFR_RTS;
126         else
127                 priv->efr &= ~UART_EFR_RTS;
128         serial_out(up, UART_EFR, priv->efr);
129         serial_out(up, UART_LCR, lcr);
130 }
131
132 /*
133  * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
134  * The access to uart register after MDR1 Access
135  * causes UART to corrupt data.
136  *
137  * Need a delay =
138  * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
139  * give 10 times as much
140  */
141 static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
142                                      struct omap8250_priv *priv)
143 {
144         u8 timeout = 255;
145         u8 old_mdr1;
146
147         old_mdr1 = serial_in(up, UART_OMAP_MDR1);
148         if (old_mdr1 == priv->mdr1)
149                 return;
150
151         serial_out(up, UART_OMAP_MDR1, priv->mdr1);
152         udelay(2);
153         serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
154                         UART_FCR_CLEAR_RCVR);
155         /*
156          * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
157          * TX_FIFO_E bit is 1.
158          */
159         while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
160                                 (UART_LSR_THRE | UART_LSR_DR))) {
161                 timeout--;
162                 if (!timeout) {
163                         /* Should *never* happen. we warn and carry on */
164                         dev_crit(up->port.dev, "Errata i202: timedout %x\n",
165                                  serial_in(up, UART_LSR));
166                         break;
167                 }
168                 udelay(1);
169         }
170 }
171
172 static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
173                                   struct omap8250_priv *priv)
174 {
175         unsigned int uartclk = port->uartclk;
176         unsigned int div_13, div_16;
177         unsigned int abs_d13, abs_d16;
178
179         /*
180          * Old custom speed handling.
181          */
182         if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
183                 priv->quot = port->custom_divisor & 0xffff;
184                 /*
185                  * I assume that nobody is using this. But hey, if somebody
186                  * would like to specify the divisor _and_ the mode then the
187                  * driver is ready and waiting for it.
188                  */
189                 if (port->custom_divisor & (1 << 16))
190                         priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
191                 else
192                         priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
193                 return;
194         }
195         div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
196         div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
197
198         if (!div_13)
199                 div_13 = 1;
200         if (!div_16)
201                 div_16 = 1;
202
203         abs_d13 = abs(baud - uartclk / 13 / div_13);
204         abs_d16 = abs(baud - uartclk / 16 / div_16);
205
206         if (abs_d13 >= abs_d16) {
207                 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
208                 priv->quot = div_16;
209         } else {
210                 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
211                 priv->quot = div_13;
212         }
213 }
214
215 static void omap8250_update_scr(struct uart_8250_port *up,
216                                 struct omap8250_priv *priv)
217 {
218         u8 old_scr;
219
220         old_scr = serial_in(up, UART_OMAP_SCR);
221         if (old_scr == priv->scr)
222                 return;
223
224         /*
225          * The manual recommends not to enable the DMA mode selector in the SCR
226          * (instead of the FCR) register _and_ selecting the DMA mode as one
227          * register write because this may lead to malfunction.
228          */
229         if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
230                 serial_out(up, UART_OMAP_SCR,
231                            priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
232         serial_out(up, UART_OMAP_SCR, priv->scr);
233 }
234
235 static void omap8250_update_mdr1(struct uart_8250_port *up,
236                                  struct omap8250_priv *priv)
237 {
238         if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
239                 omap_8250_mdr1_errataset(up, priv);
240         else
241                 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
242 }
243
244 static void omap8250_restore_regs(struct uart_8250_port *up)
245 {
246         struct omap8250_priv *priv = up->port.private_data;
247         struct uart_8250_dma    *dma = up->dma;
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         serial_out(up, UART_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         serial_out(up, UART_MCR, up->mcr);
281         serial_out(up, UART_IER, up->ier);
282
283         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
284         serial_dl_write(up, priv->quot);
285
286         serial_out(up, UART_EFR, priv->efr);
287
288         /* Configure flow control */
289         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
290         serial_out(up, UART_XON1, priv->xon);
291         serial_out(up, UART_XOFF1, priv->xoff);
292
293         serial_out(up, UART_LCR, up->lcr);
294
295         omap8250_update_mdr1(up, priv);
296
297         up->port.ops->set_mctrl(&up->port, up->port.mctrl);
298 }
299
300 /*
301  * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
302  * some differences in how we want to handle flow control.
303  */
304 static void omap_8250_set_termios(struct uart_port *port,
305                                   struct ktermios *termios,
306                                   struct ktermios *old)
307 {
308         struct uart_8250_port *up =
309                 container_of(port, struct uart_8250_port, 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 / 0xffff,
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 additionaly 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->mcr &= ~(UART_MCR_RTS | UART_MCR_XONANY);
430         up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
431
432         if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
433                 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
434                 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
435                 priv->efr |= UART_EFR_CTS;
436         } else  if (up->port.flags & UPF_SOFT_FLOW) {
437                 /*
438                  * IXON Flag:
439                  * Enable XON/XOFF flow control on input.
440                  * Receiver compares XON1, XOFF1.
441                  */
442                 if (termios->c_iflag & IXON)
443                         priv->efr |= OMAP_UART_SW_RX;
444
445                 /*
446                  * IXOFF Flag:
447                  * Enable XON/XOFF flow control on output.
448                  * Transmit XON1, XOFF1
449                  */
450                 if (termios->c_iflag & IXOFF) {
451                         up->port.status |= UPSTAT_AUTOXOFF;
452                         priv->efr |= OMAP_UART_SW_TX;
453                 }
454
455                 /*
456                  * IXANY Flag:
457                  * Enable any character to restart output.
458                  * Operation resumes after receiving any
459                  * character after recognition of the XOFF character
460                  */
461                 if (termios->c_iflag & IXANY)
462                         up->mcr |= UART_MCR_XONANY;
463         }
464         omap8250_restore_regs(up);
465
466         spin_unlock_irq(&up->port.lock);
467         pm_runtime_mark_last_busy(port->dev);
468         pm_runtime_put_autosuspend(port->dev);
469
470         /* calculate wakeup latency constraint */
471         priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
472         priv->latency = priv->calc_latency;
473
474         schedule_work(&priv->qos_work);
475
476         /* Don't rewrite B0 */
477         if (tty_termios_baud_rate(termios))
478                 tty_termios_encode_baud_rate(termios, baud, baud);
479 }
480
481 /* same as 8250 except that we may have extra flow bits set in EFR */
482 static void omap_8250_pm(struct uart_port *port, unsigned int state,
483                          unsigned int oldstate)
484 {
485         struct uart_8250_port *up = up_to_u8250p(port);
486         u8 efr;
487
488         pm_runtime_get_sync(port->dev);
489         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
490         efr = serial_in(up, UART_EFR);
491         serial_out(up, UART_EFR, efr | UART_EFR_ECB);
492         serial_out(up, UART_LCR, 0);
493
494         serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
495         serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
496         serial_out(up, UART_EFR, efr);
497         serial_out(up, UART_LCR, 0);
498
499         pm_runtime_mark_last_busy(port->dev);
500         pm_runtime_put_autosuspend(port->dev);
501 }
502
503 static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
504                                               struct omap8250_priv *priv)
505 {
506         u32 mvr, scheme;
507         u16 revision, major, minor;
508
509         mvr = uart_read(up, UART_OMAP_MVER);
510
511         /* Check revision register scheme */
512         scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
513
514         switch (scheme) {
515         case 0: /* Legacy Scheme: OMAP2/3 */
516                 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
517                 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
518                         OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
519                 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
520                 break;
521         case 1:
522                 /* New Scheme: OMAP4+ */
523                 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
524                 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
525                         OMAP_UART_MVR_MAJ_SHIFT;
526                 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
527                 break;
528         default:
529                 dev_warn(up->port.dev,
530                          "Unknown revision, defaulting to highest\n");
531                 /* highest possible revision */
532                 major = 0xff;
533                 minor = 0xff;
534         }
535         /* normalize revision for the driver */
536         revision = UART_BUILD_REVISION(major, minor);
537
538         switch (revision) {
539         case OMAP_UART_REV_46:
540                 priv->habit = UART_ERRATA_i202_MDR1_ACCESS;
541                 break;
542         case OMAP_UART_REV_52:
543                 priv->habit = UART_ERRATA_i202_MDR1_ACCESS |
544                                 OMAP_UART_WER_HAS_TX_WAKEUP;
545                 break;
546         case OMAP_UART_REV_63:
547                 priv->habit = UART_ERRATA_i202_MDR1_ACCESS |
548                         OMAP_UART_WER_HAS_TX_WAKEUP;
549                 break;
550         default:
551                 break;
552         }
553 }
554
555 static void omap8250_uart_qos_work(struct work_struct *work)
556 {
557         struct omap8250_priv *priv;
558
559         priv = container_of(work, struct omap8250_priv, qos_work);
560         pm_qos_update_request(&priv->pm_qos_request, priv->latency);
561 }
562
563 #ifdef CONFIG_SERIAL_8250_DMA
564 static int omap_8250_dma_handle_irq(struct uart_port *port);
565 #endif
566
567 static irqreturn_t omap8250_irq(int irq, void *dev_id)
568 {
569         struct uart_port *port = dev_id;
570         struct uart_8250_port *up = up_to_u8250p(port);
571         unsigned int iir;
572         int ret;
573
574 #ifdef CONFIG_SERIAL_8250_DMA
575         if (up->dma) {
576                 ret = omap_8250_dma_handle_irq(port);
577                 return IRQ_RETVAL(ret);
578         }
579 #endif
580
581         serial8250_rpm_get(up);
582         iir = serial_port_in(port, UART_IIR);
583         ret = serial8250_handle_irq(port, iir);
584         serial8250_rpm_put(up);
585
586         return IRQ_RETVAL(ret);
587 }
588
589 static int omap_8250_startup(struct uart_port *port)
590 {
591         struct uart_8250_port *up = up_to_u8250p(port);
592         struct omap8250_priv *priv = port->private_data;
593         int ret;
594
595         if (priv->wakeirq) {
596                 ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
597                 if (ret)
598                         return ret;
599         }
600
601         pm_runtime_get_sync(port->dev);
602
603         up->mcr = 0;
604         serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
605
606         serial_out(up, UART_LCR, UART_LCR_WLEN8);
607
608         up->lsr_saved_flags = 0;
609         up->msr_saved_flags = 0;
610
611         if (up->dma) {
612                 ret = serial8250_request_dma(up);
613                 if (ret) {
614                         dev_warn_ratelimited(port->dev,
615                                              "failed to request DMA\n");
616                         up->dma = NULL;
617                 }
618         }
619
620         ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
621                           dev_name(port->dev), port);
622         if (ret < 0)
623                 goto err;
624
625         up->ier = UART_IER_RLSI | UART_IER_RDI;
626         serial_out(up, UART_IER, up->ier);
627
628 #ifdef CONFIG_PM
629         up->capabilities |= UART_CAP_RPM;
630 #endif
631
632         /* Enable module level wake up */
633         priv->wer = OMAP_UART_WER_MOD_WKUP;
634         if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
635                 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
636         serial_out(up, UART_OMAP_WER, priv->wer);
637
638         if (up->dma)
639                 up->dma->rx_dma(up, 0);
640
641         pm_runtime_mark_last_busy(port->dev);
642         pm_runtime_put_autosuspend(port->dev);
643         return 0;
644 err:
645         pm_runtime_mark_last_busy(port->dev);
646         pm_runtime_put_autosuspend(port->dev);
647         dev_pm_clear_wake_irq(port->dev);
648         return ret;
649 }
650
651 static void omap_8250_shutdown(struct uart_port *port)
652 {
653         struct uart_8250_port *up = up_to_u8250p(port);
654         struct omap8250_priv *priv = port->private_data;
655
656         flush_work(&priv->qos_work);
657         if (up->dma)
658                 up->dma->rx_dma(up, UART_IIR_RX_TIMEOUT);
659
660         pm_runtime_get_sync(port->dev);
661
662         serial_out(up, UART_OMAP_WER, 0);
663
664         up->ier = 0;
665         serial_out(up, UART_IER, 0);
666
667         if (up->dma)
668                 serial8250_release_dma(up);
669
670         /*
671          * Disable break condition and FIFOs
672          */
673         if (up->lcr & UART_LCR_SBC)
674                 serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
675         serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
676
677         pm_runtime_mark_last_busy(port->dev);
678         pm_runtime_put_autosuspend(port->dev);
679         free_irq(port->irq, port);
680         dev_pm_clear_wake_irq(port->dev);
681 }
682
683 static void omap_8250_throttle(struct uart_port *port)
684 {
685         unsigned long flags;
686         struct uart_8250_port *up =
687                 container_of(port, struct uart_8250_port, port);
688
689         pm_runtime_get_sync(port->dev);
690
691         spin_lock_irqsave(&port->lock, flags);
692         up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
693         serial_out(up, UART_IER, up->ier);
694         spin_unlock_irqrestore(&port->lock, flags);
695
696         pm_runtime_mark_last_busy(port->dev);
697         pm_runtime_put_autosuspend(port->dev);
698 }
699
700 static void omap_8250_unthrottle(struct uart_port *port)
701 {
702         unsigned long flags;
703         struct uart_8250_port *up =
704                 container_of(port, struct uart_8250_port, port);
705
706         pm_runtime_get_sync(port->dev);
707
708         spin_lock_irqsave(&port->lock, flags);
709         up->ier |= UART_IER_RLSI | UART_IER_RDI;
710         serial_out(up, UART_IER, up->ier);
711         spin_unlock_irqrestore(&port->lock, flags);
712
713         pm_runtime_mark_last_busy(port->dev);
714         pm_runtime_put_autosuspend(port->dev);
715 }
716
717 #ifdef CONFIG_SERIAL_8250_DMA
718 static int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir);
719
720 static void __dma_rx_do_complete(struct uart_8250_port *p, bool error)
721 {
722         struct omap8250_priv    *priv = p->port.private_data;
723         struct uart_8250_dma    *dma = p->dma;
724         struct tty_port         *tty_port = &p->port.state->port;
725         struct dma_tx_state     state;
726         int                     count;
727         unsigned long           flags;
728
729         dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
730                                 dma->rx_size, DMA_FROM_DEVICE);
731
732         spin_lock_irqsave(&priv->rx_dma_lock, flags);
733
734         if (!dma->rx_running)
735                 goto unlock;
736
737         dma->rx_running = 0;
738         dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
739         dmaengine_terminate_all(dma->rxchan);
740
741         count = dma->rx_size - state.residue;
742
743         tty_insert_flip_string(tty_port, dma->rx_buf, count);
744         p->port.icount.rx += count;
745 unlock:
746         spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
747
748         if (!error)
749                 omap_8250_rx_dma(p, 0);
750
751         tty_flip_buffer_push(tty_port);
752 }
753
754 static void __dma_rx_complete(void *param)
755 {
756         __dma_rx_do_complete(param, false);
757 }
758
759 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
760 {
761         struct omap8250_priv    *priv = p->port.private_data;
762         struct uart_8250_dma    *dma = p->dma;
763         unsigned long           flags;
764
765         spin_lock_irqsave(&priv->rx_dma_lock, flags);
766
767         if (!dma->rx_running) {
768                 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
769                 return;
770         }
771
772         dmaengine_pause(dma->rxchan);
773
774         spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
775
776         __dma_rx_do_complete(p, true);
777 }
778
779 static int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
780 {
781         struct omap8250_priv            *priv = p->port.private_data;
782         struct uart_8250_dma            *dma = p->dma;
783         int                             err = 0;
784         struct dma_async_tx_descriptor  *desc;
785         unsigned long                   flags;
786
787         switch (iir & 0x3f) {
788         case UART_IIR_RLSI:
789                 /* 8250_core handles errors and break interrupts */
790                 omap_8250_rx_dma_flush(p);
791                 return -EIO;
792         case UART_IIR_RX_TIMEOUT:
793                 /*
794                  * If RCVR FIFO trigger level was not reached, complete the
795                  * transfer and let 8250_core copy the remaining data.
796                  */
797                 omap_8250_rx_dma_flush(p);
798                 return -ETIMEDOUT;
799         case UART_IIR_RDI:
800                 /*
801                  * The OMAP UART is a special BEAST. If we receive RDI we _have_
802                  * a DMA transfer programmed but it didn't work. One reason is
803                  * that we were too slow and there were too many bytes in the
804                  * FIFO, the UART counted wrong and never kicked the DMA engine
805                  * to do anything. That means once we receive RDI on OMAP then
806                  * the DMA won't do anything soon so we have to cancel the DMA
807                  * transfer and purge the FIFO manually.
808                  */
809                 omap_8250_rx_dma_flush(p);
810                 return -ETIMEDOUT;
811
812         default:
813                 break;
814         }
815
816         spin_lock_irqsave(&priv->rx_dma_lock, flags);
817
818         if (dma->rx_running)
819                 goto out;
820
821         desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
822                                            dma->rx_size, DMA_DEV_TO_MEM,
823                                            DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
824         if (!desc) {
825                 err = -EBUSY;
826                 goto out;
827         }
828
829         dma->rx_running = 1;
830         desc->callback = __dma_rx_complete;
831         desc->callback_param = p;
832
833         dma->rx_cookie = dmaengine_submit(desc);
834
835         dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
836                                    dma->rx_size, DMA_FROM_DEVICE);
837
838         dma_async_issue_pending(dma->rxchan);
839 out:
840         spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
841         return err;
842 }
843
844 static int omap_8250_tx_dma(struct uart_8250_port *p);
845
846 static void omap_8250_dma_tx_complete(void *param)
847 {
848         struct uart_8250_port   *p = param;
849         struct uart_8250_dma    *dma = p->dma;
850         struct circ_buf         *xmit = &p->port.state->xmit;
851         unsigned long           flags;
852         bool                    en_thri = false;
853         struct omap8250_priv    *priv = p->port.private_data;
854
855         dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
856                                 UART_XMIT_SIZE, DMA_TO_DEVICE);
857
858         spin_lock_irqsave(&p->port.lock, flags);
859
860         dma->tx_running = 0;
861
862         xmit->tail += dma->tx_size;
863         xmit->tail &= UART_XMIT_SIZE - 1;
864         p->port.icount.tx += dma->tx_size;
865
866         if (priv->delayed_restore) {
867                 priv->delayed_restore = 0;
868                 omap8250_restore_regs(p);
869         }
870
871         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
872                 uart_write_wakeup(&p->port);
873
874         if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
875                 int ret;
876
877                 ret = omap_8250_tx_dma(p);
878                 if (ret)
879                         en_thri = true;
880
881         } else if (p->capabilities & UART_CAP_RPM) {
882                 en_thri = true;
883         }
884
885         if (en_thri) {
886                 dma->tx_err = 1;
887                 p->ier |= UART_IER_THRI;
888                 serial_port_out(&p->port, UART_IER, p->ier);
889         }
890
891         spin_unlock_irqrestore(&p->port.lock, flags);
892 }
893
894 static int omap_8250_tx_dma(struct uart_8250_port *p)
895 {
896         struct uart_8250_dma            *dma = p->dma;
897         struct omap8250_priv            *priv = p->port.private_data;
898         struct circ_buf                 *xmit = &p->port.state->xmit;
899         struct dma_async_tx_descriptor  *desc;
900         unsigned int    skip_byte = 0;
901         int ret;
902
903         if (dma->tx_running)
904                 return 0;
905         if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
906
907                 /*
908                  * Even if no data, we need to return an error for the two cases
909                  * below so serial8250_tx_chars() is invoked and properly clears
910                  * THRI and/or runtime suspend.
911                  */
912                 if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
913                         ret = -EBUSY;
914                         goto err;
915                 }
916                 if (p->ier & UART_IER_THRI) {
917                         p->ier &= ~UART_IER_THRI;
918                         serial_out(p, UART_IER, p->ier);
919                 }
920                 return 0;
921         }
922
923         dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
924         if (priv->habit & OMAP_DMA_TX_KICK) {
925                 u8 tx_lvl;
926
927                 /*
928                  * We need to put the first byte into the FIFO in order to start
929                  * the DMA transfer. For transfers smaller than four bytes we
930                  * don't bother doing DMA at all. It seem not matter if there
931                  * are still bytes in the FIFO from the last transfer (in case
932                  * we got here directly from omap_8250_dma_tx_complete()). Bytes
933                  * leaving the FIFO seem not to trigger the DMA transfer. It is
934                  * really the byte that we put into the FIFO.
935                  * If the FIFO is already full then we most likely got here from
936                  * omap_8250_dma_tx_complete(). And this means the DMA engine
937                  * just completed its work. We don't have to wait the complete
938                  * 86us at 115200,8n1 but around 60us (not to mention lower
939                  * baudrates). So in that case we take the interrupt and try
940                  * again with an empty FIFO.
941                  */
942                 tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
943                 if (tx_lvl == p->tx_loadsz) {
944                         ret = -EBUSY;
945                         goto err;
946                 }
947                 if (dma->tx_size < 4) {
948                         ret = -EINVAL;
949                         goto err;
950                 }
951                 skip_byte = 1;
952         }
953
954         desc = dmaengine_prep_slave_single(dma->txchan,
955                         dma->tx_addr + xmit->tail + skip_byte,
956                         dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
957                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
958         if (!desc) {
959                 ret = -EBUSY;
960                 goto err;
961         }
962
963         dma->tx_running = 1;
964
965         desc->callback = omap_8250_dma_tx_complete;
966         desc->callback_param = p;
967
968         dma->tx_cookie = dmaengine_submit(desc);
969
970         dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
971                                    UART_XMIT_SIZE, DMA_TO_DEVICE);
972
973         dma_async_issue_pending(dma->txchan);
974         if (dma->tx_err)
975                 dma->tx_err = 0;
976
977         if (p->ier & UART_IER_THRI) {
978                 p->ier &= ~UART_IER_THRI;
979                 serial_out(p, UART_IER, p->ier);
980         }
981         if (skip_byte)
982                 serial_out(p, UART_TX, xmit->buf[xmit->tail]);
983         return 0;
984 err:
985         dma->tx_err = 1;
986         return ret;
987 }
988
989 /*
990  * This is mostly serial8250_handle_irq(). We have a slightly different DMA
991  * hoook for RX/TX and need different logic for them in the ISR. Therefore we
992  * use the default routine in the non-DMA case and this one for with DMA.
993  */
994 static int omap_8250_dma_handle_irq(struct uart_port *port)
995 {
996         struct uart_8250_port *up = up_to_u8250p(port);
997         unsigned char status;
998         unsigned long flags;
999         u8 iir;
1000         int dma_err = 0;
1001
1002         serial8250_rpm_get(up);
1003
1004         iir = serial_port_in(port, UART_IIR);
1005         if (iir & UART_IIR_NO_INT) {
1006                 serial8250_rpm_put(up);
1007                 return 0;
1008         }
1009
1010         spin_lock_irqsave(&port->lock, flags);
1011
1012         status = serial_port_in(port, UART_LSR);
1013
1014         if (status & (UART_LSR_DR | UART_LSR_BI)) {
1015
1016                 dma_err = omap_8250_rx_dma(up, iir);
1017                 if (dma_err) {
1018                         status = serial8250_rx_chars(up, status);
1019                         omap_8250_rx_dma(up, 0);
1020                 }
1021         }
1022         serial8250_modem_status(up);
1023         if (status & UART_LSR_THRE && up->dma->tx_err) {
1024                 if (uart_tx_stopped(&up->port) ||
1025                     uart_circ_empty(&up->port.state->xmit)) {
1026                         up->dma->tx_err = 0;
1027                         serial8250_tx_chars(up);
1028                 } else  {
1029                         /*
1030                          * try again due to an earlier failer which
1031                          * might have been resolved by now.
1032                          */
1033                         dma_err = omap_8250_tx_dma(up);
1034                         if (dma_err)
1035                                 serial8250_tx_chars(up);
1036                 }
1037         }
1038
1039         spin_unlock_irqrestore(&port->lock, flags);
1040         serial8250_rpm_put(up);
1041         return 1;
1042 }
1043
1044 static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1045 {
1046         return false;
1047 }
1048
1049 #else
1050
1051 static inline int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
1052 {
1053         return -EINVAL;
1054 }
1055 #endif
1056
1057 static int omap8250_no_handle_irq(struct uart_port *port)
1058 {
1059         /* IRQ has not been requested but handling irq? */
1060         WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1061         return 0;
1062 }
1063
1064 static int omap8250_probe(struct platform_device *pdev)
1065 {
1066         struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1067         struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1068         struct omap8250_priv *priv;
1069         struct uart_8250_port up;
1070         int ret;
1071         void __iomem *membase;
1072
1073         if (!regs || !irq) {
1074                 dev_err(&pdev->dev, "missing registers or irq\n");
1075                 return -EINVAL;
1076         }
1077
1078         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1079         if (!priv)
1080                 return -ENOMEM;
1081
1082         membase = devm_ioremap_nocache(&pdev->dev, regs->start,
1083                                        resource_size(regs));
1084         if (!membase)
1085                 return -ENODEV;
1086
1087         memset(&up, 0, sizeof(up));
1088         up.port.dev = &pdev->dev;
1089         up.port.mapbase = regs->start;
1090         up.port.membase = membase;
1091         up.port.irq = irq->start;
1092         /*
1093          * It claims to be 16C750 compatible however it is a little different.
1094          * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1095          * have) is enabled via EFR instead of MCR. The type is set here 8250
1096          * just to get things going. UNKNOWN does not work for a few reasons and
1097          * we don't need our own type since we don't use 8250's set_termios()
1098          * or pm callback.
1099          */
1100         up.port.type = PORT_8250;
1101         up.port.iotype = UPIO_MEM;
1102         up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
1103                 UPF_HARD_FLOW;
1104         up.port.private_data = priv;
1105
1106         up.port.regshift = 2;
1107         up.port.fifosize = 64;
1108         up.tx_loadsz = 64;
1109         up.capabilities = UART_CAP_FIFO;
1110 #ifdef CONFIG_PM
1111         /*
1112          * Runtime PM is mostly transparent. However to do it right we need to a
1113          * TX empty interrupt before we can put the device to auto idle. So if
1114          * PM is not enabled we don't add that flag and can spare that one extra
1115          * interrupt in the TX path.
1116          */
1117         up.capabilities |= UART_CAP_RPM;
1118 #endif
1119         up.port.set_termios = omap_8250_set_termios;
1120         up.port.set_mctrl = omap8250_set_mctrl;
1121         up.port.pm = omap_8250_pm;
1122         up.port.startup = omap_8250_startup;
1123         up.port.shutdown = omap_8250_shutdown;
1124         up.port.throttle = omap_8250_throttle;
1125         up.port.unthrottle = omap_8250_unthrottle;
1126
1127         if (pdev->dev.of_node) {
1128                 ret = of_alias_get_id(pdev->dev.of_node, "serial");
1129
1130                 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1131                                      &up.port.uartclk);
1132                 priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
1133         } else {
1134                 ret = pdev->id;
1135         }
1136         if (ret < 0) {
1137                 dev_err(&pdev->dev, "failed to get alias/pdev id\n");
1138                 return ret;
1139         }
1140         up.port.line = ret;
1141
1142         if (!up.port.uartclk) {
1143                 up.port.uartclk = DEFAULT_CLK_SPEED;
1144                 dev_warn(&pdev->dev,
1145                          "No clock speed specified: using default: %d\n",
1146                          DEFAULT_CLK_SPEED);
1147         }
1148
1149         priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1150         priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1151         pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
1152                            priv->latency);
1153         INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1154
1155         spin_lock_init(&priv->rx_dma_lock);
1156
1157         device_init_wakeup(&pdev->dev, true);
1158         pm_runtime_use_autosuspend(&pdev->dev);
1159         pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1160
1161         pm_runtime_irq_safe(&pdev->dev);
1162         pm_runtime_enable(&pdev->dev);
1163
1164         pm_runtime_get_sync(&pdev->dev);
1165
1166         omap_serial_fill_features_erratas(&up, priv);
1167         up.port.handle_irq = omap8250_no_handle_irq;
1168 #ifdef CONFIG_SERIAL_8250_DMA
1169         if (pdev->dev.of_node) {
1170                 /*
1171                  * Oh DMA support. If there are no DMA properties in the DT then
1172                  * we will fall back to a generic DMA channel which does not
1173                  * really work here. To ensure that we do not get a generic DMA
1174                  * channel assigned, we have the the_no_dma_filter_fn() here.
1175                  * To avoid "failed to request DMA" messages we check for DMA
1176                  * properties in DT.
1177                  */
1178                 ret = of_property_count_strings(pdev->dev.of_node, "dma-names");
1179                 if (ret == 2) {
1180                         up.dma = &priv->omap8250_dma;
1181                         priv->omap8250_dma.fn = the_no_dma_filter_fn;
1182                         priv->omap8250_dma.tx_dma = omap_8250_tx_dma;
1183                         priv->omap8250_dma.rx_dma = omap_8250_rx_dma;
1184                         priv->omap8250_dma.rx_size = RX_TRIGGER;
1185                         priv->omap8250_dma.rxconf.src_maxburst = RX_TRIGGER;
1186                         priv->omap8250_dma.txconf.dst_maxburst = TX_TRIGGER;
1187
1188                         if (of_machine_is_compatible("ti,am33xx"))
1189                                 priv->habit |= OMAP_DMA_TX_KICK;
1190                 }
1191         }
1192 #endif
1193         ret = serial8250_register_8250_port(&up);
1194         if (ret < 0) {
1195                 dev_err(&pdev->dev, "unable to register 8250 port\n");
1196                 goto err;
1197         }
1198         priv->line = ret;
1199         platform_set_drvdata(pdev, priv);
1200         pm_runtime_mark_last_busy(&pdev->dev);
1201         pm_runtime_put_autosuspend(&pdev->dev);
1202         return 0;
1203 err:
1204         pm_runtime_put(&pdev->dev);
1205         pm_runtime_disable(&pdev->dev);
1206         return ret;
1207 }
1208
1209 static int omap8250_remove(struct platform_device *pdev)
1210 {
1211         struct omap8250_priv *priv = platform_get_drvdata(pdev);
1212
1213         pm_runtime_put_sync(&pdev->dev);
1214         pm_runtime_disable(&pdev->dev);
1215         serial8250_unregister_port(priv->line);
1216         pm_qos_remove_request(&priv->pm_qos_request);
1217         device_init_wakeup(&pdev->dev, false);
1218         return 0;
1219 }
1220
1221 #ifdef CONFIG_PM_SLEEP
1222 static int omap8250_prepare(struct device *dev)
1223 {
1224         struct omap8250_priv *priv = dev_get_drvdata(dev);
1225
1226         if (!priv)
1227                 return 0;
1228         priv->is_suspending = true;
1229         return 0;
1230 }
1231
1232 static void omap8250_complete(struct device *dev)
1233 {
1234         struct omap8250_priv *priv = dev_get_drvdata(dev);
1235
1236         if (!priv)
1237                 return;
1238         priv->is_suspending = false;
1239 }
1240
1241 static int omap8250_suspend(struct device *dev)
1242 {
1243         struct omap8250_priv *priv = dev_get_drvdata(dev);
1244
1245         serial8250_suspend_port(priv->line);
1246         flush_work(&priv->qos_work);
1247         return 0;
1248 }
1249
1250 static int omap8250_resume(struct device *dev)
1251 {
1252         struct omap8250_priv *priv = dev_get_drvdata(dev);
1253
1254         serial8250_resume_port(priv->line);
1255         return 0;
1256 }
1257 #else
1258 #define omap8250_prepare NULL
1259 #define omap8250_complete NULL
1260 #endif
1261
1262 #ifdef CONFIG_PM
1263 static int omap8250_lost_context(struct uart_8250_port *up)
1264 {
1265         u32 val;
1266
1267         val = serial_in(up, UART_OMAP_MDR1);
1268         /*
1269          * If we lose context, then MDR1 is set to its reset value which is
1270          * UART_OMAP_MDR1_DISABLE. After set_termios() we set it either to 13x
1271          * or 16x but never to disable again.
1272          */
1273         if (val == UART_OMAP_MDR1_DISABLE)
1274                 return 1;
1275         return 0;
1276 }
1277
1278 static int omap8250_runtime_suspend(struct device *dev)
1279 {
1280         struct omap8250_priv *priv = dev_get_drvdata(dev);
1281         struct uart_8250_port *up;
1282
1283         up = serial8250_get_port(priv->line);
1284         /*
1285          * When using 'no_console_suspend', the console UART must not be
1286          * suspended. Since driver suspend is managed by runtime suspend,
1287          * preventing runtime suspend (by returning error) will keep device
1288          * active during suspend.
1289          */
1290         if (priv->is_suspending && !console_suspend_enabled) {
1291                 if (uart_console(&up->port))
1292                         return -EBUSY;
1293         }
1294
1295         if (up->dma && up->dma->rxchan)
1296                 omap_8250_rx_dma(up, UART_IIR_RX_TIMEOUT);
1297
1298         priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1299         schedule_work(&priv->qos_work);
1300
1301         return 0;
1302 }
1303
1304 static int omap8250_runtime_resume(struct device *dev)
1305 {
1306         struct omap8250_priv *priv = dev_get_drvdata(dev);
1307         struct uart_8250_port *up;
1308         int loss_cntx;
1309
1310         /* In case runtime-pm tries this before we are setup */
1311         if (!priv)
1312                 return 0;
1313
1314         up = serial8250_get_port(priv->line);
1315         loss_cntx = omap8250_lost_context(up);
1316
1317         if (loss_cntx)
1318                 omap8250_restore_regs(up);
1319
1320         if (up->dma && up->dma->rxchan)
1321                 omap_8250_rx_dma(up, 0);
1322
1323         priv->latency = priv->calc_latency;
1324         schedule_work(&priv->qos_work);
1325         return 0;
1326 }
1327 #endif
1328
1329 #ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1330 static int __init omap8250_console_fixup(void)
1331 {
1332         char *omap_str;
1333         char *options;
1334         u8 idx;
1335
1336         if (strstr(boot_command_line, "console=ttyS"))
1337                 /* user set a ttyS based name for the console */
1338                 return 0;
1339
1340         omap_str = strstr(boot_command_line, "console=ttyO");
1341         if (!omap_str)
1342                 /* user did not set ttyO based console, so we don't care */
1343                 return 0;
1344
1345         omap_str += 12;
1346         if ('0' <= *omap_str && *omap_str <= '9')
1347                 idx = *omap_str - '0';
1348         else
1349                 return 0;
1350
1351         omap_str++;
1352         if (omap_str[0] == ',') {
1353                 omap_str++;
1354                 options = omap_str;
1355         } else {
1356                 options = NULL;
1357         }
1358
1359         add_preferred_console("ttyS", idx, options);
1360         pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1361                idx, idx);
1362         pr_err("This ensures that you still see kernel messages. Please\n");
1363         pr_err("update your kernel commandline.\n");
1364         return 0;
1365 }
1366 console_initcall(omap8250_console_fixup);
1367 #endif
1368
1369 static const struct dev_pm_ops omap8250_dev_pm_ops = {
1370         SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1371         SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
1372                            omap8250_runtime_resume, NULL)
1373         .prepare        = omap8250_prepare,
1374         .complete       = omap8250_complete,
1375 };
1376
1377 static const struct of_device_id omap8250_dt_ids[] = {
1378         { .compatible = "ti,omap2-uart" },
1379         { .compatible = "ti,omap3-uart" },
1380         { .compatible = "ti,omap4-uart" },
1381         {},
1382 };
1383 MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1384
1385 static struct platform_driver omap8250_platform_driver = {
1386         .driver = {
1387                 .name           = "omap8250",
1388                 .pm             = &omap8250_dev_pm_ops,
1389                 .of_match_table = omap8250_dt_ids,
1390         },
1391         .probe                  = omap8250_probe,
1392         .remove                 = omap8250_remove,
1393 };
1394 module_platform_driver(omap8250_platform_driver);
1395
1396 MODULE_AUTHOR("Sebastian Andrzej Siewior");
1397 MODULE_DESCRIPTION("OMAP 8250 Driver");
1398 MODULE_LICENSE("GPL v2");