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