msm_serial.c 26.5 KB
Newer Older
1
/*
2
 * Driver for msm7k serial device and console
3 4 5
 *
 * Copyright (C) 2007 Google, Inc.
 * Author: Robert Love <rlove@google.com>
6
 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
# define SUPPORT_SYSRQ
#endif

D
David Brown 已提交
22
#include <linux/atomic.h>
23 24 25 26 27 28 29 30 31 32 33 34 35
#include <linux/hrtimer.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/irq.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <linux/clk.h>
#include <linux/platform_device.h>
36
#include <linux/delay.h>
D
David Brown 已提交
37 38
#include <linux/of.h>
#include <linux/of_device.h>
39 40 41

#include "msm_serial.h"

42 43 44 45 46 47 48
enum {
	UARTDM_1P1 = 1,
	UARTDM_1P2,
	UARTDM_1P3,
	UARTDM_1P4,
};

49 50 51 52
struct msm_port {
	struct uart_port	uart;
	char			name[16];
	struct clk		*clk;
53
	struct clk		*pclk;
54
	unsigned int		imr;
55 56
	int			is_uartdm;
	unsigned int		old_snap_state;
57
	bool			break_detected;
58 59
};

60
static inline void wait_for_xmitr(struct uart_port *port)
61
{
62 63 64 65 66 67
	while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) {
		if (msm_read(port, UART_ISR) & UART_ISR_TX_READY)
			break;
		udelay(1);
	}
	msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR);
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static void msm_stop_tx(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

	msm_port->imr &= ~UART_IMR_TXLEV;
	msm_write(port, msm_port->imr, UART_IMR);
}

static void msm_start_tx(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

	msm_port->imr |= UART_IMR_TXLEV;
	msm_write(port, msm_port->imr, UART_IMR);
}

static void msm_stop_rx(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

	msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
	msm_write(port, msm_port->imr, UART_IMR);
}

static void msm_enable_ms(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

	msm_port->imr |= UART_IMR_DELTA_CTS;
	msm_write(port, msm_port->imr, UART_IMR);
}

102 103
static void handle_rx_dm(struct uart_port *port, unsigned int misr)
{
J
Jiri Slaby 已提交
104
	struct tty_port *tport = &port->state->port;
105 106 107 108 109 110
	unsigned int sr;
	int count = 0;
	struct msm_port *msm_port = UART_TO_MSM(port);

	if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
		port->icount.overrun++;
J
Jiri Slaby 已提交
111
		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
	}

	if (misr & UART_IMR_RXSTALE) {
		count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
			msm_port->old_snap_state;
		msm_port->old_snap_state = 0;
	} else {
		count = 4 * (msm_read(port, UART_RFWR));
		msm_port->old_snap_state += count;
	}

	/* TODO: Precise error reporting */

	port->icount.rx += count;

	while (count > 0) {
129
		unsigned char buf[4];
130
		int sysrq, r_count, i;
131 132 133 134 135 136 137

		sr = msm_read(port, UART_SR);
		if ((sr & UART_SR_RX_READY) == 0) {
			msm_port->old_snap_state -= count;
			break;
		}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		ioread32_rep(port->membase + UARTDM_RF, buf, 1);
		r_count = min_t(int, count, sizeof(buf));

		for (i = 0; i < r_count; i++) {
			char flag = TTY_NORMAL;

			if (msm_port->break_detected && buf[i] == 0) {
				port->icount.brk++;
				flag = TTY_BREAK;
				msm_port->break_detected = false;
				if (uart_handle_break(port))
					continue;
			}

			if (!(port->read_status_mask & UART_SR_RX_BREAK))
				flag = TTY_NORMAL;

			spin_unlock(&port->lock);
			sysrq = uart_handle_sysrq_char(port, buf[i]);
			spin_lock(&port->lock);
			if (!sysrq)
				tty_insert_flip_char(tport, buf[i], flag);
		}
		count -= r_count;
162 163
	}

164
	spin_unlock(&port->lock);
J
Jiri Slaby 已提交
165
	tty_flip_buffer_push(tport);
166 167
	spin_lock(&port->lock);

168 169 170 171 172 173
	if (misr & (UART_IMR_RXSTALE))
		msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
	msm_write(port, 0xFFFFFF, UARTDM_DMRX);
	msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
}

