msm_serial.c 22.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 42 43 44 45

#include "msm_serial.h"

struct msm_port {
	struct uart_port	uart;
	char			name[16];
	struct clk		*clk;
46
	struct clk		*pclk;
47
	unsigned int		imr;
48 49 50
	unsigned int            *gsbi_base;
	int			is_uartdm;
	unsigned int		old_snap_state;
51 52
};

53 54 55 56 57 58 59
static inline void wait_for_xmitr(struct uart_port *port, int bits)
{
	if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY))
		while ((msm_read(port, UART_ISR) & bits) != bits)
			cpu_relax();
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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);
}

92 93
static void handle_rx_dm(struct uart_port *port, unsigned int misr)
{
J
Jiri Slaby 已提交
94
	struct tty_port *tport = &port->state->port;
95 96 97 98 99 100
	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 已提交
101
		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		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) {
		unsigned int c;

		sr = msm_read(port, UART_SR);
		if ((sr & UART_SR_RX_READY) == 0) {
			msm_port->old_snap_state -= count;
			break;
		}
		c = msm_read(port, UARTDM_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++;

		/* TODO: handle sysrq */
J
Jiri Slaby 已提交
135
		tty_insert_flip_string(tport, (char *)&c,
136 137 138 139
				       (count > 4) ? 4 : count);
		count -= 4;
	}

J
Jiri Slaby 已提交
140
	tty_flip_buffer_push(tport);
141 142 143 144 145 146
	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);
}

147 148
static void handle_rx(struct uart_port *port)
{
J
Jiri Slaby 已提交
149
	struct tty_port *tport = &port->state->port;
150 151 152 153 154 155 156 157
	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 已提交
158
		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		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;

		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;

		if (sr & UART_SR_RX_BREAK) {
			flag = TTY_BREAK;
		} else if (sr & UART_SR_PAR_FRAME_ERR) {
			flag = TTY_FRAME;
		}

		if (!uart_handle_sysrq_char(port, c))
J
Jiri Slaby 已提交
189
			tty_insert_flip_char(tport, c, flag);
190 191
	}

J
Jiri Slaby 已提交
192
	tty_flip_buffer_push(tport);
193 194
}

195 196 197 198 199 200
static void reset_dm_count(struct uart_port *port)
{
	wait_for_xmitr(port, UART_ISR_TX_READY);
	msm_write(port, 1, UARTDM_NCF_TX);
}

201 202
static void handle_tx(struct uart_port *port)
{
A
Alan Cox 已提交
203
	struct circ_buf *xmit = &port->state->xmit;
204 205 206 207
	struct msm_port *msm_port = UART_TO_MSM(port);
	int sent_tx;

	if (port->x_char) {
208 209 210 211 212
		if (msm_port->is_uartdm)
			reset_dm_count(port);

		msm_write(port, port->x_char,
			  msm_port->is_uartdm ? UARTDM_TF : UART_TF);
213 214 215 216
		port->icount.tx++;
		port->x_char = 0;
	}

217 218 219
	if (msm_port->is_uartdm)
		reset_dm_count(port);

220 221 222 223 224 225 226
	while (msm_read(port, UART_SR) & UART_SR_TX_READY) {
		if (uart_circ_empty(xmit)) {
			/* disable tx interrupts */
			msm_port->imr &= ~UART_IMR_TXLEV;
			msm_write(port, msm_port->imr, UART_IMR);
			break;
		}
227 228
		msm_write(port, xmit->buf[xmit->tail],
			  msm_port->is_uartdm ? UARTDM_TF : UART_TF);
229

230 231
		if (msm_port->is_uartdm)
			reset_dm_count(port);
232 233 234 235 236 237 238 239 240 241 242 243 244 245

		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		port->icount.tx++;
		sent_tx = 1;
	}

	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++;
246
	wake_up_interruptible(&port->state->port.delta_msr_wait);
247 248 249 250 251 252 253 254 255 256 257 258
}

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

