1/* $NetBSD: memory.c,v 1.4 2011/06/18 08:08:28 matt Exp $ */ 2/* $OpenBSD: mem.c,v 1.15 2007/10/14 17:29:04 kettenis Exp $ */ 3 4/*- 5 * Copyright (c) 1988 University of Utah. 6 * Copyright (c) 1982, 1986, 1990, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)mem.c 8.3 (Berkeley) 1/12/94 38 */ 39 40/* 41 * The EEPROMs for Serial Presence Detect don't show up in the 42 * OpenFirmware tree, but their contents are available through the 43 * "dimm-info" property of the "/memory" node. To make the 44 * information available, we fake up an I2C bus with EEPROMs 45 * containing the appropriate slices of the "dimm-info" property. 46 */ 47 48#include <sys/cdefs.h> 49__KERNEL_RCSID(0, "$NetBSD: memory.c,v 1.4 2011/06/18 08:08:28 matt Exp $"); 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/types.h> 54#include <sys/malloc.h> 55#include <sys/device.h> 56 57#include <machine/autoconf.h> 58 59#include <dev/i2c/i2cvar.h> 60#include <dev/ofw/openfirm.h> 61 62struct memory_softc { 63 device_t sc_dev; 64 65 u_char *sc_buf; 66 int sc_len; 67}; 68 69/* Size of a single SPD entry in "dimm-info" property. */ 70#define SPD_SIZE 128 71 72int memory_match(device_t, cfdata_t, void *); 73void memory_attach(device_t, device_t, void *); 74 75CFATTACH_DECL_NEW(memory, sizeof(struct memory_softc), memory_match, memory_attach, 76 NULL, NULL); 77 78int memory_i2c_acquire_bus(void *, int); 79void memory_i2c_release_bus(void *, int); 80int memory_i2c_exec(void *, i2c_op_t, i2c_addr_t, 81 const void *, size_t, void *, size_t, int); 82 83int 84memory_match(device_t parent, cfdata_t cf, void *aux) 85{ 86 struct confargs *ca = aux; 87 88 if (strcmp(ca->ca_name, "memory") == 0) 89 return (1); 90 return (0); 91} 92 93void 94memory_attach(device_t parent, device_t self, void *aux) 95{ 96 struct memory_softc *sc = device_private(self); 97 struct confargs *ca = aux; 98 struct i2c_controller ic; 99 struct i2c_attach_args ia; 100 101 sc->sc_dev = self; 102 sc->sc_len = OF_getproplen(ca->ca_node, "dimm-info"); 103 if (sc->sc_len > 0) { 104 sc->sc_buf = malloc(sc->sc_len, M_DEVBUF, M_NOWAIT); 105 if (sc->sc_buf == NULL) { 106 printf(": can't allocate memory\n"); 107 return; 108 } 109 printf(": len=%d", sc->sc_len); 110 } 111 112 printf("\n"); 113 114 if (sc->sc_len > 0) { 115 int addr; 116 117 OF_getprop(ca->ca_node, "dimm-info", sc->sc_buf, sc->sc_len); 118 119 memset(&ic, 0, sizeof ic); 120 ic.ic_cookie = sc; 121 ic.ic_acquire_bus = memory_i2c_acquire_bus; 122 ic.ic_release_bus = memory_i2c_release_bus; 123 ic.ic_exec = memory_i2c_exec; 124 125 memset(&ia, 0, sizeof ia); 126 ia.ia_tag = ⁣ 127#ifdef notyet 128 ia.ia_name = "spd"; 129#endif 130 addr = 0; 131 while ((addr * SPD_SIZE) < sc->sc_len) { 132 /* Skip entries that have not been filled in. */ 133 if (sc->sc_buf[addr * SPD_SIZE] != 0) { 134 ia.ia_addr = 0x50 + addr; 135 config_found(self, &ia, NULL); 136 } 137 addr++; 138 } 139 140 /* No need to keep the "dimm-info" contents around. */ 141 free(sc->sc_buf, M_DEVBUF); 142 sc->sc_len = -1; 143 } 144} 145 146int 147memory_i2c_acquire_bus(void *cookie, int flags) 148{ 149 return (0); 150} 151 152void 153memory_i2c_release_bus(void *cookie, int flags) 154{ 155} 156 157int 158memory_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, 159 const void *cmdbuf, size_t cmdlen, 160 void *buf, size_t len, int flags) 161{ 162 struct memory_softc *sc = cookie; 163 size_t off; 164 165 if (op != I2C_OP_READ_WITH_STOP || cmdlen != 1) 166 return (EINVAL); 167 168 off = (addr - 0x50) * SPD_SIZE + *(const u_char *)cmdbuf; 169 if ((off + len) > sc->sc_len) 170 return (EIO); 171 172 memcpy(buf, &sc->sc_buf[off], len); 173 return (0); 174} 175 176 177