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