blob: 01db470e8f144e5c52963225f4c942c6e7cc4249 [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>
13#include <asm/arch/s400_api.h>
14#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
34struct fsb_map_entry fsb_mapping_table[] = {
35 { 3, 8 },
36 { 4, 8 },
37 { 5, 8 },
38 { 6, 8 },
39 { -1, 48 }, /* Reserve 48 words */
40 { 8, 4, true },
41 { 24, 4, true },
42 { 26, 4, true },
43 { 27, 4, true },
44 { 28, 8 },
45 { 29, 8 },
46 { 30, 8 },
47 { 31, 8 },
48 { 37, 8 },
49 { 38, 8 },
50 { 39, 8 },
51 { 40, 8 },
52 { 41, 8 },
53 { 42, 8 },
54 { 43, 8 },
55 { 44, 8 },
56 { 45, 8 },
57 { 46, 8 },
58};
59
60struct s400_map_entry s400_api_mapping_table[] = {
61 { 1, 8 }, /* LOCK */
62 { 2, 8 }, /* ECID */
63 { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */
Ye Li6469bc62022-04-06 14:30:16 +080064 { 15, 8 }, /* OEM SRK HASH */
Ye Li77a57122021-08-07 16:01:05 +080065 { 23, 1, 4, 2 }, /* OTFAD */
66};
67
68static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
69{
70 s32 size = ARRAY_SIZE(fsb_mapping_table);
71 s32 i, word_pos = 0;
72
73 /* map the fuse from ocotp fuse map to FSB*/
74 for (i = 0; i < size; i++) {
75 if (fsb_mapping_table[i].fuse_bank != -1 &&
76 fsb_mapping_table[i].fuse_bank == bank) {
77 break;
78 }
79
80 word_pos += fsb_mapping_table[i].fuse_words;
81 }
82
83 if (i == size)
84 return -1; /* Failed to find */
85
86 if (fsb_mapping_table[i].redundancy) {
87 *redundancy = true;
88 return (word >> 1) + word_pos;
89 }
90
91 *redundancy = false;
92 return word + word_pos;
93}
94
95static s32 map_s400_fuse_index(u32 bank, u32 word)
96{
97 s32 size = ARRAY_SIZE(s400_api_mapping_table);
98 s32 i;
99
100 /* map the fuse from ocotp fuse map to FSB*/
101 for (i = 0; i < size; i++) {
102 if (s400_api_mapping_table[i].fuse_bank != -1 &&
103 s400_api_mapping_table[i].fuse_bank == bank) {
104 if (word >= s400_api_mapping_table[i].fuse_offset &&
105 word < (s400_api_mapping_table[i].fuse_offset +
106 s400_api_mapping_table[i].fuse_words))
107 break;
108 }
109 }
110
111 if (i == size)
112 return -1; /* Failed to find */
113
114 if (s400_api_mapping_table[i].s400_index != 0)
115 return s400_api_mapping_table[i].s400_index;
116
117 return s400_api_mapping_table[i].fuse_bank * 8 + word;
118}
119
120int fuse_sense(u32 bank, u32 word, u32 *val)
121{
122 s32 word_index;
123 bool redundancy;
124
125 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
126 return -EINVAL;
127
128 word_index = map_fsb_fuse_index(bank, word, &redundancy);
129 if (word_index >= 0) {
130 *val = readl((ulong)FSB_BASE_ADDR + 0x800 + (word_index << 2));
131 if (redundancy)
132 *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
133
134 return 0;
135 }
136
137 word_index = map_s400_fuse_index(bank, word);
138 if (word_index >= 0) {
139 u32 data[4];
140 u32 res, size = 4;
141 int ret;
142
143 /* Only UID return 4 words */
144 if (word_index != 1)
145 size = 1;
146
147 ret = ahab_read_common_fuse(word_index, data, size, &res);
148 if (ret) {
149 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
150 return ret;
151 }
152
153 if (word_index == 1) {
154 *val = data[word]; /* UID */
155 } else if (word_index == 2) {
156 /*
157 * OTFAD 3 bits as follow:
158 * bit 0: OTFAD_ENABLE
159 * bit 1: OTFAD_DISABLE_OVERRIDE
160 * bit 2: KEY_BLOB_EN
161 */
162 *val = data[0] << 3;
163 } else {
164 *val = data[0];
165 }
166
167 return 0;
168 }
169
170 return -ENOENT;
171}
172
173int fuse_read(u32 bank, u32 word, u32 *val)
174{
175 return fuse_sense(bank, word, val);
176}
177
178int fuse_prog(u32 bank, u32 word, u32 val)
179{
180 u32 res;
181 int ret;
182
183 if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
184 return -EINVAL;
185
186 ret = ahab_write_fuse((bank * 8 + word), val, false, &res);
187 if (ret) {
188 printf("ahab write fuse failed %d, 0x%x\n", ret, res);
189 return ret;
190 }
191
192 return 0;
193}
194
195int fuse_override(u32 bank, u32 word, u32 val)
196{
197 printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
198 return -EPERM;
199}