mt_spm_rc_bus26m.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (c) 2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #ifndef MTK_PLAT_CIRQ_UNSUPPORT
  8. #include <mtk_cirq.h>
  9. #endif
  10. #include <drivers/spm/mt_spm_resource_req.h>
  11. #include <lib/pm/mtk_pm.h>
  12. #include <lpm/mt_lp_rm.h>
  13. #include <mt_spm.h>
  14. #include <mt_spm_cond.h>
  15. #include <mt_spm_conservation.h>
  16. #include <mt_spm_constraint.h>
  17. #include <mt_spm_idle.h>
  18. #include <mt_spm_internal.h>
  19. #include <mt_spm_notifier.h>
  20. #include "mt_spm_rc_api.h"
  21. #include "mt_spm_rc_internal.h"
  22. #include <mt_spm_reg.h>
  23. #include <mt_spm_suspend.h>
  24. #define CONSTRAINT_BUS26M_ALLOW (MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF | \
  25. MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \
  26. MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \
  27. MT_RM_CONSTRAINT_ALLOW_VCORE_LP | \
  28. MT_RM_CONSTRAINT_ALLOW_LVTS_STATE | \
  29. MT_RM_CONSTRAINT_ALLOW_BUS26M_OFF)
  30. #define CONSTRAINT_BUS26M_PCM_FLAG (SPM_FLAG_DISABLE_INFRA_PDN | \
  31. SPM_FLAG_DISABLE_VCORE_DVS | \
  32. SPM_FLAG_DISABLE_VCORE_DFS | \
  33. SPM_FLAG_SRAM_SLEEP_CTRL | \
  34. SPM_FLAG_ENABLE_LVTS_WORKAROUND | \
  35. SPM_FLAG_KEEP_CSYSPWRACK_HIGH | \
  36. SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP)
  37. #define CONSTRAINT_BUS26M_PCM_FLAG1 (SPM_FLAG1_DISABLE_PWRAP_CLK_SWITCH)
  38. /* If sspm sram won't enter sleep voltage then vcore couldn't enter low power mode */
  39. #if defined(MTK_PLAT_SPM_SRAM_SLP_UNSUPPORT) && SPM_SRAM_SLEEP_RC_RES_RESTRICT
  40. #define CONSTRAINT_BUS26M_RESOURCE_REQ (MT_SPM_26M)
  41. #else
  42. #define CONSTRAINT_BUS26M_RESOURCE_REQ (0)
  43. #endif
  44. static unsigned int bus26m_ext_opand;
  45. static unsigned int bus26m_ext_opand2;
  46. static struct mt_irqremain *refer2remain_irq;
  47. static struct mt_spm_cond_tables cond_bus26m = {
  48. .name = "bus26m",
  49. .table_cg = {
  50. 0xFF5DD002, /* MTCMOS1 */
  51. 0x0000003C, /* MTCMOS2 */
  52. 0x27AF8000, /* INFRA0 */
  53. 0x22010876, /* INFRA1 */
  54. 0x86000650, /* INFRA2 */
  55. 0x30008020, /* INFRA3 */
  56. 0x80000000, /* INFRA4 */
  57. 0x01002A3B, /* PERI0 */
  58. 0x00090000, /* VPPSYS0_0 */
  59. 0x38FF3E69, /* VPPSYS0_1 */
  60. 0xF0081450, /* VPPSYS1_0 */
  61. 0x00003000, /* VPPSYS1_1 */
  62. 0x00000000, /* VDOSYS0_0 */
  63. 0x00000000, /* VDOSYS0_1 */
  64. 0x000001FF, /* VDOSYS1_0 */
  65. 0x000001E0, /* VDOSYS1_1 */
  66. 0x00FB0007, /* VDOSYS1_2 */
  67. },
  68. .table_pll = (PLL_BIT_UNIVPLL |
  69. PLL_BIT_MFGPLL |
  70. PLL_BIT_MSDCPLL |
  71. PLL_BIT_TVDPLL1 |
  72. PLL_BIT_TVDPLL2 |
  73. PLL_BIT_MMPLL |
  74. PLL_BIT_ETHPLL |
  75. PLL_BIT_IMGPLL |
  76. PLL_BIT_APLL1 |
  77. PLL_BIT_APLL2 |
  78. PLL_BIT_APLL3 |
  79. PLL_BIT_APLL4 |
  80. PLL_BIT_APLL5),
  81. };
  82. static struct mt_spm_cond_tables cond_bus26m_res = {
  83. .table_cg = { 0U },
  84. .table_pll = 0U,
  85. };
  86. static struct constraint_status status = {
  87. .id = MT_RM_CONSTRAINT_ID_BUS26M,
  88. .is_valid = (MT_SPM_RC_VALID_SW |
  89. MT_SPM_RC_VALID_COND_CHECK |
  90. MT_SPM_RC_VALID_COND_LATCH |
  91. MT_SPM_RC_VALID_TRACE_TIME),
  92. .is_cond_block = 0U,
  93. .enter_cnt = 0U,
  94. .all_pll_dump = 0U,
  95. .cond_res = &cond_bus26m_res,
  96. .residency = 0ULL,
  97. };
  98. #ifdef MTK_PLAT_CIRQ_UNSUPPORT
  99. #define do_irqs_delivery()
  100. #else
  101. static void mt_spm_irq_remain_dump(struct mt_irqremain *irqs,
  102. unsigned int irq_index,
  103. struct wake_status *wakeup)
  104. {
  105. if ((irqs == NULL) || (wakeup == NULL)) {
  106. return;
  107. }
  108. INFO("[SPM] r12=0x%08x(0x%08x), flag=0x%08x 0x%08x 0x%08x, irq:%u(0x%08x) set pending\n",
  109. wakeup->tr.comm.r12,
  110. wakeup->md32pcm_wakeup_sta,
  111. wakeup->tr.comm.debug_flag,
  112. wakeup->tr.comm.b_sw_flag0,
  113. wakeup->tr.comm.b_sw_flag1,
  114. irqs->wakeupsrc[irq_index],
  115. irqs->irqs[irq_index]);
  116. }
  117. static void do_irqs_delivery(void)
  118. {
  119. unsigned int idx;
  120. struct wake_status *wakeup = NULL;
  121. struct mt_irqremain *irqs = refer2remain_irq;
  122. if (irqs == NULL) {
  123. return;
  124. }
  125. if (spm_conservation_get_result(&wakeup) == 0) {
  126. if (wakeup != NULL) {
  127. for (idx = 0; idx < irqs->count; idx++) {
  128. if (((wakeup->tr.comm.r12 & irqs->wakeupsrc[idx]) != 0U) ||
  129. ((wakeup->tr.comm.raw_sta & irqs->wakeupsrc[idx]) != 0U)) {
  130. if ((irqs->wakeupsrc_cat[idx] &
  131. MT_IRQ_REMAIN_CAT_LOG) != 0U) {
  132. mt_spm_irq_remain_dump(irqs, idx, wakeup);
  133. }
  134. mt_irq_set_pending(irqs->irqs[idx]);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. #endif
  141. int spm_bus26m_conduct(int state_id, struct spm_lp_scen *spm_lp, unsigned int *resource_req)
  142. {
  143. unsigned int res_req = CONSTRAINT_BUS26M_RESOURCE_REQ;
  144. if ((spm_lp == NULL) || (resource_req == NULL)) {
  145. return -1;
  146. }
  147. spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG;
  148. spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG1;
  149. *resource_req |= res_req;
  150. return 0;
  151. }
  152. bool spm_is_valid_rc_bus26m(unsigned int cpu, int state_id)
  153. {
  154. return (!(status.is_cond_block && (status.is_valid & MT_SPM_RC_VALID_COND_CHECK) > 0) &&
  155. IS_MT_RM_RC_READY(status.is_valid) &&
  156. (IS_PLAT_SUSPEND_ID(state_id) || (state_id == MT_PLAT_PWR_STATE_SYSTEM_BUS)));
  157. }
  158. static int update_rc_condition(int state_id, const void *val)
  159. {
  160. const struct mt_spm_cond_tables *tlb = (const struct mt_spm_cond_tables *)val;
  161. const struct mt_spm_cond_tables *tlb_check =
  162. (const struct mt_spm_cond_tables *)&cond_bus26m;
  163. if (tlb == NULL) {
  164. return MT_RM_STATUS_BAD;
  165. }
  166. status.is_cond_block = mt_spm_cond_check(state_id, tlb, tlb_check,
  167. (status.is_valid & MT_SPM_RC_VALID_COND_LATCH) ?
  168. &cond_bus26m_res : NULL);
  169. status.all_pll_dump = mt_spm_dump_all_pll(tlb, tlb_check,
  170. (status.is_valid & MT_SPM_RC_VALID_COND_LATCH) ?
  171. &cond_bus26m_res : NULL);
  172. return MT_RM_STATUS_OK;
  173. }
  174. static void update_rc_remain_irqs(const void *val)
  175. {
  176. refer2remain_irq = (struct mt_irqremain *)val;
  177. }
  178. static void update_rc_fmaudio_adsp(int type, const void *val)
  179. {
  180. int *flag = (int *)val;
  181. unsigned int ext_op = (type == PLAT_RC_IS_ADSP) ?
  182. (MT_SPM_EX_OP_SET_IS_ADSP | MT_SPM_EX_OP_SET_SUSPEND_MODE) :
  183. MT_SPM_EX_OP_SET_SUSPEND_MODE;
  184. if (flag == NULL) {
  185. return;
  186. }
  187. if (*flag != 0) {
  188. SPM_RC_BITS_SET(bus26m_ext_opand, ext_op);
  189. } else {
  190. SPM_RC_BITS_CLR(bus26m_ext_opand, ext_op);
  191. }
  192. }
  193. static void update_rc_usb_peri(const void *val)
  194. {
  195. int *flag = (int *)val;
  196. if (flag == NULL) {
  197. return;
  198. }
  199. if (*flag != 0) {
  200. SPM_RC_BITS_SET(bus26m_ext_opand2, MT_SPM_EX_OP_PERI_ON);
  201. } else {
  202. SPM_RC_BITS_CLR(bus26m_ext_opand2, MT_SPM_EX_OP_PERI_ON);
  203. }
  204. }
  205. static void update_rc_usb_infra(const void *val)
  206. {
  207. int *flag = (int *)val;
  208. if (flag == NULL) {
  209. return;
  210. }
  211. if (*flag != 0) {
  212. SPM_RC_BITS_SET(bus26m_ext_opand2, MT_SPM_EX_OP_INFRA_ON);
  213. } else {
  214. SPM_RC_BITS_CLR(bus26m_ext_opand2, MT_SPM_EX_OP_INFRA_ON);
  215. }
  216. }
  217. static void update_rc_status(const void *val)
  218. {
  219. const struct rc_common_state *st;
  220. st = (const struct rc_common_state *)val;
  221. if (st == NULL) {
  222. return;
  223. }
  224. if (st->type == CONSTRAINT_UPDATE_COND_CHECK) {
  225. struct mt_spm_cond_tables * const tlb = &cond_bus26m;
  226. spm_rc_condition_modifier(st->id, st->act, st->value,
  227. MT_RM_CONSTRAINT_ID_BUS26M, tlb);
  228. } else if ((st->type == CONSTRAINT_UPDATE_VALID) ||
  229. (st->type == CONSTRAINT_RESIDNECY)) {
  230. spm_rc_constraint_status_set(st->id, st->type, st->act,
  231. MT_RM_CONSTRAINT_ID_BUS26M,
  232. (struct constraint_status * const)st->value,
  233. (struct constraint_status * const)&status);
  234. } else {
  235. INFO("[%s:%d] - Unknown type: 0x%x\n", __func__, __LINE__, st->type);
  236. }
  237. }
  238. int spm_update_rc_bus26m(int state_id, int type, const void *val)
  239. {
  240. int res = MT_RM_STATUS_OK;
  241. switch (type) {
  242. case PLAT_RC_UPDATE_CONDITION:
  243. res = update_rc_condition(state_id, val);
  244. break;
  245. case PLAT_RC_UPDATE_REMAIN_IRQS:
  246. update_rc_remain_irqs(val);
  247. break;
  248. case PLAT_RC_IS_FMAUDIO:
  249. case PLAT_RC_IS_ADSP:
  250. update_rc_fmaudio_adsp(type, val);
  251. break;
  252. case PLAT_RC_IS_USB_PERI:
  253. update_rc_usb_peri(val);
  254. break;
  255. case PLAT_RC_IS_USB_INFRA:
  256. update_rc_usb_infra(val);
  257. break;
  258. case PLAT_RC_STATUS:
  259. update_rc_status(val);
  260. break;
  261. default:
  262. INFO("[%s:%d] - Do nothing for type: %d\n", __func__, __LINE__, type);
  263. break;
  264. }
  265. return res;
  266. }
  267. unsigned int spm_allow_rc_bus26m(int state_id)
  268. {
  269. return CONSTRAINT_BUS26M_ALLOW;
  270. }
  271. int spm_run_rc_bus26m(unsigned int cpu, int state_id)
  272. {
  273. unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT;
  274. #ifndef MTK_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
  275. mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, CONSTRAINT_BUS26M_ALLOW |
  276. (IS_PLAT_SUSPEND_ID(state_id) ?
  277. MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND : 0));
  278. #endif
  279. if (status.is_valid & MT_SPM_RC_VALID_TRACE_TIME) {
  280. ext_op |= MT_SPM_EX_OP_TRACE_TIMESTAMP_EN;
  281. }
  282. if (IS_PLAT_SUSPEND_ID(state_id)) {
  283. mt_spm_suspend_enter(state_id,
  284. (MT_SPM_EX_OP_CLR_26M_RECORD |
  285. MT_SPM_EX_OP_SET_WDT |
  286. MT_SPM_EX_OP_HW_S1_DETECT |
  287. bus26m_ext_opand |
  288. bus26m_ext_opand2),
  289. CONSTRAINT_BUS26M_RESOURCE_REQ);
  290. } else {
  291. mt_spm_idle_generic_enter(state_id, ext_op, spm_bus26m_conduct);
  292. }
  293. return MT_RM_STATUS_OK;
  294. }
  295. int spm_reset_rc_bus26m(unsigned int cpu, int state_id)
  296. {
  297. unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT;
  298. #ifndef MTK_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT
  299. mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, 0);
  300. #endif
  301. if (status.is_valid & MT_SPM_RC_VALID_TRACE_TIME) {
  302. ext_op |= MT_SPM_EX_OP_TRACE_TIMESTAMP_EN;
  303. }
  304. if (IS_PLAT_SUSPEND_ID(state_id)) {
  305. mt_spm_suspend_resume(state_id,
  306. (bus26m_ext_opand | bus26m_ext_opand2 |
  307. MT_SPM_EX_OP_SET_WDT | ext_op),
  308. NULL);
  309. bus26m_ext_opand = 0;
  310. } else {
  311. struct wake_status *waken = NULL;
  312. if (spm_unlikely(status.is_valid & MT_SPM_RC_VALID_TRACE_EVENT)) {
  313. ext_op |= MT_SPM_EX_OP_TRACE_LP;
  314. }
  315. mt_spm_idle_generic_resume(state_id, ext_op, &waken, NULL);
  316. status.enter_cnt++;
  317. if (spm_unlikely(status.is_valid & MT_SPM_RC_VALID_RESIDNECY)) {
  318. status.residency += (waken != NULL) ? waken->tr.comm.timer_out : 0;
  319. }
  320. }
  321. do_irqs_delivery();
  322. return MT_RM_STATUS_OK;
  323. }
  324. int spm_get_status_rc_bus26m(unsigned int type, void *priv)
  325. {
  326. int ret = MT_RM_STATUS_OK;
  327. if (type == PLAT_RC_STATUS) {
  328. int res = 0;
  329. struct rc_common_state *st = (struct rc_common_state *)priv;
  330. if (st == NULL) {
  331. return MT_RM_STATUS_BAD;
  332. }
  333. res = spm_rc_constraint_status_get(st->id, st->type,
  334. st->act, MT_RM_CONSTRAINT_ID_BUS26M,
  335. (struct constraint_status * const)&status,
  336. (struct constraint_status * const)st->value);
  337. if ((res == 0) && (st->id != MT_RM_CONSTRAINT_ID_ALL)) {
  338. ret = MT_RM_STATUS_STOP;
  339. }
  340. }
  341. return ret;
  342. }