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