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/ofw/openfirm.h> 38 #include <dev/ofw/ofw_bus.h> 39 #include <dev/ofw/ofw_bus_subr.h> 40 41 #include <machine/bus.h> 42 #include <machine/machdep.h> 43 44 #include <arm/broadcom/bcm2835/bcm2835_wdog.h> 45 46 #define BCM2835_PASWORD 0x5a 47 48 #define BCM2835_WDOG_RESET 0 49 #define BCM2835_PASSWORD_MASK 0xff000000 50 #define BCM2835_PASSWORD_SHIFT 24 51 #define BCM2835_WDOG_TIME_MASK 0x000fffff 52 #define BCM2835_WDOG_TIME_SHIFT 0 53 54 #define READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset) 55 #define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset, (_v)) 56 57 #define BCM2835_RSTC_WRCFG_CLR 0xffffffcf 58 #define BCM2835_RSTC_WRCFG_SET 0x00000030 59 #define BCM2835_RSTC_WRCFG_FULL_RESET 0x00000020 60 #define BCM2835_RSTC_RESET 0x00000102 61 62 #define BCM2835_RSTC_REG 0x00 63 #define BCM2835_RSTS_REG 0x04 64 #define BCM2835_WDOG_REG 0x08 65 66 static struct bcmwd_softc *bcmwd_lsc = NULL; 67 68 struct bcmwd_softc { 69 device_t dev; 70 struct resource * res; 71 bus_space_tag_t bst; 72 bus_space_handle_t bsh; 73 int wdog_armed; 74 int wdog_period; 75 char wdog_passwd; 76 struct mtx mtx; 77 int regs_offset; 78 }; 79 80 #define BSD_DTB 1 81 #define UPSTREAM_DTB 2 82 #define UPSTREAM_DTB_REGS_OFFSET 0x1c 83 84 static struct ofw_compat_data compat_data[] = { 85 {"broadcom,bcm2835-wdt", BSD_DTB}, 86 {"brcm,bcm2835-pm-wdt", UPSTREAM_DTB}, 87 {NULL, 0} 88 }; 89 90 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error); 91 92 static int 93 bcmwd_probe(device_t dev) 94 { 95 96 if (!ofw_bus_status_okay(dev)) 97 return (ENXIO); 98 99 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 100 return (ENXIO); 101 102 device_set_desc(dev, "BCM2708/2835 Watchdog"); 103 104 return (BUS_PROBE_DEFAULT); 105 } 106 107 static int 108 bcmwd_attach(device_t dev) 109 { 110 struct bcmwd_softc *sc; 111 int rid; 112 113 if (bcmwd_lsc != NULL) 114 return (ENXIO); 115 116 sc = device_get_softc(dev); 117 sc->wdog_period = 7; 118 sc->wdog_passwd = BCM2835_PASWORD; 119 sc->wdog_armed = 0; 120 sc->dev = dev; 121 122 rid = 0; 123 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 124 if (sc->res == NULL) { 125 device_printf(dev, "could not allocate memory resource\n"); 126 return (ENXIO); 127 } 128 129 sc->bst = rman_get_bustag(sc->res); 130 sc->bsh = rman_get_bushandle(sc->res); 131 132 /* compensate base address difference */ 133 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data 134 == UPSTREAM_DTB) 135 sc->regs_offset = UPSTREAM_DTB_REGS_OFFSET; 136 137 bcmwd_lsc = sc; 138 mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF); 139 EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0); 140 141 return (0); 142 } 143 144 static void 145 bcmwd_watchdog_fn(void *private, u_int cmd, int *error) 146 { 147 struct bcmwd_softc *sc; 148 uint64_t sec; 149 uint32_t ticks, reg; 150 151 sc = private; 152 mtx_lock(&sc->mtx); 153 154 cmd &= WD_INTERVAL; 155 156 if (cmd > 0) { 157 sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000; 158 if (sec == 0 || sec > 15) { 159 /* 160 * Can't arm 161 * disable watchdog as watchdog(9) requires 162 */ 163 device_printf(sc->dev, 164 "Can't arm, timeout must be between 1-15 seconds\n"); 165 WRITE(sc, BCM2835_RSTC_REG, 166 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 167 BCM2835_RSTC_RESET); 168 mtx_unlock(&sc->mtx); 169 return; 170 } 171 172 ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK; 173 reg = (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | ticks; 174 WRITE(sc, BCM2835_WDOG_REG, reg); 175 176 reg = READ(sc, BCM2835_RSTC_REG); 177 reg &= BCM2835_RSTC_WRCFG_CLR; 178 reg |= BCM2835_RSTC_WRCFG_FULL_RESET; 179 reg |= (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT); 180 WRITE(sc, BCM2835_RSTC_REG, reg); 181 182 *error = 0; 183 } 184 else 185 WRITE(sc, BCM2835_RSTC_REG, 186 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 187 BCM2835_RSTC_RESET); 188 189 mtx_unlock(&sc->mtx); 190 } 191 192 void 193 bcmwd_watchdog_reset(void) 194 { 195 196 if (bcmwd_lsc == NULL) 197 return; 198 199 WRITE(bcmwd_lsc, BCM2835_WDOG_REG, 200 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 10); 201 202 WRITE(bcmwd_lsc, BCM2835_RSTC_REG, 203 (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) | 204 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 205 BCM2835_RSTC_WRCFG_FULL_RESET); 206 } 207 208 static device_method_t bcmwd_methods[] = { 209 DEVMETHOD(device_probe, bcmwd_probe), 210 DEVMETHOD(device_attach, bcmwd_attach), 211 212 DEVMETHOD_END 213 }; 214 215 static driver_t bcmwd_driver = { 216 "bcmwd", 217 bcmwd_methods, 218 sizeof(struct bcmwd_softc), 219 }; 220 static devclass_t bcmwd_devclass; 221 222 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0); 223