npcm845x_psci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2015-2023, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * Copyright (C) 2017-2023 Nuvoton Ltd.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include <assert.h>
  9. #include <stdbool.h>
  10. #include <arch.h>
  11. #include <arch_helpers.h>
  12. #include <common/debug.h>
  13. #include <drivers/arm/gicv2.h>
  14. #include <lib/mmio.h>
  15. #include <lib/psci/psci.h>
  16. #include <lib/semihosting.h>
  17. #include <npcm845x_clock.h>
  18. #include <plat/arm/common/plat_arm.h>
  19. #include <plat/common/platform.h>
  20. #include <plat_npcm845x.h>
  21. #define ADP_STOPPED_APPLICATION_EXIT 0x20026
  22. /* Make composite power state parameter till power level 0 */
  23. #if PSCI_EXTENDED_STATE_ID
  24. /* Not Extended */
  25. #define npcm845x_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
  26. (((lvl0_state) << PSTATE_ID_SHIFT) | \
  27. ((type) << PSTATE_TYPE_SHIFT))
  28. #else
  29. #define npcm845x_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
  30. (((lvl0_state) << PSTATE_ID_SHIFT) | \
  31. ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
  32. ((type) << PSTATE_TYPE_SHIFT))
  33. #endif /* PSCI_EXTENDED_STATE_ID */
  34. #define npcm845x_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
  35. (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
  36. npcm845x_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
  37. /*
  38. * The table storing the valid idle power states. Ensure that the
  39. * array entries are populated in ascending order of state-id to
  40. * enable us to use binary search during power state validation.
  41. * The table must be terminated by a NULL entry.
  42. */
  43. static const unsigned int npcm845x_pm_idle_states[] = {
  44. /*
  45. * Cluster = 0 (RUN) CPU=1 (RET, higest in idle) -
  46. * Retention. The Power state is Stand-by
  47. */
  48. /* State-id - 0x01 */
  49. npcm845x_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
  50. MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
  51. /*
  52. * For testing purposes.
  53. * Only CPU suspend to standby is supported by NPCM845x
  54. */
  55. /* State-id - 0x02 */
  56. npcm845x_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
  57. MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
  58. 0,
  59. };
  60. /*******************************************************************************
  61. * Platform handler called to check the validity of the non secure
  62. * entrypoint.
  63. ******************************************************************************/
  64. int npcm845x_validate_ns_entrypoint(uintptr_t entrypoint)
  65. {
  66. /*
  67. * Check if the non secure entrypoint lies within the non
  68. * secure DRAM.
  69. */
  70. NOTICE("%s() nuvoton_psci\n", __func__);
  71. #ifdef PLAT_ARM_TRUSTED_DRAM_BASE
  72. if ((entrypoint >= PLAT_ARM_TRUSTED_DRAM_BASE) &&
  73. (entrypoint < (PLAT_ARM_TRUSTED_DRAM_BASE +
  74. PLAT_ARM_TRUSTED_DRAM_SIZE))) {
  75. return PSCI_E_INVALID_ADDRESS;
  76. }
  77. #endif /* PLAT_ARM_TRUSTED_DRAM_BASE */
  78. /* For TFTS purposes, '0' is also illegal */
  79. #ifdef SPD_tspd
  80. if (entrypoint == 0) {
  81. return PSCI_E_INVALID_ADDRESS;
  82. }
  83. #endif /* SPD_tspd */
  84. return PSCI_E_SUCCESS;
  85. }
  86. /*******************************************************************************
  87. * Platform handler called when a CPU is about to enter standby.
  88. ******************************************************************************/
  89. void npcm845x_cpu_standby(plat_local_state_t cpu_state)
  90. {
  91. NOTICE("%s() nuvoton_psci\n", __func__);
  92. uint64_t scr;
  93. scr = read_scr_el3();
  94. write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
  95. /*
  96. * Enter standby state
  97. * dsb is good practice before using wfi to enter low power states
  98. */
  99. isb();
  100. dsb();
  101. wfi();
  102. /* Once awake */
  103. write_scr_el3(scr);
  104. }
  105. /*******************************************************************************
  106. * Platform handler called when a power domain is about to be turned on. The
  107. * mpidr determines the CPU to be turned on.
  108. ******************************************************************************/
  109. int npcm845x_pwr_domain_on(u_register_t mpidr)
  110. {
  111. int rc = PSCI_E_SUCCESS;
  112. int cpu_id = plat_core_pos_by_mpidr(mpidr);
  113. if ((unsigned int)cpu_id >= PLATFORM_CORE_COUNT) {
  114. ERROR("%s() CPU 0x%X\n", __func__, cpu_id);
  115. return PSCI_E_INVALID_PARAMS;
  116. }
  117. if (cpu_id == -1) {
  118. /* domain on was not called by a CPU */
  119. ERROR("%s() was not per CPU 0x%X\n", __func__, cpu_id);
  120. return PSCI_E_INVALID_PARAMS;
  121. }
  122. unsigned int pos = (unsigned int)plat_core_pos_by_mpidr(mpidr);
  123. uintptr_t hold_base = PLAT_NPCM_TM_HOLD_BASE;
  124. assert(pos < PLATFORM_CORE_COUNT);
  125. hold_base += pos * PLAT_NPCM_TM_HOLD_ENTRY_SIZE;
  126. mmio_write_64(hold_base, PLAT_NPCM_TM_HOLD_STATE_GO);
  127. /* No cache maintenance here, hold_base is mapped as device memory. */
  128. /* Make sure that the write has completed */
  129. dsb();
  130. isb();
  131. sev();
  132. return rc;
  133. }
  134. /*******************************************************************************
  135. * Platform handler called when a power domain is about to be suspended. The
  136. * target_state encodes the power state that each level should transition to.
  137. ******************************************************************************/
  138. void npcm845x_pwr_domain_suspend(const psci_power_state_t *target_state)
  139. {
  140. NOTICE("%s() nuvoton_psci\n", __func__);
  141. for (size_t i = 0; (uint64_t)i <= PLAT_MAX_PWR_LVL; i++) {
  142. INFO("%s: target_state->pwr_domain_state[%lu]=%x\n",
  143. __func__, i, target_state->pwr_domain_state[i]);
  144. }
  145. gicv2_cpuif_disable();
  146. NOTICE("%s() Out of suspend\n", __func__);
  147. }
  148. /*******************************************************************************
  149. * Platform handler called when a power domain has just been powered on after
  150. * being turned off earlier. The target_state encodes the low power state that
  151. * each level has woken up from.
  152. ******************************************************************************/
  153. void npcm845x_pwr_domain_on_finish(const psci_power_state_t *target_state)
  154. {
  155. NOTICE("%s() nuvoton_psci\n", __func__);
  156. for (size_t i = 0; (uint64_t)i <= PLAT_MAX_PWR_LVL; i++) {
  157. INFO("%s: target_state->pwr_domain_state[%lu]=%x\n",
  158. __func__, i, target_state->pwr_domain_state[i]);
  159. }
  160. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  161. PLAT_LOCAL_STATE_OFF);
  162. gicv2_pcpu_distif_init();
  163. gicv2_cpuif_enable();
  164. }
  165. /*******************************************************************************
  166. * Platform handler called when a power domain has just been powered on after
  167. * having been suspended earlier. The target_state encodes the low power state
  168. * that each level has woken up from.
  169. ******************************************************************************/
  170. void npcm845x_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
  171. {
  172. NOTICE("%s() nuvoton_psci\n", __func__);
  173. for (size_t i = 0; (uint64_t)i <= PLAT_MAX_PWR_LVL; i++) {
  174. INFO("%s: target_state->pwr_domain_state[%lu]=%x\n",
  175. __func__, i, target_state->pwr_domain_state[i]);
  176. }
  177. assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
  178. PLAT_LOCAL_STATE_OFF);
  179. gicv2_pcpu_distif_init();
  180. gicv2_cpuif_enable();
  181. }
  182. void __dead2 npcm845x_system_reset(void)
  183. {
  184. uintptr_t RESET_BASE_ADDR;
  185. uint32_t val;
  186. NOTICE("%s() nuvoton_psci\n", __func__);
  187. console_flush();
  188. dsbsy();
  189. isb();
  190. /*
  191. * In future - support all reset types. For now, SW1 reset
  192. * Enable software reset 1 to reboot the BMC
  193. */
  194. RESET_BASE_ADDR = (uintptr_t)0xF0801000;
  195. /* Read SW1 control register */
  196. val = mmio_read_32(RESET_BASE_ADDR + 0x44);
  197. /* Keep SPI BMC & MC persist*/
  198. val &= 0xFBFFFFDF;
  199. /* Setting SW1 control register */
  200. mmio_write_32(RESET_BASE_ADDR + 0x44, val);
  201. /* Set SW1 reset */
  202. mmio_write_32(RESET_BASE_ADDR + 0x14, 0x8);
  203. dsb();
  204. while (1) {
  205. ;
  206. }
  207. }
  208. int npcm845x_validate_power_state(unsigned int power_state,
  209. psci_power_state_t *req_state)
  210. {
  211. unsigned int state_id;
  212. int i;
  213. NOTICE("%s() nuvoton_psci\n", __func__);
  214. assert(req_state);
  215. /*
  216. * Currently we are using a linear search for finding the matching
  217. * entry in the idle power state array. This can be made a binary
  218. * search if the number of entries justify the additional complexity.
  219. */
  220. for (i = 0; !!npcm845x_pm_idle_states[i]; i++) {
  221. if (power_state == npcm845x_pm_idle_states[i]) {
  222. break;
  223. }
  224. }
  225. /* Return error if entry not found in the idle state array */
  226. if (!npcm845x_pm_idle_states[i]) {
  227. return PSCI_E_INVALID_PARAMS;
  228. }
  229. i = 0;
  230. state_id = psci_get_pstate_id(power_state);
  231. /* Parse the State ID and populate the state info parameter */
  232. while (state_id) {
  233. req_state->pwr_domain_state[i++] = (uint8_t)state_id &
  234. PLAT_LOCAL_PSTATE_MASK;
  235. state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
  236. }
  237. return PSCI_E_SUCCESS;
  238. }
  239. /*
  240. * The NPCM845 doesn't truly support power management at SYSTEM power domain.
  241. * The SYSTEM_SUSPEND will be down-graded to the cluster level within
  242. * the platform layer. The `fake` SYSTEM_SUSPEND allows us to validate
  243. * some of the driver save and restore sequences on FVP.
  244. */
  245. #if !ARM_BL31_IN_DRAM
  246. void npcm845x_get_sys_suspend_power_state(psci_power_state_t *req_state)
  247. {
  248. unsigned int i;
  249. NOTICE("%s() nuvoton_psci\n", __func__);
  250. for (i = ARM_PWR_LVL0; (uint64_t)i <= PLAT_MAX_PWR_LVL; i++) {
  251. req_state->pwr_domain_state[i] = (uint8_t)PLAT_LOCAL_STATE_OFF;
  252. }
  253. }
  254. #endif /* !ARM_BL31_IN_DRAM */
  255. /*
  256. * The rest of the PSCI implementation are for testing purposes only.
  257. * Not supported in Arbel
  258. */
  259. void __dead2 npcm845x_system_off(void)
  260. {
  261. console_flush();
  262. dsbsy();
  263. isb();
  264. /* NPCM845 doesn't allow real system off, Do reaset instead */
  265. /* Do reset here TBD which, in the meanwhile SW1 reset */
  266. for (;;) {
  267. wfi();
  268. }
  269. }
  270. void __dead2 plat_secondary_cold_boot_setup(void);
  271. void __dead2 npcm845x_pwr_down_wfi(
  272. const psci_power_state_t *target_state)
  273. {
  274. uintptr_t hold_base = PLAT_NPCM_TM_HOLD_BASE;
  275. unsigned int pos = plat_my_core_pos();
  276. if (pos == 0) {
  277. /*
  278. * The secondaries will always be in a wait
  279. * for warm boot on reset, but the BSP needs
  280. * to be able to distinguish between waiting
  281. * for warm boot (e.g. after psci_off, waiting
  282. * for psci_on) and a cold boot.
  283. */
  284. mmio_write_64(hold_base, PLAT_NPCM_TM_HOLD_STATE_BSP_OFF);
  285. /* No cache maintenance here, we run with caches off already. */
  286. dsb();
  287. isb();
  288. }
  289. wfe();
  290. while (1) {
  291. ;
  292. }
  293. }
  294. /*******************************************************************************
  295. * Platform handler called when a power domain is about to be turned off. The
  296. * target_state encodes the power state that each level should transition to.
  297. ******************************************************************************/
  298. void npcm845x_pwr_domain_off(const psci_power_state_t *target_state)
  299. {
  300. NOTICE("%s() nuvoton_psci\n", __func__);
  301. for (size_t i = 0; (uint64_t)i <= PLAT_MAX_PWR_LVL; i++) {
  302. INFO("%s: target_state->pwr_domain_state[%lu]=%x\n",
  303. __func__, i, target_state->pwr_domain_state[i]);
  304. }
  305. plat_secondary_cold_boot_setup();
  306. }
  307. static const plat_psci_ops_t npcm845x_plat_psci_ops = {
  308. .cpu_standby = npcm845x_cpu_standby,
  309. .pwr_domain_on = npcm845x_pwr_domain_on,
  310. .pwr_domain_suspend = npcm845x_pwr_domain_suspend,
  311. .pwr_domain_on_finish = npcm845x_pwr_domain_on_finish,
  312. .pwr_domain_suspend_finish = npcm845x_pwr_domain_suspend_finish,
  313. .system_reset = npcm845x_system_reset,
  314. .validate_power_state = npcm845x_validate_power_state,
  315. .validate_ns_entrypoint = npcm845x_validate_ns_entrypoint,
  316. /* For testing purposes only This PSCI states are not supported */
  317. .pwr_domain_off = npcm845x_pwr_domain_off,
  318. .pwr_domain_pwr_down_wfi = npcm845x_pwr_down_wfi,
  319. };
  320. /* For reference only
  321. * typedef struct plat_psci_ops {
  322. * void (*cpu_standby)(plat_local_state_t cpu_state);
  323. * int (*pwr_domain_on)(u_register_t mpidr);
  324. * void (*pwr_domain_off)(const psci_power_state_t *target_state);
  325. * void (*pwr_domain_suspend_pwrdown_early)(
  326. * const psci_power_state_t *target_state);
  327. * void (*pwr_domain_suspend)(const psci_power_state_t *target_state);
  328. * void (*pwr_domain_on_finish)(const psci_power_state_t *target_state);
  329. * void (*pwr_domain_on_finish_late)(
  330. * const psci_power_state_t *target_state);
  331. * void (*pwr_domain_suspend_finish)(
  332. * const psci_power_state_t *target_state);
  333. * void __dead2 (*pwr_domain_pwr_down_wfi)(
  334. * const psci_power_state_t *target_state);
  335. * void __dead2 (*system_off)(void);
  336. * void __dead2 (*system_reset)(void);
  337. * int (*validate_power_state)(unsigned int power_state,
  338. * psci_power_state_t *req_state);
  339. * int (*validate_ns_entrypoint)(uintptr_t ns_entrypoint);
  340. * void (*get_sys_suspend_power_state)(
  341. * psci_power_state_t *req_state);
  342. * int (*get_pwr_lvl_state_idx)(plat_local_state_t pwr_domain_state,
  343. * int pwrlvl);
  344. * int (*translate_power_state_by_mpidr)(u_register_t mpidr,
  345. * unsigned int power_state,
  346. * psci_power_state_t *output_state);
  347. * int (*get_node_hw_state)(u_register_t mpidr, unsigned int power_level);
  348. * int (*mem_protect_chk)(uintptr_t base, u_register_t length);
  349. * int (*read_mem_protect)(int *val);
  350. * int (*write_mem_protect)(int val);
  351. * int (*system_reset2)(int is_vendor,
  352. * int reset_type, u_register_t cookie);
  353. * } plat_psci_ops_t;
  354. */
  355. int plat_setup_psci_ops(uintptr_t sec_entrypoint,
  356. const plat_psci_ops_t **psci_ops)
  357. {
  358. uintptr_t *entrypoint = (void *)PLAT_NPCM_TM_ENTRYPOINT;
  359. *entrypoint = sec_entrypoint;
  360. *psci_ops = &npcm845x_plat_psci_ops;
  361. return 0;
  362. }