xref: /rk3399_ARM-atf/plat/allwinner/sun50i_a64/sunxi_power.c (revision eae5fe79558c0c2744f74ca88970b16ce45450b1)
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 
8*eae5fe79SAndre 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>
13f953c30fSAndre Przywara #include <mmio.h>
14f953c30fSAndre Przywara #include <platform_def.h>
15f953c30fSAndre Przywara #include <sunxi_def.h>
16f953c30fSAndre Przywara #include <sunxi_mmap.h>
174ec1a239SAndre Przywara #include <sunxi_private.h>
18f953c30fSAndre Przywara 
19f953c30fSAndre Przywara static enum pmic_type {
20f953c30fSAndre Przywara 	GENERIC_H5,
21f953c30fSAndre Przywara 	GENERIC_A64,
223d22228fSAndre Przywara 	REF_DESIGN_H5,	/* regulators controlled by GPIO pins on port L */
23*eae5fe79SAndre Przywara 	AXP803_RSB,	/* PMIC connected via RSB on most A64 boards */
24f953c30fSAndre Przywara } pmic;
25f953c30fSAndre Przywara 
26*eae5fe79SAndre Przywara #define AXP803_HW_ADDR	0x3a3
27*eae5fe79SAndre Przywara #define AXP803_RT_ADDR	0x2d
28*eae5fe79SAndre Przywara 
29f953c30fSAndre Przywara /*
30f953c30fSAndre Przywara  * On boards without a proper PMIC we struggle to turn off the system properly.
31f953c30fSAndre Przywara  * Try to turn off as much off the system as we can, to reduce power
32f953c30fSAndre Przywara  * consumption. This should be entered with only one core running and SMP
33f953c30fSAndre Przywara  * disabled.
34f953c30fSAndre Przywara  * This function only cares about peripherals.
35f953c30fSAndre Przywara  */
36f953c30fSAndre Przywara void sunxi_turn_off_soc(uint16_t socid)
37f953c30fSAndre Przywara {
38f953c30fSAndre Przywara 	int i;
39f953c30fSAndre Przywara 
40f953c30fSAndre Przywara 	/** Turn off most peripherals, most importantly DRAM users. **/
41f953c30fSAndre Przywara 	/* Keep DRAM controller running for now. */
42f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14));
43f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14));
44f953c30fSAndre Przywara 	/* Contains msgbox (bit 21) and spinlock (bit 22) */
45f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0);
46f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x64, 0);
47f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0);
48f953c30fSAndre Przywara 	/* Keep PIO controller running for now. */
49f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5)));
50f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0);
51f953c30fSAndre Przywara 	/* Contains UART0 (bit 16) */
52f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0);
53f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0);
54f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x70, 0);
55f953c30fSAndre Przywara 
56f953c30fSAndre Przywara 	/** Turn off DRAM controller. **/
57f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14));
58f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14));
59f953c30fSAndre Przywara 
60f953c30fSAndre Przywara 	/** Migrate CPU and bus clocks away from the PLLs. **/
61f953c30fSAndre Przywara 	/* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */
62f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000);
63f953c30fSAndre Przywara 	/* APB2: use OSC24M */
64f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000);
65f953c30fSAndre Przywara 	/* AHB2: use AHB1 clock */
66f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0);
67f953c30fSAndre Przywara 	/* CPU: use OSC24M */
68f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000);
69f953c30fSAndre Przywara 
70f953c30fSAndre Przywara 	/** Turn off PLLs. **/
71f953c30fSAndre Przywara 	for (i = 0; i < 6; i++)
72f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31));
73f953c30fSAndre Przywara 	switch (socid) {
74f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
75f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31));
76f953c30fSAndre Przywara 		break;
77f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
78f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31));
79f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31));
80f953c30fSAndre Przywara 		break;
81f953c30fSAndre Przywara 	}
82f953c30fSAndre Przywara }
837c26b6ecSIcenowy Zheng 
84*eae5fe79SAndre Przywara static int rsb_init(void)
85*eae5fe79SAndre Przywara {
86*eae5fe79SAndre Przywara 	int ret;
87*eae5fe79SAndre Przywara 
88*eae5fe79SAndre Przywara 	ret = rsb_init_controller();
89*eae5fe79SAndre Przywara 	if (ret)
90*eae5fe79SAndre Przywara 		return ret;
91*eae5fe79SAndre Przywara 
92*eae5fe79SAndre Przywara 	/* Start with 400 KHz to issue the I2C->RSB switch command. */
93*eae5fe79SAndre Przywara 	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 400000);
94*eae5fe79SAndre Przywara 	if (ret)
95*eae5fe79SAndre Przywara 		return ret;
96*eae5fe79SAndre Przywara 
97*eae5fe79SAndre Przywara 	/*
98*eae5fe79SAndre Przywara 	 * Initiate an I2C transaction to write 0x7c into register 0x3e,
99*eae5fe79SAndre Przywara 	 * switching the PMIC to RSB mode.
100*eae5fe79SAndre Przywara 	 */
101*eae5fe79SAndre Przywara 	ret = rsb_set_device_mode(0x7c3e00);
102*eae5fe79SAndre Przywara 	if (ret)
103*eae5fe79SAndre Przywara 		return ret;
104*eae5fe79SAndre Przywara 
105*eae5fe79SAndre Przywara 	/* Now in RSB mode, switch to the recommended 3 MHz. */
106*eae5fe79SAndre Przywara 	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
107*eae5fe79SAndre Przywara 	if (ret)
108*eae5fe79SAndre Przywara 		return ret;
109*eae5fe79SAndre Przywara 
110*eae5fe79SAndre Przywara 	/* Associate the 8-bit runtime address with the 12-bit bus address. */
111*eae5fe79SAndre Przywara 	return rsb_assign_runtime_address(AXP803_HW_ADDR,
112*eae5fe79SAndre Przywara 					  AXP803_RT_ADDR);
113*eae5fe79SAndre Przywara }
114*eae5fe79SAndre Przywara 
115*eae5fe79SAndre Przywara static int axp_setbits(uint8_t reg, uint8_t set_mask)
116*eae5fe79SAndre Przywara {
117*eae5fe79SAndre Przywara 	uint8_t regval;
118*eae5fe79SAndre Przywara 	int ret;
119*eae5fe79SAndre Przywara 
120*eae5fe79SAndre Przywara 	ret = rsb_read(AXP803_RT_ADDR, reg);
121*eae5fe79SAndre Przywara 	if (ret < 0)
122*eae5fe79SAndre Przywara 		return ret;
123*eae5fe79SAndre Przywara 
124*eae5fe79SAndre Przywara 	regval = ret | set_mask;
125*eae5fe79SAndre Przywara 
126*eae5fe79SAndre Przywara 	return rsb_write(AXP803_RT_ADDR, reg, regval);
127*eae5fe79SAndre Przywara }
128*eae5fe79SAndre Przywara 
129fe57c7d4SAndre Przywara int sunxi_pmic_setup(uint16_t socid)
1307c26b6ecSIcenowy Zheng {
131*eae5fe79SAndre Przywara 	int ret;
132*eae5fe79SAndre Przywara 
133f953c30fSAndre Przywara 	switch (socid) {
134f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
1353d22228fSAndre Przywara 		pmic = REF_DESIGN_H5;
1363d22228fSAndre Przywara 		NOTICE("BL31: PMIC: Defaulting to PortL GPIO according to H5 reference design.\n");
137f953c30fSAndre Przywara 		break;
138f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
139f953c30fSAndre Przywara 		pmic = GENERIC_A64;
140*eae5fe79SAndre Przywara 		ret = sunxi_init_platform_r_twi(socid, true);
141*eae5fe79SAndre Przywara 		if (ret)
142*eae5fe79SAndre Przywara 			return ret;
143*eae5fe79SAndre Przywara 
144*eae5fe79SAndre Przywara 		ret = rsb_init();
145*eae5fe79SAndre Przywara 		if (ret)
146*eae5fe79SAndre Przywara 			return ret;
147*eae5fe79SAndre Przywara 
148*eae5fe79SAndre Przywara 		pmic = AXP803_RSB;
149*eae5fe79SAndre Przywara 		NOTICE("BL31: PMIC: Detected AXP803 on RSB.\n");
150*eae5fe79SAndre Przywara 
151f953c30fSAndre Przywara 		break;
152f953c30fSAndre Przywara 	default:
153f953c30fSAndre Przywara 		NOTICE("BL31: PMIC: No support for Allwinner %x SoC.\n", socid);
154f953c30fSAndre Przywara 		return -ENODEV;
155f953c30fSAndre Przywara 	}
1567c26b6ecSIcenowy Zheng 	return 0;
1577c26b6ecSIcenowy Zheng }
1585069c1cfSIcenowy Zheng 
1595069c1cfSIcenowy Zheng void __dead2 sunxi_power_down(void)
1605069c1cfSIcenowy Zheng {
161f953c30fSAndre Przywara 	switch (pmic) {
162f953c30fSAndre Przywara 	case GENERIC_H5:
163f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
164f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
165f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
166f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
167f953c30fSAndre Przywara 		break;
168f953c30fSAndre Przywara 	case GENERIC_A64:
169f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
170f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_A64);
171f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
172f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
173f953c30fSAndre Przywara 		break;
1743d22228fSAndre Przywara 	case REF_DESIGN_H5:
1753d22228fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
1763d22228fSAndre Przywara 
1773d22228fSAndre Przywara 		/*
1783d22228fSAndre Przywara 		 * Switch PL pins to power off the board:
1793d22228fSAndre Przywara 		 * - PL5 (VCC_IO) -> high
1803d22228fSAndre Przywara 		 * - PL8 (PWR-STB = CPU power supply) -> low
1813d22228fSAndre Przywara 		 * - PL9 (PWR-DRAM) ->low
1823d22228fSAndre Przywara 		 * - PL10 (power LED) -> low
1833d22228fSAndre Przywara 		 * Note: Clearing PL8 will reset the board, so keep it up.
1843d22228fSAndre Przywara 		 */
1853d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 5, 1);
1863d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 9, 0);
1873d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 10, 0);
1883d22228fSAndre Przywara 
1893d22228fSAndre Przywara 		/* Turn off pin controller now. */
1903d22228fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
1913d22228fSAndre Przywara 
1923d22228fSAndre Przywara 		break;
193*eae5fe79SAndre Przywara 	case AXP803_RSB:
194*eae5fe79SAndre Przywara 		/* (Re-)init RSB in case the rich OS has disabled it. */
195*eae5fe79SAndre Przywara 		sunxi_init_platform_r_twi(SUNXI_SOC_A64, true);
196*eae5fe79SAndre Przywara 		rsb_init();
197*eae5fe79SAndre Przywara 
198*eae5fe79SAndre Przywara 		/* Set "power disable control" bit */
199*eae5fe79SAndre Przywara 		axp_setbits(0x32, BIT(7));
200*eae5fe79SAndre Przywara 		break;
201f953c30fSAndre Przywara 	default:
202f953c30fSAndre Przywara 		break;
203f953c30fSAndre Przywara 	}
204f953c30fSAndre Przywara 
205f953c30fSAndre Przywara 	udelay(1000);
206f953c30fSAndre Przywara 	ERROR("PSCI: Cannot turn off system, halting.\n");
2075069c1cfSIcenowy Zheng 	wfi();
2085069c1cfSIcenowy Zheng 	panic();
2095069c1cfSIcenowy Zheng }
210