omap-serial.c 50.0 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
#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>
35
#include <linux/platform_device.h>
36 37 38 39
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/serial_core.h>
#include <linux/irq.h>
40
#include <linux/pm_runtime.h>
41
#include <linux/pm_wakeirq.h>
42
#include <linux/of.h>
43
#include <linux/of_irq.h>
44
#include <linux/gpio.h>
M
Mark Jackson 已提交
45
#include <linux/of_gpio.h>
46
#include <linux/platform_data/serial-omap.h>
47

M
Mark Jackson 已提交
48 49
#include <dt-bindings/gpio/gpio.h>

50
#define OMAP_MAX_HSUART_PORTS	10
51

52 53 54 55 56 57 58
#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

59 60 61 62 63
#define OMAP_UART_TX_WAKEUP_EN		BIT(7)

/* Feature flags */
#define OMAP_UART_WER_HAS_TX_WAKEUP	BIT(0)

64 65 66
#define UART_ERRATA_i202_MDR1_ACCESS	BIT(0)
#define UART_ERRATA_i291_DMA_FORCEIDLE	BIT(1)

P
Pavel Machek 已提交
67
#define DEFAULT_CLK_SPEED 48000000 /* 48Mhz */
68

69 70
/* SCR register bitmasks */
#define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK		(1 << 7)
71
#define OMAP_UART_SCR_TX_TRIG_GRANU1_MASK		(1 << 6)
72
#define OMAP_UART_SCR_TX_EMPTY			(1 << 3)
73 74 75

/* FCR register bitmasks */
#define OMAP_UART_FCR_RX_FIFO_TRIG_MASK			(0x3 << 6)
76
#define OMAP_UART_FCR_TX_FIFO_TRIG_MASK			(0x3 << 4)
77

78 79 80 81 82 83 84 85 86 87 88
/* 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

89 90 91 92 93 94 95 96
#define OMAP_UART_DMA_CH_FREE	-1

#define MSR_SAVE_FLAGS		UART_MSR_ANY_DELTA
#define OMAP_MODE13X_SPEED	230400

/* WER = 0x7F
 * Enable module level wakeup in WER reg
 */
P
Pavel Machek 已提交
97
#define OMAP_UART_WER_MOD_WKUP	0x7F
98 99

/* Enable XON/XOFF flow control on output */
100
#define OMAP_UART_SW_TX		0x08
101 102

/* Enable XON/XOFF flow control on input */
103
#define OMAP_UART_SW_RX		0x02
104 105 106 107 108 109 110 111 112 113 114 115 116 117

#define OMAP_UART_SW_CLR	0xF0

#define OMAP_UART_TCR_TRIG	0x0F

struct uart_omap_dma {
	u8			uart_dma_tx;
	u8			uart_dma_rx;
	int			rx_dma_channel;
	int			tx_dma_channel;
	dma_addr_t		rx_buf_dma_phys;
	dma_addr_t		tx_buf_dma_phys;
	unsigned int		uart_base;
	/*
P
Pavel Machek 已提交
118
	 * Buffer for rx dma. It is not required for tx because the buffer
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	 * comes from port structure.
	 */
	unsigned char		*rx_buf;
	unsigned int		prev_rx_dma_pos;
	int			tx_buf_size;
	int			tx_dma_used;
	int			rx_dma_used;
	spinlock_t		tx_lock;
	spinlock_t		rx_lock;
	/* timer to poll activity on rx dma */
	struct timer_list	rx_timer;
	unsigned int		rx_buf_size;
	unsigned int		rx_poll_rate;
	unsigned int		rx_timeout;
};

135 136 137 138
struct uart_omap_port {
	struct uart_port	port;
	struct uart_omap_dma	uart_dma;
	struct device		*dev;
139
	int			wakeirq;
140 141 142 143 144 145 146 147 148 149

	unsigned char		ier;
	unsigned char		lcr;
	unsigned char		mcr;
	unsigned char		fcr;
	unsigned char		efr;
	unsigned char		dll;
	unsigned char		dlh;
	unsigned char		mdr1;
	unsigned char		scr;
150
	unsigned char		wer;
151 152 153 154

	int			use_dma;
	/*
	 * Some bits in registers are cleared on a read, so they must
P
Pavel Machek 已提交
155
	 * be saved whenever the register is read, but the bits will not
156 157 158 159 160 161
	 * be immediately processed.
	 */
	unsigned int		lsr_break_flag;
	unsigned char		msr_saved_flags;
	char			name[20];
	unsigned long		port_activity;
162
	int			context_loss_cnt;
163
	u32			errata;
164
	u32			features;
165

M
Mark Jackson 已提交
166 167
	int			rts_gpio;

168 169 170 171
	struct pm_qos_request	pm_qos_request;
	u32			latency;
	u32			calc_latency;
	struct work_struct	qos_work;
172
	bool			is_suspending;
173 174
};

175
#define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port)))
176

177 178 179
static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];

/* Forward declaration of functions */
180
static void serial_omap_mdr1_errataset(struct uart_omap_port *up, u8 mdr1);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

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);
}

202
#ifdef CONFIG_PM
203 204
static int serial_omap_get_context_loss_count(struct uart_omap_port *up)
{
J
Jingoo Han 已提交
205
	struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
206

F
Felipe Balbi 已提交
207
	if (!pdata || !pdata->get_context_loss_count)
208
		return -EINVAL;
209

210
	return pdata->get_context_loss_count(up->dev);
211 212
}

213
/* REVISIT: Remove this when omap3 boots in device tree only mode */
214 215
static void serial_omap_enable_wakeup(struct uart_omap_port *up, bool enable)
{
J
Jingoo Han 已提交
216
	struct omap_uart_port_info *pdata = dev_get_platdata(up->dev);
217

F
Felipe Balbi 已提交
218 219 220 221
	if (!pdata || !pdata->enable_wakeup)
		return;

	pdata->enable_wakeup(up->dev, enable);
222
}
223
#endif /* CONFIG_PM */
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
/*
 * Calculate the absolute difference between the desired and actual baud
 * rate for the given mode.
 */
