blob: d12539c8aac06142f72eff41cb97d801c397c30d [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 &&
Alice Guo8ee27642022-07-26 16:41:00 +0800141 fsb_mapping_table[i].fuse_bank == bank &&
142 fsb_mapping_table[i].fuse_words > word) {
Ye Li77a57122021-08-07 16:01:05 +0800143 break;
144 }
145
146 word_pos += fsb_mapping_table[i].fuse_words;
147 }
148
149 if (i == size)
150 return -1; /* Failed to find */
151
152 if (fsb_mapping_table[i].redundancy) {
153 *redundancy = true;
154 return (word >> 1) + word_pos;
155 }
156
157 *redundancy = false;
158 return word + word_pos;
159}
160
Peng Fand5c31832023-06-15 18:09:05 +0800161static s32 map_ele_fuse_index(u32 bank, u32 word)
Ye Li77a57122021-08-07 16:01:05 +0800162{
Peng Fand5c31832023-06-15 18:09:05 +0800163 s32 size = ARRAY_SIZE(ele_api_mapping_table);
Ye Li77a57122021-08-07 16:01:05 +0800164 s32 i;
165
166 /* map the fuse from ocotp fuse map to FSB*/
167 for (i = 0; i < size; i++) {
Peng Fand5c31832023-06-15 18:09:05 +0800168 if (ele_api_mapping_table[i].fuse_bank != -1 &&
169 ele_api_mapping_table[i].fuse_bank == bank) {
170 if (word >= ele_api_mapping_table[i].fuse_offset &&
171 word < (ele_api_mapping_table[i].fuse_offset +
172 ele_api_mapping_table[i].fuse_words))
Ye Li77a57122021-08-07 16:01:05 +0800173 break;
174 }
175 }
176
177 if (i == size)
178 return -1; /* Failed to find */
179
Peng Fand5c31832023-06-15 18:09:05 +0800180 if (ele_api_mapping_table[i].ele_index != 0)
181 return ele_api_mapping_table[i].ele_index;
Ye Li77a57122021-08-07 16:01:05 +0800182
Peng Fand5c31832023-06-15 18:09:05 +0800183 return ele_api_mapping_table[i].fuse_bank * 8 + word;
Ye Li77a57122021-08-07 16:01:05 +0800184}
185
Alice Guo8ee27642022-07-26 16:41:00 +0800186#if defined(CONFIG_IMX8ULP)
Ye Li77a57122021-08-07 16:01:05 +0800187int fuse_sense(u32 bank, u32 word, u32 *val)
188{
189 s32 word_index;
190 bool redundancy;
191
192 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
193 return -EINVAL;
194
195 word_index = map_fsb_fuse_index(bank, word, &redundancy);
196 if (word_index >= 0) {
Alice Guob93916d2022-07-26 16:40:59 +0800197 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
Ye Li77a57122021-08-07 16:01:05 +0800198 if (redundancy)
199 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
200
201 return 0;
202 }
203
Peng Fand5c31832023-06-15 18:09:05 +0800204 word_index = map_ele_fuse_index(bank, word);
Ye Li77a57122021-08-07 16:01:05 +0800205 if (word_index >= 0) {
206 u32 data[4];
207 u32 res, size = 4;
208 int ret;
209
210 /* Only UID return 4 words */
211 if (word_index != 1)
212 size = 1;
213
Peng Fand5c31832023-06-15 18:09:05 +0800214 ret = ele_read_common_fuse(word_index, data, size, &res);
Ye Li77a57122021-08-07 16:01:05 +0800215 if (ret) {
216 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
217 return ret;
218 }
219
220 if (word_index == 1) {
221 *val = data[word]; /* UID */
222 } else if (word_index == 2) {
223 /*
224 * OTFAD 3 bits as follow:
225 * bit 0: OTFAD_ENABLE
226 * bit 1: OTFAD_DISABLE_OVERRIDE
227 * bit 2: KEY_BLOB_EN
228 */
229 *val = data[0] << 3;
230 } else {
231 *val = data[0];
232 }
233
234 return 0;
235 }
236
237 return -ENOENT;
238}
Alice Guo8ee27642022-07-26 16:41:00 +0800239#elif defined(CONFIG_ARCH_IMX9)
240int fuse_sense(u32 bank, u32 word, u32 *val)
241{
242 s32 word_index;
243 bool redundancy;
244
245 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
246 return -EINVAL;
247
248 word_index = map_fsb_fuse_index(bank, word, &redundancy);
249 if (word_index >= 0) {
250 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
251 if (redundancy)
252 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
253
254 return 0;
255 }
256
Peng Fand5c31832023-06-15 18:09:05 +0800257 word_index = map_ele_fuse_index(bank, word);
Alice Guo8ee27642022-07-26 16:41:00 +0800258 if (word_index >= 0) {
259 u32 data;
260 u32 res, size = 1;
261 int ret;
262
Peng Fand5c31832023-06-15 18:09:05 +0800263 ret = ele_read_common_fuse(word_index, &data, size, &res);
Alice Guo8ee27642022-07-26 16:41:00 +0800264 if (ret) {
265 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
266 return ret;
267 }
268
269 *val = data;
270
271 return 0;
272 }
273
274 return -ENOENT;
275}
276#endif
Ye Li77a57122021-08-07 16:01:05 +0800277
278int fuse_read(u32 bank, u32 word, u32 *val)
279{
280 return fuse_sense(bank, word, val);
281}
282
283int fuse_prog(u32 bank, u32 word, u32 val)
284{
285 u32 res;
286 int ret;
Ye Li1a6e3ca2023-01-31 16:42:28 +0800287 bool lock = false;
Ye Li77a57122021-08-07 16:01:05 +0800288
289 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
290 return -EINVAL;
291
Ye Li1a6e3ca2023-01-31 16:42:28 +0800292 /* Lock 8ULP ECC fuse word, so second programming will return failure.
293 * iMX9 OTP can protect ECC fuse, so not need it
294 */
295#if defined(CONFIG_IMX8ULP)
296 u32 i;
297 for (i = 0; i < ARRAY_SIZE(nonecc_fuse_banks); i++) {
298 if (nonecc_fuse_banks[i] == bank)
299 break;
300 }
301
302 if (i == ARRAY_SIZE(nonecc_fuse_banks))
303 lock = true;
304#endif
305
Peng Fand5c31832023-06-15 18:09:05 +0800306 ret = ele_write_fuse((bank * 8 + word), val, lock, &res);
Ye Li77a57122021-08-07 16:01:05 +0800307 if (ret) {
308 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
309 return ret;
310 }
311
312 return 0;
313}
314
315int fuse_override(u32 bank, u32 word, u32 val)
316{
317 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
318 return -EPERM;
319}