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