Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.53
      1 /*	$NetBSD: zs.c,v 1.53 2007/03/06 14:13:02 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  * 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.53 2007/03/06 14:13:02 tsutsui 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 (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    405 		return (EBUSY);
    406 
    407 	s  = spltty();
    408 
    409 	/*
    410 	 * Do the following iff this is a first open.
    411 	 */
    412 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    413 		if(tp->t_ispeed == 0) {
    414 			tp->t_iflag = TTYDEF_IFLAG;
    415 			tp->t_oflag = TTYDEF_OFLAG;
    416 			tp->t_cflag = TTYDEF_CFLAG;
    417 			tp->t_lflag = TTYDEF_LFLAG;
    418 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    419 		}
    420 		ttychars(tp);
    421 		ttsetwater(tp);
    422 
    423 		(void)zsparam(tp, &tp->t_termios);
    424 
    425 		/*
    426 		 * Turn on DTR.  We must always do this, even if carrier is not
    427 		 * present, because otherwise we'd have to use TIOCSDTR
    428 		 * immediately after setting CLOCAL, which applications do not
    429 		 * expect.  We always assert DTR while the device is open
    430 		 * unless explicitly requested to deassert it.
    431 		 */
    432 		zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR, DMSET);
    433 		/* May never get a status intr. if DCD already on. -gwr */
    434 		if((cs->cs_rr0 = cs->cs_zc->zc_csr) & ZSRR0_DCD)
    435 			tp->t_state |= TS_CARR_ON;
    436 		if(cs->cs_softcar)
    437 			tp->t_state |= TS_CARR_ON;
    438 	}
    439 
    440 	splx(s);
    441 
    442 	error = ttyopen(tp, ZS_DIALOUT(dev), (flags & O_NONBLOCK));
    443 	if (error)
    444 		goto bad;
    445 
    446 	error = tp->t_linesw->l_open(dev, tp);
    447 	if(error)
    448 		goto bad;
    449 	return (0);
    450 
    451 bad:
    452 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    453 		/*
    454 		 * We failed to open the device, and nobody else had it opened.
    455 		 * Clean up the state as appropriate.
    456 		 */
    457 		zs_shutdown(cs);
    458 	}
    459 	return(error);
    460 }
    461 
    462 /*
    463  * Close a zs serial port.
    464  */
    465 int
    466 zsclose(dev, flags, mode, l)
    467 dev_t		dev;
    468 int		flags;
    469 int		mode;
    470 struct lwp	*l;
    471 {
    472 	register struct zs_chanstate	*cs;
    473 	register struct tty		*tp;
    474 		 struct zs_softc	*zi;
    475 		 int			unit = ZS_UNIT(dev);
    476 
    477 	zi = zs_cd.cd_devs[unit >> 1];
    478 	cs = &zi->zi_cs[unit & 1];
    479 	tp = cs->cs_ttyp;
    480 
    481 	tp->t_linesw->l_close(tp, flags);
    482 	ttyclose(tp);
    483 
    484 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
    485 		/*
    486 		 * Although we got a last close, the device may still be in
    487 		 * use; e.g. if this was the dialout node, and there are still
    488 		 * processes waiting for carrier on the non-dialout node.
    489 		 */
    490 		zs_shutdown(cs);
    491 	}
    492 	return (0);
    493 }
    494 
    495 /*
    496  * Read/write zs serial port.
    497  */
    498 int
    499 zsread(dev, uio, flags)
    500 dev_t		dev;
    501 struct uio	*uio;
    502 int		flags;
    503 {
    504 	register struct zs_chanstate	*cs;
    505 	register struct zs_softc	*zi;
    506 	register struct tty		*tp;
    507 		 int			unit;
    508 
    509 	unit = ZS_UNIT(dev);
    510 	zi   = zs_cd.cd_devs[unit >> 1];
    511 	cs   = &zi->zi_cs[unit & 1];
    512 	tp   = cs->cs_ttyp;
    513 
    514 	return(tp->t_linesw->l_read(tp, uio, flags));
    515 }
    516 
    517 int
    518 zswrite(dev, uio, flags)
    519 dev_t		dev;
    520 struct uio	*uio;
    521 int		flags;
    522 {
    523 	register struct zs_chanstate	*cs;
    524 	register struct zs_softc	*zi;
    525 	register struct tty		*tp;
    526 		 int			unit;
    527 
    528 	unit = ZS_UNIT(dev);
    529 	zi   = zs_cd.cd_devs[unit >> 1];
    530 	cs   = &zi->zi_cs[unit & 1];
    531 	tp   = cs->cs_ttyp;
    532 
    533 	return(tp->t_linesw->l_write(tp, uio, flags));
    534 }
    535 
    536 int
    537 zspoll(dev, events, l)
    538 dev_t		dev;
    539 int		events;
    540 struct lwp	*l;
    541 {
    542 	register struct zs_chanstate	*cs;
    543 	register struct zs_softc	*zi;
    544 	register struct tty		*tp;
    545 		 int			unit;
    546 
    547 	unit = ZS_UNIT(dev);
    548 	zi   = zs_cd.cd_devs[unit >> 1];
    549 	cs   = &zi->zi_cs[unit & 1];
    550 	tp   = cs->cs_ttyp;
    551 
    552 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    553 }
    554 
    555 struct tty *
    556 zstty(dev)
    557 dev_t	dev;
    558 {
    559 	register struct zs_chanstate	*cs;
    560 	register struct zs_softc	*zi;
    561 		 int			unit;
    562 
    563 	unit = ZS_UNIT(dev);
    564 	zi   = zs_cd.cd_devs[unit >> 1];
    565 	cs   = &zi->zi_cs[unit & 1];
    566 	return(cs->cs_ttyp);
    567 }
    568 
    569 /*
    570  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    571  * channels are kept in (A,B) pairs.
    572  *
    573  * Do just a little, then get out; set a software interrupt if more
    574  * work is needed.
    575  *
    576  * We deliberately ignore the vectoring Zilog gives us, and match up
    577  * only the number of `reset interrupt under service' operations, not
    578  * the order.
    579  */
    580 
    581 int
    582 zshard(sr)
    583 long sr;
    584 {
    585 	register struct zs_chanstate	*a;
    586 #define	b (a + 1)
    587 	register volatile struct zschan *zc;
    588 	register int			rr3, intflags = 0, v, i;
    589 
    590 	do {
    591 	    intflags &= ~4;
    592 	    for(a = zslist; a != NULL; a = b->cs_next) {
    593 		rr3 = ZS_READ(a->cs_zc, 3);
    594 		if(rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    595 			intflags |= 4|2;
    596 			zc = a->cs_zc;
    597 			i  = a->cs_rbput;
    598 			if(rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    599 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    600 				intflags |= 1;
    601 			}
    602 			if(rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    603 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    604 				intflags |= 1;
    605 			}
    606 			if(rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    607 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    608 				intflags |= 1;
    609 			}
    610 			a->cs_rbput = i;
    611 		}
    612 		if(rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    613 			intflags |= 4|2;
    614 			zc = b->cs_zc;
    615 			i  = b->cs_rbput;
    616 			if(rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    617 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    618 				intflags |= 1;
    619 			}
    620 			if(rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    621 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    622 				intflags |= 1;
    623 			}
    624 			if(rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    625 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    626 				intflags |= 1;
    627 			}
    628 			b->cs_rbput = i;
    629 		}
    630 	    }
    631 	} while(intflags & 4);
    632 #undef b
    633 
    634 	if(intflags & 1) {
    635 		if(BASEPRI(sr)) {
    636 			spl1();
    637 			zsshortcuts++;
    638 			return(zssoft(sr));
    639 		}
    640 		else if(!cb_scheduled) {
    641 			cb_scheduled++;
    642 			add_sicallback((si_farg)zssoft, 0, 0);
    643 		}
    644 	}
    645 	return(intflags & 2);
    646 }
    647 
    648 static int
    649 zsrint(cs, zc)
    650 register struct zs_chanstate	*cs;
    651 register volatile struct zschan	*zc;
    652 {
    653 	register int c;
    654 
    655 	/*
    656 	 * First read the status, because read of the received char
    657 	 * destroy the status of this char.
    658 	 */
    659 	c = ZS_READ(zc, 1);
    660 	c |= (zc->zc_data << 8);
    661 
    662 	/* clear receive error & interrupt condition */
    663 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    664 	zc->zc_csr = ZSWR0_CLR_INTR;
    665 
    666 	return(ZRING_MAKE(ZRING_RINT, c));
    667 }
    668 
    669 static int
    670 zsxint(cs, zc)
    671 register struct zs_chanstate	*cs;
    672 register volatile struct zschan	*zc;
    673 {
    674 	register int i = cs->cs_tbc;
    675 
    676 	if(i == 0) {
    677 		zc->zc_csr = ZSWR0_RESET_TXINT;
    678 		zc->zc_csr = ZSWR0_CLR_INTR;
    679 		return(ZRING_MAKE(ZRING_XINT, 0));
    680 	}
    681 	cs->cs_tbc = i - 1;
    682 	zc->zc_data = *cs->cs_tba++;
    683 	zc->zc_csr = ZSWR0_CLR_INTR;
    684 	return (0);
    685 }
    686 
    687 static int
    688 zssint(cs, zc)
    689 register struct zs_chanstate	*cs;
    690 register volatile struct zschan	*zc;
    691 {
    692 	register int rr0;
    693 
    694 	rr0 = zc->zc_csr;
    695 	zc->zc_csr = ZSWR0_RESET_STATUS;
    696 	zc->zc_csr = ZSWR0_CLR_INTR;
    697 	/*
    698 	 * The chip's hardware flow control is, as noted in zsreg.h,
    699 	 * busted---if the DCD line goes low the chip shuts off the
    700 	 * receiver (!).  If we want hardware CTS flow control but do
    701 	 * not have it, and carrier is now on, turn HFC on; if we have
    702 	 * HFC now but carrier has gone low, turn it off.
    703 	 */
    704 	if(rr0 & ZSRR0_DCD) {
    705 		if(cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    706 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    707 			cs->cs_creg[3] |= ZSWR3_HFC;
    708 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    709 		}
    710 	}
    711 	else {
    712 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    713 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    714 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    715 		}
    716 	}
    717 	return(ZRING_MAKE(ZRING_SINT, rr0));
    718 }
    719 
    720 /*
    721  * Print out a ring or fifo overrun error message.
    722  */
    723 static void
    724 zsoverrun(unit, ptime, what)
    725 int	unit;
    726 long	*ptime;
    727 const char *what;
    728 {
    729 
    730 	if(*ptime != time.tv_sec) {
    731 		*ptime = time.tv_sec;
    732 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    733 		    (unit & 1) + 'a', what);
    734 	}
    735 }
    736 
    737 /*
    738  * ZS software interrupt.  Scan all channels for deferred interrupts.
    739  */
    740 int
    741 zssoft(sr)
    742 long sr;
    743 {
    744     register struct zs_chanstate	*cs;
    745     register volatile struct zschan	*zc;
    746     register struct linesw		*line;
    747     register struct tty			*tp;
    748     register int			get, n, c, cc, unit, s;
    749  	     int			retval = 0;
    750 
    751     cb_scheduled = 0;
    752     s = spltty();
    753     for(cs = zslist; cs != NULL; cs = cs->cs_next) {
    754 	get = cs->cs_rbget;
    755 again:
    756 	n = cs->cs_rbput;	/* atomic			*/
    757 	if(get == n)		/* nothing more on this line	*/
    758 		continue;
    759 	retval = 1;
    760 	unit   = cs->cs_unit;	/* set up to handle interrupts	*/
    761 	zc     = cs->cs_zc;
    762 	tp     = cs->cs_ttyp;
    763 	line   = tp->t_linesw;
    764 	/*
    765 	 * Compute the number of interrupts in the receive ring.
    766 	 * If the count is overlarge, we lost some events, and
    767 	 * must advance to the first valid one.  It may get
    768 	 * overwritten if more data are arriving, but this is
    769 	 * too expensive to check and gains nothing (we already
    770 	 * lost out; all we can do at this point is trade one
    771 	 * kind of loss for another).
    772 	 */
    773 	n -= get;
    774 	if(n > ZLRB_RING_SIZE) {
    775 		zsoverrun(unit, &cs->cs_rotime, "ring");
    776 		get += n - ZLRB_RING_SIZE;
    777 		n    = ZLRB_RING_SIZE;
    778 	}
    779 	while(--n >= 0) {
    780 		/* race to keep ahead of incoming interrupts */
    781 		c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
    782 		switch (ZRING_TYPE(c)) {
    783 
    784 		case ZRING_RINT:
    785 			c = ZRING_VALUE(c);
    786 			if(c & ZSRR1_DO)
    787 				zsoverrun(unit, &cs->cs_fotime, "fifo");
    788 			cc = c >> 8;
    789 			if(c & ZSRR1_FE)
    790 				cc |= TTY_FE;
    791 			if(c & ZSRR1_PE)
    792 				cc |= TTY_PE;
    793 			line->l_rint(cc, tp);
    794 			break;
    795 
    796 		case ZRING_XINT:
    797 			/*
    798 			 * Transmit done: change registers and resume,
    799 			 * or clear BUSY.
    800 			 */
    801 			if(cs->cs_heldchange) {
    802 				int sps;
    803 
    804 				sps = splzs();
    805 				c = zc->zc_csr;
    806 				if((c & ZSRR0_DCD) == 0)
    807 					cs->cs_preg[3] &= ~ZSWR3_HFC;
    808 				bcopy((void *)cs->cs_preg,
    809 				    (void *)cs->cs_creg, 16);
    810 				zs_loadchannelregs(zc, cs->cs_creg);
    811 				splx(sps);
    812 				cs->cs_heldchange = 0;
    813 				if(cs->cs_heldtbc
    814 					&& (tp->t_state & TS_TTSTOP) == 0) {
    815 					cs->cs_tbc = cs->cs_heldtbc - 1;
    816 					zc->zc_data = *cs->cs_tba++;
    817 					goto again;
    818 				}
    819 			}
    820 			tp->t_state &= ~TS_BUSY;
    821 			if(tp->t_state & TS_FLUSH)
    822 				tp->t_state &= ~TS_FLUSH;
    823 			else ndflush(&tp->t_outq,cs->cs_tba
    824 						- tp->t_outq.c_cf);
    825 			line->l_start(tp);
    826 			break;
    827 
    828 		case ZRING_SINT:
    829 			/*
    830 			 * Status line change.  HFC bit is run in
    831 			 * hardware interrupt, to avoid locking
    832 			 * at splzs here.
    833 			 */
    834 			c = ZRING_VALUE(c);
    835 			if((c ^ cs->cs_rr0) & ZSRR0_DCD) {
    836 				cc = (c & ZSRR0_DCD) != 0;
    837 				if(line->l_modem(tp, cc) == 0)
    838 					zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR,
    839 							cc ? DMBIS : DMBIC);
    840 			}
    841 			cs->cs_rr0 = c;
    842 			break;
    843 
    844 		default:
    845 			log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
    846 			    unit >> 1, (unit & 1) + 'a', c);
    847 			break;
    848 		}
    849 	}
    850 	cs->cs_rbget = get;
    851 	goto again;
    852     }
    853     splx(s);
    854     return (retval);
    855 }
    856 
    857 int
    858 zsioctl(dev, cmd, data, flag, l)
    859 dev_t		dev;
    860 u_long		cmd;
    861 void *		data;
    862 int		flag;
    863 struct lwp	*l;
    864 {
    865 		 int			unit = ZS_UNIT(dev);
    866 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
    867 	register struct tty		*tp = zi->zi_cs[unit & 1].cs_ttyp;
    868 	register int			error, s;
    869 	register struct zs_chanstate	*cs = &zi->zi_cs[unit & 1];
    870 
    871 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
    872 	if(error != EPASSTHROUGH)
    873 		return(error);
    874 
    875 	error = ttioctl(tp, cmd, data, flag, l);
    876 	if(error !=EPASSTHROUGH)
    877 		return (error);
    878 
    879 	switch (cmd) {
    880 	case TIOCSBRK:
    881 		s = splzs();
    882 		cs->cs_preg[5] |= ZSWR5_BREAK;
    883 		cs->cs_creg[5] |= ZSWR5_BREAK;
    884 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    885 		splx(s);
    886 		break;
    887 	case TIOCCBRK:
    888 		s = splzs();
    889 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    890 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    891 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    892 		splx(s);
    893 		break;
    894 	case TIOCGFLAGS: {
    895 		int bits = 0;
    896 
    897 		if(cs->cs_softcar)
    898 			bits |= TIOCFLAG_SOFTCAR;
    899 		if(cs->cs_creg[15] & ZSWR15_DCD_IE)
    900 			bits |= TIOCFLAG_CLOCAL;
    901 		if(cs->cs_creg[3] & ZSWR3_HFC)
    902 			bits |= TIOCFLAG_CRTSCTS;
    903 		*(int *)data = bits;
    904 		break;
    905 	}
    906 	case TIOCSFLAGS: {
    907 		int userbits = 0;
    908 
    909 		error = kauth_authorize_device_tty(l->l_cred,
    910 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    911 		if(error != 0)
    912 			return (EPERM);
    913 
    914 		userbits = *(int *)data;
    915 
    916 		/*
    917 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
    918 		 # defaulting to software flow control.
    919 		 */
    920 		if(userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
    921 			return(EINVAL);
    922 		if(userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
    923 			return(ENODEV);
    924 
    925 		s = splzs();
    926 		if((userbits & TIOCFLAG_SOFTCAR)) {
    927 			cs->cs_softcar = 1;	/* turn on softcar */
    928 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
    929 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
    930 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    931 		}
    932 		else if(userbits & TIOCFLAG_CLOCAL) {
    933 			cs->cs_softcar = 0; 	/* turn off softcar */
    934 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
    935 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
    936 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    937 			tp->t_termios.c_cflag |= CLOCAL;
    938 		}
    939 		if(userbits & TIOCFLAG_CRTSCTS) {
    940 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
    941 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
    942 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    943 			cs->cs_preg[3] |= ZSWR3_HFC;
    944 			cs->cs_creg[3] |= ZSWR3_HFC;
    945 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    946 			tp->t_termios.c_cflag |= CRTSCTS;
    947 		}
    948 		else {
    949 			/* no mdmbuf, so we must want software flow control */
    950 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    951 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
    952 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    953 			cs->cs_preg[3] &= ~ZSWR3_HFC;
    954 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    955 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    956 			tp->t_termios.c_cflag &= ~CRTSCTS;
    957 		}
    958 		splx(s);
    959 		break;
    960 	}
    961 	case TIOCSDTR:
    962 		zs_modem(cs, ZSWR5_DTR, DMBIS);
    963 		break;
    964 	case TIOCCDTR:
    965 		zs_modem(cs, ZSWR5_DTR, DMBIC);
    966 		break;
    967 	case TIOCMGET:
    968 		zs_modem(cs, 0, DMGET);
    969 		break;
    970 	case TIOCMSET:
    971 	case TIOCMBIS:
    972 	case TIOCMBIC:
    973 	default:
    974 		return (EPASSTHROUGH);
    975 	}
    976 	return (0);
    977 }
    978 
    979 /*
    980  * Start or restart transmission.
    981  */
    982 static void
    983 zsstart(tp)
    984 register struct tty *tp;
    985 {
    986 	register struct zs_chanstate	*cs;
    987 	register int			s, nch;
    988 		 int			unit = ZS_UNIT(tp->t_dev);
    989 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
    990 
    991 	cs = &zi->zi_cs[unit & 1];
    992 	s  = spltty();
    993 
    994 	/*
    995 	 * If currently active or delaying, no need to do anything.
    996 	 */
    997 	if(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
    998 		goto out;
    999 
   1000 	/*
   1001 	 * If there are sleepers, and output has drained below low
   1002 	 * water mark, awaken.
   1003 	 */
   1004 	if(tp->t_outq.c_cc <= tp->t_lowat) {
   1005 		if(tp->t_state & TS_ASLEEP) {
   1006 			tp->t_state &= ~TS_ASLEEP;
   1007 			wakeup((void *)&tp->t_outq);
   1008 		}
   1009 		selwakeup(&tp->t_wsel);
   1010 	}
   1011 
   1012 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1013 	if(nch) {
   1014 		register char *p = tp->t_outq.c_cf;
   1015 
   1016 		/* mark busy, enable tx done interrupts, & send first byte */
   1017 		tp->t_state |= TS_BUSY;
   1018 		(void) splzs();
   1019 		cs->cs_preg[1] |= ZSWR1_TIE;
   1020 		cs->cs_creg[1] |= ZSWR1_TIE;
   1021 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1022 		cs->cs_zc->zc_data = *p;
   1023 		cs->cs_tba = p + 1;
   1024 		cs->cs_tbc = nch - 1;
   1025 	} else {
   1026 		/*
   1027 		 * Nothing to send, turn off transmit done interrupts.
   1028 		 * This is useful if something is doing polled output.
   1029 		 */
   1030 		(void) splzs();
   1031 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1032 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1033 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1034 	}
   1035 out:
   1036 	splx(s);
   1037 }
   1038 
   1039 /*
   1040  * Stop output, e.g., for ^S or output flush.
   1041  */
   1042 void
   1043 zsstop(tp, flag)
   1044 register struct tty	*tp;
   1045 	 int		flag;
   1046 {
   1047 	register struct zs_chanstate	*cs;
   1048 	register int			s, unit = ZS_UNIT(tp->t_dev);
   1049 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
   1050 
   1051 	cs = &zi->zi_cs[unit & 1];
   1052 	s  = splzs();
   1053 	if(tp->t_state & TS_BUSY) {
   1054 		/*
   1055 		 * Device is transmitting; must stop it.
   1056 		 */
   1057 		cs->cs_tbc = 0;
   1058 		if ((tp->t_state & TS_TTSTOP) == 0)
   1059 			tp->t_state |= TS_FLUSH;
   1060 	}
   1061 	splx(s);
   1062 }
   1063 
   1064 static void
   1065 zs_shutdown(cs)
   1066 	struct zs_chanstate	*cs;
   1067 {
   1068 	struct tty	*tp = cs->cs_ttyp;
   1069 	int		s;
   1070 
   1071 	s = splzs();
   1072 
   1073 	/*
   1074 	 * Hang up if necessary.  Wait a bit, so the other side has time to
   1075 	 * notice even if we immediately open the port again.
   1076 	 */
   1077 	if(tp->t_cflag & HUPCL) {
   1078 		zs_modem(cs, 0, DMSET);
   1079 		(void)tsleep((void *)cs, TTIPRI, ttclos, hz);
   1080 	}
   1081 
   1082 	/* Clear any break condition set with TIOCSBRK. */
   1083 	if(cs->cs_creg[5] & ZSWR5_BREAK) {
   1084 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1085 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1086 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1087 	}
   1088 
   1089 	/*
   1090 	 * Drop all lines and cancel interrupts
   1091 	 */
   1092 	zs_loadchannelregs(cs->cs_zc, zs_init_regs);
   1093 	splx(s);
   1094 }
   1095 
   1096 /*
   1097  * Set ZS tty parameters from termios.
   1098  *
   1099  * This routine makes use of the fact that only registers
   1100  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1101  */
   1102 static int
   1103 zsparam(tp, t)
   1104 register struct tty	*tp;
   1105 register struct termios	*t;
   1106 {
   1107 		 int			unit = ZS_UNIT(tp->t_dev);
   1108 		 struct zs_softc	*zi = zs_cd.cd_devs[unit >> 1];
   1109 	register struct zs_chanstate	*cs = &zi->zi_cs[unit & 1];
   1110 		 int			cdiv = 0,	/* XXX gcc4 -Wuninitialized */
   1111 					clkm = 0,	/* XXX gcc4 -Wuninitialized */
   1112 					brgm = 0,	/* XXX gcc4 -Wuninitialized */
   1113 					tcon = 0;	/* XXX gcc4 -Wuninitialized */
   1114 	register int			tmp, tmp5, cflag, s;
   1115 
   1116 	tmp  = t->c_ospeed;
   1117 	tmp5 = t->c_ispeed;
   1118 	if(tmp < 0 || (tmp5 && tmp5 != tmp))
   1119 		return(EINVAL);
   1120 	if(tmp == 0) {
   1121 		/* stty 0 => drop DTR and RTS */
   1122 		zs_modem(cs, 0, DMSET);
   1123 		return(0);
   1124 	}
   1125 	tmp = zsbaudrate(unit, tmp, &cdiv, &clkm, &brgm, &tcon);
   1126 	if (tmp < 0)
   1127 		return(EINVAL);
   1128 	tp->t_ispeed = tp->t_ospeed = tmp;
   1129 
   1130 	cflag = tp->t_cflag = t->c_cflag;
   1131 	if (cflag & CSTOPB)
   1132 		cdiv |= ZSWR4_TWOSB;
   1133 	else
   1134 		cdiv |= ZSWR4_ONESB;
   1135 	if (!(cflag & PARODD))
   1136 		cdiv |= ZSWR4_EVENP;
   1137 	if (cflag & PARENB)
   1138 		cdiv |= ZSWR4_PARENB;
   1139 
   1140 	switch(cflag & CSIZE) {
   1141 	case CS5:
   1142 		tmp  = ZSWR3_RX_5;
   1143 		tmp5 = ZSWR5_TX_5;
   1144 		break;
   1145 	case CS6:
   1146 		tmp  = ZSWR3_RX_6;
   1147 		tmp5 = ZSWR5_TX_6;
   1148 		break;
   1149 	case CS7:
   1150 		tmp  = ZSWR3_RX_7;
   1151 		tmp5 = ZSWR5_TX_7;
   1152 		break;
   1153 	case CS8:
   1154 	default:
   1155 		tmp  = ZSWR3_RX_8;
   1156 		tmp5 = ZSWR5_TX_8;
   1157 		break;
   1158 	}
   1159 	tmp  |= ZSWR3_RX_ENABLE;
   1160 	tmp5 |= ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1161 
   1162 	/*
   1163 	 * Block interrupts so that state will not
   1164 	 * be altered until we are done setting it up.
   1165 	 */
   1166 	s = splzs();
   1167 	cs->cs_preg[4]  = cdiv;
   1168 	cs->cs_preg[11] = clkm;
   1169 	cs->cs_preg[12] = tcon;
   1170 	cs->cs_preg[13] = tcon >> 8;
   1171 	cs->cs_preg[14] = brgm;
   1172 	cs->cs_preg[1]  = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1173 	cs->cs_preg[9]  = ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT;
   1174 	cs->cs_preg[10] = ZSWR10_NRZ;
   1175 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1176 
   1177 	/*
   1178 	 * Output hardware flow control on the chip is horrendous: if
   1179 	 * carrier detect drops, the receiver is disabled.  Hence we
   1180 	 * can only do this when the carrier is on.
   1181 	 */
   1182 	if(cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
   1183 		tmp |= ZSWR3_HFC;
   1184 	cs->cs_preg[3] = tmp;
   1185 	cs->cs_preg[5] = tmp5;
   1186 
   1187 	/*
   1188 	 * If nothing is being transmitted, set up new current values,
   1189 	 * else mark them as pending.
   1190 	 */
   1191 	if(cs->cs_heldchange == 0) {
   1192 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1193 			cs->cs_heldtbc = cs->cs_tbc;
   1194 			cs->cs_tbc = 0;
   1195 			cs->cs_heldchange = 1;
   1196 		} else {
   1197 			bcopy((void *)cs->cs_preg, (void *)cs->cs_creg, 16);
   1198 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1199 		}
   1200 	}
   1201 	splx(s);
   1202 	return (0);
   1203 }
   1204 
   1205 /*
   1206  * search for the best matching baudrate
   1207  */
   1208 static int
   1209 zsbaudrate(unit, wanted, divisor, clockmode, brgenmode, timeconst)
   1210 int	unit, wanted, *divisor, *clockmode, *brgenmode, *timeconst;
   1211 {
   1212 	int	bestdiff, bestbps, source;
   1213 
   1214 	bestdiff = bestbps = 0;
   1215 	unit = (unit & 1) << 2;
   1216 	for (source = 0; source < 4; ++source) {
   1217 		long	freq = zs_frequencies[unit + source];
   1218 		int	diff, bps, div, clkm, brgm, tcon;
   1219 
   1220 		bps = div = clkm = brgm = tcon = 0;
   1221 		switch (source) {
   1222 			case 0:	/* BRgen, PCLK */
   1223 				brgm = ZSWR14_BAUD_ENA|ZSWR14_BAUD_FROM_PCLK;
   1224 				break;
   1225 			case 1:	/* BRgen, RTxC */
   1226 				brgm = ZSWR14_BAUD_ENA;
   1227 				break;
   1228 			case 2: /* RTxC */
   1229 				clkm = ZSWR11_RXCLK_RTXC|ZSWR11_TXCLK_RTXC;
   1230 				break;
   1231 			case 3: /* TRxC */
   1232 				clkm = ZSWR11_RXCLK_TRXC|ZSWR11_TXCLK_TRXC;
   1233 				break;
   1234 		}
   1235 		switch (source) {
   1236 			case 0:
   1237 			case 1:
   1238 				div  = ZSWR4_CLK_X16;
   1239 				clkm = ZSWR11_RXCLK_BAUD|ZSWR11_TXCLK_BAUD;
   1240 				tcon = BPS_TO_TCONST(freq, wanted);
   1241 				if (tcon < 0)
   1242 					tcon = 0;
   1243 				bps  = TCONST_TO_BPS(freq, tcon);
   1244 				break;
   1245 			case 2:
   1246 			case 3:
   1247 			{	int	b1 = freq / 16, d1 = abs(b1 - wanted);
   1248 				int	b2 = freq / 32, d2 = abs(b2 - wanted);
   1249 				int	b3 = freq / 64, d3 = abs(b3 - wanted);
   1250 
   1251 				if (d1 < d2 && d1 < d3) {
   1252 					div = ZSWR4_CLK_X16;
   1253 					bps = b1;
   1254 				} else if (d2 < d3 && d2 < d1) {
   1255 					div = ZSWR4_CLK_X32;
   1256 					bps = b2;
   1257 				} else {
   1258 					div = ZSWR4_CLK_X64;
   1259 					bps = b3;
   1260 				}
   1261 				brgm = tcon = 0;
   1262 				break;
   1263 			}
   1264 		}
   1265 		diff = abs(bps - wanted);
   1266 		if (!source || diff < bestdiff) {
   1267 			*divisor   = div;
   1268 			*clockmode = clkm;
   1269 			*brgenmode = brgm;
   1270 			*timeconst = tcon;
   1271 			bestbps    = bps;
   1272 			bestdiff   = diff;
   1273 			if (diff == 0)
   1274 				break;
   1275 		}
   1276 	}
   1277 	/* Allow deviations upto 5% */
   1278 	if (20 * bestdiff > wanted)
   1279 		return -1;
   1280 	return bestbps;
   1281 }
   1282 
   1283 /*
   1284  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1285  * in transmission, the change is deferred.
   1286  */
   1287 static int
   1288 zs_modem(cs, bits, how)
   1289 struct zs_chanstate	*cs;
   1290 int			bits, how;
   1291 {
   1292 	int s, mbits;
   1293 
   1294 	bits  &= ZSWR5_DTR | ZSWR5_RTS;
   1295 
   1296 	s = splzs();
   1297 	mbits  = cs->cs_preg[5] &  (ZSWR5_DTR | ZSWR5_RTS);
   1298 
   1299 	switch(how) {
   1300 		case DMSET:
   1301 				mbits  = bits;
   1302 				break;
   1303 		case DMBIS:
   1304 				mbits |= bits;
   1305 				break;
   1306 		case DMBIC:
   1307 				mbits &= ~bits;
   1308 				break;
   1309 		case DMGET:
   1310 				splx(s);
   1311 				return(mbits);
   1312 	}
   1313 
   1314 	cs->cs_preg[5] = (cs->cs_preg[5] & ~(ZSWR5_DTR | ZSWR5_RTS)) | mbits;
   1315 	if(cs->cs_heldchange == 0) {
   1316 		if(cs->cs_ttyp->t_state & TS_BUSY) {
   1317 			cs->cs_heldtbc = cs->cs_tbc;
   1318 			cs->cs_tbc = 0;
   1319 			cs->cs_heldchange = 1;
   1320 		}
   1321 		else {
   1322 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1323 		}
   1324 	}
   1325 	splx(s);
   1326 	return(0);
   1327 }
   1328 
   1329 /*
   1330  * Write the given register set to the given zs channel in the proper order.
   1331  * The channel must not be transmitting at the time.  The receiver will
   1332  * be disabled for the time it takes to write all the registers.
   1333  */
   1334 static void
   1335 zs_loadchannelregs(zc, reg)
   1336 volatile struct zschan	*zc;
   1337 u_char			*reg;
   1338 {
   1339 	int i;
   1340 
   1341 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1342 	i = zc->zc_data;		/* drain fifo */
   1343 	i = zc->zc_data;
   1344 	i = zc->zc_data;
   1345 	ZS_WRITE(zc,  4, reg[4]);
   1346 	ZS_WRITE(zc, 10, reg[10]);
   1347 	ZS_WRITE(zc,  3, reg[3] & ~ZSWR3_RX_ENABLE);
   1348 	ZS_WRITE(zc,  5, reg[5] & ~ZSWR5_TX_ENABLE);
   1349 	ZS_WRITE(zc,  1, reg[1]);
   1350 	ZS_WRITE(zc,  9, reg[9]);
   1351 	ZS_WRITE(zc, 11, reg[11]);
   1352 	ZS_WRITE(zc, 12, reg[12]);
   1353 	ZS_WRITE(zc, 13, reg[13]);
   1354 	ZS_WRITE(zc, 14, reg[14]);
   1355 	ZS_WRITE(zc, 15, reg[15]);
   1356 	ZS_WRITE(zc,  3, reg[3]);
   1357 	ZS_WRITE(zc,  5, reg[5]);
   1358 }
   1359 #endif /* NZS > 1 */
   1360