gpc_common.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2018-2023, 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 <common/runtime_svc.h>
  11. #include <lib/mmio.h>
  12. #include <lib/psci/psci.h>
  13. #include <gpc.h>
  14. #include <imx8m_psci.h>
  15. #include <plat_imx8.h>
  16. #define MAX_PLL_NUM U(10)
  17. static uint32_t gpc_imr_offset[] = { IMR1_CORE0_A53, IMR1_CORE1_A53, IMR1_CORE2_A53, IMR1_CORE3_A53, };
  18. DEFINE_BAKERY_LOCK(gpc_lock);
  19. #define FSL_SIP_CONFIG_GPC_PM_DOMAIN 0x03
  20. #pragma weak imx_set_cpu_pwr_off
  21. #pragma weak imx_set_cpu_pwr_on
  22. #pragma weak imx_set_cpu_lpm
  23. #pragma weak imx_set_cluster_powerdown
  24. #pragma weak imx_set_sys_wakeup
  25. #pragma weak imx_noc_slot_config
  26. #pragma weak imx_gpc_handler
  27. #pragma weak imx_anamix_override
  28. void imx_set_cpu_secure_entry(unsigned int core_id, uintptr_t sec_entrypoint)
  29. {
  30. uint64_t temp_base;
  31. temp_base = (uint64_t) sec_entrypoint;
  32. temp_base >>= 2;
  33. mmio_write_32(IMX_SRC_BASE + SRC_GPR1_OFFSET + (core_id << 3),
  34. ((uint32_t)(temp_base >> 22) & 0xffff));
  35. mmio_write_32(IMX_SRC_BASE + SRC_GPR1_OFFSET + (core_id << 3) + 4,
  36. ((uint32_t)temp_base & 0x003fffff));
  37. }
  38. void imx_set_cpu_pwr_off(unsigned int core_id)
  39. {
  40. bakery_lock_get(&gpc_lock);
  41. /* enable the wfi power down of the core */
  42. mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id));
  43. bakery_lock_release(&gpc_lock);
  44. /* assert the pcg pcr bit of the core */
  45. mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1);
  46. }
  47. void imx_set_cpu_pwr_on(unsigned int core_id)
  48. {
  49. bakery_lock_get(&gpc_lock);
  50. /* clear the wfi power down bit of the core */
  51. mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id));
  52. bakery_lock_release(&gpc_lock);
  53. /* assert the ncpuporeset */
  54. mmio_clrbits_32(IMX_SRC_BASE + SRC_A53RCR1, (1 << core_id));
  55. /* assert the pcg pcr bit of the core */
  56. mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1);
  57. /* sw power up the core */
  58. mmio_setbits_32(IMX_GPC_BASE + CPU_PGC_UP_TRG, (1 << core_id));
  59. /* wait for the power up finished */
  60. while ((mmio_read_32(IMX_GPC_BASE + CPU_PGC_UP_TRG) & (1 << core_id)) != 0)
  61. ;
  62. /* deassert the pcg pcr bit of the core */
  63. mmio_clrbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1);
  64. /* deassert the ncpuporeset */
  65. mmio_setbits_32(IMX_SRC_BASE + SRC_A53RCR1, (1 << core_id));
  66. }
  67. void imx_set_cpu_lpm(unsigned int core_id, bool pdn)
  68. {
  69. bakery_lock_get(&gpc_lock);
  70. if (pdn) {
  71. /* enable the core WFI PDN & IRQ PUP */
  72. mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) |
  73. COREx_IRQ_WUP(core_id));
  74. /* assert the pcg pcr bit of the core */
  75. mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1);
  76. } else {
  77. /* disable CORE WFI PDN & IRQ PUP */
  78. mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) |
  79. COREx_IRQ_WUP(core_id));
  80. /* deassert the pcg pcr bit of the core */
  81. mmio_clrbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1);
  82. }
  83. bakery_lock_release(&gpc_lock);
  84. }
  85. /*
  86. * the plat and noc can only be power up & down by slot method,
  87. * slot0: plat power down; slot1: noc power down; slot2: noc power up;
  88. * slot3: plat power up. plat's pup&pdn ack is used by default. if
  89. * noc is config to power down, then noc's pdn ack should be used.
  90. */
  91. static void imx_a53_plat_slot_config(bool pdn)
  92. {
  93. if (pdn) {
  94. mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(0), PLAT_PDN_SLT_CTRL);
  95. mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(3), PLAT_PUP_SLT_CTRL);
  96. mmio_write_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, A53_PLAT_PDN_ACK |
  97. A53_PLAT_PUP_ACK);
  98. mmio_setbits_32(IMX_GPC_BASE + PLAT_PGC_PCR, 0x1);
  99. } else {
  100. mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(0), PLAT_PDN_SLT_CTRL);
  101. mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(3), PLAT_PUP_SLT_CTRL);
  102. mmio_write_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, A53_DUMMY_PUP_ACK |
  103. A53_DUMMY_PDN_ACK);
  104. mmio_clrbits_32(IMX_GPC_BASE + PLAT_PGC_PCR, 0x1);
  105. }
  106. }
  107. void imx_set_cluster_standby(bool enter)
  108. {
  109. /*
  110. * Enable BIT 6 of A53 AD register to make sure system
  111. * don't enter LPM mode.
  112. */
  113. if (enter)
  114. mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, (1 << 6));
  115. else
  116. mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_AD, (1 << 6));
  117. }
  118. /* i.mx8mq need to override it */
  119. void imx_set_cluster_powerdown(unsigned int last_core, uint8_t power_state)
  120. {
  121. uint32_t val;
  122. if (!is_local_state_run(power_state)) {
  123. /* config C0~1's LPM, enable a53 clock off in LPM */
  124. mmio_clrsetbits_32(IMX_GPC_BASE + LPCR_A53_BSC, A53_CLK_ON_LPM,
  125. LPM_MODE(power_state));
  126. /* config C2-3's LPM */
  127. mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_BSC2, LPM_MODE(power_state));
  128. /* enable PLAT/SCU power down */
  129. val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_AD);
  130. val &= ~EN_L2_WFI_PDN;
  131. /* L2 cache memory is on in WAIT mode */
  132. if (is_local_state_off(power_state)) {
  133. val |= (L2PGE | EN_PLAT_PDN);
  134. imx_a53_plat_slot_config(true);
  135. }
  136. mmio_write_32(IMX_GPC_BASE + LPCR_A53_AD, val);
  137. } else {
  138. /* clear the slot and ack for cluster power down */
  139. imx_a53_plat_slot_config(false);
  140. /* reverse the cluster level setting */
  141. mmio_clrsetbits_32(IMX_GPC_BASE + LPCR_A53_BSC, 0xf, A53_CLK_ON_LPM);
  142. mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_BSC2, 0xf);
  143. /* clear PLAT/SCU power down */
  144. mmio_clrsetbits_32(IMX_GPC_BASE + LPCR_A53_AD, (L2PGE | EN_PLAT_PDN),
  145. EN_L2_WFI_PDN);
  146. }
  147. }
  148. static unsigned int gicd_read_isenabler(uintptr_t base, unsigned int id)
  149. {
  150. unsigned int n = id >> ISENABLER_SHIFT;
  151. return mmio_read_32(base + GICD_ISENABLER + (n << 2));
  152. }
  153. /*
  154. * gic's clock will be gated in system suspend, so gic has no ability to
  155. * to wakeup the system, we need to config the imr based on the irq
  156. * enable status in gic, then gpc will monitor the wakeup irq
  157. */
  158. void imx_set_sys_wakeup(unsigned int last_core, bool pdn)
  159. {
  160. uint32_t irq_mask;
  161. uintptr_t gicd_base = PLAT_GICD_BASE;
  162. if (pdn)
  163. mmio_clrsetbits_32(IMX_GPC_BASE + LPCR_A53_BSC, A53_CORE_WUP_SRC(last_core),
  164. IRQ_SRC_A53_WUP);
  165. else
  166. mmio_clrsetbits_32(IMX_GPC_BASE + LPCR_A53_BSC, IRQ_SRC_A53_WUP,
  167. A53_CORE_WUP_SRC(last_core));
  168. /* clear last core's IMR based on GIC's mask setting */
  169. for (int i = 0; i < IRQ_IMR_NUM; i++) {
  170. if (pdn)
  171. /* set the wakeup irq base GIC */
  172. irq_mask = ~gicd_read_isenabler(gicd_base, 32 * (i + 1));
  173. else
  174. irq_mask = IMR_MASK_ALL;
  175. mmio_write_32(IMX_GPC_BASE + gpc_imr_offset[last_core] + i * 4,
  176. irq_mask);
  177. }
  178. }
  179. /*
  180. * this function only need to be override by platform
  181. * that support noc power down, for example: imx8mm.
  182. * otherwize, keep it empty.
  183. */
  184. void imx_noc_slot_config(bool pdn)
  185. {
  186. }
  187. /* this is common for all imx8m soc */
  188. void imx_set_sys_lpm(unsigned int last_core, bool retention)
  189. {
  190. uint32_t val;
  191. val = mmio_read_32(IMX_GPC_BASE + SLPCR);
  192. val &= ~(SLPCR_EN_DSM | SLPCR_VSTBY | SLPCR_SBYOS |
  193. SLPCR_BYPASS_PMIC_READY | SLPCR_A53_FASTWUP_STOP_MODE);
  194. if (retention)
  195. val |= (SLPCR_EN_DSM | SLPCR_VSTBY | SLPCR_SBYOS |
  196. SLPCR_BYPASS_PMIC_READY);
  197. mmio_write_32(IMX_GPC_BASE + SLPCR, val);
  198. /* config the noc power down */
  199. imx_noc_slot_config(retention);
  200. /* config wakeup irqs' mask in gpc */
  201. imx_set_sys_wakeup(last_core, retention);
  202. }
  203. void imx_set_rbc_count(void)
  204. {
  205. mmio_setbits_32(IMX_GPC_BASE + SLPCR, SLPCR_RBC_EN |
  206. (0x8 << SLPCR_RBC_COUNT_SHIFT));
  207. }
  208. void imx_clear_rbc_count(void)
  209. {
  210. mmio_clrbits_32(IMX_GPC_BASE + SLPCR, SLPCR_RBC_EN |
  211. (0x3f << SLPCR_RBC_COUNT_SHIFT));
  212. }
  213. struct pll_override pll[MAX_PLL_NUM] = {
  214. {.reg = 0x0, .override_mask = (1 << 12) | (1 << 8), },
  215. {.reg = 0x14, .override_mask = (1 << 12) | (1 << 8), },
  216. {.reg = 0x28, .override_mask = (1 << 12) | (1 << 8), },
  217. {.reg = 0x50, .override_mask = (1 << 12) | (1 << 8), },
  218. {.reg = 0x64, .override_mask = (1 << 10) | (1 << 8), },
  219. {.reg = 0x74, .override_mask = (1 << 10) | (1 << 8), },
  220. {.reg = 0x84, .override_mask = (1 << 10) | (1 << 8), },
  221. {.reg = 0x94, .override_mask = 0x5555500, },
  222. {.reg = 0x104, .override_mask = 0x5555500, },
  223. {.reg = 0x114, .override_mask = 0x500, },
  224. };
  225. #define PLL_BYPASS BIT(4)
  226. void imx_anamix_override(bool enter)
  227. {
  228. unsigned int i;
  229. /*
  230. * bypass all the plls & enable the override bit before
  231. * entering DSM mode.
  232. */
  233. for (i = 0U; i < MAX_PLL_NUM; i++) {
  234. if (enter) {
  235. mmio_setbits_32(IMX_ANAMIX_BASE + pll[i].reg, PLL_BYPASS);
  236. mmio_setbits_32(IMX_ANAMIX_BASE + pll[i].reg, pll[i].override_mask);
  237. } else {
  238. mmio_clrbits_32(IMX_ANAMIX_BASE + pll[i].reg, PLL_BYPASS);
  239. mmio_clrbits_32(IMX_ANAMIX_BASE + pll[i].reg, pll[i].override_mask);
  240. }
  241. }
  242. }
  243. int imx_gpc_handler(uint32_t smc_fid, u_register_t x1, u_register_t x2, u_register_t x3)
  244. {
  245. switch (x1) {
  246. case FSL_SIP_CONFIG_GPC_PM_DOMAIN:
  247. imx_gpc_pm_domain_enable(x2, x3);
  248. break;
  249. default:
  250. return SMC_UNK;
  251. }
  252. return 0;
  253. }