174 175
static void handle_rx(struct uart_port *port)
{
J
Jiri Slaby 已提交
176
	struct tty_port *tport = &port->state->port;
177 178 179 180 181 182 183 184
	unsigned int sr;

	/*
	 * Handle overrun. My understanding of the hardware is that overrun
	 * is not tied to the RX buffer, so we handle the case out of band.
	 */
	if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
		port->icount.overrun++;
J
Jiri Slaby 已提交
185
		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
186 187 188 189 190 191 192
		msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
	}

	/* and now the main RX loop */
	while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
		unsigned int c;
		char flag = TTY_NORMAL;
193
		int sysrq;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

		c = msm_read(port, UART_RF);

		if (sr & UART_SR_RX_BREAK) {
			port->icount.brk++;
			if (uart_handle_break(port))
				continue;
		} else if (sr & UART_SR_PAR_FRAME_ERR) {
			port->icount.frame++;
		} else {
			port->icount.rx++;
		}

		/* Mask conditions we're ignorning. */
		sr &= port->read_status_mask;

210
		if (sr & UART_SR_RX_BREAK)
211
			flag = TTY_BREAK;
212
		else if (sr & UART_SR_PAR_FRAME_ERR)
213 214
			flag = TTY_FRAME;

215 216 217 218
		spin_unlock(&port->lock);
		sysrq = uart_handle_sysrq_char(port, c);
		spin_lock(&port->lock);
		if (!sysrq)
J
Jiri Slaby 已提交
219
			tty_insert_flip_char(tport, c, flag);
220 221
	}

222
	spin_unlock(&port->lock);
J
Jiri Slaby 已提交
223
	tty_flip_buffer_push(tport);
224
	spin_lock(&port->lock);
225 226
}

227
static void reset_dm_count(struct uart_port *port, int count)
228
{
229
	wait_for_xmitr(port);
230
	msm_write(port, count, UARTDM_NCF_TX);
231
	msm_read(port, UARTDM_NCF_TX);
232 233
}

234 235
static void handle_tx(struct uart_port *port)
{
A
Alan Cox 已提交
236
	struct circ_buf *xmit = &port->state->xmit;
237
	struct msm_port *msm_port = UART_TO_MSM(port);
238 239
	unsigned int tx_count, num_chars;
	unsigned int tf_pointer = 0;
240 241 242 243 244 245
	void __iomem *tf;

	if (msm_port->is_uartdm)
		tf = port->membase + UARTDM_TF;
	else
		tf = port->membase + UART_TF;
246 247 248 249

	tx_count = uart_circ_chars_pending(xmit);
	tx_count = min3(tx_count, (unsigned int)UART_XMIT_SIZE - xmit->tail,
			port->fifosize);
250 251

	if (port->x_char) {
252
		if (msm_port->is_uartdm)
253
			reset_dm_count(port, tx_count + 1);
254

255
		iowrite8_rep(tf, &port->x_char, 1);
256 257
		port->icount.tx++;
		port->x_char = 0;
258 259
	} else if (tx_count && msm_port->is_uartdm) {
		reset_dm_count(port, tx_count);
260 261
	}

262 263 264
	while (tf_pointer < tx_count) {
		int i;
		char buf[4] = { 0 };
265

266
		if (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
267 268
			break;

269
		if (msm_port->is_uartdm)
270 271
			num_chars = min(tx_count - tf_pointer,
					(unsigned int)sizeof(buf));
272 273
		else
			num_chars = 1;
274

275 276 277 278 279
		for (i = 0; i < num_chars; i++) {
			buf[i] = xmit->buf[xmit->tail + i];
			port->icount.tx++;
		}

280
		iowrite32_rep(tf, buf, 1);
281 282
		xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1);
		tf_pointer += num_chars;
283 284
	}

285 286 287 288
	/* disable tx interrupts if nothing more to send */
	if (uart_circ_empty(xmit))
		msm_stop_tx(port);

289 290 291 292 293 294 295 296
	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);
}

static void handle_delta_cts(struct uart_port *port)
{
	msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
	port->icount.cts++;
297
	wake_up_interruptible(&port->state->port.delta_msr_wait);
298 299 300 301 302 303 304 305 306 307 308 309
}

