1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "hw/hw.h"
27#include "hw/i2c/i2c.h"
28#include "hw/i2c/smbus.h"
29
30
31
32typedef struct SMBusEEPROMDevice {
33 SMBusDevice smbusdev;
34 void *data;
35 uint8_t offset;
36} SMBusEEPROMDevice;
37
38static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
39{
40#ifdef DEBUG
41 printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
42#endif
43}
44
45static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
46{
47 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
48#ifdef DEBUG
49 printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
50 dev->i2c.address, val);
51#endif
52 eeprom->offset = val;
53}
54
55static uint8_t eeprom_receive_byte(SMBusDevice *dev)
56{
57 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
58 uint8_t *data = eeprom->data;
59 uint8_t val = data[eeprom->offset++];
60#ifdef DEBUG
61 printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
62 dev->i2c.address, val);
63#endif
64 return val;
65}
66
67static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
68{
69 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
70 int n;
71#ifdef DEBUG
72 printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
73 dev->i2c.address, cmd, buf[0]);
74#endif
75
76
77
78
79 if (cmd + len > 256)
80 n = 256 - cmd;
81 else
82 n = len;
83 memcpy(eeprom->data + cmd, buf, n);
84 len -= n;
85 if (len)
86 memcpy(eeprom->data, buf + n, len);
87}
88
89static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
90{
91 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
92
93 if (n == 0)
94 eeprom->offset = cmd;
95
96
97 return eeprom_receive_byte(dev);
98}
99
100static void smbus_eeprom_realize(DeviceState *dev, Error **errp)
101{
102 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
103
104 eeprom->offset = 0;
105}
106
107static Property smbus_eeprom_properties[] = {
108 DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data),
109 DEFINE_PROP_END_OF_LIST(),
110};
111
112static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data)
113{
114 DeviceClass *dc = DEVICE_CLASS(klass);
115 SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
116
117 dc->realize = smbus_eeprom_realize;
118 sc->quick_cmd = eeprom_quick_cmd;
119 sc->send_byte = eeprom_send_byte;
120 sc->receive_byte = eeprom_receive_byte;
121 sc->write_data = eeprom_write_data;
122 sc->read_data = eeprom_read_data;
123 dc->props = smbus_eeprom_properties;
124
125 dc->user_creatable = false;
126}
127
128static const TypeInfo smbus_eeprom_info = {
129 .name = "smbus-eeprom",
130 .parent = TYPE_SMBUS_DEVICE,
131 .instance_size = sizeof(SMBusEEPROMDevice),
132 .class_init = smbus_eeprom_class_initfn,
133};
134
135static void smbus_eeprom_register_types(void)
136{
137 type_register_static(&smbus_eeprom_info);
138}
139
140type_init(smbus_eeprom_register_types)
141
142void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
143{
144 DeviceState *dev;
145
146 dev = qdev_create((BusState *) smbus, "smbus-eeprom");
147 qdev_prop_set_uint8(dev, "address", address);
148 qdev_prop_set_ptr(dev, "data", eeprom_buf);
149 qdev_init_nofail(dev);
150}
151
152void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
153 const uint8_t *eeprom_spd, int eeprom_spd_size)
154{
155 int i;
156 uint8_t *eeprom_buf = g_malloc0(8 * 256);
157 if (eeprom_spd_size > 0) {
158 memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
159 }
160
161 for (i = 0; i < nb_eeprom; i++) {
162 smbus_eeprom_init_one(smbus, 0x50 + i, eeprom_buf + (i * 256));
163 }
164}
165