Home | History | Annotate | Line # | Download | only in pci
amdpm_smbus.c revision 1.3.6.2
      1  1.3.6.2  yamt /*	$NetBSD: amdpm_smbus.c,v 1.3.6.2 2006/06/21 15:05:03 yamt Exp $ */
      2  1.3.6.2  yamt 
      3  1.3.6.2  yamt /*
      4  1.3.6.2  yamt  * Copyright (c) 2005 Anil Gopinath (anil_public (at) yahoo.com)
      5  1.3.6.2  yamt  * All rights reserved.
      6  1.3.6.2  yamt  *
      7  1.3.6.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.3.6.2  yamt  * modification, are permitted provided that the following conditions
      9  1.3.6.2  yamt  * are met:
     10  1.3.6.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.3.6.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.3.6.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.3.6.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.3.6.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.3.6.2  yamt  * 3. The name of the author may not be used to endorse or promote products
     16  1.3.6.2  yamt  *    derived from this software without specific prior written permission.
     17  1.3.6.2  yamt  *
     18  1.3.6.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.3.6.2  yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.3.6.2  yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.3.6.2  yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.3.6.2  yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.3.6.2  yamt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.3.6.2  yamt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.3.6.2  yamt  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.3.6.2  yamt  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.3.6.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.3.6.2  yamt  * SUCH DAMAGE.
     29  1.3.6.2  yamt  */
     30  1.3.6.2  yamt 
     31  1.3.6.2  yamt /* driver for SMBUS 1.0 host controller found in the
     32  1.3.6.2  yamt  * AMD-8111 HyperTransport I/O Hub
     33  1.3.6.2  yamt  */
     34  1.3.6.2  yamt #include <sys/cdefs.h>
     35  1.3.6.2  yamt __KERNEL_RCSID(0, "$NetBSD: amdpm_smbus.c,v 1.3.6.2 2006/06/21 15:05:03 yamt Exp $");
     36  1.3.6.2  yamt 
     37  1.3.6.2  yamt #include <sys/param.h>
     38  1.3.6.2  yamt #include <sys/systm.h>
     39  1.3.6.2  yamt #include <sys/kernel.h>
     40  1.3.6.2  yamt #include <sys/device.h>
     41  1.3.6.2  yamt #include <sys/rnd.h>
     42  1.3.6.2  yamt #include <dev/pci/pcireg.h>
     43  1.3.6.2  yamt #include <dev/pci/pcivar.h>
     44  1.3.6.2  yamt #include <dev/pci/pcidevs.h>
     45  1.3.6.2  yamt 
     46  1.3.6.2  yamt #include <dev/i2c/i2cvar.h>
     47  1.3.6.2  yamt #include <dev/i2c/i2c_bitbang.h>
     48  1.3.6.2  yamt 
     49  1.3.6.2  yamt #include <dev/pci/amdpmreg.h>
     50  1.3.6.2  yamt #include <dev/pci/amdpmvar.h>
     51  1.3.6.2  yamt 
     52  1.3.6.2  yamt #include <dev/pci/amdpm_smbusreg.h>
     53  1.3.6.2  yamt 
     54  1.3.6.2  yamt static int       amdpm_smbus_acquire_bus(void *cookie, int flags);
     55  1.3.6.2  yamt static void      amdpm_smbus_release_bus(void *cookie, int flags);
     56  1.3.6.2  yamt static int       amdpm_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
     57  1.3.6.2  yamt 				  const void *cmd, size_t cmdlen, void *vbuf,
     58  1.3.6.2  yamt 				  size_t buflen, int flags);
     59  1.3.6.2  yamt static int       amdpm_smbus_check_done(struct amdpm_softc *sc);
     60  1.3.6.2  yamt static void      amdpm_smbus_clear_gsr(struct amdpm_softc *sc);
     61  1.3.6.2  yamt static u_int16_t amdpm_smbus_get_gsr(struct amdpm_softc *sc);
     62  1.3.6.2  yamt static int       amdpm_smbus_send_1(struct amdpm_softc *sc, u_int8_t val);
     63  1.3.6.2  yamt static int       amdpm_smbus_write_1(struct amdpm_softc *sc, u_int8_t cmd, u_int8_t data);
     64  1.3.6.2  yamt static int       amdpm_smbus_receive_1(struct amdpm_softc *sc);
     65  1.3.6.2  yamt static int       amdpm_smbus_read_1(struct amdpm_softc *sc, u_int8_t cmd);
     66  1.3.6.2  yamt 
     67  1.3.6.2  yamt 
     68  1.3.6.2  yamt void
     69  1.3.6.2  yamt amdpm_smbus_attach(struct amdpm_softc *sc)
     70  1.3.6.2  yamt {
     71  1.3.6.2  yamt         struct i2cbus_attach_args iba;
     72  1.3.6.2  yamt 
     73  1.3.6.2  yamt 	// register with iic
     74  1.3.6.2  yamt 	sc->sc_i2c.ic_cookie = sc;
     75  1.3.6.2  yamt 	sc->sc_i2c.ic_acquire_bus = amdpm_smbus_acquire_bus;
     76  1.3.6.2  yamt 	sc->sc_i2c.ic_release_bus = amdpm_smbus_release_bus;
     77  1.3.6.2  yamt 	sc->sc_i2c.ic_send_start = NULL;
     78  1.3.6.2  yamt 	sc->sc_i2c.ic_send_stop = NULL;
     79  1.3.6.2  yamt 	sc->sc_i2c.ic_initiate_xfer = NULL;
     80  1.3.6.2  yamt 	sc->sc_i2c.ic_read_byte = NULL;
     81  1.3.6.2  yamt 	sc->sc_i2c.ic_write_byte = NULL;
     82  1.3.6.2  yamt 	sc->sc_i2c.ic_exec = amdpm_smbus_exec;
     83  1.3.6.2  yamt 
     84  1.3.6.2  yamt 	lockinit(&sc->sc_lock, PZERO, "amdpm_smbus", 0, 0);
     85  1.3.6.2  yamt 
     86  1.3.6.2  yamt 	iba.iba_name = "iic";
     87  1.3.6.2  yamt 	iba.iba_tag = &sc->sc_i2c;
     88  1.3.6.2  yamt 	(void) config_found(&sc->sc_dev, &iba, iicbus_print);
     89  1.3.6.2  yamt }
     90  1.3.6.2  yamt 
     91  1.3.6.2  yamt static int
     92  1.3.6.2  yamt amdpm_smbus_acquire_bus(void *cookie, int flags)
     93  1.3.6.2  yamt {
     94  1.3.6.2  yamt 	struct amdpm_softc *sc = cookie;
     95  1.3.6.2  yamt 	int err;
     96  1.3.6.2  yamt 
     97  1.3.6.2  yamt 	err = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
     98  1.3.6.2  yamt 
     99  1.3.6.2  yamt 	return err;
    100  1.3.6.2  yamt }
    101  1.3.6.2  yamt 
    102  1.3.6.2  yamt static void
    103  1.3.6.2  yamt amdpm_smbus_release_bus(void *cookie, int flags)
    104  1.3.6.2  yamt {
    105  1.3.6.2  yamt 	struct amdpm_softc *sc = cookie;
    106  1.3.6.2  yamt 
    107  1.3.6.2  yamt 	lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
    108  1.3.6.2  yamt 
    109  1.3.6.2  yamt 	return;
    110  1.3.6.2  yamt }
    111  1.3.6.2  yamt 
    112  1.3.6.2  yamt static int
    113  1.3.6.2  yamt amdpm_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
    114  1.3.6.2  yamt     size_t cmdlen, void *vbuf, size_t buflen, int flags)
    115  1.3.6.2  yamt {
    116  1.3.6.2  yamt         struct amdpm_softc *sc  = (struct amdpm_softc *) cookie;
    117  1.3.6.2  yamt 	sc->sc_smbus_slaveaddr  = addr;
    118  1.3.6.2  yamt 
    119  1.3.6.2  yamt 	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
    120  1.3.6.2  yamt 	  return (amdpm_smbus_receive_1(sc));
    121  1.3.6.2  yamt 	}
    122  1.3.6.2  yamt 
    123  1.3.6.2  yamt 	if ( (I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    124  1.3.6.2  yamt 	  return (amdpm_smbus_read_1(sc, *(const uint8_t*)cmd));
    125  1.3.6.2  yamt 	}
    126  1.3.6.2  yamt 
    127  1.3.6.2  yamt 	if ( (I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1)) {
    128  1.3.6.2  yamt 	  return (amdpm_smbus_send_1(sc, *(uint8_t*)vbuf));
    129  1.3.6.2  yamt 	}
    130  1.3.6.2  yamt 
    131  1.3.6.2  yamt 	if ( (I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
    132  1.3.6.2  yamt 	  return (amdpm_smbus_write_1(sc,  *(const uint8_t*)cmd, *(uint8_t*)vbuf));
    133  1.3.6.2  yamt 	}
    134  1.3.6.2  yamt 
    135  1.3.6.2  yamt 	return (-1);
    136  1.3.6.2  yamt }
    137  1.3.6.2  yamt 
    138  1.3.6.2  yamt static int
    139  1.3.6.2  yamt amdpm_smbus_check_done(struct amdpm_softc *sc)
    140  1.3.6.2  yamt {
    141  1.3.6.2  yamt         int i = 0;
    142  1.3.6.2  yamt 	for (i = 0; i < 1000; i++) {
    143  1.3.6.2  yamt 	  /* check gsr and wait till cycle is done */
    144  1.3.6.2  yamt 	  u_int16_t data = amdpm_smbus_get_gsr(sc);
    145  1.3.6.2  yamt 	  if (data & AMDPM_8111_GSR_CYCLE_DONE) {
    146  1.3.6.2  yamt 	    return (0);
    147  1.3.6.2  yamt 	  }
    148  1.3.6.2  yamt 	  delay(1);
    149  1.3.6.2  yamt 	}
    150  1.3.6.2  yamt 	return (-1);
    151  1.3.6.2  yamt }
    152  1.3.6.2  yamt 
    153  1.3.6.2  yamt 
    154  1.3.6.2  yamt static void
    155  1.3.6.2  yamt amdpm_smbus_clear_gsr(struct amdpm_softc *sc)
    156  1.3.6.2  yamt {
    157  1.3.6.2  yamt         /* clear register */
    158  1.3.6.2  yamt         u_int16_t data = 0xFFFF;
    159  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_STAT, data);
    160  1.3.6.2  yamt }
    161  1.3.6.2  yamt 
    162  1.3.6.2  yamt static u_int16_t
    163  1.3.6.2  yamt amdpm_smbus_get_gsr(struct amdpm_softc *sc)
    164  1.3.6.2  yamt {
    165  1.3.6.2  yamt         return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_STAT));
    166  1.3.6.2  yamt }
    167  1.3.6.2  yamt 
    168  1.3.6.2  yamt static int
    169  1.3.6.2  yamt amdpm_smbus_send_1(struct amdpm_softc *sc,  u_int8_t val)
    170  1.3.6.2  yamt {
    171  1.3.6.2  yamt         /* first clear gsr */
    172  1.3.6.2  yamt         amdpm_smbus_clear_gsr(sc);
    173  1.3.6.2  yamt 
    174  1.3.6.2  yamt 	/* write smbus slave address to register */
    175  1.3.6.2  yamt 	u_int16_t data = 0;
    176  1.3.6.2  yamt 	data = sc->sc_smbus_slaveaddr;
    177  1.3.6.2  yamt 	data <<= 1;
    178  1.3.6.2  yamt 	data |= AMDPM_8111_SMBUS_SEND;
    179  1.3.6.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
    180  1.3.6.2  yamt 
    181  1.3.6.2  yamt 	data = val;
    182  1.3.6.2  yamt 	/* store data */
    183  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA, data);
    184  1.3.6.2  yamt 	/* host start */
    185  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL,
    186  1.3.6.2  yamt 			  AMDPM_8111_SMBUS_GSR_SB);
    187  1.3.6.2  yamt 	return(amdpm_smbus_check_done(sc));
    188  1.3.6.2  yamt }
    189  1.3.6.2  yamt 
    190  1.3.6.2  yamt 
    191  1.3.6.2  yamt static int
    192  1.3.6.2  yamt amdpm_smbus_write_1(struct amdpm_softc *sc, u_int8_t cmd, u_int8_t val)
    193  1.3.6.2  yamt {
    194  1.3.6.2  yamt         /* first clear gsr */
    195  1.3.6.2  yamt         amdpm_smbus_clear_gsr(sc);
    196  1.3.6.2  yamt 
    197  1.3.6.2  yamt 	u_int16_t data = 0;
    198  1.3.6.2  yamt 	data = sc->sc_smbus_slaveaddr;
    199  1.3.6.2  yamt 	data <<= 1;
    200  1.3.6.2  yamt 	data |= AMDPM_8111_SMBUS_WRITE;
    201  1.3.6.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
    202  1.3.6.2  yamt 
    203  1.3.6.2  yamt 	data = val;
    204  1.3.6.2  yamt 	/* store cmd */
    205  1.3.6.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTCMD, cmd);
    206  1.3.6.2  yamt 	/* store data */
    207  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA, data);
    208  1.3.6.2  yamt 	/* host start */
    209  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL, AMDPM_8111_SMBUS_GSR_WB);
    210  1.3.6.2  yamt 
    211  1.3.6.2  yamt 	return (amdpm_smbus_check_done(sc));
    212  1.3.6.2  yamt }
    213  1.3.6.2  yamt 
    214  1.3.6.2  yamt static int
    215  1.3.6.2  yamt amdpm_smbus_receive_1(struct amdpm_softc *sc)
    216  1.3.6.2  yamt {
    217  1.3.6.2  yamt         /* first clear gsr */
    218  1.3.6.2  yamt         amdpm_smbus_clear_gsr(sc);
    219  1.3.6.2  yamt 
    220  1.3.6.2  yamt 	/* write smbus slave address to register */
    221  1.3.6.2  yamt 	u_int16_t data = 0;
    222  1.3.6.2  yamt 	data = sc->sc_smbus_slaveaddr;
    223  1.3.6.2  yamt 	data <<= 1;
    224  1.3.6.2  yamt 	data |= AMDPM_8111_SMBUS_RX;
    225  1.3.6.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
    226  1.3.6.2  yamt 
    227  1.3.6.2  yamt 	/* start smbus cycle */
    228  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL, AMDPM_8111_SMBUS_GSR_RXB);
    229  1.3.6.2  yamt 
    230  1.3.6.2  yamt 	/* check for errors */
    231  1.3.6.2  yamt 	if (amdpm_smbus_check_done(sc) < 0)
    232  1.3.6.2  yamt 	  return (-1);
    233  1.3.6.2  yamt 
    234  1.3.6.2  yamt 	/* read data */
    235  1.3.6.2  yamt 	data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA);
    236  1.3.6.2  yamt 	u_int8_t ret = (u_int8_t)(data & 0x00FF);
    237  1.3.6.2  yamt 	return (ret);
    238  1.3.6.2  yamt }
    239  1.3.6.2  yamt 
    240  1.3.6.2  yamt static int
    241  1.3.6.2  yamt amdpm_smbus_read_1(struct amdpm_softc *sc, u_int8_t cmd)
    242  1.3.6.2  yamt {
    243  1.3.6.2  yamt         /* first clear gsr */
    244  1.3.6.2  yamt         amdpm_smbus_clear_gsr(sc);
    245  1.3.6.2  yamt 
    246  1.3.6.2  yamt 	/* write smbus slave address to register */
    247  1.3.6.2  yamt 	u_int16_t data = 0;
    248  1.3.6.2  yamt 	data = sc->sc_smbus_slaveaddr;
    249  1.3.6.2  yamt 	data <<= 1;
    250  1.3.6.2  yamt 	data |= AMDPM_8111_SMBUS_READ;
    251  1.3.6.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTADDR, data);
    252  1.3.6.2  yamt 
    253  1.3.6.2  yamt 	/* store cmd */
    254  1.3.6.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTCMD, cmd);
    255  1.3.6.2  yamt 	/* host start */
    256  1.3.6.2  yamt 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_CTRL, AMDPM_8111_SMBUS_GSR_RB);
    257  1.3.6.2  yamt 
    258  1.3.6.2  yamt 	/* check for errors */
    259  1.3.6.2  yamt 	if (amdpm_smbus_check_done(sc) < 0)
    260  1.3.6.2  yamt 	  return (-1);
    261  1.3.6.2  yamt 
    262  1.3.6.2  yamt 	/* store data */
    263  1.3.6.2  yamt 	data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AMDPM_8111_SMBUS_HOSTDATA);
    264  1.3.6.2  yamt 	u_int8_t ret = (u_int8_t)(data & 0x00FF);
    265  1.3.6.2  yamt 	return (ret);
    266  1.3.6.2  yamt }
    267