main.c 21 KB

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