imx8m_psci_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2018-2022, 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/delay_timer.h>
  11. #include <lib/mmio.h>
  12. #include <lib/psci/psci.h>
  13. #include <dram.h>
  14. #include <gpc.h>
  15. #include <imx8m_psci.h>
  16. #include <plat_imx8.h>
  17. /*
  18. * below callback functions need to be override by i.mx8mq,
  19. * for other i.mx8m soc, if no special requirement,
  20. * reuse below ones.
  21. */
  22. #pragma weak imx_validate_power_state
  23. #pragma weak imx_domain_suspend
  24. #pragma weak imx_domain_suspend_finish
  25. #pragma weak imx_get_sys_suspend_power_state
  26. int imx_validate_ns_entrypoint(uintptr_t ns_entrypoint)
  27. {
  28. /* The non-secure entrypoint should be in RAM space */
  29. if (ns_entrypoint < PLAT_NS_IMAGE_OFFSET)
  30. return PSCI_E_INVALID_PARAMS;
  31. return PSCI_E_SUCCESS;
  32. }
  33. int imx_pwr_domain_on(u_register_t mpidr)
  34. {
  35. unsigned int core_id;
  36. uint64_t base_addr = BL31_START;
  37. core_id = MPIDR_AFFLVL0_VAL(mpidr);
  38. imx_set_cpu_secure_entry(core_id, base_addr);
  39. imx_set_cpu_pwr_on(core_id);
  40. return PSCI_E_SUCCESS;
  41. }
  42. void imx_pwr_domain_on_finish(const psci_power_state_t *target_state)
  43. {
  44. plat_gic_pcpu_init();
  45. plat_gic_cpuif_enable();
  46. }
  47. void imx_pwr_domain_off(const psci_power_state_t *target_state)
  48. {
  49. uint64_t mpidr = read_mpidr_el1();
  50. unsigned int core_id = MPIDR_AFFLVL0_VAL(mpidr);
  51. plat_gic_cpuif_disable();
  52. imx_set_cpu_pwr_off(core_id);
  53. }
  54. int imx_validate_power_state(unsigned int power_state,
  55. psci_power_state_t *req_state)
  56. {
  57. int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
  58. int pwr_type = psci_get_pstate_type(power_state);
  59. int state_id = psci_get_pstate_id(power_state);
  60. if (pwr_lvl > PLAT_MAX_PWR_LVL)
  61. return PSCI_E_INVALID_PARAMS;
  62. if (pwr_type == PSTATE_TYPE_STANDBY) {
  63. CORE_PWR_STATE(req_state) = PLAT_MAX_RET_STATE;
  64. CLUSTER_PWR_STATE(req_state) = PLAT_MAX_RET_STATE;
  65. }
  66. if (pwr_type == PSTATE_TYPE_POWERDOWN && state_id == 0x33) {
  67. CORE_PWR_STATE(req_state) = PLAT_MAX_OFF_STATE;
  68. CLUSTER_PWR_STATE(req_state) = PLAT_WAIT_RET_STATE;
  69. }
  70. return PSCI_E_SUCCESS;
  71. }
  72. void imx_cpu_standby(plat_local_state_t cpu_state)
  73. {
  74. dsb();
  75. write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
  76. isb();
  77. wfi();
  78. write_scr_el3(read_scr_el3() & (~SCR_FIQ_BIT));
  79. isb();
  80. }
  81. void imx_domain_suspend(const psci_power_state_t *target_state)
  82. {
  83. uint64_t base_addr = BL31_START;
  84. uint64_t mpidr = read_mpidr_el1();
  85. unsigned int core_id = MPIDR_AFFLVL0_VAL(mpidr);
  86. if (is_local_state_off(CORE_PWR_STATE(target_state))) {
  87. plat_gic_cpuif_disable();
  88. imx_set_cpu_secure_entry(core_id, base_addr);
  89. imx_set_cpu_lpm(core_id, true);
  90. } else {
  91. dsb();
  92. write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
  93. isb();
  94. }
  95. if (!is_local_state_run(CLUSTER_PWR_STATE(target_state)))
  96. imx_set_cluster_powerdown(core_id, CLUSTER_PWR_STATE(target_state));
  97. if (is_local_state_off(SYSTEM_PWR_STATE(target_state))) {
  98. imx_set_sys_lpm(core_id, true);
  99. dram_enter_retention();
  100. imx_anamix_override(true);
  101. }
  102. }
  103. void imx_domain_suspend_finish(const psci_power_state_t *target_state)
  104. {
  105. uint64_t mpidr = read_mpidr_el1();
  106. unsigned int core_id = MPIDR_AFFLVL0_VAL(mpidr);
  107. if (is_local_state_off(SYSTEM_PWR_STATE(target_state))) {
  108. imx_anamix_override(false);
  109. dram_exit_retention();
  110. imx_set_sys_lpm(core_id, false);
  111. }
  112. if (!is_local_state_run(CLUSTER_PWR_STATE(target_state))) {
  113. imx_clear_rbc_count();
  114. imx_set_cluster_powerdown(core_id, PSCI_LOCAL_STATE_RUN);
  115. }
  116. if (is_local_state_off(CORE_PWR_STATE(target_state))) {
  117. imx_set_cpu_lpm(core_id, false);
  118. plat_gic_cpuif_enable();
  119. } else {
  120. write_scr_el3(read_scr_el3() & (~SCR_FIQ_BIT));
  121. isb();
  122. }
  123. }
  124. void imx_get_sys_suspend_power_state(psci_power_state_t *req_state)
  125. {
  126. unsigned int i;
  127. for (i = IMX_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++)
  128. req_state->pwr_domain_state[i] = PLAT_STOP_OFF_STATE;
  129. }
  130. static void __dead2 imx_wdog_restart(bool external_reset)
  131. {
  132. uintptr_t wdog_base = IMX_WDOG_BASE;
  133. unsigned int val;
  134. val = mmio_read_16(wdog_base);
  135. /*
  136. * Common watchdog init flags, for additional details check
  137. * 6.6.4.1 Watchdog Control Register (WDOGx_WCR)
  138. *
  139. * Initial bit selection:
  140. * WDOG_WCR_WDE - Enable the watchdog.
  141. *
  142. * 0x000E mask is used to keep previous values (that could be set
  143. * in SPL) of WDBG and WDE/WDT (both are write-one once-only bits).
  144. */
  145. val = (val & 0x000E) | WDOG_WCR_WDE;
  146. if (external_reset) {
  147. /*
  148. * To assert WDOG_B (external reset) we have
  149. * to set WDA bit 0 (already set in previous step).
  150. * SRS bits are required to be set to 1 (no effect on the
  151. * system).
  152. */
  153. val |= WDOG_WCR_SRS;
  154. } else {
  155. /*
  156. * To assert Software Reset Signal (internal reset) we have
  157. * to set SRS bit to 0 (already set in previous step).
  158. * SRE bit is required to be set to 1 when used in
  159. * conjunction with the Software Reset Signal before
  160. * SRS asserton, otherwise SRS bit will just automatically
  161. * reset to 1.
  162. *
  163. * Also we set WDA to 1 (no effect on system).
  164. */
  165. val |= WDOG_WCR_SRE | WDOG_WCR_WDA;
  166. }
  167. mmio_write_16(wdog_base, val);
  168. mmio_write_16(wdog_base + WDOG_WSR, 0x5555);
  169. mmio_write_16(wdog_base + WDOG_WSR, 0xaaaa);
  170. while (1)
  171. ;
  172. }
  173. void __dead2 imx_system_reset(void)
  174. {
  175. #ifdef IMX_WDOG_B_RESET
  176. imx_wdog_restart(true);
  177. #else
  178. imx_wdog_restart(false);
  179. #endif
  180. }
  181. int imx_system_reset2(int is_vendor, int reset_type, u_register_t cookie)
  182. {
  183. imx_wdog_restart(false);
  184. /*
  185. * imx_wdog_restart cannot return (as it's a __dead function),
  186. * however imx_system_reset2 has to return some value according
  187. * to PSCI v1.1 spec.
  188. */
  189. return 0;
  190. }
  191. void __dead2 imx_system_off(void)
  192. {
  193. uint32_t val;
  194. val = mmio_read_32(IMX_SNVS_BASE + SNVS_LPCR);
  195. val |= SNVS_LPCR_SRTC_ENV | SNVS_LPCR_DP_EN | SNVS_LPCR_TOP;
  196. mmio_write_32(IMX_SNVS_BASE + SNVS_LPCR, val);
  197. while (1)
  198. ;
  199. }
  200. void __dead2 imx_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state)
  201. {
  202. /*
  203. * before enter WAIT or STOP mode with PLAT(SCU) power down,
  204. * rbc count need to be enabled to make sure PLAT is
  205. * power down successfully even if the the wakeup IRQ is pending
  206. * early before the power down sequence. the RBC counter is
  207. * drived by the 32K OSC, so delay 30us to make sure the counter
  208. * is really running.
  209. */
  210. if (is_local_state_off(CLUSTER_PWR_STATE(target_state))) {
  211. imx_set_rbc_count();
  212. udelay(30);
  213. }
  214. while (1)
  215. wfi();
  216. }