Home | History | Annotate | Line # | Download | only in pci
piixpm.c revision 1.60
      1  1.60   thorpej /* $NetBSD: piixpm.c,v 1.60 2019/12/24 06:27:17 thorpej Exp $ */
      2  1.54   msaitoh /*	$OpenBSD: piixpm.c,v 1.39 2013/10/01 20:06:02 sf Exp $	*/
      3   1.1  jmcneill 
      4   1.1  jmcneill /*
      5   1.1  jmcneill  * Copyright (c) 2005, 2006 Alexander Yurchenko <grange (at) openbsd.org>
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Permission to use, copy, modify, and distribute this software for any
      8   1.1  jmcneill  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  jmcneill  * copyright notice and this permission notice appear in all copies.
     10   1.1  jmcneill  *
     11   1.1  jmcneill  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  jmcneill  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  jmcneill  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  jmcneill  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  jmcneill  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  jmcneill  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  jmcneill  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  jmcneill  */
     19   1.1  jmcneill 
     20   1.1  jmcneill /*
     21   1.1  jmcneill  * Intel PIIX and compatible Power Management controller driver.
     22   1.1  jmcneill  */
     23   1.1  jmcneill 
     24  1.19     lukem #include <sys/cdefs.h>
     25  1.60   thorpej __KERNEL_RCSID(0, "$NetBSD: piixpm.c,v 1.60 2019/12/24 06:27:17 thorpej Exp $");
     26  1.19     lukem 
     27   1.1  jmcneill #include <sys/param.h>
     28   1.1  jmcneill #include <sys/systm.h>
     29   1.1  jmcneill #include <sys/device.h>
     30   1.1  jmcneill #include <sys/kernel.h>
     31  1.40  pgoyette #include <sys/mutex.h>
     32  1.60   thorpej #include <sys/condvar.h>
     33   1.1  jmcneill #include <sys/proc.h>
     34   1.1  jmcneill 
     35  1.17        ad #include <sys/bus.h>
     36   1.1  jmcneill 
     37   1.1  jmcneill #include <dev/pci/pcidevs.h>
     38   1.1  jmcneill #include <dev/pci/pcireg.h>
     39   1.1  jmcneill #include <dev/pci/pcivar.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/pci/piixpmreg.h>
     42   1.1  jmcneill 
     43   1.1  jmcneill #include <dev/i2c/i2cvar.h>
     44   1.1  jmcneill 
     45   1.5  drochner #include <dev/ic/acpipmtimer.h>
     46   1.4  jmcneill 
     47   1.1  jmcneill #ifdef PIIXPM_DEBUG
     48   1.1  jmcneill #define DPRINTF(x) printf x
     49   1.1  jmcneill #else
     50   1.1  jmcneill #define DPRINTF(x)
     51   1.1  jmcneill #endif
     52   1.1  jmcneill 
     53  1.54   msaitoh #define PIIXPM_IS_CSB5(sc)						      \
     54  1.54   msaitoh 	(PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_SERVERWORKS &&		      \
     55  1.54   msaitoh 	PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_SERVERWORKS_CSB5)
     56   1.1  jmcneill #define PIIXPM_DELAY	200
     57   1.1  jmcneill #define PIIXPM_TIMEOUT	1
     58   1.1  jmcneill 
     59  1.54   msaitoh #define PIIXPM_IS_SB800GRP(sc)						      \
     60  1.54   msaitoh 	((PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_ATI) &&			      \
     61  1.54   msaitoh 	    ((PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_ATI_SB600_SMB) &&	      \
     62  1.54   msaitoh 		((sc)->sc_rev >= 0x40)))
     63  1.54   msaitoh 
     64  1.54   msaitoh #define PIIXPM_IS_HUDSON(sc)						      \
     65  1.54   msaitoh 	((PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_AMD) &&			      \
     66  1.54   msaitoh 	    (PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_AMD_HUDSON_SMB))
     67  1.54   msaitoh 
     68  1.54   msaitoh #define PIIXPM_IS_KERNCZ(sc)						      \
     69  1.54   msaitoh 	((PCI_VENDOR((sc)->sc_id) == PCI_VENDOR_AMD) &&			      \
     70  1.54   msaitoh 	    (PCI_PRODUCT((sc)->sc_id) == PCI_PRODUCT_AMD_KERNCZ_SMB))
     71  1.54   msaitoh 
     72  1.54   msaitoh #define PIIXPM_IS_FCHGRP(sc)	(PIIXPM_IS_HUDSON(sc) || PIIXPM_IS_KERNCZ(sc))
     73  1.54   msaitoh 
     74  1.42     soren struct piixpm_smbus {
     75  1.42     soren 	int			sda;
     76  1.42     soren 	struct			piixpm_softc *softc;
     77  1.42     soren };
     78  1.36  jmcneill 
     79   1.1  jmcneill struct piixpm_softc {
     80  1.25     joerg 	device_t		sc_dev;
     81   1.1  jmcneill 
     82  1.42     soren 	bus_space_tag_t		sc_iot;
     83  1.42     soren #define	sc_pm_iot sc_iot
     84  1.42     soren #define sc_smb_iot sc_iot
     85  1.42     soren 	bus_space_handle_t	sc_pm_ioh;
     86  1.42     soren 	bus_space_handle_t	sc_sb800_ioh;
     87   1.4  jmcneill 	bus_space_handle_t	sc_smb_ioh;
     88   1.4  jmcneill 	void *			sc_smb_ih;
     89   1.1  jmcneill 	int			sc_poll;
     90  1.59   msaitoh 	bool			sc_sb800_selen; /* Use SMBUS0SEL */
     91   1.1  jmcneill 
     92   1.3  jmcneill 	pci_chipset_tag_t	sc_pc;
     93   1.3  jmcneill 	pcitag_t		sc_pcitag;
     94  1.35   hannken 	pcireg_t		sc_id;
     95  1.54   msaitoh 	pcireg_t		sc_rev;
     96   1.3  jmcneill 
     97  1.46  pgoyette 	int			sc_numbusses;
     98  1.46  pgoyette 	device_t		sc_i2c_device[4];
     99  1.42     soren 	struct piixpm_smbus	sc_busses[4];
    100  1.42     soren 	struct i2c_controller	sc_i2c_tags[4];
    101  1.42     soren 
    102  1.60   thorpej 	kmutex_t		sc_exec_lock;
    103  1.60   thorpej 	kcondvar_t		sc_exec_wait;
    104  1.60   thorpej 
    105   1.1  jmcneill 	struct {
    106  1.42     soren 		i2c_op_t	op;
    107  1.42     soren 		void *		buf;
    108  1.42     soren 		size_t		len;
    109  1.42     soren 		int		flags;
    110  1.60   thorpej 		int             error;
    111  1.60   thorpej 		bool            done;
    112   1.1  jmcneill 	}			sc_i2c_xfer;
    113   1.3  jmcneill 
    114   1.3  jmcneill 	pcireg_t		sc_devact[2];
    115   1.1  jmcneill };
    116   1.1  jmcneill 
    117  1.25     joerg static int	piixpm_match(device_t, cfdata_t, void *);
    118  1.25     joerg static void	piixpm_attach(device_t, device_t, void *);
    119  1.46  pgoyette static int	piixpm_rescan(device_t, const char *, const int *);
    120  1.46  pgoyette static void	piixpm_chdet(device_t, device_t);
    121   1.1  jmcneill 
    122  1.32    dyoung static bool	piixpm_suspend(device_t, const pmf_qual_t *);
    123  1.32    dyoung static bool	piixpm_resume(device_t, const pmf_qual_t *);
    124   1.3  jmcneill 
    125  1.42     soren static int	piixpm_sb800_init(struct piixpm_softc *);
    126  1.35   hannken static void	piixpm_csb5_reset(void *);
    127  1.25     joerg static int	piixpm_i2c_acquire_bus(void *, int);
    128  1.25     joerg static void	piixpm_i2c_release_bus(void *, int);
    129  1.25     joerg static int	piixpm_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
    130  1.25     joerg     size_t, void *, size_t, int);
    131   1.1  jmcneill 
    132  1.25     joerg static int	piixpm_intr(void *);
    133   1.1  jmcneill 
    134  1.46  pgoyette CFATTACH_DECL3_NEW(piixpm, sizeof(struct piixpm_softc),
    135  1.46  pgoyette     piixpm_match, piixpm_attach, NULL, NULL, piixpm_rescan, piixpm_chdet, 0);
    136   1.1  jmcneill 
    137  1.25     joerg static int
    138  1.25     joerg piixpm_match(device_t parent, cfdata_t match, void *aux)
    139   1.1  jmcneill {
    140   1.1  jmcneill 	struct pci_attach_args *pa;
    141   1.1  jmcneill 
    142   1.1  jmcneill 	pa = (struct pci_attach_args *)aux;
    143   1.1  jmcneill 	switch (PCI_VENDOR(pa->pa_id)) {
    144   1.1  jmcneill 	case PCI_VENDOR_INTEL:
    145   1.1  jmcneill 		switch (PCI_PRODUCT(pa->pa_id)) {
    146   1.1  jmcneill 		case PCI_PRODUCT_INTEL_82371AB_PMC:
    147   1.1  jmcneill 		case PCI_PRODUCT_INTEL_82440MX_PMC:
    148   1.1  jmcneill 			return 1;
    149   1.1  jmcneill 		}
    150   1.1  jmcneill 		break;
    151   1.1  jmcneill 	case PCI_VENDOR_ATI:
    152   1.1  jmcneill 		switch (PCI_PRODUCT(pa->pa_id)) {
    153   1.1  jmcneill 		case PCI_PRODUCT_ATI_SB200_SMB:
    154  1.10    toshii 		case PCI_PRODUCT_ATI_SB300_SMB:
    155  1.10    toshii 		case PCI_PRODUCT_ATI_SB400_SMB:
    156  1.23  jmcneill 		case PCI_PRODUCT_ATI_SB600_SMB:	/* matches SB600/SB700/SB800 */
    157   1.1  jmcneill 			return 1;
    158   1.1  jmcneill 		}
    159   1.1  jmcneill 		break;
    160  1.14    martin 	case PCI_VENDOR_SERVERWORKS:
    161  1.14    martin 		switch (PCI_PRODUCT(pa->pa_id)) {
    162  1.14    martin 		case PCI_PRODUCT_SERVERWORKS_OSB4:
    163  1.14    martin 		case PCI_PRODUCT_SERVERWORKS_CSB5:
    164  1.14    martin 		case PCI_PRODUCT_SERVERWORKS_CSB6:
    165  1.14    martin 		case PCI_PRODUCT_SERVERWORKS_HT1000SB:
    166  1.53   msaitoh 		case PCI_PRODUCT_SERVERWORKS_HT1100SB:
    167  1.14    martin 			return 1;
    168  1.14    martin 		}
    169  1.50  pgoyette 		break;
    170  1.48  pgoyette 	case PCI_VENDOR_AMD:
    171  1.48  pgoyette 		switch (PCI_PRODUCT(pa->pa_id)) {
    172  1.48  pgoyette 		case PCI_PRODUCT_AMD_HUDSON_SMB:
    173  1.54   msaitoh 		case PCI_PRODUCT_AMD_KERNCZ_SMB:
    174  1.48  pgoyette 			return 1;
    175  1.48  pgoyette 		}
    176  1.50  pgoyette 		break;
    177   1.1  jmcneill 	}
    178   1.1  jmcneill 
    179   1.1  jmcneill 	return 0;
    180   1.1  jmcneill }
    181   1.1  jmcneill 
    182  1.25     joerg static void
    183  1.25     joerg piixpm_attach(device_t parent, device_t self, void *aux)
    184   1.1  jmcneill {
    185  1.25     joerg 	struct piixpm_softc *sc = device_private(self);
    186   1.1  jmcneill 	struct pci_attach_args *pa = aux;
    187   1.1  jmcneill 	pcireg_t base, conf;
    188   1.5  drochner 	pcireg_t pmmisc;
    189   1.1  jmcneill 	pci_intr_handle_t ih;
    190  1.54   msaitoh 	bool usesmi = false;
    191   1.1  jmcneill 	const char *intrstr = NULL;
    192  1.46  pgoyette 	int i, flags;
    193  1.44  christos 	char intrbuf[PCI_INTRSTR_LEN];
    194   1.1  jmcneill 
    195  1.25     joerg 	sc->sc_dev = self;
    196  1.42     soren 	sc->sc_iot = pa->pa_iot;
    197  1.35   hannken 	sc->sc_id = pa->pa_id;
    198  1.54   msaitoh 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    199   1.3  jmcneill 	sc->sc_pc = pa->pa_pc;
    200   1.3  jmcneill 	sc->sc_pcitag = pa->pa_tag;
    201  1.46  pgoyette 	sc->sc_numbusses = 1;
    202   1.3  jmcneill 
    203  1.39  drochner 	pci_aprint_devinfo(pa, NULL);
    204   1.3  jmcneill 
    205  1.60   thorpej 	mutex_init(&sc->sc_exec_lock, MUTEX_DEFAULT, IPL_BIO);
    206  1.60   thorpej 	cv_init(&sc->sc_exec_wait, device_xname(self));
    207  1.60   thorpej 
    208  1.18  jmcneill 	if (!pmf_device_register(self, piixpm_suspend, piixpm_resume))
    209  1.18  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    210   1.3  jmcneill 
    211   1.5  drochner 	if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) ||
    212   1.5  drochner 	    (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_INTEL_82371AB_PMC))
    213   1.5  drochner 		goto nopowermanagement;
    214   1.5  drochner 
    215   1.5  drochner 	/* check whether I/O access to PM regs is enabled */
    216   1.5  drochner 	pmmisc = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PMREGMISC);
    217   1.5  drochner 	if (!(pmmisc & 1))
    218   1.5  drochner 		goto nopowermanagement;
    219   1.5  drochner 
    220   1.4  jmcneill 	/* Map I/O space */
    221   1.5  drochner 	base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_PM_BASE);
    222  1.53   msaitoh 	if (base == 0 || bus_space_map(sc->sc_pm_iot, PCI_MAPREG_IO_ADDR(base),
    223   1.4  jmcneill 	    PIIX_PM_SIZE, 0, &sc->sc_pm_ioh)) {
    224  1.49   msaitoh 		aprint_error_dev(self,
    225  1.49   msaitoh 		    "can't map power management I/O space\n");
    226   1.4  jmcneill 		goto nopowermanagement;
    227   1.4  jmcneill 	}
    228   1.4  jmcneill 
    229   1.5  drochner 	/*
    230   1.5  drochner 	 * Revision 0 and 1 are PIIX4, 2 is PIIX4E, 3 is PIIX4M.
    231   1.5  drochner 	 * PIIX4 and PIIX4E have a bug in the timer latch, see Errata #20
    232   1.5  drochner 	 * in the "Specification update" (document #297738).
    233   1.5  drochner 	 */
    234  1.54   msaitoh 	acpipmtimer_attach(self, sc->sc_pm_iot, sc->sc_pm_ioh, PIIX_PM_PMTMR,
    235  1.54   msaitoh 	    (PCI_REVISION(pa->pa_class) < 3) ? ACPIPMT_BADLATCH : 0);
    236   1.4  jmcneill 
    237   1.5  drochner nopowermanagement:
    238  1.36  jmcneill 
    239  1.54   msaitoh 	/* SB800 rev 0x40+, AMD HUDSON and newer need special initialization */
    240  1.54   msaitoh 	if (PIIXPM_IS_FCHGRP(sc) || PIIXPM_IS_SB800GRP(sc)) {
    241  1.42     soren 		if (piixpm_sb800_init(sc) == 0) {
    242  1.54   msaitoh 			/* Read configuration */
    243  1.58   msaitoh 			conf = bus_space_read_1(sc->sc_iot,
    244  1.58   msaitoh 			    sc->sc_smb_ioh, SB800_SMB_HOSTC);
    245  1.58   msaitoh 			usesmi = ((conf & SB800_SMB_HOSTC_IRQ) == 0);
    246  1.54   msaitoh 			goto setintr;
    247  1.42     soren 		}
    248  1.48  pgoyette 		aprint_normal_dev(self, "SMBus initialization failed\n");
    249  1.36  jmcneill 		return;
    250  1.36  jmcneill 	}
    251  1.36  jmcneill 
    252  1.54   msaitoh 	/* Read configuration */
    253  1.54   msaitoh 	conf = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_HOSTC);
    254  1.54   msaitoh 	DPRINTF(("%s: conf 0x%08x\n", device_xname(self), conf));
    255  1.54   msaitoh 
    256   1.1  jmcneill 	if ((conf & PIIX_SMB_HOSTC_HSTEN) == 0) {
    257  1.25     joerg 		aprint_normal_dev(self, "SMBus disabled\n");
    258   1.1  jmcneill 		return;
    259   1.1  jmcneill 	}
    260  1.54   msaitoh 	usesmi = (conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_SMI;
    261   1.1  jmcneill 
    262   1.1  jmcneill 	/* Map I/O space */
    263   1.1  jmcneill 	base = pci_conf_read(pa->pa_pc, pa->pa_tag, PIIX_SMB_BASE) & 0xffff;
    264  1.54   msaitoh 	if (base == 0 ||
    265  1.54   msaitoh 	    bus_space_map(sc->sc_smb_iot, PCI_MAPREG_IO_ADDR(base),
    266   1.4  jmcneill 	    PIIX_SMB_SIZE, 0, &sc->sc_smb_ioh)) {
    267  1.25     joerg 		aprint_error_dev(self, "can't map smbus I/O space\n");
    268   1.1  jmcneill 		return;
    269   1.1  jmcneill 	}
    270   1.1  jmcneill 
    271  1.54   msaitoh setintr:
    272   1.1  jmcneill 	sc->sc_poll = 1;
    273  1.28  pgoyette 	aprint_normal_dev(self, "");
    274  1.54   msaitoh 	if (usesmi) {
    275   1.1  jmcneill 		/* No PCI IRQ */
    276  1.28  pgoyette 		aprint_normal("interrupting at SMI, ");
    277  1.53   msaitoh 	} else {
    278  1.53   msaitoh 		if ((conf & PIIX_SMB_HOSTC_INTMASK) == PIIX_SMB_HOSTC_IRQ) {
    279  1.53   msaitoh 			/* Install interrupt handler */
    280  1.53   msaitoh 			if (pci_intr_map(pa, &ih) == 0) {
    281  1.53   msaitoh 				intrstr = pci_intr_string(pa->pa_pc, ih,
    282  1.53   msaitoh 				    intrbuf, sizeof(intrbuf));
    283  1.60   thorpej 				pci_intr_setattr(pa->pa_pc, &ih,
    284  1.60   thorpej 				    PCI_INTR_MPSAFE, true);
    285  1.53   msaitoh 				sc->sc_smb_ih = pci_intr_establish_xname(
    286  1.53   msaitoh 					pa->pa_pc, ih, IPL_BIO, piixpm_intr,
    287  1.53   msaitoh 					sc, device_xname(sc->sc_dev));
    288  1.53   msaitoh 				if (sc->sc_smb_ih != NULL) {
    289  1.53   msaitoh 					aprint_normal("interrupting at %s",
    290  1.53   msaitoh 					    intrstr);
    291  1.53   msaitoh 					sc->sc_poll = 0;
    292  1.53   msaitoh 				}
    293   1.1  jmcneill 			}
    294   1.1  jmcneill 		}
    295  1.53   msaitoh 		if (sc->sc_poll)
    296  1.53   msaitoh 			aprint_normal("polling");
    297   1.1  jmcneill 	}
    298   1.1  jmcneill 
    299   1.3  jmcneill 	aprint_normal("\n");
    300   1.1  jmcneill 
    301  1.46  pgoyette 	for (i = 0; i < sc->sc_numbusses; i++)
    302  1.46  pgoyette 		sc->sc_i2c_device[i] = NULL;
    303  1.46  pgoyette 
    304  1.46  pgoyette 	flags = 0;
    305  1.46  pgoyette 	piixpm_rescan(self, "i2cbus", &flags);
    306  1.46  pgoyette }
    307  1.46  pgoyette 
    308  1.46  pgoyette static int
    309  1.54   msaitoh piixpm_iicbus_print(void *aux, const char *pnp)
    310  1.54   msaitoh {
    311  1.54   msaitoh 	struct i2cbus_attach_args *iba = aux;
    312  1.54   msaitoh 	struct i2c_controller *tag = iba->iba_tag;
    313  1.54   msaitoh 	struct piixpm_smbus *bus = tag->ic_cookie;
    314  1.54   msaitoh 	struct piixpm_softc *sc = bus->softc;
    315  1.54   msaitoh 
    316  1.54   msaitoh 	iicbus_print(aux, pnp);
    317  1.54   msaitoh 	if (sc->sc_numbusses != 0)
    318  1.54   msaitoh 		aprint_normal(" port %d", bus->sda);
    319  1.54   msaitoh 
    320  1.54   msaitoh 	return UNCONF;
    321  1.54   msaitoh }
    322  1.54   msaitoh static int
    323  1.46  pgoyette piixpm_rescan(device_t self, const char *ifattr, const int *flags)
    324  1.46  pgoyette {
    325  1.46  pgoyette 	struct piixpm_softc *sc = device_private(self);
    326  1.46  pgoyette 	struct i2cbus_attach_args iba;
    327  1.46  pgoyette 	int i;
    328  1.46  pgoyette 
    329  1.46  pgoyette 	if (!ifattr_match(ifattr, "i2cbus"))
    330  1.46  pgoyette 		return 0;
    331  1.46  pgoyette 
    332   1.1  jmcneill 	/* Attach I2C bus */
    333   1.1  jmcneill 
    334  1.46  pgoyette 	for (i = 0; i < sc->sc_numbusses; i++) {
    335  1.46  pgoyette 		if (sc->sc_i2c_device[i])
    336  1.46  pgoyette 			continue;
    337  1.42     soren 		sc->sc_busses[i].sda = i;
    338  1.42     soren 		sc->sc_busses[i].softc = sc;
    339  1.55   thorpej 		iic_tag_init(&sc->sc_i2c_tags[i]);
    340  1.42     soren 		sc->sc_i2c_tags[i].ic_cookie = &sc->sc_busses[i];
    341  1.42     soren 		sc->sc_i2c_tags[i].ic_acquire_bus = piixpm_i2c_acquire_bus;
    342  1.42     soren 		sc->sc_i2c_tags[i].ic_release_bus = piixpm_i2c_release_bus;
    343  1.42     soren 		sc->sc_i2c_tags[i].ic_exec = piixpm_i2c_exec;
    344  1.42     soren 		memset(&iba, 0, sizeof(iba));
    345  1.42     soren 		iba.iba_tag = &sc->sc_i2c_tags[i];
    346  1.46  pgoyette 		sc->sc_i2c_device[i] = config_found_ia(self, ifattr, &iba,
    347  1.54   msaitoh 		    piixpm_iicbus_print);
    348  1.42     soren 	}
    349  1.46  pgoyette 
    350  1.46  pgoyette 	return 0;
    351   1.1  jmcneill }
    352   1.1  jmcneill 
    353  1.46  pgoyette static void
    354  1.46  pgoyette piixpm_chdet(device_t self, device_t child)
    355  1.46  pgoyette {
    356  1.46  pgoyette 	struct piixpm_softc *sc = device_private(self);
    357  1.46  pgoyette 	int i;
    358  1.46  pgoyette 
    359  1.46  pgoyette 	for (i = 0; i < sc->sc_numbusses; i++) {
    360  1.46  pgoyette 		if (sc->sc_i2c_device[i] == child) {
    361  1.46  pgoyette 			sc->sc_i2c_device[i] = NULL;
    362  1.46  pgoyette 			break;
    363  1.46  pgoyette 		}
    364  1.46  pgoyette 	}
    365  1.46  pgoyette }
    366  1.46  pgoyette 
    367  1.46  pgoyette 
    368  1.18  jmcneill static bool
    369  1.32    dyoung piixpm_suspend(device_t dv, const pmf_qual_t *qual)
    370  1.18  jmcneill {
    371  1.18  jmcneill 	struct piixpm_softc *sc = device_private(dv);
    372  1.18  jmcneill 
    373  1.18  jmcneill 	sc->sc_devact[0] = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
    374  1.18  jmcneill 	    PIIX_DEVACTA);
    375  1.18  jmcneill 	sc->sc_devact[1] = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
    376  1.18  jmcneill 	    PIIX_DEVACTB);
    377  1.18  jmcneill 
    378  1.18  jmcneill 	return true;
    379  1.18  jmcneill }
    380  1.18  jmcneill 
    381  1.18  jmcneill static bool
    382  1.32    dyoung piixpm_resume(device_t dv, const pmf_qual_t *qual)
    383   1.3  jmcneill {
    384  1.18  jmcneill 	struct piixpm_softc *sc = device_private(dv);
    385   1.3  jmcneill 
    386  1.18  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_DEVACTA,
    387  1.18  jmcneill 	    sc->sc_devact[0]);
    388  1.18  jmcneill 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_DEVACTB,
    389  1.18  jmcneill 	    sc->sc_devact[1]);
    390   1.3  jmcneill 
    391  1.18  jmcneill 	return true;
    392   1.3  jmcneill }
    393   1.3  jmcneill 
    394  1.36  jmcneill /*
    395  1.36  jmcneill  * Extract SMBus base address from SB800 Power Management (PM) registers.
    396  1.36  jmcneill  * The PM registers can be accessed either through indirect I/O (CD6/CD7) or
    397  1.36  jmcneill  * direct mapping if AcpiMMioDecodeEn is enabled. Since this function is only
    398  1.36  jmcneill  * called once it uses indirect I/O for simplicity.
    399  1.36  jmcneill  */
    400  1.36  jmcneill static int
    401  1.42     soren piixpm_sb800_init(struct piixpm_softc *sc)
    402  1.36  jmcneill {
    403  1.42     soren 	bus_space_tag_t iot = sc->sc_iot;
    404  1.36  jmcneill 	bus_space_handle_t ioh;	/* indirect I/O handle */
    405  1.36  jmcneill 	uint16_t val, base_addr;
    406  1.54   msaitoh 	bool enabled;
    407  1.36  jmcneill 
    408  1.57   msaitoh 	if (PIIXPM_IS_KERNCZ(sc) ||
    409  1.57   msaitoh 	    (PIIXPM_IS_HUDSON(sc) && (sc->sc_rev >= 0x1f)))
    410  1.57   msaitoh 		sc->sc_numbusses = 2;
    411  1.57   msaitoh 	else
    412  1.57   msaitoh 		sc->sc_numbusses = 4;
    413  1.57   msaitoh 
    414  1.36  jmcneill 	/* Fetch SMB base address */
    415  1.36  jmcneill 	if (bus_space_map(iot,
    416  1.54   msaitoh 	    SB800_INDIRECTIO_BASE, SB800_INDIRECTIO_SIZE, 0, &ioh)) {
    417  1.36  jmcneill 		device_printf(sc->sc_dev, "couldn't map indirect I/O space\n");
    418  1.36  jmcneill 		return EBUSY;
    419  1.36  jmcneill 	}
    420  1.54   msaitoh 	if (PIIXPM_IS_FCHGRP(sc)) {
    421  1.54   msaitoh 		bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
    422  1.54   msaitoh 		    AMDFCH41_PM_DECODE_EN0);
    423  1.54   msaitoh 		val = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA);
    424  1.54   msaitoh 		enabled = val & AMDFCH41_SMBUS_EN;
    425  1.54   msaitoh 		if (!enabled)
    426  1.54   msaitoh 			return ENOENT;
    427  1.54   msaitoh 
    428  1.54   msaitoh 		bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
    429  1.54   msaitoh 		    AMDFCH41_PM_DECODE_EN1);
    430  1.54   msaitoh 		val = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA) << 8;
    431  1.54   msaitoh 		base_addr = val;
    432  1.54   msaitoh 	} else {
    433  1.59   msaitoh 		uint8_t data;
    434  1.59   msaitoh 
    435  1.54   msaitoh 		bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
    436  1.54   msaitoh 		    SB800_PM_SMBUS0EN_LO);
    437  1.54   msaitoh 		val = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA);
    438  1.54   msaitoh 		enabled = val & SB800_PM_SMBUS0EN_ENABLE;
    439  1.54   msaitoh 		if (!enabled)
    440  1.54   msaitoh 			return ENOENT;
    441  1.54   msaitoh 
    442  1.54   msaitoh 		bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
    443  1.54   msaitoh 		    SB800_PM_SMBUS0EN_HI);
    444  1.54   msaitoh 		val |= bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA) << 8;
    445  1.54   msaitoh 		base_addr = val & SB800_PM_SMBUS0EN_BADDR;
    446  1.54   msaitoh 
    447  1.54   msaitoh 		bus_space_write_1(iot, ioh, SB800_INDIRECTIO_INDEX,
    448  1.54   msaitoh 		    SB800_PM_SMBUS0SELEN);
    449  1.59   msaitoh 		data = bus_space_read_1(iot, ioh, SB800_INDIRECTIO_DATA);
    450  1.59   msaitoh 		if ((data & SB800_PM_USE_SMBUS0SEL) != 0)
    451  1.59   msaitoh 			sc->sc_sb800_selen = true;
    452  1.54   msaitoh 	}
    453  1.54   msaitoh 
    454  1.42     soren 	sc->sc_sb800_ioh = ioh;
    455  1.36  jmcneill 	aprint_debug_dev(sc->sc_dev, "SMBus @ 0x%04x\n", base_addr);
    456  1.36  jmcneill 
    457  1.42     soren 	if (bus_space_map(iot, PCI_MAPREG_IO_ADDR(base_addr),
    458  1.58   msaitoh 	    SB800_SMB_SIZE, 0, &sc->sc_smb_ioh)) {
    459  1.36  jmcneill 		aprint_error_dev(sc->sc_dev, "can't map smbus I/O space\n");
    460  1.36  jmcneill 		return EBUSY;
    461  1.36  jmcneill 	}
    462  1.36  jmcneill 
    463  1.36  jmcneill 	return 0;
    464  1.36  jmcneill }
    465  1.36  jmcneill 
    466  1.35   hannken static void
    467  1.35   hannken piixpm_csb5_reset(void *arg)
    468  1.35   hannken {
    469  1.35   hannken 	struct piixpm_softc *sc = arg;
    470  1.35   hannken 	pcireg_t base, hostc, pmbase;
    471  1.35   hannken 
    472  1.35   hannken 	base = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_BASE);
    473  1.35   hannken 	hostc = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_HOSTC);
    474  1.35   hannken 
    475  1.35   hannken 	pmbase = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX_PM_BASE);
    476  1.35   hannken 	pmbase |= PIIX_PM_BASE_CSB5_RESET;
    477  1.35   hannken 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_PM_BASE, pmbase);
    478  1.35   hannken 	pmbase &= ~PIIX_PM_BASE_CSB5_RESET;
    479  1.35   hannken 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_PM_BASE, pmbase);
    480  1.35   hannken 
    481  1.35   hannken 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_BASE, base);
    482  1.35   hannken 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX_SMB_HOSTC, hostc);
    483  1.35   hannken 
    484  1.35   hannken 	(void) tsleep(&sc, PRIBIO, "csb5reset", hz/2);
    485  1.35   hannken }
    486  1.35   hannken 
    487  1.25     joerg static int
    488   1.1  jmcneill piixpm_i2c_acquire_bus(void *cookie, int flags)
    489   1.1  jmcneill {
    490  1.42     soren 	struct piixpm_smbus *smbus = cookie;
    491  1.42     soren 	struct piixpm_softc *sc = smbus->softc;
    492   1.1  jmcneill 
    493  1.54   msaitoh 	if (PIIXPM_IS_KERNCZ(sc)) {
    494  1.54   msaitoh 		bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    495  1.54   msaitoh 		    SB800_INDIRECTIO_INDEX, AMDFCH41_PM_PORT_INDEX);
    496  1.42     soren 		bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    497  1.54   msaitoh 		    SB800_INDIRECTIO_DATA, smbus->sda << 3);
    498  1.54   msaitoh 	} else if (PIIXPM_IS_SB800GRP(sc) || PIIXPM_IS_HUDSON(sc)) {
    499  1.59   msaitoh 		if (sc->sc_sb800_selen) {
    500  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    501  1.59   msaitoh 			    SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0SEL);
    502  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    503  1.59   msaitoh 			    SB800_INDIRECTIO_DATA,
    504  1.59   msaitoh 			    __SHIFTIN(smbus->sda, SB800_PM_SMBUS0_MASK_E));
    505  1.59   msaitoh 		} else {
    506  1.59   msaitoh 			uint8_t data;
    507  1.59   msaitoh 
    508  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    509  1.59   msaitoh 			    SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0EN_LO);
    510  1.59   msaitoh 			data = bus_space_read_1(sc->sc_iot, sc->sc_sb800_ioh,
    511  1.59   msaitoh 			    SB800_INDIRECTIO_DATA) & ~SB800_PM_SMBUS0_MASK_C;
    512  1.59   msaitoh 			data |= __SHIFTIN(smbus->sda, SB800_PM_SMBUS0_MASK_C);
    513  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    514  1.59   msaitoh 			    SB800_INDIRECTIO_DATA, data);
    515  1.59   msaitoh 		}
    516  1.42     soren 	}
    517  1.42     soren 
    518  1.16   xtraeme 	return 0;
    519   1.1  jmcneill }
    520   1.1  jmcneill 
    521  1.25     joerg static void
    522   1.1  jmcneill piixpm_i2c_release_bus(void *cookie, int flags)
    523   1.1  jmcneill {
    524  1.42     soren 	struct piixpm_smbus *smbus = cookie;
    525  1.42     soren 	struct piixpm_softc *sc = smbus->softc;
    526  1.42     soren 
    527  1.54   msaitoh 	if (PIIXPM_IS_KERNCZ(sc)) {
    528  1.54   msaitoh 		bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    529  1.54   msaitoh 		    SB800_INDIRECTIO_INDEX, AMDFCH41_PM_PORT_INDEX);
    530  1.59   msaitoh 		/* Set to port 0 */
    531  1.54   msaitoh 		bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    532  1.54   msaitoh 		    SB800_INDIRECTIO_DATA, 0);
    533  1.54   msaitoh 	} else if (PIIXPM_IS_SB800GRP(sc) || PIIXPM_IS_HUDSON(sc)) {
    534  1.59   msaitoh 		if (sc->sc_sb800_selen) {
    535  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    536  1.59   msaitoh 			    SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0SEL);
    537  1.59   msaitoh 
    538  1.59   msaitoh 			/* Set to port 0 */
    539  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    540  1.59   msaitoh 			    SB800_INDIRECTIO_DATA, 0);
    541  1.59   msaitoh 		} else {
    542  1.59   msaitoh 			uint8_t data;
    543  1.59   msaitoh 
    544  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    545  1.59   msaitoh 			    SB800_INDIRECTIO_INDEX, SB800_PM_SMBUS0EN_LO);
    546  1.59   msaitoh 
    547  1.59   msaitoh 			/* Set to port 0 */
    548  1.59   msaitoh 			data = bus_space_read_1(sc->sc_iot, sc->sc_sb800_ioh,
    549  1.59   msaitoh 			    SB800_INDIRECTIO_DATA) & ~SB800_PM_SMBUS0_MASK_C;
    550  1.59   msaitoh 			bus_space_write_1(sc->sc_iot, sc->sc_sb800_ioh,
    551  1.59   msaitoh 			    SB800_INDIRECTIO_DATA, data);
    552  1.59   msaitoh 		}
    553  1.42     soren 	}
    554   1.1  jmcneill }
    555   1.1  jmcneill 
    556  1.25     joerg static int
    557   1.1  jmcneill piixpm_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    558   1.1  jmcneill     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    559   1.1  jmcneill {
    560  1.42     soren 	struct piixpm_smbus *smbus = cookie;
    561  1.42     soren 	struct piixpm_softc *sc = smbus->softc;
    562  1.54   msaitoh 	const uint8_t *b;
    563  1.54   msaitoh 	uint8_t ctl = 0, st;
    564   1.1  jmcneill 	int retries;
    565   1.1  jmcneill 
    566  1.53   msaitoh 	DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
    567  1.53   msaitoh 		"flags 0x%x\n",
    568  1.53   msaitoh 		device_xname(sc->sc_dev), op, addr, cmdlen, len, flags));
    569   1.1  jmcneill 
    570  1.60   thorpej 	mutex_enter(&sc->sc_exec_lock);
    571  1.60   thorpej 
    572  1.41     soren 	/* Clear status bits */
    573  1.49   msaitoh 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS,
    574  1.49   msaitoh 	    PIIX_SMB_HS_INTR | PIIX_SMB_HS_DEVERR |
    575  1.41     soren 	    PIIX_SMB_HS_BUSERR | PIIX_SMB_HS_FAILED);
    576  1.42     soren 	bus_space_barrier(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, 1,
    577  1.41     soren 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    578  1.41     soren 
    579   1.1  jmcneill 	/* Wait for bus to be idle */
    580   1.1  jmcneill 	for (retries = 100; retries > 0; retries--) {
    581   1.4  jmcneill 		st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    582   1.4  jmcneill 		    PIIX_SMB_HS);
    583   1.1  jmcneill 		if (!(st & PIIX_SMB_HS_BUSY))
    584   1.1  jmcneill 			break;
    585   1.1  jmcneill 		DELAY(PIIXPM_DELAY);
    586   1.1  jmcneill 	}
    587  1.52   msaitoh 	DPRINTF(("%s: exec: st %#x\n", device_xname(sc->sc_dev), st & 0xff));
    588  1.60   thorpej 	if (st & PIIX_SMB_HS_BUSY) {
    589  1.60   thorpej 		mutex_exit(&sc->sc_exec_lock);
    590  1.60   thorpej 		return (EBUSY);
    591  1.60   thorpej 	}
    592   1.1  jmcneill 
    593  1.56   thorpej 	if (sc->sc_poll)
    594   1.1  jmcneill 		flags |= I2C_F_POLL;
    595   1.1  jmcneill 
    596  1.34   hannken 	if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
    597  1.60   thorpej 	    (cmdlen == 0 && len > 1)) {
    598  1.60   thorpej 		mutex_exit(&sc->sc_exec_lock);
    599  1.60   thorpej 		return (EINVAL);
    600  1.60   thorpej 	}
    601   1.1  jmcneill 
    602   1.1  jmcneill 	/* Setup transfer */
    603   1.1  jmcneill 	sc->sc_i2c_xfer.op = op;
    604   1.1  jmcneill 	sc->sc_i2c_xfer.buf = buf;
    605   1.1  jmcneill 	sc->sc_i2c_xfer.len = len;
    606   1.1  jmcneill 	sc->sc_i2c_xfer.flags = flags;
    607   1.1  jmcneill 	sc->sc_i2c_xfer.error = 0;
    608  1.60   thorpej 	sc->sc_i2c_xfer.done = false;
    609   1.1  jmcneill 
    610   1.1  jmcneill 	/* Set slave address and transfer direction */
    611   1.4  jmcneill 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_TXSLVA,
    612   1.1  jmcneill 	    PIIX_SMB_TXSLVA_ADDR(addr) |
    613   1.1  jmcneill 	    (I2C_OP_READ_P(op) ? PIIX_SMB_TXSLVA_READ : 0));
    614   1.1  jmcneill 
    615   1.1  jmcneill 	b = cmdbuf;
    616   1.1  jmcneill 	if (cmdlen > 0)
    617   1.1  jmcneill 		/* Set command byte */
    618   1.4  jmcneill 		bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    619   1.4  jmcneill 		    PIIX_SMB_HCMD, b[0]);
    620   1.1  jmcneill 
    621   1.1  jmcneill 	if (I2C_OP_WRITE_P(op)) {
    622   1.1  jmcneill 		/* Write data */
    623   1.1  jmcneill 		b = buf;
    624  1.34   hannken 		if (cmdlen == 0 && len == 1)
    625  1.34   hannken 			bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    626  1.34   hannken 			    PIIX_SMB_HCMD, b[0]);
    627  1.34   hannken 		else if (len > 0)
    628   1.4  jmcneill 			bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    629   1.1  jmcneill 			    PIIX_SMB_HD0, b[0]);
    630   1.1  jmcneill 		if (len > 1)
    631   1.4  jmcneill 			bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    632   1.1  jmcneill 			    PIIX_SMB_HD1, b[1]);
    633   1.1  jmcneill 	}
    634   1.1  jmcneill 
    635   1.1  jmcneill 	/* Set SMBus command */
    636  1.34   hannken 	if (cmdlen == 0) {
    637  1.34   hannken 		if (len == 0)
    638  1.27  pgoyette 			ctl = PIIX_SMB_HC_CMD_QUICK;
    639  1.27  pgoyette 		else
    640  1.27  pgoyette 			ctl = PIIX_SMB_HC_CMD_BYTE;
    641  1.27  pgoyette 	} else if (len == 1)
    642   1.1  jmcneill 		ctl = PIIX_SMB_HC_CMD_BDATA;
    643   1.1  jmcneill 	else if (len == 2)
    644   1.1  jmcneill 		ctl = PIIX_SMB_HC_CMD_WDATA;
    645  1.54   msaitoh 	else
    646  1.54   msaitoh 		panic("%s: unexpected len %zu", __func__, len);
    647   1.1  jmcneill 
    648   1.1  jmcneill 	if ((flags & I2C_F_POLL) == 0)
    649   1.1  jmcneill 		ctl |= PIIX_SMB_HC_INTREN;
    650   1.1  jmcneill 
    651   1.1  jmcneill 	/* Start transaction */
    652   1.1  jmcneill 	ctl |= PIIX_SMB_HC_START;
    653   1.4  jmcneill 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC, ctl);
    654   1.1  jmcneill 
    655   1.1  jmcneill 	if (flags & I2C_F_POLL) {
    656   1.1  jmcneill 		/* Poll for completion */
    657  1.54   msaitoh 		if (PIIXPM_IS_CSB5(sc))
    658  1.35   hannken 			DELAY(2*PIIXPM_DELAY);
    659  1.35   hannken 		else
    660  1.35   hannken 			DELAY(PIIXPM_DELAY);
    661   1.1  jmcneill 		for (retries = 1000; retries > 0; retries--) {
    662   1.4  jmcneill 			st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    663   1.1  jmcneill 			    PIIX_SMB_HS);
    664   1.1  jmcneill 			if ((st & PIIX_SMB_HS_BUSY) == 0)
    665   1.1  jmcneill 				break;
    666   1.1  jmcneill 			DELAY(PIIXPM_DELAY);
    667   1.1  jmcneill 		}
    668   1.1  jmcneill 		if (st & PIIX_SMB_HS_BUSY)
    669   1.1  jmcneill 			goto timeout;
    670  1.45   hannken 		piixpm_intr(sc);
    671   1.1  jmcneill 	} else {
    672   1.1  jmcneill 		/* Wait for interrupt */
    673  1.60   thorpej 		while (! sc->sc_i2c_xfer.done) {
    674  1.60   thorpej 			if (cv_timedwait(&sc->sc_exec_wait, &sc->sc_exec_lock,
    675  1.60   thorpej 					 PIIXPM_TIMEOUT * hz))
    676  1.60   thorpej 				goto timeout;
    677  1.60   thorpej 		}
    678   1.1  jmcneill 	}
    679   1.1  jmcneill 
    680  1.60   thorpej 	int error = sc->sc_i2c_xfer.error;
    681  1.60   thorpej 	mutex_exit(&sc->sc_exec_lock);
    682   1.1  jmcneill 
    683  1.60   thorpej 	return (error);
    684   1.1  jmcneill 
    685   1.1  jmcneill timeout:
    686   1.1  jmcneill 	/*
    687   1.1  jmcneill 	 * Transfer timeout. Kill the transaction and clear status bits.
    688   1.1  jmcneill 	 */
    689  1.25     joerg 	aprint_error_dev(sc->sc_dev, "timeout, status 0x%x\n", st);
    690   1.4  jmcneill 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HC,
    691   1.1  jmcneill 	    PIIX_SMB_HC_KILL);
    692   1.1  jmcneill 	DELAY(PIIXPM_DELAY);
    693   1.4  jmcneill 	st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
    694   1.1  jmcneill 	if ((st & PIIX_SMB_HS_FAILED) == 0)
    695  1.49   msaitoh 		aprint_error_dev(sc->sc_dev,
    696  1.49   msaitoh 		    "transaction abort failed, status 0x%x\n", st);
    697   1.4  jmcneill 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
    698  1.35   hannken 	/*
    699  1.35   hannken 	 * CSB5 needs hard reset to unlock the smbus after timeout.
    700  1.35   hannken 	 */
    701  1.54   msaitoh 	if (PIIXPM_IS_CSB5(sc))
    702  1.35   hannken 		piixpm_csb5_reset(sc);
    703  1.60   thorpej 	mutex_exit(&sc->sc_exec_lock);
    704  1.60   thorpej 	return (ETIMEDOUT);
    705   1.1  jmcneill }
    706   1.1  jmcneill 
    707  1.25     joerg static int
    708   1.1  jmcneill piixpm_intr(void *arg)
    709   1.1  jmcneill {
    710  1.45   hannken 	struct piixpm_softc *sc = arg;
    711  1.54   msaitoh 	uint8_t st;
    712  1.54   msaitoh 	uint8_t *b;
    713   1.1  jmcneill 	size_t len;
    714   1.1  jmcneill 
    715   1.1  jmcneill 	/* Read status */
    716   1.4  jmcneill 	st = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS);
    717   1.1  jmcneill 	if ((st & PIIX_SMB_HS_BUSY) != 0 || (st & (PIIX_SMB_HS_INTR |
    718   1.1  jmcneill 	    PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
    719   1.1  jmcneill 	    PIIX_SMB_HS_FAILED)) == 0)
    720   1.1  jmcneill 		/* Interrupt was not for us */
    721   1.1  jmcneill 		return (0);
    722   1.1  jmcneill 
    723  1.52   msaitoh 	DPRINTF(("%s: intr st %#x\n", device_xname(sc->sc_dev), st & 0xff));
    724   1.1  jmcneill 
    725  1.60   thorpej 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
    726  1.60   thorpej 		mutex_enter(&sc->sc_exec_lock);
    727  1.60   thorpej 
    728   1.1  jmcneill 	/* Clear status bits */
    729   1.4  jmcneill 	bus_space_write_1(sc->sc_smb_iot, sc->sc_smb_ioh, PIIX_SMB_HS, st);
    730   1.1  jmcneill 
    731   1.1  jmcneill 	/* Check for errors */
    732   1.1  jmcneill 	if (st & (PIIX_SMB_HS_DEVERR | PIIX_SMB_HS_BUSERR |
    733   1.1  jmcneill 	    PIIX_SMB_HS_FAILED)) {
    734  1.60   thorpej 		sc->sc_i2c_xfer.error = EIO;
    735   1.1  jmcneill 		goto done;
    736   1.1  jmcneill 	}
    737   1.1  jmcneill 
    738   1.1  jmcneill 	if (st & PIIX_SMB_HS_INTR) {
    739   1.1  jmcneill 		if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
    740   1.1  jmcneill 			goto done;
    741   1.1  jmcneill 
    742   1.1  jmcneill 		/* Read data */
    743   1.1  jmcneill 		b = sc->sc_i2c_xfer.buf;
    744   1.1  jmcneill 		len = sc->sc_i2c_xfer.len;
    745   1.1  jmcneill 		if (len > 0)
    746   1.4  jmcneill 			b[0] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    747   1.1  jmcneill 			    PIIX_SMB_HD0);
    748   1.1  jmcneill 		if (len > 1)
    749   1.4  jmcneill 			b[1] = bus_space_read_1(sc->sc_smb_iot, sc->sc_smb_ioh,
    750   1.1  jmcneill 			    PIIX_SMB_HD1);
    751   1.1  jmcneill 	}
    752   1.1  jmcneill 
    753   1.1  jmcneill done:
    754  1.60   thorpej 	sc->sc_i2c_xfer.done = true;
    755  1.60   thorpej 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0) {
    756  1.60   thorpej 		cv_signal(&sc->sc_exec_wait);
    757  1.60   thorpej 		mutex_exit(&sc->sc_exec_lock);
    758  1.60   thorpej 	}
    759   1.1  jmcneill 	return (1);
    760   1.1  jmcneill }
    761