omap-serial.c 38.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Driver for OMAP-UART controller.
 * Based on drivers/serial/8250.c
 *
 * Copyright (C) 2010 Texas Instruments.
 *
 * Authors:
 *	Govindraj R	<govindraj.raja@ti.com>
 *	Thara Gopinath	<thara@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
L
Lucas De Marchi 已提交
16
 * Note: This driver is made separate from 8250 driver as we cannot
17 18 19 20 21 22
 * over load 8250 driver with omap platform specific configuration for
 * features like DMA, it makes easier to implement features like DMA and
 * hardware flow control and software flow control configuration with
 * this driver as required for the omap-platform.
 */

23 24 25 26
#if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

27 28 29 30 31 32 33 34 35 36 37 38
#include <linux/module.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/serial_reg.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/serial_core.h>
#include <linux/irq.h>
39
#include <linux/pm_runtime.h>
40
#include <linux/of.h>
41
#include <linux/gpio.h>
42 43 44 45

#include <plat/dmtimer.h>
#include <plat/omap-serial.h>

46 47 48 49 50 51 52
#define UART_BUILD_REVISION(x, y)	(((x) << 8) | (y))

#define OMAP_UART_REV_42 0x0402
#define OMAP_UART_REV_46 0x0406
#define OMAP_UART_REV_52 0x0502
#define OMAP_UART_REV_63 0x0603

53 54
#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz*/

55 56 57 58 59 60 61
/* SCR register bitmasks */
#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK		(1 << 7)

/* FCR register bitmasks */
#define OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT		6
#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK			(0x3 << 6)

62 63 64 65 66 67 68 69 70 71 72
/* MVR register bitmasks */
#define OMAP_UART_MVR_SCHEME_SHIFT	30

#define OMAP_UART_LEGACY_MVR_MAJ_MASK	0xf0
#define OMAP_UART_LEGACY_MVR_MAJ_SHIFT	4
#define OMAP_UART_LEGACY_MVR_MIN_MASK	0x0f

#define OMAP_UART_MVR_MAJ_MASK		0x700
#define OMAP_UART_MVR_MAJ_SHIFT		8
#define OMAP_UART_MVR_MIN_MASK		0x3f

73 74 75
static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];

/* Forward declaration of functions */
76
static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
77

78
static struct workqueue_struct *serial_omap_uart_wq;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
{
	offset <<= up->port.regshift;
	return readw(up->port.membase + offset);
}

static inline void serial_out(struct uart_omap_port *up, int offset, int value)
{
	offset <<= up->port.regshift;
	writew(value, up->port.membase + offset);
}

static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
{
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
		       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
	serial_out(up, UART_FCR, 0);
}

100 101
static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
{
102
	struct omap_uart_port_info *pdata = up->dev->platform_data;
103 104 105 106

	if (!pdata->get_context_loss_count)
		return 0;

107
	return pdata->get_context_loss_count(up->dev);
108 109 110 111
}

static void serial_omap_set_forceidle(struct uart_omap_port *up)
{
112
	struct omap_uart_port_info *pdata = up->dev->platform_data;
113 114

	if (pdata->set_forceidle)
115
		pdata->set_forceidle(up->dev);
116 117 118 119
}

static void serial_omap_set_noidle(struct uart_omap_port *up)
{
120
	struct omap_uart_port_info *pdata = up->dev->platform_data;
121 122

	if (pdata->set_noidle)
123
		pdata->set_noidle(up->dev);
124 125 126 127
}

static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
{
128
	struct omap_uart_port_info *pdata = up->dev->platform_data;
129 130

	if (pdata->enable_wakeup)
131
		pdata->enable_wakeup(up->dev, enable);
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/*
 * serial_omap_get_divisor - calculate divisor value
 * @port: uart port info
 * @baud: baudrate for which divisor needs to be calculated.
 *
 * We have written our own function to get the divisor so as to support
 * 13x mode. 3Mbps Baudrate as an different divisor.
 * Reference OMAP TRM Chapter 17:
 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
 * referring to oversampling - divisor value
 * baudrate 460,800 to 3,686,400 all have divisor 13
 * except 3,000,000 which has divisor value 16
 */
static unsigned int
serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
{
	unsigned int divisor;

	if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
		divisor = 13;
	else
		divisor = 16;
	return port->uartclk/(baud * divisor);
}

static void serial_omap_enable_ms(struct uart_port *port)
{
161
	struct uart_omap_port *up = to_uart_omap_port(port);
162

163
	dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->port.line);
164

165
	pm_runtime_get_sync(up->dev);
166 167
	up->ier |= UART_IER_MSI;
	serial_out(up, UART_IER, up->ier);
168
	pm_runtime_put(up->dev);
169 170 171 172
}

static void serial_omap_stop_tx(struct uart_port *port)
{
173
	struct uart_omap_port *up = to_uart_omap_port(port);
174

175
	pm_runtime_get_sync(up->dev);
176 177 178 179
	if (up->ier & UART_IER_THRI) {
		up->ier &= ~UART_IER_THRI;
		serial_out(up, UART_IER, up->ier);
	}
180

F
Felipe Balbi 已提交
181
	serial_omap_set_forceidle(up);
182

183 184
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
185 186 187 188
}

static void serial_omap_stop_rx(struct uart_port *port)
{
189
	struct uart_omap_port *up = to_uart_omap_port(port);
190

191
	pm_runtime_get_sync(up->dev);
192 193 194
	up->ier &= ~UART_IER_RLSI;
	up->port.read_status_mask &= ~UART_LSR_DR;
	serial_out(up, UART_IER, up->ier);
195 196
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
197 198
}

199 200
static inline void receive_chars(struct uart_omap_port *up,
		unsigned int *status)
