phy_init.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #ifdef CONFIG_PHY_ENABLED
  31. #include "phy.h"
  32. #include "phy_init_data.h"
  33. #include "esp_coexist.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 peripheral clock
  45. SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_EN | DPORT_WIFI_CLK_RNG_EN);
  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 peripheral clock. Do not disable clock for hardware RNG
  68. CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_EN);
  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_LOGE(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. esp_err_t err = nvs_flash_init();
  139. if (err != ESP_OK) {
  140. ESP_LOGW(TAG, "%s: failed to initialize NVS (0x%x)", __func__, err);
  141. return err;
  142. }
  143. nvs_handle handle;
  144. err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
  145. if (err != ESP_OK) {
  146. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  147. return err;
  148. }
  149. else {
  150. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  151. nvs_close(handle);
  152. return err;
  153. }
  154. }
  155. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  156. {
  157. nvs_handle handle;
  158. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  159. if (err != ESP_OK) {
  160. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  161. return err;
  162. }
  163. else {
  164. err = store_cal_data_to_nvs_handle(handle, cal_data);
  165. nvs_close(handle);
  166. return err;
  167. }
  168. }
  169. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  170. esp_phy_calibration_data_t* out_cal_data)
  171. {
  172. esp_err_t err;
  173. uint32_t cal_data_version;
  174. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  175. if (err != ESP_OK) {
  176. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  177. return err;
  178. }
  179. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  180. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  181. if (cal_data_version != cal_format_version) {
  182. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  183. __func__, cal_format_version, cal_data_version);
  184. return ESP_FAIL;
  185. }
  186. uint8_t cal_data_mac[6];
  187. size_t length = sizeof(cal_data_mac);
  188. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  189. if (err != ESP_OK) {
  190. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  191. return err;
  192. }
  193. if (length != sizeof(cal_data_mac)) {
  194. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  195. return ESP_ERR_INVALID_SIZE;
  196. }
  197. uint8_t sta_mac[6];
  198. esp_efuse_read_mac(sta_mac);
  199. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  200. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  201. MACSTR ", found " MACSTR,
  202. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  203. return ESP_FAIL;
  204. }
  205. length = sizeof(*out_cal_data);
  206. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  207. if (err != ESP_OK) {
  208. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  209. return err;
  210. }
  211. if (length != sizeof(*out_cal_data)) {
  212. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  213. return ESP_ERR_INVALID_SIZE;
  214. }
  215. return ESP_OK;
  216. }
  217. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  218. const esp_phy_calibration_data_t* cal_data)
  219. {
  220. esp_err_t err;
  221. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  222. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  223. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  224. if (err != ESP_OK) {
  225. return err;
  226. }
  227. uint8_t sta_mac[6];
  228. esp_efuse_read_mac(sta_mac);
  229. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  230. if (err != ESP_OK) {
  231. return err;
  232. }
  233. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  234. return err;
  235. }
  236. void esp_phy_load_cal_and_init(void)
  237. {
  238. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  239. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  240. if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
  241. calibration_mode = PHY_RF_CAL_NONE;
  242. }
  243. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  244. if (init_data == NULL) {
  245. ESP_LOGE(TAG, "failed to obtain PHY init data");
  246. abort();
  247. }
  248. esp_phy_calibration_data_t* cal_data =
  249. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  250. if (cal_data == NULL) {
  251. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  252. abort();
  253. }
  254. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  255. if (err != ESP_OK) {
  256. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  257. calibration_mode = PHY_RF_CAL_FULL;
  258. }
  259. esp_phy_rf_init(init_data, calibration_mode, cal_data);
  260. if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) {
  261. err = esp_phy_store_cal_data_to_nvs(cal_data);
  262. } else {
  263. err = ESP_OK;
  264. }
  265. esp_phy_release_init_data(init_data);
  266. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  267. #else
  268. esp_phy_rf_init(NULL, PHY_RF_CAL_NONE, NULL);
  269. #endif
  270. }
  271. #endif // CONFIG_PHY_ENABLED