Home | History | Annotate | Line # | Download | only in vme
vme.c revision 1.20
      1 /* $NetBSD: vme.c,v 1.20 2009/03/14 15:36:21 dsl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999
      5  *	Matthias Drochner.  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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: vme.c,v 1.20 2009/03/14 15:36:21 dsl Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/malloc.h>
     38 #include <sys/extent.h>
     39 #include <sys/bus.h>
     40 
     41 #include <dev/vme/vmereg.h>
     42 #include <dev/vme/vmevar.h>
     43 
     44 static void vme_extractlocators(int*, struct vme_attach_args*);
     45 static int vmeprint(struct vme_attach_args*, char*);
     46 static int vmesubmatch1(struct device*, struct cfdata*,
     47 			     const int *, void*);
     48 static int vmesubmatch(struct device*, struct cfdata*,
     49 			    const int *, void*);
     50 int vmematch(struct device *, struct cfdata *, void *);
     51 void vmeattach(struct device*, struct device*,void*);
     52 static struct extent *vme_select_map(struct vmebus_softc*, vme_am_t);
     53 
     54 #ifdef notyet
     55 int vmedetach(struct device*);
     56 #endif
     57 
     58 #define VME_SLAVE_DUMMYDRV "vme_slv"
     59 
     60 #define VME_NUMCFRANGES 3 /* cf. "files.vme" */
     61 
     62 CFATTACH_DECL(vme, sizeof(struct vmebus_softc),
     63     vmematch, vmeattach, NULL, NULL);
     64 
     65 const struct cfattach vme_slv_ca = {
     66 	0	/* never used */
     67 };
     68 
     69 static void
     70 vme_extractlocators(int *loc, struct vme_attach_args *aa)
     71 {
     72 	int i = 0;
     73 
     74 	/* XXX can't use constants in locators.h this way */
     75 
     76 	while (i < VME_NUMCFRANGES && i < VME_MAXCFRANGES &&
     77 	       loc[i] != -1) {
     78 		aa->r[i].offset = (vme_addr_t)loc[i];
     79 		aa->r[i].size = (vme_size_t)loc[3 + i];
     80 		aa->r[i].am = (vme_am_t)loc[6 + i];
     81 		i++;
     82 	}
     83 	aa->numcfranges = i;
     84 	aa->ilevel = loc[9];
     85 	aa->ivector = loc[10];
     86 }
     87 
     88 static int
     89 vmeprint(struct vme_attach_args *v, char *dummy)
     90 {
     91 	int i;
     92 
     93 	for (i = 0; i < v->numcfranges; i++) {
     94 		aprint_normal(" addr %x", v->r[i].offset);
     95 		if (v->r[i].size != -1)
     96 			aprint_normal("-%x", v->r[i].offset + v->r[i].size - 1);
     97 		if (v->r[i].am != -1)
     98 			aprint_normal(" am %02x", v->r[i].am);
     99 	}
    100 	if (v->ilevel != -1) {
    101 		aprint_normal(" irq %d", v->ilevel);
    102 		if (v->ivector != -1)
    103 			aprint_normal(" vector %x", v->ivector);
    104 	}
    105 	return (UNCONF);
    106 }
    107 
    108 /*
    109  * This looks for a (dummy) vme device "VME_SLAVE_DUMMYDRV".
    110  * A callback provided by the bus's parent is called for every such
    111  * entry in the config database.
    112  * This is a special hack allowing to communicate the address settings
    113  * of the VME master's slave side to its driver via the normal
    114  * configuration mechanism.
    115  * Needed in following cases:
    116  *  -DMA windows are hardware settable but not readable by software
    117  *   (driver gets offsets for DMA address calculations this way)
    118  *  -DMA windows are software settable, but not persistent
    119  *   (hardware is set up from config file entry)
    120  *  -other adapter VME slave ranges which should be kept track of
    121  *   for address space accounting
    122  * In any case, the adapter driver must get the data before VME
    123  * devices are attached.
    124  */
    125 static int
    126 vmesubmatch1(struct device *bus, struct cfdata *dev, const int *ldesc, void *aux)
    127 {
    128 	struct vmebus_softc *sc = (struct vmebus_softc*)bus;
    129 	struct vme_attach_args v;
    130 
    131 	if (strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
    132 		return (0);
    133 
    134 	vme_extractlocators(dev->cf_loc, &v);
    135 
    136 	v.va_vct = sc->sc_vct; /* for space allocation */
    137 
    138 	(*sc->slaveconfig)(device_parent(bus), &v);
    139 	return (0);
    140 }
    141 
    142 static int
    143 vmesubmatch(struct device *bus, struct cfdata *dev, const int *ldesc, void *aux)
    144 {
    145 	struct vmebus_softc *sc = (struct vmebus_softc*)bus;
    146 	struct vme_attach_args v;
    147 
    148 	if (!strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
    149 		return (0);
    150 
    151 	vme_extractlocators(dev->cf_loc, &v);
    152 
    153 	v.va_vct = sc->sc_vct;
    154 	v.va_bdt = sc->sc_bdt;
    155 
    156 	if (config_match(bus, dev, &v)) {
    157 		config_attach(bus, dev, &v, (cfprint_t)vmeprint);
    158 		return (1);
    159 	}
    160 	return (0);
    161 }
    162 
    163 int
    164 vmematch(struct device *parent, struct cfdata *match, void *aux)
    165 {
    166 	return (1);
    167 }
    168 
    169 void
    170 vmeattach(parent, self, aux)
    171 	struct device *parent, *self;
    172 	void *aux;
    173 {
    174 	struct vmebus_softc *sc = (struct vmebus_softc *)self;
    175 
    176 	struct vmebus_attach_args *aa =
    177 	    (struct vmebus_attach_args*)aux;
    178 
    179 	sc->sc_vct = aa->va_vct;
    180 	sc->sc_bdt = aa->va_bdt;
    181 
    182 	/* the "bus" are we ourselves */
    183 	sc->sc_vct->bus = sc;
    184 
    185 	sc->slaveconfig = aa->va_slaveconfig;
    186 
    187 	printf("\n");
    188 
    189 	/*
    190 	 * set up address space accounting - assume incomplete decoding
    191 	 */
    192 	sc->vme32ext = extent_create("vme32", 0, 0xffffffff,
    193 				     M_DEVBUF, 0, 0, 0);
    194 	if (!sc->vme32ext) {
    195 		printf("error creating A32 map\n");
    196 		return;
    197 	}
    198 
    199 	sc->vme24ext = extent_create("vme24", 0, 0x00ffffff,
    200 				     M_DEVBUF, 0, 0, 0);
    201 	if (!sc->vme24ext) {
    202 		printf("error creating A24 map\n");
    203 		return;
    204 	}
    205 
    206 	sc->vme16ext = extent_create("vme16", 0, 0x0000ffff,
    207 				     M_DEVBUF, 0, 0, 0);
    208 	if (!sc->vme16ext) {
    209 		printf("error creating A16 map\n");
    210 		return;
    211 	}
    212 
    213 	if (sc->slaveconfig) {
    214 		/* first get info about the bus master's slave side,
    215 		 if present */
    216 		config_search_ia(vmesubmatch1, self, "vme", 0);
    217 	}
    218 	config_search_ia(vmesubmatch, self, "vme", 0);
    219 
    220 #ifdef VMEDEBUG
    221 	if (sc->vme32ext)
    222 		extent_print(sc->vme32ext);
    223 	if (sc->vme24ext)
    224 		extent_print(sc->vme24ext);
    225 	if (sc->vme16ext)
    226 		extent_print(sc->vme16ext);
    227 #endif
    228 }
    229 
    230 #ifdef notyet
    231 int
    232 vmedetach(struct device *dev)
    233 {
    234 	struct vmebus_softc *sc = (struct vmebus_softc*)dev;
    235 
    236 	if (sc->slaveconfig) {
    237 		/* allow bus master to free its bus ressources */
    238 		(*sc->slaveconfig)(device_parent(dev), 0);
    239 	}
    240 
    241 	/* extent maps should be empty now */
    242 
    243 	if (sc->vme32ext) {
    244 #ifdef VMEDEBUG
    245 		extent_print(sc->vme32ext);
    246 #endif
    247 		extent_destroy(sc->vme32ext);
    248 	}
    249 	if (sc->vme24ext) {
    250 #ifdef VMEDEBUG
    251 		extent_print(sc->vme24ext);
    252 #endif
    253 		extent_destroy(sc->vme24ext);
    254 	}
    255 	if (sc->vme16ext) {
    256 #ifdef VMEDEBUG
    257 		extent_print(sc->vme16ext);
    258 #endif
    259 		extent_destroy(sc->vme16ext);
    260 	}
    261 
    262 	return (0);
    263 }
    264 #endif
    265 
    266 static struct extent *
    267 vme_select_map(struct vmebus_softc *sc, vme_am_t ams)
    268 {
    269 	if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A32)
    270 		return (sc->vme32ext);
    271 	else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A24)
    272 		return (sc->vme24ext);
    273 	else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A16)
    274 		return (sc->vme16ext);
    275 	else
    276 		return (0);
    277 }
    278 
    279 int
    280 _vme_space_alloc(sc, addr, len, ams)
    281 	struct vmebus_softc *sc;
    282 	vme_addr_t addr;
    283 	vme_size_t len;
    284 	vme_am_t ams;
    285 {
    286 	struct extent *ex;
    287 
    288 	ex = vme_select_map(sc, ams);
    289 	if (!ex)
    290 		return (EINVAL);
    291 
    292 	return (extent_alloc_region(ex, addr, len, EX_NOWAIT));
    293 }
    294 
    295 void
    296 _vme_space_free(sc, addr, len, ams)
    297 	struct vmebus_softc *sc;
    298 	vme_addr_t addr;
    299 	vme_size_t len;
    300 	vme_am_t ams;
    301 {
    302 	struct extent *ex;
    303 
    304 	ex = vme_select_map(sc, ams);
    305 	if (!ex) {
    306 		panic("vme_space_free: invalid am %x", ams);
    307 		return;
    308 	}
    309 
    310 	extent_free(ex, addr, len, EX_NOWAIT);
    311 }
    312 
    313 int
    314 _vme_space_get(sc, len, ams, align, addr)
    315 	struct vmebus_softc *sc;
    316 	vme_size_t len;
    317 	vme_am_t ams;
    318 	u_long align;
    319 	vme_addr_t *addr;
    320 {
    321 	struct extent *ex;
    322 	u_long help;
    323 	int res;
    324 
    325 	ex = vme_select_map(sc, ams);
    326 	if (!ex)
    327 		return (EINVAL);
    328 
    329 	res = extent_alloc(ex, len, align, EX_NOBOUNDARY, EX_NOWAIT, &help);
    330 	if (!res)
    331 		*addr = help;
    332 	return (res);
    333 }
    334