mt_spm_rc_bus26m.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2021-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. SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP)
  42. #define CONSTRAINT_BUS26M_PCM_FLAG1 0U
  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. 0xFFFFD408, /* MTCMOS1 */
  50. 0x2284C802, /* INFRA0 */
  51. 0x27AF8000, /* INFRA1 */
  52. 0x86040650, /* INFRA2 */
  53. 0x30038020, /* INFRA3 */
  54. 0x80000000, /* INFRA4 */
  55. 0x00080ABB, /* PERI0 */
  56. 0x00004000, /* VPPSYS0_0 */
  57. 0x08803000, /* VPPSYS0_1 */
  58. 0x00000000, /* VPPSYS0_2 */
  59. 0x80005555, /* VPPSYS1_0 */
  60. 0x00009008, /* VPPSYS1_1 */
  61. 0x60060000, /* VDOSYS0_0 */
  62. 0x00000000, /* VDOSYS0_1 */
  63. 0x201E01F8, /* VDOSYS1_0 */
  64. 0x00800000, /* VDOSYS1_1 */
  65. 0x00000000, /* VDOSYS1_2 */
  66. 0x00000080, /* I2C */
  67. },
  68. .table_pll = (PLL_BIT_UNIVPLL |
  69. PLL_BIT_MFGPLL |
  70. PLL_BIT_MSDCPLL |
  71. PLL_BIT_TVDPLL |
  72. PLL_BIT_MMPLL),
  73. };
  74. static struct mt_spm_cond_tables cond_bus26m_res = {
  75. .table_cg = { 0U },
  76. .table_pll = 0U,
  77. };
  78. static struct constraint_status status = {
  79. .id = MT_RM_CONSTRAINT_ID_BUS26M,
  80. .valid = (MT_SPM_RC_VALID_SW |
  81. MT_SPM_RC_VALID_COND_LATCH),
  82. .cond_block = 0U,
  83. .enter_cnt = 0U,
  84. .cond_res = &cond_bus26m_res,
  85. };
  86. /*
  87. * Cirq will take the place of gic when gic is off.
  88. * However, cirq cannot work if 26m clk is turned off when system idle/suspend.
  89. * Therefore, we need to set irq pending for specific wakeup source.
  90. */
  91. #ifdef ATF_PLAT_CIRQ_UNSUPPORT
  92. #define do_irqs_delivery()
  93. #else
  94. static void mt_spm_irq_remain_dump(struct mt_irqremain *irqs,
  95. unsigned int irq_index,
  96. struct wake_status *wakeup)
  97. {
  98. INFO("[SPM] r12 = 0x%08x(0x%08x), flag = 0x%08x 0x%08x 0x%08x\n",
  99. wakeup->tr.comm.r12, wakeup->md32pcm_wakeup_sta,
  100. wakeup->tr.comm.debug_flag, wakeup->tr.comm.b_sw_flag0,
  101. wakeup->tr.comm.b_sw_flag1);
  102. INFO("irq:%u(0x%08x) set pending\n",
  103. irqs->wakeupsrc[irq_index], irqs->irqs[irq_index]);
  104. }
  105. static void do_irqs_delivery(void)
  106. {
  107. unsigned int idx;
  108. int res = 0;
  109. struct wake_status *wakeup = NULL;
  110. struct mt_irqremain *irqs = refer2remain_irq;
  111. res = spm_conservation_get_result(&wakeup);
  112. if ((res != 0) && (irqs == NULL)) {
  113. return;
  114. }
  115. for (idx = 0U; idx < irqs->count; ++idx) {
  116. if (((wakeup->tr.comm.r12 & irqs->wakeupsrc[idx]) != 0U) ||
  117. ((wakeup->raw_sta & irqs->wakeupsrc[idx]) != 0U)) {
  118. if ((irqs->wakeupsrc_cat[idx] &
  119. MT_IRQ_REMAIN_CAT_LOG) != 0U) {
  120. mt_spm_irq_remain_dump(irqs, idx, wakeup);
  121. }
  122. mt_irq_set_pending(irqs->irqs[idx]);
  123. }
  124. }
  125. }
  126. #endif
  127. static void spm_bus26m_conduct(struct spm_lp_scen *spm_lp,
  128. unsigned int *resource_req)
  129. {
  130. spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG;
  131. spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG1;
  132. *resource_req |= CONSTRAINT_BUS26M_RESOURCE_REQ;
  133. }
  134. bool spm_is_valid_rc_bus26m(unsigned int cpu, int state_id)
  135. {
  136. (void)cpu;
  137. (void)state_id;
  138. return (status.cond_block == 0U) && IS_MT_RM_RC_READY(status.valid);
  139. }
  140. int spm_update_rc_bus26m(int state_id, int type, const void *val)
  141. {
  142. const struct mt_spm_cond_tables *tlb;
  143. const struct mt_spm_cond_tables *tlb_check;
  144. int res = MT_RM_STATUS_OK;
  145. if (val == NULL) {
  146. return MT_RM_STATUS_BAD;
  147. }
  148. if (type == PLAT_RC_UPDATE_CONDITION) {
  149. tlb = (const struct mt_spm_cond_tables *)val;
  150. tlb_check = (const struct mt_spm_cond_tables *)&cond_bus26m;
  151. status.cond_block =
  152. mt_spm_cond_check(state_id, tlb, tlb_check,
  153. ((status.valid &
  154. MT_SPM_RC_VALID_COND_LATCH) != 0U) ?
  155. &cond_bus26m_res : NULL);
  156. } else if (type == PLAT_RC_UPDATE_REMAIN_IRQS) {
  157. refer2remain_irq = (struct mt_irqremain *)val;
  158. } else {
  159. res = MT_RM_STATUS_BAD;
  160. }
  161. return res;
  162. }
  163. unsigned int spm_allow_rc_bus26m(int state_id)
  164. {
  165. (void)state_id;
  166. return CONSTRAINT_BUS26M_ALLOW;
  167. }
  168. int spm_run_rc_bus26m(unsigned int cpu, int state_id)
  169. {
  170. (void)cpu;
  171. #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
  172. mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, CONSTRAINT_BUS26M_ALLOW |
  173. (IS_PLAT_SUSPEND_ID(state_id) ?
  174. MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND : 0U));
  175. #endif
  176. if (IS_PLAT_SUSPEND_ID(state_id)) {
  177. mt_spm_suspend_enter(state_id,
  178. (MT_SPM_EX_OP_SET_WDT |
  179. MT_SPM_EX_OP_HW_S1_DETECT |
  180. MT_SPM_EX_OP_SET_SUSPEND_MODE |
  181. bus26m_ext_opand),
  182. CONSTRAINT_BUS26M_RESOURCE_REQ);
  183. } else {
  184. mt_spm_idle_generic_enter(state_id, MT_SPM_EX_OP_HW_S1_DETECT,
  185. spm_bus26m_conduct);
  186. }
  187. return 0;
  188. }
  189. int spm_reset_rc_bus26m(unsigned int cpu, int state_id)
  190. {
  191. unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT;
  192. (void)cpu;
  193. #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
  194. mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, 0U);
  195. #endif
  196. if (IS_PLAT_SUSPEND_ID(state_id)) {
  197. ext_op |= (bus26m_ext_opand | MT_SPM_EX_OP_SET_WDT);
  198. mt_spm_suspend_resume(state_id, ext_op, NULL);
  199. bus26m_ext_opand = 0U;
  200. } else {
  201. mt_spm_idle_generic_resume(state_id, ext_op, NULL);
  202. status.enter_cnt++;
  203. }
  204. do_irqs_delivery();
  205. return 0;
  206. }