blob: 4e4dcb42cdd95fc609b0e9cb711a256b5a97228f [file] [log] [blame]
Ye Li77a57122021-08-07 16:01:05 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2020 NXP
4 */
5
6#include <common.h>
7#include <console.h>
8#include <errno.h>
9#include <fuse.h>
10#include <asm/arch/sys_proto.h>
11#include <asm/arch/imx-regs.h>
12#include <env.h>
Peng Fand5c31832023-06-15 18:09:05 +080013#include <asm/mach-imx/ele_api.h>
Ye Li77a57122021-08-07 16:01:05 +080014#include <asm/global_data.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18#define FUSE_BANKS 64
19#define WORDS_PER_BANKS 8
20
21struct fsb_map_entry {
22 s32 fuse_bank;
23 u32 fuse_words;
24 bool redundancy;
25};
26
Peng Fand5c31832023-06-15 18:09:05 +080027struct ele_map_entry {
Ye Li77a57122021-08-07 16:01:05 +080028 s32 fuse_bank;
29 u32 fuse_words;
30 u32 fuse_offset;
Peng Fand5c31832023-06-15 18:09:05 +080031 u32 ele_index;
Ye Li77a57122021-08-07 16:01:05 +080032};
33
Alice Guob93916d2022-07-26 16:40:59 +080034#if defined(CONFIG_IMX8ULP)
35#define FSB_OTP_SHADOW 0x800
36
Ye Li77a57122021-08-07 16:01:05 +080037struct fsb_map_entry fsb_mapping_table[] = {
38 { 3, 8 },
39 { 4, 8 },
Peng Fan199fa602022-04-06 14:30:31 +080040 { -1, 48 }, /* Reserve 48 words */
Ye Li77a57122021-08-07 16:01:05 +080041 { 5, 8 },
42 { 6, 8 },
Ye Li77a57122021-08-07 16:01:05 +080043 { 8, 4, true },
44 { 24, 4, true },
45 { 26, 4, true },
46 { 27, 4, true },
47 { 28, 8 },
48 { 29, 8 },
49 { 30, 8 },
50 { 31, 8 },
51 { 37, 8 },
52 { 38, 8 },
53 { 39, 8 },
54 { 40, 8 },
55 { 41, 8 },
56 { 42, 8 },
57 { 43, 8 },
58 { 44, 8 },
59 { 45, 8 },
60 { 46, 8 },
61};
62
Ye Li1a6e3ca2023-01-31 16:42:28 +080063/* None ECC banks such like Redundancy or Bit protect */
64u32 nonecc_fuse_banks[] = {
65 0, 1, 8, 12, 16, 22, 24, 25, 26, 27, 36, 41, 51, 56
66};
67
Peng Fand5c31832023-06-15 18:09:05 +080068struct ele_map_entry ele_api_mapping_table[] = {
Ye Li77a57122021-08-07 16:01:05 +080069 { 1, 8 }, /* LOCK */
70 { 2, 8 }, /* ECID */
71 { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */
Ye Li6469bc62022-04-06 14:30:16 +080072 { 15, 8 }, /* OEM SRK HASH */
Ye Li77a57122021-08-07 16:01:05 +080073 { 23, 1, 4, 2 }, /* OTFAD */
Peng Fan199fa602022-04-06 14:30:31 +080074 { 25, 8 }, /* Test config2 */
Ye Liaeecf402023-01-31 16:42:27 +080075 { 26, 8 }, /* PMU */
76 { 27, 8 }, /* Test flow/USB */
77 { 32, 8 }, /* GP1 */
78 { 33, 8 }, /* GP2 */
79 { 34, 8 }, /* GP3 */
80 { 35, 8 }, /* GP4 */
81 { 36, 8 }, /* GP5 */
82 { 49, 8 }, /* GP8 */
83 { 50, 8 }, /* GP9 */
84 { 51, 8 }, /* GP10 */
Ye Li77a57122021-08-07 16:01:05 +080085};
Alice Guob93916d2022-07-26 16:40:59 +080086#elif defined(CONFIG_ARCH_IMX9)
87#define FSB_OTP_SHADOW 0x8000
88
89struct fsb_map_entry fsb_mapping_table[] = {
90 { 0, 8 },
91 { 1, 8 },
92 { 2, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080093 { 3, 8 },
Alice Guob93916d2022-07-26 16:40:59 +080094 { 4, 8 },
95 { 5, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080096 { 6, 4 },
97 { -1, 260 },
98 { 39, 8 },
99 { 40, 8 },
100 { 41, 8 },
101 { 42, 8 },
102 { 43, 8 },
103 { 44, 8 },
104 { 45, 8 },
105 { 46, 8 },
106 { 47, 8 },
107 { 48, 8 },
108 { 49, 8 },
109 { 50, 8 },
110 { 51, 8 },
111 { 52, 8 },
112 { 53, 8 },
113 { 54, 8 },
114 { 55, 8 },
115 { 56, 8 },
116 { 57, 8 },
117 { 58, 8 },
118 { 59, 8 },
119 { 60, 8 },
120 { 61, 8 },
121 { 62, 8 },
122 { 63, 8 },
Alice Guob93916d2022-07-26 16:40:59 +0800123};
124
Peng Fand5c31832023-06-15 18:09:05 +0800125struct ele_map_entry ele_api_mapping_table[] = {
Alice Guo8ee27642022-07-26 16:41:00 +0800126 { 7, 1, 7, 63 },
127 { 16, 8, },
128 { 17, 8, },
129 { 22, 1, 6 },
130 { 23, 1, 4 },
Alice Guob93916d2022-07-26 16:40:59 +0800131};
132#endif
Ye Li77a57122021-08-07 16:01:05 +0800133
134static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
135{
136 s32 size = ARRAY_SIZE(fsb_mapping_table);
137 s32 i, word_pos = 0;
138
139 /* map the fuse from ocotp fuse map to FSB*/
140 for (i = 0; i < size; i++) {
141 if (fsb_mapping_table[i].fuse_bank != -1 &&
Alice Guo8ee27642022-07-26 16:41:00 +0800142 fsb_mapping_table[i].fuse_bank == bank &&
143 fsb_mapping_table[i].fuse_words > word) {
Ye Li77a57122021-08-07 16:01:05 +0800144 break;
145 }
146
147 word_pos += fsb_mapping_table[i].fuse_words;
148 }
149
150 if (i == size)
151 return -1; /* Failed to find */
152
153 if (fsb_mapping_table[i].redundancy) {
154 *redundancy = true;
155 return (word >> 1) + word_pos;
156 }
157
158 *redundancy = false;
159 return word + word_pos;
160}
161
Peng Fand5c31832023-06-15 18:09:05 +0800162static s32 map_ele_fuse_index(u32 bank, u32 word)
Ye Li77a57122021-08-07 16:01:05 +0800163{
Peng Fand5c31832023-06-15 18:09:05 +0800164 s32 size = ARRAY_SIZE(ele_api_mapping_table);
Ye Li77a57122021-08-07 16:01:05 +0800165 s32 i;
166
167 /* map the fuse from ocotp fuse map to FSB*/
168 for (i = 0; i < size; i++) {
Peng Fand5c31832023-06-15 18:09:05 +0800169 if (ele_api_mapping_table[i].fuse_bank != -1 &&
170 ele_api_mapping_table[i].fuse_bank == bank) {
171 if (word >= ele_api_mapping_table[i].fuse_offset &&
172 word < (ele_api_mapping_table[i].fuse_offset +
173 ele_api_mapping_table[i].fuse_words))
Ye Li77a57122021-08-07 16:01:05 +0800174 break;
175 }
176 }
177
178 if (i == size)
179 return -1; /* Failed to find */
180
Peng Fand5c31832023-06-15 18:09:05 +0800181 if (ele_api_mapping_table[i].ele_index != 0)
182 return ele_api_mapping_table[i].ele_index;
Ye Li77a57122021-08-07 16:01:05 +0800183
Peng Fand5c31832023-06-15 18:09:05 +0800184 return ele_api_mapping_table[i].fuse_bank * 8 + word;
Ye Li77a57122021-08-07 16:01:05 +0800185}
186
Alice Guo8ee27642022-07-26 16:41:00 +0800187#if defined(CONFIG_IMX8ULP)
Ye Li77a57122021-08-07 16:01:05 +0800188int fuse_sense(u32 bank, u32 word, u32 *val)
189{
190 s32 word_index;
191 bool redundancy;
192
193 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
194 return -EINVAL;
195
196 word_index = map_fsb_fuse_index(bank, word, &redundancy);
197 if (word_index >= 0) {
Alice Guob93916d2022-07-26 16:40:59 +0800198 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
Ye Li77a57122021-08-07 16:01:05 +0800199 if (redundancy)
200 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
201
202 return 0;
203 }
204
Peng Fand5c31832023-06-15 18:09:05 +0800205 word_index = map_ele_fuse_index(bank, word);
Ye Li77a57122021-08-07 16:01:05 +0800206 if (word_index >= 0) {
207 u32 data[4];
208 u32 res, size = 4;
209 int ret;
210
211 /* Only UID return 4 words */
212 if (word_index != 1)
213 size = 1;
214
Peng Fand5c31832023-06-15 18:09:05 +0800215 ret = ele_read_common_fuse(word_index, data, size, &res);
Ye Li77a57122021-08-07 16:01:05 +0800216 if (ret) {
217 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
218 return ret;
219 }
220
221 if (word_index == 1) {
222 *val = data[word]; /* UID */
223 } else if (word_index == 2) {
224 /*
225 * OTFAD 3 bits as follow:
226 * bit 0: OTFAD_ENABLE
227 * bit 1: OTFAD_DISABLE_OVERRIDE
228 * bit 2: KEY_BLOB_EN
229 */
230 *val = data[0] << 3;
231 } else {
232 *val = data[0];
233 }
234
235 return 0;
236 }
237
238 return -ENOENT;
239}
Alice Guo8ee27642022-07-26 16:41:00 +0800240#elif defined(CONFIG_ARCH_IMX9)
241int fuse_sense(u32 bank, u32 word, u32 *val)
242{
243 s32 word_index;
244 bool redundancy;
245
246 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
247 return -EINVAL;
248
249 word_index = map_fsb_fuse_index(bank, word, &redundancy);
250 if (word_index >= 0) {
251 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
252 if (redundancy)
253 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
254
255 return 0;
256 }
257
Peng Fand5c31832023-06-15 18:09:05 +0800258 word_index = map_ele_fuse_index(bank, word);
Alice Guo8ee27642022-07-26 16:41:00 +0800259 if (word_index >= 0) {
260 u32 data;
261 u32 res, size = 1;
262 int ret;
263
Peng Fand5c31832023-06-15 18:09:05 +0800264 ret = ele_read_common_fuse(word_index, &data, size, &res);
Alice Guo8ee27642022-07-26 16:41:00 +0800265 if (ret) {
266 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
267 return ret;
268 }
269
270 *val = data;
271
272 return 0;
273 }
274
275 return -ENOENT;
276}
277#endif
Ye Li77a57122021-08-07 16:01:05 +0800278
279int fuse_read(u32 bank, u32 word, u32 *val)
280{
281 return fuse_sense(bank, word, val);
282}
283
284int fuse_prog(u32 bank, u32 word, u32 val)
285{
286 u32 res;
287 int ret;
Ye Li1a6e3ca2023-01-31 16:42:28 +0800288 bool lock = false;
Ye Li77a57122021-08-07 16:01:05 +0800289
290 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
291 return -EINVAL;
292
Ye Li1a6e3ca2023-01-31 16:42:28 +0800293 /* Lock 8ULP ECC fuse word, so second programming will return failure.
294 * iMX9 OTP can protect ECC fuse, so not need it
295 */
296#if defined(CONFIG_IMX8ULP)
297 u32 i;
298 for (i = 0; i < ARRAY_SIZE(nonecc_fuse_banks); i++) {
299 if (nonecc_fuse_banks[i] == bank)
300 break;
301 }
302
303 if (i == ARRAY_SIZE(nonecc_fuse_banks))
304 lock = true;
305#endif
306
Peng Fand5c31832023-06-15 18:09:05 +0800307 ret = ele_write_fuse((bank * 8 + word), val, lock, &res);
Ye Li77a57122021-08-07 16:01:05 +0800308 if (ret) {
309 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
310 return ret;
311 }
312
313 return 0;
314}
315
316int fuse_override(u32 bank, u32 word, u32 val)
317{
318 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
319 return -EPERM;
320}