options.c 26 KB

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