light-efuse-hal.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2021 Alibaba Group Holding Limited.
  4. *
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include "efuse-api.h"
  15. //#define DEBUG_INFO
  16. #ifndef ARRAY_SIZE
  17. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  18. #endif
  19. #define EFUSE_LIT_BLOCK_BIT_WIDTH 128
  20. #define EFUSE_BIG_BLOCK_BIT_WIDTH 256
  21. #define EFUSE_BYTES_PER_LIT_BLOCK (EFUSE_LIT_BLOCK_BIT_WIDTH >> 3)
  22. #define EFUSE_BYTES_PER_BIG_BLOCK (EFUSE_BIG_BLOCK_BIT_WIDTH >> 3)
  23. struct func_efuse_info {
  24. const char *func_name;
  25. unsigned int block_id;
  26. unsigned int addr; /* in byte */
  27. size_t len; /* in byte */
  28. unsigned int shift; /* left-shift bit within a byte*/
  29. unsigned int mask; /* mask within a byte */
  30. };
  31. struct func_efuse_info efuse_func_array[] = {
  32. {"UID", 5, 0x50, 20, 0, 0xff},
  33. {"USR_DSP0_JTAG_MODE", 8, 0x86, 1, 0, 0xff},
  34. {"USR_DSP1_JTAG_MODE", 8, 0x87, 1, 0, 0xff},
  35. {"USR_C910T_JTAG_MODE", 8, 0x88, 1, 0, 0xff},
  36. {"USR_C910R_JTAG_MODE", 8, 0x89, 1, 0, 0xff},
  37. {"USR_C906_JTAG_MODE", 8, 0x8a, 1, 0, 0xff},
  38. {"USR_E902_JTAG_MODE", 8, 0x8b, 1, 0, 0xff},
  39. {"USR_CHIP_DBG_MODE", 8, 0x8c, 1, 0, 0xff},
  40. {"USR_DFT_MODE", 8, 0x8d, 1, 0, 0xff},
  41. {"BOOT_OFFSET", 9, 0x90, 4, 0, 0xff},
  42. {"BOOT_INDEX", 9, 0x94, 1, 0, 0xff},
  43. {"BOOT_OFFSET_BAK", 9, 0x95, 4, 0, 0xff},
  44. {"BOOT_INDEX_BAK", 9, 0x99, 1, 0, 0xff},
  45. {"USR_USB_FASTBOOT_DIS", 9, 0x9a, 1, 4, 0x0f},
  46. {"USR_BROM_CCT_DIS", 9, 0x9a, 1, 0, 0x0f},
  47. {"IMAGE_BL2_ENC", 9, 0x9b, 1, 0, 0xff},
  48. {"IMAGE_BL3_ENC", 9, 0x9c, 1, 0, 0xff},
  49. {"IMAGE_BL4_ENC", 9, 0x9d, 1, 0, 0xff},
  50. {"BL1VERSION", 10, 0xa0, 8, 0, 0xff},
  51. {"BL2VERSION", 10, 0xa8, 8, 0, 0xff},
  52. {"SECURE_BOOT", 1, 0x10, 1, 0, 0xff},
  53. {"HASH_DEBUGPK", 25, 0x190, 32, 0, 0xff},
  54. {"BROM_DCACHE_EN", 1, 0x12, 1, 2, 0x3},
  55. {"GMAC0_MAC", 11, 0xb0, 6, 0, 0xff},
  56. {"GMAC1_MAC", 11, 0xb8, 6, 0, 0xff},
  57. {},
  58. };
  59. static const char *efuse_file = "/sys/bus/nvmem/devices/light-efuse0/nvmem";
  60. size_t efuse_read(int fd, void *buf, const char *func_name)
  61. {
  62. unsigned int offset, mask, shift;
  63. size_t len, ret;
  64. int i, block;
  65. const char *name;
  66. off_t pos;
  67. for (i = 0; i < ARRAY_SIZE(efuse_func_array); i++) {
  68. if (!strcmp(func_name, efuse_func_array[i].func_name))
  69. break;
  70. }
  71. if (i >= ARRAY_SIZE(efuse_func_array)) {
  72. printf("invalid efuse function name(%s)\n", name);
  73. return -EINVAL;
  74. }
  75. block = efuse_func_array[i].block_id;
  76. name = efuse_func_array[i].func_name;
  77. offset = efuse_func_array[i].addr;
  78. len = efuse_func_array[i].len;
  79. shift = efuse_func_array[i].shift;
  80. mask = efuse_func_array[i].mask;
  81. #ifdef DEBUG_INFO
  82. printf("[efuse info]: block: %d, name: %s, addr: 0x%x, len: %d mask: 0x%x\n",
  83. block, name, offset, (int)len, mask);
  84. #endif
  85. pos = lseek(fd, offset, SEEK_SET);
  86. if (pos < 0) {
  87. perror("failed to lseek offset to read");
  88. return -errno;
  89. }
  90. #ifdef DEBUG_INFO
  91. printf("efuse pos = %p\n", pos);
  92. #endif
  93. if (mask != 0xff) {
  94. unsigned char data;
  95. unsigned char rd_buf[len];
  96. ret = read(fd, rd_buf, len);
  97. if (ret != len) {
  98. #ifdef DEBUG_INFO
  99. printf("real read len: %d, expected read len: %d\n", ret, len);
  100. #endif
  101. perror("failed to read");
  102. ret = -errno;
  103. goto out;
  104. }
  105. data = (rd_buf[0] >> shift) & mask;
  106. #ifdef DEBUG_INFO
  107. printf("data = 0x%x\n", data);
  108. #endif
  109. memcpy(buf, &data, 1);
  110. if (len > 1) {
  111. len = len - 1;
  112. buf = (unsigned char *)buf + 1;
  113. memcpy(buf, &rd_buf[1], len);
  114. }
  115. } else { /* mask == 0xff */
  116. ret = read(fd, buf, len);
  117. if (ret != len) {
  118. #ifdef DEBUG_INFO
  119. printf("real read len: %d, expected read len: %d\n", ret, len);
  120. #endif
  121. perror("failed to read");
  122. ret = -errno;
  123. }
  124. }
  125. out:
  126. return ret;
  127. }
  128. size_t efuse_write(int fd, const void *buf, const char *func_name)
  129. {
  130. unsigned int offset, mask, shift;
  131. size_t len, ret;
  132. int i, block;
  133. const char *name;
  134. off_t pos;
  135. for (i = 0; i < ARRAY_SIZE(efuse_func_array); i++) {
  136. if (!strcmp(func_name, efuse_func_array[i].func_name))
  137. break;
  138. }
  139. if (i >= ARRAY_SIZE(efuse_func_array)) {
  140. printf("invalid efuse function name(%s)\n", name);
  141. return -EINVAL;
  142. }
  143. block = efuse_func_array[i].block_id;
  144. name = efuse_func_array[i].func_name;
  145. offset = efuse_func_array[i].addr;
  146. len = efuse_func_array[i].len;
  147. shift = efuse_func_array[i].shift;
  148. mask = efuse_func_array[i].mask;
  149. #ifdef DEBUG_INFO
  150. printf("efuse info: block: %d, name: %s, addr: 0x%x, len: %d, mask: 0x%x\n",
  151. block, name, offset, (int)len, mask);
  152. #endif
  153. pos = lseek(fd, offset, SEEK_SET);
  154. if (pos < 0) {
  155. perror("failed to lseek offset to read");
  156. return -errno;
  157. }
  158. #ifdef DEBUG_INFO
  159. printf("efuse pos = %p\n", pos);
  160. #endif
  161. if (mask != 0xff) {
  162. unsigned char data;
  163. unsigned char wr_buf[len];
  164. memcpy(wr_buf, buf, len);
  165. ret = read(fd, &data, 1);
  166. if (ret < 0) {
  167. perror("failed to read");
  168. ret = -errno;
  169. goto out;
  170. }
  171. data &= ~(mask << shift);
  172. data |= (wr_buf[0] & mask) << shift;
  173. memcpy(&wr_buf[0], &data, 1);
  174. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  175. perror("failed to lseek offset to write");
  176. ret = -errno;
  177. goto out;
  178. }
  179. ret = write(fd, wr_buf, len);
  180. if (ret != len) {
  181. perror("failed to write");
  182. ret = -errno;
  183. }
  184. } else { /* mask == 0xff */
  185. ret = write(fd, buf, len);
  186. if (ret != len) {
  187. perror("failed to write");
  188. ret = -errno;
  189. }
  190. }
  191. out:
  192. return ret;
  193. }
  194. /* according to FuseMap_v1.2.1.xlsx */
  195. size_t efuse_block_read(int fd, unsigned char *buf, unsigned int block_num)
  196. {
  197. unsigned int width, offset, bytes;
  198. size_t ret;
  199. if (block_num >= 42 && block_num <= 47)
  200. width = EFUSE_BIG_BLOCK_BIT_WIDTH;
  201. else
  202. width = EFUSE_LIT_BLOCK_BIT_WIDTH;
  203. if (block_num <= 42)
  204. offset = block_num * EFUSE_BYTES_PER_LIT_BLOCK;
  205. else if (block_num > 42 && block_num <= 48)
  206. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + (block_num - 42) * EFUSE_BYTES_PER_BIG_BLOCK;
  207. else
  208. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + 6 * EFUSE_BYTES_PER_BIG_BLOCK +
  209. (block_num - 48) * EFUSE_BYTES_PER_LIT_BLOCK;
  210. bytes = width / 8;
  211. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  212. perror("failed to lseek offset to read");
  213. return -errno;
  214. }
  215. ret = read(fd, buf, bytes);
  216. if (ret != bytes) {
  217. perror("failed to read");
  218. ret = -errno;
  219. }
  220. return ret;
  221. }
  222. /* according to FuseMap_v1.2.1.xlsx */
  223. size_t efuse_block_write(int fd, unsigned char *buf, unsigned int block_num)
  224. {
  225. unsigned int width, offset, bytes;
  226. size_t ret;
  227. if (block_num >= 42 && block_num <= 47)
  228. width = EFUSE_BIG_BLOCK_BIT_WIDTH;
  229. else
  230. width = EFUSE_LIT_BLOCK_BIT_WIDTH;
  231. if (block_num <= 42)
  232. offset = block_num * EFUSE_BYTES_PER_LIT_BLOCK;
  233. else if (block_num > 42 && block_num <= 48)
  234. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + (block_num - 42) * EFUSE_BYTES_PER_BIG_BLOCK;
  235. else
  236. offset = 42 * EFUSE_BYTES_PER_LIT_BLOCK + 6 * EFUSE_BYTES_PER_BIG_BLOCK +
  237. (block_num - 48) * EFUSE_BYTES_PER_LIT_BLOCK;
  238. bytes = width / 8;
  239. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  240. perror("failed to lseek offset to write");
  241. return -errno;
  242. }
  243. ret = write(fd, buf, bytes);
  244. if (ret != bytes) {
  245. perror("failed to write");
  246. ret = -errno;
  247. }
  248. return ret;
  249. }
  250. /**
  251. * csi_efuse_get_chipid() - Get chip id in eFuse
  252. *
  253. * @chip_id: pointer to the buffer to store chip id
  254. *
  255. * Return: 0 on success or negative code on failure
  256. */
  257. int csi_efuse_get_chipid(void *chip_id)
  258. {
  259. int ret, fd;
  260. assert(chip_id);
  261. fd = open(efuse_file, O_RDONLY);
  262. if (fd < 0) {
  263. printf("failed to open efuse device: %s\n", efuse_file);
  264. perror("open");
  265. return -errno;
  266. }
  267. ret = efuse_read(fd, chip_id, "UID");
  268. if (ret < 0)
  269. printf("failed to get 'UID' from efuse\n");
  270. else
  271. ret = 0;
  272. close(fd);
  273. return ret;
  274. }
  275. /**
  276. * csi_efuse_get_user_dbg_mode() - Get debug mode in user area
  277. *
  278. * @type: Debug type
  279. *
  280. * @dbg_mode: pointer to the buffer store debug mode
  281. *
  282. * Return: 0 on success or negative code on failure
  283. */
  284. int csi_efuse_get_user_dbg_mode(efuse_dbg_type_t type, efuse_dbg_mode_t *dbg_mode)
  285. {
  286. int ret, fd;
  287. char *mode_name;
  288. assert(dbg_mode);
  289. if ((type != USR_DSP0_JTAG) && (type != USR_DSP1_JTAG) &&
  290. (type != USR_C910T_JTAG) && (type != USR_C910R_JTAG) &&
  291. (type != USR_C906_JTAG) && (type != USR_E902_JTAG) &&
  292. (type != USR_CHIP_DBG) && (type != USR_DFT))
  293. return -EINVAL;
  294. switch (type) {
  295. case USR_DSP0_JTAG:
  296. mode_name = "USR_DSP0_JTAG_MODE";
  297. break;
  298. case USR_DSP1_JTAG:
  299. mode_name = "USR_DSP1_JTAG_MODE";
  300. break;
  301. case USR_C910T_JTAG:
  302. mode_name = "USR_C910T_JTAG_MODE";
  303. break;
  304. case USR_C910R_JTAG:
  305. mode_name = "USR_C910R_JTAG_MODE";
  306. break;
  307. case USR_C906_JTAG:
  308. mode_name = "USR_C906_JTAG_MODE";
  309. break;
  310. case USR_E902_JTAG:
  311. mode_name = "USR_E902_JTAG_MODE";
  312. break;
  313. case USR_CHIP_DBG:
  314. mode_name = "USR_CHIP_DBG_MODE";
  315. break;
  316. case USR_DFT:
  317. mode_name = "USR_DFT_MODE";
  318. break;
  319. }
  320. fd = open(efuse_file, O_RDONLY);
  321. if (fd < 0) {
  322. printf("failed to open efuse device: %s\n", efuse_file);
  323. return -errno;
  324. }
  325. ret = efuse_read(fd, dbg_mode, mode_name);
  326. if (ret < 0)
  327. printf("failed to get %s from efuse\n", mode_name);
  328. else
  329. ret = 0;
  330. close(fd);
  331. return ret;
  332. }
  333. /**
  334. * csi_efuse_set_user_dbg_mode() - Set debug mode in user area
  335. *
  336. * @type: Debug type
  337. *
  338. * @dbg_mode: debug mode
  339. *
  340. * Return: 0 on success or negative code on failure
  341. */
  342. int csi_efuse_set_user_dbg_mode(efuse_dbg_type_t type, efuse_dbg_mode_t dbg_mode)
  343. {
  344. int ret, fd;
  345. char *mode_name;
  346. if ((type != USR_DSP0_JTAG) && (type != USR_DSP1_JTAG) &&
  347. (type != USR_C910T_JTAG) && (type != USR_C910R_JTAG) &&
  348. (type != USR_C906_JTAG) && (type != USR_E902_JTAG) &&
  349. (type != USR_CHIP_DBG) && (type != USR_DFT))
  350. return -EINVAL;
  351. if ((dbg_mode != DBG_MODE_ENABLE) && (dbg_mode != DBG_MODE_PWD_PROTECT) &&
  352. (dbg_mode != DBG_MODE_DISABLE))
  353. return -EINVAL;
  354. switch (type) {
  355. case USR_DSP0_JTAG:
  356. mode_name = "USR_DSP0_JTAG_MODE";
  357. break;
  358. case USR_DSP1_JTAG:
  359. mode_name = "USR_DSP1_JTAG_MODE";
  360. break;
  361. case USR_C910T_JTAG:
  362. mode_name = "USR_C910T_JTAG_MODE";
  363. break;
  364. case USR_C910R_JTAG:
  365. mode_name = "USR_C910R_JTAG_MODE";
  366. break;
  367. case USR_C906_JTAG:
  368. mode_name = "USR_C906_JTAG_MODE";
  369. break;
  370. case USR_E902_JTAG:
  371. mode_name = "USR_E902_JTAG_MODE";
  372. break;
  373. case USR_CHIP_DBG:
  374. mode_name = "USR_CHIP_DBG_MODE";
  375. break;
  376. case USR_DFT:
  377. mode_name = "USR_DFT_MODE";
  378. break;
  379. }
  380. fd = open(efuse_file, O_RDWR);
  381. if (fd < 0) {
  382. printf("failed to open efuse device: %s\n", efuse_file);
  383. return -errno;
  384. }
  385. ret = efuse_write(fd, (unsigned char *)&dbg_mode, mode_name);
  386. if (ret < 0)
  387. printf("failed to set %s into efuse\n", mode_name);
  388. else
  389. ret = 0;
  390. close(fd);
  391. return ret;
  392. }
  393. /**
  394. * csi_efuse_get_boot_offset() - Get BL1's offset in boot media
  395. *
  396. * @offset: pointer to the buffer to store BL1's offset
  397. *
  398. * Return: 0 on success or negative code on failure
  399. */
  400. int csi_efuse_get_boot_offset(unsigned int *offset)
  401. {
  402. int ret, fd;
  403. assert(offset);
  404. fd = open(efuse_file, O_RDONLY);
  405. if (fd < 0) {
  406. printf("failed to open efuse device: %s\n", efuse_file);
  407. return -errno;
  408. }
  409. ret = efuse_read(fd, offset, "BOOT_OFFSET");
  410. if (ret < 0)
  411. printf("failed to get 'BOOT_OFFSET' from efuse\n");
  412. else
  413. ret = 0;
  414. close(fd);
  415. return ret;
  416. }
  417. /**
  418. * csi_efuse_set_boot_offset() - Set BL1's offset in boot media
  419. *
  420. * @offset: Offset value to be set
  421. *
  422. * Return: 0 on success or negative code on failure
  423. */
  424. int csi_efuse_set_boot_offset(unsigned int offset)
  425. {
  426. int ret, fd;
  427. fd = open(efuse_file, O_RDWR);
  428. if (fd < 0) {
  429. printf("failed to open efuse device: %s\n", efuse_file);
  430. return -errno;
  431. }
  432. ret = efuse_write(fd, &offset, "BOOT_OFFSET");
  433. if (ret < 0)
  434. printf("failed to set 'BOOT_OFFSET' into efuse\n");
  435. else
  436. ret = 0;
  437. close(fd);
  438. return ret;
  439. }
  440. /**
  441. * csi_efuse_get_boot_index() - Get BL1's index in boot media
  442. *
  443. * @index: pointer to the buffer to store BL1's index
  444. *
  445. * Return: 0 on success or negative code on failure
  446. */
  447. int csi_efuse_get_boot_index(unsigned char *index)
  448. {
  449. int ret, fd;
  450. assert(index);
  451. fd = open(efuse_file, O_RDONLY);
  452. if (fd < 0) {
  453. printf("failed to open efuse device: %s\n", efuse_file);
  454. return -errno;
  455. }
  456. ret = efuse_read(fd, index, "BOOT_INDEX");
  457. if (ret < 0)
  458. printf("failed to get 'BOOT_INDEX' from efuse\n");
  459. else
  460. ret = 0;
  461. close(fd);
  462. return ret;
  463. }
  464. /**
  465. * csi_efuse_set_boot_index() - Set BL1's index in boot media
  466. *
  467. * @index: index value to be set
  468. *
  469. * Return: 0 on success or negative code on failure
  470. */
  471. int csi_efuse_set_boot_index(const unsigned char index)
  472. {
  473. int ret, fd;
  474. fd = open(efuse_file, O_RDWR);
  475. if (fd < 0) {
  476. printf("failed to open efuse device: %s\n", efuse_file);
  477. return -errno;
  478. }
  479. ret = efuse_write(fd, &index, "BOOT_INDEX");
  480. if (ret < 0)
  481. printf("failed to set 'BOOT_INDEX' into efuse\n");
  482. else
  483. ret = 0;
  484. close(fd);
  485. return ret;
  486. }
  487. /**
  488. * csi_efuse_get_bak_boot_offset() - Get BL1's offset in boot media
  489. *
  490. * @offset: pointer to the buffer to store BL1's offset
  491. *
  492. * Return: 0 on success or negative code on failure
  493. */
  494. int csi_efuse_get_bak_boot_offset(unsigned int *offset)
  495. {
  496. int ret, fd;
  497. assert(offset);
  498. fd = open(efuse_file, O_RDONLY);
  499. if (fd < 0) {
  500. printf("failed to open efuse device: %s\n", efuse_file);
  501. return -errno;
  502. }
  503. ret = efuse_read(fd, offset, "BOOT_OFFSET_BAK");
  504. if (ret < 0)
  505. printf("failed to get 'BOOT_OFFSET_BAK' from efuse\n");
  506. else
  507. ret = 0;
  508. close(fd);
  509. return ret;
  510. }
  511. /**
  512. * csi_efuse_set_boot_offset() - Set BL1's offset in boot media
  513. *
  514. * @offset: Offset value to be set
  515. *
  516. * Return: 0 on success or negative code on failure
  517. */
  518. int csi_efuse_set_bak_boot_offset(unsigned int offset)
  519. {
  520. int ret, fd;
  521. fd = open(efuse_file, O_RDWR);
  522. if (fd < 0) {
  523. printf("failed to open efuse device: %s\n", efuse_file);
  524. return -errno;
  525. }
  526. ret = efuse_write(fd, &offset, "BOOT_OFFSET_BAK");
  527. if (ret < 0)
  528. printf("failed to set 'BOOT_OFFSET_BAK' into efuse\n");
  529. else
  530. ret = 0;
  531. close(fd);
  532. return ret;
  533. }
  534. /**
  535. * csi_efuse_get_bak_boot_index() - Get BL1's index in boot media
  536. *
  537. * @index: pointer to the buffer to store BL1's index
  538. *
  539. * Return: 0 on success or negative code on failure
  540. */
  541. int csi_efuse_get_bak_boot_index(unsigned char *index)
  542. {
  543. int ret, fd;
  544. assert(index);
  545. fd = open(efuse_file, O_RDONLY);
  546. if (fd < 0) {
  547. printf("failed to open efuse device: %s\n", efuse_file);
  548. return -errno;
  549. }
  550. ret = efuse_read(fd, index, "BOOT_INDEX_BAK");
  551. if (ret < 0)
  552. printf("failed to get 'BOOT_INDEX_BAK' from efuse\n");
  553. else
  554. ret = 0;
  555. close(fd);
  556. return ret;
  557. }
  558. /**
  559. * csi_efuse_set_bak_boot_index() - Set BL1's index in boot media
  560. *
  561. * @index: index value to be set
  562. *
  563. * Return: 0 on success or negative code on failure
  564. */
  565. int csi_efuse_set_bak_boot_index(unsigned char index)
  566. {
  567. int ret, fd;
  568. fd = open(efuse_file, O_RDWR);
  569. if (fd < 0) {
  570. printf("failed to open efuse device: %s\n", efuse_file);
  571. return -errno;
  572. }
  573. ret = efuse_write(fd, &index, "BOOT_INDEX_BAK");
  574. if (ret < 0)
  575. printf("failed to set 'BOOT_INDEX_BAK' into efuse\n");
  576. else
  577. ret = 0;
  578. close(fd);
  579. return ret;
  580. }
  581. /**
  582. * csi_efuse_get_usr_brom_usb_fastboot_st() - Get bootrom USB fastboots tatus in user area
  583. *
  584. * @status: pointer to the buffer to store USB fastboot status
  585. *
  586. * Return: 0 on success or negative code on failure
  587. */
  588. int csi_efuse_get_usr_brom_usb_fastboot_st(brom_usbboot_st_t *status)
  589. {
  590. int ret, fd;
  591. unsigned char tempdata;
  592. assert(status);
  593. fd = open(efuse_file, O_RDONLY);
  594. if (fd < 0) {
  595. printf("failed to open efuse device: %s\n", efuse_file);
  596. return -errno;
  597. }
  598. ret = efuse_read(fd, &tempdata, "USR_USB_FASTBOOT_DIS");
  599. if (ret < 0)
  600. printf("failed to get 'USR_USB_FASTBOOT_DIS' from efuse\n");
  601. else
  602. ret = 0;
  603. if (tempdata == 0xa)
  604. *status = BROM_USBBOOT_DIS;
  605. else if (tempdata != BROM_USBBOOT_EN)
  606. ret = -EINVAL;
  607. else
  608. *status = BROM_USBBOOT_EN;
  609. close(fd);
  610. return ret;
  611. }
  612. /**
  613. * csi_efuse_dis_usr_brom_usb_fastboot() - Disable bootrom USB fastboot status in user area
  614. *
  615. * Return: 0 on success or negative code on failure
  616. */
  617. int csi_efuse_dis_usr_brom_usb_fastboot(void)
  618. {
  619. int ret, fd;
  620. unsigned char status = 0xA; /* disable value */
  621. fd = open(efuse_file, O_RDWR);
  622. if (fd < 0) {
  623. printf("failed to open efuse device: %s\n", efuse_file);
  624. return -errno;
  625. }
  626. ret = efuse_write(fd, &status, "USR_USB_FASTBOOT_DIS");
  627. if (ret < 0)
  628. printf("failed to set 'USR_USB_FASTBOOT_DIS' status into efuse\n");
  629. else
  630. ret = 0;
  631. close(fd);
  632. return ret;
  633. }
  634. /**
  635. * csi_efuse_get_usr_brom_cct_st() - Get bootrom CCT status in user area
  636. *
  637. * @status: pointer to the buffer to store CCT
  638. *
  639. * Return: 0 on success or negative code on failure
  640. */
  641. int csi_efuse_get_usr_brom_cct_st(brom_cct_st_t *status)
  642. {
  643. int ret, fd;
  644. unsigned char tempdata;
  645. assert(status);
  646. fd = open(efuse_file, O_RDONLY);
  647. if (fd < 0) {
  648. printf("failed to open efuse device: %s\n", efuse_file);
  649. return -errno;
  650. }
  651. ret = efuse_read(fd, &tempdata, "USR_BROM_CCT_DIS");
  652. if (ret < 0)
  653. printf("failed to get 'USR_BROM_CCT_DIS' from efuse\n");
  654. else
  655. ret = 0;
  656. if (tempdata == 0xa)
  657. *status = BROM_CCT_DIS;
  658. else if(tempdata != BROM_CCT_EN)
  659. ret = -EINVAL;
  660. else
  661. *status = BROM_CCT_EN;
  662. close(fd);
  663. return ret;
  664. }
  665. /**
  666. * csi_efuse_dis_usr_brom_cct() - Disable bootrom CCT status in user area
  667. *
  668. * Return: 0 on success or negative code on failure
  669. */
  670. int csi_efuse_dis_usr_brom_cct(void)
  671. {
  672. int ret, fd;
  673. unsigned char status = 0xA; /* disable value */
  674. fd = open(efuse_file, O_RDWR);
  675. if (fd < 0) {
  676. printf("failed to open efuse device: %s\n", efuse_file);
  677. return -errno;
  678. }
  679. ret = efuse_write(fd, &status, "USR_BROM_CCT_DIS");
  680. if (ret < 0)
  681. printf("failed to set 'USR_BROM_CCT_DIS' status into efuse\n");
  682. else
  683. ret = 0;
  684. close(fd);
  685. return ret;
  686. }
  687. /**
  688. * csi_efuse_get_bl2_img_encrypt_st() - Get BL2 image encryption flag
  689. *
  690. * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
  691. *
  692. * Return: 0 on success or negative code on failure
  693. */
  694. int csi_efuse_get_bl2_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
  695. {
  696. int ret, fd;
  697. unsigned char tempdata;
  698. assert(encrypt_flag);
  699. fd = open(efuse_file, O_RDONLY);
  700. if (fd < 0) {
  701. printf("failed to open efuse device: %s\n", efuse_file);
  702. return -errno;
  703. }
  704. ret = efuse_read(fd, &tempdata, "IMAGE_BL2_ENC");
  705. if (ret < 0)
  706. printf("failed to get 'IMAGE_BL2_ENC' from efuse\n");
  707. else
  708. ret = 0;
  709. if (tempdata == 0x5a)
  710. *encrypt_flag = IMAGE_ENCRYPT_EN;
  711. else if (tempdata != IMAGE_ENCRYPT_DIS)
  712. ret = -EINVAL;
  713. else
  714. *encrypt_flag = IMAGE_ENCRYPT_DIS;
  715. close(fd);
  716. return ret;
  717. }
  718. /**
  719. * csi_efuse_set_bl2_img_encrypt_st() - Set BL2 image encryption flag
  720. *
  721. * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
  722. *
  723. * Return: 0 on success or negative code on failure
  724. */
  725. int csi_efuse_set_bl2_img_encrypt_st(img_encrypt_st_t encrypt_flag)
  726. {
  727. int ret, fd;
  728. if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
  729. return -EINVAL;
  730. fd = open(efuse_file, O_RDWR);
  731. if (fd < 0) {
  732. printf("failed to open efuse device: %s\n", efuse_file);
  733. return -errno;
  734. }
  735. if (encrypt_flag == IMAGE_ENCRYPT_EN)
  736. encrypt_flag = 0x5a;
  737. ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL2_ENC");
  738. if (ret < 0)
  739. printf("failed to get 'IMAGE_BL2_ENC' from efuse\n");
  740. else
  741. ret = 0;
  742. close(fd);
  743. return ret;
  744. }
  745. /**
  746. * csi_efuse_get_bl3_img_encrypt_st() - Get BL3 image encryption flag
  747. *
  748. * @encrypt_flag: pointer to the buffer to store BL2 encryption flag
  749. *
  750. * Return: 0 on success or negative code on failure
  751. */
  752. int csi_efuse_get_bl3_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
  753. {
  754. int ret, fd;
  755. unsigned char tempdata;
  756. assert(encrypt_flag);
  757. fd = open(efuse_file, O_RDONLY);
  758. if (fd < 0) {
  759. printf("failed to open efuse device: %s\n", efuse_file);
  760. return -errno;
  761. }
  762. ret = efuse_read(fd, &tempdata, "IMAGE_BL3_ENC");
  763. if (ret < 0)
  764. printf("failed to get 'IMAGE_BL3_ENC' from efuse\n");
  765. else
  766. ret = 0;
  767. if (tempdata == 0x5a)
  768. *encrypt_flag = IMAGE_ENCRYPT_EN;
  769. else if (tempdata != IMAGE_ENCRYPT_DIS)
  770. ret = -EINVAL;
  771. else
  772. *encrypt_flag = IMAGE_ENCRYPT_DIS;
  773. close(fd);
  774. return ret;
  775. }
  776. /**
  777. * csi_efuse_set_bl3_img_encrypt_st() - Set BL3 image encryption flag
  778. *
  779. * @encrypt_flag: pointer to the buffer to store BL3 encryption flag
  780. *
  781. * Return: 0 on success or negative code on failure
  782. */
  783. int csi_efuse_set_bl3_img_encrypt_st(img_encrypt_st_t encrypt_flag)
  784. {
  785. int ret, fd;
  786. if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
  787. return -EINVAL;
  788. fd = open(efuse_file, O_RDWR);
  789. if (fd < 0) {
  790. printf("failed to open efuse device: %s\n", efuse_file);
  791. return -errno;
  792. }
  793. if (encrypt_flag == IMAGE_ENCRYPT_EN)
  794. encrypt_flag = 0x5a;
  795. ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL3_ENC");
  796. if (ret < 0)
  797. printf("failed to get 'IMAGE_BL3_ENC' from efuse\n");
  798. else
  799. ret = 0;
  800. close(fd);
  801. return ret;
  802. }
  803. /**
  804. * csi_efuse_get_bl4_img_encrypt_st() - Get BL4 image encryption flag
  805. *
  806. * @encrypt_flag: pointer to the buffer to store BL4 encryption flag
  807. *
  808. * Return: 0 on success or negative code on failure
  809. */
  810. int csi_efuse_get_bl4_img_encrypt_st(img_encrypt_st_t *encrypt_flag)
  811. {
  812. int ret, fd;
  813. unsigned char tempdata;
  814. assert(encrypt_flag);
  815. fd = open(efuse_file, O_RDONLY);
  816. if (fd < 0) {
  817. printf("failed to open efuse device: %s\n", efuse_file);
  818. return -errno;
  819. }
  820. ret = efuse_read(fd, &tempdata, "IMAGE_BL4_ENC");
  821. if (ret < 0)
  822. printf("failed to get 'IMAGE_BL4_ENC' from efuse\n");
  823. else
  824. ret = 0;
  825. if (tempdata == 0x5a)
  826. *encrypt_flag = IMAGE_ENCRYPT_EN;
  827. else if (tempdata != IMAGE_ENCRYPT_DIS)
  828. ret = -EINVAL;
  829. else
  830. *encrypt_flag = IMAGE_ENCRYPT_DIS;
  831. close(fd);
  832. return ret;
  833. }
  834. /**
  835. * csi_efuse_set_bl4_img_encrypt_st() - Set BL4 image encryption flag
  836. *
  837. * @encrypt_flag: pointer to the buffer to store BL4 encryption flag
  838. *
  839. * Return: 0 on success or negative code on failure
  840. */
  841. int csi_efuse_set_bl4_img_encrypt_st(img_encrypt_st_t encrypt_flag)
  842. {
  843. int ret, fd;
  844. if ((encrypt_flag != IMAGE_ENCRYPT_DIS) && (encrypt_flag != IMAGE_ENCRYPT_EN))
  845. return -EINVAL;
  846. fd = open(efuse_file, O_RDWR);
  847. if (fd < 0) {
  848. printf("failed to open efuse device: %s\n", efuse_file);
  849. return -errno;
  850. }
  851. if (encrypt_flag == IMAGE_ENCRYPT_EN)
  852. encrypt_flag = 0x5a;
  853. ret = efuse_write(fd, (unsigned char *)&encrypt_flag, "IMAGE_BL4_ENC");
  854. if (ret < 0)
  855. printf("failed to get 'IMAGE_BL4_ENC' from efuse\n");
  856. else
  857. ret = 0;
  858. close(fd);
  859. return ret;
  860. }
  861. /**
  862. * csi_efuse_get_bl1_version() - Get BL1 version
  863. *
  864. * @version: pointer to the buffer to store BL1's version in eFuse
  865. *
  866. * Return: 0 on success or negative code on failure
  867. */
  868. int csi_efuse_get_bl1_version(unsigned long long *version)
  869. {
  870. int ret, fd;
  871. assert(version);
  872. fd = open(efuse_file, O_RDONLY);
  873. if (fd < 0) {
  874. printf("failed to open efuse device: %s\n", efuse_file);
  875. return -errno;
  876. }
  877. ret = efuse_read(fd, version, "BL1VERSION");
  878. if (ret < 0)
  879. printf("failed to get 'BL1VERSION' from efuse\n");
  880. else
  881. ret = 0;
  882. close(fd);
  883. return ret;
  884. }
  885. /**
  886. * csi_efuse_set_bl1_version() - Set BL1 version
  887. *
  888. * @version: pointer to the buffer to store BL1's version in eFuse
  889. *
  890. * Return: 0 on success or negative code on failure
  891. */
  892. int csi_efuse_set_bl1_version(unsigned long long version)
  893. {
  894. int ret, fd;
  895. fd = open(efuse_file, O_RDWR);
  896. if (fd < 0) {
  897. printf("failed to open efuse device: %s\n", efuse_file);
  898. return -errno;
  899. }
  900. ret = efuse_write(fd, &version, "BL1VERSION");
  901. if (ret < 0)
  902. printf("failed to set 'BL1VERSION' into efuse\n");
  903. else
  904. ret = 0;
  905. close(fd);
  906. return ret;
  907. }
  908. /**
  909. * csi_efuse_get_bl2_version() - Get BL2 version
  910. *
  911. * @version: pointer to the buffer to store BL2's version in eFuse
  912. *
  913. * Return: 0 on success or negative code on failure
  914. */
  915. int csi_efuse_get_bl2_version(unsigned long long *version)
  916. {
  917. int ret, fd;
  918. assert(version);
  919. fd = open(efuse_file, O_RDONLY);
  920. if (fd < 0) {
  921. printf("failed to open efuse device: %s\n", efuse_file);
  922. return -errno;
  923. }
  924. ret = efuse_read(fd, version, "BL2VERSION");
  925. if (ret < 0)
  926. printf("failed to get 'BL2VERSION' from efuse\n");
  927. else
  928. ret = 0;
  929. close(fd);
  930. return ret;
  931. }
  932. /**
  933. * csi_efuse_set_bl2_version() - Set BL2 version
  934. *
  935. * @version: pointer to the buffer to store BL2's version in eFuse
  936. *
  937. * Return: 0 on success or negative code on failure
  938. */
  939. int csi_efuse_set_bl2_version(unsigned long long version)
  940. {
  941. int ret, fd;
  942. fd = open(efuse_file, O_RDWR);
  943. if (fd < 0) {
  944. printf("failed to open efuse device: %s\n", efuse_file);
  945. return -errno;
  946. }
  947. ret = efuse_write(fd, &version, "BL2VERSION");
  948. if (ret < 0)
  949. printf("failed to set 'BL2VERSION' into efuse\n");
  950. else
  951. ret = 0;
  952. close(fd);
  953. return ret;
  954. }
  955. /**
  956. * csi_efuse_get_secure_boot_st() - Get seucre boot flag
  957. *
  958. * @sboot_flag: A pointer to the buffer to store secure boot flag
  959. *
  960. * Return: 0 on success or negative code on failure
  961. */
  962. int csi_efuse_get_secure_boot_st(sboot_st_t *sboot_flag)
  963. {
  964. int ret, fd;
  965. unsigned char tempdata;
  966. assert(sboot_flag);
  967. fd = open(efuse_file, O_RDONLY);
  968. if (fd < 0) {
  969. printf("failed to open efuse device: %s\n", efuse_file);
  970. return -errno;
  971. }
  972. ret = efuse_read(fd, &tempdata, "SECURE_BOOT");
  973. if (ret < 0)
  974. printf("failed to get 'SECURE_BOOT' from efuse\n");
  975. else
  976. ret = 0;
  977. if (tempdata == 0x5a)
  978. *sboot_flag = SECURE_BOOT_EN;
  979. else if (tempdata != SECURE_BOOT_DIS)
  980. ret = -EINVAL;
  981. else
  982. *sboot_flag = SECURE_BOOT_DIS;
  983. close(fd);
  984. return ret;
  985. }
  986. /**
  987. * csi_efuse_get_hash_challenge() - Get hash challenge in eFuse
  988. *
  989. * @hash_resp: pointer to the buffer to store hash response
  990. *
  991. * Return: 0 on success or negative code on failure
  992. */
  993. int csi_efuse_get_hash_challenge(void * hash_resp)
  994. {
  995. int ret, fd;
  996. assert(hash_resp);
  997. fd = open(efuse_file, O_RDONLY);
  998. if (fd < 0) {
  999. printf("failed to open efuse device: %s\n", efuse_file);
  1000. return -errno;
  1001. }
  1002. ret = efuse_read(fd, hash_resp, "HASH_DEBUGPK");
  1003. if (ret < 0)
  1004. printf("failed to get 'HASH_DEBUGPK' from efuse\n");
  1005. else
  1006. ret = 0;
  1007. close(fd);
  1008. return ret;
  1009. }
  1010. /**
  1011. * csi_efuse_get_userdata_group() - Get user data in corresponding eFuse block
  1012. *
  1013. * @key: pointer to the buffer to store user data
  1014. * @block_num: the block number in eFuse
  1015. *
  1016. * Return: 0 on success or negative code on failure
  1017. */
  1018. int csi_efuse_get_userdata_group(unsigned char *key, unsigned char block_num)
  1019. {
  1020. int ret, fd;
  1021. assert(key);
  1022. if (block_num >= 58)
  1023. return -EINVAL;
  1024. fd = open(efuse_file, O_RDONLY);
  1025. if (fd < 0) {
  1026. printf("failed to open efuse device: %s\n", efuse_file);
  1027. return -errno;
  1028. }
  1029. ret = efuse_block_read(fd, key, block_num);
  1030. if (ret < 0)
  1031. printf("failed to get block%d data from efuse\n", block_num);
  1032. else
  1033. ret = 0;
  1034. close(fd);
  1035. return ret;
  1036. }
  1037. /**
  1038. * csi_efuse_set_userdata_group() - Set user data corresponding eFuse block
  1039. *
  1040. * @key: pointer to the buffer to store user data
  1041. * @block_num: the block number in eFuse
  1042. *
  1043. * Return: 0 on success or negative code on failure
  1044. */
  1045. int csi_efuse_set_userdata_group(unsigned char *key, unsigned char block_num)
  1046. {
  1047. int ret, fd;
  1048. assert(key);
  1049. if (block_num > 58) {
  1050. printf("the block number is out of the scop \n");
  1051. return -EINVAL;
  1052. }
  1053. fd = open(efuse_file, O_RDWR);
  1054. if (fd < 0) {
  1055. printf("failed to open efuse device: %s\n", efuse_file);
  1056. return -errno;
  1057. }
  1058. ret = efuse_block_write(fd, key, block_num);
  1059. if (ret < 0)
  1060. printf("failed to set block%d data into efuse\n", block_num);
  1061. else
  1062. ret = 0;
  1063. close(fd);
  1064. return ret;
  1065. }
  1066. /**
  1067. * csi_efuse_read() - Read data from eFuse
  1068. *
  1069. * @offset: offset address
  1070. * @data: Pointer to a buffer storing the data read from eFuse
  1071. * @cnt: Number of bytes need to be read
  1072. *
  1073. * Return: number of data items read or error code
  1074. */
  1075. int csi_efuse_read(unsigned int offset, void *data, unsigned int cnt)
  1076. {
  1077. int ret, fd;
  1078. assert(data);
  1079. fd = open(efuse_file, O_RDONLY);
  1080. if (fd < 0) {
  1081. printf("failed to open efuse device: %s\n", efuse_file);
  1082. return -errno;
  1083. }
  1084. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  1085. perror("failed to lseek offset to read");
  1086. close(fd);
  1087. return -errno;
  1088. }
  1089. ret = read(fd, data, cnt);
  1090. if (ret < 0) {
  1091. perror("failed to read data from efuse");
  1092. ret = -errno;
  1093. }
  1094. close(fd);
  1095. return ret;
  1096. }
  1097. /**
  1098. * csi_efuse_write() - Write data to eFuse
  1099. *
  1100. * @offset: offset address
  1101. * @data: Pointer to a buffer storing the data write to eFuse
  1102. * @cnt: Number of bytes need to be write
  1103. *
  1104. * Return: number of data items write or error code
  1105. */
  1106. int csi_efuse_write(unsigned int offset, void *data, unsigned int cnt)
  1107. {
  1108. int ret, fd;
  1109. assert(data);
  1110. fd = open(efuse_file, O_RDWR);
  1111. if (fd < 0) {
  1112. printf("failed to open efuse device: %s\n", efuse_file);
  1113. return -errno;
  1114. }
  1115. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  1116. perror("failed to lseek offset to write");
  1117. close(fd);
  1118. return -errno;
  1119. }
  1120. ret = write(fd, data, cnt);
  1121. if (ret < 0) {
  1122. perror("failed to write data to efuse");
  1123. ret = -errno;
  1124. }
  1125. close(fd);
  1126. return ret;
  1127. }
  1128. /**
  1129. * csi_dbg_enable_c910t_jtag() - Enable C910 TEE core jtag
  1130. *
  1131. * Return: 0 on success or negative code on failure
  1132. */
  1133. int csi_dbg_enable_c910t_jtag(void)
  1134. {
  1135. }
  1136. /**
  1137. * csi_dbg_disable_c910t_jtag() - Disable C910 TEE core jtag
  1138. *
  1139. * Return: 0 on success or negative code on failure
  1140. */
  1141. int csi_dbg_disable_c910t_jtag(void)
  1142. {
  1143. }
  1144. /**
  1145. * csi_efuse_get_gmac_macaddr() - Get gmac0/gmac1 mac address in eFuse
  1146. * @dev_id: '0' means gmac0, '1' means gmac1
  1147. * @mac: the mac address string
  1148. *
  1149. * Return: 0: Success others: Failed
  1150. */
  1151. int csi_efuse_get_gmac_macaddr(int dev_id, unsigned char *mac)
  1152. {
  1153. int ret, fd;
  1154. char gmac_name[12] = {};
  1155. assert(mac);
  1156. if ((dev_id != 0) && (dev_id != 1))
  1157. return -EINVAL;
  1158. fd = open(efuse_file, O_RDONLY);
  1159. if (fd < 0) {
  1160. printf("failed to open efuse device: %s\n", efuse_file);
  1161. return -errno;
  1162. }
  1163. sprintf(gmac_name, "GMAC%d_MAC", dev_id);
  1164. ret = efuse_read(fd, mac, gmac_name);
  1165. if (ret < 0)
  1166. printf("failed to get %s\n", gmac_name);
  1167. else
  1168. ret = 0;
  1169. close(fd);
  1170. return ret;
  1171. }
  1172. /**
  1173. * csi_efuse_set_gmac_macaddr() - Set gmac0/gmac1 mac address in eFuse
  1174. * @dev_id: '0' means gmac0, '1' means gmac1
  1175. * @mac: the mac address string
  1176. *
  1177. * Return: 0: Success others: Failed
  1178. */
  1179. int csi_efuse_set_gmac_macaddr(int dev_id, unsigned char *mac)
  1180. {
  1181. int ret, fd;
  1182. char gmac_name[12] = {};
  1183. assert(mac);
  1184. if ((dev_id != 0) && (dev_id != 1))
  1185. return -EINVAL;
  1186. fd = open(efuse_file, O_RDWR);
  1187. if (fd < 0) {
  1188. printf("failed to open efuse device: %s\n", efuse_file);
  1189. return -errno;
  1190. }
  1191. sprintf(gmac_name, "GMAC%d_MAC", dev_id);
  1192. ret = efuse_write(fd, mac, gmac_name);
  1193. if (ret < 0)
  1194. printf("failed to set '%s' into efuse\n", gmac_name);
  1195. else
  1196. ret = 0;
  1197. close(fd);
  1198. return ret;
  1199. }
  1200. /**
  1201. * csi_efuse_update_lc_rma() - Upate efuse life cycle RMA
  1202. *
  1203. * Return: 0: Success others: Failed
  1204. */
  1205. int csi_efuse_update_lc_rma()
  1206. {
  1207. int fd, ret;
  1208. char d = '1';
  1209. const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/rma_lc";
  1210. fd = open(dev_path, O_WRONLY);
  1211. if (fd < 0) {
  1212. printf("failed to open device '%s'\n", dev_path);
  1213. return -errno;
  1214. }
  1215. ret = write(fd, &d, 1);
  1216. if (ret < 0){
  1217. printf("failed to update rma lifecycle\n");
  1218. return -errno;
  1219. } else {
  1220. ret = 0;
  1221. }
  1222. close(fd);
  1223. return 0;
  1224. }
  1225. /**
  1226. * csi_efuse_update_lc_rma() - Upate efuse life cycle RIP
  1227. *
  1228. * Return: 0: Success others: Failed
  1229. */
  1230. int csi_efuse_update_lc_rip()
  1231. {
  1232. int fd, ret;
  1233. char d = '1';
  1234. const char *dev_path = "/sys/devices/platform/soc/ffff210000.efuse/rip_lc";
  1235. fd = open(dev_path, O_WRONLY);
  1236. if (fd < 0) {
  1237. printf("failed to open device '%s'\n", dev_path);
  1238. return -errno;
  1239. }
  1240. ret = write(fd, &d, 1);
  1241. if (ret < 0){
  1242. printf("failed to update rip lifecycle\n");
  1243. return -errno;
  1244. } else {
  1245. ret = 0;
  1246. }
  1247. close(fd);
  1248. return 0;
  1249. }