sunxi_power.c 2.36 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

8
#include <errno.h>
9
10

#include <common/debug.h>
11
#include <drivers/allwinner/axp.h>
12
#include <drivers/allwinner/sunxi_rsb.h>
13
#include <lib/mmio.h>
14

15
#include <sunxi_cpucfg.h>
16
#include <sunxi_def.h>
17
#include <sunxi_mmap.h>
18
#include <sunxi_private.h>
19

20
21
#define AXP805_HW_ADDR	0x745
#define AXP805_RT_ADDR	0x3a
22

23
24
static enum pmic_type {
	UNKNOWN,
25
	AXP805,
26
} pmic;
27

28
int axp_read(uint8_t reg)
29
{
30
	return rsb_read(AXP805_RT_ADDR, reg);
31
32
}

33
int axp_write(uint8_t reg, uint8_t val)
34
{
35
	return rsb_write(AXP805_RT_ADDR, reg, val);
36
37
}

38
static int rsb_init(void)
39
40
41
{
	int ret;

42
	ret = rsb_init_controller();
43
44
	if (ret)
		return ret;
45

46
47
	/* Switch to the recommended 3 MHz bus clock. */
	ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
48
49
	if (ret)
		return ret;
50

51
52
53
54
55
56
57
58
59
60
61
	/* 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();
62
}
63

64
int sunxi_pmic_setup(uint16_t socid, const void *fdt)
65
{
66
67
	int ret;

68
	INFO("PMIC: Probing AXP805 on RSB\n");
69

70
	ret = sunxi_init_platform_r_twi(socid, true);
71
72
73
	if (ret)
		return ret;

74
75
76
	ret = rsb_init();
	if (ret)
		return ret;
77

78
79
	/* Switch the AXP805 to master/single-PMIC mode. */
	ret = axp_write(0xff, 0x0);
80
	if (ret)
81
82
83
		return ret;

	pmic = AXP805;
84
	axp_setup_regulators(fdt);
85

86
87
88
89
90
	/* Switch the PMIC back to I2C mode. */
	ret = axp_write(AXP20X_MODE_REG, AXP20X_MODE_I2C);
	if (ret)
		return ret;

91
92
	return 0;
}
93

94
void sunxi_power_down(void)
95
96
97
{
	switch (pmic) {
	case AXP805:
98
99
100
		/* (Re-)init RSB in case the rich OS has disabled it. */
		sunxi_init_platform_r_twi(SUNXI_SOC_H6, true);
		rsb_init();
101
		axp_power_off();
102
103
104
105
106
		break;
	default:
		break;
	}
}
107
108
109
110
111
112
113
114
115
116
117
118
119

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));
}