clk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/i2s_reg.h"
  30. #include "driver/periph_ctrl.h"
  31. #include "xtensa/core-macros.h"
  32. #include "bootloader_clock.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. #ifdef CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS
  50. /* Check the bootloader set the XTAL frequency.
  51. Bootloaders pre-v2.1 don't do this.
  52. */
  53. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  54. if (xtal_freq == RTC_XTAL_FREQ_AUTO) {
  55. ESP_EARLY_LOGW(TAG, "RTC domain not initialised by bootloader");
  56. bootloader_clock_configure();
  57. }
  58. #else
  59. /* If this assertion fails, either upgrade the bootloader or enable CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS */
  60. assert(rtc_clk_xtal_freq_get() != RTC_XTAL_FREQ_AUTO);
  61. #endif
  62. rtc_clk_fast_freq_set(RTC_FAST_FREQ_8M);
  63. #ifdef CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL
  64. select_rtc_slow_clk(RTC_SLOW_FREQ_32K_XTAL);
  65. #else
  66. select_rtc_slow_clk(RTC_SLOW_FREQ_RTC);
  67. #endif
  68. uint32_t freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
  69. rtc_cpu_freq_t freq = RTC_CPU_FREQ_80M;
  70. switch(freq_mhz) {
  71. case 240:
  72. freq = RTC_CPU_FREQ_240M;
  73. break;
  74. case 160:
  75. freq = RTC_CPU_FREQ_160M;
  76. break;
  77. default:
  78. freq_mhz = 80;
  79. /* no break */
  80. case 80:
  81. freq = RTC_CPU_FREQ_80M;
  82. break;
  83. }
  84. // Wait for UART TX to finish, otherwise some UART output will be lost
  85. // when switching APB frequency
  86. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  87. uint32_t freq_before = rtc_clk_cpu_freq_value(rtc_clk_cpu_freq_get()) / MHZ ;
  88. rtc_clk_cpu_freq_set(freq);
  89. // Re calculate the ccount to make time calculation correct.
  90. uint32_t freq_after = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
  91. XTHAL_SET_CCOUNT( XTHAL_GET_CCOUNT() * freq_after / freq_before );
  92. }
  93. int IRAM_ATTR esp_clk_cpu_freq(void)
  94. {
  95. return g_ticks_per_us_pro * 1000000;
  96. }
  97. int IRAM_ATTR esp_clk_apb_freq(void)
  98. {
  99. return MIN(g_ticks_per_us_pro, 80) * 1000000;
  100. }
  101. void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
  102. {
  103. /* Update scale factors used by ets_delay_us */
  104. g_ticks_per_us_pro = ticks_per_us;
  105. g_ticks_per_us_app = ticks_per_us;
  106. }
  107. static void select_rtc_slow_clk(rtc_slow_freq_t slow_clk)
  108. {
  109. uint32_t cal_val = 0;
  110. do {
  111. if (slow_clk == RTC_SLOW_FREQ_32K_XTAL) {
  112. /* 32k XTAL oscillator needs to be enabled and running before it can
  113. * be used. Hardware doesn't have a direct way of checking if the
  114. * oscillator is running. Here we use rtc_clk_cal function to count
  115. * the number of main XTAL cycles in the given number of 32k XTAL
  116. * oscillator cycles. If the 32k XTAL has not started up, calibration
  117. * will time out, returning 0.
  118. */
  119. uint32_t wait = 0;
  120. // increment of 'wait' counter equivalent to 3 seconds
  121. const uint32_t warning_timeout = 3 /* sec */ * 32768 /* Hz */ / (2 * XTAL_32K_DETECT_CYCLES);
  122. ESP_EARLY_LOGD(TAG, "waiting for 32k oscillator to start up")
  123. do {
  124. ++wait;
  125. rtc_clk_32k_enable(true);
  126. cal_val = rtc_clk_cal(RTC_CAL_32K_XTAL, XTAL_32K_DETECT_CYCLES);
  127. if (wait % warning_timeout == 0) {
  128. ESP_EARLY_LOGW(TAG, "still waiting for 32k oscillator to start up");
  129. }
  130. if(cal_val == 0){
  131. rtc_clk_32k_enable(false);
  132. rtc_clk_32k_bootstrap(CONFIG_ESP32_RTC_XTAL_BOOTSTRAP_CYCLES);
  133. }
  134. } while (cal_val == 0);
  135. }
  136. rtc_clk_slow_freq_set(slow_clk);
  137. if (SLOW_CLK_CAL_CYCLES > 0) {
  138. /* TODO: 32k XTAL oscillator has some frequency drift at startup.
  139. * Improve calibration routine to wait until the frequency is stable.
  140. */
  141. cal_val = rtc_clk_cal(RTC_CAL_RTC_MUX, SLOW_CLK_CAL_CYCLES);
  142. } else {
  143. const uint64_t cal_dividend = (1ULL << RTC_CLK_CAL_FRACT) * 1000000ULL;
  144. cal_val = (uint32_t) (cal_dividend / rtc_clk_slow_freq_get_hz());
  145. }
  146. } while (cal_val == 0);
  147. ESP_EARLY_LOGD(TAG, "RTC_SLOW_CLK calibration value: %d", cal_val);
  148. esp_clk_slowclk_cal_set(cal_val);
  149. }
  150. void rtc_clk_select_rtc_slow_clk()
  151. {
  152. select_rtc_slow_clk(RTC_SLOW_FREQ_32K_XTAL);
  153. }
  154. /* This function is not exposed as an API at this point.
  155. * All peripheral clocks are default enabled after chip is powered on.
  156. * This function disables some peripheral clocks when cpu starts.
  157. * These peripheral clocks are enabled when the peripherals are initialized
  158. * and disabled when they are de-initialized.
  159. */
  160. void esp_perip_clk_init(void)
  161. {
  162. uint32_t common_perip_clk, hwcrypto_perip_clk, wifi_bt_sdio_clk = 0;
  163. #if CONFIG_FREERTOS_UNICORE
  164. RESET_REASON rst_reas[1];
  165. #else
  166. RESET_REASON rst_reas[2];
  167. #endif
  168. rst_reas[0] = rtc_get_reset_reason(0);
  169. #if !CONFIG_FREERTOS_UNICORE
  170. rst_reas[1] = rtc_get_reset_reason(1);
  171. #endif
  172. /* For reason that only reset CPU, do not disable the clocks
  173. * that have been enabled before reset.
  174. */
  175. if ((rst_reas[0] >= TGWDT_CPU_RESET && rst_reas[0] <= RTCWDT_CPU_RESET)
  176. #if !CONFIG_FREERTOS_UNICORE
  177. || (rst_reas[1] >= TGWDT_CPU_RESET && rst_reas[1] <= RTCWDT_CPU_RESET)
  178. #endif
  179. ) {
  180. common_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERIP_CLK_EN_REG);
  181. hwcrypto_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERI_CLK_EN_REG);
  182. wifi_bt_sdio_clk = ~DPORT_READ_PERI_REG(DPORT_WIFI_CLK_EN_REG);
  183. }
  184. else {
  185. common_perip_clk = DPORT_WDG_CLK_EN |
  186. DPORT_I2S0_CLK_EN |
  187. #if CONFIG_CONSOLE_UART_NUM != 0
  188. DPORT_UART_CLK_EN |
  189. #endif
  190. #if CONFIG_CONSOLE_UART_NUM != 1
  191. DPORT_UART1_CLK_EN |
  192. #endif
  193. #if CONFIG_CONSOLE_UART_NUM != 2
  194. DPORT_UART2_CLK_EN |
  195. #endif
  196. DPORT_SPI_CLK_EN |
  197. DPORT_I2C_EXT0_CLK_EN |
  198. DPORT_UHCI0_CLK_EN |
  199. DPORT_RMT_CLK_EN |
  200. DPORT_PCNT_CLK_EN |
  201. DPORT_LEDC_CLK_EN |
  202. DPORT_UHCI1_CLK_EN |
  203. DPORT_TIMERGROUP1_CLK_EN |
  204. DPORT_SPI_CLK_EN_2 |
  205. DPORT_PWM0_CLK_EN |
  206. DPORT_I2C_EXT1_CLK_EN |
  207. DPORT_CAN_CLK_EN |
  208. DPORT_PWM1_CLK_EN |
  209. DPORT_I2S1_CLK_EN |
  210. DPORT_SPI_DMA_CLK_EN |
  211. DPORT_PWM2_CLK_EN |
  212. DPORT_PWM3_CLK_EN;
  213. hwcrypto_perip_clk = DPORT_PERI_EN_AES |
  214. DPORT_PERI_EN_SHA |
  215. DPORT_PERI_EN_RSA |
  216. DPORT_PERI_EN_SECUREBOOT;
  217. wifi_bt_sdio_clk = DPORT_WIFI_CLK_WIFI_EN |
  218. DPORT_WIFI_CLK_BT_EN_M |
  219. DPORT_WIFI_CLK_UNUSED_BIT5 |
  220. DPORT_WIFI_CLK_UNUSED_BIT12 |
  221. DPORT_WIFI_CLK_SDIOSLAVE_EN |
  222. DPORT_WIFI_CLK_SDIO_HOST_EN |
  223. DPORT_WIFI_CLK_EMAC_EN;
  224. }
  225. #if CONFIG_SPIRAM_SPEED_80M
  226. //80MHz SPIRAM uses SPI2 as well; it's initialized before this is called. Because it is used in
  227. //a weird mode where clock to the peripheral is disabled but reset is also disabled, it 'hangs'
  228. //in a state where it outputs a continuous 80MHz signal. Mask its bit here because we should
  229. //not modify that state, regardless of what we calculated earlier.
  230. common_perip_clk &= ~DPORT_SPI_CLK_EN_2;
  231. #endif
  232. /* Change I2S clock to audio PLL first. Because if I2S uses 160MHz clock,
  233. * the current is not reduced when disable I2S clock.
  234. */
  235. DPORT_SET_PERI_REG_MASK(I2S_CLKM_CONF_REG(0), I2S_CLKA_ENA);
  236. DPORT_SET_PERI_REG_MASK(I2S_CLKM_CONF_REG(1), I2S_CLKA_ENA);
  237. /* Disable some peripheral clocks. */
  238. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, common_perip_clk);
  239. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, common_perip_clk);
  240. /* Disable hardware crypto clocks. */
  241. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERI_CLK_EN_REG, hwcrypto_perip_clk);
  242. DPORT_SET_PERI_REG_MASK(DPORT_PERI_RST_EN_REG, hwcrypto_perip_clk);
  243. /* Disable WiFi/BT/SDIO clocks. */
  244. DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, wifi_bt_sdio_clk);
  245. /* Enable RNG clock. */
  246. periph_module_enable(PERIPH_RNG_MODULE);
  247. }