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