Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 1 | // 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 Li | c408ed3 | 2022-07-26 16:40:49 +0800 | [diff] [blame] | 13 | #include <asm/mach-imx/s400_api.h> |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 14 | #include <asm/global_data.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | #define FUSE_BANKS 64 |
| 19 | #define WORDS_PER_BANKS 8 |
| 20 | |
| 21 | struct fsb_map_entry { |
| 22 | s32 fuse_bank; |
| 23 | u32 fuse_words; |
| 24 | bool redundancy; |
| 25 | }; |
| 26 | |
| 27 | struct s400_map_entry { |
| 28 | s32 fuse_bank; |
| 29 | u32 fuse_words; |
| 30 | u32 fuse_offset; |
| 31 | u32 s400_index; |
| 32 | }; |
| 33 | |
Alice Guo | b93916d | 2022-07-26 16:40:59 +0800 | [diff] [blame] | 34 | #if defined(CONFIG_IMX8ULP) |
| 35 | #define FSB_OTP_SHADOW 0x800 |
| 36 | |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 37 | struct fsb_map_entry fsb_mapping_table[] = { |
| 38 | { 3, 8 }, |
| 39 | { 4, 8 }, |
Peng Fan | 199fa60 | 2022-04-06 14:30:31 +0800 | [diff] [blame] | 40 | { -1, 48 }, /* Reserve 48 words */ |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 41 | { 5, 8 }, |
| 42 | { 6, 8 }, |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 43 | { 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 | |
| 63 | struct s400_map_entry s400_api_mapping_table[] = { |
| 64 | { 1, 8 }, /* LOCK */ |
| 65 | { 2, 8 }, /* ECID */ |
| 66 | { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */ |
Ye Li | 6469bc6 | 2022-04-06 14:30:16 +0800 | [diff] [blame] | 67 | { 15, 8 }, /* OEM SRK HASH */ |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 68 | { 23, 1, 4, 2 }, /* OTFAD */ |
Peng Fan | 199fa60 | 2022-04-06 14:30:31 +0800 | [diff] [blame] | 69 | { 25, 8 }, /* Test config2 */ |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 70 | }; |
Alice Guo | b93916d | 2022-07-26 16:40:59 +0800 | [diff] [blame] | 71 | #elif defined(CONFIG_ARCH_IMX9) |
| 72 | #define FSB_OTP_SHADOW 0x8000 |
| 73 | |
| 74 | struct fsb_map_entry fsb_mapping_table[] = { |
| 75 | { 0, 8 }, |
| 76 | { 1, 8 }, |
| 77 | { 2, 8 }, |
Alice Guo | 8ee2764 | 2022-07-26 16:41:00 +0800 | [diff] [blame] | 78 | { 3, 8 }, |
Alice Guo | b93916d | 2022-07-26 16:40:59 +0800 | [diff] [blame] | 79 | { 4, 8 }, |
| 80 | { 5, 8 }, |
Alice Guo | 8ee2764 | 2022-07-26 16:41:00 +0800 | [diff] [blame] | 81 | { 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 Guo | b93916d | 2022-07-26 16:40:59 +0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct s400_map_entry s400_api_mapping_table[] = { |
Alice Guo | 8ee2764 | 2022-07-26 16:41:00 +0800 | [diff] [blame] | 111 | { 7, 1, 7, 63 }, |
| 112 | { 16, 8, }, |
| 113 | { 17, 8, }, |
| 114 | { 22, 1, 6 }, |
| 115 | { 23, 1, 4 }, |
Alice Guo | b93916d | 2022-07-26 16:40:59 +0800 | [diff] [blame] | 116 | }; |
| 117 | #endif |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 118 | |
| 119 | static 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 Guo | 8ee2764 | 2022-07-26 16:41:00 +0800 | [diff] [blame] | 127 | fsb_mapping_table[i].fuse_bank == bank && |
| 128 | fsb_mapping_table[i].fuse_words > word) { |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 129 | 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 | |
| 147 | static 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 Guo | 8ee2764 | 2022-07-26 16:41:00 +0800 | [diff] [blame] | 172 | #if defined(CONFIG_IMX8ULP) |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 173 | int 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 Guo | b93916d | 2022-07-26 16:40:59 +0800 | [diff] [blame] | 183 | *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2)); |
Ye Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 184 | 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 Guo | 8ee2764 | 2022-07-26 16:41:00 +0800 | [diff] [blame] | 225 | #elif defined(CONFIG_ARCH_IMX9) |
| 226 | int 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 Li | 77a5712 | 2021-08-07 16:01:05 +0800 | [diff] [blame] | 263 | |
| 264 | int fuse_read(u32 bank, u32 word, u32 *val) |
| 265 | { |
| 266 | return fuse_sense(bank, word, val); |
| 267 | } |
| 268 | |
| 269 | int 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 | |
| 286 | int 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 | } |