Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.91
      1 /*	$NetBSD: zs.c,v 1.91 2002/10/02 16:02:17 thorpej 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 #include "opt_sparc_arch.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/conf.h>
     54 #include <sys/device.h>
     55 #include <sys/file.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/kernel.h>
     58 #include <sys/proc.h>
     59 #include <sys/tty.h>
     60 #include <sys/time.h>
     61 #include <sys/syslog.h>
     62 
     63 #include <machine/bsd_openprom.h>
     64 #include <machine/autoconf.h>
     65 #include <machine/intr.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 <sparc/sparc/vaddrs.h>
     74 #include <sparc/sparc/auxreg.h>
     75 #include <sparc/sparc/auxiotwo.h>
     76 #include <sparc/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 
     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 /*
    100  * Select software interrupt bit based on TTY ipl.
    101  */
    102 #if PIL_TTY == 1
    103 # define IE_ZSSOFT IE_L1
    104 #elif PIL_TTY == 4
    105 # define IE_ZSSOFT IE_L4
    106 #elif PIL_TTY == 6
    107 # define IE_ZSSOFT IE_L6
    108 #else
    109 # error "no suitable software interrupt bit"
    110 #endif
    111 
    112 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
    113 
    114 /* The layout of this is hardware-dependent (padding, order). */
    115 struct zschan {
    116 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    117 	u_char		zc_xxx0;
    118 	volatile u_char	zc_data;	/* data */
    119 	u_char		zc_xxx1;
    120 };
    121 struct zsdevice {
    122 	/* Yes, they are backwards. */
    123 	struct	zschan zs_chan_b;
    124 	struct	zschan zs_chan_a;
    125 };
    126 
    127 /* ZS channel used as the console device (if any) */
    128 void *zs_conschan_get, *zs_conschan_put;
    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 /* Console ops */
    150 static int  zscngetc __P((dev_t));
    151 static void zscnputc __P((dev_t, int));
    152 static void zscnpollc __P((dev_t, int));
    153 
    154 struct consdev zs_consdev = {
    155 	NULL,
    156 	NULL,
    157 	zscngetc,
    158 	zscnputc,
    159 	zscnpollc,
    160 	NULL,
    161 };
    162 
    163 
    164 /****************************************************************
    165  * Autoconfig
    166  ****************************************************************/
    167 
    168 /* Definition of the driver for autoconfig. */
    169 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    170 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    171 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    172 static void zs_attach_obio __P((struct device *, struct device *, void *));
    173 
    174 #if defined(SUN4D)
    175 #include <sparc/dev/bootbusvar.h>
    176 
    177 static int  zs_match_bootbus __P((struct device *, struct cfdata *, void *));
    178 static void zs_attach_bootbus __P((struct device *, struct device *, void *));
    179 
    180 CFATTACH_DECL(zs_bootbus, sizeof(struct zsc_softc),
    181     zs_match_bootbus, zs_attach_bootbus, NULL, NULL);
    182 #endif /* SUN4D */
    183 
    184 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    185 static int  zs_print __P((void *, const char *name));
    186 
    187 CFATTACH_DECL(zs_mainbus, sizeof(struct zsc_softc),
    188     zs_match_mainbus, zs_attach_mainbus, NULL, NULL);
    189 
    190 CFATTACH_DECL(zs_obio, sizeof(struct zsc_softc),
    191     zs_match_obio, zs_attach_obio, NULL, NULL);
    192 
    193 extern struct cfdriver zs_cd;
    194 
    195 /* Interrupt handlers. */
    196 static int zshard __P((void *));
    197 static int zssoft __P((void *));
    198 
    199 static int zs_get_speed __P((struct zs_chanstate *));
    200 
    201 /* Console device support */
    202 static int zs_console_flags __P((int, int, int));
    203 
    204 /* Power management hooks */
    205 int  zs_enable __P((struct zs_chanstate *));
    206 void zs_disable __P((struct zs_chanstate *));
    207 
    208 
    209 /*
    210  * Is the zs chip present?
    211  */
    212 static int
    213 zs_match_mainbus(parent, cf, aux)
    214 	struct device *parent;
    215 	struct cfdata *cf;
    216 	void *aux;
    217 {
    218 	struct mainbus_attach_args *ma = aux;
    219 
    220 	if (strcmp(cf->cf_name, ma->ma_name) != 0)
    221 		return (0);
    222 
    223 	return (1);
    224 }
    225 
    226 static int
    227 zs_match_obio(parent, cf, aux)
    228 	struct device *parent;
    229 	struct cfdata *cf;
    230 	void *aux;
    231 {
    232 	union obio_attach_args *uoba = aux;
    233 	struct obio4_attach_args *oba;
    234 
    235 	if (uoba->uoba_isobio4 == 0) {
    236 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    237 
    238 		if (strcmp(cf->cf_name, sa->sa_name) != 0)
    239 			return (0);
    240 
    241 		return (1);
    242 	}
    243 
    244 	oba = &uoba->uoba_oba4;
    245 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    246 			        1, 0, 0, NULL, NULL));
    247 }
    248 
    249 #if defined(SUN4D)
    250 static int
    251 zs_match_bootbus(parent, cf, aux)
    252 	struct device *parent;
    253 	struct cfdata *cf;
    254 	void *aux;
    255 {
    256 	struct bootbus_attach_args *baa = aux;
    257 
    258 	return (strcmp(cf->cf_name, baa->ba_name) == 0);
    259 }
    260 #endif /* SUN4D */
    261 
    262 static void
    263 zs_attach_mainbus(parent, self, aux)
    264 	struct device *parent;
    265 	struct device *self;
    266 	void *aux;
    267 {
    268 	struct zsc_softc *zsc = (void *) self;
    269 	struct mainbus_attach_args *ma = aux;
    270 
    271 	zsc->zsc_bustag = ma->ma_bustag;
    272 	zsc->zsc_dmatag = ma->ma_dmatag;
    273 	zsc->zsc_promunit = PROM_getpropint(ma->ma_node, "slave", -2);
    274 	zsc->zsc_node = ma->ma_node;
    275 
    276 	/*
    277 	 * For machines with zs on mainbus (all sun4c models), we expect
    278 	 * the device registers to be mapped by the PROM.
    279 	 */
    280 	zs_attach(zsc, ma->ma_promvaddr, ma->ma_pri);
    281 }
    282 
    283 static void
    284 zs_attach_obio(parent, self, aux)
    285 	struct device *parent;
    286 	struct device *self;
    287 	void *aux;
    288 {
    289 	struct zsc_softc *zsc = (void *) self;
    290 	union obio_attach_args *uoba = aux;
    291 
    292 	if (uoba->uoba_isobio4 == 0) {
    293 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    294 		void *va;
    295 		struct zs_chanstate *cs;
    296 		int channel;
    297 
    298 		if (sa->sa_nintr == 0) {
    299 			printf(" no interrupt lines\n");
    300 			return;
    301 		}
    302 
    303 		/*
    304 		 * Some sun4m models (Javastations) may not map the zs device.
    305 		 */
    306 		if (sa->sa_npromvaddrs > 0)
    307 			va = (void *)sa->sa_promvaddr;
    308 		else {
    309 			bus_space_handle_t bh;
    310 
    311 			if (sbus_bus_map(sa->sa_bustag,
    312 					 sa->sa_slot,
    313 					 sa->sa_offset,
    314 					 sa->sa_size,
    315 					 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    316 				printf(" cannot map zs registers\n");
    317 				return;
    318 			}
    319 			va = (void *)bh;
    320 		}
    321 
    322 		/*
    323 		 * Check if power state can be set, e.g. Tadpole 3GX
    324 		 */
    325 		if (PROM_getpropint(sa->sa_node, "pwr-on-auxio2", 0))
    326 		{
    327 			printf (" powered via auxio2");
    328 			for (channel = 0; channel < 2; channel++) {
    329 				cs = &zsc->zsc_cs_store[channel];
    330 				cs->enable = zs_enable;
    331 				cs->disable = zs_disable;
    332 			}
    333 		}
    334 
    335 		zsc->zsc_bustag = sa->sa_bustag;
    336 		zsc->zsc_dmatag = sa->sa_dmatag;
    337 		zsc->zsc_promunit = PROM_getpropint(sa->sa_node, "slave", -2);
    338 		zsc->zsc_node = sa->sa_node;
    339 		zs_attach(zsc, va, sa->sa_pri);
    340 	} else {
    341 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    342 		bus_space_handle_t bh;
    343 		bus_addr_t paddr = oba->oba_paddr;
    344 
    345 		/*
    346 		 * As for zs on mainbus, we require a PROM mapping.
    347 		 */
    348 		if (bus_space_map(oba->oba_bustag,
    349 				  paddr,
    350 				  sizeof(struct zsdevice),
    351 				  BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
    352 				  &bh) != 0) {
    353 			printf(" cannot map zs registers\n");
    354 			return;
    355 		}
    356 		zsc->zsc_bustag = oba->oba_bustag;
    357 		zsc->zsc_dmatag = oba->oba_dmatag;
    358 		/* Find prom unit by physical address */
    359 		if (cpuinfo.cpu_type == CPUTYP_4_100)
    360 			/*
    361 			 * On the sun4/100, the top-most 4 bits are zero
    362 			 * on obio addresses; force them to 1's for the
    363 			 * sake of the comparison here.
    364 			 */
    365 			paddr |= 0xf0000000;
    366 		zsc->zsc_promunit =
    367 			(paddr == 0xf1000000) ? 0 :
    368 			(paddr == 0xf0000000) ? 1 :
    369 			(paddr == 0xe0000000) ? 2 : -2;
    370 
    371 		zs_attach(zsc, (void *)bh, oba->oba_pri);
    372 	}
    373 }
    374 
    375 #if defined(SUN4D)
    376 static void
    377 zs_attach_bootbus(parent, self, aux)
    378 	struct device *parent;
    379 	struct device *self;
    380 	void *aux;
    381 {
    382 	struct zsc_softc *zsc = (void *) self;
    383 	struct bootbus_attach_args *baa = aux;
    384 	void *va;
    385 
    386 	if (baa->ba_nintr == 0) {
    387 		printf(": no interrupt lines\n");
    388 		return;
    389 	}
    390 
    391 	if (baa->ba_npromvaddrs > 0)
    392 		va = (void *) baa->ba_promvaddrs;
    393 	else {
    394 		bus_space_handle_t bh;
    395 
    396 		if (bus_space_map(baa->ba_bustag,
    397 		    BUS_ADDR(baa->ba_slot, baa->ba_offset),
    398 		    baa->ba_size, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    399 			printf(": cannot map zs registers\n");
    400 			return;
    401 		}
    402 		va = (void *) bh;
    403 	}
    404 
    405 	zsc->zsc_bustag = baa->ba_bustag;
    406 	zsc->zsc_promunit = PROM_getpropint(baa->ba_node, "slave", -2);
    407 	zsc->zsc_node = baa->ba_node;
    408 	zs_attach(zsc, va, baa->ba_intr[0].oi_pri);
    409 }
    410 #endif /* SUN4D */
    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, zsd, pri)
    420 	struct zsc_softc *zsc;
    421 	struct zsdevice *zsd;
    422 	int pri;
    423 {
    424 	struct zsc_attach_args zsc_args;
    425 	struct zs_chanstate *cs;
    426 	int s, channel;
    427 	static int didintr, prevpri;
    428 
    429 	if (zsd == NULL) {
    430 		printf("configuration incomplete\n");
    431 		return;
    432 	}
    433 
    434 	printf(" softpri %d\n", PIL_TTY);
    435 
    436 	/*
    437 	 * Initialize software state for each channel.
    438 	 */
    439 	for (channel = 0; channel < 2; channel++) {
    440 		struct zschan *zc;
    441 
    442 		zsc_args.channel = channel;
    443 		cs = &zsc->zsc_cs_store[channel];
    444 		zsc->zsc_cs[channel] = cs;
    445 
    446 		cs->cs_channel = channel;
    447 		cs->cs_private = NULL;
    448 		cs->cs_ops = &zsops_null;
    449 		cs->cs_brg_clk = PCLK / 16;
    450 
    451 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    452 
    453 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
    454 						    zsc->zsc_node,
    455 						    channel);
    456 
    457 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
    458 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
    459 			zsc_args.consdev = &zs_consdev;
    460 		}
    461 
    462 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    463 			zs_conschan_get = zc;
    464 		}
    465 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    466 			zs_conschan_put = zc;
    467 		}
    468 		/* Childs need to set cn_dev, etc */
    469 
    470 		cs->cs_reg_csr  = &zc->zc_csr;
    471 		cs->cs_reg_data = &zc->zc_data;
    472 
    473 		bcopy(zs_init_reg, cs->cs_creg, 16);
    474 		bcopy(zs_init_reg, cs->cs_preg, 16);
    475 
    476 		/* XXX: Consult PROM properties for this?! */
    477 		cs->cs_defspeed = zs_get_speed(cs);
    478 		cs->cs_defcflag = zs_def_cflag;
    479 
    480 		/* Make these correspond to cs_defcflag (-crtscts) */
    481 		cs->cs_rr0_dcd = ZSRR0_DCD;
    482 		cs->cs_rr0_cts = 0;
    483 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    484 		cs->cs_wr5_rts = 0;
    485 
    486 		/*
    487 		 * Clear the master interrupt enable.
    488 		 * The INTENA is common to both channels,
    489 		 * so just do it on the A channel.
    490 		 */
    491 		if (channel == 0) {
    492 			zs_write_reg(cs, 9, 0);
    493 		}
    494 
    495 		/*
    496 		 * Look for a child driver for this channel.
    497 		 * The child attach will setup the hardware.
    498 		 */
    499 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    500 			/* No sub-driver.  Just reset it. */
    501 			u_char reset = (channel == 0) ?
    502 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    503 			s = splzs();
    504 			zs_write_reg(cs,  9, reset);
    505 			splx(s);
    506 		}
    507 	}
    508 
    509 	/*
    510 	 * Now safe to install interrupt handlers.  Note the arguments
    511 	 * to the interrupt handlers aren't used.  Note, we only do this
    512 	 * once since both SCCs interrupt at the same level and vector.
    513 	 */
    514 	if (!didintr) {
    515 		didintr = 1;
    516 		prevpri = pri;
    517 		bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0,
    518 				   zshard, NULL);
    519 		bus_intr_establish(zsc->zsc_bustag, PIL_TTY,
    520 				   IPL_SOFTSERIAL,
    521 				   BUS_INTR_ESTABLISH_SOFTINTR,
    522 				   zssoft, NULL);
    523 	} else if (pri != prevpri)
    524 		panic("broken zs interrupt scheme");
    525 
    526 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    527 	    zsc->zsc_dev.dv_xname, "intr");
    528 
    529 	/*
    530 	 * Set the master interrupt enable and interrupt vector.
    531 	 * (common to both channels, do it on A)
    532 	 */
    533 	cs = zsc->zsc_cs[0];
    534 	s = splhigh();
    535 	/* interrupt vector */
    536 	zs_write_reg(cs, 2, zs_init_reg[2]);
    537 	/* master interrupt control (enable) */
    538 	zs_write_reg(cs, 9, zs_init_reg[9]);
    539 	splx(s);
    540 
    541 #if 0
    542 	/*
    543 	 * XXX: L1A hack - We would like to be able to break into
    544 	 * the debugger during the rest of autoconfiguration, so
    545 	 * lower interrupts just enough to let zs interrupts in.
    546 	 * This is done after both zs devices are attached.
    547 	 */
    548 	if (zsc->zsc_promunit == 1) {
    549 		printf("zs1: enabling zs interrupts\n");
    550 		(void)splfd(); /* XXX: splzs - 1 */
    551 	}
    552 #endif
    553 }
    554 
    555 static int
    556 zs_print(aux, name)
    557 	void *aux;
    558 	const char *name;
    559 {
    560 	struct zsc_attach_args *args = aux;
    561 
    562 	if (name != NULL)
    563 		printf("%s: ", name);
    564 
    565 	if (args->channel != -1)
    566 		printf(" channel %d", args->channel);
    567 
    568 	return (UNCONF);
    569 }
    570 
    571 static volatile int zssoftpending;
    572 
    573 /*
    574  * Our ZS chips all share a common, autovectored interrupt,
    575  * so we have to look at all of them on each interrupt.
    576  */
    577 static int
    578 zshard(arg)
    579 	void *arg;
    580 {
    581 	struct zsc_softc *zsc;
    582 	int unit, rr3, rval, softreq;
    583 
    584 	rval = softreq = 0;
    585 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    586 		struct zs_chanstate *cs;
    587 
    588 		zsc = zs_cd.cd_devs[unit];
    589 		if (zsc == NULL)
    590 			continue;
    591 		rr3 = zsc_intr_hard(zsc);
    592 		/* Count up the interrupts. */
    593 		if (rr3) {
    594 			rval |= rr3;
    595 			zsc->zsc_intrcnt.ev_count++;
    596 		}
    597 		if ((cs = zsc->zsc_cs[0]) != NULL)
    598 			softreq |= cs->cs_softreq;
    599 		if ((cs = zsc->zsc_cs[1]) != NULL)
    600 			softreq |= cs->cs_softreq;
    601 	}
    602 
    603 	/* We are at splzs here, so no need to lock. */
    604 	if (softreq && (zssoftpending == 0)) {
    605 		zssoftpending = IE_ZSSOFT;
    606 #if defined(SUN4M)
    607 		if (CPU_ISSUN4M)
    608 			raise(0, PIL_TTY);
    609 		else
    610 #endif
    611 			ienab_bis(IE_ZSSOFT);
    612 	}
    613 	return (rval);
    614 }
    615 
    616 /*
    617  * Similar scheme as for zshard (look at all of them)
    618  */
    619 static int
    620 zssoft(arg)
    621 	void *arg;
    622 {
    623 	struct zsc_softc *zsc;
    624 	int s, unit;
    625 
    626 	/* This is not the only ISR on this IPL. */
    627 	if (zssoftpending == 0)
    628 		return (0);
    629 
    630 	/*
    631 	 * The soft intr. bit will be set by zshard only if
    632 	 * the variable zssoftpending is zero.  The order of
    633 	 * these next two statements prevents our clearing
    634 	 * the soft intr bit just after zshard has set it.
    635 	 */
    636 	/* ienab_bic(IE_ZSSOFT); */
    637 	zssoftpending = 0;
    638 
    639 	/* Make sure we call the tty layer at spltty. */
    640 	s = spltty();
    641 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    642 		zsc = zs_cd.cd_devs[unit];
    643 		if (zsc == NULL)
    644 			continue;
    645 		(void)zsc_intr_soft(zsc);
    646 	}
    647 	splx(s);
    648 	return (1);
    649 }
    650 
    651 
    652 /*
    653  * Compute the current baud rate given a ZS channel.
    654  */
    655 static int
    656 zs_get_speed(cs)
    657 	struct zs_chanstate *cs;
    658 {
    659 	int tconst;
    660 
    661 	tconst = zs_read_reg(cs, 12);
    662 	tconst |= zs_read_reg(cs, 13) << 8;
    663 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    664 }
    665 
    666 /*
    667  * MD functions for setting the baud rate and control modes.
    668  */
    669 int
    670 zs_set_speed(cs, bps)
    671 	struct zs_chanstate *cs;
    672 	int bps;	/* bits per second */
    673 {
    674 	int tconst, real_bps;
    675 
    676 	if (bps == 0)
    677 		return (0);
    678 
    679 #ifdef	DIAGNOSTIC
    680 	if (cs->cs_brg_clk == 0)
    681 		panic("zs_set_speed");
    682 #endif
    683 
    684 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    685 	if (tconst < 0)
    686 		return (EINVAL);
    687 
    688 	/* Convert back to make sure we can do it. */
    689 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    690 
    691 	/* XXX - Allow some tolerance here? */
    692 	if (real_bps != bps)
    693 		return (EINVAL);
    694 
    695 	cs->cs_preg[12] = tconst;
    696 	cs->cs_preg[13] = tconst >> 8;
    697 
    698 	/* Caller will stuff the pending registers. */
    699 	return (0);
    700 }
    701 
    702 int
    703 zs_set_modes(cs, cflag)
    704 	struct zs_chanstate *cs;
    705 	int cflag;	/* bits per second */
    706 {
    707 	int s;
    708 
    709 	/*
    710 	 * Output hardware flow control on the chip is horrendous:
    711 	 * if carrier detect drops, the receiver is disabled, and if
    712 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    713 	 * Therefore, NEVER set the HFC bit, and instead use the
    714 	 * status interrupt to detect CTS changes.
    715 	 */
    716 	s = splzs();
    717 	cs->cs_rr0_pps = 0;
    718 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    719 		cs->cs_rr0_dcd = 0;
    720 		if ((cflag & MDMBUF) == 0)
    721 			cs->cs_rr0_pps = ZSRR0_DCD;
    722 	} else
    723 		cs->cs_rr0_dcd = ZSRR0_DCD;
    724 	if ((cflag & CRTSCTS) != 0) {
    725 		cs->cs_wr5_dtr = ZSWR5_DTR;
    726 		cs->cs_wr5_rts = ZSWR5_RTS;
    727 		cs->cs_rr0_cts = ZSRR0_CTS;
    728 	} else if ((cflag & CDTRCTS) != 0) {
    729 		cs->cs_wr5_dtr = 0;
    730 		cs->cs_wr5_rts = ZSWR5_DTR;
    731 		cs->cs_rr0_cts = ZSRR0_CTS;
    732 	} else if ((cflag & MDMBUF) != 0) {
    733 		cs->cs_wr5_dtr = 0;
    734 		cs->cs_wr5_rts = ZSWR5_DTR;
    735 		cs->cs_rr0_cts = ZSRR0_DCD;
    736 	} else {
    737 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    738 		cs->cs_wr5_rts = 0;
    739 		cs->cs_rr0_cts = 0;
    740 	}
    741 	splx(s);
    742 
    743 	/* Caller will stuff the pending registers. */
    744 	return (0);
    745 }
    746 
    747 
    748 /*
    749  * Read or write the chip with suitable delays.
    750  */
    751 
    752 u_char
    753 zs_read_reg(cs, reg)
    754 	struct zs_chanstate *cs;
    755 	u_char reg;
    756 {
    757 	u_char val;
    758 
    759 	*cs->cs_reg_csr = reg;
    760 	ZS_DELAY();
    761 	val = *cs->cs_reg_csr;
    762 	ZS_DELAY();
    763 	return (val);
    764 }
    765 
    766 void
    767 zs_write_reg(cs, reg, val)
    768 	struct zs_chanstate *cs;
    769 	u_char reg, val;
    770 {
    771 	*cs->cs_reg_csr = reg;
    772 	ZS_DELAY();
    773 	*cs->cs_reg_csr = val;
    774 	ZS_DELAY();
    775 }
    776 
    777 u_char
    778 zs_read_csr(cs)
    779 	struct zs_chanstate *cs;
    780 {
    781 	u_char val;
    782 
    783 	val = *cs->cs_reg_csr;
    784 	ZS_DELAY();
    785 	return (val);
    786 }
    787 
    788 void
    789 zs_write_csr(cs, val)
    790 	struct zs_chanstate *cs;
    791 	u_char val;
    792 {
    793 	*cs->cs_reg_csr = val;
    794 	ZS_DELAY();
    795 }
    796 
    797 u_char
    798 zs_read_data(cs)
    799 	struct zs_chanstate *cs;
    800 {
    801 	u_char val;
    802 
    803 	val = *cs->cs_reg_data;
    804 	ZS_DELAY();
    805 	return (val);
    806 }
    807 
    808 void  zs_write_data(cs, val)
    809 	struct zs_chanstate *cs;
    810 	u_char val;
    811 {
    812 	*cs->cs_reg_data = val;
    813 	ZS_DELAY();
    814 }
    815 
    816 /****************************************************************
    817  * Console support functions (Sun specific!)
    818  * Note: this code is allowed to know about the layout of
    819  * the chip registers, and uses that to keep things simple.
    820  * XXX - I think I like the mvme167 code better. -gwr
    821  ****************************************************************/
    822 
    823 /*
    824  * Handle user request to enter kernel debugger.
    825  */
    826 void
    827 zs_abort(cs)
    828 	struct zs_chanstate *cs;
    829 {
    830 	struct zschan *zc = zs_conschan_get;
    831 	int rr0;
    832 
    833 	/* Wait for end of break to avoid PROM abort. */
    834 	/* XXX - Limit the wait? */
    835 	do {
    836 		rr0 = zc->zc_csr;
    837 		ZS_DELAY();
    838 	} while (rr0 & ZSRR0_BREAK);
    839 
    840 #if defined(KGDB)
    841 	zskgdb(cs);
    842 #elif defined(DDB)
    843 	Debugger();
    844 #else
    845 	printf("stopping on keyboard abort\n");
    846 	callrom();
    847 #endif
    848 }
    849 
    850 int  zs_getc __P((void *arg));
    851 void zs_putc __P((void *arg, int c));
    852 
    853 /*
    854  * Polled input char.
    855  */
    856 int
    857 zs_getc(arg)
    858 	void *arg;
    859 {
    860 	struct zschan *zc = arg;
    861 	int s, c, rr0;
    862 
    863 	s = splhigh();
    864 	/* Wait for a character to arrive. */
    865 	do {
    866 		rr0 = zc->zc_csr;
    867 		ZS_DELAY();
    868 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    869 
    870 	c = zc->zc_data;
    871 	ZS_DELAY();
    872 	splx(s);
    873 
    874 	/*
    875 	 * This is used by the kd driver to read scan codes,
    876 	 * so don't translate '\r' ==> '\n' here...
    877 	 */
    878 	return (c);
    879 }
    880 
    881 /*
    882  * Polled output char.
    883  */
    884 void
    885 zs_putc(arg, c)
    886 	void *arg;
    887 	int c;
    888 {
    889 	struct zschan *zc = arg;
    890 	int s, rr0;
    891 
    892 	s = splhigh();
    893 
    894 	/* Wait for transmitter to become ready. */
    895 	do {
    896 		rr0 = zc->zc_csr;
    897 		ZS_DELAY();
    898 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    899 
    900 	/*
    901 	 * Send the next character.
    902 	 * Now you'd think that this could be followed by a ZS_DELAY()
    903 	 * just like all the other chip accesses, but it turns out that
    904 	 * the `transmit-ready' interrupt isn't de-asserted until
    905 	 * some period of time after the register write completes
    906 	 * (more than a couple instructions).  So to avoid stray
    907 	 * interrupts we put in the 2us delay regardless of cpu model.
    908 	 */
    909 	zc->zc_data = c;
    910 	delay(2);
    911 
    912 	splx(s);
    913 }
    914 
    915 /*****************************************************************/
    916 /*
    917  * Polled console input putchar.
    918  */
    919 int
    920 zscngetc(dev)
    921 	dev_t dev;
    922 {
    923 	return (zs_getc(zs_conschan_get));
    924 }
    925 
    926 /*
    927  * Polled console output putchar.
    928  */
    929 void
    930 zscnputc(dev, c)
    931 	dev_t dev;
    932 	int c;
    933 {
    934 	zs_putc(zs_conschan_put, c);
    935 }
    936 
    937 void
    938 zscnpollc(dev, on)
    939 	dev_t dev;
    940 	int on;
    941 {
    942 	/* No action needed */
    943 }
    944 
    945 int
    946 zs_console_flags(promunit, node, channel)
    947 	int promunit;
    948 	int node;
    949 	int channel;
    950 {
    951 	int cookie, flags = 0;
    952 
    953 	switch (prom_version()) {
    954 	case PROM_OLDMON:
    955 	case PROM_OBP_V0:
    956 		/*
    957 		 * Use `promunit' and `channel' to derive the PROM
    958 		 * stdio handles that correspond to this device.
    959 		 */
    960 		if (promunit == 0)
    961 			cookie = PROMDEV_TTYA + channel;
    962 		else if (promunit == 1 && channel == 0)
    963 			cookie = PROMDEV_KBD;
    964 		else
    965 			cookie = -1;
    966 
    967 		if (cookie == prom_stdin())
    968 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    969 
    970 		/*
    971 		 * Prevent the keyboard from matching the output device
    972 		 * (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
    973 		 */
    974 		if (cookie != PROMDEV_KBD && cookie == prom_stdout())
    975 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    976 
    977 		break;
    978 
    979 	case PROM_OBP_V2:
    980 	case PROM_OBP_V3:
    981 	case PROM_OPENFIRM:
    982 
    983 		/*
    984 		 * Match the nodes and device arguments prepared by
    985 		 * consinit() against our device node and channel.
    986 		 * (The device argument is the part of the OBP path
    987 		 * following the colon, as in `/obio/zs@0,100000:a')
    988 		 */
    989 
    990 		/* Default to channel 0 if there are no explicit prom args */
    991 		cookie = 0;
    992 
    993 		if (node == prom_stdin_node) {
    994 			if (prom_stdin_args[0] != '\0')
    995 				/* Translate (a,b) -> (0,1) */
    996 				cookie = prom_stdin_args[0] - 'a';
    997 
    998 			if (channel == cookie)
    999 				flags |= ZS_HWFLAG_CONSOLE_INPUT;
   1000 		}
   1001 
   1002 		if (node == prom_stdout_node) {
   1003 			if (prom_stdout_args[0] != '\0')
   1004 				/* Translate (a,b) -> (0,1) */
   1005 				cookie = prom_stdout_args[0] - 'a';
   1006 
   1007 			if (channel == cookie)
   1008 				flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
   1009 		}
   1010 
   1011 		break;
   1012 
   1013 	default:
   1014 		break;
   1015 	}
   1016 
   1017 	return (flags);
   1018 }
   1019 
   1020 /*
   1021  * Power management hooks for zsopen() and zsclose().
   1022  * We use them to power on/off the ports, if necessary.
   1023  */
   1024 int
   1025 zs_enable(cs)
   1026 	struct zs_chanstate *cs;
   1027 {
   1028 	auxiotwoserialendis (ZS_ENABLE);
   1029 	cs->enabled = 1;
   1030 	return(0);
   1031 }
   1032 
   1033 void
   1034 zs_disable(cs)
   1035 	struct zs_chanstate *cs;
   1036 {
   1037 	auxiotwoserialendis (ZS_DISABLE);
   1038 	cs->enabled = 0;
   1039 }
   1040