pmu.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2016, 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 inline int rk3288_pmu_bus_idle(uint32_t req, uint32_t idle)
  31. {
  32. uint32_t mask = BIT(req);
  33. uint32_t idle_mask = 0;
  34. uint32_t idle_target = 0;
  35. uint32_t val;
  36. uint32_t wait_cnt = 0;
  37. switch (req) {
  38. case bus_ide_req_gpu:
  39. idle_mask = BIT(pmu_idle_ack_gpu) | BIT(pmu_idle_gpu);
  40. idle_target = (idle << pmu_idle_ack_gpu) |
  41. (idle << pmu_idle_gpu);
  42. break;
  43. case bus_ide_req_core:
  44. idle_mask = BIT(pmu_idle_ack_core) | BIT(pmu_idle_core);
  45. idle_target = (idle << pmu_idle_ack_core) |
  46. (idle << pmu_idle_core);
  47. break;
  48. case bus_ide_req_cpup:
  49. idle_mask = BIT(pmu_idle_ack_cpup) | BIT(pmu_idle_cpup);
  50. idle_target = (idle << pmu_idle_ack_cpup) |
  51. (idle << pmu_idle_cpup);
  52. break;
  53. case bus_ide_req_bus:
  54. idle_mask = BIT(pmu_idle_ack_bus) | BIT(pmu_idle_bus);
  55. idle_target = (idle << pmu_idle_ack_bus) |
  56. (idle << pmu_idle_bus);
  57. break;
  58. case bus_ide_req_dma:
  59. idle_mask = BIT(pmu_idle_ack_dma) | BIT(pmu_idle_dma);
  60. idle_target = (idle << pmu_idle_ack_dma) |
  61. (idle << pmu_idle_dma);
  62. break;
  63. case bus_ide_req_peri:
  64. idle_mask = BIT(pmu_idle_ack_peri) | BIT(pmu_idle_peri);
  65. idle_target = (idle << pmu_idle_ack_peri) |
  66. (idle << pmu_idle_peri);
  67. break;
  68. case bus_ide_req_video:
  69. idle_mask = BIT(pmu_idle_ack_video) | BIT(pmu_idle_video);
  70. idle_target = (idle << pmu_idle_ack_video) |
  71. (idle << pmu_idle_video);
  72. break;
  73. case bus_ide_req_hevc:
  74. idle_mask = BIT(pmu_idle_ack_hevc) | BIT(pmu_idle_hevc);
  75. idle_target = (idle << pmu_idle_ack_hevc) |
  76. (idle << pmu_idle_hevc);
  77. break;
  78. case bus_ide_req_vio:
  79. idle_mask = BIT(pmu_idle_ack_vio) | BIT(pmu_idle_vio);
  80. idle_target = (pmu_idle_ack_vio) |
  81. (idle << pmu_idle_vio);
  82. break;
  83. case bus_ide_req_alive:
  84. idle_mask = BIT(pmu_idle_ack_alive) | BIT(pmu_idle_alive);
  85. idle_target = (idle << pmu_idle_ack_alive) |
  86. (idle << pmu_idle_alive);
  87. break;
  88. default:
  89. ERROR("%s: Unsupported the idle request\n", __func__);
  90. break;
  91. }
  92. val = mmio_read_32(PMU_BASE + PMU_BUS_IDE_REQ);
  93. if (idle)
  94. val |= mask;
  95. else
  96. val &= ~mask;
  97. mmio_write_32(PMU_BASE + PMU_BUS_IDE_REQ, val);
  98. while ((mmio_read_32(PMU_BASE +
  99. PMU_BUS_IDE_ST) & idle_mask) != idle_target) {
  100. wait_cnt++;
  101. if (!(wait_cnt % MAX_WAIT_CONUT))
  102. WARN("%s:st=%x(%x)\n", __func__,
  103. mmio_read_32(PMU_BASE + PMU_BUS_IDE_ST),
  104. idle_mask);
  105. }
  106. return 0;
  107. }
  108. static bool rk3288_sleep_disable_osc(void)
  109. {
  110. static const uint32_t reg_offset[] = { GRF_UOC0_CON0, GRF_UOC1_CON0,
  111. GRF_UOC2_CON0 };
  112. uint32_t reg, i;
  113. /*
  114. * if any usb phy is still on(GRF_SIDDQ==0), that means we need the
  115. * function of usb wakeup, so do not switch to 32khz, since the usb phy
  116. * clk does not connect to 32khz osc
  117. */
  118. for (i = 0; i < ARRAY_SIZE(reg_offset); i++) {
  119. reg = mmio_read_32(GRF_BASE + reg_offset[i]);
  120. if (!(reg & GRF_SIDDQ))
  121. return false;
  122. }
  123. return true;
  124. }
  125. static void pmu_set_sleep_mode(int level)
  126. {
  127. uint32_t mode_set, mode_set1;
  128. bool osc_disable = rk3288_sleep_disable_osc();
  129. mode_set = BIT(pmu_mode_glb_int_dis) | BIT(pmu_mode_l2_flush_en) |
  130. BIT(pmu_mode_sref0_enter) | BIT(pmu_mode_sref1_enter) |
  131. BIT(pmu_mode_ddrc0_gt) | BIT(pmu_mode_ddrc1_gt) |
  132. BIT(pmu_mode_en) | BIT(pmu_mode_chip_pd) |
  133. BIT(pmu_mode_scu_pd);
  134. mode_set1 = BIT(pmu_mode_clr_core) | BIT(pmu_mode_clr_cpup);
  135. if (level == ROCKCHIP_ARM_OFF_LOGIC_DEEP) {
  136. /* arm off, logic deep sleep */
  137. mode_set |= BIT(pmu_mode_bus_pd) | BIT(pmu_mode_pmu_use_lf) |
  138. BIT(pmu_mode_ddrio1_ret) |
  139. BIT(pmu_mode_ddrio0_ret) |
  140. BIT(pmu_mode_pmu_alive_use_lf) |
  141. BIT(pmu_mode_pll_pd);
  142. if (osc_disable)
  143. mode_set |= BIT(pmu_mode_osc_dis);
  144. mode_set1 |= BIT(pmu_mode_clr_alive) | BIT(pmu_mode_clr_bus) |
  145. BIT(pmu_mode_clr_peri) | BIT(pmu_mode_clr_dma);
  146. mmio_write_32(PMU_BASE + PMU_WAKEUP_CFG1,
  147. pmu_armint_wakeup_en);
  148. /*
  149. * In deep suspend we use PMU_PMU_USE_LF to let the rk3288
  150. * switch its main clock supply to the alternative 32kHz
  151. * source. Therefore set 30ms on a 32kHz clock for pmic
  152. * stabilization. Similar 30ms on 24MHz for the other
  153. * mode below.
  154. */
  155. mmio_write_32(PMU_BASE + PMU_STABL_CNT, 32 * 30);
  156. /* only wait for stabilization, if we turned the osc off */
  157. mmio_write_32(PMU_BASE + PMU_OSC_CNT,
  158. osc_disable ? 32 * 30 : 0);
  159. } else {
  160. /*
  161. * arm off, logic normal
  162. * if pmu_clk_core_src_gate_en is not set,
  163. * wakeup will be error
  164. */
  165. mode_set |= BIT(pmu_mode_core_src_gt);
  166. mmio_write_32(PMU_BASE + PMU_WAKEUP_CFG1,
  167. BIT(pmu_armint_wakeup_en) |
  168. BIT(pmu_gpioint_wakeup_en));
  169. /* 30ms on a 24MHz clock for pmic stabilization */
  170. mmio_write_32(PMU_BASE + PMU_STABL_CNT, 24000 * 30);
  171. /* oscillator is still running, so no need to wait */
  172. mmio_write_32(PMU_BASE + PMU_OSC_CNT, 0);
  173. }
  174. mmio_write_32(PMU_BASE + PMU_PWRMODE_CON, mode_set);
  175. mmio_write_32(PMU_BASE + PMU_PWRMODE_CON1, mode_set1);
  176. }
  177. static int cpus_power_domain_on(uint32_t cpu_id)
  178. {
  179. uint32_t cpu_pd;
  180. cpu_pd = PD_CPU0 + cpu_id;
  181. /* if the core has been on, power it off first */
  182. if (pmu_power_domain_st(cpu_pd) == pmu_pd_on) {
  183. /* put core in reset - some sort of A12/A17 bug */
  184. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(0),
  185. BIT(cpu_id) | (BIT(cpu_id) << 16));
  186. pmu_power_domain_ctr(cpu_pd, pmu_pd_off);
  187. }
  188. pmu_power_domain_ctr(cpu_pd, pmu_pd_on);
  189. /* pull core out of reset */
  190. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(0), BIT(cpu_id) << 16);
  191. return 0;
  192. }
  193. static int cpus_power_domain_off(uint32_t cpu_id)
  194. {
  195. uint32_t cpu_pd = PD_CPU0 + cpu_id;
  196. if (pmu_power_domain_st(cpu_pd) == pmu_pd_off)
  197. return 0;
  198. if (check_cpu_wfie(cpu_id, CKECK_WFEI_MSK))
  199. return -EINVAL;
  200. /* put core in reset - some sort of A12/A17 bug */
  201. mmio_write_32(CRU_BASE + CRU_SOFTRSTS_CON(0),
  202. BIT(cpu_id) | (BIT(cpu_id) << 16));
  203. pmu_power_domain_ctr(cpu_pd, pmu_pd_off);
  204. return 0;
  205. }
  206. static void nonboot_cpus_off(void)
  207. {
  208. uint32_t boot_cpu, cpu;
  209. boot_cpu = plat_my_core_pos();
  210. boot_cpu = MPIDR_AFFLVL0_VAL(read_mpidr());
  211. /* turn off noboot cpus */
  212. for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++) {
  213. if (cpu == boot_cpu)
  214. continue;
  215. cpus_power_domain_off(cpu);
  216. }
  217. }
  218. void sram_save(void)
  219. {
  220. /* TODO: support the sdram save for rk3288 SoCs*/
  221. }
  222. void sram_restore(void)
  223. {
  224. /* TODO: support the sdram restore for rk3288 SoCs */
  225. }
  226. int rockchip_soc_cores_pwr_dm_on(unsigned long mpidr, uint64_t entrypoint)
  227. {
  228. uint32_t cpu_id = plat_core_pos_by_mpidr(mpidr);
  229. assert(cpu_id < PLATFORM_CORE_COUNT);
  230. assert(cpuson_flags[cpu_id] == 0);
  231. cpuson_flags[cpu_id] = PMU_CPU_HOTPLUG;
  232. cpuson_entry_point[cpu_id] = entrypoint;
  233. dsb();
  234. cpus_power_domain_on(cpu_id);
  235. /*
  236. * We communicate with the bootrom to active the cpus other
  237. * than cpu0, after a blob of initialize code, they will
  238. * stay at wfe state, once they are activated, they will check
  239. * the mailbox:
  240. * sram_base_addr + 4: 0xdeadbeaf
  241. * sram_base_addr + 8: start address for pc
  242. * The cpu0 need to wait the other cpus other than cpu0 entering
  243. * the wfe state.The wait time is affected by many aspects.
  244. * (e.g: cpu frequency, bootrom frequency, sram frequency, ...)
  245. */
  246. mdelay(1); /* ensure the cpus other than cpu0 to startup */
  247. /* tell the bootrom mailbox where to start from */
  248. mmio_write_32(SRAM_BASE + 8, cpu_warm_boot_addr);
  249. mmio_write_32(SRAM_BASE + 4, 0xDEADBEAF);
  250. dsb();
  251. sev();
  252. return 0;
  253. }
  254. int rockchip_soc_cores_pwr_dm_on_finish(void)
  255. {
  256. return 0;
  257. }
  258. int rockchip_soc_sys_pwr_dm_resume(void)
  259. {
  260. mmio_write_32(PMU_BASE + PMU_PWRMODE_CON, store_pmu_pwrmode_con);
  261. mmio_write_32(SGRF_BASE + SGRF_CPU_CON(0),
  262. store_sgrf_cpu_con0 | SGRF_DAPDEVICE_MSK);
  263. /* disable fastboot mode */
  264. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(0),
  265. store_sgrf_soc_con0 | SGRF_FAST_BOOT_DIS);
  266. secure_watchdog_ungate();
  267. clk_gate_con_restore();
  268. clk_sel_con_restore();
  269. clk_plls_resume();
  270. secure_gic_init();
  271. plat_rockchip_gic_init();
  272. return 0;
  273. }
  274. int rockchip_soc_sys_pwr_dm_suspend(void)
  275. {
  276. nonboot_cpus_off();
  277. store_sgrf_cpu_con0 = mmio_read_32(SGRF_BASE + SGRF_CPU_CON(0));
  278. store_sgrf_soc_con0 = mmio_read_32(SGRF_BASE + SGRF_SOC_CON(0));
  279. store_pmu_pwrmode_con = mmio_read_32(PMU_BASE + PMU_PWRMODE_CON);
  280. /* save clk-gates and ungate all for suspend */
  281. clk_gate_con_save();
  282. clk_gate_con_disable();
  283. clk_sel_con_save();
  284. pmu_set_sleep_mode(ROCKCHIP_ARM_OFF_LOGIC_NORMAL);
  285. clk_plls_suspend();
  286. secure_watchdog_gate();
  287. /*
  288. * The dapswjdp can not auto reset before resume, that cause it may
  289. * access some illegal address during resume. Let's disable it before
  290. * suspend, and the MASKROM will enable it back.
  291. */
  292. mmio_write_32(SGRF_BASE + SGRF_CPU_CON(0), SGRF_DAPDEVICE_MSK);
  293. /*
  294. * SGRF_FAST_BOOT_EN - system to boot from FAST_BOOT_ADDR
  295. */
  296. mmio_write_32(SGRF_BASE + SGRF_SOC_CON(0), SGRF_FAST_BOOT_ENA);
  297. /* boot-address of resuming system is from this register value */
  298. mmio_write_32(SGRF_BASE + SGRF_FAST_BOOT_ADDR,
  299. (uint32_t)&pmu_cpuson_entrypoint);
  300. /* flush all caches - otherwise we might loose the resume address */
  301. dcsw_op_all(DC_OP_CISW);
  302. return 0;
  303. }
  304. void rockchip_plat_mmu_svc_mon(void)
  305. {
  306. }
  307. void plat_rockchip_pmu_init(void)
  308. {
  309. uint32_t cpu;
  310. cpu_warm_boot_addr = (uint32_t)platform_cpu_warmboot;
  311. /* on boot all power-domains are on */
  312. for (cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu++)
  313. cpuson_flags[cpu] = pmu_pd_on;
  314. nonboot_cpus_off();
  315. }