static irqreturn_t msm_irq(int irq, void *dev_id)
{
	struct uart_port *port = dev_id;
	struct msm_port *msm_port = UART_TO_MSM(port);
	unsigned int misr;

	spin_lock(&port->lock);
	misr = msm_read(port, UART_MISR);
	msm_write(port, 0, UART_IMR); /* disable interrupt */

310 311 312 313 314
	if (misr & UART_IMR_RXBREAK_START) {
		msm_port->break_detected = true;
		msm_write(port, UART_CR_CMD_RESET_RXBREAK_START, UART_CR);
	}

315 316 317 318 319 320
	if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
		if (msm_port->is_uartdm)
			handle_rx_dm(port, misr);
		else
			handle_rx(port);
	}
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	if (misr & UART_IMR_TXLEV)
		handle_tx(port);
	if (misr & UART_IMR_DELTA_CTS)
		handle_delta_cts(port);

	msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
	spin_unlock(&port->lock);

	return IRQ_HANDLED;
}

static unsigned int msm_tx_empty(struct uart_port *port)
{
	return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
}

static unsigned int msm_get_mctrl(struct uart_port *port)
{
	return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
}

342
static void msm_reset(struct uart_port *port)
343
{
344 345
	struct msm_port *msm_port = UART_TO_MSM(port);

346 347 348 349 350 351 352
	/* reset everything */
	msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
	msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
	msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
	msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
	msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
	msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
353 354 355 356

	/* Disable DM modes */
	if (msm_port->is_uartdm)
		msm_write(port, 0, UARTDM_DMEN);
357
}
358

S
Stephen Boyd 已提交
359
static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
360 361
{
	unsigned int mr;
362

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	mr = msm_read(port, UART_MR1);

	if (!(mctrl & TIOCM_RTS)) {
		mr &= ~UART_MR1_RX_RDY_CTL;
		msm_write(port, mr, UART_MR1);
		msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
	} else {
		mr |= UART_MR1_RX_RDY_CTL;
		msm_write(port, mr, UART_MR1);
	}
}

static void msm_break_ctl(struct uart_port *port, int break_ctl)
{
	if (break_ctl)
		msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
	else
		msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
struct msm_baud_map {
	u16	divisor;
	u8	code;
	u8	rxstale;
};

static const struct msm_baud_map *
msm_find_best_baud(struct uart_port *port, unsigned int baud)
{
	unsigned int i, divisor;
	const struct msm_baud_map *entry;
	static const struct msm_baud_map table[] = {
		{ 1536, 0x00,  1 },
		{  768, 0x11,  1 },
		{  384, 0x22,  1 },
		{  192, 0x33,  1 },
		{   96, 0x44,  1 },
		{   48, 0x55,  1 },
		{   32, 0x66,  1 },
		{   24, 0x77,  1 },
		{   16, 0x88,  1 },
		{   12, 0x99,  6 },
		{    8, 0xaa,  6 },
		{    6, 0xbb,  6 },
		{    4, 0xcc,  6 },
		{    3, 0xdd,  8 },
		{    2, 0xee, 16 },
		{    1, 0xff, 31 },
	};

	divisor = uart_get_divisor(port, baud);

	for (i = 0, entry = table; i < ARRAY_SIZE(table); i++, entry++)
		if (entry->divisor <= divisor)
			break;

	return entry; /* Default to smallest divider */
}

A
Alan Cox 已提交
422
static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
423
{
424
	unsigned int rxstale, watermark;
425
	struct msm_port *msm_port = UART_TO_MSM(port);
426
	const struct msm_baud_map *entry;
427

428
	entry = msm_find_best_baud(port, baud);
429

430
	msm_write(port, entry->code, UART_CSR);
431 432

	/* RX stale watermark */
433
	rxstale = entry->rxstale;
434 435 436 437 438 439 440 441 442 443 444
	watermark = UART_IPR_STALE_LSB & rxstale;
	watermark |= UART_IPR_RXSTALE_LAST;
	watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
	msm_write(port, watermark, UART_IPR);

	/* set RX watermark */
	watermark = (port->fifosize * 3) / 4;
	msm_write(port, watermark, UART_RFWR);

	/* set TX watermark */
	msm_write(port, 10, UART_TFWR);
A
Alan Cox 已提交
445

446 447 448 449 450 451 452 453 454 455 456 457
	msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
	msm_reset(port);

	/* Enable RX and TX */
	msm_write(port, UART_CR_TX_ENABLE | UART_CR_RX_ENABLE, UART_CR);

	/* turn on RX and CTS interrupts */
	msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
			UART_IMR_CURRENT_CTS | UART_IMR_RXBREAK_START;

	msm_write(port, msm_port->imr, UART_IMR);

458 459 460 461 462 463
	if (msm_port->is_uartdm) {
		msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
		msm_write(port, 0xFFFFFF, UARTDM_DMRX);
		msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
	}

A
Alan Cox 已提交
464
	return baud;
465 466 467 468 469 470
}

static void msm_init_clock(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

471
	clk_prepare_enable(msm_port->clk);
472
	clk_prepare_enable(msm_port->pclk);
473
	msm_serial_set_mnd_regs(port);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
}

static int msm_startup(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);
	unsigned int data, rfr_level;
	int ret;

	snprintf(msm_port->name, sizeof(msm_port->name),
		 "msm_serial%d", port->line);

	ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
			  msm_port->name, port);
	if (unlikely(ret))
		return ret;

	msm_init_clock(port);

	if (likely(port->fifosize > 12))
		rfr_level = port->fifosize - 12;
	else
		rfr_level = port->fifosize;

	/* set automatic RFR level */
	data = msm_read(port, UART_MR1);
	data &= ~UART_MR1_AUTO_RFR_LEVEL1;
	data &= ~UART_MR1_AUTO_RFR_LEVEL0;
	data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
	data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
	msm_write(port, data, UART_MR1);
	return 0;
}

