#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vme.c,v 1.19 2021/08/07 16:19:06 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <uvm/uvm_extern.h>
#define _SUN68K_BUS_DMA_PRIVATE
#include <machine/autoconf.h>
#include <machine/bus.h>
#include <machine/dvma.h>
#include <machine/pmap.h>
#include <sun3/sun3x/vme.h>
extern int cpu_has_vme;
#define VME_UNITS 6
static const struct {
int bustype;
const char *name;
int pmtype;
vaddr_t base;
vaddr_t mask;
} vme_info[VME_UNITS] = {
{ BUS_VME16D16, "A16/D16", PMAP_VME16, VME16_BASE, VME16_MASK },
{ BUS_VME16D32, "A16/D32", PMAP_VME32, VME16_BASE, VME16_MASK },
{ BUS_VME24D16, "A24/D16", PMAP_VME16, VME24_BASE, VME24_MASK },
{ BUS_VME24D32, "A24/D32", PMAP_VME32, VME24_BASE, VME24_MASK },
{ BUS_VME32D16, "A32/D16", PMAP_VME16, VME32_BASE, VME32_MASK },
{ BUS_VME32D32, "A32/D32", PMAP_VME32, VME32_BASE, VME32_MASK },
};
static int vme_match(device_t, cfdata_t, void *);
static void vme_attach(device_t, device_t, void *);
struct vme_softc {
device_t sc_dev;
bus_space_tag_t sc_bustag;
bus_dma_tag_t sc_dmatag;
int sc_bustype;
};
CFATTACH_DECL_NEW(vme, sizeof(struct vme_softc),
vme_match, vme_attach, NULL, NULL);
static int vme_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
int, vaddr_t, bus_space_handle_t *);
static paddr_t vme_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
off_t, int, int);
static int vme_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
struct proc *, int);
static struct sun68k_bus_space_tag vme_space_tag = {
NULL,
NULL,
vme_bus_map,
NULL,
NULL,
NULL,
vme_bus_mmap,
NULL,
NULL,
NULL
};
static struct sun68k_bus_dma_tag vme_dma_tag;
static int
vme_match(device_t parent, cfdata_t cf, void *aux)
{
struct confargs *ca = aux;
int unit;
if (cpu_has_vme == 0)
return 0;
unit = cf->cf_unit;
if (unit >= VME_UNITS)
return 0;
if (ca->ca_bustype != vme_info[unit].bustype)
return 0;
return 1;
}
static void
vme_attach(device_t parent, device_t self, void *args)
{
struct confargs *ca = aux;
struct vme_softc *sc = device_private(self);
struct confargs vmea;
int unit;
sc->sc_dev = self;
unit = device_unit(self);
aprint_normal(": (%s)\n", vme_info[unit].name);
sc->sc_bustag = ca->ca_bustag;
sc->sc_dmatag = ca->ca_dmatag;
sc->sc_bustype = unit;
vme_space_tag.cookie = sc;
vme_space_tag.parent = sc->sc_bustag;
vme_dma_tag = *sc->sc_dmatag;
vme_dma_tag._cookie = sc;
vme_dma_tag._dmamap_load = vme_dmamap_load;
vmea = *ca;
vmea.ca_bustag = &vme_space_tag;
vmea.ca_dmatag = &vme_dma_tag;
config_search(self, args,
CFARGS(.search = bus_scan));
}
int
vme_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
{
struct vme_softc *sc = t->cookie;
paddr_t pa;
int bustype, pmtype;
bustype = sc->sc_bustype;
pa = paddr;
pa &= vme_info[bustype].mask;
pa |= vme_info[bustype].base;
pmtype = vme_info[bustype].pmtype;
return bus_space_map2(sc->sc_bustag, pmtype, pa, size,
flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
}
paddr_t
vme_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
int prot, int flags)
{
struct vme_softc *sc = t->cookie;
paddr_t pa;
int bustype, pmtype;
bustype = sc->sc_bustype;
pa = paddr;
pa &= vme_info[bustype].mask;
pa |= vme_info[bustype].base;
pmtype = vme_info[bustype].pmtype;
return bus_space_mmap2(sc->sc_bustag, pmtype, pa, off, prot, flags);
}
static int
vme_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
bus_size_t buflen, struct proc *p, int flags)
{
int error;
error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
if (error == 0)
map->dm_segs[0].ds_addr &= DVMA_VME_SLAVE_MASK;
return error;
}