sunzilog.c 39.4 KB
Newer Older
1
/* sunzilog.c: Zilog serial driver for Sparc systems.
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11
 *
 * Driver for Zilog serial chips found on Sun workstations and
 * servers.  This driver could actually be made more generic.
 *
 * This is based on the old drivers/sbus/char/zs.c code.  A lot
 * of code has been simply moved over directly from there but
 * much has been rewritten.  Credits therefore go out to Eddie
 * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
 * work there.
 *
12
 * Copyright (C) 2002, 2006, 2007 David S. Miller (davem@davemloft.net)
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/circ_buf.h>
#include <linux/serial.h>
#include <linux/sysrq.h>
#include <linux/console.h>
#include <linux/spinlock.h>
#ifdef CONFIG_SERIO
#include <linux/serio.h>
#endif
#include <linux/init.h>
35
#include <linux/of_device.h>
L
Linus Torvalds 已提交
36 37 38

#include <asm/io.h>
#include <asm/irq.h>
39
#include <asm/prom.h>
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

#include <linux/serial_core.h>

#include "suncore.h"
#include "sunzilog.h"

/* On 32-bit sparcs we need to delay after register accesses
 * to accommodate sun4 systems, but we do not need to flush writes.
 * On 64-bit sparc we only need to flush single writes to ensure
 * completion.
 */
#ifndef CONFIG_SPARC64
#define ZSDELAY()		udelay(5)
#define ZSDELAY_LONG()		udelay(20)
#define ZS_WSYNC(channel)	do { } while (0)
#else
#define ZSDELAY()
#define ZSDELAY_LONG()
#define ZS_WSYNC(__channel) \
63
	readb(&((__channel)->control))
L
Linus Torvalds 已提交
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
#endif

#define ZS_CLOCK		4915200 /* Zilog input clock rate. */
#define ZS_CLOCK_DIVISOR	16      /* Divisor this driver uses. */

/*
 * We wrap our port structure around the generic uart_port.
 */
struct uart_sunzilog_port {
	struct uart_port		port;

	/* IRQ servicing chain.  */
	struct uart_sunzilog_port	*next;

	/* Current values of Zilog write registers.  */
	unsigned char			curregs[NUM_ZSREGS];

	unsigned int			flags;
#define SUNZILOG_FLAG_CONS_KEYB		0x00000001
#define SUNZILOG_FLAG_CONS_MOUSE	0x00000002
#define SUNZILOG_FLAG_IS_CONS		0x00000004
#define SUNZILOG_FLAG_IS_KGDB		0x00000008
#define SUNZILOG_FLAG_MODEM_STATUS	0x00000010
#define SUNZILOG_FLAG_IS_CHANNEL_A	0x00000020
#define SUNZILOG_FLAG_REGS_HELD		0x00000040
#define SUNZILOG_FLAG_TX_STOPPED	0x00000080
#define SUNZILOG_FLAG_TX_ACTIVE		0x00000100
91 92
#define SUNZILOG_FLAG_ESCC		0x00000200
#define SUNZILOG_FLAG_ISR_HANDLER	0x00000400
L
Linus Torvalds 已提交
93 94 95 96 97 98 99

	unsigned int cflag;

	unsigned char			parity_mask;
	unsigned char			prev_status;

#ifdef CONFIG_SERIO
100
	struct serio			serio;
L
Linus Torvalds 已提交
101 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
	int				serio_open;
#endif
};

#define ZILOG_CHANNEL_FROM_PORT(PORT)	((struct zilog_channel __iomem *)((PORT)->membase))
#define UART_ZILOG(PORT)		((struct uart_sunzilog_port *)(PORT))

#define ZS_IS_KEYB(UP)	((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
#define ZS_IS_MOUSE(UP)	((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
#define ZS_IS_CONS(UP)	((UP)->flags & SUNZILOG_FLAG_IS_CONS)
#define ZS_IS_KGDB(UP)	((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
#define ZS_WANTS_MODEM_STATUS(UP)	((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
#define ZS_IS_CHANNEL_A(UP)	((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
#define ZS_REGS_HELD(UP)	((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
#define ZS_TX_STOPPED(UP)	((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
#define ZS_TX_ACTIVE(UP)	((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)

/* Reading and writing Zilog8530 registers.  The delays are to make this
 * driver work on the Sun4 which needs a settling delay after each chip
 * register access, other machines handle this in hardware via auxiliary
 * flip-flops which implement the settle time we do in software.
 *
 * The port lock must be held and local IRQs must be disabled
 * when {read,write}_zsreg is invoked.
 */
static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
				unsigned char reg)
{
	unsigned char retval;

131
	writeb(reg, &channel->control);
L
Linus Torvalds 已提交
132
	ZSDELAY();
133
	retval = readb(&channel->control);
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141
	ZSDELAY();

	return retval;
}

static void write_zsreg(struct zilog_channel __iomem *channel,
			unsigned char reg, unsigned char value)
{
142
	writeb(reg, &channel->control);
L
Linus Torvalds 已提交
143
	ZSDELAY();
144
	writeb(value, &channel->control);
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154
	ZSDELAY();
}

static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
{
	int i;

	for (i = 0; i < 32; i++) {
		unsigned char regval;

155
		regval = readb(&channel->control);
L
Linus Torvalds 已提交
156 157 158 159 160
		ZSDELAY();
		if (regval & Rx_CH_AV)
			break;

		regval = read_zsreg(channel, R1);
161
		readb(&channel->data);
L
Linus Torvalds 已提交
162 163 164
		ZSDELAY();

		if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
165
			writeb(ERR_RES, &channel->control);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174
			ZSDELAY();
			ZS_WSYNC(channel);
		}
	}
}

/* This function must only be called when the TX is not busy.  The UART
 * port lock must be held and local interrupts disabled.
 */
