]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/broadcom/bcm2835/bcm2835_wdog.c
Implement bcm2836 interrupt controller for INTRNG and enable it
[FreeBSD/FreeBSD.git] / sys / arm / broadcom / bcm2835 / bcm2835_wdog.c
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
46 #include <arm/broadcom/bcm2835/bcm2835_wdog.h>
47
48 #define BCM2835_PASWORD         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))
57 #define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r), (_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 };
80
81 static void bcmwd_watchdog_fn(void *private, u_int cmd, int *error);
82
83 static int
84 bcmwd_probe(device_t dev)
85 {
86
87         if (!ofw_bus_status_okay(dev))
88                 return (ENXIO);
89
90         if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-wdt")) {
91                 device_set_desc(dev, "BCM2708/2835 Watchdog");
92                 return (BUS_PROBE_DEFAULT);
93         }
94
95         return (ENXIO);
96 }
97
98 static int
99 bcmwd_attach(device_t dev)
100 {
101         struct bcmwd_softc *sc;
102         int rid;
103
104         if (bcmwd_lsc != NULL)
105                 return (ENXIO);
106
107         sc = device_get_softc(dev);
108         sc->wdog_period = 7;
109         sc->wdog_passwd = BCM2835_PASWORD;
110         sc->wdog_armed = 0;
111         sc->dev = dev;
112
113         rid = 0;
114         sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
115         if (sc->res == NULL) {
116                 device_printf(dev, "could not allocate memory resource\n");
117                 return (ENXIO);
118         }
119
120         sc->bst = rman_get_bustag(sc->res);
121         sc->bsh = rman_get_bushandle(sc->res);
122
123         bcmwd_lsc = sc;
124         mtx_init(&sc->mtx, "BCM2835 Watchdog", "bcmwd", MTX_DEF);
125         EVENTHANDLER_REGISTER(watchdog_list, bcmwd_watchdog_fn, sc, 0);
126
127         return (0);
128 }
129
130 static void
131 bcmwd_watchdog_fn(void *private, u_int cmd, int *error)
132 {
133         struct bcmwd_softc *sc;
134         uint64_t sec;
135         uint32_t ticks, reg;
136
137         sc = private;
138         mtx_lock(&sc->mtx);
139
140         cmd &= WD_INTERVAL;
141
142         if (cmd > 0) {
143                 sec = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000000;
144                 if (sec == 0 || sec > 15) {
145                         /* 
146                          * Can't arm
147                          * disable watchdog as watchdog(9) requires
148                          */
149                         device_printf(sc->dev,
150                             "Can't arm, timeout must be between 1-15 seconds\n");
151                         WRITE(sc, BCM2835_RSTC_REG, 
152                             (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
153                             BCM2835_RSTC_RESET);
154                         mtx_unlock(&sc->mtx);
155                         return;
156                 }
157
158                 ticks = (sec << 16) & BCM2835_WDOG_TIME_MASK;
159                 reg = (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | ticks;
160                 WRITE(sc, BCM2835_WDOG_REG, reg);
161
162                 reg = READ(sc, BCM2835_RSTC_REG);
163                 reg &= BCM2835_RSTC_WRCFG_CLR;
164                 reg |= BCM2835_RSTC_WRCFG_FULL_RESET;
165                 reg |= (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT);
166                 WRITE(sc, BCM2835_RSTC_REG, reg);
167
168                 *error = 0;
169         }
170         else
171                 WRITE(sc, BCM2835_RSTC_REG, 
172                     (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
173                     BCM2835_RSTC_RESET);
174
175         mtx_unlock(&sc->mtx);
176 }
177
178 void
179 bcmwd_watchdog_reset()
180 {
181
182         if (bcmwd_lsc == NULL)
183                 return;
184
185         WRITE(bcmwd_lsc, BCM2835_WDOG_REG,
186             (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) | 10);
187
188         WRITE(bcmwd_lsc, BCM2835_RSTC_REG,
189             (READ(bcmwd_lsc, BCM2835_RSTC_REG) & BCM2835_RSTC_WRCFG_CLR) |
190                 (BCM2835_PASWORD << BCM2835_PASSWORD_SHIFT) |
191                 BCM2835_RSTC_WRCFG_FULL_RESET);
192 }
193
194 static device_method_t bcmwd_methods[] = {
195         DEVMETHOD(device_probe, bcmwd_probe),
196         DEVMETHOD(device_attach, bcmwd_attach),
197
198         DEVMETHOD_END
199 };
200
201 static driver_t bcmwd_driver = {
202         "bcmwd",
203         bcmwd_methods,
204         sizeof(struct bcmwd_softc),
205 };
206 static devclass_t bcmwd_devclass;
207
208 DRIVER_MODULE(bcmwd, simplebus, bcmwd_driver, bcmwd_devclass, 0, 0);