smbus_eeprom.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * QEMU SMBus EEPROM device
  3. *
  4. * Copyright (c) 2007 Arastra, Inc.
  5. *
  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. #include "qemu/osdep.h"
  25. #include "hw/hw.h"
  26. #include "hw/i2c/i2c.h"
  27. #include "hw/i2c/smbus.h"
  28. //#define DEBUG
  29. typedef struct SMBusEEPROMDevice {
  30. SMBusDevice smbusdev;
  31. void *data;
  32. uint8_t offset;
  33. } SMBusEEPROMDevice;
  34. static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
  35. {
  36. #ifdef DEBUG
  37. printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
  38. #endif
  39. }
  40. static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
  41. {
  42. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  43. #ifdef DEBUG
  44. printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
  45. dev->i2c.address, val);
  46. #endif
  47. eeprom->offset = val;
  48. }
  49. static uint8_t eeprom_receive_byte(SMBusDevice *dev)
  50. {
  51. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  52. uint8_t *data = eeprom->data;
  53. uint8_t val = data[eeprom->offset++];
  54. #ifdef DEBUG
  55. printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
  56. dev->i2c.address, val);
  57. #endif
  58. return val;
  59. }
  60. static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
  61. {
  62. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  63. int n;
  64. #ifdef DEBUG
  65. printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
  66. dev->i2c.address, cmd, buf[0]);
  67. #endif
  68. /* A page write operation is not a valid SMBus command.
  69. It is a block write without a length byte. Fortunately we
  70. get the full block anyway. */
  71. /* TODO: Should this set the current location? */
  72. if (cmd + len > 256)
  73. n = 256 - cmd;
  74. else
  75. n = len;
  76. memcpy(eeprom->data + cmd, buf, n);
  77. len -= n;
  78. if (len)
  79. memcpy(eeprom->data, buf + n, len);
  80. }
  81. static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
  82. {
  83. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  84. /* If this is the first byte then set the current position. */
  85. if (n == 0)
  86. eeprom->offset = cmd;
  87. /* As with writes, we implement block reads without the
  88. SMBus length byte. */
  89. return eeprom_receive_byte(dev);
  90. }
  91. static void smbus_eeprom_realize(DeviceState *dev, Error **errp)
  92. {
  93. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
  94. eeprom->offset = 0;
  95. }
  96. static Property smbus_eeprom_properties[] = {
  97. DEFINE_PROP_PTR("data", SMBusEEPROMDevice, data),
  98. DEFINE_PROP_END_OF_LIST(),
  99. };
  100. static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data)
  101. {
  102. DeviceClass *dc = DEVICE_CLASS(klass);
  103. SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
  104. dc->realize = smbus_eeprom_realize;
  105. sc->quick_cmd = eeprom_quick_cmd;
  106. sc->send_byte = eeprom_send_byte;
  107. sc->receive_byte = eeprom_receive_byte;
  108. sc->write_data = eeprom_write_data;
  109. sc->read_data = eeprom_read_data;
  110. dc->props = smbus_eeprom_properties;
  111. /* Reason: pointer property "data" */
  112. dc->user_creatable = false;
  113. }
  114. static const TypeInfo smbus_eeprom_info = {
  115. .name = "smbus-eeprom",
  116. .parent = TYPE_SMBUS_DEVICE,
  117. .instance_size = sizeof(SMBusEEPROMDevice),
  118. .class_init = smbus_eeprom_class_initfn,
  119. };
  120. static void smbus_eeprom_register_types(void)
  121. {
  122. type_register_static(&smbus_eeprom_info);
  123. }
  124. type_init(smbus_eeprom_register_types)
  125. void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
  126. {
  127. DeviceState *dev;
  128. dev = qdev_create((BusState *) smbus, "smbus-eeprom");
  129. qdev_prop_set_uint8(dev, "address", address);
  130. qdev_prop_set_ptr(dev, "data", eeprom_buf);
  131. qdev_init_nofail(dev);
  132. }
  133. void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
  134. const uint8_t *eeprom_spd, int eeprom_spd_size)
  135. {
  136. int i;
  137. uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */
  138. if (eeprom_spd_size > 0) {
  139. memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
  140. }
  141. for (i = 0; i < nb_eeprom; i++) {
  142. smbus_eeprom_init_one(smbus, 0x50 + i, eeprom_buf + (i * 256));
  143. }
  144. }