spm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <lib/bakery_lock.h>
  7. #include <common/debug.h>
  8. #include <drivers/delay_timer.h>
  9. #include <lib/mmio.h>
  10. #include <spm.h>
  11. #include <spm_pmic_wrap.h>
  12. DEFINE_BAKERY_LOCK(spm_lock);
  13. /* SPM_DVS_LEVEL */
  14. #define SPM_VMODEM_LEVEL_MASK (0xff << 16)
  15. #define SPM_VMODEM_LEVEL (1U << 18)
  16. #define SPM_VCORE_LEVEL_MASK (0xff)
  17. #define SPM_VCORE_LEVEL (1U << 1)
  18. /* CLK_SCP_CFG_0 */
  19. #define SPM_CK_OFF_CONTROL (0x3FF)
  20. /* CLK_SCP_CFG_1 */
  21. #define SPM_AXI_26M_SEL (0x1)
  22. /* AP_PLL_CON3 */
  23. #define SPM_PLL_CONTROL (0x7FAAAAF)
  24. /* AP_PLL_CON4 */
  25. #define SPM_PLL_OUT_OFF_CONTROL (0xFA0A)
  26. /* AP_PLL_CON6 */
  27. #define PLL_DLY (0x20000)
  28. const char *wakeup_src_str[32] = {
  29. [0] = "R12_PCM_TIMER",
  30. [1] = "R12_SSPM_WDT_EVENT_B",
  31. [2] = "R12_KP_IRQ_B",
  32. [3] = "R12_APWDT_EVENT_B",
  33. [4] = "R12_APXGPT1_EVENT_B",
  34. [5] = "R12_CONN2AP_SPM_WAKEUP_B",
  35. [6] = "R12_EINT_EVENT_B",
  36. [7] = "R12_CONN_WDT_IRQ_B",
  37. [8] = "R12_CCIF0_EVENT_B",
  38. [9] = "R12_LOWBATTERY_IRQ_B",
  39. [10] = "R12_SSPM_SPM_IRQ_B",
  40. [11] = "R12_SCP_SPM_IRQ_B",
  41. [12] = "R12_SCP_WDT_EVENT_B",
  42. [13] = "R12_PCM_WDT_WAKEUP_B",
  43. [14] = "R12_USB_CDSC_B ",
  44. [15] = "R12_USB_POWERDWN_B",
  45. [16] = "R12_SYS_TIMER_EVENT_B",
  46. [17] = "R12_EINT_EVENT_SECURE_B",
  47. [18] = "R12_CCIF1_EVENT_B",
  48. [19] = "R12_UART0_IRQ_B",
  49. [20] = "R12_AFE_IRQ_MCU_B",
  50. [21] = "R12_THERM_CTRL_EVENT_B",
  51. [22] = "R12_SYS_CIRQ_IRQ_B",
  52. [23] = "R12_MD2AP_PEER_EVENT_B",
  53. [24] = "R12_CSYSPWREQ_B",
  54. [25] = "R12_MD1_WDT_B ",
  55. [26] = "R12_CLDMA_EVENT_B",
  56. [27] = "R12_SEJ_WDT_GPT_B",
  57. [28] = "R12_ALL_SSPM_WAKEUP_B",
  58. [29] = "R12_CPU_IRQ_B",
  59. [30] = "R12_CPU_WFI_AND_B"
  60. };
  61. const char *spm_get_firmware_version(void)
  62. {
  63. return "DYNAMIC_SPM_FW_VERSION";
  64. }
  65. void spm_lock_init(void)
  66. {
  67. bakery_lock_init(&spm_lock);
  68. }
  69. void spm_lock_get(void)
  70. {
  71. bakery_lock_get(&spm_lock);
  72. }
  73. void spm_lock_release(void)
  74. {
  75. bakery_lock_release(&spm_lock);
  76. }
  77. void spm_set_bootaddr(unsigned long bootaddr)
  78. {
  79. /* initialize core4~7 boot entry address */
  80. mmio_write_32(SW2SPM_MAILBOX_3, bootaddr);
  81. }
  82. void spm_set_cpu_status(int cpu)
  83. {
  84. if (cpu >= 0 && cpu < 4) {
  85. mmio_write_32(ROOT_CPUTOP_ADDR, 0x10006204);
  86. mmio_write_32(ROOT_CORE_ADDR, 0x10006208 + (cpu * 0x4));
  87. } else if (cpu >= 4 && cpu < 8) {
  88. mmio_write_32(ROOT_CPUTOP_ADDR, 0x10006218);
  89. mmio_write_32(ROOT_CORE_ADDR, 0x1000621c + ((cpu - 4) * 0x4));
  90. } else {
  91. ERROR("%s: error cpu number %d\n", __func__, cpu);
  92. }
  93. }
  94. void spm_set_power_control(const struct pwr_ctrl *pwrctrl)
  95. {
  96. mmio_write_32(SPM_AP_STANDBY_CON,
  97. ((pwrctrl->wfi_op & 0x1) << 0) |
  98. ((pwrctrl->mp0_cputop_idle_mask & 0x1) << 1) |
  99. ((pwrctrl->mp1_cputop_idle_mask & 0x1) << 2) |
  100. ((pwrctrl->mcusys_idle_mask & 0x1) << 4) |
  101. ((pwrctrl->mm_mask_b & 0x3) << 16) |
  102. ((pwrctrl->md_ddr_en_0_dbc_en & 0x1) << 18) |
  103. ((pwrctrl->md_ddr_en_1_dbc_en & 0x1) << 19) |
  104. ((pwrctrl->md_mask_b & 0x3) << 20) |
  105. ((pwrctrl->sspm_mask_b & 0x1) << 22) |
  106. ((pwrctrl->scp_mask_b & 0x1) << 23) |
  107. ((pwrctrl->srcclkeni_mask_b & 0x1) << 24) |
  108. ((pwrctrl->md_apsrc_1_sel & 0x1) << 25) |
  109. ((pwrctrl->md_apsrc_0_sel & 0x1) << 26) |
  110. ((pwrctrl->conn_ddr_en_dbc_en & 0x1) << 27) |
  111. ((pwrctrl->conn_mask_b & 0x1) << 28) |
  112. ((pwrctrl->conn_apsrc_sel & 0x1) << 29));
  113. mmio_write_32(SPM_SRC_REQ,
  114. ((pwrctrl->spm_apsrc_req & 0x1) << 0) |
  115. ((pwrctrl->spm_f26m_req & 0x1) << 1) |
  116. ((pwrctrl->spm_infra_req & 0x1) << 3) |
  117. ((pwrctrl->spm_vrf18_req & 0x1) << 4) |
  118. ((pwrctrl->spm_ddren_req & 0x1) << 7) |
  119. ((pwrctrl->spm_rsv_src_req & 0x7) << 8) |
  120. ((pwrctrl->spm_ddren_2_req & 0x1) << 11) |
  121. ((pwrctrl->cpu_md_dvfs_sop_force_on & 0x1) << 16));
  122. mmio_write_32(SPM_SRC_MASK,
  123. ((pwrctrl->csyspwreq_mask & 0x1) << 0) |
  124. ((pwrctrl->ccif0_md_event_mask_b & 0x1) << 1) |
  125. ((pwrctrl->ccif0_ap_event_mask_b & 0x1) << 2) |
  126. ((pwrctrl->ccif1_md_event_mask_b & 0x1) << 3) |
  127. ((pwrctrl->ccif1_ap_event_mask_b & 0x1) << 4) |
  128. ((pwrctrl->ccif2_md_event_mask_b & 0x1) << 5) |
  129. ((pwrctrl->ccif2_ap_event_mask_b & 0x1) << 6) |
  130. ((pwrctrl->ccif3_md_event_mask_b & 0x1) << 7) |
  131. ((pwrctrl->ccif3_ap_event_mask_b & 0x1) << 8) |
  132. ((pwrctrl->md_srcclkena_0_infra_mask_b & 0x1) << 9) |
  133. ((pwrctrl->md_srcclkena_1_infra_mask_b & 0x1) << 10) |
  134. ((pwrctrl->conn_srcclkena_infra_mask_b & 0x1) << 11) |
  135. ((pwrctrl->ufs_infra_req_mask_b & 0x1) << 12) |
  136. ((pwrctrl->srcclkeni_infra_mask_b & 0x1) << 13) |
  137. ((pwrctrl->md_apsrc_req_0_infra_mask_b & 0x1) << 14) |
  138. ((pwrctrl->md_apsrc_req_1_infra_mask_b & 0x1) << 15) |
  139. ((pwrctrl->conn_apsrcreq_infra_mask_b & 0x1) << 16) |
  140. ((pwrctrl->ufs_srcclkena_mask_b & 0x1) << 17) |
  141. ((pwrctrl->md_vrf18_req_0_mask_b & 0x1) << 18) |
  142. ((pwrctrl->md_vrf18_req_1_mask_b & 0x1) << 19) |
  143. ((pwrctrl->ufs_vrf18_req_mask_b & 0x1) << 20) |
  144. ((pwrctrl->gce_vrf18_req_mask_b & 0x1) << 21) |
  145. ((pwrctrl->conn_infra_req_mask_b & 0x1) << 22) |
  146. ((pwrctrl->gce_apsrc_req_mask_b & 0x1) << 23) |
  147. ((pwrctrl->disp0_apsrc_req_mask_b & 0x1) << 24) |
  148. ((pwrctrl->disp1_apsrc_req_mask_b & 0x1) << 25) |
  149. ((pwrctrl->mfg_req_mask_b & 0x1) << 26) |
  150. ((pwrctrl->vdec_req_mask_b & 0x1) << 27));
  151. mmio_write_32(SPM_SRC2_MASK,
  152. ((pwrctrl->md_ddr_en_0_mask_b & 0x1) << 0) |
  153. ((pwrctrl->md_ddr_en_1_mask_b & 0x1) << 1) |
  154. ((pwrctrl->conn_ddr_en_mask_b & 0x1) << 2) |
  155. ((pwrctrl->ddren_sspm_apsrc_req_mask_b & 0x1) << 3) |
  156. ((pwrctrl->ddren_scp_apsrc_req_mask_b & 0x1) << 4) |
  157. ((pwrctrl->disp0_ddren_mask_b & 0x1) << 5) |
  158. ((pwrctrl->disp1_ddren_mask_b & 0x1) << 6) |
  159. ((pwrctrl->gce_ddren_mask_b & 0x1) << 7) |
  160. ((pwrctrl->ddren_emi_self_refresh_ch0_mask_b & 0x1)
  161. << 8) |
  162. ((pwrctrl->ddren_emi_self_refresh_ch1_mask_b & 0x1)
  163. << 9));
  164. mmio_write_32(SPM_WAKEUP_EVENT_MASK,
  165. ((pwrctrl->spm_wakeup_event_mask & 0xffffffff) << 0));
  166. mmio_write_32(SPM_WAKEUP_EVENT_EXT_MASK,
  167. ((pwrctrl->spm_wakeup_event_ext_mask & 0xffffffff)
  168. << 0));
  169. mmio_write_32(SPM_SRC3_MASK,
  170. ((pwrctrl->md_ddr_en_2_0_mask_b & 0x1) << 0) |
  171. ((pwrctrl->md_ddr_en_2_1_mask_b & 0x1) << 1) |
  172. ((pwrctrl->conn_ddr_en_2_mask_b & 0x1) << 2) |
  173. ((pwrctrl->ddren2_sspm_apsrc_req_mask_b & 0x1) << 3) |
  174. ((pwrctrl->ddren2_scp_apsrc_req_mask_b & 0x1) << 4) |
  175. ((pwrctrl->disp0_ddren2_mask_b & 0x1) << 5) |
  176. ((pwrctrl->disp1_ddren2_mask_b & 0x1) << 6) |
  177. ((pwrctrl->gce_ddren2_mask_b & 0x1) << 7) |
  178. ((pwrctrl->ddren2_emi_self_refresh_ch0_mask_b & 0x1)
  179. << 8) |
  180. ((pwrctrl->ddren2_emi_self_refresh_ch1_mask_b & 0x1)
  181. << 9));
  182. mmio_write_32(MP0_CPU0_WFI_EN,
  183. ((pwrctrl->mp0_cpu0_wfi_en & 0x1) << 0));
  184. mmio_write_32(MP0_CPU1_WFI_EN,
  185. ((pwrctrl->mp0_cpu1_wfi_en & 0x1) << 0));
  186. mmio_write_32(MP0_CPU2_WFI_EN,
  187. ((pwrctrl->mp0_cpu2_wfi_en & 0x1) << 0));
  188. mmio_write_32(MP0_CPU3_WFI_EN,
  189. ((pwrctrl->mp0_cpu3_wfi_en & 0x1) << 0));
  190. mmio_write_32(MP1_CPU0_WFI_EN,
  191. ((pwrctrl->mp1_cpu0_wfi_en & 0x1) << 0));
  192. mmio_write_32(MP1_CPU1_WFI_EN,
  193. ((pwrctrl->mp1_cpu1_wfi_en & 0x1) << 0));
  194. mmio_write_32(MP1_CPU2_WFI_EN,
  195. ((pwrctrl->mp1_cpu2_wfi_en & 0x1) << 0));
  196. mmio_write_32(MP1_CPU3_WFI_EN,
  197. ((pwrctrl->mp1_cpu3_wfi_en & 0x1) << 0));
  198. }
  199. void spm_disable_pcm_timer(void)
  200. {
  201. mmio_clrsetbits_32(PCM_CON1, PCM_TIMER_EN_LSB, SPM_REGWR_CFG_KEY);
  202. }
  203. void spm_set_wakeup_event(const struct pwr_ctrl *pwrctrl)
  204. {
  205. uint32_t val, mask, isr;
  206. val = pwrctrl->timer_val ? pwrctrl->timer_val : PCM_TIMER_MAX;
  207. mmio_write_32(PCM_TIMER_VAL, val);
  208. mmio_setbits_32(PCM_CON1, SPM_REGWR_CFG_KEY | PCM_TIMER_EN_LSB);
  209. mask = pwrctrl->wake_src;
  210. if (pwrctrl->csyspwreq_mask)
  211. mask &= ~WAKE_SRC_R12_CSYSPWREQ_B;
  212. mmio_write_32(SPM_WAKEUP_EVENT_MASK, ~mask);
  213. isr = mmio_read_32(SPM_IRQ_MASK) & SPM_TWAM_IRQ_MASK_LSB;
  214. mmio_write_32(SPM_IRQ_MASK, isr | ISRM_RET_IRQ_AUX);
  215. }
  216. void spm_set_pcm_flags(const struct pwr_ctrl *pwrctrl)
  217. {
  218. mmio_write_32(SPM_SW_FLAG, pwrctrl->pcm_flags);
  219. mmio_write_32(SPM_SW_RSV_2, pwrctrl->pcm_flags1);
  220. }
  221. void spm_set_pcm_wdt(int en)
  222. {
  223. if (en) {
  224. mmio_clrsetbits_32(PCM_CON1, PCM_WDT_WAKE_MODE_LSB,
  225. SPM_REGWR_CFG_KEY);
  226. if (mmio_read_32(PCM_TIMER_VAL) > PCM_TIMER_MAX)
  227. mmio_write_32(PCM_TIMER_VAL, PCM_TIMER_MAX);
  228. mmio_write_32(PCM_WDT_VAL,
  229. mmio_read_32(PCM_TIMER_VAL) + PCM_WDT_TIMEOUT);
  230. mmio_setbits_32(PCM_CON1, SPM_REGWR_CFG_KEY | PCM_WDT_EN_LSB);
  231. } else {
  232. mmio_clrsetbits_32(PCM_CON1, PCM_WDT_EN_LSB,
  233. SPM_REGWR_CFG_KEY);
  234. }
  235. }
  236. void spm_send_cpu_wakeup_event(void)
  237. {
  238. mmio_write_32(PCM_REG_DATA_INI, 0);
  239. mmio_write_32(SPM_CPU_WAKEUP_EVENT, 1);
  240. }
  241. void spm_get_wakeup_status(struct wake_status *wakesta)
  242. {
  243. wakesta->assert_pc = mmio_read_32(PCM_REG_DATA_INI);
  244. wakesta->r12 = mmio_read_32(SPM_SW_RSV_0);
  245. wakesta->r12_ext = mmio_read_32(PCM_REG12_EXT_DATA);
  246. wakesta->raw_sta = mmio_read_32(SPM_WAKEUP_STA);
  247. wakesta->raw_ext_sta = mmio_read_32(SPM_WAKEUP_EXT_STA);
  248. wakesta->wake_misc = mmio_read_32(SPM_BSI_D0_SR);
  249. wakesta->timer_out = mmio_read_32(SPM_BSI_D1_SR);
  250. wakesta->r13 = mmio_read_32(PCM_REG13_DATA);
  251. wakesta->idle_sta = mmio_read_32(SUBSYS_IDLE_STA);
  252. wakesta->req_sta = mmio_read_32(SRC_REQ_STA);
  253. wakesta->sw_flag = mmio_read_32(SPM_SW_FLAG);
  254. wakesta->sw_flag1 = mmio_read_32(SPM_SW_RSV_2);
  255. wakesta->r15 = mmio_read_32(PCM_REG15_DATA);
  256. wakesta->debug_flag = mmio_read_32(SPM_SW_DEBUG);
  257. wakesta->debug_flag1 = mmio_read_32(WDT_LATCH_SPARE0_FIX);
  258. wakesta->event_reg = mmio_read_32(SPM_BSI_D2_SR);
  259. wakesta->isr = mmio_read_32(SPM_IRQ_STA);
  260. }
  261. void spm_clean_after_wakeup(void)
  262. {
  263. mmio_write_32(SPM_SW_RSV_0,
  264. mmio_read_32(SPM_WAKEUP_STA) |
  265. mmio_read_32(SPM_SW_RSV_0));
  266. mmio_write_32(SPM_CPU_WAKEUP_EVENT, 0);
  267. mmio_write_32(SPM_WAKEUP_EVENT_MASK, ~0);
  268. mmio_setbits_32(SPM_IRQ_MASK, ISRM_ALL_EXC_TWAM);
  269. mmio_write_32(SPM_IRQ_STA, ISRC_ALL_EXC_TWAM);
  270. mmio_write_32(SPM_SWINT_CLR, PCM_SW_INT_ALL);
  271. }
  272. void spm_output_wake_reason(struct wake_status *wakesta, const char *scenario)
  273. {
  274. uint32_t i;
  275. if (wakesta->assert_pc != 0) {
  276. INFO("%s: PCM ASSERT AT %u, ULPOSC_CON = 0x%x\n",
  277. scenario, wakesta->assert_pc, mmio_read_32(ULPOSC_CON));
  278. goto spm_debug_flags;
  279. }
  280. for (i = 0; i <= 31; i++) {
  281. if (wakesta->r12 & (1U << i)) {
  282. INFO("%s: wake up by %s, timer_out = %u\n",
  283. scenario, wakeup_src_str[i], wakesta->timer_out);
  284. break;
  285. }
  286. }
  287. spm_debug_flags:
  288. INFO("r15 = 0x%x, r13 = 0x%x, debug_flag = 0x%x 0x%x\n",
  289. wakesta->r15, wakesta->r13, wakesta->debug_flag,
  290. wakesta->debug_flag1);
  291. INFO("sw_flag = 0x%x 0x%x, r12 = 0x%x, r12_ext = 0x%x\n",
  292. wakesta->sw_flag, wakesta->sw_flag1, wakesta->r12,
  293. wakesta->r12_ext);
  294. INFO("idle_sta = 0x%x, req_sta = 0x%x, event_reg = 0x%x\n",
  295. wakesta->idle_sta, wakesta->req_sta, wakesta->event_reg);
  296. INFO("isr = 0x%x, raw_sta = 0x%x, raw_ext_sta = 0x%x\n",
  297. wakesta->isr, wakesta->raw_sta, wakesta->raw_ext_sta);
  298. INFO("wake_misc = 0x%x\n", wakesta->wake_misc);
  299. }
  300. void spm_boot_init(void)
  301. {
  302. NOTICE("%s() start\n", __func__);
  303. spm_lock_init();
  304. mt_spm_pmic_wrap_set_phase(PMIC_WRAP_PHASE_ALLINONE);
  305. /* Set Vmodem / Vcore DVS init level */
  306. mmio_clrsetbits_32(SPM_DVS_LEVEL,
  307. SPM_VMODEM_LEVEL_MASK | SPM_VCORE_LEVEL_MASK,
  308. SPM_VMODEM_LEVEL | SPM_VCORE_LEVEL);
  309. /* switch ck_off/axi_26m control to SPM */
  310. mmio_setbits_32(CLK_SCP_CFG_0, SPM_CK_OFF_CONTROL);
  311. mmio_setbits_32(CLK_SCP_CFG_1, SPM_AXI_26M_SEL);
  312. /* switch PLL/CLKSQ control to SPM */
  313. mmio_clrbits_32(AP_PLL_CON3, SPM_PLL_CONTROL);
  314. mmio_clrbits_32(AP_PLL_CON4, SPM_PLL_OUT_OFF_CONTROL);
  315. mmio_clrbits_32(AP_PLL_CON6, PLL_DLY);
  316. NOTICE("%s() end\n", __func__);
  317. }