1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2008-2014 Freescale Semiconductor, Inc.
8 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
9 * Based on code from spd_sdram.c
10 * Author: James Yang [at freescale.com]
14 #include <display_options.h>
17 #include <fsl_ddr_sdram.h>
21 #include <asm/bitops.h>
24 * CFG_SYS_FSL_DDR_SDRAM_BASE_PHY is the physical address from the view
25 * of DDR controllers. It is the same as CFG_SYS_DDR_SDRAM_BASE for
26 * all Power SoCs. But it could be different for ARM SoCs. For example,
27 * fsl_lsch3 has a mapping mechanism to map DDR memory to ranges (in order) of
28 * 0x00_8000_0000 ~ 0x00_ffff_ffff
29 * 0x80_8000_0000 ~ 0xff_ffff_ffff
31 #ifndef CFG_SYS_FSL_DDR_SDRAM_BASE_PHY
33 #define CFG_SYS_FSL_DDR_SDRAM_BASE_PHY CFG_SYS_SDRAM_BASE
35 #define CFG_SYS_FSL_DDR_SDRAM_BASE_PHY CFG_SYS_DDR_SDRAM_BASE
40 #include <asm/fsl_law.h>
42 void fsl_ddr_set_lawbar(
43 const common_timing_params_t *memctl_common_params,
44 unsigned int memctl_interleaved,
45 unsigned int ctrl_num);
48 void fsl_ddr_set_intl3r(const unsigned int granule_size);
49 #if defined(SPD_EEPROM_ADDRESS) || \
50 defined(SPD_EEPROM_ADDRESS1) || defined(SPD_EEPROM_ADDRESS2) || \
51 defined(SPD_EEPROM_ADDRESS3) || defined(SPD_EEPROM_ADDRESS4)
52 #if (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
53 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
54 [0][0] = SPD_EEPROM_ADDRESS,
56 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 1) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
57 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
58 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
59 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
61 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
62 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
63 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
64 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
66 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 2) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
67 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
68 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
69 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
70 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
71 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
73 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 1)
74 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
75 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
76 [1][0] = SPD_EEPROM_ADDRESS2, /* controller 2 */
77 [2][0] = SPD_EEPROM_ADDRESS3, /* controller 3 */
79 #elif (CONFIG_SYS_NUM_DDR_CTLRS == 3) && (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
80 u8 spd_i2c_addr[CONFIG_SYS_NUM_DDR_CTLRS][CONFIG_DIMM_SLOTS_PER_CTLR] = {
81 [0][0] = SPD_EEPROM_ADDRESS1, /* controller 1 */
82 [0][1] = SPD_EEPROM_ADDRESS2, /* controller 1 */
83 [1][0] = SPD_EEPROM_ADDRESS3, /* controller 2 */
84 [1][1] = SPD_EEPROM_ADDRESS4, /* controller 2 */
85 [2][0] = SPD_EEPROM_ADDRESS5, /* controller 3 */
86 [2][1] = SPD_EEPROM_ADDRESS6, /* controller 3 */
91 #if CONFIG_IS_ENABLED(DM_I2C)
92 #define DEV_TYPE struct udevice
99 #define DEV_TYPE struct ludevice
103 #define SPD_SPA0_ADDRESS 0x36
104 #define SPD_SPA1_ADDRESS 0x37
106 static int ddr_i2c_read(DEV_TYPE *dev, unsigned int addr,
107 int alen, uint8_t *buf, int len)
111 #if CONFIG_IS_ENABLED(DM_I2C)
112 ret = dm_i2c_read(dev, 0, buf, len);
114 ret = i2c_read(dev->chip, addr, alen, buf, len);
120 #ifdef CONFIG_SYS_FSL_DDR4
121 static int ddr_i2c_dummy_write(unsigned int chip_addr)
125 #if CONFIG_IS_ENABLED(DM_I2C)
129 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, chip_addr,
132 printf("%s: Cannot find udev for a bus %d\n", __func__,
133 CONFIG_SYS_SPD_BUS_NUM);
137 return dm_i2c_write(dev, 0, &buf, 1);
139 return i2c_write(chip_addr, 0, 1, &buf, 1);
146 static void __get_spd(generic_spd_eeprom_t *spd, u8 i2c_address)
151 #if CONFIG_IS_ENABLED(DM_I2C)
152 ret = i2c_get_chip_for_busnum(CONFIG_SYS_SPD_BUS_NUM, i2c_address,
155 printf("%s: Cannot find udev for a bus %d\n", __func__,
156 CONFIG_SYS_SPD_BUS_NUM);
159 #else /* Non DM I2C support - will be removed */
160 struct ludevice ldev = {
165 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
168 #ifdef CONFIG_SYS_FSL_DDR4
170 * DDR4 SPD has 384 to 512 bytes
171 * To access the lower 256 bytes, we need to set EE page address to 0
172 * To access the upper 256 bytes, we need to set EE page address to 1
173 * See Jedec standar No. 21-C for detail
175 ddr_i2c_dummy_write(SPD_SPA0_ADDRESS);
176 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd, 256);
178 ddr_i2c_dummy_write(SPD_SPA1_ADDRESS);
179 ret = ddr_i2c_read(dev, 0, 1, (uchar *)((ulong)spd + 256),
181 (int)sizeof(generic_spd_eeprom_t)
186 ret = ddr_i2c_read(dev, 0, 1, (uchar *)spd,
187 sizeof(generic_spd_eeprom_t));
192 #ifdef SPD_EEPROM_ADDRESS
194 #elif defined(SPD_EEPROM_ADDRESS1)
198 printf("DDR: failed to read SPD from address %u\n",
201 debug("DDR: failed to read SPD from address %u\n",
204 memset(spd, 0, sizeof(generic_spd_eeprom_t));
208 __attribute__((weak, alias("__get_spd")))
209 void get_spd(generic_spd_eeprom_t *spd, u8 i2c_address);
211 /* This function allows boards to update SPD address */
212 __weak void update_spd_address(unsigned int ctrl_num,
218 void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
219 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
222 unsigned int i2c_address = 0;
224 if (ctrl_num >= CONFIG_SYS_NUM_DDR_CTLRS) {
225 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
229 for (i = 0; i < dimm_slots_per_ctrl; i++) {
230 i2c_address = spd_i2c_addr[ctrl_num][i];
231 update_spd_address(ctrl_num, i, &i2c_address);
232 get_spd(&(ctrl_dimms_spd[i]), i2c_address);
236 void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
237 unsigned int ctrl_num, unsigned int dimm_slots_per_ctrl)
240 #endif /* SPD_EEPROM_ADDRESSx */
244 * - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
245 * - Same memory data bus width on all controllers
249 * The memory controller and associated documentation use confusing
250 * terminology when referring to the orgranization of DRAM.
252 * Here is a terminology translation table:
254 * memory controller/documention |industry |this code |signals
255 * -------------------------------|-----------|-----------|-----------------
256 * physical bank/bank |rank |rank |chip select (CS)
257 * logical bank/sub-bank |bank |bank |bank address (BA)
258 * page/row |row |page |row address
259 * ??? |column |column |column address
261 * The naming confusion is further exacerbated by the descriptions of the
262 * memory controller interleaving feature, where accesses are interleaved
263 * _BETWEEN_ two seperate memory controllers. This is configured only in
264 * CS0_CONFIG[INTLV_CTL] of each memory controller.
266 * memory controller documentation | number of chip selects
267 * | per memory controller supported
268 * --------------------------------|-----------------------------------------
269 * cache line interleaving | 1 (CS0 only)
270 * page interleaving | 1 (CS0 only)
271 * bank interleaving | 1 (CS0 only)
272 * superbank interleraving | depends on bank (chip select)
273 * | interleraving [rank interleaving]
274 * | mode used on every memory controller
276 * Even further confusing is the existence of the interleaving feature
277 * _WITHIN_ each memory controller. The feature is referred to in
278 * documentation as chip select interleaving or bank interleaving,
279 * although it is configured in the DDR_SDRAM_CFG field.
281 * Name of field | documentation name | this code
282 * -----------------------------|-----------------------|------------------
283 * DDR_SDRAM_CFG[BA_INTLV_CTL] | Bank (chip select) | rank interleaving
287 const char *step_string_tbl[] = {
289 "STEP_COMPUTE_DIMM_PARMS",
290 "STEP_COMPUTE_COMMON_PARMS",
292 "STEP_ASSIGN_ADDRESSES",
298 const char * step_to_string(unsigned int step) {
300 unsigned int s = __ilog2(step);
303 if ((1 << s) != step)
304 return step_string_tbl[7];
306 if ((1 << (s - 32)) != step)
307 return step_string_tbl[7];
309 if (s >= ARRAY_SIZE(step_string_tbl)) {
310 printf("Error for the step in %s\n", __func__);
314 return step_string_tbl[s];
317 static unsigned long long __step_assign_addresses(fsl_ddr_info_t *pinfo,
318 unsigned int dbw_cap_adj[])
321 unsigned long long total_mem, current_mem_base, total_ctlr_mem;
322 unsigned long long rank_density, ctlr_density = 0;
323 unsigned int first_ctrl = pinfo->first_ctrl;
324 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
327 * If a reduced data width is requested, but the SPD
328 * specifies a physically wider device, adjust the
329 * computed dimm capacities accordingly before
330 * assigning addresses.
332 for (i = first_ctrl; i <= last_ctrl; i++) {
333 unsigned int found = 0;
335 switch (pinfo->memctl_opts[i].data_bus_width) {
338 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
340 if (!pinfo->dimm_params[i][j].n_ranks)
342 dw = pinfo->dimm_params[i][j].primary_sdram_width;
343 if ((dw == 72 || dw == 64)) {
346 } else if ((dw == 40 || dw == 32)) {
355 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
357 dw = pinfo->dimm_params[i][j].data_width;
358 if (pinfo->dimm_params[i][j].n_ranks
359 && (dw == 72 || dw == 64)) {
361 * FIXME: can't really do it
362 * like this because this just
363 * further reduces the memory
379 printf("unexpected data bus width "
380 "specified controller %u\n", i);
383 debug("dbw_cap_adj[%d]=%d\n", i, dbw_cap_adj[i]);
386 current_mem_base = pinfo->mem_base;
388 if (pinfo->memctl_opts[first_ctrl].memctl_interleaving) {
389 rank_density = pinfo->dimm_params[first_ctrl][0].rank_density >>
390 dbw_cap_adj[first_ctrl];
391 switch (pinfo->memctl_opts[first_ctrl].ba_intlv_ctl &
392 FSL_DDR_CS0_CS1_CS2_CS3) {
393 case FSL_DDR_CS0_CS1_CS2_CS3:
394 ctlr_density = 4 * rank_density;
396 case FSL_DDR_CS0_CS1:
397 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
398 ctlr_density = 2 * rank_density;
400 case FSL_DDR_CS2_CS3:
402 ctlr_density = rank_density;
405 debug("rank density is 0x%llx, ctlr density is 0x%llx\n",
406 rank_density, ctlr_density);
407 for (i = first_ctrl; i <= last_ctrl; i++) {
408 if (pinfo->memctl_opts[i].memctl_interleaving) {
409 switch (pinfo->memctl_opts[i].memctl_interleaving_mode) {
410 case FSL_DDR_256B_INTERLEAVING:
411 case FSL_DDR_CACHE_LINE_INTERLEAVING:
412 case FSL_DDR_PAGE_INTERLEAVING:
413 case FSL_DDR_BANK_INTERLEAVING:
414 case FSL_DDR_SUPERBANK_INTERLEAVING:
415 total_ctlr_mem = 2 * ctlr_density;
417 case FSL_DDR_3WAY_1KB_INTERLEAVING:
418 case FSL_DDR_3WAY_4KB_INTERLEAVING:
419 case FSL_DDR_3WAY_8KB_INTERLEAVING:
420 total_ctlr_mem = 3 * ctlr_density;
422 case FSL_DDR_4WAY_1KB_INTERLEAVING:
423 case FSL_DDR_4WAY_4KB_INTERLEAVING:
424 case FSL_DDR_4WAY_8KB_INTERLEAVING:
425 total_ctlr_mem = 4 * ctlr_density;
428 panic("Unknown interleaving mode");
430 pinfo->common_timing_params[i].base_address =
432 pinfo->common_timing_params[i].total_mem =
434 total_mem = current_mem_base + total_ctlr_mem;
435 debug("ctrl %d base 0x%llx\n", i, current_mem_base);
436 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
438 /* when 3rd controller not interleaved */
439 current_mem_base = total_mem;
441 pinfo->common_timing_params[i].base_address =
443 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
444 unsigned long long cap =
445 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
446 pinfo->dimm_params[i][j].base_address =
448 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
449 current_mem_base += cap;
450 total_ctlr_mem += cap;
452 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
453 pinfo->common_timing_params[i].total_mem =
455 total_mem += total_ctlr_mem;
460 * Simple linear assignment if memory
461 * controllers are not interleaved.
463 for (i = first_ctrl; i <= last_ctrl; i++) {
465 pinfo->common_timing_params[i].base_address =
467 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
468 /* Compute DIMM base addresses. */
469 unsigned long long cap =
470 pinfo->dimm_params[i][j].capacity >> dbw_cap_adj[i];
471 pinfo->dimm_params[i][j].base_address =
473 debug("ctrl %d dimm %d base 0x%llx\n", i, j, current_mem_base);
474 current_mem_base += cap;
475 total_ctlr_mem += cap;
477 debug("ctrl %d total 0x%llx\n", i, total_ctlr_mem);
478 pinfo->common_timing_params[i].total_mem =
480 total_mem += total_ctlr_mem;
483 debug("Total mem by %s is 0x%llx\n", __func__, total_mem);
488 /* Use weak function to allow board file to override the address assignment */
489 __attribute__((weak, alias("__step_assign_addresses")))
490 unsigned long long step_assign_addresses(fsl_ddr_info_t *pinfo,
491 unsigned int dbw_cap_adj[]);
494 fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
495 unsigned int size_only)
498 unsigned long long total_mem = 0;
499 int assert_reset = 0;
500 unsigned int first_ctrl = pinfo->first_ctrl;
501 unsigned int last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
502 __maybe_unused int retval;
503 __maybe_unused bool goodspd = false;
504 __maybe_unused int dimm_slots_per_ctrl = pinfo->dimm_slots_per_ctrl;
506 fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
507 common_timing_params_t *timing_params = pinfo->common_timing_params;
508 if (pinfo->board_need_mem_reset)
509 assert_reset = pinfo->board_need_mem_reset();
511 /* data bus width capacity adjust shift amount */
512 unsigned int dbw_capacity_adjust[CONFIG_SYS_NUM_DDR_CTLRS];
514 for (i = first_ctrl; i <= last_ctrl; i++)
515 dbw_capacity_adjust[i] = 0;
517 debug("starting at step %u (%s)\n",
518 start_step, step_to_string(start_step));
520 switch (start_step) {
522 #if defined(CONFIG_DDR_SPD) || defined(CONFIG_SPD_EEPROM)
523 /* STEP 1: Gather all DIMM SPD data */
524 for (i = first_ctrl; i <= last_ctrl; i++) {
525 fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i,
526 dimm_slots_per_ctrl);
529 case STEP_COMPUTE_DIMM_PARMS:
530 /* STEP 2: Compute DIMM parameters from SPD data */
532 for (i = first_ctrl; i <= last_ctrl; i++) {
533 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
534 generic_spd_eeprom_t *spd =
535 &(pinfo->spd_installed_dimms[i][j]);
536 dimm_params_t *pdimm =
537 &(pinfo->dimm_params[i][j]);
538 retval = compute_dimm_parameters(
540 #ifdef CONFIG_SYS_DDR_RAW_TIMING
542 printf("SPD error on controller %d! "
543 "Trying fallback to raw timing "
545 retval = fsl_ddr_get_dimm_params(pdimm,
550 printf("Error: compute_dimm_parameters"
551 " non-zero returned FATAL value "
552 "for memctl=%u dimm=%u\n", i, j);
557 debug("Warning: compute_dimm_parameters"
558 " non-zero return value for memctl=%u "
568 * Throw an error if this is for main memory, i.e.
569 * first_ctrl == 0. Otherwise, siliently return 0
570 * as the memory size.
573 printf("Error: No valid SPD detected.\n");
577 #elif defined(CONFIG_SYS_DDR_RAW_TIMING)
578 case STEP_COMPUTE_DIMM_PARMS:
579 for (i = first_ctrl; i <= last_ctrl; i++) {
580 for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
581 dimm_params_t *pdimm =
582 &(pinfo->dimm_params[i][j]);
583 fsl_ddr_get_dimm_params(pdimm, i, j);
586 debug("Filling dimm parameters from board specific file\n");
588 case STEP_COMPUTE_COMMON_PARMS:
590 * STEP 3: Compute a common set of timing parameters
591 * suitable for all of the DIMMs on each memory controller
593 for (i = first_ctrl; i <= last_ctrl; i++) {
594 debug("Computing lowest common DIMM"
595 " parameters for memctl=%u\n", i);
596 compute_lowest_common_dimm_parameters
598 pinfo->dimm_params[i],
600 CONFIG_DIMM_SLOTS_PER_CTLR);
603 case STEP_GATHER_OPTS:
604 /* STEP 4: Gather configuration requirements from user */
605 for (i = first_ctrl; i <= last_ctrl; i++) {
606 debug("Reloading memory controller "
607 "configuration options for memctl=%u\n", i);
609 * This "reloads" the memory controller options
610 * to defaults. If the user "edits" an option,
611 * next_step points to the step after this,
612 * which is currently STEP_ASSIGN_ADDRESSES.
614 populate_memctl_options(
616 &pinfo->memctl_opts[i],
617 pinfo->dimm_params[i], i);
619 * For RDIMMs, JEDEC spec requires clocks to be stable
620 * before reset signal is deasserted. For the boards
621 * using fixed parameters, this function should be
622 * be called from board init file.
624 if (timing_params[i].all_dimms_registered)
627 if (assert_reset && !size_only) {
628 if (pinfo->board_mem_reset) {
629 debug("Asserting mem reset\n");
630 pinfo->board_mem_reset();
632 debug("Asserting mem reset missing\n");
636 case STEP_ASSIGN_ADDRESSES:
637 /* STEP 5: Assign addresses to chip selects */
638 check_interleaving_options(pinfo);
639 total_mem = step_assign_addresses(pinfo, dbw_capacity_adjust);
640 debug("Total mem %llu assigned\n", total_mem);
642 case STEP_COMPUTE_REGS:
643 /* STEP 6: compute controller register values */
644 debug("FSL Memory ctrl register computation\n");
645 for (i = first_ctrl; i <= last_ctrl; i++) {
646 if (timing_params[i].ndimms_present == 0) {
647 memset(&ddr_reg[i], 0,
648 sizeof(fsl_ddr_cfg_regs_t));
652 compute_fsl_memctl_config_regs
654 &pinfo->memctl_opts[i],
655 &ddr_reg[i], &timing_params[i],
656 pinfo->dimm_params[i],
657 dbw_capacity_adjust[i],
667 * Compute the amount of memory available just by
668 * looking for the highest valid CSn_BNDS value.
669 * This allows us to also experiment with using
670 * only CS0 when using dual-rank DIMMs.
672 unsigned int max_end = 0;
674 for (i = first_ctrl; i <= last_ctrl; i++) {
675 for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
676 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
677 if (reg->cs[j].config & 0x80000000) {
680 * 0xfffffff is a special value we put
683 if (reg->cs[j].bnds == 0xffffffff)
685 end = reg->cs[j].bnds & 0xffff;
693 total_mem = 1 + (((unsigned long long)max_end << 24ULL) |
694 0xFFFFFFULL) - pinfo->mem_base;
700 phys_size_t __fsl_ddr_sdram(fsl_ddr_info_t *pinfo)
702 unsigned int i, first_ctrl, last_ctrl;
704 unsigned int law_memctl = LAW_TRGT_IF_DDR_1;
706 unsigned long long total_memory;
707 int deassert_reset = 0;
709 first_ctrl = pinfo->first_ctrl;
710 last_ctrl = first_ctrl + pinfo->num_ctrls - 1;
712 /* Compute it once normally. */
713 #ifdef CONFIG_FSL_DDR_INTERACTIVE
714 if (tstc() && (getchar() == 'd')) { /* we got a key press of 'd' */
715 total_memory = fsl_ddr_interactive(pinfo, 0);
716 } else if (fsl_ddr_interactive_env_var_exists()) {
717 total_memory = fsl_ddr_interactive(pinfo, 1);
720 total_memory = fsl_ddr_compute(pinfo, STEP_GET_SPD, 0);
722 /* setup 3-way interleaving before enabling DDRC */
723 switch (pinfo->memctl_opts[first_ctrl].memctl_interleaving_mode) {
724 case FSL_DDR_3WAY_1KB_INTERLEAVING:
725 case FSL_DDR_3WAY_4KB_INTERLEAVING:
726 case FSL_DDR_3WAY_8KB_INTERLEAVING:
728 pinfo->memctl_opts[first_ctrl].
729 memctl_interleaving_mode);
736 * Program configuration registers.
737 * JEDEC specs requires clocks to be stable before deasserting reset
738 * for RDIMMs. Clocks start after chip select is enabled and clock
739 * control register is set. During step 1, all controllers have their
740 * registers set but not enabled. Step 2 proceeds after deasserting
741 * reset through board FPGA or GPIO.
742 * For non-registered DIMMs, initialization can go through but it is
743 * also OK to follow the same flow.
745 if (pinfo->board_need_mem_reset)
746 deassert_reset = pinfo->board_need_mem_reset();
747 for (i = first_ctrl; i <= last_ctrl; i++) {
748 if (pinfo->common_timing_params[i].all_dimms_registered)
751 for (i = first_ctrl; i <= last_ctrl; i++) {
752 debug("Programming controller %u\n", i);
753 if (pinfo->common_timing_params[i].ndimms_present == 0) {
754 debug("No dimms present on controller %u; "
755 "skipping programming\n", i);
759 * The following call with step = 1 returns before enabling
760 * the controller. It has to finish with step = 2 later.
762 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]), i,
763 deassert_reset ? 1 : 0);
765 if (deassert_reset) {
766 /* Use board FPGA or GPIO to deassert reset signal */
767 if (pinfo->board_mem_de_reset) {
768 debug("Deasserting mem reset\n");
769 pinfo->board_mem_de_reset();
771 debug("Deasserting mem reset missing\n");
773 for (i = first_ctrl; i <= last_ctrl; i++) {
774 /* Call with step = 2 to continue initialization */
775 fsl_ddr_set_memctl_regs(&(pinfo->fsl_ddr_config_reg[i]),
780 #ifdef CONFIG_FSL_DDR_SYNC_REFRESH
781 fsl_ddr_sync_memctl_refresh(first_ctrl, last_ctrl);
786 for (i = first_ctrl; i <= last_ctrl; i++) {
787 if (pinfo->memctl_opts[i].memctl_interleaving) {
788 switch (pinfo->memctl_opts[i].
789 memctl_interleaving_mode) {
790 case FSL_DDR_CACHE_LINE_INTERLEAVING:
791 case FSL_DDR_PAGE_INTERLEAVING:
792 case FSL_DDR_BANK_INTERLEAVING:
793 case FSL_DDR_SUPERBANK_INTERLEAVING:
797 law_memctl = LAW_TRGT_IF_DDR_INTRLV;
799 &pinfo->common_timing_params[i],
802 #if CONFIG_SYS_NUM_DDR_CTLRS > 3
804 law_memctl = LAW_TRGT_IF_DDR_INTLV_34;
806 &pinfo->common_timing_params[i],
811 case FSL_DDR_3WAY_1KB_INTERLEAVING:
812 case FSL_DDR_3WAY_4KB_INTERLEAVING:
813 case FSL_DDR_3WAY_8KB_INTERLEAVING:
814 law_memctl = LAW_TRGT_IF_DDR_INTLV_123;
817 &pinfo->common_timing_params[i],
821 case FSL_DDR_4WAY_1KB_INTERLEAVING:
822 case FSL_DDR_4WAY_4KB_INTERLEAVING:
823 case FSL_DDR_4WAY_8KB_INTERLEAVING:
824 law_memctl = LAW_TRGT_IF_DDR_INTLV_1234;
827 &pinfo->common_timing_params[i],
829 /* place holder for future 4-way interleaving */
837 law_memctl = LAW_TRGT_IF_DDR_1;
840 law_memctl = LAW_TRGT_IF_DDR_2;
843 law_memctl = LAW_TRGT_IF_DDR_3;
846 law_memctl = LAW_TRGT_IF_DDR_4;
851 fsl_ddr_set_lawbar(&pinfo->common_timing_params[i],
857 debug("total_memory by %s = %llu\n", __func__, total_memory);
859 #if !defined(CONFIG_PHYS_64BIT)
861 * Show warning about big DDR moodules. But avoid warning for 4 GB DDR
862 * modules when U-Boot supports RAM of maximal size 4 GB - 1 byte.
864 if ((first_ctrl == 0) && (total_memory - 1 > (phys_size_t)~0ULL)) {
866 print_size(total_memory, " of memory\n");
867 #ifndef CONFIG_SPL_BUILD
868 puts(" "); /* re-align to match init_dram print */
870 puts("This U-Boot only supports <= ");
871 print_size((unsigned long long)((phys_size_t)~0ULL)+1, " of DDR\n");
872 #ifndef CONFIG_SPL_BUILD
873 puts(" "); /* re-align to match init_dram print */
875 puts("You could rebuild it with CONFIG_PHYS_64BIT\n");
876 #ifndef CONFIG_SPL_BUILD
877 puts(" "); /* re-align to match init_dram print */
882 /* Ensure that total_memory does not overflow on return */
883 if (total_memory > (phys_size_t)~0ULL)
884 total_memory = (phys_size_t)~0ULL;
890 * fsl_ddr_sdram(void) -- this is the main function to be
891 * called by dram_init() in the board file.
893 * It returns amount of memory configured in bytes.
895 phys_size_t fsl_ddr_sdram(void)
899 /* Reset info structure. */
900 memset(&info, 0, sizeof(fsl_ddr_info_t));
901 info.mem_base = CFG_SYS_FSL_DDR_SDRAM_BASE_PHY;
903 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
904 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
905 info.board_need_mem_reset = board_need_mem_reset;
906 info.board_mem_reset = board_assert_mem_reset;
907 info.board_mem_de_reset = board_deassert_mem_reset;
908 remove_unused_controllers(&info);
910 return __fsl_ddr_sdram(&info);
913 #ifdef CONFIG_SYS_FSL_OTHER_DDR_NUM_CTRLS
914 phys_size_t fsl_other_ddr_sdram(unsigned long long base,
915 unsigned int first_ctrl,
916 unsigned int num_ctrls,
917 unsigned int dimm_slots_per_ctrl,
918 int (*board_need_reset)(void),
919 void (*board_reset)(void),
920 void (*board_de_reset)(void))
924 /* Reset info structure. */
925 memset(&info, 0, sizeof(fsl_ddr_info_t));
926 info.mem_base = base;
927 info.first_ctrl = first_ctrl;
928 info.num_ctrls = num_ctrls;
929 info.dimm_slots_per_ctrl = dimm_slots_per_ctrl;
930 info.board_need_mem_reset = board_need_reset;
931 info.board_mem_reset = board_reset;
932 info.board_mem_de_reset = board_de_reset;
934 return __fsl_ddr_sdram(&info);
939 * fsl_ddr_sdram_size(first_ctrl, last_intlv) - This function only returns the
940 * size of the total memory without setting ddr control registers.
943 fsl_ddr_sdram_size(void)
946 unsigned long long total_memory = 0;
948 memset(&info, 0 , sizeof(fsl_ddr_info_t));
949 info.mem_base = CFG_SYS_FSL_DDR_SDRAM_BASE_PHY;
951 info.num_ctrls = CONFIG_SYS_FSL_DDR_MAIN_NUM_CTRLS;
952 info.dimm_slots_per_ctrl = CONFIG_DIMM_SLOTS_PER_CTLR;
953 info.board_need_mem_reset = NULL;
954 remove_unused_controllers(&info);
956 /* Compute it once normally. */
957 total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
959 /* Ensure that total_memory does not overflow on return */
960 if (total_memory > (phys_size_t)~0ULL)
961 total_memory = (phys_size_t)~0ULL;