sirfsoc_uart.c 48.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Driver for CSR SiRFprimaII onboard UARTs.
 *
 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
 *
 * Licensed under GPLv2 or later.
 */

#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/sysrq.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/of.h>
#include <linux/slab.h>
#include <linux/io.h>
23
#include <linux/of_gpio.h>
24 25 26 27
#include <linux/dmaengine.h>
#include <linux/dma-direction.h>
#include <linux/dma-mapping.h>
#include <linux/sirfsoc_dma.h>
28 29 30 31 32 33 34 35 36 37 38
#include <asm/irq.h>
#include <asm/mach/irq.h>

#include "sirfsoc_uart.h"

static unsigned int
sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count);
static unsigned int
sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count);
static struct uart_driver sirfsoc_uart_drv;

39 40 41
static void sirfsoc_uart_tx_dma_complete_callback(void *param);
static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port);
static void sirfsoc_uart_rx_dma_complete_callback(void *param);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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
static const struct sirfsoc_baudrate_to_regv baudrate_to_regv[] = {
	{4000000, 2359296},
	{3500000, 1310721},
	{3000000, 1572865},
	{2500000, 1245186},
	{2000000, 1572866},
	{1500000, 1245188},
	{1152000, 1638404},
	{1000000, 1572869},
	{921600, 1114120},
	{576000, 1245196},
	{500000, 1245198},
	{460800, 1572876},
	{230400, 1310750},
	{115200, 1310781},
	{57600, 1310843},
	{38400, 1114328},
	{19200, 1114545},
	{9600, 1114979},
};

static struct sirfsoc_uart_port sirfsoc_uart_ports[SIRFSOC_UART_NR] = {
	[0] = {
		.port = {
			.iotype		= UPIO_MEM,
			.flags		= UPF_BOOT_AUTOCONF,
			.line		= 0,
		},
	},
	[1] = {
		.port = {
			.iotype		= UPIO_MEM,
			.flags		= UPF_BOOT_AUTOCONF,
			.line		= 1,
		},
	},
	[2] = {
		.port = {
			.iotype		= UPIO_MEM,
			.flags		= UPF_BOOT_AUTOCONF,
			.line		= 2,
		},
	},
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	[3] = {
		.port = {
			.iotype		= UPIO_MEM,
			.flags		= UPF_BOOT_AUTOCONF,
			.line		= 3,
		},
	},
	[4] = {
		.port = {
			.iotype		= UPIO_MEM,
			.flags		= UPF_BOOT_AUTOCONF,
			.line		= 4,
		},
	},
99 100 101 102 103 104 105 106 107 108
};

static inline struct sirfsoc_uart_port *to_sirfport(struct uart_port *port)
{
	return container_of(port, struct sirfsoc_uart_port, port);
}

static inline unsigned int sirfsoc_uart_tx_empty(struct uart_port *port)
{
	unsigned long reg;
109 110 111 112 113 114
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
	reg = rd_regl(port, ureg->sirfsoc_tx_fifo_status);

	return (reg & ufifo_st->ff_empty(port->line)) ? TIOCSER_TEMT : 0;
115 116 117 118 119
}

static unsigned int sirfsoc_uart_get_mctrl(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
120
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
121
	if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
122
		goto cts_asserted;
123
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
124 125
		if (!(rd_regl(port, ureg->sirfsoc_afc_ctrl) &
						SIRFUART_AFC_CTS_STATUS))
126 127 128
			goto cts_asserted;
		else
			goto cts_deasserted;
129 130 131 132 133
	} else {
		if (!gpio_get_value(sirfport->cts_gpio))
			goto cts_asserted;
		else
			goto cts_deasserted;
134 135 136 137 138 139 140 141 142 143
	}
cts_deasserted:
	return TIOCM_CAR | TIOCM_DSR;
cts_asserted:
	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
}

static void sirfsoc_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
144
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
145 146 147
	unsigned int assert = mctrl & TIOCM_RTS;
	unsigned int val = assert ? SIRFUART_AFC_CTRL_RX_THD : 0x0;
	unsigned int current_val;
148 149 150 151

	if (!sirfport->hw_flow_ctrl || !sirfport->ms_enabled)
		return;
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
152
		current_val = rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0xFF;
153
		val |= current_val;
154
		wr_regl(port, ureg->sirfsoc_afc_ctrl, val);
155 156 157 158 159
	} else {
		if (!val)
			gpio_set_value(sirfport->rts_gpio, 1);
		else
			gpio_set_value(sirfport->rts_gpio, 0);
160 161 162 163 164
	}
}

static void sirfsoc_uart_stop_tx(struct uart_port *port)
{
165
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
166 167
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no)) {
		if (sirfport->tx_dma_state == TX_DMA_RUNNING) {
			dmaengine_pause(sirfport->tx_dma_chan);
			sirfport->tx_dma_state = TX_DMA_PAUSE;
		} else {
			if (!sirfport->is_marco)
				wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) &
				~uint_en->sirfsoc_txfifo_empty_en);
			else
				wr_regl(port, SIRFUART_INT_EN_CLR,
				uint_en->sirfsoc_txfifo_empty_en);
		}
	} else {
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) &
				~uint_en->sirfsoc_txfifo_empty_en);
		else
			wr_regl(port, SIRFUART_INT_EN_CLR,
				uint_en->sirfsoc_txfifo_empty_en);
	}
}

static void sirfsoc_uart_tx_with_dma(struct sirfsoc_uart_port *sirfport)
{
	struct uart_port *port = &sirfport->port;
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
	struct circ_buf *xmit = &port->state->xmit;
	unsigned long tran_size;
	unsigned long tran_start;
	unsigned long pio_tx_size;

	tran_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
	tran_start = (unsigned long)(xmit->buf + xmit->tail);
	if (uart_circ_empty(xmit) || uart_tx_stopped(port) ||
			!tran_size)
		return;
	if (sirfport->tx_dma_state == TX_DMA_PAUSE) {
		dmaengine_resume(sirfport->tx_dma_chan);
		return;
	}
	if (sirfport->tx_dma_state == TX_DMA_RUNNING)
		return;
	if (!sirfport->is_marco)
215
		wr_regl(port, ureg->sirfsoc_int_en_reg,
216 217 218
				rd_regl(port, ureg->sirfsoc_int_en_reg)&
				~(uint_en->sirfsoc_txfifo_empty_en));
	else
219 220
		wr_regl(port, SIRFUART_INT_EN_CLR,
				uint_en->sirfsoc_txfifo_empty_en);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	/*
	 * DMA requires buffer address and buffer length are both aligned with
	 * 4 bytes, so we use PIO for
	 * 1. if address is not aligned with 4bytes, use PIO for the first 1~3
	 * bytes, and move to DMA for the left part aligned with 4bytes
	 * 2. if buffer length is not aligned with 4bytes, use DMA for aligned
	 * part first, move to PIO for the left 1~3 bytes
	 */
	if (tran_size < 4 || BYTES_TO_ALIGN(tran_start)) {
		wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
		wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
			rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)|
			SIRFUART_IO_MODE);
		if (BYTES_TO_ALIGN(tran_start)) {
			pio_tx_size = sirfsoc_uart_pio_tx_chars(sirfport,
				BYTES_TO_ALIGN(tran_start));
			tran_size -= pio_tx_size;
		}
		if (tran_size < 4)
			sirfsoc_uart_pio_tx_chars(sirfport, tran_size);
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg)|
				uint_en->sirfsoc_txfifo_empty_en);
		else
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				uint_en->sirfsoc_txfifo_empty_en);
		wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
	} else {
		/* tx transfer mode switch into dma mode */
		wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_STOP);
		wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
			rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl)&
			~SIRFUART_IO_MODE);
		wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
		tran_size &= ~(0x3);

		sirfport->tx_dma_addr = dma_map_single(port->dev,
			xmit->buf + xmit->tail,
			tran_size, DMA_TO_DEVICE);
		sirfport->tx_dma_desc = dmaengine_prep_slave_single(
			sirfport->tx_dma_chan, sirfport->tx_dma_addr,
			tran_size, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
		if (!sirfport->tx_dma_desc) {
			dev_err(port->dev, "DMA prep slave single fail\n");
			return;
		}
		sirfport->tx_dma_desc->callback =
			sirfsoc_uart_tx_dma_complete_callback;
		sirfport->tx_dma_desc->callback_param = (void *)sirfport;
		sirfport->transfer_size = tran_size;

		dmaengine_submit(sirfport->tx_dma_desc);
		dma_async_issue_pending(sirfport->tx_dma_chan);
		sirfport->tx_dma_state = TX_DMA_RUNNING;
	}
