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 "hw/hw.h"
26#include "hw/i2c/i2c.h"
27#include "hw/i2c/smbus.h"
28
29
30
31typedef struct SMBusEEPROMDevice {
32 SMBusDevice smbusdev;
33 void *data;
34 uint8_t offset;
35} SMBusEEPROMDevice;
36
37static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
38{
39#ifdef DEBUG
40 printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
41#endif
42}
43
44static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
45{
46 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
47#ifdef DEBUG
48 printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
49 dev->i2c.address, val);
50#endif
51 eeprom->offset = val;
52}
53
54static uint8_t eeprom_receive_byte(SMBusDevice *dev)
55{
56 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
57 uint8_t *data = eeprom->data;
58 uint8_t val = data[eeprom->offset++];
59#ifdef DEBUG
60 printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
61 dev->i2c.address, val);
62#endif
63 return val;
64}
65
66static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
67{
68 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
69 int n;
70#ifdef DEBUG
71 printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
72 dev->i2c.address, cmd, buf[0]);
73#endif
74
75
76
77
78 if (cmd + len > 256)
79 n = 256 - cmd;
80 else
81 n = len;
82 memcpy(eeprom->data + cmd, buf, n);
83 len -= n;
84 if (len)
85 memcpy(eeprom->data, buf + n, len);
86}
87
88static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
89{
90 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
91
92 if (n == 0)
93 eeprom->offset = cmd;
94
95
96 return eeprom_receive_byte(dev);
97}
98
99static int smbus_eeprom_initfn(SMBusDevice *dev)
100{
101 SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
102
103 eeprom->offset = 0;
104 return 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 sc->init = smbus_eeprom_initfn;
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->cannot_instantiate_with_device_add_yet = true;
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(I2CBus *smbus, int nb_eeprom,
143 const uint8_t *eeprom_spd, int eeprom_spd_size)
144{
145 int i;
146 uint8_t *eeprom_buf = g_malloc0(8 * 256);
147 if (eeprom_spd_size > 0) {
148 memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
149 }
150
151 for (i = 0; i < nb_eeprom; i++) {
152 DeviceState *eeprom;
153 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
154 qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
155 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
156 qdev_init_nofail(eeprom);
157 }
158}
159