259 260 261 262 263 264
	if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
		if (msm_port->is_uartdm)
			handle_rx_dm(port, misr);
		else
			handle_rx(port);
	}
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	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;
}

286 287

static void msm_reset(struct uart_port *port)
288
{
289 290 291 292 293 294 295 296
	/* 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);
}
297

298 299 300
void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	unsigned int mr;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	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);
}

A
Alan Cox 已提交
321
static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
322 323
{
	unsigned int baud_code, rxstale, watermark;
324
	struct msm_port *msm_port = UART_TO_MSM(port);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

	switch (baud) {
	case 300:
		baud_code = UART_CSR_300;
		rxstale = 1;
		break;
	case 600:
		baud_code = UART_CSR_600;
		rxstale = 1;
		break;
	case 1200:
		baud_code = UART_CSR_1200;
		rxstale = 1;
		break;
	case 2400:
		baud_code = UART_CSR_2400;
		rxstale = 1;
		break;
	case 4800:
		baud_code = UART_CSR_4800;
		rxstale = 1;
		break;
	case 9600:
		baud_code = UART_CSR_9600;
		rxstale = 2;
		break;
	case 14400:
		baud_code = UART_CSR_14400;
		rxstale = 3;
		break;
	case 19200:
		baud_code = UART_CSR_19200;
		rxstale = 4;
		break;
	case 28800:
		baud_code = UART_CSR_28800;
		rxstale = 6;
		break;
	case 38400:
		baud_code = UART_CSR_38400;
		rxstale = 8;
		break;
	case 57600:
		baud_code = UART_CSR_57600;
		rxstale = 16;
		break;
	case 115200:
	default:
		baud_code = UART_CSR_115200;
A
Alan Cox 已提交
374
		baud = 115200;
375 376 377 378
		rxstale = 31;
		break;
	}

379 380 381
	if (msm_port->is_uartdm)
		msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);

382 383 384 385 386 387 388 389 390 391 392 393 394 395
	msm_write(port, baud_code, UART_CSR);

	/* RX stale watermark */
	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 已提交
396

397 398 399 400 401 402
	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 已提交
403
	return baud;
404 405 406 407 408 409 410
}


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

411
	clk_prepare_enable(msm_port->clk);
412
	if (!IS_ERR(msm_port->pclk))
413
		clk_prepare_enable(msm_port->pclk);
414
	msm_serial_set_mnd_regs(port);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
}

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

	/* make sure that RXSTALE count is non-zero */
	data = msm_read(port, UART_IPR);
	if (unlikely(!data)) {
		data |= UART_IPR_RXSTALE_LAST;
		data |= UART_IPR_STALE_LSB;
		msm_write(port, data, UART_IPR);
	}

454 455 456 457 458 459 460 461 462
	data = 0;
	if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
		msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
		msm_reset(port);
		data = UART_CR_TX_ENABLE;
	}

	data |= UART_CR_RX_ENABLE;
	msm_write(port, data, UART_CR);	/* enable TX & RX */
463

464 465 466
	/* Make sure IPR is not 0 to start with*/
	if (msm_port->is_uartdm)
		msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
467 468 469 470 471

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

472 473 474 475 476 477 478
	if (msm_port->is_uartdm) {
		msm_write(port, 0xFFFFFF, UARTDM_DMRX);
		msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
		msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
	}

	msm_write(port, msm_port->imr, UART_IMR);
479 480 481 482 483 484 485 486 487 488
	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 */

489
	clk_disable_unprepare(msm_port->clk);
490 491 492 493 494 495 496 497 498 499 500 501 502 503

	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 已提交
