periph_ctrl.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <esp_types.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/semphr.h"
  16. #include "freertos/xtensa_api.h"
  17. #include "soc/dport_reg.h"
  18. #include "driver/periph_ctrl.h"
  19. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  20. static uint8_t ref_counts = 0;
  21. /* Static functions to return register address & mask for clk_en / rst of each peripheral */
  22. static uint32_t get_clk_en_mask(periph_module_t periph);
  23. static uint32_t get_rst_en_mask(periph_module_t periph, bool enable);
  24. static uint32_t get_clk_en_reg(periph_module_t periph);
  25. static uint32_t get_rst_en_reg(periph_module_t periph);
  26. void periph_module_enable(periph_module_t periph)
  27. {
  28. portENTER_CRITICAL_SAFE(&periph_spinlock);
  29. DPORT_SET_PERI_REG_MASK(get_clk_en_reg(periph), get_clk_en_mask(periph));
  30. DPORT_CLEAR_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, true));
  31. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  32. }
  33. void periph_module_disable(periph_module_t periph)
  34. {
  35. portENTER_CRITICAL_SAFE(&periph_spinlock);
  36. DPORT_CLEAR_PERI_REG_MASK(get_clk_en_reg(periph), get_clk_en_mask(periph));
  37. DPORT_SET_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, false));
  38. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  39. }
  40. void periph_module_reset(periph_module_t periph)
  41. {
  42. portENTER_CRITICAL_SAFE(&periph_spinlock);
  43. DPORT_SET_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, false));
  44. DPORT_CLEAR_PERI_REG_MASK(get_rst_en_reg(periph), get_rst_en_mask(periph, false));
  45. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  46. }
  47. static uint32_t get_clk_en_mask(periph_module_t periph)
  48. {
  49. switch(periph) {
  50. case PERIPH_RMT_MODULE:
  51. return DPORT_RMT_CLK_EN;
  52. case PERIPH_LEDC_MODULE:
  53. return DPORT_LEDC_CLK_EN;
  54. case PERIPH_UART0_MODULE:
  55. return DPORT_UART_CLK_EN;
  56. case PERIPH_UART1_MODULE:
  57. return DPORT_UART1_CLK_EN;
  58. case PERIPH_UART2_MODULE:
  59. return DPORT_UART2_CLK_EN;
  60. case PERIPH_I2C0_MODULE:
  61. return DPORT_I2C_EXT0_CLK_EN;
  62. case PERIPH_I2C1_MODULE:
  63. return DPORT_I2C_EXT1_CLK_EN;
  64. case PERIPH_I2S0_MODULE:
  65. return DPORT_I2S0_CLK_EN;
  66. case PERIPH_I2S1_MODULE:
  67. return DPORT_I2S1_CLK_EN;
  68. case PERIPH_TIMG0_MODULE:
  69. return DPORT_TIMERGROUP_CLK_EN;
  70. case PERIPH_TIMG1_MODULE:
  71. return DPORT_TIMERGROUP1_CLK_EN;
  72. case PERIPH_PWM0_MODULE:
  73. return DPORT_PWM0_CLK_EN;
  74. case PERIPH_PWM1_MODULE:
  75. return DPORT_PWM1_CLK_EN;
  76. case PERIPH_PWM2_MODULE:
  77. return DPORT_PWM2_CLK_EN;
  78. case PERIPH_PWM3_MODULE:
  79. return DPORT_PWM3_CLK_EN;
  80. case PERIPH_UHCI0_MODULE:
  81. return DPORT_UHCI0_CLK_EN;
  82. case PERIPH_UHCI1_MODULE:
  83. return DPORT_UHCI1_CLK_EN;
  84. case PERIPH_PCNT_MODULE:
  85. return DPORT_PCNT_CLK_EN;
  86. case PERIPH_SPI_MODULE:
  87. return DPORT_SPI01_CLK_EN;
  88. case PERIPH_HSPI_MODULE:
  89. return DPORT_SPI2_CLK_EN;
  90. case PERIPH_VSPI_MODULE:
  91. return DPORT_SPI3_CLK_EN;
  92. case PERIPH_SPI_DMA_MODULE:
  93. return DPORT_SPI_DMA_CLK_EN;
  94. case PERIPH_SDMMC_MODULE:
  95. return DPORT_WIFI_CLK_SDIO_HOST_EN;
  96. case PERIPH_SDIO_SLAVE_MODULE:
  97. return DPORT_WIFI_CLK_SDIOSLAVE_EN;
  98. case PERIPH_CAN_MODULE:
  99. return DPORT_CAN_CLK_EN;
  100. case PERIPH_EMAC_MODULE:
  101. return DPORT_WIFI_CLK_EMAC_EN;
  102. case PERIPH_RNG_MODULE:
  103. return DPORT_WIFI_CLK_RNG_EN;
  104. case PERIPH_WIFI_MODULE:
  105. return DPORT_WIFI_CLK_WIFI_EN_M;
  106. case PERIPH_BT_MODULE:
  107. return DPORT_WIFI_CLK_BT_EN_M;
  108. case PERIPH_WIFI_BT_COMMON_MODULE:
  109. return DPORT_WIFI_CLK_WIFI_BT_COMMON_M;
  110. case PERIPH_BT_BASEBAND_MODULE:
  111. return DPORT_BT_BASEBAND_EN;
  112. case PERIPH_BT_LC_MODULE:
  113. return DPORT_BT_LC_EN;
  114. case PERIPH_AES_MODULE:
  115. return DPORT_PERI_EN_AES;
  116. case PERIPH_SHA_MODULE:
  117. return DPORT_PERI_EN_SHA;
  118. case PERIPH_RSA_MODULE:
  119. return DPORT_PERI_EN_RSA;
  120. default:
  121. return 0;
  122. }
  123. }
  124. static uint32_t get_rst_en_mask(periph_module_t periph, bool enable)
  125. {
  126. switch(periph) {
  127. case PERIPH_RMT_MODULE:
  128. return DPORT_RMT_RST;
  129. case PERIPH_LEDC_MODULE:
  130. return DPORT_LEDC_RST;
  131. case PERIPH_UART0_MODULE:
  132. return DPORT_UART_RST;
  133. case PERIPH_UART1_MODULE:
  134. return DPORT_UART1_RST;
  135. case PERIPH_UART2_MODULE:
  136. return DPORT_UART2_RST;
  137. case PERIPH_I2C0_MODULE:
  138. return DPORT_I2C_EXT0_RST;
  139. case PERIPH_I2C1_MODULE:
  140. return DPORT_I2C_EXT1_RST;
  141. case PERIPH_I2S0_MODULE:
  142. return DPORT_I2S0_RST;
  143. case PERIPH_I2S1_MODULE:
  144. return DPORT_I2S1_RST;
  145. case PERIPH_TIMG0_MODULE:
  146. return DPORT_TIMERGROUP_RST;
  147. case PERIPH_TIMG1_MODULE:
  148. return DPORT_TIMERGROUP1_RST;
  149. case PERIPH_PWM0_MODULE:
  150. return DPORT_PWM0_RST;
  151. case PERIPH_PWM1_MODULE:
  152. return DPORT_PWM1_RST;
  153. case PERIPH_PWM2_MODULE:
  154. return DPORT_PWM2_RST;
  155. case PERIPH_PWM3_MODULE:
  156. return DPORT_PWM3_RST;
  157. case PERIPH_UHCI0_MODULE:
  158. return DPORT_UHCI0_RST;
  159. case PERIPH_UHCI1_MODULE:
  160. return DPORT_UHCI1_RST;
  161. case PERIPH_PCNT_MODULE:
  162. return DPORT_PCNT_RST;
  163. case PERIPH_SPI_MODULE:
  164. return DPORT_SPI01_RST;
  165. case PERIPH_HSPI_MODULE:
  166. return DPORT_SPI2_RST;
  167. case PERIPH_VSPI_MODULE:
  168. return DPORT_SPI3_RST;
  169. case PERIPH_SPI_DMA_MODULE:
  170. return DPORT_SPI_DMA_RST;
  171. case PERIPH_SDMMC_MODULE:
  172. return DPORT_SDIO_HOST_RST;
  173. case PERIPH_SDIO_SLAVE_MODULE:
  174. return DPORT_SDIO_RST;
  175. case PERIPH_CAN_MODULE:
  176. return DPORT_CAN_RST;
  177. case PERIPH_EMAC_MODULE:
  178. return DPORT_EMAC_RST;
  179. case PERIPH_AES_MODULE:
  180. if (enable == true) {
  181. // Clear reset on digital signature & secure boot units, otherwise AES unit is held in reset also.
  182. return (DPORT_PERI_EN_AES | DPORT_PERI_EN_DIGITAL_SIGNATURE | DPORT_PERI_EN_SECUREBOOT);
  183. } else {
  184. //Don't return other units to reset, as this pulls reset on RSA & SHA units, respectively.
  185. return DPORT_PERI_EN_AES;
  186. }
  187. case PERIPH_SHA_MODULE:
  188. if (enable == true) {
  189. // Clear reset on secure boot, otherwise SHA is held in reset
  190. return (DPORT_PERI_EN_SHA | DPORT_PERI_EN_SECUREBOOT);
  191. } else {
  192. // Don't assert reset on secure boot, otherwise AES is held in reset
  193. return DPORT_PERI_EN_SHA;
  194. }
  195. case PERIPH_RSA_MODULE:
  196. if (enable == true) {
  197. // Also clear reset on digital signature, otherwise RSA is held in reset
  198. return (DPORT_PERI_EN_RSA | DPORT_PERI_EN_DIGITAL_SIGNATURE);
  199. } else {
  200. // Don't reset digital signature unit, as this resets AES also
  201. return DPORT_PERI_EN_RSA;
  202. }
  203. case PERIPH_WIFI_MODULE:
  204. case PERIPH_BT_MODULE:
  205. case PERIPH_WIFI_BT_COMMON_MODULE:
  206. case PERIPH_BT_BASEBAND_MODULE:
  207. case PERIPH_BT_LC_MODULE:
  208. return 0;
  209. default:
  210. return 0;
  211. }
  212. }
  213. static bool is_wifi_clk_peripheral(periph_module_t periph)
  214. {
  215. /* A small subset of peripherals use WIFI_CLK_EN_REG and
  216. CORE_RST_EN_REG for their clock & reset registers */
  217. switch(periph) {
  218. case PERIPH_SDMMC_MODULE:
  219. case PERIPH_SDIO_SLAVE_MODULE:
  220. case PERIPH_EMAC_MODULE:
  221. case PERIPH_RNG_MODULE:
  222. case PERIPH_WIFI_MODULE:
  223. case PERIPH_BT_MODULE:
  224. case PERIPH_WIFI_BT_COMMON_MODULE:
  225. case PERIPH_BT_BASEBAND_MODULE:
  226. case PERIPH_BT_LC_MODULE:
  227. return true;
  228. default:
  229. return false;
  230. }
  231. }
  232. static uint32_t get_clk_en_reg(periph_module_t periph)
  233. {
  234. if (periph == PERIPH_AES_MODULE || periph == PERIPH_SHA_MODULE || periph == PERIPH_RSA_MODULE) {
  235. return DPORT_PERI_CLK_EN_REG;
  236. } else {
  237. return is_wifi_clk_peripheral(periph) ? DPORT_WIFI_CLK_EN_REG : DPORT_PERIP_CLK_EN_REG;
  238. }
  239. }
  240. static uint32_t get_rst_en_reg(periph_module_t periph)
  241. {
  242. if (periph == PERIPH_AES_MODULE || periph == PERIPH_SHA_MODULE || periph == PERIPH_RSA_MODULE) {
  243. return DPORT_PERI_RST_EN_REG;
  244. } else {
  245. return is_wifi_clk_peripheral(periph) ? DPORT_CORE_RST_EN_REG : DPORT_PERIP_RST_EN_REG;
  246. }
  247. }
  248. IRAM_ATTR void wifi_bt_common_module_enable(void)
  249. {
  250. portENTER_CRITICAL_SAFE(&periph_spinlock);
  251. if (ref_counts == 0) {
  252. DPORT_SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG,DPORT_WIFI_CLK_WIFI_BT_COMMON_M);
  253. DPORT_CLEAR_PERI_REG_MASK(DPORT_CORE_RST_EN_REG,0);
  254. }
  255. ref_counts++;
  256. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  257. }
  258. IRAM_ATTR void wifi_bt_common_module_disable(void)
  259. {
  260. portENTER_CRITICAL_SAFE(&periph_spinlock);
  261. ref_counts--;
  262. if (ref_counts == 0) {
  263. DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG,DPORT_WIFI_CLK_WIFI_BT_COMMON_M);
  264. DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG,0);
  265. }
  266. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  267. }