mt_spm_cond.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2021-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_SPM_REG(ofs) (SPM_BASE + ofs)
  17. #define MT_LP_TZ_TOPCK_REG(ofs) (TOPCKGEN_BASE + ofs)
  18. #define MT_LP_TZ_APMIXEDSYS(ofs) (APMIXEDSYS + ofs)
  19. #define MT_LP_TZ_VPPSYS0_REG(ofs) (VPPSYS0_BASE + ofs)
  20. #define MT_LP_TZ_VPPSYS1_REG(ofs) (VPPSYS1_BASE + ofs)
  21. #define MT_LP_TZ_VDOSYS0_REG(ofs) (VDOSYS0_BASE + ofs)
  22. #define MT_LP_TZ_VDOSYS1_REG(ofs) (VDOSYS1_BASE + ofs)
  23. #define MT_LP_TZ_PERI_AO_REG(ofs) (PERICFG_AO_BASE + ofs)
  24. #define SPM_PWR_STATUS MT_LP_TZ_SPM_REG(0x016C)
  25. #define SPM_PWR_STATUS_2ND MT_LP_TZ_SPM_REG(0x0170)
  26. #define INFRA_SW_CG0 MT_LP_TZ_INFRA_REG(0x0094)
  27. #define INFRA_SW_CG1 MT_LP_TZ_INFRA_REG(0x0090)
  28. #define INFRA_SW_CG2 MT_LP_TZ_INFRA_REG(0x00AC)
  29. #define INFRA_SW_CG3 MT_LP_TZ_INFRA_REG(0x00C8)
  30. #define INFRA_SW_CG4 MT_LP_TZ_INFRA_REG(0x00E8)
  31. #define TOP_SW_I2C_CG MT_LP_TZ_TOPCK_REG(0x00BC)
  32. #define PERI_SW_CG0 MT_LP_TZ_PERI_AO_REG(0x0018)
  33. #define VPPSYS0_SW_CG0 MT_LP_TZ_VPPSYS0_REG(0x0020)
  34. #define VPPSYS0_SW_CG1 MT_LP_TZ_VPPSYS0_REG(0x002C)
  35. #define VPPSYS0_SW_CG2 MT_LP_TZ_VPPSYS0_REG(0x0038)
  36. #define VPPSYS1_SW_CG0 MT_LP_TZ_VPPSYS1_REG(0x0100)
  37. #define VPPSYS1_SW_CG1 MT_LP_TZ_VPPSYS1_REG(0x0110)
  38. #define VDOSYS0_SW_CG0 MT_LP_TZ_VDOSYS0_REG(0x0100)
  39. #define VDOSYS0_SW_CG1 MT_LP_TZ_VDOSYS0_REG(0x0110)
  40. #define VDOSYS1_SW_CG0 MT_LP_TZ_VDOSYS1_REG(0x0100)
  41. #define VDOSYS1_SW_CG1 MT_LP_TZ_VDOSYS1_REG(0x0120)
  42. #define VDOSYS1_SW_CG2 MT_LP_TZ_VDOSYS1_REG(0x0130)
  43. /***********************************************************
  44. * Check clkmux registers
  45. ***********************************************************/
  46. #define CLK_CFG(id) MT_LP_TZ_TOPCK_REG(0x98 + id * 0x10)
  47. #define PDN_CHECK BIT(7)
  48. #define CLK_CHECK BIT(31)
  49. enum {
  50. CLKMUX_DISP = 0,
  51. NF_CLKMUX,
  52. };
  53. static bool is_clkmux_pdn(unsigned int clkmux_id)
  54. {
  55. unsigned int reg, val, idx;
  56. if ((clkmux_id & CLK_CHECK) != 0U) {
  57. clkmux_id = (clkmux_id & ~CLK_CHECK);
  58. reg = clkmux_id / 4U;
  59. val = mmio_read_32(CLK_CFG(reg));
  60. idx = clkmux_id % 4U;
  61. val = (val >> (idx * 8U)) & PDN_CHECK;
  62. return (val != 0U);
  63. }
  64. return false;
  65. }
  66. static struct mt_spm_cond_tables spm_cond_t;
  67. struct idle_cond_info {
  68. unsigned int subsys_mask;
  69. uintptr_t addr;
  70. bool bBitflip;
  71. unsigned int clkmux_id;
  72. };
  73. #define IDLE_CG(mask, addr, bitflip, clkmux) \
  74. {mask, (uintptr_t)addr, bitflip, clkmux}
  75. static struct idle_cond_info idle_cg_info[PLAT_SPM_COND_MAX] = {
  76. IDLE_CG(0xffffffff, SPM_PWR_STATUS, false, 0U),
  77. IDLE_CG(0xffffffff, INFRA_SW_CG0, true, 0U),
  78. IDLE_CG(0xffffffff, INFRA_SW_CG1, true, 0U),
  79. IDLE_CG(0xffffffff, INFRA_SW_CG2, true, 0U),
  80. IDLE_CG(0xffffffff, INFRA_SW_CG3, true, 0U),
  81. IDLE_CG(0xffffffff, INFRA_SW_CG4, true, 0U),
  82. IDLE_CG(0xffffffff, PERI_SW_CG0, true, 0U),
  83. IDLE_CG(0x00000800, VPPSYS0_SW_CG0, true, (CLK_CHECK|CLKMUX_DISP)),
  84. IDLE_CG(0x00000800, VPPSYS0_SW_CG1, true, (CLK_CHECK|CLKMUX_DISP)),
  85. IDLE_CG(0x00000800, VPPSYS0_SW_CG2, true, (CLK_CHECK|CLKMUX_DISP)),
  86. IDLE_CG(0x00001000, VPPSYS1_SW_CG0, true, (CLK_CHECK|CLKMUX_DISP)),
  87. IDLE_CG(0x00001000, VPPSYS1_SW_CG1, true, (CLK_CHECK|CLKMUX_DISP)),
  88. IDLE_CG(0x00002000, VDOSYS0_SW_CG0, true, (CLK_CHECK|CLKMUX_DISP)),
  89. IDLE_CG(0x00002000, VDOSYS0_SW_CG1, true, (CLK_CHECK|CLKMUX_DISP)),
  90. IDLE_CG(0x00004000, VDOSYS1_SW_CG0, true, (CLK_CHECK|CLKMUX_DISP)),
  91. IDLE_CG(0x00004000, VDOSYS1_SW_CG1, true, (CLK_CHECK|CLKMUX_DISP)),
  92. IDLE_CG(0x00004000, VDOSYS1_SW_CG2, true, (CLK_CHECK|CLKMUX_DISP)),
  93. IDLE_CG(0x00000080, TOP_SW_I2C_CG, true, (CLK_CHECK|CLKMUX_DISP)),
  94. };
  95. /***********************************************************
  96. * Check pll idle condition
  97. ***********************************************************/
  98. #define PLL_MFGPLL MT_LP_TZ_APMIXEDSYS(0x340)
  99. #define PLL_MMPLL MT_LP_TZ_APMIXEDSYS(0x0E0)
  100. #define PLL_UNIVPLL MT_LP_TZ_APMIXEDSYS(0x1F0)
  101. #define PLL_MSDCPLL MT_LP_TZ_APMIXEDSYS(0x710)
  102. #define PLL_TVDPLL MT_LP_TZ_APMIXEDSYS(0x380)
  103. unsigned int mt_spm_cond_check(int state_id,
  104. const struct mt_spm_cond_tables *src,
  105. const struct mt_spm_cond_tables *dest,
  106. struct mt_spm_cond_tables *res)
  107. {
  108. unsigned int blocked = 0U, i;
  109. bool is_system_suspend = IS_PLAT_SUSPEND_ID(state_id);
  110. if ((src == NULL) || (dest == NULL)) {
  111. return SPM_COND_CHECK_FAIL;
  112. }
  113. for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
  114. if (res != NULL) {
  115. res->table_cg[i] =
  116. (src->table_cg[i] & dest->table_cg[i]);
  117. if (is_system_suspend && (res->table_cg[i] != 0U)) {
  118. INFO("suspend: %s block[%u](0x%lx) = 0x%08x\n",
  119. dest->name, i, idle_cg_info[i].addr,
  120. res->table_cg[i]);
  121. }
  122. if (res->table_cg[i] != 0U) {
  123. blocked |= (1U << i);
  124. }
  125. } else if ((src->table_cg[i] & dest->table_cg[i]) != 0U) {
  126. blocked |= (1U << i);
  127. break;
  128. }
  129. }
  130. if (res != NULL) {
  131. res->table_pll = (src->table_pll & dest->table_pll);
  132. if (res->table_pll != 0U) {
  133. blocked |=
  134. (res->table_pll << SPM_COND_BLOCKED_PLL_IDX) |
  135. SPM_COND_CHECK_BLOCKED_PLL;
  136. }
  137. } else if ((src->table_pll & dest->table_pll) != 0U) {
  138. blocked |= SPM_COND_CHECK_BLOCKED_PLL;
  139. }
  140. if (is_system_suspend && (blocked != 0U)) {
  141. INFO("suspend: %s blocked=0x%08x\n", dest->name, blocked);
  142. }
  143. return blocked;
  144. }
  145. #define IS_MT_SPM_PWR_OFF(mask) \
  146. (((mmio_read_32(SPM_PWR_STATUS) & mask) == 0U) && \
  147. ((mmio_read_32(SPM_PWR_STATUS_2ND) & mask) == 0U))
  148. int mt_spm_cond_update(struct mt_resource_constraint **con, unsigned int num,
  149. int stateid, void *priv)
  150. {
  151. int res;
  152. uint32_t i;
  153. struct mt_resource_constraint *const *rc;
  154. /* read all cg state */
  155. for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
  156. spm_cond_t.table_cg[i] = 0U;
  157. /* check mtcmos, if off set idle_value and clk to 0 disable */
  158. if (IS_MT_SPM_PWR_OFF(idle_cg_info[i].subsys_mask)) {
  159. continue;
  160. }
  161. /* check clkmux */
  162. if (is_clkmux_pdn(idle_cg_info[i].clkmux_id)) {
  163. continue;
  164. }
  165. spm_cond_t.table_cg[i] = idle_cg_info[i].bBitflip ?
  166. ~mmio_read_32(idle_cg_info[i].addr) :
  167. mmio_read_32(idle_cg_info[i].addr);
  168. }
  169. spm_cond_t.table_pll = 0U;
  170. if ((mmio_read_32(PLL_MFGPLL) & 0x200) != 0U) {
  171. spm_cond_t.table_pll |= PLL_BIT_MFGPLL;
  172. }
  173. if ((mmio_read_32(PLL_MMPLL) & 0x200) != 0U) {
  174. spm_cond_t.table_pll |= PLL_BIT_MMPLL;
  175. }
  176. if ((mmio_read_32(PLL_UNIVPLL) & 0x200) != 0U) {
  177. spm_cond_t.table_pll |= PLL_BIT_UNIVPLL;
  178. }
  179. if ((mmio_read_32(PLL_MSDCPLL) & 0x200) != 0U) {
  180. spm_cond_t.table_pll |= PLL_BIT_MSDCPLL;
  181. }
  182. if ((mmio_read_32(PLL_TVDPLL) & 0x200) != 0U) {
  183. spm_cond_t.table_pll |= PLL_BIT_TVDPLL;
  184. }
  185. spm_cond_t.priv = priv;
  186. for (rc = con; *rc != NULL; rc++) {
  187. if (((*rc)->update) == NULL) {
  188. continue;
  189. }
  190. res = (*rc)->update(stateid, PLAT_RC_UPDATE_CONDITION,
  191. (void const *)&spm_cond_t);
  192. if (res != MT_RM_STATUS_OK) {
  193. break;
  194. }
  195. }
  196. return 0;
  197. }