summaryrefslogtreecommitdiffstats
path: root/plat/mediatek/mt8195/drivers/apusys/apupwr_clkctl.c
blob: 465054d76d0427ef08e64581466b662ab679a391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <errno.h>

#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/delay_timer.h>

#include <apupwr_clkctl.h>
#include <apupwr_clkctl_def.h>
#include <mtk_plat_common.h>
#include <platform_def.h>

/* 8195 use PCW mode to change freq directly */
enum pll_set_rate_mode PLL_MODE = CON0_PCW;

char *buck_domain_str[APUSYS_BUCK_DOMAIN_NUM] = {
	"V_VPU0",
	"V_VPU1",
	"V_MDLA0",
	"V_MDLA1",
	"V_APU_CONN",
	"V_TOP_IOMMU",
	"V_VCORE",
};

uint32_t aacc_set[APUSYS_BUCK_DOMAIN_NUM] = {
	APU_ACC_CONFG_SET1, APU_ACC_CONFG_SET2,
	APU_ACC_CONFG_SET4, APU_ACC_CONFG_SET5,
	APU_ACC_CONFG_SET0, APU_ACC_CONFG_SET7
};

uint32_t aacc_clr[APUSYS_BUCK_DOMAIN_NUM] = {
	APU_ACC_CONFG_CLR1, APU_ACC_CONFG_CLR2,
	APU_ACC_CONFG_CLR4, APU_ACC_CONFG_CLR5,
	APU_ACC_CONFG_CLR0, APU_ACC_CONFG_CLR7
};

struct reg_seq {
	uint32_t address;
	uint32_t val;
};

static const struct reg_seq init_acc_cfg[] = {
	{ APU_ACC_CONFG_SET0, BIT(BIT_SEL_APU) },
	{ APU_ACC_CONFG_CLR0, BIT(BIT_CGEN_SOC) },
	{ APU_ACC_CONFG_SET0, BIT(BIT_SEL_APU_DIV2) },
	{ APU_ACC_CONFG_SET7, BIT(BIT_SEL_APU) },
	{ APU_ACC_CONFG_CLR7, BIT(BIT_CGEN_SOC) },
	{ APU_ACC_CONFG_SET7, BIT(BIT_SEL_APU_DIV2) },
	{ APU_ACC_CONFG_SET1, BIT(BIT_SEL_APU) },
	{ APU_ACC_CONFG_CLR1, BIT(BIT_CGEN_SOC) },
	{ APU_ACC_CONFG_SET1, BIT(BIT_SEL_APU_DIV2) },
	{ APU_ACC_CONFG_SET2, BIT(BIT_INVEN_OUT) },
	{ APU_ACC_CONFG_SET2, BIT(BIT_SEL_APU) },
	{ APU_ACC_CONFG_CLR2, BIT(BIT_CGEN_SOC) },
	{ APU_ACC_CONFG_SET2, BIT(BIT_SEL_APU_DIV2) },
	{ APU_ACC_CONFG_SET4, BIT(BIT_SEL_APU) },
	{ APU_ACC_CONFG_CLR4, BIT(BIT_CGEN_SOC) },
	{ APU_ACC_CONFG_SET4, BIT(BIT_SEL_APU_DIV2) },
	{ APU_ACC_CONFG_SET5, BIT(BIT_INVEN_OUT) },
	{ APU_ACC_CONFG_SET5, BIT(BIT_SEL_APU) },
	{ APU_ACC_CONFG_CLR5, BIT(BIT_CGEN_SOC) },
	{ APU_ACC_CONFG_SET5, BIT(BIT_SEL_APU_DIV2) },
};

int32_t apupwr_smc_acc_init_all(void)
{
	int32_t i;

	for (i = 0; i < ARRAY_SIZE(init_acc_cfg); i++) {
		apupwr_writel(init_acc_cfg[i].val,
			      init_acc_cfg[i].address);
	}

	/* Deault ACC will raise APU_DIV_2 */
	apupwr_smc_pll_set_rate(BUCK_VCONN_DOMAIN_DEFAULT_FREQ,
				true, V_APU_CONN);

	apupwr_smc_pll_set_rate(BUCK_VCONN_DOMAIN_DEFAULT_FREQ,
				true, V_TOP_IOMMU);

	apupwr_smc_pll_set_rate(BUCK_VVPU_DOMAIN_DEFAULT_FREQ,
				true, V_VPU0);

	apupwr_smc_pll_set_rate(BUCK_VMDLA_DOMAIN_DEFAULT_FREQ,
				true, V_MDLA0);

	return 0;
}

