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