static void msm_shutdown(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

	msm_port->imr = 0;
	msm_write(port, 0, UART_IMR); /* disable interrupts */

514
	clk_disable_unprepare(msm_port->clk);
515 516 517 518 519 520 521 522 523 524 525 526 527 528

	free_irq(port->irq, port);
}

static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
			    struct ktermios *old)
{
	unsigned long flags;
	unsigned int baud, mr;

	spin_lock_irqsave(&port->lock, flags);

	/* calculate and set baud rate */
	baud = uart_get_baud_rate(port, termios, old, 300, 115200);
A
Alan Cox 已提交
529 530 531
	baud = msm_set_baud_rate(port, baud);
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);
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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	/* calculate parity */
	mr = msm_read(port, UART_MR2);
	mr &= ~UART_MR2_PARITY_MODE;
	if (termios->c_cflag & PARENB) {
		if (termios->c_cflag & PARODD)
			mr |= UART_MR2_PARITY_MODE_ODD;
		else if (termios->c_cflag & CMSPAR)
			mr |= UART_MR2_PARITY_MODE_SPACE;
		else
			mr |= UART_MR2_PARITY_MODE_EVEN;
	}

	/* calculate bits per char */
	mr &= ~UART_MR2_BITS_PER_CHAR;
	switch (termios->c_cflag & CSIZE) {
	case CS5:
		mr |= UART_MR2_BITS_PER_CHAR_5;
		break;
	case CS6:
		mr |= UART_MR2_BITS_PER_CHAR_6;
		break;
	case CS7:
		mr |= UART_MR2_BITS_PER_CHAR_7;
		break;
	case CS8:
	default:
		mr |= UART_MR2_BITS_PER_CHAR_8;
		break;
	}

	/* calculate stop bits */
	mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
	if (termios->c_cflag & CSTOPB)
		mr |= UART_MR2_STOP_BIT_LEN_TWO;
	else
		mr |= UART_MR2_STOP_BIT_LEN_ONE;

	/* set parity, bits per char, and stop bit */
	msm_write(port, mr, UART_MR2);

	/* calculate and set hardware flow control */
	mr = msm_read(port, UART_MR1);
	mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
	if (termios->c_cflag & CRTSCTS) {
		mr |= UART_MR1_CTS_CTL;
		mr |= UART_MR1_RX_RDY_CTL;
	}
	msm_write(port, mr, UART_MR1);

	/* Configure status bits to ignore based on termio flags. */
	port->read_status_mask = 0;
	if (termios->c_iflag & INPCK)
		port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
P
Peter Hurley 已提交
586
	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
		port->read_status_mask |= UART_SR_RX_BREAK;

	uart_update_timeout(port, termios->c_cflag, baud);

	spin_unlock_irqrestore(&port->lock, flags);
}