277 278
}

279
static void sirfsoc_uart_start_tx(struct uart_port *port)
280 281
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
282 283
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
284 285 286 287 288 289 290 291 292 293 294 295 296
	if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no))
		sirfsoc_uart_tx_with_dma(sirfport);
	else {
		sirfsoc_uart_pio_tx_chars(sirfport, 1);
		wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_START);
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
					rd_regl(port, ureg->sirfsoc_int_en_reg)|
					uint_en->sirfsoc_txfifo_empty_en);
		else
			wr_regl(port, ureg->sirfsoc_int_en_reg,
					uint_en->sirfsoc_txfifo_empty_en);
	}
297 298 299 300
}

static void sirfsoc_uart_stop_rx(struct uart_port *port)
{
301
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
302 303
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
304

305
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no)) {
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) &
				~(SIRFUART_RX_DMA_INT_EN(port, uint_en) |
				uint_en->sirfsoc_rx_done_en));
		else
			wr_regl(port, SIRFUART_INT_EN_CLR,
					SIRFUART_RX_DMA_INT_EN(port, uint_en)|
					uint_en->sirfsoc_rx_done_en);
		dmaengine_terminate_all(sirfport->rx_dma_chan);
	} else {
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg)&
				~(SIRFUART_RX_IO_INT_EN(port, uint_en)));
		else
			wr_regl(port, SIRFUART_INT_EN_CLR,
					SIRFUART_RX_IO_INT_EN(port, uint_en));
	}
326 327 328 329 330
}

static void sirfsoc_uart_disable_ms(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
331 332
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
333

334 335
	if (!sirfport->hw_flow_ctrl)
		return;
336 337 338 339 340 341 342 343 344 345 346
	sirfport->ms_enabled = false;
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		wr_regl(port, ureg->sirfsoc_afc_ctrl,
				rd_regl(port, ureg->sirfsoc_afc_ctrl) & ~0x3FF);
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
					rd_regl(port, ureg->sirfsoc_int_en_reg)&
					~uint_en->sirfsoc_cts_en);
		else
			wr_regl(port, SIRFUART_INT_EN_CLR,
					uint_en->sirfsoc_cts_en);
347
	} else
348 349 350 351 352 353 354 355 356 357 358
		disable_irq(gpio_to_irq(sirfport->cts_gpio));
}

static irqreturn_t sirfsoc_uart_usp_cts_handler(int irq, void *dev_id)
{
	struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
	struct uart_port *port = &sirfport->port;
	if (gpio_is_valid(sirfport->cts_gpio) && sirfport->ms_enabled)
		uart_handle_cts_change(port,
				!gpio_get_value(sirfport->cts_gpio));
	return IRQ_HANDLED;
359 360 361 362 363
}

static void sirfsoc_uart_enable_ms(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
364 365
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
366

367 368
	if (!sirfport->hw_flow_ctrl)
		return;
369 370 371 372 373 374 375 376 377 378 379 380
	sirfport->ms_enabled = true;
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		wr_regl(port, ureg->sirfsoc_afc_ctrl,
				rd_regl(port, ureg->sirfsoc_afc_ctrl) |
				SIRFUART_AFC_TX_EN | SIRFUART_AFC_RX_EN);
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
					rd_regl(port, ureg->sirfsoc_int_en_reg)
					| uint_en->sirfsoc_cts_en);
		else
			wr_regl(port, ureg->sirfsoc_int_en_reg,
					uint_en->sirfsoc_cts_en);
381
	} else
382
		enable_irq(gpio_to_irq(sirfport->cts_gpio));
383 384 385 386
}

static void sirfsoc_uart_break_ctl(struct uart_port *port, int break_state)
{
387 388 389 390 391 392 393 394 395 396
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		unsigned long ulcon = rd_regl(port, ureg->sirfsoc_line_ctrl);
		if (break_state)
			ulcon |= SIRFUART_SET_BREAK;
		else
			ulcon &= ~SIRFUART_SET_BREAK;
		wr_regl(port, ureg->sirfsoc_line_ctrl, ulcon);
	}
397 398 399 400 401
}

static unsigned int
sirfsoc_uart_pio_rx_chars(struct uart_port *port, unsigned int max_rx_count)
{
402 403 404
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
405
	unsigned int ch, rx_count = 0;
406 407 408 409 410 411 412 413
	struct tty_struct *tty;
	tty = tty_port_tty_get(&port->state->port);
	if (!tty)
		return -ENODEV;
	while (!(rd_regl(port, ureg->sirfsoc_rx_fifo_status) &
					ufifo_st->ff_empty(port->line))) {
		ch = rd_regl(port, ureg->sirfsoc_rx_fifo_data) |
			SIRFUART_DUMMY_READ;
414 415 416 417 418 419 420 421
		if (unlikely(uart_handle_sysrq_char(port, ch)))
			continue;
		uart_insert_char(port, 0, 0, ch, TTY_NORMAL);
		rx_count++;
		if (rx_count >= max_rx_count)
			break;
	}

422
	sirfport->rx_io_count += rx_count;
423
	port->icount.rx += rx_count;
424 425

	spin_unlock(&port->lock);
J
Jiri Slaby 已提交
426
	tty_flip_buffer_push(&port->state->port);
427
	spin_lock(&port->lock);
428 429 430 431 432 433 434 435

	return rx_count;
}

static unsigned int
sirfsoc_uart_pio_tx_chars(struct sirfsoc_uart_port *sirfport, int count)
{
	struct uart_port *port = &sirfport->port;
436 437
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
438 439 440
	struct circ_buf *xmit = &port->state->xmit;
	unsigned int num_tx = 0;
	while (!uart_circ_empty(xmit) &&
441 442
		!(rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
					ufifo_st->ff_full(port->line)) &&
443
		count--) {
444 445
		wr_regl(port, ureg->sirfsoc_tx_fifo_data,
				xmit->buf[xmit->tail]);
446 447 448 449 450 451 452 453 454
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		port->icount.tx++;
		num_tx++;
	}
	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);
	return num_tx;
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 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 497 498 499 500 501 502 503 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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
static void sirfsoc_uart_tx_dma_complete_callback(void *param)
{
	struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
	struct uart_port *port = &sirfport->port;
	struct circ_buf *xmit = &port->state->xmit;
	unsigned long flags;

	xmit->tail = (xmit->tail + sirfport->transfer_size) &
				(UART_XMIT_SIZE - 1);
	port->icount.tx += sirfport->transfer_size;
	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);
	if (sirfport->tx_dma_addr)
		dma_unmap_single(port->dev, sirfport->tx_dma_addr,
				sirfport->transfer_size, DMA_TO_DEVICE);
	spin_lock_irqsave(&sirfport->tx_lock, flags);
	sirfport->tx_dma_state = TX_DMA_IDLE;
	sirfsoc_uart_tx_with_dma(sirfport);
	spin_unlock_irqrestore(&sirfport->tx_lock, flags);
}

