2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Alexander Rybalko <ray@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/reboot.h>
40 #include <sys/systm.h>
41 #include <sys/watchdog.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
47 #include <machine/bus.h>
48 #include <machine/machdep.h>
50 #include <arm/broadcom/bcm2835/bcm2835_wdog.h>
52 #define BCM2835_PASSWORD 0x5a
54 #define BCM2835_WDOG_RESET 0
55 #define BCM2835_PASSWORD_MASK 0xff000000
56 #define BCM2835_PASSWORD_SHIFT 24
57 #define BCM2835_WDOG_TIME_MASK 0x000fffff
58 #define BCM2835_WDOG_TIME_SHIFT 0
60 #define READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset)
61 #define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset, (_v))
63 #define BCM2835_RSTC_WRCFG_CLR 0xffffffcf
64 #define BCM2835_RSTC_WRCFG_SET 0x00000030
65 #define BCM2835_RSTC_WRCFG_FULL_RESET 0x00000020
66 #define BCM2835_RSTC_RESET 0x00000102
68 #define BCM2835_RSTC_REG 0x00
69 #define BCM2835_RSTS_REG 0x04
70 #define BCM2835_WDOG_REG 0x08
72 static struct bcmwd_softc *bcmwd_lsc = NULL;
76 struct resource * res;
78 bus_space_handle_t bsh;
87 #define UPSTREAM_DTB 2
88 #define UPSTREAM_DTB_REGS_OFFSET 0x1c
90 static struct ofw_compat_data compat_data[] = {
91 {"broadcom,bcm2835-wdt", BSD_DTB},
92 {"brcm,bcm2835-pm-wdt", UPSTREAM_DTB},
93 {"brcm,bcm2835-pm", UPSTREAM_DTB},
97 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error);
98 static void bcmwd_reboot_system(void *, int);
101 bcmwd_probe(device_t dev)
104 if (!ofw_bus_status_okay(dev))
107 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
110 device_set_desc(dev, "BCM2708/2835 Watchdog");
112 return (BUS_PROBE_DEFAULT);
116 bcmwd_attach(device_t dev)
118 struct bcmwd_softc *sc;
121 if (bcmwd_lsc != NULL)
124 sc = device_get_softc(dev);
126 sc->wdog_passwd = BCM2835_PASSWORD;
131 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
132 if (sc->res == NULL) {
133 device_printf(dev, "could not allocate memory resource\n");
137 sc->bst = rman_get_bustag(sc->res);
138 sc->bsh = rman_get_bushandle(sc->res);
140 /* compensate base address difference */
141 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data
143 sc->regs_offset = UPSTREAM_DTB_REGS_OFFSET;
146 mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF);
147 EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0);
150 * Handle reboot events. This needs to happen with slightly greater
151 * priority than the PSCI handler, since PSCI reset is not properly
152 * implemented on the Pi and it just puts the Pi into a halt
155 EVENTHANDLER_REGISTER(shutdown_final, bcmwd_reboot_system, sc,
156 SHUTDOWN_PRI_LAST-1);
162 bcmwd_watchdog_fn(void *private, u_int cmd, int *error)
164 struct bcmwd_softc *sc;
174 sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000;
175 if (sec == 0 || sec > 15) {
178 * disable watchdog as watchdog(9) requires
180 device_printf(sc->dev,
181 "Can't arm, timeout must be between 1-15 seconds\n");
182 WRITE(sc, BCM2835_RSTC_REG,
183 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
185 mtx_unlock(&sc->mtx);
190 ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK;
191 reg = (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | ticks;
192 WRITE(sc, BCM2835_WDOG_REG, reg);
194 reg = READ(sc, BCM2835_RSTC_REG);
195 reg &= BCM2835_RSTC_WRCFG_CLR;
196 reg |= BCM2835_RSTC_WRCFG_FULL_RESET;
197 reg |= (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT);
198 WRITE(sc, BCM2835_RSTC_REG, reg);
203 WRITE(sc, BCM2835_RSTC_REG,
204 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
207 mtx_unlock(&sc->mtx);
211 bcmwd_watchdog_reset(void)
214 if (bcmwd_lsc == NULL)
217 WRITE(bcmwd_lsc, BCM2835_WDOG_REG,
218 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 10);
220 WRITE(bcmwd_lsc, BCM2835_RSTC_REG,
221 (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) |
222 (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
223 BCM2835_RSTC_WRCFG_FULL_RESET);
227 bcmwd_reboot_system(void *sc, int howto)
231 /* Only handle reset. */
232 if (howto & RB_HALT || howto & RB_POWEROFF)
235 printf("Resetting system ... ");
238 bcmwd_watchdog_fn(sc, cmd, &error);
240 /* Wait for watchdog timeout. */
243 /* Not reached ... one hopes. */
244 printf("failed to reset (errno %d).\n", error);
247 static device_method_t bcmwd_methods[] = {
248 DEVMETHOD(device_probe, bcmwd_probe),
249 DEVMETHOD(device_attach, bcmwd_attach),
254 static driver_t bcmwd_driver = {
257 sizeof(struct bcmwd_softc),
259 static devclass_t bcmwd_devclass;
261 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0);