psci-mx7.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
  4. * Copyright 2017 NXP
  5. */
  6. #include <asm/io.h>
  7. #include <asm/psci.h>
  8. #include <asm/secure.h>
  9. #include <asm/arch/imx-regs.h>
  10. #include <linux/bitops.h>
  11. #include <common.h>
  12. #include <fsl_wdog.h>
  13. #define GPC_CPU_PGC_SW_PDN_REQ 0xfc
  14. #define GPC_CPU_PGC_SW_PUP_REQ 0xf0
  15. #define GPC_PGC_C0 0x800
  16. #define GPC_PGC_C1 0x840
  17. #define BM_CPU_PGC_SW_PDN_PUP_REQ_CORE0_A7 0x1
  18. #define BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7 0x2
  19. /* below is for i.MX7D */
  20. #define SRC_GPR1_MX7D 0x074
  21. #define SRC_A7RCR0 0x004
  22. #define SRC_A7RCR1 0x008
  23. #define BP_SRC_A7RCR0_A7_CORE_RESET0 0
  24. #define BP_SRC_A7RCR1_A7_CORE1_ENABLE 1
  25. #define SNVS_LPCR 0x38
  26. #define BP_SNVS_LPCR_DP_EN 0x20
  27. #define BP_SNVS_LPCR_TOP 0x40
  28. #define CCM_CCGR_SNVS 0x4250
  29. #define CCM_ROOT_WDOG 0xbb80
  30. #define CCM_CCGR_WDOG1 0x49c0
  31. #define MPIDR_AFF0 GENMASK(7, 0)
  32. #define IMX7D_PSCI_NR_CPUS 2
  33. #if IMX7D_PSCI_NR_CPUS > CONFIG_ARMV7_PSCI_NR_CPUS
  34. #error "invalid value for CONFIG_ARMV7_PSCI_NR_CPUS"
  35. #endif
  36. u8 psci_state[IMX7D_PSCI_NR_CPUS] __secure_data = {
  37. PSCI_AFFINITY_LEVEL_ON,
  38. PSCI_AFFINITY_LEVEL_OFF};
  39. static inline void psci_set_state(int cpu, u8 state)
  40. {
  41. psci_state[cpu] = state;
  42. dsb();
  43. isb();
  44. }
  45. static inline void imx_gpcv2_set_m_core_pgc(bool enable, u32 offset)
  46. {
  47. writel(enable, GPC_IPS_BASE_ADDR + offset);
  48. }
  49. __secure void imx_gpcv2_set_core_power(int cpu, bool pdn)
  50. {
  51. u32 reg = pdn ? GPC_CPU_PGC_SW_PUP_REQ : GPC_CPU_PGC_SW_PDN_REQ;
  52. u32 pgc = cpu ? GPC_PGC_C1 : GPC_PGC_C0;
  53. u32 pdn_pup_req = cpu ? BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7 :
  54. BM_CPU_PGC_SW_PDN_PUP_REQ_CORE0_A7;
  55. u32 val;
  56. imx_gpcv2_set_m_core_pgc(true, pgc);
  57. val = readl(GPC_IPS_BASE_ADDR + reg);
  58. val |= pdn_pup_req;
  59. writel(val, GPC_IPS_BASE_ADDR + reg);
  60. while ((readl(GPC_IPS_BASE_ADDR + reg) & pdn_pup_req) != 0)
  61. ;
  62. imx_gpcv2_set_m_core_pgc(false, pgc);
  63. }
  64. __secure void imx_enable_cpu_ca7(int cpu, bool enable)
  65. {
  66. u32 mask, val;
  67. mask = 1 << (BP_SRC_A7RCR1_A7_CORE1_ENABLE + cpu - 1);
  68. val = readl(SRC_BASE_ADDR + SRC_A7RCR1);
  69. val = enable ? val | mask : val & ~mask;
  70. writel(val, SRC_BASE_ADDR + SRC_A7RCR1);
  71. }
  72. __secure void psci_arch_cpu_entry(void)
  73. {
  74. u32 cpu = psci_get_cpu_id();
  75. psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON);
  76. }
  77. __secure s32 psci_cpu_on(u32 __always_unused function_id, u32 mpidr, u32 ep,
  78. u32 context_id)
  79. {
  80. u32 cpu = mpidr & MPIDR_AFF0;
  81. if (mpidr & ~MPIDR_AFF0)
  82. return ARM_PSCI_RET_INVAL;
  83. if (cpu >= IMX7D_PSCI_NR_CPUS)
  84. return ARM_PSCI_RET_INVAL;
  85. if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON)
  86. return ARM_PSCI_RET_ALREADY_ON;
  87. if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON_PENDING)
  88. return ARM_PSCI_RET_ON_PENDING;
  89. psci_save(cpu, ep, context_id);
  90. writel((u32)psci_cpu_entry, SRC_BASE_ADDR + cpu * 8 + SRC_GPR1_MX7D);
  91. psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON_PENDING);
  92. imx_gpcv2_set_core_power(cpu, true);
  93. imx_enable_cpu_ca7(cpu, true);
  94. return ARM_PSCI_RET_SUCCESS;
  95. }
  96. __secure s32 psci_cpu_off(void)
  97. {
  98. int cpu;
  99. cpu = psci_get_cpu_id();
  100. psci_cpu_off_common();
  101. psci_set_state(cpu, PSCI_AFFINITY_LEVEL_OFF);
  102. imx_enable_cpu_ca7(cpu, false);
  103. imx_gpcv2_set_core_power(cpu, false);
  104. writel(0, SRC_BASE_ADDR + cpu * 8 + SRC_GPR1_MX7D + 4);
  105. while (1)
  106. wfi();
  107. }
  108. __secure void psci_system_reset(void)
  109. {
  110. struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
  111. /* make sure WDOG1 clock is enabled */
  112. writel(0x1 << 28, CCM_BASE_ADDR + CCM_ROOT_WDOG);
  113. writel(0x3, CCM_BASE_ADDR + CCM_CCGR_WDOG1);
  114. writew(WCR_WDE, &wdog->wcr);
  115. while (1)
  116. wfi();
  117. }
  118. __secure void psci_system_off(void)
  119. {
  120. u32 val;
  121. /* make sure SNVS clock is enabled */
  122. writel(0x3, CCM_BASE_ADDR + CCM_CCGR_SNVS);
  123. val = readl(SNVS_BASE_ADDR + SNVS_LPCR);
  124. val |= BP_SNVS_LPCR_DP_EN | BP_SNVS_LPCR_TOP;
  125. writel(val, SNVS_BASE_ADDR + SNVS_LPCR);
  126. while (1)
  127. wfi();
  128. }
  129. __secure u32 psci_version(void)
  130. {
  131. return ARM_PSCI_VER_1_0;
  132. }
  133. __secure s32 psci_cpu_suspend(u32 __always_unused function_id, u32 power_state,
  134. u32 entry_point_address,
  135. u32 context_id)
  136. {
  137. return ARM_PSCI_RET_INVAL;
  138. }
  139. __secure s32 psci_affinity_info(u32 __always_unused function_id,
  140. u32 target_affinity,
  141. u32 lowest_affinity_level)
  142. {
  143. u32 cpu = target_affinity & MPIDR_AFF0;
  144. if (lowest_affinity_level > 0)
  145. return ARM_PSCI_RET_INVAL;
  146. if (target_affinity & ~MPIDR_AFF0)
  147. return ARM_PSCI_RET_INVAL;
  148. if (cpu >= IMX7D_PSCI_NR_CPUS)
  149. return ARM_PSCI_RET_INVAL;
  150. return psci_state[cpu];
  151. }
  152. __secure s32 psci_migrate_info_type(u32 function_id)
  153. {
  154. /* Trusted OS is either not present or does not require migration */
  155. return 2;
  156. }
  157. __secure s32 psci_features(u32 __always_unused function_id, u32 psci_fid)
  158. {
  159. switch (psci_fid) {
  160. case ARM_PSCI_0_2_FN_PSCI_VERSION:
  161. case ARM_PSCI_0_2_FN_CPU_OFF:
  162. case ARM_PSCI_0_2_FN_CPU_ON:
  163. case ARM_PSCI_0_2_FN_AFFINITY_INFO:
  164. case ARM_PSCI_0_2_FN_MIGRATE_INFO_TYPE:
  165. case ARM_PSCI_0_2_FN_SYSTEM_OFF:
  166. case ARM_PSCI_0_2_FN_SYSTEM_RESET:
  167. case ARM_PSCI_1_0_FN_PSCI_FEATURES:
  168. return 0x0;
  169. }
  170. return ARM_PSCI_RET_NI;
  171. }