Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.28
      1 /*	$NetBSD: zs.c,v 1.28 1995/07/03 02:52:13 gwr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Gordon W. Ross
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This software was developed by the Computer Systems Engineering group
      9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     10  * contributed to Berkeley.
     11  *
     12  * All advertising materials mentioning features or use of this software
     13  * must display the following acknowledgement:
     14  *	This product includes software developed by the University of
     15  *	California, Lawrence Berkeley Laboratory.
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  * 3. All advertising materials mentioning features or use of this software
     26  *    must display the following acknowledgement:
     27  *	This product includes software developed by the University of
     28  *	California, Berkeley and its contributors.
     29  * 4. Neither the name of the University nor the names of its contributors
     30  *    may be used to endorse or promote products derived from this software
     31  *    without specific prior written permission.
     32  *
     33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     43  * SUCH DAMAGE.
     44  *
     45  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     46  */
     47 
     48 /*
     49  * Zilog Z8530 (ZSCC) driver.
     50  *
     51  * Runs two tty ports (ttya and ttyb) on zs0,
     52  * and runs a keyboard and mouse on zs1.
     53  *
     54  * This driver knows far too much about chip to usage mappings.
     55  */
     56 #define	NZS	2		/* XXX */
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/proc.h>
     61 #include <sys/device.h>
     62 #include <sys/conf.h>
     63 #include <sys/file.h>
     64 #include <sys/ioctl.h>
     65 #include <sys/tty.h>
     66 #include <sys/time.h>
     67 #include <sys/kernel.h>
     68 #include <sys/syslog.h>
     69 
     70 #include <machine/autoconf.h>
     71 #include <machine/cpu.h>
     72 #include <machine/isr.h>
     73 #include <machine/obio.h>
     74 #include <machine/mon.h>
     75 #include <machine/eeprom.h>
     76 #include <machine/kbd.h>
     77 
     78 #include <dev/cons.h>
     79 
     80 #include <dev/ic/z8530reg.h>
     81 #include <sun3/dev/zsvar.h>
     82 
     83 /*
     84  * The default parity REALLY needs to be the same as the PROM uses,
     85  * or you can not see messages done with printf during boot-up...
     86  */
     87 #undef	TTYDEF_CFLAG
     88 #define	TTYDEF_CFLAG	(CREAD | CS8 | HUPCL)
     89 
     90 #ifdef KGDB
     91 #include <machine/remote-sl.h>
     92 #endif
     93 
     94 #define	ZSMAJOR	12		/* XXX */
     95 
     96 #define	ZS_KBD		2	/* XXX */
     97 #define	ZS_MOUSE	3	/* XXX */
     98 
     99 /* The Sun3 provides a 4.9152 MHz clock to the ZS chips. */
    100 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
    101 
    102 /*
    103  * Define interrupt levels.
    104  */
    105 #define ZSHARD_PRI	6	/* Wired on the CPU board... */
    106 #define ZSSOFT_PRI	3	/* Want tty pri (4) but this is OK. */
    107 
    108 /*
    109  * Software state per found chip.  This would be called `zs_softc',
    110  * but the previous driver had a rather different zs_softc....
    111  */
    112 struct zsinfo {
    113 	struct	device zi_dev;		/* base device */
    114 	volatile struct zsdevice *zi_zs;/* chip registers */
    115 	struct	zs_chanstate zi_cs[2];	/* channel A and B software state */
    116 };
    117 
    118 static struct tty *zs_tty[NZS * 2]; 	/* XXX should be dynamic */
    119 
    120 /* Definition of the driver for autoconfig. */
    121 static int	zs_match(struct device *, void *, void *);
    122 static void	zs_attach(struct device *, struct device *, void *);
    123 
    124 struct cfdriver zscd = {
    125 	NULL, "zs", zs_match, zs_attach,
    126 	DV_TTY, sizeof(struct zsinfo) };
    127 
    128 /* Interrupt handlers. */
    129 static int	zshard(int);
    130 static int	zssoft(int);
    131 
    132 struct zs_chanstate *zslist;
    133 
    134 /* Routines called from other code. */
    135 int zsopen(dev_t, int, int, struct proc *);
    136 int zsclose(dev_t, int, int, struct proc *);
    137 static void	zsiopen(struct tty *);
    138 static void	zsiclose(struct tty *);
    139 static void	zsstart(struct tty *);
    140 void		zsstop(struct tty *, int);
    141 static int	zsparam(struct tty *, struct termios *);
    142 
    143 /* Routines purely local to this driver. */
    144 static int	zs_getspeed(volatile struct zschan *);
    145 static void	zs_reset(volatile struct zschan *, int, int);
    146 static void	zs_modem(struct zs_chanstate *, int);
    147 static void	zs_loadchannelregs(volatile struct zschan *, u_char *);
    148 static u_char zs_read(volatile struct zschan *, u_char);
    149 static u_char zs_write(volatile struct zschan *, u_char, u_char);
    150 
    151 /* Console stuff. */
    152 static volatile struct zschan *zs_conschan;
    153 
    154 #ifdef KGDB
    155 /* KGDB stuff.  Must reboot to change zs_kgdbunit. */
    156 extern int kgdb_dev, kgdb_rate;
    157 static int zs_kgdb_savedspeed;
    158 static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *);
    159 #endif
    160 
    161 /*
    162  * Console keyboard L1-A processing is done in the hardware interrupt code,
    163  * so we need to duplicate some of the console keyboard decode state.  (We
    164  * must not use the regular state as the hardware code keeps ahead of the
    165  * software state: the software state tracks the most recent ring input but
    166  * the hardware state tracks the most recent ZSCC input.)  See also kbd.h.
    167  */
    168 static struct conk_state {	/* console keyboard state */
    169 	char	conk_id;	/* true => ID coming up (console only) */
    170 	char	conk_l1;	/* true => L1 pressed (console only) */
    171 } zsconk_state;
    172 
    173 int zshardscope;
    174 int zsshortcuts;		/* number of "shortcut" software interrupts */
    175 
    176 int zssoftpending;		/* We have done isr_soft_request() */
    177 
    178 static struct zsdevice *zsaddr[NZS];	/* XXX, but saves work */
    179 
    180 /* Default OBIO addresses. */
    181 static int zs_physaddr[NZS] = { OBIO_ZS, OBIO_KEYBD_MS };
    182 
    183 static u_char zs_init_reg[16] = {
    184 	0,	/* 0: CMD (reset, etc.) */
    185 	ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE,
    186 	0x18 + ZSHARD_PRI,	/* IVECT */
    187 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    188 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    189 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    190 	0,	/* 6: TXSYNC/SYNCLO */
    191 	0,	/* 7: RXSYNC/SYNCHI */
    192 	0,	/* 8: alias for data port */
    193 	0,	/* 9: ZSWR9_MASTER_IE (later) */
    194 	0,	/*10: Misc. TX/RX control bits */
    195 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    196 	0,	/*12: BAUDLO (later) */
    197 	0,	/*13: BAUDHI (later) */
    198 	ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
    199 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    200 };
    201 
    202 /* Find PROM mappings (for console support). */
    203 void zs_init()
    204 {
    205 	int i;
    206 
    207 	for (i = 0; i < NZS; i++) {
    208 		zsaddr[i] = (struct zsdevice *)
    209 			obio_find_mapping(zs_physaddr[i], OBIO_ZS_SIZE);
    210 	}
    211 }
    212 
    213 /*
    214  * Match slave number to zs unit number, so that misconfiguration will
    215  * not set up the keyboard as ttya, etc.
    216  */
    217 static int
    218 zs_match(struct device *parent, void *vcf, void *args)
    219 {
    220 	struct cfdata *cf = vcf;
    221 	struct confargs *ca = args;
    222 	int unit, x;
    223 	void *zsva;
    224 
    225 	unit = cf->cf_unit;
    226 	if (unit < 0 || unit >= NZS)
    227 		return (0);
    228 
    229 	zsva = zsaddr[unit];
    230 	if (zsva == NULL)
    231 		return (0);
    232 
    233 	if (ca->ca_paddr == -1)
    234 		ca->ca_paddr = zs_physaddr[unit];
    235 	if (ca->ca_intpri == -1)
    236 		ca->ca_intpri = ZSHARD_PRI;
    237 
    238 	/* This returns -1 on a fault (bus error). */
    239 	x = peek_byte(zsva);
    240 	return (x != -1);
    241 }
    242 
    243 /*
    244  * Attach a found zs.
    245  *
    246  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    247  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    248  */
    249 static void
    250 zs_attach(struct device *parent, struct device *self, void *args)
    251 {
    252 	struct cfdata *cf;
    253 	struct confargs *ca;
    254 	register int zs, unit;
    255 	register struct zsinfo *zi;
    256 	register struct zs_chanstate *cs;
    257 	register volatile struct zsdevice *addr;
    258 	register struct tty *tp, *ctp;
    259 	int softcar;
    260 	static int didintr;
    261 
    262 	cf = self->dv_cfdata;
    263 	zs = self->dv_unit;
    264 	ca = args;
    265 
    266 	printf(" softpri %d\n", ZSSOFT_PRI);
    267 
    268 	if (zsaddr[zs] == NULL)
    269 		panic("zs_attach: zs%d not mapped\n", zs);
    270 	addr = zsaddr[zs];
    271 
    272 	if (!didintr) {
    273 		didintr = 1;
    274 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
    275 		isr_add_autovect(zshard, NULL, ZSHARD_PRI);
    276 	}
    277 
    278 	zi = (struct zsinfo *)self;
    279 	zi->zi_zs = addr;
    280 	unit = zs * 2;
    281 	cs = zi->zi_cs;
    282 	softcar = cf->cf_flags;
    283 
    284 	if(!zs_tty[unit])
    285 		zs_tty[unit] = ttymalloc();
    286 	if(!zs_tty[unit+1])
    287 		zs_tty[unit+1] = ttymalloc();
    288 
    289 	/* link into interrupt list with order (A,B) (B=A+1) */
    290 	cs[0].cs_next = &cs[1];
    291 	cs[1].cs_next = zslist;
    292 	zslist = cs;
    293 
    294 	tp = zs_tty[unit];
    295 	cs->cs_unit = unit;
    296 	cs->cs_zc = &addr->zs_chan[ZS_CHAN_A];
    297 	cs->cs_speed = zs_getspeed(cs->cs_zc);
    298 #ifdef	DEBUG
    299 	mon_printf("zs%da speed %d ",  zs, cs->cs_speed);
    300 #endif
    301 	cs->cs_softcar = softcar & 1;
    302 	cs->cs_ttyp = tp;
    303 	tp->t_dev = makedev(ZSMAJOR, unit);
    304 	tp->t_oproc = zsstart;
    305 	tp->t_param = zsparam;
    306 	if (cs->cs_zc == zs_conschan) {
    307 		/* This unit is the console. */
    308 		cs->cs_consio = 1;
    309 		cs->cs_brkabort = 1;
    310 		cs->cs_softcar = 1;
    311 		/* Call zsparam so interrupts get enabled. */
    312 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    313 		tp->t_cflag = TTYDEF_CFLAG;
    314 		(void) zsparam(tp, &tp->t_termios);
    315 	} else {
    316 		/* Can not run kgdb on the console? */
    317 #ifdef KGDB
    318 		zs_checkkgdb(unit, cs, tp);
    319 #endif
    320 	}
    321 #if 0
    322 	/* XXX - Drop carrier here? -gwr */
    323 	zs_modem(cs, cs->cs_softcar ? 1 : 0);
    324 #endif
    325 
    326 	if (unit == ZS_KBD) {
    327 		/*
    328 		 * Keyboard: tell /dev/kbd driver how to talk to us.
    329 		 */
    330 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    331 		tp->t_cflag = CS8;
    332 		/* zsparam called by zsiopen */
    333 		kbd_serial(tp, zsiopen, zsiclose);
    334 		cs->cs_conk = 1;		/* do L1-A processing */
    335 	}
    336 	unit++;
    337 	cs++;
    338 	tp = zs_tty[unit];
    339 
    340 	cs->cs_unit = unit;
    341 	cs->cs_zc = &addr->zs_chan[ZS_CHAN_B];
    342 	cs->cs_speed = zs_getspeed(cs->cs_zc);
    343 #ifdef	DEBUG
    344 	mon_printf("zs%db speed %d\n", zs, cs->cs_speed);
    345 #endif
    346 	cs->cs_softcar = softcar & 2;
    347 	cs->cs_ttyp = tp;
    348 	tp->t_dev = makedev(ZSMAJOR, unit);
    349 	tp->t_oproc = zsstart;
    350 	tp->t_param = zsparam;
    351 	if (cs->cs_zc == zs_conschan) {
    352 		/* This unit is the console. */
    353 		cs->cs_consio = 1;
    354 		cs->cs_brkabort = 1;
    355 		cs->cs_softcar = 1;
    356 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    357 		tp->t_cflag = TTYDEF_CFLAG;
    358 		(void) zsparam(tp, &tp->t_termios);
    359 	} else {
    360 		/* Can not run kgdb on the console? */
    361 #ifdef KGDB
    362 		zs_checkkgdb(unit, cs, tp);
    363 #endif
    364 	}
    365 #if 0
    366 	/* XXX - Drop carrier here? -gwr */
    367 	zs_modem(cs, cs->cs_softcar ? 1 : 0);
    368 #endif
    369 
    370 	if (unit == ZS_MOUSE) {
    371 		/*
    372 		 * Mouse: tell /dev/mouse driver how to talk to us.
    373 		 */
    374 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    375 		tp->t_cflag = CS8;
    376 		/* zsparam called by zsiopen */
    377 		ms_serial(tp, zsiopen, zsiclose);
    378 	}
    379 }
    380 
    381 /*
    382  * XXX - Temporary hack...
    383  */
    384 struct tty *
    385 zstty(dev)
    386 	dev_t dev;
    387 {
    388 	int unit = minor(dev);
    389 
    390 	return (zs_tty[unit]);
    391 }
    392 
    393 /*
    394  * Put a channel in a known state.  Interrupts may be left disabled
    395  * or enabled, as desired.  (Used only by kgdb)
    396  */
    397 static void
    398 zs_reset(zc, inten, speed)
    399 	volatile struct zschan *zc;
    400 	int inten, speed;
    401 {
    402 	int tconst;
    403 	u_char reg[16];
    404 
    405 	bcopy(zs_init_reg, reg, 16);
    406 	if (inten)
    407 		reg[9] |= ZSWR9_MASTER_IE;
    408 
    409 	tconst = BPS_TO_TCONST(PCLK / 16, speed);
    410 	reg[12] = tconst;
    411 	reg[13] = tconst >> 8;
    412 	zs_loadchannelregs(zc, reg);
    413 }
    414 
    415 /*
    416  * Console support
    417  */
    418 
    419 /*
    420  * Used by the kd driver to find out if it can work.
    421  */
    422 int
    423 zscnprobe_kbd()
    424 {
    425 	if (zsaddr[1] == NULL) {
    426 		mon_printf("zscnprobe_kbd: zs1 not yet mapped\n");
    427 		return CN_DEAD;
    428 	}
    429 	return CN_INTERNAL;
    430 }
    431 
    432 /*
    433  * This is the console probe routine for ttya and ttyb.
    434  */
    435 static int
    436 zscnprobe(struct consdev *cn, int unit)
    437 {
    438 	int maj;
    439 
    440 	if (zsaddr[0] == NULL) {
    441 		mon_printf("zscnprobe: zs0 not mapped\n");
    442 		cn->cn_pri = CN_DEAD;
    443 		return 0;
    444 	}
    445 	/* XXX - Also try to make sure it exists? */
    446 
    447 	/* locate the major number */
    448 	for (maj = 0; maj < nchrdev; maj++)
    449 		if (cdevsw[maj].d_open == (void*)zsopen)
    450 			break;
    451 
    452 	cn->cn_dev = makedev(maj, unit);
    453 
    454 	/* Use EEPROM console setting to decide "remote" console. */
    455 	/* Note: EE_CONS_TTYA + 1 == EE_CONS_TTYB */
    456 	if (ee_console == (EE_CONS_TTYA + unit)) {
    457 		cn->cn_pri = CN_REMOTE;
    458 	} else {
    459 		cn->cn_pri = CN_NORMAL;
    460 	}
    461 	return (0);
    462 }
    463 
    464 /* This is the constab entry for TTYA. */
    465 int
    466 zscnprobe_a(struct consdev *cn)
    467 {
    468 	return (zscnprobe(cn, 0));
    469 }
    470 
    471 /* This is the constab entry for TTYB. */
    472 int
    473 zscnprobe_b(struct consdev *cn)
    474 {
    475 	return (zscnprobe(cn, 1));
    476 }
    477 
    478 /* Called by kdcninit() or below. */
    479 void
    480 zs_set_conschan(unit, ab)
    481 	int unit, ab;
    482 {
    483 	volatile struct zsdevice *addr;
    484 
    485 	addr = zsaddr[unit];
    486 	zs_conschan = ((ab == 0) ?
    487 				   &addr->zs_chan[ZS_CHAN_A] :
    488 				   &addr->zs_chan[ZS_CHAN_B] );
    489 }
    490 
    491 /* Attach as console.  Also set zs_conschan */
    492 int
    493 zscninit(struct consdev *cn)
    494 {
    495 	int ab = minor(cn->cn_dev) & 1;
    496 	zs_set_conschan(0, ab);
    497 	mon_printf("console on zs0 (tty%c)\n", 'a' + ab);
    498 }
    499 
    500 
    501 /*
    502  * Polled console input putchar.
    503  */
    504 int
    505 zscngetc(dev)
    506 	dev_t dev;
    507 {
    508 	register volatile struct zschan *zc = zs_conschan;
    509 	register int s, c, rr0;
    510 
    511 	if (zc == NULL)
    512 		return (0);
    513 
    514 	s = splhigh();
    515 
    516 	/* Wait for a character to arrive. */
    517 	do {
    518 		rr0 = zc->zc_csr;
    519 		ZS_DELAY();
    520 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    521 
    522 	c = zc->zc_data;
    523 	ZS_DELAY();
    524 
    525 	splx(s);
    526 
    527 	/*
    528 	 * This is used by the kd driver to read scan codes,
    529 	 * so don't translate '\r' ==> '\n' here...
    530 	 */
    531 	return (c);
    532 }
    533 
    534 /*
    535  * Polled console output putchar.
    536  */
    537 int
    538 zscnputc(dev, c)
    539 	dev_t dev;
    540 	int c;
    541 {
    542 	register volatile struct zschan *zc = zs_conschan;
    543 	register int s, rr0;
    544 
    545 	if (zc == NULL) {
    546 		s = splhigh();
    547 		mon_putchar(c);
    548 		splx(s);
    549 		return (0);
    550 	}
    551 	s = splhigh();
    552 
    553 	/* Wait for transmitter to become ready. */
    554 	do {
    555 		rr0 = zc->zc_csr;
    556 		ZS_DELAY();
    557 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    558 
    559 	zc->zc_data = c;
    560 	ZS_DELAY();
    561 	splx(s);
    562 }
    563 
    564 #ifdef KGDB
    565 /*
    566  * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
    567  * Pick up the current speed and character size and restore the original
    568  * speed.
    569  */
    570 static void
    571 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
    572 {
    573 
    574 	if (kgdb_dev == makedev(ZSMAJOR, unit)) {
    575 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    576 		tp->t_cflag = CS8;
    577 		cs->cs_kgdb = 1;
    578 		cs->cs_speed = zs_kgdb_savedspeed;
    579 		(void) zsparam(tp, &tp->t_termios);
    580 	}
    581 }
    582 #endif
    583 
    584 /*
    585  * Compute the current baud rate given a ZSCC channel.
    586  */
    587 static int
    588 zs_getspeed(zc)
    589 	register volatile struct zschan *zc;
    590 {
    591 	register int tconst;
    592 
    593 	tconst = ZS_READ(zc, 12);
    594 	tconst |= ZS_READ(zc, 13) << 8;
    595 	return (TCONST_TO_BPS(PCLK / 16, tconst));
    596 }
    597 
    598 
    599 /*
    600  * Do an internal open.
    601  */
    602 static void
    603 zsiopen(struct tty *tp)
    604 {
    605 
    606 	(void) zsparam(tp, &tp->t_termios);
    607 	ttsetwater(tp);
    608 	tp->t_state = TS_ISOPEN | TS_CARR_ON;
    609 }
    610 
    611 /*
    612  * Do an internal close.  Eventually we should shut off the chip when both
    613  * ports on it are closed.
    614  */
    615 static void
    616 zsiclose(struct tty *tp)
    617 {
    618 
    619 	ttylclose(tp, 0);	/* ??? */
    620 	ttyclose(tp);		/* ??? */
    621 	tp->t_state = 0;
    622 }
    623 
    624 
    625 /*
    626  * Open a zs serial port.  This interface may not be used to open
    627  * the keyboard and mouse ports. (XXX)
    628  */
    629 int
    630 zsopen(dev_t dev, int flags, int mode, struct proc *p)
    631 {
    632 	register struct tty *tp;
    633 	register struct zs_chanstate *cs;
    634 	struct zsinfo *zi;
    635 	int unit = minor(dev), zs = unit >> 1, error, s;
    636 
    637 #ifdef	DEBUG
    638 	mon_printf("zs_open\n");
    639 #endif
    640 	if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
    641 	    unit == ZS_KBD || unit == ZS_MOUSE)
    642 		return (ENXIO);
    643 	cs = &zi->zi_cs[unit & 1];
    644 	tp = cs->cs_ttyp;
    645 	s = spltty();
    646 	if ((tp->t_state & TS_ISOPEN) == 0) {
    647 		ttychars(tp);
    648 		tp->t_iflag = TTYDEF_IFLAG;
    649 		tp->t_oflag = TTYDEF_OFLAG;
    650 		tp->t_cflag = TTYDEF_CFLAG;
    651 		tp->t_lflag = TTYDEF_LFLAG;
    652 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    653 		(void) zsparam(tp, &tp->t_termios);
    654 		ttsetwater(tp);
    655 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    656 		splx(s);
    657 		return (EBUSY);
    658 	}
    659 	error = 0;
    660 #ifdef	DEBUG
    661 	mon_printf("wait for carrier...\n");
    662 #endif
    663 	for (;;) {
    664 		register int rr0;
    665 
    666 		/* loop, turning on the device, until carrier present */
    667 		zs_modem(cs, 1);
    668 		/* May never get status intr if carrier already on. -gwr */
    669 		rr0 = cs->cs_zc->zc_csr;
    670 		ZS_DELAY();
    671 		if (rr0 & ZSRR0_DCD)
    672 			tp->t_state |= TS_CARR_ON;
    673 		if (cs->cs_softcar)
    674 			tp->t_state |= TS_CARR_ON;
    675 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
    676 		    tp->t_state & TS_CARR_ON)
    677 			break;
    678 		tp->t_state |= TS_WOPEN;
    679 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    680 		    ttopen, 0)) {
    681 			if (!(tp->t_state & TS_ISOPEN)) {
    682 				zs_modem(cs, 0);
    683 				tp->t_state &= ~TS_WOPEN;
    684 				ttwakeup(tp);
    685 			}
    686 			splx(s);
    687 			return error;
    688 		}
    689 	}
    690 #ifdef	DEBUG
    691 	mon_printf("...carrier %s\n",
    692 			   (tp->t_state & TS_CARR_ON) ? "on" : "off");
    693 #endif
    694 	splx(s);
    695 	if (error == 0)
    696 		error = linesw[tp->t_line].l_open(dev, tp);
    697 	if (error)
    698 		zs_modem(cs, 0);
    699 	return (error);
    700 }
    701 
    702 /*
    703  * Close a zs serial port.
    704  */
    705 int
    706 zsclose(dev_t dev, int flags, int mode, struct proc *p)
    707 {
    708 	register struct zs_chanstate *cs;
    709 	register struct tty *tp;
    710 	struct zsinfo *zi;
    711 	int unit = minor(dev), s;
    712 
    713 #ifdef	DEBUG
    714 	mon_printf("zs_close\n");
    715 #endif
    716 	zi = zscd.cd_devs[unit >> 1];
    717 	cs = &zi->zi_cs[unit & 1];
    718 	tp = cs->cs_ttyp;
    719 	linesw[tp->t_line].l_close(tp, flags);
    720 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
    721 	    (tp->t_state & TS_ISOPEN) == 0) {
    722 		zs_modem(cs, 0);
    723 		/* hold low for 1 second */
    724 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
    725 	}
    726 	if (cs->cs_creg[5] & ZSWR5_BREAK)
    727 	{
    728 		s = splzs();
    729 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    730 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    731 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    732 		splx(s);
    733 	}
    734 	ttyclose(tp);
    735 #ifdef KGDB
    736 	/* Reset the speed if we're doing kgdb on this port */
    737 	if (cs->cs_kgdb) {
    738 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    739 		(void) zsparam(tp, &tp->t_termios);
    740 	}
    741 #endif
    742 	return (0);
    743 }
    744 
    745 /*
    746  * Read/write zs serial port.
    747  */
    748 int
    749 zsread(dev_t dev, struct uio *uio, int flags)
    750 {
    751 	register struct tty *tp = zs_tty[minor(dev)];
    752 
    753 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    754 }
    755 
    756 int
    757 zswrite(dev_t dev, struct uio *uio, int flags)
    758 {
    759 	register struct tty *tp = zs_tty[minor(dev)];
    760 
    761 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    762 }
    763 
    764 /*
    765  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    766  * channels are kept in (A,B) pairs.
    767  *
    768  * Do just a little, then get out; set a software interrupt if more
    769  * work is needed.
    770  *
    771  * We deliberately ignore the vectoring Zilog gives us, and match up
    772  * only the number of `reset interrupt under service' operations, not
    773  * the order.
    774  */
    775 /* ARGSUSED */
    776 int
    777 zshard(int intrarg)
    778 {
    779 	register struct zs_chanstate *a;
    780 #define	b (a + 1)
    781 	register volatile struct zschan *zc;
    782 	register int rr3, intflags = 0, v, i;
    783 	static int zsrint(struct zs_chanstate *, volatile struct zschan *);
    784 	static int zsxint(struct zs_chanstate *, volatile struct zschan *);
    785 	static int zssint(struct zs_chanstate *, volatile struct zschan *);
    786 
    787 	for (a = zslist; a != NULL; a = b->cs_next) {
    788 		rr3 = ZS_READ(a->cs_zc, 3);
    789 
    790 		/* XXX - This should loop to empty the on-chip fifo. */
    791 		if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    792 			intflags |= 2;
    793 			zc = a->cs_zc;
    794 			i = a->cs_rbput;
    795 			if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    796 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    797 				intflags |= 1;
    798 			}
    799 			if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    800 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    801 				intflags |= 1;
    802 			}
    803 			if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    804 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    805 				intflags |= 1;
    806 			}
    807 			a->cs_rbput = i;
    808 		}
    809 
    810 		/* XXX - This should loop to empty the on-chip fifo. */
    811 		if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    812 			intflags |= 2;
    813 			zc = b->cs_zc;
    814 			i = b->cs_rbput;
    815 			if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    816 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    817 				intflags |= 1;
    818 			}
    819 			if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    820 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    821 				intflags |= 1;
    822 			}
    823 			if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    824 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    825 				intflags |= 1;
    826 			}
    827 			b->cs_rbput = i;
    828 		}
    829 	}
    830 #undef b
    831 	if (intflags & 1) {
    832 		if (zssoftpending == 0) {
    833 			/* We are at splzs here, so no need to lock. */
    834 			zssoftpending = ZSSOFT_PRI;
    835 			isr_soft_request(ZSSOFT_PRI);
    836 		}
    837 	}
    838 	return (intflags & 2);
    839 }
    840 
    841 static int
    842 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
    843 {
    844 	register int c;
    845 
    846 	c = zc->zc_data;
    847 	ZS_DELAY();
    848 
    849 	if (cs->cs_conk) {
    850 		register struct conk_state *conk = &zsconk_state;
    851 
    852 		/*
    853 		 * Check here for console abort function, so that we
    854 		 * can abort even when interrupts are locking up the
    855 		 * machine.
    856 		 */
    857 		if (c == KBD_RESET) {
    858 			conk->conk_id = 1;	/* ignore next byte */
    859 			conk->conk_l1 = 0;
    860 		} else if (conk->conk_id)
    861 			conk->conk_id = 0;	/* stop ignoring bytes */
    862 		else if (c == KBD_L1)
    863 			conk->conk_l1 = 1;	/* L1 went down */
    864 		else if (c == (KBD_L1|KBD_UP))
    865 			conk->conk_l1 = 0;	/* L1 went up */
    866 		else if (c == KBD_A && conk->conk_l1) {
    867 			zsabort();
    868 			/* Debugger done.  Send L1-up in case X is running. */
    869 			conk->conk_l1 = 0;
    870 			c = (KBD_L1|KBD_UP);
    871 		}
    872 	}
    873 #ifdef KGDB
    874 	if (c == FRAME_START && cs->cs_kgdb &&
    875 	    (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
    876 		zskgdb(cs->cs_unit);
    877 		c = 0;
    878 		goto clearit;
    879 	}
    880 #endif
    881 	/* compose receive character and status */
    882 	c <<= 8;
    883 	c |= ZS_READ(zc, 1);
    884 	c = ZRING_MAKE(ZRING_RINT, c);
    885 
    886 clearit:
    887 	/* clear receive error & interrupt condition */
    888 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    889 	ZS_DELAY();
    890 	zc->zc_csr = ZSWR0_CLR_INTR;
    891 	ZS_DELAY();
    892 	return (c);
    893 }
    894 
    895 static int
    896 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
    897 {
    898 	register int i = cs->cs_tbc;
    899 
    900 	if (i == 0) {
    901 		zc->zc_csr = ZSWR0_RESET_TXINT;
    902 		ZS_DELAY();
    903 		zc->zc_csr = ZSWR0_CLR_INTR;
    904 		ZS_DELAY();
    905 		return (ZRING_MAKE(ZRING_XINT, 0));
    906 	}
    907 	cs->cs_tbc = i - 1;
    908 	zc->zc_data = *cs->cs_tba++;
    909 	ZS_DELAY();
    910 	zc->zc_csr = ZSWR0_CLR_INTR;
    911 	ZS_DELAY();
    912 	return (0);
    913 }
    914 
    915 static int
    916 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
    917 {
    918 	register int rr0;
    919 
    920 	rr0 = zc->zc_csr;
    921 	ZS_DELAY();
    922 	zc->zc_csr = ZSWR0_RESET_STATUS;
    923 	ZS_DELAY();
    924 	zc->zc_csr = ZSWR0_CLR_INTR;
    925 	ZS_DELAY();
    926 	/*
    927 	 * The chip's hardware flow control is, as noted in zsreg.h,
    928 	 * busted---if the DCD line goes low the chip shuts off the
    929 	 * receiver (!).  If we want hardware CTS flow control but do
    930 	 * not have it, and carrier is now on, turn HFC on; if we have
    931 	 * HFC now but carrier has gone low, turn it off.
    932 	 */
    933 	if (rr0 & ZSRR0_DCD) {
    934 		if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    935 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    936 			cs->cs_creg[3] |= ZSWR3_HFC;
    937 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    938 		}
    939 	} else {
    940 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    941 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    942 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    943 		}
    944 	}
    945 	if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
    946 		/* Wait for end of break to avoid PROM abort. */
    947 		do {
    948 			rr0 = zc->zc_csr;
    949 			ZS_DELAY();
    950 		} while (rr0 & ZSRR0_BREAK);
    951 		zsabort();
    952 		return (0);
    953 	}
    954 	return (ZRING_MAKE(ZRING_SINT, rr0));
    955 }
    956 
    957 zsabort()
    958 {
    959 #ifdef DDB
    960 	Debugger();
    961 #else
    962 	sun3_mon_abort();
    963 #endif
    964 }
    965 
    966 #ifdef KGDB
    967 /*
    968  * KGDB framing character received: enter kernel debugger.  This probably
    969  * should time out after a few seconds to avoid hanging on spurious input.
    970  */
    971 zskgdb(int unit)
    972 {
    973 
    974 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
    975 	kgdb_connect(1);
    976 }
    977 #endif
    978 
    979 /*
    980  * Print out a ring or fifo overrun error message.
    981  */
    982 static void
    983 zsoverrun(int unit, long *ptime, char *what)
    984 {
    985 
    986 	if (*ptime != time.tv_sec) {
    987 		*ptime = time.tv_sec;
    988 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    989 		    (unit & 1) + 'a', what);
    990 	}
    991 }
    992 
    993 /*
    994  * ZS software interrupt.  Scan all channels for deferred interrupts.
    995  */
    996 int
    997 zssoft(int arg)
    998 {
    999 	register struct zs_chanstate *cs;
   1000 	register volatile struct zschan *zc;
   1001 	register struct linesw *line;
   1002 	register struct tty *tp;
   1003 	register int get, n, c, cc, unit, s;
   1004 
   1005 	/* This is not the only ISR on this IPL. */
   1006 	if (zssoftpending == 0)
   1007 		return (0);
   1008 
   1009 	/*
   1010 	 * The soft intr. bit will be set by zshard only if
   1011 	 * the variable zssoftpending is zero.  The order of
   1012 	 * these next two statements prevents our clearing
   1013 	 * the soft intr bit just after zshard has set it.
   1014 	 */
   1015 	isr_soft_clear(ZSSOFT_PRI);
   1016 	zssoftpending = 0;	/* Now zshard may set it again. */
   1017 
   1018 	for (cs = zslist; cs != NULL; cs = cs->cs_next) {
   1019 		get = cs->cs_rbget;
   1020 again:
   1021 		n = cs->cs_rbput;	/* atomic */
   1022 		if (get == n)		/* nothing more on this line */
   1023 			continue;
   1024 		unit = cs->cs_unit;	/* set up to handle interrupts */
   1025 		zc = cs->cs_zc;
   1026 		tp = cs->cs_ttyp;
   1027 		line = &linesw[tp->t_line];
   1028 		/*
   1029 		 * Compute the number of interrupts in the receive ring.
   1030 		 * If the count is overlarge, we lost some events, and
   1031 		 * must advance to the first valid one.  It may get
   1032 		 * overwritten if more data are arriving, but this is
   1033 		 * too expensive to check and gains nothing (we already
   1034 		 * lost out; all we can do at this point is trade one
   1035 		 * kind of loss for another).
   1036 		 */
   1037 		n -= get;
   1038 		if (n > ZLRB_RING_SIZE) {
   1039 			zsoverrun(unit, &cs->cs_rotime, "ring");
   1040 			get += n - ZLRB_RING_SIZE;
   1041 			n = ZLRB_RING_SIZE;
   1042 		}
   1043 		while (--n >= 0) {
   1044 			/* race to keep ahead of incoming interrupts */
   1045 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
   1046 			switch (ZRING_TYPE(c)) {
   1047 
   1048 			case ZRING_RINT:
   1049 				c = ZRING_VALUE(c);
   1050 				if (c & ZSRR1_DO)
   1051 					zsoverrun(unit, &cs->cs_fotime, "fifo");
   1052 				cc = c >> 8;
   1053 				if (c & ZSRR1_FE)
   1054 					cc |= TTY_FE;
   1055 				if (c & ZSRR1_PE)
   1056 					cc |= TTY_PE;
   1057 				/*
   1058 				 * this should be done through
   1059 				 * bstreams	XXX gag choke
   1060 				 */
   1061 				if (unit == ZS_KBD)
   1062 					kbd_rint(cc);
   1063 				else if (unit == ZS_MOUSE)
   1064 					ms_rint(cc);
   1065 				else
   1066 					line->l_rint(cc, tp);
   1067 				break;
   1068 
   1069 			case ZRING_XINT:
   1070 				/*
   1071 				 * Transmit done: change registers and resume,
   1072 				 * or clear BUSY.
   1073 				 */
   1074 				if (cs->cs_heldchange) {
   1075 					s = splzs();
   1076 					c = zc->zc_csr;
   1077 					ZS_DELAY();
   1078 					if ((c & ZSRR0_DCD) == 0)
   1079 						cs->cs_preg[3] &= ~ZSWR3_HFC;
   1080 					bcopy((caddr_t)cs->cs_preg,
   1081 					    (caddr_t)cs->cs_creg, 16);
   1082 					zs_loadchannelregs(zc, cs->cs_creg);
   1083 					splx(s);
   1084 					cs->cs_heldchange = 0;
   1085 					if (cs->cs_heldtbc &&
   1086 					    (tp->t_state & TS_TTSTOP) == 0) {
   1087 						cs->cs_tbc = cs->cs_heldtbc - 1;
   1088 						zc->zc_data = *cs->cs_tba++;
   1089 						ZS_DELAY();
   1090 						goto again;
   1091 					}
   1092 				}
   1093 				tp->t_state &= ~TS_BUSY;
   1094 				if (tp->t_state & TS_FLUSH)
   1095 					tp->t_state &= ~TS_FLUSH;
   1096 				else
   1097 					ndflush(&tp->t_outq, cs->cs_tba -
   1098 						(caddr_t) tp->t_outq.c_cf);
   1099 				line->l_start(tp);
   1100 				break;
   1101 
   1102 			case ZRING_SINT:
   1103 				/*
   1104 				 * Status line change.  HFC bit is run in
   1105 				 * hardware interrupt, to avoid locking
   1106 				 * at splzs here.
   1107 				 */
   1108 				c = ZRING_VALUE(c);
   1109 				if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
   1110 					cc = (c & ZSRR0_DCD) != 0;
   1111 					if (line->l_modem(tp, cc) == 0)
   1112 						zs_modem(cs, cc);
   1113 				}
   1114 				cs->cs_rr0 = c;
   1115 				break;
   1116 
   1117 			default:
   1118 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
   1119 				    unit >> 1, (unit & 1) + 'a', c);
   1120 				break;
   1121 			}
   1122 		}
   1123 		cs->cs_rbget = get;
   1124 		goto again;
   1125 	}
   1126 	return (1);
   1127 }
   1128 
   1129 int
   1130 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
   1131 {
   1132 	int unit = minor(dev);
   1133 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1134 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1135 	register struct tty *tp = cs->cs_ttyp;
   1136 	register int error, s;
   1137 
   1138 	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
   1139 	if (error >= 0)
   1140 		return (error);
   1141 	error = ttioctl(tp, cmd, data, flag, p);
   1142 	if (error >= 0)
   1143 		return (error);
   1144 
   1145 	switch (cmd) {
   1146 
   1147 	case TIOCSBRK:
   1148 		s = splzs();
   1149 		cs->cs_preg[5] |= ZSWR5_BREAK;
   1150 		cs->cs_creg[5] |= ZSWR5_BREAK;
   1151 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1152 		splx(s);
   1153 		break;
   1154 
   1155 	case TIOCCBRK:
   1156 		s = splzs();
   1157 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1158 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1159 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1160 		splx(s);
   1161 		break;
   1162 
   1163 	case TIOCGFLAGS: {
   1164 		int bits = 0;
   1165 
   1166 		if (cs->cs_softcar)
   1167 			bits |= TIOCFLAG_SOFTCAR;
   1168 		if (cs->cs_creg[15] & ZSWR15_DCD_IE)
   1169 			bits |= TIOCFLAG_CLOCAL;
   1170 		if (cs->cs_creg[3] & ZSWR3_HFC)
   1171 			bits |= TIOCFLAG_CRTSCTS;
   1172 		*(int *)data = bits;
   1173 		break;
   1174 	}
   1175 
   1176 	case TIOCSFLAGS: {
   1177 		int userbits, driverbits = 0;
   1178 
   1179 		error = suser(p->p_ucred, &p->p_acflag);
   1180 		if (error != 0)
   1181 			return (EPERM);
   1182 
   1183 		userbits = *(int *)data;
   1184 
   1185 		/*
   1186 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
   1187 		 * defaulting to software flow control.
   1188 		 */
   1189 		if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
   1190 			return(EINVAL);
   1191 		if (userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
   1192 			return(ENXIO);
   1193 
   1194 		s = splzs();
   1195 		if ((userbits & TIOCFLAG_SOFTCAR) ||
   1196 			(cs->cs_zc == zs_conschan))
   1197 		{
   1198 			cs->cs_softcar = 1;	/* turn on softcar */
   1199 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
   1200 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
   1201 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1202 		} else if (userbits & TIOCFLAG_CLOCAL) {
   1203 			cs->cs_softcar = 0; 	/* turn off softcar */
   1204 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
   1205 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
   1206 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1207 			tp->t_termios.c_cflag |= CLOCAL;
   1208 		}
   1209 		if (userbits & TIOCFLAG_CRTSCTS) {
   1210 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
   1211 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
   1212 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1213 			cs->cs_preg[3] |= ZSWR3_HFC;
   1214 			cs->cs_creg[3] |= ZSWR3_HFC;
   1215 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1216 			tp->t_termios.c_cflag |= CRTSCTS;
   1217 		} else {
   1218 			/* no mdmbuf, so we must want software flow control */
   1219 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
   1220 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
   1221 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1222 			cs->cs_preg[3] &= ~ZSWR3_HFC;
   1223 			cs->cs_creg[3] &= ~ZSWR3_HFC;
   1224 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1225 			tp->t_termios.c_cflag &= ~CRTSCTS;
   1226 		}
   1227 		splx(s);
   1228 		break;
   1229 	}
   1230 
   1231 	case TIOCSDTR:
   1232 	case TIOCCDTR:
   1233 	case TIOCMSET:
   1234 	case TIOCMBIS:
   1235 	case TIOCMBIC:
   1236 	case TIOCMGET:
   1237 	default:
   1238 		return (ENOTTY);
   1239 	}
   1240 	return (0);
   1241 }
   1242 
   1243 /*
   1244  * Start or restart transmission.
   1245  */
   1246 static void
   1247 zsstart(register struct tty *tp)
   1248 {
   1249 	register struct zs_chanstate *cs;
   1250 	register int s, nch;
   1251 	int unit = minor(tp->t_dev);
   1252 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1253 
   1254 	cs = &zi->zi_cs[unit & 1];
   1255 	s = spltty();
   1256 
   1257 	/*
   1258 	 * If currently active or delaying, no need to do anything.
   1259 	 */
   1260 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
   1261 		goto out;
   1262 
   1263 	/*
   1264 	 * If there are sleepers, and output has drained below low
   1265 	 * water mark, awaken.
   1266 	 */
   1267 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1268 		if (tp->t_state & TS_ASLEEP) {
   1269 			tp->t_state &= ~TS_ASLEEP;
   1270 			wakeup((caddr_t)&tp->t_outq);
   1271 		}
   1272 		selwakeup(&tp->t_wsel);
   1273 	}
   1274 
   1275 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1276 	if (nch) {
   1277 		register char *p = tp->t_outq.c_cf;
   1278 
   1279 		/* mark busy, enable tx done interrupts, & send first byte */
   1280 		tp->t_state |= TS_BUSY;
   1281 		(void) splzs();
   1282 		cs->cs_preg[1] |= ZSWR1_TIE;
   1283 		cs->cs_creg[1] |= ZSWR1_TIE;
   1284 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1285 		cs->cs_zc->zc_data = *p;
   1286 		ZS_DELAY();
   1287 		cs->cs_tba = p + 1;
   1288 		cs->cs_tbc = nch - 1;
   1289 	} else {
   1290 		/*
   1291 		 * Nothing to send, turn off transmit done interrupts.
   1292 		 * This is useful if something is doing polled output.
   1293 		 */
   1294 		(void) splzs();
   1295 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1296 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1297 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1298 	}
   1299 out:
   1300 	splx(s);
   1301 }
   1302 
   1303 /*
   1304  * Stop output, e.g., for ^S or output flush.
   1305  */
   1306 void
   1307 zsstop(register struct tty *tp, int flag)
   1308 {
   1309 	register struct zs_chanstate *cs;
   1310 	register int s, unit = minor(tp->t_dev);
   1311 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1312 
   1313 	cs = &zi->zi_cs[unit & 1];
   1314 	s = splzs();
   1315 	if (tp->t_state & TS_BUSY) {
   1316 		/*
   1317 		 * Device is transmitting; must stop it.
   1318 		 */
   1319 		cs->cs_tbc = 0;
   1320 		if ((tp->t_state & TS_TTSTOP) == 0)
   1321 			tp->t_state |= TS_FLUSH;
   1322 	}
   1323 	splx(s);
   1324 }
   1325 
   1326 /*
   1327  * Set ZS tty parameters from termios.
   1328  */
   1329 static int
   1330 zsparam(register struct tty *tp, register struct termios *t)
   1331 {
   1332 	int unit = minor(tp->t_dev);
   1333 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1334 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1335 	register int tmp, tmp5, cflag, s;
   1336 
   1337 	/*
   1338 	 * Because PCLK is only run at 4.9 MHz, the fastest we
   1339 	 * can go is 51200 baud (this corresponds to TC=1).
   1340 	 * This is somewhat unfortunate as there is no real
   1341 	 * reason we should not be able to handle higher rates.
   1342 	 */
   1343 	tmp = t->c_ospeed;
   1344 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
   1345 		return (EINVAL);
   1346 	if (tmp == 0) {
   1347 		/* stty 0 => drop DTR and RTS */
   1348 		zs_modem(cs, 0);
   1349 		return (0);
   1350 	}
   1351 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
   1352 	if (tmp < 2)
   1353 		return (EINVAL);
   1354 
   1355 	cflag = t->c_cflag;
   1356 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
   1357 	tp->t_cflag = cflag;
   1358 
   1359 	/*
   1360 	 * Block interrupts so that state will not
   1361 	 * be altered until we are done setting it up.
   1362 	 */
   1363 	s = splzs();
   1364 	bcopy(zs_init_reg, cs->cs_preg, 16);
   1365 	cs->cs_preg[12] = tmp;
   1366 	cs->cs_preg[13] = tmp >> 8;
   1367 	cs->cs_preg[9] |= ZSWR9_MASTER_IE;
   1368 	switch (cflag & CSIZE) {
   1369 	case CS5:
   1370 		tmp = ZSWR3_RX_5;
   1371 		tmp5 = ZSWR5_TX_5;
   1372 		break;
   1373 	case CS6:
   1374 		tmp = ZSWR3_RX_6;
   1375 		tmp5 = ZSWR5_TX_6;
   1376 		break;
   1377 	case CS7:
   1378 		tmp = ZSWR3_RX_7;
   1379 		tmp5 = ZSWR5_TX_7;
   1380 		break;
   1381 	case CS8:
   1382 	default:
   1383 		tmp = ZSWR3_RX_8;
   1384 		tmp5 = ZSWR5_TX_8;
   1385 		break;
   1386 	}
   1387 
   1388 	/*
   1389 	 * Output hardware flow control on the chip is horrendous: if
   1390 	 * carrier detect drops, the receiver is disabled.  Hence we
   1391 	 * can only do this when the carrier is on.
   1392 	 */
   1393 	tmp |= ZSWR3_RX_ENABLE;
   1394 	if (cflag & CCTS_OFLOW) {
   1395 		if (cs->cs_zc->zc_csr & ZSRR0_DCD)
   1396 			tmp |= ZSWR3_HFC;
   1397 		ZS_DELAY();
   1398 	}
   1399 
   1400 	cs->cs_preg[3] = tmp;
   1401 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1402 
   1403 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
   1404 	if ((cflag & PARODD) == 0)
   1405 		tmp |= ZSWR4_EVENP;
   1406 	if (cflag & PARENB)
   1407 		tmp |= ZSWR4_PARENB;
   1408 	cs->cs_preg[4] = tmp;
   1409 
   1410 	/*
   1411 	 * If nothing is being transmitted, set up new current values,
   1412 	 * else mark them as pending.
   1413 	 */
   1414 	if (cs->cs_heldchange == 0) {
   1415 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1416 			cs->cs_heldtbc = cs->cs_tbc;
   1417 			cs->cs_tbc = 0;
   1418 			cs->cs_heldchange = 1;
   1419 		} else {
   1420 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1421 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1422 		}
   1423 	}
   1424 	splx(s);
   1425 	return (0);
   1426 }
   1427 
   1428 /*
   1429  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1430  * in transmission, the change is deferred.
   1431  */
   1432 static void
   1433 zs_modem(struct zs_chanstate *cs, int onoff)
   1434 {
   1435 	int s, bis, and;
   1436 
   1437 	if (onoff) {
   1438 		bis = ZSWR5_DTR | ZSWR5_RTS;
   1439 		and = ~0;
   1440 	} else {
   1441 		bis = 0;
   1442 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
   1443 	}
   1444 	s = splzs();
   1445 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
   1446 	if (cs->cs_heldchange == 0) {
   1447 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1448 			cs->cs_heldtbc = cs->cs_tbc;
   1449 			cs->cs_tbc = 0;
   1450 			cs->cs_heldchange = 1;
   1451 		} else {
   1452 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
   1453 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1454 		}
   1455 	}
   1456 	splx(s);
   1457 }
   1458 
   1459 /*
   1460  * Write the given register set to the given zs channel in the proper order.
   1461  * The channel must not be transmitting at the time.  The receiver will
   1462  * be disabled for the time it takes to write all the registers.
   1463  */
   1464 static void
   1465 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
   1466 {
   1467 	int i;
   1468 
   1469 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1470 	ZS_DELAY();
   1471 
   1472 #if 1	/* XXX - Is this really a good idea? -gwr */
   1473 	i = zc->zc_data;		/* drain fifo */
   1474 	ZS_DELAY();
   1475 	i = zc->zc_data;
   1476 	ZS_DELAY();
   1477 	i = zc->zc_data;
   1478 	ZS_DELAY();
   1479 #endif
   1480 
   1481 	/* baud clock divisor, stop bits, parity */
   1482 	ZS_WRITE(zc, 4, reg[4]);
   1483 
   1484 	/* misc. TX/RX control bits */
   1485 	ZS_WRITE(zc, 10, reg[10]);
   1486 
   1487 	/* char size, enable (RX/TX) */
   1488 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
   1489 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
   1490 
   1491 	/* interrupt enables: TX, TX, STATUS */
   1492 	ZS_WRITE(zc, 1, reg[1]);
   1493 
   1494 	/* interrupt vector */
   1495 	ZS_WRITE(zc, 2, reg[2]);
   1496 
   1497 	/* master interrupt control */
   1498 	ZS_WRITE(zc, 9, reg[9]);
   1499 
   1500 	/* clock mode control */
   1501 	ZS_WRITE(zc, 11, reg[11]);
   1502 
   1503 	/* baud rate (lo/hi) */
   1504 	ZS_WRITE(zc, 12, reg[12]);
   1505 	ZS_WRITE(zc, 13, reg[13]);
   1506 
   1507 	/* Misc. control bits */
   1508 	ZS_WRITE(zc, 14, reg[14]);
   1509 
   1510 	/* which lines cause status interrupts */
   1511 	ZS_WRITE(zc, 15, reg[15]);
   1512 
   1513 	/* char size, enable (RX/TX)*/
   1514 	ZS_WRITE(zc, 3, reg[3]);
   1515 	ZS_WRITE(zc, 5, reg[5]);
   1516 }
   1517 
   1518 static u_char
   1519 zs_read(zc, reg)
   1520 	volatile struct zschan *zc;
   1521 	u_char reg;
   1522 {
   1523 	u_char val;
   1524 
   1525 	zc->zc_csr = reg;
   1526 	ZS_DELAY();
   1527 	val = zc->zc_csr;
   1528 	ZS_DELAY();
   1529 	return val;
   1530 }
   1531 
   1532 static u_char
   1533 zs_write(zc, reg, val)
   1534 	volatile struct zschan *zc;
   1535 	u_char reg, val;
   1536 {
   1537 	zc->zc_csr = reg;
   1538 	ZS_DELAY();
   1539 	zc->zc_csr = val;
   1540 	ZS_DELAY();
   1541 	return val;
   1542 }
   1543 
   1544 #ifdef KGDB
   1545 /*
   1546  * Get a character from the given kgdb channel.  Called at splhigh().
   1547  * XXX - Add delays, or combine with zscngetc()...
   1548  */
   1549 static int
   1550 zs_kgdb_getc(void *arg)
   1551 {
   1552 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1553 	register int c, rr0;
   1554 
   1555 	do {
   1556 		rr0 = zc->zc_csr;
   1557 		ZS_DELAY();
   1558 	} while ((rr0 & ZSRR0_RX_READY) == 0);
   1559 	c = zc->zc_data;
   1560 	ZS_DELAY();
   1561 	return (c);
   1562 }
   1563 
   1564 /*
   1565  * Put a character to the given kgdb channel.  Called at splhigh().
   1566  */
   1567 static void
   1568 zs_kgdb_putc(void *arg, int c)
   1569 {
   1570 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1571 	register int c, rr0;
   1572 
   1573 	do {
   1574 		rr0 = zc->zc_csr;
   1575 		ZS_DELAY();
   1576 	} while ((rr0 & ZSRR0_TX_READY) == 0);
   1577 	zc->zc_data = c;
   1578 	ZS_DELAY();
   1579 }
   1580 
   1581 /*
   1582  * Set up for kgdb; called at boot time before configuration.
   1583  * KGDB interrupts will be enabled later when zs0 is configured.
   1584  */
   1585 void
   1586 zs_kgdb_init()
   1587 {
   1588 	volatile struct zsdevice *addr;
   1589 	volatile struct zschan *zc;
   1590 	int unit, zs;
   1591 
   1592 	if (major(kgdb_dev) != ZSMAJOR)
   1593 		return;
   1594 	unit = minor(kgdb_dev);
   1595 	/*
   1596 	 * Unit must be 0 or 1 (zs0).
   1597 	 */
   1598 	if ((unsigned)unit >= ZS_KBD) {
   1599 		printf("zs_kgdb_init: bad minor dev %d\n", unit);
   1600 		return;
   1601 	}
   1602 	zs = unit >> 1;
   1603 	unit &= 1;
   1604 
   1605 	if (zsaddr[0] == NULL)
   1606 		panic("kbdb_attach: zs0 not yet mapped");
   1607 	addr = zsaddr[0];
   1608 
   1609 	zc = (unit == 0) ?
   1610 		&addr->zs_chan[ZS_CHAN_A] :
   1611 		&addr->zs_chan[ZS_CHAN_B];
   1612 	zs_kgdb_savedspeed = zs_getspeed(zc);
   1613 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
   1614 	    zs, unit + 'a', kgdb_rate);
   1615 	zs_reset(zc, 1, kgdb_rate);
   1616 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
   1617 }
   1618 #endif /* KGDB */
   1619