syscon.c revision 1.3
1/* $OpenBSD: syscon.c,v 1.3 2017/10/09 11:47:53 kettenis Exp $ */ 2/* 3 * Copyright (c) 2017 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 22#include <machine/bus.h> 23#include <machine/fdt.h> 24 25#include <dev/ofw/openfirm.h> 26#include <dev/ofw/ofw_misc.h> 27#include <dev/ofw/fdt.h> 28 29extern void (*cpuresetfn)(void); 30extern void (*powerdownfn)(void); 31 32struct syscon_softc { 33 struct device sc_dev; 34 bus_space_tag_t sc_iot; 35 bus_space_handle_t sc_ioh; 36 uint32_t sc_regmap; 37 bus_size_t sc_offset; 38 uint32_t sc_mask; 39}; 40 41struct syscon_softc *syscon_reboot_sc; 42struct syscon_softc *syscon_poweroff_sc; 43 44int syscon_match(struct device *, void *, void *); 45void syscon_attach(struct device *, struct device *, void *); 46 47struct cfattach syscon_ca = { 48 sizeof(struct syscon_softc), syscon_match, syscon_attach 49}; 50 51struct cfdriver syscon_cd = { 52 NULL, "syscon", DV_DULL 53}; 54 55void syscon_reset(void); 56void syscon_powerdown(void); 57 58int 59syscon_match(struct device *parent, void *match, void *aux) 60{ 61 struct fdt_attach_args *faa = aux; 62 63 return OF_is_compatible(faa->fa_node, "syscon") || 64 OF_is_compatible(faa->fa_node, "syscon-reboot") || 65 OF_is_compatible(faa->fa_node, "syscon-poweroff"); 66} 67 68void 69syscon_attach(struct device *parent, struct device *self, void *aux) 70{ 71 struct syscon_softc *sc = (struct syscon_softc *)self; 72 struct fdt_attach_args *faa = aux; 73 char name[32]; 74 75 if (OF_is_compatible(faa->fa_node, "syscon")) { 76 if (faa->fa_nreg < 1) { 77 printf(": no registers\n"); 78 return; 79 } 80 81 sc->sc_iot = faa->fa_iot; 82 83 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 84 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 85 printf(": can't map registers\n"); 86 return; 87 } 88 89 regmap_register(faa->fa_node, sc->sc_iot, sc->sc_ioh, 90 faa->fa_reg[0].size); 91 92 if (OF_getprop(faa->fa_node, "name", name, sizeof(name)) > 0) { 93 name[sizeof(name) - 1] = 0; 94 printf(": \"%s\"", name); 95 } 96 97 printf("\n"); 98 } 99 100 if (OF_is_compatible(faa->fa_node, "syscon-reboot") || 101 OF_is_compatible(faa->fa_node, "syscon-poweroff")) { 102 printf("\n"); 103 104 sc->sc_regmap = OF_getpropint(faa->fa_node, "regmap", 0); 105 if (sc->sc_regmap == 0) 106 return; 107 108 if (OF_getproplen(faa->fa_node, "offset") != sizeof(uint32_t) || 109 OF_getproplen(faa->fa_node, "mask") != sizeof(uint32_t)) 110 return; 111 112 sc->sc_offset = OF_getpropint(faa->fa_node, "offset", 0); 113 sc->sc_mask = OF_getpropint(faa->fa_node, "mask", 0); 114 115 if (OF_is_compatible(faa->fa_node, "syscon-reboot")) { 116 syscon_reboot_sc = sc; 117 cpuresetfn = syscon_reset; 118 } else if (OF_is_compatible(faa->fa_node, "syscon-poweroff")) { 119 syscon_poweroff_sc = sc; 120 powerdownfn = syscon_powerdown; 121 } 122 } 123} 124 125void 126syscon_reset(void) 127{ 128 struct syscon_softc *sc = syscon_reboot_sc; 129 struct regmap *rm; 130 131 rm = regmap_byphandle(sc->sc_regmap); 132 if (rm == NULL) 133 return; 134 135 regmap_write_4(rm, sc->sc_offset, sc->sc_mask); 136 delay(1000000); 137} 138 139void 140syscon_powerdown(void) 141{ 142 struct syscon_softc *sc = syscon_poweroff_sc; 143 struct regmap *rm; 144 145 rm = regmap_byphandle(sc->sc_regmap); 146 if (rm == NULL) 147 return; 148 149 regmap_write_4(rm, sc->sc_offset, sc->sc_mask); 150 delay(1000000); 151} 152