Home | History | Annotate | Line # | Download | only in vme
vme.c revision 1.28
      1 /* $NetBSD: vme.c,v 1.28 2021/04/24 23:36:59 thorpej 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.28 2021/04/24 23:36:59 thorpej 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(device_t, cfdata_t, const int *, void *);
     47 static int vmesubmatch(device_t, cfdata_t, const int *, void *);
     48 int vmematch(device_t, cfdata_t, void *);
     49 void vmeattach(device_t, device_t, void *);
     50 static struct extent *vme_select_map(struct vmebus_softc*, vme_am_t);
     51 
     52 #define VME_SLAVE_DUMMYDRV "vme_slv"
     53 
     54 #define VME_NUMCFRANGES 3 /* cf. "files.vme" */
     55 
     56 CFATTACH_DECL_NEW(vme, sizeof(struct vmebus_softc),
     57     vmematch, vmeattach, NULL, NULL);
     58 
     59 const struct cfattach vme_slv_ca = {
     60 	0	/* never used */
     61 };
     62 
     63 static void
     64 vme_extractlocators(int *loc, struct vme_attach_args *aa)
     65 {
     66 	int i = 0;
     67 
     68 	/* XXX can't use constants in locators.h this way */
     69 
     70 	while (i < VME_NUMCFRANGES && i < VME_MAXCFRANGES &&
     71 	       loc[i] != -1) {
     72 		aa->r[i].offset = (vme_addr_t)loc[i];
     73 		aa->r[i].size = (vme_size_t)loc[3 + i];
     74 		aa->r[i].am = (vme_am_t)loc[6 + i];
     75 		i++;
     76 	}
     77 	aa->numcfranges = i;
     78 	aa->ilevel = loc[9];
     79 	aa->ivector = loc[10];
     80 }
     81 
     82 static int
     83 vmeprint(struct vme_attach_args *v, char *dummy)
     84 {
     85 	int i;
     86 
     87 	for (i = 0; i < v->numcfranges; i++) {
     88 		aprint_normal(" addr %x", v->r[i].offset);
     89 		if (v->r[i].size != -1)
     90 			aprint_normal("-%x", v->r[i].offset + v->r[i].size - 1);
     91 		if (v->r[i].am != -1)
     92 			aprint_normal(" am %02x", v->r[i].am);
     93 	}
     94 	if (v->ilevel != -1) {
     95 		aprint_normal(" irq %d", v->ilevel);
     96 		if (v->ivector != -1)
     97 			aprint_normal(" vector %x", v->ivector);
     98 	}
     99 	return (UNCONF);
    100 }
    101 
    102 /*
    103  * This looks for a (dummy) vme device "VME_SLAVE_DUMMYDRV".
    104  * A callback provided by the bus's parent is called for every such
    105  * entry in the config database.
    106  * This is a special hack allowing to communicate the address settings
    107  * of the VME master's slave side to its driver via the normal
    108  * configuration mechanism.
    109  * Needed in following cases:
    110  *  -DMA windows are hardware settable but not readable by software
    111  *   (driver gets offsets for DMA address calculations this way)
    112  *  -DMA windows are software settable, but not persistent
    113  *   (hardware is set up from config file entry)
    114  *  -other adapter VME slave ranges which should be kept track of
    115  *   for address space accounting
    116  * In any case, the adapter driver must get the data before VME
    117  * devices are attached.
    118  */
    119 static int
    120 vmesubmatch1(device_t bus, cfdata_t dev, const int *ldesc, void *aux)
    121 {
    122 	struct vmebus_softc *sc = device_private(bus);
    123 	struct vme_attach_args v;
    124 
    125 	if (strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
    126 		return (0);
    127 
    128 	vme_extractlocators(dev->cf_loc, &v);
    129 
    130 	v.va_vct = sc->sc_vct; /* for space allocation */
    131 
    132 	(*sc->slaveconfig)(device_parent(bus), &v);
    133 	return (0);
    134 }
    135 
    136 static int
    137 vmesubmatch(device_t bus, cfdata_t dev, const int *ldesc, void *aux)
    138 {
    139 	struct vmebus_softc *sc = device_private(bus);
    140 	struct vme_attach_args v;
    141 
    142 	if (!strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
    143 		return (0);
    144 
    145 	vme_extractlocators(dev->cf_loc, &v);
    146 
    147 	v.va_vct = sc->sc_vct;
    148 	v.va_bdt = sc->sc_bdt;
    149 
    150 	if (config_probe(bus, dev, &v)) {
    151 		config_attach(bus, dev, &v, (cfprint_t)vmeprint, CFARG_EOL);
    152 		return (1);
    153 	}
    154 	return (0);
    155 }
    156 
    157 int
    158 vmematch(device_t parent, cfdata_t match, void *aux)
    159 {
    160 	return (1);
    161 }
    162 
    163 void
    164 vmeattach(device_t parent, device_t self, void *aux)
    165 {
    166 	struct vmebus_softc *sc = device_private(self);
    167 
    168 	struct vmebus_attach_args *aa = aux;
    169 
    170 	sc->sc_vct = aa->va_vct;
    171 	sc->sc_bdt = aa->va_bdt;
    172 
    173 	/* the "bus" are we ourselves */
    174 	sc->sc_vct->bus = sc;
    175 
    176 	sc->slaveconfig = aa->va_slaveconfig;
    177 
    178 	printf("\n");
    179 
    180 	/*
    181 	 * set up address space accounting - assume incomplete decoding
    182 	 */
    183 	sc->vme32ext = extent_create("vme32", 0, 0xffffffff, 0, 0, 0);
    184 	if (!sc->vme32ext) {
    185 		printf("error creating A32 map\n");
    186 		return;
    187 	}
    188 
    189 	sc->vme24ext = extent_create("vme24", 0, 0x00ffffff, 0, 0, 0);
    190 	if (!sc->vme24ext) {
    191 		printf("error creating A24 map\n");
    192 		return;
    193 	}
    194 
    195 	sc->vme16ext = extent_create("vme16", 0, 0x0000ffff, 0, 0, 0);
    196 	if (!sc->vme16ext) {
    197 		printf("error creating A16 map\n");
    198 		return;
    199 	}
    200 
    201 	if (sc->slaveconfig) {
    202 		/* first get info about the bus master's slave side,
    203 		 if present */
    204 		config_search(self, NULL,
    205 		    CFARG_SEARCH, vmesubmatch1,
    206 		    CFARG_EOL);
    207 	}
    208 	config_search(self, NULL,
    209 	    CFARG_SEARCH, vmesubmatch,
    210 	    CFARG_EOL);
    211 
    212 #ifdef VMEDEBUG
    213 	if (sc->vme32ext)
    214 		extent_print(sc->vme32ext);
    215 	if (sc->vme24ext)
    216 		extent_print(sc->vme24ext);
    217 	if (sc->vme16ext)
    218 		extent_print(sc->vme16ext);
    219 #endif
    220 }
    221 
    222 #ifdef notyet
    223 int
    224 vmedetach(device_t dev)
    225 {
    226 	struct vmebus_softc *sc = device_private(dev);
    227 
    228 	if (sc->slaveconfig) {
    229 		/* allow bus master to free its bus resources */
    230 		(*sc->slaveconfig)(device_parent(dev), 0);
    231 	}
    232 
    233 	/* extent maps should be empty now */
    234 
    235 	if (sc->vme32ext) {
    236 #ifdef VMEDEBUG
    237 		extent_print(sc->vme32ext);
    238 #endif
    239 		extent_destroy(sc->vme32ext);
    240 	}
    241 	if (sc->vme24ext) {
    242 #ifdef VMEDEBUG
    243 		extent_print(sc->vme24ext);
    244 #endif
    245 		extent_destroy(sc->vme24ext);
    246 	}
    247 	if (sc->vme16ext) {
    248 #ifdef VMEDEBUG
    249 		extent_print(sc->vme16ext);
    250 #endif
    251 		extent_destroy(sc->vme16ext);
    252 	}
    253 
    254 	return (0);
    255 }
    256 #endif
    257 
    258 static struct extent *
    259 vme_select_map(struct vmebus_softc *sc, vme_am_t ams)
    260 {
    261 	if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A32)
    262 		return (sc->vme32ext);
    263 	else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A24)
    264 		return (sc->vme24ext);
    265 	else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A16)
    266 		return (sc->vme16ext);
    267 	else
    268 		return (0);
    269 }
    270 
    271 int
    272 _vme_space_alloc(struct vmebus_softc *sc, vme_addr_t addr, vme_size_t len, vme_am_t ams)
    273 {
    274 	struct extent *ex;
    275 
    276 	ex = vme_select_map(sc, ams);
    277 	if (!ex)
    278 		return (EINVAL);
    279 
    280 	return (extent_alloc_region(ex, addr, len, EX_NOWAIT));
    281 }
    282 
    283 void
    284 _vme_space_free(struct vmebus_softc *sc, vme_addr_t addr, vme_size_t len, vme_am_t ams)
    285 {
    286 	struct extent *ex;
    287 
    288 	ex = vme_select_map(sc, ams);
    289 	if (!ex) {
    290 		panic("vme_space_free: invalid am %x", ams);
    291 		return;
    292 	}
    293 
    294 	extent_free(ex, addr, len, EX_NOWAIT);
    295 }
    296 
    297 int
    298 _vme_space_get(struct vmebus_softc *sc, vme_size_t len, vme_am_t ams, u_long align, vme_addr_t *addr)
    299 {
    300 	struct extent *ex;
    301 	u_long help;
    302 	int res;
    303 
    304 	ex = vme_select_map(sc, ams);
    305 	if (!ex)
    306 		return (EINVAL);
    307 
    308 	res = extent_alloc(ex, len, align, EX_NOBOUNDARY, EX_NOWAIT, &help);
    309 	if (!res)
    310 		*addr = help;
    311 	return (res);
    312 }
    313