options.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2008, 2010-2011 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. unsigned int populate_memctl_options(int all_DIMMs_registered,
  25. memctl_options_t *popts,
  26. dimm_params_t *pdimm,
  27. unsigned int ctrl_num)
  28. {
  29. unsigned int i;
  30. char buffer[HWCONFIG_BUFFER_SIZE];
  31. char *buf = NULL;
  32. /*
  33. * Extract hwconfig from environment since we have not properly setup
  34. * the environment but need it for ddr config params
  35. */
  36. if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
  37. buf = buffer;
  38. /* Chip select options. */
  39. /* Pick chip-select local options. */
  40. for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
  41. /* If not DDR2, odt_rd_cfg and odt_wr_cfg need to be 0. */
  42. /* only for single CS? */
  43. popts->cs_local_opts[i].odt_rd_cfg = 0;
  44. popts->cs_local_opts[i].odt_wr_cfg = 1;
  45. popts->cs_local_opts[i].auto_precharge = 0;
  46. }
  47. /* Pick interleaving mode. */
  48. /*
  49. * 0 = no interleaving
  50. * 1 = interleaving between 2 controllers
  51. */
  52. popts->memctl_interleaving = 0;
  53. /*
  54. * 0 = cacheline
  55. * 1 = page
  56. * 2 = (logical) bank
  57. * 3 = superbank (only if CS interleaving is enabled)
  58. */
  59. popts->memctl_interleaving_mode = 0;
  60. /*
  61. * 0: cacheline: bit 30 of the 36-bit physical addr selects the memctl
  62. * 1: page: bit to the left of the column bits selects the memctl
  63. * 2: bank: bit to the left of the bank bits selects the memctl
  64. * 3: superbank: bit to the left of the chip select selects the memctl
  65. *
  66. * NOTE: ba_intlv (rank interleaving) is independent of memory
  67. * controller interleaving; it is only within a memory controller.
  68. * Must use superbank interleaving if rank interleaving is used and
  69. * memory controller interleaving is enabled.
  70. */
  71. /*
  72. * 0 = no
  73. * 0x40 = CS0,CS1
  74. * 0x20 = CS2,CS3
  75. * 0x60 = CS0,CS1 + CS2,CS3
  76. * 0x04 = CS0,CS1,CS2,CS3
  77. */
  78. popts->ba_intlv_ctl = 0;
  79. /* Memory Organization Parameters */
  80. popts->registered_dimm_en = all_DIMMs_registered;
  81. /* Operational Mode Paramters */
  82. /* Pick ECC modes */
  83. #ifdef CONFIG_DDR_ECC
  84. popts->ECC_mode = 1; /* 0 = disabled, 1 = enabled */
  85. #else
  86. popts->ECC_mode = 0; /* 0 = disabled, 1 = enabled */
  87. #endif
  88. popts->ECC_init_using_memctl = 1; /* 0 = use DMA, 1 = use memctl */
  89. /*
  90. * Choose DQS config
  91. * 0 for DDR1
  92. * 1 for DDR2
  93. */
  94. #if defined(CONFIG_FSL_DDR1)
  95. popts->DQS_config = 0;
  96. #elif defined(CONFIG_FSL_DDR2) || defined(CONFIG_FSL_DDR3)
  97. popts->DQS_config = 1;
  98. #endif
  99. /* Choose self-refresh during sleep. */
  100. popts->self_refresh_in_sleep = 1;
  101. /* Choose dynamic power management mode. */
  102. popts->dynamic_power = 0;
  103. /* 0 = 64-bit, 1 = 32-bit, 2 = 16-bit */
  104. popts->data_bus_width = 0;
  105. /* Choose burst length. */
  106. #if defined(CONFIG_FSL_DDR3)
  107. #if defined(CONFIG_E500MC)
  108. popts->OTF_burst_chop_en = 0; /* on-the-fly burst chop disable */
  109. popts->burst_length = DDR_BL8; /* Fixed 8-beat burst len */
  110. #else
  111. popts->OTF_burst_chop_en = 1; /* on-the-fly burst chop */
  112. popts->burst_length = DDR_OTF; /* on-the-fly BC4 and BL8 */
  113. #endif
  114. #else
  115. popts->burst_length = DDR_BL4; /* has to be 4 for DDR2 */
  116. #endif
  117. /* Choose ddr controller address mirror mode */
  118. #if defined(CONFIG_FSL_DDR3)
  119. popts->mirrored_dimm = pdimm[0].mirrored_dimm;
  120. #endif
  121. /* Global Timing Parameters. */
  122. debug("mclk_ps = %u ps\n", get_memory_clk_period_ps());
  123. /* Pick a caslat override. */
  124. popts->cas_latency_override = 0;
  125. popts->cas_latency_override_value = 3;
  126. if (popts->cas_latency_override) {
  127. debug("using caslat override value = %u\n",
  128. popts->cas_latency_override_value);
  129. }
  130. /* Decide whether to use the computed derated latency */
  131. popts->use_derated_caslat = 0;
  132. /* Choose an additive latency. */
  133. popts->additive_latency_override = 0;
  134. popts->additive_latency_override_value = 3;
  135. if (popts->additive_latency_override) {
  136. debug("using additive latency override value = %u\n",
  137. popts->additive_latency_override_value);
  138. }
  139. /*
  140. * 2T_EN setting
  141. *
  142. * Factors to consider for 2T_EN:
  143. * - number of DIMMs installed
  144. * - number of components, number of active ranks
  145. * - how much time you want to spend playing around
  146. */
  147. popts->twoT_en = 0;
  148. popts->threeT_en = 0;
  149. /*
  150. * BSTTOPRE precharge interval
  151. *
  152. * Set this to 0 for global auto precharge
  153. *
  154. * FIXME: Should this be configured in picoseconds?
  155. * Why it should be in ps: better understanding of this
  156. * relative to actual DRAM timing parameters such as tRAS.
  157. * e.g. tRAS(min) = 40 ns
  158. */
  159. popts->bstopre = 0x100;
  160. /* Minimum CKE pulse width -- tCKE(MIN) */
  161. popts->tCKE_clock_pulse_width_ps
  162. = mclk_to_picos(FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR);
  163. /*
  164. * Window for four activates -- tFAW
  165. *
  166. * FIXME: UM: applies only to DDR2/DDR3 with eight logical banks only
  167. * FIXME: varies depending upon number of column addresses or data
  168. * FIXME: width, was considering looking at pdimm->primary_sdram_width
  169. */
  170. #if defined(CONFIG_FSL_DDR1)
  171. popts->tFAW_window_four_activates_ps = mclk_to_picos(1);
  172. #elif defined(CONFIG_FSL_DDR2)
  173. /*
  174. * x4/x8; some datasheets have 35000
  175. * x16 wide columns only? Use 50000?
  176. */
  177. popts->tFAW_window_four_activates_ps = 37500;
  178. #elif defined(CONFIG_FSL_DDR3)
  179. popts->tFAW_window_four_activates_ps = pdimm[0].tFAW_ps;
  180. #endif
  181. popts->zq_en = 0;
  182. popts->wrlvl_en = 0;
  183. #if defined(CONFIG_FSL_DDR3)
  184. /*
  185. * due to ddr3 dimm is fly-by topology
  186. * we suggest to enable write leveling to
  187. * meet the tQDSS under different loading.
  188. */
  189. popts->wrlvl_en = 1;
  190. popts->zq_en = 1;
  191. popts->wrlvl_override = 0;
  192. #endif
  193. /*
  194. * Check interleaving configuration from environment.
  195. * Please refer to doc/README.fsl-ddr for the detail.
  196. *
  197. * If memory controller interleaving is enabled, then the data
  198. * bus widths must be programmed identically for all memory controllers.
  199. *
  200. * XXX: Attempt to set all controllers to the same chip select
  201. * interleaving mode. It will do a best effort to get the
  202. * requested ranks interleaved together such that the result
  203. * should be a subset of the requested configuration.
  204. */
  205. #if (CONFIG_NUM_DDR_CONTROLLERS > 1)
  206. if (hwconfig_sub_f("fsl_ddr", "ctlr_intlv", buf)) {
  207. if (pdimm[0].n_ranks == 0) {
  208. printf("There is no rank on CS0 for controller %d. Because only"
  209. " rank on CS0 and ranks chip-select interleaved with CS0"
  210. " are controller interleaved, force non memory "
  211. "controller interleaving\n", ctrl_num);
  212. popts->memctl_interleaving = 0;
  213. } else {
  214. popts->memctl_interleaving = 1;
  215. /*
  216. * test null first. if CONFIG_HWCONFIG is not defined
  217. * hwconfig_arg_cmp returns non-zero
  218. */
  219. if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
  220. "null", buf)) {
  221. popts->memctl_interleaving = 0;
  222. debug("memory controller interleaving disabled.\n");
  223. } else if (hwconfig_subarg_cmp_f("fsl_ddr",
  224. "ctlr_intlv",
  225. "cacheline", buf))
  226. popts->memctl_interleaving_mode =
  227. FSL_DDR_CACHE_LINE_INTERLEAVING;
  228. else if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
  229. "page", buf))
  230. popts->memctl_interleaving_mode =
  231. FSL_DDR_PAGE_INTERLEAVING;
  232. else if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
  233. "bank", buf))
  234. popts->memctl_interleaving_mode =
  235. FSL_DDR_BANK_INTERLEAVING;
  236. else if (hwconfig_subarg_cmp_f("fsl_ddr", "ctlr_intlv",
  237. "superbank", buf))
  238. popts->memctl_interleaving_mode =
  239. FSL_DDR_SUPERBANK_INTERLEAVING;
  240. else {
  241. popts->memctl_interleaving = 0;
  242. printf("hwconfig has unrecognized parameter for ctlr_intlv.\n");
  243. }
  244. }
  245. }
  246. #endif
  247. if ((hwconfig_sub_f("fsl_ddr", "bank_intlv", buf)) &&
  248. (CONFIG_CHIP_SELECTS_PER_CTRL > 1)) {
  249. /* test null first. if CONFIG_HWCONFIG is not defined,
  250. * hwconfig_subarg_cmp_f returns non-zero */
  251. if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  252. "null", buf))
  253. debug("bank interleaving disabled.\n");
  254. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  255. "cs0_cs1", buf))
  256. popts->ba_intlv_ctl = FSL_DDR_CS0_CS1;
  257. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  258. "cs2_cs3", buf))
  259. popts->ba_intlv_ctl = FSL_DDR_CS2_CS3;
  260. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  261. "cs0_cs1_and_cs2_cs3", buf))
  262. popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_AND_CS2_CS3;
  263. else if (hwconfig_subarg_cmp_f("fsl_ddr", "bank_intlv",
  264. "cs0_cs1_cs2_cs3", buf))
  265. popts->ba_intlv_ctl = FSL_DDR_CS0_CS1_CS2_CS3;
  266. else
  267. printf("hwconfig has unrecognized parameter for bank_intlv.\n");
  268. switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
  269. case FSL_DDR_CS0_CS1_CS2_CS3:
  270. #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  271. if (pdimm[0].n_ranks < 4) {
  272. popts->ba_intlv_ctl = 0;
  273. printf("Not enough bank(chip-select) for "
  274. "CS0+CS1+CS2+CS3 on controller %d, "
  275. "force non-interleaving!\n", ctrl_num);
  276. }
  277. #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  278. if ((pdimm[0].n_ranks < 2) && (pdimm[1].n_ranks < 2)) {
  279. popts->ba_intlv_ctl = 0;
  280. printf("Not enough bank(chip-select) for "
  281. "CS0+CS1+CS2+CS3 on controller %d, "
  282. "force non-interleaving!\n", ctrl_num);
  283. }
  284. if (pdimm[0].capacity != pdimm[1].capacity) {
  285. popts->ba_intlv_ctl = 0;
  286. printf("Not identical DIMM size for "
  287. "CS0+CS1+CS2+CS3 on controller %d, "
  288. "force non-interleaving!\n", ctrl_num);
  289. }
  290. #endif
  291. break;
  292. case FSL_DDR_CS0_CS1:
  293. if (pdimm[0].n_ranks < 2) {
  294. popts->ba_intlv_ctl = 0;
  295. printf("Not enough bank(chip-select) for "
  296. "CS0+CS1 on controller %d, "
  297. "force non-interleaving!\n", ctrl_num);
  298. }
  299. break;
  300. case FSL_DDR_CS2_CS3:
  301. #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  302. if (pdimm[0].n_ranks < 4) {
  303. popts->ba_intlv_ctl = 0;
  304. printf("Not enough bank(chip-select) for CS2+CS3 "
  305. "on controller %d, force non-interleaving!\n", ctrl_num);
  306. }
  307. #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  308. if (pdimm[1].n_ranks < 2) {
  309. popts->ba_intlv_ctl = 0;
  310. printf("Not enough bank(chip-select) for CS2+CS3 "
  311. "on controller %d, force non-interleaving!\n", ctrl_num);
  312. }
  313. #endif
  314. break;
  315. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  316. #if (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  317. if (pdimm[0].n_ranks < 4) {
  318. popts->ba_intlv_ctl = 0;
  319. printf("Not enough bank(CS) for CS0+CS1 and "
  320. "CS2+CS3 on controller %d, "
  321. "force non-interleaving!\n", ctrl_num);
  322. }
  323. #elif (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  324. if ((pdimm[0].n_ranks < 2) || (pdimm[1].n_ranks < 2)) {
  325. popts->ba_intlv_ctl = 0;
  326. printf("Not enough bank(CS) for CS0+CS1 and "
  327. "CS2+CS3 on controller %d, "
  328. "force non-interleaving!\n", ctrl_num);
  329. }
  330. #endif
  331. break;
  332. default:
  333. popts->ba_intlv_ctl = 0;
  334. break;
  335. }
  336. }
  337. if (hwconfig_sub_f("fsl_ddr", "addr_hash", buf)) {
  338. if (hwconfig_subarg_cmp_f("fsl_ddr", "addr_hash", "null", buf))
  339. popts->addr_hash = 0;
  340. else if (hwconfig_subarg_cmp_f("fsl_ddr", "addr_hash",
  341. "true", buf))
  342. popts->addr_hash = 1;
  343. }
  344. if (pdimm[0].n_ranks == 4)
  345. popts->quad_rank_present = 1;
  346. fsl_ddr_board_options(popts, pdimm, ctrl_num);
  347. return 0;
  348. }
  349. void check_interleaving_options(fsl_ddr_info_t *pinfo)
  350. {
  351. int i, j, check_n_ranks, intlv_fixed = 0;
  352. unsigned long long check_rank_density;
  353. /*
  354. * Check if all controllers are configured for memory
  355. * controller interleaving. Identical dimms are recommended. At least
  356. * the size should be checked.
  357. */
  358. j = 0;
  359. check_n_ranks = pinfo->dimm_params[0][0].n_ranks;
  360. check_rank_density = pinfo->dimm_params[0][0].rank_density;
  361. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  362. if ((pinfo->memctl_opts[i].memctl_interleaving) && \
  363. (check_rank_density == pinfo->dimm_params[i][0].rank_density) && \
  364. (check_n_ranks == pinfo->dimm_params[i][0].n_ranks)) {
  365. j++;
  366. }
  367. }
  368. if (j != CONFIG_NUM_DDR_CONTROLLERS) {
  369. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  370. if (pinfo->memctl_opts[i].memctl_interleaving) {
  371. pinfo->memctl_opts[i].memctl_interleaving = 0;
  372. intlv_fixed = 1;
  373. }
  374. if (intlv_fixed)
  375. printf("Not all DIMMs are identical in size. "
  376. "Memory controller interleaving disabled.\n");
  377. }
  378. }
  379. int fsl_use_spd(void)
  380. {
  381. int use_spd = 0;
  382. #ifdef CONFIG_DDR_SPD
  383. char buffer[HWCONFIG_BUFFER_SIZE];
  384. char *buf = NULL;
  385. /*
  386. * Extract hwconfig from environment since we have not properly setup
  387. * the environment but need it for ddr config params
  388. */
  389. if (getenv_f("hwconfig", buffer, sizeof(buffer)) > 0)
  390. buf = buffer;
  391. /* if hwconfig is not enabled, or "sdram" is not defined, use spd */
  392. if (hwconfig_sub_f("fsl_ddr", "sdram", buf)) {
  393. if (hwconfig_subarg_cmp_f("fsl_ddr", "sdram", "spd", buf))
  394. use_spd = 1;
  395. else if (hwconfig_subarg_cmp_f("fsl_ddr", "sdram",
  396. "fixed", buf))
  397. use_spd = 0;
  398. else
  399. use_spd = 1;
  400. } else
  401. use_spd = 1;
  402. #endif
  403. return use_spd;
  404. }