mrst_max3110.c 20.2 KB
Newer Older
1
/*
2
 *  mrst_max3110.c - spi uart protocol driver for Maxim 3110
3
 *
4
 * Copyright (c) 2008-2010, Intel Corporation.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * Note:
 * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has
 *    1 word. If SPI master controller doesn't support sclk frequency change,
 *    then the char need be sent out one by one with some delay
 *
Lucas De Marchi's avatar
Lucas De Marchi committed
26
 * 2. Currently only RX available interrrupt is used, no need for waiting TXE
27 28 29 30 31
 *    interrupt for a low speed UART device
 */

#include <linux/module.h>
#include <linux/ioport.h>
32
#include <linux/irq.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46
#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_reg.h>

#include <linux/kthread.h>
#include <linux/spi/spi.h>

#include "mrst_max3110.h"

#define PR_FMT	"mrst_max3110: "

47 48 49 50
#define UART_TX_NEEDED 1
#define CON_TX_NEEDED  2
#define BIT_IRQ_PENDING    3

51 52 53
struct uart_max3110 {
	struct uart_port port;
	struct spi_device *spi;
54
	char name[SPI_NAME_SIZE];
55 56 57 58

	wait_queue_head_t wq;
	struct task_struct *main_thread;
	struct task_struct *read_thread;
59
	struct mutex thread_mutex;
60 61 62 63 64

	u32 baud;
	u16 cur_conf;
	u8 clock;
	u8 parity, word_7bits;
65
	u16 irq;
66

67
	unsigned long uart_flags;
68 69 70 71 72 73

	/* console related */
	struct circ_buf con_xmit;
};

/* global data structure, may need be removed */
74 75
static struct uart_max3110 *pmax;

76 77
static void receive_chars(struct uart_max3110 *max,
				unsigned char *str, int len);
78 79
static int max3110_read_multi(struct uart_max3110 *max, u8 *buf);
static void max3110_con_receive(struct uart_max3110 *max);
80

81 82
static int max3110_write_then_read(struct uart_max3110 *max,
		const void *txbuf, void *rxbuf, unsigned len, int always_fast)
83 84 85 86 87 88 89 90 91 92 93 94 95 96
{
	struct spi_device *spi = max->spi;
	struct spi_message	message;
	struct spi_transfer	x;
	int ret;

	spi_message_init(&message);
	memset(&x, 0, sizeof x);
	x.len = len;
	x.tx_buf = txbuf;
	x.rx_buf = rxbuf;
	spi_message_add_tail(&x, &message);

	if (always_fast)
97
		x.speed_hz = spi->max_speed_hz;
98 99 100 101 102 103 104 105
	else if (max->baud)
		x.speed_hz = max->baud;

	/* Do the i/o */
	ret = spi_sync(spi, &message);
	return ret;
}

106 107
/* Write a 16b word to the device */
static int max3110_out(struct uart_max3110 *max, const u16 out)
108
{
109 110 111
	void *buf;
	u16 *obuf, *ibuf;
	u8  ch;
112 113
	int ret;

114 115 116 117 118 119 120 121 122 123 124 125 126
	buf = kzalloc(8, GFP_KERNEL | GFP_DMA);
	if (!buf)
		return -ENOMEM;

	obuf = buf;
	ibuf = buf + 4;
	*obuf = out;
	ret = max3110_write_then_read(max, obuf, ibuf, 2, 1);
	if (ret) {
		pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n",
				__func__, ret, out);
		goto exit;
	}
127 128

	/* If some valid data is read back */
129 130 131 132
	if (*ibuf & MAX3110_READ_DATA_AVAILABLE) {
		ch = *ibuf & 0xff;
		receive_chars(max, &ch, 1);
	}
133

134 135
exit:
	kfree(buf);
136 137 138 139 140
	return ret;
}

/*
 * This is usually used to read data from SPIC RX FIFO, which doesn't
141 142 143
 * need any delay like flushing character out.
 *
 * Return how many valide bytes are read back
144
 */
145
static int max3110_read_multi(struct uart_max3110 *max, u8 *rxbuf)
146
{
147 148 149 150
	void *buf;
	u16 *obuf, *ibuf;
	u8 *pbuf, valid_str[M3110_RX_FIFO_DEPTH];
	int i, j, blen;
151

152 153 154 155
	blen = M3110_RX_FIFO_DEPTH * sizeof(u16);
	buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA);
	if (!buf) {
		pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__);
