lc_common_dimm_params.c 15 KB

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