lc_common_dimm_params.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright 2008 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * Version 2 as published by the Free Software Foundation.
  7. */
  8. #include <common.h>
  9. #include <asm/fsl_ddr_sdram.h>
  10. #include "ddr.h"
  11. /*
  12. * compute_lowest_common_dimm_parameters()
  13. *
  14. * Determine the worst-case DIMM timing parameters from the set of DIMMs
  15. * whose parameters have been computed into the array pointed to
  16. * by dimm_params.
  17. */
  18. unsigned int
  19. compute_lowest_common_dimm_parameters(const dimm_params_t *dimm_params,
  20. common_timing_params_t *outpdimm,
  21. unsigned int number_of_dimms)
  22. {
  23. unsigned int i;
  24. unsigned int tCKmin_X_ps = 0;
  25. unsigned int tCKmax_ps = 0xFFFFFFFF;
  26. unsigned int tCKmax_max_ps = 0;
  27. unsigned int tRCD_ps = 0;
  28. unsigned int tRP_ps = 0;
  29. unsigned int tRAS_ps = 0;
  30. unsigned int tWR_ps = 0;
  31. unsigned int tWTR_ps = 0;
  32. unsigned int tRFC_ps = 0;
  33. unsigned int tRRD_ps = 0;
  34. unsigned int tRC_ps = 0;
  35. unsigned int refresh_rate_ps = 0;
  36. unsigned int tIS_ps = 0;
  37. unsigned int tIH_ps = 0;
  38. unsigned int tDS_ps = 0;
  39. unsigned int tDH_ps = 0;
  40. unsigned int tRTP_ps = 0;
  41. unsigned int tDQSQ_max_ps = 0;
  42. unsigned int tQHS_ps = 0;
  43. unsigned int temp1, temp2;
  44. unsigned int lowest_good_caslat;
  45. unsigned int additive_latency = 0;
  46. const unsigned int mclk_ps = get_memory_clk_period_ps();
  47. unsigned int not_ok;
  48. debug("using mclk_ps = %u\n", mclk_ps);
  49. temp1 = 0;
  50. for (i = 0; i < number_of_dimms; i++) {
  51. /*
  52. * If there are no ranks on this DIMM,
  53. * it probably doesn't exist, so skip it.
  54. */
  55. if (dimm_params[i].n_ranks == 0) {
  56. temp1++;
  57. continue;
  58. }
  59. /*
  60. * Find minimum tCKmax_ps to find fastest slow speed,
  61. * i.e., this is the slowest the whole system can go.
  62. */
  63. tCKmax_ps = min(tCKmax_ps, dimm_params[i].tCKmax_ps);
  64. /* Either find maximum value to determine slowest
  65. * speed, delay, time, period, etc */
  66. tCKmin_X_ps = max(tCKmin_X_ps, dimm_params[i].tCKmin_X_ps);
  67. tCKmax_max_ps = max(tCKmax_max_ps, dimm_params[i].tCKmax_ps);
  68. tRCD_ps = max(tRCD_ps, dimm_params[i].tRCD_ps);
  69. tRP_ps = max(tRP_ps, dimm_params[i].tRP_ps);
  70. tRAS_ps = max(tRAS_ps, dimm_params[i].tRAS_ps);
  71. tWR_ps = max(tWR_ps, dimm_params[i].tWR_ps);
  72. tWTR_ps = max(tWTR_ps, dimm_params[i].tWTR_ps);
  73. tRFC_ps = max(tRFC_ps, dimm_params[i].tRFC_ps);
  74. tRRD_ps = max(tRRD_ps, dimm_params[i].tRRD_ps);
  75. tRC_ps = max(tRC_ps, dimm_params[i].tRC_ps);
  76. tIS_ps = max(tIS_ps, dimm_params[i].tIS_ps);
  77. tIH_ps = max(tIH_ps, dimm_params[i].tIH_ps);
  78. tDS_ps = max(tDS_ps, dimm_params[i].tDS_ps);
  79. tDH_ps = max(tDH_ps, dimm_params[i].tDH_ps);
  80. tRTP_ps = max(tRTP_ps, dimm_params[i].tRTP_ps);
  81. tQHS_ps = max(tQHS_ps, dimm_params[i].tQHS_ps);
  82. refresh_rate_ps = max(refresh_rate_ps,
  83. dimm_params[i].refresh_rate_ps);
  84. /*
  85. * Find maximum tDQSQ_max_ps to find slowest.
  86. *
  87. * FIXME: is finding the slowest value the correct
  88. * strategy for this parameter?
  89. */
  90. tDQSQ_max_ps = max(tDQSQ_max_ps, dimm_params[i].tDQSQ_max_ps);
  91. }
  92. outpdimm->ndimms_present = number_of_dimms - temp1;
  93. if (temp1 == number_of_dimms) {
  94. debug("no dimms this memory controller\n");
  95. return 0;
  96. }
  97. outpdimm->tCKmin_X_ps = tCKmin_X_ps;
  98. outpdimm->tCKmax_ps = tCKmax_ps;
  99. outpdimm->tCKmax_max_ps = tCKmax_max_ps;
  100. outpdimm->tRCD_ps = tRCD_ps;
  101. outpdimm->tRP_ps = tRP_ps;
  102. outpdimm->tRAS_ps = tRAS_ps;
  103. outpdimm->tWR_ps = tWR_ps;
  104. outpdimm->tWTR_ps = tWTR_ps;
  105. outpdimm->tRFC_ps = tRFC_ps;
  106. outpdimm->tRRD_ps = tRRD_ps;
  107. outpdimm->tRC_ps = tRC_ps;
  108. outpdimm->refresh_rate_ps = refresh_rate_ps;
  109. outpdimm->tIS_ps = tIS_ps;
  110. outpdimm->tIH_ps = tIH_ps;
  111. outpdimm->tDS_ps = tDS_ps;
  112. outpdimm->tDH_ps = tDH_ps;
  113. outpdimm->tRTP_ps = tRTP_ps;
  114. outpdimm->tDQSQ_max_ps = tDQSQ_max_ps;
  115. outpdimm->tQHS_ps = tQHS_ps;
  116. /* Determine common burst length for all DIMMs. */
  117. temp1 = 0xff;
  118. for (i = 0; i < number_of_dimms; i++) {
  119. if (dimm_params[i].n_ranks) {
  120. temp1 &= dimm_params[i].burst_lengths_bitmask;
  121. }
  122. }
  123. outpdimm->all_DIMMs_burst_lengths_bitmask = temp1;
  124. /* Determine if all DIMMs registered buffered. */
  125. temp1 = temp2 = 0;
  126. for (i = 0; i < number_of_dimms; i++) {
  127. if (dimm_params[i].n_ranks) {
  128. if (dimm_params[i].registered_dimm)
  129. temp1 = 1;
  130. if (!dimm_params[i].registered_dimm)
  131. temp2 = 1;
  132. }
  133. }
  134. outpdimm->all_DIMMs_registered = 0;
  135. if (temp1 && !temp2) {
  136. outpdimm->all_DIMMs_registered = 1;
  137. }
  138. outpdimm->all_DIMMs_unbuffered = 0;
  139. if (!temp1 && temp2) {
  140. outpdimm->all_DIMMs_unbuffered = 1;
  141. }
  142. /* CHECKME: */
  143. if (!outpdimm->all_DIMMs_registered
  144. && !outpdimm->all_DIMMs_unbuffered) {
  145. printf("ERROR: Mix of registered buffered and unbuffered "
  146. "DIMMs detected!\n");
  147. }
  148. /*
  149. * Compute a CAS latency suitable for all DIMMs
  150. *
  151. * Strategy for SPD-defined latencies: compute only
  152. * CAS latency defined by all DIMMs.
  153. */
  154. /*
  155. * Step 1: find CAS latency common to all DIMMs using bitwise
  156. * operation.
  157. */
  158. temp1 = 0xFF;
  159. for (i = 0; i < number_of_dimms; i++) {
  160. if (dimm_params[i].n_ranks) {
  161. temp2 = 0;
  162. temp2 |= 1 << dimm_params[i].caslat_X;
  163. temp2 |= 1 << dimm_params[i].caslat_X_minus_1;
  164. temp2 |= 1 << dimm_params[i].caslat_X_minus_2;
  165. /*
  166. * FIXME: If there was no entry for X-2 (X-1) in
  167. * the SPD, then caslat_X_minus_2
  168. * (caslat_X_minus_1) contains either 255 or
  169. * 0xFFFFFFFF because that's what the glorious
  170. * __ilog2 function returns for an input of 0.
  171. * On 32-bit PowerPC, left shift counts with bit
  172. * 26 set (that the value of 255 or 0xFFFFFFFF
  173. * will have), cause the destination register to
  174. * be 0. That is why this works.
  175. */
  176. temp1 &= temp2;
  177. }
  178. }
  179. /*
  180. * Step 2: check each common CAS latency against tCK of each
  181. * DIMM's SPD.
  182. */
  183. lowest_good_caslat = 0;
  184. temp2 = 0;
  185. while (temp1) {
  186. not_ok = 0;
  187. temp2 = __ilog2(temp1);
  188. debug("checking common caslat = %u\n", temp2);
  189. /* Check if this CAS latency will work on all DIMMs at tCK. */
  190. for (i = 0; i < number_of_dimms; i++) {
  191. if (!dimm_params[i].n_ranks) {
  192. continue;
  193. }
  194. if (dimm_params[i].caslat_X == temp2) {
  195. if (mclk_ps >= dimm_params[i].tCKmin_X_ps) {
  196. debug("CL = %u ok on DIMM %u at tCK=%u"
  197. " ps with its tCKmin_X_ps of %u\n",
  198. temp2, i, mclk_ps,
  199. dimm_params[i].tCKmin_X_ps);
  200. continue;
  201. } else {
  202. not_ok++;
  203. }
  204. }
  205. if (dimm_params[i].caslat_X_minus_1 == temp2) {
  206. unsigned int tCKmin_X_minus_1_ps
  207. = dimm_params[i].tCKmin_X_minus_1_ps;
  208. if (mclk_ps >= tCKmin_X_minus_1_ps) {
  209. debug("CL = %u ok on DIMM %u at "
  210. "tCK=%u ps with its "
  211. "tCKmin_X_minus_1_ps of %u\n",
  212. temp2, i, mclk_ps,
  213. tCKmin_X_minus_1_ps);
  214. continue;
  215. } else {
  216. not_ok++;
  217. }
  218. }
  219. if (dimm_params[i].caslat_X_minus_2 == temp2) {
  220. unsigned int tCKmin_X_minus_2_ps
  221. = dimm_params[i].tCKmin_X_minus_2_ps;
  222. if (mclk_ps >= tCKmin_X_minus_2_ps) {
  223. debug("CL = %u ok on DIMM %u at "
  224. "tCK=%u ps with its "
  225. "tCKmin_X_minus_2_ps of %u\n",
  226. temp2, i, mclk_ps,
  227. tCKmin_X_minus_2_ps);
  228. continue;
  229. } else {
  230. not_ok++;
  231. }
  232. }
  233. }
  234. if (!not_ok) {
  235. lowest_good_caslat = temp2;
  236. }
  237. temp1 &= ~(1 << temp2);
  238. }
  239. debug("lowest common SPD-defined CAS latency = %u\n",
  240. lowest_good_caslat);
  241. outpdimm->lowest_common_SPD_caslat = lowest_good_caslat;
  242. /*
  243. * Compute a common 'de-rated' CAS latency.
  244. *
  245. * The strategy here is to find the *highest* dereated cas latency
  246. * with the assumption that all of the DIMMs will support a dereated
  247. * CAS latency higher than or equal to their lowest dereated value.
  248. */
  249. temp1 = 0;
  250. for (i = 0; i < number_of_dimms; i++) {
  251. temp1 = max(temp1, dimm_params[i].caslat_lowest_derated);
  252. }
  253. outpdimm->highest_common_derated_caslat = temp1;
  254. debug("highest common dereated CAS latency = %u\n", temp1);
  255. /* Determine if all DIMMs ECC capable. */
  256. temp1 = 1;
  257. for (i = 0; i < number_of_dimms; i++) {
  258. if (dimm_params[i].n_ranks && dimm_params[i].edc_config != 2) {
  259. temp1 = 0;
  260. break;
  261. }
  262. }
  263. if (temp1) {
  264. debug("all DIMMs ECC capable\n");
  265. } else {
  266. debug("Warning: not all DIMMs ECC capable, cant enable ECC\n");
  267. }
  268. outpdimm->all_DIMMs_ECC_capable = temp1;
  269. /* FIXME: move to somewhere else to validate. */
  270. if (mclk_ps > tCKmax_max_ps) {
  271. printf("Warning: some of the installed DIMMs "
  272. "can not operate this slowly.\n");
  273. return 1;
  274. }
  275. /*
  276. * Compute additive latency.
  277. *
  278. * For DDR1, additive latency should be 0.
  279. *
  280. * For DDR2, with ODT enabled, use "a value" less than ACTTORW,
  281. * which comes from Trcd, and also note that:
  282. * add_lat + caslat must be >= 4
  283. *
  284. * For DDR3, FIXME additive latency determination
  285. *
  286. * When to use additive latency for DDR2:
  287. *
  288. * I. Because you are using CL=3 and need to do ODT on writes and
  289. * want functionality.
  290. * 1. Are you going to use ODT? (Does your board not have
  291. * additional termination circuitry for DQ, DQS, DQS_,
  292. * DM, RDQS, RDQS_ for x4/x8 configs?)
  293. * 2. If so, is your lowest supported CL going to be 3?
  294. * 3. If so, then you must set AL=1 because
  295. *
  296. * WL >= 3 for ODT on writes
  297. * RL = AL + CL
  298. * WL = RL - 1
  299. * ->
  300. * WL = AL + CL - 1
  301. * AL + CL - 1 >= 3
  302. * AL + CL >= 4
  303. * QED
  304. *
  305. * RL >= 3 for ODT on reads
  306. * RL = AL + CL
  307. *
  308. * Since CL aren't usually less than 2, AL=0 is a minimum,
  309. * so the WL-derived AL should be the -- FIXME?
  310. *
  311. * II. Because you are using auto-precharge globally and want to
  312. * use additive latency (posted CAS) to get more bandwidth.
  313. * 1. Are you going to use auto-precharge mode globally?
  314. *
  315. * Use addtivie latency and compute AL to be 1 cycle less than
  316. * tRCD, i.e. the READ or WRITE command is in the cycle
  317. * immediately following the ACTIVATE command..
  318. *
  319. * III. Because you feel like it or want to do some sort of
  320. * degraded-performance experiment.
  321. * 1. Do you just want to use additive latency because you feel
  322. * like it?
  323. *
  324. * Validation: AL is less than tRCD, and within the other
  325. * read-to-precharge constraints.
  326. */
  327. additive_latency = 0;
  328. #if defined(CONFIG_FSL_DDR2)
  329. if (lowest_good_caslat < 4) {
  330. additive_latency = picos_to_mclk(tRCD_ps) - lowest_good_caslat;
  331. if (mclk_to_picos(additive_latency) > tRCD_ps) {
  332. additive_latency = picos_to_mclk(tRCD_ps);
  333. debug("setting additive_latency to %u because it was "
  334. " greater than tRCD_ps\n", additive_latency);
  335. }
  336. }
  337. #elif defined(CONFIG_FSL_DDR3)
  338. error "FIXME determine additive latency for DDR3"
  339. #endif
  340. /*
  341. * Validate additive latency
  342. * FIXME: move to somewhere else to validate
  343. *
  344. * AL <= tRCD(min)
  345. */
  346. if (mclk_to_picos(additive_latency) > tRCD_ps) {
  347. printf("Error: invalid additive latency exceeds tRCD(min).\n");
  348. return 1;
  349. }
  350. /*
  351. * RL = CL + AL; RL >= 3 for ODT_RD_CFG to be enabled
  352. * WL = RL - 1; WL >= 3 for ODT_WL_CFG to be enabled
  353. * ADD_LAT (the register) must be set to a value less
  354. * than ACTTORW if WL = 1, then AL must be set to 1
  355. * RD_TO_PRE (the register) must be set to a minimum
  356. * tRTP + AL if AL is nonzero
  357. */
  358. /*
  359. * Additive latency will be applied only if the memctl option to
  360. * use it.
  361. */
  362. outpdimm->additive_latency = additive_latency;
  363. return 0;
  364. }