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