xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_cpu_ops.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1333d66cfSSamuel Holland /*
2333d66cfSSamuel Holland  * Copyright (c) 2017-2018, 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>
8*09d40e0eSAntonio Nino Diaz 
9333d66cfSSamuel Holland #include <platform_def.h>
10*09d40e0eSAntonio Nino Diaz 
11*09d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
12*09d40e0eSAntonio Nino Diaz #include <common/debug.h>
13*09d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
14*09d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
15*09d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
16*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
17*09d40e0eSAntonio Nino Diaz 
18*09d40e0eSAntonio 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 
48333d66cfSSamuel Holland void sunxi_cpu_off(unsigned int cluster, unsigned int core)
49333d66cfSSamuel Holland {
507db0c960SAndre Przywara 	int corenr = cluster * PLATFORM_MAX_CPUS_PER_CLUSTER + core;
517db0c960SAndre Przywara 
5227f9616fSAndre Przywara 	VERBOSE("PSCI: Powering off cluster %d core %d\n", cluster, core);
53333d66cfSSamuel Holland 
54333d66cfSSamuel Holland 	/* Deassert DBGPWRDUP */
55333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
567db0c960SAndre Przywara 
577db0c960SAndre Przywara 	/* We can't turn ourself off like this, but it works for other cores. */
587db0c960SAndre Przywara 	if (plat_my_core_pos() != corenr) {
597db0c960SAndre Przywara 		/* Activate the core output clamps, but not for core 0. */
607db0c960SAndre Przywara 		if (corenr != 0)
617db0c960SAndre Przywara 			mmio_setbits_32(SUNXI_POWEROFF_GATING_REG(cluster),
627db0c960SAndre Przywara 					BIT(core));
63333d66cfSSamuel Holland 		/* Assert CPU power-on reset */
64333d66cfSSamuel Holland 		mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
65333d66cfSSamuel Holland 		/* Remove power from the CPU */
66333d66cfSSamuel Holland 		sunxi_cpu_disable_power(cluster, core);
677db0c960SAndre Przywara 
687db0c960SAndre Przywara 		return;
697db0c960SAndre Przywara 	}
707db0c960SAndre Przywara 
717db0c960SAndre Przywara 	/* Simplifies assembly, all SoCs so far are single cluster anyway. */
727db0c960SAndre Przywara 	assert(cluster == 0);
737db0c960SAndre Przywara 
747db0c960SAndre Przywara 	/*
757db0c960SAndre Przywara 	 * If we are supposed to turn ourself off, tell the arisc SCP
767db0c960SAndre Przywara 	 * to do that work for us. The code expects the core mask to be
777db0c960SAndre Przywara 	 * patched into the first instruction.
787db0c960SAndre Przywara 	 */
797db0c960SAndre Przywara 	sunxi_execute_arisc_code(arisc_core_off, sizeof(arisc_core_off),
807db0c960SAndre Przywara 				 0, BIT_32(core));
81333d66cfSSamuel Holland }
82333d66cfSSamuel Holland 
83333d66cfSSamuel Holland void sunxi_cpu_on(unsigned int cluster, unsigned int core)
84333d66cfSSamuel Holland {
8527f9616fSAndre Przywara 	VERBOSE("PSCI: Powering on cluster %d core %d\n", cluster, core);
86333d66cfSSamuel Holland 
87333d66cfSSamuel Holland 	/* Assert CPU core reset */
88333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core));
89333d66cfSSamuel Holland 	/* Assert CPU power-on reset */
90333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
91333d66cfSSamuel Holland 	/* Set CPU to start in AArch64 mode */
92333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_CPUCFG_CLS_CTRL_REG0(cluster), BIT(24 + core));
93333d66cfSSamuel Holland 	/* Apply power to the CPU */
94333d66cfSSamuel Holland 	sunxi_cpu_enable_power(cluster, core);
95333d66cfSSamuel Holland 	/* Release the core output clamps */
96333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core));
97333d66cfSSamuel Holland 	/* Deassert CPU power-on reset */
98333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
99333d66cfSSamuel Holland 	/* Deassert CPU core reset */
100333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core));
101333d66cfSSamuel Holland 	/* Assert DBGPWRDUP */
102333d66cfSSamuel Holland 	mmio_setbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
103333d66cfSSamuel Holland }
104333d66cfSSamuel Holland 
105333d66cfSSamuel Holland void sunxi_disable_secondary_cpus(unsigned int primary_cpu)
106333d66cfSSamuel Holland {
107333d66cfSSamuel Holland 	for (unsigned int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) {
108333d66cfSSamuel Holland 		if (cpu == primary_cpu)
109333d66cfSSamuel Holland 			continue;
110333d66cfSSamuel Holland 		sunxi_cpu_off(cpu / PLATFORM_MAX_CPUS_PER_CLUSTER,
111333d66cfSSamuel Holland 			       cpu % PLATFORM_MAX_CPUS_PER_CLUSTER);
112333d66cfSSamuel Holland 	}
113333d66cfSSamuel Holland }
114