mtspmc.c 9.28 KB
Newer Older
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (c) 2019, MediaTek Inc. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <arch_helpers.h>
#include <cortex_a53.h>
#include <cortex_a73.h>
#include <common/debug.h>
#include <lib/mmio.h>
#include <platform_def.h>
#include <mcucfg.h>
#include <spm.h>
#include <drivers/delay_timer.h>
#include <mtspmc.h>

#include "mtspmc_private.h"


static void set_retention(int cluster, int tick)
{
	uint64_t cpuectlr;

	if (cluster)
		cpuectlr = read_a73_cpuectlr_el1();
	else
		cpuectlr = read_a53_cpuectlr_el1();

	cpuectlr &= ~0x7ULL;
	cpuectlr |= tick & 0x7;

	if (cluster)
		write_a73_cpuectlr_el1(cpuectlr);
	else
		write_a53_cpuectlr_el1(cpuectlr);
}

void spm_enable_cpu_auto_off(int cluster, int cpu)
{
	uintptr_t reg = per_cpu(cluster, cpu, MCUCFG_SPARK);

	set_retention(cluster, 1);
	mmio_clrbits_32(reg, SW_NO_WAIT_Q);
}

void spm_disable_cpu_auto_off(int cluster, int cpu)
{
	uintptr_t reg = per_cpu(cluster, cpu, MCUCFG_SPARK);

	mmio_setbits_32(reg, SW_NO_WAIT_Q);
	set_retention(cluster, 0);
}

void spm_set_cpu_power_off(int cluster, int cpu)
{
	mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON);
}

void spm_enable_cluster_auto_off(int cluster)
{
	assert(cluster);

	mmio_clrbits_32(MCUCFG_MP2_SPMC, SW_NO_WAIT_Q);
	mmio_clrbits_32(MCUCFG_MP2_COQ, BIT(0));

	mmio_clrbits_32(SPM_SPMC_DORMANT_ENABLE, MP1_SPMC_SRAM_DORMANT_EN);

	mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON);
}

void mcucfg_set_bootaddr(int cluster, int cpu, uintptr_t bootaddr)
{
	uintptr_t reg;
	const uintptr_t mp2_bootreg[] = {
			MCUCFG_MP2_RVADDR0, MCUCFG_MP2_RVADDR1,
			MCUCFG_MP2_RVADDR2, MCUCFG_MP2_RVADDR3 };

	if (cluster) {
		assert(cpu >= 0 && cpu < 4);
		reg = mp2_bootreg[cpu];
	} else {
		reg = per_cpu(cluster, cpu, MCUCFG_BOOTADDR);
	}

	mmio_write_32(reg, bootaddr);
}

uintptr_t mcucfg_get_bootaddr(int cluster, int cpu)
{
	uintptr_t reg;
	const uintptr_t mp2_bootreg[] = {
			MCUCFG_MP2_RVADDR0, MCUCFG_MP2_RVADDR1,
			MCUCFG_MP2_RVADDR2, MCUCFG_MP2_RVADDR3 };

	if (cluster) {
		assert(cpu >= 0 && cpu < 4);
		reg = mp2_bootreg[cpu];
	} else {
		reg = per_cpu(cluster, cpu, MCUCFG_BOOTADDR);
	}

	return mmio_read_32(reg);
}

void mcucfg_init_archstate(int cluster, int cpu, int arm64)
{
	uintptr_t reg;
	int i;

	reg = per_cluster(cluster, MCUCFG_INITARCH);
	i = cluster ? 16 : 12;

	mmio_setbits_32(reg, (arm64 & 1) << (i + cpu));
}

/**
 * Return power state of specified subsystem
 *
 * @mask: mask to SPM_PWR_STATUS to query the power state
 *        of one subsystem.
 * RETURNS:
 * 0 (the subsys was powered off)
 * 1 (the subsys was powered on)
 */
int spm_get_powerstate(uint32_t mask)
{
	return mmio_read_32(SPM_PWR_STATUS) & mask;
}

int spm_get_cluster_powerstate(int cluster)
{
	uint32_t mask;

	mask = cluster ? PWR_STATUS_MP1_CPUTOP : PWR_STATUS_MP0_CPUTOP;

	return spm_get_powerstate(mask);
}

