ddr3_dimm_params.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  4. * Dave Liu <daveliu@freescale.com>
  5. *
  6. * calculate the organization and timing parameter
  7. * from ddr3 spd, please refer to the spec
  8. * JEDEC standard No.21-C 4_01_02_11R18.pdf
  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. * each rank size =
  18. * sdram capacity(bit) / 8 * primary bus width / sdram width
  19. *
  20. * where: sdram capacity = spd byte4[3:0]
  21. * primary bus width = spd byte8[2:0]
  22. * sdram width = spd byte7[2:0]
  23. *
  24. * SPD byte4 - sdram density and banks
  25. * bit[3:0] size(bit) size(byte)
  26. * 0000 256Mb 32MB
  27. * 0001 512Mb 64MB
  28. * 0010 1Gb 128MB
  29. * 0011 2Gb 256MB
  30. * 0100 4Gb 512MB
  31. * 0101 8Gb 1GB
  32. * 0110 16Gb 2GB
  33. *
  34. * SPD byte8 - module memory bus width
  35. * bit[2:0] primary bus width
  36. * 000 8bits
  37. * 001 16bits
  38. * 010 32bits
  39. * 011 64bits
  40. *
  41. * SPD byte7 - module organiztion
  42. * bit[2:0] sdram device width
  43. * 000 4bits
  44. * 001 8bits
  45. * 010 16bits
  46. * 011 32bits
  47. *
  48. */
  49. static unsigned long long
  50. compute_ranksize(const ddr3_spd_eeprom_t *spd)
  51. {
  52. unsigned long long bsize;
  53. int nbit_sdram_cap_bsize = 0;
  54. int nbit_primary_bus_width = 0;
  55. int nbit_sdram_width = 0;
  56. if ((spd->density_banks & 0xf) < 7)
  57. nbit_sdram_cap_bsize = (spd->density_banks & 0xf) + 28;
  58. if ((spd->bus_width & 0x7) < 4)
  59. nbit_primary_bus_width = (spd->bus_width & 0x7) + 3;
  60. if ((spd->organization & 0x7) < 4)
  61. nbit_sdram_width = (spd->organization & 0x7) + 2;
  62. bsize = 1ULL << (nbit_sdram_cap_bsize - 3
  63. + nbit_primary_bus_width - nbit_sdram_width);
  64. debug("DDR: DDR III rank density = 0x%16llx\n", bsize);
  65. return bsize;
  66. }
  67. /*
  68. * ddr_compute_dimm_parameters for DDR3 SPD
  69. *
  70. * Compute DIMM parameters based upon the SPD information in spd.
  71. * Writes the results to the dimm_params_t structure pointed by pdimm.
  72. *
  73. */
  74. unsigned int ddr_compute_dimm_parameters(const unsigned int ctrl_num,
  75. const ddr3_spd_eeprom_t *spd,
  76. dimm_params_t *pdimm,
  77. unsigned int dimm_number)
  78. {
  79. unsigned int retval;
  80. unsigned int mtb_ps;
  81. int ftb_10th_ps;
  82. int i;
  83. if (spd->mem_type) {
  84. if (spd->mem_type != SPD_MEMTYPE_DDR3) {
  85. printf("DIMM %u: is not a DDR3 SPD.\n", dimm_number);
  86. return 1;
  87. }
  88. } else {
  89. memset(pdimm, 0, sizeof(dimm_params_t));
  90. return 1;
  91. }
  92. retval = ddr3_spd_check(spd);
  93. if (retval) {
  94. printf("DIMM %u: failed checksum\n", dimm_number);
  95. return 2;
  96. }
  97. /*
  98. * The part name in ASCII in the SPD EEPROM is not null terminated.
  99. * Guarantee null termination here by presetting all bytes to 0
  100. * and copying the part name in ASCII from the SPD onto it
  101. */
  102. memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
  103. if ((spd->info_size_crc & 0xF) > 1)
  104. memcpy(pdimm->mpart, spd->mpart, sizeof(pdimm->mpart) - 1);
  105. /* DIMM organization parameters */
  106. pdimm->n_ranks = ((spd->organization >> 3) & 0x7) + 1;
  107. pdimm->rank_density = compute_ranksize(spd);
  108. pdimm->capacity = pdimm->n_ranks * pdimm->rank_density;
  109. pdimm->primary_sdram_width = 1 << (3 + (spd->bus_width & 0x7));
  110. if ((spd->bus_width >> 3) & 0x3)
  111. pdimm->ec_sdram_width = 8;
  112. else
  113. pdimm->ec_sdram_width = 0;
  114. pdimm->data_width = pdimm->primary_sdram_width
  115. + pdimm->ec_sdram_width;
  116. pdimm->device_width = 1 << ((spd->organization & 0x7) + 2);
  117. /* These are the types defined by the JEDEC DDR3 SPD spec */
  118. pdimm->mirrored_dimm = 0;
  119. pdimm->registered_dimm = 0;
  120. switch (spd->module_type & DDR3_SPD_MODULETYPE_MASK) {
  121. case DDR3_SPD_MODULETYPE_RDIMM:
  122. case DDR3_SPD_MODULETYPE_MINI_RDIMM:
  123. case DDR3_SPD_MODULETYPE_72B_SO_RDIMM:
  124. /* Registered/buffered DIMMs */
  125. pdimm->registered_dimm = 1;
  126. for (i = 0; i < 16; i += 2) {
  127. u8 rcw = spd->mod_section.registered.rcw[i/2];
  128. pdimm->rcw[i] = (rcw >> 0) & 0x0F;
  129. pdimm->rcw[i+1] = (rcw >> 4) & 0x0F;
  130. }
  131. break;
  132. case DDR3_SPD_MODULETYPE_UDIMM:
  133. case DDR3_SPD_MODULETYPE_SO_DIMM:
  134. case DDR3_SPD_MODULETYPE_MICRO_DIMM:
  135. case DDR3_SPD_MODULETYPE_MINI_UDIMM:
  136. case DDR3_SPD_MODULETYPE_MINI_CDIMM:
  137. case DDR3_SPD_MODULETYPE_72B_SO_UDIMM:
  138. case DDR3_SPD_MODULETYPE_72B_SO_CDIMM:
  139. case DDR3_SPD_MODULETYPE_LRDIMM:
  140. case DDR3_SPD_MODULETYPE_16B_SO_DIMM:
  141. case DDR3_SPD_MODULETYPE_32B_SO_DIMM:
  142. /* Unbuffered DIMMs */
  143. if (spd->mod_section.unbuffered.addr_mapping & 0x1)
  144. pdimm->mirrored_dimm = 1;
  145. break;
  146. default:
  147. printf("unknown module_type 0x%02X\n", spd->module_type);
  148. return 1;
  149. }
  150. /* SDRAM device parameters */
  151. pdimm->n_row_addr = ((spd->addressing >> 3) & 0x7) + 12;
  152. pdimm->n_col_addr = (spd->addressing & 0x7) + 9;
  153. pdimm->n_banks_per_sdram_device = 8 << ((spd->density_banks >> 4) & 0x7);
  154. /*
  155. * The SPD spec has not the ECC bit,
  156. * We consider the DIMM as ECC capability
  157. * when the extension bus exist
  158. */
  159. if (pdimm->ec_sdram_width)
  160. pdimm->edc_config = 0x02;
  161. else
  162. pdimm->edc_config = 0x00;
  163. /*
  164. * The SPD spec has not the burst length byte
  165. * but DDR3 spec has nature BL8 and BC4,
  166. * BL8 -bit3, BC4 -bit2
  167. */
  168. pdimm->burst_lengths_bitmask = 0x0c;
  169. /* MTB - medium timebase
  170. * The unit in the SPD spec is ns,
  171. * We convert it to ps.
  172. * eg: MTB = 0.125ns (125ps)
  173. */
  174. mtb_ps = (spd->mtb_dividend * 1000) /spd->mtb_divisor;
  175. pdimm->mtb_ps = mtb_ps;
  176. /*
  177. * FTB - fine timebase
  178. * use 1/10th of ps as our unit to avoid floating point
  179. * eg, 10 for 1ps, 25 for 2.5ps, 50 for 5ps
  180. */
  181. ftb_10th_ps =
  182. ((spd->ftb_div & 0xf0) >> 4) * 10 / (spd->ftb_div & 0x0f);
  183. pdimm->ftb_10th_ps = ftb_10th_ps;
  184. /*
  185. * sdram minimum cycle time
  186. * we assume the MTB is 0.125ns
  187. * eg:
  188. * tck_min=15 MTB (1.875ns) ->DDR3-1066
  189. * =12 MTB (1.5ns) ->DDR3-1333
  190. * =10 MTB (1.25ns) ->DDR3-1600
  191. */
  192. pdimm->tckmin_x_ps = spd->tck_min * mtb_ps +
  193. (spd->fine_tck_min * ftb_10th_ps) / 10;
  194. /*
  195. * CAS latency supported
  196. * bit4 - CL4
  197. * bit5 - CL5
  198. * bit18 - CL18
  199. */
  200. pdimm->caslat_x = ((spd->caslat_msb << 8) | spd->caslat_lsb) << 4;
  201. /*
  202. * min CAS latency time
  203. * eg: taa_min =
  204. * DDR3-800D 100 MTB (12.5ns)
  205. * DDR3-1066F 105 MTB (13.125ns)
  206. * DDR3-1333H 108 MTB (13.5ns)
  207. * DDR3-1600H 90 MTB (11.25ns)
  208. */
  209. pdimm->taa_ps = spd->taa_min * mtb_ps +
  210. (spd->fine_taa_min * ftb_10th_ps) / 10;
  211. /*
  212. * min write recovery time
  213. * eg:
  214. * twr_min = 120 MTB (15ns) -> all speed grades.
  215. */
  216. pdimm->twr_ps = spd->twr_min * mtb_ps;
  217. /*
  218. * min RAS to CAS delay time
  219. * eg: trcd_min =
  220. * DDR3-800 100 MTB (12.5ns)
  221. * DDR3-1066F 105 MTB (13.125ns)
  222. * DDR3-1333H 108 MTB (13.5ns)
  223. * DDR3-1600H 90 MTB (11.25)
  224. */
  225. pdimm->trcd_ps = spd->trcd_min * mtb_ps +
  226. (spd->fine_trcd_min * ftb_10th_ps) / 10;
  227. /*
  228. * min row active to row active delay time
  229. * eg: trrd_min =
  230. * DDR3-800(1KB page) 80 MTB (10ns)
  231. * DDR3-1333(1KB page) 48 MTB (6ns)
  232. */
  233. pdimm->trrd_ps = spd->trrd_min * mtb_ps;
  234. /*
  235. * min row precharge delay time
  236. * eg: trp_min =
  237. * DDR3-800D 100 MTB (12.5ns)
  238. * DDR3-1066F 105 MTB (13.125ns)
  239. * DDR3-1333H 108 MTB (13.5ns)
  240. * DDR3-1600H 90 MTB (11.25ns)
  241. */
  242. pdimm->trp_ps = spd->trp_min * mtb_ps +
  243. (spd->fine_trp_min * ftb_10th_ps) / 10;
  244. /* min active to precharge delay time
  245. * eg: tRAS_min =
  246. * DDR3-800D 300 MTB (37.5ns)
  247. * DDR3-1066F 300 MTB (37.5ns)
  248. * DDR3-1333H 288 MTB (36ns)
  249. * DDR3-1600H 280 MTB (35ns)
  250. */
  251. pdimm->tras_ps = (((spd->tras_trc_ext & 0xf) << 8) | spd->tras_min_lsb)
  252. * mtb_ps;
  253. /*
  254. * min active to actice/refresh delay time
  255. * eg: tRC_min =
  256. * DDR3-800D 400 MTB (50ns)
  257. * DDR3-1066F 405 MTB (50.625ns)
  258. * DDR3-1333H 396 MTB (49.5ns)
  259. * DDR3-1600H 370 MTB (46.25ns)
  260. */
  261. pdimm->trc_ps = (((spd->tras_trc_ext & 0xf0) << 4) | spd->trc_min_lsb)
  262. * mtb_ps + (spd->fine_trc_min * ftb_10th_ps) / 10;
  263. /*
  264. * min refresh recovery delay time
  265. * eg: tRFC_min =
  266. * 512Mb 720 MTB (90ns)
  267. * 1Gb 880 MTB (110ns)
  268. * 2Gb 1280 MTB (160ns)
  269. */
  270. pdimm->trfc_ps = ((spd->trfc_min_msb << 8) | spd->trfc_min_lsb)
  271. * mtb_ps;
  272. /*
  273. * min internal write to read command delay time
  274. * eg: twtr_min = 40 MTB (7.5ns) - all speed bins.
  275. * tWRT is at least 4 mclk independent of operating freq.
  276. */
  277. pdimm->twtr_ps = spd->twtr_min * mtb_ps;
  278. /*
  279. * min internal read to precharge command delay time
  280. * eg: trtp_min = 40 MTB (7.5ns) - all speed bins.
  281. * tRTP is at least 4 mclk independent of operating freq.
  282. */
  283. pdimm->trtp_ps = spd->trtp_min * mtb_ps;
  284. /*
  285. * Average periodic refresh interval
  286. * tREFI = 7.8 us at normal temperature range
  287. * = 3.9 us at ext temperature range
  288. */
  289. pdimm->refresh_rate_ps = 7800000;
  290. if ((spd->therm_ref_opt & 0x1) && !(spd->therm_ref_opt & 0x2)) {
  291. pdimm->refresh_rate_ps = 3900000;
  292. pdimm->extended_op_srt = 1;
  293. }
  294. /*
  295. * min four active window delay time
  296. * eg: tfaw_min =
  297. * DDR3-800(1KB page) 320 MTB (40ns)
  298. * DDR3-1066(1KB page) 300 MTB (37.5ns)
  299. * DDR3-1333(1KB page) 240 MTB (30ns)
  300. * DDR3-1600(1KB page) 240 MTB (30ns)
  301. */
  302. pdimm->tfaw_ps = (((spd->tfaw_msb & 0xf) << 8) | spd->tfaw_min)
  303. * mtb_ps;
  304. return 0;
  305. }