1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 Alexander Rybalko <ray@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/eventhandler.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/reboot.h> 39 #include <sys/rman.h> 40 #include <sys/systm.h> 41 #include <sys/watchdog.h> 42 43 #include <dev/ofw/openfirm.h> 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/ofw_bus_subr.h> 46 47 #include <machine/bus.h> 48 #include <machine/machdep.h> 49 50 #include <arm/broadcom/bcm2835/bcm2835_wdog.h> 51 52 #define BCM2835_PASSWORD 0x5a 53 54 #define BCM2835_WDOG_RESET 0 55 #define BCM2835_PASSWORD_MASK 0xff000000 56 #define BCM2835_PASSWORD_SHIFT 24 57 #define BCM2835_WDOG_TIME_MASK 0x000fffff 58 #define BCM2835_WDOG_TIME_SHIFT 0 59 60 #define READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset) 61 #define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset, (_v)) 62 63 #define BCM2835_RSTC_WRCFG_CLR 0xffffffcf 64 #define BCM2835_RSTC_WRCFG_SET 0x00000030 65 #define BCM2835_RSTC_WRCFG_FULL_RESET 0x00000020 66 #define BCM2835_RSTC_RESET 0x00000102 67 68 #define BCM2835_RSTC_REG 0x00 69 #define BCM2835_RSTS_REG 0x04 70 #define BCM2835_WDOG_REG 0x08 71 72 static struct bcmwd_softc *bcmwd_lsc = NULL; 73 74 struct bcmwd_softc { 75 device_t dev; 76 struct resource * res; 77 bus_space_tag_t bst; 78 bus_space_handle_t bsh; 79 int wdog_armed; 80 int wdog_period; 81 char wdog_passwd; 82 struct mtx mtx; 83 int regs_offset; 84 }; 85 86 #define BSD_DTB 1 87 #define UPSTREAM_DTB 2 88 #define UPSTREAM_DTB_REGS_OFFSET 0x1c 89 90 static struct ofw_compat_data compat_data[] = { 91 {"broadcom,bcm2835-wdt", BSD_DTB}, 92 {"brcm,bcm2835-pm-wdt", UPSTREAM_DTB}, 93 {"brcm,bcm2835-pm", UPSTREAM_DTB}, 94 {NULL, 0} 95 }; 96 97 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error); 98 static void bcmwd_reboot_system(void *, int); 99 100 static int 101 bcmwd_probe(device_t dev) 102 { 103 104 if (!ofw_bus_status_okay(dev)) 105 return (ENXIO); 106 107 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 108 return (ENXIO); 109 110 device_set_desc(dev, "BCM2708/2835 Watchdog"); 111 112 return (BUS_PROBE_DEFAULT); 113 } 114 115 static int 116 bcmwd_attach(device_t dev) 117 { 118 struct bcmwd_softc *sc; 119 int rid; 120 121 if (bcmwd_lsc != NULL) 122 return (ENXIO); 123 124 sc = device_get_softc(dev); 125 sc->wdog_period = 7; 126 sc->wdog_passwd = BCM2835_PASSWORD; 127 sc->wdog_armed = 0; 128 sc->dev = dev; 129 130 rid = 0; 131 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 132 if (sc->res == NULL) { 133 device_printf(dev, "could not allocate memory resource\n"); 134 return (ENXIO); 135 } 136 137 sc->bst = rman_get_bustag(sc->res); 138 sc->bsh = rman_get_bushandle(sc->res); 139 140 /* compensate base address difference */ 141 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data 142 == UPSTREAM_DTB) 143 sc->regs_offset = UPSTREAM_DTB_REGS_OFFSET; 144 145 bcmwd_lsc = sc; 146 mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF); 147 EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0); 148 149 /* 150 * Handle reboot events. This needs to happen with slightly greater 151 * priority than the PSCI handler, since PSCI reset is not properly 152 * implemented on the Pi and it just puts the Pi into a halt 153 * state. 154 */ 155 EVENTHANDLER_REGISTER(shutdown_final, bcmwd_reboot_system, sc, 156 SHUTDOWN_PRI_LAST-1); 157 158 return (0); 159 } 160 161 static void 162 bcmwd_watchdog_fn(void *private, u_int cmd, int *error) 163 { 164 struct bcmwd_softc *sc; 165 uint64_t sec; 166 uint32_t ticks, reg; 167 168 sc = private; 169 mtx_lock(&sc->mtx); 170 171 cmd &= WD_INTERVAL; 172 173 if (cmd > 0) { 174 sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000; 175 if (sec == 0 || sec > 15) { 176 /* 177 * Can't arm 178 * disable watchdog as watchdog(9) requires 179 */ 180 device_printf(sc->dev, 181 "Can't arm, timeout must be between 1-15 seconds\n"); 182 WRITE(sc, BCM2835_RSTC_REG, 183 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 184 BCM2835_RSTC_RESET); 185 mtx_unlock(&sc->mtx); 186 *error = EINVAL; 187 return; 188 } 189 190 ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK; 191 reg = (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | ticks; 192 WRITE(sc, BCM2835_WDOG_REG, reg); 193 194 reg = READ(sc, BCM2835_RSTC_REG); 195 reg &= BCM2835_RSTC_WRCFG_CLR; 196 reg |= BCM2835_RSTC_WRCFG_FULL_RESET; 197 reg |= (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT); 198 WRITE(sc, BCM2835_RSTC_REG, reg); 199 200 *error = 0; 201 } 202 else 203 WRITE(sc, BCM2835_RSTC_REG, 204 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 205 BCM2835_RSTC_RESET); 206 207 mtx_unlock(&sc->mtx); 208 } 209 210 void 211 bcmwd_watchdog_reset(void) 212 { 213 214 if (bcmwd_lsc == NULL) 215 return; 216 217 WRITE(bcmwd_lsc, BCM2835_WDOG_REG, 218 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 10); 219 220 WRITE(bcmwd_lsc, BCM2835_RSTC_REG, 221 (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) | 222 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 223 BCM2835_RSTC_WRCFG_FULL_RESET); 224 } 225 226 static void 227 bcmwd_reboot_system(void *sc, int howto) 228 { 229 int cmd, error = 0; 230 231 /* Only handle reset. */ 232 if (howto & RB_HALT || howto & RB_POWEROFF) 233 return; 234 235 printf("Resetting system ... "); 236 237 cmd = WD_TO_1SEC; 238 bcmwd_watchdog_fn(sc, cmd, &error); 239 240 /* Wait for watchdog timeout. */ 241 DELAY(2000000); 242 243 /* Not reached ... one hopes. */ 244 printf("failed to reset (errno %d).\n", error); 245 } 246 247 static device_method_t bcmwd_methods[] = { 248 DEVMETHOD(device_probe, bcmwd_probe), 249 DEVMETHOD(device_attach, bcmwd_attach), 250 251 DEVMETHOD_END 252 }; 253 254 static driver_t bcmwd_driver = { 255 "bcmwd", 256 bcmwd_methods, 257 sizeof(struct bcmwd_softc), 258 }; 259 static devclass_t bcmwd_devclass; 260 261 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0); 262