1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Jessica Clarke <jrtc27@FreeBSD.org> 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/param.h> 37 #include <sys/bus.h> 38 #include <sys/types.h> 39 #include <sys/eventhandler.h> 40 #include <sys/kernel.h> 41 #include <sys/module.h> 42 #include <sys/reboot.h> 43 44 #include <machine/bus.h> 45 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 #include <dev/ofw/openfirm.h> 49 50 #include "syscon_if.h" 51 #include "syscon.h" 52 53 struct syscon_power_softc { 54 struct syscon *regmap; 55 uint32_t offset; 56 uint32_t value; 57 uint32_t mask; 58 bool reboot; 59 eventhandler_tag shutdown_tag; 60 }; 61 62 static void 63 syscon_power_shutdown_final(device_t dev, int howto) 64 { 65 struct syscon_power_softc *sc; 66 bool write; 67 68 sc = device_get_softc(dev); 69 if (sc->reboot) 70 write = (howto & RB_HALT) == 0; 71 else 72 write = (howto & RB_POWEROFF) != 0; 73 74 if (write) 75 SYSCON_MODIFY_4(sc->regmap, sc->offset, sc->mask, 76 sc->value & sc->mask); 77 } 78 79 static int 80 syscon_power_probe(device_t dev) 81 { 82 83 if (!ofw_bus_status_okay(dev)) 84 return (ENXIO); 85 86 if (ofw_bus_is_compatible(dev, "syscon-poweroff")) { 87 device_set_desc(dev, "Syscon poweroff"); 88 return (BUS_PROBE_DEFAULT); 89 } else if (ofw_bus_is_compatible(dev, "syscon-reboot")) { 90 device_set_desc(dev, "Syscon reboot"); 91 return (BUS_PROBE_DEFAULT); 92 } 93 94 return (ENXIO); 95 } 96 97 static int 98 syscon_power_attach(device_t dev) 99 { 100 struct syscon_power_softc *sc; 101 phandle_t node; 102 int error, len; 103 bool has_mask; 104 105 sc = device_get_softc(dev); 106 node = ofw_bus_get_node(dev); 107 108 if (!OF_hasprop(node, "regmap")) { 109 device_printf(dev, "could not find regmap\n"); 110 return (ENXIO); 111 } 112 113 error = syscon_get_by_ofw_property(dev, node, "regmap", &sc->regmap); 114 if (error != 0) { 115 device_printf(dev, "could not get syscon\n"); 116 return (ENXIO); 117 } 118 119 len = OF_getproplen(node, "offset"); 120 if (len != 4) { 121 device_printf(dev, "could not get offset\n"); 122 return (ENXIO); 123 } 124 125 OF_getencprop(node, "offset", &sc->offset, sizeof(sc->offset)); 126 127 /* Optional mask */ 128 has_mask = OF_hasprop(node, "mask"); 129 if (has_mask) { 130 len = OF_getproplen(node, "mask"); 131 if (len != 4) { 132 device_printf(dev, "cannot handle mask\n"); 133 return (ENXIO); 134 } 135 136 OF_getencprop(node, "mask", &sc->mask, sizeof(sc->mask)); 137 } else { 138 sc->mask = 0xffffffff; 139 } 140 141 /* 142 * From the device tree specification: 143 * 144 * Legacy usage: If a node doesn't contain a value property but 145 * contains a mask property, the mask property is used as the value. 146 */ 147 if (!OF_hasprop(node, "value")) { 148 if (!has_mask) { 149 device_printf(dev, "must have a value or a mask\n"); 150 return (ENXIO); 151 } 152 153 sc->value = sc->mask; 154 } else { 155 len = OF_getproplen(node, "value"); 156 if (len != 4) { 157 device_printf(dev, "cannot handle value\n"); 158 return (ENXIO); 159 } 160 161 OF_getencprop(node, "value", &sc->value, sizeof(sc->value)); 162 } 163 164 /* Handle reboot after shutdown_panic. */ 165 sc->reboot = ofw_bus_is_compatible(dev, "syscon-reboot"); 166 sc->shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final, 167 syscon_power_shutdown_final, dev, 168 sc->reboot ? SHUTDOWN_PRI_LAST + 150 : SHUTDOWN_PRI_LAST); 169 170 return (0); 171 } 172 173 static int 174 syscon_power_detach(device_t dev) 175 { 176 struct syscon_power_softc *sc; 177 178 sc = device_get_softc(dev); 179 EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_tag); 180 181 return (0); 182 } 183 184 static device_method_t syscon_power_methods[] = { 185 DEVMETHOD(device_probe, syscon_power_probe), 186 DEVMETHOD(device_attach, syscon_power_attach), 187 DEVMETHOD(device_detach, syscon_power_detach), 188 189 DEVMETHOD_END 190 }; 191 192 DEFINE_CLASS_0(syscon_power, syscon_power_driver, syscon_power_methods, 193 sizeof(struct syscon_power_softc)); 194 195 DRIVER_MODULE(syscon_power, simplebus, syscon_power_driver, NULL, NULL); 196