xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_wdog.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/rman.h>
39 #include <sys/systm.h>
40 #include <sys/watchdog.h>
41 
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/broadcom/bcm2835/bcm2835_wdog.h>
50 
51 #define	BCM2835_PASSWORD	0x5a
52 
53 #define BCM2835_WDOG_RESET	0
54 #define BCM2835_PASSWORD_MASK	0xff000000
55 #define BCM2835_PASSWORD_SHIFT	24
56 #define BCM2835_WDOG_TIME_MASK	0x000fffff
57 #define BCM2835_WDOG_TIME_SHIFT	0
58 
59 #define	READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset)
60 #define	WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r) + (_sc)->regs_offset, (_v))
61 
62 #define BCM2835_RSTC_WRCFG_CLR		0xffffffcf
63 #define BCM2835_RSTC_WRCFG_SET		0x00000030
64 #define BCM2835_RSTC_WRCFG_FULL_RESET	0x00000020
65 #define BCM2835_RSTC_RESET		0x00000102
66 
67 #define	BCM2835_RSTC_REG	0x00
68 #define	BCM2835_RSTS_REG	0x04
69 #define	BCM2835_WDOG_REG	0x08
70 
71 static struct bcmwd_softc *bcmwd_lsc = NULL;
72 
73 struct bcmwd_softc {
74 	device_t		dev;
75 	struct resource *	res;
76 	bus_space_tag_t		bst;
77 	bus_space_handle_t	bsh;
78 	int			wdog_armed;
79 	int			wdog_period;
80 	char			wdog_passwd;
81 	struct mtx		mtx;
82 	int			regs_offset;
83 };
84 
85 #define	BSD_DTB		1
86 #define	UPSTREAM_DTB	2
87 #define	UPSTREAM_DTB_REGS_OFFSET	0x1c
88 
89 static struct ofw_compat_data compat_data[] = {
90 	{"broadcom,bcm2835-wdt",	BSD_DTB},
91 	{"brcm,bcm2835-pm-wdt",		UPSTREAM_DTB},
92 	{NULL,				0}
93 };
94 
95 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error);
96 
97 static int
98 bcmwd_probe(device_t dev)
99 {
100 
101 	if (!ofw_bus_status_okay(dev))
102 		return (ENXIO);
103 
104 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
105 		return (ENXIO);
106 
107 	device_set_desc(dev, "BCM2708/2835 Watchdog");
108 
109 	return (BUS_PROBE_DEFAULT);
110 }
111 
112 static int
113 bcmwd_attach(device_t dev)
114 {
115 	struct bcmwd_softc *sc;
116 	int rid;
117 
118 	if (bcmwd_lsc != NULL)
119 		return (ENXIO);
120 
121 	sc = device_get_softc(dev);
122 	sc->wdog_period = 7;
123 	sc->wdog_passwd = BCM2835_PASSWORD;
124 	sc->wdog_armed = 0;
125 	sc->dev = dev;
126 
127 	rid = 0;
128 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
129 	if (sc->res == NULL) {
130 		device_printf(dev, "could not allocate memory resource\n");
131 		return (ENXIO);
132 	}
133 
134 	sc->bst = rman_get_bustag(sc->res);
135 	sc->bsh = rman_get_bushandle(sc->res);
136 
137 	/* compensate base address difference */
138 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data
139 	   == UPSTREAM_DTB)
140 		sc->regs_offset = UPSTREAM_DTB_REGS_OFFSET;
141 
142 	bcmwd_lsc = sc;
143 	mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF);
144 	EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0);
145 
146 	return (0);
147 }
148 
149 static void
150 bcmwd_watchdog_fn(void *private, u_int cmd, int *error)
151 {
152 	struct bcmwd_softc *sc;
153 	uint64_t sec;
154 	uint32_t ticks, reg;
155 
156 	sc = private;
157 	mtx_lock(&sc->mtx);
158 
159 	cmd &= WD_INTERVAL;
160 
161 	if (cmd > 0) {
162 		sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000;
163 		if (sec == 0 || sec > 15) {
164 			/*
165 			 * Can't arm
166 			 * disable watchdog as watchdog(9) requires
167 			 */
168 			device_printf(sc->dev,
169 			    "Can't arm, timeout must be between 1-15 seconds\n");
170 			WRITE(sc, BCM2835_RSTC_REG,
171 			    (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
172 			    BCM2835_RSTC_RESET);
173 			mtx_unlock(&sc->mtx);
174 			return;
175 		}
176 
177 		ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK;
178 		reg = (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | ticks;
179 		WRITE(sc, BCM2835_WDOG_REG, reg);
180 
181 		reg = READ(sc, BCM2835_RSTC_REG);
182 		reg &= BCM2835_RSTC_WRCFG_CLR;
183 		reg |= BCM2835_RSTC_WRCFG_FULL_RESET;
184 		reg |= (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT);
185 		WRITE(sc, BCM2835_RSTC_REG, reg);
186 
187 		*error = 0;
188 	}
189 	else
190 		WRITE(sc, BCM2835_RSTC_REG,
191 		    (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
192 		    BCM2835_RSTC_RESET);
193 
194 	mtx_unlock(&sc->mtx);
195 }
196 
197 void
198 bcmwd_watchdog_reset(void)
199 {
200 
201 	if (bcmwd_lsc == NULL)
202 		return;
203 
204 	WRITE(bcmwd_lsc, BCM2835_WDOG_REG,
205 	    (BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) | 10);
206 
207 	WRITE(bcmwd_lsc, BCM2835_RSTC_REG,
208 	    (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) |
209 		(BCM2835_PASSWORD << BCM2835_PASSWORD_SHIFT) |
210 		BCM2835_RSTC_WRCFG_FULL_RESET);
211 }
212 
213 static device_method_t bcmwd_methods[] = {
214 	DEVMETHOD(device_probe, bcmwd_probe),
215 	DEVMETHOD(device_attach, bcmwd_attach),
216 
217 	DEVMETHOD_END
218 };
219 
220 static driver_t bcmwd_driver = {
221 	"bcmwd",
222 	bcmwd_methods,
223 	sizeof(struct bcmwd_softc),
224 };
225 static devclass_t bcmwd_devclass;
226 
227 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0);
228