xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_wdog.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
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 };
80 
81 #ifdef notyet
82 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error);
83 #endif
84 
85 static int
86 bcmwd_probe(device_t dev)
87 {
88 
89 	if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-wdt")) {
90 		device_set_desc(dev, "BCM2708/2835 Watchdog");
91 		return (BUS_PROBE_DEFAULT);
92 	}
93 
94 	return (ENXIO);
95 }
96 
97 static int
98 bcmwd_attach(device_t dev)
99 {
100 	struct bcmwd_softc *sc;
101 	int rid;
102 
103 	if (bcmwd_lsc != NULL)
104 		return (ENXIO);
105 
106 	sc = device_get_softc(dev);
107 	sc->wdog_period = 7;
108 	sc->wdog_passwd = BCM2835_PASWORD;
109 	sc->wdog_armed = 0;
110 	sc->dev = dev;
111 
112 	rid = 0;
113 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
114 	if (sc->res == NULL) {
115 		device_printf(dev, "could not allocate memory resource\n");
116 		return (ENXIO);
117 	}
118 
119 	sc->bst = rman_get_bustag(sc->res);
120 	sc->bsh = rman_get_bushandle(sc->res);
121 
122 	bcmwd_lsc = sc;
123 #ifdef notyet
124 	EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0);
125 #endif
126 	return (0);
127 }
128 
129 #ifdef notyet
130 static void
131 bcmwd_watchdog_fn(void *private, u_int cmd, int *error)
132 {
133 	/* XXX: not yet */
134 }
135 #endif
136 
137 void
138 bcmwd_watchdog_reset()
139 {
140 
141 	if (bcmwd_lsc == NULL)
142 		return;
143 
144 	WRITE(bcmwd_lsc, BCM2835_WDOG_REG,
145 	    (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 10);
146 
147 	WRITE(bcmwd_lsc, BCM2835_RSTC_REG,
148 	    (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) |
149 		(BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
150 		BCM2835_RSTC_WRCFG_FULL_RESET);
151 }
152 
153 static device_method_t bcmwd_methods[] = {
154 	DEVMETHOD(device_probe, bcmwd_probe),
155 	DEVMETHOD(device_attach, bcmwd_attach),
156 
157 	DEVMETHOD_END
158 };
159 
160 static driver_t bcmwd_driver = {
161 	"bcmwd",
162 	bcmwd_methods,
163 	sizeof(struct bcmwd_softc),
164 };
165 static devclass_t bcmwd_devclass;
166 
167 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0);
168