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