xref: /freebsd-13-stable/sys/mips/atheros/ar531x/ar5315_wdog.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * Copyright (c) 2016, Hiroki Mori
3  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    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 
29 /*
30  * Watchdog driver for AR5315
31  */
32 
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/watchdog.h>
37 #include <sys/bus.h>
38 #include <sys/eventhandler.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/sysctl.h>
42 
43 #include <mips/atheros/ar531x/ar5315reg.h>
44 #include <mips/atheros/ar531x/ar5315_cpudef.h>
45 
46 struct ar5315_wdog_softc {
47 	device_t dev;
48 	int armed;
49 	int reboot_from_watchdog;
50 	int debug;
51 };
52 
53 static void
ar5315_wdog_watchdog_fn(void * private,u_int cmd,int * error)54 ar5315_wdog_watchdog_fn(void *private, u_int cmd, int *error)
55 {
56 	struct ar5315_wdog_softc *sc = private;
57 	uint64_t timer_val;
58 
59 	cmd &= WD_INTERVAL;
60 	if (sc->debug)
61 		device_printf(sc->dev, "ar5315_wdog_watchdog_fn: cmd: %x\n", cmd);
62 	if (cmd > 0) {
63 		timer_val = (uint64_t)(1ULL << cmd) * ar531x_ahb_freq() /
64 		    1000000000;
65 		if (sc->debug)
66 			device_printf(sc->dev, "ar5315_wdog_watchdog_fn: programming timer: %jx\n", (uintmax_t) timer_val);
67 		/*
68 		 * Load timer with large enough value to prevent spurious
69 		 * reset
70 		 */
71 		ATH_WRITE_REG(ar531x_wdog_timer(),
72 		    ar531x_ahb_freq() * 10);
73 		ATH_WRITE_REG(ar531x_wdog_ctl(),
74 		    AR5315_WDOG_CTL_RESET);
75 		ATH_WRITE_REG(ar531x_wdog_timer(),
76 		    (timer_val & 0xffffffff));
77 		sc->armed = 1;
78 		*error = 0;
79 	} else {
80 		if (sc->debug)
81 			device_printf(sc->dev, "ar5315_wdog_watchdog_fn: disarming\n");
82 		if (sc->armed) {
83 			ATH_WRITE_REG(ar531x_wdog_ctl(),
84 			    AR5315_WDOG_CTL_IGNORE);
85 			sc->armed = 0;
86 		}
87 	}
88 }
89 
90 static int
ar5315_wdog_probe(device_t dev)91 ar5315_wdog_probe(device_t dev)
92 {
93 
94 	device_set_desc(dev, "Atheros AR531x watchdog timer");
95 	return (0);
96 }
97 
98 static void
ar5315_wdog_sysctl(device_t dev)99 ar5315_wdog_sysctl(device_t dev)
100 {
101 	struct ar5315_wdog_softc *sc = device_get_softc(dev);
102 
103         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
104         struct sysctl_oid *tree = device_get_sysctl_tree(sc->dev);
105 
106         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
107                 "debug", CTLFLAG_RW, &sc->debug, 0,
108                 "enable watchdog debugging");
109         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
110                 "armed", CTLFLAG_RD, &sc->armed, 0,
111                 "whether the watchdog is armed");
112         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
113                 "reboot_from_watchdog", CTLFLAG_RD, &sc->reboot_from_watchdog, 0,
114                 "whether the system rebooted from the watchdog");
115 }
116 
117 static int
ar5315_wdog_attach(device_t dev)118 ar5315_wdog_attach(device_t dev)
119 {
120 	struct ar5315_wdog_softc *sc = device_get_softc(dev);
121 
122 	/* Initialise */
123 	sc->reboot_from_watchdog = 0;
124 	sc->armed = 0;
125 	sc->debug = 0;
126 	ATH_WRITE_REG(ar531x_wdog_ctl(), AR5315_WDOG_CTL_IGNORE);
127 
128 	sc->dev = dev;
129 	EVENTHANDLER_REGISTER(watchdog_list, ar5315_wdog_watchdog_fn, sc, 0);
130 	ar5315_wdog_sysctl(dev);
131 
132 	return (0);
133 }
134 
135 static device_method_t ar5315_wdog_methods[] = {
136 	DEVMETHOD(device_probe, ar5315_wdog_probe),
137 	DEVMETHOD(device_attach, ar5315_wdog_attach),
138 	DEVMETHOD_END
139 };
140 
141 static driver_t ar5315_wdog_driver = {
142 	"ar5315_wdog",
143 	ar5315_wdog_methods,
144 	sizeof(struct ar5315_wdog_softc),
145 };
146 static devclass_t ar5315_wdog_devclass;
147 
148 DRIVER_MODULE(ar5315_wdog, apb, ar5315_wdog_driver, ar5315_wdog_devclass, 0, 0);
149