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