201 202
{
	struct tty_struct *tty = up->port.state->port.tty;
203 204
	unsigned int flag, lsr = *status;
	unsigned char ch = 0;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	int max_count = 256;

	do {
		if (likely(lsr & UART_LSR_DR))
			ch = serial_in(up, UART_RX);
		flag = TTY_NORMAL;
		up->port.icount.rx++;

		if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
			/*
			 * For statistics only
			 */
			if (lsr & UART_LSR_BI) {
				lsr &= ~(UART_LSR_FE | UART_LSR_PE);
				up->port.icount.brk++;
				/*
				 * We do the SysRQ and SAK checking
				 * here because otherwise the break
				 * may get masked by ignore_status_mask
				 * or read_status_mask.
				 */
				if (uart_handle_break(&up->port))
					goto ignore_char;
			} else if (lsr & UART_LSR_PE) {
				up->port.icount.parity++;
			} else if (lsr & UART_LSR_FE) {
				up->port.icount.frame++;
			}

			if (lsr & UART_LSR_OE)
				up->port.icount.overrun++;

			/*
			 * Mask off conditions which should be ignored.
			 */
			lsr &= up->port.read_status_mask;

#ifdef CONFIG_SERIAL_OMAP_CONSOLE
			if (up->port.line == up->port.cons->index) {
				/* Recover the break flag from console xmit */
				lsr |= up->lsr_break_flag;
			}
#endif
			if (lsr & UART_LSR_BI)
				flag = TTY_BREAK;
			else if (lsr & UART_LSR_PE)
				flag = TTY_PARITY;
			else if (lsr & UART_LSR_FE)
				flag = TTY_FRAME;
		}

		if (uart_handle_sysrq_char(&up->port, ch))
			goto ignore_char;
		uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
ignore_char:
		lsr = serial_in(up, UART_LSR);
	} while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
	spin_unlock(&up->port.lock);
	tty_flip_buffer_push(tty);
	spin_lock(&up->port.lock);
}

static void transmit_chars(struct uart_omap_port *up)
{
	struct circ_buf *xmit = &up->port.state->xmit;
	int count;

	if (up->port.x_char) {
		serial_out(up, UART_TX, up->port.x_char);
		up->port.icount.tx++;
		up->port.x_char = 0;
		return;
	}
	if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
		serial_omap_stop_tx(&up->port);
		return;
	}
282
	count = up->port.fifosize / 4;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	do {
		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		up->port.icount.tx++;
		if (uart_circ_empty(xmit))
			break;
	} while (--count > 0);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&up->port);

	if (uart_circ_empty(xmit))
		serial_omap_stop_tx(&up->port);
}

static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
{
	if (!(up->ier & UART_IER_THRI)) {
		up->ier |= UART_IER_THRI;
		serial_out(up, UART_IER, up->ier);
	}
}

static void serial_omap_start_tx(struct uart_port *port)
{
308
	struct uart_omap_port *up = to_uart_omap_port(port);
309

F
Felipe Balbi 已提交
310 311 312 313 314
	pm_runtime_get_sync(up->dev);
	serial_omap_enable_ier_thri(up);
	serial_omap_set_noidle(up);
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
}

static unsigned int check_modem_status(struct uart_omap_port *up)
{
	unsigned int status;

	status = serial_in(up, UART_MSR);
	status |= up->msr_saved_flags;
	up->msr_saved_flags = 0;
	if ((status & UART_MSR_ANY_DELTA) == 0)
		return status;

	if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
	    up->port.state != NULL) {
		if (status & UART_MSR_TERI)
			up->port.icount.rng++;
		if (status & UART_MSR_DDSR)
			up->port.icount.dsr++;
		if (status & UART_MSR_DDCD)
			uart_handle_dcd_change
				(&up->port, status & UART_MSR_DCD);
		if (status & UART_MSR_DCTS)
			uart_handle_cts_change
				(&up->port, status & UART_MSR_CTS);
		wake_up_interruptible(&up->port.state->port.delta_msr_wait);
	}

	return status;
}

/**
 * serial_omap_irq() - This handles the interrupt from one port
 * @irq: uart port irq number
 * @dev_id: uart port info
 */
static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
{
	struct uart_omap_port *up = dev_id;
	unsigned int iir, lsr;
354
	unsigned int type;
355
	unsigned long flags;
356
	irqreturn_t ret = IRQ_NONE;
357

358
	spin_lock_irqsave(&up->port.lock, flags);
359
	pm_runtime_get_sync(up->dev);
360
	iir = serial_in(up, UART_IIR);
361 362 363
again:
	if (iir & UART_IIR_NO_INT)
		goto out;
364

365
	ret = IRQ_HANDLED;
366
	lsr = serial_in(up, UART_LSR);
367 368 369 370 371 372 373 374 375 376 377 378 379

	/* extract IRQ type from IIR register */
	type = iir & 0x3e;

	switch (type) {
	case UART_IIR_MSI:
		check_modem_status(up);
		break;
	case UART_IIR_THRI:
		if (lsr & UART_LSR_THRE)
			transmit_chars(up);
		break;
	case UART_IIR_RDI:
F
Felipe Balbi 已提交
380 381
		if (lsr & UART_LSR_DR)
			receive_chars(up, &lsr);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		break;
	case UART_IIR_RLSI:
		if (lsr & UART_LSR_BRK_ERROR_BITS)
			receive_chars(up, &lsr);
		break;
	case UART_IIR_RX_TIMEOUT:
		receive_chars(up, &lsr);
		break;
	case UART_IIR_CTS_RTS_DSR:
		iir = serial_in(up, UART_IIR);
		goto again;
	case UART_IIR_XOFF:
		/* FALLTHROUGH */
	default:
		break;
397 398
	}

399
out:
400
	spin_unlock_irqrestore(&up->port.lock, flags);
401 402
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
403
	up->port_activity = jiffies;
404 405

	return ret;
406 407 408 409
}

static unsigned int serial_omap_tx_empty(struct uart_port *port)
{
410
	struct uart_omap_port *up = to_uart_omap_port(port);
411 412 413
	unsigned long flags = 0;
	unsigned int ret = 0;

414
	pm_runtime_get_sync(up->dev);
415
	dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
416 417 418
	spin_lock_irqsave(&up->port.lock, flags);
	ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
	spin_unlock_irqrestore(&up->port.lock, flags);
419
	pm_runtime_put(up->dev);
420 421 422 423 424
	return ret;
}

