main.c 22 KB

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