psci-mx7.c 4.6 KB

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