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