sunzilog.c 42.3 KB
Newer Older
1 2 3 4
/*
 * sunzilog.c
 *
 * Driver for Zilog serial chips found on Sun workstations and
5
 * servers.  This driver could actually be made more generic.
6
 *
7
 * This is based on the old drivers/sbus/char/zs.c code.  A lot
8 9
 * of code has been simply moved over directly from there but
 * much has been rewritten.  Credits therefore go out to Eddie
10
 * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
11 12 13 14 15
 * work there.
 *
 *  Copyright (C) 2002 David S. Miller (davem@redhat.com)
 */

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.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/console.h>
#include <linux/spinlock.h>
#ifdef CONFIG_SERIO
#include <linux/serio.h>
#endif
#include <linux/init.h>

#include <asm/io.h>
#include <asm/irq.h>
40 41 42 43
#ifdef CONFIG_SPARC64
#include <asm/fhc.h>
#endif
#include <asm/sbus.h>
44 45 46

#include <linux/serial_core.h>

47
#include "suncore.h"
48 49 50 51 52 53 54
#include "sunzilog.h"

/* On 32-bit sparcs we need to delay after register accesses
 * to accomodate sun4 systems, but we do not need to flush writes.
 * On 64-bit sparc we only need to flush single writes to ensure
 * completion.
 */
55
#ifndef CONFIG_SPARC64
56 57
#define ZSDELAY()		udelay(5)
#define ZSDELAY_LONG()		udelay(20)
58
#define ZS_WSYNC(channel)	do { } while (0)
59 60 61 62 63 64 65
#else
#define ZSDELAY()
#define ZSDELAY_LONG()
#define ZS_WSYNC(__channel) \
	sbus_readb(&((__channel)->control))
#endif

66
static int num_sunzilog;
67 68 69
#define NUM_SUNZILOG	num_sunzilog
#define NUM_CHANNELS	(NUM_SUNZILOG * 2)

70 71 72 73 74 75
#define KEYBOARD_LINE 0x2
#define MOUSE_LINE    0x3

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

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
 * 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
95 96 97
#define SUNZILOG_FLAG_REGS_HELD		0x00000040
#define SUNZILOG_FLAG_TX_STOPPED	0x00000080
#define SUNZILOG_FLAG_TX_ACTIVE		0x00000100
98

99 100
	unsigned int cflag;

101 102 103 104 105 106
	/* L1-A keyboard break state.  */
	int				kbd_id;
	int				l1_down;

	unsigned char			parity_mask;
	unsigned char			prev_status;
107 108 109 110

#ifdef CONFIG_SERIO
	struct serio			serio;
#endif
111 112 113 114 115 116 117 118
};

#define ZILOG_CHANNEL_FROM_PORT(PORT)	((struct zilog_channel *)((PORT)->membase))
#define UART_ZILOG(PORT)		((struct uart_sunzilog_port *)(PORT))
#define SUNZILOG_GET_CURR_REG(PORT, REGNUM)		\
	(UART_ZILOG(PORT)->curregs[REGNUM])
#define SUNZILOG_SET_CURR_REG(PORT, REGNUM, REGVAL)	\
	((UART_ZILOG(PORT)->curregs[REGNUM]) = (REGVAL))
119 120 121 122 123 124 125 126 127
#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)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

/* 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 *channel,
				unsigned char reg)
{
	unsigned char retval;

	sbus_writeb(reg, &channel->control);
	ZSDELAY();
	retval = sbus_readb(&channel->control);
	ZSDELAY();

	return retval;
}

static void write_zsreg(struct zilog_channel *channel,
			unsigned char reg, unsigned char value)
{
	sbus_writeb(reg, &channel->control);
	ZSDELAY();
	sbus_writeb(value, &channel->control);
	ZSDELAY();
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static void sunzilog_clear_fifo(struct zilog_channel *channel)
{
	int i;

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

		regval = sbus_readb(&channel->control);
		ZSDELAY();
		if (regval & Rx_CH_AV)
			break;

		regval = read_zsreg(channel, R1);
		sbus_readb(&channel->data);
		ZSDELAY();

		if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
			sbus_writeb(ERR_RES, &channel->control);
			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.
 */
static void __load_zsregs(struct zilog_channel *channel, unsigned char *regs)
187 188 189
{
	int i;

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

198 199 200 201 202
	sbus_writeb(ERR_RES, &channel->control);
	ZSDELAY();
	ZS_WSYNC(channel);

	sunzilog_clear_fifo(channel);
203

204 205 206 207 208
	/* 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.  */
209
	write_zsreg(channel, R4, regs[R4]);
210 211 212 213 214

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

	/* Set TX/RX controls sans the enable bits.  */
215 216
	write_zsreg(channel, R3, regs[R3] & ~RxENAB);
	write_zsreg(channel, R5, regs[R5] & ~TxENAB);
217 218 219 220 221 222 223 224 225 226 227 228 229 230

	/* 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.  */
231
	write_zsreg(channel, R11, regs[R11]);
232 233

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

	/* External status interrupt control.  */
241
	write_zsreg(channel, R15, regs[R15]);
242 243

	/* Reset external status interrupts.  */
244
	write_zsreg(channel, R0, RES_EXT_INT);
245 246 247 248 249 250 251
	write_zsreg(channel, R0, RES_EXT_INT);

	/* 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.  */
252 253 254
	write_zsreg(channel, R1, regs[R1]);
}

255 256 257 258 259 260
/* 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.
 */
261 262
static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
				       struct zilog_channel *channel)
263
{
264 265 266 267 268 269 270
	if (!ZS_REGS_HELD(up)) {
		if (ZS_TX_ACTIVE(up)) {
			up->flags |= SUNZILOG_FLAG_REGS_HELD;
		} else {
			__load_zsregs(channel, up->curregs);
		}
	}
271 272
}

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

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

