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