wdog.c revision 1.7 1 1.7 matt /* $NetBSD: wdog.c,v 1.7 2011/07/10 00:03:53 matt Exp $ */
2 1.1 gdamore /*-
3 1.1 gdamore * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4 1.1 gdamore * Copyright (c) 2006 Garrett D'Amore.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Portions of this code were written by Garrett D'Amore for the
8 1.1 gdamore * Champaign-Urbana Community Wireless Network Project.
9 1.1 gdamore *
10 1.1 gdamore * Redistribution and use in source and binary forms, with or
11 1.1 gdamore * without modification, are permitted provided that the following
12 1.1 gdamore * conditions are met:
13 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
14 1.1 gdamore * notice, this list of conditions and the following disclaimer.
15 1.1 gdamore * 2. Redistributions in binary form must reproduce the above
16 1.1 gdamore * copyright notice, this list of conditions and the following
17 1.1 gdamore * disclaimer in the documentation and/or other materials provided
18 1.1 gdamore * with the distribution.
19 1.1 gdamore * 3. All advertising materials mentioning features or use of this
20 1.1 gdamore * software must display the following acknowledgements:
21 1.1 gdamore * This product includes software developed by the Urbana-Champaign
22 1.1 gdamore * Independent Media Center.
23 1.1 gdamore * This product includes software developed by Garrett D'Amore.
24 1.1 gdamore * 4. Urbana-Champaign Independent Media Center's name and Garrett
25 1.1 gdamore * D'Amore's name may not be used to endorse or promote products
26 1.1 gdamore * derived from this software without specific prior written permission.
27 1.1 gdamore *
28 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29 1.1 gdamore * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30 1.1 gdamore * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 1.1 gdamore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33 1.1 gdamore * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34 1.1 gdamore * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 1.1 gdamore * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 1.1 gdamore * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.1 gdamore * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40 1.1 gdamore * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.1 gdamore */
42 1.1 gdamore /*
43 1.1 gdamore * Copyright (c) 2002 Wasabi Systems, Inc.
44 1.1 gdamore * All rights reserved.
45 1.1 gdamore *
46 1.1 gdamore * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
47 1.1 gdamore *
48 1.1 gdamore * Redistribution and use in source and binary forms, with or without
49 1.1 gdamore * modification, are permitted provided that the following conditions
50 1.1 gdamore * are met:
51 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
52 1.1 gdamore * notice, this list of conditions and the following disclaimer.
53 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
54 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
55 1.1 gdamore * documentation and/or other materials provided with the distribution.
56 1.1 gdamore * 3. All advertising materials mentioning features or use of this software
57 1.1 gdamore * must display the following acknowledgement:
58 1.1 gdamore * This product includes software developed for the NetBSD Project by
59 1.1 gdamore * Wasabi Systems, Inc.
60 1.1 gdamore * 4. The name of Wasabi Systems, Inc. may not be used to endorse
61 1.1 gdamore * or promote products derived from this software without specific prior
62 1.1 gdamore * written permission.
63 1.1 gdamore *
64 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
65 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
66 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
68 1.1 gdamore * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69 1.1 gdamore * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 1.1 gdamore * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71 1.1 gdamore * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
72 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
74 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
75 1.1 gdamore */
76 1.1 gdamore
77 1.1 gdamore /*
78 1.3 gdamore * Watchdog timer support for the AR531x.
79 1.1 gdamore */
80 1.1 gdamore
81 1.1 gdamore #include <sys/cdefs.h>
82 1.7 matt __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.7 2011/07/10 00:03:53 matt Exp $");
83 1.1 gdamore
84 1.1 gdamore #include <sys/param.h>
85 1.7 matt #include <sys/cpu.h>
86 1.1 gdamore #include <sys/device.h>
87 1.1 gdamore #include <sys/wdog.h>
88 1.1 gdamore
89 1.6 matt #include <mips/atheros/include/platform.h>
90 1.1 gdamore
91 1.1 gdamore #include <dev/sysmon/sysmonvar.h>
92 1.1 gdamore
93 1.1 gdamore #ifndef WDOG_DEFAULT_PERIOD
94 1.1 gdamore #define WDOG_DEFAULT_PERIOD 5
95 1.1 gdamore #endif
96 1.1 gdamore
97 1.6 matt static int wdog_match(device_t, cfdata_t, void *);
98 1.5 dyoung static void wdog_attach(device_t, device_t, void *);
99 1.1 gdamore static int wdog_tickle(struct sysmon_wdog *);
100 1.1 gdamore static int wdog_setmode(struct sysmon_wdog *);
101 1.1 gdamore
102 1.1 gdamore struct wdog_softc {
103 1.1 gdamore struct sysmon_wdog sc_smw;
104 1.1 gdamore int sc_wdog_period;
105 1.1 gdamore int sc_wdog_max;
106 1.1 gdamore uint32_t sc_wdog_reload;
107 1.1 gdamore uint32_t sc_mult;
108 1.1 gdamore };
109 1.1 gdamore
110 1.5 dyoung CFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc),
111 1.1 gdamore wdog_match, wdog_attach, NULL, NULL);
112 1.1 gdamore
113 1.1 gdamore static int
114 1.5 dyoung wdog_match(device_t parent, struct cfdata *cf, void *aux)
115 1.1 gdamore {
116 1.1 gdamore
117 1.1 gdamore return (1);
118 1.1 gdamore }
119 1.1 gdamore
120 1.1 gdamore static void
121 1.5 dyoung wdog_attach(device_t parent, device_t self, void *aux)
122 1.1 gdamore {
123 1.5 dyoung struct wdog_softc *sc = device_private(self);
124 1.1 gdamore
125 1.6 matt sc->sc_mult = atheros_get_bus_freq();
126 1.1 gdamore sc->sc_wdog_period = WDOG_DEFAULT_PERIOD;
127 1.1 gdamore sc->sc_wdog_max = 0xffffffffU / sc->sc_mult;
128 1.1 gdamore sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
129 1.5 dyoung aprint_normal(": %d second period\n", sc->sc_wdog_period);
130 1.1 gdamore
131 1.5 dyoung sc->sc_smw.smw_name = device_xname(self);
132 1.1 gdamore sc->sc_smw.smw_cookie = sc;
133 1.1 gdamore sc->sc_smw.smw_setmode = wdog_setmode;
134 1.1 gdamore sc->sc_smw.smw_tickle = wdog_tickle;
135 1.1 gdamore sc->sc_smw.smw_period = sc->sc_wdog_period;
136 1.1 gdamore
137 1.5 dyoung if (sysmon_wdog_register(&sc->sc_smw) != 0) {
138 1.5 dyoung aprint_error_dev(self,
139 1.5 dyoung "unable to register with sysmon\n");
140 1.5 dyoung }
141 1.1 gdamore }
142 1.1 gdamore
143 1.1 gdamore static int
144 1.1 gdamore wdog_tickle(struct sysmon_wdog *smw)
145 1.1 gdamore {
146 1.1 gdamore struct wdog_softc *sc = smw->smw_cookie;
147 1.1 gdamore
148 1.6 matt atheros_wdog_reload(sc->sc_wdog_reload);
149 1.1 gdamore return (0);
150 1.1 gdamore }
151 1.1 gdamore
152 1.1 gdamore static int
153 1.1 gdamore wdog_setmode(struct sysmon_wdog *smw)
154 1.1 gdamore {
155 1.1 gdamore struct wdog_softc *sc = smw->smw_cookie;
156 1.1 gdamore
157 1.1 gdamore if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
158 1.6 matt atheros_wdog_reload(0);
159 1.1 gdamore } else {
160 1.1 gdamore
161 1.1 gdamore if (smw->smw_period == WDOG_PERIOD_DEFAULT)
162 1.1 gdamore smw->smw_period = sc->sc_wdog_period;
163 1.1 gdamore else if (smw->smw_period != sc->sc_wdog_period) {
164 1.1 gdamore /*
165 1.1 gdamore * we can't set watchdog periods in excess of
166 1.1 gdamore * the processor maximum.
167 1.1 gdamore */
168 1.1 gdamore if (smw->smw_period > sc->sc_wdog_max) {
169 1.1 gdamore return EOPNOTSUPP;
170 1.1 gdamore }
171 1.1 gdamore sc->sc_wdog_period = smw->smw_period;
172 1.1 gdamore sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
173 1.1 gdamore }
174 1.1 gdamore
175 1.6 matt atheros_wdog_reload(sc->sc_wdog_reload);
176 1.1 gdamore }
177 1.5 dyoung
178 1.1 gdamore return (0);
179 1.1 gdamore }
180