xref: /netbsd-src/sys/arch/x86/pci/ichlpcib.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: ichlpcib.c,v 1.14 2008/10/13 15:10:51 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Minoura Makoto and Matthew R. Green.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Intel I/O Controller Hub (ICHn) LPC Interface Bridge driver
34  *
35  *  LPC Interface Bridge is basically a pcib (PCI-ISA Bridge), but has
36  *  some power management and monitoring functions.
37  *  Currently we support the watchdog timer, SpeedStep (on some systems)
38  *  and the power management timer.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: ichlpcib.c,v 1.14 2008/10/13 15:10:51 joerg Exp $");
43 
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/sysctl.h>
49 #include <sys/timetc.h>
50 #include <machine/bus.h>
51 
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcidevs.h>
55 
56 #include <dev/sysmon/sysmonvar.h>
57 
58 #include <dev/ic/acpipmtimer.h>
59 #include <dev/ic/i82801lpcreg.h>
60 #include <dev/ic/hpetreg.h>
61 #include <dev/ic/hpetvar.h>
62 
63 #include "hpet.h"
64 #include "pcibvar.h"
65 
66 struct lpcib_softc {
67 	/* we call pcibattach() which assumes this starts like this: */
68 	struct pcib_softc	sc_pcib;
69 
70 	struct pci_attach_args	sc_pa;
71 	int			sc_has_rcba;
72 	int			sc_has_ich5_hpet;
73 
74 	/* RCBA */
75 	bus_space_tag_t		sc_rcbat;
76 	bus_space_handle_t	sc_rcbah;
77 	pcireg_t		sc_rcba_reg;
78 
79 	/* Watchdog variables. */
80 	struct sysmon_wdog	sc_smw;
81 	bus_space_tag_t		sc_iot;
82 	bus_space_handle_t	sc_ioh;
83 
84 #if NHPET > 0
85 	/* HPET variables. */
86 	uint32_t		sc_hpet_reg;
87 #endif
88 
89 	/* Power management */
90 	pcireg_t		sc_pirq[2];
91 	pcireg_t		sc_pmcon;
92 	pcireg_t		sc_fwhsel2;
93 };
94 
95 static int lpcibmatch(device_t, cfdata_t, void *);
96 static void lpcibattach(device_t, device_t, void *);
97 static bool lpcib_suspend(device_t PMF_FN_PROTO);
98 static bool lpcib_resume(device_t PMF_FN_PROTO);
99 
100 static void pmtimer_configure(device_t);
101 
102 static void tcotimer_configure(device_t);
103 static int tcotimer_setmode(struct sysmon_wdog *);
104 static int tcotimer_tickle(struct sysmon_wdog *);
105 static void tcotimer_stop(struct lpcib_softc *);
106 static void tcotimer_start(struct lpcib_softc *);
107 static void tcotimer_status_reset(struct lpcib_softc *);
108 static int  tcotimer_disable_noreboot(device_t);
109 
110 static void speedstep_configure(device_t);
111 static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
112 
113 #if NHPET > 0
114 static void lpcib_hpet_configure(device_t);
115 #endif
116 
117 struct lpcib_softc *speedstep_cookie;	/* XXX */
118 
119 CFATTACH_DECL_NEW(ichlpcib, sizeof(struct lpcib_softc),
120     lpcibmatch, lpcibattach, NULL, NULL);
121 
122 static struct lpcib_device {
123 	pcireg_t vendor, product;
124 	int has_rcba;
125 	int has_ich5_hpet;
126 } lpcib_devices[] = {
127 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801AA_LPC, 0, 0 },
128 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BA_LPC, 0, 0 },
129 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BAM_LPC, 0, 0 },
130 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CA_LPC, 0, 0 },
131 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CAM_LPC, 0, 0 },
132 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DB_LPC, 0, 0 },
133 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DB_ISA, 0, 0 },
134 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801EB_LPC, 0, 1 },
135 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801FB_LPC, 1, 0 },
136 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801FBM_LPC, 1, 0 },
137 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801G_LPC, 1, 0 },
138 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801GBM_LPC, 1, 0 },
139 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801GHM_LPC, 1, 0 },
140 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801H_LPC, 1, 0 },
141 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HEM_LPC, 1, 0 },
142 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HH_LPC, 1, 0 },
143 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HO_LPC, 1, 0 },
144 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HBM_LPC, 1, 0 },
145 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IH_LPC, 1, 0 },
146 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IO_LPC, 1, 0 },
147 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IR_LPC, 1, 0 },
148 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IB_LPC, 1, 0 },
149 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_63XXESB_LPC, 1, 0 },
150 
151 	{ 0, 0, 0, 0 },
152 };
153 
154 /*
155  * Autoconf callbacks.
156  */
157 static int
158 lpcibmatch(device_t parent, cfdata_t match, void *aux)
159 {
160 	struct pci_attach_args *pa = aux;
161 	struct lpcib_device *lpcib_dev;
162 
163 	/* We are ISA bridge, of course */
164 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
165 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
166 		return 0;
167 
168 	for (lpcib_dev = lpcib_devices; lpcib_dev->vendor; ++lpcib_dev) {
169 		if (PCI_VENDOR(pa->pa_id) == lpcib_dev->vendor &&
170 		    PCI_PRODUCT(pa->pa_id) == lpcib_dev->product)
171 			return 10;
172 	}
173 
174 	return 0;
175 }
176 
177 static void
178 lpcibattach(device_t parent, device_t self, void *aux)
179 {
180 	struct pci_attach_args *pa = aux;
181 	struct lpcib_softc *sc = device_private(self);
182 	struct lpcib_device *lpcib_dev;
183 
184 	sc->sc_pa = *pa;
185 
186 	for (lpcib_dev = lpcib_devices; lpcib_dev->vendor; ++lpcib_dev) {
187 		if (PCI_VENDOR(pa->pa_id) != lpcib_dev->vendor ||
188 		    PCI_PRODUCT(pa->pa_id) != lpcib_dev->product)
189 			continue;
190 		sc->sc_has_rcba = lpcib_dev->has_rcba;
191 		sc->sc_has_ich5_hpet = lpcib_dev->has_ich5_hpet;
192 		break;
193 	}
194 
195 	pcibattach(parent, self, aux);
196 
197 	/*
198 	 * Part of our I/O registers are used as ACPI PM regs.
199 	 * Since our ACPI subsystem accesses the I/O space directly so far,
200 	 * we do not have to bother bus_space I/O map confliction.
201 	 */
202 	if (pci_mapreg_map(pa, LPCIB_PCI_PMBASE, PCI_MAPREG_TYPE_IO, 0,
203 			   &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
204 		aprint_error_dev(self, "can't map power management i/o space");
205 		return;
206 	}
207 
208 	/* For ICH6 and later, always enable RCBA */
209 	if (sc->sc_has_rcba) {
210 		pcireg_t rcba;
211 
212 		sc->sc_rcbat = sc->sc_pa.pa_memt;
213 
214 		rcba = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
215 		     LPCIB_RCBA);
216 		if ((rcba & LPCIB_RCBA_EN) == 0) {
217 			aprint_error_dev(self, "RCBA is not enabled");
218 			return;
219 		}
220 		rcba &= ~LPCIB_RCBA_EN;
221 
222 		if (bus_space_map(sc->sc_rcbat, rcba, LPCIB_RCBA_SIZE, 0,
223 				  &sc->sc_rcbah)) {
224 			aprint_error_dev(self, "RCBA could not be mapped");
225 			return;
226 		}
227 	}
228 
229 	/* Set up the power management timer. */
230 	pmtimer_configure(self);
231 
232 	/* Set up the TCO (watchdog). */
233 	tcotimer_configure(self);
234 
235 	/* Set up SpeedStep. */
236 	speedstep_configure(self);
237 
238 #if NHPET > 0
239 	/* Set up HPET. */
240 	lpcib_hpet_configure(self);
241 #endif
242 
243 	/* Install power handler */
244 	if (!pmf_device_register(self, lpcib_suspend, lpcib_resume))
245 		aprint_error_dev(self, "couldn't establish power handler\n");
246 }
247 
248 static bool
249 lpcib_suspend(device_t dv PMF_FN_ARGS)
250 {
251 	struct lpcib_softc *sc = device_private(dv);
252 	pci_chipset_tag_t pc = sc->sc_pcib.sc_pc;
253 	pcitag_t tag = sc->sc_pcib.sc_tag;
254 
255 	/* capture PIRQ routing control registers */
256 	sc->sc_pirq[0] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQA_ROUT);
257 	sc->sc_pirq[1] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQE_ROUT);
258 
259 	sc->sc_pmcon = pci_conf_read(pc, tag, LPCIB_PCI_GEN_PMCON_1);
260 	sc->sc_fwhsel2 = pci_conf_read(pc, tag, LPCIB_PCI_GEN_STA);
261 
262 	if (sc->sc_has_rcba) {
263 		sc->sc_rcba_reg = pci_conf_read(pc, tag, LPCIB_RCBA);
264 #if NHPET > 0
265 		sc->sc_hpet_reg = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
266 		    LPCIB_RCBA_HPTC);
267 #endif
268 	} else if (sc->sc_has_ich5_hpet) {
269 #if NHPET > 0
270 		sc->sc_hpet_reg = pci_conf_read(pc, tag, LPCIB_PCI_GEN_CNTL);
271 #endif
272 	}
273 
274 	return true;
275 }
276 
277 static bool
278 lpcib_resume(device_t dv PMF_FN_ARGS)
279 {
280 	struct lpcib_softc *sc = device_private(dv);
281 	pci_chipset_tag_t pc = sc->sc_pcib.sc_pc;
282 	pcitag_t tag = sc->sc_pcib.sc_tag;
283 
284 	/* restore PIRQ routing control registers */
285 	pci_conf_write(pc, tag, LPCIB_PCI_PIRQA_ROUT, sc->sc_pirq[0]);
286 	pci_conf_write(pc, tag, LPCIB_PCI_PIRQE_ROUT, sc->sc_pirq[1]);
287 
288 	pci_conf_write(pc, tag, LPCIB_PCI_GEN_PMCON_1, sc->sc_pmcon);
289 	pci_conf_write(pc, tag, LPCIB_PCI_GEN_STA, sc->sc_fwhsel2);
290 
291 	if (sc->sc_has_rcba) {
292 		pci_conf_write(pc, tag, LPCIB_RCBA, sc->sc_rcba_reg);
293 #if NHPET > 0
294 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
295 		    sc->sc_hpet_reg);
296 #endif
297 	} else if (sc->sc_has_ich5_hpet) {
298 #if NHPET > 0
299 		pci_conf_write(pc, tag, LPCIB_PCI_GEN_CNTL, sc->sc_hpet_reg);
300 #endif
301 	}
302 
303 	return true;
304 }
305 
306 /*
307  * Initialize the power management timer.
308  */
309 static void
310 pmtimer_configure(device_t self)
311 {
312 	struct lpcib_softc *sc = device_private(self);
313 	pcireg_t control;
314 
315 	/*
316 	 * Check if power management I/O space is enabled and enable the ACPI_EN
317 	 * bit if it's disabled.
318 	 */
319 	control = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
320 	    LPCIB_PCI_ACPI_CNTL);
321 	if ((control & LPCIB_PCI_ACPI_CNTL_EN) == 0) {
322 		control |= LPCIB_PCI_ACPI_CNTL_EN;
323 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
324 		    LPCIB_PCI_ACPI_CNTL, control);
325 	}
326 
327 	/* Attach our PM timer with the generic acpipmtimer function */
328 	acpipmtimer_attach(self, sc->sc_iot, sc->sc_ioh,
329 	    LPCIB_PM1_TMR, 0);
330 }
331 
332 /*
333  * Initialize the watchdog timer.
334  */
335 static void
336 tcotimer_configure(device_t self)
337 {
338 	struct lpcib_softc *sc = device_private(self);
339 	uint32_t ioreg;
340 	unsigned int period;
341 
342 	/* Explicitly stop the TCO timer. */
343 	tcotimer_stop(sc);
344 
345 	/*
346 	 * Enable TCO timeout SMI only if the hardware reset does not
347 	 * work. We don't know what the SMBIOS does.
348 	 */
349 	ioreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN);
350 	ioreg &= ~LPCIB_SMI_EN_TCO_EN;
351 
352 	/*
353 	 * Clear the No Reboot (NR) bit. If this fails, enabling the TCO_EN bit
354 	 * in the SMI_EN register is the last chance.
355 	 */
356 	if (tcotimer_disable_noreboot(self)) {
357 		ioreg |= LPCIB_SMI_EN_TCO_EN;
358 	}
359 	if ((ioreg & LPCIB_SMI_EN_GBL_SMI_EN) != 0) {
360 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN, ioreg);
361 	}
362 
363 	/* Reset the watchdog status registers. */
364 	tcotimer_status_reset(sc);
365 
366 	/*
367 	 * Register the driver with the sysmon watchdog framework.
368 	 */
369 	sc->sc_smw.smw_name = device_xname(self);
370 	sc->sc_smw.smw_cookie = sc;
371 	sc->sc_smw.smw_setmode = tcotimer_setmode;
372 	sc->sc_smw.smw_tickle = tcotimer_tickle;
373 	if (sc->sc_has_rcba)
374 		period = LPCIB_TCOTIMER2_MAX_TICK;
375 	else
376 		period = LPCIB_TCOTIMER_MAX_TICK;
377 	sc->sc_smw.smw_period = lpcib_tcotimer_tick_to_second(period);
378 
379 	if (sysmon_wdog_register(&sc->sc_smw)) {
380 		aprint_error_dev(self, "unable to register TCO timer"
381 		       "as a sysmon watchdog device.\n");
382 		return;
383 	}
384 
385 	aprint_verbose_dev(self, "TCO (watchdog) timer configured.\n");
386 }
387 
388 /*
389  * Sysmon watchdog callbacks.
390  */
391 static int
392 tcotimer_setmode(struct sysmon_wdog *smw)
393 {
394 	struct lpcib_softc *sc = smw->smw_cookie;
395 	unsigned int period;
396 	uint16_t ich6period = 0;
397 
398 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
399 		/* Stop the TCO timer. */
400 		tcotimer_stop(sc);
401 	} else {
402 		/*
403 		 * ICH6 or newer are limited to 2s min and 613s max.
404 		 * ICH5 or older are limited to 4s min and 39s max.
405 		 */
406 		if (sc->sc_has_rcba) {
407 			if (smw->smw_period < LPCIB_TCOTIMER2_MIN_TICK ||
408 			    smw->smw_period > LPCIB_TCOTIMER2_MAX_TICK)
409 				return EINVAL;
410 		} else {
411 			if (smw->smw_period < LPCIB_TCOTIMER_MIN_TICK ||
412 			    smw->smw_period > LPCIB_TCOTIMER_MAX_TICK)
413 				return EINVAL;
414 		}
415 		period = lpcib_tcotimer_second_to_tick(smw->smw_period);
416 
417 		/* Stop the TCO timer, */
418 		tcotimer_stop(sc);
419 
420 		/* set the timeout, */
421 		if (sc->sc_has_rcba) {
422 			/* ICH6 or newer */
423 			ich6period = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
424 						      LPCIB_TCO_TMR2);
425 			ich6period &= 0xfc00;
426 			bus_space_write_2(sc->sc_iot, sc->sc_ioh,
427 					  LPCIB_TCO_TMR2, ich6period | period);
428 		} else {
429 			/* ICH5 or older */
430 			period |= bus_space_read_1(sc->sc_iot, sc->sc_ioh,
431 						   LPCIB_TCO_TMR);
432 			period &= 0xc0;
433 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
434 					  LPCIB_TCO_TMR, period);
435 		}
436 
437 		/* and start/reload the timer. */
438 		tcotimer_start(sc);
439 		tcotimer_tickle(smw);
440 	}
441 
442 	return 0;
443 }
444 
445 static int
446 tcotimer_tickle(struct sysmon_wdog *smw)
447 {
448 	struct lpcib_softc *sc = smw->smw_cookie;
449 
450 	/* any value is allowed */
451 	if (sc->sc_has_rcba)
452 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
453 	else
454 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
455 
456 	return 0;
457 }
458 
459 static void
460 tcotimer_stop(struct lpcib_softc *sc)
461 {
462 	uint16_t ioreg;
463 
464 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
465 	ioreg |= LPCIB_TCO1_CNT_TCO_TMR_HLT;
466 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
467 }
468 
469 static void
470 tcotimer_start(struct lpcib_softc *sc)
471 {
472 	uint16_t ioreg;
473 
474 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
475 	ioreg &= ~LPCIB_TCO1_CNT_TCO_TMR_HLT;
476 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
477 }
478 
479 static void
480 tcotimer_status_reset(struct lpcib_softc *sc)
481 {
482 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_STS,
483 			  LPCIB_TCO1_STS_TIMEOUT);
484 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
485 			  LPCIB_TCO2_STS_BOOT_STS);
486 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
487 			  LPCIB_TCO2_STS_SECONDS_TO_STS);
488 }
489 
490 /*
491  * Clear the No Reboot (NR) bit, this enables reboots when the timer
492  * reaches the timeout for the second time.
493  */
494 static int
495 tcotimer_disable_noreboot(device_t self)
496 {
497 	struct lpcib_softc *sc = device_private(self);
498 
499 	if (sc->sc_has_rcba) {
500 		uint32_t status;
501 
502 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
503 		    LPCIB_GCS_OFFSET);
504 		status &= ~LPCIB_GCS_NO_REBOOT;
505 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah,
506 		    LPCIB_GCS_OFFSET, status);
507 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
508 		    LPCIB_GCS_OFFSET);
509 		if (status & LPCIB_GCS_NO_REBOOT)
510 			goto error;
511 	} else {
512 		pcireg_t pcireg;
513 
514 		pcireg = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
515 				       LPCIB_PCI_GEN_STA);
516 		if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT) {
517 			/* TCO timeout reset is disabled; try to enable it */
518 			pcireg &= ~LPCIB_PCI_GEN_STA_NO_REBOOT;
519 			pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
520 				       LPCIB_PCI_GEN_STA, pcireg);
521 			if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT)
522 				goto error;
523 		}
524 	}
525 
526 	return 0;
527 error:
528 	aprint_error_dev(self, "TCO timer reboot disabled by hardware; "
529 	    "hope SMBIOS properly handles it.\n");
530 	return EINVAL;
531 }
532 
533 
534 /*
535  * Intel ICH SpeedStep support.
536  */
537 #define SS_READ(sc, reg) \
538 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (reg))
539 #define SS_WRITE(sc, reg, val) \
540 	bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
541 
542 /*
543  * Linux driver says that SpeedStep on older chipsets cause
544  * lockups on Dell Inspiron 8000 and 8100.
545  */
546 static int
547 speedstep_bad_hb_check(struct pci_attach_args *pa)
548 {
549 
550 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82815_FULL_HUB &&
551 	    PCI_REVISION(pa->pa_class) < 5)
552 		return 1;
553 
554 	return 0;
555 }
556 
557 static void
558 speedstep_configure(device_t self)
559 {
560 	struct lpcib_softc *sc = device_private(self);
561 	const struct sysctlnode	*node, *ssnode;
562 	int rv;
563 
564 	/* Supported on ICH2-M, ICH3-M and ICH4-M.  */
565 	if (PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801DB_ISA ||
566 	    PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801CAM_LPC ||
567 	    (PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801BAM_LPC &&
568 	     pci_find_device(&sc->sc_pa, speedstep_bad_hb_check) == 0)) {
569 		uint8_t pmcon;
570 
571 		/* Enable SpeedStep if it isn't already enabled. */
572 		pmcon = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
573 				      LPCIB_PCI_GEN_PMCON_1);
574 		if ((pmcon & LPCIB_PCI_GEN_PMCON_1_SS_EN) == 0)
575 			pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
576 				       LPCIB_PCI_GEN_PMCON_1,
577 				       pmcon | LPCIB_PCI_GEN_PMCON_1_SS_EN);
578 
579 		/* Put in machdep.speedstep_state (0 for low, 1 for high). */
580 		if ((rv = sysctl_createv(NULL, 0, NULL, &node,
581 		    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
582 		    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
583 			goto err;
584 
585 		/* CTLFLAG_ANYWRITE? kernel option like EST? */
586 		if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
587 		    CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
588 		    speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
589 		    CTL_EOL)) != 0)
590 			goto err;
591 
592 		/* XXX save the sc for IO tag/handle */
593 		speedstep_cookie = sc;
594 		aprint_verbose_dev(self, "SpeedStep enabled\n");
595 	}
596 
597 	return;
598 
599 err:
600 	aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
601 }
602 
603 /*
604  * get/set the SpeedStep state: 0 == low power, 1 == high power.
605  */
606 static int
607 speedstep_sysctl_helper(SYSCTLFN_ARGS)
608 {
609 	struct sysctlnode	node;
610 	struct lpcib_softc 	*sc = speedstep_cookie;
611 	uint8_t			state, state2;
612 	int			ostate, nstate, s, error = 0;
613 
614 	/*
615 	 * We do the dance with spl's to avoid being at high ipl during
616 	 * sysctl_lookup() which can both copyin and copyout.
617 	 */
618 	s = splserial();
619 	state = SS_READ(sc, LPCIB_PM_SS_CNTL);
620 	splx(s);
621 	if ((state & LPCIB_PM_SS_STATE_LOW) == 0)
622 		ostate = 1;
623 	else
624 		ostate = 0;
625 	nstate = ostate;
626 
627 	node = *rnode;
628 	node.sysctl_data = &nstate;
629 
630 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
631 	if (error || newp == NULL)
632 		goto out;
633 
634 	/* Only two states are available */
635 	if (nstate != 0 && nstate != 1) {
636 		error = EINVAL;
637 		goto out;
638 	}
639 
640 	s = splserial();
641 	state2 = SS_READ(sc, LPCIB_PM_SS_CNTL);
642 	if ((state2 & LPCIB_PM_SS_STATE_LOW) == 0)
643 		ostate = 1;
644 	else
645 		ostate = 0;
646 
647 	if (ostate != nstate) {
648 		uint8_t cntl;
649 
650 		if (nstate == 0)
651 			state2 |= LPCIB_PM_SS_STATE_LOW;
652 		else
653 			state2 &= ~LPCIB_PM_SS_STATE_LOW;
654 
655 		/*
656 		 * Must disable bus master arbitration during the change.
657 		 */
658 		cntl = SS_READ(sc, LPCIB_PM_CTRL);
659 		SS_WRITE(sc, LPCIB_PM_CTRL, cntl | LPCIB_PM_SS_CNTL_ARB_DIS);
660 		SS_WRITE(sc, LPCIB_PM_SS_CNTL, state2);
661 		SS_WRITE(sc, LPCIB_PM_CTRL, cntl);
662 	}
663 	splx(s);
664 out:
665 	return error;
666 }
667 
668 #if NHPET > 0
669 struct lpcib_hpet_attach_arg {
670 	bus_space_tag_t hpet_mem_t;
671 	uint32_t hpet_reg;
672 };
673 
674 static int
675 lpcib_hpet_match(device_t parent, cfdata_t match, void *aux)
676 {
677 	struct lpcib_hpet_attach_arg *arg = aux;
678 	bus_space_tag_t tag;
679 	bus_space_handle_t handle;
680 
681 	tag = arg->hpet_mem_t;
682 
683 	if (bus_space_map(tag, arg->hpet_reg, HPET_WINDOW_SIZE, 0, &handle)) {
684 		aprint_verbose_dev(parent, "HPET window not mapped, skipping\n");
685 		return 0;
686 	}
687 	bus_space_unmap(tag, handle, HPET_WINDOW_SIZE);
688 
689 	return 1;
690 }
691 
692 static void
693 lpcib_hpet_attach(device_t parent, device_t self, void *aux)
694 {
695 	struct hpet_softc *sc = device_private(self);
696 	struct lpcib_hpet_attach_arg *arg = aux;
697 
698 	aprint_naive("\n");
699 	aprint_normal("\n");
700 
701 	sc->sc_memt = arg->hpet_mem_t;
702 
703 	if (bus_space_map(sc->sc_memt, arg->hpet_reg, HPET_WINDOW_SIZE, 0,
704 			  &sc->sc_memh)) {
705 		aprint_error_dev(self,
706 		    "HPET memory window could not be mapped");
707 		return;
708 	}
709 
710 	hpet_attach_subr(self);
711 }
712 
713 CFATTACH_DECL_NEW(ichlpcib_hpet, sizeof(struct hpet_softc), lpcib_hpet_match,
714     lpcib_hpet_attach, NULL, NULL);
715 
716 static void
717 lpcib_hpet_configure(device_t self)
718 {
719 	struct lpcib_softc *sc = device_private(self);
720 	struct lpcib_hpet_attach_arg arg;
721 	uint32_t hpet_reg, val;
722 
723 	if (sc->sc_has_ich5_hpet) {
724 		val = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
725 		    LPCIB_PCI_GEN_CNTL);
726 		switch (val & LPCIB_ICH5_HPTC_WIN_MASK) {
727 		case LPCIB_ICH5_HPTC_0000:
728 			hpet_reg = LPCIB_ICH5_HPTC_0000_BASE;
729 			break;
730 		case LPCIB_ICH5_HPTC_1000:
731 			hpet_reg = LPCIB_ICH5_HPTC_1000_BASE;
732 			break;
733 		case LPCIB_ICH5_HPTC_2000:
734 			hpet_reg = LPCIB_ICH5_HPTC_2000_BASE;
735 			break;
736 		case LPCIB_ICH5_HPTC_3000:
737 			hpet_reg = LPCIB_ICH5_HPTC_3000_BASE;
738 			break;
739 		default:
740 			return;
741 		}
742 		val |= sc->sc_hpet_reg | LPCIB_ICH5_HPTC_EN;
743 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
744 		    LPCIB_PCI_GEN_CNTL, val);
745 	} else if (sc->sc_has_rcba) {
746 		val = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
747 		    LPCIB_RCBA_HPTC);
748 		switch (val & LPCIB_RCBA_HPTC_WIN_MASK) {
749 		case LPCIB_RCBA_HPTC_0000:
750 			hpet_reg = LPCIB_RCBA_HPTC_0000_BASE;
751 			break;
752 		case LPCIB_RCBA_HPTC_1000:
753 			hpet_reg = LPCIB_RCBA_HPTC_1000_BASE;
754 			break;
755 		case LPCIB_RCBA_HPTC_2000:
756 			hpet_reg = LPCIB_RCBA_HPTC_2000_BASE;
757 			break;
758 		case LPCIB_RCBA_HPTC_3000:
759 			hpet_reg = LPCIB_RCBA_HPTC_3000_BASE;
760 			break;
761 		default:
762 			return;
763 		}
764 		val |= LPCIB_RCBA_HPTC_EN;
765 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
766 		    val);
767 	} else {
768 		/* No HPET here */
769 		return;
770 	}
771 
772 	arg.hpet_mem_t = sc->sc_pa.pa_memt;
773 	arg.hpet_reg = hpet_reg;
774 
775 	config_found_ia(self, "hpetichbus", &arg, NULL);
776 }
777 #endif
778