xref: /qemu/hw/i2c/smbus_eeprom.c (revision 9cf27d74a829f651c0da5d80c014a6cef9d4cbd8)
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"
290d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h"
3093198b6cSCorey Minyard #include "hw/i2c/smbus_slave.h"
3193198b6cSCorey Minyard #include "hw/i2c/smbus_eeprom.h"
323fffc223Sths 
333fffc223Sths //#define DEBUG
343fffc223Sths 
353fffc223Sths typedef struct SMBusEEPROMDevice {
361ea96673SPaul Brook     SMBusDevice smbusdev;
37bf2782d7SGerd Hoffmann     void *data;
383fffc223Sths     uint8_t offset;
393fffc223Sths } SMBusEEPROMDevice;
403fffc223Sths 
413fffc223Sths static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
423fffc223Sths {
433fffc223Sths #ifdef DEBUG
44ab7d9131Sbalrog     printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
453fffc223Sths #endif
463fffc223Sths }
473fffc223Sths 
483fffc223Sths static uint8_t eeprom_receive_byte(SMBusDevice *dev)
493fffc223Sths {
503fffc223Sths     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
51bf2782d7SGerd Hoffmann     uint8_t *data = eeprom->data;
52bf2782d7SGerd Hoffmann     uint8_t val = data[eeprom->offset++];
533fffc223Sths #ifdef DEBUG
54ab7d9131Sbalrog     printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
55ab7d9131Sbalrog            dev->i2c.address, val);
563fffc223Sths #endif
573fffc223Sths     return val;
583fffc223Sths }
593fffc223Sths 
60*9cf27d74SCorey Minyard static int eeprom_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len)
613fffc223Sths {
623fffc223Sths     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
63*9cf27d74SCorey Minyard     uint8_t *data = eeprom->data;
64*9cf27d74SCorey Minyard 
653fffc223Sths #ifdef DEBUG
66ab7d9131Sbalrog     printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
67*9cf27d74SCorey Minyard            dev->i2c.address, buf[0], buf[1]);
683fffc223Sths #endif
69*9cf27d74SCorey Minyard     /* len is guaranteed to be > 0 */
70*9cf27d74SCorey Minyard     eeprom->offset = buf[0];
71*9cf27d74SCorey Minyard     buf++;
72*9cf27d74SCorey Minyard     len--;
73*9cf27d74SCorey Minyard 
74*9cf27d74SCorey Minyard     for (; len > 0; len--) {
75*9cf27d74SCorey Minyard         data[eeprom->offset] = *buf++;
76*9cf27d74SCorey Minyard         eeprom->offset = (eeprom->offset + 1) % 256;
773fffc223Sths     }
783fffc223Sths 
79*9cf27d74SCorey Minyard     return 0;
80*9cf27d74SCorey Minyard }
81*9cf27d74SCorey Minyard 
82*9cf27d74SCorey Minyard static uint8_t eeprom_read_data(SMBusDevice *dev, int n)
833fffc223Sths {
840ff596d0Spbrook     /* As with writes, we implement block reads without the
850ff596d0Spbrook        SMBus length byte.  */
860ff596d0Spbrook     return eeprom_receive_byte(dev);
873fffc223Sths }
883fffc223Sths 
8919473e51SPhilippe Mathieu-Daudé static void smbus_eeprom_realize(DeviceState *dev, Error **errp)
903fffc223Sths {
911ea96673SPaul Brook     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
920ff596d0Spbrook 
933fffc223Sths     eeprom->offset = 0;
943fffc223Sths }
951ea96673SPaul Brook 
9639bffca2SAnthony Liguori static Property smbus_eeprom_properties[] = {
9739bffca2SAnthony Liguori     DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data),
9839bffca2SAnthony Liguori     DEFINE_PROP_END_OF_LIST(),
9939bffca2SAnthony Liguori };
10039bffca2SAnthony Liguori 
101b5ea9327SAnthony Liguori static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data)
102b5ea9327SAnthony Liguori {
10339bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
104b5ea9327SAnthony Liguori     SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
105b5ea9327SAnthony Liguori 
10619473e51SPhilippe Mathieu-Daudé     dc->realize = smbus_eeprom_realize;
107b5ea9327SAnthony Liguori     sc->quick_cmd = eeprom_quick_cmd;
108b5ea9327SAnthony Liguori     sc->receive_byte = eeprom_receive_byte;
109b5ea9327SAnthony Liguori     sc->write_data = eeprom_write_data;
110b5ea9327SAnthony Liguori     sc->read_data = eeprom_read_data;
11139bffca2SAnthony Liguori     dc->props = smbus_eeprom_properties;
1121b111dc1SMarkus Armbruster     /* Reason: pointer property "data" */
113e90f2a8cSEduardo Habkost     dc->user_creatable = false;
114b5ea9327SAnthony Liguori }
115b5ea9327SAnthony Liguori 
1168c43a6f0SAndreas Färber static const TypeInfo smbus_eeprom_info = {
117b5ea9327SAnthony Liguori     .name          = "smbus-eeprom",
11839bffca2SAnthony Liguori     .parent        = TYPE_SMBUS_DEVICE,
11939bffca2SAnthony Liguori     .instance_size = sizeof(SMBusEEPROMDevice),
120b5ea9327SAnthony Liguori     .class_init    = smbus_eeprom_class_initfn,
1211ea96673SPaul Brook };
1221ea96673SPaul Brook 
12383f7d43aSAndreas Färber static void smbus_eeprom_register_types(void)
1241ea96673SPaul Brook {
12539bffca2SAnthony Liguori     type_register_static(&smbus_eeprom_info);
1261ea96673SPaul Brook }
1271ea96673SPaul Brook 
12883f7d43aSAndreas Färber type_init(smbus_eeprom_register_types)
129a88df0b9SIsaku Yamahata 
130e2224214SCédric Le Goater void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
131e2224214SCédric Le Goater {
132e2224214SCédric Le Goater     DeviceState *dev;
133e2224214SCédric Le Goater 
134e2224214SCédric Le Goater     dev = qdev_create((BusState *) smbus, "smbus-eeprom");
135e2224214SCédric Le Goater     qdev_prop_set_uint8(dev, "address", address);
136e2224214SCédric Le Goater     qdev_prop_set_ptr(dev, "data", eeprom_buf);
137e2224214SCédric Le Goater     qdev_init_nofail(dev);
138e2224214SCédric Le Goater }
139e2224214SCédric Le Goater 
140a5c82852SAndreas Färber void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
141a88df0b9SIsaku Yamahata                        const uint8_t *eeprom_spd, int eeprom_spd_size)
142a88df0b9SIsaku Yamahata {
143a88df0b9SIsaku Yamahata     int i;
1447267c094SAnthony Liguori     uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */
145a88df0b9SIsaku Yamahata     if (eeprom_spd_size > 0) {
146a88df0b9SIsaku Yamahata         memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
147a88df0b9SIsaku Yamahata     }
148a88df0b9SIsaku Yamahata 
149a88df0b9SIsaku Yamahata     for (i = 0; i < nb_eeprom; i++) {
150e2224214SCédric Le Goater         smbus_eeprom_init_one(smbus, 0x50 + i, eeprom_buf + (i * 256));
151a88df0b9SIsaku Yamahata     }
152a88df0b9SIsaku Yamahata }
153b296b664SBALATON Zoltan 
154b296b664SBALATON Zoltan /* Generate SDRAM SPD EEPROM data describing a module of type and size */
155b296b664SBALATON Zoltan uint8_t *spd_data_generate(enum sdram_type type, ram_addr_t ram_size,
156b296b664SBALATON Zoltan                            Error **errp)
157b296b664SBALATON Zoltan {
158b296b664SBALATON Zoltan     uint8_t *spd;
159b296b664SBALATON Zoltan     uint8_t nbanks;
160b296b664SBALATON Zoltan     uint16_t density;
161b296b664SBALATON Zoltan     uint32_t size;
162b296b664SBALATON Zoltan     int min_log2, max_log2, sz_log2;
163b296b664SBALATON Zoltan     int i;
164b296b664SBALATON Zoltan 
165b296b664SBALATON Zoltan     switch (type) {
166b296b664SBALATON Zoltan     case SDR:
167b296b664SBALATON Zoltan         min_log2 = 2;
168b296b664SBALATON Zoltan         max_log2 = 9;
169b296b664SBALATON Zoltan         break;
170b296b664SBALATON Zoltan     case DDR:
171b296b664SBALATON Zoltan         min_log2 = 5;
172b296b664SBALATON Zoltan         max_log2 = 12;
173b296b664SBALATON Zoltan         break;
174b296b664SBALATON Zoltan     case DDR2:
175b296b664SBALATON Zoltan         min_log2 = 7;
176b296b664SBALATON Zoltan         max_log2 = 14;
177b296b664SBALATON Zoltan         break;
178b296b664SBALATON Zoltan     default:
179b296b664SBALATON Zoltan         g_assert_not_reached();
180b296b664SBALATON Zoltan     }
181b296b664SBALATON Zoltan     size = ram_size >> 20; /* work in terms of megabytes */
182b296b664SBALATON Zoltan     if (size < 4) {
183b296b664SBALATON Zoltan         error_setg(errp, "SDRAM size is too small");
184b296b664SBALATON Zoltan         return NULL;
185b296b664SBALATON Zoltan     }
186b296b664SBALATON Zoltan     sz_log2 = 31 - clz32(size);
187b296b664SBALATON Zoltan     size = 1U << sz_log2;
188b296b664SBALATON Zoltan     if (ram_size > size * MiB) {
189b296b664SBALATON Zoltan         error_setg(errp, "SDRAM size 0x"RAM_ADDR_FMT" is not a power of 2, "
190b296b664SBALATON Zoltan                    "truncating to %u MB", ram_size, size);
191b296b664SBALATON Zoltan     }
192b296b664SBALATON Zoltan     if (sz_log2 < min_log2) {
193b296b664SBALATON Zoltan         error_setg(errp,
194b296b664SBALATON Zoltan                    "Memory size is too small for SDRAM type, adjusting type");
195b296b664SBALATON Zoltan         if (size >= 32) {
196b296b664SBALATON Zoltan             type = DDR;
197b296b664SBALATON Zoltan             min_log2 = 5;
198b296b664SBALATON Zoltan             max_log2 = 12;
199b296b664SBALATON Zoltan         } else {
200b296b664SBALATON Zoltan             type = SDR;
201b296b664SBALATON Zoltan             min_log2 = 2;
202b296b664SBALATON Zoltan             max_log2 = 9;
203b296b664SBALATON Zoltan         }
204b296b664SBALATON Zoltan     }
205b296b664SBALATON Zoltan 
206b296b664SBALATON Zoltan     nbanks = 1;
207b296b664SBALATON Zoltan     while (sz_log2 > max_log2 && nbanks < 8) {
208b296b664SBALATON Zoltan         sz_log2--;
209b296b664SBALATON Zoltan         nbanks++;
210b296b664SBALATON Zoltan     }
211b296b664SBALATON Zoltan 
212b296b664SBALATON Zoltan     if (size > (1ULL << sz_log2) * nbanks) {
213b296b664SBALATON Zoltan         error_setg(errp, "Memory size is too big for SDRAM, truncating");
214b296b664SBALATON Zoltan     }
215b296b664SBALATON Zoltan 
216b296b664SBALATON Zoltan     /* split to 2 banks if possible to avoid a bug in MIPS Malta firmware */
217b296b664SBALATON Zoltan     if (nbanks == 1 && sz_log2 > min_log2) {
218b296b664SBALATON Zoltan         sz_log2--;
219b296b664SBALATON Zoltan         nbanks++;
220b296b664SBALATON Zoltan     }
221b296b664SBALATON Zoltan 
222b296b664SBALATON Zoltan     density = 1ULL << (sz_log2 - 2);
223b296b664SBALATON Zoltan     switch (type) {
224b296b664SBALATON Zoltan     case DDR2:
225b296b664SBALATON Zoltan         density = (density & 0xe0) | (density >> 8 & 0x1f);
226b296b664SBALATON Zoltan         break;
227b296b664SBALATON Zoltan     case DDR:
228b296b664SBALATON Zoltan         density = (density & 0xf8) | (density >> 8 & 0x07);
229b296b664SBALATON Zoltan         break;
230b296b664SBALATON Zoltan     case SDR:
231b296b664SBALATON Zoltan     default:
232b296b664SBALATON Zoltan         density &= 0xff;
233b296b664SBALATON Zoltan         break;
234b296b664SBALATON Zoltan     }
235b296b664SBALATON Zoltan 
236b296b664SBALATON Zoltan     spd = g_malloc0(256);
237b296b664SBALATON Zoltan     spd[0] = 128;   /* data bytes in EEPROM */
238b296b664SBALATON Zoltan     spd[1] = 8;     /* log2 size of EEPROM */
239b296b664SBALATON Zoltan     spd[2] = type;
240b296b664SBALATON Zoltan     spd[3] = 13;    /* row address bits */
241b296b664SBALATON Zoltan     spd[4] = 10;    /* column address bits */
242b296b664SBALATON Zoltan     spd[5] = (type == DDR2 ? nbanks - 1 : nbanks);
243b296b664SBALATON Zoltan     spd[6] = 64;    /* module data width */
244b296b664SBALATON Zoltan                     /* reserved / data width high */
245b296b664SBALATON Zoltan     spd[8] = 4;     /* interface voltage level */
246b296b664SBALATON Zoltan     spd[9] = 0x25;  /* highest CAS latency */
247b296b664SBALATON Zoltan     spd[10] = 1;    /* access time */
248b296b664SBALATON Zoltan                     /* DIMM configuration 0 = non-ECC */
249b296b664SBALATON Zoltan     spd[12] = 0x82; /* refresh requirements */
250b296b664SBALATON Zoltan     spd[13] = 8;    /* primary SDRAM width */
251b296b664SBALATON Zoltan                     /* ECC SDRAM width */
252b296b664SBALATON Zoltan     spd[15] = (type == DDR2 ? 0 : 1); /* reserved / delay for random col rd */
253b296b664SBALATON Zoltan     spd[16] = 12;   /* burst lengths supported */
254b296b664SBALATON Zoltan     spd[17] = 4;    /* banks per SDRAM device */
255b296b664SBALATON Zoltan     spd[18] = 12;   /* ~CAS latencies supported */
256b296b664SBALATON Zoltan     spd[19] = (type == DDR2 ? 0 : 1); /* reserved / ~CS latencies supported */
257b296b664SBALATON Zoltan     spd[20] = 2;    /* DIMM type / ~WE latencies */
258b296b664SBALATON Zoltan                     /* module features */
259b296b664SBALATON Zoltan                     /* memory chip features */
260b296b664SBALATON Zoltan     spd[23] = 0x12; /* clock cycle time @ medium CAS latency */
261b296b664SBALATON Zoltan                     /* data access time */
262b296b664SBALATON Zoltan                     /* clock cycle time @ short CAS latency */
263b296b664SBALATON Zoltan                     /* data access time */
264b296b664SBALATON Zoltan     spd[27] = 20;   /* min. row precharge time */
265b296b664SBALATON Zoltan     spd[28] = 15;   /* min. row active row delay */
266b296b664SBALATON Zoltan     spd[29] = 20;   /* min. ~RAS to ~CAS delay */
267b296b664SBALATON Zoltan     spd[30] = 45;   /* min. active to precharge time */
268b296b664SBALATON Zoltan     spd[31] = density;
269b296b664SBALATON Zoltan     spd[32] = 20;   /* addr/cmd setup time */
270b296b664SBALATON Zoltan     spd[33] = 8;    /* addr/cmd hold time */
271b296b664SBALATON Zoltan     spd[34] = 20;   /* data input setup time */
272b296b664SBALATON Zoltan     spd[35] = 8;    /* data input hold time */
273b296b664SBALATON Zoltan 
274b296b664SBALATON Zoltan     /* checksum */
275b296b664SBALATON Zoltan     for (i = 0; i < 63; i++) {
276b296b664SBALATON Zoltan         spd[63] += spd[i];
277b296b664SBALATON Zoltan     }
278b296b664SBALATON Zoltan     return spd;
279b296b664SBALATON Zoltan }
280