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