ddr_init.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <common/debug.h>
  15. #include <ddr.h>
  16. #include <lib/utils.h>
  17. #include <load_img.h>
  18. #include "plat_common.h"
  19. #include <platform_def.h>
  20. #ifdef CONFIG_STATIC_DDR
  21. const struct ddr_cfg_regs static_1600 = {
  22. .cs[0].config = U(0xA8050322),
  23. .cs[1].config = U(0x80000322),
  24. .cs[0].bnds = U(0x3FF),
  25. .cs[1].bnds = U(0x3FF),
  26. .sdram_cfg[0] = U(0xE5044000),
  27. .sdram_cfg[1] = U(0x401011),
  28. .timing_cfg[0] = U(0xFF550018),
  29. .timing_cfg[1] = U(0xBAB48C42),
  30. .timing_cfg[2] = U(0x48C111),
  31. .timing_cfg[3] = U(0x10C1000),
  32. .timing_cfg[4] = U(0x2),
  33. .timing_cfg[5] = U(0x3401400),
  34. .timing_cfg[7] = U(0x13300000),
  35. .timing_cfg[8] = U(0x2114600),
  36. .sdram_mode[0] = U(0x6010210),
  37. .sdram_mode[8] = U(0x500),
  38. .sdram_mode[9] = U(0x4240000),
  39. .interval = U(0x18600000),
  40. .data_init = U(0xDEADBEEF),
  41. .zq_cntl = U(0x8A090705),
  42. };
  43. const struct dimm_params static_dimm = {
  44. .rdimm = U(0),
  45. .primary_sdram_width = U(64),
  46. .ec_sdram_width = U(8),
  47. .n_ranks = U(2),
  48. .device_width = U(8),
  49. .mirrored_dimm = U(1),
  50. };
  51. /* Sample code using two UDIMM MT18ASF1G72AZ-2G6B1, on each DDR controller */
  52. unsigned long long board_static_ddr(struct ddr_info *priv)
  53. {
  54. memcpy(&priv->ddr_reg, &static_1600, sizeof(static_1600));
  55. memcpy(&priv->dimm, &static_dimm, sizeof(static_dimm));
  56. priv->conf.cs_on_dimm[0] = 0x3;
  57. ddr_board_options(priv);
  58. compute_ddr_phy(priv);
  59. return ULL(0x400000000);
  60. }
  61. #elif defined(CONFIG_DDR_NODIMM)
  62. /*
  63. * Sample code to bypass reading SPD. This is a sample, not recommended
  64. * for boards with slots. DDR model number: UDIMM MT18ASF1G72AZ-2G6B1.
  65. */
  66. const struct dimm_params ddr_raw_timing = {
  67. .n_ranks = U(2),
  68. .rank_density = U(4294967296u),
  69. .capacity = U(8589934592u),
  70. .primary_sdram_width = U(64),
  71. .ec_sdram_width = U(8),
  72. .device_width = U(8),
  73. .die_density = U(0x4),
  74. .rdimm = U(0),
  75. .mirrored_dimm = U(1),
  76. .n_row_addr = U(15),
  77. .n_col_addr = U(10),
  78. .bank_addr_bits = U(0),
  79. .bank_group_bits = U(2),
  80. .edc_config = U(2),
  81. .burst_lengths_bitmask = U(0x0c),
  82. .tckmin_x_ps = 750,
  83. .tckmax_ps = 1600,
  84. .caslat_x = U(0x00FFFC00),
  85. .taa_ps = 13750,
  86. .trcd_ps = 13750,
  87. .trp_ps = 13750,
  88. .tras_ps = 32000,
  89. .trc_ps = 457500,
  90. .twr_ps = 15000,
  91. .trfc1_ps = 260000,
  92. .trfc2_ps = 160000,
  93. .trfc4_ps = 110000,
  94. .tfaw_ps = 21000,
  95. .trrds_ps = 3000,
  96. .trrdl_ps = 4900,
  97. .tccdl_ps = 5000,
  98. .refresh_rate_ps = U(7800000),
  99. };
  100. int ddr_get_ddr_params(struct dimm_params *pdimm,
  101. struct ddr_conf *conf)
  102. {
  103. static const char dimm_model[] = "Fixed DDR on board";
  104. conf->dimm_in_use[0] = 1; /* Modify accordingly */
  105. memcpy(pdimm, &ddr_raw_timing, sizeof(struct dimm_params));
  106. memcpy(pdimm->mpart, dimm_model, sizeof(dimm_model) - 1);
  107. /* valid DIMM mask, change accordingly, together with dimm_on_ctlr. */
  108. return 0x5;
  109. }
  110. #endif /* CONFIG_DDR_NODIMM */
  111. int ddr_board_options(struct ddr_info *priv)
  112. {
  113. struct memctl_opt *popts = &priv->opt;
  114. const struct ddr_conf *conf = &priv->conf;
  115. popts->vref_dimm = U(0x24); /* range 1, 83.4% */
  116. popts->rtt_override = 0;
  117. popts->rtt_park = U(240);
  118. popts->otf_burst_chop_en = 0;
  119. popts->burst_length = U(DDR_BL8);
  120. popts->trwt_override = U(1);
  121. popts->bstopre = U(0); /* auto precharge */
  122. popts->addr_hash = 1;
  123. /* Set ODT impedance on PHY side */
  124. switch (conf->cs_on_dimm[1]) {
  125. case 0xc: /* Two slots dual rank */
  126. case 0x4: /* Two slots single rank, not valid for interleaving */
  127. popts->trwt = U(0xf);
  128. popts->twrt = U(0x7);
  129. popts->trrt = U(0x7);
  130. popts->twwt = U(0x7);
  131. popts->vref_phy = U(0x6B); /* 83.6% */
  132. popts->odt = U(60);
  133. popts->phy_tx_impedance = U(28);
  134. break;
  135. case 0: /* One slot used */
  136. default:
  137. popts->trwt = U(0x3);
  138. popts->twrt = U(0x3);
  139. popts->trrt = U(0x3);
  140. popts->twwt = U(0x3);
  141. popts->vref_phy = U(0x60); /* 75% */
  142. popts->odt = U(48);
  143. popts->phy_tx_impedance = U(28);
  144. break;
  145. }
  146. return 0;
  147. }
  148. long long init_ddr(void)
  149. {
  150. int spd_addr[] = { 0x51, 0x52, 0x53, 0x54 };
  151. struct ddr_info info;
  152. struct sysinfo sys;
  153. long long dram_size;
  154. zeromem(&sys, sizeof(sys));
  155. if (get_clocks(&sys) != 0) {
  156. ERROR("System clocks are not set\n");
  157. panic();
  158. }
  159. debug("platform clock %lu\n", sys.freq_platform);
  160. debug("DDR PLL1 %lu\n", sys.freq_ddr_pll0);
  161. debug("DDR PLL2 %lu\n", sys.freq_ddr_pll1);
  162. zeromem(&info, sizeof(info));
  163. /* Set two DDRC. Unused DDRC will be removed automatically. */
  164. info.num_ctlrs = NUM_OF_DDRC;
  165. info.spd_addr = spd_addr;
  166. info.ddr[0] = (void *)NXP_DDR_ADDR;
  167. info.ddr[1] = (void *)NXP_DDR2_ADDR;
  168. info.phy[0] = (void *)NXP_DDR_PHY1_ADDR;
  169. info.phy[1] = (void *)NXP_DDR_PHY2_ADDR;
  170. info.clk = get_ddr_freq(&sys, 0);
  171. info.img_loadr = load_img;
  172. info.phy_gen2_fw_img_buf = PHY_GEN2_FW_IMAGE_BUFFER;
  173. if (info.clk == 0) {
  174. info.clk = get_ddr_freq(&sys, 1);
  175. }
  176. info.dimm_on_ctlr = DDRC_NUM_DIMM;
  177. info.warm_boot_flag = DDR_WRM_BOOT_NT_SUPPORTED;
  178. dram_size = dram_init(&info
  179. #if defined(NXP_HAS_CCN504) || defined(NXP_HAS_CCN508)
  180. , NXP_CCN_HN_F_0_ADDR
  181. #endif
  182. );
  183. if (dram_size < 0) {
  184. ERROR("DDR init failed.\n");
  185. }
  186. return dram_size;
  187. }