static inline int calculate_baud_abs_diff(struct uart_port *port,
				unsigned int baud, unsigned int mode)
{
	unsigned int n = port->uartclk / (mode * baud);
	int abs_diff;

	if (n == 0)
		n = 1;

	abs_diff = baud - (port->uartclk / (mode * n));
	if (abs_diff < 0)
		abs_diff = -abs_diff;

	return abs_diff;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258
/*
 * serial_omap_baud_is_mode16 - check if baud rate is MODE16X
 * @port: uart port info
 * @baud: baudrate for which mode needs to be determined
 *
 * Returns true if baud rate is MODE16X and false if MODE13X
 * Original table in OMAP TRM named "UART Mode Baud Rates, Divisor Values,
 * and Error Rates" determines modes not for all common baud rates.
 * E.g. for 1000000 baud rate mode must be 16x, but according to that
 * table it's determined as 13x.
 */
static bool
serial_omap_baud_is_mode16(struct uart_port *port, unsigned int baud)
{
259 260 261 262
	int abs_diff_13 = calculate_baud_abs_diff(port, baud, 13);
	int abs_diff_16 = calculate_baud_abs_diff(port, baud, 16);

	return (abs_diff_13 >= abs_diff_16);
263 264
}

265 266 267 268 269 270 271 272
/*
 * serial_omap_get_divisor - calculate divisor value
 * @port: uart port info
 * @baud: baudrate for which divisor needs to be calculated.
 */
static unsigned int
serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
{
273
	unsigned int mode;
274

275
	if (!serial_omap_baud_is_mode16(port, baud))
276
		mode = 13;
277
	else
278 279
		mode = 16;
	return port->uartclk/(mode * baud);
280 281 282 283
}

static void serial_omap_enable_ms(struct uart_port *port)
{
284
	struct uart_omap_port *up = to_uart_omap_port(port);
285

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

288
	pm_runtime_get_sync(up->dev);
289 290
	up->ier |= UART_IER_MSI;
	serial_out(up, UART_IER, up->ier);
291 292
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
293 294 295 296
}

static void serial_omap_stop_tx(struct uart_port *port)
{
297
	struct uart_omap_port *up = to_uart_omap_port(port);
M
Mark Jackson 已提交
298
	int res;
299

300
	pm_runtime_get_sync(up->dev);
M
Mark Jackson 已提交
301

302
	/* Handle RS-485 */
303
	if (port->rs485.flags & SER_RS485_ENABLED) {
304 305 306 307 308 309 310 311 312 313
		if (up->scr & OMAP_UART_SCR_TX_EMPTY) {
			/* THR interrupt is fired when both TX FIFO and TX
			 * shift register are empty. This means there's nothing
			 * left to transmit now, so make sure the THR interrupt
			 * is fired when TX FIFO is below the trigger level,
			 * disable THR interrupts and toggle the RS-485 GPIO
			 * data direction pin if needed.
			 */
			up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
			serial_out(up, UART_OMAP_SCR, up->scr);
314 315
			res = (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) ?
				1 : 0;
M
Mark Jackson 已提交
316
			if (gpio_get_value(up->rts_gpio) != res) {
317 318 319
				if (port->rs485.delay_rts_after_send > 0)
					mdelay(
					port->rs485.delay_rts_after_send);
M
Mark Jackson 已提交
320 321
				gpio_set_value(up->rts_gpio, res);
			}
322 323 324 325 326 327 328 329 330 331 332 333
		} else {
			/* We're asked to stop, but there's still stuff in the
			 * UART FIFO, so make sure the THR interrupt is fired
			 * when both TX FIFO and TX shift register are empty.
			 * The next THR interrupt (if no transmission is started
			 * in the meantime) will indicate the end of a
			 * transmission. Therefore we _don't_ disable THR
			 * interrupts in this situation.
			 */
			up->scr |= OMAP_UART_SCR_TX_EMPTY;
			serial_out(up, UART_OMAP_SCR, up->scr);
			return;
M
Mark Jackson 已提交
334 335 336
		}
	}

337 338 339 340
	if (up->ier & UART_IER_THRI) {
		up->ier &= ~UART_IER_THRI;
		serial_out(up, UART_IER, up->ier);
	}
341

342 343
	if ((port->rs485.flags & SER_RS485_ENABLED) &&
	    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
344 345 346 347 348 349
		/*
		 * Empty the RX FIFO, we are not interested in anything
		 * received during the half-duplex transmission.
		 */
		serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_RCVR);
		/* Re-enable RX interrupts */
350 351
		up->ier |= UART_IER_RLSI | UART_IER_RDI;
		up->port.read_status_mask |= UART_LSR_DR;
M
Mark Jackson 已提交
352 353 354
		serial_out(up, UART_IER, up->ier);
	}

355 356
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
357 358 359 360
}

static void serial_omap_stop_rx(struct uart_port *port)
{
361
	struct uart_omap_port *up = to_uart_omap_port(port);
362

363
	pm_runtime_get_sync(up->dev);
364
	up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
365 366
	up->port.read_status_mask &= ~UART_LSR_DR;
	serial_out(up, UART_IER, up->ier);
367 368
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
369 370
}

371
static void transmit_chars(struct uart_omap_port *up, unsigned int lsr)
372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
	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;
	}
386
	count = up->port.fifosize / 4;
387 388 389 390 391 392 393 394
	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);

