mt_spm_suspend.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_SPM_UART_UNSUPPORT
  8. #include <drivers/uart.h>
  9. #endif
  10. #include <lib/mmio.h>
  11. #ifndef MTK_PLAT_CIRQ_UNSUPPORT
  12. #include <mtk_cirq.h>
  13. #endif
  14. #include <constraints/mt_spm_rc_internal.h>
  15. #include <drivers/spm/mt_spm_resource_req.h>
  16. #include <lib/pm/mtk_pm.h>
  17. #include <lpm/mt_lp_api.h>
  18. #include <mt_spm.h>
  19. #include <mt_spm_conservation.h>
  20. #include <mt_spm_internal.h>
  21. #include <mt_spm_reg.h>
  22. #include <mt_spm_suspend.h>
  23. #include <pcm_def.h>
  24. #define SPM_SUSPEND_SLEEP_PCM_FLAG \
  25. (SPM_FLAG_DISABLE_INFRA_PDN | \
  26. SPM_FLAG_DISABLE_VCORE_DVS | \
  27. SPM_FLAG_DISABLE_VCORE_DFS | \
  28. SPM_FLAG_KEEP_CSYSPWRACK_HIGH | \
  29. SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP | \
  30. SPM_FLAG_SRAM_SLEEP_CTRL)
  31. #define SPM_SUSPEND_SLEEP_PCM_FLAG1 (SPM_FLAG1_DISABLE_PWRAP_CLK_SWITCH)
  32. #define SPM_SUSPEND_PCM_FLAG \
  33. (SPM_FLAG_DISABLE_VCORE_DVS | \
  34. SPM_FLAG_DISABLE_VCORE_DFS | \
  35. SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP | \
  36. SPM_FLAG_SRAM_SLEEP_CTRL)
  37. #define SPM_SUSPEND_PCM_FLAG1 (SPM_FLAG1_DISABLE_PWRAP_CLK_SWITCH)
  38. /* Suspend spm power control */
  39. #define __WAKE_SRC_FOR_SUSPEND_COMMON__ ( \
  40. (R12_PCM_TIMER) | \
  41. (R12_KP_IRQ_B) | \
  42. (R12_APWDT_EVENT_B) | \
  43. (R12_MSDC_WAKEUP_B) | \
  44. (R12_EINT_EVENT_B) | \
  45. (R12_SBD_INTR_WAKEUP_B) | \
  46. (R12_SSPM2SPM_WAKEUP_B) | \
  47. (R12_SCP2SPM_WAKEUP_B) | \
  48. (R12_ADSP2SPM_WAKEUP_B) | \
  49. (R12_USBX_CDSC_B) | \
  50. (R12_USBX_POWERDWN_B) | \
  51. (R12_SYS_TIMER_EVENT_B) | \
  52. (R12_EINT_EVENT_SECURE_B) | \
  53. (R12_ECE_INT_HDMI_B) | \
  54. (R12_SYS_CIRQ_IRQ_B) | \
  55. (R12_PCIE_WAKEUPEVENT_B) | \
  56. (R12_SPM_CPU_WAKEUPEVENT_B) | \
  57. (R12_APUSYS_WAKE_HOST_B))
  58. #if defined(CFG_MICROTRUST_TEE_SUPPORT)
  59. #define WAKE_SRC_FOR_SUSPEND (__WAKE_SRC_FOR_SUSPEND_COMMON__)
  60. #else
  61. #define WAKE_SRC_FOR_SUSPEND (__WAKE_SRC_FOR_SUSPEND_COMMON__ | R12_SEJ_EVENT_B)
  62. #endif
  63. static struct pwr_ctrl suspend_ctrl = {
  64. .wake_src = WAKE_SRC_FOR_SUSPEND,
  65. /* SPM_AP_STANDBY_CON */
  66. /* [0] */
  67. .reg_wfi_op = 0,
  68. /* [1] */
  69. .reg_wfi_type = 0,
  70. /* [2] */
  71. .reg_mp0_cputop_idle_mask = 0,
  72. /* [3] */
  73. .reg_mp1_cputop_idle_mask = 0,
  74. /* [4] */
  75. .reg_mcusys_idle_mask = 0,
  76. /* [25] */
  77. .reg_md_apsrc_1_sel = 0,
  78. /* [26] */
  79. .reg_md_apsrc_0_sel = 0,
  80. /* [29] */
  81. .reg_conn_apsrc_sel = 0,
  82. /* SPM_SRC_REQ */
  83. /* [0] */
  84. .reg_spm_apsrc_req = 0,
  85. /* [1] */
  86. .reg_spm_f26m_req = 0,
  87. /* [3] */
  88. .reg_spm_infra_req = 0,
  89. /* [4] */
  90. .reg_spm_vrf18_req = 0,
  91. /* [7] */
  92. .reg_spm_ddr_en_req = 0,
  93. /* [8] */
  94. .reg_spm_dvfs_req = 0,
  95. /* [9] */
  96. .reg_spm_sw_mailbox_req = 0,
  97. /* [10] */
  98. .reg_spm_sspm_mailbox_req = 0,
  99. /* [11] */
  100. .reg_spm_adsp_mailbox_req = 0,
  101. /* [12] */
  102. .reg_spm_scp_mailbox_req = 0,
  103. /* SPM_SRC_MASK */
  104. /* [0] */
  105. .reg_sspm_srcclkena_0_mask_b = 1,
  106. /* [1] */
  107. .reg_sspm_infra_req_0_mask_b = 1,
  108. /* [2] */
  109. .reg_sspm_apsrc_req_0_mask_b = 0,
  110. /* [3] */
  111. .reg_sspm_vrf18_req_0_mask_b = 0,
  112. /* [4] */
  113. .reg_sspm_ddr_en_0_mask_b = 0,
  114. /* [5] */
  115. .reg_scp_srcclkena_mask_b = 1,
  116. /* [6] */
  117. .reg_scp_infra_req_mask_b = 1,
  118. /* [7] */
  119. .reg_scp_apsrc_req_mask_b = 1,
  120. /* [8] */
  121. .reg_scp_vrf18_req_mask_b = 1,
  122. /* [9] */
  123. .reg_scp_ddr_en_mask_b = 1,
  124. /* [10] */
  125. .reg_audio_dsp_srcclkena_mask_b = 1,
  126. /* [11] */
  127. .reg_audio_dsp_infra_req_mask_b = 1,
  128. /* [12] */
  129. .reg_audio_dsp_apsrc_req_mask_b = 1,
  130. /* [13] */
  131. .reg_audio_dsp_vrf18_req_mask_b = 1,
  132. /* [14] */
  133. .reg_audio_dsp_ddr_en_mask_b = 1,
  134. /* [15] */
  135. .reg_apu_srcclkena_mask_b = 1,
  136. /* [16] */
  137. .reg_apu_infra_req_mask_b = 1,
  138. /* [17] */
  139. .reg_apu_apsrc_req_mask_b = 0,
  140. /* [18] */
  141. .reg_apu_vrf18_req_mask_b = 1,
  142. /* [19] */
  143. .reg_apu_ddr_en_mask_b = 1,
  144. /* [20] */
  145. .reg_cpueb_srcclkena_mask_b = 1,
  146. /* [21] */
  147. .reg_cpueb_infra_req_mask_b = 1,
  148. /* [22] */
  149. .reg_cpueb_apsrc_req_mask_b = 1,
  150. /* [23] */
  151. .reg_cpueb_vrf18_req_mask_b = 1,
  152. /* [24] */
  153. .reg_cpueb_ddr_en_mask_b = 1,
  154. /* [25] */
  155. .reg_bak_psri_srcclkena_mask_b = 0,
  156. /* [26] */
  157. .reg_bak_psri_infra_req_mask_b = 0,
  158. /* [27] */
  159. .reg_bak_psri_apsrc_req_mask_b = 0,
  160. /* [28] */
  161. .reg_bak_psri_vrf18_req_mask_b = 0,
  162. /* [29] */
  163. .reg_bak_psri_ddr_en_mask_b = 0,
  164. /* [30] */
  165. .reg_cam_ddren_req_mask_b = 0,
  166. /* [31] */
  167. .reg_img_ddren_req_mask_b = 0,
  168. /* SPM_SRC2_MASK */
  169. /* [0] */
  170. .reg_msdc0_srcclkena_mask_b = 1,
  171. /* [1] */
  172. .reg_msdc0_infra_req_mask_b = 1,
  173. /* [2] */
  174. .reg_msdc0_apsrc_req_mask_b = 1,
  175. /* [3] */
  176. .reg_msdc0_vrf18_req_mask_b = 1,
  177. /* [4] */
  178. .reg_msdc0_ddr_en_mask_b = 1,
  179. /* [5] */
  180. .reg_msdc1_srcclkena_mask_b = 1,
  181. /* [6] */
  182. .reg_msdc1_infra_req_mask_b = 1,
  183. /* [7] */
  184. .reg_msdc1_apsrc_req_mask_b = 1,
  185. /* [8] */
  186. .reg_msdc1_vrf18_req_mask_b = 1,
  187. /* [9] */
  188. .reg_msdc1_ddr_en_mask_b = 1,
  189. /* [10] */
  190. .reg_msdc2_srcclkena_mask_b = 1,
  191. /* [11] */
  192. .reg_msdc2_infra_req_mask_b = 1,
  193. /* [12] */
  194. .reg_msdc2_apsrc_req_mask_b = 1,
  195. /* [13] */
  196. .reg_msdc2_vrf18_req_mask_b = 1,
  197. /* [14] */
  198. .reg_msdc2_ddr_en_mask_b = 1,
  199. /* [15] */
  200. .reg_ufs_srcclkena_mask_b = 1,
  201. /* [16] */
  202. .reg_ufs_infra_req_mask_b = 1,
  203. /* [17] */
  204. .reg_ufs_apsrc_req_mask_b = 1,
  205. /* [18] */
  206. .reg_ufs_vrf18_req_mask_b = 1,
  207. /* [19] */
  208. .reg_ufs_ddr_en_mask_b = 1,
  209. /* [20] */
  210. .reg_usb_srcclkena_mask_b = 1,
  211. /* [21] */
  212. .reg_usb_infra_req_mask_b = 1,
  213. /* [22] */
  214. .reg_usb_apsrc_req_mask_b = 1,
  215. /* [23] */
  216. .reg_usb_vrf18_req_mask_b = 1,
  217. /* [24] */
  218. .reg_usb_ddr_en_mask_b = 1,
  219. /* [25] */
  220. .reg_pextp_p0_srcclkena_mask_b = 1,
  221. /* [26] */
  222. .reg_pextp_p0_infra_req_mask_b = 1,
  223. /* [27] */
  224. .reg_pextp_p0_apsrc_req_mask_b = 1,
  225. /* [28] */
  226. .reg_pextp_p0_vrf18_req_mask_b = 1,
  227. /* [29] */
  228. .reg_pextp_p0_ddr_en_mask_b = 1,
  229. /* SPM_SRC3_MASK */
  230. /* [0] */
  231. .reg_pextp_p1_srcclkena_mask_b = 1,
  232. /* [1] */
  233. .reg_pextp_p1_infra_req_mask_b = 1,
  234. /* [2] */
  235. .reg_pextp_p1_apsrc_req_mask_b = 1,
  236. /* [3] */
  237. .reg_pextp_p1_vrf18_req_mask_b = 1,
  238. /* [4] */
  239. .reg_pextp_p1_ddr_en_mask_b = 1,
  240. /* [5] */
  241. .reg_gce0_infra_req_mask_b = 1,
  242. /* [6] */
  243. .reg_gce0_apsrc_req_mask_b = 1,
  244. /* [7] */
  245. .reg_gce0_vrf18_req_mask_b = 1,
  246. /* [8] */
  247. .reg_gce0_ddr_en_mask_b = 1,
  248. /* [9] */
  249. .reg_gce1_infra_req_mask_b = 1,
  250. /* [10] */
  251. .reg_gce1_apsrc_req_mask_b = 1,
  252. /* [11] */
  253. .reg_gce1_vrf18_req_mask_b = 1,
  254. /* [12] */
  255. .reg_gce1_ddr_en_mask_b = 1,
  256. /* [13] */
  257. .reg_spm_srcclkena_reserved_mask_b = 1,
  258. /* [14] */
  259. .reg_spm_infra_req_reserved_mask_b = 1,
  260. /* [15] */
  261. .reg_spm_apsrc_req_reserved_mask_b = 1,
  262. /* [16] */
  263. .reg_spm_vrf18_req_reserved_mask_b = 1,
  264. /* [17] */
  265. .reg_spm_ddr_en_reserved_mask_b = 1,
  266. /* [18] */
  267. .reg_disp0_apsrc_req_mask_b = 1,
  268. /* [19] */
  269. .reg_disp0_ddr_en_mask_b = 1,
  270. /* [20] */
  271. .reg_disp1_apsrc_req_mask_b = 1,
  272. /* [21] */
  273. .reg_disp1_ddr_en_mask_b = 1,
  274. /* [22] */
  275. .reg_disp2_apsrc_req_mask_b = 1,
  276. /* [23] */
  277. .reg_disp2_ddr_en_mask_b = 1,
  278. /* [24] */
  279. .reg_disp3_apsrc_req_mask_b = 1,
  280. /* [25] */
  281. .reg_disp3_ddr_en_mask_b = 1,
  282. /* [26] */
  283. .reg_infrasys_apsrc_req_mask_b = 0,
  284. /* [27] */
  285. .reg_infrasys_ddr_en_mask_b = 1,
  286. /* [28] */
  287. .reg_cg_check_srcclkena_mask_b = 1,
  288. /* [29] */
  289. .reg_cg_check_apsrc_req_mask_b = 1,
  290. /* [30] */
  291. .reg_cg_check_vrf18_req_mask_b = 1,
  292. /* [31] */
  293. .reg_cg_check_ddr_en_mask_b = 1,
  294. /* SPM_SRC4_MASK */
  295. /* [8:0] */
  296. .reg_mcusys_merge_apsrc_req_mask_b = 0,
  297. /* [17:9] */
  298. .reg_mcusys_merge_ddr_en_mask_b = 0,
  299. /* [19:18] */
  300. .reg_dramc_md32_infra_req_mask_b = 3,
  301. /* [21:20] */
  302. .reg_dramc_md32_vrf18_req_mask_b = 3,
  303. /* [23:22] */
  304. .reg_dramc_md32_ddr_en_mask_b = 0,
  305. /* [24] */
  306. .reg_dvfsrc_event_trigger_mask_b = 1,
  307. /* SPM_WAKEUP_EVENT_MASK2 */
  308. /* [3:0] */
  309. .reg_sc_sw2spm_wakeup_mask_b = 0,
  310. /* [4] */
  311. .reg_sc_adsp2spm_wakeup_mask_b = 0,
  312. /* [8:5] */
  313. .reg_sc_sspm2spm_wakeup_mask_b = 0,
  314. /* [9] */
  315. .reg_sc_scp2spm_wakeup_mask_b = 0,
  316. /* [10] */
  317. .reg_csyspwrup_ack_mask = 0,
  318. /* [11] */
  319. .reg_csyspwrup_req_mask = 1,
  320. /* SPM_WAKEUP_EVENT_MASK */
  321. /* [31:0] */
  322. .reg_wakeup_event_mask = 0xC1382213,
  323. /* SPM_WAKEUP_EVENT_EXT_MASK */
  324. /* [31:0] */
  325. .reg_ext_wakeup_event_mask = 0xFFFFFFFF,
  326. /*sw flag setting */
  327. .pcm_flags = SPM_SUSPEND_PCM_FLAG,
  328. .pcm_flags1 = SPM_SUSPEND_PCM_FLAG1,
  329. };
  330. struct spm_lp_scen __spm_suspend = {
  331. .pwrctrl = &suspend_ctrl,
  332. };
  333. int mt_spm_suspend_mode_set(int mode, void *prv)
  334. {
  335. if (mode == MT_SPM_SUSPEND_SLEEP) {
  336. suspend_ctrl.pcm_flags = SPM_SUSPEND_SLEEP_PCM_FLAG;
  337. suspend_ctrl.pcm_flags1 = SPM_SUSPEND_SLEEP_PCM_FLAG1;
  338. } else {
  339. suspend_ctrl.pcm_flags = SPM_SUSPEND_PCM_FLAG;
  340. suspend_ctrl.pcm_flags1 = SPM_SUSPEND_PCM_FLAG1;
  341. }
  342. return 0;
  343. }
  344. int mt_spm_suspend_enter(int state_id, unsigned int ext_opand, unsigned int reosuce_req)
  345. {
  346. int ret = 0;
  347. /* if FMAudio, ADSP is active, change to sleep suspend mode */
  348. if ((ext_opand & MT_SPM_EX_OP_SET_SUSPEND_MODE) != 0U) {
  349. mt_spm_suspend_mode_set(MT_SPM_SUSPEND_SLEEP, NULL);
  350. }
  351. if ((ext_opand & MT_SPM_EX_OP_PERI_ON) != 0U) {
  352. suspend_ctrl.pcm_flags |= SPM_FLAG_PERI_ON_IN_SUSPEND;
  353. } else {
  354. suspend_ctrl.pcm_flags &= ~SPM_FLAG_PERI_ON_IN_SUSPEND;
  355. }
  356. if ((ext_opand & MT_SPM_EX_OP_INFRA_ON) != 0U) {
  357. suspend_ctrl.pcm_flags |= SPM_FLAG_DISABLE_INFRA_PDN;
  358. } else {
  359. suspend_ctrl.pcm_flags &= ~SPM_FLAG_DISABLE_INFRA_PDN;
  360. }
  361. #ifndef MTK_PLAT_SPM_UART_UNSUPPORT
  362. /* Notify UART to sleep */
  363. mtk_uart_save();
  364. #endif
  365. ret = spm_conservation(state_id, ext_opand, &__spm_suspend, reosuce_req);
  366. if (ret == 0) {
  367. struct mt_lp_publish_event event = {
  368. .id = MT_LPM_PUBEVENTS_SYS_POWER_OFF,
  369. .val.u32 = 0U,
  370. };
  371. MT_LP_SUSPEND_PUBLISH_EVENT(&event);
  372. }
  373. return ret;
  374. }
  375. void mt_spm_suspend_resume(int state_id, unsigned int ext_opand, struct wake_status **status)
  376. {
  377. struct mt_lp_publish_event event = {
  378. .id = MT_LPM_PUBEVENTS_SYS_POWER_ON,
  379. .val.u32 = 0U,
  380. };
  381. struct wake_status *st = NULL;
  382. spm_conservation_finish(state_id, ext_opand, &__spm_suspend, &st);
  383. #ifndef MTK_PLAT_SPM_UART_UNSUPPORT
  384. /* Notify UART to wakeup */
  385. mtk_uart_restore();
  386. #endif
  387. /* If FMAudio, ADSP is active, change back to suspend mode and counting in resume */
  388. if ((ext_opand & MT_SPM_EX_OP_SET_SUSPEND_MODE) != 0U) {
  389. mt_spm_suspend_mode_set(MT_SPM_SUSPEND_SYSTEM_PDN, NULL);
  390. }
  391. if (status != NULL) {
  392. *status = st;
  393. }
  394. MT_LP_SUSPEND_PUBLISH_EVENT(&event);
  395. }