#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>
#include <dev/bhnd/bhnd.h>
#include "bhnd_nvram_if.h"
#include "bhnd_nvram_io.h"
#include "bhnd_spromvar.h"
int
bhnd_sprom_probe(device_t dev)
{
device_set_desc(dev, "SPROM/OTP");
return (BUS_PROBE_NOWILDCARD);
}
static int
bhnd_sprom_attach_meth(device_t dev)
{
return (bhnd_sprom_attach(dev, 0));
}
int
bhnd_sprom_attach(device_t dev, bus_size_t offset)
{
struct bhnd_sprom_softc *sc;
struct bhnd_nvram_io *io;
struct bhnd_resource *r;
bus_size_t r_size, sprom_size;
int error;
sc = device_get_softc(dev);
sc->dev = dev;
sc->store = NULL;
io = NULL;
r = NULL;
r = bhnd_alloc_resource_any(dev, SYS_RES_MEMORY, 0, RF_ACTIVE);
if (r == NULL) {
device_printf(dev, "failed to allocate resources\n");
return (ENXIO);
}
r_size = rman_get_size(r->res);
if (r_size <= offset || (r_size - offset) > BUS_SPACE_MAXSIZE) {
device_printf(dev, "invalid sprom offset\n");
error = ENXIO;
goto failed;
}
sprom_size = r_size - offset;
io = bhnd_nvram_iores_new(r, offset, sprom_size, sizeof(uint16_t));
if (io == NULL) {
error = ENXIO;
goto failed;
}
error = bhnd_nvram_store_parse_new(&sc->store, io,
&bhnd_nvram_sprom_class);
if (error)
goto failed;
bhnd_nvram_io_free(io);
bhnd_release_resource(dev, r);
io = NULL;
r = NULL;
if ((error = bhnd_register_provider(dev, BHND_SERVICE_NVRAM))) {
device_printf(dev, "failed to register NVRAM provider: %d\n",
error);
goto failed;
}
return (0);
failed:
if (io != NULL)
bhnd_nvram_io_free(io);
if (r != NULL)
bhnd_release_resource(dev, r);
if (sc->store != NULL)
bhnd_nvram_store_free(sc->store);
return (error);
}
int
bhnd_sprom_resume(device_t dev)
{
return (0);
}
int
bhnd_sprom_suspend(device_t dev)
{
return (0);
}
int
bhnd_sprom_detach(device_t dev)
{
struct bhnd_sprom_softc *sc;
int error;
sc = device_get_softc(dev);
if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))
return (error);
bhnd_nvram_store_free(sc->store);
return (0);
}
static int
bhnd_sprom_getvar_method(device_t dev, const char *name, void *buf, size_t *len,
bhnd_nvram_type type)
{
struct bhnd_sprom_softc *sc = device_get_softc(dev);
return (bhnd_nvram_store_getvar(sc->store, name, buf, len, type));
}
static int
bhnd_sprom_setvar_method(device_t dev, const char *name, const void *buf,
size_t len, bhnd_nvram_type type)
{
struct bhnd_sprom_softc *sc = device_get_softc(dev);
return (bhnd_nvram_store_setvar(sc->store, name, buf, len, type));
}
static device_method_t bhnd_sprom_methods[] = {
DEVMETHOD(device_probe, bhnd_sprom_probe),
DEVMETHOD(device_attach, bhnd_sprom_attach_meth),
DEVMETHOD(device_resume, bhnd_sprom_resume),
DEVMETHOD(device_suspend, bhnd_sprom_suspend),
DEVMETHOD(device_detach, bhnd_sprom_detach),
DEVMETHOD(bhnd_nvram_getvar, bhnd_sprom_getvar_method),
DEVMETHOD(bhnd_nvram_setvar, bhnd_sprom_setvar_method),
DEVMETHOD_END
};
DEFINE_CLASS_0(bhnd_nvram_store, bhnd_sprom_driver, bhnd_sprom_methods, sizeof(struct bhnd_sprom_softc));
MODULE_VERSION(bhnd_sprom, 1);