156 157 158
		return 0;
	}

159 160 161
	/* tx/rx always have the same length */
	obuf = buf;
	ibuf = buf + blen;
162

163 164
	if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
		kfree(buf);
165
		return 0;
166
	}
167

168 169
	/* If caller doesn't provide a buffer, then handle received char */
	pbuf = rxbuf ? rxbuf : valid_str;
170

171 172 173
	for (i = 0, j = 0; i < M3110_RX_FIFO_DEPTH; i++) {
		if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
			pbuf[j++] = ibuf[i] & 0xff;
174 175 176 177 178
	}

	if (j && (pbuf == valid_str))
		receive_chars(max, valid_str, j);

179
	kfree(buf);
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
	return j;
}

static void serial_m3110_con_putchar(struct uart_port *port, int ch)
{
	struct uart_max3110 *max =
		container_of(port, struct uart_max3110, port);
	struct circ_buf *xmit = &max->con_xmit;

	if (uart_circ_chars_free(xmit)) {
		xmit->buf[xmit->head] = (char)ch;
		xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
	}
}

/*
 * Print a string to the serial port trying not to disturb
 * any possible real use of the port...
 *
 *	The console_lock must be held when we get here.
 */
static void serial_m3110_con_write(struct console *co,
				const char *s, unsigned int count)
{
	if (!pmax)
		return;

	uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
208 209 210

	if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
		wake_up_process(pmax->main_thread);
211 212 213 214 215 216 217 218 219 220 221 222 223
}

