1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Alexander Rybalko <ray@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/eventhandler.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/reboot.h>
37 #include <sys/rman.h>
38 #include <sys/systm.h>
39 #include <sys/watchdog.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/broadcom/bcm2835/bcm2835_wdog.h>
49 
50 #define	BCM2835_PASSWORD	0x5a
51 
52 #define BCM2835_WDOG_RESET	0
53 #define BCM2835_PASSWORD_MASK	0xff000000
54 #define BCM2835_PASSWORD_SHIFT	24
55 #define BCM2835_WDOG_TIME_MASK	0x000fffff
56 #define BCM2835_WDOG_TIME_SHIFT	0
57 
58 #define	READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset)
59 #define	WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset, (_v))
60 
61 #define BCM2835_RSTC_WRCFG_CLR		0xffffffcf
62 #define BCM2835_RSTC_WRCFG_SET		0x00000030
63 #define BCM2835_RSTC_WRCFG_FULL_RESET	0x00000020
64 #define BCM2835_RSTC_RESET		0x00000102
65 
66 #define	BCM2835_RSTC_REG	0x00
67 #define	BCM2835_RSTS_REG	0x04
68 #define	BCM2835_WDOG_REG	0x08
69 
70 static struct bcmwd_softc *bcmwd_lsc = NULL;
71 
72 struct bcmwd_softc {
73 	device_t		dev;
74 	struct resource *	res;
75 	bus_space_tag_t		bst;
76 	bus_space_handle_t	bsh;
77 	int			wdog_armed;
78 	int			wdog_period;
79 	char			wdog_passwd;
80 	struct mtx		mtx;
81 	int			regs_offset;
82 };
83 
84 #define	BSD_DTB		1
85 #define	UPSTREAM_DTB	2
86 #define	UPSTREAM_DTB_REGS_OFFSET	0x1c
87 
88 static struct ofw_compat_data compat_data[] = {
89 	{"broadcom,bcm2835-wdt",	BSD_DTB},
90 	{"brcm,bcm2835-pm-wdt",		UPSTREAM_DTB},
91 	{"brcm,bcm2835-pm",		UPSTREAM_DTB},
92 	{NULL,				0}
93 };
94 
95 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error);
96 static void bcmwd_reboot_system(void *, int);
97 
98 static int
bcmwd_probe(device_t dev)99 bcmwd_probe(device_t dev)
100 {
101 
102 	if (!ofw_bus_status_okay(dev))
103 		return (ENXIO);
104 
105 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
106 		return (ENXIO);
107 
108 	device_set_desc(dev, "BCM2708/2835 Watchdog");
109 
110 	return (BUS_PROBE_DEFAULT);
111 }
112 
113 static int
bcmwd_attach(device_t dev)114 bcmwd_attach(device_t dev)
115 {
116 	struct bcmwd_softc *sc;
117 	int rid;
118 
119 	if (bcmwd_lsc != NULL)
120 		return (ENXIO);
121 
122 	sc = device_get_softc(dev);
123 	sc->wdog_period = 7;
124 	sc->wdog_passwd = BCM2835_PASSWORD;
125 	sc->wdog_armed = 0;
126 	sc->dev = dev;
127 
128 	rid = 0;
129 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
130 	if (sc->res == NULL) {
131 		device_printf(dev, "could not allocate memory resource\n");
132 		return (ENXIO);
133 	}
134 
135 	sc->bst = rman_get_bustag(sc->res);
136 	sc->bsh = rman_get_bushandle(sc->res);
137 
138 	/* compensate base address difference */
139 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data
140 	   == UPSTREAM_DTB)
141 		sc->regs_offset = UPSTREAM_DTB_REGS_OFFSET;
142 
143 	bcmwd_lsc = sc;
144 	mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF);
145 	EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0);
146 
147 	/*
148 	 * Handle reboot events. This needs to happen with slightly greater
149 	 * priority than the PSCI handler, since PSCI reset is not properly
150 	 * implemented on the Pi and it just puts the Pi into a halt
151 	 * state.
152 	 */
153 	EVENTHANDLER_REGISTER(shutdown_final, bcmwd_reboot_system, sc,
154 	    SHUTDOWN_PRI_LAST-1);
155 
156 	return (0);
157 }
158 
159 static void
bcmwd_watchdog_fn(void * private,u_int cmd,int * error)160 bcmwd_watchdog_fn(void *private, u_int cmd, int *error)
161 {
162 	struct bcmwd_softc *sc;
163 	uint64_t sec;
164 	uint32_t ticks, reg;
165 
166 	sc = private;
167 	mtx_lock(&sc->mtx);
168 
169 	cmd &= WD_INTERVAL;
170 
171 	if (cmd > 0) {
172 		sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000;
173 		if (sec == 0 || sec > 15) {
174 			/*
175 			 * Can't arm
176 			 * disable watchdog as watchdog(9) requires
177 			 */
178 			device_printf(sc->dev,
179 			    "Can't arm, timeout must be between 1-15 seconds\n");
180 			WRITE(sc, BCM2835_RSTC_REG,
181 			    (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
182 			    BCM2835_RSTC_RESET);
183 			mtx_unlock(&sc->mtx);
184 			*error = EINVAL;
185 			return;
186 		}
187 
188 		ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK;
189 		reg = (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | ticks;
190 		WRITE(sc, BCM2835_WDOG_REG, reg);
191 
192 		reg = READ(sc, BCM2835_RSTC_REG);
193 		reg &= BCM2835_RSTC_WRCFG_CLR;
194 		reg |= BCM2835_RSTC_WRCFG_FULL_RESET;
195 		reg |= (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT);
196 		WRITE(sc, BCM2835_RSTC_REG, reg);
197 
198 		*error = 0;
199 	}
200 	else
201 		WRITE(sc, BCM2835_RSTC_REG,
202 		    (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
203 		    BCM2835_RSTC_RESET);
204 
205 	mtx_unlock(&sc->mtx);
206 }
207 
208 void
bcmwd_watchdog_reset(void)209 bcmwd_watchdog_reset(void)
210 {
211 
212 	if (bcmwd_lsc == NULL)
213 		return;
214 
215 	WRITE(bcmwd_lsc, BCM2835_WDOG_REG,
216 	    (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 10);
217 
218 	WRITE(bcmwd_lsc, BCM2835_RSTC_REG,
219 	    (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) |
220 		(BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
221 		BCM2835_RSTC_WRCFG_FULL_RESET);
222 }
223 
224 static void
bcmwd_reboot_system(void * sc,int howto)225 bcmwd_reboot_system(void *sc, int howto)
226 {
227 	int cmd, error = 0;
228 
229 	/* Only handle reset. */
230 	if (howto & RB_HALT || howto & RB_POWEROFF)
231 		return;
232 
233 	printf("Resetting system ... ");
234 
235 	cmd = WD_TO_1SEC;
236 	bcmwd_watchdog_fn(sc, cmd, &error);
237 
238 	/* Wait for watchdog timeout. */
239 	DELAY(2000000);
240 
241 	/* Not reached ... one hopes. */
242 	printf("failed to reset (errno %d).\n", error);
243 }
244 
245 static device_method_t bcmwd_methods[] = {
246 	DEVMETHOD(device_probe, bcmwd_probe),
247 	DEVMETHOD(device_attach, bcmwd_attach),
248 
249 	DEVMETHOD_END
250 };
251 
252 static driver_t bcmwd_driver = {
253 	"bcmwd",
254 	bcmwd_methods,
255 	sizeof(struct bcmwd_softc),
256 };
257 
258 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, 0, 0);
259