blob: 2f653c48639de8a05d19447a3e9f8cdb7dd6ae12 [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>
14#include <linux/serial_core.h>
15#include <linux/serial_reg.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18#include <linux/of.h>
19#include <linux/of_gpio.h>
20#include <linux/of_irq.h>
21#include <linux/delay.h>
22#include <linux/pm_runtime.h>
23#include <linux/console.h>
24#include <linux/pm_qos.h>
25
26#include "8250.h"
27
28#define DEFAULT_CLK_SPEED 48000000
29
30#define UART_ERRATA_i202_MDR1_ACCESS (1 << 0)
31#define OMAP_UART_WER_HAS_TX_WAKEUP (1 << 1)
32
33#define OMAP_UART_FCR_RX_TRIG 6
34#define OMAP_UART_FCR_TX_TRIG 4
35
36/* SCR register bitmasks */
37#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
38#define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK (1 << 6)
39#define OMAP_UART_SCR_TX_EMPTY (1 << 3)
40#define OMAP_UART_SCR_DMAMODE_MASK (3 << 1)
41#define OMAP_UART_SCR_DMAMODE_1 (1 << 1)
42#define OMAP_UART_SCR_DMAMODE_CTL (1 << 0)
43
44/* MVR register bitmasks */
45#define OMAP_UART_MVR_SCHEME_SHIFT 30
46#define OMAP_UART_LEGACY_MVR_MAJ_MASK 0xf0
47#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT 4
48#define OMAP_UART_LEGACY_MVR_MIN_MASK 0x0f
49#define OMAP_UART_MVR_MAJ_MASK 0x700
50#define OMAP_UART_MVR_MAJ_SHIFT 8
51#define OMAP_UART_MVR_MIN_MASK 0x3f
52
53#define UART_TI752_TLR_TX 0
54#define UART_TI752_TLR_RX 4
55
56#define TRIGGER_TLR_MASK(x) ((x & 0x3c) >> 2)
57#define TRIGGER_FCR_MASK(x) (x & 3)
58
59/* Enable XON/XOFF flow control on output */
60#define OMAP_UART_SW_TX 0x08
61/* Enable XON/XOFF flow control on input */
62#define OMAP_UART_SW_RX 0x02
63
64#define OMAP_UART_WER_MOD_WKUP 0x7f
65#define OMAP_UART_TX_WAKEUP_EN (1 << 7)
66
67#define TX_TRIGGER 1
68#define RX_TRIGGER 48
69
70#define OMAP_UART_TCR_RESTORE(x) ((x / 4) << 4)
71#define OMAP_UART_TCR_HALT(x) ((x / 4) << 0)
72
73#define UART_BUILD_REVISION(x, y) (((x) << 8) | (y))
74
75#define OMAP_UART_REV_46 0x0406
76#define OMAP_UART_REV_52 0x0502
77#define OMAP_UART_REV_63 0x0603
78
79struct omap8250_priv {
80 int line;
81 u8 habit;
82 u8 mdr1;
83 u8 efr;
84 u8 scr;
85 u8 wer;
86 u8 xon;
87 u8 xoff;
88 u16 quot;
89
90 bool is_suspending;
91 int wakeirq;
92 int wakeups_enabled;
93 u32 latency;
94 u32 calc_latency;
95 struct pm_qos_request pm_qos_request;
96 struct work_struct qos_work;
97 struct uart_8250_dma omap8250_dma;
98};
99
100static u32 uart_read(struct uart_8250_port *up, u32 reg)
101{
102 return readl(up->port.membase + (reg << up->port.regshift));
103}
104
105/*
106 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
107 * The access to uart register after MDR1 Access
108 * causes UART to corrupt data.
109 *
110 * Need a delay =
111 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
112 * give 10 times as much
113 */
114static void omap_8250_mdr1_errataset(struct uart_8250_port *up,
115 struct omap8250_priv *priv)
116{
117 u8 timeout = 255;
118 u8 old_mdr1;
119
120 old_mdr1 = serial_in(up, UART_OMAP_MDR1);
121 if (old_mdr1 == priv->mdr1)
122 return;
123
124 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
125 udelay(2);
126 serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
127 UART_FCR_CLEAR_RCVR);
128 /*
129 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
130 * TX_FIFO_E bit is 1.
131 */
132 while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
133 (UART_LSR_THRE | UART_LSR_DR))) {
134 timeout--;
135 if (!timeout) {
136 /* Should *never* happen. we warn and carry on */
137 dev_crit(up->port.dev, "Errata i202: timedout %x\n",
138 serial_in(up, UART_LSR));
139 break;
140 }
141 udelay(1);
142 }
143}
144
145static void omap_8250_get_divisor(struct uart_port *port, unsigned int baud,
146 struct omap8250_priv *priv)
147{
148 unsigned int uartclk = port->uartclk;
149 unsigned int div_13, div_16;
150 unsigned int abs_d13, abs_d16;
151
152 /*
153 * Old custom speed handling.
154 */
155 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST) {
156 priv->quot = port->custom_divisor & 0xffff;
157 /*
158 * I assume that nobody is using this. But hey, if somebody
159 * would like to specify the divisor _and_ the mode then the
160 * driver is ready and waiting for it.
161 */
162 if (port->custom_divisor & (1 << 16))
163 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
164 else
165 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
166 return;
167 }
168 div_13 = DIV_ROUND_CLOSEST(uartclk, 13 * baud);
169 div_16 = DIV_ROUND_CLOSEST(uartclk, 16 * baud);
170
171 if (!div_13)
172 div_13 = 1;
173 if (!div_16)
174 div_16 = 1;
175
176 abs_d13 = abs(baud - uartclk / 13 / div_13);
177 abs_d16 = abs(baud - uartclk / 16 / div_16);
178
179 if (abs_d13 >= abs_d16) {
180 priv->mdr1 = UART_OMAP_MDR1_16X_MODE;
181 priv->quot = div_16;
182 } else {
183 priv->mdr1 = UART_OMAP_MDR1_13X_MODE;
184 priv->quot = div_13;
185 }
186}
187
188static void omap8250_update_scr(struct uart_8250_port *up,
189 struct omap8250_priv *priv)
190{
191 u8 old_scr;
192
193 old_scr = serial_in(up, UART_OMAP_SCR);
194 if (old_scr == priv->scr)
195 return;
196
197 /*
198 * The manual recommends not to enable the DMA mode selector in the SCR
199 * (instead of the FCR) register _and_ selecting the DMA mode as one
200 * register write because this may lead to malfunction.
201 */
202 if (priv->scr & OMAP_UART_SCR_DMAMODE_MASK)
203 serial_out(up, UART_OMAP_SCR,
204 priv->scr & ~OMAP_UART_SCR_DMAMODE_MASK);
205 serial_out(up, UART_OMAP_SCR, priv->scr);
206}
207
208static void omap8250_restore_regs(struct uart_8250_port *up)
209{
210 struct omap8250_priv *priv = up->port.private_data;
211
212 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
213 serial_out(up, UART_EFR, UART_EFR_ECB);
214
215 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
216 serial_out(up, UART_MCR, UART_MCR_TCRTLR);
217 serial_out(up, UART_FCR, up->fcr);
218
219 omap8250_update_scr(up, priv);
220
221 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
222
223 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_RESTORE(16) |
224 OMAP_UART_TCR_HALT(52));
225 serial_out(up, UART_TI752_TLR,
226 TRIGGER_TLR_MASK(TX_TRIGGER) << UART_TI752_TLR_TX |
227 TRIGGER_TLR_MASK(RX_TRIGGER) << UART_TI752_TLR_RX);
228
229 serial_out(up, UART_LCR, 0);
230
231 /* drop TCR + TLR access, we setup XON/XOFF later */
232 serial_out(up, UART_MCR, up->mcr);
233 serial_out(up, UART_IER, up->ier);
234
235 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
236 serial_dl_write(up, priv->quot);
237
238 serial_out(up, UART_EFR, priv->efr);
239
240 /* Configure flow control */
241 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
242 serial_out(up, UART_XON1, priv->xon);
243 serial_out(up, UART_XOFF1, priv->xoff);
244
245 serial_out(up, UART_LCR, up->lcr);
246 /* need mode A for FCR */
247 if (priv->habit & UART_ERRATA_i202_MDR1_ACCESS)
248 omap_8250_mdr1_errataset(up, priv);
249 else
250 serial_out(up, UART_OMAP_MDR1, priv->mdr1);
251 up->port.ops->set_mctrl(&up->port, up->port.mctrl);
252}
253
254/*
255 * OMAP can use "CLK / (16 or 13) / div" for baud rate. And then we have have
256 * some differences in how we want to handle flow control.
257 */
258static void omap_8250_set_termios(struct uart_port *port,
259 struct ktermios *termios,
260 struct ktermios *old)
261{
262 struct uart_8250_port *up =
263 container_of(port, struct uart_8250_port, port);
264 struct omap8250_priv *priv = up->port.private_data;
265 unsigned char cval = 0;
266 unsigned int baud;
267
268 switch (termios->c_cflag & CSIZE) {
269 case CS5:
270 cval = UART_LCR_WLEN5;
271 break;
272 case CS6:
273 cval = UART_LCR_WLEN6;
274 break;
275 case CS7:
276 cval = UART_LCR_WLEN7;
277 break;
278 default:
279 case CS8:
280 cval = UART_LCR_WLEN8;
281 break;
282 }
283
284 if (termios->c_cflag & CSTOPB)
285 cval |= UART_LCR_STOP;
286 if (termios->c_cflag & PARENB)
287 cval |= UART_LCR_PARITY;
288 if (!(termios->c_cflag & PARODD))
289 cval |= UART_LCR_EPAR;
290 if (termios->c_cflag & CMSPAR)
291 cval |= UART_LCR_SPAR;
292
293 /*
294 * Ask the core to calculate the divisor for us.
295 */
296 baud = uart_get_baud_rate(port, termios, old,
297 port->uartclk / 16 / 0xffff,
298 port->uartclk / 13);
299 omap_8250_get_divisor(port, baud, priv);
300
301 /*
302 * Ok, we're now changing the port state. Do it with
303 * interrupts disabled.
304 */
305 pm_runtime_get_sync(port->dev);
306 spin_lock_irq(&port->lock);
307
308 /*
309 * Update the per-port timeout.
310 */
311 uart_update_timeout(port, termios->c_cflag, baud);
312
313 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
314 if (termios->c_iflag & INPCK)
315 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
316 if (termios->c_iflag & (IGNBRK | PARMRK))
317 up->port.read_status_mask |= UART_LSR_BI;
318
319 /*
320 * Characters to ignore
321 */
322 up->port.ignore_status_mask = 0;
323 if (termios->c_iflag & IGNPAR)
324 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
325 if (termios->c_iflag & IGNBRK) {
326 up->port.ignore_status_mask |= UART_LSR_BI;
327 /*
328 * If we're ignoring parity and break indicators,
329 * ignore overruns too (for real raw support).
330 */
331 if (termios->c_iflag & IGNPAR)
332 up->port.ignore_status_mask |= UART_LSR_OE;
333 }
334
335 /*
336 * ignore all characters if CREAD is not set
337 */
338 if ((termios->c_cflag & CREAD) == 0)
339 up->port.ignore_status_mask |= UART_LSR_DR;
340
341 /*
342 * Modem status interrupts
343 */
344 up->ier &= ~UART_IER_MSI;
345 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
346 up->ier |= UART_IER_MSI;
347
348 up->lcr = cval;
349 /* Up to here it was mostly serial8250_do_set_termios() */
350
351 /*
352 * We enable TRIG_GRANU for RX and TX and additionaly we set
353 * SCR_TX_EMPTY bit. The result is the following:
354 * - RX_TRIGGER amount of bytes in the FIFO will cause an interrupt.
355 * - less than RX_TRIGGER number of bytes will also cause an interrupt
356 * once the UART decides that there no new bytes arriving.
357 * - Once THRE is enabled, the interrupt will be fired once the FIFO is
358 * empty - the trigger level is ignored here.
359 *
360 * Once DMA is enabled:
361 * - UART will assert the TX DMA line once there is room for TX_TRIGGER
362 * bytes in the TX FIFO. On each assert the DMA engine will move
363 * TX_TRIGGER bytes into the FIFO.
364 * - UART will assert the RX DMA line once there are RX_TRIGGER bytes in
365 * the FIFO and move RX_TRIGGER bytes.
366 * This is because threshold and trigger values are the same.
367 */
368 up->fcr = UART_FCR_ENABLE_FIFO;
369 up->fcr |= TRIGGER_FCR_MASK(TX_TRIGGER) << OMAP_UART_FCR_TX_TRIG;
370 up->fcr |= TRIGGER_FCR_MASK(RX_TRIGGER) << OMAP_UART_FCR_RX_TRIG;
371
372 priv->scr = OMAP_UART_SCR_RX_TRIG_GRANU1_MASK | OMAP_UART_SCR_TX_EMPTY |
373 OMAP_UART_SCR_TX_TRIG_GRANU1_MASK;
374
375 priv->xon = termios->c_cc[VSTART];
376 priv->xoff = termios->c_cc[VSTOP];
377
378 priv->efr = 0;
379 up->mcr &= ~(UART_MCR_RTS | UART_MCR_XONANY);
380 if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
381 /* Enable AUTORTS and AUTOCTS */
382 priv->efr |= UART_EFR_CTS | UART_EFR_RTS;
383
384 /* Ensure MCR RTS is asserted */
385 up->mcr |= UART_MCR_RTS;
386 } else if (up->port.flags & UPF_SOFT_FLOW) {
387 /*
388 * IXON Flag:
389 * Enable XON/XOFF flow control on input.
390 * Receiver compares XON1, XOFF1.
391 */
392 if (termios->c_iflag & IXON)
393 priv->efr |= OMAP_UART_SW_RX;
394
395 /*
396 * IXOFF Flag:
397 * Enable XON/XOFF flow control on output.
398 * Transmit XON1, XOFF1
399 */
400 if (termios->c_iflag & IXOFF)
401 priv->efr |= OMAP_UART_SW_TX;
402
403 /*
404 * IXANY Flag:
405 * Enable any character to restart output.
406 * Operation resumes after receiving any
407 * character after recognition of the XOFF character
408 */
409 if (termios->c_iflag & IXANY)
410 up->mcr |= UART_MCR_XONANY;
411 }
412 omap8250_restore_regs(up);
413
414 spin_unlock_irq(&up->port.lock);
415 pm_runtime_mark_last_busy(port->dev);
416 pm_runtime_put_autosuspend(port->dev);
417
418 /* calculate wakeup latency constraint */
419 priv->calc_latency = USEC_PER_SEC * 64 * 8 / baud;
420 priv->latency = priv->calc_latency;
421
422 schedule_work(&priv->qos_work);
423
424 /* Don't rewrite B0 */
425 if (tty_termios_baud_rate(termios))
426 tty_termios_encode_baud_rate(termios, baud, baud);
427}
428
429/* same as 8250 except that we may have extra flow bits set in EFR */
430static void omap_8250_pm(struct uart_port *port, unsigned int state,
431 unsigned int oldstate)
432{
433 struct uart_8250_port *up =
434 container_of(port, struct uart_8250_port, port);
435 struct omap8250_priv *priv = up->port.private_data;
436
437 pm_runtime_get_sync(port->dev);
438 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
439 serial_out(up, UART_EFR, priv->efr | UART_EFR_ECB);
440 serial_out(up, UART_LCR, 0);
441
442 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
443 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
444 serial_out(up, UART_EFR, priv->efr);
445 serial_out(up, UART_LCR, 0);
446
447 pm_runtime_mark_last_busy(port->dev);
448 pm_runtime_put_autosuspend(port->dev);
449}
450
451static void omap_serial_fill_features_erratas(struct uart_8250_port *up,
452 struct omap8250_priv *priv)
453{
454 u32 mvr, scheme;
455 u16 revision, major, minor;
456
457 mvr = uart_read(up, UART_OMAP_MVER);
458
459 /* Check revision register scheme */
460 scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;
461
462 switch (scheme) {
463 case 0: /* Legacy Scheme: OMAP2/3 */
464 /* MINOR_REV[0:4], MAJOR_REV[4:7] */
465 major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
466 OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
467 minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
468 break;
469 case 1:
470 /* New Scheme: OMAP4+ */
471 /* MINOR_REV[0:5], MAJOR_REV[8:10] */
472 major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
473 OMAP_UART_MVR_MAJ_SHIFT;
474 minor = (mvr & OMAP_UART_MVR_MIN_MASK);
475 break;
476 default:
477 dev_warn(up->port.dev,
478 "Unknown revision, defaulting to highest\n");
479 /* highest possible revision */
480 major = 0xff;
481 minor = 0xff;
482 }
483 /* normalize revision for the driver */
484 revision = UART_BUILD_REVISION(major, minor);
485
486 switch (revision) {
487 case OMAP_UART_REV_46:
488 priv->habit = UART_ERRATA_i202_MDR1_ACCESS;
489 break;
490 case OMAP_UART_REV_52:
491 priv->habit = UART_ERRATA_i202_MDR1_ACCESS |
492 OMAP_UART_WER_HAS_TX_WAKEUP;
493 break;
494 case OMAP_UART_REV_63:
495 priv->habit = UART_ERRATA_i202_MDR1_ACCESS |
496 OMAP_UART_WER_HAS_TX_WAKEUP;
497 break;
498 default:
499 break;
500 }
501}
502
503static void omap8250_uart_qos_work(struct work_struct *work)
504{
505 struct omap8250_priv *priv;
506
507 priv = container_of(work, struct omap8250_priv, qos_work);
508 pm_qos_update_request(&priv->pm_qos_request, priv->latency);
509}
510
511static irqreturn_t omap_wake_irq(int irq, void *dev_id)
512{
513 struct uart_port *port = dev_id;
514 int ret;
515
516 ret = port->handle_irq(port);
517 if (ret)
518 return IRQ_HANDLED;
519 return IRQ_NONE;
520}
521
522static int omap_8250_startup(struct uart_port *port)
523{
524 struct uart_8250_port *up =
525 container_of(port, struct uart_8250_port, port);
526 struct omap8250_priv *priv = port->private_data;
527
528 int ret;
529
530 if (priv->wakeirq) {
531 ret = request_irq(priv->wakeirq, omap_wake_irq,
532 port->irqflags, "uart wakeup irq", port);
533 if (ret)
534 return ret;
535 disable_irq(priv->wakeirq);
536 }
537
538 pm_runtime_get_sync(port->dev);
539
540 ret = serial8250_do_startup(port);
541 if (ret)
542 goto err;
543
544#ifdef CONFIG_PM_RUNTIME
545 up->capabilities |= UART_CAP_RPM;
546#endif
547
548 /* Enable module level wake up */
549 priv->wer = OMAP_UART_WER_MOD_WKUP;
550 if (priv->habit & OMAP_UART_WER_HAS_TX_WAKEUP)
551 priv->wer |= OMAP_UART_TX_WAKEUP_EN;
552 serial_out(up, UART_OMAP_WER, priv->wer);
553
554 pm_runtime_mark_last_busy(port->dev);
555 pm_runtime_put_autosuspend(port->dev);
556 return 0;
557err:
558 pm_runtime_mark_last_busy(port->dev);
559 pm_runtime_put_autosuspend(port->dev);
560 if (priv->wakeirq)
561 free_irq(priv->wakeirq, port);
562 return ret;
563}
564
565static void omap_8250_shutdown(struct uart_port *port)
566{
567 struct uart_8250_port *up =
568 container_of(port, struct uart_8250_port, port);
569 struct omap8250_priv *priv = port->private_data;
570
571 flush_work(&priv->qos_work);
572
573 pm_runtime_get_sync(port->dev);
574
575 serial_out(up, UART_OMAP_WER, 0);
576 serial8250_do_shutdown(port);
577
578 pm_runtime_mark_last_busy(port->dev);
579 pm_runtime_put_autosuspend(port->dev);
580
581 if (priv->wakeirq)
582 free_irq(priv->wakeirq, port);
583}
584
585static void omap_8250_throttle(struct uart_port *port)
586{
587 unsigned long flags;
588 struct uart_8250_port *up =
589 container_of(port, struct uart_8250_port, port);
590
591 pm_runtime_get_sync(port->dev);
592
593 spin_lock_irqsave(&port->lock, flags);
594 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
595 serial_out(up, UART_IER, up->ier);
596 spin_unlock_irqrestore(&port->lock, flags);
597
598 pm_runtime_mark_last_busy(port->dev);
599 pm_runtime_put_autosuspend(port->dev);
600}
601
602static void omap_8250_unthrottle(struct uart_port *port)
603{
604 unsigned long flags;
605 struct uart_8250_port *up =
606 container_of(port, struct uart_8250_port, port);
607
608 pm_runtime_get_sync(port->dev);
609
610 spin_lock_irqsave(&port->lock, flags);
611 up->ier |= UART_IER_RLSI | UART_IER_RDI;
612 serial_out(up, UART_IER, up->ier);
613 spin_unlock_irqrestore(&port->lock, flags);
614
615 pm_runtime_mark_last_busy(port->dev);
616 pm_runtime_put_autosuspend(port->dev);
617}
618
619static int omap8250_probe(struct platform_device *pdev)
620{
621 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
622 struct resource *irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
623 struct omap8250_priv *priv;
624 struct uart_8250_port up;
625 int ret;
626 void __iomem *membase;
627
628 if (!regs || !irq) {
629 dev_err(&pdev->dev, "missing registers or irq\n");
630 return -EINVAL;
631 }
632
633 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
634 if (!priv)
635 return -ENOMEM;
636
637 membase = devm_ioremap_nocache(&pdev->dev, regs->start,
638 resource_size(regs));
639 if (!membase)
640 return -ENODEV;
641
642 memset(&up, 0, sizeof(up));
643 up.port.dev = &pdev->dev;
644 up.port.mapbase = regs->start;
645 up.port.membase = membase;
646 up.port.irq = irq->start;
647 /*
648 * It claims to be 16C750 compatible however it is a little different.
649 * It has EFR and has no FCR7_64byte bit. The AFE (which it claims to
650 * have) is enabled via EFR instead of MCR. The type is set here 8250
651 * just to get things going. UNKNOWN does not work for a few reasons and
652 * we don't need our own type since we don't use 8250's set_termios()
653 * or pm callback.
654 */
655 up.port.type = PORT_8250;
656 up.port.iotype = UPIO_MEM;
657 up.port.flags = UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_SOFT_FLOW |
658 UPF_HARD_FLOW;
659 up.port.private_data = priv;
660
661 up.port.regshift = 2;
662 up.port.fifosize = 64;
663 up.tx_loadsz = 64;
664 up.capabilities = UART_CAP_FIFO;
665#ifdef CONFIG_PM_RUNTIME
666 /*
667 * PM_RUNTIME is mostly transparent. However to do it right we need to a
668 * TX empty interrupt before we can put the device to auto idle. So if
669 * PM_RUNTIME is not enabled we don't add that flag and can spare that
670 * one extra interrupt in the TX path.
671 */
672 up.capabilities |= UART_CAP_RPM;
673#endif
674 up.port.set_termios = omap_8250_set_termios;
675 up.port.pm = omap_8250_pm;
676 up.port.startup = omap_8250_startup;
677 up.port.shutdown = omap_8250_shutdown;
678 up.port.throttle = omap_8250_throttle;
679 up.port.unthrottle = omap_8250_unthrottle;
680
681 if (pdev->dev.of_node) {
682 up.port.line = of_alias_get_id(pdev->dev.of_node, "serial");
683 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
684 &up.port.uartclk);
685 priv->wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
686 } else {
687 up.port.line = pdev->id;
688 }
689
690 if (up.port.line < 0) {
691 dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
692 up.port.line);
693 return -ENODEV;
694 }
695 if (!up.port.uartclk) {
696 up.port.uartclk = DEFAULT_CLK_SPEED;
697 dev_warn(&pdev->dev,
698 "No clock speed specified: using default: %d\n",
699 DEFAULT_CLK_SPEED);
700 }
701
702 priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
703 priv->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
704 pm_qos_add_request(&priv->pm_qos_request, PM_QOS_CPU_DMA_LATENCY,
705 priv->latency);
706 INIT_WORK(&priv->qos_work, omap8250_uart_qos_work);
707
708 device_init_wakeup(&pdev->dev, true);
709 pm_runtime_use_autosuspend(&pdev->dev);
710 pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
711
712 pm_runtime_irq_safe(&pdev->dev);
713 pm_runtime_enable(&pdev->dev);
714
715 pm_runtime_get_sync(&pdev->dev);
716
717 omap_serial_fill_features_erratas(&up, priv);
718 ret = serial8250_register_8250_port(&up);
719 if (ret < 0) {
720 dev_err(&pdev->dev, "unable to register 8250 port\n");
721 goto err;
722 }
723 priv->line = ret;
724 platform_set_drvdata(pdev, priv);
725 pm_runtime_mark_last_busy(&pdev->dev);
726 pm_runtime_put_autosuspend(&pdev->dev);
727 return 0;
728err:
729 pm_runtime_put(&pdev->dev);
730 pm_runtime_disable(&pdev->dev);
731 return ret;
732}
733
734static int omap8250_remove(struct platform_device *pdev)
735{
736 struct omap8250_priv *priv = platform_get_drvdata(pdev);
737
738 pm_runtime_put_sync(&pdev->dev);
739 pm_runtime_disable(&pdev->dev);
740 serial8250_unregister_port(priv->line);
741 pm_qos_remove_request(&priv->pm_qos_request);
742 device_init_wakeup(&pdev->dev, false);
743 return 0;
744}
745
746#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_RUNTIME)
747
748static inline void omap8250_enable_wakeirq(struct omap8250_priv *priv,
749 bool enable)
750{
751 if (!priv->wakeirq)
752 return;
753
754 if (enable)
755 enable_irq(priv->wakeirq);
756 else
757 disable_irq_nosync(priv->wakeirq);
758}
759
760static void omap8250_enable_wakeup(struct omap8250_priv *priv,
761 bool enable)
762{
763 if (enable == priv->wakeups_enabled)
764 return;
765
766 omap8250_enable_wakeirq(priv, enable);
767 priv->wakeups_enabled = enable;
768}
769#endif
770
771#ifdef CONFIG_PM_SLEEP
772static int omap8250_prepare(struct device *dev)
773{
774 struct omap8250_priv *priv = dev_get_drvdata(dev);
775
776 if (!priv)
777 return 0;
778 priv->is_suspending = true;
779 return 0;
780}
781
782static void omap8250_complete(struct device *dev)
783{
784 struct omap8250_priv *priv = dev_get_drvdata(dev);
785
786 if (!priv)
787 return;
788 priv->is_suspending = false;
789}
790
791static int omap8250_suspend(struct device *dev)
792{
793 struct omap8250_priv *priv = dev_get_drvdata(dev);
794
795 serial8250_suspend_port(priv->line);
796 flush_work(&priv->qos_work);
797
798 if (device_may_wakeup(dev))
799 omap8250_enable_wakeup(priv, true);
800 else
801 omap8250_enable_wakeup(priv, false);
802 return 0;
803}
804
805static int omap8250_resume(struct device *dev)
806{
807 struct omap8250_priv *priv = dev_get_drvdata(dev);
808
809 if (device_may_wakeup(dev))
810 omap8250_enable_wakeup(priv, false);
811
812 serial8250_resume_port(priv->line);
813 return 0;
814}
815#else
816#define omap8250_prepare NULL
817#define omap8250_complete NULL
818#endif
819
820#ifdef CONFIG_PM_RUNTIME
821static int omap8250_lost_context(struct uart_8250_port *up)
822{
823 u32 val;
824
825 val = serial_in(up, UART_OMAP_MDR1);
826 /*
827 * If we lose context, then MDR1 is set to its reset value which is
828 * UART_OMAP_MDR1_DISABLE. After set_termios() we set it either to 13x
829 * or 16x but never to disable again.
830 */
831 if (val == UART_OMAP_MDR1_DISABLE)
832 return 1;
833 return 0;
834}
835
836static int omap8250_runtime_suspend(struct device *dev)
837{
838 struct omap8250_priv *priv = dev_get_drvdata(dev);
839 struct uart_8250_port *up;
840
841 up = serial8250_get_port(priv->line);
842 /*
843 * When using 'no_console_suspend', the console UART must not be
844 * suspended. Since driver suspend is managed by runtime suspend,
845 * preventing runtime suspend (by returning error) will keep device
846 * active during suspend.
847 */
848 if (priv->is_suspending && !console_suspend_enabled) {
849 if (uart_console(&up->port))
850 return -EBUSY;
851 }
852
853 omap8250_enable_wakeup(priv, true);
854
855 priv->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
856 schedule_work(&priv->qos_work);
857
858 return 0;
859}
860
861static int omap8250_runtime_resume(struct device *dev)
862{
863 struct omap8250_priv *priv = dev_get_drvdata(dev);
864 struct uart_8250_port *up;
865 int loss_cntx;
866
867 /* In case runtime-pm tries this before we are setup */
868 if (!priv)
869 return 0;
870
871 up = serial8250_get_port(priv->line);
872 omap8250_enable_wakeup(priv, false);
873 loss_cntx = omap8250_lost_context(up);
874
875 if (loss_cntx)
876 omap8250_restore_regs(up);
877
878 priv->latency = priv->calc_latency;
879 schedule_work(&priv->qos_work);
880 return 0;
881}
882#endif
883
884static const struct dev_pm_ops omap8250_dev_pm_ops = {
885 SET_SYSTEM_SLEEP_PM_OPS(omap8250_suspend, omap8250_resume)
886 SET_RUNTIME_PM_OPS(omap8250_runtime_suspend,
887 omap8250_runtime_resume, NULL)
888 .prepare = omap8250_prepare,
889 .complete = omap8250_complete,
890};
891
892static const struct of_device_id omap8250_dt_ids[] = {
893 { .compatible = "ti,omap2-uart" },
894 { .compatible = "ti,omap3-uart" },
895 { .compatible = "ti,omap4-uart" },
896 {},
897};
898MODULE_DEVICE_TABLE(of, omap8250_dt_ids);
899
900static struct platform_driver omap8250_platform_driver = {
901 .driver = {
902 .name = "omap8250",
903 .pm = &omap8250_dev_pm_ops,
904 .of_match_table = omap8250_dt_ids,
905 .owner = THIS_MODULE,
906 },
907 .probe = omap8250_probe,
908 .remove = omap8250_remove,
909};
910module_platform_driver(omap8250_platform_driver);
911
912MODULE_AUTHOR("Sebastian Andrzej Siewior");
913MODULE_DESCRIPTION("OMAP 8250 Driver");
914MODULE_LICENSE("GPL v2");