clk.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2015-2017 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 <stdint.h>
  15. #include <sys/cdefs.h>
  16. #include <sys/time.h>
  17. #include <sys/param.h>
  18. #include "sdkconfig.h"
  19. #include "esp_attr.h"
  20. #include "esp_log.h"
  21. #include "esp_clk.h"
  22. #include "esp_clk_internal.h"
  23. #include "rom/ets_sys.h"
  24. #include "rom/uart.h"
  25. #include "rom/rtc.h"
  26. #include "soc/soc.h"
  27. #include "soc/rtc.h"
  28. #include "soc/rtc_cntl_reg.h"
  29. #include "soc/dport_reg.h"
  30. #include "soc/i2s_reg.h"
  31. #include "driver/periph_ctrl.h"
  32. #include "xtensa/core-macros.h"
  33. /* Number of cycles to wait from the 32k XTAL oscillator to consider it running.
  34. * Larger values increase startup delay. Smaller values may cause false positive
  35. * detection (i.e. oscillator runs for a few cycles and then stops).
  36. */
  37. #define XTAL_32K_DETECT_CYCLES 32
  38. #define SLOW_CLK_CAL_CYCLES CONFIG_ESP32_RTC_CLK_CAL_CYCLES
  39. #define MHZ (1000000)
  40. static void select_rtc_slow_clk(rtc_slow_freq_t slow_clk);
  41. // g_ticks_us defined in ROMs for PRO and APP CPU
  42. extern uint32_t g_ticks_per_us_pro;
  43. extern uint32_t g_ticks_per_us_app;
  44. static const char* TAG = "clk";
  45. void esp_clk_init(void)
  46. {
  47. rtc_config_t cfg = RTC_CONFIG_DEFAULT();
  48. rtc_init(cfg);
  49. rtc_clk_fast_freq_set(RTC_FAST_FREQ_8M);
  50. #ifdef CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL
  51. select_rtc_slow_clk(RTC_SLOW_FREQ_32K_XTAL);
  52. #else
  53. select_rtc_slow_clk(RTC_SLOW_FREQ_RTC);
  54. #endif
  55. uint32_t freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
  56. rtc_cpu_freq_t freq = RTC_CPU_FREQ_80M;
  57. switch(freq_mhz) {
  58. case 240:
  59. freq = RTC_CPU_FREQ_240M;
  60. break;
  61. case 160:
  62. freq = RTC_CPU_FREQ_160M;
  63. break;
  64. default:
  65. freq_mhz = 80;
  66. /* no break */
  67. case 80:
  68. freq = RTC_CPU_FREQ_80M;
  69. break;
  70. }
  71. // Wait for UART TX to finish, otherwise some UART output will be lost
  72. // when switching APB frequency
  73. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  74. uint32_t freq_before = rtc_clk_cpu_freq_value(rtc_clk_cpu_freq_get()) / MHZ ;
  75. rtc_clk_cpu_freq_set(freq);
  76. // Re calculate the ccount to make time calculation correct.
  77. uint32_t freq_after = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
  78. XTHAL_SET_CCOUNT( XTHAL_GET_CCOUNT() * freq_after / freq_before );
  79. }
  80. int IRAM_ATTR esp_clk_cpu_freq(void)
  81. {
  82. return g_ticks_per_us_pro * 1000000;
  83. }
  84. int IRAM_ATTR esp_clk_apb_freq(void)
  85. {
  86. return MIN(g_ticks_per_us_pro, 80) * 1000000;
  87. }
  88. void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
  89. {
  90. /* Update scale factors used by ets_delay_us */
  91. g_ticks_per_us_pro = ticks_per_us;
  92. g_ticks_per_us_app = ticks_per_us;
  93. }
  94. static void select_rtc_slow_clk(rtc_slow_freq_t slow_clk)
  95. {
  96. if (slow_clk == RTC_SLOW_FREQ_32K_XTAL) {
  97. /* 32k XTAL oscillator needs to be enabled and running before it can
  98. * be used. Hardware doesn't have a direct way of checking if the
  99. * oscillator is running. Here we use rtc_clk_cal function to count
  100. * the number of main XTAL cycles in the given number of 32k XTAL
  101. * oscillator cycles. If the 32k XTAL has not started up, calibration
  102. * will time out, returning 0.
  103. */
  104. rtc_clk_32k_enable(true);
  105. uint32_t cal_val = 0;
  106. uint32_t wait = 0;
  107. // increment of 'wait' counter equivalent to 3 seconds
  108. const uint32_t warning_timeout = 3 /* sec */ * 32768 /* Hz */ / (2 * XTAL_32K_DETECT_CYCLES);
  109. ESP_EARLY_LOGD(TAG, "waiting for 32k oscillator to start up")
  110. do {
  111. ++wait;
  112. cal_val = rtc_clk_cal(RTC_CAL_32K_XTAL, XTAL_32K_DETECT_CYCLES);
  113. if (wait % warning_timeout == 0) {
  114. ESP_EARLY_LOGW(TAG, "still waiting for 32k oscillator to start up");
  115. }
  116. } while (cal_val == 0);
  117. ESP_EARLY_LOGD(TAG, "32k oscillator ready, wait=%d", wait);
  118. }
  119. rtc_clk_slow_freq_set(slow_clk);
  120. uint32_t cal_val;
  121. if (SLOW_CLK_CAL_CYCLES > 0) {
  122. /* TODO: 32k XTAL oscillator has some frequency drift at startup.
  123. * Improve calibration routine to wait until the frequency is stable.
  124. */
  125. cal_val = rtc_clk_cal(RTC_CAL_RTC_MUX, SLOW_CLK_CAL_CYCLES);
  126. } else {
  127. const uint64_t cal_dividend = (1ULL << RTC_CLK_CAL_FRACT) * 1000000ULL;
  128. cal_val = (uint32_t) (cal_dividend / rtc_clk_slow_freq_get_hz());
  129. }
  130. ESP_EARLY_LOGD(TAG, "RTC_SLOW_CLK calibration value: %d", cal_val);
  131. esp_clk_slowclk_cal_set(cal_val);
  132. }
  133. /* This function is not exposed as an API at this point.
  134. * All peripheral clocks are default enabled after chip is powered on.
  135. * This function disables some peripheral clocks when cpu starts.
  136. * These peripheral clocks are enabled when the peripherals are initialized
  137. * and disabled when they are de-initialized.
  138. */
  139. void esp_perip_clk_init(void)
  140. {
  141. uint32_t common_perip_clk, hwcrypto_perip_clk, wifi_bt_sdio_clk = 0;
  142. #if CONFIG_FREERTOS_UNICORE
  143. RESET_REASON rst_reas[1];
  144. #else
  145. RESET_REASON rst_reas[2];
  146. #endif
  147. rst_reas[0] = rtc_get_reset_reason(0);
  148. #if !CONFIG_FREERTOS_UNICORE
  149. rst_reas[1] = rtc_get_reset_reason(1);
  150. #endif
  151. /* For reason that only reset CPU, do not disable the clocks
  152. * that have been enabled before reset.
  153. */
  154. if ((rst_reas[0] >= TGWDT_CPU_RESET && rst_reas[0] <= RTCWDT_CPU_RESET)
  155. #if !CONFIG_FREERTOS_UNICORE
  156. || (rst_reas[1] >= TGWDT_CPU_RESET && rst_reas[1] <= RTCWDT_CPU_RESET)
  157. #endif
  158. ) {
  159. common_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERIP_CLK_EN_REG);
  160. hwcrypto_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERI_CLK_EN_REG);
  161. wifi_bt_sdio_clk = ~DPORT_READ_PERI_REG(DPORT_WIFI_CLK_EN_REG);
  162. }
  163. else {
  164. common_perip_clk = DPORT_WDG_CLK_EN |
  165. DPORT_I2S0_CLK_EN |
  166. #if CONFIG_CONSOLE_UART_NUM != 0
  167. DPORT_UART0_CLK_EN |
  168. #endif
  169. #if CONFIG_CONSOLE_UART_NUM != 1
  170. DPORT_UART1_CLK_EN |
  171. #endif
  172. #if CONFIG_CONSOLE_UART_NUM != 2
  173. DPORT_UART2_CLK_EN |
  174. #endif
  175. DPORT_SPI_CLK_EN |
  176. DPORT_I2C_EXT0_CLK_EN |
  177. DPORT_UHCI0_CLK_EN |
  178. DPORT_RMT_CLK_EN |
  179. DPORT_PCNT_CLK_EN |
  180. DPORT_LEDC_CLK_EN |
  181. DPORT_UHCI1_CLK_EN |
  182. DPORT_TIMERGROUP1_CLK_EN |
  183. //80MHz SPIRAM uses SPI2 as well; it's initialized before this is called. Do not disable the clock for that if this is enabled.
  184. #if !CONFIG_SPIRAM_SPEED_80M
  185. DPORT_SPI_CLK_EN_2 |
  186. #endif
  187. DPORT_PWM0_CLK_EN |
  188. DPORT_I2C_EXT1_CLK_EN |
  189. DPORT_CAN_CLK_EN |
  190. DPORT_PWM1_CLK_EN |
  191. DPORT_I2S1_CLK_EN |
  192. DPORT_SPI_DMA_CLK_EN |
  193. DPORT_PWM2_CLK_EN |
  194. DPORT_PWM3_CLK_EN;
  195. hwcrypto_perip_clk = DPORT_PERI_EN_AES |
  196. DPORT_PERI_EN_SHA |
  197. DPORT_PERI_EN_RSA |
  198. DPORT_PERI_EN_SECUREBOOT;
  199. wifi_bt_sdio_clk = DPORT_WIFI_CLK_WIFI_EN |
  200. DPORT_WIFI_CLK_BT_EN_M |
  201. DPORT_WIFI_CLK_UNUSED_BIT5 |
  202. DPORT_WIFI_CLK_UNUSED_BIT12 |
  203. DPORT_WIFI_CLK_SDIOSLAVE_EN |
  204. DPORT_WIFI_CLK_SDIO_HOST_EN |
  205. DPORT_WIFI_CLK_EMAC_EN;
  206. }
  207. /* Change I2S clock to audio PLL first. Because if I2S uses 160MHz clock,
  208. * the current is not reduced when disable I2S clock.
  209. */
  210. DPORT_SET_PERI_REG_MASK(I2S_CLKM_CONF_REG(0), I2S_CLKA_ENA);
  211. DPORT_SET_PERI_REG_MASK(I2S_CLKM_CONF_REG(1), I2S_CLKA_ENA);
  212. /* Disable some peripheral clocks. */
  213. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, common_perip_clk);
  214. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, common_perip_clk);
  215. /* Disable hardware crypto clocks. */
  216. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERI_CLK_EN_REG, hwcrypto_perip_clk);
  217. DPORT_SET_PERI_REG_MASK(DPORT_PERI_RST_EN_REG, hwcrypto_perip_clk);
  218. /* Disable WiFi/BT/SDIO clocks. */
  219. DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, wifi_bt_sdio_clk);
  220. /* Enable RNG clock. */
  221. periph_module_enable(PERIPH_RNG_MODULE);
  222. }