static void sirfsoc_uart_insert_rx_buf_to_tty(
		struct sirfsoc_uart_port *sirfport, int count)
{
	struct uart_port *port = &sirfport->port;
	struct tty_port *tport = &port->state->port;
	int inserted;

	inserted = tty_insert_flip_string(tport,
		sirfport->rx_dma_items[sirfport->rx_completed].xmit.buf, count);
	port->icount.rx += inserted;
	tty_flip_buffer_push(tport);
}

static void sirfsoc_rx_submit_one_dma_desc(struct uart_port *port, int index)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);

	sirfport->rx_dma_items[index].xmit.tail =
		sirfport->rx_dma_items[index].xmit.head = 0;
	sirfport->rx_dma_items[index].desc =
		dmaengine_prep_slave_single(sirfport->rx_dma_chan,
		sirfport->rx_dma_items[index].dma_addr, SIRFSOC_RX_DMA_BUF_SIZE,
		DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
	if (!sirfport->rx_dma_items[index].desc) {
		dev_err(port->dev, "DMA slave single fail\n");
		return;
	}
	sirfport->rx_dma_items[index].desc->callback =
		sirfsoc_uart_rx_dma_complete_callback;
	sirfport->rx_dma_items[index].desc->callback_param = sirfport;
	sirfport->rx_dma_items[index].cookie =
		dmaengine_submit(sirfport->rx_dma_items[index].desc);
	dma_async_issue_pending(sirfport->rx_dma_chan);
}

static void sirfsoc_rx_tmo_process_tl(unsigned long param)
{
	struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
	struct uart_port *port = &sirfport->port;
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
	struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
	unsigned int count;
	unsigned long flags;

	spin_lock_irqsave(&sirfport->rx_lock, flags);
	while (sirfport->rx_completed != sirfport->rx_issued) {
		sirfsoc_uart_insert_rx_buf_to_tty(sirfport,
					SIRFSOC_RX_DMA_BUF_SIZE);
		sirfsoc_rx_submit_one_dma_desc(port, sirfport->rx_completed++);
		sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT;
	}
	count = CIRC_CNT(sirfport->rx_dma_items[sirfport->rx_issued].xmit.head,
		sirfport->rx_dma_items[sirfport->rx_issued].xmit.tail,
		SIRFSOC_RX_DMA_BUF_SIZE);
	if (count > 0)
		sirfsoc_uart_insert_rx_buf_to_tty(sirfport, count);
	wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
			rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
			SIRFUART_IO_MODE);
	sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count);
	spin_unlock_irqrestore(&sirfport->rx_lock, flags);
	if (sirfport->rx_io_count == 4) {
		spin_lock_irqsave(&sirfport->rx_lock, flags);
		sirfport->rx_io_count = 0;
		wr_regl(port, ureg->sirfsoc_int_st_reg,
				uint_st->sirfsoc_rx_done);
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) &
				~(uint_en->sirfsoc_rx_done_en));
		else
			wr_regl(port, SIRFUART_INT_EN_CLR,
					uint_en->sirfsoc_rx_done_en);
		spin_unlock_irqrestore(&sirfport->rx_lock, flags);

		sirfsoc_uart_start_next_rx_dma(port);
	} else {
		spin_lock_irqsave(&sirfport->rx_lock, flags);
		wr_regl(port, ureg->sirfsoc_int_st_reg,
				uint_st->sirfsoc_rx_done);
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) |
				(uint_en->sirfsoc_rx_done_en));
		else
			wr_regl(port, ureg->sirfsoc_int_en_reg,
					uint_en->sirfsoc_rx_done_en);
		spin_unlock_irqrestore(&sirfport->rx_lock, flags);
	}
}

static void sirfsoc_uart_handle_rx_tmo(struct sirfsoc_uart_port *sirfport)
{
	struct uart_port *port = &sirfport->port;
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
	struct dma_tx_state tx_state;
	spin_lock(&sirfport->rx_lock);

	dmaengine_tx_status(sirfport->rx_dma_chan,
		sirfport->rx_dma_items[sirfport->rx_issued].cookie, &tx_state);
	dmaengine_terminate_all(sirfport->rx_dma_chan);
	sirfport->rx_dma_items[sirfport->rx_issued].xmit.head =
		SIRFSOC_RX_DMA_BUF_SIZE - tx_state.residue;
	if (!sirfport->is_marco)
		wr_regl(port, ureg->sirfsoc_int_en_reg,
			rd_regl(port, ureg->sirfsoc_int_en_reg) &
			~(uint_en->sirfsoc_rx_timeout_en));
	else
		wr_regl(port, SIRFUART_INT_EN_CLR,
				uint_en->sirfsoc_rx_timeout_en);
	spin_unlock(&sirfport->rx_lock);
	tasklet_schedule(&sirfport->rx_tmo_process_tasklet);
}

static void sirfsoc_uart_handle_rx_done(struct sirfsoc_uart_port *sirfport)
{
	struct uart_port *port = &sirfport->port;
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
	struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;

	sirfsoc_uart_pio_rx_chars(port, 4 - sirfport->rx_io_count);
	if (sirfport->rx_io_count == 4) {
		sirfport->rx_io_count = 0;
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) &
				~(uint_en->sirfsoc_rx_done_en));
		else
			wr_regl(port, SIRFUART_INT_EN_CLR,
					uint_en->sirfsoc_rx_done_en);
		wr_regl(port, ureg->sirfsoc_int_st_reg,
				uint_st->sirfsoc_rx_timeout);
		sirfsoc_uart_start_next_rx_dma(port);
	}
}

615 616 617 618 619 620 621
static irqreturn_t sirfsoc_uart_isr(int irq, void *dev_id)
{
	unsigned long intr_status;
	unsigned long cts_status;
	unsigned long flag = TTY_NORMAL;
	struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)dev_id;
	struct uart_port *port = &sirfport->port;
622 623 624 625
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
	struct sirfsoc_int_status *uint_st = &sirfport->uart_reg->uart_int_st;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
626 627
	struct uart_state *state = port->state;
	struct circ_buf *xmit = &port->state->xmit;
628
	spin_lock(&port->lock);
629 630
	intr_status = rd_regl(port, ureg->sirfsoc_int_st_reg);
	wr_regl(port, ureg->sirfsoc_int_st_reg, intr_status);
631
	intr_status &= rd_regl(port, ureg->sirfsoc_int_en_reg);
632 633 634
	if (unlikely(intr_status & (SIRFUART_ERR_INT_STAT(port, uint_st)))) {
		if (intr_status & uint_st->sirfsoc_rxd_brk) {
			port->icount.brk++;
635 636 637
			if (uart_handle_break(port))
				goto recv_char;
		}
638
		if (intr_status & uint_st->sirfsoc_rx_oflow)
639
			port->icount.overrun++;
640
		if (intr_status & uint_st->sirfsoc_frm_err) {
641 642 643
			port->icount.frame++;
			flag = TTY_FRAME;
		}
644
		if (intr_status & uint_st->sirfsoc_parity_err)
645
			flag = TTY_PARITY;
646 647 648
		wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
		wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
		wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
649 650
		intr_status &= port->read_status_mask;
		uart_insert_char(port, intr_status,
651 652
					uint_en->sirfsoc_rx_oflow_en, 0, flag);
		tty_flip_buffer_push(&state->port);
653 654
	}
