1 /*- 2 * Copyright (c) 2012 Alexander Rybalko <ray@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/watchdog.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/rman.h> 36 37 #include <dev/fdt/fdt_common.h> 38 #include <dev/ofw/openfirm.h> 39 #include <dev/ofw/ofw_bus.h> 40 #include <dev/ofw/ofw_bus_subr.h> 41 42 #include <machine/bus.h> 43 #include <machine/cpufunc.h> 44 #include <machine/machdep.h> 45 #include <machine/fdt.h> 46 47 #include <arm/broadcom/bcm2835/bcm2835_wdog.h> 48 49 #define BCM2835_PASWORD 0x5a 50 51 #define BCM2835_WDOG_RESET 0 52 #define BCM2835_PASSWORD_MASK 0xff000000 53 #define BCM2835_PASSWORD_SHIFT 24 54 #define BCM2835_WDOG_TIME_MASK 0x000fffff 55 #define BCM2835_WDOG_TIME_SHIFT 0 56 57 #define READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r)) 58 #define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r), (_v)) 59 60 #define BCM2835_RSTC_WRCFG_CLR 0xffffffcf 61 #define BCM2835_RSTC_WRCFG_SET 0x00000030 62 #define BCM2835_RSTC_WRCFG_FULL_RESET 0x00000020 63 #define BCM2835_RSTC_RESET 0x00000102 64 65 #define BCM2835_RSTC_REG 0x00 66 #define BCM2835_RSTS_REG 0x04 67 #define BCM2835_WDOG_REG 0x08 68 69 static struct bcmwd_softc *bcmwd_lsc = NULL; 70 71 struct bcmwd_softc { 72 device_t dev; 73 struct resource * res; 74 bus_space_tag_t bst; 75 bus_space_handle_t bsh; 76 int wdog_armed; 77 int wdog_period; 78 char wdog_passwd; 79 struct mtx mtx; 80 }; 81 82 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error); 83 84 static int 85 bcmwd_probe(device_t dev) 86 { 87 88 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-wdt")) { 89 device_set_desc(dev, "BCM2708/2835 Watchdog"); 90 return (BUS_PROBE_DEFAULT); 91 } 92 93 return (ENXIO); 94 } 95 96 static int 97 bcmwd_attach(device_t dev) 98 { 99 struct bcmwd_softc *sc; 100 int rid; 101 102 if (bcmwd_lsc != NULL) 103 return (ENXIO); 104 105 sc = device_get_softc(dev); 106 sc->wdog_period = 7; 107 sc->wdog_passwd = BCM2835_PASWORD; 108 sc->wdog_armed = 0; 109 sc->dev = dev; 110 111 rid = 0; 112 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 113 if (sc->res == NULL) { 114 device_printf(dev, "could not allocate memory resource\n"); 115 return (ENXIO); 116 } 117 118 sc->bst = rman_get_bustag(sc->res); 119 sc->bsh = rman_get_bushandle(sc->res); 120 121 bcmwd_lsc = sc; 122 mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF); 123 EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0); 124 125 return (0); 126 } 127 128 static void 129 bcmwd_watchdog_fn(void *private, u_int cmd, int *error) 130 { 131 struct bcmwd_softc *sc; 132 uint64_t sec; 133 uint32_t ticks, reg; 134 135 sc = private; 136 mtx_lock(&sc->mtx); 137 138 cmd &= WD_INTERVAL; 139 140 if (cmd > 0) { 141 sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000; 142 ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK; 143 if (ticks == 0) { 144 /* 145 * Can't arm 146 * disable watchdog as watchdog(9) requires 147 */ 148 device_printf(sc->dev, 149 "Can't arm, timeout is less than 1 second\n"); 150 WRITE(sc, BCM2835_RSTC_REG, 151 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 152 BCM2835_RSTC_RESET); 153 mtx_unlock(&sc->mtx); 154 return; 155 } 156 157 reg = (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | ticks; 158 WRITE(sc, BCM2835_WDOG_REG, reg); 159 160 reg = READ(sc, BCM2835_RSTC_REG); 161 reg &= BCM2835_RSTC_WRCFG_CLR; 162 reg |= BCM2835_RSTC_WRCFG_FULL_RESET; 163 reg |= (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT); 164 WRITE(sc, BCM2835_RSTC_REG, reg); 165 166 *error = 0; 167 } 168 else 169 WRITE(sc, BCM2835_RSTC_REG, 170 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 171 BCM2835_RSTC_RESET); 172 173 mtx_unlock(&sc->mtx); 174 } 175 176 void 177 bcmwd_watchdog_reset() 178 { 179 180 if (bcmwd_lsc == NULL) 181 return; 182 183 WRITE(bcmwd_lsc, BCM2835_WDOG_REG, 184 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 10); 185 186 WRITE(bcmwd_lsc, BCM2835_RSTC_REG, 187 (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) | 188 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 189 BCM2835_RSTC_WRCFG_FULL_RESET); 190 } 191 192 static device_method_t bcmwd_methods[] = { 193 DEVMETHOD(device_probe, bcmwd_probe), 194 DEVMETHOD(device_attach, bcmwd_attach), 195 196 DEVMETHOD_END 197 }; 198 199 static driver_t bcmwd_driver = { 200 "bcmwd", 201 bcmwd_methods, 202 sizeof(struct bcmwd_softc), 203 }; 204 static devclass_t bcmwd_devclass; 205 206 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0); 207