main.c 25 KB

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