rpi3_pm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <platform_def.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/console.h>
  11. #include <lib/mmio.h>
  12. #include <lib/psci/psci.h>
  13. #include <plat/common/platform.h>
  14. #include <rpi_hw.h>
  15. #ifdef RPI_HAVE_GIC
  16. #include <drivers/arm/gicv2.h>
  17. #endif
  18. /* Registers on top of RPI3_PM_BASE. */
  19. #define RPI3_PM_RSTC_OFFSET ULL(0x0000001C)
  20. #define RPI3_PM_RSTS_OFFSET ULL(0x00000020)
  21. #define RPI3_PM_WDOG_OFFSET ULL(0x00000024)
  22. /* Watchdog constants */
  23. #define RPI3_PM_PASSWORD U(0x5A000000)
  24. #define RPI3_PM_RSTC_WRCFG_MASK U(0x00000030)
  25. #define RPI3_PM_RSTC_WRCFG_FULL_RESET U(0x00000020)
  26. /*
  27. * The RSTS register is used by the VideoCore firmware when booting the
  28. * Raspberry Pi to know which partition to boot from. The partition value is
  29. * formed by bits 0, 2, 4, 6, 8 and 10. Partition 63 is used by said firmware
  30. * to indicate halt.
  31. */
  32. #define RPI3_PM_RSTS_WRCFG_HALT U(0x00000555)
  33. /* Make composite power state parameter till power level 0 */
  34. #if PSCI_EXTENDED_STATE_ID
  35. #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
  36. (((lvl0_state) << PSTATE_ID_SHIFT) | \
  37. ((type) << PSTATE_TYPE_SHIFT))
  38. #else
  39. #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
  40. (((lvl0_state) << PSTATE_ID_SHIFT) | \
  41. ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
  42. ((type) << PSTATE_TYPE_SHIFT))
  43. #endif /* PSCI_EXTENDED_STATE_ID */
  44. #define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
  45. (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
  46. rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
  47. /*
  48. * The table storing the valid idle power states. Ensure that the
  49. * array entries are populated in ascending order of state-id to
  50. * enable us to use binary search during power state validation.
  51. * The table must be terminated by a NULL entry.
  52. */
  53. static const unsigned int rpi3_pm_idle_states[] = {
  54. /* State-id - 0x01 */
  55. rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
  56. MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
  57. /* State-id - 0x02 */
  58. rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
  59. MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
  60. /* State-id - 0x22 */
  61. rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF,
  62. MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
  63. 0,
  64. };
  65. /*******************************************************************************
  66. * Platform handler called to check the validity of the power state
  67. * parameter. The power state parameter has to be a composite power state.
  68. ******************************************************************************/
  69. static int rpi3_validate_power_state(unsigned int power_state,
  70. psci_power_state_t *req_state)
  71. {
  72. unsigned int state_id;
  73. int i;
  74. assert(req_state != 0);
  75. /*
  76. * Currently we are using a linear search for finding the matching
  77. * entry in the idle power state array. This can be made a binary
  78. * search if the number of entries justify the additional complexity.
  79. */
  80. for (i = 0; rpi3_pm_idle_states[i] != 0; i++) {
  81. if (power_state == rpi3_pm_idle_states[i]) {
  82. break;
  83. }
  84. }
  85. /* Return error if entry not found in the idle state array */
  86. if (!rpi3_pm_idle_states[i]) {
  87. return PSCI_E_INVALID_PARAMS;
  88. }
  89. i = 0;
  90. state_id = psci_get_pstate_id(power_state);
  91. /* Parse the State ID and populate the state info parameter */
  92. while (state_id) {
  93. req_state->pwr_domain_state[i++] = state_id &
  94. PLAT_LOCAL_PSTATE_MASK;
  95. state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
  96. }
  97. return PSCI_E_SUCCESS;
  98. }
  99. /*******************************************************************************
  100. * Platform handler called when a CPU is about to enter standby.
  101. ******************************************************************************/
  102. static void rpi3_cpu_standby(plat_local_state_t cpu_state)
  103. {
  104. assert(cpu_state == PLAT_LOCAL_STATE_RET);
  105. /*
  106. * Enter standby state.
  107. * dsb is good practice before using wfi to enter low power states
  108. */
  109. dsb();
  110. wfi();
  111. }
  112. static void rpi3_pwr_domain_off(const psci_power_state_t *target_state)
  113. {
  114. #ifdef RPI_HAVE_GIC
  115. gicv2_cpuif_disable();
  116. #endif
  117. }
  118. /*******************************************************************************
  119. * Platform handler called when a power domain is about to be turned on. The
  120. * mpidr determines the CPU to be turned on.
  121. ******************************************************************************/
  122. static int rpi3_pwr_domain_on(u_register_t mpidr)
  123. {
  124. int rc = PSCI_E_SUCCESS;
  125. unsigned int pos = plat_core_pos_by_mpidr(mpidr);
  126. uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
  127. assert(pos < PLATFORM_CORE_COUNT);
  128. hold_base += pos * PLAT_RPI3_TM_HOLD_ENTRY_SIZE;
  129. mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_GO);
  130. /* No cache maintenance here, hold_base is mapped as device memory. */
  131. /* Make sure that the write has completed */
  132. dsb();
  133. isb();
  134. sev();
  135. return rc;
  136. }
  137. /*******************************************************************************
  138. * Platform handler called when a power domain has just been powered on after
  139. * being turned off earlier. The target_state encodes the low power state that
  140. * each level has woken up from.
  141. ******************************************************************************/
  142. static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state)
  143. {
  144. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  145. PLAT_LOCAL_STATE_OFF);
  146. #ifdef RPI_HAVE_GIC
  147. gicv2_pcpu_distif_init();
  148. gicv2_cpuif_enable();
  149. #endif
  150. }
  151. static void __dead2 rpi3_pwr_down_wfi(
  152. const psci_power_state_t *target_state)
  153. {
  154. uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
  155. unsigned int pos = plat_my_core_pos();
  156. if (pos == 0) {
  157. /*
  158. * The secondaries will always be in a wait
  159. * for warm boot on reset, but the BSP needs
  160. * to be able to distinguish between waiting
  161. * for warm boot (e.g. after psci_off, waiting
  162. * for psci_on) and a cold boot.
  163. */
  164. mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_BSP_OFF);
  165. /* No cache maintenance here, we run with caches off already. */
  166. dsb();
  167. isb();
  168. }
  169. write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
  170. while (1) {
  171. wfi();
  172. }
  173. }
  174. /*******************************************************************************
  175. * Platform handlers for system reset and system off.
  176. ******************************************************************************/
  177. /* 10 ticks (Watchdog timer = Timer clock / 16) */
  178. #define RESET_TIMEOUT U(10)
  179. static void __dead2 rpi3_watchdog_reset(void)
  180. {
  181. uint32_t rstc;
  182. console_flush();
  183. dsbsy();
  184. isb();
  185. mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET,
  186. RPI3_PM_PASSWORD | RESET_TIMEOUT);
  187. rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET);
  188. rstc &= ~RPI3_PM_RSTC_WRCFG_MASK;
  189. rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET;
  190. mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc);
  191. for (;;) {
  192. wfi();
  193. }
  194. }
  195. static void __dead2 rpi3_system_reset(void)
  196. {
  197. INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n");
  198. rpi3_watchdog_reset();
  199. }
  200. static void __dead2 rpi3_system_off(void)
  201. {
  202. uint32_t rsts;
  203. INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n");
  204. /*
  205. * This function doesn't actually make the Raspberry Pi turn itself off,
  206. * the hardware doesn't allow it. It simply reboots it and the RSTS
  207. * value tells the bootcode.bin firmware not to continue the regular
  208. * bootflow and to stay in a low power mode.
  209. */
  210. rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET);
  211. rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT;
  212. mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts);
  213. rpi3_watchdog_reset();
  214. }
  215. /*******************************************************************************
  216. * Platform handlers and setup function.
  217. ******************************************************************************/
  218. static const plat_psci_ops_t plat_rpi3_psci_pm_ops = {
  219. .cpu_standby = rpi3_cpu_standby,
  220. .pwr_domain_off = rpi3_pwr_domain_off,
  221. .pwr_domain_on = rpi3_pwr_domain_on,
  222. .pwr_domain_on_finish = rpi3_pwr_domain_on_finish,
  223. .pwr_domain_pwr_down_wfi = rpi3_pwr_down_wfi,
  224. .system_off = rpi3_system_off,
  225. .system_reset = rpi3_system_reset,
  226. .validate_power_state = rpi3_validate_power_state,
  227. };
  228. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  229. const plat_psci_ops_t **psci_ops)
  230. {
  231. uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT;
  232. *entrypoint = sec_entrypoint;
  233. *psci_ops = &plat_rpi3_psci_pm_ops;
  234. return 0;
  235. }