xref: /arm-trusted-firmware/plat/mediatek/mt8192/drivers/spm/mt_spm_cond.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2020-2023, MediaTek Inc. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <stdbool.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <common/debug.h>
10*91f16700Schasinglulu #include <lib/mmio.h>
11*91f16700Schasinglulu 
12*91f16700Schasinglulu #include <mt_spm_cond.h>
13*91f16700Schasinglulu #include <mt_spm_conservation.h>
14*91f16700Schasinglulu #include <mt_spm_constraint.h>
15*91f16700Schasinglulu #include <plat_mtk_lpm.h>
16*91f16700Schasinglulu #include <plat_pm.h>
17*91f16700Schasinglulu #include <platform_def.h>
18*91f16700Schasinglulu 
19*91f16700Schasinglulu #define MT_LP_TZ_INFRA_REG(ofs)		(INFRACFG_AO_BASE + ofs)
20*91f16700Schasinglulu #define MT_LP_TZ_MM_REG(ofs)		(MMSYS_BASE + ofs)
21*91f16700Schasinglulu #define MT_LP_TZ_SPM_REG(ofs)		(SPM_BASE + ofs)
22*91f16700Schasinglulu #define MT_LP_TZ_TOPCK_REG(ofs)		(TOPCKGEN_BASE + ofs)
23*91f16700Schasinglulu #define MT_LP_TZ_APMIXEDSYS(ofs)	(APMIXEDSYS + ofs)
24*91f16700Schasinglulu 
25*91f16700Schasinglulu #define SPM_PWR_STATUS			MT_LP_TZ_SPM_REG(0x016C)
26*91f16700Schasinglulu #define SPM_PWR_STATUS_2ND		MT_LP_TZ_SPM_REG(0x0170)
27*91f16700Schasinglulu #define	INFRA_SW_CG0			MT_LP_TZ_INFRA_REG(0x0094)
28*91f16700Schasinglulu #define	INFRA_SW_CG1			MT_LP_TZ_INFRA_REG(0x0090)
29*91f16700Schasinglulu #define	INFRA_SW_CG2			MT_LP_TZ_INFRA_REG(0x00AC)
30*91f16700Schasinglulu #define	INFRA_SW_CG3			MT_LP_TZ_INFRA_REG(0x00C8)
31*91f16700Schasinglulu #define INFRA_SW_CG4                    MT_LP_TZ_INFRA_REG(0x00D8)
32*91f16700Schasinglulu #define INFRA_SW_CG5                    MT_LP_TZ_INFRA_REG(0x00E8)
33*91f16700Schasinglulu #define MMSYS_CG_CON0			MT_LP_TZ_MM_REG(0x100)
34*91f16700Schasinglulu #define MMSYS_CG_CON1			MT_LP_TZ_MM_REG(0x110)
35*91f16700Schasinglulu #define MMSYS_CG_CON2                   MT_LP_TZ_MM_REG(0x1A0)
36*91f16700Schasinglulu 
37*91f16700Schasinglulu /***********************************************************
38*91f16700Schasinglulu  * Check clkmux registers
39*91f16700Schasinglulu  ***********************************************************/
40*91f16700Schasinglulu #define CLK_CFG(id)	MT_LP_TZ_TOPCK_REG(0x20 + id * 0x10)
41*91f16700Schasinglulu #define PDN_CHECK	BIT(7)
42*91f16700Schasinglulu #define CLK_CHECK	BIT(31)
43*91f16700Schasinglulu 
44*91f16700Schasinglulu enum {
45*91f16700Schasinglulu 	CLKMUX_DISP = 0,
46*91f16700Schasinglulu 	CLKMUX_MDP  = 1,
47*91f16700Schasinglulu 	CLKMUX_IMG1 = 2,
48*91f16700Schasinglulu 	CLKMUX_IMG2 = 3,
49*91f16700Schasinglulu 	NF_CLKMUX,
50*91f16700Schasinglulu };
51*91f16700Schasinglulu 
52*91f16700Schasinglulu static bool is_clkmux_pdn(unsigned int clkmux_id)
53*91f16700Schasinglulu {
54*91f16700Schasinglulu 	unsigned int reg, val, idx;
55*91f16700Schasinglulu 
56*91f16700Schasinglulu 	if ((clkmux_id & CLK_CHECK) != 0U) {
57*91f16700Schasinglulu 		clkmux_id = (clkmux_id & ~CLK_CHECK);
58*91f16700Schasinglulu 		reg = clkmux_id / 4U;
59*91f16700Schasinglulu 		val = mmio_read_32(CLK_CFG(reg));
60*91f16700Schasinglulu 		idx = clkmux_id % 4U;
61*91f16700Schasinglulu 		val = (val >> (idx * 8U)) & PDN_CHECK;
62*91f16700Schasinglulu 		return (val != 0U);
63*91f16700Schasinglulu 	}
64*91f16700Schasinglulu 
65*91f16700Schasinglulu 	return false;
66*91f16700Schasinglulu }
67*91f16700Schasinglulu 
68*91f16700Schasinglulu static struct mt_spm_cond_tables spm_cond_t;
69*91f16700Schasinglulu 
70*91f16700Schasinglulu struct idle_cond_info {
71*91f16700Schasinglulu 	unsigned int subsys_mask;
72*91f16700Schasinglulu 	uintptr_t addr;
73*91f16700Schasinglulu 	bool bBitflip;
74*91f16700Schasinglulu 	unsigned int clkmux_id;
75*91f16700Schasinglulu };
76*91f16700Schasinglulu 
77*91f16700Schasinglulu #define IDLE_CG(mask, addr, bitflip, clkmux)	\
78*91f16700Schasinglulu 	{mask, (uintptr_t)addr, bitflip, clkmux}
79*91f16700Schasinglulu 
80*91f16700Schasinglulu static struct idle_cond_info idle_cg_info[PLAT_SPM_COND_MAX] = {
81*91f16700Schasinglulu 	IDLE_CG(0xffffffff, SPM_PWR_STATUS, false, 0U),
82*91f16700Schasinglulu 	IDLE_CG(0x00000200, INFRA_SW_CG0, true, 0U),
83*91f16700Schasinglulu 	IDLE_CG(0x00000200, INFRA_SW_CG1, true, 0U),
84*91f16700Schasinglulu 	IDLE_CG(0x00000200, INFRA_SW_CG2, true, 0U),
85*91f16700Schasinglulu 	IDLE_CG(0x00000200, INFRA_SW_CG3, true, 0U),
86*91f16700Schasinglulu 	IDLE_CG(0x00000200, INFRA_SW_CG4, true, 0U),
87*91f16700Schasinglulu 	IDLE_CG(0x00000200, INFRA_SW_CG5, true, 0U),
88*91f16700Schasinglulu 	IDLE_CG(0x00100000, MMSYS_CG_CON0, true, (CLK_CHECK | CLKMUX_DISP)),
89*91f16700Schasinglulu 	IDLE_CG(0x00100000, MMSYS_CG_CON1, true, (CLK_CHECK | CLKMUX_DISP)),
90*91f16700Schasinglulu 	IDLE_CG(0x00100000, MMSYS_CG_CON2, true, (CLK_CHECK | CLKMUX_DISP)),
91*91f16700Schasinglulu };
92*91f16700Schasinglulu 
93*91f16700Schasinglulu /***********************************************************
94*91f16700Schasinglulu  * Check pll idle condition
95*91f16700Schasinglulu  ***********************************************************/
96*91f16700Schasinglulu #define PLL_MFGPLL	MT_LP_TZ_APMIXEDSYS(0x268)
97*91f16700Schasinglulu #define PLL_MMPLL	MT_LP_TZ_APMIXEDSYS(0x360)
98*91f16700Schasinglulu #define PLL_UNIVPLL	MT_LP_TZ_APMIXEDSYS(0x308)
99*91f16700Schasinglulu #define PLL_MSDCPLL	MT_LP_TZ_APMIXEDSYS(0x350)
100*91f16700Schasinglulu #define PLL_TVDPLL	MT_LP_TZ_APMIXEDSYS(0x380)
101*91f16700Schasinglulu 
102*91f16700Schasinglulu unsigned int mt_spm_cond_check(int state_id,
103*91f16700Schasinglulu 			       const struct mt_spm_cond_tables *src,
104*91f16700Schasinglulu 			       const struct mt_spm_cond_tables *dest,
105*91f16700Schasinglulu 			       struct mt_spm_cond_tables *res)
106*91f16700Schasinglulu {
107*91f16700Schasinglulu 	unsigned int blocked = 0U, i;
108*91f16700Schasinglulu 	bool is_system_suspend = IS_PLAT_SUSPEND_ID(state_id);
109*91f16700Schasinglulu 
110*91f16700Schasinglulu 	if ((src == NULL) || (dest == NULL)) {
111*91f16700Schasinglulu 		return SPM_COND_CHECK_FAIL;
112*91f16700Schasinglulu 	}
113*91f16700Schasinglulu 
114*91f16700Schasinglulu 	for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
115*91f16700Schasinglulu 		if (res != NULL) {
116*91f16700Schasinglulu 			res->table_cg[i] =
117*91f16700Schasinglulu 				(src->table_cg[i] & dest->table_cg[i]);
118*91f16700Schasinglulu 
119*91f16700Schasinglulu 			if (is_system_suspend && (res->table_cg[i] != 0U)) {
120*91f16700Schasinglulu 				INFO("suspend: %s block[%u](0x%lx) = 0x%08x\n",
121*91f16700Schasinglulu 				     dest->name, i, idle_cg_info[i].addr,
122*91f16700Schasinglulu 				     res->table_cg[i]);
123*91f16700Schasinglulu 			}
124*91f16700Schasinglulu 
125*91f16700Schasinglulu 			if (res->table_cg[i] != 0U) {
126*91f16700Schasinglulu 				blocked |= (1U << i);
127*91f16700Schasinglulu 			}
128*91f16700Schasinglulu 		} else if ((src->table_cg[i] & dest->table_cg[i]) != 0U) {
129*91f16700Schasinglulu 			blocked |= (1U << i);
130*91f16700Schasinglulu 			break;
131*91f16700Schasinglulu 		}
132*91f16700Schasinglulu 	}
133*91f16700Schasinglulu 
134*91f16700Schasinglulu 	if (res != NULL) {
135*91f16700Schasinglulu 		res->table_pll = (src->table_pll & dest->table_pll);
136*91f16700Schasinglulu 
137*91f16700Schasinglulu 		if (res->table_pll != 0U) {
138*91f16700Schasinglulu 			blocked |=
139*91f16700Schasinglulu 				(res->table_pll << SPM_COND_BLOCKED_PLL_IDX) |
140*91f16700Schasinglulu 				 SPM_COND_CHECK_BLOCKED_PLL;
141*91f16700Schasinglulu 		}
142*91f16700Schasinglulu 	} else if ((src->table_pll & dest->table_pll) != 0U) {
143*91f16700Schasinglulu 		blocked |= SPM_COND_CHECK_BLOCKED_PLL;
144*91f16700Schasinglulu 	}
145*91f16700Schasinglulu 
146*91f16700Schasinglulu 	if (is_system_suspend && (blocked != 0U)) {
147*91f16700Schasinglulu 		INFO("suspend: %s total blocked = 0x%08x\n",
148*91f16700Schasinglulu 		     dest->name, blocked);
149*91f16700Schasinglulu 	}
150*91f16700Schasinglulu 
151*91f16700Schasinglulu 	return blocked;
152*91f16700Schasinglulu }
153*91f16700Schasinglulu 
154*91f16700Schasinglulu #define IS_MT_SPM_PWR_OFF(mask)					\
155*91f16700Schasinglulu 	(((mmio_read_32(SPM_PWR_STATUS) & mask) == 0U) &&	\
156*91f16700Schasinglulu 	 ((mmio_read_32(SPM_PWR_STATUS_2ND) & mask) == 0U))
157*91f16700Schasinglulu 
158*91f16700Schasinglulu int mt_spm_cond_update(struct mt_resource_constraint **con, unsigned int num,
159*91f16700Schasinglulu 		       int stateid, void *priv)
160*91f16700Schasinglulu {
161*91f16700Schasinglulu 	int res;
162*91f16700Schasinglulu 	uint32_t i;
163*91f16700Schasinglulu 	struct mt_resource_constraint *const *rc;
164*91f16700Schasinglulu 
165*91f16700Schasinglulu 	/* read all cg state */
166*91f16700Schasinglulu 	for (i = 0U; i < PLAT_SPM_COND_MAX; i++) {
167*91f16700Schasinglulu 		spm_cond_t.table_cg[i] = 0U;
168*91f16700Schasinglulu 
169*91f16700Schasinglulu 		/* check mtcmos, if off set idle_value and clk to 0 disable */
170*91f16700Schasinglulu 		if (IS_MT_SPM_PWR_OFF(idle_cg_info[i].subsys_mask)) {
171*91f16700Schasinglulu 			continue;
172*91f16700Schasinglulu 		}
173*91f16700Schasinglulu 
174*91f16700Schasinglulu 		/* check clkmux */
175*91f16700Schasinglulu 		if (is_clkmux_pdn(idle_cg_info[i].clkmux_id)) {
176*91f16700Schasinglulu 			continue;
177*91f16700Schasinglulu 		}
178*91f16700Schasinglulu 
179*91f16700Schasinglulu 		spm_cond_t.table_cg[i] = idle_cg_info[i].bBitflip ?
180*91f16700Schasinglulu 					 ~mmio_read_32(idle_cg_info[i].addr) :
181*91f16700Schasinglulu 					 mmio_read_32(idle_cg_info[i].addr);
182*91f16700Schasinglulu 	}
183*91f16700Schasinglulu 
184*91f16700Schasinglulu 	spm_cond_t.table_pll = 0U;
185*91f16700Schasinglulu 	if ((mmio_read_32(PLL_MFGPLL) & 0x1) != 0U) {
186*91f16700Schasinglulu 		spm_cond_t.table_pll |= PLL_BIT_MFGPLL;
187*91f16700Schasinglulu 	}
188*91f16700Schasinglulu 
189*91f16700Schasinglulu 	if ((mmio_read_32(PLL_MMPLL) & 0x1) != 0U) {
190*91f16700Schasinglulu 		spm_cond_t.table_pll |= PLL_BIT_MMPLL;
191*91f16700Schasinglulu 	}
192*91f16700Schasinglulu 
193*91f16700Schasinglulu 	if ((mmio_read_32(PLL_UNIVPLL) & 0x1) != 0U) {
194*91f16700Schasinglulu 		spm_cond_t.table_pll |= PLL_BIT_UNIVPLL;
195*91f16700Schasinglulu 	}
196*91f16700Schasinglulu 
197*91f16700Schasinglulu 	if ((mmio_read_32(PLL_MSDCPLL) & 0x1) != 0U) {
198*91f16700Schasinglulu 		spm_cond_t.table_pll |= PLL_BIT_MSDCPLL;
199*91f16700Schasinglulu 	}
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	if ((mmio_read_32(PLL_TVDPLL) & 0x1) != 0U) {
202*91f16700Schasinglulu 		spm_cond_t.table_pll |= PLL_BIT_TVDPLL;
203*91f16700Schasinglulu 	}
204*91f16700Schasinglulu 
205*91f16700Schasinglulu 	spm_cond_t.priv = priv;
206*91f16700Schasinglulu 	for (rc = con; *rc != NULL; rc++) {
207*91f16700Schasinglulu 		if (((*rc)->update) == NULL) {
208*91f16700Schasinglulu 			continue;
209*91f16700Schasinglulu 		}
210*91f16700Schasinglulu 
211*91f16700Schasinglulu 		res = (*rc)->update(stateid, PLAT_RC_UPDATE_CONDITION,
212*91f16700Schasinglulu 				    (void const *)&spm_cond_t);
213*91f16700Schasinglulu 		if (res != MT_RM_STATUS_OK) {
214*91f16700Schasinglulu 			break;
215*91f16700Schasinglulu 		}
216*91f16700Schasinglulu 	}
217*91f16700Schasinglulu 
218*91f16700Schasinglulu 	return 0;
219*91f16700Schasinglulu }
220