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 17*9227719dSAndre Przywara #include <core_off_arisc.h> 18f953c30fSAndre Przywara #include <sunxi_def.h> 19f953c30fSAndre Przywara #include <sunxi_mmap.h> 204ec1a239SAndre Przywara #include <sunxi_private.h> 21f953c30fSAndre Przywara 22f953c30fSAndre Przywara static enum pmic_type { 23c0e109f2SSamuel Holland UNKNOWN, 24f953c30fSAndre Przywara GENERIC_H5, 25f953c30fSAndre Przywara GENERIC_A64, 263d22228fSAndre Przywara REF_DESIGN_H5, /* regulators controlled by GPIO pins on port L */ 27eae5fe79SAndre Przywara AXP803_RSB, /* PMIC connected via RSB on most A64 boards */ 28f953c30fSAndre Przywara } pmic; 29f953c30fSAndre Przywara 30eae5fe79SAndre Przywara #define AXP803_HW_ADDR 0x3a3 31eae5fe79SAndre Przywara #define AXP803_RT_ADDR 0x2d 32eae5fe79SAndre Przywara 33f953c30fSAndre Przywara /* 34f953c30fSAndre Przywara * On boards without a proper PMIC we struggle to turn off the system properly. 35f953c30fSAndre Przywara * Try to turn off as much off the system as we can, to reduce power 36f953c30fSAndre Przywara * consumption. This should be entered with only one core running and SMP 37f953c30fSAndre Przywara * disabled. 38f953c30fSAndre Przywara * This function only cares about peripherals. 39f953c30fSAndre Przywara */ 40df77a954SSamuel Holland static void sunxi_turn_off_soc(uint16_t socid) 41f953c30fSAndre Przywara { 42f953c30fSAndre Przywara int i; 43f953c30fSAndre Przywara 44f953c30fSAndre Przywara /** Turn off most peripherals, most importantly DRAM users. **/ 45f953c30fSAndre Przywara /* Keep DRAM controller running for now. */ 46f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, ~BIT_32(14)); 47f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, ~BIT_32(14)); 48f953c30fSAndre Przywara /* Contains msgbox (bit 21) and spinlock (bit 22) */ 49f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x2c4, 0); 50f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x64, 0); 51f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x2c8, 0); 52f953c30fSAndre Przywara /* Keep PIO controller running for now. */ 53f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x68, ~(BIT_32(5))); 54f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x2d0, 0); 55f953c30fSAndre Przywara /* Contains UART0 (bit 16) */ 56f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x2d8, 0); 57f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x6c, 0); 58f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x70, 0); 59f953c30fSAndre Przywara 60f953c30fSAndre Przywara /** Turn off DRAM controller. **/ 61f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c0, BIT_32(14)); 62f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x60, BIT_32(14)); 63f953c30fSAndre Przywara 64f953c30fSAndre Przywara /** Migrate CPU and bus clocks away from the PLLs. **/ 65f953c30fSAndre Przywara /* AHB1: use OSC24M/1, APB1 = AHB1 / 2 */ 66f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x54, 0x1000); 67f953c30fSAndre Przywara /* APB2: use OSC24M */ 68f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x58, 0x1000000); 69f953c30fSAndre Przywara /* AHB2: use AHB1 clock */ 70f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x5c, 0); 71f953c30fSAndre Przywara /* CPU: use OSC24M */ 72f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x50, 0x10000); 73f953c30fSAndre Przywara 74f953c30fSAndre Przywara /** Turn off PLLs. **/ 75f953c30fSAndre Przywara for (i = 0; i < 6; i++) 76f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + i * 8, BIT(31)); 77f953c30fSAndre Przywara switch (socid) { 78f953c30fSAndre Przywara case SUNXI_SOC_H5: 79f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x44, BIT(31)); 80f953c30fSAndre Przywara break; 81f953c30fSAndre Przywara case SUNXI_SOC_A64: 82f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x2c, BIT(31)); 83f953c30fSAndre Przywara mmio_clrbits_32(SUNXI_CCU_BASE + 0x4c, BIT(31)); 84f953c30fSAndre Przywara break; 85f953c30fSAndre Przywara } 86f953c30fSAndre Przywara } 877c26b6ecSIcenowy Zheng 88eae5fe79SAndre Przywara static int rsb_init(void) 89eae5fe79SAndre Przywara { 90eae5fe79SAndre Przywara int ret; 91eae5fe79SAndre Przywara 92eae5fe79SAndre Przywara ret = rsb_init_controller(); 93eae5fe79SAndre Przywara if (ret) 94eae5fe79SAndre Przywara return ret; 95eae5fe79SAndre Przywara 96d6fdb52bSSamuel Holland /* Switch to the recommended 3 MHz bus clock. */ 97d6fdb52bSSamuel Holland ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000); 98eae5fe79SAndre Przywara if (ret) 99eae5fe79SAndre Przywara return ret; 100eae5fe79SAndre Przywara 10144702983SSamuel Holland /* Initiate an I2C transaction to switch the PMIC to RSB mode. */ 10244702983SSamuel Holland ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8); 103eae5fe79SAndre Przywara if (ret) 104eae5fe79SAndre Przywara return ret; 105eae5fe79SAndre Przywara 106eae5fe79SAndre Przywara /* Associate the 8-bit runtime address with the 12-bit bus address. */ 1070bc752c9SSamuel Holland ret = rsb_assign_runtime_address(AXP803_HW_ADDR, 108eae5fe79SAndre Przywara AXP803_RT_ADDR); 1090bc752c9SSamuel Holland if (ret) 110eae5fe79SAndre Przywara return ret; 111eae5fe79SAndre Przywara 1120bc752c9SSamuel Holland return axp_check_id(); 113eae5fe79SAndre Przywara } 114eae5fe79SAndre Przywara 1150bc752c9SSamuel Holland int axp_read(uint8_t reg) 116ed80c1e2SAndre Przywara { 1170bc752c9SSamuel Holland return rsb_read(AXP803_RT_ADDR, reg); 118ed80c1e2SAndre Przywara } 119ed80c1e2SAndre Przywara 1200bc752c9SSamuel Holland int axp_write(uint8_t reg, uint8_t val) 121fb4e9786SAndre Przywara { 1220bc752c9SSamuel Holland return rsb_write(AXP803_RT_ADDR, reg, val); 123ed80c1e2SAndre Przywara } 124ed80c1e2SAndre Przywara 125df301601SAndre Przywara int sunxi_pmic_setup(uint16_t socid, const void *fdt) 1267c26b6ecSIcenowy Zheng { 127eae5fe79SAndre Przywara int ret; 128eae5fe79SAndre Przywara 129f953c30fSAndre Przywara switch (socid) { 130f953c30fSAndre Przywara case SUNXI_SOC_H5: 1314538c498SSamuel Holland NOTICE("PMIC: Assuming H5 reference regulator design\n"); 1324538c498SSamuel Holland 1333d22228fSAndre Przywara pmic = REF_DESIGN_H5; 1344538c498SSamuel Holland 135f953c30fSAndre Przywara break; 136f953c30fSAndre Przywara case SUNXI_SOC_A64: 137f953c30fSAndre Przywara pmic = GENERIC_A64; 1384538c498SSamuel Holland 1394538c498SSamuel Holland INFO("PMIC: Probing AXP803 on RSB\n"); 1404538c498SSamuel Holland 141eae5fe79SAndre Przywara ret = sunxi_init_platform_r_twi(socid, true); 142eae5fe79SAndre Przywara if (ret) 143eae5fe79SAndre Przywara return ret; 144eae5fe79SAndre Przywara 145eae5fe79SAndre Przywara ret = rsb_init(); 146eae5fe79SAndre Przywara if (ret) 147eae5fe79SAndre Przywara return ret; 148eae5fe79SAndre Przywara 149eae5fe79SAndre Przywara pmic = AXP803_RSB; 1500bc752c9SSamuel Holland axp_setup_regulators(fdt); 151ed80c1e2SAndre Przywara 15244702983SSamuel Holland /* Switch the PMIC back to I2C mode. */ 15344702983SSamuel Holland ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C); 15444702983SSamuel Holland if (ret) 15544702983SSamuel Holland return ret; 15644702983SSamuel Holland 157f953c30fSAndre Przywara break; 158f953c30fSAndre Przywara default: 159f953c30fSAndre Przywara return -ENODEV; 160f953c30fSAndre Przywara } 1617c26b6ecSIcenowy Zheng return 0; 1627c26b6ecSIcenowy Zheng } 1635069c1cfSIcenowy Zheng 164818e6732SSamuel Holland void sunxi_power_down(void) 1655069c1cfSIcenowy Zheng { 166f953c30fSAndre Przywara switch (pmic) { 167f953c30fSAndre Przywara case GENERIC_H5: 168f953c30fSAndre Przywara /* Turn off as many peripherals and clocks as we can. */ 169f953c30fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_H5); 170f953c30fSAndre Przywara /* Turn off the pin controller now. */ 171f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 172f953c30fSAndre Przywara break; 173f953c30fSAndre Przywara case GENERIC_A64: 174f953c30fSAndre Przywara /* Turn off as many peripherals and clocks as we can. */ 175f953c30fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_A64); 176f953c30fSAndre Przywara /* Turn off the pin controller now. */ 177f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 178f953c30fSAndre Przywara break; 1793d22228fSAndre Przywara case REF_DESIGN_H5: 1803d22228fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_H5); 1813d22228fSAndre Przywara 1823d22228fSAndre Przywara /* 1833d22228fSAndre Przywara * Switch PL pins to power off the board: 1843d22228fSAndre Przywara * - PL5 (VCC_IO) -> high 1853d22228fSAndre Przywara * - PL8 (PWR-STB = CPU power supply) -> low 1863d22228fSAndre Przywara * - PL9 (PWR-DRAM) ->low 1873d22228fSAndre Przywara * - PL10 (power LED) -> low 1883d22228fSAndre Przywara * Note: Clearing PL8 will reset the board, so keep it up. 1893d22228fSAndre Przywara */ 1903d22228fSAndre Przywara sunxi_set_gpio_out('L', 5, 1); 1913d22228fSAndre Przywara sunxi_set_gpio_out('L', 9, 0); 1923d22228fSAndre Przywara sunxi_set_gpio_out('L', 10, 0); 1933d22228fSAndre Przywara 1943d22228fSAndre Przywara /* Turn off pin controller now. */ 1953d22228fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 1963d22228fSAndre Przywara 1973d22228fSAndre Przywara break; 198eae5fe79SAndre Przywara case AXP803_RSB: 199eae5fe79SAndre Przywara /* (Re-)init RSB in case the rich OS has disabled it. */ 200eae5fe79SAndre Przywara sunxi_init_platform_r_twi(SUNXI_SOC_A64, true); 201eae5fe79SAndre Przywara rsb_init(); 2020bc752c9SSamuel Holland axp_power_off(); 203eae5fe79SAndre Przywara break; 204f953c30fSAndre Przywara default: 205f953c30fSAndre Przywara break; 206f953c30fSAndre Przywara } 207f953c30fSAndre Przywara 2085069c1cfSIcenowy Zheng } 209*9227719dSAndre Przywara 210*9227719dSAndre Przywara /* This lock synchronises access to the arisc management processor. */ 211*9227719dSAndre Przywara static DEFINE_BAKERY_LOCK(arisc_lock); 212*9227719dSAndre Przywara 213*9227719dSAndre Przywara /* 214*9227719dSAndre Przywara * If we are supposed to turn ourself off, tell the arisc SCP to do that 215*9227719dSAndre Przywara * work for us. Without any SCPI provider running there, we place some 216*9227719dSAndre Przywara * OpenRISC code into SRAM, put the address of that into the reset vector 217*9227719dSAndre Przywara * and release the arisc reset line. The SCP will wait for the core to enter 218*9227719dSAndre Przywara * WFI, then execute that code and pull the line up again. 219*9227719dSAndre Przywara * The code expects the core mask to be patched into the first instruction. 220*9227719dSAndre Przywara */ 221*9227719dSAndre Przywara void sunxi_cpu_power_off_self(void) 222*9227719dSAndre Przywara { 223*9227719dSAndre Przywara u_register_t mpidr = read_mpidr(); 224*9227719dSAndre Przywara unsigned int core = MPIDR_AFFLVL0_VAL(mpidr); 225*9227719dSAndre Przywara uintptr_t arisc_reset_vec = SUNXI_SRAM_A2_BASE + 0x100; 226*9227719dSAndre Przywara uint32_t *code = arisc_core_off; 227*9227719dSAndre Przywara 228*9227719dSAndre Przywara do { 229*9227719dSAndre Przywara bakery_lock_get(&arisc_lock); 230*9227719dSAndre Przywara /* Wait until the arisc is in reset state. */ 231*9227719dSAndre Przywara if (!(mmio_read_32(SUNXI_R_CPUCFG_BASE) & BIT(0))) 232*9227719dSAndre Przywara break; 233*9227719dSAndre Przywara 234*9227719dSAndre Przywara bakery_lock_release(&arisc_lock); 235*9227719dSAndre Przywara } while (1); 236*9227719dSAndre Przywara 237*9227719dSAndre Przywara /* Patch up the code to feed in an input parameter. */ 238*9227719dSAndre Przywara code[0] = (code[0] & ~0xffff) | BIT_32(core); 239*9227719dSAndre Przywara clean_dcache_range((uintptr_t)code, sizeof(arisc_core_off)); 240*9227719dSAndre Przywara 241*9227719dSAndre Przywara /* 242*9227719dSAndre Przywara * The OpenRISC unconditional branch has opcode 0, the branch offset 243*9227719dSAndre Przywara * is in the lower 26 bits, containing the distance to the target, 244*9227719dSAndre Przywara * in instruction granularity (32 bits). 245*9227719dSAndre Przywara */ 246*9227719dSAndre Przywara mmio_write_32(arisc_reset_vec, ((uintptr_t)code - arisc_reset_vec) / 4); 247*9227719dSAndre Przywara clean_dcache_range(arisc_reset_vec, 4); 248*9227719dSAndre Przywara 249*9227719dSAndre Przywara /* De-assert the arisc reset line to let it run. */ 250*9227719dSAndre Przywara mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0)); 251*9227719dSAndre Przywara 252*9227719dSAndre Przywara /* 253*9227719dSAndre Przywara * We release the lock here, although the arisc is still busy. 254*9227719dSAndre Przywara * But as long as it runs, the reset line is high, so other users 255*9227719dSAndre Przywara * won't leave the loop above. 256*9227719dSAndre Przywara * Once it has finished, the code is supposed to clear the reset line, 257*9227719dSAndre Przywara * to signal this to other users. 258*9227719dSAndre Przywara */ 259*9227719dSAndre Przywara bakery_lock_release(&arisc_lock); 260*9227719dSAndre Przywara } 261