lpddr4_dvfs.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2018-2023 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <lib/mmio.h>
  7. #include <dram.h>
  8. static void lpddr4_mr_write(uint32_t mr_rank, uint32_t mr_addr, uint32_t mr_data)
  9. {
  10. /*
  11. * 1. Poll MRSTAT.mr_wr_busy until it is 0. This checks that there
  12. * is no outstanding MR transaction. No
  13. * writes should be performed to MRCTRL0 and MRCTRL1 if MRSTAT.mr_wr_busy = 1.
  14. */
  15. while (mmio_read_32(DDRC_MRSTAT(0)) & 0x1)
  16. ;
  17. /*
  18. * 2. Write the MRCTRL0.mr_type, MRCTRL0.mr_addr,
  19. * MRCTRL0.mr_rank and (for MRWs)
  20. * MRCTRL1.mr_data to define the MR transaction.
  21. */
  22. mmio_write_32(DDRC_MRCTRL0(0), (mr_rank << 4));
  23. mmio_write_32(DDRC_MRCTRL1(0), (mr_addr << 8) | mr_data);
  24. mmio_setbits_32(DDRC_MRCTRL0(0), BIT(31));
  25. }
  26. void lpddr4_swffc(struct dram_info *info, unsigned int init_fsp,
  27. unsigned int fsp_index)
  28. {
  29. uint32_t mr, emr, emr2, emr3;
  30. uint32_t mr11, mr12, mr22, mr14;
  31. uint32_t val;
  32. uint32_t derate_backup[3];
  33. uint32_t (*mr_data)[8];
  34. uint32_t phy_master;
  35. /* 1. program targetd UMCTL2_REGS_FREQ1/2/3,already done, skip it. */
  36. /* 2. MR13.FSP-WR=1, MRW to update MR registers */
  37. mr_data = info->mr_table;
  38. mr = mr_data[fsp_index][0];
  39. emr = mr_data[fsp_index][1];
  40. emr2 = mr_data[fsp_index][2];
  41. emr3 = mr_data[fsp_index][3];
  42. mr11 = mr_data[fsp_index][4];
  43. mr12 = mr_data[fsp_index][5];
  44. mr22 = mr_data[fsp_index][6];
  45. mr14 = mr_data[fsp_index][7];
  46. val = (init_fsp == 1) ? 0x2 << 6 : 0x1 << 6;
  47. emr3 = (emr3 & 0x003f) | val | 0x0d00;
  48. /* 12. set PWRCTL.selfref_en=0 */
  49. mmio_clrbits_32(DDRC_PWRCTL(0), 0xf);
  50. phy_master = mmio_read_32(DDRC_DFIPHYMSTR(0));
  51. /* It is more safe to config it here */
  52. mmio_clrbits_32(DDRC_DFIPHYMSTR(0), 0x1);
  53. lpddr4_mr_write(3, 13, emr3);
  54. lpddr4_mr_write(3, 1, mr);
  55. lpddr4_mr_write(3, 2, emr);
  56. lpddr4_mr_write(3, 3, emr2);
  57. lpddr4_mr_write(3, 11, mr11);
  58. lpddr4_mr_write(3, 12, mr12);
  59. lpddr4_mr_write(3, 14, mr14);
  60. lpddr4_mr_write(3, 22, mr22);
  61. do {
  62. val = mmio_read_32(DDRC_MRSTAT(0));
  63. } while (val & 0x1);
  64. /* 3. disable AXI ports */
  65. mmio_write_32(DDRC_PCTRL_0(0), 0x0);
  66. /* 4.Poll PSTAT.rd_port_busy_n=0 and PSTAT.wr_port_busy_n=0. */
  67. do {
  68. val = mmio_read_32(DDRC_PSTAT(0));
  69. } while (val != 0);
  70. /* 6.disable SBRCTL.scrub_en, skip if never enable it */
  71. /* 7.poll SBRSTAT.scrub_busy Q2: should skip phy master if never enable it */
  72. /* Disable phy master */
  73. #ifdef DFILP_SPT
  74. /* 8. disable DFI LP */
  75. /* DFILPCFG0.dfi_lp_en_sr */
  76. val = mmio_read_32(DDRC_DFILPCFG0(0));
  77. if (val & 0x100) {
  78. mmio_write_32(DDRC_DFILPCFG0(0), 0x0);
  79. do {
  80. val = mmio_read_32(DDRC_DFISTAT(0)); // dfi_lp_ack
  81. val2 = mmio_read_32(DDRC_STAT(0)); // operating_mode
  82. } while (((val & 0x2) == 0x2) && ((val2 & 0x7) == 3));
  83. }
  84. #endif
  85. /* 9. wait until in normal or power down states */
  86. do {
  87. /* operating_mode */
  88. val = mmio_read_32(DDRC_STAT(0));
  89. } while (((val & 0x7) != 1) && ((val & 0x7) != 2));
  90. /* 10. Disable automatic derating: derate_enable */
  91. val = mmio_read_32(DDRC_DERATEEN(0));
  92. derate_backup[0] = val;
  93. mmio_clrbits_32(DDRC_DERATEEN(0), 0x1);
  94. val = mmio_read_32(DDRC_FREQ1_DERATEEN(0));
  95. derate_backup[1] = val;
  96. mmio_clrbits_32(DDRC_FREQ1_DERATEEN(0), 0x1);
  97. val = mmio_read_32(DDRC_FREQ2_DERATEEN(0));
  98. derate_backup[2] = val;
  99. mmio_clrbits_32(DDRC_FREQ2_DERATEEN(0), 0x1);
  100. /* 11. disable automatic ZQ calibration */
  101. mmio_setbits_32(DDRC_ZQCTL0(0), BIT(31));
  102. mmio_setbits_32(DDRC_FREQ1_ZQCTL0(0), BIT(31));
  103. mmio_setbits_32(DDRC_FREQ2_ZQCTL0(0), BIT(31));
  104. /* 12. set PWRCTL.selfref_en=0 */
  105. mmio_clrbits_32(DDRC_PWRCTL(0), 0x1);
  106. /* 13.Poll STAT.operating_mode is in "Normal" (001) or "Power-down" (010) */
  107. do {
  108. val = mmio_read_32(DDRC_STAT(0));
  109. } while (((val & 0x7) != 1) && ((val & 0x7) != 2));
  110. /* 14-15. trigger SW SR */
  111. /* bit 5: selfref_sw, bit 6: stay_in_selfref */
  112. mmio_setbits_32(DDRC_PWRCTL(0), 0x60);
  113. /* 16. Poll STAT.selfref_state in "Self Refresh 1" */
  114. do {
  115. val = mmio_read_32(DDRC_STAT(0));
  116. } while ((val & 0x300) != 0x100);
  117. /* 17. disable dq */
  118. mmio_setbits_32(DDRC_DBG1(0), 0x1);
  119. /* 18. Poll DBGCAM.wr_data_pipeline_empty and DBGCAM.rd_data_pipeline_empty */
  120. do {
  121. val = mmio_read_32(DDRC_DBGCAM(0));
  122. val &= 0x30000000;
  123. } while (val != 0x30000000);
  124. /* 19. change MR13.FSP-OP to new FSP and MR13.VRCG to high current */
  125. emr3 = (((~init_fsp) & 0x1) << 7) | (0x1 << 3) | (emr3 & 0x0077) | 0x0d00;
  126. lpddr4_mr_write(3, 13, emr3);
  127. /* 20. enter SR Power Down */
  128. mmio_clrsetbits_32(DDRC_PWRCTL(0), 0x60, 0x20);
  129. /* 21. Poll STAT.selfref_state is in "SR Power down" */
  130. do {
  131. val = mmio_read_32(DDRC_STAT(0));
  132. } while ((val & 0x300) != 0x200);
  133. /* 22. set dfi_init_complete_en = 0 */
  134. /* 23. switch clock */
  135. /* set SWCTL.dw_done to 0 */
  136. mmio_write_32(DDRC_SWCTL(0), 0x0000);
  137. /* 24. program frequency mode=1(bit 29), target_frequency=target_freq (bit 29) */
  138. mmio_write_32(DDRC_MSTR2(0), fsp_index);
  139. /* 25. DBICTL for FSP-OP[1], skip it if never enable it */
  140. /* 26.trigger initialization in the PHY */
  141. /* Q3: if refresh level is updated, then should program */
  142. /* as updating refresh, need to toggle refresh_update_level signal */
  143. val = mmio_read_32(DDRC_RFSHCTL3(0));
  144. val = val ^ 0x2;
  145. mmio_write_32(DDRC_RFSHCTL3(0), val);
  146. /* Q4: only for legacy PHY, so here can skipped */
  147. /* dfi_frequency -> 0x1x */
  148. val = mmio_read_32(DDRC_DFIMISC(0));
  149. val &= 0xFE;
  150. val |= (fsp_index << 8);
  151. mmio_write_32(DDRC_DFIMISC(0), val);
  152. /* dfi_init_start */
  153. val |= 0x20;
  154. mmio_write_32(DDRC_DFIMISC(0), val);
  155. /* polling dfi_init_complete de-assert */
  156. do {
  157. val = mmio_read_32(DDRC_DFISTAT(0));
  158. } while ((val & 0x1) == 0x1);
  159. /* change the clock frequency */
  160. dram_clock_switch(info->timing_info->fsp_table[fsp_index], info->bypass_mode);
  161. /* dfi_init_start de-assert */
  162. mmio_clrbits_32(DDRC_DFIMISC(0), 0x20);
  163. /* polling dfi_init_complete re-assert */
  164. do {
  165. val = mmio_read_32(DDRC_DFISTAT(0));
  166. } while ((val & 0x1) == 0x0);
  167. /* 27. set ZQCTL0.dis_srx_zqcl = 1 */
  168. if (fsp_index == 0) {
  169. mmio_setbits_32(DDRC_ZQCTL0(0), BIT(30));
  170. } else if (fsp_index == 1) {
  171. mmio_setbits_32(DDRC_FREQ1_ZQCTL0(0), BIT(30));
  172. } else {
  173. mmio_setbits_32(DDRC_FREQ2_ZQCTL0(0), BIT(30));
  174. }
  175. /* 28,29. exit "self refresh power down" to stay "self refresh 2" */
  176. /* exit SR power down */
  177. mmio_clrsetbits_32(DDRC_PWRCTL(0), 0x60, 0x40);
  178. /* 30. Poll STAT.selfref_state in "Self refresh 2" */
  179. do {
  180. val = mmio_read_32(DDRC_STAT(0));
  181. } while ((val & 0x300) != 0x300);
  182. /* 31. change MR13.VRCG to normal */
  183. emr3 = (emr3 & 0x00f7) | 0x0d00;
  184. lpddr4_mr_write(3, 13, emr3);
  185. /* restore the PHY master */
  186. mmio_write_32(DDRC_DFIPHYMSTR(0), phy_master);
  187. /* 32. issue ZQ if required: zq_calib_short, bit 4 */
  188. /* polling zq_calib_short_busy */
  189. mmio_setbits_32(DDRC_DBGCMD(0), 0x10);
  190. do {
  191. val = mmio_read_32(DDRC_DBGSTAT(0));
  192. } while ((val & 0x10) != 0x0);
  193. /* 33. Reset ZQCTL0.dis_srx_zqcl=0 */
  194. if (fsp_index == 1)
  195. mmio_clrbits_32(DDRC_FREQ1_ZQCTL0(0), BIT(30));
  196. else if (fsp_index == 2)
  197. mmio_clrbits_32(DDRC_FREQ2_ZQCTL0(0), BIT(30));
  198. else
  199. mmio_clrbits_32(DDRC_ZQCTL0(0), BIT(30));
  200. /* set SWCTL.dw_done to 1 and poll SWSTAT.sw_done_ack=1 */
  201. mmio_write_32(DDRC_SWCTL(0), 0x1);
  202. /* wait SWSTAT.sw_done_ack to 1 */
  203. do {
  204. val = mmio_read_32(DDRC_SWSTAT(0));
  205. } while ((val & 0x1) == 0x0);
  206. /* 34. set PWRCTL.stay_in_selfreh=0, exit SR */
  207. mmio_clrbits_32(DDRC_PWRCTL(0), 0x40);
  208. /* wait tXSR */
  209. /* 35. Poll STAT.selfref_state in "Idle" */
  210. do {
  211. val = mmio_read_32(DDRC_STAT(0));
  212. } while ((val & 0x300) != 0x0);
  213. #ifdef DFILP_SPT
  214. /* 36. restore dfi_lp.dfi_lp_en_sr */
  215. mmio_setbits_32(DDRC_DFILPCFG0(0), BIT(8));
  216. #endif
  217. /* 37. re-enable CAM: dis_dq */
  218. mmio_clrbits_32(DDRC_DBG1(0), 0x1);
  219. /* 38. re-enable automatic SR: selfref_en */
  220. mmio_setbits_32(DDRC_PWRCTL(0), 0x1);
  221. /* 39. re-enable automatic ZQ: dis_auto_zq=0 */
  222. /* disable automatic ZQ calibration */
  223. if (fsp_index == 1)
  224. mmio_clrbits_32(DDRC_FREQ1_ZQCTL0(0), BIT(31));
  225. else if (fsp_index == 2)
  226. mmio_clrbits_32(DDRC_FREQ2_ZQCTL0(0), BIT(31));
  227. else
  228. mmio_clrbits_32(DDRC_ZQCTL0(0), BIT(31));
  229. /* 40. re-emable automatic derating: derate_enable */
  230. mmio_write_32(DDRC_DERATEEN(0), derate_backup[0]);
  231. mmio_write_32(DDRC_FREQ1_DERATEEN(0), derate_backup[1]);
  232. mmio_write_32(DDRC_FREQ2_DERATEEN(0), derate_backup[2]);
  233. /* 41. write 1 to PCTRL.port_en */
  234. mmio_write_32(DDRC_PCTRL_0(0), 0x1);
  235. /* 42. enable SBRCTL.scrub_en, skip if never enable it */
  236. }