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