stm32mp_ddr.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (C) 2022-2024, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <drivers/delay_timer.h>
  8. #include <drivers/st/stm32mp_ddr.h>
  9. #include <drivers/st/stm32mp_ddrctrl_regs.h>
  10. #include <lib/mmio.h>
  11. #include <platform_def.h>
  12. #define INVALID_OFFSET 0xFFU
  13. static bool axi_port_reenable_request;
  14. static bool host_interface_reenable_request;
  15. static uintptr_t get_base_addr(const struct stm32mp_ddr_priv *priv, enum stm32mp_ddr_base_type base)
  16. {
  17. if (base == DDRPHY_BASE) {
  18. return (uintptr_t)priv->phy;
  19. } else {
  20. return (uintptr_t)priv->ctl;
  21. }
  22. }
  23. void stm32mp_ddr_set_reg(const struct stm32mp_ddr_priv *priv, enum stm32mp_ddr_reg_type type,
  24. const void *param, const struct stm32mp_ddr_reg_info *ddr_registers)
  25. {
  26. unsigned int i;
  27. unsigned int value;
  28. enum stm32mp_ddr_base_type base = ddr_registers[type].base;
  29. uintptr_t base_addr = get_base_addr(priv, base);
  30. const struct stm32mp_ddr_reg_desc *desc = ddr_registers[type].desc;
  31. VERBOSE("init %s\n", ddr_registers[type].name);
  32. for (i = 0; i < ddr_registers[type].size; i++) {
  33. uintptr_t ptr = base_addr + desc[i].offset;
  34. if (desc[i].par_offset == INVALID_OFFSET) {
  35. ERROR("invalid parameter offset for %s - index %u",
  36. ddr_registers[type].name, i);
  37. panic();
  38. } else {
  39. #if !STM32MP13 && !STM32MP15
  40. if (desc[i].qd) {
  41. stm32mp_ddr_start_sw_done(priv->ctl);
  42. }
  43. #endif
  44. value = *((uint32_t *)((uintptr_t)param +
  45. desc[i].par_offset));
  46. mmio_write_32(ptr, value);
  47. #if !STM32MP13 && !STM32MP15
  48. if (desc[i].qd) {
  49. stm32mp_ddr_wait_sw_done_ack(priv->ctl);
  50. }
  51. #endif
  52. }
  53. }
  54. }
  55. /* Start quasi dynamic register update */
  56. void stm32mp_ddr_start_sw_done(struct stm32mp_ddrctl *ctl)
  57. {
  58. mmio_clrbits_32((uintptr_t)&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
  59. VERBOSE("[0x%lx] swctl = 0x%x\n",
  60. (uintptr_t)&ctl->swctl, mmio_read_32((uintptr_t)&ctl->swctl));
  61. }
  62. /* Wait quasi dynamic register update */
  63. void stm32mp_ddr_wait_sw_done_ack(struct stm32mp_ddrctl *ctl)
  64. {
  65. uint64_t timeout;
  66. uint32_t swstat;
  67. mmio_setbits_32((uintptr_t)&ctl->swctl, DDRCTRL_SWCTL_SW_DONE);
  68. VERBOSE("[0x%lx] swctl = 0x%x\n",
  69. (uintptr_t)&ctl->swctl, mmio_read_32((uintptr_t)&ctl->swctl));
  70. timeout = timeout_init_us(DDR_TIMEOUT_US_1S);
  71. do {
  72. swstat = mmio_read_32((uintptr_t)&ctl->swstat);
  73. VERBOSE("[0x%lx] swstat = 0x%x ",
  74. (uintptr_t)&ctl->swstat, swstat);
  75. if (timeout_elapsed(timeout)) {
  76. panic();
  77. }
  78. } while ((swstat & DDRCTRL_SWSTAT_SW_DONE_ACK) == 0U);
  79. VERBOSE("[0x%lx] swstat = 0x%x\n",
  80. (uintptr_t)&ctl->swstat, swstat);
  81. }
  82. void stm32mp_ddr_enable_axi_port(struct stm32mp_ddrctl *ctl)
  83. {
  84. /* Enable uMCTL2 AXI port 0 */
  85. mmio_setbits_32((uintptr_t)&ctl->pctrl_0, DDRCTRL_PCTRL_N_PORT_EN);
  86. VERBOSE("[0x%lx] pctrl_0 = 0x%x\n", (uintptr_t)&ctl->pctrl_0,
  87. mmio_read_32((uintptr_t)&ctl->pctrl_0));
  88. #if STM32MP_DDR_DUAL_AXI_PORT
  89. /* Enable uMCTL2 AXI port 1 */
  90. mmio_setbits_32((uintptr_t)&ctl->pctrl_1, DDRCTRL_PCTRL_N_PORT_EN);
  91. VERBOSE("[0x%lx] pctrl_1 = 0x%x\n", (uintptr_t)&ctl->pctrl_1,
  92. mmio_read_32((uintptr_t)&ctl->pctrl_1));
  93. #endif
  94. }
  95. int stm32mp_ddr_disable_axi_port(struct stm32mp_ddrctl *ctl)
  96. {
  97. uint64_t timeout;
  98. uint32_t pstat;
  99. /* Disable uMCTL2 AXI port 0 */
  100. mmio_clrbits_32((uintptr_t)&ctl->pctrl_0, DDRCTRL_PCTRL_N_PORT_EN);
  101. VERBOSE("[0x%lx] pctrl_0 = 0x%x\n", (uintptr_t)&ctl->pctrl_0,
  102. mmio_read_32((uintptr_t)&ctl->pctrl_0));
  103. #if STM32MP_DDR_DUAL_AXI_PORT
  104. /* Disable uMCTL2 AXI port 1 */
  105. mmio_clrbits_32((uintptr_t)&ctl->pctrl_1, DDRCTRL_PCTRL_N_PORT_EN);
  106. VERBOSE("[0x%lx] pctrl_1 = 0x%x\n", (uintptr_t)&ctl->pctrl_1,
  107. mmio_read_32((uintptr_t)&ctl->pctrl_1));
  108. #endif
  109. /*
  110. * Waits until all AXI ports are idle
  111. * Poll PSTAT.rd_port_busy_n = 0
  112. * Poll PSTAT.wr_port_busy_n = 0
  113. */
  114. timeout = timeout_init_us(DDR_TIMEOUT_US_1S);
  115. do {
  116. pstat = mmio_read_32((uintptr_t)&ctl->pstat);
  117. VERBOSE("[0x%lx] pstat = 0x%x ",
  118. (uintptr_t)&ctl->pstat, pstat);
  119. if (timeout_elapsed(timeout)) {
  120. return -1;
  121. }
  122. } while (pstat != 0U);
  123. return 0;
  124. }
  125. static bool ddr_is_axi_port_enabled(struct stm32mp_ddrctl *ctl)
  126. {
  127. return (mmio_read_32((uintptr_t)&ctl->pctrl_0) & DDRCTRL_PCTRL_N_PORT_EN) != 0U;
  128. }
  129. void stm32mp_ddr_enable_host_interface(struct stm32mp_ddrctl *ctl)
  130. {
  131. mmio_clrbits_32((uintptr_t)&ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
  132. VERBOSE("[0x%lx] dbg1 = 0x%x\n",
  133. (uintptr_t)&ctl->dbg1,
  134. mmio_read_32((uintptr_t)&ctl->dbg1));
  135. }
  136. void stm32mp_ddr_disable_host_interface(struct stm32mp_ddrctl *ctl)
  137. {
  138. uint64_t timeout;
  139. uint32_t dbgcam;
  140. int count = 0;
  141. mmio_setbits_32((uintptr_t)&ctl->dbg1, DDRCTRL_DBG1_DIS_HIF);
  142. VERBOSE("[0x%lx] dbg1 = 0x%x\n",
  143. (uintptr_t)&ctl->dbg1,
  144. mmio_read_32((uintptr_t)&ctl->dbg1));
  145. /*
  146. * Waits until all queues and pipelines are empty
  147. * Poll DBGCAM.dbg_wr_q_empty = 1
  148. * Poll DBGCAM.dbg_rd_q_empty = 1
  149. * Poll DBGCAM.dbg_wr_data_pipeline_empty = 1
  150. * Poll DBGCAM.dbg_rd_data_pipeline_empty = 1
  151. *
  152. * data_pipeline fields must be polled twice to ensure
  153. * value propoagation, so count is added to loop condition.
  154. */
  155. timeout = timeout_init_us(DDR_TIMEOUT_US_1S);
  156. do {
  157. dbgcam = mmio_read_32((uintptr_t)&ctl->dbgcam);
  158. VERBOSE("[0x%lx] dbgcam = 0x%x ",
  159. (uintptr_t)&ctl->dbgcam, dbgcam);
  160. if (timeout_elapsed(timeout)) {
  161. panic();
  162. }
  163. count++;
  164. } while (((dbgcam & DDRCTRL_DBG_Q_AND_DATA_PIPELINE_EMPTY) !=
  165. DDRCTRL_DBG_Q_AND_DATA_PIPELINE_EMPTY) || (count < 2));
  166. }
  167. static bool ddr_is_host_interface_enabled(struct stm32mp_ddrctl *ctl)
  168. {
  169. return (mmio_read_32((uintptr_t)&ctl->dbg1) & DDRCTRL_DBG1_DIS_HIF) == 0U;
  170. }
  171. int stm32mp_ddr_sw_selfref_entry(struct stm32mp_ddrctl *ctl)
  172. {
  173. uint64_t timeout;
  174. uint32_t stat;
  175. uint32_t operating_mode;
  176. uint32_t selref_type;
  177. mmio_setbits_32((uintptr_t)&ctl->pwrctl, DDRCTRL_PWRCTL_SELFREF_SW);
  178. VERBOSE("[0x%lx] pwrctl = 0x%x\n",
  179. (uintptr_t)&ctl->pwrctl,
  180. mmio_read_32((uintptr_t)&ctl->pwrctl));
  181. /*
  182. * Wait operating mode change in self-refresh mode
  183. * with STAT.operating_mode[1:0]==11.
  184. * Ensure transition to self-refresh was due to software
  185. * by checking also that STAT.selfref_type[1:0]=2.
  186. */
  187. timeout = timeout_init_us(DDR_TIMEOUT_500US);
  188. while (!timeout_elapsed(timeout)) {
  189. stat = mmio_read_32((uintptr_t)&ctl->stat);
  190. operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK;
  191. selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK;
  192. if ((operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR) &&
  193. (selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR)) {
  194. return 0;
  195. }
  196. }
  197. return -1;
  198. }
  199. void stm32mp_ddr_sw_selfref_exit(struct stm32mp_ddrctl *ctl)
  200. {
  201. mmio_clrbits_32((uintptr_t)&ctl->pwrctl, DDRCTRL_PWRCTL_SELFREF_SW);
  202. VERBOSE("[0x%lx] pwrctl = 0x%x\n",
  203. (uintptr_t)&ctl->pwrctl,
  204. mmio_read_32((uintptr_t)&ctl->pwrctl));
  205. }
  206. void stm32mp_ddr_set_qd3_update_conditions(struct stm32mp_ddrctl *ctl)
  207. {
  208. if (ddr_is_axi_port_enabled(ctl)) {
  209. if (stm32mp_ddr_disable_axi_port(ctl) != 0) {
  210. panic();
  211. }
  212. axi_port_reenable_request = true;
  213. }
  214. if (ddr_is_host_interface_enabled(ctl)) {
  215. stm32mp_ddr_disable_host_interface(ctl);
  216. host_interface_reenable_request = true;
  217. }
  218. stm32mp_ddr_start_sw_done(ctl);
  219. }
  220. void stm32mp_ddr_unset_qd3_update_conditions(struct stm32mp_ddrctl *ctl)
  221. {
  222. stm32mp_ddr_wait_sw_done_ack(ctl);
  223. if (host_interface_reenable_request) {
  224. stm32mp_ddr_enable_host_interface(ctl);
  225. host_interface_reenable_request = false;
  226. }
  227. if (axi_port_reenable_request) {
  228. stm32mp_ddr_enable_axi_port(ctl);
  229. axi_port_reenable_request = false;
  230. }
  231. }
  232. void stm32mp_ddr_wait_refresh_update_done_ack(struct stm32mp_ddrctl *ctl)
  233. {
  234. uint64_t timeout;
  235. uint32_t rfshctl3;
  236. uint32_t refresh_update_level = DDRCTRL_RFSHCTL3_REFRESH_UPDATE_LEVEL;
  237. /* Toggle rfshctl3.refresh_update_level */
  238. rfshctl3 = mmio_read_32((uintptr_t)&ctl->rfshctl3);
  239. if ((rfshctl3 & refresh_update_level) == refresh_update_level) {
  240. mmio_setbits_32((uintptr_t)&ctl->rfshctl3, refresh_update_level);
  241. } else {
  242. mmio_clrbits_32((uintptr_t)&ctl->rfshctl3, refresh_update_level);
  243. refresh_update_level = 0U;
  244. }
  245. VERBOSE("[0x%lx] rfshctl3 = 0x%x\n",
  246. (uintptr_t)&ctl->rfshctl3, mmio_read_32((uintptr_t)&ctl->rfshctl3));
  247. timeout = timeout_init_us(DDR_TIMEOUT_US_1S);
  248. do {
  249. rfshctl3 = mmio_read_32((uintptr_t)&ctl->rfshctl3);
  250. VERBOSE("[0x%lx] rfshctl3 = 0x%x ", (uintptr_t)&ctl->rfshctl3, rfshctl3);
  251. if (timeout_elapsed(timeout)) {
  252. panic();
  253. }
  254. } while ((rfshctl3 & DDRCTRL_RFSHCTL3_REFRESH_UPDATE_LEVEL) != refresh_update_level);
  255. VERBOSE("[0x%lx] rfshctl3 = 0x%x\n", (uintptr_t)&ctl->rfshctl3, rfshctl3);
  256. }