1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2022, MediaTek Inc. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <arch_helpers.h> 8*91f16700Schasinglulu #include <common/debug.h> 9*91f16700Schasinglulu 10*91f16700Schasinglulu #ifndef ATF_PLAT_CIRQ_UNSUPPORT 11*91f16700Schasinglulu #include <mt_gic_v3.h> 12*91f16700Schasinglulu #endif 13*91f16700Schasinglulu 14*91f16700Schasinglulu #include <mt_lp_rm.h> 15*91f16700Schasinglulu #include <mt_spm.h> 16*91f16700Schasinglulu #include <mt_spm_cond.h> 17*91f16700Schasinglulu #include <mt_spm_conservation.h> 18*91f16700Schasinglulu #include <mt_spm_constraint.h> 19*91f16700Schasinglulu #include <mt_spm_idle.h> 20*91f16700Schasinglulu #include <mt_spm_internal.h> 21*91f16700Schasinglulu #include <mt_spm_notifier.h> 22*91f16700Schasinglulu #include <mt_spm_pmic_wrap.h> 23*91f16700Schasinglulu #include <mt_spm_rc_internal.h> 24*91f16700Schasinglulu #include <mt_spm_reg.h> 25*91f16700Schasinglulu #include <mt_spm_resource_req.h> 26*91f16700Schasinglulu #include <mt_spm_suspend.h> 27*91f16700Schasinglulu 28*91f16700Schasinglulu #ifndef ATF_PLAT_CIRQ_UNSUPPORT 29*91f16700Schasinglulu #include <mt_cirq.h> 30*91f16700Schasinglulu #endif 31*91f16700Schasinglulu 32*91f16700Schasinglulu #include <plat_mtk_lpm.h> 33*91f16700Schasinglulu #include <plat_pm.h> 34*91f16700Schasinglulu 35*91f16700Schasinglulu #define CONSTRAINT_BUS26M_ALLOW \ 36*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF | \ 37*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \ 38*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \ 39*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_VCORE_LP | \ 40*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_LVTS_STATE | \ 41*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_BUS26M_OFF) 42*91f16700Schasinglulu 43*91f16700Schasinglulu #define CONSTRAINT_BUS26M_PCM_FLAG \ 44*91f16700Schasinglulu (SPM_FLAG_DISABLE_INFRA_PDN | \ 45*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DVS | \ 46*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DFS | \ 47*91f16700Schasinglulu SPM_FLAG_SRAM_SLEEP_CTRL | \ 48*91f16700Schasinglulu SPM_FLAG_ENABLE_TIA_WORKAROUND | \ 49*91f16700Schasinglulu SPM_FLAG_ENABLE_LVTS_WORKAROUND | \ 50*91f16700Schasinglulu SPM_FLAG_KEEP_CSYSPWRACK_HIGH) 51*91f16700Schasinglulu 52*91f16700Schasinglulu #define CONSTRAINT_BUS26M_PCM_FLAG1 (0U) 53*91f16700Schasinglulu #define CONSTRAINT_BUS26M_RESOURCE_REQ (0U) 54*91f16700Schasinglulu 55*91f16700Schasinglulu static unsigned int bus26m_ext_opand; 56*91f16700Schasinglulu static struct mt_irqremain *refer2remain_irq; 57*91f16700Schasinglulu static struct mt_spm_cond_tables cond_bus26m = { 58*91f16700Schasinglulu .name = "bus26m", 59*91f16700Schasinglulu .table_cg = { 60*91f16700Schasinglulu 0x0385E03C, /* MTCMOS1 */ 61*91f16700Schasinglulu 0x003F0100, /* INFRA0 */ 62*91f16700Schasinglulu 0x0A040802, /* INFRA1 */ 63*91f16700Schasinglulu 0x06017E51, /* INFRA2 */ 64*91f16700Schasinglulu 0x08000000, /* INFRA3 */ 65*91f16700Schasinglulu 0x00000000, /* INFRA4 */ 66*91f16700Schasinglulu 0x00000000, /* INFRA5 */ 67*91f16700Schasinglulu 0x03720820, /* MMSYS0 */ 68*91f16700Schasinglulu 0x00000000, /* MMSYS1 */ 69*91f16700Schasinglulu 0x00000000, /* MMSYS2 */ 70*91f16700Schasinglulu 0x00015151, /* MMSYS3 */ 71*91f16700Schasinglulu }, 72*91f16700Schasinglulu .table_pll = (PLL_BIT_UNIVPLL | PLL_BIT_MFGPLL | 73*91f16700Schasinglulu PLL_BIT_MSDCPLL | PLL_BIT_TVDPLL | 74*91f16700Schasinglulu PLL_BIT_MMPLL), 75*91f16700Schasinglulu }; 76*91f16700Schasinglulu 77*91f16700Schasinglulu static struct mt_spm_cond_tables cond_bus26m_res = { 78*91f16700Schasinglulu .table_cg = {0U}, 79*91f16700Schasinglulu .table_pll = 0U, 80*91f16700Schasinglulu }; 81*91f16700Schasinglulu 82*91f16700Schasinglulu static struct constraint_status status = { 83*91f16700Schasinglulu .id = MT_RM_CONSTRAINT_ID_BUS26M, 84*91f16700Schasinglulu .valid = (MT_SPM_RC_VALID_SW | MT_SPM_RC_VALID_COND_LATCH), 85*91f16700Schasinglulu .cond_block = 0U, 86*91f16700Schasinglulu .enter_cnt = 0U, 87*91f16700Schasinglulu .cond_res = &cond_bus26m_res, 88*91f16700Schasinglulu }; 89*91f16700Schasinglulu 90*91f16700Schasinglulu /* 91*91f16700Schasinglulu * Cirq will take the place of gic when gic is off. 92*91f16700Schasinglulu * However, cirq cannot work if 26m clk is turned off when system idle/suspend. 93*91f16700Schasinglulu * Therefore, we need to set irq pending for specific wakeup source. 94*91f16700Schasinglulu */ 95*91f16700Schasinglulu #ifdef ATF_PLAT_CIRQ_UNSUPPORT 96*91f16700Schasinglulu #define do_irqs_delivery() 97*91f16700Schasinglulu #else 98*91f16700Schasinglulu static void mt_spm_irq_remain_dump(struct mt_irqremain *irqs, 99*91f16700Schasinglulu unsigned int irq_index, 100*91f16700Schasinglulu struct wake_status *wakeup) 101*91f16700Schasinglulu { 102*91f16700Schasinglulu INFO("[SPM] r12 = 0x%08x(0x%08x), flag = 0x%08x 0x%08x 0x%08x\n", 103*91f16700Schasinglulu wakeup->tr.comm.r12, wakeup->md32pcm_wakeup_sta, 104*91f16700Schasinglulu wakeup->tr.comm.debug_flag, wakeup->tr.comm.b_sw_flag0, 105*91f16700Schasinglulu wakeup->tr.comm.b_sw_flag1); 106*91f16700Schasinglulu 107*91f16700Schasinglulu INFO("irq:%u(0x%08x) set pending\n", 108*91f16700Schasinglulu irqs->wakeupsrc[irq_index], irqs->irqs[irq_index]); 109*91f16700Schasinglulu } 110*91f16700Schasinglulu 111*91f16700Schasinglulu static void do_irqs_delivery(void) 112*91f16700Schasinglulu { 113*91f16700Schasinglulu unsigned int idx; 114*91f16700Schasinglulu int res = 0; 115*91f16700Schasinglulu struct wake_status *wakeup = NULL; 116*91f16700Schasinglulu struct mt_irqremain *irqs = refer2remain_irq; 117*91f16700Schasinglulu 118*91f16700Schasinglulu res = spm_conservation_get_result(&wakeup); 119*91f16700Schasinglulu 120*91f16700Schasinglulu if ((res != 0) && (irqs == NULL)) { 121*91f16700Schasinglulu return; 122*91f16700Schasinglulu } 123*91f16700Schasinglulu 124*91f16700Schasinglulu for (idx = 0; idx < irqs->count; ++idx) { 125*91f16700Schasinglulu if (((wakeup->tr.comm.r12 & irqs->wakeupsrc[idx]) != 0U) || 126*91f16700Schasinglulu ((wakeup->raw_sta & irqs->wakeupsrc[idx]) != 0U)) { 127*91f16700Schasinglulu if ((irqs->wakeupsrc_cat[idx] & MT_IRQ_REMAIN_CAT_LOG) != 0U) { 128*91f16700Schasinglulu mt_spm_irq_remain_dump(irqs, idx, wakeup); 129*91f16700Schasinglulu } 130*91f16700Schasinglulu 131*91f16700Schasinglulu mt_irq_set_pending(irqs->irqs[idx]); 132*91f16700Schasinglulu } 133*91f16700Schasinglulu } 134*91f16700Schasinglulu } 135*91f16700Schasinglulu #endif 136*91f16700Schasinglulu 137*91f16700Schasinglulu static void spm_bus26m_conduct(struct spm_lp_scen *spm_lp, unsigned int *resource_req) 138*91f16700Schasinglulu { 139*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG; 140*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_BUS26M_PCM_FLAG1; 141*91f16700Schasinglulu *resource_req |= CONSTRAINT_BUS26M_RESOURCE_REQ; 142*91f16700Schasinglulu } 143*91f16700Schasinglulu 144*91f16700Schasinglulu bool spm_is_valid_rc_bus26m(unsigned int cpu, int state_id) 145*91f16700Schasinglulu { 146*91f16700Schasinglulu (void)cpu; 147*91f16700Schasinglulu (void)state_id; 148*91f16700Schasinglulu 149*91f16700Schasinglulu return ((status.cond_block == 0U) && IS_MT_RM_RC_READY(status.valid)); 150*91f16700Schasinglulu } 151*91f16700Schasinglulu 152*91f16700Schasinglulu int spm_update_rc_bus26m(int state_id, int type, const void *val) 153*91f16700Schasinglulu { 154*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb; 155*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb_check; 156*91f16700Schasinglulu int res = MT_RM_STATUS_OK; 157*91f16700Schasinglulu 158*91f16700Schasinglulu if (val == NULL) { 159*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 160*91f16700Schasinglulu } else { 161*91f16700Schasinglulu if (type == PLAT_RC_UPDATE_CONDITION) { 162*91f16700Schasinglulu tlb = (const struct mt_spm_cond_tables *)val; 163*91f16700Schasinglulu tlb_check = (const struct mt_spm_cond_tables *)&cond_bus26m; 164*91f16700Schasinglulu 165*91f16700Schasinglulu status.cond_block = 166*91f16700Schasinglulu mt_spm_cond_check(state_id, tlb, tlb_check, 167*91f16700Schasinglulu ((status.valid & 168*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH) != 0U) ? 169*91f16700Schasinglulu (&cond_bus26m_res) : (NULL)); 170*91f16700Schasinglulu } else if (type == PLAT_RC_UPDATE_REMAIN_IRQS) { 171*91f16700Schasinglulu refer2remain_irq = (struct mt_irqremain *)val; 172*91f16700Schasinglulu } else { 173*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 174*91f16700Schasinglulu } 175*91f16700Schasinglulu } 176*91f16700Schasinglulu 177*91f16700Schasinglulu return res; 178*91f16700Schasinglulu } 179*91f16700Schasinglulu 180*91f16700Schasinglulu unsigned int spm_allow_rc_bus26m(int state_id) 181*91f16700Schasinglulu { 182*91f16700Schasinglulu (void)state_id; 183*91f16700Schasinglulu 184*91f16700Schasinglulu return CONSTRAINT_BUS26M_ALLOW; 185*91f16700Schasinglulu } 186*91f16700Schasinglulu 187*91f16700Schasinglulu int spm_run_rc_bus26m(unsigned int cpu, int state_id) 188*91f16700Schasinglulu { 189*91f16700Schasinglulu (void)cpu; 190*91f16700Schasinglulu 191*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 192*91f16700Schasinglulu 193*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 194*91f16700Schasinglulu 195*91f16700Schasinglulu #ifdef ATF_VOLTAGE_BIN_VCORE_SUPPORT 196*91f16700Schasinglulu #define SUSPEND_VB_MAGIC (0x5642) 197*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 198*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_SUSPEND_VCORE_VOLTAGE, 199*91f16700Schasinglulu ((SUSPEND_VB_MAGIC << 16) | 200*91f16700Schasinglulu spm_get_suspend_vcore_voltage_idx())); 201*91f16700Schasinglulu } 202*91f16700Schasinglulu #endif 203*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, CONSTRAINT_BUS26M_ALLOW | 204*91f16700Schasinglulu (IS_PLAT_SUSPEND_ID(state_id) ? 205*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND) : (0U))); 206*91f16700Schasinglulu #endif 207*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 208*91f16700Schasinglulu mt_spm_suspend_enter(state_id, 209*91f16700Schasinglulu (MT_SPM_EX_OP_CLR_26M_RECORD | 210*91f16700Schasinglulu MT_SPM_EX_OP_SET_WDT | 211*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT | 212*91f16700Schasinglulu bus26m_ext_opand), 213*91f16700Schasinglulu CONSTRAINT_BUS26M_RESOURCE_REQ); 214*91f16700Schasinglulu } else { 215*91f16700Schasinglulu mt_spm_idle_generic_enter(state_id, ext_op, spm_bus26m_conduct); 216*91f16700Schasinglulu } 217*91f16700Schasinglulu 218*91f16700Schasinglulu return 0; 219*91f16700Schasinglulu } 220*91f16700Schasinglulu 221*91f16700Schasinglulu int spm_reset_rc_bus26m(unsigned int cpu, int state_id) 222*91f16700Schasinglulu { 223*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 224*91f16700Schasinglulu 225*91f16700Schasinglulu (void)cpu; 226*91f16700Schasinglulu 227*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 228*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, 0U); 229*91f16700Schasinglulu #endif 230*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 231*91f16700Schasinglulu ext_op |= (bus26m_ext_opand | MT_SPM_EX_OP_SET_WDT); 232*91f16700Schasinglulu mt_spm_suspend_resume(state_id, ext_op, NULL); 233*91f16700Schasinglulu bus26m_ext_opand = 0U; 234*91f16700Schasinglulu } else { 235*91f16700Schasinglulu mt_spm_idle_generic_resume(state_id, ext_op, NULL, NULL); 236*91f16700Schasinglulu status.enter_cnt++; 237*91f16700Schasinglulu } 238*91f16700Schasinglulu 239*91f16700Schasinglulu do_irqs_delivery(); 240*91f16700Schasinglulu return 0; 241*91f16700Schasinglulu } 242