main.c 25 KB

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