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