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