175
static int __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
L
Linus Torvalds 已提交
176 177
{
	int i;
178 179
	int escc;
	unsigned char r15;
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188

	/* Let pending transmits finish.  */
	for (i = 0; i < 1000; i++) {
		unsigned char stat = read_zsreg(channel, R1);
		if (stat & ALL_SNT)
			break;
		udelay(100);
	}

189
	writeb(ERR_RES, &channel->control);
L
Linus Torvalds 已提交
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	ZSDELAY();
	ZS_WSYNC(channel);

	sunzilog_clear_fifo(channel);

	/* Disable all interrupts.  */
	write_zsreg(channel, R1,
		    regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));

	/* Set parity, sync config, stop bits, and clock divisor.  */
	write_zsreg(channel, R4, regs[R4]);

	/* Set misc. TX/RX control bits.  */
	write_zsreg(channel, R10, regs[R10]);

	/* Set TX/RX controls sans the enable bits.  */
	write_zsreg(channel, R3, regs[R3] & ~RxENAB);
	write_zsreg(channel, R5, regs[R5] & ~TxENAB);

	/* Synchronous mode config.  */
	write_zsreg(channel, R6, regs[R6]);
	write_zsreg(channel, R7, regs[R7]);

	/* Don't mess with the interrupt vector (R2, unused by us) and
	 * master interrupt control (R9).  We make sure this is setup
	 * properly at probe time then never touch it again.
	 */

	/* Disable baud generator.  */
	write_zsreg(channel, R14, regs[R14] & ~BRENAB);

	/* Clock mode control.  */
	write_zsreg(channel, R11, regs[R11]);

	/* Lower and upper byte of baud rate generator divisor.  */
	write_zsreg(channel, R12, regs[R12]);
	write_zsreg(channel, R13, regs[R13]);
	
	/* Now rewrite R14, with BRENAB (if set).  */
	write_zsreg(channel, R14, regs[R14]);

	/* External status interrupt control.  */
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	write_zsreg(channel, R15, (regs[R15] | WR7pEN) & ~FIFOEN);

	/* ESCC Extension Register */
	r15 = read_zsreg(channel, R15);
	if (r15 & 0x01)	{
		write_zsreg(channel, R7,  regs[R7p]);

		/* External status interrupt and FIFO control.  */
		write_zsreg(channel, R15, regs[R15] & ~WR7pEN);
		escc = 1;
	} else {
		 /* Clear FIFO bit case it is an issue */
		regs[R15] &= ~FIFOEN;
		escc = 0;
	}
L
Linus Torvalds 已提交
247 248

	/* Reset external status interrupts.  */
249 250
	write_zsreg(channel, R0, RES_EXT_INT); /* First Latch  */
	write_zsreg(channel, R0, RES_EXT_INT); /* Second Latch */
L
Linus Torvalds 已提交
251 252 253 254 255 256 257

	/* Rewrite R3/R5, this time without enables masked.  */
	write_zsreg(channel, R3, regs[R3]);
	write_zsreg(channel, R5, regs[R5]);

	/* Rewrite R1, this time without IRQ enabled masked.  */
	write_zsreg(channel, R1, regs[R1]);
258 259

	return escc;
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
}

/* Reprogram the Zilog channel HW registers with the copies found in the
 * software state struct.  If the transmitter is busy, we defer this update
 * until the next TX complete interrupt.  Else, we do it right now.
 *
 * The UART port lock must be held and local interrupts disabled.
 */
static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
				       struct zilog_channel __iomem *channel)
{
	if (!ZS_REGS_HELD(up)) {
		if (ZS_TX_ACTIVE(up)) {
			up->flags |= SUNZILOG_FLAG_REGS_HELD;
		} else {
			__load_zsregs(channel, up->curregs);
		}
	}
}

static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
{
	unsigned int cur_cflag = up->cflag;
	int brg, new_baud;

	up->cflag &= ~CBAUD;
	up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);

	brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
	up->curregs[R12] = (brg & 0xff);
	up->curregs[R13] = (brg >> 8) & 0xff;
	sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
}

static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
295
					 unsigned char ch, int is_break)
L
Linus Torvalds 已提交
296 297 298 299 300
{
	if (ZS_IS_KEYB(up)) {
		/* Stop-A is handled by drivers/char/keyboard.c now. */
#ifdef CONFIG_SERIO
		if (up->serio_open)
301
			serio_interrupt(&up->serio, ch, 0);
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315
#endif
	} else if (ZS_IS_MOUSE(up)) {
		int ret = suncore_mouse_baud_detection(ch, is_break);

		switch (ret) {
		case 2:
			sunzilog_change_mouse_baud(up);
			/* fallthru */
		case 1:
			break;

		case 0:
#ifdef CONFIG_SERIO
			if (up->serio_open)
316
				serio_interrupt(&up->serio, ch, 0);
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324
#endif
			break;
		};
	}
}

static struct tty_struct *
sunzilog_receive_chars(struct uart_sunzilog_port *up,
325
		       struct zilog_channel __iomem *channel)
L
Linus Torvalds 已提交
326 327
{
	struct tty_struct *tty;
A
Alan Cox 已提交
328
	unsigned char ch, r1, flag;
L
Linus Torvalds 已提交
329 330 331

	tty = NULL;
	if (up->port.info != NULL &&		/* Unopened serial console */
332 333
	    up->port.info->port.tty != NULL)	/* Keyboard || mouse */
		tty = up->port.info->port.tty;
L
Linus Torvalds 已提交
334 335 336 337 338

	for (;;) {

		r1 = read_zsreg(channel, R1);
		if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
339
			writeb(ERR_RES, &channel->control);
L
Linus Torvalds 已提交
340 341 342 343
			ZSDELAY();
			ZS_WSYNC(channel);
		}

344
		ch = readb(&channel->control);
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355
		ZSDELAY();

		/* This funny hack depends upon BRK_ABRT not interfering
		 * with the other bits we care about in R1.
		 */
		if (ch & BRK_ABRT)
			r1 |= BRK_ABRT;

		if (!(ch & Rx_CH_AV))
			break;

356
		ch = readb(&channel->data);
L
Linus Torvalds 已提交
357 358 359 360 361
		ZSDELAY();

		ch &= up->parity_mask;

		if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
362
			sunzilog_kbdms_receive_chars(up, ch, 0);
L
Linus Torvalds 已提交
363 364 365 366
			continue;
		}

		if (tty == NULL) {
367
			uart_handle_sysrq_char(&up->port, ch);
L
Linus Torvalds 已提交
368 369 370 371
			continue;
		}

		/* A real serial line, record the character and status.  */
A
Alan Cox 已提交
372
		flag = TTY_NORMAL;
L
Linus Torvalds 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
		up->port.icount.rx++;
		if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
			if (r1 & BRK_ABRT) {
				r1 &= ~(PAR_ERR | CRC_ERR);
				up->port.icount.brk++;
				if (uart_handle_break(&up->port))
					continue;
			}
			else if (r1 & PAR_ERR)
				up->port.icount.parity++;
			else if (r1 & CRC_ERR)
				up->port.icount.frame++;
			if (r1 & Rx_OVR)
				up->port.icount.overrun++;
			r1 &= up->port.read_status_mask;
			if (r1 & BRK_ABRT)
A
Alan Cox 已提交
389
				flag = TTY_BREAK;
L
Linus Torvalds 已提交
390
			else if (r1 & PAR_ERR)
A
Alan Cox 已提交
391
				flag = TTY_PARITY;
L
Linus Torvalds 已提交
392
			else if (r1 & CRC_ERR)
A
Alan Cox 已提交
393
				flag = TTY_FRAME;
L
Linus Torvalds 已提交
394
		}
395
		if (uart_handle_sysrq_char(&up->port, ch))
L
Linus Torvalds 已提交
396 397 398 399
			continue;

		if (up->port.ignore_status_mask == 0xff ||
		    (r1 & up->port.ignore_status_mask) == 0) {
A
Alan Cox 已提交
400
		    	tty_insert_flip_char(tty, ch, flag);
L
Linus Torvalds 已提交
401
		}
