geodewdg.c revision 1.2.6.4 1 1.2.6.4 yamt /* $NetBSD: geodewdg.c,v 1.2.6.4 2008/01/21 09:37:12 yamt Exp $ */
2 1.2.6.2 yamt
3 1.2.6.2 yamt /*-
4 1.2.6.2 yamt * Copyright (c) 2005 David Young. All rights reserved.
5 1.2.6.2 yamt *
6 1.2.6.2 yamt * This code was written by David Young.
7 1.2.6.2 yamt *
8 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.2.6.2 yamt * modification, are permitted provided that the following conditions
10 1.2.6.2 yamt * are met:
11 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.2.6.2 yamt * documentation and/or other materials provided with the distribution.
16 1.2.6.2 yamt * 3. All advertising materials mentioning features or use of this software
17 1.2.6.2 yamt * must display the following acknowledgement:
18 1.2.6.2 yamt * This product includes software developed by David Young.
19 1.2.6.2 yamt * 4. The name of David Young may not be used to endorse or promote
20 1.2.6.2 yamt * products derived from this software without specific prior
21 1.2.6.2 yamt * written permission.
22 1.2.6.2 yamt *
23 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
24 1.2.6.2 yamt * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 1.2.6.2 yamt * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 1.2.6.2 yamt * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
27 1.2.6.2 yamt * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 1.2.6.2 yamt * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.2.6.2 yamt * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.2.6.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 1.2.6.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 1.2.6.2 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.2.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 1.2.6.2 yamt * OF SUCH DAMAGE.
35 1.2.6.2 yamt */
36 1.2.6.2 yamt /*-
37 1.2.6.2 yamt * Copyright (c) 2002 The NetBSD Foundation, Inc.
38 1.2.6.2 yamt * All rights reserved.
39 1.2.6.2 yamt *
40 1.2.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
41 1.2.6.2 yamt * by Jason R. Thorpe.
42 1.2.6.2 yamt *
43 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or without
44 1.2.6.2 yamt * modification, are permitted provided that the following conditions
45 1.2.6.2 yamt * are met:
46 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
47 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
48 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
49 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer in the
50 1.2.6.2 yamt * documentation and/or other materials provided with the distribution.
51 1.2.6.2 yamt * 3. All advertising materials mentioning features or use of this software
52 1.2.6.2 yamt * must display the following acknowledgement:
53 1.2.6.2 yamt * This product includes software developed by the NetBSD
54 1.2.6.2 yamt * Foundation, Inc. and its contributors.
55 1.2.6.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
56 1.2.6.2 yamt * contributors may be used to endorse or promote products derived
57 1.2.6.2 yamt * from this software without specific prior written permission.
58 1.2.6.2 yamt *
59 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 1.2.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 1.2.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 1.2.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 1.2.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 1.2.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 1.2.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 1.2.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 1.2.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 1.2.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 1.2.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
70 1.2.6.2 yamt */
71 1.2.6.2 yamt
72 1.2.6.2 yamt /*
73 1.2.6.2 yamt * Device driver for the watchdog timer built into the
74 1.2.6.2 yamt * AMD Geode SC1100 processor.
75 1.2.6.2 yamt */
76 1.2.6.2 yamt
77 1.2.6.2 yamt #include <sys/cdefs.h>
78 1.2.6.2 yamt
79 1.2.6.4 yamt __KERNEL_RCSID(0, "$NetBSD: geodewdg.c,v 1.2.6.4 2008/01/21 09:37:12 yamt Exp $");
80 1.2.6.2 yamt
81 1.2.6.2 yamt #include <sys/param.h>
82 1.2.6.2 yamt #include <sys/systm.h>
83 1.2.6.2 yamt #include <sys/device.h>
84 1.2.6.2 yamt #include <sys/wdog.h>
85 1.2.6.2 yamt #include <uvm/uvm_extern.h>
86 1.2.6.2 yamt #include <machine/bus.h>
87 1.2.6.2 yamt #include <dev/pci/pcivar.h>
88 1.2.6.2 yamt #include <dev/pci/pcidevs.h>
89 1.2.6.2 yamt #include <arch/i386/pci/geodevar.h>
90 1.2.6.2 yamt #include <arch/i386/pci/geodereg.h>
91 1.2.6.2 yamt #include <dev/sysmon/sysmonvar.h>
92 1.2.6.2 yamt
93 1.2.6.2 yamt #ifdef GEODE_DEBUG
94 1.2.6.2 yamt #define GEODE_DPRINTF(__x) printf __x
95 1.2.6.2 yamt #else /* GEODE_DEBUG */
96 1.2.6.2 yamt #define GEODE_DPRINTF(__x) /* nothing */
97 1.2.6.2 yamt #endif
98 1.2.6.2 yamt
99 1.2.6.2 yamt struct geode_wdog_softc {
100 1.2.6.2 yamt struct device sc_dev;
101 1.2.6.2 yamt struct geode_gcb_softc *sc_gcb_dev;
102 1.2.6.2 yamt
103 1.2.6.2 yamt uint16_t sc_countdown;
104 1.2.6.2 yamt uint8_t sc_prescale;
105 1.2.6.2 yamt struct sysmon_wdog sc_smw;
106 1.2.6.2 yamt };
107 1.2.6.2 yamt
108 1.2.6.2 yamt static int attached = 0;
109 1.2.6.2 yamt
110 1.2.6.2 yamt static void
111 1.2.6.2 yamt geode_wdog_disable(struct geode_wdog_softc *sc)
112 1.2.6.2 yamt {
113 1.2.6.2 yamt uint16_t wdcnfg;
114 1.2.6.2 yamt
115 1.2.6.2 yamt /* cancel any pending countdown */
116 1.2.6.2 yamt sc->sc_countdown = 0;
117 1.2.6.4 yamt bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
118 1.2.6.4 yamt SC1100_GCB_WDTO, 0);
119 1.2.6.2 yamt /* power-down clock */
120 1.2.6.4 yamt wdcnfg = bus_space_read_2(sc->sc_gcb_dev->sc_iot,
121 1.2.6.4 yamt sc->sc_gcb_dev->sc_ioh, SC1100_GCB_WDCNFG);
122 1.2.6.2 yamt
123 1.2.6.2 yamt GEODE_DPRINTF(("%s: wdcnfg %#04" PRIx16 " -> ", __func__, wdcnfg));
124 1.2.6.2 yamt
125 1.2.6.2 yamt wdcnfg |= SC1100_WDCNFG_WD32KPD;
126 1.2.6.2 yamt wdcnfg &= ~(SC1100_WDCNFG_WDTYPE2_MASK | SC1100_WDCNFG_WDTYPE1_MASK);
127 1.2.6.2 yamt /* This no-op is for the reader's benefit. */
128 1.2.6.2 yamt wdcnfg |= SC1100_WDCNFG_WDTYPE1_NOACTION |
129 1.2.6.2 yamt SC1100_WDCNFG_WDTYPE2_NOACTION;
130 1.2.6.4 yamt bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
131 1.2.6.4 yamt SC1100_GCB_WDCNFG, wdcnfg);
132 1.2.6.2 yamt
133 1.2.6.2 yamt GEODE_DPRINTF(("%#04" PRIx16 "\n", wdcnfg));
134 1.2.6.2 yamt }
135 1.2.6.2 yamt
136 1.2.6.2 yamt static void
137 1.2.6.2 yamt geode_wdog_enable(struct geode_wdog_softc *sc)
138 1.2.6.2 yamt {
139 1.2.6.2 yamt uint16_t wdcnfg;
140 1.2.6.2 yamt
141 1.2.6.2 yamt /* power-up clock and set prescale */
142 1.2.6.4 yamt wdcnfg = bus_space_read_2(sc->sc_gcb_dev->sc_iot,
143 1.2.6.4 yamt sc->sc_gcb_dev->sc_ioh, SC1100_GCB_WDCNFG);
144 1.2.6.2 yamt
145 1.2.6.2 yamt GEODE_DPRINTF(("%s: wdcnfg %#04" PRIx16 " -> ", __func__, wdcnfg));
146 1.2.6.2 yamt
147 1.2.6.2 yamt wdcnfg &= ~(SC1100_WDCNFG_WD32KPD | SC1100_WDCNFG_WDPRES_MASK |
148 1.2.6.2 yamt SC1100_WDCNFG_WDTYPE1_MASK | SC1100_WDCNFG_WDTYPE2_MASK);
149 1.2.6.3 yamt wdcnfg |= __SHIFTIN(sc->sc_prescale, SC1100_WDCNFG_WDPRES_MASK);
150 1.2.6.2 yamt wdcnfg |= SC1100_WDCNFG_WDTYPE1_RESET | SC1100_WDCNFG_WDTYPE2_NOACTION;
151 1.2.6.2 yamt
152 1.2.6.4 yamt bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
153 1.2.6.4 yamt SC1100_GCB_WDCNFG, wdcnfg);
154 1.2.6.2 yamt
155 1.2.6.2 yamt GEODE_DPRINTF(("%#04" PRIx16 "\n", wdcnfg));
156 1.2.6.2 yamt }
157 1.2.6.2 yamt
158 1.2.6.2 yamt static void
159 1.2.6.2 yamt geode_wdog_reset(struct geode_wdog_softc *sc)
160 1.2.6.2 yamt {
161 1.2.6.2 yamt /* set countdown */
162 1.2.6.4 yamt bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
163 1.2.6.4 yamt SC1100_GCB_WDTO, sc->sc_countdown);
164 1.2.6.2 yamt }
165 1.2.6.2 yamt
166 1.2.6.2 yamt static int
167 1.2.6.2 yamt geode_wdog_tickle(struct sysmon_wdog *smw)
168 1.2.6.2 yamt {
169 1.2.6.2 yamt int s;
170 1.2.6.2 yamt struct geode_wdog_softc *sc = smw->smw_cookie;
171 1.2.6.2 yamt
172 1.2.6.2 yamt s = splhigh();
173 1.2.6.2 yamt geode_wdog_reset(sc);
174 1.2.6.2 yamt splx(s);
175 1.2.6.2 yamt return 0;
176 1.2.6.2 yamt }
177 1.2.6.2 yamt
178 1.2.6.2 yamt static int
179 1.2.6.2 yamt geode_wdog_setmode(struct sysmon_wdog *smw)
180 1.2.6.2 yamt {
181 1.2.6.2 yamt struct geode_wdog_softc *sc = smw->smw_cookie;
182 1.2.6.2 yamt uint32_t ticks;
183 1.2.6.2 yamt int prescale, s;
184 1.2.6.2 yamt
185 1.2.6.2 yamt if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
186 1.2.6.2 yamt s = splhigh();
187 1.2.6.2 yamt geode_wdog_disable(sc);
188 1.2.6.2 yamt splx(s);
189 1.2.6.2 yamt return 0;
190 1.2.6.2 yamt }
191 1.2.6.2 yamt if (smw->smw_period == WDOG_PERIOD_DEFAULT)
192 1.2.6.2 yamt smw->smw_period = 32;
193 1.2.6.2 yamt else if (smw->smw_period > SC1100_WDIVL_MAX) /* too big? */
194 1.2.6.2 yamt return EINVAL;
195 1.2.6.2 yamt
196 1.2.6.2 yamt GEODE_DPRINTF(("%s: period %u\n", __func__, smw->smw_period));
197 1.2.6.2 yamt
198 1.2.6.2 yamt ticks = smw->smw_period * SC1100_WDCLK_HZ;
199 1.2.6.2 yamt
200 1.2.6.2 yamt GEODE_DPRINTF(("%s: ticks0 %" PRIu32 "\n", __func__, ticks));
201 1.2.6.2 yamt
202 1.2.6.2 yamt for (prescale = 0; ticks > UINT16_MAX; prescale++)
203 1.2.6.2 yamt ticks /= 2;
204 1.2.6.2 yamt
205 1.2.6.2 yamt GEODE_DPRINTF(("%s: ticks %" PRIu32 "\n", __func__, ticks));
206 1.2.6.2 yamt GEODE_DPRINTF(("%s: prescale %d\n", __func__, prescale));
207 1.2.6.2 yamt
208 1.2.6.2 yamt KASSERT(prescale <= SC1100_WDCNFG_WDPRES_MAX);
209 1.2.6.2 yamt KASSERT(ticks <= UINT16_MAX);
210 1.2.6.2 yamt
211 1.2.6.2 yamt s = splhigh();
212 1.2.6.2 yamt
213 1.2.6.2 yamt sc->sc_prescale = (uint8_t)prescale;
214 1.2.6.2 yamt sc->sc_countdown = (uint16_t)ticks;
215 1.2.6.2 yamt
216 1.2.6.2 yamt geode_wdog_enable(sc);
217 1.2.6.2 yamt
218 1.2.6.2 yamt geode_wdog_reset(sc);
219 1.2.6.2 yamt
220 1.2.6.2 yamt splx(s);
221 1.2.6.2 yamt return 0;
222 1.2.6.2 yamt }
223 1.2.6.2 yamt
224 1.2.6.2 yamt static int
225 1.2.6.4 yamt geode_wdog_match(device_t parent, struct cfdata *match, void *aux)
226 1.2.6.2 yamt {
227 1.2.6.2 yamt return !attached;
228 1.2.6.2 yamt }
229 1.2.6.2 yamt
230 1.2.6.2 yamt static void
231 1.2.6.4 yamt geode_wdog_attach(device_t parent, device_t self, void *aux)
232 1.2.6.2 yamt {
233 1.2.6.4 yamt struct geode_wdog_softc *sc = device_private(self);
234 1.2.6.2 yamt uint8_t wdsts;
235 1.2.6.2 yamt
236 1.2.6.2 yamt aprint_naive(": Watchdog Timer\n");
237 1.2.6.2 yamt aprint_normal(": AMD Geode SC1100 Watchdog Timer\n");
238 1.2.6.2 yamt
239 1.2.6.2 yamt
240 1.2.6.2 yamt /*
241 1.2.6.2 yamt * Hook up the watchdog timer.
242 1.2.6.2 yamt */
243 1.2.6.4 yamt sc->sc_gcb_dev = device_private(parent);
244 1.2.6.4 yamt sc->sc_smw.smw_name = device_xname(self);
245 1.2.6.2 yamt sc->sc_smw.smw_cookie = sc;
246 1.2.6.2 yamt sc->sc_smw.smw_setmode = geode_wdog_setmode;
247 1.2.6.2 yamt sc->sc_smw.smw_tickle = geode_wdog_tickle;
248 1.2.6.2 yamt sc->sc_smw.smw_period = 32;
249 1.2.6.2 yamt
250 1.2.6.2 yamt /*
251 1.2.6.2 yamt * Determine cause of the last reset, and issue a warning if it
252 1.2.6.2 yamt * was due to watchdog expiry.
253 1.2.6.2 yamt */
254 1.2.6.4 yamt wdsts = bus_space_read_1(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
255 1.2.6.4 yamt SC1100_GCB_WDSTS);
256 1.2.6.2 yamt
257 1.2.6.4 yamt GEODE_DPRINTF(("%s: status %#02" PRIx8 "\n", device_xname(self),
258 1.2.6.2 yamt wdsts));
259 1.2.6.2 yamt
260 1.2.6.2 yamt if (wdsts & SC1100_WDSTS_WDRST)
261 1.2.6.2 yamt aprint_error(
262 1.2.6.2 yamt "%s: WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n",
263 1.2.6.4 yamt device_xname(self));
264 1.2.6.2 yamt
265 1.2.6.2 yamt /* reset WDOVF by writing 1 to it */
266 1.2.6.4 yamt bus_space_write_1(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
267 1.2.6.4 yamt SC1100_GCB_WDSTS, wdsts & SC1100_WDSTS_WDOVF);
268 1.2.6.2 yamt
269 1.2.6.2 yamt if (sysmon_wdog_register(&sc->sc_smw) != 0)
270 1.2.6.2 yamt aprint_error("%s: unable to register watchdog with sysmon\n",
271 1.2.6.4 yamt device_xname(self));
272 1.2.6.2 yamt
273 1.2.6.2 yamt /* cancel any pending countdown */
274 1.2.6.2 yamt geode_wdog_disable(sc);
275 1.2.6.2 yamt
276 1.2.6.2 yamt attached = 1;
277 1.2.6.2 yamt }
278 1.2.6.2 yamt
279 1.2.6.4 yamt static int
280 1.2.6.4 yamt geode_wdog_detach(device_t self, int flags)
281 1.2.6.4 yamt {
282 1.2.6.4 yamt int rc;
283 1.2.6.4 yamt struct geode_wdog_softc *sc = device_private(self);
284 1.2.6.4 yamt
285 1.2.6.4 yamt if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
286 1.2.6.4 yamt if (rc == ERESTART)
287 1.2.6.4 yamt rc = EINTR;
288 1.2.6.4 yamt return rc;
289 1.2.6.4 yamt }
290 1.2.6.4 yamt
291 1.2.6.4 yamt /* cancel any pending countdown */
292 1.2.6.4 yamt geode_wdog_disable(sc);
293 1.2.6.4 yamt
294 1.2.6.4 yamt attached = 0;
295 1.2.6.4 yamt
296 1.2.6.4 yamt return 0;
297 1.2.6.4 yamt }
298 1.2.6.4 yamt
299 1.2.6.2 yamt CFATTACH_DECL(geodewdog, sizeof(struct geode_wdog_softc),
300 1.2.6.4 yamt geode_wdog_match, geode_wdog_attach, geode_wdog_detach, NULL);
301