phy_init.c 37 KB

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