recv_char:
655
	if ((sirfport->uart_reg->uart_type == SIRF_REAL_UART) &&
656 657
			(intr_status & SIRFUART_CTS_INT_ST(uint_st)) &&
			!sirfport->tx_dma_state) {
658 659 660 661 662 663 664 665
		cts_status = rd_regl(port, ureg->sirfsoc_afc_ctrl) &
					SIRFUART_AFC_CTS_STATUS;
		if (cts_status != 0)
			cts_status = 0;
		else
			cts_status = 1;
		uart_handle_cts_change(port, cts_status);
		wake_up_interruptible(&state->port.delta_msr_wait);
666
	}
667 668 669 670 671 672 673 674 675 676
	if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no)) {
		if (intr_status & uint_st->sirfsoc_rx_timeout)
			sirfsoc_uart_handle_rx_tmo(sirfport);
		if (intr_status & uint_st->sirfsoc_rx_done)
			sirfsoc_uart_handle_rx_done(sirfport);
	} else {
		if (intr_status & SIRFUART_RX_IO_INT_ST(uint_st))
			sirfsoc_uart_pio_rx_chars(port,
					SIRFSOC_UART_IO_RX_MAX_CNT);
	}
677
	if (intr_status & uint_st->sirfsoc_txfifo_empty) {
678 679 680 681 682 683 684 685
		if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no))
			sirfsoc_uart_tx_with_dma(sirfport);
		else {
			if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
				spin_unlock(&port->lock);
				return IRQ_HANDLED;
			} else {
				sirfsoc_uart_pio_tx_chars(sirfport,
686
					SIRFSOC_UART_IO_TX_REASONABLE_CNT);
687
				if ((uart_circ_empty(xmit)) &&
688
				(rd_regl(port, ureg->sirfsoc_tx_fifo_status) &
689 690 691
				ufifo_st->ff_empty(port->line)))
					sirfsoc_uart_stop_tx(port);
			}
692 693
		}
	}
694
	spin_unlock(&port->lock);
695 696 697
	return IRQ_HANDLED;
}

698 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
static void sirfsoc_uart_rx_dma_complete_tl(unsigned long param)
{
	struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
	struct uart_port *port = &sirfport->port;
	unsigned long flags;
	spin_lock_irqsave(&sirfport->rx_lock, flags);
	while (sirfport->rx_completed != sirfport->rx_issued) {
		sirfsoc_uart_insert_rx_buf_to_tty(sirfport,
					SIRFSOC_RX_DMA_BUF_SIZE);
		sirfsoc_rx_submit_one_dma_desc(port, sirfport->rx_completed++);
		sirfport->rx_completed %= SIRFSOC_RX_LOOP_BUF_CNT;
	}
	spin_unlock_irqrestore(&sirfport->rx_lock, flags);
}

static void sirfsoc_uart_rx_dma_complete_callback(void *param)
{
	struct sirfsoc_uart_port *sirfport = (struct sirfsoc_uart_port *)param;
	spin_lock(&sirfport->rx_lock);
	sirfport->rx_issued++;
	sirfport->rx_issued %= SIRFSOC_RX_LOOP_BUF_CNT;
	spin_unlock(&sirfport->rx_lock);
	tasklet_schedule(&sirfport->rx_dma_complete_tasklet);
}

/* submit rx dma task into dmaengine */
static void sirfsoc_uart_start_next_rx_dma(struct uart_port *port)
725
{
726
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
727 728
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
729 730 731 732 733 734 735 736 737 738 739 740 741
	unsigned long flags;
	int i;
	spin_lock_irqsave(&sirfport->rx_lock, flags);
	sirfport->rx_io_count = 0;
	wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
		rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) &
		~SIRFUART_IO_MODE);
	spin_unlock_irqrestore(&sirfport->rx_lock, flags);
	for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++)
		sirfsoc_rx_submit_one_dma_desc(port, i);
	sirfport->rx_completed = sirfport->rx_issued = 0;
	spin_lock_irqsave(&sirfport->rx_lock, flags);
	if (!sirfport->is_marco)
742
		wr_regl(port, ureg->sirfsoc_int_en_reg,
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
				rd_regl(port, ureg->sirfsoc_int_en_reg) |
				SIRFUART_RX_DMA_INT_EN(port, uint_en));
	else
		wr_regl(port, ureg->sirfsoc_int_en_reg,
			SIRFUART_RX_DMA_INT_EN(port, uint_en));
	spin_unlock_irqrestore(&sirfport->rx_lock, flags);
}

static void sirfsoc_uart_start_rx(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;

	sirfport->rx_io_count = 0;
758 759 760
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_START);
761 762 763 764 765 766 767 768 769 770 771
	if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no))
		sirfsoc_uart_start_next_rx_dma(port);
	else {
		if (!sirfport->is_marco)
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				rd_regl(port, ureg->sirfsoc_int_en_reg) |
				SIRFUART_RX_IO_INT_EN(port, uint_en));
		else
			wr_regl(port, ureg->sirfsoc_int_en_reg,
				SIRFUART_RX_IO_INT_EN(port, uint_en));
	}
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
}

static unsigned int
sirfsoc_usp_calc_sample_div(unsigned long set_rate,
		unsigned long ioclk_rate, unsigned long *sample_reg)
{
	unsigned long min_delta = ~0UL;
	unsigned short sample_div;
	unsigned long ioclk_div = 0;
	unsigned long temp_delta;

	for (sample_div = SIRF_MIN_SAMPLE_DIV;
			sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
		temp_delta = ioclk_rate -
		(ioclk_rate + (set_rate * sample_div) / 2)
		/ (set_rate * sample_div) * set_rate * sample_div;
788

789 790 791 792 793 794 795 796 797 798 799 800 801
		temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
		if (temp_delta < min_delta) {
			ioclk_div = (2 * ioclk_rate /
				(set_rate * sample_div) + 1) / 2 - 1;
			if (ioclk_div > SIRF_IOCLK_DIV_MAX)
				continue;
			min_delta = temp_delta;
			*sample_reg = sample_div;
			if (!temp_delta)
				break;
		}
	}
	return ioclk_div;
802 803 804
}

static unsigned int
805 806
sirfsoc_uart_calc_sample_div(unsigned long baud_rate,
			unsigned long ioclk_rate, unsigned long *set_baud)
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
{
	unsigned long min_delta = ~0UL;
	unsigned short sample_div;
	unsigned int regv = 0;
	unsigned long ioclk_div;
	unsigned long baud_tmp;
	int temp_delta;

	for (sample_div = SIRF_MIN_SAMPLE_DIV;
			sample_div <= SIRF_MAX_SAMPLE_DIV; sample_div++) {
		ioclk_div = (ioclk_rate / (baud_rate * (sample_div + 1))) - 1;
		if (ioclk_div > SIRF_IOCLK_DIV_MAX)
			continue;
		baud_tmp = ioclk_rate / ((ioclk_div + 1) * (sample_div + 1));
		temp_delta = baud_tmp - baud_rate;
		temp_delta = (temp_delta > 0) ? temp_delta : -temp_delta;
		if (temp_delta < min_delta) {
			regv = regv & (~SIRF_IOCLK_DIV_MASK);
			regv = regv | ioclk_div;
			regv = regv & (~SIRF_SAMPLE_DIV_MASK);
			regv = regv | (sample_div << SIRF_SAMPLE_DIV_SHIFT);
			min_delta = temp_delta;
829
			*set_baud = baud_tmp;
830 831 832 833 834 835 836 837 838 839
		}
	}
	return regv;
}

static void sirfsoc_uart_set_termios(struct uart_port *port,
				       struct ktermios *termios,
				       struct ktermios *old)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
840 841
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_int_en *uint_en = &sirfport->uart_reg->uart_int_en;
842 843
	unsigned long	config_reg = 0;
	unsigned long	baud_rate;
844
	unsigned long	set_baud;
845 846 847
	unsigned long	flags;
	unsigned long	ic;
	unsigned int	clk_div_reg = 0;