A
Alan Cox 已提交
402 403
		if (r1 & Rx_OVR)
			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
L
Linus Torvalds 已提交
404 405 406 407 408 409
	}

	return tty;
}

static void sunzilog_status_handle(struct uart_sunzilog_port *up,
410
				   struct zilog_channel __iomem *channel)
L
Linus Torvalds 已提交
411 412 413
{
	unsigned char status;

414
	status = readb(&channel->control);
L
Linus Torvalds 已提交
415 416
	ZSDELAY();

417
	writeb(RES_EXT_INT, &channel->control);
L
Linus Torvalds 已提交
418 419 420 421 422
	ZSDELAY();
	ZS_WSYNC(channel);

	if (status & BRK_ABRT) {
		if (ZS_IS_MOUSE(up))
423
			sunzilog_kbdms_receive_chars(up, 0, 1);
L
Linus Torvalds 已提交
424 425 426 427 428
		if (ZS_IS_CONS(up)) {
			/* Wait for BREAK to deassert to avoid potentially
			 * confusing the PROM.
			 */
			while (1) {
429
				status = readb(&channel->control);
L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
				ZSDELAY();
				if (!(status & BRK_ABRT))
					break;
			}
			sun_do_break();
			return;
		}
	}

	if (ZS_WANTS_MODEM_STATUS(up)) {
		if (status & SYNC)
			up->port.icount.dsr++;

		/* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
		 * But it does not tell us which bit has changed, we have to keep
		 * track of this ourselves.
		 */
		if ((status ^ up->prev_status) ^ DCD)
			uart_handle_dcd_change(&up->port,
					       (status & DCD));
		if ((status ^ up->prev_status) ^ CTS)
			uart_handle_cts_change(&up->port,
					       (status & CTS));

		wake_up_interruptible(&up->port.info->delta_msr_wait);
	}

	up->prev_status = status;
}

static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
				    struct zilog_channel __iomem *channel)
{
	struct circ_buf *xmit;

	if (ZS_IS_CONS(up)) {
466
		unsigned char status = readb(&channel->control);
L
Linus Torvalds 已提交
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
		ZSDELAY();

		/* TX still busy?  Just wait for the next TX done interrupt.
		 *
		 * It can occur because of how we do serial console writes.  It would
		 * be nice to transmit console writes just like we normally would for
		 * a TTY line. (ie. buffered and TX interrupt driven).  That is not
		 * easy because console writes cannot sleep.  One solution might be
		 * to poll on enough port->xmit space becomming free.  -DaveM
		 */
		if (!(status & Tx_BUF_EMP))
			return;
	}

	up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;

	if (ZS_REGS_HELD(up)) {
		__load_zsregs(channel, up->curregs);
		up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
	}

	if (ZS_TX_STOPPED(up)) {
		up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
		goto ack_tx_int;
	}

	if (up->port.x_char) {
		up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
495
		writeb(up->port.x_char, &channel->data);
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506
		ZSDELAY();
		ZS_WSYNC(channel);

		up->port.icount.tx++;
		up->port.x_char = 0;
		return;
	}

	if (up->port.info == NULL)
		goto ack_tx_int;
	xmit = &up->port.info->xmit;
507
	if (uart_circ_empty(xmit))
L
Linus Torvalds 已提交
508
		goto ack_tx_int;
509

L
Linus Torvalds 已提交
510 511 512 513
	if (uart_tx_stopped(&up->port))
		goto ack_tx_int;

	up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
514
	writeb(xmit->buf[xmit->tail], &channel->data);
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523 524 525 526
	ZSDELAY();
	ZS_WSYNC(channel);

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

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&up->port);

	return;

ack_tx_int:
527
	writeb(RES_Tx_P, &channel->control);
L
Linus Torvalds 已提交
528 529 530 531
	ZSDELAY();
	ZS_WSYNC(channel);
}

532
static irqreturn_t sunzilog_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
{
	struct uart_sunzilog_port *up = dev_id;

	while (up) {
		struct zilog_channel __iomem *channel
			= ZILOG_CHANNEL_FROM_PORT(&up->port);
		struct tty_struct *tty;
		unsigned char r3;

		spin_lock(&up->port.lock);
		r3 = read_zsreg(channel, R3);

		/* Channel A */
		tty = NULL;
		if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
548
			writeb(RES_H_IUS, &channel->control);
L
Linus Torvalds 已提交
549 550 551 552
			ZSDELAY();
			ZS_WSYNC(channel);

			if (r3 & CHARxIP)
553
				tty = sunzilog_receive_chars(up, channel);
L
Linus Torvalds 已提交
554
			if (r3 & CHAEXT)
555
				sunzilog_status_handle(up, channel);
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
			if (r3 & CHATxIP)
				sunzilog_transmit_chars(up, channel);
		}
		spin_unlock(&up->port.lock);

		if (tty)
			tty_flip_buffer_push(tty);

		/* Channel B */
		up = up->next;
		channel = ZILOG_CHANNEL_FROM_PORT(&up->port);

		spin_lock(&up->port.lock);
		tty = NULL;
		if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
571
			writeb(RES_H_IUS, &channel->control);
L
Linus Torvalds 已提交
572 573 574 575
			ZSDELAY();
			ZS_WSYNC(channel);

			if (r3 & CHBRxIP)
576
				tty = sunzilog_receive_chars(up, channel);
L
Linus Torvalds 已提交
577
			if (r3 & CHBEXT)
578
				sunzilog_status_handle(up, channel);
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
			if (r3 & CHBTxIP)
				sunzilog_transmit_chars(up, channel);
		}
		spin_unlock(&up->port.lock);

		if (tty)
			tty_flip_buffer_push(tty);

		up = up->next;
	}

	return IRQ_HANDLED;
}

/* A convenient way to quickly get R0 status.  The caller must _not_ hold the
 * port lock, it is acquired here.
 */
static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
{
	struct zilog_channel __iomem *channel;
	unsigned char status;

	channel = ZILOG_CHANNEL_FROM_PORT(port);
602
	status = readb(&channel->control);
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610
	ZSDELAY();

	return status;
}

/* The port lock is not held.  */
static unsigned int sunzilog_tx_empty(struct uart_port *port)
{
611
	unsigned long flags;
L
Linus Torvalds 已提交
612 613 614
	unsigned char status;
	unsigned int ret;

615 616
	spin_lock_irqsave(&port->lock, flags);

L
Linus Torvalds 已提交
617
	status = sunzilog_read_channel_status(port);
618 619 620

	spin_unlock_irqrestore(&port->lock, flags);

L
Linus Torvalds 已提交
621 622 623 624 625 626 627 628
	if (status & Tx_BUF_EMP)
		ret = TIOCSER_TEMT;
	else
		ret = 0;

	return ret;
}

