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