1 /* $NetBSD: spdmem_i2c.c,v 1.9 2014/04/14 14:30:24 pooka 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.9 2014/04/14 14:30:24 pooka 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 0x3f8 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 static int 71 spdmem_i2c_match(device_t parent, cfdata_t match, void *aux) 72 { 73 struct i2c_attach_args *ia = aux; 74 struct spdmem_i2c_softc sc; 75 76 if (ia->ia_name) { 77 /* add other names as we find more firmware variations */ 78 if (strcmp(ia->ia_name, "dimm-spd") && 79 strcmp(ia->ia_name, "dimm")) 80 return 0; 81 } 82 83 /* only do this lame test when not using direct config */ 84 if (ia->ia_name == NULL) { 85 if ((ia->ia_addr & SPDMEM_I2C_ADDRMASK) != SPDMEM_I2C_ADDR) 86 return 0; 87 } 88 89 sc.sc_tag = ia->ia_tag; 90 sc.sc_addr = ia->ia_addr; 91 sc.sc_base.sc_read = spdmem_i2c_read; 92 93 return spdmem_common_probe(&sc.sc_base); 94 } 95 96 static void 97 spdmem_i2c_attach(device_t parent, device_t self, void *aux) 98 { 99 struct spdmem_i2c_softc *sc = device_private(self); 100 struct i2c_attach_args *ia = aux; 101 102 sc->sc_tag = ia->ia_tag; 103 sc->sc_addr = ia->ia_addr; 104 sc->sc_base.sc_read = spdmem_i2c_read; 105 106 if (!pmf_device_register(self, NULL, NULL)) 107 aprint_error_dev(self, "couldn't establish power handler\n"); 108 109 spdmem_common_attach(&sc->sc_base, self); 110 } 111 112 static int 113 spdmem_i2c_detach(device_t self, int flags) 114 { 115 struct spdmem_i2c_softc *sc = device_private(self); 116 117 pmf_device_deregister(self); 118 119 return spdmem_common_detach(&sc->sc_base, self); 120 } 121 122 static uint8_t 123 spdmem_i2c_read(struct spdmem_softc *softc, uint8_t reg) 124 { 125 uint8_t val; 126 struct spdmem_i2c_softc *sc = (struct spdmem_i2c_softc *)softc; 127 128 iic_acquire_bus(sc->sc_tag, 0); 129 iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, ®, 1, 130 &val, 1, I2C_F_POLL); 131 iic_release_bus(sc->sc_tag, 0); 132 133 return val; 134 } 135 136 MODULE(MODULE_CLASS_DRIVER, spdmem, "iic"); 137 138 #ifdef _MODULE 139 #include "ioconf.c" 140 #endif 141 142 static int 143 spdmem_modcmd(modcmd_t cmd, void *opaque) 144 { 145 int error = 0; 146 #ifdef _MODULE 147 static struct sysctllog *spdmem_sysctl_clog; 148 #endif 149 150 switch (cmd) { 151 case MODULE_CMD_INIT: 152 #ifdef _MODULE 153 error = config_init_component(cfdriver_ioconf_spdmem, 154 cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 155 #endif 156 return error; 157 case MODULE_CMD_FINI: 158 #ifdef _MODULE 159 error = config_fini_component(cfdriver_ioconf_spdmem, 160 cfattach_ioconf_spdmem, cfdata_ioconf_spdmem); 161 sysctl_teardown(&spdmem_sysctl_clog); 162 #endif 163 return error; 164 default: 165 return ENOTTY; 166 } 167 } 168