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> 13*0bc752c9SSamuel 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 95eae5fe79SAndre Przywara /* Start with 400 KHz to issue the I2C->RSB switch command. */ 96eae5fe79SAndre Przywara ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 400000); 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 /* Now in RSB mode, switch to the recommended 3 MHz. */ 109eae5fe79SAndre Przywara ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000); 110eae5fe79SAndre Przywara if (ret) 111eae5fe79SAndre Przywara return ret; 112eae5fe79SAndre Przywara 113eae5fe79SAndre Przywara /* Associate the 8-bit runtime address with the 12-bit bus address. */ 114*0bc752c9SSamuel Holland ret = rsb_assign_runtime_address(AXP803_HW_ADDR, 115eae5fe79SAndre Przywara AXP803_RT_ADDR); 116*0bc752c9SSamuel Holland if (ret) 117eae5fe79SAndre Przywara return ret; 118eae5fe79SAndre Przywara 119*0bc752c9SSamuel Holland return axp_check_id(); 120eae5fe79SAndre Przywara } 121eae5fe79SAndre Przywara 122*0bc752c9SSamuel Holland int axp_read(uint8_t reg) 123ed80c1e2SAndre Przywara { 124*0bc752c9SSamuel Holland return rsb_read(AXP803_RT_ADDR, reg); 125ed80c1e2SAndre Przywara } 126ed80c1e2SAndre Przywara 127*0bc752c9SSamuel Holland int axp_write(uint8_t reg, uint8_t val) 128fb4e9786SAndre Przywara { 129*0bc752c9SSamuel Holland return rsb_write(AXP803_RT_ADDR, reg, val); 130ed80c1e2SAndre Przywara } 131ed80c1e2SAndre Przywara 132df301601SAndre Przywara int sunxi_pmic_setup(uint16_t socid, const void *fdt) 1337c26b6ecSIcenowy Zheng { 134eae5fe79SAndre Przywara int ret; 135eae5fe79SAndre Przywara 136f953c30fSAndre Przywara switch (socid) { 137f953c30fSAndre Przywara case SUNXI_SOC_H5: 1384538c498SSamuel Holland NOTICE("PMIC: Assuming H5 reference regulator design\n"); 1394538c498SSamuel Holland 1403d22228fSAndre Przywara pmic = REF_DESIGN_H5; 1414538c498SSamuel Holland 142f953c30fSAndre Przywara break; 143f953c30fSAndre Przywara case SUNXI_SOC_A64: 144f953c30fSAndre Przywara pmic = GENERIC_A64; 1454538c498SSamuel Holland 1464538c498SSamuel Holland INFO("PMIC: Probing AXP803 on RSB\n"); 1474538c498SSamuel Holland 148eae5fe79SAndre Przywara ret = sunxi_init_platform_r_twi(socid, true); 149eae5fe79SAndre Przywara if (ret) 150eae5fe79SAndre Przywara return ret; 151eae5fe79SAndre Przywara 152eae5fe79SAndre Przywara ret = rsb_init(); 153eae5fe79SAndre Przywara if (ret) 154eae5fe79SAndre Przywara return ret; 155eae5fe79SAndre Przywara 156eae5fe79SAndre Przywara pmic = AXP803_RSB; 157*0bc752c9SSamuel Holland axp_setup_regulators(fdt); 158ed80c1e2SAndre Przywara 159f953c30fSAndre Przywara break; 160f953c30fSAndre Przywara default: 161f953c30fSAndre Przywara return -ENODEV; 162f953c30fSAndre Przywara } 1637c26b6ecSIcenowy Zheng return 0; 1647c26b6ecSIcenowy Zheng } 1655069c1cfSIcenowy Zheng 166818e6732SSamuel Holland void sunxi_power_down(void) 1675069c1cfSIcenowy Zheng { 168f953c30fSAndre Przywara switch (pmic) { 169f953c30fSAndre Przywara case GENERIC_H5: 170f953c30fSAndre Przywara /* Turn off as many peripherals and clocks as we can. */ 171f953c30fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_H5); 172f953c30fSAndre Przywara /* Turn off the pin controller now. */ 173f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 174f953c30fSAndre Przywara break; 175f953c30fSAndre Przywara case GENERIC_A64: 176f953c30fSAndre Przywara /* Turn off as many peripherals and clocks as we can. */ 177f953c30fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_A64); 178f953c30fSAndre Przywara /* Turn off the pin controller now. */ 179f953c30fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 180f953c30fSAndre Przywara break; 1813d22228fSAndre Przywara case REF_DESIGN_H5: 1823d22228fSAndre Przywara sunxi_turn_off_soc(SUNXI_SOC_H5); 1833d22228fSAndre Przywara 1843d22228fSAndre Przywara /* 1853d22228fSAndre Przywara * Switch PL pins to power off the board: 1863d22228fSAndre Przywara * - PL5 (VCC_IO) -> high 1873d22228fSAndre Przywara * - PL8 (PWR-STB = CPU power supply) -> low 1883d22228fSAndre Przywara * - PL9 (PWR-DRAM) ->low 1893d22228fSAndre Przywara * - PL10 (power LED) -> low 1903d22228fSAndre Przywara * Note: Clearing PL8 will reset the board, so keep it up. 1913d22228fSAndre Przywara */ 1923d22228fSAndre Przywara sunxi_set_gpio_out('L', 5, 1); 1933d22228fSAndre Przywara sunxi_set_gpio_out('L', 9, 0); 1943d22228fSAndre Przywara sunxi_set_gpio_out('L', 10, 0); 1953d22228fSAndre Przywara 1963d22228fSAndre Przywara /* Turn off pin controller now. */ 1973d22228fSAndre Przywara mmio_write_32(SUNXI_CCU_BASE + 0x68, 0); 1983d22228fSAndre Przywara 1993d22228fSAndre Przywara break; 200eae5fe79SAndre Przywara case AXP803_RSB: 201eae5fe79SAndre Przywara /* (Re-)init RSB in case the rich OS has disabled it. */ 202eae5fe79SAndre Przywara sunxi_init_platform_r_twi(SUNXI_SOC_A64, true); 203eae5fe79SAndre Przywara rsb_init(); 204*0bc752c9SSamuel Holland axp_power_off(); 205eae5fe79SAndre Przywara break; 206f953c30fSAndre Przywara default: 207f953c30fSAndre Przywara break; 208f953c30fSAndre Przywara } 209f953c30fSAndre Przywara 2105069c1cfSIcenowy Zheng } 211