ddr4_dimm_params.c 8.5 KB

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