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 #include <mt_lp_rm.h> 10*91f16700Schasinglulu #include <mt_spm.h> 11*91f16700Schasinglulu #include <mt_spm_cond.h> 12*91f16700Schasinglulu #include <mt_spm_conservation.h> 13*91f16700Schasinglulu #include <mt_spm_constraint.h> 14*91f16700Schasinglulu #include <mt_spm_idle.h> 15*91f16700Schasinglulu #include <mt_spm_internal.h> 16*91f16700Schasinglulu #include <mt_spm_notifier.h> 17*91f16700Schasinglulu #include <mt_spm_rc_internal.h> 18*91f16700Schasinglulu #include <mt_spm_reg.h> 19*91f16700Schasinglulu #include <mt_spm_resource_req.h> 20*91f16700Schasinglulu #include <mt_spm_suspend.h> 21*91f16700Schasinglulu #include <plat_mtk_lpm.h> 22*91f16700Schasinglulu #include <plat_pm.h> 23*91f16700Schasinglulu 24*91f16700Schasinglulu #define CONSTRAINT_SYSPLL_ALLOW \ 25*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF | \ 26*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \ 27*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \ 28*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_VCORE_LP) 29*91f16700Schasinglulu 30*91f16700Schasinglulu #if (MTK_SPM_EXTENSION_PMIC_CONTROL == 6362) 31*91f16700Schasinglulu #define SPM_FLAG_EXTRA_PMIC_CONTROL (SPM_FLAG_ENABLE_6362_CTRL) 32*91f16700Schasinglulu #else 33*91f16700Schasinglulu #define SPM_FLAG_EXTRA_PMIC_CONTROL (SPM_FLAG_ENABLE_6315_CTRL) 34*91f16700Schasinglulu #endif 35*91f16700Schasinglulu 36*91f16700Schasinglulu #define CONSTRAINT_SYSPLL_PCM_FLAG \ 37*91f16700Schasinglulu (SPM_FLAG_DISABLE_INFRA_PDN | \ 38*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DVS | \ 39*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DFS | \ 40*91f16700Schasinglulu SPM_FLAG_USE_SRCCLKENO2 | \ 41*91f16700Schasinglulu SPM_FLAG_SRAM_SLEEP_CTRL | \ 42*91f16700Schasinglulu SPM_FLAG_KEEP_CSYSPWRACK_HIGH | \ 43*91f16700Schasinglulu SPM_FLAG_USE_SRCCLKENO2) 44*91f16700Schasinglulu 45*91f16700Schasinglulu #define CONSTRAINT_SYSPLL_PCM_FLAG1 (0U) 46*91f16700Schasinglulu #define CONSTRAINT_SYSPLL_RESOURCE_REQ (MT_SPM_26M) 47*91f16700Schasinglulu 48*91f16700Schasinglulu static struct mt_spm_cond_tables cond_syspll = { 49*91f16700Schasinglulu .name = "syspll", 50*91f16700Schasinglulu .table_cg = { 51*91f16700Schasinglulu 0x0385E03C, /* MTCMOS1 */ 52*91f16700Schasinglulu 0x003F0100, /* INFRA0 */ 53*91f16700Schasinglulu 0x08040802, /* INFRA1 */ 54*91f16700Schasinglulu 0x06015641, /* INFRA2 */ 55*91f16700Schasinglulu 0x00000000, /* INFRA3 */ 56*91f16700Schasinglulu 0x00000000, /* INFRA4 */ 57*91f16700Schasinglulu 0x00000000, /* INFRA5 */ 58*91f16700Schasinglulu 0x03720820, /* MMSYS0 */ 59*91f16700Schasinglulu 0x00000000, /* MMSYS1 */ 60*91f16700Schasinglulu 0x00000000, /* MMSYS2 */ 61*91f16700Schasinglulu 0x00015151, /* MMSYS3 */ 62*91f16700Schasinglulu }, 63*91f16700Schasinglulu .table_pll = 0U, 64*91f16700Schasinglulu }; 65*91f16700Schasinglulu 66*91f16700Schasinglulu static struct mt_spm_cond_tables cond_syspll_res = { 67*91f16700Schasinglulu .table_cg = {0U}, 68*91f16700Schasinglulu .table_pll = 0U, 69*91f16700Schasinglulu }; 70*91f16700Schasinglulu 71*91f16700Schasinglulu static struct constraint_status status = { 72*91f16700Schasinglulu .id = MT_RM_CONSTRAINT_ID_SYSPLL, 73*91f16700Schasinglulu .valid = (MT_SPM_RC_VALID_SW | 74*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH | 75*91f16700Schasinglulu MT_SPM_RC_VALID_XSOC_BBLPM), 76*91f16700Schasinglulu .cond_block = 0U, 77*91f16700Schasinglulu .enter_cnt = 0U, 78*91f16700Schasinglulu .cond_res = &cond_syspll_res, 79*91f16700Schasinglulu }; 80*91f16700Schasinglulu 81*91f16700Schasinglulu static void spm_syspll_conduct(struct spm_lp_scen *spm_lp, 82*91f16700Schasinglulu unsigned int *resource_req) 83*91f16700Schasinglulu { 84*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_SYSPLL_PCM_FLAG; 85*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_SYSPLL_PCM_FLAG1; 86*91f16700Schasinglulu 87*91f16700Schasinglulu *resource_req |= CONSTRAINT_SYSPLL_RESOURCE_REQ; 88*91f16700Schasinglulu } 89*91f16700Schasinglulu 90*91f16700Schasinglulu bool spm_is_valid_rc_syspll(unsigned int cpu, int state_id) 91*91f16700Schasinglulu { 92*91f16700Schasinglulu (void)cpu; 93*91f16700Schasinglulu (void)state_id; 94*91f16700Schasinglulu 95*91f16700Schasinglulu return ((status.cond_block == 0U) && IS_MT_RM_RC_READY(status.valid)); 96*91f16700Schasinglulu } 97*91f16700Schasinglulu 98*91f16700Schasinglulu int spm_update_rc_syspll(int state_id, int type, const void *val) 99*91f16700Schasinglulu { 100*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb; 101*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb_check; 102*91f16700Schasinglulu int res = MT_RM_STATUS_OK; 103*91f16700Schasinglulu 104*91f16700Schasinglulu if (val == NULL) { 105*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 106*91f16700Schasinglulu } else { 107*91f16700Schasinglulu if (type == PLAT_RC_UPDATE_CONDITION) { 108*91f16700Schasinglulu tlb = (const struct mt_spm_cond_tables *)val; 109*91f16700Schasinglulu tlb_check = (const struct mt_spm_cond_tables *)&cond_syspll; 110*91f16700Schasinglulu 111*91f16700Schasinglulu status.cond_block = 112*91f16700Schasinglulu mt_spm_cond_check(state_id, tlb, tlb_check, 113*91f16700Schasinglulu ((status.valid & 114*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH) != 0U) ? 115*91f16700Schasinglulu (&cond_syspll_res) : (NULL)); 116*91f16700Schasinglulu } else { 117*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 118*91f16700Schasinglulu } 119*91f16700Schasinglulu } 120*91f16700Schasinglulu 121*91f16700Schasinglulu return res; 122*91f16700Schasinglulu } 123*91f16700Schasinglulu 124*91f16700Schasinglulu unsigned int spm_allow_rc_syspll(int state_id) 125*91f16700Schasinglulu { 126*91f16700Schasinglulu (void)state_id; 127*91f16700Schasinglulu 128*91f16700Schasinglulu return CONSTRAINT_SYSPLL_ALLOW; 129*91f16700Schasinglulu } 130*91f16700Schasinglulu 131*91f16700Schasinglulu int spm_run_rc_syspll(unsigned int cpu, int state_id) 132*91f16700Schasinglulu { 133*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 134*91f16700Schasinglulu unsigned int allows = CONSTRAINT_SYSPLL_ALLOW; 135*91f16700Schasinglulu 136*91f16700Schasinglulu (void)cpu; 137*91f16700Schasinglulu 138*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 139*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 140*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 141*91f16700Schasinglulu #else 142*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 143*91f16700Schasinglulu #endif 144*91f16700Schasinglulu } 145*91f16700Schasinglulu 146*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 147*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, allows | (IS_PLAT_SUSPEND_ID(state_id) ? 148*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND) : (0U))); 149*91f16700Schasinglulu #else 150*91f16700Schasinglulu (void)allows; 151*91f16700Schasinglulu #endif 152*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 153*91f16700Schasinglulu mt_spm_suspend_enter(state_id, 154*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | 155*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT | 156*91f16700Schasinglulu MT_SPM_EX_OP_SET_SUSPEND_MODE), 157*91f16700Schasinglulu CONSTRAINT_SYSPLL_RESOURCE_REQ); 158*91f16700Schasinglulu } else { 159*91f16700Schasinglulu mt_spm_idle_generic_enter(state_id, ext_op, spm_syspll_conduct); 160*91f16700Schasinglulu } 161*91f16700Schasinglulu 162*91f16700Schasinglulu return 0; 163*91f16700Schasinglulu } 164*91f16700Schasinglulu 165*91f16700Schasinglulu int spm_reset_rc_syspll(unsigned int cpu, int state_id) 166*91f16700Schasinglulu { 167*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 168*91f16700Schasinglulu unsigned int allows = CONSTRAINT_SYSPLL_ALLOW; 169*91f16700Schasinglulu 170*91f16700Schasinglulu (void)cpu; 171*91f16700Schasinglulu 172*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 173*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 174*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 175*91f16700Schasinglulu #else 176*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 177*91f16700Schasinglulu #endif 178*91f16700Schasinglulu } 179*91f16700Schasinglulu 180*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 181*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, allows); 182*91f16700Schasinglulu #else 183*91f16700Schasinglulu (void)allows; 184*91f16700Schasinglulu #endif 185*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 186*91f16700Schasinglulu mt_spm_suspend_resume(state_id, 187*91f16700Schasinglulu (MT_SPM_EX_OP_SET_SUSPEND_MODE | 188*91f16700Schasinglulu MT_SPM_EX_OP_SET_WDT | 189*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT), 190*91f16700Schasinglulu NULL); 191*91f16700Schasinglulu } else { 192*91f16700Schasinglulu mt_spm_idle_generic_resume(state_id, ext_op, NULL, NULL); 193*91f16700Schasinglulu status.enter_cnt++; 194*91f16700Schasinglulu } 195*91f16700Schasinglulu 196*91f16700Schasinglulu return 0; 197*91f16700Schasinglulu } 198