options.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright 2008, 2010-2012 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. */
  9. #include <common.h>
  10. #include <hwconfig.h>
  11. #include <asm/fsl_ddr_sdram.h>
  12. #include "ddr.h"
  13. /*
  14. * Use our own stack based buffer before relocation to allow accessing longer
  15. * hwconfig strings that might be in the environment before we've relocated.
  16. * This is pretty fragile on both the use of stack and if the buffer is big
  17. * enough. However we will get a warning from getenv_f for the later.
  18. */
  19. /* Board-specific functions defined in each board's ddr.c */
  20. extern void fsl_ddr_board_options(memctl_options_t *popts,
  21. dimm_params_t *pdimm,
  22. unsigned int ctrl_num);
  23. struct dynamic_odt {
  24. unsigned int odt_rd_cfg;
  25. unsigned int odt_wr_cfg;
  26. unsigned int odt_rtt_norm;
  27. unsigned int odt_rtt_wr;
  28. };
  29. #ifdef CONFIG_FSL_DDR3
  30. static const struct dynamic_odt single_Q[4] = {
  31. { /* cs0 */
  32. FSL_DDR_ODT_NEVER,
  33. FSL_DDR_ODT_CS_AND_OTHER_DIMM,
  34. DDR3_RTT_20_OHM,
  35. DDR3_RTT_120_OHM
  36. },
  37. { /* cs1 */
  38. FSL_DDR_ODT_NEVER,
  39. FSL_DDR_ODT_NEVER, /* tied high */
  40. DDR3_RTT_OFF,
  41. DDR3_RTT_120_OHM
  42. },
  43. { /* cs2 */
  44. FSL_DDR_ODT_NEVER,
  45. FSL_DDR_ODT_CS_AND_OTHER_DIMM,
  46. DDR3_RTT_20_OHM,
  47. DDR3_RTT_120_OHM
  48. },
  49. { /* cs3 */
  50. FSL_DDR_ODT_NEVER,
  51. FSL_DDR_ODT_NEVER, /* tied high */
  52. DDR3_RTT_OFF,
  53. DDR3_RTT_120_OHM
  54. }
  55. };
  56. static const struct dynamic_odt single_D[4] = {
  57. { /* cs0 */
  58. FSL_DDR_ODT_NEVER,
  59. FSL_DDR_ODT_ALL,
  60. DDR3_RTT_40_OHM,
  61. DDR3_RTT_OFF
  62. },
  63. { /* cs1 */
  64. FSL_DDR_ODT_NEVER,
  65. FSL_DDR_ODT_NEVER,
  66. DDR3_RTT_OFF,
  67. DDR3_RTT_OFF
  68. },
  69. {0, 0, 0, 0},
  70. {0, 0, 0, 0}
  71. };
  72. static const struct dynamic_odt single_S[4] = {
  73. { /* cs0 */
  74. FSL_DDR_ODT_NEVER,
  75. FSL_DDR_ODT_ALL,
  76. DDR3_RTT_40_OHM,
  77. DDR3_RTT_OFF
  78. },
  79. {0, 0, 0, 0},
  80. {0, 0, 0, 0},
  81. {0, 0, 0, 0},
  82. };
  83. static const struct dynamic_odt dual_DD[4] = {
  84. { /* cs0 */
  85. FSL_DDR_ODT_NEVER,
  86. FSL_DDR_ODT_SAME_DIMM,
  87. DDR3_RTT_120_OHM,
  88. DDR3_RTT_OFF
  89. },
  90. { /* cs1 */
  91. FSL_DDR_ODT_OTHER_DIMM,
  92. FSL_DDR_ODT_OTHER_DIMM,
  93. DDR3_RTT_30_OHM,
  94. DDR3_RTT_OFF
  95. },
  96. { /* cs2 */
  97. FSL_DDR_ODT_NEVER,
  98. FSL_DDR_ODT_SAME_DIMM,
  99. DDR3_RTT_120_OHM,
  100. DDR3_RTT_OFF
  101. },
  102. { /* cs3 */
  103. FSL_DDR_ODT_OTHER_DIMM,
  104. FSL_DDR_ODT_OTHER_DIMM,
  105. DDR3_RTT_30_OHM,
  106. DDR3_RTT_OFF
  107. }
  108. };
  109. static const struct dynamic_odt dual_DS[4] = {
  110. { /* cs0 */
  111. FSL_DDR_ODT_NEVER,
  112. FSL_DDR_ODT_SAME_DIMM,
  113. DDR3_RTT_120_OHM,
  114. DDR3_RTT_OFF
  115. },
  116. { /* cs1 */
  117. FSL_DDR_ODT_OTHER_DIMM,
  118. FSL_DDR_ODT_OTHER_DIMM,
  119. DDR3_RTT_30_OHM,
  120. DDR3_RTT_OFF
  121. },
  122. { /* cs2 */
  123. FSL_DDR_ODT_OTHER_DIMM,
  124. FSL_DDR_ODT_ALL,
  125. DDR3_RTT_20_OHM,
  126. DDR3_RTT_120_OHM
  127. },
  128. {0, 0, 0, 0}
  129. };
  130. static const struct dynamic_odt dual_SD[4] = {
  131. { /* cs0 */
  132. FSL_DDR_ODT_OTHER_DIMM,
  133. FSL_DDR_ODT_ALL,
  134. DDR3_RTT_20_OHM,
  135. DDR3_RTT_120_OHM
  136. },
  137. {0, 0, 0, 0},
  138. { /* cs2 */
  139. FSL_DDR_ODT_NEVER,
  140. FSL_DDR_ODT_SAME_DIMM,
  141. DDR3_RTT_120_OHM,
  142. DDR3_RTT_OFF
  143. },
  144. { /* cs3 */
  145. FSL_DDR_ODT_OTHER_DIMM,
  146. FSL_DDR_ODT_OTHER_DIMM,
  147. DDR3_RTT_20_OHM,
  148. DDR3_RTT_OFF
  149. }
  150. };
  151. static const struct dynamic_odt dual_SS[4] = {
  152. { /* cs0 */
  153. FSL_DDR_ODT_OTHER_DIMM,
  154. FSL_DDR_ODT_ALL,
  155. DDR3_RTT_30_OHM,
  156. DDR3_RTT_120_OHM
  157. },
  158. {0, 0, 0, 0},
  159. { /* cs2 */
  160. FSL_DDR_ODT_OTHER_DIMM,
  161. FSL_DDR_ODT_ALL,
  162. DDR3_RTT_30_OHM,
  163. DDR3_RTT_120_OHM
  164. },
  165. {0, 0, 0, 0}
  166. };
  167. static const struct dynamic_odt dual_D0[4] = {
  168. { /* cs0 */
  169. FSL_DDR_ODT_NEVER,
  170. FSL_DDR_ODT_SAME_DIMM,
  171. DDR3_RTT_40_OHM,
  172. DDR3_RTT_OFF
  173. },
  174. { /* cs1 */
  175. FSL_DDR_ODT_NEVER,
  176. FSL_DDR_ODT_NEVER,
  177. DDR3_RTT_OFF,
  178. DDR3_RTT_OFF
  179. },
  180. {0, 0, 0, 0},
  181. {0, 0, 0, 0}
  182. };
  183. static const struct dynamic_odt dual_0D[4] = {
  184. {0, 0, 0, 0},
  185. {0, 0, 0, 0},
  186. { /* cs2 */
  187. FSL_DDR_ODT_NEVER,
  188. FSL_DDR_ODT_SAME_DIMM,
  189. DDR3_RTT_40_OHM,
  190. DDR3_RTT_OFF
  191. },
  192. { /* cs3 */
  193. FSL_DDR_ODT_NEVER,
  194. FSL_DDR_ODT_NEVER,
  195. DDR3_RTT_OFF,
  196. DDR3_RTT_OFF
  197. }
  198. };
  199. static const struct dynamic_odt dual_S0[4] = {
  200. { /* cs0 */
  201. FSL_DDR_ODT_NEVER,
  202. FSL_DDR_ODT_CS,
  203. DDR3_RTT_40_OHM,
  204. DDR3_RTT_OFF
  205. },
  206. {0, 0, 0, 0},
  207. {0, 0, 0, 0},
  208. {0, 0, 0, 0}
  209. };
  210. static const struct dynamic_odt dual_0S[4] = {
  211. {0, 0, 0, 0},
  212. {0, 0, 0, 0},
  213. { /* cs2 */
  214. FSL_DDR_ODT_NEVER,
  215. FSL_DDR_ODT_CS,
  216. DDR3_RTT_40_OHM,
  217. DDR3_RTT_OFF
  218. },
  219. {0, 0, 0, 0}
  220. };
  221. static const struct dynamic_odt odt_unknown[4] = {
  222. { /* cs0 */
  223. FSL_DDR_ODT_NEVER,
  224. FSL_DDR_ODT_CS,
  225. DDR3_RTT_120_OHM,
  226. DDR3_RTT_OFF
  227. },
  228. { /* cs1 */
  229. FSL_DDR_ODT_NEVER,
  230. FSL_DDR_ODT_CS,
  231. DDR3_RTT_120_OHM,
  232. DDR3_RTT_OFF
  233. },
  234. { /* cs2 */
  235. FSL_DDR_ODT_NEVER,
  236. FSL_DDR_ODT_CS,
  237. DDR3_RTT_120_OHM,
  238. DDR3_RTT_OFF
  239. },
  240. { /* cs3 */
  241. FSL_DDR_ODT_NEVER,
  242. FSL_DDR_ODT_CS,
  243. DDR3_RTT_120_OHM,
  244. DDR3_RTT_OFF
  245. }
  246. };
  247. #else /* CONFIG_FSL_DDR3 */
  248. static const struct dynamic_odt single_Q[4] = {
  249. {0, 0, 0, 0},
  250. {0, 0, 0, 0},
  251. {0, 0, 0, 0},
  252. {0, 0, 0, 0}
  253. };
  254. static const struct dynamic_odt single_D[4] = {
  255. { /* cs0 */
  256. FSL_DDR_ODT_NEVER,
  257. FSL_DDR_ODT_ALL,
  258. DDR2_RTT_150_OHM,
  259. DDR2_RTT_OFF
  260. },
  261. { /* cs1 */
  262. FSL_DDR_ODT_NEVER,
  263. FSL_DDR_ODT_NEVER,
  264. DDR2_RTT_OFF,
  265. DDR2_RTT_OFF
  266. },
  267. {0, 0, 0, 0},
  268. {0, 0, 0, 0}
  269. };
  270. static const struct dynamic_odt single_S[4] = {
  271. { /* cs0 */
  272. FSL_DDR_ODT_NEVER,
  273. FSL_DDR_ODT_ALL,
  274. DDR2_RTT_150_OHM,
  275. DDR2_RTT_OFF
  276. },
  277. {0, 0, 0, 0},
  278. {0, 0, 0, 0},
  279. {0, 0, 0, 0},
  280. };
  281. static const struct dynamic_odt dual_DD[4] = {
  282. { /* cs0 */
  283. FSL_DDR_ODT_OTHER_DIMM,
  284. FSL_DDR_ODT_OTHER_DIMM,
  285. DDR2_RTT_75_OHM,
  286. DDR2_RTT_OFF
  287. },
  288. { /* cs1 */
  289. FSL_DDR_ODT_NEVER,
  290. FSL_DDR_ODT_NEVER,
  291. DDR2_RTT_OFF,
  292. DDR2_RTT_OFF
  293. },
  294. { /* cs2 */
  295. FSL_DDR_ODT_OTHER_DIMM,
  296. FSL_DDR_ODT_OTHER_DIMM,
  297. DDR2_RTT_75_OHM,
  298. DDR2_RTT_OFF
  299. },
  300. { /* cs3 */
  301. FSL_DDR_ODT_NEVER,
  302. FSL_DDR_ODT_NEVER,
  303. DDR2_RTT_OFF,
  304. DDR2_RTT_OFF
  305. }
  306. };
  307. static const struct dynamic_odt dual_DS[4] = {
  308. { /* cs0 */
  309. FSL_DDR_ODT_OTHER_DIMM,
  310. FSL_DDR_ODT_OTHER_DIMM,
  311. DDR2_RTT_75_OHM,
  312. DDR2_RTT_OFF
  313. },
  314. { /* cs1 */
  315. FSL_DDR_ODT_NEVER,
  316. FSL_DDR_ODT_NEVER,
  317. DDR2_RTT_OFF,
  318. DDR2_RTT_OFF
  319. },
  320. { /* cs2 */
  321. FSL_DDR_ODT_OTHER_DIMM,
  322. FSL_DDR_ODT_OTHER_DIMM,
  323. DDR2_RTT_75_OHM,
  324. DDR2_RTT_OFF
  325. },
  326. {0, 0, 0, 0}
  327. };
  328. static const struct dynamic_odt dual_SD[4] = {
  329. { /* cs0 */
  330. FSL_DDR_ODT_OTHER_DIMM,
  331. FSL_DDR_ODT_OTHER_DIMM,
  332. DDR2_RTT_75_OHM,
  333. DDR2_RTT_OFF
  334. },
  335. {0, 0, 0, 0},
  336. { /* cs2 */
  337. FSL_DDR_ODT_OTHER_DIMM,
  338. FSL_DDR_ODT_OTHER_DIMM,
  339. DDR2_RTT_75_OHM,
  340. DDR2_RTT_OFF
  341. },
  342. { /* cs3 */
  343. FSL_DDR_ODT_NEVER,
  344. FSL_DDR_ODT_NEVER,
  345. DDR2_RTT_OFF,
  346. DDR2_RTT_OFF
  347. }
  348. };
  349. static const struct dynamic_odt dual_SS[4] = {
  350. { /* cs0 */
  351. FSL_DDR_ODT_OTHER_DIMM,
  352. FSL_DDR_ODT_OTHER_DIMM,
  353. DDR2_RTT_75_OHM,
  354. DDR2_RTT_OFF
  355. },
  356. {0, 0, 0, 0},
  357. { /* cs2 */
  358. FSL_DDR_ODT_OTHER_DIMM,
  359. FSL_DDR_ODT_OTHER_DIMM,
  360. DDR2_RTT_75_OHM,
  361. DDR2_RTT_OFF
  362. },
  363. {0, 0, 0, 0}
  364. };
  365. static const struct dynamic_odt dual_D0[4] = {
  366. { /* cs0 */
  367. FSL_DDR_ODT_NEVER,
  368. FSL_DDR_ODT_ALL,
  369. DDR2_RTT_150_OHM,
  370. DDR2_RTT_OFF
  371. },
  372. { /* cs1 */
  373. FSL_DDR_ODT_NEVER,
  374. FSL_DDR_ODT_NEVER,
  375. DDR2_RTT_OFF,
  376. DDR2_RTT_OFF
  377. },
  378. {0, 0, 0, 0},
  379. {0, 0, 0, 0}
  380. };
  381. static const struct dynamic_odt dual_0D[4] = {
  382. {0, 0, 0, 0},
  383. {0, 0, 0, 0},
  384. { /* cs2 */
  385. FSL_DDR_ODT_NEVER,
  386. FSL_DDR_ODT_ALL,
  387. DDR2_RTT_150_OHM,
  388. DDR2_RTT_OFF
  389. },
  390. { /* cs3 */
  391. FSL_DDR_ODT_NEVER,
  392. FSL_DDR_ODT_NEVER,
  393. DDR2_RTT_OFF,
  394. DDR2_RTT_OFF
  395. }
  396. };
  397. static const struct dynamic_odt dual_S0[4] = {
  398. { /* cs0 */
  399. FSL_DDR_ODT_NEVER,
  400. FSL_DDR_ODT_CS,
  401. DDR2_RTT_150_OHM,
  402. DDR2_RTT_OFF
  403. },
  404. {0, 0, 0, 0},
  405. {0, 0, 0, 0},
  406. {0, 0, 0, 0}
  407. };
  408. static const struct dynamic_odt dual_0S[4] = {
  409. {0, 0, 0, 0},
  410. {0, 0, 0, 0},
  411. { /* cs2 */
  412. FSL_DDR_ODT_NEVER,
  413. FSL_DDR_ODT_CS,
  414. DDR2_RTT_150_OHM,
  415. DDR2_RTT_OFF
  416. },
  417. {0, 0, 0, 0}
  418. };
  419. static const struct dynamic_odt odt_unknown[4] = {
  420. { /* cs0 */
  421. FSL_DDR_ODT_NEVER,
  422. FSL_DDR_ODT_CS,
  423. DDR2_RTT_75_OHM,
  424. DDR2_RTT_OFF
  425. },
  426. { /* cs1 */
  427. FSL_DDR_ODT_NEVER,
  428. FSL_DDR_ODT_NEVER,
  429. DDR2_RTT_OFF,
  430. DDR2_RTT_OFF
  431. },
  432. { /* cs2 */
  433. FSL_DDR_ODT_NEVER,
  434. FSL_DDR_ODT_CS,
  435. DDR2_RTT_75_OHM,
  436. DDR2_RTT_OFF
  437. },
  438. { /* cs3 */
  439. FSL_DDR_ODT_NEVER,
  440. FSL_DDR_ODT_NEVER,
  441. DDR2_RTT_OFF,
  442. DDR2_RTT_OFF
  443. }
  444. };
  445. #endif
  446. unsigned int populate_memctl_options(int all_DIMMs_registered,
  447. memctl_options_t *popts,
  448. dimm_params_t *pdimm,
  449. unsigned int ctrl_num)
  450. {
  451. unsigned int i;
  452. char buffer[HWCONFIG_BUFFER_SIZE];
  453. char *buf = NULL;
  454. #if defined(CONFIG_FSL_DDR3) || defined(CONFIG_FSL_DDR2)
  455. const struct dynamic_odt *pdodt = odt_unknown;
  456. #endif
  457. ulong ddr_freq;
  458. /*
  459. * Extract hwconfig from environment since we have not properly setup
  460. * the environment but need it for ddr config params
  461. */
  462. if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
  463. buf = buffer;
  464. #if defined(CONFIG_FSL_DDR3) || defined(CONFIG_FSL_DDR2)
  465. /* Chip select options. */
  466. if (CONFIG_DIMM_SLOTS_PER_CTLR == 1) {
  467. switch (pdimm[0].n_ranks) {
  468. case 1:
  469. pdodt = single_S;
  470. break;
  471. case 2:
  472. pdodt = single_D;
  473. break;
  474. case 4:
  475. pdodt = single_Q;
  476. break;
  477. }
  478. } else if (CONFIG_DIMM_SLOTS_PER_CTLR == 2) {
  479. switch (pdimm[0].n_ranks) {
  480. case 2:
  481. switch (pdimm[1].n_ranks) {
  482. case 2:
  483. pdodt = dual_DD;
  484. break;
  485. case 1:
  486. pdodt = dual_DS;
  487. break;
  488. case 0:
  489. pdodt = dual_D0;
  490. break;
  491. }
  492. break;
  493. case 1:
  494. switch (pdimm[1].n_ranks) {
  495. case 2:
  496. pdodt = dual_SD;
  497. break;
  498. case 1:
  499. pdodt = dual_SS;
  500. break;
  501. case 0:
  502. pdodt = dual_S0;
  503. break;
  504. }
  505. break;
  506. case 0:
  507. switch (pdimm[1].n_ranks) {
  508. case 2:
  509. pdodt = dual_0D;
  510. break;
  511. case 1:
  512. pdodt = dual_0S;
  513. break;
  514. }
  515. break;
  516. }
  517. }
  518. #endif
  519. /* Pick chip-select local options. */
  520. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  521. #if defined(CONFIG_FSL_DDR3) || defined(CONFIG_FSL_DDR2)
  522. popts->cs_local_opts[i].odt_rd_cfg = pdodt[i].odt_rd_cfg;
  523. popts->cs_local_opts[i].odt_wr_cfg = pdodt[i].odt_wr_cfg;
  524. popts->cs_local_opts[i].odt_rtt_norm = pdodt[i].odt_rtt_norm;
  525. popts->cs_local_opts[i].odt_rtt_wr = pdodt[i].odt_rtt_wr;
  526. #else
  527. popts->cs_local_opts[i].odt_rd_cfg = FSL_DDR_ODT_NEVER;
  528. popts->cs_local_opts[i].odt_wr_cfg = FSL_DDR_ODT_CS;
  529. #endif
  530. popts->cs_local_opts[i].auto_precharge = 0;
  531. }
  532. /* Pick interleaving mode. */
  533. /*
  534. * 0 = no interleaving
  535. * 1 = interleaving between 2 controllers
  536. */
  537. popts->memctl_interleaving = 0;
  538. /*
  539. * 0 = cacheline
  540. * 1 = page
  541. * 2 = (logical) bank
  542. * 3 = superbank (only if CS interleaving is enabled)
  543. */
  544. popts->memctl_interleaving_mode = 0;
  545. /*
  546. * 0: cacheline: bit 30 of the 36-bit physical addr selects the memctl
  547. * 1: page: bit to the left of the column bits selects the memctl
  548. * 2: bank: bit to the left of the bank bits selects the memctl
  549. * 3: superbank: bit to the left of the chip select selects the memctl
  550. *
  551. * NOTE: ba_intlv (rank interleaving) is independent of memory
  552. * controller interleaving; it is only within a memory controller.
  553. * Must use superbank interleaving if rank interleaving is used and
  554. * memory controller interleaving is enabled.
  555. */
  556. /*
  557. * 0 = no
  558. * 0x40 = CS0,CS1
  559. * 0x20 = CS2,CS3
  560. * 0x60 = CS0,CS1 + CS2,CS3
  561. * 0x04 = CS0,CS1,CS2,CS3
  562. */
  563. popts->ba_intlv_ctl = 0;
  564. /* Memory Organization Parameters */
  565. popts->registered_dimm_en = all_DIMMs_registered;
  566. /* Operational Mode Paramters */
  567. /* Pick ECC modes */
  568. popts->ECC_mode = 0; /* 0 = disabled, 1 = enabled */
  569. #ifdef CONFIG_DDR_ECC
  570. if (hwconfig_sub_f("fsl_ddr", "ecc", buf)) {
  571. if (hwconfig_subarg_cmp_f("fsl_ddr", "ecc", "on", buf))
  572. popts->ECC_mode = 1;
  573. } else
  574. popts->ECC_mode = 1;
  575. #endif
  576. popts->ECC_init_using_memctl = 1; /* 0 = use DMA, 1 = use memctl */
  577. /*
  578. * Choose DQS config
  579. * 0 for DDR1
  580. * 1 for DDR2
  581. */
  582. #if defined(CONFIG_FSL_DDR1)
  583. popts->DQS_config = 0;
  584. #elif defined(CONFIG_FSL_DDR2) || defined(CONFIG_FSL_DDR3)
  585. popts->DQS_config = 1;
  586. #endif
  587. /* Choose self-refresh during sleep. */
  588. popts->self_refresh_in_sleep = 1;
  589. /* Choose dynamic power management mode. */
  590. popts->dynamic_power = 0;
  591. /*
  592. * check first dimm for primary sdram width
  593. * presuming all dimms are similar
  594. * 0 = 64-bit, 1 = 32-bit, 2 = 16-bit
  595. */
  596. #if defined(CONFIG_FSL_DDR1) || defined(CONFIG_FSL_DDR2)
  597. if (pdimm[0].n_ranks != 0) {
  598. if ((pdimm[0].data_width >= 64) && \
  599. (pdimm[0].data_width <= 72))
  600. popts->data_bus_width = 0;
  601. else if ((pdimm[0].data_width >= 32) || \
  602. (pdimm[0].data_width <= 40))
  603. popts->data_bus_width = 1;
  604. else {
  605. panic("Error: data width %u is invalid!\n",
  606. pdimm[0].data_width);
  607. }
  608. }
  609. #else
  610. if (pdimm[0].n_ranks != 0) {
  611. if (pdimm[0].primary_sdram_width == 64)
  612. popts->data_bus_width = 0;
  613. else if (pdimm[0].primary_sdram_width == 32)
  614. popts->data_bus_width = 1;
  615. else if (pdimm[0].primary_sdram_width == 16)
  616. popts->data_bus_width = 2;
  617. else {
  618. panic("Error: primary sdram width %u is invalid!\n",
  619. pdimm[0].primary_sdram_width);
  620. }
  621. }
  622. #endif
  623. /* Choose burst length. */
  624. #if defined(CONFIG_FSL_DDR3)
  625. #if defined(CONFIG_E500MC)
  626. popts->OTF_burst_chop_en = 0; /* on-the-fly burst chop disable */
  627. popts->burst_length = DDR_BL8; /* Fixed 8-beat burst len */
  628. #else
  629. if ((popts->data_bus_width == 1) || (popts->data_bus_width == 2)) {
  630. /* 32-bit or 16-bit bus */
  631. popts->OTF_burst_chop_en = 0;
  632. popts->burst_length = DDR_BL8;
  633. } else {
  634. popts->OTF_burst_chop_en = 1; /* on-the-fly burst chop */
  635. popts->burst_length = DDR_OTF; /* on-the-fly BC4 and BL8 */
  636. }
  637. #endif
  638. #else
  639. popts->burst_length = DDR_BL4; /* has to be 4 for DDR2 */
  640. #endif
  641. /* Choose ddr controller address mirror mode */
  642. #if defined(CONFIG_FSL_DDR3)
  643. popts->mirrored_dimm = pdimm[0].mirrored_dimm;
  644. #endif
  645. /* Global Timing Parameters. */
  646. debug("mclk_ps = %u ps\n", get_memory_clk_period_ps());
  647. /* Pick a caslat override. */
  648. popts->cas_latency_override = 0;
  649. popts->cas_latency_override_value = 3;
  650. if (popts->cas_latency_override) {
  651. debug("using caslat override value = %u\n",
  652. popts->cas_latency_override_value);
  653. }
  654. /* Decide whether to use the computed derated latency */
  655. popts->use_derated_caslat = 0;
  656. /* Choose an additive latency. */
  657. popts->additive_latency_override = 0;
  658. popts->additive_latency_override_value = 3;
  659. if (popts->additive_latency_override) {
  660. debug("using additive latency override value = %u\n",
  661. popts->additive_latency_override_value);
  662. }
  663. /*
  664. * 2T_EN setting
  665. *
  666. * Factors to consider for 2T_EN:
  667. * - number of DIMMs installed
  668. * - number of components, number of active ranks
  669. * - how much time you want to spend playing around
  670. */
  671. popts->twoT_en = 0;
  672. popts->threeT_en = 0;
  673. /* for RDIMM, address parity enable */
  674. popts->ap_en = 1;
  675. /*
  676. * BSTTOPRE precharge interval
  677. *
  678. * Set this to 0 for global auto precharge
  679. *
  680. * FIXME: Should this be configured in picoseconds?
  681. * Why it should be in ps: better understanding of this
  682. * relative to actual DRAM timing parameters such as tRAS.
  683. * e.g. tRAS(min) = 40 ns
  684. */
  685. popts->bstopre = 0x100;
  686. /* Minimum CKE pulse width -- tCKE(MIN) */
  687. popts->tCKE_clock_pulse_width_ps
  688. = mclk_to_picos(FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR);
  689. /*
  690. * Window for four activates -- tFAW
  691. *
  692. * FIXME: UM: applies only to DDR2/DDR3 with eight logical banks only
  693. * FIXME: varies depending upon number of column addresses or data
  694. * FIXME: width, was considering looking at pdimm->primary_sdram_width
  695. */
  696. #if defined(CONFIG_FSL_DDR1)
  697. popts->tFAW_window_four_activates_ps = mclk_to_picos(1);
  698. #elif defined(CONFIG_FSL_DDR2)
  699. /*
  700. * x4/x8; some datasheets have 35000
  701. * x16 wide columns only? Use 50000?
  702. */
  703. popts->tFAW_window_four_activates_ps = 37500;
  704. #elif defined(CONFIG_FSL_DDR3)
  705. popts->tFAW_window_four_activates_ps = pdimm[0].tFAW_ps;
  706. #endif
  707. popts->zq_en = 0;
  708. popts->wrlvl_en = 0;
  709. #if defined(CONFIG_FSL_DDR3)
  710. /*
  711. * due to ddr3 dimm is fly-by topology
  712. * we suggest to enable write leveling to
  713. * meet the tQDSS under different loading.
  714. */
  715. popts->wrlvl_en = 1;
  716. popts->zq_en = 1;
  717. popts->wrlvl_override = 0;
  718. #endif
  719. /*
  720. * Check interleaving configuration from environment.
  721. * Please refer to doc/README.fsl-ddr for the detail.
  722. *
  723. * If memory controller interleaving is enabled, then the data
  724. * bus widths must be programmed identically for all memory controllers.
  725. *
  726. * XXX: Attempt to set all controllers to the same chip select
  727. * interleaving mode. It will do a best effort to get the
  728. * requested ranks interleaved together such that the result
  729. * should be a subset of the requested configuration.
  730. */
  731. #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
  732. if (!hwconfig_sub_f("fsl_ddr", "ctlr_intlv", buf))
  733. goto done;
  734. if (pdimm[0].n_ranks == 0) {
  735. printf("There is no rank on CS0 for controller %d.\n", ctrl_num);
  736. popts->memctl_interleaving = 0;
  737. goto done;
  738. }
  739. popts->memctl_interleaving = 1;
  740. /*
  741. * test null first. if CONFIG_HWCONFIG is not defined
  742. * hwconfig_arg_cmp returns non-zero
  743. */
  744. if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
  745. "null", buf)) {
  746. popts->memctl_interleaving = 0;
  747. debug("memory controller interleaving disabled.\n");
  748. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  749. "ctlr_intlv",
  750. "cacheline", buf)) {
  751. popts->memctl_interleaving_mode =
  752. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  753. 0 : FSL_DDR_CACHE_LINE_INTERLEAVING;
  754. popts->memctl_interleaving =
  755. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  756. 0 : 1;
  757. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  758. "ctlr_intlv",
  759. "page", buf)) {
  760. popts->memctl_interleaving_mode =
  761. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  762. 0 : FSL_DDR_PAGE_INTERLEAVING;
  763. popts->memctl_interleaving =
  764. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  765. 0 : 1;
  766. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  767. "ctlr_intlv",
  768. "bank", buf)) {
  769. popts->memctl_interleaving_mode =
  770. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  771. 0 : FSL_DDR_BANK_INTERLEAVING;
  772. popts->memctl_interleaving =
  773. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  774. 0 : 1;
  775. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  776. "ctlr_intlv",
  777. "superbank", buf)) {
  778. popts->memctl_interleaving_mode =
  779. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  780. 0 : FSL_DDR_SUPERBANK_INTERLEAVING;
  781. popts->memctl_interleaving =
  782. ((CONFIG_NUM_DDR_CONTROLLERS == 3) && ctrl_num == 2) ?
  783. 0 : 1;
  784. #if (CONFIG_NUM_DDR_CONTROLLERS == 3)
  785. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  786. "ctlr_intlv",
  787. "3way_1KB", buf)) {
  788. popts->memctl_interleaving_mode =
  789. FSL_DDR_3WAY_1KB_INTERLEAVING;
  790. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  791. "ctlr_intlv",
  792. "3way_4KB", buf)) {
  793. popts->memctl_interleaving_mode =
  794. FSL_DDR_3WAY_4KB_INTERLEAVING;
  795. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  796. "ctlr_intlv",
  797. "3way_8KB", buf)) {
  798. popts->memctl_interleaving_mode =
  799. FSL_DDR_3WAY_8KB_INTERLEAVING;
  800. #elif (CONFIG_NUM_DDR_CONTROLLERS == 4)
  801. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  802. "ctlr_intlv",
  803. "4way_1KB", buf)) {
  804. popts->memctl_interleaving_mode =
  805. FSL_DDR_4WAY_1KB_INTERLEAVING;
  806. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  807. "ctlr_intlv",
  808. "4way_4KB", buf)) {
  809. popts->memctl_interleaving_mode =
  810. FSL_DDR_4WAY_4KB_INTERLEAVING;
  811. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  812. "ctlr_intlv",
  813. "4way_8KB", buf)) {
  814. popts->memctl_interleaving_mode =
  815. FSL_DDR_4WAY_8KB_INTERLEAVING;
  816. #endif
  817. } else {
  818. popts->memctl_interleaving = 0;
  819. printf("hwconfig has unrecognized parameter for ctlr_intlv.\n");
  820. }
  821. done:
  822. #endif
  823. if ((hwconfig_sub_f("fsl_ddr", "bank_intlv", buf)) &&
  824. (CONFIG_CHIP_SELECTS_PER_CTRL > 1)) {
  825. /* test null first. if CONFIG_HWCONFIG is not defined,
  826. * hwconfig_subarg_cmp_f returns non-zero */
  827. if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  828. "null", buf))
  829. debug("bank interleaving disabled.\n");
  830. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  831. "cs0_cs1", buf))
  832. popts->ba_intlv_ctl = FSL_DDR_CS0_CS1;
  833. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  834. "cs2_cs3", buf))
  835. popts->ba_intlv_ctl = FSL_DDR_CS2_CS3;
  836. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  837. "cs0_cs1_and_cs2_cs3", buf))
  838. popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_AND_CS2_CS3;
  839. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  840. "cs0_cs1_cs2_cs3", buf))
  841. popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_CS2_CS3;
  842. else
  843. printf("hwconfig has unrecognized parameter for bank_intlv.\n");
  844. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  845. case FSL_DDR_CS0_CS1_CS2_CS3:
  846. #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  847. if (pdimm[0].n_ranks < 4) {
  848. popts->ba_intlv_ctl = 0;
  849. printf("Not enough bank(chip-select) for "
  850. "CS0+CS1+CS2+CS3 on controller %d, "
  851. "interleaving disabled!\n", ctrl_num);
  852. }
  853. #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  854. if ((pdimm[0].n_ranks < 2) && (pdimm[1].n_ranks < 2)) {
  855. popts->ba_intlv_ctl = 0;
  856. printf("Not enough bank(chip-select) for "
  857. "CS0+CS1+CS2+CS3 on controller %d, "
  858. "interleaving disabled!\n", ctrl_num);
  859. }
  860. if (pdimm[0].capacity != pdimm[1].capacity) {
  861. popts->ba_intlv_ctl = 0;
  862. printf("Not identical DIMM size for "
  863. "CS0+CS1+CS2+CS3 on controller %d, "
  864. "interleaving disabled!\n", ctrl_num);
  865. }
  866. #endif
  867. break;
  868. case FSL_DDR_CS0_CS1:
  869. if (pdimm[0].n_ranks < 2) {
  870. popts->ba_intlv_ctl = 0;
  871. printf("Not enough bank(chip-select) for "
  872. "CS0+CS1 on controller %d, "
  873. "interleaving disabled!\n", ctrl_num);
  874. }
  875. break;
  876. case FSL_DDR_CS2_CS3:
  877. #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  878. if (pdimm[0].n_ranks < 4) {
  879. popts->ba_intlv_ctl = 0;
  880. printf("Not enough bank(chip-select) for CS2+CS3 "
  881. "on controller %d, interleaving disabled!\n", ctrl_num);
  882. }
  883. #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  884. if (pdimm[1].n_ranks < 2) {
  885. popts->ba_intlv_ctl = 0;
  886. printf("Not enough bank(chip-select) for CS2+CS3 "
  887. "on controller %d, interleaving disabled!\n", ctrl_num);
  888. }
  889. #endif
  890. break;
  891. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  892. #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  893. if (pdimm[0].n_ranks < 4) {
  894. popts->ba_intlv_ctl = 0;
  895. printf("Not enough bank(CS) for CS0+CS1 and "
  896. "CS2+CS3 on controller %d, "
  897. "interleaving disabled!\n", ctrl_num);
  898. }
  899. #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  900. if ((pdimm[0].n_ranks < 2) || (pdimm[1].n_ranks < 2)) {
  901. popts->ba_intlv_ctl = 0;
  902. printf("Not enough bank(CS) for CS0+CS1 and "
  903. "CS2+CS3 on controller %d, "
  904. "interleaving disabled!\n", ctrl_num);
  905. }
  906. #endif
  907. break;
  908. default:
  909. popts->ba_intlv_ctl = 0;
  910. break;
  911. }
  912. }
  913. if (hwconfig_sub_f("fsl_ddr", "addr_hash", buf)) {
  914. if (hwconfig_subarg_cmp_f("fsl_ddr", "addr_hash", "null", buf))
  915. popts->addr_hash = 0;
  916. else if (hwconfig_subarg_cmp_f("fsl_ddr", "addr_hash",
  917. "true", buf))
  918. popts->addr_hash = 1;
  919. }
  920. if (pdimm[0].n_ranks == 4)
  921. popts->quad_rank_present = 1;
  922. ddr_freq = get_ddr_freq(0) / 1000000;
  923. if (popts->registered_dimm_en) {
  924. popts->rcw_override = 1;
  925. popts->rcw_1 = 0x000a5a00;
  926. if (ddr_freq <= 800)
  927. popts->rcw_2 = 0x00000000;
  928. else if (ddr_freq <= 1066)
  929. popts->rcw_2 = 0x00100000;
  930. else if (ddr_freq <= 1333)
  931. popts->rcw_2 = 0x00200000;
  932. else
  933. popts->rcw_2 = 0x00300000;
  934. }
  935. fsl_ddr_board_options(popts, pdimm, ctrl_num);
  936. return 0;
  937. }
  938. void check_interleaving_options(fsl_ddr_info_t *pinfo)
  939. {
  940. int i, j, k, check_n_ranks, intlv_invalid = 0;
  941. unsigned int check_intlv, check_n_row_addr, check_n_col_addr;
  942. unsigned long long check_rank_density;
  943. struct dimm_params_s *dimm;
  944. /*
  945. * Check if all controllers are configured for memory
  946. * controller interleaving. Identical dimms are recommended. At least
  947. * the size, row and col address should be checked.
  948. */
  949. j = 0;
  950. check_n_ranks = pinfo->dimm_params[0][0].n_ranks;
  951. check_rank_density = pinfo->dimm_params[0][0].rank_density;
  952. check_n_row_addr = pinfo->dimm_params[0][0].n_row_addr;
  953. check_n_col_addr = pinfo->dimm_params[0][0].n_col_addr;
  954. check_intlv = pinfo->memctl_opts[0].memctl_interleaving_mode;
  955. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  956. dimm = &pinfo->dimm_params[i][0];
  957. if (!pinfo->memctl_opts[i].memctl_interleaving) {
  958. continue;
  959. } else if (((check_rank_density != dimm->rank_density) ||
  960. (check_n_ranks != dimm->n_ranks) ||
  961. (check_n_row_addr != dimm->n_row_addr) ||
  962. (check_n_col_addr != dimm->n_col_addr) ||
  963. (check_intlv !=
  964. pinfo->memctl_opts[i].memctl_interleaving_mode))){
  965. intlv_invalid = 1;
  966. break;
  967. } else {
  968. j++;
  969. }
  970. }
  971. if (intlv_invalid) {
  972. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  973. pinfo->memctl_opts[i].memctl_interleaving = 0;
  974. printf("Not all DIMMs are identical. "
  975. "Memory controller interleaving disabled.\n");
  976. } else {
  977. switch (check_intlv) {
  978. case FSL_DDR_CACHE_LINE_INTERLEAVING:
  979. case FSL_DDR_PAGE_INTERLEAVING:
  980. case FSL_DDR_BANK_INTERLEAVING:
  981. case FSL_DDR_SUPERBANK_INTERLEAVING:
  982. if (3 == CONFIG_NUM_DDR_CONTROLLERS)
  983. k = 2;
  984. else
  985. k = CONFIG_NUM_DDR_CONTROLLERS;
  986. break;
  987. case FSL_DDR_3WAY_1KB_INTERLEAVING:
  988. case FSL_DDR_3WAY_4KB_INTERLEAVING:
  989. case FSL_DDR_3WAY_8KB_INTERLEAVING:
  990. case FSL_DDR_4WAY_1KB_INTERLEAVING:
  991. case FSL_DDR_4WAY_4KB_INTERLEAVING:
  992. case FSL_DDR_4WAY_8KB_INTERLEAVING:
  993. default:
  994. k = CONFIG_NUM_DDR_CONTROLLERS;
  995. break;
  996. }
  997. debug("%d of %d controllers are interleaving.\n", j, k);
  998. if (j != k) {
  999. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  1000. pinfo->memctl_opts[i].memctl_interleaving = 0;
  1001. printf("Not all controllers have compatible "
  1002. "interleaving mode. All disabled.\n");
  1003. }
  1004. }
  1005. debug("Checking interleaving options completed\n");
  1006. }
  1007. int fsl_use_spd(void)
  1008. {
  1009. int use_spd = 0;
  1010. #ifdef CONFIG_DDR_SPD
  1011. char buffer[HWCONFIG_BUFFER_SIZE];
  1012. char *buf = NULL;
  1013. /*
  1014. * Extract hwconfig from environment since we have not properly setup
  1015. * the environment but need it for ddr config params
  1016. */
  1017. if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
  1018. buf = buffer;
  1019. /* if hwconfig is not enabled, or "sdram" is not defined, use spd */
  1020. if (hwconfig_sub_f("fsl_ddr", "sdram", buf)) {
  1021. if (hwconfig_subarg_cmp_f("fsl_ddr", "sdram", "spd", buf))
  1022. use_spd = 1;
  1023. else if (hwconfig_subarg_cmp_f("fsl_ddr", "sdram",
  1024. "fixed", buf))
  1025. use_spd = 0;
  1026. else
  1027. use_spd = 1;
  1028. } else
  1029. use_spd = 1;
  1030. #endif
  1031. return use_spd;
  1032. }