xref: /qemu/hw/i2c/smbus_eeprom.c (revision b36dc67b95dedcece8757ec23bf42625a7ccda34)
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 
2583c9f4caSPaolo Bonzini #include "hw/hw.h"
260d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h"
270d09e41aSPaolo Bonzini #include "hw/i2c/smbus.h"
283fffc223Sths 
293fffc223Sths //#define DEBUG
303fffc223Sths 
313fffc223Sths typedef struct SMBusEEPROMDevice {
321ea96673SPaul Brook     SMBusDevice smbusdev;
33bf2782d7SGerd Hoffmann     void *data;
343fffc223Sths     uint8_t offset;
353fffc223Sths } SMBusEEPROMDevice;
363fffc223Sths 
373fffc223Sths static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
383fffc223Sths {
393fffc223Sths #ifdef DEBUG
40ab7d9131Sbalrog     printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
413fffc223Sths #endif
423fffc223Sths }
433fffc223Sths 
443fffc223Sths static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
453fffc223Sths {
463fffc223Sths     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
473fffc223Sths #ifdef DEBUG
48ab7d9131Sbalrog     printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
49ab7d9131Sbalrog            dev->i2c.address, val);
503fffc223Sths #endif
513fffc223Sths     eeprom->offset = val;
523fffc223Sths }
533fffc223Sths 
543fffc223Sths static uint8_t eeprom_receive_byte(SMBusDevice *dev)
553fffc223Sths {
563fffc223Sths     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
57bf2782d7SGerd Hoffmann     uint8_t *data = eeprom->data;
58bf2782d7SGerd Hoffmann     uint8_t val = data[eeprom->offset++];
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 
660ff596d0Spbrook static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
673fffc223Sths {
683fffc223Sths     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
690ff596d0Spbrook     int n;
703fffc223Sths #ifdef DEBUG
71ab7d9131Sbalrog     printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
72ab7d9131Sbalrog            dev->i2c.address, cmd, buf[0]);
733fffc223Sths #endif
74*b36dc67bSStefan Weil     /* A page write operation is not a valid SMBus command.
750ff596d0Spbrook        It is a block write without a length byte.  Fortunately we
760ff596d0Spbrook        get the full block anyway.  */
770ff596d0Spbrook     /* TODO: Should this set the current location?  */
780ff596d0Spbrook     if (cmd + len > 256)
790ff596d0Spbrook         n = 256 - cmd;
800ff596d0Spbrook     else
810ff596d0Spbrook         n = len;
820ff596d0Spbrook     memcpy(eeprom->data + cmd, buf, n);
830ff596d0Spbrook     len -= n;
840ff596d0Spbrook     if (len)
850ff596d0Spbrook         memcpy(eeprom->data, buf + n, len);
863fffc223Sths }
873fffc223Sths 
880ff596d0Spbrook static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
893fffc223Sths {
903fffc223Sths     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
910ff596d0Spbrook     /* If this is the first byte then set the current position.  */
920ff596d0Spbrook     if (n == 0)
930ff596d0Spbrook         eeprom->offset = cmd;
940ff596d0Spbrook     /* As with writes, we implement block reads without the
950ff596d0Spbrook        SMBus length byte.  */
960ff596d0Spbrook     return eeprom_receive_byte(dev);
973fffc223Sths }
983fffc223Sths 
99a88df0b9SIsaku Yamahata static int smbus_eeprom_initfn(SMBusDevice *dev)
1003fffc223Sths {
1011ea96673SPaul Brook     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
1020ff596d0Spbrook 
1033fffc223Sths     eeprom->offset = 0;
10481a322d4SGerd Hoffmann     return 0;
1053fffc223Sths }
1061ea96673SPaul Brook 
10739bffca2SAnthony Liguori static Property smbus_eeprom_properties[] = {
10839bffca2SAnthony Liguori     DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data),
10939bffca2SAnthony Liguori     DEFINE_PROP_END_OF_LIST(),
11039bffca2SAnthony Liguori };
11139bffca2SAnthony Liguori 
112b5ea9327SAnthony Liguori static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data)
113b5ea9327SAnthony Liguori {
11439bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
115b5ea9327SAnthony Liguori     SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
116b5ea9327SAnthony Liguori 
117b5ea9327SAnthony Liguori     sc->init = smbus_eeprom_initfn;
118b5ea9327SAnthony Liguori     sc->quick_cmd = eeprom_quick_cmd;
119b5ea9327SAnthony Liguori     sc->send_byte = eeprom_send_byte;
120b5ea9327SAnthony Liguori     sc->receive_byte = eeprom_receive_byte;
121b5ea9327SAnthony Liguori     sc->write_data = eeprom_write_data;
122b5ea9327SAnthony Liguori     sc->read_data = eeprom_read_data;
12339bffca2SAnthony Liguori     dc->props = smbus_eeprom_properties;
1241b111dc1SMarkus Armbruster     /* Reason: pointer property "data" */
1251b111dc1SMarkus Armbruster     dc->cannot_instantiate_with_device_add_yet = true;
126b5ea9327SAnthony Liguori }
127b5ea9327SAnthony Liguori 
1288c43a6f0SAndreas Färber static const TypeInfo smbus_eeprom_info = {
129b5ea9327SAnthony Liguori     .name          = "smbus-eeprom",
13039bffca2SAnthony Liguori     .parent        = TYPE_SMBUS_DEVICE,
13139bffca2SAnthony Liguori     .instance_size = sizeof(SMBusEEPROMDevice),
132b5ea9327SAnthony Liguori     .class_init    = smbus_eeprom_class_initfn,
1331ea96673SPaul Brook };
1341ea96673SPaul Brook 
13583f7d43aSAndreas Färber static void smbus_eeprom_register_types(void)
1361ea96673SPaul Brook {
13739bffca2SAnthony Liguori     type_register_static(&smbus_eeprom_info);
1381ea96673SPaul Brook }
1391ea96673SPaul Brook 
14083f7d43aSAndreas Färber type_init(smbus_eeprom_register_types)
141a88df0b9SIsaku Yamahata 
142a5c82852SAndreas Färber void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
143a88df0b9SIsaku Yamahata                        const uint8_t *eeprom_spd, int eeprom_spd_size)
144a88df0b9SIsaku Yamahata {
145a88df0b9SIsaku Yamahata     int i;
1467267c094SAnthony Liguori     uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */
147a88df0b9SIsaku Yamahata     if (eeprom_spd_size > 0) {
148a88df0b9SIsaku Yamahata         memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
149a88df0b9SIsaku Yamahata     }
150a88df0b9SIsaku Yamahata 
151a88df0b9SIsaku Yamahata     for (i = 0; i < nb_eeprom; i++) {
152a88df0b9SIsaku Yamahata         DeviceState *eeprom;
153a88df0b9SIsaku Yamahata         eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
154a88df0b9SIsaku Yamahata         qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
155a88df0b9SIsaku Yamahata         qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
156a88df0b9SIsaku Yamahata         qdev_init_nofail(eeprom);
157a88df0b9SIsaku Yamahata     }
158a88df0b9SIsaku Yamahata }
159