main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. /*
  9. * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
  10. * Based on code from spd_sdram.c
  11. * Author: James Yang [at freescale.com]
  12. */
  13. #include <common.h>
  14. #include <i2c.h>
  15. #include <asm/fsl_ddr_sdram.h>
  16. #include <asm/fsl_law.h>
  17. #include "ddr.h"
  18. void fsl_ddr_set_lawbar(
  19. const common_timing_params_t *memctl_common_params,
  20. unsigned int memctl_interleaved,
  21. unsigned int ctrl_num);
  22. void fsl_ddr_set_intl3r(const unsigned int granule_size);
  23. /* processor specific function */
  24. extern void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
  25. unsigned int ctrl_num);
  26. #if defined(SPD_EEPROM_ADDRESS) || \
  27. defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
  28. defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
  29. #if (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  30. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  31. [0][0] = SPD_EEPROM_ADDRESS,
  32. };
  33. #elif (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  34. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  35. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  36. [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
  37. };
  38. #elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  39. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  40. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  41. [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
  42. };
  43. #elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  44. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  45. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  46. [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
  47. [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
  48. [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
  49. };
  50. #elif (CONFIG_NUM_DDR_CONTROLLERS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  51. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  52. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  53. [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
  54. [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
  55. };
  56. #elif (CONFIG_NUM_DDR_CONTROLLERS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  57. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  58. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  59. [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
  60. [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
  61. [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
  62. [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
  63. [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
  64. };
  65. #endif
  66. static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
  67. {
  68. int ret = i2c_read(i2c_address, 0, 1, (uchar *)spd,
  69. sizeof(generic_spd_eeprom_t));
  70. if (ret) {
  71. printf("DDR: failed to read SPD from address %u\n", i2c_address);
  72. memset(spd, 0, sizeof(generic_spd_eeprom_t));
  73. }
  74. }
  75. __attribute__((weak, alias("__get_spd")))
  76. void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
  77. void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
  78. unsigned int ctrl_num)
  79. {
  80. unsigned int i;
  81. unsigned int i2c_address = 0;
  82. if (ctrl_num >= CONFIG_NUM_DDR_CONTROLLERS) {
  83. printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
  84. return;
  85. }
  86. for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
  87. i2c_address = spd_i2c_addr[ctrl_num][i];
  88. get_spd(&(ctrl_dimms_spd[i]), i2c_address);
  89. }
  90. }
  91. #else
  92. void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
  93. unsigned int ctrl_num)
  94. {
  95. }
  96. #endif /* SPD_EEPROM_ADDRESSx */
  97. /*
  98. * ASSUMPTIONS:
  99. * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
  100. * - Same memory data bus width on all controllers
  101. *
  102. * NOTES:
  103. *
  104. * The memory controller and associated documentation use confusing
  105. * terminology when referring to the orgranization of DRAM.
  106. *
  107. * Here is a terminology translation table:
  108. *
  109. * memory controller/documention |industry |this code |signals
  110. * -------------------------------|-----------|-----------|-----------------
  111. * physical bank/bank |rank |rank |chip select (CS)
  112. * logical bank/sub-bank |bank |bank |bank address (BA)
  113. * page/row |row |page |row address
  114. * ??? |column |column |column address
  115. *
  116. * The naming confusion is further exacerbated by the descriptions of the
  117. * memory controller interleaving feature, where accesses are interleaved
  118. * _BETWEEN_ two seperate memory controllers. This is configured only in
  119. * CS0_CONFIG[INTLV_CTL] of each memory controller.
  120. *
  121. * memory controller documentation | number of chip selects
  122. * | per memory controller supported
  123. * --------------------------------|-----------------------------------------
  124. * cache line interleaving | 1 (CS0 only)
  125. * page interleaving | 1 (CS0 only)
  126. * bank interleaving | 1 (CS0 only)
  127. * superbank interleraving | depends on bank (chip select)
  128. * | interleraving [rank interleaving]
  129. * | mode used on every memory controller
  130. *
  131. * Even further confusing is the existence of the interleaving feature
  132. * _WITHIN_ each memory controller. The feature is referred to in
  133. * documentation as chip select interleaving or bank interleaving,
  134. * although it is configured in the DDR_SDRAM_CFG field.
  135. *
  136. * Name of field | documentation name | this code
  137. * -----------------------------|-----------------------|------------------
  138. * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
  139. * | interleaving
  140. */
  141. const char *step_string_tbl[] = {
  142. "STEP_GET_SPD",
  143. "STEP_COMPUTE_DIMM_PARMS",
  144. "STEP_COMPUTE_COMMON_PARMS",
  145. "STEP_GATHER_OPTS",
  146. "STEP_ASSIGN_ADDRESSES",
  147. "STEP_COMPUTE_REGS",
  148. "STEP_PROGRAM_REGS",
  149. "STEP_ALL"
  150. };
  151. const char * step_to_string(unsigned int step) {
  152. unsigned int s = __ilog2(step);
  153. if ((1 << s) != step)
  154. return step_string_tbl[7];
  155. return step_string_tbl[s];
  156. }
  157. unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
  158. unsigned int dbw_cap_adj[])
  159. {
  160. int i, j;
  161. unsigned long long total_mem, current_mem_base, total_ctlr_mem;
  162. unsigned long long rank_density, ctlr_density = 0;
  163. /*
  164. * If a reduced data width is requested, but the SPD
  165. * specifies a physically wider device, adjust the
  166. * computed dimm capacities accordingly before
  167. * assigning addresses.
  168. */
  169. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  170. unsigned int found = 0;
  171. switch (pinfo->memctl_opts[i].data_bus_width) {
  172. case 2:
  173. /* 16-bit */
  174. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  175. unsigned int dw;
  176. if (!pinfo->dimm_params[i][j].n_ranks)
  177. continue;
  178. dw = pinfo->dimm_params[i][j].primary_sdram_width;
  179. if ((dw == 72 || dw == 64)) {
  180. dbw_cap_adj[i] = 2;
  181. break;
  182. } else if ((dw == 40 || dw == 32)) {
  183. dbw_cap_adj[i] = 1;
  184. break;
  185. }
  186. }
  187. break;
  188. case 1:
  189. /* 32-bit */
  190. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  191. unsigned int dw;
  192. dw = pinfo->dimm_params[i][j].data_width;
  193. if (pinfo->dimm_params[i][j].n_ranks
  194. && (dw == 72 || dw == 64)) {
  195. /*
  196. * FIXME: can't really do it
  197. * like this because this just
  198. * further reduces the memory
  199. */
  200. found = 1;
  201. break;
  202. }
  203. }
  204. if (found) {
  205. dbw_cap_adj[i] = 1;
  206. }
  207. break;
  208. case 0:
  209. /* 64-bit */
  210. break;
  211. default:
  212. printf("unexpected data bus width "
  213. "specified controller %u\n", i);
  214. return 1;
  215. }
  216. debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
  217. }
  218. current_mem_base = 0ull;
  219. total_mem = 0;
  220. if (pinfo->memctl_opts[0].memctl_interleaving) {
  221. rank_density = pinfo->dimm_params[0][0].rank_density >>
  222. dbw_cap_adj[0];
  223. switch (pinfo->memctl_opts[0].ba_intlv_ctl &
  224. FSL_DDR_CS0_CS1_CS2_CS3) {
  225. case FSL_DDR_CS0_CS1_CS2_CS3:
  226. ctlr_density = 4 * rank_density;
  227. break;
  228. case FSL_DDR_CS0_CS1:
  229. case FSL_DDR_CS0_CS1_AND_CS2_CS3:
  230. ctlr_density = 2 * rank_density;
  231. break;
  232. case FSL_DDR_CS2_CS3:
  233. default:
  234. ctlr_density = rank_density;
  235. break;
  236. }
  237. debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
  238. rank_density, ctlr_density);
  239. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  240. if (pinfo->memctl_opts[i].memctl_interleaving) {
  241. switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
  242. case FSL_DDR_CACHE_LINE_INTERLEAVING:
  243. case FSL_DDR_PAGE_INTERLEAVING:
  244. case FSL_DDR_BANK_INTERLEAVING:
  245. case FSL_DDR_SUPERBANK_INTERLEAVING:
  246. total_ctlr_mem = 2 * ctlr_density;
  247. break;
  248. case FSL_DDR_3WAY_1KB_INTERLEAVING:
  249. case FSL_DDR_3WAY_4KB_INTERLEAVING:
  250. case FSL_DDR_3WAY_8KB_INTERLEAVING:
  251. total_ctlr_mem = 3 * ctlr_density;
  252. break;
  253. case FSL_DDR_4WAY_1KB_INTERLEAVING:
  254. case FSL_DDR_4WAY_4KB_INTERLEAVING:
  255. case FSL_DDR_4WAY_8KB_INTERLEAVING:
  256. total_ctlr_mem = 4 * ctlr_density;
  257. break;
  258. default:
  259. panic("Unknown interleaving mode");
  260. }
  261. pinfo->common_timing_params[i].base_address =
  262. current_mem_base;
  263. pinfo->common_timing_params[i].total_mem =
  264. total_ctlr_mem;
  265. total_mem = current_mem_base + total_ctlr_mem;
  266. debug("ctrl %d base 0x%llx\n", i, current_mem_base);
  267. debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
  268. } else {
  269. /* when 3rd controller not interleaved */
  270. current_mem_base = total_mem;
  271. total_ctlr_mem = 0;
  272. pinfo->common_timing_params[i].base_address =
  273. current_mem_base;
  274. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  275. unsigned long long cap =
  276. pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
  277. pinfo->dimm_params[i][j].base_address =
  278. current_mem_base;
  279. debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
  280. current_mem_base += cap;
  281. total_ctlr_mem += cap;
  282. }
  283. debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
  284. pinfo->common_timing_params[i].total_mem =
  285. total_ctlr_mem;
  286. total_mem += total_ctlr_mem;
  287. }
  288. }
  289. } else {
  290. /*
  291. * Simple linear assignment if memory
  292. * controllers are not interleaved.
  293. */
  294. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  295. total_ctlr_mem = 0;
  296. pinfo->common_timing_params[i].base_address =
  297. current_mem_base;
  298. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  299. /* Compute DIMM base addresses. */
  300. unsigned long long cap =
  301. pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
  302. pinfo->dimm_params[i][j].base_address =
  303. current_mem_base;
  304. debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
  305. current_mem_base += cap;
  306. total_ctlr_mem += cap;
  307. }
  308. debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
  309. pinfo->common_timing_params[i].total_mem =
  310. total_ctlr_mem;
  311. total_mem += total_ctlr_mem;
  312. }
  313. }
  314. debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
  315. return total_mem;
  316. }
  317. unsigned long long
  318. fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
  319. unsigned int size_only)
  320. {
  321. unsigned int i, j;
  322. unsigned long long total_mem = 0;
  323. fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
  324. common_timing_params_t *timing_params = pinfo->common_timing_params;
  325. /* data bus width capacity adjust shift amount */
  326. unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
  327. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  328. dbw_capacity_adjust[i] = 0;
  329. }
  330. debug("starting at step %u (%s)\n",
  331. start_step, step_to_string(start_step));
  332. switch (start_step) {
  333. case STEP_GET_SPD:
  334. #if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
  335. /* STEP 1: Gather all DIMM SPD data */
  336. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  337. fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
  338. }
  339. case STEP_COMPUTE_DIMM_PARMS:
  340. /* STEP 2: Compute DIMM parameters from SPD data */
  341. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  342. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  343. unsigned int retval;
  344. generic_spd_eeprom_t *spd =
  345. &(pinfo->spd_installed_dimms[i][j]);
  346. dimm_params_t *pdimm =
  347. &(pinfo->dimm_params[i][j]);
  348. retval = compute_dimm_parameters(spd, pdimm, i);
  349. #ifdef CONFIG_SYS_DDR_RAW_TIMING
  350. if (!i && !j && retval) {
  351. printf("SPD error on controller %d! "
  352. "Trying fallback to raw timing "
  353. "calculation\n", i);
  354. fsl_ddr_get_dimm_params(pdimm, i, j);
  355. }
  356. #else
  357. if (retval == 2) {
  358. printf("Error: compute_dimm_parameters"
  359. " non-zero returned FATAL value "
  360. "for memctl=%u dimm=%u\n", i, j);
  361. return 0;
  362. }
  363. #endif
  364. if (retval) {
  365. debug("Warning: compute_dimm_parameters"
  366. " non-zero return value for memctl=%u "
  367. "dimm=%u\n", i, j);
  368. }
  369. }
  370. }
  371. #elif defined(CONFIG_SYS_DDR_RAW_TIMING)
  372. case STEP_COMPUTE_DIMM_PARMS:
  373. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  374. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  375. dimm_params_t *pdimm =
  376. &(pinfo->dimm_params[i][j]);
  377. fsl_ddr_get_dimm_params(pdimm, i, j);
  378. }
  379. }
  380. debug("Filling dimm parameters from board specific file\n");
  381. #endif
  382. case STEP_COMPUTE_COMMON_PARMS:
  383. /*
  384. * STEP 3: Compute a common set of timing parameters
  385. * suitable for all of the DIMMs on each memory controller
  386. */
  387. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  388. debug("Computing lowest common DIMM"
  389. " parameters for memctl=%u\n", i);
  390. compute_lowest_common_dimm_parameters(
  391. pinfo->dimm_params[i],
  392. &timing_params[i],
  393. CONFIG_DIMM_SLOTS_PER_CTLR);
  394. }
  395. case STEP_GATHER_OPTS:
  396. /* STEP 4: Gather configuration requirements from user */
  397. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  398. debug("Reloading memory controller "
  399. "configuration options for memctl=%u\n", i);
  400. /*
  401. * This "reloads" the memory controller options
  402. * to defaults. If the user "edits" an option,
  403. * next_step points to the step after this,
  404. * which is currently STEP_ASSIGN_ADDRESSES.
  405. */
  406. populate_memctl_options(
  407. timing_params[i].all_DIMMs_registered,
  408. &pinfo->memctl_opts[i],
  409. pinfo->dimm_params[i], i);
  410. }
  411. case STEP_ASSIGN_ADDRESSES:
  412. /* STEP 5: Assign addresses to chip selects */
  413. check_interleaving_options(pinfo);
  414. total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
  415. case STEP_COMPUTE_REGS:
  416. /* STEP 6: compute controller register values */
  417. debug("FSL Memory ctrl register computation\n");
  418. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  419. if (timing_params[i].ndimms_present == 0) {
  420. memset(&ddr_reg[i], 0,
  421. sizeof(fsl_ddr_cfg_regs_t));
  422. continue;
  423. }
  424. compute_fsl_memctl_config_regs(
  425. &pinfo->memctl_opts[i],
  426. &ddr_reg[i], &timing_params[i],
  427. pinfo->dimm_params[i],
  428. dbw_capacity_adjust[i],
  429. size_only);
  430. }
  431. default:
  432. break;
  433. }
  434. {
  435. /*
  436. * Compute the amount of memory available just by
  437. * looking for the highest valid CSn_BNDS value.
  438. * This allows us to also experiment with using
  439. * only CS0 when using dual-rank DIMMs.
  440. */
  441. unsigned int max_end = 0;
  442. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  443. for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
  444. fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
  445. if (reg->cs[j].config & 0x80000000) {
  446. unsigned int end;
  447. end = reg->cs[j].bnds & 0xFFF;
  448. if (end > max_end) {
  449. max_end = end;
  450. }
  451. }
  452. }
  453. }
  454. total_mem = 1 + (((unsigned long long)max_end << 24ULL)
  455. | 0xFFFFFFULL);
  456. }
  457. return total_mem;
  458. }
  459. /*
  460. * fsl_ddr_sdram() -- this is the main function to be called by
  461. * initdram() in the board file.
  462. *
  463. * It returns amount of memory configured in bytes.
  464. */
  465. phys_size_t fsl_ddr_sdram(void)
  466. {
  467. unsigned int i;
  468. unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
  469. unsigned long long total_memory;
  470. fsl_ddr_info_t info;
  471. /* Reset info structure. */
  472. memset(&info, 0, sizeof(fsl_ddr_info_t));
  473. /* Compute it once normally. */
  474. #ifdef CONFIG_FSL_DDR_INTERACTIVE
  475. if (getenv("ddr_interactive"))
  476. total_memory = fsl_ddr_interactive(&info);
  477. else
  478. #endif
  479. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
  480. /* Program configuration registers. */
  481. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  482. debug("Programming controller %u\n", i);
  483. if (info.common_timing_params[i].ndimms_present == 0) {
  484. debug("No dimms present on controller %u; "
  485. "skipping programming\n", i);
  486. continue;
  487. }
  488. fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i);
  489. }
  490. /* program LAWs */
  491. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  492. if (info.memctl_opts[i].memctl_interleaving) {
  493. switch (info.memctl_opts[i].memctl_interleaving_mode) {
  494. case FSL_DDR_CACHE_LINE_INTERLEAVING:
  495. case FSL_DDR_PAGE_INTERLEAVING:
  496. case FSL_DDR_BANK_INTERLEAVING:
  497. case FSL_DDR_SUPERBANK_INTERLEAVING:
  498. if (i == 0) {
  499. law_memctl = LAW_TRGT_IF_DDR_INTRLV;
  500. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  501. law_memctl, i);
  502. } else if (i == 2) {
  503. law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
  504. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  505. law_memctl, i);
  506. }
  507. break;
  508. case FSL_DDR_3WAY_1KB_INTERLEAVING:
  509. case FSL_DDR_3WAY_4KB_INTERLEAVING:
  510. case FSL_DDR_3WAY_8KB_INTERLEAVING:
  511. law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
  512. if (i == 0) {
  513. fsl_ddr_set_intl3r(info.memctl_opts[i].memctl_interleaving_mode);
  514. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  515. law_memctl, i);
  516. }
  517. break;
  518. case FSL_DDR_4WAY_1KB_INTERLEAVING:
  519. case FSL_DDR_4WAY_4KB_INTERLEAVING:
  520. case FSL_DDR_4WAY_8KB_INTERLEAVING:
  521. law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
  522. if (i == 0)
  523. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  524. law_memctl, i);
  525. /* place holder for future 4-way interleaving */
  526. break;
  527. default:
  528. break;
  529. }
  530. } else {
  531. switch (i) {
  532. case 0:
  533. law_memctl = LAW_TRGT_IF_DDR_1;
  534. break;
  535. case 1:
  536. law_memctl = LAW_TRGT_IF_DDR_2;
  537. break;
  538. case 2:
  539. law_memctl = LAW_TRGT_IF_DDR_3;
  540. break;
  541. case 3:
  542. law_memctl = LAW_TRGT_IF_DDR_4;
  543. break;
  544. default:
  545. break;
  546. }
  547. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  548. law_memctl, i);
  549. }
  550. }
  551. debug("total_memory by %s = %llu\n", __func__, total_memory);
  552. #if !defined(CONFIG_PHYS_64BIT)
  553. /* Check for 4G or more. Bad. */
  554. if (total_memory >= (1ull << 32)) {
  555. printf("Detected %lld MB of memory\n", total_memory >> 20);
  556. printf(" This U-Boot only supports < 4G of DDR\n");
  557. printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
  558. printf(" "); /* re-align to match init_func_ram print */
  559. total_memory = CONFIG_MAX_MEM_MAPPED;
  560. }
  561. #endif
  562. return total_memory;
  563. }
  564. /*
  565. * fsl_ddr_sdram_size() - This function only returns the size of the total
  566. * memory without setting ddr control registers.
  567. */
  568. phys_size_t
  569. fsl_ddr_sdram_size(void)
  570. {
  571. fsl_ddr_info_t info;
  572. unsigned long long total_memory = 0;
  573. memset(&info, 0 , sizeof(fsl_ddr_info_t));
  574. /* Compute it once normally. */
  575. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
  576. return total_memory;
  577. }