mt_spm_cond.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdbool.h>
  7. #include <lib/mmio.h>
  8. #include <lib/pm/mtk_pm.h>
  9. #include <mt_spm_cond.h>
  10. #include <mt_spm_conservation.h>
  11. #include <mt_spm_constraint.h>
  12. #include <platform_def.h>
  13. #define TOPCKGEB_BASE (IO_PHYS)
  14. #define MT_LP_TZ_INFRA_REG(ofs) (INFRACFG_AO_BASE + ofs)
  15. #define MT_LP_TZ_SPM_REG(ofs) (SPM_BASE + ofs)
  16. #define MT_LP_TZ_TOPCK_REG(ofs) (TOPCKGEB_BASE + ofs)
  17. #define MT_LP_TZ_APMIXEDSYS(ofs) (APMIXEDSYS + ofs)
  18. #define MT_LP_TZ_VPPSYS0_REG(ofs) (VPPSYS0_BASE + ofs)
  19. #define MT_LP_TZ_VPPSYS1_REG(ofs) (VPPSYS1_BASE + ofs)
  20. #define MT_LP_TZ_VDOSYS0_REG(ofs) (VDOSYS0_BASE + ofs)
  21. #define MT_LP_TZ_VDOSYS1_REG(ofs) (VDOSYS1_BASE + ofs)
  22. #define MT_LP_TZ_PERI_AO_REG(ofs) (PERICFG_AO_BASE + ofs)
  23. #undef SPM_PWR_STATUS
  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 SPM_CPU_PWR_STATUS MT_LP_TZ_SPM_REG(0x0174)
  27. #define INFRA_SW_CG0 MT_LP_TZ_INFRA_REG(0x0090)
  28. #define INFRA_SW_CG1 MT_LP_TZ_INFRA_REG(0x0094)
  29. #define INFRA_SW_CG2 MT_LP_TZ_INFRA_REG(0x00AC)
  30. #define INFRA_SW_CG3 MT_LP_TZ_INFRA_REG(0x00C8)
  31. #define INFRA_SW_CG4 MT_LP_TZ_INFRA_REG(0x00E8)
  32. #define TOP_SW_I2C_CG MT_LP_TZ_TOPCK_REG(0x00A4)
  33. #define PERI_SW_CG0 MT_LP_TZ_PERI_AO_REG(0x0018)
  34. #define VPPSYS0_SW_CG0 MT_LP_TZ_VPPSYS0_REG(0x0020)
  35. #define VPPSYS0_SW_CG1 MT_LP_TZ_VPPSYS0_REG(0x002C)
  36. #define VPPSYS0_SW_CG2 MT_LP_TZ_VPPSYS0_REG(0x0038)
  37. #define VPPSYS1_SW_CG0 MT_LP_TZ_VPPSYS1_REG(0x0100)
  38. #define VPPSYS1_SW_CG1 MT_LP_TZ_VPPSYS1_REG(0x0110)
  39. #define VDOSYS0_SW_CG0 MT_LP_TZ_VDOSYS0_REG(0x0100)
  40. #define VDOSYS0_SW_CG1 MT_LP_TZ_VDOSYS0_REG(0x0110)
  41. #define VDOSYS1_SW_CG0 MT_LP_TZ_VDOSYS1_REG(0x0100)
  42. #define VDOSYS1_SW_CG1 MT_LP_TZ_VDOSYS1_REG(0x0120)
  43. #define VDOSYS1_SW_CG2 MT_LP_TZ_VDOSYS1_REG(0x0130)
  44. #define CLK_CFG(id) MT_LP_TZ_TOPCK_REG(0x2c + id * 0xc)
  45. enum {
  46. /* CLK_CFG_0 1000_002c */
  47. CLKMUX_VPP = 0,
  48. NF_CLKMUX,
  49. };
  50. #define CLK_CHECK BIT(31)
  51. static bool check_clkmux_pdn(unsigned int clkmux_id)
  52. {
  53. unsigned int reg, val, idx;
  54. bool ret = false;
  55. if ((clkmux_id & CLK_CHECK) != 0U) {
  56. clkmux_id = (clkmux_id & ~CLK_CHECK);
  57. reg = clkmux_id / 4U;
  58. val = mmio_read_32(CLK_CFG(reg));
  59. idx = clkmux_id % 4U;
  60. ret = (((val >> (idx * 8U)) & 0x80) != 0U);
  61. }
  62. return ret;
  63. }
  64. static struct mt_spm_cond_tables spm_cond_t;
  65. /* local definitions */
  66. struct idle_cond_info {
  67. /* check SPM_PWR_STATUS for bit definition */
  68. unsigned int subsys_mask;
  69. /* cg address */
  70. uintptr_t addr;
  71. /* bitflip value from *addr ? */
  72. bool bBitflip;
  73. /* check clkmux if bit 31 = 1, id is bit[30:0] */
  74. unsigned int clkmux_id;
  75. };
  76. #define IDLE_CG(mask, addr, bitflip, clkmux) {mask, (uintptr_t)addr, bitflip, clkmux}
  77. static struct idle_cond_info idle_cg_info[PLAT_SPM_COND_MAX] = {
  78. IDLE_CG(0xffffffff, SPM_PWR_STATUS, false, 0),
  79. IDLE_CG(0xffffffff, SPM_CPU_PWR_STATUS, false, 0),
  80. IDLE_CG(0xffffffff, INFRA_SW_CG0, true, 0),
  81. IDLE_CG(0xffffffff, INFRA_SW_CG1, true, 0),
  82. IDLE_CG(0xffffffff, INFRA_SW_CG2, true, 0),
  83. IDLE_CG(0xffffffff, INFRA_SW_CG3, true, 0),
  84. IDLE_CG(0xffffffff, INFRA_SW_CG4, true, 0),
  85. IDLE_CG(0xffffffff, PERI_SW_CG0, true, 0),
  86. IDLE_CG(0x00000800, VPPSYS0_SW_CG0, true, (CLK_CHECK | CLKMUX_VPP)),
  87. IDLE_CG(0x00000800, VPPSYS0_SW_CG1, true, (CLK_CHECK | CLKMUX_VPP)),
  88. IDLE_CG(0x00001000, VPPSYS1_SW_CG0, true, (CLK_CHECK | CLKMUX_VPP)),
  89. IDLE_CG(0x00001000, VPPSYS1_SW_CG1, true, (CLK_CHECK | CLKMUX_VPP)),
  90. IDLE_CG(0x00002000, VDOSYS0_SW_CG0, true, (CLK_CHECK | CLKMUX_VPP)),
  91. IDLE_CG(0x00002000, VDOSYS0_SW_CG1, true, (CLK_CHECK | CLKMUX_VPP)),
  92. IDLE_CG(0x00004000, VDOSYS1_SW_CG0, true, (CLK_CHECK | CLKMUX_VPP)),
  93. IDLE_CG(0x00004000, VDOSYS1_SW_CG1, true, (CLK_CHECK | CLKMUX_VPP)),
  94. IDLE_CG(0x00004000, VDOSYS1_SW_CG2, true, (CLK_CHECK | CLKMUX_VPP)),
  95. };
  96. /* check pll idle condition */
  97. #define PLL_MFGPLL MT_LP_TZ_APMIXEDSYS(0x340)
  98. #define PLL_MMPLL MT_LP_TZ_APMIXEDSYS(0x544)
  99. #define PLL_UNIVPLL MT_LP_TZ_APMIXEDSYS(0x504)
  100. #define PLL_MSDCPLL MT_LP_TZ_APMIXEDSYS(0x514)
  101. #define PLL_TVDPLL1 MT_LP_TZ_APMIXEDSYS(0x524)
  102. #define PLL_TVDPLL2 MT_LP_TZ_APMIXEDSYS(0x534)
  103. #define PLL_ETHPLL MT_LP_TZ_APMIXEDSYS(0x44c)
  104. #define PLL_IMGPLL MT_LP_TZ_APMIXEDSYS(0x554)
  105. #define PLL_APLL1 MT_LP_TZ_APMIXEDSYS(0x304)
  106. #define PLL_APLL2 MT_LP_TZ_APMIXEDSYS(0x318)
  107. #define PLL_APLL3 MT_LP_TZ_APMIXEDSYS(0x32c)
  108. #define PLL_APLL4 MT_LP_TZ_APMIXEDSYS(0x404)
  109. #define PLL_APLL5 MT_LP_TZ_APMIXEDSYS(0x418)
  110. unsigned int mt_spm_cond_check(int state_id,
  111. const struct mt_spm_cond_tables *src,
  112. const struct mt_spm_cond_tables *dest,
  113. struct mt_spm_cond_tables *res)
  114. {
  115. unsigned int b_res = 0U;
  116. unsigned int i;
  117. bool is_system_suspend = IS_PLAT_SUSPEND_ID(state_id);
  118. if ((src == NULL) || (dest == NULL)) {
  119. return SPM_COND_CHECK_FAIL;
  120. }
  121. for (i = 0; i < PLAT_SPM_COND_MAX; i++) {
  122. if (res != NULL) {
  123. res->table_cg[i] = (src->table_cg[i] & dest->table_cg[i]);
  124. if (is_system_suspend && ((res->table_cg[i]) != 0U)) {
  125. INFO("suspend: %s block[%u](0x%lx) = 0x%08x\n",
  126. dest->name, i, idle_cg_info[i].addr,
  127. res->table_cg[i]);
  128. }
  129. if ((res->table_cg[i]) != 0U) {
  130. b_res |= BIT(i);
  131. }
  132. } else if ((src->table_cg[i] & dest->table_cg[i]) != 0U) {
  133. b_res |= BIT(i);
  134. break;
  135. }
  136. }
  137. if (res != NULL) {
  138. res->table_pll = (src->table_pll & dest->table_pll);
  139. if ((res->table_pll) != 0U) {
  140. b_res |= (res->table_pll << SPM_COND_BLOCKED_PLL_IDX) |
  141. SPM_COND_CHECK_BLOCKED_PLL;
  142. }
  143. } else if ((src->table_pll & dest->table_pll) != 0U) {
  144. b_res |= SPM_COND_CHECK_BLOCKED_PLL;
  145. }
  146. if (is_system_suspend && ((b_res) != 0U)) {
  147. INFO("suspend: %s total blocked = 0x%08x\n", dest->name, b_res);
  148. }
  149. return b_res;
  150. }
  151. unsigned int mt_spm_dump_all_pll(const struct mt_spm_cond_tables *src,
  152. const struct mt_spm_cond_tables *dest,
  153. struct mt_spm_cond_tables *res)
  154. {
  155. unsigned int b_res = 0U;
  156. if (res != NULL) {
  157. res->table_all_pll = src->table_all_pll;
  158. if ((res->table_all_pll) != 0U) {
  159. b_res |= (res->table_all_pll << SPM_COND_BLOCKED_PLL_IDX) |
  160. SPM_COND_CHECK_BLOCKED_PLL;
  161. }
  162. } else if ((src->table_pll & dest->table_pll) != 0U) {
  163. b_res |= SPM_COND_CHECK_BLOCKED_PLL;
  164. }
  165. return b_res;
  166. }
  167. #define IS_MT_SPM_PWR_OFF(mask) \
  168. (!(mmio_read_32(SPM_PWR_STATUS) & mask) && \
  169. !(mmio_read_32(SPM_PWR_STATUS_2ND) & mask))
  170. int mt_spm_cond_update(struct mt_resource_constraint **con, unsigned int num,
  171. int stateid, void *priv)
  172. {
  173. static const struct {
  174. uintptr_t en_reg;
  175. uint32_t pll_b;
  176. } plls[] = {
  177. { PLL_MFGPLL, PLL_BIT_MFGPLL },
  178. { PLL_MMPLL, PLL_BIT_MMPLL },
  179. { PLL_UNIVPLL, PLL_BIT_UNIVPLL },
  180. { PLL_MSDCPLL, PLL_BIT_MSDCPLL },
  181. { PLL_TVDPLL1, PLL_BIT_TVDPLL1 },
  182. { PLL_TVDPLL2, PLL_BIT_TVDPLL2 },
  183. { PLL_ETHPLL, PLL_BIT_ETHPLL },
  184. { PLL_IMGPLL, PLL_BIT_IMGPLL },
  185. { PLL_APLL1, PLL_BIT_APLL1 },
  186. { PLL_APLL2, PLL_BIT_APLL2 },
  187. { PLL_APLL3, PLL_BIT_APLL3 },
  188. { PLL_APLL4, PLL_BIT_APLL4 },
  189. { PLL_APLL5, PLL_BIT_APLL5 },
  190. };
  191. int res;
  192. unsigned int i;
  193. struct mt_resource_constraint *const *_con;
  194. /* read all cg state */
  195. for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
  196. spm_cond_t.table_cg[i] = 0U;
  197. /* check mtcmos, if off set idle_value and clk to 0 disable */
  198. if (IS_MT_SPM_PWR_OFF(idle_cg_info[i].subsys_mask)) {
  199. continue;
  200. }
  201. /* check clkmux */
  202. if (check_clkmux_pdn(idle_cg_info[i].clkmux_id)) {
  203. continue;
  204. }
  205. spm_cond_t.table_cg[i] = idle_cg_info[i].bBitflip ?
  206. ~mmio_read_32(idle_cg_info[i].addr) :
  207. mmio_read_32(idle_cg_info[i].addr);
  208. }
  209. spm_cond_t.table_pll = 0U;
  210. for (i = 0U; i < ARRAY_SIZE(plls); i++) {
  211. if ((mmio_read_32(plls[i].en_reg) & BIT(9)) != 0U) {
  212. spm_cond_t.table_pll |= plls[i].pll_b;
  213. }
  214. }
  215. spm_cond_t.priv = priv;
  216. for (i = 0U, _con = con; (*_con != NULL) && (i < num); _con++, i++) {
  217. if ((*_con)->update == NULL) {
  218. continue;
  219. }
  220. res = (*_con)->update(stateid, PLAT_RC_UPDATE_CONDITION,
  221. (void const *)&spm_cond_t);
  222. if (res != MT_RM_STATUS_OK) {
  223. break;
  224. }
  225. }
  226. return 0;
  227. }