lc_common_dimm_params.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2008-2011 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. unsigned int
  12. compute_cas_latency_ddr3(const dimm_params_t *dimm_params,
  13. common_timing_params_t *outpdimm,
  14. unsigned int number_of_dimms)
  15. {
  16. unsigned int i;
  17. unsigned int tAAmin_ps = 0;
  18. unsigned int tCKmin_X_ps = 0;
  19. unsigned int common_caslat;
  20. unsigned int caslat_actual;
  21. unsigned int retry = 16;
  22. unsigned int tmp;
  23. const unsigned int mclk_ps = get_memory_clk_period_ps();
  24. /* compute the common CAS latency supported between slots */
  25. tmp = dimm_params[0].caslat_X;
  26. for (i = 1; i < number_of_dimms; i++)
  27. tmp &= dimm_params[i].caslat_X;
  28. common_caslat = tmp;
  29. /* compute the max tAAmin tCKmin between slots */
  30. for (i = 0; i < number_of_dimms; i++) {
  31. tAAmin_ps = max(tAAmin_ps, dimm_params[i].tAA_ps);
  32. tCKmin_X_ps = max(tCKmin_X_ps, dimm_params[i].tCKmin_X_ps);
  33. }
  34. /* validate if the memory clk is in the range of dimms */
  35. if (mclk_ps < tCKmin_X_ps) {
  36. printf("DDR clock (MCLK cycle %u ps) is faster than "
  37. "the slowest DIMM(s) (tCKmin %u ps) can support.\n",
  38. mclk_ps, tCKmin_X_ps);
  39. return 1;
  40. }
  41. /* determine the acutal cas latency */
  42. caslat_actual = (tAAmin_ps + mclk_ps - 1) / mclk_ps;
  43. /* check if the dimms support the CAS latency */
  44. while (!(common_caslat & (1 << caslat_actual)) && retry > 0) {
  45. caslat_actual++;
  46. retry--;
  47. }
  48. /* once the caculation of caslat_actual is completed
  49. * we must verify that this CAS latency value does not
  50. * exceed tAAmax, which is 20 ns for all DDR3 speed grades
  51. */
  52. if (caslat_actual * mclk_ps > 20000) {
  53. printf("The choosen cas latency %d is too large\n",
  54. caslat_actual);
  55. return 1;
  56. }
  57. outpdimm->lowest_common_SPD_caslat = caslat_actual;
  58. return 0;
  59. }
  60. /*
  61. * compute_lowest_common_dimm_parameters()
  62. *
  63. * Determine the worst-case DIMM timing parameters from the set of DIMMs
  64. * whose parameters have been computed into the array pointed to
  65. * by dimm_params.
  66. */
  67. unsigned int
  68. compute_lowest_common_dimm_parameters(const dimm_params_t *dimm_params,
  69. common_timing_params_t *outpdimm,
  70. unsigned int number_of_dimms)
  71. {
  72. unsigned int i, j;
  73. unsigned int tCKmin_X_ps = 0;
  74. unsigned int tCKmax_ps = 0xFFFFFFFF;
  75. unsigned int tCKmax_max_ps = 0;
  76. unsigned int tRCD_ps = 0;
  77. unsigned int tRP_ps = 0;
  78. unsigned int tRAS_ps = 0;
  79. unsigned int tWR_ps = 0;
  80. unsigned int tWTR_ps = 0;
  81. unsigned int tRFC_ps = 0;
  82. unsigned int tRRD_ps = 0;
  83. unsigned int tRC_ps = 0;
  84. unsigned int refresh_rate_ps = 0;
  85. unsigned int tIS_ps = 0;
  86. unsigned int tIH_ps = 0;
  87. unsigned int tDS_ps = 0;
  88. unsigned int tDH_ps = 0;
  89. unsigned int tRTP_ps = 0;
  90. unsigned int tDQSQ_max_ps = 0;
  91. unsigned int tQHS_ps = 0;
  92. unsigned int temp1, temp2;
  93. unsigned int additive_latency = 0;
  94. #if !defined(CONFIG_FSL_DDR3)
  95. const unsigned int mclk_ps = get_memory_clk_period_ps();
  96. unsigned int lowest_good_caslat;
  97. unsigned int not_ok;
  98. debug("using mclk_ps = %u\n", mclk_ps);
  99. #endif
  100. temp1 = 0;
  101. for (i = 0; i < number_of_dimms; i++) {
  102. /*
  103. * If there are no ranks on this DIMM,
  104. * it probably doesn't exist, so skip it.
  105. */
  106. if (dimm_params[i].n_ranks == 0) {
  107. temp1++;
  108. continue;
  109. }
  110. if (dimm_params[i].n_ranks == 4 && i != 0) {
  111. printf("Found Quad-rank DIMM in wrong bank, ignored."
  112. " Software may not run as expected.\n");
  113. temp1++;
  114. continue;
  115. }
  116. if (dimm_params[i].n_ranks == 4 && \
  117. CONFIG_CHIP_SELECTS_PER_CTRL/CONFIG_DIMM_SLOTS_PER_CTLR < 4) {
  118. printf("Found Quad-rank DIMM, not able to support.");
  119. temp1++;
  120. continue;
  121. }
  122. /*
  123. * Find minimum tCKmax_ps to find fastest slow speed,
  124. * i.e., this is the slowest the whole system can go.
  125. */
  126. tCKmax_ps = min(tCKmax_ps, dimm_params[i].tCKmax_ps);
  127. /* Either find maximum value to determine slowest
  128. * speed, delay, time, period, etc */
  129. tCKmin_X_ps = max(tCKmin_X_ps, dimm_params[i].tCKmin_X_ps);
  130. tCKmax_max_ps = max(tCKmax_max_ps, dimm_params[i].tCKmax_ps);
  131. tRCD_ps = max(tRCD_ps, dimm_params[i].tRCD_ps);
  132. tRP_ps = max(tRP_ps, dimm_params[i].tRP_ps);
  133. tRAS_ps = max(tRAS_ps, dimm_params[i].tRAS_ps);
  134. tWR_ps = max(tWR_ps, dimm_params[i].tWR_ps);
  135. tWTR_ps = max(tWTR_ps, dimm_params[i].tWTR_ps);
  136. tRFC_ps = max(tRFC_ps, dimm_params[i].tRFC_ps);
  137. tRRD_ps = max(tRRD_ps, dimm_params[i].tRRD_ps);
  138. tRC_ps = max(tRC_ps, dimm_params[i].tRC_ps);
  139. tIS_ps = max(tIS_ps, dimm_params[i].tIS_ps);
  140. tIH_ps = max(tIH_ps, dimm_params[i].tIH_ps);
  141. tDS_ps = max(tDS_ps, dimm_params[i].tDS_ps);
  142. tDH_ps = max(tDH_ps, dimm_params[i].tDH_ps);
  143. tRTP_ps = max(tRTP_ps, dimm_params[i].tRTP_ps);
  144. tQHS_ps = max(tQHS_ps, dimm_params[i].tQHS_ps);
  145. refresh_rate_ps = max(refresh_rate_ps,
  146. dimm_params[i].refresh_rate_ps);
  147. /*
  148. * Find maximum tDQSQ_max_ps to find slowest.
  149. *
  150. * FIXME: is finding the slowest value the correct
  151. * strategy for this parameter?
  152. */
  153. tDQSQ_max_ps = max(tDQSQ_max_ps, dimm_params[i].tDQSQ_max_ps);
  154. }
  155. outpdimm->ndimms_present = number_of_dimms - temp1;
  156. if (temp1 == number_of_dimms) {
  157. debug("no dimms this memory controller\n");
  158. return 0;
  159. }
  160. outpdimm->tCKmin_X_ps = tCKmin_X_ps;
  161. outpdimm->tCKmax_ps = tCKmax_ps;
  162. outpdimm->tCKmax_max_ps = tCKmax_max_ps;
  163. outpdimm->tRCD_ps = tRCD_ps;
  164. outpdimm->tRP_ps = tRP_ps;
  165. outpdimm->tRAS_ps = tRAS_ps;
  166. outpdimm->tWR_ps = tWR_ps;
  167. outpdimm->tWTR_ps = tWTR_ps;
  168. outpdimm->tRFC_ps = tRFC_ps;
  169. outpdimm->tRRD_ps = tRRD_ps;
  170. outpdimm->tRC_ps = tRC_ps;
  171. outpdimm->refresh_rate_ps = refresh_rate_ps;
  172. outpdimm->tIS_ps = tIS_ps;
  173. outpdimm->tIH_ps = tIH_ps;
  174. outpdimm->tDS_ps = tDS_ps;
  175. outpdimm->tDH_ps = tDH_ps;
  176. outpdimm->tRTP_ps = tRTP_ps;
  177. outpdimm->tDQSQ_max_ps = tDQSQ_max_ps;
  178. outpdimm->tQHS_ps = tQHS_ps;
  179. /* Determine common burst length for all DIMMs. */
  180. temp1 = 0xff;
  181. for (i = 0; i < number_of_dimms; i++) {
  182. if (dimm_params[i].n_ranks) {
  183. temp1 &= dimm_params[i].burst_lengths_bitmask;
  184. }
  185. }
  186. outpdimm->all_DIMMs_burst_lengths_bitmask = temp1;
  187. /* Determine if all DIMMs registered buffered. */
  188. temp1 = temp2 = 0;
  189. for (i = 0; i < number_of_dimms; i++) {
  190. if (dimm_params[i].n_ranks) {
  191. if (dimm_params[i].registered_dimm) {
  192. temp1 = 1;
  193. printf("Detected RDIMM %s\n",
  194. dimm_params[i].mpart);
  195. } else {
  196. temp2 = 1;
  197. printf("Detected UDIMM %s\n",
  198. dimm_params[i].mpart);
  199. }
  200. }
  201. }
  202. outpdimm->all_DIMMs_registered = 0;
  203. outpdimm->all_DIMMs_unbuffered = 0;
  204. if (temp1 && !temp2) {
  205. outpdimm->all_DIMMs_registered = 1;
  206. } else if (!temp1 && temp2) {
  207. outpdimm->all_DIMMs_unbuffered = 1;
  208. } else {
  209. printf("ERROR: Mix of registered buffered and unbuffered "
  210. "DIMMs detected!\n");
  211. }
  212. temp1 = 0;
  213. if (outpdimm->all_DIMMs_registered)
  214. for (j = 0; j < 16; j++) {
  215. outpdimm->rcw[j] = dimm_params[0].rcw[j];
  216. for (i = 1; i < number_of_dimms; i++)
  217. if (dimm_params[i].rcw[j] != dimm_params[0].rcw[j]) {
  218. temp1 = 1;
  219. break;
  220. }
  221. }
  222. if (temp1 != 0)
  223. printf("ERROR: Mix different RDIMM detected!\n");
  224. #if defined(CONFIG_FSL_DDR3)
  225. if (compute_cas_latency_ddr3(dimm_params, outpdimm, number_of_dimms))
  226. return 1;
  227. #else
  228. /*
  229. * Compute a CAS latency suitable for all DIMMs
  230. *
  231. * Strategy for SPD-defined latencies: compute only
  232. * CAS latency defined by all DIMMs.
  233. */
  234. /*
  235. * Step 1: find CAS latency common to all DIMMs using bitwise
  236. * operation.
  237. */
  238. temp1 = 0xFF;
  239. for (i = 0; i < number_of_dimms; i++) {
  240. if (dimm_params[i].n_ranks) {
  241. temp2 = 0;
  242. temp2 |= 1 << dimm_params[i].caslat_X;
  243. temp2 |= 1 << dimm_params[i].caslat_X_minus_1;
  244. temp2 |= 1 << dimm_params[i].caslat_X_minus_2;
  245. /*
  246. * FIXME: If there was no entry for X-2 (X-1) in
  247. * the SPD, then caslat_X_minus_2
  248. * (caslat_X_minus_1) contains either 255 or
  249. * 0xFFFFFFFF because that's what the glorious
  250. * __ilog2 function returns for an input of 0.
  251. * On 32-bit PowerPC, left shift counts with bit
  252. * 26 set (that the value of 255 or 0xFFFFFFFF
  253. * will have), cause the destination register to
  254. * be 0. That is why this works.
  255. */
  256. temp1 &= temp2;
  257. }
  258. }
  259. /*
  260. * Step 2: check each common CAS latency against tCK of each
  261. * DIMM's SPD.
  262. */
  263. lowest_good_caslat = 0;
  264. temp2 = 0;
  265. while (temp1) {
  266. not_ok = 0;
  267. temp2 = __ilog2(temp1);
  268. debug("checking common caslat = %u\n", temp2);
  269. /* Check if this CAS latency will work on all DIMMs at tCK. */
  270. for (i = 0; i < number_of_dimms; i++) {
  271. if (!dimm_params[i].n_ranks) {
  272. continue;
  273. }
  274. if (dimm_params[i].caslat_X == temp2) {
  275. if (mclk_ps >= dimm_params[i].tCKmin_X_ps) {
  276. debug("CL = %u ok on DIMM %u at tCK=%u"
  277. " ps with its tCKmin_X_ps of %u\n",
  278. temp2, i, mclk_ps,
  279. dimm_params[i].tCKmin_X_ps);
  280. continue;
  281. } else {
  282. not_ok++;
  283. }
  284. }
  285. if (dimm_params[i].caslat_X_minus_1 == temp2) {
  286. unsigned int tCKmin_X_minus_1_ps
  287. = dimm_params[i].tCKmin_X_minus_1_ps;
  288. if (mclk_ps >= tCKmin_X_minus_1_ps) {
  289. debug("CL = %u ok on DIMM %u at "
  290. "tCK=%u ps with its "
  291. "tCKmin_X_minus_1_ps of %u\n",
  292. temp2, i, mclk_ps,
  293. tCKmin_X_minus_1_ps);
  294. continue;
  295. } else {
  296. not_ok++;
  297. }
  298. }
  299. if (dimm_params[i].caslat_X_minus_2 == temp2) {
  300. unsigned int tCKmin_X_minus_2_ps
  301. = dimm_params[i].tCKmin_X_minus_2_ps;
  302. if (mclk_ps >= tCKmin_X_minus_2_ps) {
  303. debug("CL = %u ok on DIMM %u at "
  304. "tCK=%u ps with its "
  305. "tCKmin_X_minus_2_ps of %u\n",
  306. temp2, i, mclk_ps,
  307. tCKmin_X_minus_2_ps);
  308. continue;
  309. } else {
  310. not_ok++;
  311. }
  312. }
  313. }
  314. if (!not_ok) {
  315. lowest_good_caslat = temp2;
  316. }
  317. temp1 &= ~(1 << temp2);
  318. }
  319. debug("lowest common SPD-defined CAS latency = %u\n",
  320. lowest_good_caslat);
  321. outpdimm->lowest_common_SPD_caslat = lowest_good_caslat;
  322. /*
  323. * Compute a common 'de-rated' CAS latency.
  324. *
  325. * The strategy here is to find the *highest* dereated cas latency
  326. * with the assumption that all of the DIMMs will support a dereated
  327. * CAS latency higher than or equal to their lowest dereated value.
  328. */
  329. temp1 = 0;
  330. for (i = 0; i < number_of_dimms; i++) {
  331. temp1 = max(temp1, dimm_params[i].caslat_lowest_derated);
  332. }
  333. outpdimm->highest_common_derated_caslat = temp1;
  334. debug("highest common dereated CAS latency = %u\n", temp1);
  335. #endif /* #if defined(CONFIG_FSL_DDR3) */
  336. /* Determine if all DIMMs ECC capable. */
  337. temp1 = 1;
  338. for (i = 0; i < number_of_dimms; i++) {
  339. if (dimm_params[i].n_ranks &&
  340. !(dimm_params[i].edc_config & EDC_ECC)) {
  341. temp1 = 0;
  342. break;
  343. }
  344. }
  345. if (temp1) {
  346. debug("all DIMMs ECC capable\n");
  347. } else {
  348. debug("Warning: not all DIMMs ECC capable, cant enable ECC\n");
  349. }
  350. outpdimm->all_DIMMs_ECC_capable = temp1;
  351. #ifndef CONFIG_FSL_DDR3
  352. /* FIXME: move to somewhere else to validate. */
  353. if (mclk_ps > tCKmax_max_ps) {
  354. printf("Warning: some of the installed DIMMs "
  355. "can not operate this slowly.\n");
  356. return 1;
  357. }
  358. #endif
  359. /*
  360. * Compute additive latency.
  361. *
  362. * For DDR1, additive latency should be 0.
  363. *
  364. * For DDR2, with ODT enabled, use "a value" less than ACTTORW,
  365. * which comes from Trcd, and also note that:
  366. * add_lat + caslat must be >= 4
  367. *
  368. * For DDR3, we use the AL=0
  369. *
  370. * When to use additive latency for DDR2:
  371. *
  372. * I. Because you are using CL=3 and need to do ODT on writes and
  373. * want functionality.
  374. * 1. Are you going to use ODT? (Does your board not have
  375. * additional termination circuitry for DQ, DQS, DQS_,
  376. * DM, RDQS, RDQS_ for x4/x8 configs?)
  377. * 2. If so, is your lowest supported CL going to be 3?
  378. * 3. If so, then you must set AL=1 because
  379. *
  380. * WL >= 3 for ODT on writes
  381. * RL = AL + CL
  382. * WL = RL - 1
  383. * ->
  384. * WL = AL + CL - 1
  385. * AL + CL - 1 >= 3
  386. * AL + CL >= 4
  387. * QED
  388. *
  389. * RL >= 3 for ODT on reads
  390. * RL = AL + CL
  391. *
  392. * Since CL aren't usually less than 2, AL=0 is a minimum,
  393. * so the WL-derived AL should be the -- FIXME?
  394. *
  395. * II. Because you are using auto-precharge globally and want to
  396. * use additive latency (posted CAS) to get more bandwidth.
  397. * 1. Are you going to use auto-precharge mode globally?
  398. *
  399. * Use addtivie latency and compute AL to be 1 cycle less than
  400. * tRCD, i.e. the READ or WRITE command is in the cycle
  401. * immediately following the ACTIVATE command..
  402. *
  403. * III. Because you feel like it or want to do some sort of
  404. * degraded-performance experiment.
  405. * 1. Do you just want to use additive latency because you feel
  406. * like it?
  407. *
  408. * Validation: AL is less than tRCD, and within the other
  409. * read-to-precharge constraints.
  410. */
  411. additive_latency = 0;
  412. #if defined(CONFIG_FSL_DDR2)
  413. if (lowest_good_caslat < 4) {
  414. additive_latency = picos_to_mclk(tRCD_ps) - lowest_good_caslat;
  415. if (mclk_to_picos(additive_latency) > tRCD_ps) {
  416. additive_latency = picos_to_mclk(tRCD_ps);
  417. debug("setting additive_latency to %u because it was "
  418. " greater than tRCD_ps\n", additive_latency);
  419. }
  420. }
  421. #elif defined(CONFIG_FSL_DDR3)
  422. /*
  423. * The system will not use the global auto-precharge mode.
  424. * However, it uses the page mode, so we set AL=0
  425. */
  426. additive_latency = 0;
  427. #endif
  428. /*
  429. * Validate additive latency
  430. * FIXME: move to somewhere else to validate
  431. *
  432. * AL <= tRCD(min)
  433. */
  434. if (mclk_to_picos(additive_latency) > tRCD_ps) {
  435. printf("Error: invalid additive latency exceeds tRCD(min).\n");
  436. return 1;
  437. }
  438. /*
  439. * RL = CL + AL; RL >= 3 for ODT_RD_CFG to be enabled
  440. * WL = RL - 1; WL >= 3 for ODT_WL_CFG to be enabled
  441. * ADD_LAT (the register) must be set to a value less
  442. * than ACTTORW if WL = 1, then AL must be set to 1
  443. * RD_TO_PRE (the register) must be set to a minimum
  444. * tRTP + AL if AL is nonzero
  445. */
  446. /*
  447. * Additive latency will be applied only if the memctl option to
  448. * use it.
  449. */
  450. outpdimm->additive_latency = additive_latency;
  451. return 0;
  452. }