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>
48 #include <arm/allwinner/a10_wdog.h>
50 #define READ(_sc, _r) bus_read_4((_sc)->res, (_r))
51 #define WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v))
53 #define WDOG_CTRL 0x00
54 #define WDOG_CTRL_RESTART (1 << 0)
55 #define WDOG_MODE 0x04
56 #define WDOG_MODE_INTVL_SHIFT 3
57 #define WDOG_MODE_RST_EN (1 << 1)
58 #define WDOG_MODE_EN (1 << 0)
60 struct a10wd_interval {
61 uint64_t milliseconds;
65 struct a10wd_interval wd_intervals[] = {
78 { 0, 0 } /* sentinel */
81 static struct a10wd_softc *a10wd_sc = NULL;
85 struct resource * res;
89 static void a10wd_watchdog_fn(void *private, u_int cmd, int *error);
92 a10wd_probe(device_t dev)
95 if (!ofw_bus_status_okay(dev))
98 if (ofw_bus_is_compatible(dev, "allwinner,sun4i-wdt")) {
99 device_set_desc(dev, "Allwinner A10 Watchdog");
100 return (BUS_PROBE_DEFAULT);
107 a10wd_attach(device_t dev)
109 struct a10wd_softc *sc;
112 if (a10wd_sc != NULL)
115 sc = device_get_softc(dev);
119 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
120 if (sc->res == NULL) {
121 device_printf(dev, "could not allocate memory resource\n");
126 mtx_init(&sc->mtx, "A10 Watchdog", "a10wd", MTX_DEF);
127 EVENTHANDLER_REGISTER(watchdog_list, a10wd_watchdog_fn, sc, 0);
133 a10wd_watchdog_fn(void *private, u_int cmd, int *error)
135 struct a10wd_softc *sc;
145 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
147 while (wd_intervals[i].milliseconds &&
148 (ms > wd_intervals[i].milliseconds))
150 if (wd_intervals[i].milliseconds) {
152 (wd_intervals[i].value << WDOG_MODE_INTVL_SHIFT) |
153 WDOG_MODE_EN | WDOG_MODE_RST_EN);
154 WRITE(sc, WDOG_CTRL, WDOG_CTRL_RESTART);
160 * disable watchdog as watchdog(9) requires
162 device_printf(sc->dev,
163 "Can't arm, timeout is more than 16 sec\n");
164 mtx_unlock(&sc->mtx);
165 WRITE(sc, WDOG_MODE, 0);
170 WRITE(sc, WDOG_MODE, 0);
172 mtx_unlock(&sc->mtx);
176 a10wd_watchdog_reset()
179 if (a10wd_sc == NULL) {
180 printf("Reset: watchdog device has not been initialized\n");
184 WRITE(a10wd_sc, WDOG_MODE,
185 (wd_intervals[0].value << WDOG_MODE_INTVL_SHIFT) |
186 WDOG_MODE_EN | WDOG_MODE_RST_EN);
193 static device_method_t a10wd_methods[] = {
194 DEVMETHOD(device_probe, a10wd_probe),
195 DEVMETHOD(device_attach, a10wd_attach),
200 static driver_t a10wd_driver = {
203 sizeof(struct a10wd_softc),
205 static devclass_t a10wd_devclass;
207 DRIVER_MODULE(a10wd, simplebus, a10wd_driver, a10wd_devclass, 0, 0);