Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.72
      1  1.72     lukem /*	$NetBSD: zs.c,v 1.72 2003/07/15 03:36:16 lukem 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.72     lukem 
     47  1.72     lukem #include <sys/cdefs.h>
     48  1.72     lukem __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.72 2003/07/15 03:36:16 lukem Exp $");
     49  1.62     lukem 
     50  1.62     lukem #include "opt_kgdb.h"
     51   1.1     glass 
     52   1.5       gwr #include <sys/param.h>
     53   1.1     glass #include <sys/systm.h>
     54  1.43       gwr #include <sys/conf.h>
     55   1.1     glass #include <sys/device.h>
     56   1.1     glass #include <sys/file.h>
     57   1.1     glass #include <sys/ioctl.h>
     58  1.43       gwr #include <sys/kernel.h>
     59  1.43       gwr #include <sys/proc.h>
     60   1.1     glass #include <sys/tty.h>
     61   1.1     glass #include <sys/time.h>
     62   1.1     glass #include <sys/syslog.h>
     63   1.1     glass 
     64   1.1     glass #include <machine/autoconf.h>
     65   1.1     glass #include <machine/cpu.h>
     66   1.3       gwr #include <machine/mon.h>
     67  1.49       gwr #include <machine/z8530var.h>
     68  1.49       gwr 
     69  1.53       gwr #include <sun3/sun3/machdep.h>
     70  1.53       gwr #ifdef	_SUN3X_
     71  1.53       gwr #include <sun3/sun3x/obio.h>
     72  1.53       gwr #else
     73  1.53       gwr #include <sun3/sun3/obio.h>
     74  1.53       gwr #endif
     75  1.53       gwr #include <sun3/dev/zs_cons.h>
     76  1.53       gwr 
     77  1.49       gwr #include <dev/cons.h>
     78  1.49       gwr #include <dev/ic/z8530reg.h>
     79   1.1     glass 
     80  1.50       gwr #include "kbd.h"	/* NKBD */
     81  1.50       gwr #include "zsc.h"	/* NZSC */
     82  1.50       gwr #define NZS NZSC
     83  1.50       gwr 
     84  1.50       gwr /* Make life easier for the initialized arrays here. */
     85  1.50       gwr #if NZS < 2
     86  1.50       gwr #undef  NZS
     87  1.50       gwr #define NZS 2
     88  1.50       gwr #endif
     89  1.47       gwr 
     90  1.16       gwr /*
     91  1.43       gwr  * Some warts needed by z8530tty.c -
     92  1.43       gwr  * The default parity REALLY needs to be the same as the PROM uses,
     93  1.43       gwr  * or you can not see messages done with printf during boot-up...
     94  1.43       gwr  */
     95  1.43       gwr int zs_def_cflag = (CREAD | CS8 | HUPCL);
     96   1.1     glass 
     97  1.43       gwr /*
     98  1.43       gwr  * The Sun3 provides a 4.9152 MHz clock to the ZS chips.
     99  1.43       gwr  */
    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.33       gwr #define ZS_DELAY()			delay(2)
    109  1.31       gwr 
    110  1.31       gwr /* The layout of this is hardware-dependent (padding, order). */
    111  1.31       gwr struct zschan {
    112  1.31       gwr 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    113  1.31       gwr 	u_char		zc_xxx0;
    114  1.31       gwr 	volatile u_char	zc_data;	/* data */
    115  1.31       gwr 	u_char		zc_xxx1;
    116  1.31       gwr };
    117  1.31       gwr struct zsdevice {
    118  1.31       gwr 	/* Yes, they are backwards. */
    119  1.31       gwr 	struct	zschan zs_chan_b;
    120  1.31       gwr 	struct	zschan zs_chan_a;
    121   1.1     glass };
    122   1.1     glass 
    123   1.1     glass 
    124  1.31       gwr /* Default OBIO addresses. */
    125  1.50       gwr static int zs_physaddr[NZS] = {
    126  1.44       gwr 	OBIO_ZS_KBD_MS,
    127  1.44       gwr 	OBIO_ZS_TTY_AB };
    128  1.43       gwr 
    129  1.31       gwr /* Saved PROM mappings */
    130  1.50       gwr static struct zsdevice *zsaddr[NZS];
    131  1.43       gwr 
    132  1.31       gwr /* Flags from cninit() */
    133  1.50       gwr static int zs_hwflags[NZS][2];
    134  1.43       gwr 
    135  1.31       gwr /* Default speed for each channel */
    136  1.50       gwr static int zs_defspeed[NZS][2] = {
    137  1.31       gwr 	{ 1200, 	/* keyboard */
    138  1.31       gwr 	  1200 },	/* mouse */
    139  1.31       gwr 	{ 9600, 	/* ttya */
    140  1.31       gwr 	  9600 },	/* ttyb */
    141  1.31       gwr };
    142  1.13       gwr 
    143  1.43       gwr static u_char zs_init_reg[16] = {
    144  1.43       gwr 	0,	/* 0: CMD (reset, etc.) */
    145  1.43       gwr 	0,	/* 1: No interrupts yet. */
    146  1.43       gwr 	0x18 + ZSHARD_PRI,	/* IVECT */
    147  1.43       gwr 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    148  1.43       gwr 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    149  1.43       gwr 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    150  1.43       gwr 	0,	/* 6: TXSYNC/SYNCLO */
    151  1.43       gwr 	0,	/* 7: RXSYNC/SYNCHI */
    152  1.43       gwr 	0,	/* 8: alias for data port */
    153  1.43       gwr 	ZSWR9_MASTER_IE,
    154  1.43       gwr 	0,	/*10: Misc. TX/RX control bits */
    155  1.43       gwr 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    156  1.56   mycroft 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    157  1.56   mycroft 	0,			/*13: BAUDHI (default=9600) */
    158  1.43       gwr 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    159  1.55   mycroft 	ZSWR15_BREAK_IE,
    160  1.43       gwr };
    161  1.43       gwr 
    162   1.1     glass 
    163  1.31       gwr /* Find PROM mappings (for console support). */
    164  1.43       gwr void
    165  1.43       gwr zs_init()
    166  1.31       gwr {
    167  1.31       gwr 	int i;
    168   1.1     glass 
    169  1.50       gwr 	for (i = 0; i < NZS; i++) {
    170  1.31       gwr 		zsaddr[i] = (struct zsdevice *)
    171  1.44       gwr 			obio_find_mapping(zs_physaddr[i], sizeof(struct zschan));
    172  1.31       gwr 	}
    173  1.43       gwr }
    174  1.13       gwr 
    175  1.47       gwr struct zschan *
    176  1.50       gwr zs_get_chan_addr(zs_unit, channel)
    177  1.50       gwr 	int zs_unit, channel;
    178  1.31       gwr {
    179  1.31       gwr 	struct zsdevice *addr;
    180  1.31       gwr 	struct zschan *zc;
    181  1.31       gwr 
    182  1.50       gwr 	if (zs_unit >= NZS)
    183  1.31       gwr 		return NULL;
    184  1.50       gwr 	addr = zsaddr[zs_unit];
    185  1.31       gwr 	if (addr == NULL)
    186  1.31       gwr 		return NULL;
    187  1.31       gwr 	if (channel == 0) {
    188  1.31       gwr 		zc = &addr->zs_chan_a;
    189  1.31       gwr 	} else {
    190  1.31       gwr 		zc = &addr->zs_chan_b;
    191  1.31       gwr 	}
    192  1.31       gwr 	return (zc);
    193  1.31       gwr }
    194  1.13       gwr 
    195  1.18       gwr 
    196  1.31       gwr /****************************************************************
    197  1.31       gwr  * Autoconfig
    198  1.31       gwr  ****************************************************************/
    199  1.31       gwr 
    200  1.31       gwr /* Definition of the driver for autoconfig. */
    201  1.50       gwr static int	zs_match __P((struct device *, struct cfdata *, void *));
    202  1.50       gwr static void	zs_attach __P((struct device *, struct device *, void *));
    203  1.50       gwr static int  zs_print __P((void *, const char *name));
    204  1.31       gwr 
    205  1.68   thorpej CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
    206  1.69   thorpej     zs_match, zs_attach, NULL, NULL);
    207  1.34   thorpej 
    208  1.52   thorpej extern struct cfdriver zsc_cd;
    209  1.31       gwr 
    210  1.43       gwr static int zshard __P((void *));
    211  1.43       gwr static int zssoft __P((void *));
    212  1.43       gwr static int zs_get_speed __P((struct zs_chanstate *));
    213  1.31       gwr 
    214   1.9       gwr 
    215   1.1     glass /*
    216  1.31       gwr  * Is the zs chip present?
    217   1.1     glass  */
    218   1.1     glass static int
    219  1.50       gwr zs_match(parent, cf, aux)
    220  1.31       gwr 	struct device *parent;
    221  1.43       gwr 	struct cfdata *cf;
    222  1.43       gwr 	void *aux;
    223   1.1     glass {
    224  1.31       gwr 	struct confargs *ca = aux;
    225  1.61       chs 	int unit;
    226  1.35       gwr 	void *va;
    227  1.13       gwr 
    228  1.35       gwr 	/*
    229  1.43       gwr 	 * This driver only supports its wired-in mappings,
    230  1.43       gwr 	 * because the console support depends on those.
    231  1.35       gwr 	 */
    232  1.61       chs 	if (ca->ca_paddr == zs_physaddr[0]) {
    233  1.61       chs 		unit = 0;
    234  1.61       chs 	} else if (ca->ca_paddr == zs_physaddr[1]) {
    235  1.61       chs 		unit = 1;
    236  1.61       chs 	} else {
    237  1.35       gwr 		return (0);
    238  1.61       chs 	}
    239  1.35       gwr 
    240  1.31       gwr 	/* Make sure zs_init() found mappings. */
    241  1.35       gwr 	va = zsaddr[unit];
    242  1.35       gwr 	if (va == NULL)
    243  1.21       gwr 		return (0);
    244  1.21       gwr 
    245  1.21       gwr 	/* This returns -1 on a fault (bus error). */
    246  1.43       gwr 	if (peek_byte(va) == -1)
    247  1.43       gwr 		return (0);
    248  1.43       gwr 
    249  1.43       gwr 	/* Default interrupt priority (always splbio==2) */
    250  1.43       gwr 	if (ca->ca_intpri == -1)
    251  1.43       gwr 		ca->ca_intpri = ZSHARD_PRI;
    252  1.43       gwr 
    253  1.43       gwr 	return (1);
    254   1.1     glass }
    255   1.1     glass 
    256   1.1     glass /*
    257   1.1     glass  * Attach a found zs.
    258   1.1     glass  *
    259  1.31       gwr  * Match slave number to zs unit number, so that misconfiguration will
    260  1.31       gwr  * not set up the keyboard as ttya, etc.
    261   1.1     glass  */
    262   1.1     glass static void
    263  1.50       gwr zs_attach(parent, self, aux)
    264  1.31       gwr 	struct device *parent;
    265  1.31       gwr 	struct device *self;
    266  1.31       gwr 	void *aux;
    267  1.31       gwr {
    268  1.31       gwr 	struct zsc_softc *zsc = (void *) self;
    269  1.31       gwr 	struct confargs *ca = aux;
    270  1.31       gwr 	struct zsc_attach_args zsc_args;
    271  1.31       gwr 	volatile struct zschan *zc;
    272  1.31       gwr 	struct zs_chanstate *cs;
    273  1.50       gwr 	int s, zs_unit, channel;
    274  1.50       gwr 	static int didintr;
    275   1.2     glass 
    276  1.50       gwr 	zs_unit = zsc->zsc_dev.dv_unit;
    277  1.13       gwr 
    278  1.43       gwr 	printf(": (softpri %d)\n", ZSSOFT_PRI);
    279   1.1     glass 
    280  1.31       gwr 	/* Use the mapping setup by the Sun PROM. */
    281  1.50       gwr 	if (zsaddr[zs_unit] == NULL)
    282  1.66    provos 		panic("zs_attach: zs%d not mapped", zs_unit);
    283  1.31       gwr 
    284  1.31       gwr 	/*
    285  1.31       gwr 	 * Initialize software state for each channel.
    286  1.31       gwr 	 */
    287  1.31       gwr 	for (channel = 0; channel < 2; channel++) {
    288  1.43       gwr 		zsc_args.channel = channel;
    289  1.50       gwr 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    290  1.43       gwr 		cs = &zsc->zsc_cs_store[channel];
    291  1.43       gwr 		zsc->zsc_cs[channel] = cs;
    292   1.1     glass 
    293  1.71        pk 		simple_lock_init(&cs->cs_lock);
    294  1.31       gwr 		cs->cs_channel = channel;
    295  1.31       gwr 		cs->cs_private = NULL;
    296  1.31       gwr 		cs->cs_ops = &zsops_null;
    297  1.37       gwr 		cs->cs_brg_clk = PCLK / 16;
    298  1.31       gwr 
    299  1.50       gwr 		zc = zs_get_chan_addr(zs_unit, channel);
    300  1.43       gwr 		cs->cs_reg_csr  = &zc->zc_csr;
    301  1.43       gwr 		cs->cs_reg_data = &zc->zc_data;
    302   1.2     glass 
    303  1.64   tsutsui 		memcpy(cs->cs_creg, zs_init_reg, 16);
    304  1.64   tsutsui 		memcpy(cs->cs_preg, zs_init_reg, 16);
    305  1.15       gwr 
    306  1.43       gwr 		/* XXX: Get these from the EEPROM instead? */
    307  1.43       gwr 		/* XXX: See the mvme167 code.  Better. */
    308  1.43       gwr 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    309  1.43       gwr 			cs->cs_defspeed = zs_get_speed(cs);
    310  1.43       gwr 		else
    311  1.50       gwr 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    312  1.43       gwr 		cs->cs_defcflag = zs_def_cflag;
    313  1.43       gwr 
    314  1.47       gwr 		/* Make these correspond to cs_defcflag (-crtscts) */
    315  1.46       gwr 		cs->cs_rr0_dcd = ZSRR0_DCD;
    316  1.47       gwr 		cs->cs_rr0_cts = 0;
    317  1.47       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    318  1.47       gwr 		cs->cs_wr5_rts = 0;
    319  1.46       gwr 
    320   1.1     glass 		/*
    321  1.31       gwr 		 * Clear the master interrupt enable.
    322  1.31       gwr 		 * The INTENA is common to both channels,
    323  1.31       gwr 		 * so just do it on the A channel.
    324   1.1     glass 		 */
    325  1.31       gwr 		if (channel == 0) {
    326  1.32       gwr 			zs_write_reg(cs, 9, 0);
    327  1.31       gwr 		}
    328  1.15       gwr 
    329   1.1     glass 		/*
    330  1.31       gwr 		 * Look for a child driver for this channel.
    331  1.31       gwr 		 * The child attach will setup the hardware.
    332   1.1     glass 		 */
    333  1.50       gwr 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    334  1.31       gwr 			/* No sub-driver.  Just reset it. */
    335  1.43       gwr 			u_char reset = (channel == 0) ?
    336  1.31       gwr 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    337  1.45       gwr 			s = splhigh();
    338  1.32       gwr 			zs_write_reg(cs,  9, reset);
    339  1.31       gwr 			splx(s);
    340  1.31       gwr 		}
    341   1.1     glass 	}
    342   1.1     glass 
    343  1.43       gwr 	/*
    344  1.43       gwr 	 * Now safe to install interrupt handlers.  Note the arguments
    345  1.43       gwr 	 * to the interrupt handlers aren't used.  Note, we only do this
    346  1.43       gwr 	 * once since both SCCs interrupt at the same level and vector.
    347  1.43       gwr 	 */
    348  1.50       gwr 	if (!didintr) {
    349  1.50       gwr 		didintr = 1;
    350  1.31       gwr 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
    351  1.43       gwr 		isr_add_autovect(zshard, NULL, ca->ca_intpri);
    352  1.31       gwr 	}
    353  1.50       gwr 	/* XXX; evcnt_attach() ? */
    354  1.24       gwr 
    355  1.31       gwr 	/*
    356  1.31       gwr 	 * Set the master interrupt enable and interrupt vector.
    357  1.31       gwr 	 * (common to both channels, do it on A)
    358  1.31       gwr 	 */
    359  1.43       gwr 	cs = zsc->zsc_cs[0];
    360  1.45       gwr 	s = splhigh();
    361  1.31       gwr 	/* interrupt vector */
    362  1.32       gwr 	zs_write_reg(cs, 2, zs_init_reg[2]);
    363  1.31       gwr 	/* master interrupt control (enable) */
    364  1.32       gwr 	zs_write_reg(cs, 9, zs_init_reg[9]);
    365  1.31       gwr 	splx(s);
    366  1.45       gwr 
    367  1.45       gwr 	/*
    368  1.45       gwr 	 * XXX: L1A hack - We would like to be able to break into
    369  1.45       gwr 	 * the debugger during the rest of autoconfiguration, so
    370  1.45       gwr 	 * lower interrupts just enough to let zs interrupts in.
    371  1.50       gwr 	 * This is done after both zs devices are attached.
    372  1.45       gwr 	 */
    373  1.50       gwr 	if (zs_unit == 1) {
    374  1.45       gwr 		(void)spl5(); /* splzs - 1 */
    375  1.45       gwr 	}
    376  1.35       gwr }
    377  1.35       gwr 
    378  1.35       gwr static int
    379  1.50       gwr zs_print(aux, name)
    380  1.35       gwr 	void *aux;
    381  1.39       cgd 	const char *name;
    382  1.35       gwr {
    383  1.35       gwr 	struct zsc_attach_args *args = aux;
    384  1.35       gwr 
    385  1.35       gwr 	if (name != NULL)
    386  1.70   thorpej 		aprint_normal("%s: ", name);
    387  1.35       gwr 
    388  1.35       gwr 	if (args->channel != -1)
    389  1.70   thorpej 		aprint_normal(" channel %d", args->channel);
    390  1.35       gwr 
    391  1.35       gwr 	return UNCONF;
    392  1.24       gwr }
    393  1.24       gwr 
    394  1.48       gwr static volatile int zssoftpending;
    395  1.43       gwr 
    396  1.43       gwr /*
    397  1.43       gwr  * Our ZS chips all share a common, autovectored interrupt,
    398  1.43       gwr  * so we have to look at all of them on each interrupt.
    399  1.43       gwr  */
    400  1.31       gwr static int
    401  1.31       gwr zshard(arg)
    402  1.31       gwr 	void *arg;
    403   1.1     glass {
    404  1.63   tsutsui 	struct zsc_softc *zsc;
    405  1.63   tsutsui 	int unit, rval, softreq;
    406  1.43       gwr 
    407  1.48       gwr 	rval = softreq = 0;
    408  1.48       gwr 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    409  1.34   thorpej 		zsc = zsc_cd.cd_devs[unit];
    410  1.43       gwr 		if (zsc == NULL)
    411  1.43       gwr 			continue;
    412  1.43       gwr 		rval |= zsc_intr_hard(zsc);
    413  1.48       gwr 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    414  1.48       gwr 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    415  1.48       gwr 	}
    416  1.48       gwr 
    417  1.48       gwr 	/* We are at splzs here, so no need to lock. */
    418  1.48       gwr 	if (softreq && (zssoftpending == 0)) {
    419  1.48       gwr 		zssoftpending = ZSSOFT_PRI;
    420  1.48       gwr 		isr_soft_request(ZSSOFT_PRI);
    421  1.31       gwr 	}
    422  1.31       gwr 	return (rval);
    423   1.1     glass }
    424   1.1     glass 
    425  1.43       gwr /*
    426  1.43       gwr  * Similar scheme as for zshard (look at all of them)
    427  1.43       gwr  */
    428   1.3       gwr static int
    429  1.31       gwr zssoft(arg)
    430  1.31       gwr 	void *arg;
    431   1.3       gwr {
    432  1.63   tsutsui 	struct zsc_softc *zsc;
    433  1.63   tsutsui 	int s, unit;
    434  1.31       gwr 
    435  1.31       gwr 	/* This is not the only ISR on this IPL. */
    436  1.31       gwr 	if (zssoftpending == 0)
    437  1.31       gwr 		return (0);
    438   1.3       gwr 
    439  1.31       gwr 	/*
    440  1.31       gwr 	 * The soft intr. bit will be set by zshard only if
    441  1.31       gwr 	 * the variable zssoftpending is zero.  The order of
    442  1.31       gwr 	 * these next two statements prevents our clearing
    443  1.31       gwr 	 * the soft intr bit just after zshard has set it.
    444  1.31       gwr 	 */
    445  1.31       gwr 	isr_soft_clear(ZSSOFT_PRI);
    446  1.31       gwr 	zssoftpending = 0;
    447   1.2     glass 
    448  1.48       gwr 	/* Make sure we call the tty layer at spltty. */
    449  1.48       gwr 	s = spltty();
    450  1.48       gwr 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    451  1.34   thorpej 		zsc = zsc_cd.cd_devs[unit];
    452  1.43       gwr 		if (zsc == NULL)
    453  1.43       gwr 			continue;
    454  1.43       gwr 		(void) zsc_intr_soft(zsc);
    455   1.3       gwr 	}
    456  1.48       gwr 	splx(s);
    457  1.31       gwr 	return (1);
    458   1.2     glass }
    459   1.2     glass 
    460   1.2     glass 
    461  1.31       gwr /*
    462  1.50       gwr  * Compute the current baud rate given a ZS channel.
    463  1.43       gwr  */
    464  1.43       gwr static int
    465  1.43       gwr zs_get_speed(cs)
    466  1.43       gwr 	struct zs_chanstate *cs;
    467  1.43       gwr {
    468  1.43       gwr 	int tconst;
    469  1.43       gwr 
    470  1.43       gwr 	tconst = zs_read_reg(cs, 12);
    471  1.43       gwr 	tconst |= zs_read_reg(cs, 13) << 8;
    472  1.43       gwr 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    473  1.43       gwr }
    474  1.43       gwr 
    475  1.43       gwr /*
    476  1.43       gwr  * MD functions for setting the baud rate and control modes.
    477  1.43       gwr  */
    478  1.43       gwr int
    479  1.43       gwr zs_set_speed(cs, bps)
    480  1.43       gwr 	struct zs_chanstate *cs;
    481  1.43       gwr 	int bps;	/* bits per second */
    482  1.43       gwr {
    483  1.43       gwr 	int tconst, real_bps;
    484  1.43       gwr 
    485  1.43       gwr 	if (bps == 0)
    486  1.43       gwr 		return (0);
    487  1.43       gwr 
    488  1.43       gwr #ifdef	DIAGNOSTIC
    489  1.43       gwr 	if (cs->cs_brg_clk == 0)
    490  1.43       gwr 		panic("zs_set_speed");
    491  1.43       gwr #endif
    492  1.43       gwr 
    493  1.43       gwr 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    494  1.43       gwr 	if (tconst < 0)
    495  1.43       gwr 		return (EINVAL);
    496  1.43       gwr 
    497  1.43       gwr 	/* Convert back to make sure we can do it. */
    498  1.43       gwr 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    499  1.43       gwr 
    500  1.43       gwr 	/* XXX - Allow some tolerance here? */
    501  1.43       gwr 	if (real_bps != bps)
    502  1.43       gwr 		return (EINVAL);
    503  1.43       gwr 
    504  1.43       gwr 	cs->cs_preg[12] = tconst;
    505  1.43       gwr 	cs->cs_preg[13] = tconst >> 8;
    506  1.43       gwr 
    507  1.43       gwr 	/* Caller will stuff the pending registers. */
    508  1.43       gwr 	return (0);
    509  1.43       gwr }
    510  1.43       gwr 
    511  1.43       gwr int
    512  1.43       gwr zs_set_modes(cs, cflag)
    513  1.43       gwr 	struct zs_chanstate *cs;
    514  1.43       gwr 	int cflag;	/* bits per second */
    515  1.43       gwr {
    516  1.43       gwr 	int s;
    517  1.43       gwr 
    518  1.43       gwr 	/*
    519  1.43       gwr 	 * Output hardware flow control on the chip is horrendous:
    520  1.43       gwr 	 * if carrier detect drops, the receiver is disabled, and if
    521  1.43       gwr 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    522  1.43       gwr 	 * Therefore, NEVER set the HFC bit, and instead use the
    523  1.43       gwr 	 * status interrupt to detect CTS changes.
    524  1.43       gwr 	 */
    525  1.43       gwr 	s = splzs();
    526  1.57  wrstuden 	cs->cs_rr0_pps = 0;
    527  1.57  wrstuden 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    528  1.43       gwr 		cs->cs_rr0_dcd = 0;
    529  1.57  wrstuden 		if ((cflag & MDMBUF) == 0)
    530  1.57  wrstuden 			cs->cs_rr0_pps = ZSRR0_DCD;
    531  1.57  wrstuden 	} else
    532  1.43       gwr 		cs->cs_rr0_dcd = ZSRR0_DCD;
    533  1.51   mycroft 	if ((cflag & CRTSCTS) != 0) {
    534  1.43       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR;
    535  1.43       gwr 		cs->cs_wr5_rts = ZSWR5_RTS;
    536  1.43       gwr 		cs->cs_rr0_cts = ZSRR0_CTS;
    537  1.51   mycroft 	} else if ((cflag & MDMBUF) != 0) {
    538  1.51   mycroft 		cs->cs_wr5_dtr = 0;
    539  1.51   mycroft 		cs->cs_wr5_rts = ZSWR5_DTR;
    540  1.51   mycroft 		cs->cs_rr0_cts = ZSRR0_DCD;
    541  1.43       gwr 	} else {
    542  1.43       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    543  1.43       gwr 		cs->cs_wr5_rts = 0;
    544  1.43       gwr 		cs->cs_rr0_cts = 0;
    545  1.43       gwr 	}
    546  1.43       gwr 	splx(s);
    547  1.43       gwr 
    548  1.43       gwr 	/* Caller will stuff the pending registers. */
    549  1.43       gwr 	return (0);
    550  1.43       gwr }
    551  1.43       gwr 
    552  1.43       gwr 
    553  1.43       gwr /*
    554  1.31       gwr  * Read or write the chip with suitable delays.
    555  1.31       gwr  */
    556   1.2     glass 
    557  1.31       gwr u_char
    558  1.31       gwr zs_read_reg(cs, reg)
    559  1.31       gwr 	struct zs_chanstate *cs;
    560  1.31       gwr 	u_char reg;
    561   1.1     glass {
    562  1.31       gwr 	u_char val;
    563   1.1     glass 
    564  1.31       gwr 	*cs->cs_reg_csr = reg;
    565  1.31       gwr 	ZS_DELAY();
    566  1.31       gwr 	val = *cs->cs_reg_csr;
    567  1.31       gwr 	ZS_DELAY();
    568  1.31       gwr 	return val;
    569  1.17       gwr }
    570   1.3       gwr 
    571  1.31       gwr void
    572  1.31       gwr zs_write_reg(cs, reg, val)
    573  1.31       gwr 	struct zs_chanstate *cs;
    574  1.31       gwr 	u_char reg, val;
    575  1.17       gwr {
    576  1.31       gwr 	*cs->cs_reg_csr = reg;
    577  1.31       gwr 	ZS_DELAY();
    578  1.31       gwr 	*cs->cs_reg_csr = val;
    579  1.32       gwr 	ZS_DELAY();
    580  1.32       gwr }
    581  1.32       gwr 
    582  1.32       gwr u_char zs_read_csr(cs)
    583  1.32       gwr 	struct zs_chanstate *cs;
    584  1.32       gwr {
    585  1.63   tsutsui 	u_char val;
    586  1.32       gwr 
    587  1.43       gwr 	val = *cs->cs_reg_csr;
    588  1.32       gwr 	ZS_DELAY();
    589  1.43       gwr 	return val;
    590  1.32       gwr }
    591  1.32       gwr 
    592  1.43       gwr void  zs_write_csr(cs, val)
    593  1.32       gwr 	struct zs_chanstate *cs;
    594  1.43       gwr 	u_char val;
    595  1.32       gwr {
    596  1.43       gwr 	*cs->cs_reg_csr = val;
    597  1.32       gwr 	ZS_DELAY();
    598  1.32       gwr }
    599  1.32       gwr 
    600  1.43       gwr u_char zs_read_data(cs)
    601  1.32       gwr 	struct zs_chanstate *cs;
    602  1.32       gwr {
    603  1.63   tsutsui 	u_char val;
    604  1.43       gwr 
    605  1.43       gwr 	val = *cs->cs_reg_data;
    606  1.32       gwr 	ZS_DELAY();
    607  1.43       gwr 	return val;
    608  1.32       gwr }
    609  1.32       gwr 
    610  1.32       gwr void  zs_write_data(cs, val)
    611  1.32       gwr 	struct zs_chanstate *cs;
    612  1.32       gwr 	u_char val;
    613  1.32       gwr {
    614  1.32       gwr 	*cs->cs_reg_data = val;
    615  1.31       gwr 	ZS_DELAY();
    616   1.1     glass }
    617   1.3       gwr 
    618  1.31       gwr /****************************************************************
    619  1.31       gwr  * Console support functions (Sun3 specific!)
    620  1.43       gwr  * Note: this code is allowed to know about the layout of
    621  1.43       gwr  * the chip registers, and uses that to keep things simple.
    622  1.43       gwr  * XXX - I think I like the mvme167 code better. -gwr
    623  1.31       gwr  ****************************************************************/
    624   1.1     glass 
    625  1.43       gwr void *zs_conschan;
    626  1.43       gwr 
    627   1.2     glass /*
    628  1.47       gwr  * Handle user request to enter kernel debugger.
    629  1.47       gwr  */
    630  1.47       gwr void
    631  1.47       gwr zs_abort(cs)
    632  1.47       gwr 	struct zs_chanstate *cs;
    633  1.47       gwr {
    634  1.63   tsutsui 	volatile struct zschan *zc = zs_conschan;
    635  1.47       gwr 	int rr0;
    636  1.47       gwr 
    637  1.47       gwr 	/* Wait for end of break to avoid PROM abort. */
    638  1.47       gwr 	/* XXX - Limit the wait? */
    639  1.47       gwr 	do {
    640  1.47       gwr 		rr0 = zc->zc_csr;
    641  1.47       gwr 		ZS_DELAY();
    642  1.47       gwr 	} while (rr0 & ZSRR0_BREAK);
    643  1.47       gwr 
    644  1.59  jdolecek 	/* This is always available on the Sun3. */
    645  1.47       gwr 	Debugger();
    646  1.47       gwr }
    647  1.47       gwr 
    648  1.47       gwr /*
    649  1.31       gwr  * Polled input char.
    650   1.2     glass  */
    651   1.2     glass int
    652  1.31       gwr zs_getc(arg)
    653  1.31       gwr 	void *arg;
    654   1.2     glass {
    655  1.63   tsutsui 	volatile struct zschan *zc = arg;
    656  1.63   tsutsui 	int s, c, rr0;
    657   1.2     glass 
    658   1.2     glass 	s = splhigh();
    659   1.9       gwr 	/* Wait for a character to arrive. */
    660  1.25       gwr 	do {
    661  1.25       gwr 		rr0 = zc->zc_csr;
    662   1.3       gwr 		ZS_DELAY();
    663  1.25       gwr 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    664   1.9       gwr 
    665   1.2     glass 	c = zc->zc_data;
    666   1.9       gwr 	ZS_DELAY();
    667   1.2     glass 	splx(s);
    668  1.17       gwr 
    669  1.17       gwr 	/*
    670  1.17       gwr 	 * This is used by the kd driver to read scan codes,
    671  1.17       gwr 	 * so don't translate '\r' ==> '\n' here...
    672  1.17       gwr 	 */
    673   1.2     glass 	return (c);
    674   1.2     glass }
    675   1.1     glass 
    676   1.1     glass /*
    677  1.31       gwr  * Polled output char.
    678   1.1     glass  */
    679  1.31       gwr void
    680  1.31       gwr zs_putc(arg, c)
    681  1.31       gwr 	void *arg;
    682   1.1     glass 	int c;
    683   1.1     glass {
    684  1.63   tsutsui 	volatile struct zschan *zc = arg;
    685  1.63   tsutsui 	int s, rr0;
    686   1.1     glass 
    687   1.9       gwr 	s = splhigh();
    688   1.9       gwr 	/* Wait for transmitter to become ready. */
    689  1.25       gwr 	do {
    690  1.25       gwr 		rr0 = zc->zc_csr;
    691   1.3       gwr 		ZS_DELAY();
    692  1.25       gwr 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    693   1.9       gwr 
    694   1.1     glass 	zc->zc_data = c;
    695   1.3       gwr 	ZS_DELAY();
    696   1.1     glass 	splx(s);
    697   1.1     glass }
    698   1.2     glass 
    699  1.50       gwr /*****************************************************************/
    700  1.50       gwr 
    701  1.50       gwr static void zscninit __P((struct consdev *));
    702  1.50       gwr static int  zscngetc __P((dev_t));
    703  1.50       gwr static void zscnputc __P((dev_t, int));
    704  1.50       gwr 
    705  1.50       gwr /*
    706  1.50       gwr  * Console table shared by ttya, ttyb
    707  1.50       gwr  */
    708  1.50       gwr struct consdev consdev_tty = {
    709  1.50       gwr 	nullcnprobe,
    710  1.50       gwr 	zscninit,
    711  1.50       gwr 	zscngetc,
    712  1.50       gwr 	zscnputc,
    713  1.50       gwr 	nullcnpollc,
    714  1.60   thorpej 	NULL,
    715  1.50       gwr };
    716  1.50       gwr 
    717  1.50       gwr static void
    718  1.50       gwr zscninit(cn)
    719  1.50       gwr 	struct consdev *cn;
    720  1.50       gwr {
    721  1.50       gwr }
    722  1.50       gwr 
    723  1.50       gwr /*
    724  1.50       gwr  * Polled console input putchar.
    725  1.50       gwr  */
    726  1.50       gwr static int
    727  1.50       gwr zscngetc(dev)
    728  1.50       gwr 	dev_t dev;
    729  1.50       gwr {
    730  1.50       gwr 	return (zs_getc(zs_conschan));
    731  1.50       gwr }
    732  1.50       gwr 
    733  1.50       gwr /*
    734  1.50       gwr  * Polled console output putchar.
    735  1.50       gwr  */
    736  1.50       gwr static void
    737  1.50       gwr zscnputc(dev, c)
    738  1.50       gwr 	dev_t dev;
    739  1.50       gwr 	int c;
    740  1.50       gwr {
    741  1.50       gwr 	zs_putc(zs_conschan, c);
    742  1.50       gwr }
    743  1.50       gwr 
    744  1.50       gwr /*****************************************************************/
    745  1.50       gwr 
    746  1.50       gwr static void prom_cninit __P((struct consdev *));
    747  1.50       gwr static int  prom_cngetc __P((dev_t));
    748  1.50       gwr static void prom_cnputc __P((dev_t, int));
    749  1.50       gwr 
    750  1.50       gwr /*
    751  1.50       gwr  * The console is set to this one initially,
    752  1.50       gwr  * which lets us use the PROM until consinit()
    753  1.50       gwr  * is called to select a real console.
    754  1.50       gwr  */
    755  1.50       gwr struct consdev consdev_prom = {
    756  1.50       gwr 	nullcnprobe,
    757  1.50       gwr 	prom_cninit,
    758  1.50       gwr 	prom_cngetc,
    759  1.50       gwr 	prom_cnputc,
    760  1.50       gwr 	nullcnpollc,
    761  1.50       gwr };
    762  1.50       gwr 
    763  1.50       gwr /*
    764  1.50       gwr  * The console table pointer is statically initialized
    765  1.50       gwr  * to point to the PROM (output only) table, so that
    766  1.50       gwr  * early calls to printf will work.
    767  1.50       gwr  */
    768  1.50       gwr struct consdev *cn_tab = &consdev_prom;
    769  1.50       gwr 
    770  1.50       gwr void
    771  1.50       gwr nullcnprobe(cn)
    772  1.50       gwr 	struct consdev *cn;
    773  1.50       gwr {
    774  1.50       gwr }
    775  1.50       gwr 
    776  1.50       gwr static void
    777  1.50       gwr prom_cninit(cn)
    778  1.50       gwr 	struct consdev *cn;
    779  1.50       gwr {
    780  1.50       gwr }
    781  1.50       gwr 
    782  1.50       gwr /*
    783  1.50       gwr  * PROM console input putchar.
    784  1.50       gwr  * (dummy - this is output only)
    785  1.50       gwr  */
    786  1.50       gwr static int
    787  1.50       gwr prom_cngetc(dev)
    788  1.50       gwr 	dev_t dev;
    789  1.50       gwr {
    790  1.50       gwr 	return (0);
    791  1.50       gwr }
    792  1.50       gwr 
    793  1.50       gwr /*
    794  1.50       gwr  * PROM console output putchar.
    795  1.50       gwr  */
    796  1.50       gwr static void
    797  1.50       gwr prom_cnputc(dev, c)
    798  1.50       gwr 	dev_t dev;
    799  1.50       gwr 	int c;
    800  1.50       gwr {
    801  1.50       gwr 	(*romVectorPtr->putChar)(c & 0x7f);
    802  1.50       gwr }
    803  1.50       gwr 
    804  1.50       gwr /*****************************************************************/
    805  1.50       gwr 
    806  1.50       gwr extern struct consdev consdev_kd;
    807  1.31       gwr 
    808  1.43       gwr static struct {
    809  1.50       gwr 	int zs_unit, channel;
    810  1.50       gwr } zstty_conf[NZS*2] = {
    811  1.43       gwr 	/* XXX: knowledge from the config file here... */
    812  1.43       gwr 	{ 1, 0 },	/* ttya */
    813  1.43       gwr 	{ 1, 1 },	/* ttyb */
    814  1.43       gwr 	{ 0, 0 },	/* ttyc */
    815  1.43       gwr 	{ 0, 1 },	/* ttyd */
    816  1.43       gwr };
    817  1.31       gwr 
    818  1.47       gwr static char *prom_inSrc_name[] = {
    819  1.47       gwr 	"keyboard/display",
    820  1.47       gwr 	"ttya", "ttyb",
    821  1.47       gwr 	"ttyc", "ttyd" };
    822  1.47       gwr 
    823   1.1     glass /*
    824  1.31       gwr  * This function replaces sys/dev/cninit.c
    825  1.31       gwr  * Determine which device is the console using
    826  1.38       gwr  * the PROM "input source" and "output sink".
    827   1.1     glass  */
    828  1.31       gwr void
    829  1.31       gwr cninit()
    830   1.1     glass {
    831  1.53       gwr 	struct sunromvec *v;
    832  1.31       gwr 	struct zschan *zc;
    833  1.31       gwr 	struct consdev *cn;
    834  1.50       gwr 	int channel, zs_unit, zstty_unit;
    835  1.50       gwr 	u_char inSource, outSink;
    836  1.65   gehenna 	extern const struct cdevsw zstty_cdevsw;
    837  1.53       gwr 
    838  1.53       gwr 	/* Get the zs driver ready for console duty. */
    839  1.53       gwr 	zs_init();
    840  1.31       gwr 
    841  1.38       gwr 	v = romVectorPtr;
    842  1.50       gwr 	inSource = *v->inSource;
    843  1.50       gwr 	outSink  = *v->outSink;
    844  1.50       gwr 	if (inSource != outSink) {
    845  1.38       gwr 		mon_printf("cninit: mismatched PROM output selector\n");
    846  1.38       gwr 	}
    847  1.38       gwr 
    848  1.38       gwr 	switch (inSource) {
    849  1.47       gwr 	default:
    850  1.47       gwr 		mon_printf("cninit: invalid inSource=%d\n", inSource);
    851  1.47       gwr 		sunmon_abort();
    852  1.47       gwr 		inSource = 0;
    853  1.47       gwr 		/* fall through */
    854  1.47       gwr 
    855  1.47       gwr 	case 0:	/* keyboard/display */
    856  1.47       gwr #if NKBD > 0
    857  1.50       gwr 		zs_unit = 0;
    858  1.47       gwr 		channel = 0;
    859  1.47       gwr 		cn = &consdev_kd;
    860  1.47       gwr 		/* Set cn_dev, cn_pri in kd.c */
    861  1.47       gwr 		break;
    862  1.47       gwr #else	/* NKBD */
    863  1.47       gwr 		mon_printf("cninit: kdb/display not configured\n");
    864  1.47       gwr 		sunmon_abort();
    865  1.47       gwr 		inSource = 1;
    866  1.47       gwr 		/* fall through */
    867  1.47       gwr #endif	/* NKBD */
    868  1.47       gwr 
    869  1.38       gwr 	case 1:	/* ttya */
    870  1.38       gwr 	case 2:	/* ttyb */
    871  1.38       gwr 	case 3:	/* ttyc (rewired keyboard connector) */
    872  1.38       gwr 	case 4:	/* ttyd (rewired mouse connector)   */
    873  1.43       gwr 		zstty_unit = inSource - 1;
    874  1.50       gwr 		zs_unit = zstty_conf[zstty_unit].zs_unit;
    875  1.50       gwr 		channel = zstty_conf[zstty_unit].channel;
    876  1.38       gwr 		cn = &consdev_tty;
    877  1.65   gehenna 		cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw),
    878  1.65   gehenna 				     zstty_unit);
    879  1.38       gwr 		cn->cn_pri = CN_REMOTE;
    880  1.38       gwr 		break;
    881  1.38       gwr 
    882   1.1     glass 	}
    883  1.47       gwr 	/* Now that inSource has been validated, print it. */
    884  1.47       gwr 	mon_printf("console is %s\n", prom_inSrc_name[inSource]);
    885   1.1     glass 
    886  1.50       gwr 	zc = zs_get_chan_addr(zs_unit, channel);
    887  1.31       gwr 	if (zc == NULL) {
    888  1.31       gwr 		mon_printf("cninit: zs not mapped.\n");
    889  1.31       gwr 		return;
    890  1.31       gwr 	}
    891  1.31       gwr 	zs_conschan = zc;
    892  1.50       gwr 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
    893  1.31       gwr 	cn_tab = cn;
    894  1.31       gwr 	(*cn->cn_init)(cn);
    895  1.47       gwr #ifdef	KGDB
    896  1.47       gwr 	zs_kgdb_init();
    897  1.47       gwr #endif
    898   1.1     glass }
    899