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