]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/atheros/ar71xx_wdog.c
amd64: use register macros for gdb_cpu_getreg()
[FreeBSD/FreeBSD.git] / sys / mips / atheros / ar71xx_wdog.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@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 unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * Watchdog driver for AR71xx 
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/eventhandler.h>
39 #include <sys/systm.h>
40 #include <sys/watchdog.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/sysctl.h>
45
46 #include <mips/atheros/ar71xxreg.h>
47 #include <mips/atheros/ar71xx_cpudef.h>
48
49 struct ar71xx_wdog_softc {
50         device_t dev;
51         int armed;
52         int reboot_from_watchdog;
53         int watchdog_nmi;
54         int debug;
55 };
56
57 static void
58 ar71xx_wdog_watchdog_fn(void *private, u_int cmd, int *error)
59 {
60         struct ar71xx_wdog_softc *sc = private;
61         uint64_t timer_val;
62         int action;
63
64         action = RST_WDOG_ACTION_RESET;
65         if (sc->watchdog_nmi != 0)
66                 action = RST_WDOG_ACTION_NMI;
67
68         cmd &= WD_INTERVAL;
69         if (sc->debug)
70                 device_printf(sc->dev, "%s: : cmd: %x\n", __func__, cmd);
71         if (cmd > 0) {
72                 timer_val = (uint64_t)(1ULL << cmd) * ar71xx_ahb_freq() /
73                     1000000000;
74
75                 /*
76                  * Clamp the timer value in case we overflow.
77                  */
78                 if (timer_val > 0xffffffff)
79                         timer_val = 0xffffffff;
80                 if (sc->debug)
81                         device_printf(sc->dev, "%s: programming timer: %jx\n",
82                             __func__, (uintmax_t) timer_val);
83                 /*
84                  * Make sure the watchdog is set to NOACTION and give it
85                  * time to take.
86                  */
87                 ATH_WRITE_REG(AR71XX_RST_WDOG_CONTROL, RST_WDOG_ACTION_NOACTION);
88                 wmb();
89                 DELAY(100);
90
91                 /*
92                  * Update the timer value.  It's already clamped at this
93                  * point so we don't have to wrap/clamp it here.
94                  */
95                 ATH_WRITE_REG(AR71XX_RST_WDOG_TIMER, timer_val);
96                 wmb();
97                 DELAY(100);
98
99                 /*
100                  * And now, arm.
101                  */
102                 ATH_WRITE_REG(AR71XX_RST_WDOG_CONTROL, action);
103                 sc->armed = 1;
104                 *error = 0;
105         } else {
106                 if (sc->debug)
107                         device_printf(sc->dev, "%s: disarming\n", __func__);
108                 if (sc->armed) {
109                         ATH_WRITE_REG(AR71XX_RST_WDOG_CONTROL,
110                             RST_WDOG_ACTION_NOACTION);
111                         sc->armed = 0;
112                 }
113         }
114 }
115
116 static int
117 ar71xx_wdog_probe(device_t dev)
118 {
119
120         device_set_desc(dev, "Atheros AR71XX watchdog timer");
121         return (BUS_PROBE_NOWILDCARD);
122 }
123
124 static void
125 ar71xx_wdog_sysctl(device_t dev)
126 {
127         struct ar71xx_wdog_softc *sc = device_get_softc(dev);
128
129         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
130         struct sysctl_oid *tree = device_get_sysctl_tree(sc->dev);
131
132         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
133                 "debug", CTLFLAG_RW, &sc->debug, 0,
134                 "enable watchdog debugging");
135         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
136                 "nmi", CTLFLAG_RW, &sc->watchdog_nmi, 0,
137                 "watchdog triggers NMI instead of reset");
138         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
139                 "armed", CTLFLAG_RD, &sc->armed, 0,
140                 "whether the watchdog is armed");
141         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
142                 "reboot_from_watchdog", CTLFLAG_RD, &sc->reboot_from_watchdog, 0,
143                 "whether the system rebooted from the watchdog");
144 }
145
146 static int
147 ar71xx_wdog_attach(device_t dev)
148 {
149         struct ar71xx_wdog_softc *sc = device_get_softc(dev);
150
151         /* Initialise */
152         sc->reboot_from_watchdog = 0;
153         sc->armed = 0;
154         sc->debug = 0;
155
156         if (ATH_READ_REG(AR71XX_RST_WDOG_CONTROL) & RST_WDOG_LAST) {
157                 device_printf (dev, 
158                     "Previous reset was due to watchdog timeout\n");
159                 sc->reboot_from_watchdog = 1;
160         }
161
162         ATH_WRITE_REG(AR71XX_RST_WDOG_CONTROL, RST_WDOG_ACTION_NOACTION);
163
164         sc->dev = dev;
165         EVENTHANDLER_REGISTER(watchdog_list, ar71xx_wdog_watchdog_fn, sc, 0);
166         ar71xx_wdog_sysctl(dev);
167
168         return (0);
169 }
170
171 static device_method_t ar71xx_wdog_methods[] = {
172         DEVMETHOD(device_probe, ar71xx_wdog_probe),
173         DEVMETHOD(device_attach, ar71xx_wdog_attach),
174         {0, 0},
175 };
176
177 static driver_t ar71xx_wdog_driver = {
178         "ar71xx_wdog",
179         ar71xx_wdog_methods,
180         sizeof(struct ar71xx_wdog_softc),
181 };
182 static devclass_t ar71xx_wdog_devclass;
183
184 DRIVER_MODULE(ar71xx_wdog, nexus, ar71xx_wdog_driver, ar71xx_wdog_devclass, 0, 0);