phy_init.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10. #include <sys/lock.h>
  11. #include "soc/rtc.h"
  12. #include "esp_err.h"
  13. #include "esp_phy_init.h"
  14. #include "esp_mac.h"
  15. #include "esp_log.h"
  16. #include "nvs.h"
  17. #include "nvs_flash.h"
  18. #include "esp_efuse.h"
  19. #include "esp_timer.h"
  20. #include "sdkconfig.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/portmacro.h"
  23. #include "endian.h"
  24. #include "phy.h"
  25. #include "phy_init_data.h"
  26. #include "esp_coexist_internal.h"
  27. #include "esp_private/periph_ctrl.h"
  28. #include "esp_private/wifi.h"
  29. #include "esp_rom_crc.h"
  30. #include "esp_rom_sys.h"
  31. #include "soc/rtc_cntl_reg.h"
  32. #include "soc/syscon_reg.h"
  33. #if CONFIG_IDF_TARGET_ESP32
  34. #include "soc/dport_reg.h"
  35. #endif
  36. #include "hal/efuse_hal.h"
  37. #if CONFIG_IDF_TARGET_ESP32
  38. extern wifi_mac_time_update_cb_t s_wifi_mac_time_update_cb;
  39. #endif
  40. static const char* TAG = "phy_init";
  41. static _lock_t s_phy_access_lock;
  42. #if !CONFIG_IDF_TARGET_ESP32C2 // TODO - WIFI-4424
  43. static DRAM_ATTR struct {
  44. int count; /* power on count of wifi and bt power domain */
  45. _lock_t lock;
  46. } s_wifi_bt_pd_controller = { .count = 0 };
  47. #endif
  48. /* Indicate PHY is calibrated or not */
  49. static bool s_is_phy_calibrated = false;
  50. static bool s_is_phy_reg_stored = false;
  51. /* Reference count of enabling PHY */
  52. static uint8_t s_phy_access_ref = 0;
  53. #if CONFIG_MAC_BB_PD
  54. /* Reference of powering down MAC and BB */
  55. static bool s_mac_bb_pu = true;
  56. #endif
  57. #if CONFIG_IDF_TARGET_ESP32
  58. /* time stamp updated when the PHY/RF is turned on */
  59. static int64_t s_phy_rf_en_ts = 0;
  60. #endif
  61. /* PHY spinlock for libphy.a */
  62. static DRAM_ATTR portMUX_TYPE s_phy_int_mux = portMUX_INITIALIZER_UNLOCKED;
  63. /* Memory to store PHY digital registers */
  64. static uint32_t* s_phy_digital_regs_mem = NULL;
  65. static uint8_t s_phy_modem_init_ref = 0;
  66. #if CONFIG_MAC_BB_PD
  67. uint32_t* s_mac_bb_pd_mem = NULL;
  68. /* Reference count of MAC BB backup memory */
  69. static uint8_t s_macbb_backup_mem_ref = 0;
  70. #endif
  71. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
  72. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  73. extern uint8_t multi_phy_init_data_bin_start[] asm("_binary_phy_multiple_init_data_bin_start");
  74. extern uint8_t multi_phy_init_data_bin_end[] asm("_binary_phy_multiple_init_data_bin_end");
  75. #endif
  76. /* The following static variables are only used by Wi-Fi tasks, so they can be handled without lock */
  77. static phy_init_data_type_t s_phy_init_data_type = 0;
  78. static phy_init_data_type_t s_current_apply_phy_init_data = 0;
  79. static char s_phy_current_country[PHY_COUNTRY_CODE_LEN] = {0};
  80. /* Whether it is a new bin */
  81. static bool s_multiple_phy_init_data_bin = false;
  82. /* PHY init data type array */
  83. static char* s_phy_type[ESP_PHY_INIT_DATA_TYPE_NUMBER] = {"DEFAULT", "SRRC", "FCC", "CE", "NCC", "KCC", "MIC", "IC",
  84. "ACMA", "ANATEL", "ISED", "WPC", "OFCA", "IFETEL", "RCM"};
  85. /* Country and PHY init data type map */
  86. static phy_country_to_bin_type_t s_country_code_map_type_table[] = {
  87. {"AT", ESP_PHY_INIT_DATA_TYPE_CE},
  88. {"AU", ESP_PHY_INIT_DATA_TYPE_ACMA},
  89. {"BE", ESP_PHY_INIT_DATA_TYPE_CE},
  90. {"BG", ESP_PHY_INIT_DATA_TYPE_CE},
  91. {"BR", ESP_PHY_INIT_DATA_TYPE_ANATEL},
  92. {"CA", ESP_PHY_INIT_DATA_TYPE_ISED},
  93. {"CH", ESP_PHY_INIT_DATA_TYPE_CE},
  94. {"CN", ESP_PHY_INIT_DATA_TYPE_SRRC},
  95. {"CY", ESP_PHY_INIT_DATA_TYPE_CE},
  96. {"CZ", ESP_PHY_INIT_DATA_TYPE_CE},
  97. {"DE", ESP_PHY_INIT_DATA_TYPE_CE},
  98. {"DK", ESP_PHY_INIT_DATA_TYPE_CE},
  99. {"EE", ESP_PHY_INIT_DATA_TYPE_CE},
  100. {"ES", ESP_PHY_INIT_DATA_TYPE_CE},
  101. {"FI", ESP_PHY_INIT_DATA_TYPE_CE},
  102. {"FR", ESP_PHY_INIT_DATA_TYPE_CE},
  103. {"GB", ESP_PHY_INIT_DATA_TYPE_CE},
  104. {"GR", ESP_PHY_INIT_DATA_TYPE_CE},
  105. {"HK", ESP_PHY_INIT_DATA_TYPE_OFCA},
  106. {"HR", ESP_PHY_INIT_DATA_TYPE_CE},
  107. {"HU", ESP_PHY_INIT_DATA_TYPE_CE},
  108. {"IE", ESP_PHY_INIT_DATA_TYPE_CE},
  109. {"IN", ESP_PHY_INIT_DATA_TYPE_WPC},
  110. {"IS", ESP_PHY_INIT_DATA_TYPE_CE},
  111. {"IT", ESP_PHY_INIT_DATA_TYPE_CE},
  112. {"JP", ESP_PHY_INIT_DATA_TYPE_MIC},
  113. {"KR", ESP_PHY_INIT_DATA_TYPE_KCC},
  114. {"LI", ESP_PHY_INIT_DATA_TYPE_CE},
  115. {"LT", ESP_PHY_INIT_DATA_TYPE_CE},
  116. {"LU", ESP_PHY_INIT_DATA_TYPE_CE},
  117. {"LV", ESP_PHY_INIT_DATA_TYPE_CE},
  118. {"MT", ESP_PHY_INIT_DATA_TYPE_CE},
  119. {"MX", ESP_PHY_INIT_DATA_TYPE_IFETEL},
  120. {"NL", ESP_PHY_INIT_DATA_TYPE_CE},
  121. {"NO", ESP_PHY_INIT_DATA_TYPE_CE},
  122. {"NZ", ESP_PHY_INIT_DATA_TYPE_RCM},
  123. {"PL", ESP_PHY_INIT_DATA_TYPE_CE},
  124. {"PT", ESP_PHY_INIT_DATA_TYPE_CE},
  125. {"RO", ESP_PHY_INIT_DATA_TYPE_CE},
  126. {"SE", ESP_PHY_INIT_DATA_TYPE_CE},
  127. {"SI", ESP_PHY_INIT_DATA_TYPE_CE},
  128. {"SK", ESP_PHY_INIT_DATA_TYPE_CE},
  129. {"TW", ESP_PHY_INIT_DATA_TYPE_NCC},
  130. {"US", ESP_PHY_INIT_DATA_TYPE_FCC},
  131. };
  132. #endif
  133. uint32_t IRAM_ATTR phy_enter_critical(void)
  134. {
  135. if (xPortInIsrContext()) {
  136. portENTER_CRITICAL_ISR(&s_phy_int_mux);
  137. } else {
  138. portENTER_CRITICAL(&s_phy_int_mux);
  139. }
  140. // Interrupt level will be stored in current tcb, so always return zero.
  141. return 0;
  142. }
  143. void IRAM_ATTR phy_exit_critical(uint32_t level)
  144. {
  145. // Param level don't need any more, ignore it.
  146. if (xPortInIsrContext()) {
  147. portEXIT_CRITICAL_ISR(&s_phy_int_mux);
  148. } else {
  149. portEXIT_CRITICAL(&s_phy_int_mux);
  150. }
  151. }
  152. #if CONFIG_IDF_TARGET_ESP32
  153. int64_t esp_phy_rf_get_on_ts(void)
  154. {
  155. return s_phy_rf_en_ts;
  156. }
  157. static inline void phy_update_wifi_mac_time(bool en_clock_stopped, int64_t now)
  158. {
  159. static uint32_t s_common_clock_disable_time = 0;
  160. if (en_clock_stopped) {
  161. s_common_clock_disable_time = (uint32_t)now;
  162. } else {
  163. if (s_common_clock_disable_time) {
  164. uint32_t diff = (uint64_t)now - s_common_clock_disable_time;
  165. if (s_wifi_mac_time_update_cb) {
  166. s_wifi_mac_time_update_cb(diff);
  167. }
  168. s_common_clock_disable_time = 0;
  169. }
  170. }
  171. }
  172. #endif
  173. IRAM_ATTR void esp_phy_common_clock_enable(void)
  174. {
  175. wifi_bt_common_module_enable();
  176. }
  177. IRAM_ATTR void esp_phy_common_clock_disable(void)
  178. {
  179. wifi_bt_common_module_disable();
  180. }
  181. static inline void phy_digital_regs_store(void)
  182. {
  183. if (s_phy_digital_regs_mem != NULL) {
  184. phy_dig_reg_backup(true, s_phy_digital_regs_mem);
  185. s_is_phy_reg_stored = true;
  186. }
  187. }
  188. static inline void phy_digital_regs_load(void)
  189. {
  190. if (s_is_phy_reg_stored && s_phy_digital_regs_mem != NULL) {
  191. phy_dig_reg_backup(false, s_phy_digital_regs_mem);
  192. }
  193. }
  194. void esp_phy_enable(void)
  195. {
  196. _lock_acquire(&s_phy_access_lock);
  197. if (s_phy_access_ref == 0) {
  198. #if CONFIG_IDF_TARGET_ESP32
  199. // Update time stamp
  200. s_phy_rf_en_ts = esp_timer_get_time();
  201. // Update WiFi MAC time before WiFi/BT common clock is enabled
  202. phy_update_wifi_mac_time(false, s_phy_rf_en_ts);
  203. #endif
  204. esp_phy_common_clock_enable();
  205. if (s_is_phy_calibrated == false) {
  206. esp_phy_load_cal_and_init();
  207. s_is_phy_calibrated = true;
  208. }
  209. else {
  210. phy_wakeup_init();
  211. phy_digital_regs_load();
  212. }
  213. #if CONFIG_IDF_TARGET_ESP32
  214. coex_bt_high_prio();
  215. #endif
  216. }
  217. s_phy_access_ref++;
  218. _lock_release(&s_phy_access_lock);
  219. }
  220. void esp_phy_disable(void)
  221. {
  222. _lock_acquire(&s_phy_access_lock);
  223. s_phy_access_ref--;
  224. if (s_phy_access_ref == 0) {
  225. phy_digital_regs_store();
  226. // Disable PHY and RF.
  227. phy_close_rf();
  228. #if !CONFIG_IDF_TARGET_ESP32
  229. // Disable PHY temperature sensor
  230. phy_xpd_tsens();
  231. #endif
  232. #if CONFIG_IDF_TARGET_ESP32
  233. // Update WiFi MAC time before disalbe WiFi/BT common peripheral clock
  234. phy_update_wifi_mac_time(true, esp_timer_get_time());
  235. #endif
  236. // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG
  237. esp_phy_common_clock_disable();
  238. }
  239. _lock_release(&s_phy_access_lock);
  240. }
  241. void IRAM_ATTR esp_wifi_bt_power_domain_on(void)
  242. {
  243. #if !CONFIG_IDF_TARGET_ESP32C2 // TODO - WIFI-4424
  244. _lock_acquire(&s_wifi_bt_pd_controller.lock);
  245. if (s_wifi_bt_pd_controller.count++ == 0) {
  246. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PD);
  247. #if !CONFIG_IDF_TARGET_ESP32
  248. SET_PERI_REG_MASK(SYSCON_WIFI_RST_EN_REG, MODEM_RESET_FIELD_WHEN_PU);
  249. CLEAR_PERI_REG_MASK(SYSCON_WIFI_RST_EN_REG, MODEM_RESET_FIELD_WHEN_PU);
  250. #endif
  251. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_WIFI_FORCE_ISO);
  252. }
  253. _lock_release(&s_wifi_bt_pd_controller.lock);
  254. #endif
  255. }
  256. void esp_wifi_bt_power_domain_off(void)
  257. {
  258. #if !CONFIG_IDF_TARGET_ESP32C2 // TODO - WIFI-4424
  259. _lock_acquire(&s_wifi_bt_pd_controller.lock);
  260. if (--s_wifi_bt_pd_controller.count == 0) {
  261. SET_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_WIFI_FORCE_ISO);
  262. SET_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PD);
  263. }
  264. _lock_release(&s_wifi_bt_pd_controller.lock);
  265. #endif
  266. }
  267. void esp_phy_modem_init(void)
  268. {
  269. _lock_acquire(&s_phy_access_lock);
  270. s_phy_modem_init_ref++;
  271. if (s_phy_digital_regs_mem == NULL) {
  272. s_phy_digital_regs_mem = (uint32_t *)heap_caps_malloc(SOC_PHY_DIG_REGS_MEM_SIZE, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  273. }
  274. _lock_release(&s_phy_access_lock);
  275. }
  276. void esp_phy_modem_deinit(void)
  277. {
  278. _lock_acquire(&s_phy_access_lock);
  279. s_phy_modem_init_ref--;
  280. if (s_phy_modem_init_ref == 0) {
  281. s_is_phy_reg_stored = false;
  282. free(s_phy_digital_regs_mem);
  283. s_phy_digital_regs_mem = NULL;
  284. /* Fix the issue caused by the power domain off.
  285. * This issue is only on ESP32C3.
  286. */
  287. #if CONFIG_IDF_TARGET_ESP32C3
  288. phy_init_flag();
  289. #endif
  290. }
  291. _lock_release(&s_phy_access_lock);
  292. }
  293. #if CONFIG_MAC_BB_PD
  294. void esp_mac_bb_pd_mem_init(void)
  295. {
  296. _lock_acquire(&s_phy_access_lock);
  297. s_macbb_backup_mem_ref++;
  298. if (s_mac_bb_pd_mem == NULL) {
  299. s_mac_bb_pd_mem = (uint32_t *)heap_caps_malloc(SOC_MAC_BB_PD_MEM_SIZE, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  300. }
  301. _lock_release(&s_phy_access_lock);
  302. }
  303. void esp_mac_bb_pd_mem_deinit(void)
  304. {
  305. _lock_acquire(&s_phy_access_lock);
  306. s_macbb_backup_mem_ref--;
  307. if (s_macbb_backup_mem_ref == 0) {
  308. free(s_mac_bb_pd_mem);
  309. s_mac_bb_pd_mem = NULL;
  310. }
  311. _lock_release(&s_phy_access_lock);
  312. }
  313. IRAM_ATTR void esp_mac_bb_power_up(void)
  314. {
  315. if (s_mac_bb_pd_mem == NULL) {
  316. return;
  317. }
  318. esp_wifi_bt_power_domain_on();
  319. if (!s_mac_bb_pu) {
  320. esp_phy_common_clock_enable();
  321. phy_freq_mem_backup(false, s_mac_bb_pd_mem);
  322. esp_phy_common_clock_disable();
  323. s_mac_bb_pu = true;
  324. }
  325. }
  326. IRAM_ATTR void esp_mac_bb_power_down(void)
  327. {
  328. if (s_mac_bb_pd_mem == NULL) {
  329. return;
  330. }
  331. if (s_mac_bb_pu) {
  332. esp_phy_common_clock_enable();
  333. phy_freq_mem_backup(true, s_mac_bb_pd_mem);
  334. esp_phy_common_clock_disable();
  335. s_mac_bb_pu = false;
  336. }
  337. esp_wifi_bt_power_domain_off();
  338. }
  339. #endif
  340. // PHY init data handling functions
  341. #if CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
  342. #include "esp_partition.h"
  343. const esp_phy_init_data_t* esp_phy_get_init_data(void)
  344. {
  345. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  346. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  347. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  348. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  349. if (init_data_store == NULL) {
  350. ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
  351. return NULL;
  352. }
  353. memcpy(init_data_store, multi_phy_init_data_bin_start, init_data_store_length);
  354. ESP_LOGI(TAG, "loading embedded multiple PHY init data");
  355. #else
  356. const esp_partition_t* partition = esp_partition_find_first(
  357. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
  358. if (partition == NULL) {
  359. ESP_LOGE(TAG, "PHY data partition not found");
  360. return NULL;
  361. }
  362. ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
  363. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  364. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  365. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  366. if (init_data_store == NULL) {
  367. ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
  368. return NULL;
  369. }
  370. // read phy data from flash
  371. esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
  372. if (err != ESP_OK) {
  373. ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
  374. free(init_data_store);
  375. return NULL;
  376. }
  377. #endif
  378. // verify data
  379. if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
  380. memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  381. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
  382. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  383. ESP_LOGE(TAG, "failed to validate embedded PHY init data");
  384. free(init_data_store);
  385. return NULL;
  386. #else
  387. #ifndef CONFIG_ESP_PHY_DEFAULT_INIT_IF_INVALID
  388. ESP_LOGE(TAG, "failed to validate PHY data partition");
  389. free(init_data_store);
  390. return NULL;
  391. #else
  392. ESP_LOGE(TAG, "failed to validate PHY data partition, restoring default data into flash...");
  393. memcpy(init_data_store,
  394. PHY_INIT_MAGIC, sizeof(phy_init_magic_pre));
  395. memcpy(init_data_store + sizeof(phy_init_magic_pre),
  396. &phy_init_data, sizeof(phy_init_data));
  397. memcpy(init_data_store + sizeof(phy_init_magic_pre) + sizeof(phy_init_data),
  398. PHY_INIT_MAGIC, sizeof(phy_init_magic_post));
  399. assert(memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) == 0);
  400. assert(memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  401. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) == 0);
  402. // write default data
  403. err = esp_partition_write(partition, 0, init_data_store, init_data_store_length);
  404. if (err != ESP_OK) {
  405. ESP_LOGE(TAG, "failed to write default PHY data partition (0x%x)", err);
  406. free(init_data_store);
  407. return NULL;
  408. }
  409. #endif // CONFIG_ESP_PHY_DEFAULT_INIT_IF_INVALID
  410. #endif // CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  411. }
  412. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
  413. if ((*(init_data_store + (sizeof(phy_init_magic_pre) + PHY_SUPPORT_MULTIPLE_BIN_OFFSET)))) {
  414. s_multiple_phy_init_data_bin = true;
  415. ESP_LOGI(TAG, "Support multiple PHY init data bins");
  416. } else {
  417. ESP_LOGW(TAG, "Does not support multiple PHY init data bins");
  418. }
  419. #endif
  420. ESP_LOGD(TAG, "PHY data partition validated");
  421. return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
  422. }
  423. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  424. {
  425. free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
  426. }
  427. #else // CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
  428. // phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
  429. const esp_phy_init_data_t* esp_phy_get_init_data(void)
  430. {
  431. ESP_LOGD(TAG, "loading PHY init data from application binary");
  432. return &phy_init_data;
  433. }
  434. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  435. {
  436. // no-op
  437. }
  438. #endif // CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
  439. // PHY calibration data handling functions
  440. static const char* PHY_NAMESPACE = "phy";
  441. static const char* PHY_CAL_VERSION_KEY = "cal_version";
  442. static const char* PHY_CAL_MAC_KEY = "cal_mac";
  443. static const char* PHY_CAL_DATA_KEY = "cal_data";
  444. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle_t handle,
  445. esp_phy_calibration_data_t* out_cal_data);
  446. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle_t handle,
  447. const esp_phy_calibration_data_t* cal_data);
  448. esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
  449. {
  450. nvs_handle_t handle;
  451. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
  452. if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
  453. ESP_LOGE(TAG, "%s: NVS has not been initialized. "
  454. "Call nvs_flash_init before starting WiFi/BT.", __func__);
  455. return err;
  456. } else if (err != ESP_OK) {
  457. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  458. return err;
  459. }
  460. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  461. nvs_close(handle);
  462. return err;
  463. }
  464. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  465. {
  466. nvs_handle_t handle;
  467. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  468. if (err != ESP_OK) {
  469. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  470. return err;
  471. }
  472. else {
  473. err = store_cal_data_to_nvs_handle(handle, cal_data);
  474. nvs_close(handle);
  475. return err;
  476. }
  477. }
  478. esp_err_t esp_phy_erase_cal_data_in_nvs(void)
  479. {
  480. nvs_handle_t handle;
  481. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  482. if (err != ESP_OK) {
  483. ESP_LOGE(TAG, "%s: failed to open NVS phy namespace (0x%x)", __func__, err);
  484. return err;
  485. }
  486. else {
  487. err = nvs_erase_all(handle);
  488. if (err != ESP_OK) {
  489. ESP_LOGE(TAG, "%s: failed to erase NVS phy namespace (0x%x)", __func__, err);
  490. }
  491. else {
  492. err = nvs_commit(handle);
  493. if (err != ESP_OK) {
  494. ESP_LOGE(TAG, "%s: failed to commit NVS phy namespace (0x%x)", __func__, err);
  495. }
  496. }
  497. }
  498. nvs_close(handle);
  499. return err;
  500. }
  501. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle_t handle,
  502. esp_phy_calibration_data_t* out_cal_data)
  503. {
  504. esp_err_t err;
  505. uint32_t cal_data_version;
  506. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  507. if (err != ESP_OK) {
  508. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  509. return err;
  510. }
  511. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  512. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  513. if (cal_data_version != cal_format_version) {
  514. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  515. __func__, cal_format_version, cal_data_version);
  516. return ESP_FAIL;
  517. }
  518. uint8_t cal_data_mac[6];
  519. size_t length = sizeof(cal_data_mac);
  520. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  521. if (err != ESP_OK) {
  522. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  523. return err;
  524. }
  525. if (length != sizeof(cal_data_mac)) {
  526. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  527. return ESP_ERR_INVALID_SIZE;
  528. }
  529. uint8_t sta_mac[6];
  530. esp_efuse_mac_get_default(sta_mac);
  531. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  532. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  533. MACSTR ", found " MACSTR,
  534. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  535. return ESP_FAIL;
  536. }
  537. length = sizeof(*out_cal_data);
  538. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  539. if (err != ESP_OK) {
  540. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  541. return err;
  542. }
  543. if (length != sizeof(*out_cal_data)) {
  544. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  545. return ESP_ERR_INVALID_SIZE;
  546. }
  547. return ESP_OK;
  548. }
  549. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle_t handle,
  550. const esp_phy_calibration_data_t* cal_data)
  551. {
  552. esp_err_t err;
  553. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  554. if (err != ESP_OK) {
  555. ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
  556. return err;
  557. }
  558. uint8_t sta_mac[6];
  559. esp_efuse_mac_get_default(sta_mac);
  560. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  561. if (err != ESP_OK) {
  562. ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
  563. return err;
  564. }
  565. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  566. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  567. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  568. if (err != ESP_OK) {
  569. ESP_LOGE(TAG, "%s: store calibration version failed(0x%x)\n", __func__, err);
  570. return err;
  571. }
  572. err = nvs_commit(handle);
  573. if (err != ESP_OK) {
  574. ESP_LOGE(TAG, "%s: store calibration nvs commit failed(0x%x)\n", __func__, err);
  575. }
  576. return err;
  577. }
  578. #if CONFIG_ESP_PHY_REDUCE_TX_POWER
  579. static void __attribute((unused)) esp_phy_reduce_tx_power(esp_phy_init_data_t* init_data)
  580. {
  581. uint8_t i;
  582. for(i = 0; i < PHY_TX_POWER_NUM; i++) {
  583. // LOWEST_PHY_TX_POWER is the lowest tx power
  584. init_data->params[PHY_TX_POWER_OFFSET+i] = PHY_TX_POWER_LOWEST;
  585. }
  586. }
  587. #endif
  588. void esp_phy_load_cal_and_init(void)
  589. {
  590. char * phy_version = get_phy_version_str();
  591. ESP_LOGI(TAG, "phy_version %s", phy_version);
  592. #if CONFIG_IDF_TARGET_ESP32S2
  593. phy_eco_version_sel(efuse_hal_chip_revision() / 100);
  594. #endif
  595. esp_phy_calibration_data_t* cal_data =
  596. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  597. if (cal_data == NULL) {
  598. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  599. abort();
  600. }
  601. #if CONFIG_ESP_PHY_REDUCE_TX_POWER
  602. const esp_phy_init_data_t* phy_init_data = esp_phy_get_init_data();
  603. if (phy_init_data == NULL) {
  604. ESP_LOGE(TAG, "failed to obtain PHY init data");
  605. abort();
  606. }
  607. esp_phy_init_data_t* init_data = (esp_phy_init_data_t*) malloc(sizeof(esp_phy_init_data_t));
  608. if (init_data == NULL) {
  609. ESP_LOGE(TAG, "failed to allocate memory for phy init data");
  610. abort();
  611. }
  612. memcpy(init_data, phy_init_data, sizeof(esp_phy_init_data_t));
  613. if (esp_reset_reason() == ESP_RST_BROWNOUT) {
  614. esp_phy_reduce_tx_power(init_data);
  615. }
  616. #else
  617. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  618. if (init_data == NULL) {
  619. ESP_LOGE(TAG, "failed to obtain PHY init data");
  620. abort();
  621. }
  622. #endif
  623. #if CONFIG_ESP_PHY_ENABLE_USB
  624. phy_bbpll_en_usb(true);
  625. #endif
  626. #ifdef CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE
  627. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  628. uint8_t sta_mac[6];
  629. if (esp_rom_get_reset_reason(0) == RESET_REASON_CORE_DEEP_SLEEP) {
  630. calibration_mode = PHY_RF_CAL_NONE;
  631. }
  632. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  633. if (err != ESP_OK) {
  634. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  635. calibration_mode = PHY_RF_CAL_FULL;
  636. }
  637. esp_efuse_mac_get_default(sta_mac);
  638. memcpy(cal_data->mac, sta_mac, 6);
  639. esp_err_t ret = register_chipv7_phy(init_data, cal_data, calibration_mode);
  640. if (ret == ESP_CAL_DATA_CHECK_FAIL) {
  641. ESP_LOGW(TAG, "saving new calibration data because of checksum failure, mode(%d)", calibration_mode);
  642. }
  643. if ((calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) ||
  644. (calibration_mode != PHY_RF_CAL_FULL && ret == ESP_CAL_DATA_CHECK_FAIL)) {
  645. err = esp_phy_store_cal_data_to_nvs(cal_data);
  646. } else {
  647. err = ESP_OK;
  648. }
  649. #else
  650. register_chipv7_phy(init_data, cal_data, PHY_RF_CAL_FULL);
  651. #endif
  652. #if CONFIG_ESP_PHY_REDUCE_TX_POWER
  653. esp_phy_release_init_data(phy_init_data);
  654. free(init_data);
  655. #else
  656. esp_phy_release_init_data(init_data);
  657. #endif
  658. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  659. }
  660. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
  661. static esp_err_t phy_crc_check_init_data(uint8_t* init_data, const uint8_t* checksum, size_t init_data_length)
  662. {
  663. uint32_t crc_data = 0;
  664. crc_data = esp_rom_crc32_le(crc_data, init_data, init_data_length);
  665. uint32_t crc_size_conversion = htobe32(crc_data);
  666. if (crc_size_conversion != *(uint32_t*)(checksum)) {
  667. return ESP_FAIL;
  668. }
  669. return ESP_OK;
  670. }
  671. static uint8_t phy_find_bin_type_according_country(const char* country)
  672. {
  673. uint32_t i = 0;
  674. uint8_t phy_init_data_type = 0;
  675. for (i = 0; i < sizeof(s_country_code_map_type_table)/sizeof(phy_country_to_bin_type_t); i++)
  676. {
  677. if (!memcmp(country, s_country_code_map_type_table[i].cc, sizeof(s_phy_current_country))) {
  678. phy_init_data_type = s_country_code_map_type_table[i].type;
  679. ESP_LOGD(TAG, "Current country is %c%c, PHY init data type is %s\n", s_country_code_map_type_table[i].cc[0],
  680. s_country_code_map_type_table[i].cc[1], s_phy_type[s_country_code_map_type_table[i].type]);
  681. break;
  682. }
  683. }
  684. if (i == sizeof(s_country_code_map_type_table)/sizeof(phy_country_to_bin_type_t)) {
  685. phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
  686. ESP_LOGW(TAG, "Use the default certification code beacuse %c%c doesn't have a certificate", country[0], country[1]);
  687. }
  688. return phy_init_data_type;
  689. }
  690. static esp_err_t phy_find_bin_data_according_type(uint8_t* out_init_data_store,
  691. const phy_control_info_data_t* init_data_control_info,
  692. const uint8_t* init_data_multiple,
  693. phy_init_data_type_t init_data_type)
  694. {
  695. int i = 0;
  696. for (i = 0; i < init_data_control_info->number; i++) {
  697. if (init_data_type == *(init_data_multiple + (i * sizeof(esp_phy_init_data_t)) + PHY_INIT_DATA_TYPE_OFFSET)) {
  698. memcpy(out_init_data_store + sizeof(phy_init_magic_pre),
  699. init_data_multiple + (i * sizeof(esp_phy_init_data_t)), sizeof(esp_phy_init_data_t));
  700. break;
  701. }
  702. }
  703. if (i == init_data_control_info->number) {
  704. return ESP_FAIL;
  705. }
  706. return ESP_OK;
  707. }
  708. static esp_err_t phy_get_multiple_init_data(const esp_partition_t* partition,
  709. uint8_t* init_data_store,
  710. size_t init_data_store_length,
  711. phy_init_data_type_t init_data_type)
  712. {
  713. phy_control_info_data_t* init_data_control_info = (phy_control_info_data_t*) malloc(sizeof(phy_control_info_data_t));
  714. if (init_data_control_info == NULL) {
  715. ESP_LOGE(TAG, "failed to allocate memory for PHY init data control info");
  716. return ESP_FAIL;
  717. }
  718. esp_err_t err = ESP_OK;
  719. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  720. memcpy(init_data_control_info, multi_phy_init_data_bin_start + init_data_store_length, sizeof(phy_control_info_data_t));
  721. #else
  722. err = esp_partition_read(partition, init_data_store_length, init_data_control_info, sizeof(phy_control_info_data_t));
  723. if (err != ESP_OK) {
  724. free(init_data_control_info);
  725. ESP_LOGE(TAG, "failed to read PHY control info data partition (0x%x)", err);
  726. return ESP_FAIL;
  727. }
  728. #endif
  729. if ((init_data_control_info->check_algorithm) == PHY_CRC_ALGORITHM) {
  730. err = phy_crc_check_init_data(init_data_control_info->multiple_bin_checksum, init_data_control_info->control_info_checksum,
  731. sizeof(phy_control_info_data_t) - sizeof(init_data_control_info->control_info_checksum));
  732. if (err != ESP_OK) {
  733. free(init_data_control_info);
  734. ESP_LOGE(TAG, "PHY init data control info check error");
  735. return ESP_FAIL;
  736. }
  737. } else {
  738. free(init_data_control_info);
  739. ESP_LOGE(TAG, "Check algorithm not CRC, PHY init data update failed");
  740. return ESP_FAIL;
  741. }
  742. uint8_t* init_data_multiple = (uint8_t*) malloc(sizeof(esp_phy_init_data_t) * init_data_control_info->number);
  743. if (init_data_multiple == NULL) {
  744. free(init_data_control_info);
  745. ESP_LOGE(TAG, "failed to allocate memory for PHY init data multiple bin");
  746. return ESP_FAIL;
  747. }
  748. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  749. memcpy(init_data_multiple, multi_phy_init_data_bin_start + init_data_store_length + sizeof(phy_control_info_data_t), sizeof(esp_phy_init_data_t) * init_data_control_info->number);
  750. #else
  751. err = esp_partition_read(partition, init_data_store_length + sizeof(phy_control_info_data_t),
  752. init_data_multiple, sizeof(esp_phy_init_data_t) * init_data_control_info->number);
  753. if (err != ESP_OK) {
  754. free(init_data_multiple);
  755. free(init_data_control_info);
  756. ESP_LOGE(TAG, "failed to read PHY init data multiple bin partition (0x%x)", err);
  757. return ESP_FAIL;
  758. }
  759. #endif
  760. if ((init_data_control_info->check_algorithm) == PHY_CRC_ALGORITHM) {
  761. err = phy_crc_check_init_data(init_data_multiple, init_data_control_info->multiple_bin_checksum,
  762. sizeof(esp_phy_init_data_t) * init_data_control_info->number);
  763. if (err != ESP_OK) {
  764. free(init_data_multiple);
  765. free(init_data_control_info);
  766. ESP_LOGE(TAG, "PHY init data multiple bin check error");
  767. return ESP_FAIL;
  768. }
  769. } else {
  770. free(init_data_multiple);
  771. free(init_data_control_info);
  772. ESP_LOGE(TAG, "Check algorithm not CRC, PHY init data update failed");
  773. return ESP_FAIL;
  774. }
  775. err = phy_find_bin_data_according_type(init_data_store, init_data_control_info, init_data_multiple, init_data_type);
  776. if (err != ESP_OK) {
  777. ESP_LOGW(TAG, "%s has not been certified, use DEFAULT PHY init data", s_phy_type[init_data_type]);
  778. s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
  779. } else {
  780. s_phy_init_data_type = init_data_type;
  781. }
  782. free(init_data_multiple);
  783. free(init_data_control_info);
  784. return ESP_OK;
  785. }
  786. esp_err_t esp_phy_update_init_data(phy_init_data_type_t init_data_type)
  787. {
  788. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
  789. esp_err_t err = ESP_OK;
  790. const esp_partition_t* partition = NULL;
  791. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  792. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  793. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  794. if (init_data_store == NULL) {
  795. ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
  796. return ESP_ERR_NO_MEM;
  797. }
  798. memcpy(init_data_store, multi_phy_init_data_bin_start, init_data_store_length);
  799. ESP_LOGI(TAG, "load embedded multi phy init data");
  800. #else
  801. const esp_partition_t* partition = esp_partition_find_first(
  802. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
  803. if (partition == NULL) {
  804. ESP_LOGE(TAG, "Updated country code PHY data partition not found");
  805. return ESP_FAIL;
  806. }
  807. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  808. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  809. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  810. if (init_data_store == NULL) {
  811. ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
  812. return ESP_ERR_NO_MEM;
  813. }
  814. esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
  815. if (err != ESP_OK) {
  816. free(init_data_store);
  817. ESP_LOGE(TAG, "failed to read updated country code PHY data partition (0x%x)", err);
  818. return ESP_FAIL;
  819. }
  820. #endif
  821. if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
  822. memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  823. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
  824. free(init_data_store);
  825. ESP_LOGE(TAG, "failed to validate updated country code PHY data partition");
  826. return ESP_FAIL;
  827. }
  828. //find init data bin according init data type
  829. if (init_data_type != ESP_PHY_INIT_DATA_TYPE_DEFAULT) {
  830. err = phy_get_multiple_init_data(partition, init_data_store, init_data_store_length, init_data_type);
  831. if (err != ESP_OK) {
  832. free(init_data_store);
  833. #if CONFIG_ESP_PHY_INIT_DATA_ERROR
  834. abort();
  835. #else
  836. return ESP_FAIL;
  837. #endif
  838. }
  839. } else {
  840. s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
  841. }
  842. if (s_current_apply_phy_init_data != s_phy_init_data_type) {
  843. err = esp_phy_apply_phy_init_data(init_data_store + sizeof(phy_init_magic_pre));
  844. if (err != ESP_OK) {
  845. ESP_LOGE(TAG, "PHY init data failed to load");
  846. free(init_data_store);
  847. return ESP_FAIL;
  848. }
  849. ESP_LOGI(TAG, "PHY init data type updated from %s to %s",
  850. s_phy_type[s_current_apply_phy_init_data], s_phy_type[s_phy_init_data_type]);
  851. s_current_apply_phy_init_data = s_phy_init_data_type;
  852. }
  853. free(init_data_store);
  854. return ESP_OK;
  855. }
  856. #endif
  857. esp_err_t esp_phy_update_country_info(const char *country)
  858. {
  859. #if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
  860. uint8_t phy_init_data_type_map = 0;
  861. if (!s_multiple_phy_init_data_bin) {
  862. ESP_LOGD(TAG, "Does not support multiple PHY init data bins");
  863. return ESP_FAIL;
  864. }
  865. //if country equal s_phy_current_country, return;
  866. if (!memcmp(country, s_phy_current_country, sizeof(s_phy_current_country))) {
  867. return ESP_OK;
  868. }
  869. memcpy(s_phy_current_country, country, sizeof(s_phy_current_country));
  870. phy_init_data_type_map = phy_find_bin_type_according_country(country);
  871. if (phy_init_data_type_map == s_phy_init_data_type) {
  872. return ESP_OK;
  873. }
  874. esp_err_t err = esp_phy_update_init_data(phy_init_data_type_map);
  875. if (err != ESP_OK) {
  876. return err;
  877. }
  878. #endif
  879. return ESP_OK;
  880. }
  881. void esp_wifi_power_domain_on(void) __attribute__((alias("esp_wifi_bt_power_domain_on")));
  882. void esp_wifi_power_domain_off(void) __attribute__((alias("esp_wifi_bt_power_domain_off")));