clk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_wdt.h"
  29. #include "soc/rtc_cntl_reg.h"
  30. #include "soc/i2s_reg.h"
  31. #include "driver/periph_ctrl.h"
  32. #include "xtensa/core-macros.h"
  33. #include "bootloader_clock.h"
  34. #include "driver/spi_common.h"
  35. /* Number of cycles to wait from the 32k XTAL oscillator to consider it running.
  36. * Larger values increase startup delay. Smaller values may cause false positive
  37. * detection (i.e. oscillator runs for a few cycles and then stops).
  38. */
  39. #define SLOW_CLK_CAL_CYCLES CONFIG_ESP32_RTC_CLK_CAL_CYCLES
  40. #define MHZ (1000000)
  41. /* Indicates that this 32k oscillator gets input from external oscillator, rather
  42. * than a crystal.
  43. */
  44. #define EXT_OSC_FLAG BIT(3)
  45. /* This is almost the same as rtc_slow_freq_t, except that we define
  46. * an extra enum member for the external 32k oscillator.
  47. * For convenience, lower 2 bits should correspond to rtc_slow_freq_t values.
  48. */
  49. typedef enum {
  50. SLOW_CLK_150K = RTC_SLOW_FREQ_RTC, //!< Internal 150 kHz RC oscillator
  51. SLOW_CLK_32K_XTAL = RTC_SLOW_FREQ_32K_XTAL, //!< External 32 kHz XTAL
  52. SLOW_CLK_8MD256 = RTC_SLOW_FREQ_8MD256, //!< Internal 8 MHz RC oscillator, divided by 256
  53. SLOW_CLK_32K_EXT_OSC = RTC_SLOW_FREQ_32K_XTAL | EXT_OSC_FLAG //!< External 32k oscillator connected to 32K_XP pin
  54. } slow_clk_sel_t;
  55. static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
  56. // g_ticks_us defined in ROMs for PRO and APP CPU
  57. extern uint32_t g_ticks_per_us_pro;
  58. #ifndef CONFIG_FREERTOS_UNICORE
  59. extern uint32_t g_ticks_per_us_app;
  60. #endif
  61. static const char* TAG = "clk";
  62. void esp_clk_init(void)
  63. {
  64. rtc_config_t cfg = RTC_CONFIG_DEFAULT();
  65. rtc_init(cfg);
  66. #ifdef CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS
  67. /* Check the bootloader set the XTAL frequency.
  68. Bootloaders pre-v2.1 don't do this.
  69. */
  70. rtc_xtal_freq_t xtal_freq = rtc_clk_xtal_freq_get();
  71. if (xtal_freq == RTC_XTAL_FREQ_AUTO) {
  72. ESP_EARLY_LOGW(TAG, "RTC domain not initialised by bootloader");
  73. bootloader_clock_configure();
  74. }
  75. #else
  76. /* If this assertion fails, either upgrade the bootloader or enable CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS */
  77. assert(rtc_clk_xtal_freq_get() != RTC_XTAL_FREQ_AUTO);
  78. #endif
  79. rtc_clk_fast_freq_set(RTC_FAST_FREQ_8M);
  80. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  81. // WDT uses a SLOW_CLK clock source. After a function select_rtc_slow_clk a frequency of this source can changed.
  82. // If the frequency changes from 150kHz to 32kHz, then the timeout set for the WDT will increase 4.6 times.
  83. // Therefore, for the time of frequency change, set a new lower timeout value (1.6 sec).
  84. // This prevents excessive delay before resetting in case the supply voltage is drawdown.
  85. // (If frequency is changed from 150kHz to 32kHz then WDT timeout will increased to 1.6sec * 150/32 = 7.5 sec).
  86. rtc_wdt_protect_off();
  87. rtc_wdt_feed();
  88. rtc_wdt_set_time(RTC_WDT_STAGE0, 1600);
  89. rtc_wdt_protect_on();
  90. #endif
  91. #if defined(CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL)
  92. select_rtc_slow_clk(SLOW_CLK_32K_XTAL);
  93. #elif defined(CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_OSC)
  94. select_rtc_slow_clk(SLOW_CLK_32K_EXT_OSC);
  95. #elif defined(CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_8MD256)
  96. select_rtc_slow_clk(SLOW_CLK_8MD256);
  97. #else
  98. select_rtc_slow_clk(RTC_SLOW_FREQ_RTC);
  99. #endif
  100. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  101. // After changing a frequency WDT timeout needs to be set for new frequency.
  102. rtc_wdt_protect_off();
  103. rtc_wdt_feed();
  104. rtc_wdt_set_time(RTC_WDT_STAGE0, CONFIG_BOOTLOADER_WDT_TIME_MS);
  105. rtc_wdt_protect_on();
  106. #endif
  107. rtc_cpu_freq_config_t old_config, new_config;
  108. rtc_clk_cpu_freq_get_config(&old_config);
  109. const uint32_t old_freq_mhz = old_config.freq_mhz;
  110. const uint32_t new_freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
  111. bool res = rtc_clk_cpu_freq_mhz_to_config(new_freq_mhz, &new_config);
  112. assert(res);
  113. // Wait for UART TX to finish, otherwise some UART output will be lost
  114. // when switching APB frequency
  115. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  116. rtc_clk_cpu_freq_set_config(&new_config);
  117. // Re calculate the ccount to make time calculation correct.
  118. XTHAL_SET_CCOUNT( XTHAL_GET_CCOUNT() * new_freq_mhz / old_freq_mhz );
  119. }
  120. int IRAM_ATTR esp_clk_cpu_freq(void)
  121. {
  122. return g_ticks_per_us_pro * MHZ;
  123. }
  124. int IRAM_ATTR esp_clk_apb_freq(void)
  125. {
  126. return MIN(g_ticks_per_us_pro, 80) * MHZ;
  127. }
  128. int IRAM_ATTR esp_clk_xtal_freq(void)
  129. {
  130. return rtc_clk_xtal_freq_get() * MHZ;
  131. }
  132. void IRAM_ATTR ets_update_cpu_frequency(uint32_t ticks_per_us)
  133. {
  134. /* Update scale factors used by ets_delay_us */
  135. g_ticks_per_us_pro = ticks_per_us;
  136. #ifndef CONFIG_FREERTOS_UNICORE
  137. g_ticks_per_us_app = ticks_per_us;
  138. #endif
  139. }
  140. static void select_rtc_slow_clk(slow_clk_sel_t slow_clk)
  141. {
  142. rtc_slow_freq_t rtc_slow_freq = slow_clk & RTC_CNTL_ANA_CLK_RTC_SEL_V;
  143. uint32_t cal_val = 0;
  144. do {
  145. if (rtc_slow_freq == RTC_SLOW_FREQ_32K_XTAL) {
  146. /* 32k XTAL oscillator needs to be enabled and running before it can
  147. * be used. Hardware doesn't have a direct way of checking if the
  148. * oscillator is running. Here we use rtc_clk_cal function to count
  149. * the number of main XTAL cycles in the given number of 32k XTAL
  150. * oscillator cycles. If the 32k XTAL has not started up, calibration
  151. * will time out, returning 0.
  152. */
  153. ESP_EARLY_LOGD(TAG, "waiting for 32k oscillator to start up");
  154. if (slow_clk == SLOW_CLK_32K_XTAL) {
  155. rtc_clk_32k_enable(true);
  156. } else if (slow_clk == SLOW_CLK_32K_EXT_OSC) {
  157. rtc_clk_32k_enable_external();
  158. }
  159. // When SLOW_CLK_CAL_CYCLES is set to 0, clock calibration will not be performed at startup.
  160. if (SLOW_CLK_CAL_CYCLES > 0) {
  161. cal_val = rtc_clk_cal(RTC_CAL_32K_XTAL, SLOW_CLK_CAL_CYCLES);
  162. if (cal_val == 0 || cal_val < 15000000L) {
  163. ESP_EARLY_LOGW(TAG, "32 kHz XTAL not found, switching to internal 150 kHz oscillator");
  164. rtc_slow_freq = RTC_SLOW_FREQ_RTC;
  165. }
  166. }
  167. } else if (rtc_slow_freq == RTC_SLOW_FREQ_8MD256) {
  168. rtc_clk_8m_enable(true, true);
  169. }
  170. rtc_clk_slow_freq_set(rtc_slow_freq);
  171. if (SLOW_CLK_CAL_CYCLES > 0) {
  172. /* TODO: 32k XTAL oscillator has some frequency drift at startup.
  173. * Improve calibration routine to wait until the frequency is stable.
  174. */
  175. cal_val = rtc_clk_cal(RTC_CAL_RTC_MUX, SLOW_CLK_CAL_CYCLES);
  176. } else {
  177. const uint64_t cal_dividend = (1ULL << RTC_CLK_CAL_FRACT) * 1000000ULL;
  178. cal_val = (uint32_t) (cal_dividend / rtc_clk_slow_freq_get_hz());
  179. }
  180. } while (cal_val == 0);
  181. ESP_EARLY_LOGD(TAG, "RTC_SLOW_CLK calibration value: %d", cal_val);
  182. esp_clk_slowclk_cal_set(cal_val);
  183. }
  184. void rtc_clk_select_rtc_slow_clk()
  185. {
  186. select_rtc_slow_clk(RTC_SLOW_FREQ_32K_XTAL);
  187. }
  188. /* This function is not exposed as an API at this point.
  189. * All peripheral clocks are default enabled after chip is powered on.
  190. * This function disables some peripheral clocks when cpu starts.
  191. * These peripheral clocks are enabled when the peripherals are initialized
  192. * and disabled when they are de-initialized.
  193. */
  194. void esp_perip_clk_init(void)
  195. {
  196. uint32_t common_perip_clk, hwcrypto_perip_clk, wifi_bt_sdio_clk = 0;
  197. #if CONFIG_FREERTOS_UNICORE
  198. RESET_REASON rst_reas[1];
  199. #else
  200. RESET_REASON rst_reas[2];
  201. #endif
  202. rst_reas[0] = rtc_get_reset_reason(0);
  203. #if !CONFIG_FREERTOS_UNICORE
  204. rst_reas[1] = rtc_get_reset_reason(1);
  205. #endif
  206. /* For reason that only reset CPU, do not disable the clocks
  207. * that have been enabled before reset.
  208. */
  209. if ((rst_reas[0] >= TGWDT_CPU_RESET && rst_reas[0] <= RTCWDT_CPU_RESET)
  210. #if !CONFIG_FREERTOS_UNICORE
  211. || (rst_reas[1] >= TGWDT_CPU_RESET && rst_reas[1] <= RTCWDT_CPU_RESET)
  212. #endif
  213. ) {
  214. common_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERIP_CLK_EN_REG);
  215. hwcrypto_perip_clk = ~DPORT_READ_PERI_REG(DPORT_PERI_CLK_EN_REG);
  216. wifi_bt_sdio_clk = ~DPORT_READ_PERI_REG(DPORT_WIFI_CLK_EN_REG);
  217. }
  218. else {
  219. common_perip_clk = DPORT_WDG_CLK_EN |
  220. DPORT_PCNT_CLK_EN |
  221. DPORT_LEDC_CLK_EN |
  222. DPORT_TIMERGROUP1_CLK_EN |
  223. DPORT_PWM0_CLK_EN |
  224. DPORT_CAN_CLK_EN |
  225. DPORT_PWM1_CLK_EN |
  226. DPORT_PWM2_CLK_EN |
  227. DPORT_PWM3_CLK_EN;
  228. hwcrypto_perip_clk = DPORT_PERI_EN_AES |
  229. DPORT_PERI_EN_SHA |
  230. DPORT_PERI_EN_RSA |
  231. DPORT_PERI_EN_SECUREBOOT;
  232. wifi_bt_sdio_clk = DPORT_WIFI_CLK_WIFI_EN |
  233. DPORT_WIFI_CLK_BT_EN_M |
  234. DPORT_WIFI_CLK_UNUSED_BIT5 |
  235. DPORT_WIFI_CLK_UNUSED_BIT12 |
  236. DPORT_WIFI_CLK_SDIOSLAVE_EN |
  237. DPORT_WIFI_CLK_SDIO_HOST_EN |
  238. DPORT_WIFI_CLK_EMAC_EN;
  239. }
  240. //Reset the communication peripherals like I2C, SPI, UART, I2S and bring them to known state.
  241. common_perip_clk |= DPORT_I2S0_CLK_EN |
  242. #if CONFIG_CONSOLE_UART_NUM != 0
  243. DPORT_UART_CLK_EN |
  244. #endif
  245. #if CONFIG_CONSOLE_UART_NUM != 1
  246. DPORT_UART1_CLK_EN |
  247. #endif
  248. #if CONFIG_CONSOLE_UART_NUM != 2
  249. DPORT_UART2_CLK_EN |
  250. #endif
  251. DPORT_SPI2_CLK_EN |
  252. DPORT_I2C_EXT0_CLK_EN |
  253. DPORT_UHCI0_CLK_EN |
  254. DPORT_RMT_CLK_EN |
  255. DPORT_UHCI1_CLK_EN |
  256. DPORT_SPI3_CLK_EN |
  257. DPORT_I2C_EXT1_CLK_EN |
  258. DPORT_I2S1_CLK_EN |
  259. DPORT_SPI_DMA_CLK_EN;
  260. #if CONFIG_SPIRAM_SPEED_80M
  261. //80MHz SPIRAM uses SPI2/SPI3 as well; it's initialized before this is called. Because it is used in
  262. //a weird mode where clock to the peripheral is disabled but reset is also disabled, it 'hangs'
  263. //in a state where it outputs a continuous 80MHz signal. Mask its bit here because we should
  264. //not modify that state, regardless of what we calculated earlier.
  265. if (spicommon_periph_in_use(HSPI_HOST)) {
  266. common_perip_clk &= ~DPORT_SPI2_CLK_EN;
  267. }
  268. if (spicommon_periph_in_use(VSPI_HOST)) {
  269. common_perip_clk &= ~DPORT_SPI3_CLK_EN;
  270. }
  271. #endif
  272. /* Change I2S clock to audio PLL first. Because if I2S uses 160MHz clock,
  273. * the current is not reduced when disable I2S clock.
  274. */
  275. DPORT_SET_PERI_REG_MASK(I2S_CLKM_CONF_REG(0), I2S_CLKA_ENA);
  276. DPORT_SET_PERI_REG_MASK(I2S_CLKM_CONF_REG(1), I2S_CLKA_ENA);
  277. /* Disable some peripheral clocks. */
  278. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, common_perip_clk);
  279. DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, common_perip_clk);
  280. /* Disable hardware crypto clocks. */
  281. DPORT_CLEAR_PERI_REG_MASK(DPORT_PERI_CLK_EN_REG, hwcrypto_perip_clk);
  282. DPORT_SET_PERI_REG_MASK(DPORT_PERI_RST_EN_REG, hwcrypto_perip_clk);
  283. /* Disable WiFi/BT/SDIO clocks. */
  284. DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, wifi_bt_sdio_clk);
  285. /* Enable RNG clock. */
  286. periph_module_enable(PERIPH_RNG_MODULE);
  287. }