main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. }
  149. /* Check that all controllers are rank interleaving. */
  150. j = 0;
  151. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  152. if (pinfo->memctl_opts[i].ba_intlv_ctl) {
  153. j++;
  154. }
  155. }
  156. if (j == 2) {
  157. *rank_interleaving = 1;
  158. }
  159. if (*memctl_interleaving) {
  160. phys_addr_t addr;
  161. phys_size_t total_mem_per_ctlr = 0;
  162. /*
  163. * If interleaving between memory controllers,
  164. * make each controller start at a base address
  165. * of 0.
  166. *
  167. * Also, if bank interleaving (chip select
  168. * interleaving) is enabled on each memory
  169. * controller, CS0 needs to be programmed to
  170. * cover the entire memory range on that memory
  171. * controller
  172. *
  173. * Bank interleaving also implies that each
  174. * addressed chip select is identical in size.
  175. */
  176. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  177. addr = 0;
  178. pinfo->common_timing_params[i].base_address =
  179. (phys_addr_t)addr;
  180. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  181. unsigned long long cap
  182. = pinfo->dimm_params[i][j].capacity;
  183. pinfo->dimm_params[i][j].base_address = addr;
  184. addr += (phys_addr_t)(cap >> dbw_cap_adj[i]);
  185. total_mem_per_ctlr += cap >> dbw_cap_adj[i];
  186. }
  187. }
  188. pinfo->common_timing_params[0].total_mem = total_mem_per_ctlr;
  189. } else {
  190. /*
  191. * Simple linear assignment if memory
  192. * controllers are not interleaved.
  193. */
  194. phys_size_t cur_memsize = 0;
  195. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  196. phys_size_t total_mem_per_ctlr = 0;
  197. pinfo->common_timing_params[i].base_address =
  198. (phys_addr_t)cur_memsize;
  199. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  200. /* Compute DIMM base addresses. */
  201. unsigned long long cap =
  202. pinfo->dimm_params[i][j].capacity;
  203. pinfo->dimm_params[i][j].base_address =
  204. (phys_addr_t)cur_memsize;
  205. cur_memsize += cap >> dbw_cap_adj[i];
  206. total_mem_per_ctlr += cap >> dbw_cap_adj[i];
  207. }
  208. pinfo->common_timing_params[i].total_mem =
  209. total_mem_per_ctlr;
  210. }
  211. }
  212. return 0;
  213. }
  214. phys_size_t
  215. fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step)
  216. {
  217. unsigned int i, j;
  218. unsigned int all_controllers_memctl_interleaving = 0;
  219. unsigned int all_controllers_rank_interleaving = 0;
  220. phys_size_t total_mem = 0;
  221. fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
  222. common_timing_params_t *timing_params = pinfo->common_timing_params;
  223. /* data bus width capacity adjust shift amount */
  224. unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
  225. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  226. dbw_capacity_adjust[i] = 0;
  227. }
  228. debug("starting at step %u (%s)\n",
  229. start_step, step_to_string(start_step));
  230. switch (start_step) {
  231. case STEP_GET_SPD:
  232. /* STEP 1: Gather all DIMM SPD data */
  233. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  234. fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
  235. }
  236. case STEP_COMPUTE_DIMM_PARMS:
  237. /* STEP 2: Compute DIMM parameters from SPD data */
  238. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  239. for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
  240. unsigned int retval;
  241. generic_spd_eeprom_t *spd =
  242. &(pinfo->spd_installed_dimms[i][j]);
  243. dimm_params_t *pdimm =
  244. &(pinfo->dimm_params[i][j]);
  245. retval = compute_dimm_parameters(spd, pdimm, i);
  246. if (retval == 2) {
  247. printf("Error: compute_dimm_parameters"
  248. " non-zero returned FATAL value "
  249. "for memctl=%u dimm=%u\n", i, j);
  250. return 0;
  251. }
  252. if (retval) {
  253. debug("Warning: compute_dimm_parameters"
  254. " non-zero return value for memctl=%u "
  255. "dimm=%u\n", i, j);
  256. }
  257. }
  258. }
  259. case STEP_COMPUTE_COMMON_PARMS:
  260. /*
  261. * STEP 3: Compute a common set of timing parameters
  262. * suitable for all of the DIMMs on each memory controller
  263. */
  264. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  265. debug("Computing lowest common DIMM"
  266. " parameters for memctl=%u\n", i);
  267. compute_lowest_common_dimm_parameters(
  268. pinfo->dimm_params[i],
  269. &timing_params[i],
  270. CONFIG_DIMM_SLOTS_PER_CTLR);
  271. }
  272. case STEP_GATHER_OPTS:
  273. /* STEP 4: Gather configuration requirements from user */
  274. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  275. debug("Reloading memory controller "
  276. "configuration options for memctl=%u\n", i);
  277. /*
  278. * This "reloads" the memory controller options
  279. * to defaults. If the user "edits" an option,
  280. * next_step points to the step after this,
  281. * which is currently STEP_ASSIGN_ADDRESSES.
  282. */
  283. populate_memctl_options(
  284. timing_params[i].all_DIMMs_registered,
  285. &pinfo->memctl_opts[i], i);
  286. }
  287. case STEP_ASSIGN_ADDRESSES:
  288. /* STEP 5: Assign addresses to chip selects */
  289. step_assign_addresses(pinfo,
  290. dbw_capacity_adjust,
  291. &all_controllers_memctl_interleaving,
  292. &all_controllers_rank_interleaving);
  293. case STEP_COMPUTE_REGS:
  294. /* STEP 6: compute controller register values */
  295. debug("FSL Memory ctrl cg register computation\n");
  296. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  297. if (timing_params[i].ndimms_present == 0) {
  298. memset(&ddr_reg[i], 0,
  299. sizeof(fsl_ddr_cfg_regs_t));
  300. continue;
  301. }
  302. compute_fsl_memctl_config_regs(
  303. &pinfo->memctl_opts[i],
  304. &ddr_reg[i], &timing_params[i],
  305. pinfo->dimm_params[i],
  306. dbw_capacity_adjust[i]);
  307. }
  308. default:
  309. break;
  310. }
  311. /* Compute the total amount of memory. */
  312. /*
  313. * If bank interleaving but NOT memory controller interleaving
  314. * CS_BNDS describe the quantity of memory on each memory
  315. * controller, so the total is the sum across.
  316. */
  317. if (!all_controllers_memctl_interleaving
  318. && all_controllers_rank_interleaving) {
  319. total_mem = 0;
  320. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  321. total_mem += timing_params[i].total_mem;
  322. }
  323. } else {
  324. /*
  325. * Compute the amount of memory available just by
  326. * looking for the highest valid CSn_BNDS value.
  327. * This allows us to also experiment with using
  328. * only CS0 when using dual-rank DIMMs.
  329. */
  330. unsigned int max_end = 0;
  331. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  332. for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
  333. fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
  334. if (reg->cs[j].config & 0x80000000) {
  335. unsigned int end;
  336. end = reg->cs[j].bnds & 0xFFF;
  337. if (end > max_end) {
  338. max_end = end;
  339. }
  340. }
  341. }
  342. }
  343. #if !defined(CONFIG_PHYS_64BIT)
  344. /* Check for 4G or more with a 32-bit phys_addr_t. Bad. */
  345. if (max_end >= 0xff) {
  346. printf("This U-Boot only supports < 4G of DDR\n");
  347. printf("You could rebuild it with CONFIG_PHYS_64BIT\n");
  348. return 0; /* Ensure DDR setup failure. */
  349. }
  350. #endif
  351. total_mem = 1 + (((unsigned long long)max_end << 24ULL)
  352. | 0xFFFFFFULL);
  353. }
  354. return total_mem;
  355. }
  356. /*
  357. * fsl_ddr_sdram() -- this is the main function to be called by
  358. * initdram() in the board file.
  359. *
  360. * It returns amount of memory configured in bytes.
  361. */
  362. phys_size_t fsl_ddr_sdram(void)
  363. {
  364. unsigned int i;
  365. unsigned int memctl_interleaved;
  366. phys_size_t total_memory;
  367. fsl_ddr_info_t info;
  368. /* Reset info structure. */
  369. memset(&info, 0, sizeof(fsl_ddr_info_t));
  370. /* Compute it once normally. */
  371. total_memory = fsl_ddr_compute(&info, STEP_GET_SPD);
  372. /* Check for memory controller interleaving. */
  373. memctl_interleaved = 0;
  374. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  375. memctl_interleaved +=
  376. info.memctl_opts[i].memctl_interleaving;
  377. }
  378. if (memctl_interleaved) {
  379. if (memctl_interleaved == CONFIG_NUM_DDR_CONTROLLERS) {
  380. debug("memctl interleaving\n");
  381. /*
  382. * Change the meaning of memctl_interleaved
  383. * to be "boolean".
  384. */
  385. memctl_interleaved = 1;
  386. } else {
  387. printf("Error: memctl interleaving not "
  388. "properly configured on all controllers\n");
  389. while (1);
  390. }
  391. }
  392. /* Program configuration registers. */
  393. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  394. debug("Programming controller %u\n", i);
  395. if (info.common_timing_params[i].ndimms_present == 0) {
  396. debug("No dimms present on controller %u; "
  397. "skipping programming\n", i);
  398. continue;
  399. }
  400. fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i);
  401. }
  402. if (memctl_interleaved) {
  403. const unsigned int ctrl_num = 0;
  404. /* Only set LAWBAR1 if memory controller interleaving is on. */
  405. fsl_ddr_set_lawbar(&info.common_timing_params[0],
  406. memctl_interleaved, ctrl_num);
  407. } else {
  408. /*
  409. * Memory controller interleaving is NOT on;
  410. * set each lawbar individually.
  411. */
  412. for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
  413. fsl_ddr_set_lawbar(&info.common_timing_params[i],
  414. 0, i);
  415. }
  416. }
  417. debug("total_memory = %llu\n", (u64)total_memory);
  418. return total_memory;
  419. }