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