848
	unsigned long	txfifo_op_reg, ioclk_rate;
849 850
	unsigned long	rx_time_out;
	int		threshold_div;
851 852 853
	u32		data_bit_len, stop_bit_len, len_val;
	unsigned long	sample_div_reg = 0xf;
	ioclk_rate	= port->uartclk;
854 855 856 857

	switch (termios->c_cflag & CSIZE) {
	default:
	case CS8:
858
		data_bit_len = 8;
859 860 861
		config_reg |= SIRFUART_DATA_BIT_LEN_8;
		break;
	case CS7:
862
		data_bit_len = 7;
863 864 865
		config_reg |= SIRFUART_DATA_BIT_LEN_7;
		break;
	case CS6:
866
		data_bit_len = 6;
867 868 869
		config_reg |= SIRFUART_DATA_BIT_LEN_6;
		break;
	case CS5:
870
		data_bit_len = 5;
871 872 873
		config_reg |= SIRFUART_DATA_BIT_LEN_5;
		break;
	}
874
	if (termios->c_cflag & CSTOPB) {
875
		config_reg |= SIRFUART_STOP_BIT_LEN_2;
876 877 878 879
		stop_bit_len = 2;
	} else
		stop_bit_len = 1;

880
	spin_lock_irqsave(&port->lock, flags);
881
	port->read_status_mask = uint_en->sirfsoc_rx_oflow_en;
882
	port->ignore_status_mask = 0;
883 884 885 886
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		if (termios->c_iflag & INPCK)
			port->read_status_mask |= uint_en->sirfsoc_frm_err_en |
				uint_en->sirfsoc_parity_err_en;
887
	} else {
888 889 890
		if (termios->c_iflag & INPCK)
			port->read_status_mask |= uint_en->sirfsoc_frm_err_en;
	}
891
	if (termios->c_iflag & (BRKINT | PARMRK))
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
			port->read_status_mask |= uint_en->sirfsoc_rxd_brk_en;
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		if (termios->c_iflag & IGNPAR)
			port->ignore_status_mask |=
				uint_en->sirfsoc_frm_err_en |
				uint_en->sirfsoc_parity_err_en;
		if (termios->c_cflag & PARENB) {
			if (termios->c_cflag & CMSPAR) {
				if (termios->c_cflag & PARODD)
					config_reg |= SIRFUART_STICK_BIT_MARK;
				else
					config_reg |= SIRFUART_STICK_BIT_SPACE;
			} else if (termios->c_cflag & PARODD) {
				config_reg |= SIRFUART_STICK_BIT_ODD;
			} else {
				config_reg |= SIRFUART_STICK_BIT_EVEN;
			}
		}
910
	} else {
911 912 913 914 915 916 917 918
		if (termios->c_iflag & IGNPAR)
			port->ignore_status_mask |=
				uint_en->sirfsoc_frm_err_en;
		if (termios->c_cflag & PARENB)
			dev_warn(port->dev,
					"USP-UART not support parity err\n");
	}
	if (termios->c_iflag & IGNBRK) {
919
		port->ignore_status_mask |=
920 921 922 923 924
			uint_en->sirfsoc_rxd_brk_en;
		if (termios->c_iflag & IGNPAR)
			port->ignore_status_mask |=
				uint_en->sirfsoc_rx_oflow_en;
	}
925 926 927 928 929 930 931 932 933 934
	if ((termios->c_cflag & CREAD) == 0)
		port->ignore_status_mask |= SIRFUART_DUMMY_READ;
	/* Hardware Flow Control Settings */
	if (UART_ENABLE_MS(port, termios->c_cflag)) {
		if (!sirfport->ms_enabled)
			sirfsoc_uart_enable_ms(port);
	} else {
		if (sirfport->ms_enabled)
			sirfsoc_uart_disable_ms(port);
	}
935 936
	baud_rate = uart_get_baud_rate(port, termios, old, 0, 4000000);
	if (ioclk_rate == 150000000) {
937 938 939 940
		for (ic = 0; ic < SIRF_BAUD_RATE_SUPPORT_NR; ic++)
			if (baud_rate == baudrate_to_regv[ic].baud_rate)
				clk_div_reg = baudrate_to_regv[ic].reg_val;
	}
941 942 943 944 945 946
	set_baud = baud_rate;
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		if (unlikely(clk_div_reg == 0))
			clk_div_reg = sirfsoc_uart_calc_sample_div(baud_rate,
					ioclk_rate, &set_baud);
		wr_regl(port, ureg->sirfsoc_divisor, clk_div_reg);
947
	} else {
948 949 950 951 952 953 954 955 956 957
		clk_div_reg = sirfsoc_usp_calc_sample_div(baud_rate,
				ioclk_rate, &sample_div_reg);
		sample_div_reg--;
		set_baud = ((ioclk_rate / (clk_div_reg+1) - 1) /
				(sample_div_reg + 1));
		/* setting usp mode 2 */
		len_val = ((1 << 0) | (1 << 8));
		len_val |= ((clk_div_reg & 0x3ff) << 21);
		wr_regl(port, ureg->sirfsoc_mode2,
				len_val);
958

959
	}
960
	if (tty_termios_baud_rate(termios))
961 962 963 964
		tty_termios_encode_baud_rate(termios, set_baud, set_baud);
	/* set receive timeout && data bits len */
	rx_time_out = SIRFSOC_UART_RX_TIMEOUT(set_baud, 20000);
	rx_time_out = SIRFUART_RECV_TIMEOUT_VALUE(rx_time_out);
965
	txfifo_op_reg = rd_regl(port, ureg->sirfsoc_tx_fifo_op);
966 967
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
	wr_regl(port, ureg->sirfsoc_tx_fifo_op,
968
			(txfifo_op_reg & ~SIRFUART_FIFO_START));
969 970 971
	if (sirfport->uart_reg->uart_type == SIRF_REAL_UART) {
		config_reg |= SIRFUART_RECV_TIMEOUT(port, rx_time_out);
		wr_regl(port, ureg->sirfsoc_line_ctrl, config_reg);
972
	} else {
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
		/*tx frame ctrl*/
		len_val = (data_bit_len - 1) << 0;
		len_val |= (data_bit_len + 1 + stop_bit_len - 1) << 16;
		len_val |= ((data_bit_len - 1) << 24);
		len_val |= (((clk_div_reg & 0xc00) >> 10) << 30);
		wr_regl(port, ureg->sirfsoc_tx_frame_ctrl, len_val);
		/*rx frame ctrl*/
		len_val = (data_bit_len - 1) << 0;
		len_val |= (data_bit_len + 1 + stop_bit_len - 1) << 8;
		len_val |= (data_bit_len - 1) << 16;
		len_val |= (((clk_div_reg & 0xf000) >> 12) << 24);
		wr_regl(port, ureg->sirfsoc_rx_frame_ctrl, len_val);
		/*async param*/
		wr_regl(port, ureg->sirfsoc_async_param_reg,
			(SIRFUART_RECV_TIMEOUT(port, rx_time_out)) |
			(sample_div_reg & 0x3f) << 16);
	}
990 991 992 993 994 995 996 997
	if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no))
		wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_DMA_MODE);
	else
		wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl, SIRFUART_IO_MODE);
	if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no))
		wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_DMA_MODE);
	else
		wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl, SIRFUART_IO_MODE);
998
	/* Reset Rx/Tx FIFO Threshold level for proper baudrate */
999
	if (set_baud < 1000000)
1000 1001 1002
		threshold_div = 1;
	else
		threshold_div = 2;