629
/* The port lock is held and interrupts are disabled.  */
L
Linus Torvalds 已提交
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 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
static unsigned int sunzilog_get_mctrl(struct uart_port *port)
{
	unsigned char status;
	unsigned int ret;

	status = sunzilog_read_channel_status(port);

	ret = 0;
	if (status & DCD)
		ret |= TIOCM_CAR;
	if (status & SYNC)
		ret |= TIOCM_DSR;
	if (status & CTS)
		ret |= TIOCM_CTS;

	return ret;
}

/* The port lock is held and interrupts are disabled.  */
static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
	struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
	unsigned char set_bits, clear_bits;

	set_bits = clear_bits = 0;

	if (mctrl & TIOCM_RTS)
		set_bits |= RTS;
	else
		clear_bits |= RTS;
	if (mctrl & TIOCM_DTR)
		set_bits |= DTR;
	else
		clear_bits |= DTR;

	/* NOTE: Not subject to 'transmitter active' rule.  */ 
	up->curregs[R5] |= set_bits;
	up->curregs[R5] &= ~clear_bits;
	write_zsreg(channel, R5, up->curregs[R5]);
}

/* The port lock is held and interrupts are disabled.  */
673
static void sunzilog_stop_tx(struct uart_port *port)
L
Linus Torvalds 已提交
674 675 676 677 678 679 680
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;

	up->flags |= SUNZILOG_FLAG_TX_STOPPED;
}

/* The port lock is held and interrupts are disabled.  */
681
static void sunzilog_start_tx(struct uart_port *port)
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
	struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
	unsigned char status;

	up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
	up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;

690
	status = readb(&channel->control);
L
Linus Torvalds 已提交
691 692 693 694 695 696 697 698 699 700
	ZSDELAY();

	/* TX busy?  Just wait for the TX done interrupt.  */
	if (!(status & Tx_BUF_EMP))
		return;

	/* Send the first character to jump-start the TX done
	 * IRQ sending engine.
	 */
	if (port->x_char) {
701
		writeb(port->x_char, &channel->data);
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709
		ZSDELAY();
		ZS_WSYNC(channel);

		port->icount.tx++;
		port->x_char = 0;
	} else {
		struct circ_buf *xmit = &port->info->xmit;

710
		writeb(xmit->buf[xmit->tail], &channel->data);
L
Linus Torvalds 已提交
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 740 741 742 743 744 745 746 747 748 749
		ZSDELAY();
		ZS_WSYNC(channel);

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

		if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
			uart_write_wakeup(&up->port);
	}
}

/* The port lock is held.  */
static void sunzilog_stop_rx(struct uart_port *port)
{
	struct uart_sunzilog_port *up = UART_ZILOG(port);
	struct zilog_channel __iomem *channel;

	if (ZS_IS_CONS(up))
		return;

	channel = ZILOG_CHANNEL_FROM_PORT(port);

	/* Disable all RX interrupts.  */
	up->curregs[R1] &= ~RxINT_MASK;
	sunzilog_maybe_update_regs(up, channel);
}

/* The port lock is held.  */
static void sunzilog_enable_ms(struct uart_port *port)
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
	struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
	unsigned char new_reg;

	new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
	if (new_reg != up->curregs[R15]) {
		up->curregs[R15] = new_reg;

		/* NOTE: Not subject to 'transmitter active' rule.  */ 
750
		write_zsreg(channel, R15, up->curregs[R15] & ~WR7pEN);
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
	}
}

/* The port lock is not held.  */
static void sunzilog_break_ctl(struct uart_port *port, int break_state)
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
	struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
	unsigned char set_bits, clear_bits, new_reg;
	unsigned long flags;

	set_bits = clear_bits = 0;

	if (break_state)
		set_bits |= SND_BRK;
	else
		clear_bits |= SND_BRK;

	spin_lock_irqsave(&port->lock, flags);

	new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
	if (new_reg != up->curregs[R5]) {
		up->curregs[R5] = new_reg;

		/* NOTE: Not subject to 'transmitter active' rule.  */ 
		write_zsreg(channel, R5, up->curregs[R5]);
	}

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

static void __sunzilog_startup(struct uart_sunzilog_port *up)
{
	struct zilog_channel __iomem *channel;

	channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
787
	up->prev_status = readb(&channel->control);
L
Linus Torvalds 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 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 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

	/* Enable receiver and transmitter.  */
	up->curregs[R3] |= RxENAB;
	up->curregs[R5] |= TxENAB;

	up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
	sunzilog_maybe_update_regs(up, channel);
}

static int sunzilog_startup(struct uart_port *port)
{
	struct uart_sunzilog_port *up = UART_ZILOG(port);
	unsigned long flags;

	if (ZS_IS_CONS(up))
		return 0;

	spin_lock_irqsave(&port->lock, flags);
	__sunzilog_startup(up);
	spin_unlock_irqrestore(&port->lock, flags);
	return 0;
}

/*
 * The test for ZS_IS_CONS is explained by the following e-mail:
 *****
 * From: Russell King <rmk@arm.linux.org.uk>
 * Date: Sun, 8 Dec 2002 10:18:38 +0000
 *
 * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
 * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
 * > and I noticed that something is not right with reference
 * > counting in this case. It seems that when the console
 * > is open by kernel initially, this is not accounted
 * > as an open, and uart_startup is not called.
 *
 * That is correct.  We are unable to call uart_startup when the serial
 * console is initialised because it may need to allocate memory (as
 * request_irq does) and the memory allocators may not have been
 * initialised.
 *
 * 1. initialise the port into a state where it can send characters in the
 *    console write method.
 *
 * 2. don't do the actual hardware shutdown in your shutdown() method (but
 *    do the normal software shutdown - ie, free irqs etc)
 *****
 */
