blob: c2a03e334cfa50db43970aef9440de3601695565 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
Haiying Wang80ad4012010-12-01 10:35:31 -05002 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-08-26 15:01:29 -05003 *
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/*
10 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
11 * Based on code from spd_sdram.c
12 * Author: James Yang [at freescale.com]
13 */
14
15#include <common.h>
Kumar Galac68e86c2011-01-31 22:18:47 -060016#include <i2c.h>
Kumar Gala124b0822008-08-26 15:01:29 -050017#include <asm/fsl_ddr_sdram.h>
18
19#include "ddr.h"
20
21extern void fsl_ddr_set_lawbar(
22 const common_timing_params_t *memctl_common_params,
23 unsigned int memctl_interleaved,
24 unsigned int ctrl_num);
25
26/* processor specific function */
27extern void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
28 unsigned int ctrl_num);
29
Kumar Galac68e86c2011-01-31 22:18:47 -060030#if defined(SPD_EEPROM_ADDRESS) || \
31 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
32 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
33#if (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
34u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
35 [0][0] = SPD_EEPROM_ADDRESS,
36};
York Sun92b46ac2011-08-26 11:32:41 -070037#elif (CONFIG_NUM_DDR_CONTROLLERS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
Kumar Galac68e86c2011-01-31 22:18:47 -060038u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
39 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
York Sun92b46ac2011-08-26 11:32:41 -070040 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
41};
42#elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
43u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
44 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
Kumar Galac68e86c2011-01-31 22:18:47 -060045 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
46};
York Sun92b46ac2011-08-26 11:32:41 -070047#elif (CONFIG_NUM_DDR_CONTROLLERS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
Kumar Galac68e86c2011-01-31 22:18:47 -060048u8 spd_i2c_addr[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
49 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
50 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
51 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
52 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
53};
54#endif
55
56static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
57{
58 int ret = i2c_read(i2c_address, 0, 1, (uchar *)spd,
59 sizeof(generic_spd_eeprom_t));
60
61 if (ret) {
62 printf("DDR: failed to read SPD from address %u\n", i2c_address);
63 memset(spd, 0, sizeof(generic_spd_eeprom_t));
64 }
65}
66
67__attribute__((weak, alias("__get_spd")))
68void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
69
70void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
71 unsigned int ctrl_num)
72{
73 unsigned int i;
74 unsigned int i2c_address = 0;
75
76 if (ctrl_num >= CONFIG_NUM_DDR_CONTROLLERS) {
77 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
78 return;
79 }
80
81 for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
82 i2c_address = spd_i2c_addr[ctrl_num][i];
83 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
84 }
85}
86#else
87void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
88 unsigned int ctrl_num)
89{
90}
91#endif /* SPD_EEPROM_ADDRESSx */
Kumar Gala124b0822008-08-26 15:01:29 -050092
93/*
94 * ASSUMPTIONS:
95 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
96 * - Same memory data bus width on all controllers
97 *
98 * NOTES:
99 *
100 * The memory controller and associated documentation use confusing
101 * terminology when referring to the orgranization of DRAM.
102 *
103 * Here is a terminology translation table:
104 *
105 * memory controller/documention |industry |this code |signals
106 * -------------------------------|-----------|-----------|-----------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200107 * physical bank/bank |rank |rank |chip select (CS)
108 * logical bank/sub-bank |bank |bank |bank address (BA)
109 * page/row |row |page |row address
110 * ??? |column |column |column address
Kumar Gala124b0822008-08-26 15:01:29 -0500111 *
112 * The naming confusion is further exacerbated by the descriptions of the
113 * memory controller interleaving feature, where accesses are interleaved
114 * _BETWEEN_ two seperate memory controllers. This is configured only in
115 * CS0_CONFIG[INTLV_CTL] of each memory controller.
116 *
117 * memory controller documentation | number of chip selects
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200118 * | per memory controller supported
Kumar Gala124b0822008-08-26 15:01:29 -0500119 * --------------------------------|-----------------------------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200120 * cache line interleaving | 1 (CS0 only)
121 * page interleaving | 1 (CS0 only)
122 * bank interleaving | 1 (CS0 only)
123 * superbank interleraving | depends on bank (chip select)
124 * | interleraving [rank interleaving]
125 * | mode used on every memory controller
Kumar Gala124b0822008-08-26 15:01:29 -0500126 *
127 * Even further confusing is the existence of the interleaving feature
128 * _WITHIN_ each memory controller. The feature is referred to in
129 * documentation as chip select interleaving or bank interleaving,
130 * although it is configured in the DDR_SDRAM_CFG field.
131 *
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200132 * Name of field | documentation name | this code
Kumar Gala124b0822008-08-26 15:01:29 -0500133 * -----------------------------|-----------------------|------------------
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200134 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
135 * | interleaving
Kumar Gala124b0822008-08-26 15:01:29 -0500136 */
137
Kumar Gala124b0822008-08-26 15:01:29 -0500138const char *step_string_tbl[] = {
139 "STEP_GET_SPD",
140 "STEP_COMPUTE_DIMM_PARMS",
141 "STEP_COMPUTE_COMMON_PARMS",
142 "STEP_GATHER_OPTS",
143 "STEP_ASSIGN_ADDRESSES",
144 "STEP_COMPUTE_REGS",
145 "STEP_PROGRAM_REGS",
146 "STEP_ALL"
147};
148
149const char * step_to_string(unsigned int step) {
150
151 unsigned int s = __ilog2(step);
152
153 if ((1 << s) != step)
154 return step_string_tbl[7];
155
156 return step_string_tbl[s];
157}
Kumar Gala124b0822008-08-26 15:01:29 -0500158
159int step_assign_addresses(fsl_ddr_info_t *pinfo,
160 unsigned int dbw_cap_adj[],
york93799ca2010-07-02 22:25:52 +0000161 unsigned int *all_memctl_interleaving,
162 unsigned int *all_ctlr_rank_interleaving)
Kumar Gala124b0822008-08-26 15:01:29 -0500163{
164 int i, j;
165
166 /*
167 * If a reduced data width is requested, but the SPD
168 * specifies a physically wider device, adjust the
169 * computed dimm capacities accordingly before
170 * assigning addresses.
171 */
172 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
173 unsigned int found = 0;
174
175 switch (pinfo->memctl_opts[i].data_bus_width) {
176 case 2:
177 /* 16-bit */
York Sundd803dd2011-05-27 07:25:51 +0800178 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
179 unsigned int dw;
180 if (!pinfo->dimm_params[i][j].n_ranks)
181 continue;
182 dw = pinfo->dimm_params[i][j].primary_sdram_width;
183 if ((dw == 72 || dw == 64)) {
184 dbw_cap_adj[i] = 2;
185 break;
186 } else if ((dw == 40 || dw == 32)) {
187 dbw_cap_adj[i] = 1;
188 break;
189 }
190 }
Kumar Gala124b0822008-08-26 15:01:29 -0500191 break;
192
193 case 1:
194 /* 32-bit */
195 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
196 unsigned int dw;
197 dw = pinfo->dimm_params[i][j].data_width;
198 if (pinfo->dimm_params[i][j].n_ranks
199 && (dw == 72 || dw == 64)) {
200 /*
201 * FIXME: can't really do it
202 * like this because this just
203 * further reduces the memory
204 */
205 found = 1;
206 break;
207 }
208 }
209 if (found) {
210 dbw_cap_adj[i] = 1;
211 }
212 break;
213
214 case 0:
215 /* 64-bit */
216 break;
217
218 default:
219 printf("unexpected data bus width "
220 "specified controller %u\n", i);
221 return 1;
222 }
223 }
224
Kumar Gala124b0822008-08-26 15:01:29 -0500225 j = 0;
york93799ca2010-07-02 22:25:52 +0000226 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
227 if (pinfo->memctl_opts[i].memctl_interleaving)
Kumar Gala124b0822008-08-26 15:01:29 -0500228 j++;
york93799ca2010-07-02 22:25:52 +0000229 /*
230 * Not support less than all memory controllers interleaving
231 * if more than two controllers
232 */
233 if (j == CONFIG_NUM_DDR_CONTROLLERS)
234 *all_memctl_interleaving = 1;
Haiying Wangb834f922008-10-03 12:37:10 -0400235
Kumar Gala124b0822008-08-26 15:01:29 -0500236 /* Check that all controllers are rank interleaving. */
237 j = 0;
york93799ca2010-07-02 22:25:52 +0000238 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
239 if (pinfo->memctl_opts[i].ba_intlv_ctl)
Kumar Gala124b0822008-08-26 15:01:29 -0500240 j++;
york93799ca2010-07-02 22:25:52 +0000241 /*
242 * All memory controllers must be populated to qualify for
243 * all controller rank interleaving
244 */
245 if (j == CONFIG_NUM_DDR_CONTROLLERS)
246 *all_ctlr_rank_interleaving = 1;
Haiying Wangb834f922008-10-03 12:37:10 -0400247
york93799ca2010-07-02 22:25:52 +0000248 if (*all_memctl_interleaving) {
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500249 unsigned long long addr, total_mem_per_ctlr = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500250 /*
251 * If interleaving between memory controllers,
252 * make each controller start at a base address
253 * of 0.
254 *
255 * Also, if bank interleaving (chip select
256 * interleaving) is enabled on each memory
257 * controller, CS0 needs to be programmed to
258 * cover the entire memory range on that memory
259 * controller
260 *
261 * Bank interleaving also implies that each
262 * addressed chip select is identical in size.
263 */
264
265 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
266 addr = 0;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500267 pinfo->common_timing_params[i].base_address = 0ull;
Kumar Gala124b0822008-08-26 15:01:29 -0500268 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
269 unsigned long long cap
270 = pinfo->dimm_params[i][j].capacity;
271
272 pinfo->dimm_params[i][j].base_address = addr;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500273 addr += cap >> dbw_cap_adj[i];
Haiying Wang272b5962008-10-03 12:36:39 -0400274 total_mem_per_ctlr += cap >> dbw_cap_adj[i];
Kumar Gala124b0822008-08-26 15:01:29 -0500275 }
276 }
Haiying Wang272b5962008-10-03 12:36:39 -0400277 pinfo->common_timing_params[0].total_mem = total_mem_per_ctlr;
Kumar Gala124b0822008-08-26 15:01:29 -0500278 } else {
279 /*
280 * Simple linear assignment if memory
281 * controllers are not interleaved.
282 */
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500283 unsigned long long cur_memsize = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500284 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500285 u64 total_mem_per_ctlr = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500286 pinfo->common_timing_params[i].base_address =
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500287 cur_memsize;
Kumar Gala124b0822008-08-26 15:01:29 -0500288 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
289 /* Compute DIMM base addresses. */
290 unsigned long long cap =
291 pinfo->dimm_params[i][j].capacity;
Kumar Gala124b0822008-08-26 15:01:29 -0500292 pinfo->dimm_params[i][j].base_address =
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500293 cur_memsize;
Kumar Gala124b0822008-08-26 15:01:29 -0500294 cur_memsize += cap >> dbw_cap_adj[i];
295 total_mem_per_ctlr += cap >> dbw_cap_adj[i];
296 }
297 pinfo->common_timing_params[i].total_mem =
298 total_mem_per_ctlr;
299 }
300 }
301
302 return 0;
303}
304
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500305unsigned long long
Haiying Wang80ad4012010-12-01 10:35:31 -0500306fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
307 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -0500308{
309 unsigned int i, j;
310 unsigned int all_controllers_memctl_interleaving = 0;
311 unsigned int all_controllers_rank_interleaving = 0;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500312 unsigned long long total_mem = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500313
314 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
315 common_timing_params_t *timing_params = pinfo->common_timing_params;
316
317 /* data bus width capacity adjust shift amount */
318 unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
319
320 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
321 dbw_capacity_adjust[i] = 0;
322 }
323
324 debug("starting at step %u (%s)\n",
325 start_step, step_to_string(start_step));
326
327 switch (start_step) {
328 case STEP_GET_SPD:
York Sune73cc042011-06-07 09:42:16 +0800329#if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
Kumar Gala124b0822008-08-26 15:01:29 -0500330 /* STEP 1: Gather all DIMM SPD data */
331 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
332 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
333 }
334
335 case STEP_COMPUTE_DIMM_PARMS:
336 /* STEP 2: Compute DIMM parameters from SPD data */
337
338 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
339 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
340 unsigned int retval;
341 generic_spd_eeprom_t *spd =
342 &(pinfo->spd_installed_dimms[i][j]);
343 dimm_params_t *pdimm =
Wolfgang Denk9dbbd7b2008-09-13 02:23:05 +0200344 &(pinfo->dimm_params[i][j]);
Kumar Gala124b0822008-08-26 15:01:29 -0500345
346 retval = compute_dimm_parameters(spd, pdimm, i);
York Sun09d8aa82011-06-07 09:42:17 +0800347#ifdef CONFIG_SYS_DDR_RAW_TIMING
348 if (retval != 0) {
349 printf("SPD error! Trying fallback to "
350 "raw timing calculation\n");
351 fsl_ddr_get_dimm_params(pdimm, i, j);
352 }
353#else
Kumar Gala124b0822008-08-26 15:01:29 -0500354 if (retval == 2) {
355 printf("Error: compute_dimm_parameters"
356 " non-zero returned FATAL value "
357 "for memctl=%u dimm=%u\n", i, j);
358 return 0;
359 }
York Sun09d8aa82011-06-07 09:42:17 +0800360#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500361 if (retval) {
362 debug("Warning: compute_dimm_parameters"
363 " non-zero return value for memctl=%u "
364 "dimm=%u\n", i, j);
365 }
366 }
367 }
368
Shaohui Xie24009042012-06-28 23:36:38 +0000369#elif defined(CONFIG_SYS_DDR_RAW_TIMING)
York Sune73cc042011-06-07 09:42:16 +0800370 case STEP_COMPUTE_DIMM_PARMS:
371 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
372 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
373 dimm_params_t *pdimm =
374 &(pinfo->dimm_params[i][j]);
375 fsl_ddr_get_dimm_params(pdimm, i, j);
376 }
377 }
378 debug("Filling dimm parameters from board specific file\n");
379#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500380 case STEP_COMPUTE_COMMON_PARMS:
381 /*
382 * STEP 3: Compute a common set of timing parameters
383 * suitable for all of the DIMMs on each memory controller
384 */
385 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
386 debug("Computing lowest common DIMM"
387 " parameters for memctl=%u\n", i);
388 compute_lowest_common_dimm_parameters(
389 pinfo->dimm_params[i],
390 &timing_params[i],
391 CONFIG_DIMM_SLOTS_PER_CTLR);
392 }
393
394 case STEP_GATHER_OPTS:
395 /* STEP 4: Gather configuration requirements from user */
396 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
397 debug("Reloading memory controller "
398 "configuration options for memctl=%u\n", i);
399 /*
400 * This "reloads" the memory controller options
401 * to defaults. If the user "edits" an option,
402 * next_step points to the step after this,
403 * which is currently STEP_ASSIGN_ADDRESSES.
404 */
405 populate_memctl_options(
406 timing_params[i].all_DIMMs_registered,
Haiying Wangfa440362008-10-03 12:36:55 -0400407 &pinfo->memctl_opts[i],
408 pinfo->dimm_params[i], i);
Kumar Gala124b0822008-08-26 15:01:29 -0500409 }
york93799ca2010-07-02 22:25:52 +0000410 check_interleaving_options(pinfo);
Kumar Gala124b0822008-08-26 15:01:29 -0500411 case STEP_ASSIGN_ADDRESSES:
412 /* STEP 5: Assign addresses to chip selects */
413 step_assign_addresses(pinfo,
414 dbw_capacity_adjust,
415 &all_controllers_memctl_interleaving,
416 &all_controllers_rank_interleaving);
417
418 case STEP_COMPUTE_REGS:
419 /* STEP 6: compute controller register values */
420 debug("FSL Memory ctrl cg register computation\n");
421 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
422 if (timing_params[i].ndimms_present == 0) {
423 memset(&ddr_reg[i], 0,
424 sizeof(fsl_ddr_cfg_regs_t));
425 continue;
426 }
427
428 compute_fsl_memctl_config_regs(
429 &pinfo->memctl_opts[i],
430 &ddr_reg[i], &timing_params[i],
431 pinfo->dimm_params[i],
Haiying Wang80ad4012010-12-01 10:35:31 -0500432 dbw_capacity_adjust[i],
433 size_only);
Kumar Gala124b0822008-08-26 15:01:29 -0500434 }
435
436 default:
437 break;
438 }
439
440 /* Compute the total amount of memory. */
441
442 /*
443 * If bank interleaving but NOT memory controller interleaving
444 * CS_BNDS describe the quantity of memory on each memory
445 * controller, so the total is the sum across.
446 */
447 if (!all_controllers_memctl_interleaving
448 && all_controllers_rank_interleaving) {
449 total_mem = 0;
450 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
451 total_mem += timing_params[i].total_mem;
452 }
453
454 } else {
455 /*
456 * Compute the amount of memory available just by
457 * looking for the highest valid CSn_BNDS value.
458 * This allows us to also experiment with using
459 * only CS0 when using dual-rank DIMMs.
460 */
461 unsigned int max_end = 0;
462
463 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
464 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
465 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
466 if (reg->cs[j].config & 0x80000000) {
467 unsigned int end;
468 end = reg->cs[j].bnds & 0xFFF;
469 if (end > max_end) {
470 max_end = end;
471 }
472 }
473 }
474 }
475
Kumar Gala124b0822008-08-26 15:01:29 -0500476 total_mem = 1 + (((unsigned long long)max_end << 24ULL)
477 | 0xFFFFFFULL);
478 }
479
480 return total_mem;
481}
482
483/*
484 * fsl_ddr_sdram() -- this is the main function to be called by
485 * initdram() in the board file.
486 *
487 * It returns amount of memory configured in bytes.
488 */
489phys_size_t fsl_ddr_sdram(void)
490{
491 unsigned int i;
492 unsigned int memctl_interleaved;
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500493 unsigned long long total_memory;
Kumar Gala124b0822008-08-26 15:01:29 -0500494 fsl_ddr_info_t info;
495
496 /* Reset info structure. */
497 memset(&info, 0, sizeof(fsl_ddr_info_t));
498
499 /* Compute it once normally. */
York Sunbd495cf2011-09-16 13:21:35 -0700500#ifdef CONFIG_FSL_DDR_INTERACTIVE
501 if (getenv("ddr_interactive"))
502 total_memory = fsl_ddr_interactive(&info);
503 else
504#endif
505 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
Kumar Gala124b0822008-08-26 15:01:29 -0500506
507 /* Check for memory controller interleaving. */
508 memctl_interleaved = 0;
509 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
510 memctl_interleaved +=
511 info.memctl_opts[i].memctl_interleaving;
512 }
513
514 if (memctl_interleaved) {
515 if (memctl_interleaved == CONFIG_NUM_DDR_CONTROLLERS) {
516 debug("memctl interleaving\n");
517 /*
518 * Change the meaning of memctl_interleaved
519 * to be "boolean".
520 */
521 memctl_interleaved = 1;
522 } else {
Ed Swarthoutb135d932008-10-29 09:21:44 -0500523 printf("Warning: memctl interleaving not "
Kumar Gala124b0822008-08-26 15:01:29 -0500524 "properly configured on all controllers\n");
Ed Swarthoutb135d932008-10-29 09:21:44 -0500525 memctl_interleaved = 0;
526 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
527 info.memctl_opts[i].memctl_interleaving = 0;
528 debug("Recomputing with memctl_interleaving off.\n");
529 total_memory = fsl_ddr_compute(&info,
Haiying Wang80ad4012010-12-01 10:35:31 -0500530 STEP_ASSIGN_ADDRESSES,
531 0);
Kumar Gala124b0822008-08-26 15:01:29 -0500532 }
533 }
534
535 /* Program configuration registers. */
536 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
537 debug("Programming controller %u\n", i);
538 if (info.common_timing_params[i].ndimms_present == 0) {
539 debug("No dimms present on controller %u; "
540 "skipping programming\n", i);
541 continue;
542 }
543
544 fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i);
545 }
546
547 if (memctl_interleaved) {
548 const unsigned int ctrl_num = 0;
549
550 /* Only set LAWBAR1 if memory controller interleaving is on. */
551 fsl_ddr_set_lawbar(&info.common_timing_params[0],
552 memctl_interleaved, ctrl_num);
553 } else {
554 /*
555 * Memory controller interleaving is NOT on;
556 * set each lawbar individually.
557 */
558 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
559 fsl_ddr_set_lawbar(&info.common_timing_params[i],
560 0, i);
561 }
562 }
563
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500564 debug("total_memory = %llu\n", total_memory);
565
566#if !defined(CONFIG_PHYS_64BIT)
567 /* Check for 4G or more. Bad. */
568 if (total_memory >= (1ull << 32)) {
569 printf("Detected %lld MB of memory\n", total_memory >> 20);
Becky Bruce2d8ecac2010-12-17 17:17:59 -0600570 printf(" This U-Boot only supports < 4G of DDR\n");
571 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
572 printf(" "); /* re-align to match init_func_ram print */
Kumar Gala68ef4bd2009-06-11 23:42:35 -0500573 total_memory = CONFIG_MAX_MEM_MAPPED;
574 }
575#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500576
577 return total_memory;
578}
Haiying Wang80ad4012010-12-01 10:35:31 -0500579
580/*
581 * fsl_ddr_sdram_size() - This function only returns the size of the total
582 * memory without setting ddr control registers.
583 */
584phys_size_t
585fsl_ddr_sdram_size(void)
586{
587 fsl_ddr_info_t info;
588 unsigned long long total_memory = 0;
589
590 memset(&info, 0 , sizeof(fsl_ddr_info_t));
591
592 /* Compute it once normally. */
593 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
594
595 return total_memory;
596}