281 282 283 284
	brg = BPS_TO_BRG(new_baud,
			 (ZS_CLOCK / ZS_CLOCK_DIVISOR));
	up->curregs[R12] = (brg & 0xff);
	up->curregs[R13] = (brg >> 8) & 0xff;
285
	sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
286 287
}

288
static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
289 290
					 unsigned char ch, int is_break,
					 struct pt_regs *regs)
291
{
292 293 294 295 296 297
	if (ZS_IS_KEYB(up)) {
		if (ch == SUNKBD_RESET) {
			up->kbd_id = 1;
			up->l1_down = 0;
		} else if (up->kbd_id) {
			up->kbd_id = 0;
298
		} else if (ch == SUNKBD_L1) {
299
			up->l1_down = 1;
300
		} else if (ch == (SUNKBD_L1 | SUNKBD_UP)) {
301 302 303 304 305 306 307
			up->l1_down = 0;
		} else if (ch == SUNKBD_A && up->l1_down) {
			sun_do_break();
			up->l1_down = 0;
			up->kbd_id = 0;
			return;
		}
308
		kbd_pt_regs = regs;
309 310 311 312 313 314 315 316 317 318 319 320
#ifdef CONFIG_SERIO
		serio_interrupt(&up->serio, ch, 0);
#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;
321

322 323 324 325 326 327 328
		case 0:
#ifdef CONFIG_SERIO
			serio_interrupt(&up->serio, ch, 0);
#endif
			break;
		};
	}
329 330
}

331
static void sunzilog_receive_chars(struct uart_sunzilog_port *up,
332
				   struct zilog_channel *channel,
333 334
				   struct pt_regs *regs)
{
335
	struct tty_struct *tty = up->port.info->tty;
336 337 338 339 340

	while (1) {
		unsigned char ch, r1;

		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
Ingo Molnar's avatar
Ingo Molnar committed
341
			tty->flip.work.func((void *)tty);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
				return;
		}

		r1 = read_zsreg(channel, R1);
		if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
			sbus_writeb(ERR_RES, &channel->control);
			ZSDELAY();
			ZS_WSYNC(channel);
		}

		ch = sbus_readb(&channel->control);
		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;

		ch = sbus_readb(&channel->data);
		ZSDELAY();

365 366 367
		ch &= up->parity_mask;

		if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
368
			sunzilog_kbdms_receive_chars(up, ch, 0, regs);
369 370
			goto next_char;
		}
371 372 373 374 375 376 377 378 379 380 381

		if (ZS_IS_CONS(up) && (r1 & BRK_ABRT)) {
			/* Wait for BREAK to deassert to avoid potentially
			 * confusing the PROM.
			 */
			while (1) {
				ch = sbus_readb(&channel->control);
				ZSDELAY();
				if (!(ch & BRK_ABRT))
					break;
			}
382 383 384 385 386 387 388
			sun_do_break();
			return;
		}

		/* A real serial line, record the character and status.  */
		*tty->flip.char_buf_ptr = ch;
		*tty->flip.flag_buf_ptr = TTY_NORMAL;
389
		up->port.icount.rx++;
390 391 392
		if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
			if (r1 & BRK_ABRT) {
				r1 &= ~(PAR_ERR | CRC_ERR);
393
				up->port.icount.brk++;
394
				if (uart_handle_break(&up->port))
395 396 397
					goto next_char;
			}
			else if (r1 & PAR_ERR)
398
				up->port.icount.parity++;
399
			else if (r1 & CRC_ERR)
400
				up->port.icount.frame++;
401
			if (r1 & Rx_OVR)
402 403
				up->port.icount.overrun++;
			r1 &= up->port.read_status_mask;
404 405 406 407 408 409 410
			if (r1 & BRK_ABRT)
				*tty->flip.flag_buf_ptr = TTY_BREAK;
			else if (r1 & PAR_ERR)
				*tty->flip.flag_buf_ptr = TTY_PARITY;
			else if (r1 & CRC_ERR)
				*tty->flip.flag_buf_ptr = TTY_FRAME;
		}
411
		if (uart_handle_sysrq_char(&up->port, ch, regs))
412 413
			goto next_char;

414
		if (up->port.ignore_status_mask == 0xff ||
415
		    (r1 & up->port.ignore_status_mask) == 0) {
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
			tty->flip.flag_buf_ptr++;
			tty->flip.char_buf_ptr++;
			tty->flip.count++;
		}
		if ((r1 & Rx_OVR) &&
		    tty->flip.count < TTY_FLIPBUF_SIZE) {
			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
			tty->flip.flag_buf_ptr++;
			tty->flip.char_buf_ptr++;
			tty->flip.count++;
		}
	next_char:
		ch = sbus_readb(&channel->control);
		ZSDELAY();
		if (!(ch & Rx_CH_AV))
			break;
	}

	tty_flip_buffer_push(tty);
}

437
static void sunzilog_status_handle(struct uart_sunzilog_port *up,
438 439
				   struct zilog_channel *channel,
				   struct pt_regs *regs)
440 441 442 443 444 445 446 447 448 449
{
	unsigned char status;

	status = sbus_readb(&channel->control);
	ZSDELAY();

	sbus_writeb(RES_EXT_INT, &channel->control);
	ZSDELAY();
	ZS_WSYNC(channel);

450
	if ((status & BRK_ABRT) && ZS_IS_MOUSE(up))
451
		sunzilog_kbdms_receive_chars(up, 0, 1, regs);
452

453
	if (ZS_WANTS_MODEM_STATUS(up)) {
454
		if (status & SYNC)
455
			up->port.icount.dsr++;
456 457 458 459 460

		/* 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.
		 */
461 462 463 464 465
		if ((status & DCD) ^ up->prev_status)
			uart_handle_dcd_change(&up->port,
					       (status & DCD));
		if ((status & CTS) ^ up->prev_status)
			uart_handle_cts_change(&up->port,
466 467
					       (status & CTS));

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

471
	up->prev_status = status;
472 473
}

474
static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
475
				    struct zilog_channel *channel)