static void sunzilog_shutdown(struct uart_port *port)
{
	struct uart_sunzilog_port *up = UART_ZILOG(port);
	struct zilog_channel __iomem *channel;
	unsigned long flags;

	if (ZS_IS_CONS(up))
		return;

	spin_lock_irqsave(&port->lock, flags);

	channel = ZILOG_CHANNEL_FROM_PORT(port);

	/* Disable receiver and transmitter.  */
	up->curregs[R3] &= ~RxENAB;
	up->curregs[R5] &= ~TxENAB;

	/* Disable all interrupts and BRK assertion.  */
	up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
	up->curregs[R5] &= ~SND_BRK;
	sunzilog_maybe_update_regs(up, channel);

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

/* Shared by TTY driver and serial console setup.  The port lock is held
 * and local interrupts are disabled.
 */
static void
sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
		       unsigned int iflag, int brg)
{

	up->curregs[R10] = NRZ;
	up->curregs[R11] = TCBR | RCBR;

	/* Program BAUD and clock source. */
	up->curregs[R4] &= ~XCLK_MASK;
	up->curregs[R4] |= X16CLK;
	up->curregs[R12] = brg & 0xff;
	up->curregs[R13] = (brg >> 8) & 0xff;
	up->curregs[R14] = BRSRC | BRENAB;

	/* Character size, stop bits, and parity. */
880 881
	up->curregs[R3] &= ~RxN_MASK;
	up->curregs[R5] &= ~TxN_MASK;
L
Linus Torvalds 已提交
882 883
	switch (cflag & CSIZE) {
	case CS5:
884 885
		up->curregs[R3] |= Rx5;
		up->curregs[R5] |= Tx5;
L
Linus Torvalds 已提交
886 887 888
		up->parity_mask = 0x1f;
		break;
	case CS6:
889 890
		up->curregs[R3] |= Rx6;
		up->curregs[R5] |= Tx6;
L
Linus Torvalds 已提交
891 892 893
		up->parity_mask = 0x3f;
		break;
	case CS7:
894 895
		up->curregs[R3] |= Rx7;
		up->curregs[R5] |= Tx7;
L
Linus Torvalds 已提交
896 897 898 899
		up->parity_mask = 0x7f;
		break;
	case CS8:
	default:
900 901
		up->curregs[R3] |= Rx8;
		up->curregs[R5] |= Tx8;
L
Linus Torvalds 已提交
902 903 904
		up->parity_mask = 0xff;
		break;
	};
905
	up->curregs[R4] &= ~0x0c;
L
Linus Torvalds 已提交
906
	if (cflag & CSTOPB)
907
		up->curregs[R4] |= SB2;
L
Linus Torvalds 已提交
908
	else
909
		up->curregs[R4] |= SB1;
L
Linus Torvalds 已提交
910
	if (cflag & PARENB)
911
		up->curregs[R4] |= PAR_ENAB;
L
Linus Torvalds 已提交
912
	else
913
		up->curregs[R4] &= ~PAR_ENAB;
L
Linus Torvalds 已提交
914
	if (!(cflag & PARODD))
915
		up->curregs[R4] |= PAR_EVEN;
L
Linus Torvalds 已提交
916
	else
917
		up->curregs[R4] &= ~PAR_EVEN;
L
Linus Torvalds 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

	up->port.read_status_mask = Rx_OVR;
	if (iflag & INPCK)
		up->port.read_status_mask |= CRC_ERR | PAR_ERR;
	if (iflag & (BRKINT | PARMRK))
		up->port.read_status_mask |= BRK_ABRT;

	up->port.ignore_status_mask = 0;
	if (iflag & IGNPAR)
		up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
	if (iflag & IGNBRK) {
		up->port.ignore_status_mask |= BRK_ABRT;
		if (iflag & IGNPAR)
			up->port.ignore_status_mask |= Rx_OVR;
	}

	if ((cflag & CREAD) == 0)
		up->port.ignore_status_mask = 0xff;
}

/* The port lock is not held.  */
static void
A
Alan Cox 已提交
940 941
sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
		     struct ktermios *old)
L
Linus Torvalds 已提交
942 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
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
	unsigned long flags;
	int baud, brg;

	baud = uart_get_baud_rate(port, termios, old, 1200, 76800);

	spin_lock_irqsave(&up->port.lock, flags);

	brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);

	sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);

	if (UART_ENABLE_MS(&up->port, termios->c_cflag))
		up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
	else
		up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;

	up->cflag = termios->c_cflag;

	sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));

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

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

static const char *sunzilog_type(struct uart_port *port)
{
971 972 973
	struct uart_sunzilog_port *up = UART_ZILOG(port);

	return (up->flags & SUNZILOG_FLAG_ESCC) ? "zs (ESCC)" : "zs";
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
}

/* We do not request/release mappings of the registers here, this
 * happens at early serial probe time.
 */
static void sunzilog_release_port(struct uart_port *port)
{
}

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

/* These do not need to do anything interesting either.  */
static void sunzilog_config_port(struct uart_port *port, int flags)
{
}

/* We do not support letting the user mess with the divisor, IRQ, etc. */
static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	return -EINVAL;
}

static struct uart_ops sunzilog_pops = {
	.tx_empty	=	sunzilog_tx_empty,
	.set_mctrl	=	sunzilog_set_mctrl,
	.get_mctrl	=	sunzilog_get_mctrl,
	.stop_tx	=	sunzilog_stop_tx,
	.start_tx	=	sunzilog_start_tx,
	.stop_rx	=	sunzilog_stop_rx,
	.enable_ms	=	sunzilog_enable_ms,
	.break_ctl	=	sunzilog_break_ctl,
	.startup	=	sunzilog_startup,
	.shutdown	=	sunzilog_shutdown,
	.set_termios	=	sunzilog_set_termios,
	.type		=	sunzilog_type,
	.release_port	=	sunzilog_release_port,
	.request_port	=	sunzilog_request_port,
	.config_port	=	sunzilog_config_port,
	.verify_port	=	sunzilog_verify_port,
};

R
Robert Reif 已提交
1018
static int uart_chip_count;
L
Linus Torvalds 已提交
1019 1020 1021 1022 1023 1024 1025
static struct uart_sunzilog_port *sunzilog_port_table;
static struct zilog_layout __iomem **sunzilog_chip_regs;

static struct uart_sunzilog_port *sunzilog_irq_chain;

static struct uart_driver sunzilog_reg = {
	.owner		=	THIS_MODULE,
1026
	.driver_name	=	"sunzilog",
L
Linus Torvalds 已提交
1027 1028 1029 1030
	.dev_name	=	"ttyS",
	.major		=	TTY_MAJOR,
};

1031
static int __init sunzilog_alloc_tables(int num_sunzilog)
L
Linus Torvalds 已提交
1032
{
1033 1034
	struct uart_sunzilog_port *up;
	unsigned long size;
1035
	int num_channels = num_sunzilog * 2;
1036
	int i;
L
Linus Torvalds 已提交
1037

1038
	size = num_channels * sizeof(struct uart_sunzilog_port);
1039 1040 1041
	sunzilog_port_table = kzalloc(size, GFP_KERNEL);
	if (!sunzilog_port_table)
		return -ENOMEM;
L
Linus Torvalds 已提交
1042

1043
	for (i = 0; i < num_channels; i++) {
1044
		up = &sunzilog_port_table[i];
L
Linus Torvalds 已提交
1045

1046
		spin_lock_init(&up->port.lock);
L
Linus Torvalds 已提交
1047

1048 1049
		if (i == 0)
			sunzilog_irq_chain = up;
L
Linus Torvalds 已提交
1050

1051
		if (i < num_channels - 1)
1052 1053 1054
			up->next = up + 1;
		else
			up->next = NULL;
L
Linus Torvalds 已提交
1055 1056
	}

1057
	size = num_sunzilog * sizeof(struct zilog_layout __iomem *);
1058 1059 1060 1061 1062
	sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
	if (!sunzilog_chip_regs) {
		kfree(sunzilog_port_table);
		sunzilog_irq_chain = NULL;
		return -ENOMEM;
L
Linus Torvalds 已提交
1063 1064
	}

1065
	return 0;
L
Linus Torvalds 已提交
1066 1067
}

