xref: /freebsd/sys/arm/allwinner/aw_wdog.c (revision d6b92ffa)
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/ofw/openfirm.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #include <machine/bus.h>
46 #include <machine/machdep.h>
47 
48 #include <arm/allwinner/aw_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	A10_WDOG_CTRL		0x00
54 #define	A31_WDOG_CTRL		0x10
55 #define	 WDOG_CTRL_RESTART	(1 << 0)
56 #define	 A31_WDOG_CTRL_KEY	(0xa57 << 1)
57 #define	A10_WDOG_MODE		0x04
58 #define	A31_WDOG_MODE		0x18
59 #define	 A10_WDOG_MODE_INTVL_SHIFT	3
60 #define	 A31_WDOG_MODE_INTVL_SHIFT	4
61 #define	 A10_WDOG_MODE_RST_EN	(1 << 1)
62 #define	 WDOG_MODE_EN		(1 << 0)
63 #define	A31_WDOG_CONFIG		0x14
64 #define	 A31_WDOG_CONFIG_RST_EN_SYSTEM	(1 << 0)
65 #define	 A31_WDOG_CONFIG_RST_EN_INT	(2 << 0)
66 
67 struct aw_wdog_interval {
68 	uint64_t	milliseconds;
69 	unsigned int	value;
70 };
71 
72 struct aw_wdog_interval wd_intervals[] = {
73 	{   500,	 0 },
74 	{  1000,	 1 },
75 	{  2000,	 2 },
76 	{  3000,	 3 },
77 	{  4000,	 4 },
78 	{  5000,	 5 },
79 	{  6000,	 6 },
80 	{  8000,	 7 },
81 	{ 10000,	 8 },
82 	{ 12000,	 9 },
83 	{ 14000,	10 },
84 	{ 16000,	11 },
85 	{ 0,		 0 } /* sentinel */
86 };
87 
88 static struct aw_wdog_softc *aw_wdog_sc = NULL;
89 
90 struct aw_wdog_softc {
91 	device_t		dev;
92 	struct resource *	res;
93 	struct mtx		mtx;
94 	uint8_t			wdog_ctrl;
95 	uint32_t		wdog_ctrl_key;
96 	uint8_t			wdog_mode;
97 	uint8_t			wdog_mode_intvl_shift;
98 	uint8_t			wdog_mode_en;
99 	uint8_t			wdog_config;
100 	uint8_t			wdog_config_value;
101 };
102 
103 #define	A10_WATCHDOG	1
104 #define	A31_WATCHDOG	2
105 
106 static struct ofw_compat_data compat_data[] = {
107 	{"allwinner,sun4i-a10-wdt", A10_WATCHDOG},
108 	{"allwinner,sun6i-a31-wdt", A31_WATCHDOG},
109 	{NULL,             0}
110 };
111 
112 static void aw_wdog_watchdog_fn(void *, u_int, int *);
113 static void aw_wdog_shutdown_fn(void *, int);
114 
115 static int
116 aw_wdog_probe(device_t dev)
117 {
118 	struct aw_wdog_softc *sc;
119 
120 	sc = device_get_softc(dev);
121 
122 	if (!ofw_bus_status_okay(dev))
123 		return (ENXIO);
124 	switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
125 	case A10_WATCHDOG:
126 		device_set_desc(dev, "Allwinner A10 Watchdog");
127 		return (BUS_PROBE_DEFAULT);
128 	case A31_WATCHDOG:
129 		device_set_desc(dev, "Allwinner A31 Watchdog");
130 		return (BUS_PROBE_DEFAULT);
131 	}
132 	return (ENXIO);
133 }
134 
135 static int
136 aw_wdog_attach(device_t dev)
137 {
138 	struct aw_wdog_softc *sc;
139 	int rid;
140 
141 	if (aw_wdog_sc != NULL)
142 		return (ENXIO);
143 
144 	sc = device_get_softc(dev);
145 	sc->dev = dev;
146 
147 	rid = 0;
148 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
149 	if (sc->res == NULL) {
150 		device_printf(dev, "could not allocate memory resource\n");
151 		return (ENXIO);
152 	}
153 
154 	aw_wdog_sc = sc;
155 
156 	switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
157 	case A10_WATCHDOG:
158 		sc->wdog_ctrl = A10_WDOG_CTRL;
159 		sc->wdog_mode = A10_WDOG_MODE;
160 		sc->wdog_mode_intvl_shift = A10_WDOG_MODE_INTVL_SHIFT;
161 		sc->wdog_mode_en = A10_WDOG_MODE_RST_EN | WDOG_MODE_EN;
162 		break;
163 	case A31_WATCHDOG:
164 		sc->wdog_ctrl = A31_WDOG_CTRL;
165 		sc->wdog_ctrl_key = A31_WDOG_CTRL_KEY;
166 		sc->wdog_mode = A31_WDOG_MODE;
167 		sc->wdog_mode_intvl_shift = A31_WDOG_MODE_INTVL_SHIFT;
168 		sc->wdog_mode_en = WDOG_MODE_EN;
169 		sc->wdog_config = A31_WDOG_CONFIG;
170 		sc->wdog_config_value = A31_WDOG_CONFIG_RST_EN_SYSTEM;
171 		break;
172 	default:
173 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res);
174 		return (ENXIO);
175 	}
176 
177 	mtx_init(&sc->mtx, "AW Watchdog", "aw_wdog", MTX_DEF);
178 	EVENTHANDLER_REGISTER(watchdog_list, aw_wdog_watchdog_fn, sc, 0);
179 	EVENTHANDLER_REGISTER(shutdown_final, aw_wdog_shutdown_fn, sc,
180 	    SHUTDOWN_PRI_LAST - 1);
181 
182 	return (0);
183 }
184 
185 static void
186 aw_wdog_watchdog_fn(void *private, u_int cmd, int *error)
187 {
188 	struct aw_wdog_softc *sc;
189 	uint64_t ms;
190 	int i;
191 
192 	sc = private;
193 	mtx_lock(&sc->mtx);
194 
195 	cmd &= WD_INTERVAL;
196 
197 	if (cmd > 0) {
198 		ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
199 		i = 0;
200 		while (wd_intervals[i].milliseconds &&
201 		    (ms > wd_intervals[i].milliseconds))
202 			i++;
203 		if (wd_intervals[i].milliseconds) {
204 			WRITE(sc, sc->wdog_mode,
205 			  (wd_intervals[i].value << sc->wdog_mode_intvl_shift) |
206 			    sc->wdog_mode_en);
207 			WRITE(sc, sc->wdog_ctrl,
208 			    WDOG_CTRL_RESTART | sc->wdog_ctrl_key);
209 			if (sc->wdog_config)
210 				WRITE(sc, sc->wdog_config,
211 				    sc->wdog_config_value);
212 			*error = 0;
213 		}
214 		else {
215 			/*
216 			 * Can't arm
217 			 * disable watchdog as watchdog(9) requires
218 			 */
219 			device_printf(sc->dev,
220 			    "Can't arm, timeout is more than 16 sec\n");
221 			mtx_unlock(&sc->mtx);
222 			WRITE(sc, sc->wdog_mode, 0);
223 			return;
224 		}
225 	}
226 	else
227 		WRITE(sc, sc->wdog_mode, 0);
228 
229 	mtx_unlock(&sc->mtx);
230 }
231 
232 static void
233 aw_wdog_shutdown_fn(void *private, int howto)
234 {
235 	if ((howto & (RB_POWEROFF|RB_HALT)) == 0)
236 		aw_wdog_watchdog_reset();
237 }
238 
239 void
240 aw_wdog_watchdog_reset(void)
241 {
242 
243 	if (aw_wdog_sc == NULL) {
244 		printf("Reset: watchdog device has not been initialized\n");
245 		return;
246 	}
247 
248 	WRITE(aw_wdog_sc, aw_wdog_sc->wdog_mode,
249 	    (wd_intervals[0].value << aw_wdog_sc->wdog_mode_intvl_shift) |
250 	    aw_wdog_sc->wdog_mode_en);
251 	if (aw_wdog_sc->wdog_config)
252 		WRITE(aw_wdog_sc, aw_wdog_sc->wdog_config,
253 		      aw_wdog_sc->wdog_config_value);
254 	WRITE(aw_wdog_sc, aw_wdog_sc->wdog_ctrl,
255 	    WDOG_CTRL_RESTART | aw_wdog_sc->wdog_ctrl_key);
256 	while(1)
257 		;
258 
259 }
260 
261 static device_method_t aw_wdog_methods[] = {
262 	DEVMETHOD(device_probe, aw_wdog_probe),
263 	DEVMETHOD(device_attach, aw_wdog_attach),
264 
265 	DEVMETHOD_END
266 };
267 
268 static driver_t aw_wdog_driver = {
269 	"aw_wdog",
270 	aw_wdog_methods,
271 	sizeof(struct aw_wdog_softc),
272 };
273 static devclass_t aw_wdog_devclass;
274 
275 DRIVER_MODULE(aw_wdog, simplebus, aw_wdog_driver, aw_wdog_devclass, 0, 0);
276