blob: 0fdb62d057b55185a266b2bd1364130a9e9b9ae4 [file] [log] [blame]
Icenowy Zheng7508bef2018-07-21 20:41:12 +08001/*
Samuel Hollandf95b3682019-10-20 15:12:20 -05002 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
Icenowy Zheng7508bef2018-07-21 20:41:12 +08003 * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Andre Przywaraa920a772018-10-02 00:21:49 +01008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Andre Przywaraa920a772018-10-02 00:21:49 +010010#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
Samuel Holland56147892019-10-20 20:50:57 -050013#include <drivers/allwinner/axp.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/allwinner/sunxi_rsb.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <lib/mmio.h>
16
Andre Przywara51760752021-02-14 23:56:04 +000017#include <core_off_arisc.h>
Andre Przywaraa920a772018-10-02 00:21:49 +010018#include <sunxi_def.h>
19#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010020#include <sunxi_private.h>
Icenowy Zheng7508bef2018-07-21 20:41:12 +080021
Andre Przywaraa920a772018-10-02 00:21:49 +010022static enum pmic_type {
Samuel Hollandf95b3682019-10-20 15:12:20 -050023 UNKNOWN,
Andre Przywaraa920a772018-10-02 00:21:49 +010024 GENERIC_H5,
25 GENERIC_A64,
Andre Przywara74f7a952018-10-02 00:21:53 +010026 REF_DESIGN_H5, /* regulators controlled by GPIO pins on port L */
Andre Przywara7f3c0792018-09-15 01:18:49 +010027 AXP803_RSB, /* PMIC connected via RSB on most A64 boards */
Andre Przywaraa920a772018-10-02 00:21:49 +010028} pmic;
29
Andre Przywara7f3c0792018-09-15 01:18:49 +010030#define AXP803_HW_ADDR 0x3a3
31#define AXP803_RT_ADDR 0x2d
32
Andre Przywaraa920a772018-10-02 00:21:49 +010033/*
34 * On boards without a proper PMIC we struggle to turn off the system properly.
35 * Try to turn off as much off the system as we can, to reduce power
36 * consumption. This should be entered with only one core running and SMP
37 * disabled.
38 * This function only cares about peripherals.
39 */
Samuel Holland76780602019-10-20 20:00:27 -050040static void sunxi_turn_off_soc(uint16_t socid)
Icenowy Zheng7508bef2018-07-21 20:41:12 +080041{
Andre Przywaraa920a772018-10-02 00:21:49 +010042 int i;
43
44 /** Turn off most peripherals, most importantly DRAM users. **/
45 /* Keep DRAM controller running for now. */
46 mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14));
47 mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14));
48 /* Contains msgbox (bit 21) and spinlock (bit 22) */
49 mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0);
50 mmio_write_32(SUNXI_CCU_BASE + 0x64, 0);
51 mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0);
52 /* Keep PIO controller running for now. */
53 mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5)));
54 mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0);
55 /* Contains UART0 (bit 16) */
56 mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0);
57 mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0);
58 mmio_write_32(SUNXI_CCU_BASE + 0x70, 0);
59
60 /** Turn off DRAM controller. **/
61 mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14));
62 mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14));
63
64 /** Migrate CPU and bus clocks away from the PLLs. **/
65 /* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */
66 mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000);
67 /* APB2: use OSC24M */
68 mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000);
69 /* AHB2: use AHB1 clock */
70 mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0);
71 /* CPU: use OSC24M */
72 mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000);
Icenowy Zheng7508bef2018-07-21 20:41:12 +080073
Andre Przywaraa920a772018-10-02 00:21:49 +010074 /** Turn off PLLs. **/
75 for (i = 0; i < 6; i++)
76 mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31));
77 switch (socid) {
78 case SUNXI_SOC_H5:
79 mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31));
80 break;
81 case SUNXI_SOC_A64:
82 mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31));
83 mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31));
84 break;
85 }
86}
87
Andre Przywara7f3c0792018-09-15 01:18:49 +010088static int rsb_init(void)
89{
90 int ret;
91
92 ret = rsb_init_controller();
93 if (ret)
94 return ret;
95
Samuel Holland40b19ae2020-12-13 22:53:02 -060096 /* Switch to the recommended 3 MHz bus clock. */
97 ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
Andre Przywara7f3c0792018-09-15 01:18:49 +010098 if (ret)
99 return ret;
100
Samuel Holland2d47ab22020-12-13 22:43:15 -0600101 /* Initiate an I2C transaction to switch the PMIC to RSB mode. */
102 ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
Andre Przywara7f3c0792018-09-15 01:18:49 +0100103 if (ret)
104 return ret;
105
Andre Przywara7f3c0792018-09-15 01:18:49 +0100106 /* Associate the 8-bit runtime address with the 12-bit bus address. */
Samuel Holland56147892019-10-20 20:50:57 -0500107 ret = rsb_assign_runtime_address(AXP803_HW_ADDR,
108 AXP803_RT_ADDR);
109 if (ret)
Andre Przywara7f3c0792018-09-15 01:18:49 +0100110 return ret;
111
Samuel Holland56147892019-10-20 20:50:57 -0500112 return axp_check_id();
Andre Przywara6ec3dd52018-09-16 11:24:05 +0100113}
114
Samuel Holland56147892019-10-20 20:50:57 -0500115int axp_read(uint8_t reg)
Andre Przywarae28d4ce2018-09-16 11:24:34 +0100116{
Samuel Holland56147892019-10-20 20:50:57 -0500117 return rsb_read(AXP803_RT_ADDR, reg);
Andre Przywarae28d4ce2018-09-16 11:24:34 +0100118}
119
Samuel Holland56147892019-10-20 20:50:57 -0500120int axp_write(uint8_t reg, uint8_t val)
Andre Przywarae28d4ce2018-09-16 11:24:34 +0100121{
Samuel Holland56147892019-10-20 20:50:57 -0500122 return rsb_write(AXP803_RT_ADDR, reg, val);
Andre Przywara6ec3dd52018-09-16 11:24:05 +0100123}
124
Andre Przywara4e4b1e62018-09-08 19:18:37 +0100125int sunxi_pmic_setup(uint16_t socid, const void *fdt)
Andre Przywaraa920a772018-10-02 00:21:49 +0100126{
Andre Przywara7f3c0792018-09-15 01:18:49 +0100127 int ret;
128
Andre Przywaraa920a772018-10-02 00:21:49 +0100129 switch (socid) {
130 case SUNXI_SOC_H5:
Samuel Hollandf39fd862019-10-20 15:28:14 -0500131 NOTICE("PMIC: Assuming H5 reference regulator design\n");
132
Andre Przywara74f7a952018-10-02 00:21:53 +0100133 pmic = REF_DESIGN_H5;
Samuel Hollandf39fd862019-10-20 15:28:14 -0500134
Andre Przywaraa920a772018-10-02 00:21:49 +0100135 break;
136 case SUNXI_SOC_A64:
137 pmic = GENERIC_A64;
Samuel Hollandf39fd862019-10-20 15:28:14 -0500138
139 INFO("PMIC: Probing AXP803 on RSB\n");
140
Andre Przywara7f3c0792018-09-15 01:18:49 +0100141 ret = sunxi_init_platform_r_twi(socid, true);
142 if (ret)
143 return ret;
144
145 ret = rsb_init();
146 if (ret)
147 return ret;
148
149 pmic = AXP803_RSB;
Samuel Holland56147892019-10-20 20:50:57 -0500150 axp_setup_regulators(fdt);
Andre Przywara6ec3dd52018-09-16 11:24:05 +0100151
Samuel Holland2d47ab22020-12-13 22:43:15 -0600152 /* Switch the PMIC back to I2C mode. */
153 ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
154 if (ret)
155 return ret;
156
Andre Przywaraa920a772018-10-02 00:21:49 +0100157 break;
158 default:
Andre Przywaraa920a772018-10-02 00:21:49 +0100159 return -ENODEV;
160 }
Icenowy Zheng7508bef2018-07-21 20:41:12 +0800161 return 0;
162}
Icenowy Zhengbd57eb52018-07-22 21:52:50 +0800163
Samuel Hollandfa4d9352019-10-20 15:06:57 -0500164void sunxi_power_down(void)
Icenowy Zhengbd57eb52018-07-22 21:52:50 +0800165{
Andre Przywaraa920a772018-10-02 00:21:49 +0100166 switch (pmic) {
167 case GENERIC_H5:
168 /* Turn off as many peripherals and clocks as we can. */
169 sunxi_turn_off_soc(SUNXI_SOC_H5);
170 /* Turn off the pin controller now. */
171 mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
172 break;
173 case GENERIC_A64:
174 /* Turn off as many peripherals and clocks as we can. */
175 sunxi_turn_off_soc(SUNXI_SOC_A64);
176 /* Turn off the pin controller now. */
177 mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
178 break;
Andre Przywara74f7a952018-10-02 00:21:53 +0100179 case REF_DESIGN_H5:
180 sunxi_turn_off_soc(SUNXI_SOC_H5);
181
182 /*
183 * Switch PL pins to power off the board:
184 * - PL5 (VCC_IO) -> high
185 * - PL8 (PWR-STB = CPU power supply) -> low
186 * - PL9 (PWR-DRAM) ->low
187 * - PL10 (power LED) -> low
188 * Note: Clearing PL8 will reset the board, so keep it up.
189 */
190 sunxi_set_gpio_out('L', 5, 1);
191 sunxi_set_gpio_out('L', 9, 0);
192 sunxi_set_gpio_out('L', 10, 0);
193
194 /* Turn off pin controller now. */
195 mmio_write_32(SUNXI_CCU_BASE + 0x68, 0);
196
197 break;
Andre Przywara7f3c0792018-09-15 01:18:49 +0100198 case AXP803_RSB:
199 /* (Re-)init RSB in case the rich OS has disabled it. */
200 sunxi_init_platform_r_twi(SUNXI_SOC_A64, true);
201 rsb_init();
Samuel Holland56147892019-10-20 20:50:57 -0500202 axp_power_off();
Andre Przywara7f3c0792018-09-15 01:18:49 +0100203 break;
Andre Przywaraa920a772018-10-02 00:21:49 +0100204 default:
205 break;
206 }
207
Icenowy Zhengbd57eb52018-07-22 21:52:50 +0800208}
Andre Przywara51760752021-02-14 23:56:04 +0000209
210/* This lock synchronises access to the arisc management processor. */
211static DEFINE_BAKERY_LOCK(arisc_lock);
212
213/*
214 * If we are supposed to turn ourself off, tell the arisc SCP to do that
215 * work for us. Without any SCPI provider running there, we place some
216 * OpenRISC code into SRAM, put the address of that into the reset vector
217 * and release the arisc reset line. The SCP will wait for the core to enter
218 * WFI, then execute that code and pull the line up again.
219 * The code expects the core mask to be patched into the first instruction.
220 */
221void sunxi_cpu_power_off_self(void)
222{
223 u_register_t mpidr = read_mpidr();
224 unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
225 uintptr_t arisc_reset_vec = SUNXI_SRAM_A2_BASE + 0x100;
226 uint32_t *code = arisc_core_off;
227
228 do {
229 bakery_lock_get(&arisc_lock);
230 /* Wait until the arisc is in reset state. */
231 if (!(mmio_read_32(SUNXI_R_CPUCFG_BASE) & BIT(0)))
232 break;
233
234 bakery_lock_release(&arisc_lock);
235 } while (1);
236
237 /* Patch up the code to feed in an input parameter. */
238 code[0] = (code[0] & ~0xffff) | BIT_32(core);
239 clean_dcache_range((uintptr_t)code, sizeof(arisc_core_off));
240
241 /*
242 * The OpenRISC unconditional branch has opcode 0, the branch offset
243 * is in the lower 26 bits, containing the distance to the target,
244 * in instruction granularity (32 bits).
245 */
246 mmio_write_32(arisc_reset_vec, ((uintptr_t)code - arisc_reset_vec) / 4);
247 clean_dcache_range(arisc_reset_vec, 4);
248
249 /* De-assert the arisc reset line to let it run. */
250 mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0));
251
252 /*
253 * We release the lock here, although the arisc is still busy.
254 * But as long as it runs, the reset line is high, so other users
255 * won't leave the loop above.
256 * Once it has finished, the code is supposed to clear the reset line,
257 * to signal this to other users.
258 */
259 bakery_lock_release(&arisc_lock);
260}