2 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/watchdog.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
39 #include <dev/fdt/fdt_common.h>
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
44 #include <machine/bus.h>
45 #include <machine/cpufunc.h>
46 #include <machine/machdep.h>
47 #include <machine/fdt.h>
49 #include <arm/allwinner/a10_wdog.h>
51 #define READ(_sc, _r) bus_read_4((_sc)->res, (_r))
52 #define WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v))
54 #define WDOG_CTRL 0x00
55 #define WDOG_CTRL_RESTART (1 << 0)
56 #define WDOG_MODE 0x04
57 #define WDOG_MODE_INTVL_SHIFT 3
58 #define WDOG_MODE_RST_EN (1 << 1)
59 #define WDOG_MODE_EN (1 << 0)
61 struct a10wd_interval {
62 uint64_t milliseconds;
66 struct a10wd_interval wd_intervals[] = {
79 { 0, 0 } /* sentinel */
82 static struct a10wd_softc *a10wd_sc = NULL;
86 struct resource * res;
90 static void a10wd_watchdog_fn(void *private, u_int cmd, int *error);
93 a10wd_probe(device_t dev)
96 if (ofw_bus_is_compatible(dev, "allwinner,sun4i-wdt")) {
97 device_set_desc(dev, "Allwinner A10 Watchdog");
98 return (BUS_PROBE_DEFAULT);
105 a10wd_attach(device_t dev)
107 struct a10wd_softc *sc;
110 if (a10wd_sc != NULL)
113 sc = device_get_softc(dev);
117 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
118 if (sc->res == NULL) {
119 device_printf(dev, "could not allocate memory resource\n");
124 mtx_init(&sc->mtx, "A10 Watchdog", "a10wd", MTX_DEF);
125 EVENTHANDLER_REGISTER(watchdog_list, a10wd_watchdog_fn, sc, 0);
131 a10wd_watchdog_fn(void *private, u_int cmd, int *error)
133 struct a10wd_softc *sc;
143 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
145 while (wd_intervals[i].milliseconds &&
146 (ms > wd_intervals[i].milliseconds))
148 if (wd_intervals[i].milliseconds) {
150 (wd_intervals[i].value << WDOG_MODE_INTVL_SHIFT) |
151 WDOG_MODE_EN | WDOG_MODE_RST_EN);
152 WRITE(sc, WDOG_CTRL, WDOG_CTRL_RESTART);
156 WRITE(sc, WDOG_MODE, 0);
158 mtx_unlock(&sc->mtx);
162 a10wd_watchdog_reset()
165 if (a10wd_sc == NULL) {
166 printf("Reset: watchdog device has not been initialized\n");
170 WRITE(a10wd_sc, WDOG_MODE,
171 (wd_intervals[0].value << WDOG_MODE_INTVL_SHIFT) |
172 WDOG_MODE_EN | WDOG_MODE_RST_EN);
179 static device_method_t a10wd_methods[] = {
180 DEVMETHOD(device_probe, a10wd_probe),
181 DEVMETHOD(device_attach, a10wd_attach),
186 static driver_t a10wd_driver = {
189 sizeof(struct a10wd_softc),
191 static devclass_t a10wd_devclass;
193 DRIVER_MODULE(a10wd, simplebus, a10wd_driver, a10wd_devclass, 0, 0);