static const char *msm_type(struct uart_port *port)
{
	return "MSM";
}

static void msm_release_port(struct uart_port *port)
{
	struct platform_device *pdev = to_platform_device(port->dev);
602
	struct resource *uart_resource;
603 604
	resource_size_t size;

605 606
	uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (unlikely(!uart_resource))
607
		return;
608
	size = resource_size(uart_resource);
609 610 611 612 613 614 615 616 617

	release_mem_region(port->mapbase, size);
	iounmap(port->membase);
	port->membase = NULL;
}

static int msm_request_port(struct uart_port *port)
{
	struct platform_device *pdev = to_platform_device(port->dev);
618
	struct resource *uart_resource;
619
	resource_size_t size;
620
	int ret;
621

622
	uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
623
	if (unlikely(!uart_resource))
624 625
		return -ENXIO;

626 627 628
	size = resource_size(uart_resource);

	if (!request_mem_region(port->mapbase, size, "msm_serial"))
629 630 631 632
		return -EBUSY;

	port->membase = ioremap(port->mapbase, size);
	if (!port->membase) {
633 634 635 636
		ret = -EBUSY;
		goto fail_release_port;
	}

637
	return 0;
638 639 640 641

fail_release_port:
	release_mem_region(port->mapbase, size);
	return ret;
642 643 644 645
}

static void msm_config_port(struct uart_port *port, int flags)
{
646
	int ret;
647

648 649
	if (flags & UART_CONFIG_TYPE) {
		port->type = PORT_MSM;
650 651 652
		ret = msm_request_port(port);
		if (ret)
			return;
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
	}
}

static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
		return -EINVAL;
	if (unlikely(port->irq != ser->irq))
		return -EINVAL;
	return 0;
}

static void msm_power(struct uart_port *port, unsigned int state,
		      unsigned int oldstate)
{
	struct msm_port *msm_port = UART_TO_MSM(port);

	switch (state) {
	case 0:
672
		clk_prepare_enable(msm_port->clk);
673
		clk_prepare_enable(msm_port->pclk);
674 675
		break;
	case 3:
676
		clk_disable_unprepare(msm_port->clk);
677
		clk_disable_unprepare(msm_port->pclk);
678 679
		break;
	default:
680
		pr_err("msm_serial: Unknown PM state %d\n", state);
681 682 683
	}
}

684 685 686 687 688 689 690 691
#ifdef CONFIG_CONSOLE_POLL
static int msm_poll_get_char_single(struct uart_port *port)
{
	struct msm_port *msm_port = UART_TO_MSM(port);
	unsigned int rf_reg = msm_port->is_uartdm ? UARTDM_RF : UART_RF;

	if (!(msm_read(port, UART_SR) & UART_SR_RX_READY))
		return NO_POLL_CHAR;
692 693

	return msm_read(port, rf_reg) & 0xff;
694 695
}

696
static int msm_poll_get_char_dm(struct uart_port *port)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
{
	int c;
	static u32 slop;
	static int count;
	unsigned char *sp = (unsigned char *)&slop;

	/* Check if a previous read had more than one char */
	if (count) {
		c = sp[sizeof(slop) - count];
		count--;
	/* Or if FIFO is empty */
	} else if (!(msm_read(port, UART_SR) & UART_SR_RX_READY)) {
		/*
		 * If RX packing buffer has less than a word, force stale to
		 * push contents into RX FIFO
		 */
		count = msm_read(port, UARTDM_RXFS);
		count = (count >> UARTDM_RXFS_BUF_SHIFT) & UARTDM_RXFS_BUF_MASK;
		if (count) {
			msm_write(port, UART_CR_CMD_FORCE_STALE, UART_CR);
			slop = msm_read(port, UARTDM_RF);
			c = sp[0];
			count--;
720 721 722 723
			msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
			msm_write(port, 0xFFFFFF, UARTDM_DMRX);
			msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE,
				  UART_CR);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
		} else {
			c = NO_POLL_CHAR;
		}
	/* FIFO has a word */
	} else {
		slop = msm_read(port, UARTDM_RF);
		c = sp[0];
		count = sizeof(slop) - 1;
	}

	return c;
}

