sunxi_scpi_pm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2017-2021, 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/arm/css/css_scpi.h>
  11. #include <drivers/arm/gicv2.h>
  12. #include <lib/mmio.h>
  13. #include <lib/psci/psci.h>
  14. #include <sunxi_mmap.h>
  15. #include <sunxi_private.h>
  16. /*
  17. * The addresses for the SCP exception vectors are defined in the or1k
  18. * architecture specification.
  19. */
  20. #define OR1K_VEC_FIRST 0x01
  21. #define OR1K_VEC_LAST 0x0e
  22. #define OR1K_VEC_ADDR(n) (0x100 * (n))
  23. /*
  24. * This magic value is the little-endian representation of the or1k
  25. * instruction "l.mfspr r2, r0, 0x12", which is guaranteed to be the
  26. * first instruction in the SCP firmware.
  27. */
  28. #define SCP_FIRMWARE_MAGIC 0xb4400012
  29. #define PLAT_LOCAL_PSTATE_WIDTH U(4)
  30. #define PLAT_LOCAL_PSTATE_MASK ((U(1) << PLAT_LOCAL_PSTATE_WIDTH) - 1)
  31. #define CPU_PWR_LVL MPIDR_AFFLVL0
  32. #define CLUSTER_PWR_LVL MPIDR_AFFLVL1
  33. #define SYSTEM_PWR_LVL MPIDR_AFFLVL2
  34. #define CPU_PWR_STATE(state) \
  35. ((state)->pwr_domain_state[CPU_PWR_LVL])
  36. #define CLUSTER_PWR_STATE(state) \
  37. ((state)->pwr_domain_state[CLUSTER_PWR_LVL])
  38. #define SYSTEM_PWR_STATE(state) \
  39. ((state)->pwr_domain_state[SYSTEM_PWR_LVL])
  40. static void sunxi_cpu_standby(plat_local_state_t cpu_state)
  41. {
  42. u_register_t scr = read_scr_el3();
  43. assert(is_local_state_retn(cpu_state));
  44. write_scr_el3(scr | SCR_IRQ_BIT);
  45. wfi();
  46. write_scr_el3(scr);
  47. }
  48. static int sunxi_pwr_domain_on(u_register_t mpidr)
  49. {
  50. scpi_set_css_power_state(mpidr,
  51. scpi_power_on,
  52. scpi_power_on,
  53. scpi_power_on);
  54. return PSCI_E_SUCCESS;
  55. }
  56. static void sunxi_pwr_domain_off(const psci_power_state_t *target_state)
  57. {
  58. plat_local_state_t cpu_pwr_state = CPU_PWR_STATE(target_state);
  59. plat_local_state_t cluster_pwr_state = CLUSTER_PWR_STATE(target_state);
  60. plat_local_state_t system_pwr_state = SYSTEM_PWR_STATE(target_state);
  61. if (is_local_state_off(cpu_pwr_state)) {
  62. gicv2_cpuif_disable();
  63. }
  64. scpi_set_css_power_state(read_mpidr(),
  65. cpu_pwr_state,
  66. cluster_pwr_state,
  67. system_pwr_state);
  68. }
  69. static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state)
  70. {
  71. if (is_local_state_off(SYSTEM_PWR_STATE(target_state))) {
  72. gicv2_distif_init();
  73. }
  74. if (is_local_state_off(CPU_PWR_STATE(target_state))) {
  75. gicv2_pcpu_distif_init();
  76. gicv2_cpuif_enable();
  77. }
  78. }
  79. static void __dead2 sunxi_system_off(void)
  80. {
  81. uint32_t ret;
  82. gicv2_cpuif_disable();
  83. /* Send the power down request to the SCP. */
  84. ret = scpi_sys_power_state(scpi_system_shutdown);
  85. if (ret != SCP_OK) {
  86. ERROR("PSCI: SCPI %s failed: %d\n", "shutdown", ret);
  87. }
  88. psci_power_down_wfi();
  89. }
  90. static void __dead2 sunxi_system_reset(void)
  91. {
  92. uint32_t ret;
  93. gicv2_cpuif_disable();
  94. /* Send the system reset request to the SCP. */
  95. ret = scpi_sys_power_state(scpi_system_reboot);
  96. if (ret != SCP_OK) {
  97. ERROR("PSCI: SCPI %s failed: %d\n", "reboot", ret);
  98. }
  99. psci_power_down_wfi();
  100. }
  101. static int sunxi_system_reset2(int is_vendor, int reset_type, u_register_t cookie)
  102. {
  103. uint32_t ret;
  104. if (is_vendor || (reset_type != PSCI_RESET2_SYSTEM_WARM_RESET))
  105. return PSCI_E_NOT_SUPPORTED;
  106. gicv2_cpuif_disable();
  107. /* Send the system reset request to the SCP. */
  108. ret = scpi_sys_power_state(scpi_system_reset);
  109. if (ret != SCP_OK) {
  110. ERROR("PSCI: SCPI %s failed: %d\n", "reset", ret);
  111. return PSCI_E_INVALID_PARAMS;
  112. }
  113. psci_power_down_wfi();
  114. /*
  115. * Should not reach here.
  116. * However sunxi_system_reset2 has to return some value
  117. * according to PSCI v1.1 spec.
  118. */
  119. return PSCI_E_SUCCESS;
  120. }
  121. static int sunxi_validate_power_state(unsigned int power_state,
  122. psci_power_state_t *req_state)
  123. {
  124. unsigned int power_level = psci_get_pstate_pwrlvl(power_state);
  125. unsigned int state_id = psci_get_pstate_id(power_state);
  126. unsigned int type = psci_get_pstate_type(power_state);
  127. unsigned int i;
  128. assert(req_state != NULL);
  129. if (power_level > PLAT_MAX_PWR_LVL) {
  130. return PSCI_E_INVALID_PARAMS;
  131. }
  132. if (type == PSTATE_TYPE_STANDBY) {
  133. return PSCI_E_INVALID_PARAMS;
  134. }
  135. /* Pass through the requested PSCI state as-is. */
  136. for (i = 0; i <= power_level; ++i) {
  137. unsigned int local_pstate = state_id & PLAT_LOCAL_PSTATE_MASK;
  138. req_state->pwr_domain_state[i] = local_pstate;
  139. state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
  140. }
  141. /* Higher power domain levels should all remain running */
  142. for (; i <= PLAT_MAX_PWR_LVL; ++i) {
  143. req_state->pwr_domain_state[i] = PSCI_LOCAL_STATE_RUN;
  144. }
  145. return PSCI_E_SUCCESS;
  146. }
  147. static void sunxi_get_sys_suspend_power_state(psci_power_state_t *req_state)
  148. {
  149. assert(req_state != NULL);
  150. for (unsigned int i = 0; i <= PLAT_MAX_PWR_LVL; ++i) {
  151. req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
  152. }
  153. }
  154. static const plat_psci_ops_t sunxi_scpi_psci_ops = {
  155. .cpu_standby = sunxi_cpu_standby,
  156. .pwr_domain_on = sunxi_pwr_domain_on,
  157. .pwr_domain_off = sunxi_pwr_domain_off,
  158. .pwr_domain_suspend = sunxi_pwr_domain_off,
  159. .pwr_domain_on_finish = sunxi_pwr_domain_on_finish,
  160. .pwr_domain_suspend_finish = sunxi_pwr_domain_on_finish,
  161. .system_off = sunxi_system_off,
  162. .system_reset = sunxi_system_reset,
  163. .system_reset2 = sunxi_system_reset2,
  164. .validate_power_state = sunxi_validate_power_state,
  165. .validate_ns_entrypoint = sunxi_validate_ns_entrypoint,
  166. .get_sys_suspend_power_state = sunxi_get_sys_suspend_power_state,
  167. };
  168. int sunxi_set_scpi_psci_ops(const plat_psci_ops_t **psci_ops)
  169. {
  170. *psci_ops = &sunxi_scpi_psci_ops;
  171. /* Check for a valid SCP firmware. */
  172. if (mmio_read_32(SUNXI_SCP_BASE) != SCP_FIRMWARE_MAGIC) {
  173. return -1;
  174. }
  175. /* Program SCP exception vectors to the firmware entrypoint. */
  176. for (unsigned int i = OR1K_VEC_FIRST; i <= OR1K_VEC_LAST; ++i) {
  177. uint32_t vector = SUNXI_SRAM_A2_BASE + OR1K_VEC_ADDR(i);
  178. uint32_t offset = SUNXI_SCP_BASE - vector;
  179. mmio_write_32(vector, offset >> 2);
  180. }
  181. /* Take the SCP out of reset. */
  182. mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0));
  183. /* Wait for the SCP firmware to boot. */
  184. return scpi_wait_ready();
  185. }