ddr4_dvfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright 2018-2023 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <drivers/delay_timer.h>
  7. #include <lib/mmio.h>
  8. #include <dram.h>
  9. void ddr4_mr_write(uint32_t mr, uint32_t data, uint32_t mr_type,
  10. uint32_t rank, uint32_t dram_type)
  11. {
  12. uint32_t val, mr_mirror, data_mirror;
  13. /*
  14. * 1. Poll MRSTAT.mr_wr_busy until it is 0 to make sure
  15. * that there is no outstanding MR transAction.
  16. */
  17. /*
  18. * ERR050712:
  19. * When performing a software driven MR access, the following sequence
  20. * must be done automatically before performing other APB register accesses.
  21. * 1. Set MRCTRL0.mr_wr=1
  22. * 2. Check for MRSTAT.mr_wr_busy=0. If not, go to step (2)
  23. * 3. Check for MRSTAT.mr_wr_busy=0 again (for the second time). If not, go to step (2)
  24. */
  25. mmio_setbits_32(DDRC_MRCTRL0(0), BIT(31));
  26. do {
  27. while (mmio_read_32(DDRC_MRSTAT(0)) & 0x1) {
  28. ;
  29. }
  30. } while (mmio_read_32(DDRC_MRSTAT(0)) & 0x1);
  31. /*
  32. * 2. Write the MRCTRL0.mr_type, MRCTRL0.mr_addr, MRCTRL0.mr_rank
  33. * and (for MRWs) MRCTRL1.mr_data to define the MR transaction.
  34. */
  35. val = mmio_read_32(DDRC_DIMMCTL(0));
  36. if ((val & 0x2) && (rank == 0x2)) {
  37. mr_mirror = (mr & 0x4) | ((mr & 0x1) << 1) | ((mr & 0x2) >> 1); /* BA0, BA1 swap */
  38. if (dram_type == DDRC_DDR4) {
  39. data_mirror = (data & 0x1607) | ((data & 0x8) << 1) | ((data & 0x10) >> 1) |
  40. ((data & 0x20) << 1) | ((data & 0x40) >> 1) | ((data & 0x80) << 1) |
  41. ((data & 0x100) >> 1) | ((data & 0x800) << 2) | ((data & 0x2000) >> 2) ;
  42. } else {
  43. data_mirror = (data & 0xfe07) | ((data & 0x8) << 1) | ((data & 0x10) >> 1) |
  44. ((data & 0x20) << 1) | ((data & 0x40) >> 1) | ((data & 0x80) << 1) |
  45. ((data & 0x100) >> 1);
  46. }
  47. } else {
  48. mr_mirror = mr;
  49. data_mirror = data;
  50. }
  51. mmio_write_32(DDRC_MRCTRL0(0), mr_type | (mr_mirror << 12) | (rank << 4));
  52. mmio_write_32(DDRC_MRCTRL1(0), data_mirror);
  53. /*
  54. * 3. In a separate APB transaction, write the MRCTRL0.mr_wr to 1.
  55. * This bit is self-clearing, and triggers the MR transaction.
  56. * The uMCTL2 then asserts the MRSTAT.mr_wr_busy while it performs
  57. * the MR transaction to SDRAM, and no further accesses can be
  58. * initiated until it is deasserted.
  59. */
  60. mmio_setbits_32(DDRC_MRCTRL0(0), BIT(31));
  61. while (mmio_read_32(DDRC_MRSTAT(0))) {
  62. ;
  63. }
  64. }
  65. void dram_cfg_all_mr(struct dram_info *info, uint32_t pstate)
  66. {
  67. uint32_t num_rank = info->num_rank;
  68. uint32_t dram_type = info->dram_type;
  69. /*
  70. * 15. Perform MRS commands as required to re-program
  71. * timing registers in the SDRAM for the new frequency
  72. * (in particular, CL, CWL and WR may need to be changed).
  73. */
  74. for (int i = 1; i <= num_rank; i++) {
  75. for (int j = 0; j < 6; j++) {
  76. ddr4_mr_write(j, info->mr_table[pstate][j], 0, i, dram_type);
  77. }
  78. ddr4_mr_write(6, info->mr_table[pstate][7], 0, i, dram_type);
  79. }
  80. }
  81. void sw_pstate(uint32_t pstate, uint32_t drate)
  82. {
  83. uint32_t val;
  84. mmio_write_32(DDRC_SWCTL(0), 0x0);
  85. /*
  86. * Update any registers which may be required to
  87. * change for the new frequency.
  88. */
  89. mmio_write_32(DDRC_MSTR2(0), pstate);
  90. mmio_setbits_32(DDRC_MSTR(0), (0x1 << 29));
  91. /*
  92. * Toggle RFSHCTL3.refresh_update_level to allow the
  93. * new refresh-related register values to propagate
  94. * to the refresh logic.
  95. */
  96. val = mmio_read_32(DDRC_RFSHCTL3(0));
  97. if (val & 0x2) {
  98. mmio_write_32(DDRC_RFSHCTL3(0), val & 0xFFFFFFFD);
  99. } else {
  100. mmio_write_32(DDRC_RFSHCTL3(0), val | 0x2);
  101. }
  102. /*
  103. * 19. If required, trigger the initialization in the PHY.
  104. * If using the gen2 multiPHY, PLL initialization should
  105. * be triggered at this point. See the PHY databook for
  106. * details about the frequency change procedure.
  107. */
  108. mmio_write_32(DDRC_DFIMISC(0), 0x00000000 | (pstate << 8));
  109. mmio_write_32(DDRC_DFIMISC(0), 0x00000020 | (pstate << 8));
  110. /* wait DFISTAT.dfi_init_complete to 0 */
  111. while (mmio_read_32(DDRC_DFISTAT(0)) & 0x1) {
  112. ;
  113. }
  114. /* change the clock to the target frequency */
  115. dram_clock_switch(drate, false);
  116. mmio_write_32(DDRC_DFIMISC(0), 0x00000000 | (pstate << 8));
  117. /* wait DFISTAT.dfi_init_complete to 1 */
  118. while (!(mmio_read_32(DDRC_DFISTAT(0)) & 0x1)) {
  119. ;
  120. }
  121. /*
  122. * When changing frequencies the controller may violate the JEDEC
  123. * requirement that no more than 16 refreshes should be issued within
  124. * 2*tREFI. These extra refreshes are not expected to cause a problem
  125. * in the SDRAM. This issue can be avoided by waiting for at least 2*tREFI
  126. * before exiting self-refresh in step 19.
  127. */
  128. udelay(14);
  129. /* 14. Exit the self-refresh state by setting PWRCTL.selfref_sw = 0. */
  130. mmio_clrbits_32(DDRC_PWRCTL(0), (1 << 5));
  131. while ((mmio_read_32(DDRC_STAT(0)) & 0x3f) == 0x23) {
  132. ;
  133. }
  134. }
  135. void ddr4_swffc(struct dram_info *info, unsigned int pstate)
  136. {
  137. uint32_t drate = info->timing_info->fsp_table[pstate];
  138. /*
  139. * 1. set SWCTL.sw_done to disable quasi-dynamic register
  140. * programming outside reset.
  141. */
  142. mmio_write_32(DDRC_SWCTL(0), 0x0);
  143. /*
  144. * 2. Write 0 to PCTRL_n.port_en. This blocks AXI port(s)
  145. * from taking any transaction (blocks traffic on AXI ports).
  146. */
  147. mmio_write_32(DDRC_PCTRL_0(0), 0x0);
  148. /*
  149. * 3. Poll PSTAT.rd_port_busy_n=0 and PSTAT.wr_port_busy_n=0.
  150. * Wait until all AXI ports are idle (the uMCTL2 core has to
  151. * be idle).
  152. */
  153. while (mmio_read_32(DDRC_PSTAT(0)) & 0x10001) {
  154. ;
  155. }
  156. /*
  157. * 4. Write 0 to SBRCTL.scrub_en. Disable SBR, required only if
  158. * SBR instantiated.
  159. * 5. Poll SBRSTAT.scrub_busy=0.
  160. * 6. Set DERATEEN.derate_enable = 0, if DERATEEN.derate_eanble = 1
  161. * and the read latency (RL) value needs to change after the frequency
  162. * change (LPDDR2/3/4 only).
  163. * 7. Set DBG1.dis_hif=1 so that no new commands will be accepted by the uMCTL2.
  164. */
  165. mmio_setbits_32(DDRC_DBG1(0), (0x1 << 1));
  166. /*
  167. * 8. Poll DBGCAM.dbg_wr_q_empty and DBGCAM.dbg_rd_q_empty to ensure
  168. * that write and read data buffers are empty.
  169. */
  170. while ((mmio_read_32(DDRC_DBGCAM(0)) & 0x06000000) != 0x06000000) {
  171. ;
  172. }
  173. /*
  174. * 9. For DDR4, update MR6 with the new tDLLK value via the Mode
  175. * Register Write signals
  176. * 10. Set DFILPCFG0.dfi_lp_en_sr = 0, if DFILPCFG0.dfi_lp_en_sr = 1,
  177. * and wait until DFISTAT.dfi_lp_ack
  178. * 11. If DFI PHY Master interface is active in uMCTL2, then disable it
  179. * 12. Wait until STAT.operating_mode[1:0]!=11 indicating that the
  180. * controller is not in self-refresh mode.
  181. */
  182. if ((mmio_read_32(DDRC_STAT(0)) & 0x3) == 0x3) {
  183. VERBOSE("DRAM is in Self Refresh\n");
  184. }
  185. /*
  186. * 13. Assert PWRCTL.selfref_sw for the DWC_ddr_umctl2 core to enter
  187. * the self-refresh mode.
  188. */
  189. mmio_setbits_32(DDRC_PWRCTL(0), (1 << 5));
  190. /*
  191. * 14. Wait until STAT.operating_mode[1:0]==11 indicating that the
  192. * controller core is in self-refresh mode.
  193. */
  194. while ((mmio_read_32(DDRC_STAT(0)) & 0x3f) != 0x23) {
  195. ;
  196. }
  197. sw_pstate(pstate, drate);
  198. dram_cfg_all_mr(info, pstate);
  199. /* 23. Enable HIF commands by setting DBG1.dis_hif=0. */
  200. mmio_clrbits_32(DDRC_DBG1(0), (0x1 << 1));
  201. /*
  202. * 24. Reset DERATEEN.derate_enable = 1 if DERATEEN.derate_enable
  203. * has been set to 0 in step 6.
  204. * 25. If DFI PHY Master interface was active before step 11 then
  205. * enable it back by programming DFIPHYMSTR.phymstr_en = 1'b1.
  206. * 26. Write 1 to PCTRL_n.port_en. AXI port(s) are no longer blocked
  207. * from taking transactions (Re-enable traffic on AXI ports)
  208. */
  209. mmio_write_32(DDRC_PCTRL_0(0), 0x1);
  210. /*
  211. * 27. Write 1 to SBRCTL.scrub_en. Enable SBR if desired, only
  212. * required if SBR instantiated.
  213. */
  214. /*
  215. * set SWCTL.sw_done to enable quasi-dynamic register programming
  216. * outside reset.
  217. */
  218. mmio_write_32(DDRC_SWCTL(0), 0x1);
  219. /* wait SWSTAT.sw_done_ack to 1 */
  220. while (!(mmio_read_32(DDRC_SWSTAT(0)) & 0x1)) {
  221. ;
  222. }
  223. }