blob: 6873724f67fe8fe4e811c736fbfbe62cda8b39d8 [file] [log] [blame]
Andre Przywara2d42e5f2020-11-28 01:39:17 +00001/*
2 * Copyright (c) 2017-2020, ARM Limited. All rights reserved.
3 * Copyright (c) 2018, Icenowy Zheng <icenowy@aosc.io>
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include <errno.h>
9#include <string.h>
10
11#include <arch_helpers.h>
12#include <common/debug.h>
Andre Przywara1ad742c2024-03-21 14:54:36 +000013#include <common/fdt_wrappers.h>
Andre Przywara2d42e5f2020-11-28 01:39:17 +000014#include <drivers/allwinner/axp.h>
15#include <drivers/allwinner/sunxi_rsb.h>
16#include <lib/mmio.h>
Andre Przywara1ad742c2024-03-21 14:54:36 +000017#include <libfdt.h>
Andre Przywara2d42e5f2020-11-28 01:39:17 +000018
19#include <sunxi_cpucfg.h>
20#include <sunxi_def.h>
21#include <sunxi_mmap.h>
22#include <sunxi_private.h>
23
Andre Przywara1ad742c2024-03-21 14:54:36 +000024static uint16_t pmic_bus_addr;
25static uint8_t rsb_rt_addr;
Andre Przywara2d42e5f2020-11-28 01:39:17 +000026
27static enum pmic_type {
28 UNKNOWN,
29 AXP305,
30} pmic;
31
Andre Przywara1ad742c2024-03-21 14:54:36 +000032static uint8_t get_rsb_rt_address(uint16_t hw_addr)
33{
34 switch (hw_addr) {
35 case 0x745: return 0x3a;
36 }
37
38 return 0;
39}
40
Andre Przywara2d42e5f2020-11-28 01:39:17 +000041int axp_read(uint8_t reg)
42{
Andre Przywara1ad742c2024-03-21 14:54:36 +000043 return rsb_read(rsb_rt_addr, reg);
Andre Przywara2d42e5f2020-11-28 01:39:17 +000044}
45
46int axp_write(uint8_t reg, uint8_t val)
47{
Andre Przywara1ad742c2024-03-21 14:54:36 +000048 return rsb_write(rsb_rt_addr, reg, val);
Andre Przywara2d42e5f2020-11-28 01:39:17 +000049}
50
Andre Przywara1ad742c2024-03-21 14:54:36 +000051static int rsb_init(int rsb_hw_addr)
Andre Przywara2d42e5f2020-11-28 01:39:17 +000052{
53 int ret;
54
55 ret = rsb_init_controller();
Andre Przywara1ad742c2024-03-21 14:54:36 +000056 if (ret) {
Andre Przywara2d42e5f2020-11-28 01:39:17 +000057 return ret;
Andre Przywara1ad742c2024-03-21 14:54:36 +000058 }
Andre Przywara2d42e5f2020-11-28 01:39:17 +000059
60 /* Switch to the recommended 3 MHz bus clock. */
61 ret = rsb_set_bus_speed(SUNXI_OSC24M_CLK_IN_HZ, 3000000);
Andre Przywara1ad742c2024-03-21 14:54:36 +000062 if (ret) {
Andre Przywara2d42e5f2020-11-28 01:39:17 +000063 return ret;
Andre Przywara1ad742c2024-03-21 14:54:36 +000064 }
Andre Przywara2d42e5f2020-11-28 01:39:17 +000065
66 /* Initiate an I2C transaction to switch the PMIC to RSB mode. */
67 ret = rsb_set_device_mode(AXP20X_MODE_RSB << 16 | AXP20X_MODE_REG << 8);
Andre Przywara1ad742c2024-03-21 14:54:36 +000068 if (ret) {
Andre Przywara2d42e5f2020-11-28 01:39:17 +000069 return ret;
Andre Przywara1ad742c2024-03-21 14:54:36 +000070 }
Andre Przywara2d42e5f2020-11-28 01:39:17 +000071
72 /* Associate the 8-bit runtime address with the 12-bit bus address. */
Andre Przywara1ad742c2024-03-21 14:54:36 +000073 ret = rsb_assign_runtime_address(rsb_hw_addr, rsb_rt_addr);
74 if (ret) {
Andre Przywara2d42e5f2020-11-28 01:39:17 +000075 return ret;
Andre Przywara1ad742c2024-03-21 14:54:36 +000076 }
Andre Przywara2d42e5f2020-11-28 01:39:17 +000077
Andre Przywara1ad742c2024-03-21 14:54:36 +000078 return 0;
Andre Przywara2d42e5f2020-11-28 01:39:17 +000079}
80
Andre Przywara1ad742c2024-03-21 14:54:36 +000081static int pmic_bus_init(uint16_t socid, uint16_t rsb_hw_addr)
Andre Przywara2d42e5f2020-11-28 01:39:17 +000082{
83 int ret;
84
Andre Przywara2d42e5f2020-11-28 01:39:17 +000085 ret = sunxi_init_platform_r_twi(socid, true);
86 if (ret) {
87 INFO("Could not init platform bus: %d\n", ret);
Andre Przywara1ad742c2024-03-21 14:54:36 +000088 pmic = UNKNOWN;
Andre Przywara2d42e5f2020-11-28 01:39:17 +000089 return ret;
90 }
91
Andre Przywara1ad742c2024-03-21 14:54:36 +000092 ret = rsb_init(rsb_hw_addr);
Andre Przywara2d42e5f2020-11-28 01:39:17 +000093 if (ret) {
Andre Przywara1ad742c2024-03-21 14:54:36 +000094 pmic = UNKNOWN;
Andre Przywara2d42e5f2020-11-28 01:39:17 +000095 return ret;
96 }
97
Andre Przywara1ad742c2024-03-21 14:54:36 +000098 return 0;
99}
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000100
Andre Przywara1ad742c2024-03-21 14:54:36 +0000101int sunxi_pmic_setup(uint16_t socid, const void *fdt)
102{
103 int node, ret;
104 uint32_t reg;
105
106 node = fdt_node_offset_by_compatible(fdt, 0, "x-powers,axp806");
107 if (node >= 0) {
108 pmic = AXP305;
109 }
110
111 if (pmic == UNKNOWN) {
112 INFO("PMIC: No known PMIC in DT, skipping setup.\n");
113 return -ENODEV;
114 }
115
116 if (fdt_read_uint32(fdt, node, "reg", &reg)) {
117 ERROR("PMIC: PMIC DT node does not contain reg property.\n");
118 return -EINVAL;
119 }
120
121 pmic_bus_addr = reg;
122 rsb_rt_addr = get_rsb_rt_address(pmic_bus_addr);
123 if (rsb_rt_addr == 0) {
124 ERROR("PMIC: no mapping for RSB address 0x%x\n", reg);
125 return -EINVAL;
126 }
127
128 INFO("Probing for PMIC on RSB:\n");
129
130 ret = pmic_bus_init(socid, pmic_bus_addr);
131 if (ret) {
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000132 return ret;
Andre Przywara1ad742c2024-03-21 14:54:36 +0000133 }
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000134
Andre Przywara1ad742c2024-03-21 14:54:36 +0000135 ret = axp_read(0x03);
136 switch (ret & 0xcf) {
137 case 0x40: /* AXP305 */
138 if (pmic == AXP305) {
139 INFO("PMIC: found AXP305, setting up regulators\n");
140 axp_setup_regulators(fdt);
141 } else {
142 pmic = UNKNOWN;
143 }
144 break;
145 }
146
147 /* Switch the PMIC back to I2C mode. */
148 return rsb_write(rsb_rt_addr, AXP20X_MODE_REG, AXP20X_MODE_I2C);
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000149}
150
151void sunxi_power_down(void)
152{
Andre Przywara1ad742c2024-03-21 14:54:36 +0000153 int ret;
154
155 if (pmic == UNKNOWN) {
156 return;
157 }
158
159 /* Re-initialise after rich OS might have used it. */
160 ret = pmic_bus_init(SUNXI_SOC_H616, pmic_bus_addr);
161 if (ret) {
162 return;
163 }
164
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000165 switch (pmic) {
166 case AXP305:
Andre Przywara1ad742c2024-03-21 14:54:36 +0000167 axp_setbits(0x32, BIT(7));
Andre Przywara2d42e5f2020-11-28 01:39:17 +0000168 break;
169 default:
170 break;
171 }
172}
173
174void sunxi_cpu_power_off_self(void)
175{
176 u_register_t mpidr = read_mpidr();
177 unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
178
179 /* Enable the CPUIDLE hardware (only really needs to be done once). */
180 mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0x16aa0000);
181 mmio_write_32(SUNXI_CPUIDLE_EN_REG, 0xaa160001);
182
183 /* Trigger power off for this core. */
184 mmio_write_32(SUNXI_CORE_CLOSE_REG, BIT_32(core));
185}