spdmem_i2c.c revision 1.3
11.3Sjmcneill/* $NetBSD: spdmem_i2c.c,v 1.3 2011/10/02 19:03:56 jmcneill Exp $ */ 21.1Spgoyette 31.1Spgoyette/* 41.1Spgoyette * Copyright (c) 2007 Nicolas Joly 51.1Spgoyette * Copyright (c) 2007 Paul Goyette 61.1Spgoyette * Copyright (c) 2007 Tobias Nygren 71.1Spgoyette * All rights reserved. 81.1Spgoyette * 91.1Spgoyette * Redistribution and use in source and binary forms, with or without 101.1Spgoyette * modification, are permitted provided that the following conditions 111.1Spgoyette * are met: 121.1Spgoyette * 1. Redistributions of source code must retain the above copyright 131.1Spgoyette * notice, this list of conditions and the following disclaimer. 141.1Spgoyette * 2. Redistributions in binary form must reproduce the above copyright 151.1Spgoyette * notice, this list of conditions and the following disclaimer in the 161.1Spgoyette * documentation and/or other materials provided with the distribution. 171.1Spgoyette * 3. The name of the author may not be used to endorse or promote products 181.1Spgoyette * derived from this software without specific prior written permission. 191.1Spgoyette * 201.1Spgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 211.1Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 221.1Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 231.1Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 241.1Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 251.1Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 261.1Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 271.1Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 281.1Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 291.1Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 301.1Spgoyette * POSSIBILITY OF SUCH DAMAGE. 311.1Spgoyette */ 321.1Spgoyette 331.1Spgoyette/* 341.1Spgoyette * Serial Presence Detect (SPD) memory identification 351.1Spgoyette */ 361.1Spgoyette 371.1Spgoyette#include <sys/cdefs.h> 381.3Sjmcneill__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.3 2011/10/02 19:03:56 jmcneill Exp $"); 391.1Spgoyette 401.1Spgoyette#include <sys/param.h> 411.1Spgoyette#include <sys/device.h> 421.1Spgoyette#include <sys/endian.h> 431.2Spgoyette#include <sys/module.h> 441.1Spgoyette#include <sys/sysctl.h> 451.1Spgoyette#include <machine/bswap.h> 461.1Spgoyette 471.1Spgoyette#include <dev/i2c/i2cvar.h> 481.1Spgoyette#include <dev/ic/spdmemreg.h> 491.1Spgoyette#include <dev/ic/spdmemvar.h> 501.1Spgoyette 511.1Spgoyette/* Constants for matching i2c bus address */ 521.1Spgoyette#define SPDMEM_I2C_ADDRMASK 0x78 531.1Spgoyette#define SPDMEM_I2C_ADDR 0x50 541.1Spgoyette 551.1Spgoyettestruct spdmem_i2c_softc { 561.1Spgoyette struct spdmem_softc sc_base; 571.1Spgoyette i2c_tag_t sc_tag; 581.1Spgoyette i2c_addr_t sc_addr; 591.1Spgoyette}; 601.1Spgoyette 611.2Spgoyettestatic int spdmem_i2c_match(device_t, cfdata_t, void *); 621.1Spgoyettestatic void spdmem_i2c_attach(device_t, device_t, void *); 631.2Spgoyettestatic int spdmem_i2c_detach(device_t, int); 641.2Spgoyette 651.2SpgoyetteCFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc), 661.2Spgoyette spdmem_i2c_match, spdmem_i2c_attach, spdmem_i2c_detach, NULL); 671.1Spgoyette 681.1Spgoyettestatic uint8_t spdmem_i2c_read(struct spdmem_softc *, uint8_t); 691.1Spgoyette 701.2SpgoyetteSYSCTL_SETUP_PROTO(sysctl_spdmem_setup); 711.1Spgoyette 721.1Spgoyettestatic int 731.1Spgoyettespdmem_i2c_match(device_t parent, cfdata_t match, void *aux) 741.1Spgoyette{ 751.1Spgoyette struct i2c_attach_args *ia = aux; 761.1Spgoyette struct spdmem_i2c_softc sc; 771.1Spgoyette 781.1Spgoyette if (ia->ia_name) { 791.1Spgoyette /* add other names as we find more firmware variations */ 801.1Spgoyette if (strcmp(ia->ia_name, "dimm-spd")) 811.1Spgoyette return 0; 821.1Spgoyette } 831.1Spgoyette 841.1Spgoyette /* only do this lame test when not using direct config */ 851.1Spgoyette if (ia->ia_name == NULL) { 861.1Spgoyette if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR) 871.1Spgoyette return 0; 881.1Spgoyette } 891.1Spgoyette 901.1Spgoyette sc.sc_tag = ia->ia_tag; 911.1Spgoyette sc.sc_addr = ia->ia_addr; 921.1Spgoyette sc.sc_base.sc_read = spdmem_i2c_read; 931.1Spgoyette 941.1Spgoyette return spdmem_common_probe(&sc.sc_base); 951.1Spgoyette} 961.1Spgoyette 971.1Spgoyettestatic void 981.1Spgoyettespdmem_i2c_attach(device_t parent, device_t self, void *aux) 991.1Spgoyette{ 1001.1Spgoyette struct spdmem_i2c_softc *sc = device_private(self); 1011.1Spgoyette struct i2c_attach_args *ia = aux; 1021.1Spgoyette 1031.1Spgoyette sc->sc_tag = ia->ia_tag; 1041.1Spgoyette sc->sc_addr = ia->ia_addr; 1051.1Spgoyette sc->sc_base.sc_read = spdmem_i2c_read; 1061.1Spgoyette 1071.1Spgoyette if (!pmf_device_register(self, NULL, NULL)) 1081.1Spgoyette aprint_error_dev(self, "couldn't establish power handler\n"); 1091.1Spgoyette 1101.1Spgoyette spdmem_common_attach(&sc->sc_base, self); 1111.1Spgoyette} 1121.1Spgoyette 1131.2Spgoyettestatic int 1141.2Spgoyettespdmem_i2c_detach(device_t self, int flags) 1151.2Spgoyette{ 1161.2Spgoyette struct spdmem_i2c_softc *sc = device_private(self); 1171.2Spgoyette 1181.2Spgoyette pmf_device_deregister(self); 1191.2Spgoyette 1201.2Spgoyette return spdmem_common_detach(&sc->sc_base, self); 1211.2Spgoyette} 1221.2Spgoyette 1231.1Spgoyettestatic uint8_t 1241.1Spgoyettespdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg) 1251.1Spgoyette{ 1261.1Spgoyette uint8_t val; 1271.1Spgoyette struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc; 1281.1Spgoyette 1291.1Spgoyette iic_acquire_bus(sc->sc_tag, 0); 1301.1Spgoyette iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, ®, 1, 1311.1Spgoyette &val, 1, 0); 1321.1Spgoyette iic_release_bus(sc->sc_tag, 0); 1331.1Spgoyette 1341.1Spgoyette return val; 1351.1Spgoyette} 1361.2Spgoyette 1371.3SjmcneillMODULE(MODULE_CLASS_DRIVER, spdmem, "iic"); 1381.2Spgoyette 1391.2Spgoyette#ifdef _MODULE 1401.2Spgoyette#include "ioconf.c" 1411.2Spgoyette#endif 1421.2Spgoyette 1431.2Spgoyettestatic int 1441.2Spgoyettespdmem_modcmd(modcmd_t cmd, void *opaque) 1451.2Spgoyette{ 1461.2Spgoyette int error = 0; 1471.2Spgoyette 1481.2Spgoyette switch (cmd) { 1491.2Spgoyette case MODULE_CMD_INIT: 1501.2Spgoyette#ifdef _MODULE 1511.2Spgoyette error = config_init_component(cfdriver_ioconf_spdmem, 1521.2Spgoyette cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 1531.2Spgoyette#endif 1541.2Spgoyette return error; 1551.2Spgoyette case MODULE_CMD_FINI: 1561.2Spgoyette#ifdef _MODULE 1571.2Spgoyette error = config_fini_component(cfdriver_ioconf_spdmem, 1581.2Spgoyette cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 1591.2Spgoyette#endif 1601.2Spgoyette return error; 1611.2Spgoyette default: 1621.2Spgoyette return ENOTTY; 1631.2Spgoyette } 1641.2Spgoyette} 165