phy_init.c 10 KB

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