main.c 19 KB

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