Home | History | Annotate | Line # | Download | only in pci
ichsmb.c revision 1.60.4.2
      1  1.60.4.2    martin /*	$NetBSD: ichsmb.c,v 1.60.4.2 2020/07/10 11:31:59 martin Exp $	*/
      2       1.1  kiyohara /*	$OpenBSD: ichiic.c,v 1.18 2007/05/03 09:36:26 dlg 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.60.4.2    martin __KERNEL_RCSID(0, "$NetBSD: ichsmb.c,v 1.60.4.2 2020/07/10 11:31:59 martin 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.1  kiyohara #include <sys/proc.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.1  kiyohara 	struct i2c_controller	sc_i2c_tag;
     66      1.27  pgoyette 	kmutex_t 		sc_i2c_mutex;
     67       1.1  kiyohara 	struct {
     68       1.1  kiyohara 		i2c_op_t     op;
     69       1.1  kiyohara 		void *       buf;
     70       1.1  kiyohara 		size_t       len;
     71       1.1  kiyohara 		int          flags;
     72       1.1  kiyohara 		volatile int error;
     73       1.1  kiyohara 	}			sc_i2c_xfer;
     74      1.42  pgoyette 	device_t		sc_i2c_device;
     75       1.1  kiyohara };
     76       1.1  kiyohara 
     77      1.21    cegger static int	ichsmb_match(device_t, cfdata_t, void *);
     78      1.12  kiyohara static void	ichsmb_attach(device_t, device_t, void *);
     79      1.55  pgoyette static int	ichsmb_detach(device_t, int);
     80      1.42  pgoyette static int	ichsmb_rescan(device_t, const char *, const int *);
     81      1.42  pgoyette static void	ichsmb_chdet(device_t, device_t);
     82       1.1  kiyohara 
     83       1.1  kiyohara static int	ichsmb_i2c_acquire_bus(void *, int);
     84       1.1  kiyohara static void	ichsmb_i2c_release_bus(void *, int);
     85       1.1  kiyohara static int	ichsmb_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     86       1.1  kiyohara 		    size_t, void *, size_t, int);
     87       1.1  kiyohara 
     88       1.1  kiyohara static int	ichsmb_intr(void *);
     89       1.1  kiyohara 
     90      1.53  pgoyette #include "ioconf.h"
     91       1.1  kiyohara 
     92      1.42  pgoyette CFATTACH_DECL3_NEW(ichsmb, sizeof(struct ichsmb_softc),
     93      1.55  pgoyette     ichsmb_match, ichsmb_attach, ichsmb_detach, NULL, ichsmb_rescan,
     94      1.59  jdolecek     ichsmb_chdet, DVF_DETACH_SHUTDOWN);
     95       1.1  kiyohara 
     96       1.1  kiyohara 
     97       1.1  kiyohara static int
     98      1.21    cegger ichsmb_match(device_t parent, cfdata_t match, void *aux)
     99       1.1  kiyohara {
    100       1.1  kiyohara 	struct pci_attach_args *pa = aux;
    101       1.1  kiyohara 
    102       1.1  kiyohara 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
    103       1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    104       1.1  kiyohara 		case PCI_PRODUCT_INTEL_6300ESB_SMB:
    105       1.1  kiyohara 		case PCI_PRODUCT_INTEL_63XXESB_SMB:
    106       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801AA_SMB:
    107       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801AB_SMB:
    108       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801BA_SMB:
    109       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801CA_SMB:
    110       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801DB_SMB:
    111       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801E_SMB:
    112       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801EB_SMB:
    113       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801FB_SMB:
    114       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801G_SMB:
    115       1.1  kiyohara 		case PCI_PRODUCT_INTEL_82801H_SMB:
    116       1.7   xtraeme 		case PCI_PRODUCT_INTEL_82801I_SMB:
    117      1.23     njoly 		case PCI_PRODUCT_INTEL_82801JD_SMB:
    118      1.23     njoly 		case PCI_PRODUCT_INTEL_82801JI_SMB:
    119      1.22       tnn 		case PCI_PRODUCT_INTEL_3400_SMB:
    120      1.25   msaitoh 		case PCI_PRODUCT_INTEL_6SERIES_SMB:
    121      1.28  riastrad 		case PCI_PRODUCT_INTEL_7SERIES_SMB:
    122      1.31   msaitoh 		case PCI_PRODUCT_INTEL_8SERIES_SMB:
    123      1.39   msaitoh 		case PCI_PRODUCT_INTEL_9SERIES_SMB:
    124      1.44   msaitoh 		case PCI_PRODUCT_INTEL_100SERIES_SMB:
    125      1.49   msaitoh 		case PCI_PRODUCT_INTEL_100SERIES_LP_SMB:
    126      1.48   msaitoh 		case PCI_PRODUCT_INTEL_2HS_SMB:
    127      1.57   msaitoh 		case PCI_PRODUCT_INTEL_3HS_SMB:
    128  1.60.4.2    martin 		case PCI_PRODUCT_INTEL_3HS_U_SMB:
    129      1.33   msaitoh 		case PCI_PRODUCT_INTEL_CORE4G_M_SMB:
    130      1.41       tnn 		case PCI_PRODUCT_INTEL_CORE5G_M_SMB:
    131  1.60.4.2    martin 		case PCI_PRODUCT_INTEL_CMTLK_SMB:
    132      1.36   msaitoh 		case PCI_PRODUCT_INTEL_BAYTRAIL_PCU_SMB:
    133      1.43   msaitoh 		case PCI_PRODUCT_INTEL_BSW_PCU_SMB:
    134      1.56   msaitoh 		case PCI_PRODUCT_INTEL_APL_SMB:
    135      1.56   msaitoh 		case PCI_PRODUCT_INTEL_GLK_SMB:
    136      1.30  riastrad 		case PCI_PRODUCT_INTEL_C600_SMBUS:
    137      1.29   msaitoh 		case PCI_PRODUCT_INTEL_C600_SMB_0:
    138      1.29   msaitoh 		case PCI_PRODUCT_INTEL_C600_SMB_1:
    139      1.29   msaitoh 		case PCI_PRODUCT_INTEL_C600_SMB_2:
    140      1.40   msaitoh 		case PCI_PRODUCT_INTEL_C610_SMB:
    141      1.52   msaitoh 		case PCI_PRODUCT_INTEL_C620_SMB:
    142      1.52   msaitoh 		case PCI_PRODUCT_INTEL_C620_SMB_S:
    143      1.36   msaitoh 		case PCI_PRODUCT_INTEL_EP80579_SMB:
    144      1.38   msaitoh 		case PCI_PRODUCT_INTEL_DH89XXCC_SMB:
    145      1.38   msaitoh 		case PCI_PRODUCT_INTEL_DH89XXCL_SMB:
    146      1.34   msaitoh 		case PCI_PRODUCT_INTEL_C2000_PCU_SMBUS:
    147      1.51   msaitoh 		case PCI_PRODUCT_INTEL_C3K_SMBUS_LEGACY:
    148       1.1  kiyohara 			return 1;
    149       1.1  kiyohara 		}
    150       1.1  kiyohara 	}
    151       1.1  kiyohara 	return 0;
    152       1.1  kiyohara }
    153       1.1  kiyohara 
    154       1.1  kiyohara static void
    155      1.12  kiyohara ichsmb_attach(device_t parent, device_t self, void *aux)
    156       1.1  kiyohara {
    157      1.12  kiyohara 	struct ichsmb_softc *sc = device_private(self);
    158       1.1  kiyohara 	struct pci_attach_args *pa = aux;
    159       1.1  kiyohara 	pcireg_t conf;
    160       1.1  kiyohara 	const char *intrstr = NULL;
    161      1.35  christos 	char intrbuf[PCI_INTRSTR_LEN];
    162      1.42  pgoyette 	int flags;
    163       1.1  kiyohara 
    164      1.12  kiyohara 	sc->sc_dev = self;
    165      1.55  pgoyette 	sc->sc_pc = pa->pa_pc;
    166      1.12  kiyohara 
    167      1.26  drochner 	pci_aprint_devinfo(pa, NULL);
    168  1.60.4.2    martin 	mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
    169       1.1  kiyohara 
    170       1.1  kiyohara 	/* Read configuration */
    171       1.5   xtraeme 	conf = pci_conf_read(pa->pa_pc, pa->pa_tag, LPCIB_SMB_HOSTC);
    172      1.16     njoly 	DPRINTF(("%s: conf 0x%08x\n", device_xname(sc->sc_dev), conf));
    173       1.1  kiyohara 
    174       1.5   xtraeme 	if ((conf & LPCIB_SMB_HOSTC_HSTEN) == 0) {
    175      1.12  kiyohara 		aprint_error_dev(self, "SMBus disabled\n");
    176      1.37  riastrad 		goto out;
    177       1.1  kiyohara 	}
    178       1.1  kiyohara 
    179       1.1  kiyohara 	/* Map I/O space */
    180       1.5   xtraeme 	if (pci_mapreg_map(pa, LPCIB_SMB_BASE, PCI_MAPREG_TYPE_IO, 0,
    181      1.55  pgoyette 	    &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_size)) {
    182      1.12  kiyohara 		aprint_error_dev(self, "can't map I/O space\n");
    183      1.37  riastrad 		goto out;
    184       1.1  kiyohara 	}
    185       1.1  kiyohara 
    186       1.1  kiyohara 	sc->sc_poll = 1;
    187      1.55  pgoyette 	sc->sc_ih = NULL;
    188       1.5   xtraeme 	if (conf & LPCIB_SMB_HOSTC_SMIEN) {
    189       1.1  kiyohara 		/* No PCI IRQ */
    190      1.15     njoly 		aprint_normal_dev(self, "interrupting at SMI\n");
    191       1.1  kiyohara 	} else {
    192       1.1  kiyohara 		/* Install interrupt handler */
    193      1.58  jdolecek 		if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0) == 0) {
    194      1.58  jdolecek 			intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0],
    195      1.58  jdolecek 			    intrbuf, sizeof(intrbuf));
    196      1.58  jdolecek 			sc->sc_ih = pci_intr_establish_xname(pa->pa_pc,
    197      1.58  jdolecek 			    sc->sc_pihp[0], IPL_BIO, ichsmb_intr, sc,
    198      1.58  jdolecek 			    device_xname(sc->sc_dev));
    199       1.1  kiyohara 			if (sc->sc_ih != NULL) {
    200      1.12  kiyohara 				aprint_normal_dev(self, "interrupting at %s\n",
    201      1.12  kiyohara 				    intrstr);
    202       1.1  kiyohara 				sc->sc_poll = 0;
    203      1.60  jdolecek 			} else {
    204      1.60  jdolecek 				pci_intr_release(pa->pa_pc, sc->sc_pihp, 1);
    205      1.60  jdolecek 				sc->sc_pihp = NULL;
    206       1.1  kiyohara 			}
    207       1.1  kiyohara 		}
    208       1.1  kiyohara 		if (sc->sc_poll)
    209      1.12  kiyohara 			aprint_normal_dev(self, "polling\n");
    210       1.1  kiyohara 	}
    211       1.1  kiyohara 
    212      1.42  pgoyette 	sc->sc_i2c_device = NULL;
    213      1.42  pgoyette 	flags = 0;
    214      1.42  pgoyette 	ichsmb_rescan(self, "i2cbus", &flags);
    215      1.42  pgoyette 
    216      1.42  pgoyette out:	if (!pmf_device_register(self, NULL, NULL))
    217      1.42  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    218      1.42  pgoyette }
    219      1.42  pgoyette 
    220      1.42  pgoyette static int
    221      1.42  pgoyette ichsmb_rescan(device_t self, const char *ifattr, const int *flags)
    222      1.42  pgoyette {
    223      1.42  pgoyette 	struct ichsmb_softc *sc = device_private(self);
    224      1.42  pgoyette 	struct i2cbus_attach_args iba;
    225      1.42  pgoyette 
    226      1.42  pgoyette 	if (!ifattr_match(ifattr, "i2cbus"))
    227      1.42  pgoyette 		return 0;
    228      1.42  pgoyette 
    229      1.42  pgoyette 	if (sc->sc_i2c_device)
    230      1.42  pgoyette 		return 0;
    231      1.42  pgoyette 
    232       1.1  kiyohara 	/* Attach I2C bus */
    233       1.1  kiyohara 	sc->sc_i2c_tag.ic_cookie = sc;
    234       1.1  kiyohara 	sc->sc_i2c_tag.ic_acquire_bus = ichsmb_i2c_acquire_bus;
    235       1.1  kiyohara 	sc->sc_i2c_tag.ic_release_bus = ichsmb_i2c_release_bus;
    236       1.1  kiyohara 	sc->sc_i2c_tag.ic_exec = ichsmb_i2c_exec;
    237       1.1  kiyohara 
    238      1.20    cegger 	memset(&iba, 0, sizeof(iba));
    239       1.9       riz 	iba.iba_type = I2C_TYPE_SMBUS;
    240       1.1  kiyohara 	iba.iba_tag = &sc->sc_i2c_tag;
    241      1.42  pgoyette 	sc->sc_i2c_device = config_found_ia(self, ifattr, &iba, iicbus_print);
    242      1.42  pgoyette 
    243      1.42  pgoyette 	return 0;
    244      1.42  pgoyette }
    245      1.42  pgoyette 
    246      1.55  pgoyette static int
    247      1.55  pgoyette ichsmb_detach(device_t self, int flags)
    248      1.55  pgoyette {
    249      1.55  pgoyette 	struct ichsmb_softc *sc = device_private(self);
    250      1.55  pgoyette 	int error;
    251      1.55  pgoyette 
    252      1.55  pgoyette 	if (sc->sc_i2c_device) {
    253      1.55  pgoyette 		error = config_detach(sc->sc_i2c_device, flags);
    254      1.55  pgoyette 		if (error)
    255      1.55  pgoyette 			return error;
    256      1.55  pgoyette 	}
    257      1.55  pgoyette 
    258      1.55  pgoyette 	mutex_destroy(&sc->sc_i2c_mutex);
    259      1.55  pgoyette 
    260      1.58  jdolecek 	if (sc->sc_ih) {
    261      1.55  pgoyette 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    262      1.58  jdolecek 		sc->sc_ih = NULL;
    263      1.58  jdolecek 	}
    264      1.58  jdolecek 
    265      1.58  jdolecek 	if (sc->sc_pihp) {
    266      1.58  jdolecek 		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
    267      1.58  jdolecek 		sc->sc_pihp = NULL;
    268      1.58  jdolecek 	}
    269      1.55  pgoyette 
    270  1.60.4.1    martin 	if (sc->sc_size != 0)
    271  1.60.4.1    martin 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
    272      1.55  pgoyette 
    273      1.55  pgoyette 	return 0;
    274      1.55  pgoyette }
    275      1.55  pgoyette 
    276      1.42  pgoyette static void
    277      1.42  pgoyette ichsmb_chdet(device_t self, device_t child)
    278      1.42  pgoyette {
    279      1.42  pgoyette 	struct ichsmb_softc *sc = device_private(self);
    280      1.42  pgoyette 
    281      1.42  pgoyette 	if (sc->sc_i2c_device == child)
    282      1.42  pgoyette 		sc->sc_i2c_device = NULL;
    283       1.1  kiyohara }
    284       1.1  kiyohara 
    285       1.1  kiyohara static int
    286       1.1  kiyohara ichsmb_i2c_acquire_bus(void *cookie, int flags)
    287       1.1  kiyohara {
    288       1.1  kiyohara 	struct ichsmb_softc *sc = cookie;
    289       1.1  kiyohara 
    290      1.27  pgoyette 	if (cold)
    291       1.8   xtraeme 		return 0;
    292       1.1  kiyohara 
    293      1.27  pgoyette 	mutex_enter(&sc->sc_i2c_mutex);
    294       1.8   xtraeme 	return 0;
    295       1.1  kiyohara }
    296       1.1  kiyohara 
    297       1.1  kiyohara static void
    298       1.1  kiyohara ichsmb_i2c_release_bus(void *cookie, int flags)
    299       1.1  kiyohara {
    300       1.1  kiyohara 	struct ichsmb_softc *sc = cookie;
    301       1.1  kiyohara 
    302      1.27  pgoyette 	if (cold)
    303       1.1  kiyohara 		return;
    304       1.1  kiyohara 
    305      1.27  pgoyette 	mutex_exit(&sc->sc_i2c_mutex);
    306       1.1  kiyohara }
    307       1.1  kiyohara 
    308       1.1  kiyohara static int
    309       1.1  kiyohara ichsmb_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    310       1.1  kiyohara     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    311       1.1  kiyohara {
    312       1.1  kiyohara 	struct ichsmb_softc *sc = cookie;
    313       1.1  kiyohara 	const uint8_t *b;
    314       1.1  kiyohara 	uint8_t ctl = 0, st;
    315       1.1  kiyohara 	int retries;
    316       1.4  kiyohara 	char fbuf[64];
    317       1.1  kiyohara 
    318      1.14     njoly 	DPRINTF(("%s: exec: op %d, addr 0x%02x, cmdlen %zu, len %zu, "
    319      1.14     njoly 	    "flags 0x%02x\n", device_xname(sc->sc_dev), op, addr, cmdlen,
    320       1.1  kiyohara 	    len, flags));
    321       1.1  kiyohara 
    322      1.32     soren 	/* Clear status bits */
    323      1.32     soren 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS,
    324      1.32     soren 	    LPCIB_SMB_HS_INTR | LPCIB_SMB_HS_DEVERR |
    325      1.32     soren 	    LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED);
    326      1.32     soren 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, 1,
    327  1.60.4.2    martin 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
    328      1.32     soren 
    329       1.1  kiyohara 	/* Wait for bus to be idle */
    330       1.1  kiyohara 	for (retries = 100; retries > 0; retries--) {
    331       1.5   xtraeme 		st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
    332       1.5   xtraeme 		if (!(st & LPCIB_SMB_HS_BUSY))
    333       1.1  kiyohara 			break;
    334       1.1  kiyohara 		DELAY(ICHIIC_DELAY);
    335       1.1  kiyohara 	}
    336       1.4  kiyohara #ifdef ICHIIC_DEBUG
    337      1.18  christos 	snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    338      1.50   msaitoh 	printf("%s: exec: st %s\n", device_xname(sc->sc_dev), fbuf);
    339       1.4  kiyohara #endif
    340       1.5   xtraeme 	if (st & LPCIB_SMB_HS_BUSY)
    341       1.1  kiyohara 		return (1);
    342       1.1  kiyohara 
    343       1.1  kiyohara 	if (cold || sc->sc_poll)
    344       1.1  kiyohara 		flags |= I2C_F_POLL;
    345       1.1  kiyohara 
    346      1.24   hannken 	if (!I2C_OP_STOP_P(op) || cmdlen > 1 || len > 2 ||
    347      1.24   hannken 	    (cmdlen == 0 && len > 1))
    348       1.1  kiyohara 		return (1);
    349       1.1  kiyohara 
    350       1.1  kiyohara 	/* Setup transfer */
    351       1.1  kiyohara 	sc->sc_i2c_xfer.op = op;
    352       1.1  kiyohara 	sc->sc_i2c_xfer.buf = buf;
    353       1.1  kiyohara 	sc->sc_i2c_xfer.len = len;
    354       1.1  kiyohara 	sc->sc_i2c_xfer.flags = flags;
    355       1.1  kiyohara 	sc->sc_i2c_xfer.error = 0;
    356       1.1  kiyohara 
    357       1.1  kiyohara 	/* Set slave address and transfer direction */
    358       1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_TXSLVA,
    359       1.5   xtraeme 	    LPCIB_SMB_TXSLVA_ADDR(addr) |
    360       1.5   xtraeme 	    (I2C_OP_READ_P(op) ? LPCIB_SMB_TXSLVA_READ : 0));
    361       1.1  kiyohara 
    362       1.1  kiyohara 	b = (const uint8_t *)cmdbuf;
    363       1.1  kiyohara 	if (cmdlen > 0)
    364       1.1  kiyohara 		/* Set command byte */
    365       1.5   xtraeme 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HCMD, b[0]);
    366       1.1  kiyohara 
    367       1.1  kiyohara 	if (I2C_OP_WRITE_P(op)) {
    368       1.1  kiyohara 		/* Write data */
    369       1.1  kiyohara 		b = buf;
    370      1.24   hannken 		if (cmdlen == 0 && len == 1)
    371      1.24   hannken 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    372      1.24   hannken 			    LPCIB_SMB_HCMD, b[0]);
    373      1.24   hannken 		else if (len > 0)
    374       1.1  kiyohara 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    375       1.5   xtraeme 			    LPCIB_SMB_HD0, b[0]);
    376       1.1  kiyohara 		if (len > 1)
    377       1.1  kiyohara 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    378       1.5   xtraeme 			    LPCIB_SMB_HD1, b[1]);
    379       1.1  kiyohara 	}
    380       1.1  kiyohara 
    381       1.1  kiyohara 	/* Set SMBus command */
    382      1.24   hannken 	if (cmdlen == 0) {
    383      1.24   hannken 		if (len == 0)
    384      1.19  pgoyette 			ctl = LPCIB_SMB_HC_CMD_QUICK;
    385      1.19  pgoyette 		else
    386      1.19  pgoyette 			ctl = LPCIB_SMB_HC_CMD_BYTE;
    387      1.19  pgoyette 	} else if (len == 1)
    388       1.5   xtraeme 		ctl = LPCIB_SMB_HC_CMD_BDATA;
    389       1.1  kiyohara 	else if (len == 2)
    390       1.5   xtraeme 		ctl = LPCIB_SMB_HC_CMD_WDATA;
    391       1.1  kiyohara 
    392       1.1  kiyohara 	if ((flags & I2C_F_POLL) == 0)
    393       1.5   xtraeme 		ctl |= LPCIB_SMB_HC_INTREN;
    394       1.1  kiyohara 
    395       1.1  kiyohara 	/* Start transaction */
    396       1.5   xtraeme 	ctl |= LPCIB_SMB_HC_START;
    397       1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC, ctl);
    398       1.1  kiyohara 
    399       1.1  kiyohara 	if (flags & I2C_F_POLL) {
    400       1.1  kiyohara 		/* Poll for completion */
    401       1.1  kiyohara 		DELAY(ICHIIC_DELAY);
    402       1.1  kiyohara 		for (retries = 1000; retries > 0; retries--) {
    403       1.1  kiyohara 			st = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    404       1.5   xtraeme 			    LPCIB_SMB_HS);
    405       1.5   xtraeme 			if ((st & LPCIB_SMB_HS_BUSY) == 0)
    406       1.1  kiyohara 				break;
    407       1.1  kiyohara 			DELAY(ICHIIC_DELAY);
    408       1.1  kiyohara 		}
    409       1.5   xtraeme 		if (st & LPCIB_SMB_HS_BUSY)
    410       1.1  kiyohara 			goto timeout;
    411       1.1  kiyohara 		ichsmb_intr(sc);
    412       1.1  kiyohara 	} else {
    413       1.1  kiyohara 		/* Wait for interrupt */
    414       1.1  kiyohara 		if (tsleep(sc, PRIBIO, "iicexec", ICHIIC_TIMEOUT * hz))
    415       1.1  kiyohara 			goto timeout;
    416       1.1  kiyohara 	}
    417       1.1  kiyohara 
    418       1.1  kiyohara 	if (sc->sc_i2c_xfer.error)
    419       1.1  kiyohara 		return (1);
    420       1.1  kiyohara 
    421       1.1  kiyohara 	return (0);
    422       1.1  kiyohara 
    423       1.1  kiyohara timeout:
    424       1.1  kiyohara 	/*
    425       1.1  kiyohara 	 * Transfer timeout. Kill the transaction and clear status bits.
    426       1.1  kiyohara 	 */
    427      1.18  christos 	snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    428      1.12  kiyohara 	aprint_error_dev(sc->sc_dev,
    429      1.12  kiyohara 	    "exec: op %d, addr 0x%02x, cmdlen %zd, len %zd, "
    430      1.50   msaitoh 	    "flags 0x%02x: timeout, status %s\n",
    431      1.12  kiyohara 	    op, addr, cmdlen, len, flags, fbuf);
    432       1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HC,
    433       1.5   xtraeme 	    LPCIB_SMB_HC_KILL);
    434       1.1  kiyohara 	DELAY(ICHIIC_DELAY);
    435       1.5   xtraeme 	st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
    436       1.5   xtraeme 	if ((st & LPCIB_SMB_HS_FAILED) == 0) {
    437      1.18  christos 		snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    438      1.50   msaitoh 		aprint_error_dev(sc->sc_dev, "abort failed, status %s\n",
    439      1.12  kiyohara 		    fbuf);
    440       1.4  kiyohara 	}
    441       1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
    442       1.1  kiyohara 	return (1);
    443       1.1  kiyohara }
    444       1.1  kiyohara 
    445       1.1  kiyohara static int
    446       1.1  kiyohara ichsmb_intr(void *arg)
    447       1.1  kiyohara {
    448       1.1  kiyohara 	struct ichsmb_softc *sc = arg;
    449       1.1  kiyohara 	uint8_t st;
    450       1.1  kiyohara 	uint8_t *b;
    451       1.1  kiyohara 	size_t len;
    452       1.4  kiyohara #ifdef ICHIIC_DEBUG
    453       1.4  kiyohara 	char fbuf[64];
    454       1.4  kiyohara #endif
    455       1.1  kiyohara 
    456       1.1  kiyohara 	/* Read status */
    457       1.5   xtraeme 	st = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS);
    458       1.5   xtraeme 	if ((st & LPCIB_SMB_HS_BUSY) != 0 || (st & (LPCIB_SMB_HS_INTR |
    459       1.5   xtraeme 	    LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED |
    460       1.5   xtraeme 	    LPCIB_SMB_HS_SMBAL | LPCIB_SMB_HS_BDONE)) == 0)
    461       1.1  kiyohara 		/* Interrupt was not for us */
    462       1.1  kiyohara 		return (0);
    463       1.1  kiyohara 
    464       1.4  kiyohara #ifdef ICHIIC_DEBUG
    465      1.18  christos 	snprintb(fbuf, sizeof(fbuf), LPCIB_SMB_HS_BITS, st);
    466      1.50   msaitoh 	printf("%s: intr st %s\n", device_xname(sc->sc_dev), fbuf);
    467       1.4  kiyohara #endif
    468       1.1  kiyohara 
    469       1.1  kiyohara 	/* Clear status bits */
    470       1.5   xtraeme 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_SMB_HS, st);
    471       1.1  kiyohara 
    472       1.1  kiyohara 	/* Check for errors */
    473       1.5   xtraeme 	if (st & (LPCIB_SMB_HS_DEVERR | LPCIB_SMB_HS_BUSERR | LPCIB_SMB_HS_FAILED)) {
    474       1.1  kiyohara 		sc->sc_i2c_xfer.error = 1;
    475       1.1  kiyohara 		goto done;
    476       1.1  kiyohara 	}
    477       1.1  kiyohara 
    478       1.5   xtraeme 	if (st & LPCIB_SMB_HS_INTR) {
    479       1.1  kiyohara 		if (I2C_OP_WRITE_P(sc->sc_i2c_xfer.op))
    480       1.1  kiyohara 			goto done;
    481       1.1  kiyohara 
    482       1.1  kiyohara 		/* Read data */
    483       1.1  kiyohara 		b = sc->sc_i2c_xfer.buf;
    484       1.1  kiyohara 		len = sc->sc_i2c_xfer.len;
    485       1.1  kiyohara 		if (len > 0)
    486       1.1  kiyohara 			b[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    487       1.5   xtraeme 			    LPCIB_SMB_HD0);
    488       1.1  kiyohara 		if (len > 1)
    489       1.1  kiyohara 			b[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    490       1.5   xtraeme 			    LPCIB_SMB_HD1);
    491       1.1  kiyohara 	}
    492       1.1  kiyohara 
    493       1.1  kiyohara done:
    494       1.1  kiyohara 	if ((sc->sc_i2c_xfer.flags & I2C_F_POLL) == 0)
    495       1.1  kiyohara 		wakeup(sc);
    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