395
	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		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)
{
412
	struct uart_omap_port *up = to_uart_omap_port(port);
M
Mark Jackson 已提交
413
	int res;
414

F
Felipe Balbi 已提交
415
	pm_runtime_get_sync(up->dev);
M
Mark Jackson 已提交
416

417
	/* Handle RS-485 */
418
	if (port->rs485.flags & SER_RS485_ENABLED) {
419 420 421 422
		/* Fire THR interrupts when FIFO is below trigger level */
		up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
		serial_out(up, UART_OMAP_SCR, up->scr);

M
Mark Jackson 已提交
423
		/* if rts not already enabled */
424
		res = (port->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
M
Mark Jackson 已提交
425 426
		if (gpio_get_value(up->rts_gpio) != res) {
			gpio_set_value(up->rts_gpio, res);
427 428
			if (port->rs485.delay_rts_before_send > 0)
				mdelay(port->rs485.delay_rts_before_send);
M
Mark Jackson 已提交
429 430 431
		}
	}

432 433
	if ((port->rs485.flags & SER_RS485_ENABLED) &&
	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
M
Mark Jackson 已提交
434 435
		serial_omap_stop_rx(port);

F
Felipe Balbi 已提交
436 437 438
	serial_omap_enable_ier_thri(up);
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
439 440
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static void serial_omap_throttle(struct uart_port *port)
{
	struct uart_omap_port *up = to_uart_omap_port(port);
	unsigned long flags;

	pm_runtime_get_sync(up->dev);
	spin_lock_irqsave(&up->port.lock, flags);
	up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
	serial_out(up, UART_IER, up->ier);
	spin_unlock_irqrestore(&up->port.lock, flags);
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
}

static void serial_omap_unthrottle(struct uart_port *port)
{
	struct uart_omap_port *up = to_uart_omap_port(port);
	unsigned long flags;

	pm_runtime_get_sync(up->dev);
	spin_lock_irqsave(&up->port.lock, flags);
	up->ier |= UART_IER_RLSI | UART_IER_RDI;
	serial_out(up, UART_IER, up->ier);
	spin_unlock_irqrestore(&up->port.lock, flags);
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
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;
}

497 498 499
static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr)
{
	unsigned int flag;
500 501 502 503
	unsigned char ch = 0;

	if (likely(lsr & UART_LSR_DR))
		ch = serial_in(up, UART_RX);
504 505 506 507 508 509 510 511 512 513 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 552 553 554 555 556 557 558 559 560 561 562

	up->port.icount.rx++;
	flag = TTY_NORMAL;

	if (lsr & UART_LSR_BI) {
		flag = TTY_BREAK;
		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))
			return;

	}

	if (lsr & UART_LSR_PE) {
		flag = TTY_PARITY;
		up->port.icount.parity++;
	}

	if (lsr & UART_LSR_FE) {
		flag = TTY_FRAME;
		up->port.icount.frame++;
	}

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

#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
	uart_insert_char(&up->port, lsr, UART_LSR_OE, 0, flag);
}

static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr)
{
	unsigned char ch = 0;
	unsigned int flag;

	if (!(lsr & UART_LSR_DR))
		return;

	ch = serial_in(up, UART_RX);
	flag = TTY_NORMAL;
	up->port.icount.rx++;

	if (uart_handle_sysrq_char(&up->port, ch))
		return;

	uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
}

563 564 565 566 567
/**
 * serial_omap_irq() - This handles the interrupt from one port
 * @irq: uart port irq number
 * @dev_id: uart port info
 */
568
static irqreturn_t serial_omap_irq(int irq, void *dev_id)
569 570 571
{
	struct uart_omap_port *up = dev_id;
	unsigned int iir, lsr;
572
	unsigned int type;
573
	irqreturn_t ret = IRQ_NONE;
574
	int max_count = 256;
575

576
	spin_lock(&up->port.lock);
577
	pm_runtime_get_sync(up->dev);
578 579

	do {
580
		iir = serial_in(up, UART_IIR);
581 582 583
		if (iir & UART_IIR_NO_INT)
			break;

584
		ret = IRQ_HANDLED;
585 586 587 588 589 590 591 592 593 594
		lsr = serial_in(up, UART_LSR);

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

		switch (type) {
		case UART_IIR_MSI:
			check_modem_status(up);
			break;
		case UART_IIR_THRI:
595
			transmit_chars(up, lsr);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
			break;
		case UART_IIR_RX_TIMEOUT:
			/* FALLTHROUGH */
		case UART_IIR_RDI:
			serial_omap_rdi(up, lsr);
			break;
		case UART_IIR_RLSI:
			serial_omap_rlsi(up, lsr);
			break;
		case UART_IIR_CTS_RTS_DSR:
			/* simply try again */
			break;
		case UART_IIR_XOFF:
			/* FALLTHROUGH */
		default:
			break;
		}
	} while (!(iir & UART_IIR_NO_INT) && max_count--);
614

615
	spin_unlock(&up->port.lock);
616

J
Jiri Slaby 已提交
617
	tty_flip_buffer_push(&up->port.state->port);
618

619 620
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
621
	up->port_activity = jiffies;
622

623
	return ret;
624 625 626 627
}

static unsigned int serial_omap_tx_empty(struct uart_port *port)
{
628
	struct uart_omap_port *up = to_uart_omap_port(port);
629 630 631
	unsigned long flags = 0;
	unsigned int ret = 0;

632
	pm_runtime_get_sync(up->dev);
633
	dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->port.line);
634 635 636
	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);
637 638
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
639 640 641 642 643
	return ret;
}

static unsigned int serial_omap_get_mctrl(struct uart_port *port)
{
644
	struct uart_omap_port *up = to_uart_omap_port(port);
645
	unsigned int status;
646 647
	unsigned int ret = 0;

648
	pm_runtime_get_sync(up->dev);
649
	status = check_modem_status(up);
650 651
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
652

653
	dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->port.line);
654 655 656 657 658 659 660 661 662 663 664 665 666 667

	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)
{
668
	struct uart_omap_port *up = to_uart_omap_port(port);
P
Peter Hurley 已提交
669
	unsigned char mcr = 0, old_mcr, lcr;
670

671
	dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->port.line);
672 673 674 675 676 677 678 679 680 681 682
	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;

683
	pm_runtime_get_sync(up->dev);
684 685 686 687
	old_mcr = serial_in(up, UART_MCR);
	old_mcr &= ~(UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_OUT1 |
		     UART_MCR_DTR | UART_MCR_RTS);
	up->mcr = old_mcr | mcr;
