#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <armv7/armv7/armv7_machdep.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>
#define SYS_ID 0x00
#define SYS_PROCID0 0x84
#define SYS_PROCID1 0x88
#define SYS_CFGDATA 0xa0
#define SYS_CFGCTRL 0xa4
#define SYS_CFGSTAT 0xa8
#define SYS_CFG_WRITE (1 << 30)
#define SYS_CFG_START (1U << 31)
#define SYS_CFG_RESET 5
#define SYS_CFG_SHUTDOWN 8
#define SYS_CFG_REBOOT 9
#define SYS_CFGSTAT_COMPLETE (1 << 0)
#define SYS_CFGSTAT_ERROR (1 << 1)
struct sysreg_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
};
struct sysreg_softc *sysreg_sc;
int sysreg_match(struct device *, void *, void *);
void sysreg_attach(struct device *, struct device *, void *);
void sysconf_function(struct sysreg_softc *, int);
void sysconf_reboot(void);
void sysconf_shutdown(void);
const struct cfattach sysreg_ca = {
sizeof (struct sysreg_softc), sysreg_match, sysreg_attach
};
struct cfdriver sysreg_cd = {
NULL, "sysreg", DV_DULL
};
int
sysreg_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "arm,vexpress-sysreg");
}
void
sysreg_attach(struct device *parent, struct device *self, void *aux)
{
struct fdt_attach_args *faa = aux;
struct sysreg_softc *sc = (struct sysreg_softc *)self;
uint32_t id;
sc->sc_iot = faa->fa_iot;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc_ioh))
panic(": bus_space_map failed!");
sysreg_sc = sc;
id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_ID);
printf(": ID 0x%x", id);
id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_PROCID0);
printf(" PROCID0 0x%x\n", id);
cpuresetfn = sysconf_reboot;
powerdownfn = sysconf_shutdown;
}
void
sysconf_function(struct sysreg_softc *sc, int function)
{
int dcc, site, position, device;
dcc = 0;
site = 0;
position = 0;
device = 0;
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SYS_CFGSTAT, 0);
bus_space_write_4(sc->sc_iot, sc->sc_ioh, SYS_CFGCTRL,
SYS_CFG_START | SYS_CFG_WRITE |
(dcc << 26) | (function << 20) | (site << 16) |
(position << 12) | device);
while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_CFGSTAT) &
SYS_CFGSTAT_COMPLETE) == 0);
if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, SYS_CFGSTAT) &
SYS_CFGSTAT_ERROR)
printf("SYS_CFGSTAT error\n");
}
void
sysconf_reboot(void)
{
struct sysreg_softc *sc = sysreg_sc;
if (sc == NULL)
return;
sysconf_function(sc, SYS_CFG_REBOOT);
}
void
sysconf_shutdown(void)
{
struct sysreg_softc *sc = sysreg_sc;
if (sc == NULL)
return;
sysconf_function(sc, SYS_CFG_SHUTDOWN);
}