smbus_eeprom.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "hw.h"
  25. #include "i2c.h"
  26. #include "smbus.h"
  27. //#define DEBUG
  28. typedef struct SMBusEEPROMDevice {
  29. SMBusDevice smbusdev;
  30. void *data;
  31. uint8_t offset;
  32. } SMBusEEPROMDevice;
  33. static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
  34. {
  35. #ifdef DEBUG
  36. printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
  37. #endif
  38. }
  39. static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
  40. {
  41. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  42. #ifdef DEBUG
  43. printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
  44. dev->i2c.address, val);
  45. #endif
  46. eeprom->offset = val;
  47. }
  48. static uint8_t eeprom_receive_byte(SMBusDevice *dev)
  49. {
  50. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  51. uint8_t *data = eeprom->data;
  52. uint8_t val = data[eeprom->offset++];
  53. #ifdef DEBUG
  54. printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
  55. dev->i2c.address, val);
  56. #endif
  57. return val;
  58. }
  59. static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
  60. {
  61. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  62. int n;
  63. #ifdef DEBUG
  64. printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
  65. dev->i2c.address, cmd, buf[0]);
  66. #endif
  67. /* An page write operation is not a valid SMBus command.
  68. It is a block write without a length byte. Fortunately we
  69. get the full block anyway. */
  70. /* TODO: Should this set the current location? */
  71. if (cmd + len > 256)
  72. n = 256 - cmd;
  73. else
  74. n = len;
  75. memcpy(eeprom->data + cmd, buf, n);
  76. len -= n;
  77. if (len)
  78. memcpy(eeprom->data, buf + n, len);
  79. }
  80. static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
  81. {
  82. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  83. /* If this is the first byte then set the current position. */
  84. if (n == 0)
  85. eeprom->offset = cmd;
  86. /* As with writes, we implement block reads without the
  87. SMBus length byte. */
  88. return eeprom_receive_byte(dev);
  89. }
  90. static int smbus_eeprom_initfn(SMBusDevice *dev)
  91. {
  92. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
  93. eeprom->offset = 0;
  94. return 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. sc->init = smbus_eeprom_initfn;
  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. }
  112. static TypeInfo smbus_eeprom_info = {
  113. .name = "smbus-eeprom",
  114. .parent = TYPE_SMBUS_DEVICE,
  115. .instance_size = sizeof(SMBusEEPROMDevice),
  116. .class_init = smbus_eeprom_class_initfn,
  117. };
  118. static void smbus_eeprom_register_types(void)
  119. {
  120. type_register_static(&smbus_eeprom_info);
  121. }
  122. type_init(smbus_eeprom_register_types)
  123. void smbus_eeprom_init(i2c_bus *smbus, int nb_eeprom,
  124. const uint8_t *eeprom_spd, int eeprom_spd_size)
  125. {
  126. int i;
  127. uint8_t *eeprom_buf = g_malloc0(8 * 256); /* XXX: make this persistent */
  128. if (eeprom_spd_size > 0) {
  129. memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
  130. }
  131. for (i = 0; i < nb_eeprom; i++) {
  132. DeviceState *eeprom;
  133. eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
  134. qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
  135. qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
  136. qdev_init_nofail(eeprom);
  137. }
  138. }