1068
static void sunzilog_free_tables(void)
L
Linus Torvalds 已提交
1069
{
1070 1071 1072
	kfree(sunzilog_port_table);
	sunzilog_irq_chain = NULL;
	kfree(sunzilog_chip_regs);
L
Linus Torvalds 已提交
1073 1074 1075 1076
}

#define ZS_PUT_CHAR_MAX_DELAY	2000	/* 10 ms */

1077
static void sunzilog_putchar(struct uart_port *port, int ch)
L
Linus Torvalds 已提交
1078
{
A
Al Viro 已提交
1079
	struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085
	int loops = ZS_PUT_CHAR_MAX_DELAY;

	/* This is a timed polling loop so do not switch the explicit
	 * udelay with ZSDELAY as that is a NOP on some platforms.  -DaveM
	 */
	do {
1086
		unsigned char val = readb(&channel->control);
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092 1093
		if (val & Tx_BUF_EMP) {
			ZSDELAY();
			break;
		}
		udelay(5);
	} while (--loops);

1094
	writeb(ch, &channel->data);
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	ZSDELAY();
	ZS_WSYNC(channel);
}

#ifdef CONFIG_SERIO

static DEFINE_SPINLOCK(sunzilog_serio_lock);

static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
{
	struct uart_sunzilog_port *up = serio->port_data;
	unsigned long flags;

	spin_lock_irqsave(&sunzilog_serio_lock, flags);

1110
	sunzilog_putchar(&up->port, ch);
L
Linus Torvalds 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

	spin_unlock_irqrestore(&sunzilog_serio_lock, flags);

	return 0;
}

static int sunzilog_serio_open(struct serio *serio)
{
	struct uart_sunzilog_port *up = serio->port_data;
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&sunzilog_serio_lock, flags);
	if (!up->serio_open) {
		up->serio_open = 1;
		ret = 0;
	} else
		ret = -EBUSY;
	spin_unlock_irqrestore(&sunzilog_serio_lock, flags);

	return ret;
}

static void sunzilog_serio_close(struct serio *serio)
{
	struct uart_sunzilog_port *up = serio->port_data;
	unsigned long flags;

	spin_lock_irqsave(&sunzilog_serio_lock, flags);
	up->serio_open = 0;
	spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
}

#endif /* CONFIG_SERIO */

#ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
static void
sunzilog_console_write(struct console *con, const char *s, unsigned int count)
{
	struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
	unsigned long flags;
1152 1153 1154 1155 1156 1157 1158 1159 1160
	int locked = 1;

	local_irq_save(flags);
	if (up->port.sysrq) {
		locked = 0;
	} else if (oops_in_progress) {
		locked = spin_trylock(&up->port.lock);
	} else
		spin_lock(&up->port.lock);
L
Linus Torvalds 已提交
1161

1162
	uart_console_write(&up->port, s, count, sunzilog_putchar);
L
Linus Torvalds 已提交
1163
	udelay(2);
1164 1165 1166 1167

	if (locked)
		spin_unlock(&up->port.lock);
	local_irq_restore(flags);
L
Linus Torvalds 已提交
1168 1169 1170 1171 1172 1173 1174 1175
}

static int __init sunzilog_console_setup(struct console *con, char *options)
{
	struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
	unsigned long flags;
	int baud, brg;

1176 1177 1178
	if (up->port.type != PORT_SUNZILOG)
		return -1;

L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
	       (sunzilog_reg.minor - 64) + con->index, con->index);

	/* Get firmware console settings.  */
	sunserial_console_termios(con);

	/* Firmware console speed is limited to 150-->38400 baud so
	 * this hackish cflag thing is OK.
	 */
	switch (con->cflag & CBAUD) {
	case B150: baud = 150; break;
	case B300: baud = 300; break;
	case B600: baud = 600; break;
	case B1200: baud = 1200; break;
	case B2400: baud = 2400; break;
	case B4800: baud = 4800; break;
	default: case B9600: baud = 9600; break;
	case B19200: baud = 19200; break;
	case B38400: baud = 38400; break;
	};

	brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);

	spin_lock_irqsave(&up->port.lock, flags);

1204
	up->curregs[R15] |= BRKIE;
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	sunzilog_convert_to_zs(up, con->cflag, 0, brg);

	sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
	__sunzilog_startup(up);

	spin_unlock_irqrestore(&up->port.lock, flags);

	return 0;
}

1215
static struct console sunzilog_console_ops = {
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224
	.name	=	"ttyS",
	.write	=	sunzilog_console_write,
	.device	=	uart_console_device,
	.setup	=	sunzilog_console_setup,
	.flags	=	CON_PRINTBUFFER,
	.index	=	-1,
	.data   =	&sunzilog_reg,
};

1225 1226
static inline struct console *SUNZILOG_CONSOLE(void)
{
1227
	return &sunzilog_console_ops;
1228 1229
}

L
Linus Torvalds 已提交
1230
#else
1231
#define SUNZILOG_CONSOLE()	(NULL)
L
Linus Torvalds 已提交
1232 1233
#endif

1234
static void __devinit sunzilog_init_kbdms(struct uart_sunzilog_port *up)
L
Linus Torvalds 已提交
1235 1236 1237
{
	int baud, brg;

1238
	if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
L
Linus Torvalds 已提交
1239 1240 1241 1242 1243 1244 1245
		up->cflag = B1200 | CS8 | CLOCAL | CREAD;
		baud = 1200;
	} else {
		up->cflag = B4800 | CS8 | CLOCAL | CREAD;
		baud = 4800;
	}

1246
	up->curregs[R15] |= BRKIE;
L
Linus Torvalds 已提交
1247 1248 1249 1250 1251 1252 1253
	brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
	sunzilog_convert_to_zs(up, up->cflag, 0, brg);
	sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
	__sunzilog_startup(up);
}

#ifdef CONFIG_SERIO
1254
static void __devinit sunzilog_register_serio(struct uart_sunzilog_port *up)
L
Linus Torvalds 已提交
1255
{
1256
	struct serio *serio = &up->serio;
L
Linus Torvalds 已提交
1257

1258
	serio->port_data = up;
L
Linus Torvalds 已提交
1259

1260
	serio->id.type = SERIO_RS232;
1261
	if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
1262 1263
		serio->id.proto = SERIO_SUNKBD;
		strlcpy(serio->name, "zskbd", sizeof(serio->name));
L
Linus Torvalds 已提交
1264
	} else {
1265 1266 1267
		serio->id.proto = SERIO_SUN;
		serio->id.extra = 1;
		strlcpy(serio->name, "zsms", sizeof(serio->name));
L
Linus Torvalds 已提交
1268
	}
