xref: /rk3399_ARM-atf/plat/allwinner/sun50i_a64/sunxi_power.c (revision 4ec1a2399cf6e182ba2828a40795912d20eca1ab)
17c26b6ecSIcenowy Zheng /*
27c26b6ecSIcenowy Zheng  * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
37c26b6ecSIcenowy Zheng  * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
47c26b6ecSIcenowy Zheng  *
57c26b6ecSIcenowy Zheng  * SPDX-License-Identifier: BSD-3-Clause
67c26b6ecSIcenowy Zheng  */
77c26b6ecSIcenowy Zheng 
85069c1cfSIcenowy Zheng #include <arch_helpers.h>
97c26b6ecSIcenowy Zheng #include <debug.h>
10f953c30fSAndre Przywara #include <delay_timer.h>
11f953c30fSAndre Przywara #include <errno.h>
12f953c30fSAndre Przywara #include <mmio.h>
13f953c30fSAndre Przywara #include <platform_def.h>
14f953c30fSAndre Przywara #include <sunxi_def.h>
15f953c30fSAndre Przywara #include <sunxi_mmap.h>
16*4ec1a239SAndre Przywara #include <sunxi_private.h>
17f953c30fSAndre Przywara 
18f953c30fSAndre Przywara static enum pmic_type {
19f953c30fSAndre Przywara 	GENERIC_H5,
20f953c30fSAndre Przywara 	GENERIC_A64,
21f953c30fSAndre Przywara } pmic;
22f953c30fSAndre Przywara 
23f953c30fSAndre Przywara /*
24f953c30fSAndre Przywara  * On boards without a proper PMIC we struggle to turn off the system properly.
25f953c30fSAndre Przywara  * Try to turn off as much off the system as we can, to reduce power
26f953c30fSAndre Przywara  * consumption. This should be entered with only one core running and SMP
27f953c30fSAndre Przywara  * disabled.
28f953c30fSAndre Przywara  * This function only cares about peripherals.
29f953c30fSAndre Przywara  */
30f953c30fSAndre Przywara void sunxi_turn_off_soc(uint16_t socid)
31f953c30fSAndre Przywara {
32f953c30fSAndre Przywara 	int i;
33f953c30fSAndre Przywara 
34f953c30fSAndre Przywara 	/** Turn off most peripherals, most importantly DRAM users. **/
35f953c30fSAndre Przywara 	/* Keep DRAM controller running for now. */
36f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14));
37f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14));
38f953c30fSAndre Przywara 	/* Contains msgbox (bit 21) and spinlock (bit 22) */
39f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0);
40f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x64, 0);
41f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0);
42f953c30fSAndre Przywara 	/* Keep PIO controller running for now. */
43f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5)));
44f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0);
45f953c30fSAndre Przywara 	/* Contains UART0 (bit 16) */
46f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0);
47f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0);
48f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x70, 0);
49f953c30fSAndre Przywara 
50f953c30fSAndre Przywara 	/** Turn off DRAM controller. **/
51f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14));
52f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14));
53f953c30fSAndre Przywara 
54f953c30fSAndre Przywara 	/** Migrate CPU and bus clocks away from the PLLs. **/
55f953c30fSAndre Przywara 	/* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */
56f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000);
57f953c30fSAndre Przywara 	/* APB2: use OSC24M */
58f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000);
59f953c30fSAndre Przywara 	/* AHB2: use AHB1 clock */
60f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0);
61f953c30fSAndre Przywara 	/* CPU: use OSC24M */
62f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000);
63f953c30fSAndre Przywara 
64f953c30fSAndre Przywara 	/** Turn off PLLs. **/
65f953c30fSAndre Przywara 	for (i = 0; i < 6; i++)
66f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31));
67f953c30fSAndre Przywara 	switch (socid) {
68f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
69f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31));
70f953c30fSAndre Przywara 		break;
71f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
72f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31));
73f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31));
74f953c30fSAndre Przywara 		break;
75f953c30fSAndre Przywara 	}
76f953c30fSAndre Przywara }
777c26b6ecSIcenowy Zheng 
78fe57c7d4SAndre Przywara int sunxi_pmic_setup(uint16_t socid)
797c26b6ecSIcenowy Zheng {
80f953c30fSAndre Przywara 	switch (socid) {
81f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
82f953c30fSAndre Przywara 		pmic = GENERIC_H5;
83f953c30fSAndre Przywara 		break;
84f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
85f953c30fSAndre Przywara 		pmic = GENERIC_A64;
86f953c30fSAndre Przywara 		break;
87f953c30fSAndre Przywara 	default:
88f953c30fSAndre Przywara 		NOTICE("BL31: PMIC: No support for Allwinner %x SoC.\n", socid);
89f953c30fSAndre Przywara 		return -ENODEV;
90f953c30fSAndre Przywara 	}
917c26b6ecSIcenowy Zheng 	return 0;
927c26b6ecSIcenowy Zheng }
935069c1cfSIcenowy Zheng 
945069c1cfSIcenowy Zheng void __dead2 sunxi_power_down(void)
955069c1cfSIcenowy Zheng {
96f953c30fSAndre Przywara 	switch (pmic) {
97f953c30fSAndre Przywara 	case GENERIC_H5:
98f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
99f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
100f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
101f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
102f953c30fSAndre Przywara 		break;
103f953c30fSAndre Przywara 	case GENERIC_A64:
104f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
105f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_A64);
106f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
107f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
108f953c30fSAndre Przywara 		break;
109f953c30fSAndre Przywara 	default:
110f953c30fSAndre Przywara 		break;
111f953c30fSAndre Przywara 	}
112f953c30fSAndre Przywara 
113f953c30fSAndre Przywara 	udelay(1000);
114f953c30fSAndre Przywara 	ERROR("PSCI: Cannot turn off system, halting.\n");
1155069c1cfSIcenowy Zheng 	wfi();
1165069c1cfSIcenowy Zheng 	panic();
1175069c1cfSIcenowy Zheng }
118