phy_init.c 33 KB

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