static int msm_poll_get_char(struct uart_port *port)
{
	u32 imr;
	int c;
	struct msm_port *msm_port = UART_TO_MSM(port);

	/* Disable all interrupts */
	imr = msm_read(port, UART_IMR);
	msm_write(port, 0, UART_IMR);

747 748
	if (msm_port->is_uartdm)
		c = msm_poll_get_char_dm(port);
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 784 785
	else
		c = msm_poll_get_char_single(port);

	/* Enable interrupts */
	msm_write(port, imr, UART_IMR);

	return c;
}

static void msm_poll_put_char(struct uart_port *port, unsigned char c)
{
	u32 imr;
	struct msm_port *msm_port = UART_TO_MSM(port);

	/* Disable all interrupts */
	imr = msm_read(port, UART_IMR);
	msm_write(port, 0, UART_IMR);

	if (msm_port->is_uartdm)
		reset_dm_count(port, 1);

	/* Wait until FIFO is empty */
	while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
		cpu_relax();

	/* Write a character */
	msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);

	/* Wait until FIFO is empty */
	while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
		cpu_relax();

	/* Enable interrupts */
	msm_write(port, imr, UART_IMR);
}
#endif

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static struct uart_ops msm_uart_pops = {
	.tx_empty = msm_tx_empty,
	.set_mctrl = msm_set_mctrl,
	.get_mctrl = msm_get_mctrl,
	.stop_tx = msm_stop_tx,
	.start_tx = msm_start_tx,
	.stop_rx = msm_stop_rx,
	.enable_ms = msm_enable_ms,
	.break_ctl = msm_break_ctl,
	.startup = msm_startup,
	.shutdown = msm_shutdown,
	.set_termios = msm_set_termios,
	.type = msm_type,
	.release_port = msm_release_port,
	.request_port = msm_request_port,
	.config_port = msm_config_port,
	.verify_port = msm_verify_port,
	.pm = msm_power,
804 805 806 807
#ifdef CONFIG_CONSOLE_POLL
	.poll_get_char	= msm_poll_get_char,
	.poll_put_char	= msm_poll_put_char,
#endif
808 809 810 811 812 813 814 815
};

static struct msm_port msm_uart_ports[] = {
	{
		.uart = {
			.iotype = UPIO_MEM,
			.ops = &msm_uart_pops,
			.flags = UPF_BOOT_AUTOCONF,
816
			.fifosize = 64,
817 818 819 820 821 822 823 824
			.line = 0,
		},
	},
	{
		.uart = {
			.iotype = UPIO_MEM,
			.ops = &msm_uart_pops,
			.flags = UPF_BOOT_AUTOCONF,
825
			.fifosize = 64,
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
			.line = 1,
		},
	},
	{
		.uart = {
			.iotype = UPIO_MEM,
			.ops = &msm_uart_pops,
			.flags = UPF_BOOT_AUTOCONF,
			.fifosize = 64,
			.line = 2,
		},
	},
};

#define UART_NR	ARRAY_SIZE(msm_uart_ports)

static inline struct uart_port *get_port_from_line(unsigned int line)
{
	return &msm_uart_ports[line].uart;
}

#ifdef CONFIG_SERIAL_MSM_CONSOLE
848 849
static void __msm_console_write(struct uart_port *port, const char *s,
				unsigned int count, bool is_uartdm)
850
{
851 852 853
	int i;
	int num_newlines = 0;
	bool replaced = false;
854
	void __iomem *tf;
855

856
	if (is_uartdm)
857 858 859 860
		tf = port->membase + UARTDM_TF;
	else
		tf = port->membase + UART_TF;

861 862 863 864 865 866
	/* Account for newlines that will get a carriage return added */
	for (i = 0; i < count; i++)
		if (s[i] == '\n')
			num_newlines++;
	count += num_newlines;

867
	spin_lock(&port->lock);
868
	if (is_uartdm)
869 870 871 872 873 874 875 876
		reset_dm_count(port, count);

	i = 0;
	while (i < count) {
		int j;
		unsigned int num_chars;
		char buf[4] = { 0 };

877
		if (is_uartdm)
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
			num_chars = min(count - i, (unsigned int)sizeof(buf));
		else
			num_chars = 1;

		for (j = 0; j < num_chars; j++) {
			char c = *s;

			if (c == '\n' && !replaced) {
				buf[j] = '\r';
				j++;
				replaced = true;
			}
			if (j < num_chars) {
				buf[j] = c;
				s++;
				replaced = false;
			}
		}

		while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
			cpu_relax();

900
		iowrite32_rep(tf, buf, 1);
901 902
		i += num_chars;
	}
903 904 905
	spin_unlock(&port->lock);
}

