2
0

smbus_eeprom.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "qemu/units.h"
  26. #include "qapi/error.h"
  27. #include "hw/boards.h"
  28. #include "hw/i2c/i2c.h"
  29. #include "hw/i2c/smbus_slave.h"
  30. #include "hw/qdev-properties.h"
  31. #include "migration/vmstate.h"
  32. #include "hw/i2c/smbus_eeprom.h"
  33. #include "qom/object.h"
  34. //#define DEBUG
  35. #define TYPE_SMBUS_EEPROM "smbus-eeprom"
  36. OBJECT_DECLARE_SIMPLE_TYPE(SMBusEEPROMDevice, SMBUS_EEPROM)
  37. #define SMBUS_EEPROM_SIZE 256
  38. struct SMBusEEPROMDevice {
  39. SMBusDevice smbusdev;
  40. uint8_t data[SMBUS_EEPROM_SIZE];
  41. uint8_t *init_data;
  42. uint8_t offset;
  43. bool accessed;
  44. };
  45. static uint8_t eeprom_receive_byte(SMBusDevice *dev)
  46. {
  47. SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
  48. uint8_t *data = eeprom->data;
  49. uint8_t val = data[eeprom->offset++];
  50. eeprom->accessed = true;
  51. #ifdef DEBUG
  52. printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
  53. dev->i2c.address, val);
  54. #endif
  55. return val;
  56. }
  57. static int eeprom_write_data(SMBusDevice *dev, uint8_t *buf, uint8_t len)
  58. {
  59. SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
  60. uint8_t *data = eeprom->data;
  61. eeprom->accessed = true;
  62. #ifdef DEBUG
  63. printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
  64. dev->i2c.address, buf[0], buf[1]);
  65. #endif
  66. /* len is guaranteed to be > 0 */
  67. eeprom->offset = buf[0];
  68. buf++;
  69. len--;
  70. for (; len > 0; len--) {
  71. data[eeprom->offset] = *buf++;
  72. eeprom->offset = (eeprom->offset + 1) % SMBUS_EEPROM_SIZE;
  73. }
  74. return 0;
  75. }
  76. static bool smbus_eeprom_vmstate_needed(void *opaque)
  77. {
  78. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  79. SMBusEEPROMDevice *eeprom = opaque;
  80. return (eeprom->accessed || smbus_vmstate_needed(&eeprom->smbusdev)) &&
  81. !mc->smbus_no_migration_support;
  82. }
  83. static const VMStateDescription vmstate_smbus_eeprom = {
  84. .name = "smbus-eeprom",
  85. .version_id = 1,
  86. .minimum_version_id = 1,
  87. .needed = smbus_eeprom_vmstate_needed,
  88. .fields = (const VMStateField[]) {
  89. VMSTATE_SMBUS_DEVICE(smbusdev, SMBusEEPROMDevice),
  90. VMSTATE_UINT8_ARRAY(data, SMBusEEPROMDevice, SMBUS_EEPROM_SIZE),
  91. VMSTATE_UINT8(offset, SMBusEEPROMDevice),
  92. VMSTATE_BOOL(accessed, SMBusEEPROMDevice),
  93. VMSTATE_END_OF_LIST()
  94. }
  95. };
  96. /*
  97. * Reset the EEPROM contents to the initial state on a reset. This
  98. * isn't really how an EEPROM works, of course, but the general
  99. * principle of QEMU is to restore function on reset to what it would
  100. * be if QEMU was stopped and started.
  101. *
  102. * The proper thing to do would be to have a backing blockdev to hold
  103. * the contents and restore that on startup, and not do this on reset.
  104. * But until that time, act as if we had been stopped and restarted.
  105. */
  106. static void smbus_eeprom_reset(DeviceState *dev)
  107. {
  108. SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
  109. memcpy(eeprom->data, eeprom->init_data, SMBUS_EEPROM_SIZE);
  110. eeprom->offset = 0;
  111. }
  112. static void smbus_eeprom_realize(DeviceState *dev, Error **errp)
  113. {
  114. SMBusEEPROMDevice *eeprom = SMBUS_EEPROM(dev);
  115. smbus_eeprom_reset(dev);
  116. if (eeprom->init_data == NULL) {
  117. error_setg(errp, "init_data cannot be NULL");
  118. }
  119. }
  120. static void smbus_eeprom_class_initfn(ObjectClass *klass, void *data)
  121. {
  122. DeviceClass *dc = DEVICE_CLASS(klass);
  123. SMBusDeviceClass *sc = SMBUS_DEVICE_CLASS(klass);
  124. dc->realize = smbus_eeprom_realize;
  125. device_class_set_legacy_reset(dc, smbus_eeprom_reset);
  126. sc->receive_byte = eeprom_receive_byte;
  127. sc->write_data = eeprom_write_data;
  128. dc->vmsd = &vmstate_smbus_eeprom;
  129. /* Reason: init_data */
  130. dc->user_creatable = false;
  131. }
  132. static const TypeInfo smbus_eeprom_types[] = {
  133. {
  134. .name = TYPE_SMBUS_EEPROM,
  135. .parent = TYPE_SMBUS_DEVICE,
  136. .instance_size = sizeof(SMBusEEPROMDevice),
  137. .class_init = smbus_eeprom_class_initfn,
  138. },
  139. };
  140. DEFINE_TYPES(smbus_eeprom_types)
  141. void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
  142. {
  143. DeviceState *dev;
  144. dev = qdev_new(TYPE_SMBUS_EEPROM);
  145. qdev_prop_set_uint8(dev, "address", address);
  146. /* FIXME: use an array of byte or block backend property? */
  147. SMBUS_EEPROM(dev)->init_data = eeprom_buf;
  148. qdev_realize_and_unref(dev, (BusState *)smbus, &error_fatal);
  149. }
  150. void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
  151. const uint8_t *eeprom_spd, int eeprom_spd_size)
  152. {
  153. int i;
  154. /* XXX: make this persistent */
  155. assert(nb_eeprom <= 8);
  156. uint8_t *eeprom_buf = g_malloc0(8 * SMBUS_EEPROM_SIZE);
  157. if (eeprom_spd_size > 0) {
  158. memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
  159. }
  160. for (i = 0; i < nb_eeprom; i++) {
  161. smbus_eeprom_init_one(smbus, 0x50 + i,
  162. eeprom_buf + (i * SMBUS_EEPROM_SIZE));
  163. }
  164. }
  165. /* Generate SDRAM SPD EEPROM data describing a module of type and size */
  166. uint8_t *spd_data_generate(enum sdram_type type, ram_addr_t ram_size)
  167. {
  168. uint8_t *spd;
  169. uint8_t nbanks;
  170. uint16_t density;
  171. uint32_t size;
  172. int min_log2, max_log2, sz_log2;
  173. int i;
  174. switch (type) {
  175. case SDR:
  176. min_log2 = 2;
  177. max_log2 = 9;
  178. break;
  179. case DDR:
  180. min_log2 = 5;
  181. max_log2 = 12;
  182. break;
  183. case DDR2:
  184. min_log2 = 7;
  185. max_log2 = 14;
  186. break;
  187. default:
  188. g_assert_not_reached();
  189. }
  190. size = ram_size >> 20; /* work in terms of megabytes */
  191. sz_log2 = 31 - clz32(size);
  192. size = 1U << sz_log2;
  193. assert(ram_size == size * MiB);
  194. assert(sz_log2 >= min_log2);
  195. nbanks = 1;
  196. while (sz_log2 > max_log2 && nbanks < 8) {
  197. sz_log2--;
  198. nbanks *= 2;
  199. }
  200. assert(size == (1ULL << sz_log2) * nbanks);
  201. /* split to 2 banks if possible to avoid a bug in MIPS Malta firmware */
  202. if (nbanks == 1 && sz_log2 > min_log2) {
  203. sz_log2--;
  204. nbanks++;
  205. }
  206. density = 1ULL << (sz_log2 - 2);
  207. switch (type) {
  208. case DDR2:
  209. density = (density & 0xe0) | (density >> 8 & 0x1f);
  210. break;
  211. case DDR:
  212. density = (density & 0xf8) | (density >> 8 & 0x07);
  213. break;
  214. case SDR:
  215. default:
  216. density &= 0xff;
  217. break;
  218. }
  219. spd = g_malloc0(256);
  220. spd[0] = 128; /* data bytes in EEPROM */
  221. spd[1] = 8; /* log2 size of EEPROM */
  222. spd[2] = type;
  223. spd[3] = 13; /* row address bits */
  224. spd[4] = 10; /* column address bits */
  225. spd[5] = (type == DDR2 ? nbanks - 1 : nbanks);
  226. spd[6] = 64; /* module data width */
  227. /* reserved / data width high */
  228. spd[8] = 4; /* interface voltage level */
  229. spd[9] = 0x25; /* highest CAS latency */
  230. spd[10] = 1; /* access time */
  231. /* DIMM configuration 0 = non-ECC */
  232. spd[12] = 0x82; /* refresh requirements */
  233. spd[13] = 8; /* primary SDRAM width */
  234. /* ECC SDRAM width */
  235. spd[15] = (type == DDR2 ? 0 : 1); /* reserved / delay for random col rd */
  236. spd[16] = 12; /* burst lengths supported */
  237. spd[17] = 4; /* banks per SDRAM device */
  238. spd[18] = 12; /* ~CAS latencies supported */
  239. spd[19] = (type == DDR2 ? 0 : 1); /* reserved / ~CS latencies supported */
  240. spd[20] = 2; /* DIMM type / ~WE latencies */
  241. spd[21] = (type < DDR2 ? 0x20 : 0); /* module features */
  242. /* memory chip features */
  243. spd[23] = 0x12; /* clock cycle time @ medium CAS latency */
  244. /* data access time */
  245. /* clock cycle time @ short CAS latency */
  246. /* data access time */
  247. spd[27] = 20; /* min. row precharge time */
  248. spd[28] = 15; /* min. row active row delay */
  249. spd[29] = 20; /* min. ~RAS to ~CAS delay */
  250. spd[30] = 45; /* min. active to precharge time */
  251. spd[31] = density;
  252. spd[32] = 20; /* addr/cmd setup time */
  253. spd[33] = 8; /* addr/cmd hold time */
  254. spd[34] = 20; /* data input setup time */
  255. spd[35] = 8; /* data input hold time */
  256. /* checksum */
  257. for (i = 0; i < 63; i++) {
  258. spd[63] += spd[i];
  259. }
  260. return spd;
  261. }