static unsigned int serial_omap_get_mctrl(struct uart_port *port)
{
425
	struct uart_omap_port *up = to_uart_omap_port(port);
426
	unsigned int status;
427 428
	unsigned int ret = 0;

429
	pm_runtime_get_sync(up->dev);
430
	status = check_modem_status(up);
431
	pm_runtime_put(up->dev);
432

433
	dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
434 435 436 437 438 439 440 441 442 443 444 445 446 447

	if (status & UART_MSR_DCD)
		ret |= TIOCM_CAR;
	if (status & UART_MSR_RI)
		ret |= TIOCM_RNG;
	if (status & UART_MSR_DSR)
		ret |= TIOCM_DSR;
	if (status & UART_MSR_CTS)
		ret |= TIOCM_CTS;
	return ret;
}

static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
448
	struct uart_omap_port *up = to_uart_omap_port(port);
449 450
	unsigned char mcr = 0;

451
	dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
452 453 454 455 456 457 458 459 460 461 462
	if (mctrl & TIOCM_RTS)
		mcr |= UART_MCR_RTS;
	if (mctrl & TIOCM_DTR)
		mcr |= UART_MCR_DTR;
	if (mctrl & TIOCM_OUT1)
		mcr |= UART_MCR_OUT1;
	if (mctrl & TIOCM_OUT2)
		mcr |= UART_MCR_OUT2;
	if (mctrl & TIOCM_LOOP)
		mcr |= UART_MCR_LOOP;

463
	pm_runtime_get_sync(up->dev);
464 465 466
	up->mcr = serial_in(up, UART_MCR);
	up->mcr |= mcr;
	serial_out(up, UART_MCR, up->mcr);
467
	pm_runtime_put(up->dev);
468 469 470 471 472 473 474 475 476 477

	if (gpio_is_valid(up->DTR_gpio) &&
	    !!(mctrl & TIOCM_DTR) != up->DTR_active) {
		up->DTR_active = !up->DTR_active;
		if (gpio_cansleep(up->DTR_gpio))
			schedule_work(&up->qos_work);
		else
			gpio_set_value(up->DTR_gpio,
				       up->DTR_active != up->DTR_inverted);
	}
478 479 480 481
}

static void serial_omap_break_ctl(struct uart_port *port, int break_state)
{
482
	struct uart_omap_port *up = to_uart_omap_port(port);
483 484
	unsigned long flags = 0;

485
	dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
486
	pm_runtime_get_sync(up->dev);
487 488 489 490 491 492 493
	spin_lock_irqsave(&up->port.lock, flags);
	if (break_state == -1)
		up->lcr |= UART_LCR_SBC;
	else
		up->lcr &= ~UART_LCR_SBC;
	serial_out(up, UART_LCR, up->lcr);
	spin_unlock_irqrestore(&up->port.lock, flags);
494
	pm_runtime_put(up->dev);
495 496 497 498
}

static int serial_omap_startup(struct uart_port *port)
{
499
	struct uart_omap_port *up = to_uart_omap_port(port);
500 501 502 503 504 505 506 507 508 509 510
	unsigned long flags = 0;
	int retval;

	/*
	 * Allocate the IRQ
	 */
	retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
				up->name, up);
	if (retval)
		return retval;

511
	dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->port.line);
512

513
	pm_runtime_get_sync(up->dev);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
	/*
	 * Clear the FIFO buffers and disable them.
	 * (they will be reenabled in set_termios())
	 */
	serial_omap_clear_fifos(up);
	/* For Hardware flow control */
	serial_out(up, UART_MCR, UART_MCR_RTS);

	/*
	 * Clear the interrupt registers.
	 */
	(void) serial_in(up, UART_LSR);
	if (serial_in(up, UART_LSR) & UART_LSR_DR)
		(void) serial_in(up, UART_RX);
	(void) serial_in(up, UART_IIR);
	(void) serial_in(up, UART_MSR);

	/*
	 * Now, initialize the UART
	 */
	serial_out(up, UART_LCR, UART_LCR_WLEN8);
	spin_lock_irqsave(&up->port.lock, flags);
	/*
	 * Most PC uarts need OUT2 raised to enable interrupts.
	 */
	up->port.mctrl |= TIOCM_OUT2;
	serial_omap_set_mctrl(&up->port, up->port.mctrl);
	spin_unlock_irqrestore(&up->port.lock, flags);

	up->msr_saved_flags = 0;
	/*
	 * Finally, enable interrupts. Note: Modem status interrupts
	 * are set via set_termios(), which will be occurring imminently
	 * anyway, so we don't enable them here.
	 */
	up->ier = UART_IER_RLSI | UART_IER_RDI;
	serial_out(up, UART_IER, up->ier);

552 553 554
	/* Enable module level wake up */
	serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);

555 556
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
557 558 559 560 561 562
	up->port_activity = jiffies;
	return 0;
}

static void serial_omap_shutdown(struct uart_port *port)
{
563
	struct uart_omap_port *up = to_uart_omap_port(port);
564 565
	unsigned long flags = 0;

566
	dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->port.line);
567

568
	pm_runtime_get_sync(up->dev);
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	/*
	 * Disable interrupts from this port
	 */
	up->ier = 0;
	serial_out(up, UART_IER, 0);

	spin_lock_irqsave(&up->port.lock, flags);
	up->port.mctrl &= ~TIOCM_OUT2;
	serial_omap_set_mctrl(&up->port, up->port.mctrl);
	spin_unlock_irqrestore(&up->port.lock, flags);

	/*
	 * Disable break condition and FIFOs
	 */
	serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
	serial_omap_clear_fifos(up);

	/*
	 * Read data port to reset things, and then free the irq
	 */
	if (serial_in(up, UART_LSR) & UART_LSR_DR)
		(void) serial_in(up, UART_RX);
591

592
	pm_runtime_put(up->dev);