1269
	strlcpy(serio->phys,
1270 1271
		((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
		 "zs/serio0" : "zs/serio1"),
1272 1273 1274 1275 1276 1277 1278 1279
		sizeof(serio->phys));

	serio->write = sunzilog_serio_write;
	serio->open = sunzilog_serio_open;
	serio->close = sunzilog_serio_close;
	serio->dev.parent = up->port.dev;

	serio_register_port(serio);
L
Linus Torvalds 已提交
1280 1281 1282
}
#endif

1283
static void __devinit sunzilog_init_hw(struct uart_sunzilog_port *up)
L
Linus Torvalds 已提交
1284
{
1285 1286 1287
	struct zilog_channel __iomem *channel;
	unsigned long flags;
	int baud, brg;
L
Linus Torvalds 已提交
1288

1289
	channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
L
Linus Torvalds 已提交
1290

1291 1292 1293 1294 1295 1296
	spin_lock_irqsave(&up->port.lock, flags);
	if (ZS_IS_CHANNEL_A(up)) {
		write_zsreg(channel, R9, FHWRES);
		ZSDELAY_LONG();
		(void) read_zsreg(channel, R0);
	}
L
Linus Torvalds 已提交
1297

1298 1299
	if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
			 SUNZILOG_FLAG_CONS_MOUSE)) {
1300 1301 1302 1303 1304 1305 1306 1307
		up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
		up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
		up->curregs[R3] = RxENAB | Rx8;
		up->curregs[R5] = TxENAB | Tx8;
		up->curregs[R6] = 0x00; /* SDLC Address */
		up->curregs[R7] = 0x7E; /* SDLC Flag    */
		up->curregs[R9] = NV;
		up->curregs[R7p] = 0x00;
1308
		sunzilog_init_kbdms(up);
1309 1310 1311
		/* Only enable interrupts if an ISR handler available */
		if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
			up->curregs[R9] |= MIE;
1312 1313 1314 1315 1316 1317 1318 1319
		write_zsreg(channel, R9, up->curregs[R9]);
	} else {
		/* Normal serial TTY. */
		up->parity_mask = 0xff;
		up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
		up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
		up->curregs[R3] = RxENAB | Rx8;
		up->curregs[R5] = TxENAB | Tx8;
1320 1321 1322
		up->curregs[R6] = 0x00; /* SDLC Address */
		up->curregs[R7] = 0x7E; /* SDLC Flag    */
		up->curregs[R9] = NV;
1323 1324 1325 1326 1327 1328 1329
		up->curregs[R10] = NRZ;
		up->curregs[R11] = TCBR | RCBR;
		baud = 9600;
		brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
		up->curregs[R12] = (brg & 0xff);
		up->curregs[R13] = (brg >> 8) & 0xff;
		up->curregs[R14] = BRSRC | BRENAB;
1330 1331 1332 1333 1334 1335 1336 1337
		up->curregs[R15] = FIFOEN; /* Use FIFO if on ESCC */
		up->curregs[R7p] = TxFIFO_LVL | RxFIFO_LVL;
		if (__load_zsregs(channel, up->curregs)) {
			up->flags |= SUNZILOG_FLAG_ESCC;
		}
		/* Only enable interrupts if an ISR handler available */
		if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
			up->curregs[R9] |= MIE;
1338 1339
		write_zsreg(channel, R9, up->curregs[R9]);
	}
L
Linus Torvalds 已提交
1340

1341
	spin_unlock_irqrestore(&up->port.lock, flags);
L
Linus Torvalds 已提交
1342 1343

#ifdef CONFIG_SERIO
1344 1345 1346
	if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
			 SUNZILOG_FLAG_CONS_MOUSE))
		sunzilog_register_serio(up);
L
Linus Torvalds 已提交
1347 1348 1349
#endif
}

1350 1351
static int zilog_irq = -1;

1352
static int __devinit zs_probe(struct of_device *op, const struct of_device_id *match)
1353
{
R
Robert Reif 已提交
1354 1355
	static int kbm_inst, uart_inst;
	int inst;
1356 1357
	struct uart_sunzilog_port *up;
	struct zilog_layout __iomem *rp;
R
Robert Reif 已提交
1358
	int keyboard_mouse = 0;
1359
	int err;
L
Linus Torvalds 已提交
1360

1361 1362 1363
	if (of_find_property(op->node, "keyboard", NULL))
		keyboard_mouse = 1;

R
Robert Reif 已提交
1364 1365 1366 1367 1368 1369
	/* uarts must come before keyboards/mice */
	if (keyboard_mouse)
		inst = uart_chip_count + kbm_inst;
	else
		inst = uart_inst;

1370 1371 1372 1373 1374
	sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
					      sizeof(struct zilog_layout),
					      "zs");
	if (!sunzilog_chip_regs[inst])
		return -ENOMEM;
L
Linus Torvalds 已提交
1375

1376
	rp = sunzilog_chip_regs[inst];
L
Linus Torvalds 已提交
1377

1378
	if (zilog_irq == -1)
1379
		zilog_irq = op->irqs[0];
1380

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
	up = &sunzilog_port_table[inst * 2];

	/* Channel A */
	up[0].port.mapbase = op->resource[0].start + 0x00;
	up[0].port.membase = (void __iomem *) &rp->channelA;
	up[0].port.iotype = UPIO_MEM;
	up[0].port.irq = op->irqs[0];
	up[0].port.uartclk = ZS_CLOCK;
	up[0].port.fifosize = 1;
	up[0].port.ops = &sunzilog_pops;
	up[0].port.type = PORT_SUNZILOG;
	up[0].port.flags = 0;
	up[0].port.line = (inst * 2) + 0;
	up[0].port.dev = &op->dev;
	up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
1396
	if (keyboard_mouse)
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
		up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
	sunzilog_init_hw(&up[0]);

	/* Channel B */
	up[1].port.mapbase = op->resource[0].start + 0x04;
	up[1].port.membase = (void __iomem *) &rp->channelB;
	up[1].port.iotype = UPIO_MEM;
	up[1].port.irq = op->irqs[0];
	up[1].port.uartclk = ZS_CLOCK;
	up[1].port.fifosize = 1;
	up[1].port.ops = &sunzilog_pops;
	up[1].port.type = PORT_SUNZILOG;
	up[1].port.flags = 0;
	up[1].port.line = (inst * 2) + 1;
	up[1].port.dev = &op->dev;
	up[1].flags |= 0;
1413
	if (keyboard_mouse)
1414 1415 1416
		up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
	sunzilog_init_hw(&up[1]);

