Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.1
      1 /*	$NetBSD: z8530tty.c,v 1.1 1996/01/24 01:07:25 gwr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Gordon W. Ross
      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  * 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. All advertising materials mentioning features or use of this software
     26  *    must display the following acknowledgement:
     27  *	This product includes software developed by the University of
     28  *	California, Berkeley and its contributors.
     29  * 4. Neither the name of the University nor the names of its contributors
     30  *    may be used to endorse or promote products derived from this software
     31  *    without specific prior written permission.
     32  *
     33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     43  * SUCH DAMAGE.
     44  *
     45  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     46  */
     47 
     48 /*
     49  * Zilog Z8530 Dual UART driver (tty interface)
     50  *
     51  * This is the "slave" driver that will be attached to
     52  * the "zsc" driver for plain "tty" async. serial lines.
     53  */
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/proc.h>
     58 #include <sys/device.h>
     59 #include <sys/conf.h>
     60 #include <sys/file.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/tty.h>
     63 #include <sys/time.h>
     64 #include <sys/kernel.h>
     65 #include <sys/syslog.h>
     66 
     67 #include <dev/ic/z8530reg.h>
     68 #include <machine/z8530var.h>
     69 
     70 #ifdef KGDB
     71 extern int zs_check_kgdb();
     72 #endif
     73 
     74 /*
     75  * Allow the MD var.h to override the default CFLAG so that
     76  * console messages during boot come out with correct parity.
     77  */
     78 #ifndef	ZSTTY_DEF_CFLAG
     79 #define	ZSTTY_DEF_CFLAG	TTYDEF_CFLAG
     80 #endif
     81 
     82 /*
     83  * How many input characters we can buffer.
     84  * The port-specific var.h may override this.
     85  * Note: must be a power of two!
     86  */
     87 #ifndef	ZSTTY_RING_SIZE
     88 #define	ZSTTY_RING_SIZE	1024
     89 #endif
     90 #define ZSTTY_RING_MASK (ZSTTY_RING_SIZE-1)
     91 
     92 struct zstty_softc {
     93 	struct	device zst_dev;		/* required first: base device */
     94 	struct  tty *zst_tty;
     95 	struct	zs_chanstate *zst_cs;
     96 
     97 	int zst_hwflags;	/* see z8530var.h */
     98 	int zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
     99 
    100 	/* Flags to communicate with zstty_softint() */
    101 	volatile int zst_intr_flags;
    102 #define	INTR_RX_OVERRUN 1
    103 #define INTR_TX_EMPTY   2
    104 #define INTR_ST_CHECK   4
    105 
    106 	/*
    107 	 * The transmit byte count and address are used for pseudo-DMA
    108 	 * output in the hardware interrupt code.  PDMA can be suspended
    109 	 * to get pending changes done; heldtbc is used for this.  It can
    110 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
    111 	 */
    112 	int 	zst_tbc;			/* transmit byte count */
    113 	caddr_t	zst_tba;			/* transmit buffer address */
    114 	int 	zst_heldtbc;		/* held tbc while xmission stopped */
    115 
    116 	/*
    117 	 * Printing an overrun error message often takes long enough to
    118 	 * cause another overrun, so we only print one per second.
    119 	 */
    120 	long	zst_rotime;		/* time of last ring overrun */
    121 	long	zst_fotime;		/* time of last fifo overrun */
    122 
    123 	/*
    124 	 * The receive ring buffer.
    125 	 */
    126 	u_int	zst_rbget;	/* ring buffer `get' index */
    127 	volatile u_int	zst_rbput;	/* ring buffer `put' index */
    128 	u_short	zst_rbuf[ZSTTY_RING_SIZE]; /* rr1, data pairs */
    129 };
    130 
    131 
    132 /* Definition of the driver for autoconfig. */
    133 static int	zstty_match(struct device *, void *, void *);
    134 static void	zstty_attach(struct device *, struct device *, void *);
    135 
    136 struct cfdriver zsttycd = {
    137 	NULL, "zstty", zstty_match, zstty_attach,
    138 	DV_TTY, sizeof(struct zstty_softc), NULL,
    139 };
    140 
    141 struct zsops zsops_tty;
    142 
    143 /* Routines called from other code. */
    144 cdev_decl(zs);	/* open, close, read, write, ioctl, stop, ... */
    145 
    146 static void	zsstart(struct tty *);
    147 static int	zsparam(struct tty *, struct termios *);
    148 static void zs_modem(struct zstty_softc *zst, int onoff);
    149 
    150 /*
    151  * zstty_match: how is this zs channel configured?
    152  */
    153 int
    154 zstty_match(parent, match, aux)
    155 	struct device *parent;
    156 	void   *match, *aux;
    157 {
    158 	struct cfdata *cf = match;
    159 	struct zsc_attach_args *args = aux;
    160 
    161 	/* Exact match is better than wildcard. */
    162 	if (cf->cf_loc[0] == args->channel)
    163 		return 2;
    164 
    165 	/* This driver accepts wildcard. */
    166 	if (cf->cf_loc[0] == -1)
    167 		return 1;
    168 
    169 	return 0;
    170 }
    171 
    172 void
    173 zstty_attach(parent, self, aux)
    174 	struct device *parent, *self;
    175 	void   *aux;
    176 
    177 {
    178 	struct zsc_softc *zsc = (void *) parent;
    179 	struct zstty_softc *zst = (void *) self;
    180 	struct zsc_attach_args *args = aux;
    181 	struct zs_chanstate *cs;
    182 	struct cfdata *cf;
    183 	struct tty *tp;
    184 	int channel, tty_unit;
    185 	dev_t dev;
    186 
    187 	cf = zst->zst_dev.dv_cfdata;
    188 	tty_unit = cf->cf_unit;
    189 	channel = args->channel;
    190 	cs = &zsc->zsc_cs[channel];
    191 	cs->cs_private = zst;
    192 	cs->cs_ops = &zsops_tty;
    193 
    194 	zst->zst_cs = cs;
    195 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
    196 	zst->zst_hwflags = args->hwflags;
    197 	dev = makedev(ZSTTY_MAJOR, tty_unit);
    198 
    199 	if (zst->zst_swflags)
    200 		printf(" flags 0x%x", zst->zst_swflags);
    201 
    202 	if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
    203 		printf(" (console)");
    204 	else {
    205 #ifdef KGDB
    206 		/*
    207 		 * Allow kgdb to "take over" this port.  If this port is
    208 		 * NOT the kgdb port, zs_check_kgdb() will return zero.
    209 		 * If it IS the kgdb port, it will print "kgdb,...\n"
    210 		 * and then return non-zero.
    211 		 */
    212 		if (zs_check_kgdb(cs, dev)) {
    213 			/*
    214 			 * This is the kgdb port (exclusive use)
    215 			 * so skip the normal attach code.
    216 			 */
    217 			return;
    218 		}
    219 #endif
    220 	}
    221 	printf("\n");
    222 
    223 	tp = zst->zst_tty = ttymalloc();
    224 	tp->t_dev = dev;
    225 	tp->t_oproc = zsstart;
    226 	tp->t_param = zsparam;
    227 	tp->t_sc = zst; 	/* XXX - Quick access! */
    228 
    229 	/*
    230 	 * Hardware init
    231 	 */
    232 	if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE) {
    233 		/* This unit is the console. */
    234 		zst->zst_swflags |= TIOCFLAG_SOFTCAR;
    235 		/* Call _param so interrupts get enabled. */
    236 		cs->cs_defspeed = zs_getspeed(cs);
    237 		tp->t_ispeed = cs->cs_defspeed;
    238 		tp->t_ospeed = cs->cs_defspeed;
    239 		tp->t_cflag = ZSTTY_DEF_CFLAG;
    240 		(void) zsparam(tp, &tp->t_termios);
    241 	} else {
    242 		/* Not the console; may need reset. */
    243 		int reset, s;
    244 		reset = (channel == 0) ?
    245 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    246 		s = splzs();
    247 		ZS_WRITE(cs, 9, reset);
    248 		splx(s);
    249 	}
    250 
    251 	/*
    252 	 * Initialize state of modem control lines (DTR).
    253 	 * If softcar is set, turn on DTR now and leave it.
    254 	 * otherwise, turn off DTR now, and raise in open.
    255 	 * (Keeps modem from answering too early.)
    256 	 */
    257 	zs_modem(zst, (zst->zst_swflags & TIOCFLAG_SOFTCAR) ? 1 : 0);
    258 }
    259 
    260 
    261 /*
    262  * Return pointer to our tty.
    263  */
    264 struct tty *
    265 zstty(dev)
    266 	dev_t dev;
    267 {
    268 	struct zstty_softc *zst;
    269 	int unit = minor(dev);
    270 
    271 #ifdef	DIAGNOSTIC
    272 	if (unit >= zsttycd.cd_ndevs)
    273 		panic("zstty");
    274 #endif
    275 	zst = zsttycd.cd_devs[unit];
    276 	return (zst->zst_tty);
    277 }
    278 
    279 
    280 /*
    281  * Open a zs serial (tty) port.
    282  */
    283 int
    284 zsopen(dev, flags, mode, p)
    285 	dev_t dev;
    286 	int flags;
    287 	int mode;
    288 	struct proc *p;
    289 {
    290 	register struct tty *tp;
    291 	register struct zs_chanstate *cs;
    292 	struct zstty_softc *zst;
    293 	int error, s, unit;
    294 
    295 	unit = minor(dev);
    296 	if (unit >= zsttycd.cd_ndevs)
    297 		return (ENXIO);
    298 	zst = zsttycd.cd_devs[unit];
    299 	if (zst == NULL)
    300 		return (ENXIO);
    301 	tp = zst->zst_tty;
    302 	cs = zst->zst_cs;
    303 
    304 	/* If KGDB took the line, then tp==NULL */
    305 	if (tp == NULL)
    306 		return (EBUSY);
    307 
    308 	/* It's simpler to do this up here. */
    309 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
    310 	     ==             (TS_ISOPEN | TS_XCLUDE))
    311 	    && (p->p_ucred->cr_uid != 0) )
    312 	{
    313 		return (EBUSY);
    314 	}
    315 
    316 	s = spltty();
    317 
    318 	if ((tp->t_state & TS_ISOPEN) == 0) {
    319 		/* First open. */
    320 		ttychars(tp);
    321 		tp->t_iflag = TTYDEF_IFLAG;
    322 		tp->t_oflag = TTYDEF_OFLAG;
    323 		tp->t_cflag = ZSTTY_DEF_CFLAG;
    324 		if (zst->zst_swflags & TIOCFLAG_CLOCAL)
    325 			tp->t_cflag |= CLOCAL;
    326 		if (zst->zst_swflags & TIOCFLAG_CRTSCTS)
    327 			tp->t_cflag |= CRTSCTS;
    328 		if (zst->zst_swflags & TIOCFLAG_MDMBUF)
    329 			tp->t_cflag |= MDMBUF;
    330 		tp->t_lflag = TTYDEF_LFLAG;
    331 		tp->t_ispeed = tp->t_ospeed = cs->cs_defspeed;
    332 		(void) zsparam(tp, &tp->t_termios);
    333 		ttsetwater(tp);
    334 		/* Flush any pending input. */
    335 		zst->zst_rbget = zst->zst_rbput;
    336 		zs_iflush(cs);	/* XXX */
    337 		/* Turn on DTR */
    338 		zs_modem(zst, 1);
    339 		if (zst->zst_swflags & TIOCFLAG_SOFTCAR) {
    340 			tp->t_state |= TS_CARR_ON;
    341 		}
    342 	}
    343 	error = 0;
    344 
    345 	/* Wait for carrier. */
    346 	for (;;) {
    347 		register int rr0;
    348 
    349 		/* Might never get status intr if carrier already on. */
    350 		rr0 = *(cs->cs_reg_csr);
    351 		ZS_DELAY();
    352 		if (rr0 & ZSRR0_DCD) {
    353 			tp->t_state |= TS_CARR_ON;
    354 			break;
    355 		}
    356 
    357 		if ((tp->t_state & TS_CARR_ON) ||
    358 		    (tp->t_cflag & CLOCAL) ||
    359 		    (flags & O_NONBLOCK) )
    360 		{
    361 			break;
    362 		}
    363 
    364 		tp->t_state |= TS_WOPEN;
    365 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    366 			TTIPRI | PCATCH, ttopen, 0);
    367 		if (error) {
    368 			if ((tp->t_state & TS_ISOPEN) == 0) {
    369 				/* Never get here with softcar */
    370 				zs_modem(zst, 0);
    371 				tp->t_state &= ~TS_WOPEN;
    372 				ttwakeup(tp);
    373 			}
    374 			break;
    375 		}
    376 	}
    377 
    378 	splx(s);
    379 
    380 	if (error == 0)
    381 		error = linesw[tp->t_line].l_open(dev, tp);
    382 
    383 	return (error);
    384 }
    385 
    386 /*
    387  * Close a zs serial port.
    388  */
    389 int
    390 zsclose(dev, flags, mode, p)
    391 	dev_t dev;
    392 	int flags;
    393 	int mode;
    394 	struct proc *p;
    395 {
    396 	struct zstty_softc *zst;
    397 	register struct zs_chanstate *cs;
    398 	register struct tty *tp;
    399 	struct zsinfo *zi;
    400 	int hup, s;
    401 
    402 	zst = zsttycd.cd_devs[minor(dev)];
    403 	cs = zst->zst_cs;
    404 	tp = zst->zst_tty;
    405 
    406 	/* XXX This is for cons.c. */
    407 	if ((tp->t_state & TS_ISOPEN) == 0)
    408 		return 0;
    409 
    410 	(*linesw[tp->t_line].l_close)(tp, flags);
    411 	hup = tp->t_cflag & HUPCL;
    412 	if (zst->zst_swflags & TIOCFLAG_SOFTCAR)
    413 		hup = 0;
    414 	if (hup) {
    415 		zs_modem(zst, 0);
    416 		/* hold low for 1 second */
    417 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
    418 	}
    419 	if (cs->cs_creg[5] & ZSWR5_BREAK) {
    420 		zs_break(cs, 0);
    421 	}
    422 	/* XXX - turn off interrupts? */
    423 
    424 	ttyclose(tp);
    425 	return (0);
    426 }
    427 
    428 /*
    429  * Read/write zs serial port.
    430  */
    431 int
    432 zsread(dev, uio, flags)
    433 	dev_t dev;
    434 	struct uio *uio;
    435 	int flags;
    436 {
    437 	register struct zstty_softc *zst;
    438 	register struct tty *tp;
    439 
    440 	zst = zsttycd.cd_devs[minor(dev)];
    441 	tp = zst->zst_tty;
    442 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    443 }
    444 
    445 int
    446 zswrite(dev, uio, flags)
    447 	dev_t dev;
    448 	struct uio *uio;
    449 	int flags;
    450 {
    451 	register struct zstty_softc *zst;
    452 	register struct tty *tp;
    453 
    454 	zst = zsttycd.cd_devs[minor(dev)];
    455 	tp = zst->zst_tty;
    456 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    457 }
    458 
    459 #define TIOCFLAG_ALL (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | \
    460                       TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF )
    461 
    462 int
    463 zsioctl(dev, cmd, data, flag, p)
    464 	dev_t dev;
    465 	u_long cmd;
    466 	caddr_t data;
    467 	int flag;
    468 	struct proc *p;
    469 {
    470 	register struct zstty_softc *zst;
    471 	register struct zs_chanstate *cs;
    472 	register struct tty *tp;
    473 	register int error, tmp;
    474 
    475 	zst = zsttycd.cd_devs[minor(dev)];
    476 	cs = zst->zst_cs;
    477 	tp = zst->zst_tty;
    478 
    479 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    480 	if (error >= 0)
    481 		return (error);
    482 	error = ttioctl(tp, cmd, data, flag, p);
    483 	if (error >= 0)
    484 		return (error);
    485 
    486 	switch (cmd) {
    487 
    488 	case TIOCSBRK:
    489 		zs_break(cs, 1);
    490 		break;
    491 
    492 	case TIOCCBRK:
    493 		zs_break(cs, 0);
    494 		break;
    495 
    496 	case TIOCGFLAGS:
    497 		*(int *)data = zst->zst_swflags;
    498 		break;
    499 
    500 	case TIOCSFLAGS:
    501 		error = suser(p->p_ucred, &p->p_acflag);
    502 		if (error != 0)
    503 			return (EPERM);
    504 		tmp = *(int *)data;
    505 		/* Check for random bits... */
    506 		if (tmp & ~TIOCFLAG_ALL)
    507 			return(EINVAL);
    508 		/* Silently enforce softcar on the console. */
    509 		if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
    510 			tmp |= TIOCFLAG_SOFTCAR;
    511 		/* These flags take effect during open. */
    512 		zst->zst_swflags = tmp;
    513 		break;
    514 
    515 	case TIOCSDTR:
    516 		zs_modem(zst, 1);
    517 		break;
    518 
    519 	case TIOCCDTR:
    520 		zs_modem(zst, 0);
    521 		break;
    522 
    523 	case TIOCMSET:
    524 	case TIOCMBIS:
    525 	case TIOCMBIC:
    526 	case TIOCMGET:
    527 	default:
    528 		return (ENOTTY);
    529 	}
    530 	return (0);
    531 }
    532 
    533 /*
    534  * Start or restart transmission.
    535  */
    536 static void
    537 zsstart(tp)
    538 	register struct tty *tp;
    539 {
    540 	register struct zstty_softc *zst;
    541 	register struct zs_chanstate *cs;
    542 	register int s, nch;
    543 
    544 	zst = tp->t_sc;
    545 	cs = zst->zst_cs;
    546 
    547 	s = spltty();
    548 
    549 	/*
    550 	 * If currently active or delaying, no need to do anything.
    551 	 */
    552 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
    553 		goto out;
    554 
    555 	/*
    556 	 * If there are sleepers, and output has drained below low
    557 	 * water mark, awaken.
    558 	 */
    559 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    560 		if (tp->t_state & TS_ASLEEP) {
    561 			tp->t_state &= ~TS_ASLEEP;
    562 			wakeup((caddr_t)&tp->t_outq);
    563 		}
    564 		selwakeup(&tp->t_wsel);
    565 	}
    566 
    567 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
    568 	if (nch) {
    569 		register char *p = tp->t_outq.c_cf;
    570 
    571 		/* mark busy, enable tx done interrupts, & send first byte */
    572 		tp->t_state |= TS_BUSY;
    573 		(void) splzs();
    574 
    575 		cs->cs_preg[1] |= ZSWR1_TIE;
    576 		cs->cs_creg[1] |= ZSWR1_TIE;
    577 		ZS_WRITE(cs, 1, cs->cs_creg[1]);
    578 		*(cs->cs_reg_data) = *p;
    579 		ZS_DELAY();
    580 		zst->zst_tba = p + 1;
    581 		zst->zst_tbc = nch - 1;
    582 	} else {
    583 		/*
    584 		 * Nothing to send, turn off transmit done interrupts.
    585 		 * This is useful if something is doing polled output.
    586 		 */
    587 		(void) splzs();
    588 		cs->cs_preg[1] &= ~ZSWR1_TIE;
    589 		cs->cs_creg[1] &= ~ZSWR1_TIE;
    590 		ZS_WRITE(cs, 1, cs->cs_creg[1]);
    591 	}
    592 out:
    593 	splx(s);
    594 }
    595 
    596 /*
    597  * Stop output, e.g., for ^S or output flush.
    598  */
    599 int
    600 zsstop(tp, flag)
    601 	struct tty *tp;
    602 	int flag;
    603 {
    604 	register struct zstty_softc *zst;
    605 	register struct zs_chanstate *cs;
    606 	register int s;
    607 
    608 	zst = tp->t_sc;
    609 	cs = zst->zst_cs;
    610 
    611 	s = splzs();
    612 	if (tp->t_state & TS_BUSY) {
    613 		/*
    614 		 * Device is transmitting; must stop it.
    615 		 */
    616 		zst->zst_tbc = 0;
    617 		if ((tp->t_state & TS_TTSTOP) == 0)
    618 			tp->t_state |= TS_FLUSH;
    619 	}
    620 	splx(s);
    621 	return (0);
    622 }
    623 
    624 /*
    625  * Set ZS tty parameters from termios.
    626  * XXX - Should just copy the whole termios after
    627  * making sure all the changes could be done.
    628  * XXX - Only whack the UART when params change...
    629  */
    630 static int
    631 zsparam(tp, t)
    632 	register struct tty *tp;
    633 	register struct termios *t;
    634 {
    635 	register struct zstty_softc *zst;
    636 	register struct zs_chanstate *cs;
    637 	register int s, bps, cflag, tconst;
    638 	u_char tmp3, tmp4, tmp5, reset;
    639 
    640 	zst = tp->t_sc;
    641 	cs = zst->zst_cs;
    642 
    643 	/*
    644 	 * Because PCLK is only run at 4.9 MHz, the fastest we
    645 	 * can go is 51200 baud (this corresponds to TC=1).
    646 	 * This is somewhat unfortunate as there is no real
    647 	 * reason we should not be able to handle higher rates.
    648 	 */
    649 	bps = t->c_ospeed;
    650 	if (bps < 0 || (t->c_ispeed && t->c_ispeed != bps))
    651 		return (EINVAL);
    652 	if (bps == 0) {
    653 		/* stty 0 => drop DTR and RTS */
    654 		zs_modem(zst, 0);
    655 		return (0);
    656 	}
    657 	tconst = BPS_TO_TCONST(cs->cs_pclk_div16, bps);
    658 	if (tconst < 0)
    659 		return (EINVAL);
    660 
    661 	/* Convert back to make sure we can do it. */
    662 	bps = TCONST_TO_BPS(cs->cs_pclk_div16, tconst);
    663 	if (bps != t->c_ospeed)
    664 		return (EINVAL);
    665 	tp->t_ispeed = tp->t_ospeed = bps;
    666 
    667 	cflag = t->c_cflag;
    668 	tp->t_cflag = cflag;
    669 
    670 	/*
    671 	 * Block interrupts so that state will not
    672 	 * be altered until we are done setting it up.
    673 	 */
    674 	s = splzs();
    675 
    676 	/*
    677 	 * Initial values in cs_preg are set before
    678 	 * our attach routine is called.  The master
    679 	 * interrupt enable is handled by zsc.c
    680 	 */
    681 
    682 	cs->cs_preg[12] = tconst;
    683 	cs->cs_preg[13] = tconst >> 8;
    684 
    685 	switch (cflag & CSIZE) {
    686 	case CS5:
    687 		tmp3 = ZSWR3_RX_5;
    688 		tmp5 = ZSWR5_TX_5;
    689 		break;
    690 	case CS6:
    691 		tmp3 = ZSWR3_RX_6;
    692 		tmp5 = ZSWR5_TX_6;
    693 		break;
    694 	case CS7:
    695 		tmp3 = ZSWR3_RX_7;
    696 		tmp5 = ZSWR5_TX_7;
    697 		break;
    698 	case CS8:
    699 	default:
    700 		tmp3 = ZSWR3_RX_8;
    701 		tmp5 = ZSWR5_TX_8;
    702 		break;
    703 	}
    704 
    705 	/*
    706 	 * Output hardware flow control on the chip is horrendous: if
    707 	 * carrier detect drops, the receiver is disabled.  Hence we
    708 	 * can only do this when the carrier is on.
    709 	 */
    710 	tmp3 |= ZSWR3_RX_ENABLE;
    711 	if (cflag & CCTS_OFLOW) {
    712 		if (*(cs->cs_reg_csr) & ZSRR0_DCD)
    713 			tmp3 |= ZSWR3_HFC;
    714 		ZS_DELAY();
    715 	}
    716 
    717 	cs->cs_preg[3] = tmp3;
    718 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
    719 
    720 	tmp4 = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
    721 	if ((cflag & PARODD) == 0)
    722 		tmp4 |= ZSWR4_EVENP;
    723 	if (cflag & PARENB)
    724 		tmp4 |= ZSWR4_PARENB;
    725 	cs->cs_preg[4] = tmp4;
    726 
    727 	/*
    728 	 * If nothing is being transmitted, set up new current values,
    729 	 * else mark them as pending.
    730 	 */
    731 	if (cs->cs_heldchange == 0) {
    732 		if (tp->t_state & TS_BUSY) {
    733 			zst->zst_heldtbc = zst->zst_tbc;
    734 			zst->zst_tbc = 0;
    735 			cs->cs_heldchange = 1;
    736 		} else {
    737 			zs_loadchannelregs(cs);
    738 		}
    739 	}
    740 	splx(s);
    741 	return (0);
    742 }
    743 
    744 /*
    745  * Raise or lower modem control (DTR/RTS) signals.  If a character is
    746  * in transmission, the change is deferred.
    747  */
    748 static void
    749 zs_modem(zst, onoff)
    750 	struct zstty_softc *zst;
    751 	int onoff;
    752 {
    753 	struct zs_chanstate *cs;
    754 	struct tty *tp;
    755 	int s, bis, and;
    756 
    757 	cs = zst->zst_cs;
    758 	tp = zst->zst_tty;
    759 
    760 	if (onoff) {
    761 		bis = ZSWR5_DTR | ZSWR5_RTS;
    762 		and = ~0;
    763 	} else {
    764 		bis = 0;
    765 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
    766 	}
    767 	s = splzs();
    768 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
    769 	if (cs->cs_heldchange == 0) {
    770 		if (tp->t_state & TS_BUSY) {
    771 			zst->zst_heldtbc = zst->zst_tbc;
    772 			zst->zst_tbc = 0;
    773 			cs->cs_heldchange = 1;
    774 		} else {
    775 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
    776 			ZS_WRITE(cs, 5, cs->cs_creg[5]);
    777 		}
    778 	}
    779 	splx(s);
    780 }
    781 
    782 
    783 /****************************************************************
    784  * Interface to the lower layer (zscc)
    785  ****************************************************************/
    786 
    787 static int
    788 zstty_rxint(cs)
    789 	register struct zs_chanstate *cs;
    790 {
    791 	register struct zstty_softc *zst;
    792 	register put, put_next;
    793 	register u_char c, rr0, rr1;
    794 
    795 	zst = cs->cs_private;
    796 	put = zst->zst_rbput;
    797 
    798 nextchar:
    799 	/* Read the input data ASAP. */
    800 	c = *(cs->cs_reg_data);
    801 	ZS_DELAY();
    802 
    803 	/* Save the status register too. */
    804 	rr1 = ZS_READ(cs, 1);
    805 
    806 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    807 		/* Clear the receive error. */
    808 		*(cs->cs_reg_csr) = ZSWR0_RESET_ERRORS;
    809 		ZS_DELAY();
    810 	}
    811 
    812 	zst->zst_rbuf[put] = (c << 8) | rr1;
    813 	put_next = (put + 1) & ZSTTY_RING_MASK;
    814 
    815 	/* Would overrun if increment makes (put==get). */
    816 	if (put_next == zst->zst_rbget) {
    817 		zst->zst_intr_flags |= INTR_RX_OVERRUN;
    818 	} else {
    819 		/* OK, really increment. */
    820 		put = put_next;
    821 	}
    822 
    823 	/* Keep reading until the FIFO is empty. */
    824 	rr0 = *(cs->cs_reg_csr);
    825 	ZS_DELAY();
    826 	if (rr0 & ZSRR0_RX_READY)
    827 		goto nextchar;
    828 
    829 	/* Done reading. */
    830 	zst->zst_rbput = put;
    831 
    832 	/* Ask for softint() call. */
    833 	cs->cs_softreq = 1;
    834 	return(1);
    835 }
    836 
    837 static int
    838 zstty_txint(cs)
    839 	register struct zs_chanstate *cs;
    840 {
    841 	register struct zstty_softc *zst;
    842 	register int count, rval;
    843 
    844 	zst = cs->cs_private;
    845 	count = zst->zst_tbc;
    846 
    847 	if (count > 0) {
    848 		/* Send the next char. */
    849 		*(cs->cs_reg_data) = *zst->zst_tba++;
    850 		ZS_DELAY();
    851 		zst->zst_tbc = --count;
    852 		rval = 0;
    853 	} else {
    854 		/* Nothing more to send. */
    855 		*(cs->cs_reg_csr) = ZSWR0_RESET_TXINT;
    856 		ZS_DELAY();
    857 		zst->zst_intr_flags |= INTR_TX_EMPTY;
    858 		rval = 1;	/* want softcall */
    859 	}
    860 
    861 	cs->cs_softreq = rval;
    862 	return (rval);
    863 }
    864 
    865 static int
    866 zstty_stint(cs)
    867 	register struct zs_chanstate *cs;
    868 {
    869 	register struct zstty_softc *zst;
    870 	register int rr0;
    871 
    872 	zst = cs->cs_private;
    873 
    874 	rr0 = *(cs->cs_reg_csr);
    875 	ZS_DELAY();
    876 
    877 	*(cs->cs_reg_csr) = ZSWR0_RESET_STATUS;
    878 	ZS_DELAY();
    879 
    880 	if ((rr0 & ZSRR0_BREAK) &&
    881 		(zst->zst_hwflags & ZS_HWFLAG_CONSOLE))
    882 	{
    883 		zs_abort();
    884 		return (0);
    885 	}
    886 
    887 	zst->zst_intr_flags |= INTR_ST_CHECK;
    888 	/* Ask for softint() call. */
    889 	cs->cs_softreq = 1;
    890 	return (1);
    891 }
    892 
    893 /*
    894  * Print out a ring or fifo overrun error message.
    895  */
    896 static void
    897 zsoverrun(zst, ptime, what)
    898 	struct zstty_softc *zst;
    899 	long *ptime;
    900 	char *what;
    901 {
    902 
    903 	if (*ptime != time.tv_sec) {
    904 		*ptime = time.tv_sec;
    905 		log(LOG_WARNING, "%s: %s overrun\n",
    906 			zst->zst_dev.dv_xname, what);
    907 	}
    908 }
    909 
    910 static int
    911 zstty_softint(cs)
    912 	struct zs_chanstate *cs;
    913 {
    914 	register struct zstty_softc *zst;
    915 	register struct linesw *line;
    916 	register struct tty *tp;
    917 	register int get, c, s;
    918 	int intr_flags;
    919 	register u_short ring_data;
    920 	register u_char rr0, rr1;
    921 
    922 	zst  = cs->cs_private;
    923 	tp   = zst->zst_tty;
    924 	line = &linesw[tp->t_line];
    925 
    926 	/* Atomically get and clear flags. */
    927 	s = splzs();
    928 	intr_flags = zst->zst_intr_flags;
    929 	zst->zst_intr_flags = 0;
    930 	splx(s);
    931 
    932 	if (intr_flags & INTR_RX_OVERRUN) {
    933 		/* May turn this on again below. */
    934 		intr_flags &= ~INTR_RX_OVERRUN;
    935 		zsoverrun(zst, "ring");
    936 	}
    937 
    938 	/*
    939 	 * Copy data from the receive ring into the tty layer.
    940 	 */
    941 	get = zst->zst_rbget;
    942 	while (get != zst->zst_rbput) {
    943 		ring_data = zst->zst_rbuf[get];
    944 		get = (get + 1) & ZSTTY_RING_MASK;
    945 
    946 		if (ring_data & ZSRR1_DO)
    947 			intr_flags |= INTR_RX_OVERRUN;
    948 		/* low byte of ring_data is rr1 */
    949 		c = (ring_data >> 8) & 0xff;
    950 		if (ring_data & ZSRR1_FE)
    951 			c |= TTY_FE;
    952 		if (ring_data & ZSRR1_PE)
    953 			c |= TTY_PE;
    954 
    955 		line->l_rint(c, tp);
    956 	}
    957 	zst->zst_rbget = get;
    958 
    959 	/* If set, it is from the loop above. */
    960 	if (intr_flags & INTR_RX_OVERRUN) {
    961 		zsoverrun(zst, "fifo");
    962 	}
    963 
    964 	if (intr_flags & INTR_TX_EMPTY) {
    965 		/*
    966 		 * Transmit done.  Change registers and resume,
    967 		 * or just clear BUSY.
    968 		 */
    969 		if (cs->cs_heldchange) {
    970 			s = splzs();
    971 			rr0 = *(cs->cs_reg_csr);
    972 			ZS_DELAY();
    973 			if ((rr0 & ZSRR0_DCD) == 0)
    974 				cs->cs_preg[3] &= ~ZSWR3_HFC;
    975 			zs_loadchannelregs(cs);
    976 			splx(s);
    977 			cs->cs_heldchange = 0;
    978 
    979 			if (zst->zst_heldtbc &&
    980 				(tp->t_state & TS_TTSTOP) == 0)
    981 			{
    982 				zst->zst_tbc = zst->zst_heldtbc - 1;
    983 				*(cs->cs_reg_data) = *zst->zst_tba++;
    984 				ZS_DELAY();
    985 				goto tx_resumed;
    986 			}
    987 		}
    988 		tp->t_state &= ~TS_BUSY;
    989 		if (tp->t_state & TS_FLUSH)
    990 			tp->t_state &= ~TS_FLUSH;
    991 		else
    992 			ndflush(&tp->t_outq, zst->zst_tba -
    993 					(caddr_t) tp->t_outq.c_cf);
    994 		line->l_start(tp);
    995 	tx_resumed:
    996 	}
    997 
    998 	if (intr_flags & INTR_ST_CHECK) {
    999 		/*
   1000 		 * Status line change.
   1001 		 *
   1002 		 * The chip's hardware flow control is, as noted in zsreg.h,
   1003 		 * busted---if the DCD line goes low the chip shuts off the
   1004 		 * receiver (!).  If we want hardware CTS flow control but do
   1005 		 * not have it, and carrier is now on, turn HFC on; if we have
   1006 		 * HFC now but carrier has gone low, turn it off.
   1007 		 */
   1008 		s = splzs();
   1009 		rr0 = *(cs->cs_reg_csr);
   1010 		if (rr0 & ZSRR0_DCD) {
   1011 			if (tp->t_cflag & CCTS_OFLOW &&
   1012 				(cs->cs_creg[3] & ZSWR3_HFC) == 0) {
   1013 				cs->cs_creg[3] |= ZSWR3_HFC;
   1014 				ZS_WRITE(cs, 3, cs->cs_creg[3]);
   1015 			}
   1016 		} else {
   1017 			if (cs->cs_creg[3] & ZSWR3_HFC) {
   1018 				cs->cs_creg[3] &= ~ZSWR3_HFC;
   1019 				ZS_WRITE(cs, 3, cs->cs_creg[3]);
   1020 			}
   1021 		}
   1022 		splx(s);
   1023 
   1024 		/* Was there a change on DCD? */
   1025 		if ((rr0 ^ cs->cs_rr0) & ZSRR0_DCD) {
   1026 			c = ((rr0 & ZSRR0_DCD) != 0);
   1027 			if (line->l_modem(tp, c) == 0)
   1028 				zs_modem(zst, c);
   1029 		}
   1030 		cs->cs_rr0 = rr0;
   1031 	}
   1032 
   1033 	return (1);
   1034 }
   1035 
   1036 struct zsops zsops_tty = {
   1037 	zstty_rxint,	/* receive char available */
   1038 	zstty_stint,	/* external/status */
   1039 	zstty_txint,	/* xmit buffer empty */
   1040 	zstty_softint,	/* process software interrupt */
   1041 };
   1042 
   1043