Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.28
      1 /*	$NetBSD: zs.c,v 1.28 1995/04/21 15:51:26 pk 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/z8530.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)
    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 		/* loop, turning on the device, until carrier present */
    574 		zs_modem(cs, 1);
    575 		if (cs->cs_softcar)
    576 			tp->t_state |= TS_CARR_ON;
    577 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
    578 		    tp->t_state & TS_CARR_ON)
    579 			break;
    580 		tp->t_state |= TS_WOPEN;
    581 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    582 		    ttopen, 0)) {
    583 			if (!(tp->t_state & TS_ISOPEN)) {
    584 				zs_modem(cs, 0);
    585 				tp->t_state &= ~TS_WOPEN;
    586 				ttwakeup(tp);
    587 			}
    588 			splx(s);
    589 			return error;
    590 		}
    591 	}
    592 	splx(s);
    593 	if (error == 0)
    594 		error = linesw[tp->t_line].l_open(dev, tp);
    595 	if (error)
    596 		zs_modem(cs, 0);
    597 	return (error);
    598 }
    599 
    600 /*
    601  * Close a zs serial port.
    602  */
    603 int
    604 zsclose(dev, flags, mode, p)
    605 	dev_t dev;
    606 	int flags;
    607 	int mode;
    608 	struct proc *p;
    609 {
    610 	register struct zs_chanstate *cs;
    611 	register struct tty *tp;
    612 	struct zsinfo *zi;
    613 	int unit = minor(dev), s;
    614 
    615 	zi = zscd.cd_devs[unit >> 1];
    616 	cs = &zi->zi_cs[unit & 1];
    617 	tp = cs->cs_ttyp;
    618 	linesw[tp->t_line].l_close(tp, flags);
    619 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
    620 	    (tp->t_state & TS_ISOPEN) == 0) {
    621 		zs_modem(cs, 0);
    622 		/* hold low for 1 second */
    623 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
    624 	}
    625 	if (cs->cs_creg[5] & ZSWR5_BREAK)
    626 	{
    627 		s = splzs();
    628 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    629 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    630 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    631 		splx(s);
    632 	}
    633 	ttyclose(tp);
    634 #ifdef KGDB
    635 	/* Reset the speed if we're doing kgdb on this port */
    636 	if (cs->cs_kgdb) {
    637 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    638 		(void) zsparam(tp, &tp->t_termios);
    639 	}
    640 #endif
    641 	return (0);
    642 }
    643 
    644 /*
    645  * Read/write zs serial port.
    646  */
    647 int
    648 zsread(dev, uio, flags)
    649 	dev_t dev;
    650 	struct uio *uio;
    651 	int flags;
    652 {
    653 	register struct zs_chanstate *cs;
    654 	register struct zsinfo *zi;
    655 	register struct tty *tp;
    656 	int unit = minor(dev);
    657 
    658 	zi = zscd.cd_devs[unit >> 1];
    659 	cs = &zi->zi_cs[unit & 1];
    660 	tp = cs->cs_ttyp;
    661 
    662 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    663 
    664 }
    665 
    666 int
    667 zswrite(dev, uio, flags)
    668 	dev_t dev;
    669 	struct uio *uio;
    670 	int flags;
    671 {
    672 	register struct zs_chanstate *cs;
    673 	register struct zsinfo *zi;
    674 	register struct tty *tp;
    675 	int unit = minor(dev);
    676 
    677 	zi = zscd.cd_devs[unit >> 1];
    678 	cs = &zi->zi_cs[unit & 1];
    679 	tp = cs->cs_ttyp;
    680 
    681 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    682 }
    683 
    684 struct tty *
    685 zstty(dev)
    686 	dev_t dev;
    687 {
    688 	register struct zs_chanstate *cs;
    689 	register struct zsinfo *zi;
    690 	int unit = minor(dev);
    691 
    692 	zi = zscd.cd_devs[unit >> 1];
    693 	cs = &zi->zi_cs[unit & 1];
    694 
    695 	return (cs->cs_ttyp);
    696 
    697 }
    698 
    699 /*
    700  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    701  * channels are kept in (A,B) pairs.
    702  *
    703  * Do just a little, then get out; set a software interrupt if more
    704  * work is needed.
    705  *
    706  * We deliberately ignore the vectoring Zilog gives us, and match up
    707  * only the number of `reset interrupt under service' operations, not
    708  * the order.
    709  */
    710 /* ARGSUSED */
    711 int
    712 zshard(intrarg)
    713 	void *intrarg;
    714 {
    715 	register struct zs_chanstate *a;
    716 #define	b (a + 1)
    717 	register volatile struct zschan *zc;
    718 	register int rr3, intflags = 0, v, i;
    719 	static int zsrint(struct zs_chanstate *, volatile struct zschan *);
    720 	static int zsxint(struct zs_chanstate *, volatile struct zschan *);
    721 	static int zssint(struct zs_chanstate *, volatile struct zschan *);
    722 
    723 	for (a = zslist; a != NULL; a = b->cs_next) {
    724 		rr3 = ZS_READ(a->cs_zc, 3);
    725 		if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    726 			intflags |= 2;
    727 			zc = a->cs_zc;
    728 			i = a->cs_rbput;
    729 			if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    730 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    731 				intflags |= 1;
    732 			}
    733 			if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    734 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    735 				intflags |= 1;
    736 			}
    737 			if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    738 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    739 				intflags |= 1;
    740 			}
    741 			a->cs_rbput = i;
    742 		}
    743 		if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    744 			intflags |= 2;
    745 			zc = b->cs_zc;
    746 			i = b->cs_rbput;
    747 			if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    748 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    749 				intflags |= 1;
    750 			}
    751 			if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    752 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    753 				intflags |= 1;
    754 			}
    755 			if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    756 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    757 				intflags |= 1;
    758 			}
    759 			b->cs_rbput = i;
    760 		}
    761 	}
    762 #undef b
    763 
    764 	if (intflags & 1) {
    765 #if defined(SUN4C) || defined(SUN4M)
    766 		if (cputyp==CPU_SUN4M || cputyp==CPU_SUN4C) {
    767 			/* XXX -- but this will go away when zshard moves to locore.s */
    768 			struct clockframe *p = intrarg;
    769 
    770 			if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
    771 				zsshortcuts++;
    772 				(void) spltty();
    773 				if (zshardscope) {
    774 					LED_ON;
    775 					LED_OFF;
    776 				}
    777 				return (zssoft(intrarg));
    778 			}
    779 		}
    780 #endif
    781 		ienab_bis(IE_ZSSOFT);
    782 	}
    783 	return (intflags & 2);
    784 }
    785 
    786 static int
    787 zsrint(cs, zc)
    788 	register struct zs_chanstate *cs;
    789 	register volatile struct zschan *zc;
    790 {
    791 	register int c = zc->zc_data;
    792 
    793 	if (cs->cs_conk) {
    794 		register struct conk_state *conk = &zsconk_state;
    795 
    796 		/*
    797 		 * Check here for console abort function, so that we
    798 		 * can abort even when interrupts are locking up the
    799 		 * machine.
    800 		 */
    801 		if (c == KBD_RESET) {
    802 			conk->conk_id = 1;	/* ignore next byte */
    803 			conk->conk_l1 = 0;
    804 		} else if (conk->conk_id)
    805 			conk->conk_id = 0;	/* stop ignoring bytes */
    806 		else if (c == KBD_L1)
    807 			conk->conk_l1 = 1;	/* L1 went down */
    808 		else if (c == (KBD_L1|KBD_UP))
    809 			conk->conk_l1 = 0;	/* L1 went up */
    810 		else if (c == KBD_A && conk->conk_l1) {
    811 			zsabort();
    812 			conk->conk_l1 = 0;	/* we never see the up */
    813 			goto clearit;		/* eat the A after L1-A */
    814 		}
    815 	}
    816 #ifdef KGDB
    817 	if (c == FRAME_START && cs->cs_kgdb &&
    818 	    (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
    819 		zskgdb(cs->cs_unit);
    820 		goto clearit;
    821 	}
    822 #endif
    823 	/* compose receive character and status */
    824 	c <<= 8;
    825 	c |= ZS_READ(zc, 1);
    826 
    827 	/* clear receive error & interrupt condition */
    828 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    829 	ZS_DELAY();
    830 	zc->zc_csr = ZSWR0_CLR_INTR;
    831 	ZS_DELAY();
    832 
    833 	return (ZRING_MAKE(ZRING_RINT, c));
    834 
    835 clearit:
    836 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    837 	ZS_DELAY();
    838 	zc->zc_csr = ZSWR0_CLR_INTR;
    839 	ZS_DELAY();
    840 	return (0);
    841 }
    842 
    843 static int
    844 zsxint(cs, zc)
    845 	register struct zs_chanstate *cs;
    846 	register volatile struct zschan *zc;
    847 {
    848 	register int i = cs->cs_tbc;
    849 
    850 	if (i == 0) {
    851 		zc->zc_csr = ZSWR0_RESET_TXINT;
    852 		ZS_DELAY();
    853 		zc->zc_csr = ZSWR0_CLR_INTR;
    854 		ZS_DELAY();
    855 		return (ZRING_MAKE(ZRING_XINT, 0));
    856 	}
    857 	cs->cs_tbc = i - 1;
    858 	zc->zc_data = *cs->cs_tba++;
    859 	ZS_DELAY();
    860 	zc->zc_csr = ZSWR0_CLR_INTR;
    861 	ZS_DELAY();
    862 	return (0);
    863 }
    864 
    865 static int
    866 zssint(cs, zc)
    867 	register struct zs_chanstate *cs;
    868 	register volatile struct zschan *zc;
    869 {
    870 	register int rr0;
    871 
    872 	rr0 = zc->zc_csr;
    873 	zc->zc_csr = ZSWR0_RESET_STATUS;
    874 	ZS_DELAY();
    875 	zc->zc_csr = ZSWR0_CLR_INTR;
    876 	ZS_DELAY();
    877 	/*
    878 	 * The chip's hardware flow control is, as noted in zsreg.h,
    879 	 * busted---if the DCD line goes low the chip shuts off the
    880 	 * receiver (!).  If we want hardware CTS flow control but do
    881 	 * not have it, and carrier is now on, turn HFC on; if we have
    882 	 * HFC now but carrier has gone low, turn it off.
    883 	 */
    884 	if (rr0 & ZSRR0_DCD) {
    885 		if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    886 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    887 			cs->cs_creg[3] |= ZSWR3_HFC;
    888 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    889 		}
    890 	} else {
    891 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    892 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    893 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    894 		}
    895 	}
    896 	if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
    897 #ifdef SUN4
    898 		/*
    899 		 * XXX This might not be necessary. Test and
    900 		 * delete if it isn't.
    901 		 */
    902 		if (cputyp==CPU_SUN4) {
    903 			while (zc->zc_csr & ZSRR0_BREAK)
    904 				ZS_DELAY();
    905 		}
    906 #endif
    907 		zsabort();
    908 		return (0);
    909 	}
    910 	return (ZRING_MAKE(ZRING_SINT, rr0));
    911 }
    912 
    913 zsabort()
    914 {
    915 
    916 #ifdef DDB
    917 	Debugger();
    918 #else
    919 	printf("stopping on keyboard abort\n");
    920 	callrom();
    921 #endif
    922 }
    923 
    924 #ifdef KGDB
    925 /*
    926  * KGDB framing character received: enter kernel debugger.  This probably
    927  * should time out after a few seconds to avoid hanging on spurious input.
    928  */
    929 zskgdb(unit)
    930 	int unit;
    931 {
    932 
    933 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
    934 	kgdb_connect(1);
    935 }
    936 #endif
    937 
    938 /*
    939  * Print out a ring or fifo overrun error message.
    940  */
    941 static void
    942 zsoverrun(unit, ptime, what)
    943 	int unit;
    944 	long *ptime;
    945 	char *what;
    946 {
    947 
    948 	if (*ptime != time.tv_sec) {
    949 		*ptime = time.tv_sec;
    950 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    951 		    (unit & 1) + 'a', what);
    952 	}
    953 }
    954 
    955 /*
    956  * ZS software interrupt.  Scan all channels for deferred interrupts.
    957  */
    958 int
    959 zssoft(arg)
    960 	void *arg;
    961 {
    962 	register struct zs_chanstate *cs;
    963 	register volatile struct zschan *zc;
    964 	register struct linesw *line;
    965 	register struct tty *tp;
    966 	register int get, n, c, cc, unit, s;
    967 	int	retval = 0;
    968 
    969 	for (cs = zslist; cs != NULL; cs = cs->cs_next) {
    970 		get = cs->cs_rbget;
    971 again:
    972 		n = cs->cs_rbput;	/* atomic */
    973 		if (get == n)		/* nothing more on this line */
    974 			continue;
    975 		retval = 1;
    976 		unit = cs->cs_unit;	/* set up to handle interrupts */
    977 		zc = cs->cs_zc;
    978 		tp = cs->cs_ttyp;
    979 		line = &linesw[tp->t_line];
    980 		/*
    981 		 * Compute the number of interrupts in the receive ring.
    982 		 * If the count is overlarge, we lost some events, and
    983 		 * must advance to the first valid one.  It may get
    984 		 * overwritten if more data are arriving, but this is
    985 		 * too expensive to check and gains nothing (we already
    986 		 * lost out; all we can do at this point is trade one
    987 		 * kind of loss for another).
    988 		 */
    989 		n -= get;
    990 		if (n > ZLRB_RING_SIZE) {
    991 			zsoverrun(unit, &cs->cs_rotime, "ring");
    992 			get += n - ZLRB_RING_SIZE;
    993 			n = ZLRB_RING_SIZE;
    994 		}
    995 		while (--n >= 0) {
    996 			/* race to keep ahead of incoming interrupts */
    997 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
    998 			switch (ZRING_TYPE(c)) {
    999 
   1000 			case ZRING_RINT:
   1001 				c = ZRING_VALUE(c);
   1002 				if (c & ZSRR1_DO)
   1003 					zsoverrun(unit, &cs->cs_fotime, "fifo");
   1004 				cc = c >> 8;
   1005 				if (c & ZSRR1_FE)
   1006 					cc |= TTY_FE;
   1007 				if (c & ZSRR1_PE)
   1008 					cc |= TTY_PE;
   1009 				/*
   1010 				 * this should be done through
   1011 				 * bstreams	XXX gag choke
   1012 				 */
   1013 				if (unit == ZS_KBD)
   1014 					kbd_rint(cc);
   1015 				else if (unit == ZS_MOUSE)
   1016 					ms_rint(cc);
   1017 				else
   1018 					line->l_rint(cc, tp);
   1019 				break;
   1020 
   1021 			case ZRING_XINT:
   1022 				/*
   1023 				 * Transmit done: change registers and resume,
   1024 				 * or clear BUSY.
   1025 				 */
   1026 				if (cs->cs_heldchange) {
   1027 					s = splzs();
   1028 					c = zc->zc_csr;
   1029 					ZS_DELAY();
   1030 					if ((c & ZSRR0_DCD) == 0)
   1031 						cs->cs_preg[3] &= ~ZSWR3_HFC;
   1032 					bcopy((caddr_t)cs->cs_preg,
   1033 					    (caddr_t)cs->cs_creg, 16);
   1034 					zs_loadchannelregs(zc, cs->cs_creg);
   1035 					splx(s);
   1036 					cs->cs_heldchange = 0;
   1037 					if (cs->cs_heldtbc &&
   1038 					    (tp->t_state & TS_TTSTOP) == 0) {
   1039 						cs->cs_tbc = cs->cs_heldtbc - 1;
   1040 						zc->zc_data = *cs->cs_tba++;
   1041 						ZS_DELAY();
   1042 						goto again;
   1043 					}
   1044 				}
   1045 				tp->t_state &= ~TS_BUSY;
   1046 				if (tp->t_state & TS_FLUSH)
   1047 					tp->t_state &= ~TS_FLUSH;
   1048 				else
   1049 					ndflush(&tp->t_outq,
   1050 					 cs->cs_tba - (caddr_t)tp->t_outq.c_cf);
   1051 				line->l_start(tp);
   1052 				break;
   1053 
   1054 			case ZRING_SINT:
   1055 				/*
   1056 				 * Status line change.  HFC bit is run in
   1057 				 * hardware interrupt, to avoid locking
   1058 				 * at splzs here.
   1059 				 */
   1060 				c = ZRING_VALUE(c);
   1061 				if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
   1062 					cc = (c & ZSRR0_DCD) != 0;
   1063 					if (line->l_modem(tp, cc) == 0)
   1064 						zs_modem(cs, cc);
   1065 				}
   1066 				cs->cs_rr0 = c;
   1067 				break;
   1068 
   1069 			default:
   1070 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
   1071 				    unit >> 1, (unit & 1) + 'a', c);
   1072 				break;
   1073 			}
   1074 		}
   1075 		cs->cs_rbget = get;
   1076 		goto again;
   1077 	}
   1078 	return (retval);
   1079 }
   1080 
   1081 int
   1082 zsioctl(dev, cmd, data, flag, p)
   1083 	dev_t dev;
   1084 	u_long cmd;
   1085 	caddr_t data;
   1086 	int flag;
   1087 	struct proc *p;
   1088 {
   1089 	int unit = minor(dev);
   1090 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1091 	register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
   1092 	register int error, s;
   1093 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1094 
   1095 	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
   1096 	if (error >= 0)
   1097 		return (error);
   1098 	error = ttioctl(tp, cmd, data, flag, p);
   1099 	if (error >= 0)
   1100 		return (error);
   1101 
   1102 	switch (cmd) {
   1103 	case TIOCSBRK:
   1104 		s = splzs();
   1105 		cs->cs_preg[5] |= ZSWR5_BREAK;
   1106 		cs->cs_creg[5] |= ZSWR5_BREAK;
   1107 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1108 		splx(s);
   1109 		break;
   1110 	case TIOCCBRK:
   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 TIOCGFLAGS: {
   1118 		int bits = 0;
   1119 
   1120 		if (cs->cs_softcar)
   1121 			bits |= TIOCFLAG_SOFTCAR;
   1122 		if (cs->cs_creg[15] & ZSWR15_DCD_IE)
   1123 			bits |= TIOCFLAG_CLOCAL;
   1124 		if (cs->cs_creg[3] & ZSWR3_HFC)
   1125 			bits |= TIOCFLAG_CRTSCTS;
   1126 		*(int *)data = bits;
   1127 		break;
   1128 	}
   1129 	case TIOCSFLAGS: {
   1130 		int userbits, driverbits = 0;
   1131 
   1132 		error = suser(p->p_ucred, &p->p_acflag);
   1133 		if (error != 0)
   1134 			return (EPERM);
   1135 
   1136 		userbits = *(int *)data;
   1137 
   1138 		/*
   1139 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
   1140 		 # defaulting to software flow control.
   1141 		 */
   1142 		if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
   1143 			return(EINVAL);
   1144 		if (userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
   1145 			return(ENXIO);
   1146 
   1147 		s = splzs();
   1148 		if ((userbits & TIOCFLAG_SOFTCAR) || (tp == zs_ctty)) {
   1149 			cs->cs_softcar = 1;	/* turn on softcar */
   1150 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
   1151 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
   1152 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1153 		} else if (userbits & TIOCFLAG_CLOCAL) {
   1154 			cs->cs_softcar = 0; 	/* turn off softcar */
   1155 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
   1156 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
   1157 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1158 			tp->t_termios.c_cflag |= CLOCAL;
   1159 		}
   1160 		if (userbits & TIOCFLAG_CRTSCTS) {
   1161 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
   1162 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
   1163 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1164 			cs->cs_preg[3] |= ZSWR3_HFC;
   1165 			cs->cs_creg[3] |= ZSWR3_HFC;
   1166 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1167 			tp->t_termios.c_cflag |= CRTSCTS;
   1168 		} else {
   1169 			/* no mdmbuf, so we must want software flow control */
   1170 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
   1171 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
   1172 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1173 			cs->cs_preg[3] &= ~ZSWR3_HFC;
   1174 			cs->cs_creg[3] &= ~ZSWR3_HFC;
   1175 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1176 			tp->t_termios.c_cflag &= ~CRTSCTS;
   1177 		}
   1178 		splx(s);
   1179 		break;
   1180 	}
   1181 	case TIOCSDTR:
   1182 		zs_modem(cs, 1);
   1183 		break;
   1184 	case TIOCCDTR:
   1185 		zs_modem(cs, 0);
   1186 		break;
   1187 	case TIOCMSET:
   1188 	case TIOCMBIS:
   1189 	case TIOCMBIC:
   1190 	case TIOCMGET:
   1191 	default:
   1192 		return (ENOTTY);
   1193 	}
   1194 	return (0);
   1195 }
   1196 
   1197 /*
   1198  * Start or restart transmission.
   1199  */
   1200 static void
   1201 zsstart(tp)
   1202 	register struct tty *tp;
   1203 {
   1204 	register struct zs_chanstate *cs;
   1205 	register int s, nch;
   1206 	int unit = minor(tp->t_dev);
   1207 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1208 
   1209 	cs = &zi->zi_cs[unit & 1];
   1210 	s = spltty();
   1211 
   1212 	/*
   1213 	 * If currently active or delaying, no need to do anything.
   1214 	 */
   1215 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
   1216 		goto out;
   1217 
   1218 	/*
   1219 	 * If there are sleepers, and output has drained below low
   1220 	 * water mark, awaken.
   1221 	 */
   1222 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1223 		if (tp->t_state & TS_ASLEEP) {
   1224 			tp->t_state &= ~TS_ASLEEP;
   1225 			wakeup((caddr_t)&tp->t_outq);
   1226 		}
   1227 		selwakeup(&tp->t_wsel);
   1228 	}
   1229 
   1230 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1231 	if (nch) {
   1232 		register char *p = tp->t_outq.c_cf;
   1233 
   1234 		/* mark busy, enable tx done interrupts, & send first byte */
   1235 		tp->t_state |= TS_BUSY;
   1236 		(void) splzs();
   1237 		cs->cs_preg[1] |= ZSWR1_TIE;
   1238 		cs->cs_creg[1] |= ZSWR1_TIE;
   1239 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1240 		cs->cs_zc->zc_data = *p;
   1241 		ZS_DELAY();
   1242 		cs->cs_tba = p + 1;
   1243 		cs->cs_tbc = nch - 1;
   1244 	} else {
   1245 		/*
   1246 		 * Nothing to send, turn off transmit done interrupts.
   1247 		 * This is useful if something is doing polled output.
   1248 		 */
   1249 		(void) splzs();
   1250 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1251 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1252 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1253 	}
   1254 out:
   1255 	splx(s);
   1256 }
   1257 
   1258 /*
   1259  * Stop output, e.g., for ^S or output flush.
   1260  */
   1261 void
   1262 zsstop(tp, flag)
   1263 	register struct tty *tp;
   1264 	int flag;
   1265 {
   1266 	register struct zs_chanstate *cs;
   1267 	register int s, unit = minor(tp->t_dev);
   1268 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1269 
   1270 	cs = &zi->zi_cs[unit & 1];
   1271 	s = splzs();
   1272 	if (tp->t_state & TS_BUSY) {
   1273 		/*
   1274 		 * Device is transmitting; must stop it.
   1275 		 */
   1276 		cs->cs_tbc = 0;
   1277 		if ((tp->t_state & TS_TTSTOP) == 0)
   1278 			tp->t_state |= TS_FLUSH;
   1279 	}
   1280 	splx(s);
   1281 }
   1282 
   1283 /*
   1284  * Set ZS tty parameters from termios.
   1285  *
   1286  * This routine makes use of the fact that only registers
   1287  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1288  */
   1289 static int
   1290 zsparam(tp, t)
   1291 	register struct tty *tp;
   1292 	register struct termios *t;
   1293 {
   1294 	int unit = minor(tp->t_dev);
   1295 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1296 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1297 	register int tmp, tmp5, cflag, s;
   1298 
   1299 	/*
   1300 	 * Because PCLK is only run at 4.9 MHz, the fastest we
   1301 	 * can go is 51200 baud (this corresponds to TC=1).
   1302 	 * This is somewhat unfortunate as there is no real
   1303 	 * reason we should not be able to handle higher rates.
   1304 	 */
   1305 	tmp = t->c_ospeed;
   1306 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
   1307 		return (EINVAL);
   1308 	if (tmp == 0) {
   1309 		/* stty 0 => drop DTR and RTS */
   1310 		zs_modem(cs, 0);
   1311 		return (0);
   1312 	}
   1313 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
   1314 	if (tmp < 2)
   1315 		return (EINVAL);
   1316 
   1317 	cflag = t->c_cflag;
   1318 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
   1319 	tp->t_cflag = cflag;
   1320 
   1321 	/*
   1322 	 * Block interrupts so that state will not
   1323 	 * be altered until we are done setting it up.
   1324 	 */
   1325 	s = splzs();
   1326 	cs->cs_preg[12] = tmp;
   1327 	cs->cs_preg[13] = tmp >> 8;
   1328 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1329 	switch (cflag & CSIZE) {
   1330 	case CS5:
   1331 		tmp = ZSWR3_RX_5;
   1332 		tmp5 = ZSWR5_TX_5;
   1333 		break;
   1334 	case CS6:
   1335 		tmp = ZSWR3_RX_6;
   1336 		tmp5 = ZSWR5_TX_6;
   1337 		break;
   1338 	case CS7:
   1339 		tmp = ZSWR3_RX_7;
   1340 		tmp5 = ZSWR5_TX_7;
   1341 		break;
   1342 	case CS8:
   1343 	default:
   1344 		tmp = ZSWR3_RX_8;
   1345 		tmp5 = ZSWR5_TX_8;
   1346 		break;
   1347 	}
   1348 
   1349 	/*
   1350 	 * Output hardware flow control on the chip is horrendous: if
   1351 	 * carrier detect drops, the receiver is disabled.  Hence we
   1352 	 * can only do this when the carrier is on.
   1353 	 */
   1354 	if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
   1355 		tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
   1356 	else
   1357 		tmp |= ZSWR3_RX_ENABLE;
   1358 	cs->cs_preg[3] = tmp;
   1359 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1360 
   1361 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
   1362 	if ((cflag & PARODD) == 0)
   1363 		tmp |= ZSWR4_EVENP;
   1364 	if (cflag & PARENB)
   1365 		tmp |= ZSWR4_PARENB;
   1366 	cs->cs_preg[4] = tmp;
   1367 	cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
   1368 	cs->cs_preg[10] = ZSWR10_NRZ;
   1369 	cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
   1370 	cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
   1371 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1372 
   1373 	/*
   1374 	 * If nothing is being transmitted, set up new current values,
   1375 	 * else mark them as pending.
   1376 	 */
   1377 	if (cs->cs_heldchange == 0) {
   1378 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1379 			cs->cs_heldtbc = cs->cs_tbc;
   1380 			cs->cs_tbc = 0;
   1381 			cs->cs_heldchange = 1;
   1382 		} else {
   1383 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1384 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1385 		}
   1386 	}
   1387 	splx(s);
   1388 	return (0);
   1389 }
   1390 
   1391 /*
   1392  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1393  * in transmission, the change is deferred.
   1394  */
   1395 static void
   1396 zs_modem(cs, onoff)
   1397 	struct zs_chanstate *cs;
   1398 	int onoff;
   1399 {
   1400 	int s, bis, and;
   1401 
   1402 	if (onoff) {
   1403 		bis = ZSWR5_DTR | ZSWR5_RTS;
   1404 		and = ~0;
   1405 	} else {
   1406 		bis = 0;
   1407 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
   1408 	}
   1409 	s = splzs();
   1410 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
   1411 	if (cs->cs_heldchange == 0) {
   1412 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1413 			cs->cs_heldtbc = cs->cs_tbc;
   1414 			cs->cs_tbc = 0;
   1415 			cs->cs_heldchange = 1;
   1416 		} else {
   1417 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
   1418 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1419 		}
   1420 	}
   1421 	splx(s);
   1422 }
   1423 
   1424 /*
   1425  * Write the given register set to the given zs channel in the proper order.
   1426  * The channel must not be transmitting at the time.  The receiver will
   1427  * be disabled for the time it takes to write all the registers.
   1428  */
   1429 static void
   1430 zs_loadchannelregs(zc, reg)
   1431 	volatile struct zschan *zc;
   1432 	u_char *reg;
   1433 {
   1434 	int i;
   1435 
   1436 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1437 	ZS_DELAY();
   1438 	i = zc->zc_data;		/* drain fifo */
   1439 	ZS_DELAY();
   1440 	i = zc->zc_data;
   1441 	ZS_DELAY();
   1442 	i = zc->zc_data;
   1443 	ZS_DELAY();
   1444 	ZS_WRITE(zc, 4, reg[4]);
   1445 	ZS_WRITE(zc, 10, reg[10]);
   1446 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
   1447 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
   1448 	ZS_WRITE(zc, 1, reg[1]);
   1449 	ZS_WRITE(zc, 9, reg[9]);
   1450 	ZS_WRITE(zc, 11, reg[11]);
   1451 	ZS_WRITE(zc, 12, reg[12]);
   1452 	ZS_WRITE(zc, 13, reg[13]);
   1453 	ZS_WRITE(zc, 14, reg[14]);
   1454 	ZS_WRITE(zc, 15, reg[15]);
   1455 	ZS_WRITE(zc, 3, reg[3]);
   1456 	ZS_WRITE(zc, 5, reg[5]);
   1457 }
   1458 
   1459 #ifdef KGDB
   1460 /*
   1461  * Get a character from the given kgdb channel.  Called at splhigh().
   1462  */
   1463 static int
   1464 zs_kgdb_getc(arg)
   1465 	void *arg;
   1466 {
   1467 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1468 
   1469 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
   1470 		ZS_DELAY();
   1471 	return (zc->zc_data);
   1472 }
   1473 
   1474 /*
   1475  * Put a character to the given kgdb channel.  Called at splhigh().
   1476  */
   1477 static void
   1478 zs_kgdb_putc(arg, c)
   1479 	void *arg;
   1480 	int c;
   1481 {
   1482 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1483 
   1484 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
   1485 		ZS_DELAY();
   1486 	zc->zc_data = c;
   1487 	ZS_DELAY();
   1488 }
   1489 
   1490 /*
   1491  * Set up for kgdb; called at boot time before configuration.
   1492  * KGDB interrupts will be enabled later when zs0 is configured.
   1493  */
   1494 void
   1495 zs_kgdb_init()
   1496 {
   1497 	volatile struct zsdevice *addr;
   1498 	volatile struct zschan *zc;
   1499 	int unit, zs;
   1500 
   1501 	if (major(kgdb_dev) != ZSMAJOR)
   1502 		return;
   1503 	unit = minor(kgdb_dev);
   1504 	/*
   1505 	 * Unit must be 0 or 1 (zs0).
   1506 	 */
   1507 	if ((unsigned)unit >= ZS_KBD) {
   1508 		printf("zs_kgdb_init: bad minor dev %d\n", unit);
   1509 		return;
   1510 	}
   1511 	zs = unit >> 1;
   1512 	if ((addr = zsaddr[zs]) == NULL)
   1513 		addr = zsaddr[zs] = findzs(zs);
   1514 	unit &= 1;
   1515 	zc = unit == 0 ? &addr->zs_chan[ZS_CHAN_A] : &addr->zs_chan[ZS_CHAN_B];
   1516 	zs_kgdb_savedspeed = zs_getspeed(zc);
   1517 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
   1518 	    zs, unit + 'a', kgdb_rate);
   1519 	zs_reset(zc, 1, kgdb_rate);
   1520 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
   1521 }
   1522 #endif /* KGDB */
   1523