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