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