int spm_get_cpu_powerstate(int cluster, int cpu)
{
	uint32_t i;

	/*
	 * a quick way to specify the mask of cpu[0-3]/cpu[4-7] in PWR_STATUS
	 * register which are the BITS[9:12](MP0_CPU0~3) and
	 * BITS[16:19](MP1_CPU0~3)
	 */
	i = (cluster) ? 16 : 9;
	i = 1 << (i + cpu);

	return spm_get_powerstate(i);
}

int spmc_init(void)
{
	/* enable SPM register control */
	mmio_write_32(SPM_POWERON_CONFIG_EN,
		      PROJECT_CODE | MD_BCLK_CG_EN | BCLK_CG_EN);

#if SPMC_MODE == 1
	INFO("SPM: enable SPMC mode\n");

	/* 0: SPMC mode  1: Legacy mode */
	mmio_write_32(SPM_BYPASS_SPMC, 0);

	mmio_clrbits_32(per_cluster(0, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON_2ND);

	mmio_clrbits_32(per_cpu(0, 0, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_clrbits_32(per_cpu(0, 1, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_clrbits_32(per_cpu(0, 2, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_clrbits_32(per_cpu(0, 3, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);

	mmio_setbits_32(per_cpu(0, 1, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
	mmio_setbits_32(per_cpu(0, 2, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
	mmio_setbits_32(per_cpu(0, 3, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
#endif

	mmio_clrbits_32(per_cluster(1, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_setbits_32(per_cluster(1, SPM_CLUSTER_PWR), PWRCTRL_PWR_RST_B);
	mmio_clrbits_32(per_cluster(1, SPM_CLUSTER_PWR), PWRCTRL_PWR_CLK_DIS);

	mmio_clrbits_32(per_cpu(1, 0, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_clrbits_32(per_cpu(1, 1, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_clrbits_32(per_cpu(1, 2, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);
	mmio_clrbits_32(per_cpu(1, 3, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);

	mmio_setbits_32(per_cpu(1, 0, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
	mmio_setbits_32(per_cpu(1, 1, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
	mmio_setbits_32(per_cpu(1, 2, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);
	mmio_setbits_32(per_cpu(1, 3, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);

	return 0;
}

/**
 * Power on a core with specified cluster and core index
 *
 * @cluster: the cluster ID of the CPU which to be powered on
 * @cpu: the CPU ID of the CPU which to be powered on
 */
void spm_poweron_cpu(int cluster, int cpu)
{
	INFO("spmc: power on core %d.%d\n", cluster, cpu);

	/* STA_POWER_ON */
	/* Start to turn on MP0_CPU0 */

	/* Set PWR_RST_B = 1 */
	mmio_setbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);

	/* Set PWR_ON = 1 */
	mmio_setbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON);

	/* Wait until MP0_CPU0_PWR_STA_MASK = 1 */
	while (!spm_get_cpu_powerstate(cluster, cpu))
		;

	/* Finish to turn on MP0_CPU0 */
	INFO("spmc: power on core %d.%d successfully\n", cluster, cpu);
}

/**
 * Power off a core with specified cluster and core index
 *
 * @cluster: the cluster ID of the CPU which to be powered off
 * @cpu: the CPU ID of the CPU which to be powered off
 */
void spm_poweroff_cpu(int cluster, int cpu)
{
	INFO("spmc: power off core %d.%d\n", cluster, cpu);

	/* Start to turn off MP0_CPU0 */
	/* Set PWR_ON_2ND = 0 */
	mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON_2ND);

	/* Set PWR_ON = 0 */
	mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_ON);

	/* Wait until MP0_CPU0_PWR_STA_MASK = 0 */
	while (spm_get_cpu_powerstate(cluster, cpu))
		;

	/* Set PWR_RST_B = 0 */
	mmio_clrbits_32(per_cpu(cluster, cpu, SPM_CPU_PWR), PWRCTRL_PWR_RST_B);

	/* Finish to turn off MP0_CPU0 */
	INFO("spmc: power off core %d.%d successfully\n", cluster, cpu);
}

/**
 * Power off a cluster with specified index
 *
 * @cluster: the cluster index which to be powered off
 */
void spm_poweroff_cluster(int cluster)
{
	uint32_t mask;
	uint32_t pwr_rst_ctl;

	INFO("spmc: power off cluster %d\n", cluster);

	/* Start to turn off MP0_CPUTOP */
	/* Set bus protect - step1 : 0 */
	mask = (cluster) ? MP1_CPUTOP_PROT_STEP1_0_MASK :
			   MP0_CPUTOP_PROT_STEP1_0_MASK;
	mmio_write_32(INFRA_TOPAXI_PROTECTEN_1_SET, mask);

	while ((mmio_read_32(INFRA_TOPAXI_PROTECTEN_STA1_1) & mask) != mask)
		;

	/* Set PWR_ON_2ND = 0 */
	mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
			PWRCTRL_PWR_ON_2ND);

	/* SPMC_DORMANT_ENABLE[0]=0 */
	mask = (cluster) ? MP1_SPMC_SRAM_DORMANT_EN : MP0_SPMC_SRAM_DORMANT_EN;
	mmio_clrbits_32(SPM_SPMC_DORMANT_ENABLE, mask);

	/* Set PWR_ON = 0" */
	mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON);

	/* Wait until MP0_CPUTOP_PWR_STA_MASK = 0 */
	while (spm_get_cluster_powerstate(cluster))
		;

	/* NOTE
	 * Following flow only for BIG core cluster. It was from
	 * application note but not covered in mtcmos_ctrl.c
	 */
	if (cluster) {
		pwr_rst_ctl = mmio_read_32(MCUCFG_MP2_PWR_RST_CTL);
		mmio_write_32(MCUCFG_MP2_PWR_RST_CTL,
				(pwr_rst_ctl & ~SW_RST_B) | TOPAON_APB_MASK);
	}

	/* CPU_EXT_BUCK_ISO[0]=1 */
	if (cluster)
		mmio_setbits_32(SPM_CPU_EXT_BUCK_ISO, MP1_EXT_BUCK_ISO);

	/* Finish to turn off MP0_CPUTOP */
	INFO("spmc: power off cluster %d successfully\n", cluster);
}

/**
 * Power on a cluster with specified index
 *
 * @cluster: the cluster index which to be powered on
 */
void spm_poweron_cluster(int cluster)
{
	uint32_t mask;
	uint32_t pwr_rst_ctl;

	INFO("spmc: power on cluster %d\n", cluster);

	/* Start to turn on MP1_CPUTOP */

	/* NOTE
	 * Following flow only for BIG core cluster. It was from
	 * application note but not covered in mtcmos_ctrl.c
	 */
	if (cluster) {
		mmio_clrbits_32(MCUCFG_MP2_PWR_RST_CTL, SW_RST_B);

		/* CPU_EXT_BUCK_ISO[1]=0 */
		/* Set mp<n>_vproc_ext_off to 0 to release vproc isolation control */
		mmio_clrbits_32(SPM_CPU_EXT_BUCK_ISO, MP1_EXT_BUCK_ISO);

		/* NOTE
		 * Following flow only for BIG core cluster. It was from
		 * application note but not covered in mtcmos_ctrl.c
		 */
		pwr_rst_ctl = mmio_read_32(MCUCFG_MP2_PWR_RST_CTL);
		mmio_write_32(MCUCFG_MP2_PWR_RST_CTL,
				(pwr_rst_ctl | SW_RST_B) & ~TOPAON_APB_MASK);
	}

	/* Set PWR_ON_2ND = 0 */
	mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
			PWRCTRL_PWR_ON_2ND);

	/* Set PWR_RST_B = 1 */
	mmio_setbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
			PWRCTRL_PWR_RST_B);

	/* Set PWR_CLK_DIS = 0 */
	mmio_clrbits_32(per_cluster(cluster, SPM_CLUSTER_PWR),
			PWRCTRL_PWR_CLK_DIS);

	/* Set PWR_ON = 1 */
	mmio_setbits_32(per_cluster(cluster, SPM_CLUSTER_PWR), PWRCTRL_PWR_ON);

	/* Wait until MP1_CPUTOP_PWR_STA_MASK = 1 */
	while (!spm_get_cluster_powerstate(cluster))
		;

	/* Release bus protect - step1 : 0 */
	mask = (cluster) ? MP1_CPUTOP_PROT_STEP1_0_MASK :
			   MP0_CPUTOP_PROT_STEP1_0_MASK;
	mmio_write_32(INFRA_TOPAXI_PROTECTEN_1_CLR, mask);

	/* Finish to turn on MP1_CPUTOP */
	INFO("spmc: power on cluster %d successfully\n", cluster);
}