summaryrefslogtreecommitdiffstats
path: root/sys/arm/at91/at91_wdt.c
blob: 95332025e2f73a4ed37c3087bdc42ad77d0bcad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*-
 * Copyright (c) 2010 Greg Ansley.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * The SAM9 watchdog hardware can be programed only once. So we set the
 * hardware watchdog to 16 s in wdt_attach and only reset it in the wdt_tick
 * handler.  The watchdog is halted in processor debug mode.
 */

#include "opt_platform.h"

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/watchdog.h>

#include <machine/bus.h>

#include <arm/at91/at91var.h>
#include <arm/at91/at91_wdtreg.h>

#ifdef FDT
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif

struct wdt_softc {
	struct mtx	sc_mtx;
	device_t	sc_dev;
	struct resource	*mem_res;
	struct callout	tick_ch;
	eventhandler_tag sc_wet;
	void		*intrhand;
	u_int		cmd;
	u_int		interval;
};

static inline uint32_t
RD4(struct wdt_softc *sc, bus_size_t off)
{

	return (bus_read_4(sc->mem_res, off));
}

static inline void
WR4(struct wdt_softc *sc, bus_size_t off, uint32_t val)
{

	bus_write_4(sc->mem_res, off, val);
}

static int
wdt_intr(void *argp)
{
	struct wdt_softc *sc = argp;


	if (RD4(sc, WDT_SR) & (WDT_WDUNF | WDT_WDERR)) {
#if defined(KDB) && !defined(KDB_UNATTENDED)
		kdb_backtrace();
		kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
#else
		panic("watchdog timeout");
#endif
	}
	return (FILTER_STRAY);
}

/* User interface, see watchdog(9) */
static void
wdt_watchdog(void *argp, u_int cmd, int *error)
{
	struct wdt_softc *sc = argp;
	u_int interval;

	mtx_lock(&sc->sc_mtx);

	*error = 0;
	sc->cmd = 0;
	interval = cmd & WD_INTERVAL;
	if (interval > WD_TO_16SEC)
		*error = EOPNOTSUPP;
	else if (interval > 0)
		sc->cmd = interval | WD_ACTIVE;

	/* We cannot turn off our watchdog so if user
	 * fails to turn us on go to passive mode. */
	if ((sc->cmd & WD_ACTIVE) == 0)
		sc->cmd = WD_PASSIVE;

	mtx_unlock(&sc->sc_mtx);
}

/* This routine is called no matter what state the user sets the
 * watchdog mode to. Called at a rate that is slightly less than
 * half the hardware timeout. */
static void
wdt_tick(void *argp)
{
	struct wdt_softc *sc = argp;

	mtx_assert(&sc->sc_mtx, MA_OWNED);
	if (sc->cmd & (WD_ACTIVE | WD_PASSIVE))
		WR4(sc, WDT_CR, WDT_KEY|WDT_WDRSTT);

	sc->cmd &= WD_PASSIVE;
	callout_reset(&sc->tick_ch, sc->interval, wdt_tick, sc);
}

static int
wdt_probe(device_t dev)
{
#ifdef FDT
	if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-wdt"))
		return (ENXIO);
#endif
	device_set_desc(dev, "WDT");
	return (0);
}

static int
wdt_attach(device_t dev)
{
	static struct wdt_softc *sc;
	struct resource *irq;
	uint32_t wdt_mr;
	int rid, err;

	sc = device_get_softc(dev);
	sc->cmd = WD_PASSIVE;
	sc->sc_dev = dev;

	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "at91_wdt", MTX_DEF);
	callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0);

	rid = 0;
	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
	    RF_ACTIVE);

	if (sc->mem_res == NULL)
		panic("couldn't allocate wdt register resources");

	wdt_mr = RD4(sc, WDT_MR);
	if ((wdt_mr & WDT_WDRSTEN) == 0)
		device_printf(dev, "Watchdog disabled! (Boot ROM?)\n");
	else {
#ifdef WDT_RESET
		/* Rude, full reset of whole system on watch dog timeout */
		WR4(sc, WDT_MR, WDT_WDDBGHLT | WDT_WDD(0xC00)|
		    WDT_WDRSTEN| WDT_WDV(0xFFF));
#else
		/* Generate stack trace and panic on watchdog timeout*/
		WR4(sc, WDT_MR, WDT_WDDBGHLT | WDT_WDD(0xC00)|
		    WDT_WDFIEN| WDT_WDV(0xFFF));
#endif
		/*
		 * This may have been set by Boot ROM so register value may
		 * not be what we just requested since this is a write once
		 * register.
		 */
		wdt_mr = RD4(sc, WDT_MR);
		if (wdt_mr & WDT_WDFIEN) {
			rid = 0;
			irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
			    RF_ACTIVE | RF_SHAREABLE);
			if (!irq)
				panic("could not allocate interrupt.\n");

			err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, wdt_intr,
			    NULL, sc, &sc->intrhand);
		}

		/* interval * hz */
		sc->interval = (((wdt_mr & WDT_WDV(~0)) + 1) * WDT_DIV) /
		    (WDT_CLOCK/hz);

		device_printf(dev, "watchdog timeout: %d seconds\n",
		    sc->interval / hz);

		/* Slightly less than 1/2 of watchdog hardware timeout */
		sc->interval = (sc->interval/2) - (sc->interval/20);
		callout_reset(&sc->tick_ch, sc->interval, wdt_tick, sc);

		/* Register us as a watchdog */
		sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
		    wdt_watchdog, sc, 0);
	}
	return (0);
}

static device_method_t wdt_methods[] = {
	DEVMETHOD(device_probe, wdt_probe),
	DEVMETHOD(device_attach, wdt_attach),
	DEVMETHOD_END
};

static driver_t wdt_driver = {
	"at91_wdt",
	wdt_methods,
	sizeof(struct wdt_softc),
};

static devclass_t wdt_devclass;

#ifdef FDT
DRIVER_MODULE(at91_wdt, simplebus, wdt_driver, wdt_devclass, NULL, NULL);
#else
DRIVER_MODULE(at91_wdt, atmelarm, wdt_driver, wdt_devclass, NULL, NULL);
#endif
OpenPOWER on IntegriCloud