main.c 14 KB

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