Home | History | Annotate | Line # | Download | only in pci
ichlpcib.c revision 1.14.8.2
      1  1.14.8.2       jym /*	$NetBSD: ichlpcib.c,v 1.14.8.2 2009/11/01 13:58:17 jym 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.14.8.2       jym __KERNEL_RCSID(0, "$NetBSD: ichlpcib.c,v 1.14.8.2 2009/11/01 13:58:17 jym 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.14.8.2       jym #include <sys/gpio.h>
     51       1.1   xtraeme #include <machine/bus.h>
     52       1.1   xtraeme 
     53       1.1   xtraeme #include <dev/pci/pcivar.h>
     54       1.1   xtraeme #include <dev/pci/pcireg.h>
     55       1.1   xtraeme #include <dev/pci/pcidevs.h>
     56       1.1   xtraeme 
     57  1.14.8.2       jym #include <dev/gpio/gpiovar.h>
     58       1.1   xtraeme #include <dev/sysmon/sysmonvar.h>
     59       1.1   xtraeme 
     60       1.6  jmcneill #include <dev/ic/acpipmtimer.h>
     61       1.1   xtraeme #include <dev/ic/i82801lpcreg.h>
     62       1.6  jmcneill #include <dev/ic/hpetreg.h>
     63       1.6  jmcneill #include <dev/ic/hpetvar.h>
     64       1.6  jmcneill 
     65       1.6  jmcneill #include "hpet.h"
     66      1.12    martin #include "pcibvar.h"
     67  1.14.8.2       jym #include "gpio.h"
     68  1.14.8.2       jym 
     69  1.14.8.2       jym #define LPCIB_GPIO_NPINS 64
     70       1.1   xtraeme 
     71       1.1   xtraeme struct lpcib_softc {
     72      1.12    martin 	/* we call pcibattach() which assumes this starts like this: */
     73      1.12    martin 	struct pcib_softc	sc_pcib;
     74       1.1   xtraeme 
     75       1.6  jmcneill 	struct pci_attach_args	sc_pa;
     76       1.6  jmcneill 	int			sc_has_rcba;
     77       1.6  jmcneill 	int			sc_has_ich5_hpet;
     78       1.6  jmcneill 
     79       1.6  jmcneill 	/* RCBA */
     80       1.6  jmcneill 	bus_space_tag_t		sc_rcbat;
     81       1.6  jmcneill 	bus_space_handle_t	sc_rcbah;
     82       1.6  jmcneill 	pcireg_t		sc_rcba_reg;
     83       1.6  jmcneill 
     84       1.1   xtraeme 	/* Watchdog variables. */
     85       1.1   xtraeme 	struct sysmon_wdog	sc_smw;
     86       1.1   xtraeme 	bus_space_tag_t		sc_iot;
     87       1.1   xtraeme 	bus_space_handle_t	sc_ioh;
     88  1.14.8.2       jym 	bus_size_t		sc_iosize;
     89       1.6  jmcneill 
     90       1.6  jmcneill #if NHPET > 0
     91       1.6  jmcneill 	/* HPET variables. */
     92       1.6  jmcneill 	uint32_t		sc_hpet_reg;
     93       1.6  jmcneill #endif
     94       1.6  jmcneill 
     95  1.14.8.2       jym #if NGPIO > 0
     96  1.14.8.2       jym 	device_t		sc_gpiobus;
     97  1.14.8.2       jym 	kmutex_t		sc_gpio_mtx;
     98  1.14.8.2       jym 	bus_space_tag_t		sc_gpio_iot;
     99  1.14.8.2       jym 	bus_space_handle_t	sc_gpio_ioh;
    100  1.14.8.2       jym 	bus_size_t		sc_gpio_ios;
    101  1.14.8.2       jym 	struct gpio_chipset_tag	sc_gpio_gc;
    102  1.14.8.2       jym 	gpio_pin_t		sc_gpio_pins[LPCIB_GPIO_NPINS];
    103  1.14.8.2       jym #endif
    104  1.14.8.2       jym 
    105  1.14.8.1       jym 	/* Speedstep */
    106  1.14.8.1       jym 	pcireg_t		sc_pmcon_orig;
    107  1.14.8.1       jym 
    108       1.1   xtraeme 	/* Power management */
    109       1.7  drochner 	pcireg_t		sc_pirq[2];
    110       1.6  jmcneill 	pcireg_t		sc_pmcon;
    111       1.6  jmcneill 	pcireg_t		sc_fwhsel2;
    112  1.14.8.2       jym 
    113  1.14.8.2       jym 	/* Child devices */
    114  1.14.8.2       jym 	device_t		sc_hpetbus;
    115  1.14.8.2       jym 	acpipmtimer_t		sc_pmtimer;
    116  1.14.8.2       jym 	pcireg_t		sc_acpi_cntl;
    117  1.14.8.2       jym 
    118  1.14.8.2       jym 	struct sysctllog	*sc_log;
    119       1.1   xtraeme };
    120       1.1   xtraeme 
    121       1.9   xtraeme static int lpcibmatch(device_t, cfdata_t, void *);
    122       1.9   xtraeme static void lpcibattach(device_t, device_t, void *);
    123  1.14.8.2       jym static int lpcibdetach(device_t, int);
    124  1.14.8.2       jym static void lpcibchilddet(device_t, device_t);
    125  1.14.8.2       jym static int lpcibrescan(device_t, const char *, const int *);
    126       1.8    dyoung static bool lpcib_suspend(device_t PMF_FN_PROTO);
    127       1.8    dyoung static bool lpcib_resume(device_t PMF_FN_PROTO);
    128  1.14.8.1       jym static bool lpcib_shutdown(device_t, int);
    129       1.1   xtraeme 
    130       1.9   xtraeme static void pmtimer_configure(device_t);
    131  1.14.8.2       jym static int pmtimer_unconfigure(device_t, int);
    132       1.1   xtraeme 
    133       1.9   xtraeme static void tcotimer_configure(device_t);
    134  1.14.8.2       jym static int tcotimer_unconfigure(device_t, int);
    135       1.1   xtraeme static int tcotimer_setmode(struct sysmon_wdog *);
    136       1.1   xtraeme static int tcotimer_tickle(struct sysmon_wdog *);
    137       1.1   xtraeme static void tcotimer_stop(struct lpcib_softc *);
    138       1.1   xtraeme static void tcotimer_start(struct lpcib_softc *);
    139       1.1   xtraeme static void tcotimer_status_reset(struct lpcib_softc *);
    140       1.9   xtraeme static int  tcotimer_disable_noreboot(device_t);
    141       1.1   xtraeme 
    142       1.9   xtraeme static void speedstep_configure(device_t);
    143  1.14.8.2       jym static void speedstep_unconfigure(device_t);
    144       1.1   xtraeme static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
    145       1.1   xtraeme 
    146       1.6  jmcneill #if NHPET > 0
    147       1.9   xtraeme static void lpcib_hpet_configure(device_t);
    148  1.14.8.2       jym static int lpcib_hpet_unconfigure(device_t, int);
    149  1.14.8.2       jym #endif
    150  1.14.8.2       jym 
    151  1.14.8.2       jym #if NGPIO > 0
    152  1.14.8.2       jym static void lpcib_gpio_configure(device_t);
    153  1.14.8.2       jym static int lpcib_gpio_unconfigure(device_t, int);
    154  1.14.8.2       jym static int lpcib_gpio_pin_read(void *, int);
    155  1.14.8.2       jym static void lpcib_gpio_pin_write(void *, int, int);
    156  1.14.8.2       jym static void lpcib_gpio_pin_ctl(void *, int, int);
    157       1.6  jmcneill #endif
    158       1.6  jmcneill 
    159       1.1   xtraeme struct lpcib_softc *speedstep_cookie;	/* XXX */
    160       1.1   xtraeme 
    161  1.14.8.2       jym CFATTACH_DECL2_NEW(ichlpcib, sizeof(struct lpcib_softc),
    162  1.14.8.2       jym     lpcibmatch, lpcibattach, lpcibdetach, NULL, lpcibrescan, lpcibchilddet);
    163       1.1   xtraeme 
    164       1.6  jmcneill static struct lpcib_device {
    165       1.6  jmcneill 	pcireg_t vendor, product;
    166       1.6  jmcneill 	int has_rcba;
    167       1.6  jmcneill 	int has_ich5_hpet;
    168       1.6  jmcneill } lpcib_devices[] = {
    169       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801AA_LPC, 0, 0 },
    170       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BA_LPC, 0, 0 },
    171       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BAM_LPC, 0, 0 },
    172       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CA_LPC, 0, 0 },
    173       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CAM_LPC, 0, 0 },
    174       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DB_LPC, 0, 0 },
    175       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DB_ISA, 0, 0 },
    176       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801EB_LPC, 0, 1 },
    177       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801FB_LPC, 1, 0 },
    178       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801FBM_LPC, 1, 0 },
    179       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801G_LPC, 1, 0 },
    180       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801GBM_LPC, 1, 0 },
    181       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801GHM_LPC, 1, 0 },
    182       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801H_LPC, 1, 0 },
    183       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HEM_LPC, 1, 0 },
    184       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HH_LPC, 1, 0 },
    185       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HO_LPC, 1, 0 },
    186       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801HBM_LPC, 1, 0 },
    187       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IH_LPC, 1, 0 },
    188       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IO_LPC, 1, 0 },
    189       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IR_LPC, 1, 0 },
    190  1.14.8.1       jym 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IEM_LPC, 1, 0 },
    191       1.6  jmcneill 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801IB_LPC, 1, 0 },
    192      1.14     joerg 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_63XXESB_LPC, 1, 0 },
    193      1.14     joerg 
    194       1.6  jmcneill 	{ 0, 0, 0, 0 },
    195       1.6  jmcneill };
    196       1.6  jmcneill 
    197       1.1   xtraeme /*
    198       1.1   xtraeme  * Autoconf callbacks.
    199       1.1   xtraeme  */
    200       1.1   xtraeme static int
    201       1.9   xtraeme lpcibmatch(device_t parent, cfdata_t match, void *aux)
    202       1.1   xtraeme {
    203       1.1   xtraeme 	struct pci_attach_args *pa = aux;
    204       1.6  jmcneill 	struct lpcib_device *lpcib_dev;
    205       1.1   xtraeme 
    206       1.1   xtraeme 	/* We are ISA bridge, of course */
    207       1.1   xtraeme 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
    208       1.1   xtraeme 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
    209       1.1   xtraeme 		return 0;
    210       1.1   xtraeme 
    211       1.6  jmcneill 	for (lpcib_dev = lpcib_devices; lpcib_dev->vendor; ++lpcib_dev) {
    212       1.6  jmcneill 		if (PCI_VENDOR(pa->pa_id) == lpcib_dev->vendor &&
    213       1.6  jmcneill 		    PCI_PRODUCT(pa->pa_id) == lpcib_dev->product)
    214       1.1   xtraeme 			return 10;
    215       1.1   xtraeme 	}
    216       1.1   xtraeme 
    217       1.1   xtraeme 	return 0;
    218       1.1   xtraeme }
    219       1.1   xtraeme 
    220       1.1   xtraeme static void
    221       1.9   xtraeme lpcibattach(device_t parent, device_t self, void *aux)
    222       1.1   xtraeme {
    223       1.1   xtraeme 	struct pci_attach_args *pa = aux;
    224       1.6  jmcneill 	struct lpcib_softc *sc = device_private(self);
    225       1.6  jmcneill 	struct lpcib_device *lpcib_dev;
    226       1.1   xtraeme 
    227       1.6  jmcneill 	sc->sc_pa = *pa;
    228       1.6  jmcneill 
    229       1.6  jmcneill 	for (lpcib_dev = lpcib_devices; lpcib_dev->vendor; ++lpcib_dev) {
    230       1.6  jmcneill 		if (PCI_VENDOR(pa->pa_id) != lpcib_dev->vendor ||
    231       1.6  jmcneill 		    PCI_PRODUCT(pa->pa_id) != lpcib_dev->product)
    232       1.6  jmcneill 			continue;
    233       1.6  jmcneill 		sc->sc_has_rcba = lpcib_dev->has_rcba;
    234       1.6  jmcneill 		sc->sc_has_ich5_hpet = lpcib_dev->has_ich5_hpet;
    235       1.6  jmcneill 		break;
    236       1.6  jmcneill 	}
    237       1.1   xtraeme 
    238       1.1   xtraeme 	pcibattach(parent, self, aux);
    239       1.1   xtraeme 
    240       1.1   xtraeme 	/*
    241       1.1   xtraeme 	 * Part of our I/O registers are used as ACPI PM regs.
    242       1.1   xtraeme 	 * Since our ACPI subsystem accesses the I/O space directly so far,
    243       1.1   xtraeme 	 * we do not have to bother bus_space I/O map confliction.
    244       1.1   xtraeme 	 */
    245       1.1   xtraeme 	if (pci_mapreg_map(pa, LPCIB_PCI_PMBASE, PCI_MAPREG_TYPE_IO, 0,
    246  1.14.8.2       jym 			   &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_iosize)) {
    247       1.9   xtraeme 		aprint_error_dev(self, "can't map power management i/o space");
    248       1.1   xtraeme 		return;
    249       1.1   xtraeme 	}
    250       1.1   xtraeme 
    251  1.14.8.1       jym 	sc->sc_pmcon_orig = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    252  1.14.8.1       jym 	    LPCIB_PCI_GEN_PMCON_1);
    253  1.14.8.1       jym 
    254       1.6  jmcneill 	/* For ICH6 and later, always enable RCBA */
    255       1.6  jmcneill 	if (sc->sc_has_rcba) {
    256       1.6  jmcneill 		pcireg_t rcba;
    257       1.6  jmcneill 
    258       1.6  jmcneill 		sc->sc_rcbat = sc->sc_pa.pa_memt;
    259       1.6  jmcneill 
    260      1.12    martin 		rcba = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    261      1.12    martin 		     LPCIB_RCBA);
    262       1.6  jmcneill 		if ((rcba & LPCIB_RCBA_EN) == 0) {
    263       1.9   xtraeme 			aprint_error_dev(self, "RCBA is not enabled");
    264       1.6  jmcneill 			return;
    265       1.6  jmcneill 		}
    266       1.6  jmcneill 		rcba &= ~LPCIB_RCBA_EN;
    267       1.6  jmcneill 
    268       1.6  jmcneill 		if (bus_space_map(sc->sc_rcbat, rcba, LPCIB_RCBA_SIZE, 0,
    269       1.6  jmcneill 				  &sc->sc_rcbah)) {
    270       1.9   xtraeme 			aprint_error_dev(self, "RCBA could not be mapped");
    271       1.6  jmcneill 			return;
    272       1.6  jmcneill 		}
    273       1.6  jmcneill 	}
    274       1.6  jmcneill 
    275       1.1   xtraeme 	/* Set up the power management timer. */
    276       1.9   xtraeme 	pmtimer_configure(self);
    277       1.1   xtraeme 
    278       1.1   xtraeme 	/* Set up the TCO (watchdog). */
    279       1.9   xtraeme 	tcotimer_configure(self);
    280       1.1   xtraeme 
    281       1.1   xtraeme 	/* Set up SpeedStep. */
    282       1.9   xtraeme 	speedstep_configure(self);
    283       1.1   xtraeme 
    284       1.6  jmcneill #if NHPET > 0
    285       1.6  jmcneill 	/* Set up HPET. */
    286       1.9   xtraeme 	lpcib_hpet_configure(self);
    287       1.6  jmcneill #endif
    288       1.6  jmcneill 
    289  1.14.8.2       jym #if NGPIO > 0
    290  1.14.8.2       jym 	/* Set up GPIO */
    291  1.14.8.2       jym 	lpcib_gpio_configure(self);
    292  1.14.8.2       jym #endif
    293  1.14.8.2       jym 
    294       1.6  jmcneill 	/* Install power handler */
    295  1.14.8.1       jym 	if (!pmf_device_register1(self, lpcib_suspend, lpcib_resume,
    296  1.14.8.1       jym 	    lpcib_shutdown))
    297       1.6  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    298       1.6  jmcneill }
    299       1.6  jmcneill 
    300  1.14.8.2       jym static void
    301  1.14.8.2       jym lpcibchilddet(device_t self, device_t child)
    302  1.14.8.2       jym {
    303  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
    304  1.14.8.2       jym 	uint32_t val;
    305  1.14.8.2       jym 
    306  1.14.8.2       jym #if NGPIO > 0
    307  1.14.8.2       jym 	if (sc->sc_gpiobus == child) {
    308  1.14.8.2       jym 		sc->sc_gpiobus = NULL;
    309  1.14.8.2       jym 		return;
    310  1.14.8.2       jym 	}
    311  1.14.8.2       jym #endif
    312  1.14.8.2       jym 	if (sc->sc_hpetbus != child) {
    313  1.14.8.2       jym 		pcibchilddet(self, child);
    314  1.14.8.2       jym 		return;
    315  1.14.8.2       jym 	}
    316  1.14.8.2       jym 	sc->sc_hpetbus = NULL;
    317  1.14.8.2       jym 	if (sc->sc_has_ich5_hpet) {
    318  1.14.8.2       jym 		val = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    319  1.14.8.2       jym 		    LPCIB_PCI_GEN_CNTL);
    320  1.14.8.2       jym 		switch (val & LPCIB_ICH5_HPTC_WIN_MASK) {
    321  1.14.8.2       jym 		case LPCIB_ICH5_HPTC_0000:
    322  1.14.8.2       jym 		case LPCIB_ICH5_HPTC_1000:
    323  1.14.8.2       jym 		case LPCIB_ICH5_HPTC_2000:
    324  1.14.8.2       jym 		case LPCIB_ICH5_HPTC_3000:
    325  1.14.8.2       jym 			break;
    326  1.14.8.2       jym 		default:
    327  1.14.8.2       jym 			return;
    328  1.14.8.2       jym 		}
    329  1.14.8.2       jym 		val &= ~LPCIB_ICH5_HPTC_EN;
    330  1.14.8.2       jym 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    331  1.14.8.2       jym 		    LPCIB_PCI_GEN_CNTL, val);
    332  1.14.8.2       jym 	} else if (sc->sc_has_rcba) {
    333  1.14.8.2       jym 		val = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    334  1.14.8.2       jym 		    LPCIB_RCBA_HPTC);
    335  1.14.8.2       jym 		switch (val & LPCIB_RCBA_HPTC_WIN_MASK) {
    336  1.14.8.2       jym 		case LPCIB_RCBA_HPTC_0000:
    337  1.14.8.2       jym 		case LPCIB_RCBA_HPTC_1000:
    338  1.14.8.2       jym 		case LPCIB_RCBA_HPTC_2000:
    339  1.14.8.2       jym 		case LPCIB_RCBA_HPTC_3000:
    340  1.14.8.2       jym 			break;
    341  1.14.8.2       jym 		default:
    342  1.14.8.2       jym 			return;
    343  1.14.8.2       jym 		}
    344  1.14.8.2       jym 		val &= ~LPCIB_RCBA_HPTC_EN;
    345  1.14.8.2       jym 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
    346  1.14.8.2       jym 		    val);
    347  1.14.8.2       jym 	}
    348  1.14.8.2       jym }
    349  1.14.8.2       jym 
    350  1.14.8.2       jym #if NHPET > 0 || NGPIO > 0
    351  1.14.8.2       jym /* XXX share this with sys/arch/i386/pci/elan520.c */
    352  1.14.8.2       jym static bool
    353  1.14.8.2       jym ifattr_match(const char *snull, const char *t)
    354  1.14.8.2       jym {
    355  1.14.8.2       jym 	return (snull == NULL) || strcmp(snull, t) == 0;
    356  1.14.8.2       jym }
    357  1.14.8.2       jym #endif
    358  1.14.8.2       jym 
    359  1.14.8.2       jym static int
    360  1.14.8.2       jym lpcibrescan(device_t self, const char *ifattr, const int *locators)
    361  1.14.8.2       jym {
    362  1.14.8.2       jym #if NHPET > 0 || NGPIO > 0
    363  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
    364  1.14.8.2       jym #endif
    365  1.14.8.2       jym 
    366  1.14.8.2       jym #if NHPET > 0
    367  1.14.8.2       jym 	if (ifattr_match(ifattr, "hpetichbus") && sc->sc_hpetbus == NULL)
    368  1.14.8.2       jym 		lpcib_hpet_configure(self);
    369  1.14.8.2       jym #endif
    370  1.14.8.2       jym 
    371  1.14.8.2       jym #if NGPIO > 0
    372  1.14.8.2       jym 	if (ifattr_match(ifattr, "gpiobus") && sc->sc_gpiobus == NULL)
    373  1.14.8.2       jym 		lpcib_gpio_configure(self);
    374  1.14.8.2       jym #endif
    375  1.14.8.2       jym 
    376  1.14.8.2       jym 	return pcibrescan(self, ifattr, locators);
    377  1.14.8.2       jym }
    378  1.14.8.2       jym 
    379  1.14.8.2       jym static int
    380  1.14.8.2       jym lpcibdetach(device_t self, int flags)
    381  1.14.8.2       jym {
    382  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
    383  1.14.8.2       jym 	int rc;
    384  1.14.8.2       jym 
    385  1.14.8.2       jym 	pmf_device_deregister(self);
    386  1.14.8.2       jym 
    387  1.14.8.2       jym #if NHPET > 0
    388  1.14.8.2       jym 	if ((rc = lpcib_hpet_unconfigure(self, flags)) != 0)
    389  1.14.8.2       jym 		return rc;
    390  1.14.8.2       jym #endif
    391  1.14.8.2       jym 
    392  1.14.8.2       jym #if NGPIO > 0
    393  1.14.8.2       jym 	if ((rc = lpcib_gpio_unconfigure(self, flags)) != 0)
    394  1.14.8.2       jym 		return rc;
    395  1.14.8.2       jym #endif
    396  1.14.8.2       jym 
    397  1.14.8.2       jym 	/* Set up SpeedStep. */
    398  1.14.8.2       jym 	speedstep_unconfigure(self);
    399  1.14.8.2       jym 
    400  1.14.8.2       jym 	if ((rc = tcotimer_unconfigure(self, flags)) != 0)
    401  1.14.8.2       jym 		return rc;
    402  1.14.8.2       jym 
    403  1.14.8.2       jym 	if ((rc = pmtimer_unconfigure(self, flags)) != 0)
    404  1.14.8.2       jym 		return rc;
    405  1.14.8.2       jym 
    406  1.14.8.2       jym 	if (sc->sc_has_rcba)
    407  1.14.8.2       jym 		bus_space_unmap(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_SIZE);
    408  1.14.8.2       jym 
    409  1.14.8.2       jym 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    410  1.14.8.2       jym 
    411  1.14.8.2       jym 	return pcibdetach(self, flags);
    412  1.14.8.2       jym }
    413  1.14.8.2       jym 
    414       1.6  jmcneill static bool
    415  1.14.8.1       jym lpcib_shutdown(device_t dv, int howto)
    416  1.14.8.1       jym {
    417  1.14.8.1       jym 	struct lpcib_softc *sc = device_private(dv);
    418  1.14.8.1       jym 
    419  1.14.8.1       jym 	pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    420  1.14.8.1       jym 	    LPCIB_PCI_GEN_PMCON_1, sc->sc_pmcon_orig);
    421  1.14.8.1       jym 
    422  1.14.8.1       jym 	return true;
    423  1.14.8.1       jym }
    424  1.14.8.1       jym 
    425  1.14.8.1       jym static bool
    426       1.8    dyoung lpcib_suspend(device_t dv PMF_FN_ARGS)
    427       1.6  jmcneill {
    428       1.6  jmcneill 	struct lpcib_softc *sc = device_private(dv);
    429      1.12    martin 	pci_chipset_tag_t pc = sc->sc_pcib.sc_pc;
    430      1.12    martin 	pcitag_t tag = sc->sc_pcib.sc_tag;
    431       1.6  jmcneill 
    432       1.6  jmcneill 	/* capture PIRQ routing control registers */
    433       1.6  jmcneill 	sc->sc_pirq[0] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQA_ROUT);
    434       1.7  drochner 	sc->sc_pirq[1] = pci_conf_read(pc, tag, LPCIB_PCI_PIRQE_ROUT);
    435       1.6  jmcneill 
    436       1.6  jmcneill 	sc->sc_pmcon = pci_conf_read(pc, tag, LPCIB_PCI_GEN_PMCON_1);
    437       1.6  jmcneill 	sc->sc_fwhsel2 = pci_conf_read(pc, tag, LPCIB_PCI_GEN_STA);
    438       1.6  jmcneill 
    439       1.6  jmcneill 	if (sc->sc_has_rcba) {
    440       1.6  jmcneill 		sc->sc_rcba_reg = pci_conf_read(pc, tag, LPCIB_RCBA);
    441       1.6  jmcneill #if NHPET > 0
    442       1.6  jmcneill 		sc->sc_hpet_reg = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    443       1.6  jmcneill 		    LPCIB_RCBA_HPTC);
    444       1.6  jmcneill #endif
    445       1.6  jmcneill 	} else if (sc->sc_has_ich5_hpet) {
    446       1.6  jmcneill #if NHPET > 0
    447       1.6  jmcneill 		sc->sc_hpet_reg = pci_conf_read(pc, tag, LPCIB_PCI_GEN_CNTL);
    448       1.6  jmcneill #endif
    449       1.6  jmcneill 	}
    450       1.6  jmcneill 
    451       1.6  jmcneill 	return true;
    452       1.6  jmcneill }
    453       1.6  jmcneill 
    454       1.6  jmcneill static bool
    455       1.8    dyoung lpcib_resume(device_t dv PMF_FN_ARGS)
    456       1.6  jmcneill {
    457       1.6  jmcneill 	struct lpcib_softc *sc = device_private(dv);
    458      1.12    martin 	pci_chipset_tag_t pc = sc->sc_pcib.sc_pc;
    459      1.12    martin 	pcitag_t tag = sc->sc_pcib.sc_tag;
    460       1.6  jmcneill 
    461       1.6  jmcneill 	/* restore PIRQ routing control registers */
    462       1.6  jmcneill 	pci_conf_write(pc, tag, LPCIB_PCI_PIRQA_ROUT, sc->sc_pirq[0]);
    463       1.7  drochner 	pci_conf_write(pc, tag, LPCIB_PCI_PIRQE_ROUT, sc->sc_pirq[1]);
    464       1.6  jmcneill 
    465       1.6  jmcneill 	pci_conf_write(pc, tag, LPCIB_PCI_GEN_PMCON_1, sc->sc_pmcon);
    466       1.6  jmcneill 	pci_conf_write(pc, tag, LPCIB_PCI_GEN_STA, sc->sc_fwhsel2);
    467       1.6  jmcneill 
    468       1.6  jmcneill 	if (sc->sc_has_rcba) {
    469       1.6  jmcneill 		pci_conf_write(pc, tag, LPCIB_RCBA, sc->sc_rcba_reg);
    470       1.6  jmcneill #if NHPET > 0
    471       1.6  jmcneill 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
    472       1.6  jmcneill 		    sc->sc_hpet_reg);
    473       1.6  jmcneill #endif
    474       1.6  jmcneill 	} else if (sc->sc_has_ich5_hpet) {
    475       1.6  jmcneill #if NHPET > 0
    476       1.6  jmcneill 		pci_conf_write(pc, tag, LPCIB_PCI_GEN_CNTL, sc->sc_hpet_reg);
    477       1.6  jmcneill #endif
    478       1.6  jmcneill 	}
    479       1.1   xtraeme 
    480       1.6  jmcneill 	return true;
    481       1.1   xtraeme }
    482       1.1   xtraeme 
    483       1.1   xtraeme /*
    484       1.1   xtraeme  * Initialize the power management timer.
    485       1.1   xtraeme  */
    486       1.1   xtraeme static void
    487       1.9   xtraeme pmtimer_configure(device_t self)
    488       1.1   xtraeme {
    489       1.9   xtraeme 	struct lpcib_softc *sc = device_private(self);
    490       1.1   xtraeme 	pcireg_t control;
    491       1.1   xtraeme 
    492       1.1   xtraeme 	/*
    493       1.1   xtraeme 	 * Check if power management I/O space is enabled and enable the ACPI_EN
    494       1.1   xtraeme 	 * bit if it's disabled.
    495       1.1   xtraeme 	 */
    496      1.12    martin 	control = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    497      1.12    martin 	    LPCIB_PCI_ACPI_CNTL);
    498  1.14.8.2       jym 	sc->sc_acpi_cntl = control;
    499       1.1   xtraeme 	if ((control & LPCIB_PCI_ACPI_CNTL_EN) == 0) {
    500       1.1   xtraeme 		control |= LPCIB_PCI_ACPI_CNTL_EN;
    501      1.12    martin 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    502      1.12    martin 		    LPCIB_PCI_ACPI_CNTL, control);
    503       1.1   xtraeme 	}
    504       1.1   xtraeme 
    505       1.1   xtraeme 	/* Attach our PM timer with the generic acpipmtimer function */
    506  1.14.8.2       jym 	sc->sc_pmtimer = acpipmtimer_attach(self, sc->sc_iot, sc->sc_ioh,
    507       1.1   xtraeme 	    LPCIB_PM1_TMR, 0);
    508       1.1   xtraeme }
    509       1.1   xtraeme 
    510  1.14.8.2       jym static int
    511  1.14.8.2       jym pmtimer_unconfigure(device_t self, int flags)
    512  1.14.8.2       jym {
    513  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
    514  1.14.8.2       jym 	int rc;
    515  1.14.8.2       jym 
    516  1.14.8.2       jym 	if (sc->sc_pmtimer != NULL &&
    517  1.14.8.2       jym 	    (rc = acpipmtimer_detach(sc->sc_pmtimer, flags)) != 0)
    518  1.14.8.2       jym 		return rc;
    519  1.14.8.2       jym 
    520  1.14.8.2       jym 	pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    521  1.14.8.2       jym 	    LPCIB_PCI_ACPI_CNTL, sc->sc_acpi_cntl);
    522  1.14.8.2       jym 
    523  1.14.8.2       jym 	return 0;
    524  1.14.8.2       jym }
    525  1.14.8.2       jym 
    526       1.1   xtraeme /*
    527       1.1   xtraeme  * Initialize the watchdog timer.
    528       1.1   xtraeme  */
    529       1.1   xtraeme static void
    530       1.9   xtraeme tcotimer_configure(device_t self)
    531       1.1   xtraeme {
    532       1.9   xtraeme 	struct lpcib_softc *sc = device_private(self);
    533       1.1   xtraeme 	uint32_t ioreg;
    534       1.1   xtraeme 	unsigned int period;
    535       1.1   xtraeme 
    536      1.13      yamt 	/* Explicitly stop the TCO timer. */
    537      1.13      yamt 	tcotimer_stop(sc);
    538      1.13      yamt 
    539      1.13      yamt 	/*
    540      1.13      yamt 	 * Enable TCO timeout SMI only if the hardware reset does not
    541      1.13      yamt 	 * work. We don't know what the SMBIOS does.
    542      1.13      yamt 	 */
    543      1.13      yamt 	ioreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN);
    544      1.13      yamt 	ioreg &= ~LPCIB_SMI_EN_TCO_EN;
    545      1.13      yamt 
    546       1.1   xtraeme 	/*
    547       1.4   xtraeme 	 * Clear the No Reboot (NR) bit. If this fails, enabling the TCO_EN bit
    548       1.1   xtraeme 	 * in the SMI_EN register is the last chance.
    549       1.1   xtraeme 	 */
    550       1.9   xtraeme 	if (tcotimer_disable_noreboot(self)) {
    551       1.1   xtraeme 		ioreg |= LPCIB_SMI_EN_TCO_EN;
    552      1.13      yamt 	}
    553      1.13      yamt 	if ((ioreg & LPCIB_SMI_EN_GBL_SMI_EN) != 0) {
    554       1.1   xtraeme 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN, ioreg);
    555       1.1   xtraeme 	}
    556       1.1   xtraeme 
    557       1.1   xtraeme 	/* Reset the watchdog status registers. */
    558       1.1   xtraeme 	tcotimer_status_reset(sc);
    559       1.1   xtraeme 
    560       1.1   xtraeme 	/*
    561       1.1   xtraeme 	 * Register the driver with the sysmon watchdog framework.
    562       1.1   xtraeme 	 */
    563       1.9   xtraeme 	sc->sc_smw.smw_name = device_xname(self);
    564       1.1   xtraeme 	sc->sc_smw.smw_cookie = sc;
    565       1.1   xtraeme 	sc->sc_smw.smw_setmode = tcotimer_setmode;
    566       1.1   xtraeme 	sc->sc_smw.smw_tickle = tcotimer_tickle;
    567       1.6  jmcneill 	if (sc->sc_has_rcba)
    568       1.1   xtraeme 		period = LPCIB_TCOTIMER2_MAX_TICK;
    569       1.1   xtraeme 	else
    570       1.1   xtraeme 		period = LPCIB_TCOTIMER_MAX_TICK;
    571       1.1   xtraeme 	sc->sc_smw.smw_period = lpcib_tcotimer_tick_to_second(period);
    572       1.1   xtraeme 
    573       1.1   xtraeme 	if (sysmon_wdog_register(&sc->sc_smw)) {
    574       1.9   xtraeme 		aprint_error_dev(self, "unable to register TCO timer"
    575       1.9   xtraeme 		       "as a sysmon watchdog device.\n");
    576       1.1   xtraeme 		return;
    577       1.1   xtraeme 	}
    578       1.1   xtraeme 
    579       1.9   xtraeme 	aprint_verbose_dev(self, "TCO (watchdog) timer configured.\n");
    580       1.1   xtraeme }
    581       1.1   xtraeme 
    582  1.14.8.2       jym static int
    583  1.14.8.2       jym tcotimer_unconfigure(device_t self, int flags)
    584  1.14.8.2       jym {
    585  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
    586  1.14.8.2       jym 	int rc;
    587  1.14.8.2       jym 
    588  1.14.8.2       jym 	if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
    589  1.14.8.2       jym 		if (rc == ERESTART)
    590  1.14.8.2       jym 			rc = EINTR;
    591  1.14.8.2       jym 		return rc;
    592  1.14.8.2       jym 	}
    593  1.14.8.2       jym 
    594  1.14.8.2       jym 	/* Explicitly stop the TCO timer. */
    595  1.14.8.2       jym 	tcotimer_stop(sc);
    596  1.14.8.2       jym 
    597  1.14.8.2       jym 	/* XXX Set No Reboot? */
    598  1.14.8.2       jym 
    599  1.14.8.2       jym 	return 0;
    600  1.14.8.2       jym }
    601  1.14.8.2       jym 
    602  1.14.8.2       jym 
    603       1.1   xtraeme /*
    604       1.1   xtraeme  * Sysmon watchdog callbacks.
    605       1.1   xtraeme  */
    606       1.1   xtraeme static int
    607       1.1   xtraeme tcotimer_setmode(struct sysmon_wdog *smw)
    608       1.1   xtraeme {
    609       1.1   xtraeme 	struct lpcib_softc *sc = smw->smw_cookie;
    610       1.1   xtraeme 	unsigned int period;
    611       1.1   xtraeme 	uint16_t ich6period = 0;
    612  1.14.8.2       jym 	uint8_t ich5period = 0;
    613       1.1   xtraeme 
    614       1.1   xtraeme 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    615       1.1   xtraeme 		/* Stop the TCO timer. */
    616       1.1   xtraeme 		tcotimer_stop(sc);
    617       1.1   xtraeme 	} else {
    618       1.1   xtraeme 		/*
    619       1.6  jmcneill 		 * ICH6 or newer are limited to 2s min and 613s max.
    620       1.1   xtraeme 		 * ICH5 or older are limited to 4s min and 39s max.
    621       1.1   xtraeme 		 */
    622  1.14.8.2       jym 		period = lpcib_tcotimer_second_to_tick(smw->smw_period);
    623       1.6  jmcneill 		if (sc->sc_has_rcba) {
    624  1.14.8.2       jym 			if (period < LPCIB_TCOTIMER2_MIN_TICK ||
    625  1.14.8.2       jym 			    period > LPCIB_TCOTIMER2_MAX_TICK)
    626       1.6  jmcneill 				return EINVAL;
    627       1.6  jmcneill 		} else {
    628  1.14.8.2       jym 			if (period < LPCIB_TCOTIMER_MIN_TICK ||
    629  1.14.8.2       jym 			    period > LPCIB_TCOTIMER_MAX_TICK)
    630       1.1   xtraeme 				return EINVAL;
    631       1.1   xtraeme 		}
    632       1.5   xtraeme 
    633       1.1   xtraeme 		/* Stop the TCO timer, */
    634       1.1   xtraeme 		tcotimer_stop(sc);
    635       1.1   xtraeme 
    636       1.1   xtraeme 		/* set the timeout, */
    637       1.6  jmcneill 		if (sc->sc_has_rcba) {
    638       1.1   xtraeme 			/* ICH6 or newer */
    639       1.1   xtraeme 			ich6period = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    640       1.1   xtraeme 						      LPCIB_TCO_TMR2);
    641       1.1   xtraeme 			ich6period &= 0xfc00;
    642       1.1   xtraeme 			bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    643       1.1   xtraeme 					  LPCIB_TCO_TMR2, ich6period | period);
    644       1.1   xtraeme 		} else {
    645       1.1   xtraeme 			/* ICH5 or older */
    646  1.14.8.2       jym 			ich5period = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    647       1.1   xtraeme 						   LPCIB_TCO_TMR);
    648  1.14.8.2       jym 			ich5period &= 0xc0;
    649       1.1   xtraeme 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    650  1.14.8.2       jym 					  LPCIB_TCO_TMR, ich5period | period);
    651       1.1   xtraeme 		}
    652       1.1   xtraeme 
    653       1.1   xtraeme 		/* and start/reload the timer. */
    654       1.1   xtraeme 		tcotimer_start(sc);
    655       1.1   xtraeme 		tcotimer_tickle(smw);
    656       1.1   xtraeme 	}
    657       1.1   xtraeme 
    658       1.1   xtraeme 	return 0;
    659       1.1   xtraeme }
    660       1.1   xtraeme 
    661       1.1   xtraeme static int
    662       1.1   xtraeme tcotimer_tickle(struct sysmon_wdog *smw)
    663       1.1   xtraeme {
    664       1.1   xtraeme 	struct lpcib_softc *sc = smw->smw_cookie;
    665       1.1   xtraeme 
    666       1.1   xtraeme 	/* any value is allowed */
    667       1.6  jmcneill 	if (sc->sc_has_rcba)
    668       1.6  jmcneill 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
    669       1.6  jmcneill 	else
    670       1.1   xtraeme 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
    671       1.1   xtraeme 
    672       1.1   xtraeme 	return 0;
    673       1.1   xtraeme }
    674       1.1   xtraeme 
    675       1.1   xtraeme static void
    676       1.1   xtraeme tcotimer_stop(struct lpcib_softc *sc)
    677       1.1   xtraeme {
    678       1.1   xtraeme 	uint16_t ioreg;
    679       1.1   xtraeme 
    680       1.1   xtraeme 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
    681       1.1   xtraeme 	ioreg |= LPCIB_TCO1_CNT_TCO_TMR_HLT;
    682       1.1   xtraeme 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
    683       1.1   xtraeme }
    684       1.1   xtraeme 
    685       1.1   xtraeme static void
    686       1.1   xtraeme tcotimer_start(struct lpcib_softc *sc)
    687       1.1   xtraeme {
    688       1.1   xtraeme 	uint16_t ioreg;
    689       1.1   xtraeme 
    690       1.1   xtraeme 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
    691       1.1   xtraeme 	ioreg &= ~LPCIB_TCO1_CNT_TCO_TMR_HLT;
    692       1.1   xtraeme 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
    693       1.1   xtraeme }
    694       1.1   xtraeme 
    695       1.1   xtraeme static void
    696       1.1   xtraeme tcotimer_status_reset(struct lpcib_softc *sc)
    697       1.1   xtraeme {
    698       1.1   xtraeme 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_STS,
    699       1.1   xtraeme 			  LPCIB_TCO1_STS_TIMEOUT);
    700       1.1   xtraeme 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
    701       1.1   xtraeme 			  LPCIB_TCO2_STS_BOOT_STS);
    702       1.1   xtraeme 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
    703       1.1   xtraeme 			  LPCIB_TCO2_STS_SECONDS_TO_STS);
    704       1.1   xtraeme }
    705       1.1   xtraeme 
    706       1.1   xtraeme /*
    707       1.4   xtraeme  * Clear the No Reboot (NR) bit, this enables reboots when the timer
    708       1.4   xtraeme  * reaches the timeout for the second time.
    709       1.1   xtraeme  */
    710       1.1   xtraeme static int
    711       1.9   xtraeme tcotimer_disable_noreboot(device_t self)
    712       1.1   xtraeme {
    713       1.9   xtraeme 	struct lpcib_softc *sc = device_private(self);
    714       1.1   xtraeme 
    715       1.6  jmcneill 	if (sc->sc_has_rcba) {
    716       1.6  jmcneill 		uint32_t status;
    717       1.6  jmcneill 
    718       1.9   xtraeme 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    719       1.9   xtraeme 		    LPCIB_GCS_OFFSET);
    720       1.6  jmcneill 		status &= ~LPCIB_GCS_NO_REBOOT;
    721       1.9   xtraeme 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah,
    722       1.9   xtraeme 		    LPCIB_GCS_OFFSET, status);
    723       1.9   xtraeme 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    724       1.9   xtraeme 		    LPCIB_GCS_OFFSET);
    725       1.6  jmcneill 		if (status & LPCIB_GCS_NO_REBOOT)
    726       1.6  jmcneill 			goto error;
    727       1.6  jmcneill 	} else {
    728       1.6  jmcneill 		pcireg_t pcireg;
    729       1.6  jmcneill 
    730      1.12    martin 		pcireg = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    731       1.1   xtraeme 				       LPCIB_PCI_GEN_STA);
    732       1.1   xtraeme 		if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT) {
    733       1.1   xtraeme 			/* TCO timeout reset is disabled; try to enable it */
    734       1.1   xtraeme 			pcireg &= ~LPCIB_PCI_GEN_STA_NO_REBOOT;
    735      1.12    martin 			pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    736       1.1   xtraeme 				       LPCIB_PCI_GEN_STA, pcireg);
    737       1.1   xtraeme 			if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT)
    738       1.1   xtraeme 				goto error;
    739       1.1   xtraeme 		}
    740       1.1   xtraeme 	}
    741       1.1   xtraeme 
    742       1.1   xtraeme 	return 0;
    743       1.1   xtraeme error:
    744       1.9   xtraeme 	aprint_error_dev(self, "TCO timer reboot disabled by hardware; "
    745       1.9   xtraeme 	    "hope SMBIOS properly handles it.\n");
    746       1.1   xtraeme 	return EINVAL;
    747       1.1   xtraeme }
    748       1.1   xtraeme 
    749       1.1   xtraeme 
    750       1.1   xtraeme /*
    751       1.1   xtraeme  * Intel ICH SpeedStep support.
    752       1.1   xtraeme  */
    753       1.1   xtraeme #define SS_READ(sc, reg) \
    754       1.1   xtraeme 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (reg))
    755       1.1   xtraeme #define SS_WRITE(sc, reg, val) \
    756       1.1   xtraeme 	bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
    757       1.1   xtraeme 
    758       1.1   xtraeme /*
    759       1.1   xtraeme  * Linux driver says that SpeedStep on older chipsets cause
    760       1.1   xtraeme  * lockups on Dell Inspiron 8000 and 8100.
    761  1.14.8.1       jym  * It should also not be enabled on systems with the 82855GM
    762  1.14.8.1       jym  * Hub, which typically have an EST-enabled CPU.
    763       1.1   xtraeme  */
    764       1.1   xtraeme static int
    765       1.1   xtraeme speedstep_bad_hb_check(struct pci_attach_args *pa)
    766       1.1   xtraeme {
    767       1.1   xtraeme 
    768       1.1   xtraeme 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82815_FULL_HUB &&
    769       1.1   xtraeme 	    PCI_REVISION(pa->pa_class) < 5)
    770       1.1   xtraeme 		return 1;
    771       1.1   xtraeme 
    772  1.14.8.1       jym 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82855GM_MCH)
    773  1.14.8.1       jym 		return 1;
    774  1.14.8.1       jym 
    775       1.1   xtraeme 	return 0;
    776       1.1   xtraeme }
    777       1.1   xtraeme 
    778       1.1   xtraeme static void
    779       1.9   xtraeme speedstep_configure(device_t self)
    780       1.1   xtraeme {
    781       1.9   xtraeme 	struct lpcib_softc *sc = device_private(self);
    782       1.1   xtraeme 	const struct sysctlnode	*node, *ssnode;
    783       1.1   xtraeme 	int rv;
    784       1.1   xtraeme 
    785       1.1   xtraeme 	/* Supported on ICH2-M, ICH3-M and ICH4-M.  */
    786       1.6  jmcneill 	if (PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801DB_ISA ||
    787       1.6  jmcneill 	    PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801CAM_LPC ||
    788       1.6  jmcneill 	    (PCI_PRODUCT(sc->sc_pa.pa_id) == PCI_PRODUCT_INTEL_82801BAM_LPC &&
    789       1.6  jmcneill 	     pci_find_device(&sc->sc_pa, speedstep_bad_hb_check) == 0)) {
    790  1.14.8.2       jym 		pcireg_t pmcon;
    791       1.1   xtraeme 
    792       1.1   xtraeme 		/* Enable SpeedStep if it isn't already enabled. */
    793      1.12    martin 		pmcon = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    794       1.1   xtraeme 				      LPCIB_PCI_GEN_PMCON_1);
    795       1.1   xtraeme 		if ((pmcon & LPCIB_PCI_GEN_PMCON_1_SS_EN) == 0)
    796      1.12    martin 			pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    797       1.1   xtraeme 				       LPCIB_PCI_GEN_PMCON_1,
    798       1.1   xtraeme 				       pmcon | LPCIB_PCI_GEN_PMCON_1_SS_EN);
    799       1.1   xtraeme 
    800       1.1   xtraeme 		/* Put in machdep.speedstep_state (0 for low, 1 for high). */
    801  1.14.8.2       jym 		if ((rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
    802       1.1   xtraeme 		    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    803       1.1   xtraeme 		    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
    804       1.1   xtraeme 			goto err;
    805       1.1   xtraeme 
    806       1.1   xtraeme 		/* CTLFLAG_ANYWRITE? kernel option like EST? */
    807  1.14.8.2       jym 		if ((rv = sysctl_createv(&sc->sc_log, 0, &node, &ssnode,
    808       1.1   xtraeme 		    CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
    809       1.1   xtraeme 		    speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
    810       1.1   xtraeme 		    CTL_EOL)) != 0)
    811       1.1   xtraeme 			goto err;
    812       1.1   xtraeme 
    813       1.1   xtraeme 		/* XXX save the sc for IO tag/handle */
    814       1.1   xtraeme 		speedstep_cookie = sc;
    815       1.9   xtraeme 		aprint_verbose_dev(self, "SpeedStep enabled\n");
    816       1.1   xtraeme 	}
    817       1.1   xtraeme 
    818       1.1   xtraeme 	return;
    819       1.1   xtraeme 
    820       1.1   xtraeme err:
    821       1.1   xtraeme 	aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
    822       1.1   xtraeme }
    823       1.1   xtraeme 
    824  1.14.8.2       jym static void
    825  1.14.8.2       jym speedstep_unconfigure(device_t self)
    826  1.14.8.2       jym {
    827  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
    828  1.14.8.2       jym 
    829  1.14.8.2       jym 	sysctl_teardown(&sc->sc_log);
    830  1.14.8.2       jym 	pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    831  1.14.8.2       jym 	    LPCIB_PCI_GEN_PMCON_1, sc->sc_pmcon_orig);
    832  1.14.8.2       jym 
    833  1.14.8.2       jym 	speedstep_cookie = NULL;
    834  1.14.8.2       jym }
    835  1.14.8.2       jym 
    836       1.1   xtraeme /*
    837       1.1   xtraeme  * get/set the SpeedStep state: 0 == low power, 1 == high power.
    838       1.1   xtraeme  */
    839       1.1   xtraeme static int
    840       1.1   xtraeme speedstep_sysctl_helper(SYSCTLFN_ARGS)
    841       1.1   xtraeme {
    842       1.1   xtraeme 	struct sysctlnode	node;
    843       1.1   xtraeme 	struct lpcib_softc 	*sc = speedstep_cookie;
    844       1.1   xtraeme 	uint8_t			state, state2;
    845       1.1   xtraeme 	int			ostate, nstate, s, error = 0;
    846       1.1   xtraeme 
    847       1.1   xtraeme 	/*
    848       1.1   xtraeme 	 * We do the dance with spl's to avoid being at high ipl during
    849       1.1   xtraeme 	 * sysctl_lookup() which can both copyin and copyout.
    850       1.1   xtraeme 	 */
    851       1.1   xtraeme 	s = splserial();
    852       1.1   xtraeme 	state = SS_READ(sc, LPCIB_PM_SS_CNTL);
    853       1.1   xtraeme 	splx(s);
    854       1.1   xtraeme 	if ((state & LPCIB_PM_SS_STATE_LOW) == 0)
    855       1.1   xtraeme 		ostate = 1;
    856       1.1   xtraeme 	else
    857       1.1   xtraeme 		ostate = 0;
    858       1.1   xtraeme 	nstate = ostate;
    859       1.1   xtraeme 
    860       1.1   xtraeme 	node = *rnode;
    861       1.1   xtraeme 	node.sysctl_data = &nstate;
    862       1.1   xtraeme 
    863       1.1   xtraeme 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    864       1.1   xtraeme 	if (error || newp == NULL)
    865       1.1   xtraeme 		goto out;
    866       1.1   xtraeme 
    867       1.1   xtraeme 	/* Only two states are available */
    868       1.1   xtraeme 	if (nstate != 0 && nstate != 1) {
    869       1.1   xtraeme 		error = EINVAL;
    870       1.1   xtraeme 		goto out;
    871       1.1   xtraeme 	}
    872       1.1   xtraeme 
    873       1.1   xtraeme 	s = splserial();
    874       1.1   xtraeme 	state2 = SS_READ(sc, LPCIB_PM_SS_CNTL);
    875       1.1   xtraeme 	if ((state2 & LPCIB_PM_SS_STATE_LOW) == 0)
    876       1.1   xtraeme 		ostate = 1;
    877       1.1   xtraeme 	else
    878       1.1   xtraeme 		ostate = 0;
    879       1.1   xtraeme 
    880       1.1   xtraeme 	if (ostate != nstate) {
    881       1.1   xtraeme 		uint8_t cntl;
    882       1.1   xtraeme 
    883       1.1   xtraeme 		if (nstate == 0)
    884       1.1   xtraeme 			state2 |= LPCIB_PM_SS_STATE_LOW;
    885       1.1   xtraeme 		else
    886       1.1   xtraeme 			state2 &= ~LPCIB_PM_SS_STATE_LOW;
    887       1.1   xtraeme 
    888       1.1   xtraeme 		/*
    889       1.1   xtraeme 		 * Must disable bus master arbitration during the change.
    890       1.1   xtraeme 		 */
    891       1.1   xtraeme 		cntl = SS_READ(sc, LPCIB_PM_CTRL);
    892       1.1   xtraeme 		SS_WRITE(sc, LPCIB_PM_CTRL, cntl | LPCIB_PM_SS_CNTL_ARB_DIS);
    893       1.1   xtraeme 		SS_WRITE(sc, LPCIB_PM_SS_CNTL, state2);
    894       1.1   xtraeme 		SS_WRITE(sc, LPCIB_PM_CTRL, cntl);
    895       1.1   xtraeme 	}
    896       1.1   xtraeme 	splx(s);
    897       1.1   xtraeme out:
    898       1.1   xtraeme 	return error;
    899       1.1   xtraeme }
    900       1.6  jmcneill 
    901       1.6  jmcneill #if NHPET > 0
    902       1.6  jmcneill struct lpcib_hpet_attach_arg {
    903       1.6  jmcneill 	bus_space_tag_t hpet_mem_t;
    904       1.6  jmcneill 	uint32_t hpet_reg;
    905       1.6  jmcneill };
    906       1.6  jmcneill 
    907       1.6  jmcneill static int
    908       1.9   xtraeme lpcib_hpet_match(device_t parent, cfdata_t match, void *aux)
    909       1.6  jmcneill {
    910       1.6  jmcneill 	struct lpcib_hpet_attach_arg *arg = aux;
    911       1.6  jmcneill 	bus_space_tag_t tag;
    912       1.6  jmcneill 	bus_space_handle_t handle;
    913       1.6  jmcneill 
    914       1.6  jmcneill 	tag = arg->hpet_mem_t;
    915       1.6  jmcneill 
    916       1.6  jmcneill 	if (bus_space_map(tag, arg->hpet_reg, HPET_WINDOW_SIZE, 0, &handle)) {
    917      1.10    cegger 		aprint_verbose_dev(parent, "HPET window not mapped, skipping\n");
    918       1.6  jmcneill 		return 0;
    919       1.6  jmcneill 	}
    920       1.6  jmcneill 	bus_space_unmap(tag, handle, HPET_WINDOW_SIZE);
    921       1.6  jmcneill 
    922       1.6  jmcneill 	return 1;
    923       1.6  jmcneill }
    924       1.6  jmcneill 
    925  1.14.8.2       jym static int
    926  1.14.8.2       jym lpcib_hpet_detach(device_t self, int flags)
    927  1.14.8.2       jym {
    928  1.14.8.2       jym 	struct hpet_softc *sc = device_private(self);
    929  1.14.8.2       jym 	int rc;
    930  1.14.8.2       jym 
    931  1.14.8.2       jym 	if ((rc = hpet_detach(self, flags)) != 0)
    932  1.14.8.2       jym 		return rc;
    933  1.14.8.2       jym 
    934  1.14.8.2       jym 	bus_space_unmap(sc->sc_memt, sc->sc_memh, HPET_WINDOW_SIZE);
    935  1.14.8.2       jym 
    936  1.14.8.2       jym 	return 0;
    937  1.14.8.2       jym }
    938  1.14.8.2       jym 
    939       1.6  jmcneill static void
    940       1.6  jmcneill lpcib_hpet_attach(device_t parent, device_t self, void *aux)
    941       1.6  jmcneill {
    942       1.6  jmcneill 	struct hpet_softc *sc = device_private(self);
    943       1.6  jmcneill 	struct lpcib_hpet_attach_arg *arg = aux;
    944       1.6  jmcneill 
    945       1.6  jmcneill 	aprint_naive("\n");
    946       1.6  jmcneill 	aprint_normal("\n");
    947       1.6  jmcneill 
    948       1.6  jmcneill 	sc->sc_memt = arg->hpet_mem_t;
    949       1.6  jmcneill 
    950       1.6  jmcneill 	if (bus_space_map(sc->sc_memt, arg->hpet_reg, HPET_WINDOW_SIZE, 0,
    951       1.6  jmcneill 			  &sc->sc_memh)) {
    952       1.9   xtraeme 		aprint_error_dev(self,
    953       1.9   xtraeme 		    "HPET memory window could not be mapped");
    954       1.6  jmcneill 		return;
    955       1.6  jmcneill 	}
    956       1.6  jmcneill 
    957       1.9   xtraeme 	hpet_attach_subr(self);
    958       1.6  jmcneill }
    959       1.6  jmcneill 
    960       1.9   xtraeme CFATTACH_DECL_NEW(ichlpcib_hpet, sizeof(struct hpet_softc), lpcib_hpet_match,
    961  1.14.8.2       jym     lpcib_hpet_attach, lpcib_hpet_detach, NULL);
    962       1.6  jmcneill 
    963       1.6  jmcneill static void
    964       1.9   xtraeme lpcib_hpet_configure(device_t self)
    965       1.6  jmcneill {
    966       1.9   xtraeme 	struct lpcib_softc *sc = device_private(self);
    967       1.6  jmcneill 	struct lpcib_hpet_attach_arg arg;
    968       1.6  jmcneill 	uint32_t hpet_reg, val;
    969       1.6  jmcneill 
    970       1.6  jmcneill 	if (sc->sc_has_ich5_hpet) {
    971      1.12    martin 		val = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    972       1.9   xtraeme 		    LPCIB_PCI_GEN_CNTL);
    973       1.6  jmcneill 		switch (val & LPCIB_ICH5_HPTC_WIN_MASK) {
    974       1.6  jmcneill 		case LPCIB_ICH5_HPTC_0000:
    975       1.6  jmcneill 			hpet_reg = LPCIB_ICH5_HPTC_0000_BASE;
    976       1.6  jmcneill 			break;
    977       1.6  jmcneill 		case LPCIB_ICH5_HPTC_1000:
    978       1.6  jmcneill 			hpet_reg = LPCIB_ICH5_HPTC_1000_BASE;
    979       1.6  jmcneill 			break;
    980       1.6  jmcneill 		case LPCIB_ICH5_HPTC_2000:
    981       1.6  jmcneill 			hpet_reg = LPCIB_ICH5_HPTC_2000_BASE;
    982       1.6  jmcneill 			break;
    983       1.6  jmcneill 		case LPCIB_ICH5_HPTC_3000:
    984       1.6  jmcneill 			hpet_reg = LPCIB_ICH5_HPTC_3000_BASE;
    985       1.6  jmcneill 			break;
    986       1.6  jmcneill 		default:
    987       1.6  jmcneill 			return;
    988       1.6  jmcneill 		}
    989       1.6  jmcneill 		val |= sc->sc_hpet_reg | LPCIB_ICH5_HPTC_EN;
    990      1.12    martin 		pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
    991       1.9   xtraeme 		    LPCIB_PCI_GEN_CNTL, val);
    992       1.6  jmcneill 	} else if (sc->sc_has_rcba) {
    993       1.6  jmcneill 		val = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    994       1.6  jmcneill 		    LPCIB_RCBA_HPTC);
    995       1.6  jmcneill 		switch (val & LPCIB_RCBA_HPTC_WIN_MASK) {
    996       1.6  jmcneill 		case LPCIB_RCBA_HPTC_0000:
    997       1.6  jmcneill 			hpet_reg = LPCIB_RCBA_HPTC_0000_BASE;
    998       1.6  jmcneill 			break;
    999       1.6  jmcneill 		case LPCIB_RCBA_HPTC_1000:
   1000       1.6  jmcneill 			hpet_reg = LPCIB_RCBA_HPTC_1000_BASE;
   1001       1.6  jmcneill 			break;
   1002       1.6  jmcneill 		case LPCIB_RCBA_HPTC_2000:
   1003       1.6  jmcneill 			hpet_reg = LPCIB_RCBA_HPTC_2000_BASE;
   1004       1.6  jmcneill 			break;
   1005       1.6  jmcneill 		case LPCIB_RCBA_HPTC_3000:
   1006       1.6  jmcneill 			hpet_reg = LPCIB_RCBA_HPTC_3000_BASE;
   1007       1.6  jmcneill 			break;
   1008       1.6  jmcneill 		default:
   1009       1.6  jmcneill 			return;
   1010       1.6  jmcneill 		}
   1011       1.6  jmcneill 		val |= LPCIB_RCBA_HPTC_EN;
   1012       1.6  jmcneill 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah, LPCIB_RCBA_HPTC,
   1013       1.6  jmcneill 		    val);
   1014       1.6  jmcneill 	} else {
   1015       1.6  jmcneill 		/* No HPET here */
   1016       1.6  jmcneill 		return;
   1017       1.6  jmcneill 	}
   1018       1.6  jmcneill 
   1019       1.6  jmcneill 	arg.hpet_mem_t = sc->sc_pa.pa_memt;
   1020       1.6  jmcneill 	arg.hpet_reg = hpet_reg;
   1021       1.6  jmcneill 
   1022  1.14.8.2       jym 	sc->sc_hpetbus = config_found_ia(self, "hpetichbus", &arg, NULL);
   1023  1.14.8.2       jym }
   1024  1.14.8.2       jym 
   1025  1.14.8.2       jym static int
   1026  1.14.8.2       jym lpcib_hpet_unconfigure(device_t self, int flags)
   1027  1.14.8.2       jym {
   1028  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
   1029  1.14.8.2       jym 	int rc;
   1030  1.14.8.2       jym 
   1031  1.14.8.2       jym 	if (sc->sc_hpetbus != NULL &&
   1032  1.14.8.2       jym 	    (rc = config_detach(sc->sc_hpetbus, flags)) != 0)
   1033  1.14.8.2       jym 		return rc;
   1034  1.14.8.2       jym 
   1035  1.14.8.2       jym 	return 0;
   1036  1.14.8.2       jym }
   1037  1.14.8.2       jym #endif
   1038  1.14.8.2       jym 
   1039  1.14.8.2       jym #if NGPIO > 0
   1040  1.14.8.2       jym static void
   1041  1.14.8.2       jym lpcib_gpio_configure(device_t self)
   1042  1.14.8.2       jym {
   1043  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
   1044  1.14.8.2       jym 	struct gpiobus_attach_args gba;
   1045  1.14.8.2       jym 	pcireg_t gpio_cntl;
   1046  1.14.8.2       jym 	uint32_t use, io, bit;
   1047  1.14.8.2       jym 	int pin, shift, base_reg, cntl_reg, reg;
   1048  1.14.8.2       jym 
   1049  1.14.8.2       jym 	/* this implies ICH >= 6, and thus different mapreg */
   1050  1.14.8.2       jym 	if (sc->sc_has_rcba) {
   1051  1.14.8.2       jym 		base_reg = LPCIB_PCI_GPIO_BASE_ICH6;
   1052  1.14.8.2       jym 		cntl_reg = LPCIB_PCI_GPIO_CNTL_ICH6;
   1053  1.14.8.2       jym 	} else {
   1054  1.14.8.2       jym 		base_reg = LPCIB_PCI_GPIO_BASE;
   1055  1.14.8.2       jym 		cntl_reg = LPCIB_PCI_GPIO_CNTL;
   1056  1.14.8.2       jym 	}
   1057  1.14.8.2       jym 
   1058  1.14.8.2       jym 	gpio_cntl = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
   1059  1.14.8.2       jym 				  cntl_reg);
   1060  1.14.8.2       jym 
   1061  1.14.8.2       jym 	/* Is GPIO enabled? */
   1062  1.14.8.2       jym 	if ((gpio_cntl & LPCIB_PCI_GPIO_CNTL_EN) == 0)
   1063  1.14.8.2       jym 		return;
   1064  1.14.8.2       jym 
   1065  1.14.8.2       jym 	if (pci_mapreg_map(&sc->sc_pa, base_reg, PCI_MAPREG_TYPE_IO, 0,
   1066  1.14.8.2       jym 			   &sc->sc_gpio_iot, &sc->sc_gpio_ioh,
   1067  1.14.8.2       jym 			   NULL, &sc->sc_gpio_ios)) {
   1068  1.14.8.2       jym 		aprint_error_dev(self, "can't map general purpose i/o space\n");
   1069  1.14.8.2       jym 		return;
   1070  1.14.8.2       jym 	}
   1071  1.14.8.2       jym 
   1072  1.14.8.2       jym 	mutex_init(&sc->sc_gpio_mtx, MUTEX_DEFAULT, IPL_NONE);
   1073  1.14.8.2       jym 
   1074  1.14.8.2       jym 	for (pin = 0; pin < LPCIB_GPIO_NPINS; pin++) {
   1075  1.14.8.2       jym 		sc->sc_gpio_pins[pin].pin_num = pin;
   1076  1.14.8.2       jym 
   1077  1.14.8.2       jym 		/* Read initial state */
   1078  1.14.8.2       jym 		reg = (pin < 32) ? LPCIB_GPIO_GPIO_USE_SEL : LPCIB_GPIO_GPIO_USE_SEL2;
   1079  1.14.8.2       jym 		use = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
   1080  1.14.8.2       jym 		reg = (pin < 32) ? LPCIB_GPIO_GP_IO_SEL : LPCIB_GPIO_GP_IO_SEL;
   1081  1.14.8.2       jym 		io = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, 4);
   1082  1.14.8.2       jym 		shift = pin % 32;
   1083  1.14.8.2       jym 		bit = __BIT(shift);
   1084  1.14.8.2       jym 
   1085  1.14.8.2       jym 		if ((use & bit) != 0) {
   1086  1.14.8.2       jym 			sc->sc_gpio_pins[pin].pin_caps =
   1087  1.14.8.2       jym 			    GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
   1088  1.14.8.2       jym 			if (pin < 32)
   1089  1.14.8.2       jym 				sc->sc_gpio_pins[pin].pin_caps |=
   1090  1.14.8.2       jym 				    GPIO_PIN_PULSATE;
   1091  1.14.8.2       jym 			if ((io & bit) != 0)
   1092  1.14.8.2       jym 				sc->sc_gpio_pins[pin].pin_flags =
   1093  1.14.8.2       jym 				    GPIO_PIN_INPUT;
   1094  1.14.8.2       jym 			else
   1095  1.14.8.2       jym 				sc->sc_gpio_pins[pin].pin_flags =
   1096  1.14.8.2       jym 				    GPIO_PIN_OUTPUT;
   1097  1.14.8.2       jym 		} else
   1098  1.14.8.2       jym 			sc->sc_gpio_pins[pin].pin_caps = 0;
   1099  1.14.8.2       jym 
   1100  1.14.8.2       jym 		if (lpcib_gpio_pin_read(sc, pin) == 0)
   1101  1.14.8.2       jym 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_LOW;
   1102  1.14.8.2       jym 		else
   1103  1.14.8.2       jym 			sc->sc_gpio_pins[pin].pin_state = GPIO_PIN_HIGH;
   1104  1.14.8.2       jym 
   1105  1.14.8.2       jym 	}
   1106  1.14.8.2       jym 
   1107  1.14.8.2       jym 	/* Create controller tag */
   1108  1.14.8.2       jym 	sc->sc_gpio_gc.gp_cookie = sc;
   1109  1.14.8.2       jym 	sc->sc_gpio_gc.gp_pin_read = lpcib_gpio_pin_read;
   1110  1.14.8.2       jym 	sc->sc_gpio_gc.gp_pin_write = lpcib_gpio_pin_write;
   1111  1.14.8.2       jym 	sc->sc_gpio_gc.gp_pin_ctl = lpcib_gpio_pin_ctl;
   1112  1.14.8.2       jym 
   1113  1.14.8.2       jym 	memset(&gba, 0, sizeof(gba));
   1114  1.14.8.2       jym 
   1115  1.14.8.2       jym 	gba.gba_gc = &sc->sc_gpio_gc;
   1116  1.14.8.2       jym 	gba.gba_pins = sc->sc_gpio_pins;
   1117  1.14.8.2       jym 	gba.gba_npins = LPCIB_GPIO_NPINS;
   1118  1.14.8.2       jym 
   1119  1.14.8.2       jym 	sc->sc_gpiobus = config_found_ia(self, "gpiobus", &gba, gpiobus_print);
   1120  1.14.8.2       jym }
   1121  1.14.8.2       jym 
   1122  1.14.8.2       jym static int
   1123  1.14.8.2       jym lpcib_gpio_unconfigure(device_t self, int flags)
   1124  1.14.8.2       jym {
   1125  1.14.8.2       jym 	struct lpcib_softc *sc = device_private(self);
   1126  1.14.8.2       jym 	int rc;
   1127  1.14.8.2       jym 
   1128  1.14.8.2       jym 	if (sc->sc_gpiobus != NULL &&
   1129  1.14.8.2       jym 	    (rc = config_detach(sc->sc_gpiobus, flags)) != 0)
   1130  1.14.8.2       jym 		return rc;
   1131  1.14.8.2       jym 
   1132  1.14.8.2       jym 	mutex_destroy(&sc->sc_gpio_mtx);
   1133  1.14.8.2       jym 
   1134  1.14.8.2       jym 	bus_space_unmap(sc->sc_gpio_iot, sc->sc_gpio_ioh, sc->sc_gpio_ios);
   1135  1.14.8.2       jym 
   1136  1.14.8.2       jym 	return 0;
   1137  1.14.8.2       jym }
   1138  1.14.8.2       jym 
   1139  1.14.8.2       jym static int
   1140  1.14.8.2       jym lpcib_gpio_pin_read(void *arg, int pin)
   1141  1.14.8.2       jym {
   1142  1.14.8.2       jym 	struct lpcib_softc *sc = arg;
   1143  1.14.8.2       jym 	uint32_t data;
   1144  1.14.8.2       jym 	int reg, shift;
   1145  1.14.8.2       jym 
   1146  1.14.8.2       jym 	reg = (pin < 32) ? LPCIB_GPIO_GP_LVL : LPCIB_GPIO_GP_LVL2;
   1147  1.14.8.2       jym 	shift = pin % 32;
   1148  1.14.8.2       jym 
   1149  1.14.8.2       jym 	mutex_enter(&sc->sc_gpio_mtx);
   1150  1.14.8.2       jym 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
   1151  1.14.8.2       jym 	mutex_exit(&sc->sc_gpio_mtx);
   1152  1.14.8.2       jym 
   1153  1.14.8.2       jym 	return (__SHIFTOUT(data, __BIT(shift)) ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
   1154  1.14.8.2       jym }
   1155  1.14.8.2       jym 
   1156  1.14.8.2       jym static void
   1157  1.14.8.2       jym lpcib_gpio_pin_write(void *arg, int pin, int value)
   1158  1.14.8.2       jym {
   1159  1.14.8.2       jym 	struct lpcib_softc *sc = arg;
   1160  1.14.8.2       jym 	uint32_t data;
   1161  1.14.8.2       jym 	int reg, shift;
   1162  1.14.8.2       jym 
   1163  1.14.8.2       jym 	reg = (pin < 32) ? LPCIB_GPIO_GP_LVL : LPCIB_GPIO_GP_LVL2;
   1164  1.14.8.2       jym 	shift = pin % 32;
   1165  1.14.8.2       jym 
   1166  1.14.8.2       jym 	mutex_enter(&sc->sc_gpio_mtx);
   1167  1.14.8.2       jym 
   1168  1.14.8.2       jym 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
   1169  1.14.8.2       jym 
   1170  1.14.8.2       jym 	if(value)
   1171  1.14.8.2       jym 		data |= __BIT(shift);
   1172  1.14.8.2       jym 	else
   1173  1.14.8.2       jym 		data &= ~__BIT(shift);
   1174  1.14.8.2       jym 
   1175  1.14.8.2       jym 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
   1176  1.14.8.2       jym 
   1177  1.14.8.2       jym 	mutex_exit(&sc->sc_gpio_mtx);
   1178  1.14.8.2       jym }
   1179  1.14.8.2       jym 
   1180  1.14.8.2       jym static void
   1181  1.14.8.2       jym lpcib_gpio_pin_ctl(void *arg, int pin, int flags)
   1182  1.14.8.2       jym {
   1183  1.14.8.2       jym 	struct lpcib_softc *sc = arg;
   1184  1.14.8.2       jym 	uint32_t data;
   1185  1.14.8.2       jym 	int reg, shift;
   1186  1.14.8.2       jym 
   1187  1.14.8.2       jym 	shift = pin % 32;
   1188  1.14.8.2       jym 	reg = (pin < 32) ? LPCIB_GPIO_GP_IO_SEL : LPCIB_GPIO_GP_IO_SEL2;
   1189  1.14.8.2       jym 
   1190  1.14.8.2       jym 	mutex_enter(&sc->sc_gpio_mtx);
   1191  1.14.8.2       jym 
   1192  1.14.8.2       jym 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
   1193  1.14.8.2       jym 
   1194  1.14.8.2       jym 	if (flags & GPIO_PIN_OUTPUT)
   1195  1.14.8.2       jym 		data &= ~__BIT(shift);
   1196  1.14.8.2       jym 
   1197  1.14.8.2       jym 	if (flags & GPIO_PIN_INPUT)
   1198  1.14.8.2       jym 		data |= __BIT(shift);
   1199  1.14.8.2       jym 
   1200  1.14.8.2       jym 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
   1201  1.14.8.2       jym 
   1202  1.14.8.2       jym 
   1203  1.14.8.2       jym 	if (pin < 32) {
   1204  1.14.8.2       jym 		reg = LPCIB_GPIO_GPO_BLINK;
   1205  1.14.8.2       jym 		data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
   1206  1.14.8.2       jym 
   1207  1.14.8.2       jym 		if (flags & GPIO_PIN_PULSATE)
   1208  1.14.8.2       jym 			data |= __BIT(shift);
   1209  1.14.8.2       jym 		else
   1210  1.14.8.2       jym 			data &= ~__BIT(shift);
   1211  1.14.8.2       jym 
   1212  1.14.8.2       jym 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
   1213  1.14.8.2       jym 	}
   1214  1.14.8.2       jym 
   1215  1.14.8.2       jym 	mutex_exit(&sc->sc_gpio_mtx);
   1216       1.6  jmcneill }
   1217       1.6  jmcneill #endif
   1218