i2c_eeprom_emul.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Simulate an I2C eeprom
  4. *
  5. * Copyright (c) 2014 Google, Inc
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <i2c.h>
  11. #include <malloc.h>
  12. #include <asm/test.h>
  13. #ifdef DEBUG
  14. #define debug_buffer print_buffer
  15. #else
  16. #define debug_buffer(x, ...)
  17. #endif
  18. struct sandbox_i2c_flash_plat_data {
  19. enum sandbox_i2c_eeprom_test_mode test_mode;
  20. const char *filename;
  21. int offset_len; /* Length of an offset in bytes */
  22. int size; /* Size of data buffer */
  23. };
  24. struct sandbox_i2c_flash {
  25. uint8_t *data;
  26. };
  27. void sandbox_i2c_eeprom_set_test_mode(struct udevice *dev,
  28. enum sandbox_i2c_eeprom_test_mode mode)
  29. {
  30. struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
  31. plat->test_mode = mode;
  32. }
  33. void sandbox_i2c_eeprom_set_offset_len(struct udevice *dev, int offset_len)
  34. {
  35. struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
  36. plat->offset_len = offset_len;
  37. }
  38. static int sandbox_i2c_eeprom_xfer(struct udevice *emul, struct i2c_msg *msg,
  39. int nmsgs)
  40. {
  41. struct sandbox_i2c_flash *priv = dev_get_priv(emul);
  42. uint offset = 0;
  43. debug("\n%s\n", __func__);
  44. debug_buffer(0, priv->data, 1, 16, 0);
  45. for (; nmsgs > 0; nmsgs--, msg++) {
  46. struct sandbox_i2c_flash_plat_data *plat =
  47. dev_get_platdata(emul);
  48. int len;
  49. u8 *ptr;
  50. if (!plat->size)
  51. return -ENODEV;
  52. if (msg->addr + msg->len > plat->size) {
  53. debug("%s: Address %x, len %x is outside range 0..%x\n",
  54. __func__, msg->addr, msg->len, plat->size);
  55. return -EINVAL;
  56. }
  57. len = msg->len;
  58. debug(" %s: msg->len=%d",
  59. msg->flags & I2C_M_RD ? "read" : "write",
  60. msg->len);
  61. if (msg->flags & I2C_M_RD) {
  62. if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
  63. len = 1;
  64. debug(", offset %x, len %x: ", offset, len);
  65. memcpy(msg->buf, priv->data + offset, len);
  66. memset(msg->buf + len, '\xff', msg->len - len);
  67. debug_buffer(0, msg->buf, 1, msg->len, 0);
  68. } else if (len >= plat->offset_len) {
  69. int i;
  70. ptr = msg->buf;
  71. for (i = 0; i < plat->offset_len; i++, len--)
  72. offset = (offset << 8) | *ptr++;
  73. debug(", set offset %x: ", offset);
  74. debug_buffer(0, msg->buf, 1, msg->len, 0);
  75. if (plat->test_mode == SIE_TEST_MODE_SINGLE_BYTE)
  76. len = min(len, 1);
  77. /* For testing, map offsets into our limited buffer */
  78. for (i = 24; i > 0; i -= 8) {
  79. if (offset > (1 << i)) {
  80. offset = (offset >> i) |
  81. (offset & ((1 << i) - 1));
  82. offset += i;
  83. }
  84. }
  85. memcpy(priv->data + offset, ptr, len);
  86. }
  87. }
  88. debug_buffer(0, priv->data, 1, 16, 0);
  89. return 0;
  90. }
  91. struct dm_i2c_ops sandbox_i2c_emul_ops = {
  92. .xfer = sandbox_i2c_eeprom_xfer,
  93. };
  94. static int sandbox_i2c_eeprom_ofdata_to_platdata(struct udevice *dev)
  95. {
  96. struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
  97. plat->size = dev_read_u32_default(dev, "sandbox,size", 32);
  98. plat->filename = dev_read_string(dev, "sandbox,filename");
  99. if (!plat->filename) {
  100. debug("%s: No filename for device '%s'\n", __func__,
  101. dev->name);
  102. return -EINVAL;
  103. }
  104. plat->test_mode = SIE_TEST_MODE_NONE;
  105. plat->offset_len = 1;
  106. return 0;
  107. }
  108. static int sandbox_i2c_eeprom_probe(struct udevice *dev)
  109. {
  110. struct sandbox_i2c_flash_plat_data *plat = dev_get_platdata(dev);
  111. struct sandbox_i2c_flash *priv = dev_get_priv(dev);
  112. priv->data = calloc(1, plat->size);
  113. if (!priv->data)
  114. return -ENOMEM;
  115. return 0;
  116. }
  117. static int sandbox_i2c_eeprom_remove(struct udevice *dev)
  118. {
  119. struct sandbox_i2c_flash *priv = dev_get_priv(dev);
  120. free(priv->data);
  121. return 0;
  122. }
  123. static const struct udevice_id sandbox_i2c_ids[] = {
  124. { .compatible = "sandbox,i2c-eeprom" },
  125. { }
  126. };
  127. U_BOOT_DRIVER(sandbox_i2c_emul) = {
  128. .name = "sandbox_i2c_eeprom_emul",
  129. .id = UCLASS_I2C_EMUL,
  130. .of_match = sandbox_i2c_ids,
  131. .ofdata_to_platdata = sandbox_i2c_eeprom_ofdata_to_platdata,
  132. .probe = sandbox_i2c_eeprom_probe,
  133. .remove = sandbox_i2c_eeprom_remove,
  134. .priv_auto_alloc_size = sizeof(struct sandbox_i2c_flash),
  135. .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_flash_plat_data),
  136. .ops = &sandbox_i2c_emul_ops,
  137. };