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