xref: /freebsd/sys/arm64/apple/apple_wdog.c (revision f1ddb6fb8c4d051a205dae3a848776c9d56f86ff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 Michael J. Karels <karels@freebsd.org>
5  * Copyright (c) 2012 Alexander Rybalko <ray@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/reboot.h>
41 #include <sys/rman.h>
42 #include <sys/systm.h>
43 #include <sys/watchdog.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <dev/clk/clk.h>
50 
51 #include <machine/bus.h>
52 #include <machine/machdep.h>
53 
54 #define	APPLE_WDOG_WD0_TIMER		0x0000
55 #define	APPLE_WDOG_WD0_RESET		0x0004
56 #define	APPLE_WDOG_WD0_INTR		0x0008
57 #define	APPLE_WDOG_WD0_CNTL		0x000c
58 
59 #define	APPLE_WDOG_WD1_TIMER		0x0010
60 #define	APPLE_WDOG_WD1_RESET		0x0014
61 #define	APPLE_WDOG_WD1_CNTL		0x001c
62 
63 #define	APPLE_WDOG_WD2_TIMER		0x0020
64 #define	APPLE_WDOG_WD2_RESET		0x0024
65 #define	APPLE_WDOG_WD2_CNTL		0x002c
66 
67 #define	APPLE_WDOG_CNTL_INTENABLE	0x0001
68 #define	APPLE_WDOG_CNTL_INTSTAT		0x0002
69 #define	APPLE_WDOG_CNTL_RSTENABLE	0x0004
70 
71 #define	READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r))
72 #define	WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r), (_v))
73 
74 struct apple_wdog_softc {
75 	device_t		dev;
76 	struct resource *	res;
77 	bus_space_tag_t		bst;
78 	bus_space_handle_t	bsh;
79 	clk_t			clk;
80 	uint64_t		clk_freq;
81 	struct mtx		mtx;
82 };
83 
84 static struct ofw_compat_data compat_data[] = {
85 	{"apple,wdt",		1},
86 	{NULL,			0}
87 };
88 
89 static void apple_wdog_watchdog_fn(void *private, u_int cmd, int *error);
90 static void apple_wdog_reboot_system(void *, int);
91 
92 static int
93 apple_wdog_probe(device_t dev)
94 {
95 
96 	if (!ofw_bus_status_okay(dev))
97 		return (ENXIO);
98 
99 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
100 		return (ENXIO);
101 
102 	device_set_desc(dev, "Apple Watchdog");
103 
104 	return (BUS_PROBE_DEFAULT);
105 }
106 
107 static int
108 apple_wdog_attach(device_t dev)
109 {
110 	struct apple_wdog_softc *sc;
111 	int error, rid;
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 	sc->bst = rman_get_bustag(sc->res);
124 	sc->bsh = rman_get_bushandle(sc->res);
125 
126 	error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
127 	if (error != 0) {
128 		device_printf(dev, "cannot get clock\n");
129 		goto fail;
130 	}
131 	error = clk_enable(sc->clk);
132 	if (error != 0) {
133 		device_printf(dev, "cannot enable clock\n");
134 		goto fail;
135 	}
136 	error = clk_get_freq(sc->clk, &sc->clk_freq);
137 	if (error != 0) {
138 		device_printf(dev, "cannot get base frequency\n");
139 		goto fail_clk;
140 	}
141 
142 	mtx_init(&sc->mtx, "Apple Watchdog", "apple_wdog", MTX_DEF);
143 	EVENTHANDLER_REGISTER(watchdog_list, apple_wdog_watchdog_fn, sc, 0);
144 	EVENTHANDLER_REGISTER(shutdown_final, apple_wdog_reboot_system, sc,
145 	    SHUTDOWN_PRI_LAST);
146 
147 	/* Reset the watchdog timers. */
148 	WRITE(sc, APPLE_WDOG_WD0_CNTL, 0);
149 	WRITE(sc, APPLE_WDOG_WD1_CNTL, 0);
150 
151 	return (0);
152 
153 fail_clk:
154 	clk_disable(sc->clk);
155 fail:
156 	bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->res);
157 	return (error);
158 }
159 
160 static void
161 apple_wdog_watchdog_fn(void *private, u_int cmd, int *error)
162 {
163 	struct apple_wdog_softc *sc;
164 	uint64_t sec;
165 	uint32_t ticks, sec_max;
166 
167 	sc = private;
168 	mtx_lock(&sc->mtx);
169 
170 	cmd &= WD_INTERVAL;
171 
172 	if (cmd > 0) {
173 		sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000;
174 		sec_max = UINT_MAX / sc->clk_freq;
175 		if (sec == 0 || sec > sec_max) {
176 			/*
177 			 * Can't arm
178 			 * disable watchdog as watchdog(9) requires
179 			 */
180 			device_printf(sc->dev,
181 			    "Can't arm, timeout must be between 1-%d seconds\n",
182 			    sec_max);
183 			WRITE(sc, APPLE_WDOG_WD1_CNTL, 0);
184 			mtx_unlock(&sc->mtx);
185 			*error = EINVAL;
186 			return;
187 		}
188 
189 		ticks = sec * sc->clk_freq;
190 		WRITE(sc, APPLE_WDOG_WD1_TIMER, 0);
191 		WRITE(sc, APPLE_WDOG_WD1_RESET, ticks);
192 		WRITE(sc, APPLE_WDOG_WD1_CNTL, APPLE_WDOG_CNTL_RSTENABLE);
193 
194 		*error = 0;
195 	} else
196 		WRITE(sc, APPLE_WDOG_WD1_CNTL, 0);
197 
198 	mtx_unlock(&sc->mtx);
199 }
200 
201 static void
202 apple_wdog_reboot_system(void *private, int howto)
203 {
204 	struct apple_wdog_softc *sc = private;
205 
206 	/* Only handle reset. */
207 	if ((howto & (RB_HALT | RB_POWEROFF)) != 0)
208 		return;
209 
210 	printf("Resetting system ... ");
211 
212 	WRITE(sc, APPLE_WDOG_WD1_CNTL, APPLE_WDOG_CNTL_RSTENABLE);
213 	WRITE(sc, APPLE_WDOG_WD1_RESET, 1);
214 	WRITE(sc, APPLE_WDOG_WD1_TIMER, 0);
215 
216 	/* Wait for watchdog timeout; should take milliseconds. */
217 	DELAY(2000000);
218 
219 	/* Not reached ... one hopes. */
220 	printf("failed to reset.\n");
221 }
222 
223 static device_method_t apple_wdog_methods[] = {
224 	DEVMETHOD(device_probe, apple_wdog_probe),
225 	DEVMETHOD(device_attach, apple_wdog_attach),
226 
227 	DEVMETHOD_END
228 };
229 
230 static driver_t apple_wdog_driver = {
231 	"apple_wdog",
232 	apple_wdog_methods,
233 	sizeof(struct apple_wdog_softc),
234 };
235 
236 DRIVER_MODULE(apple_wdog, simplebus, apple_wdog_driver, 0, 0);
237