xref: /rk3399_ARM-atf/plat/allwinner/sun50i_a64/sunxi_power.c (revision 4470298333d0e453d5ac2f9beb73b6a3787edcce)
17c26b6ecSIcenowy Zheng /*
2c0e109f2SSamuel Holland  * Copyright (c) 2017-2019, 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 
8f953c30fSAndre Przywara #include <errno.h>
909d40e0eSAntonio Nino Diaz 
10f953c30fSAndre Przywara #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz 
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
130bc752c9SSamuel Holland #include <drivers/allwinner/axp.h>
1409d40e0eSAntonio Nino Diaz #include <drivers/allwinner/sunxi_rsb.h>
1509d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1609d40e0eSAntonio Nino Diaz 
17f953c30fSAndre Przywara #include <sunxi_def.h>
18f953c30fSAndre Przywara #include <sunxi_mmap.h>
194ec1a239SAndre Przywara #include <sunxi_private.h>
20f953c30fSAndre Przywara 
21f953c30fSAndre Przywara static enum pmic_type {
22c0e109f2SSamuel Holland 	UNKNOWN,
23f953c30fSAndre Przywara 	GENERIC_H5,
24f953c30fSAndre Przywara 	GENERIC_A64,
253d22228fSAndre Przywara 	REF_DESIGN_H5,	/* regulators controlled by GPIO pins on port L */
26eae5fe79SAndre Przywara 	AXP803_RSB,	/* PMIC connected via RSB on most A64 boards */
27f953c30fSAndre Przywara } pmic;
28f953c30fSAndre Przywara 
29eae5fe79SAndre Przywara #define AXP803_HW_ADDR	0x3a3
30eae5fe79SAndre Przywara #define AXP803_RT_ADDR	0x2d
31eae5fe79SAndre Przywara 
32f953c30fSAndre Przywara /*
33f953c30fSAndre Przywara  * On boards without a proper PMIC we struggle to turn off the system properly.
34f953c30fSAndre Przywara  * Try to turn off as much off the system as we can, to reduce power
35f953c30fSAndre Przywara  * consumption. This should be entered with only one core running and SMP
36f953c30fSAndre Przywara  * disabled.
37f953c30fSAndre Przywara  * This function only cares about peripherals.
38f953c30fSAndre Przywara  */
39df77a954SSamuel Holland static void sunxi_turn_off_soc(uint16_t socid)
40f953c30fSAndre Przywara {
41f953c30fSAndre Przywara 	int i;
42f953c30fSAndre Przywara 
43f953c30fSAndre Przywara 	/** Turn off most peripherals, most importantly DRAM users. **/
44f953c30fSAndre Przywara 	/* Keep DRAM controller running for now. */
45f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14));
46f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14));
47f953c30fSAndre Przywara 	/* Contains msgbox (bit 21) and spinlock (bit 22) */
48f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0);
49f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x64, 0);
50f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0);
51f953c30fSAndre Przywara 	/* Keep PIO controller running for now. */
52f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5)));
53f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0);
54f953c30fSAndre Przywara 	/* Contains UART0 (bit 16) */
55f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0);
56f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0);
57f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x70, 0);
58f953c30fSAndre Przywara 
59f953c30fSAndre Przywara 	/** Turn off DRAM controller. **/
60f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14));
61f953c30fSAndre Przywara 	mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14));
62f953c30fSAndre Przywara 
63f953c30fSAndre Przywara 	/** Migrate CPU and bus clocks away from the PLLs. **/
64f953c30fSAndre Przywara 	/* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */
65f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000);
66f953c30fSAndre Przywara 	/* APB2: use OSC24M */
67f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000);
68f953c30fSAndre Przywara 	/* AHB2: use AHB1 clock */
69f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0);
70f953c30fSAndre Przywara 	/* CPU: use OSC24M */
71f953c30fSAndre Przywara 	mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000);
72f953c30fSAndre Przywara 
73f953c30fSAndre Przywara 	/** Turn off PLLs. **/
74f953c30fSAndre Przywara 	for (i = 0; i < 6; i++)
75f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31));
76f953c30fSAndre Przywara 	switch (socid) {
77f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
78f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31));
79f953c30fSAndre Przywara 		break;
80f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
81f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31));
82f953c30fSAndre Przywara 		mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31));
83f953c30fSAndre Przywara 		break;
84f953c30fSAndre Przywara 	}
85f953c30fSAndre Przywara }
867c26b6ecSIcenowy Zheng 
87eae5fe79SAndre Przywara static int rsb_init(void)
88eae5fe79SAndre Przywara {
89eae5fe79SAndre Przywara 	int ret;
90eae5fe79SAndre Przywara 
91eae5fe79SAndre Przywara 	ret = rsb_init_controller();
92eae5fe79SAndre Przywara 	if (ret)
93eae5fe79SAndre Przywara 		return ret;
94eae5fe79SAndre Przywara 
95d6fdb52bSSamuel Holland 	/* Switch to the recommended 3 MHz bus clock. */
96d6fdb52bSSamuel Holland 	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
97eae5fe79SAndre Przywara 	if (ret)
98eae5fe79SAndre Przywara 		return ret;
99eae5fe79SAndre Przywara 
100*44702983SSamuel Holland 	/* Initiate an I2C transaction to switch the PMIC to RSB mode. */
101*44702983SSamuel Holland 	ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
102eae5fe79SAndre Przywara 	if (ret)
103eae5fe79SAndre Przywara 		return ret;
104eae5fe79SAndre Przywara 
105eae5fe79SAndre Przywara 	/* Associate the 8-bit runtime address with the 12-bit bus address. */
1060bc752c9SSamuel Holland 	ret = rsb_assign_runtime_address(AXP803_HW_ADDR,
107eae5fe79SAndre Przywara 					 AXP803_RT_ADDR);
1080bc752c9SSamuel Holland 	if (ret)
109eae5fe79SAndre Przywara 		return ret;
110eae5fe79SAndre Przywara 
1110bc752c9SSamuel Holland 	return axp_check_id();
112eae5fe79SAndre Przywara }
113eae5fe79SAndre Przywara 
1140bc752c9SSamuel Holland int axp_read(uint8_t reg)
115ed80c1e2SAndre Przywara {
1160bc752c9SSamuel Holland 	return rsb_read(AXP803_RT_ADDR, reg);
117ed80c1e2SAndre Przywara }
118ed80c1e2SAndre Przywara 
1190bc752c9SSamuel Holland int axp_write(uint8_t reg, uint8_t val)
120fb4e9786SAndre Przywara {
1210bc752c9SSamuel Holland 	return rsb_write(AXP803_RT_ADDR, reg, val);
122ed80c1e2SAndre Przywara }
123ed80c1e2SAndre Przywara 
124df301601SAndre Przywara int sunxi_pmic_setup(uint16_t socid, const void *fdt)
1257c26b6ecSIcenowy Zheng {
126eae5fe79SAndre Przywara 	int ret;
127eae5fe79SAndre Przywara 
128f953c30fSAndre Przywara 	switch (socid) {
129f953c30fSAndre Przywara 	case SUNXI_SOC_H5:
1304538c498SSamuel Holland 		NOTICE("PMIC: Assuming H5 reference regulator design\n");
1314538c498SSamuel Holland 
1323d22228fSAndre Przywara 		pmic = REF_DESIGN_H5;
1334538c498SSamuel Holland 
134f953c30fSAndre Przywara 		break;
135f953c30fSAndre Przywara 	case SUNXI_SOC_A64:
136f953c30fSAndre Przywara 		pmic = GENERIC_A64;
1374538c498SSamuel Holland 
1384538c498SSamuel Holland 		INFO("PMIC: Probing AXP803 on RSB\n");
1394538c498SSamuel Holland 
140eae5fe79SAndre Przywara 		ret = sunxi_init_platform_r_twi(socid, true);
141eae5fe79SAndre Przywara 		if (ret)
142eae5fe79SAndre Przywara 			return ret;
143eae5fe79SAndre Przywara 
144eae5fe79SAndre Przywara 		ret = rsb_init();
145eae5fe79SAndre Przywara 		if (ret)
146eae5fe79SAndre Przywara 			return ret;
147eae5fe79SAndre Przywara 
148eae5fe79SAndre Przywara 		pmic = AXP803_RSB;
1490bc752c9SSamuel Holland 		axp_setup_regulators(fdt);
150ed80c1e2SAndre Przywara 
151*44702983SSamuel Holland 		/* Switch the PMIC back to I2C mode. */
152*44702983SSamuel Holland 		ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
153*44702983SSamuel Holland 		if (ret)
154*44702983SSamuel Holland 			return ret;
155*44702983SSamuel Holland 
156f953c30fSAndre Przywara 		break;
157f953c30fSAndre Przywara 	default:
158f953c30fSAndre Przywara 		return -ENODEV;
159f953c30fSAndre Przywara 	}
1607c26b6ecSIcenowy Zheng 	return 0;
1617c26b6ecSIcenowy Zheng }
1625069c1cfSIcenowy Zheng 
163818e6732SSamuel Holland void sunxi_power_down(void)
1645069c1cfSIcenowy Zheng {
165f953c30fSAndre Przywara 	switch (pmic) {
166f953c30fSAndre Przywara 	case GENERIC_H5:
167f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
168f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
169f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
170f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
171f953c30fSAndre Przywara 		break;
172f953c30fSAndre Przywara 	case GENERIC_A64:
173f953c30fSAndre Przywara 		/* Turn off as many peripherals and clocks as we can. */
174f953c30fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_A64);
175f953c30fSAndre Przywara 		/* Turn off the pin controller now. */
176f953c30fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
177f953c30fSAndre Przywara 		break;
1783d22228fSAndre Przywara 	case REF_DESIGN_H5:
1793d22228fSAndre Przywara 		sunxi_turn_off_soc(SUNXI_SOC_H5);
1803d22228fSAndre Przywara 
1813d22228fSAndre Przywara 		/*
1823d22228fSAndre Przywara 		 * Switch PL pins to power off the board:
1833d22228fSAndre Przywara 		 * - PL5 (VCC_IO) -> high
1843d22228fSAndre Przywara 		 * - PL8 (PWR-STB = CPU power supply) -> low
1853d22228fSAndre Przywara 		 * - PL9 (PWR-DRAM) ->low
1863d22228fSAndre Przywara 		 * - PL10 (power LED) -> low
1873d22228fSAndre Przywara 		 * Note: Clearing PL8 will reset the board, so keep it up.
1883d22228fSAndre Przywara 		 */
1893d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 5, 1);
1903d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 9, 0);
1913d22228fSAndre Przywara 		sunxi_set_gpio_out('L', 10, 0);
1923d22228fSAndre Przywara 
1933d22228fSAndre Przywara 		/* Turn off pin controller now. */
1943d22228fSAndre Przywara 		mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
1953d22228fSAndre Przywara 
1963d22228fSAndre Przywara 		break;
197eae5fe79SAndre Przywara 	case AXP803_RSB:
198eae5fe79SAndre Przywara 		/* (Re-)init RSB in case the rich OS has disabled it. */
199eae5fe79SAndre Przywara 		sunxi_init_platform_r_twi(SUNXI_SOC_A64, true);
200eae5fe79SAndre Przywara 		rsb_init();
2010bc752c9SSamuel Holland 		axp_power_off();
202eae5fe79SAndre Przywara 		break;
203f953c30fSAndre Przywara 	default:
204f953c30fSAndre Przywara 		break;
205f953c30fSAndre Przywara 	}
206f953c30fSAndre Przywara 
2075069c1cfSIcenowy Zheng }
208