stm32mp1_pm.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2015-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 <arch_helpers.h>
  9. #include <bl32/sp_min/platform_sp_min.h>
  10. #include <common/debug.h>
  11. #include <drivers/arm/gic_common.h>
  12. #include <drivers/arm/gicv2.h>
  13. #include <drivers/clk.h>
  14. #include <drivers/st/stm32mp_reset.h>
  15. #include <dt-bindings/clock/stm32mp1-clks.h>
  16. #include <lib/mmio.h>
  17. #include <lib/psci/psci.h>
  18. #include <plat/common/platform.h>
  19. #include <platform_def.h>
  20. static uintptr_t stm32_sec_entrypoint;
  21. static uint32_t cntfrq_core0;
  22. /*******************************************************************************
  23. * STM32MP1 handler called when a CPU is about to enter standby.
  24. * call by core 1 to enter in wfi
  25. ******************************************************************************/
  26. static void stm32_cpu_standby(plat_local_state_t cpu_state)
  27. {
  28. uint32_t interrupt = GIC_SPURIOUS_INTERRUPT;
  29. assert(cpu_state == ARM_LOCAL_STATE_RET);
  30. /*
  31. * Enter standby state
  32. * dsb is good practice before using wfi to enter low power states
  33. */
  34. isb();
  35. dsb();
  36. while (interrupt == GIC_SPURIOUS_INTERRUPT) {
  37. wfi();
  38. /* Acknowledge IT */
  39. interrupt = gicv2_acknowledge_interrupt();
  40. /* If Interrupt == 1022 it will be acknowledged by non secure */
  41. if ((interrupt != PENDING_G1_INTID) &&
  42. (interrupt != GIC_SPURIOUS_INTERRUPT)) {
  43. gicv2_end_of_interrupt(interrupt);
  44. }
  45. }
  46. }
  47. /*******************************************************************************
  48. * STM32MP1 handler called when a power domain is about to be turned on. The
  49. * mpidr determines the CPU to be turned on.
  50. * call by core 0 to activate core 1
  51. ******************************************************************************/
  52. static int stm32_pwr_domain_on(u_register_t mpidr)
  53. {
  54. unsigned long current_cpu_mpidr = read_mpidr_el1();
  55. uintptr_t bkpr_core1_addr =
  56. tamp_bkpr(BOOT_API_CORE1_BRANCH_ADDRESS_TAMP_BCK_REG_IDX);
  57. uintptr_t bkpr_core1_magic =
  58. tamp_bkpr(BOOT_API_CORE1_MAGIC_NUMBER_TAMP_BCK_REG_IDX);
  59. if (mpidr == current_cpu_mpidr) {
  60. return PSCI_E_INVALID_PARAMS;
  61. }
  62. /* Only one valid entry point */
  63. if (stm32_sec_entrypoint != (uintptr_t)&sp_min_warm_entrypoint) {
  64. return PSCI_E_INVALID_ADDRESS;
  65. }
  66. clk_enable(RTCAPB);
  67. cntfrq_core0 = read_cntfrq_el0();
  68. /* Write entrypoint in backup RAM register */
  69. mmio_write_32(bkpr_core1_addr, stm32_sec_entrypoint);
  70. /* Write magic number in backup register */
  71. mmio_write_32(bkpr_core1_magic, BOOT_API_A7_CORE1_MAGIC_NUMBER);
  72. clk_disable(RTCAPB);
  73. /* Generate an IT to core 1 */
  74. gicv2_raise_sgi(ARM_IRQ_SEC_SGI_0, false, STM32MP_SECONDARY_CPU);
  75. return PSCI_E_SUCCESS;
  76. }
  77. /*******************************************************************************
  78. * STM32MP1 handler called when a power domain is about to be turned off. The
  79. * target_state encodes the power state that each level should transition to.
  80. ******************************************************************************/
  81. static void stm32_pwr_domain_off(const psci_power_state_t *target_state)
  82. {
  83. /* Nothing to do */
  84. }
  85. /*******************************************************************************
  86. * STM32MP1 handler called when a power domain is about to be suspended. The
  87. * target_state encodes the power state that each level should transition to.
  88. ******************************************************************************/
  89. static void stm32_pwr_domain_suspend(const psci_power_state_t *target_state)
  90. {
  91. /* Nothing to do, power domain is not disabled */
  92. }
  93. /*******************************************************************************
  94. * STM32MP1 handler called when a power domain has just been powered on after
  95. * being turned off earlier. The target_state encodes the low power state that
  96. * each level has woken up from.
  97. * call by core 1 just after wake up
  98. ******************************************************************************/
  99. static void stm32_pwr_domain_on_finish(const psci_power_state_t *target_state)
  100. {
  101. stm32mp_gic_pcpu_init();
  102. write_cntfrq_el0(cntfrq_core0);
  103. }
  104. /*******************************************************************************
  105. * STM32MP1 handler called when a power domain has just been powered on after
  106. * having been suspended earlier. The target_state encodes the low power state
  107. * that each level has woken up from.
  108. ******************************************************************************/
  109. static void stm32_pwr_domain_suspend_finish(const psci_power_state_t
  110. *target_state)
  111. {
  112. /* Nothing to do, power domain is not disabled */
  113. }
  114. static void __dead2 stm32_pwr_domain_pwr_down_wfi(const psci_power_state_t
  115. *target_state)
  116. {
  117. ERROR("stm32mpu1 Power Down WFI: operation not handled.\n");
  118. panic();
  119. }
  120. static void __dead2 stm32_system_off(void)
  121. {
  122. ERROR("stm32mpu1 System Off: operation not handled.\n");
  123. panic();
  124. }
  125. static void __dead2 stm32_system_reset(void)
  126. {
  127. stm32mp_system_reset();
  128. }
  129. static int stm32_validate_power_state(unsigned int power_state,
  130. psci_power_state_t *req_state)
  131. {
  132. if (psci_get_pstate_type(power_state) != 0U) {
  133. return PSCI_E_INVALID_PARAMS;
  134. }
  135. if (psci_get_pstate_pwrlvl(power_state) != 0U) {
  136. return PSCI_E_INVALID_PARAMS;
  137. }
  138. if (psci_get_pstate_id(power_state) != 0U) {
  139. return PSCI_E_INVALID_PARAMS;
  140. }
  141. req_state->pwr_domain_state[0] = ARM_LOCAL_STATE_RET;
  142. req_state->pwr_domain_state[1] = ARM_LOCAL_STATE_RUN;
  143. return PSCI_E_SUCCESS;
  144. }
  145. static int stm32_validate_ns_entrypoint(uintptr_t entrypoint)
  146. {
  147. /* The non-secure entry point must be in DDR */
  148. if (entrypoint < STM32MP_DDR_BASE) {
  149. return PSCI_E_INVALID_ADDRESS;
  150. }
  151. return PSCI_E_SUCCESS;
  152. }
  153. static int stm32_node_hw_state(u_register_t target_cpu,
  154. unsigned int power_level)
  155. {
  156. /*
  157. * The format of 'power_level' is implementation-defined, but 0 must
  158. * mean a CPU. Only allow level 0.
  159. */
  160. if (power_level != MPIDR_AFFLVL0) {
  161. return PSCI_E_INVALID_PARAMS;
  162. }
  163. /*
  164. * From psci view the CPU 0 is always ON,
  165. * CPU 1 can be SUSPEND or RUNNING.
  166. * Therefore do not manage POWER OFF state and always return HW_ON.
  167. */
  168. return (int)HW_ON;
  169. }
  170. /*******************************************************************************
  171. * Export the platform handlers. The ARM Standard platform layer will take care
  172. * of registering the handlers with PSCI.
  173. ******************************************************************************/
  174. static const plat_psci_ops_t stm32_psci_ops = {
  175. .cpu_standby = stm32_cpu_standby,
  176. .pwr_domain_on = stm32_pwr_domain_on,
  177. .pwr_domain_off = stm32_pwr_domain_off,
  178. .pwr_domain_suspend = stm32_pwr_domain_suspend,
  179. .pwr_domain_on_finish = stm32_pwr_domain_on_finish,
  180. .pwr_domain_suspend_finish = stm32_pwr_domain_suspend_finish,
  181. .pwr_domain_pwr_down_wfi = stm32_pwr_domain_pwr_down_wfi,
  182. .system_off = stm32_system_off,
  183. .system_reset = stm32_system_reset,
  184. .validate_power_state = stm32_validate_power_state,
  185. .validate_ns_entrypoint = stm32_validate_ns_entrypoint,
  186. .get_node_hw_state = stm32_node_hw_state
  187. };
  188. /*******************************************************************************
  189. * Export the platform specific power ops.
  190. ******************************************************************************/
  191. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  192. const plat_psci_ops_t **psci_ops)
  193. {
  194. stm32_sec_entrypoint = sec_entrypoint;
  195. *psci_ops = &stm32_psci_ops;
  196. return 0;
  197. }