xref: /rk3399_ARM-atf/plat/allwinner/sun50i_a64/sunxi_power.c (revision ed80c1e2b306d347c25adfe350aa8100a89d88b6)
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 
8eae5fe79SAndre Przywara #include <allwinner/sunxi_rsb.h>
95069c1cfSIcenowy Zheng #include <arch_helpers.h>
107c26b6ecSIcenowy Zheng #include <debug.h>
11f953c30fSAndre Przywara #include <delay_timer.h>
12f953c30fSAndre Przywara #include <errno.h>
13*ed80c1e2SAndre Przywara #include <libfdt.h>
14f953c30fSAndre Przywara #include <mmio.h>
15f953c30fSAndre Przywara #include <platform_def.h>
16f953c30fSAndre Przywara #include <sunxi_def.h>
17f953c30fSAndre Przywara #include <sunxi_mmap.h>
184ec1a239SAndre Przywara #include <sunxi_private.h>
19f953c30fSAndre Przywara 
20f953c30fSAndre Przywara static enum pmic_type {
21f953c30fSAndre Przywara 	GENERIC_H5,
22f953c30fSAndre Przywara 	GENERIC_A64,
233d22228fSAndre Przywara 	REF_DESIGN_H5,	/* regulators controlled by GPIO pins on port L */
24eae5fe79SAndre Przywara 	AXP803_RSB,	/* PMIC connected via RSB on most A64 boards */
25f953c30fSAndre Przywara } pmic;
26f953c30fSAndre Przywara 
27eae5fe79SAndre Przywara #define AXP803_HW_ADDR	0x3a3
28eae5fe79SAndre Przywara #define AXP803_RT_ADDR	0x2d
29eae5fe79SAndre Przywara 
30f953c30fSAndre Przywara /*
31f953c30fSAndre Przywara  * On boards without a proper PMIC we struggle to turn off the system properly.
32f953c30fSAndre Przywara  * Try to turn off as much off the system as we can, to reduce power
33f953c30fSAndre Przywara  * consumption. This should be entered with only one core running and SMP
34f953c30fSAndre Przywara  * disabled.
35f953c30fSAndre Przywara  * This function only cares about peripherals.
36f953c30fSAndre Przywara  */
37f953c30fSAndre Przywara void sunxi_turn_off_soc(uint16_t socid)
38f953c30fSAndre Przywara {
39f953c30fSAndre Przywara 	int i;
40f953c30fSAndre Przywara 
41f953c30fSAndre Przywara 	/** Turn off most peripherals, most importantly DRAM users. **/
42f953c30fSAndre Przywara 	/* Keep DRAM controller running for now. */
43f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14));
44f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14));
45f953c30fSAndre Przywara 	/* Contains msgbox (bit 21) and spinlock (bit 22) */
46f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0);
47f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x64, 0);
48f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0);
49f953c30fSAndre Przywara 	/* Keep PIO controller running for now. */
50f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5)));
51f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0);
52f953c30fSAndre Przywara 	/* Contains UART0 (bit 16) */
53f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0);
54f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0);
55f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x70, 0);
56f953c30fSAndre Przywara 
57f953c30fSAndre Przywara 	/** Turn off DRAM controller. **/
58f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14));
59f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14));
60f953c30fSAndre Przywara 
61f953c30fSAndre Przywara 	/** Migrate CPU and bus clocks away from the PLLs. **/
62f953c30fSAndre Przywara 	/* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */
63f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000);
64f953c30fSAndre Przywara 	/* APB2: use OSC24M */
65f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000);
66f953c30fSAndre Przywara 	/* AHB2: use AHB1 clock */
67f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0);
68f953c30fSAndre Przywara 	/* CPU: use OSC24M */
69f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000);
70f953c30fSAndre Przywara 
71f953c30fSAndre Przywara 	/** Turn off PLLs. **/
72f953c30fSAndre Przywara 	for (i = 0; i < 6; i++)
73f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31));
74f953c30fSAndre Przywara 	switch (socid) {
75f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
76f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31));
77f953c30fSAndre Przywara 		break;
78f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
79f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31));
80f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31));
81f953c30fSAndre Przywara 		break;
82f953c30fSAndre Przywara 	}
83f953c30fSAndre Przywara }
847c26b6ecSIcenowy Zheng 
85eae5fe79SAndre Przywara static int rsb_init(void)
86eae5fe79SAndre Przywara {
87eae5fe79SAndre Przywara 	int ret;
88eae5fe79SAndre Przywara 
89eae5fe79SAndre Przywara 	ret = rsb_init_controller();
90eae5fe79SAndre Przywara 	if (ret)
91eae5fe79SAndre Przywara 		return ret;
92eae5fe79SAndre Przywara 
93eae5fe79SAndre Przywara 	/* Start with 400 KHz to issue the I2C->RSB switch command. */
94eae5fe79SAndre Przywara 	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 400000);
95eae5fe79SAndre Przywara 	if (ret)
96eae5fe79SAndre Przywara 		return ret;
97eae5fe79SAndre Przywara 
98eae5fe79SAndre Przywara 	/*
99eae5fe79SAndre Przywara 	 * Initiate an I2C transaction to write 0x7c into register 0x3e,
100eae5fe79SAndre Przywara 	 * switching the PMIC to RSB mode.
101eae5fe79SAndre Przywara 	 */
102eae5fe79SAndre Przywara 	ret = rsb_set_device_mode(0x7c3e00);
103eae5fe79SAndre Przywara 	if (ret)
104eae5fe79SAndre Przywara 		return ret;
105eae5fe79SAndre Przywara 
106eae5fe79SAndre Przywara 	/* Now in RSB mode, switch to the recommended 3 MHz. */
107eae5fe79SAndre Przywara 	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
108eae5fe79SAndre Przywara 	if (ret)
109eae5fe79SAndre Przywara 		return ret;
110eae5fe79SAndre Przywara 
111eae5fe79SAndre Przywara 	/* Associate the 8-bit runtime address with the 12-bit bus address. */
112eae5fe79SAndre Przywara 	return rsb_assign_runtime_address(AXP803_HW_ADDR,
113eae5fe79SAndre Przywara 					  AXP803_RT_ADDR);
114eae5fe79SAndre Przywara }
115eae5fe79SAndre Przywara 
116eae5fe79SAndre Przywara static int axp_setbits(uint8_t reg, uint8_t set_mask)
117eae5fe79SAndre Przywara {
118eae5fe79SAndre Przywara 	uint8_t regval;
119eae5fe79SAndre Przywara 	int ret;
120eae5fe79SAndre Przywara 
121eae5fe79SAndre Przywara 	ret = rsb_read(AXP803_RT_ADDR, reg);
122eae5fe79SAndre Przywara 	if (ret < 0)
123eae5fe79SAndre Przywara 		return ret;
124eae5fe79SAndre Przywara 
125eae5fe79SAndre Przywara 	regval = ret | set_mask;
126eae5fe79SAndre Przywara 
127eae5fe79SAndre Przywara 	return rsb_write(AXP803_RT_ADDR, reg, regval);
128eae5fe79SAndre Przywara }
129eae5fe79SAndre Przywara 
130*ed80c1e2SAndre Przywara static bool should_enable_regulator(const void *fdt, int node)
131*ed80c1e2SAndre Przywara {
132*ed80c1e2SAndre Przywara 	if (fdt_getprop(fdt, node, "phandle", NULL) != NULL)
133*ed80c1e2SAndre Przywara 		return true;
134*ed80c1e2SAndre Przywara 	if (fdt_getprop(fdt, node, "regulator-always-on", NULL) != NULL)
135*ed80c1e2SAndre Przywara 		return true;
136*ed80c1e2SAndre Przywara 	return false;
137*ed80c1e2SAndre Przywara }
138*ed80c1e2SAndre Przywara 
139*ed80c1e2SAndre Przywara static void setup_axp803_rails(const void *fdt)
140*ed80c1e2SAndre Przywara {
141*ed80c1e2SAndre Przywara 	int node;
142*ed80c1e2SAndre Przywara 
143*ed80c1e2SAndre Przywara 	/* locate the PMIC DT node, bail out if not found */
144*ed80c1e2SAndre Przywara 	node = fdt_node_offset_by_compatible(fdt, -1, "x-powers,axp803");
145*ed80c1e2SAndre Przywara 	if (node == -FDT_ERR_NOTFOUND) {
146*ed80c1e2SAndre Przywara 		WARN("BL31: PMIC: No AXP803 DT node, skipping initial setup.\n");
147*ed80c1e2SAndre Przywara 		return;
148*ed80c1e2SAndre Przywara 	}
149*ed80c1e2SAndre Przywara 
150*ed80c1e2SAndre Przywara 	if (fdt_getprop(fdt, node, "x-powers,drive-vbus-en", NULL))
151*ed80c1e2SAndre Przywara 		axp_setbits(0x8f, BIT(4));
152*ed80c1e2SAndre Przywara 
153*ed80c1e2SAndre Przywara 	/* descend into the "regulators" subnode */
154*ed80c1e2SAndre Przywara 	node = fdt_first_subnode(fdt, node);
155*ed80c1e2SAndre Przywara 
156*ed80c1e2SAndre Przywara 	/* iterate over all regulators to find used ones */
157*ed80c1e2SAndre Przywara 	for (node = fdt_first_subnode(fdt, node);
158*ed80c1e2SAndre Przywara 	     node != -FDT_ERR_NOTFOUND;
159*ed80c1e2SAndre Przywara 	     node = fdt_next_subnode(fdt, node)) {
160*ed80c1e2SAndre Przywara 		const char *name;
161*ed80c1e2SAndre Przywara 		int length;
162*ed80c1e2SAndre Przywara 
163*ed80c1e2SAndre Przywara 		/* We only care if it's always on or referenced. */
164*ed80c1e2SAndre Przywara 		if (!should_enable_regulator(fdt, node))
165*ed80c1e2SAndre Przywara 			continue;
166*ed80c1e2SAndre Przywara 
167*ed80c1e2SAndre Przywara 		name = fdt_get_name(fdt, node, &length);
168*ed80c1e2SAndre Przywara 		if (!strncmp(name, "dc1sw", length)) {
169*ed80c1e2SAndre Przywara 			INFO("PMIC: AXP803: Enabling DC1SW\n");
170*ed80c1e2SAndre Przywara 			axp_setbits(0x12, BIT(7));
171*ed80c1e2SAndre Przywara 			continue;
172*ed80c1e2SAndre Przywara 		}
173*ed80c1e2SAndre Przywara 	}
174*ed80c1e2SAndre Przywara }
175*ed80c1e2SAndre Przywara 
176df301601SAndre Przywara int sunxi_pmic_setup(uint16_t socid, const void *fdt)
1777c26b6ecSIcenowy Zheng {
178eae5fe79SAndre Przywara 	int ret;
179eae5fe79SAndre Przywara 
180f953c30fSAndre Przywara 	switch (socid) {
181f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
1823d22228fSAndre Przywara 		pmic = REF_DESIGN_H5;
1833d22228fSAndre Przywara 		NOTICE("BL31: PMIC: Defaulting to PortL GPIO according to H5 reference design.\n");
184f953c30fSAndre Przywara 		break;
185f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
186f953c30fSAndre Przywara 		pmic = GENERIC_A64;
187eae5fe79SAndre Przywara 		ret = sunxi_init_platform_r_twi(socid, true);
188eae5fe79SAndre Przywara 		if (ret)
189eae5fe79SAndre Przywara 			return ret;
190eae5fe79SAndre Przywara 
191eae5fe79SAndre Przywara 		ret = rsb_init();
192eae5fe79SAndre Przywara 		if (ret)
193eae5fe79SAndre Przywara 			return ret;
194eae5fe79SAndre Przywara 
195eae5fe79SAndre Przywara 		pmic = AXP803_RSB;
196eae5fe79SAndre Przywara 		NOTICE("BL31: PMIC: Detected AXP803 on RSB.\n");
197eae5fe79SAndre Przywara 
198*ed80c1e2SAndre Przywara 		if (fdt)
199*ed80c1e2SAndre Przywara 			setup_axp803_rails(fdt);
200*ed80c1e2SAndre Przywara 
201f953c30fSAndre Przywara 		break;
202f953c30fSAndre Przywara 	default:
203f953c30fSAndre Przywara 		NOTICE("BL31: PMIC: No support for Allwinner %x SoC.\n", socid);
204f953c30fSAndre Przywara 		return -ENODEV;
205f953c30fSAndre Przywara 	}
2067c26b6ecSIcenowy Zheng 	return 0;
2077c26b6ecSIcenowy Zheng }
2085069c1cfSIcenowy Zheng 
2095069c1cfSIcenowy Zheng void __dead2 sunxi_power_down(void)
2105069c1cfSIcenowy Zheng {
211f953c30fSAndre Przywara 	switch (pmic) {
212f953c30fSAndre Przywara 	case GENERIC_H5:
213f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
214f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
215f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
216f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
217f953c30fSAndre Przywara 		break;
218f953c30fSAndre Przywara 	case GENERIC_A64:
219f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
220f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_A64);
221f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
222f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
223f953c30fSAndre Przywara 		break;
2243d22228fSAndre Przywara 	case REF_DESIGN_H5:
2253d22228fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
2263d22228fSAndre Przywara 
2273d22228fSAndre Przywara 		/*
2283d22228fSAndre Przywara 		 * Switch PL pins to power off the board:
2293d22228fSAndre Przywara 		 * - PL5 (VCC_IO) -> high
2303d22228fSAndre Przywara 		 * - PL8 (PWR-STB = CPU power supply) -> low
2313d22228fSAndre Przywara 		 * - PL9 (PWR-DRAM) ->low
2323d22228fSAndre Przywara 		 * - PL10 (power LED) -> low
2333d22228fSAndre Przywara 		 * Note: Clearing PL8 will reset the board, so keep it up.
2343d22228fSAndre Przywara 		 */
2353d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 5, 1);
2363d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 9, 0);
2373d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 10, 0);
2383d22228fSAndre Przywara 
2393d22228fSAndre Przywara 		/* Turn off pin controller now. */
2403d22228fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
2413d22228fSAndre Przywara 
2423d22228fSAndre Przywara 		break;
243eae5fe79SAndre Przywara 	case AXP803_RSB:
244eae5fe79SAndre Przywara 		/* (Re-)init RSB in case the rich OS has disabled it. */
245eae5fe79SAndre Przywara 		sunxi_init_platform_r_twi(SUNXI_SOC_A64, true);
246eae5fe79SAndre Przywara 		rsb_init();
247eae5fe79SAndre Przywara 
248eae5fe79SAndre Przywara 		/* Set "power disable control" bit */
249eae5fe79SAndre Przywara 		axp_setbits(0x32, BIT(7));
250eae5fe79SAndre Przywara 		break;
251f953c30fSAndre Przywara 	default:
252f953c30fSAndre Przywara 		break;
253f953c30fSAndre Przywara 	}
254f953c30fSAndre Przywara 
255f953c30fSAndre Przywara 	udelay(1000);
256f953c30fSAndre Przywara 	ERROR("PSCI: Cannot turn off system, halting.\n");
2575069c1cfSIcenowy Zheng 	wfi();
2585069c1cfSIcenowy Zheng 	panic();
2595069c1cfSIcenowy Zheng }
260