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