aw_wdog.c revision 308325
1/*- 2 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * Copyright (c) 2016 Emmanuel Vadot <manu@bidouilliste.com> 4 * All rights reserved. 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#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/11/sys/arm/allwinner/aw_wdog.c 308325 2016-11-05 04:30:44Z mmel $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/watchdog.h> 33#include <sys/bus.h> 34#include <sys/kernel.h> 35#include <sys/lock.h> 36#include <sys/module.h> 37#include <sys/mutex.h> 38#include <sys/rman.h> 39 40#include <dev/fdt/fdt_common.h> 41#include <dev/ofw/openfirm.h> 42#include <dev/ofw/ofw_bus.h> 43#include <dev/ofw/ofw_bus_subr.h> 44 45#include <machine/bus.h> 46#include <machine/machdep.h> 47 48#include <arm/allwinner/aw_wdog.h> 49 50#define READ(_sc, _r) bus_read_4((_sc)->res, (_r)) 51#define WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v)) 52 53#define A10_WDOG_CTRL 0x00 54#define A31_WDOG_CTRL 0x10 55#define WDOG_CTRL_RESTART (1 << 0) 56#define A10_WDOG_MODE 0x04 57#define A31_WDOG_MODE 0x18 58#define A10_WDOG_MODE_INTVL_SHIFT 3 59#define A31_WDOG_MODE_INTVL_SHIFT 4 60#define A10_WDOG_MODE_RST_EN (1 << 1) 61#define WDOG_MODE_EN (1 << 0) 62#define A31_WDOG_CONFIG 0x14 63#define A31_WDOG_CONFIG_RST_EN_SYSTEM (1 << 0) 64#define A31_WDOG_CONFIG_RST_EN_INT (2 << 0) 65 66struct aw_wdog_interval { 67 uint64_t milliseconds; 68 unsigned int value; 69}; 70 71struct aw_wdog_interval wd_intervals[] = { 72 { 500, 0 }, 73 { 1000, 1 }, 74 { 2000, 2 }, 75 { 3000, 3 }, 76 { 4000, 4 }, 77 { 5000, 5 }, 78 { 6000, 6 }, 79 { 8000, 7 }, 80 { 10000, 8 }, 81 { 12000, 9 }, 82 { 14000, 10 }, 83 { 16000, 11 }, 84 { 0, 0 } /* sentinel */ 85}; 86 87static struct aw_wdog_softc *aw_wdog_sc = NULL; 88 89struct aw_wdog_softc { 90 device_t dev; 91 struct resource * res; 92 struct mtx mtx; 93 uint8_t wdog_ctrl; 94 uint8_t wdog_mode; 95 uint8_t wdog_mode_intvl_shift; 96 uint8_t wdog_mode_en; 97 uint8_t wdog_config; 98 uint8_t wdog_config_value; 99}; 100 101#define A10_WATCHDOG 1 102#define A31_WATCHDOG 2 103 104static struct ofw_compat_data compat_data[] = { 105 {"allwinner,sun4i-a10-wdt", A10_WATCHDOG}, 106 {"allwinner,sun6i-a31-wdt", A31_WATCHDOG}, 107 {NULL, 0} 108}; 109 110static void aw_wdog_watchdog_fn(void *private, u_int cmd, int *error); 111 112static int 113aw_wdog_probe(device_t dev) 114{ 115 struct aw_wdog_softc *sc; 116 117 sc = device_get_softc(dev); 118 119 if (!ofw_bus_status_okay(dev)) 120 return (ENXIO); 121 switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) { 122 case A10_WATCHDOG: 123 device_set_desc(dev, "Allwinner A10 Watchdog"); 124 return (BUS_PROBE_DEFAULT); 125 case A31_WATCHDOG: 126 device_set_desc(dev, "Allwinner A31 Watchdog"); 127 return (BUS_PROBE_DEFAULT); 128 } 129 return (ENXIO); 130} 131 132static int 133aw_wdog_attach(device_t dev) 134{ 135 struct aw_wdog_softc *sc; 136 int rid; 137 138 if (aw_wdog_sc != NULL) 139 return (ENXIO); 140 141 sc = device_get_softc(dev); 142 sc->dev = dev; 143 144 rid = 0; 145 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 146 if (sc->res == NULL) { 147 device_printf(dev, "could not allocate memory resource\n"); 148 return (ENXIO); 149 } 150 151 aw_wdog_sc = sc; 152 153 switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) { 154 case A10_WATCHDOG: 155 sc->wdog_ctrl = A10_WDOG_CTRL; 156 sc->wdog_mode = A10_WDOG_MODE; 157 sc->wdog_mode_intvl_shift = A10_WDOG_MODE_INTVL_SHIFT; 158 sc->wdog_mode_en = A10_WDOG_MODE_RST_EN | WDOG_MODE_EN; 159 break; 160 case A31_WATCHDOG: 161 sc->wdog_ctrl = A31_WDOG_CTRL; 162 sc->wdog_mode = A31_WDOG_MODE; 163 sc->wdog_mode_intvl_shift = A31_WDOG_MODE_INTVL_SHIFT; 164 sc->wdog_mode_en = WDOG_MODE_EN; 165 sc->wdog_config = A31_WDOG_CONFIG; 166 sc->wdog_config_value = A31_WDOG_CONFIG_RST_EN_SYSTEM; 167 break; 168 default: 169 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res); 170 return (ENXIO); 171 } 172 173 mtx_init(&sc->mtx, "AW Watchdog", "aw_wdog", MTX_DEF); 174 EVENTHANDLER_REGISTER(watchdog_list, aw_wdog_watchdog_fn, sc, 0); 175 return (0); 176} 177 178static void 179aw_wdog_watchdog_fn(void *private, u_int cmd, int *error) 180{ 181 struct aw_wdog_softc *sc; 182 uint64_t ms; 183 int i; 184 185 sc = private; 186 mtx_lock(&sc->mtx); 187 188 cmd &= WD_INTERVAL; 189 190 if (cmd > 0) { 191 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000; 192 i = 0; 193 while (wd_intervals[i].milliseconds && 194 (ms > wd_intervals[i].milliseconds)) 195 i++; 196 if (wd_intervals[i].milliseconds) { 197 WRITE(sc, sc->wdog_mode, 198 (wd_intervals[i].value << sc->wdog_mode_intvl_shift) | 199 sc->wdog_mode_en); 200 WRITE(sc, sc->wdog_ctrl, WDOG_CTRL_RESTART); 201 if (sc->wdog_config) 202 WRITE(sc, sc->wdog_config, 203 sc->wdog_config_value); 204 *error = 0; 205 } 206 else { 207 /* 208 * Can't arm 209 * disable watchdog as watchdog(9) requires 210 */ 211 device_printf(sc->dev, 212 "Can't arm, timeout is more than 16 sec\n"); 213 mtx_unlock(&sc->mtx); 214 WRITE(sc, sc->wdog_mode, 0); 215 return; 216 } 217 } 218 else 219 WRITE(sc, sc->wdog_mode, 0); 220 221 mtx_unlock(&sc->mtx); 222} 223 224void 225aw_wdog_watchdog_reset() 226{ 227 228 if (aw_wdog_sc == NULL) { 229 printf("Reset: watchdog device has not been initialized\n"); 230 return; 231 } 232 233 WRITE(aw_wdog_sc, aw_wdog_sc->wdog_mode, 234 (wd_intervals[0].value << aw_wdog_sc->wdog_mode_intvl_shift) | 235 aw_wdog_sc->wdog_mode_en); 236 if (aw_wdog_sc->wdog_config) 237 WRITE(aw_wdog_sc, aw_wdog_sc->wdog_config, 238 aw_wdog_sc->wdog_config_value); 239 while(1) 240 ; 241 242} 243 244static device_method_t aw_wdog_methods[] = { 245 DEVMETHOD(device_probe, aw_wdog_probe), 246 DEVMETHOD(device_attach, aw_wdog_attach), 247 248 DEVMETHOD_END 249}; 250 251static driver_t aw_wdog_driver = { 252 "aw_wdog", 253 aw_wdog_methods, 254 sizeof(struct aw_wdog_softc), 255}; 256static devclass_t aw_wdog_devclass; 257 258DRIVER_MODULE(aw_wdog, simplebus, aw_wdog_driver, aw_wdog_devclass, 0, 0); 259