476
{
477
	struct circ_buf *xmit = &up->port.info->xmit;
478

479 480 481
	if (ZS_IS_CONS(up)) {
		unsigned char status = sbus_readb(&channel->control);
		ZSDELAY();
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
		/* 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;
	}

	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 disable_tx_int;
	}
504

505
	if (up->port.x_char) {
506
		sbus_writeb(up->port.x_char, &channel->data);
507 508 509
		ZSDELAY();
		ZS_WSYNC(channel);

510 511
		up->port.icount.tx++;
		up->port.x_char = 0;
512 513 514
		return;
	}

515 516
	if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port))
		goto disable_tx_int;
517 518 519 520 521 522

	sbus_writeb(xmit->buf[xmit->tail], &channel->data);
	ZSDELAY();
	ZS_WSYNC(channel);

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

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
526
		uart_write_wakeup(&up->port);
527 528 529

	if (!uart_circ_empty(xmit))
		return;
530

531 532 533
disable_tx_int:
	up->curregs[R5] &= ~TxENAB;
	write_zsreg(ZILOG_CHANNEL_FROM_PORT(&up->port), R5, up->curregs[R5]);
534 535 536 537
}

static void sunzilog_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
538
	struct uart_sunzilog_port *up = dev_id;
539

540
	while (up) {
541
		struct zilog_channel *channel
542
			= ZILOG_CHANNEL_FROM_PORT(&up->port);
543
		unsigned char r3;
544

545
		spin_lock(&up->port.lock);
546 547 548 549 550 551 552 553 554
		r3 = read_zsreg(channel, 3);

		/* Channel A */
		if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
			sbus_writeb(RES_H_IUS, &channel->control);
			ZSDELAY();
			ZS_WSYNC(channel);

			if (r3 & CHARxIP)
555
				sunzilog_receive_chars(up, channel, regs);
556
			if (r3 & CHAEXT)
557
				sunzilog_status_handle(up, channel, regs);
558
			if (r3 & CHATxIP)
559
				sunzilog_transmit_chars(up, channel);
560
		}
561
		spin_unlock(&up->port.lock);
562 563

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

567
		spin_lock(&up->port.lock);
568 569 570 571 572 573
		if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
			sbus_writeb(RES_H_IUS, &channel->control);
			ZSDELAY();
			ZS_WSYNC(channel);

			if (r3 & CHBRxIP)
574
				sunzilog_receive_chars(up, channel, regs);
575
			if (r3 & CHBEXT)
576
				sunzilog_status_handle(up, channel, regs);
577
			if (r3 & CHBTxIP)
578
				sunzilog_transmit_chars(up, channel);
579
		}
580
		spin_unlock(&up->port.lock);
581

582
		up = up->next;
583
	}
584 585 586 587 588 589 590 591 592 593 594 595 596 597
}

/* 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 *channel;
	unsigned long flags;
	unsigned char status;

	spin_lock_irqsave(&port->lock, flags);

	channel = ZILOG_CHANNEL_FROM_PORT(port);
598
	status = sbus_readb(&channel->control);
599 600 601 602 603
	ZSDELAY();

	spin_unlock_irqrestore(&port->lock, flags);

	return status;
604 605 606 607 608 609 610 611 612 613
}

/* The port lock is not held.  */
static unsigned int sunzilog_tx_empty(struct uart_port *port)
{
	unsigned char status;
	unsigned int ret;

	status = sunzilog_read_channel_status(port);
	if (status & Tx_BUF_EMP)
614
		ret = TIOCSER_TEMT;
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	else
		ret = 0;

	return ret;
}

/* The port lock is not held.  */
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)
{
643
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
644
	struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
645 646 647 648 649 650 651 652 653 654 655 656 657
	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;

658 659 660 661
	/* NOTE: Not subject to 'transmitter active' rule.  */ 
	up->curregs[R5] |= set_bits;
	up->curregs[R5] &= ~clear_bits;
	write_zsreg(channel, R5, up->curregs[R5]);
662 663 664 665 666
}

/* The port lock is held and interrupts are disabled.  */
static void sunzilog_stop_tx(struct uart_port *port, unsigned int tty_stop)
{
667 668 669
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;

	up->flags |= SUNZILOG_FLAG_TX_STOPPED;
670 671 672 673 674 675
}

/* The port lock is held and interrupts are disabled.  */
static void sunzilog_start_tx(struct uart_port *port, unsigned int tty_start)
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
676
	struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
677 678
	unsigned char status;

679 680
	up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
	up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
681

682 683 684 685 686 687
	/* Enable the transmitter.  */
	if (!(up->curregs[R5] & TxENAB)) {
		/* NOTE: Not subject to 'transmitter active' rule.  */ 
		up->curregs[R5] |= TxENAB;
		write_zsreg(channel, R5, up->curregs[R5]);
	}
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

	status = sbus_readb(&channel->control);
	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) {
		sbus_writeb(port->x_char, &channel->data);
		ZSDELAY();
		ZS_WSYNC(channel);

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

709
		sbus_writeb(xmit->buf[xmit->tail], &channel->data);
710 711 712 713 714 715 716
		ZSDELAY();
		ZS_WSYNC(channel);

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

		if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
717
			uart_write_wakeup(&up->port);
718 719 720 721 722 723
	}
}

/* The port lock is not held.  */
static void sunzilog_stop_rx(struct uart_port *port)
{
724
	struct uart_sunzilog_port *up = UART_ZILOG(port);
725
	struct zilog_channel *channel;
726 727 728 729 730 731 732 733
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

	channel = ZILOG_CHANNEL_FROM_PORT(port);

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

	spin_unlock_irqrestore(&port->lock, flags);
737 738 739 740 741
}

