Home | History | Annotate | Line # | Download | only in pci
nfsmb.c revision 1.15.2.1
      1  1.15.2.1     skrll /*	$NetBSD: nfsmb.c,v 1.15.2.1 2009/03/03 18:31:07 skrll Exp $	*/
      2       1.1  kiyohara /*
      3       1.1  kiyohara  * Copyright (c) 2007 KIYOHARA Takashi
      4       1.1  kiyohara  * All rights reserved.
      5       1.1  kiyohara  *
      6       1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7       1.1  kiyohara  * modification, are permitted provided that the following conditions
      8       1.1  kiyohara  * are met:
      9       1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10       1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11       1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14       1.1  kiyohara  *
     15       1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16       1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19       1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20       1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23       1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24       1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25       1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26       1.1  kiyohara  *
     27       1.1  kiyohara  */
     28       1.1  kiyohara #include <sys/cdefs.h>
     29  1.15.2.1     skrll __KERNEL_RCSID(0, "$NetBSD: nfsmb.c,v 1.15.2.1 2009/03/03 18:31:07 skrll Exp $");
     30       1.1  kiyohara 
     31       1.1  kiyohara #include <sys/param.h>
     32       1.1  kiyohara #include <sys/device.h>
     33       1.1  kiyohara #include <sys/errno.h>
     34       1.1  kiyohara #include <sys/kernel.h>
     35       1.5   xtraeme #include <sys/rwlock.h>
     36       1.1  kiyohara #include <sys/proc.h>
     37       1.1  kiyohara 
     38       1.7        ad #include <sys/bus.h>
     39       1.1  kiyohara 
     40       1.1  kiyohara #include <dev/i2c/i2cvar.h>
     41       1.1  kiyohara 
     42       1.1  kiyohara #include <dev/pci/pcivar.h>
     43       1.1  kiyohara #include <dev/pci/pcireg.h>
     44       1.1  kiyohara #include <dev/pci/pcidevs.h>
     45       1.1  kiyohara 
     46       1.1  kiyohara #include <dev/pci/nfsmbreg.h>
     47       1.1  kiyohara 
     48       1.1  kiyohara 
     49       1.1  kiyohara struct nfsmbc_attach_args {
     50       1.1  kiyohara 	int nfsmb_num;
     51       1.1  kiyohara 	bus_space_tag_t nfsmb_iot;
     52       1.1  kiyohara 	int nfsmb_addr;
     53       1.1  kiyohara };
     54       1.1  kiyohara 
     55       1.1  kiyohara struct nfsmb_softc;
     56       1.1  kiyohara struct nfsmbc_softc {
     57      1.12  kiyohara 	device_t sc_dev;
     58       1.1  kiyohara 
     59       1.1  kiyohara 	pci_chipset_tag_t sc_pc;
     60       1.1  kiyohara 	pcitag_t sc_tag;
     61       1.1  kiyohara 	struct pci_attach_args *sc_pa;
     62       1.1  kiyohara 
     63       1.1  kiyohara 	bus_space_tag_t sc_iot;
     64       1.1  kiyohara 	struct device *sc_nfsmb[2];
     65       1.1  kiyohara };
     66       1.1  kiyohara 
     67       1.1  kiyohara struct nfsmb_softc {
     68      1.12  kiyohara 	device_t sc_dev;
     69       1.1  kiyohara 	int sc_num;
     70       1.1  kiyohara 	struct device *sc_nfsmbc;
     71       1.1  kiyohara 
     72       1.1  kiyohara 	bus_space_tag_t sc_iot;
     73       1.1  kiyohara 	bus_space_handle_t sc_ioh;
     74       1.1  kiyohara 
     75       1.1  kiyohara 	struct i2c_controller sc_i2c;	/* i2c controller info */
     76       1.5   xtraeme 	krwlock_t sc_rwlock;
     77       1.1  kiyohara };
     78       1.1  kiyohara 
     79       1.1  kiyohara 
     80      1.12  kiyohara static int nfsmbc_match(device_t, struct cfdata *, void *);
     81      1.12  kiyohara static void nfsmbc_attach(device_t, device_t, void *);
     82       1.1  kiyohara static int nfsmbc_print(void *, const char *);
     83       1.1  kiyohara 
     84      1.12  kiyohara static int nfsmb_match(device_t, struct cfdata *, void *);
     85      1.12  kiyohara static void nfsmb_attach(device_t, device_t, void *);
     86       1.1  kiyohara static int nfsmb_acquire_bus(void *, int);
     87       1.1  kiyohara static void nfsmb_release_bus(void *, int);
     88       1.1  kiyohara static int nfsmb_exec(
     89       1.1  kiyohara     void *, i2c_op_t, i2c_addr_t, const void *, size_t, void *, size_t, int);
     90       1.1  kiyohara static int nfsmb_check_done(struct nfsmb_softc *);
     91       1.1  kiyohara static int
     92       1.1  kiyohara     nfsmb_send_1(struct nfsmb_softc *, uint8_t, i2c_addr_t, i2c_op_t, int);
     93       1.1  kiyohara static int nfsmb_write_1(
     94       1.1  kiyohara     struct nfsmb_softc *, uint8_t, uint8_t, i2c_addr_t, i2c_op_t, int);
     95       1.1  kiyohara static int nfsmb_write_2(
     96       1.1  kiyohara     struct nfsmb_softc *, uint8_t, uint16_t, i2c_addr_t, i2c_op_t, int);
     97       1.1  kiyohara static int nfsmb_receive_1(struct nfsmb_softc *, i2c_addr_t, i2c_op_t, int);
     98       1.1  kiyohara static int
     99       1.1  kiyohara     nfsmb_read_1(struct nfsmb_softc *, uint8_t, i2c_addr_t, i2c_op_t, int);
    100       1.1  kiyohara static int
    101       1.1  kiyohara     nfsmb_read_2(struct nfsmb_softc *, uint8_t, i2c_addr_t, i2c_op_t, int);
    102  1.15.2.1     skrll static int
    103  1.15.2.1     skrll     nfsmb_quick(struct nfsmb_softc *, i2c_addr_t, i2c_op_t, int);
    104       1.1  kiyohara 
    105      1.12  kiyohara CFATTACH_DECL_NEW(nfsmbc, sizeof(struct nfsmbc_softc),
    106       1.1  kiyohara     nfsmbc_match, nfsmbc_attach, NULL, NULL);
    107       1.1  kiyohara 
    108       1.1  kiyohara static int
    109      1.12  kiyohara nfsmbc_match(device_t parent, struct cfdata *match, void *aux)
    110       1.1  kiyohara {
    111       1.1  kiyohara 	struct pci_attach_args *pa = aux;
    112       1.1  kiyohara 
    113       1.1  kiyohara 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NVIDIA) {
    114       1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    115       1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE2_SMBUS:
    116       1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE2_400_SMBUS:
    117       1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE3_SMBUS:
    118       1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE3_250_SMBUS:
    119       1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE4_SMBUS:
    120       1.8   xtraeme 		case PCI_PRODUCT_NVIDIA_NFORCE430_SMBUS:
    121       1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_MCP04_SMBUS:
    122       1.8   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP55_SMB:
    123       1.8   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP61_SMB:
    124       1.8   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP65_SMB:
    125       1.8   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP67_SMB:
    126       1.9   xtraeme 		case PCI_PRODUCT_NVIDIA_MCP73_SMB:
    127       1.1  kiyohara 			return 1;
    128       1.1  kiyohara 		}
    129       1.1  kiyohara 	}
    130       1.1  kiyohara 
    131       1.1  kiyohara 	return 0;
    132       1.1  kiyohara }
    133       1.1  kiyohara 
    134       1.1  kiyohara static void
    135      1.12  kiyohara nfsmbc_attach(device_t parent, device_t self, void *aux)
    136       1.1  kiyohara {
    137      1.12  kiyohara 	struct nfsmbc_softc *sc = device_private(self);
    138       1.1  kiyohara 	struct pci_attach_args *pa = aux;
    139       1.1  kiyohara 	struct nfsmbc_attach_args nfsmbca;
    140       1.3  kiyohara 	pcireg_t reg;
    141      1.10   xtraeme 	int baseregs[2];
    142       1.1  kiyohara 	char devinfo[256];
    143       1.1  kiyohara 
    144       1.1  kiyohara 	aprint_naive("\n");
    145       1.1  kiyohara 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    146       1.1  kiyohara 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    147       1.1  kiyohara 	    PCI_REVISION(pa->pa_class));
    148       1.1  kiyohara 
    149      1.12  kiyohara 	sc->sc_dev = self;
    150       1.1  kiyohara 	sc->sc_pc = pa->pa_pc;
    151       1.1  kiyohara 	sc->sc_tag = pa->pa_tag;
    152       1.1  kiyohara 	sc->sc_pa = pa;
    153       1.1  kiyohara 	sc->sc_iot = pa->pa_iot;
    154       1.1  kiyohara 
    155       1.1  kiyohara 	nfsmbca.nfsmb_iot = sc->sc_iot;
    156       1.1  kiyohara 
    157      1.10   xtraeme 	switch (PCI_PRODUCT(pa->pa_id)) {
    158      1.10   xtraeme 	case PCI_PRODUCT_NVIDIA_NFORCE2_SMBUS:
    159      1.10   xtraeme 	case PCI_PRODUCT_NVIDIA_NFORCE2_400_SMBUS:
    160      1.10   xtraeme 	case PCI_PRODUCT_NVIDIA_NFORCE3_SMBUS:
    161      1.10   xtraeme 	case PCI_PRODUCT_NVIDIA_NFORCE3_250_SMBUS:
    162      1.10   xtraeme 	case PCI_PRODUCT_NVIDIA_NFORCE4_SMBUS:
    163      1.10   xtraeme 		baseregs[0] = NFORCE_OLD_SMB1;
    164      1.10   xtraeme 		baseregs[1] = NFORCE_OLD_SMB2;
    165      1.10   xtraeme 		break;
    166      1.10   xtraeme 	default:
    167      1.10   xtraeme 		baseregs[0] = NFORCE_SMB1;
    168      1.10   xtraeme 		baseregs[1] = NFORCE_SMB2;
    169      1.10   xtraeme 		break;
    170      1.10   xtraeme 	}
    171      1.10   xtraeme 
    172      1.10   xtraeme 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, baseregs[0]);
    173       1.1  kiyohara 	nfsmbca.nfsmb_num = 1;
    174       1.1  kiyohara 	nfsmbca.nfsmb_addr = NFORCE_SMBBASE(reg);
    175      1.12  kiyohara 	sc->sc_nfsmb[0] = config_found(sc->sc_dev, &nfsmbca, nfsmbc_print);
    176       1.1  kiyohara 
    177      1.10   xtraeme 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, baseregs[1]);
    178       1.1  kiyohara 	nfsmbca.nfsmb_num = 2;
    179       1.1  kiyohara 	nfsmbca.nfsmb_addr = NFORCE_SMBBASE(reg);
    180      1.12  kiyohara 	sc->sc_nfsmb[1] = config_found(sc->sc_dev, &nfsmbca, nfsmbc_print);
    181      1.13    cegger 
    182      1.14    cegger 	/* This driver is similar to an ISA bridge that doesn't
    183      1.14    cegger 	 * need any special handling. So registering NULL handlers
    184      1.14    cegger 	 * are sufficent. */
    185      1.13    cegger 	if (!pmf_device_register(self, NULL, NULL))
    186      1.13    cegger 		aprint_error_dev(self, "couldn't establish power handler\n");
    187       1.1  kiyohara }
    188       1.1  kiyohara 
    189       1.1  kiyohara static int
    190       1.1  kiyohara nfsmbc_print(void *aux, const char *pnp)
    191       1.1  kiyohara {
    192       1.1  kiyohara 	struct nfsmbc_attach_args *nfsmbcap = aux;
    193       1.1  kiyohara 
    194       1.1  kiyohara 	if (pnp)
    195       1.1  kiyohara 		aprint_normal("nfsmb SMBus %d at %s",
    196       1.1  kiyohara 		    nfsmbcap->nfsmb_num, pnp);
    197       1.1  kiyohara 	else
    198       1.1  kiyohara 		aprint_normal(" SMBus %d", nfsmbcap->nfsmb_num);
    199       1.1  kiyohara 	return UNCONF;
    200       1.1  kiyohara }
    201       1.1  kiyohara 
    202       1.1  kiyohara 
    203      1.12  kiyohara CFATTACH_DECL_NEW(nfsmb, sizeof(struct nfsmb_softc),
    204       1.1  kiyohara     nfsmb_match, nfsmb_attach, NULL, NULL);
    205       1.1  kiyohara 
    206       1.1  kiyohara static int
    207      1.12  kiyohara nfsmb_match(device_t parent, struct cfdata *match, void *aux)
    208       1.1  kiyohara {
    209       1.1  kiyohara 	struct nfsmbc_attach_args *nfsmbcap = aux;
    210       1.1  kiyohara 
    211       1.1  kiyohara 	if (nfsmbcap->nfsmb_num == 1 || nfsmbcap->nfsmb_num == 2)
    212       1.1  kiyohara 		return 1;
    213       1.1  kiyohara 	return 0;
    214       1.1  kiyohara }
    215       1.1  kiyohara 
    216       1.1  kiyohara static void
    217      1.12  kiyohara nfsmb_attach(device_t parent, device_t self, void *aux)
    218       1.1  kiyohara {
    219      1.12  kiyohara 	struct nfsmb_softc *sc = device_private(self);
    220       1.1  kiyohara 	struct nfsmbc_attach_args *nfsmbcap = aux;
    221       1.1  kiyohara 	struct i2cbus_attach_args iba;
    222       1.1  kiyohara 
    223       1.1  kiyohara 	aprint_naive("\n");
    224       1.1  kiyohara 	aprint_normal("\n");
    225       1.1  kiyohara 
    226      1.12  kiyohara 	sc->sc_dev = self;
    227       1.1  kiyohara 	sc->sc_nfsmbc = parent;
    228       1.1  kiyohara 	sc->sc_num = nfsmbcap->nfsmb_num;
    229       1.1  kiyohara 	sc->sc_iot = nfsmbcap->nfsmb_iot;
    230       1.1  kiyohara 
    231       1.1  kiyohara 	/* register with iic */
    232       1.1  kiyohara 	sc->sc_i2c.ic_cookie = sc;
    233       1.1  kiyohara 	sc->sc_i2c.ic_acquire_bus = nfsmb_acquire_bus;
    234       1.1  kiyohara 	sc->sc_i2c.ic_release_bus = nfsmb_release_bus;
    235       1.1  kiyohara 	sc->sc_i2c.ic_send_start = NULL;
    236       1.1  kiyohara 	sc->sc_i2c.ic_send_stop = NULL;
    237       1.1  kiyohara 	sc->sc_i2c.ic_initiate_xfer = NULL;
    238       1.1  kiyohara 	sc->sc_i2c.ic_read_byte = NULL;
    239       1.1  kiyohara 	sc->sc_i2c.ic_write_byte = NULL;
    240       1.1  kiyohara 	sc->sc_i2c.ic_exec = nfsmb_exec;
    241       1.1  kiyohara 
    242       1.5   xtraeme 	rw_init(&sc->sc_rwlock);
    243       1.1  kiyohara 
    244       1.1  kiyohara 	if (bus_space_map(sc->sc_iot, nfsmbcap->nfsmb_addr, NFORCE_SMBSIZE, 0,
    245       1.3  kiyohara 	    &sc->sc_ioh) != 0) {
    246      1.12  kiyohara 		aprint_error_dev(self, "failed to map SMBus space\n");
    247       1.1  kiyohara 		return;
    248       1.1  kiyohara 	}
    249       1.1  kiyohara 
    250       1.6       riz 	iba.iba_type = I2C_TYPE_SMBUS;
    251       1.1  kiyohara 	iba.iba_tag = &sc->sc_i2c;
    252      1.12  kiyohara 	(void) config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
    253      1.13    cegger 
    254      1.14    cegger 	/* This driver is similar to an ISA bridge that doesn't
    255      1.14    cegger 	 * need any special handling. So registering NULL handlers
    256      1.14    cegger 	 * are sufficent. */
    257      1.13    cegger 	if (!pmf_device_register(self, NULL, NULL))
    258      1.13    cegger 		aprint_error_dev(self, "couldn't establish power handler\n");
    259       1.1  kiyohara }
    260       1.1  kiyohara 
    261       1.1  kiyohara static int
    262       1.1  kiyohara nfsmb_acquire_bus(void *cookie, int flags)
    263       1.1  kiyohara {
    264       1.1  kiyohara 	struct nfsmb_softc *sc = cookie;
    265       1.1  kiyohara 
    266       1.5   xtraeme 	rw_enter(&sc->sc_rwlock, RW_WRITER);
    267       1.5   xtraeme 	return 0;
    268       1.1  kiyohara }
    269       1.1  kiyohara 
    270       1.1  kiyohara static void
    271       1.1  kiyohara nfsmb_release_bus(void *cookie, int flags)
    272       1.1  kiyohara {
    273       1.1  kiyohara 	struct nfsmb_softc *sc = cookie;
    274       1.1  kiyohara 
    275       1.5   xtraeme 	rw_exit(&sc->sc_rwlock);
    276       1.1  kiyohara }
    277       1.1  kiyohara 
    278       1.1  kiyohara static int
    279       1.1  kiyohara nfsmb_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
    280       1.1  kiyohara 	   size_t cmdlen, void *vbuf, size_t buflen, int flags)
    281       1.1  kiyohara {
    282       1.1  kiyohara 	struct nfsmb_softc *sc  = (struct nfsmb_softc *)cookie;
    283       1.1  kiyohara 	uint8_t *p = vbuf;
    284       1.1  kiyohara 	int rv;
    285       1.1  kiyohara 
    286  1.15.2.1     skrll 	if ((cmdlen == 0) && (buflen == 0)) {
    287  1.15.2.1     skrll 		return nfsmb_quick(sc, addr, op, flags);
    288  1.15.2.1     skrll 	}
    289  1.15.2.1     skrll 
    290       1.1  kiyohara 	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
    291       1.1  kiyohara 		rv = nfsmb_receive_1(sc, addr, op, flags);
    292       1.1  kiyohara 		if (rv == -1)
    293       1.1  kiyohara 			return -1;
    294       1.1  kiyohara 		*p = (uint8_t)rv;
    295       1.1  kiyohara 		return 0;
    296       1.1  kiyohara 	}
    297       1.1  kiyohara 
    298       1.1  kiyohara 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    299       1.1  kiyohara 		rv = nfsmb_read_1(sc, *(const uint8_t*)cmd, addr, op, flags);
    300       1.1  kiyohara 		if (rv == -1)
    301       1.1  kiyohara 			return -1;
    302       1.1  kiyohara 		*p = (uint8_t)rv;
    303       1.1  kiyohara 		return 0;
    304       1.1  kiyohara 	}
    305       1.4  kiyohara 
    306       1.1  kiyohara 	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
    307       1.1  kiyohara 		rv = nfsmb_read_2(sc, *(const uint8_t*)cmd, addr, op, flags);
    308       1.1  kiyohara 		if (rv == -1)
    309       1.1  kiyohara 			return -1;
    310      1.15  pgoyette 		*(uint16_t *)p = (uint16_t)rv;
    311       1.1  kiyohara 		return 0;
    312       1.1  kiyohara 	}
    313       1.1  kiyohara 
    314       1.1  kiyohara 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
    315       1.1  kiyohara 		return nfsmb_send_1(sc, *(uint8_t*)vbuf, addr, op, flags);
    316       1.1  kiyohara 
    317       1.1  kiyohara 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1))
    318       1.1  kiyohara 		return nfsmb_write_1(sc, *(const uint8_t*)cmd, *(uint8_t*)vbuf,
    319       1.1  kiyohara 		    addr, op, flags);
    320       1.1  kiyohara 
    321       1.1  kiyohara 	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2))
    322       1.1  kiyohara 		return nfsmb_write_2(sc,
    323       1.1  kiyohara 		    *(const uint8_t*)cmd, *((uint16_t *)vbuf), addr, op, flags);
    324       1.1  kiyohara 
    325       1.4  kiyohara 	return -1;
    326       1.1  kiyohara }
    327       1.1  kiyohara 
    328       1.1  kiyohara static int
    329       1.1  kiyohara nfsmb_check_done(struct nfsmb_softc *sc)
    330       1.1  kiyohara {
    331       1.2  kiyohara 	int us;
    332       1.1  kiyohara 	uint8_t stat;
    333       1.1  kiyohara 
    334       1.2  kiyohara 	us = 10 * 1000;	/* XXXX: wait maximum 10 msec */
    335       1.2  kiyohara 	do {
    336       1.2  kiyohara 		delay(10);
    337       1.2  kiyohara 		us -= 10;
    338       1.2  kiyohara 		if (us <= 0)
    339       1.2  kiyohara 			return -1;
    340       1.2  kiyohara 	} while (bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    341       1.2  kiyohara 	    NFORCE_SMB_PROTOCOL) != 0);
    342       1.2  kiyohara 
    343       1.1  kiyohara 	stat = bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_STATUS);
    344       1.1  kiyohara 	if ((stat & NFORCE_SMB_STATUS_DONE) &&
    345       1.1  kiyohara 	    !(stat & NFORCE_SMB_STATUS_STATUS))
    346       1.1  kiyohara 		return 0;
    347       1.1  kiyohara 	return -1;
    348       1.1  kiyohara }
    349       1.1  kiyohara 
    350       1.1  kiyohara /* ARGSUSED */
    351       1.1  kiyohara static int
    352  1.15.2.1     skrll nfsmb_quick(struct nfsmb_softc *sc, i2c_addr_t addr, i2c_op_t op, int flags)
    353  1.15.2.1     skrll {
    354  1.15.2.1     skrll 	uint8_t data;
    355  1.15.2.1     skrll 
    356  1.15.2.1     skrll 	/* write smbus slave address to register */
    357  1.15.2.1     skrll 	data = addr << 1;
    358  1.15.2.1     skrll 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    359  1.15.2.1     skrll 
    360  1.15.2.1     skrll 	/* write smbus protocol to register */
    361  1.15.2.1     skrll 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_QUICK;
    362  1.15.2.1     skrll 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    363  1.15.2.1     skrll 
    364  1.15.2.1     skrll 	return nfsmb_check_done(sc);
    365  1.15.2.1     skrll }
    366  1.15.2.1     skrll 
    367  1.15.2.1     skrll /* ARGSUSED */
    368  1.15.2.1     skrll static int
    369       1.1  kiyohara nfsmb_send_1(struct nfsmb_softc *sc, uint8_t val, i2c_addr_t addr, i2c_op_t op,
    370       1.1  kiyohara 	     int flags)
    371       1.1  kiyohara {
    372       1.1  kiyohara 	uint8_t data;
    373       1.1  kiyohara 
    374       1.1  kiyohara 	/* store cmd */
    375       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, val);
    376       1.1  kiyohara 
    377       1.1  kiyohara 	/* write smbus slave address to register */
    378       1.1  kiyohara 	data = addr << 1;
    379       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    380       1.1  kiyohara 
    381       1.1  kiyohara 	/* write smbus protocol to register */
    382       1.1  kiyohara 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE;
    383       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    384       1.1  kiyohara 
    385       1.1  kiyohara 	return nfsmb_check_done(sc);
    386       1.1  kiyohara }
    387       1.1  kiyohara 
    388       1.1  kiyohara /* ARGSUSED */
    389       1.1  kiyohara static int
    390       1.1  kiyohara nfsmb_write_1(struct nfsmb_softc *sc, uint8_t cmd, uint8_t val, i2c_addr_t addr,
    391       1.1  kiyohara 	      i2c_op_t op, int flags)
    392       1.1  kiyohara {
    393       1.1  kiyohara 	uint8_t data;
    394       1.1  kiyohara 
    395       1.1  kiyohara 	/* store cmd */
    396       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
    397       1.1  kiyohara 
    398       1.1  kiyohara 	/* store data */
    399       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA, val);
    400       1.1  kiyohara 
    401       1.1  kiyohara 	/* write smbus slave address to register */
    402       1.1  kiyohara 	data = addr << 1;
    403       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    404       1.1  kiyohara 
    405       1.1  kiyohara 	/* write smbus protocol to register */
    406       1.1  kiyohara 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE_DATA;
    407       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    408       1.1  kiyohara 
    409       1.1  kiyohara 	return nfsmb_check_done(sc);
    410       1.1  kiyohara }
    411       1.1  kiyohara 
    412       1.1  kiyohara static int
    413       1.1  kiyohara nfsmb_write_2(struct nfsmb_softc *sc, uint8_t cmd, uint16_t val,
    414       1.1  kiyohara 	      i2c_addr_t addr, i2c_op_t op, int flags)
    415       1.1  kiyohara {
    416       1.1  kiyohara 	uint8_t data, low, high;
    417       1.1  kiyohara 
    418       1.1  kiyohara 	/* store cmd */
    419       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
    420       1.1  kiyohara 
    421       1.1  kiyohara 	/* store data */
    422       1.1  kiyohara 	low = val;
    423       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA, low);
    424       1.1  kiyohara 	high = val >> 8;
    425       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA, high);
    426       1.1  kiyohara 
    427       1.1  kiyohara 	/* write smbus slave address to register */
    428       1.1  kiyohara 	data = addr << 1;
    429       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    430       1.1  kiyohara 
    431       1.1  kiyohara 	/* write smbus protocol to register */
    432       1.1  kiyohara 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_WORD_DATA;
    433       1.1  kiyohara 	if (flags & I2C_F_PEC)
    434       1.1  kiyohara 		data |= NFORCE_SMB_PROTOCOL_PEC;
    435       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    436       1.1  kiyohara 
    437       1.1  kiyohara 	return nfsmb_check_done(sc);
    438       1.1  kiyohara }
    439       1.1  kiyohara 
    440       1.1  kiyohara /* ARGSUSED */
    441       1.1  kiyohara static int
    442       1.1  kiyohara nfsmb_receive_1(struct nfsmb_softc *sc, i2c_addr_t addr, i2c_op_t op, int flags)
    443       1.1  kiyohara {
    444       1.1  kiyohara 	uint8_t data;
    445       1.1  kiyohara 
    446       1.1  kiyohara 	/* write smbus slave address to register */
    447       1.1  kiyohara 	data = addr << 1;
    448       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    449       1.1  kiyohara 
    450       1.1  kiyohara 	/* write smbus protocol to register */
    451       1.1  kiyohara 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE;
    452       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    453       1.1  kiyohara 
    454       1.1  kiyohara 	/* check for errors */
    455       1.1  kiyohara 	if (nfsmb_check_done(sc) < 0)
    456       1.1  kiyohara 		return -1;
    457       1.1  kiyohara 
    458       1.1  kiyohara 	/* read data */
    459       1.1  kiyohara 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
    460       1.1  kiyohara }
    461       1.1  kiyohara 
    462       1.1  kiyohara /* ARGSUSED */
    463       1.1  kiyohara static int
    464       1.1  kiyohara nfsmb_read_1(struct nfsmb_softc *sc, uint8_t cmd, i2c_addr_t addr, i2c_op_t op,
    465       1.1  kiyohara 	     int flags)
    466       1.1  kiyohara {
    467       1.1  kiyohara 	uint8_t data;
    468       1.1  kiyohara 
    469       1.1  kiyohara 	/* store cmd */
    470       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
    471       1.1  kiyohara 
    472       1.1  kiyohara 	/* write smbus slave address to register */
    473       1.1  kiyohara 	data = addr << 1;
    474       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    475       1.1  kiyohara 
    476       1.1  kiyohara 	/* write smbus protocol to register */
    477       1.1  kiyohara 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE_DATA;
    478       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    479       1.1  kiyohara 
    480       1.1  kiyohara 	/* check for errors */
    481       1.1  kiyohara 	if (nfsmb_check_done(sc) < 0)
    482       1.4  kiyohara 		return -1;
    483       1.1  kiyohara 
    484       1.1  kiyohara 	/* read data */
    485       1.1  kiyohara 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
    486       1.1  kiyohara }
    487       1.1  kiyohara 
    488       1.1  kiyohara static int
    489       1.1  kiyohara nfsmb_read_2(struct nfsmb_softc *sc, uint8_t cmd, i2c_addr_t addr, i2c_op_t op,
    490       1.1  kiyohara 	     int flags)
    491       1.1  kiyohara {
    492       1.1  kiyohara 	uint8_t data, low, high;
    493       1.1  kiyohara 
    494       1.1  kiyohara 	/* store cmd */
    495       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_COMMAND, cmd);
    496       1.1  kiyohara 
    497       1.1  kiyohara 	/* write smbus slave address to register */
    498       1.1  kiyohara 	data = addr << 1;
    499       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_ADDRESS, data);
    500       1.1  kiyohara 
    501       1.1  kiyohara 	/* write smbus protocol to register */
    502       1.1  kiyohara 	data = I2C_OP_READ_P(op) | NFORCE_SMB_PROTOCOL_BYTE_DATA;
    503       1.1  kiyohara 	if (flags & I2C_F_PEC)
    504       1.1  kiyohara 		data |= NFORCE_SMB_PROTOCOL_PEC;
    505       1.1  kiyohara 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_PROTOCOL, data);
    506       1.1  kiyohara 
    507       1.1  kiyohara 	/* check for errors */
    508       1.1  kiyohara 	if (nfsmb_check_done(sc) < 0)
    509       1.4  kiyohara 		return -1;
    510       1.1  kiyohara 
    511       1.1  kiyohara 	/* read data */
    512       1.1  kiyohara 	low = bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
    513       1.1  kiyohara 	high = bus_space_read_1(sc->sc_iot, sc->sc_ioh, NFORCE_SMB_DATA);
    514       1.1  kiyohara 	return low | high << 8;
    515       1.1  kiyohara }
    516