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