/* The port lock is not held.  */
static void sunzilog_enable_ms(struct uart_port *port)
{
742
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
743
	struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
744
	unsigned char new_reg;
745 746 747
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
748 749 750 751 752 753 754 755 756

	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.  */ 
		write_zsreg(channel, R15, up->curregs[R15]);
	}

757 758 759 760 761 762
	spin_unlock_irqrestore(&port->lock, flags);
}

/* The port lock is not held.  */
static void sunzilog_break_ctl(struct uart_port *port, int break_state)
{
763
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
764
	struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
765
	unsigned char set_bits, clear_bits, new_reg;
766 767 768 769 770 771 772 773 774 775
	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);
776 777 778 779 780 781 782 783 784

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

785 786 787 788 789
	spin_unlock_irqrestore(&port->lock, flags);
}

static int sunzilog_startup(struct uart_port *port)
{
790
	struct uart_sunzilog_port *up = UART_ZILOG(port);
791
	struct zilog_channel *channel;
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

	channel = ZILOG_CHANNEL_FROM_PORT(port);
	up->prev_status = sbus_readb(&channel->control);

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

	/* Enable RX and status interrupts.  TX interrupts are enabled
	 * as needed.
	 */
	up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx;
	up->curregs[R9] |= MIE;
808
	sunzilog_maybe_update_regs(up, channel);
809 810

	spin_unlock_irqrestore(&port->lock, flags);
811 812

	return 0;
813 814 815 816
}

static void sunzilog_shutdown(struct uart_port *port)
{
817
	struct uart_sunzilog_port *up = UART_ZILOG(port);
818
	struct zilog_channel *channel;
819 820 821 822 823 824 825 826 827 828 829 830 831 832
	unsigned long flags;

	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;
	up->curregs[R9] &= ~MIE;
833
	sunzilog_maybe_update_regs(up, channel);
834 835

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

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

846 847 848 849 850
	/* Don't modify MIE. */
	up->curregs[R9] |= NV;

	up->curregs[R10] = NRZ;
	up->curregs[11] = TCBR | RCBR;
851 852 853 854

	/* Program BAUD and clock source. */
	up->curregs[4] &= ~XCLK_MASK;
	up->curregs[4] |= X16CLK;
855 856
	up->curregs[12] = brg & 0xff;
	up->curregs[13] = (brg >> 8) & 0xff;
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
	up->curregs[14] = BRSRC | BRENAB;

	/* Character size, stop bits, and parity. */
	up->curregs[3] &= ~RxN_MASK;
	up->curregs[5] &= ~TxN_MASK;
	switch (cflag & CSIZE) {
	case CS5:
		up->curregs[3] |= Rx5;
		up->curregs[5] |= Tx5;
		up->parity_mask = 0x1f;
		break;
	case CS6:
		up->curregs[3] |= Rx6;
		up->curregs[5] |= Tx6;
		up->parity_mask = 0x3f;
		break;
	case CS7:
		up->curregs[3] |= Rx7;
		up->curregs[5] |= Tx7;
		up->parity_mask = 0x7f;
		break;
	case CS8:
	default:
		up->curregs[3] |= Rx8;
		up->curregs[5] |= Tx8;
		up->parity_mask = 0xff;
		break;
	};
	up->curregs[4] &= ~0x0c;
	if (cflag & CSTOPB)
		up->curregs[4] |= SB2;
	else
		up->curregs[4] |= SB1;
	if (cflag & PARENB)
		up->curregs[4] |= PAR_ENAB;
	else
		up->curregs[4] &= ~PAR_ENAB;
	if (!(cflag & PARODD))
		up->curregs[4] |= PAR_EVEN;
	else
		up->curregs[4] &= ~PAR_EVEN;

899
	up->port.read_status_mask = Rx_OVR;
900
	if (iflag & INPCK)
901
		up->port.read_status_mask |= CRC_ERR | PAR_ERR;
902
	if (iflag & (BRKINT | PARMRK))
903
		up->port.read_status_mask |= BRK_ABRT;
904

905
	up->port.ignore_status_mask = 0;
906
	if (iflag & IGNPAR)
907
		up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
908
	if (iflag & IGNBRK) {
909
		up->port.ignore_status_mask |= BRK_ABRT;
910
		if (iflag & IGNPAR)
911
			up->port.ignore_status_mask |= Rx_OVR;
912 913 914
	}

	if ((cflag & CREAD) == 0)
915
		up->port.ignore_status_mask = 0xff;
916 917 918 919 920 921 922 923 924 925 926 927 928
}

/* The port lock is not held.  */
static void
sunzilog_change_speed(struct uart_port *port, unsigned int cflag,
		      unsigned int iflag, unsigned int quot)
{
	struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
	unsigned long flags;
	int baud, brg;

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

929
	baud = (ZS_CLOCK / (quot * 16));
930 931 932
	brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);

	sunzilog_convert_to_zs(up, cflag, iflag, brg);
933 934 935 936 937 938

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

939 940 941
	up->cflag = cflag;

	sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

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

static const char *sunzilog_type(struct uart_port *port)
{
	return "SunZilog";
}

