1 /*-
2  * Copyright (c) 2012 Alexander Rybalko <ray@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/watchdog.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/rman.h>
36 
37 #include <dev/fdt/fdt_common.h>
38 #include <dev/ofw/openfirm.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include <machine/bus.h>
43 #include <machine/cpufunc.h>
44 #include <machine/machdep.h>
45 #include <machine/fdt.h>
46 
47 #include <arm/broadcom/bcm2835/bcm2835_wdog.h>
48 
49 #define	BCM2835_PASWORD		0x5a
50 
51 #define BCM2835_WDOG_RESET	0
52 #define BCM2835_PASSWORD_MASK	0xff000000
53 #define BCM2835_PASSWORD_SHIFT	24
54 #define BCM2835_WDOG_TIME_MASK	0x000fffff
55 #define BCM2835_WDOG_TIME_SHIFT	0
56 
57 #define	READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r))
58 #define	WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r), (_v))
59 
60 #define BCM2835_RSTC_WRCFG_CLR		0xffffffcf
61 #define BCM2835_RSTC_WRCFG_SET		0x00000030
62 #define BCM2835_RSTC_WRCFG_FULL_RESET	0x00000020
63 #define BCM2835_RSTC_RESET		0x00000102
64 
65 #define	BCM2835_RSTC_REG	0x00
66 #define	BCM2835_RSTS_REG	0x04
67 #define	BCM2835_WDOG_REG	0x08
68 
69 static struct bcmwd_softc *bcmwd_lsc = NULL;
70 
71 struct bcmwd_softc {
72 	device_t		dev;
73 	struct resource *	res;
74 	bus_space_tag_t		bst;
75 	bus_space_handle_t	bsh;
76 	int			wdog_armed;
77 	int			wdog_period;
78 	char			wdog_passwd;
79 	struct mtx		mtx;
80 };
81 
82 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error);
83 
84 static int
85 bcmwd_probe(device_t dev)
86 {
87 
88 	if (!ofw_bus_status_okay(dev))
89 		return (ENXIO);
90 
91 	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-wdt")) {
92 		device_set_desc(dev, "BCM2708/2835 Watchdog");
93 		return (BUS_PROBE_DEFAULT);
94 	}
95 
96 	return (ENXIO);
97 }
98 
99 static int
100 bcmwd_attach(device_t dev)
101 {
102 	struct bcmwd_softc *sc;
103 	int rid;
104 
105 	if (bcmwd_lsc != NULL)
106 		return (ENXIO);
107 
108 	sc = device_get_softc(dev);
109 	sc->wdog_period = 7;
110 	sc->wdog_passwd = BCM2835_PASWORD;
111 	sc->wdog_armed = 0;
112 	sc->dev = dev;
113 
114 	rid = 0;
115 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
116 	if (sc->res == NULL) {
117 		device_printf(dev, "could not allocate memory resource\n");
118 		return (ENXIO);
119 	}
120 
121 	sc->bst = rman_get_bustag(sc->res);
122 	sc->bsh = rman_get_bushandle(sc->res);
123 
124 	bcmwd_lsc = sc;
125 	mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF);
126 	EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0);
127 
128 	return (0);
129 }
130 
131 static void
132 bcmwd_watchdog_fn(void *private, u_int cmd, int *error)
133 {
134 	struct bcmwd_softc *sc;
135 	uint64_t sec;
136 	uint32_t ticks, reg;
137 
138 	sc = private;
139 	mtx_lock(&sc->mtx);
140 
141 	cmd &= WD_INTERVAL;
142 
143 	if (cmd > 0) {
144 		sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000;
145 		ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK;
146 		if (ticks == 0) {
147 			/*
148 			 * Can't arm
149 			 * disable watchdog as watchdog(9) requires
150 			 */
151 			device_printf(sc->dev,
152 			    "Can't arm, timeout is less than 1 second\n");
153 			WRITE(sc, BCM2835_RSTC_REG,
154 			    (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
155 			    BCM2835_RSTC_RESET);
156 			mtx_unlock(&sc->mtx);
157 			return;
158 		}
159 
160 		reg = (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | ticks;
161 		WRITE(sc, BCM2835_WDOG_REG, reg);
162 
163 		reg = READ(sc, BCM2835_RSTC_REG);
164 		reg &= BCM2835_RSTC_WRCFG_CLR;
165 		reg |= BCM2835_RSTC_WRCFG_FULL_RESET;
166 		reg |= (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT);
167 		WRITE(sc, BCM2835_RSTC_REG, reg);
168 
169 		*error = 0;
170 	}
171 	else
172 		WRITE(sc, BCM2835_RSTC_REG,
173 		    (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
174 		    BCM2835_RSTC_RESET);
175 
176 	mtx_unlock(&sc->mtx);
177 }
178 
179 void
180 bcmwd_watchdog_reset()
181 {
182 
183 	if (bcmwd_lsc == NULL)
184 		return;
185 
186 	WRITE(bcmwd_lsc, BCM2835_WDOG_REG,
187 	    (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 10);
188 
189 	WRITE(bcmwd_lsc, BCM2835_RSTC_REG,
190 	    (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) |
191 		(BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
192 		BCM2835_RSTC_WRCFG_FULL_RESET);
193 }
194 
195 static device_method_t bcmwd_methods[] = {
196 	DEVMETHOD(device_probe, bcmwd_probe),
197 	DEVMETHOD(device_attach, bcmwd_attach),
198 
199 	DEVMETHOD_END
200 };
201 
202 static driver_t bcmwd_driver = {
203 	"bcmwd",
204 	bcmwd_methods,
205 	sizeof(struct bcmwd_softc),
206 };
207 static devclass_t bcmwd_devclass;
208 
209 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0);
210