xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_cpu_ops.c (revision a1d349beb02f1d9d4cad7c1b5827ab737faff433)
1333d66cfSSamuel Holland /*
2*a1d349beSSamuel Holland  * Copyright (c) 2017-2021, 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*a1d349beSSamuel Holland /* We can't turn ourself off like this, but it works for other cores. */
49*a1d349beSSamuel Holland static void sunxi_cpu_off(u_register_t mpidr)
50333d66cfSSamuel Holland {
515d4bd66dSSamuel Holland 	unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr);
525d4bd66dSSamuel Holland 	unsigned int core    = MPIDR_AFFLVL0_VAL(mpidr);
537db0c960SAndre Przywara 
5427f9616fSAndre Przywara 	VERBOSE("PSCI: Powering off cluster %d core %d\n", cluster, core);
55333d66cfSSamuel Holland 
56333d66cfSSamuel Holland 	/* Deassert DBGPWRDUP */
57333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
587db0c960SAndre Przywara 	/* Activate the core output clamps, but not for core 0. */
595d4bd66dSSamuel Holland 	if (core != 0)
60*a1d349beSSamuel Holland 		mmio_setbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core));
61333d66cfSSamuel Holland 	/* Assert CPU power-on reset */
62333d66cfSSamuel Holland 	mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
63333d66cfSSamuel Holland 	/* Remove power from the CPU */
64333d66cfSSamuel Holland 	sunxi_cpu_disable_power(cluster, core);
657db0c960SAndre Przywara }
667db0c960SAndre Przywara 
67*a1d349beSSamuel Holland void sunxi_cpu_power_off_self(void)
68*a1d349beSSamuel Holland {
69*a1d349beSSamuel Holland 	u_register_t mpidr = read_mpidr();
70*a1d349beSSamuel Holland 	unsigned int core  = MPIDR_AFFLVL0_VAL(mpidr);
71*a1d349beSSamuel Holland 
727db0c960SAndre Przywara 	/* Simplifies assembly, all SoCs so far are single cluster anyway. */
73*a1d349beSSamuel Holland 	assert(MPIDR_AFFLVL1_VAL(mpidr) == 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),
815cffedceSSamuel Holland 				 BIT_32(core));
82333d66cfSSamuel Holland }
83333d66cfSSamuel Holland 
845d4bd66dSSamuel Holland void sunxi_cpu_on(u_register_t mpidr)
85333d66cfSSamuel Holland {
865d4bd66dSSamuel Holland 	unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr);
875d4bd66dSSamuel Holland 	unsigned int core    = MPIDR_AFFLVL0_VAL(mpidr);
885d4bd66dSSamuel 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*a1d349beSSamuel Holland void sunxi_cpu_power_off_others(void)
110333d66cfSSamuel Holland {
111*a1d349beSSamuel Holland 	u_register_t self = read_mpidr();
1125d4bd66dSSamuel Holland 	unsigned int cluster;
1135d4bd66dSSamuel Holland 	unsigned int core;
1145d4bd66dSSamuel Holland 
1155d4bd66dSSamuel Holland 	for (cluster = 0; cluster < PLATFORM_CLUSTER_COUNT; ++cluster) {
1165d4bd66dSSamuel Holland 		for (core = 0; core < PLATFORM_MAX_CPUS_PER_CLUSTER; ++core) {
1175d4bd66dSSamuel Holland 			u_register_t mpidr = (cluster << MPIDR_AFF1_SHIFT) |
1185d4bd66dSSamuel Holland 					     (core    << MPIDR_AFF0_SHIFT) |
1195d4bd66dSSamuel Holland 					     BIT(31);
120*a1d349beSSamuel Holland 			if (mpidr != self)
1215d4bd66dSSamuel Holland 				sunxi_cpu_off(mpidr);
1225d4bd66dSSamuel Holland 		}
123333d66cfSSamuel Holland 	}
124333d66cfSSamuel Holland }
125