mt_spm_cond.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2022-2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdbool.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include <mt_spm_cond.h>
  10. #include <mt_spm_conservation.h>
  11. #include <mt_spm_constraint.h>
  12. #include <plat_mtk_lpm.h>
  13. #include <plat_pm.h>
  14. #include <platform_def.h>
  15. #define MT_LP_TZ_INFRA_REG(ofs) (INFRACFG_AO_BASE + ofs)
  16. #define MT_LP_TZ_MM_REG(ofs) (MMSYS_BASE + ofs)
  17. #define MT_LP_TZ_MDP_REG(ofs) (MDPSYS_BASE + ofs)
  18. #define MT_LP_TZ_SPM_REG(ofs) (SPM_BASE + ofs)
  19. #define MT_LP_TZ_TOPCK_REG(ofs) (TOPCKGEN_BASE + ofs)
  20. #define MT_LP_TZ_APMIXEDSYS(ofs) (APMIXEDSYS + ofs)
  21. #define SPM_PWR_STATUS MT_LP_TZ_SPM_REG(0x016C)
  22. #define SPM_PWR_STATUS_2ND MT_LP_TZ_SPM_REG(0x0170)
  23. #define INFRA_SW_CG0 MT_LP_TZ_INFRA_REG(0x0090)
  24. #define INFRA_SW_CG1 MT_LP_TZ_INFRA_REG(0x0094)
  25. #define INFRA_SW_CG2 MT_LP_TZ_INFRA_REG(0x00AC)
  26. #define INFRA_SW_CG3 MT_LP_TZ_INFRA_REG(0x00C8)
  27. #define INFRA_SW_CG4 MT_LP_TZ_INFRA_REG(0x00E8)
  28. #define INFRA_SW_CG5 MT_LP_TZ_INFRA_REG(0x00D8)
  29. #define MMSYS_CG_CON0 MT_LP_TZ_MM_REG(0x100)
  30. #define MMSYS_CG_CON1 MT_LP_TZ_MM_REG(0x110)
  31. #define MMSYS_CG_CON2 MT_LP_TZ_MM_REG(0x1A0)
  32. #define MMSYS_CG_CON3 MT_LP_TZ_MDP_REG(0x100)
  33. /* Check clkmux registers */
  34. #define CLK_CFG(id) MT_LP_TZ_TOPCK_REG(0xe0 + id * 0x10)
  35. #define CLK_CHECK BIT(31)
  36. enum {
  37. CLKMUX_DISP = 0,
  38. CLKMUX_MDP = 1,
  39. CLKMUX_IMG1 = 2,
  40. CLKMUX_IMG2 = 3,
  41. NF_CLKMUX = 4,
  42. };
  43. static bool is_clkmux_pdn(unsigned int clkmux_id)
  44. {
  45. unsigned int reg, val, idx;
  46. bool ret = false;
  47. if (clkmux_id & CLK_CHECK) {
  48. clkmux_id = (clkmux_id & ~CLK_CHECK);
  49. reg = clkmux_id / 4U;
  50. val = mmio_read_32(CLK_CFG(reg));
  51. idx = clkmux_id % 4U;
  52. ret = (((val >> (idx * 8U)) & 0x80) != 0U);
  53. }
  54. return ret;
  55. }
  56. static struct mt_spm_cond_tables spm_cond_t;
  57. struct idle_cond_info {
  58. unsigned int subsys_mask;
  59. uintptr_t addr;
  60. bool bit_flip;
  61. unsigned int clkmux_id;
  62. };
  63. #define IDLE_CG(mask, addr, bitflip, clkmux) \
  64. {mask, (uintptr_t)addr, bitflip, clkmux}
  65. static struct idle_cond_info idle_cg_info[PLAT_SPM_COND_MAX] = {
  66. IDLE_CG(0xffffffff, SPM_PWR_STATUS, false, 0U),
  67. IDLE_CG(0x00000200, INFRA_SW_CG0, true, 0U),
  68. IDLE_CG(0x00000200, INFRA_SW_CG1, true, 0U),
  69. IDLE_CG(0x00000200, INFRA_SW_CG2, true, 0U),
  70. IDLE_CG(0x00000200, INFRA_SW_CG3, true, 0U),
  71. IDLE_CG(0x00000200, INFRA_SW_CG4, true, 0U),
  72. IDLE_CG(0x00000200, INFRA_SW_CG5, true, 0U),
  73. IDLE_CG(0x00200000, MMSYS_CG_CON0, true, (CLK_CHECK | CLKMUX_DISP)),
  74. IDLE_CG(0x00200000, MMSYS_CG_CON1, true, (CLK_CHECK | CLKMUX_DISP)),
  75. IDLE_CG(0x00200000, MMSYS_CG_CON2, true, (CLK_CHECK | CLKMUX_DISP)),
  76. IDLE_CG(0x00200000, MMSYS_CG_CON3, true, (CLK_CHECK | CLKMUX_MDP)),
  77. };
  78. /* Check pll idle condition */
  79. #define PLL_MFGPLL MT_LP_TZ_APMIXEDSYS(0x314)
  80. #define PLL_MMPLL MT_LP_TZ_APMIXEDSYS(0x254)
  81. #define PLL_UNIVPLL MT_LP_TZ_APMIXEDSYS(0x324)
  82. #define PLL_MSDCPLL MT_LP_TZ_APMIXEDSYS(0x38c)
  83. #define PLL_TVDPLL MT_LP_TZ_APMIXEDSYS(0x264)
  84. unsigned int mt_spm_cond_check(int state_id,
  85. const struct mt_spm_cond_tables *src,
  86. const struct mt_spm_cond_tables *dest,
  87. struct mt_spm_cond_tables *res)
  88. {
  89. unsigned int blocked = 0U;
  90. unsigned int i;
  91. bool is_system_suspend = IS_PLAT_SUSPEND_ID(state_id);
  92. if ((src == NULL) || (dest == NULL)) {
  93. blocked = SPM_COND_CHECK_FAIL;
  94. } else {
  95. for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
  96. if (res != NULL) {
  97. res->table_cg[i] = (src->table_cg[i] & dest->table_cg[i]);
  98. if (is_system_suspend && ((res->table_cg[i]) != 0U)) {
  99. INFO("suspend: %s block[%u](0x%lx) = 0x%08x\n",
  100. dest->name, i, idle_cg_info[i].addr,
  101. res->table_cg[i]);
  102. }
  103. if ((res->table_cg[i]) != 0U) {
  104. blocked |= BIT(i);
  105. }
  106. } else if ((src->table_cg[i] & dest->table_cg[i]) != 0U) {
  107. blocked |= BIT(i);
  108. break;
  109. }
  110. }
  111. if (res != NULL) {
  112. res->table_pll = (src->table_pll & dest->table_pll);
  113. if (res->table_pll != 0U) {
  114. blocked |= (res->table_pll << SPM_COND_BLOCKED_PLL_IDX) |
  115. SPM_COND_CHECK_BLOCKED_PLL;
  116. }
  117. } else if ((src->table_pll & dest->table_pll) != 0U) {
  118. blocked |= SPM_COND_CHECK_BLOCKED_PLL;
  119. }
  120. if (is_system_suspend && ((blocked) != 0U)) {
  121. INFO("suspend: %s total blocked = 0x%08x\n", dest->name, blocked);
  122. }
  123. }
  124. return blocked;
  125. }
  126. #define IS_MT_SPM_PWR_OFF(mask) \
  127. (((mmio_read_32(SPM_PWR_STATUS) & mask) == 0U) && \
  128. ((mmio_read_32(SPM_PWR_STATUS_2ND) & mask) == 0U))
  129. int mt_spm_cond_update(struct mt_resource_constraint **con, unsigned int num,
  130. int stateid, void *priv)
  131. {
  132. int res;
  133. uint32_t i;
  134. struct mt_resource_constraint *const *rc;
  135. /* read all cg state */
  136. for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
  137. spm_cond_t.table_cg[i] = 0U;
  138. /* check mtcmos, if off set idle_value and clk to 0 disable */
  139. if (IS_MT_SPM_PWR_OFF(idle_cg_info[i].subsys_mask)) {
  140. continue;
  141. }
  142. /* check clkmux */
  143. if (is_clkmux_pdn(idle_cg_info[i].clkmux_id)) {
  144. continue;
  145. }
  146. spm_cond_t.table_cg[i] = idle_cg_info[i].bit_flip ?
  147. ~mmio_read_32(idle_cg_info[i].addr) :
  148. mmio_read_32(idle_cg_info[i].addr);
  149. }
  150. spm_cond_t.table_pll = 0U;
  151. if ((mmio_read_32(PLL_MFGPLL) & 0x1) != 0U) {
  152. spm_cond_t.table_pll |= PLL_BIT_MFGPLL;
  153. }
  154. if ((mmio_read_32(PLL_MMPLL) & 0x1) != 0U) {
  155. spm_cond_t.table_pll |= PLL_BIT_MMPLL;
  156. }
  157. if ((mmio_read_32(PLL_UNIVPLL) & 0x1) != 0U) {
  158. spm_cond_t.table_pll |= PLL_BIT_UNIVPLL;
  159. }
  160. if ((mmio_read_32(PLL_MSDCPLL) & 0x1) != 0U) {
  161. spm_cond_t.table_pll |= PLL_BIT_MSDCPLL;
  162. }
  163. if ((mmio_read_32(PLL_TVDPLL) & 0x1) != 0U) {
  164. spm_cond_t.table_pll |= PLL_BIT_TVDPLL;
  165. }
  166. spm_cond_t.priv = priv;
  167. for (rc = con; *rc != NULL; rc++) {
  168. if (((*rc)->update) == NULL) {
  169. continue;
  170. }
  171. res = (*rc)->update(stateid, PLAT_RC_UPDATE_CONDITION,
  172. (void const *)&spm_cond_t);
  173. if (res != MT_RM_STATUS_OK) {
  174. break;
  175. }
  176. }
  177. return 0;
  178. }