imx8qx_psci.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdbool.h>
  7. #include <arch.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/arm/gicv3.h>
  11. #include <lib/mmio.h>
  12. #include <lib/psci/psci.h>
  13. #include <plat_imx8.h>
  14. #include <sci/sci.h>
  15. #include "../../common/sci/imx8_mu.h"
  16. static const int ap_core_index[PLATFORM_CORE_COUNT] = {
  17. SC_R_A35_0, SC_R_A35_1, SC_R_A35_2, SC_R_A35_3
  18. };
  19. /* save gic dist/redist context when GIC is power down */
  20. static struct plat_gic_ctx imx_gicv3_ctx;
  21. static unsigned int gpt_lpcg, gpt_reg[2];
  22. static void imx_enable_irqstr_wakeup(void)
  23. {
  24. uint32_t irq_mask;
  25. gicv3_dist_ctx_t *dist_ctx = &imx_gicv3_ctx.dist_ctx;
  26. /* put IRQSTR into ON mode */
  27. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_ON);
  28. /* enable the irqsteer to handle wakeup irq */
  29. mmio_write_32(IMX_WUP_IRQSTR_BASE, 0x1);
  30. for (int i = 0; i < 15; i++) {
  31. irq_mask = dist_ctx->gicd_isenabler[i];
  32. mmio_write_32(IMX_WUP_IRQSTR_BASE + 0x3c - 0x4 * i, irq_mask);
  33. }
  34. /* set IRQSTR low power mode */
  35. if (imx_is_wakeup_src_irqsteer())
  36. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_STBY);
  37. else
  38. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_OFF);
  39. }
  40. static void imx_disable_irqstr_wakeup(void)
  41. {
  42. /* Put IRQSTEER back to ON mode */
  43. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_ON);
  44. /* disable the irqsteer */
  45. mmio_write_32(IMX_WUP_IRQSTR_BASE, 0x0);
  46. for (int i = 0; i < 16; i++)
  47. mmio_write_32(IMX_WUP_IRQSTR_BASE + 0x4 + 0x4 * i, 0x0);
  48. /* Put IRQSTEER into OFF mode */
  49. sc_pm_set_resource_power_mode(ipc_handle, SC_R_IRQSTR_SCU2, SC_PM_PW_MODE_OFF);
  50. }
  51. int imx_pwr_domain_on(u_register_t mpidr)
  52. {
  53. int ret = PSCI_E_SUCCESS;
  54. unsigned int cpu_id;
  55. cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  56. printf("imx_pwr_domain_on cpu_id %d\n", cpu_id);
  57. if (sc_pm_set_resource_power_mode(ipc_handle, ap_core_index[cpu_id],
  58. SC_PM_PW_MODE_ON) != SC_ERR_NONE) {
  59. ERROR("core %d power on failed!\n", cpu_id);
  60. ret = PSCI_E_INTERN_FAIL;
  61. }
  62. if (sc_pm_cpu_start(ipc_handle, ap_core_index[cpu_id],
  63. true, BL31_BASE) != SC_ERR_NONE) {
  64. ERROR("boot core %d failed!\n", cpu_id);
  65. ret = PSCI_E_INTERN_FAIL;
  66. }
  67. return ret;
  68. }
  69. void imx_pwr_domain_on_finish(const psci_power_state_t *target_state)
  70. {
  71. plat_gic_pcpu_init();
  72. plat_gic_cpuif_enable();
  73. }
  74. int imx_validate_ns_entrypoint(uintptr_t ns_entrypoint)
  75. {
  76. return PSCI_E_SUCCESS;
  77. }
  78. void imx_pwr_domain_off(const psci_power_state_t *target_state)
  79. {
  80. u_register_t mpidr = read_mpidr_el1();
  81. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  82. plat_gic_cpuif_disable();
  83. sc_pm_req_cpu_low_power_mode(ipc_handle, ap_core_index[cpu_id],
  84. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_NONE);
  85. printf("turn off core:%d\n", cpu_id);
  86. }
  87. void imx_domain_suspend(const psci_power_state_t *target_state)
  88. {
  89. u_register_t mpidr = read_mpidr_el1();
  90. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  91. if (is_local_state_off(target_state->pwr_domain_state[MPIDR_AFFLVL0])) {
  92. plat_gic_cpuif_disable();
  93. sc_pm_set_cpu_resume(ipc_handle, ap_core_index[cpu_id], true, BL31_BASE);
  94. sc_pm_req_cpu_low_power_mode(ipc_handle, ap_core_index[cpu_id],
  95. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_GIC);
  96. } else {
  97. dsb();
  98. write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
  99. isb();
  100. }
  101. if (is_local_state_off(target_state->pwr_domain_state[MPIDR_AFFLVL1]))
  102. sc_pm_req_low_power_mode(ipc_handle, SC_R_A35, SC_PM_PW_MODE_OFF);
  103. if (is_local_state_retn(target_state->pwr_domain_state[PLAT_MAX_PWR_LVL])) {
  104. plat_gic_cpuif_disable();
  105. /* save gic context */
  106. plat_gic_save(cpu_id, &imx_gicv3_ctx);
  107. /* enable the irqsteer for wakeup */
  108. imx_enable_irqstr_wakeup();
  109. /* Save GPT clock and registers, then turn off its power */
  110. gpt_lpcg = mmio_read_32(IMX_GPT0_LPCG_BASE);
  111. gpt_reg[0] = mmio_read_32(IMX_GPT0_BASE);
  112. gpt_reg[1] = mmio_read_32(IMX_GPT0_BASE + 0x4);
  113. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GPT_0, SC_PM_PW_MODE_OFF);
  114. sc_pm_req_low_power_mode(ipc_handle, SC_R_A35, SC_PM_PW_MODE_OFF);
  115. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_DDR,
  116. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  117. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_MU,
  118. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  119. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_INTERCONNECT,
  120. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_OFF);
  121. /* Put GIC in OFF mode. */
  122. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GIC, SC_PM_PW_MODE_OFF);
  123. sc_pm_set_cpu_resume(ipc_handle, ap_core_index[cpu_id], true, BL31_BASE);
  124. if (imx_is_wakeup_src_irqsteer())
  125. sc_pm_req_cpu_low_power_mode(ipc_handle, ap_core_index[cpu_id],
  126. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_IRQSTEER);
  127. else
  128. sc_pm_req_cpu_low_power_mode(ipc_handle, ap_core_index[cpu_id],
  129. SC_PM_PW_MODE_OFF, SC_PM_WAKE_SRC_SCU);
  130. }
  131. }
  132. void imx_domain_suspend_finish(const psci_power_state_t *target_state)
  133. {
  134. u_register_t mpidr = read_mpidr_el1();
  135. unsigned int cpu_id = MPIDR_AFFLVL0_VAL(mpidr);
  136. if (is_local_state_retn(target_state->pwr_domain_state[PLAT_MAX_PWR_LVL])) {
  137. MU_Resume(SC_IPC_BASE);
  138. sc_pm_req_low_power_mode(ipc_handle, ap_core_index[cpu_id], SC_PM_PW_MODE_ON);
  139. sc_pm_req_cpu_low_power_mode(ipc_handle, ap_core_index[cpu_id],
  140. SC_PM_PW_MODE_ON, SC_PM_WAKE_SRC_GIC);
  141. /* Put GIC back to high power mode. */
  142. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GIC, SC_PM_PW_MODE_ON);
  143. /* restore gic context */
  144. plat_gic_restore(cpu_id, &imx_gicv3_ctx);
  145. /* Turn on GPT power and restore its clock and registers */
  146. sc_pm_set_resource_power_mode(ipc_handle, SC_R_GPT_0, SC_PM_PW_MODE_ON);
  147. sc_pm_clock_enable(ipc_handle, SC_R_GPT_0, SC_PM_CLK_PER, true, 0);
  148. mmio_write_32(IMX_GPT0_BASE, gpt_reg[0]);
  149. mmio_write_32(IMX_GPT0_BASE + 0x4, gpt_reg[1]);
  150. mmio_write_32(IMX_GPT0_LPCG_BASE, gpt_lpcg);
  151. sc_pm_req_low_power_mode(ipc_handle, SC_R_A35, SC_PM_PW_MODE_ON);
  152. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_DDR,
  153. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  154. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_MU,
  155. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  156. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_INTERCONNECT,
  157. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  158. /* disable the irqsteer wakeup */
  159. imx_disable_irqstr_wakeup();
  160. plat_gic_cpuif_enable();
  161. }
  162. if (is_local_state_off(target_state->pwr_domain_state[MPIDR_AFFLVL1]))
  163. sc_pm_req_low_power_mode(ipc_handle, SC_R_A35, SC_PM_PW_MODE_ON);
  164. if (is_local_state_off(target_state->pwr_domain_state[MPIDR_AFFLVL0])) {
  165. sc_pm_req_cpu_low_power_mode(ipc_handle, ap_core_index[cpu_id],
  166. SC_PM_PW_MODE_ON, SC_PM_WAKE_SRC_GIC);
  167. plat_gic_cpuif_enable();
  168. } else {
  169. write_scr_el3(read_scr_el3() & (~SCR_FIQ_BIT));
  170. isb();
  171. }
  172. }
  173. static const plat_psci_ops_t imx_plat_psci_ops = {
  174. .pwr_domain_on = imx_pwr_domain_on,
  175. .pwr_domain_on_finish = imx_pwr_domain_on_finish,
  176. .validate_ns_entrypoint = imx_validate_ns_entrypoint,
  177. .system_off = imx_system_off,
  178. .system_reset = imx_system_reset,
  179. .pwr_domain_off = imx_pwr_domain_off,
  180. .pwr_domain_suspend = imx_domain_suspend,
  181. .pwr_domain_suspend_finish = imx_domain_suspend_finish,
  182. .get_sys_suspend_power_state = imx_get_sys_suspend_power_state,
  183. .validate_power_state = imx_validate_power_state,
  184. };
  185. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  186. const plat_psci_ops_t **psci_ops)
  187. {
  188. imx_mailbox_init(sec_entrypoint);
  189. *psci_ops = &imx_plat_psci_ops;
  190. /* make sure system sources power ON in low power mode by default */
  191. sc_pm_req_low_power_mode(ipc_handle, SC_R_A35, SC_PM_PW_MODE_ON);
  192. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_DDR,
  193. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  194. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_MU,
  195. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  196. sc_pm_req_sys_if_power_mode(ipc_handle, SC_R_A35, SC_PM_SYS_IF_INTERCONNECT,
  197. SC_PM_PW_MODE_ON, SC_PM_PW_MODE_ON);
  198. return 0;
  199. }