Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.29
      1 /*	$NetBSD: zs.c,v 1.29 1995/08/08 20:54:08 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/z8530reg.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(tp)
    604 	struct tty *tp;
    605 {
    606 
    607 	(void) zsparam(tp, &tp->t_termios);
    608 	ttsetwater(tp);
    609 	tp->t_state = TS_ISOPEN | TS_CARR_ON;
    610 }
    611 
    612 /*
    613  * Do an internal close.  Eventually we should shut off the chip when both
    614  * ports on it are closed.
    615  */
    616 static void
    617 zsiclose(tp)
    618 	struct tty *tp;
    619 {
    620 
    621 	ttylclose(tp, 0);	/* ??? */
    622 	ttyclose(tp);		/* ??? */
    623 	tp->t_state = 0;
    624 }
    625 
    626 
    627 /*
    628  * Open a zs serial port.  This interface may not be used to open
    629  * the keyboard and mouse ports. (XXX)
    630  */
    631 int
    632 zsopen(dev, flags, mode, p)
    633 	dev_t dev;
    634 	int flags;
    635 	int mode;
    636 	struct proc *p;
    637 {
    638 	register struct tty *tp;
    639 	register struct zs_chanstate *cs;
    640 	struct zsinfo *zi;
    641 	int unit = minor(dev), zs = unit >> 1, error, s;
    642 
    643 #ifdef	DEBUG
    644 	mon_printf("zs_open\n");
    645 #endif
    646 	if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
    647 	    unit == ZS_KBD || unit == ZS_MOUSE)
    648 		return (ENXIO);
    649 	cs = &zi->zi_cs[unit & 1];
    650 	tp = cs->cs_ttyp;
    651 	s = spltty();
    652 	if ((tp->t_state & TS_ISOPEN) == 0) {
    653 		ttychars(tp);
    654 		tp->t_iflag = TTYDEF_IFLAG;
    655 		tp->t_oflag = TTYDEF_OFLAG;
    656 		tp->t_cflag = TTYDEF_CFLAG;
    657 		tp->t_lflag = TTYDEF_LFLAG;
    658 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    659 		(void) zsparam(tp, &tp->t_termios);
    660 		ttsetwater(tp);
    661 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    662 		splx(s);
    663 		return (EBUSY);
    664 	}
    665 	error = 0;
    666 #ifdef	DEBUG
    667 	mon_printf("wait for carrier...\n");
    668 #endif
    669 	for (;;) {
    670 		register int rr0;
    671 
    672 		/* loop, turning on the device, until carrier present */
    673 		zs_modem(cs, 1);
    674 		/* May never get status intr if carrier already on. -gwr */
    675 		rr0 = cs->cs_zc->zc_csr;
    676 		ZS_DELAY();
    677 		if (rr0 & ZSRR0_DCD)
    678 			tp->t_state |= TS_CARR_ON;
    679 		if (cs->cs_softcar)
    680 			tp->t_state |= TS_CARR_ON;
    681 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
    682 		    tp->t_state & TS_CARR_ON)
    683 			break;
    684 		tp->t_state |= TS_WOPEN;
    685 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    686 		    ttopen, 0)) {
    687 			if (!(tp->t_state & TS_ISOPEN)) {
    688 				zs_modem(cs, 0);
    689 				tp->t_state &= ~TS_WOPEN;
    690 				ttwakeup(tp);
    691 			}
    692 			splx(s);
    693 			return error;
    694 		}
    695 	}
    696 #ifdef	DEBUG
    697 	mon_printf("...carrier %s\n",
    698 			   (tp->t_state & TS_CARR_ON) ? "on" : "off");
    699 #endif
    700 	splx(s);
    701 	if (error == 0)
    702 		error = linesw[tp->t_line].l_open(dev, tp);
    703 	if (error)
    704 		zs_modem(cs, 0);
    705 	return (error);
    706 }
    707 
    708 /*
    709  * Close a zs serial port.
    710  */
    711 int
    712 zsclose(dev, flags, mode, p)
    713 	dev_t dev;
    714 	int flags;
    715 	int mode;
    716 	struct proc *p;
    717 {
    718 	register struct zs_chanstate *cs;
    719 	register struct tty *tp;
    720 	struct zsinfo *zi;
    721 	int unit = minor(dev), s;
    722 
    723 #ifdef	DEBUG
    724 	mon_printf("zs_close\n");
    725 #endif
    726 	zi = zscd.cd_devs[unit >> 1];
    727 	cs = &zi->zi_cs[unit & 1];
    728 	tp = cs->cs_ttyp;
    729 	linesw[tp->t_line].l_close(tp, flags);
    730 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
    731 	    (tp->t_state & TS_ISOPEN) == 0) {
    732 		zs_modem(cs, 0);
    733 		/* hold low for 1 second */
    734 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
    735 	}
    736 	if (cs->cs_creg[5] & ZSWR5_BREAK)
    737 	{
    738 		s = splzs();
    739 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    740 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    741 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    742 		splx(s);
    743 	}
    744 	ttyclose(tp);
    745 #ifdef KGDB
    746 	/* Reset the speed if we're doing kgdb on this port */
    747 	if (cs->cs_kgdb) {
    748 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    749 		(void) zsparam(tp, &tp->t_termios);
    750 	}
    751 #endif
    752 	return (0);
    753 }
    754 
    755 /*
    756  * Read/write zs serial port.
    757  */
    758 int
    759 zsread(dev, uio, flags)
    760 	dev_t dev;
    761 	struct uio *uio;
    762 	int flags;
    763 {
    764 	register struct tty *tp = zs_tty[minor(dev)];
    765 
    766 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    767 }
    768 
    769 int
    770 zswrite(dev, uio, flags)
    771 	dev_t dev;
    772 	struct uio *uio;
    773 	int flags;
    774 {
    775 	register struct tty *tp = zs_tty[minor(dev)];
    776 
    777 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    778 }
    779 
    780 /*
    781  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    782  * channels are kept in (A,B) pairs.
    783  *
    784  * Do just a little, then get out; set a software interrupt if more
    785  * work is needed.
    786  *
    787  * We deliberately ignore the vectoring Zilog gives us, and match up
    788  * only the number of `reset interrupt under service' operations, not
    789  * the order.
    790  */
    791 /* ARGSUSED */
    792 int
    793 zshard(intrarg)
    794 	int intrarg;
    795 {
    796 	register struct zs_chanstate *a;
    797 #define	b (a + 1)
    798 	register volatile struct zschan *zc;
    799 	register int rr3, intflags = 0, v, i;
    800 	static int zsrint(struct zs_chanstate *, volatile struct zschan *);
    801 	static int zsxint(struct zs_chanstate *, volatile struct zschan *);
    802 	static int zssint(struct zs_chanstate *, volatile struct zschan *);
    803 
    804 	for (a = zslist; a != NULL; a = b->cs_next) {
    805 		rr3 = ZS_READ(a->cs_zc, 3);
    806 
    807 		/* XXX - This should loop to empty the on-chip fifo. */
    808 		if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    809 			intflags |= 2;
    810 			zc = a->cs_zc;
    811 			i = a->cs_rbput;
    812 			if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    813 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    814 				intflags |= 1;
    815 			}
    816 			if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    817 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    818 				intflags |= 1;
    819 			}
    820 			if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    821 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    822 				intflags |= 1;
    823 			}
    824 			a->cs_rbput = i;
    825 		}
    826 
    827 		/* XXX - This should loop to empty the on-chip fifo. */
    828 		if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    829 			intflags |= 2;
    830 			zc = b->cs_zc;
    831 			i = b->cs_rbput;
    832 			if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    833 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    834 				intflags |= 1;
    835 			}
    836 			if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    837 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    838 				intflags |= 1;
    839 			}
    840 			if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    841 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    842 				intflags |= 1;
    843 			}
    844 			b->cs_rbput = i;
    845 		}
    846 	}
    847 #undef b
    848 	if (intflags & 1) {
    849 		if (zssoftpending == 0) {
    850 			/* We are at splzs here, so no need to lock. */
    851 			zssoftpending = ZSSOFT_PRI;
    852 			isr_soft_request(ZSSOFT_PRI);
    853 		}
    854 	}
    855 	return (intflags & 2);
    856 }
    857 
    858 static int
    859 zsrint(cs, zc)
    860 	register struct zs_chanstate *cs;
    861 	register volatile struct zschan *zc;
    862 {
    863 	register int c;
    864 
    865 	c = zc->zc_data;
    866 	ZS_DELAY();
    867 
    868 	if (cs->cs_conk) {
    869 		register struct conk_state *conk = &zsconk_state;
    870 
    871 		/*
    872 		 * Check here for console abort function, so that we
    873 		 * can abort even when interrupts are locking up the
    874 		 * machine.
    875 		 */
    876 		if (c == KBD_RESET) {
    877 			conk->conk_id = 1;	/* ignore next byte */
    878 			conk->conk_l1 = 0;
    879 		} else if (conk->conk_id)
    880 			conk->conk_id = 0;	/* stop ignoring bytes */
    881 		else if (c == KBD_L1)
    882 			conk->conk_l1 = 1;	/* L1 went down */
    883 		else if (c == (KBD_L1|KBD_UP))
    884 			conk->conk_l1 = 0;	/* L1 went up */
    885 		else if (c == KBD_A && conk->conk_l1) {
    886 			zsabort();
    887 			/* Debugger done.  Send L1-up in case X is running. */
    888 			conk->conk_l1 = 0;
    889 			c = (KBD_L1|KBD_UP);
    890 		}
    891 	}
    892 #ifdef KGDB
    893 	if (c == FRAME_START && cs->cs_kgdb &&
    894 	    (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
    895 		zskgdb(cs->cs_unit);
    896 		c = 0;
    897 		goto clearit;
    898 	}
    899 #endif
    900 	/* compose receive character and status */
    901 	c <<= 8;
    902 	c |= ZS_READ(zc, 1);
    903 	c = ZRING_MAKE(ZRING_RINT, c);
    904 
    905 clearit:
    906 	/* clear receive error & interrupt condition */
    907 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    908 	ZS_DELAY();
    909 	zc->zc_csr = ZSWR0_CLR_INTR;
    910 	ZS_DELAY();
    911 	return (c);
    912 }
    913 
    914 static int
    915 zsxint(cs, zc)
    916 	register struct zs_chanstate *cs;
    917 	register volatile struct zschan *zc;
    918 {
    919 	register int i = cs->cs_tbc;
    920 
    921 	if (i == 0) {
    922 		zc->zc_csr = ZSWR0_RESET_TXINT;
    923 		ZS_DELAY();
    924 		zc->zc_csr = ZSWR0_CLR_INTR;
    925 		ZS_DELAY();
    926 		return (ZRING_MAKE(ZRING_XINT, 0));
    927 	}
    928 	cs->cs_tbc = i - 1;
    929 	zc->zc_data = *cs->cs_tba++;
    930 	ZS_DELAY();
    931 	zc->zc_csr = ZSWR0_CLR_INTR;
    932 	ZS_DELAY();
    933 	return (0);
    934 }
    935 
    936 static int
    937 zssint(cs, zc)
    938 	register struct zs_chanstate *cs;
    939 	register volatile struct zschan *zc;
    940 {
    941 	register int rr0;
    942 
    943 	rr0 = zc->zc_csr;
    944 	ZS_DELAY();
    945 	zc->zc_csr = ZSWR0_RESET_STATUS;
    946 	ZS_DELAY();
    947 	zc->zc_csr = ZSWR0_CLR_INTR;
    948 	ZS_DELAY();
    949 	/*
    950 	 * The chip's hardware flow control is, as noted in zsreg.h,
    951 	 * busted---if the DCD line goes low the chip shuts off the
    952 	 * receiver (!).  If we want hardware CTS flow control but do
    953 	 * not have it, and carrier is now on, turn HFC on; if we have
    954 	 * HFC now but carrier has gone low, turn it off.
    955 	 */
    956 	if (rr0 & ZSRR0_DCD) {
    957 		if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    958 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    959 			cs->cs_creg[3] |= ZSWR3_HFC;
    960 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    961 		}
    962 	} else {
    963 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    964 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    965 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    966 		}
    967 	}
    968 	if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
    969 		/* Wait for end of break to avoid PROM abort. */
    970 		do {
    971 			rr0 = zc->zc_csr;
    972 			ZS_DELAY();
    973 		} while (rr0 & ZSRR0_BREAK);
    974 		zsabort();
    975 		return (0);
    976 	}
    977 	return (ZRING_MAKE(ZRING_SINT, rr0));
    978 }
    979 
    980 zsabort()
    981 {
    982 #ifdef DDB
    983 	Debugger();
    984 #else
    985 	sun3_mon_abort();
    986 #endif
    987 }
    988 
    989 #ifdef KGDB
    990 /*
    991  * KGDB framing character received: enter kernel debugger.  This probably
    992  * should time out after a few seconds to avoid hanging on spurious input.
    993  */
    994 zskgdb(unit)
    995 	int unit;
    996 {
    997 
    998 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
    999 	kgdb_connect(1);
   1000 }
   1001 #endif
   1002 
   1003 /*
   1004  * Print out a ring or fifo overrun error message.
   1005  */
   1006 static void
   1007 zsoverrun(unit, ptime, what)
   1008 	int unit;
   1009 	long *ptime;
   1010 	char *what;
   1011 {
   1012 
   1013 	if (*ptime != time.tv_sec) {
   1014 		*ptime = time.tv_sec;
   1015 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
   1016 		    (unit & 1) + 'a', what);
   1017 	}
   1018 }
   1019 
   1020 /*
   1021  * ZS software interrupt.  Scan all channels for deferred interrupts.
   1022  */
   1023 int
   1024 zssoft(arg)
   1025 	int arg;
   1026 {
   1027 	register struct zs_chanstate *cs;
   1028 	register volatile struct zschan *zc;
   1029 	register struct linesw *line;
   1030 	register struct tty *tp;
   1031 	register int get, n, c, cc, unit, s;
   1032 
   1033 	/* This is not the only ISR on this IPL. */
   1034 	if (zssoftpending == 0)
   1035 		return (0);
   1036 
   1037 	/*
   1038 	 * The soft intr. bit will be set by zshard only if
   1039 	 * the variable zssoftpending is zero.  The order of
   1040 	 * these next two statements prevents our clearing
   1041 	 * the soft intr bit just after zshard has set it.
   1042 	 */
   1043 	isr_soft_clear(ZSSOFT_PRI);
   1044 	zssoftpending = 0;	/* Now zshard may set it again. */
   1045 
   1046 	for (cs = zslist; cs != NULL; cs = cs->cs_next) {
   1047 		get = cs->cs_rbget;
   1048 again:
   1049 		n = cs->cs_rbput;	/* atomic */
   1050 		if (get == n)		/* nothing more on this line */
   1051 			continue;
   1052 		unit = cs->cs_unit;	/* set up to handle interrupts */
   1053 		zc = cs->cs_zc;
   1054 		tp = cs->cs_ttyp;
   1055 		line = &linesw[tp->t_line];
   1056 		/*
   1057 		 * Compute the number of interrupts in the receive ring.
   1058 		 * If the count is overlarge, we lost some events, and
   1059 		 * must advance to the first valid one.  It may get
   1060 		 * overwritten if more data are arriving, but this is
   1061 		 * too expensive to check and gains nothing (we already
   1062 		 * lost out; all we can do at this point is trade one
   1063 		 * kind of loss for another).
   1064 		 */
   1065 		n -= get;
   1066 		if (n > ZLRB_RING_SIZE) {
   1067 			zsoverrun(unit, &cs->cs_rotime, "ring");
   1068 			get += n - ZLRB_RING_SIZE;
   1069 			n = ZLRB_RING_SIZE;
   1070 		}
   1071 		while (--n >= 0) {
   1072 			/* race to keep ahead of incoming interrupts */
   1073 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
   1074 			switch (ZRING_TYPE(c)) {
   1075 
   1076 			case ZRING_RINT:
   1077 				c = ZRING_VALUE(c);
   1078 				if (c & ZSRR1_DO)
   1079 					zsoverrun(unit, &cs->cs_fotime, "fifo");
   1080 				cc = c >> 8;
   1081 				if (c & ZSRR1_FE)
   1082 					cc |= TTY_FE;
   1083 				if (c & ZSRR1_PE)
   1084 					cc |= TTY_PE;
   1085 				/*
   1086 				 * this should be done through
   1087 				 * bstreams	XXX gag choke
   1088 				 */
   1089 				if (unit == ZS_KBD)
   1090 					kbd_rint(cc);
   1091 				else if (unit == ZS_MOUSE)
   1092 					ms_rint(cc);
   1093 				else
   1094 					line->l_rint(cc, tp);
   1095 				break;
   1096 
   1097 			case ZRING_XINT:
   1098 				/*
   1099 				 * Transmit done: change registers and resume,
   1100 				 * or clear BUSY.
   1101 				 */
   1102 				if (cs->cs_heldchange) {
   1103 					s = splzs();
   1104 					c = zc->zc_csr;
   1105 					ZS_DELAY();
   1106 					if ((c & ZSRR0_DCD) == 0)
   1107 						cs->cs_preg[3] &= ~ZSWR3_HFC;
   1108 					bcopy((caddr_t)cs->cs_preg,
   1109 					    (caddr_t)cs->cs_creg, 16);
   1110 					zs_loadchannelregs(zc, cs->cs_creg);
   1111 					splx(s);
   1112 					cs->cs_heldchange = 0;
   1113 					if (cs->cs_heldtbc &&
   1114 					    (tp->t_state & TS_TTSTOP) == 0) {
   1115 						cs->cs_tbc = cs->cs_heldtbc - 1;
   1116 						zc->zc_data = *cs->cs_tba++;
   1117 						ZS_DELAY();
   1118 						goto again;
   1119 					}
   1120 				}
   1121 				tp->t_state &= ~TS_BUSY;
   1122 				if (tp->t_state & TS_FLUSH)
   1123 					tp->t_state &= ~TS_FLUSH;
   1124 				else
   1125 					ndflush(&tp->t_outq, cs->cs_tba -
   1126 						(caddr_t) tp->t_outq.c_cf);
   1127 				line->l_start(tp);
   1128 				break;
   1129 
   1130 			case ZRING_SINT:
   1131 				/*
   1132 				 * Status line change.  HFC bit is run in
   1133 				 * hardware interrupt, to avoid locking
   1134 				 * at splzs here.
   1135 				 */
   1136 				c = ZRING_VALUE(c);
   1137 				if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
   1138 					cc = (c & ZSRR0_DCD) != 0;
   1139 					if (line->l_modem(tp, cc) == 0)
   1140 						zs_modem(cs, cc);
   1141 				}
   1142 				cs->cs_rr0 = c;
   1143 				break;
   1144 
   1145 			default:
   1146 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
   1147 				    unit >> 1, (unit & 1) + 'a', c);
   1148 				break;
   1149 			}
   1150 		}
   1151 		cs->cs_rbget = get;
   1152 		goto again;
   1153 	}
   1154 	return (1);
   1155 }
   1156 
   1157 int
   1158 zsioctl(dev, cmd, data, flag, p)
   1159 	dev_t dev;
   1160 	u_long cmd;
   1161 	caddr_t data;
   1162 	int flag;
   1163 	struct proc *p;
   1164 {
   1165 	int unit = minor(dev);
   1166 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1167 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1168 	register struct tty *tp = cs->cs_ttyp;
   1169 	register int error, s;
   1170 
   1171 	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
   1172 	if (error >= 0)
   1173 		return (error);
   1174 	error = ttioctl(tp, cmd, data, flag, p);
   1175 	if (error >= 0)
   1176 		return (error);
   1177 
   1178 	switch (cmd) {
   1179 
   1180 	case TIOCSBRK:
   1181 		s = splzs();
   1182 		cs->cs_preg[5] |= ZSWR5_BREAK;
   1183 		cs->cs_creg[5] |= ZSWR5_BREAK;
   1184 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1185 		splx(s);
   1186 		break;
   1187 
   1188 	case TIOCCBRK:
   1189 		s = splzs();
   1190 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1191 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1192 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1193 		splx(s);
   1194 		break;
   1195 
   1196 	case TIOCGFLAGS: {
   1197 		int bits = 0;
   1198 
   1199 		if (cs->cs_softcar)
   1200 			bits |= TIOCFLAG_SOFTCAR;
   1201 		if (cs->cs_creg[15] & ZSWR15_DCD_IE)
   1202 			bits |= TIOCFLAG_CLOCAL;
   1203 		if (cs->cs_creg[3] & ZSWR3_HFC)
   1204 			bits |= TIOCFLAG_CRTSCTS;
   1205 		*(int *)data = bits;
   1206 		break;
   1207 	}
   1208 
   1209 	case TIOCSFLAGS: {
   1210 		int userbits, driverbits = 0;
   1211 
   1212 		error = suser(p->p_ucred, &p->p_acflag);
   1213 		if (error != 0)
   1214 			return (EPERM);
   1215 
   1216 		userbits = *(int *)data;
   1217 
   1218 		/*
   1219 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
   1220 		 * defaulting to software flow control.
   1221 		 */
   1222 		if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
   1223 			return(EINVAL);
   1224 		if (userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
   1225 			return(ENXIO);
   1226 
   1227 		s = splzs();
   1228 		if ((userbits & TIOCFLAG_SOFTCAR) ||
   1229 			(cs->cs_zc == zs_conschan))
   1230 		{
   1231 			cs->cs_softcar = 1;	/* turn on softcar */
   1232 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
   1233 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
   1234 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1235 		} else if (userbits & TIOCFLAG_CLOCAL) {
   1236 			cs->cs_softcar = 0; 	/* turn off softcar */
   1237 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
   1238 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
   1239 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1240 			tp->t_termios.c_cflag |= CLOCAL;
   1241 		}
   1242 		if (userbits & TIOCFLAG_CRTSCTS) {
   1243 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
   1244 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
   1245 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1246 			cs->cs_preg[3] |= ZSWR3_HFC;
   1247 			cs->cs_creg[3] |= ZSWR3_HFC;
   1248 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1249 			tp->t_termios.c_cflag |= CRTSCTS;
   1250 		} else {
   1251 			/* no mdmbuf, so we must want software flow control */
   1252 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
   1253 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
   1254 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1255 			cs->cs_preg[3] &= ~ZSWR3_HFC;
   1256 			cs->cs_creg[3] &= ~ZSWR3_HFC;
   1257 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1258 			tp->t_termios.c_cflag &= ~CRTSCTS;
   1259 		}
   1260 		splx(s);
   1261 		break;
   1262 	}
   1263 
   1264 	case TIOCSDTR:
   1265 		zs_modem(cs, 1);
   1266 		break;
   1267 	case TIOCCDTR:
   1268 		zs_modem(cs, 0);
   1269 		break;
   1270 
   1271 	case TIOCMSET:
   1272 	case TIOCMBIS:
   1273 	case TIOCMBIC:
   1274 	case TIOCMGET:
   1275 	default:
   1276 		return (ENOTTY);
   1277 	}
   1278 	return (0);
   1279 }
   1280 
   1281 /*
   1282  * Start or restart transmission.
   1283  */
   1284 static void
   1285 zsstart(tp)
   1286 	register struct tty *tp;
   1287 {
   1288 	register struct zs_chanstate *cs;
   1289 	register int s, nch;
   1290 	int unit = minor(tp->t_dev);
   1291 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1292 
   1293 	cs = &zi->zi_cs[unit & 1];
   1294 	s = spltty();
   1295 
   1296 	/*
   1297 	 * If currently active or delaying, no need to do anything.
   1298 	 */
   1299 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
   1300 		goto out;
   1301 
   1302 	/*
   1303 	 * If there are sleepers, and output has drained below low
   1304 	 * water mark, awaken.
   1305 	 */
   1306 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1307 		if (tp->t_state & TS_ASLEEP) {
   1308 			tp->t_state &= ~TS_ASLEEP;
   1309 			wakeup((caddr_t)&tp->t_outq);
   1310 		}
   1311 		selwakeup(&tp->t_wsel);
   1312 	}
   1313 
   1314 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1315 	if (nch) {
   1316 		register char *p = tp->t_outq.c_cf;
   1317 
   1318 		/* mark busy, enable tx done interrupts, & send first byte */
   1319 		tp->t_state |= TS_BUSY;
   1320 		(void) splzs();
   1321 		cs->cs_preg[1] |= ZSWR1_TIE;
   1322 		cs->cs_creg[1] |= ZSWR1_TIE;
   1323 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1324 		cs->cs_zc->zc_data = *p;
   1325 		ZS_DELAY();
   1326 		cs->cs_tba = p + 1;
   1327 		cs->cs_tbc = nch - 1;
   1328 	} else {
   1329 		/*
   1330 		 * Nothing to send, turn off transmit done interrupts.
   1331 		 * This is useful if something is doing polled output.
   1332 		 */
   1333 		(void) splzs();
   1334 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1335 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1336 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1337 	}
   1338 out:
   1339 	splx(s);
   1340 }
   1341 
   1342 /*
   1343  * Stop output, e.g., for ^S or output flush.
   1344  */
   1345 void
   1346 zsstop(tp, flag)
   1347 	register struct tty *tp;
   1348 	int flag;
   1349 {
   1350 	register struct zs_chanstate *cs;
   1351 	register int s, unit = minor(tp->t_dev);
   1352 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1353 
   1354 	cs = &zi->zi_cs[unit & 1];
   1355 	s = splzs();
   1356 	if (tp->t_state & TS_BUSY) {
   1357 		/*
   1358 		 * Device is transmitting; must stop it.
   1359 		 */
   1360 		cs->cs_tbc = 0;
   1361 		if ((tp->t_state & TS_TTSTOP) == 0)
   1362 			tp->t_state |= TS_FLUSH;
   1363 	}
   1364 	splx(s);
   1365 }
   1366 
   1367 /*
   1368  * Set ZS tty parameters from termios.
   1369  */
   1370 static int
   1371 zsparam(tp, t)
   1372 	register struct tty *tp;
   1373 	register struct termios *t;
   1374 {
   1375 	int unit = minor(tp->t_dev);
   1376 	struct zsinfo *zi = zscd.cd_devs[unit >> 1];
   1377 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1378 	register int tmp, tmp5, cflag, s;
   1379 
   1380 	/*
   1381 	 * Because PCLK is only run at 4.9 MHz, the fastest we
   1382 	 * can go is 51200 baud (this corresponds to TC=1).
   1383 	 * This is somewhat unfortunate as there is no real
   1384 	 * reason we should not be able to handle higher rates.
   1385 	 */
   1386 	tmp = t->c_ospeed;
   1387 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
   1388 		return (EINVAL);
   1389 	if (tmp == 0) {
   1390 		/* stty 0 => drop DTR and RTS */
   1391 		zs_modem(cs, 0);
   1392 		return (0);
   1393 	}
   1394 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
   1395 	if (tmp < 2)
   1396 		return (EINVAL);
   1397 
   1398 	cflag = t->c_cflag;
   1399 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
   1400 	tp->t_cflag = cflag;
   1401 
   1402 	/*
   1403 	 * Block interrupts so that state will not
   1404 	 * be altered until we are done setting it up.
   1405 	 */
   1406 	s = splzs();
   1407 	bcopy(zs_init_reg, cs->cs_preg, 16);
   1408 	cs->cs_preg[12] = tmp;
   1409 	cs->cs_preg[13] = tmp >> 8;
   1410 	cs->cs_preg[9] |= ZSWR9_MASTER_IE;
   1411 	switch (cflag & CSIZE) {
   1412 	case CS5:
   1413 		tmp = ZSWR3_RX_5;
   1414 		tmp5 = ZSWR5_TX_5;
   1415 		break;
   1416 	case CS6:
   1417 		tmp = ZSWR3_RX_6;
   1418 		tmp5 = ZSWR5_TX_6;
   1419 		break;
   1420 	case CS7:
   1421 		tmp = ZSWR3_RX_7;
   1422 		tmp5 = ZSWR5_TX_7;
   1423 		break;
   1424 	case CS8:
   1425 	default:
   1426 		tmp = ZSWR3_RX_8;
   1427 		tmp5 = ZSWR5_TX_8;
   1428 		break;
   1429 	}
   1430 
   1431 	/*
   1432 	 * Output hardware flow control on the chip is horrendous: if
   1433 	 * carrier detect drops, the receiver is disabled.  Hence we
   1434 	 * can only do this when the carrier is on.
   1435 	 */
   1436 	tmp |= ZSWR3_RX_ENABLE;
   1437 	if (cflag & CCTS_OFLOW) {
   1438 		if (cs->cs_zc->zc_csr & ZSRR0_DCD)
   1439 			tmp |= ZSWR3_HFC;
   1440 		ZS_DELAY();
   1441 	}
   1442 
   1443 	cs->cs_preg[3] = tmp;
   1444 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1445 
   1446 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
   1447 	if ((cflag & PARODD) == 0)
   1448 		tmp |= ZSWR4_EVENP;
   1449 	if (cflag & PARENB)
   1450 		tmp |= ZSWR4_PARENB;
   1451 	cs->cs_preg[4] = tmp;
   1452 
   1453 	/*
   1454 	 * If nothing is being transmitted, set up new current values,
   1455 	 * else mark them as pending.
   1456 	 */
   1457 	if (cs->cs_heldchange == 0) {
   1458 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1459 			cs->cs_heldtbc = cs->cs_tbc;
   1460 			cs->cs_tbc = 0;
   1461 			cs->cs_heldchange = 1;
   1462 		} else {
   1463 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1464 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1465 		}
   1466 	}
   1467 	splx(s);
   1468 	return (0);
   1469 }
   1470 
   1471 /*
   1472  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1473  * in transmission, the change is deferred.
   1474  */
   1475 static void
   1476 zs_modem(cs, onoff)
   1477 	struct zs_chanstate *cs;
   1478 	int onoff;
   1479 {
   1480 	int s, bis, and;
   1481 
   1482 	if (onoff) {
   1483 		bis = ZSWR5_DTR | ZSWR5_RTS;
   1484 		and = ~0;
   1485 	} else {
   1486 		bis = 0;
   1487 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
   1488 	}
   1489 	s = splzs();
   1490 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
   1491 	if (cs->cs_heldchange == 0) {
   1492 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1493 			cs->cs_heldtbc = cs->cs_tbc;
   1494 			cs->cs_tbc = 0;
   1495 			cs->cs_heldchange = 1;
   1496 		} else {
   1497 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
   1498 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1499 		}
   1500 	}
   1501 	splx(s);
   1502 }
   1503 
   1504 /*
   1505  * Write the given register set to the given zs channel in the proper order.
   1506  * The channel must not be transmitting at the time.  The receiver will
   1507  * be disabled for the time it takes to write all the registers.
   1508  */
   1509 static void
   1510 zs_loadchannelregs(zc, reg)
   1511 	volatile struct zschan *zc;
   1512 	u_char *reg;
   1513 {
   1514 	int i;
   1515 
   1516 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1517 	ZS_DELAY();
   1518 
   1519 #if 1	/* XXX - Is this really a good idea? -gwr */
   1520 	i = zc->zc_data;		/* drain fifo */
   1521 	ZS_DELAY();
   1522 	i = zc->zc_data;
   1523 	ZS_DELAY();
   1524 	i = zc->zc_data;
   1525 	ZS_DELAY();
   1526 #endif
   1527 
   1528 	/* baud clock divisor, stop bits, parity */
   1529 	ZS_WRITE(zc, 4, reg[4]);
   1530 
   1531 	/* misc. TX/RX control bits */
   1532 	ZS_WRITE(zc, 10, reg[10]);
   1533 
   1534 	/* char size, enable (RX/TX) */
   1535 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
   1536 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
   1537 
   1538 	/* interrupt enables: TX, TX, STATUS */
   1539 	ZS_WRITE(zc, 1, reg[1]);
   1540 
   1541 	/* interrupt vector */
   1542 	ZS_WRITE(zc, 2, reg[2]);
   1543 
   1544 	/* master interrupt control */
   1545 	ZS_WRITE(zc, 9, reg[9]);
   1546 
   1547 	/* clock mode control */
   1548 	ZS_WRITE(zc, 11, reg[11]);
   1549 
   1550 	/* baud rate (lo/hi) */
   1551 	ZS_WRITE(zc, 12, reg[12]);
   1552 	ZS_WRITE(zc, 13, reg[13]);
   1553 
   1554 	/* Misc. control bits */
   1555 	ZS_WRITE(zc, 14, reg[14]);
   1556 
   1557 	/* which lines cause status interrupts */
   1558 	ZS_WRITE(zc, 15, reg[15]);
   1559 
   1560 	/* char size, enable (RX/TX)*/
   1561 	ZS_WRITE(zc, 3, reg[3]);
   1562 	ZS_WRITE(zc, 5, reg[5]);
   1563 }
   1564 
   1565 static u_char
   1566 zs_read(zc, reg)
   1567 	volatile struct zschan *zc;
   1568 	u_char reg;
   1569 {
   1570 	u_char val;
   1571 
   1572 	zc->zc_csr = reg;
   1573 	ZS_DELAY();
   1574 	val = zc->zc_csr;
   1575 	ZS_DELAY();
   1576 	return val;
   1577 }
   1578 
   1579 static u_char
   1580 zs_write(zc, reg, val)
   1581 	volatile struct zschan *zc;
   1582 	u_char reg, val;
   1583 {
   1584 	zc->zc_csr = reg;
   1585 	ZS_DELAY();
   1586 	zc->zc_csr = val;
   1587 	ZS_DELAY();
   1588 	return val;
   1589 }
   1590 
   1591 #ifdef KGDB
   1592 /*
   1593  * Get a character from the given kgdb channel.  Called at splhigh().
   1594  * XXX - Add delays, or combine with zscngetc()...
   1595  */
   1596 static int
   1597 zs_kgdb_getc(arg)
   1598 	void *arg;
   1599 {
   1600 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1601 	register int c, rr0;
   1602 
   1603 	do {
   1604 		rr0 = zc->zc_csr;
   1605 		ZS_DELAY();
   1606 	} while ((rr0 & ZSRR0_RX_READY) == 0);
   1607 	c = zc->zc_data;
   1608 	ZS_DELAY();
   1609 	return (c);
   1610 }
   1611 
   1612 /*
   1613  * Put a character to the given kgdb channel.  Called at splhigh().
   1614  */
   1615 static void
   1616 zs_kgdb_putc(arg, c)
   1617 	void *arg;
   1618 	int c;
   1619 {
   1620 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1621 	register int c, rr0;
   1622 
   1623 	do {
   1624 		rr0 = zc->zc_csr;
   1625 		ZS_DELAY();
   1626 	} while ((rr0 & ZSRR0_TX_READY) == 0);
   1627 	zc->zc_data = c;
   1628 	ZS_DELAY();
   1629 }
   1630 
   1631 /*
   1632  * Set up for kgdb; called at boot time before configuration.
   1633  * KGDB interrupts will be enabled later when zs0 is configured.
   1634  */
   1635 void
   1636 zs_kgdb_init()
   1637 {
   1638 	volatile struct zsdevice *addr;
   1639 	volatile struct zschan *zc;
   1640 	int unit, zs;
   1641 
   1642 	if (major(kgdb_dev) != ZSMAJOR)
   1643 		return;
   1644 	unit = minor(kgdb_dev);
   1645 	/*
   1646 	 * Unit must be 0 or 1 (zs0).
   1647 	 */
   1648 	if ((unsigned)unit >= ZS_KBD) {
   1649 		printf("zs_kgdb_init: bad minor dev %d\n", unit);
   1650 		return;
   1651 	}
   1652 	zs = unit >> 1;
   1653 	unit &= 1;
   1654 
   1655 	if (zsaddr[0] == NULL)
   1656 		panic("kbdb_attach: zs0 not yet mapped");
   1657 	addr = zsaddr[0];
   1658 
   1659 	zc = (unit == 0) ?
   1660 		&addr->zs_chan[ZS_CHAN_A] :
   1661 		&addr->zs_chan[ZS_CHAN_B];
   1662 	zs_kgdb_savedspeed = zs_getspeed(zc);
   1663 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
   1664 	    zs, unit + 'a', kgdb_rate);
   1665 	zs_reset(zc, 1, kgdb_rate);
   1666 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
   1667 }
   1668 #endif /* KGDB */
   1669