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