ddr4_dimm_params.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2014-2016 Freescale Semiconductor, Inc.
  3. * Copyright 2017-2018 NXP Semiconductor
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * calculate the organization and timing parameter
  8. * from ddr3 spd, please refer to the spec
  9. * JEDEC standard No.21-C 4_01_02_12R23A.pdf
  10. *
  11. *
  12. */
  13. #include <common.h>
  14. #include <fsl_ddr_sdram.h>
  15. #include <fsl_ddr.h>
  16. /*
  17. * Calculate the Density of each Physical Rank.
  18. * Returned size is in bytes.
  19. *
  20. * Total DIMM size =
  21. * sdram capacity(bit) / 8 * primary bus width / sdram width
  22. * * Logical Ranks per DIMM
  23. *
  24. * where: sdram capacity = spd byte4[3:0]
  25. * primary bus width = spd byte13[2:0]
  26. * sdram width = spd byte12[2:0]
  27. * Logical Ranks per DIMM = spd byte12[5:3] for SDP, DDP, QDP
  28. * spd byte12{5:3] * spd byte6[6:4] for 3DS
  29. *
  30. * To simplify each rank size = total DIMM size / Number of Package Ranks
  31. * where Number of Package Ranks = spd byte12[5:3]
  32. *
  33. * SPD byte4 - sdram density and banks
  34. * bit[3:0] size(bit) size(byte)
  35. * 0000 256Mb 32MB
  36. * 0001 512Mb 64MB
  37. * 0010 1Gb 128MB
  38. * 0011 2Gb 256MB
  39. * 0100 4Gb 512MB
  40. * 0101 8Gb 1GB
  41. * 0110 16Gb 2GB
  42. * 0111 32Gb 4GB
  43. *
  44. * SPD byte13 - module memory bus width
  45. * bit[2:0] primary bus width
  46. * 000 8bits
  47. * 001 16bits
  48. * 010 32bits
  49. * 011 64bits
  50. *
  51. * SPD byte12 - module organization
  52. * bit[2:0] sdram device width
  53. * 000 4bits
  54. * 001 8bits
  55. * 010 16bits
  56. * 011 32bits
  57. *
  58. * SPD byte12 - module organization
  59. * bit[5:3] number of package ranks per DIMM
  60. * 000 1
  61. * 001 2
  62. * 010 3
  63. * 011 4
  64. *
  65. * SPD byte6 - SDRAM package type
  66. * bit[6:4] Die count
  67. * 000 1
  68. * 001 2
  69. * 010 3
  70. * 011 4
  71. * 100 5
  72. * 101 6
  73. * 110 7
  74. * 111 8
  75. *
  76. * SPD byte6 - SRAM package type
  77. * bit[1:0] Signal loading
  78. * 00 Not specified
  79. * 01 Multi load stack
  80. * 10 Sigle load stack (3DS)
  81. * 11 Reserved
  82. */
  83. static unsigned long long
  84. compute_ranksize(const struct ddr4_spd_eeprom_s *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. if ((spd->bus_width & 0x7) < 4)
  95. nbit_primary_bus_width = (spd->bus_width & 0x7) + 3;
  96. if ((spd->organization & 0x7) < 4)
  97. nbit_sdram_width = (spd->organization & 0x7) + 2;
  98. package_3ds = (spd->package_type & 0x3) == 0x2;
  99. if ((spd->package_type & 0x80) && !package_3ds) { /* other than 3DS */
  100. printf("Warning: not supported SDRAM package type\n");
  101. return 0;
  102. }
  103. if (package_3ds)
  104. die_count = (spd->package_type >> 4) & 0x7;
  105. bsize = 1ULL << (nbit_sdram_cap_bsize - 3 +
  106. nbit_primary_bus_width - nbit_sdram_width +
  107. die_count);
  108. debug("DDR: DDR rank density = 0x%16llx\n", bsize);
  109. return bsize;
  110. }
  111. #define spd_to_ps(mtb, ftb) \
  112. (mtb * pdimm->mtb_ps + (ftb * pdimm->ftb_10th_ps) / 10)
  113. /*
  114. * ddr_compute_dimm_parameters for DDR4 SPD
  115. *
  116. * Compute DIMM parameters based upon the SPD information in spd.
  117. * Writes the results to the dimm_params_t structure pointed by pdimm.
  118. *
  119. */
  120. unsigned int ddr_compute_dimm_parameters(const unsigned int ctrl_num,
  121. const generic_spd_eeprom_t *spd,
  122. dimm_params_t *pdimm,
  123. unsigned int dimm_number)
  124. {
  125. unsigned int retval;
  126. int i;
  127. const u8 udimm_rc_e_dq[18] = {
  128. 0x0c, 0x2c, 0x15, 0x35, 0x15, 0x35, 0x0b, 0x2c, 0x15,
  129. 0x35, 0x0b, 0x35, 0x0b, 0x2c, 0x0b, 0x35, 0x15, 0x36
  130. };
  131. int spd_error = 0;
  132. u8 *ptr;
  133. if (spd->mem_type) {
  134. if (spd->mem_type != SPD_MEMTYPE_DDR4) {
  135. printf("Ctrl %u DIMM %u: is not a DDR4 SPD.\n",
  136. ctrl_num, dimm_number);
  137. return 1;
  138. }
  139. } else {
  140. memset(pdimm, 0, sizeof(dimm_params_t));
  141. return 1;
  142. }
  143. retval = ddr4_spd_check(spd);
  144. if (retval) {
  145. printf("DIMM %u: failed checksum\n", dimm_number);
  146. return 2;
  147. }
  148. /*
  149. * The part name in ASCII in the SPD EEPROM is not null terminated.
  150. * Guarantee null termination here by presetting all bytes to 0
  151. * and copying the part name in ASCII from the SPD onto it
  152. */
  153. memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
  154. if ((spd->info_size_crc & 0xF) > 2)
  155. memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
  156. /* DIMM organization parameters */
  157. pdimm->n_ranks = ((spd->organization >> 3) & 0x7) + 1;
  158. pdimm->rank_density = compute_ranksize(spd);
  159. pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
  160. pdimm->die_density = spd->density_banks & 0xf;
  161. pdimm->primary_sdram_width = 1 << (3 + (spd->bus_width & 0x7));
  162. if ((spd->bus_width >> 3) & 0x3)
  163. pdimm->ec_sdram_width = 8;
  164. else
  165. pdimm->ec_sdram_width = 0;
  166. pdimm->data_width = pdimm->primary_sdram_width
  167. + pdimm->ec_sdram_width;
  168. pdimm->device_width = 1 << ((spd->organization & 0x7) + 2);
  169. pdimm->package_3ds = (spd->package_type & 0x3) == 0x2 ?
  170. (spd->package_type >> 4) & 0x7 : 0;
  171. /* These are the types defined by the JEDEC SPD spec */
  172. pdimm->mirrored_dimm = 0;
  173. pdimm->registered_dimm = 0;
  174. switch (spd->module_type & DDR4_SPD_MODULETYPE_MASK) {
  175. case DDR4_SPD_MODULETYPE_RDIMM:
  176. /* Registered/buffered DIMMs */
  177. pdimm->registered_dimm = 1;
  178. if (spd->mod_section.registered.reg_map & 0x1)
  179. pdimm->mirrored_dimm = 1;
  180. break;
  181. case DDR4_SPD_MODULETYPE_UDIMM:
  182. case DDR4_SPD_MODULETYPE_SO_DIMM:
  183. /* Unbuffered DIMMs */
  184. if (spd->mod_section.unbuffered.addr_mapping & 0x1)
  185. pdimm->mirrored_dimm = 1;
  186. if ((spd->mod_section.unbuffered.mod_height & 0xe0) == 0 &&
  187. (spd->mod_section.unbuffered.ref_raw_card == 0x04)) {
  188. /* Fix SPD error found on DIMMs with raw card E0 */
  189. for (i = 0; i < 18; i++) {
  190. if (spd->mapping[i] == udimm_rc_e_dq[i])
  191. continue;
  192. spd_error = 1;
  193. debug("SPD byte %d: 0x%x, should be 0x%x\n",
  194. 60 + i, spd->mapping[i],
  195. udimm_rc_e_dq[i]);
  196. ptr = (u8 *)&spd->mapping[i];
  197. *ptr = udimm_rc_e_dq[i];
  198. }
  199. if (spd_error)
  200. puts("SPD DQ mapping error fixed\n");
  201. }
  202. break;
  203. default:
  204. printf("unknown module_type 0x%02X\n", spd->module_type);
  205. return 1;
  206. }
  207. /* SDRAM device parameters */
  208. pdimm->n_row_addr = ((spd->addressing >> 3) & 0x7) + 12;
  209. pdimm->n_col_addr = (spd->addressing & 0x7) + 9;
  210. pdimm->bank_addr_bits = (spd->density_banks >> 4) & 0x3;
  211. pdimm->bank_group_bits = (spd->density_banks >> 6) & 0x3;
  212. /*
  213. * The SPD spec has not the ECC bit,
  214. * We consider the DIMM as ECC capability
  215. * when the extension bus exist
  216. */
  217. if (pdimm->ec_sdram_width)
  218. pdimm->edc_config = 0x02;
  219. else
  220. pdimm->edc_config = 0x00;
  221. /*
  222. * The SPD spec has not the burst length byte
  223. * but DDR4 spec has nature BL8 and BC4,
  224. * BL8 -bit3, BC4 -bit2
  225. */
  226. pdimm->burst_lengths_bitmask = 0x0c;
  227. pdimm->row_density = __ilog2(pdimm->rank_density);
  228. /* MTB - medium timebase
  229. * The MTB in the SPD spec is 125ps,
  230. *
  231. * FTB - fine timebase
  232. * use 1/10th of ps as our unit to avoid floating point
  233. * eg, 10 for 1ps, 25 for 2.5ps, 50 for 5ps
  234. */
  235. if ((spd->timebases & 0xf) == 0x0) {
  236. pdimm->mtb_ps = 125;
  237. pdimm->ftb_10th_ps = 10;
  238. } else {
  239. printf("Unknown Timebases\n");
  240. }
  241. /* sdram minimum cycle time */
  242. pdimm->tckmin_x_ps = spd_to_ps(spd->tck_min, spd->fine_tck_min);
  243. /* sdram max cycle time */
  244. pdimm->tckmax_ps = spd_to_ps(spd->tck_max, spd->fine_tck_max);
  245. /*
  246. * CAS latency supported
  247. * bit0 - CL7
  248. * bit4 - CL11
  249. * bit8 - CL15
  250. * bit12- CL19
  251. * bit16- CL23
  252. */
  253. pdimm->caslat_x = (spd->caslat_b1 << 7) |
  254. (spd->caslat_b2 << 15) |
  255. (spd->caslat_b3 << 23);
  256. BUG_ON(spd->caslat_b4 != 0);
  257. /*
  258. * min CAS latency time
  259. */
  260. pdimm->taa_ps = spd_to_ps(spd->taa_min, spd->fine_taa_min);
  261. /*
  262. * min RAS to CAS delay time
  263. */
  264. pdimm->trcd_ps = spd_to_ps(spd->trcd_min, spd->fine_trcd_min);
  265. /*
  266. * Min Row Precharge Delay Time
  267. */
  268. pdimm->trp_ps = spd_to_ps(spd->trp_min, spd->fine_trp_min);
  269. /* min active to precharge delay time */
  270. pdimm->tras_ps = (((spd->tras_trc_ext & 0xf) << 8) +
  271. spd->tras_min_lsb) * pdimm->mtb_ps;
  272. /* min active to actice/refresh delay time */
  273. pdimm->trc_ps = spd_to_ps((((spd->tras_trc_ext & 0xf0) << 4) +
  274. spd->trc_min_lsb), spd->fine_trc_min);
  275. /* Min Refresh Recovery Delay Time */
  276. pdimm->trfc1_ps = ((spd->trfc1_min_msb << 8) | (spd->trfc1_min_lsb)) *
  277. pdimm->mtb_ps;
  278. pdimm->trfc2_ps = ((spd->trfc2_min_msb << 8) | (spd->trfc2_min_lsb)) *
  279. pdimm->mtb_ps;
  280. pdimm->trfc4_ps = ((spd->trfc4_min_msb << 8) | (spd->trfc4_min_lsb)) *
  281. pdimm->mtb_ps;
  282. /* min four active window delay time */
  283. pdimm->tfaw_ps = (((spd->tfaw_msb & 0xf) << 8) | spd->tfaw_min) *
  284. pdimm->mtb_ps;
  285. /* min row active to row active delay time, different bank group */
  286. pdimm->trrds_ps = spd_to_ps(spd->trrds_min, spd->fine_trrds_min);
  287. /* min row active to row active delay time, same bank group */
  288. pdimm->trrdl_ps = spd_to_ps(spd->trrdl_min, spd->fine_trrdl_min);
  289. /* min CAS to CAS Delay Time (tCCD_Lmin), same bank group */
  290. pdimm->tccdl_ps = spd_to_ps(spd->tccdl_min, spd->fine_tccdl_min);
  291. if (pdimm->package_3ds) {
  292. if (pdimm->die_density <= 0x4) {
  293. pdimm->trfc_slr_ps = 260000;
  294. } else if (pdimm->die_density <= 0x5) {
  295. pdimm->trfc_slr_ps = 350000;
  296. } else {
  297. printf("WARN: Unsupported logical rank density 0x%x\n",
  298. pdimm->die_density);
  299. }
  300. }
  301. /*
  302. * Average periodic refresh interval
  303. * tREFI = 7.8 us at normal temperature range
  304. */
  305. pdimm->refresh_rate_ps = 7800000;
  306. for (i = 0; i < 18; i++)
  307. pdimm->dq_mapping[i] = spd->mapping[i];
  308. pdimm->dq_mapping_ors = ((spd->mapping[0] >> 6) & 0x3) == 0 ? 1 : 0;
  309. return 0;
  310. }