1 /* $OpenBSD: gpiorestart.c,v 1.1 2022/06/09 12:13:56 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2022 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/malloc.h> 22 23 #include <machine/bus.h> 24 #include <machine/fdt.h> 25 26 #include <dev/ofw/openfirm.h> 27 #include <dev/ofw/ofw_gpio.h> 28 #include <dev/ofw/fdt.h> 29 30 extern void (*cpuresetfn)(void); 31 32 struct gpiorestart_softc { 33 struct device sc_dev; 34 uint32_t *sc_gpio; 35 uint32_t sc_active_delay; 36 uint32_t sc_inactive_delay; 37 uint32_t sc_wait_delay; 38 }; 39 40 int gpiorestart_match(struct device *, void *, void *); 41 void gpiorestart_attach(struct device *, struct device *, void *); 42 43 const struct cfattach gpiorestart_ca = { 44 sizeof(struct gpiorestart_softc), gpiorestart_match, gpiorestart_attach 45 }; 46 47 struct cfdriver gpiorestart_cd = { 48 NULL, "gpiorestart", DV_DULL 49 }; 50 51 void gpiorestart_reset(void); 52 53 int 54 gpiorestart_match(struct device *parent, void *match, void *aux) 55 { 56 struct fdt_attach_args *faa = aux; 57 58 return OF_is_compatible(faa->fa_node, "gpio-restart"); 59 } 60 61 void 62 gpiorestart_attach(struct device *parent, struct device *self, void *aux) 63 { 64 struct gpiorestart_softc *sc = (struct gpiorestart_softc *)self; 65 struct fdt_attach_args *faa = aux; 66 int len; 67 68 len = OF_getproplen(faa->fa_node, "gpios"); 69 if (len <= 0) { 70 printf(": no gpio\n"); 71 return; 72 } 73 74 sc->sc_gpio = malloc(len, M_DEVBUF, M_WAITOK); 75 OF_getpropintarray(faa->fa_node, "gpios", sc->sc_gpio, len); 76 77 sc->sc_active_delay = 78 OF_getpropint(faa->fa_node, "active-delay", 100) * 1000; 79 sc->sc_inactive_delay = 80 OF_getpropint(faa->fa_node, "inactive-delay", 100) * 1000; 81 sc->sc_wait_delay = 82 OF_getpropint(faa->fa_node, "wait-delay", 3000) * 1000; 83 84 if (cpuresetfn == NULL) 85 cpuresetfn = gpiorestart_reset; 86 87 printf("\n"); 88 } 89 90 void 91 gpiorestart_reset(void) 92 { 93 struct gpiorestart_softc *sc = gpiorestart_cd.cd_devs[0]; 94 95 gpio_controller_config_pin(sc->sc_gpio, GPIO_CONFIG_OUTPUT); 96 gpio_controller_set_pin(sc->sc_gpio, 1); 97 delay(sc->sc_active_delay); 98 gpio_controller_set_pin(sc->sc_gpio, 0); 99 delay(sc->sc_inactive_delay); 100 gpio_controller_set_pin(sc->sc_gpio, 1); 101 delay(sc->sc_wait_delay); 102 } 103