bootloader_clock.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 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 "sdkconfig.h"
  15. #include "soc/soc.h"
  16. #include "soc/rtc.h"
  17. #include "soc/dport_reg.h"
  18. #include "soc/efuse_periph.h"
  19. #include "soc/rtc_cntl_reg.h"
  20. #ifdef CONFIG_IDF_TARGET_ESP32
  21. #include "esp32/rom/uart.h"
  22. #include "esp32/rom/rtc.h"
  23. #elif CONFIG_IDF_TARGET_ESP32S2
  24. #include "esp32s2/rom/uart.h"
  25. #include "esp32s2/rom/rtc.h"
  26. #endif
  27. void bootloader_clock_configure(void)
  28. {
  29. // ROM bootloader may have put a lot of text into UART0 FIFO.
  30. // Wait for it to be printed.
  31. // This is not needed on power on reset, when ROM bootloader is running at
  32. // 40 MHz. But in case of TG WDT reset, CPU may still be running at >80 MHZ,
  33. // and will be done with the bootloader much earlier than UART FIFO is empty.
  34. uart_tx_wait_idle(0);
  35. /* Set CPU to 80MHz. Keep other clocks unmodified. */
  36. int cpu_freq_mhz = 80;
  37. #if CONFIG_IDF_TARGET_ESP32
  38. /* On ESP32 rev 0, switching to 80/160 MHz if clock was previously set to
  39. * 240 MHz may cause the chip to lock up (see section 3.5 of the errata
  40. * document). For rev. 0, switch to 240 instead if it has been enabled
  41. * previously.
  42. */
  43. uint32_t chip_ver_reg = REG_READ(EFUSE_BLK0_RDATA3_REG);
  44. if ((chip_ver_reg & EFUSE_RD_CHIP_VER_REV1_M) == 0 &&
  45. DPORT_REG_GET_FIELD(DPORT_CPU_PER_CONF_REG, DPORT_CPUPERIOD_SEL) == DPORT_CPUPERIOD_SEL_240) {
  46. cpu_freq_mhz = 240;
  47. }
  48. #endif
  49. rtc_clk_config_t clk_cfg = RTC_CLK_CONFIG_DEFAULT();
  50. #if CONFIG_IDF_TARGET_ESP32
  51. clk_cfg.xtal_freq = CONFIG_ESP32_XTAL_FREQ;
  52. #endif
  53. /* ESP32-S2 doesn't have XTAL_FREQ choice, always 40MHz */
  54. clk_cfg.cpu_freq_mhz = cpu_freq_mhz;
  55. clk_cfg.slow_freq = rtc_clk_slow_freq_get();
  56. clk_cfg.fast_freq = rtc_clk_fast_freq_get();
  57. rtc_clk_init(clk_cfg);
  58. /* As a slight optimization, if 32k XTAL was enabled in sdkconfig, we enable
  59. * it here. Usually it needs some time to start up, so we amortize at least
  60. * part of the start up time by enabling 32k XTAL early.
  61. * App startup code will wait until the oscillator has started up.
  62. */
  63. /* TODO: move the clock option into esp_system, so that this doesn't have
  64. * to continue:
  65. */
  66. #if CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  67. if (!rtc_clk_32k_enabled()) {
  68. rtc_clk_32k_bootstrap(CONFIG_ESP32_RTC_XTAL_BOOTSTRAP_CYCLES);
  69. }
  70. #endif
  71. #if CONFIG_ESP32S2_RTC_CLK_SRC_EXT_CRYS
  72. if (!rtc_clk_32k_enabled()) {
  73. rtc_clk_32k_bootstrap(0);
  74. }
  75. #endif
  76. }
  77. #ifdef BOOTLOADER_BUILD
  78. int esp_clk_apb_freq(void)
  79. {
  80. return rtc_clk_apb_freq_get();
  81. }
  82. #endif // BOOTLOADER_BUILD