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