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