smbus_eeprom.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 = (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. dc->reset = 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_info = {
  133. .name = TYPE_SMBUS_EEPROM,
  134. .parent = TYPE_SMBUS_DEVICE,
  135. .instance_size = sizeof(SMBusEEPROMDevice),
  136. .class_init = smbus_eeprom_class_initfn,
  137. };
  138. static void smbus_eeprom_register_types(void)
  139. {
  140. type_register_static(&smbus_eeprom_info);
  141. }
  142. type_init(smbus_eeprom_register_types)
  143. void smbus_eeprom_init_one(I2CBus *smbus, uint8_t address, uint8_t *eeprom_buf)
  144. {
  145. DeviceState *dev;
  146. dev = qdev_new(TYPE_SMBUS_EEPROM);
  147. qdev_prop_set_uint8(dev, "address", address);
  148. /* FIXME: use an array of byte or block backend property? */
  149. SMBUS_EEPROM(dev)->init_data = eeprom_buf;
  150. qdev_realize_and_unref(dev, (BusState *)smbus, &error_fatal);
  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. {
  170. uint8_t *spd;
  171. uint8_t nbanks;
  172. uint16_t density;
  173. uint32_t size;
  174. int min_log2, max_log2, sz_log2;
  175. int i;
  176. switch (type) {
  177. case SDR:
  178. min_log2 = 2;
  179. max_log2 = 9;
  180. break;
  181. case DDR:
  182. min_log2 = 5;
  183. max_log2 = 12;
  184. break;
  185. case DDR2:
  186. min_log2 = 7;
  187. max_log2 = 14;
  188. break;
  189. default:
  190. g_assert_not_reached();
  191. }
  192. size = ram_size >> 20; /* work in terms of megabytes */
  193. sz_log2 = 31 - clz32(size);
  194. size = 1U << sz_log2;
  195. assert(ram_size == size * MiB);
  196. assert(sz_log2 >= min_log2);
  197. nbanks = 1;
  198. while (sz_log2 > max_log2 && nbanks < 8) {
  199. sz_log2--;
  200. nbanks *= 2;
  201. }
  202. assert(size == (1ULL << sz_log2) * nbanks);
  203. /* split to 2 banks if possible to avoid a bug in MIPS Malta firmware */
  204. if (nbanks == 1 && sz_log2 > min_log2) {
  205. sz_log2--;
  206. nbanks++;
  207. }
  208. density = 1ULL << (sz_log2 - 2);
  209. switch (type) {
  210. case DDR2:
  211. density = (density & 0xe0) | (density >> 8 & 0x1f);
  212. break;
  213. case DDR:
  214. density = (density & 0xf8) | (density >> 8 & 0x07);
  215. break;
  216. case SDR:
  217. default:
  218. density &= 0xff;
  219. break;
  220. }
  221. spd = g_malloc0(256);
  222. spd[0] = 128; /* data bytes in EEPROM */
  223. spd[1] = 8; /* log2 size of EEPROM */
  224. spd[2] = type;
  225. spd[3] = 13; /* row address bits */
  226. spd[4] = 10; /* column address bits */
  227. spd[5] = (type == DDR2 ? nbanks - 1 : nbanks);
  228. spd[6] = 64; /* module data width */
  229. /* reserved / data width high */
  230. spd[8] = 4; /* interface voltage level */
  231. spd[9] = 0x25; /* highest CAS latency */
  232. spd[10] = 1; /* access time */
  233. /* DIMM configuration 0 = non-ECC */
  234. spd[12] = 0x82; /* refresh requirements */
  235. spd[13] = 8; /* primary SDRAM width */
  236. /* ECC SDRAM width */
  237. spd[15] = (type == DDR2 ? 0 : 1); /* reserved / delay for random col rd */
  238. spd[16] = 12; /* burst lengths supported */
  239. spd[17] = 4; /* banks per SDRAM device */
  240. spd[18] = 12; /* ~CAS latencies supported */
  241. spd[19] = (type == DDR2 ? 0 : 1); /* reserved / ~CS latencies supported */
  242. spd[20] = 2; /* DIMM type / ~WE latencies */
  243. spd[21] = (type < DDR2 ? 0x20 : 0); /* module features */
  244. /* memory chip features */
  245. spd[23] = 0x12; /* clock cycle time @ medium CAS latency */
  246. /* data access time */
  247. /* clock cycle time @ short CAS latency */
  248. /* data access time */
  249. spd[27] = 20; /* min. row precharge time */
  250. spd[28] = 15; /* min. row active row delay */
  251. spd[29] = 20; /* min. ~RAS to ~CAS delay */
  252. spd[30] = 45; /* min. active to precharge time */
  253. spd[31] = density;
  254. spd[32] = 20; /* addr/cmd setup time */
  255. spd[33] = 8; /* addr/cmd hold time */
  256. spd[34] = 20; /* data input setup time */
  257. spd[35] = 8; /* data input hold time */
  258. /* checksum */
  259. for (i = 0; i < 63; i++) {
  260. spd[63] += spd[i];
  261. }
  262. return spd;
  263. }