phy_init.c 10 KB

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