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