13fffc223Sths /* 23fffc223Sths * QEMU SMBus EEPROM device 33fffc223Sths * 43fffc223Sths * Copyright (c) 2007 Arastra, Inc. 53fffc223Sths * 63fffc223Sths * Permission is hereby granted, free of charge, to any person obtaining a copy 73fffc223Sths * of this software and associated documentation files (the "Software"), to deal 83fffc223Sths * in the Software without restriction, including without limitation the rights 93fffc223Sths * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 103fffc223Sths * copies of the Software, and to permit persons to whom the Software is 113fffc223Sths * furnished to do so, subject to the following conditions: 123fffc223Sths * 133fffc223Sths * The above copyright notice and this permission notice shall be included in 143fffc223Sths * all copies or substantial portions of the Software. 153fffc223Sths * 163fffc223Sths * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 173fffc223Sths * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 183fffc223Sths * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 193fffc223Sths * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 203fffc223Sths * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 213fffc223Sths * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 223fffc223Sths * THE SOFTWARE. 233fffc223Sths */ 243fffc223Sths 250430891cSPeter Maydell #include "qemu/osdep.h" 26b296b664SBALATON Zoltan #include "qemu/units.h" 27b296b664SBALATON Zoltan #include "qapi/error.h" 2883c9f4caSPaolo Bonzini #include "hw/hw.h" 29fd9df33fSCorey Minyard #include "hw/boards.h" 300d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h" 3193198b6cSCorey Minyard #include "hw/i2c/smbus_slave.h" 3293198b6cSCorey Minyard #include "hw/i2c/smbus_eeprom.h" 333fffc223Sths 343fffc223Sths //#define DEBUG 353fffc223Sths 36b398a924SCorey Minyard #define TYPE_SMBUS_EEPROM "smbus-eeprom" 37b398a924SCorey Minyard 38b398a924SCorey Minyard #define SMBUS_EEPROM(obj) \ 39b398a924SCorey Minyard OBJECT_CHECK(SMBusEEPROMDevice, (obj), TYPE_SMBUS_EEPROM) 40b398a924SCorey Minyard 410cf487e5SCorey Minyard #define SMBUS_EEPROM_SIZE 256 420cf487e5SCorey Minyard 433fffc223Sths typedef struct SMBusEEPROMDevice { 441ea96673SPaul Brook SMBusDevice smbusdev; 45fd9df33fSCorey Minyard uint8_t data[SMBUS_EEPROM_SIZE]; 46fd9df33fSCorey Minyard void *init_data; 473fffc223Sths uint8_t offset; 48fd9df33fSCorey Minyard bool accessed; 493fffc223Sths } SMBusEEPROMDevice; 503fffc223Sths 513fffc223Sths static uint8_t eeprom_receive_byte(SMBusDevice *dev) 523fffc223Sths { 53b398a924SCorey Minyard SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev); 54bf2782d7SGerd Hoffmann uint8_t *data = eeprom->data; 55bf2782d7SGerd Hoffmann uint8_t val = data[eeprom->offset++]; 568b38e532SCorey Minyard 57fd9df33fSCorey Minyard eeprom->accessed = true; 583fffc223Sths #ifdef DEBUG 59ab7d9131Sbalrog printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", 60ab7d9131Sbalrog dev->i2c.address, val); 613fffc223Sths #endif 623fffc223Sths return val; 633fffc223Sths } 643fffc223Sths 659cf27d74SCorey Minyard static int eeprom_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len) 663fffc223Sths { 67b398a924SCorey Minyard SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev); 689cf27d74SCorey Minyard uint8_t *data = eeprom->data; 699cf27d74SCorey Minyard 70fd9df33fSCorey Minyard eeprom->accessed = true; 713fffc223Sths #ifdef DEBUG 72ab7d9131Sbalrog printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", 739cf27d74SCorey Minyard dev->i2c.address, buf[0], buf[1]); 743fffc223Sths #endif 759cf27d74SCorey Minyard /* len is guaranteed to be > 0 */ 769cf27d74SCorey Minyard eeprom->offset = buf[0]; 779cf27d74SCorey Minyard buf++; 789cf27d74SCorey Minyard len--; 799cf27d74SCorey Minyard 809cf27d74SCorey Minyard for (; len > 0; len--) { 819cf27d74SCorey Minyard data[eeprom->offset] = *buf++; 820cf487e5SCorey Minyard eeprom->offset = (eeprom->offset + 1) % SMBUS_EEPROM_SIZE; 833fffc223Sths } 843fffc223Sths 859cf27d74SCorey Minyard return 0; 869cf27d74SCorey Minyard } 879cf27d74SCorey Minyard 88fd9df33fSCorey Minyard static bool smbus_eeprom_vmstate_needed(void *opaque) 89fd9df33fSCorey Minyard { 90fd9df33fSCorey Minyard MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); 91fd9df33fSCorey Minyard SMBusEEPROMDevice *eeprom = opaque; 92fd9df33fSCorey Minyard 93fd9df33fSCorey Minyard return (eeprom->accessed || smbus_vmstate_needed(&eeprom->smbusdev)) && 94fd9df33fSCorey Minyard !mc->smbus_no_migration_support; 95fd9df33fSCorey Minyard } 96fd9df33fSCorey Minyard 97fd9df33fSCorey Minyard static const VMStateDescription vmstate_smbus_eeprom = { 98fd9df33fSCorey Minyard .name = "smbus-eeprom", 99fd9df33fSCorey Minyard .version_id = 1, 100fd9df33fSCorey Minyard .minimum_version_id = 1, 101fd9df33fSCorey Minyard .needed = smbus_eeprom_vmstate_needed, 102fd9df33fSCorey Minyard .fields = (VMStateField[]) { 103fd9df33fSCorey Minyard VMSTATE_SMBUS_DEVICE(smbusdev, SMBusEEPROMDevice), 104fd9df33fSCorey Minyard VMSTATE_UINT8_ARRAY(data, SMBusEEPROMDevice, SMBUS_EEPROM_SIZE), 105fd9df33fSCorey Minyard VMSTATE_UINT8(offset, SMBusEEPROMDevice), 106fd9df33fSCorey Minyard VMSTATE_BOOL(accessed, SMBusEEPROMDevice), 107fd9df33fSCorey Minyard VMSTATE_END_OF_LIST() 108fd9df33fSCorey Minyard } 109fd9df33fSCorey Minyard }; 110fd9df33fSCorey Minyard 1111042b22dSCorey Minyard /* 1121042b22dSCorey Minyard * Reset the EEPROM contents to the initial state on a reset. This 1131042b22dSCorey Minyard * isn't really how an EEPROM works, of course, but the general 1141042b22dSCorey Minyard * principle of QEMU is to restore function on reset to what it would 1151042b22dSCorey Minyard * be if QEMU was stopped and started. 1161042b22dSCorey Minyard * 1171042b22dSCorey Minyard * The proper thing to do would be to have a backing blockdev to hold 1181042b22dSCorey Minyard * the contents and restore that on startup, and not do this on reset. 1191042b22dSCorey Minyard * But until that time, act as if we had been stopped and restarted. 1201042b22dSCorey Minyard */ 1211042b22dSCorey Minyard static void smbus_eeprom_reset(DeviceState *dev) 1223fffc223Sths { 123b398a924SCorey Minyard SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev); 1240ff596d0Spbrook 125fd9df33fSCorey Minyard memcpy(eeprom->data, eeprom->init_data, SMBUS_EEPROM_SIZE); 1263fffc223Sths eeprom->offset = 0; 1273fffc223Sths } 1281ea96673SPaul Brook 1291042b22dSCorey Minyard static void smbus_eeprom_realize(DeviceState *dev, Error **errp) 1301042b22dSCorey Minyard { 1311042b22dSCorey Minyard smbus_eeprom_reset(dev); 1321042b22dSCorey Minyard } 1331042b22dSCorey Minyard 13439bffca2SAnthony Liguori static Property smbus_eeprom_properties[] = { 135fd9df33fSCorey Minyard DEFINE_PROP_PTR("data", SMBusEEPROMDevice, init_data), 13639bffca2SAnthony Liguori DEFINE_PROP_END_OF_LIST(), 13739bffca2SAnthony Liguori }; 13839bffca2SAnthony Liguori 139b5ea9327SAnthony Liguori static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data) 140b5ea9327SAnthony Liguori { 14139bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 142b5ea9327SAnthony Liguori SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass); 143b5ea9327SAnthony Liguori 14419473e51SPhilippe Mathieu-Daudé dc->realize = smbus_eeprom_realize; 1451042b22dSCorey Minyard dc->reset = smbus_eeprom_reset; 146b5ea9327SAnthony Liguori sc->receive_byte = eeprom_receive_byte; 147b5ea9327SAnthony Liguori sc->write_data = eeprom_write_data; 14839bffca2SAnthony Liguori dc->props = smbus_eeprom_properties; 149fd9df33fSCorey Minyard dc->vmsd = &vmstate_smbus_eeprom; 1501b111dc1SMarkus Armbruster /* Reason: pointer property "data" */ 151e90f2a8cSEduardo Habkost dc->user_creatable = false; 152b5ea9327SAnthony Liguori } 153b5ea9327SAnthony Liguori 1548c43a6f0SAndreas Färber static const TypeInfo smbus_eeprom_info = { 155b398a924SCorey Minyard .name = TYPE_SMBUS_EEPROM, 15639bffca2SAnthony Liguori .parent = TYPE_SMBUS_DEVICE, 15739bffca2SAnthony Liguori .instance_size = sizeof(SMBusEEPROMDevice), 158b5ea9327SAnthony Liguori .class_init = smbus_eeprom_class_initfn, 1591ea96673SPaul Brook }; 1601ea96673SPaul Brook 16183f7d43aSAndreas Färber static void smbus_eeprom_register_types(void) 1621ea96673SPaul Brook { 16339bffca2SAnthony Liguori type_register_static(&smbus_eeprom_info); 1641ea96673SPaul Brook } 1651ea96673SPaul Brook 16683f7d43aSAndreas Färber type_init(smbus_eeprom_register_types) 167a88df0b9SIsaku Yamahata 168e2224214SCédric Le Goater void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf) 169e2224214SCédric Le Goater { 170e2224214SCédric Le Goater DeviceState *dev; 171e2224214SCédric Le Goater 172b398a924SCorey Minyard dev = qdev_create((BusState *) smbus, TYPE_SMBUS_EEPROM); 173e2224214SCédric Le Goater qdev_prop_set_uint8(dev, "address", address); 174e2224214SCédric Le Goater qdev_prop_set_ptr(dev, "data", eeprom_buf); 175e2224214SCédric Le Goater qdev_init_nofail(dev); 176e2224214SCédric Le Goater } 177e2224214SCédric Le Goater 178a5c82852SAndreas Färber void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom, 179a88df0b9SIsaku Yamahata const uint8_t *eeprom_spd, int eeprom_spd_size) 180a88df0b9SIsaku Yamahata { 181a88df0b9SIsaku Yamahata int i; 1820cf487e5SCorey Minyard /* XXX: make this persistent */ 183*c203d451SCorey Minyard 184*c203d451SCorey Minyard assert(nb_eeprom <= 8); 1850cf487e5SCorey Minyard uint8_t *eeprom_buf = g_malloc0(8 * SMBUS_EEPROM_SIZE); 186a88df0b9SIsaku Yamahata if (eeprom_spd_size > 0) { 187a88df0b9SIsaku Yamahata memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size); 188a88df0b9SIsaku Yamahata } 189a88df0b9SIsaku Yamahata 190a88df0b9SIsaku Yamahata for (i = 0; i < nb_eeprom; i++) { 1910cf487e5SCorey Minyard smbus_eeprom_init_one(smbus, 0x50 + i, 1920cf487e5SCorey Minyard eeprom_buf + (i * SMBUS_EEPROM_SIZE)); 193a88df0b9SIsaku Yamahata } 194a88df0b9SIsaku Yamahata } 195b296b664SBALATON Zoltan 196b296b664SBALATON Zoltan /* Generate SDRAM SPD EEPROM data describing a module of type and size */ 197b296b664SBALATON Zoltan uint8_t *spd_data_generate(enum sdram_type type, ram_addr_t ram_size, 198b296b664SBALATON Zoltan Error **errp) 199b296b664SBALATON Zoltan { 200b296b664SBALATON Zoltan uint8_t *spd; 201b296b664SBALATON Zoltan uint8_t nbanks; 202b296b664SBALATON Zoltan uint16_t density; 203b296b664SBALATON Zoltan uint32_t size; 204b296b664SBALATON Zoltan int min_log2, max_log2, sz_log2; 205b296b664SBALATON Zoltan int i; 206b296b664SBALATON Zoltan 207b296b664SBALATON Zoltan switch (type) { 208b296b664SBALATON Zoltan case SDR: 209b296b664SBALATON Zoltan min_log2 = 2; 210b296b664SBALATON Zoltan max_log2 = 9; 211b296b664SBALATON Zoltan break; 212b296b664SBALATON Zoltan case DDR: 213b296b664SBALATON Zoltan min_log2 = 5; 214b296b664SBALATON Zoltan max_log2 = 12; 215b296b664SBALATON Zoltan break; 216b296b664SBALATON Zoltan case DDR2: 217b296b664SBALATON Zoltan min_log2 = 7; 218b296b664SBALATON Zoltan max_log2 = 14; 219b296b664SBALATON Zoltan break; 220b296b664SBALATON Zoltan default: 221b296b664SBALATON Zoltan g_assert_not_reached(); 222b296b664SBALATON Zoltan } 223b296b664SBALATON Zoltan size = ram_size >> 20; /* work in terms of megabytes */ 224b296b664SBALATON Zoltan if (size < 4) { 225b296b664SBALATON Zoltan error_setg(errp, "SDRAM size is too small"); 226b296b664SBALATON Zoltan return NULL; 227b296b664SBALATON Zoltan } 228b296b664SBALATON Zoltan sz_log2 = 31 - clz32(size); 229b296b664SBALATON Zoltan size = 1U << sz_log2; 230b296b664SBALATON Zoltan if (ram_size > size * MiB) { 231b296b664SBALATON Zoltan error_setg(errp, "SDRAM size 0x"RAM_ADDR_FMT" is not a power of 2, " 232b296b664SBALATON Zoltan "truncating to %u MB", ram_size, size); 233b296b664SBALATON Zoltan } 234b296b664SBALATON Zoltan if (sz_log2 < min_log2) { 235b296b664SBALATON Zoltan error_setg(errp, 236b296b664SBALATON Zoltan "Memory size is too small for SDRAM type, adjusting type"); 237b296b664SBALATON Zoltan if (size >= 32) { 238b296b664SBALATON Zoltan type = DDR; 239b296b664SBALATON Zoltan min_log2 = 5; 240b296b664SBALATON Zoltan max_log2 = 12; 241b296b664SBALATON Zoltan } else { 242b296b664SBALATON Zoltan type = SDR; 243b296b664SBALATON Zoltan min_log2 = 2; 244b296b664SBALATON Zoltan max_log2 = 9; 245b296b664SBALATON Zoltan } 246b296b664SBALATON Zoltan } 247b296b664SBALATON Zoltan 248b296b664SBALATON Zoltan nbanks = 1; 249b296b664SBALATON Zoltan while (sz_log2 > max_log2 && nbanks < 8) { 250b296b664SBALATON Zoltan sz_log2--; 251b296b664SBALATON Zoltan nbanks++; 252b296b664SBALATON Zoltan } 253b296b664SBALATON Zoltan 254b296b664SBALATON Zoltan if (size > (1ULL << sz_log2) * nbanks) { 255b296b664SBALATON Zoltan error_setg(errp, "Memory size is too big for SDRAM, truncating"); 256b296b664SBALATON Zoltan } 257b296b664SBALATON Zoltan 258b296b664SBALATON Zoltan /* split to 2 banks if possible to avoid a bug in MIPS Malta firmware */ 259b296b664SBALATON Zoltan if (nbanks == 1 && sz_log2 > min_log2) { 260b296b664SBALATON Zoltan sz_log2--; 261b296b664SBALATON Zoltan nbanks++; 262b296b664SBALATON Zoltan } 263b296b664SBALATON Zoltan 264b296b664SBALATON Zoltan density = 1ULL << (sz_log2 - 2); 265b296b664SBALATON Zoltan switch (type) { 266b296b664SBALATON Zoltan case DDR2: 267b296b664SBALATON Zoltan density = (density & 0xe0) | (density >> 8 & 0x1f); 268b296b664SBALATON Zoltan break; 269b296b664SBALATON Zoltan case DDR: 270b296b664SBALATON Zoltan density = (density & 0xf8) | (density >> 8 & 0x07); 271b296b664SBALATON Zoltan break; 272b296b664SBALATON Zoltan case SDR: 273b296b664SBALATON Zoltan default: 274b296b664SBALATON Zoltan density &= 0xff; 275b296b664SBALATON Zoltan break; 276b296b664SBALATON Zoltan } 277b296b664SBALATON Zoltan 278b296b664SBALATON Zoltan spd = g_malloc0(256); 279b296b664SBALATON Zoltan spd[0] = 128; /* data bytes in EEPROM */ 280b296b664SBALATON Zoltan spd[1] = 8; /* log2 size of EEPROM */ 281b296b664SBALATON Zoltan spd[2] = type; 282b296b664SBALATON Zoltan spd[3] = 13; /* row address bits */ 283b296b664SBALATON Zoltan spd[4] = 10; /* column address bits */ 284b296b664SBALATON Zoltan spd[5] = (type == DDR2 ? nbanks - 1 : nbanks); 285b296b664SBALATON Zoltan spd[6] = 64; /* module data width */ 286b296b664SBALATON Zoltan /* reserved / data width high */ 287b296b664SBALATON Zoltan spd[8] = 4; /* interface voltage level */ 288b296b664SBALATON Zoltan spd[9] = 0x25; /* highest CAS latency */ 289b296b664SBALATON Zoltan spd[10] = 1; /* access time */ 290b296b664SBALATON Zoltan /* DIMM configuration 0 = non-ECC */ 291b296b664SBALATON Zoltan spd[12] = 0x82; /* refresh requirements */ 292b296b664SBALATON Zoltan spd[13] = 8; /* primary SDRAM width */ 293b296b664SBALATON Zoltan /* ECC SDRAM width */ 294b296b664SBALATON Zoltan spd[15] = (type == DDR2 ? 0 : 1); /* reserved / delay for random col rd */ 295b296b664SBALATON Zoltan spd[16] = 12; /* burst lengths supported */ 296b296b664SBALATON Zoltan spd[17] = 4; /* banks per SDRAM device */ 297b296b664SBALATON Zoltan spd[18] = 12; /* ~CAS latencies supported */ 298b296b664SBALATON Zoltan spd[19] = (type == DDR2 ? 0 : 1); /* reserved / ~CS latencies supported */ 299b296b664SBALATON Zoltan spd[20] = 2; /* DIMM type / ~WE latencies */ 300b296b664SBALATON Zoltan /* module features */ 301b296b664SBALATON Zoltan /* memory chip features */ 302b296b664SBALATON Zoltan spd[23] = 0x12; /* clock cycle time @ medium CAS latency */ 303b296b664SBALATON Zoltan /* data access time */ 304b296b664SBALATON Zoltan /* clock cycle time @ short CAS latency */ 305b296b664SBALATON Zoltan /* data access time */ 306b296b664SBALATON Zoltan spd[27] = 20; /* min. row precharge time */ 307b296b664SBALATON Zoltan spd[28] = 15; /* min. row active row delay */ 308b296b664SBALATON Zoltan spd[29] = 20; /* min. ~RAS to ~CAS delay */ 309b296b664SBALATON Zoltan spd[30] = 45; /* min. active to precharge time */ 310b296b664SBALATON Zoltan spd[31] = density; 311b296b664SBALATON Zoltan spd[32] = 20; /* addr/cmd setup time */ 312b296b664SBALATON Zoltan spd[33] = 8; /* addr/cmd hold time */ 313b296b664SBALATON Zoltan spd[34] = 20; /* data input setup time */ 314b296b664SBALATON Zoltan spd[35] = 8; /* data input hold time */ 315b296b664SBALATON Zoltan 316b296b664SBALATON Zoltan /* checksum */ 317b296b664SBALATON Zoltan for (i = 0; i < 63; i++) { 318b296b664SBALATON Zoltan spd[63] += spd[i]; 319b296b664SBALATON Zoltan } 320b296b664SBALATON Zoltan return spd; 321b296b664SBALATON Zoltan } 322