Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.28.2.1
      1  1.28.2.1   nathanw /*	$NetBSD: zs.c,v 1.28.2.1 2001/06/21 19:35:48 nathanw 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.28.2.1   nathanw #include "opt_kgdb.h"
     49       1.1       eeh 
     50       1.1       eeh #include <sys/param.h>
     51       1.1       eeh #include <sys/systm.h>
     52       1.1       eeh #include <sys/conf.h>
     53       1.1       eeh #include <sys/device.h>
     54       1.1       eeh #include <sys/file.h>
     55       1.1       eeh #include <sys/ioctl.h>
     56       1.1       eeh #include <sys/kernel.h>
     57       1.1       eeh #include <sys/proc.h>
     58       1.1       eeh #include <sys/tty.h>
     59       1.1       eeh #include <sys/time.h>
     60       1.1       eeh #include <sys/syslog.h>
     61       1.1       eeh 
     62       1.1       eeh #include <machine/autoconf.h>
     63       1.1       eeh #include <machine/openfirm.h>
     64       1.1       eeh #include <machine/bsd_openprom.h>
     65       1.1       eeh #include <machine/conf.h>
     66       1.1       eeh #include <machine/cpu.h>
     67       1.1       eeh #include <machine/eeprom.h>
     68       1.1       eeh #include <machine/psl.h>
     69       1.1       eeh #include <machine/z8530var.h>
     70       1.1       eeh 
     71       1.1       eeh #include <dev/cons.h>
     72       1.1       eeh #include <dev/ic/z8530reg.h>
     73      1.26       eeh #include <dev/sun/kbd_ms_ttyvar.h>
     74      1.16       mrg #include <ddb/db_output.h>
     75       1.1       eeh 
     76       1.1       eeh #include <sparc64/dev/cons.h>
     77       1.1       eeh 
     78       1.1       eeh #include "kbd.h"	/* NKBD */
     79      1.26       eeh #include "ms.h"		/* NMS */
     80       1.1       eeh #include "zs.h" 	/* NZS */
     81       1.1       eeh 
     82       1.1       eeh /* Make life easier for the initialized arrays here. */
     83       1.1       eeh #if NZS < 3
     84       1.1       eeh #undef  NZS
     85       1.1       eeh #define NZS 3
     86       1.1       eeh #endif
     87       1.1       eeh 
     88       1.1       eeh /*
     89       1.1       eeh  * Some warts needed by z8530tty.c -
     90       1.1       eeh  * The default parity REALLY needs to be the same as the PROM uses,
     91       1.1       eeh  * or you can not see messages done with printf during boot-up...
     92       1.1       eeh  */
     93       1.1       eeh int zs_def_cflag = (CREAD | CS8 | HUPCL);
     94       1.1       eeh int zs_major = 12;
     95       1.1       eeh 
     96       1.1       eeh /*
     97       1.1       eeh  * The Sun provides a 4.9152 MHz clock to the ZS chips.
     98       1.1       eeh  */
     99       1.1       eeh #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
    100       1.1       eeh 
    101      1.10       eeh #define	ZS_DELAY()
    102       1.1       eeh 
    103       1.1       eeh /* The layout of this is hardware-dependent (padding, order). */
    104       1.1       eeh struct zschan {
    105       1.1       eeh 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    106       1.1       eeh 	u_char		zc_xxx0;
    107       1.1       eeh 	volatile u_char	zc_data;	/* data */
    108       1.1       eeh 	u_char		zc_xxx1;
    109       1.1       eeh };
    110       1.1       eeh struct zsdevice {
    111       1.1       eeh 	/* Yes, they are backwards. */
    112       1.1       eeh 	struct	zschan zs_chan_b;
    113       1.1       eeh 	struct	zschan zs_chan_a;
    114       1.1       eeh };
    115       1.1       eeh 
    116      1.20       eeh /* ZS channel used as the console device (if any) */
    117      1.20       eeh void *zs_conschan_get, *zs_conschan_put;
    118      1.20       eeh 
    119       1.1       eeh /* Saved PROM mappings */
    120       1.1       eeh static struct zsdevice *zsaddr[NZS];
    121       1.1       eeh 
    122       1.1       eeh static u_char zs_init_reg[16] = {
    123       1.1       eeh 	0,	/* 0: CMD (reset, etc.) */
    124       1.1       eeh 	0,	/* 1: No interrupts yet. */
    125       1.1       eeh 	0,	/* 2: IVECT */
    126       1.1       eeh 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    127       1.1       eeh 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    128       1.1       eeh 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    129       1.1       eeh 	0,	/* 6: TXSYNC/SYNCLO */
    130       1.1       eeh 	0,	/* 7: RXSYNC/SYNCHI */
    131       1.1       eeh 	0,	/* 8: alias for data port */
    132       1.1       eeh 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    133       1.1       eeh 	0,	/*10: Misc. TX/RX control bits */
    134       1.1       eeh 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    135       1.7   mycroft 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    136       1.7   mycroft 	0,			/*13: BAUDHI (default=9600) */
    137       1.1       eeh 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    138       1.6   mycroft 	ZSWR15_BREAK_IE,
    139       1.1       eeh };
    140       1.1       eeh 
    141      1.20       eeh /* Console ops */
    142      1.20       eeh static int  zscngetc __P((dev_t));
    143      1.20       eeh static void zscnputc __P((dev_t, int));
    144      1.20       eeh static void zscnpollc __P((dev_t, int));
    145      1.20       eeh 
    146      1.20       eeh struct consdev zs_consdev = {
    147      1.20       eeh 	NULL,
    148      1.20       eeh 	NULL,
    149      1.20       eeh 	zscngetc,
    150      1.20       eeh 	zscnputc,
    151      1.20       eeh 	zscnpollc,
    152      1.20       eeh 	NULL,
    153      1.20       eeh };
    154       1.1       eeh 
    155       1.1       eeh 
    156       1.1       eeh /****************************************************************
    157       1.1       eeh  * Autoconfig
    158       1.1       eeh  ****************************************************************/
    159       1.1       eeh 
    160       1.1       eeh /* Definition of the driver for autoconfig. */
    161       1.1       eeh static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
    162       1.1       eeh static void zs_attach_mainbus __P((struct device *, struct device *, void *));
    163       1.1       eeh 
    164      1.20       eeh static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
    165       1.1       eeh static int  zs_print __P((void *, const char *name));
    166       1.1       eeh 
    167      1.20       eeh /* Do we really need this ? */
    168       1.1       eeh struct cfattach zs_ca = {
    169      1.20       eeh 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    170       1.1       eeh };
    171       1.1       eeh 
    172       1.1       eeh struct cfattach zs_mainbus_ca = {
    173       1.1       eeh 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
    174       1.1       eeh };
    175       1.1       eeh 
    176       1.1       eeh extern struct cfdriver zs_cd;
    177      1.12       eeh extern int stdinnode;
    178      1.12       eeh extern int fbnode;
    179       1.1       eeh 
    180       1.1       eeh /* Interrupt handlers. */
    181      1.24       eeh int zscheckintr __P((void *));
    182       1.1       eeh static int zshard __P((void *));
    183      1.28      fvdl static void zssoft __P((void *));
    184       1.1       eeh 
    185       1.1       eeh static int zs_get_speed __P((struct zs_chanstate *));
    186       1.1       eeh 
    187      1.20       eeh /* Console device support */
    188      1.20       eeh static int zs_console_flags __P((int, int, int));
    189      1.20       eeh 
    190      1.20       eeh /* Power management hooks */
    191      1.20       eeh int  zs_enable __P((struct zs_chanstate *));
    192      1.20       eeh void zs_disable __P((struct zs_chanstate *));
    193       1.1       eeh 
    194       1.1       eeh /*
    195       1.1       eeh  * Is the zs chip present?
    196       1.1       eeh  */
    197       1.1       eeh static int
    198       1.1       eeh zs_match_mainbus(parent, cf, aux)
    199       1.1       eeh 	struct device *parent;
    200       1.1       eeh 	struct cfdata *cf;
    201       1.1       eeh 	void *aux;
    202       1.1       eeh {
    203       1.1       eeh 	struct sbus_attach_args *sa = aux;
    204       1.1       eeh 
    205       1.1       eeh 	if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
    206       1.1       eeh 		return (0);
    207       1.1       eeh 
    208      1.20       eeh 	return (1);
    209       1.1       eeh }
    210       1.1       eeh 
    211       1.1       eeh static void
    212       1.1       eeh zs_attach_mainbus(parent, self, aux)
    213       1.1       eeh 	struct device *parent;
    214       1.1       eeh 	struct device *self;
    215       1.1       eeh 	void *aux;
    216       1.1       eeh {
    217       1.1       eeh 	struct zsc_softc *zsc = (void *) self;
    218       1.1       eeh 	struct sbus_attach_args *sa = aux;
    219       1.1       eeh 	int zs_unit = zsc->zsc_dev.dv_unit;
    220       1.1       eeh 
    221      1.20       eeh 	if (sa->sa_nintr == 0) {
    222      1.20       eeh 		printf(" no interrupt lines\n");
    223      1.20       eeh 		return;
    224      1.20       eeh 	}
    225       1.1       eeh 
    226       1.1       eeh 	/* Use the mapping setup by the Sun PROM. */
    227      1.10       eeh 	if (zsaddr[zs_unit] == NULL) {
    228      1.20       eeh 		/* Only map registers once. */
    229      1.10       eeh 		if (sa->sa_npromvaddrs) {
    230      1.10       eeh 			/*
    231      1.10       eeh 			 * We're converting from a 32-bit pointer to a 64-bit
    232      1.10       eeh 			 * pointer.  Since the 32-bit entity is negative, but
    233      1.10       eeh 			 * the kernel is still mapped into the lower 4GB
    234      1.10       eeh 			 * range, this needs to be zero-extended.
    235      1.10       eeh 			 *
    236      1.10       eeh 			 * XXXXX If we map the kernel and devices into the
    237      1.10       eeh 			 * high 4GB range, this needs to be changed to
    238      1.10       eeh 			 * sign-extend the address.
    239      1.10       eeh 			 */
    240      1.10       eeh 			zsaddr[zs_unit] =
    241      1.10       eeh 				(struct zsdevice *)
    242      1.20       eeh 				(uintptr_t)sa->sa_promvaddrs[0];
    243      1.10       eeh 		} else {
    244      1.10       eeh 			bus_space_handle_t kvaddr;
    245      1.10       eeh 
    246      1.10       eeh 			if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    247      1.10       eeh 					 sa->sa_offset,
    248      1.10       eeh 					 sa->sa_size,
    249      1.10       eeh 					 BUS_SPACE_MAP_LINEAR,
    250      1.10       eeh 					 0, &kvaddr) != 0) {
    251      1.10       eeh 				printf("%s @ sbus: cannot map registers\n",
    252      1.10       eeh 				       self->dv_xname);
    253      1.10       eeh 				return;
    254      1.10       eeh 			}
    255      1.11       eeh 			zsaddr[zs_unit] = (struct zsdevice *)
    256      1.20       eeh 				(uintptr_t)kvaddr;
    257      1.10       eeh 		}
    258      1.10       eeh 	}
    259      1.20       eeh 	zsc->zsc_bustag = sa->sa_bustag;
    260      1.20       eeh 	zsc->zsc_dmatag = sa->sa_dmatag;
    261      1.20       eeh 	zsc->zsc_promunit = getpropint(sa->sa_node, "slave", -2);
    262      1.20       eeh 	zsc->zsc_node = sa->sa_node;
    263      1.20       eeh 	zs_attach(zsc, zsaddr[zs_unit], sa->sa_pri);
    264       1.1       eeh }
    265       1.1       eeh 
    266       1.1       eeh /*
    267       1.1       eeh  * Attach a found zs.
    268       1.1       eeh  *
    269       1.1       eeh  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    270       1.1       eeh  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    271       1.1       eeh  */
    272       1.1       eeh static void
    273      1.20       eeh zs_attach(zsc, zsd, pri)
    274       1.1       eeh 	struct zsc_softc *zsc;
    275      1.20       eeh 	struct zsdevice *zsd;
    276       1.1       eeh 	int pri;
    277       1.1       eeh {
    278       1.1       eeh 	struct zsc_attach_args zsc_args;
    279       1.1       eeh 	struct zs_chanstate *cs;
    280      1.24       eeh 	int s, channel, softpri = PIL_TTY;
    281      1.20       eeh 
    282      1.20       eeh 	if (zsd == NULL) {
    283      1.20       eeh 		printf("configuration incomplete\n");
    284      1.20       eeh 		return;
    285      1.20       eeh 	}
    286       1.1       eeh 
    287      1.24       eeh 	printf(" softpri %d\n", softpri);
    288       1.1       eeh 
    289       1.1       eeh 	/*
    290       1.1       eeh 	 * Initialize software state for each channel.
    291       1.1       eeh 	 */
    292       1.1       eeh 	for (channel = 0; channel < 2; channel++) {
    293      1.20       eeh 		struct zschan *zc;
    294      1.26       eeh 		struct device *child;
    295      1.26       eeh 		extern struct cfdriver zstty_cd; /* in ioconf.c */
    296      1.20       eeh 
    297       1.1       eeh 		zsc_args.channel = channel;
    298       1.1       eeh 		cs = &zsc->zsc_cs_store[channel];
    299       1.1       eeh 		zsc->zsc_cs[channel] = cs;
    300       1.1       eeh 
    301       1.1       eeh 		cs->cs_channel = channel;
    302       1.1       eeh 		cs->cs_private = NULL;
    303       1.1       eeh 		cs->cs_ops = &zsops_null;
    304       1.1       eeh 		cs->cs_brg_clk = PCLK / 16;
    305       1.1       eeh 
    306      1.20       eeh 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    307      1.20       eeh 
    308      1.26       eeh 		zsc_args.consdev = NULL;
    309      1.20       eeh 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
    310      1.20       eeh 						    zsc->zsc_node,
    311      1.20       eeh 						    channel);
    312      1.20       eeh 
    313      1.20       eeh 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
    314      1.20       eeh 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
    315      1.20       eeh 			zsc_args.consdev = &zs_consdev;
    316      1.11       eeh 		}
    317      1.20       eeh 
    318      1.20       eeh 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    319      1.20       eeh 			zs_conschan_get = zc;
    320      1.20       eeh 		}
    321      1.20       eeh 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    322      1.20       eeh 			zs_conschan_put = zc;
    323      1.20       eeh 		}
    324      1.20       eeh 		/* Childs need to set cn_dev, etc */
    325      1.20       eeh 
    326       1.1       eeh 		cs->cs_reg_csr  = &zc->zc_csr;
    327       1.1       eeh 		cs->cs_reg_data = &zc->zc_data;
    328       1.1       eeh 
    329       1.1       eeh 		bcopy(zs_init_reg, cs->cs_creg, 16);
    330       1.1       eeh 		bcopy(zs_init_reg, cs->cs_preg, 16);
    331       1.1       eeh 
    332      1.20       eeh 		/* XXX: Consult PROM properties for this?! */
    333      1.20       eeh 		cs->cs_defspeed = zs_get_speed(cs);
    334       1.1       eeh 		cs->cs_defcflag = zs_def_cflag;
    335       1.1       eeh 
    336       1.1       eeh 		/* Make these correspond to cs_defcflag (-crtscts) */
    337       1.1       eeh 		cs->cs_rr0_dcd = ZSRR0_DCD;
    338       1.1       eeh 		cs->cs_rr0_cts = 0;
    339       1.1       eeh 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    340       1.1       eeh 		cs->cs_wr5_rts = 0;
    341       1.1       eeh 
    342       1.1       eeh 		/*
    343       1.1       eeh 		 * Clear the master interrupt enable.
    344       1.1       eeh 		 * The INTENA is common to both channels,
    345       1.1       eeh 		 * so just do it on the A channel.
    346       1.1       eeh 		 */
    347       1.1       eeh 		if (channel == 0) {
    348       1.1       eeh 			zs_write_reg(cs, 9, 0);
    349       1.1       eeh 		}
    350       1.1       eeh 
    351       1.1       eeh 		/*
    352       1.1       eeh 		 * Look for a child driver for this channel.
    353       1.1       eeh 		 * The child attach will setup the hardware.
    354       1.1       eeh 		 */
    355      1.26       eeh 		if (!(child =
    356      1.26       eeh 		      config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print))) {
    357       1.1       eeh 			/* No sub-driver.  Just reset it. */
    358       1.1       eeh 			u_char reset = (channel == 0) ?
    359       1.1       eeh 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    360       1.1       eeh 			s = splzs();
    361       1.1       eeh 			zs_write_reg(cs,  9, reset);
    362       1.1       eeh 			splx(s);
    363      1.26       eeh 		}
    364      1.26       eeh #if (NKBD > 0) || (NMS > 0)
    365      1.26       eeh 		/*
    366      1.26       eeh 		 * If this was a zstty it has a keyboard
    367      1.26       eeh 		 * property on it we need to attach the
    368      1.26       eeh 		 * sunkbd and sunms line disciplines.
    369      1.26       eeh 		 */
    370      1.26       eeh 		if (child
    371      1.26       eeh 		    && (child->dv_cfdata->cf_driver == &zstty_cd)
    372      1.26       eeh 		    && (getproplen(zsc->zsc_node, "keyboard") == 0)) {
    373      1.26       eeh 			struct kbd_ms_tty_attach_args kma;
    374      1.26       eeh 			struct zstty_softc {
    375      1.26       eeh 				/* The following are the only fields we need here */
    376      1.26       eeh 				struct	device zst_dev;
    377      1.26       eeh 				struct  tty *zst_tty;
    378      1.26       eeh 				struct	zs_chanstate *zst_cs;
    379      1.26       eeh 			} *zst = (struct zstty_softc *)child;
    380      1.26       eeh 			struct tty *tp;
    381      1.26       eeh 
    382      1.26       eeh 			kma.kmta_tp = tp = zst->zst_tty;
    383      1.26       eeh 			kma.kmta_dev = tp->t_dev;
    384      1.26       eeh 			kma.kmta_consdev = zsc_args.consdev;
    385      1.26       eeh 
    386      1.26       eeh 			/* Attach 'em if we got 'em. */
    387      1.26       eeh #if (NKBD > 0)
    388      1.26       eeh 			if (channel == 0) {
    389      1.26       eeh 				kma.kmta_name = "keyboard";
    390      1.26       eeh 				config_found(child, (void *)&kma, NULL);
    391      1.26       eeh 			}
    392      1.26       eeh #endif
    393      1.26       eeh #if (NMS > 0)
    394      1.26       eeh 			if (channel == 1) {
    395      1.26       eeh 				kma.kmta_name = "mouse";
    396      1.26       eeh 				config_found(child, (void *)&kma, NULL);
    397      1.26       eeh 			}
    398      1.26       eeh #endif
    399       1.1       eeh 		}
    400      1.26       eeh #endif
    401       1.1       eeh 	}
    402       1.1       eeh 
    403       1.1       eeh 	/*
    404       1.1       eeh 	 * Now safe to install interrupt handlers.  Note the arguments
    405       1.1       eeh 	 * to the interrupt handlers aren't used.  Note, we only do this
    406       1.1       eeh 	 * once since both SCCs interrupt at the same level and vector.
    407       1.1       eeh 	 */
    408      1.25       eeh 	bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard, zsc);
    409      1.24       eeh 	if (!(zsc->zsc_softintr = softintr_establish(softpri, zssoft, zsc)))
    410      1.24       eeh 		panic("zsattach: could not establish soft interrupt\n");
    411       1.1       eeh 
    412      1.21       cgd 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
    413      1.21       cgd 	    zsc->zsc_dev.dv_xname, "intr");
    414       1.1       eeh 
    415      1.24       eeh 
    416       1.1       eeh 	/*
    417       1.1       eeh 	 * Set the master interrupt enable and interrupt vector.
    418       1.1       eeh 	 * (common to both channels, do it on A)
    419       1.1       eeh 	 */
    420       1.1       eeh 	cs = zsc->zsc_cs[0];
    421       1.1       eeh 	s = splhigh();
    422       1.1       eeh 	/* interrupt vector */
    423       1.1       eeh 	zs_write_reg(cs, 2, zs_init_reg[2]);
    424       1.1       eeh 	/* master interrupt control (enable) */
    425       1.1       eeh 	zs_write_reg(cs, 9, zs_init_reg[9]);
    426       1.1       eeh 	splx(s);
    427       1.1       eeh 
    428       1.1       eeh }
    429       1.1       eeh 
    430       1.1       eeh static int
    431       1.1       eeh zs_print(aux, name)
    432       1.1       eeh 	void *aux;
    433       1.1       eeh 	const char *name;
    434       1.1       eeh {
    435       1.1       eeh 	struct zsc_attach_args *args = aux;
    436       1.1       eeh 
    437       1.1       eeh 	if (name != NULL)
    438       1.1       eeh 		printf("%s: ", name);
    439       1.1       eeh 
    440       1.1       eeh 	if (args->channel != -1)
    441       1.1       eeh 		printf(" channel %d", args->channel);
    442       1.1       eeh 
    443       1.1       eeh 	return (UNCONF);
    444       1.1       eeh }
    445       1.1       eeh 
    446      1.24       eeh /* Deprecate this? */
    447       1.1       eeh static volatile int zssoftpending;
    448       1.1       eeh 
    449       1.1       eeh static int
    450       1.1       eeh zshard(arg)
    451       1.1       eeh 	void *arg;
    452       1.1       eeh {
    453      1.24       eeh 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    454      1.24       eeh 	int rr3, rval;
    455      1.24       eeh 
    456      1.24       eeh 	rval = 0;
    457      1.24       eeh 	while ((rr3 = zsc_intr_hard(zsc))) {
    458      1.24       eeh 		/* Count up the interrupts. */
    459      1.24       eeh 		rval |= rr3;
    460      1.24       eeh 		zsc->zsc_intrcnt.ev_count++;
    461      1.24       eeh 	}
    462      1.24       eeh 	if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) ||
    463      1.24       eeh 	     (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) &&
    464      1.24       eeh 	    zsc->zsc_softintr) {
    465      1.24       eeh 		zssoftpending = PIL_TTY;
    466      1.24       eeh 		softintr_schedule(zsc->zsc_softintr);
    467      1.24       eeh 	}
    468      1.24       eeh 	return (rval);
    469      1.24       eeh }
    470      1.24       eeh 
    471      1.24       eeh int
    472      1.24       eeh zscheckintr(arg)
    473      1.24       eeh 	void *arg;
    474      1.24       eeh {
    475      1.20       eeh 	struct zsc_softc *zsc;
    476      1.24       eeh 	int unit, rval;
    477       1.1       eeh 
    478      1.24       eeh 	rval = 0;
    479       1.1       eeh 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
    480      1.20       eeh 
    481       1.1       eeh 		zsc = zs_cd.cd_devs[unit];
    482       1.1       eeh 		if (zsc == NULL)
    483       1.1       eeh 			continue;
    484      1.24       eeh 		rval = (zshard((void *)zsc) || rval);
    485       1.1       eeh 	}
    486       1.1       eeh 	return (rval);
    487       1.1       eeh }
    488       1.1       eeh 
    489      1.24       eeh 
    490       1.1       eeh /*
    491      1.24       eeh  * We need this only for TTY_DEBUG purposes.
    492       1.1       eeh  */
    493      1.28      fvdl static void
    494       1.1       eeh zssoft(arg)
    495       1.1       eeh 	void *arg;
    496       1.1       eeh {
    497      1.24       eeh 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    498      1.24       eeh 	int s;
    499       1.1       eeh 
    500       1.1       eeh 	/* Make sure we call the tty layer at spltty. */
    501       1.1       eeh 	s = spltty();
    502      1.24       eeh 	zssoftpending = 0;
    503      1.24       eeh 	(void)zsc_intr_soft(zsc);
    504      1.13       eeh #ifdef TTY_DEBUG
    505      1.24       eeh 	{
    506      1.24       eeh 		struct zstty_softc *zst0 = zsc->zsc_cs[0]->cs_private;
    507      1.24       eeh 		struct zstty_softc *zst1 = zsc->zsc_cs[1]->cs_private;
    508      1.24       eeh 		if (zst0->zst_overflows || zst1->zst_overflows ) {
    509      1.24       eeh 			struct trapframe *frame = (struct trapframe *)arg;
    510      1.24       eeh 
    511      1.24       eeh 			printf("zs silo overflow from %p\n",
    512      1.24       eeh 			       (long)frame->tf_pc);
    513      1.13       eeh 		}
    514      1.24       eeh 	}
    515      1.13       eeh #endif
    516       1.1       eeh 	splx(s);
    517       1.1       eeh }
    518       1.1       eeh 
    519       1.1       eeh 
    520       1.1       eeh /*
    521       1.1       eeh  * Compute the current baud rate given a ZS channel.
    522       1.1       eeh  */
    523       1.1       eeh static int
    524       1.1       eeh zs_get_speed(cs)
    525       1.1       eeh 	struct zs_chanstate *cs;
    526       1.1       eeh {
    527       1.1       eeh 	int tconst;
    528       1.1       eeh 
    529       1.1       eeh 	tconst = zs_read_reg(cs, 12);
    530       1.1       eeh 	tconst |= zs_read_reg(cs, 13) << 8;
    531       1.1       eeh 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    532       1.1       eeh }
    533       1.1       eeh 
    534       1.1       eeh /*
    535       1.1       eeh  * MD functions for setting the baud rate and control modes.
    536       1.1       eeh  */
    537       1.1       eeh int
    538       1.1       eeh zs_set_speed(cs, bps)
    539       1.1       eeh 	struct zs_chanstate *cs;
    540       1.1       eeh 	int bps;	/* bits per second */
    541       1.1       eeh {
    542       1.1       eeh 	int tconst, real_bps;
    543       1.1       eeh 
    544       1.1       eeh 	if (bps == 0)
    545       1.1       eeh 		return (0);
    546       1.1       eeh 
    547       1.1       eeh #ifdef	DIAGNOSTIC
    548       1.1       eeh 	if (cs->cs_brg_clk == 0)
    549       1.1       eeh 		panic("zs_set_speed");
    550       1.1       eeh #endif
    551       1.1       eeh 
    552       1.1       eeh 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    553       1.1       eeh 	if (tconst < 0)
    554       1.1       eeh 		return (EINVAL);
    555       1.1       eeh 
    556       1.1       eeh 	/* Convert back to make sure we can do it. */
    557       1.1       eeh 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    558       1.1       eeh 
    559       1.1       eeh 	/* XXX - Allow some tolerance here? */
    560       1.1       eeh 	if (real_bps != bps)
    561       1.1       eeh 		return (EINVAL);
    562       1.1       eeh 
    563       1.1       eeh 	cs->cs_preg[12] = tconst;
    564       1.1       eeh 	cs->cs_preg[13] = tconst >> 8;
    565       1.1       eeh 
    566       1.1       eeh 	/* Caller will stuff the pending registers. */
    567       1.1       eeh 	return (0);
    568       1.1       eeh }
    569       1.1       eeh 
    570       1.1       eeh int
    571       1.1       eeh zs_set_modes(cs, cflag)
    572       1.1       eeh 	struct zs_chanstate *cs;
    573       1.1       eeh 	int cflag;	/* bits per second */
    574       1.1       eeh {
    575       1.1       eeh 	int s;
    576       1.1       eeh 
    577       1.1       eeh 	/*
    578       1.1       eeh 	 * Output hardware flow control on the chip is horrendous:
    579       1.1       eeh 	 * if carrier detect drops, the receiver is disabled, and if
    580       1.1       eeh 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    581       1.1       eeh 	 * Therefore, NEVER set the HFC bit, and instead use the
    582       1.1       eeh 	 * status interrupt to detect CTS changes.
    583       1.1       eeh 	 */
    584       1.1       eeh 	s = splzs();
    585       1.9  wrstuden 	cs->cs_rr0_pps = 0;
    586       1.9  wrstuden 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    587       1.1       eeh 		cs->cs_rr0_dcd = 0;
    588       1.9  wrstuden 		if ((cflag & MDMBUF) == 0)
    589       1.9  wrstuden 			cs->cs_rr0_pps = ZSRR0_DCD;
    590       1.9  wrstuden 	} else
    591       1.1       eeh 		cs->cs_rr0_dcd = ZSRR0_DCD;
    592       1.1       eeh 	if ((cflag & CRTSCTS) != 0) {
    593       1.1       eeh 		cs->cs_wr5_dtr = ZSWR5_DTR;
    594       1.1       eeh 		cs->cs_wr5_rts = ZSWR5_RTS;
    595       1.1       eeh 		cs->cs_rr0_cts = ZSRR0_CTS;
    596       1.1       eeh 	} else if ((cflag & CDTRCTS) != 0) {
    597       1.1       eeh 		cs->cs_wr5_dtr = 0;
    598       1.1       eeh 		cs->cs_wr5_rts = ZSWR5_DTR;
    599       1.1       eeh 		cs->cs_rr0_cts = ZSRR0_CTS;
    600       1.1       eeh 	} else if ((cflag & MDMBUF) != 0) {
    601       1.1       eeh 		cs->cs_wr5_dtr = 0;
    602       1.1       eeh 		cs->cs_wr5_rts = ZSWR5_DTR;
    603       1.1       eeh 		cs->cs_rr0_cts = ZSRR0_DCD;
    604       1.1       eeh 	} else {
    605       1.1       eeh 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    606       1.1       eeh 		cs->cs_wr5_rts = 0;
    607       1.1       eeh 		cs->cs_rr0_cts = 0;
    608       1.1       eeh 	}
    609       1.1       eeh 	splx(s);
    610       1.1       eeh 
    611       1.1       eeh 	/* Caller will stuff the pending registers. */
    612       1.1       eeh 	return (0);
    613       1.1       eeh }
    614       1.1       eeh 
    615       1.1       eeh 
    616       1.1       eeh /*
    617       1.1       eeh  * Read or write the chip with suitable delays.
    618       1.1       eeh  */
    619       1.1       eeh 
    620       1.1       eeh u_char
    621       1.1       eeh zs_read_reg(cs, reg)
    622       1.1       eeh 	struct zs_chanstate *cs;
    623       1.1       eeh 	u_char reg;
    624       1.1       eeh {
    625       1.1       eeh 	u_char val;
    626       1.1       eeh 
    627       1.1       eeh 	*cs->cs_reg_csr = reg;
    628       1.1       eeh 	ZS_DELAY();
    629       1.1       eeh 	val = *cs->cs_reg_csr;
    630       1.1       eeh 	ZS_DELAY();
    631       1.1       eeh 	return (val);
    632       1.1       eeh }
    633       1.1       eeh 
    634       1.1       eeh void
    635       1.1       eeh zs_write_reg(cs, reg, val)
    636       1.1       eeh 	struct zs_chanstate *cs;
    637       1.1       eeh 	u_char reg, val;
    638       1.1       eeh {
    639       1.1       eeh 	*cs->cs_reg_csr = reg;
    640       1.1       eeh 	ZS_DELAY();
    641       1.1       eeh 	*cs->cs_reg_csr = val;
    642       1.1       eeh 	ZS_DELAY();
    643       1.1       eeh }
    644       1.1       eeh 
    645       1.1       eeh u_char
    646       1.1       eeh zs_read_csr(cs)
    647       1.1       eeh 	struct zs_chanstate *cs;
    648       1.1       eeh {
    649      1.20       eeh 	u_char val;
    650       1.1       eeh 
    651       1.1       eeh 	val = *cs->cs_reg_csr;
    652       1.1       eeh 	ZS_DELAY();
    653       1.1       eeh 	return (val);
    654       1.1       eeh }
    655       1.1       eeh 
    656       1.1       eeh void  zs_write_csr(cs, val)
    657       1.1       eeh 	struct zs_chanstate *cs;
    658       1.1       eeh 	u_char val;
    659       1.1       eeh {
    660       1.1       eeh 	*cs->cs_reg_csr = val;
    661       1.1       eeh 	ZS_DELAY();
    662       1.1       eeh }
    663       1.1       eeh 
    664       1.1       eeh u_char zs_read_data(cs)
    665       1.1       eeh 	struct zs_chanstate *cs;
    666       1.1       eeh {
    667      1.20       eeh 	u_char val;
    668       1.1       eeh 
    669       1.1       eeh 	val = *cs->cs_reg_data;
    670       1.1       eeh 	ZS_DELAY();
    671       1.1       eeh 	return (val);
    672       1.1       eeh }
    673       1.1       eeh 
    674       1.1       eeh void  zs_write_data(cs, val)
    675       1.1       eeh 	struct zs_chanstate *cs;
    676       1.1       eeh 	u_char val;
    677       1.1       eeh {
    678       1.1       eeh 	*cs->cs_reg_data = val;
    679       1.1       eeh 	ZS_DELAY();
    680       1.1       eeh }
    681       1.1       eeh 
    682       1.1       eeh /****************************************************************
    683       1.1       eeh  * Console support functions (Sun specific!)
    684       1.1       eeh  * Note: this code is allowed to know about the layout of
    685       1.1       eeh  * the chip registers, and uses that to keep things simple.
    686       1.1       eeh  * XXX - I think I like the mvme167 code better. -gwr
    687       1.1       eeh  ****************************************************************/
    688       1.1       eeh 
    689       1.1       eeh extern void Debugger __P((void));
    690       1.1       eeh 
    691       1.1       eeh /*
    692       1.1       eeh  * Handle user request to enter kernel debugger.
    693       1.1       eeh  */
    694       1.1       eeh void
    695       1.1       eeh zs_abort(cs)
    696       1.1       eeh 	struct zs_chanstate *cs;
    697       1.1       eeh {
    698      1.20       eeh 	volatile struct zschan *zc = zs_conschan_get;
    699       1.1       eeh 	int rr0;
    700       1.1       eeh 
    701       1.1       eeh 	/* Wait for end of break to avoid PROM abort. */
    702       1.1       eeh 	/* XXX - Limit the wait? */
    703       1.1       eeh 	do {
    704       1.1       eeh 		rr0 = zc->zc_csr;
    705       1.1       eeh 		ZS_DELAY();
    706       1.1       eeh 	} while (rr0 & ZSRR0_BREAK);
    707       1.1       eeh 
    708       1.1       eeh #if defined(KGDB)
    709       1.1       eeh 	zskgdb(cs);
    710       1.1       eeh #elif defined(DDB)
    711      1.12       eeh 	{
    712      1.12       eeh 		extern int db_active;
    713      1.12       eeh 
    714      1.12       eeh 		if (!db_active)
    715      1.12       eeh 			Debugger();
    716      1.12       eeh 		else
    717      1.12       eeh 			/* Debugger is probably hozed */
    718      1.12       eeh 			callrom();
    719      1.12       eeh 	}
    720       1.1       eeh #else
    721       1.1       eeh 	printf("stopping on keyboard abort\n");
    722       1.1       eeh 	callrom();
    723       1.1       eeh #endif
    724       1.1       eeh }
    725       1.1       eeh 
    726      1.20       eeh 
    727       1.1       eeh /*
    728       1.1       eeh  * Polled input char.
    729       1.1       eeh  */
    730       1.1       eeh int
    731       1.1       eeh zs_getc(arg)
    732       1.1       eeh 	void *arg;
    733       1.1       eeh {
    734      1.20       eeh 	volatile struct zschan *zc = arg;
    735      1.20       eeh 	int s, c, rr0;
    736       1.1       eeh 
    737       1.1       eeh 	s = splhigh();
    738       1.1       eeh 	/* Wait for a character to arrive. */
    739       1.1       eeh 	do {
    740       1.1       eeh 		rr0 = zc->zc_csr;
    741       1.1       eeh 		ZS_DELAY();
    742       1.1       eeh 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    743       1.1       eeh 
    744       1.1       eeh 	c = zc->zc_data;
    745       1.1       eeh 	ZS_DELAY();
    746       1.1       eeh 	splx(s);
    747       1.1       eeh 
    748       1.1       eeh 	/*
    749       1.1       eeh 	 * This is used by the kd driver to read scan codes,
    750       1.1       eeh 	 * so don't translate '\r' ==> '\n' here...
    751       1.1       eeh 	 */
    752       1.1       eeh 	return (c);
    753       1.1       eeh }
    754       1.1       eeh 
    755       1.1       eeh /*
    756       1.1       eeh  * Polled output char.
    757       1.1       eeh  */
    758       1.1       eeh void
    759       1.1       eeh zs_putc(arg, c)
    760       1.1       eeh 	void *arg;
    761       1.1       eeh 	int c;
    762       1.1       eeh {
    763      1.20       eeh 	volatile struct zschan *zc = arg;
    764      1.20       eeh 	int s, rr0;
    765       1.1       eeh 
    766       1.1       eeh 	s = splhigh();
    767       1.1       eeh 
    768       1.1       eeh 	/* Wait for transmitter to become ready. */
    769       1.1       eeh 	do {
    770       1.1       eeh 		rr0 = zc->zc_csr;
    771       1.1       eeh 		ZS_DELAY();
    772       1.1       eeh 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    773       1.1       eeh 
    774       1.1       eeh 	/*
    775       1.1       eeh 	 * Send the next character.
    776       1.1       eeh 	 * Now you'd think that this could be followed by a ZS_DELAY()
    777       1.1       eeh 	 * just like all the other chip accesses, but it turns out that
    778       1.1       eeh 	 * the `transmit-ready' interrupt isn't de-asserted until
    779       1.1       eeh 	 * some period of time after the register write completes
    780       1.1       eeh 	 * (more than a couple instructions).  So to avoid stray
    781       1.1       eeh 	 * interrupts we put in the 2us delay regardless of cpu model.
    782       1.1       eeh 	 */
    783       1.1       eeh 	zc->zc_data = c;
    784       1.1       eeh 	delay(2);
    785       1.1       eeh 
    786       1.1       eeh 	splx(s);
    787       1.1       eeh }
    788       1.1       eeh 
    789       1.1       eeh /*****************************************************************/
    790       1.1       eeh 
    791       1.1       eeh 
    792      1.20       eeh 
    793       1.1       eeh 
    794       1.1       eeh /*
    795       1.1       eeh  * Polled console input putchar.
    796       1.1       eeh  */
    797       1.1       eeh static int
    798       1.1       eeh zscngetc(dev)
    799       1.1       eeh 	dev_t dev;
    800       1.1       eeh {
    801      1.20       eeh 	return (zs_getc(zs_conschan_get));
    802       1.1       eeh }
    803       1.1       eeh 
    804       1.1       eeh /*
    805       1.1       eeh  * Polled console output putchar.
    806       1.1       eeh  */
    807       1.1       eeh static void
    808       1.1       eeh zscnputc(dev, c)
    809       1.1       eeh 	dev_t dev;
    810       1.1       eeh 	int c;
    811       1.1       eeh {
    812      1.20       eeh 	zs_putc(zs_conschan_put, c);
    813       1.5       eeh }
    814       1.5       eeh 
    815       1.5       eeh int swallow_zsintrs;
    816       1.5       eeh 
    817       1.5       eeh static void
    818       1.5       eeh zscnpollc(dev, on)
    819       1.5       eeh 	dev_t dev;
    820       1.5       eeh 	int on;
    821       1.5       eeh {
    822       1.5       eeh 	/*
    823       1.5       eeh 	 * Need to tell zs driver to acknowledge all interrupts or we get
    824       1.5       eeh 	 * annoying spurious interrupt messages.  This is because mucking
    825       1.5       eeh 	 * with spl() levels during polling does not prevent interrupts from
    826       1.5       eeh 	 * being generated.
    827       1.5       eeh 	 */
    828       1.5       eeh 
    829       1.5       eeh 	if (on) swallow_zsintrs++;
    830       1.5       eeh 	else swallow_zsintrs--;
    831       1.1       eeh }
    832      1.20       eeh 
    833      1.20       eeh int
    834      1.20       eeh zs_console_flags(promunit, node, channel)
    835      1.20       eeh 	int promunit;
    836      1.20       eeh 	int node;
    837      1.20       eeh 	int channel;
    838      1.20       eeh {
    839      1.20       eeh 	int cookie, flags = 0;
    840      1.20       eeh 	u_int chosen;
    841      1.20       eeh 	char buf[255];
    842      1.20       eeh 
    843      1.20       eeh 	/*
    844      1.20       eeh 	 * We'll just to the OBP grovelling down here since that's
    845      1.20       eeh 	 * the only type of firmware we support.
    846      1.20       eeh 	 */
    847      1.20       eeh 	chosen = OF_finddevice("/chosen");
    848      1.20       eeh 
    849      1.20       eeh 	/* Default to channel 0 if there are no explicit prom args */
    850      1.20       eeh 	cookie = 0;
    851      1.20       eeh 	if (node == OF_instance_to_package(OF_stdin())) {
    852      1.20       eeh 		if (OF_getprop(chosen, "input-device", buf, sizeof(buf)) != -1) {
    853      1.20       eeh 
    854      1.20       eeh 			if (!strcmp("ttyb", buf))
    855      1.20       eeh 				cookie = 1;
    856      1.20       eeh 		}
    857      1.20       eeh 
    858      1.20       eeh 		if (channel == cookie)
    859      1.20       eeh 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
    860      1.20       eeh 	}
    861      1.20       eeh 
    862      1.20       eeh 	if (node == OF_instance_to_package(OF_stdout())) {
    863      1.20       eeh 		if (OF_getprop(chosen, "output-device", buf, sizeof(buf)) != -1) {
    864      1.20       eeh 
    865      1.20       eeh 			if (!strcmp("ttyb", buf))
    866      1.20       eeh 				cookie = 1;
    867      1.20       eeh 		}
    868      1.20       eeh 
    869      1.20       eeh 		if (channel == cookie)
    870      1.20       eeh 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
    871      1.20       eeh 	}
    872      1.20       eeh 
    873      1.20       eeh 	return (flags);
    874      1.20       eeh }
    875      1.20       eeh 
    876