2 * Copyright (c) 2010 Greg Ansley. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * The SAM9 watchdog hardware can be programed only once. So we set the
28 * hardware watchdog to 16 s in wdt_attach and only reset it in the wdt_tick
29 * handler. The watchdog is halted in processor debug mode.
32 #include "opt_platform.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/watchdog.h>
45 #include <machine/bus.h>
47 #include <arm/at91/at91var.h>
48 #include <arm/at91/at91_wdtreg.h>
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
59 struct resource *mem_res;
60 struct callout tick_ch;
61 eventhandler_tag sc_wet;
67 static inline uint32_t
68 RD4(struct wdt_softc *sc, bus_size_t off)
71 return (bus_read_4(sc->mem_res, off));
75 WR4(struct wdt_softc *sc, bus_size_t off, uint32_t val)
78 bus_write_4(sc->mem_res, off, val);
84 struct wdt_softc *sc = argp;
87 if (RD4(sc, WDT_SR) & (WDT_WDUNF | WDT_WDERR)) {
88 #if defined(KDB) && !defined(KDB_UNATTENDED)
90 kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
92 panic("watchdog timeout");
95 return (FILTER_STRAY);
98 /* User interface, see watchdog(9) */
100 wdt_watchdog(void *argp, u_int cmd, int *error)
102 struct wdt_softc *sc = argp;
105 mtx_lock(&sc->sc_mtx);
109 interval = cmd & WD_INTERVAL;
110 if (interval > WD_TO_16SEC)
112 else if (interval > 0)
113 sc->cmd = interval | WD_ACTIVE;
115 /* We cannot turn off our watchdog so if user
116 * fails to turn us on go to passive mode. */
117 if ((sc->cmd & WD_ACTIVE) == 0)
118 sc->cmd = WD_PASSIVE;
120 mtx_unlock(&sc->sc_mtx);
123 /* This routine is called no matter what state the user sets the
124 * watchdog mode to. Called at a rate that is slightly less than
125 * half the hardware timeout. */
129 struct wdt_softc *sc = argp;
131 mtx_assert(&sc->sc_mtx, MA_OWNED);
132 if (sc->cmd & (WD_ACTIVE | WD_PASSIVE))
133 WR4(sc, WDT_CR, WDT_KEY|WDT_WDRSTT);
135 sc->cmd &= WD_PASSIVE;
136 callout_reset(&sc->tick_ch, sc->interval, wdt_tick, sc);
140 wdt_probe(device_t dev)
143 if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-wdt"))
146 device_set_desc(dev, "WDT");
151 wdt_attach(device_t dev)
153 static struct wdt_softc *sc;
154 struct resource *irq;
158 sc = device_get_softc(dev);
159 sc->cmd = WD_PASSIVE;
162 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "at91_wdt", MTX_DEF);
163 callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0);
166 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
169 if (sc->mem_res == NULL)
170 panic("couldn't allocate wdt register resources");
172 wdt_mr = RD4(sc, WDT_MR);
173 if ((wdt_mr & WDT_WDRSTEN) == 0)
174 device_printf(dev, "Watchdog disabled! (Boot ROM?)\n");
177 /* Rude, full reset of whole system on watch dog timeout */
178 WR4(sc, WDT_MR, WDT_WDDBGHLT | WDT_WDD(0xC00)|
179 WDT_WDRSTEN| WDT_WDV(0xFFF));
181 /* Generate stack trace and panic on watchdog timeout*/
182 WR4(sc, WDT_MR, WDT_WDDBGHLT | WDT_WDD(0xC00)|
183 WDT_WDFIEN| WDT_WDV(0xFFF));
186 * This may have been set by Boot ROM so register value may
187 * not be what we just requested since this is a write once
190 wdt_mr = RD4(sc, WDT_MR);
191 if (wdt_mr & WDT_WDFIEN) {
193 irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
194 RF_ACTIVE | RF_SHAREABLE);
196 panic("could not allocate interrupt.\n");
198 err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, wdt_intr,
199 NULL, sc, &sc->intrhand);
203 sc->interval = (((wdt_mr & WDT_WDV(~0)) + 1) * WDT_DIV) /
206 device_printf(dev, "watchdog timeout: %d seconds\n",
209 /* Slightly less than 1/2 of watchdog hardware timeout */
210 sc->interval = (sc->interval/2) - (sc->interval/20);
211 callout_reset(&sc->tick_ch, sc->interval, wdt_tick, sc);
213 /* Register us as a watchdog */
214 sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
215 wdt_watchdog, sc, 0);
220 static device_method_t wdt_methods[] = {
221 DEVMETHOD(device_probe, wdt_probe),
222 DEVMETHOD(device_attach, wdt_attach),
226 static driver_t wdt_driver = {
229 sizeof(struct wdt_softc),
232 static devclass_t wdt_devclass;
235 DRIVER_MODULE(at91_wdt, simplebus, wdt_driver, wdt_devclass, NULL, NULL);
237 DRIVER_MODULE(at91_wdt, atmelarm, wdt_driver, wdt_devclass, NULL, NULL);