1 /*-
2 * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: releng/10.0/sys/arm/allwinner/a10_wdog.c 246707 2013-02-12 07:27:40Z gonzo $");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/watchdog.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/rman.h>
38
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>
43
44 #include <machine/bus.h>
45 #include <machine/cpufunc.h>
46 #include <machine/machdep.h>
47 #include <machine/fdt.h>
48
49 #include <arm/allwinner/a10_wdog.h>
50
51 #define READ(_sc, _r) bus_read_4((_sc)->res, (_r))
52 #define WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v))
53
54 #define WDOG_CTRL 0x00
55 #define WDOG_CTRL_RESTART (1 << 0)
56 #define WDOG_MODE 0x04
57 #define WDOG_MODE_INTVL_SHIFT 3
58 #define WDOG_MODE_RST_EN (1 << 1)
59 #define WDOG_MODE_EN (1 << 0)
60
61 struct a10wd_interval {
62 uint64_t milliseconds;
63 unsigned int value;
64 };
65
66 struct a10wd_interval wd_intervals[] = {
67 { 500, 0 },
68 { 1000, 1 },
69 { 2000, 2 },
70 { 3000, 3 },
71 { 4000, 4 },
72 { 5000, 5 },
73 { 6000, 6 },
74 { 8000, 7 },
75 { 10000, 8 },
76 { 12000, 9 },
77 { 14000, 10 },
78 { 16000, 11 },
79 { 0, 0 } /* sentinel */
80 };
81
82 static struct a10wd_softc *a10wd_sc = NULL;
83
84 struct a10wd_softc {
85 device_t dev;
86 struct resource * res;
87 struct mtx mtx;
88 };
89
90 static void a10wd_watchdog_fn(void *private, u_int cmd, int *error);
91
92 static int
93 a10wd_probe(device_t dev)
94 {
95
96 if (ofw_bus_is_compatible(dev, "allwinner,sun4i-wdt")) {
97 device_set_desc(dev, "Allwinner A10 Watchdog");
98 return (BUS_PROBE_DEFAULT);
99 }
100
101 return (ENXIO);
102 }
103
104 static int
105 a10wd_attach(device_t dev)
106 {
107 struct a10wd_softc *sc;
108 int rid;
109
110 if (a10wd_sc != NULL)
111 return (ENXIO);
112
113 sc = device_get_softc(dev);
114 sc->dev = dev;
115
116 rid = 0;
117 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
118 if (sc->res == NULL) {
119 device_printf(dev, "could not allocate memory resource\n");
120 return (ENXIO);
121 }
122
123 a10wd_sc = sc;
124 mtx_init(&sc->mtx, "A10 Watchdog", "a10wd", MTX_DEF);
125 EVENTHANDLER_REGISTER(watchdog_list, a10wd_watchdog_fn, sc, 0);
126
127 return (0);
128 }
129
130 static void
131 a10wd_watchdog_fn(void *private, u_int cmd, int *error)
132 {
133 struct a10wd_softc *sc;
134 uint64_t ms;
135 int i;
136
137 sc = private;
138 mtx_lock(&sc->mtx);
139
140 cmd &= WD_INTERVAL;
141
142 if (cmd > 0) {
143 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
144 i = 0;
145 while (wd_intervals[i].milliseconds &&
146 (ms > wd_intervals[i].milliseconds))
147 i++;
148 if (wd_intervals[i].milliseconds) {
149 WRITE(sc, WDOG_MODE,
150 (wd_intervals[i].value << WDOG_MODE_INTVL_SHIFT) |
151 WDOG_MODE_EN | WDOG_MODE_RST_EN);
152 WRITE(sc, WDOG_CTRL, WDOG_CTRL_RESTART);
153 }
154 }
155 else
156 WRITE(sc, WDOG_MODE, 0);
157
158 mtx_unlock(&sc->mtx);
159 }
160
161 void
162 a10wd_watchdog_reset()
163 {
164
165 if (a10wd_sc == NULL) {
166 printf("Reset: watchdog device has not been initialized\n");
167 return;
168 }
169
170 WRITE(a10wd_sc, WDOG_MODE,
171 (wd_intervals[0].value << WDOG_MODE_INTVL_SHIFT) |
172 WDOG_MODE_EN | WDOG_MODE_RST_EN);
173
174 while(1)
175 ;
176
177 }
178
179 static device_method_t a10wd_methods[] = {
180 DEVMETHOD(device_probe, a10wd_probe),
181 DEVMETHOD(device_attach, a10wd_attach),
182
183 DEVMETHOD_END
184 };
185
186 static driver_t a10wd_driver = {
187 "a10wd",
188 a10wd_methods,
189 sizeof(struct a10wd_softc),
190 };
191 static devclass_t a10wd_devclass;
192
193 DRIVER_MODULE(a10wd, simplebus, a10wd_driver, a10wd_devclass, 0, 0);
Cache object: 22160c755c3960bae9073569aac07c4e
|