/* 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)
{
960
	return 0;
961 962 963 964 965 966 967
}

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

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

static struct uart_ops sunzilog_pops = {
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
	.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,
	.change_speed	=	sunzilog_change_speed,
	.type		=	sunzilog_type,
	.release_port	=	sunzilog_release_port,
	.request_port	=	sunzilog_request_port,
	.config_port	=	sunzilog_config_port,
	.verify_port	=	sunzilog_verify_port,
991 992
};

993 994 995 996 997
static struct uart_sunzilog_port *sunzilog_port_table;
static struct zilog_layout **sunzilog_chip_regs;

static struct uart_sunzilog_port *sunzilog_irq_chain;
static int zilog_irq = -1;
998 999

static struct uart_driver sunzilog_reg = {
1000 1001
	.owner		=	THIS_MODULE,
	.driver_name	=	"ttyS",
1002
#ifdef CONFIG_DEVFS_FS
1003
	.dev_name	=	"ttyS%d",
1004
#else
1005
	.dev_name	=	"ttyS%d",
1006
#endif
1007
	.major		=	TTY_MAJOR,
1008 1009
};

1010
static void * __init alloc_one_table(unsigned long size)
1011 1012 1013
{
	void *ret;

1014
	ret = kmalloc(size, GFP_KERNEL);
1015 1016 1017 1018 1019 1020 1021 1022
	if (ret != NULL)
		memset(ret, 0, size);

	return ret;
}

static void __init sunzilog_alloc_tables(void)
{
1023
	sunzilog_port_table = (struct uart_sunzilog_port *)
1024
		alloc_one_table(NUM_CHANNELS * sizeof(struct uart_sunzilog_port));
1025
	sunzilog_chip_regs = (struct zilog_layout **)
1026
		alloc_one_table(NUM_SUNZILOG * sizeof(struct zilog_layout *));
1027

1028
	if (sunzilog_port_table == NULL || sunzilog_chip_regs == NULL) {
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		prom_printf("sunzilog_init: Cannot alloc SunZilog tables.\n");
		prom_halt();
	}
}

#ifdef CONFIG_SPARC64

/* We used to attempt to use the address property of the Zilog device node
 * but that totally is not necessary on sparc64.
 */
1039
static struct zilog_layout * __init get_zs_sun4u(int chip, int node)
1040
{
1041
	unsigned long mapped_addr = 0xdeadbeefUL;
1042
	unsigned int sun4u_ino;
1043
	int zsnode, seen;
1044

1045
	zsnode = node;
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 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	seen = 0;
	while (zsnode) {
		int slave;

		zsnode = prom_searchsiblings(zsnode, "zs");
		if (zsnode == 0 || zsnode == -1)
			break;
		slave = prom_getintdefault(zsnode, "slave", -1);
		if ((slave == chip) || (seen == chip)) {
			struct sbus_bus *sbus = NULL;
			struct sbus_dev *sdev = NULL;
			int err;

			if (central_bus == NULL) {
				for_each_sbus(sbus) {
					for_each_sbusdev(sdev, sbus) {
						if (sdev->prom_node == zsnode)
							goto found;
					}
				}
			}
		found:
			if (sdev == NULL && central_bus == NULL) {
				prom_printf("SunZilog: sdev&&central == NULL for "
					    "Zilog %d in get_zs_sun4u.\n", chip);
				prom_halt();
			}
			if (central_bus == NULL) {
				mapped_addr =
					sbus_ioremap(&sdev->resource[0], 0,
						     PAGE_SIZE,
						     "Zilog Registers");
			} else {
				struct linux_prom_registers zsregs[1];

				err = prom_getproperty(zsnode, "reg",
						       (char *) &zsregs[0],
						       sizeof(zsregs));
				if (err == -1) {
					prom_printf("SunZilog: Cannot map "
						    "Zilog %d regs on "
						    "central bus.\n", chip);
					prom_halt();
				}
				apply_fhc_ranges(central_bus->child,
						 &zsregs[0], 1);
				apply_central_ranges(central_bus, &zsregs[0], 1);
				mapped_addr =
					(((u64)zsregs[0].which_io)<<32UL) |
					((u64)zsregs[0].phys_addr);
			}

			if (zilog_irq == -1) {
				if (central_bus) {
					unsigned long iclr, imap;

					iclr = central_bus->child->fhc_regs.uregs
						+ FHC_UREGS_ICLR;
					imap = central_bus->child->fhc_regs.uregs
						+ FHC_UREGS_IMAP;
					zilog_irq = build_irq(12, 0, iclr, imap);
				} else {
					err = prom_getproperty(zsnode, "interrupts",
							       (char *) &sun4u_ino,
							       sizeof(sun4u_ino));
					zilog_irq = sbus_build_irq(sbus_root, sun4u_ino);
				}
			}
			break;
		}
		zsnode = prom_getsibling(zsnode);
		seen++;
	}

	if (zsnode == 0 || zsnode == -1) {
		prom_printf("SunZilog: Cannot find Zilog %d in get_zs_sun4u.\n", chip);
		prom_halt();
	}

	return (struct zilog_layout *) mapped_addr;
}
#else /* CONFIG_SPARC64 */
1128 1129 1130 1131 1132 1133

/*
 * XXX The sun4d case is utterly screwed: it tries to re-walk the tree
 * (for the 3rd time) in order to find bootbus and cpu. Streamline it.
 */
