wm_efuse.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**************************************************************************
  2. * File Name : tls_efuse.c
  3. * Author :
  4. * Version :
  5. * Date :
  6. * Description : Use Flash Addr as virtual efuse
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. * All rights reserved.
  10. *
  11. ***************************************************************************/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "wm_debug.h"
  16. #include "wm_regs.h"
  17. #include "wm_efuse.h"
  18. #include "wm_config.h"
  19. #include "list.h"
  20. #include "wm_internal_flash.h"
  21. #include "wm_crypto_hard.h"
  22. #include "wm_mem.h"
  23. #define USE_OTA_FT_PARAM 0
  24. #include "wm_flash_map.h"
  25. extern void flashSRRW(unsigned long offset,unsigned char *buf,unsigned long sz, unsigned char *backbuf, unsigned int backlen, unsigned int rd);
  26. #define FT_MAGICNUM_ADDR (FLASH_BASE_ADDR)
  27. #define MAGICNUM_LEN (4)
  28. #define MAC_ADDR_LEN (8)
  29. #define FT_GAIN_LEN (84)
  30. typedef struct FT_PARAM
  31. {
  32. unsigned int magic_no;
  33. unsigned int checksum;
  34. unsigned char wifi_mac_addr[MAC_ADDR_LEN];
  35. unsigned char bt_mac_addr[MAC_ADDR_LEN];
  36. unsigned int tx_dcoffset;
  37. unsigned int rx_dcoffset;
  38. unsigned int tx_iq_gain;
  39. unsigned int rx_iq_gain;
  40. unsigned int tx_iq_phase;
  41. unsigned int rx_iq_phase;
  42. unsigned char tx_gain[FT_GAIN_LEN];
  43. }FT_PARAM_ST;
  44. static u8 default_mac[6] = {0x00,0x25,0x08,0x09,0x01,0x0F};
  45. FT_PARAM_ST gftParam;
  46. int tls_ft_param_init(void)
  47. {
  48. u32 crcvalue = 0;
  49. psCrcContext_t ctx;
  50. FT_PARAM_ST *pft = NULL;
  51. if (gftParam.magic_no == SIGNATURE_WORD)
  52. {
  53. return TRUE;
  54. }
  55. pft = tls_mem_alloc(sizeof(FT_PARAM_ST));
  56. if (pft == NULL)
  57. {
  58. return FALSE;
  59. }
  60. memset(pft, 0xFF, sizeof(FT_PARAM_ST));
  61. memset(&gftParam, 0xFF, sizeof(FT_PARAM_ST));
  62. tls_fls_read(FT_MAGICNUM_ADDR, (unsigned char *)pft, sizeof(FT_PARAM_ST));
  63. if (pft->magic_no == SIGNATURE_WORD)
  64. {
  65. tls_crypto_init();
  66. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  67. tls_crypto_crc_update(&ctx, (unsigned char *)pft + 8, sizeof(FT_PARAM_ST) - 8);
  68. tls_crypto_crc_final(&ctx, &crcvalue);
  69. if (pft->checksum != crcvalue)
  70. {
  71. tls_mem_free(pft);
  72. return FALSE;
  73. }
  74. if (gftParam.magic_no != SIGNATURE_WORD)
  75. {
  76. memcpy(&gftParam, pft, sizeof(FT_PARAM_ST));
  77. }
  78. }
  79. tls_mem_free(pft);
  80. /*lock parameter*/
  81. tls_flash_unlock();
  82. return TRUE;
  83. }
  84. int tls_ft_param_get(unsigned int opnum, void *data, unsigned int rdlen)
  85. {
  86. switch (opnum)
  87. {
  88. case CMD_WIFI_MAC: /*MAC*/
  89. if ((gftParam.wifi_mac_addr[0]&0x1)
  90. ||(0 == (gftParam.wifi_mac_addr[0]|gftParam.wifi_mac_addr[1]|gftParam.wifi_mac_addr[2]|gftParam.wifi_mac_addr[3]|gftParam.wifi_mac_addr[4]|gftParam.wifi_mac_addr[5])))
  91. {
  92. memcpy(data, default_mac, rdlen);
  93. }
  94. else
  95. {
  96. memcpy(data, gftParam.wifi_mac_addr, rdlen);
  97. }
  98. break;
  99. case CMD_BT_MAC: /*MAC*/
  100. {
  101. u8 invalid_bt_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  102. u8 invalid_bt_mac1[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  103. if ((memcmp(gftParam.bt_mac_addr, invalid_bt_mac, 6) == 0)||(memcmp(gftParam.bt_mac_addr, invalid_bt_mac1, 6) == 0))
  104. {
  105. memcpy(data, default_mac, rdlen);
  106. *((u8*)data+5) +=1; /*defalut plus 1*/
  107. *((u8*)data) |= 0xC0; /*defalut public static type*/
  108. }
  109. else
  110. {
  111. memcpy(data, gftParam.bt_mac_addr, rdlen);
  112. }
  113. }
  114. break;
  115. case CMD_TX_DC: /*tx_dcoffset*/
  116. *(unsigned int *)data = gftParam.tx_dcoffset;
  117. break;
  118. case CMD_RX_DC: /*rx_dcoffset*/
  119. *(unsigned int *)data = gftParam.rx_dcoffset;
  120. break;
  121. case CMD_TX_IQ_GAIN:
  122. *(unsigned int *)data = gftParam.tx_iq_gain;
  123. break;
  124. case CMD_RX_IQ_GAIN:
  125. *(unsigned int *)data = gftParam.rx_iq_gain;
  126. break;
  127. case CMD_TX_IQ_PHASE:
  128. *(unsigned int *)data = gftParam.tx_iq_phase;
  129. break;
  130. case CMD_RX_IQ_PHASE:
  131. *(unsigned int *)data = gftParam.rx_iq_phase;
  132. break;
  133. case CMD_TX_GAIN: /*gain*/
  134. if (rdlen < FT_GAIN_LEN)
  135. {
  136. memcpy(data, gftParam.tx_gain, rdlen);
  137. }
  138. else
  139. {
  140. memcpy(data, gftParam.tx_gain, FT_GAIN_LEN);
  141. }
  142. break;
  143. default:
  144. return -1;
  145. }
  146. //printf("tls_ft_param_get: opnum=%d, val=%x\n", opnum, *(unsigned int *)data);
  147. return 0;
  148. }
  149. int tls_ft_param_set(unsigned int opnum, void *data, unsigned int len)
  150. {
  151. psCrcContext_t ctx;
  152. unsigned int writelen = 0;
  153. if (!data || !len)
  154. {
  155. return -1;
  156. }
  157. //printf("tls_ft_param_set: opnum=%d, val=%x\n", opnum, *(unsigned int *)data);
  158. //tls_fls_read(FT_MAGICNUM_ADDR, (unsigned char *)&gftParam, sizeof(gftParam));
  159. switch (opnum)
  160. {
  161. case CMD_WIFI_MAC: /*MAC*/
  162. memcpy(gftParam.wifi_mac_addr, (unsigned char *)data, len);
  163. break;
  164. case CMD_BT_MAC: /*BT MAC*/
  165. memcpy(gftParam.bt_mac_addr, (unsigned char *)data, len);
  166. break;
  167. case CMD_TX_DC: /*tx_dcoffset*/
  168. gftParam.tx_dcoffset = *(unsigned int *)data;
  169. break;
  170. case CMD_RX_DC: /*rx_dcoffset*/
  171. gftParam.rx_dcoffset = *(unsigned int *)data;
  172. break;
  173. case CMD_TX_IQ_GAIN:
  174. gftParam.tx_iq_gain = *(unsigned int *)data;
  175. break;
  176. case CMD_RX_IQ_GAIN:
  177. gftParam.rx_iq_gain = *(unsigned int *) data;
  178. break;
  179. case CMD_TX_IQ_PHASE:
  180. gftParam.tx_iq_phase = *(unsigned int *)data;
  181. break;
  182. case CMD_RX_IQ_PHASE:
  183. gftParam.rx_iq_phase = *(unsigned int *) data;
  184. break;
  185. case CMD_TX_GAIN: /*gain*/
  186. if (len >= FT_GAIN_LEN)
  187. {
  188. writelen = FT_GAIN_LEN;
  189. }
  190. else
  191. {
  192. writelen = len;
  193. }
  194. memcpy(gftParam.tx_gain, data, writelen);
  195. break;
  196. default:
  197. return -1;
  198. }
  199. tls_crypto_init();
  200. tls_crypto_crc_init(&ctx, 0xFFFFFFFF, CRYPTO_CRC_TYPE_32, INPUT_REFLECT | OUTPUT_REFLECT);
  201. gftParam.magic_no = SIGNATURE_WORD;
  202. tls_crypto_crc_update(&ctx, (unsigned char *)&gftParam + 8, sizeof(gftParam) -8);
  203. tls_crypto_crc_final(&ctx, &gftParam.checksum);
  204. tls_flash_unlock();
  205. tls_fls_write(FT_MAGICNUM_ADDR, (unsigned char *)&gftParam, sizeof(gftParam));
  206. tls_flash_lock();
  207. return 0;
  208. }
  209. /**********************************************************************************************************
  210. * Description: This function is used to get mac addr.
  211. *
  212. * Arguments : mac mac addr,6 byte
  213. *
  214. * Returns : TLS_EFUSE_STATUS_OK get success
  215. * TLS_EFUSE_STATUS_EIO get failed
  216. **********************************************************************************************************/
  217. int tls_get_mac_addr(u8 *mac)
  218. {
  219. return tls_ft_param_get(CMD_WIFI_MAC, mac, 6);
  220. }
  221. /**********************************************************************************************************
  222. * Description: This function is used to set mac addr.
  223. *
  224. * Arguments : mac mac addr,6 byte
  225. *
  226. * Returns : TLS_EFUSE_STATUS_OK get success
  227. * TLS_EFUSE_STATUS_EIO get failed
  228. **********************************************************************************************************/
  229. int tls_set_mac_addr(u8 *mac)
  230. {
  231. return tls_ft_param_set(CMD_WIFI_MAC, mac, 6);
  232. }
  233. /**********************************************************************************************************
  234. * Description: This function is used to get bluetooth mac addr.
  235. *
  236. * Arguments : mac mac addr,6 byte
  237. *
  238. * Returns : TLS_EFUSE_STATUS_OK get success
  239. * TLS_EFUSE_STATUS_EIO get failed
  240. **********************************************************************************************************/
  241. int tls_get_bt_mac_addr(u8 *mac)
  242. {
  243. return tls_ft_param_get(CMD_BT_MAC, mac, 6);
  244. }
  245. /**********************************************************************************************************
  246. * Description: This function is used to set bluetooth mac addr.
  247. *
  248. * Arguments : mac mac addr,6 byte
  249. *
  250. * Returns : TLS_EFUSE_STATUS_OK get success
  251. * TLS_EFUSE_STATUS_EIO get failed
  252. **********************************************************************************************************/
  253. int tls_set_bt_mac_addr(u8 *mac)
  254. {
  255. return tls_ft_param_set(CMD_BT_MAC, mac, 6);
  256. }
  257. /**********************************************************************************************************
  258. * Description: This function is used to get tx lod.
  259. *
  260. * Arguments : *txlo
  261. *
  262. * Returns : 0 get success
  263. * -1 get failed
  264. **********************************************************************************************************/
  265. int tls_get_tx_lo(u8 *txlo)
  266. {
  267. return tls_ft_param_get(CMD_TX_DC, txlo, 4);
  268. }
  269. /**********************************************************************************************************
  270. * Description: This function is used to set tx lo.
  271. *
  272. * Arguments : txlo
  273. *
  274. * Returns : 0 set success
  275. * -1 set failed
  276. **********************************************************************************************************/
  277. int tls_set_tx_lo(u8 *txlo)
  278. {
  279. return tls_ft_param_set(CMD_TX_DC, txlo, 4);
  280. }
  281. /**********************************************************************************************************
  282. * Description: This function is used to get tx iq gain.
  283. *
  284. * Arguments : txGain
  285. *
  286. * Returns : 0 set success
  287. * -1 set failed
  288. **********************************************************************************************************/
  289. int tls_get_tx_iq_gain(u8 *txGain)
  290. {
  291. return tls_ft_param_get(CMD_TX_IQ_GAIN, txGain, 4);
  292. }
  293. /**********************************************************************************************************
  294. * Description: This function is used to set tx iq gain.
  295. *
  296. * Arguments : txGain
  297. *
  298. * Returns : 0 set success
  299. * -1 set failed
  300. **********************************************************************************************************/
  301. int tls_set_tx_iq_gain(u8 *txGain)
  302. {
  303. return tls_ft_param_set(CMD_TX_IQ_GAIN, txGain, 4);
  304. }
  305. /**********************************************************************************************************
  306. * Description: This function is used to get rx iq gain.
  307. *
  308. * Arguments : rxGain
  309. *
  310. * Returns : 0 set success
  311. * -1 set failed
  312. **********************************************************************************************************/
  313. int tls_get_rx_iq_gain(u8 *rxGain)
  314. {
  315. return tls_ft_param_get(CMD_RX_IQ_GAIN, rxGain, 4);
  316. }
  317. /**********************************************************************************************************
  318. * Description: This function is used to set rx iq gain.
  319. *
  320. * Arguments : rxGain
  321. *
  322. * Returns : 0 set success
  323. * -1 set failed
  324. **********************************************************************************************************/
  325. int tls_set_rx_iq_gain(u8 *rxGain)
  326. {
  327. return tls_ft_param_set(CMD_RX_IQ_GAIN, rxGain, 4);
  328. }
  329. /**********************************************************************************************************
  330. * Description: This function is used to get tx iq phase.
  331. *
  332. * Arguments : txPhase
  333. *
  334. * Returns : 0 set success
  335. * -1 set failed
  336. **********************************************************************************************************/
  337. int tls_get_tx_iq_phase(u8 *txPhase)
  338. {
  339. return tls_ft_param_get(CMD_TX_IQ_PHASE, txPhase, 4);
  340. }
  341. /**********************************************************************************************************
  342. * Description: This function is used to set tx iq phase.
  343. *
  344. * Arguments : txPhase
  345. *
  346. * Returns : 0 set success
  347. * -1 set failed
  348. **********************************************************************************************************/
  349. int tls_set_tx_iq_phase(u8 *txPhase)
  350. {
  351. return tls_ft_param_set(CMD_TX_IQ_PHASE, txPhase, 4);
  352. }
  353. /**********************************************************************************************************
  354. * Description: This function is used to get rx iq phase.
  355. *
  356. * Arguments : rxPhase
  357. *
  358. * Returns : 0 set success
  359. * -1 set failed
  360. **********************************************************************************************************/
  361. int tls_get_rx_iq_phase(u8 *rxPhase)
  362. {
  363. return tls_ft_param_get(CMD_RX_IQ_PHASE, rxPhase, 4);
  364. }
  365. /**********************************************************************************************************
  366. * Description: This function is used to set tx iq phase.
  367. *
  368. * Arguments : rxPhase
  369. *
  370. * Returns : 0 set success
  371. * -1 set failed
  372. **********************************************************************************************************/
  373. int tls_set_rx_iq_phase(u8 *rxPhase)
  374. {
  375. return tls_ft_param_set(CMD_RX_IQ_PHASE, rxPhase, 4);
  376. }
  377. int tls_freq_err_op(u8 *freqerr, u8 flag)
  378. {
  379. tls_flash_unlock();
  380. if (flag){
  381. tls_fls_write(FT_MAGICNUM_ADDR + sizeof(FT_PARAM_ST), freqerr, 4);
  382. }
  383. else
  384. {
  385. tls_fls_read(FT_MAGICNUM_ADDR + sizeof(FT_PARAM_ST), freqerr, 4);
  386. }
  387. tls_flash_lock();
  388. return TLS_EFUSE_STATUS_OK;
  389. }
  390. int tls_rf_vcg_ctrl_op(u8 *vcg, u8 flag)
  391. {
  392. tls_flash_unlock();
  393. if (flag){
  394. tls_fls_write(VCG_ADDR, vcg, VCG_LEN);
  395. }
  396. else
  397. {
  398. tls_fls_read(VCG_ADDR, vcg, VCG_LEN);
  399. }
  400. tls_flash_lock();
  401. return TLS_EFUSE_STATUS_OK;
  402. }
  403. /**********************************************************************************************************
  404. * Description: This function is used to get tx gain.
  405. *
  406. * Arguments : txgain tx gain
  407. *
  408. * Returns : 0 get success
  409. * -1 get failed
  410. **********************************************************************************************************/
  411. int tls_get_tx_gain(u8 *txgain)
  412. {
  413. return tls_ft_param_get(CMD_TX_GAIN, txgain, TX_GAIN_LEN);
  414. }
  415. /**********************************************************************************************************
  416. * Description: This function is used to set tx gain.
  417. *
  418. * Arguments : txgain tx gain
  419. *
  420. * Returns : 0 set success
  421. * -1 set failed
  422. **********************************************************************************************************/
  423. int tls_set_tx_gain(u8 *txgain)
  424. {
  425. return tls_ft_param_set(CMD_TX_GAIN, txgain, TX_GAIN_LEN);
  426. }