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_DRAM_ALLOW \ 25*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \ 26*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \ 27*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF) 28*91f16700Schasinglulu 29*91f16700Schasinglulu #define CONSTRAINT_DRAM_PCM_FLAG \ 30*91f16700Schasinglulu (SPM_FLAG_DISABLE_INFRA_PDN | \ 31*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DVS | \ 32*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DFS | \ 33*91f16700Schasinglulu SPM_FLAG_SRAM_SLEEP_CTRL | \ 34*91f16700Schasinglulu SPM_FLAG_KEEP_CSYSPWRACK_HIGH) 35*91f16700Schasinglulu 36*91f16700Schasinglulu #define CONSTRAINT_DRAM_PCM_FLAG1 (0U) 37*91f16700Schasinglulu 38*91f16700Schasinglulu #define CONSTRAINT_DRAM_RESOURCE_REQ \ 39*91f16700Schasinglulu (MT_SPM_SYSPLL | \ 40*91f16700Schasinglulu MT_SPM_INFRA | \ 41*91f16700Schasinglulu MT_SPM_26M) 42*91f16700Schasinglulu 43*91f16700Schasinglulu static struct mt_spm_cond_tables cond_dram = { 44*91f16700Schasinglulu .name = "dram", 45*91f16700Schasinglulu .table_cg = { 46*91f16700Schasinglulu 0x0385E03C, /* MTCMOS1 */ 47*91f16700Schasinglulu 0x003F0100, /* INFRA0 */ 48*91f16700Schasinglulu 0x08040802, /* INFRA1 */ 49*91f16700Schasinglulu 0x06015641, /* INFRA2 */ 50*91f16700Schasinglulu 0x00000000, /* INFRA3 */ 51*91f16700Schasinglulu 0x00000000, /* INFRA4 */ 52*91f16700Schasinglulu 0x00000000, /* INFRA5 */ 53*91f16700Schasinglulu 0x02300020, /* MMSYS0 */ 54*91f16700Schasinglulu 0x00000000, /* MMSYS1 */ 55*91f16700Schasinglulu 0x00000000, /* MMSYS2 */ 56*91f16700Schasinglulu 0x00015111, /* MMSYS3 */ 57*91f16700Schasinglulu }, 58*91f16700Schasinglulu .table_pll = 0U, 59*91f16700Schasinglulu }; 60*91f16700Schasinglulu 61*91f16700Schasinglulu static struct mt_spm_cond_tables cond_dram_res = { 62*91f16700Schasinglulu .table_cg = {0U}, 63*91f16700Schasinglulu .table_pll = 0U, 64*91f16700Schasinglulu }; 65*91f16700Schasinglulu 66*91f16700Schasinglulu static struct constraint_status status = { 67*91f16700Schasinglulu .id = MT_RM_CONSTRAINT_ID_DRAM, 68*91f16700Schasinglulu .valid = (MT_SPM_RC_VALID_SW | 69*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH | 70*91f16700Schasinglulu MT_SPM_RC_VALID_XSOC_BBLPM), 71*91f16700Schasinglulu .cond_block = 0U, 72*91f16700Schasinglulu .enter_cnt = 0U, 73*91f16700Schasinglulu .cond_res = &cond_dram_res, 74*91f16700Schasinglulu }; 75*91f16700Schasinglulu 76*91f16700Schasinglulu static void spm_dram_conduct(struct spm_lp_scen *spm_lp, 77*91f16700Schasinglulu unsigned int *resource_req) 78*91f16700Schasinglulu { 79*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_DRAM_PCM_FLAG; 80*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_DRAM_PCM_FLAG1; 81*91f16700Schasinglulu *resource_req |= CONSTRAINT_DRAM_RESOURCE_REQ; 82*91f16700Schasinglulu } 83*91f16700Schasinglulu 84*91f16700Schasinglulu bool spm_is_valid_rc_dram(unsigned int cpu, int state_id) 85*91f16700Schasinglulu { 86*91f16700Schasinglulu (void)cpu; 87*91f16700Schasinglulu (void)state_id; 88*91f16700Schasinglulu 89*91f16700Schasinglulu return ((status.cond_block == 0U) && IS_MT_RM_RC_READY(status.valid)); 90*91f16700Schasinglulu } 91*91f16700Schasinglulu 92*91f16700Schasinglulu int spm_update_rc_dram(int state_id, int type, const void *val) 93*91f16700Schasinglulu { 94*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb; 95*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb_check; 96*91f16700Schasinglulu int res = MT_RM_STATUS_OK; 97*91f16700Schasinglulu 98*91f16700Schasinglulu if (val == NULL) { 99*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 100*91f16700Schasinglulu } else { 101*91f16700Schasinglulu if (type == PLAT_RC_UPDATE_CONDITION) { 102*91f16700Schasinglulu tlb = (const struct mt_spm_cond_tables *)val; 103*91f16700Schasinglulu tlb_check = (const struct mt_spm_cond_tables *)&cond_dram; 104*91f16700Schasinglulu status.cond_block = 105*91f16700Schasinglulu mt_spm_cond_check(state_id, tlb, tlb_check, 106*91f16700Schasinglulu ((status.valid & 107*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH) != 0U) ? 108*91f16700Schasinglulu (&cond_dram_res) : (NULL)); 109*91f16700Schasinglulu } else { 110*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 111*91f16700Schasinglulu } 112*91f16700Schasinglulu } 113*91f16700Schasinglulu 114*91f16700Schasinglulu return res; 115*91f16700Schasinglulu } 116*91f16700Schasinglulu 117*91f16700Schasinglulu unsigned int spm_allow_rc_dram(int state_id) 118*91f16700Schasinglulu { 119*91f16700Schasinglulu (void)state_id; 120*91f16700Schasinglulu 121*91f16700Schasinglulu return CONSTRAINT_DRAM_ALLOW; 122*91f16700Schasinglulu } 123*91f16700Schasinglulu 124*91f16700Schasinglulu int spm_run_rc_dram(unsigned int cpu, int state_id) 125*91f16700Schasinglulu { 126*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 127*91f16700Schasinglulu unsigned int allows = CONSTRAINT_DRAM_ALLOW; 128*91f16700Schasinglulu 129*91f16700Schasinglulu (void)cpu; 130*91f16700Schasinglulu 131*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 132*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 133*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 134*91f16700Schasinglulu #else 135*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 136*91f16700Schasinglulu #endif 137*91f16700Schasinglulu } 138*91f16700Schasinglulu 139*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 140*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, allows | (IS_PLAT_SUSPEND_ID(state_id) ? 141*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND) : (0U))); 142*91f16700Schasinglulu #else 143*91f16700Schasinglulu (void)allows; 144*91f16700Schasinglulu #endif 145*91f16700Schasinglulu 146*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 147*91f16700Schasinglulu mt_spm_suspend_enter(state_id, 148*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | MT_SPM_EX_OP_HW_S1_DETECT), 149*91f16700Schasinglulu CONSTRAINT_DRAM_RESOURCE_REQ); 150*91f16700Schasinglulu } else { 151*91f16700Schasinglulu mt_spm_idle_generic_enter(state_id, ext_op, spm_dram_conduct); 152*91f16700Schasinglulu } 153*91f16700Schasinglulu 154*91f16700Schasinglulu return 0; 155*91f16700Schasinglulu } 156*91f16700Schasinglulu 157*91f16700Schasinglulu int spm_reset_rc_dram(unsigned int cpu, int state_id) 158*91f16700Schasinglulu { 159*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 160*91f16700Schasinglulu unsigned int allows = CONSTRAINT_DRAM_ALLOW; 161*91f16700Schasinglulu 162*91f16700Schasinglulu (void)cpu; 163*91f16700Schasinglulu 164*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 165*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 166*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 167*91f16700Schasinglulu #else 168*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 169*91f16700Schasinglulu #endif 170*91f16700Schasinglulu } 171*91f16700Schasinglulu 172*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 173*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, allows); 174*91f16700Schasinglulu #else 175*91f16700Schasinglulu (void)allows; 176*91f16700Schasinglulu #endif 177*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 178*91f16700Schasinglulu mt_spm_suspend_resume(state_id, 179*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | MT_SPM_EX_OP_HW_S1_DETECT), 180*91f16700Schasinglulu NULL); 181*91f16700Schasinglulu } else { 182*91f16700Schasinglulu mt_spm_idle_generic_resume(state_id, ext_op, NULL, NULL); 183*91f16700Schasinglulu status.enter_cnt++; 184*91f16700Schasinglulu } 185*91f16700Schasinglulu 186*91f16700Schasinglulu return 0; 187*91f16700Schasinglulu } 188