688
	serial_out(up, UART_MCR, up->mcr);
P
Peter Hurley 已提交
689 690 691 692 693 694 695 696 697 698 699

	/* Turn off autoRTS if RTS is lowered; restore autoRTS if RTS raised */
	lcr = serial_in(up, UART_LCR);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
		up->efr |= UART_EFR_RTS;
	else
		up->efr &= UART_EFR_RTS;
	serial_out(up, UART_EFR, up->efr);
	serial_out(up, UART_LCR, lcr);

700 701
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
702 703 704 705
}

static void serial_omap_break_ctl(struct uart_port *port, int break_state)
{
706
	struct uart_omap_port *up = to_uart_omap_port(port);
707 708
	unsigned long flags = 0;

709
	dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->port.line);
710
	pm_runtime_get_sync(up->dev);
711 712 713 714 715 716 717
	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);
718 719
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
720 721 722 723
}

static int serial_omap_startup(struct uart_port *port)
{
724
	struct uart_omap_port *up = to_uart_omap_port(port);
725 726 727 728 729 730 731 732 733 734 735
	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;

736 737
	/* Optional wake-up IRQ */
	if (up->wakeirq) {
738
		retval = dev_pm_set_dedicated_wake_irq(up->dev, up->wakeirq);
739 740 741 742 743 744
		if (retval) {
			free_irq(up->port.irq, up);
			return retval;
		}
	}

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

747
	pm_runtime_get_sync(up->dev);
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	/*
	 * Clear the FIFO buffers and disable them.
	 * (they will be reenabled in set_termios())
	 */
	serial_omap_clear_fifos(up);

	/*
	 * 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);

784
	/* Enable module level wake up */
785 786 787 788 789
	up->wer = OMAP_UART_WER_MOD_WKUP;
	if (up->features & OMAP_UART_WER_HAS_TX_WAKEUP)
		up->wer |= OMAP_UART_TX_WAKEUP_EN;

	serial_out(up, UART_OMAP_WER, up->wer);
790

791 792
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
793 794 795 796 797 798
	up->port_activity = jiffies;
	return 0;
}

static void serial_omap_shutdown(struct uart_port *port)
{
799
	struct uart_omap_port *up = to_uart_omap_port(port);
800 801
	unsigned long flags = 0;

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

804
	pm_runtime_get_sync(up->dev);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	/*
	 * 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);
827

828 829
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
830
	free_irq(up->port.irq, up);
831
	dev_pm_clear_wake_irq(up->dev);
832 833
}

834 835 836 837 838 839 840 841
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);
}

842 843 844 845
static void
serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
			struct ktermios *old)
{
846
	struct uart_omap_port *up = to_uart_omap_port(port);
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	unsigned char cval = 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;
873 874
	if (termios->c_cflag & CMSPAR)
		cval |= UART_LCR_SPAR;
875 876 877 878 879 880 881 882

	/*
	 * 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);

883
	/* calculate wakeup latency constraint */
884
	up->calc_latency = (USEC_PER_SEC * up->port.fifosize) / (baud / 8);
885 886 887
	up->latency = up->calc_latency;
	schedule_work(&up->qos_work);

888 889 890 891
	up->dll = quot & 0xff;
	up->dlh = quot >> 8;
	up->mdr1 = UART_OMAP_MDR1_DISABLE;

892 893 894 895 896 897 898
	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.
	 */
899
	pm_runtime_get_sync(up->dev);
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
	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 */
943
	up->lcr = cval;
944
	up->scr = 0;
945 946 947 948 949 950 951

	/* FIFOs and DMA Settings */

	/* FCR can be changed only when the
	 * baud clock is not running
	 * DLL_REG and DLH_REG set to 0.
	 */
952
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
953 954 955 956
	serial_out(up, UART_DLL, 0);
	serial_out(up, UART_DLM, 0);
	serial_out(up, UART_LCR, 0);

957
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
958

959
	up->efr = serial_in(up, UART_EFR) & ~UART_EFR_ECB;
960
	up->efr &= ~UART_EFR_SCD;
961 962
	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);

963
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
964
	up->mcr = serial_in(up, UART_MCR) & ~UART_MCR_TCRTLR;
965 966
	serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
	/* FIFO ENABLE, DMA MODE */
967

968 969 970 971 972 973 974 975 976 977 978
	up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
	/*
	 * NOTE: Setting OMAP_UART_SCR_RX_TRIG_GRANU1_MASK
	 * sets Enables the granularity of 1 for TRIGGER RX
	 * level. Along with setting RX FIFO trigger level
	 * to 1 (as noted below, 16 characters) and TLR[3:0]
	 * to zero this will result RX FIFO threshold level
	 * to 1 character, instead of 16 as noted in comment
	 * below.
	 */

979
	/* Set receive FIFO threshold to 16 characters and
980
	 * transmit FIFO threshold to 32 spaces
981
	 */
F
Felipe Balbi 已提交
982
	up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
983 984 985
	up->fcr &= ~OMAP_UART_FCR_TX_FIFO_TRIG_MASK;
	up->fcr |= UART_FCR6_R_TRIGGER_16 | UART_FCR6_T_TRIGGER_24 |
		UART_FCR_ENABLE_FIFO;
986

987 988 989
	serial_out(up, UART_FCR, up->fcr);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);

990 991
	serial_out(up, UART_OMAP_SCR, up->scr);

992
	/* Reset UART_MCR_TCRTLR: this must be done with the EFR_ECB bit set */
993
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
994
	serial_out(up, UART_MCR, up->mcr);
995 996 997
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
	serial_out(up, UART_EFR, up->efr);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
998 999 1000

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

1001 1002 1003 1004 1005
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, up->mdr1);
	else
		serial_out(up, UART_OMAP_MDR1, up->mdr1);

1006
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1007 1008 1009 1010
	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);

	serial_out(up, UART_LCR, 0);
	serial_out(up, UART_IER, 0);