1003 1004 1005 1006 1007 1008
	wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl,
				SIRFUART_FIFO_THD(port) / threshold_div);
	wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl,
				SIRFUART_FIFO_THD(port) / threshold_div);
	txfifo_op_reg |= SIRFUART_FIFO_START;
	wr_regl(port, ureg->sirfsoc_tx_fifo_op, txfifo_op_reg);
1009
	uart_update_timeout(port, termios->c_cflag, set_baud);
1010
	sirfsoc_uart_start_rx(port);
1011
	wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_TX_EN | SIRFUART_RX_EN);
1012 1013 1014
	spin_unlock_irqrestore(&port->lock, flags);
}

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
static unsigned int sirfsoc_uart_init_tx_dma(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	dma_cap_mask_t dma_mask;
	struct dma_slave_config tx_slv_cfg = {
		.dst_maxburst = 2,
	};

	dma_cap_zero(dma_mask);
	dma_cap_set(DMA_SLAVE, dma_mask);
	sirfport->tx_dma_chan = dma_request_channel(dma_mask,
		(dma_filter_fn)sirfsoc_dma_filter_id,
		(void *)sirfport->tx_dma_no);
	if (!sirfport->tx_dma_chan) {
		dev_err(port->dev, "Uart Request Dma Channel Fail %d\n",
					sirfport->tx_dma_no);
		return  -EPROBE_DEFER;
	}
	dmaengine_slave_config(sirfport->tx_dma_chan, &tx_slv_cfg);

	return 0;
}

static unsigned int sirfsoc_uart_init_rx_dma(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	dma_cap_mask_t dma_mask;
	int ret;
	int i, j;
	struct dma_slave_config slv_cfg = {
		.src_maxburst = 2,
	};

	dma_cap_zero(dma_mask);
	dma_cap_set(DMA_SLAVE, dma_mask);
	sirfport->rx_dma_chan = dma_request_channel(dma_mask,
					(dma_filter_fn)sirfsoc_dma_filter_id,
					(void *)sirfport->rx_dma_no);
	if (!sirfport->rx_dma_chan) {
		dev_err(port->dev, "Uart Request Dma Channel Fail %d\n",
				sirfport->rx_dma_no);
		ret = -EPROBE_DEFER;
		goto request_err;
	}
	for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++) {
		sirfport->rx_dma_items[i].xmit.buf =
			dma_alloc_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
			&sirfport->rx_dma_items[i].dma_addr, GFP_KERNEL);
		if (!sirfport->rx_dma_items[i].xmit.buf) {
			dev_err(port->dev, "Uart alloc bufa failed\n");
			ret = -ENOMEM;
			goto alloc_coherent_err;
		}
		sirfport->rx_dma_items[i].xmit.head =
			sirfport->rx_dma_items[i].xmit.tail = 0;
	}
	dmaengine_slave_config(sirfport->rx_dma_chan, &slv_cfg);

	return 0;
alloc_coherent_err:
	for (j = 0; j < i; j++)
		dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
				sirfport->rx_dma_items[j].xmit.buf,
				sirfport->rx_dma_items[j].dma_addr);
	dma_release_channel(sirfport->rx_dma_chan);
request_err:
	return ret;
}

static void sirfsoc_uart_uninit_tx_dma(struct sirfsoc_uart_port *sirfport)
{
	dmaengine_terminate_all(sirfport->tx_dma_chan);
	dma_release_channel(sirfport->tx_dma_chan);
}

static void sirfsoc_uart_uninit_rx_dma(struct sirfsoc_uart_port *sirfport)
{
	int i;
	struct uart_port *port = &sirfport->port;
	dmaengine_terminate_all(sirfport->rx_dma_chan);
	dma_release_channel(sirfport->rx_dma_chan);
	for (i = 0; i < SIRFSOC_RX_LOOP_BUF_CNT; i++)
		dma_free_coherent(port->dev, SIRFSOC_RX_DMA_BUF_SIZE,
				sirfport->rx_dma_items[i].xmit.buf,
				sirfport->rx_dma_items[i].dma_addr);
}

1102 1103 1104
static int sirfsoc_uart_startup(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport	= to_sirfport(port);
1105
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	unsigned int index			= port->line;
	int ret;
	set_irq_flags(port->irq, IRQF_VALID | IRQF_NOAUTOEN);
	ret = request_irq(port->irq,
				sirfsoc_uart_isr,
				0,
				SIRFUART_PORT_NAME,
				sirfport);
	if (ret != 0) {
		dev_err(port->dev, "UART%d request IRQ line (%d) failed.\n",
							index, port->irq);
		goto irq_err;
	}
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

	/* initial hardware settings */
	wr_regl(port, ureg->sirfsoc_tx_dma_io_ctrl,
		rd_regl(port, ureg->sirfsoc_tx_dma_io_ctrl) |
		SIRFUART_IO_MODE);
	wr_regl(port, ureg->sirfsoc_rx_dma_io_ctrl,
		rd_regl(port, ureg->sirfsoc_rx_dma_io_ctrl) |
		SIRFUART_IO_MODE);
	wr_regl(port, ureg->sirfsoc_tx_dma_io_len, 0);
	wr_regl(port, ureg->sirfsoc_rx_dma_io_len, 0);
	wr_regl(port, ureg->sirfsoc_tx_rx_en, SIRFUART_RX_EN | SIRFUART_TX_EN);
	if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
		wr_regl(port, ureg->sirfsoc_mode1,
			SIRFSOC_USP_ENDIAN_CTRL_LSBF |
			SIRFSOC_USP_EN);
	wr_regl(port, ureg->sirfsoc_tx_fifo_op, SIRFUART_FIFO_RESET);
	wr_regl(port, ureg->sirfsoc_tx_fifo_op, 0);
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, SIRFUART_FIFO_RESET);
	wr_regl(port, ureg->sirfsoc_rx_fifo_op, 0);
	wr_regl(port, ureg->sirfsoc_tx_fifo_ctrl, SIRFUART_FIFO_THD(port));
	wr_regl(port, ureg->sirfsoc_rx_fifo_ctrl, SIRFUART_FIFO_THD(port));
1140

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no)) {
		ret = sirfsoc_uart_init_rx_dma(port);
		if (ret)
			goto init_rx_err;
		wr_regl(port, ureg->sirfsoc_rx_fifo_level_chk,
				SIRFUART_RX_FIFO_CHK_SC(port->line, 0x4) |
				SIRFUART_RX_FIFO_CHK_LC(port->line, 0xe) |
				SIRFUART_RX_FIFO_CHK_HC(port->line, 0x1b));
	}
	if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no)) {
		sirfsoc_uart_init_tx_dma(port);
		sirfport->tx_dma_state = TX_DMA_IDLE;
		wr_regl(port, ureg->sirfsoc_tx_fifo_level_chk,
				SIRFUART_TX_FIFO_CHK_SC(port->line, 0x1b) |
				SIRFUART_TX_FIFO_CHK_LC(port->line, 0xe) |
				SIRFUART_TX_FIFO_CHK_HC(port->line, 0x4));
	}
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	sirfport->ms_enabled = false;
	if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
		sirfport->hw_flow_ctrl) {
		set_irq_flags(gpio_to_irq(sirfport->cts_gpio),
			IRQF_VALID | IRQF_NOAUTOEN);
		ret = request_irq(gpio_to_irq(sirfport->cts_gpio),
			sirfsoc_uart_usp_cts_handler, IRQF_TRIGGER_FALLING |
			IRQF_TRIGGER_RISING, "usp_cts_irq", sirfport);
		if (ret != 0) {
			dev_err(port->dev, "UART-USP:request gpio irq fail\n");
			goto init_rx_err;
		}
	}

1172
	enable_irq(port->irq);
1173

1174
	return 0;
1175 1176
init_rx_err:
	free_irq(port->irq, sirfport);