static int __init
serial_m3110_con_setup(struct console *co, char *options)
{
	struct uart_max3110 *max = pmax;
	int baud = 115200;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';

	pr_info(PR_FMT "setting up console\n");

224 225 226
	if (co->index == -1)
		co->index = 0;

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
	if (!max) {
		pr_err(PR_FMT "pmax is NULL, return");
		return -ENODEV;
	}

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

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

static struct tty_driver *serial_m3110_con_device(struct console *co,
							int *index)
{
	struct uart_driver *p = co->data;
	*index = co->index;
	return p->tty_driver;
}

static struct uart_driver serial_m3110_reg;
static struct console serial_m3110_console = {
	.name		= "ttyS",
	.write		= serial_m3110_con_write,
	.device		= serial_m3110_con_device,
	.setup		= serial_m3110_con_setup,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.data		= &serial_m3110_reg,
};

static unsigned int serial_m3110_tx_empty(struct uart_port *port)
{
	return 1;
}

static void serial_m3110_stop_tx(struct uart_port *port)
{
	return;
}

/* stop_rx will be called in spin_lock env */
static void serial_m3110_stop_rx(struct uart_port *port)
{
	return;
}

#define WORDS_PER_XFER	128
274
static void send_circ_buf(struct uart_max3110 *max,
275 276
				struct circ_buf *xmit)
{
277 278
	void *buf;
	u16 *obuf, *ibuf;
279
	u8 valid_str[WORDS_PER_XFER];
280 281 282 283 284 285 286 287 288
	int i, j, len, blen, dma_size, left, ret = 0;


	dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
	buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
	if (!buf)
		return;
	obuf = buf;
	ibuf = buf + dma_size/2;
289 290 291 292

	while (!uart_circ_empty(xmit)) {
		left = uart_circ_chars_pending(xmit);
		while (left) {
293 294 295
			len = min(left, WORDS_PER_XFER);
			blen = len * sizeof(u16);
			memset(ibuf, 0, blen);
296 297 298 299 300 301

			for (i = 0; i < len; i++) {
				obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
				xmit->tail = (xmit->tail + 1) &
						(UART_XMIT_SIZE - 1);
			}
302 303 304 305 306 307

			/* Fail to send msg to console is not very critical */
			ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
			if (ret)
				pr_warning(PR_FMT "%s(): get err msg %d\n",
						__func__, ret);
308 309 310

			for (i = 0, j = 0; i < len; i++) {
				if (ibuf[i] & MAX3110_READ_DATA_AVAILABLE)
311
					valid_str[j++] = ibuf[i] & 0xff;
312 313 314 315 316 317 318 319 320
			}

			if (j)
				receive_chars(max, valid_str, j);

			max->port.icount.tx += len;
			left -= len;
		}
	}
321 322

	kfree(buf);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

static void transmit_char(struct uart_max3110 *max)
{
	struct uart_port *port = &max->port;
	struct circ_buf *xmit = &port->state->xmit;

	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
		return;

	send_circ_buf(max, xmit);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);

	if (uart_circ_empty(xmit))
		serial_m3110_stop_tx(port);
}

342 343 344 345
/*
 * This will be called by uart_write() and tty_write, can't
 * go to sleep
 */
346 347 348 349 350
static void serial_m3110_start_tx(struct uart_port *port)
{
	struct uart_max3110 *max =
		container_of(port, struct uart_max3110, port);

351
	if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		wake_up_process(max->main_thread);
}

static void receive_chars(struct uart_max3110 *max, unsigned char *str, int len)
{
	struct uart_port *port = &max->port;
	struct tty_struct *tty;
	int usable;

	/* If uart is not opened, just return */
	if (!port->state)
		return;

	tty = port->state->port.tty;
	if (!tty)
367
		return;
368 369 370 371 372 373 374 375 376 377

	while (len) {
		usable = tty_buffer_request_room(tty, len);
		if (usable) {
			tty_insert_flip_string(tty, str, usable);
			str += usable;
			port->icount.rx += usable;
		}
		len -= usable;
	}
378
	tty_flip_buffer_push(tty);
379 380
}

381 382 383 384 385 386 387 388 389 390 391
/*
 * This routine will be used in read_thread or RX IRQ handling,
 * it will first do one round buffer read(8 words), if there is some
 * valid RX data, will try to read 5 more rounds till all data
 * is read out.
 *
 * Use stack space as data buffer to save some system load, and chose
 * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
 * receiving bulk data, a much bigger buffer may cause stack overflow
 */
static void max3110_con_receive(struct uart_max3110 *max)
392 393 394 395 396 397
{
	int loop = 1, num, total = 0;
	u8 recv_buf[512], *pbuf;

	pbuf = recv_buf;
	do {
398
		num = max3110_read_multi(max, pbuf);
399 400

		if (num) {
401
			loop = 5;
402 403 404
			pbuf += num;
			total += num;

405
			if (total >= 504) {
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
				receive_chars(max, recv_buf, total);
				pbuf = recv_buf;
				total = 0;
			}
		}
	} while (--loop);

	if (total)
		receive_chars(max, recv_buf, total);
}

static int max3110_main_thread(void *_max)
{
	struct uart_max3110 *max = _max;
	wait_queue_head_t *wq = &max->wq;
	int ret = 0;
	struct circ_buf *xmit = &max->con_xmit;

	init_waitqueue_head(wq);
	pr_info(PR_FMT "start main thread\n");

	do {
428
		wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop());
429 430

		mutex_lock(&max->thread_mutex);
431

432
		if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
433
			max3110_con_receive(max);
434 435

		/* first handle console output */
436
		if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
437 438 439
			send_circ_buf(max, xmit);

		/* handle uart output */
440
		if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
441
			transmit_char(max);
442

443
		mutex_unlock(&max->thread_mutex);
444

445 446 447 448 449 450 451 452 453 454 455
	} while (!kthread_should_stop());

	return ret;
}

static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
{
	struct uart_max3110 *max = dev_id;

	/* max3110's irq is a falling edge, not level triggered,
	 * so no need to disable the irq */
456
	if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
457
		wake_up_process(max->main_thread);
458

459 460
	return IRQ_HANDLED;
}
461

462 463 464 465 466 467 468
/* if don't use RX IRQ, then need a thread to polling read */
static int max3110_read_thread(void *_max)
{
	struct uart_max3110 *max = _max;

	pr_info(PR_FMT "start read thread\n");
	do {
469 470 471 472 473 474 475 476
		/*
		 * If can't acquire the mutex, it means the main thread
		 * is running which will also perform the rx job
		 */
		if (mutex_trylock(&max->thread_mutex)) {
			max3110_con_receive(max);
			mutex_unlock(&max->thread_mutex);
		}
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(HZ / 20);
	} while (!kthread_should_stop());

	return 0;
}

