Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.43
      1  1.43       gwr /*	$NetBSD: zs.c,v 1.43 1996/12/17 20:52:12 gwr Exp $	*/
      2  1.10       cgd 
      3  1.42       gwr /*-
      4  1.42       gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.31       gwr  * All rights reserved.
      6   1.1     glass  *
      7  1.42       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8  1.42       gwr  * by Gordon W. Ross.
      9  1.42       gwr  *
     10   1.1     glass  * Redistribution and use in source and binary forms, with or without
     11   1.1     glass  * modification, are permitted provided that the following conditions
     12   1.1     glass  * are met:
     13   1.1     glass  * 1. Redistributions of source code must retain the above copyright
     14   1.1     glass  *    notice, this list of conditions and the following disclaimer.
     15   1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     glass  *    documentation and/or other materials provided with the distribution.
     18  1.42       gwr  * 3. All advertising materials mentioning features or use of this software
     19   1.1     glass  *    must display the following acknowledgement:
     20  1.42       gwr  *        This product includes software developed by the NetBSD
     21  1.42       gwr  *        Foundation, Inc. and its contributors.
     22  1.42       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.42       gwr  *    contributors may be used to endorse or promote products derived
     24  1.42       gwr  *    from this software without specific prior written permission.
     25   1.1     glass  *
     26  1.42       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.42       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.42       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.42       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  1.42       gwr  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.42       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.42       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.42       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.42       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.42       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.42       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     glass  */
     38   1.1     glass 
     39   1.1     glass /*
     40  1.31       gwr  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41   1.1     glass  *
     42  1.31       gwr  * Runs two serial lines per chip using slave drivers.
     43  1.31       gwr  * Plain tty/async lines use the zs_async slave.
     44  1.31       gwr  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     45   1.1     glass  */
     46   1.1     glass 
     47   1.5       gwr #include <sys/param.h>
     48   1.1     glass #include <sys/systm.h>
     49  1.43       gwr #include <sys/conf.h>
     50   1.1     glass #include <sys/device.h>
     51   1.1     glass #include <sys/file.h>
     52   1.1     glass #include <sys/ioctl.h>
     53  1.43       gwr #include <sys/kernel.h>
     54  1.43       gwr #include <sys/proc.h>
     55   1.1     glass #include <sys/tty.h>
     56   1.1     glass #include <sys/time.h>
     57   1.1     glass #include <sys/syslog.h>
     58   1.1     glass 
     59  1.31       gwr #include <dev/cons.h>
     60  1.31       gwr #include <dev/ic/z8530reg.h>
     61  1.31       gwr #include <machine/z8530var.h>
     62  1.31       gwr 
     63   1.1     glass #include <machine/autoconf.h>
     64   1.1     glass #include <machine/cpu.h>
     65   1.1     glass #include <machine/obio.h>
     66   1.3       gwr #include <machine/mon.h>
     67   1.1     glass 
     68  1.43       gwr #include "zs_cons.h"
     69  1.43       gwr 
     70  1.16       gwr /*
     71  1.31       gwr  * XXX: Hard code this to make console init easier...
     72  1.16       gwr  */
     73  1.43       gwr #define	NZSC	2		/* XXX */
     74   1.1     glass 
     75  1.43       gwr /*
     76  1.43       gwr  * Some warts needed by z8530tty.c -
     77  1.43       gwr  * The default parity REALLY needs to be the same as the PROM uses,
     78  1.43       gwr  * or you can not see messages done with printf during boot-up...
     79  1.43       gwr  */
     80  1.43       gwr int zs_def_cflag = (CREAD | CS8 | HUPCL);
     81  1.43       gwr int zs_major = 12;
     82   1.1     glass 
     83  1.43       gwr /*
     84  1.43       gwr  * The Sun3 provides a 4.9152 MHz clock to the ZS chips.
     85  1.43       gwr  */
     86   1.2     glass #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     87   1.2     glass 
     88   1.2     glass /*
     89  1.22       gwr  * Define interrupt levels.
     90   1.2     glass  */
     91   1.2     glass #define ZSHARD_PRI	6	/* Wired on the CPU board... */
     92  1.22       gwr #define ZSSOFT_PRI	3	/* Want tty pri (4) but this is OK. */
     93   1.1     glass 
     94  1.33       gwr #define ZS_DELAY()			delay(2)
     95  1.31       gwr 
     96  1.31       gwr /* The layout of this is hardware-dependent (padding, order). */
     97  1.31       gwr struct zschan {
     98  1.31       gwr 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     99  1.31       gwr 	u_char		zc_xxx0;
    100  1.31       gwr 	volatile u_char	zc_data;	/* data */
    101  1.31       gwr 	u_char		zc_xxx1;
    102  1.31       gwr };
    103  1.31       gwr struct zsdevice {
    104  1.31       gwr 	/* Yes, they are backwards. */
    105  1.31       gwr 	struct	zschan zs_chan_b;
    106  1.31       gwr 	struct	zschan zs_chan_a;
    107   1.1     glass };
    108   1.1     glass 
    109   1.1     glass 
    110  1.31       gwr /* Default OBIO addresses. */
    111  1.43       gwr static int zs_physaddr[NZSC] = { OBIO_KEYBD_MS, OBIO_ZS };
    112  1.43       gwr 
    113  1.31       gwr /* Saved PROM mappings */
    114  1.43       gwr static struct zsdevice *zsaddr[NZSC];	/* See zs_init() */
    115  1.43       gwr 
    116  1.31       gwr /* Flags from cninit() */
    117  1.43       gwr static int zs_hwflags[NZSC][2];
    118  1.43       gwr 
    119  1.31       gwr /* Default speed for each channel */
    120  1.43       gwr static int zs_defspeed[NZSC][2] = {
    121  1.31       gwr 	{ 1200, 	/* keyboard */
    122  1.31       gwr 	  1200 },	/* mouse */
    123  1.31       gwr 	{ 9600, 	/* ttya */
    124  1.31       gwr 	  9600 },	/* ttyb */
    125  1.31       gwr };
    126  1.13       gwr 
    127  1.43       gwr static u_char zs_init_reg[16] = {
    128  1.43       gwr 	0,	/* 0: CMD (reset, etc.) */
    129  1.43       gwr 	0,	/* 1: No interrupts yet. */
    130  1.43       gwr 	0x18 + ZSHARD_PRI,	/* IVECT */
    131  1.43       gwr 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    132  1.43       gwr 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    133  1.43       gwr 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    134  1.43       gwr 	0,	/* 6: TXSYNC/SYNCLO */
    135  1.43       gwr 	0,	/* 7: RXSYNC/SYNCHI */
    136  1.43       gwr 	0,	/* 8: alias for data port */
    137  1.43       gwr 	ZSWR9_MASTER_IE,
    138  1.43       gwr 	0,	/*10: Misc. TX/RX control bits */
    139  1.43       gwr 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    140  1.43       gwr 	14,	/*12: BAUDLO (default=9600) */
    141  1.43       gwr 	0,	/*13: BAUDHI (default=9600) */
    142  1.43       gwr 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    143  1.43       gwr 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    144  1.43       gwr };
    145  1.43       gwr 
    146  1.43       gwr static struct zschan *
    147  1.43       gwr zs_get_chan_addr __P((int zsc_unit, int channel));
    148  1.43       gwr 
    149   1.1     glass 
    150  1.31       gwr /* Find PROM mappings (for console support). */
    151  1.43       gwr void
    152  1.43       gwr zs_init()
    153  1.31       gwr {
    154  1.31       gwr 	int i;
    155   1.1     glass 
    156  1.43       gwr 	for (i = 0; i < NZSC; i++) {
    157  1.31       gwr 		zsaddr[i] = (struct zsdevice *)
    158  1.31       gwr 			obio_find_mapping(zs_physaddr[i], OBIO_ZS_SIZE);
    159  1.31       gwr 	}
    160  1.43       gwr }
    161  1.13       gwr 
    162  1.43       gwr static struct zschan *
    163  1.31       gwr zs_get_chan_addr(zsc_unit, channel)
    164  1.31       gwr 	int zsc_unit, channel;
    165  1.31       gwr {
    166  1.31       gwr 	struct zsdevice *addr;
    167  1.31       gwr 	struct zschan *zc;
    168  1.31       gwr 
    169  1.43       gwr 	if (zsc_unit >= NZSC)
    170  1.31       gwr 		return NULL;
    171  1.31       gwr 	addr = zsaddr[zsc_unit];
    172  1.31       gwr 	if (addr == NULL)
    173  1.31       gwr 		return NULL;
    174  1.31       gwr 	if (channel == 0) {
    175  1.31       gwr 		zc = &addr->zs_chan_a;
    176  1.31       gwr 	} else {
    177  1.31       gwr 		zc = &addr->zs_chan_b;
    178  1.31       gwr 	}
    179  1.31       gwr 	return (zc);
    180  1.31       gwr }
    181  1.13       gwr 
    182  1.18       gwr 
    183  1.31       gwr /****************************************************************
    184  1.31       gwr  * Autoconfig
    185  1.31       gwr  ****************************************************************/
    186  1.31       gwr 
    187  1.31       gwr /* Definition of the driver for autoconfig. */
    188  1.43       gwr static int	zsc_match __P((struct device *, struct cfdata *, void *));
    189  1.35       gwr static void	zsc_attach __P((struct device *, struct device *, void *));
    190  1.39       cgd static int  zsc_print __P((void *, const char *name));
    191  1.31       gwr 
    192  1.34   thorpej struct cfattach zsc_ca = {
    193  1.34   thorpej 	sizeof(struct zsc_softc), zsc_match, zsc_attach
    194  1.34   thorpej };
    195  1.34   thorpej 
    196  1.34   thorpej struct cfdriver zsc_cd = {
    197  1.34   thorpej 	NULL, "zsc", DV_DULL
    198  1.31       gwr };
    199  1.31       gwr 
    200  1.43       gwr static int zshard __P((void *));
    201  1.43       gwr static int zssoft __P((void *));
    202  1.43       gwr static int zs_get_speed __P((struct zs_chanstate *));
    203  1.31       gwr 
    204   1.9       gwr 
    205   1.1     glass /*
    206  1.31       gwr  * Is the zs chip present?
    207   1.1     glass  */
    208   1.1     glass static int
    209  1.43       gwr zsc_match(parent, cf, aux)
    210  1.31       gwr 	struct device *parent;
    211  1.43       gwr 	struct cfdata *cf;
    212  1.43       gwr 	void *aux;
    213   1.1     glass {
    214  1.31       gwr 	struct confargs *ca = aux;
    215  1.43       gwr 	int unit;
    216  1.35       gwr 	void *va;
    217  1.13       gwr 
    218  1.43       gwr 	/* We have arrays sized with NZSC so validate. */
    219  1.13       gwr 	unit = cf->cf_unit;
    220  1.43       gwr 	if (unit < 0 || unit >= NZSC)
    221  1.13       gwr 		return (0);
    222   1.1     glass 
    223  1.35       gwr 	/*
    224  1.43       gwr 	 * This driver only supports its wired-in mappings,
    225  1.43       gwr 	 * because the console support depends on those.
    226  1.35       gwr 	 */
    227  1.43       gwr 	if (ca->ca_paddr != zs_physaddr[unit])
    228  1.35       gwr 		return (0);
    229  1.35       gwr 
    230  1.31       gwr 	/* Make sure zs_init() found mappings. */
    231  1.35       gwr 	va = zsaddr[unit];
    232  1.35       gwr 	if (va == NULL)
    233  1.21       gwr 		return (0);
    234  1.21       gwr 
    235  1.21       gwr 	/* This returns -1 on a fault (bus error). */
    236  1.43       gwr 	if (peek_byte(va) == -1)
    237  1.43       gwr 		return (0);
    238  1.43       gwr 
    239  1.43       gwr 	/* Default interrupt priority (always splbio==2) */
    240  1.43       gwr 	if (ca->ca_intpri == -1)
    241  1.43       gwr 		ca->ca_intpri = ZSHARD_PRI;
    242  1.43       gwr 
    243  1.43       gwr 	return (1);
    244   1.1     glass }
    245   1.1     glass 
    246   1.1     glass /*
    247   1.1     glass  * Attach a found zs.
    248   1.1     glass  *
    249  1.31       gwr  * Match slave number to zs unit number, so that misconfiguration will
    250  1.31       gwr  * not set up the keyboard as ttya, etc.
    251   1.1     glass  */
    252   1.1     glass static void
    253  1.31       gwr zsc_attach(parent, self, aux)
    254  1.31       gwr 	struct device *parent;
    255  1.31       gwr 	struct device *self;
    256  1.31       gwr 	void *aux;
    257  1.31       gwr {
    258  1.31       gwr 	struct zsc_softc *zsc = (void *) self;
    259  1.31       gwr 	struct confargs *ca = aux;
    260  1.31       gwr 	struct zsc_attach_args zsc_args;
    261  1.31       gwr 	volatile struct zschan *zc;
    262  1.31       gwr 	struct zs_chanstate *cs;
    263  1.43       gwr 	int s, zsc_unit, channel;
    264   1.2     glass 	static int didintr;
    265   1.2     glass 
    266  1.31       gwr 	zsc_unit = zsc->zsc_dev.dv_unit;
    267  1.13       gwr 
    268  1.43       gwr 	printf(": (softpri %d)\n", ZSSOFT_PRI);
    269   1.1     glass 
    270  1.31       gwr 	/* Use the mapping setup by the Sun PROM. */
    271  1.31       gwr 	if (zsaddr[zsc_unit] == NULL)
    272  1.31       gwr 		panic("zs_attach: zs%d not mapped\n", zsc_unit);
    273  1.31       gwr 
    274  1.31       gwr 	/*
    275  1.31       gwr 	 * Initialize software state for each channel.
    276  1.31       gwr 	 */
    277  1.31       gwr 	for (channel = 0; channel < 2; channel++) {
    278  1.43       gwr 		zsc_args.channel = channel;
    279  1.43       gwr 		zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
    280  1.43       gwr 		cs = &zsc->zsc_cs_store[channel];
    281  1.43       gwr 		zsc->zsc_cs[channel] = cs;
    282   1.1     glass 
    283  1.31       gwr 		cs->cs_channel = channel;
    284  1.31       gwr 		cs->cs_private = NULL;
    285  1.31       gwr 		cs->cs_ops = &zsops_null;
    286  1.37       gwr 		cs->cs_brg_clk = PCLK / 16;
    287  1.31       gwr 
    288  1.43       gwr 		zc = zs_get_chan_addr(zsc_unit, channel);
    289  1.43       gwr 		cs->cs_reg_csr  = &zc->zc_csr;
    290  1.43       gwr 		cs->cs_reg_data = &zc->zc_data;
    291   1.2     glass 
    292  1.31       gwr 		bcopy(zs_init_reg, cs->cs_creg, 16);
    293  1.31       gwr 		bcopy(zs_init_reg, cs->cs_preg, 16);
    294  1.15       gwr 
    295  1.43       gwr 		/* XXX: Get these from the EEPROM instead? */
    296  1.43       gwr 		/* XXX: See the mvme167 code.  Better. */
    297  1.43       gwr 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    298  1.43       gwr 			cs->cs_defspeed = zs_get_speed(cs);
    299  1.43       gwr 		else
    300  1.43       gwr 			cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
    301  1.43       gwr 		cs->cs_defcflag = zs_def_cflag;
    302  1.43       gwr 
    303   1.1     glass 		/*
    304  1.31       gwr 		 * Clear the master interrupt enable.
    305  1.31       gwr 		 * The INTENA is common to both channels,
    306  1.31       gwr 		 * so just do it on the A channel.
    307   1.1     glass 		 */
    308  1.31       gwr 		if (channel == 0) {
    309  1.32       gwr 			zs_write_reg(cs, 9, 0);
    310  1.31       gwr 		}
    311  1.15       gwr 
    312   1.1     glass 		/*
    313  1.31       gwr 		 * Look for a child driver for this channel.
    314  1.31       gwr 		 * The child attach will setup the hardware.
    315   1.1     glass 		 */
    316  1.43       gwr 		if (!config_found(self, (void *)&zsc_args, zsc_print)) {
    317  1.31       gwr 			/* No sub-driver.  Just reset it. */
    318  1.43       gwr 			u_char reset = (channel == 0) ?
    319  1.31       gwr 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    320  1.31       gwr 			s = splzs();
    321  1.32       gwr 			zs_write_reg(cs,  9, reset);
    322  1.31       gwr 			splx(s);
    323  1.31       gwr 		}
    324   1.1     glass 	}
    325   1.1     glass 
    326  1.43       gwr 	/*
    327  1.43       gwr 	 * Now safe to install interrupt handlers.  Note the arguments
    328  1.43       gwr 	 * to the interrupt handlers aren't used.  Note, we only do this
    329  1.43       gwr 	 * once since both SCCs interrupt at the same level and vector.
    330  1.43       gwr 	 */
    331  1.31       gwr 	if (!didintr) {
    332  1.31       gwr 		didintr = 1;
    333  1.31       gwr 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
    334  1.43       gwr 		isr_add_autovect(zshard, NULL, ca->ca_intpri);
    335  1.31       gwr 	}
    336  1.24       gwr 
    337  1.31       gwr 	/*
    338  1.31       gwr 	 * Set the master interrupt enable and interrupt vector.
    339  1.31       gwr 	 * (common to both channels, do it on A)
    340  1.31       gwr 	 */
    341  1.43       gwr 	cs = zsc->zsc_cs[0];
    342  1.31       gwr 	s = splzs();
    343  1.31       gwr 	/* interrupt vector */
    344  1.32       gwr 	zs_write_reg(cs, 2, zs_init_reg[2]);
    345  1.31       gwr 	/* master interrupt control (enable) */
    346  1.32       gwr 	zs_write_reg(cs, 9, zs_init_reg[9]);
    347  1.31       gwr 	splx(s);
    348  1.35       gwr }
    349  1.35       gwr 
    350  1.35       gwr static int
    351  1.35       gwr zsc_print(aux, name)
    352  1.35       gwr 	void *aux;
    353  1.39       cgd 	const char *name;
    354  1.35       gwr {
    355  1.35       gwr 	struct zsc_attach_args *args = aux;
    356  1.35       gwr 
    357  1.35       gwr 	if (name != NULL)
    358  1.41  christos 		printf("%s: ", name);
    359  1.35       gwr 
    360  1.35       gwr 	if (args->channel != -1)
    361  1.41  christos 		printf(" channel %d", args->channel);
    362  1.35       gwr 
    363  1.35       gwr 	return UNCONF;
    364  1.24       gwr }
    365  1.24       gwr 
    366  1.43       gwr static int zssoftpending;
    367  1.43       gwr 
    368  1.43       gwr /*
    369  1.43       gwr  * Our ZS chips all share a common, autovectored interrupt,
    370  1.43       gwr  * so we have to look at all of them on each interrupt.
    371  1.43       gwr  */
    372  1.31       gwr static int
    373  1.31       gwr zshard(arg)
    374  1.31       gwr 	void *arg;
    375   1.1     glass {
    376  1.43       gwr 	register struct zsc_softc *zsc;
    377  1.43       gwr 	register int unit, rval;
    378  1.43       gwr 
    379  1.31       gwr 	/* Do ttya/ttyb first, because they go faster. */
    380  1.31       gwr 	rval = 0;
    381  1.34   thorpej 	unit = zsc_cd.cd_ndevs;
    382  1.31       gwr 	while (--unit >= 0) {
    383  1.34   thorpej 		zsc = zsc_cd.cd_devs[unit];
    384  1.43       gwr 		if (zsc == NULL)
    385  1.43       gwr 			continue;
    386  1.43       gwr 		rval |= zsc_intr_hard(zsc);
    387  1.43       gwr 		if ((zsc->zsc_cs[0]->cs_softreq) ||
    388  1.43       gwr 			(zsc->zsc_cs[1]->cs_softreq))
    389  1.43       gwr 		{
    390  1.43       gwr 			/* zsc_req_softint(zsc); */
    391  1.43       gwr 			/* We are at splzs here, so no need to lock. */
    392  1.43       gwr 			if (zssoftpending == 0) {
    393  1.43       gwr 				zssoftpending = ZSSOFT_PRI;
    394  1.43       gwr 				isr_soft_request(ZSSOFT_PRI);
    395  1.43       gwr 			}
    396  1.31       gwr 		}
    397  1.31       gwr 	}
    398  1.31       gwr 	return (rval);
    399   1.1     glass }
    400   1.1     glass 
    401  1.43       gwr /*
    402  1.43       gwr  * Similar scheme as for zshard (look at all of them)
    403  1.43       gwr  */
    404   1.3       gwr static int
    405  1.31       gwr zssoft(arg)
    406  1.31       gwr 	void *arg;
    407   1.3       gwr {
    408  1.43       gwr 	register struct zsc_softc *zsc;
    409  1.43       gwr 	register int unit;
    410  1.31       gwr 
    411  1.31       gwr 	/* This is not the only ISR on this IPL. */
    412  1.31       gwr 	if (zssoftpending == 0)
    413  1.31       gwr 		return (0);
    414   1.3       gwr 
    415  1.31       gwr 	/*
    416  1.31       gwr 	 * The soft intr. bit will be set by zshard only if
    417  1.31       gwr 	 * the variable zssoftpending is zero.  The order of
    418  1.31       gwr 	 * these next two statements prevents our clearing
    419  1.31       gwr 	 * the soft intr bit just after zshard has set it.
    420  1.31       gwr 	 */
    421  1.31       gwr 	isr_soft_clear(ZSSOFT_PRI);
    422  1.31       gwr 	zssoftpending = 0;
    423   1.2     glass 
    424  1.31       gwr 	/* Do ttya/ttyb first, because they go faster. */
    425  1.34   thorpej 	unit = zsc_cd.cd_ndevs;
    426  1.31       gwr 	while (--unit >= 0) {
    427  1.34   thorpej 		zsc = zsc_cd.cd_devs[unit];
    428  1.43       gwr 		if (zsc == NULL)
    429  1.43       gwr 			continue;
    430  1.43       gwr 		(void) zsc_intr_soft(zsc);
    431   1.3       gwr 	}
    432  1.31       gwr 	return (1);
    433   1.2     glass }
    434   1.2     glass 
    435   1.2     glass 
    436  1.31       gwr /*
    437  1.43       gwr  * Compute the current baud rate given a ZSCC channel.
    438  1.43       gwr  */
    439  1.43       gwr static int
    440  1.43       gwr zs_get_speed(cs)
    441  1.43       gwr 	struct zs_chanstate *cs;
    442  1.43       gwr {
    443  1.43       gwr 	int tconst;
    444  1.43       gwr 
    445  1.43       gwr 	tconst = zs_read_reg(cs, 12);
    446  1.43       gwr 	tconst |= zs_read_reg(cs, 13) << 8;
    447  1.43       gwr 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    448  1.43       gwr }
    449  1.43       gwr 
    450  1.43       gwr /*
    451  1.43       gwr  * MD functions for setting the baud rate and control modes.
    452  1.43       gwr  */
    453  1.43       gwr int
    454  1.43       gwr zs_set_speed(cs, bps)
    455  1.43       gwr 	struct zs_chanstate *cs;
    456  1.43       gwr 	int bps;	/* bits per second */
    457  1.43       gwr {
    458  1.43       gwr 	int tconst, real_bps;
    459  1.43       gwr 
    460  1.43       gwr 	if (bps == 0)
    461  1.43       gwr 		return (0);
    462  1.43       gwr 
    463  1.43       gwr #ifdef	DIAGNOSTIC
    464  1.43       gwr 	if (cs->cs_brg_clk == 0)
    465  1.43       gwr 		panic("zs_set_speed");
    466  1.43       gwr #endif
    467  1.43       gwr 
    468  1.43       gwr 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    469  1.43       gwr 	if (tconst < 0)
    470  1.43       gwr 		return (EINVAL);
    471  1.43       gwr 
    472  1.43       gwr 	/* Convert back to make sure we can do it. */
    473  1.43       gwr 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    474  1.43       gwr 
    475  1.43       gwr 	/* XXX - Allow some tolerance here? */
    476  1.43       gwr 	if (real_bps != bps)
    477  1.43       gwr 		return (EINVAL);
    478  1.43       gwr 
    479  1.43       gwr 	cs->cs_preg[12] = tconst;
    480  1.43       gwr 	cs->cs_preg[13] = tconst >> 8;
    481  1.43       gwr 
    482  1.43       gwr 	/* Caller will stuff the pending registers. */
    483  1.43       gwr 	return (0);
    484  1.43       gwr }
    485  1.43       gwr 
    486  1.43       gwr int
    487  1.43       gwr zs_set_modes(cs, cflag)
    488  1.43       gwr 	struct zs_chanstate *cs;
    489  1.43       gwr 	int cflag;	/* bits per second */
    490  1.43       gwr {
    491  1.43       gwr 	int s;
    492  1.43       gwr 
    493  1.43       gwr 	/*
    494  1.43       gwr 	 * Output hardware flow control on the chip is horrendous:
    495  1.43       gwr 	 * if carrier detect drops, the receiver is disabled, and if
    496  1.43       gwr 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    497  1.43       gwr 	 * Therefore, NEVER set the HFC bit, and instead use the
    498  1.43       gwr 	 * status interrupt to detect CTS changes.
    499  1.43       gwr 	 */
    500  1.43       gwr 	s = splzs();
    501  1.43       gwr 	if (cflag & CLOCAL) {
    502  1.43       gwr 		cs->cs_rr0_dcd = 0;
    503  1.43       gwr 		cs->cs_preg[15] &= ~ZSWR15_DCD_IE;
    504  1.43       gwr 	} else {
    505  1.43       gwr 		cs->cs_rr0_dcd = ZSRR0_DCD;
    506  1.43       gwr 		cs->cs_preg[15] |= ZSWR15_DCD_IE;
    507  1.43       gwr 	}
    508  1.43       gwr 	if (cflag & CRTSCTS) {
    509  1.43       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR;
    510  1.43       gwr 		cs->cs_wr5_rts = ZSWR5_RTS;
    511  1.43       gwr 		cs->cs_rr0_cts = ZSRR0_CTS;
    512  1.43       gwr 		cs->cs_preg[15] |= ZSWR15_CTS_IE;
    513  1.43       gwr 	} else {
    514  1.43       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    515  1.43       gwr 		cs->cs_wr5_rts = 0;
    516  1.43       gwr 		cs->cs_rr0_cts = 0;
    517  1.43       gwr 		cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    518  1.43       gwr 	}
    519  1.43       gwr 	splx(s);
    520  1.43       gwr 
    521  1.43       gwr 	/* Caller will stuff the pending registers. */
    522  1.43       gwr 	return (0);
    523  1.43       gwr }
    524  1.43       gwr 
    525  1.43       gwr 
    526  1.43       gwr /*
    527  1.31       gwr  * Read or write the chip with suitable delays.
    528  1.31       gwr  */
    529   1.2     glass 
    530  1.31       gwr u_char
    531  1.31       gwr zs_read_reg(cs, reg)
    532  1.31       gwr 	struct zs_chanstate *cs;
    533  1.31       gwr 	u_char reg;
    534   1.1     glass {
    535  1.31       gwr 	u_char val;
    536   1.1     glass 
    537  1.31       gwr 	*cs->cs_reg_csr = reg;
    538  1.31       gwr 	ZS_DELAY();
    539  1.31       gwr 	val = *cs->cs_reg_csr;
    540  1.31       gwr 	ZS_DELAY();
    541  1.31       gwr 	return val;
    542  1.17       gwr }
    543   1.3       gwr 
    544  1.31       gwr void
    545  1.31       gwr zs_write_reg(cs, reg, val)
    546  1.31       gwr 	struct zs_chanstate *cs;
    547  1.31       gwr 	u_char reg, val;
    548  1.17       gwr {
    549  1.31       gwr 	*cs->cs_reg_csr = reg;
    550  1.31       gwr 	ZS_DELAY();
    551  1.31       gwr 	*cs->cs_reg_csr = val;
    552  1.32       gwr 	ZS_DELAY();
    553  1.32       gwr }
    554  1.32       gwr 
    555  1.32       gwr u_char zs_read_csr(cs)
    556  1.32       gwr 	struct zs_chanstate *cs;
    557  1.32       gwr {
    558  1.43       gwr 	register u_char val;
    559  1.32       gwr 
    560  1.43       gwr 	val = *cs->cs_reg_csr;
    561  1.32       gwr 	ZS_DELAY();
    562  1.43       gwr 	return val;
    563  1.32       gwr }
    564  1.32       gwr 
    565  1.43       gwr void  zs_write_csr(cs, val)
    566  1.32       gwr 	struct zs_chanstate *cs;
    567  1.43       gwr 	u_char val;
    568  1.32       gwr {
    569  1.43       gwr 	*cs->cs_reg_csr = val;
    570  1.32       gwr 	ZS_DELAY();
    571  1.32       gwr }
    572  1.32       gwr 
    573  1.43       gwr u_char zs_read_data(cs)
    574  1.32       gwr 	struct zs_chanstate *cs;
    575  1.32       gwr {
    576  1.43       gwr 	register u_char val;
    577  1.43       gwr 
    578  1.43       gwr 	val = *cs->cs_reg_data;
    579  1.32       gwr 	ZS_DELAY();
    580  1.43       gwr 	return val;
    581  1.32       gwr }
    582  1.32       gwr 
    583  1.32       gwr void  zs_write_data(cs, val)
    584  1.32       gwr 	struct zs_chanstate *cs;
    585  1.32       gwr 	u_char val;
    586  1.32       gwr {
    587  1.32       gwr 	*cs->cs_reg_data = val;
    588  1.31       gwr 	ZS_DELAY();
    589   1.1     glass }
    590   1.3       gwr 
    591  1.31       gwr /****************************************************************
    592  1.31       gwr  * Console support functions (Sun3 specific!)
    593  1.43       gwr  * Note: this code is allowed to know about the layout of
    594  1.43       gwr  * the chip registers, and uses that to keep things simple.
    595  1.43       gwr  * XXX - I think I like the mvme167 code better. -gwr
    596  1.31       gwr  ****************************************************************/
    597   1.1     glass 
    598  1.43       gwr void *zs_conschan;
    599  1.43       gwr 
    600   1.2     glass /*
    601  1.31       gwr  * Polled input char.
    602   1.2     glass  */
    603   1.2     glass int
    604  1.31       gwr zs_getc(arg)
    605  1.31       gwr 	void *arg;
    606   1.2     glass {
    607  1.31       gwr 	register volatile struct zschan *zc = arg;
    608  1.25       gwr 	register int s, c, rr0;
    609   1.2     glass 
    610   1.2     glass 	s = splhigh();
    611   1.9       gwr 	/* Wait for a character to arrive. */
    612  1.25       gwr 	do {
    613  1.25       gwr 		rr0 = zc->zc_csr;
    614   1.3       gwr 		ZS_DELAY();
    615  1.25       gwr 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    616   1.9       gwr 
    617   1.2     glass 	c = zc->zc_data;
    618   1.9       gwr 	ZS_DELAY();
    619   1.2     glass 	splx(s);
    620  1.17       gwr 
    621  1.17       gwr 	/*
    622  1.17       gwr 	 * This is used by the kd driver to read scan codes,
    623  1.17       gwr 	 * so don't translate '\r' ==> '\n' here...
    624  1.17       gwr 	 */
    625   1.2     glass 	return (c);
    626   1.2     glass }
    627   1.1     glass 
    628   1.1     glass /*
    629  1.31       gwr  * Polled output char.
    630   1.1     glass  */
    631  1.31       gwr void
    632  1.31       gwr zs_putc(arg, c)
    633  1.31       gwr 	void *arg;
    634   1.1     glass 	int c;
    635   1.1     glass {
    636  1.31       gwr 	register volatile struct zschan *zc = arg;
    637  1.25       gwr 	register int s, rr0;
    638   1.1     glass 
    639   1.9       gwr 	s = splhigh();
    640   1.9       gwr 	/* Wait for transmitter to become ready. */
    641  1.25       gwr 	do {
    642  1.25       gwr 		rr0 = zc->zc_csr;
    643   1.3       gwr 		ZS_DELAY();
    644  1.25       gwr 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    645   1.9       gwr 
    646   1.1     glass 	zc->zc_data = c;
    647   1.3       gwr 	ZS_DELAY();
    648   1.1     glass 	splx(s);
    649   1.1     glass }
    650   1.2     glass 
    651  1.31       gwr extern struct consdev consdev_kd;	/* keyboard/display */
    652  1.31       gwr extern struct consdev consdev_tty;
    653  1.31       gwr extern struct consdev *cn_tab;	/* physical console device info */
    654  1.31       gwr 
    655  1.43       gwr static int  zscngetc __P((dev_t));
    656  1.43       gwr static void zscnputc __P((dev_t, int));
    657  1.43       gwr static void zscninit __P((struct consdev *));
    658  1.43       gwr 
    659  1.43       gwr static struct {
    660  1.43       gwr 	int zsc_unit, channel;
    661  1.43       gwr } zstty_conf[NZSC*2] = {
    662  1.43       gwr 	/* XXX: knowledge from the config file here... */
    663  1.43       gwr 	{ 1, 0 },	/* ttya */
    664  1.43       gwr 	{ 1, 1 },	/* ttyb */
    665  1.43       gwr 	{ 0, 0 },	/* ttyc */
    666  1.43       gwr 	{ 0, 1 },	/* ttyd */
    667  1.43       gwr };
    668  1.31       gwr 
    669   1.1     glass /*
    670  1.31       gwr  * This function replaces sys/dev/cninit.c
    671  1.31       gwr  * Determine which device is the console using
    672  1.38       gwr  * the PROM "input source" and "output sink".
    673   1.1     glass  */
    674  1.31       gwr void
    675  1.31       gwr cninit()
    676   1.1     glass {
    677  1.38       gwr 	MachMonRomVector *v;
    678  1.31       gwr 	struct zschan *zc;
    679  1.31       gwr 	struct consdev *cn;
    680  1.43       gwr 	int channel, zsc_unit, zstty_unit;
    681  1.43       gwr 	u_char inSource;
    682  1.31       gwr 
    683  1.38       gwr 	v = romVectorPtr;
    684  1.38       gwr 	inSource = *(v->inSource);
    685  1.31       gwr 
    686  1.38       gwr 	if (inSource != *(v->outSink)) {
    687  1.38       gwr 		mon_printf("cninit: mismatched PROM output selector\n");
    688  1.38       gwr 	}
    689  1.38       gwr 
    690  1.38       gwr 	switch (inSource) {
    691  1.38       gwr 
    692  1.38       gwr 	case 1:	/* ttya */
    693  1.38       gwr 	case 2:	/* ttyb */
    694  1.38       gwr 	case 3:	/* ttyc (rewired keyboard connector) */
    695  1.38       gwr 	case 4:	/* ttyd (rewired mouse connector)   */
    696  1.43       gwr 		zstty_unit = inSource - 1;
    697  1.43       gwr 		zsc_unit = zstty_conf[zstty_unit].zsc_unit;
    698  1.43       gwr 		channel  = zstty_conf[zstty_unit].channel;
    699  1.38       gwr 		cn = &consdev_tty;
    700  1.43       gwr 		cn->cn_dev = makedev(zs_major, zstty_unit);
    701  1.38       gwr 		cn->cn_pri = CN_REMOTE;
    702  1.38       gwr 		break;
    703  1.38       gwr 
    704  1.31       gwr 	default:
    705  1.38       gwr 		mon_printf("cninit: invalid PROM console selector\n");
    706  1.31       gwr 		/* assume keyboard/display */
    707  1.31       gwr 		/* fallthrough */
    708  1.38       gwr 	case 0:	/* keyboard/display */
    709  1.31       gwr 		zsc_unit = 0;
    710  1.31       gwr 		channel = 0;
    711  1.31       gwr 		cn = &consdev_kd;
    712  1.31       gwr 		/* Set cn_dev, cn_pri in kd.c */
    713  1.31       gwr 		break;
    714   1.1     glass 	}
    715   1.1     glass 
    716  1.31       gwr 	zc = zs_get_chan_addr(zsc_unit, channel);
    717  1.31       gwr 	if (zc == NULL) {
    718  1.31       gwr 		mon_printf("cninit: zs not mapped.\n");
    719  1.31       gwr 		return;
    720  1.31       gwr 	}
    721  1.31       gwr 	zs_conschan = zc;
    722  1.31       gwr 	zs_hwflags[zsc_unit][channel] = ZS_HWFLAG_CONSOLE;
    723  1.31       gwr 	cn_tab = cn;
    724  1.31       gwr 	(*cn->cn_init)(cn);
    725   1.1     glass }
    726   1.1     glass 
    727  1.31       gwr /* We never call this. */
    728  1.31       gwr void
    729  1.31       gwr nullcnprobe(cn)
    730  1.31       gwr 	struct consdev *cn;
    731   1.1     glass {
    732   1.1     glass }
    733   1.1     glass 
    734  1.31       gwr void
    735  1.31       gwr zscninit(cn)
    736  1.31       gwr 	struct consdev *cn;
    737   1.1     glass {
    738  1.31       gwr 	int unit = minor(cn->cn_dev) & 1;
    739   1.1     glass 
    740  1.31       gwr 	mon_printf("console is zstty%d (tty%c)\n",
    741  1.31       gwr 		   unit, unit + 'a');
    742   1.1     glass }
    743   1.1     glass 
    744   1.1     glass /*
    745  1.31       gwr  * Polled console input putchar.
    746   1.1     glass  */
    747  1.43       gwr static int
    748  1.31       gwr zscngetc(dev)
    749  1.29       gwr 	dev_t dev;
    750   1.1     glass {
    751  1.31       gwr 	register int c;
    752   1.1     glass 
    753  1.43       gwr 	c = zs_getc(zs_conschan);
    754  1.31       gwr 	return (c);
    755   1.1     glass }
    756   1.1     glass 
    757   1.1     glass /*
    758  1.31       gwr  * Polled console output putchar.
    759   1.1     glass  */
    760  1.43       gwr static void
    761  1.31       gwr zscnputc(dev, c)
    762  1.29       gwr 	dev_t dev;
    763  1.31       gwr 	int c;
    764   1.1     glass {
    765   1.1     glass 
    766  1.43       gwr 	zs_putc(zs_conschan, c);
    767   1.1     glass }
    768   1.1     glass 
    769   1.1     glass 
    770  1.31       gwr struct consdev consdev_tty = {
    771  1.31       gwr 	nullcnprobe,
    772  1.31       gwr 	zscninit,
    773  1.31       gwr 	zscngetc,
    774  1.31       gwr 	zscnputc,
    775  1.31       gwr 	nullcnpollc,
    776  1.31       gwr };
    777  1.31       gwr 
    778   1.1     glass 
    779   1.1     glass /*
    780  1.31       gwr  * Handle user request to enter kernel debugger.
    781   1.1     glass  */
    782  1.31       gwr void
    783  1.43       gwr zs_abort(cs)
    784  1.43       gwr 	struct zs_chanstate *cs;
    785   1.1     glass {
    786  1.31       gwr 	register volatile struct zschan *zc = zs_conschan;
    787  1.31       gwr 	int rr0;
    788   1.3       gwr 
    789  1.31       gwr 	/* Wait for end of break to avoid PROM abort. */
    790  1.31       gwr 	/* XXX - Limit the wait? */
    791  1.31       gwr 	do {
    792  1.31       gwr 		rr0 = zc->zc_csr;
    793  1.31       gwr 		ZS_DELAY();
    794  1.31       gwr 	} while (rr0 & ZSRR0_BREAK);
    795   1.1     glass 
    796  1.31       gwr 	/* XXX - Always available, but may be the PROM monitor. */
    797  1.31       gwr 	Debugger();
    798   1.1     glass }
    799