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