static struct zilog_layout * __init get_zs_sun4cmd(int chip, int node)
1134 1135
{
	struct linux_prom_irqs irq_info[2];
1136
	unsigned long mapped_addr = 0;
1137 1138 1139
	int zsnode, cpunode, bbnode;
	struct linux_prom_registers zsreg[4];
	struct resource res;
1140 1141

	if (sparc_cpu_model == sun4d) {
1142
		int walk;
1143 1144 1145

		zsnode = 0;
		bbnode = 0;
1146
		cpunode = 0;
1147 1148 1149 1150 1151 1152
		for (walk = prom_getchild(prom_root_node);
		     (walk = prom_searchsiblings(walk, "cpu-unit")) != 0;
		     walk = prom_getsibling(walk)) {
			bbnode = prom_getchild(walk);
			if (bbnode &&
			    (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
1153
				if ((zsnode = prom_getchild(bbnode)) == node) {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
					cpunode = walk;
					break;
				}
			}
		}
		if (!walk) {
			prom_printf("SunZilog: Cannot find the %d'th bootbus on sun4d.\n",
				    (chip / 2));
			prom_halt();
		}

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
		if (prom_getproperty(zsnode, "reg",
				     (char *) zsreg, sizeof(zsreg)) == -1) {
			prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
			prom_halt();
		}
		/* XXX Looks like an off by one? */
		prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
		res.start = zsreg[0].phys_addr;
		res.end = res.start + (8 - 1);
		res.flags = zsreg[0].which_io | IORESOURCE_IO;
		mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
1176

1177 1178
	} else {
		zsnode = node;
1179

1180
#if 0 /* XXX When was this used? */
1181 1182 1183 1184
		if (prom_getintdefault(zsnode, "slave", -1) != chipid) {
			zsnode = prom_getsibling(zsnode);
			continue;
		}
1185
#endif
1186

1187 1188 1189 1190 1191
		/*
		 * "address" is only present on ports that OBP opened
		 * (from Mitch Bradley's "Hitchhiker's Guide to OBP").
		 * We do not use it.
		 */
1192

1193 1194 1195
		if (prom_getproperty(zsnode, "reg",
				     (char *) zsreg, sizeof(zsreg)) == -1) {
			prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
1196
			prom_halt();
1197
		}
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
		if (sparc_cpu_model == sun4m)	/* Crude. Pass parent. XXX */
			prom_apply_obio_ranges(zsreg, 1);
		res.start = zsreg[0].phys_addr;
		res.end = res.start + (8 - 1);
		res.flags = zsreg[0].which_io | IORESOURCE_IO;
		mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
	}

	if (prom_getproperty(zsnode, "intr",
			     (char *) irq_info, sizeof(irq_info))
		    % sizeof(struct linux_prom_irqs)) {
		prom_printf("SunZilog: Cannot get IRQ property for Zilog %d.\n",
			    chip);
		prom_halt();
1212
	}
1213 1214 1215 1216 1217 1218
	if (zilog_irq == -1) {
		zilog_irq = irq_info[0].pri;
	} else if (zilog_irq != irq_info[0].pri) {
		/* XXX. Dumb. Should handle per-chip IRQ, for add-ons. */
		prom_printf("SunZilog: Inconsistent IRQ layout for Zilog %d.\n",
			    chip);
1219 1220 1221 1222 1223 1224 1225 1226
		prom_halt();
	}

	return (struct zilog_layout *) mapped_addr;
}
#endif /* !(CONFIG_SPARC64) */

/* Get the address of the registers for SunZilog instance CHIP.  */
1227
static struct zilog_layout * __init get_zs(int chip, int node)
1228 1229 1230 1231 1232 1233 1234
{
	if (chip < 0 || chip >= NUM_SUNZILOG) {
		prom_printf("SunZilog: Illegal chip number %d in get_zs.\n", chip);
		prom_halt();
	}

#ifdef CONFIG_SPARC64
1235
	return get_zs_sun4u(chip, node);
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
#else

	if (sparc_cpu_model == sun4) {
		struct resource res;

		/* Not probe-able, hard code it. */
		switch (chip) {
		case 0:
			res.start = 0xf1000000;
			break;
		case 1:
			res.start = 0xf0000000;
			break;
		};
		zilog_irq = 12;
		res.end = (res.start + (8 - 1));
		res.flags = IORESOURCE_IO;
1253
		return (struct zilog_layout *) sbus_ioremap(&res, 0, 8, "SunZilog");
1254 1255
	}

1256
	return get_zs_sun4cmd(chip, node);
1257 1258 1259 1260 1261
#endif
}

#define ZS_PUT_CHAR_MAX_DELAY	2000	/* 10 ms */

1262
static void sunzilog_put_char(struct zilog_channel *channel, unsigned char ch)
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
{
	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 {
		unsigned char val = sbus_readb(&channel->control);
		if (val & Tx_BUF_EMP) {
			ZSDELAY();
			break;
		}
		udelay(5);
	} while (--loops);

	sbus_writeb(ch, &channel->data);
	ZSDELAY();
	ZS_WSYNC(channel);
}

#ifdef CONFIG_SERIO

static spinlock_t sunzilog_serio_lock = SPIN_LOCK_UNLOCKED;

1287
static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
1288 1289 1290 1291 1292 1293
{
	struct uart_sunzilog_port *up = serio->driver;
	unsigned long flags;

	spin_lock_irqsave(&sunzilog_serio_lock, flags);

1294
	sunzilog_put_char(ZILOG_CHANNEL_FROM_PORT(&up->port), ch);
1295

1296 1297 1298
	spin_unlock_irqrestore(&sunzilog_serio_lock, flags);

	return 0;
1299 1300 1301 1302 1303 1304 1305
}

static int sunzilog_serio_open(struct serio *serio)
{
	unsigned long flags;
	int ret;

1306
	spin_lock_irqsave(&sunzilog_serio_lock, flags);
1307 1308 1309 1310 1311
	if (serio->private == NULL) {
		serio->private = (void *) -1L;
		ret = 0;
	} else
		ret = -EBUSY;
1312
	spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1313 1314 1315 1316 1317 1318 1319 1320

	return ret;
}

static void sunzilog_serio_close(struct serio *serio)
{
	unsigned long flags;

1321
	spin_lock_irqsave(&sunzilog_serio_lock, flags);
1322
	serio->private = NULL;
1323
	spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
1324 1325 1326 1327 1328
}

#endif /* CONFIG_SERIO */

static void
1329
sunzilog_console_write(struct console *con, const char *s, unsigned int count)
1330 1331
{
	struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
1332
	struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	unsigned long flags;
	int i;

	spin_lock_irqsave(&up->port.lock, flags);
	for (i = 0; i < count; i++, s++) {
		sunzilog_put_char(channel, *s);
		if (*s == 10)
			sunzilog_put_char(channel, 13);
	}
	udelay(2);
	spin_unlock_irqrestore(&up->port.lock, flags);
}

1346
static kdev_t sunzilog_console_device(struct console *con)
1347
{
1348
	return mk_kdev(sunzilog_reg.major, sunzilog_reg.minor + con->index);
1349 1350
}

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

1357 1358
	printk("Console: ttyS%d (Zilog8530)\n",
	       (sunzilog_reg.minor - 64) + con->index);
1359 1360 1361 1362 1363 1364 1365

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

	/* Firmware console speed is limited to 150-->38400 baud so
	 * this hackish cflag thing is OK.
	 */
1366
	switch (con->cflag & CBAUD) {
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	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);

	/*
	 * Temporary fix.
	 */
	spin_lock_init(&up->port.lock);

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

	up->curregs[R15] = BRKIE;

	sunzilog_convert_to_zs(up, con->cflag, 0, brg);

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

1393 1394 1395 1396
	sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
	sunzilog_startup(&up->port);

	return 0;
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
}

