1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2021, 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 SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP) 37*91f16700Schasinglulu 38*91f16700Schasinglulu #define CONSTRAINT_DRAM_PCM_FLAG1 0U 39*91f16700Schasinglulu 40*91f16700Schasinglulu #define CONSTRAINT_DRAM_RESOURCE_REQ \ 41*91f16700Schasinglulu (MT_SPM_SYSPLL | \ 42*91f16700Schasinglulu MT_SPM_INFRA | \ 43*91f16700Schasinglulu MT_SPM_26M) 44*91f16700Schasinglulu 45*91f16700Schasinglulu static struct mt_spm_cond_tables cond_dram = { 46*91f16700Schasinglulu .name = "dram", 47*91f16700Schasinglulu .table_cg = { 48*91f16700Schasinglulu 0xFFFDD008, /* MTCMOS1 */ 49*91f16700Schasinglulu 0x20040802, /* INFRA0 */ 50*91f16700Schasinglulu 0x27AF8000, /* INFRA1 */ 51*91f16700Schasinglulu 0x86040640, /* INFRA2 */ 52*91f16700Schasinglulu 0x00000000, /* INFRA3 */ 53*91f16700Schasinglulu 0x80000000, /* INFRA4 */ 54*91f16700Schasinglulu 0x00000000, /* PERI0 */ 55*91f16700Schasinglulu 0x00004000, /* VPPSYS0_0 */ 56*91f16700Schasinglulu 0x08803000, /* VPPSYS0_1 */ 57*91f16700Schasinglulu 0x00000000, /* VPPSYS0_2 */ 58*91f16700Schasinglulu 0x80005555, /* VPPSYS1_0 */ 59*91f16700Schasinglulu 0x00009008, /* VPPSYS1_1 */ 60*91f16700Schasinglulu 0x60060000, /* VDOSYS0_0 */ 61*91f16700Schasinglulu 0x00000000, /* VDOSYS0_1 */ 62*91f16700Schasinglulu 0x201E01F8, /* VDOSYS1_0 */ 63*91f16700Schasinglulu 0x00800000, /* VDOSYS1_1 */ 64*91f16700Schasinglulu 0x00000000, /* VDOSYS1_2 */ 65*91f16700Schasinglulu 0x00000080, /* I2C */ 66*91f16700Schasinglulu }, 67*91f16700Schasinglulu .table_pll = 0U, 68*91f16700Schasinglulu }; 69*91f16700Schasinglulu 70*91f16700Schasinglulu static struct mt_spm_cond_tables cond_dram_res = { 71*91f16700Schasinglulu .table_cg = { 0U }, 72*91f16700Schasinglulu .table_pll = 0U, 73*91f16700Schasinglulu }; 74*91f16700Schasinglulu 75*91f16700Schasinglulu static struct constraint_status status = { 76*91f16700Schasinglulu .id = MT_RM_CONSTRAINT_ID_DRAM, 77*91f16700Schasinglulu .valid = (MT_SPM_RC_VALID_SW | 78*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH | 79*91f16700Schasinglulu MT_SPM_RC_VALID_XSOC_BBLPM), 80*91f16700Schasinglulu .cond_block = 0U, 81*91f16700Schasinglulu .enter_cnt = 0U, 82*91f16700Schasinglulu .cond_res = &cond_dram_res, 83*91f16700Schasinglulu }; 84*91f16700Schasinglulu 85*91f16700Schasinglulu static void spm_dram_conduct(struct spm_lp_scen *spm_lp, 86*91f16700Schasinglulu unsigned int *resource_req) 87*91f16700Schasinglulu { 88*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_DRAM_PCM_FLAG; 89*91f16700Schasinglulu spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_DRAM_PCM_FLAG1; 90*91f16700Schasinglulu *resource_req |= CONSTRAINT_DRAM_RESOURCE_REQ; 91*91f16700Schasinglulu } 92*91f16700Schasinglulu 93*91f16700Schasinglulu bool spm_is_valid_rc_dram(unsigned int cpu, int state_id) 94*91f16700Schasinglulu { 95*91f16700Schasinglulu (void)cpu; 96*91f16700Schasinglulu (void)state_id; 97*91f16700Schasinglulu 98*91f16700Schasinglulu return (status.cond_block == 0U) && IS_MT_RM_RC_READY(status.valid); 99*91f16700Schasinglulu } 100*91f16700Schasinglulu 101*91f16700Schasinglulu int spm_update_rc_dram(int state_id, int type, const void *val) 102*91f16700Schasinglulu { 103*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb; 104*91f16700Schasinglulu const struct mt_spm_cond_tables *tlb_check; 105*91f16700Schasinglulu int res = MT_RM_STATUS_OK; 106*91f16700Schasinglulu 107*91f16700Schasinglulu if (val == NULL) { 108*91f16700Schasinglulu return MT_RM_STATUS_BAD; 109*91f16700Schasinglulu } 110*91f16700Schasinglulu 111*91f16700Schasinglulu if (type == PLAT_RC_UPDATE_CONDITION) { 112*91f16700Schasinglulu tlb = (const struct mt_spm_cond_tables *)val; 113*91f16700Schasinglulu tlb_check = (const struct mt_spm_cond_tables *)&cond_dram; 114*91f16700Schasinglulu status.cond_block = 115*91f16700Schasinglulu mt_spm_cond_check(state_id, tlb, tlb_check, 116*91f16700Schasinglulu ((status.valid & 117*91f16700Schasinglulu MT_SPM_RC_VALID_COND_LATCH) != 0U) ? 118*91f16700Schasinglulu &cond_dram_res : NULL); 119*91f16700Schasinglulu } else { 120*91f16700Schasinglulu res = MT_RM_STATUS_BAD; 121*91f16700Schasinglulu } 122*91f16700Schasinglulu 123*91f16700Schasinglulu return res; 124*91f16700Schasinglulu } 125*91f16700Schasinglulu 126*91f16700Schasinglulu unsigned int spm_allow_rc_dram(int state_id) 127*91f16700Schasinglulu { 128*91f16700Schasinglulu (void)state_id; 129*91f16700Schasinglulu 130*91f16700Schasinglulu return CONSTRAINT_DRAM_ALLOW; 131*91f16700Schasinglulu } 132*91f16700Schasinglulu 133*91f16700Schasinglulu int spm_run_rc_dram(unsigned int cpu, int state_id) 134*91f16700Schasinglulu { 135*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 136*91f16700Schasinglulu unsigned int allows = CONSTRAINT_DRAM_ALLOW; 137*91f16700Schasinglulu 138*91f16700Schasinglulu (void)cpu; 139*91f16700Schasinglulu 140*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 141*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 142*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 143*91f16700Schasinglulu #else 144*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 145*91f16700Schasinglulu #endif 146*91f16700Schasinglulu } 147*91f16700Schasinglulu 148*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 149*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, allows | 150*91f16700Schasinglulu (IS_PLAT_SUSPEND_ID(state_id) ? 151*91f16700Schasinglulu MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND : 0U)); 152*91f16700Schasinglulu #else 153*91f16700Schasinglulu (void)allows; 154*91f16700Schasinglulu #endif 155*91f16700Schasinglulu 156*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 157*91f16700Schasinglulu mt_spm_suspend_enter(state_id, 158*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | 159*91f16700Schasinglulu MT_SPM_EX_OP_SET_SUSPEND_MODE | 160*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT), 161*91f16700Schasinglulu CONSTRAINT_DRAM_RESOURCE_REQ); 162*91f16700Schasinglulu } else { 163*91f16700Schasinglulu mt_spm_idle_generic_enter(state_id, ext_op, spm_dram_conduct); 164*91f16700Schasinglulu } 165*91f16700Schasinglulu 166*91f16700Schasinglulu return 0; 167*91f16700Schasinglulu } 168*91f16700Schasinglulu 169*91f16700Schasinglulu int spm_reset_rc_dram(unsigned int cpu, int state_id) 170*91f16700Schasinglulu { 171*91f16700Schasinglulu unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; 172*91f16700Schasinglulu unsigned int allows = CONSTRAINT_DRAM_ALLOW; 173*91f16700Schasinglulu 174*91f16700Schasinglulu (void)cpu; 175*91f16700Schasinglulu 176*91f16700Schasinglulu if (IS_MT_SPM_RC_BBLPM_MODE(status.valid)) { 177*91f16700Schasinglulu #ifdef MT_SPM_USING_SRCLKEN_RC 178*91f16700Schasinglulu ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; 179*91f16700Schasinglulu #else 180*91f16700Schasinglulu allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; 181*91f16700Schasinglulu #endif 182*91f16700Schasinglulu } 183*91f16700Schasinglulu 184*91f16700Schasinglulu #ifndef ATF_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT 185*91f16700Schasinglulu mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, allows); 186*91f16700Schasinglulu #else 187*91f16700Schasinglulu (void)allows; 188*91f16700Schasinglulu #endif 189*91f16700Schasinglulu 190*91f16700Schasinglulu if (IS_PLAT_SUSPEND_ID(state_id)) { 191*91f16700Schasinglulu mt_spm_suspend_resume(state_id, 192*91f16700Schasinglulu (MT_SPM_EX_OP_SET_WDT | 193*91f16700Schasinglulu MT_SPM_EX_OP_HW_S1_DETECT), 194*91f16700Schasinglulu NULL); 195*91f16700Schasinglulu } else { 196*91f16700Schasinglulu mt_spm_idle_generic_resume(state_id, ext_op, NULL); 197*91f16700Schasinglulu status.enter_cnt++; 198*91f16700Schasinglulu } 199*91f16700Schasinglulu 200*91f16700Schasinglulu return 0; 201*91f16700Schasinglulu } 202