593 594 595 596 597 598 599 600
	free_irq(up->port.irq, up);
}

static inline void
serial_omap_configure_xonxoff
		(struct uart_omap_port *up, struct ktermios *termios)
{
	up->lcr = serial_in(up, UART_LCR);
601
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
602 603 604 605 606 607 608
	up->efr = serial_in(up, UART_EFR);
	serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);

	serial_out(up, UART_XON1, termios->c_cc[VSTART]);
	serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);

	/* clear SW control mode bits */
609
	up->efr &= OMAP_UART_SW_CLR;
610 611 612 613 614 615 616

	/*
	 * IXON Flag:
	 * Enable XON/XOFF flow control on output.
	 * Transmit XON1, XOFF1
	 */
	if (termios->c_iflag & IXON)
617
		up->efr |= OMAP_UART_SW_TX;
618 619 620 621 622 623 624

	/*
	 * IXOFF Flag:
	 * Enable XON/XOFF flow control on input.
	 * Receiver compares XON1, XOFF1.
	 */
	if (termios->c_iflag & IXOFF)
625
		up->efr |= OMAP_UART_SW_RX;
626 627

	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
628
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
629 630 631 632 633 634 635 636 637 638 639 640 641

	up->mcr = serial_in(up, UART_MCR);

	/*
	 * IXANY Flag:
	 * Enable any character to restart output.
	 * Operation resumes after receiving any
	 * character after recognition of the XOFF character
	 */
	if (termios->c_iflag & IXANY)
		up->mcr |= UART_MCR_XONANY;

	serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
642
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
643 644 645 646 647
	serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
	/* Enable special char function UARTi.EFR_REG[5] and
	 * load the new software flow control mode IXON or IXOFF
	 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
	 */
648
	serial_out(up, UART_EFR, up->efr | UART_EFR_SCD);
649
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
650 651 652 653 654

	serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
	serial_out(up, UART_LCR, up->lcr);
}

655 656 657 658 659 660
static void serial_omap_uart_qos_work(struct work_struct *work)
{
	struct uart_omap_port *up = container_of(work, struct uart_omap_port,
						qos_work);

	pm_qos_update_request(&up->pm_qos_request, up->latency);
661 662 663
	if (gpio_is_valid(up->DTR_gpio))
		gpio_set_value_cansleep(up->DTR_gpio,
					up->DTR_active != up->DTR_inverted);
664 665
}

666 667 668 669
static void
serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
			struct ktermios *old)
{
670
	struct uart_omap_port *up = to_uart_omap_port(port);
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	unsigned char cval = 0;
	unsigned char efr = 0;
	unsigned long flags = 0;
	unsigned int baud, quot;

	switch (termios->c_cflag & CSIZE) {
	case CS5:
		cval = UART_LCR_WLEN5;
		break;
	case CS6:
		cval = UART_LCR_WLEN6;
		break;
	case CS7:
		cval = UART_LCR_WLEN7;
		break;
	default:
	case CS8:
		cval = UART_LCR_WLEN8;
		break;
	}

	if (termios->c_cflag & CSTOPB)
		cval |= UART_LCR_STOP;
	if (termios->c_cflag & PARENB)
		cval |= UART_LCR_PARITY;
	if (!(termios->c_cflag & PARODD))
		cval |= UART_LCR_EPAR;

	/*
	 * Ask the core to calculate the divisor for us.
	 */

	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
	quot = serial_omap_get_divisor(port, baud);

706
	/* calculate wakeup latency constraint */
707
	up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
708 709 710
	up->latency = up->calc_latency;
	schedule_work(&up->qos_work);

711 712 713 714
	up->dll = quot & 0xff;
	up->dlh = quot >> 8;
	up->mdr1 = UART_OMAP_MDR1_DISABLE;

715 716 717 718 719 720 721
	up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
			UART_FCR_ENABLE_FIFO;

	/*
	 * Ok, we're now changing the port state. Do it with
	 * interrupts disabled.
	 */
722
	pm_runtime_get_sync(up->dev);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	spin_lock_irqsave(&up->port.lock, flags);

	/*
	 * Update the per-port timeout.
	 */
	uart_update_timeout(port, termios->c_cflag, baud);

	up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
	if (termios->c_iflag & INPCK)
		up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
	if (termios->c_iflag & (BRKINT | PARMRK))
		up->port.read_status_mask |= UART_LSR_BI;

	/*
	 * Characters to ignore
	 */
	up->port.ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR)
		up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
	if (termios->c_iflag & IGNBRK) {
		up->port.ignore_status_mask |= UART_LSR_BI;
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (termios->c_iflag & IGNPAR)
			up->port.ignore_status_mask |= UART_LSR_OE;
	}

	/*
	 * ignore all characters if CREAD is not set
	 */
	if ((termios->c_cflag & CREAD) == 0)
		up->port.ignore_status_mask |= UART_LSR_DR;

	/*
	 * Modem status interrupts
	 */
	up->ier &= ~UART_IER_MSI;
	if (UART_ENABLE_MS(&up->port, termios->c_cflag))
		up->ier |= UART_IER_MSI;
	serial_out(up, UART_IER, up->ier);
	serial_out(up, UART_LCR, cval);		/* reset DLAB */
766
	up->lcr = cval;
767
	up->scr = OMAP_UART_SCR_TX_EMPTY;
768 769 770 771 772 773 774

	/* FIFOs and DMA Settings */

	/* FCR can be changed only when the
	 * baud clock is not running
	 * DLL_REG and DLH_REG set to 0.
	 */
775
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
776 777 778 779
	serial_out(up, UART_DLL, 0);
	serial_out(up, UART_DLM, 0);
	serial_out(up, UART_LCR, 0);

780
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
781 782 783 784

	up->efr = serial_in(up, UART_EFR);
	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);

785
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
786 787 788
	up->mcr = serial_in(up, UART_MCR);
	serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
	/* FIFO ENABLE, DMA MODE */
789 790

	up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
791

