mt_spm_internal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (c) 2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <common/debug.h>
  11. #include <drivers/delay_timer.h>
  12. #include <lib/mmio.h>
  13. #include <drivers/spm/mt_spm_resource_req.h>
  14. #include "mt_spm.h"
  15. #include "mt_spm_internal.h"
  16. #include "mt_spm_pmic_wrap.h"
  17. #include "mt_spm_reg.h"
  18. #include <platform_def.h>
  19. #define SPM_INIT_DONE_US (20) /* Simulation result */
  20. wake_reason_t __spm_output_wake_reason(const struct wake_status *wakesta)
  21. {
  22. uint32_t bk_vtcxo_dur, spm_26m_off_pct;
  23. wake_reason_t wr = WR_UNKNOWN;
  24. if (wakesta == NULL) {
  25. return wr;
  26. }
  27. if (wakesta->is_abort != 0U) {
  28. VERBOSE("SPM EARLY WAKE r12 = 0x%x, debug_flag = 0x%x 0x%x\n",
  29. wakesta->tr.comm.r12,
  30. wakesta->tr.comm.debug_flag, wakesta->tr.comm.debug_flag1);
  31. VERBOSE("SPM EARLY WAKE sw_flag = 0x%x 0x%x b_sw_flag = 0x%x 0x%x\n",
  32. wakesta->sw_flag0, wakesta->sw_flag1,
  33. wakesta->tr.comm.b_sw_flag0, wakesta->tr.comm.b_sw_flag1);
  34. }
  35. if ((wakesta->tr.comm.r12 & R12_PCM_TIMER) != 0U) {
  36. if ((wakesta->wake_misc & WAKE_MISC_PCM_TIMER_EVENT) != 0U) {
  37. wr = WR_PCM_TIMER;
  38. }
  39. }
  40. INFO("r12 = 0x%x, r12_ext = 0x%x, r13 = 0x%x, debug_flag = 0x%x 0x%x\n",
  41. wakesta->tr.comm.r12, wakesta->r12_ext, wakesta->tr.comm.r13, wakesta->tr.comm.debug_flag,
  42. wakesta->tr.comm.debug_flag1);
  43. INFO("raw_sta = 0x%x 0x%x 0x%x, idle_sta = 0x%x, cg_check_sta = 0x%x\n",
  44. wakesta->tr.comm.raw_sta, wakesta->md32pcm_wakeup_sta,
  45. wakesta->md32pcm_event_sta, wakesta->idle_sta,
  46. wakesta->cg_check_sta);
  47. INFO("req_sta = 0x%x 0x%x 0x%x 0x%x 0x%x, isr = 0x%x\n",
  48. wakesta->tr.comm.req_sta0, wakesta->tr.comm.req_sta1, wakesta->tr.comm.req_sta2,
  49. wakesta->tr.comm.req_sta3, wakesta->tr.comm.req_sta4, wakesta->isr);
  50. INFO("rt_req_sta0 = 0x%x, rt_req_sta1 = 0x%x, rt_req_sta2 = 0x%x\n",
  51. wakesta->rt_req_sta0, wakesta->rt_req_sta1, wakesta->rt_req_sta2);
  52. INFO("rt_req_sta3 = 0x%x, dram_sw_con_3 = 0x%x, raw_ext_sta = 0x%x\n",
  53. wakesta->rt_req_sta3, wakesta->rt_req_sta4, wakesta->raw_ext_sta);
  54. INFO("wake_misc = 0x%x, pcm_flag = 0x%x 0x%x 0x%x 0x%x, req = 0x%x\n",
  55. wakesta->wake_misc, wakesta->sw_flag0, wakesta->sw_flag1,
  56. wakesta->tr.comm.b_sw_flag0, wakesta->tr.comm.b_sw_flag1, wakesta->src_req);
  57. INFO("clk_settle = 0x%x, wlk_cntcv_l = 0x%x, wlk_cntcv_h = 0x%x\n",
  58. wakesta->clk_settle, mmio_read_32(SYS_TIMER_VALUE_L),
  59. mmio_read_32(SYS_TIMER_VALUE_H));
  60. if (wakesta->tr.comm.timer_out != 0U) {
  61. bk_vtcxo_dur = mmio_read_32(SPM_BK_VTCXO_DUR);
  62. spm_26m_off_pct = (100 * bk_vtcxo_dur) / wakesta->tr.comm.timer_out;
  63. INFO("spm_26m_off_pct = %u\n", spm_26m_off_pct);
  64. }
  65. return wr;
  66. }
  67. void __spm_set_cpu_status(unsigned int cpu)
  68. {
  69. if (cpu >= 8) {
  70. ERROR("%s: error cpu number %d\n", __func__, cpu);
  71. return;
  72. }
  73. mmio_write_32(ROOT_CPUTOP_ADDR, BIT(cpu));
  74. mmio_write_32(ROOT_CORE_ADDR, SPM_CPU0_PWR_CON + (cpu * 0x4) + 0x20000000);
  75. /* Notify MCUPM to wake the target CPU up */
  76. mmio_write_32(MCUPM_MBOX_WAKEUP_CPU, cpu);
  77. }
  78. void __spm_src_req_update(const struct pwr_ctrl *pwrctrl, unsigned int resource_usage)
  79. {
  80. uint8_t reg_spm_apsrc_req = (resource_usage & MT_SPM_DRAM_S0) ?
  81. 1 : pwrctrl->reg_spm_apsrc_req;
  82. uint8_t reg_spm_ddr_en_req = (resource_usage & MT_SPM_DRAM_S1) ?
  83. 1 : pwrctrl->reg_spm_ddr_en_req;
  84. uint8_t reg_spm_vrf18_req = (resource_usage & MT_SPM_SYSPLL) ?
  85. 1 : pwrctrl->reg_spm_vrf18_req;
  86. uint8_t reg_spm_infra_req = (resource_usage & MT_SPM_INFRA) ?
  87. 1 : pwrctrl->reg_spm_infra_req;
  88. uint8_t reg_spm_f26m_req = (resource_usage & (MT_SPM_26M | MT_SPM_XO_FPM)) ?
  89. 1 : pwrctrl->reg_spm_f26m_req;
  90. /* SPM_SRC_REQ */
  91. mmio_write_32(SPM_SRC_REQ,
  92. ((reg_spm_apsrc_req & 0x1) << 0) |
  93. ((reg_spm_f26m_req & 0x1) << 1) |
  94. ((reg_spm_infra_req & 0x1) << 3) |
  95. ((reg_spm_vrf18_req & 0x1) << 4) |
  96. ((reg_spm_ddr_en_req & 0x1) << 7) |
  97. ((pwrctrl->reg_spm_dvfs_req & 0x1) << 8) |
  98. ((pwrctrl->reg_spm_sw_mailbox_req & 0x1) << 9) |
  99. ((pwrctrl->reg_spm_sspm_mailbox_req & 0x1) << 10) |
  100. ((pwrctrl->reg_spm_adsp_mailbox_req & 0x1) << 11) |
  101. ((pwrctrl->reg_spm_scp_mailbox_req & 0x1) << 12));
  102. }
  103. void __spm_set_power_control(const struct pwr_ctrl *pwrctrl)
  104. {
  105. /* SPM_AP_STANDBY_CON */
  106. mmio_write_32(SPM_AP_STANDBY_CON,
  107. ((pwrctrl->reg_wfi_op & 0x1) << 0) |
  108. ((pwrctrl->reg_wfi_type & 0x1) << 1) |
  109. ((pwrctrl->reg_mp0_cputop_idle_mask & 0x1) << 2) |
  110. ((pwrctrl->reg_mp1_cputop_idle_mask & 0x1) << 3) |
  111. ((pwrctrl->reg_mcusys_idle_mask & 0x1) << 4) |
  112. ((pwrctrl->reg_md_apsrc_1_sel & 0x1) << 25) |
  113. ((pwrctrl->reg_md_apsrc_0_sel & 0x1) << 26) |
  114. ((pwrctrl->reg_conn_apsrc_sel & 0x1) << 29));
  115. /* SPM_SRC_REQ */
  116. mmio_write_32(SPM_SRC_REQ,
  117. ((pwrctrl->reg_spm_apsrc_req & 0x1) << 0) |
  118. ((pwrctrl->reg_spm_f26m_req & 0x1) << 1) |
  119. ((pwrctrl->reg_spm_infra_req & 0x1) << 3) |
  120. ((pwrctrl->reg_spm_vrf18_req & 0x1) << 4) |
  121. ((pwrctrl->reg_spm_ddr_en_req & 0x1) << 7) |
  122. ((pwrctrl->reg_spm_dvfs_req & 0x1) << 8) |
  123. ((pwrctrl->reg_spm_sw_mailbox_req & 0x1) << 9) |
  124. ((pwrctrl->reg_spm_sspm_mailbox_req & 0x1) << 10) |
  125. ((pwrctrl->reg_spm_adsp_mailbox_req & 0x1) << 11) |
  126. ((pwrctrl->reg_spm_scp_mailbox_req & 0x1) << 12));
  127. /* SPM_SRC_MASK */
  128. mmio_write_32(SPM_SRC_MASK,
  129. ((pwrctrl->reg_sspm_srcclkena_0_mask_b & 0x1) << 0) |
  130. ((pwrctrl->reg_sspm_infra_req_0_mask_b & 0x1) << 1) |
  131. ((pwrctrl->reg_sspm_apsrc_req_0_mask_b & 0x1) << 2) |
  132. ((pwrctrl->reg_sspm_vrf18_req_0_mask_b & 0x1) << 3) |
  133. ((pwrctrl->reg_sspm_ddr_en_0_mask_b & 0x1) << 4) |
  134. ((pwrctrl->reg_scp_srcclkena_mask_b & 0x1) << 5) |
  135. ((pwrctrl->reg_scp_infra_req_mask_b & 0x1) << 6) |
  136. ((pwrctrl->reg_scp_apsrc_req_mask_b & 0x1) << 7) |
  137. ((pwrctrl->reg_scp_vrf18_req_mask_b & 0x1) << 8) |
  138. ((pwrctrl->reg_scp_ddr_en_mask_b & 0x1) << 9) |
  139. ((pwrctrl->reg_audio_dsp_srcclkena_mask_b & 0x1) << 10) |
  140. ((pwrctrl->reg_audio_dsp_infra_req_mask_b & 0x1) << 11) |
  141. ((pwrctrl->reg_audio_dsp_apsrc_req_mask_b & 0x1) << 12) |
  142. ((pwrctrl->reg_audio_dsp_vrf18_req_mask_b & 0x1) << 13) |
  143. ((pwrctrl->reg_audio_dsp_ddr_en_mask_b & 0x1) << 14) |
  144. ((pwrctrl->reg_apu_srcclkena_mask_b & 0x1) << 15) |
  145. ((pwrctrl->reg_apu_infra_req_mask_b & 0x1) << 16) |
  146. ((pwrctrl->reg_apu_apsrc_req_mask_b & 0x1) << 17) |
  147. ((pwrctrl->reg_apu_vrf18_req_mask_b & 0x1) << 18) |
  148. ((pwrctrl->reg_apu_ddr_en_mask_b & 0x1) << 19) |
  149. ((pwrctrl->reg_cpueb_srcclkena_mask_b & 0x1) << 20) |
  150. ((pwrctrl->reg_cpueb_infra_req_mask_b & 0x1) << 21) |
  151. ((pwrctrl->reg_cpueb_apsrc_req_mask_b & 0x1) << 22) |
  152. ((pwrctrl->reg_cpueb_vrf18_req_mask_b & 0x1) << 23) |
  153. ((pwrctrl->reg_cpueb_ddr_en_mask_b & 0x1) << 24) |
  154. ((pwrctrl->reg_bak_psri_srcclkena_mask_b & 0x1) << 25) |
  155. ((pwrctrl->reg_bak_psri_infra_req_mask_b & 0x1) << 26) |
  156. ((pwrctrl->reg_bak_psri_apsrc_req_mask_b & 0x1) << 27) |
  157. ((pwrctrl->reg_bak_psri_vrf18_req_mask_b & 0x1) << 28) |
  158. ((pwrctrl->reg_bak_psri_ddr_en_mask_b & 0x1) << 29) |
  159. ((pwrctrl->reg_cam_ddren_req_mask_b & 0x1) << 30) |
  160. ((pwrctrl->reg_img_ddren_req_mask_b & 0x1) << 31));
  161. /* SPM_SRC2_MASK */
  162. mmio_write_32(SPM_SRC2_MASK,
  163. ((pwrctrl->reg_msdc0_srcclkena_mask_b & 0x1) << 0) |
  164. ((pwrctrl->reg_msdc0_infra_req_mask_b & 0x1) << 1) |
  165. ((pwrctrl->reg_msdc0_apsrc_req_mask_b & 0x1) << 2) |
  166. ((pwrctrl->reg_msdc0_vrf18_req_mask_b & 0x1) << 3) |
  167. ((pwrctrl->reg_msdc0_ddr_en_mask_b & 0x1) << 4) |
  168. ((pwrctrl->reg_msdc1_srcclkena_mask_b & 0x1) << 5) |
  169. ((pwrctrl->reg_msdc1_infra_req_mask_b & 0x1) << 6) |
  170. ((pwrctrl->reg_msdc1_apsrc_req_mask_b & 0x1) << 7) |
  171. ((pwrctrl->reg_msdc1_vrf18_req_mask_b & 0x1) << 8) |
  172. ((pwrctrl->reg_msdc1_ddr_en_mask_b & 0x1) << 9) |
  173. ((pwrctrl->reg_msdc2_srcclkena_mask_b & 0x1) << 10) |
  174. ((pwrctrl->reg_msdc2_infra_req_mask_b & 0x1) << 11) |
  175. ((pwrctrl->reg_msdc2_apsrc_req_mask_b & 0x1) << 12) |
  176. ((pwrctrl->reg_msdc2_vrf18_req_mask_b & 0x1) << 13) |
  177. ((pwrctrl->reg_msdc2_ddr_en_mask_b & 0x1) << 14) |
  178. ((pwrctrl->reg_ufs_srcclkena_mask_b & 0x1) << 15) |
  179. ((pwrctrl->reg_ufs_infra_req_mask_b & 0x1) << 16) |
  180. ((pwrctrl->reg_ufs_apsrc_req_mask_b & 0x1) << 17) |
  181. ((pwrctrl->reg_ufs_vrf18_req_mask_b & 0x1) << 18) |
  182. ((pwrctrl->reg_ufs_ddr_en_mask_b & 0x1) << 19) |
  183. ((pwrctrl->reg_usb_srcclkena_mask_b & 0x1) << 20) |
  184. ((pwrctrl->reg_usb_infra_req_mask_b & 0x1) << 21) |
  185. ((pwrctrl->reg_usb_apsrc_req_mask_b & 0x1) << 22) |
  186. ((pwrctrl->reg_usb_vrf18_req_mask_b & 0x1) << 23) |
  187. ((pwrctrl->reg_usb_ddr_en_mask_b & 0x1) << 24) |
  188. ((pwrctrl->reg_pextp_p0_srcclkena_mask_b & 0x1) << 25) |
  189. ((pwrctrl->reg_pextp_p0_infra_req_mask_b & 0x1) << 26) |
  190. ((pwrctrl->reg_pextp_p0_apsrc_req_mask_b & 0x1) << 27) |
  191. ((pwrctrl->reg_pextp_p0_vrf18_req_mask_b & 0x1) << 28) |
  192. ((pwrctrl->reg_pextp_p0_ddr_en_mask_b & 0x1) << 29));
  193. /* SPM_SRC3_MASK */
  194. mmio_write_32(SPM_SRC3_MASK,
  195. ((pwrctrl->reg_pextp_p1_srcclkena_mask_b & 0x1) << 0) |
  196. ((pwrctrl->reg_pextp_p1_infra_req_mask_b & 0x1) << 1) |
  197. ((pwrctrl->reg_pextp_p1_apsrc_req_mask_b & 0x1) << 2) |
  198. ((pwrctrl->reg_pextp_p1_vrf18_req_mask_b & 0x1) << 3) |
  199. ((pwrctrl->reg_pextp_p1_ddr_en_mask_b & 0x1) << 4) |
  200. ((pwrctrl->reg_gce0_infra_req_mask_b & 0x1) << 5) |
  201. ((pwrctrl->reg_gce0_apsrc_req_mask_b & 0x1) << 6) |
  202. ((pwrctrl->reg_gce0_vrf18_req_mask_b & 0x1) << 7) |
  203. ((pwrctrl->reg_gce0_ddr_en_mask_b & 0x1) << 8) |
  204. ((pwrctrl->reg_gce1_infra_req_mask_b & 0x1) << 9) |
  205. ((pwrctrl->reg_gce1_apsrc_req_mask_b & 0x1) << 10) |
  206. ((pwrctrl->reg_gce1_vrf18_req_mask_b & 0x1) << 11) |
  207. ((pwrctrl->reg_gce1_ddr_en_mask_b & 0x1) << 12) |
  208. ((pwrctrl->reg_spm_srcclkena_reserved_mask_b & 0x1) << 13) |
  209. ((pwrctrl->reg_spm_infra_req_reserved_mask_b & 0x1) << 14) |
  210. ((pwrctrl->reg_spm_apsrc_req_reserved_mask_b & 0x1) << 15) |
  211. ((pwrctrl->reg_spm_vrf18_req_reserved_mask_b & 0x1) << 16) |
  212. ((pwrctrl->reg_spm_ddr_en_reserved_mask_b & 0x1) << 17) |
  213. ((pwrctrl->reg_disp0_ddr_en_mask_b & 0x1) << 18) |
  214. ((pwrctrl->reg_disp0_ddr_en_mask_b & 0x1) << 19) |
  215. ((pwrctrl->reg_disp1_apsrc_req_mask_b & 0x1) << 20) |
  216. ((pwrctrl->reg_disp1_ddr_en_mask_b & 0x1) << 21) |
  217. ((pwrctrl->reg_disp2_apsrc_req_mask_b & 0x1) << 22) |
  218. ((pwrctrl->reg_disp2_ddr_en_mask_b & 0x1) << 23) |
  219. ((pwrctrl->reg_disp3_apsrc_req_mask_b & 0x1) << 24) |
  220. ((pwrctrl->reg_disp3_ddr_en_mask_b & 0x1) << 25) |
  221. ((pwrctrl->reg_infrasys_apsrc_req_mask_b & 0x1) << 26) |
  222. ((pwrctrl->reg_infrasys_ddr_en_mask_b & 0x1) << 27));
  223. /* SPM_SRC4_MASK */
  224. mmio_write_32(SPM_SRC4_MASK,
  225. ((pwrctrl->reg_mcusys_merge_apsrc_req_mask_b & 0x1ff) << 0) |
  226. ((pwrctrl->reg_mcusys_merge_ddr_en_mask_b & 0x1ff) << 9) |
  227. ((pwrctrl->reg_dramc_md32_infra_req_mask_b & 0x3) << 18) |
  228. ((pwrctrl->reg_dramc_md32_vrf18_req_mask_b & 0x3) << 20) |
  229. ((pwrctrl->reg_dramc_md32_ddr_en_mask_b & 0x3) << 22) |
  230. ((pwrctrl->reg_dvfsrc_event_trigger_mask_b & 0x1) << 24));
  231. /* SPM_WAKEUP_EVENT_MASK */
  232. mmio_write_32(SPM_WAKEUP_EVENT_MASK,
  233. ((pwrctrl->reg_wakeup_event_mask & 0xffffffff) << 0));
  234. /* SPM_WAKEUP_EVENT_EXT_MASK */
  235. mmio_write_32(SPM_WAKEUP_EVENT_EXT_MASK,
  236. ((pwrctrl->reg_ext_wakeup_event_mask & 0xffffffff) << 0));
  237. }
  238. void __spm_set_wakeup_event(const struct pwr_ctrl *pwrctrl)
  239. {
  240. unsigned int val, mask;
  241. /* toggle event counter clear */
  242. mmio_setbits_32(PCM_CON1, SPM_REGWR_CFG_KEY | SPM_EVENT_COUNTER_CLR_LSB);
  243. /* toggle for reset SYS TIMER start point */
  244. mmio_setbits_32(SYS_TIMER_CON, SYS_TIMER_START_EN_LSB);
  245. if (pwrctrl->timer_val_cust == 0U) {
  246. val = (pwrctrl->timer_val != 0U) ? pwrctrl->timer_val : PCM_TIMER_MAX;
  247. } else {
  248. val = pwrctrl->timer_val_cust;
  249. }
  250. mmio_write_32(PCM_TIMER_VAL, val);
  251. mmio_setbits_32(PCM_CON1, SPM_REGWR_CFG_KEY | RG_PCM_TIMER_EN_LSB);
  252. /* unmask AP wakeup source */
  253. if (pwrctrl->wake_src_cust == 0U) {
  254. mask = pwrctrl->wake_src;
  255. } else {
  256. mask = pwrctrl->wake_src_cust;
  257. }
  258. mmio_write_32(SPM_WAKEUP_EVENT_MASK, ~mask);
  259. /* unmask SPM ISR (keep TWAM setting) */
  260. mmio_setbits_32(SPM_IRQ_MASK, ISRM_RET_IRQ_AUX);
  261. /* toggle event counter clear */
  262. mmio_clrsetbits_32(PCM_CON1, SPM_EVENT_COUNTER_CLR_LSB, SPM_REGWR_CFG_KEY);
  263. /* toggle for reset SYS TIMER start point */
  264. mmio_clrbits_32(SYS_TIMER_CON, SYS_TIMER_START_EN_LSB);
  265. }
  266. void __spm_set_pcm_flags(struct pwr_ctrl *pwrctrl)
  267. {
  268. /* set PCM flags and data */
  269. if (pwrctrl->pcm_flags_cust_clr != 0U) {
  270. pwrctrl->pcm_flags &= ~pwrctrl->pcm_flags_cust_clr;
  271. }
  272. if (pwrctrl->pcm_flags_cust_set != 0U) {
  273. pwrctrl->pcm_flags |= pwrctrl->pcm_flags_cust_set;
  274. }
  275. if (pwrctrl->pcm_flags1_cust_clr != 0U) {
  276. pwrctrl->pcm_flags1 &= ~pwrctrl->pcm_flags1_cust_clr;
  277. }
  278. if (pwrctrl->pcm_flags1_cust_set != 0U) {
  279. pwrctrl->pcm_flags1 |= pwrctrl->pcm_flags1_cust_set;
  280. }
  281. mmio_write_32(SPM_SW_FLAG_0, pwrctrl->pcm_flags);
  282. mmio_write_32(SPM_SW_FLAG_1, pwrctrl->pcm_flags1);
  283. mmio_write_32(SPM_SW_RSV_7, pwrctrl->pcm_flags);
  284. mmio_write_32(SPM_SW_RSV_8, pwrctrl->pcm_flags1);
  285. }
  286. void __spm_get_wakeup_status(struct wake_status *wakesta, unsigned int ext_status)
  287. {
  288. /* get wakeup event */
  289. wakesta->tr.comm.r12 = mmio_read_32(SPM_BK_WAKE_EVENT); /* backup of PCM_REG12_DATA */
  290. wakesta->r12_ext = mmio_read_32(SPM_WAKEUP_EXT_STA);
  291. wakesta->tr.comm.raw_sta = mmio_read_32(SPM_WAKEUP_STA);
  292. wakesta->raw_ext_sta = mmio_read_32(SPM_WAKEUP_EXT_STA);
  293. wakesta->md32pcm_wakeup_sta = mmio_read_32(MD32PCM_WAKEUP_STA);
  294. wakesta->md32pcm_event_sta = mmio_read_32(MD32PCM_EVENT_STA);
  295. wakesta->wake_misc = mmio_read_32(SPM_BK_WAKE_MISC); /* backup of SPM_WAKEUP_MISC */
  296. /* get sleep time */
  297. wakesta->tr.comm.timer_out =
  298. mmio_read_32(SPM_BK_PCM_TIMER); /* backup of PCM_TIMER_OUT */
  299. /* get other SYS and co-clock status */
  300. wakesta->tr.comm.r13 = mmio_read_32(PCM_REG13_DATA);
  301. wakesta->idle_sta = mmio_read_32(SUBSYS_IDLE_STA);
  302. wakesta->tr.comm.req_sta0 = mmio_read_32(SRC_REQ_STA_0);
  303. wakesta->tr.comm.req_sta1 = mmio_read_32(SRC_REQ_STA_1);
  304. wakesta->tr.comm.req_sta2 = mmio_read_32(SRC_REQ_STA_2);
  305. wakesta->tr.comm.req_sta3 = mmio_read_32(SRC_REQ_STA_3);
  306. wakesta->tr.comm.req_sta4 = mmio_read_32(SRC_REQ_STA_4);
  307. /* get debug flag for PCM execution check */
  308. wakesta->tr.comm.debug_flag = mmio_read_32(PCM_WDT_LATCH_SPARE_0);
  309. wakesta->tr.comm.debug_flag1 = mmio_read_32(PCM_WDT_LATCH_SPARE_1);
  310. if ((ext_status & SPM_INTERNAL_STATUS_HW_S1) != 0U) {
  311. wakesta->tr.comm.debug_flag |= (SPM_DBG_DEBUG_IDX_DDREN_WAKE |
  312. SPM_DBG_DEBUG_IDX_DDREN_SLEEP);
  313. mmio_write_32(PCM_WDT_LATCH_SPARE_0, wakesta->tr.comm.debug_flag);
  314. }
  315. /* get backup SW flag status */
  316. wakesta->tr.comm.b_sw_flag0 = mmio_read_32(SPM_SW_RSV_7); /* SPM_SW_RSV_7 */
  317. wakesta->tr.comm.b_sw_flag1 = mmio_read_32(SPM_SW_RSV_8); /* SPM_SW_RSV_8 */
  318. /* record below spm info for debug */
  319. wakesta->src_req = mmio_read_32(SPM_SRC_REQ);
  320. /* get HW CG check status */
  321. wakesta->cg_check_sta = mmio_read_32(SPM_CG_CHECK_STA);
  322. wakesta->rt_req_sta0 = mmio_read_32(SPM_SW_RSV_2);
  323. wakesta->rt_req_sta1 = mmio_read_32(SPM_SW_RSV_3);
  324. wakesta->rt_req_sta2 = mmio_read_32(SPM_SW_RSV_4);
  325. wakesta->rt_req_sta3 = mmio_read_32(SPM_SW_RSV_5);
  326. wakesta->rt_req_sta4 = mmio_read_32(SPM_SW_RSV_6);
  327. /* get ISR status */
  328. wakesta->isr = mmio_read_32(SPM_IRQ_STA);
  329. /* get SW flag status */
  330. wakesta->sw_flag0 = mmio_read_32(SPM_SW_FLAG_0);
  331. wakesta->sw_flag1 = mmio_read_32(SPM_SW_FLAG_1);
  332. /* get CLK SETTLE */
  333. wakesta->clk_settle = mmio_read_32(SPM_CLK_SETTLE);
  334. /* check abort */
  335. wakesta->is_abort = wakesta->tr.comm.debug_flag & DEBUG_ABORT_MASK;
  336. wakesta->is_abort |= wakesta->tr.comm.debug_flag1 & DEBUG_ABORT_MASK_1;
  337. }
  338. void __spm_clean_after_wakeup(void)
  339. {
  340. /*
  341. * Copy SPM_WAKEUP_STA to SPM_BK_WAKE_EVENT before clear SPM_WAKEUP_STA
  342. *
  343. * CPU dormant driver @kernel will copy edge-trig IRQ pending
  344. * (recorded @SPM_BK_WAKE_EVENT) to GIC
  345. */
  346. mmio_write_32(SPM_BK_WAKE_EVENT, mmio_read_32(SPM_WAKEUP_STA) |
  347. mmio_read_32(SPM_BK_WAKE_EVENT));
  348. /* clean CPU wakeup event */
  349. mmio_write_32(SPM_CPU_WAKEUP_EVENT, 0U);
  350. /* clean wakeup event raw status (for edge trigger event) */
  351. mmio_write_32(SPM_WAKEUP_EVENT_MASK, 0xefffffff); /* bit[28] for cpu wake up event */
  352. /* clean ISR status (except TWAM) */
  353. mmio_setbits_32(SPM_IRQ_MASK, ISRM_ALL_EXC_TWAM);
  354. mmio_write_32(SPM_IRQ_STA, ISRC_ALL_EXC_TWAM);
  355. mmio_write_32(SPM_SWINT_CLR, PCM_SW_INT_ALL);
  356. }
  357. void __spm_set_pcm_wdt(int en)
  358. {
  359. /* enable PCM WDT (normal mode) to start count if needed */
  360. if (en != 0) {
  361. mmio_clrsetbits_32(PCM_CON1, RG_PCM_WDT_WAKE_LSB, SPM_REGWR_CFG_KEY);
  362. if (mmio_read_32(PCM_TIMER_VAL) > PCM_TIMER_MAX) {
  363. mmio_write_32(PCM_TIMER_VAL, PCM_TIMER_MAX);
  364. }
  365. mmio_write_32(PCM_WDT_VAL, mmio_read_32(PCM_TIMER_VAL) + PCM_WDT_TIMEOUT);
  366. mmio_setbits_32(PCM_CON1, SPM_REGWR_CFG_KEY | RG_PCM_WDT_EN_LSB);
  367. } else {
  368. mmio_clrsetbits_32(PCM_CON1, RG_PCM_WDT_EN_LSB, SPM_REGWR_CFG_KEY);
  369. }
  370. }
  371. void __spm_send_cpu_wakeup_event(void)
  372. {
  373. mmio_write_32(SPM_CPU_WAKEUP_EVENT, 1);
  374. /* SPM will clear SPM_CPU_WAKEUP_EVENT */
  375. }
  376. void __spm_ext_int_wakeup_req_clr(void)
  377. {
  378. mmio_write_32(EXT_INT_WAKEUP_REQ_CLR, mmio_read_32(ROOT_CPUTOP_ADDR));
  379. /* clear spm2mcupm wakeup interrupt status */
  380. mmio_write_32(SPM2CPUEB_CON, 0);
  381. }
  382. void __spm_clean_before_wfi(void)
  383. {
  384. }
  385. void __spm_hw_s1_state_monitor(int en, unsigned int *status)
  386. {
  387. unsigned int reg;
  388. if (en != 0) {
  389. mmio_clrsetbits_32(SPM_ACK_CHK_CON_3, SPM_ACK_CHK_3_CON_CLR_ALL,
  390. SPM_ACK_CHK_3_CON_EN);
  391. } else {
  392. reg = mmio_read_32(SPM_ACK_CHK_CON_3);
  393. if ((reg & SPM_ACK_CHK_3_CON_RESULT) != 0U) {
  394. if (status != NULL) {
  395. *status |= SPM_INTERNAL_STATUS_HW_S1;
  396. }
  397. }
  398. mmio_clrsetbits_32(SPM_ACK_CHK_CON_3, SPM_ACK_CHK_3_CON_EN,
  399. (SPM_ACK_CHK_3_CON_HW_MODE_TRIG | SPM_ACK_CHK_3_CON_CLR_ALL));
  400. }
  401. }