blob: e8b34f16ba2c95fc7420a20a1eca181ca62c4901 [file] [log] [blame]
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001/*
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>
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +020014#include <linux/serial_reg.h>
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +020015#include <linux/tty_flip.h>
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +020016#include <linux/platform_device.h>
17#include <linux/slab.h>
18#include <linux/of.h>
Sekhar Nori4fcdff92015-07-14 13:32:06 +053019#include <linux/of_device.h>
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +020020#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>
Tony Lindgrena3e362f2015-06-09 23:35:00 -070026#include <linux/pm_wakeirq.h>
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +020027#include <linux/dma-mapping.h>
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +020028
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)
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +020035#define OMAP_DMA_TX_KICK (1 << 2)
Sekhar Noricdb929e2015-07-14 13:32:07 +053036/*
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)
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +020041
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
Sekhar Noricdb929e2015-07-14 13:32:07 +053062/* 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
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +020068#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
94struct 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;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200103 u8 delayed_restore;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200104 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;
John Ognesseda0cd32015-04-27 13:52:33 +0200114 spinlock_t rx_dma_lock;
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200115 bool rx_dma_broken;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200116};
117
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700118#ifdef CONFIG_SERIAL_8250_DMA
119static void omap_8250_rx_dma_flush(struct uart_8250_port *p);
120#else
121static inline void omap_8250_rx_dma_flush(struct uart_8250_port *p) { }
122#endif
123
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200124static u32 uart_read(struct uart_8250_port *up, u32 reg)
125{
126 return readl(up->port.membase + (reg << up->port.regshift));
127}
128
Peter Hurley4bf4ea92014-12-30 20:28:15 -0500129static 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
Andy Shevchenko5db4f7f2016-08-16 15:06:54 +0300137 /*
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);
Peter Hurley4bf4ea92014-12-30 20:28:15 -0500149}
150
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200151/*
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 */
160static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
161 struct omap8250_priv *priv)
162{
163 u8 timeout = 255;
164 u8 old_mdr1;
165
166 old_mdr1 = serial_in(up, UART_OMAP_MDR1);
167 if (old_mdr1 == priv->mdr1)
168 return;
169
170 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
171 udelay(2);
172 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
173 UART_FCR_CLEAR_RCVR);
174 /*
175 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
176 * TX_FIFO_E bit is 1.
177 */
178 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
179 (UART_LSR_THRE | UART_LSR_DR))) {
180 timeout--;
181 if (!timeout) {
182 /* Should *never* happen. we warn and carry on */
183 dev_crit(up->port.dev, "Errata i202: timedout %x\n",
184 serial_in(up, UART_LSR));
185 break;
186 }
187 udelay(1);
188 }
189}
190
191static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
192 struct omap8250_priv *priv)
193{
194 unsigned int uartclk = port->uartclk;
195 unsigned int div_13, div_16;
196 unsigned int abs_d13, abs_d16;
197
198 /*
199 * Old custom speed handling.
200 */
201 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
202 priv->quot = port->custom_divisor & 0xffff;
203 /*
204 * I assume that nobody is using this. But hey, if somebody
205 * would like to specify the divisor _and_ the mode then the
206 * driver is ready and waiting for it.
207 */
208 if (port->custom_divisor & (1 << 16))
209 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
210 else
211 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
212 return;
213 }
214 div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
215 div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
216
217 if (!div_13)
218 div_13 = 1;
219 if (!div_16)
220 div_16 = 1;
221
222 abs_d13 = abs(baud - uartclk / 13 / div_13);
223 abs_d16 = abs(baud - uartclk / 16 / div_16);
224
225 if (abs_d13 >= abs_d16) {
226 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
227 priv->quot = div_16;
228 } else {
229 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
230 priv->quot = div_13;
231 }
232}
233
234static void omap8250_update_scr(struct uart_8250_port *up,
235 struct omap8250_priv *priv)
236{
237 u8 old_scr;
238
239 old_scr = serial_in(up, UART_OMAP_SCR);
240 if (old_scr == priv->scr)
241 return;
242
243 /*
244 * The manual recommends not to enable the DMA mode selector in the SCR
245 * (instead of the FCR) register _and_ selecting the DMA mode as one
246 * register write because this may lead to malfunction.
247 */
248 if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
249 serial_out(up, UART_OMAP_SCR,
250 priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
251 serial_out(up, UART_OMAP_SCR, priv->scr);
252}
253
Sekhar Nori6f035412015-07-14 13:32:05 +0530254static void omap8250_update_mdr1(struct uart_8250_port *up,
255 struct omap8250_priv *priv)
256{
257 if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
258 omap_8250_mdr1_errataset(up, priv);
259 else
260 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
261}
262
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200263static void omap8250_restore_regs(struct uart_8250_port *up)
264{
265 struct omap8250_priv *priv = up->port.private_data;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200266 struct uart_8250_dma *dma = up->dma;
267
268 if (dma && dma->tx_running) {
269 /*
270 * TCSANOW requests the change to occur immediately however if
271 * we have a TX-DMA operation in progress then it has been
272 * observed that it might stall and never complete. Therefore we
273 * delay DMA completes to prevent this hang from happen.
274 */
275 priv->delayed_restore = 1;
276 return;
277 }
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200278
279 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
280 serial_out(up, UART_EFR, UART_EFR_ECB);
281
282 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
Yegor Yefremov36fd95b2016-05-31 10:59:15 +0200283 serial8250_out_MCR(up, UART_MCR_TCRTLR);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200284 serial_out(up, UART_FCR, up->fcr);
285
286 omap8250_update_scr(up, priv);
287
288 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
289
290 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
291 OMAP_UART_TCR_HALT(52));
292 serial_out(up, UART_TI752_TLR,
293 TRIGGER_TLR_MASK(TX_TRIGGER) << UART_TI752_TLR_TX |
294 TRIGGER_TLR_MASK(RX_TRIGGER) << UART_TI752_TLR_RX);
295
296 serial_out(up, UART_LCR, 0);
297
298 /* drop TCR + TLR access, we setup XON/XOFF later */
Yegor Yefremov36fd95b2016-05-31 10:59:15 +0200299 serial8250_out_MCR(up, up->mcr);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200300 serial_out(up, UART_IER, up->ier);
301
302 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
303 serial_dl_write(up, priv->quot);
304
Peter Hurley9719acc2015-01-25 14:44:52 -0500305 serial_out(up, UART_EFR, priv->efr);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200306
307 /* Configure flow control */
308 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
309 serial_out(up, UART_XON1, priv->xon);
310 serial_out(up, UART_XOFF1, priv->xoff);
311
312 serial_out(up, UART_LCR, up->lcr);
Sekhar Nori6f035412015-07-14 13:32:05 +0530313
314 omap8250_update_mdr1(up, priv);
315
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200316 up->port.ops->set_mctrl(&up->port, up->port.mctrl);
317}
318
319/*
320 * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
321 * some differences in how we want to handle flow control.
322 */
323static void omap_8250_set_termios(struct uart_port *port,
324 struct ktermios *termios,
325 struct ktermios *old)
326{
Andy Shevchenko013e3582016-02-18 21:22:59 +0200327 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200328 struct omap8250_priv *priv = up->port.private_data;
329 unsigned char cval = 0;
330 unsigned int baud;
331
332 switch (termios->c_cflag & CSIZE) {
333 case CS5:
334 cval = UART_LCR_WLEN5;
335 break;
336 case CS6:
337 cval = UART_LCR_WLEN6;
338 break;
339 case CS7:
340 cval = UART_LCR_WLEN7;
341 break;
342 default:
343 case CS8:
344 cval = UART_LCR_WLEN8;
345 break;
346 }
347
348 if (termios->c_cflag & CSTOPB)
349 cval |= UART_LCR_STOP;
350 if (termios->c_cflag & PARENB)
351 cval |= UART_LCR_PARITY;
352 if (!(termios->c_cflag & PARODD))
353 cval |= UART_LCR_EPAR;
354 if (termios->c_cflag & CMSPAR)
355 cval |= UART_LCR_SPAR;
356
357 /*
358 * Ask the core to calculate the divisor for us.
359 */
360 baud = uart_get_baud_rate(port, termios, old,
361 port->uartclk / 16 / 0xffff,
362 port->uartclk / 13);
363 omap_8250_get_divisor(port, baud, priv);
364
365 /*
366 * Ok, we're now changing the port state. Do it with
367 * interrupts disabled.
368 */
369 pm_runtime_get_sync(port->dev);
370 spin_lock_irq(&port->lock);
371
372 /*
373 * Update the per-port timeout.
374 */
375 uart_update_timeout(port, termios->c_cflag, baud);
376
377 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
378 if (termios->c_iflag & INPCK)
379 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
380 if (termios->c_iflag & (IGNBRK | PARMRK))
381 up->port.read_status_mask |= UART_LSR_BI;
382
383 /*
384 * Characters to ignore
385 */
386 up->port.ignore_status_mask = 0;
387 if (termios->c_iflag & IGNPAR)
388 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
389 if (termios->c_iflag & IGNBRK) {
390 up->port.ignore_status_mask |= UART_LSR_BI;
391 /*
392 * If we're ignoring parity and break indicators,
393 * ignore overruns too (for real raw support).
394 */
395 if (termios->c_iflag & IGNPAR)
396 up->port.ignore_status_mask |= UART_LSR_OE;
397 }
398
399 /*
400 * ignore all characters if CREAD is not set
401 */
402 if ((termios->c_cflag & CREAD) == 0)
403 up->port.ignore_status_mask |= UART_LSR_DR;
404
405 /*
406 * Modem status interrupts
407 */
408 up->ier &= ~UART_IER_MSI;
409 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
410 up->ier |= UART_IER_MSI;
411
412 up->lcr = cval;
413 /* Up to here it was mostly serial8250_do_set_termios() */
414
415 /*
416 * We enable TRIG_GRANU for RX and TX and additionaly we set
417 * SCR_TX_EMPTY bit. The result is the following:
418 * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
419 * - less than RX_TRIGGER number of bytes will also cause an interrupt
420 * once the UART decides that there no new bytes arriving.
421 * - Once THRE is enabled, the interrupt will be fired once the FIFO is
422 * empty - the trigger level is ignored here.
423 *
424 * Once DMA is enabled:
425 * - UART will assert the TX DMA line once there is room for TX_TRIGGER
426 * bytes in the TX FIFO. On each assert the DMA engine will move
427 * TX_TRIGGER bytes into the FIFO.
428 * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
429 * the FIFO and move RX_TRIGGER bytes.
430 * This is because threshold and trigger values are the same.
431 */
432 up->fcr = UART_FCR_ENABLE_FIFO;
433 up->fcr |= TRIGGER_FCR_MASK(TX_TRIGGER) << OMAP_UART_FCR_TX_TRIG;
434 up->fcr |= TRIGGER_FCR_MASK(RX_TRIGGER) << OMAP_UART_FCR_RX_TRIG;
435
436 priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
437 OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
438
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200439 if (up->dma)
440 priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
441 OMAP_UART_SCR_DMAMODE_CTL;
442
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200443 priv->xon = termios->c_cc[VSTART];
444 priv->xoff = termios->c_cc[VSTOP];
445
446 priv->efr = 0;
Peter Hurley391f93f2015-01-25 14:44:51 -0500447 up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
448
Andy Shevchenko5db4f7f2016-08-16 15:06:54 +0300449 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
Peter Hurley9719acc2015-01-25 14:44:52 -0500450 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
Peter Hurley391f93f2015-01-25 14:44:51 -0500451 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Peter Hurley9719acc2015-01-25 14:44:52 -0500452 priv->efr |= UART_EFR_CTS;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200453 } else if (up->port.flags & UPF_SOFT_FLOW) {
454 /*
Peter Hurley5bac4b32015-06-27 09:28:55 -0400455 * OMAP rx s/w flow control is borked; the transmitter remains
456 * stuck off even if rx flow control is subsequently disabled
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200457 */
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200458
459 /*
460 * IXOFF Flag:
461 * Enable XON/XOFF flow control on output.
462 * Transmit XON1, XOFF1
463 */
Peter Hurley391f93f2015-01-25 14:44:51 -0500464 if (termios->c_iflag & IXOFF) {
465 up->port.status |= UPSTAT_AUTOXOFF;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200466 priv->efr |= OMAP_UART_SW_TX;
Peter Hurley391f93f2015-01-25 14:44:51 -0500467 }
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200468 }
469 omap8250_restore_regs(up);
470
471 spin_unlock_irq(&up->port.lock);
472 pm_runtime_mark_last_busy(port->dev);
473 pm_runtime_put_autosuspend(port->dev);
474
475 /* calculate wakeup latency constraint */
476 priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
477 priv->latency = priv->calc_latency;
478
479 schedule_work(&priv->qos_work);
480
481 /* Don't rewrite B0 */
482 if (tty_termios_baud_rate(termios))
483 tty_termios_encode_baud_rate(termios, baud, baud);
484}
485
486/* same as 8250 except that we may have extra flow bits set in EFR */
487static void omap_8250_pm(struct uart_port *port, unsigned int state,
488 unsigned int oldstate)
489{
Peter Hurley3e29af22014-12-31 16:32:49 -0500490 struct uart_8250_port *up = up_to_u8250p(port);
491 u8 efr;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200492
493 pm_runtime_get_sync(port->dev);
494 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Peter Hurley3e29af22014-12-31 16:32:49 -0500495 efr = serial_in(up, UART_EFR);
496 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200497 serial_out(up, UART_LCR, 0);
498
499 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
500 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Peter Hurley3e29af22014-12-31 16:32:49 -0500501 serial_out(up, UART_EFR, efr);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200502 serial_out(up, UART_LCR, 0);
503
504 pm_runtime_mark_last_busy(port->dev);
505 pm_runtime_put_autosuspend(port->dev);
506}
507
508static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
509 struct omap8250_priv *priv)
510{
511 u32 mvr, scheme;
512 u16 revision, major, minor;
513
514 mvr = uart_read(up, UART_OMAP_MVER);
515
516 /* Check revision register scheme */
517 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
518
519 switch (scheme) {
520 case 0: /* Legacy Scheme: OMAP2/3 */
521 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
522 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
523 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
524 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
525 break;
526 case 1:
527 /* New Scheme: OMAP4+ */
528 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
529 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
530 OMAP_UART_MVR_MAJ_SHIFT;
531 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
532 break;
533 default:
534 dev_warn(up->port.dev,
535 "Unknown revision, defaulting to highest\n");
536 /* highest possible revision */
537 major = 0xff;
538 minor = 0xff;
539 }
540 /* normalize revision for the driver */
541 revision = UART_BUILD_REVISION(major, minor);
542
543 switch (revision) {
544 case OMAP_UART_REV_46:
Sekhar Nori4fcdff92015-07-14 13:32:06 +0530545 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200546 break;
547 case OMAP_UART_REV_52:
Sekhar Nori4fcdff92015-07-14 13:32:06 +0530548 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200549 OMAP_UART_WER_HAS_TX_WAKEUP;
550 break;
551 case OMAP_UART_REV_63:
Sekhar Nori4fcdff92015-07-14 13:32:06 +0530552 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200553 OMAP_UART_WER_HAS_TX_WAKEUP;
554 break;
555 default:
556 break;
557 }
558}
559
560static void omap8250_uart_qos_work(struct work_struct *work)
561{
562 struct omap8250_priv *priv;
563
564 priv = container_of(work, struct omap8250_priv, qos_work);
565 pm_qos_update_request(&priv->pm_qos_request, priv->latency);
566}
567
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200568#ifdef CONFIG_SERIAL_8250_DMA
569static int omap_8250_dma_handle_irq(struct uart_port *port);
570#endif
571
572static irqreturn_t omap8250_irq(int irq, void *dev_id)
573{
574 struct uart_port *port = dev_id;
575 struct uart_8250_port *up = up_to_u8250p(port);
576 unsigned int iir;
577 int ret;
578
579#ifdef CONFIG_SERIAL_8250_DMA
580 if (up->dma) {
581 ret = omap_8250_dma_handle_irq(port);
582 return IRQ_RETVAL(ret);
583 }
584#endif
585
586 serial8250_rpm_get(up);
587 iir = serial_port_in(port, UART_IIR);
588 ret = serial8250_handle_irq(port, iir);
589 serial8250_rpm_put(up);
590
591 return IRQ_RETVAL(ret);
592}
593
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200594static int omap_8250_startup(struct uart_port *port)
595{
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200596 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200597 struct omap8250_priv *priv = port->private_data;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200598 int ret;
599
600 if (priv->wakeirq) {
Tony Lindgrena3e362f2015-06-09 23:35:00 -0700601 ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200602 if (ret)
603 return ret;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200604 }
605
606 pm_runtime_get_sync(port->dev);
607
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200608 up->mcr = 0;
609 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
610
611 serial_out(up, UART_LCR, UART_LCR_WLEN8);
612
613 up->lsr_saved_flags = 0;
614 up->msr_saved_flags = 0;
615
Vignesh R65db5642017-04-22 18:37:19 +0530616 /* Disable DMA for console UART */
617 if (uart_console(port))
618 up->dma = NULL;
619
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200620 if (up->dma) {
621 ret = serial8250_request_dma(up);
622 if (ret) {
623 dev_warn_ratelimited(port->dev,
624 "failed to request DMA\n");
625 up->dma = NULL;
626 }
627 }
628
629 ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
630 dev_name(port->dev), port);
631 if (ret < 0)
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200632 goto err;
633
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200634 up->ier = UART_IER_RLSI | UART_IER_RDI;
635 serial_out(up, UART_IER, up->ier);
636
Rafael J. Wysocki71504e52014-12-19 15:27:58 +0100637#ifdef CONFIG_PM
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200638 up->capabilities |= UART_CAP_RPM;
639#endif
640
641 /* Enable module level wake up */
642 priv->wer = OMAP_UART_WER_MOD_WKUP;
643 if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
644 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
645 serial_out(up, UART_OMAP_WER, priv->wer);
646
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200647 if (up->dma)
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700648 up->dma->rx_dma(up);
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200649
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200650 pm_runtime_mark_last_busy(port->dev);
651 pm_runtime_put_autosuspend(port->dev);
652 return 0;
653err:
654 pm_runtime_mark_last_busy(port->dev);
655 pm_runtime_put_autosuspend(port->dev);
Tony Lindgrena3e362f2015-06-09 23:35:00 -0700656 dev_pm_clear_wake_irq(port->dev);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200657 return ret;
658}
659
660static void omap_8250_shutdown(struct uart_port *port)
661{
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200662 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200663 struct omap8250_priv *priv = port->private_data;
664
665 flush_work(&priv->qos_work);
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200666 if (up->dma)
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700667 omap_8250_rx_dma_flush(up);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200668
669 pm_runtime_get_sync(port->dev);
670
671 serial_out(up, UART_OMAP_WER, 0);
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200672
673 up->ier = 0;
674 serial_out(up, UART_IER, 0);
675
676 if (up->dma)
677 serial8250_release_dma(up);
678
679 /*
680 * Disable break condition and FIFOs
681 */
682 if (up->lcr & UART_LCR_SBC)
683 serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
684 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200685
686 pm_runtime_mark_last_busy(port->dev);
687 pm_runtime_put_autosuspend(port->dev);
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200688 free_irq(port->irq, port);
Tony Lindgrena3e362f2015-06-09 23:35:00 -0700689 dev_pm_clear_wake_irq(port->dev);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200690}
691
692static void omap_8250_throttle(struct uart_port *port)
693{
Andy Shevchenko013e3582016-02-18 21:22:59 +0200694 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200695 unsigned long flags;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200696
697 pm_runtime_get_sync(port->dev);
698
699 spin_lock_irqsave(&port->lock, flags);
700 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
701 serial_out(up, UART_IER, up->ier);
702 spin_unlock_irqrestore(&port->lock, flags);
703
704 pm_runtime_mark_last_busy(port->dev);
705 pm_runtime_put_autosuspend(port->dev);
706}
707
Matwey V. Kornilov344cee22016-02-01 21:09:22 +0300708static int omap_8250_rs485_config(struct uart_port *port,
709 struct serial_rs485 *rs485)
710{
711 struct uart_8250_port *up = up_to_u8250p(port);
712
713 /* Clamp the delays to [0, 100ms] */
714 rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
715 rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U);
716
717 port->rs485 = *rs485;
718
719 /*
720 * Both serial8250_em485_init and serial8250_em485_destroy
721 * are idempotent
722 */
723 if (rs485->flags & SER_RS485_ENABLED) {
724 int ret = serial8250_em485_init(up);
725
726 if (ret) {
727 rs485->flags &= ~SER_RS485_ENABLED;
728 port->rs485.flags &= ~SER_RS485_ENABLED;
729 }
730 return ret;
731 }
732
733 serial8250_em485_destroy(up);
734
735 return 0;
736}
737
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200738static void omap_8250_unthrottle(struct uart_port *port)
739{
Andy Shevchenko013e3582016-02-18 21:22:59 +0200740 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200741 unsigned long flags;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200742
743 pm_runtime_get_sync(port->dev);
744
745 spin_lock_irqsave(&port->lock, flags);
746 up->ier |= UART_IER_RLSI | UART_IER_RDI;
747 serial_out(up, UART_IER, up->ier);
748 spin_unlock_irqrestore(&port->lock, flags);
749
750 pm_runtime_mark_last_busy(port->dev);
751 pm_runtime_put_autosuspend(port->dev);
752}
753
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200754#ifdef CONFIG_SERIAL_8250_DMA
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700755static int omap_8250_rx_dma(struct uart_8250_port *p);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200756
Peter Hurleyb74fdd22016-04-09 22:14:35 -0700757static void __dma_rx_do_complete(struct uart_8250_port *p)
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200758{
John Ognesseda0cd32015-04-27 13:52:33 +0200759 struct omap8250_priv *priv = p->port.private_data;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200760 struct uart_8250_dma *dma = p->dma;
761 struct tty_port *tty_port = &p->port.state->port;
762 struct dma_tx_state state;
763 int count;
John Ognesseda0cd32015-04-27 13:52:33 +0200764 unsigned long flags;
Sebastian Andrzej Siewior658e2eb2015-08-14 18:01:03 +0200765 int ret;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200766
John Ognesseda0cd32015-04-27 13:52:33 +0200767 spin_lock_irqsave(&priv->rx_dma_lock, flags);
768
769 if (!dma->rx_running)
770 goto unlock;
771
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200772 dma->rx_running = 0;
773 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200774
775 count = dma->rx_size - state.residue;
776
Sebastian Andrzej Siewior658e2eb2015-08-14 18:01:03 +0200777 ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
778
779 p->port.icount.rx += ret;
780 p->port.icount.buf_overrun += count - ret;
John Ognesseda0cd32015-04-27 13:52:33 +0200781unlock:
782 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
783
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200784 tty_flip_buffer_push(tty_port);
785}
786
787static void __dma_rx_complete(void *param)
788{
Peter Hurleyb74fdd22016-04-09 22:14:35 -0700789 __dma_rx_do_complete(param);
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700790 omap_8250_rx_dma(param);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200791}
792
John Ognesseda0cd32015-04-27 13:52:33 +0200793static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
794{
795 struct omap8250_priv *priv = p->port.private_data;
796 struct uart_8250_dma *dma = p->dma;
797 unsigned long flags;
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200798 int ret;
John Ognesseda0cd32015-04-27 13:52:33 +0200799
800 spin_lock_irqsave(&priv->rx_dma_lock, flags);
801
802 if (!dma->rx_running) {
803 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
804 return;
805 }
806
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200807 ret = dmaengine_pause(dma->rxchan);
808 if (WARN_ON_ONCE(ret))
809 priv->rx_dma_broken = true;
John Ognesseda0cd32015-04-27 13:52:33 +0200810
811 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
812
Peter Hurleyb74fdd22016-04-09 22:14:35 -0700813 __dma_rx_do_complete(p);
Peter Hurleyd1f98152016-04-09 22:14:37 -0700814 dmaengine_terminate_all(dma->rxchan);
John Ognesseda0cd32015-04-27 13:52:33 +0200815}
816
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700817static int omap_8250_rx_dma(struct uart_8250_port *p)
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200818{
John Ognesseda0cd32015-04-27 13:52:33 +0200819 struct omap8250_priv *priv = p->port.private_data;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200820 struct uart_8250_dma *dma = p->dma;
John Ognesseda0cd32015-04-27 13:52:33 +0200821 int err = 0;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200822 struct dma_async_tx_descriptor *desc;
John Ognesseda0cd32015-04-27 13:52:33 +0200823 unsigned long flags;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200824
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200825 if (priv->rx_dma_broken)
826 return -EINVAL;
827
John Ognesseda0cd32015-04-27 13:52:33 +0200828 spin_lock_irqsave(&priv->rx_dma_lock, flags);
829
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200830 if (dma->rx_running)
John Ognesseda0cd32015-04-27 13:52:33 +0200831 goto out;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200832
833 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
834 dma->rx_size, DMA_DEV_TO_MEM,
835 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
John Ognesseda0cd32015-04-27 13:52:33 +0200836 if (!desc) {
837 err = -EBUSY;
838 goto out;
839 }
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200840
841 dma->rx_running = 1;
842 desc->callback = __dma_rx_complete;
843 desc->callback_param = p;
844
845 dma->rx_cookie = dmaengine_submit(desc);
846
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200847 dma_async_issue_pending(dma->rxchan);
John Ognesseda0cd32015-04-27 13:52:33 +0200848out:
849 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
850 return err;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200851}
852
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200853static int omap_8250_tx_dma(struct uart_8250_port *p);
854
855static void omap_8250_dma_tx_complete(void *param)
856{
857 struct uart_8250_port *p = param;
858 struct uart_8250_dma *dma = p->dma;
859 struct circ_buf *xmit = &p->port.state->xmit;
860 unsigned long flags;
861 bool en_thri = false;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200862 struct omap8250_priv *priv = p->port.private_data;
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200863
864 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
865 UART_XMIT_SIZE, DMA_TO_DEVICE);
866
867 spin_lock_irqsave(&p->port.lock, flags);
868
869 dma->tx_running = 0;
870
871 xmit->tail += dma->tx_size;
872 xmit->tail &= UART_XMIT_SIZE - 1;
873 p->port.icount.tx += dma->tx_size;
874
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200875 if (priv->delayed_restore) {
876 priv->delayed_restore = 0;
877 omap8250_restore_regs(p);
878 }
879
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200880 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
881 uart_write_wakeup(&p->port);
882
883 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
884 int ret;
885
886 ret = omap_8250_tx_dma(p);
887 if (ret)
888 en_thri = true;
889
890 } else if (p->capabilities & UART_CAP_RPM) {
891 en_thri = true;
892 }
893
894 if (en_thri) {
895 dma->tx_err = 1;
896 p->ier |= UART_IER_THRI;
897 serial_port_out(&p->port, UART_IER, p->ier);
898 }
899
900 spin_unlock_irqrestore(&p->port.lock, flags);
901}
902
903static int omap_8250_tx_dma(struct uart_8250_port *p)
904{
905 struct uart_8250_dma *dma = p->dma;
906 struct omap8250_priv *priv = p->port.private_data;
907 struct circ_buf *xmit = &p->port.state->xmit;
908 struct dma_async_tx_descriptor *desc;
909 unsigned int skip_byte = 0;
910 int ret;
911
912 if (dma->tx_running)
913 return 0;
914 if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
915
916 /*
917 * Even if no data, we need to return an error for the two cases
918 * below so serial8250_tx_chars() is invoked and properly clears
919 * THRI and/or runtime suspend.
920 */
921 if (dma->tx_err || p->capabilities & UART_CAP_RPM) {
922 ret = -EBUSY;
923 goto err;
924 }
925 if (p->ier & UART_IER_THRI) {
926 p->ier &= ~UART_IER_THRI;
927 serial_out(p, UART_IER, p->ier);
928 }
929 return 0;
930 }
931
932 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
933 if (priv->habit & OMAP_DMA_TX_KICK) {
934 u8 tx_lvl;
935
936 /*
937 * We need to put the first byte into the FIFO in order to start
938 * the DMA transfer. For transfers smaller than four bytes we
939 * don't bother doing DMA at all. It seem not matter if there
940 * are still bytes in the FIFO from the last transfer (in case
941 * we got here directly from omap_8250_dma_tx_complete()). Bytes
942 * leaving the FIFO seem not to trigger the DMA transfer. It is
943 * really the byte that we put into the FIFO.
944 * If the FIFO is already full then we most likely got here from
945 * omap_8250_dma_tx_complete(). And this means the DMA engine
946 * just completed its work. We don't have to wait the complete
947 * 86us at 115200,8n1 but around 60us (not to mention lower
948 * baudrates). So in that case we take the interrupt and try
949 * again with an empty FIFO.
950 */
951 tx_lvl = serial_in(p, UART_OMAP_TX_LVL);
952 if (tx_lvl == p->tx_loadsz) {
953 ret = -EBUSY;
954 goto err;
955 }
956 if (dma->tx_size < 4) {
957 ret = -EINVAL;
958 goto err;
959 }
960 skip_byte = 1;
961 }
962
963 desc = dmaengine_prep_slave_single(dma->txchan,
964 dma->tx_addr + xmit->tail + skip_byte,
965 dma->tx_size - skip_byte, DMA_MEM_TO_DEV,
966 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
967 if (!desc) {
968 ret = -EBUSY;
969 goto err;
970 }
971
972 dma->tx_running = 1;
973
974 desc->callback = omap_8250_dma_tx_complete;
975 desc->callback_param = p;
976
977 dma->tx_cookie = dmaengine_submit(desc);
978
979 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
980 UART_XMIT_SIZE, DMA_TO_DEVICE);
981
982 dma_async_issue_pending(dma->txchan);
983 if (dma->tx_err)
984 dma->tx_err = 0;
985
986 if (p->ier & UART_IER_THRI) {
987 p->ier &= ~UART_IER_THRI;
988 serial_out(p, UART_IER, p->ier);
989 }
990 if (skip_byte)
991 serial_out(p, UART_TX, xmit->buf[xmit->tail]);
992 return 0;
993err:
994 dma->tx_err = 1;
995 return ret;
996}
997
Peter Hurley33d9b8b22016-04-09 22:14:36 -0700998static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
999{
1000 switch (iir & 0x3f) {
1001 case UART_IIR_RLSI:
1002 case UART_IIR_RX_TIMEOUT:
1003 case UART_IIR_RDI:
1004 omap_8250_rx_dma_flush(up);
1005 return true;
1006 }
1007 return omap_8250_rx_dma(up);
1008}
1009
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001010/*
1011 * This is mostly serial8250_handle_irq(). We have a slightly different DMA
1012 * hoook for RX/TX and need different logic for them in the ISR. Therefore we
1013 * use the default routine in the non-DMA case and this one for with DMA.
1014 */
1015static int omap_8250_dma_handle_irq(struct uart_port *port)
1016{
1017 struct uart_8250_port *up = up_to_u8250p(port);
1018 unsigned char status;
1019 unsigned long flags;
1020 u8 iir;
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001021
1022 serial8250_rpm_get(up);
1023
1024 iir = serial_port_in(port, UART_IIR);
1025 if (iir & UART_IIR_NO_INT) {
1026 serial8250_rpm_put(up);
1027 return 0;
1028 }
1029
1030 spin_lock_irqsave(&port->lock, flags);
1031
1032 status = serial_port_in(port, UART_LSR);
1033
1034 if (status & (UART_LSR_DR | UART_LSR_BI)) {
Peter Hurley33d9b8b22016-04-09 22:14:36 -07001035 if (handle_rx_dma(up, iir)) {
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001036 status = serial8250_rx_chars(up, status);
Peter Hurley33d9b8b22016-04-09 22:14:36 -07001037 omap_8250_rx_dma(up);
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001038 }
1039 }
1040 serial8250_modem_status(up);
1041 if (status & UART_LSR_THRE && up->dma->tx_err) {
1042 if (uart_tx_stopped(&up->port) ||
1043 uart_circ_empty(&up->port.state->xmit)) {
1044 up->dma->tx_err = 0;
1045 serial8250_tx_chars(up);
1046 } else {
1047 /*
1048 * try again due to an earlier failer which
1049 * might have been resolved by now.
1050 */
Peter Hurleya86f50e2016-04-09 20:49:41 -07001051 if (omap_8250_tx_dma(up))
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001052 serial8250_tx_chars(up);
1053 }
1054 }
1055
1056 spin_unlock_irqrestore(&port->lock, flags);
1057 serial8250_rpm_put(up);
1058 return 1;
1059}
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001060
1061static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1062{
1063 return false;
1064}
1065
1066#else
1067
Peter Hurley33d9b8b22016-04-09 22:14:36 -07001068static inline int omap_8250_rx_dma(struct uart_8250_port *p)
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001069{
1070 return -EINVAL;
1071}
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +02001072#endif
1073
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +02001074static int omap8250_no_handle_irq(struct uart_port *port)
1075{
1076 /* IRQ has not been requested but handling irq? */
1077 WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1078 return 0;
1079}
1080
Sekhar Noricdb929e2015-07-14 13:32:07 +05301081static const u8 am3352_habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE;
Vignesh R80b0d7e2017-04-04 19:32:22 +00001082static const u8 dra742_habit = UART_ERRATA_CLOCK_DISABLE;
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301083
1084static const struct of_device_id omap8250_dt_ids[] = {
1085 { .compatible = "ti,omap2-uart" },
1086 { .compatible = "ti,omap3-uart" },
1087 { .compatible = "ti,omap4-uart" },
1088 { .compatible = "ti,am3352-uart", .data = &am3352_habit, },
Vignesh R80b0d7e2017-04-04 19:32:22 +00001089 { .compatible = "ti,am4372-uart", .data = &am3352_habit, },
1090 { .compatible = "ti,dra742-uart", .data = &dra742_habit, },
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301091 {},
1092};
1093MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1094
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001095static int omap8250_probe(struct platform_device *pdev)
1096{
1097 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1098 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1099 struct omap8250_priv *priv;
1100 struct uart_8250_port up;
1101 int ret;
1102 void __iomem *membase;
1103
1104 if (!regs || !irq) {
1105 dev_err(&pdev->dev, "missing registers or irq\n");
1106 return -EINVAL;
1107 }
1108
1109 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1110 if (!priv)
1111 return -ENOMEM;
1112
1113 membase = devm_ioremap_nocache(&pdev->dev, regs->start,
1114 resource_size(regs));
1115 if (!membase)
1116 return -ENODEV;
1117
1118 memset(&up, 0, sizeof(up));
1119 up.port.dev = &pdev->dev;
1120 up.port.mapbase = regs->start;
1121 up.port.membase = membase;
1122 up.port.irq = irq->start;
1123 /*
1124 * It claims to be 16C750 compatible however it is a little different.
1125 * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1126 * have) is enabled via EFR instead of MCR. The type is set here 8250
1127 * just to get things going. UNKNOWN does not work for a few reasons and
1128 * we don't need our own type since we don't use 8250's set_termios()
1129 * or pm callback.
1130 */
1131 up.port.type = PORT_8250;
1132 up.port.iotype = UPIO_MEM;
1133 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
1134 UPF_HARD_FLOW;
1135 up.port.private_data = priv;
1136
1137 up.port.regshift = 2;
1138 up.port.fifosize = 64;
1139 up.tx_loadsz = 64;
1140 up.capabilities = UART_CAP_FIFO;
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001141#ifdef CONFIG_PM
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001142 /*
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001143 * Runtime PM is mostly transparent. However to do it right we need to a
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001144 * TX empty interrupt before we can put the device to auto idle. So if
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001145 * PM is not enabled we don't add that flag and can spare that one extra
1146 * interrupt in the TX path.
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001147 */
1148 up.capabilities |= UART_CAP_RPM;
1149#endif
1150 up.port.set_termios = omap_8250_set_termios;
Peter Hurley4bf4ea92014-12-30 20:28:15 -05001151 up.port.set_mctrl = omap8250_set_mctrl;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001152 up.port.pm = omap_8250_pm;
1153 up.port.startup = omap_8250_startup;
1154 up.port.shutdown = omap_8250_shutdown;
1155 up.port.throttle = omap_8250_throttle;
1156 up.port.unthrottle = omap_8250_unthrottle;
Matwey V. Kornilov344cee22016-02-01 21:09:22 +03001157 up.port.rs485_config = omap_8250_rs485_config;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001158
1159 if (pdev->dev.of_node) {
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301160 const struct of_device_id *id;
1161
Sebastian Andrzej Siewior54178fe2014-11-12 10:28:33 +01001162 ret = of_alias_get_id(pdev->dev.of_node, "serial");
1163
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001164 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1165 &up.port.uartclk);
1166 priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301167
1168 id = of_match_device(of_match_ptr(omap8250_dt_ids), &pdev->dev);
1169 if (id && id->data)
1170 priv->habit |= *(u8 *)id->data;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001171 } else {
Sebastian Andrzej Siewior54178fe2014-11-12 10:28:33 +01001172 ret = pdev->id;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001173 }
Sebastian Andrzej Siewior54178fe2014-11-12 10:28:33 +01001174 if (ret < 0) {
1175 dev_err(&pdev->dev, "failed to get alias/pdev id\n");
1176 return ret;
1177 }
1178 up.port.line = ret;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001179
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001180 if (!up.port.uartclk) {
1181 up.port.uartclk = DEFAULT_CLK_SPEED;
1182 dev_warn(&pdev->dev,
1183 "No clock speed specified: using default: %d\n",
1184 DEFAULT_CLK_SPEED);
1185 }
1186
1187 priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1188 priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1189 pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
1190 priv->latency);
1191 INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1192
John Ognesseda0cd32015-04-27 13:52:33 +02001193 spin_lock_init(&priv->rx_dma_lock);
1194
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001195 device_init_wakeup(&pdev->dev, true);
1196 pm_runtime_use_autosuspend(&pdev->dev);
1197 pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1198
1199 pm_runtime_irq_safe(&pdev->dev);
1200 pm_runtime_enable(&pdev->dev);
1201
1202 pm_runtime_get_sync(&pdev->dev);
1203
1204 omap_serial_fill_features_erratas(&up, priv);
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +02001205 up.port.handle_irq = omap8250_no_handle_irq;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001206#ifdef CONFIG_SERIAL_8250_DMA
1207 if (pdev->dev.of_node) {
1208 /*
1209 * Oh DMA support. If there are no DMA properties in the DT then
1210 * we will fall back to a generic DMA channel which does not
1211 * really work here. To ensure that we do not get a generic DMA
1212 * channel assigned, we have the the_no_dma_filter_fn() here.
1213 * To avoid "failed to request DMA" messages we check for DMA
1214 * properties in DT.
1215 */
1216 ret = of_property_count_strings(pdev->dev.of_node, "dma-names");
1217 if (ret == 2) {
1218 up.dma = &priv->omap8250_dma;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001219 priv->omap8250_dma.fn = the_no_dma_filter_fn;
1220 priv->omap8250_dma.tx_dma = omap_8250_tx_dma;
1221 priv->omap8250_dma.rx_dma = omap_8250_rx_dma;
1222 priv->omap8250_dma.rx_size = RX_TRIGGER;
1223 priv->omap8250_dma.rxconf.src_maxburst = RX_TRIGGER;
1224 priv->omap8250_dma.txconf.dst_maxburst = TX_TRIGGER;
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +02001225 /*
1226 * pause is currently not supported atleast on omap-sdma
1227 * and edma on most earlier kernels.
1228 */
1229 priv->rx_dma_broken = true;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001230 }
1231 }
1232#endif
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001233 ret = serial8250_register_8250_port(&up);
1234 if (ret < 0) {
1235 dev_err(&pdev->dev, "unable to register 8250 port\n");
1236 goto err;
1237 }
1238 priv->line = ret;
1239 platform_set_drvdata(pdev, priv);
1240 pm_runtime_mark_last_busy(&pdev->dev);
1241 pm_runtime_put_autosuspend(&pdev->dev);
1242 return 0;
1243err:
Tony Lindgren71fd9a92017-01-20 12:22:31 -08001244 pm_runtime_dont_use_autosuspend(&pdev->dev);
1245 pm_runtime_put_sync(&pdev->dev);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001246 pm_runtime_disable(&pdev->dev);
1247 return ret;
1248}
1249
1250static int omap8250_remove(struct platform_device *pdev)
1251{
1252 struct omap8250_priv *priv = platform_get_drvdata(pdev);
1253
Tony Lindgren71fd9a92017-01-20 12:22:31 -08001254 pm_runtime_dont_use_autosuspend(&pdev->dev);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001255 pm_runtime_put_sync(&pdev->dev);
1256 pm_runtime_disable(&pdev->dev);
1257 serial8250_unregister_port(priv->line);
1258 pm_qos_remove_request(&priv->pm_qos_request);
1259 device_init_wakeup(&pdev->dev, false);
1260 return 0;
1261}
1262
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001263#ifdef CONFIG_PM_SLEEP
1264static int omap8250_prepare(struct device *dev)
1265{
1266 struct omap8250_priv *priv = dev_get_drvdata(dev);
1267
1268 if (!priv)
1269 return 0;
1270 priv->is_suspending = true;
1271 return 0;
1272}
1273
1274static void omap8250_complete(struct device *dev)
1275{
1276 struct omap8250_priv *priv = dev_get_drvdata(dev);
1277
1278 if (!priv)
1279 return;
1280 priv->is_suspending = false;
1281}
1282
1283static int omap8250_suspend(struct device *dev)
1284{
1285 struct omap8250_priv *priv = dev_get_drvdata(dev);
1286
1287 serial8250_suspend_port(priv->line);
1288 flush_work(&priv->qos_work);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001289 return 0;
1290}
1291
1292static int omap8250_resume(struct device *dev)
1293{
1294 struct omap8250_priv *priv = dev_get_drvdata(dev);
1295
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001296 serial8250_resume_port(priv->line);
1297 return 0;
1298}
1299#else
1300#define omap8250_prepare NULL
1301#define omap8250_complete NULL
1302#endif
1303
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001304#ifdef CONFIG_PM
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001305static int omap8250_lost_context(struct uart_8250_port *up)
1306{
1307 u32 val;
1308
Sekhar Noricdb929e2015-07-14 13:32:07 +05301309 val = serial_in(up, UART_OMAP_SCR);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001310 /*
Sekhar Noricdb929e2015-07-14 13:32:07 +05301311 * If we lose context, then SCR is set to its reset value of zero.
1312 * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
1313 * among other bits, to never set the register back to zero again.
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001314 */
Sekhar Noricdb929e2015-07-14 13:32:07 +05301315 if (!val)
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001316 return 1;
1317 return 0;
1318}
1319
Sekhar Noricdb929e2015-07-14 13:32:07 +05301320/* TODO: in future, this should happen via API in drivers/reset/ */
1321static int omap8250_soft_reset(struct device *dev)
1322{
1323 struct omap8250_priv *priv = dev_get_drvdata(dev);
1324 struct uart_8250_port *up = serial8250_get_port(priv->line);
1325 int timeout = 100;
1326 int sysc;
1327 int syss;
1328
1329 sysc = serial_in(up, UART_OMAP_SYSC);
1330
1331 /* softreset the UART */
1332 sysc |= OMAP_UART_SYSC_SOFTRESET;
1333 serial_out(up, UART_OMAP_SYSC, sysc);
1334
1335 /* By experiments, 1us enough for reset complete on AM335x */
1336 do {
1337 udelay(1);
1338 syss = serial_in(up, UART_OMAP_SYSS);
1339 } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
1340
1341 if (!timeout) {
1342 dev_err(dev, "timed out waiting for reset done\n");
1343 return -ETIMEDOUT;
1344 }
1345
1346 return 0;
1347}
1348
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001349static int omap8250_runtime_suspend(struct device *dev)
1350{
1351 struct omap8250_priv *priv = dev_get_drvdata(dev);
1352 struct uart_8250_port *up;
1353
Tony Lindgren71fd9a92017-01-20 12:22:31 -08001354 /* In case runtime-pm tries this before we are setup */
1355 if (!priv)
1356 return 0;
1357
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001358 up = serial8250_get_port(priv->line);
1359 /*
1360 * When using 'no_console_suspend', the console UART must not be
1361 * suspended. Since driver suspend is managed by runtime suspend,
1362 * preventing runtime suspend (by returning error) will keep device
1363 * active during suspend.
1364 */
1365 if (priv->is_suspending && !console_suspend_enabled) {
1366 if (uart_console(&up->port))
1367 return -EBUSY;
1368 }
1369
Sekhar Noricdb929e2015-07-14 13:32:07 +05301370 if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
1371 int ret;
1372
1373 ret = omap8250_soft_reset(dev);
1374 if (ret)
1375 return ret;
1376
1377 /* Restore to UART mode after reset (for wakeup) */
1378 omap8250_update_mdr1(up, priv);
1379 }
1380
Sekhar Nori727fd8a2015-07-14 13:32:03 +05301381 if (up->dma && up->dma->rxchan)
Peter Hurley33d9b8b22016-04-09 22:14:36 -07001382 omap_8250_rx_dma_flush(up);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001383
1384 priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1385 schedule_work(&priv->qos_work);
1386
1387 return 0;
1388}
1389
1390static int omap8250_runtime_resume(struct device *dev)
1391{
1392 struct omap8250_priv *priv = dev_get_drvdata(dev);
1393 struct uart_8250_port *up;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001394
1395 /* In case runtime-pm tries this before we are setup */
1396 if (!priv)
1397 return 0;
1398
1399 up = serial8250_get_port(priv->line);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001400
Peter Hurleyf56f0a52016-04-09 20:49:43 -07001401 if (omap8250_lost_context(up))
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001402 omap8250_restore_regs(up);
1403
Sekhar Nori727fd8a2015-07-14 13:32:03 +05301404 if (up->dma && up->dma->rxchan)
Peter Hurley33d9b8b22016-04-09 22:14:36 -07001405 omap_8250_rx_dma(up);
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001406
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001407 priv->latency = priv->calc_latency;
1408 schedule_work(&priv->qos_work);
1409 return 0;
1410}
1411#endif
1412
Sebastian Andrzej Siewior00648d02014-12-18 18:47:12 +01001413#ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1414static int __init omap8250_console_fixup(void)
1415{
1416 char *omap_str;
1417 char *options;
1418 u8 idx;
1419
1420 if (strstr(boot_command_line, "console=ttyS"))
1421 /* user set a ttyS based name for the console */
1422 return 0;
1423
1424 omap_str = strstr(boot_command_line, "console=ttyO");
1425 if (!omap_str)
1426 /* user did not set ttyO based console, so we don't care */
1427 return 0;
1428
1429 omap_str += 12;
1430 if ('0' <= *omap_str && *omap_str <= '9')
1431 idx = *omap_str - '0';
1432 else
1433 return 0;
1434
1435 omap_str++;
1436 if (omap_str[0] == ',') {
1437 omap_str++;
1438 options = omap_str;
1439 } else {
1440 options = NULL;
1441 }
1442
1443 add_preferred_console("ttyS", idx, options);
1444 pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1445 idx, idx);
1446 pr_err("This ensures that you still see kernel messages. Please\n");
1447 pr_err("update your kernel commandline.\n");
1448 return 0;
1449}
1450console_initcall(omap8250_console_fixup);
1451#endif
1452
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001453static const struct dev_pm_ops omap8250_dev_pm_ops = {
1454 SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1455 SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
1456 omap8250_runtime_resume, NULL)
1457 .prepare = omap8250_prepare,
1458 .complete = omap8250_complete,
1459};
1460
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001461static struct platform_driver omap8250_platform_driver = {
1462 .driver = {
1463 .name = "omap8250",
1464 .pm = &omap8250_dev_pm_ops,
1465 .of_match_table = omap8250_dt_ids,
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001466 },
1467 .probe = omap8250_probe,
1468 .remove = omap8250_remove,
1469};
1470module_platform_driver(omap8250_platform_driver);
1471
1472MODULE_AUTHOR("Sebastian Andrzej Siewior");
1473MODULE_DESCRIPTION("OMAP 8250 Driver");
1474MODULE_LICENSE("GPL v2");