xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_cpu_ops.c (revision 5d4bd66d2f893c2db972e56d2ac97ee5a066d6ad)
1333d66cfSSamuel Holland /*
2*5d4bd66dSSamuel Holland  * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
3333d66cfSSamuel Holland  *
4333d66cfSSamuel Holland  * SPDX-License-Identifier: BSD-3-Clause
5333d66cfSSamuel Holland  */
6333d66cfSSamuel Holland 
77db0c960SAndre Przywara #include <assert.h>
809d40e0eSAntonio Nino Diaz 
9333d66cfSSamuel Holland #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
1309d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
1409d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1509d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
1609d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1709d40e0eSAntonio Nino Diaz 
1809d40e0eSAntonio Nino Diaz #include <core_off_arisc.h>
19333d66cfSSamuel Holland #include <sunxi_cpucfg.h>
207db0c960SAndre Przywara #include <sunxi_mmap.h>
214ec1a239SAndre Przywara #include <sunxi_private.h>
22333d66cfSSamuel Holland 
23333d66cfSSamuel Holland static void sunxi_cpu_disable_power(unsigned int cluster, unsigned int core)
24333d66cfSSamuel Holland {
25333d66cfSSamuel Holland 	if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0xff)
26333d66cfSSamuel Holland 		return;
27333d66cfSSamuel Holland 
2827f9616fSAndre Przywara 	VERBOSE("PSCI: Disabling power to cluster %d core %d\n", cluster, core);
29333d66cfSSamuel Holland 
30333d66cfSSamuel Holland 	mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xff);
31333d66cfSSamuel Holland }
32333d66cfSSamuel Holland 
33333d66cfSSamuel Holland static void sunxi_cpu_enable_power(unsigned int cluster, unsigned int core)
34333d66cfSSamuel Holland {
35333d66cfSSamuel Holland 	if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0)
36333d66cfSSamuel Holland 		return;
37333d66cfSSamuel Holland 
3827f9616fSAndre Przywara 	VERBOSE("PSCI: Enabling power to cluster %d core %d\n", cluster, core);
39333d66cfSSamuel Holland 
40333d66cfSSamuel Holland 	/* Power enable sequence from original Allwinner sources */
41333d66cfSSamuel Holland 	mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xfe);
42333d66cfSSamuel Holland 	mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xf8);
43333d66cfSSamuel Holland 	mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xe0);
44333d66cfSSamuel Holland 	mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x80);
45333d66cfSSamuel Holland 	mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x00);
46333d66cfSSamuel Holland }
47333d66cfSSamuel Holland 
48*5d4bd66dSSamuel Holland void sunxi_cpu_off(u_register_t mpidr)
49333d66cfSSamuel Holland {
50*5d4bd66dSSamuel Holland 	unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr);
51*5d4bd66dSSamuel Holland 	unsigned int core    = MPIDR_AFFLVL0_VAL(mpidr);
527db0c960SAndre Przywara 
5327f9616fSAndre Przywara 	VERBOSE("PSCI: Powering off cluster %d core %d\n", cluster, core);
54333d66cfSSamuel Holland 
55333d66cfSSamuel Holland 	/* Deassert DBGPWRDUP */
56333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
577db0c960SAndre Przywara 
587db0c960SAndre Przywara 	/* We can't turn ourself off like this, but it works for other cores. */
59*5d4bd66dSSamuel Holland 	if (read_mpidr() != mpidr) {
607db0c960SAndre Przywara 		/* Activate the core output clamps, but not for core 0. */
61*5d4bd66dSSamuel Holland 		if (core != 0)
627db0c960SAndre Przywara 			mmio_setbits_32(SUNXI_POWEROFF_GATING_REG(cluster),
637db0c960SAndre Przywara 					BIT(core));
64333d66cfSSamuel Holland 		/* Assert CPU power-on reset */
65333d66cfSSamuel Holland 		mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
66333d66cfSSamuel Holland 		/* Remove power from the CPU */
67333d66cfSSamuel Holland 		sunxi_cpu_disable_power(cluster, core);
687db0c960SAndre Przywara 
697db0c960SAndre Przywara 		return;
707db0c960SAndre Przywara 	}
717db0c960SAndre Przywara 
727db0c960SAndre Przywara 	/* Simplifies assembly, all SoCs so far are single cluster anyway. */
737db0c960SAndre Przywara 	assert(cluster == 0);
747db0c960SAndre Przywara 
757db0c960SAndre Przywara 	/*
767db0c960SAndre Przywara 	 * If we are supposed to turn ourself off, tell the arisc SCP
777db0c960SAndre Przywara 	 * to do that work for us. The code expects the core mask to be
787db0c960SAndre Przywara 	 * patched into the first instruction.
797db0c960SAndre Przywara 	 */
807db0c960SAndre Przywara 	sunxi_execute_arisc_code(arisc_core_off, sizeof(arisc_core_off),
817db0c960SAndre Przywara 				 0, BIT_32(core));
82333d66cfSSamuel Holland }
83333d66cfSSamuel Holland 
84*5d4bd66dSSamuel Holland void sunxi_cpu_on(u_register_t mpidr)
85333d66cfSSamuel Holland {
86*5d4bd66dSSamuel Holland 	unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr);
87*5d4bd66dSSamuel Holland 	unsigned int core    = MPIDR_AFFLVL0_VAL(mpidr);
88*5d4bd66dSSamuel Holland 
8927f9616fSAndre Przywara 	VERBOSE("PSCI: Powering on cluster %d core %d\n", cluster, core);
90333d66cfSSamuel Holland 
91333d66cfSSamuel Holland 	/* Assert CPU core reset */
92333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core));
93333d66cfSSamuel Holland 	/* Assert CPU power-on reset */
94333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
95333d66cfSSamuel Holland 	/* Set CPU to start in AArch64 mode */
96333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_CPUCFG_CLS_CTRL_REG0(cluster), BIT(24 + core));
97333d66cfSSamuel Holland 	/* Apply power to the CPU */
98333d66cfSSamuel Holland 	sunxi_cpu_enable_power(cluster, core);
99333d66cfSSamuel Holland 	/* Release the core output clamps */
100333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core));
101333d66cfSSamuel Holland 	/* Deassert CPU power-on reset */
102333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
103333d66cfSSamuel Holland 	/* Deassert CPU core reset */
104333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core));
105333d66cfSSamuel Holland 	/* Assert DBGPWRDUP */
106333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
107333d66cfSSamuel Holland }
108333d66cfSSamuel Holland 
109*5d4bd66dSSamuel Holland void sunxi_disable_secondary_cpus(u_register_t primary_mpidr)
110333d66cfSSamuel Holland {
111*5d4bd66dSSamuel Holland 	unsigned int cluster;
112*5d4bd66dSSamuel Holland 	unsigned int core;
113*5d4bd66dSSamuel Holland 
114*5d4bd66dSSamuel Holland 	for (cluster = 0; cluster < PLATFORM_CLUSTER_COUNT; ++cluster) {
115*5d4bd66dSSamuel Holland 		for (core = 0; core < PLATFORM_MAX_CPUS_PER_CLUSTER; ++core) {
116*5d4bd66dSSamuel Holland 			u_register_t mpidr = (cluster << MPIDR_AFF1_SHIFT) |
117*5d4bd66dSSamuel Holland 					     (core    << MPIDR_AFF0_SHIFT) |
118*5d4bd66dSSamuel Holland 					     BIT(31);
119*5d4bd66dSSamuel Holland 			if (mpidr != primary_mpidr)
120*5d4bd66dSSamuel Holland 				sunxi_cpu_off(mpidr);
121*5d4bd66dSSamuel Holland 		}
122333d66cfSSamuel Holland 	}
123333d66cfSSamuel Holland }
124