504 505 506
	baud = msm_set_baud_rate(port, baud);
	if (tty_termios_baud_rate(termios))
		tty_termios_encode_baud_rate(termios, baud, baud);
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 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	/* 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;
	if (termios->c_iflag & (BRKINT | PARMRK))
		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);
577 578 579
	struct msm_port *msm_port = UART_TO_MSM(port);
	struct resource *uart_resource;
	struct resource *gsbi_resource;
580 581
	resource_size_t size;

582 583
	uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (unlikely(!uart_resource))
584
		return;
585
	size = resource_size(uart_resource);
586 587 588 589

	release_mem_region(port->mapbase, size);
	iounmap(port->membase);
	port->membase = NULL;
590 591 592 593 594

	if (msm_port->gsbi_base) {
		iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base +
			  GSBI_CONTROL);

595 596
		gsbi_resource = platform_get_resource(pdev,
							IORESOURCE_MEM, 1);
597 598 599 600 601 602 603 604 605

		if (unlikely(!gsbi_resource))
			return;

		size = resource_size(gsbi_resource);
		release_mem_region(gsbi_resource->start, size);
		iounmap(msm_port->gsbi_base);
		msm_port->gsbi_base = NULL;
	}
606 607 608 609
}

static int msm_request_port(struct uart_port *port)
{
610
	struct msm_port *msm_port = UART_TO_MSM(port);
611
	struct platform_device *pdev = to_platform_device(port->dev);
612 613
	struct resource *uart_resource;
	struct resource *gsbi_resource;
614
	resource_size_t size;
615
	int ret;
616

617
	uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
618
	if (unlikely(!uart_resource))
619 620
		return -ENXIO;

621 622 623
	size = resource_size(uart_resource);

	if (!request_mem_region(port->mapbase, size, "msm_serial"))
624 625 626 627
		return -EBUSY;

	port->membase = ioremap(port->mapbase, size);
	if (!port->membase) {
628 629 630 631
		ret = -EBUSY;
		goto fail_release_port;
	}

632
	gsbi_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	/* Is this a GSBI-based port? */
	if (gsbi_resource) {
		size = resource_size(gsbi_resource);

		if (!request_mem_region(gsbi_resource->start, size,
						 "msm_serial")) {
			ret = -EBUSY;
			goto fail_release_port;
		}

		msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
		if (!msm_port->gsbi_base) {
			ret = -EBUSY;
			goto fail_release_gsbi;
		}
648 649 650
	}

	return 0;
651 652 653 654 655 656

fail_release_gsbi:
	release_mem_region(gsbi_resource->start, size);
fail_release_port:
	release_mem_region(port->mapbase, size);
	return ret;
657 658 659 660
}

static void msm_config_port(struct uart_port *port, int flags)
{
661 662
	struct msm_port *msm_port = UART_TO_MSM(port);
	int ret;
663 664
	if (flags & UART_CONFIG_TYPE) {
		port->type = PORT_MSM;
665 666 667
		ret = msm_request_port(port);
		if (ret)
			return;
668
	}
669 670 671 672

	if (msm_port->is_uartdm)
		iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base +
			  GSBI_CONTROL);
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
}

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:
691
		clk_prepare_enable(msm_port->clk);
692
		if (!IS_ERR(msm_port->pclk))
693
			clk_prepare_enable(msm_port->pclk);
694 695
		break;
	case 3:
696
		clk_disable_unprepare(msm_port->clk);
697
		if (!IS_ERR(msm_port->pclk))
698
			clk_disable_unprepare(msm_port->pclk);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
		break;
	default:
		printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
	}
}

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

