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