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]
15 #include <fsl_ddr_sdram.h>
19 #include <asm/bitops.h>
22 * CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
23 * of DDR controllers. It is the same as CONFIG_SYS_DDR_SDRAM_BASE for
24 * all Power SoCs. But it could be different for ARM SoCs. For example,
25 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
26 * 0x00_8000_0000 ~ 0x00_ffff_ffff
27 * 0x80_8000_0000 ~ 0xff_ffff_ffff
29 #ifndef CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY
31 #define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_SDRAM_BASE
33 #define CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY CONFIG_SYS_DDR_SDRAM_BASE
38 #include <asm/fsl_law.h>
40 void fsl_ddr_set_lawbar(
41 const common_timing_params_t *memctl_common_params,
42 unsigned int memctl_interleaved,
43 unsigned int ctrl_num);
46 void fsl_ddr_set_intl3r(const unsigned int granule_size);
47 #if defined(SPD_EEPROM_ADDRESS) || \
48 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
49 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
50 #if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
51 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
52 [0][0] = SPD_EEPROM_ADDRESS,
54 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
55 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
56 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
57 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
59 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
60 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
61 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
62 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
64 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
65 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
66 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
67 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
68 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
69 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
71 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
72 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
73 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
74 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
75 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
77 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
78 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
79 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
80 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
81 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
82 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
83 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
84 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
89 #if defined(CONFIG_DM_I2C)
90 #define DEV_TYPE struct udevice
97 #define DEV_TYPE struct ludevice
101 #define SPD_SPA0_ADDRESS 0x36
102 #define SPD_SPA1_ADDRESS 0x37
104 static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
105 int alen, uint8_t *buf, int len)
110 ret = dm_i2c_read(dev, 0, buf, len);
112 ret = i2c_read(dev->chip, addr, alen, buf, len);
118 #ifdef CONFIG_SYS_FSL_DDR4
119 static int ddr_i2c_dummy_write(unsigned int chip_addr)
127 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, chip_addr,
130 printf("%s: Cannot find udev for a bus %d\n", __func__,
131 CONFIG_SYS_SPD_BUS_NUM);
135 return dm_i2c_write(dev, 0, &buf, 1);
137 return i2c_write(chip_addr, 0, 1, &buf, 1);
144 static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
149 #if defined(CONFIG_DM_I2C)
150 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, i2c_address,
153 printf("%s: Cannot find udev for a bus %d\n", __func__,
154 CONFIG_SYS_SPD_BUS_NUM);
157 #else /* Non DM I2C support - will be removed */
158 struct ludevice ldev = {
163 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
166 #ifdef CONFIG_SYS_FSL_DDR4
168 * DDR4 SPD has 384 to 512 bytes
169 * To access the lower 256 bytes, we need to set EE page address to 0
170 * To access the upper 256 bytes, we need to set EE page address to 1
171 * See Jedec standar No. 21-C for detail
173 ddr_i2c_dummy_write(SPD_SPA0_ADDRESS);
174 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd, 256);
176 ddr_i2c_dummy_write(SPD_SPA1_ADDRESS);
177 ret = ddr_i2c_read(dev, 0, 1, (uchar *)((ulong)spd + 256),
179 (int)sizeof(generic_spd_eeprom_t)
184 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
185 sizeof(generic_spd_eeprom_t));
190 #ifdef SPD_EEPROM_ADDRESS
192 #elif defined(SPD_EEPROM_ADDRESS1)
196 printf("DDR: failed to read SPD from address %u\n",
199 debug("DDR: failed to read SPD from address %u\n",
202 memset(spd, 0, sizeof(generic_spd_eeprom_t));
206 __attribute__((weak, alias("__get_spd")))
207 void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
209 /* This function allows boards to update SPD address */
210 __weak void update_spd_address(unsigned int ctrl_num,
216 void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
217 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
220 unsigned int i2c_address = 0;
222 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
223 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
227 for (i = 0; i < dimm_slots_per_ctrl; i++) {
228 i2c_address = spd_i2c_addr[ctrl_num][i];
229 update_spd_address(ctrl_num, i, &i2c_address);
230 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
234 void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
235 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
238 #endif /* SPD_EEPROM_ADDRESSx */
242 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
243 * - Same memory data bus width on all controllers
247 * The memory controller and associated documentation use confusing
248 * terminology when referring to the orgranization of DRAM.
250 * Here is a terminology translation table:
252 * memory controller/documention |industry |this code |signals
253 * -------------------------------|-----------|-----------|-----------------
254 * physical bank/bank |rank |rank |chip select (CS)
255 * logical bank/sub-bank |bank |bank |bank address (BA)
256 * page/row |row |page |row address
257 * ??? |column |column |column address
259 * The naming confusion is further exacerbated by the descriptions of the
260 * memory controller interleaving feature, where accesses are interleaved
261 * _BETWEEN_ two seperate memory controllers. This is configured only in
262 * CS0_CONFIG[INTLV_CTL] of each memory controller.
264 * memory controller documentation | number of chip selects
265 * | per memory controller supported
266 * --------------------------------|-----------------------------------------
267 * cache line interleaving | 1 (CS0 only)
268 * page interleaving | 1 (CS0 only)
269 * bank interleaving | 1 (CS0 only)
270 * superbank interleraving | depends on bank (chip select)
271 * | interleraving [rank interleaving]
272 * | mode used on every memory controller
274 * Even further confusing is the existence of the interleaving feature
275 * _WITHIN_ each memory controller. The feature is referred to in
276 * documentation as chip select interleaving or bank interleaving,
277 * although it is configured in the DDR_SDRAM_CFG field.
279 * Name of field | documentation name | this code
280 * -----------------------------|-----------------------|------------------
281 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
285 const char *step_string_tbl[] = {
287 "STEP_COMPUTE_DIMM_PARMS",
288 "STEP_COMPUTE_COMMON_PARMS",
290 "STEP_ASSIGN_ADDRESSES",
296 const char * step_to_string(unsigned int step) {
298 unsigned int s = __ilog2(step);
300 if ((1 << s) != step)
301 return step_string_tbl[7];
303 if (s >= ARRAY_SIZE(step_string_tbl)) {
304 printf("Error for the step in %s\n", __func__);
308 return step_string_tbl[s];
311 static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
312 unsigned int dbw_cap_adj[])
315 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
316 unsigned long long rank_density, ctlr_density = 0;
317 unsigned int first_ctrl = pinfo->first_ctrl;
318 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
321 * If a reduced data width is requested, but the SPD
322 * specifies a physically wider device, adjust the
323 * computed dimm capacities accordingly before
324 * assigning addresses.
326 for (i = first_ctrl; i <= last_ctrl; i++) {
327 unsigned int found = 0;
329 switch (pinfo->memctl_opts[i].data_bus_width) {
332 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
334 if (!pinfo->dimm_params[i][j].n_ranks)
336 dw = pinfo->dimm_params[i][j].primary_sdram_width;
337 if ((dw == 72 || dw == 64)) {
340 } else if ((dw == 40 || dw == 32)) {
349 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
351 dw = pinfo->dimm_params[i][j].data_width;
352 if (pinfo->dimm_params[i][j].n_ranks
353 && (dw == 72 || dw == 64)) {
355 * FIXME: can't really do it
356 * like this because this just
357 * further reduces the memory
373 printf("unexpected data bus width "
374 "specified controller %u\n", i);
377 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
380 current_mem_base = pinfo->mem_base;
382 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
383 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
384 dbw_cap_adj[first_ctrl];
385 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
386 FSL_DDR_CS0_CS1_CS2_CS3) {
387 case FSL_DDR_CS0_CS1_CS2_CS3:
388 ctlr_density = 4 * rank_density;
390 case FSL_DDR_CS0_CS1:
391 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
392 ctlr_density = 2 * rank_density;
394 case FSL_DDR_CS2_CS3:
396 ctlr_density = rank_density;
399 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
400 rank_density, ctlr_density);
401 for (i = first_ctrl; i <= last_ctrl; i++) {
402 if (pinfo->memctl_opts[i].memctl_interleaving) {
403 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
404 case FSL_DDR_256B_INTERLEAVING:
405 case FSL_DDR_CACHE_LINE_INTERLEAVING:
406 case FSL_DDR_PAGE_INTERLEAVING:
407 case FSL_DDR_BANK_INTERLEAVING:
408 case FSL_DDR_SUPERBANK_INTERLEAVING:
409 total_ctlr_mem = 2 * ctlr_density;
411 case FSL_DDR_3WAY_1KB_INTERLEAVING:
412 case FSL_DDR_3WAY_4KB_INTERLEAVING:
413 case FSL_DDR_3WAY_8KB_INTERLEAVING:
414 total_ctlr_mem = 3 * ctlr_density;
416 case FSL_DDR_4WAY_1KB_INTERLEAVING:
417 case FSL_DDR_4WAY_4KB_INTERLEAVING:
418 case FSL_DDR_4WAY_8KB_INTERLEAVING:
419 total_ctlr_mem = 4 * ctlr_density;
422 panic("Unknown interleaving mode");
424 pinfo->common_timing_params[i].base_address =
426 pinfo->common_timing_params[i].total_mem =
428 total_mem = current_mem_base + total_ctlr_mem;
429 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
430 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
432 /* when 3rd controller not interleaved */
433 current_mem_base = total_mem;
435 pinfo->common_timing_params[i].base_address =
437 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
438 unsigned long long cap =
439 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
440 pinfo->dimm_params[i][j].base_address =
442 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
443 current_mem_base += cap;
444 total_ctlr_mem += cap;
446 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
447 pinfo->common_timing_params[i].total_mem =
449 total_mem += total_ctlr_mem;
454 * Simple linear assignment if memory
455 * controllers are not interleaved.
457 for (i = first_ctrl; i <= last_ctrl; i++) {
459 pinfo->common_timing_params[i].base_address =
461 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
462 /* Compute DIMM base addresses. */
463 unsigned long long cap =
464 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
465 pinfo->dimm_params[i][j].base_address =
467 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
468 current_mem_base += cap;
469 total_ctlr_mem += cap;
471 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
472 pinfo->common_timing_params[i].total_mem =
474 total_mem += total_ctlr_mem;
477 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
482 /* Use weak function to allow board file to override the address assignment */
483 __attribute__((weak, alias("__step_assign_addresses")))
484 unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
485 unsigned int dbw_cap_adj[]);
488 fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
489 unsigned int size_only)
492 unsigned long long total_mem = 0;
493 int assert_reset = 0;
494 unsigned int first_ctrl = pinfo->first_ctrl;
495 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
496 __maybe_unused int retval;
497 __maybe_unused bool goodspd = false;
498 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
500 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
501 common_timing_params_t *timing_params = pinfo->common_timing_params;
502 if (pinfo->board_need_mem_reset)
503 assert_reset = pinfo->board_need_mem_reset();
505 /* data bus width capacity adjust shift amount */
506 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
508 for (i = first_ctrl; i <= last_ctrl; i++)
509 dbw_capacity_adjust[i] = 0;
511 debug("starting at step %u (%s)\n",
512 start_step, step_to_string(start_step));
514 switch (start_step) {
516 #if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
517 /* STEP 1: Gather all DIMM SPD data */
518 for (i = first_ctrl; i <= last_ctrl; i++) {
519 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
520 dimm_slots_per_ctrl);
523 case STEP_COMPUTE_DIMM_PARMS:
524 /* STEP 2: Compute DIMM parameters from SPD data */
526 for (i = first_ctrl; i <= last_ctrl; i++) {
527 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
528 generic_spd_eeprom_t *spd =
529 &(pinfo->spd_installed_dimms[i][j]);
530 dimm_params_t *pdimm =
531 &(pinfo->dimm_params[i][j]);
532 retval = compute_dimm_parameters(
534 #ifdef CONFIG_SYS_DDR_RAW_TIMING
536 printf("SPD error on controller %d! "
537 "Trying fallback to raw timing "
539 retval = fsl_ddr_get_dimm_params(pdimm,
544 printf("Error: compute_dimm_parameters"
545 " non-zero returned FATAL value "
546 "for memctl=%u dimm=%u\n", i, j);
551 debug("Warning: compute_dimm_parameters"
552 " non-zero return value for memctl=%u "
562 * Throw an error if this is for main memory, i.e.
563 * first_ctrl == 0. Otherwise, siliently return 0
564 * as the memory size.
567 printf("Error: No valid SPD detected.\n");
571 #elif defined(CONFIG_SYS_DDR_RAW_TIMING)
572 case STEP_COMPUTE_DIMM_PARMS:
573 for (i = first_ctrl; i <= last_ctrl; i++) {
574 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
575 dimm_params_t *pdimm =
576 &(pinfo->dimm_params[i][j]);
577 fsl_ddr_get_dimm_params(pdimm, i, j);
580 debug("Filling dimm parameters from board specific file\n");
582 case STEP_COMPUTE_COMMON_PARMS:
584 * STEP 3: Compute a common set of timing parameters
585 * suitable for all of the DIMMs on each memory controller
587 for (i = first_ctrl; i <= last_ctrl; i++) {
588 debug("Computing lowest common DIMM"
589 " parameters for memctl=%u\n", i);
590 compute_lowest_common_dimm_parameters
592 pinfo->dimm_params[i],
594 CONFIG_DIMM_SLOTS_PER_CTLR);
597 case STEP_GATHER_OPTS:
598 /* STEP 4: Gather configuration requirements from user */
599 for (i = first_ctrl; i <= last_ctrl; i++) {
600 debug("Reloading memory controller "
601 "configuration options for memctl=%u\n", i);
603 * This "reloads" the memory controller options
604 * to defaults. If the user "edits" an option,
605 * next_step points to the step after this,
606 * which is currently STEP_ASSIGN_ADDRESSES.
608 populate_memctl_options(
610 &pinfo->memctl_opts[i],
611 pinfo->dimm_params[i], i);
613 * For RDIMMs, JEDEC spec requires clocks to be stable
614 * before reset signal is deasserted. For the boards
615 * using fixed parameters, this function should be
616 * be called from board init file.
618 if (timing_params[i].all_dimms_registered)
621 if (assert_reset && !size_only) {
622 if (pinfo->board_mem_reset) {
623 debug("Asserting mem reset\n");
624 pinfo->board_mem_reset();
626 debug("Asserting mem reset missing\n");
630 case STEP_ASSIGN_ADDRESSES:
631 /* STEP 5: Assign addresses to chip selects */
632 check_interleaving_options(pinfo);
633 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
634 debug("Total mem %llu assigned\n", total_mem);
636 case STEP_COMPUTE_REGS:
637 /* STEP 6: compute controller register values */
638 debug("FSL Memory ctrl register computation\n");
639 for (i = first_ctrl; i <= last_ctrl; i++) {
640 if (timing_params[i].ndimms_present == 0) {
641 memset(&ddr_reg[i], 0,
642 sizeof(fsl_ddr_cfg_regs_t));
646 compute_fsl_memctl_config_regs
648 &pinfo->memctl_opts[i],
649 &ddr_reg[i], &timing_params[i],
650 pinfo->dimm_params[i],
651 dbw_capacity_adjust[i],
661 * Compute the amount of memory available just by
662 * looking for the highest valid CSn_BNDS value.
663 * This allows us to also experiment with using
664 * only CS0 when using dual-rank DIMMs.
666 unsigned int max_end = 0;
668 for (i = first_ctrl; i <= last_ctrl; i++) {
669 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
670 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
671 if (reg->cs[j].config & 0x80000000) {
674 * 0xfffffff is a special value we put
677 if (reg->cs[j].bnds == 0xffffffff)
679 end = reg->cs[j].bnds & 0xffff;
687 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
688 0xFFFFFFULL) - pinfo->mem_base;
694 phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
696 unsigned int i, first_ctrl, last_ctrl;
698 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
700 unsigned long long total_memory;
701 int deassert_reset = 0;
703 first_ctrl = pinfo->first_ctrl;
704 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
706 /* Compute it once normally. */
707 #ifdef CONFIG_FSL_DDR_INTERACTIVE
708 if (tstc() && (getchar() == 'd')) { /* we got a key press of 'd' */
709 total_memory = fsl_ddr_interactive(pinfo, 0);
710 } else if (fsl_ddr_interactive_env_var_exists()) {
711 total_memory = fsl_ddr_interactive(pinfo, 1);
714 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
716 /* setup 3-way interleaving before enabling DDRC */
717 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
718 case FSL_DDR_3WAY_1KB_INTERLEAVING:
719 case FSL_DDR_3WAY_4KB_INTERLEAVING:
720 case FSL_DDR_3WAY_8KB_INTERLEAVING:
722 pinfo->memctl_opts[first_ctrl].
723 memctl_interleaving_mode);
730 * Program configuration registers.
731 * JEDEC specs requires clocks to be stable before deasserting reset
732 * for RDIMMs. Clocks start after chip select is enabled and clock
733 * control register is set. During step 1, all controllers have their
734 * registers set but not enabled. Step 2 proceeds after deasserting
735 * reset through board FPGA or GPIO.
736 * For non-registered DIMMs, initialization can go through but it is
737 * also OK to follow the same flow.
739 if (pinfo->board_need_mem_reset)
740 deassert_reset = pinfo->board_need_mem_reset();
741 for (i = first_ctrl; i <= last_ctrl; i++) {
742 if (pinfo->common_timing_params[i].all_dimms_registered)
745 for (i = first_ctrl; i <= last_ctrl; i++) {
746 debug("Programming controller %u\n", i);
747 if (pinfo->common_timing_params[i].ndimms_present == 0) {
748 debug("No dimms present on controller %u; "
749 "skipping programming\n", i);
753 * The following call with step = 1 returns before enabling
754 * the controller. It has to finish with step = 2 later.
756 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
757 deassert_reset ? 1 : 0);
759 if (deassert_reset) {
760 /* Use board FPGA or GPIO to deassert reset signal */
761 if (pinfo->board_mem_de_reset) {
762 debug("Deasserting mem reset\n");
763 pinfo->board_mem_de_reset();
765 debug("Deasserting mem reset missing\n");
767 for (i = first_ctrl; i <= last_ctrl; i++) {
768 /* Call with step = 2 to continue initialization */
769 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
774 #ifdef CONFIG_FSL_DDR_SYNC_REFRESH
775 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
780 for (i = first_ctrl; i <= last_ctrl; i++) {
781 if (pinfo->memctl_opts[i].memctl_interleaving) {
782 switch (pinfo->memctl_opts[i].
783 memctl_interleaving_mode) {
784 case FSL_DDR_CACHE_LINE_INTERLEAVING:
785 case FSL_DDR_PAGE_INTERLEAVING:
786 case FSL_DDR_BANK_INTERLEAVING:
787 case FSL_DDR_SUPERBANK_INTERLEAVING:
791 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
793 &pinfo->common_timing_params[i],
796 #if CONFIG_SYS_NUM_DDR_CTLRS > 3
798 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
800 &pinfo->common_timing_params[i],
805 case FSL_DDR_3WAY_1KB_INTERLEAVING:
806 case FSL_DDR_3WAY_4KB_INTERLEAVING:
807 case FSL_DDR_3WAY_8KB_INTERLEAVING:
808 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
811 &pinfo->common_timing_params[i],
815 case FSL_DDR_4WAY_1KB_INTERLEAVING:
816 case FSL_DDR_4WAY_4KB_INTERLEAVING:
817 case FSL_DDR_4WAY_8KB_INTERLEAVING:
818 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
821 &pinfo->common_timing_params[i],
823 /* place holder for future 4-way interleaving */
831 law_memctl = LAW_TRGT_IF_DDR_1;
834 law_memctl = LAW_TRGT_IF_DDR_2;
837 law_memctl = LAW_TRGT_IF_DDR_3;
840 law_memctl = LAW_TRGT_IF_DDR_4;
845 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
851 debug("total_memory by %s = %llu\n", __func__, total_memory);
853 #if !defined(CONFIG_PHYS_64BIT)
854 /* Check for 4G or more. Bad. */
855 if ((first_ctrl == 0) && (total_memory >= (1ull << 32))) {
857 print_size(total_memory, " of memory\n");
858 printf(" This U-Boot only supports < 4G of DDR\n");
859 printf(" You could rebuild it with CONFIG_PHYS_64BIT\n");
860 printf(" "); /* re-align to match init_dram print */
861 total_memory = CONFIG_MAX_MEM_MAPPED;
869 * fsl_ddr_sdram(void) -- this is the main function to be
870 * called by dram_init() in the board file.
872 * It returns amount of memory configured in bytes.
874 phys_size_t fsl_ddr_sdram(void)
878 /* Reset info structure. */
879 memset(&info, 0, sizeof(fsl_ddr_info_t));
880 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
882 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
883 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
884 info.board_need_mem_reset = board_need_mem_reset;
885 info.board_mem_reset = board_assert_mem_reset;
886 info.board_mem_de_reset = board_deassert_mem_reset;
887 remove_unused_controllers(&info);
889 return __fsl_ddr_sdram(&info);
892 #ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
893 phys_size_t fsl_other_ddr_sdram(unsigned long long base,
894 unsigned int first_ctrl,
895 unsigned int num_ctrls,
896 unsigned int dimm_slots_per_ctrl,
897 int (*board_need_reset)(void),
898 void (*board_reset)(void),
899 void (*board_de_reset)(void))
903 /* Reset info structure. */
904 memset(&info, 0, sizeof(fsl_ddr_info_t));
905 info.mem_base = base;
906 info.first_ctrl = first_ctrl;
907 info.num_ctrls = num_ctrls;
908 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
909 info.board_need_mem_reset = board_need_reset;
910 info.board_mem_reset = board_reset;
911 info.board_mem_de_reset = board_de_reset;
913 return __fsl_ddr_sdram(&info);
918 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
919 * size of the total memory without setting ddr control registers.
922 fsl_ddr_sdram_size(void)
925 unsigned long long total_memory = 0;
927 memset(&info, 0 , sizeof(fsl_ddr_info_t));
928 info.mem_base = CONFIG_SYS_FSL_DDR_SDRAM_BASE_PHY;
930 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
931 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
932 info.board_need_mem_reset = NULL;
933 remove_unused_controllers(&info);
935 /* Compute it once normally. */
936 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);