main.c 13 KB

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