Home | History | Annotate | Line # | Download | only in imcsmb
imcsmb.c revision 1.2
      1  1.2  pgoyette /* $NetBSD: imcsmb.c,v 1.2 2018/03/03 05:27:02 pgoyette Exp $ */
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pgoyette  * by Paul Goyette
      9  1.1  pgoyette  *
     10  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11  1.1  pgoyette  * modification, are permitted provided that the following conditions
     12  1.1  pgoyette  * are met:
     13  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18  1.1  pgoyette  *
     19  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  pgoyette  */
     31  1.1  pgoyette 
     32  1.1  pgoyette /*-
     33  1.1  pgoyette  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
     34  1.1  pgoyette  *
     35  1.1  pgoyette  * Authors: Joe Kloss; Ravi Pokala (rpokala (at) freebsd.org)
     36  1.1  pgoyette  *
     37  1.1  pgoyette  * Copyright (c) 2017-2018 Panasas
     38  1.1  pgoyette  * All rights reserved.
     39  1.1  pgoyette  *
     40  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     41  1.1  pgoyette  * modification, are permitted provided that the following conditions
     42  1.1  pgoyette  * are met:
     43  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     44  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     45  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     46  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     47  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     48  1.1  pgoyette  *
     49  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     50  1.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  1.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  1.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     53  1.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  1.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  1.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  1.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  1.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  1.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  1.1  pgoyette  * SUCH DAMAGE.
     60  1.1  pgoyette  */
     61  1.1  pgoyette 
     62  1.1  pgoyette /*
     63  1.1  pgoyette  * Driver for the SMBus controllers in Intel's Integrated Memory Controllers
     64  1.1  pgoyette  * in certain CPUs.  A more detailed description of this device is present
     65  1.1  pgoyette  * in imc.c
     66  1.1  pgoyette  */
     67  1.1  pgoyette 
     68  1.1  pgoyette #include <sys/cdefs.h>
     69  1.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: imcsmb.c,v 1.2 2018/03/03 05:27:02 pgoyette Exp $");
     70  1.1  pgoyette 
     71  1.1  pgoyette #include <sys/param.h>
     72  1.1  pgoyette #include <sys/kernel.h>
     73  1.1  pgoyette #include <sys/module.h>
     74  1.1  pgoyette #include <sys/endian.h>
     75  1.1  pgoyette #include <sys/errno.h>
     76  1.1  pgoyette #include <sys/mutex.h>
     77  1.1  pgoyette #include <sys/bus.h>
     78  1.1  pgoyette 
     79  1.1  pgoyette #include <dev/pci/pcidevs.h>
     80  1.1  pgoyette #include <dev/pci/pcivar.h>
     81  1.1  pgoyette #include <dev/pci/pcireg.h>
     82  1.1  pgoyette 
     83  1.1  pgoyette #include <dev/i2c/i2cvar.h>
     84  1.1  pgoyette 
     85  1.1  pgoyette #include "imcsmb_reg.h"
     86  1.1  pgoyette #include "imcsmb_var.h"
     87  1.1  pgoyette 
     88  1.1  pgoyette /* Device methods */
     89  1.1  pgoyette static int  imcsmb_probe(device_t, cfdata_t, void *);
     90  1.1  pgoyette static void imcsmb_attach(device_t, device_t, void *);
     91  1.1  pgoyette static int  imcsmb_detach(device_t, int flags);
     92  1.1  pgoyette static int  imcsmb_rescan(device_t, const char *, const int *);
     93  1.1  pgoyette static void imcsmb_chdet(device_t, device_t);
     94  1.1  pgoyette 
     95  1.1  pgoyette CFATTACH_DECL3_NEW(imcsmb, sizeof(struct imcsmb_softc),
     96  1.1  pgoyette     imcsmb_probe, imcsmb_attach, imcsmb_detach, NULL, imcsmb_rescan,
     97  1.1  pgoyette     imcsmb_chdet, 0);
     98  1.1  pgoyette 
     99  1.1  pgoyette /* Bus access control methods */
    100  1.1  pgoyette static int imcsmb_acquire_bus(void *cookie, int flags);
    101  1.1  pgoyette static void imcsmb_release_bus(void *cookie, int flags);
    102  1.1  pgoyette 
    103  1.1  pgoyette /* SMBus methods */
    104  1.1  pgoyette static int imcsmb_exec(void *cookie, i2c_op_t, i2c_addr_t, const void *,
    105  1.1  pgoyette     size_t, void *, size_t, int);
    106  1.1  pgoyette 
    107  1.1  pgoyette /**
    108  1.1  pgoyette  * device_attach() method. Set up the softc, including getting the set of the
    109  1.1  pgoyette  * parent imcsmb_pci's registers that we will use. Create the smbus(4) device,
    110  1.1  pgoyette  * which any SMBus slave device drivers will connect to. Probe and attach
    111  1.1  pgoyette  * anything which might be downstream.
    112  1.1  pgoyette  *
    113  1.1  pgoyette  * @author rpokala
    114  1.1  pgoyette  *
    115  1.1  pgoyette  * @param[in,out] dev
    116  1.1  pgoyette  *      Device being attached.
    117  1.1  pgoyette  */
    118  1.1  pgoyette 
    119  1.1  pgoyette static void
    120  1.1  pgoyette imcsmb_attach(device_t parent, device_t self, void *aux)
    121  1.1  pgoyette {
    122  1.1  pgoyette 	struct imcsmb_softc *sc = device_private(self);
    123  1.1  pgoyette 	struct imc_attach_args *imca = aux;
    124  1.1  pgoyette 
    125  1.1  pgoyette 	aprint_naive("\n");
    126  1.2  pgoyette 	aprint_normal(": SMBus controller\n");
    127  1.1  pgoyette 
    128  1.1  pgoyette 	/* Initialize private state */
    129  1.1  pgoyette 	sc->sc_dev = self;
    130  1.1  pgoyette 	sc->sc_regs = imca->ia_regs;
    131  1.1  pgoyette 	sc->sc_pci_tag = imca->ia_pci_tag;
    132  1.1  pgoyette 	sc->sc_pci_chipset_tag = imca->ia_pci_chipset_tag;
    133  1.1  pgoyette 	mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
    134  1.1  pgoyette 
    135  1.1  pgoyette 	if (!pmf_device_register(self, NULL, NULL))
    136  1.1  pgoyette 		aprint_error_dev(self, "couldn't establish power handler\n");
    137  1.1  pgoyette 
    138  1.1  pgoyette 	imcsmb_rescan(self, "i2cbus", 0);
    139  1.1  pgoyette }
    140  1.1  pgoyette 
    141  1.1  pgoyette static int
    142  1.1  pgoyette imcsmb_rescan(device_t self, const char *ifattr, const int *flags)
    143  1.1  pgoyette {
    144  1.1  pgoyette 	struct imcsmb_softc *sc = device_private(self);
    145  1.1  pgoyette 	struct i2cbus_attach_args iba;
    146  1.1  pgoyette 
    147  1.1  pgoyette 	if (!ifattr_match(ifattr, "i2cbus"))
    148  1.1  pgoyette 		return 0;
    149  1.1  pgoyette 
    150  1.1  pgoyette 	/* Create the i2cbus child */
    151  1.1  pgoyette 	if (sc->sc_smbus != NULL)
    152  1.1  pgoyette 		return 0;
    153  1.1  pgoyette 
    154  1.1  pgoyette 	sc->sc_i2c_tag.ic_cookie = sc;
    155  1.1  pgoyette 	sc->sc_i2c_tag.ic_acquire_bus = imcsmb_acquire_bus;
    156  1.1  pgoyette 	sc->sc_i2c_tag.ic_release_bus = imcsmb_release_bus;
    157  1.1  pgoyette 	sc->sc_i2c_tag.ic_exec = imcsmb_exec;
    158  1.1  pgoyette 
    159  1.1  pgoyette 	memset(&iba, 0, sizeof(iba));
    160  1.1  pgoyette 	iba.iba_type = I2C_TYPE_SMBUS;
    161  1.1  pgoyette 	iba.iba_tag = &sc->sc_i2c_tag;
    162  1.1  pgoyette 	sc->sc_smbus = config_found_ia(self, ifattr, &iba, iicbus_print);
    163  1.1  pgoyette 
    164  1.1  pgoyette 	if (sc->sc_smbus == NULL) {
    165  1.1  pgoyette 		aprint_normal_dev(self, "no child found\n");
    166  1.1  pgoyette 		return ENXIO;
    167  1.1  pgoyette 	}
    168  1.1  pgoyette 
    169  1.1  pgoyette 	return 0;
    170  1.1  pgoyette }
    171  1.1  pgoyette 
    172  1.1  pgoyette static void
    173  1.1  pgoyette imcsmb_chdet(device_t self, device_t child)
    174  1.1  pgoyette {
    175  1.1  pgoyette 	struct imcsmb_softc *sc = device_private(self);
    176  1.1  pgoyette 
    177  1.1  pgoyette 	if (child == sc->sc_smbus)
    178  1.1  pgoyette 		sc->sc_smbus = NULL;
    179  1.1  pgoyette 	else KASSERT(child == NULL);
    180  1.1  pgoyette }
    181  1.1  pgoyette 
    182  1.1  pgoyette /**
    183  1.1  pgoyette  * device_detach() method. attach() didn't do any allocations, so there's
    184  1.1  pgoyette  * nothing special needed
    185  1.1  pgoyette  */
    186  1.1  pgoyette static int
    187  1.1  pgoyette imcsmb_detach(device_t self, int flags)
    188  1.1  pgoyette {
    189  1.1  pgoyette 	int error;
    190  1.1  pgoyette 	struct imcsmb_softc *sc = device_private(self);
    191  1.1  pgoyette 
    192  1.1  pgoyette 	if (sc->sc_smbus != NULL) {
    193  1.1  pgoyette 		error = config_detach(sc->sc_smbus, flags);
    194  1.1  pgoyette 		if (error)
    195  1.1  pgoyette 			return error;
    196  1.1  pgoyette 	}
    197  1.1  pgoyette 
    198  1.1  pgoyette 	pmf_device_deregister(self);
    199  1.1  pgoyette 	mutex_destroy(&sc->sc_i2c_mutex);
    200  1.1  pgoyette 	return 0;
    201  1.1  pgoyette }
    202  1.1  pgoyette 
    203  1.1  pgoyette /**
    204  1.1  pgoyette  * device_probe() method. All the actual probing was done by the imc
    205  1.1  pgoyette  * parent, so just report success.
    206  1.1  pgoyette  *
    207  1.1  pgoyette  * @author Joe Kloss
    208  1.1  pgoyette  *
    209  1.1  pgoyette  * @param[in,out] dev
    210  1.1  pgoyette  *      Device being probed.
    211  1.1  pgoyette  */
    212  1.1  pgoyette static int
    213  1.1  pgoyette imcsmb_probe(device_t parent, cfdata_t match, void *aux)
    214  1.1  pgoyette {
    215  1.1  pgoyette 
    216  1.1  pgoyette 	return 1;
    217  1.1  pgoyette }
    218  1.1  pgoyette 
    219  1.1  pgoyette static int
    220  1.1  pgoyette imcsmb_acquire_bus(void *cookie, int flags)
    221  1.1  pgoyette {
    222  1.1  pgoyette 	struct imcsmb_softc *sc = cookie;
    223  1.1  pgoyette 
    224  1.1  pgoyette 	if (cold)
    225  1.1  pgoyette 		return 0;
    226  1.1  pgoyette 
    227  1.1  pgoyette 	mutex_enter(&sc->sc_i2c_mutex);
    228  1.1  pgoyette 
    229  1.1  pgoyette 	imc_callback(sc, IMC_BIOS_DISABLE);
    230  1.1  pgoyette 
    231  1.1  pgoyette 	return 0;
    232  1.1  pgoyette }
    233  1.1  pgoyette 
    234  1.1  pgoyette static void
    235  1.1  pgoyette imcsmb_release_bus(void *cookie, int flags)
    236  1.1  pgoyette {
    237  1.1  pgoyette 	struct imcsmb_softc *sc = cookie;
    238  1.1  pgoyette 
    239  1.1  pgoyette 	if (cold)
    240  1.1  pgoyette 		return;
    241  1.1  pgoyette 
    242  1.1  pgoyette 	imc_callback(sc, IMC_BIOS_ENABLE);
    243  1.1  pgoyette 
    244  1.1  pgoyette 	mutex_exit(&sc->sc_i2c_mutex);
    245  1.1  pgoyette }
    246  1.1  pgoyette 
    247  1.1  pgoyette static int
    248  1.1  pgoyette imcsmb_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    249  1.1  pgoyette     size_t cmdlen, void *buf, size_t len, int flags)
    250  1.1  pgoyette {
    251  1.1  pgoyette 	struct imcsmb_softc *sc = cookie;
    252  1.1  pgoyette 	int i;
    253  1.1  pgoyette 	int rc = 0;
    254  1.1  pgoyette 	uint32_t cmd_val;
    255  1.1  pgoyette 	uint32_t cntl_val;
    256  1.1  pgoyette 	uint32_t orig_cntl_val;
    257  1.1  pgoyette 	uint32_t stat_val;
    258  1.1  pgoyette 	uint16_t *word;
    259  1.1  pgoyette 	uint16_t lword;
    260  1.1  pgoyette 	uint8_t *byte;
    261  1.1  pgoyette 	uint8_t lbyte;
    262  1.1  pgoyette 	uint8_t cmd;
    263  1.1  pgoyette 
    264  1.1  pgoyette 	if ((cmdlen != 1) || (len > 2) || (len < 1))
    265  1.1  pgoyette 		return EINVAL;
    266  1.1  pgoyette 
    267  1.1  pgoyette 	byte = (uint8_t *) buf;
    268  1.1  pgoyette 	word = (uint16_t *) buf;
    269  1.1  pgoyette 	lbyte = *byte;
    270  1.1  pgoyette 	lword = *word;
    271  1.1  pgoyette 
    272  1.1  pgoyette 	/* We modify the value of the control register; save the original, so
    273  1.1  pgoyette 	 * we can restore it later
    274  1.1  pgoyette 	 */
    275  1.1  pgoyette 	orig_cntl_val = pci_conf_read(sc->sc_pci_chipset_tag, sc->sc_pci_tag,
    276  1.1  pgoyette 	    sc->sc_regs->smb_cntl);
    277  1.1  pgoyette 
    278  1.1  pgoyette 	cntl_val = orig_cntl_val;
    279  1.1  pgoyette 
    280  1.1  pgoyette 	/*
    281  1.1  pgoyette 	 * Set up the SMBCNTL register
    282  1.1  pgoyette 	 */
    283  1.1  pgoyette 
    284  1.1  pgoyette 	/* [31:28] Clear the existing value of the DTI bits, then set them to
    285  1.1  pgoyette 	 * the four high bits of the slave address.
    286  1.1  pgoyette 	 */
    287  1.1  pgoyette 	cntl_val &= ~IMCSMB_CNTL_DTI_MASK;
    288  1.1  pgoyette 	cntl_val |= ((uint32_t) addr & 0x78) << 25;
    289  1.1  pgoyette 
    290  1.1  pgoyette 	/* [27:27] Set the CLK_OVERRIDE bit, to enable normal operation */
    291  1.1  pgoyette 	cntl_val |= IMCSMB_CNTL_CLK_OVERRIDE;
    292  1.1  pgoyette 
    293  1.1  pgoyette 	/* [26:26] Clear the WRITE_DISABLE bit; the datasheet says this isn't
    294  1.1  pgoyette 	 * necessary, but empirically, it is.
    295  1.1  pgoyette 	 */
    296  1.1  pgoyette 	cntl_val &= ~IMCSMB_CNTL_WRITE_DISABLE_BIT;
    297  1.1  pgoyette 
    298  1.1  pgoyette 	/* [9:9] Clear the POLL_EN bit, to stop the hardware TSOD polling. */
    299  1.1  pgoyette 	cntl_val &= ~IMCSMB_CNTL_POLL_EN;
    300  1.1  pgoyette 
    301  1.1  pgoyette 	/*
    302  1.1  pgoyette 	 * Set up the SMBCMD register
    303  1.1  pgoyette 	 */
    304  1.1  pgoyette 
    305  1.1  pgoyette 	/* [31:31] Set the TRIGGER bit; when this gets written, the controller
    306  1.1  pgoyette 	 * will issue the command.
    307  1.1  pgoyette 	 */
    308  1.1  pgoyette 	cmd_val = IMCSMB_CMD_TRIGGER_BIT;
    309  1.1  pgoyette 
    310  1.1  pgoyette 	/* [29:29] For word operations, set the WORD_ACCESS bit. */
    311  1.1  pgoyette 	if (len == 2) {
    312  1.1  pgoyette 		cmd_val |= IMCSMB_CMD_WORD_ACCESS;
    313  1.1  pgoyette 	}
    314  1.1  pgoyette 
    315  1.1  pgoyette 	/* [27:27] For write operations, set the WRITE bit. */
    316  1.1  pgoyette 	if (I2C_OP_WRITE_P(op)) {
    317  1.1  pgoyette 		cmd_val |= IMCSMB_CMD_WRITE_BIT;
    318  1.1  pgoyette 	}
    319  1.1  pgoyette 
    320  1.1  pgoyette 	/* [26:24] The three non-DTI, non-R/W bits of the slave address. */
    321  1.1  pgoyette 	cmd_val |= (uint32_t) ((addr & 0x7) << 24);
    322  1.1  pgoyette 
    323  1.1  pgoyette 	/* [23:16] The command (offset in the case of an EEPROM, or register in
    324  1.1  pgoyette 	 * the case of TSOD or NVDIMM controller).
    325  1.1  pgoyette 	 */
    326  1.1  pgoyette 	cmd = *((const uint8_t *) cmdbuf);
    327  1.1  pgoyette 	cmd_val |= (uint32_t) (cmd << 16);
    328  1.1  pgoyette 
    329  1.1  pgoyette 	/* [15:0] The data to be written for a write operation. */
    330  1.1  pgoyette 	if (I2C_OP_WRITE_P(op)) {
    331  1.1  pgoyette 		if (len == 2) {
    332  1.1  pgoyette 			/* The datasheet says the controller uses different
    333  1.1  pgoyette 			 * endianness for word operations on I2C vs SMBus!
    334  1.1  pgoyette 			 *      I2C: [15:8] = MSB; [7:0] = LSB
    335  1.1  pgoyette 			 *      SMB: [15:8] = LSB; [7:0] = MSB
    336  1.1  pgoyette 			 * As a practical matter, this controller is very
    337  1.1  pgoyette 			 * specifically for use with DIMMs, the SPD (and
    338  1.1  pgoyette 			 * NVDIMM controllers) are only accessed as bytes,
    339  1.1  pgoyette 			 * the temperature sensor is only accessed as words, and
    340  1.1  pgoyette 			 * the temperature sensors are I2C. Thus, byte-swap the
    341  1.1  pgoyette 			 * word.
    342  1.1  pgoyette 			 */
    343  1.1  pgoyette 			lword = htobe16(*(uint16_t *)buf);
    344  1.1  pgoyette 		} else {
    345  1.1  pgoyette 			/* For byte operations, the data goes in the LSB, and
    346  1.1  pgoyette 			 * the MSB is a don't care.
    347  1.1  pgoyette 			 */
    348  1.1  pgoyette 			lword = *(uint8_t *)buf;
    349  1.1  pgoyette 		}
    350  1.1  pgoyette 		cmd_val |= lword;
    351  1.1  pgoyette 	}
    352  1.1  pgoyette 
    353  1.1  pgoyette 	/* Write the updated value to the control register first, to disable
    354  1.1  pgoyette 	 * the hardware TSOD polling.
    355  1.1  pgoyette 	 */
    356  1.1  pgoyette 	pci_conf_write(sc->sc_pci_chipset_tag, sc->sc_pci_tag,
    357  1.1  pgoyette 	    sc->sc_regs->smb_cntl, cntl_val);
    358  1.1  pgoyette 
    359  1.1  pgoyette 	/* Poll on the BUSY bit in the status register until clear, or timeout.
    360  1.1  pgoyette 	 * We just cleared the auto-poll bit, so we need to make sure the device
    361  1.1  pgoyette 	 * is idle before issuing a command. We can safely timeout after 35 ms,
    362  1.1  pgoyette 	 * as this is the maximum time the SMBus spec allows for a transaction.
    363  1.1  pgoyette 	 */
    364  1.1  pgoyette 	for (i = 4; i != 0; i--) {
    365  1.1  pgoyette 		stat_val = pci_conf_read(sc->sc_pci_chipset_tag,
    366  1.1  pgoyette 		    sc->sc_pci_tag, sc->sc_regs->smb_stat);
    367  1.1  pgoyette 		if (! (stat_val & IMCSMB_STATUS_BUSY_BIT)) {
    368  1.1  pgoyette 			break;
    369  1.1  pgoyette 		}
    370  1.1  pgoyette 		delay(100);	/* wait 10ms */
    371  1.1  pgoyette 	}
    372  1.1  pgoyette 
    373  1.1  pgoyette 	if (i == 0) {
    374  1.1  pgoyette 		aprint_debug_dev(sc->sc_dev,
    375  1.1  pgoyette 		    "transfer: timeout waiting for device to settle\n");
    376  1.1  pgoyette 	}
    377  1.1  pgoyette 
    378  1.1  pgoyette 	/* Now that polling has stopped, we can write the command register. This
    379  1.1  pgoyette 	 * starts the SMBus command.
    380  1.1  pgoyette 	 */
    381  1.1  pgoyette 	pci_conf_write(sc->sc_pci_chipset_tag, sc->sc_pci_tag,
    382  1.1  pgoyette 	    sc->sc_regs->smb_cmd, cmd_val);
    383  1.1  pgoyette 
    384  1.1  pgoyette 	/* Wait for WRITE_DATA_DONE/READ_DATA_VALID to be set, or timeout and
    385  1.1  pgoyette 	 * fail. We wait up to 35ms.
    386  1.1  pgoyette 	 */
    387  1.1  pgoyette 	for (i = 35000; i != 0; i -= 10)
    388  1.1  pgoyette 	{
    389  1.1  pgoyette 		delay(10);
    390  1.1  pgoyette 		stat_val = pci_conf_read(sc->sc_pci_chipset_tag,
    391  1.1  pgoyette 		    sc->sc_pci_tag, sc->sc_regs->smb_stat);
    392  1.1  pgoyette 		/*
    393  1.1  pgoyette 		 * For a write, the bits holding the data contain the data
    394  1.1  pgoyette 		 * being written. You would think that would cause the
    395  1.1  pgoyette 		 * READ_DATA_VALID bit to be cleared, because the data bits
    396  1.1  pgoyette 		 * no longer contain valid data from the most recent read
    397  1.1  pgoyette 		 * operation. While that would be logical, that's not the
    398  1.1  pgoyette 		 * case here: READ_DATA_VALID is only cleared when starting
    399  1.1  pgoyette 		 * a read operation, and WRITE_DATA_DONE is only cleared
    400  1.1  pgoyette 		 * when starting a write operation.
    401  1.1  pgoyette 		 */
    402  1.1  pgoyette 		if (I2C_OP_WRITE_P(op)) {
    403  1.1  pgoyette 			if (stat_val & IMCSMB_STATUS_WRITE_DATA_DONE) {
    404  1.1  pgoyette 				break;
    405  1.1  pgoyette 			}
    406  1.1  pgoyette 		} else {
    407  1.1  pgoyette 			if (stat_val & IMCSMB_STATUS_READ_DATA_VALID) {
    408  1.1  pgoyette 				break;
    409  1.1  pgoyette 			}
    410  1.1  pgoyette 		}
    411  1.1  pgoyette 	}
    412  1.1  pgoyette 	if (i == 0) {
    413  1.1  pgoyette 		rc = ETIMEDOUT;
    414  1.1  pgoyette 		aprint_debug_dev(sc->sc_dev, "transfer timeout\n");
    415  1.1  pgoyette 		goto out;
    416  1.1  pgoyette 	}
    417  1.1  pgoyette 
    418  1.1  pgoyette 	/* It is generally the case that this bit indicates non-ACK, but it
    419  1.1  pgoyette 	 * could also indicate other bus errors. There's no way to tell the
    420  1.1  pgoyette 	 * difference.
    421  1.1  pgoyette 	 */
    422  1.1  pgoyette 	if (stat_val & IMCSMB_STATUS_BUS_ERROR_BIT) {
    423  1.1  pgoyette 		/* While it is not documented, empirically, SPD page-change
    424  1.1  pgoyette 		 * commands (writes with DTI = 0x30) always complete with the
    425  1.1  pgoyette 		 * error bit set. So, ignore it in those cases.
    426  1.1  pgoyette 		 */
    427  1.1  pgoyette 		if ((addr & 0x78) != 0x30) {
    428  1.1  pgoyette 			rc = ENODEV;
    429  1.1  pgoyette 			goto out;
    430  1.1  pgoyette 		}
    431  1.1  pgoyette 	}
    432  1.1  pgoyette 
    433  1.1  pgoyette 	/* For a read operation, copy the data out */
    434  1.1  pgoyette 	if (I2C_OP_READ_P(op)) {
    435  1.1  pgoyette 		if (len == 2) {
    436  1.1  pgoyette 			/* The data is returned in bits [15:0]; as discussed
    437  1.1  pgoyette 			 * above, byte-swap.
    438  1.1  pgoyette 			 */
    439  1.1  pgoyette 			lword = (uint16_t) (stat_val & 0xffff);
    440  1.1  pgoyette 			lword = htobe16(lword);
    441  1.1  pgoyette 			*(uint16_t *)buf = lword;
    442  1.1  pgoyette 		} else {
    443  1.1  pgoyette 			/* The data is returned in bits [7:0] */
    444  1.1  pgoyette 			lbyte = (uint8_t) (stat_val & 0xff);
    445  1.1  pgoyette 			*(uint8_t *)buf = lbyte;
    446  1.1  pgoyette 		}
    447  1.1  pgoyette 	}
    448  1.1  pgoyette 
    449  1.1  pgoyette out:
    450  1.1  pgoyette 	/* Restore the original value of the control register. */
    451  1.1  pgoyette 	pci_conf_write(sc->sc_pci_chipset_tag, sc->sc_pci_tag,
    452  1.1  pgoyette 	    sc->sc_regs->smb_cntl, orig_cntl_val);
    453  1.1  pgoyette 	return rc;
    454  1.1  pgoyette };
    455  1.1  pgoyette 
    456  1.1  pgoyette MODULE(MODULE_CLASS_DRIVER, imcsmb, "imc,iic");
    457  1.1  pgoyette 
    458  1.1  pgoyette #ifdef _MODULE
    459  1.1  pgoyette #include "ioconf.c"
    460  1.1  pgoyette #endif
    461  1.1  pgoyette 
    462  1.1  pgoyette static int
    463  1.1  pgoyette imcsmb_modcmd(modcmd_t cmd, void *opaque)
    464  1.1  pgoyette {
    465  1.1  pgoyette 	int error = 0;
    466  1.1  pgoyette 
    467  1.1  pgoyette #ifdef _MODULE
    468  1.1  pgoyette 	switch (cmd) {
    469  1.1  pgoyette 	case MODULE_CMD_INIT:
    470  1.1  pgoyette 		error = config_init_component(cfdriver_ioconf_imcsmb,
    471  1.1  pgoyette 		    cfattach_ioconf_imcsmb, cfdata_ioconf_imcsmb);
    472  1.1  pgoyette 		break;
    473  1.1  pgoyette 	case MODULE_CMD_FINI:
    474  1.1  pgoyette 		error = config_fini_component(cfdriver_ioconf_imcsmb,
    475  1.1  pgoyette 		    cfattach_ioconf_imcsmb, cfdata_ioconf_imcsmb);
    476  1.1  pgoyette 		break;
    477  1.1  pgoyette 	default:
    478  1.1  pgoyette 		error = ENOTTY;
    479  1.1  pgoyette 		break;
    480  1.1  pgoyette 	}
    481  1.1  pgoyette #endif
    482  1.1  pgoyette 
    483  1.1  pgoyette 	return error;
    484  1.1  pgoyette }
    485