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