#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.1 2026/06/27 13:28:35 rkujawa Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/vmem.h>
#include <sys/bus.h>
#include <dev/ofw/openfirm.h>
#include <machine/autoconf.h>
#include <powerpc/mpc5200/obiovar.h>
#include <powerpc/mpc5200/mpc5200reg.h>
#include <powerpc/mpc5200/sramvar.h>
static int sram_match(device_t, cfdata_t, void *);
static void sram_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(sram, sizeof(struct sram_softc),
sram_match, sram_attach, NULL, NULL);
static struct sram_softc *sram_sc;
static int
sram_match(device_t parent, cfdata_t cf, void *aux)
{
struct obio_attach_args *oba = aux;
char compat[32];
int len;
if (strcmp(oba->obio_name, "sram") == 0)
return 1;
len = OF_getprop(oba->obio_node, "compatible", compat, sizeof(compat));
if (len > 0 &&
(strcmp(compat, "mpc5200-sram") == 0 ||
strcmp(compat, "mpc5200b-sram") == 0))
return 1;
return 0;
}
static void
sram_attach(device_t parent, device_t self, void *aux)
{
struct sram_softc *sc = device_private(self);
struct obio_attach_args *oba = aux;
bus_size_t size;
sc->sc_dev = self;
sc->sc_iot = oba->obio_bst;
size = oba->obio_size != 0 ? oba->obio_size : MPC5200_SRAM_SIZE;
if (bus_space_map(sc->sc_iot, oba->obio_addr, size,
BUS_SPACE_MAP_LINEAR, &sc->sc_ioh) != 0) {
aprint_error(": can't map registers\n");
return;
}
sc->sc_pa = oba->obio_addr;
sc->sc_kva = (vaddr_t)bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
sc->sc_size = size;
sc->sc_arena = vmem_create(device_xname(self), sc->sc_pa, size,
sizeof(uint32_t), NULL, NULL, NULL, 0, VM_SLEEP, IPL_VM);
if (sc->sc_arena == NULL) {
aprint_error(": can't create arena\n");
bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
return;
}
sram_sc = sc;
aprint_normal(": %ju KB on-chip SRAM\n", (uintmax_t)(size / 1024));
}
bool
sram_available(void)
{
return sram_sc != NULL;
}
bus_addr_t
sram_alloc(size_t size, size_t align)
{
vmem_addr_t addr;
if (sram_sc == NULL)
return 0;
if (vmem_xalloc(sram_sc->sc_arena, size, align, 0, 0,
VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_BESTFIT | VM_NOSLEEP, &addr) != 0)
return 0;
return (bus_addr_t)addr;
}
void
sram_free(bus_addr_t addr, size_t size)
{
if (sram_sc == NULL || addr == 0)
return;
vmem_xfree(sram_sc->sc_arena, (vmem_addr_t)addr, size);
}
void *
sram_kva(bus_addr_t addr)
{
if (sram_sc == NULL ||
addr < sram_sc->sc_pa || addr >= sram_sc->sc_pa + sram_sc->sc_size)
return NULL;
return (void *)(sram_sc->sc_kva + (addr - sram_sc->sc_pa));
}