1177 1178 1179 1180 1181 1182 1183
irq_err:
	return ret;
}

static void sirfsoc_uart_shutdown(struct uart_port *port)
{
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
1184
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1185
	if (!sirfport->is_marco)
1186
		wr_regl(port, ureg->sirfsoc_int_en_reg, 0);
1187 1188 1189
	else
		wr_regl(port, SIRFUART_INT_EN_CLR, ~0UL);

1190
	free_irq(port->irq, sirfport);
1191
	if (sirfport->ms_enabled)
1192
		sirfsoc_uart_disable_ms(port);
1193 1194 1195 1196
	if (sirfport->uart_reg->uart_type == SIRF_USP_UART &&
			sirfport->hw_flow_ctrl) {
		gpio_set_value(sirfport->rts_gpio, 1);
		free_irq(gpio_to_irq(sirfport->cts_gpio), sirfport);
1197
	}
1198 1199 1200 1201 1202 1203
	if (IS_DMA_CHAN_VALID(sirfport->rx_dma_no))
		sirfsoc_uart_uninit_rx_dma(sirfport);
	if (IS_DMA_CHAN_VALID(sirfport->tx_dma_no)) {
		sirfsoc_uart_uninit_tx_dma(sirfport);
		sirfport->tx_dma_state = TX_DMA_IDLE;
	}
1204 1205 1206 1207 1208 1209 1210 1211 1212
}

static const char *sirfsoc_uart_type(struct uart_port *port)
{
	return port->type == SIRFSOC_PORT_TYPE ? SIRFUART_PORT_NAME : NULL;
}

static int sirfsoc_uart_request_port(struct uart_port *port)
{
1213 1214
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_uart_param *uart_param = &sirfport->uart_reg->uart_param;
1215 1216
	void *ret;
	ret = request_mem_region(port->mapbase,
1217
		SIRFUART_MAP_SIZE, uart_param->port_name);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
	return ret ? 0 : -EBUSY;
}

static void sirfsoc_uart_release_port(struct uart_port *port)
{
	release_mem_region(port->mapbase, SIRFUART_MAP_SIZE);
}

static void sirfsoc_uart_config_port(struct uart_port *port, int flags)
{
	if (flags & UART_CONFIG_TYPE) {
		port->type = SIRFSOC_PORT_TYPE;
		sirfsoc_uart_request_port(port);
	}
}

static struct uart_ops sirfsoc_uart_ops = {
	.tx_empty	= sirfsoc_uart_tx_empty,
	.get_mctrl	= sirfsoc_uart_get_mctrl,
	.set_mctrl	= sirfsoc_uart_set_mctrl,
	.stop_tx	= sirfsoc_uart_stop_tx,
	.start_tx	= sirfsoc_uart_start_tx,
	.stop_rx	= sirfsoc_uart_stop_rx,
	.enable_ms	= sirfsoc_uart_enable_ms,
	.break_ctl	= sirfsoc_uart_break_ctl,
	.startup	= sirfsoc_uart_startup,
	.shutdown	= sirfsoc_uart_shutdown,
	.set_termios	= sirfsoc_uart_set_termios,
	.type		= sirfsoc_uart_type,
	.release_port	= sirfsoc_uart_release_port,
	.request_port	= sirfsoc_uart_request_port,
	.config_port	= sirfsoc_uart_config_port,
};

#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
1253 1254
static int __init
sirfsoc_uart_console_setup(struct console *co, char *options)
1255 1256 1257 1258 1259 1260
{
	unsigned int baud = 115200;
	unsigned int bits = 8;
	unsigned int parity = 'n';
	unsigned int flow = 'n';
	struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
1261 1262
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
1263 1264 1265 1266 1267 1268
	if (co->index < 0 || co->index >= SIRFSOC_UART_NR)
		return -EINVAL;

	if (!port->mapbase)
		return -ENODEV;

1269 1270 1271 1272
	/* enable usp in mode1 register */
	if (sirfport->uart_reg->uart_type == SIRF_USP_UART)
		wr_regl(port, ureg->sirfsoc_mode1, SIRFSOC_USP_EN |
				SIRFSOC_USP_ENDIAN_CTRL_LSBF);
1273 1274 1275
	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);
	port->cons = co;
1276

1277 1278 1279
	/* default console tx/rx transfer using io mode */
	sirfport->rx_dma_no = UNVALID_DMA_CHAN;
	sirfport->tx_dma_no = UNVALID_DMA_CHAN;
1280 1281 1282 1283 1284
	return uart_set_options(port, co, baud, parity, bits, flow);
}

static void sirfsoc_uart_console_putchar(struct uart_port *port, int ch)
{
1285 1286 1287
	struct sirfsoc_uart_port *sirfport = to_sirfport(port);
	struct sirfsoc_register *ureg = &sirfport->uart_reg->uart_reg;
	struct sirfsoc_fifo_status *ufifo_st = &sirfport->uart_reg->fifo_status;
1288
	while (rd_regl(port,
1289
		ureg->sirfsoc_tx_fifo_status) & ufifo_st->ff_full(port->line))
1290
		cpu_relax();
1291
	wr_regb(port, ureg->sirfsoc_tx_fifo_data, ch);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
}

static void sirfsoc_uart_console_write(struct console *co, const char *s,
							unsigned int count)
{
	struct uart_port *port = &sirfsoc_uart_ports[co->index].port;
	uart_console_write(port, s, count, sirfsoc_uart_console_putchar);
}

static struct console sirfsoc_uart_console = {
	.name		= SIRFSOC_UART_NAME,
	.device		= uart_console_device,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.write		= sirfsoc_uart_console_write,
	.setup		= sirfsoc_uart_console_setup,
	.data           = &sirfsoc_uart_drv,
};

static int __init sirfsoc_uart_console_init(void)
{
	register_console(&sirfsoc_uart_console);
	return 0;
}
console_initcall(sirfsoc_uart_console_init);
#endif

static struct uart_driver sirfsoc_uart_drv = {
	.owner		= THIS_MODULE,
	.driver_name	= SIRFUART_PORT_NAME,
	.nr		= SIRFSOC_UART_NR,
	.dev_name	= SIRFSOC_UART_NAME,
	.major		= SIRFSOC_UART_MAJOR,
	.minor		= SIRFSOC_UART_MINOR,
#ifdef CONFIG_SERIAL_SIRFSOC_CONSOLE
	.cons			= &sirfsoc_uart_console,
#else
	.cons			= NULL,
#endif
};

1333 1334 1335 1336 1337 1338 1339 1340
static struct of_device_id sirfsoc_uart_ids[] = {
	{ .compatible = "sirf,prima2-uart", .data = &sirfsoc_uart,},
	{ .compatible = "sirf,marco-uart", .data = &sirfsoc_uart},
	{ .compatible = "sirf,prima2-usp-uart", .data = &sirfsoc_usp},
	{}
};
MODULE_DEVICE_TABLE(of, sirfsoc_uart_ids);

