Home | History | Annotate | Line # | Download | only in sbus
dma_sbus.c revision 1.3
      1 /*	$NetBSD: dma_sbus.c,v 1.3 1998/09/05 17:28:57 pk 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/types.h>
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kernel.h>
     72 #include <sys/errno.h>
     73 #include <sys/device.h>
     74 #include <sys/malloc.h>
     75 
     76 #include <machine/bus.h>
     77 #include <machine/autoconf.h>
     78 #include <machine/cpu.h>
     79 
     80 #include <dev/sbus/sbusvar.h>
     81 
     82 #include <dev/ic/lsi64854reg.h>
     83 #include <dev/ic/lsi64854var.h>
     84 
     85 #include <dev/scsipi/scsi_all.h>
     86 #include <dev/scsipi/scsipi_all.h>
     87 #include <dev/scsipi/scsiconf.h>
     88 
     89 #include <dev/ic/ncr53c9xreg.h>
     90 #include <dev/ic/ncr53c9xvar.h>
     91 
     92 struct dma_softc {
     93 	struct lsi64854_softc	sc_lsi64854;	/* base device */
     94 	struct sbusdev	sc_sd;			/* sbus device */
     95 };
     96 
     97 int	dmamatch_sbus	__P((struct device *, struct cfdata *, void *));
     98 void	dmaattach_sbus	__P((struct device *, struct device *, void *));
     99 
    100 int	dmaprint_sbus	__P((void *, const char *));
    101 
    102 void	*dmabus_intr_establish __P((
    103 		bus_space_tag_t,
    104 		int,			/*level*/
    105 		int,			/*flags*/
    106 		int (*) __P((void *)),	/*handler*/
    107 		void *));		/*handler arg*/
    108 
    109 static	bus_space_tag_t dma_alloc_bustag __P((struct dma_softc *sc));
    110 
    111 struct cfattach dma_sbus_ca = {
    112 	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
    113 };
    114 
    115 struct cfattach ledma_ca = {
    116 	sizeof(struct dma_softc), dmamatch_sbus, dmaattach_sbus
    117 };
    118 
    119 int
    120 dmaprint_sbus(aux, busname)
    121 	void *aux;
    122 	const char *busname;
    123 {
    124 	struct sbus_attach_args *sa = aux;
    125 	bus_space_tag_t t = sa->sa_bustag;
    126 	struct dma_softc *sc = t->cookie;
    127 
    128 	sa->sa_bustag = sc->sc_lsi64854.sc_bustag;	/* XXX */
    129 	sbus_print(aux, busname);	/* XXX */
    130 	sa->sa_bustag = t;		/* XXX */
    131 	return (UNCONF);
    132 }
    133 
    134 int
    135 dmamatch_sbus(parent, cf, aux)
    136 	struct device *parent;
    137 	struct cfdata *cf;
    138 	void *aux;
    139 {
    140 	struct sbus_attach_args *sa = aux;
    141 
    142 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
    143 		strcmp("espdma", sa->sa_name) == 0);
    144 }
    145 
    146 void
    147 dmaattach_sbus(parent, self, aux)
    148 	struct device *parent, *self;
    149 	void *aux;
    150 {
    151 	struct sbus_attach_args *sa = aux;
    152 	struct dma_softc *dsc = (void *)self;
    153 	struct lsi64854_softc *sc = &dsc->sc_lsi64854;
    154 	bus_space_handle_t bh;
    155 /*XXX*/	struct bootpath *bp;
    156 	bus_space_tag_t sbt;
    157 	int sbusburst, burst;
    158 	int node;
    159 
    160 	node = sa->sa_node;
    161 
    162 	sc->sc_bustag = sa->sa_bustag;
    163 	sc->sc_dmatag = sa->sa_dmatag;
    164 
    165 	/* Map registers */
    166 	if (sa->sa_npromvaddrs != 0)
    167 		sc->sc_regs = (bus_space_handle_t)sa->sa_promvaddrs[0];
    168 	else {
    169 		if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
    170 				 sa->sa_offset,
    171 				 sa->sa_size,
    172 				 0, 0, &bh) != 0) {
    173 			printf("%s: cannot map registers\n", self->dv_xname);
    174 			return;
    175 		}
    176 		sc->sc_regs = bh;
    177 	}
    178 
    179 	/*
    180 	 * Get transfer burst size from PROM and plug it into the
    181 	 * controller registers. This is needed on the Sun4m; do
    182 	 * others need it too?
    183 	 */
    184 	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
    185 	if (sbusburst == 0)
    186 		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
    187 
    188 	burst = getpropint(node,"burst-sizes", -1);
    189 	if (burst == -1)
    190 		/* take SBus burst sizes */
    191 		burst = sbusburst;
    192 
    193 	/* Clamp at parent's burst sizes */
    194 	burst &= sbusburst;
    195 	sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
    196 		       (burst & SBUS_BURST_16) ? 16 : 0;
    197 
    198 	if (sc->sc_dev.dv_cfdata->cf_attach == &ledma_ca) {
    199 		char *cabletype;
    200 		u_int32_t csr;
    201 		/*
    202 		 * Check to see which cable type is currently active and
    203 		 * set the appropriate bit in the ledma csr so that it
    204 		 * gets used. If we didn't netboot, the PROM won't have
    205 		 * the "cable-selection" property; default to TP and then
    206 		 * the user can change it via a "media" option to ifconfig.
    207 		 */
    208 		cabletype = getpropstring(node, "cable-selection");
    209 		csr = L64854_GCSR(sc);
    210 		if (strcmp(cabletype, "tpe") == 0) {
    211 			csr |= E_TP_AUI;
    212 		} else if (strcmp(cabletype, "aui") == 0) {
    213 			csr &= ~E_TP_AUI;
    214 		} else {
    215 			/* assume TP if nothing there */
    216 			csr |= E_TP_AUI;
    217 		}
    218 		L64854_SCSR(sc, csr);
    219 		delay(20000);	/* manual says we need a 20ms delay */
    220 		sc->sc_channel = L64854_CHANNEL_ENET;
    221 	} else {
    222 		sc->sc_channel = L64854_CHANNEL_SCSI;
    223 	}
    224 
    225 
    226 	/* Propagate bootpath */
    227 	bp = NULL;
    228 	if (sa->sa_bp != NULL) {
    229 		char *bpname = sa->sa_bp->name;
    230 		if (strcmp(bpname, "espdma") == 0)
    231 			/* We call everything "dma" */
    232 			bpname = "dma";
    233 
    234 		if (strcmp(bpname, self->dv_cfdata->cf_driver->cd_name) == 0)
    235 			bp = sa->sa_bp + 1;
    236 	}
    237 
    238 	sbus_establish(&dsc->sc_sd, &sc->sc_dev);
    239 	sbt = dma_alloc_bustag(dsc);
    240 	lsi64854_attach(sc);
    241 
    242 	/* Attach children */
    243 	for (node = firstchild(sa->sa_node); node; node = nextsibling(node)) {
    244 		struct sbus_attach_args sa;
    245 		sbus_setup_attach_args((struct sbus_softc *)parent,
    246 				       sbt, sc->sc_dmatag, node, bp, &sa);
    247 		(void) config_found(&sc->sc_dev, (void *)&sa, dmaprint_sbus);
    248 		sbus_destroy_attach_args(&sa);
    249 	}
    250 }
    251 
    252 void *
    253 dmabus_intr_establish(t, level, flags, handler, arg)
    254 	bus_space_tag_t t;
    255 	int level;
    256 	int flags;
    257 	int (*handler) __P((void *));
    258 	void *arg;
    259 {
    260 	struct lsi64854_softc *sc = t->cookie;
    261 
    262 	/* XXX - for now only le; do esp later */
    263 	if (sc->sc_channel == L64854_CHANNEL_ENET) {
    264 		sc->sc_intrchain = handler;
    265 		sc->sc_intrchainarg = arg;
    266 		handler = lsi64854_enet_intr;
    267 		arg = sc;
    268 	}
    269 	return (bus_intr_establish(sc->sc_bustag, level, flags, handler, arg));
    270 }
    271 
    272 bus_space_tag_t
    273 dma_alloc_bustag(sc)
    274 	struct dma_softc *sc;
    275 {
    276 	bus_space_tag_t sbt;
    277 
    278 	sbt = (bus_space_tag_t)
    279 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    280 	if (sbt == NULL)
    281 		return (NULL);
    282 
    283 	bzero(sbt, sizeof *sbt);
    284 	sbt->cookie = sc;
    285 	sbt->parent = sc->sc_lsi64854.sc_bustag;
    286 	sbt->sparc_intr_establish = dmabus_intr_establish;
    287 	return (sbt);
    288 }
    289