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