906 907 908 909 910 911 912 913 914 915 916 917 918 919
static void msm_console_write(struct console *co, const char *s,
			      unsigned int count)
{
	struct uart_port *port;
	struct msm_port *msm_port;

	BUG_ON(co->index < 0 || co->index >= UART_NR);

	port = get_port_from_line(co->index);
	msm_port = UART_TO_MSM(port);

	__msm_console_write(port, s, count, msm_port->is_uartdm);
}

920 921 922
static int __init msm_console_setup(struct console *co, char *options)
{
	struct uart_port *port;
923
	struct msm_port *msm_port;
924
	int baud = 0, flow, bits, parity;
925 926 927 928 929

	if (unlikely(co->index >= UART_NR || co->index < 0))
		return -ENXIO;

	port = get_port_from_line(co->index);
930
	msm_port = UART_TO_MSM(port);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

	if (unlikely(!port->membase))
		return -ENXIO;

	msm_init_clock(port);

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

	bits = 8;
	parity = 'n';
	flow = 'n';
	msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
		  UART_MR2);	/* 8N1 */

	if (baud < 300 || baud > 115200)
		baud = 115200;
	msm_set_baud_rate(port, baud);

	msm_reset(port);

952 953 954 955 956
	if (msm_port->is_uartdm) {
		msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
		msm_write(port, UART_CR_TX_ENABLE, UART_CR);
	}

957
	pr_info("msm_serial: console setup on port #%d\n", port->line);
958 959 960 961

	return uart_set_options(port, co, baud, parity, bits, flow);
}

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
static void
msm_serial_early_write(struct console *con, const char *s, unsigned n)
{
	struct earlycon_device *dev = con->data;

	__msm_console_write(&dev->port, s, n, false);
}

static int __init
msm_serial_early_console_setup(struct earlycon_device *device, const char *opt)
{
	if (!device->port.membase)
		return -ENODEV;

	device->con->write = msm_serial_early_write;
	return 0;
}
EARLYCON_DECLARE(msm_serial, msm_serial_early_console_setup);
OF_EARLYCON_DECLARE(msm_serial, "qcom,msm-uart",
		    msm_serial_early_console_setup);

static void
msm_serial_early_write_dm(struct console *con, const char *s, unsigned n)
{
	struct earlycon_device *dev = con->data;

	__msm_console_write(&dev->port, s, n, true);
}

static int __init
msm_serial_early_console_setup_dm(struct earlycon_device *device,
				  const char *opt)
{
	if (!device->port.membase)
		return -ENODEV;

	device->con->write = msm_serial_early_write_dm;
	return 0;
}
EARLYCON_DECLARE(msm_serial_dm, msm_serial_early_console_setup_dm);
OF_EARLYCON_DECLARE(msm_serial_dm, "qcom,msm-uartdm",
		    msm_serial_early_console_setup_dm);

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
static struct uart_driver msm_uart_driver;

static struct console msm_console = {
	.name = "ttyMSM",
	.write = msm_console_write,
	.device = uart_console_device,
	.setup = msm_console_setup,
	.flags = CON_PRINTBUFFER,
	.index = -1,
	.data = &msm_uart_driver,
};

#define MSM_CONSOLE	(&msm_console)

#else
#define MSM_CONSOLE	NULL
#endif

static struct uart_driver msm_uart_driver = {
	.owner = THIS_MODULE,
	.driver_name = "msm_serial",
	.dev_name = "ttyMSM",
	.nr = UART_NR,
	.cons = MSM_CONSOLE,
};

D
David Brown 已提交
1031 1032
static atomic_t msm_uart_next_id = ATOMIC_INIT(0);

1033
static const struct of_device_id msm_uartdm_table[] = {
1034 1035 1036 1037
	{ .compatible = "qcom,msm-uartdm-v1.1", .data = (void *)UARTDM_1P1 },
	{ .compatible = "qcom,msm-uartdm-v1.2", .data = (void *)UARTDM_1P2 },
	{ .compatible = "qcom,msm-uartdm-v1.3", .data = (void *)UARTDM_1P3 },
	{ .compatible = "qcom,msm-uartdm-v1.4", .data = (void *)UARTDM_1P4 },
1038 1039 1040
	{ }
};