static int serial_m3110_startup(struct uart_port *port)
{
	struct uart_max3110 *max =
		container_of(port, struct uart_max3110, port);
	u16 config = 0;
	int ret = 0;

492
	if (port->line != 0) {
493
		pr_err(PR_FMT "uart port startup failed\n");
494 495
		return -1;
	}
496

497
	/* Disable all IRQ and config it to 115200, 8n1 */
498 499 500 501 502 503 504 505
	config = WC_TAG | WC_FIFO_ENABLE
			| WC_1_STOPBITS
			| WC_8BIT_WORD
			| WC_BAUD_DR2;

	/* as we use thread to handle tx/rx, need set low latency */
	port->state->port.tty->low_latency = 1;

506 507 508
	if (max->irq) {
		max->read_thread = NULL;
		ret = request_irq(max->irq, serial_m3110_irq,
509
				IRQ_TYPE_EDGE_FALLING, "max3110", max);
510 511 512 513 514 515 516 517
		if (ret) {
			max->irq = 0;
			pr_err(PR_FMT "unable to allocate IRQ, polling\n");
		}  else {
			/* Enable RX IRQ only */
			config |= WC_RXA_IRQ_ENABLE;
		}
	}
518

519 520 521 522 523 524 525 526 527 528
	if (max->irq == 0) {
		/* If IRQ is disabled, start a read thread for input data */
		max->read_thread =
			kthread_run(max3110_read_thread, max, "max3110_read");
		if (IS_ERR(max->read_thread)) {
			ret = PTR_ERR(max->read_thread);
			max->read_thread = NULL;
			pr_err(PR_FMT "Can't create read thread!\n");
			return ret;
		}
529 530 531 532
	}

	ret = max3110_out(max, config);
	if (ret) {
533 534 535 536
		if (max->irq)
			free_irq(max->irq, max);
		if (max->read_thread)
			kthread_stop(max->read_thread);
537 538 539
		max->read_thread = NULL;
		return ret;
	}
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	max->cur_conf = config;
	return 0;
}

static void serial_m3110_shutdown(struct uart_port *port)
{
	struct uart_max3110 *max =
		container_of(port, struct uart_max3110, port);
	u16 config;

	if (max->read_thread) {
		kthread_stop(max->read_thread);
		max->read_thread = NULL;
	}

556 557
	if (max->irq)
		free_irq(max->irq, max);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

	/* Disable interrupts from this port */
	config = WC_TAG | WC_SW_SHDI;
	max3110_out(max, config);
}

static void serial_m3110_release_port(struct uart_port *port)
{
}

static int serial_m3110_request_port(struct uart_port *port)
{
	return 0;
}

static void serial_m3110_config_port(struct uart_port *port, int flags)
{
575
	port->type = PORT_MAX3100;
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
}

static int
serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	/* we don't want the core code to modify any port params */
	return -EINVAL;
}


static const char *serial_m3110_type(struct uart_port *port)
{
	struct uart_max3110 *max =
		container_of(port, struct uart_max3110, port);
	return max->name;
}

static void
serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
		       struct ktermios *old)
{
	struct uart_max3110 *max =
		container_of(port, struct uart_max3110, port);
	unsigned char cval;
	unsigned int baud, parity = 0;
	int clk_div = -1;
	u16 new_conf = max->cur_conf;

	switch (termios->c_cflag & CSIZE) {
	case CS7:
		cval = UART_LCR_WLEN7;
		new_conf |= WC_7BIT_WORD;
		break;
	default:
610 611 612
		/* We only support CS7 & CS8 */
		termios->c_cflag &= ~CSIZE;
		termios->c_cflag |= CS8;
613 614 615 616 617 618 619 620
	case CS8:
		cval = UART_LCR_WLEN8;
		new_conf |= WC_8BIT_WORD;
		break;
	}