static struct msm_port msm_uart_ports[] = {
	{
		.uart = {
			.iotype = UPIO_MEM,
			.ops = &msm_uart_pops,
			.flags = UPF_BOOT_AUTOCONF,
731
			.fifosize = 64,
732 733 734 735 736 737 738 739
			.line = 0,
		},
	},
	{
		.uart = {
			.iotype = UPIO_MEM,
			.ops = &msm_uart_pops,
			.flags = UPF_BOOT_AUTOCONF,
740
			.fifosize = 64,
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
			.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

static void msm_console_putchar(struct uart_port *port, int c)
{
766 767 768 769 770
	struct msm_port *msm_port = UART_TO_MSM(port);

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

771 772
	while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
		;
773
	msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
}

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

	spin_lock(&port->lock);
	uart_console_write(port, s, count, msm_console_putchar);
	spin_unlock(&port->lock);
}

static int __init msm_console_setup(struct console *co, char *options)
{
	struct uart_port *port;
795
	struct msm_port *msm_port;
796 797 798 799 800 801
	int baud, flow, bits, parity;

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

	port = get_port_from_line(co->index);
802
	msm_port = UART_TO_MSM(port);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

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

824 825 826 827 828
	if (msm_port->is_uartdm) {
		msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
		msm_write(port, UART_CR_TX_ENABLE, UART_CR);
	}

829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);

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

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 已提交
860 861
static atomic_t msm_uart_next_id = ATOMIC_INIT(0);

862 863 864 865 866
static int __init msm_serial_probe(struct platform_device *pdev)
{
	struct msm_port *msm_port;
	struct resource *resource;
	struct uart_port *port;
R
Roel Kluin 已提交
867
	int irq;
868

D
David Brown 已提交
869 870 871
	if (pdev->id == -1)
		pdev->id = atomic_inc_return(&msm_uart_next_id) - 1;

872 873 874 875 876 877 878 879 880
	if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
		return -ENXIO;

	printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);

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

881
	if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
882 883 884 885 886
		msm_port->is_uartdm = 1;
	else
		msm_port->is_uartdm = 0;

	if (msm_port->is_uartdm) {
887 888
		msm_port->clk = devm_clk_get(&pdev->dev, "gsbi_uart_clk");
		msm_port->pclk = devm_clk_get(&pdev->dev, "gsbi_pclk");
889
	} else {
890
		msm_port->clk = devm_clk_get(&pdev->dev, "uart_clk");
891 892 893
		msm_port->pclk = ERR_PTR(-ENOENT);
	}

894 895 896 897 898 899
	if (IS_ERR(msm_port->clk))
		return PTR_ERR(msm_port->clk);

	if (msm_port->is_uartdm) {
		if (IS_ERR(msm_port->pclk))
			return PTR_ERR(msm_port->pclk);
900

901
		clk_set_rate(msm_port->clk, 1843200);
902
	}
903

904
	port->uartclk = clk_get_rate(msm_port->clk);
905 906
	printk(KERN_INFO "uartclk = %d\n", port->uartclk);

907

908
	resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
909 910 911 912
	if (unlikely(!resource))
		return -ENXIO;
	port->mapbase = resource->start;

R
Roel Kluin 已提交
913 914
	irq = platform_get_irq(pdev, 0);
	if (unlikely(irq < 0))
915
		return -ENXIO;
R
Roel Kluin 已提交
916
	port->irq = irq;
917 918 919 920 921 922

	platform_set_drvdata(pdev, port);

	return uart_add_one_port(&msm_uart_driver, port);
}

B
Bill Pemberton 已提交
923
static int msm_serial_remove(struct platform_device *pdev)
924
{
925
	struct uart_port *port = platform_get_drvdata(pdev);
926

927
	uart_remove_one_port(&msm_uart_driver, port);
928 929 930 931

	return 0;
}

D
David Brown 已提交
932 933 934 935 936
static struct of_device_id msm_match_table[] = {
	{ .compatible = "qcom,msm-uart" },
	{}
};

937 938 939 940 941
static struct platform_driver msm_platform_driver = {
	.remove = msm_serial_remove,
	.driver = {
		.name = "msm_serial",
		.owner = THIS_MODULE,
D
David Brown 已提交
942
		.of_match_table = msm_match_table,
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	},
};

static int __init msm_serial_init(void)
{
	int ret;

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

	ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
	if (unlikely(ret))
		uart_unregister_driver(&msm_uart_driver);

	printk(KERN_INFO "msm_serial: driver initialized\n");

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