periph_ctrl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "hal/clk_gate_ll.h"
  8. #include "esp_attr.h"
  9. #include "esp_private/periph_ctrl.h"
  10. #include "soc/soc_caps.h"
  11. #if SOC_MODEM_CLOCK_IS_INDEPENDENT
  12. #include "esp_private/esp_modem_clock.h"
  13. #endif
  14. /// @brief For simplicity and backward compatible, we are using the same spin lock for both bus clock on/off and reset
  15. /// @note We may want to split them into two spin locks in the future
  16. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  17. static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0};
  18. IRAM_ATTR void periph_rcc_enter(void)
  19. {
  20. portENTER_CRITICAL_SAFE(&periph_spinlock);
  21. }
  22. IRAM_ATTR void periph_rcc_exit(void)
  23. {
  24. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  25. }
  26. uint8_t periph_rcc_acquire_enter(periph_module_t periph)
  27. {
  28. periph_rcc_enter();
  29. return ref_counts[periph];
  30. }
  31. void periph_rcc_acquire_exit(periph_module_t periph, uint8_t ref_count)
  32. {
  33. ref_counts[periph] = ++ref_count;
  34. periph_rcc_exit();
  35. }
  36. uint8_t periph_rcc_release_enter(periph_module_t periph)
  37. {
  38. periph_rcc_enter();
  39. return ref_counts[periph] - 1;
  40. }
  41. void periph_rcc_release_exit(periph_module_t periph, uint8_t ref_count)
  42. {
  43. ref_counts[periph] = ref_count;
  44. periph_rcc_exit();
  45. }
  46. void periph_module_enable(periph_module_t periph)
  47. {
  48. assert(periph < PERIPH_MODULE_MAX);
  49. portENTER_CRITICAL_SAFE(&periph_spinlock);
  50. if (ref_counts[periph] == 0) {
  51. periph_ll_enable_clk_clear_rst(periph);
  52. }
  53. ref_counts[periph]++;
  54. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  55. }
  56. void periph_module_disable(periph_module_t periph)
  57. {
  58. assert(periph < PERIPH_MODULE_MAX);
  59. portENTER_CRITICAL_SAFE(&periph_spinlock);
  60. ref_counts[periph]--;
  61. if (ref_counts[periph] == 0) {
  62. periph_ll_disable_clk_set_rst(periph);
  63. }
  64. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  65. }
  66. void periph_module_reset(periph_module_t periph)
  67. {
  68. assert(periph < PERIPH_MODULE_MAX);
  69. portENTER_CRITICAL_SAFE(&periph_spinlock);
  70. periph_ll_reset(periph);
  71. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  72. }
  73. #if !SOC_IEEE802154_BLE_ONLY
  74. #if SOC_BT_SUPPORTED || SOC_WIFI_SUPPORTED
  75. IRAM_ATTR void wifi_bt_common_module_enable(void)
  76. {
  77. #if SOC_MODEM_CLOCK_IS_INDEPENDENT
  78. modem_clock_module_enable(PERIPH_PHY_MODULE);
  79. #else
  80. portENTER_CRITICAL_SAFE(&periph_spinlock);
  81. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  82. periph_ll_wifi_bt_module_enable_clk();
  83. }
  84. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++;
  85. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  86. #endif
  87. }
  88. IRAM_ATTR void wifi_bt_common_module_disable(void)
  89. {
  90. #if SOC_MODEM_CLOCK_IS_INDEPENDENT
  91. modem_clock_module_disable(PERIPH_PHY_MODULE);
  92. #else
  93. portENTER_CRITICAL_SAFE(&periph_spinlock);
  94. ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--;
  95. if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
  96. periph_ll_wifi_bt_module_disable_clk();
  97. }
  98. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  99. #endif
  100. }
  101. #endif //#if SOC_BT_SUPPORTED || SOC_WIFI_SUPPORTED
  102. #endif //#if !SOC_IEEE802154_BLE_ONLY
  103. #if CONFIG_ESP_WIFI_ENABLED
  104. void wifi_module_enable(void)
  105. {
  106. #if SOC_MODEM_CLOCK_IS_INDEPENDENT
  107. modem_clock_module_enable(PERIPH_WIFI_MODULE);
  108. #else
  109. portENTER_CRITICAL_SAFE(&periph_spinlock);
  110. periph_ll_wifi_module_enable_clk_clear_rst();
  111. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  112. #endif
  113. }
  114. void wifi_module_disable(void)
  115. {
  116. #if SOC_MODEM_CLOCK_IS_INDEPENDENT
  117. modem_clock_module_disable(PERIPH_WIFI_MODULE);
  118. #else
  119. portENTER_CRITICAL_SAFE(&periph_spinlock);
  120. periph_ll_wifi_module_disable_clk_set_rst();
  121. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  122. #endif
  123. }
  124. #endif // CONFIG_ESP_WIFI_ENABLED