clk.c 13 KB

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