xref: /freebsd/sys/arm/allwinner/aw_wdog.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1 /*-
2  * Copyright (c) 2013 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * Copyright (c) 2016 Emmanuel Vadot <manu@bidouilliste.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/watchdog.h>
33 #include <sys/reboot.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 
41 #include <dev/fdt/fdt_common.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <machine/bus.h>
47 #include <machine/cpufunc.h>
48 #include <machine/machdep.h>
49 
50 #include <arm/allwinner/aw_wdog.h>
51 
52 #define	READ(_sc, _r) bus_read_4((_sc)->res, (_r))
53 #define	WRITE(_sc, _r, _v) bus_write_4((_sc)->res, (_r), (_v))
54 
55 #define	A10_WDOG_CTRL		0x00
56 #define	A31_WDOG_CTRL		0x10
57 #define	 WDOG_CTRL_RESTART	(1 << 0)
58 #define	 A31_WDOG_CTRL_KEY	(0xa57 << 1)
59 #define	A10_WDOG_MODE		0x04
60 #define	A31_WDOG_MODE		0x18
61 #define	 A10_WDOG_MODE_INTVL_SHIFT	3
62 #define	 A31_WDOG_MODE_INTVL_SHIFT	4
63 #define	 A10_WDOG_MODE_RST_EN	(1 << 1)
64 #define	 WDOG_MODE_EN		(1 << 0)
65 #define	A31_WDOG_CONFIG		0x14
66 #define	 A31_WDOG_CONFIG_RST_EN_SYSTEM	(1 << 0)
67 #define	 A31_WDOG_CONFIG_RST_EN_INT	(2 << 0)
68 
69 struct aw_wdog_interval {
70 	uint64_t	milliseconds;
71 	unsigned int	value;
72 };
73 
74 struct aw_wdog_interval wd_intervals[] = {
75 	{   500,	 0 },
76 	{  1000,	 1 },
77 	{  2000,	 2 },
78 	{  3000,	 3 },
79 	{  4000,	 4 },
80 	{  5000,	 5 },
81 	{  6000,	 6 },
82 	{  8000,	 7 },
83 	{ 10000,	 8 },
84 	{ 12000,	 9 },
85 	{ 14000,	10 },
86 	{ 16000,	11 },
87 	{ 0,		 0 } /* sentinel */
88 };
89 
90 static struct aw_wdog_softc *aw_wdog_sc = NULL;
91 
92 struct aw_wdog_softc {
93 	device_t		dev;
94 	struct resource *	res;
95 	struct mtx		mtx;
96 	uint8_t			wdog_ctrl;
97 	uint32_t		wdog_ctrl_key;
98 	uint8_t			wdog_mode;
99 	uint8_t			wdog_mode_intvl_shift;
100 	uint8_t			wdog_mode_en;
101 	uint8_t			wdog_config;
102 	uint8_t			wdog_config_value;
103 };
104 
105 #define	A10_WATCHDOG	1
106 #define	A31_WATCHDOG	2
107 
108 static struct ofw_compat_data compat_data[] = {
109 	{"allwinner,sun4i-a10-wdt", A10_WATCHDOG},
110 	{"allwinner,sun6i-a31-wdt", A31_WATCHDOG},
111 	{NULL,             0}
112 };
113 
114 static void aw_wdog_watchdog_fn(void *, u_int, int *);
115 static void aw_wdog_shutdown_fn(void *, int);
116 
117 static int
118 aw_wdog_probe(device_t dev)
119 {
120 	struct aw_wdog_softc *sc;
121 
122 	sc = device_get_softc(dev);
123 
124 	if (!ofw_bus_status_okay(dev))
125 		return (ENXIO);
126 	switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
127 	case A10_WATCHDOG:
128 		device_set_desc(dev, "Allwinner A10 Watchdog");
129 		return (BUS_PROBE_DEFAULT);
130 	case A31_WATCHDOG:
131 		device_set_desc(dev, "Allwinner A31 Watchdog");
132 		return (BUS_PROBE_DEFAULT);
133 	}
134 	return (ENXIO);
135 }
136 
137 static int
138 aw_wdog_attach(device_t dev)
139 {
140 	struct aw_wdog_softc *sc;
141 	int rid;
142 
143 	if (aw_wdog_sc != NULL)
144 		return (ENXIO);
145 
146 	sc = device_get_softc(dev);
147 	sc->dev = dev;
148 
149 	rid = 0;
150 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
151 	if (sc->res == NULL) {
152 		device_printf(dev, "could not allocate memory resource\n");
153 		return (ENXIO);
154 	}
155 
156 	aw_wdog_sc = sc;
157 
158 	switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
159 	case A10_WATCHDOG:
160 		sc->wdog_ctrl = A10_WDOG_CTRL;
161 		sc->wdog_mode = A10_WDOG_MODE;
162 		sc->wdog_mode_intvl_shift = A10_WDOG_MODE_INTVL_SHIFT;
163 		sc->wdog_mode_en = A10_WDOG_MODE_RST_EN | WDOG_MODE_EN;
164 		break;
165 	case A31_WATCHDOG:
166 		sc->wdog_ctrl = A31_WDOG_CTRL;
167 		sc->wdog_ctrl_key = A31_WDOG_CTRL_KEY;
168 		sc->wdog_mode = A31_WDOG_MODE;
169 		sc->wdog_mode_intvl_shift = A31_WDOG_MODE_INTVL_SHIFT;
170 		sc->wdog_mode_en = WDOG_MODE_EN;
171 		sc->wdog_config = A31_WDOG_CONFIG;
172 		sc->wdog_config_value = A31_WDOG_CONFIG_RST_EN_SYSTEM;
173 		break;
174 	default:
175 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res);
176 		return (ENXIO);
177 	}
178 
179 	mtx_init(&sc->mtx, "AW Watchdog", "aw_wdog", MTX_DEF);
180 	EVENTHANDLER_REGISTER(watchdog_list, aw_wdog_watchdog_fn, sc, 0);
181 	EVENTHANDLER_REGISTER(shutdown_final, aw_wdog_shutdown_fn, sc,
182 	    SHUTDOWN_PRI_LAST - 1);
183 
184 	return (0);
185 }
186 
187 static void
188 aw_wdog_watchdog_fn(void *private, u_int cmd, int *error)
189 {
190 	struct aw_wdog_softc *sc;
191 	uint64_t ms;
192 	int i;
193 
194 	sc = private;
195 	mtx_lock(&sc->mtx);
196 
197 	cmd &= WD_INTERVAL;
198 
199 	if (cmd > 0) {
200 		ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
201 		i = 0;
202 		while (wd_intervals[i].milliseconds &&
203 		    (ms > wd_intervals[i].milliseconds))
204 			i++;
205 		if (wd_intervals[i].milliseconds) {
206 			WRITE(sc, sc->wdog_mode,
207 			  (wd_intervals[i].value << sc->wdog_mode_intvl_shift) |
208 			    sc->wdog_mode_en);
209 			WRITE(sc, sc->wdog_ctrl,
210 			    WDOG_CTRL_RESTART | sc->wdog_ctrl_key);
211 			if (sc->wdog_config)
212 				WRITE(sc, sc->wdog_config,
213 				    sc->wdog_config_value);
214 			*error = 0;
215 		}
216 		else {
217 			/*
218 			 * Can't arm
219 			 * disable watchdog as watchdog(9) requires
220 			 */
221 			device_printf(sc->dev,
222 			    "Can't arm, timeout is more than 16 sec\n");
223 			mtx_unlock(&sc->mtx);
224 			WRITE(sc, sc->wdog_mode, 0);
225 			return;
226 		}
227 	}
228 	else
229 		WRITE(sc, sc->wdog_mode, 0);
230 
231 	mtx_unlock(&sc->mtx);
232 }
233 
234 static void
235 aw_wdog_shutdown_fn(void *private, int howto)
236 {
237 	if ((howto & (RB_POWEROFF|RB_HALT)) == 0)
238 		aw_wdog_watchdog_reset();
239 }
240 
241 void
242 aw_wdog_watchdog_reset()
243 {
244 
245 	if (aw_wdog_sc == NULL) {
246 		printf("Reset: watchdog device has not been initialized\n");
247 		return;
248 	}
249 
250 	WRITE(aw_wdog_sc, aw_wdog_sc->wdog_mode,
251 	    (wd_intervals[0].value << aw_wdog_sc->wdog_mode_intvl_shift) |
252 	    aw_wdog_sc->wdog_mode_en);
253 	if (aw_wdog_sc->wdog_config)
254 		WRITE(aw_wdog_sc, aw_wdog_sc->wdog_config,
255 		      aw_wdog_sc->wdog_config_value);
256 	WRITE(aw_wdog_sc, aw_wdog_sc->wdog_ctrl,
257 	    WDOG_CTRL_RESTART | aw_wdog_sc->wdog_ctrl_key);
258 	while(1)
259 		;
260 
261 }
262 
263 static device_method_t aw_wdog_methods[] = {
264 	DEVMETHOD(device_probe, aw_wdog_probe),
265 	DEVMETHOD(device_attach, aw_wdog_attach),
266 
267 	DEVMETHOD_END
268 };
269 
270 static driver_t aw_wdog_driver = {
271 	"aw_wdog",
272 	aw_wdog_methods,
273 	sizeof(struct aw_wdog_softc),
274 };
275 static devclass_t aw_wdog_devclass;
276 
277 DRIVER_MODULE(aw_wdog, simplebus, aw_wdog_driver, aw_wdog_devclass, 0, 0);
278