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