bootloader_clock.c 2.6 KB

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