blob: aa691d37919708377c9182c5a665127874b580d3 [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>
Ye Lic408ed32022-07-26 16:40:49 +080013#include <asm/mach-imx/s400_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
27struct s400_map_entry {
28 s32 fuse_bank;
29 u32 fuse_words;
30 u32 fuse_offset;
31 u32 s400_index;
32};
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
63struct s400_map_entry s400_api_mapping_table[] = {
64 { 1, 8 }, /* LOCK */
65 { 2, 8 }, /* ECID */
66 { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */
Ye Li6469bc62022-04-06 14:30:16 +080067 { 15, 8 }, /* OEM SRK HASH */
Ye Li77a57122021-08-07 16:01:05 +080068 { 23, 1, 4, 2 }, /* OTFAD */
Peng Fan199fa602022-04-06 14:30:31 +080069 { 25, 8 }, /* Test config2 */
Ye Liaeecf402023-01-31 16:42:27 +080070 { 26, 8 }, /* PMU */
71 { 27, 8 }, /* Test flow/USB */
72 { 32, 8 }, /* GP1 */
73 { 33, 8 }, /* GP2 */
74 { 34, 8 }, /* GP3 */
75 { 35, 8 }, /* GP4 */
76 { 36, 8 }, /* GP5 */
77 { 49, 8 }, /* GP8 */
78 { 50, 8 }, /* GP9 */
79 { 51, 8 }, /* GP10 */
Ye Li77a57122021-08-07 16:01:05 +080080};
Alice Guob93916d2022-07-26 16:40:59 +080081#elif defined(CONFIG_ARCH_IMX9)
82#define FSB_OTP_SHADOW 0x8000
83
84struct fsb_map_entry fsb_mapping_table[] = {
85 { 0, 8 },
86 { 1, 8 },
87 { 2, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080088 { 3, 8 },
Alice Guob93916d2022-07-26 16:40:59 +080089 { 4, 8 },
90 { 5, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080091 { 6, 4 },
92 { -1, 260 },
93 { 39, 8 },
94 { 40, 8 },
95 { 41, 8 },
96 { 42, 8 },
97 { 43, 8 },
98 { 44, 8 },
99 { 45, 8 },
100 { 46, 8 },
101 { 47, 8 },
102 { 48, 8 },
103 { 49, 8 },
104 { 50, 8 },
105 { 51, 8 },
106 { 52, 8 },
107 { 53, 8 },
108 { 54, 8 },
109 { 55, 8 },
110 { 56, 8 },
111 { 57, 8 },
112 { 58, 8 },
113 { 59, 8 },
114 { 60, 8 },
115 { 61, 8 },
116 { 62, 8 },
117 { 63, 8 },
Alice Guob93916d2022-07-26 16:40:59 +0800118};
119
120struct s400_map_entry s400_api_mapping_table[] = {
Alice Guo8ee27642022-07-26 16:41:00 +0800121 { 7, 1, 7, 63 },
122 { 16, 8, },
123 { 17, 8, },
124 { 22, 1, 6 },
125 { 23, 1, 4 },
Alice Guob93916d2022-07-26 16:40:59 +0800126};
127#endif
Ye Li77a57122021-08-07 16:01:05 +0800128
129static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
130{
131 s32 size = ARRAY_SIZE(fsb_mapping_table);
132 s32 i, word_pos = 0;
133
134 /* map the fuse from ocotp fuse map to FSB*/
135 for (i = 0; i < size; i++) {
136 if (fsb_mapping_table[i].fuse_bank != -1 &&
Alice Guo8ee27642022-07-26 16:41:00 +0800137 fsb_mapping_table[i].fuse_bank == bank &&
138 fsb_mapping_table[i].fuse_words > word) {
Ye Li77a57122021-08-07 16:01:05 +0800139 break;
140 }
141
142 word_pos += fsb_mapping_table[i].fuse_words;
143 }
144
145 if (i == size)
146 return -1; /* Failed to find */
147
148 if (fsb_mapping_table[i].redundancy) {
149 *redundancy = true;
150 return (word >> 1) + word_pos;
151 }
152
153 *redundancy = false;
154 return word + word_pos;
155}
156
157static s32 map_s400_fuse_index(u32 bank, u32 word)
158{
159 s32 size = ARRAY_SIZE(s400_api_mapping_table);
160 s32 i;
161
162 /* map the fuse from ocotp fuse map to FSB*/
163 for (i = 0; i < size; i++) {
164 if (s400_api_mapping_table[i].fuse_bank != -1 &&
165 s400_api_mapping_table[i].fuse_bank == bank) {
166 if (word >= s400_api_mapping_table[i].fuse_offset &&
167 word < (s400_api_mapping_table[i].fuse_offset +
168 s400_api_mapping_table[i].fuse_words))
169 break;
170 }
171 }
172
173 if (i == size)
174 return -1; /* Failed to find */
175
176 if (s400_api_mapping_table[i].s400_index != 0)
177 return s400_api_mapping_table[i].s400_index;
178
179 return s400_api_mapping_table[i].fuse_bank * 8 + word;
180}
181
Alice Guo8ee27642022-07-26 16:41:00 +0800182#if defined(CONFIG_IMX8ULP)
Ye Li77a57122021-08-07 16:01:05 +0800183int fuse_sense(u32 bank, u32 word, u32 *val)
184{
185 s32 word_index;
186 bool redundancy;
187
188 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
189 return -EINVAL;
190
191 word_index = map_fsb_fuse_index(bank, word, &redundancy);
192 if (word_index >= 0) {
Alice Guob93916d2022-07-26 16:40:59 +0800193 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
Ye Li77a57122021-08-07 16:01:05 +0800194 if (redundancy)
195 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
196
197 return 0;
198 }
199
200 word_index = map_s400_fuse_index(bank, word);
201 if (word_index >= 0) {
202 u32 data[4];
203 u32 res, size = 4;
204 int ret;
205
206 /* Only UID return 4 words */
207 if (word_index != 1)
208 size = 1;
209
210 ret = ahab_read_common_fuse(word_index, data, size, &res);
211 if (ret) {
212 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
213 return ret;
214 }
215
216 if (word_index == 1) {
217 *val = data[word]; /* UID */
218 } else if (word_index == 2) {
219 /*
220 * OTFAD 3 bits as follow:
221 * bit 0: OTFAD_ENABLE
222 * bit 1: OTFAD_DISABLE_OVERRIDE
223 * bit 2: KEY_BLOB_EN
224 */
225 *val = data[0] << 3;
226 } else {
227 *val = data[0];
228 }
229
230 return 0;
231 }
232
233 return -ENOENT;
234}
Alice Guo8ee27642022-07-26 16:41:00 +0800235#elif defined(CONFIG_ARCH_IMX9)
236int fuse_sense(u32 bank, u32 word, u32 *val)
237{
238 s32 word_index;
239 bool redundancy;
240
241 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
242 return -EINVAL;
243
244 word_index = map_fsb_fuse_index(bank, word, &redundancy);
245 if (word_index >= 0) {
246 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
247 if (redundancy)
248 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
249
250 return 0;
251 }
252
253 word_index = map_s400_fuse_index(bank, word);
254 if (word_index >= 0) {
255 u32 data;
256 u32 res, size = 1;
257 int ret;
258
259 ret = ahab_read_common_fuse(word_index, &data, size, &res);
260 if (ret) {
261 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
262 return ret;
263 }
264
265 *val = data;
266
267 return 0;
268 }
269
270 return -ENOENT;
271}
272#endif
Ye Li77a57122021-08-07 16:01:05 +0800273
274int fuse_read(u32 bank, u32 word, u32 *val)
275{
276 return fuse_sense(bank, word, val);
277}
278
279int fuse_prog(u32 bank, u32 word, u32 val)
280{
281 u32 res;
282 int ret;
283
284 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
285 return -EINVAL;
286
287 ret = ahab_write_fuse((bank * 8 + word), val, false, &res);
288 if (ret) {
289 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
290 return ret;
291 }
292
293 return 0;
294}
295
296int fuse_override(u32 bank, u32 word, u32 val)
297{
298 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
299 return -EPERM;
300}