blob: 370685e9e19b7836029058604b67fc5a57fb8623 [file] [log] [blame]
Peng Fanac2cb4d2021-08-07 16:01:10 +08001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Copyright 2021 NXP
4 */
5
6#include <log.h>
7#include <asm/io.h>
8#include <asm/arch/imx-regs.h>
9#include <linux/delay.h>
10
11#include "upower_api.h"
12
13#define UPOWER_AP_MU1_ADDR 0x29280000
Peng Fan869e3ce2023-01-31 16:42:22 +080014
15#define PS_RTD BIT(0)
16#define PS_DSP BIT(1)
17#define PS_A35_0 BIT(2)
18#define PS_A35_1 BIT(3)
19#define PS_L2 BIT(4)
20#define PS_FAST_NIC BIT(5)
21#define PS_APD_PERIPH BIT(6)
22#define PS_GPU3D BIT(7)
23#define PS_HIFI4 BIT(8)
24#define PS_DDR GENMASK(12, 9)
25#define PS_PXP_EPDC BIT(13)
26#define PS_MIPI_DSI BIT(14)
27#define PS_MIPI_CSI BIT(15)
28#define PS_NIC_LPAV BIT(16)
29#define PS_FUSION_AO BIT(17)
30#define PS_FUSE BIT(18)
31#define PS_UPOWER BIT(19)
32
Peng Fanac2cb4d2021-08-07 16:01:10 +080033static struct mu_type *muptr = (struct mu_type *)UPOWER_AP_MU1_ADDR;
34
35void upower_wait_resp(void)
36{
37 while (!(readl(&muptr->rsr) & BIT(0))) {
38 debug("%s: poll the mu:%x\n", __func__, readl(&muptr->rsr));
39 udelay(100);
40 }
41
42 upwr_txrx_isr();
43}
44
45u32 upower_status(int status)
46{
47 u32 ret = -1;
48
49 switch (status) {
50 case 0:
51 debug("%s: finished successfully!\n", __func__);
52 ret = 0;
53 break;
54 case -1:
55 printf("%s: memory allocation or resource failed!\n", __func__);
56 break;
57 case -2:
58 printf("%s: invalid argument!\n", __func__);
59 break;
60 case -3:
61 printf("%s: called in an invalid API state!\n", __func__);
62 break;
63 default:
64 printf("%s: invalid return status\n", __func__);
65 break;
66 }
67 return ret;
68}
69
70void user_upwr_rdy_callb(u32 soc, u32 vmajor, u32 vminor)
71{
72 printf("%s: soc=%x\n", __func__, soc);
73 printf("%s: RAM version:%d.%d\n", __func__, vmajor, vminor);
74}
75
76int upower_pmic_i2c_write(u32 reg_addr, u32 reg_val)
77{
78 int ret, ret_val;
79 enum upwr_resp err_code;
80
81 ret = upwr_xcp_i2c_access(0x32, 1, 1, reg_addr, reg_val, NULL);
82 if (ret) {
83 printf("pmic i2c write failed ret %d\n", ret);
84 return ret;
85 }
86
87 upower_wait_resp();
88 ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, &err_code, &ret_val, 1000);
89 if (ret != UPWR_REQ_OK) {
90 printf("i2c poll Failure %d, err_code %d, ret_val 0x%x\n", ret, err_code, ret_val);
91 return ret;
92 }
93
94 debug("PMIC write reg[0x%x], val[0x%x]\n", reg_addr, reg_val);
95
96 return 0;
97}
98
99int upower_pmic_i2c_read(u32 reg_addr, u32 *reg_val)
100{
101 int ret, ret_val;
102 enum upwr_resp err_code;
103
104 if (!reg_val)
105 return -1;
106
107 ret = upwr_xcp_i2c_access(0x32, -1, 1, reg_addr, 0, NULL);
108 if (ret) {
109 printf("pmic i2c read failed ret %d\n", ret);
110 return ret;
111 }
112
113 upower_wait_resp();
114 ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, &err_code, &ret_val, 1000);
115 if (ret != UPWR_REQ_OK) {
116 printf("i2c poll Failure %d, err_code %d, ret_val 0x%x\n", ret, err_code, ret_val);
117 return ret;
118 }
119
120 *reg_val = ret_val;
121
122 debug("PMIC read reg[0x%x], val[0x%x]\n", reg_addr, *reg_val);
123
124 return 0;
125}
126
127int upower_init(void)
128{
129 u32 fw_major, fw_minor, fw_vfixes;
130 u32 soc_id;
131 int status;
132
133 u32 swton;
134 u64 memon;
135 int ret, ret_val;
136
137 do {
138 status = upwr_init(1, muptr);
139 if (upower_status(status)) {
140 printf("%s: upower init failure\n", __func__);
141 break;
142 }
143
144 soc_id = upwr_rom_version(&fw_major, &fw_minor, &fw_vfixes);
145 if (!soc_id) {
146 printf("%s:, soc_id not initialized\n", __func__);
147 break;
148 }
149
150 printf("%s: soc_id=%d\n", __func__, soc_id);
151 printf("%s: version:%d.%d.%d\n", __func__, fw_major, fw_minor, fw_vfixes);
152
153 printf("%s: start uPower RAM service\n", __func__);
154 status = upwr_start(1, user_upwr_rdy_callb);
155 upower_wait_resp();
156 if (upower_status(status)) {
157 printf("%s: upower init failure\n", __func__);
158 break;
159 }
160 } while (0);
161
Peng Fan869e3ce2023-01-31 16:42:22 +0800162 swton = PS_UPOWER | PS_FUSE | PS_FUSION_AO | PS_NIC_LPAV | PS_PXP_EPDC | PS_DDR |
163 PS_HIFI4 | PS_GPU3D | PS_MIPI_DSI;
Peng Fanac2cb4d2021-08-07 16:01:10 +0800164 ret = upwr_pwm_power_on(&swton, NULL, NULL);
165 if (ret)
166 printf("Turn on switches fail %d\n", ret);
167 else
168 printf("Turn on switches ok\n");
169 upower_wait_resp();
170 ret = upwr_poll_req_status(UPWR_SG_PWRMGMT, NULL, NULL, &ret_val, 1000);
171 if (ret != UPWR_REQ_OK)
172 printf("Failure %d\n", ret);
173
174 memon = 0x3FFFFFFFFFFFFCUL;
175 ret = upwr_pwm_power_on(NULL, (const u32 *)&memon, NULL);
176 if (ret)
177 printf("Turn on memories fail %d\n", ret);
178 else
179 printf("Turn on memories ok\n");
180 upower_wait_resp();
181 ret = upwr_poll_req_status(UPWR_SG_PWRMGMT, NULL, NULL, &ret_val, 1000);
182 if (ret != UPWR_REQ_OK)
183 printf("Failure %d\n", ret);
184
185 mdelay(1);
186
187 ret = upwr_xcp_set_ddr_retention(APD_DOMAIN, 0, NULL);
188 if (ret)
189 printf("Clear DDR retention fail %d\n", ret);
190 else
191 printf("Clear DDR retention ok\n");
192
193 upower_wait_resp();
194
195 ret = upwr_poll_req_status(UPWR_SG_EXCEPT, NULL, NULL, &ret_val, 1000);
196 if (ret != UPWR_REQ_OK)
197 printf("Failure %d\n", ret);
198
199 return 0;
200}