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