Home | History | Annotate | Line # | Download | only in sbus
dma_sbus.c revision 1.7
      1 /*	$NetBSD: dma_sbus.c,v 1.7 2001/11/13 06:58:17 lukem Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1994 Peter Galbavy.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by Peter Galbavy.
     53  * 4. The name of the author may not be used to endorse or promote products
     54  *    derived from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     58  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     59  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     60  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     61  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     62  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     63  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     64  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     65  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: dma_sbus.c,v 1.7 2001/11/13 06:58:17 lukem Exp $");
     70 
     71 #include <sys/types.h>
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/kernel.h>
     75 #include <sys/errno.h>
     76 #include <sys/device.h>
     77 #include <sys/malloc.h>
     78 
     79 #include <machine/bus.h>
     80 #include <machine/intr.h>
     81 #include <machine/autoconf.h>
     82 
     83 #include <dev/sbus/sbusvar.h>
     84 
     85 #include <dev/ic/lsi64854reg.h>
     86 #include <dev/ic/lsi64854var.h>
     87 
     88 #include <dev/scsipi/scsi_all.h>
     89 #include <dev/scsipi/scsipi_all.h>
     90 #include <dev/scsipi/scsiconf.h>
     91 
     92 #include <dev/ic/ncr53c9xreg.h>
     93 #include <dev/ic/ncr53c9xvar.h>
     94 
     95 struct dma_softc {
     96 	struct lsi64854_softc	sc_lsi64854;	/* base device */
     97 	struct sbusdev	sc_sd;			/* sbus device */
     98 };
     99 
    100 int	dmamatch_sbus	__P((struct device *, struct cfdata *, void *));
    101 void	dmaattach_sbus	__P((struct device *, struct device *, void *));
    102 
    103 int	dmaprint_sbus	__P((void *, const char *));
    104 
    105 void	*dmabus_intr_establish __P((
    106 		bus_space_tag_t,
    107 		int,			/*bus interrupt priority*/
    108 		int,			/*`device class' level*/
    109 		int,			/*flags*/
    110 		int (*) __P((void *)),	/*handler*/
    111 		void *));		/*handler arg*/
    112 
    113 static	bus_space_tag_t dma_alloc_bustag __P((struct dma_softc *sc));
    114 
    115 struct cfattach dma_sbus_ca = {
    116 	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
    117 };
    118 
    119 struct cfattach ledma_ca = {
    120 	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
    121 };
    122 
    123 int
    124 dmaprint_sbus(aux, busname)
    125 	void *aux;
    126 	const char *busname;
    127 {
    128 	struct sbus_attach_args *sa = aux;
    129 	bus_space_tag_t t = sa->sa_bustag;
    130 	struct dma_softc *sc = t->cookie;
    131 
    132 	sa->sa_bustag = sc->sc_lsi64854.sc_bustag;	/* XXX */
    133 	sbus_print(aux, busname);	/* XXX */
    134 	sa->sa_bustag = t;		/* XXX */
    135 	return (UNCONF);
    136 }
    137 
    138 int
    139 dmamatch_sbus(parent, cf, aux)
    140 	struct device *parent;
    141 	struct cfdata *cf;
    142 	void *aux;
    143 {
    144 	struct sbus_attach_args *sa = aux;
    145 
    146 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
    147 		strcmp("espdma", sa->sa_name) == 0);
    148 }
    149 
    150 void
    151 dmaattach_sbus(parent, self, aux)
    152 	struct device *parent, *self;
    153 	void *aux;
    154 {
    155 	struct sbus_attach_args *sa = aux;
    156 	struct dma_softc *dsc = (void *)self;
    157 	struct lsi64854_softc *sc = &dsc->sc_lsi64854;
    158 	bus_space_handle_t bh;
    159 	bus_space_tag_t sbt;
    160 	int sbusburst, burst;
    161 	int node;
    162 
    163 	node = sa->sa_node;
    164 
    165 	sc->sc_bustag = sa->sa_bustag;
    166 	sc->sc_dmatag = sa->sa_dmatag;
    167 
    168 	/* Map registers */
    169 	if (sa->sa_npromvaddrs != 0)
    170 		sc->sc_regs = (bus_space_handle_t)sa->sa_promvaddrs[0];
    171 	else {
    172 		if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    173 				 sa->sa_offset,
    174 				 sa->sa_size,
    175 				 0, 0, &bh) != 0) {
    176 			printf("%s: cannot map registers\n", self->dv_xname);
    177 			return;
    178 		}
    179 		sc->sc_regs = bh;
    180 	}
    181 
    182 	/*
    183 	 * Get transfer burst size from PROM and plug it into the
    184 	 * controller registers. This is needed on the Sun4m; do
    185 	 * others need it too?
    186 	 */
    187 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
    188 	if (sbusburst == 0)
    189 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
    190 
    191 	burst = PROM_getpropint(node,"burst-sizes", -1);
    192 	if (burst == -1)
    193 		/* take SBus burst sizes */
    194 		burst = sbusburst;
    195 
    196 	/* Clamp at parent's burst sizes */
    197 	burst &= sbusburst;
    198 	sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
    199 		       (burst & SBUS_BURST_16) ? 16 : 0;
    200 
    201 	if (sc->sc_dev.dv_cfdata->cf_attach == &ledma_ca) {
    202 		char *cabletype;
    203 		u_int32_t csr;
    204 		/*
    205 		 * Check to see which cable type is currently active and
    206 		 * set the appropriate bit in the ledma csr so that it
    207 		 * gets used. If we didn't netboot, the PROM won't have
    208 		 * the "cable-selection" property; default to TP and then
    209 		 * the user can change it via a "media" option to ifconfig.
    210 		 */
    211 		cabletype = PROM_getpropstring(node, "cable-selection");
    212 		csr = L64854_GCSR(sc);
    213 		if (strcmp(cabletype, "tpe") == 0) {
    214 			csr |= E_TP_AUI;
    215 		} else if (strcmp(cabletype, "aui") == 0) {
    216 			csr &= ~E_TP_AUI;
    217 		} else {
    218 			/* assume TP if nothing there */
    219 			csr |= E_TP_AUI;
    220 		}
    221 		L64854_SCSR(sc, csr);
    222 		delay(20000);	/* manual says we need a 20ms delay */
    223 		sc->sc_channel = L64854_CHANNEL_ENET;
    224 	} else {
    225 		sc->sc_channel = L64854_CHANNEL_SCSI;
    226 	}
    227 
    228 	sbus_establish(&dsc->sc_sd, &sc->sc_dev);
    229 	sbt = dma_alloc_bustag(dsc);
    230 	lsi64854_attach(sc);
    231 
    232 	/* Attach children */
    233 	for (node = firstchild(sa->sa_node); node; node = nextsibling(node)) {
    234 		struct sbus_attach_args sa;
    235 		sbus_setup_attach_args((struct sbus_softc *)parent,
    236 				       sbt, sc->sc_dmatag, node, &sa);
    237 		(void) config_found(&sc->sc_dev, (void *)&sa, dmaprint_sbus);
    238 		sbus_destroy_attach_args(&sa);
    239 	}
    240 }
    241 
    242 void *
    243 dmabus_intr_establish(t, pri, level, flags, handler, arg)
    244 	bus_space_tag_t t;
    245 	int pri;
    246 	int level;
    247 	int flags;
    248 	int (*handler) __P((void *));
    249 	void *arg;
    250 {
    251 	struct lsi64854_softc *sc = t->cookie;
    252 
    253 	/* XXX - for now only le; do esp later */
    254 	if (sc->sc_channel == L64854_CHANNEL_ENET) {
    255 		sc->sc_intrchain = handler;
    256 		sc->sc_intrchainarg = arg;
    257 		handler = lsi64854_enet_intr;
    258 		arg = sc;
    259 	}
    260 	return (bus_intr_establish(sc->sc_bustag, pri, level, flags,
    261 				   handler, arg));
    262 }
    263 
    264 bus_space_tag_t
    265 dma_alloc_bustag(sc)
    266 	struct dma_softc *sc;
    267 {
    268 	bus_space_tag_t sbt;
    269 
    270 	sbt = (bus_space_tag_t)
    271 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    272 	if (sbt == NULL)
    273 		return (NULL);
    274 
    275 	bzero(sbt, sizeof *sbt);
    276 	sbt->cookie = sc;
    277 	sbt->parent = sc->sc_lsi64854.sc_bustag;
    278 	sbt->sparc_intr_establish = dmabus_intr_establish;
    279 	return (sbt);
    280 }
    281