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