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