main.c 25 KB

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