xref: /qemu/hw/i2c/smbus_eeprom.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
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"
28fd9df33fSCorey Minyard #include "hw/boards.h"
290d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h"
3093198b6cSCorey Minyard #include "hw/i2c/smbus_slave.h"
31a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
32d6454270SMarkus Armbruster #include "migration/vmstate.h"
3393198b6cSCorey Minyard #include "hw/i2c/smbus_eeprom.h"
34db1015e9SEduardo Habkost #include "qom/object.h"
353fffc223Sths 
363fffc223Sths //#define DEBUG
373fffc223Sths 
38b398a924SCorey Minyard #define TYPE_SMBUS_EEPROM "smbus-eeprom"
39b398a924SCorey Minyard 
408063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SMBusEEPROMDevice, SMBUS_EEPROM)
41b398a924SCorey Minyard 
420cf487e5SCorey Minyard #define SMBUS_EEPROM_SIZE 256
430cf487e5SCorey Minyard 
44db1015e9SEduardo Habkost struct SMBusEEPROMDevice {
451ea96673SPaul Brook     SMBusDevice smbusdev;
46fd9df33fSCorey Minyard     uint8_t data[SMBUS_EEPROM_SIZE];
47b9751d20SMarc-André Lureau     uint8_t *init_data;
483fffc223Sths     uint8_t offset;
49fd9df33fSCorey Minyard     bool accessed;
50db1015e9SEduardo Habkost };
513fffc223Sths 
eeprom_receive_byte(SMBusDevice * dev)523fffc223Sths static uint8_t eeprom_receive_byte(SMBusDevice *dev)
533fffc223Sths {
54b398a924SCorey Minyard     SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
55bf2782d7SGerd Hoffmann     uint8_t *data = eeprom->data;
56bf2782d7SGerd Hoffmann     uint8_t val = data[eeprom->offset++];
578b38e532SCorey Minyard 
58fd9df33fSCorey Minyard     eeprom->accessed = true;
593fffc223Sths #ifdef DEBUG
60ab7d9131Sbalrog     printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
61ab7d9131Sbalrog            dev->i2c.address, val);
623fffc223Sths #endif
633fffc223Sths     return val;
643fffc223Sths }
653fffc223Sths 
eeprom_write_data(SMBusDevice * dev,uint8_t * buf,uint8_t len)669cf27d74SCorey Minyard static int eeprom_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len)
673fffc223Sths {
68b398a924SCorey Minyard     SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
699cf27d74SCorey Minyard     uint8_t *data = eeprom->data;
709cf27d74SCorey Minyard 
71fd9df33fSCorey Minyard     eeprom->accessed = true;
723fffc223Sths #ifdef DEBUG
73ab7d9131Sbalrog     printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
749cf27d74SCorey Minyard            dev->i2c.address, buf[0], buf[1]);
753fffc223Sths #endif
769cf27d74SCorey Minyard     /* len is guaranteed to be > 0 */
779cf27d74SCorey Minyard     eeprom->offset = buf[0];
789cf27d74SCorey Minyard     buf++;
799cf27d74SCorey Minyard     len--;
809cf27d74SCorey Minyard 
819cf27d74SCorey Minyard     for (; len > 0; len--) {
829cf27d74SCorey Minyard         data[eeprom->offset] = *buf++;
830cf487e5SCorey Minyard         eeprom->offset = (eeprom->offset + 1) % SMBUS_EEPROM_SIZE;
843fffc223Sths     }
853fffc223Sths 
869cf27d74SCorey Minyard     return 0;
879cf27d74SCorey Minyard }
889cf27d74SCorey Minyard 
smbus_eeprom_vmstate_needed(void * opaque)89fd9df33fSCorey Minyard static bool smbus_eeprom_vmstate_needed(void *opaque)
90fd9df33fSCorey Minyard {
91fd9df33fSCorey Minyard     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
92fd9df33fSCorey Minyard     SMBusEEPROMDevice *eeprom = opaque;
93fd9df33fSCorey Minyard 
94fd9df33fSCorey Minyard     return (eeprom->accessed || smbus_vmstate_needed(&eeprom->smbusdev)) &&
95fd9df33fSCorey Minyard         !mc->smbus_no_migration_support;
96fd9df33fSCorey Minyard }
97fd9df33fSCorey Minyard 
98fd9df33fSCorey Minyard static const VMStateDescription vmstate_smbus_eeprom = {
99fd9df33fSCorey Minyard     .name = "smbus-eeprom",
100fd9df33fSCorey Minyard     .version_id = 1,
101fd9df33fSCorey Minyard     .minimum_version_id = 1,
102fd9df33fSCorey Minyard     .needed = smbus_eeprom_vmstate_needed,
10301d9442aSRichard Henderson     .fields = (const VMStateField[]) {
104fd9df33fSCorey Minyard         VMSTATE_SMBUS_DEVICE(smbusdev, SMBusEEPROMDevice),
105fd9df33fSCorey Minyard         VMSTATE_UINT8_ARRAY(data, SMBusEEPROMDevice, SMBUS_EEPROM_SIZE),
106fd9df33fSCorey Minyard         VMSTATE_UINT8(offset, SMBusEEPROMDevice),
107fd9df33fSCorey Minyard         VMSTATE_BOOL(accessed, SMBusEEPROMDevice),
108fd9df33fSCorey Minyard         VMSTATE_END_OF_LIST()
109fd9df33fSCorey Minyard     }
110fd9df33fSCorey Minyard };
111fd9df33fSCorey Minyard 
1121042b22dSCorey Minyard /*
1131042b22dSCorey Minyard  * Reset the EEPROM contents to the initial state on a reset.  This
1141042b22dSCorey Minyard  * isn't really how an EEPROM works, of course, but the general
1151042b22dSCorey Minyard  * principle of QEMU is to restore function on reset to what it would
1161042b22dSCorey Minyard  * be if QEMU was stopped and started.
1171042b22dSCorey Minyard  *
1181042b22dSCorey Minyard  * The proper thing to do would be to have a backing blockdev to hold
1191042b22dSCorey Minyard  * the contents and restore that on startup, and not do this on reset.
1201042b22dSCorey Minyard  * But until that time, act as if we had been stopped and restarted.
1211042b22dSCorey Minyard  */
smbus_eeprom_reset(DeviceState * dev)1221042b22dSCorey Minyard static void smbus_eeprom_reset(DeviceState *dev)
1233fffc223Sths {
124b398a924SCorey Minyard     SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
1250ff596d0Spbrook 
126fd9df33fSCorey Minyard     memcpy(eeprom->data, eeprom->init_data, SMBUS_EEPROM_SIZE);
1273fffc223Sths     eeprom->offset = 0;
1283fffc223Sths }
1291ea96673SPaul Brook 
smbus_eeprom_realize(DeviceState * dev,Error ** errp)1301042b22dSCorey Minyard static void smbus_eeprom_realize(DeviceState *dev, Error **errp)
1311042b22dSCorey Minyard {
132b9751d20SMarc-André Lureau     SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
1331042b22dSCorey Minyard 
134b9751d20SMarc-André Lureau     smbus_eeprom_reset(dev);
135b9751d20SMarc-André Lureau     if (eeprom->init_data == NULL) {
136b9751d20SMarc-André Lureau         error_setg(errp, "init_data cannot be NULL");
137b9751d20SMarc-André Lureau     }
138b9751d20SMarc-André Lureau }
13939bffca2SAnthony Liguori 
smbus_eeprom_class_initfn(ObjectClass * klass,const void * data)140*12d1a768SPhilippe Mathieu-Daudé static void smbus_eeprom_class_initfn(ObjectClass *klass, const void *data)
141b5ea9327SAnthony Liguori {
14239bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
143b5ea9327SAnthony Liguori     SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
144b5ea9327SAnthony Liguori 
14519473e51SPhilippe Mathieu-Daudé     dc->realize = smbus_eeprom_realize;
146e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, smbus_eeprom_reset);
147b5ea9327SAnthony Liguori     sc->receive_byte = eeprom_receive_byte;
148b5ea9327SAnthony Liguori     sc->write_data = eeprom_write_data;
149fd9df33fSCorey Minyard     dc->vmsd = &vmstate_smbus_eeprom;
150b9751d20SMarc-André Lureau     /* Reason: init_data */
151e90f2a8cSEduardo Habkost     dc->user_creatable = false;
152b5ea9327SAnthony Liguori }
153b5ea9327SAnthony Liguori 
15434965e89SBernhard Beschow static const TypeInfo smbus_eeprom_types[] = {
15534965e89SBernhard Beschow     {
156b398a924SCorey Minyard         .name          = TYPE_SMBUS_EEPROM,
15739bffca2SAnthony Liguori         .parent        = TYPE_SMBUS_DEVICE,
15839bffca2SAnthony Liguori         .instance_size = sizeof(SMBusEEPROMDevice),
159b5ea9327SAnthony Liguori         .class_init    = smbus_eeprom_class_initfn,
16034965e89SBernhard Beschow     },
1611ea96673SPaul Brook };
1621ea96673SPaul Brook 
DEFINE_TYPES(smbus_eeprom_types)16334965e89SBernhard Beschow DEFINE_TYPES(smbus_eeprom_types)
164a88df0b9SIsaku Yamahata 
165e2224214SCédric Le Goater void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
166e2224214SCédric Le Goater {
167e2224214SCédric Le Goater     DeviceState *dev;
168e2224214SCédric Le Goater 
1693e80f690SMarkus Armbruster     dev = qdev_new(TYPE_SMBUS_EEPROM);
170e2224214SCédric Le Goater     qdev_prop_set_uint8(dev, "address", address);
171b9751d20SMarc-André Lureau     /* FIXME: use an array of byte or block backend property? */
172b9751d20SMarc-André Lureau     SMBUS_EEPROM(dev)->init_data = eeprom_buf;
1733e80f690SMarkus Armbruster     qdev_realize_and_unref(dev, (BusState *)smbus, &error_fatal);
174e2224214SCédric Le Goater }
175e2224214SCédric Le Goater 
smbus_eeprom_init(I2CBus * smbus,int nb_eeprom,const uint8_t * eeprom_spd,int eeprom_spd_size)176a5c82852SAndreas Färber void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
177a88df0b9SIsaku Yamahata                        const uint8_t *eeprom_spd, int eeprom_spd_size)
178a88df0b9SIsaku Yamahata {
179a88df0b9SIsaku Yamahata     int i;
1800cf487e5SCorey Minyard      /* XXX: make this persistent */
181c203d451SCorey Minyard 
182c203d451SCorey Minyard     assert(nb_eeprom <= 8);
1830cf487e5SCorey Minyard     uint8_t *eeprom_buf = g_malloc0(8 * SMBUS_EEPROM_SIZE);
184a88df0b9SIsaku Yamahata     if (eeprom_spd_size > 0) {
185a88df0b9SIsaku Yamahata         memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
186a88df0b9SIsaku Yamahata     }
187a88df0b9SIsaku Yamahata 
188a88df0b9SIsaku Yamahata     for (i = 0; i < nb_eeprom; i++) {
1890cf487e5SCorey Minyard         smbus_eeprom_init_one(smbus, 0x50 + i,
1900cf487e5SCorey Minyard                               eeprom_buf + (i * SMBUS_EEPROM_SIZE));
191a88df0b9SIsaku Yamahata     }
192a88df0b9SIsaku Yamahata }
193b296b664SBALATON Zoltan 
194b296b664SBALATON Zoltan /* Generate SDRAM SPD EEPROM data describing a module of type and size */
spd_data_generate(enum sdram_type type,ram_addr_t ram_size)195f26740c6SMarkus Armbruster uint8_t *spd_data_generate(enum sdram_type type, ram_addr_t ram_size)
196b296b664SBALATON Zoltan {
197b296b664SBALATON Zoltan     uint8_t *spd;
198b296b664SBALATON Zoltan     uint8_t nbanks;
199b296b664SBALATON Zoltan     uint16_t density;
200b296b664SBALATON Zoltan     uint32_t size;
201b296b664SBALATON Zoltan     int min_log2, max_log2, sz_log2;
202b296b664SBALATON Zoltan     int i;
203b296b664SBALATON Zoltan 
204b296b664SBALATON Zoltan     switch (type) {
205b296b664SBALATON Zoltan     case SDR:
206b296b664SBALATON Zoltan         min_log2 = 2;
207b296b664SBALATON Zoltan         max_log2 = 9;
208b296b664SBALATON Zoltan         break;
209b296b664SBALATON Zoltan     case DDR:
210b296b664SBALATON Zoltan         min_log2 = 5;
211b296b664SBALATON Zoltan         max_log2 = 12;
212b296b664SBALATON Zoltan         break;
213b296b664SBALATON Zoltan     case DDR2:
214b296b664SBALATON Zoltan         min_log2 = 7;
215b296b664SBALATON Zoltan         max_log2 = 14;
216b296b664SBALATON Zoltan         break;
217b296b664SBALATON Zoltan     default:
218b296b664SBALATON Zoltan         g_assert_not_reached();
219b296b664SBALATON Zoltan     }
220b296b664SBALATON Zoltan     size = ram_size >> 20; /* work in terms of megabytes */
221b296b664SBALATON Zoltan     sz_log2 = 31 - clz32(size);
222b296b664SBALATON Zoltan     size = 1U << sz_log2;
223f26740c6SMarkus Armbruster     assert(ram_size == size * MiB);
224f26740c6SMarkus Armbruster     assert(sz_log2 >= min_log2);
225b296b664SBALATON Zoltan 
226b296b664SBALATON Zoltan     nbanks = 1;
227b296b664SBALATON Zoltan     while (sz_log2 > max_log2 && nbanks < 8) {
228b296b664SBALATON Zoltan         sz_log2--;
22932c82f0eSMarkus Armbruster         nbanks *= 2;
230b296b664SBALATON Zoltan     }
231b296b664SBALATON Zoltan 
232f26740c6SMarkus Armbruster     assert(size == (1ULL << sz_log2) * nbanks);
233b296b664SBALATON Zoltan 
234b296b664SBALATON Zoltan     /* split to 2 banks if possible to avoid a bug in MIPS Malta firmware */
235b296b664SBALATON Zoltan     if (nbanks == 1 && sz_log2 > min_log2) {
236b296b664SBALATON Zoltan         sz_log2--;
237b296b664SBALATON Zoltan         nbanks++;
238b296b664SBALATON Zoltan     }
239b296b664SBALATON Zoltan 
240b296b664SBALATON Zoltan     density = 1ULL << (sz_log2 - 2);
241b296b664SBALATON Zoltan     switch (type) {
242b296b664SBALATON Zoltan     case DDR2:
243b296b664SBALATON Zoltan         density = (density & 0xe0) | (density >> 8 & 0x1f);
244b296b664SBALATON Zoltan         break;
245b296b664SBALATON Zoltan     case DDR:
246b296b664SBALATON Zoltan         density = (density & 0xf8) | (density >> 8 & 0x07);
247b296b664SBALATON Zoltan         break;
248b296b664SBALATON Zoltan     case SDR:
249b296b664SBALATON Zoltan     default:
250b296b664SBALATON Zoltan         density &= 0xff;
251b296b664SBALATON Zoltan         break;
252b296b664SBALATON Zoltan     }
253b296b664SBALATON Zoltan 
254b296b664SBALATON Zoltan     spd = g_malloc0(256);
255b296b664SBALATON Zoltan     spd[0] = 128;   /* data bytes in EEPROM */
256b296b664SBALATON Zoltan     spd[1] = 8;     /* log2 size of EEPROM */
257b296b664SBALATON Zoltan     spd[2] = type;
258b296b664SBALATON Zoltan     spd[3] = 13;    /* row address bits */
259b296b664SBALATON Zoltan     spd[4] = 10;    /* column address bits */
260b296b664SBALATON Zoltan     spd[5] = (type == DDR2 ? nbanks - 1 : nbanks);
261b296b664SBALATON Zoltan     spd[6] = 64;    /* module data width */
262b296b664SBALATON Zoltan                     /* reserved / data width high */
263b296b664SBALATON Zoltan     spd[8] = 4;     /* interface voltage level */
264b296b664SBALATON Zoltan     spd[9] = 0x25;  /* highest CAS latency */
265b296b664SBALATON Zoltan     spd[10] = 1;    /* access time */
266b296b664SBALATON Zoltan                     /* DIMM configuration 0 = non-ECC */
267b296b664SBALATON Zoltan     spd[12] = 0x82; /* refresh requirements */
268b296b664SBALATON Zoltan     spd[13] = 8;    /* primary SDRAM width */
269b296b664SBALATON Zoltan                     /* ECC SDRAM width */
270b296b664SBALATON Zoltan     spd[15] = (type == DDR2 ? 0 : 1); /* reserved / delay for random col rd */
271b296b664SBALATON Zoltan     spd[16] = 12;   /* burst lengths supported */
272b296b664SBALATON Zoltan     spd[17] = 4;    /* banks per SDRAM device */
273b296b664SBALATON Zoltan     spd[18] = 12;   /* ~CAS latencies supported */
274b296b664SBALATON Zoltan     spd[19] = (type == DDR2 ? 0 : 1); /* reserved / ~CS latencies supported */
275b296b664SBALATON Zoltan     spd[20] = 2;    /* DIMM type / ~WE latencies */
276793abe24SBALATON Zoltan     spd[21] = (type < DDR2 ? 0x20 : 0); /* module features */
277b296b664SBALATON Zoltan                     /* memory chip features */
278b296b664SBALATON Zoltan     spd[23] = 0x12; /* clock cycle time @ medium CAS latency */
279b296b664SBALATON Zoltan                     /* data access time */
280b296b664SBALATON Zoltan                     /* clock cycle time @ short CAS latency */
281b296b664SBALATON Zoltan                     /* data access time */
282b296b664SBALATON Zoltan     spd[27] = 20;   /* min. row precharge time */
283b296b664SBALATON Zoltan     spd[28] = 15;   /* min. row active row delay */
284b296b664SBALATON Zoltan     spd[29] = 20;   /* min. ~RAS to ~CAS delay */
285b296b664SBALATON Zoltan     spd[30] = 45;   /* min. active to precharge time */
286b296b664SBALATON Zoltan     spd[31] = density;
287b296b664SBALATON Zoltan     spd[32] = 20;   /* addr/cmd setup time */
288b296b664SBALATON Zoltan     spd[33] = 8;    /* addr/cmd hold time */
289b296b664SBALATON Zoltan     spd[34] = 20;   /* data input setup time */
290b296b664SBALATON Zoltan     spd[35] = 8;    /* data input hold time */
291b296b664SBALATON Zoltan 
292b296b664SBALATON Zoltan     /* checksum */
293b296b664SBALATON Zoltan     for (i = 0; i < 63; i++) {
294b296b664SBALATON Zoltan         spd[63] += spd[i];
295b296b664SBALATON Zoltan     }
296b296b664SBALATON Zoltan     return spd;
297b296b664SBALATON Zoltan }
298