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