1011
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1012

1013 1014
	serial_out(up, UART_DLL, up->dll);	/* LS of divisor */
	serial_out(up, UART_DLM, up->dlh);	/* MS of divisor */
1015 1016 1017

	serial_out(up, UART_LCR, 0);
	serial_out(up, UART_IER, up->ier);
1018
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1019 1020 1021 1022

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

1023
	if (!serial_omap_baud_is_mode16(port, baud))
1024
		up->mdr1 = UART_OMAP_MDR1_13X_MODE;
1025
	else
1026 1027
		up->mdr1 = UART_OMAP_MDR1_16X_MODE;

1028 1029 1030 1031
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, up->mdr1);
	else
		serial_out(up, UART_OMAP_MDR1, up->mdr1);
1032

1033
	/* Configure flow control */
R
Russell King 已提交
1034
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1035 1036 1037 1038 1039 1040

	/* XON1/XOFF1 accessible mode B, TCRTLR=0, ECB=0 */
	serial_out(up, UART_XON1, termios->c_cc[VSTART]);
	serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);

	/* Enable access to TCR/TLR */
R
Russell King 已提交
1041 1042 1043
	serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
	serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
1044

R
Russell King 已提交
1045
	serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
1046

1047 1048
	up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);

R
Russell King 已提交
1049
	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
P
Peter Hurley 已提交
1050
		/* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
1051
		up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
P
Peter Hurley 已提交
1052
		up->efr |= UART_EFR_CTS;
1053 1054 1055
	} else {
		/* Disable AUTORTS and AUTOCTS */
		up->efr &= ~(UART_EFR_CTS | UART_EFR_RTS);
1056 1057
	}

1058 1059 1060
	if (up->port.flags & UPF_SOFT_FLOW) {
		/* clear SW control mode bits */
		up->efr &= OMAP_UART_SW_CLR;
1061

1062 1063
		/*
		 * IXON Flag:
1064 1065
		 * Enable XON/XOFF flow control on input.
		 * Receiver compares XON1, XOFF1.
1066 1067
		 */
		if (termios->c_iflag & IXON)
1068
			up->efr |= OMAP_UART_SW_RX;
1069

1070 1071
		/*
		 * IXOFF Flag:
1072 1073
		 * Enable XON/XOFF flow control on output.
		 * Transmit XON1, XOFF1
1074
		 */
1075 1076
		if (termios->c_iflag & IXOFF) {
			up->port.status |= UPSTAT_AUTOXOFF;
1077
			up->efr |= OMAP_UART_SW_TX;
1078
		}
1079

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
		/*
		 * 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;
		else
			up->mcr &= ~UART_MCR_XONANY;
1090
	}
R
Russell King 已提交
1091
	serial_out(up, UART_MCR, up->mcr);
R
Russell King 已提交
1092 1093 1094
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
	serial_out(up, UART_EFR, up->efr);
	serial_out(up, UART_LCR, up->lcr);
1095 1096 1097 1098

	serial_omap_set_mctrl(&up->port, up->port.mctrl);

	spin_unlock_irqrestore(&up->port.lock, flags);
1099 1100
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1101
	dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->port.line);
1102 1103 1104 1105 1106 1107
}

static void
serial_omap_pm(struct uart_port *port, unsigned int state,
	       unsigned int oldstate)
{
1108
	struct uart_omap_port *up = to_uart_omap_port(port);
1109 1110
	unsigned char efr;

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

1113
	pm_runtime_get_sync(up->dev);
1114
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1115 1116 1117 1118 1119
	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);
1120
	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1121 1122
	serial_out(up, UART_EFR, efr);
	serial_out(up, UART_LCR, 0);
1123

1124 1125
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
}

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)
{
1141
	struct uart_omap_port *up = to_uart_omap_port(port);
1142 1143

	dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
1144
							up->port.line);
1145
	up->port.type = PORT_OMAP;
1146
	up->port.flags |= UPF_SOFT_FLOW | UPF_HARD_FLOW;
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
}

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)
{
1160
	struct uart_omap_port *up = to_uart_omap_port(port);
1161

1162
	dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->port.line);
1163 1164 1165 1166 1167
	return up->name;
}

#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)

1168
static void __maybe_unused wait_for_xmitr(struct uart_omap_port *up)
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
{
	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);
		}
	}
}

1199 1200 1201 1202
#ifdef CONFIG_CONSOLE_POLL

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

1205
	pm_runtime_get_sync(up->dev);
1206 1207
	wait_for_xmitr(up);
	serial_out(up, UART_TX, ch);
1208 1209
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1210 1211 1212 1213
}

static int serial_omap_poll_get_char(struct uart_port *port)
{
1214
	struct uart_omap_port *up = to_uart_omap_port(port);
1215
	unsigned int status;
1216

1217
	pm_runtime_get_sync(up->dev);
1218
	status = serial_in(up, UART_LSR);
1219 1220 1221 1222
	if (!(status & UART_LSR_DR)) {
		status = NO_POLL_CHAR;
		goto out;
	}
1223

1224
	status = serial_in(up, UART_RX);
1225 1226

out:
1227 1228
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1229

1230
	return status;
1231 1232 1233 1234 1235 1236
}

#endif /* CONFIG_CONSOLE_POLL */

#ifdef CONFIG_SERIAL_OMAP_CONSOLE

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
#ifdef CONFIG_SERIAL_EARLYCON
static unsigned int __init omap_serial_early_in(struct uart_port *port,
						int offset)
{
	offset <<= port->regshift;
	return readw(port->membase + offset);
}

static void __init omap_serial_early_out(struct uart_port *port, int offset,
					 int value)
{
	offset <<= port->regshift;
	writew(value, port->membase + offset);
}

static void __init omap_serial_early_putc(struct uart_port *port, int c)
{
	unsigned int status;

	for (;;) {
		status = omap_serial_early_in(port, UART_LSR);
		if ((status & BOTH_EMPTY) == BOTH_EMPTY)
			break;
		cpu_relax();
	}
	omap_serial_early_out(port, UART_TX, c);
}

static void __init early_omap_serial_write(struct console *console,
					   const char *s, unsigned int count)
{
	struct earlycon_device *device = console->data;
	struct uart_port *port = &device->port;

	uart_console_write(port, s, count, omap_serial_early_putc);
}

static int __init early_omap_serial_setup(struct earlycon_device *device,
					  const char *options)
{
	struct uart_port *port = &device->port;

	if (!(device->port.membase || device->port.iobase))
		return -ENODEV;

	port->regshift = 2;
	device->con->write = early_omap_serial_write;
	return 0;
}

OF_EARLYCON_DECLARE(omapserial, "ti,omap2-uart", early_omap_serial_setup);
OF_EARLYCON_DECLARE(omapserial, "ti,omap3-uart", early_omap_serial_setup);
OF_EARLYCON_DECLARE(omapserial, "ti,omap4-uart", early_omap_serial_setup);
#endif /* CONFIG_SERIAL_EARLYCON */

1292
static struct uart_omap_port *serial_omap_console_ports[OMAP_MAX_HSUART_PORTS];
1293 1294 1295

static struct uart_driver serial_omap_reg;

1296 1297
static void serial_omap_console_putchar(struct uart_port *port, int ch)
{
1298
	struct uart_omap_port *up = to_uart_omap_port(port);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

	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;

1313
	pm_runtime_get_sync(up->dev);
1314

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	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);

1347 1348
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
	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)
{
1385
	serial_omap_console_ports[up->port.line] = up;
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
}

