bootloader_clock_init.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "soc/soc.h"
  8. #include "soc/rtc.h"
  9. #include "hal/efuse_hal.h"
  10. #include "soc/rtc_cntl_reg.h"
  11. #if CONFIG_IDF_TARGET_ESP32
  12. #include "hal/clk_tree_ll.h"
  13. #endif
  14. #include "esp_rom_sys.h"
  15. #include "esp_rom_uart.h"
  16. __attribute__((weak)) void bootloader_clock_configure(void)
  17. {
  18. // ROM bootloader may have put a lot of text into UART0 FIFO.
  19. // Wait for it to be printed.
  20. // This is not needed on power on reset, when ROM bootloader is running at
  21. // 40 MHz. But in case of TG WDT reset, CPU may still be running at >80 MHZ,
  22. // and will be done with the bootloader much earlier than UART FIFO is empty.
  23. esp_rom_uart_tx_wait_idle(0);
  24. /* Set CPU to 80MHz. Keep other clocks unmodified. */
  25. int cpu_freq_mhz = 80;
  26. #if CONFIG_IDF_TARGET_ESP32
  27. /* On ESP32 rev 0, switching to 80/160 MHz if clock was previously set to
  28. * 240 MHz may cause the chip to lock up (see section 3.5 of the errata
  29. * document). For rev. 0, switch to 240 instead if it has been enabled
  30. * previously.
  31. */
  32. if (efuse_hal_get_chip_revision() == 0 &&
  33. clk_ll_cpu_get_freq_mhz_from_pll() == CLK_LL_PLL_240M_FREQ_MHZ) {
  34. cpu_freq_mhz = 240;
  35. }
  36. #elif CONFIG_IDF_TARGET_ESP32H2
  37. cpu_freq_mhz = 64;
  38. #endif
  39. if (esp_rom_get_reset_reason(0) != RESET_REASON_CPU0_SW || rtc_clk_apb_freq_get() < APB_CLK_FREQ) {
  40. rtc_clk_config_t clk_cfg = RTC_CLK_CONFIG_DEFAULT();
  41. #if CONFIG_IDF_TARGET_ESP32
  42. clk_cfg.xtal_freq = CONFIG_ESP32_XTAL_FREQ;
  43. #endif
  44. /* Except ESP32, there is no XTAL_FREQ choice */
  45. clk_cfg.cpu_freq_mhz = cpu_freq_mhz;
  46. clk_cfg.slow_clk_src = rtc_clk_slow_src_get();
  47. if (clk_cfg.slow_clk_src == SOC_RTC_SLOW_CLK_SRC_INVALID) {
  48. clk_cfg.slow_clk_src = SOC_RTC_SLOW_CLK_SRC_RC_SLOW;
  49. }
  50. clk_cfg.fast_clk_src = rtc_clk_fast_src_get();
  51. if (clk_cfg.fast_clk_src == SOC_RTC_FAST_CLK_SRC_INVALID) {
  52. clk_cfg.fast_clk_src = SOC_RTC_FAST_CLK_SRC_XTAL_DIV;
  53. }
  54. rtc_clk_init(clk_cfg);
  55. }
  56. /* As a slight optimization, if 32k XTAL was enabled in sdkconfig, we enable
  57. * it here. Usually it needs some time to start up, so we amortize at least
  58. * part of the start up time by enabling 32k XTAL early.
  59. * App startup code will wait until the oscillator has started up.
  60. */
  61. #if CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  62. if (!rtc_clk_32k_enabled()) {
  63. rtc_clk_32k_bootstrap(CONFIG_ESP_SYSTEM_RTC_EXT_XTAL_BOOTSTRAP_CYCLES);
  64. }
  65. #endif // CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  66. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  67. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  68. }