]>
Commit | Line | Data |
---|---|---|
3fffc223 TS |
1 | /* |
2 | * QEMU SMBus EEPROM device | |
5fafdf24 | 3 | * |
3fffc223 | 4 | * Copyright (c) 2007 Arastra, Inc. |
5fafdf24 | 5 | * |
3fffc223 TS |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
0430891c | 25 | #include "qemu/osdep.h" |
83c9f4ca | 26 | #include "hw/hw.h" |
0d09e41a PB |
27 | #include "hw/i2c/i2c.h" |
28 | #include "hw/i2c/smbus.h" | |
3fffc223 TS |
29 | |
30 | //#define DEBUG | |
31 | ||
32 | typedef struct SMBusEEPROMDevice { | |
1ea96673 | 33 | SMBusDevice smbusdev; |
bf2782d7 | 34 | void *data; |
3fffc223 TS |
35 | uint8_t offset; |
36 | } SMBusEEPROMDevice; | |
37 | ||
38 | static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read) | |
39 | { | |
40 | #ifdef DEBUG | |
ab7d9131 | 41 | printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read); |
3fffc223 TS |
42 | #endif |
43 | } | |
44 | ||
45 | static void eeprom_send_byte(SMBusDevice *dev, uint8_t val) | |
46 | { | |
47 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
48 | #ifdef DEBUG | |
ab7d9131 AZ |
49 | printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n", |
50 | dev->i2c.address, val); | |
3fffc223 TS |
51 | #endif |
52 | eeprom->offset = val; | |
53 | } | |
54 | ||
55 | static uint8_t eeprom_receive_byte(SMBusDevice *dev) | |
56 | { | |
57 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
bf2782d7 GH |
58 | uint8_t *data = eeprom->data; |
59 | uint8_t val = data[eeprom->offset++]; | |
3fffc223 | 60 | #ifdef DEBUG |
ab7d9131 AZ |
61 | printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n", |
62 | dev->i2c.address, val); | |
3fffc223 TS |
63 | #endif |
64 | return val; | |
65 | } | |
66 | ||
0ff596d0 | 67 | static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len) |
3fffc223 TS |
68 | { |
69 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
0ff596d0 | 70 | int n; |
3fffc223 | 71 | #ifdef DEBUG |
ab7d9131 AZ |
72 | printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n", |
73 | dev->i2c.address, cmd, buf[0]); | |
3fffc223 | 74 | #endif |
b36dc67b | 75 | /* A page write operation is not a valid SMBus command. |
0ff596d0 PB |
76 | It is a block write without a length byte. Fortunately we |
77 | get the full block anyway. */ | |
78 | /* TODO: Should this set the current location? */ | |
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); | |
3fffc223 TS |
87 | } |
88 | ||
0ff596d0 | 89 | static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n) |
3fffc223 TS |
90 | { |
91 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev; | |
0ff596d0 PB |
92 | /* If this is the first byte then set the current position. */ |
93 | if (n == 0) | |
94 | eeprom->offset = cmd; | |
95 | /* As with writes, we implement block reads without the | |
96 | SMBus length byte. */ | |
97 | return eeprom_receive_byte(dev); | |
3fffc223 TS |
98 | } |
99 | ||
a88df0b9 | 100 | static int smbus_eeprom_initfn(SMBusDevice *dev) |
3fffc223 | 101 | { |
1ea96673 | 102 | SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev; |
3b46e624 | 103 | |
3fffc223 | 104 | eeprom->offset = 0; |
81a322d4 | 105 | return 0; |
3fffc223 | 106 | } |
1ea96673 | 107 | |
39bffca2 AL |
108 | static Property smbus_eeprom_properties[] = { |
109 | DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data), | |
110 | DEFINE_PROP_END_OF_LIST(), | |
111 | }; | |
112 | ||
b5ea9327 AL |
113 | static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data) |
114 | { | |
39bffca2 | 115 | DeviceClass *dc = DEVICE_CLASS(klass); |
b5ea9327 AL |
116 | SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass); |
117 | ||
118 | sc->init = smbus_eeprom_initfn; | |
119 | sc->quick_cmd = eeprom_quick_cmd; | |
120 | sc->send_byte = eeprom_send_byte; | |
121 | sc->receive_byte = eeprom_receive_byte; | |
122 | sc->write_data = eeprom_write_data; | |
123 | sc->read_data = eeprom_read_data; | |
39bffca2 | 124 | dc->props = smbus_eeprom_properties; |
1b111dc1 MA |
125 | /* Reason: pointer property "data" */ |
126 | dc->cannot_instantiate_with_device_add_yet = true; | |
b5ea9327 AL |
127 | } |
128 | ||
8c43a6f0 | 129 | static const TypeInfo smbus_eeprom_info = { |
39bffca2 AL |
130 | .name = "smbus-eeprom", |
131 | .parent = TYPE_SMBUS_DEVICE, | |
132 | .instance_size = sizeof(SMBusEEPROMDevice), | |
133 | .class_init = smbus_eeprom_class_initfn, | |
1ea96673 PB |
134 | }; |
135 | ||
83f7d43a | 136 | static void smbus_eeprom_register_types(void) |
1ea96673 | 137 | { |
39bffca2 | 138 | type_register_static(&smbus_eeprom_info); |
1ea96673 PB |
139 | } |
140 | ||
83f7d43a | 141 | type_init(smbus_eeprom_register_types) |
a88df0b9 | 142 | |
a5c82852 | 143 | void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom, |
a88df0b9 IY |
144 | const uint8_t *eeprom_spd, int eeprom_spd_size) |
145 | { | |
146 | int i; | |
7267c094 | 147 | uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */ |
a88df0b9 IY |
148 | if (eeprom_spd_size > 0) { |
149 | memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size); | |
150 | } | |
151 | ||
152 | for (i = 0; i < nb_eeprom; i++) { | |
153 | DeviceState *eeprom; | |
154 | eeprom = qdev_create((BusState *)smbus, "smbus-eeprom"); | |
155 | qdev_prop_set_uint8(eeprom, "address", 0x50 + i); | |
156 | qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256)); | |
157 | qdev_init_nofail(eeprom); | |
158 | } | |
159 | } |