blob: 47880d8aeea831263cc974a5bb34535692b85c65 [file] [log] [blame]
Ye Li77a57122021-08-07 16:01:05 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2020 NXP
4 */
5
Ye Li77a57122021-08-07 16:01:05 +08006#include <console.h>
7#include <errno.h>
8#include <fuse.h>
9#include <asm/arch/sys_proto.h>
10#include <asm/arch/imx-regs.h>
11#include <env.h>
Peng Fand5c31832023-06-15 18:09:05 +080012#include <asm/mach-imx/ele_api.h>
Ye Li77a57122021-08-07 16:01:05 +080013#include <asm/global_data.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17#define FUSE_BANKS 64
18#define WORDS_PER_BANKS 8
19
20struct fsb_map_entry {
21 s32 fuse_bank;
22 u32 fuse_words;
23 bool redundancy;
24};
25
Peng Fand5c31832023-06-15 18:09:05 +080026struct ele_map_entry {
Ye Li77a57122021-08-07 16:01:05 +080027 s32 fuse_bank;
28 u32 fuse_words;
29 u32 fuse_offset;
Peng Fand5c31832023-06-15 18:09:05 +080030 u32 ele_index;
Ye Li77a57122021-08-07 16:01:05 +080031};
32
Alice Guob93916d2022-07-26 16:40:59 +080033#if defined(CONFIG_IMX8ULP)
34#define FSB_OTP_SHADOW 0x800
35
Ye Li77a57122021-08-07 16:01:05 +080036struct fsb_map_entry fsb_mapping_table[] = {
37 { 3, 8 },
38 { 4, 8 },
Peng Fan199fa602022-04-06 14:30:31 +080039 { -1, 48 }, /* Reserve 48 words */
Ye Li77a57122021-08-07 16:01:05 +080040 { 5, 8 },
41 { 6, 8 },
Ye Li77a57122021-08-07 16:01:05 +080042 { 8, 4, true },
43 { 24, 4, true },
44 { 26, 4, true },
45 { 27, 4, true },
46 { 28, 8 },
47 { 29, 8 },
48 { 30, 8 },
49 { 31, 8 },
50 { 37, 8 },
51 { 38, 8 },
52 { 39, 8 },
53 { 40, 8 },
54 { 41, 8 },
55 { 42, 8 },
56 { 43, 8 },
57 { 44, 8 },
58 { 45, 8 },
59 { 46, 8 },
60};
61
Ye Li1a6e3ca2023-01-31 16:42:28 +080062/* None ECC banks such like Redundancy or Bit protect */
63u32 nonecc_fuse_banks[] = {
64 0, 1, 8, 12, 16, 22, 24, 25, 26, 27, 36, 41, 51, 56
65};
66
Peng Fand5c31832023-06-15 18:09:05 +080067struct ele_map_entry ele_api_mapping_table[] = {
Ye Li77a57122021-08-07 16:01:05 +080068 { 1, 8 }, /* LOCK */
69 { 2, 8 }, /* ECID */
70 { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */
Ye Li6469bc62022-04-06 14:30:16 +080071 { 15, 8 }, /* OEM SRK HASH */
Ye Li77a57122021-08-07 16:01:05 +080072 { 23, 1, 4, 2 }, /* OTFAD */
Peng Fan199fa602022-04-06 14:30:31 +080073 { 25, 8 }, /* Test config2 */
Ye Liaeecf402023-01-31 16:42:27 +080074 { 26, 8 }, /* PMU */
75 { 27, 8 }, /* Test flow/USB */
76 { 32, 8 }, /* GP1 */
77 { 33, 8 }, /* GP2 */
78 { 34, 8 }, /* GP3 */
79 { 35, 8 }, /* GP4 */
80 { 36, 8 }, /* GP5 */
81 { 49, 8 }, /* GP8 */
82 { 50, 8 }, /* GP9 */
83 { 51, 8 }, /* GP10 */
Ye Li77a57122021-08-07 16:01:05 +080084};
Alice Guob93916d2022-07-26 16:40:59 +080085#elif defined(CONFIG_ARCH_IMX9)
86#define FSB_OTP_SHADOW 0x8000
87
88struct fsb_map_entry fsb_mapping_table[] = {
89 { 0, 8 },
90 { 1, 8 },
91 { 2, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080092 { 3, 8 },
Alice Guob93916d2022-07-26 16:40:59 +080093 { 4, 8 },
94 { 5, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080095 { 6, 4 },
96 { -1, 260 },
97 { 39, 8 },
98 { 40, 8 },
99 { 41, 8 },
100 { 42, 8 },
101 { 43, 8 },
102 { 44, 8 },
103 { 45, 8 },
104 { 46, 8 },
105 { 47, 8 },
106 { 48, 8 },
107 { 49, 8 },
108 { 50, 8 },
109 { 51, 8 },
110 { 52, 8 },
111 { 53, 8 },
112 { 54, 8 },
113 { 55, 8 },
114 { 56, 8 },
115 { 57, 8 },
116 { 58, 8 },
117 { 59, 8 },
118 { 60, 8 },
119 { 61, 8 },
120 { 62, 8 },
121 { 63, 8 },
Alice Guob93916d2022-07-26 16:40:59 +0800122};
123
Peng Fand5c31832023-06-15 18:09:05 +0800124struct ele_map_entry ele_api_mapping_table[] = {
Alice Guo8ee27642022-07-26 16:41:00 +0800125 { 7, 1, 7, 63 },
126 { 16, 8, },
127 { 17, 8, },
128 { 22, 1, 6 },
129 { 23, 1, 4 },
Alice Guob93916d2022-07-26 16:40:59 +0800130};
131#endif
Ye Li77a57122021-08-07 16:01:05 +0800132
133static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
134{
135 s32 size = ARRAY_SIZE(fsb_mapping_table);
136 s32 i, word_pos = 0;
137
138 /* map the fuse from ocotp fuse map to FSB*/
139 for (i = 0; i < size; i++) {
140 if (fsb_mapping_table[i].fuse_bank != -1 &&
Ye Lia48556c2024-10-06 08:30:04 +0800141 fsb_mapping_table[i].fuse_bank == bank) {
Ye Li77a57122021-08-07 16:01:05 +0800142 break;
143 }
144
145 word_pos += fsb_mapping_table[i].fuse_words;
146 }
147
148 if (i == size)
149 return -1; /* Failed to find */
150
151 if (fsb_mapping_table[i].redundancy) {
Ye Lia48556c2024-10-06 08:30:04 +0800152 if ((fsb_mapping_table[i].fuse_words << 1) <= word)
153 return -2; /* Not valid word */
154
Ye Li77a57122021-08-07 16:01:05 +0800155 *redundancy = true;
156 return (word >> 1) + word_pos;
Ye Lia48556c2024-10-06 08:30:04 +0800157 } else if (fsb_mapping_table[i].fuse_words <= word) {
158 return -2; /* Not valid word */
Ye Li77a57122021-08-07 16:01:05 +0800159 }
160
161 *redundancy = false;
162 return word + word_pos;
163}
164
Peng Fand5c31832023-06-15 18:09:05 +0800165static s32 map_ele_fuse_index(u32 bank, u32 word)
Ye Li77a57122021-08-07 16:01:05 +0800166{
Peng Fand5c31832023-06-15 18:09:05 +0800167 s32 size = ARRAY_SIZE(ele_api_mapping_table);
Ye Li77a57122021-08-07 16:01:05 +0800168 s32 i;
169
170 /* map the fuse from ocotp fuse map to FSB*/
171 for (i = 0; i < size; i++) {
Peng Fand5c31832023-06-15 18:09:05 +0800172 if (ele_api_mapping_table[i].fuse_bank != -1 &&
173 ele_api_mapping_table[i].fuse_bank == bank) {
174 if (word >= ele_api_mapping_table[i].fuse_offset &&
175 word < (ele_api_mapping_table[i].fuse_offset +
176 ele_api_mapping_table[i].fuse_words))
Ye Li77a57122021-08-07 16:01:05 +0800177 break;
178 }
179 }
180
181 if (i == size)
182 return -1; /* Failed to find */
183
Peng Fand5c31832023-06-15 18:09:05 +0800184 if (ele_api_mapping_table[i].ele_index != 0)
185 return ele_api_mapping_table[i].ele_index;
Ye Li77a57122021-08-07 16:01:05 +0800186
Peng Fand5c31832023-06-15 18:09:05 +0800187 return ele_api_mapping_table[i].fuse_bank * 8 + word;
Ye Li77a57122021-08-07 16:01:05 +0800188}
189
Alice Guo8ee27642022-07-26 16:41:00 +0800190#if defined(CONFIG_IMX8ULP)
Ye Li77a57122021-08-07 16:01:05 +0800191int fuse_sense(u32 bank, u32 word, u32 *val)
192{
193 s32 word_index;
194 bool redundancy;
195
196 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
197 return -EINVAL;
198
199 word_index = map_fsb_fuse_index(bank, word, &redundancy);
200 if (word_index >= 0) {
Alice Guob93916d2022-07-26 16:40:59 +0800201 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
Ye Li77a57122021-08-07 16:01:05 +0800202 if (redundancy)
203 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
204
205 return 0;
206 }
207
Peng Fand5c31832023-06-15 18:09:05 +0800208 word_index = map_ele_fuse_index(bank, word);
Ye Li77a57122021-08-07 16:01:05 +0800209 if (word_index >= 0) {
210 u32 data[4];
Ye Lia48556c2024-10-06 08:30:04 +0800211 u32 res = 0, size = 4;
Ye Li77a57122021-08-07 16:01:05 +0800212 int ret;
213
214 /* Only UID return 4 words */
215 if (word_index != 1)
216 size = 1;
217
Peng Fand5c31832023-06-15 18:09:05 +0800218 ret = ele_read_common_fuse(word_index, data, size, &res);
Ye Li77a57122021-08-07 16:01:05 +0800219 if (ret) {
220 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
221 return ret;
222 }
223
224 if (word_index == 1) {
225 *val = data[word]; /* UID */
226 } else if (word_index == 2) {
227 /*
228 * OTFAD 3 bits as follow:
229 * bit 0: OTFAD_ENABLE
230 * bit 1: OTFAD_DISABLE_OVERRIDE
231 * bit 2: KEY_BLOB_EN
232 */
233 *val = data[0] << 3;
234 } else {
235 *val = data[0];
236 }
237
238 return 0;
239 }
240
241 return -ENOENT;
242}
Alice Guo8ee27642022-07-26 16:41:00 +0800243#elif defined(CONFIG_ARCH_IMX9)
244int fuse_sense(u32 bank, u32 word, u32 *val)
245{
246 s32 word_index;
247 bool redundancy;
248
249 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
250 return -EINVAL;
251
252 word_index = map_fsb_fuse_index(bank, word, &redundancy);
253 if (word_index >= 0) {
254 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
255 if (redundancy)
256 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
257
258 return 0;
259 }
260
Peng Fand5c31832023-06-15 18:09:05 +0800261 word_index = map_ele_fuse_index(bank, word);
Alice Guo8ee27642022-07-26 16:41:00 +0800262 if (word_index >= 0) {
263 u32 data;
Ye Lia48556c2024-10-06 08:30:04 +0800264 u32 res = 0, size = 1;
Alice Guo8ee27642022-07-26 16:41:00 +0800265 int ret;
266
Peng Fand5c31832023-06-15 18:09:05 +0800267 ret = ele_read_common_fuse(word_index, &data, size, &res);
Alice Guo8ee27642022-07-26 16:41:00 +0800268 if (ret) {
269 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
270 return ret;
271 }
272
273 *val = data;
274
275 return 0;
276 }
277
278 return -ENOENT;
279}
280#endif
Ye Li77a57122021-08-07 16:01:05 +0800281
282int fuse_read(u32 bank, u32 word, u32 *val)
283{
284 return fuse_sense(bank, word, val);
285}
286
287int fuse_prog(u32 bank, u32 word, u32 val)
288{
Ye Lia48556c2024-10-06 08:30:04 +0800289 u32 res = 0;
Ye Li77a57122021-08-07 16:01:05 +0800290 int ret;
Ye Li1a6e3ca2023-01-31 16:42:28 +0800291 bool lock = false;
Ye Li77a57122021-08-07 16:01:05 +0800292
293 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
294 return -EINVAL;
295
Ye Li1a6e3ca2023-01-31 16:42:28 +0800296 /* Lock 8ULP ECC fuse word, so second programming will return failure.
297 * iMX9 OTP can protect ECC fuse, so not need it
298 */
299#if defined(CONFIG_IMX8ULP)
300 u32 i;
301 for (i = 0; i < ARRAY_SIZE(nonecc_fuse_banks); i++) {
302 if (nonecc_fuse_banks[i] == bank)
303 break;
304 }
305
306 if (i == ARRAY_SIZE(nonecc_fuse_banks))
307 lock = true;
308#endif
309
Peng Fand5c31832023-06-15 18:09:05 +0800310 ret = ele_write_fuse((bank * 8 + word), val, lock, &res);
Ye Li77a57122021-08-07 16:01:05 +0800311 if (ret) {
312 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
313 return ret;
314 }
315
316 return 0;
317}
318
319int fuse_override(u32 bank, u32 word, u32 val)
320{
321 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
322 return -EPERM;
323}