main.c 21 KB

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