ddr4_dimm_params.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 DDR3 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
  114. ddr_compute_dimm_parameters(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. if (spd->mem_type) {
  121. if (spd->mem_type != SPD_MEMTYPE_DDR4) {
  122. printf("DIMM %u: is not a DDR4 SPD.\n", dimm_number);
  123. return 1;
  124. }
  125. } else {
  126. memset(pdimm, 0, sizeof(dimm_params_t));
  127. return 1;
  128. }
  129. retval = ddr4_spd_check(spd);
  130. if (retval) {
  131. printf("DIMM %u: failed checksum\n", dimm_number);
  132. return 2;
  133. }
  134. /*
  135. * The part name in ASCII in the SPD EEPROM is not null terminated.
  136. * Guarantee null termination here by presetting all bytes to 0
  137. * and copying the part name in ASCII from the SPD onto it
  138. */
  139. memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
  140. if ((spd->info_size_crc & 0xF) > 2)
  141. memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
  142. /* DIMM organization parameters */
  143. pdimm->n_ranks = ((spd->organization >> 3) & 0x7) + 1;
  144. pdimm->rank_density = compute_ranksize(spd);
  145. pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
  146. pdimm->primary_sdram_width = 1 << (3 + (spd->bus_width & 0x7));
  147. if ((spd->bus_width >> 3) & 0x3)
  148. pdimm->ec_sdram_width = 8;
  149. else
  150. pdimm->ec_sdram_width = 0;
  151. pdimm->data_width = pdimm->primary_sdram_width
  152. + pdimm->ec_sdram_width;
  153. pdimm->device_width = 1 << ((spd->organization & 0x7) + 2);
  154. /* These are the types defined by the JEDEC DDR3 SPD spec */
  155. pdimm->mirrored_dimm = 0;
  156. pdimm->registered_dimm = 0;
  157. switch (spd->module_type & DDR3_SPD_MODULETYPE_MASK) {
  158. case DDR3_SPD_MODULETYPE_RDIMM:
  159. /* Registered/buffered DIMMs */
  160. pdimm->registered_dimm = 1;
  161. break;
  162. case DDR3_SPD_MODULETYPE_UDIMM:
  163. case DDR3_SPD_MODULETYPE_SO_DIMM:
  164. /* Unbuffered DIMMs */
  165. if (spd->mod_section.unbuffered.addr_mapping & 0x1)
  166. pdimm->mirrored_dimm = 1;
  167. break;
  168. default:
  169. printf("unknown module_type 0x%02X\n", spd->module_type);
  170. return 1;
  171. }
  172. /* SDRAM device parameters */
  173. pdimm->n_row_addr = ((spd->addressing >> 3) & 0x7) + 12;
  174. pdimm->n_col_addr = (spd->addressing & 0x7) + 9;
  175. pdimm->bank_addr_bits = (spd->density_banks >> 4) & 0x3;
  176. pdimm->bank_group_bits = (spd->density_banks >> 6) & 0x3;
  177. /*
  178. * The SPD spec has not the ECC bit,
  179. * We consider the DIMM as ECC capability
  180. * when the extension bus exist
  181. */
  182. if (pdimm->ec_sdram_width)
  183. pdimm->edc_config = 0x02;
  184. else
  185. pdimm->edc_config = 0x00;
  186. /*
  187. * The SPD spec has not the burst length byte
  188. * but DDR4 spec has nature BL8 and BC4,
  189. * BL8 -bit3, BC4 -bit2
  190. */
  191. pdimm->burst_lengths_bitmask = 0x0c;
  192. pdimm->row_density = __ilog2(pdimm->rank_density);
  193. /* MTB - medium timebase
  194. * The MTB in the SPD spec is 125ps,
  195. *
  196. * FTB - fine timebase
  197. * use 1/10th of ps as our unit to avoid floating point
  198. * eg, 10 for 1ps, 25 for 2.5ps, 50 for 5ps
  199. */
  200. if ((spd->timebases & 0xf) == 0x0) {
  201. pdimm->mtb_ps = 125;
  202. pdimm->ftb_10th_ps = 10;
  203. } else {
  204. printf("Unknown Timebases\n");
  205. }
  206. /* sdram minimum cycle time */
  207. pdimm->tckmin_x_ps = spd_to_ps(spd->tck_min, spd->fine_tck_min);
  208. /* sdram max cycle time */
  209. pdimm->tckmax_ps = spd_to_ps(spd->tck_max, spd->fine_tck_max);
  210. /*
  211. * CAS latency supported
  212. * bit0 - CL7
  213. * bit4 - CL11
  214. * bit8 - CL15
  215. * bit12- CL19
  216. * bit16- CL23
  217. */
  218. pdimm->caslat_x = (spd->caslat_b1 << 7) |
  219. (spd->caslat_b2 << 15) |
  220. (spd->caslat_b3 << 23);
  221. BUG_ON(spd->caslat_b4 != 0);
  222. /*
  223. * min CAS latency time
  224. */
  225. pdimm->taa_ps = spd_to_ps(spd->taa_min, spd->fine_taa_min);
  226. /*
  227. * min RAS to CAS delay time
  228. */
  229. pdimm->trcd_ps = spd_to_ps(spd->trcd_min, spd->fine_trcd_min);
  230. /*
  231. * Min Row Precharge Delay Time
  232. */
  233. pdimm->trp_ps = spd_to_ps(spd->trp_min, spd->fine_trp_min);
  234. /* min active to precharge delay time */
  235. pdimm->tras_ps = (((spd->tras_trc_ext & 0xf) << 8) +
  236. spd->tras_min_lsb) * pdimm->mtb_ps;
  237. /* min active to actice/refresh delay time */
  238. pdimm->trc_ps = spd_to_ps((((spd->tras_trc_ext & 0xf0) << 4) +
  239. spd->trc_min_lsb), spd->fine_trc_min);
  240. /* Min Refresh Recovery Delay Time */
  241. pdimm->trfc1_ps = ((spd->trfc1_min_msb << 8) | (spd->trfc1_min_lsb)) *
  242. pdimm->mtb_ps;
  243. pdimm->trfc2_ps = ((spd->trfc2_min_msb << 8) | (spd->trfc2_min_lsb)) *
  244. pdimm->mtb_ps;
  245. pdimm->trfc4_ps = ((spd->trfc4_min_msb << 8) | (spd->trfc4_min_lsb)) *
  246. pdimm->mtb_ps;
  247. /* min four active window delay time */
  248. pdimm->tfaw_ps = (((spd->tfaw_msb & 0xf) << 8) | spd->tfaw_min) *
  249. pdimm->mtb_ps;
  250. /* min row active to row active delay time, different bank group */
  251. pdimm->trrds_ps = spd_to_ps(spd->trrds_min, spd->fine_trrds_min);
  252. /* min row active to row active delay time, same bank group */
  253. pdimm->trrdl_ps = spd_to_ps(spd->trrdl_min, spd->fine_trrdl_min);
  254. /* min CAS to CAS Delay Time (tCCD_Lmin), same bank group */
  255. pdimm->tccdl_ps = spd_to_ps(spd->tccdl_min, spd->fine_tccdl_min);
  256. /*
  257. * Average periodic refresh interval
  258. * tREFI = 7.8 us at normal temperature range
  259. */
  260. pdimm->refresh_rate_ps = 7800000;
  261. for (i = 0; i < 18; i++)
  262. pdimm->dq_mapping[i] = spd->mapping[i];
  263. pdimm->dq_mapping_ors = ((spd->mapping[0] >> 6) & 0x3) == 0 ? 1 : 0;
  264. return 0;
  265. }