ddr.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <i2c.h>
  7. #include <hwconfig.h>
  8. #include <asm/mmu.h>
  9. #include <fsl_ddr_sdram.h>
  10. #include <fsl_ddr_dimm_params.h>
  11. #include <asm/fsl_law.h>
  12. #include <asm/mpc85xx_gpio.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. struct board_specific_parameters {
  15. u32 n_ranks;
  16. u32 datarate_mhz_high;
  17. u32 rank_gb;
  18. u32 clk_adjust;
  19. u32 wrlvl_start;
  20. u32 wrlvl_ctl_2;
  21. u32 wrlvl_ctl_3;
  22. };
  23. /*
  24. * datarate_mhz_high values need to be in ascending order
  25. */
  26. static const struct board_specific_parameters udimm0[] = {
  27. /*
  28. * memory controller 0
  29. * num| hi| rank| clk| wrlvl | wrlvl | wrlvl |
  30. * ranks| mhz| GB |adjst| start | ctl2 | ctl3 |
  31. */
  32. #if defined(CONFIG_SYS_FSL_DDR4)
  33. {2, 1666, 0, 8, 7, 0x0808090B, 0x0C0D0E0A,},
  34. {2, 1900, 0, 8, 6, 0x08080A0C, 0x0D0E0F0A,},
  35. {1, 1666, 0, 8, 6, 0x0708090B, 0x0C0D0E09,},
  36. {1, 1900, 0, 8, 6, 0x08080A0C, 0x0D0E0F0A,},
  37. {1, 2200, 0, 8, 7, 0x08090A0D, 0x0F0F100C,},
  38. #elif defined(CONFIG_SYS_FSL_DDR3)
  39. {2, 833, 0, 8, 6, 0x06060607, 0x08080807,},
  40. {2, 1350, 0, 8, 7, 0x0708080A, 0x0A0B0C09,},
  41. {2, 1666, 0, 8, 7, 0x0808090B, 0x0C0D0E0A,},
  42. {1, 833, 0, 8, 6, 0x06060607, 0x08080807,},
  43. {1, 1350, 0, 8, 7, 0x0708080A, 0x0A0B0C09,},
  44. {1, 1666, 0, 8, 7, 0x0808090B, 0x0C0D0E0A,},
  45. #else
  46. #error DDR type not defined
  47. #endif
  48. {}
  49. };
  50. static const struct board_specific_parameters *udimms[] = {
  51. udimm0,
  52. };
  53. void fsl_ddr_board_options(memctl_options_t *popts,
  54. dimm_params_t *pdimm,
  55. unsigned int ctrl_num)
  56. {
  57. const struct board_specific_parameters *pbsp, *pbsp_highest = NULL;
  58. ulong ddr_freq;
  59. struct cpu_type *cpu = gd->arch.cpu;
  60. if (ctrl_num > 2) {
  61. printf("Not supported controller number %d\n", ctrl_num);
  62. return;
  63. }
  64. if (!pdimm->n_ranks)
  65. return;
  66. pbsp = udimms[0];
  67. /* Get clk_adjust according to the board ddr freqency and n_banks
  68. * specified in board_specific_parameters table.
  69. */
  70. ddr_freq = get_ddr_freq(0) / 1000000;
  71. while (pbsp->datarate_mhz_high) {
  72. if (pbsp->n_ranks == pdimm->n_ranks &&
  73. (pdimm->rank_density >> 30) >= pbsp->rank_gb) {
  74. if (ddr_freq <= pbsp->datarate_mhz_high) {
  75. popts->clk_adjust = pbsp->clk_adjust;
  76. popts->wrlvl_start = pbsp->wrlvl_start;
  77. popts->wrlvl_ctl_2 = pbsp->wrlvl_ctl_2;
  78. popts->wrlvl_ctl_3 = pbsp->wrlvl_ctl_3;
  79. goto found;
  80. }
  81. pbsp_highest = pbsp;
  82. }
  83. pbsp++;
  84. }
  85. if (pbsp_highest) {
  86. printf("Error: board specific timing not found\n");
  87. printf("for data rate %lu MT/s\n", ddr_freq);
  88. printf("Trying to use the highest speed (%u) parameters\n",
  89. pbsp_highest->datarate_mhz_high);
  90. popts->clk_adjust = pbsp_highest->clk_adjust;
  91. popts->wrlvl_start = pbsp_highest->wrlvl_start;
  92. popts->wrlvl_ctl_2 = pbsp->wrlvl_ctl_2;
  93. popts->wrlvl_ctl_3 = pbsp->wrlvl_ctl_3;
  94. } else {
  95. panic("DIMM is not supported by this board");
  96. }
  97. found:
  98. debug("Found timing match: n_ranks %d, data rate %d, rank_gb %d\n",
  99. pbsp->n_ranks, pbsp->datarate_mhz_high, pbsp->rank_gb);
  100. debug("\tclk_adjust %d, wrlvl_start %d, wrlvl_ctrl_2 0x%x, ",
  101. pbsp->clk_adjust, pbsp->wrlvl_start, pbsp->wrlvl_ctl_2);
  102. debug("wrlvl_ctrl_3 0x%x\n", pbsp->wrlvl_ctl_3);
  103. /*
  104. * Factors to consider for half-strength driver enable:
  105. * - number of DIMMs installed
  106. */
  107. popts->half_strength_driver_enable = 1;
  108. /*
  109. * Write leveling override
  110. */
  111. popts->wrlvl_override = 1;
  112. popts->wrlvl_sample = 0xf;
  113. /*
  114. * rtt and rtt_wr override
  115. */
  116. popts->rtt_override = 0;
  117. /* Enable ZQ calibration */
  118. popts->zq_en = 1;
  119. /* DHC_EN =1, ODT = 75 Ohm */
  120. #ifdef CONFIG_SYS_FSL_DDR4
  121. popts->ddr_cdr1 = DDR_CDR1_DHC_EN | DDR_CDR1_ODT(DDR_CDR_ODT_80ohm);
  122. popts->ddr_cdr2 = DDR_CDR2_ODT(DDR_CDR_ODT_80ohm) |
  123. DDR_CDR2_VREF_OVRD(70); /* Vref = 70% */
  124. #else
  125. popts->ddr_cdr1 = DDR_CDR1_DHC_EN | DDR_CDR1_ODT(DDR_CDR_ODT_75ohm);
  126. popts->ddr_cdr2 = DDR_CDR2_ODT(DDR_CDR_ODT_75ohm);
  127. /* optimize cpo for erratum A-009942 */
  128. popts->cpo_sample = 0x5f;
  129. #endif
  130. /* T1023 supports max DDR bus 32bit width, T1024 supports DDR 64bit,
  131. * set DDR bus width to 32bit for T1023
  132. */
  133. if (cpu->soc_ver == SVR_T1023)
  134. popts->data_bus_width = DDR_DATA_BUS_WIDTH_32;
  135. #ifdef CONFIG_FORCE_DDR_DATA_BUS_WIDTH_32
  136. /* for DDR bus 32bit test on T1024 */
  137. popts->data_bus_width = DDR_DATA_BUS_WIDTH_32;
  138. #endif
  139. }
  140. #if defined(CONFIG_DEEP_SLEEP)
  141. void board_mem_sleep_setup(void)
  142. {
  143. void __iomem *qixis_base = (void *)QIXIS_BASE;
  144. /* does not provide HW signals for power management */
  145. clrbits_8(qixis_base + 0x21, 0x2);
  146. /* Disable MCKE isolation */
  147. gpio_set_value(2, 0);
  148. udelay(1);
  149. }
  150. #endif
  151. int dram_init(void)
  152. {
  153. phys_size_t dram_size;
  154. #if defined(CONFIG_SPL_BUILD) || !defined(CONFIG_RAMBOOT_PBL)
  155. puts("Initializing....using SPD\n");
  156. dram_size = fsl_ddr_sdram();
  157. #else
  158. /* DDR has been initialised by first stage boot loader */
  159. dram_size = fsl_ddr_sdram_size();
  160. #endif
  161. dram_size = setup_ddr_tlbs(dram_size / 0x100000);
  162. dram_size *= 0x100000;
  163. #if defined(CONFIG_DEEP_SLEEP) && !defined(CONFIG_SPL_BUILD)
  164. fsl_dp_resume();
  165. #endif
  166. gd->ram_size = dram_size;
  167. return 0;
  168. }