Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.30
      1 /*	$NetBSD: zs.c,v 1.30 2001/09/26 20:53:12 eeh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41  *
     42  * Runs two serial lines per chip using slave drivers.
     43  * Plain tty/async lines use the zs_async slave.
     44  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     45  */
     46 
     47 #include "opt_ddb.h"
     48 #include "opt_kgdb.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/conf.h>
     53 #include <sys/device.h>
     54 #include <sys/file.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/proc.h>
     58 #include <sys/tty.h>
     59 #include <sys/time.h>
     60 #include <sys/syslog.h>
     61 
     62 #include <machine/autoconf.h>
     63 #include <machine/openfirm.h>
     64 #include <machine/bsd_openprom.h>
     65 #include <machine/conf.h>
     66 #include <machine/cpu.h>
     67 #include <machine/eeprom.h>
     68 #include <machine/psl.h>
     69 #include <machine/z8530var.h>
     70 
     71 #include <dev/cons.h>
     72 #include <dev/ic/z8530reg.h>
     73 #include <dev/sun/kbd_ms_ttyvar.h>
     74 #include <ddb/db_output.h>
     75 
     76 #include <sparc64/dev/cons.h>
     77 
     78 #include "kbd.h"	/* NKBD */
     79 #include "ms.h"		/* NMS */
     80 #include "zs.h" 	/* NZS */
     81 
     82 /* Make life easier for the initialized arrays here. */
     83 #if NZS < 3
     84 #undef  NZS
     85 #define NZS 3
     86 #endif
     87 
     88 /*
     89  * Some warts needed by z8530tty.c -
     90  * The default parity REALLY needs to be the same as the PROM uses,
     91  * or you can not see messages done with printf during boot-up...
     92  */
     93 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     94 int zs_major = 12;
     95 
     96 /*
     97  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     98  */
     99 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
    100 
    101 #define	ZS_DELAY()
    102 
    103 /* The layout of this is hardware-dependent (padding, order). */
    104 struct zschan {
    105 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    106 	u_char		zc_xxx0;
    107 	volatile u_char	zc_data;	/* data */
    108 	u_char		zc_xxx1;
    109 };
    110 struct zsdevice {
    111 	/* Yes, they are backwards. */
    112 	struct	zschan zs_chan_b;
    113 	struct	zschan zs_chan_a;
    114 };
    115 
    116 /* ZS channel used as the console device (if any) */
    117 void *zs_conschan_get, *zs_conschan_put;
    118 
    119 /* Saved PROM mappings */
    120 static struct zsdevice *zsaddr[NZS];
    121 
    122 static u_char zs_init_reg[16] = {
    123 	0,	/* 0: CMD (reset, etc.) */
    124 	0,	/* 1: No interrupts yet. */
    125 	0,	/* 2: IVECT */
    126 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    127 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    128 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    129 	0,	/* 6: TXSYNC/SYNCLO */
    130 	0,	/* 7: RXSYNC/SYNCHI */
    131 	0,	/* 8: alias for data port */
    132 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    133 	0,	/*10: Misc. TX/RX control bits */
    134 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    135 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    136 	0,			/*13: BAUDHI (default=9600) */
    137 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    138 	ZSWR15_BREAK_IE,
    139 };
    140 
    141 /* Console ops */
    142 static int  zscngetc __P((dev_t));
    143 static void zscnputc __P((dev_t, int));
    144 static void zscnpollc __P((dev_t, int));
    145 
    146 struct consdev zs_consdev = {
    147 	NULL,
    148 	NULL,
    149 	zscngetc,
    150 	zscnputc,
    151 	zscnpollc,
    152 	NULL,
    153 };
    154 
    155 
    156 /****************************************************************
    157  * Autoconfig
    158  ****************************************************************/
    159 
    160 /* Definition of the driver for autoconfig. */
    161 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    162 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    163 
    164 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    165 static int  zs_print __P((void *, const char *name));
    166 
    167 /* Do we really need this ? */
    168 struct cfattach zs_ca = {
    169 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    170 };
    171 
    172 struct cfattach zs_mainbus_ca = {
    173 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    174 };
    175 
    176 extern struct cfdriver zs_cd;
    177 extern int stdinnode;
    178 extern int fbnode;
    179 
    180 /* Interrupt handlers. */
    181 int zscheckintr __P((void *));
    182 static int zshard __P((void *));
    183 static void zssoft __P((void *));
    184 
    185 static int zs_get_speed __P((struct zs_chanstate *));
    186 
    187 /* Console device support */
    188 static int zs_console_flags __P((int, int, int));
    189 
    190 /* Power management hooks */
    191 int  zs_enable __P((struct zs_chanstate *));
    192 void zs_disable __P((struct zs_chanstate *));
    193 
    194 /*
    195  * Is the zs chip present?
    196  */
    197 static int
    198 zs_match_mainbus(parent, cf, aux)
    199 	struct device *parent;
    200 	struct cfdata *cf;
    201 	void *aux;
    202 {
    203 	struct sbus_attach_args *sa = aux;
    204 
    205 	if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    206 		return (0);
    207 
    208 	return (1);
    209 }
    210 
    211 static void
    212 zs_attach_mainbus(parent, self, aux)
    213 	struct device *parent;
    214 	struct device *self;
    215 	void *aux;
    216 {
    217 	struct zsc_softc *zsc = (void *) self;
    218 	struct sbus_attach_args *sa = aux;
    219 	int zs_unit = zsc->zsc_dev.dv_unit;
    220 
    221 	if (sa->sa_nintr == 0) {
    222 		printf(" no interrupt lines\n");
    223 		return;
    224 	}
    225 
    226 	/* Use the mapping setup by the Sun PROM. */
    227 	if (zsaddr[zs_unit] == NULL) {
    228 		/* Only map registers once. */
    229 		if (sa->sa_npromvaddrs) {
    230 			/*
    231 			 * We're converting from a 32-bit pointer to a 64-bit
    232 			 * pointer.  Since the 32-bit entity is negative, but
    233 			 * the kernel is still mapped into the lower 4GB
    234 			 * range, this needs to be zero-extended.
    235 			 *
    236 			 * XXXXX If we map the kernel and devices into the
    237 			 * high 4GB range, this needs to be changed to
    238 			 * sign-extend the address.
    239 			 */
    240 			zsaddr[zs_unit] =
    241 				(struct zsdevice *)
    242 				(uintptr_t)sa->sa_promvaddrs[0];
    243 		} else {
    244 			bus_space_handle_t kvaddr;
    245 
    246 			if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    247 					 sa->sa_offset,
    248 					 sa->sa_size,
    249 					 BUS_SPACE_MAP_LINEAR,
    250 					 0, &kvaddr) != 0) {
    251 				printf("%s @ sbus: cannot map registers\n",
    252 				       self->dv_xname);
    253 				return;
    254 			}
    255 			zsaddr[zs_unit] = (struct zsdevice *)
    256 				(uintptr_t)kvaddr;
    257 		}
    258 	}
    259 	zsc->zsc_bustag = sa->sa_bustag;
    260 	zsc->zsc_dmatag = sa->sa_dmatag;
    261 	zsc->zsc_promunit = PROM_getpropint(sa->sa_node, "slave", -2);
    262 	zsc->zsc_node = sa->sa_node;
    263 	zs_attach(zsc, zsaddr[zs_unit], sa->sa_pri);
    264 }
    265 
    266 /*
    267  * Attach a found zs.
    268  *
    269  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    270  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    271  */
    272 static void
    273 zs_attach(zsc, zsd, pri)
    274 	struct zsc_softc *zsc;
    275 	struct zsdevice *zsd;
    276 	int pri;
    277 {
    278 	struct zsc_attach_args zsc_args;
    279 	struct zs_chanstate *cs;
    280 	int s, channel, softpri = PIL_TTY;
    281 
    282 	if (zsd == NULL) {
    283 		printf("configuration incomplete\n");
    284 		return;
    285 	}
    286 
    287 	printf(" softpri %d\n", softpri);
    288 
    289 	/*
    290 	 * Initialize software state for each channel.
    291 	 */
    292 	for (channel = 0; channel < 2; channel++) {
    293 		struct zschan *zc;
    294 		struct device *child;
    295 		extern struct cfdriver zstty_cd; /* in ioconf.c */
    296 
    297 		zsc_args.channel = channel;
    298 		cs = &zsc->zsc_cs_store[channel];
    299 		zsc->zsc_cs[channel] = cs;
    300 
    301 		cs->cs_channel = channel;
    302 		cs->cs_private = NULL;
    303 		cs->cs_ops = &zsops_null;
    304 		cs->cs_brg_clk = PCLK / 16;
    305 
    306 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    307 
    308 		zsc_args.consdev = NULL;
    309 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
    310 						    zsc->zsc_node,
    311 						    channel);
    312 
    313 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
    314 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
    315 			zsc_args.consdev = &zs_consdev;
    316 		}
    317 
    318 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    319 			zs_conschan_get = zc;
    320 		}
    321 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    322 			zs_conschan_put = zc;
    323 		}
    324 		/* Childs need to set cn_dev, etc */
    325 
    326 		cs->cs_reg_csr  = &zc->zc_csr;
    327 		cs->cs_reg_data = &zc->zc_data;
    328 
    329 		bcopy(zs_init_reg, cs->cs_creg, 16);
    330 		bcopy(zs_init_reg, cs->cs_preg, 16);
    331 
    332 		/* XXX: Consult PROM properties for this?! */
    333 		cs->cs_defspeed = zs_get_speed(cs);
    334 		cs->cs_defcflag = zs_def_cflag;
    335 
    336 		/* Make these correspond to cs_defcflag (-crtscts) */
    337 		cs->cs_rr0_dcd = ZSRR0_DCD;
    338 		cs->cs_rr0_cts = 0;
    339 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    340 		cs->cs_wr5_rts = 0;
    341 
    342 		/*
    343 		 * Clear the master interrupt enable.
    344 		 * The INTENA is common to both channels,
    345 		 * so just do it on the A channel.
    346 		 */
    347 		if (channel == 0) {
    348 			zs_write_reg(cs, 9, 0);
    349 		}
    350 
    351 		/*
    352 		 * Look for a child driver for this channel.
    353 		 * The child attach will setup the hardware.
    354 		 */
    355 		if (!(child =
    356 		      config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print))) {
    357 			/* No sub-driver.  Just reset it. */
    358 			u_char reset = (channel == 0) ?
    359 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    360 			s = splzs();
    361 			zs_write_reg(cs,  9, reset);
    362 			splx(s);
    363 		}
    364 #if (NKBD > 0) || (NMS > 0)
    365 		/*
    366 		 * If this was a zstty it has a keyboard
    367 		 * property on it we need to attach the
    368 		 * sunkbd and sunms line disciplines.
    369 		 */
    370 		if (child
    371 		    && (child->dv_cfdata->cf_driver == &zstty_cd)
    372 		    && (PROM_getproplen(zsc->zsc_node, "keyboard") == 0)) {
    373 			struct kbd_ms_tty_attach_args kma;
    374 			struct zstty_softc {
    375 				/* The following are the only fields we need here */
    376 				struct	device zst_dev;
    377 				struct  tty *zst_tty;
    378 				struct	zs_chanstate *zst_cs;
    379 			} *zst = (struct zstty_softc *)child;
    380 			struct tty *tp;
    381 
    382 			kma.kmta_tp = tp = zst->zst_tty;
    383 			kma.kmta_dev = tp->t_dev;
    384 			kma.kmta_consdev = zsc_args.consdev;
    385 
    386 			/* Attach 'em if we got 'em. */
    387 #if (NKBD > 0)
    388 			if (channel == 0) {
    389 				kma.kmta_name = "keyboard";
    390 				config_found(child, (void *)&kma, NULL);
    391 			}
    392 #endif
    393 #if (NMS > 0)
    394 			if (channel == 1) {
    395 				kma.kmta_name = "mouse";
    396 				config_found(child, (void *)&kma, NULL);
    397 			}
    398 #endif
    399 		}
    400 #endif
    401 	}
    402 
    403 	/*
    404 	 * Now safe to install interrupt handlers.  Note the arguments
    405 	 * to the interrupt handlers aren't used.  Note, we only do this
    406 	 * once since both SCCs interrupt at the same level and vector.
    407 	 */
    408 	bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard, zsc);
    409 	if (!(zsc->zsc_softintr = softintr_establish(softpri, zssoft, zsc)))
    410 		panic("zsattach: could not establish soft interrupt\n");
    411 
    412 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    413 	    zsc->zsc_dev.dv_xname, "intr");
    414 
    415 
    416 	/*
    417 	 * Set the master interrupt enable and interrupt vector.
    418 	 * (common to both channels, do it on A)
    419 	 */
    420 	cs = zsc->zsc_cs[0];
    421 	s = splhigh();
    422 	/* interrupt vector */
    423 	zs_write_reg(cs, 2, zs_init_reg[2]);
    424 	/* master interrupt control (enable) */
    425 	zs_write_reg(cs, 9, zs_init_reg[9]);
    426 	splx(s);
    427 
    428 }
    429 
    430 static int
    431 zs_print(aux, name)
    432 	void *aux;
    433 	const char *name;
    434 {
    435 	struct zsc_attach_args *args = aux;
    436 
    437 	if (name != NULL)
    438 		printf("%s: ", name);
    439 
    440 	if (args->channel != -1)
    441 		printf(" channel %d", args->channel);
    442 
    443 	return (UNCONF);
    444 }
    445 
    446 /* Deprecate this? */
    447 static volatile int zssoftpending;
    448 
    449 static int
    450 zshard(arg)
    451 	void *arg;
    452 {
    453 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    454 	int rr3, rval;
    455 
    456 	rval = 0;
    457 	while ((rr3 = zsc_intr_hard(zsc))) {
    458 		/* Count up the interrupts. */
    459 		rval |= rr3;
    460 		zsc->zsc_intrcnt.ev_count++;
    461 	}
    462 	if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
    463 	     (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
    464 	    zsc->zsc_softintr) {
    465 		zssoftpending = PIL_TTY;
    466 		softintr_schedule(zsc->zsc_softintr);
    467 	}
    468 	return (rval);
    469 }
    470 
    471 int
    472 zscheckintr(arg)
    473 	void *arg;
    474 {
    475 	struct zsc_softc *zsc;
    476 	int unit, rval;
    477 
    478 	rval = 0;
    479 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    480 
    481 		zsc = zs_cd.cd_devs[unit];
    482 		if (zsc == NULL)
    483 			continue;
    484 		rval = (zshard((void *)zsc) || rval);
    485 	}
    486 	return (rval);
    487 }
    488 
    489 
    490 /*
    491  * We need this only for TTY_DEBUG purposes.
    492  */
    493 static void
    494 zssoft(arg)
    495 	void *arg;
    496 {
    497 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    498 	int s;
    499 
    500 	/* Make sure we call the tty layer at spltty. */
    501 	s = spltty();
    502 	zssoftpending = 0;
    503 	(void)zsc_intr_soft(zsc);
    504 #ifdef TTY_DEBUG
    505 	{
    506 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
    507 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
    508 		if (zst0->zst_overflows || zst1->zst_overflows ) {
    509 			struct trapframe *frame = (struct trapframe *)arg;
    510 
    511 			printf("zs silo overflow from %p\n",
    512 			       (long)frame->tf_pc);
    513 		}
    514 	}
    515 #endif
    516 	splx(s);
    517 }
    518 
    519 
    520 /*
    521  * Compute the current baud rate given a ZS channel.
    522  */
    523 static int
    524 zs_get_speed(cs)
    525 	struct zs_chanstate *cs;
    526 {
    527 	int tconst;
    528 
    529 	tconst = zs_read_reg(cs, 12);
    530 	tconst |= zs_read_reg(cs, 13) << 8;
    531 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    532 }
    533 
    534 /*
    535  * MD functions for setting the baud rate and control modes.
    536  */
    537 int
    538 zs_set_speed(cs, bps)
    539 	struct zs_chanstate *cs;
    540 	int bps;	/* bits per second */
    541 {
    542 	int tconst, real_bps;
    543 
    544 	if (bps == 0)
    545 		return (0);
    546 
    547 #ifdef	DIAGNOSTIC
    548 	if (cs->cs_brg_clk == 0)
    549 		panic("zs_set_speed");
    550 #endif
    551 
    552 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    553 	if (tconst < 0)
    554 		return (EINVAL);
    555 
    556 	/* Convert back to make sure we can do it. */
    557 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    558 
    559 	/* XXX - Allow some tolerance here? */
    560 	if (real_bps != bps)
    561 		return (EINVAL);
    562 
    563 	cs->cs_preg[12] = tconst;
    564 	cs->cs_preg[13] = tconst >> 8;
    565 
    566 	/* Caller will stuff the pending registers. */
    567 	return (0);
    568 }
    569 
    570 int
    571 zs_set_modes(cs, cflag)
    572 	struct zs_chanstate *cs;
    573 	int cflag;	/* bits per second */
    574 {
    575 	int s;
    576 
    577 	/*
    578 	 * Output hardware flow control on the chip is horrendous:
    579 	 * if carrier detect drops, the receiver is disabled, and if
    580 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    581 	 * Therefore, NEVER set the HFC bit, and instead use the
    582 	 * status interrupt to detect CTS changes.
    583 	 */
    584 	s = splzs();
    585 	cs->cs_rr0_pps = 0;
    586 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    587 		cs->cs_rr0_dcd = 0;
    588 		if ((cflag & MDMBUF) == 0)
    589 			cs->cs_rr0_pps = ZSRR0_DCD;
    590 	} else
    591 		cs->cs_rr0_dcd = ZSRR0_DCD;
    592 	if ((cflag & CRTSCTS) != 0) {
    593 		cs->cs_wr5_dtr = ZSWR5_DTR;
    594 		cs->cs_wr5_rts = ZSWR5_RTS;
    595 		cs->cs_rr0_cts = ZSRR0_CTS;
    596 	} else if ((cflag & CDTRCTS) != 0) {
    597 		cs->cs_wr5_dtr = 0;
    598 		cs->cs_wr5_rts = ZSWR5_DTR;
    599 		cs->cs_rr0_cts = ZSRR0_CTS;
    600 	} else if ((cflag & MDMBUF) != 0) {
    601 		cs->cs_wr5_dtr = 0;
    602 		cs->cs_wr5_rts = ZSWR5_DTR;
    603 		cs->cs_rr0_cts = ZSRR0_DCD;
    604 	} else {
    605 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    606 		cs->cs_wr5_rts = 0;
    607 		cs->cs_rr0_cts = 0;
    608 	}
    609 	splx(s);
    610 
    611 	/* Caller will stuff the pending registers. */
    612 	return (0);
    613 }
    614 
    615 
    616 /*
    617  * Read or write the chip with suitable delays.
    618  */
    619 
    620 u_char
    621 zs_read_reg(cs, reg)
    622 	struct zs_chanstate *cs;
    623 	u_char reg;
    624 {
    625 	u_char val;
    626 
    627 	*cs->cs_reg_csr = reg;
    628 	ZS_DELAY();
    629 	val = *cs->cs_reg_csr;
    630 	ZS_DELAY();
    631 	return (val);
    632 }
    633 
    634 void
    635 zs_write_reg(cs, reg, val)
    636 	struct zs_chanstate *cs;
    637 	u_char reg, val;
    638 {
    639 	*cs->cs_reg_csr = reg;
    640 	ZS_DELAY();
    641 	*cs->cs_reg_csr = val;
    642 	ZS_DELAY();
    643 }
    644 
    645 u_char
    646 zs_read_csr(cs)
    647 	struct zs_chanstate *cs;
    648 {
    649 	u_char val;
    650 
    651 	val = *cs->cs_reg_csr;
    652 	ZS_DELAY();
    653 	return (val);
    654 }
    655 
    656 void  zs_write_csr(cs, val)
    657 	struct zs_chanstate *cs;
    658 	u_char val;
    659 {
    660 	*cs->cs_reg_csr = val;
    661 	ZS_DELAY();
    662 }
    663 
    664 u_char zs_read_data(cs)
    665 	struct zs_chanstate *cs;
    666 {
    667 	u_char val;
    668 
    669 	val = *cs->cs_reg_data;
    670 	ZS_DELAY();
    671 	return (val);
    672 }
    673 
    674 void  zs_write_data(cs, val)
    675 	struct zs_chanstate *cs;
    676 	u_char val;
    677 {
    678 	*cs->cs_reg_data = val;
    679 	ZS_DELAY();
    680 }
    681 
    682 /****************************************************************
    683  * Console support functions (Sun specific!)
    684  * Note: this code is allowed to know about the layout of
    685  * the chip registers, and uses that to keep things simple.
    686  * XXX - I think I like the mvme167 code better. -gwr
    687  ****************************************************************/
    688 
    689 extern void Debugger __P((void));
    690 
    691 /*
    692  * Handle user request to enter kernel debugger.
    693  */
    694 void
    695 zs_abort(cs)
    696 	struct zs_chanstate *cs;
    697 {
    698 	volatile struct zschan *zc = zs_conschan_get;
    699 	int rr0;
    700 
    701 	/* Wait for end of break to avoid PROM abort. */
    702 	/* XXX - Limit the wait? */
    703 	do {
    704 		rr0 = zc->zc_csr;
    705 		ZS_DELAY();
    706 	} while (rr0 & ZSRR0_BREAK);
    707 
    708 #if defined(KGDB)
    709 	zskgdb(cs);
    710 #elif defined(DDB)
    711 	{
    712 		extern int db_active;
    713 
    714 		if (!db_active)
    715 			Debugger();
    716 		else
    717 			/* Debugger is probably hozed */
    718 			callrom();
    719 	}
    720 #else
    721 	printf("stopping on keyboard abort\n");
    722 	callrom();
    723 #endif
    724 }
    725 
    726 
    727 /*
    728  * Polled input char.
    729  */
    730 int
    731 zs_getc(arg)
    732 	void *arg;
    733 {
    734 	volatile struct zschan *zc = arg;
    735 	int s, c, rr0;
    736 
    737 	s = splhigh();
    738 	/* Wait for a character to arrive. */
    739 	do {
    740 		rr0 = zc->zc_csr;
    741 		ZS_DELAY();
    742 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    743 
    744 	c = zc->zc_data;
    745 	ZS_DELAY();
    746 	splx(s);
    747 
    748 	/*
    749 	 * This is used by the kd driver to read scan codes,
    750 	 * so don't translate '\r' ==> '\n' here...
    751 	 */
    752 	return (c);
    753 }
    754 
    755 /*
    756  * Polled output char.
    757  */
    758 void
    759 zs_putc(arg, c)
    760 	void *arg;
    761 	int c;
    762 {
    763 	volatile struct zschan *zc = arg;
    764 	int s, rr0;
    765 
    766 	s = splhigh();
    767 
    768 	/* Wait for transmitter to become ready. */
    769 	do {
    770 		rr0 = zc->zc_csr;
    771 		ZS_DELAY();
    772 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    773 
    774 	/*
    775 	 * Send the next character.
    776 	 * Now you'd think that this could be followed by a ZS_DELAY()
    777 	 * just like all the other chip accesses, but it turns out that
    778 	 * the `transmit-ready' interrupt isn't de-asserted until
    779 	 * some period of time after the register write completes
    780 	 * (more than a couple instructions).  So to avoid stray
    781 	 * interrupts we put in the 2us delay regardless of cpu model.
    782 	 */
    783 	zc->zc_data = c;
    784 	delay(2);
    785 
    786 	splx(s);
    787 }
    788 
    789 /*****************************************************************/
    790 
    791 
    792 
    793 
    794 /*
    795  * Polled console input putchar.
    796  */
    797 static int
    798 zscngetc(dev)
    799 	dev_t dev;
    800 {
    801 	return (zs_getc(zs_conschan_get));
    802 }
    803 
    804 /*
    805  * Polled console output putchar.
    806  */
    807 static void
    808 zscnputc(dev, c)
    809 	dev_t dev;
    810 	int c;
    811 {
    812 	zs_putc(zs_conschan_put, c);
    813 }
    814 
    815 int swallow_zsintrs;
    816 
    817 static void
    818 zscnpollc(dev, on)
    819 	dev_t dev;
    820 	int on;
    821 {
    822 	/*
    823 	 * Need to tell zs driver to acknowledge all interrupts or we get
    824 	 * annoying spurious interrupt messages.  This is because mucking
    825 	 * with spl() levels during polling does not prevent interrupts from
    826 	 * being generated.
    827 	 */
    828 
    829 	if (on) swallow_zsintrs++;
    830 	else swallow_zsintrs--;
    831 }
    832 
    833 int
    834 zs_console_flags(promunit, node, channel)
    835 	int promunit;
    836 	int node;
    837 	int channel;
    838 {
    839 	int cookie, flags = 0;
    840 	u_int chosen;
    841 	char buf[255];
    842 
    843 	/*
    844 	 * We'll just to the OBP grovelling down here since that's
    845 	 * the only type of firmware we support.
    846 	 */
    847 	chosen = OF_finddevice("/chosen");
    848 
    849 	/* Default to channel 0 if there are no explicit prom args */
    850 	cookie = 0;
    851 	if (node == OF_instance_to_package(OF_stdin())) {
    852 		if (OF_getprop(chosen, "input-device", buf, sizeof(buf)) != -1) {
    853 
    854 			if (!strcmp("ttyb", buf))
    855 				cookie = 1;
    856 		}
    857 
    858 		if (channel == cookie)
    859 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    860 	}
    861 
    862 	if (node == OF_instance_to_package(OF_stdout())) {
    863 		if (OF_getprop(chosen, "output-device", buf, sizeof(buf)) != -1) {
    864 
    865 			if (!strcmp("ttyb", buf))
    866 				cookie = 1;
    867 		}
    868 
    869 		if (channel == cookie)
    870 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    871 	}
    872 
    873 	return (flags);
    874 }
    875 
    876