fuse.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 NXP
  4. */
  5. #include <common.h>
  6. #include <console.h>
  7. #include <errno.h>
  8. #include <fuse.h>
  9. #include <asm/arch/sys_proto.h>
  10. #include <asm/arch/imx-regs.h>
  11. #include <env.h>
  12. #include <asm/mach-imx/ele_api.h>
  13. #include <asm/global_data.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #define FUSE_BANKS 64
  16. #define WORDS_PER_BANKS 8
  17. struct fsb_map_entry {
  18. s32 fuse_bank;
  19. u32 fuse_words;
  20. bool redundancy;
  21. };
  22. struct ele_map_entry {
  23. s32 fuse_bank;
  24. u32 fuse_words;
  25. u32 fuse_offset;
  26. u32 ele_index;
  27. };
  28. #if defined(CONFIG_IMX8ULP)
  29. #define FSB_OTP_SHADOW 0x800
  30. struct fsb_map_entry fsb_mapping_table[] = {
  31. { 3, 8 },
  32. { 4, 8 },
  33. { -1, 48 }, /* Reserve 48 words */
  34. { 5, 8 },
  35. { 6, 8 },
  36. { 8, 4, true },
  37. { 24, 4, true },
  38. { 26, 4, true },
  39. { 27, 4, true },
  40. { 28, 8 },
  41. { 29, 8 },
  42. { 30, 8 },
  43. { 31, 8 },
  44. { 37, 8 },
  45. { 38, 8 },
  46. { 39, 8 },
  47. { 40, 8 },
  48. { 41, 8 },
  49. { 42, 8 },
  50. { 43, 8 },
  51. { 44, 8 },
  52. { 45, 8 },
  53. { 46, 8 },
  54. };
  55. /* None ECC banks such like Redundancy or Bit protect */
  56. u32 nonecc_fuse_banks[] = {
  57. 0, 1, 8, 12, 16, 22, 24, 25, 26, 27, 36, 41, 51, 56
  58. };
  59. struct ele_map_entry ele_api_mapping_table[] = {
  60. { 1, 8 }, /* LOCK */
  61. { 2, 8 }, /* ECID */
  62. { 7, 4, 0, 1 }, /* OTP_UNIQ_ID */
  63. { 15, 8 }, /* OEM SRK HASH */
  64. { 23, 1, 4, 2 }, /* OTFAD */
  65. { 25, 8 }, /* Test config2 */
  66. { 26, 8 }, /* PMU */
  67. { 27, 8 }, /* Test flow/USB */
  68. { 32, 8 }, /* GP1 */
  69. { 33, 8 }, /* GP2 */
  70. { 34, 8 }, /* GP3 */
  71. { 35, 8 }, /* GP4 */
  72. { 36, 8 }, /* GP5 */
  73. { 49, 8 }, /* GP8 */
  74. { 50, 8 }, /* GP9 */
  75. { 51, 8 }, /* GP10 */
  76. };
  77. #elif defined(CONFIG_ARCH_IMX9)
  78. #define FSB_OTP_SHADOW 0x8000
  79. struct fsb_map_entry fsb_mapping_table[] = {
  80. { 0, 8 },
  81. { 1, 8 },
  82. { 2, 8 },
  83. { 3, 8 },
  84. { 4, 8 },
  85. { 5, 8 },
  86. { 6, 4 },
  87. { -1, 260 },
  88. { 39, 8 },
  89. { 40, 8 },
  90. { 41, 8 },
  91. { 42, 8 },
  92. { 43, 8 },
  93. { 44, 8 },
  94. { 45, 8 },
  95. { 46, 8 },
  96. { 47, 8 },
  97. { 48, 8 },
  98. { 49, 8 },
  99. { 50, 8 },
  100. { 51, 8 },
  101. { 52, 8 },
  102. { 53, 8 },
  103. { 54, 8 },
  104. { 55, 8 },
  105. { 56, 8 },
  106. { 57, 8 },
  107. { 58, 8 },
  108. { 59, 8 },
  109. { 60, 8 },
  110. { 61, 8 },
  111. { 62, 8 },
  112. { 63, 8 },
  113. };
  114. struct ele_map_entry ele_api_mapping_table[] = {
  115. { 7, 1, 7, 63 },
  116. { 16, 8, },
  117. { 17, 8, },
  118. { 22, 1, 6 },
  119. { 23, 1, 4 },
  120. };
  121. #endif
  122. static s32 map_fsb_fuse_index(u32 bank, u32 word, bool *redundancy)
  123. {
  124. s32 size = ARRAY_SIZE(fsb_mapping_table);
  125. s32 i, word_pos = 0;
  126. /* map the fuse from ocotp fuse map to FSB*/
  127. for (i = 0; i < size; i++) {
  128. if (fsb_mapping_table[i].fuse_bank != -1 &&
  129. fsb_mapping_table[i].fuse_bank == bank &&
  130. fsb_mapping_table[i].fuse_words > word) {
  131. break;
  132. }
  133. word_pos += fsb_mapping_table[i].fuse_words;
  134. }
  135. if (i == size)
  136. return -1; /* Failed to find */
  137. if (fsb_mapping_table[i].redundancy) {
  138. *redundancy = true;
  139. return (word >> 1) + word_pos;
  140. }
  141. *redundancy = false;
  142. return word + word_pos;
  143. }
  144. static s32 map_ele_fuse_index(u32 bank, u32 word)
  145. {
  146. s32 size = ARRAY_SIZE(ele_api_mapping_table);
  147. s32 i;
  148. /* map the fuse from ocotp fuse map to FSB*/
  149. for (i = 0; i < size; i++) {
  150. if (ele_api_mapping_table[i].fuse_bank != -1 &&
  151. ele_api_mapping_table[i].fuse_bank == bank) {
  152. if (word >= ele_api_mapping_table[i].fuse_offset &&
  153. word < (ele_api_mapping_table[i].fuse_offset +
  154. ele_api_mapping_table[i].fuse_words))
  155. break;
  156. }
  157. }
  158. if (i == size)
  159. return -1; /* Failed to find */
  160. if (ele_api_mapping_table[i].ele_index != 0)
  161. return ele_api_mapping_table[i].ele_index;
  162. return ele_api_mapping_table[i].fuse_bank * 8 + word;
  163. }
  164. #if defined(CONFIG_IMX8ULP)
  165. int fuse_sense(u32 bank, u32 word, u32 *val)
  166. {
  167. s32 word_index;
  168. bool redundancy;
  169. if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
  170. return -EINVAL;
  171. word_index = map_fsb_fuse_index(bank, word, &redundancy);
  172. if (word_index >= 0) {
  173. *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
  174. if (redundancy)
  175. *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
  176. return 0;
  177. }
  178. word_index = map_ele_fuse_index(bank, word);
  179. if (word_index >= 0) {
  180. u32 data[4];
  181. u32 res, size = 4;
  182. int ret;
  183. /* Only UID return 4 words */
  184. if (word_index != 1)
  185. size = 1;
  186. ret = ele_read_common_fuse(word_index, data, size, &res);
  187. if (ret) {
  188. printf("ahab read fuse failed %d, 0x%x\n", ret, res);
  189. return ret;
  190. }
  191. if (word_index == 1) {
  192. *val = data[word]; /* UID */
  193. } else if (word_index == 2) {
  194. /*
  195. * OTFAD 3 bits as follow:
  196. * bit 0: OTFAD_ENABLE
  197. * bit 1: OTFAD_DISABLE_OVERRIDE
  198. * bit 2: KEY_BLOB_EN
  199. */
  200. *val = data[0] << 3;
  201. } else {
  202. *val = data[0];
  203. }
  204. return 0;
  205. }
  206. return -ENOENT;
  207. }
  208. #elif defined(CONFIG_ARCH_IMX9)
  209. int fuse_sense(u32 bank, u32 word, u32 *val)
  210. {
  211. s32 word_index;
  212. bool redundancy;
  213. if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
  214. return -EINVAL;
  215. word_index = map_fsb_fuse_index(bank, word, &redundancy);
  216. if (word_index >= 0) {
  217. *val = readl((ulong)FSB_BASE_ADDR + FSB_OTP_SHADOW + (word_index << 2));
  218. if (redundancy)
  219. *val = (*val >> ((word % 2) * 16)) & 0xFFFF;
  220. return 0;
  221. }
  222. word_index = map_ele_fuse_index(bank, word);
  223. if (word_index >= 0) {
  224. u32 data;
  225. u32 res, size = 1;
  226. int ret;
  227. ret = ele_read_common_fuse(word_index, &data, size, &res);
  228. if (ret) {
  229. printf("ahab read fuse failed %d, 0x%x\n", ret, res);
  230. return ret;
  231. }
  232. *val = data;
  233. return 0;
  234. }
  235. return -ENOENT;
  236. }
  237. #endif
  238. int fuse_read(u32 bank, u32 word, u32 *val)
  239. {
  240. return fuse_sense(bank, word, val);
  241. }
  242. int fuse_prog(u32 bank, u32 word, u32 val)
  243. {
  244. u32 res;
  245. int ret;
  246. bool lock = false;
  247. if (bank >= FUSE_BANKS || word >= WORDS_PER_BANKS || !val)
  248. return -EINVAL;
  249. /* Lock 8ULP ECC fuse word, so second programming will return failure.
  250. * iMX9 OTP can protect ECC fuse, so not need it
  251. */
  252. #if defined(CONFIG_IMX8ULP)
  253. u32 i;
  254. for (i = 0; i < ARRAY_SIZE(nonecc_fuse_banks); i++) {
  255. if (nonecc_fuse_banks[i] == bank)
  256. break;
  257. }
  258. if (i == ARRAY_SIZE(nonecc_fuse_banks))
  259. lock = true;
  260. #endif
  261. ret = ele_write_fuse((bank * 8 + word), val, lock, &res);
  262. if (ret) {
  263. printf("ahab write fuse failed %d, 0x%x\n", ret, res);
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. int fuse_override(u32 bank, u32 word, u32 val)
  269. {
  270. printf("Override fuse to i.MX8ULP in u-boot is forbidden\n");
  271. return -EPERM;
  272. }