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