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