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