pmu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2016-2024, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <drivers/delay_timer.h>
  12. #include <lib/mmio.h>
  13. #include <plat/common/platform.h>
  14. #include <plat_private.h>
  15. #include <pmu.h>
  16. #include <pmu_com.h>
  17. #include <rk3288_def.h>
  18. #include <secure.h>
  19. #include <soc.h>
  20. DEFINE_BAKERY_LOCK(rockchip_pd_lock);
  21. static uint32_t cpu_warm_boot_addr;
  22. static uint32_t store_pmu_pwrmode_con;
  23. static uint32_t store_sgrf_soc_con0;
  24. static uint32_t store_sgrf_cpu_con0;
  25. /* These enum are variants of low power mode */
  26. enum {
  27. ROCKCHIP_ARM_OFF_LOGIC_NORMAL = 0,
  28. ROCKCHIP_ARM_OFF_LOGIC_DEEP = 1,
  29. };
  30. static bool rk3288_sleep_disable_osc(void)
  31. {
  32. static const uint32_t reg_offset[] = { GRF_UOC0_CON0, GRF_UOC1_CON0,
  33. GRF_UOC2_CON0 };
  34. uint32_t reg, i;
  35. /*
  36. * if any usb phy is still on(GRF_SIDDQ==0), that means we need the
  37. * function of usb wakeup, so do not switch to 32khz, since the usb phy
  38. * clk does not connect to 32khz osc
  39. */
  40. for (i = 0; i < ARRAY_SIZE(reg_offset); i++) {
  41. reg = mmio_read_32(GRF_BASE + reg_offset[i]);
  42. if (!(reg & GRF_SIDDQ))
  43. return false;
  44. }
  45. return true;
  46. }
  47. static void pmu_set_sleep_mode(int level)
  48. {
  49. uint32_t mode_set, mode_set1;
  50. bool osc_disable = rk3288_sleep_disable_osc();
  51. mode_set = BIT(pmu_mode_glb_int_dis) | BIT(pmu_mode_l2_flush_en) |
  52. BIT(pmu_mode_sref0_enter) | BIT(pmu_mode_sref1_enter) |
  53. BIT(pmu_mode_ddrc0_gt) | BIT(pmu_mode_ddrc1_gt) |
  54. BIT(pmu_mode_en) | BIT(pmu_mode_chip_pd) |
  55. BIT(pmu_mode_scu_pd);
  56. mode_set1 = BIT(pmu_mode_clr_core) | BIT(pmu_mode_clr_cpup);
  57. if (level == ROCKCHIP_ARM_OFF_LOGIC_DEEP) {
  58. /* arm off, logic deep sleep */
  59. mode_set |= BIT(pmu_mode_bus_pd) | BIT(pmu_mode_pmu_use_lf) |
  60. BIT(pmu_mode_ddrio1_ret) |
  61. BIT(pmu_mode_ddrio0_ret) |
  62. BIT(pmu_mode_pmu_alive_use_lf) |
  63. BIT(pmu_mode_pll_pd);
  64. if (osc_disable)
  65. mode_set |= BIT(pmu_mode_osc_dis);
  66. mode_set1 |= BIT(pmu_mode_clr_alive) | BIT(pmu_mode_clr_bus) |
  67. BIT(pmu_mode_clr_peri) | BIT(pmu_mode_clr_dma);
  68. mmio_write_32(PMU_BASE + PMU_WAKEUP_CFG1,
  69. pmu_armint_wakeup_en);
  70. /*
  71. * In deep suspend we use PMU_PMU_USE_LF to let the rk3288
  72. * switch its main clock supply to the alternative 32kHz
  73. * source. Therefore set 30ms on a 32kHz clock for pmic
  74. * stabilization. Similar 30ms on 24MHz for the other
  75. * mode below.
  76. */
  77. mmio_write_32(PMU_BASE + PMU_STABL_CNT, 32 * 30);
  78. /* only wait for stabilization, if we turned the osc off */
  79. mmio_write_32(PMU_BASE + PMU_OSC_CNT,
  80. osc_disable ? 32 * 30 : 0);
  81. } else {
  82. /*
  83. * arm off, logic normal
  84. * if pmu_clk_core_src_gate_en is not set,
  85. * wakeup will be error
  86. */
  87. mode_set |= BIT(pmu_mode_core_src_gt);
  88. mmio_write_32(PMU_BASE + PMU_WAKEUP_CFG1,
  89. BIT(pmu_armint_wakeup_en) |
  90. BIT(pmu_gpioint_wakeup_en));
  91. /* 30ms on a 24MHz clock for pmic stabilization */
  92. mmio_write_32(PMU_BASE + PMU_STABL_CNT, 24000 * 30);
  93. /* oscillator is still running, so no need to wait */
  94. mmio_write_32(PMU_BASE + PMU_OSC_CNT, 0);
  95. }
  96. mmio_write_32(PMU_BASE + PMU_PWRMODE_CON, mode_set);
  97. mmio_write_32(PMU_BASE + PMU_PWRMODE_CON1, mode_set1);
  98. }
  99. static int cpus_power_domain_on(uint32_t cpu_id)
  100. {
  101. uint32_t cpu_pd;
  102. cpu_pd = PD_CPU0 + cpu_id;
  103. /* if the core has been on, power it off first */
  104. if (pmu_power_domain_st(cpu_pd) == pmu_pd_on) {
  105. /* put core in reset - some sort of A12/A17 bug */
  106. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(0),
  107. BIT(cpu_id) | (BIT(cpu_id) << 16));
  108. pmu_power_domain_ctr(cpu_pd, pmu_pd_off);
  109. }
  110. pmu_power_domain_ctr(cpu_pd, pmu_pd_on);
  111. /* pull core out of reset */
  112. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(0), BIT(cpu_id) << 16);
  113. return 0;
  114. }
  115. static int cpus_power_domain_off(uint32_t cpu_id)
  116. {
  117. uint32_t cpu_pd = PD_CPU0 + cpu_id;
  118. if (pmu_power_domain_st(cpu_pd) == pmu_pd_off)
  119. return 0;
  120. if (check_cpu_wfie(cpu_id, CKECK_WFEI_MSK))
  121. return -EINVAL;
  122. /* put core in reset - some sort of A12/A17 bug */
  123. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(0),
  124. BIT(cpu_id) | (BIT(cpu_id) << 16));
  125. pmu_power_domain_ctr(cpu_pd, pmu_pd_off);
  126. return 0;
  127. }
  128. static void nonboot_cpus_off(void)
  129. {
  130. uint32_t boot_cpu, cpu;
  131. boot_cpu = plat_my_core_pos();
  132. boot_cpu = MPIDR_AFFLVL0_VAL(read_mpidr());
  133. /* turn off noboot cpus */
  134. for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++) {
  135. if (cpu == boot_cpu)
  136. continue;
  137. cpus_power_domain_off(cpu);
  138. }
  139. }
  140. void sram_save(void)
  141. {
  142. /* TODO: support the sdram save for rk3288 SoCs*/
  143. }
  144. void sram_restore(void)
  145. {
  146. /* TODO: support the sdram restore for rk3288 SoCs */
  147. }
  148. int rockchip_soc_cores_pwr_dm_on(unsigned long mpidr, uint64_t entrypoint)
  149. {
  150. uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
  151. assert(cpu_id < PLATFORM_CORE_COUNT);
  152. assert(cpuson_flags[cpu_id] == 0);
  153. cpuson_flags[cpu_id] = PMU_CPU_HOTPLUG;
  154. cpuson_entry_point[cpu_id] = entrypoint;
  155. dsb();
  156. cpus_power_domain_on(cpu_id);
  157. /*
  158. * We communicate with the bootrom to active the cpus other
  159. * than cpu0, after a blob of initialize code, they will
  160. * stay at wfe state, once they are activated, they will check
  161. * the mailbox:
  162. * sram_base_addr + 4: 0xdeadbeaf
  163. * sram_base_addr + 8: start address for pc
  164. * The cpu0 need to wait the other cpus other than cpu0 entering
  165. * the wfe state.The wait time is affected by many aspects.
  166. * (e.g: cpu frequency, bootrom frequency, sram frequency, ...)
  167. */
  168. mdelay(1); /* ensure the cpus other than cpu0 to startup */
  169. /* tell the bootrom mailbox where to start from */
  170. mmio_write_32(SRAM_BASE + 8, cpu_warm_boot_addr);
  171. mmio_write_32(SRAM_BASE + 4, 0xDEADBEAF);
  172. dsb();
  173. sev();
  174. return 0;
  175. }
  176. int rockchip_soc_cores_pwr_dm_on_finish(void)
  177. {
  178. return 0;
  179. }
  180. int rockchip_soc_sys_pwr_dm_resume(void)
  181. {
  182. mmio_write_32(PMU_BASE + PMU_PWRMODE_CON, store_pmu_pwrmode_con);
  183. mmio_write_32(SGRF_BASE + SGRF_CPU_CON(0),
  184. store_sgrf_cpu_con0 | SGRF_DAPDEVICE_MSK);
  185. /* disable fastboot mode */
  186. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(0),
  187. store_sgrf_soc_con0 | SGRF_FAST_BOOT_DIS);
  188. secure_watchdog_ungate();
  189. clk_gate_con_restore();
  190. clk_sel_con_restore();
  191. clk_plls_resume();
  192. secure_gic_init();
  193. plat_rockchip_gic_init();
  194. return 0;
  195. }
  196. int rockchip_soc_sys_pwr_dm_suspend(void)
  197. {
  198. nonboot_cpus_off();
  199. store_sgrf_cpu_con0 = mmio_read_32(SGRF_BASE + SGRF_CPU_CON(0));
  200. store_sgrf_soc_con0 = mmio_read_32(SGRF_BASE + SGRF_SOC_CON(0));
  201. store_pmu_pwrmode_con = mmio_read_32(PMU_BASE + PMU_PWRMODE_CON);
  202. /* save clk-gates and ungate all for suspend */
  203. clk_gate_con_save();
  204. clk_gate_con_disable();
  205. clk_sel_con_save();
  206. pmu_set_sleep_mode(ROCKCHIP_ARM_OFF_LOGIC_NORMAL);
  207. clk_plls_suspend();
  208. secure_watchdog_gate();
  209. /*
  210. * The dapswjdp can not auto reset before resume, that cause it may
  211. * access some illegal address during resume. Let's disable it before
  212. * suspend, and the MASKROM will enable it back.
  213. */
  214. mmio_write_32(SGRF_BASE + SGRF_CPU_CON(0), SGRF_DAPDEVICE_MSK);
  215. /*
  216. * SGRF_FAST_BOOT_EN - system to boot from FAST_BOOT_ADDR
  217. */
  218. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(0), SGRF_FAST_BOOT_ENA);
  219. /* boot-address of resuming system is from this register value */
  220. mmio_write_32(SGRF_BASE + SGRF_FAST_BOOT_ADDR,
  221. (uint32_t)&pmu_cpuson_entrypoint);
  222. /* flush all caches - otherwise we might loose the resume address */
  223. dcsw_op_all(DC_OP_CISW);
  224. return 0;
  225. }
  226. void rockchip_plat_mmu_svc_mon(void)
  227. {
  228. }
  229. void plat_rockchip_pmu_init(void)
  230. {
  231. uint32_t cpu;
  232. cpu_warm_boot_addr = (uint32_t)platform_cpu_warmboot;
  233. /* on boot all power-domains are on */
  234. for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++)
  235. cpuson_flags[cpu] = pmu_pd_on;
  236. nonboot_cpus_off();
  237. }