mt_spm_conservation.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2020, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <lib/mmio.h>
  8. #include <mt_spm.h>
  9. #include <mt_spm_conservation.h>
  10. #include <mt_spm_internal.h>
  11. #include <mt_spm_reg.h>
  12. #include <mt_spm_vcorefs.h>
  13. #include <plat_mtk_lpm.h>
  14. #include <plat_pm.h>
  15. #include <plat/common/platform.h>
  16. #include <platform_def.h>
  17. struct wake_status spm_wakesta; /* record last wakesta */
  18. static int go_to_spm_before_wfi(int state_id, unsigned int ext_opand,
  19. struct spm_lp_scen *spm_lp,
  20. unsigned int resource_req)
  21. {
  22. int ret = 0;
  23. struct pwr_ctrl *pwrctrl;
  24. uint32_t cpu = plat_my_core_pos();
  25. pwrctrl = spm_lp->pwrctrl;
  26. __spm_set_cpu_status(cpu);
  27. __spm_set_power_control(pwrctrl);
  28. __spm_set_wakeup_event(pwrctrl);
  29. __spm_sync_vcore_dvfs_power_control(pwrctrl, __spm_vcorefs.pwrctrl);
  30. __spm_set_pcm_flags(pwrctrl);
  31. __spm_src_req_update(pwrctrl, resource_req);
  32. if ((ext_opand & MT_SPM_EX_OP_SET_WDT) != 0U) {
  33. __spm_set_pcm_wdt(1);
  34. }
  35. if ((ext_opand & MT_SPM_EX_OP_SRCLKEN_RC_BBLPM) != 0U) {
  36. __spm_xo_soc_bblpm(1);
  37. }
  38. if ((ext_opand & MT_SPM_EX_OP_HW_S1_DETECT) != 0U) {
  39. spm_hw_s1_state_monitor_resume();
  40. }
  41. /* Disable auto resume by PCM in system suspend stage */
  42. if (IS_PLAT_SUSPEND_ID(state_id)) {
  43. __spm_disable_pcm_timer();
  44. __spm_set_pcm_wdt(0);
  45. }
  46. __spm_send_cpu_wakeup_event();
  47. INFO("cpu%d: wakesrc = 0x%x, settle = 0x%x, sec = %u\n",
  48. cpu, pwrctrl->wake_src, mmio_read_32(SPM_CLK_SETTLE),
  49. mmio_read_32(PCM_TIMER_VAL) / 32768);
  50. INFO("sw_flag = 0x%x 0x%x, req = 0x%x, pwr = 0x%x 0x%x\n",
  51. pwrctrl->pcm_flags, pwrctrl->pcm_flags1,
  52. mmio_read_32(SPM_SRC_REQ), mmio_read_32(PWR_STATUS),
  53. mmio_read_32(PWR_STATUS_2ND));
  54. return ret;
  55. }
  56. static void go_to_spm_after_wfi(int state_id, unsigned int ext_opand,
  57. struct spm_lp_scen *spm_lp,
  58. struct wake_status **status)
  59. {
  60. unsigned int ext_status = 0U;
  61. /* system watchdog will be resumed at kernel stage */
  62. if ((ext_opand & MT_SPM_EX_OP_SET_WDT) != 0U) {
  63. __spm_set_pcm_wdt(0);
  64. }
  65. if ((ext_opand & MT_SPM_EX_OP_SRCLKEN_RC_BBLPM) != 0U) {
  66. __spm_xo_soc_bblpm(0);
  67. }
  68. if ((ext_opand & MT_SPM_EX_OP_HW_S1_DETECT) != 0U) {
  69. spm_hw_s1_state_monitor_pause(&ext_status);
  70. }
  71. __spm_ext_int_wakeup_req_clr();
  72. __spm_get_wakeup_status(&spm_wakesta, ext_status);
  73. if (status != NULL) {
  74. *status = &spm_wakesta;
  75. }
  76. __spm_clean_after_wakeup();
  77. if (IS_PLAT_SUSPEND_ID(state_id)) {
  78. __spm_output_wake_reason(state_id, &spm_wakesta);
  79. }
  80. }
  81. int spm_conservation(int state_id, unsigned int ext_opand,
  82. struct spm_lp_scen *spm_lp, unsigned int resource_req)
  83. {
  84. if (spm_lp == NULL) {
  85. return -1;
  86. }
  87. spm_lock_get();
  88. go_to_spm_before_wfi(state_id, ext_opand, spm_lp, resource_req);
  89. spm_lock_release();
  90. return 0;
  91. }
  92. void spm_conservation_finish(int state_id, unsigned int ext_opand,
  93. struct spm_lp_scen *spm_lp,
  94. struct wake_status **status)
  95. {
  96. spm_lock_get();
  97. go_to_spm_after_wfi(state_id, ext_opand, spm_lp, status);
  98. spm_lock_release();
  99. }
  100. int spm_conservation_get_result(struct wake_status **res)
  101. {
  102. if (res == NULL) {
  103. return -1;
  104. }
  105. *res = &spm_wakesta;
  106. return 0;
  107. }
  108. #define GPIO_BANK (GPIO_BASE + 0x6F0)
  109. #define TRAP_UFS_FIRST BIT(11) /* bit 11, 0: UFS, 1: eMMC */
  110. void spm_conservation_pwrctrl_init(struct pwr_ctrl *pwrctrl)
  111. {
  112. if (pwrctrl == NULL) {
  113. return;
  114. }
  115. /* For ufs, emmc storage type */
  116. if ((mmio_read_32(GPIO_BANK) & TRAP_UFS_FIRST) != 0U) {
  117. /* If eMMC is used, mask UFS req */
  118. pwrctrl->reg_ufs_srcclkena_mask_b = 0;
  119. pwrctrl->reg_ufs_infra_req_mask_b = 0;
  120. pwrctrl->reg_ufs_apsrc_req_mask_b = 0;
  121. pwrctrl->reg_ufs_vrf18_req_mask_b = 0;
  122. pwrctrl->reg_ufs_ddr_en_mask_b = 0;
  123. }
  124. }