1417
	if (!keyboard_mouse) {
1418 1419 1420
		if (sunserial_console_match(SUNZILOG_CONSOLE(), op->node,
					    &sunzilog_reg, up[0].port.line))
			up->flags |= SUNZILOG_FLAG_IS_CONS;
1421 1422
		err = uart_add_one_port(&sunzilog_reg, &up[0].port);
		if (err) {
1423 1424
			of_iounmap(&op->resource[0],
				   rp, sizeof(struct zilog_layout));
1425 1426
			return err;
		}
1427 1428 1429
		if (sunserial_console_match(SUNZILOG_CONSOLE(), op->node,
					    &sunzilog_reg, up[1].port.line))
			up->flags |= SUNZILOG_FLAG_IS_CONS;
1430 1431 1432
		err = uart_add_one_port(&sunzilog_reg, &up[1].port);
		if (err) {
			uart_remove_one_port(&sunzilog_reg, &up[0].port);
1433 1434
			of_iounmap(&op->resource[0],
				   rp, sizeof(struct zilog_layout));
1435
			return err;
L
Linus Torvalds 已提交
1436
		}
R
Robert Reif 已提交
1437
		uart_inst++;
1438
	} else {
1439
		printk(KERN_INFO "%s: Keyboard at MMIO 0x%llx (irq = %d) "
1440
		       "is a %s\n",
1441 1442 1443 1444
		       op->dev.bus_id,
		       (unsigned long long) up[0].port.mapbase,
		       op->irqs[0], sunzilog_type(&up[0].port));
		printk(KERN_INFO "%s: Mouse at MMIO 0x%llx (irq = %d) "
1445
		       "is a %s\n",
1446 1447 1448
		       op->dev.bus_id,
		       (unsigned long long) up[1].port.mapbase,
		       op->irqs[0], sunzilog_type(&up[1].port));
R
Robert Reif 已提交
1449
		kbm_inst++;
L
Linus Torvalds 已提交
1450 1451
	}

1452
	dev_set_drvdata(&op->dev, &up[0]);
1453

1454
	return 0;
L
Linus Torvalds 已提交
1455 1456
}

1457
static void __devexit zs_remove_one(struct uart_sunzilog_port *up)
L
Linus Torvalds 已提交
1458
{
1459 1460 1461 1462 1463 1464
	if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
#ifdef CONFIG_SERIO
		serio_unregister_port(&up->serio);
#endif
	} else
		uart_remove_one_port(&sunzilog_reg, &up->port);
1465
}
L
Linus Torvalds 已提交
1466

1467
static int __devexit zs_remove(struct of_device *op)
1468
{
1469
	struct uart_sunzilog_port *up = dev_get_drvdata(&op->dev);
1470 1471 1472 1473
	struct zilog_layout __iomem *regs;

	zs_remove_one(&up[0]);
	zs_remove_one(&up[1]);
L
Linus Torvalds 已提交
1474

1475
	regs = sunzilog_chip_regs[up[0].port.line / 2];
1476
	of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
1477

1478
	dev_set_drvdata(&op->dev, NULL);
L
Linus Torvalds 已提交
1479

1480
	return 0;
L
Linus Torvalds 已提交
1481 1482
}

1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
static struct of_device_id zs_match[] = {
	{
		.name = "zs",
	},
	{},
};
MODULE_DEVICE_TABLE(of, zs_match);

static struct of_platform_driver zs_driver = {
	.name		= "zs",
	.match_table	= zs_match,
	.probe		= zs_probe,
	.remove		= __devexit_p(zs_remove),
};

L
Linus Torvalds 已提交
1498 1499
static int __init sunzilog_init(void)
{
1500
	struct device_node *dp;
R
Robert Reif 已提交
1501 1502
	int err;
	int num_keybms = 0;
1503
	int num_sunzilog = 0;
L
Linus Torvalds 已提交
1504

1505
	for_each_node_by_name(dp, "zs") {
1506
		num_sunzilog++;
1507 1508 1509
		if (of_find_property(dp, "keyboard", NULL))
			num_keybms++;
	}
L
Linus Torvalds 已提交
1510

1511 1512
	if (num_sunzilog) {
		err = sunzilog_alloc_tables(num_sunzilog);
1513
		if (err)
1514
			goto out;
L
Linus Torvalds 已提交
1515

R
Robert Reif 已提交
1516
		uart_chip_count = num_sunzilog - num_keybms;
1517

R
Robert Reif 已提交
1518 1519
		err = sunserial_register_minors(&sunzilog_reg,
						uart_chip_count * 2);
1520 1521
		if (err)
			goto out_free_tables;
1522 1523
	}

1524 1525 1526 1527 1528
	err = of_register_driver(&zs_driver, &of_bus_type);
	if (err)
		goto out_unregister_uart;

	if (zilog_irq != -1) {
1529
		struct uart_sunzilog_port *up = sunzilog_irq_chain;
1530 1531 1532 1533
		err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
				  "zs", sunzilog_irq_chain);
		if (err)
			goto out_unregister_driver;
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

		/* Enable Interrupts */
		while (up) {
			struct zilog_channel __iomem *channel;

			/* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
			channel          = ZILOG_CHANNEL_FROM_PORT(&up->port);
			up->flags       |= SUNZILOG_FLAG_ISR_HANDLER;
			up->curregs[R9] |= MIE;
			write_zsreg(channel, R9, up->curregs[R9]);
			up = up->next;
		}
1546 1547 1548 1549 1550 1551 1552 1553 1554
	}

out:
	return err;

out_unregister_driver:
	of_unregister_driver(&zs_driver);

out_unregister_uart:
1555 1556
	if (num_sunzilog) {
		sunserial_unregister_minors(&sunzilog_reg, num_sunzilog);
1557 1558 1559 1560 1561 1562
		sunzilog_reg.cons = NULL;
	}

out_free_tables:
	sunzilog_free_tables();
	goto out;
L
Linus Torvalds 已提交
1563 1564 1565 1566
}

static void __exit sunzilog_exit(void)
{
1567
	of_unregister_driver(&zs_driver);
L
Linus Torvalds 已提交
1568

1569
	if (zilog_irq != -1) {
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
		struct uart_sunzilog_port *up = sunzilog_irq_chain;

		/* Disable Interrupts */
		while (up) {
			struct zilog_channel __iomem *channel;

			/* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
			channel          = ZILOG_CHANNEL_FROM_PORT(&up->port);
			up->flags       &= ~SUNZILOG_FLAG_ISR_HANDLER;
			up->curregs[R9] &= ~MIE;
			write_zsreg(channel, R9, up->curregs[R9]);
			up = up->next;
		}

1584 1585 1586 1587
		free_irq(zilog_irq, sunzilog_irq_chain);
		zilog_irq = -1;
	}

1588 1589
	if (sunzilog_reg.nr) {
		sunserial_unregister_minors(&sunzilog_reg, sunzilog_reg.nr);
1590
		sunzilog_free_tables();
L
Linus Torvalds 已提交
1591 1592 1593 1594 1595 1596 1597 1598
	}
}

module_init(sunzilog_init);
module_exit(sunzilog_exit);

MODULE_AUTHOR("David S. Miller");
MODULE_DESCRIPTION("Sun Zilog serial port driver");
1599
MODULE_VERSION("2.0");
L
Linus Torvalds 已提交
1600
MODULE_LICENSE("GPL");