clk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "esp32s2/clk.h"
  22. #include "esp_clk_internal.h"
  23. #include "esp32s2/rom/ets_sys.h"
  24. #include "esp32s2/rom/uart.h"
  25. #include "esp32s2/rom/rtc.h"
  26. #include "soc/system_reg.h"
  27. #include "soc/dport_access.h"
  28. #include "soc/soc.h"
  29. #include "soc/rtc.h"
  30. #include "soc/rtc_periph.h"
  31. #include "soc/i2s_reg.h"
  32. #include "hal/wdt_hal.h"
  33. #include "driver/periph_ctrl.h"
  34. #include "xtensa/core-macros.h"
  35. #include "bootloader_clock.h"
  36. #include "soc/syscon_reg.h"
  37. /* Number of cycles to wait from the 32k XTAL oscillator to consider it running.
  38. * Larger values increase startup delay. Smaller values may cause false positive
  39. * detection (i.e. oscillator runs for a few cycles and then stops).
  40. */
  41. #define SLOW_CLK_CAL_CYCLES CONFIG_ESP32S2_RTC_CLK_CAL_CYCLES
  42. #ifdef CONFIG_ESP32S2_RTC_XTAL_CAL_RETRY
  43. #define RTC_XTAL_CAL_RETRY CONFIG_ESP32S2_RTC_XTAL_CAL_RETRY
  44. #else
  45. #define RTC_XTAL_CAL_RETRY 1
  46. #endif
  47. #define MHZ (1000000)
  48. /* Lower threshold for a reasonably-looking calibration value for a 32k XTAL.
  49. * The ideal value (assuming 32768 Hz frequency) is 1000000/32768*(2**19) = 16*10^6.
  50. */
  51. #define MIN_32K_XTAL_CAL_VAL 15000000L
  52. /* Indicates that this 32k oscillator gets input from external oscillator, rather
  53. * than a crystal.
  54. */
  55. #define EXT_OSC_FLAG BIT(3)
  56. /* This is almost the same as rtc_slow_freq_t, except that we define
  57. * an extra enum member for the external 32k oscillator.
  58. * For convenience, lower 2 bits should correspond to rtc_slow_freq_t values.
  59. */
  60. typedef enum {
  61. SLOW_CLK_RTC = RTC_SLOW_FREQ_RTC, //!< Internal 90 kHz RC oscillator
  62. SLOW_CLK_32K_XTAL = RTC_SLOW_FREQ_32K_XTAL, //!< External 32 kHz XTAL
  63. SLOW_CLK_8MD256 = RTC_SLOW_FREQ_8MD256, //!< Internal 8 MHz RC oscillator, divided by 256
  64. SLOW_CLK_32K_EXT_OSC = RTC_SLOW_FREQ_32K_XTAL | EXT_OSC_FLAG //!< External 32k oscillator connected to 32K_XP pin
  65. } slow_clk_sel_t;
  66. static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
  67. static const char *TAG = "clk";
  68. void esp_clk_init(void)
  69. {
  70. rtc_config_t cfg = RTC_CONFIG_DEFAULT();
  71. RESET_REASON rst_reas;
  72. rst_reas = rtc_get_reset_reason(0);
  73. if (rst_reas == POWERON_RESET) {
  74. cfg.cali_ocode = 1;
  75. }
  76. rtc_init(cfg);
  77. assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M);
  78. rtc_clk_fast_freq_set(RTC_FAST_FREQ_8M);
  79. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  80. // WDT uses a SLOW_CLK clock source. After a function select_rtc_slow_clk a frequency of this source can changed.
  81. // If the frequency changes from 90kHz to 32kHz, then the timeout set for the WDT will increase 2.8 times.
  82. // Therefore, for the time of frequency change, set a new lower timeout value (1.6 sec).
  83. // This prevents excessive delay before resetting in case the supply voltage is drawdown.
  84. // (If frequency is changed from 90kHz to 32kHz then WDT timeout will increased to 1.6sec * 90/32 = 4.5 sec).
  85. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  86. uint32_t stage_timeout_ticks = (uint32_t)(1600ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  87. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  88. wdt_hal_feed(&rtc_wdt_ctx);
  89. //Bootloader has enabled RTC WDT until now. We're only modifying timeout, so keep the stage and timeout action the same
  90. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  91. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  92. #endif
  93. #if defined(CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS)
  94. select_rtc_slow_clk(SLOW_CLK_32K_XTAL);
  95. #elif defined(CONFIG_ESP32S2_RTC_CLK_SRC_EXT_OSC)
  96. select_rtc_slow_clk(SLOW_CLK_32K_EXT_OSC);
  97. #elif defined(CONFIG_ESP32S2_RTC_CLK_SRC_INT_8MD256)
  98. select_rtc_slow_clk(SLOW_CLK_8MD256);
  99. #else
  100. select_rtc_slow_clk(RTC_SLOW_FREQ_RTC);
  101. #endif
  102. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  103. // After changing a frequency WDT timeout needs to be set for new frequency.
  104. stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000ULL);
  105. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  106. wdt_hal_feed(&rtc_wdt_ctx);
  107. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  108. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  109. #endif
  110. rtc_cpu_freq_config_t old_config, new_config;
  111. rtc_clk_cpu_freq_get_config(&old_config);
  112. const uint32_t old_freq_mhz = old_config.freq_mhz;
  113. const uint32_t new_freq_mhz = CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ;
  114. bool res = rtc_clk_cpu_freq_mhz_to_config(new_freq_mhz, &new_config);
  115. assert(res);
  116. // Wait for UART TX to finish, otherwise some UART output will be lost
  117. // when switching APB frequency
  118. uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  119. rtc_clk_cpu_freq_set_config(&new_config);
  120. // Re calculate the ccount to make time calculation correct.
  121. XTHAL_SET_CCOUNT( (uint64_t)XTHAL_GET_CCOUNT() * new_freq_mhz / old_freq_mhz );
  122. }
  123. int IRAM_ATTR esp_clk_cpu_freq(void)
  124. {
  125. return ets_get_cpu_frequency() * 1000000;
  126. }
  127. int IRAM_ATTR esp_clk_apb_freq(void)
  128. {
  129. return MIN(ets_get_cpu_frequency(), 80) * 1000000;
  130. }
  131. static void select_rtc_slow_clk(slow_clk_sel_t slow_clk)
  132. {
  133. rtc_slow_freq_t rtc_slow_freq = slow_clk & RTC_CNTL_ANA_CLK_RTC_SEL_V;
  134. uint32_t cal_val = 0;
  135. /* number of times to repeat 32k XTAL calibration
  136. * before giving up and switching to the internal RC
  137. */
  138. int retry_32k_xtal = RTC_XTAL_CAL_RETRY;
  139. do {
  140. if (rtc_slow_freq == RTC_SLOW_FREQ_32K_XTAL) {
  141. /* 32k XTAL oscillator needs to be enabled and running before it can
  142. * be used. Hardware doesn't have a direct way of checking if the
  143. * oscillator is running. Here we use rtc_clk_cal function to count
  144. * the number of main XTAL cycles in the given number of 32k XTAL
  145. * oscillator cycles. If the 32k XTAL has not started up, calibration
  146. * will time out, returning 0.
  147. */
  148. ESP_EARLY_LOGD(TAG, "waiting for 32k oscillator to start up");
  149. if (slow_clk == SLOW_CLK_32K_XTAL) {
  150. rtc_clk_32k_enable(true);
  151. } else if (slow_clk == SLOW_CLK_32K_EXT_OSC) {
  152. rtc_clk_32k_enable_external();
  153. }
  154. // When SLOW_CLK_CAL_CYCLES is set to 0, clock calibration will not be performed at startup.
  155. if (SLOW_CLK_CAL_CYCLES > 0) {
  156. cal_val = rtc_clk_cal(RTC_CAL_32K_XTAL, SLOW_CLK_CAL_CYCLES);
  157. if (cal_val == 0 || cal_val < MIN_32K_XTAL_CAL_VAL) {
  158. if (retry_32k_xtal-- > 0) {
  159. continue;
  160. }
  161. ESP_EARLY_LOGW(TAG, "32 kHz XTAL not found, switching to internal 90 kHz oscillator");
  162. rtc_slow_freq = RTC_SLOW_FREQ_RTC;
  163. }
  164. }
  165. } else if (rtc_slow_freq == RTC_SLOW_FREQ_8MD256) {
  166. rtc_clk_8m_enable(true, true);
  167. }
  168. rtc_clk_slow_freq_set(rtc_slow_freq);
  169. if (SLOW_CLK_CAL_CYCLES > 0) {
  170. /* TODO: 32k XTAL oscillator has some frequency drift at startup.
  171. * Improve calibration routine to wait until the frequency is stable.
  172. */
  173. cal_val = rtc_clk_cal(RTC_CAL_RTC_MUX, SLOW_CLK_CAL_CYCLES);
  174. } else {
  175. const uint64_t cal_dividend = (1ULL << RTC_CLK_CAL_FRACT) * 1000000ULL;
  176. cal_val = (uint32_t) (cal_dividend / rtc_clk_slow_freq_get_hz());
  177. }
  178. } while (cal_val == 0);
  179. ESP_EARLY_LOGD(TAG, "RTC_SLOW_CLK calibration value: %d", cal_val);
  180. esp_clk_slowclk_cal_set(cal_val);
  181. }
  182. void rtc_clk_select_rtc_slow_clk(void)
  183. {
  184. select_rtc_slow_clk(RTC_SLOW_FREQ_32K_XTAL);
  185. }
  186. /* This function is not exposed as an API at this point.
  187. * All peripheral clocks are default enabled after chip is powered on.
  188. * This function disables some peripheral clocks when cpu starts.
  189. * These peripheral clocks are enabled when the peripherals are initialized
  190. * and disabled when they are de-initialized.
  191. */
  192. void esp_perip_clk_init(void)
  193. {
  194. uint32_t common_perip_clk, hwcrypto_perip_clk, wifi_bt_sdio_clk = 0;
  195. uint32_t common_perip_clk1 = 0;
  196. RESET_REASON rst_reas[1];
  197. rst_reas[0] = rtc_get_reset_reason(0);
  198. /* For reason that only reset CPU, do not disable the clocks
  199. * that have been enabled before reset.
  200. */
  201. if (rst_reas[0] >= TG0WDT_CPU_RESET &&
  202. rst_reas[0] <= TG0WDT_CPU_RESET &&
  203. rst_reas[0] != RTCWDT_BROWN_OUT_RESET) {
  204. common_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERIP_CLK_EN_REG);
  205. hwcrypto_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERIP_CLK_EN1_REG);
  206. wifi_bt_sdio_clk = ~DPORT_READ_PERI_REG(DPORT_WIFI_CLK_EN_REG);
  207. } else {
  208. common_perip_clk = DPORT_WDG_CLK_EN |
  209. DPORT_I2S0_CLK_EN |
  210. #if CONFIG_ESP_CONSOLE_UART_NUM != 0
  211. DPORT_UART_CLK_EN |
  212. #endif
  213. #if CONFIG_ESP_CONSOLE_UART_NUM != 1
  214. DPORT_UART1_CLK_EN |
  215. #endif
  216. DPORT_USB_CLK_EN |
  217. DPORT_SPI2_CLK_EN |
  218. DPORT_I2C_EXT0_CLK_EN |
  219. DPORT_UHCI0_CLK_EN |
  220. DPORT_RMT_CLK_EN |
  221. DPORT_PCNT_CLK_EN |
  222. DPORT_LEDC_CLK_EN |
  223. DPORT_TIMERGROUP1_CLK_EN |
  224. DPORT_SPI3_CLK_EN |
  225. DPORT_SPI4_CLK_EN |
  226. DPORT_PWM0_CLK_EN |
  227. DPORT_TWAI_CLK_EN |
  228. DPORT_PWM1_CLK_EN |
  229. DPORT_I2S1_CLK_EN |
  230. DPORT_SPI2_DMA_CLK_EN |
  231. DPORT_SPI3_DMA_CLK_EN |
  232. DPORT_PWM2_CLK_EN |
  233. DPORT_PWM3_CLK_EN;
  234. common_perip_clk1 = 0;
  235. hwcrypto_perip_clk = DPORT_CRYPTO_AES_CLK_EN |
  236. DPORT_CRYPTO_SHA_CLK_EN |
  237. DPORT_CRYPTO_RSA_CLK_EN;
  238. wifi_bt_sdio_clk = DPORT_WIFI_CLK_WIFI_EN |
  239. DPORT_WIFI_CLK_BT_EN_M |
  240. DPORT_WIFI_CLK_UNUSED_BIT5 |
  241. DPORT_WIFI_CLK_UNUSED_BIT12 |
  242. DPORT_WIFI_CLK_SDIOSLAVE_EN |
  243. DPORT_WIFI_CLK_SDIO_HOST_EN |
  244. DPORT_WIFI_CLK_EMAC_EN;
  245. }
  246. //Reset the communication peripherals like I2C, SPI, UART, I2S and bring them to known state.
  247. common_perip_clk |= DPORT_I2S0_CLK_EN |
  248. #if CONFIG_ESP_CONSOLE_UART_NUM != 0
  249. DPORT_UART_CLK_EN |
  250. #endif
  251. #if CONFIG_ESP_CONSOLE_UART_NUM != 1
  252. DPORT_UART1_CLK_EN |
  253. #endif
  254. DPORT_USB_CLK_EN |
  255. DPORT_SPI2_CLK_EN |
  256. DPORT_I2C_EXT0_CLK_EN |
  257. DPORT_UHCI0_CLK_EN |
  258. DPORT_RMT_CLK_EN |
  259. DPORT_UHCI1_CLK_EN |
  260. DPORT_SPI3_CLK_EN |
  261. DPORT_SPI4_CLK_EN |
  262. DPORT_I2C_EXT1_CLK_EN |
  263. DPORT_I2S1_CLK_EN |
  264. DPORT_SPI2_DMA_CLK_EN |
  265. DPORT_SPI3_DMA_CLK_EN;
  266. common_perip_clk1 = 0;
  267. /* Change I2S clock to audio PLL first. Because if I2S uses 160MHz clock,
  268. * the current is not reduced when disable I2S clock.
  269. */
  270. REG_SET_FIELD(I2S_CLKM_CONF_REG(0), I2S_CLK_SEL, I2S_CLK_AUDIO_PLL);
  271. REG_SET_FIELD(I2S_CLKM_CONF_REG(1), I2S_CLK_SEL, I2S_CLK_AUDIO_PLL);
  272. /* Disable some peripheral clocks. */
  273. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, common_perip_clk);
  274. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, common_perip_clk);
  275. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN1_REG, common_perip_clk1);
  276. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN1_REG, common_perip_clk1);
  277. /* Disable hardware crypto clocks. */
  278. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN1_REG, hwcrypto_perip_clk);
  279. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN1_REG, hwcrypto_perip_clk);
  280. /* Disable WiFi/BT/SDIO clocks. */
  281. DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, wifi_bt_sdio_clk);
  282. /* Enable WiFi MAC and POWER clocks */
  283. DPORT_SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_EN);
  284. /* Set WiFi light sleep clock source to RTC slow clock */
  285. DPORT_REG_SET_FIELD(DPORT_BT_LPCK_DIV_INT_REG, DPORT_BT_LPCK_DIV_NUM, 0);
  286. DPORT_CLEAR_PERI_REG_MASK(DPORT_BT_LPCK_DIV_FRAC_REG, DPORT_LPCLK_SEL_8M);
  287. DPORT_SET_PERI_REG_MASK(DPORT_BT_LPCK_DIV_FRAC_REG, DPORT_LPCLK_SEL_RTC_SLOW);
  288. /* Enable RNG clock. */
  289. periph_module_enable(PERIPH_RNG_MODULE);
  290. }