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