]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_wdt.c
MFV r316083,316094:
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_wdt.c
1 /*-
2  * Copyright (c) 2010 Greg Ansley.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 /*
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.
30  */
31
32 #include "opt_platform.h"
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/kdb.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/systm.h>
43 #include <sys/watchdog.h>
44
45 #include <machine/bus.h>
46
47 #include <arm/at91/at91var.h>
48 #include <arm/at91/at91_wdtreg.h>
49
50 #ifdef FDT
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 #endif
54
55 struct wdt_softc {
56         struct mtx      sc_mtx;
57         device_t        sc_dev;
58         struct resource *mem_res;
59         struct callout  tick_ch;
60         eventhandler_tag sc_wet;
61         void            *intrhand;
62         u_int           cmd;
63         u_int           interval;
64 };
65
66 static inline uint32_t
67 RD4(struct wdt_softc *sc, bus_size_t off)
68 {
69
70         return (bus_read_4(sc->mem_res, off));
71 }
72
73 static inline void
74 WR4(struct wdt_softc *sc, bus_size_t off, uint32_t val)
75 {
76
77         bus_write_4(sc->mem_res, off, val);
78 }
79
80 static int
81 wdt_intr(void *argp)
82 {
83         struct wdt_softc *sc = argp;
84
85
86         if (RD4(sc, WDT_SR) & (WDT_WDUNF | WDT_WDERR)) {
87 #if defined(KDB) && !defined(KDB_UNATTENDED)
88                 kdb_backtrace();
89                 kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
90 #else
91                 panic("watchdog timeout");
92 #endif
93         }
94         return (FILTER_STRAY);
95 }
96
97 /* User interface, see watchdog(9) */
98 static void
99 wdt_watchdog(void *argp, u_int cmd, int *error)
100 {
101         struct wdt_softc *sc = argp;
102         u_int interval;
103
104         mtx_lock(&sc->sc_mtx);
105
106         *error = 0;
107         sc->cmd = 0;
108         interval = cmd & WD_INTERVAL;
109         if (interval > WD_TO_16SEC)
110                 *error = EOPNOTSUPP;
111         else if (interval > 0)
112                 sc->cmd = interval | WD_ACTIVE;
113
114         /* We cannot turn off our watchdog so if user
115          * fails to turn us on go to passive mode. */
116         if ((sc->cmd & WD_ACTIVE) == 0)
117                 sc->cmd = WD_PASSIVE;
118
119         mtx_unlock(&sc->sc_mtx);
120 }
121
122 /* This routine is called no matter what state the user sets the
123  * watchdog mode to. Called at a rate that is slightly less than
124  * half the hardware timeout. */
125 static void
126 wdt_tick(void *argp)
127 {
128         struct wdt_softc *sc = argp;
129
130         mtx_assert(&sc->sc_mtx, MA_OWNED);
131         if (sc->cmd & (WD_ACTIVE | WD_PASSIVE))
132                 WR4(sc, WDT_CR, WDT_KEY|WDT_WDRSTT);
133
134         sc->cmd &= WD_PASSIVE;
135         callout_reset(&sc->tick_ch, sc->interval, wdt_tick, sc);
136 }
137
138 static int
139 wdt_probe(device_t dev)
140 {
141 #ifdef FDT
142         if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-wdt"))
143                 return (ENXIO);
144 #endif
145         device_set_desc(dev, "WDT");
146         return (0);
147 }
148
149 static int
150 wdt_attach(device_t dev)
151 {
152         static struct wdt_softc *sc;
153         struct resource *irq;
154         uint32_t wdt_mr;
155         int rid, err;
156
157         sc = device_get_softc(dev);
158         sc->cmd = WD_PASSIVE;
159         sc->sc_dev = dev;
160
161         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "at91_wdt", MTX_DEF);
162         callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0);
163
164         rid = 0;
165         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
166             RF_ACTIVE);
167
168         if (sc->mem_res == NULL)
169                 panic("couldn't allocate wdt register resources");
170
171         wdt_mr = RD4(sc, WDT_MR);
172         if ((wdt_mr & WDT_WDRSTEN) == 0)
173                 device_printf(dev, "Watchdog disabled! (Boot ROM?)\n");
174         else {
175 #ifdef WDT_RESET
176                 /* Rude, full reset of whole system on watch dog timeout */
177                 WR4(sc, WDT_MR, WDT_WDDBGHLT | WDT_WDD(0xC00)|
178                     WDT_WDRSTEN| WDT_WDV(0xFFF));
179 #else
180                 /* Generate stack trace and panic on watchdog timeout*/
181                 WR4(sc, WDT_MR, WDT_WDDBGHLT | WDT_WDD(0xC00)|
182                     WDT_WDFIEN| WDT_WDV(0xFFF));
183 #endif
184                 /*
185                  * This may have been set by Boot ROM so register value may
186                  * not be what we just requested since this is a write once
187                  * register.
188                  */
189                 wdt_mr = RD4(sc, WDT_MR);
190                 if (wdt_mr & WDT_WDFIEN) {
191                         rid = 0;
192                         irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
193                             RF_ACTIVE | RF_SHAREABLE);
194                         if (!irq)
195                                 panic("could not allocate interrupt.\n");
196
197                         err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, wdt_intr,
198                             NULL, sc, &sc->intrhand);
199                 }
200
201                 /* interval * hz */
202                 sc->interval = (((wdt_mr & WDT_WDV(~0)) + 1) * WDT_DIV) /
203                     (WDT_CLOCK/hz);
204
205                 device_printf(dev, "watchdog timeout: %d seconds\n",
206                     sc->interval / hz);
207
208                 /* Slightly less than 1/2 of watchdog hardware timeout */
209                 sc->interval = (sc->interval/2) - (sc->interval/20);
210                 callout_reset(&sc->tick_ch, sc->interval, wdt_tick, sc);
211
212                 /* Register us as a watchdog */
213                 sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
214                     wdt_watchdog, sc, 0);
215         }
216         return (0);
217 }
218
219 static device_method_t wdt_methods[] = {
220         DEVMETHOD(device_probe, wdt_probe),
221         DEVMETHOD(device_attach, wdt_attach),
222         DEVMETHOD_END
223 };
224
225 static driver_t wdt_driver = {
226         "at91_wdt",
227         wdt_methods,
228         sizeof(struct wdt_softc),
229 };
230
231 static devclass_t wdt_devclass;
232
233 #ifdef FDT
234 DRIVER_MODULE(at91_wdt, simplebus, wdt_driver, wdt_devclass, NULL, NULL);
235 #else
236 DRIVER_MODULE(at91_wdt, atmelarm, wdt_driver, wdt_devclass, NULL, NULL);
237 #endif