1 // SPDX-License-Identifier: GPL-2.0
10 #include <asm/arch/sys_proto.h>
11 #include <asm/arch/imx-regs.h>
13 #include <asm/mach-imx/s400_api.h>
14 #include <asm/global_data.h>
16 DECLARE_GLOBAL_DATA_PTR;
19 #define WORDS_PER_BANKS 8
21 struct fsb_map_entry {
27 struct s400_map_entry {
34 #if defined(CONFIG_IMX8ULP)
35 #define FSB_OTP_SHADOW 0x800
37 struct fsb_map_entry fsb_mapping_table[] = {
40 { -1, 48 }, /* Reserve 48 words */
63 /* None ECC banks such like Redundancy or Bit protect */
64 u32 nonecc_fuse_banks[] = {
65 0, 1, 8, 12, 16, 22, 24, 25, 26, 27, 36, 41, 51, 56
68 struct s400_map_entry s400_api_mapping_table[] = {
71 { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */
72 { 15, 8 }, /* OEM SRK HASH */
73 { 23, 1, 4, 2 }, /* OTFAD */
74 { 25, 8 }, /* Test config2 */
76 { 27, 8 }, /* Test flow/USB */
86 #elif defined(CONFIG_ARCH_IMX9)
87 #define FSB_OTP_SHADOW 0x8000
89 struct fsb_map_entry fsb_mapping_table[] = {
125 struct s400_map_entry s400_api_mapping_table[] = {
134 static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
136 s32 size = ARRAY_SIZE(fsb_mapping_table);
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 &&
142 fsb_mapping_table[i].fuse_bank == bank &&
143 fsb_mapping_table[i].fuse_words > word) {
147 word_pos += fsb_mapping_table[i].fuse_words;
151 return -1; /* Failed to find */
153 if (fsb_mapping_table[i].redundancy) {
155 return (word >> 1) + word_pos;
159 return word + word_pos;
162 static s32 map_s400_fuse_index(u32 bank, u32 word)
164 s32 size = ARRAY_SIZE(s400_api_mapping_table);
167 /* map the fuse from ocotp fuse map to FSB*/
168 for (i = 0; i < size; i++) {
169 if (s400_api_mapping_table[i].fuse_bank != -1 &&
170 s400_api_mapping_table[i].fuse_bank == bank) {
171 if (word >= s400_api_mapping_table[i].fuse_offset &&
172 word < (s400_api_mapping_table[i].fuse_offset +
173 s400_api_mapping_table[i].fuse_words))
179 return -1; /* Failed to find */
181 if (s400_api_mapping_table[i].s400_index != 0)
182 return s400_api_mapping_table[i].s400_index;
184 return s400_api_mapping_table[i].fuse_bank * 8 + word;
187 #if defined(CONFIG_IMX8ULP)
188 int fuse_sense(u32 bank, u32 word, u32 *val)
193 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
196 word_index = map_fsb_fuse_index(bank, word, &redundancy);
197 if (word_index >= 0) {
198 *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
200 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
205 word_index = map_s400_fuse_index(bank, word);
206 if (word_index >= 0) {
211 /* Only UID return 4 words */
215 ret = ahab_read_common_fuse(word_index, data, size, &res);
217 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
221 if (word_index == 1) {
222 *val = data[word]; /* UID */
223 } else if (word_index == 2) {
225 * OTFAD 3 bits as follow:
226 * bit 0: OTFAD_ENABLE
227 * bit 1: OTFAD_DISABLE_OVERRIDE
240 #elif defined(CONFIG_ARCH_IMX9)
241 int fuse_sense(u32 bank, u32 word, u32 *val)
246 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
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));
253 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
258 word_index = map_s400_fuse_index(bank, word);
259 if (word_index >= 0) {
264 ret = ahab_read_common_fuse(word_index, &data, size, &res);
266 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
279 int fuse_read(u32 bank, u32 word, u32 *val)
281 return fuse_sense(bank, word, val);
284 int fuse_prog(u32 bank, u32 word, u32 val)
290 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
293 /* Lock 8ULP ECC fuse word, so second programming will return failure.
294 * iMX9 OTP can protect ECC fuse, so not need it
296 #if defined(CONFIG_IMX8ULP)
298 for (i = 0; i < ARRAY_SIZE(nonecc_fuse_banks); i++) {
299 if (nonecc_fuse_banks[i] == bank)
303 if (i == ARRAY_SIZE(nonecc_fuse_banks))
307 ret = ahab_write_fuse((bank * 8 + word), val, lock, &res);
309 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
316 int fuse_override(u32 bank, u32 word, u32 val)
318 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");