F
Felipe Balbi 已提交
792 793 794
	/* Set receive FIFO threshold to 1 byte */
	up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
	up->fcr |= (0x1 << OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT);
795

796 797 798
	serial_out(up, UART_FCR, up->fcr);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);

799 800
	serial_out(up, UART_OMAP_SCR, up->scr);

801
	serial_out(up, UART_EFR, up->efr);
802
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
803 804 805 806
	serial_out(up, UART_MCR, up->mcr);

	/* Protocol, Baud Rate, and Interrupt Settings */

807 808 809 810 811
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, up->mdr1);
	else
		serial_out(up, UART_OMAP_MDR1, up->mdr1);

812
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
813 814 815 816 817 818

	up->efr = serial_in(up, UART_EFR);
	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);

	serial_out(up, UART_LCR, 0);
	serial_out(up, UART_IER, 0);
819
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
820

821 822
	serial_out(up, UART_DLL, up->dll);	/* LS of divisor */
	serial_out(up, UART_DLM, up->dlh);	/* MS of divisor */
823 824 825

	serial_out(up, UART_LCR, 0);
	serial_out(up, UART_IER, up->ier);
826
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
827 828 829 830 831

	serial_out(up, UART_EFR, up->efr);
	serial_out(up, UART_LCR, cval);

	if (baud > 230400 && baud != 3000000)
832
		up->mdr1 = UART_OMAP_MDR1_13X_MODE;
833
	else
834 835
		up->mdr1 = UART_OMAP_MDR1_16X_MODE;

836 837 838 839
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, up->mdr1);
	else
		serial_out(up, UART_OMAP_MDR1, up->mdr1);
840 841 842 843 844

	/* Hardware Flow Control Configuration */

	if (termios->c_cflag & CRTSCTS) {
		efr |= (UART_EFR_CTS | UART_EFR_RTS);
845
		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
846 847 848 849

		up->mcr = serial_in(up, UART_MCR);
		serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);

850
		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
851 852 853 854 855
		up->efr = serial_in(up, UART_EFR);
		serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);

		serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
		serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
856
		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
857 858 859 860 861 862
		serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
		serial_out(up, UART_LCR, cval);
	}

	serial_omap_set_mctrl(&up->port, up->port.mctrl);
	/* Software Flow Control Configuration */
863
	serial_omap_configure_xonxoff(up, termios);
864 865

	spin_unlock_irqrestore(&up->port.lock, flags);
866
	pm_runtime_put(up->dev);
867
	dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
868 869 870 871 872 873
}

static void
serial_omap_pm(struct uart_port *port, unsigned int state,
	       unsigned int oldstate)
{
874
	struct uart_omap_port *up = to_uart_omap_port(port);
875 876
	unsigned char efr;

877
	dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->port.line);
878

879
	pm_runtime_get_sync(up->dev);
880
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
881 882 883 884 885
	efr = serial_in(up, UART_EFR);
	serial_out(up, UART_EFR, efr | UART_EFR_ECB);
	serial_out(up, UART_LCR, 0);

	serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
886
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
887 888
	serial_out(up, UART_EFR, efr);
	serial_out(up, UART_LCR, 0);
889

890
	if (!device_may_wakeup(up->dev)) {
891
		if (!state)
892
			pm_runtime_forbid(up->dev);
893
		else
894
			pm_runtime_allow(up->dev);
895 896
	}

897
	pm_runtime_put(up->dev);
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
}

static void serial_omap_release_port(struct uart_port *port)
{
	dev_dbg(port->dev, "serial_omap_release_port+\n");
}

static int serial_omap_request_port(struct uart_port *port)
{
	dev_dbg(port->dev, "serial_omap_request_port+\n");
	return 0;
}

static void serial_omap_config_port(struct uart_port *port, int flags)
{
913
	struct uart_omap_port *up = to_uart_omap_port(port);
914 915

	dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
916
							up->port.line);
917 918 919 920 921 922 923 924 925 926 927 928 929 930
	up->port.type = PORT_OMAP;
}

static int
serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	/* we don't want the core code to modify any port params */
	dev_dbg(port->dev, "serial_omap_verify_port+\n");
	return -EINVAL;
}

static const char *
serial_omap_type(struct uart_port *port)
{
931
	struct uart_omap_port *up = to_uart_omap_port(port);
932

933
	dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	return up->name;
}

#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)

static inline void wait_for_xmitr(struct uart_omap_port *up)
{
	unsigned int status, tmout = 10000;

	/* Wait up to 10ms for the character(s) to be sent. */
	do {
		status = serial_in(up, UART_LSR);

		if (status & UART_LSR_BI)
			up->lsr_break_flag = UART_LSR_BI;

		if (--tmout == 0)
			break;
		udelay(1);
	} while ((status & BOTH_EMPTY) != BOTH_EMPTY);

	/* Wait up to 1s for flow control if necessary */
	if (up->port.flags & UPF_CONS_FLOW) {
		tmout = 1000000;
		for (tmout = 1000000; tmout; tmout--) {
			unsigned int msr = serial_in(up, UART_MSR);

			up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
			if (msr & UART_MSR_CTS)
				break;

			udelay(1);
		}
	}
}

970 971 972 973
#ifdef CONFIG_CONSOLE_POLL

static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
{
974
	struct uart_omap_port *up = to_uart_omap_port(port);
975

976
	pm_runtime_get_sync(up->dev);
977 978
	wait_for_xmitr(up);
	serial_out(up, UART_TX, ch);
979
	pm_runtime_put(up->dev);
980 981 982 983
}

static int serial_omap_poll_get_char(struct uart_port *port)
{
984
	struct uart_omap_port *up = to_uart_omap_port(port);
985
	unsigned int status;
986

987
	pm_runtime_get_sync(up->dev);
988
	status = serial_in(up, UART_LSR);
989 990 991
	if (!(status & UART_LSR_DR))
		return NO_POLL_CHAR;

992
	status = serial_in(up, UART_RX);
993
	pm_runtime_put(up->dev);
994
	return status;
995 996 997 998 999 1000 1001 1002 1003 1004
}

