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