smbus_eeprom.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 dev;
  30. uint8_t *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 val = eeprom->data[eeprom->offset++];
  52. #ifdef DEBUG
  53. printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
  54. dev->i2c.address, val);
  55. #endif
  56. return val;
  57. }
  58. static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
  59. {
  60. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  61. int n;
  62. #ifdef DEBUG
  63. printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
  64. dev->i2c.address, cmd, buf[0]);
  65. #endif
  66. /* An page write operation is not a valid SMBus command.
  67. It is a block write without a length byte. Fortunately we
  68. get the full block anyway. */
  69. /* TODO: Should this set the current location? */
  70. if (cmd + len > 256)
  71. n = 256 - cmd;
  72. else
  73. n = len;
  74. memcpy(eeprom->data + cmd, buf, n);
  75. len -= n;
  76. if (len)
  77. memcpy(eeprom->data, buf + n, len);
  78. }
  79. static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
  80. {
  81. SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
  82. /* If this is the first byte then set the current position. */
  83. if (n == 0)
  84. eeprom->offset = cmd;
  85. /* As with writes, we implement block reads without the
  86. SMBus length byte. */
  87. return eeprom_receive_byte(dev);
  88. }
  89. void smbus_eeprom_device_init(i2c_bus *bus, uint8_t addr, uint8_t *buf)
  90. {
  91. SMBusEEPROMDevice *eeprom;
  92. eeprom = (SMBusEEPROMDevice *)smbus_device_init(bus, addr,
  93. sizeof(SMBusEEPROMDevice));
  94. eeprom->dev.quick_cmd = eeprom_quick_cmd;
  95. eeprom->dev.send_byte = eeprom_send_byte;
  96. eeprom->dev.receive_byte = eeprom_receive_byte;
  97. eeprom->dev.write_data = eeprom_write_data;
  98. eeprom->dev.read_data = eeprom_read_data;
  99. eeprom->data = buf;
  100. eeprom->offset = 0;
  101. }