1 /* $NetBSD: spdmem_i2c.c,v 1.3 2011/10/02 19:03:56 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicolas Joly 5 * Copyright (c) 2007 Paul Goyette 6 * Copyright (c) 2007 Tobias Nygren 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Serial Presence Detect (SPD) memory identification 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: spdmem_i2c.c,v 1.3 2011/10/02 19:03:56 jmcneill Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/device.h> 42 #include <sys/endian.h> 43 #include <sys/module.h> 44 #include <sys/sysctl.h> 45 #include <machine/bswap.h> 46 47 #include <dev/i2c/i2cvar.h> 48 #include <dev/ic/spdmemreg.h> 49 #include <dev/ic/spdmemvar.h> 50 51 /* Constants for matching i2c bus address */ 52 #define SPDMEM_I2C_ADDRMASK 0x78 53 #define SPDMEM_I2C_ADDR 0x50 54 55 struct spdmem_i2c_softc { 56 struct spdmem_softc sc_base; 57 i2c_tag_t sc_tag; 58 i2c_addr_t sc_addr; 59 }; 60 61 static int spdmem_i2c_match(device_t, cfdata_t, void *); 62 static void spdmem_i2c_attach(device_t, device_t, void *); 63 static int spdmem_i2c_detach(device_t, int); 64 65 CFATTACH_DECL_NEW(spdmem_iic, sizeof(struct spdmem_i2c_softc), 66 spdmem_i2c_match, spdmem_i2c_attach, spdmem_i2c_detach, NULL); 67 68 static uint8_t spdmem_i2c_read(struct spdmem_softc *, uint8_t); 69 70 SYSCTL_SETUP_PROTO(sysctl_spdmem_setup); 71 72 static int 73 spdmem_i2c_match(device_t parent, cfdata_t match, void *aux) 74 { 75 struct i2c_attach_args *ia = aux; 76 struct spdmem_i2c_softc sc; 77 78 if (ia->ia_name) { 79 /* add other names as we find more firmware variations */ 80 if (strcmp(ia->ia_name, "dimm-spd")) 81 return 0; 82 } 83 84 /* only do this lame test when not using direct config */ 85 if (ia->ia_name == NULL) { 86 if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR) 87 return 0; 88 } 89 90 sc.sc_tag = ia->ia_tag; 91 sc.sc_addr = ia->ia_addr; 92 sc.sc_base.sc_read = spdmem_i2c_read; 93 94 return spdmem_common_probe(&sc.sc_base); 95 } 96 97 static void 98 spdmem_i2c_attach(device_t parent, device_t self, void *aux) 99 { 100 struct spdmem_i2c_softc *sc = device_private(self); 101 struct i2c_attach_args *ia = aux; 102 103 sc->sc_tag = ia->ia_tag; 104 sc->sc_addr = ia->ia_addr; 105 sc->sc_base.sc_read = spdmem_i2c_read; 106 107 if (!pmf_device_register(self, NULL, NULL)) 108 aprint_error_dev(self, "couldn't establish power handler\n"); 109 110 spdmem_common_attach(&sc->sc_base, self); 111 } 112 113 static int 114 spdmem_i2c_detach(device_t self, int flags) 115 { 116 struct spdmem_i2c_softc *sc = device_private(self); 117 118 pmf_device_deregister(self); 119 120 return spdmem_common_detach(&sc->sc_base, self); 121 } 122 123 static uint8_t 124 spdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg) 125 { 126 uint8_t val; 127 struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc; 128 129 iic_acquire_bus(sc->sc_tag, 0); 130 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, ®, 1, 131 &val, 1, 0); 132 iic_release_bus(sc->sc_tag, 0); 133 134 return val; 135 } 136 137 MODULE(MODULE_CLASS_DRIVER, spdmem, "iic"); 138 139 #ifdef _MODULE 140 #include "ioconf.c" 141 #endif 142 143 static int 144 spdmem_modcmd(modcmd_t cmd, void *opaque) 145 { 146 int error = 0; 147 148 switch (cmd) { 149 case MODULE_CMD_INIT: 150 #ifdef _MODULE 151 error = config_init_component(cfdriver_ioconf_spdmem, 152 cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 153 #endif 154 return error; 155 case MODULE_CMD_FINI: 156 #ifdef _MODULE 157 error = config_fini_component(cfdriver_ioconf_spdmem, 158 cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 159 #endif 160 return error; 161 default: 162 return ENOTTY; 163 } 164 } 165