dimm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright 2021-2022 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <common/debug.h>
  14. #include <ddr.h>
  15. #include <dimm.h>
  16. #include <i2c.h>
  17. #include <lib/utils.h>
  18. int read_spd(unsigned char chip, void *buf, int len)
  19. {
  20. unsigned char dummy = 0U;
  21. int ret;
  22. if (len < 256) {
  23. ERROR("Invalid SPD length\n");
  24. return -EINVAL;
  25. }
  26. i2c_write(SPD_SPA0_ADDRESS, 0, 1, &dummy, 1);
  27. ret = i2c_read(chip, 0, 1, buf, 256);
  28. if (ret == 0) {
  29. i2c_write(SPD_SPA1_ADDRESS, 0, 1, &dummy, 1);
  30. ret = i2c_read(chip, 0, 1, buf + 256, min(256, len - 256));
  31. }
  32. if (ret != 0) {
  33. zeromem(buf, len);
  34. }
  35. return ret;
  36. }
  37. int crc16(unsigned char *ptr, int count)
  38. {
  39. int i;
  40. int crc = 0;
  41. while (--count >= 0) {
  42. crc = crc ^ (int)*ptr++ << 8;
  43. for (i = 0; i < 8; ++i) {
  44. if ((crc & 0x8000) != 0) {
  45. crc = crc << 1 ^ 0x1021;
  46. } else {
  47. crc = crc << 1;
  48. }
  49. }
  50. }
  51. return crc & 0xffff;
  52. }
  53. static int ddr4_spd_check(const struct ddr4_spd *spd)
  54. {
  55. void *p = (void *)spd;
  56. int csum16;
  57. int len;
  58. char crc_lsb; /* byte 126 */
  59. char crc_msb; /* byte 127 */
  60. len = 126;
  61. csum16 = crc16(p, len);
  62. crc_lsb = (char) (csum16 & 0xff);
  63. crc_msb = (char) (csum16 >> 8);
  64. if (spd->crc[0] != crc_lsb || spd->crc[1] != crc_msb) {
  65. ERROR("SPD CRC = 0x%x%x, computed CRC = 0x%x%x\n",
  66. spd->crc[1], spd->crc[0], crc_msb, crc_lsb);
  67. return -EINVAL;
  68. }
  69. p = (void *)spd + 128;
  70. len = 126;
  71. csum16 = crc16(p, len);
  72. crc_lsb = (char) (csum16 & 0xff);
  73. crc_msb = (char) (csum16 >> 8);
  74. if (spd->mod_section.uc[126] != crc_lsb ||
  75. spd->mod_section.uc[127] != crc_msb) {
  76. ERROR("SPD CRC = 0x%x%x, computed CRC = 0x%x%x\n",
  77. spd->mod_section.uc[127], spd->mod_section.uc[126],
  78. crc_msb, crc_lsb);
  79. return -EINVAL;
  80. }
  81. return 0;
  82. }
  83. static unsigned long long
  84. compute_ranksize(const struct ddr4_spd *spd)
  85. {
  86. unsigned long long bsize;
  87. int nbit_sdram_cap_bsize = 0;
  88. int nbit_primary_bus_width = 0;
  89. int nbit_sdram_width = 0;
  90. int die_count = 0;
  91. bool package_3ds;
  92. if ((spd->density_banks & 0xf) <= 7) {
  93. nbit_sdram_cap_bsize = (spd->density_banks & 0xf) + 28;
  94. }
  95. if ((spd->bus_width & 0x7) < 4) {
  96. nbit_primary_bus_width = (spd->bus_width & 0x7) + 3;
  97. }
  98. if ((spd->organization & 0x7) < 4) {
  99. nbit_sdram_width = (spd->organization & 0x7) + 2;
  100. }
  101. package_3ds = (spd->package_type & 0x3) == 0x2;
  102. if (package_3ds) {
  103. die_count = (spd->package_type >> 4) & 0x7;
  104. }
  105. bsize = 1ULL << (nbit_sdram_cap_bsize - 3 +
  106. nbit_primary_bus_width - nbit_sdram_width +
  107. die_count);
  108. return bsize;
  109. }
  110. int cal_dimm_params(const struct ddr4_spd *spd, struct dimm_params *pdimm)
  111. {
  112. int ret;
  113. int i;
  114. static const unsigned char udimm_rc_e_dq[18] = {
  115. 0x0c, 0x2c, 0x15, 0x35, 0x15, 0x35, 0x0b, 0x2c, 0x15,
  116. 0x35, 0x0b, 0x35, 0x0b, 0x2c, 0x0b, 0x35, 0x15, 0x36
  117. };
  118. int spd_error = 0;
  119. unsigned char *ptr;
  120. unsigned char val;
  121. if (spd->mem_type != SPD_MEMTYPE_DDR4) {
  122. ERROR("Not a DDR4 DIMM.\n");
  123. return -EINVAL;
  124. }
  125. ret = ddr4_spd_check(spd);
  126. if (ret != 0) {
  127. ERROR("DIMM SPD checksum mismatch\n");
  128. return -EINVAL;
  129. }
  130. /*
  131. * The part name in ASCII in the SPD EEPROM is not null terminated.
  132. * Guarantee null termination here by presetting all bytes to 0
  133. * and copying the part name in ASCII from the SPD onto it
  134. */
  135. if ((spd->info_size_crc & 0xF) > 2) {
  136. memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
  137. }
  138. /* DIMM organization parameters */
  139. pdimm->n_ranks = ((spd->organization >> 3) & 0x7) + 1;
  140. debug("n_ranks %d\n", pdimm->n_ranks);
  141. pdimm->rank_density = compute_ranksize(spd);
  142. if (pdimm->rank_density == 0) {
  143. return -EINVAL;
  144. }
  145. debug("rank_density 0x%llx\n", pdimm->rank_density);
  146. pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
  147. debug("capacity 0x%llx\n", pdimm->capacity);
  148. pdimm->die_density = spd->density_banks & 0xf;
  149. debug("die density 0x%x\n", pdimm->die_density);
  150. pdimm->primary_sdram_width = 1 << (3 + (spd->bus_width & 0x7));
  151. debug("primary_sdram_width %d\n", pdimm->primary_sdram_width);
  152. if (((spd->bus_width >> 3) & 0x3) != 0) {
  153. pdimm->ec_sdram_width = 8;
  154. } else {
  155. pdimm->ec_sdram_width = 0;
  156. }
  157. debug("ec_sdram_width %d\n", pdimm->ec_sdram_width);
  158. pdimm->device_width = 1 << ((spd->organization & 0x7) + 2);
  159. debug("device_width %d\n", pdimm->device_width);
  160. pdimm->package_3ds = (spd->package_type & 0x3) == 0x2 ?
  161. (spd->package_type >> 4) & 0x7 : 0;
  162. debug("package_3ds %d\n", pdimm->package_3ds);
  163. switch (spd->module_type & DDR4_SPD_MODULETYPE_MASK) {
  164. case DDR4_SPD_RDIMM:
  165. case DDR4_SPD_MINI_RDIMM:
  166. case DDR4_SPD_72B_SO_RDIMM:
  167. pdimm->rdimm = 1;
  168. pdimm->rc = spd->mod_section.registered.ref_raw_card & 0x9f;
  169. if ((spd->mod_section.registered.reg_map & 0x1) != 0) {
  170. pdimm->mirrored_dimm = 1;
  171. }
  172. val = spd->mod_section.registered.ca_stren;
  173. pdimm->rcw[3] = val >> 4;
  174. pdimm->rcw[4] = ((val & 0x3) << 2) | ((val & 0xc) >> 2);
  175. val = spd->mod_section.registered.clk_stren;
  176. pdimm->rcw[5] = ((val & 0x3) << 2) | ((val & 0xc) >> 2);
  177. pdimm->rcw[6] = 0xf;
  178. /* A17 used for 16Gb+, C[2:0] used for 3DS */
  179. pdimm->rcw[8] = pdimm->die_density >= 0x6 ? 0x0 : 0x8 |
  180. (pdimm->package_3ds > 0x3 ? 0x0 :
  181. (pdimm->package_3ds > 0x1 ? 0x1 :
  182. (pdimm->package_3ds > 0 ? 0x2 : 0x3)));
  183. if (pdimm->package_3ds != 0 || pdimm->n_ranks != 4) {
  184. pdimm->rcw[13] = 0x4;
  185. } else {
  186. pdimm->rcw[13] = 0x5;
  187. }
  188. pdimm->rcw[13] |= pdimm->mirrored_dimm ? 0x8 : 0;
  189. break;
  190. case DDR4_SPD_UDIMM:
  191. case DDR4_SPD_SO_DIMM:
  192. case DDR4_SPD_MINI_UDIMM:
  193. case DDR4_SPD_72B_SO_UDIMM:
  194. case DDR4_SPD_16B_SO_DIMM:
  195. case DDR4_SPD_32B_SO_DIMM:
  196. pdimm->rc = spd->mod_section.unbuffered.ref_raw_card & 0x9f;
  197. if ((spd->mod_section.unbuffered.addr_mapping & 0x1) != 0) {
  198. pdimm->mirrored_dimm = 1;
  199. }
  200. if ((spd->mod_section.unbuffered.mod_height & 0xe0) == 0 &&
  201. (spd->mod_section.unbuffered.ref_raw_card == 0x04)) {
  202. /* Fix SPD error found on DIMMs with raw card E0 */
  203. for (i = 0; i < 18; i++) {
  204. if (spd->mapping[i] == udimm_rc_e_dq[i]) {
  205. continue;
  206. }
  207. spd_error = 1;
  208. ptr = (unsigned char *)&spd->mapping[i];
  209. *ptr = udimm_rc_e_dq[i];
  210. }
  211. if (spd_error != 0) {
  212. INFO("SPD DQ mapping error fixed\n");
  213. }
  214. }
  215. break;
  216. default:
  217. ERROR("Unknown module_type 0x%x\n", spd->module_type);
  218. return -EINVAL;
  219. }
  220. debug("rdimm %d\n", pdimm->rdimm);
  221. debug("mirrored_dimm %d\n", pdimm->mirrored_dimm);
  222. debug("rc 0x%x\n", pdimm->rc);
  223. /* SDRAM device parameters */
  224. pdimm->n_row_addr = ((spd->addressing >> 3) & 0x7) + 12;
  225. debug("n_row_addr %d\n", pdimm->n_row_addr);
  226. pdimm->n_col_addr = (spd->addressing & 0x7) + 9;
  227. debug("n_col_addr %d\n", pdimm->n_col_addr);
  228. pdimm->bank_addr_bits = (spd->density_banks >> 4) & 0x3;
  229. debug("bank_addr_bits %d\n", pdimm->bank_addr_bits);
  230. pdimm->bank_group_bits = (spd->density_banks >> 6) & 0x3;
  231. debug("bank_group_bits %d\n", pdimm->bank_group_bits);
  232. if (pdimm->ec_sdram_width != 0) {
  233. pdimm->edc_config = 0x02;
  234. } else {
  235. pdimm->edc_config = 0x00;
  236. }
  237. debug("edc_config %d\n", pdimm->edc_config);
  238. /* DDR4 spec has BL8 -bit3, BC4 -bit2 */
  239. pdimm->burst_lengths_bitmask = 0x0c;
  240. debug("burst_lengths_bitmask 0x%x\n", pdimm->burst_lengths_bitmask);
  241. /* MTB - medium timebase
  242. * The MTB in the SPD spec is 125ps,
  243. *
  244. * FTB - fine timebase
  245. * use 1/10th of ps as our unit to avoid floating point
  246. * eg, 10 for 1ps, 25 for 2.5ps, 50 for 5ps
  247. */
  248. if ((spd->timebases & 0xf) == 0x0) {
  249. pdimm->mtb_ps = 125;
  250. pdimm->ftb_10th_ps = 10;
  251. } else {
  252. ERROR("Unknown Timebases\n");
  253. return -EINVAL;
  254. }
  255. /* sdram minimum cycle time */
  256. pdimm->tckmin_x_ps = spd_to_ps(spd->tck_min, spd->fine_tck_min);
  257. debug("tckmin_x_ps %d\n", pdimm->tckmin_x_ps);
  258. /* sdram max cycle time */
  259. pdimm->tckmax_ps = spd_to_ps(spd->tck_max, spd->fine_tck_max);
  260. debug("tckmax_ps %d\n", pdimm->tckmax_ps);
  261. /*
  262. * CAS latency supported
  263. * bit0 - CL7
  264. * bit4 - CL11
  265. * bit8 - CL15
  266. * bit12- CL19
  267. * bit16- CL23
  268. */
  269. pdimm->caslat_x = (spd->caslat_b1 << 7) |
  270. (spd->caslat_b2 << 15) |
  271. (spd->caslat_b3 << 23);
  272. debug("caslat_x 0x%x\n", pdimm->caslat_x);
  273. if (spd->caslat_b4 != 0) {
  274. WARN("Unhandled caslat_b4 value\n");
  275. }
  276. /*
  277. * min CAS latency time
  278. */
  279. pdimm->taa_ps = spd_to_ps(spd->taa_min, spd->fine_taa_min);
  280. debug("taa_ps %d\n", pdimm->taa_ps);
  281. /*
  282. * min RAS to CAS delay time
  283. */
  284. pdimm->trcd_ps = spd_to_ps(spd->trcd_min, spd->fine_trcd_min);
  285. debug("trcd_ps %d\n", pdimm->trcd_ps);
  286. /*
  287. * Min Row Precharge Delay Time
  288. */
  289. pdimm->trp_ps = spd_to_ps(spd->trp_min, spd->fine_trp_min);
  290. debug("trp_ps %d\n", pdimm->trp_ps);
  291. /* min active to precharge delay time */
  292. pdimm->tras_ps = (((spd->tras_trc_ext & 0xf) << 8) +
  293. spd->tras_min_lsb) * pdimm->mtb_ps;
  294. debug("tras_ps %d\n", pdimm->tras_ps);
  295. /* min active to actice/refresh delay time */
  296. pdimm->trc_ps = spd_to_ps((((spd->tras_trc_ext & 0xf0) << 4) +
  297. spd->trc_min_lsb), spd->fine_trc_min);
  298. debug("trc_ps %d\n", pdimm->trc_ps);
  299. /* Min Refresh Recovery Delay Time */
  300. pdimm->trfc1_ps = ((spd->trfc1_min_msb << 8) | (spd->trfc1_min_lsb)) *
  301. pdimm->mtb_ps;
  302. debug("trfc1_ps %d\n", pdimm->trfc1_ps);
  303. pdimm->trfc2_ps = ((spd->trfc2_min_msb << 8) | (spd->trfc2_min_lsb)) *
  304. pdimm->mtb_ps;
  305. debug("trfc2_ps %d\n", pdimm->trfc2_ps);
  306. pdimm->trfc4_ps = ((spd->trfc4_min_msb << 8) | (spd->trfc4_min_lsb)) *
  307. pdimm->mtb_ps;
  308. debug("trfc4_ps %d\n", pdimm->trfc4_ps);
  309. /* min four active window delay time */
  310. pdimm->tfaw_ps = (((spd->tfaw_msb & 0xf) << 8) | spd->tfaw_min) *
  311. pdimm->mtb_ps;
  312. debug("tfaw_ps %d\n", pdimm->tfaw_ps);
  313. /* min row active to row active delay time, different bank group */
  314. pdimm->trrds_ps = spd_to_ps(spd->trrds_min, spd->fine_trrds_min);
  315. debug("trrds_ps %d\n", pdimm->trrds_ps);
  316. /* min row active to row active delay time, same bank group */
  317. pdimm->trrdl_ps = spd_to_ps(spd->trrdl_min, spd->fine_trrdl_min);
  318. debug("trrdl_ps %d\n", pdimm->trrdl_ps);
  319. /* min CAS to CAS Delay Time (tCCD_Lmin), same bank group */
  320. pdimm->tccdl_ps = spd_to_ps(spd->tccdl_min, spd->fine_tccdl_min);
  321. debug("tccdl_ps %d\n", pdimm->tccdl_ps);
  322. if (pdimm->package_3ds != 0) {
  323. if (pdimm->die_density > 5) {
  324. debug("Unsupported logical rank density 0x%x\n",
  325. pdimm->die_density);
  326. return -EINVAL;
  327. }
  328. pdimm->trfc_slr_ps = (pdimm->die_density <= 4) ?
  329. 260000 : 350000;
  330. }
  331. debug("trfc_slr_ps %d\n", pdimm->trfc_slr_ps);
  332. /* 15ns for all speed bins */
  333. pdimm->twr_ps = 15000;
  334. debug("twr_ps %d\n", pdimm->twr_ps);
  335. /*
  336. * Average periodic refresh interval
  337. * tREFI = 7.8 us at normal temperature range
  338. */
  339. pdimm->refresh_rate_ps = 7800000;
  340. debug("refresh_rate_ps %d\n", pdimm->refresh_rate_ps);
  341. for (i = 0; i < 18; i++) {
  342. pdimm->dq_mapping[i] = spd->mapping[i];
  343. debug("dq_mapping 0x%x\n", pdimm->dq_mapping[i]);
  344. }
  345. pdimm->dq_mapping_ors = ((spd->mapping[0] >> 6) & 0x3) == 0 ? 1 : 0;
  346. debug("dq_mapping_ors %d\n", pdimm->dq_mapping_ors);
  347. return 0;
  348. }