#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/fdt.h>
extern void (*cpuresetfn)(void);
struct gpiorestart_softc {
struct device sc_dev;
uint32_t *sc_gpio;
uint32_t sc_active_delay;
uint32_t sc_inactive_delay;
uint32_t sc_wait_delay;
};
int gpiorestart_match(struct device *, void *, void *);
void gpiorestart_attach(struct device *, struct device *, void *);
const struct cfattach gpiorestart_ca = {
sizeof(struct gpiorestart_softc), gpiorestart_match, gpiorestart_attach
};
struct cfdriver gpiorestart_cd = {
NULL, "gpiorestart", DV_DULL
};
void gpiorestart_reset(void);
int
gpiorestart_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "gpio-restart");
}
void
gpiorestart_attach(struct device *parent, struct device *self, void *aux)
{
struct gpiorestart_softc *sc = (struct gpiorestart_softc *)self;
struct fdt_attach_args *faa = aux;
int len;
len = OF_getproplen(faa->fa_node, "gpios");
if (len <= 0) {
printf(": no gpio\n");
return;
}
sc->sc_gpio = malloc(len, M_DEVBUF, M_WAITOK);
OF_getpropintarray(faa->fa_node, "gpios", sc->sc_gpio, len);
sc->sc_active_delay =
OF_getpropint(faa->fa_node, "active-delay", 100) * 1000;
sc->sc_inactive_delay =
OF_getpropint(faa->fa_node, "inactive-delay", 100) * 1000;
sc->sc_wait_delay =
OF_getpropint(faa->fa_node, "wait-delay", 3000) * 1000;
if (cpuresetfn == NULL)
cpuresetfn = gpiorestart_reset;
printf("\n");
}
void
gpiorestart_reset(void)
{
struct gpiorestart_softc *sc = gpiorestart_cd.cd_devs[0];
gpio_controller_config_pin(sc->sc_gpio, GPIO_CONFIG_OUTPUT);
gpio_controller_set_pin(sc->sc_gpio, 1);
delay(sc->sc_active_delay);
gpio_controller_set_pin(sc->sc_gpio, 0);
delay(sc->sc_inactive_delay);
gpio_controller_set_pin(sc->sc_gpio, 1);
delay(sc->sc_wait_delay);
}