#endif /* CONFIG_CONSOLE_POLL */

#ifdef CONFIG_SERIAL_OMAP_CONSOLE

static struct uart_omap_port *serial_omap_console_ports[4];

static struct uart_driver serial_omap_reg;

1005 1006
static void serial_omap_console_putchar(struct uart_port *port, int ch)
{
1007
	struct uart_omap_port *up = to_uart_omap_port(port);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	wait_for_xmitr(up);
	serial_out(up, UART_TX, ch);
}

static void
serial_omap_console_write(struct console *co, const char *s,
		unsigned int count)
{
	struct uart_omap_port *up = serial_omap_console_ports[co->index];
	unsigned long flags;
	unsigned int ier;
	int locked = 1;

1022
	pm_runtime_get_sync(up->dev);
1023

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	local_irq_save(flags);
	if (up->port.sysrq)
		locked = 0;
	else if (oops_in_progress)
		locked = spin_trylock(&up->port.lock);
	else
		spin_lock(&up->port.lock);

	/*
	 * First save the IER then disable the interrupts
	 */
	ier = serial_in(up, UART_IER);
	serial_out(up, UART_IER, 0);

	uart_console_write(&up->port, s, count, serial_omap_console_putchar);

	/*
	 * Finally, wait for transmitter to become empty
	 * and restore the IER
	 */
	wait_for_xmitr(up);
	serial_out(up, UART_IER, ier);
	/*
	 * The receive handling will happen properly because the
	 * receive ready bit will still be set; it is not cleared
	 * on read.  However, modem control will not, we must
	 * call it if we have saved something in the saved flags
	 * while processing with interrupts off.
	 */
	if (up->msr_saved_flags)
		check_modem_status(up);

1056 1057
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	if (locked)
		spin_unlock(&up->port.lock);
	local_irq_restore(flags);
}

static int __init
serial_omap_console_setup(struct console *co, char *options)
{
	struct uart_omap_port *up;
	int baud = 115200;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

	if (serial_omap_console_ports[co->index] == NULL)
		return -ENODEV;
	up = serial_omap_console_ports[co->index];

	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);

	return uart_set_options(&up->port, co, baud, parity, bits, flow);
}

static struct console serial_omap_console = {
	.name		= OMAP_SERIAL_NAME,
	.write		= serial_omap_console_write,
	.device		= uart_console_device,
	.setup		= serial_omap_console_setup,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.data		= &serial_omap_reg,
};

static void serial_omap_add_console_port(struct uart_omap_port *up)
{
1094
	serial_omap_console_ports[up->port.line] = up;
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
}

#define OMAP_CONSOLE	(&serial_omap_console)

#else

#define OMAP_CONSOLE	NULL

static inline void serial_omap_add_console_port(struct uart_omap_port *up)
{}

#endif

static struct uart_ops serial_omap_pops = {
	.tx_empty	= serial_omap_tx_empty,
	.set_mctrl	= serial_omap_set_mctrl,
	.get_mctrl	= serial_omap_get_mctrl,
	.stop_tx	= serial_omap_stop_tx,
	.start_tx	= serial_omap_start_tx,
	.stop_rx	= serial_omap_stop_rx,
	.enable_ms	= serial_omap_enable_ms,
	.break_ctl	= serial_omap_break_ctl,
	.startup	= serial_omap_startup,
	.shutdown	= serial_omap_shutdown,
	.set_termios	= serial_omap_set_termios,
	.pm		= serial_omap_pm,
	.type		= serial_omap_type,
	.release_port	= serial_omap_release_port,
	.request_port	= serial_omap_request_port,
	.config_port	= serial_omap_config_port,
	.verify_port	= serial_omap_verify_port,
1126 1127 1128 1129
#ifdef CONFIG_CONSOLE_POLL
	.poll_put_char  = serial_omap_poll_put_char,
	.poll_get_char  = serial_omap_poll_get_char,
#endif
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
};

static struct uart_driver serial_omap_reg = {
	.owner		= THIS_MODULE,
	.driver_name	= "OMAP-SERIAL",
	.dev_name	= OMAP_SERIAL_NAME,
	.nr		= OMAP_MAX_HSUART_PORTS,
	.cons		= OMAP_CONSOLE,
};

1140
#ifdef CONFIG_PM_SLEEP
1141
static int serial_omap_suspend(struct device *dev)
1142
{
1143
	struct uart_omap_port *up = dev_get_drvdata(dev);
1144

1145
	if (up) {
1146
		uart_suspend_port(&serial_omap_reg, &up->port);
1147 1148 1149
		flush_work_sync(&up->qos_work);
	}

1150 1151 1152
	return 0;
}

1153
static int serial_omap_resume(struct device *dev)
1154
{
1155
	struct uart_omap_port *up = dev_get_drvdata(dev);
1156 1157 1158 1159 1160

	if (up)
		uart_resume_port(&serial_omap_reg, &up->port);
	return 0;
}
1161
#endif
1162

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
{
	u32 mvr, scheme;
	u16 revision, major, minor;

	mvr = serial_in(up, UART_OMAP_MVER);

	/* Check revision register scheme */
	scheme = mvr >> OMAP_UART_MVR_SCHEME_SHIFT;

	switch (scheme) {
	case 0: /* Legacy Scheme: OMAP2/3 */
		/* MINOR_REV[0:4], MAJOR_REV[4:7] */
		major = (mvr & OMAP_UART_LEGACY_MVR_MAJ_MASK) >>
					OMAP_UART_LEGACY_MVR_MAJ_SHIFT;
		minor = (mvr & OMAP_UART_LEGACY_MVR_MIN_MASK);
		break;
	case 1:
		/* New Scheme: OMAP4+ */
		/* MINOR_REV[0:5], MAJOR_REV[8:10] */
		major = (mvr & OMAP_UART_MVR_MAJ_MASK) >>
					OMAP_UART_MVR_MAJ_SHIFT;
		minor = (mvr & OMAP_UART_MVR_MIN_MASK);
		break;
	default:
1188
		dev_warn(up->dev,
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
			"Unknown %s revision, defaulting to highest\n",
			up->name);
		/* highest possible revision */
		major = 0xff;
		minor = 0xff;
	}

	/* normalize revision for the driver */
	revision = UART_BUILD_REVISION(major, minor);

	switch (revision) {
	case OMAP_UART_REV_46:
		up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
				UART_ERRATA_i291_DMA_FORCEIDLE);
		break;
	case OMAP_UART_REV_52:
		up->errata |= (UART_ERRATA_i202_MDR1_ACCESS |
				UART_ERRATA_i291_DMA_FORCEIDLE);
		break;
	case OMAP_UART_REV_63:
		up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
		break;
	default:
		break;
	}
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
{
	struct omap_uart_port_info *omap_up_info;

	omap_up_info = devm_kzalloc(dev, sizeof(*omap_up_info), GFP_KERNEL);
	if (!omap_up_info)
		return NULL; /* out of memory */

	of_property_read_u32(dev->of_node, "clock-frequency",
					 &omap_up_info->uartclk);
	return omap_up_info;
}