void apupwr_smc_acc_top(bool enable)
{
	if (enable) {
		apupwr_writel(BIT(BIT_CGEN_APU), aacc_set[V_APU_CONN]);
		apupwr_writel(BIT(BIT_CGEN_APU), aacc_set[V_TOP_IOMMU]);
	} else {
		apupwr_writel(BIT(BIT_CGEN_APU), aacc_clr[V_APU_CONN]);
		apupwr_writel(BIT(BIT_CGEN_APU), aacc_clr[V_TOP_IOMMU]);
	}
}

/*
 * acc_clk_set_parent:ACC MUX select
 * 0. freq parameters here, only ACC clksrc is valid
 * 1. Switch between APUPLL <=> Parking (F26M, PARK)
 * 2. Turn on/off CG_F26M, CG_PARK, CG_SOC, but no CG_APU
 * 3. Clear APU Div2 while Parking
 * 4. Only use clksrc of APUPLL while ACC CG_APU is on
 */
int32_t apupwr_smc_acc_set_parent(uint32_t freq, uint32_t domain)
{
	uint32_t acc_set = 0;
	uint32_t acc_clr = 0;
	int32_t ret = 0;

	if (freq > DVFS_FREQ_ACC_APUPLL) {
		ERROR("%s wrong clksrc: %d\n", __func__, freq);
		ret = -EIO;
		goto err;
	}

	switch (domain) {
	case V_VPU1:
	case V_VPU0:
	case V_MDLA1:
	case V_MDLA0:
	case V_APU_CONN:
	case V_TOP_IOMMU:
		acc_set = aacc_set[domain];
		acc_clr = aacc_clr[domain];
		break;
	default:
		ret = -EIO;
		break;
	}

	/* Select park source */
	switch (freq) {
	case DVFS_FREQ_ACC_PARKING:
		/* Select park source */
		apupwr_writel(BIT(BIT_SEL_PARK), acc_set);
		apupwr_writel(BIT(BIT_SEL_F26M), acc_clr);
		/* Enable park cg */
		apupwr_writel(BIT(BIT_CGEN_PARK), acc_set);
		apupwr_writel(BIT(BIT_CGEN_F26M) | BIT(BIT_CGEN_SOC), acc_clr);
		/* Select park path */
		apupwr_writel(BIT(BIT_SEL_APU), acc_clr);
		/* clear apu div 2 */
		apupwr_writel(BIT(BIT_SEL_APU_DIV2), acc_clr);
		break;

	case DVFS_FREQ_ACC_APUPLL:
		/* Select park path */
		apupwr_writel(BIT(BIT_SEL_APU), acc_set);
		/* Clear park cg */
		apupwr_writel(BIT(BIT_CGEN_PARK) | BIT(BIT_CGEN_F26M) |
			      BIT(BIT_CGEN_SOC), acc_clr);
		break;

	case DVFS_FREQ_ACC_SOC:
		/* Select park source */
		apupwr_writel(BIT(BIT_SEL_PARK), acc_clr);
		apupwr_writel(BIT(BIT_SEL_F26M), acc_clr);
		/* Enable park cg */
		apupwr_writel(BIT(BIT_CGEN_SOC), acc_set);
		apupwr_writel(BIT(BIT_CGEN_F26M) | BIT(BIT_CGEN_PARK), acc_clr);
		/* Select park path */
		apupwr_writel(BIT(BIT_SEL_APU), acc_clr);
		/* clear apu div 2 */
		apupwr_writel(BIT(BIT_SEL_APU_DIV2), acc_clr);
		break;

	case DVFS_FREQ_ACC_26M:
	case DVFS_FREQ_NOT_SUPPORT:
	default:
		/* Select park source */
		apupwr_writel(BIT(BIT_SEL_F26M), acc_set);
		apupwr_writel(BIT(BIT_SEL_PARK), acc_clr);
		/* Enable park cg */
		apupwr_writel(BIT(BIT_CGEN_F26M), acc_set);
		apupwr_writel(BIT(BIT_CGEN_PARK) | BIT(BIT_CGEN_SOC), acc_clr);
		/* Select park path */
		apupwr_writel(BIT(BIT_SEL_APU), acc_clr);
		/* clear apu div 2 */
		apupwr_writel(BIT(BIT_SEL_APU_DIV2), acc_clr);
		ERROR("[APUPWR] %s wrong ACC clksrc : %d, force assign 26M\n",
		      __func__, freq);
		break;
	}

err:
	return ret;
}

