#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.21 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>
#include <machine/autoconf.h>
#include <machine/pmap.h>
#include <machine/pte.h>
#include <sun2/sun2/control.h>
#include <sun2/sun2/machdep.h>
extern int cpu_has_multibus;
static int obio_match(device_t, cfdata_t, void *);
static void obio_attach(device_t, device_t, void *);
struct obio_softc {
device_t sc_dev;
bus_space_tag_t sc_bustag;
bus_dma_tag_t sc_dmatag;
};
CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
obio_match, obio_attach, NULL, NULL);
static int obio_attached;
static paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t, off_t,
int, int);
static int _obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
int, vaddr_t, bus_space_handle_t *);
static int _obio_addr_bad(bus_space_tag_t, bus_space_handle_t, bus_size_t,
size_t);
static int _obio_bus_peek(bus_space_tag_t, bus_space_handle_t, bus_size_t,
size_t, void *);
static int _obio_bus_poke(bus_space_tag_t, bus_space_handle_t, bus_size_t,
size_t, uint32_t);
static struct sun68k_bus_space_tag obio_space_tag = {
NULL,
NULL,
_obio_bus_map,
NULL,
NULL,
NULL,
obio_bus_mmap,
NULL,
_obio_bus_peek,
_obio_bus_poke
};
static int
obio_match(device_t parent, cfdata_t cf, void *aux)
{
struct mainbus_attach_args *ma = aux;
if (obio_attached)
return 0;
return ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0;
}
static void
obio_attach(device_t parent, device_t self, void *aux)
{
struct mainbus_attach_args *ma = aux;
struct obio_softc *sc = device_private(self);
struct obio_attach_args oba;
const char *const *cpp;
static const char *const special[] = {
NULL
};
obio_attached = 1;
sc->sc_dev = self;
aprint_normal("\n");
sc->sc_bustag = ma->ma_bustag;
sc->sc_dmatag = ma->ma_dmatag;
obio_space_tag.cookie = sc;
obio_space_tag.parent = sc->sc_bustag;
oba = *ma;
oba.oba_bustag = &obio_space_tag;
oba.oba_paddr = LOCATOR_REQUIRED;
oba.oba_pri = LOCATOR_OPTIONAL;
for (cpp = special; *cpp != NULL; cpp++) {
oba.oba_name = *cpp;
config_search(self, &oba,
CFARGS(.search = sun68k_bus_search));
}
oba.oba_name = NULL;
config_search(self, &oba,
CFARGS(.search = sun68k_bus_search));
}
int
_obio_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 obio_softc *sc = t->cookie;
return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
}
paddr_t
obio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
int prot, int flags)
{
struct obio_softc *sc = t->cookie;
return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off,
prot, flags);
}
int
_obio_addr_bad(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s)
{
u_int pte;
paddr_t pa;
pte = get_pte((vaddr_t) (h + o));
if ((pte & PG_VALID) == 0)
return -1;
pa = PG_PA(pte);
return (!!cpu_has_multibus) != (pa >= 0x2000 && pa < 0x4000);
}
int
_obio_bus_peek(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s,
void *vp)
{
struct obio_softc *sc = t->cookie;
return _obio_addr_bad(t, h, o, s) ||
_bus_space_peek(sc->sc_bustag, h, o, s, vp);
}
int
_obio_bus_poke(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s,
uint32_t v)
{
struct obio_softc *sc = t->cookie;
return _obio_addr_bad(t, h, o, s) ||
_bus_space_poke(sc->sc_bustag, h, o, s, v);
}