1041
static int msm_serial_probe(struct platform_device *pdev)
1042 1043 1044 1045
{
	struct msm_port *msm_port;
	struct resource *resource;
	struct uart_port *port;
1046
	const struct of_device_id *id;
R
Roel Kluin 已提交
1047
	int irq;
1048

D
David Brown 已提交
1049 1050 1051
	if (pdev->id == -1)
		pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;

1052 1053 1054
	if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
		return -ENXIO;

1055
	dev_info(&pdev->dev, "msm_serial: detected port #%d\n", pdev->id);
1056 1057 1058 1059 1060

	port = get_port_from_line(pdev->id);
	port->dev = &pdev->dev;
	msm_port = UART_TO_MSM(port);

1061 1062 1063
	id = of_match_device(msm_uartdm_table, &pdev->dev);
	if (id)
		msm_port->is_uartdm = (unsigned long)id->data;
1064 1065 1066
	else
		msm_port->is_uartdm = 0;

1067
	msm_port->clk = devm_clk_get(&pdev->dev, "core");
1068 1069 1070 1071
	if (IS_ERR(msm_port->clk))
		return PTR_ERR(msm_port->clk);

	if (msm_port->is_uartdm) {
1072
		msm_port->pclk = devm_clk_get(&pdev->dev, "iface");
1073 1074
		if (IS_ERR(msm_port->pclk))
			return PTR_ERR(msm_port->pclk);
1075

1076
		clk_set_rate(msm_port->clk, 1843200);
1077
	}
1078

1079
	port->uartclk = clk_get_rate(msm_port->clk);
1080
	dev_info(&pdev->dev, "uartclk = %d\n", port->uartclk);
1081

1082
	resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1083 1084 1085 1086
	if (unlikely(!resource))
		return -ENXIO;
	port->mapbase = resource->start;

R
Roel Kluin 已提交
1087 1088
	irq = platform_get_irq(pdev, 0);
	if (unlikely(irq < 0))
1089
		return -ENXIO;
R
Roel Kluin 已提交
1090
	port->irq = irq;
1091 1092 1093 1094 1095 1096

	platform_set_drvdata(pdev, port);

	return uart_add_one_port(&msm_uart_driver, port);
}

B
Bill Pemberton 已提交
1097
static int msm_serial_remove(struct platform_device *pdev)
1098
{
1099
	struct uart_port *port = platform_get_drvdata(pdev);
1100

1101
	uart_remove_one_port(&msm_uart_driver, port);
1102 1103 1104 1105

	return 0;
}

1106
static const struct of_device_id msm_match_table[] = {
D
David Brown 已提交
1107
	{ .compatible = "qcom,msm-uart" },
1108
	{ .compatible = "qcom,msm-uartdm" },
D
David Brown 已提交
1109 1110 1111
	{}
};

1112 1113
static struct platform_driver msm_platform_driver = {
	.remove = msm_serial_remove,
1114
	.probe = msm_serial_probe,
1115 1116 1117
	.driver = {
		.name = "msm_serial",
		.owner = THIS_MODULE,
D
David Brown 已提交
1118
		.of_match_table = msm_match_table,
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	},
};

static int __init msm_serial_init(void)
{
	int ret;

	ret = uart_register_driver(&msm_uart_driver);
	if (unlikely(ret))
		return ret;

1130
	ret = platform_driver_register(&msm_platform_driver);
1131 1132 1133
	if (unlikely(ret))
		uart_unregister_driver(&msm_uart_driver);

1134
	pr_info("msm_serial: driver initialized\n");
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

	return ret;
}

static void __exit msm_serial_exit(void)
{
#ifdef CONFIG_SERIAL_MSM_CONSOLE
	unregister_console(&msm_console);
#endif
	platform_driver_unregister(&msm_platform_driver);
	uart_unregister_driver(&msm_uart_driver);
}

module_init(msm_serial_init);
module_exit(msm_serial_exit);

MODULE_AUTHOR("Robert Love <rlove@google.com>");
MODULE_DESCRIPTION("Driver for msm7x serial device");
MODULE_LICENSE("GPL");