1229 1230 1231
static int serial_omap_probe(struct platform_device *pdev)
{
	struct uart_omap_port	*up;
F
Felipe Balbi 已提交
1232
	struct resource		*mem, *irq;
1233
	struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1234
	int ret;
1235

1236 1237 1238
	if (pdev->dev.of_node)
		omap_up_info = of_get_uart_port_info(&pdev->dev);

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem) {
		dev_err(&pdev->dev, "no mem resource?\n");
		return -ENODEV;
	}

	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!irq) {
		dev_err(&pdev->dev, "no irq resource?\n");
		return -ENODEV;
	}

1251
	if (!devm_request_mem_region(&pdev->dev, mem->start, resource_size(mem),
1252
				pdev->dev.driver->name)) {
1253 1254 1255 1256
		dev_err(&pdev->dev, "memory region already claimed\n");
		return -EBUSY;
	}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	if (gpio_is_valid(omap_up_info->DTR_gpio) &&
	    omap_up_info->DTR_present) {
		ret = gpio_request(omap_up_info->DTR_gpio, "omap-serial");
		if (ret < 0)
			return ret;
		ret = gpio_direction_output(omap_up_info->DTR_gpio,
					    omap_up_info->DTR_inverted);
		if (ret < 0)
			return ret;
	}

1268 1269 1270
	up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
	if (!up)
		return -ENOMEM;
1271

1272 1273 1274 1275 1276 1277 1278 1279
	if (gpio_is_valid(omap_up_info->DTR_gpio) &&
	    omap_up_info->DTR_present) {
		up->DTR_gpio = omap_up_info->DTR_gpio;
		up->DTR_inverted = omap_up_info->DTR_inverted;
	} else
		up->DTR_gpio = -EINVAL;
	up->DTR_active = 0;

1280
	up->dev = &pdev->dev;
1281 1282 1283 1284 1285 1286 1287 1288 1289
	up->port.dev = &pdev->dev;
	up->port.type = PORT_OMAP;
	up->port.iotype = UPIO_MEM;
	up->port.irq = irq->start;

	up->port.regshift = 2;
	up->port.fifosize = 64;
	up->port.ops = &serial_omap_pops;

1290 1291 1292 1293 1294 1295 1296 1297 1298
	if (pdev->dev.of_node)
		up->port.line = of_alias_get_id(pdev->dev.of_node, "serial");
	else
		up->port.line = pdev->id;

	if (up->port.line < 0) {
		dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
								up->port.line);
		ret = -ENODEV;
1299
		goto err_port_line;
1300 1301 1302
	}

	sprintf(up->name, "OMAP UART%d", up->port.line);
1303
	up->port.mapbase = mem->start;
1304 1305
	up->port.membase = devm_ioremap(&pdev->dev, mem->start,
						resource_size(mem));
1306 1307 1308
	if (!up->port.membase) {
		dev_err(&pdev->dev, "can't ioremap UART\n");
		ret = -ENOMEM;
1309
		goto err_ioremap;
1310 1311
	}

1312 1313
	up->port.flags = omap_up_info->flags;
	up->port.uartclk = omap_up_info->uartclk;
1314 1315 1316 1317 1318
	if (!up->port.uartclk) {
		up->port.uartclk = DEFAULT_CLK_SPEED;
		dev_warn(&pdev->dev, "No clock speed specified: using default:"
						"%d\n", DEFAULT_CLK_SPEED);
	}
1319

1320 1321 1322 1323 1324 1325 1326
	up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
	up->calc_latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
	pm_qos_add_request(&up->pm_qos_request,
		PM_QOS_CPU_DMA_LATENCY, up->latency);
	serial_omap_uart_wq = create_singlethread_workqueue(up->name);
	INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);

1327 1328
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_set_autosuspend_delay(&pdev->dev,
1329
			omap_up_info->autosuspend_timeout);
1330 1331 1332 1333 1334

	pm_runtime_irq_safe(&pdev->dev);
	pm_runtime_enable(&pdev->dev);
	pm_runtime_get_sync(&pdev->dev);

1335 1336
	omap_serial_fill_features_erratas(up);

1337
	ui[up->port.line] = up;
1338 1339 1340 1341
	serial_omap_add_console_port(up);

	ret = uart_add_one_port(&serial_omap_reg, &up->port);
	if (ret != 0)
1342
		goto err_add_port;
1343

1344
	pm_runtime_put(&pdev->dev);
1345 1346
	platform_set_drvdata(pdev, up);
	return 0;
1347 1348 1349 1350 1351 1352

err_add_port:
	pm_runtime_put(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
err_ioremap:
err_port_line:
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
	dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
				pdev->id, __func__, ret);
	return ret;
}

