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