#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

M
Mark Jackson 已提交
1399
/* Enable or disable the rs485 support */
1400
static int
1401
serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485)
M
Mark Jackson 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
{
	struct uart_omap_port *up = to_uart_omap_port(port);
	unsigned int mode;
	int val;

	pm_runtime_get_sync(up->dev);

	/* Disable interrupts from this port */
	mode = up->ier;
	up->ier = 0;
	serial_out(up, UART_IER, 0);

1414 1415 1416 1417
	/* Clamp the delays to [0, 100ms] */
	rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
	rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send, 100U);

M
Mark Jackson 已提交
1418
	/* store new config */
1419
	port->rs485 = *rs485;
M
Mark Jackson 已提交
1420 1421 1422 1423 1424 1425 1426

	/*
	 * Just as a precaution, only allow rs485
	 * to be enabled if the gpio pin is valid
	 */
	if (gpio_is_valid(up->rts_gpio)) {
		/* enable / disable rts */
1427
		val = (port->rs485.flags & SER_RS485_ENABLED) ?
M
Mark Jackson 已提交
1428
			SER_RS485_RTS_AFTER_SEND : SER_RS485_RTS_ON_SEND;
1429
		val = (port->rs485.flags & val) ? 1 : 0;
M
Mark Jackson 已提交
1430 1431
		gpio_set_value(up->rts_gpio, val);
	} else
1432
		port->rs485.flags &= ~SER_RS485_ENABLED;
M
Mark Jackson 已提交
1433 1434 1435 1436 1437

	/* Enable interrupts */
	up->ier = mode;
	serial_out(up, UART_IER, up->ier);

1438 1439 1440
	/* If RS-485 is disabled, make sure the THR interrupt is fired when
	 * TX FIFO is below the trigger level.
	 */
1441
	if (!(port->rs485.flags & SER_RS485_ENABLED) &&
1442 1443 1444 1445 1446
	    (up->scr & OMAP_UART_SCR_TX_EMPTY)) {
		up->scr &= ~OMAP_UART_SCR_TX_EMPTY;
		serial_out(up, UART_OMAP_SCR, up->scr);
	}

M
Mark Jackson 已提交
1447 1448 1449 1450 1451 1452
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);

	return 0;
}

1453
static const struct uart_ops serial_omap_pops = {
1454 1455 1456 1457 1458
	.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,
1459 1460
	.throttle	= serial_omap_throttle,
	.unthrottle	= serial_omap_unthrottle,
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
	.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,
1473 1474 1475 1476
#ifdef CONFIG_CONSOLE_POLL
	.poll_put_char  = serial_omap_poll_put_char,
	.poll_get_char  = serial_omap_poll_get_char,
#endif
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
};

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,
};

1487
#ifdef CONFIG_PM_SLEEP
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
static int serial_omap_prepare(struct device *dev)
{
	struct uart_omap_port *up = dev_get_drvdata(dev);

	up->is_suspending = true;

	return 0;
}

static void serial_omap_complete(struct device *dev)
{
	struct uart_omap_port *up = dev_get_drvdata(dev);

	up->is_suspending = false;
}

1504
static int serial_omap_suspend(struct device *dev)
1505
{
1506
	struct uart_omap_port *up = dev_get_drvdata(dev);
1507

1508
	uart_suspend_port(&serial_omap_reg, &up->port);
1509
	flush_work(&up->qos_work);
1510

1511 1512 1513 1514 1515
	if (device_may_wakeup(dev))
		serial_omap_enable_wakeup(up, true);
	else
		serial_omap_enable_wakeup(up, false);

1516 1517 1518
	return 0;
}

1519
static int serial_omap_resume(struct device *dev)
1520
{
1521
	struct uart_omap_port *up = dev_get_drvdata(dev);
1522

1523 1524 1525
	if (device_may_wakeup(dev))
		serial_omap_enable_wakeup(up, false);

1526 1527
	uart_resume_port(&serial_omap_reg, &up->port);

1528 1529
	return 0;
}
1530 1531
#else
#define serial_omap_prepare NULL
1532
#define serial_omap_complete NULL
1533
#endif /* CONFIG_PM_SLEEP */
1534