static int serial_omap_remove(struct platform_device *dev)
{
	struct uart_omap_port *up = platform_get_drvdata(dev);

	if (up) {
1363
		pm_runtime_disable(up->dev);
1364
		uart_remove_one_port(&serial_omap_reg, &up->port);
1365
		pm_qos_remove_request(&up->pm_qos_request);
1366
	}
1367 1368 1369 1370 1371

	platform_set_drvdata(dev, NULL);
	return 0;
}

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
/*
 * Work Around for Errata i202 (2430, 3430, 3630, 4430 and 4460)
 * The access to uart register after MDR1 Access
 * causes UART to corrupt data.
 *
 * Need a delay =
 * 5 L4 clock cycles + 5 UART functional clock cycle (@48MHz = ~0.2uS)
 * give 10 times as much
 */
static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1)
{
	u8 timeout = 255;

	serial_out(up, UART_OMAP_MDR1, mdr1);
	udelay(2);
	serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_XMIT |
			UART_FCR_CLEAR_RCVR);
	/*
	 * Wait for FIFO to empty: when empty, RX_FIFO_E bit is 0 and
	 * TX_FIFO_E bit is 1.
	 */
	while (UART_LSR_THRE != (serial_in(up, UART_LSR) &
				(UART_LSR_THRE | UART_LSR_DR))) {
		timeout--;
		if (!timeout) {
			/* Should *never* happen. we warn and carry on */
1398
			dev_crit(up->dev, "Errata i202: timedout %x\n",
1399 1400 1401 1402 1403 1404 1405
						serial_in(up, UART_LSR));
			break;
		}
		udelay(1);
	}
}

1406
#ifdef CONFIG_PM_RUNTIME
1407 1408
static void serial_omap_restore_context(struct uart_omap_port *up)
{
1409 1410 1411 1412 1413
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, UART_OMAP_MDR1_DISABLE);
	else
		serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);

1414 1415 1416 1417 1418
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
	serial_out(up, UART_EFR, UART_EFR_ECB);
	serial_out(up, UART_LCR, 0x0); /* Operational mode */
	serial_out(up, UART_IER, 0x0);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1419 1420
	serial_out(up, UART_DLL, up->dll);
	serial_out(up, UART_DLM, up->dlh);
1421 1422 1423 1424 1425 1426
	serial_out(up, UART_LCR, 0x0); /* Operational mode */
	serial_out(up, UART_IER, up->ier);
	serial_out(up, UART_FCR, up->fcr);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
	serial_out(up, UART_MCR, up->mcr);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); /* Config B mode */
1427
	serial_out(up, UART_OMAP_SCR, up->scr);
1428 1429
	serial_out(up, UART_EFR, up->efr);
	serial_out(up, UART_LCR, up->lcr);
1430 1431 1432 1433
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, up->mdr1);
	else
		serial_out(up, UART_OMAP_MDR1, up->mdr1);
1434 1435
}

1436 1437
static int serial_omap_runtime_suspend(struct device *dev)
{
1438 1439 1440 1441 1442 1443
	struct uart_omap_port *up = dev_get_drvdata(dev);
	struct omap_uart_port_info *pdata = dev->platform_data;

	if (!up)
		return -EINVAL;

1444
	if (!pdata)
1445 1446
		return 0;

1447
	up->context_loss_cnt = serial_omap_get_context_loss_count(up);
1448

1449 1450
	if (device_may_wakeup(dev)) {
		if (!up->wakeups_enabled) {
1451
			serial_omap_enable_wakeup(up, true);
1452 1453 1454 1455
			up->wakeups_enabled = true;
		}
	} else {
		if (up->wakeups_enabled) {
1456
			serial_omap_enable_wakeup(up, false);
1457 1458 1459 1460
			up->wakeups_enabled = false;
		}
	}

1461 1462 1463
	up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
	schedule_work(&up->qos_work);

1464 1465 1466
	return 0;
}

1467 1468
static int serial_omap_runtime_resume(struct device *dev)
{
1469
	struct uart_omap_port *up = dev_get_drvdata(dev);
1470
	struct omap_uart_port_info *pdata = dev->platform_data;
1471

1472
	if (up && pdata) {
1473
			u32 loss_cnt = serial_omap_get_context_loss_count(up);
1474 1475 1476

			if (up->context_loss_cnt != loss_cnt)
				serial_omap_restore_context(up);
1477

1478 1479
		up->latency = up->calc_latency;
		schedule_work(&up->qos_work);
1480
	}
1481

1482 1483
	return 0;
}
1484 1485 1486 1487 1488 1489 1490 1491
#endif

static const struct dev_pm_ops serial_omap_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(serial_omap_suspend, serial_omap_resume)
	SET_RUNTIME_PM_OPS(serial_omap_runtime_suspend,
				serial_omap_runtime_resume, NULL)
};

1492 1493 1494 1495 1496 1497 1498 1499 1500
#if defined(CONFIG_OF)
static const struct of_device_id omap_serial_of_match[] = {
	{ .compatible = "ti,omap2-uart" },
	{ .compatible = "ti,omap3-uart" },
	{ .compatible = "ti,omap4-uart" },
	{},
};
MODULE_DEVICE_TABLE(of, omap_serial_of_match);
#endif
1501 1502 1503 1504 1505 1506

static struct platform_driver serial_omap_driver = {
	.probe          = serial_omap_probe,
	.remove         = serial_omap_remove,
	.driver		= {
		.name	= DRIVER_NAME,
1507
		.pm	= &serial_omap_dev_pm_ops,
1508
		.of_match_table = of_match_ptr(omap_serial_of_match),
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
	},
};

static int __init serial_omap_init(void)
{
	int ret;

	ret = uart_register_driver(&serial_omap_reg);
	if (ret != 0)
		return ret;
	ret = platform_driver_register(&serial_omap_driver);
	if (ret != 0)
		uart_unregister_driver(&serial_omap_reg);
	return ret;
}

static void __exit serial_omap_exit(void)
{
	platform_driver_unregister(&serial_omap_driver);
	uart_unregister_driver(&serial_omap_reg);
}

module_init(serial_omap_init);
module_exit(serial_omap_exit);

MODULE_DESCRIPTION("OMAP High Speed UART driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Texas Instruments Inc");