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 95*d6fdb52bSSamuel Holland /* Switch to the recommended 3 MHz bus clock. */ 96*d6fdb52bSSamuel Holland ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000); 97eae5fe79SAndre Przywara if (ret) 98eae5fe79SAndre Przywara return ret; 99eae5fe79SAndre Przywara 100eae5fe79SAndre Przywara /* 101eae5fe79SAndre Przywara * Initiate an I2C transaction to write 0x7c into register 0x3e, 102eae5fe79SAndre Przywara * switching the PMIC to RSB mode. 103eae5fe79SAndre Przywara */ 104eae5fe79SAndre Przywara ret = rsb_set_device_mode(0x7c3e00); 105eae5fe79SAndre Przywara if (ret) 106eae5fe79SAndre Przywara return ret; 107eae5fe79SAndre Przywara 108eae5fe79SAndre Przywara /* Associate the 8-bit runtime address with the 12-bit bus address. */ 1090bc752c9SSamuel Holland ret = rsb_assign_runtime_address(AXP803_HW_ADDR, 110eae5fe79SAndre Przywara AXP803_RT_ADDR); 1110bc752c9SSamuel Holland if (ret) 112eae5fe79SAndre Przywara return ret; 113eae5fe79SAndre Przywara 1140bc752c9SSamuel Holland return axp_check_id(); 115eae5fe79SAndre Przywara } 116eae5fe79SAndre Przywara 1170bc752c9SSamuel Holland int axp_read(uint8_t reg) 118ed80c1e2SAndre Przywara { 1190bc752c9SSamuel Holland return rsb_read(AXP803_RT_ADDR, reg); 120ed80c1e2SAndre Przywara } 121ed80c1e2SAndre Przywara 1220bc752c9SSamuel Holland int axp_write(uint8_t reg, uint8_t val) 123fb4e9786SAndre Przywara { 1240bc752c9SSamuel Holland return rsb_write(AXP803_RT_ADDR, reg, val); 125ed80c1e2SAndre Przywara } 126ed80c1e2SAndre Przywara 127df301601SAndre Przywara int sunxi_pmic_setup(uint16_t socid, const void *fdt) 1287c26b6ecSIcenowy Zheng { 129eae5fe79SAndre Przywara int ret; 130eae5fe79SAndre Przywara 131f953c30fSAndre Przywara switch (socid) { 132f953c30fSAndre Przywara case SUNXI_SOC_H5: 1334538c498SSamuel Holland NOTICE("PMIC: Assuming H5 reference regulator design\n"); 1344538c498SSamuel Holland 1353d22228fSAndre Przywara pmic = REF_DESIGN_H5; 1364538c498SSamuel Holland 137f953c30fSAndre Przywara break; 138f953c30fSAndre Przywara case SUNXI_SOC_A64: 139f953c30fSAndre Przywara pmic = GENERIC_A64; 1404538c498SSamuel Holland 1414538c498SSamuel Holland INFO("PMIC: Probing AXP803 on RSB\n"); 1424538c498SSamuel Holland 143eae5fe79SAndre Przywara ret = sunxi_init_platform_r_twi(socid, true); 144eae5fe79SAndre Przywara if (ret) 145eae5fe79SAndre Przywara return ret; 146eae5fe79SAndre Przywara 147eae5fe79SAndre Przywara ret = rsb_init(); 148eae5fe79SAndre Przywara if (ret) 149eae5fe79SAndre Przywara return ret; 150eae5fe79SAndre Przywara 151eae5fe79SAndre Przywara pmic = AXP803_RSB; 1520bc752c9SSamuel Holland axp_setup_regulators(fdt); 153ed80c1e2SAndre Przywara 154f953c30fSAndre Przywara break; 155f953c30fSAndre Przywara default: 156f953c30fSAndre Przywara return -ENODEV; 157f953c30fSAndre Przywara } 1587c26b6ecSIcenowy Zheng return 0; 1597c26b6ecSIcenowy Zheng } 1605069c1cfSIcenowy Zheng 161818e6732SSamuel Holland void sunxi_power_down(void) 1625069c1cfSIcenowy Zheng { 163f953c30fSAndre Przywara switch (pmic) { 164f953c30fSAndre Przywara case GENERIC_H5: 165f953c30fSAndre Przywara /* Turn off as many peripherals and clocks as we can. */ 166f953c30fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_H5); 167f953c30fSAndre Przywara /* Turn off the pin controller now. */ 168f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 169f953c30fSAndre Przywara break; 170f953c30fSAndre Przywara case GENERIC_A64: 171f953c30fSAndre Przywara /* Turn off as many peripherals and clocks as we can. */ 172f953c30fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_A64); 173f953c30fSAndre Przywara /* Turn off the pin controller now. */ 174f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 175f953c30fSAndre Przywara break; 1763d22228fSAndre Przywara case REF_DESIGN_H5: 1773d22228fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_H5); 1783d22228fSAndre Przywara 1793d22228fSAndre Przywara /* 1803d22228fSAndre Przywara * Switch PL pins to power off the board: 1813d22228fSAndre Przywara * - PL5 (VCC_IO) -> high 1823d22228fSAndre Przywara * - PL8 (PWR-STB = CPU power supply) -> low 1833d22228fSAndre Przywara * - PL9 (PWR-DRAM) ->low 1843d22228fSAndre Przywara * - PL10 (power LED) -> low 1853d22228fSAndre Przywara * Note: Clearing PL8 will reset the board, so keep it up. 1863d22228fSAndre Przywara */ 1873d22228fSAndre Przywara sunxi_set_gpio_out('L', 5, 1); 1883d22228fSAndre Przywara sunxi_set_gpio_out('L', 9, 0); 1893d22228fSAndre Przywara sunxi_set_gpio_out('L', 10, 0); 1903d22228fSAndre Przywara 1913d22228fSAndre Przywara /* Turn off pin controller now. */ 1923d22228fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 1933d22228fSAndre Przywara 1943d22228fSAndre Przywara break; 195eae5fe79SAndre Przywara case AXP803_RSB: 196eae5fe79SAndre Przywara /* (Re-)init RSB in case the rich OS has disabled it. */ 197eae5fe79SAndre Przywara sunxi_init_platform_r_twi(SUNXI_SOC_A64, true); 198eae5fe79SAndre Przywara rsb_init(); 1990bc752c9SSamuel Holland axp_power_off(); 200eae5fe79SAndre Przywara break; 201f953c30fSAndre Przywara default: 202f953c30fSAndre Przywara break; 203f953c30fSAndre Przywara } 204f953c30fSAndre Przywara 2055069c1cfSIcenowy Zheng } 206