bootloader_clock_init.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "soc/dport_reg.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. DPORT_REG_GET_FIELD(DPORT_CPU_PER_CONF_REG, DPORT_CPUPERIOD_SEL) == DPORT_CPUPERIOD_SEL_240) {
  34. cpu_freq_mhz = 240;
  35. }
  36. #endif
  37. if (rtc_clk_apb_freq_get() < APB_CLK_FREQ || esp_rom_get_reset_reason(0) != RESET_REASON_CPU0_SW) {
  38. rtc_clk_config_t clk_cfg = RTC_CLK_CONFIG_DEFAULT();
  39. #if CONFIG_IDF_TARGET_ESP32
  40. clk_cfg.xtal_freq = CONFIG_ESP32_XTAL_FREQ;
  41. #endif
  42. /* ESP32-S2 doesn't have XTAL_FREQ choice, always 40MHz */
  43. clk_cfg.cpu_freq_mhz = cpu_freq_mhz;
  44. clk_cfg.slow_freq = rtc_clk_slow_freq_get();
  45. clk_cfg.fast_freq = rtc_clk_fast_freq_get();
  46. rtc_clk_init(clk_cfg);
  47. }
  48. /* As a slight optimization, if 32k XTAL was enabled in sdkconfig, we enable
  49. * it here. Usually it needs some time to start up, so we amortize at least
  50. * part of the start up time by enabling 32k XTAL early.
  51. * App startup code will wait until the oscillator has started up.
  52. */
  53. #if CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  54. if (!rtc_clk_32k_enabled()) {
  55. rtc_clk_32k_bootstrap(CONFIG_ESP_SYSTEM_RTC_EXT_XTAL_BOOTSTRAP_CYCLES);
  56. }
  57. #endif // CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
  58. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  59. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  60. }