1341
static int sirfsoc_uart_probe(struct platform_device *pdev)
1342 1343 1344 1345 1346
{
	struct sirfsoc_uart_port *sirfport;
	struct uart_port *port;
	struct resource *res;
	int ret;
1347
	const struct of_device_id *match;
1348

1349
	match = of_match_node(sirfsoc_uart_ids, pdev->dev.of_node);
1350 1351 1352 1353 1354 1355
	if (of_property_read_u32(pdev->dev.of_node, "cell-index", &pdev->id)) {
		dev_err(&pdev->dev,
			"Unable to find cell-index in uart node.\n");
		ret = -EFAULT;
		goto err;
	}
1356 1357 1358
	if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart"))
		pdev->id += ((struct sirfsoc_uart_register *)
				match->data)->uart_param.register_uart_nr;
1359 1360 1361 1362
	sirfport = &sirfsoc_uart_ports[pdev->id];
	port = &sirfport->port;
	port->dev = &pdev->dev;
	port->private_data = sirfport;
1363
	sirfport->uart_reg = (struct sirfsoc_uart_register *)match->data;
1364

1365 1366
	sirfport->hw_flow_ctrl = of_property_read_bool(pdev->dev.of_node,
		"sirf,uart-has-rtscts");
1367
	if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-uart")) {
1368
		sirfport->uart_reg->uart_type = SIRF_REAL_UART;
1369 1370 1371 1372 1373 1374 1375 1376 1377
		if (of_property_read_u32(pdev->dev.of_node,
				"sirf,uart-dma-rx-channel",
				&sirfport->rx_dma_no))
			sirfport->rx_dma_no = UNVALID_DMA_CHAN;
		if (of_property_read_u32(pdev->dev.of_node,
				"sirf,uart-dma-tx-channel",
				&sirfport->tx_dma_no))
			sirfport->tx_dma_no = UNVALID_DMA_CHAN;
	}
1378
	if (of_device_is_compatible(pdev->dev.of_node, "sirf,prima2-usp-uart")) {
1379
		sirfport->uart_reg->uart_type =	SIRF_USP_UART;
1380 1381 1382 1383 1384 1385 1386 1387
		if (of_property_read_u32(pdev->dev.of_node,
				"sirf,usp-dma-rx-channel",
				&sirfport->rx_dma_no))
			sirfport->rx_dma_no = UNVALID_DMA_CHAN;
		if (of_property_read_u32(pdev->dev.of_node,
				"sirf,usp-dma-tx-channel",
				&sirfport->tx_dma_no))
			sirfport->tx_dma_no = UNVALID_DMA_CHAN;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
		if (!sirfport->hw_flow_ctrl)
			goto usp_no_flow_control;
		if (of_find_property(pdev->dev.of_node, "cts-gpios", NULL))
			sirfport->cts_gpio = of_get_named_gpio(
					pdev->dev.of_node, "cts-gpios", 0);
		else
			sirfport->cts_gpio = -1;
		if (of_find_property(pdev->dev.of_node, "rts-gpios", NULL))
			sirfport->rts_gpio = of_get_named_gpio(
					pdev->dev.of_node, "rts-gpios", 0);
		else
			sirfport->rts_gpio = -1;

		if ((!gpio_is_valid(sirfport->cts_gpio) ||
			 !gpio_is_valid(sirfport->rts_gpio))) {
			ret = -EINVAL;
			dev_err(&pdev->dev,
1405
				"Usp flow control must have cts and rts gpio");
1406 1407 1408
			goto err;
		}
		ret = devm_gpio_request(&pdev->dev, sirfport->cts_gpio,
1409
				"usp-cts-gpio");
1410
		if (ret) {
1411
			dev_err(&pdev->dev, "Unable request cts gpio");
1412 1413 1414 1415
			goto err;
		}
		gpio_direction_input(sirfport->cts_gpio);
		ret = devm_gpio_request(&pdev->dev, sirfport->rts_gpio,
1416
				"usp-rts-gpio");
1417
		if (ret) {
1418
			dev_err(&pdev->dev, "Unable request rts gpio");
1419 1420 1421 1422 1423
			goto err;
		}
		gpio_direction_output(sirfport->rts_gpio, 1);
	}
usp_no_flow_control:
1424 1425 1426
	if (of_device_is_compatible(pdev->dev.of_node, "sirf,marco-uart"))
		sirfport->is_marco = true;

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
	if (of_property_read_u32(pdev->dev.of_node,
			"fifosize",
			&port->fifosize)) {
		dev_err(&pdev->dev,
			"Unable to find fifosize in uart node.\n");
		ret = -EFAULT;
		goto err;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		dev_err(&pdev->dev, "Insufficient resources.\n");
		ret = -EFAULT;
		goto err;
	}
1442 1443 1444 1445 1446 1447
	spin_lock_init(&sirfport->rx_lock);
	spin_lock_init(&sirfport->tx_lock);
	tasklet_init(&sirfport->rx_dma_complete_tasklet,
			sirfsoc_uart_rx_dma_complete_tl, (unsigned long)sirfport);
	tasklet_init(&sirfport->rx_tmo_process_tasklet,
			sirfsoc_rx_tmo_process_tl, (unsigned long)sirfport);
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	port->mapbase = res->start;
	port->membase = devm_ioremap(&pdev->dev, res->start, resource_size(res));
	if (!port->membase) {
		dev_err(&pdev->dev, "Cannot remap resource.\n");
		ret = -ENOMEM;
		goto err;
	}
	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (res == NULL) {
		dev_err(&pdev->dev, "Insufficient resources.\n");
		ret = -EFAULT;
1459
		goto err;
1460 1461 1462
	}
	port->irq = res->start;

1463 1464 1465
	sirfport->clk = clk_get(&pdev->dev, NULL);
	if (IS_ERR(sirfport->clk)) {
		ret = PTR_ERR(sirfport->clk);
1466
		goto err;
1467 1468 1469 1470
	}
	clk_prepare_enable(sirfport->clk);
	port->uartclk = clk_get_rate(sirfport->clk);

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	port->ops = &sirfsoc_uart_ops;
	spin_lock_init(&port->lock);

	platform_set_drvdata(pdev, sirfport);
	ret = uart_add_one_port(&sirfsoc_uart_drv, port);
	if (ret != 0) {
		dev_err(&pdev->dev, "Cannot add UART port(%d).\n", pdev->id);
		goto port_err;
	}

	return 0;

port_err:
1484 1485
	clk_disable_unprepare(sirfport->clk);
	clk_put(sirfport->clk);
1486 1487 1488 1489 1490 1491 1492 1493
err:
	return ret;
}

static int sirfsoc_uart_remove(struct platform_device *pdev)
{
	struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
	struct uart_port *port = &sirfport->port;
1494 1495
	clk_disable_unprepare(sirfport->clk);
	clk_put(sirfport->clk);
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	uart_remove_one_port(&sirfsoc_uart_drv, port);
	return 0;
}

static int
sirfsoc_uart_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
	struct uart_port *port = &sirfport->port;
	uart_suspend_port(&sirfsoc_uart_drv, port);
	return 0;
}

static int sirfsoc_uart_resume(struct platform_device *pdev)
{
	struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
	struct uart_port *port = &sirfport->port;
	uart_resume_port(&sirfsoc_uart_drv, port);
	return 0;
}

static struct platform_driver sirfsoc_uart_driver = {
	.probe		= sirfsoc_uart_probe,
1519
	.remove		= sirfsoc_uart_remove,
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	.suspend	= sirfsoc_uart_suspend,
	.resume		= sirfsoc_uart_resume,
	.driver		= {
		.name	= SIRFUART_PORT_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = sirfsoc_uart_ids,
	},
};

static int __init sirfsoc_uart_init(void)
{
	int ret = 0;

	ret = uart_register_driver(&sirfsoc_uart_drv);
	if (ret)
		goto out;

	ret = platform_driver_register(&sirfsoc_uart_driver);
	if (ret)
		uart_unregister_driver(&sirfsoc_uart_drv);
out:
	return ret;
}
module_init(sirfsoc_uart_init);

static void __exit sirfsoc_uart_exit(void)
{
	platform_driver_unregister(&sirfsoc_uart_driver);
	uart_unregister_driver(&sirfsoc_uart_drv);
}
module_exit(sirfsoc_uart_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Bin Shi <Bin.Shi@csr.com>, Rong Wang<Rong.Wang@csr.com>");
MODULE_DESCRIPTION("CSR SiRFprimaII Uart Driver");