phy_init.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <stddef.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdbool.h>
  18. #include <sys/lock.h>
  19. #include "rom/ets_sys.h"
  20. #include "rom/rtc.h"
  21. #include "soc/rtc.h"
  22. #include "soc/dport_reg.h"
  23. #include "esp_err.h"
  24. #include "esp_phy_init.h"
  25. #include "esp_system.h"
  26. #include "esp_log.h"
  27. #include "nvs.h"
  28. #include "nvs_flash.h"
  29. #include "sdkconfig.h"
  30. #include "phy.h"
  31. #include "phy_init_data.h"
  32. #include "esp_coexist.h"
  33. #include "driver/periph_ctrl.h"
  34. static const char* TAG = "phy_init";
  35. /* Count value to indicate if there is peripheral that has initialized PHY and RF */
  36. static int s_phy_rf_init_count = 0;
  37. static _lock_t s_phy_rf_init_lock;
  38. esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data,
  39. esp_phy_calibration_mode_t mode, esp_phy_calibration_data_t* calibration_data)
  40. {
  41. assert((s_phy_rf_init_count <= 1) && (s_phy_rf_init_count >= 0));
  42. _lock_acquire(&s_phy_rf_init_lock);
  43. if (s_phy_rf_init_count == 0) {
  44. // Enable WiFi/BT common peripheral clock
  45. periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE);
  46. ESP_LOGV(TAG, "register_chipv7_phy, init_data=%p, cal_data=%p, mode=%d",
  47. init_data, calibration_data, mode);
  48. phy_set_wifi_mode_only(0);
  49. register_chipv7_phy(init_data, calibration_data, mode);
  50. coex_bt_high_prio();
  51. } else {
  52. #if CONFIG_SW_COEXIST_ENABLE
  53. coex_init();
  54. #endif
  55. }
  56. s_phy_rf_init_count++;
  57. _lock_release(&s_phy_rf_init_lock);
  58. return ESP_OK;
  59. }
  60. esp_err_t esp_phy_rf_deinit(void)
  61. {
  62. assert((s_phy_rf_init_count <= 2) && (s_phy_rf_init_count >= 1));
  63. _lock_acquire(&s_phy_rf_init_lock);
  64. if (s_phy_rf_init_count == 1) {
  65. // Disable PHY and RF.
  66. phy_close_rf();
  67. // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG
  68. periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE);
  69. } else {
  70. #if CONFIG_SW_COEXIST_ENABLE
  71. coex_deinit();
  72. #endif
  73. }
  74. s_phy_rf_init_count--;
  75. _lock_release(&s_phy_rf_init_lock);
  76. return ESP_OK;
  77. }
  78. // PHY init data handling functions
  79. #if CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  80. #include "esp_partition.h"
  81. const esp_phy_init_data_t* esp_phy_get_init_data()
  82. {
  83. const esp_partition_t* partition = esp_partition_find_first(
  84. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
  85. if (partition == NULL) {
  86. ESP_LOGE(TAG, "PHY data partition not found");
  87. return NULL;
  88. }
  89. ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
  90. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  91. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  92. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  93. if (init_data_store == NULL) {
  94. ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
  95. return NULL;
  96. }
  97. esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
  98. if (err != ESP_OK) {
  99. ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
  100. return NULL;
  101. }
  102. if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
  103. memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  104. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
  105. ESP_LOGE(TAG, "failed to validate PHY data partition");
  106. return NULL;
  107. }
  108. ESP_LOGD(TAG, "PHY data partition validated");
  109. return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
  110. }
  111. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  112. {
  113. free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
  114. }
  115. #else // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  116. // phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
  117. const esp_phy_init_data_t* esp_phy_get_init_data()
  118. {
  119. ESP_LOGD(TAG, "loading PHY init data from application binary");
  120. return &phy_init_data;
  121. }
  122. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  123. {
  124. // no-op
  125. }
  126. #endif // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  127. // PHY calibration data handling functions
  128. static const char* PHY_NAMESPACE = "phy";
  129. static const char* PHY_CAL_VERSION_KEY = "cal_version";
  130. static const char* PHY_CAL_MAC_KEY = "cal_mac";
  131. static const char* PHY_CAL_DATA_KEY = "cal_data";
  132. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  133. esp_phy_calibration_data_t* out_cal_data);
  134. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  135. const esp_phy_calibration_data_t* cal_data);
  136. esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
  137. {
  138. nvs_handle handle;
  139. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
  140. if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
  141. ESP_LOGE(TAG, "%s: NVS has not been initialized. "
  142. "Call nvs_flash_init before starting WiFi/BT.", __func__);
  143. } else if (err != ESP_OK) {
  144. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  145. return err;
  146. }
  147. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  148. nvs_close(handle);
  149. return err;
  150. }
  151. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  152. {
  153. nvs_handle handle;
  154. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  155. if (err != ESP_OK) {
  156. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  157. return err;
  158. }
  159. else {
  160. err = store_cal_data_to_nvs_handle(handle, cal_data);
  161. nvs_close(handle);
  162. return err;
  163. }
  164. }
  165. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  166. esp_phy_calibration_data_t* out_cal_data)
  167. {
  168. esp_err_t err;
  169. uint32_t cal_data_version;
  170. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  171. if (err != ESP_OK) {
  172. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  173. return err;
  174. }
  175. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  176. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  177. if (cal_data_version != cal_format_version) {
  178. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  179. __func__, cal_format_version, cal_data_version);
  180. return ESP_FAIL;
  181. }
  182. uint8_t cal_data_mac[6];
  183. size_t length = sizeof(cal_data_mac);
  184. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  185. if (err != ESP_OK) {
  186. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  187. return err;
  188. }
  189. if (length != sizeof(cal_data_mac)) {
  190. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  191. return ESP_ERR_INVALID_SIZE;
  192. }
  193. uint8_t sta_mac[6];
  194. esp_efuse_mac_get_default(sta_mac);
  195. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  196. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  197. MACSTR ", found " MACSTR,
  198. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  199. return ESP_FAIL;
  200. }
  201. length = sizeof(*out_cal_data);
  202. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  203. if (err != ESP_OK) {
  204. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  205. return err;
  206. }
  207. if (length != sizeof(*out_cal_data)) {
  208. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  209. return ESP_ERR_INVALID_SIZE;
  210. }
  211. return ESP_OK;
  212. }
  213. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  214. const esp_phy_calibration_data_t* cal_data)
  215. {
  216. esp_err_t err;
  217. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  218. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  219. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  220. if (err != ESP_OK) {
  221. return err;
  222. }
  223. uint8_t sta_mac[6];
  224. esp_efuse_mac_get_default(sta_mac);
  225. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  226. if (err != ESP_OK) {
  227. return err;
  228. }
  229. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  230. return err;
  231. }
  232. void esp_phy_load_cal_and_init(void)
  233. {
  234. esp_phy_calibration_data_t* cal_data =
  235. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  236. if (cal_data == NULL) {
  237. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  238. abort();
  239. }
  240. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  241. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  242. if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
  243. calibration_mode = PHY_RF_CAL_NONE;
  244. }
  245. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  246. if (init_data == NULL) {
  247. ESP_LOGE(TAG, "failed to obtain PHY init data");
  248. abort();
  249. }
  250. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  251. if (err != ESP_OK) {
  252. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  253. calibration_mode = PHY_RF_CAL_FULL;
  254. }
  255. esp_phy_rf_init(init_data, calibration_mode, cal_data);
  256. if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) {
  257. err = esp_phy_store_cal_data_to_nvs(cal_data);
  258. } else {
  259. err = ESP_OK;
  260. }
  261. esp_phy_release_init_data(init_data);
  262. #else
  263. esp_phy_rf_init(NULL, PHY_RF_CAL_FULL, cal_data);
  264. #endif
  265. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  266. }