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