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