	baud = uart_get_baud_rate(port, termios, old, 0, 230400);

621
	/* First calc the div for 1.8MHZ clock case */
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	switch (baud) {
	case 300:
		clk_div = WC_BAUD_DR384;
		break;
	case 600:
		clk_div = WC_BAUD_DR192;
		break;
	case 1200:
		clk_div = WC_BAUD_DR96;
		break;
	case 2400:
		clk_div = WC_BAUD_DR48;
		break;
	case 4800:
		clk_div = WC_BAUD_DR24;
		break;
	case 9600:
		clk_div = WC_BAUD_DR12;
		break;
	case 19200:
		clk_div = WC_BAUD_DR6;
		break;
	case 38400:
		clk_div = WC_BAUD_DR3;
		break;
	case 57600:
		clk_div = WC_BAUD_DR2;
		break;
	case 115200:
		clk_div = WC_BAUD_DR1;
		break;
	case 230400:
		if (max->clock & MAX3110_HIGH_CLK)
			break;
	default:
657
		/* Pick the previous baud rate */
658 659 660 661 662 663 664
		baud = max->baud;
		clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
		tty_termios_encode_baud_rate(termios, baud, baud);
	}

	if (max->clock & MAX3110_HIGH_CLK) {
		clk_div += 1;
665 666
		/* High clk version max3110 doesn't support B300 */
		if (baud == 300) {
667
			baud = 600;
668 669
			clk_div = WC_BAUD_DR384;
		}
670 671 672 673 674 675
		if (baud == 230400)
			clk_div = WC_BAUD_DR1;
		tty_termios_encode_baud_rate(termios, baud, baud);
	}

	new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
676 677 678 679

	if (unlikely(termios->c_cflag & CMSPAR))
		termios->c_cflag &= ~CMSPAR;

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	if (termios->c_cflag & CSTOPB)
		new_conf |= WC_2_STOPBITS;
	else
		new_conf &= ~WC_2_STOPBITS;

	if (termios->c_cflag & PARENB) {
		new_conf |= WC_PARITY_ENABLE;
		parity |= UART_LCR_PARITY;
	} else
		new_conf &= ~WC_PARITY_ENABLE;

	if (!(termios->c_cflag & PARODD))
		parity |= UART_LCR_EPAR;
	max->parity = parity;

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

	new_conf |= WC_TAG;
	if (new_conf != max->cur_conf) {
699 700 701 702
		if (!max3110_out(max, new_conf)) {
			max->cur_conf = new_conf;
			max->baud = baud;
		}
703 704 705
	}
}

706
/* Don't handle hw handshaking */
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
{
	return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
}

static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
}

static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
{
}

static void serial_m3110_pm(struct uart_port *port, unsigned int state,
			unsigned int oldstate)
{
}

static void serial_m3110_enable_ms(struct uart_port *port)
{
}

struct uart_ops serial_m3110_ops = {
	.tx_empty	= serial_m3110_tx_empty,
	.set_mctrl	= serial_m3110_set_mctrl,
	.get_mctrl	= serial_m3110_get_mctrl,
	.stop_tx	= serial_m3110_stop_tx,
	.start_tx	= serial_m3110_start_tx,
	.stop_rx	= serial_m3110_stop_rx,
	.enable_ms	= serial_m3110_enable_ms,
	.break_ctl	= serial_m3110_break_ctl,
	.startup	= serial_m3110_startup,
	.shutdown	= serial_m3110_shutdown,
740
	.set_termios	= serial_m3110_set_termios,
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
	.pm		= serial_m3110_pm,
	.type		= serial_m3110_type,
	.release_port	= serial_m3110_release_port,
	.request_port	= serial_m3110_request_port,
	.config_port	= serial_m3110_config_port,
	.verify_port	= serial_m3110_verify_port,
};

static struct uart_driver serial_m3110_reg = {
	.owner		= THIS_MODULE,
	.driver_name	= "MRST serial",
	.dev_name	= "ttyS",
	.major		= TTY_MAJOR,
	.minor		= 64,
	.nr		= 1,
756
	.cons		= &serial_m3110_console,
757 758
};

759
#ifdef CONFIG_PM
760 761
static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
{
762 763 764 765 766
	struct uart_max3110 *max = spi_get_drvdata(spi);

	disable_irq(max->irq);
	uart_suspend_port(&serial_m3110_reg, &max->port);
	max3110_out(max, max->cur_conf | WC_SW_SHDI);
767 768 769 770 771
	return 0;
}

static int serial_m3110_resume(struct spi_device *spi)
{
772 773 774 775 776
	struct uart_max3110 *max = spi_get_drvdata(spi);

	max3110_out(max, max->cur_conf);
	uart_resume_port(&serial_m3110_reg, &max->port);
	enable_irq(max->irq);
777 778
	return 0;
}
779 780 781 782
#else
#define serial_m3110_suspend	NULL
#define serial_m3110_resume	NULL
#endif
783

