main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. printf("can't handle 16-bit mode yet\n");
  157. break;
  158. case 1:
  159. /* 32-bit */
  160. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  161. unsigned int dw;
  162. dw = pinfo->dimm_params[i][j].data_width;
  163. if (pinfo->dimm_params[i][j].n_ranks
  164. && (dw == 72 || dw == 64)) {
  165. /*
  166. * FIXME: can't really do it
  167. * like this because this just
  168. * further reduces the memory
  169. */
  170. found = 1;
  171. break;
  172. }
  173. }
  174. if (found) {
  175. dbw_cap_adj[i] = 1;
  176. }
  177. break;
  178. case 0:
  179. /* 64-bit */
  180. break;
  181. default:
  182. printf("unexpected data bus width "
  183. "specified controller %u\n", i);
  184. return 1;
  185. }
  186. }
  187. j = 0;
  188. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  189. if (pinfo->memctl_opts[i].memctl_interleaving)
  190. j++;
  191. /*
  192. * Not support less than all memory controllers interleaving
  193. * if more than two controllers
  194. */
  195. if (j == CONFIG_NUM_DDR_CONTROLLERS)
  196. *all_memctl_interleaving = 1;
  197. /* Check that all controllers are rank interleaving. */
  198. j = 0;
  199. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  200. if (pinfo->memctl_opts[i].ba_intlv_ctl)
  201. j++;
  202. /*
  203. * All memory controllers must be populated to qualify for
  204. * all controller rank interleaving
  205. */
  206. if (j == CONFIG_NUM_DDR_CONTROLLERS)
  207. *all_ctlr_rank_interleaving = 1;
  208. if (*all_memctl_interleaving) {
  209. unsigned long long addr, total_mem_per_ctlr = 0;
  210. /*
  211. * If interleaving between memory controllers,
  212. * make each controller start at a base address
  213. * of 0.
  214. *
  215. * Also, if bank interleaving (chip select
  216. * interleaving) is enabled on each memory
  217. * controller, CS0 needs to be programmed to
  218. * cover the entire memory range on that memory
  219. * controller
  220. *
  221. * Bank interleaving also implies that each
  222. * addressed chip select is identical in size.
  223. */
  224. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  225. addr = 0;
  226. pinfo->common_timing_params[i].base_address = 0ull;
  227. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  228. unsigned long long cap
  229. = pinfo->dimm_params[i][j].capacity;
  230. pinfo->dimm_params[i][j].base_address = addr;
  231. addr += cap >> dbw_cap_adj[i];
  232. total_mem_per_ctlr += cap >> dbw_cap_adj[i];
  233. }
  234. }
  235. pinfo->common_timing_params[0].total_mem = total_mem_per_ctlr;
  236. } else {
  237. /*
  238. * Simple linear assignment if memory
  239. * controllers are not interleaved.
  240. */
  241. unsigned long long cur_memsize = 0;
  242. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  243. u64 total_mem_per_ctlr = 0;
  244. pinfo->common_timing_params[i].base_address =
  245. cur_memsize;
  246. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  247. /* Compute DIMM base addresses. */
  248. unsigned long long cap =
  249. pinfo->dimm_params[i][j].capacity;
  250. pinfo->dimm_params[i][j].base_address =
  251. cur_memsize;
  252. cur_memsize += cap >> dbw_cap_adj[i];
  253. total_mem_per_ctlr += cap >> dbw_cap_adj[i];
  254. }
  255. pinfo->common_timing_params[i].total_mem =
  256. total_mem_per_ctlr;
  257. }
  258. }
  259. return 0;
  260. }
  261. unsigned long long
  262. fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
  263. unsigned int size_only)
  264. {
  265. unsigned int i, j;
  266. unsigned int all_controllers_memctl_interleaving = 0;
  267. unsigned int all_controllers_rank_interleaving = 0;
  268. unsigned long long total_mem = 0;
  269. fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
  270. common_timing_params_t *timing_params = pinfo->common_timing_params;
  271. /* data bus width capacity adjust shift amount */
  272. unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
  273. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  274. dbw_capacity_adjust[i] = 0;
  275. }
  276. debug("starting at step %u (%s)\n",
  277. start_step, step_to_string(start_step));
  278. switch (start_step) {
  279. case STEP_GET_SPD:
  280. /* STEP 1: Gather all DIMM SPD data */
  281. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  282. fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
  283. }
  284. case STEP_COMPUTE_DIMM_PARMS:
  285. /* STEP 2: Compute DIMM parameters from SPD data */
  286. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  287. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  288. unsigned int retval;
  289. generic_spd_eeprom_t *spd =
  290. &(pinfo->spd_installed_dimms[i][j]);
  291. dimm_params_t *pdimm =
  292. &(pinfo->dimm_params[i][j]);
  293. retval = compute_dimm_parameters(spd, pdimm, i);
  294. if (retval == 2) {
  295. printf("Error: compute_dimm_parameters"
  296. " non-zero returned FATAL value "
  297. "for memctl=%u dimm=%u\n", i, j);
  298. return 0;
  299. }
  300. if (retval) {
  301. debug("Warning: compute_dimm_parameters"
  302. " non-zero return value for memctl=%u "
  303. "dimm=%u\n", i, j);
  304. }
  305. }
  306. }
  307. case STEP_COMPUTE_COMMON_PARMS:
  308. /*
  309. * STEP 3: Compute a common set of timing parameters
  310. * suitable for all of the DIMMs on each memory controller
  311. */
  312. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  313. debug("Computing lowest common DIMM"
  314. " parameters for memctl=%u\n", i);
  315. compute_lowest_common_dimm_parameters(
  316. pinfo->dimm_params[i],
  317. &timing_params[i],
  318. CONFIG_DIMM_SLOTS_PER_CTLR);
  319. }
  320. case STEP_GATHER_OPTS:
  321. /* STEP 4: Gather configuration requirements from user */
  322. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  323. debug("Reloading memory controller "
  324. "configuration options for memctl=%u\n", i);
  325. /*
  326. * This "reloads" the memory controller options
  327. * to defaults. If the user "edits" an option,
  328. * next_step points to the step after this,
  329. * which is currently STEP_ASSIGN_ADDRESSES.
  330. */
  331. populate_memctl_options(
  332. timing_params[i].all_DIMMs_registered,
  333. &pinfo->memctl_opts[i],
  334. pinfo->dimm_params[i], i);
  335. }
  336. check_interleaving_options(pinfo);
  337. case STEP_ASSIGN_ADDRESSES:
  338. /* STEP 5: Assign addresses to chip selects */
  339. step_assign_addresses(pinfo,
  340. dbw_capacity_adjust,
  341. &all_controllers_memctl_interleaving,
  342. &all_controllers_rank_interleaving);
  343. case STEP_COMPUTE_REGS:
  344. /* STEP 6: compute controller register values */
  345. debug("FSL Memory ctrl cg register computation\n");
  346. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  347. if (timing_params[i].ndimms_present == 0) {
  348. memset(&ddr_reg[i], 0,
  349. sizeof(fsl_ddr_cfg_regs_t));
  350. continue;
  351. }
  352. compute_fsl_memctl_config_regs(
  353. &pinfo->memctl_opts[i],
  354. &ddr_reg[i], &timing_params[i],
  355. pinfo->dimm_params[i],
  356. dbw_capacity_adjust[i],
  357. size_only);
  358. }
  359. default:
  360. break;
  361. }
  362. /* Compute the total amount of memory. */
  363. /*
  364. * If bank interleaving but NOT memory controller interleaving
  365. * CS_BNDS describe the quantity of memory on each memory
  366. * controller, so the total is the sum across.
  367. */
  368. if (!all_controllers_memctl_interleaving
  369. && all_controllers_rank_interleaving) {
  370. total_mem = 0;
  371. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  372. total_mem += timing_params[i].total_mem;
  373. }
  374. } else {
  375. /*
  376. * Compute the amount of memory available just by
  377. * looking for the highest valid CSn_BNDS value.
  378. * This allows us to also experiment with using
  379. * only CS0 when using dual-rank DIMMs.
  380. */
  381. unsigned int max_end = 0;
  382. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  383. for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
  384. fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
  385. if (reg->cs[j].config & 0x80000000) {
  386. unsigned int end;
  387. end = reg->cs[j].bnds & 0xFFF;
  388. if (end > max_end) {
  389. max_end = end;
  390. }
  391. }
  392. }
  393. }
  394. total_mem = 1 + (((unsigned long long)max_end << 24ULL)
  395. | 0xFFFFFFULL);
  396. }
  397. return total_mem;
  398. }
  399. /*
  400. * fsl_ddr_sdram() -- this is the main function to be called by
  401. * initdram() in the board file.
  402. *
  403. * It returns amount of memory configured in bytes.
  404. */
  405. phys_size_t fsl_ddr_sdram(void)
  406. {
  407. unsigned int i;
  408. unsigned int memctl_interleaved;
  409. unsigned long long total_memory;
  410. fsl_ddr_info_t info;
  411. /* Reset info structure. */
  412. memset(&info, 0, sizeof(fsl_ddr_info_t));
  413. /* Compute it once normally. */
  414. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
  415. /* Check for memory controller interleaving. */
  416. memctl_interleaved = 0;
  417. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  418. memctl_interleaved +=
  419. info.memctl_opts[i].memctl_interleaving;
  420. }
  421. if (memctl_interleaved) {
  422. if (memctl_interleaved == CONFIG_NUM_DDR_CONTROLLERS) {
  423. debug("memctl interleaving\n");
  424. /*
  425. * Change the meaning of memctl_interleaved
  426. * to be "boolean".
  427. */
  428. memctl_interleaved = 1;
  429. } else {
  430. printf("Warning: memctl interleaving not "
  431. "properly configured on all controllers\n");
  432. memctl_interleaved = 0;
  433. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
  434. info.memctl_opts[i].memctl_interleaving = 0;
  435. debug("Recomputing with memctl_interleaving off.\n");
  436. total_memory = fsl_ddr_compute(&info,
  437. STEP_ASSIGN_ADDRESSES,
  438. 0);
  439. }
  440. }
  441. /* Program configuration registers. */
  442. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  443. debug("Programming controller %u\n", i);
  444. if (info.common_timing_params[i].ndimms_present == 0) {
  445. debug("No dimms present on controller %u; "
  446. "skipping programming\n", i);
  447. continue;
  448. }
  449. fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i);
  450. }
  451. if (memctl_interleaved) {
  452. const unsigned int ctrl_num = 0;
  453. /* Only set LAWBAR1 if memory controller interleaving is on. */
  454. fsl_ddr_set_lawbar(&info.common_timing_params[0],
  455. memctl_interleaved, ctrl_num);
  456. } else {
  457. /*
  458. * Memory controller interleaving is NOT on;
  459. * set each lawbar individually.
  460. */
  461. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  462. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  463. 0, i);
  464. }
  465. }
  466. debug("total_memory = %llu\n", total_memory);
  467. #if !defined(CONFIG_PHYS_64BIT)
  468. /* Check for 4G or more. Bad. */
  469. if (total_memory >= (1ull << 32)) {
  470. printf("Detected %lld MB of memory\n", total_memory >> 20);
  471. printf(" This U-Boot only supports < 4G of DDR\n");
  472. printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
  473. printf(" "); /* re-align to match init_func_ram print */
  474. total_memory = CONFIG_MAX_MEM_MAPPED;
  475. }
  476. #endif
  477. return total_memory;
  478. }
  479. /*
  480. * fsl_ddr_sdram_size() - This function only returns the size of the total
  481. * memory without setting ddr control registers.
  482. */
  483. phys_size_t
  484. fsl_ddr_sdram_size(void)
  485. {
  486. fsl_ddr_info_t info;
  487. unsigned long long total_memory = 0;
  488. memset(&info, 0 , sizeof(fsl_ddr_info_t));
  489. /* Compute it once normally. */
  490. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
  491. return total_memory;
  492. }