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