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