main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * Version 2 as published by the Free Software Foundation.
  7. */
  8. /*
  9. * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
  10. * Based on code from spd_sdram.c
  11. * Author: James Yang [at freescale.com]
  12. */
  13. #include <common.h>
  14. #include <i2c.h>
  15. #include <asm/fsl_ddr_sdram.h>
  16. #include "ddr.h"
  17. extern void fsl_ddr_set_lawbar(
  18. const common_timing_params_t *memctl_common_params,
  19. unsigned int memctl_interleaved,
  20. unsigned int ctrl_num);
  21. /* processor specific function */
  22. extern void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
  23. unsigned int ctrl_num);
  24. #if defined(SPD_EEPROM_ADDRESS) || \
  25. defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
  26. defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
  27. #if (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  28. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  29. [0][0] = SPD_EEPROM_ADDRESS,
  30. };
  31. #endif
  32. #if (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
  33. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  34. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  35. [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
  36. };
  37. #endif
  38. #if (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
  39. u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
  40. [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
  41. [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
  42. [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
  43. [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
  44. };
  45. #endif
  46. static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
  47. {
  48. int ret = i2c_read(i2c_address, 0, 1, (uchar *)spd,
  49. sizeof(generic_spd_eeprom_t));
  50. if (ret) {
  51. printf("DDR: failed to read SPD from address %u\n", i2c_address);
  52. memset(spd, 0, sizeof(generic_spd_eeprom_t));
  53. }
  54. }
  55. __attribute__((weak, alias("__get_spd")))
  56. void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
  57. void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
  58. unsigned int ctrl_num)
  59. {
  60. unsigned int i;
  61. unsigned int i2c_address = 0;
  62. if (ctrl_num >= CONFIG_NUM_DDR_CONTROLLERS) {
  63. printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
  64. return;
  65. }
  66. for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
  67. i2c_address = spd_i2c_addr[ctrl_num][i];
  68. get_spd(&(ctrl_dimms_spd[i]), i2c_address);
  69. }
  70. }
  71. #else
  72. void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
  73. unsigned int ctrl_num)
  74. {
  75. }
  76. #endif /* SPD_EEPROM_ADDRESSx */
  77. /*
  78. * ASSUMPTIONS:
  79. * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
  80. * - Same memory data bus width on all controllers
  81. *
  82. * NOTES:
  83. *
  84. * The memory controller and associated documentation use confusing
  85. * terminology when referring to the orgranization of DRAM.
  86. *
  87. * Here is a terminology translation table:
  88. *
  89. * memory controller/documention |industry |this code |signals
  90. * -------------------------------|-----------|-----------|-----------------
  91. * physical bank/bank |rank |rank |chip select (CS)
  92. * logical bank/sub-bank |bank |bank |bank address (BA)
  93. * page/row |row |page |row address
  94. * ??? |column |column |column address
  95. *
  96. * The naming confusion is further exacerbated by the descriptions of the
  97. * memory controller interleaving feature, where accesses are interleaved
  98. * _BETWEEN_ two seperate memory controllers. This is configured only in
  99. * CS0_CONFIG[INTLV_CTL] of each memory controller.
  100. *
  101. * memory controller documentation | number of chip selects
  102. * | per memory controller supported
  103. * --------------------------------|-----------------------------------------
  104. * cache line interleaving | 1 (CS0 only)
  105. * page interleaving | 1 (CS0 only)
  106. * bank interleaving | 1 (CS0 only)
  107. * superbank interleraving | depends on bank (chip select)
  108. * | interleraving [rank interleaving]
  109. * | mode used on every memory controller
  110. *
  111. * Even further confusing is the existence of the interleaving feature
  112. * _WITHIN_ each memory controller. The feature is referred to in
  113. * documentation as chip select interleaving or bank interleaving,
  114. * although it is configured in the DDR_SDRAM_CFG field.
  115. *
  116. * Name of field | documentation name | this code
  117. * -----------------------------|-----------------------|------------------
  118. * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
  119. * | interleaving
  120. */
  121. #ifdef DEBUG
  122. const char *step_string_tbl[] = {
  123. "STEP_GET_SPD",
  124. "STEP_COMPUTE_DIMM_PARMS",
  125. "STEP_COMPUTE_COMMON_PARMS",
  126. "STEP_GATHER_OPTS",
  127. "STEP_ASSIGN_ADDRESSES",
  128. "STEP_COMPUTE_REGS",
  129. "STEP_PROGRAM_REGS",
  130. "STEP_ALL"
  131. };
  132. const char * step_to_string(unsigned int step) {
  133. unsigned int s = __ilog2(step);
  134. if ((1 << s) != step)
  135. return step_string_tbl[7];
  136. return step_string_tbl[s];
  137. }
  138. #endif
  139. int step_assign_addresses(fsl_ddr_info_t *pinfo,
  140. unsigned int dbw_cap_adj[],
  141. unsigned int *all_memctl_interleaving,
  142. unsigned int *all_ctlr_rank_interleaving)
  143. {
  144. int i, j;
  145. /*
  146. * If a reduced data width is requested, but the SPD
  147. * specifies a physically wider device, adjust the
  148. * computed dimm capacities accordingly before
  149. * assigning addresses.
  150. */
  151. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  152. unsigned int found = 0;
  153. switch (pinfo->memctl_opts[i].data_bus_width) {
  154. case 2:
  155. /* 16-bit */
  156. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  157. unsigned int dw;
  158. if (!pinfo->dimm_params[i][j].n_ranks)
  159. continue;
  160. dw = pinfo->dimm_params[i][j].primary_sdram_width;
  161. if ((dw == 72 || dw == 64)) {
  162. dbw_cap_adj[i] = 2;
  163. break;
  164. } else if ((dw == 40 || dw == 32)) {
  165. dbw_cap_adj[i] = 1;
  166. break;
  167. }
  168. }
  169. break;
  170. case 1:
  171. /* 32-bit */
  172. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  173. unsigned int dw;
  174. dw = pinfo->dimm_params[i][j].data_width;
  175. if (pinfo->dimm_params[i][j].n_ranks
  176. && (dw == 72 || dw == 64)) {
  177. /*
  178. * FIXME: can't really do it
  179. * like this because this just
  180. * further reduces the memory
  181. */
  182. found = 1;
  183. break;
  184. }
  185. }
  186. if (found) {
  187. dbw_cap_adj[i] = 1;
  188. }
  189. break;
  190. case 0:
  191. /* 64-bit */
  192. break;
  193. default:
  194. printf("unexpected data bus width "
  195. "specified controller %u\n", i);
  196. return 1;
  197. }
  198. }
  199. j = 0;
  200. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  201. if (pinfo->memctl_opts[i].memctl_interleaving)
  202. j++;
  203. /*
  204. * Not support less than all memory controllers interleaving
  205. * if more than two controllers
  206. */
  207. if (j == CONFIG_NUM_DDR_CONTROLLERS)
  208. *all_memctl_interleaving = 1;
  209. /* Check that all controllers are rank interleaving. */
  210. j = 0;
  211. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  212. if (pinfo->memctl_opts[i].ba_intlv_ctl)
  213. j++;
  214. /*
  215. * All memory controllers must be populated to qualify for
  216. * all controller rank interleaving
  217. */
  218. if (j == CONFIG_NUM_DDR_CONTROLLERS)
  219. *all_ctlr_rank_interleaving = 1;
  220. if (*all_memctl_interleaving) {
  221. unsigned long long addr, total_mem_per_ctlr = 0;
  222. /*
  223. * If interleaving between memory controllers,
  224. * make each controller start at a base address
  225. * of 0.
  226. *
  227. * Also, if bank interleaving (chip select
  228. * interleaving) is enabled on each memory
  229. * controller, CS0 needs to be programmed to
  230. * cover the entire memory range on that memory
  231. * controller
  232. *
  233. * Bank interleaving also implies that each
  234. * addressed chip select is identical in size.
  235. */
  236. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  237. addr = 0;
  238. pinfo->common_timing_params[i].base_address = 0ull;
  239. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  240. unsigned long long cap
  241. = pinfo->dimm_params[i][j].capacity;
  242. pinfo->dimm_params[i][j].base_address = addr;
  243. addr += cap >> dbw_cap_adj[i];
  244. total_mem_per_ctlr += cap >> dbw_cap_adj[i];
  245. }
  246. }
  247. pinfo->common_timing_params[0].total_mem = total_mem_per_ctlr;
  248. } else {
  249. /*
  250. * Simple linear assignment if memory
  251. * controllers are not interleaved.
  252. */
  253. unsigned long long cur_memsize = 0;
  254. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  255. u64 total_mem_per_ctlr = 0;
  256. pinfo->common_timing_params[i].base_address =
  257. cur_memsize;
  258. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  259. /* Compute DIMM base addresses. */
  260. unsigned long long cap =
  261. pinfo->dimm_params[i][j].capacity;
  262. pinfo->dimm_params[i][j].base_address =
  263. cur_memsize;
  264. cur_memsize += cap >> dbw_cap_adj[i];
  265. total_mem_per_ctlr += cap >> dbw_cap_adj[i];
  266. }
  267. pinfo->common_timing_params[i].total_mem =
  268. total_mem_per_ctlr;
  269. }
  270. }
  271. return 0;
  272. }
  273. unsigned long long
  274. fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
  275. unsigned int size_only)
  276. {
  277. unsigned int i, j;
  278. unsigned int all_controllers_memctl_interleaving = 0;
  279. unsigned int all_controllers_rank_interleaving = 0;
  280. unsigned long long total_mem = 0;
  281. fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
  282. common_timing_params_t *timing_params = pinfo->common_timing_params;
  283. /* data bus width capacity adjust shift amount */
  284. unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
  285. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  286. dbw_capacity_adjust[i] = 0;
  287. }
  288. debug("starting at step %u (%s)\n",
  289. start_step, step_to_string(start_step));
  290. switch (start_step) {
  291. case STEP_GET_SPD:
  292. #if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
  293. /* STEP 1: Gather all DIMM SPD data */
  294. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  295. fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
  296. }
  297. case STEP_COMPUTE_DIMM_PARMS:
  298. /* STEP 2: Compute DIMM parameters from SPD data */
  299. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  300. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  301. unsigned int retval;
  302. generic_spd_eeprom_t *spd =
  303. &(pinfo->spd_installed_dimms[i][j]);
  304. dimm_params_t *pdimm =
  305. &(pinfo->dimm_params[i][j]);
  306. retval = compute_dimm_parameters(spd, pdimm, i);
  307. if (retval == 2) {
  308. printf("Error: compute_dimm_parameters"
  309. " non-zero returned FATAL value "
  310. "for memctl=%u dimm=%u\n", i, j);
  311. return 0;
  312. }
  313. if (retval) {
  314. debug("Warning: compute_dimm_parameters"
  315. " non-zero return value for memctl=%u "
  316. "dimm=%u\n", i, j);
  317. }
  318. }
  319. }
  320. #else
  321. case STEP_COMPUTE_DIMM_PARMS:
  322. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  323. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  324. dimm_params_t *pdimm =
  325. &(pinfo->dimm_params[i][j]);
  326. fsl_ddr_get_dimm_params(pdimm, i, j);
  327. }
  328. }
  329. debug("Filling dimm parameters from board specific file\n");
  330. #endif
  331. case STEP_COMPUTE_COMMON_PARMS:
  332. /*
  333. * STEP 3: Compute a common set of timing parameters
  334. * suitable for all of the DIMMs on each memory controller
  335. */
  336. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  337. debug("Computing lowest common DIMM"
  338. " parameters for memctl=%u\n", i);
  339. compute_lowest_common_dimm_parameters(
  340. pinfo->dimm_params[i],
  341. &timing_params[i],
  342. CONFIG_DIMM_SLOTS_PER_CTLR);
  343. }
  344. case STEP_GATHER_OPTS:
  345. /* STEP 4: Gather configuration requirements from user */
  346. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  347. debug("Reloading memory controller "
  348. "configuration options for memctl=%u\n", i);
  349. /*
  350. * This "reloads" the memory controller options
  351. * to defaults. If the user "edits" an option,
  352. * next_step points to the step after this,
  353. * which is currently STEP_ASSIGN_ADDRESSES.
  354. */
  355. populate_memctl_options(
  356. timing_params[i].all_DIMMs_registered,
  357. &pinfo->memctl_opts[i],
  358. pinfo->dimm_params[i], i);
  359. }
  360. check_interleaving_options(pinfo);
  361. case STEP_ASSIGN_ADDRESSES:
  362. /* STEP 5: Assign addresses to chip selects */
  363. step_assign_addresses(pinfo,
  364. dbw_capacity_adjust,
  365. &all_controllers_memctl_interleaving,
  366. &all_controllers_rank_interleaving);
  367. case STEP_COMPUTE_REGS:
  368. /* STEP 6: compute controller register values */
  369. debug("FSL Memory ctrl cg register computation\n");
  370. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  371. if (timing_params[i].ndimms_present == 0) {
  372. memset(&ddr_reg[i], 0,
  373. sizeof(fsl_ddr_cfg_regs_t));
  374. continue;
  375. }
  376. compute_fsl_memctl_config_regs(
  377. &pinfo->memctl_opts[i],
  378. &ddr_reg[i], &timing_params[i],
  379. pinfo->dimm_params[i],
  380. dbw_capacity_adjust[i],
  381. size_only);
  382. }
  383. default:
  384. break;
  385. }
  386. /* Compute the total amount of memory. */
  387. /*
  388. * If bank interleaving but NOT memory controller interleaving
  389. * CS_BNDS describe the quantity of memory on each memory
  390. * controller, so the total is the sum across.
  391. */
  392. if (!all_controllers_memctl_interleaving
  393. && all_controllers_rank_interleaving) {
  394. total_mem = 0;
  395. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  396. total_mem += timing_params[i].total_mem;
  397. }
  398. } else {
  399. /*
  400. * Compute the amount of memory available just by
  401. * looking for the highest valid CSn_BNDS value.
  402. * This allows us to also experiment with using
  403. * only CS0 when using dual-rank DIMMs.
  404. */
  405. unsigned int max_end = 0;
  406. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  407. for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
  408. fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
  409. if (reg->cs[j].config & 0x80000000) {
  410. unsigned int end;
  411. end = reg->cs[j].bnds & 0xFFF;
  412. if (end > max_end) {
  413. max_end = end;
  414. }
  415. }
  416. }
  417. }
  418. total_mem = 1 + (((unsigned long long)max_end << 24ULL)
  419. | 0xFFFFFFULL);
  420. }
  421. return total_mem;
  422. }
  423. /*
  424. * fsl_ddr_sdram() -- this is the main function to be called by
  425. * initdram() in the board file.
  426. *
  427. * It returns amount of memory configured in bytes.
  428. */
  429. phys_size_t fsl_ddr_sdram(void)
  430. {
  431. unsigned int i;
  432. unsigned int memctl_interleaved;
  433. unsigned long long total_memory;
  434. fsl_ddr_info_t info;
  435. /* Reset info structure. */
  436. memset(&info, 0, sizeof(fsl_ddr_info_t));
  437. /* Compute it once normally. */
  438. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
  439. /* Check for memory controller interleaving. */
  440. memctl_interleaved = 0;
  441. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  442. memctl_interleaved +=
  443. info.memctl_opts[i].memctl_interleaving;
  444. }
  445. if (memctl_interleaved) {
  446. if (memctl_interleaved == CONFIG_NUM_DDR_CONTROLLERS) {
  447. debug("memctl interleaving\n");
  448. /*
  449. * Change the meaning of memctl_interleaved
  450. * to be "boolean".
  451. */
  452. memctl_interleaved = 1;
  453. } else {
  454. printf("Warning: memctl interleaving not "
  455. "properly configured on all controllers\n");
  456. memctl_interleaved = 0;
  457. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  458. info.memctl_opts[i].memctl_interleaving = 0;
  459. debug("Recomputing with memctl_interleaving off.\n");
  460. total_memory = fsl_ddr_compute(&info,
  461. STEP_ASSIGN_ADDRESSES,
  462. 0);
  463. }
  464. }
  465. /* Program configuration registers. */
  466. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  467. debug("Programming controller %u\n", i);
  468. if (info.common_timing_params[i].ndimms_present == 0) {
  469. debug("No dimms present on controller %u; "
  470. "skipping programming\n", i);
  471. continue;
  472. }
  473. fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i);
  474. }
  475. if (memctl_interleaved) {
  476. const unsigned int ctrl_num = 0;
  477. /* Only set LAWBAR1 if memory controller interleaving is on. */
  478. fsl_ddr_set_lawbar(&info.common_timing_params[0],
  479. memctl_interleaved, ctrl_num);
  480. } else {
  481. /*
  482. * Memory controller interleaving is NOT on;
  483. * set each lawbar individually.
  484. */
  485. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  486. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  487. 0, i);
  488. }
  489. }
  490. debug("total_memory = %llu\n", total_memory);
  491. #if !defined(CONFIG_PHYS_64BIT)
  492. /* Check for 4G or more. Bad. */
  493. if (total_memory >= (1ull << 32)) {
  494. printf("Detected %lld MB of memory\n", total_memory >> 20);
  495. printf(" This U-Boot only supports < 4G of DDR\n");
  496. printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
  497. printf(" "); /* re-align to match init_func_ram print */
  498. total_memory = CONFIG_MAX_MEM_MAPPED;
  499. }
  500. #endif
  501. return total_memory;
  502. }
  503. /*
  504. * fsl_ddr_sdram_size() - This function only returns the size of the total
  505. * memory without setting ddr control registers.
  506. */
  507. phys_size_t
  508. fsl_ddr_sdram_size(void)
  509. {
  510. fsl_ddr_info_t info;
  511. unsigned long long total_memory = 0;
  512. memset(&info, 0 , sizeof(fsl_ddr_info_t));
  513. /* Compute it once normally. */
  514. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
  515. return total_memory;
  516. }