Home | History | Annotate | Line # | Download | only in pci
ichsmb.c revision 1.76
      1  1.76   msaitoh /*	$NetBSD: ichsmb.c,v 1.76 2022/01/25 16:07:57 msaitoh Exp $	*/
      2  1.69   thorpej /*	$OpenBSD: ichiic.c,v 1.44 2020/10/07 11:23:05 jsg Exp $	*/
      3   1.1  kiyohara 
      4   1.1  kiyohara /*
      5   1.1  kiyohara  * Copyright (c) 2005, 2006 Alexander Yurchenko <grange (at) openbsd.org>
      6   1.1  kiyohara  *
      7   1.1  kiyohara  * Permission to use, copy, modify, and distribute this software for any
      8   1.1  kiyohara  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  kiyohara  * copyright notice and this permission notice appear in all copies.
     10   1.1  kiyohara  *
     11   1.1  kiyohara  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  kiyohara  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  kiyohara  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  kiyohara  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  kiyohara  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  kiyohara  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  kiyohara  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  kiyohara  */
     19   1.1  kiyohara 
     20   1.1  kiyohara /*
     21   1.1  kiyohara  * Intel ICH SMBus controller driver.
     22   1.1  kiyohara  */
     23   1.1  kiyohara 
     24   1.6   xtraeme #include <sys/cdefs.h>
     25  1.76   msaitoh __KERNEL_RCSID(0, "$NetBSD: ichsmb.c,v 1.76 2022/01/25 16:07:57 msaitoh Exp $");
     26   1.6   xtraeme 
     27   1.1  kiyohara #include <sys/param.h>
     28   1.1  kiyohara #include <sys/device.h>
     29   1.1  kiyohara #include <sys/errno.h>
     30   1.1  kiyohara #include <sys/kernel.h>
     31  1.27  pgoyette #include <sys/mutex.h>
     32  1.65   thorpej #include <sys/condvar.h>
     33  1.53  pgoyette #include <sys/module.h>
     34   1.1  kiyohara 
     35  1.10        ad #include <sys/bus.h>
     36   1.1  kiyohara 
     37   1.1  kiyohara #include <dev/pci/pcidevs.h>
     38   1.1  kiyohara #include <dev/pci/pcireg.h>
     39   1.1  kiyohara #include <dev/pci/pcivar.h>
     40   1.1  kiyohara 
     41   1.5   xtraeme #include <dev/ic/i82801lpcreg.h>
     42   1.1  kiyohara 
     43   1.1  kiyohara #include <dev/i2c/i2cvar.h>
     44   1.1  kiyohara 
     45   1.1  kiyohara #ifdef ICHIIC_DEBUG
     46   1.1  kiyohara #define DPRINTF(x) printf x
     47   1.1  kiyohara #else
     48   1.1  kiyohara #define DPRINTF(x)
     49   1.1  kiyohara #endif
     50   1.1  kiyohara 
     51   1.1  kiyohara #define ICHIIC_DELAY	100
     52   1.1  kiyohara #define ICHIIC_TIMEOUT	1
     53   1.1  kiyohara 
     54   1.1  kiyohara struct ichsmb_softc {
     55  1.12  kiyohara 	device_t		sc_dev;
     56   1.1  kiyohara 
     57   1.1  kiyohara 	bus_space_tag_t		sc_iot;
     58   1.1  kiyohara 	bus_space_handle_t	sc_ioh;
     59  1.55  pgoyette 	bus_size_t		sc_size;
     60  1.55  pgoyette 	pci_chipset_tag_t	sc_pc;
     61   1.1  kiyohara 	void *			sc_ih;
     62   1.1  kiyohara 	int			sc_poll;
     63  1.58  jdolecek 	pci_intr_handle_t	*sc_pihp;
     64   1.1  kiyohara 
     65  1.65   thorpej 	kmutex_t		sc_exec_lock;
     66  1.65   thorpej 	kcondvar_t		sc_exec_wait;
     67  1.65   thorpej 
     68   1.1  kiyohara 	struct i2c_controller	sc_i2c_tag;
     69   1.1  kiyohara 	struct {
     70   1.1  kiyohara 		i2c_op_t     op;
     71   1.1  kiyohara 		void *       buf;
     72   1.1  kiyohara 		size_t       len;
     73   1.1  kiyohara 		int          flags;
     74  1.65   thorpej 		int          error;
     75  1.65   thorpej 		bool         done;
     76   1.1  kiyohara 	}			sc_i2c_xfer;
     77  1.42  pgoyette 	device_t		sc_i2c_device;
     78   1.1  kiyohara };
     79   1.1  kiyohara 
     80  1.21    cegger static int	ichsmb_match(device_t, cfdata_t, void *);
     81  1.12  kiyohara static void	ichsmb_attach(device_t, device_t, void *);
     82  1.55  pgoyette static int	ichsmb_detach(device_t, int);
     83  1.42  pgoyette static int	ichsmb_rescan(device_t, const char *, const int *);
     84  1.42  pgoyette static void	ichsmb_chdet(device_t, device_t);
     85   1.1  kiyohara 
     86   1.1  kiyohara static int	ichsmb_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     87   1.1  kiyohara 		    size_t, void *, size_t, int);
     88   1.1  kiyohara 
     89   1.1  kiyohara static int	ichsmb_intr(void *);
     90   1.1  kiyohara 
     91  1.53  pgoyette #include "ioconf.h"
     92   1.1  kiyohara 
     93  1.42  pgoyette CFATTACH_DECL3_NEW(ichsmb, sizeof(struct ichsmb_softc),
     94  1.55  pgoyette     ichsmb_match, ichsmb_attach, ichsmb_detach, NULL, ichsmb_rescan,
     95  1.59  jdolecek     ichsmb_chdet, DVF_DETACH_SHUTDOWN);
     96   1.1  kiyohara 
     97   1.1  kiyohara 
     98   1.1  kiyohara static int
     99  1.21    cegger ichsmb_match(device_t parent, cfdata_t match, void *aux)
    100   1.1  kiyohara {
    101   1.1  kiyohara 	struct pci_attach_args *pa = aux;
    102   1.1  kiyohara 
    103   1.1  kiyohara 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
    104   1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    105   1.1  kiyohara 		case PCI_PRODUCT_INTEL_6300ESB_SMB:
    106   1.1  kiyohara 		case PCI_PRODUCT_INTEL_63XXESB_SMB:
    107   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801AA_SMB:
    108   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801AB_SMB:
    109   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801BA_SMB:
    110   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801CA_SMB:
    111   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801DB_SMB:
    112   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801E_SMB:
    113   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801EB_SMB:
    114   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801FB_SMB:
    115   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801G_SMB:
    116   1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801H_SMB:
    117   1.7   xtraeme 		case PCI_PRODUCT_INTEL_82801I_SMB:
    118  1.23     njoly 		case PCI_PRODUCT_INTEL_82801JD_SMB:
    119  1.23     njoly 		case PCI_PRODUCT_INTEL_82801JI_SMB:
    120  1.22       tnn 		case PCI_PRODUCT_INTEL_3400_SMB:
    121  1.25   msaitoh 		case PCI_PRODUCT_INTEL_6SERIES_SMB:
    122  1.28  riastrad 		case PCI_PRODUCT_INTEL_7SERIES_SMB:
    123  1.31   msaitoh 		case PCI_PRODUCT_INTEL_8SERIES_SMB:
    124  1.39   msaitoh 		case PCI_PRODUCT_INTEL_9SERIES_SMB:
    125  1.44   msaitoh 		case PCI_PRODUCT_INTEL_100SERIES_SMB:
    126  1.49   msaitoh 		case PCI_PRODUCT_INTEL_100SERIES_LP_SMB:
    127  1.48   msaitoh 		case PCI_PRODUCT_INTEL_2HS_SMB:
    128  1.57   msaitoh 		case PCI_PRODUCT_INTEL_3HS_SMB:
    129  1.68   msaitoh 		case PCI_PRODUCT_INTEL_3HS_U_SMB:
    130  1.69   thorpej 		case PCI_PRODUCT_INTEL_4HS_H_SMB:
    131  1.74   msaitoh 		case PCI_PRODUCT_INTEL_4HS_V_SMB:
    132  1.33   msaitoh 		case PCI_PRODUCT_INTEL_CORE4G_M_SMB:
    133  1.41       tnn 		case PCI_PRODUCT_INTEL_CORE5G_M_SMB:
    134  1.67   msaitoh 		case PCI_PRODUCT_INTEL_CMTLK_SMB:
    135  1.36   msaitoh 		case PCI_PRODUCT_INTEL_BAYTRAIL_PCU_SMB:
    136  1.43   msaitoh 		case PCI_PRODUCT_INTEL_BSW_PCU_SMB:
    137  1.56   msaitoh 		case PCI_PRODUCT_INTEL_APL_SMB:
    138  1.56   msaitoh 		case PCI_PRODUCT_INTEL_GLK_SMB:
    139  1.75   msaitoh 		case PCI_PRODUCT_INTEL_EHL_SMB:
    140  1.71   msaitoh 		case PCI_PRODUCT_INTEL_JSL_SMB:
    141  1.30  riastrad 		case PCI_PRODUCT_INTEL_C600_SMBUS:
    142  1.29   msaitoh 		case PCI_PRODUCT_INTEL_C600_SMB_0:
    143  1.29   msaitoh 		case PCI_PRODUCT_INTEL_C600_SMB_1:
    144  1.29   msaitoh 		case PCI_PRODUCT_INTEL_C600_SMB_2:
    145  1.40   msaitoh 		case PCI_PRODUCT_INTEL_C610_SMB:
    146  1.52   msaitoh 		case PCI_PRODUCT_INTEL_C620_SMB:
    147  1.52   msaitoh 		case PCI_PRODUCT_INTEL_C620_SMB_S:
    148  1.36   msaitoh 		case PCI_PRODUCT_INTEL_EP80579_SMB:
    149  1.38   msaitoh 		case PCI_PRODUCT_INTEL_DH89XXCC_SMB:
    150  1.38   msaitoh 		case PCI_PRODUCT_INTEL_DH89XXCL_SMB:
    151  1.34   msaitoh 		case PCI_PRODUCT_INTEL_C2000_PCU_SMBUS:
    152  1.51   msaitoh 		case PCI_PRODUCT_INTEL_C3K_SMBUS_LEGACY:
    153  1.69   thorpej 		case PCI_PRODUCT_INTEL_495_YU_SMB:
    154  1.73   msaitoh 		case PCI_PRODUCT_INTEL_5HS_H_SMB:
    155  1.69   thorpej 		case PCI_PRODUCT_INTEL_5HS_LP_SMB:
    156  1.76   msaitoh 		case PCI_PRODUCT_INTEL_6HS_H_SMB:
    157   1.1  kiyohara 			return 1;
    158   1.1  kiyohara 		}
    159   1.1  kiyohara 	}
    160   1.1  kiyohara 	return 0;
    161   1.1  kiyohara }
    162   1.1  kiyohara 
    163   1.1  kiyohara static void
    164  1.12  kiyohara ichsmb_attach(device_t parent, device_t self, void *aux)
    165   1.1  kiyohara {
    166  1.12  kiyohara 	struct ichsmb_softc *sc = device_private(self);
    167   1.1  kiyohara 	struct pci_attach_args *pa = aux;
    168   1.1  kiyohara 	pcireg_t conf;
    169   1.1  kiyohara 	const char *intrstr = NULL;
    170  1.35  christos 	char intrbuf[PCI_INTRSTR_LEN];
    171   1.1  kiyohara 
    172  1.12  kiyohara 	sc->sc_dev = self;
    173  1.55  pgoyette 	sc->sc_pc = pa->pa_pc;
    174  1.12  kiyohara 
    175  1.26  drochner 	pci_aprint_devinfo(pa, NULL);
    176   1.1  kiyohara 
    177  1.65   thorpej 	mutex_init(&sc->sc_exec_lock, MUTEX_DEFAULT, IPL_BIO);
    178  1.65   thorpej 	cv_init(&sc->sc_exec_wait, device_xname(self));
    179  1.65   thorpej 
    180   1.1  kiyohara 	/* Read configuration */
    181   1.5   xtraeme 	conf = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_SMB_HOSTC);
    182  1.16     njoly 	DPRINTF(("%s: conf 0x%08x\n", device_xname(sc->sc_dev), conf));
    183   1.1  kiyohara 
    184   1.5   xtraeme 	if ((conf & LPCIB_SMB_HOSTC_HSTEN) == 0) {
    185  1.12  kiyohara 		aprint_error_dev(self, "SMBus disabled\n");
    186  1.37  riastrad 		goto out;
    187   1.1  kiyohara 	}
    188   1.1  kiyohara 
    189   1.1  kiyohara 	/* Map I/O space */
    190   1.5   xtraeme 	if (pci_mapreg_map(pa, LPCIB_SMB_BASE, PCI_MAPREG_TYPE_IO, 0,
    191  1.55  pgoyette 	    &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_size)) {
    192  1.12  kiyohara 		aprint_error_dev(self, "can't map I/O space\n");
    193  1.37  riastrad 		goto out;
    194   1.1  kiyohara 	}
    195   1.1  kiyohara 
    196   1.1  kiyohara 	sc->sc_poll = 1;
    197  1.55  pgoyette 	sc->sc_ih = NULL;
    198   1.5   xtraeme 	if (conf & LPCIB_SMB_HOSTC_SMIEN) {
    199   1.1  kiyohara 		/* No PCI IRQ */
    200  1.15     njoly 		aprint_normal_dev(self, "interrupting at SMI\n");
    201   1.1  kiyohara 	} else {
    202   1.1  kiyohara 		/* Install interrupt handler */
    203  1.58  jdolecek 		if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0) {
    204  1.58  jdolecek 			intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0],
    205  1.58  jdolecek 			    intrbuf, sizeof(intrbuf));
    206  1.65   thorpej 			pci_intr_setattr(pa->pa_pc, &sc->sc_pihp[0],
    207  1.65   thorpej 			    PCI_INTR_MPSAFE, true);
    208  1.58  jdolecek 			sc->sc_ih = pci_intr_establish_xname(pa->pa_pc,
    209  1.58  jdolecek 			    sc->sc_pihp[0], IPL_BIO, ichsmb_intr, sc,
    210  1.58  jdolecek 			    device_xname(sc->sc_dev));
    211   1.1  kiyohara 			if (sc->sc_ih != NULL) {
    212  1.12  kiyohara 				aprint_normal_dev(self, "interrupting at %s\n",
    213  1.12  kiyohara 				    intrstr);
    214   1.1  kiyohara 				sc->sc_poll = 0;
    215  1.60  jdolecek 			} else {
    216  1.60  jdolecek 				pci_intr_release(pa->pa_pc, sc->sc_pihp, 1);
    217  1.60  jdolecek 				sc->sc_pihp = NULL;
    218   1.1  kiyohara 			}
    219   1.1  kiyohara 		}
    220   1.1  kiyohara 		if (sc->sc_poll)
    221  1.12  kiyohara 			aprint_normal_dev(self, "polling\n");
    222   1.1  kiyohara 	}
    223   1.1  kiyohara 
    224  1.42  pgoyette 	sc->sc_i2c_device = NULL;
    225  1.70   thorpej 	ichsmb_rescan(self, NULL, NULL);
    226  1.42  pgoyette 
    227  1.42  pgoyette out:	if (!pmf_device_register(self, NULL, NULL))
    228  1.42  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    229  1.42  pgoyette }
    230  1.42  pgoyette 
    231  1.42  pgoyette static int
    232  1.70   thorpej ichsmb_rescan(device_t self, const char *ifattr, const int *locators)
    233  1.42  pgoyette {
    234  1.42  pgoyette 	struct ichsmb_softc *sc = device_private(self);
    235  1.42  pgoyette 	struct i2cbus_attach_args iba;
    236  1.42  pgoyette 
    237  1.70   thorpej 	if (sc->sc_i2c_device != NULL)
    238  1.42  pgoyette 		return 0;
    239  1.42  pgoyette 
    240   1.1  kiyohara 	/* Attach I2C bus */
    241  1.63   thorpej 	iic_tag_init(&sc->sc_i2c_tag);
    242   1.1  kiyohara 	sc->sc_i2c_tag.ic_cookie = sc;
    243   1.1  kiyohara 	sc->sc_i2c_tag.ic_exec = ichsmb_i2c_exec;
    244   1.1  kiyohara 
    245  1.20    cegger 	memset(&iba, 0, sizeof(iba));
    246   1.1  kiyohara 	iba.iba_tag = &sc->sc_i2c_tag;
    247  1.72   thorpej 	sc->sc_i2c_device = config_found(self, &iba, iicbus_print, CFARGS_NONE);
    248  1.42  pgoyette 
    249  1.42  pgoyette 	return 0;
    250  1.42  pgoyette }
    251  1.42  pgoyette 
    252  1.55  pgoyette static int
    253  1.55  pgoyette ichsmb_detach(device_t self, int flags)
    254  1.55  pgoyette {
    255  1.55  pgoyette 	struct ichsmb_softc *sc = device_private(self);
    256  1.55  pgoyette 	int error;
    257  1.55  pgoyette 
    258  1.55  pgoyette 	if (sc->sc_i2c_device) {
    259  1.55  pgoyette 		error = config_detach(sc->sc_i2c_device, flags);
    260  1.55  pgoyette 		if (error)
    261  1.55  pgoyette 			return error;
    262  1.55  pgoyette 	}
    263  1.55  pgoyette 
    264  1.63   thorpej 	iic_tag_fini(&sc->sc_i2c_tag);
    265  1.55  pgoyette 
    266  1.58  jdolecek 	if (sc->sc_ih) {
    267  1.55  pgoyette 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    268  1.58  jdolecek 		sc->sc_ih = NULL;
    269  1.58  jdolecek 	}
    270  1.58  jdolecek 
    271  1.58  jdolecek 	if (sc->sc_pihp) {
    272  1.58  jdolecek 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    273  1.58  jdolecek 		sc->sc_pihp = NULL;
    274  1.58  jdolecek 	}
    275  1.55  pgoyette 
    276  1.61        ad 	if (sc->sc_size != 0)
    277  1.61        ad 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
    278  1.55  pgoyette 
    279  1.65   thorpej 	mutex_destroy(&sc->sc_exec_lock);
    280  1.65   thorpej 	cv_destroy(&sc->sc_exec_wait);
    281  1.65   thorpej 
    282  1.55  pgoyette 	return 0;
    283  1.55  pgoyette }
    284  1.55  pgoyette 
    285  1.42  pgoyette static void
    286  1.42  pgoyette ichsmb_chdet(device_t self, device_t child)
    287  1.42  pgoyette {
    288  1.42  pgoyette 	struct ichsmb_softc *sc = device_private(self);
    289  1.42  pgoyette 
    290  1.42  pgoyette 	if (sc->sc_i2c_device == child)
    291  1.42  pgoyette 		sc->sc_i2c_device = NULL;
    292   1.1  kiyohara }
    293   1.1  kiyohara 
    294   1.1  kiyohara static int
    295   1.1  kiyohara ichsmb_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    296   1.1  kiyohara     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    297   1.1  kiyohara {
    298   1.1  kiyohara 	struct ichsmb_softc *sc = cookie;
    299   1.1  kiyohara 	const uint8_t *b;
    300   1.1  kiyohara 	uint8_t ctl = 0, st;
    301   1.1  kiyohara 	int retries;
    302   1.4  kiyohara 	char fbuf[64];
    303   1.1  kiyohara 
    304  1.14     njoly 	DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
    305  1.14     njoly 	    "flags 0x%02x\n", device_xname(sc->sc_dev), op, addr, cmdlen,
    306   1.1  kiyohara 	    len, flags));
    307   1.1  kiyohara 
    308  1.65   thorpej 	mutex_enter(&sc->sc_exec_lock);
    309  1.65   thorpej 
    310  1.32     soren 	/* Clear status bits */
    311  1.32     soren 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS,
    312  1.32     soren 	    LPCIB_SMB_HS_INTR | LPCIB_SMB_HS_DEVERR |
    313  1.32     soren 	    LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED);
    314  1.32     soren 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, 1,
    315  1.66   msaitoh 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    316  1.32     soren 
    317   1.1  kiyohara 	/* Wait for bus to be idle */
    318   1.1  kiyohara 	for (retries = 100; retries > 0; retries--) {
    319   1.5   xtraeme 		st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
    320   1.5   xtraeme 		if (!(st & LPCIB_SMB_HS_BUSY))
    321   1.1  kiyohara 			break;
    322   1.1  kiyohara 		DELAY(ICHIIC_DELAY);
    323   1.1  kiyohara 	}
    324   1.4  kiyohara #ifdef ICHIIC_DEBUG
    325  1.18  christos 	snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    326  1.50   msaitoh 	printf("%s: exec: st %s\n", device_xname(sc->sc_dev), fbuf);
    327   1.4  kiyohara #endif
    328  1.65   thorpej 	if (st & LPCIB_SMB_HS_BUSY) {
    329  1.65   thorpej 		mutex_exit(&sc->sc_exec_lock);
    330  1.65   thorpej 		return (EBUSY);
    331  1.65   thorpej 	}
    332   1.1  kiyohara 
    333  1.64   thorpej 	if (sc->sc_poll)
    334   1.1  kiyohara 		flags |= I2C_F_POLL;
    335   1.1  kiyohara 
    336  1.24   hannken 	if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
    337  1.65   thorpej 	    (cmdlen == 0 && len > 1)) {
    338  1.65   thorpej 		mutex_exit(&sc->sc_exec_lock);
    339  1.65   thorpej 		return (EINVAL);
    340  1.65   thorpej 	}
    341   1.1  kiyohara 
    342   1.1  kiyohara 	/* Setup transfer */
    343   1.1  kiyohara 	sc->sc_i2c_xfer.op = op;
    344   1.1  kiyohara 	sc->sc_i2c_xfer.buf = buf;
    345   1.1  kiyohara 	sc->sc_i2c_xfer.len = len;
    346   1.1  kiyohara 	sc->sc_i2c_xfer.flags = flags;
    347   1.1  kiyohara 	sc->sc_i2c_xfer.error = 0;
    348  1.65   thorpej 	sc->sc_i2c_xfer.done = false;
    349   1.1  kiyohara 
    350   1.1  kiyohara 	/* Set slave address and transfer direction */
    351   1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_TXSLVA,
    352   1.5   xtraeme 	    LPCIB_SMB_TXSLVA_ADDR(addr) |
    353   1.5   xtraeme 	    (I2C_OP_READ_P(op) ? LPCIB_SMB_TXSLVA_READ : 0));
    354   1.1  kiyohara 
    355   1.1  kiyohara 	b = (const uint8_t *)cmdbuf;
    356   1.1  kiyohara 	if (cmdlen > 0)
    357   1.1  kiyohara 		/* Set command byte */
    358   1.5   xtraeme 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HCMD, b[0]);
    359   1.1  kiyohara 
    360   1.1  kiyohara 	if (I2C_OP_WRITE_P(op)) {
    361   1.1  kiyohara 		/* Write data */
    362   1.1  kiyohara 		b = buf;
    363  1.24   hannken 		if (cmdlen == 0 && len == 1)
    364  1.24   hannken 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    365  1.24   hannken 			    LPCIB_SMB_HCMD, b[0]);
    366  1.24   hannken 		else if (len > 0)
    367   1.1  kiyohara 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    368   1.5   xtraeme 			    LPCIB_SMB_HD0, b[0]);
    369   1.1  kiyohara 		if (len > 1)
    370   1.1  kiyohara 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    371   1.5   xtraeme 			    LPCIB_SMB_HD1, b[1]);
    372   1.1  kiyohara 	}
    373   1.1  kiyohara 
    374   1.1  kiyohara 	/* Set SMBus command */
    375  1.24   hannken 	if (cmdlen == 0) {
    376  1.24   hannken 		if (len == 0)
    377  1.19  pgoyette 			ctl = LPCIB_SMB_HC_CMD_QUICK;
    378  1.19  pgoyette 		else
    379  1.19  pgoyette 			ctl = LPCIB_SMB_HC_CMD_BYTE;
    380  1.19  pgoyette 	} else if (len == 1)
    381   1.5   xtraeme 		ctl = LPCIB_SMB_HC_CMD_BDATA;
    382   1.1  kiyohara 	else if (len == 2)
    383   1.5   xtraeme 		ctl = LPCIB_SMB_HC_CMD_WDATA;
    384   1.1  kiyohara 
    385   1.1  kiyohara 	if ((flags & I2C_F_POLL) == 0)
    386   1.5   xtraeme 		ctl |= LPCIB_SMB_HC_INTREN;
    387   1.1  kiyohara 
    388   1.1  kiyohara 	/* Start transaction */
    389   1.5   xtraeme 	ctl |= LPCIB_SMB_HC_START;
    390   1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC, ctl);
    391   1.1  kiyohara 
    392   1.1  kiyohara 	if (flags & I2C_F_POLL) {
    393   1.1  kiyohara 		/* Poll for completion */
    394   1.1  kiyohara 		DELAY(ICHIIC_DELAY);
    395   1.1  kiyohara 		for (retries = 1000; retries > 0; retries--) {
    396   1.1  kiyohara 			st = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    397   1.5   xtraeme 			    LPCIB_SMB_HS);
    398   1.5   xtraeme 			if ((st & LPCIB_SMB_HS_BUSY) == 0)
    399   1.1  kiyohara 				break;
    400   1.1  kiyohara 			DELAY(ICHIIC_DELAY);
    401   1.1  kiyohara 		}
    402   1.5   xtraeme 		if (st & LPCIB_SMB_HS_BUSY)
    403   1.1  kiyohara 			goto timeout;
    404   1.1  kiyohara 		ichsmb_intr(sc);
    405   1.1  kiyohara 	} else {
    406   1.1  kiyohara 		/* Wait for interrupt */
    407  1.65   thorpej 		while (! sc->sc_i2c_xfer.done) {
    408  1.65   thorpej 			if (cv_timedwait(&sc->sc_exec_wait, &sc->sc_exec_lock,
    409  1.65   thorpej 					 ICHIIC_TIMEOUT * hz))
    410  1.65   thorpej 				goto timeout;
    411  1.65   thorpej 		}
    412   1.1  kiyohara 	}
    413   1.1  kiyohara 
    414  1.65   thorpej 	int error = sc->sc_i2c_xfer.error;
    415  1.65   thorpej 	mutex_exit(&sc->sc_exec_lock);
    416   1.1  kiyohara 
    417  1.65   thorpej 	return (error);
    418   1.1  kiyohara 
    419   1.1  kiyohara timeout:
    420   1.1  kiyohara 	/*
    421   1.1  kiyohara 	 * Transfer timeout. Kill the transaction and clear status bits.
    422   1.1  kiyohara 	 */
    423   1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC,
    424   1.5   xtraeme 	    LPCIB_SMB_HC_KILL);
    425   1.1  kiyohara 	DELAY(ICHIIC_DELAY);
    426   1.5   xtraeme 	st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
    427   1.5   xtraeme 	if ((st & LPCIB_SMB_HS_FAILED) == 0) {
    428  1.18  christos 		snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    429  1.50   msaitoh 		aprint_error_dev(sc->sc_dev, "abort failed, status %s\n",
    430  1.12  kiyohara 		    fbuf);
    431   1.4  kiyohara 	}
    432   1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
    433  1.65   thorpej 	mutex_exit(&sc->sc_exec_lock);
    434  1.65   thorpej 	return (ETIMEDOUT);
    435   1.1  kiyohara }
    436   1.1  kiyohara 
    437   1.1  kiyohara static int
    438   1.1  kiyohara ichsmb_intr(void *arg)
    439   1.1  kiyohara {
    440   1.1  kiyohara 	struct ichsmb_softc *sc = arg;
    441   1.1  kiyohara 	uint8_t st;
    442   1.1  kiyohara 	uint8_t *b;
    443   1.1  kiyohara 	size_t len;
    444   1.4  kiyohara #ifdef ICHIIC_DEBUG
    445   1.4  kiyohara 	char fbuf[64];
    446   1.4  kiyohara #endif
    447   1.1  kiyohara 
    448   1.1  kiyohara 	/* Read status */
    449   1.5   xtraeme 	st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
    450  1.69   thorpej 
    451  1.69   thorpej 	/* Clear status bits */
    452  1.69   thorpej 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
    453  1.69   thorpej 
    454  1.69   thorpej 	/* XXX Ignore SMBALERT# for now */
    455   1.5   xtraeme 	if ((st & LPCIB_SMB_HS_BUSY) != 0 || (st & (LPCIB_SMB_HS_INTR |
    456   1.5   xtraeme 	    LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED |
    457  1.69   thorpej 	    LPCIB_SMB_HS_BDONE)) == 0)
    458   1.1  kiyohara 		/* Interrupt was not for us */
    459   1.1  kiyohara 		return (0);
    460   1.1  kiyohara 
    461   1.4  kiyohara #ifdef ICHIIC_DEBUG
    462  1.18  christos 	snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    463  1.50   msaitoh 	printf("%s: intr st %s\n", device_xname(sc->sc_dev), fbuf);
    464   1.4  kiyohara #endif
    465   1.1  kiyohara 
    466  1.65   thorpej 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
    467  1.65   thorpej 		mutex_enter(&sc->sc_exec_lock);
    468  1.65   thorpej 
    469   1.1  kiyohara 	/* Check for errors */
    470   1.5   xtraeme 	if (st & (LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED)) {
    471  1.65   thorpej 		sc->sc_i2c_xfer.error = EIO;
    472   1.1  kiyohara 		goto done;
    473   1.1  kiyohara 	}
    474   1.1  kiyohara 
    475   1.5   xtraeme 	if (st & LPCIB_SMB_HS_INTR) {
    476   1.1  kiyohara 		if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
    477   1.1  kiyohara 			goto done;
    478   1.1  kiyohara 
    479   1.1  kiyohara 		/* Read data */
    480   1.1  kiyohara 		b = sc->sc_i2c_xfer.buf;
    481   1.1  kiyohara 		len = sc->sc_i2c_xfer.len;
    482   1.1  kiyohara 		if (len > 0)
    483   1.1  kiyohara 			b[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    484   1.5   xtraeme 			    LPCIB_SMB_HD0);
    485   1.1  kiyohara 		if (len > 1)
    486   1.1  kiyohara 			b[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    487   1.5   xtraeme 			    LPCIB_SMB_HD1);
    488   1.1  kiyohara 	}
    489   1.1  kiyohara 
    490   1.1  kiyohara done:
    491  1.65   thorpej 	sc->sc_i2c_xfer.done = true;
    492  1.65   thorpej 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0) {
    493  1.65   thorpej 		cv_signal(&sc->sc_exec_wait);
    494  1.65   thorpej 		mutex_exit(&sc->sc_exec_lock);
    495  1.65   thorpej 	}
    496   1.1  kiyohara 	return (1);
    497   1.1  kiyohara }
    498  1.53  pgoyette 
    499  1.54  pgoyette MODULE(MODULE_CLASS_DRIVER, ichsmb, "pci,iic");
    500  1.53  pgoyette 
    501  1.53  pgoyette #ifdef _MODULE
    502  1.53  pgoyette #include "ioconf.c"
    503  1.53  pgoyette #endif
    504  1.53  pgoyette 
    505  1.53  pgoyette static int
    506  1.53  pgoyette ichsmb_modcmd(modcmd_t cmd, void *opaque)
    507  1.53  pgoyette {
    508  1.53  pgoyette 	int error = 0;
    509  1.53  pgoyette 
    510  1.53  pgoyette 	switch (cmd) {
    511  1.53  pgoyette 	case MODULE_CMD_INIT:
    512  1.53  pgoyette #ifdef _MODULE
    513  1.53  pgoyette 		error = config_init_component(cfdriver_ioconf_ichsmb,
    514  1.53  pgoyette 		    cfattach_ioconf_ichsmb, cfdata_ioconf_ichsmb);
    515  1.53  pgoyette #endif
    516  1.53  pgoyette 		break;
    517  1.53  pgoyette 	case MODULE_CMD_FINI:
    518  1.53  pgoyette #ifdef _MODULE
    519  1.53  pgoyette 		error = config_fini_component(cfdriver_ioconf_ichsmb,
    520  1.53  pgoyette 		    cfattach_ioconf_ichsmb, cfdata_ioconf_ichsmb);
    521  1.53  pgoyette #endif
    522  1.53  pgoyette 		break;
    523  1.53  pgoyette 	default:
    524  1.53  pgoyette #ifdef _MODULE
    525  1.53  pgoyette 		error = ENOTTY;
    526  1.53  pgoyette #endif
    527  1.53  pgoyette 		break;
    528  1.53  pgoyette 	}
    529  1.53  pgoyette 
    530  1.53  pgoyette 	return error;
    531  1.53  pgoyette }
    532