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