xref: /netbsd-src/sys/arch/x86/pci/ichlpcib.c (revision b78992537496bc71ee3d761f9fe0be0fc0a9a001)
1 /*	$NetBSD: ichlpcib.c,v 1.13 2008/08/14 01:20:30 yamt 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.13 2008/08/14 01:20:30 yamt 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 	{ 0, 0, 0, 0 },
150 };
151 
152 /*
153  * Autoconf callbacks.
154  */
155 static int
156 lpcibmatch(device_t parent, cfdata_t match, void *aux)
157 {
158 	struct pci_attach_args *pa = aux;
159 	struct lpcib_device *lpcib_dev;
160 
161 	/* We are ISA bridge, of course */
162 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
163 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
164 		return 0;
165 
166 	for (lpcib_dev = lpcib_devices; lpcib_dev->vendor; ++lpcib_dev) {
167 		if (PCI_VENDOR(pa->pa_id) == lpcib_dev->vendor &&
168 		    PCI_PRODUCT(pa->pa_id) == lpcib_dev->product)
169 			return 10;
170 	}
171 
172 	return 0;
173 }
174 
175 static void
176 lpcibattach(device_t parent, device_t self, void *aux)
177 {
178 	struct pci_attach_args *pa = aux;
179 	struct lpcib_softc *sc = device_private(self);
180 	struct lpcib_device *lpcib_dev;
181 
182 	sc->sc_pa = *pa;
183 
184 	for (lpcib_dev = lpcib_devices; lpcib_dev->vendor; ++lpcib_dev) {
185 		if (PCI_VENDOR(pa->pa_id) != lpcib_dev->vendor ||
186 		    PCI_PRODUCT(pa->pa_id) != lpcib_dev->product)
187 			continue;
188 		sc->sc_has_rcba = lpcib_dev->has_rcba;
189 		sc->sc_has_ich5_hpet = lpcib_dev->has_ich5_hpet;
190 		break;
191 	}
192 
193 	pcibattach(parent, self, aux);
194 
195 	/*
196 	 * Part of our I/O registers are used as ACPI PM regs.
197 	 * Since our ACPI subsystem accesses the I/O space directly so far,
198 	 * we do not have to bother bus_space I/O map confliction.
199 	 */
200 	if (pci_mapreg_map(pa, LPCIB_PCI_PMBASE, PCI_MAPREG_TYPE_IO, 0,
201 			   &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
202 		aprint_error_dev(self, "can't map power management i/o space");
203 		return;
204 	}
205 
206 	/* For ICH6 and later, always enable RCBA */
207 	if (sc->sc_has_rcba) {
208 		pcireg_t rcba;
209 
210 		sc->sc_rcbat = sc->sc_pa.pa_memt;
211 
212 		rcba = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
213 		     LPCIB_RCBA);
214 		if ((rcba & LPCIB_RCBA_EN) == 0) {
215 			aprint_error_dev(self, "RCBA is not enabled");
216 			return;
217 		}
218 		rcba &= ~LPCIB_RCBA_EN;
219 
220 		if (bus_space_map(sc->sc_rcbat, rcba, LPCIB_RCBA_SIZE, 0,
221 				  &sc->sc_rcbah)) {
222 			aprint_error_dev(self, "RCBA could not be mapped");
223 			return;
224 		}
225 	}
226 
227 	/* Set up the power management timer. */
228 	pmtimer_configure(self);
229 
230 	/* Set up the TCO (watchdog). */
231 	tcotimer_configure(self);
232 
233 	/* Set up SpeedStep. */
234 	speedstep_configure(self);
235 
236 #if NHPET > 0
237 	/* Set up HPET. */
238 	lpcib_hpet_configure(self);
239 #endif
240 
241 	/* Install power handler */
242 	if (!pmf_device_register(self, lpcib_suspend, lpcib_resume))
243 		aprint_error_dev(self, "couldn't establish power handler\n");
244 }
245 
246 static bool
247 lpcib_suspend(device_t dv PMF_FN_ARGS)
248 {
249 	struct lpcib_softc *sc = device_private(dv);
250 	pci_chipset_tag_t pc = sc->sc_pcib.sc_pc;
251 	pcitag_t tag = sc->sc_pcib.sc_tag;
252 
253 	/* capture PIRQ routing control registers */
254 	sc->sc_pirq[0] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQA_ROUT);
255 	sc->sc_pirq[1] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQE_ROUT);
256 
257 	sc->sc_pmcon = pci_conf_read(pc, tag, LPCIB_PCI_GEN_PMCON_1);
258 	sc->sc_fwhsel2 = pci_conf_read(pc, tag, LPCIB_PCI_GEN_STA);
259 
260 	if (sc->sc_has_rcba) {
261 		sc->sc_rcba_reg = pci_conf_read(pc, tag, LPCIB_RCBA);
262 #if NHPET > 0
263 		sc->sc_hpet_reg = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
264 		    LPCIB_RCBA_HPTC);
265 #endif
266 	} else if (sc->sc_has_ich5_hpet) {
267 #if NHPET > 0
268 		sc->sc_hpet_reg = pci_conf_read(pc, tag, LPCIB_PCI_GEN_CNTL);
269 #endif
270 	}
271 
272 	return true;
273 }
274 
275 static bool
276 lpcib_resume(device_t dv PMF_FN_ARGS)
277 {
278 	struct lpcib_softc *sc = device_private(dv);
279 	pci_chipset_tag_t pc = sc->sc_pcib.sc_pc;
280 	pcitag_t tag = sc->sc_pcib.sc_tag;
281 
282 	/* restore PIRQ routing control registers */
283 	pci_conf_write(pc, tag, LPCIB_PCI_PIRQA_ROUT, sc->sc_pirq[0]);
284 	pci_conf_write(pc, tag, LPCIB_PCI_PIRQE_ROUT, sc->sc_pirq[1]);
285 
286 	pci_conf_write(pc, tag, LPCIB_PCI_GEN_PMCON_1, sc->sc_pmcon);
287 	pci_conf_write(pc, tag, LPCIB_PCI_GEN_STA, sc->sc_fwhsel2);
288 
289 	if (sc->sc_has_rcba) {
290 		pci_conf_write(pc, tag, LPCIB_RCBA, sc->sc_rcba_reg);
291 #if NHPET > 0
292 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
293 		    sc->sc_hpet_reg);
294 #endif
295 	} else if (sc->sc_has_ich5_hpet) {
296 #if NHPET > 0
297 		pci_conf_write(pc, tag, LPCIB_PCI_GEN_CNTL, sc->sc_hpet_reg);
298 #endif
299 	}
300 
301 	return true;
302 }
303 
304 /*
305  * Initialize the power management timer.
306  */
307 static void
308 pmtimer_configure(device_t self)
309 {
310 	struct lpcib_softc *sc = device_private(self);
311 	pcireg_t control;
312 
313 	/*
314 	 * Check if power management I/O space is enabled and enable the ACPI_EN
315 	 * bit if it's disabled.
316 	 */
317 	control = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
318 	    LPCIB_PCI_ACPI_CNTL);
319 	if ((control & LPCIB_PCI_ACPI_CNTL_EN) == 0) {
320 		control |= LPCIB_PCI_ACPI_CNTL_EN;
321 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
322 		    LPCIB_PCI_ACPI_CNTL, control);
323 	}
324 
325 	/* Attach our PM timer with the generic acpipmtimer function */
326 	acpipmtimer_attach(self, sc->sc_iot, sc->sc_ioh,
327 	    LPCIB_PM1_TMR, 0);
328 }
329 
330 /*
331  * Initialize the watchdog timer.
332  */
333 static void
334 tcotimer_configure(device_t self)
335 {
336 	struct lpcib_softc *sc = device_private(self);
337 	uint32_t ioreg;
338 	unsigned int period;
339 
340 	/* Explicitly stop the TCO timer. */
341 	tcotimer_stop(sc);
342 
343 	/*
344 	 * Enable TCO timeout SMI only if the hardware reset does not
345 	 * work. We don't know what the SMBIOS does.
346 	 */
347 	ioreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN);
348 	ioreg &= ~LPCIB_SMI_EN_TCO_EN;
349 
350 	/*
351 	 * Clear the No Reboot (NR) bit. If this fails, enabling the TCO_EN bit
352 	 * in the SMI_EN register is the last chance.
353 	 */
354 	if (tcotimer_disable_noreboot(self)) {
355 		ioreg |= LPCIB_SMI_EN_TCO_EN;
356 	}
357 	if ((ioreg & LPCIB_SMI_EN_GBL_SMI_EN) != 0) {
358 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN, ioreg);
359 	}
360 
361 	/* Reset the watchdog status registers. */
362 	tcotimer_status_reset(sc);
363 
364 	/*
365 	 * Register the driver with the sysmon watchdog framework.
366 	 */
367 	sc->sc_smw.smw_name = device_xname(self);
368 	sc->sc_smw.smw_cookie = sc;
369 	sc->sc_smw.smw_setmode = tcotimer_setmode;
370 	sc->sc_smw.smw_tickle = tcotimer_tickle;
371 	if (sc->sc_has_rcba)
372 		period = LPCIB_TCOTIMER2_MAX_TICK;
373 	else
374 		period = LPCIB_TCOTIMER_MAX_TICK;
375 	sc->sc_smw.smw_period = lpcib_tcotimer_tick_to_second(period);
376 
377 	if (sysmon_wdog_register(&sc->sc_smw)) {
378 		aprint_error_dev(self, "unable to register TCO timer"
379 		       "as a sysmon watchdog device.\n");
380 		return;
381 	}
382 
383 	aprint_verbose_dev(self, "TCO (watchdog) timer configured.\n");
384 }
385 
386 /*
387  * Sysmon watchdog callbacks.
388  */
389 static int
390 tcotimer_setmode(struct sysmon_wdog *smw)
391 {
392 	struct lpcib_softc *sc = smw->smw_cookie;
393 	unsigned int period;
394 	uint16_t ich6period = 0;
395 
396 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
397 		/* Stop the TCO timer. */
398 		tcotimer_stop(sc);
399 	} else {
400 		/*
401 		 * ICH6 or newer are limited to 2s min and 613s max.
402 		 * ICH5 or older are limited to 4s min and 39s max.
403 		 */
404 		if (sc->sc_has_rcba) {
405 			if (smw->smw_period < LPCIB_TCOTIMER2_MIN_TICK ||
406 			    smw->smw_period > LPCIB_TCOTIMER2_MAX_TICK)
407 				return EINVAL;
408 		} else {
409 			if (smw->smw_period < LPCIB_TCOTIMER_MIN_TICK ||
410 			    smw->smw_period > LPCIB_TCOTIMER_MAX_TICK)
411 				return EINVAL;
412 		}
413 		period = lpcib_tcotimer_second_to_tick(smw->smw_period);
414 
415 		/* Stop the TCO timer, */
416 		tcotimer_stop(sc);
417 
418 		/* set the timeout, */
419 		if (sc->sc_has_rcba) {
420 			/* ICH6 or newer */
421 			ich6period = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
422 						      LPCIB_TCO_TMR2);
423 			ich6period &= 0xfc00;
424 			bus_space_write_2(sc->sc_iot, sc->sc_ioh,
425 					  LPCIB_TCO_TMR2, ich6period | period);
426 		} else {
427 			/* ICH5 or older */
428 			period |= bus_space_read_1(sc->sc_iot, sc->sc_ioh,
429 						   LPCIB_TCO_TMR);
430 			period &= 0xc0;
431 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
432 					  LPCIB_TCO_TMR, period);
433 		}
434 
435 		/* and start/reload the timer. */
436 		tcotimer_start(sc);
437 		tcotimer_tickle(smw);
438 	}
439 
440 	return 0;
441 }
442 
443 static int
444 tcotimer_tickle(struct sysmon_wdog *smw)
445 {
446 	struct lpcib_softc *sc = smw->smw_cookie;
447 
448 	/* any value is allowed */
449 	if (sc->sc_has_rcba)
450 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
451 	else
452 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
453 
454 	return 0;
455 }
456 
457 static void
458 tcotimer_stop(struct lpcib_softc *sc)
459 {
460 	uint16_t ioreg;
461 
462 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
463 	ioreg |= LPCIB_TCO1_CNT_TCO_TMR_HLT;
464 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
465 }
466 
467 static void
468 tcotimer_start(struct lpcib_softc *sc)
469 {
470 	uint16_t ioreg;
471 
472 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
473 	ioreg &= ~LPCIB_TCO1_CNT_TCO_TMR_HLT;
474 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
475 }
476 
477 static void
478 tcotimer_status_reset(struct lpcib_softc *sc)
479 {
480 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_STS,
481 			  LPCIB_TCO1_STS_TIMEOUT);
482 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
483 			  LPCIB_TCO2_STS_BOOT_STS);
484 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
485 			  LPCIB_TCO2_STS_SECONDS_TO_STS);
486 }
487 
488 /*
489  * Clear the No Reboot (NR) bit, this enables reboots when the timer
490  * reaches the timeout for the second time.
491  */
492 static int
493 tcotimer_disable_noreboot(device_t self)
494 {
495 	struct lpcib_softc *sc = device_private(self);
496 
497 	if (sc->sc_has_rcba) {
498 		uint32_t status;
499 
500 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
501 		    LPCIB_GCS_OFFSET);
502 		status &= ~LPCIB_GCS_NO_REBOOT;
503 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah,
504 		    LPCIB_GCS_OFFSET, status);
505 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
506 		    LPCIB_GCS_OFFSET);
507 		if (status & LPCIB_GCS_NO_REBOOT)
508 			goto error;
509 	} else {
510 		pcireg_t pcireg;
511 
512 		pcireg = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
513 				       LPCIB_PCI_GEN_STA);
514 		if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT) {
515 			/* TCO timeout reset is disabled; try to enable it */
516 			pcireg &= ~LPCIB_PCI_GEN_STA_NO_REBOOT;
517 			pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
518 				       LPCIB_PCI_GEN_STA, pcireg);
519 			if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT)
520 				goto error;
521 		}
522 	}
523 
524 	return 0;
525 error:
526 	aprint_error_dev(self, "TCO timer reboot disabled by hardware; "
527 	    "hope SMBIOS properly handles it.\n");
528 	return EINVAL;
529 }
530 
531 
532 /*
533  * Intel ICH SpeedStep support.
534  */
535 #define SS_READ(sc, reg) \
536 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (reg))
537 #define SS_WRITE(sc, reg, val) \
538 	bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
539 
540 /*
541  * Linux driver says that SpeedStep on older chipsets cause
542  * lockups on Dell Inspiron 8000 and 8100.
543  */
544 static int
545 speedstep_bad_hb_check(struct pci_attach_args *pa)
546 {
547 
548 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82815_FULL_HUB &&
549 	    PCI_REVISION(pa->pa_class) < 5)
550 		return 1;
551 
552 	return 0;
553 }
554 
555 static void
556 speedstep_configure(device_t self)
557 {
558 	struct lpcib_softc *sc = device_private(self);
559 	const struct sysctlnode	*node, *ssnode;
560 	int rv;
561 
562 	/* Supported on ICH2-M, ICH3-M and ICH4-M.  */
563 	if (PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801DB_ISA ||
564 	    PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801CAM_LPC ||
565 	    (PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801BAM_LPC &&
566 	     pci_find_device(&sc->sc_pa, speedstep_bad_hb_check) == 0)) {
567 		uint8_t pmcon;
568 
569 		/* Enable SpeedStep if it isn't already enabled. */
570 		pmcon = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
571 				      LPCIB_PCI_GEN_PMCON_1);
572 		if ((pmcon & LPCIB_PCI_GEN_PMCON_1_SS_EN) == 0)
573 			pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
574 				       LPCIB_PCI_GEN_PMCON_1,
575 				       pmcon | LPCIB_PCI_GEN_PMCON_1_SS_EN);
576 
577 		/* Put in machdep.speedstep_state (0 for low, 1 for high). */
578 		if ((rv = sysctl_createv(NULL, 0, NULL, &node,
579 		    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
580 		    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
581 			goto err;
582 
583 		/* CTLFLAG_ANYWRITE? kernel option like EST? */
584 		if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
585 		    CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
586 		    speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
587 		    CTL_EOL)) != 0)
588 			goto err;
589 
590 		/* XXX save the sc for IO tag/handle */
591 		speedstep_cookie = sc;
592 		aprint_verbose_dev(self, "SpeedStep enabled\n");
593 	}
594 
595 	return;
596 
597 err:
598 	aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
599 }
600 
601 /*
602  * get/set the SpeedStep state: 0 == low power, 1 == high power.
603  */
604 static int
605 speedstep_sysctl_helper(SYSCTLFN_ARGS)
606 {
607 	struct sysctlnode	node;
608 	struct lpcib_softc 	*sc = speedstep_cookie;
609 	uint8_t			state, state2;
610 	int			ostate, nstate, s, error = 0;
611 
612 	/*
613 	 * We do the dance with spl's to avoid being at high ipl during
614 	 * sysctl_lookup() which can both copyin and copyout.
615 	 */
616 	s = splserial();
617 	state = SS_READ(sc, LPCIB_PM_SS_CNTL);
618 	splx(s);
619 	if ((state & LPCIB_PM_SS_STATE_LOW) == 0)
620 		ostate = 1;
621 	else
622 		ostate = 0;
623 	nstate = ostate;
624 
625 	node = *rnode;
626 	node.sysctl_data = &nstate;
627 
628 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
629 	if (error || newp == NULL)
630 		goto out;
631 
632 	/* Only two states are available */
633 	if (nstate != 0 && nstate != 1) {
634 		error = EINVAL;
635 		goto out;
636 	}
637 
638 	s = splserial();
639 	state2 = SS_READ(sc, LPCIB_PM_SS_CNTL);
640 	if ((state2 & LPCIB_PM_SS_STATE_LOW) == 0)
641 		ostate = 1;
642 	else
643 		ostate = 0;
644 
645 	if (ostate != nstate) {
646 		uint8_t cntl;
647 
648 		if (nstate == 0)
649 			state2 |= LPCIB_PM_SS_STATE_LOW;
650 		else
651 			state2 &= ~LPCIB_PM_SS_STATE_LOW;
652 
653 		/*
654 		 * Must disable bus master arbitration during the change.
655 		 */
656 		cntl = SS_READ(sc, LPCIB_PM_CTRL);
657 		SS_WRITE(sc, LPCIB_PM_CTRL, cntl | LPCIB_PM_SS_CNTL_ARB_DIS);
658 		SS_WRITE(sc, LPCIB_PM_SS_CNTL, state2);
659 		SS_WRITE(sc, LPCIB_PM_CTRL, cntl);
660 	}
661 	splx(s);
662 out:
663 	return error;
664 }
665 
666 #if NHPET > 0
667 struct lpcib_hpet_attach_arg {
668 	bus_space_tag_t hpet_mem_t;
669 	uint32_t hpet_reg;
670 };
671 
672 static int
673 lpcib_hpet_match(device_t parent, cfdata_t match, void *aux)
674 {
675 	struct lpcib_hpet_attach_arg *arg = aux;
676 	bus_space_tag_t tag;
677 	bus_space_handle_t handle;
678 
679 	tag = arg->hpet_mem_t;
680 
681 	if (bus_space_map(tag, arg->hpet_reg, HPET_WINDOW_SIZE, 0, &handle)) {
682 		aprint_verbose_dev(parent, "HPET window not mapped, skipping\n");
683 		return 0;
684 	}
685 	bus_space_unmap(tag, handle, HPET_WINDOW_SIZE);
686 
687 	return 1;
688 }
689 
690 static void
691 lpcib_hpet_attach(device_t parent, device_t self, void *aux)
692 {
693 	struct hpet_softc *sc = device_private(self);
694 	struct lpcib_hpet_attach_arg *arg = aux;
695 
696 	aprint_naive("\n");
697 	aprint_normal("\n");
698 
699 	sc->sc_memt = arg->hpet_mem_t;
700 
701 	if (bus_space_map(sc->sc_memt, arg->hpet_reg, HPET_WINDOW_SIZE, 0,
702 			  &sc->sc_memh)) {
703 		aprint_error_dev(self,
704 		    "HPET memory window could not be mapped");
705 		return;
706 	}
707 
708 	hpet_attach_subr(self);
709 }
710 
711 CFATTACH_DECL_NEW(ichlpcib_hpet, sizeof(struct hpet_softc), lpcib_hpet_match,
712     lpcib_hpet_attach, NULL, NULL);
713 
714 static void
715 lpcib_hpet_configure(device_t self)
716 {
717 	struct lpcib_softc *sc = device_private(self);
718 	struct lpcib_hpet_attach_arg arg;
719 	uint32_t hpet_reg, val;
720 
721 	if (sc->sc_has_ich5_hpet) {
722 		val = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
723 		    LPCIB_PCI_GEN_CNTL);
724 		switch (val & LPCIB_ICH5_HPTC_WIN_MASK) {
725 		case LPCIB_ICH5_HPTC_0000:
726 			hpet_reg = LPCIB_ICH5_HPTC_0000_BASE;
727 			break;
728 		case LPCIB_ICH5_HPTC_1000:
729 			hpet_reg = LPCIB_ICH5_HPTC_1000_BASE;
730 			break;
731 		case LPCIB_ICH5_HPTC_2000:
732 			hpet_reg = LPCIB_ICH5_HPTC_2000_BASE;
733 			break;
734 		case LPCIB_ICH5_HPTC_3000:
735 			hpet_reg = LPCIB_ICH5_HPTC_3000_BASE;
736 			break;
737 		default:
738 			return;
739 		}
740 		val |= sc->sc_hpet_reg | LPCIB_ICH5_HPTC_EN;
741 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
742 		    LPCIB_PCI_GEN_CNTL, val);
743 	} else if (sc->sc_has_rcba) {
744 		val = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
745 		    LPCIB_RCBA_HPTC);
746 		switch (val & LPCIB_RCBA_HPTC_WIN_MASK) {
747 		case LPCIB_RCBA_HPTC_0000:
748 			hpet_reg = LPCIB_RCBA_HPTC_0000_BASE;
749 			break;
750 		case LPCIB_RCBA_HPTC_1000:
751 			hpet_reg = LPCIB_RCBA_HPTC_1000_BASE;
752 			break;
753 		case LPCIB_RCBA_HPTC_2000:
754 			hpet_reg = LPCIB_RCBA_HPTC_2000_BASE;
755 			break;
756 		case LPCIB_RCBA_HPTC_3000:
757 			hpet_reg = LPCIB_RCBA_HPTC_3000_BASE;
758 			break;
759 		default:
760 			return;
761 		}
762 		val |= LPCIB_RCBA_HPTC_EN;
763 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
764 		    val);
765 	} else {
766 		/* No HPET here */
767 		return;
768 	}
769 
770 	arg.hpet_mem_t = sc->sc_pa.pa_memt;
771 	arg.hpet_reg = hpet_reg;
772 
773 	config_found_ia(self, "hpetichbus", &arg, NULL);
774 }
775 #endif
776