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