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