-
Notifications
You must be signed in to change notification settings - Fork 12
/
hal_efuse.c
337 lines (309 loc) · 11.4 KB
/
hal_efuse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Disassemble hal_efuse.o pvvx 10.2016
*/
#include "rtl8195a.h"
#ifdef CONFIG_EFUSE_EN
#include "hal_efuse.h"
//#define NO_ROM_API
#define EFUSE_WRITE_ENABLE 0
#define EFUSE_SECTION_SIZE (1<<7) // 128 bytes
#define EFUSE_BUF_MAX_LEN (1<<5) // 32 bytes
#define OTP_START_ADDR EFUSE_SECTION_SIZE
#define OTP_BUF_MAX_LEN (1<<5) // 32 bytes
#define EFUSE_SECTION_CODE 11
#ifdef NO_ROM_API
//====================================================== Start libs ROM efuse
//----- HalEFUSEPowerSwitch8195AROM addr 0x6561
_LONG_CALL_ROM_ int HalEFUSEPowerSwitch8195AROM(IN unsigned char bWrite, IN unsigned char PwrState, IN unsigned char L25OutVoltage) {
if (PwrState == 1) {
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_EEPROM_CTRL0, (HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EEPROM_CTRL0) & 0xFFFFFF) | 0x69000000); // EFUSE_UNLOCK
if (!(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_FUNC_EN) & BIT_SYS_FEN_EELDR)) // REG_SYS_FUNC_EN BIT_SYS_FEN_EELDR ?
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_FUNC_EN, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_FUNC_EN) | BIT_SYS_FEN_EELDR);
if (!(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_CLK_CTRL0) & BIT_SYSON_CK_EELDR_EN)) // REG_SYS_CLK_CTRL0 BIT_SYSON_CK_EELDR_EN ?
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_CLK_CTRL0, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_CLK_CTRL0) | BIT_SYSON_CK_EELDR_EN);
if (!(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_CLK_CTRL1) & BIT_PESOC_EELDR_CK_SEL)) // REG_SYS_CLK_CTRL1 BIT_PESOC_EELDR_CK_SEL ?
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_CLK_CTRL1, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_CLK_CTRL1) | BIT_PESOC_EELDR_CK_SEL);
if (bWrite == 1)
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_REGU_CTRL0, (HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_REGU_CTRL0) & 0xFFFFF0FF) | BIT_SYS_REGU_LDO25E_EN | BIT_SYS_REGU_LDO25E_ADJ(L25OutVoltage));
}
else
{
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_EEPROM_CTRL0, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EEPROM_CTRL0) & 0xFFFFFF); // EFUSE_UNLOCK
if ( bWrite == 1 )
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_REGU_CTRL0, (HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_REGU_CTRL0) & (~BIT_SYS_REGU_LDO25E_EN)));
}
return bWrite;
}
//----- HALEFUSEOneByteReadROM addr 0x6561
_LONG_CALL_ROM_ int HALEFUSEOneByteReadROM(IN unsigned int CtrlSetting, IN unsigned short Addr, OUT unsigned char *Data, IN unsigned char L25OutVoltage)
{
int i = 0, result = 0;
if ( (Addr <= 0xFF) || ((CtrlSetting & 0xFFFF) == 0x26AE) ) {
HalEFUSEPowerSwitch8195AROM(1, 1, L25OutVoltage);
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_TEST, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_TEST) & (~BIT_SYS_EF_FORCE_PGMEN));
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL,
(CtrlSetting & (~(BIT_SYS_EF_RWFLAG | (BIT_MASK_SYS_EF_ADDR << BIT_SHIFT_SYS_EF_ADDR) | (BIT_MASK_SYS_EF_DATA << BIT_SHIFT_SYS_EF_DATA))))
| BIT_SYS_EF_ADDR(Addr));
while(1) {
if(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL) & BIT_SYS_EF_RWFLAG) {
*Data = HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL);
result = 1;
break;
}
HalDelayUs(1000);
if (i++ >= 100) {
*Data = -1;
break;
};
};
HalEFUSEPowerSwitch8195AROM(1, 0, L25OutVoltage);
}
else *Data = -1;
return result;
}
//----- HALEFUSEOneByteWriteROM addr 0x6699
_LONG_CALL_ROM_ int HALEFUSEOneByteWriteROM(IN unsigned int CtrlSetting, IN unsigned short Addr, IN unsigned char Data, IN unsigned char L25OutVoltage)
{
int i = 0, result = 0;
if ( (Addr <= 0xFF) || ((CtrlSetting & 0xFFFF) == 0x26AE) ) {
HalEFUSEPowerSwitch8195AROM(1, 1, L25OutVoltage);
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_TEST, HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_TEST) | BIT_SYS_EF_FORCE_PGMEN);
HAL_WRITE32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL, Data | BIT_SYS_EF_RWFLAG | BIT_SYS_EF_ADDR(Addr) | BIT_SYS_EF_DATA(Data) |
(CtrlSetting & (~(BIT_SYS_EF_RWFLAG | (BIT_MASK_SYS_EF_ADDR << BIT_SHIFT_SYS_EF_ADDR) | (BIT_MASK_SYS_EF_DATA << BIT_SHIFT_SYS_EF_DATA)))));
while(1) {
HalDelayUs(1000);
if(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL) & BIT_SYS_EF_RWFLAG) break;
if (i++ >= 100) {
result = 1;
break;
};
};
HalEFUSEPowerSwitch8195AROM(1, 0, L25OutVoltage);
}
return result;
}
//====================================================== End libs ROM efuse
#endif
//----- HALOTPOneByteReadRAM
int HALOTPOneByteReadRAM(IN unsigned int CtrlSetting, IN unsigned short Addr, OUT unsigned char *Data, IN unsigned char L25OutVoltage)
{
int result;
if ( (unsigned int)(Addr - EFUSE_SECTION_SIZE) > OTP_BUF_MAX_LEN - 1 )
result = 1;
else
result = HALEFUSEOneByteReadROM(CtrlSetting, Addr, Data, L25OutVoltage);
return result;
}
//----- HALOTPOneByteWriteRAM
int HALOTPOneByteWriteRAM(IN unsigned int CtrlSetting, IN unsigned short Addr, IN unsigned char Data, IN unsigned char L25OutVoltage)
{
#if EFUSE_WRITE_ENABLE
int result;
if ( (unsigned int)(Addr - EFUSE_SECTION_SIZE) > OTP_BUF_MAX_LEN - 1 )
result = 1;
else
result = HALEFUSEOneByteWriteROM(CtrlSetting, Addr, Data, L25OutVoltage);
return result;
#else
return 1;
#endif
}
//----- HALEFUSEOneByteReadRAM
int HALEFUSEOneByteReadRAM(IN unsigned int CtrlSetting, IN unsigned short Addr, IN unsigned char *Data, IN unsigned char L25OutVoltage)
{
int result;
if ( (unsigned int)(Addr - 160) > 0x33 )
{
result = HALEFUSEOneByteReadROM(CtrlSetting, Addr, Data, L25OutVoltage);
}
else
{
*Data = -1;
result = 1;
}
return result;
}
//----- HALEFUSEOneByteWriteRAM
int HALEFUSEOneByteWriteRAM(IN unsigned int CtrlSetting, IN unsigned short Addr, IN unsigned char Data, IN unsigned char L25OutVoltage)
{
#if EFUSE_WRITE_ENABLE
int result;
if ( (unsigned int)(Addr - 127) <= 0x54 )
result = 1;
else {
result = HALEFUSEOneByteWriteROM(CtrlSetting, Addr, Data, L25OutVoltage);
}
return result;
#else
return 1;
#endif
}
//----- ReadEfuseContant
void ReadEfuseContant(IN unsigned char UserCode, IN unsigned char *pContant)
{
unsigned int i, offset, bcnt, eFuse_Addr = 0;
unsigned char DataTemp0;
unsigned char DataTemp1;
unsigned char * pbuf = pContant;
do {
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), eFuse_Addr, &DataTemp0, L25EOUTVOLTAGE);
if (DataTemp0 == 0x0FF) break;
if ((DataTemp0 & 0x0F) == 0x0F) {
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), ++eFuse_Addr, &DataTemp1, L25EOUTVOLTAGE);
offset = ((DataTemp1 & 0x0F0) | (DataTemp0 >> 4)) >> 1;
bcnt = (~DataTemp1) & 0x0F;
if (((UserCode + EFUSE_SECTION_CODE) << 2) > offset || offset >= ((UserCode + EFUSE_SECTION_CODE + 1) << 2)) {
while(bcnt) {
if (bcnt & 1) eFuse_Addr += 2;
bcnt >>= 1;
}
}
else {
int base = (offset - ((EFUSE_SECTION_CODE + UserCode) << 2)) << 3;
i = 0;
while(bcnt) {
if (bcnt & 1) {
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), ++eFuse_Addr, &pbuf[base + i], L25EOUTVOLTAGE);
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), ++eFuse_Addr, &pbuf[base + i + 1], L25EOUTVOLTAGE);
}
bcnt >>= 1;
i += 2;
}
}
}
else for(i = (~DataTemp0) & 0x0F; i; i >>= 1) if (i & 1) eFuse_Addr += 2;
eFuse_Addr++;
}
while(eFuse_Addr < EFUSE_SECTION_SIZE - 1);
}
//----- ReadEfuseContant1
void ReadEfuseContant1(OUT unsigned char *pContant)
{
ReadEfuseContant(0, pContant);
}
//----- ReadEfuseContant2
void ReadEfuseContant2(OUT unsigned char *pContant)
{
ReadEfuseContant(1, pContant);
}
//----- ReadEfuseContant3
void ReadEfuseContant3(OUT unsigned char *pContant)
{
ReadEfuseContant(2, pContant);
}
//----- GetRemainingEfuseLength
int GetRemainingEfuseLength(void)
{
unsigned int i, eFuse_Addr = 0;
unsigned char DataTemp0;
do
{
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), eFuse_Addr, &DataTemp0, L25EOUTVOLTAGE);
if(DataTemp0 == 0x0FF) break;
if((DataTemp0 & 0x0F) == 0x0F)
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), ++eFuse_Addr, &DataTemp0, L25EOUTVOLTAGE);
for (i = (~DataTemp0) & 0x0F; i; i >>= 1 ) if (i & 1) eFuse_Addr += 2;
eFuse_Addr++;
}
while(eFuse_Addr < EFUSE_SECTION_SIZE - 1);
return (EFUSE_SECTION_SIZE - 1 - eFuse_Addr);
}
//----- WriteEfuseContant
int WriteEfuseContant(IN unsigned char UserCode, IN unsigned char CodeWordNum, IN unsigned char WordEnable, IN unsigned char *pContant)
{
#if EFUSE_WRITE_ENABLE
int result = 0;
unsigned int i, j, eFuse_Addr; // r4@3
unsigned char DataTemp0;
unsigned int bmask = WordEnable & 0xF;
if (bmask) {
eFuse_Addr = 0;
do { // eFuse_Addr = 128 - _GetRemainingEfuseLength
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), eFuse_Addr, &DataTemp0, L25EOUTVOLTAGE);
if (DataTemp0 == 0x0ff) break;
if ((DataTemp0 & 0x0F) == 0x0F)
HALEFUSEOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), ++eFuse_Addr, &DataTemp0, L25EOUTVOLTAGE);
for (i = (~DataTemp0) & 0x0F; i; i >>= 1) if (i & 1) eFuse_Addr += 2;
eFuse_Addr++;
}
while (eFuse_Addr <= EFUSE_SECTION_SIZE - 2);
j = 0;
do
{
if (bmask & 1) j += 2;
bmask >>= 1;
}
while (bmask);
if ((eFuse_Addr + j) <= EFUSE_SECTION_SIZE - 4)
{
HALEFUSEOneByteWriteRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), eFuse_Addr, (((UserCode + EFUSE_SECTION_CODE) << 7) | 0x0F) + ((CodeWordNum & 3) << 5), L25EOUTVOLTAGE);
HALEFUSEOneByteWriteRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), eFuse_Addr + 1, (((UserCode + EFUSE_SECTION_CODE) << 3) & 0xF0) | ((~bmask) & 0xF), L25EOUTVOLTAGE);
i = 0;
while (i < j)
{
HALEFUSEOneByteWriteRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), eFuse_Addr + 2 + i, pContant[i], L25EOUTVOLTAGE);
i++;
}
result = 1;
}
}
return result;
#else
return 1;
#endif
}
//----- WriteEfuseContant1
int WriteEfuseContant1(IN unsigned char CodeWordNum, IN unsigned char WordEnable, IN unsigned char *pContant)
{
#if EFUSE_WRITE_ENABLE
return WriteEfuseContant(0, CodeWordNum, WordEnable, pContant);
#else
return 1;
#endif
}
//----- WriteEfuseContant2
int WriteEfuseContant2(IN unsigned char CodeWordNum, IN unsigned char WordEnable, IN unsigned char *pContant)
{
#if EFUSE_WRITE_ENABLE
return WriteEfuseContant(1, CodeWordNum, WordEnable, pContant);
#else
return 1;
#endif
}
//----- WriteEfuseContant2
int WriteEfuseContant3(IN unsigned char CodeWordNum, IN unsigned char WordEnable, IN unsigned char *pContant)
{
#if EFUSE_WRITE_ENABLE
return WriteEfuseContant(2, CodeWordNum, WordEnable, pContant);
#else
return 1;
#endif
}
//----- ReadEOTPContant
void ReadEOTPContant(IN unsigned char *pContant)
{
int i;
for(i = 0; i < OTP_BUF_MAX_LEN; i++ )
HALOTPOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), i+EFUSE_SECTION_SIZE, &pContant[i], L25EOUTVOLTAGE);
}
//----- WriteEOTPContant
void WriteEOTPContant(IN unsigned char *pContant)
{
#if EFUSE_WRITE_ENABLE
int i;
unsigned char DataTemp0;
for(i = 0; i < OTP_BUF_MAX_LEN; i++ ) {
HALOTPOneByteReadRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), i+EFUSE_SECTION_SIZE, &DataTemp0, L25EOUTVOLTAGE);
if (DataTemp0 == 0xFF)
HALOTPOneByteWriteRAM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), i+EFUSE_SECTION_SIZE, pContant[i], L25EOUTVOLTAGE);
}
#endif
}
//----- HALJtagOff
void HALJtagOff(void)
{
#if EFUSE_WRITE_ENABLE
HALEFUSEOneByteWriteROM(HAL_READ32(SYSTEM_CTRL_BASE, REG_SYS_EFUSE_CTRL), 211, 0xFE, L25EOUTVOLTAGE);
#endif
}
#endif //CONFIG_EFUSE_EN