static struct console sunzilog_console = {
	.name	=	"ttyS",
	.write	=	sunzilog_console_write,
	.device	=	sunzilog_console_device,
	.setup	=	sunzilog_console_setup,
	.flags	=	CON_PRINTBUFFER,
	.index	=	-1,
};

static int __init sunzilog_console_init(void)
{
1410 1411
	int i;

1412 1413 1414
	if (con_is_present())
		return 0;

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
	for (i = 0; i < NUM_CHANNELS; i++) {
		int this_minor = sunzilog_reg.minor + i;

		if ((this_minor - 64) == (serial_console - 1))
			break;
	}
	if (i == NUM_CHANNELS)
		return 0;

	sunzilog_console.index = i;
1425 1426 1427 1428
	register_console(&sunzilog_console);
	return 0;
}

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
/*
 * We scan the PROM tree recursively. This is the most reliable way
 * to find Zilog nodes on various platforms. However, we face an extreme
 * shortage of kernel stack, so we must be very careful. To that end,
 * we scan only to a certain depth, and we use a common property buffer
 * in the scan structure.
 */
#define ZS_PROPSIZE  128
#define ZS_SCAN_DEPTH	5

struct zs_probe_scan {
	int depth;
	void (*scanner)(struct zs_probe_scan *t, int node);

	int devices;
	char prop[ZS_PROPSIZE];
};

static int __inline__ sunzilog_node_ok(int node, const char *name, int len)
{
	if (strncmp(name, "zs", len) == 0)
		return 1;
	/* Don't fold this procedure just yet. Compare to su_node_ok(). */
	return 0;
}

static void __init sunzilog_scan(struct zs_probe_scan *t, int node)
{
	int len;

	for (; node != 0; node = prom_getsibling(node)) {
		len = prom_getproperty(node, "name", t->prop, ZS_PROPSIZE);
		if (len <= 1)
			continue;		/* Broken PROM node */
		if (sunzilog_node_ok(node, t->prop, len)) {
			(*t->scanner)(t, node);
		} else {
			if (t->depth < ZS_SCAN_DEPTH) {
				t->depth++;
				sunzilog_scan(t, prom_getchild(node));
				--t->depth;
			}
		}
	}
}

