lc_common_dimm_params.c 15 KB

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