mt_spm_cond.c 5.8 KB

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