1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Jessica Clarke <[email protected]> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Driver for simple syscon poweroff and reset devices. The device tree 30 * specifications are fully described at: 31 * 32 * https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-poweroff.txt 33 * https://www.kernel.org/doc/Documentation/devicetree/bindings/power/reset/syscon-reboot.txt 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/bus.h> 41 #include <sys/types.h> 42 #include <sys/eventhandler.h> 43 #include <sys/kernel.h> 44 #include <sys/module.h> 45 #include <sys/reboot.h> 46 47 #include <machine/bus.h> 48 49 #include <dev/ofw/ofw_bus.h> 50 #include <dev/ofw/ofw_bus_subr.h> 51 #include <dev/ofw/openfirm.h> 52 53 #include "syscon_if.h" 54 #include "syscon.h" 55 56 struct syscon_power_softc { 57 struct syscon *regmap; 58 uint32_t offset; 59 uint32_t value; 60 uint32_t mask; 61 bool reboot; 62 eventhandler_tag shutdown_tag; 63 }; 64 65 static void 66 syscon_power_shutdown_final(device_t dev, int howto) 67 { 68 struct syscon_power_softc *sc; 69 bool write; 70 71 sc = device_get_softc(dev); 72 if (sc->reboot) 73 write = (howto & RB_HALT) == 0; 74 else 75 write = (howto & RB_POWEROFF) != 0; 76 77 if (write) 78 SYSCON_MODIFY_4(sc->regmap, sc->offset, sc->mask, 79 sc->value & sc->mask); 80 } 81 82 static int 83 syscon_power_probe(device_t dev) 84 { 85 86 if (!ofw_bus_status_okay(dev)) 87 return (ENXIO); 88 89 if (ofw_bus_is_compatible(dev, "syscon-poweroff")) { 90 device_set_desc(dev, "Syscon poweroff"); 91 return (BUS_PROBE_DEFAULT); 92 } else if (ofw_bus_is_compatible(dev, "syscon-reboot")) { 93 device_set_desc(dev, "Syscon reboot"); 94 return (BUS_PROBE_DEFAULT); 95 } 96 97 return (ENXIO); 98 } 99 100 static int 101 syscon_power_attach(device_t dev) 102 { 103 struct syscon_power_softc *sc; 104 phandle_t node; 105 int error, len; 106 bool has_mask; 107 108 sc = device_get_softc(dev); 109 node = ofw_bus_get_node(dev); 110 111 if (!OF_hasprop(node, "regmap")) { 112 device_printf(dev, "could not find regmap\n"); 113 return (ENXIO); 114 } 115 116 error = syscon_get_by_ofw_property(dev, node, "regmap", &sc->regmap); 117 if (error != 0) { 118 device_printf(dev, "could not get syscon\n"); 119 return (ENXIO); 120 } 121 122 len = OF_getproplen(node, "offset"); 123 if (len != 4) { 124 device_printf(dev, "could not get offset\n"); 125 return (ENXIO); 126 } 127 128 OF_getencprop(node, "offset", &sc->offset, sizeof(sc->offset)); 129 130 /* Optional mask */ 131 has_mask = OF_hasprop(node, "mask"); 132 if (has_mask) { 133 len = OF_getproplen(node, "mask"); 134 if (len != 4) { 135 device_printf(dev, "cannot handle mask\n"); 136 return (ENXIO); 137 } 138 139 OF_getencprop(node, "mask", &sc->mask, sizeof(sc->mask)); 140 } else { 141 sc->mask = 0xffffffff; 142 } 143 144 /* 145 * From the device tree specification: 146 * 147 * Legacy usage: If a node doesn't contain a value property but 148 * contains a mask property, the mask property is used as the value. 149 */ 150 if (!OF_hasprop(node, "value")) { 151 if (!has_mask) { 152 device_printf(dev, "must have a value or a mask\n"); 153 return (ENXIO); 154 } 155 156 sc->value = sc->mask; 157 } else { 158 len = OF_getproplen(node, "value"); 159 if (len != 4) { 160 device_printf(dev, "cannot handle value\n"); 161 return (ENXIO); 162 } 163 164 OF_getencprop(node, "value", &sc->value, sizeof(sc->value)); 165 } 166 167 sc->reboot = ofw_bus_is_compatible(dev, "syscon-reboot"); 168 sc->shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final, 169 syscon_power_shutdown_final, dev, SHUTDOWN_PRI_LAST); 170 171 return (0); 172 } 173 174 static int 175 syscon_power_detach(device_t dev) 176 { 177 struct syscon_power_softc *sc; 178 179 sc = device_get_softc(dev); 180 EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_tag); 181 182 return (0); 183 } 184 185 static device_method_t syscon_power_methods[] = { 186 DEVMETHOD(device_probe, syscon_power_probe), 187 DEVMETHOD(device_attach, syscon_power_attach), 188 DEVMETHOD(device_detach, syscon_power_detach), 189 190 DEVMETHOD_END 191 }; 192 193 DEFINE_CLASS_0(syscon_power, syscon_power_driver, syscon_power_methods, 194 sizeof(struct syscon_power_softc)); 195 static devclass_t syscon_power_devclass; 196 197 DRIVER_MODULE(syscon_power, simplebus, syscon_power_driver, 198 syscon_power_devclass, NULL, NULL); 199