2 * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@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/rockchip/rk30xx_wdog.h>
52 #define RK30_WDT_BASE 0x2004c000
53 #define RK30_WDT_PSIZE 0x100
56 #define RK30_WDT_READ(_sc, _r) bus_read_4((_sc)->res, (_r))
57 #define RK30_WDT_WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v))
59 #define WDOG_CTRL 0x00
60 #define WDOG_CTRL_EN (1 << 0)
61 #define WDOG_CTRL_RSP_MODE (1 << 1)
62 #define WDOG_CTRL_RST_PULSE (4 << 2)
63 #define WDOG_CTRL_RST 0xa
64 #define WDOG_TORR 0x04
65 #define WDOG_TORR_INTVL_SHIFT 0
66 #define WDOG_CCVR 0x08
68 #define WDOG_CRR_PWD 0x76
69 #define WDOG_STAT 0x10
72 static struct rk30_wd_softc *rk30_wd_sc = NULL;
74 struct rk30_wd_softc {
81 static void rk30_wd_watchdog_fn(void *private, u_int cmd, int *error);
84 rk30_wd_probe(device_t dev)
87 if (!ofw_bus_status_okay(dev))
90 if (ofw_bus_is_compatible(dev, "rockchip,rk30xx-wdt")) {
91 device_set_desc(dev, "Rockchip RK30XX Watchdog");
92 return (BUS_PROBE_DEFAULT);
99 rk30_wd_attach(device_t dev)
101 struct rk30_wd_softc *sc;
106 if (rk30_wd_sc != NULL)
109 sc = device_get_softc(dev);
112 node = ofw_bus_get_node(sc->dev);
113 if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) > 0)
114 sc->freq = cell / 1000000;
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, "RK30XX Watchdog", "rk30_wd", MTX_DEF);
127 EVENTHANDLER_REGISTER(watchdog_list, rk30_wd_watchdog_fn, sc, 0);
133 rk30_wd_watchdog_fn(void *private, u_int cmd, int *error)
135 struct rk30_wd_softc *sc;
145 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
146 m = 0xffff / sc->freq;
147 max = 0x7fffffff / sc->freq + 1;
149 while (m < max && m < ms) {
154 RK30_WDT_WRITE(sc, WDOG_TORR,
155 i << WDOG_TORR_INTVL_SHIFT);
156 RK30_WDT_WRITE(sc, WDOG_CTRL,
157 WDOG_CTRL_EN | WDOG_CTRL_RSP_MODE |
158 WDOG_CTRL_RST_PULSE);
159 RK30_WDT_WRITE(sc, WDOG_CRR, WDOG_CRR_PWD);
162 device_printf(sc->dev, "Can not be disabled\n");
163 mtx_unlock(&sc->mtx);
164 RK30_WDT_WRITE(sc, WDOG_CTRL, WDOG_CTRL_RST);
169 RK30_WDT_WRITE(sc, WDOG_CTRL, WDOG_CTRL_RST);
171 mtx_unlock(&sc->mtx);
175 rk30_wd_watchdog_reset()
177 bus_space_handle_t bsh;
179 bus_space_map(fdtbus_bs_tag, RK30_WDT_BASE, RK30_WDT_PSIZE, 0, &bsh);
180 bus_space_write_4(fdtbus_bs_tag, bsh, WDOG_TORR, 0);
181 bus_space_write_4(fdtbus_bs_tag, bsh, WDOG_CTRL,
182 WDOG_CTRL_EN | WDOG_CTRL_RSP_MODE | WDOG_CTRL_RST_PULSE);
187 static device_method_t rk30_wd_methods[] = {
188 DEVMETHOD(device_probe, rk30_wd_probe),
189 DEVMETHOD(device_attach, rk30_wd_attach),
194 static driver_t rk30_wd_driver = {
197 sizeof(struct rk30_wd_softc),
199 static devclass_t rk30_wd_devclass;
201 DRIVER_MODULE(rk30_wd, simplebus, rk30_wd_driver, rk30_wd_devclass, 0, 0);