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