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