2
0

smbus_eeprom.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. //#define DEBUG
  34. #define TYPE_SMBUS_EEPROM "smbus-eeprom"
  35. #define SMBUS_EEPROM(obj) \
  36. OBJECT_CHECK(SMBusEEPROMDevice, (obj), TYPE_SMBUS_EEPROM)
  37. #define SMBUS_EEPROM_SIZE 256
  38. typedef struct SMBusEEPROMDevice {
  39. SMBusDevice smbusdev;
  40. uint8_t data[SMBUS_EEPROM_SIZE];
  41. void *init_data;
  42. uint8_t offset;
  43. bool accessed;
  44. } SMBusEEPROMDevice;
  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 = (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. smbus_eeprom_reset(dev);
  115. }
  116. static Property smbus_eeprom_properties[] = {
  117. DEFINE_PROP_PTR("data", SMBusEEPROMDevice, init_data),
  118. DEFINE_PROP_END_OF_LIST(),
  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. dc->reset = smbus_eeprom_reset;
  126. sc->receive_byte = eeprom_receive_byte;
  127. sc->write_data = eeprom_write_data;
  128. dc->props = smbus_eeprom_properties;
  129. dc->vmsd = &vmstate_smbus_eeprom;
  130. /* Reason: pointer property "data" */
  131. dc->user_creatable = false;
  132. }
  133. static const TypeInfo smbus_eeprom_info = {
  134. .name = TYPE_SMBUS_EEPROM,
  135. .parent = TYPE_SMBUS_DEVICE,
  136. .instance_size = sizeof(SMBusEEPROMDevice),
  137. .class_init = smbus_eeprom_class_initfn,
  138. };
  139. static void smbus_eeprom_register_types(void)
  140. {
  141. type_register_static(&smbus_eeprom_info);
  142. }
  143. type_init(smbus_eeprom_register_types)
  144. void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
  145. {
  146. DeviceState *dev;
  147. dev = qdev_create((BusState *) smbus, TYPE_SMBUS_EEPROM);
  148. qdev_prop_set_uint8(dev, "address", address);
  149. qdev_prop_set_ptr(dev, "data", eeprom_buf);
  150. qdev_init_nofail(dev);
  151. }
  152. void smbus_eeprom_init(I2CBus *smbus, int nb_eeprom,
  153. const uint8_t *eeprom_spd, int eeprom_spd_size)
  154. {
  155. int i;
  156. /* XXX: make this persistent */
  157. assert(nb_eeprom <= 8);
  158. uint8_t *eeprom_buf = g_malloc0(8 * SMBUS_EEPROM_SIZE);
  159. if (eeprom_spd_size > 0) {
  160. memcpy(eeprom_buf, eeprom_spd, eeprom_spd_size);
  161. }
  162. for (i = 0; i < nb_eeprom; i++) {
  163. smbus_eeprom_init_one(smbus, 0x50 + i,
  164. eeprom_buf + (i * SMBUS_EEPROM_SIZE));
  165. }
  166. }
  167. /* Generate SDRAM SPD EEPROM data describing a module of type and size */
  168. uint8_t *spd_data_generate(enum sdram_type type, ram_addr_t ram_size,
  169. Error **errp)
  170. {
  171. uint8_t *spd;
  172. uint8_t nbanks;
  173. uint16_t density;
  174. uint32_t size;
  175. int min_log2, max_log2, sz_log2;
  176. int i;
  177. switch (type) {
  178. case SDR:
  179. min_log2 = 2;
  180. max_log2 = 9;
  181. break;
  182. case DDR:
  183. min_log2 = 5;
  184. max_log2 = 12;
  185. break;
  186. case DDR2:
  187. min_log2 = 7;
  188. max_log2 = 14;
  189. break;
  190. default:
  191. g_assert_not_reached();
  192. }
  193. size = ram_size >> 20; /* work in terms of megabytes */
  194. if (size < 4) {
  195. error_setg(errp, "SDRAM size is too small");
  196. return NULL;
  197. }
  198. sz_log2 = 31 - clz32(size);
  199. size = 1U << sz_log2;
  200. if (ram_size > size * MiB) {
  201. error_setg(errp, "SDRAM size 0x"RAM_ADDR_FMT" is not a power of 2, "
  202. "truncating to %u MB", ram_size, size);
  203. }
  204. if (sz_log2 < min_log2) {
  205. error_setg(errp,
  206. "Memory size is too small for SDRAM type, adjusting type");
  207. if (size >= 32) {
  208. type = DDR;
  209. min_log2 = 5;
  210. max_log2 = 12;
  211. } else {
  212. type = SDR;
  213. min_log2 = 2;
  214. max_log2 = 9;
  215. }
  216. }
  217. nbanks = 1;
  218. while (sz_log2 > max_log2 && nbanks < 8) {
  219. sz_log2--;
  220. nbanks++;
  221. }
  222. if (size > (1ULL << sz_log2) * nbanks) {
  223. error_setg(errp, "Memory size is too big for SDRAM, truncating");
  224. }
  225. /* split to 2 banks if possible to avoid a bug in MIPS Malta firmware */
  226. if (nbanks == 1 && sz_log2 > min_log2) {
  227. sz_log2--;
  228. nbanks++;
  229. }
  230. density = 1ULL << (sz_log2 - 2);
  231. switch (type) {
  232. case DDR2:
  233. density = (density & 0xe0) | (density >> 8 & 0x1f);
  234. break;
  235. case DDR:
  236. density = (density & 0xf8) | (density >> 8 & 0x07);
  237. break;
  238. case SDR:
  239. default:
  240. density &= 0xff;
  241. break;
  242. }
  243. spd = g_malloc0(256);
  244. spd[0] = 128; /* data bytes in EEPROM */
  245. spd[1] = 8; /* log2 size of EEPROM */
  246. spd[2] = type;
  247. spd[3] = 13; /* row address bits */
  248. spd[4] = 10; /* column address bits */
  249. spd[5] = (type == DDR2 ? nbanks - 1 : nbanks);
  250. spd[6] = 64; /* module data width */
  251. /* reserved / data width high */
  252. spd[8] = 4; /* interface voltage level */
  253. spd[9] = 0x25; /* highest CAS latency */
  254. spd[10] = 1; /* access time */
  255. /* DIMM configuration 0 = non-ECC */
  256. spd[12] = 0x82; /* refresh requirements */
  257. spd[13] = 8; /* primary SDRAM width */
  258. /* ECC SDRAM width */
  259. spd[15] = (type == DDR2 ? 0 : 1); /* reserved / delay for random col rd */
  260. spd[16] = 12; /* burst lengths supported */
  261. spd[17] = 4; /* banks per SDRAM device */
  262. spd[18] = 12; /* ~CAS latencies supported */
  263. spd[19] = (type == DDR2 ? 0 : 1); /* reserved / ~CS latencies supported */
  264. spd[20] = 2; /* DIMM type / ~WE latencies */
  265. /* module features */
  266. /* memory chip features */
  267. spd[23] = 0x12; /* clock cycle time @ medium CAS latency */
  268. /* data access time */
  269. /* clock cycle time @ short CAS latency */
  270. /* data access time */
  271. spd[27] = 20; /* min. row precharge time */
  272. spd[28] = 15; /* min. row active row delay */
  273. spd[29] = 20; /* min. ~RAS to ~CAS delay */
  274. spd[30] = 45; /* min. active to precharge time */
  275. spd[31] = density;
  276. spd[32] = 20; /* addr/cmd setup time */
  277. spd[33] = 8; /* addr/cmd hold time */
  278. spd[34] = 20; /* data input setup time */
  279. spd[35] = 8; /* data input hold time */
  280. /* checksum */
  281. for (i = 0; i < 63; i++) {
  282. spd[63] += spd[i];
  283. }
  284. return spd;
  285. }