Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.57
      1  1.57        pk /*	$NetBSD: zs.c,v 1.57 1998/03/21 20:24:11 pk Exp $	*/
      2  1.18   deraadt 
      3  1.50       gwr /*-
      4  1.50       gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.50       gwr  * All rights reserved.
      6   1.1   deraadt  *
      7  1.50       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8  1.50       gwr  * by Gordon W. Ross.
      9   1.1   deraadt  *
     10   1.1   deraadt  * Redistribution and use in source and binary forms, with or without
     11   1.1   deraadt  * modification, are permitted provided that the following conditions
     12   1.1   deraadt  * are met:
     13   1.1   deraadt  * 1. Redistributions of source code must retain the above copyright
     14   1.1   deraadt  *    notice, this list of conditions and the following disclaimer.
     15   1.1   deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   deraadt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   deraadt  *    documentation and/or other materials provided with the distribution.
     18   1.1   deraadt  * 3. All advertising materials mentioning features or use of this software
     19   1.1   deraadt  *    must display the following acknowledgement:
     20  1.50       gwr  *        This product includes software developed by the NetBSD
     21  1.50       gwr  *        Foundation, Inc. and its contributors.
     22  1.50       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.50       gwr  *    contributors may be used to endorse or promote products derived
     24  1.50       gwr  *    from this software without specific prior written permission.
     25  1.50       gwr  *
     26  1.50       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.50       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.50       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.50       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.50       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.50       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.50       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.50       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.50       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.50       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.50       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1   deraadt  */
     38   1.1   deraadt 
     39   1.1   deraadt /*
     40  1.50       gwr  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41  1.50       gwr  *
     42  1.50       gwr  * Runs two serial lines per chip using slave drivers.
     43  1.50       gwr  * Plain tty/async lines use the zs_async slave.
     44  1.50       gwr  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     45   1.1   deraadt  */
     46  1.38       mrg 
     47   1.1   deraadt #include <sys/param.h>
     48  1.34  christos #include <sys/systm.h>
     49  1.50       gwr #include <sys/conf.h>
     50   1.1   deraadt #include <sys/device.h>
     51   1.1   deraadt #include <sys/file.h>
     52   1.1   deraadt #include <sys/ioctl.h>
     53  1.50       gwr #include <sys/kernel.h>
     54  1.50       gwr #include <sys/proc.h>
     55   1.1   deraadt #include <sys/tty.h>
     56   1.1   deraadt #include <sys/time.h>
     57   1.1   deraadt #include <sys/syslog.h>
     58   1.1   deraadt 
     59   1.1   deraadt #include <machine/autoconf.h>
     60  1.50       gwr #include <machine/bsd_openprom.h>
     61  1.37  christos #include <machine/conf.h>
     62   1.1   deraadt #include <machine/cpu.h>
     63  1.50       gwr #include <machine/eeprom.h>
     64  1.50       gwr #include <machine/psl.h>
     65  1.50       gwr #include <machine/z8530var.h>
     66  1.50       gwr 
     67  1.50       gwr #include <dev/cons.h>
     68  1.50       gwr #include <dev/ic/z8530reg.h>
     69   1.1   deraadt 
     70   1.1   deraadt #include <sparc/sparc/vaddrs.h>
     71   1.1   deraadt #include <sparc/sparc/auxreg.h>
     72  1.50       gwr #include <sparc/dev/cons.h>
     73  1.50       gwr 
     74  1.50       gwr #include "kbd.h"	/* NKBD */
     75  1.50       gwr #include "zs.h" 	/* NZS */
     76   1.1   deraadt 
     77  1.50       gwr /* Make life easier for the initialized arrays here. */
     78  1.50       gwr #if NZS < 3
     79  1.50       gwr #undef  NZS
     80  1.50       gwr #define NZS 3
     81   1.1   deraadt #endif
     82   1.1   deraadt 
     83  1.50       gwr /*
     84  1.50       gwr  * Some warts needed by z8530tty.c -
     85  1.50       gwr  * The default parity REALLY needs to be the same as the PROM uses,
     86  1.50       gwr  * or you can not see messages done with printf during boot-up...
     87  1.50       gwr  */
     88  1.50       gwr int zs_def_cflag = (CREAD | CS8 | HUPCL);
     89  1.50       gwr int zs_major = 12;
     90   1.1   deraadt 
     91  1.50       gwr /*
     92  1.50       gwr  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     93  1.50       gwr  */
     94  1.50       gwr #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     95   1.1   deraadt 
     96   1.1   deraadt /*
     97   1.1   deraadt  * Select software interrupt bit based on TTY ipl.
     98   1.1   deraadt  */
     99   1.1   deraadt #if PIL_TTY == 1
    100   1.1   deraadt # define IE_ZSSOFT IE_L1
    101   1.1   deraadt #elif PIL_TTY == 4
    102   1.1   deraadt # define IE_ZSSOFT IE_L4
    103   1.1   deraadt #elif PIL_TTY == 6
    104   1.1   deraadt # define IE_ZSSOFT IE_L6
    105   1.1   deraadt #else
    106   1.1   deraadt # error "no suitable software interrupt bit"
    107   1.1   deraadt #endif
    108   1.1   deraadt 
    109  1.54        pk /*
    110  1.54        pk  * The next three variables provide a shortcut to the channel state
    111  1.54        pk  * structure used by zscnputc().
    112  1.54        pk  */
    113  1.54        pk int zs_console_unit = -1;
    114  1.54        pk int zs_console_channel = -1;
    115  1.54        pk struct zs_chanstate *zs_conschanstate;
    116  1.54        pk 
    117  1.50       gwr #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
    118   1.1   deraadt 
    119  1.50       gwr /* The layout of this is hardware-dependent (padding, order). */
    120  1.50       gwr struct zschan {
    121  1.50       gwr 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    122  1.50       gwr 	u_char		zc_xxx0;
    123  1.50       gwr 	volatile u_char	zc_data;	/* data */
    124  1.50       gwr 	u_char		zc_xxx1;
    125  1.35   thorpej };
    126  1.50       gwr struct zsdevice {
    127  1.50       gwr 	/* Yes, they are backwards. */
    128  1.50       gwr 	struct	zschan zs_chan_b;
    129  1.50       gwr 	struct	zschan zs_chan_a;
    130  1.35   thorpej };
    131   1.1   deraadt 
    132  1.50       gwr /* Saved PROM mappings */
    133  1.50       gwr static struct zsdevice *zsaddr[NZS];
    134   1.1   deraadt 
    135  1.50       gwr /* Flags from cninit() */
    136  1.50       gwr static int zs_hwflags[NZS][2];
    137   1.1   deraadt 
    138  1.50       gwr /* Default speed for each channel */
    139  1.50       gwr static int zs_defspeed[NZS][2] = {
    140  1.50       gwr 	{ 9600, 	/* ttya */
    141  1.50       gwr 	  9600 },	/* ttyb */
    142  1.50       gwr 	{ 1200, 	/* keyboard */
    143  1.50       gwr 	  1200 },	/* mouse */
    144  1.50       gwr 	{ 9600, 	/* ttyc */
    145  1.50       gwr 	  9600 },	/* ttyd */
    146  1.50       gwr };
    147   1.1   deraadt 
    148  1.50       gwr static u_char zs_init_reg[16] = {
    149  1.50       gwr 	0,	/* 0: CMD (reset, etc.) */
    150  1.50       gwr 	0,	/* 1: No interrupts yet. */
    151  1.50       gwr 	0,	/* 2: IVECT */
    152  1.50       gwr 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    153  1.50       gwr 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    154  1.50       gwr 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    155  1.50       gwr 	0,	/* 6: TXSYNC/SYNCLO */
    156  1.50       gwr 	0,	/* 7: RXSYNC/SYNCHI */
    157  1.50       gwr 	0,	/* 8: alias for data port */
    158  1.50       gwr 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    159  1.50       gwr 	0,	/*10: Misc. TX/RX control bits */
    160  1.50       gwr 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    161  1.50       gwr 	14,	/*12: BAUDLO (default=9600) */
    162  1.50       gwr 	0,	/*13: BAUDHI (default=9600) */
    163  1.50       gwr 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    164  1.50       gwr 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    165  1.50       gwr };
    166   1.1   deraadt 
    167  1.50       gwr struct zschan *
    168  1.50       gwr zs_get_chan_addr(zs_unit, channel)
    169  1.50       gwr 	int zs_unit, channel;
    170  1.50       gwr {
    171  1.57        pk 	struct zsdevice	*addr;
    172  1.57        pk 	struct zschan	*zc;
    173  1.50       gwr 
    174  1.50       gwr 	if (zs_unit >= NZS)
    175  1.57        pk 		return (NULL);
    176  1.50       gwr 	addr = zsaddr[zs_unit];
    177  1.50       gwr 	if (addr == NULL)
    178  1.50       gwr 		addr = zsaddr[zs_unit] = findzs(zs_unit);
    179  1.50       gwr 	if (addr == NULL)
    180  1.57        pk 		return (NULL);
    181  1.50       gwr 	if (channel == 0) {
    182  1.50       gwr 		zc = &addr->zs_chan_a;
    183  1.50       gwr 	} else {
    184  1.50       gwr 		zc = &addr->zs_chan_b;
    185  1.50       gwr 	}
    186  1.50       gwr 	return (zc);
    187  1.50       gwr }
    188  1.34  christos 
    189  1.34  christos 
    190  1.50       gwr /****************************************************************
    191  1.50       gwr  * Autoconfig
    192  1.50       gwr  ****************************************************************/
    193   1.1   deraadt 
    194  1.50       gwr /* Definition of the driver for autoconfig. */
    195  1.57        pk static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    196  1.57        pk static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
    197  1.57        pk static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    198  1.57        pk static void zs_attach_obio __P((struct device *, struct device *, void *));
    199  1.57        pk 
    200  1.57        pk static void zs_attach __P((struct zsc_softc *, int));
    201  1.50       gwr static int  zs_print __P((void *, const char *name));
    202   1.1   deraadt 
    203  1.57        pk struct cfattach zs_mainbus_ca = {
    204  1.57        pk 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    205  1.57        pk };
    206  1.57        pk 
    207  1.57        pk struct cfattach zs_obio_ca = {
    208  1.57        pk 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
    209  1.50       gwr };
    210   1.1   deraadt 
    211  1.55   thorpej extern struct cfdriver zs_cd;
    212  1.34  christos 
    213  1.50       gwr /* Interrupt handlers. */
    214  1.50       gwr static int zshard __P((void *));
    215  1.50       gwr static int zssoft __P((void *));
    216  1.50       gwr static struct intrhand levelsoft = { zssoft };
    217  1.12   deraadt 
    218  1.50       gwr static int zs_get_speed __P((struct zs_chanstate *));
    219  1.12   deraadt 
    220  1.12   deraadt 
    221   1.1   deraadt /*
    222  1.50       gwr  * Is the zs chip present?
    223   1.1   deraadt  */
    224   1.1   deraadt static int
    225  1.57        pk zs_match_mainbus(parent, cf, aux)
    226  1.16   deraadt 	struct device *parent;
    227  1.45        pk 	struct cfdata *cf;
    228  1.45        pk 	void *aux;
    229   1.1   deraadt {
    230  1.57        pk 	struct mainbus_attach_args *ma = aux;
    231   1.1   deraadt 
    232  1.57        pk 	if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
    233  1.14   deraadt 		return (0);
    234  1.57        pk 
    235  1.57        pk 	return (getpropint(ma->ma_node, "slave", -2) == cf->cf_unit);
    236   1.1   deraadt }
    237   1.1   deraadt 
    238  1.57        pk static int
    239  1.57        pk zs_match_obio(parent, cf, aux)
    240  1.57        pk 	struct device *parent;
    241  1.57        pk 	struct cfdata *cf;
    242  1.57        pk 	void *aux;
    243  1.57        pk {
    244  1.57        pk 	union obio_attach_args *uoba = aux;
    245  1.57        pk 	struct obio4_attach_args *oba;
    246  1.57        pk 
    247  1.57        pk 	if (uoba->uoba_isobio4 == 0) {
    248  1.57        pk 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    249  1.57        pk 
    250  1.57        pk 		if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    251  1.57        pk 			return (0);
    252  1.57        pk 
    253  1.57        pk 		return (getpropint(sa->sa_node, "slave", -2) == cf->cf_unit);
    254  1.57        pk 	}
    255  1.57        pk 
    256  1.57        pk 	oba = &uoba->uoba_oba4;
    257  1.57        pk 	return (obio_bus_probe(oba->oba_bustag, oba->oba_paddr,
    258  1.57        pk 			       0, 1, NULL, NULL));
    259  1.57        pk }
    260  1.57        pk 
    261  1.57        pk static void
    262  1.57        pk zs_attach_mainbus(parent, self, aux)
    263  1.57        pk 	struct device *parent;
    264  1.57        pk 	struct device *self;
    265  1.57        pk 	void *aux;
    266  1.57        pk {
    267  1.57        pk 	struct zsc_softc *zsc = (void *) self;
    268  1.57        pk 	struct mainbus_attach_args *ma = aux;
    269  1.57        pk 	int zs_unit = zsc->zsc_dev.dv_unit;
    270  1.57        pk 
    271  1.57        pk 	zsc->zsc_bustag = ma->ma_bustag;
    272  1.57        pk 	zsc->zsc_dmatag = ma->ma_dmatag;
    273  1.57        pk 
    274  1.57        pk 	/* Use the mapping setup by the Sun PROM. */
    275  1.57        pk 	if (zsaddr[zs_unit] == NULL)
    276  1.57        pk 		zsaddr[zs_unit] = findzs(zs_unit);
    277  1.57        pk 	if ((void*)zsaddr[zs_unit] != ma->ma_promvaddr)
    278  1.57        pk 		panic("zsattach_mainbus");
    279  1.57        pk 
    280  1.57        pk 	zs_attach(zsc, ma->ma_pri);
    281  1.57        pk }
    282  1.57        pk 
    283  1.57        pk static void
    284  1.57        pk zs_attach_obio(parent, self, aux)
    285  1.57        pk 	struct device *parent;
    286  1.57        pk 	struct device *self;
    287  1.57        pk 	void *aux;
    288  1.57        pk {
    289  1.57        pk 	struct zsc_softc *zsc = (void *) self;
    290  1.57        pk 	union obio_attach_args *uoba = aux;
    291  1.57        pk 	int zs_unit = zsc->zsc_dev.dv_unit;
    292  1.57        pk 
    293  1.57        pk 	/* Use the mapping setup by the Sun PROM. */
    294  1.57        pk 	if (zsaddr[zs_unit] == NULL)
    295  1.57        pk 		zsaddr[zs_unit] = findzs(zs_unit);
    296  1.57        pk 
    297  1.57        pk 	if (uoba->uoba_isobio4 == 0) {
    298  1.57        pk 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
    299  1.57        pk 		zsc->zsc_bustag = sa->sa_bustag;
    300  1.57        pk 		zsc->zsc_dmatag = sa->sa_dmatag;
    301  1.57        pk 		zs_attach(zsc, sa->sa_pri);
    302  1.57        pk 	} else {
    303  1.57        pk 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
    304  1.57        pk 		zsc->zsc_bustag = oba->oba_bustag;
    305  1.57        pk 		zsc->zsc_dmatag = oba->oba_dmatag;
    306  1.57        pk 		zs_attach(zsc, oba->oba_pri);
    307  1.57        pk 	}
    308  1.57        pk }
    309   1.1   deraadt /*
    310   1.1   deraadt  * Attach a found zs.
    311   1.1   deraadt  *
    312   1.1   deraadt  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    313   1.1   deraadt  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    314   1.1   deraadt  */
    315   1.1   deraadt static void
    316  1.57        pk zs_attach(zsc, pri)
    317  1.57        pk 	struct zsc_softc *zsc;
    318  1.57        pk 	int pri;
    319   1.1   deraadt {
    320  1.50       gwr 	struct zsc_attach_args zsc_args;
    321  1.50       gwr 	volatile struct zschan *zc;
    322  1.50       gwr 	struct zs_chanstate *cs;
    323  1.57        pk 	int s, zs_unit, channel;
    324   1.1   deraadt 	static int didintr, prevpri;
    325   1.1   deraadt 
    326  1.57        pk 	printf(" softpri %d\n", PIL_TTY);
    327  1.50       gwr 
    328  1.50       gwr 	/*
    329  1.50       gwr 	 * Initialize software state for each channel.
    330  1.50       gwr 	 */
    331  1.57        pk 	zs_unit = zsc->zsc_dev.dv_unit;
    332  1.50       gwr 	for (channel = 0; channel < 2; channel++) {
    333  1.50       gwr 		zsc_args.channel = channel;
    334  1.50       gwr 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    335  1.50       gwr 		cs = &zsc->zsc_cs_store[channel];
    336  1.50       gwr 		zsc->zsc_cs[channel] = cs;
    337  1.54        pk 		if (zs_unit == zs_console_unit &&
    338  1.54        pk 		    channel == zs_console_channel) {
    339  1.54        pk 			zs_conschanstate = cs;
    340  1.54        pk 		}
    341  1.50       gwr 
    342  1.50       gwr 		cs->cs_channel = channel;
    343  1.50       gwr 		cs->cs_private = NULL;
    344  1.50       gwr 		cs->cs_ops = &zsops_null;
    345  1.50       gwr 		cs->cs_brg_clk = PCLK / 16;
    346  1.50       gwr 
    347  1.50       gwr 		zc = zs_get_chan_addr(zs_unit, channel);
    348  1.50       gwr 		cs->cs_reg_csr  = &zc->zc_csr;
    349  1.50       gwr 		cs->cs_reg_data = &zc->zc_data;
    350  1.50       gwr 
    351  1.50       gwr 		bcopy(zs_init_reg, cs->cs_creg, 16);
    352  1.50       gwr 		bcopy(zs_init_reg, cs->cs_preg, 16);
    353  1.50       gwr 
    354  1.50       gwr 		/* XXX: Get these from the PROM properties! */
    355  1.50       gwr 		/* XXX: See the mvme167 code.  Better. */
    356  1.50       gwr 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    357  1.50       gwr 			cs->cs_defspeed = zs_get_speed(cs);
    358  1.50       gwr 		else
    359  1.50       gwr 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    360  1.50       gwr 		cs->cs_defcflag = zs_def_cflag;
    361  1.50       gwr 
    362  1.50       gwr 		/* Make these correspond to cs_defcflag (-crtscts) */
    363  1.50       gwr 		cs->cs_rr0_dcd = ZSRR0_DCD;
    364  1.50       gwr 		cs->cs_rr0_cts = 0;
    365  1.50       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    366  1.50       gwr 		cs->cs_wr5_rts = 0;
    367  1.50       gwr 
    368  1.50       gwr 		/*
    369  1.50       gwr 		 * Clear the master interrupt enable.
    370  1.50       gwr 		 * The INTENA is common to both channels,
    371  1.50       gwr 		 * so just do it on the A channel.
    372  1.50       gwr 		 */
    373  1.50       gwr 		if (channel == 0) {
    374  1.50       gwr 			zs_write_reg(cs, 9, 0);
    375  1.50       gwr 		}
    376  1.50       gwr 
    377  1.50       gwr 		/*
    378  1.50       gwr 		 * Look for a child driver for this channel.
    379  1.50       gwr 		 * The child attach will setup the hardware.
    380  1.50       gwr 		 */
    381  1.57        pk 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
    382  1.50       gwr 			/* No sub-driver.  Just reset it. */
    383  1.50       gwr 			u_char reset = (channel == 0) ?
    384  1.50       gwr 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    385  1.56       mrg 			s = splzs();
    386  1.50       gwr 			zs_write_reg(cs,  9, reset);
    387  1.50       gwr 			splx(s);
    388  1.50       gwr 		}
    389  1.50       gwr 	}
    390  1.50       gwr 
    391  1.50       gwr 	/*
    392  1.50       gwr 	 * Now safe to install interrupt handlers.  Note the arguments
    393  1.50       gwr 	 * to the interrupt handlers aren't used.  Note, we only do this
    394  1.50       gwr 	 * once since both SCCs interrupt at the same level and vector.
    395  1.50       gwr 	 */
    396   1.1   deraadt 	if (!didintr) {
    397   1.1   deraadt 		didintr = 1;
    398   1.1   deraadt 		prevpri = pri;
    399  1.57        pk 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
    400   1.1   deraadt 		intr_establish(PIL_TTY, &levelsoft);
    401   1.1   deraadt 	} else if (pri != prevpri)
    402   1.1   deraadt 		panic("broken zs interrupt scheme");
    403  1.57        pk 
    404  1.50       gwr 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
    405   1.1   deraadt 
    406   1.1   deraadt 	/*
    407  1.50       gwr 	 * Set the master interrupt enable and interrupt vector.
    408  1.50       gwr 	 * (common to both channels, do it on A)
    409   1.1   deraadt 	 */
    410  1.50       gwr 	cs = zsc->zsc_cs[0];
    411   1.1   deraadt 	s = splhigh();
    412  1.50       gwr 	/* interrupt vector */
    413  1.50       gwr 	zs_write_reg(cs, 2, zs_init_reg[2]);
    414  1.50       gwr 	/* master interrupt control (enable) */
    415  1.50       gwr 	zs_write_reg(cs, 9, zs_init_reg[9]);
    416  1.50       gwr 	splx(s);
    417  1.50       gwr 
    418  1.50       gwr #if 0
    419  1.47        pk 	/*
    420  1.50       gwr 	 * XXX: L1A hack - We would like to be able to break into
    421  1.50       gwr 	 * the debugger during the rest of autoconfiguration, so
    422  1.50       gwr 	 * lower interrupts just enough to let zs interrupts in.
    423  1.50       gwr 	 * This is done after both zs devices are attached.
    424  1.50       gwr 	 */
    425  1.50       gwr 	if (zs_unit == 1) {
    426  1.50       gwr 		printf("zs1: enabling zs interrupts\n");
    427  1.50       gwr 		(void)splfd(); /* XXX: splzs - 1 */
    428  1.47        pk 	}
    429  1.50       gwr #endif
    430   1.1   deraadt }
    431   1.1   deraadt 
    432  1.50       gwr static int
    433  1.50       gwr zs_print(aux, name)
    434  1.50       gwr 	void *aux;
    435  1.50       gwr 	const char *name;
    436   1.1   deraadt {
    437  1.50       gwr 	struct zsc_attach_args *args = aux;
    438   1.1   deraadt 
    439  1.50       gwr 	if (name != NULL)
    440  1.50       gwr 		printf("%s: ", name);
    441   1.1   deraadt 
    442  1.50       gwr 	if (args->channel != -1)
    443  1.50       gwr 		printf(" channel %d", args->channel);
    444   1.1   deraadt 
    445  1.57        pk 	return (UNCONF);
    446   1.1   deraadt }
    447   1.1   deraadt 
    448  1.50       gwr static volatile int zssoftpending;
    449   1.1   deraadt 
    450   1.1   deraadt /*
    451  1.50       gwr  * Our ZS chips all share a common, autovectored interrupt,
    452  1.50       gwr  * so we have to look at all of them on each interrupt.
    453   1.1   deraadt  */
    454   1.1   deraadt static int
    455  1.50       gwr zshard(arg)
    456  1.50       gwr 	void *arg;
    457   1.1   deraadt {
    458  1.50       gwr 	register struct zsc_softc *zsc;
    459  1.50       gwr 	register int unit, rr3, rval, softreq;
    460   1.1   deraadt 
    461  1.50       gwr 	rval = softreq = 0;
    462  1.50       gwr 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    463  1.50       gwr 		zsc = zs_cd.cd_devs[unit];
    464  1.50       gwr 		if (zsc == NULL)
    465  1.50       gwr 			continue;
    466  1.50       gwr 		rr3 = zsc_intr_hard(zsc);
    467  1.50       gwr 		/* Count up the interrupts. */
    468  1.50       gwr 		if (rr3) {
    469  1.50       gwr 			rval |= rr3;
    470  1.50       gwr 			zsc->zsc_intrcnt.ev_count++;
    471  1.50       gwr 		}
    472  1.50       gwr 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    473  1.50       gwr 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    474  1.50       gwr 	}
    475   1.1   deraadt 
    476  1.50       gwr 	/* We are at splzs here, so no need to lock. */
    477  1.50       gwr 	if (softreq && (zssoftpending == 0)) {
    478  1.50       gwr 		zssoftpending = IE_ZSSOFT;
    479  1.50       gwr #if defined(SUN4M)
    480  1.50       gwr 		if (CPU_ISSUN4M)
    481  1.50       gwr 			raise(0, PIL_TTY);
    482  1.50       gwr 		else
    483  1.50       gwr #endif
    484  1.56       mrg 			ienab_bis(IE_ZSSOFT);
    485  1.50       gwr 	}
    486  1.50       gwr 	return (rval);
    487   1.1   deraadt }
    488   1.1   deraadt 
    489   1.1   deraadt /*
    490  1.50       gwr  * Similar scheme as for zshard (look at all of them)
    491   1.1   deraadt  */
    492  1.50       gwr static int
    493  1.50       gwr zssoft(arg)
    494  1.50       gwr 	void *arg;
    495   1.1   deraadt {
    496  1.50       gwr 	register struct zsc_softc *zsc;
    497  1.50       gwr 	register int s, unit;
    498   1.1   deraadt 
    499  1.50       gwr 	/* This is not the only ISR on this IPL. */
    500  1.50       gwr 	if (zssoftpending == 0)
    501  1.50       gwr 		return (0);
    502   1.1   deraadt 
    503  1.50       gwr 	/*
    504  1.50       gwr 	 * The soft intr. bit will be set by zshard only if
    505  1.50       gwr 	 * the variable zssoftpending is zero.  The order of
    506  1.50       gwr 	 * these next two statements prevents our clearing
    507  1.50       gwr 	 * the soft intr bit just after zshard has set it.
    508  1.50       gwr 	 */
    509  1.50       gwr 	/* ienab_bic(IE_ZSSOFT); */
    510  1.50       gwr 	zssoftpending = 0;
    511   1.1   deraadt 
    512  1.50       gwr 	/* Make sure we call the tty layer at spltty. */
    513   1.1   deraadt 	s = spltty();
    514  1.50       gwr 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    515  1.50       gwr 		zsc = zs_cd.cd_devs[unit];
    516  1.50       gwr 		if (zsc == NULL)
    517  1.50       gwr 			continue;
    518  1.56       mrg 		(void)zsc_intr_soft(zsc);
    519   1.1   deraadt 	}
    520   1.1   deraadt 	splx(s);
    521  1.50       gwr 	return (1);
    522   1.1   deraadt }
    523   1.1   deraadt 
    524  1.50       gwr 
    525   1.1   deraadt /*
    526  1.50       gwr  * Compute the current baud rate given a ZS channel.
    527   1.1   deraadt  */
    528  1.50       gwr static int
    529  1.50       gwr zs_get_speed(cs)
    530  1.50       gwr 	struct zs_chanstate *cs;
    531  1.50       gwr {
    532  1.50       gwr 	int tconst;
    533  1.50       gwr 
    534  1.50       gwr 	tconst = zs_read_reg(cs, 12);
    535  1.50       gwr 	tconst |= zs_read_reg(cs, 13) << 8;
    536  1.50       gwr 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    537   1.1   deraadt }
    538   1.1   deraadt 
    539   1.1   deraadt /*
    540  1.50       gwr  * MD functions for setting the baud rate and control modes.
    541   1.1   deraadt  */
    542   1.1   deraadt int
    543  1.50       gwr zs_set_speed(cs, bps)
    544  1.50       gwr 	struct zs_chanstate *cs;
    545  1.50       gwr 	int bps;	/* bits per second */
    546   1.1   deraadt {
    547  1.50       gwr 	int tconst, real_bps;
    548  1.50       gwr 
    549  1.50       gwr 	if (bps == 0)
    550  1.50       gwr 		return (0);
    551   1.1   deraadt 
    552  1.50       gwr #ifdef	DIAGNOSTIC
    553  1.50       gwr 	if (cs->cs_brg_clk == 0)
    554  1.50       gwr 		panic("zs_set_speed");
    555  1.50       gwr #endif
    556  1.50       gwr 
    557  1.50       gwr 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    558  1.50       gwr 	if (tconst < 0)
    559  1.50       gwr 		return (EINVAL);
    560  1.28        pk 
    561  1.50       gwr 	/* Convert back to make sure we can do it. */
    562  1.50       gwr 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    563   1.1   deraadt 
    564  1.50       gwr 	/* XXX - Allow some tolerance here? */
    565  1.50       gwr 	if (real_bps != bps)
    566  1.50       gwr 		return (EINVAL);
    567  1.28        pk 
    568  1.50       gwr 	cs->cs_preg[12] = tconst;
    569  1.50       gwr 	cs->cs_preg[13] = tconst >> 8;
    570   1.1   deraadt 
    571  1.50       gwr 	/* Caller will stuff the pending registers. */
    572  1.50       gwr 	return (0);
    573  1.28        pk }
    574  1.28        pk 
    575  1.50       gwr int
    576  1.50       gwr zs_set_modes(cs, cflag)
    577  1.50       gwr 	struct zs_chanstate *cs;
    578  1.50       gwr 	int cflag;	/* bits per second */
    579  1.28        pk {
    580  1.50       gwr 	int s;
    581  1.28        pk 
    582  1.50       gwr 	/*
    583  1.50       gwr 	 * Output hardware flow control on the chip is horrendous:
    584  1.50       gwr 	 * if carrier detect drops, the receiver is disabled, and if
    585  1.50       gwr 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    586  1.50       gwr 	 * Therefore, NEVER set the HFC bit, and instead use the
    587  1.50       gwr 	 * status interrupt to detect CTS changes.
    588  1.50       gwr 	 */
    589  1.50       gwr 	s = splzs();
    590  1.52   mycroft 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    591  1.50       gwr 		cs->cs_rr0_dcd = 0;
    592  1.52   mycroft 	else
    593  1.50       gwr 		cs->cs_rr0_dcd = ZSRR0_DCD;
    594  1.52   mycroft 	if ((cflag & CRTSCTS) != 0) {
    595  1.50       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR;
    596  1.50       gwr 		cs->cs_wr5_rts = ZSWR5_RTS;
    597  1.53   mycroft 		cs->cs_rr0_cts = ZSRR0_CTS;
    598  1.53   mycroft 	} else if ((cflag & CDTRCTS) != 0) {
    599  1.53   mycroft 		cs->cs_wr5_dtr = 0;
    600  1.53   mycroft 		cs->cs_wr5_rts = ZSWR5_DTR;
    601  1.50       gwr 		cs->cs_rr0_cts = ZSRR0_CTS;
    602  1.52   mycroft 	} else if ((cflag & MDMBUF) != 0) {
    603  1.52   mycroft 		cs->cs_wr5_dtr = 0;
    604  1.52   mycroft 		cs->cs_wr5_rts = ZSWR5_DTR;
    605  1.52   mycroft 		cs->cs_rr0_cts = ZSRR0_DCD;
    606  1.50       gwr 	} else {
    607  1.50       gwr 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    608  1.50       gwr 		cs->cs_wr5_rts = 0;
    609  1.50       gwr 		cs->cs_rr0_cts = 0;
    610  1.50       gwr 	}
    611  1.50       gwr 	splx(s);
    612  1.28        pk 
    613  1.50       gwr 	/* Caller will stuff the pending registers. */
    614  1.50       gwr 	return (0);
    615  1.38       mrg }
    616  1.28        pk 
    617   1.1   deraadt 
    618   1.1   deraadt /*
    619  1.50       gwr  * Read or write the chip with suitable delays.
    620   1.1   deraadt  */
    621  1.50       gwr 
    622  1.50       gwr u_char
    623  1.50       gwr zs_read_reg(cs, reg)
    624  1.50       gwr 	struct zs_chanstate *cs;
    625  1.50       gwr 	u_char reg;
    626   1.1   deraadt {
    627  1.50       gwr 	u_char val;
    628  1.14   deraadt 
    629  1.50       gwr 	*cs->cs_reg_csr = reg;
    630  1.50       gwr 	ZS_DELAY();
    631  1.50       gwr 	val = *cs->cs_reg_csr;
    632  1.50       gwr 	ZS_DELAY();
    633  1.57        pk 	return (val);
    634   1.1   deraadt }
    635   1.1   deraadt 
    636  1.50       gwr void
    637  1.50       gwr zs_write_reg(cs, reg, val)
    638  1.50       gwr 	struct zs_chanstate *cs;
    639  1.50       gwr 	u_char reg, val;
    640   1.1   deraadt {
    641  1.50       gwr 	*cs->cs_reg_csr = reg;
    642  1.14   deraadt 	ZS_DELAY();
    643  1.50       gwr 	*cs->cs_reg_csr = val;
    644  1.14   deraadt 	ZS_DELAY();
    645  1.50       gwr }
    646   1.1   deraadt 
    647  1.56       mrg u_char
    648  1.56       mrg zs_read_csr(cs)
    649  1.50       gwr 	struct zs_chanstate *cs;
    650  1.50       gwr {
    651  1.50       gwr 	register u_char val;
    652   1.1   deraadt 
    653  1.50       gwr 	val = *cs->cs_reg_csr;
    654  1.14   deraadt 	ZS_DELAY();
    655  1.57        pk 	return (val);
    656   1.1   deraadt }
    657   1.1   deraadt 
    658  1.50       gwr void  zs_write_csr(cs, val)
    659  1.50       gwr 	struct zs_chanstate *cs;
    660  1.50       gwr 	u_char val;
    661  1.50       gwr {
    662  1.50       gwr 	*cs->cs_reg_csr = val;
    663  1.14   deraadt 	ZS_DELAY();
    664   1.1   deraadt }
    665   1.1   deraadt 
    666  1.50       gwr u_char zs_read_data(cs)
    667  1.50       gwr 	struct zs_chanstate *cs;
    668   1.1   deraadt {
    669  1.50       gwr 	register u_char val;
    670   1.1   deraadt 
    671  1.50       gwr 	val = *cs->cs_reg_data;
    672  1.29        pk 	ZS_DELAY();
    673  1.57        pk 	return (val);
    674  1.50       gwr }
    675  1.50       gwr 
    676  1.50       gwr void  zs_write_data(cs, val)
    677  1.50       gwr 	struct zs_chanstate *cs;
    678  1.50       gwr 	u_char val;
    679  1.50       gwr {
    680  1.50       gwr 	*cs->cs_reg_data = val;
    681  1.14   deraadt 	ZS_DELAY();
    682   1.1   deraadt }
    683   1.1   deraadt 
    684  1.50       gwr /****************************************************************
    685  1.50       gwr  * Console support functions (Sun specific!)
    686  1.50       gwr  * Note: this code is allowed to know about the layout of
    687  1.50       gwr  * the chip registers, and uses that to keep things simple.
    688  1.50       gwr  * XXX - I think I like the mvme167 code better. -gwr
    689  1.50       gwr  ****************************************************************/
    690  1.50       gwr 
    691  1.50       gwr extern void Debugger __P((void));
    692  1.50       gwr void *zs_conschan;
    693  1.50       gwr 
    694  1.50       gwr /*
    695  1.50       gwr  * Handle user request to enter kernel debugger.
    696  1.50       gwr  */
    697  1.34  christos void
    698  1.50       gwr zs_abort(cs)
    699  1.50       gwr 	struct zs_chanstate *cs;
    700   1.1   deraadt {
    701  1.50       gwr 	register volatile struct zschan *zc = zs_conschan;
    702  1.50       gwr 	int rr0;
    703  1.50       gwr 
    704  1.50       gwr 	/* Wait for end of break to avoid PROM abort. */
    705  1.50       gwr 	/* XXX - Limit the wait? */
    706  1.50       gwr 	do {
    707  1.50       gwr 		rr0 = zc->zc_csr;
    708  1.50       gwr 		ZS_DELAY();
    709  1.50       gwr 	} while (rr0 & ZSRR0_BREAK);
    710   1.1   deraadt 
    711  1.49        pk #if defined(KGDB)
    712  1.50       gwr 	zskgdb(cs);
    713  1.49        pk #elif defined(DDB)
    714   1.5        pk 	Debugger();
    715   1.5        pk #else
    716  1.44  christos 	printf("stopping on keyboard abort\n");
    717   1.1   deraadt 	callrom();
    718   1.5        pk #endif
    719   1.1   deraadt }
    720   1.1   deraadt 
    721   1.1   deraadt /*
    722  1.50       gwr  * Polled input char.
    723   1.1   deraadt  */
    724  1.50       gwr int
    725  1.50       gwr zs_getc(arg)
    726  1.50       gwr 	void *arg;
    727   1.1   deraadt {
    728  1.50       gwr 	register volatile struct zschan *zc = arg;
    729  1.50       gwr 	register int s, c, rr0;
    730   1.1   deraadt 
    731  1.50       gwr 	s = splhigh();
    732  1.50       gwr 	/* Wait for a character to arrive. */
    733  1.50       gwr 	do {
    734  1.50       gwr 		rr0 = zc->zc_csr;
    735  1.50       gwr 		ZS_DELAY();
    736  1.50       gwr 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    737   1.1   deraadt 
    738  1.50       gwr 	c = zc->zc_data;
    739  1.50       gwr 	ZS_DELAY();
    740  1.50       gwr 	splx(s);
    741   1.1   deraadt 
    742  1.50       gwr 	/*
    743  1.50       gwr 	 * This is used by the kd driver to read scan codes,
    744  1.50       gwr 	 * so don't translate '\r' ==> '\n' here...
    745  1.50       gwr 	 */
    746  1.50       gwr 	return (c);
    747   1.1   deraadt }
    748   1.1   deraadt 
    749   1.1   deraadt /*
    750  1.50       gwr  * Polled output char.
    751   1.1   deraadt  */
    752  1.50       gwr void
    753  1.50       gwr zs_putc(arg, c)
    754  1.16   deraadt 	void *arg;
    755  1.50       gwr 	int c;
    756   1.1   deraadt {
    757  1.50       gwr 	register volatile struct zschan *zc = arg;
    758  1.50       gwr 	register int s, rr0;
    759   1.1   deraadt 
    760  1.50       gwr 	s = splhigh();
    761  1.50       gwr 	/* Wait for transmitter to become ready. */
    762  1.50       gwr 	do {
    763  1.50       gwr 		rr0 = zc->zc_csr;
    764  1.50       gwr 		ZS_DELAY();
    765  1.50       gwr 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    766  1.21   deraadt 
    767  1.54        pk 	/*
    768  1.54        pk 	 * If the transmitter was busy doing regular tty I/O (ZSWR1_TIE on),
    769  1.54        pk 	 * defer our output until the transmit interrupt runs. We still
    770  1.54        pk 	 * sync with TX_READY so we can get by with a single-char "queue".
    771  1.54        pk 	 */
    772  1.54        pk 	if (zs_conschanstate && (zs_conschanstate->cs_preg[1] & ZSWR1_TIE)) {
    773  1.54        pk 		/*
    774  1.54        pk 		 * If a previous held character has not yet gone out, we can
    775  1.54        pk 		 * send it now;  zsxint() will field the interrupt for our
    776  1.54        pk 		 * char, but doesn't care. We're running at sufficiently
    777  1.54        pk 		 * high spl for this to work.
    778  1.54        pk 		 */
    779  1.54        pk 		if (zs_conschanstate->cs_heldchar != 0)
    780  1.54        pk 			zc->zc_data = zs_conschanstate->cs_heldchar;
    781  1.54        pk 		zs_conschanstate->cs_heldchar = c;
    782  1.54        pk 		ZS_DELAY();
    783  1.54        pk 		splx(s);
    784  1.54        pk 		return;
    785  1.54        pk 	}
    786  1.54        pk 
    787  1.50       gwr 	zc->zc_data = c;
    788  1.50       gwr 	ZS_DELAY();
    789  1.50       gwr 	splx(s);
    790  1.50       gwr }
    791  1.21   deraadt 
    792  1.50       gwr /*****************************************************************/
    793  1.21   deraadt 
    794  1.50       gwr static void zscninit __P((struct consdev *));
    795  1.50       gwr static int  zscngetc __P((dev_t));
    796  1.50       gwr static void zscnputc __P((dev_t, int));
    797   1.1   deraadt 
    798   1.1   deraadt /*
    799  1.50       gwr  * Console table shared by ttya, ttyb
    800   1.1   deraadt  */
    801  1.50       gwr struct consdev consdev_tty = {
    802  1.50       gwr 	nullcnprobe,
    803  1.50       gwr 	zscninit,
    804  1.50       gwr 	zscngetc,
    805  1.50       gwr 	zscnputc,
    806  1.50       gwr 	nullcnpollc,
    807  1.50       gwr };
    808  1.50       gwr 
    809   1.1   deraadt static void
    810  1.50       gwr zscninit(cn)
    811  1.50       gwr 	struct consdev *cn;
    812   1.1   deraadt {
    813   1.1   deraadt }
    814   1.1   deraadt 
    815   1.1   deraadt /*
    816  1.50       gwr  * Polled console input putchar.
    817   1.1   deraadt  */
    818  1.50       gwr static int
    819  1.50       gwr zscngetc(dev)
    820  1.50       gwr 	dev_t dev;
    821  1.50       gwr {
    822  1.50       gwr 	return (zs_getc(zs_conschan));
    823   1.1   deraadt }
    824   1.1   deraadt 
    825   1.1   deraadt /*
    826  1.50       gwr  * Polled console output putchar.
    827   1.1   deraadt  */
    828  1.50       gwr static void
    829  1.50       gwr zscnputc(dev, c)
    830  1.50       gwr 	dev_t dev;
    831  1.50       gwr 	int c;
    832  1.50       gwr {
    833  1.50       gwr 	zs_putc(zs_conschan, c);
    834  1.50       gwr }
    835   1.1   deraadt 
    836  1.50       gwr /*****************************************************************/
    837   1.1   deraadt 
    838  1.50       gwr static void prom_cninit __P((struct consdev *));
    839  1.50       gwr static int  prom_cngetc __P((dev_t));
    840  1.50       gwr static void prom_cnputc __P((dev_t, int));
    841   1.1   deraadt 
    842  1.50       gwr /*
    843  1.50       gwr  * The console is set to this one initially,
    844  1.50       gwr  * which lets us use the PROM until consinit()
    845  1.50       gwr  * is called to select a real console.
    846  1.50       gwr  */
    847  1.50       gwr struct consdev consdev_prom = {
    848  1.50       gwr 	nullcnprobe,
    849  1.50       gwr 	prom_cninit,
    850  1.50       gwr 	prom_cngetc,
    851  1.50       gwr 	prom_cnputc,
    852  1.50       gwr 	nullcnpollc,
    853  1.50       gwr };
    854   1.1   deraadt 
    855   1.1   deraadt /*
    856  1.50       gwr  * The console table pointer is statically initialized
    857  1.50       gwr  * to point to the PROM (output only) table, so that
    858  1.50       gwr  * early calls to printf will work.
    859   1.1   deraadt  */
    860  1.50       gwr struct consdev *cn_tab = &consdev_prom;
    861  1.50       gwr 
    862  1.50       gwr void
    863  1.50       gwr nullcnprobe(cn)
    864  1.50       gwr 	struct consdev *cn;
    865   1.1   deraadt {
    866   1.1   deraadt }
    867   1.1   deraadt 
    868   1.1   deraadt static void
    869  1.50       gwr prom_cninit(cn)
    870  1.50       gwr 	struct consdev *cn;
    871   1.1   deraadt {
    872   1.1   deraadt }
    873   1.1   deraadt 
    874   1.1   deraadt /*
    875  1.50       gwr  * PROM console input putchar.
    876  1.50       gwr  * (dummy - this is output only)
    877   1.1   deraadt  */
    878   1.1   deraadt static int
    879  1.50       gwr prom_cngetc(dev)
    880  1.50       gwr 	dev_t dev;
    881   1.1   deraadt {
    882  1.50       gwr 	return (0);
    883   1.1   deraadt }
    884   1.1   deraadt 
    885   1.1   deraadt /*
    886  1.50       gwr  * PROM console output putchar.
    887   1.1   deraadt  */
    888   1.1   deraadt static void
    889  1.50       gwr prom_cnputc(dev, c)
    890  1.50       gwr 	dev_t dev;
    891  1.16   deraadt 	int c;
    892   1.1   deraadt {
    893  1.50       gwr 	char c0 = (c & 0x7f);
    894   1.1   deraadt 
    895  1.50       gwr 	if (promvec->pv_romvec_vers > 2)
    896  1.50       gwr 		(*promvec->pv_v2devops.v2_write)
    897  1.50       gwr 			(*promvec->pv_v2bootargs.v2_fd1, &c0, 1);
    898  1.50       gwr 	else
    899  1.50       gwr 		(*promvec->pv_putchar)(c);
    900   1.1   deraadt }
    901   1.1   deraadt 
    902  1.50       gwr /*****************************************************************/
    903  1.50       gwr 
    904  1.50       gwr extern struct consdev consdev_kd;
    905  1.50       gwr 
    906  1.50       gwr static char *prom_inSrc_name[] = {
    907  1.50       gwr 	"keyboard/display",
    908  1.50       gwr 	"ttya", "ttyb",
    909  1.50       gwr 	"ttyc", "ttyd" };
    910  1.50       gwr 
    911   1.1   deraadt /*
    912  1.50       gwr  * This function replaces sys/dev/cninit.c
    913  1.50       gwr  * Determine which device is the console using
    914  1.50       gwr  * the PROM "input source" and "output sink".
    915   1.1   deraadt  */
    916   1.1   deraadt void
    917  1.50       gwr consinit()
    918   1.1   deraadt {
    919  1.50       gwr 	struct zschan *zc;
    920  1.50       gwr 	struct consdev *cn;
    921  1.50       gwr 	int channel, zs_unit, zstty_unit;
    922  1.50       gwr 	int inSource, outSink;
    923  1.50       gwr 
    924  1.50       gwr 	if (promvec->pv_romvec_vers > 2) {
    925  1.50       gwr 		/* We need to probe the PROM device tree */
    926  1.50       gwr 		register int node,fd;
    927  1.50       gwr 		char buffer[128];
    928  1.50       gwr 		register struct nodeops *no;
    929  1.50       gwr 		register struct v2devops *op;
    930  1.50       gwr 		register char *cp;
    931  1.50       gwr 		extern int fbnode;
    932  1.50       gwr 
    933  1.50       gwr 		inSource = outSink = -1;
    934  1.50       gwr 		no = promvec->pv_nodeops;
    935  1.50       gwr 		op = &promvec->pv_v2devops;
    936  1.50       gwr 
    937  1.50       gwr 		node = findroot();
    938  1.50       gwr 		if (no->no_proplen(node, "stdin-path") >= sizeof(buffer)) {
    939  1.50       gwr 			printf("consinit: increase buffer size and recompile\n");
    940  1.50       gwr 			goto setup_output;
    941  1.50       gwr 		}
    942  1.50       gwr 		/* XXX: fix above */
    943  1.50       gwr 
    944  1.50       gwr 		no->no_getprop(node, "stdin-path",buffer);
    945  1.50       gwr 
    946  1.50       gwr 		/*
    947  1.50       gwr 		 * Open an "instance" of this device.
    948  1.50       gwr 		 * You'd think it would be appropriate to call v2_close()
    949  1.50       gwr 		 * on the handle when we're done with it. But that seems
    950  1.50       gwr 		 * to cause the device to shut down somehow; for the moment,
    951  1.50       gwr 		 * we simply leave it open...
    952  1.50       gwr 		 */
    953  1.50       gwr 		if ((fd = op->v2_open(buffer)) == 0 ||
    954  1.50       gwr 		     (node = op->v2_fd_phandle(fd)) == 0) {
    955  1.50       gwr 			printf("consinit: bogus stdin path %s.\n",buffer);
    956  1.50       gwr 			goto setup_output;
    957  1.50       gwr 		}
    958  1.50       gwr 		if (no->no_proplen(node,"keyboard") >= 0) {
    959  1.50       gwr 			inSource = PROMDEV_KBD;
    960  1.50       gwr 			goto setup_output;
    961  1.50       gwr 		}
    962  1.50       gwr 		if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
    963  1.50       gwr 			/* not a serial, not keyboard. what is it?!? */
    964  1.50       gwr 			inSource = -1;
    965  1.50       gwr 			goto setup_output;
    966  1.50       gwr 		}
    967  1.50       gwr 		/*
    968  1.50       gwr 		 * At this point we assume the device path is in the form
    969  1.50       gwr 		 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
    970  1.50       gwr 		 * If it isn't, we defer to the ROM
    971  1.50       gwr 		 */
    972  1.50       gwr 		cp = buffer;
    973  1.50       gwr 		while (*cp)
    974  1.50       gwr 		    cp++;
    975  1.50       gwr 		cp -= 2;
    976  1.50       gwr #ifdef DEBUG
    977  1.50       gwr 		if (cp < buffer)
    978  1.50       gwr 		    panic("consinit: bad stdin path %s",buffer);
    979  1.50       gwr #endif
    980  1.50       gwr 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
    981  1.50       gwr 		if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
    982  1.50       gwr 		    inSource = PROMDEV_TTYA + (cp[1] - 'a');
    983  1.50       gwr 		/* else use rom */
    984  1.50       gwr setup_output:
    985  1.50       gwr 		node = findroot();
    986  1.50       gwr 		if (no->no_proplen(node, "stdout-path") >= sizeof(buffer)) {
    987  1.50       gwr 			printf("consinit: increase buffer size and recompile\n");
    988  1.50       gwr 			goto setup_console;
    989  1.50       gwr 		}
    990  1.50       gwr 		/* XXX: fix above */
    991  1.50       gwr 
    992  1.50       gwr 		no->no_getprop(node, "stdout-path", buffer);
    993  1.50       gwr 
    994  1.50       gwr 		if ((fd = op->v2_open(buffer)) == 0 ||
    995  1.50       gwr 		     (node = op->v2_fd_phandle(fd)) == 0) {
    996  1.50       gwr 			printf("consinit: bogus stdout path %s.\n",buffer);
    997  1.50       gwr 			goto setup_output;
    998  1.50       gwr 		}
    999  1.50       gwr 		if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
   1000  1.50       gwr 			/* frame buffer output */
   1001  1.50       gwr 			outSink = PROMDEV_SCREEN;
   1002  1.50       gwr 			fbnode = node;
   1003  1.50       gwr 		} else if (strcmp(getpropstring(node,"device_type"), "serial")
   1004  1.50       gwr 			   != 0) {
   1005  1.50       gwr 			/* not screen, not serial. Whatzit? */
   1006  1.50       gwr 			outSink = -1;
   1007  1.50       gwr 		} else { /* serial console. which? */
   1008  1.50       gwr 			/*
   1009  1.50       gwr 			 * At this point we assume the device path is in the
   1010  1.50       gwr 			 * form:
   1011  1.50       gwr 			 * ....device@x,y:a for ttya, etc.
   1012  1.50       gwr 			 * If it isn't, we defer to the ROM
   1013  1.50       gwr 			 */
   1014  1.50       gwr 			cp = buffer;
   1015  1.50       gwr 			while (*cp)
   1016  1.50       gwr 			    cp++;
   1017  1.50       gwr 			cp -= 2;
   1018  1.50       gwr #ifdef DEBUG
   1019  1.50       gwr 			if (cp < buffer)
   1020  1.50       gwr 				panic("consinit: bad stdout path %s",buffer);
   1021  1.50       gwr #endif
   1022  1.50       gwr 			/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
   1023  1.50       gwr 			if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
   1024  1.50       gwr 			    outSink = PROMDEV_TTYA + (cp[1] - 'a');
   1025  1.50       gwr 			else outSink = -1;
   1026  1.50       gwr 		}
   1027  1.50       gwr 	} else {
   1028  1.50       gwr 		inSource = *promvec->pv_stdin;
   1029  1.50       gwr 		outSink  = *promvec->pv_stdout;
   1030  1.50       gwr 	}
   1031  1.50       gwr 
   1032  1.50       gwr setup_console:
   1033  1.50       gwr 
   1034  1.50       gwr 	if (inSource != outSink) {
   1035  1.50       gwr 		printf("cninit: mismatched PROM output selector\n");
   1036  1.50       gwr 	}
   1037  1.50       gwr 
   1038  1.50       gwr 	switch (inSource) {
   1039  1.50       gwr 	default:
   1040  1.50       gwr 		printf("cninit: invalid inSource=%d\n", inSource);
   1041  1.50       gwr 		callrom();
   1042  1.50       gwr 		inSource = PROMDEV_KBD;
   1043  1.50       gwr 		/* fall through */
   1044  1.50       gwr 
   1045  1.50       gwr 	case 0:	/* keyboard/display */
   1046  1.50       gwr #if NKBD > 0
   1047  1.51       gwr 		zs_unit = 1;	/* XXX - config info! */
   1048  1.50       gwr 		channel = 0;
   1049  1.50       gwr 		cn = &consdev_kd;
   1050  1.50       gwr 		/* Set cn_dev, cn_pri in kd.c */
   1051  1.50       gwr 		break;
   1052  1.50       gwr #else	/* NKBD */
   1053  1.50       gwr 		printf("cninit: kdb/display not configured\n");
   1054  1.50       gwr 		callrom();
   1055  1.50       gwr 		inSource = PROMDEV_TTYA;
   1056  1.50       gwr 		/* fall through */
   1057  1.50       gwr #endif	/* NKBD */
   1058  1.50       gwr 
   1059  1.50       gwr 	case PROMDEV_TTYA:
   1060  1.50       gwr 	case PROMDEV_TTYB:
   1061  1.50       gwr 		zstty_unit = inSource - PROMDEV_TTYA;
   1062  1.51       gwr 		zs_unit = 0;	/* XXX - config info! */
   1063  1.50       gwr 		channel = zstty_unit & 1;
   1064  1.50       gwr 		cn = &consdev_tty;
   1065  1.50       gwr 		cn->cn_dev = makedev(zs_major, zstty_unit);
   1066  1.50       gwr 		cn->cn_pri = CN_REMOTE;
   1067  1.54        pk 		zs_console_unit = zs_unit;
   1068  1.54        pk 		zs_console_channel = channel;
   1069  1.50       gwr 		break;
   1070  1.50       gwr 
   1071  1.50       gwr 	}
   1072  1.50       gwr 	/* Now that inSource has been validated, print it. */
   1073  1.50       gwr 	printf("console is %s\n", prom_inSrc_name[inSource]);
   1074   1.1   deraadt 
   1075  1.50       gwr 	zc = zs_get_chan_addr(zs_unit, channel);
   1076  1.50       gwr 	if (zc == NULL) {
   1077  1.50       gwr 		printf("cninit: zs not mapped.\n");
   1078   1.1   deraadt 		return;
   1079   1.1   deraadt 	}
   1080  1.50       gwr 	zs_conschan = zc;
   1081  1.50       gwr 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
   1082  1.50       gwr 	cn_tab = cn;
   1083  1.50       gwr 	(*cn->cn_init)(cn);
   1084  1.50       gwr #ifdef	KGDB
   1085  1.50       gwr 	zs_kgdb_init();
   1086  1.50       gwr #endif
   1087   1.1   deraadt }
   1088