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