784
static int __devinit serial_m3110_probe(struct spi_device *spi)
785 786
{
	struct uart_max3110 *max;
787
	void *buffer;
788
	u16 res;
789 790
	int ret = 0;

791 792 793 794
	max = kzalloc(sizeof(*max), GFP_KERNEL);
	if (!max)
		return -ENOMEM;

795
	/* Set spi info */
796 797 798 799 800
	spi->bits_per_word = 16;
	max->clock = MAX3110_HIGH_CLK;

	spi_setup(spi);

801 802
	max->port.type = PORT_MAX3100;
	max->port.fifosize = 2;		/* Only have 16b buffer */
803 804 805 806 807 808
	max->port.ops = &serial_m3110_ops;
	max->port.line = 0;
	max->port.dev = &spi->dev;
	max->port.uartclk = 115200;

	max->spi = spi;
809
	strcpy(max->name, spi->modalias);
810 811
	max->irq = (u16)spi->irq;

812
	mutex_init(&max->thread_mutex);
813 814 815 816 817 818

	max->word_7bits = 0;
	max->parity = 0;
	max->baud = 0;

	max->cur_conf = 0;
819 820
	max->uart_flags = 0;

821 822 823 824 825
	/* Check if reading configuration register returns something sane */

	res = RC_TAG;
	ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
	if (ret < 0 || res == 0 || res == 0xffff) {
826
		dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
827 828 829 830
									res);
		ret = -ENODEV;
		goto err_get_page;
	}
831 832

	buffer = (void *)__get_free_page(GFP_KERNEL);
833 834 835 836
	if (!buffer) {
		ret = -ENOMEM;
		goto err_get_page;
	}
837 838 839
	max->con_xmit.buf = buffer;
	max->con_xmit.head = 0;
	max->con_xmit.tail = 0;
840 841 842 843 844 845 846 847

	max->main_thread = kthread_run(max3110_main_thread,
					max, "max3110_main");
	if (IS_ERR(max->main_thread)) {
		ret = PTR_ERR(max->main_thread);
		goto err_kthread;
	}

848
	spi_set_drvdata(spi, max);
849
	pmax = max;
850 851

	/* Give membase a psudo value to pass serial_core's check */
852 853 854 855 856 857 858 859 860 861 862 863
	max->port.membase = (void *)0xff110000;
	uart_add_one_port(&serial_m3110_reg, &max->port);

	return 0;

err_kthread:
	free_page((unsigned long)buffer);
err_get_page:
	kfree(max);
	return ret;
}

864
static int __devexit serial_m3110_remove(struct spi_device *dev)
865
{
866
	struct uart_max3110 *max = spi_get_drvdata(dev);
867

868
	if (!max)
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
		return 0;

	uart_remove_one_port(&serial_m3110_reg, &max->port);

	free_page((unsigned long)max->con_xmit.buf);

	if (max->main_thread)
		kthread_stop(max->main_thread);

	kfree(max);
	return 0;
}

static struct spi_driver uart_max3110_driver = {
	.driver = {
			.name	= "spi_max3111",
			.bus	= &spi_bus_type,
			.owner	= THIS_MODULE,
	},
	.probe		= serial_m3110_probe,
889
	.remove		= __devexit_p(serial_m3110_remove),
890 891 892 893
	.suspend	= serial_m3110_suspend,
	.resume		= serial_m3110_resume,
};

894
static int __init serial_m3110_init(void)
895 896 897 898 899 900 901 902 903 904 905 906 907 908
{
	int ret = 0;

	ret = uart_register_driver(&serial_m3110_reg);
	if (ret)
		return ret;

	ret = spi_register_driver(&uart_max3110_driver);
	if (ret)
		uart_unregister_driver(&serial_m3110_reg);

	return ret;
}

909
static void __exit serial_m3110_exit(void)
910 911 912 913 914 915 916 917
{
	spi_unregister_driver(&uart_max3110_driver);
	uart_unregister_driver(&serial_m3110_reg);
}

module_init(serial_m3110_init);
module_exit(serial_m3110_exit);

918
MODULE_LICENSE("GPL v2");
919
MODULE_ALIAS("max3110-uart");