]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/rockchip/rk30xx_wdog.c
Import Zstandard 1.2.0
[FreeBSD/FreeBSD.git] / sys / arm / rockchip / rk30xx_wdog.c
1 /*-
2  * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@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/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.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 #include <machine/fdt.h>
46
47 #include <arm/rockchip/rk30xx_wdog.h>
48
49 #ifndef RK30_WDT_BASE
50 #define RK30_WDT_BASE           0x2004c000
51 #define RK30_WDT_PSIZE          0x100
52 #endif
53
54 #define RK30_WDT_READ(_sc, _r)          bus_read_4((_sc)->res, (_r))
55 #define RK30_WDT_WRITE(_sc, _r, _v)     bus_write_4((_sc)->res, (_r), (_v))
56
57 #define WDOG_CTRL               0x00
58 #define WDOG_CTRL_EN            (1 << 0)
59 #define WDOG_CTRL_RSP_MODE      (1 << 1)
60 #define WDOG_CTRL_RST_PULSE     (4 << 2)
61 #define WDOG_CTRL_RST           0xa
62 #define WDOG_TORR               0x04
63 #define WDOG_TORR_INTVL_SHIFT   0
64 #define WDOG_CCVR               0x08
65 #define WDOG_CRR                0x0c
66 #define WDOG_CRR_PWD            0x76
67 #define WDOG_STAT               0x10
68 #define WDOG_EOI                0x14
69
70 static struct rk30_wd_softc *rk30_wd_sc = NULL;
71
72 struct rk30_wd_softc {
73         device_t                dev;
74         struct resource         *res;
75         struct mtx              mtx;
76         int                     freq;
77 };
78
79 static void rk30_wd_watchdog_fn(void *private, u_int cmd, int *error);
80
81 static int
82 rk30_wd_probe(device_t dev)
83 {
84
85         if (!ofw_bus_status_okay(dev))
86                 return (ENXIO);
87
88         if (ofw_bus_is_compatible(dev, "rockchip,rk30xx-wdt")) {
89                 device_set_desc(dev, "Rockchip RK30XX Watchdog");
90                 return (BUS_PROBE_DEFAULT);
91         }
92
93         return (ENXIO);
94 }
95
96 static int
97 rk30_wd_attach(device_t dev)
98 {
99         struct rk30_wd_softc *sc;
100         int rid;
101         phandle_t node;
102         pcell_t cell;
103
104         if (rk30_wd_sc != NULL)
105                 return (ENXIO);
106
107         sc = device_get_softc(dev);
108         sc->dev = dev;
109
110         node = ofw_bus_get_node(sc->dev);
111         if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) > 0)
112                 sc->freq = cell / 1000000;
113         else
114                 return (ENXIO);
115
116         rid = 0;
117         sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
118         if (sc->res == NULL) {
119                 device_printf(dev, "could not allocate memory resource\n");
120                 return (ENXIO);
121         }
122
123         rk30_wd_sc = sc;
124         mtx_init(&sc->mtx, "RK30XX Watchdog", "rk30_wd", MTX_DEF);
125         EVENTHANDLER_REGISTER(watchdog_list, rk30_wd_watchdog_fn, sc, 0);
126
127         return (0);
128 }
129
130 static void
131 rk30_wd_watchdog_fn(void *private, u_int cmd, int *error)
132 {
133         struct rk30_wd_softc *sc;
134         uint64_t ms, m, max;
135         int i;
136
137         sc = private;
138         mtx_lock(&sc->mtx);
139
140         cmd &= WD_INTERVAL;
141
142         if (cmd > 0) {
143                 ms = ((uint64_t)1 << (cmd & WD_INTERVAL)) / 1000000;
144                 m = 0xffff / sc->freq;
145                 max = 0x7fffffff / sc->freq + 1;
146                 i = 0;
147                 while (m < max && m < ms) {
148                         m <<= 1;
149                         i++;
150                 }
151                 if (m < max) {
152                         RK30_WDT_WRITE(sc, WDOG_TORR,
153                             i << WDOG_TORR_INTVL_SHIFT);
154                         RK30_WDT_WRITE(sc, WDOG_CTRL,
155                             WDOG_CTRL_EN | WDOG_CTRL_RSP_MODE |
156                             WDOG_CTRL_RST_PULSE);
157                         RK30_WDT_WRITE(sc, WDOG_CRR, WDOG_CRR_PWD);
158                         *error = 0;
159                 } else {
160                         device_printf(sc->dev, "Can not be disabled\n");
161                         mtx_unlock(&sc->mtx);
162                         RK30_WDT_WRITE(sc, WDOG_CTRL, WDOG_CTRL_RST);
163                         return;
164                 }
165         }
166         else
167                 RK30_WDT_WRITE(sc, WDOG_CTRL, WDOG_CTRL_RST);
168
169         mtx_unlock(&sc->mtx);
170 }
171
172 void
173 rk30_wd_watchdog_reset(void)
174 {
175         bus_space_handle_t bsh;
176
177         bus_space_map(fdtbus_bs_tag, RK30_WDT_BASE, RK30_WDT_PSIZE, 0, &bsh);
178         bus_space_write_4(fdtbus_bs_tag, bsh, WDOG_TORR, 0);
179         bus_space_write_4(fdtbus_bs_tag, bsh, WDOG_CTRL,
180             WDOG_CTRL_EN | WDOG_CTRL_RSP_MODE | WDOG_CTRL_RST_PULSE);
181
182         while (1);
183 }
184
185 static device_method_t rk30_wd_methods[] = {
186         DEVMETHOD(device_probe, rk30_wd_probe),
187         DEVMETHOD(device_attach, rk30_wd_attach),
188
189         DEVMETHOD_END
190 };
191
192 static driver_t rk30_wd_driver = {
193         "rk30_wd",
194         rk30_wd_methods,
195         sizeof(struct rk30_wd_softc),
196 };
197 static devclass_t rk30_wd_devclass;
198
199 DRIVER_MODULE(rk30_wd, simplebus, rk30_wd_driver, rk30_wd_devclass, 0, 0);