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$");
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
48 #include <arm/allwinner/a10_wdog.h>
49
50 #define READ(_sc, _r) bus_read_4((_sc)->res, (_r))
51 #define WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v))
52
53 #define WDOG_CTRL 0x00
54 #define WDOG_CTRL_RESTART (1 << 0)
55 #define WDOG_MODE 0x04
56 #define WDOG_MODE_INTVL_SHIFT 3
57 #define WDOG_MODE_RST_EN (1 << 1)
58 #define WDOG_MODE_EN (1 << 0)
59
60 struct a10wd_interval {
61 uint64_t milliseconds;
62 unsigned int value;
63 };
64
65 struct a10wd_interval wd_intervals[] = {
66 { 500, 0 },
67 { 1000, 1 },
68 { 2000, 2 },
69 { 3000, 3 },
70 { 4000, 4 },
71 { 5000, 5 },
72 { 6000, 6 },
73 { 8000, 7 },
74 { 10000, 8 },
75 { 12000, 9 },
76 { 14000, 10 },
77 { 16000, 11 },
78 { 0, 0 } /* sentinel */
79 };
80
81 static struct a10wd_softc *a10wd_sc = NULL;
82
83 struct a10wd_softc {
84 device_t dev;
85 struct resource * res;
86 struct mtx mtx;
87 };
88
89 static void a10wd_watchdog_fn(void *private, u_int cmd, int *error);
90
91 static int
a10wd_probe(device_t dev)92 a10wd_probe(device_t dev)
93 {
94
95 if (!ofw_bus_status_okay(dev))
96 return (ENXIO);
97
98 if (ofw_bus_is_compatible(dev, "allwinner,sun4i-wdt")) {
99 device_set_desc(dev, "Allwinner A10 Watchdog");
100 return (BUS_PROBE_DEFAULT);
101 }
102
103 return (ENXIO);
104 }
105
106 static int
a10wd_attach(device_t dev)107 a10wd_attach(device_t dev)
108 {
109 struct a10wd_softc *sc;
110 int rid;
111
112 if (a10wd_sc != NULL)
113 return (ENXIO);
114
115 sc = device_get_softc(dev);
116 sc->dev = dev;
117
118 rid = 0;
119 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
120 if (sc->res == NULL) {
121 device_printf(dev, "could not allocate memory resource\n");
122 return (ENXIO);
123 }
124
125 a10wd_sc = sc;
126 mtx_init(&sc->mtx, "A10 Watchdog", "a10wd", MTX_DEF);
127 EVENTHANDLER_REGISTER(watchdog_list, a10wd_watchdog_fn, sc, 0);
128
129 return (0);
130 }
131
132 static void
a10wd_watchdog_fn(void * private,u_int cmd,int * error)133 a10wd_watchdog_fn(void *private, u_int cmd, int *error)
134 {
135 struct a10wd_softc *sc;
136 uint64_t ms;
137 int i;
138
139 sc = private;
140 mtx_lock(&sc->mtx);
141
142 cmd &= WD_INTERVAL;
143
144 if (cmd > 0) {
145 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
146 i = 0;
147 while (wd_intervals[i].milliseconds &&
148 (ms > wd_intervals[i].milliseconds))
149 i++;
150 if (wd_intervals[i].milliseconds) {
151 WRITE(sc, WDOG_MODE,
152 (wd_intervals[i].value << WDOG_MODE_INTVL_SHIFT) |
153 WDOG_MODE_EN | WDOG_MODE_RST_EN);
154 WRITE(sc, WDOG_CTRL, WDOG_CTRL_RESTART);
155 *error = 0;
156 }
157 else {
158 /*
159 * Can't arm
160 * disable watchdog as watchdog(9) requires
161 */
162 device_printf(sc->dev,
163 "Can't arm, timeout is more than 16 sec\n");
164 mtx_unlock(&sc->mtx);
165 WRITE(sc, WDOG_MODE, 0);
166 return;
167 }
168 }
169 else
170 WRITE(sc, WDOG_MODE, 0);
171
172 mtx_unlock(&sc->mtx);
173 }
174
175 void
a10wd_watchdog_reset()176 a10wd_watchdog_reset()
177 {
178
179 if (a10wd_sc == NULL) {
180 printf("Reset: watchdog device has not been initialized\n");
181 return;
182 }
183
184 WRITE(a10wd_sc, WDOG_MODE,
185 (wd_intervals[0].value << WDOG_MODE_INTVL_SHIFT) |
186 WDOG_MODE_EN | WDOG_MODE_RST_EN);
187
188 while(1)
189 ;
190
191 }
192
193 static device_method_t a10wd_methods[] = {
194 DEVMETHOD(device_probe, a10wd_probe),
195 DEVMETHOD(device_attach, a10wd_attach),
196
197 DEVMETHOD_END
198 };
199
200 static driver_t a10wd_driver = {
201 "a10wd",
202 a10wd_methods,
203 sizeof(struct a10wd_softc),
204 };
205 static devclass_t a10wd_devclass;
206
207 DRIVER_MODULE(a10wd, simplebus, a10wd_driver, a10wd_devclass, 0, 0);
208