int32_t apupwr_smc_pll_set_rate(uint32_t freq, bool div2, uint32_t domain)
{
	int32_t ret = 0;
	uint32_t acc_set0 = 0, acc_set1 = 0;

	if (freq > DVFS_FREQ_MAX) {
		ERROR("%s wrong freq: %d\n", __func__, freq);
		ret = -EIO;
		goto err;
	}

	/*
	 * Switch to Parking src
	 * 1. Need to switch out all ACCs sharing the same apupll
	 */
	switch (domain) {
	case V_MDLA0:
	case V_MDLA1:
		acc_set0 = APU_ACC_CONFG_SET4;
		acc_set1 = APU_ACC_CONFG_SET5;
		ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_PARKING,
						V_MDLA0);
		ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_PARKING,
						V_MDLA1);
		break;
	case V_VPU0:
	case V_VPU1:
		acc_set0 = APU_ACC_CONFG_SET1;
		acc_set1 = APU_ACC_CONFG_SET2;
		ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_PARKING,
						V_VPU0);
		ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_PARKING,
						V_VPU1);
		break;
	case V_APU_CONN:
		acc_set0 = APU_ACC_CONFG_SET0;
		ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_PARKING,
						V_APU_CONN);
		break;
	case V_TOP_IOMMU:
		acc_set0 = APU_ACC_CONFG_SET7;
		ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_PARKING,
						V_TOP_IOMMU);
		break;
	default:
		ERROR("[APUPWR] %s %d invalid domain (%d)\n",
		      __func__, __LINE__, domain);
		ret = -EIO;
		goto err;
	}

	anpu_pll_set_rate(domain, PLL_MODE, (div2) ? (freq * 2) : freq);

	if (div2) {
		apupwr_writel(BIT(BIT_SEL_APU_DIV2), acc_set0);
		if (acc_set1) {
			apupwr_writel(BIT(BIT_SEL_APU_DIV2), acc_set1);
		}
	}

	/*
	 * Switch back to APUPLL
	 * Only switch back to APUPLL while CG_APU on
	 * And clksrc is not APUPLL
	 */
	switch (domain) {
	case V_VPU0:
	case V_VPU1:
		if ((apupwr_readl(acc_set0) & BIT(BIT_CGEN_APU)) &&
		     !(apupwr_readl(acc_set0) & BIT(BIT_SEL_APU))) {
			ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_APUPLL,
							V_VPU0);
		}
		if ((apupwr_readl(acc_set1) & BIT(BIT_CGEN_APU)) &&
		     !(apupwr_readl(acc_set1) & BIT(BIT_SEL_APU))) {
			ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_APUPLL,
							V_VPU1);
		}
		break;
	case V_MDLA0:
	case V_MDLA1:
		if ((apupwr_readl(acc_set0) & BIT(BIT_CGEN_APU)) &&
		     !(apupwr_readl(acc_set0) & BIT(BIT_SEL_APU))) {
			ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_APUPLL,
							V_MDLA0);
		}
		if ((apupwr_readl(acc_set1) & BIT(BIT_CGEN_APU)) &&
		     !(apupwr_readl(acc_set1) & BIT(BIT_SEL_APU))) {
			ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_APUPLL,
							V_MDLA1);
		}
		break;
	case V_APU_CONN:
	case V_TOP_IOMMU:
		if ((apupwr_readl(acc_set0) & BIT(BIT_CGEN_APU)) &&
		    !(apupwr_readl(acc_set0) & BIT(BIT_SEL_APU))) {
			ret = apupwr_smc_acc_set_parent(DVFS_FREQ_ACC_APUPLL,
							domain);
		}
		break;
	default:
		ERROR("[APUPWR] %s %d invalid domain (%d)\n",
		      __func__, __LINE__, domain);
		ret = -EIO;
		break;
	}
	INFO("[%s][%d] set domain %d to freq %d\n",
	     __func__, __LINE__, domain, (div2) ? (freq * 2) : freq);

err:
	return ret;
}

int32_t apupwr_smc_bulk_pll(bool enable)
{
	int32_t ret = 0;
	int32_t pll_idx;

	if (enable) {
		for (pll_idx = APUPLL; pll_idx < APUPLL_MAX; pll_idx++) {
			ret = apu_pll_enable(pll_idx, enable, false);
			if (ret != 0) {
				goto err;
			}
		}
	} else {
		for (pll_idx = APUPLL2; pll_idx >= APUPLL; pll_idx--) {
			ret = apu_pll_enable(pll_idx, enable, false);
			if (ret != 0) {
				goto err;
			}
		}
	}

err:
	return ret;
}

void apupwr_smc_bus_prot_cg_on(void)
{
	apupwr_clrbits(AO_MD32_MNOC_MASK, APU_CSR_DUMMY_0);
}