Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.1
      1  1.1  oki /*	$NetBSD: zs.c,v 1.1 1996/05/05 12:17:06 oki Exp $ */
      2  1.1  oki 
      3  1.1  oki /*
      4  1.1  oki  * Copyright (c) 1992, 1993
      5  1.1  oki  *	The Regents of the University of California.  All rights reserved.
      6  1.1  oki  *
      7  1.1  oki  * This software was developed by the Computer Systems Engineering group
      8  1.1  oki  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1  oki  * contributed to Berkeley.
     10  1.1  oki  *
     11  1.1  oki  * All advertising materials mentioning features or use of this software
     12  1.1  oki  * must display the following acknowledgement:
     13  1.1  oki  *	This product includes software developed by the University of
     14  1.1  oki  *	California, Lawrence Berkeley Laboratory.
     15  1.1  oki  *
     16  1.1  oki  * Redistribution and use in source and binary forms, with or without
     17  1.1  oki  * modification, are permitted provided that the following conditions
     18  1.1  oki  * are met:
     19  1.1  oki  * 1. Redistributions of source code must retain the above copyright
     20  1.1  oki  *    notice, this list of conditions and the following disclaimer.
     21  1.1  oki  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  oki  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  oki  *    documentation and/or other materials provided with the distribution.
     24  1.1  oki  * 3. All advertising materials mentioning features or use of this software
     25  1.1  oki  *    must display the following acknowledgement:
     26  1.1  oki  *	This product includes software developed by the University of
     27  1.1  oki  *	California, Berkeley and its contributors.
     28  1.1  oki  * 4. Neither the name of the University nor the names of its contributors
     29  1.1  oki  *    may be used to endorse or promote products derived from this software
     30  1.1  oki  *    without specific prior written permission.
     31  1.1  oki  *
     32  1.1  oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  1.1  oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.1  oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.1  oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  1.1  oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.1  oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.1  oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.1  oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.1  oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.1  oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.1  oki  * SUCH DAMAGE.
     43  1.1  oki  *
     44  1.1  oki  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     45  1.1  oki  */
     46  1.1  oki 
     47  1.1  oki /*
     48  1.1  oki  * Zilog Z8530 (ZSCC) driver.
     49  1.1  oki  *
     50  1.1  oki  * Runs two tty ports (ttya and ttyb) on zs0,
     51  1.1  oki  * and runs a keyboard and mouse on zs1.
     52  1.1  oki  *
     53  1.1  oki  * This driver knows far too much about chip to usage mappings.
     54  1.1  oki  */
     55  1.1  oki #include "zs.h"
     56  1.1  oki #if NZS > 0
     57  1.1  oki 
     58  1.1  oki #include <sys/param.h>
     59  1.1  oki #include <sys/systm.h>
     60  1.1  oki #include <sys/proc.h>
     61  1.1  oki #include <sys/device.h>
     62  1.1  oki #include <sys/conf.h>
     63  1.1  oki #include <sys/file.h>
     64  1.1  oki #include <sys/ioctl.h>
     65  1.1  oki #include <sys/tty.h>
     66  1.1  oki #include <sys/time.h>
     67  1.1  oki #include <sys/kernel.h>
     68  1.1  oki #include <sys/syslog.h>
     69  1.1  oki 
     70  1.1  oki #include <machine/cpu.h>
     71  1.1  oki 
     72  1.1  oki #include <x68k/x68k/iodevice.h>
     73  1.1  oki #include <dev/ic/z8530reg.h>
     74  1.1  oki #include <x68k/dev/zsvar.h>
     75  1.1  oki 
     76  1.1  oki #ifdef KGDB
     77  1.1  oki #include <machine/remote-sl.h>
     78  1.1  oki #endif
     79  1.1  oki 
     80  1.1  oki #define	ZSMAJOR	12		/* XXX */
     81  1.1  oki 
     82  1.1  oki #define	ZS_MOUSE	1	/* XXX */
     83  1.1  oki 
     84  1.1  oki #define PCLK	(5*1000*1000)	/* PCLK pin input clock rate */
     85  1.1  oki 
     86  1.1  oki #if 0
     87  1.1  oki /*
     88  1.1  oki  * Select software interrupt bit based on TTY ipl.
     89  1.1  oki  */
     90  1.1  oki #if PIL_TTY == 1
     91  1.1  oki # define IE_ZSSOFT IE_L1
     92  1.1  oki #elif PIL_TTY == 4
     93  1.1  oki # define IE_ZSSOFT IE_L4
     94  1.1  oki #elif PIL_TTY == 6
     95  1.1  oki # define IE_ZSSOFT IE_L6
     96  1.1  oki #else
     97  1.1  oki # error "no suitable software interrupt bit"
     98  1.1  oki #endif
     99  1.1  oki #endif
    100  1.1  oki 
    101  1.1  oki /*
    102  1.1  oki  * Software state per found chip.  This would be called `zs_softc',
    103  1.1  oki  * but the previous driver had a rather different zs_softc....
    104  1.1  oki  */
    105  1.1  oki struct zs_softc {
    106  1.1  oki 	struct	device zi_dev;		/* base device */
    107  1.1  oki 	volatile struct zsdevice *zi_zs;/* chip registers */
    108  1.1  oki 	struct	zs_chanstate zi_cs[2];	/* channel A and B software state */
    109  1.1  oki };
    110  1.1  oki 
    111  1.1  oki struct tty *zs_tty[NZS * 2];		/* XXX should be dynamic */
    112  1.1  oki 
    113  1.1  oki /* Definition of the driver for autoconfig. */
    114  1.1  oki static int	zsmatch __P((struct device *, void *, void *));
    115  1.1  oki static void	zsattach __P((struct device *, struct device *, void *));
    116  1.1  oki 
    117  1.1  oki struct cfattach zs_ca = {
    118  1.1  oki 	sizeof(struct zs_softc), zsmatch, zsattach
    119  1.1  oki };
    120  1.1  oki 
    121  1.1  oki struct cfdriver zs_cd = {
    122  1.1  oki 	NULL, "zs", DV_TTY, NULL, 0
    123  1.1  oki };
    124  1.1  oki 
    125  1.1  oki #ifdef x68k
    126  1.1  oki static struct zs_chanstate *zsms;
    127  1.1  oki void zs_msmodem __P((int));
    128  1.1  oki #endif
    129  1.1  oki 
    130  1.1  oki /* Interrupt handlers. */
    131  1.1  oki void	zshard __P((int));
    132  1.1  oki int	zssoft __P((void *));
    133  1.1  oki 
    134  1.1  oki struct zs_chanstate *zslist;
    135  1.1  oki 
    136  1.1  oki /* Routines called from other code. */
    137  1.1  oki static void	zsiopen __P((struct tty *));
    138  1.1  oki static void	zsiclose __P((struct tty *));
    139  1.1  oki static void	zsstart __P((struct tty *));
    140  1.1  oki int		zsstop __P((struct tty *, int));
    141  1.1  oki static int	zsparam __P((struct tty *, struct termios *));
    142  1.1  oki static int	zshwiflow __P((struct tty *, int));
    143  1.1  oki 
    144  1.1  oki /* Routines purely local to this driver. */
    145  1.1  oki static int	zs_getspeed __P((volatile struct zschan *));
    146  1.1  oki #ifdef KGDB
    147  1.1  oki static void	zs_reset __P((volatile struct zschan *, int, int));
    148  1.1  oki #endif
    149  1.1  oki static void	zs_modem __P((struct zs_chanstate *, int));
    150  1.1  oki static void	zs_loadchannelregs __P((volatile struct zschan *, u_char *));
    151  1.1  oki static void	zsabort __P((void));
    152  1.1  oki 
    153  1.1  oki /* Console stuff. */
    154  1.1  oki static struct tty *zs_ctty;	/* console `struct tty *' */
    155  1.1  oki static int zs_consin = -1, zs_consout = -1;
    156  1.1  oki static void zscnputc __P((int));	/* console putc function */
    157  1.1  oki static volatile struct zschan *zs_conschan;
    158  1.1  oki static struct tty *zs_checkcons __P((struct zs_softc *, int, struct zs_chanstate *));
    159  1.1  oki 
    160  1.1  oki #ifdef KGDB
    161  1.1  oki /* KGDB stuff.  Must reboot to change zs_kgdbunit. */
    162  1.1  oki extern int kgdb_dev, kgdb_rate;
    163  1.1  oki static int zs_kgdb_savedspeed;
    164  1.1  oki static void zs_checkkgdb __P((int, struct zs_chanstate *, struct tty *));
    165  1.1  oki #endif
    166  1.1  oki 
    167  1.1  oki static volatile struct zsdevice *findzs __P((int));
    168  1.1  oki static volatile struct zsdevice *zsaddr[NZS];	/* XXX, but saves work */
    169  1.1  oki 
    170  1.1  oki int zshardscope;
    171  1.1  oki int zsshortcuts;		/* number of "shortcut" software interrupts */
    172  1.1  oki 
    173  1.1  oki static u_char
    174  1.1  oki zs_read(zc, reg)
    175  1.1  oki 	volatile struct zschan *zc;
    176  1.1  oki 	u_char reg;
    177  1.1  oki {
    178  1.1  oki 	u_char val;
    179  1.1  oki 
    180  1.1  oki 	zc->zc_csr = reg;
    181  1.1  oki 	ZS_DELAY();
    182  1.1  oki 	val = zc->zc_csr;
    183  1.1  oki 	ZS_DELAY();
    184  1.1  oki 	return val;
    185  1.1  oki }
    186  1.1  oki 
    187  1.1  oki static u_char
    188  1.1  oki zs_write(zc, reg, val)
    189  1.1  oki 	volatile struct zschan *zc;
    190  1.1  oki 	u_char reg, val;
    191  1.1  oki {
    192  1.1  oki 	zc->zc_csr = reg;
    193  1.1  oki 	ZS_DELAY();
    194  1.1  oki 	zc->zc_csr = val;
    195  1.1  oki 	ZS_DELAY();
    196  1.1  oki 	return val;
    197  1.1  oki }
    198  1.1  oki 
    199  1.1  oki /*
    200  1.1  oki  * find zs address for x68k architecture
    201  1.1  oki  */
    202  1.1  oki static volatile struct zsdevice *
    203  1.1  oki findzs(zs)
    204  1.1  oki 	int zs;
    205  1.1  oki {
    206  1.1  oki 	if (zs == 0)
    207  1.1  oki 		return &IODEVbase->io_inscc;
    208  1.1  oki 	if (1 <= zs && zs <= 4)
    209  1.1  oki 		return &(IODEVbase->io_exscc)[zs - 1];
    210  1.1  oki 	/* none */
    211  1.1  oki 	return 0;
    212  1.1  oki }
    213  1.1  oki 
    214  1.1  oki /*
    215  1.1  oki  * Match slave number to zs unit number, so that misconfiguration will
    216  1.1  oki  * not set up the keyboard as ttya, etc.
    217  1.1  oki  */
    218  1.1  oki static int
    219  1.1  oki zsmatch(parent, match, aux)
    220  1.1  oki 	struct device *parent;
    221  1.1  oki 	void *match, *aux;
    222  1.1  oki {
    223  1.1  oki 	struct cfdata *cfp = match;
    224  1.1  oki 	volatile void *addr;
    225  1.1  oki 
    226  1.1  oki 	if(strcmp("zs", aux) || (addr = findzs(cfp->cf_unit)) == 0)
    227  1.1  oki 		return(0);
    228  1.1  oki 	if (badaddr(addr))
    229  1.1  oki 		return 0;
    230  1.1  oki 	return(1);
    231  1.1  oki }
    232  1.1  oki 
    233  1.1  oki /*
    234  1.1  oki  * Attach a found zs.
    235  1.1  oki  *
    236  1.1  oki  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    237  1.1  oki  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    238  1.1  oki  */
    239  1.1  oki static void
    240  1.1  oki zsattach(parent, dev, aux)
    241  1.1  oki 	struct device *parent;
    242  1.1  oki 	struct device *dev;
    243  1.1  oki 	void *aux;
    244  1.1  oki {
    245  1.1  oki 	register int zs = dev->dv_unit, unit;
    246  1.1  oki 	register struct zs_softc *zi;
    247  1.1  oki 	register struct zs_chanstate *cs;
    248  1.1  oki 	register volatile struct zsdevice *addr;
    249  1.1  oki 	register struct tty *tp, *ctp;
    250  1.1  oki 	register struct confargs *ca = aux;
    251  1.1  oki 	int pri;
    252  1.1  oki 
    253  1.1  oki 	if ((addr = zsaddr[zs]) == NULL)
    254  1.1  oki 		addr = zsaddr[zs] = findzs(zs);
    255  1.1  oki 	printf(" (%s)\n", zs ? "external" : "onboard");
    256  1.1  oki 	zi = (struct zs_softc *)dev;
    257  1.1  oki 	zi->zi_zs = addr;
    258  1.1  oki 	unit = zs * 2;
    259  1.1  oki 	cs = zi->zi_cs;
    260  1.1  oki 	cs->cs_ttyp = tp = ttymalloc();
    261  1.1  oki 
    262  1.1  oki 	/* link into interrupt list with order (A,B) (B=A+1) */
    263  1.1  oki 	cs[0].cs_next = &cs[1];
    264  1.1  oki 	cs[1].cs_next = zslist;
    265  1.1  oki 	zslist = cs;
    266  1.1  oki 
    267  1.1  oki 	cs->cs_unit = unit;
    268  1.1  oki 	cs->cs_speed = zs_getspeed(&addr->zs_chan[ZS_CHAN_A]);
    269  1.1  oki 	cs->cs_zc = &addr->zs_chan[ZS_CHAN_A];
    270  1.1  oki 	tp->t_dev = makedev(ZSMAJOR, unit);
    271  1.1  oki 	tp->t_oproc = zsstart;
    272  1.1  oki 	tp->t_param = zsparam;
    273  1.1  oki 	tp->t_hwiflow = zshwiflow;
    274  1.1  oki 	if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
    275  1.1  oki 		cs->cs_ttyp = tp = ctp;
    276  1.1  oki #ifdef KGDB
    277  1.1  oki 	if (ctp == NULL)
    278  1.1  oki 		zs_checkkgdb(unit, cs, tp);
    279  1.1  oki #endif
    280  1.1  oki #ifdef sun
    281  1.1  oki 	if (unit == ZS_KBD) {
    282  1.1  oki 		/*
    283  1.1  oki 		 * Keyboard: tell /dev/kbd driver how to talk to us.
    284  1.1  oki 		 */
    285  1.1  oki 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    286  1.1  oki 		tp->t_cflag = CS8;
    287  1.1  oki 		kbd_serial(tp, zsiopen, zsiclose);
    288  1.1  oki 		cs->cs_conk = 1;		/* do L1-A processing */
    289  1.1  oki 	}
    290  1.1  oki #endif
    291  1.1  oki 	ZS_WRITE(cs->cs_zc, 2, 0x70 + zs); /* XXX interrupt vector */
    292  1.1  oki 	unit++;
    293  1.1  oki 	cs++;
    294  1.1  oki 	cs->cs_ttyp = tp = ttymalloc();
    295  1.1  oki 	cs->cs_unit = unit;
    296  1.1  oki 	cs->cs_speed = zs_getspeed(&addr->zs_chan[ZS_CHAN_B]);
    297  1.1  oki 	cs->cs_zc = &addr->zs_chan[ZS_CHAN_B];
    298  1.1  oki 	tp->t_dev = makedev(ZSMAJOR, unit);
    299  1.1  oki 	tp->t_oproc = zsstart;
    300  1.1  oki 	tp->t_param = zsparam;
    301  1.1  oki 	tp->t_hwiflow = zshwiflow;
    302  1.1  oki 	if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
    303  1.1  oki 		cs->cs_ttyp = tp = ctp;
    304  1.1  oki #ifdef KGDB
    305  1.1  oki 	if (ctp == NULL)
    306  1.1  oki 		zs_checkkgdb(unit, cs, tp);
    307  1.1  oki #endif
    308  1.1  oki 	if (unit == ZS_MOUSE) {
    309  1.1  oki 		/*
    310  1.1  oki 		 * Mouse: tell /dev/mouse driver how to talk to us.
    311  1.1  oki 		 */
    312  1.1  oki 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    313  1.1  oki 		tp->t_cflag = CS8 | CSTOPB;
    314  1.1  oki 		ms_serial(tp, zsiopen, zsiclose);
    315  1.1  oki #ifdef x68k
    316  1.1  oki 		zsms = cs;
    317  1.1  oki #endif
    318  1.1  oki 	}
    319  1.1  oki }
    320  1.1  oki 
    321  1.1  oki #ifdef KGDB
    322  1.1  oki /*
    323  1.1  oki  * Put a channel in a known state.  Interrupts may be left disabled
    324  1.1  oki  * or enabled, as desired.
    325  1.1  oki  */
    326  1.1  oki static void
    327  1.1  oki zs_reset(zc, inten, speed)
    328  1.1  oki 	volatile struct zschan *zc;
    329  1.1  oki 	int inten, speed;
    330  1.1  oki {
    331  1.1  oki 	int tconst;
    332  1.1  oki 	static u_char reg[16] = {
    333  1.1  oki 		0,
    334  1.1  oki 		0,
    335  1.1  oki 		0,
    336  1.1  oki 		ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    337  1.1  oki 		ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    338  1.1  oki 		ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    339  1.1  oki 		0,
    340  1.1  oki 		0,
    341  1.1  oki 		0,
    342  1.1  oki 		0,
    343  1.1  oki 		ZSWR10_NRZ,
    344  1.1  oki 		ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    345  1.1  oki 		0,
    346  1.1  oki 		0,
    347  1.1  oki 		ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
    348  1.1  oki 		ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    349  1.1  oki 	};
    350  1.1  oki 
    351  1.1  oki 	reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
    352  1.1  oki 	tconst = BPS_TO_TCONST(PCLK / 16, speed);
    353  1.1  oki 	reg[12] = tconst;
    354  1.1  oki 	reg[13] = tconst >> 8;
    355  1.1  oki 	zs_loadchannelregs(zc, reg);
    356  1.1  oki }
    357  1.1  oki #endif
    358  1.1  oki 
    359  1.1  oki /*
    360  1.1  oki  * Declare the given tty (which is in fact &cons) as a console input
    361  1.1  oki  * or output.  This happens before the zs chip is attached; the hookup
    362  1.1  oki  * is finished later, in zs_setcons() below.
    363  1.1  oki  *
    364  1.1  oki  * This is used only for ports a and b.  The console keyboard is decoded
    365  1.1  oki  * independently (we always send unit-2 input to /dev/kbd, which will
    366  1.1  oki  * direct it to /dev/console if appropriate).
    367  1.1  oki  */
    368  1.1  oki void
    369  1.1  oki zsconsole(tp, unit, out, fnstop)
    370  1.1  oki 	register struct tty *tp;
    371  1.1  oki 	register int unit;
    372  1.1  oki 	int out;
    373  1.1  oki 	int (**fnstop) __P((struct tty *, int));
    374  1.1  oki {
    375  1.1  oki 	int zs;
    376  1.1  oki 	volatile struct zsdevice *addr;
    377  1.1  oki 
    378  1.1  oki 	if (out) {
    379  1.1  oki 		zs_consout = unit;
    380  1.1  oki 		zs = unit >> 1;
    381  1.1  oki 		if ((addr = zsaddr[zs]) == NULL)
    382  1.1  oki 			addr = zsaddr[zs] = findzs(zs);
    383  1.1  oki 		zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[ZS_CHAN_A] :
    384  1.1  oki 		    &addr->zs_chan[ZS_CHAN_B];
    385  1.1  oki 		v_putc = zscnputc;
    386  1.1  oki 	} else
    387  1.1  oki 		zs_consin = unit;
    388  1.1  oki 	if(fnstop)
    389  1.1  oki 		*fnstop = &zsstop;
    390  1.1  oki 	zs_ctty = tp;
    391  1.1  oki }
    392  1.1  oki 
    393  1.1  oki /*
    394  1.1  oki  * Polled console output putchar.
    395  1.1  oki  */
    396  1.1  oki static void
    397  1.1  oki zscnputc(c)
    398  1.1  oki 	int c;
    399  1.1  oki {
    400  1.1  oki 	register volatile struct zschan *zc = zs_conschan;
    401  1.1  oki 	register int s;
    402  1.1  oki 
    403  1.1  oki 	if (c == '\n')
    404  1.1  oki 		zscnputc('\r');
    405  1.1  oki 	/*
    406  1.1  oki 	 * Must block output interrupts (i.e., raise to >= splzs) without
    407  1.1  oki 	 * lowering current ipl.  Need a better way.
    408  1.1  oki 	 */
    409  1.1  oki 	s = splhigh();
    410  1.1  oki #ifdef SUN4C		/* XXX */
    411  1.1  oki 	if (CPU_ISSUN4C && s <= (12 << 8))
    412  1.1  oki 		(void) splzs();
    413  1.1  oki #endif
    414  1.1  oki 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
    415  1.1  oki 		ZS_DELAY();
    416  1.1  oki 	zc->zc_data = c;
    417  1.1  oki 	ZS_DELAY();
    418  1.1  oki 	splx(s);
    419  1.1  oki }
    420  1.1  oki 
    421  1.1  oki /*
    422  1.1  oki  * Set up the given unit as console input, output, both, or neither, as
    423  1.1  oki  * needed.  Return console tty if it is to receive console input.
    424  1.1  oki  */
    425  1.1  oki static struct tty *
    426  1.1  oki zs_checkcons(zi, unit, cs)
    427  1.1  oki 	struct zs_softc *zi;
    428  1.1  oki 	int unit;
    429  1.1  oki 	struct zs_chanstate *cs;
    430  1.1  oki {
    431  1.1  oki 	register struct tty *tp;
    432  1.1  oki 	char *i, *o;
    433  1.1  oki 
    434  1.1  oki 	if ((tp = zs_ctty) == NULL) /* XXX */
    435  1.1  oki 		return (0);
    436  1.1  oki 	i = zs_consin == unit ? "input" : NULL;
    437  1.1  oki 	o = zs_consout == unit ? "output" : NULL;
    438  1.1  oki 	if (i == NULL && o == NULL)
    439  1.1  oki 		return (0);
    440  1.1  oki 
    441  1.1  oki 	/* rewire the minor device (gack) */
    442  1.1  oki 	tp->t_dev = makedev(major(tp->t_dev), unit);
    443  1.1  oki 
    444  1.1  oki 	/*
    445  1.1  oki 	 * Rewire input and/or output.  Note that baud rate reflects
    446  1.1  oki 	 * input settings, not output settings, but we can do no better
    447  1.1  oki 	 * if the console is split across two ports.
    448  1.1  oki 	 *
    449  1.1  oki 	 * XXX	split consoles don't work anyway -- this needs to be
    450  1.1  oki 	 *	thrown away and redone
    451  1.1  oki 	 */
    452  1.1  oki 	if (i) {
    453  1.1  oki 		tp->t_param = zsparam;
    454  1.1  oki 		tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    455  1.1  oki 		tp->t_cflag = CS8;
    456  1.1  oki 		ttsetwater(tp);
    457  1.1  oki 	}
    458  1.1  oki 	if (o) {
    459  1.1  oki 		tp->t_oproc = zsstart;
    460  1.1  oki 	}
    461  1.1  oki 	printf("%s%c: console %s\n",
    462  1.1  oki 	    zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o);
    463  1.1  oki 	cs->cs_consio = 1;
    464  1.1  oki 	cs->cs_brkabort = 1;
    465  1.1  oki 	return (tp);
    466  1.1  oki }
    467  1.1  oki 
    468  1.1  oki #ifdef KGDB
    469  1.1  oki /*
    470  1.1  oki  * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
    471  1.1  oki  * Pick up the current speed and character size and restore the original
    472  1.1  oki  * speed.
    473  1.1  oki  */
    474  1.1  oki static void
    475  1.1  oki zs_checkkgdb(unit, cs, tp)
    476  1.1  oki 	int unit;
    477  1.1  oki 	struct zs_chanstate *cs;
    478  1.1  oki 	struct tty *tp;
    479  1.1  oki {
    480  1.1  oki 
    481  1.1  oki 	if (kgdb_dev == makedev(ZSMAJOR, unit)) {
    482  1.1  oki 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    483  1.1  oki 		tp->t_cflag = CS8;
    484  1.1  oki 		cs->cs_kgdb = 1;
    485  1.1  oki 		cs->cs_speed = zs_kgdb_savedspeed;
    486  1.1  oki 		(void) zsparam(tp, &tp->t_termios);
    487  1.1  oki 	}
    488  1.1  oki }
    489  1.1  oki #endif
    490  1.1  oki 
    491  1.1  oki /*
    492  1.1  oki  * Compute the current baud rate given a ZSCC channel.
    493  1.1  oki  */
    494  1.1  oki static int
    495  1.1  oki zs_getspeed(zc)
    496  1.1  oki 	register volatile struct zschan *zc;
    497  1.1  oki {
    498  1.1  oki 	register int tconst;
    499  1.1  oki 
    500  1.1  oki 	tconst = ZS_READ(zc, 12);
    501  1.1  oki 	tconst |= ZS_READ(zc, 13) << 8;
    502  1.1  oki 	return (TCONST_TO_BPS(PCLK / 16, tconst));
    503  1.1  oki }
    504  1.1  oki 
    505  1.1  oki 
    506  1.1  oki /*
    507  1.1  oki  * Do an internal open.
    508  1.1  oki  */
    509  1.1  oki static void
    510  1.1  oki zsiopen(tp)
    511  1.1  oki 	struct tty *tp;
    512  1.1  oki {
    513  1.1  oki 
    514  1.1  oki 	(void) zsparam(tp, &tp->t_termios);
    515  1.1  oki 	ttsetwater(tp);
    516  1.1  oki 	tp->t_state = TS_ISOPEN | TS_CARR_ON;
    517  1.1  oki }
    518  1.1  oki 
    519  1.1  oki /*
    520  1.1  oki  * Do an internal close.  Eventually we should shut off the chip when both
    521  1.1  oki  * ports on it are closed.
    522  1.1  oki  */
    523  1.1  oki static void
    524  1.1  oki zsiclose(tp)
    525  1.1  oki 	struct tty *tp;
    526  1.1  oki {
    527  1.1  oki 
    528  1.1  oki 	ttylclose(tp, 0);	/* ??? */
    529  1.1  oki 	ttyclose(tp);		/* ??? */
    530  1.1  oki 	tp->t_state = 0;
    531  1.1  oki }
    532  1.1  oki 
    533  1.1  oki 
    534  1.1  oki /*
    535  1.1  oki  * Open a zs serial port.  This interface may not be used to open
    536  1.1  oki  * the keyboard and mouse ports. (XXX)
    537  1.1  oki  */
    538  1.1  oki int
    539  1.1  oki zsopen(dev, flags, mode, p)
    540  1.1  oki 	dev_t dev;
    541  1.1  oki 	int flags;
    542  1.1  oki 	int mode;
    543  1.1  oki 	struct proc *p;
    544  1.1  oki {
    545  1.1  oki 	register struct tty *tp;
    546  1.1  oki 	register struct zs_chanstate *cs;
    547  1.1  oki 	struct zs_softc *zi;
    548  1.1  oki 	int unit = minor(dev), zs = unit >> 1, error, s;
    549  1.1  oki 
    550  1.1  oki 	if (zs >= zs_cd.cd_ndevs || (zi = zs_cd.cd_devs[zs]) == NULL ||
    551  1.1  oki 	    unit == ZS_MOUSE)
    552  1.1  oki 		return (ENXIO);
    553  1.1  oki 	if (zi->zi_zs == NULL)
    554  1.1  oki 		return (ENXIO);
    555  1.1  oki 	cs = &zi->zi_cs[unit & 1];
    556  1.1  oki 	if (cs->cs_consio)
    557  1.1  oki 		return (ENXIO);		/* ??? */
    558  1.1  oki 	tp = cs->cs_ttyp;
    559  1.1  oki 	s = spltty();
    560  1.1  oki 	if ((tp->t_state & TS_ISOPEN) == 0) {
    561  1.1  oki 		ttychars(tp);
    562  1.1  oki 		if (tp->t_ispeed == 0) {
    563  1.1  oki 			tp->t_iflag = TTYDEF_IFLAG;
    564  1.1  oki 			tp->t_oflag = TTYDEF_OFLAG;
    565  1.1  oki 			tp->t_cflag = TTYDEF_CFLAG;
    566  1.1  oki 			tp->t_lflag = TTYDEF_LFLAG;
    567  1.1  oki 			tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
    568  1.1  oki 		}
    569  1.1  oki 		(void) zsparam(tp, &tp->t_termios);
    570  1.1  oki 		ttsetwater(tp);
    571  1.1  oki 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    572  1.1  oki 		splx(s);
    573  1.1  oki 		return (EBUSY);
    574  1.1  oki 	}
    575  1.1  oki 	error = 0;
    576  1.1  oki 	for (;;) {
    577  1.1  oki 		register int rr0;
    578  1.1  oki 
    579  1.1  oki 		/* loop, turning on the device, until carrier present */
    580  1.1  oki 		zs_modem(cs, 1);
    581  1.1  oki 		/* May never get status intr if carrier already on. -gwr */
    582  1.1  oki 		rr0 = cs->cs_zc->zc_csr;
    583  1.1  oki 		ZS_DELAY();
    584  1.1  oki 		if ((rr0 & ZSRR0_DCD) || cs->cs_softcar)
    585  1.1  oki 			tp->t_state |= TS_CARR_ON;
    586  1.1  oki 		if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
    587  1.1  oki 		    tp->t_state & TS_CARR_ON)
    588  1.1  oki 			break;
    589  1.1  oki 		tp->t_state |= TS_WOPEN;
    590  1.1  oki 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
    591  1.1  oki 				 ttopen, 0);
    592  1.1  oki 		if (error) {
    593  1.1  oki 			if (!(tp->t_state & TS_ISOPEN)) {
    594  1.1  oki 				zs_modem(cs, 0);
    595  1.1  oki 				tp->t_state &= ~TS_WOPEN;
    596  1.1  oki 				ttwakeup(tp);
    597  1.1  oki 			}
    598  1.1  oki 			splx(s);
    599  1.1  oki 			return error;
    600  1.1  oki 		}
    601  1.1  oki 	}
    602  1.1  oki 	splx(s);
    603  1.1  oki 	if (error == 0)
    604  1.1  oki 		error = linesw[tp->t_line].l_open(dev, tp);
    605  1.1  oki 	if (error)
    606  1.1  oki 		zs_modem(cs, 0);
    607  1.1  oki 	return (error);
    608  1.1  oki }
    609  1.1  oki 
    610  1.1  oki /*
    611  1.1  oki  * Close a zs serial port.
    612  1.1  oki  */
    613  1.1  oki int
    614  1.1  oki zsclose(dev, flags, mode, p)
    615  1.1  oki 	dev_t dev;
    616  1.1  oki 	int flags;
    617  1.1  oki 	int mode;
    618  1.1  oki 	struct proc *p;
    619  1.1  oki {
    620  1.1  oki 	register struct zs_chanstate *cs;
    621  1.1  oki 	register struct tty *tp;
    622  1.1  oki 	struct zs_softc *zi;
    623  1.1  oki 	int unit = minor(dev), s;
    624  1.1  oki 
    625  1.1  oki 	zi = zs_cd.cd_devs[unit >> 1];
    626  1.1  oki 	cs = &zi->zi_cs[unit & 1];
    627  1.1  oki 	tp = cs->cs_ttyp;
    628  1.1  oki 	linesw[tp->t_line].l_close(tp, flags);
    629  1.1  oki 	if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
    630  1.1  oki 	    (tp->t_state & TS_ISOPEN) == 0) {
    631  1.1  oki 		zs_modem(cs, 0);
    632  1.1  oki 		/* hold low for 1 second */
    633  1.1  oki 		(void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
    634  1.1  oki 	}
    635  1.1  oki 	if (cs->cs_creg[5] & ZSWR5_BREAK)
    636  1.1  oki 	{
    637  1.1  oki 		s = splzs();
    638  1.1  oki 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    639  1.1  oki 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    640  1.1  oki 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    641  1.1  oki 		splx(s);
    642  1.1  oki 	}
    643  1.1  oki 	ttyclose(tp);
    644  1.1  oki #ifdef KGDB
    645  1.1  oki 	/* Reset the speed if we're doing kgdb on this port */
    646  1.1  oki 	if (cs->cs_kgdb) {
    647  1.1  oki 		tp->t_ispeed = tp->t_ospeed = kgdb_rate;
    648  1.1  oki 		(void) zsparam(tp, &tp->t_termios);
    649  1.1  oki 	}
    650  1.1  oki #endif
    651  1.1  oki 	return (0);
    652  1.1  oki }
    653  1.1  oki 
    654  1.1  oki /*
    655  1.1  oki  * Read/write zs serial port.
    656  1.1  oki  */
    657  1.1  oki int
    658  1.1  oki zsread(dev, uio, flags)
    659  1.1  oki 	dev_t dev;
    660  1.1  oki 	struct uio *uio;
    661  1.1  oki 	int flags;
    662  1.1  oki {
    663  1.1  oki 	register struct zs_chanstate *cs;
    664  1.1  oki 	register struct zs_softc *zi;
    665  1.1  oki 	register struct tty *tp;
    666  1.1  oki 	int unit = minor(dev);
    667  1.1  oki 
    668  1.1  oki 	zi = zs_cd.cd_devs[unit >> 1];
    669  1.1  oki 	cs = &zi->zi_cs[unit & 1];
    670  1.1  oki 	tp = cs->cs_ttyp;
    671  1.1  oki 
    672  1.1  oki 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    673  1.1  oki 
    674  1.1  oki }
    675  1.1  oki 
    676  1.1  oki int
    677  1.1  oki zswrite(dev, uio, flags)
    678  1.1  oki 	dev_t dev;
    679  1.1  oki 	struct uio *uio;
    680  1.1  oki 	int flags;
    681  1.1  oki {
    682  1.1  oki 	register struct zs_chanstate *cs;
    683  1.1  oki 	register struct zs_softc *zi;
    684  1.1  oki 	register struct tty *tp;
    685  1.1  oki 	int unit = minor(dev);
    686  1.1  oki 
    687  1.1  oki 	zi = zs_cd.cd_devs[unit >> 1];
    688  1.1  oki 	cs = &zi->zi_cs[unit & 1];
    689  1.1  oki 	tp = cs->cs_ttyp;
    690  1.1  oki 
    691  1.1  oki 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    692  1.1  oki }
    693  1.1  oki 
    694  1.1  oki struct tty *
    695  1.1  oki zstty(dev)
    696  1.1  oki 	dev_t dev;
    697  1.1  oki {
    698  1.1  oki 	register struct zs_chanstate *cs;
    699  1.1  oki 	register struct zs_softc *zi;
    700  1.1  oki 	int unit = minor(dev);
    701  1.1  oki 
    702  1.1  oki 	zi = zs_cd.cd_devs[unit >> 1];
    703  1.1  oki 	cs = &zi->zi_cs[unit & 1];
    704  1.1  oki 
    705  1.1  oki 	return (cs->cs_ttyp);
    706  1.1  oki 
    707  1.1  oki }
    708  1.1  oki 
    709  1.1  oki /*
    710  1.1  oki  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    711  1.1  oki  * channels are kept in (A,B) pairs.
    712  1.1  oki  *
    713  1.1  oki  * Do just a little, then get out; set a software interrupt if more
    714  1.1  oki  * work is needed.
    715  1.1  oki  *
    716  1.1  oki  * We deliberately ignore the vectoring Zilog gives us, and match up
    717  1.1  oki  * only the number of `reset interrupt under service' operations, not
    718  1.1  oki  * the order.
    719  1.1  oki  */
    720  1.1  oki /* ARGSUSED */
    721  1.1  oki void
    722  1.1  oki zshard(intrarg)
    723  1.1  oki 	int intrarg;
    724  1.1  oki {
    725  1.1  oki 	register struct zs_chanstate *a;
    726  1.1  oki #define	b (a + 1)
    727  1.1  oki 	register volatile struct zschan *zc;
    728  1.1  oki 	register int rr3, intflags = 0, v, i;
    729  1.1  oki 	static int zsrint __P((struct zs_chanstate *, volatile struct zschan *));
    730  1.1  oki 	static int zsxint __P((struct zs_chanstate *, volatile struct zschan *));
    731  1.1  oki 	static int zssint __P((struct zs_chanstate *, volatile struct zschan *));
    732  1.1  oki 
    733  1.1  oki 	{
    734  1.1  oki 		a = &((struct zs_softc*)zs_cd.cd_devs[(intrarg >> 2) & 0x0f])->zi_cs[0];
    735  1.1  oki 		rr3 = ZS_READ(a->cs_zc, 3);
    736  1.1  oki 		if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
    737  1.1  oki 			intflags |= 2;
    738  1.1  oki 			zc = a->cs_zc;
    739  1.1  oki 			i = a->cs_rbput;
    740  1.1  oki 			if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
    741  1.1  oki 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    742  1.1  oki 				intflags |= 1;
    743  1.1  oki 			}
    744  1.1  oki 			if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
    745  1.1  oki 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    746  1.1  oki 				intflags |= 1;
    747  1.1  oki 				intflags |= 4;
    748  1.1  oki 			}
    749  1.1  oki 			if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
    750  1.1  oki 				a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    751  1.1  oki 				intflags |= 1;
    752  1.1  oki 			}
    753  1.1  oki 			a->cs_rbput = i;
    754  1.1  oki 		}
    755  1.1  oki 		if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
    756  1.1  oki 			intflags |= 2;
    757  1.1  oki 			zc = b->cs_zc;
    758  1.1  oki 			i = b->cs_rbput;
    759  1.1  oki 			if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
    760  1.1  oki 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    761  1.1  oki 				intflags |= 1;
    762  1.1  oki 			}
    763  1.1  oki 			if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
    764  1.1  oki 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    765  1.1  oki 				intflags |= 1;
    766  1.1  oki 				intflags |= 4;
    767  1.1  oki 			}
    768  1.1  oki 			if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
    769  1.1  oki 				b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    770  1.1  oki 				intflags |= 1;
    771  1.1  oki 			}
    772  1.1  oki 			b->cs_rbput = i;
    773  1.1  oki 		}
    774  1.1  oki 	}
    775  1.1  oki #undef b
    776  1.1  oki 	if (intflags & 1) {
    777  1.1  oki #if defined(SUN4C) || defined(SUN4M)
    778  1.1  oki 		if (CPU_ISSUN4M || CPU_ISSUN4C) {
    779  1.1  oki 			/* XXX -- but this will go away when zshard moves to locore.s */
    780  1.1  oki 			struct clockframe *p = intrarg;
    781  1.1  oki 
    782  1.1  oki 			if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
    783  1.1  oki 				zsshortcuts++;
    784  1.1  oki 				(void) spltty();
    785  1.1  oki 				if (zshardscope) {
    786  1.1  oki 					LED_ON;
    787  1.1  oki 					LED_OFF;
    788  1.1  oki 				}
    789  1.1  oki 				return (zssoft(intrarg));
    790  1.1  oki 			}
    791  1.1  oki 		}
    792  1.1  oki #endif
    793  1.1  oki #if x68k
    794  1.1  oki #define PSL_TTY PSL_IPL4 /* XXX */
    795  1.1  oki 		if (((intrarg >> 16) & PSL_IPL) < PSL_TTY) {
    796  1.1  oki 			zsshortcuts++;
    797  1.1  oki 			(void) spltty();
    798  1.1  oki 			zssoft(0/*intrarg*/);
    799  1.1  oki 			return;
    800  1.1  oki 		}
    801  1.1  oki 		setsoftserial();
    802  1.1  oki #else
    803  1.1  oki 		ienab_bis(IE_ZSSOFT);
    804  1.1  oki #endif
    805  1.1  oki 	}
    806  1.1  oki }
    807  1.1  oki 
    808  1.1  oki static int
    809  1.1  oki zsrint(cs, zc)
    810  1.1  oki 	register struct zs_chanstate *cs;
    811  1.1  oki 	register volatile struct zschan *zc;
    812  1.1  oki {
    813  1.1  oki 	register int c = zc->zc_data;
    814  1.1  oki 
    815  1.1  oki 	ZS_DELAY();
    816  1.1  oki #ifndef x68k
    817  1.1  oki 	if (cs->cs_conk) {
    818  1.1  oki 		register struct conk_state *conk = &zsconk_state;
    819  1.1  oki 
    820  1.1  oki 		/*
    821  1.1  oki 		 * Check here for console abort function, so that we
    822  1.1  oki 		 * can abort even when interrupts are locking up the
    823  1.1  oki 		 * machine.
    824  1.1  oki 		 */
    825  1.1  oki 		if (c == KBD_RESET) {
    826  1.1  oki 			conk->conk_id = 1;	/* ignore next byte */
    827  1.1  oki 			conk->conk_l1 = 0;
    828  1.1  oki 		} else if (conk->conk_id)
    829  1.1  oki 			conk->conk_id = 0;	/* stop ignoring bytes */
    830  1.1  oki 		else if (c == KBD_L1)
    831  1.1  oki 			conk->conk_l1 = 1;	/* L1 went down */
    832  1.1  oki 		else if (c == (KBD_L1|KBD_UP))
    833  1.1  oki 			conk->conk_l1 = 0;	/* L1 went up */
    834  1.1  oki 		else if (c == KBD_A && conk->conk_l1) {
    835  1.1  oki 			zsabort();
    836  1.1  oki 			conk->conk_l1 = 0;	/* we never see the up */
    837  1.1  oki 			goto clearit;		/* eat the A after L1-A */
    838  1.1  oki 		}
    839  1.1  oki 	}
    840  1.1  oki #endif
    841  1.1  oki #ifdef KGDB
    842  1.1  oki 	if (c == FRAME_START && cs->cs_kgdb &&
    843  1.1  oki 	    (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
    844  1.1  oki 		zskgdb(cs->cs_unit);
    845  1.1  oki 		goto clearit;
    846  1.1  oki 	}
    847  1.1  oki #endif
    848  1.1  oki 	/* compose receive character and status */
    849  1.1  oki 	c <<= 8;
    850  1.1  oki 	c |= ZS_READ(zc, 1);
    851  1.1  oki 
    852  1.1  oki 	/* clear receive error & interrupt condition */
    853  1.1  oki 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    854  1.1  oki 	ZS_DELAY();
    855  1.1  oki 	zc->zc_csr = ZSWR0_CLR_INTR;
    856  1.1  oki 	ZS_DELAY();
    857  1.1  oki 
    858  1.1  oki 	return (ZRING_MAKE(ZRING_RINT, c));
    859  1.1  oki 
    860  1.1  oki clearit:
    861  1.1  oki 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    862  1.1  oki 	ZS_DELAY();
    863  1.1  oki 	zc->zc_csr = ZSWR0_CLR_INTR;
    864  1.1  oki 	ZS_DELAY();
    865  1.1  oki 	return (0);
    866  1.1  oki }
    867  1.1  oki 
    868  1.1  oki static int
    869  1.1  oki zsxint(cs, zc)
    870  1.1  oki 	register struct zs_chanstate *cs;
    871  1.1  oki 	register volatile struct zschan *zc;
    872  1.1  oki {
    873  1.1  oki 	register int i = cs->cs_tbc;
    874  1.1  oki 
    875  1.1  oki 	if (i == 0) {
    876  1.1  oki 		zc->zc_csr = ZSWR0_RESET_TXINT;
    877  1.1  oki 		ZS_DELAY();
    878  1.1  oki 		zc->zc_csr = ZSWR0_CLR_INTR;
    879  1.1  oki 		ZS_DELAY();
    880  1.1  oki 		return (ZRING_MAKE(ZRING_XINT, 0));
    881  1.1  oki 	}
    882  1.1  oki 	cs->cs_tbc = i - 1;
    883  1.1  oki 	zc->zc_data = *cs->cs_tba++;
    884  1.1  oki 	ZS_DELAY();
    885  1.1  oki 	zc->zc_csr = ZSWR0_CLR_INTR;
    886  1.1  oki 	ZS_DELAY();
    887  1.1  oki 	return (0);
    888  1.1  oki }
    889  1.1  oki 
    890  1.1  oki static int
    891  1.1  oki zssint(cs, zc)
    892  1.1  oki 	register struct zs_chanstate *cs;
    893  1.1  oki 	register volatile struct zschan *zc;
    894  1.1  oki {
    895  1.1  oki 	register int rr0;
    896  1.1  oki 
    897  1.1  oki 	rr0 = zc->zc_csr;
    898  1.1  oki 	ZS_DELAY();
    899  1.1  oki 	zc->zc_csr = ZSWR0_RESET_STATUS;
    900  1.1  oki 	ZS_DELAY();
    901  1.1  oki 	zc->zc_csr = ZSWR0_CLR_INTR;
    902  1.1  oki 	ZS_DELAY();
    903  1.1  oki 	/*
    904  1.1  oki 	 * The chip's hardware flow control is, as noted in zsreg.h,
    905  1.1  oki 	 * busted---if the DCD line goes low the chip shuts off the
    906  1.1  oki 	 * receiver (!).  If we want hardware CTS flow control but do
    907  1.1  oki 	 * not have it, and carrier is now on, turn HFC on; if we have
    908  1.1  oki 	 * HFC now but carrier has gone low, turn it off.
    909  1.1  oki 	 */
    910  1.1  oki 	if (rr0 & ZSRR0_DCD) {
    911  1.1  oki 		if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    912  1.1  oki 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    913  1.1  oki 			cs->cs_creg[3] |= ZSWR3_HFC;
    914  1.1  oki 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    915  1.1  oki 		}
    916  1.1  oki 	} else {
    917  1.1  oki 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    918  1.1  oki 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    919  1.1  oki 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    920  1.1  oki 		}
    921  1.1  oki 	}
    922  1.1  oki 	if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
    923  1.1  oki #ifdef SUN4
    924  1.1  oki 		/*
    925  1.1  oki 		 * XXX This might not be necessary. Test and
    926  1.1  oki 		 * delete if it isn't.
    927  1.1  oki 		 */
    928  1.1  oki 		if (CPU_ISSUN4) {
    929  1.1  oki 			while (zc->zc_csr & ZSRR0_BREAK)
    930  1.1  oki 				ZS_DELAY();
    931  1.1  oki 		}
    932  1.1  oki #endif
    933  1.1  oki 		zsabort();
    934  1.1  oki 		return (0);
    935  1.1  oki 	}
    936  1.1  oki 	return (ZRING_MAKE(ZRING_SINT, rr0));
    937  1.1  oki }
    938  1.1  oki 
    939  1.1  oki static void
    940  1.1  oki zsabort()
    941  1.1  oki {
    942  1.1  oki 
    943  1.1  oki #ifdef DDB
    944  1.1  oki 	Debugger();
    945  1.1  oki #else
    946  1.1  oki 	printf("stopping on keyboard abort\n");
    947  1.1  oki #ifndef x68k
    948  1.1  oki 	callrom();
    949  1.1  oki #endif
    950  1.1  oki #endif
    951  1.1  oki }
    952  1.1  oki 
    953  1.1  oki #ifdef KGDB
    954  1.1  oki /*
    955  1.1  oki  * KGDB framing character received: enter kernel debugger.  This probably
    956  1.1  oki  * should time out after a few seconds to avoid hanging on spurious input.
    957  1.1  oki  */
    958  1.1  oki void
    959  1.1  oki zskgdb(unit)
    960  1.1  oki 	int unit;
    961  1.1  oki {
    962  1.1  oki 
    963  1.1  oki 	printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
    964  1.1  oki 	kgdb_connect(1);
    965  1.1  oki }
    966  1.1  oki #endif
    967  1.1  oki 
    968  1.1  oki /*
    969  1.1  oki  * Print out a ring or fifo overrun error message.
    970  1.1  oki  */
    971  1.1  oki static void
    972  1.1  oki zsoverrun(unit, ptime, what)
    973  1.1  oki 	int unit;
    974  1.1  oki 	long *ptime;
    975  1.1  oki 	char *what;
    976  1.1  oki {
    977  1.1  oki 
    978  1.1  oki 	if (*ptime != time.tv_sec) {
    979  1.1  oki 		*ptime = time.tv_sec;
    980  1.1  oki 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    981  1.1  oki 		    (unit & 1) + 'a', what);
    982  1.1  oki 	}
    983  1.1  oki }
    984  1.1  oki 
    985  1.1  oki /*
    986  1.1  oki  * ZS software interrupt.  Scan all channels for deferred interrupts.
    987  1.1  oki  */
    988  1.1  oki int
    989  1.1  oki zssoft(arg)
    990  1.1  oki 	void *arg;
    991  1.1  oki {
    992  1.1  oki 	register struct zs_chanstate *cs;
    993  1.1  oki 	register volatile struct zschan *zc;
    994  1.1  oki 	register struct linesw *line;
    995  1.1  oki 	register struct tty *tp;
    996  1.1  oki 	register int get, n, c, cc, unit, s;
    997  1.1  oki 	int	retval = 0;
    998  1.1  oki 
    999  1.1  oki 	for (cs = zslist; cs != NULL; cs = cs->cs_next) {
   1000  1.1  oki 		get = cs->cs_rbget;
   1001  1.1  oki again:
   1002  1.1  oki 		n = cs->cs_rbput;	/* atomic */
   1003  1.1  oki 		if (get == n)		/* nothing more on this line */
   1004  1.1  oki 			continue;
   1005  1.1  oki 		retval = 1;
   1006  1.1  oki 		unit = cs->cs_unit;	/* set up to handle interrupts */
   1007  1.1  oki 		zc = cs->cs_zc;
   1008  1.1  oki 		tp = cs->cs_ttyp;
   1009  1.1  oki 		line = &linesw[tp->t_line];
   1010  1.1  oki 		/*
   1011  1.1  oki 		 * Compute the number of interrupts in the receive ring.
   1012  1.1  oki 		 * If the count is overlarge, we lost some events, and
   1013  1.1  oki 		 * must advance to the first valid one.  It may get
   1014  1.1  oki 		 * overwritten if more data are arriving, but this is
   1015  1.1  oki 		 * too expensive to check and gains nothing (we already
   1016  1.1  oki 		 * lost out; all we can do at this point is trade one
   1017  1.1  oki 		 * kind of loss for another).
   1018  1.1  oki 		 */
   1019  1.1  oki 		n -= get;
   1020  1.1  oki 		if (n > ZLRB_RING_SIZE) {
   1021  1.1  oki 			zsoverrun(unit, &cs->cs_rotime, "ring");
   1022  1.1  oki 			get += n - ZLRB_RING_SIZE;
   1023  1.1  oki 			n = ZLRB_RING_SIZE;
   1024  1.1  oki 		}
   1025  1.1  oki 		while (--n >= 0) {
   1026  1.1  oki 			/* race to keep ahead of incoming interrupts */
   1027  1.1  oki 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
   1028  1.1  oki 			switch (ZRING_TYPE(c)) {
   1029  1.1  oki 
   1030  1.1  oki 			case ZRING_RINT:
   1031  1.1  oki 				c = ZRING_VALUE(c);
   1032  1.1  oki 				if (c & ZSRR1_DO)
   1033  1.1  oki 					zsoverrun(unit, &cs->cs_fotime, "fifo");
   1034  1.1  oki 				cc = c >> 8;
   1035  1.1  oki 				if (c & ZSRR1_FE)
   1036  1.1  oki 					cc |= TTY_FE;
   1037  1.1  oki 				if (c & ZSRR1_PE)
   1038  1.1  oki 					cc |= TTY_PE;
   1039  1.1  oki 				/*
   1040  1.1  oki 				 * this should be done through
   1041  1.1  oki 				 * bstreams	XXX gag choke
   1042  1.1  oki 				 */
   1043  1.1  oki 				else if (unit == ZS_MOUSE)
   1044  1.1  oki 					ms_rint(cc);
   1045  1.1  oki 				else
   1046  1.1  oki 					line->l_rint(cc, tp);
   1047  1.1  oki 				break;
   1048  1.1  oki 
   1049  1.1  oki 			case ZRING_XINT:
   1050  1.1  oki 				/*
   1051  1.1  oki 				 * Transmit done: change registers and resume,
   1052  1.1  oki 				 * or clear BUSY.
   1053  1.1  oki 				 */
   1054  1.1  oki 				if (cs->cs_heldchange) {
   1055  1.1  oki 					s = splzs();
   1056  1.1  oki 					c = zc->zc_csr;
   1057  1.1  oki 					ZS_DELAY();
   1058  1.1  oki 					if ((c & ZSRR0_DCD) == 0)
   1059  1.1  oki 						cs->cs_preg[3] &= ~ZSWR3_HFC;
   1060  1.1  oki 					bcopy((caddr_t)cs->cs_preg,
   1061  1.1  oki 					    (caddr_t)cs->cs_creg, 16);
   1062  1.1  oki 					zs_loadchannelregs(zc, cs->cs_creg);
   1063  1.1  oki 					splx(s);
   1064  1.1  oki 					cs->cs_heldchange = 0;
   1065  1.1  oki 					if (cs->cs_heldtbc &&
   1066  1.1  oki 					    (tp->t_state & TS_TTSTOP) == 0) {
   1067  1.1  oki 						cs->cs_tbc = cs->cs_heldtbc - 1;
   1068  1.1  oki 						zc->zc_data = *cs->cs_tba++;
   1069  1.1  oki 						ZS_DELAY();
   1070  1.1  oki 						goto again;
   1071  1.1  oki 					}
   1072  1.1  oki 				}
   1073  1.1  oki 				tp->t_state &= ~TS_BUSY;
   1074  1.1  oki 				if (tp->t_state & TS_FLUSH)
   1075  1.1  oki 					tp->t_state &= ~TS_FLUSH;
   1076  1.1  oki 				else
   1077  1.1  oki 					ndflush(&tp->t_outq,
   1078  1.1  oki 					 cs->cs_tba - (caddr_t)tp->t_outq.c_cf);
   1079  1.1  oki 				line->l_start(tp);
   1080  1.1  oki 				break;
   1081  1.1  oki 
   1082  1.1  oki 			case ZRING_SINT:
   1083  1.1  oki 				/*
   1084  1.1  oki 				 * Status line change.  HFC bit is run in
   1085  1.1  oki 				 * hardware interrupt, to avoid locking
   1086  1.1  oki 				 * at splzs here.
   1087  1.1  oki 				 */
   1088  1.1  oki 				c = ZRING_VALUE(c);
   1089  1.1  oki 				if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
   1090  1.1  oki 					cc = (c & ZSRR0_DCD) != 0;
   1091  1.1  oki 					if (line->l_modem(tp, cc) == 0)
   1092  1.1  oki 						zs_modem(cs, cc);
   1093  1.1  oki 				}
   1094  1.1  oki 				cs->cs_rr0 = c;
   1095  1.1  oki 				break;
   1096  1.1  oki 
   1097  1.1  oki 			default:
   1098  1.1  oki 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
   1099  1.1  oki 				    unit >> 1, (unit & 1) + 'a', c);
   1100  1.1  oki 				break;
   1101  1.1  oki 			}
   1102  1.1  oki 		}
   1103  1.1  oki 		cs->cs_rbget = get;
   1104  1.1  oki 		goto again;
   1105  1.1  oki 	}
   1106  1.1  oki 	return (retval);
   1107  1.1  oki }
   1108  1.1  oki 
   1109  1.1  oki int
   1110  1.1  oki zsioctl(dev, cmd, data, flag, p)
   1111  1.1  oki 	dev_t dev;
   1112  1.1  oki 	u_long cmd;
   1113  1.1  oki 	caddr_t data;
   1114  1.1  oki 	int flag;
   1115  1.1  oki 	struct proc *p;
   1116  1.1  oki {
   1117  1.1  oki 	int unit = minor(dev);
   1118  1.1  oki 	struct zs_softc *zi = zs_cd.cd_devs[unit >> 1];
   1119  1.1  oki 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1120  1.1  oki 	register struct tty *tp = cs->cs_ttyp;
   1121  1.1  oki 	register int error, s;
   1122  1.1  oki 
   1123  1.1  oki 	error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
   1124  1.1  oki 	if (error >= 0)
   1125  1.1  oki 		return (error);
   1126  1.1  oki 	error = ttioctl(tp, cmd, data, flag, p);
   1127  1.1  oki 	if (error >= 0)
   1128  1.1  oki 		return (error);
   1129  1.1  oki 
   1130  1.1  oki 	switch (cmd) {
   1131  1.1  oki 	case TIOCSBRK:
   1132  1.1  oki 		s = splzs();
   1133  1.1  oki 		cs->cs_preg[5] |= ZSWR5_BREAK;
   1134  1.1  oki 		cs->cs_creg[5] |= ZSWR5_BREAK;
   1135  1.1  oki 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1136  1.1  oki 		splx(s);
   1137  1.1  oki 		break;
   1138  1.1  oki 	case TIOCCBRK:
   1139  1.1  oki 		s = splzs();
   1140  1.1  oki 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1141  1.1  oki 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1142  1.1  oki 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1143  1.1  oki 		splx(s);
   1144  1.1  oki 		break;
   1145  1.1  oki 	case TIOCGFLAGS: {
   1146  1.1  oki 		int bits = 0;
   1147  1.1  oki 
   1148  1.1  oki 		if (cs->cs_softcar)
   1149  1.1  oki 			bits |= TIOCFLAG_SOFTCAR;
   1150  1.1  oki 		if (cs->cs_creg[15] & ZSWR15_DCD_IE)
   1151  1.1  oki 			bits |= TIOCFLAG_CLOCAL;
   1152  1.1  oki 		if (cs->cs_creg[3] & ZSWR3_HFC)
   1153  1.1  oki 			bits |= TIOCFLAG_CRTSCTS;
   1154  1.1  oki 		*(int *)data = bits;
   1155  1.1  oki 		break;
   1156  1.1  oki 	}
   1157  1.1  oki 	case TIOCSFLAGS: {
   1158  1.1  oki 		int userbits, driverbits = 0;
   1159  1.1  oki 
   1160  1.1  oki 		error = suser(p->p_ucred, &p->p_acflag);
   1161  1.1  oki 		if (error != 0)
   1162  1.1  oki 			return (EPERM);
   1163  1.1  oki 
   1164  1.1  oki 		userbits = *(int *)data;
   1165  1.1  oki 
   1166  1.1  oki 		/*
   1167  1.1  oki 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
   1168  1.1  oki 		 # defaulting to software flow control.
   1169  1.1  oki 		 */
   1170  1.1  oki 		if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
   1171  1.1  oki 			return(EINVAL);
   1172  1.1  oki 		if (userbits & TIOCFLAG_MDMBUF)	/* don't support this (yet?) */
   1173  1.1  oki 			return(ENXIO);
   1174  1.1  oki 
   1175  1.1  oki 		s = splzs();
   1176  1.1  oki 		if ((userbits & TIOCFLAG_SOFTCAR) || cs->cs_consio) {
   1177  1.1  oki 			cs->cs_softcar = 1;	/* turn on softcar */
   1178  1.1  oki 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
   1179  1.1  oki 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
   1180  1.1  oki 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1181  1.1  oki 		} else if (userbits & TIOCFLAG_CLOCAL) {
   1182  1.1  oki 			cs->cs_softcar = 0; 	/* turn off softcar */
   1183  1.1  oki 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
   1184  1.1  oki 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
   1185  1.1  oki 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1186  1.1  oki 			tp->t_termios.c_cflag |= CLOCAL;
   1187  1.1  oki 		}
   1188  1.1  oki 		if (userbits & TIOCFLAG_CRTSCTS) {
   1189  1.1  oki 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
   1190  1.1  oki 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
   1191  1.1  oki 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1192  1.1  oki 			cs->cs_preg[3] |= ZSWR3_HFC;
   1193  1.1  oki 			cs->cs_creg[3] |= ZSWR3_HFC;
   1194  1.1  oki 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1195  1.1  oki 			tp->t_termios.c_cflag |= CRTSCTS;
   1196  1.1  oki 		} else {
   1197  1.1  oki 			/* no mdmbuf, so we must want software flow control */
   1198  1.1  oki 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
   1199  1.1  oki 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
   1200  1.1  oki 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
   1201  1.1  oki 			cs->cs_preg[3] &= ~ZSWR3_HFC;
   1202  1.1  oki 			cs->cs_creg[3] &= ~ZSWR3_HFC;
   1203  1.1  oki 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
   1204  1.1  oki 			tp->t_termios.c_cflag &= ~CRTSCTS;
   1205  1.1  oki 		}
   1206  1.1  oki 		splx(s);
   1207  1.1  oki 		break;
   1208  1.1  oki 	}
   1209  1.1  oki 	case TIOCSDTR:
   1210  1.1  oki 		zs_modem(cs, 1);
   1211  1.1  oki 		break;
   1212  1.1  oki 	case TIOCCDTR:
   1213  1.1  oki 		zs_modem(cs, 0);
   1214  1.1  oki 		break;
   1215  1.1  oki 	case TIOCMSET:
   1216  1.1  oki 	case TIOCMBIS:
   1217  1.1  oki 	case TIOCMBIC:
   1218  1.1  oki 	case TIOCMGET:
   1219  1.1  oki 	default:
   1220  1.1  oki 		return (ENOTTY);
   1221  1.1  oki 	}
   1222  1.1  oki 	return (0);
   1223  1.1  oki }
   1224  1.1  oki 
   1225  1.1  oki /*
   1226  1.1  oki  * Start or restart transmission.
   1227  1.1  oki  */
   1228  1.1  oki static void
   1229  1.1  oki zsstart(tp)
   1230  1.1  oki 	register struct tty *tp;
   1231  1.1  oki {
   1232  1.1  oki 	register struct zs_chanstate *cs;
   1233  1.1  oki 	register int s, nch;
   1234  1.1  oki 	int unit = minor(tp->t_dev);
   1235  1.1  oki 	struct zs_softc *zi = zs_cd.cd_devs[unit >> 1];
   1236  1.1  oki 
   1237  1.1  oki 	cs = &zi->zi_cs[unit & 1];
   1238  1.1  oki 	s = spltty();
   1239  1.1  oki 
   1240  1.1  oki 	/*
   1241  1.1  oki 	 * If currently active or delaying, no need to do anything.
   1242  1.1  oki 	 */
   1243  1.1  oki 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
   1244  1.1  oki 		goto out;
   1245  1.1  oki 
   1246  1.1  oki 	/*
   1247  1.1  oki 	 * If there are sleepers, and output has drained below low
   1248  1.1  oki 	 * water mark, awaken.
   1249  1.1  oki 	 */
   1250  1.1  oki 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1251  1.1  oki 		if (tp->t_state & TS_ASLEEP) {
   1252  1.1  oki 			tp->t_state &= ~TS_ASLEEP;
   1253  1.1  oki 			wakeup((caddr_t)&tp->t_outq);
   1254  1.1  oki 		}
   1255  1.1  oki 		selwakeup(&tp->t_wsel);
   1256  1.1  oki 	}
   1257  1.1  oki 
   1258  1.1  oki 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1259  1.1  oki 	if (nch) {
   1260  1.1  oki 		register char *p = tp->t_outq.c_cf;
   1261  1.1  oki 
   1262  1.1  oki 		/* mark busy, enable tx done interrupts, & send first byte */
   1263  1.1  oki 		tp->t_state |= TS_BUSY;
   1264  1.1  oki 		(void) splzs();
   1265  1.1  oki 		cs->cs_preg[1] |= ZSWR1_TIE;
   1266  1.1  oki 		cs->cs_creg[1] |= ZSWR1_TIE;
   1267  1.1  oki 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1268  1.1  oki 		cs->cs_zc->zc_data = *p;
   1269  1.1  oki 		ZS_DELAY();
   1270  1.1  oki 		cs->cs_tba = p + 1;
   1271  1.1  oki 		cs->cs_tbc = nch - 1;
   1272  1.1  oki 	} else {
   1273  1.1  oki 		/*
   1274  1.1  oki 		 * Nothing to send, turn off transmit done interrupts.
   1275  1.1  oki 		 * This is useful if something is doing polled output.
   1276  1.1  oki 		 */
   1277  1.1  oki 		(void) splzs();
   1278  1.1  oki 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1279  1.1  oki 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1280  1.1  oki 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1281  1.1  oki 	}
   1282  1.1  oki out:
   1283  1.1  oki 	splx(s);
   1284  1.1  oki }
   1285  1.1  oki 
   1286  1.1  oki /*
   1287  1.1  oki  * Stop output, e.g., for ^S or output flush.
   1288  1.1  oki  */
   1289  1.1  oki int
   1290  1.1  oki zsstop(tp, flag)
   1291  1.1  oki 	register struct tty *tp;
   1292  1.1  oki 	int flag;
   1293  1.1  oki {
   1294  1.1  oki 	register struct zs_chanstate *cs;
   1295  1.1  oki 	register int s, unit = minor(tp->t_dev);
   1296  1.1  oki 	struct zs_softc *zi = zs_cd.cd_devs[unit >> 1];
   1297  1.1  oki 
   1298  1.1  oki 	cs = &zi->zi_cs[unit & 1];
   1299  1.1  oki 	s = splzs();
   1300  1.1  oki 	if (tp->t_state & TS_BUSY) {
   1301  1.1  oki 		/*
   1302  1.1  oki 		 * Device is transmitting; must stop it.
   1303  1.1  oki 		 */
   1304  1.1  oki 		cs->cs_tbc = 0;
   1305  1.1  oki 		if ((tp->t_state & TS_TTSTOP) == 0)
   1306  1.1  oki 			tp->t_state |= TS_FLUSH;
   1307  1.1  oki 	}
   1308  1.1  oki 	splx(s);
   1309  1.1  oki }
   1310  1.1  oki 
   1311  1.1  oki /*
   1312  1.1  oki  * Set ZS tty parameters from termios.
   1313  1.1  oki  *
   1314  1.1  oki  * This routine makes use of the fact that only registers
   1315  1.1  oki  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1316  1.1  oki  */
   1317  1.1  oki static int
   1318  1.1  oki zsparam(tp, t)
   1319  1.1  oki 	register struct tty *tp;
   1320  1.1  oki 	register struct termios *t;
   1321  1.1  oki {
   1322  1.1  oki 	int unit = minor(tp->t_dev);
   1323  1.1  oki 	struct zs_softc *zi = zs_cd.cd_devs[unit >> 1];
   1324  1.1  oki 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1325  1.1  oki 	register int tmp, tmp5, cflag, s;
   1326  1.1  oki 
   1327  1.1  oki 	/*
   1328  1.1  oki 	 * Because PCLK is only run at 5 MHz, the fastest we
   1329  1.1  oki 	 * can go is 51200 baud (this corresponds to TC=1).
   1330  1.1  oki 	 * This is somewhat unfortunate as there is no real
   1331  1.1  oki 	 * reason we should not be able to handle higher rates.
   1332  1.1  oki 	 */
   1333  1.1  oki 	tmp = t->c_ospeed;
   1334  1.1  oki 	if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
   1335  1.1  oki 		return (EINVAL);
   1336  1.1  oki 	if (tmp == 0) {
   1337  1.1  oki 		/* stty 0 => drop DTR and RTS */
   1338  1.1  oki 		zs_modem(cs, 0);
   1339  1.1  oki 		return (0);
   1340  1.1  oki 	}
   1341  1.1  oki 	tmp = BPS_TO_TCONST(PCLK / 16, tmp);
   1342  1.1  oki 	if (tmp < 2)
   1343  1.1  oki 		return (EINVAL);
   1344  1.1  oki 
   1345  1.1  oki 	cflag = t->c_cflag;
   1346  1.1  oki 	tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
   1347  1.1  oki 	tp->t_cflag = cflag;
   1348  1.1  oki 
   1349  1.1  oki 	/*
   1350  1.1  oki 	 * Block interrupts so that state will not
   1351  1.1  oki 	 * be altered until we are done setting it up.
   1352  1.1  oki 	 */
   1353  1.1  oki 	s = splzs();
   1354  1.1  oki 	cs->cs_preg[12] = tmp;
   1355  1.1  oki 	cs->cs_preg[13] = tmp >> 8;
   1356  1.1  oki 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1357  1.1  oki 	switch (cflag & CSIZE) {
   1358  1.1  oki 	case CS5:
   1359  1.1  oki 		tmp = ZSWR3_RX_5;
   1360  1.1  oki 		tmp5 = ZSWR5_TX_5;
   1361  1.1  oki 		break;
   1362  1.1  oki 	case CS6:
   1363  1.1  oki 		tmp = ZSWR3_RX_6;
   1364  1.1  oki 		tmp5 = ZSWR5_TX_6;
   1365  1.1  oki 		break;
   1366  1.1  oki 	case CS7:
   1367  1.1  oki 		tmp = ZSWR3_RX_7;
   1368  1.1  oki 		tmp5 = ZSWR5_TX_7;
   1369  1.1  oki 		break;
   1370  1.1  oki 	case CS8:
   1371  1.1  oki 	default:
   1372  1.1  oki 		tmp = ZSWR3_RX_8;
   1373  1.1  oki 		tmp5 = ZSWR5_TX_8;
   1374  1.1  oki 		break;
   1375  1.1  oki 	}
   1376  1.1  oki 
   1377  1.1  oki 	/*
   1378  1.1  oki 	 * Output hardware flow control on the chip is horrendous: if
   1379  1.1  oki 	 * carrier detect drops, the receiver is disabled.  Hence we
   1380  1.1  oki 	 * can only do this when the carrier is on.
   1381  1.1  oki 	 */
   1382  1.1  oki 	tmp |= ZSWR3_RX_ENABLE;
   1383  1.1  oki 	if (cflag & CCTS_OFLOW) {
   1384  1.1  oki 		if (cs->cs_zc->zc_csr & ZSRR0_DCD)
   1385  1.1  oki 			tmp |= ZSWR3_HFC;
   1386  1.1  oki 		ZS_DELAY();
   1387  1.1  oki 	}
   1388  1.1  oki 	cs->cs_preg[3] = tmp;
   1389  1.1  oki 	cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1390  1.1  oki 
   1391  1.1  oki 	tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
   1392  1.1  oki 	if ((cflag & PARODD) == 0)
   1393  1.1  oki 		tmp |= ZSWR4_EVENP;
   1394  1.1  oki 	if (cflag & PARENB)
   1395  1.1  oki 		tmp |= ZSWR4_PARENB;
   1396  1.1  oki 	cs->cs_preg[4] = tmp;
   1397  1.1  oki 	cs->cs_preg[9] = ZSWR9_MASTER_IE /*| ZSWR9_NO_VECTOR*/;
   1398  1.1  oki 	cs->cs_preg[10] = ZSWR10_NRZ;
   1399  1.1  oki 	cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
   1400  1.1  oki 	cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
   1401  1.1  oki 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1402  1.1  oki 
   1403  1.1  oki 	/*
   1404  1.1  oki 	 * If nothing is being transmitted, set up new current values,
   1405  1.1  oki 	 * else mark them as pending.
   1406  1.1  oki 	 */
   1407  1.1  oki 	if (cs->cs_heldchange == 0) {
   1408  1.1  oki 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1409  1.1  oki 			cs->cs_heldtbc = cs->cs_tbc;
   1410  1.1  oki 			cs->cs_tbc = 0;
   1411  1.1  oki 			cs->cs_heldchange = 1;
   1412  1.1  oki 		} else {
   1413  1.1  oki 			bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
   1414  1.1  oki 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1415  1.1  oki 		}
   1416  1.1  oki 	}
   1417  1.1  oki 	splx(s);
   1418  1.1  oki 	return (0);
   1419  1.1  oki }
   1420  1.1  oki 
   1421  1.1  oki /*
   1422  1.1  oki  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1423  1.1  oki  * in transmission, the change is deferred.
   1424  1.1  oki  */
   1425  1.1  oki static void
   1426  1.1  oki zs_modem(cs, onoff)
   1427  1.1  oki 	struct zs_chanstate *cs;
   1428  1.1  oki 	int onoff;
   1429  1.1  oki {
   1430  1.1  oki 	int s, bis, and;
   1431  1.1  oki 
   1432  1.1  oki 	if (onoff) {
   1433  1.1  oki 		bis = ZSWR5_DTR | ZSWR5_RTS;
   1434  1.1  oki 		and = ~0;
   1435  1.1  oki 	} else {
   1436  1.1  oki 		bis = 0;
   1437  1.1  oki 		and = ~(ZSWR5_DTR | ZSWR5_RTS);
   1438  1.1  oki 	}
   1439  1.1  oki 	s = splzs();
   1440  1.1  oki 	cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
   1441  1.1  oki 	if (cs->cs_heldchange == 0) {
   1442  1.1  oki 		if (cs->cs_ttyp->t_state & TS_BUSY) {
   1443  1.1  oki 			cs->cs_heldtbc = cs->cs_tbc;
   1444  1.1  oki 			cs->cs_tbc = 0;
   1445  1.1  oki 			cs->cs_heldchange = 1;
   1446  1.1  oki 		} else {
   1447  1.1  oki 			cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
   1448  1.1  oki 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1449  1.1  oki 		}
   1450  1.1  oki 	}
   1451  1.1  oki 	splx(s);
   1452  1.1  oki }
   1453  1.1  oki 
   1454  1.1  oki /*
   1455  1.1  oki  * Hardware flow (RTS) control.
   1456  1.1  oki  */
   1457  1.1  oki static int
   1458  1.1  oki zshwiflow(tp, flag)
   1459  1.1  oki 	struct tty *tp;
   1460  1.1  oki 	int flag;
   1461  1.1  oki {
   1462  1.1  oki 	int unit = minor(tp->t_dev);
   1463  1.1  oki 	struct zs_softc *zi = zs_cd.cd_devs[unit >> 1];
   1464  1.1  oki 	register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
   1465  1.1  oki 	int s;
   1466  1.1  oki 
   1467  1.1  oki #if 0
   1468  1.1  oki        printf ("zshwiflow %d\n", flag);
   1469  1.1  oki #endif
   1470  1.1  oki 	s = splzs();
   1471  1.1  oki 	if (flag) {
   1472  1.1  oki 		cs->cs_preg[5] &= ~ZSWR5_RTS;
   1473  1.1  oki 		cs->cs_creg[5] &= ~ZSWR5_RTS;
   1474  1.1  oki 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1475  1.1  oki 	} else {
   1476  1.1  oki 		cs->cs_preg[5] |= ZSWR5_RTS;
   1477  1.1  oki 		cs->cs_creg[5] |= ZSWR5_RTS;
   1478  1.1  oki 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1479  1.1  oki 	}
   1480  1.1  oki 	splx(s);
   1481  1.1  oki 	return 1;
   1482  1.1  oki }
   1483  1.1  oki 
   1484  1.1  oki /*
   1485  1.1  oki  * Write the given register set to the given zs channel in the proper order.
   1486  1.1  oki  * The channel must not be transmitting at the time.  The receiver will
   1487  1.1  oki  * be disabled for the time it takes to write all the registers.
   1488  1.1  oki  */
   1489  1.1  oki static void
   1490  1.1  oki zs_loadchannelregs(zc, reg)
   1491  1.1  oki 	volatile struct zschan *zc;
   1492  1.1  oki 	u_char *reg;
   1493  1.1  oki {
   1494  1.1  oki 	int i;
   1495  1.1  oki 
   1496  1.1  oki 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1497  1.1  oki 	ZS_DELAY();
   1498  1.1  oki 	i = zc->zc_data;		/* drain fifo */
   1499  1.1  oki 	ZS_DELAY();
   1500  1.1  oki 	i = zc->zc_data;
   1501  1.1  oki 	ZS_DELAY();
   1502  1.1  oki 	i = zc->zc_data;
   1503  1.1  oki 	ZS_DELAY();
   1504  1.1  oki 	ZS_WRITE(zc, 4, reg[4]);
   1505  1.1  oki 	ZS_WRITE(zc, 10, reg[10]);
   1506  1.1  oki 	ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
   1507  1.1  oki 	ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
   1508  1.1  oki 	ZS_WRITE(zc, 1, reg[1]);
   1509  1.1  oki 	ZS_WRITE(zc, 9, reg[9]);
   1510  1.1  oki 	ZS_WRITE(zc, 11, reg[11]);
   1511  1.1  oki 	ZS_WRITE(zc, 12, reg[12]);
   1512  1.1  oki 	ZS_WRITE(zc, 13, reg[13]);
   1513  1.1  oki 	ZS_WRITE(zc, 14, reg[14]);
   1514  1.1  oki 	ZS_WRITE(zc, 15, reg[15]);
   1515  1.1  oki 	ZS_WRITE(zc, 3, reg[3]);
   1516  1.1  oki 	ZS_WRITE(zc, 5, reg[5]);
   1517  1.1  oki }
   1518  1.1  oki 
   1519  1.1  oki #ifdef x68k
   1520  1.1  oki void
   1521  1.1  oki zs_msmodem(onoff)
   1522  1.1  oki 	int onoff;
   1523  1.1  oki {
   1524  1.1  oki 	if (zsms != NULL) {
   1525  1.1  oki 		zs_modem(zsms, onoff);
   1526  1.1  oki 		while(!(mfp.tsr & MFP_TSR_BE))
   1527  1.1  oki 			/* XXX wait */ ;
   1528  1.1  oki 		mfp.udr = 0x40 | (onoff ? 0 : 1);
   1529  1.1  oki 	}
   1530  1.1  oki }
   1531  1.1  oki #endif
   1532  1.1  oki 
   1533  1.1  oki #ifdef KGDB
   1534  1.1  oki /*
   1535  1.1  oki  * Get a character from the given kgdb channel.  Called at splhigh().
   1536  1.1  oki  */
   1537  1.1  oki static int
   1538  1.1  oki zs_kgdb_getc(arg)
   1539  1.1  oki 	void *arg;
   1540  1.1  oki {
   1541  1.1  oki 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1542  1.1  oki 
   1543  1.1  oki 	while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
   1544  1.1  oki 		ZS_DELAY();
   1545  1.1  oki 	return (zc->zc_data);
   1546  1.1  oki }
   1547  1.1  oki 
   1548  1.1  oki /*
   1549  1.1  oki  * Put a character to the given kgdb channel.  Called at splhigh().
   1550  1.1  oki  */
   1551  1.1  oki static void
   1552  1.1  oki zs_kgdb_putc(arg, c)
   1553  1.1  oki 	void *arg;
   1554  1.1  oki 	int c;
   1555  1.1  oki {
   1556  1.1  oki 	register volatile struct zschan *zc = (volatile struct zschan *)arg;
   1557  1.1  oki 
   1558  1.1  oki 	while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
   1559  1.1  oki 		ZS_DELAY();
   1560  1.1  oki 	zc->zc_data = c;
   1561  1.1  oki 	ZS_DELAY();
   1562  1.1  oki }
   1563  1.1  oki 
   1564  1.1  oki /*
   1565  1.1  oki  * Set up for kgdb; called at boot time before configuration.
   1566  1.1  oki  * KGDB interrupts will be enabled later when zs0 is configured.
   1567  1.1  oki  */
   1568  1.1  oki void
   1569  1.1  oki zs_kgdb_init()
   1570  1.1  oki {
   1571  1.1  oki 	volatile struct zsdevice *addr;
   1572  1.1  oki 	volatile struct zschan *zc;
   1573  1.1  oki 	int unit, zs;
   1574  1.1  oki 
   1575  1.1  oki 	if (major(kgdb_dev) != ZSMAJOR)
   1576  1.1  oki 		return;
   1577  1.1  oki 	unit = minor(kgdb_dev);
   1578  1.1  oki 	zs = unit >> 1;
   1579  1.1  oki 	if ((addr = zsaddr[zs]) == NULL)
   1580  1.1  oki 		addr = zsaddr[zs] = findzs(zs);
   1581  1.1  oki 	unit &= 1;
   1582  1.1  oki 	zc = unit == 0 ? &addr->zs_chan[ZS_CHAN_A] : &addr->zs_chan[ZS_CHAN_B];
   1583  1.1  oki 	zs_kgdb_savedspeed = zs_getspeed(zc);
   1584  1.1  oki 	printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
   1585  1.1  oki 	    zs, unit + 'a', kgdb_rate);
   1586  1.1  oki 	zs_reset(zc, 1, kgdb_rate);
   1587  1.1  oki 	kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
   1588  1.1  oki }
   1589  1.1  oki #endif /* KGDB */
   1590  1.1  oki #endif
   1591