Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.17
      1 /*	$NetBSD: zs.c,v 1.17 2000/04/10 13:34:18 pk 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 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/conf.h>
     52 #include <sys/device.h>
     53 #include <sys/file.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/tty.h>
     58 #include <sys/time.h>
     59 #include <sys/syslog.h>
     60 
     61 #include <machine/autoconf.h>
     62 #include <machine/openfirm.h>
     63 #include <machine/bsd_openprom.h>
     64 #include <machine/conf.h>
     65 #include <machine/cpu.h>
     66 #include <machine/eeprom.h>
     67 #include <machine/psl.h>
     68 #include <machine/z8530var.h>
     69 
     70 #include <dev/cons.h>
     71 #include <dev/ic/z8530reg.h>
     72 #include <ddb/db_output.h>
     73 
     74 #include <sparc64/sparc64/vaddrs.h>
     75 #include <sparc64/sparc64/auxreg.h>
     76 #include <sparc64/dev/cons.h>
     77 
     78 #include "kbd.h"	/* NKBD */
     79 #include "zs.h" 	/* NZS */
     80 
     81 /* Make life easier for the initialized arrays here. */
     82 #if NZS < 3
     83 #undef  NZS
     84 #define NZS 3
     85 #endif
     86 
     87 /*
     88  * Some warts needed by z8530tty.c -
     89  * The default parity REALLY needs to be the same as the PROM uses,
     90  * or you can not see messages done with printf during boot-up...
     91  */
     92 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     93 int zs_major = 12;
     94 
     95 /*
     96  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     97  */
     98 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     99 
    100 #define	ZS_DELAY()
    101 
    102 /* The layout of this is hardware-dependent (padding, order). */
    103 struct zschan {
    104 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    105 	u_char		zc_xxx0;
    106 	volatile u_char	zc_data;	/* data */
    107 	u_char		zc_xxx1;
    108 };
    109 struct zsdevice {
    110 	/* Yes, they are backwards. */
    111 	struct	zschan zs_chan_b;
    112 	struct	zschan zs_chan_a;
    113 };
    114 
    115 /* Saved PROM mappings */
    116 static struct zsdevice *zsaddr[NZS];
    117 
    118 /* Flags from cninit() */
    119 static int zs_hwflags[NZS][2];
    120 
    121 /* Default speed for each channel */
    122 static int zs_defspeed[NZS][2] = {
    123 	{ 9600, 	/* ttya */
    124 	  9600 },	/* ttyb */
    125 	{ 1200, 	/* keyboard */
    126 	  1200 },	/* mouse */
    127 	{ 9600, 	/* ttyc */
    128 	  9600 },	/* ttyd */
    129 };
    130 
    131 static u_char zs_init_reg[16] = {
    132 	0,	/* 0: CMD (reset, etc.) */
    133 	0,	/* 1: No interrupts yet. */
    134 	0,	/* 2: IVECT */
    135 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    136 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    137 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    138 	0,	/* 6: TXSYNC/SYNCLO */
    139 	0,	/* 7: RXSYNC/SYNCHI */
    140 	0,	/* 8: alias for data port */
    141 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    142 	0,	/*10: Misc. TX/RX control bits */
    143 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    144 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    145 	0,			/*13: BAUDHI (default=9600) */
    146 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    147 	ZSWR15_BREAK_IE,
    148 };
    149 
    150 struct zschan *
    151 zs_get_chan_addr(zs_unit, channel)
    152 	int zs_unit, channel;
    153 {
    154 	struct zsdevice	*addr;
    155 	struct zschan	*zc;
    156 
    157 	if (zs_unit >= NZS)
    158 		return (NULL);
    159 	addr = zsaddr[zs_unit];
    160 #if defined(DEBUG) && defined(DDB)
    161 	if (addr == NULL) {
    162 		db_printf("zs_get_chan_addr(): unit %d channel %d not found\n", zs_unit, channel);
    163 		Debugger();
    164 	}
    165 #endif
    166 	if (addr == NULL)
    167 		return (NULL);
    168 	if (channel == 0) {
    169 		zc = &addr->zs_chan_a;
    170 	} else {
    171 		zc = &addr->zs_chan_b;
    172 	}
    173 	return (zc);
    174 }
    175 
    176 
    177 /****************************************************************
    178  * Autoconfig
    179  ****************************************************************/
    180 
    181 /* Definition of the driver for autoconfig. */
    182 static int  zs_match_sbus __P((struct device *, struct cfdata *, void *));
    183 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    184 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    185 static void zs_attach_sbus __P((struct device *, struct device *, void *));
    186 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    187 static void zs_attach_obio __P((struct device *, struct device *, void *));
    188 
    189 static void zs_attach __P((struct zsc_softc *, int));
    190 static int  zs_print __P((void *, const char *name));
    191 
    192 struct cfattach zs_ca = {
    193 	sizeof(struct zsc_softc), zs_match_sbus, zs_attach_sbus
    194 };
    195 
    196 struct cfattach zs_mainbus_ca = {
    197 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    198 };
    199 
    200 struct cfattach zs_obio_ca = {
    201 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    202 };
    203 
    204 extern struct cfdriver zs_cd;
    205 extern struct consdev consdev_kd;
    206 extern struct consdev consdev_zs;
    207 extern struct consdev *cn_hw;
    208 extern int stdinnode;
    209 extern int fbnode;
    210 
    211 /* Interrupt handlers. */
    212 static int zshard __P((void *));
    213 static int zssoft __P((void *));
    214 static struct intrhand levelsoft = { zssoft };
    215 
    216 static int zs_get_speed __P((struct zs_chanstate *));
    217 
    218 
    219 /*
    220  * Is the zs chip present?
    221  */
    222 static int
    223 zs_match_mainbus(parent, cf, aux)
    224 	struct device *parent;
    225 	struct cfdata *cf;
    226 	void *aux;
    227 {
    228 	struct mainbus_attach_args *ma = aux;
    229 
    230 	if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
    231 		return (0);
    232 
    233 	return (getpropint(ma->ma_node, "slave", -2) == cf->cf_unit);
    234 }
    235 
    236 static int
    237 zs_match_sbus(parent, cf, aux)
    238 	struct device *parent;
    239 	struct cfdata *cf;
    240 	void *aux;
    241 {
    242 	struct sbus_attach_args *sa = aux;
    243 
    244 	if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    245 		return (0);
    246 
    247 	return 1;
    248 }
    249 
    250 static int
    251 zs_match_obio(parent, cf, aux)
    252 	struct device *parent;
    253 	struct cfdata *cf;
    254 	void *aux;
    255 {
    256 #ifdef SUN4U
    257 	return 0;
    258 #else
    259 	union obio_attach_args *uoba = aux;
    260 	struct obio4_attach_args *oba;
    261 
    262 	if (uoba->uoba_isobio4 == 0) {
    263 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    264 
    265 		if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    266 			return (0);
    267 
    268 		return (getpropint(sa->sa_node, "slave", -2) == cf->cf_unit);
    269 	}
    270 
    271 	oba = &uoba->uoba_oba4;
    272 	return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
    273 			        1, 0, 0, NULL, NULL));
    274 #endif
    275 }
    276 
    277 static void
    278 zs_attach_mainbus(parent, self, aux)
    279 	struct device *parent;
    280 	struct device *self;
    281 	void *aux;
    282 {
    283 #ifdef SUN4U
    284 	return;
    285 #else
    286 	struct zsc_softc *zsc = (void *) self;
    287 	struct mainbus_attach_args *ma = aux;
    288 	int zs_unit = zsc->zsc_dev.dv_unit;
    289 
    290 	zsc->zsc_bustag = ma->ma_bustag;
    291 	zsc->zsc_dmatag = ma->ma_dmatag;
    292 
    293 	/* Use the mapping setup by the Sun PROM. */
    294 	if (zsaddr[zs_unit] == NULL)
    295 		zsaddr[zs_unit] = findzs(zs_unit);
    296 	if ((void*)zsaddr[zs_unit] != (void*)(u_long)ma->ma_address[0])
    297 		panic("zsattach_mainbus");
    298 	zs_attach(zsc, ma->ma_pri);
    299 #endif
    300 }
    301 
    302 
    303 static void
    304 zs_attach_sbus(parent, self, aux)
    305 	struct device *parent;
    306 	struct device *self;
    307 	void *aux;
    308 {
    309 	struct zsc_softc *zsc = (void *) self;
    310 	struct sbus_attach_args *sa = aux;
    311 	int zs_unit = zsc->zsc_dev.dv_unit;
    312 	struct consdev *cn = NULL;
    313 
    314 	zsc->zsc_bustag = sa->sa_bustag;
    315 	zsc->zsc_dmatag = sa->sa_dmatag;
    316 
    317 	/* Use the mapping setup by the Sun PROM. */
    318 	if (zsaddr[zs_unit] == NULL) {
    319 		if (sa->sa_npromvaddrs) {
    320 			/*
    321 			 * We're converting from a 32-bit pointer to a 64-bit
    322 			 * pointer.  Since the 32-bit entity is negative, but
    323 			 * the kernel is still mapped into the lower 4GB
    324 			 * range, this needs to be zero-extended.
    325 			 *
    326 			 * XXXXX If we map the kernel and devices into the
    327 			 * high 4GB range, this needs to be changed to
    328 			 * sign-extend the address.
    329 			 */
    330 			zsaddr[zs_unit] =
    331 				(struct zsdevice *)
    332 				(unsigned long)sa->sa_promvaddrs[0];
    333 		} else {
    334 			bus_space_handle_t kvaddr;
    335 
    336 			if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    337 					 sa->sa_offset,
    338 					 sa->sa_size,
    339 					 BUS_SPACE_MAP_LINEAR,
    340 					 0, &kvaddr) != 0) {
    341 				printf("%s @ sbus: cannot map registers\n",
    342 				       self->dv_xname);
    343 				return;
    344 			}
    345 			zsaddr[zs_unit] = (struct zsdevice *)
    346 				(long)kvaddr;
    347 		}
    348 	}
    349 	/*
    350 	 * Check to see if we're the console.  We presume the input comes from
    351 	 * the same location as the output, although that may not be true.
    352 	 * To support input from the serial line but output to a display we
    353 	 * would need to generate some really weird consdev vectors.
    354 	 */
    355 	if (sa->sa_node == stdinnode) {
    356 		char buf[256];
    357 		int chan = 0;
    358 		int len;
    359 
    360 		if ((len = OF_instance_to_path(sa->sa_node, buf, sizeof(buf))) > 0) {
    361 			/* With zs nodes, the last :a or :b selects the channel */
    362 			if (buf[len] == 0) len--;
    363 			if (buf[len] == 'b') chan = 1;
    364 			/* But keyboards don't have a :a or :b */
    365 		}
    366 		zs_hwflags[zs_unit][chan] = ZS_HWFLAG_CONSOLE;
    367 		zs_conschan = zs_get_chan_addr(zs_unit, chan);
    368 		if (OF_getproplen(sa->sa_node, "keyboard") >= 0) {
    369 			cn_hw = &consdev_zs;
    370 			cn = &consdev_kd;
    371 		} else {
    372 			cn = &consdev_zs;
    373 		}
    374 	}
    375 	zs_attach(zsc, sa->sa_pri);
    376 	if (cn) {
    377 		cn_tab = cn;
    378 		(*cn->cn_init)(cn);
    379 #ifdef	KGDB
    380 		zs_kgdb_init();
    381 #endif
    382 	}
    383 }
    384 
    385 static void
    386 zs_attach_obio(parent, self, aux)
    387 	struct device *parent;
    388 	struct device *self;
    389 	void *aux;
    390 {
    391 #ifndef SUN4U
    392 	struct zsc_softc *zsc = (void *) self;
    393 	union obio_attach_args *uoba = aux;
    394 	int zs_unit = zsc->zsc_dev.dv_unit;
    395 
    396 	/* Use the mapping setup by the Sun PROM. */
    397 	if (zsaddr[zs_unit] == NULL)
    398 		zsaddr[zs_unit] = findzs(zs_unit);
    399 
    400 	if (uoba->uoba_isobio4 == 0) {
    401 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    402 		zsc->zsc_bustag = sa->sa_bustag;
    403 		zsc->zsc_dmatag = sa->sa_dmatag;
    404 		zs_attach(zsc, sa->sa_pri);
    405 	} else {
    406 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    407 		zsc->zsc_bustag = oba->oba_bustag;
    408 		zsc->zsc_dmatag = oba->oba_dmatag;
    409 		zs_attach(zsc, oba->oba_pri);
    410 	}
    411 #endif
    412 }
    413 /*
    414  * Attach a found zs.
    415  *
    416  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    417  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    418  */
    419 static void
    420 zs_attach(zsc, pri)
    421 	struct zsc_softc *zsc;
    422 	int pri;
    423 {
    424 	struct zsc_attach_args zsc_args;
    425 	volatile struct zschan *zc;
    426 	struct zs_chanstate *cs;
    427 	int s, zs_unit, channel;
    428 	static int didintr, prevpri;
    429 
    430 	printf(" softpri %d\n", PIL_TTY);
    431 
    432 	/*
    433 	 * Initialize software state for each channel.
    434 	 */
    435 	zs_unit = zsc->zsc_dev.dv_unit;
    436 	for (channel = 0; channel < 2; channel++) {
    437 		zsc_args.channel = channel;
    438 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    439 		cs = &zsc->zsc_cs_store[channel];
    440 		zsc->zsc_cs[channel] = cs;
    441 
    442 		cs->cs_channel = channel;
    443 		cs->cs_private = NULL;
    444 		cs->cs_ops = &zsops_null;
    445 		cs->cs_brg_clk = PCLK / 16;
    446 
    447 		zc = zs_get_chan_addr(zs_unit, channel);
    448 		if (zs_hwflags[zs_unit][channel] == ZS_HWFLAG_CONSOLE) {
    449 			zs_conschan = (struct zschan *)zc;
    450 		}
    451 		cs->cs_reg_csr  = &zc->zc_csr;
    452 		cs->cs_reg_data = &zc->zc_data;
    453 
    454 		bcopy(zs_init_reg, cs->cs_creg, 16);
    455 		bcopy(zs_init_reg, cs->cs_preg, 16);
    456 
    457 		/* XXX: Get these from the PROM properties! */
    458 		/* XXX: See the mvme167 code.  Better. */
    459 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    460 			cs->cs_defspeed = zs_get_speed(cs);
    461 		else
    462 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    463 		cs->cs_defcflag = zs_def_cflag;
    464 
    465 		/* Make these correspond to cs_defcflag (-crtscts) */
    466 		cs->cs_rr0_dcd = ZSRR0_DCD;
    467 		cs->cs_rr0_cts = 0;
    468 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    469 		cs->cs_wr5_rts = 0;
    470 
    471 		/*
    472 		 * Clear the master interrupt enable.
    473 		 * The INTENA is common to both channels,
    474 		 * so just do it on the A channel.
    475 		 */
    476 		if (channel == 0) {
    477 			zs_write_reg(cs, 9, 0);
    478 		}
    479 
    480 		/*
    481 		 * Look for a child driver for this channel.
    482 		 * The child attach will setup the hardware.
    483 		 */
    484 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    485 			/* No sub-driver.  Just reset it. */
    486 			u_char reset = (channel == 0) ?
    487 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    488 			s = splzs();
    489 			zs_write_reg(cs,  9, reset);
    490 			splx(s);
    491 		}
    492 	}
    493 
    494 	/*
    495 	 * Now safe to install interrupt handlers.  Note the arguments
    496 	 * to the interrupt handlers aren't used.  Note, we only do this
    497 	 * once since both SCCs interrupt at the same level and vector.
    498 	 */
    499 	if (!didintr) {
    500 		didintr = 1;
    501 		prevpri = pri;
    502 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
    503 		intr_establish(PIL_TTY, &levelsoft);
    504 	} else if (pri != prevpri)
    505 		panic("broken zs interrupt scheme");
    506 
    507 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    508 
    509 	/*
    510 	 * Set the master interrupt enable and interrupt vector.
    511 	 * (common to both channels, do it on A)
    512 	 */
    513 	cs = zsc->zsc_cs[0];
    514 	s = splhigh();
    515 	/* interrupt vector */
    516 	zs_write_reg(cs, 2, zs_init_reg[2]);
    517 	/* master interrupt control (enable) */
    518 	zs_write_reg(cs, 9, zs_init_reg[9]);
    519 	splx(s);
    520 
    521 #if 0
    522 	/*
    523 	 * XXX: L1A hack - We would like to be able to break into
    524 	 * the debugger during the rest of autoconfiguration, so
    525 	 * lower interrupts just enough to let zs interrupts in.
    526 	 * This is done after both zs devices are attached.
    527 	 */
    528 	if (zs_unit == 1) {
    529 		printf("zs1: enabling zs interrupts\n");
    530 		(void)splfd(); /* XXX: splzs - 1 */
    531 	}
    532 #endif
    533 }
    534 
    535 static int
    536 zs_print(aux, name)
    537 	void *aux;
    538 	const char *name;
    539 {
    540 	struct zsc_attach_args *args = aux;
    541 
    542 	if (name != NULL)
    543 		printf("%s: ", name);
    544 
    545 	if (args->channel != -1)
    546 		printf(" channel %d", args->channel);
    547 
    548 	return (UNCONF);
    549 }
    550 
    551 static volatile int zssoftpending;
    552 
    553 /*
    554  * Our ZS chips all share a common, autovectored interrupt,
    555  * so we have to look at all of them on each interrupt.
    556  */
    557 static int
    558 zshard(arg)
    559 	void *arg;
    560 {
    561 	register struct zsc_softc *zsc;
    562 	register int unit, rr3, rval, softreq;
    563 
    564 	rval = softreq = 0;
    565 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    566 		zsc = zs_cd.cd_devs[unit];
    567 		if (zsc == NULL)
    568 			continue;
    569 		rr3 = zsc_intr_hard(zsc);
    570 		/* Count up the interrupts. */
    571 		if (rr3) {
    572 			rval |= rr3;
    573 			zsc->zsc_intrcnt.ev_count++;
    574 		}
    575 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    576 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    577 	}
    578 
    579 	/* We are at splzs here, so no need to lock. */
    580 	if (softreq && (zssoftpending == 0)) {
    581 		zssoftpending = PIL_TTY;
    582 		send_softint(-1, PIL_TTY, &levelsoft);
    583 	}
    584 	return (rval);
    585 }
    586 
    587 /*
    588  * Similar scheme as for zshard (look at all of them)
    589  */
    590 static int
    591 zssoft(arg)
    592 	void *arg;
    593 {
    594 	register struct zsc_softc *zsc;
    595 	register int s, unit;
    596 
    597 	/* This is not the only ISR on this IPL. */
    598 	if (zssoftpending == 0)
    599 		return (0);
    600 	zssoftpending = 0;
    601 
    602 	/* Make sure we call the tty layer at spltty. */
    603 	s = spltty();
    604 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    605 		zsc = zs_cd.cd_devs[unit];
    606 		if (zsc == NULL)
    607 			continue;
    608 		(void)zsc_intr_soft(zsc);
    609 #ifdef TTY_DEBUG
    610 		{
    611 			struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
    612 			struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
    613 			if (zst0->zst_overflows || zst1->zst_overflows ) {
    614 				struct trapframe *frame = (struct trapframe *)arg;
    615 
    616 				printf("zs silo overflow from %p\n",
    617 				       (long)frame->tf_pc);
    618 			}
    619 		}
    620 #endif
    621 	}
    622 	splx(s);
    623 	return (1);
    624 }
    625 
    626 
    627 /*
    628  * Compute the current baud rate given a ZS channel.
    629  */
    630 static int
    631 zs_get_speed(cs)
    632 	struct zs_chanstate *cs;
    633 {
    634 	int tconst;
    635 
    636 	tconst = zs_read_reg(cs, 12);
    637 	tconst |= zs_read_reg(cs, 13) << 8;
    638 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    639 }
    640 
    641 /*
    642  * MD functions for setting the baud rate and control modes.
    643  */
    644 int
    645 zs_set_speed(cs, bps)
    646 	struct zs_chanstate *cs;
    647 	int bps;	/* bits per second */
    648 {
    649 	int tconst, real_bps;
    650 
    651 	if (bps == 0)
    652 		return (0);
    653 
    654 #ifdef	DIAGNOSTIC
    655 	if (cs->cs_brg_clk == 0)
    656 		panic("zs_set_speed");
    657 #endif
    658 
    659 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    660 	if (tconst < 0)
    661 		return (EINVAL);
    662 
    663 	/* Convert back to make sure we can do it. */
    664 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    665 
    666 	/* XXX - Allow some tolerance here? */
    667 	if (real_bps != bps)
    668 		return (EINVAL);
    669 
    670 	cs->cs_preg[12] = tconst;
    671 	cs->cs_preg[13] = tconst >> 8;
    672 
    673 	/* Caller will stuff the pending registers. */
    674 	return (0);
    675 }
    676 
    677 int
    678 zs_set_modes(cs, cflag)
    679 	struct zs_chanstate *cs;
    680 	int cflag;	/* bits per second */
    681 {
    682 	int s;
    683 
    684 	/*
    685 	 * Output hardware flow control on the chip is horrendous:
    686 	 * if carrier detect drops, the receiver is disabled, and if
    687 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    688 	 * Therefore, NEVER set the HFC bit, and instead use the
    689 	 * status interrupt to detect CTS changes.
    690 	 */
    691 	s = splzs();
    692 	cs->cs_rr0_pps = 0;
    693 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    694 		cs->cs_rr0_dcd = 0;
    695 		if ((cflag & MDMBUF) == 0)
    696 			cs->cs_rr0_pps = ZSRR0_DCD;
    697 	} else
    698 		cs->cs_rr0_dcd = ZSRR0_DCD;
    699 	if ((cflag & CRTSCTS) != 0) {
    700 		cs->cs_wr5_dtr = ZSWR5_DTR;
    701 		cs->cs_wr5_rts = ZSWR5_RTS;
    702 		cs->cs_rr0_cts = ZSRR0_CTS;
    703 	} else if ((cflag & CDTRCTS) != 0) {
    704 		cs->cs_wr5_dtr = 0;
    705 		cs->cs_wr5_rts = ZSWR5_DTR;
    706 		cs->cs_rr0_cts = ZSRR0_CTS;
    707 	} else if ((cflag & MDMBUF) != 0) {
    708 		cs->cs_wr5_dtr = 0;
    709 		cs->cs_wr5_rts = ZSWR5_DTR;
    710 		cs->cs_rr0_cts = ZSRR0_DCD;
    711 	} else {
    712 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    713 		cs->cs_wr5_rts = 0;
    714 		cs->cs_rr0_cts = 0;
    715 	}
    716 	splx(s);
    717 
    718 	/* Caller will stuff the pending registers. */
    719 	return (0);
    720 }
    721 
    722 
    723 /*
    724  * Read or write the chip with suitable delays.
    725  */
    726 
    727 u_char
    728 zs_read_reg(cs, reg)
    729 	struct zs_chanstate *cs;
    730 	u_char reg;
    731 {
    732 	u_char val;
    733 
    734 	*cs->cs_reg_csr = reg;
    735 	ZS_DELAY();
    736 	val = *cs->cs_reg_csr;
    737 	ZS_DELAY();
    738 	return (val);
    739 }
    740 
    741 void
    742 zs_write_reg(cs, reg, val)
    743 	struct zs_chanstate *cs;
    744 	u_char reg, val;
    745 {
    746 	*cs->cs_reg_csr = reg;
    747 	ZS_DELAY();
    748 	*cs->cs_reg_csr = val;
    749 	ZS_DELAY();
    750 }
    751 
    752 u_char
    753 zs_read_csr(cs)
    754 	struct zs_chanstate *cs;
    755 {
    756 	register u_char val;
    757 
    758 	val = *cs->cs_reg_csr;
    759 	ZS_DELAY();
    760 	return (val);
    761 }
    762 
    763 void  zs_write_csr(cs, val)
    764 	struct zs_chanstate *cs;
    765 	u_char val;
    766 {
    767 	*cs->cs_reg_csr = val;
    768 	ZS_DELAY();
    769 }
    770 
    771 u_char zs_read_data(cs)
    772 	struct zs_chanstate *cs;
    773 {
    774 	register u_char val;
    775 
    776 	val = *cs->cs_reg_data;
    777 	ZS_DELAY();
    778 	return (val);
    779 }
    780 
    781 void  zs_write_data(cs, val)
    782 	struct zs_chanstate *cs;
    783 	u_char val;
    784 {
    785 	*cs->cs_reg_data = val;
    786 	ZS_DELAY();
    787 }
    788 
    789 /****************************************************************
    790  * Console support functions (Sun specific!)
    791  * Note: this code is allowed to know about the layout of
    792  * the chip registers, and uses that to keep things simple.
    793  * XXX - I think I like the mvme167 code better. -gwr
    794  ****************************************************************/
    795 
    796 extern void Debugger __P((void));
    797 void *zs_conschan;
    798 
    799 /*
    800  * Handle user request to enter kernel debugger.
    801  */
    802 void
    803 zs_abort(cs)
    804 	struct zs_chanstate *cs;
    805 {
    806 	register volatile struct zschan *zc = zs_conschan;
    807 	int rr0;
    808 
    809 	/* Wait for end of break to avoid PROM abort. */
    810 	/* XXX - Limit the wait? */
    811 	do {
    812 		rr0 = zc->zc_csr;
    813 		ZS_DELAY();
    814 	} while (rr0 & ZSRR0_BREAK);
    815 
    816 #if defined(KGDB)
    817 	zskgdb(cs);
    818 #elif defined(DDB)
    819 	{
    820 		extern int db_active;
    821 
    822 		if (!db_active)
    823 			Debugger();
    824 		else
    825 			/* Debugger is probably hozed */
    826 			callrom();
    827 	}
    828 #else
    829 	printf("stopping on keyboard abort\n");
    830 	callrom();
    831 #endif
    832 }
    833 
    834 /*
    835  * Polled input char.
    836  */
    837 int
    838 zs_getc(arg)
    839 	void *arg;
    840 {
    841 	register volatile struct zschan *zc = arg;
    842 	register int s, c, rr0;
    843 
    844 	s = splhigh();
    845 	/* Wait for a character to arrive. */
    846 	do {
    847 		rr0 = zc->zc_csr;
    848 		ZS_DELAY();
    849 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    850 
    851 	c = zc->zc_data;
    852 	ZS_DELAY();
    853 	splx(s);
    854 
    855 	/*
    856 	 * This is used by the kd driver to read scan codes,
    857 	 * so don't translate '\r' ==> '\n' here...
    858 	 */
    859 	return (c);
    860 }
    861 
    862 /*
    863  * Polled output char.
    864  */
    865 void
    866 zs_putc(arg, c)
    867 	void *arg;
    868 	int c;
    869 {
    870 	register volatile struct zschan *zc = arg;
    871 	register int s, rr0;
    872 
    873 	s = splhigh();
    874 
    875 	/* Wait for transmitter to become ready. */
    876 	do {
    877 		rr0 = zc->zc_csr;
    878 		ZS_DELAY();
    879 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    880 
    881 	/*
    882 	 * Send the next character.
    883 	 * Now you'd think that this could be followed by a ZS_DELAY()
    884 	 * just like all the other chip accesses, but it turns out that
    885 	 * the `transmit-ready' interrupt isn't de-asserted until
    886 	 * some period of time after the register write completes
    887 	 * (more than a couple instructions).  So to avoid stray
    888 	 * interrupts we put in the 2us delay regardless of cpu model.
    889 	 */
    890 	zc->zc_data = c;
    891 	delay(2);
    892 
    893 	splx(s);
    894 }
    895 
    896 /*****************************************************************/
    897 
    898 static void zscninit __P((struct consdev *));
    899 static int  zscngetc __P((dev_t));
    900 static void zscnputc __P((dev_t, int));
    901 static void zscnpollc __P((dev_t, int));
    902 /*
    903  * Console table shared by ttya, ttyb
    904  */
    905 struct consdev consdev_zs = {
    906 	nullcnprobe,
    907 	zscninit,
    908 	zscngetc,
    909 	zscnputc,
    910 	zscnpollc,
    911 	NULL,
    912 };
    913 
    914 static void
    915 zscninit(cn)
    916 	struct consdev *cn;
    917 {
    918 }
    919 
    920 /*
    921  * Polled console input putchar.
    922  */
    923 static int
    924 zscngetc(dev)
    925 	dev_t dev;
    926 {
    927 	return (zs_getc(zs_conschan));
    928 }
    929 
    930 /*
    931  * Polled console output putchar.
    932  */
    933 static void
    934 zscnputc(dev, c)
    935 	dev_t dev;
    936 	int c;
    937 {
    938 	zs_putc(zs_conschan, c);
    939 }
    940 
    941 int swallow_zsintrs;
    942 
    943 static void
    944 zscnpollc(dev, on)
    945 	dev_t dev;
    946 	int on;
    947 {
    948 	/*
    949 	 * Need to tell zs driver to acknowledge all interrupts or we get
    950 	 * annoying spurious interrupt messages.  This is because mucking
    951 	 * with spl() levels during polling does not prevent interrupts from
    952 	 * being generated.
    953 	 */
    954 
    955 	if (on) swallow_zsintrs++;
    956 	else swallow_zsintrs--;
    957 }
    958