fuse_prov.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright 2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <caam.h>
  13. #include <common/debug.h>
  14. #include <dcfg.h>
  15. #include <drivers/delay_timer.h>
  16. #include <fuse_prov.h>
  17. #include <sfp.h>
  18. #include <sfp_error_codes.h>
  19. static int write_a_fuse(uint32_t *fuse_addr, uint32_t *fuse_hdr_val,
  20. uint32_t mask)
  21. {
  22. uint32_t last_stored_val = sfp_read32(fuse_addr);
  23. /* Check if fuse already blown or not */
  24. if ((last_stored_val & mask) == mask) {
  25. return ERROR_ALREADY_BLOWN;
  26. }
  27. /* Write fuse in mirror registers */
  28. sfp_write32(fuse_addr, last_stored_val | (*fuse_hdr_val & mask));
  29. /* Read back to check if write success */
  30. if (sfp_read32(fuse_addr) != (last_stored_val | (*fuse_hdr_val & mask))) {
  31. return ERROR_WRITE;
  32. }
  33. return 0;
  34. }
  35. static int write_fuses(uint32_t *fuse_addr, uint32_t *fuse_hdr_val, uint8_t len)
  36. {
  37. int i;
  38. /* Check if fuse already blown or not */
  39. for (i = 0; i < len; i++) {
  40. if (sfp_read32(&fuse_addr[i]) != 0) {
  41. return ERROR_ALREADY_BLOWN;
  42. }
  43. }
  44. /* Write fuse in mirror registers */
  45. for (i = 0; i < len; i++) {
  46. sfp_write32(&fuse_addr[i], fuse_hdr_val[i]);
  47. }
  48. /* Read back to check if write success */
  49. for (i = 0; i < len; i++) {
  50. if (sfp_read32(&fuse_addr[i]) != fuse_hdr_val[i]) {
  51. return ERROR_WRITE;
  52. }
  53. }
  54. return 0;
  55. }
  56. /*
  57. * This function program Super Root Key Hash (SRKH) in fuse
  58. * registers.
  59. */
  60. static int prog_srkh(struct fuse_hdr_t *fuse_hdr,
  61. struct sfp_ccsr_regs_t *sfp_ccsr_regs)
  62. {
  63. int ret = 0;
  64. ret = write_fuses(sfp_ccsr_regs->srk_hash, fuse_hdr->srkh, 8);
  65. if (ret != 0) {
  66. ret = (ret == ERROR_ALREADY_BLOWN) ?
  67. ERROR_SRKH_ALREADY_BLOWN : ERROR_SRKH_WRITE;
  68. }
  69. return ret;
  70. }
  71. /* This function program OEMUID[0-4] in fuse registers. */
  72. static int prog_oemuid(struct fuse_hdr_t *fuse_hdr,
  73. struct sfp_ccsr_regs_t *sfp_ccsr_regs)
  74. {
  75. int i, ret = 0;
  76. for (i = 0; i < 5; i++) {
  77. /* Check OEMUIDx to be blown or not */
  78. if (((fuse_hdr->flags >> (FLAG_OUID0_SHIFT + i)) & 0x1) != 0) {
  79. /* Check if OEMUID[i] already blown or not */
  80. ret = write_fuses(&sfp_ccsr_regs->oem_uid[i],
  81. &fuse_hdr->oem_uid[i], 1);
  82. if (ret != 0) {
  83. ret = (ret == ERROR_ALREADY_BLOWN) ?
  84. ERROR_OEMUID_ALREADY_BLOWN
  85. : ERROR_OEMUID_WRITE;
  86. }
  87. }
  88. }
  89. return ret;
  90. }
  91. /* This function program DCV[0-1], DRV[0-1] in fuse registers. */
  92. static int prog_debug(struct fuse_hdr_t *fuse_hdr,
  93. struct sfp_ccsr_regs_t *sfp_ccsr_regs)
  94. {
  95. int ret;
  96. /* Check DCV to be blown or not */
  97. if (((fuse_hdr->flags >> (FLAG_DCV0_SHIFT)) & 0x3) != 0) {
  98. /* Check if DCV[i] already blown or not */
  99. ret = write_fuses(sfp_ccsr_regs->dcv, fuse_hdr->dcv, 2);
  100. if (ret != 0) {
  101. ret = (ret == ERROR_ALREADY_BLOWN) ?
  102. ERROR_DCV_ALREADY_BLOWN
  103. : ERROR_DCV_WRITE;
  104. }
  105. }
  106. /* Check DRV to be blown or not */
  107. if ((((fuse_hdr->flags >> (FLAG_DRV0_SHIFT)) & 0x3)) != 0) {
  108. /* Check if DRV[i] already blown or not */
  109. ret = write_fuses(sfp_ccsr_regs->drv, fuse_hdr->drv, 2);
  110. if (ret != 0) {
  111. ret = (ret == ERROR_ALREADY_BLOWN) ?
  112. ERROR_DRV_ALREADY_BLOWN
  113. : ERROR_DRV_WRITE;
  114. } else {
  115. /* Check for DRV hamming error */
  116. if (sfp_read32((void *)(get_sfp_addr()
  117. + SFP_SVHESR_OFFSET))
  118. & SFP_SVHESR_DRV_MASK) {
  119. return ERROR_DRV_HAMMING_ERROR;
  120. }
  121. }
  122. }
  123. return 0;
  124. }
  125. /*
  126. * Turn a 256-bit random value (32 bytes) into an OTPMK code word
  127. * modifying the input data array in place
  128. */
  129. static void otpmk_make_code_word_256(uint8_t *otpmk, bool minimal_flag)
  130. {
  131. int i;
  132. uint8_t parity_bit;
  133. uint8_t code_bit;
  134. if (minimal_flag == true) {
  135. /*
  136. * Force bits 252, 253, 254 and 255 to 1
  137. * This is because these fuses may have already been blown
  138. * and the OTPMK cannot force them back to 0
  139. */
  140. otpmk[252/8] |= (1 << (252%8));
  141. otpmk[253/8] |= (1 << (253%8));
  142. otpmk[254/8] |= (1 << (254%8));
  143. otpmk[255/8] |= (1 << (255%8));
  144. }
  145. /* Generate the hamming code for the code word */
  146. parity_bit = 0;
  147. code_bit = 0;
  148. for (i = 0; i < 256; i += 1) {
  149. if ((otpmk[i/8] & (1 << (i%8))) != 0) {
  150. parity_bit ^= 1;
  151. code_bit ^= i;
  152. }
  153. }
  154. /* Inverting otpmk[code_bit] will cause the otpmk
  155. * to become a valid code word (except for overall parity)
  156. */
  157. if (code_bit < 252) {
  158. otpmk[code_bit/8] ^= (1 << (code_bit % 8));
  159. parity_bit ^= 1; // account for flipping a bit changing parity
  160. } else {
  161. /* Invert two bits: (code_bit - 4) and 4
  162. * Because we invert two bits, no need to touch the parity bit
  163. */
  164. otpmk[(code_bit - 4)/8] ^= (1 << ((code_bit - 4) % 8));
  165. otpmk[4/8] ^= (1 << (4 % 8));
  166. }
  167. /* Finally, adjust the overall parity of the otpmk
  168. * otpmk bit 0
  169. */
  170. otpmk[0] ^= parity_bit;
  171. }
  172. /* This function program One Time Programmable Master Key (OTPMK)
  173. * in fuse registers.
  174. */
  175. static int prog_otpmk(struct fuse_hdr_t *fuse_hdr,
  176. struct sfp_ccsr_regs_t *sfp_ccsr_regs)
  177. {
  178. int ret = 0;
  179. uint32_t otpmk_flags;
  180. uint32_t otpmk_random[8] __aligned(CACHE_WRITEBACK_GRANULE);
  181. otpmk_flags = (fuse_hdr->flags >> (FLAG_OTPMK_SHIFT)) & FLAG_OTPMK_MASK;
  182. switch (otpmk_flags) {
  183. case PROG_OTPMK_MIN:
  184. memset(fuse_hdr->otpmk, 0, sizeof(fuse_hdr->otpmk));
  185. /* Minimal OTPMK value (252-255 bits set to 1) */
  186. fuse_hdr->otpmk[0] |= OTPMK_MIM_BITS_MASK;
  187. break;
  188. case PROG_OTPMK_RANDOM:
  189. if (is_sec_enabled() == false) {
  190. ret = ERROR_OTPMK_SEC_DISABLED;
  191. goto out;
  192. }
  193. /* Generate Random number using CAAM for OTPMK */
  194. memset(otpmk_random, 0, sizeof(otpmk_random));
  195. if (get_rand_bytes_hw((uint8_t *)otpmk_random,
  196. sizeof(otpmk_random)) != 0) {
  197. ret = ERROR_OTPMK_SEC_ERROR;
  198. goto out;
  199. }
  200. /* Run hamming over random no. to make OTPMK */
  201. otpmk_make_code_word_256((uint8_t *)otpmk_random, false);
  202. /* Swap OTPMK */
  203. fuse_hdr->otpmk[0] = otpmk_random[7];
  204. fuse_hdr->otpmk[1] = otpmk_random[6];
  205. fuse_hdr->otpmk[2] = otpmk_random[5];
  206. fuse_hdr->otpmk[3] = otpmk_random[4];
  207. fuse_hdr->otpmk[4] = otpmk_random[3];
  208. fuse_hdr->otpmk[5] = otpmk_random[2];
  209. fuse_hdr->otpmk[6] = otpmk_random[1];
  210. fuse_hdr->otpmk[7] = otpmk_random[0];
  211. break;
  212. case PROG_OTPMK_USER:
  213. break;
  214. case PROG_OTPMK_RANDOM_MIN:
  215. /* Here assumption is that user is aware of minimal OTPMK
  216. * already blown.
  217. */
  218. /* Generate Random number using CAAM for OTPMK */
  219. if (is_sec_enabled() == false) {
  220. ret = ERROR_OTPMK_SEC_DISABLED;
  221. goto out;
  222. }
  223. memset(otpmk_random, 0, sizeof(otpmk_random));
  224. if (get_rand_bytes_hw((uint8_t *)otpmk_random,
  225. sizeof(otpmk_random)) != 0) {
  226. ret = ERROR_OTPMK_SEC_ERROR;
  227. goto out;
  228. }
  229. /* Run hamming over random no. to make OTPMK */
  230. otpmk_make_code_word_256((uint8_t *)otpmk_random, true);
  231. /* Swap OTPMK */
  232. fuse_hdr->otpmk[0] = otpmk_random[7];
  233. fuse_hdr->otpmk[1] = otpmk_random[6];
  234. fuse_hdr->otpmk[2] = otpmk_random[5];
  235. fuse_hdr->otpmk[3] = otpmk_random[4];
  236. fuse_hdr->otpmk[4] = otpmk_random[3];
  237. fuse_hdr->otpmk[5] = otpmk_random[2];
  238. fuse_hdr->otpmk[6] = otpmk_random[1];
  239. fuse_hdr->otpmk[7] = otpmk_random[0];
  240. break;
  241. case PROG_OTPMK_USER_MIN:
  242. /*
  243. * Here assumption is that user is aware of minimal OTPMK
  244. * already blown. Check if minimal bits are set in user
  245. * supplied OTPMK.
  246. */
  247. if ((fuse_hdr->otpmk[0] & OTPMK_MIM_BITS_MASK) !=
  248. OTPMK_MIM_BITS_MASK) {
  249. ret = ERROR_OTPMK_USER_MIN;
  250. goto out;
  251. }
  252. break;
  253. default:
  254. ret = 0;
  255. goto out;
  256. }
  257. ret = write_fuses(sfp_ccsr_regs->otpmk, fuse_hdr->otpmk, 8);
  258. if (ret != 0) {
  259. ret = (ret == ERROR_ALREADY_BLOWN) ?
  260. ERROR_OTPMK_ALREADY_BLOWN
  261. : ERROR_OTPMK_WRITE;
  262. } else {
  263. /* Check for DRV hamming error */
  264. if ((sfp_read32((void *)(get_sfp_addr() + SFP_SVHESR_OFFSET))
  265. & SFP_SVHESR_OTPMK_MASK) != 0) {
  266. ret = ERROR_OTPMK_HAMMING_ERROR;
  267. }
  268. }
  269. out:
  270. return ret;
  271. }
  272. /* This function program OSPR1 in fuse registers.
  273. */
  274. static int prog_ospr1(struct fuse_hdr_t *fuse_hdr,
  275. struct sfp_ccsr_regs_t *sfp_ccsr_regs)
  276. {
  277. int ret;
  278. uint32_t mask = 0;
  279. #ifdef NXP_SFP_VER_3_4
  280. if (((fuse_hdr->flags >> FLAG_MC_SHIFT) & 0x1) != 0) {
  281. mask = OSPR1_MC_MASK;
  282. }
  283. #endif
  284. if (((fuse_hdr->flags >> FLAG_DBG_LVL_SHIFT) & 0x1) != 0) {
  285. mask = mask | OSPR1_DBG_LVL_MASK;
  286. }
  287. ret = write_a_fuse(&sfp_ccsr_regs->ospr1, &fuse_hdr->ospr1, mask);
  288. if (ret != 0) {
  289. ret = (ret == ERROR_ALREADY_BLOWN) ?
  290. ERROR_OSPR1_ALREADY_BLOWN
  291. : ERROR_OSPR1_WRITE;
  292. }
  293. return ret;
  294. }
  295. /* This function program SYSCFG in fuse registers.
  296. */
  297. static int prog_syscfg(struct fuse_hdr_t *fuse_hdr,
  298. struct sfp_ccsr_regs_t *sfp_ccsr_regs)
  299. {
  300. int ret;
  301. /* Check if SYSCFG already blown or not */
  302. ret = write_a_fuse(&sfp_ccsr_regs->ospr, &fuse_hdr->sc, OSPR0_SC_MASK);
  303. if (ret != 0) {
  304. ret = (ret == ERROR_ALREADY_BLOWN) ?
  305. ERROR_SC_ALREADY_BLOWN
  306. : ERROR_SC_WRITE;
  307. }
  308. return ret;
  309. }
  310. /* This function does fuse provisioning.
  311. */
  312. int provision_fuses(unsigned long long fuse_scr_addr,
  313. bool en_povdd_status)
  314. {
  315. struct fuse_hdr_t *fuse_hdr = NULL;
  316. struct sfp_ccsr_regs_t *sfp_ccsr_regs = (void *)(get_sfp_addr()
  317. + SFP_FUSE_REGS_OFFSET);
  318. int ret = 0;
  319. fuse_hdr = (struct fuse_hdr_t *)fuse_scr_addr;
  320. /*
  321. * Check for Write Protect (WP) fuse. If blown then do
  322. * no fuse provisioning.
  323. */
  324. if ((sfp_read32(&sfp_ccsr_regs->ospr) & 0x1) != 0) {
  325. goto out;
  326. }
  327. /* Check if SRKH to be blown or not */
  328. if (((fuse_hdr->flags >> FLAG_SRKH_SHIFT) & 0x1) != 0) {
  329. INFO("Fuse: Program SRKH\n");
  330. ret = prog_srkh(fuse_hdr, sfp_ccsr_regs);
  331. if (ret != 0) {
  332. error_handler(ret);
  333. goto out;
  334. }
  335. }
  336. /* Check if OEMUID to be blown or not */
  337. if (((fuse_hdr->flags >> FLAG_OUID0_SHIFT) & FLAG_OUID_MASK) != 0) {
  338. INFO("Fuse: Program OEMUIDs\n");
  339. ret = prog_oemuid(fuse_hdr, sfp_ccsr_regs);
  340. if (ret != 0) {
  341. error_handler(ret);
  342. goto out;
  343. }
  344. }
  345. /* Check if Debug values to be blown or not */
  346. if (((fuse_hdr->flags >> FLAG_DCV0_SHIFT) & FLAG_DEBUG_MASK) != 0) {
  347. INFO("Fuse: Program Debug values\n");
  348. ret = prog_debug(fuse_hdr, sfp_ccsr_regs);
  349. if (ret != 0) {
  350. error_handler(ret);
  351. goto out;
  352. }
  353. }
  354. /* Check if OTPMK values to be blown or not */
  355. if (((fuse_hdr->flags >> FLAG_OTPMK_SHIFT) & PROG_NO_OTPMK) !=
  356. PROG_NO_OTPMK) {
  357. INFO("Fuse: Program OTPMK\n");
  358. ret = prog_otpmk(fuse_hdr, sfp_ccsr_regs);
  359. if (ret != 0) {
  360. error_handler(ret);
  361. goto out;
  362. }
  363. }
  364. /* Check if MC or DBG LVL to be blown or not */
  365. if ((((fuse_hdr->flags >> FLAG_MC_SHIFT) & 0x1) != 0) ||
  366. (((fuse_hdr->flags >> FLAG_DBG_LVL_SHIFT) & 0x1) != 0)) {
  367. INFO("Fuse: Program OSPR1\n");
  368. ret = prog_ospr1(fuse_hdr, sfp_ccsr_regs);
  369. if (ret != 0) {
  370. error_handler(ret);
  371. goto out;
  372. }
  373. }
  374. /* Check if SYSCFG to be blown or not */
  375. if (((fuse_hdr->flags >> FLAG_SYSCFG_SHIFT) & 0x1) != 0) {
  376. INFO("Fuse: Program SYSCFG\n");
  377. ret = prog_syscfg(fuse_hdr, sfp_ccsr_regs);
  378. if (ret != 0) {
  379. error_handler(ret);
  380. goto out;
  381. }
  382. }
  383. if (en_povdd_status) {
  384. ret = sfp_program_fuses();
  385. if (ret != 0) {
  386. error_handler(ret);
  387. goto out;
  388. }
  389. }
  390. out:
  391. return ret;
  392. }