B
Bill Pemberton 已提交
1535
static void omap_serial_fill_features_erratas(struct uart_omap_port *up)
1536 1537 1538 1539
{
	u32 mvr, scheme;
	u16 revision, major, minor;

1540
	mvr = readl(up->port.membase + (UART_OMAP_MVER << up->port.regshift));
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

	/* 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:
1560
		dev_warn(up->dev,
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
			"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);
1579
		up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
1580 1581 1582
		break;
	case OMAP_UART_REV_63:
		up->errata |= UART_ERRATA_i202_MDR1_ACCESS;
1583
		up->features |= OMAP_UART_WER_HAS_TX_WAKEUP;
1584 1585 1586 1587 1588 1589
		break;
	default:
		break;
	}
}

B
Bill Pemberton 已提交
1590
static struct omap_uart_port_info *of_get_uart_port_info(struct device *dev)
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
{
	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;
}

M
Mark Jackson 已提交
1603 1604 1605
static int serial_omap_probe_rs485(struct uart_omap_port *up,
				   struct device_node *np)
{
1606
	struct serial_rs485 *rs485conf = &up->port.rs485;
M
Mark Jackson 已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
	u32 rs485_delay[2];
	enum of_gpio_flags flags;
	int ret;

	rs485conf->flags = 0;
	up->rts_gpio = -EINVAL;

	if (!np)
		return 0;

	if (of_property_read_bool(np, "rs485-rts-active-high"))
		rs485conf->flags |= SER_RS485_RTS_ON_SEND;
	else
		rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;

	/* check for tx enable gpio */
	up->rts_gpio = of_get_named_gpio_flags(np, "rts-gpio", 0, &flags);
	if (gpio_is_valid(up->rts_gpio)) {
1625
		ret = devm_gpio_request(up->dev, up->rts_gpio, "omap-serial");
M
Mark Jackson 已提交
1626 1627 1628 1629 1630 1631
		if (ret < 0)
			return ret;
		ret = gpio_direction_output(up->rts_gpio,
					    flags & SER_RS485_RTS_AFTER_SEND);
		if (ret < 0)
			return ret;
1632 1633 1634
	} else if (up->rts_gpio == -EPROBE_DEFER) {
		return -EPROBE_DEFER;
	} else {
M
Mark Jackson 已提交
1635
		up->rts_gpio = -EINVAL;
1636
	}
M
Mark Jackson 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652

	if (of_property_read_u32_array(np, "rs485-rts-delay",
				    rs485_delay, 2) == 0) {
		rs485conf->delay_rts_before_send = rs485_delay[0];
		rs485conf->delay_rts_after_send = rs485_delay[1];
	}

	if (of_property_read_bool(np, "rs485-rx-during-tx"))
		rs485conf->flags |= SER_RS485_RX_DURING_TX;

	if (of_property_read_bool(np, "linux,rs485-enabled-at-boot-time"))
		rs485conf->flags |= SER_RS485_ENABLED;

	return 0;
}

B
Bill Pemberton 已提交
1653
static int serial_omap_probe(struct platform_device *pdev)
1654
{
J
Jingoo Han 已提交
1655
	struct omap_uart_port_info *omap_up_info = dev_get_platdata(&pdev->dev);
1656 1657
	struct uart_omap_port *up;
	struct resource *mem;
1658
	void __iomem *base;
1659 1660 1661
	int uartirq = 0;
	int wakeirq = 0;
	int ret;
1662

1663
	/* The optional wakeirq may be specified in the board dts file */
1664
	if (pdev->dev.of_node) {
1665 1666 1667 1668
		uartirq = irq_of_parse_and_map(pdev->dev.of_node, 0);
		if (!uartirq)
			return -EPROBE_DEFER;
		wakeirq = irq_of_parse_and_map(pdev->dev.of_node, 1);
1669
		omap_up_info = of_get_uart_port_info(&pdev->dev);
1670
		pdev->dev.platform_data = omap_up_info;
1671
	} else {
1672 1673 1674
		uartirq = platform_get_irq(pdev, 0);
		if (uartirq < 0)
			return -EPROBE_DEFER;
1675
	}
1676

1677 1678 1679
	up = devm_kzalloc(&pdev->dev, sizeof(*up), GFP_KERNEL);
	if (!up)
		return -ENOMEM;
1680

1681 1682 1683 1684
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	base = devm_ioremap_resource(&pdev->dev, mem);
	if (IS_ERR(base))
		return PTR_ERR(base);
1685

1686
	up->dev = &pdev->dev;
1687 1688 1689
	up->port.dev = &pdev->dev;
	up->port.type = PORT_OMAP;
	up->port.iotype = UPIO_MEM;
1690
	up->port.irq = uartirq;
1691 1692 1693 1694
	up->port.regshift = 2;
	up->port.fifosize = 64;
	up->port.ops = &serial_omap_pops;

1695
	if (pdev->dev.of_node)
1696
		ret = of_alias_get_id(pdev->dev.of_node, "serial");
1697
	else
1698
		ret = pdev->id;
1699

1700
	if (ret < 0) {
1701
		dev_err(&pdev->dev, "failed to get alias/pdev id, errno %d\n",
1702
			ret);
1703
		goto err_port_line;
1704
	}
1705
	up->port.line = ret;
1706

1707 1708 1709 1710 1711 1712 1713
	if (up->port.line >= OMAP_MAX_HSUART_PORTS) {
		dev_err(&pdev->dev, "uart ID %d >  MAX %d.\n", up->port.line,
			OMAP_MAX_HSUART_PORTS);
		ret = -ENXIO;
		goto err_port_line;
	}

1714 1715 1716 1717 1718
	up->wakeirq = wakeirq;
	if (!up->wakeirq)
		dev_info(up->port.dev, "no wakeirq for uart%d\n",
			 up->port.line);

M
Mark Jackson 已提交
1719 1720 1721 1722
	ret = serial_omap_probe_rs485(up, pdev->dev.of_node);
	if (ret < 0)
		goto err_rs485;

1723
	sprintf(up->name, "OMAP UART%d", up->port.line);
1724
	up->port.mapbase = mem->start;
1725
	up->port.membase = base;
1726 1727
	up->port.flags = omap_up_info->flags;
	up->port.uartclk = omap_up_info->uartclk;
1728
	up->port.rs485_config = serial_omap_config_rs485;
1729 1730
	if (!up->port.uartclk) {
		up->port.uartclk = DEFAULT_CLK_SPEED;
1731
		dev_warn(&pdev->dev,
P
Philippe Proulx 已提交
1732
			 "No clock speed specified: using default: %d\n",
1733
			 DEFAULT_CLK_SPEED);
1734
	}
1735

1736 1737 1738 1739 1740 1741
	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);
	INIT_WORK(&up->qos_work, serial_omap_uart_qos_work);

