1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2020, 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 #include <mt_lp_rm.h> 11*91f16700Schasinglulu #include <mt_spm.h> 12*91f16700Schasinglulu #include <mt_spm_cond.h> 13*91f16700Schasinglulu #include <mt_spm_constraint.h> 14*91f16700Schasinglulu #include <mt_spm_conservation.h> 15*91f16700Schasinglulu #include <mt_spm_idle.h> 16*91f16700Schasinglulu #include <mt_spm_internal.h> 17*91f16700Schasinglulu #include <mt_spm_notifier.h> 18*91f16700Schasinglulu #include <mt_spm_resource_req.h> 19*91f16700Schasinglulu #include <mt_spm_reg.h> 20*91f16700Schasinglulu #include <mt_spm_rc_internal.h> 21*91f16700Schasinglulu #include <mt_spm_suspend.h> 22*91f16700Schasinglulu #include <plat_pm.h> 23*91f16700Schasinglulu #include <plat_mtk_lpm.h> 24*91f16700Schasinglulu 25*91f16700Schasinglulu #define CONSTRAINT_DRAM_ALLOW \ 26*91f16700Schasinglulu (MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \ 27*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \ 28*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF) 29*91f16700Schasinglulu 30*91f16700Schasinglulu #define CONSTRAINT_DRAM_PCM_FLAG \ 31*91f16700Schasinglulu (SPM_FLAG_DISABLE_INFRA_PDN | \ 32*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DVS | \ 33*91f16700Schasinglulu SPM_FLAG_DISABLE_VCORE_DFS | \ 34*91f16700Schasinglulu SPM_FLAG_SRAM_SLEEP_CTRL | \ 35*91f16700Schasinglulu SPM_FLAG_KEEP_CSYSPWRACK_HIGH) 36*91f16700Schasinglulu 37*91f16700Schasinglulu #define CONSTRAINT_DRAM_PCM_FLAG1 0U 38*91f16700Schasinglulu 39*91f16700Schasinglulu #define CONSTRAINT_DRAM_RESOURCE_REQ \ 40*91f16700Schasinglulu (MT_SPM_SYSPLL | \ 41*91f16700Schasinglulu MT_SPM_INFRA | \ 42*91f16700Schasinglulu MT_SPM_26M) 43*91f16700Schasinglulu 44*91f16700Schasinglulu static struct mt_spm_cond_tables cond_dram = { 45*91f16700Schasinglulu .name = "dram", 46*91f16700Schasinglulu .table_cg = { 47*91f16700Schasinglulu 0x078BF1FC, /* MTCMOS1 */ 48*91f16700Schasinglulu 0x080D8856, /* INFRA0 */ 49*91f16700Schasinglulu 0x03AF9A00, /* INFRA1 */ 50*91f16700Schasinglulu 0x86000640, /* INFRA2 */ 51*91f16700Schasinglulu 0xC800C000, /* INFRA3 */ 52*91f16700Schasinglulu 0x00000000, /* INFRA4 */ 53*91f16700Schasinglulu 0x00000000, /* INFRA5 */ 54*91f16700Schasinglulu 0x200C0000, /* MMSYS0 */ 55*91f16700Schasinglulu 0x00000000, /* MMSYS1 */ 56*91f16700Schasinglulu 0x00000000, /* MMSYS2 */ 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 return MT_RM_STATUS_BAD; 100*91f16700Schasinglulu } 101*91f16700Schasinglulu 102*91f16700Schasinglulu if (type == PLAT_RC_UPDATE_CONDITION) { 103*91f16700Schasinglulu tlb = (const struct mt_spm_cond_tables *)val; 104*91f16700Schasinglulu tlb_check = (const struct mt_spm_cond_tables *)&cond_dram; 105*91f16700Schasinglulu status.cond_block = 106*91f16700Schasinglulu mt_spm_cond_check(state_id, tlb, tlb_check, 107*91f16700Schasinglulu ((status.valid & 108*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH) != 0U) ? 109*91f16700Schasinglulu &cond_dram_res : NULL); 110*91f16700Schasinglulu } else { 111*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 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 | 141*91f16700Schasinglulu (IS_PLAT_SUSPEND_ID(state_id) ? 142*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND : 0U)); 143*91f16700Schasinglulu #else 144*91f16700Schasinglulu (void)allows; 145*91f16700Schasinglulu #endif 146*91f16700Schasinglulu 147*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 148*91f16700Schasinglulu mt_spm_suspend_enter(state_id, 149*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | 150*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT), 151*91f16700Schasinglulu CONSTRAINT_DRAM_RESOURCE_REQ); 152*91f16700Schasinglulu } else { 153*91f16700Schasinglulu mt_spm_idle_generic_enter(state_id, ext_op, spm_dram_conduct); 154*91f16700Schasinglulu } 155*91f16700Schasinglulu 156*91f16700Schasinglulu return 0; 157*91f16700Schasinglulu } 158*91f16700Schasinglulu 159*91f16700Schasinglulu int spm_reset_rc_dram(unsigned int cpu, int state_id) 160*91f16700Schasinglulu { 161*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 162*91f16700Schasinglulu unsigned int allows = CONSTRAINT_DRAM_ALLOW; 163*91f16700Schasinglulu 164*91f16700Schasinglulu (void)cpu; 165*91f16700Schasinglulu 166*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 167*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 168*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 169*91f16700Schasinglulu #else 170*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 171*91f16700Schasinglulu #endif 172*91f16700Schasinglulu } 173*91f16700Schasinglulu 174*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 175*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, allows); 176*91f16700Schasinglulu #else 177*91f16700Schasinglulu (void)allows; 178*91f16700Schasinglulu #endif 179*91f16700Schasinglulu 180*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 181*91f16700Schasinglulu mt_spm_suspend_resume(state_id, 182*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | 183*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT), 184*91f16700Schasinglulu NULL); 185*91f16700Schasinglulu } else { 186*91f16700Schasinglulu mt_spm_idle_generic_resume(state_id, ext_op, NULL); 187*91f16700Schasinglulu status.enter_cnt++; 188*91f16700Schasinglulu } 189*91f16700Schasinglulu 190*91f16700Schasinglulu return 0; 191*91f16700Schasinglulu } 192