main.c 25 KB

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