Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.7
      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.7 1994/05/30 06:22:36 glass 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 	if (cs->cs_zc == zs_conschan) {
    255 		/* This unit is the console. */
    256 		cs->cs_consio = 1;
    257 		cs->cs_brkabort = 1;
    258 		cs->cs_softcar = 1;
    259 	} else {
    260 		/* Can not run kgdb on the console? */
    261 #ifdef KGDB
    262 		zs_checkkgdb(unit, cs, tp);
    263 #endif
    264 	}
    265 	if (unit == ZS_KBD) {
    266 		/*
    267 		 * Keyboard: tell /dev/kbd driver how to talk to us.
    268 		 */
    269 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    270 		tp->t_cflag = CS8;
    271 		kbd_serial(tp, zsiopen, zsiclose);
    272 		cs->cs_conk = 1;		/* do L1-A processing */
    273 	}
    274 	unit++;
    275 	cs++;
    276 	tp = zs_tty[unit];
    277 
    278 	cs->cs_unit = unit;
    279 	cs->cs_zc = &addr->zs_chan[CHAN_B];
    280 	cs->cs_speed = zs_getspeed(cs->cs_zc);
    281 #ifdef	DEBUG
    282 	mon_printf("zs%db speed %d\n", zs, cs->cs_speed);
    283 #endif
    284 	cs->cs_softcar = softcar & 2;
    285 #if 0
    286 	/* XXX - Drop carrier here? -gwr */
    287 	zs_modem(cs, cs->cs_softcar ? 1 : 0);
    288 #endif
    289 	cs->cs_ttyp = tp;
    290 	tp->t_dev = makedev(ZSMAJOR, unit);
    291 	tp->t_oproc = zsstart;
    292 	tp->t_param = zsparam;
    293 	if (cs->cs_zc == zs_conschan) {
    294 		/* This unit is the console. */
    295 		cs->cs_consio = 1;
    296 		cs->cs_brkabort = 1;
    297 		cs->cs_softcar = 1;
    298 	} else {
    299 		/* Can not run kgdb on the console? */
    300 #ifdef KGDB
    301 		zs_checkkgdb(unit, cs, tp);
    302 #endif
    303 	}
    304 	if (unit == ZS_MOUSE) {
    305 		/*
    306 		 * Mouse: tell /dev/mouse driver how to talk to us.
    307 		 */
    308 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    309 		tp->t_cflag = CS8;
    310 		ms_serial(tp, zsiopen, zsiclose);
    311 	}
    312 }
    313 
    314 /*
    315  * Put a channel in a known state.  Interrupts may be left disabled
    316  * or enabled, as desired.
    317  */
    318 static void
    319 zs_reset(zc, inten, speed)
    320 	volatile struct zschan *zc;
    321 	int inten, speed;
    322 {
    323 	int tconst;
    324 	static u_char reg[16] = {
    325 		0,
    326 		0,
    327 		0,
    328 		ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    329 		ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    330 		ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    331 		0,
    332 		0,
    333 		0,
    334 		0,
    335 		ZSWR10_NRZ,
    336 		ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    337 		0,
    338 		0,
    339 		ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
    340 		ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    341 	};
    342 
    343 	reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
    344 	tconst = BPS_TO_TCONST(PCLK / 16, speed);
    345 	reg[12] = tconst;
    346 	reg[13] = tconst >> 8;
    347 	zs_loadchannelregs(zc, reg);
    348 }
    349 
    350 /*
    351  * Console support
    352  */
    353 
    354 /*
    355  * Used by the kd driver to find out if it can work.
    356  */
    357 int
    358 zscnprobe_kbd()
    359 {
    360 	if (zs1_va == NULL) {
    361 		mon_printf("zscnprobe_kbd: zs1 not yet mapped\n");
    362 		return CN_DEAD;
    363 	}
    364 	zsaddr[1] = (struct zsdevice *)zs1_va;
    365 	return CN_INTERNAL;
    366 }
    367 
    368 /*
    369  * This is the console probe routine for ttya and ttyb.
    370  */
    371 static int
    372 zscnprobe(struct consdev *cn, int unit)
    373 {
    374 	int maj, eeCons;
    375 
    376 	if (zs0_va == NULL) {
    377 		mon_printf("zscnprobe: zs0 not yet mapped\n");
    378 		cn->cn_pri = CN_DEAD;
    379 		return 0;
    380 	}
    381 	zsaddr[0] = (struct zsdevice *)zs0_va;
    382 	/* XXX - Also try to make sure it exists? */
    383 
    384 	/* locate the major number */
    385 	for (maj = 0; maj < nchrdev; maj++)
    386 		if (cdevsw[maj].d_open == zsopen)
    387 			break;
    388 
    389 	cn->cn_dev = makedev(maj, unit);
    390 
    391 	/* Use EEPROM console setting to decide "remote" console. */
    392 	if (eeprom_va == NULL) {
    393 		mon_printf("zscnprobe: eeprom not yet mapped\n");
    394 		eeCons = -1;
    395 	} else {
    396 		eeCons = ((struct eeprom *)eeprom_va)->eeConsole;
    397 	}
    398 
    399 	/* Hack: EE_CONS_TTYA + 1 == EE_CONS_TTYB */
    400 	if (eeCons == (EE_CONS_TTYA + unit)) {
    401 		cn->cn_pri = CN_REMOTE;
    402 	} else {
    403 		cn->cn_pri = CN_NORMAL;
    404 	}
    405 	return (0);
    406 }
    407 
    408 /* This is the constab entry for TTYA. */
    409 int
    410 zscnprobe_a(struct consdev *cn)
    411 {
    412 	return (zscnprobe(cn, 0));
    413 }
    414 
    415 /* This is the constab entry for TTYB. */
    416 int
    417 zscnprobe_b(struct consdev *cn)
    418 {
    419 	return (zscnprobe(cn, 1));
    420 }
    421 
    422 /* Attach as console.  Also set zs_conschan */
    423 int
    424 zscninit(struct consdev *cn)
    425 {
    426 	int unit;
    427 	volatile struct zsdevice *addr;
    428 
    429 	unit = minor(cn->cn_dev) & 1;
    430 	addr = (struct zsdevice *)zs0_va;
    431 	zs_conschan = ((unit == 0) ?
    432 				   &addr->zs_chan[CHAN_A] :
    433 				   &addr->zs_chan[CHAN_B] );
    434 
    435 	mon_printf("console on zs0 (tty%c)\n", unit + 'a');
    436 }
    437 
    438 
    439 /*
    440  * Polled console input putchar.
    441  */
    442 int
    443 zscngetc(dev)
    444 	dev_t dev;
    445 {
    446 	register volatile struct zschan *zc = zs_conschan;
    447 	register int s, c;
    448 
    449 	if (zc == NULL)
    450 		return (0);
    451 
    452 	s = splhigh();
    453 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
    454 		ZS_DELAY();
    455 	ZS_DELAY();
    456 	c = zc->zc_data;
    457 	splx(s);
    458 	return (c);
    459 }
    460 
    461 /*
    462  * Polled console output putchar.
    463  */
    464 int
    465 zscnputc(dev, c)
    466 	dev_t dev;
    467 	int c;
    468 {
    469 	register volatile struct zschan *zc = zs_conschan;
    470 	register int s;
    471 
    472 	if (zc == NULL) {
    473 		s = splhigh();
    474 		mon_putchar(c);
    475 		splx(s);
    476 		return (0);
    477 	}
    478 
    479 	s = splhigh();
    480 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
    481 		ZS_DELAY();
    482 	ZS_DELAY();
    483 	zc->zc_data = c;
    484 	ZS_DELAY();
    485 	splx(s);
    486 }
    487 
    488 #ifdef KGDB
    489 /*
    490  * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
    491  * Pick up the current speed and character size and restore the original
    492  * speed.
    493  */
    494 static void
    495 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
    496 {
    497 
    498 	if (kgdb_dev == makedev(ZSMAJOR, unit)) {
    499 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    500 		tp->t_cflag = CS8;
    501 		cs->cs_kgdb = 1;
    502 		cs->cs_speed = zs_kgdb_savedspeed;
    503 		(void) zsparam(tp, &tp->t_termios);
    504 	}
    505 }
    506 #endif
    507 
    508 /*
    509  * Compute the current baud rate given a ZSCC channel.
    510  */
    511 static int
    512 zs_getspeed(zc)
    513 	register volatile struct zschan *zc;
    514 {
    515 	register int tconst;
    516 
    517 	tconst = ZS_READ(zc, 12);
    518 	tconst |= ZS_READ(zc, 13) << 8;
    519 	return (TCONST_TO_BPS(PCLK / 16, tconst));
    520 }
    521 
    522 
    523 /*
    524  * Do an internal open.
    525  */
    526 static void
    527 zsiopen(struct tty *tp)
    528 {
    529 
    530 	(void) zsparam(tp, &tp->t_termios);
    531 	ttsetwater(tp);
    532 	tp->t_state = TS_ISOPEN | TS_CARR_ON;
    533 }
    534 
    535 /*
    536  * Do an internal close.  Eventually we should shut off the chip when both
    537  * ports on it are closed.
    538  */
    539 static void
    540 zsiclose(struct tty *tp)
    541 {
    542 
    543 	ttylclose(tp, 0);	/* ??? */
    544 	ttyclose(tp);		/* ??? */
    545 	tp->t_state = 0;
    546 }
    547 
    548 
    549 /*
    550  * Open a zs serial port.  This interface may not be used to open
    551  * the keyboard and mouse ports. (XXX)
    552  */
    553 int
    554 zsopen(dev_t dev, int flags, int mode, struct proc *p)
    555 {
    556 	register struct tty *tp;
    557 	register struct zs_chanstate *cs;
    558 	struct zsinfo *zi;
    559 	int unit = minor(dev), zs = unit >> 1, error, s;
    560 
    561 #ifdef	DEBUG
    562 	mon_printf("zs_open\n");
    563 #endif
    564 	if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
    565 	    unit == ZS_KBD || unit == ZS_MOUSE)
    566 		return (ENXIO);
    567 	cs = &zi->zi_cs[unit & 1];
    568 #if 0
    569 	/* The kd driver avoids the need for this hack. */
    570 	if (cs->cs_consio)
    571 		return (ENXIO);		/* ??? */
    572 #endif
    573 	tp = cs->cs_ttyp;
    574 	s = spltty();
    575 	if ((tp->t_state & TS_ISOPEN) == 0) {
    576 		ttychars(tp);
    577 		if (tp->t_ispeed == 0) {
    578 			tp->t_iflag = TTYDEF_IFLAG;
    579 			tp->t_oflag = TTYDEF_OFLAG;
    580 			tp->t_cflag = TTYDEF_CFLAG;
    581 			tp->t_lflag = TTYDEF_LFLAG;
    582 			tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    583 		}
    584 		(void) zsparam(tp, &tp->t_termios);
    585 		ttsetwater(tp);
    586 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    587 		splx(s);
    588 		return (EBUSY);
    589 	}
    590 	error = 0;
    591 #ifdef	DEBUG
    592 	mon_printf("wait for carrier...\n");
    593 #endif
    594 	for (;;) {
    595 		/* loop, turning on the device, until carrier present */
    596 		zs_modem(cs, 1);
    597 		/* May never get status intr if carrier already on. -gwr */
    598 		if (cs->cs_zc->zc_csr & ZSRR0_DCD)
    599 			tp->t_state |= TS_CARR_ON;
    600 		if (cs->cs_softcar)
    601 			tp->t_state |= TS_CARR_ON;
    602 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
    603 		    tp->t_state & TS_CARR_ON)
    604 			break;
    605 		tp->t_state |= TS_WOPEN;
    606 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    607 		    ttopen, 0))
    608 			break;
    609 	}
    610 #ifdef	DEBUG
    611 	mon_printf("...carrier %s\n",
    612 			   (tp->t_state & TS_CARR_ON) ? "on" : "off");
    613 #endif
    614 	splx(s);
    615 	if (error == 0)
    616 		error = linesw[tp->t_line].l_open(dev, tp);
    617 	if (error)
    618 		zs_modem(cs, 0);
    619 	return (error);
    620 }
    621 
    622 /*
    623  * Close a zs serial port.
    624  */
    625 int
    626 zsclose(dev_t dev, int flags, int mode, struct proc *p)
    627 {
    628 	register struct zs_chanstate *cs;
    629 	register struct tty *tp;
    630 	struct zsinfo *zi;
    631 	int unit = minor(dev), s;
    632 
    633 #ifdef	DEBUG
    634 	mon_printf("zs_close\n");
    635 #endif
    636 	zi = zscd.cd_devs[unit >> 1];
    637 	cs = &zi->zi_cs[unit & 1];
    638 	tp = cs->cs_ttyp;
    639 	linesw[tp->t_line].l_close(tp, flags);
    640 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
    641 	    (tp->t_state & TS_ISOPEN) == 0) {
    642 		zs_modem(cs, 0);
    643 		/* hold low for 1 second */
    644 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
    645 	}
    646 	if (cs->cs_creg[5] & ZSWR5_BREAK)
    647 	{
    648 		s = splzs();
    649 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    650 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    651 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    652 		splx(s);
    653 	}
    654 	ttyclose(tp);
    655 #ifdef KGDB
    656 	/* Reset the speed if we're doing kgdb on this port */
    657 	if (cs->cs_kgdb) {
    658 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    659 		(void) zsparam(tp, &tp->t_termios);
    660 	}
    661 #endif
    662 	return (0);
    663 }
    664 
    665 /*
    666  * Read/write zs serial port.
    667  */
    668 int
    669 zsread(dev_t dev, struct uio *uio, int flags)
    670 {
    671 	register struct tty *tp = zs_tty[minor(dev)];
    672 
    673 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    674 }
    675 
    676 int
    677 zswrite(dev_t dev, struct uio *uio, int flags)
    678 {
    679 	register struct tty *tp = zs_tty[minor(dev)];
    680 
    681 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    682 }
    683 
    684 /*
    685  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    686  * channels are kept in (A,B) pairs.
    687  *
    688  * Do just a little, then get out; set a software interrupt if more
    689  * work is needed.
    690  *
    691  * We deliberately ignore the vectoring Zilog gives us, and match up
    692  * only the number of `reset interrupt under service' operations, not
    693  * the order.
    694  */
    695 /* ARGSUSED */
    696 int
    697 zshard(int intrarg)
    698 {
    699 	register struct zs_chanstate *a;
    700 #define	b (a + 1)
    701 	register volatile struct zschan *zc;
    702 	register int rr3, intflags = 0, v, i;
    703 	static int zsrint(struct zs_chanstate *, volatile struct zschan *);
    704 	static int zsxint(struct zs_chanstate *, volatile struct zschan *);
    705 	static int zssint(struct zs_chanstate *, volatile struct zschan *);
    706 
    707 	for (a = zslist; a != NULL; a = b->cs_next) {
    708 		rr3 = ZS_READ(a->cs_zc, 3);
    709 
    710 		/* XXX - This should loop to empty the on-chip fifo. */
    711 		if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    712 			intflags |= 2;
    713 			zc = a->cs_zc;
    714 			i = a->cs_rbput;
    715 			if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    716 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    717 				intflags |= 1;
    718 			}
    719 			if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    720 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    721 				intflags |= 1;
    722 			}
    723 			if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    724 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    725 				intflags |= 1;
    726 			}
    727 			a->cs_rbput = i;
    728 		}
    729 
    730 		/* XXX - This should loop to empty the on-chip fifo. */
    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 		/* Wait for end of break to avoid PROM abort. */
    853 		while (zc->zc_csr & ZSRR0_BREAK)
    854 			ZS_DELAY();
    855 		zsabort();
    856 		return (0);
    857 	}
    858 	return (ZRING_MAKE(ZRING_SINT, rr0));
    859 }
    860 
    861 zsabort()
    862 {
    863 #ifdef DDB
    864 	Debugger();
    865 #else
    866 	printf("stopping on keyboard abort\n");
    867 	sun3_rom_abort();
    868 #endif
    869 }
    870 
    871 #ifdef KGDB
    872 /*
    873  * KGDB framing character received: enter kernel debugger.  This probably
    874  * should time out after a few seconds to avoid hanging on spurious input.
    875  */
    876 zskgdb(int unit)
    877 {
    878 
    879 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
    880 	kgdb_connect(1);
    881 }
    882 #endif
    883 
    884 /*
    885  * Print out a ring or fifo overrun error message.
    886  */
    887 static void
    888 zsoverrun(int unit, long *ptime, char *what)
    889 {
    890 
    891 	if (*ptime != time.tv_sec) {
    892 		*ptime = time.tv_sec;
    893 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    894 		    (unit & 1) + 'a', what);
    895 	}
    896 }
    897 
    898 /*
    899  * ZS software interrupt.  Scan all channels for deferred interrupts.
    900  */
    901 int
    902 zssoft(int arg)
    903 {
    904 	register struct zs_chanstate *cs;
    905 	register volatile struct zschan *zc;
    906 	register struct linesw *line;
    907 	register struct tty *tp;
    908 	register int get, n, c, cc, unit, s;
    909 
    910 	isr_soft_clear(ZSSOFT_PRI);
    911 
    912 	for (cs = zslist; cs != NULL; cs = cs->cs_next) {
    913 		get = cs->cs_rbget;
    914 again:
    915 		n = cs->cs_rbput;	/* atomic */
    916 		if (get == n)		/* nothing more on this line */
    917 			continue;
    918 		unit = cs->cs_unit;	/* set up to handle interrupts */
    919 		zc = cs->cs_zc;
    920 		tp = cs->cs_ttyp;
    921 		line = &linesw[tp->t_line];
    922 		/*
    923 		 * Compute the number of interrupts in the receive ring.
    924 		 * If the count is overlarge, we lost some events, and
    925 		 * must advance to the first valid one.  It may get
    926 		 * overwritten if more data are arriving, but this is
    927 		 * too expensive to check and gains nothing (we already
    928 		 * lost out; all we can do at this point is trade one
    929 		 * kind of loss for another).
    930 		 */
    931 		n -= get;
    932 		if (n > ZLRB_RING_SIZE) {
    933 			zsoverrun(unit, &cs->cs_rotime, "ring");
    934 			get += n - ZLRB_RING_SIZE;
    935 			n = ZLRB_RING_SIZE;
    936 		}
    937 		while (--n >= 0) {
    938 			/* race to keep ahead of incoming interrupts */
    939 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
    940 			switch (ZRING_TYPE(c)) {
    941 
    942 			case ZRING_RINT:
    943 				c = ZRING_VALUE(c);
    944 				if (c & ZSRR1_DO)
    945 					zsoverrun(unit, &cs->cs_fotime, "fifo");
    946 				cc = c >> 8;
    947 				if (c & ZSRR1_FE)
    948 					cc |= TTY_FE;
    949 				if (c & ZSRR1_PE)
    950 					cc |= TTY_PE;
    951 				/*
    952 				 * this should be done through
    953 				 * bstreams	XXX gag choke
    954 				 */
    955 				if (unit == ZS_KBD)
    956 					kbd_rint(cc);
    957 				else if (unit == ZS_MOUSE)
    958 					ms_rint(cc);
    959 				else
    960 					line->l_rint(cc, tp);
    961 				break;
    962 
    963 			case ZRING_XINT:
    964 				/*
    965 				 * Transmit done: change registers and resume,
    966 				 * or clear BUSY.
    967 				 */
    968 				if (cs->cs_heldchange) {
    969 					s = splzs();
    970 					c = zc->zc_csr;
    971 					if ((c & ZSRR0_DCD) == 0)
    972 						cs->cs_preg[3] &= ~ZSWR3_HFC;
    973 					bcopy((caddr_t)cs->cs_preg,
    974 					    (caddr_t)cs->cs_creg, 16);
    975 					zs_loadchannelregs(zc, cs->cs_creg);
    976 					splx(s);
    977 					cs->cs_heldchange = 0;
    978 					if (cs->cs_heldtbc &&
    979 					    (tp->t_state & TS_TTSTOP) == 0) {
    980 						cs->cs_tbc = cs->cs_heldtbc - 1;
    981 						zc->zc_data = *cs->cs_tba++;
    982 						goto again;
    983 					}
    984 				}
    985 				tp->t_state &= ~TS_BUSY;
    986 				if (tp->t_state & TS_FLUSH)
    987 					tp->t_state &= ~TS_FLUSH;
    988 				else
    989 					ndflush(&tp->t_outq, cs->cs_tba -
    990 						(caddr_t) tp->t_outq.c_cf);
    991 				line->l_start(tp);
    992 				break;
    993 
    994 			case ZRING_SINT:
    995 				/*
    996 				 * Status line change.  HFC bit is run in
    997 				 * hardware interrupt, to avoid locking
    998 				 * at splzs here.
    999 				 */
   1000 				c = ZRING_VALUE(c);
   1001 				if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
   1002 					cc = (c & ZSRR0_DCD) != 0;
   1003 					if (line->l_modem(tp, cc) == 0)
   1004 						zs_modem(cs, cc);
   1005 				}
   1006 				cs->cs_rr0 = c;
   1007 				break;
   1008 
   1009 			default:
   1010 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
   1011 				    unit >> 1, (unit & 1) + 'a', c);
   1012 				break;
   1013 			}
   1014 		}
   1015 		cs->cs_rbget = get;
   1016 		goto again;
   1017 	}
   1018 	return (1);
   1019 }
   1020 
   1021 int
   1022 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
   1023 {
   1024 	int unit = minor(dev);
   1025 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1026 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1027 	register struct tty *tp = cs->cs_ttyp;
   1028 	register int error, s;
   1029 
   1030 	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
   1031 	if (error >= 0)
   1032 		return (error);
   1033 	error = ttioctl(tp, cmd, data, flag, p);
   1034 	if (error >= 0)
   1035 		return (error);
   1036 
   1037 	switch (cmd) {
   1038 
   1039 	case TIOCSBRK:
   1040 		{
   1041 			s = splzs();
   1042 			cs->cs_preg[5] |= ZSWR5_BREAK;
   1043 			cs->cs_creg[5] |= ZSWR5_BREAK;
   1044 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1045 			splx(s);
   1046 			break;
   1047 		}
   1048 
   1049 	case TIOCCBRK:
   1050 		{
   1051 			s = splzs();
   1052 			cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1053 			cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1054 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1055 			splx(s);
   1056 			break;
   1057 		}
   1058 
   1059 	case TIOCSDTR:
   1060 
   1061 	case TIOCCDTR:
   1062 
   1063 	case TIOCMSET:
   1064 
   1065 	case TIOCMBIS:
   1066 
   1067 	case TIOCMBIC:
   1068 
   1069 	case TIOCMGET:
   1070 
   1071 	default:
   1072 		return (ENOTTY);
   1073 	}
   1074 	return (0);
   1075 }
   1076 
   1077 /*
   1078  * Start or restart transmission.
   1079  */
   1080 static void
   1081 zsstart(register struct tty *tp)
   1082 {
   1083 	register struct zs_chanstate *cs;
   1084 	register int s, nch;
   1085 	int unit = minor(tp->t_dev);
   1086 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1087 
   1088 	cs = &zi->zi_cs[unit & 1];
   1089 	s = spltty();
   1090 
   1091 	/*
   1092 	 * If currently active or delaying, no need to do anything.
   1093 	 */
   1094 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
   1095 		goto out;
   1096 
   1097 	/*
   1098 	 * If there are sleepers, and output has drained below low
   1099 	 * water mark, awaken.
   1100 	 */
   1101 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1102 		if (tp->t_state & TS_ASLEEP) {
   1103 			tp->t_state &= ~TS_ASLEEP;
   1104 			wakeup((caddr_t)&tp->t_outq);
   1105 		}
   1106 		selwakeup(&tp->t_wsel);
   1107 	}
   1108 
   1109 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1110 	if (nch) {
   1111 		register char *p = tp->t_outq.c_cf;
   1112 
   1113 		/* mark busy, enable tx done interrupts, & send first byte */
   1114 		tp->t_state |= TS_BUSY;
   1115 		(void) splzs();
   1116 		cs->cs_preg[1] |= ZSWR1_TIE;
   1117 		cs->cs_creg[1] |= ZSWR1_TIE;
   1118 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1119 		cs->cs_zc->zc_data = *p;
   1120 		cs->cs_tba = p + 1;
   1121 		cs->cs_tbc = nch - 1;
   1122 	} else {
   1123 		/*
   1124 		 * Nothing to send, turn off transmit done interrupts.
   1125 		 * This is useful if something is doing polled output.
   1126 		 */
   1127 		(void) splzs();
   1128 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1129 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1130 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1131 	}
   1132 out:
   1133 	splx(s);
   1134 }
   1135 
   1136 /*
   1137  * Stop output, e.g., for ^S or output flush.
   1138  */
   1139 void
   1140 zsstop(register struct tty *tp, int flag)
   1141 {
   1142 	register struct zs_chanstate *cs;
   1143 	register int s, unit = minor(tp->t_dev);
   1144 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1145 
   1146 	cs = &zi->zi_cs[unit & 1];
   1147 	s = splzs();
   1148 	if (tp->t_state & TS_BUSY) {
   1149 		/*
   1150 		 * Device is transmitting; must stop it.
   1151 		 */
   1152 		cs->cs_tbc = 0;
   1153 		if ((tp->t_state & TS_TTSTOP) == 0)
   1154 			tp->t_state |= TS_FLUSH;
   1155 	}
   1156 	splx(s);
   1157 }
   1158 
   1159 /*
   1160  * Set ZS tty parameters from termios.
   1161  *
   1162  * This routine makes use of the fact that only registers
   1163  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1164  */
   1165 static int
   1166 zsparam(register struct tty *tp, register struct termios *t)
   1167 {
   1168 	int unit = minor(tp->t_dev);
   1169 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1170 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1171 	register int tmp, tmp5, cflag, s;
   1172 
   1173 	/*
   1174 	 * Because PCLK is only run at 4.9 MHz, the fastest we
   1175 	 * can go is 51200 baud (this corresponds to TC=1).
   1176 	 * This is somewhat unfortunate as there is no real
   1177 	 * reason we should not be able to handle higher rates.
   1178 	 */
   1179 	tmp = t->c_ospeed;
   1180 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
   1181 		return (EINVAL);
   1182 	if (tmp == 0) {
   1183 		/* stty 0 => drop DTR and RTS */
   1184 		zs_modem(cs, 0);
   1185 		return (0);
   1186 	}
   1187 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
   1188 	if (tmp < 2)
   1189 		return (EINVAL);
   1190 
   1191 	cflag = t->c_cflag;
   1192 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
   1193 	tp->t_cflag = cflag;
   1194 
   1195 	/*
   1196 	 * Block interrupts so that state will not
   1197 	 * be altered until we are done setting it up.
   1198 	 */
   1199 	s = splzs();
   1200 	cs->cs_preg[12] = tmp;
   1201 	cs->cs_preg[13] = tmp >> 8;
   1202 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1203 	switch (cflag & CSIZE) {
   1204 	case CS5:
   1205 		tmp = ZSWR3_RX_5;
   1206 		tmp5 = ZSWR5_TX_5;
   1207 		break;
   1208 	case CS6:
   1209 		tmp = ZSWR3_RX_6;
   1210 		tmp5 = ZSWR5_TX_6;
   1211 		break;
   1212 	case CS7:
   1213 		tmp = ZSWR3_RX_7;
   1214 		tmp5 = ZSWR5_TX_7;
   1215 		break;
   1216 	case CS8:
   1217 	default:
   1218 		tmp = ZSWR3_RX_8;
   1219 		tmp5 = ZSWR5_TX_8;
   1220 		break;
   1221 	}
   1222 
   1223 	/*
   1224 	 * Output hardware flow control on the chip is horrendous: if
   1225 	 * carrier detect drops, the receiver is disabled.  Hence we
   1226 	 * can only do this when the carrier is on.
   1227 	 */
   1228 	if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
   1229 		tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
   1230 	else
   1231 		tmp |= ZSWR3_RX_ENABLE;
   1232 	cs->cs_preg[3] = tmp;
   1233 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1234 
   1235 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
   1236 	if ((cflag & PARODD) == 0)
   1237 		tmp |= ZSWR4_EVENP;
   1238 	if (cflag & PARENB)
   1239 		tmp |= ZSWR4_PARENB;
   1240 	cs->cs_preg[4] = tmp;
   1241 	cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
   1242 	cs->cs_preg[10] = ZSWR10_NRZ;
   1243 	cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
   1244 	cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
   1245 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1246 
   1247 	/*
   1248 	 * If nothing is being transmitted, set up new current values,
   1249 	 * else mark them as pending.
   1250 	 */
   1251 	if (cs->cs_heldchange == 0) {
   1252 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1253 			cs->cs_heldtbc = cs->cs_tbc;
   1254 			cs->cs_tbc = 0;
   1255 			cs->cs_heldchange = 1;
   1256 		} else {
   1257 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1258 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1259 		}
   1260 	}
   1261 	splx(s);
   1262 	return (0);
   1263 }
   1264 
   1265 /*
   1266  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1267  * in transmission, the change is deferred.
   1268  */
   1269 static void
   1270 zs_modem(struct zs_chanstate *cs, int onoff)
   1271 {
   1272 	int s, bis, and;
   1273 
   1274 	if (onoff) {
   1275 		bis = ZSWR5_DTR | ZSWR5_RTS;
   1276 		and = ~0;
   1277 	} else {
   1278 		bis = 0;
   1279 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
   1280 	}
   1281 	s = splzs();
   1282 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
   1283 	if (cs->cs_heldchange == 0) {
   1284 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1285 			cs->cs_heldtbc = cs->cs_tbc;
   1286 			cs->cs_tbc = 0;
   1287 			cs->cs_heldchange = 1;
   1288 		} else {
   1289 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
   1290 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1291 		}
   1292 	}
   1293 	splx(s);
   1294 }
   1295 
   1296 /*
   1297  * Write the given register set to the given zs channel in the proper order.
   1298  * The channel must not be transmitting at the time.  The receiver will
   1299  * be disabled for the time it takes to write all the registers.
   1300  */
   1301 static void
   1302 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
   1303 {
   1304 	int i;
   1305 
   1306 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1307 	i = zc->zc_data;		/* drain fifo */
   1308 	i = zc->zc_data;
   1309 	i = zc->zc_data;
   1310 	ZS_WRITE(zc, 4, reg[4]);
   1311 	ZS_WRITE(zc, 10, reg[10]);
   1312 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
   1313 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
   1314 	ZS_WRITE(zc, 1, reg[1]);
   1315 	ZS_WRITE(zc, 9, reg[9]);
   1316 	ZS_WRITE(zc, 11, reg[11]);
   1317 	ZS_WRITE(zc, 12, reg[12]);
   1318 	ZS_WRITE(zc, 13, reg[13]);
   1319 	ZS_WRITE(zc, 14, reg[14]);
   1320 	ZS_WRITE(zc, 15, reg[15]);
   1321 	ZS_WRITE(zc, 3, reg[3]);
   1322 	ZS_WRITE(zc, 5, reg[5]);
   1323 }
   1324 
   1325 static u_char
   1326 zs_read(zc, reg)
   1327 	volatile struct zschan *zc;
   1328 	u_char reg;
   1329 {
   1330 	u_char val;
   1331 
   1332 	zc->zc_csr = reg;
   1333 	ZS_DELAY();
   1334 	val = zc->zc_csr;
   1335 	ZS_DELAY();
   1336 	return val;
   1337 }
   1338 
   1339 static u_char
   1340 zs_write(zc, reg, val)
   1341 	volatile struct zschan *zc;
   1342 	u_char reg, val;
   1343 {
   1344 	zc->zc_csr = reg;
   1345 	ZS_DELAY();
   1346 	zc->zc_csr = val;
   1347 	ZS_DELAY();
   1348 	return val;
   1349 }
   1350 
   1351 #ifdef KGDB
   1352 /*
   1353  * Get a character from the given kgdb channel.  Called at splhigh().
   1354  */
   1355 static int
   1356 zs_kgdb_getc(void *arg)
   1357 {
   1358 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1359 
   1360 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
   1361 		continue;
   1362 	return (zc->zc_data);
   1363 }
   1364 
   1365 /*
   1366  * Put a character to the given kgdb channel.  Called at splhigh().
   1367  */
   1368 static void
   1369 zs_kgdb_putc(void *arg, int c)
   1370 {
   1371 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1372 
   1373 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
   1374 		continue;
   1375 	zc->zc_data = c;
   1376 }
   1377 
   1378 /*
   1379  * Set up for kgdb; called at boot time before configuration.
   1380  * KGDB interrupts will be enabled later when zs0 is configured.
   1381  */
   1382 void
   1383 zs_kgdb_init()
   1384 {
   1385 	volatile struct zsdevice *addr;
   1386 	volatile struct zschan *zc;
   1387 	int unit, zs;
   1388 
   1389 	if (major(kgdb_dev) != ZSMAJOR)
   1390 		return;
   1391 	unit = minor(kgdb_dev);
   1392 	/*
   1393 	 * Unit must be 0 or 1 (zs0).
   1394 	 */
   1395 	if ((unsigned)unit >= ZS_KBD) {
   1396 		printf("zs_kgdb_init: bad minor dev %d\n", unit);
   1397 		return;
   1398 	}
   1399 	zs = unit >> 1;
   1400 	unit &= 1;
   1401 
   1402 	if ((addr = zs0_va) == NULL)
   1403 		panic("kbdb_attach: zs0 not yet mapped");
   1404 
   1405 	zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B];
   1406 	zs_kgdb_savedspeed = zs_getspeed(zc);
   1407 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
   1408 	    zs, unit + 'a', kgdb_rate);
   1409 	zs_reset(zc, 1, kgdb_rate);
   1410 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
   1411 }
   1412 #endif /* KGDB */
   1413