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