1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
static void __init sunzilog_prepare(void)
{
	struct uart_sunzilog_port *up;
	struct zilog_layout *rp;
	int channel, chip;

	sunzilog_irq_chain = up = &sunzilog_port_table[0];
	for (channel = 0; channel < NUM_CHANNELS - 1; channel++)
		up[channel].next = &up[channel + 1];
	up[channel].next = NULL;

	for (chip = 0; chip < NUM_SUNZILOG; chip++) {
1487 1488 1489
		rp = sunzilog_chip_regs[chip];
		up[(chip * 2) + 0].port.membase = (char *) &rp->channelA;
		up[(chip * 2) + 1].port.membase = (char *) &rp->channelB;
1490 1491 1492 1493

		/* Channel A */
		up[(chip * 2) + 0].port.iotype = SERIAL_IO_MEM;
		up[(chip * 2) + 0].port.irq = zilog_irq;
1494
		up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
1495 1496
		up[(chip * 2) + 0].port.fifosize = 1;
		up[(chip * 2) + 0].port.ops = &sunzilog_pops;
1497
		up[(chip * 2) + 0].port.type = PORT_SUNZILOG;
1498 1499 1500 1501 1502 1503 1504
		up[(chip * 2) + 0].port.flags = 0;
		up[(chip * 2) + 0].port.line = (chip * 2) + 0;
		up[(chip * 2) + 0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;

		/* Channel B */
		up[(chip * 2) + 1].port.iotype = SERIAL_IO_MEM;
		up[(chip * 2) + 1].port.irq = zilog_irq;
1505
		up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
1506 1507
		up[(chip * 2) + 1].port.fifosize = 1;
		up[(chip * 2) + 1].port.ops = &sunzilog_pops;
1508
		up[(chip * 2) + 1].port.type = PORT_SUNZILOG;
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
		up[(chip * 2) + 1].port.flags = 0;
		up[(chip * 2) + 1].port.line = (chip * 2) + 1;
		up[(chip * 2) + 1].flags |= 0;
	}
}

static void __init sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
{
	int baud, brg;

	if (channel == KEYBOARD_LINE) {
		up->flags |= SUNZILOG_FLAG_CONS_KEYB;
1521
		up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1522 1523 1524
		baud = 1200;
	} else {
		up->flags |= SUNZILOG_FLAG_CONS_MOUSE;
1525
		up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1526 1527 1528 1529 1530 1531 1532
		baud = 4800;
	}
	printk(KERN_INFO "zs%d at 0x%p (irq = %s) is a Zilog8530\n",
	       channel, up->port.membase, __irq_itoa(zilog_irq));

	up->curregs[R15] = BRKIE;
	brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
1533
	sunzilog_convert_to_zs(up, up->cflag, 0, brg);
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

#ifdef CONFIG_SERIO
	memset(&up->serio, 0, sizeof(up->serio));

	up->serio.driver = up;

	up->serio.type = SERIO_RS232;
	if (channel == KEYBOARD_LINE) {
		up->serio.type |= SERIO_SUNKBD;
		up->serio.name = "zskbd";
	} else {
		up->serio.type |= SERIO_SUN;
		up->serio.name = "zsms";
	}
1548 1549
	up->serio.phys = (channel == KEYBOARD_LINE ?
			  "zs/serio0" : "zs/serio1");
1550 1551 1552 1553 1554 1555 1556 1557 1558

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

	serio_register_port(&up->serio);
#endif

	spin_unlock(&up->port.lock);
1559
	sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
1560 1561 1562 1563 1564 1565
	sunzilog_startup(&up->port);
	spin_lock(&up->port.lock);
}

static void __init sunzilog_init_hw(void)
{
1566
	int i;
1567

1568 1569
	for (i = 0; i < NUM_CHANNELS; i++) {
		struct uart_sunzilog_port *up = &sunzilog_port_table[i];
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
		struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
		unsigned long flags;
		int baud, brg;

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

1582 1583
		if (i == KEYBOARD_LINE || i == MOUSE_LINE) {
			sunzilog_init_kbdms(up, i);
1584 1585 1586 1587 1588
		} else if (ZS_IS_CONS(up)) {
			/* sunzilog_console_setup takes care of this */
		} else {
			/* Normal serial TTY. */
			up->parity_mask = 0xff;
1589 1590 1591
			up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
			up->curregs[R3] = RxENAB | Rx8;
			up->curregs[R5] = TxENAB | Tx8;
1592 1593 1594 1595 1596 1597 1598 1599
			up->curregs[R9] = NV | MIE;
			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;
1600
			sunzilog_maybe_update_regs(up, channel);
1601 1602 1603 1604 1605 1606
		}

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

1607 1608 1609 1610 1611 1612 1613 1614
static struct zilog_layout * __init get_zs(int chip, int node);

static void __init sunzilog_scan_probe(struct zs_probe_scan *t, int node)
{
	sunzilog_chip_regs[t->devices] = get_zs(t->devices, node);
	t->devices++;
}

1615 1616
static int __init sunzilog_ports_init(void)
{
1617
	struct zs_probe_scan scan;
1618 1619
	int ret;

1620 1621 1622 1623 1624 1625
	printk(KERN_INFO "Serial: Sun Zilog driver (%d chips).\n", NUM_SUNZILOG);

	scan.scanner = sunzilog_scan_probe;
	scan.depth = 0;
	scan.devices = 0;
	sunzilog_scan(&scan, prom_getchild(prom_root_node));
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641

	sunzilog_prepare();

	if (request_irq(zilog_irq, sunzilog_interrupt, SA_SHIRQ,
			"SunZilog", sunzilog_irq_chain)) {
		prom_printf("SunZilog: Unable to register zs interrupt handler.\n");
		prom_halt();
	}

	sunzilog_init_hw();

	/* We can only init this once we have probed the Zilogs
	 * in the system.
	 */
	sunzilog_reg.nr = NUM_CHANNELS;
	sunzilog_reg.cons = &sunzilog_console;
1642 1643 1644

	sunzilog_reg.minor = sunserial_current_minor;
	sunserial_current_minor += NUM_CHANNELS;
1645 1646 1647 1648 1649 1650

	ret = uart_register_driver(&sunzilog_reg);
	if (ret == 0) {
		int i;

		for (i = 0; i < NUM_CHANNELS; i++) {
1651 1652
			struct uart_sunzilog_port *up = &sunzilog_port_table[i];

1653 1654 1655
			if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
				continue;

1656
			uart_add_one_port(&sunzilog_reg, &up->port);
1657 1658 1659 1660 1661 1662
		}
	}

	return ret;
}

1663
static void __init sunzilog_scan_count(struct zs_probe_scan *t, int node)
1664
{
1665 1666 1667 1668 1669 1670
	t->devices++;
}

static int __init sunzilog_ports_count(void)
{
	struct zs_probe_scan scan;
1671 1672

	/* Sun4 Zilog setup is hard coded, no probing to do.  */
1673 1674
	if (sparc_cpu_model == sun4)
		return 2;
1675

1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
	scan.scanner = sunzilog_scan_count;
	scan.depth = 0;
	scan.devices = 0;

	sunzilog_scan(&scan, prom_getchild(prom_root_node));

	return scan.devices;
}

static int __init sunzilog_init(void)
{

	NUM_SUNZILOG = sunzilog_ports_count();
	if (NUM_SUNZILOG == 0)
		return -ENODEV;
1691

1692
	sunzilog_alloc_tables();
1693

1694 1695
	sunzilog_ports_init();
	sunzilog_console_init();
1696

1697
	return 0;
1698 1699 1700 1701 1702 1703
}

static void __exit sunzilog_exit(void)
{
	int i;

1704
	for (i = 0; i < NUM_CHANNELS; i++) {
1705 1706
		struct uart_sunzilog_port *up = &sunzilog_port_table[i];

1707 1708 1709
		if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
			continue;

1710
		uart_remove_one_port(&sunzilog_reg, &up->port);
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	}

	uart_unregister_driver(&sunzilog_reg);
}

module_init(sunzilog_init);
module_exit(sunzilog_exit);

EXPORT_NO_SYMBOLS;

MODULE_AUTHOR("David S. Miller");
MODULE_DESCRIPTION("Sun Zilog serial port driver");
MODULE_LICENSE("GPL");