1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2008-2014 Freescale Semiconductor, Inc.
7 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
8 * Based on code from spd_sdram.c
9 * Author: James Yang [at freescale.com]
14 #include <fsl_ddr_sdram.h>
18 * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
19 * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for
20 * all Power SoCs. But it could be different for ARM SoCs. For example,
21 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
22 * 0x00_8000_0000 ~ 0x00_ffff_ffff
23 * 0x80_8000_0000 ~ 0xff_ffff_ffff
25 #ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
27 #define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE
29 #define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
34 #include <asm/fsl_law.h>
36 void fsl_ddr_set_lawbar(
37 const common_timing_params_t
*memctl_common_params
,
38 unsigned int memctl_interleaved
,
39 unsigned int ctrl_num
);
42 void fsl_ddr_set_intl3r(const unsigned int granule_size
);
43 #if defined(SPD_EEPROM_ADDRESS) || \
44 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
45 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
46 #if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
47 u8 spd_i2c_addr
[CONFIG_SYS_NUM_DDR_CTLRS
][CONFIG_DIMM_SLOTS_PER_CTLR
] = {
48 [0][0] = SPD_EEPROM_ADDRESS
,
50 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
51 u8 spd_i2c_addr
[CONFIG_SYS_NUM_DDR_CTLRS
][CONFIG_DIMM_SLOTS_PER_CTLR
] = {
52 [0][0] = SPD_EEPROM_ADDRESS1
, /* controller 1 */
53 [0][1] = SPD_EEPROM_ADDRESS2
, /* controller 1 */
55 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
56 u8 spd_i2c_addr
[CONFIG_SYS_NUM_DDR_CTLRS
][CONFIG_DIMM_SLOTS_PER_CTLR
] = {
57 [0][0] = SPD_EEPROM_ADDRESS1
, /* controller 1 */
58 [1][0] = SPD_EEPROM_ADDRESS2
, /* controller 2 */
60 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
61 u8 spd_i2c_addr
[CONFIG_SYS_NUM_DDR_CTLRS
][CONFIG_DIMM_SLOTS_PER_CTLR
] = {
62 [0][0] = SPD_EEPROM_ADDRESS1
, /* controller 1 */
63 [0][1] = SPD_EEPROM_ADDRESS2
, /* controller 1 */
64 [1][0] = SPD_EEPROM_ADDRESS3
, /* controller 2 */
65 [1][1] = SPD_EEPROM_ADDRESS4
, /* controller 2 */
67 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
68 u8 spd_i2c_addr
[CONFIG_SYS_NUM_DDR_CTLRS
][CONFIG_DIMM_SLOTS_PER_CTLR
] = {
69 [0][0] = SPD_EEPROM_ADDRESS1
, /* controller 1 */
70 [1][0] = SPD_EEPROM_ADDRESS2
, /* controller 2 */
71 [2][0] = SPD_EEPROM_ADDRESS3
, /* controller 3 */
73 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
74 u8 spd_i2c_addr
[CONFIG_SYS_NUM_DDR_CTLRS
][CONFIG_DIMM_SLOTS_PER_CTLR
] = {
75 [0][0] = SPD_EEPROM_ADDRESS1
, /* controller 1 */
76 [0][1] = SPD_EEPROM_ADDRESS2
, /* controller 1 */
77 [1][0] = SPD_EEPROM_ADDRESS3
, /* controller 2 */
78 [1][1] = SPD_EEPROM_ADDRESS4
, /* controller 2 */
79 [2][0] = SPD_EEPROM_ADDRESS5
, /* controller 3 */
80 [2][1] = SPD_EEPROM_ADDRESS6
, /* controller 3 */
85 #define SPD_SPA0_ADDRESS 0x36
86 #define SPD_SPA1_ADDRESS 0x37
88 static void __get_spd(generic_spd_eeprom_t
*spd
, u8 i2c_address
)
91 #ifdef CONFIG_SYS_FSL_DDR4
96 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM
);
100 #ifdef CONFIG_SYS_FSL_DDR4
102 * DDR4 SPD has 384 to 512 bytes
103 * To access the lower 256 bytes, we need to set EE page address to 0
104 * To access the upper 256 bytes, we need to set EE page address to 1
105 * See Jedec standar No. 21-C for detail
107 #ifndef CONFIG_DM_I2C
108 i2c_write(SPD_SPA0_ADDRESS
, 0, 1, &dummy
, 1);
109 ret
= i2c_read(i2c_address
, 0, 1, (uchar
*)spd
, 256);
111 i2c_write(SPD_SPA1_ADDRESS
, 0, 1, &dummy
, 1);
112 ret
= i2c_read(i2c_address
, 0, 1,
113 (uchar
*)((ulong
)spd
+ 256),
115 (int)sizeof(generic_spd_eeprom_t
) - 256));
119 int read_len
= min(256, (int)sizeof(generic_spd_eeprom_t
) - 256);
121 ret
= i2c_get_chip_for_busnum(0, SPD_SPA0_ADDRESS
, 1, &dev
);
123 dm_i2c_write(dev
, 0, &dummy
, 1);
124 ret
= i2c_get_chip_for_busnum(0, i2c_address
, 1, &dev
);
126 if (!dm_i2c_read(dev
, 0, (uchar
*)spd
, 256)) {
127 if (!i2c_get_chip_for_busnum(0, SPD_SPA1_ADDRESS
,
129 dm_i2c_write(dev
, 0, &dummy
, 1);
130 if (!i2c_get_chip_for_busnum(0, i2c_address
, 1, &dev
))
131 ret
= dm_i2c_read(dev
, 0,
132 (uchar
*)((ulong
)spd
+ 256),
140 #ifndef CONFIG_DM_I2C
141 ret
= i2c_read(i2c_address
, 0, 1, (uchar
*)spd
,
142 sizeof(generic_spd_eeprom_t
));
144 ret
= i2c_get_chip_for_busnum(0, i2c_address
, 1, &dev
);
146 ret
= dm_i2c_read(dev
, 0, (uchar
*)spd
,
147 sizeof(generic_spd_eeprom_t
));
154 #ifdef SPD_EEPROM_ADDRESS
156 #elif defined(SPD_EEPROM_ADDRESS1)
160 printf("DDR: failed to read SPD from address %u\n",
163 debug("DDR: failed to read SPD from address %u\n",
166 memset(spd
, 0, sizeof(generic_spd_eeprom_t
));
170 __attribute__((weak
, alias("__get_spd")))
171 void get_spd(generic_spd_eeprom_t
*spd
, u8 i2c_address
);
173 /* This function allows boards to update SPD address */
174 __weak
void update_spd_address(unsigned int ctrl_num
,
180 void fsl_ddr_get_spd(generic_spd_eeprom_t
*ctrl_dimms_spd
,
181 unsigned int ctrl_num
, unsigned int dimm_slots_per_ctrl
)
184 unsigned int i2c_address
= 0;
186 if (ctrl_num
>= CONFIG_SYS_NUM_DDR_CTLRS
) {
187 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__
, ctrl_num
);
191 for (i
= 0; i
< dimm_slots_per_ctrl
; i
++) {
192 i2c_address
= spd_i2c_addr
[ctrl_num
][i
];
193 update_spd_address(ctrl_num
, i
, &i2c_address
);
194 get_spd(&(ctrl_dimms_spd
[i
]), i2c_address
);
198 void fsl_ddr_get_spd(generic_spd_eeprom_t
*ctrl_dimms_spd
,
199 unsigned int ctrl_num
, unsigned int dimm_slots_per_ctrl
)
202 #endif /* SPD_EEPROM_ADDRESSx */
206 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
207 * - Same memory data bus width on all controllers
211 * The memory controller and associated documentation use confusing
212 * terminology when referring to the orgranization of DRAM.
214 * Here is a terminology translation table:
216 * memory controller/documention |industry |this code |signals
217 * -------------------------------|-----------|-----------|-----------------
218 * physical bank/bank |rank |rank |chip select (CS)
219 * logical bank/sub-bank |bank |bank |bank address (BA)
220 * page/row |row |page |row address
221 * ??? |column |column |column address
223 * The naming confusion is further exacerbated by the descriptions of the
224 * memory controller interleaving feature, where accesses are interleaved
225 * _BETWEEN_ two seperate memory controllers. This is configured only in
226 * CS0_CONFIG[INTLV_CTL] of each memory controller.
228 * memory controller documentation | number of chip selects
229 * | per memory controller supported
230 * --------------------------------|-----------------------------------------
231 * cache line interleaving | 1 (CS0 only)
232 * page interleaving | 1 (CS0 only)
233 * bank interleaving | 1 (CS0 only)
234 * superbank interleraving | depends on bank (chip select)
235 * | interleraving [rank interleaving]
236 * | mode used on every memory controller
238 * Even further confusing is the existence of the interleaving feature
239 * _WITHIN_ each memory controller. The feature is referred to in
240 * documentation as chip select interleaving or bank interleaving,
241 * although it is configured in the DDR_SDRAM_CFG field.
243 * Name of field | documentation name | this code
244 * -----------------------------|-----------------------|------------------
245 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
249 const char *step_string_tbl
[] = {
251 "STEP_COMPUTE_DIMM_PARMS",
252 "STEP_COMPUTE_COMMON_PARMS",
254 "STEP_ASSIGN_ADDRESSES",
260 const char * step_to_string(unsigned int step
) {
262 unsigned int s
= __ilog2(step
);
264 if ((1 << s
) != step
)
265 return step_string_tbl
[7];
267 if (s
>= ARRAY_SIZE(step_string_tbl
)) {
268 printf("Error for the step in %s\n", __func__
);
272 return step_string_tbl
[s
];
275 static unsigned long long __step_assign_addresses(fsl_ddr_info_t
*pinfo
,
276 unsigned int dbw_cap_adj
[])
279 unsigned long long total_mem
, current_mem_base
, total_ctlr_mem
;
280 unsigned long long rank_density
, ctlr_density
= 0;
281 unsigned int first_ctrl
= pinfo
->first_ctrl
;
282 unsigned int last_ctrl
= first_ctrl
+ pinfo
->num_ctrls
- 1;
285 * If a reduced data width is requested, but the SPD
286 * specifies a physically wider device, adjust the
287 * computed dimm capacities accordingly before
288 * assigning addresses.
290 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
291 unsigned int found
= 0;
293 switch (pinfo
->memctl_opts
[i
].data_bus_width
) {
296 for (j
= 0; j
< CONFIG_DIMM_SLOTS_PER_CTLR
; j
++) {
298 if (!pinfo
->dimm_params
[i
][j
].n_ranks
)
300 dw
= pinfo
->dimm_params
[i
][j
].primary_sdram_width
;
301 if ((dw
== 72 || dw
== 64)) {
304 } else if ((dw
== 40 || dw
== 32)) {
313 for (j
= 0; j
< CONFIG_DIMM_SLOTS_PER_CTLR
; j
++) {
315 dw
= pinfo
->dimm_params
[i
][j
].data_width
;
316 if (pinfo
->dimm_params
[i
][j
].n_ranks
317 && (dw
== 72 || dw
== 64)) {
319 * FIXME: can't really do it
320 * like this because this just
321 * further reduces the memory
337 printf("unexpected data bus width "
338 "specified controller %u\n", i
);
341 debug("dbw_cap_adj[%d]=%d\n", i
, dbw_cap_adj
[i
]);
344 current_mem_base
= pinfo
->mem_base
;
346 if (pinfo
->memctl_opts
[first_ctrl
].memctl_interleaving
) {
347 rank_density
= pinfo
->dimm_params
[first_ctrl
][0].rank_density
>>
348 dbw_cap_adj
[first_ctrl
];
349 switch (pinfo
->memctl_opts
[first_ctrl
].ba_intlv_ctl
&
350 FSL_DDR_CS0_CS1_CS2_CS3
) {
351 case FSL_DDR_CS0_CS1_CS2_CS3
:
352 ctlr_density
= 4 * rank_density
;
354 case FSL_DDR_CS0_CS1
:
355 case FSL_DDR_CS0_CS1_AND_CS2_CS3
:
356 ctlr_density
= 2 * rank_density
;
358 case FSL_DDR_CS2_CS3
:
360 ctlr_density
= rank_density
;
363 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
364 rank_density
, ctlr_density
);
365 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
366 if (pinfo
->memctl_opts
[i
].memctl_interleaving
) {
367 switch (pinfo
->memctl_opts
[i
].memctl_interleaving_mode
) {
368 case FSL_DDR_256B_INTERLEAVING
:
369 case FSL_DDR_CACHE_LINE_INTERLEAVING
:
370 case FSL_DDR_PAGE_INTERLEAVING
:
371 case FSL_DDR_BANK_INTERLEAVING
:
372 case FSL_DDR_SUPERBANK_INTERLEAVING
:
373 total_ctlr_mem
= 2 * ctlr_density
;
375 case FSL_DDR_3WAY_1KB_INTERLEAVING
:
376 case FSL_DDR_3WAY_4KB_INTERLEAVING
:
377 case FSL_DDR_3WAY_8KB_INTERLEAVING
:
378 total_ctlr_mem
= 3 * ctlr_density
;
380 case FSL_DDR_4WAY_1KB_INTERLEAVING
:
381 case FSL_DDR_4WAY_4KB_INTERLEAVING
:
382 case FSL_DDR_4WAY_8KB_INTERLEAVING
:
383 total_ctlr_mem
= 4 * ctlr_density
;
386 panic("Unknown interleaving mode");
388 pinfo
->common_timing_params
[i
].base_address
=
390 pinfo
->common_timing_params
[i
].total_mem
=
392 total_mem
= current_mem_base
+ total_ctlr_mem
;
393 debug("ctrl %d base 0x%llx\n", i
, current_mem_base
);
394 debug("ctrl %d total 0x%llx\n", i
, total_ctlr_mem
);
396 /* when 3rd controller not interleaved */
397 current_mem_base
= total_mem
;
399 pinfo
->common_timing_params
[i
].base_address
=
401 for (j
= 0; j
< CONFIG_DIMM_SLOTS_PER_CTLR
; j
++) {
402 unsigned long long cap
=
403 pinfo
->dimm_params
[i
][j
].capacity
>> dbw_cap_adj
[i
];
404 pinfo
->dimm_params
[i
][j
].base_address
=
406 debug("ctrl %d dimm %d base 0x%llx\n", i
, j
, current_mem_base
);
407 current_mem_base
+= cap
;
408 total_ctlr_mem
+= cap
;
410 debug("ctrl %d total 0x%llx\n", i
, total_ctlr_mem
);
411 pinfo
->common_timing_params
[i
].total_mem
=
413 total_mem
+= total_ctlr_mem
;
418 * Simple linear assignment if memory
419 * controllers are not interleaved.
421 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
423 pinfo
->common_timing_params
[i
].base_address
=
425 for (j
= 0; j
< CONFIG_DIMM_SLOTS_PER_CTLR
; j
++) {
426 /* Compute DIMM base addresses. */
427 unsigned long long cap
=
428 pinfo
->dimm_params
[i
][j
].capacity
>> dbw_cap_adj
[i
];
429 pinfo
->dimm_params
[i
][j
].base_address
=
431 debug("ctrl %d dimm %d base 0x%llx\n", i
, j
, current_mem_base
);
432 current_mem_base
+= cap
;
433 total_ctlr_mem
+= cap
;
435 debug("ctrl %d total 0x%llx\n", i
, total_ctlr_mem
);
436 pinfo
->common_timing_params
[i
].total_mem
=
438 total_mem
+= total_ctlr_mem
;
441 debug("Total mem by %s is 0x%llx\n", __func__
, total_mem
);
446 /* Use weak function to allow board file to override the address assignment */
447 __attribute__((weak
, alias("__step_assign_addresses")))
448 unsigned long long step_assign_addresses(fsl_ddr_info_t
*pinfo
,
449 unsigned int dbw_cap_adj
[]);
452 fsl_ddr_compute(fsl_ddr_info_t
*pinfo
, unsigned int start_step
,
453 unsigned int size_only
)
456 unsigned long long total_mem
= 0;
457 int assert_reset
= 0;
458 unsigned int first_ctrl
= pinfo
->first_ctrl
;
459 unsigned int last_ctrl
= first_ctrl
+ pinfo
->num_ctrls
- 1;
460 __maybe_unused
int retval
;
461 __maybe_unused
bool goodspd
= false;
462 __maybe_unused
int dimm_slots_per_ctrl
= pinfo
->dimm_slots_per_ctrl
;
464 fsl_ddr_cfg_regs_t
*ddr_reg
= pinfo
->fsl_ddr_config_reg
;
465 common_timing_params_t
*timing_params
= pinfo
->common_timing_params
;
466 if (pinfo
->board_need_mem_reset
)
467 assert_reset
= pinfo
->board_need_mem_reset();
469 /* data bus width capacity adjust shift amount */
470 unsigned int dbw_capacity_adjust
[CONFIG_SYS_NUM_DDR_CTLRS
];
472 for (i
= first_ctrl
; i
<= last_ctrl
; i
++)
473 dbw_capacity_adjust
[i
] = 0;
475 debug("starting at step %u (%s)\n",
476 start_step
, step_to_string(start_step
));
478 switch (start_step
) {
480 #if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
481 /* STEP 1: Gather all DIMM SPD data */
482 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
483 fsl_ddr_get_spd(pinfo
->spd_installed_dimms
[i
], i
,
484 dimm_slots_per_ctrl
);
487 case STEP_COMPUTE_DIMM_PARMS
:
488 /* STEP 2: Compute DIMM parameters from SPD data */
490 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
491 for (j
= 0; j
< CONFIG_DIMM_SLOTS_PER_CTLR
; j
++) {
492 generic_spd_eeprom_t
*spd
=
493 &(pinfo
->spd_installed_dimms
[i
][j
]);
494 dimm_params_t
*pdimm
=
495 &(pinfo
->dimm_params
[i
][j
]);
496 retval
= compute_dimm_parameters(
498 #ifdef CONFIG_SYS_DDR_RAW_TIMING
500 printf("SPD error on controller %d! "
501 "Trying fallback to raw timing "
503 retval
= fsl_ddr_get_dimm_params(pdimm
,
508 printf("Error: compute_dimm_parameters"
509 " non-zero returned FATAL value "
510 "for memctl=%u dimm=%u\n", i
, j
);
515 debug("Warning: compute_dimm_parameters"
516 " non-zero return value for memctl=%u "
526 * Throw an error if this is for main memory, i.e.
527 * first_ctrl == 0. Otherwise, siliently return 0
528 * as the memory size.
531 printf("Error: No valid SPD detected.\n");
535 #elif defined(CONFIG_SYS_DDR_RAW_TIMING)
536 case STEP_COMPUTE_DIMM_PARMS
:
537 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
538 for (j
= 0; j
< CONFIG_DIMM_SLOTS_PER_CTLR
; j
++) {
539 dimm_params_t
*pdimm
=
540 &(pinfo
->dimm_params
[i
][j
]);
541 fsl_ddr_get_dimm_params(pdimm
, i
, j
);
544 debug("Filling dimm parameters from board specific file\n");
546 case STEP_COMPUTE_COMMON_PARMS
:
548 * STEP 3: Compute a common set of timing parameters
549 * suitable for all of the DIMMs on each memory controller
551 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
552 debug("Computing lowest common DIMM"
553 " parameters for memctl=%u\n", i
);
554 compute_lowest_common_dimm_parameters
556 pinfo
->dimm_params
[i
],
558 CONFIG_DIMM_SLOTS_PER_CTLR
);
561 case STEP_GATHER_OPTS
:
562 /* STEP 4: Gather configuration requirements from user */
563 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
564 debug("Reloading memory controller "
565 "configuration options for memctl=%u\n", i
);
567 * This "reloads" the memory controller options
568 * to defaults. If the user "edits" an option,
569 * next_step points to the step after this,
570 * which is currently STEP_ASSIGN_ADDRESSES.
572 populate_memctl_options(
574 &pinfo
->memctl_opts
[i
],
575 pinfo
->dimm_params
[i
], i
);
577 * For RDIMMs, JEDEC spec requires clocks to be stable
578 * before reset signal is deasserted. For the boards
579 * using fixed parameters, this function should be
580 * be called from board init file.
582 if (timing_params
[i
].all_dimms_registered
)
585 if (assert_reset
&& !size_only
) {
586 if (pinfo
->board_mem_reset
) {
587 debug("Asserting mem reset\n");
588 pinfo
->board_mem_reset();
590 debug("Asserting mem reset missing\n");
594 case STEP_ASSIGN_ADDRESSES
:
595 /* STEP 5: Assign addresses to chip selects */
596 check_interleaving_options(pinfo
);
597 total_mem
= step_assign_addresses(pinfo
, dbw_capacity_adjust
);
598 debug("Total mem %llu assigned\n", total_mem
);
600 case STEP_COMPUTE_REGS
:
601 /* STEP 6: compute controller register values */
602 debug("FSL Memory ctrl register computation\n");
603 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
604 if (timing_params
[i
].ndimms_present
== 0) {
605 memset(&ddr_reg
[i
], 0,
606 sizeof(fsl_ddr_cfg_regs_t
));
610 compute_fsl_memctl_config_regs
612 &pinfo
->memctl_opts
[i
],
613 &ddr_reg
[i
], &timing_params
[i
],
614 pinfo
->dimm_params
[i
],
615 dbw_capacity_adjust
[i
],
625 * Compute the amount of memory available just by
626 * looking for the highest valid CSn_BNDS value.
627 * This allows us to also experiment with using
628 * only CS0 when using dual-rank DIMMs.
630 unsigned int max_end
= 0;
632 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
633 for (j
= 0; j
< CONFIG_CHIP_SELECTS_PER_CTRL
; j
++) {
634 fsl_ddr_cfg_regs_t
*reg
= &ddr_reg
[i
];
635 if (reg
->cs
[j
].config
& 0x80000000) {
638 * 0xfffffff is a special value we put
641 if (reg
->cs
[j
].bnds
== 0xffffffff)
643 end
= reg
->cs
[j
].bnds
& 0xffff;
651 total_mem
= 1 + (((unsigned long long)max_end
<< 24ULL) |
652 0xFFFFFFULL
) - pinfo
->mem_base
;
658 phys_size_t
__fsl_ddr_sdram(fsl_ddr_info_t
*pinfo
)
660 unsigned int i
, first_ctrl
, last_ctrl
;
662 unsigned int law_memctl
= LAW_TRGT_IF_DDR_1
;
664 unsigned long long total_memory
;
665 int deassert_reset
= 0;
667 first_ctrl
= pinfo
->first_ctrl
;
668 last_ctrl
= first_ctrl
+ pinfo
->num_ctrls
- 1;
670 /* Compute it once normally. */
671 #ifdef CONFIG_FSL_DDR_INTERACTIVE
672 if (tstc() && (getc() == 'd')) { /* we got a key press of 'd' */
673 total_memory
= fsl_ddr_interactive(pinfo
, 0);
674 } else if (fsl_ddr_interactive_env_var_exists()) {
675 total_memory
= fsl_ddr_interactive(pinfo
, 1);
678 total_memory
= fsl_ddr_compute(pinfo
, STEP_GET_SPD
, 0);
680 /* setup 3-way interleaving before enabling DDRC */
681 switch (pinfo
->memctl_opts
[first_ctrl
].memctl_interleaving_mode
) {
682 case FSL_DDR_3WAY_1KB_INTERLEAVING
:
683 case FSL_DDR_3WAY_4KB_INTERLEAVING
:
684 case FSL_DDR_3WAY_8KB_INTERLEAVING
:
686 pinfo
->memctl_opts
[first_ctrl
].
687 memctl_interleaving_mode
);
694 * Program configuration registers.
695 * JEDEC specs requires clocks to be stable before deasserting reset
696 * for RDIMMs. Clocks start after chip select is enabled and clock
697 * control register is set. During step 1, all controllers have their
698 * registers set but not enabled. Step 2 proceeds after deasserting
699 * reset through board FPGA or GPIO.
700 * For non-registered DIMMs, initialization can go through but it is
701 * also OK to follow the same flow.
703 if (pinfo
->board_need_mem_reset
)
704 deassert_reset
= pinfo
->board_need_mem_reset();
705 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
706 if (pinfo
->common_timing_params
[i
].all_dimms_registered
)
709 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
710 debug("Programming controller %u\n", i
);
711 if (pinfo
->common_timing_params
[i
].ndimms_present
== 0) {
712 debug("No dimms present on controller %u; "
713 "skipping programming\n", i
);
717 * The following call with step = 1 returns before enabling
718 * the controller. It has to finish with step = 2 later.
720 fsl_ddr_set_memctl_regs(&(pinfo
->fsl_ddr_config_reg
[i
]), i
,
721 deassert_reset
? 1 : 0);
723 if (deassert_reset
) {
724 /* Use board FPGA or GPIO to deassert reset signal */
725 if (pinfo
->board_mem_de_reset
) {
726 debug("Deasserting mem reset\n");
727 pinfo
->board_mem_de_reset();
729 debug("Deasserting mem reset missing\n");
731 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
732 /* Call with step = 2 to continue initialization */
733 fsl_ddr_set_memctl_regs(&(pinfo
->fsl_ddr_config_reg
[i
]),
738 #ifdef CONFIG_FSL_DDR_SYNC_REFRESH
739 fsl_ddr_sync_memctl_refresh(first_ctrl
, last_ctrl
);
744 for (i
= first_ctrl
; i
<= last_ctrl
; i
++) {
745 if (pinfo
->memctl_opts
[i
].memctl_interleaving
) {
746 switch (pinfo
->memctl_opts
[i
].
747 memctl_interleaving_mode
) {
748 case FSL_DDR_CACHE_LINE_INTERLEAVING
:
749 case FSL_DDR_PAGE_INTERLEAVING
:
750 case FSL_DDR_BANK_INTERLEAVING
:
751 case FSL_DDR_SUPERBANK_INTERLEAVING
:
755 law_memctl
= LAW_TRGT_IF_DDR_INTRLV
;
757 &pinfo
->common_timing_params
[i
],
760 #if CONFIG_SYS_NUM_DDR_CTLRS > 3
762 law_memctl
= LAW_TRGT_IF_DDR_INTLV_34
;
764 &pinfo
->common_timing_params
[i
],
769 case FSL_DDR_3WAY_1KB_INTERLEAVING
:
770 case FSL_DDR_3WAY_4KB_INTERLEAVING
:
771 case FSL_DDR_3WAY_8KB_INTERLEAVING
:
772 law_memctl
= LAW_TRGT_IF_DDR_INTLV_123
;
775 &pinfo
->common_timing_params
[i
],
779 case FSL_DDR_4WAY_1KB_INTERLEAVING
:
780 case FSL_DDR_4WAY_4KB_INTERLEAVING
:
781 case FSL_DDR_4WAY_8KB_INTERLEAVING
:
782 law_memctl
= LAW_TRGT_IF_DDR_INTLV_1234
;
785 &pinfo
->common_timing_params
[i
],
787 /* place holder for future 4-way interleaving */
795 law_memctl
= LAW_TRGT_IF_DDR_1
;
798 law_memctl
= LAW_TRGT_IF_DDR_2
;
801 law_memctl
= LAW_TRGT_IF_DDR_3
;
804 law_memctl
= LAW_TRGT_IF_DDR_4
;
809 fsl_ddr_set_lawbar(&pinfo
->common_timing_params
[i
],
815 debug("total_memory by %s = %llu\n", __func__
, total_memory
);
817 #if !defined(CONFIG_PHYS_64BIT)
818 /* Check for 4G or more. Bad. */
819 if ((first_ctrl
== 0) && (total_memory
>= (1ull << 32))) {
821 print_size(total_memory
, " of memory\n");
822 printf(" This U-Boot only supports < 4G of DDR\n");
823 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
824 printf(" "); /* re-align to match init_dram print */
825 total_memory
= CONFIG_MAX_MEM_MAPPED
;
833 * fsl_ddr_sdram(void) -- this is the main function to be
834 * called by dram_init() in the board file.
836 * It returns amount of memory configured in bytes.
838 phys_size_t
fsl_ddr_sdram(void)
842 /* Reset info structure. */
843 memset(&info
, 0, sizeof(fsl_ddr_info_t
));
844 info
.mem_base
= CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
;
846 info
.num_ctrls
= CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS
;
847 info
.dimm_slots_per_ctrl
= CONFIG_DIMM_SLOTS_PER_CTLR
;
848 info
.board_need_mem_reset
= board_need_mem_reset
;
849 info
.board_mem_reset
= board_assert_mem_reset
;
850 info
.board_mem_de_reset
= board_deassert_mem_reset
;
851 remove_unused_controllers(&info
);
853 return __fsl_ddr_sdram(&info
);
856 #ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
857 phys_size_t
fsl_other_ddr_sdram(unsigned long long base
,
858 unsigned int first_ctrl
,
859 unsigned int num_ctrls
,
860 unsigned int dimm_slots_per_ctrl
,
861 int (*board_need_reset
)(void),
862 void (*board_reset
)(void),
863 void (*board_de_reset
)(void))
867 /* Reset info structure. */
868 memset(&info
, 0, sizeof(fsl_ddr_info_t
));
869 info
.mem_base
= base
;
870 info
.first_ctrl
= first_ctrl
;
871 info
.num_ctrls
= num_ctrls
;
872 info
.dimm_slots_per_ctrl
= dimm_slots_per_ctrl
;
873 info
.board_need_mem_reset
= board_need_reset
;
874 info
.board_mem_reset
= board_reset
;
875 info
.board_mem_de_reset
= board_de_reset
;
877 return __fsl_ddr_sdram(&info
);
882 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
883 * size of the total memory without setting ddr control registers.
886 fsl_ddr_sdram_size(void)
889 unsigned long long total_memory
= 0;
891 memset(&info
, 0 , sizeof(fsl_ddr_info_t
));
892 info
.mem_base
= CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
;
894 info
.num_ctrls
= CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS
;
895 info
.dimm_slots_per_ctrl
= CONFIG_DIMM_SLOTS_PER_CTLR
;
896 info
.board_need_mem_reset
= NULL
;
897 remove_unused_controllers(&info
);
899 /* Compute it once normally. */
900 total_memory
= fsl_ddr_compute(&info
, STEP_GET_SPD
, 1);