2018-07-21 13:41:12 +01:00
|
|
|
/*
|
2019-10-20 21:12:20 +01:00
|
|
|
* Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
|
2018-07-21 13:41:12 +01:00
|
|
|
* Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
2018-07-22 14:30:14 +01:00
|
|
|
#include <errno.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
|
|
|
|
#include <common/debug.h>
|
2019-10-21 03:34:38 +01:00
|
|
|
#include <drivers/allwinner/axp.h>
|
2020-12-14 04:34:10 +00:00
|
|
|
#include <drivers/allwinner/sunxi_rsb.h>
|
2021-02-14 23:56:04 +00:00
|
|
|
#include <lib/mmio.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
|
2021-02-14 23:56:04 +00:00
|
|
|
#include <sunxi_cpucfg.h>
|
2018-10-14 22:13:53 +01:00
|
|
|
#include <sunxi_def.h>
|
2018-07-22 14:30:14 +01:00
|
|
|
#include <sunxi_mmap.h>
|
2018-10-14 12:02:02 +01:00
|
|
|
#include <sunxi_private.h>
|
2018-07-22 14:30:14 +01:00
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
#define AXP805_HW_ADDR 0x745
|
|
|
|
#define AXP805_RT_ADDR 0x3a
|
2018-07-22 14:30:14 +01:00
|
|
|
|
2019-10-20 21:12:20 +01:00
|
|
|
static enum pmic_type {
|
|
|
|
UNKNOWN,
|
2018-07-22 14:30:14 +01:00
|
|
|
AXP805,
|
2019-10-20 21:12:20 +01:00
|
|
|
} pmic;
|
2018-07-22 14:30:14 +01:00
|
|
|
|
2019-10-21 03:34:38 +01:00
|
|
|
int axp_read(uint8_t reg)
|
2018-07-22 14:30:14 +01:00
|
|
|
{
|
2020-12-14 04:34:10 +00:00
|
|
|
return rsb_read(AXP805_RT_ADDR, reg);
|
2018-07-22 14:30:14 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 03:34:38 +01:00
|
|
|
int axp_write(uint8_t reg, uint8_t val)
|
2018-07-22 14:30:14 +01:00
|
|
|
{
|
2020-12-14 04:34:10 +00:00
|
|
|
return rsb_write(AXP805_RT_ADDR, reg, val);
|
2018-07-22 14:30:14 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
static int rsb_init(void)
|
2018-07-22 14:30:14 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
ret = rsb_init_controller();
|
2019-10-20 21:28:14 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-07-22 14:30:14 +01:00
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
/* Switch to the recommended 3 MHz bus clock. */
|
|
|
|
ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
|
2019-10-20 21:28:14 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-07-22 14:30:14 +01:00
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
/* Initiate an I2C transaction to switch the PMIC to RSB mode. */
|
|
|
|
ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Associate the 8-bit runtime address with the 12-bit bus address. */
|
|
|
|
ret = rsb_assign_runtime_address(AXP805_HW_ADDR, AXP805_RT_ADDR);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return axp_check_id();
|
2018-07-22 14:30:14 +01:00
|
|
|
}
|
2018-07-21 13:41:12 +01:00
|
|
|
|
2018-09-08 19:18:37 +01:00
|
|
|
int sunxi_pmic_setup(uint16_t socid, const void *fdt)
|
2018-07-21 13:41:12 +01:00
|
|
|
{
|
2018-07-22 14:30:14 +01:00
|
|
|
int ret;
|
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
INFO("PMIC: Probing AXP805 on RSB\n");
|
2019-10-20 21:28:14 +01:00
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
ret = sunxi_init_platform_r_twi(socid, true);
|
2019-10-20 21:28:14 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
ret = rsb_init();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-07-22 14:30:14 +01:00
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
/* Switch the AXP805 to master/single-PMIC mode. */
|
|
|
|
ret = axp_write(0xff, 0x0);
|
2018-07-22 14:30:14 +01:00
|
|
|
if (ret)
|
2019-10-20 21:12:20 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
pmic = AXP805;
|
2019-10-21 03:34:38 +01:00
|
|
|
axp_setup_regulators(fdt);
|
2018-07-21 13:41:12 +01:00
|
|
|
|
2020-12-14 04:34:10 +00:00
|
|
|
/* Switch the PMIC back to I2C mode. */
|
|
|
|
ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-07-21 13:41:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-22 14:52:50 +01:00
|
|
|
|
2019-10-20 21:06:57 +01:00
|
|
|
void sunxi_power_down(void)
|
2018-07-22 14:52:50 +01:00
|
|
|
{
|
|
|
|
switch (pmic) {
|
|
|
|
case AXP805:
|
2020-12-14 04:34:10 +00:00
|
|
|
/* (Re-)init RSB in case the rich OS has disabled it. */
|
|
|
|
sunxi_init_platform_r_twi(SUNXI_SOC_H6, true);
|
|
|
|
rsb_init();
|
2019-10-21 03:34:38 +01:00
|
|
|
axp_power_off();
|
2018-07-22 14:52:50 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 23:56:04 +00:00
|
|
|
|
|
|
|
void sunxi_cpu_power_off_self(void)
|
|
|
|
{
|
|
|
|
u_register_t mpidr = read_mpidr();
|
|
|
|
unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
|
|
|
|
|
|
|
|
/* Enable the CPUIDLE hardware (only really needs to be done once). */
|
|
|
|
mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0x16aa0000);
|
|
|
|
mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0xaa160001);
|
|
|
|
|
|
|
|
/* Trigger power off for this core. */
|
|
|
|
mmio_write_32(SUNXI_CORE_CLOSE_REG, BIT_32(core));
|
|
|
|
}
|