mt_spm_rc_bus26m.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2020-2022, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <common/debug.h>
  8. #include <mt_lp_rm.h>
  9. #include <mt_spm.h>
  10. #include <mt_spm_cond.h>
  11. #include <mt_spm_constraint.h>
  12. #include <mt_spm_conservation.h>
  13. #include <mt_spm_idle.h>
  14. #include <mt_spm_internal.h>
  15. #include <mt_spm_notifier.h>
  16. #include <mt_spm_rc_internal.h>
  17. #include <mt_spm_resource_req.h>
  18. #include <mt_spm_reg.h>
  19. #include <mt_spm_suspend.h>
  20. #include <plat_pm.h>
  21. #include <plat_mtk_lpm.h>
  22. #ifndef ATF_PLAT_CIRQ_UNSUPPORT
  23. #include <mt_cirq.h>
  24. #include <mt_gic_v3.h>
  25. #endif
  26. #define CONSTRAINT_BUS26M_ALLOW \
  27. (MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF | \
  28. MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \
  29. MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \
  30. MT_RM_CONSTRAINT_ALLOW_VCORE_LP | \
  31. MT_RM_CONSTRAINT_ALLOW_LVTS_STATE | \
  32. MT_RM_CONSTRAINT_ALLOW_BUS26M_OFF)
  33. #define CONSTRAINT_BUS26M_PCM_FLAG \
  34. (SPM_FLAG_DISABLE_INFRA_PDN | \
  35. SPM_FLAG_DISABLE_VCORE_DVS | \
  36. SPM_FLAG_DISABLE_VCORE_DFS | \
  37. SPM_FLAG_SRAM_SLEEP_CTRL | \
  38. SPM_FLAG_ENABLE_TIA_WORKAROUND | \
  39. SPM_FLAG_ENABLE_LVTS_WORKAROUND | \
  40. SPM_FLAG_KEEP_CSYSPWRACK_HIGH)
  41. #define CONSTRAINT_BUS26M_PCM_FLAG1 \
  42. (SPM_FLAG1_DISABLE_MD26M_CK_OFF)
  43. #define CONSTRAINT_BUS26M_RESOURCE_REQ 0U
  44. static unsigned int bus26m_ext_opand;
  45. static struct mt_irqremain *refer2remain_irq;
  46. static struct mt_spm_cond_tables cond_bus26m = {
  47. .name = "bus26m",
  48. .table_cg = {
  49. 0x07CBF1FC, /* MTCMOS1 */
  50. 0x0A0D8856, /* INFRA0 */
  51. 0x03AF9A00, /* INFRA1 */
  52. 0x86000650, /* INFRA2 */
  53. 0xC800C000, /* INFRA3 */
  54. 0x00000000, /* INFRA4 */
  55. 0x4000007C, /* INFRA5 */
  56. 0x280E0800, /* MMSYS0 */
  57. 0x00000001, /* MMSYS1 */
  58. 0x00000000, /* MMSYS2 */
  59. },
  60. .table_pll = (PLL_BIT_UNIVPLL | PLL_BIT_MFGPLL |
  61. PLL_BIT_MSDCPLL | PLL_BIT_TVDPLL |
  62. PLL_BIT_MMPLL),
  63. };
  64. static struct mt_spm_cond_tables cond_bus26m_res = {
  65. .table_cg = { 0U },
  66. .table_pll = 0U,
  67. };
  68. static struct constraint_status status = {
  69. .id = MT_RM_CONSTRAINT_ID_BUS26M,
  70. .valid = (MT_SPM_RC_VALID_SW |
  71. MT_SPM_RC_VALID_COND_LATCH),
  72. .cond_block = 0U,
  73. .enter_cnt = 0U,
  74. .cond_res = &cond_bus26m_res,
  75. };
  76. /*
  77. * Cirq will take the place of gic when gic is off.
  78. * However, cirq cannot work if 26m clk is turned off when system idle/suspend.
  79. * Therefore, we need to set irq pending for specific wakeup source.
  80. */
  81. #ifdef ATF_PLAT_CIRQ_UNSUPPORT
  82. #define do_irqs_delivery()
  83. #else
  84. static void mt_spm_irq_remain_dump(struct mt_irqremain *irqs,
  85. unsigned int irq_index,
  86. struct wake_status *wakeup)
  87. {
  88. INFO("[SPM] r12 = 0x%08x(0x%08x), flag = 0x%08x 0x%08x 0x%08x\n",
  89. wakeup->tr.comm.r12, wakeup->md32pcm_wakeup_sta,
  90. wakeup->tr.comm.debug_flag, wakeup->tr.comm.b_sw_flag0,
  91. wakeup->tr.comm.b_sw_flag1);
  92. INFO("irq:%u(0x%08x) set pending\n",
  93. irqs->wakeupsrc[irq_index], irqs->irqs[irq_index]);
  94. }
  95. static void do_irqs_delivery(void)
  96. {
  97. unsigned int idx;
  98. int res = 0;
  99. struct wake_status *wakeup = NULL;
  100. struct mt_irqremain *irqs = refer2remain_irq;
  101. res = spm_conservation_get_result(&wakeup);
  102. if ((res != 0) && (irqs == NULL)) {
  103. return;
  104. }
  105. for (idx = 0U; idx < irqs->count; ++idx) {
  106. if (((wakeup->tr.comm.r12 & irqs->wakeupsrc[idx]) != 0U) ||
  107. ((wakeup->raw_sta & irqs->wakeupsrc[idx]) != 0U)) {
  108. if ((irqs->wakeupsrc_cat[idx] &
  109. MT_IRQ_REMAIN_CAT_LOG) != 0U) {
  110. mt_spm_irq_remain_dump(irqs, idx, wakeup);
  111. }
  112. mt_irq_set_pending(irqs->irqs[idx]);
  113. }
  114. }
  115. }
  116. #endif
  117. static void spm_bus26m_conduct(struct spm_lp_scen *spm_lp,
  118. unsigned int *resource_req)
  119. {
  120. spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG;
  121. spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG1;
  122. *resource_req |= CONSTRAINT_BUS26M_RESOURCE_REQ;
  123. }
  124. bool spm_is_valid_rc_bus26m(unsigned int cpu, int state_id)
  125. {
  126. (void)cpu;
  127. (void)state_id;
  128. return (status.cond_block == 0U) && IS_MT_RM_RC_READY(status.valid);
  129. }
  130. int spm_update_rc_bus26m(int state_id, int type, const void *val)
  131. {
  132. const struct mt_spm_cond_tables *tlb;
  133. const struct mt_spm_cond_tables *tlb_check;
  134. int res = MT_RM_STATUS_OK;
  135. if (val == NULL) {
  136. return MT_RM_STATUS_BAD;
  137. }
  138. if (type == PLAT_RC_UPDATE_CONDITION) {
  139. tlb = (const struct mt_spm_cond_tables *)val;
  140. tlb_check = (const struct mt_spm_cond_tables *)&cond_bus26m;
  141. status.cond_block =
  142. mt_spm_cond_check(state_id, tlb, tlb_check,
  143. ((status.valid &
  144. MT_SPM_RC_VALID_COND_LATCH) != 0U) ?
  145. &cond_bus26m_res : NULL);
  146. } else if (type == PLAT_RC_UPDATE_REMAIN_IRQS) {
  147. refer2remain_irq = (struct mt_irqremain *)val;
  148. } else {
  149. res = MT_RM_STATUS_BAD;
  150. }
  151. return res;
  152. }
  153. unsigned int spm_allow_rc_bus26m(int state_id)
  154. {
  155. (void)state_id;
  156. return CONSTRAINT_BUS26M_ALLOW;
  157. }
  158. int spm_run_rc_bus26m(unsigned int cpu, int state_id)
  159. {
  160. (void)cpu;
  161. #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
  162. mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, CONSTRAINT_BUS26M_ALLOW |
  163. (IS_PLAT_SUSPEND_ID(state_id) ?
  164. MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND : 0U));
  165. #endif
  166. if (IS_PLAT_SUSPEND_ID(state_id)) {
  167. mt_spm_suspend_enter(state_id,
  168. (MT_SPM_EX_OP_SET_WDT |
  169. MT_SPM_EX_OP_HW_S1_DETECT |
  170. bus26m_ext_opand),
  171. CONSTRAINT_BUS26M_RESOURCE_REQ);
  172. } else {
  173. mt_spm_idle_generic_enter(state_id, MT_SPM_EX_OP_HW_S1_DETECT,
  174. spm_bus26m_conduct);
  175. }
  176. return 0;
  177. }
  178. int spm_reset_rc_bus26m(unsigned int cpu, int state_id)
  179. {
  180. unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT;
  181. (void)cpu;
  182. #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
  183. mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, 0U);
  184. #endif
  185. if (IS_PLAT_SUSPEND_ID(state_id)) {
  186. ext_op |= (bus26m_ext_opand | MT_SPM_EX_OP_SET_WDT);
  187. mt_spm_suspend_resume(state_id, ext_op, NULL);
  188. bus26m_ext_opand = 0U;
  189. } else {
  190. mt_spm_idle_generic_resume(state_id, ext_op, NULL);
  191. status.enter_cnt++;
  192. }
  193. do_irqs_delivery();
  194. return 0;
  195. }