spdmem_i2c.c revision 1.10
11.10Sjmcneill/* $NetBSD: spdmem_i2c.c,v 1.10 2015/03/07 14:16:51 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.10Sjmcneill__KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.10 2015/03/07 14:16:51 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.8Ssoren#define SPDMEM_I2C_ADDRMASK 0x3f8 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.1Spgoyettestatic int 711.1Spgoyettespdmem_i2c_match(device_t parent, cfdata_t match, void *aux) 721.1Spgoyette{ 731.1Spgoyette struct i2c_attach_args *ia = aux; 741.1Spgoyette struct spdmem_i2c_softc sc; 751.1Spgoyette 761.1Spgoyette if (ia->ia_name) { 771.1Spgoyette /* add other names as we find more firmware variations */ 781.4Snakayama if (strcmp(ia->ia_name, "dimm-spd") && 791.4Snakayama strcmp(ia->ia_name, "dimm")) 801.1Spgoyette return 0; 811.1Spgoyette } 821.1Spgoyette 831.1Spgoyette /* only do this lame test when not using direct config */ 841.1Spgoyette if (ia->ia_name == NULL) { 851.1Spgoyette if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR) 861.1Spgoyette return 0; 871.1Spgoyette } 881.1Spgoyette 891.1Spgoyette sc.sc_tag = ia->ia_tag; 901.1Spgoyette sc.sc_addr = ia->ia_addr; 911.1Spgoyette sc.sc_base.sc_read = spdmem_i2c_read; 921.1Spgoyette 931.1Spgoyette return spdmem_common_probe(&sc.sc_base); 941.1Spgoyette} 951.1Spgoyette 961.1Spgoyettestatic void 971.1Spgoyettespdmem_i2c_attach(device_t parent, device_t self, void *aux) 981.1Spgoyette{ 991.1Spgoyette struct spdmem_i2c_softc *sc = device_private(self); 1001.1Spgoyette struct i2c_attach_args *ia = aux; 1011.1Spgoyette 1021.1Spgoyette sc->sc_tag = ia->ia_tag; 1031.1Spgoyette sc->sc_addr = ia->ia_addr; 1041.1Spgoyette sc->sc_base.sc_read = spdmem_i2c_read; 1051.1Spgoyette 1061.1Spgoyette if (!pmf_device_register(self, NULL, NULL)) 1071.1Spgoyette aprint_error_dev(self, "couldn't establish power handler\n"); 1081.1Spgoyette 1091.1Spgoyette spdmem_common_attach(&sc->sc_base, self); 1101.1Spgoyette} 1111.1Spgoyette 1121.2Spgoyettestatic int 1131.2Spgoyettespdmem_i2c_detach(device_t self, int flags) 1141.2Spgoyette{ 1151.2Spgoyette struct spdmem_i2c_softc *sc = device_private(self); 1161.2Spgoyette 1171.2Spgoyette pmf_device_deregister(self); 1181.2Spgoyette 1191.2Spgoyette return spdmem_common_detach(&sc->sc_base, self); 1201.2Spgoyette} 1211.2Spgoyette 1221.1Spgoyettestatic uint8_t 1231.1Spgoyettespdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg) 1241.1Spgoyette{ 1251.1Spgoyette uint8_t val; 1261.1Spgoyette struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc; 1271.1Spgoyette 1281.1Spgoyette iic_acquire_bus(sc->sc_tag, 0); 1291.1Spgoyette iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, ®, 1, 1301.7Skiyohara &val, 1, I2C_F_POLL); 1311.1Spgoyette iic_release_bus(sc->sc_tag, 0); 1321.1Spgoyette 1331.1Spgoyette return val; 1341.1Spgoyette} 1351.2Spgoyette 1361.10SjmcneillMODULE(MODULE_CLASS_DRIVER, spdmem, "i2cexec"); 1371.2Spgoyette 1381.2Spgoyette#ifdef _MODULE 1391.2Spgoyette#include "ioconf.c" 1401.2Spgoyette#endif 1411.2Spgoyette 1421.2Spgoyettestatic int 1431.2Spgoyettespdmem_modcmd(modcmd_t cmd, void *opaque) 1441.2Spgoyette{ 1451.2Spgoyette int error = 0; 1461.5Spgoyette#ifdef _MODULE 1471.5Spgoyette static struct sysctllog *spdmem_sysctl_clog; 1481.5Spgoyette#endif 1491.2Spgoyette 1501.2Spgoyette switch (cmd) { 1511.2Spgoyette case MODULE_CMD_INIT: 1521.2Spgoyette#ifdef _MODULE 1531.2Spgoyette error = config_init_component(cfdriver_ioconf_spdmem, 1541.2Spgoyette cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 1551.2Spgoyette#endif 1561.2Spgoyette return error; 1571.2Spgoyette case MODULE_CMD_FINI: 1581.2Spgoyette#ifdef _MODULE 1591.2Spgoyette error = config_fini_component(cfdriver_ioconf_spdmem, 1601.2Spgoyette cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 1611.5Spgoyette sysctl_teardown(&spdmem_sysctl_clog); 1621.2Spgoyette#endif 1631.2Spgoyette return error; 1641.2Spgoyette default: 1651.2Spgoyette return ENOTTY; 1661.2Spgoyette } 1671.2Spgoyette} 168