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