blob: e2b687576644d928a35ac725ef64956b94388689 [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 Li77a57122021-08-07 16:01:05 +080070};
Alice Guob93916d2022-07-26 16:40:59 +080071#elif defined(CONFIG_ARCH_IMX9)
72#define FSB_OTP_SHADOW 0x8000
73
74struct fsb_map_entry fsb_mapping_table[] = {
75 { 0, 8 },
76 { 1, 8 },
77 { 2, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080078 { 3, 8 },
Alice Guob93916d2022-07-26 16:40:59 +080079 { 4, 8 },
80 { 5, 8 },
Alice Guo8ee27642022-07-26 16:41:00 +080081 { 6, 4 },
82 { -1, 260 },
83 { 39, 8 },
84 { 40, 8 },
85 { 41, 8 },
86 { 42, 8 },
87 { 43, 8 },
88 { 44, 8 },
89 { 45, 8 },
90 { 46, 8 },
91 { 47, 8 },
92 { 48, 8 },
93 { 49, 8 },
94 { 50, 8 },
95 { 51, 8 },
96 { 52, 8 },
97 { 53, 8 },
98 { 54, 8 },
99 { 55, 8 },
100 { 56, 8 },
101 { 57, 8 },
102 { 58, 8 },
103 { 59, 8 },
104 { 60, 8 },
105 { 61, 8 },
106 { 62, 8 },
107 { 63, 8 },
Alice Guob93916d2022-07-26 16:40:59 +0800108};
109
110struct s400_map_entry s400_api_mapping_table[] = {
Alice Guo8ee27642022-07-26 16:41:00 +0800111 { 7, 1, 7, 63 },
112 { 16, 8, },
113 { 17, 8, },
114 { 22, 1, 6 },
115 { 23, 1, 4 },
Alice Guob93916d2022-07-26 16:40:59 +0800116};
117#endif
Ye Li77a57122021-08-07 16:01:05 +0800118
119static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
120{
121 s32 size = ARRAY_SIZE(fsb_mapping_table);
122 s32 i, word_pos = 0;
123
124 /* map the fuse from ocotp fuse map to FSB*/
125 for (i = 0; i < size; i++) {
126 if (fsb_mapping_table[i].fuse_bank != -1 &&
Alice Guo8ee27642022-07-26 16:41:00 +0800127 fsb_mapping_table[i].fuse_bank == bank &&
128 fsb_mapping_table[i].fuse_words > word) {
Ye Li77a57122021-08-07 16:01:05 +0800129 break;
130 }
131
132 word_pos += fsb_mapping_table[i].fuse_words;
133 }
134
135 if (i == size)
136 return -1; /* Failed to find */
137
138 if (fsb_mapping_table[i].redundancy) {
139 *redundancy = true;
140 return (word >> 1) + word_pos;
141 }
142
143 *redundancy = false;
144 return word + word_pos;
145}
146
147static s32 map_s400_fuse_index(u32 bank, u32 word)
148{
149 s32 size = ARRAY_SIZE(s400_api_mapping_table);
150 s32 i;
151
152 /* map the fuse from ocotp fuse map to FSB*/
153 for (i = 0; i < size; i++) {
154 if (s400_api_mapping_table[i].fuse_bank != -1 &&
155 s400_api_mapping_table[i].fuse_bank == bank) {
156 if (word >= s400_api_mapping_table[i].fuse_offset &&
157 word < (s400_api_mapping_table[i].fuse_offset +
158 s400_api_mapping_table[i].fuse_words))
159 break;
160 }
161 }
162
163 if (i == size)
164 return -1; /* Failed to find */
165
166 if (s400_api_mapping_table[i].s400_index != 0)
167 return s400_api_mapping_table[i].s400_index;
168
169 return s400_api_mapping_table[i].fuse_bank * 8 + word;
170}
171
Alice Guo8ee27642022-07-26 16:41:00 +0800172#if defined(CONFIG_IMX8ULP)
Ye Li77a57122021-08-07 16:01:05 +0800173int fuse_sense(u32 bank, u32 word, u32 *val)
174{
175 s32 word_index;
176 bool redundancy;
177
178 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
179 return -EINVAL;
180
181 word_index = map_fsb_fuse_index(bank, word, &redundancy);
182 if (word_index >= 0) {
Alice Guob93916d2022-07-26 16:40:59 +0800183 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
Ye Li77a57122021-08-07 16:01:05 +0800184 if (redundancy)
185 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
186
187 return 0;
188 }
189
190 word_index = map_s400_fuse_index(bank, word);
191 if (word_index >= 0) {
192 u32 data[4];
193 u32 res, size = 4;
194 int ret;
195
196 /* Only UID return 4 words */
197 if (word_index != 1)
198 size = 1;
199
200 ret = ahab_read_common_fuse(word_index, data, size, &res);
201 if (ret) {
202 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
203 return ret;
204 }
205
206 if (word_index == 1) {
207 *val = data[word]; /* UID */
208 } else if (word_index == 2) {
209 /*
210 * OTFAD 3 bits as follow:
211 * bit 0: OTFAD_ENABLE
212 * bit 1: OTFAD_DISABLE_OVERRIDE
213 * bit 2: KEY_BLOB_EN
214 */
215 *val = data[0] << 3;
216 } else {
217 *val = data[0];
218 }
219
220 return 0;
221 }
222
223 return -ENOENT;
224}
Alice Guo8ee27642022-07-26 16:41:00 +0800225#elif defined(CONFIG_ARCH_IMX9)
226int fuse_sense(u32 bank, u32 word, u32 *val)
227{
228 s32 word_index;
229 bool redundancy;
230
231 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
232 return -EINVAL;
233
234 word_index = map_fsb_fuse_index(bank, word, &redundancy);
235 if (word_index >= 0) {
236 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
237 if (redundancy)
238 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
239
240 return 0;
241 }
242
243 word_index = map_s400_fuse_index(bank, word);
244 if (word_index >= 0) {
245 u32 data;
246 u32 res, size = 1;
247 int ret;
248
249 ret = ahab_read_common_fuse(word_index, &data, size, &res);
250 if (ret) {
251 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
252 return ret;
253 }
254
255 *val = data;
256
257 return 0;
258 }
259
260 return -ENOENT;
261}
262#endif
Ye Li77a57122021-08-07 16:01:05 +0800263
264int fuse_read(u32 bank, u32 word, u32 *val)
265{
266 return fuse_sense(bank, word, val);
267}
268
269int fuse_prog(u32 bank, u32 word, u32 val)
270{
271 u32 res;
272 int ret;
273
274 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
275 return -EINVAL;
276
277 ret = ahab_write_fuse((bank * 8 + word), val, false, &res);
278 if (ret) {
279 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
280 return ret;
281 }
282
283 return 0;
284}
285
286int fuse_override(u32 bank, u32 word, u32 val)
287{
288 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
289 return -EPERM;
290}