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