blob: 27702d5e88111dfa301cc48668942a9c9ba29f41 [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
118static u32 uart_read(struct uart_8250_port *up, u32 reg)
119{
120 return readl(up->port.membase + (reg << up->port.regshift));
121}
122
Peter Hurley4bf4ea92014-12-30 20:28:15 -0500123static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
124{
125 struct uart_8250_port *up = up_to_u8250p(port);
126 struct omap8250_priv *priv = up->port.private_data;
127 u8 lcr;
128
129 serial8250_do_set_mctrl(port, mctrl);
130
131 /*
132 * Turn off autoRTS if RTS is lowered and restore autoRTS setting
133 * if RTS is raised
134 */
135 lcr = serial_in(up, UART_LCR);
136 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Peter Hurley9719acc2015-01-25 14:44:52 -0500137 if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
138 priv->efr |= UART_EFR_RTS;
Peter Hurley4bf4ea92014-12-30 20:28:15 -0500139 else
Peter Hurley9719acc2015-01-25 14:44:52 -0500140 priv->efr &= ~UART_EFR_RTS;
141 serial_out(up, UART_EFR, priv->efr);
Peter Hurley4bf4ea92014-12-30 20:28:15 -0500142 serial_out(up, UART_LCR, lcr);
143}
144
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200145/*
146 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
147 * The access to uart register after MDR1 Access
148 * causes UART to corrupt data.
149 *
150 * Need a delay =
151 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
152 * give 10 times as much
153 */
154static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
155 struct omap8250_priv *priv)
156{
157 u8 timeout = 255;
158 u8 old_mdr1;
159
160 old_mdr1 = serial_in(up, UART_OMAP_MDR1);
161 if (old_mdr1 == priv->mdr1)
162 return;
163
164 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
165 udelay(2);
166 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
167 UART_FCR_CLEAR_RCVR);
168 /*
169 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
170 * TX_FIFO_E bit is 1.
171 */
172 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
173 (UART_LSR_THRE | UART_LSR_DR))) {
174 timeout--;
175 if (!timeout) {
176 /* Should *never* happen. we warn and carry on */
177 dev_crit(up->port.dev, "Errata i202: timedout %x\n",
178 serial_in(up, UART_LSR));
179 break;
180 }
181 udelay(1);
182 }
183}
184
185static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
186 struct omap8250_priv *priv)
187{
188 unsigned int uartclk = port->uartclk;
189 unsigned int div_13, div_16;
190 unsigned int abs_d13, abs_d16;
191
192 /*
193 * Old custom speed handling.
194 */
195 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
196 priv->quot = port->custom_divisor & 0xffff;
197 /*
198 * I assume that nobody is using this. But hey, if somebody
199 * would like to specify the divisor _and_ the mode then the
200 * driver is ready and waiting for it.
201 */
202 if (port->custom_divisor & (1 << 16))
203 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
204 else
205 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
206 return;
207 }
208 div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
209 div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
210
211 if (!div_13)
212 div_13 = 1;
213 if (!div_16)
214 div_16 = 1;
215
216 abs_d13 = abs(baud - uartclk / 13 / div_13);
217 abs_d16 = abs(baud - uartclk / 16 / div_16);
218
219 if (abs_d13 >= abs_d16) {
220 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
221 priv->quot = div_16;
222 } else {
223 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
224 priv->quot = div_13;
225 }
226}
227
228static void omap8250_update_scr(struct uart_8250_port *up,
229 struct omap8250_priv *priv)
230{
231 u8 old_scr;
232
233 old_scr = serial_in(up, UART_OMAP_SCR);
234 if (old_scr == priv->scr)
235 return;
236
237 /*
238 * The manual recommends not to enable the DMA mode selector in the SCR
239 * (instead of the FCR) register _and_ selecting the DMA mode as one
240 * register write because this may lead to malfunction.
241 */
242 if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
243 serial_out(up, UART_OMAP_SCR,
244 priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
245 serial_out(up, UART_OMAP_SCR, priv->scr);
246}
247
Sekhar Nori6f035412015-07-14 13:32:05 +0530248static void omap8250_update_mdr1(struct uart_8250_port *up,
249 struct omap8250_priv *priv)
250{
251 if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
252 omap_8250_mdr1_errataset(up, priv);
253 else
254 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
255}
256
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200257static void omap8250_restore_regs(struct uart_8250_port *up)
258{
259 struct omap8250_priv *priv = up->port.private_data;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200260 struct uart_8250_dma *dma = up->dma;
261
262 if (dma && dma->tx_running) {
263 /*
264 * TCSANOW requests the change to occur immediately however if
265 * we have a TX-DMA operation in progress then it has been
266 * observed that it might stall and never complete. Therefore we
267 * delay DMA completes to prevent this hang from happen.
268 */
269 priv->delayed_restore = 1;
270 return;
271 }
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200272
273 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
274 serial_out(up, UART_EFR, UART_EFR_ECB);
275
276 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
277 serial_out(up, UART_MCR, UART_MCR_TCRTLR);
278 serial_out(up, UART_FCR, up->fcr);
279
280 omap8250_update_scr(up, priv);
281
282 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
283
284 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
285 OMAP_UART_TCR_HALT(52));
286 serial_out(up, UART_TI752_TLR,
287 TRIGGER_TLR_MASK(TX_TRIGGER) << UART_TI752_TLR_TX |
288 TRIGGER_TLR_MASK(RX_TRIGGER) << UART_TI752_TLR_RX);
289
290 serial_out(up, UART_LCR, 0);
291
292 /* drop TCR + TLR access, we setup XON/XOFF later */
293 serial_out(up, UART_MCR, up->mcr);
294 serial_out(up, UART_IER, up->ier);
295
296 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
297 serial_dl_write(up, priv->quot);
298
Peter Hurley9719acc2015-01-25 14:44:52 -0500299 serial_out(up, UART_EFR, priv->efr);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200300
301 /* Configure flow control */
302 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
303 serial_out(up, UART_XON1, priv->xon);
304 serial_out(up, UART_XOFF1, priv->xoff);
305
306 serial_out(up, UART_LCR, up->lcr);
Sekhar Nori6f035412015-07-14 13:32:05 +0530307
308 omap8250_update_mdr1(up, priv);
309
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200310 up->port.ops->set_mctrl(&up->port, up->port.mctrl);
311}
312
313/*
314 * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
315 * some differences in how we want to handle flow control.
316 */
317static void omap_8250_set_termios(struct uart_port *port,
318 struct ktermios *termios,
319 struct ktermios *old)
320{
Andy Shevchenko013e3582016-02-18 21:22:59 +0200321 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200322 struct omap8250_priv *priv = up->port.private_data;
323 unsigned char cval = 0;
324 unsigned int baud;
325
326 switch (termios->c_cflag & CSIZE) {
327 case CS5:
328 cval = UART_LCR_WLEN5;
329 break;
330 case CS6:
331 cval = UART_LCR_WLEN6;
332 break;
333 case CS7:
334 cval = UART_LCR_WLEN7;
335 break;
336 default:
337 case CS8:
338 cval = UART_LCR_WLEN8;
339 break;
340 }
341
342 if (termios->c_cflag & CSTOPB)
343 cval |= UART_LCR_STOP;
344 if (termios->c_cflag & PARENB)
345 cval |= UART_LCR_PARITY;
346 if (!(termios->c_cflag & PARODD))
347 cval |= UART_LCR_EPAR;
348 if (termios->c_cflag & CMSPAR)
349 cval |= UART_LCR_SPAR;
350
351 /*
352 * Ask the core to calculate the divisor for us.
353 */
354 baud = uart_get_baud_rate(port, termios, old,
355 port->uartclk / 16 / 0xffff,
356 port->uartclk / 13);
357 omap_8250_get_divisor(port, baud, priv);
358
359 /*
360 * Ok, we're now changing the port state. Do it with
361 * interrupts disabled.
362 */
363 pm_runtime_get_sync(port->dev);
364 spin_lock_irq(&port->lock);
365
366 /*
367 * Update the per-port timeout.
368 */
369 uart_update_timeout(port, termios->c_cflag, baud);
370
371 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
372 if (termios->c_iflag & INPCK)
373 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
374 if (termios->c_iflag & (IGNBRK | PARMRK))
375 up->port.read_status_mask |= UART_LSR_BI;
376
377 /*
378 * Characters to ignore
379 */
380 up->port.ignore_status_mask = 0;
381 if (termios->c_iflag & IGNPAR)
382 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
383 if (termios->c_iflag & IGNBRK) {
384 up->port.ignore_status_mask |= UART_LSR_BI;
385 /*
386 * If we're ignoring parity and break indicators,
387 * ignore overruns too (for real raw support).
388 */
389 if (termios->c_iflag & IGNPAR)
390 up->port.ignore_status_mask |= UART_LSR_OE;
391 }
392
393 /*
394 * ignore all characters if CREAD is not set
395 */
396 if ((termios->c_cflag & CREAD) == 0)
397 up->port.ignore_status_mask |= UART_LSR_DR;
398
399 /*
400 * Modem status interrupts
401 */
402 up->ier &= ~UART_IER_MSI;
403 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
404 up->ier |= UART_IER_MSI;
405
406 up->lcr = cval;
407 /* Up to here it was mostly serial8250_do_set_termios() */
408
409 /*
410 * We enable TRIG_GRANU for RX and TX and additionaly we set
411 * SCR_TX_EMPTY bit. The result is the following:
412 * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
413 * - less than RX_TRIGGER number of bytes will also cause an interrupt
414 * once the UART decides that there no new bytes arriving.
415 * - Once THRE is enabled, the interrupt will be fired once the FIFO is
416 * empty - the trigger level is ignored here.
417 *
418 * Once DMA is enabled:
419 * - UART will assert the TX DMA line once there is room for TX_TRIGGER
420 * bytes in the TX FIFO. On each assert the DMA engine will move
421 * TX_TRIGGER bytes into the FIFO.
422 * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
423 * the FIFO and move RX_TRIGGER bytes.
424 * This is because threshold and trigger values are the same.
425 */
426 up->fcr = UART_FCR_ENABLE_FIFO;
427 up->fcr |= TRIGGER_FCR_MASK(TX_TRIGGER) << OMAP_UART_FCR_TX_TRIG;
428 up->fcr |= TRIGGER_FCR_MASK(RX_TRIGGER) << OMAP_UART_FCR_RX_TRIG;
429
430 priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
431 OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
432
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200433 if (up->dma)
434 priv->scr |= OMAP_UART_SCR_DMAMODE_1 |
435 OMAP_UART_SCR_DMAMODE_CTL;
436
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200437 priv->xon = termios->c_cc[VSTART];
438 priv->xoff = termios->c_cc[VSTOP];
439
440 priv->efr = 0;
Peter Hurley391f93f2015-01-25 14:44:51 -0500441 up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
442
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200443 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
Peter Hurley9719acc2015-01-25 14:44:52 -0500444 /* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
Peter Hurley391f93f2015-01-25 14:44:51 -0500445 up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
Peter Hurley9719acc2015-01-25 14:44:52 -0500446 priv->efr |= UART_EFR_CTS;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200447 } else if (up->port.flags & UPF_SOFT_FLOW) {
448 /*
Peter Hurley5bac4b32015-06-27 09:28:55 -0400449 * OMAP rx s/w flow control is borked; the transmitter remains
450 * stuck off even if rx flow control is subsequently disabled
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200451 */
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200452
453 /*
454 * IXOFF Flag:
455 * Enable XON/XOFF flow control on output.
456 * Transmit XON1, XOFF1
457 */
Peter Hurley391f93f2015-01-25 14:44:51 -0500458 if (termios->c_iflag & IXOFF) {
459 up->port.status |= UPSTAT_AUTOXOFF;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200460 priv->efr |= OMAP_UART_SW_TX;
Peter Hurley391f93f2015-01-25 14:44:51 -0500461 }
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200462 }
463 omap8250_restore_regs(up);
464
465 spin_unlock_irq(&up->port.lock);
466 pm_runtime_mark_last_busy(port->dev);
467 pm_runtime_put_autosuspend(port->dev);
468
469 /* calculate wakeup latency constraint */
470 priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
471 priv->latency = priv->calc_latency;
472
473 schedule_work(&priv->qos_work);
474
475 /* Don't rewrite B0 */
476 if (tty_termios_baud_rate(termios))
477 tty_termios_encode_baud_rate(termios, baud, baud);
478}
479
480/* same as 8250 except that we may have extra flow bits set in EFR */
481static void omap_8250_pm(struct uart_port *port, unsigned int state,
482 unsigned int oldstate)
483{
Peter Hurley3e29af22014-12-31 16:32:49 -0500484 struct uart_8250_port *up = up_to_u8250p(port);
485 u8 efr;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200486
487 pm_runtime_get_sync(port->dev);
488 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Peter Hurley3e29af22014-12-31 16:32:49 -0500489 efr = serial_in(up, UART_EFR);
490 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200491 serial_out(up, UART_LCR, 0);
492
493 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
494 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
Peter Hurley3e29af22014-12-31 16:32:49 -0500495 serial_out(up, UART_EFR, efr);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200496 serial_out(up, UART_LCR, 0);
497
498 pm_runtime_mark_last_busy(port->dev);
499 pm_runtime_put_autosuspend(port->dev);
500}
501
502static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
503 struct omap8250_priv *priv)
504{
505 u32 mvr, scheme;
506 u16 revision, major, minor;
507
508 mvr = uart_read(up, UART_OMAP_MVER);
509
510 /* Check revision register scheme */
511 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
512
513 switch (scheme) {
514 case 0: /* Legacy Scheme: OMAP2/3 */
515 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
516 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
517 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
518 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
519 break;
520 case 1:
521 /* New Scheme: OMAP4+ */
522 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
523 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
524 OMAP_UART_MVR_MAJ_SHIFT;
525 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
526 break;
527 default:
528 dev_warn(up->port.dev,
529 "Unknown revision, defaulting to highest\n");
530 /* highest possible revision */
531 major = 0xff;
532 minor = 0xff;
533 }
534 /* normalize revision for the driver */
535 revision = UART_BUILD_REVISION(major, minor);
536
537 switch (revision) {
538 case OMAP_UART_REV_46:
Sekhar Nori4fcdff92015-07-14 13:32:06 +0530539 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200540 break;
541 case OMAP_UART_REV_52:
Sekhar Nori4fcdff92015-07-14 13:32:06 +0530542 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200543 OMAP_UART_WER_HAS_TX_WAKEUP;
544 break;
545 case OMAP_UART_REV_63:
Sekhar Nori4fcdff92015-07-14 13:32:06 +0530546 priv->habit |= UART_ERRATA_i202_MDR1_ACCESS |
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200547 OMAP_UART_WER_HAS_TX_WAKEUP;
548 break;
549 default:
550 break;
551 }
552}
553
554static void omap8250_uart_qos_work(struct work_struct *work)
555{
556 struct omap8250_priv *priv;
557
558 priv = container_of(work, struct omap8250_priv, qos_work);
559 pm_qos_update_request(&priv->pm_qos_request, priv->latency);
560}
561
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200562#ifdef CONFIG_SERIAL_8250_DMA
563static int omap_8250_dma_handle_irq(struct uart_port *port);
564#endif
565
566static irqreturn_t omap8250_irq(int irq, void *dev_id)
567{
568 struct uart_port *port = dev_id;
569 struct uart_8250_port *up = up_to_u8250p(port);
570 unsigned int iir;
571 int ret;
572
573#ifdef CONFIG_SERIAL_8250_DMA
574 if (up->dma) {
575 ret = omap_8250_dma_handle_irq(port);
576 return IRQ_RETVAL(ret);
577 }
578#endif
579
580 serial8250_rpm_get(up);
581 iir = serial_port_in(port, UART_IIR);
582 ret = serial8250_handle_irq(port, iir);
583 serial8250_rpm_put(up);
584
585 return IRQ_RETVAL(ret);
586}
587
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200588static int omap_8250_startup(struct uart_port *port)
589{
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200590 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200591 struct omap8250_priv *priv = port->private_data;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200592 int ret;
593
594 if (priv->wakeirq) {
Tony Lindgrena3e362f2015-06-09 23:35:00 -0700595 ret = dev_pm_set_dedicated_wake_irq(port->dev, priv->wakeirq);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200596 if (ret)
597 return ret;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200598 }
599
600 pm_runtime_get_sync(port->dev);
601
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200602 up->mcr = 0;
603 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
604
605 serial_out(up, UART_LCR, UART_LCR_WLEN8);
606
607 up->lsr_saved_flags = 0;
608 up->msr_saved_flags = 0;
609
610 if (up->dma) {
611 ret = serial8250_request_dma(up);
612 if (ret) {
613 dev_warn_ratelimited(port->dev,
614 "failed to request DMA\n");
615 up->dma = NULL;
616 }
617 }
618
619 ret = request_irq(port->irq, omap8250_irq, IRQF_SHARED,
620 dev_name(port->dev), port);
621 if (ret < 0)
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200622 goto err;
623
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200624 up->ier = UART_IER_RLSI | UART_IER_RDI;
625 serial_out(up, UART_IER, up->ier);
626
Rafael J. Wysocki71504e52014-12-19 15:27:58 +0100627#ifdef CONFIG_PM
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200628 up->capabilities |= UART_CAP_RPM;
629#endif
630
631 /* Enable module level wake up */
632 priv->wer = OMAP_UART_WER_MOD_WKUP;
633 if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
634 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
635 serial_out(up, UART_OMAP_WER, priv->wer);
636
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200637 if (up->dma)
638 up->dma->rx_dma(up, 0);
639
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200640 pm_runtime_mark_last_busy(port->dev);
641 pm_runtime_put_autosuspend(port->dev);
642 return 0;
643err:
644 pm_runtime_mark_last_busy(port->dev);
645 pm_runtime_put_autosuspend(port->dev);
Tony Lindgrena3e362f2015-06-09 23:35:00 -0700646 dev_pm_clear_wake_irq(port->dev);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200647 return ret;
648}
649
650static void omap_8250_shutdown(struct uart_port *port)
651{
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200652 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200653 struct omap8250_priv *priv = port->private_data;
654
655 flush_work(&priv->qos_work);
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200656 if (up->dma)
657 up->dma->rx_dma(up, UART_IIR_RX_TIMEOUT);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200658
659 pm_runtime_get_sync(port->dev);
660
661 serial_out(up, UART_OMAP_WER, 0);
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200662
663 up->ier = 0;
664 serial_out(up, UART_IER, 0);
665
666 if (up->dma)
667 serial8250_release_dma(up);
668
669 /*
670 * Disable break condition and FIFOs
671 */
672 if (up->lcr & UART_LCR_SBC)
673 serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
674 serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200675
676 pm_runtime_mark_last_busy(port->dev);
677 pm_runtime_put_autosuspend(port->dev);
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +0200678 free_irq(port->irq, port);
Tony Lindgrena3e362f2015-06-09 23:35:00 -0700679 dev_pm_clear_wake_irq(port->dev);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200680}
681
682static void omap_8250_throttle(struct uart_port *port)
683{
Andy Shevchenko013e3582016-02-18 21:22:59 +0200684 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200685 unsigned long flags;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200686
687 pm_runtime_get_sync(port->dev);
688
689 spin_lock_irqsave(&port->lock, flags);
690 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
691 serial_out(up, UART_IER, up->ier);
692 spin_unlock_irqrestore(&port->lock, flags);
693
694 pm_runtime_mark_last_busy(port->dev);
695 pm_runtime_put_autosuspend(port->dev);
696}
697
Matwey V. Kornilov344cee22016-02-01 21:09:22 +0300698static int omap_8250_rs485_config(struct uart_port *port,
699 struct serial_rs485 *rs485)
700{
701 struct uart_8250_port *up = up_to_u8250p(port);
702
703 /* Clamp the delays to [0, 100ms] */
704 rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
705 rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U);
706
707 port->rs485 = *rs485;
708
709 /*
710 * Both serial8250_em485_init and serial8250_em485_destroy
711 * are idempotent
712 */
713 if (rs485->flags & SER_RS485_ENABLED) {
714 int ret = serial8250_em485_init(up);
715
716 if (ret) {
717 rs485->flags &= ~SER_RS485_ENABLED;
718 port->rs485.flags &= ~SER_RS485_ENABLED;
719 }
720 return ret;
721 }
722
723 serial8250_em485_destroy(up);
724
725 return 0;
726}
727
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200728static void omap_8250_unthrottle(struct uart_port *port)
729{
Andy Shevchenko013e3582016-02-18 21:22:59 +0200730 struct uart_8250_port *up = up_to_u8250p(port);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200731 unsigned long flags;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +0200732
733 pm_runtime_get_sync(port->dev);
734
735 spin_lock_irqsave(&port->lock, flags);
736 up->ier |= UART_IER_RLSI | UART_IER_RDI;
737 serial_out(up, UART_IER, up->ier);
738 spin_unlock_irqrestore(&port->lock, flags);
739
740 pm_runtime_mark_last_busy(port->dev);
741 pm_runtime_put_autosuspend(port->dev);
742}
743
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200744#ifdef CONFIG_SERIAL_8250_DMA
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200745static int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir);
746
Peter Hurleyb74fdd22016-04-09 22:14:35 -0700747static void __dma_rx_do_complete(struct uart_8250_port *p)
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200748{
John Ognesseda0cd32015-04-27 13:52:33 +0200749 struct omap8250_priv *priv = p->port.private_data;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200750 struct uart_8250_dma *dma = p->dma;
751 struct tty_port *tty_port = &p->port.state->port;
752 struct dma_tx_state state;
753 int count;
John Ognesseda0cd32015-04-27 13:52:33 +0200754 unsigned long flags;
Sebastian Andrzej Siewior658e2eb2015-08-14 18:01:03 +0200755 int ret;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200756
John Ognesseda0cd32015-04-27 13:52:33 +0200757 spin_lock_irqsave(&priv->rx_dma_lock, flags);
758
759 if (!dma->rx_running)
760 goto unlock;
761
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200762 dma->rx_running = 0;
763 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
764 dmaengine_terminate_all(dma->rxchan);
765
766 count = dma->rx_size - state.residue;
767
Sebastian Andrzej Siewior658e2eb2015-08-14 18:01:03 +0200768 ret = tty_insert_flip_string(tty_port, dma->rx_buf, count);
769
770 p->port.icount.rx += ret;
771 p->port.icount.buf_overrun += count - ret;
John Ognesseda0cd32015-04-27 13:52:33 +0200772unlock:
773 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
774
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200775 tty_flip_buffer_push(tty_port);
776}
777
778static void __dma_rx_complete(void *param)
779{
Peter Hurleyb74fdd22016-04-09 22:14:35 -0700780 __dma_rx_do_complete(param);
781 omap_8250_rx_dma(param, 0);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200782}
783
John Ognesseda0cd32015-04-27 13:52:33 +0200784static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
785{
786 struct omap8250_priv *priv = p->port.private_data;
787 struct uart_8250_dma *dma = p->dma;
788 unsigned long flags;
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200789 int ret;
John Ognesseda0cd32015-04-27 13:52:33 +0200790
791 spin_lock_irqsave(&priv->rx_dma_lock, flags);
792
793 if (!dma->rx_running) {
794 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
795 return;
796 }
797
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200798 ret = dmaengine_pause(dma->rxchan);
799 if (WARN_ON_ONCE(ret))
800 priv->rx_dma_broken = true;
John Ognesseda0cd32015-04-27 13:52:33 +0200801
802 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
803
Peter Hurleyb74fdd22016-04-09 22:14:35 -0700804 __dma_rx_do_complete(p);
John Ognesseda0cd32015-04-27 13:52:33 +0200805}
806
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200807static int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
808{
John Ognesseda0cd32015-04-27 13:52:33 +0200809 struct omap8250_priv *priv = p->port.private_data;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200810 struct uart_8250_dma *dma = p->dma;
John Ognesseda0cd32015-04-27 13:52:33 +0200811 int err = 0;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200812 struct dma_async_tx_descriptor *desc;
John Ognesseda0cd32015-04-27 13:52:33 +0200813 unsigned long flags;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200814
815 switch (iir & 0x3f) {
816 case UART_IIR_RLSI:
817 /* 8250_core handles errors and break interrupts */
John Ognesseda0cd32015-04-27 13:52:33 +0200818 omap_8250_rx_dma_flush(p);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200819 return -EIO;
820 case UART_IIR_RX_TIMEOUT:
821 /*
822 * If RCVR FIFO trigger level was not reached, complete the
823 * transfer and let 8250_core copy the remaining data.
824 */
John Ognesseda0cd32015-04-27 13:52:33 +0200825 omap_8250_rx_dma_flush(p);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200826 return -ETIMEDOUT;
827 case UART_IIR_RDI:
828 /*
829 * The OMAP UART is a special BEAST. If we receive RDI we _have_
830 * a DMA transfer programmed but it didn't work. One reason is
831 * that we were too slow and there were too many bytes in the
832 * FIFO, the UART counted wrong and never kicked the DMA engine
833 * to do anything. That means once we receive RDI on OMAP then
834 * the DMA won't do anything soon so we have to cancel the DMA
835 * transfer and purge the FIFO manually.
836 */
John Ognesseda0cd32015-04-27 13:52:33 +0200837 omap_8250_rx_dma_flush(p);
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200838 return -ETIMEDOUT;
839
840 default:
841 break;
842 }
843
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +0200844 if (priv->rx_dma_broken)
845 return -EINVAL;
846
John Ognesseda0cd32015-04-27 13:52:33 +0200847 spin_lock_irqsave(&priv->rx_dma_lock, flags);
848
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200849 if (dma->rx_running)
John Ognesseda0cd32015-04-27 13:52:33 +0200850 goto out;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200851
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);
John Ognesseda0cd32015-04-27 13:52:33 +0200855 if (!desc) {
856 err = -EBUSY;
857 goto out;
858 }
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200859
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
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200866 dma_async_issue_pending(dma->rxchan);
John Ognesseda0cd32015-04-27 13:52:33 +0200867out:
868 spin_unlock_irqrestore(&priv->rx_dma_lock, flags);
869 return err;
Sebastian Andrzej Siewior0e31c8d2014-09-29 20:06:44 +0200870}
871
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200872static int omap_8250_tx_dma(struct uart_8250_port *p);
873
874static 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;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200881 struct omap8250_priv *priv = p->port.private_data;
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200882
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
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +0200894 if (priv->delayed_restore) {
895 priv->delayed_restore = 0;
896 omap8250_restore_regs(p);
897 }
898
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +0200899 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
922static 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;
1012err:
1013 dma->tx_err = 1;
1014 return ret;
1015}
1016
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001017/*
1018 * This is mostly serial8250_handle_irq(). We have a slightly different DMA
1019 * hoook for RX/TX and need different logic for them in the ISR. Therefore we
1020 * use the default routine in the non-DMA case and this one for with DMA.
1021 */
1022static int omap_8250_dma_handle_irq(struct uart_port *port)
1023{
1024 struct uart_8250_port *up = up_to_u8250p(port);
1025 unsigned char status;
1026 unsigned long flags;
1027 u8 iir;
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001028
1029 serial8250_rpm_get(up);
1030
1031 iir = serial_port_in(port, UART_IIR);
1032 if (iir & UART_IIR_NO_INT) {
1033 serial8250_rpm_put(up);
1034 return 0;
1035 }
1036
1037 spin_lock_irqsave(&port->lock, flags);
1038
1039 status = serial_port_in(port, UART_LSR);
1040
1041 if (status & (UART_LSR_DR | UART_LSR_BI)) {
Peter Hurleya86f50e2016-04-09 20:49:41 -07001042 if (omap_8250_rx_dma(up, iir)) {
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001043 status = serial8250_rx_chars(up, status);
1044 omap_8250_rx_dma(up, 0);
1045 }
1046 }
1047 serial8250_modem_status(up);
1048 if (status & UART_LSR_THRE && up->dma->tx_err) {
1049 if (uart_tx_stopped(&up->port) ||
1050 uart_circ_empty(&up->port.state->xmit)) {
1051 up->dma->tx_err = 0;
1052 serial8250_tx_chars(up);
1053 } else {
1054 /*
1055 * try again due to an earlier failer which
1056 * might have been resolved by now.
1057 */
Peter Hurleya86f50e2016-04-09 20:49:41 -07001058 if (omap_8250_tx_dma(up))
Sebastian Andrzej Siewior77285242014-09-29 20:06:48 +02001059 serial8250_tx_chars(up);
1060 }
1061 }
1062
1063 spin_unlock_irqrestore(&port->lock, flags);
1064 serial8250_rpm_put(up);
1065 return 1;
1066}
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001067
1068static bool the_no_dma_filter_fn(struct dma_chan *chan, void *param)
1069{
1070 return false;
1071}
1072
1073#else
1074
1075static inline int omap_8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
1076{
1077 return -EINVAL;
1078}
Sebastian Andrzej Siewior31a17132014-09-29 20:06:43 +02001079#endif
1080
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +02001081static int omap8250_no_handle_irq(struct uart_port *port)
1082{
1083 /* IRQ has not been requested but handling irq? */
1084 WARN_ONCE(1, "Unexpected irq handling before port startup\n");
1085 return 0;
1086}
1087
Sekhar Noricdb929e2015-07-14 13:32:07 +05301088static const u8 am3352_habit = OMAP_DMA_TX_KICK | UART_ERRATA_CLOCK_DISABLE;
1089static const u8 am4372_habit = UART_ERRATA_CLOCK_DISABLE;
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301090
1091static const struct of_device_id omap8250_dt_ids[] = {
1092 { .compatible = "ti,omap2-uart" },
1093 { .compatible = "ti,omap3-uart" },
1094 { .compatible = "ti,omap4-uart" },
1095 { .compatible = "ti,am3352-uart", .data = &am3352_habit, },
Sekhar Noricdb929e2015-07-14 13:32:07 +05301096 { .compatible = "ti,am4372-uart", .data = &am4372_habit, },
Sekhar Nori27c93af2015-07-14 13:32:08 +05301097 { .compatible = "ti,dra742-uart", .data = &am4372_habit, },
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301098 {},
1099};
1100MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
1101
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001102static int omap8250_probe(struct platform_device *pdev)
1103{
1104 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1105 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1106 struct omap8250_priv *priv;
1107 struct uart_8250_port up;
1108 int ret;
1109 void __iomem *membase;
1110
1111 if (!regs || !irq) {
1112 dev_err(&pdev->dev, "missing registers or irq\n");
1113 return -EINVAL;
1114 }
1115
1116 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1117 if (!priv)
1118 return -ENOMEM;
1119
1120 membase = devm_ioremap_nocache(&pdev->dev, regs->start,
1121 resource_size(regs));
1122 if (!membase)
1123 return -ENODEV;
1124
1125 memset(&up, 0, sizeof(up));
1126 up.port.dev = &pdev->dev;
1127 up.port.mapbase = regs->start;
1128 up.port.membase = membase;
1129 up.port.irq = irq->start;
1130 /*
1131 * It claims to be 16C750 compatible however it is a little different.
1132 * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
1133 * have) is enabled via EFR instead of MCR. The type is set here 8250
1134 * just to get things going. UNKNOWN does not work for a few reasons and
1135 * we don't need our own type since we don't use 8250's set_termios()
1136 * or pm callback.
1137 */
1138 up.port.type = PORT_8250;
1139 up.port.iotype = UPIO_MEM;
1140 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
1141 UPF_HARD_FLOW;
1142 up.port.private_data = priv;
1143
1144 up.port.regshift = 2;
1145 up.port.fifosize = 64;
1146 up.tx_loadsz = 64;
1147 up.capabilities = UART_CAP_FIFO;
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001148#ifdef CONFIG_PM
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001149 /*
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001150 * Runtime PM is mostly transparent. However to do it right we need to a
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001151 * TX empty interrupt before we can put the device to auto idle. So if
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001152 * PM is not enabled we don't add that flag and can spare that one extra
1153 * interrupt in the TX path.
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001154 */
1155 up.capabilities |= UART_CAP_RPM;
1156#endif
1157 up.port.set_termios = omap_8250_set_termios;
Peter Hurley4bf4ea92014-12-30 20:28:15 -05001158 up.port.set_mctrl = omap8250_set_mctrl;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001159 up.port.pm = omap_8250_pm;
1160 up.port.startup = omap_8250_startup;
1161 up.port.shutdown = omap_8250_shutdown;
1162 up.port.throttle = omap_8250_throttle;
1163 up.port.unthrottle = omap_8250_unthrottle;
Matwey V. Kornilov344cee22016-02-01 21:09:22 +03001164 up.port.rs485_config = omap_8250_rs485_config;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001165
1166 if (pdev->dev.of_node) {
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301167 const struct of_device_id *id;
1168
Sebastian Andrzej Siewior54178fe2014-11-12 10:28:33 +01001169 ret = of_alias_get_id(pdev->dev.of_node, "serial");
1170
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001171 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1172 &up.port.uartclk);
1173 priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
Sekhar Nori4fcdff92015-07-14 13:32:06 +05301174
1175 id = of_match_device(of_match_ptr(omap8250_dt_ids), &pdev->dev);
1176 if (id && id->data)
1177 priv->habit |= *(u8 *)id->data;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001178 } else {
Sebastian Andrzej Siewior54178fe2014-11-12 10:28:33 +01001179 ret = pdev->id;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001180 }
Sebastian Andrzej Siewior54178fe2014-11-12 10:28:33 +01001181 if (ret < 0) {
1182 dev_err(&pdev->dev, "failed to get alias/pdev id\n");
1183 return ret;
1184 }
1185 up.port.line = ret;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001186
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001187 if (!up.port.uartclk) {
1188 up.port.uartclk = DEFAULT_CLK_SPEED;
1189 dev_warn(&pdev->dev,
1190 "No clock speed specified: using default: %d\n",
1191 DEFAULT_CLK_SPEED);
1192 }
1193
1194 priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1195 priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1196 pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
1197 priv->latency);
1198 INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
1199
John Ognesseda0cd32015-04-27 13:52:33 +02001200 spin_lock_init(&priv->rx_dma_lock);
1201
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001202 device_init_wakeup(&pdev->dev, true);
1203 pm_runtime_use_autosuspend(&pdev->dev);
1204 pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
1205
1206 pm_runtime_irq_safe(&pdev->dev);
1207 pm_runtime_enable(&pdev->dev);
1208
1209 pm_runtime_get_sync(&pdev->dev);
1210
1211 omap_serial_fill_features_erratas(&up, priv);
Sebastian Andrzej Siewior9e915972015-05-20 22:07:35 +02001212 up.port.handle_irq = omap8250_no_handle_irq;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001213#ifdef CONFIG_SERIAL_8250_DMA
1214 if (pdev->dev.of_node) {
1215 /*
1216 * Oh DMA support. If there are no DMA properties in the DT then
1217 * we will fall back to a generic DMA channel which does not
1218 * really work here. To ensure that we do not get a generic DMA
1219 * channel assigned, we have the the_no_dma_filter_fn() here.
1220 * To avoid "failed to request DMA" messages we check for DMA
1221 * properties in DT.
1222 */
1223 ret = of_property_count_strings(pdev->dev.of_node, "dma-names");
1224 if (ret == 2) {
1225 up.dma = &priv->omap8250_dma;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001226 priv->omap8250_dma.fn = the_no_dma_filter_fn;
1227 priv->omap8250_dma.tx_dma = omap_8250_tx_dma;
1228 priv->omap8250_dma.rx_dma = omap_8250_rx_dma;
1229 priv->omap8250_dma.rx_size = RX_TRIGGER;
1230 priv->omap8250_dma.rxconf.src_maxburst = RX_TRIGGER;
1231 priv->omap8250_dma.txconf.dst_maxburst = TX_TRIGGER;
1232
1233 if (of_machine_is_compatible("ti,am33xx"))
1234 priv->habit |= OMAP_DMA_TX_KICK;
Sebastian Andrzej Siewior830acf92015-08-14 17:52:07 +02001235 /*
1236 * pause is currently not supported atleast on omap-sdma
1237 * and edma on most earlier kernels.
1238 */
1239 priv->rx_dma_broken = true;
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001240 }
1241 }
1242#endif
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001243 ret = serial8250_register_8250_port(&up);
1244 if (ret < 0) {
1245 dev_err(&pdev->dev, "unable to register 8250 port\n");
1246 goto err;
1247 }
1248 priv->line = ret;
1249 platform_set_drvdata(pdev, priv);
1250 pm_runtime_mark_last_busy(&pdev->dev);
1251 pm_runtime_put_autosuspend(&pdev->dev);
1252 return 0;
1253err:
1254 pm_runtime_put(&pdev->dev);
1255 pm_runtime_disable(&pdev->dev);
1256 return ret;
1257}
1258
1259static int omap8250_remove(struct platform_device *pdev)
1260{
1261 struct omap8250_priv *priv = platform_get_drvdata(pdev);
1262
1263 pm_runtime_put_sync(&pdev->dev);
1264 pm_runtime_disable(&pdev->dev);
1265 serial8250_unregister_port(priv->line);
1266 pm_qos_remove_request(&priv->pm_qos_request);
1267 device_init_wakeup(&pdev->dev, false);
1268 return 0;
1269}
1270
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001271#ifdef CONFIG_PM_SLEEP
1272static int omap8250_prepare(struct device *dev)
1273{
1274 struct omap8250_priv *priv = dev_get_drvdata(dev);
1275
1276 if (!priv)
1277 return 0;
1278 priv->is_suspending = true;
1279 return 0;
1280}
1281
1282static void omap8250_complete(struct device *dev)
1283{
1284 struct omap8250_priv *priv = dev_get_drvdata(dev);
1285
1286 if (!priv)
1287 return;
1288 priv->is_suspending = false;
1289}
1290
1291static int omap8250_suspend(struct device *dev)
1292{
1293 struct omap8250_priv *priv = dev_get_drvdata(dev);
1294
1295 serial8250_suspend_port(priv->line);
1296 flush_work(&priv->qos_work);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001297 return 0;
1298}
1299
1300static int omap8250_resume(struct device *dev)
1301{
1302 struct omap8250_priv *priv = dev_get_drvdata(dev);
1303
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001304 serial8250_resume_port(priv->line);
1305 return 0;
1306}
1307#else
1308#define omap8250_prepare NULL
1309#define omap8250_complete NULL
1310#endif
1311
Rafael J. Wysocki71504e52014-12-19 15:27:58 +01001312#ifdef CONFIG_PM
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001313static int omap8250_lost_context(struct uart_8250_port *up)
1314{
1315 u32 val;
1316
Sekhar Noricdb929e2015-07-14 13:32:07 +05301317 val = serial_in(up, UART_OMAP_SCR);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001318 /*
Sekhar Noricdb929e2015-07-14 13:32:07 +05301319 * If we lose context, then SCR is set to its reset value of zero.
1320 * After set_termios() we set bit 3 of SCR (TX_EMPTY_CTL_IT) to 1,
1321 * among other bits, to never set the register back to zero again.
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001322 */
Sekhar Noricdb929e2015-07-14 13:32:07 +05301323 if (!val)
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001324 return 1;
1325 return 0;
1326}
1327
Sekhar Noricdb929e2015-07-14 13:32:07 +05301328/* TODO: in future, this should happen via API in drivers/reset/ */
1329static int omap8250_soft_reset(struct device *dev)
1330{
1331 struct omap8250_priv *priv = dev_get_drvdata(dev);
1332 struct uart_8250_port *up = serial8250_get_port(priv->line);
1333 int timeout = 100;
1334 int sysc;
1335 int syss;
1336
1337 sysc = serial_in(up, UART_OMAP_SYSC);
1338
1339 /* softreset the UART */
1340 sysc |= OMAP_UART_SYSC_SOFTRESET;
1341 serial_out(up, UART_OMAP_SYSC, sysc);
1342
1343 /* By experiments, 1us enough for reset complete on AM335x */
1344 do {
1345 udelay(1);
1346 syss = serial_in(up, UART_OMAP_SYSS);
1347 } while (--timeout && !(syss & OMAP_UART_SYSS_RESETDONE));
1348
1349 if (!timeout) {
1350 dev_err(dev, "timed out waiting for reset done\n");
1351 return -ETIMEDOUT;
1352 }
1353
1354 return 0;
1355}
1356
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001357static int omap8250_runtime_suspend(struct device *dev)
1358{
1359 struct omap8250_priv *priv = dev_get_drvdata(dev);
1360 struct uart_8250_port *up;
1361
1362 up = serial8250_get_port(priv->line);
1363 /*
1364 * When using 'no_console_suspend', the console UART must not be
1365 * suspended. Since driver suspend is managed by runtime suspend,
1366 * preventing runtime suspend (by returning error) will keep device
1367 * active during suspend.
1368 */
1369 if (priv->is_suspending && !console_suspend_enabled) {
1370 if (uart_console(&up->port))
1371 return -EBUSY;
1372 }
1373
Sekhar Noricdb929e2015-07-14 13:32:07 +05301374 if (priv->habit & UART_ERRATA_CLOCK_DISABLE) {
1375 int ret;
1376
1377 ret = omap8250_soft_reset(dev);
1378 if (ret)
1379 return ret;
1380
1381 /* Restore to UART mode after reset (for wakeup) */
1382 omap8250_update_mdr1(up, priv);
1383 }
1384
Sekhar Nori727fd8a2015-07-14 13:32:03 +05301385 if (up->dma && up->dma->rxchan)
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001386 omap_8250_rx_dma(up, UART_IIR_RX_TIMEOUT);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001387
1388 priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
1389 schedule_work(&priv->qos_work);
1390
1391 return 0;
1392}
1393
1394static int omap8250_runtime_resume(struct device *dev)
1395{
1396 struct omap8250_priv *priv = dev_get_drvdata(dev);
1397 struct uart_8250_port *up;
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001398
1399 /* In case runtime-pm tries this before we are setup */
1400 if (!priv)
1401 return 0;
1402
1403 up = serial8250_get_port(priv->line);
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001404
Peter Hurleyf56f0a52016-04-09 20:49:43 -07001405 if (omap8250_lost_context(up))
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001406 omap8250_restore_regs(up);
1407
Sekhar Nori727fd8a2015-07-14 13:32:03 +05301408 if (up->dma && up->dma->rxchan)
Sebastian Andrzej Siewior0a0661d2014-09-29 20:06:49 +02001409 omap_8250_rx_dma(up, 0);
1410
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001411 priv->latency = priv->calc_latency;
1412 schedule_work(&priv->qos_work);
1413 return 0;
1414}
1415#endif
1416
Sebastian Andrzej Siewior00648d02014-12-18 18:47:12 +01001417#ifdef CONFIG_SERIAL_8250_OMAP_TTYO_FIXUP
1418static int __init omap8250_console_fixup(void)
1419{
1420 char *omap_str;
1421 char *options;
1422 u8 idx;
1423
1424 if (strstr(boot_command_line, "console=ttyS"))
1425 /* user set a ttyS based name for the console */
1426 return 0;
1427
1428 omap_str = strstr(boot_command_line, "console=ttyO");
1429 if (!omap_str)
1430 /* user did not set ttyO based console, so we don't care */
1431 return 0;
1432
1433 omap_str += 12;
1434 if ('0' <= *omap_str && *omap_str <= '9')
1435 idx = *omap_str - '0';
1436 else
1437 return 0;
1438
1439 omap_str++;
1440 if (omap_str[0] == ',') {
1441 omap_str++;
1442 options = omap_str;
1443 } else {
1444 options = NULL;
1445 }
1446
1447 add_preferred_console("ttyS", idx, options);
1448 pr_err("WARNING: Your 'console=ttyO%d' has been replaced by 'ttyS%d'\n",
1449 idx, idx);
1450 pr_err("This ensures that you still see kernel messages. Please\n");
1451 pr_err("update your kernel commandline.\n");
1452 return 0;
1453}
1454console_initcall(omap8250_console_fixup);
1455#endif
1456
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001457static const struct dev_pm_ops omap8250_dev_pm_ops = {
1458 SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
1459 SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
1460 omap8250_runtime_resume, NULL)
1461 .prepare = omap8250_prepare,
1462 .complete = omap8250_complete,
1463};
1464
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001465static struct platform_driver omap8250_platform_driver = {
1466 .driver = {
1467 .name = "omap8250",
1468 .pm = &omap8250_dev_pm_ops,
1469 .of_match_table = omap8250_dt_ids,
Sebastian Andrzej Siewior61929cf2014-09-29 20:06:39 +02001470 },
1471 .probe = omap8250_probe,
1472 .remove = omap8250_remove,
1473};
1474module_platform_driver(omap8250_platform_driver);
1475
1476MODULE_AUTHOR("Sebastian Andrzej Siewior");
1477MODULE_DESCRIPTION("OMAP 8250 Driver");
1478MODULE_LICENSE("GPL v2");