1742
	platform_set_drvdata(pdev, up);
1743 1744
	if (omap_up_info->autosuspend_timeout == 0)
		omap_up_info->autosuspend_timeout = -1;
F
Felipe Balbi 已提交
1745

1746
	device_init_wakeup(up->dev, true);
1747 1748
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_set_autosuspend_delay(&pdev->dev,
1749
			omap_up_info->autosuspend_timeout);
1750 1751

	pm_runtime_irq_safe(&pdev->dev);
1752 1753
	pm_runtime_enable(&pdev->dev);

1754 1755
	pm_runtime_get_sync(&pdev->dev);

1756 1757
	omap_serial_fill_features_erratas(up);

1758
	ui[up->port.line] = up;
1759 1760 1761 1762
	serial_omap_add_console_port(up);

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

1765 1766
	pm_runtime_mark_last_busy(up->dev);
	pm_runtime_put_autosuspend(up->dev);
1767
	return 0;
1768 1769 1770 1771

err_add_port:
	pm_runtime_put(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
1772 1773
	pm_qos_remove_request(&up->pm_qos_request);
	device_init_wakeup(up->dev, false);
M
Mark Jackson 已提交
1774
err_rs485:
1775
err_port_line:
1776 1777 1778
	return ret;
}

B
Bill Pemberton 已提交
1779
static int serial_omap_remove(struct platform_device *dev)
1780 1781 1782
{
	struct uart_omap_port *up = platform_get_drvdata(dev);

1783
	pm_runtime_put_sync(up->dev);
1784 1785 1786
	pm_runtime_disable(up->dev);
	uart_remove_one_port(&serial_omap_reg, &up->port);
	pm_qos_remove_request(&up->pm_qos_request);
1787
	device_init_wakeup(&dev->dev, false);
1788 1789 1790 1791

	return 0;
}

1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
/*
 * 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 */
1818
			dev_crit(up->dev, "Errata i202: timedout %x\n",
1819 1820 1821 1822 1823 1824 1825
						serial_in(up, UART_LSR));
			break;
		}
		udelay(1);
	}
}

1826
#ifdef CONFIG_PM
1827 1828
static void serial_omap_restore_context(struct uart_omap_port *up)
{
1829 1830 1831 1832 1833
	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);

1834 1835 1836 1837 1838
	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 */
1839 1840
	serial_out(up, UART_DLL, up->dll);
	serial_out(up, UART_DLM, up->dlh);
1841 1842 1843 1844 1845 1846
	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 */
1847
	serial_out(up, UART_OMAP_SCR, up->scr);
1848 1849
	serial_out(up, UART_EFR, up->efr);
	serial_out(up, UART_LCR, up->lcr);
1850 1851 1852 1853
	if (up->errata & UART_ERRATA_i202_MDR1_ACCESS)
		serial_omap_mdr1_errataset(up, up->mdr1);
	else
		serial_out(up, UART_OMAP_MDR1, up->mdr1);
1854
	serial_out(up, UART_OMAP_WER, up->wer);
1855 1856
}

1857 1858
static int serial_omap_runtime_suspend(struct device *dev)
{
1859 1860
	struct uart_omap_port *up = dev_get_drvdata(dev);

1861 1862 1863
	if (!up)
		return -EINVAL;

1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
	/*
	* When using 'no_console_suspend', the console UART must not be
	* suspended. Since driver suspend is managed by runtime suspend,
	* preventing runtime suspend (by returning error) will keep device
	* active during suspend.
	*/
	if (up->is_suspending && !console_suspend_enabled &&
	    uart_console(&up->port))
		return -EBUSY;

1874
	up->context_loss_cnt = serial_omap_get_context_loss_count(up);
1875

1876
	serial_omap_enable_wakeup(up, true);
1877

1878 1879 1880
	up->latency = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE;
	schedule_work(&up->qos_work);

1881 1882 1883
	return 0;
}

1884 1885
static int serial_omap_runtime_resume(struct device *dev)
{
1886 1887
	struct uart_omap_port *up = dev_get_drvdata(dev);

1888
	int loss_cnt = serial_omap_get_context_loss_count(up);
1889

1890 1891
	serial_omap_enable_wakeup(up, false);

1892
	if (loss_cnt < 0) {
1893
		dev_dbg(dev, "serial_omap_get_context_loss_count failed : %d\n",
1894
			loss_cnt);
1895
		serial_omap_restore_context(up);
1896 1897 1898
	} else if (up->context_loss_cnt != loss_cnt) {
		serial_omap_restore_context(up);
	}
1899 1900
	up->latency = up->calc_latency;
	schedule_work(&up->qos_work);
1901

1902 1903
	return 0;
}
1904 1905 1906 1907 1908 1909
#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)
1910 1911
	.prepare        = serial_omap_prepare,
	.complete       = serial_omap_complete,
1912 1913
};

1914 1915 1916 1917 1918 1919 1920 1921 1922
#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
1923 1924 1925

static struct platform_driver serial_omap_driver = {
	.probe          = serial_omap_probe,
1926
	.remove         = serial_omap_remove,
1927
	.driver		= {
1928
		.name	= OMAP_SERIAL_DRIVER_NAME,
1929
		.pm	= &serial_omap_dev_pm_ops,
1930
		.of_match_table = of_match_ptr(omap_serial_of_match),
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	},
};

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");