Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.33.2.1
      1 /*	$NetBSD: zs.c,v 1.33.2.1 2001/06/21 19:20:23 nathanw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 L. Weppelman (Atari modifications)
      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  *
     13  * All advertising materials mentioning features or use of this software
     14  * must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Lawrence Berkeley Laboratory.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. All advertising materials mentioning features or use of this software
     27  *    must display the following acknowledgement:
     28  *	This product includes software developed by the University of
     29  *	California, Berkeley and its contributors.
     30  * 4. Neither the name of the University nor the names of its contributors
     31  *    may be used to endorse or promote products derived from this software
     32  *    without specific prior written permission.
     33  *
     34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     44  * SUCH DAMAGE.
     45  *
     46  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     47  */
     48 
     49 /*
     50  * Zilog Z8530 (ZSCC) driver.
     51  *
     52  * Runs two tty ports (modem2 and serial2) on zs0.
     53  *
     54  * This driver knows far too much about chip to usage mappings.
     55  */
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/proc.h>
     59 #include <sys/device.h>
     60 #include <sys/conf.h>
     61 #include <sys/file.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/malloc.h>
     64 #include <sys/tty.h>
     65 #include <sys/time.h>
     66 #include <sys/kernel.h>
     67 #include <sys/syslog.h>
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/iomap.h>
     71 #include <machine/scu.h>
     72 #include <machine/mfp.h>
     73 #include <atari/dev/ym2149reg.h>
     74 
     75 #include <dev/ic/z8530reg.h>
     76 #include <atari/dev/zsvar.h>
     77 #include "zs.h"
     78 #if NZS > 1
     79 #error "This driver supports only 1 85C30!"
     80 #endif
     81 
     82 #if NZS > 0
     83 
     84 #define PCLK	(8053976)	/* PCLK pin input clock rate */
     85 #define PCLK_HD	(9600 * 1536)	/* PCLK on Hades pin input clock rate */
     86 
     87 #define splzs	spl5
     88 
     89 /*
     90  * Software state per found chip.
     91  */
     92 struct zs_softc {
     93     struct	device		zi_dev;    /* base device		  */
     94     volatile struct zsdevice	*zi_zs;    /* chip registers		  */
     95     struct	zs_chanstate	zi_cs[2];  /* chan A and B software state */
     96 };
     97 
     98 static u_char	cb_scheduled = 0;	/* Already asked for callback? */
     99 /*
    100  * Define the registers for a closed port
    101  */
    102 static u_char zs_init_regs[16] = {
    103 /*  0 */	0,
    104 /*  1 */	0,
    105 /*  2 */	0x60,
    106 /*  3 */	0,
    107 /*  4 */	0,
    108 /*  5 */	0,
    109 /*  6 */	0,
    110 /*  7 */	0,
    111 /*  8 */	0,
    112 /*  9 */	ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT,
    113 /* 10 */	ZSWR10_NRZ,
    114 /* 11 */	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    115 /* 12 */	0,
    116 /* 13 */	0,
    117 /* 14 */	ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
    118 /* 15 */	0
    119 };
    120 
    121 /*
    122  * Define the machine dependant clock frequencies
    123  * If BRgen feeds sender/receiver we always use a
    124  * divisor 16, therefor the division by 16 can as
    125  * well be done here.
    126  */
    127 static u_long zs_freqs_tt[] = {
    128 	/*
    129 	 * Atari TT, RTxCB is generated by TT-MFP timer C,
    130 	 * which is set to 307.2KHz during initialisation
    131 	 * and never changed afterwards.
    132 	 */
    133 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    134 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    135 	3672000,	/* RTxCA, from PCLK4		*/
    136 	      0,	/* TRxCA, external		*/
    137 
    138 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    139 	  19200,	/* BRgen, RTxCB, divisor 16	*/
    140 	 307200,	/* RTxCB, from TT-MFP TCO	*/
    141 	2457600		/* TRxCB, from BCLK		*/
    142 };
    143 
    144 static u_long zs_freqs_falcon[] = {
    145 	/*
    146 	 * Atari Falcon, XXX no specs available, this might be wrong
    147 	 */
    148 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    149 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    150 	3672000,	/* RTxCA, ???			*/
    151 	      0,	/* TRxCA, external		*/
    152 
    153 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    154 	 229500,	/* BRgen, RTxCB, divisor 16	*/
    155 	3672000,	/* RTxCB, ???			*/
    156 	2457600		/* TRxCB, ???			*/
    157 };
    158 
    159 static u_long zs_freqs_hades[] = {
    160 	/*
    161 	 * XXX: Channel-A unchecked!!!!!
    162 	 */
    163      PCLK_HD/16,	/* BRgen, PCLK,  divisor 16	*/
    164 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    165 	3672000,	/* RTxCA, from PCLK4		*/
    166 	      0,	/* TRxCA, external		*/
    167 
    168      PCLK_HD/16,	/* BRgen, PCLK,  divisor 16	*/
    169 	 235550,	/* BRgen, RTxCB, divisor 16	*/
    170 	3768800,	/* RTxCB, 3.7688MHz		*/
    171 	3768800		/* TRxCB, 3.7688MHz		*/
    172 };
    173 
    174 static u_long zs_freqs_generic[] = {
    175 	/*
    176 	 * other machines, assume only PCLK is available
    177 	 */
    178 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    179 	      0,	/* BRgen, RTxCA, divisor 16	*/
    180 	      0,	/* RTxCA, unknown		*/
    181 	      0,	/* TRxCA, unknown		*/
    182 
    183 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    184 	      0,	/* BRgen, RTxCB, divisor 16	*/
    185 	      0,	/* RTxCB, unknown		*/
    186 	      0		/* TRxCB, unknown		*/
    187 };
    188 static u_long *zs_frequencies;
    189 
    190 /* Definition of the driver for autoconfig. */
    191 static int	zsmatch __P((struct device *, struct cfdata *, void *));
    192 static void	zsattach __P((struct device *, struct device *, void *));
    193 
    194 struct cfattach zs_ca = {
    195 	sizeof(struct zs_softc), zsmatch, zsattach
    196 };
    197 
    198 extern struct cfdriver zs_cd;
    199 
    200 /* {b,c}devsw[] function prototypes */
    201 dev_type_open(zsopen);
    202 dev_type_close(zsclose);
    203 dev_type_read(zsread);
    204 dev_type_write(zswrite);
    205 dev_type_poll(zspoll);
    206 dev_type_ioctl(zsioctl);
    207 dev_type_tty(zstty);
    208 
    209 /* Interrupt handlers. */
    210 int		zshard __P((long));
    211 static int	zssoft __P((long));
    212 static int	zsrint __P((struct zs_chanstate *, volatile struct zschan *));
    213 static int	zsxint __P((struct zs_chanstate *, volatile struct zschan *));
    214 static int	zssint __P((struct zs_chanstate *, volatile struct zschan *));
    215 
    216 static struct zs_chanstate *zslist;
    217 
    218 /* Routines called from other code. */
    219 static void	zsstart __P((struct tty *));
    220 void		zsstop __P((struct tty *, int));
    221 
    222 /* Routines purely local to this driver. */
    223 static void	zsoverrun __P((int, long *, char *));
    224 static int	zsparam __P((struct tty *, struct termios *));
    225 static int	zsbaudrate __P((int, int, int *, int *, int *, int *));
    226 static int	zs_modem __P((struct zs_chanstate *, int, int));
    227 static void	zs_loadchannelregs __P((volatile struct zschan *, u_char *));
    228 static void	zs_shutdown __P((struct zs_chanstate *));
    229 
    230 static int zsshortcuts;	/* number of "shortcut" software interrupts */
    231 
    232 static int
    233 zsmatch(pdp, cfp, auxp)
    234 struct device	*pdp;
    235 struct cfdata	*cfp;
    236 void		*auxp;
    237 {
    238 	static int	zs_matched = 0;
    239 
    240 	if(strcmp("zs", auxp) || zs_matched)
    241 		return(0);
    242 	zs_matched = 1;
    243 	return(1);
    244 }
    245 
    246 /*
    247  * Attach a found zs.
    248  */
    249 static void
    250 zsattach(parent, dev, aux)
    251 struct device	*parent;
    252 struct device	*dev;
    253 void		*aux;
    254 {
    255 	register struct zs_softc		*zi;
    256 	register struct zs_chanstate		*cs;
    257 	register volatile struct zsdevice	*addr;
    258 		 char				tmp;
    259 
    260 	addr      = (struct zsdevice *)AD_SCC;
    261 	zi        = (struct zs_softc *)dev;
    262 	zi->zi_zs = addr;
    263 	cs        = zi->zi_cs;
    264 
    265 	/*
    266 	 * Get the command register into a known state.
    267 	 */
    268 	tmp = addr->zs_chan[ZS_CHAN_A].zc_csr;
    269 	tmp = addr->zs_chan[ZS_CHAN_A].zc_csr;
    270 	tmp = addr->zs_chan[ZS_CHAN_B].zc_csr;
    271 	tmp = addr->zs_chan[ZS_CHAN_B].zc_csr;
    272 
    273 	/*
    274 	 * Do a hardware reset.
    275 	 */
    276 	ZS_WRITE(&addr->zs_chan[ZS_CHAN_A], 9, ZSWR9_HARD_RESET);
    277 	delay(50000);	/*enough ? */
    278 	ZS_WRITE(&addr->zs_chan[ZS_CHAN_A], 9, 0);
    279 
    280 	/*
    281 	 * Initialize both channels
    282 	 */
    283 	zs_loadchannelregs(&addr->zs_chan[ZS_CHAN_A], zs_init_regs);
    284 	zs_loadchannelregs(&addr->zs_chan[ZS_CHAN_B], zs_init_regs);
    285 
    286 	if(machineid & ATARI_TT) {
    287 		/*
    288 		 * ininitialise TT-MFP timer C: 307200Hz
    289 		 * timer C and D share one control register:
    290 		 *	bits 0-2 control timer D
    291 		 *	bits 4-6 control timer C
    292 		 */
    293 		int cr = MFP2->mf_tcdcr & 7;
    294 		MFP2->mf_tcdcr = cr;		/* stop timer C  */
    295 		MFP2->mf_tcdr  = 1;		/* counter 1     */
    296 		cr |= T_Q004 << 4;		/* divisor 4     */
    297 		MFP2->mf_tcdcr = cr;		/* start timer C */
    298 		/*
    299 		 * enable scc related interrupts
    300 		 */
    301 		SCU->vme_mask |= SCU_SCC;
    302 
    303 		zs_frequencies = zs_freqs_tt;
    304 	} else if (machineid & ATARI_FALCON) {
    305 		zs_frequencies = zs_freqs_falcon;
    306 	} else if (machineid & ATARI_HADES) {
    307 		zs_frequencies = zs_freqs_hades;
    308 	} else {
    309 		zs_frequencies = zs_freqs_generic;
    310 	}
    311 
    312 	/* link into interrupt list with order (A,B) (B=A+1) */
    313 	cs[0].cs_next = &cs[1];
    314 	cs[1].cs_next = zslist;
    315 	zslist        = cs;
    316 
    317 	cs->cs_unit  = 0;
    318 	cs->cs_zc    = &addr->zs_chan[ZS_CHAN_A];
    319 	cs++;
    320 	cs->cs_unit  = 1;
    321 	cs->cs_zc    = &addr->zs_chan[ZS_CHAN_B];
    322 
    323 	printf(": serial2 on channel a and modem2 on channel b\n");
    324 }
    325 
    326 /*
    327  * Open a zs serial port.
    328  */
    329 int
    330 zsopen(dev, flags, mode, p)
    331 dev_t		dev;
    332 int		flags;
    333 int		mode;
    334 struct proc	*p;
    335 {
    336 	register struct tty		*tp;
    337 	register struct zs_chanstate	*cs;
    338 		 struct zs_softc	*zi;
    339 		 int			unit = ZS_UNIT(dev);
    340 		 int			zs = unit >> 1;
    341 		 int			error, s;
    342 
    343 	if(zs >= zs_cd.cd_ndevs || (zi = zs_cd.cd_devs[zs]) == NULL)
    344 		return (ENXIO);
    345 	cs = &zi->zi_cs[unit & 1];
    346 
    347 	/*
    348 	 * When port A (ser02) is selected on the TT, make sure
    349 	 * the port is enabled.
    350 	 */
    351 	if((machineid & ATARI_TT) && !(unit & 1))
    352 		ym2149_ser2(1);
    353 
    354 	if (cs->cs_rbuf == NULL) {
    355 		cs->cs_rbuf = malloc(ZLRB_RING_SIZE * sizeof(int), M_DEVBUF,
    356 								   M_WAITOK);
    357 	}
    358 
    359 	tp = cs->cs_ttyp;
    360 	if(tp == NULL) {
    361 		cs->cs_ttyp = tp = ttymalloc();
    362 		tty_attach(tp);
    363 		tp->t_dev   = dev;
    364 		tp->t_oproc = zsstart;
    365 		tp->t_param = zsparam;
    366 	}
    367 
    368 	if ((tp->t_state & TS_ISOPEN) &&
    369 	    (tp->t_state & TS_XCLUDE) &&
    370 	    p->p_ucred->cr_uid != 0)
    371 		return (EBUSY);
    372 
    373 	s  = spltty();
    374 
    375 	/*
    376 	 * Do the following iff this is a first open.
    377 	 */
    378 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    379 		if(tp->t_ispeed == 0) {
    380 			tp->t_iflag = TTYDEF_IFLAG;
    381 			tp->t_oflag = TTYDEF_OFLAG;
    382 			tp->t_cflag = TTYDEF_CFLAG;
    383 			tp->t_lflag = TTYDEF_LFLAG;
    384 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    385 		}
    386 		ttychars(tp);
    387 		ttsetwater(tp);
    388 
    389 		(void)zsparam(tp, &tp->t_termios);
    390 
    391 		/*
    392 		 * Turn on DTR.  We must always do this, even if carrier is not
    393 		 * present, because otherwise we'd have to use TIOCSDTR
    394 		 * immediately after setting CLOCAL, which applications do not
    395 		 * expect.  We always assert DTR while the device is open
    396 		 * unless explicitly requested to deassert it.
    397 		 */
    398 		zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR, DMSET);
    399 		/* May never get a status intr. if DCD already on. -gwr */
    400 		if((cs->cs_rr0 = cs->cs_zc->zc_csr) & ZSRR0_DCD)
    401 			tp->t_state |= TS_CARR_ON;
    402 		if(cs->cs_softcar)
    403 			tp->t_state |= TS_CARR_ON;
    404 	}
    405 
    406 	splx(s);
    407 
    408 	error = ttyopen(tp, ZS_DIALOUT(dev), (flags & O_NONBLOCK));
    409 	if (error)
    410 		goto bad;
    411 
    412 	error = tp->t_linesw->l_open(dev, tp);
    413 	if(error)
    414 		goto bad;
    415 	return (0);
    416 
    417 bad:
    418 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    419 		/*
    420 		 * We failed to open the device, and nobody else had it opened.
    421 		 * Clean up the state as appropriate.
    422 		 */
    423 		zs_shutdown(cs);
    424 	}
    425 	return(error);
    426 }
    427 
    428 /*
    429  * Close a zs serial port.
    430  */
    431 int
    432 zsclose(dev, flags, mode, p)
    433 dev_t		dev;
    434 int		flags;
    435 int		mode;
    436 struct proc	*p;
    437 {
    438 	register struct zs_chanstate	*cs;
    439 	register struct tty		*tp;
    440 		 struct zs_softc	*zi;
    441 		 int			unit = ZS_UNIT(dev);
    442 
    443 	zi = zs_cd.cd_devs[unit >> 1];
    444 	cs = &zi->zi_cs[unit & 1];
    445 	tp = cs->cs_ttyp;
    446 
    447 	tp->t_linesw->l_close(tp, flags);
    448 	ttyclose(tp);
    449 
    450 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    451 		/*
    452 		 * Although we got a last close, the device may still be in
    453 		 * use; e.g. if this was the dialout node, and there are still
    454 		 * processes waiting for carrier on the non-dialout node.
    455 		 */
    456 		zs_shutdown(cs);
    457 	}
    458 	return (0);
    459 }
    460 
    461 /*
    462  * Read/write zs serial port.
    463  */
    464 int
    465 zsread(dev, uio, flags)
    466 dev_t		dev;
    467 struct uio	*uio;
    468 int		flags;
    469 {
    470 	register struct zs_chanstate	*cs;
    471 	register struct zs_softc	*zi;
    472 	register struct tty		*tp;
    473 		 int			unit;
    474 
    475 	unit = ZS_UNIT(dev);
    476 	zi   = zs_cd.cd_devs[unit >> 1];
    477 	cs   = &zi->zi_cs[unit & 1];
    478 	tp   = cs->cs_ttyp;
    479 
    480 	return(tp->t_linesw->l_read(tp, uio, flags));
    481 }
    482 
    483 int
    484 zswrite(dev, uio, flags)
    485 dev_t		dev;
    486 struct uio	*uio;
    487 int		flags;
    488 {
    489 	register struct zs_chanstate	*cs;
    490 	register struct zs_softc	*zi;
    491 	register struct tty		*tp;
    492 		 int			unit;
    493 
    494 	unit = ZS_UNIT(dev);
    495 	zi   = zs_cd.cd_devs[unit >> 1];
    496 	cs   = &zi->zi_cs[unit & 1];
    497 	tp   = cs->cs_ttyp;
    498 
    499 	return(tp->t_linesw->l_write(tp, uio, flags));
    500 }
    501 
    502 int
    503 zspoll(dev, events, p)
    504 dev_t		dev;
    505 int		events;
    506 struct proc	*p;
    507 {
    508 	register struct zs_chanstate	*cs;
    509 	register struct zs_softc	*zi;
    510 	register struct tty		*tp;
    511 		 int			unit;
    512 
    513 	unit = ZS_UNIT(dev);
    514 	zi   = zs_cd.cd_devs[unit >> 1];
    515 	cs   = &zi->zi_cs[unit & 1];
    516 	tp   = cs->cs_ttyp;
    517 
    518 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    519 }
    520 
    521 struct tty *
    522 zstty(dev)
    523 dev_t	dev;
    524 {
    525 	register struct zs_chanstate	*cs;
    526 	register struct zs_softc	*zi;
    527 		 int			unit;
    528 
    529 	unit = ZS_UNIT(dev);
    530 	zi   = zs_cd.cd_devs[unit >> 1];
    531 	cs   = &zi->zi_cs[unit & 1];
    532 	return(cs->cs_ttyp);
    533 }
    534 
    535 /*
    536  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    537  * channels are kept in (A,B) pairs.
    538  *
    539  * Do just a little, then get out; set a software interrupt if more
    540  * work is needed.
    541  *
    542  * We deliberately ignore the vectoring Zilog gives us, and match up
    543  * only the number of `reset interrupt under service' operations, not
    544  * the order.
    545  */
    546 
    547 int
    548 zshard(sr)
    549 long sr;
    550 {
    551 	register struct zs_chanstate	*a;
    552 #define	b (a + 1)
    553 	register volatile struct zschan *zc;
    554 	register int			rr3, intflags = 0, v, i;
    555 
    556 	do {
    557 	    intflags &= ~4;
    558 	    for(a = zslist; a != NULL; a = b->cs_next) {
    559 		rr3 = ZS_READ(a->cs_zc, 3);
    560 		if(rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    561 			intflags |= 4|2;
    562 			zc = a->cs_zc;
    563 			i  = a->cs_rbput;
    564 			if(rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    565 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    566 				intflags |= 1;
    567 			}
    568 			if(rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    569 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    570 				intflags |= 1;
    571 			}
    572 			if(rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    573 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    574 				intflags |= 1;
    575 			}
    576 			a->cs_rbput = i;
    577 		}
    578 		if(rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    579 			intflags |= 4|2;
    580 			zc = b->cs_zc;
    581 			i  = b->cs_rbput;
    582 			if(rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    583 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    584 				intflags |= 1;
    585 			}
    586 			if(rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    587 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    588 				intflags |= 1;
    589 			}
    590 			if(rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    591 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    592 				intflags |= 1;
    593 			}
    594 			b->cs_rbput = i;
    595 		}
    596 	    }
    597 	} while(intflags & 4);
    598 #undef b
    599 
    600 	if(intflags & 1) {
    601 		if(BASEPRI(sr)) {
    602 			spl1();
    603 			zsshortcuts++;
    604 			return(zssoft(sr));
    605 		}
    606 		else if(!cb_scheduled) {
    607 			cb_scheduled++;
    608 			add_sicallback((si_farg)zssoft, 0, 0);
    609 		}
    610 	}
    611 	return(intflags & 2);
    612 }
    613 
    614 static int
    615 zsrint(cs, zc)
    616 register struct zs_chanstate	*cs;
    617 register volatile struct zschan	*zc;
    618 {
    619 	register int c;
    620 
    621 	/*
    622 	 * First read the status, because read of the received char
    623 	 * destroy the status of this char.
    624 	 */
    625 	c = ZS_READ(zc, 1);
    626 	c |= (zc->zc_data << 8);
    627 
    628 	/* clear receive error & interrupt condition */
    629 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    630 	zc->zc_csr = ZSWR0_CLR_INTR;
    631 
    632 	return(ZRING_MAKE(ZRING_RINT, c));
    633 }
    634 
    635 static int
    636 zsxint(cs, zc)
    637 register struct zs_chanstate	*cs;
    638 register volatile struct zschan	*zc;
    639 {
    640 	register int i = cs->cs_tbc;
    641 
    642 	if(i == 0) {
    643 		zc->zc_csr = ZSWR0_RESET_TXINT;
    644 		zc->zc_csr = ZSWR0_CLR_INTR;
    645 		return(ZRING_MAKE(ZRING_XINT, 0));
    646 	}
    647 	cs->cs_tbc = i - 1;
    648 	zc->zc_data = *cs->cs_tba++;
    649 	zc->zc_csr = ZSWR0_CLR_INTR;
    650 	return (0);
    651 }
    652 
    653 static int
    654 zssint(cs, zc)
    655 register struct zs_chanstate	*cs;
    656 register volatile struct zschan	*zc;
    657 {
    658 	register int rr0;
    659 
    660 	rr0 = zc->zc_csr;
    661 	zc->zc_csr = ZSWR0_RESET_STATUS;
    662 	zc->zc_csr = ZSWR0_CLR_INTR;
    663 	/*
    664 	 * The chip's hardware flow control is, as noted in zsreg.h,
    665 	 * busted---if the DCD line goes low the chip shuts off the
    666 	 * receiver (!).  If we want hardware CTS flow control but do
    667 	 * not have it, and carrier is now on, turn HFC on; if we have
    668 	 * HFC now but carrier has gone low, turn it off.
    669 	 */
    670 	if(rr0 & ZSRR0_DCD) {
    671 		if(cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    672 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    673 			cs->cs_creg[3] |= ZSWR3_HFC;
    674 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    675 		}
    676 	}
    677 	else {
    678 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    679 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    680 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    681 		}
    682 	}
    683 	return(ZRING_MAKE(ZRING_SINT, rr0));
    684 }
    685 
    686 /*
    687  * Print out a ring or fifo overrun error message.
    688  */
    689 static void
    690 zsoverrun(unit, ptime, what)
    691 int	unit;
    692 long	*ptime;
    693 char	*what;
    694 {
    695 
    696 	if(*ptime != time.tv_sec) {
    697 		*ptime = time.tv_sec;
    698 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    699 		    (unit & 1) + 'a', what);
    700 	}
    701 }
    702 
    703 /*
    704  * ZS software interrupt.  Scan all channels for deferred interrupts.
    705  */
    706 int
    707 zssoft(sr)
    708 long sr;
    709 {
    710     register struct zs_chanstate	*cs;
    711     register volatile struct zschan	*zc;
    712     register struct linesw		*line;
    713     register struct tty			*tp;
    714     register int			get, n, c, cc, unit, s;
    715  	     int			retval = 0;
    716 
    717     cb_scheduled = 0;
    718     s = spltty();
    719     for(cs = zslist; cs != NULL; cs = cs->cs_next) {
    720 	get = cs->cs_rbget;
    721 again:
    722 	n = cs->cs_rbput;	/* atomic			*/
    723 	if(get == n)		/* nothing more on this line	*/
    724 		continue;
    725 	retval = 1;
    726 	unit   = cs->cs_unit;	/* set up to handle interrupts	*/
    727 	zc     = cs->cs_zc;
    728 	tp     = cs->cs_ttyp;
    729 	line   = tp->t_linesw;
    730 	/*
    731 	 * Compute the number of interrupts in the receive ring.
    732 	 * If the count is overlarge, we lost some events, and
    733 	 * must advance to the first valid one.  It may get
    734 	 * overwritten if more data are arriving, but this is
    735 	 * too expensive to check and gains nothing (we already
    736 	 * lost out; all we can do at this point is trade one
    737 	 * kind of loss for another).
    738 	 */
    739 	n -= get;
    740 	if(n > ZLRB_RING_SIZE) {
    741 		zsoverrun(unit, &cs->cs_rotime, "ring");
    742 		get += n - ZLRB_RING_SIZE;
    743 		n    = ZLRB_RING_SIZE;
    744 	}
    745 	while(--n >= 0) {
    746 		/* race to keep ahead of incoming interrupts */
    747 		c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
    748 		switch (ZRING_TYPE(c)) {
    749 
    750 		case ZRING_RINT:
    751 			c = ZRING_VALUE(c);
    752 			if(c & ZSRR1_DO)
    753 				zsoverrun(unit, &cs->cs_fotime, "fifo");
    754 			cc = c >> 8;
    755 			if(c & ZSRR1_FE)
    756 				cc |= TTY_FE;
    757 			if(c & ZSRR1_PE)
    758 				cc |= TTY_PE;
    759 			line->l_rint(cc, tp);
    760 			break;
    761 
    762 		case ZRING_XINT:
    763 			/*
    764 			 * Transmit done: change registers and resume,
    765 			 * or clear BUSY.
    766 			 */
    767 			if(cs->cs_heldchange) {
    768 				int sps;
    769 
    770 				sps = splzs();
    771 				c = zc->zc_csr;
    772 				if((c & ZSRR0_DCD) == 0)
    773 					cs->cs_preg[3] &= ~ZSWR3_HFC;
    774 				bcopy((caddr_t)cs->cs_preg,
    775 				    (caddr_t)cs->cs_creg, 16);
    776 				zs_loadchannelregs(zc, cs->cs_creg);
    777 				splx(sps);
    778 				cs->cs_heldchange = 0;
    779 				if(cs->cs_heldtbc
    780 					&& (tp->t_state & TS_TTSTOP) == 0) {
    781 					cs->cs_tbc = cs->cs_heldtbc - 1;
    782 					zc->zc_data = *cs->cs_tba++;
    783 					goto again;
    784 				}
    785 			}
    786 			tp->t_state &= ~TS_BUSY;
    787 			if(tp->t_state & TS_FLUSH)
    788 				tp->t_state &= ~TS_FLUSH;
    789 			else ndflush(&tp->t_outq,cs->cs_tba
    790 						- (caddr_t)tp->t_outq.c_cf);
    791 			line->l_start(tp);
    792 			break;
    793 
    794 		case ZRING_SINT:
    795 			/*
    796 			 * Status line change.  HFC bit is run in
    797 			 * hardware interrupt, to avoid locking
    798 			 * at splzs here.
    799 			 */
    800 			c = ZRING_VALUE(c);
    801 			if((c ^ cs->cs_rr0) & ZSRR0_DCD) {
    802 				cc = (c & ZSRR0_DCD) != 0;
    803 				if(line->l_modem(tp, cc) == 0)
    804 					zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR,
    805 							cc ? DMBIS : DMBIC);
    806 			}
    807 			cs->cs_rr0 = c;
    808 			break;
    809 
    810 		default:
    811 			log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
    812 			    unit >> 1, (unit & 1) + 'a', c);
    813 			break;
    814 		}
    815 	}
    816 	cs->cs_rbget = get;
    817 	goto again;
    818     }
    819     splx(s);
    820     return (retval);
    821 }
    822 
    823 int
    824 zsioctl(dev, cmd, data, flag, p)
    825 dev_t		dev;
    826 u_long		cmd;
    827 caddr_t		data;
    828 int		flag;
    829 struct proc	*p;
    830 {
    831 		 int			unit = ZS_UNIT(dev);
    832 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
    833 	register struct tty		*tp = zi->zi_cs[unit & 1].cs_ttyp;
    834 	register int			error, s;
    835 	register struct zs_chanstate	*cs = &zi->zi_cs[unit & 1];
    836 
    837 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p);
    838 	if(error >= 0)
    839 		return(error);
    840 	error = ttioctl(tp, cmd, data, flag, p);
    841 	if(error >= 0)
    842 		return (error);
    843 
    844 	switch (cmd) {
    845 	case TIOCSBRK:
    846 		s = splzs();
    847 		cs->cs_preg[5] |= ZSWR5_BREAK;
    848 		cs->cs_creg[5] |= ZSWR5_BREAK;
    849 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    850 		splx(s);
    851 		break;
    852 	case TIOCCBRK:
    853 		s = splzs();
    854 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    855 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    856 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    857 		splx(s);
    858 		break;
    859 	case TIOCGFLAGS: {
    860 		int bits = 0;
    861 
    862 		if(cs->cs_softcar)
    863 			bits |= TIOCFLAG_SOFTCAR;
    864 		if(cs->cs_creg[15] & ZSWR15_DCD_IE)
    865 			bits |= TIOCFLAG_CLOCAL;
    866 		if(cs->cs_creg[3] & ZSWR3_HFC)
    867 			bits |= TIOCFLAG_CRTSCTS;
    868 		*(int *)data = bits;
    869 		break;
    870 	}
    871 	case TIOCSFLAGS: {
    872 		int userbits = 0;
    873 
    874 		error = suser(p->p_ucred, &p->p_acflag);
    875 		if(error != 0)
    876 			return (EPERM);
    877 
    878 		userbits = *(int *)data;
    879 
    880 		/*
    881 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
    882 		 # defaulting to software flow control.
    883 		 */
    884 		if(userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
    885 			return(EINVAL);
    886 		if(userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
    887 			return(ENODEV);
    888 
    889 		s = splzs();
    890 		if((userbits & TIOCFLAG_SOFTCAR)) {
    891 			cs->cs_softcar = 1;	/* turn on softcar */
    892 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
    893 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
    894 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    895 		}
    896 		else if(userbits & TIOCFLAG_CLOCAL) {
    897 			cs->cs_softcar = 0; 	/* turn off softcar */
    898 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
    899 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
    900 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    901 			tp->t_termios.c_cflag |= CLOCAL;
    902 		}
    903 		if(userbits & TIOCFLAG_CRTSCTS) {
    904 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
    905 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
    906 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    907 			cs->cs_preg[3] |= ZSWR3_HFC;
    908 			cs->cs_creg[3] |= ZSWR3_HFC;
    909 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    910 			tp->t_termios.c_cflag |= CRTSCTS;
    911 		}
    912 		else {
    913 			/* no mdmbuf, so we must want software flow control */
    914 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    915 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
    916 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    917 			cs->cs_preg[3] &= ~ZSWR3_HFC;
    918 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    919 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    920 			tp->t_termios.c_cflag &= ~CRTSCTS;
    921 		}
    922 		splx(s);
    923 		break;
    924 	}
    925 	case TIOCSDTR:
    926 		zs_modem(cs, ZSWR5_DTR, DMBIS);
    927 		break;
    928 	case TIOCCDTR:
    929 		zs_modem(cs, ZSWR5_DTR, DMBIC);
    930 		break;
    931 	case TIOCMGET:
    932 		zs_modem(cs, 0, DMGET);
    933 		break;
    934 	case TIOCMSET:
    935 	case TIOCMBIS:
    936 	case TIOCMBIC:
    937 	default:
    938 		return (ENOTTY);
    939 	}
    940 	return (0);
    941 }
    942 
    943 /*
    944  * Start or restart transmission.
    945  */
    946 static void
    947 zsstart(tp)
    948 register struct tty *tp;
    949 {
    950 	register struct zs_chanstate	*cs;
    951 	register int			s, nch;
    952 		 int			unit = ZS_UNIT(tp->t_dev);
    953 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
    954 
    955 	cs = &zi->zi_cs[unit & 1];
    956 	s  = spltty();
    957 
    958 	/*
    959 	 * If currently active or delaying, no need to do anything.
    960 	 */
    961 	if(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
    962 		goto out;
    963 
    964 	/*
    965 	 * If there are sleepers, and output has drained below low
    966 	 * water mark, awaken.
    967 	 */
    968 	if(tp->t_outq.c_cc <= tp->t_lowat) {
    969 		if(tp->t_state & TS_ASLEEP) {
    970 			tp->t_state &= ~TS_ASLEEP;
    971 			wakeup((caddr_t)&tp->t_outq);
    972 		}
    973 		selwakeup(&tp->t_wsel);
    974 	}
    975 
    976 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
    977 	if(nch) {
    978 		register char *p = tp->t_outq.c_cf;
    979 
    980 		/* mark busy, enable tx done interrupts, & send first byte */
    981 		tp->t_state |= TS_BUSY;
    982 		(void) splzs();
    983 		cs->cs_preg[1] |= ZSWR1_TIE;
    984 		cs->cs_creg[1] |= ZSWR1_TIE;
    985 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
    986 		cs->cs_zc->zc_data = *p;
    987 		cs->cs_tba = p + 1;
    988 		cs->cs_tbc = nch - 1;
    989 	} else {
    990 		/*
    991 		 * Nothing to send, turn off transmit done interrupts.
    992 		 * This is useful if something is doing polled output.
    993 		 */
    994 		(void) splzs();
    995 		cs->cs_preg[1] &= ~ZSWR1_TIE;
    996 		cs->cs_creg[1] &= ~ZSWR1_TIE;
    997 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
    998 	}
    999 out:
   1000 	splx(s);
   1001 }
   1002 
   1003 /*
   1004  * Stop output, e.g., for ^S or output flush.
   1005  */
   1006 void
   1007 zsstop(tp, flag)
   1008 register struct tty	*tp;
   1009 	 int		flag;
   1010 {
   1011 	register struct zs_chanstate	*cs;
   1012 	register int			s, unit = ZS_UNIT(tp->t_dev);
   1013 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
   1014 
   1015 	cs = &zi->zi_cs[unit & 1];
   1016 	s  = splzs();
   1017 	if(tp->t_state & TS_BUSY) {
   1018 		/*
   1019 		 * Device is transmitting; must stop it.
   1020 		 */
   1021 		cs->cs_tbc = 0;
   1022 		if ((tp->t_state & TS_TTSTOP) == 0)
   1023 			tp->t_state |= TS_FLUSH;
   1024 	}
   1025 	splx(s);
   1026 }
   1027 
   1028 static void
   1029 zs_shutdown(cs)
   1030 	struct zs_chanstate	*cs;
   1031 {
   1032 	struct tty	*tp = cs->cs_ttyp;
   1033 	int		s;
   1034 
   1035 	s = splzs();
   1036 
   1037 	/*
   1038 	 * Hang up if necessary.  Wait a bit, so the other side has time to
   1039 	 * notice even if we immediately open the port again.
   1040 	 */
   1041 	if(tp->t_cflag & HUPCL) {
   1042 		zs_modem(cs, 0, DMSET);
   1043 		(void)tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
   1044 	}
   1045 
   1046 	/* Clear any break condition set with TIOCSBRK. */
   1047 	if(cs->cs_creg[5] & ZSWR5_BREAK) {
   1048 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1049 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1050 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1051 	}
   1052 
   1053 	/*
   1054 	 * Drop all lines and cancel interrupts
   1055 	 */
   1056 	zs_loadchannelregs(cs->cs_zc, zs_init_regs);
   1057 	splx(s);
   1058 }
   1059 
   1060 /*
   1061  * Set ZS tty parameters from termios.
   1062  *
   1063  * This routine makes use of the fact that only registers
   1064  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1065  */
   1066 static int
   1067 zsparam(tp, t)
   1068 register struct tty	*tp;
   1069 register struct termios	*t;
   1070 {
   1071 		 int			unit = ZS_UNIT(tp->t_dev);
   1072 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
   1073 	register struct zs_chanstate	*cs = &zi->zi_cs[unit & 1];
   1074 		 int			cdiv, clkm, brgm, tcon;
   1075 	register int			tmp, tmp5, cflag, s;
   1076 
   1077 	tmp  = t->c_ospeed;
   1078 	tmp5 = t->c_ispeed;
   1079 	if(tmp < 0 || (tmp5 && tmp5 != tmp))
   1080 		return(EINVAL);
   1081 	if(tmp == 0) {
   1082 		/* stty 0 => drop DTR and RTS */
   1083 		zs_modem(cs, 0, DMSET);
   1084 		return(0);
   1085 	}
   1086 	tmp = zsbaudrate(unit, tmp, &cdiv, &clkm, &brgm, &tcon);
   1087 	if (tmp < 0)
   1088 		return(EINVAL);
   1089 	tp->t_ispeed = tp->t_ospeed = tmp;
   1090 
   1091 	cflag = tp->t_cflag = t->c_cflag;
   1092 	if (cflag & CSTOPB)
   1093 		cdiv |= ZSWR4_TWOSB;
   1094 	else
   1095 		cdiv |= ZSWR4_ONESB;
   1096 	if (!(cflag & PARODD))
   1097 		cdiv |= ZSWR4_EVENP;
   1098 	if (cflag & PARENB)
   1099 		cdiv |= ZSWR4_PARENB;
   1100 
   1101 	switch(cflag & CSIZE) {
   1102 	case CS5:
   1103 		tmp  = ZSWR3_RX_5;
   1104 		tmp5 = ZSWR5_TX_5;
   1105 		break;
   1106 	case CS6:
   1107 		tmp  = ZSWR3_RX_6;
   1108 		tmp5 = ZSWR5_TX_6;
   1109 		break;
   1110 	case CS7:
   1111 		tmp  = ZSWR3_RX_7;
   1112 		tmp5 = ZSWR5_TX_7;
   1113 		break;
   1114 	case CS8:
   1115 	default:
   1116 		tmp  = ZSWR3_RX_8;
   1117 		tmp5 = ZSWR5_TX_8;
   1118 		break;
   1119 	}
   1120 	tmp  |= ZSWR3_RX_ENABLE;
   1121 	tmp5 |= ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1122 
   1123 	/*
   1124 	 * Block interrupts so that state will not
   1125 	 * be altered until we are done setting it up.
   1126 	 */
   1127 	s = splzs();
   1128 	cs->cs_preg[4]  = cdiv;
   1129 	cs->cs_preg[11] = clkm;
   1130 	cs->cs_preg[12] = tcon;
   1131 	cs->cs_preg[13] = tcon >> 8;
   1132 	cs->cs_preg[14] = brgm;
   1133 	cs->cs_preg[1]  = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1134 	cs->cs_preg[9]  = ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT;
   1135 	cs->cs_preg[10] = ZSWR10_NRZ;
   1136 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1137 
   1138 	/*
   1139 	 * Output hardware flow control on the chip is horrendous: if
   1140 	 * carrier detect drops, the receiver is disabled.  Hence we
   1141 	 * can only do this when the carrier is on.
   1142 	 */
   1143 	if(cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
   1144 		tmp |= ZSWR3_HFC;
   1145 	cs->cs_preg[3] = tmp;
   1146 	cs->cs_preg[5] = tmp5;
   1147 
   1148 	/*
   1149 	 * If nothing is being transmitted, set up new current values,
   1150 	 * else mark them as pending.
   1151 	 */
   1152 	if(cs->cs_heldchange == 0) {
   1153 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1154 			cs->cs_heldtbc = cs->cs_tbc;
   1155 			cs->cs_tbc = 0;
   1156 			cs->cs_heldchange = 1;
   1157 		} else {
   1158 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1159 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1160 		}
   1161 	}
   1162 	splx(s);
   1163 	return (0);
   1164 }
   1165 
   1166 /*
   1167  * search for the best matching baudrate
   1168  */
   1169 static int
   1170 zsbaudrate(unit, wanted, divisor, clockmode, brgenmode, timeconst)
   1171 int	unit, wanted, *divisor, *clockmode, *brgenmode, *timeconst;
   1172 {
   1173 	int	bestdiff, bestbps, source;
   1174 
   1175 	bestdiff = bestbps = 0;
   1176 	unit = (unit & 1) << 2;
   1177 	for (source = 0; source < 4; ++source) {
   1178 		long	freq = zs_frequencies[unit + source];
   1179 		int	diff, bps, div, clkm, brgm, tcon;
   1180 
   1181 		bps = div = clkm = brgm = tcon = 0;
   1182 		switch (source) {
   1183 			case 0:	/* BRgen, PCLK */
   1184 				brgm = ZSWR14_BAUD_ENA|ZSWR14_BAUD_FROM_PCLK;
   1185 				break;
   1186 			case 1:	/* BRgen, RTxC */
   1187 				brgm = ZSWR14_BAUD_ENA;
   1188 				break;
   1189 			case 2: /* RTxC */
   1190 				clkm = ZSWR11_RXCLK_RTXC|ZSWR11_TXCLK_RTXC;
   1191 				break;
   1192 			case 3: /* TRxC */
   1193 				clkm = ZSWR11_RXCLK_TRXC|ZSWR11_TXCLK_TRXC;
   1194 				break;
   1195 		}
   1196 		switch (source) {
   1197 			case 0:
   1198 			case 1:
   1199 				div  = ZSWR4_CLK_X16;
   1200 				clkm = ZSWR11_RXCLK_BAUD|ZSWR11_TXCLK_BAUD;
   1201 				tcon = BPS_TO_TCONST(freq, wanted);
   1202 				if (tcon < 0)
   1203 					tcon = 0;
   1204 				bps  = TCONST_TO_BPS(freq, tcon);
   1205 				break;
   1206 			case 2:
   1207 			case 3:
   1208 			{	int	b1 = freq / 16, d1 = abs(b1 - wanted);
   1209 				int	b2 = freq / 32, d2 = abs(b2 - wanted);
   1210 				int	b3 = freq / 64, d3 = abs(b3 - wanted);
   1211 
   1212 				if (d1 < d2 && d1 < d3) {
   1213 					div = ZSWR4_CLK_X16;
   1214 					bps = b1;
   1215 				} else if (d2 < d3 && d2 < d1) {
   1216 					div = ZSWR4_CLK_X32;
   1217 					bps = b2;
   1218 				} else {
   1219 					div = ZSWR4_CLK_X64;
   1220 					bps = b3;
   1221 				}
   1222 				brgm = tcon = 0;
   1223 				break;
   1224 			}
   1225 		}
   1226 		diff = abs(bps - wanted);
   1227 		if (!source || diff < bestdiff) {
   1228 			*divisor   = div;
   1229 			*clockmode = clkm;
   1230 			*brgenmode = brgm;
   1231 			*timeconst = tcon;
   1232 			bestbps    = bps;
   1233 			bestdiff   = diff;
   1234 			if (diff == 0)
   1235 				break;
   1236 		}
   1237 	}
   1238 	/* Allow deviations upto 5% */
   1239 	if (20 * bestdiff > wanted)
   1240 		return -1;
   1241 	return bestbps;
   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 int
   1249 zs_modem(cs, bits, how)
   1250 struct zs_chanstate	*cs;
   1251 int			bits, how;
   1252 {
   1253 	int s, mbits;
   1254 
   1255 	bits  &= ZSWR5_DTR | ZSWR5_RTS;
   1256 
   1257 	s = splzs();
   1258 	mbits  = cs->cs_preg[5] &  (ZSWR5_DTR | ZSWR5_RTS);
   1259 
   1260 	switch(how) {
   1261 		case DMSET:
   1262 				mbits  = bits;
   1263 				break;
   1264 		case DMBIS:
   1265 				mbits |= bits;
   1266 				break;
   1267 		case DMBIC:
   1268 				mbits &= ~bits;
   1269 				break;
   1270 		case DMGET:
   1271 				splx(s);
   1272 				return(mbits);
   1273 	}
   1274 
   1275 	cs->cs_preg[5] = (cs->cs_preg[5] & ~(ZSWR5_DTR | ZSWR5_RTS)) | mbits;
   1276 	if(cs->cs_heldchange == 0) {
   1277 		if(cs->cs_ttyp->t_state & TS_BUSY) {
   1278 			cs->cs_heldtbc = cs->cs_tbc;
   1279 			cs->cs_tbc = 0;
   1280 			cs->cs_heldchange = 1;
   1281 		}
   1282 		else {
   1283 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1284 		}
   1285 	}
   1286 	splx(s);
   1287 	return(0);
   1288 }
   1289 
   1290 /*
   1291  * Write the given register set to the given zs channel in the proper order.
   1292  * The channel must not be transmitting at the time.  The receiver will
   1293  * be disabled for the time it takes to write all the registers.
   1294  */
   1295 static void
   1296 zs_loadchannelregs(zc, reg)
   1297 volatile struct zschan	*zc;
   1298 u_char			*reg;
   1299 {
   1300 	int i;
   1301 
   1302 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1303 	i = zc->zc_data;		/* drain fifo */
   1304 	i = zc->zc_data;
   1305 	i = zc->zc_data;
   1306 	ZS_WRITE(zc,  4, reg[4]);
   1307 	ZS_WRITE(zc, 10, reg[10]);
   1308 	ZS_WRITE(zc,  3, reg[3] & ~ZSWR3_RX_ENABLE);
   1309 	ZS_WRITE(zc,  5, reg[5] & ~ZSWR5_TX_ENABLE);
   1310 	ZS_WRITE(zc,  1, reg[1]);
   1311 	ZS_WRITE(zc,  9, reg[9]);
   1312 	ZS_WRITE(zc, 11, reg[11]);
   1313 	ZS_WRITE(zc, 12, reg[12]);
   1314 	ZS_WRITE(zc, 13, reg[13]);
   1315 	ZS_WRITE(zc, 14, reg[14]);
   1316 	ZS_WRITE(zc, 15, reg[15]);
   1317 	ZS_WRITE(zc,  3, reg[3]);
   1318 	ZS_WRITE(zc,  5, reg[5]);
   1319 }
   1320 #endif /* NZS > 1 */
   1321