system_internal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "sdkconfig.h"
  8. #include "esp_system.h"
  9. #include "esp_private/system_internal.h"
  10. #include "esp_attr.h"
  11. #include "esp_efuse.h"
  12. #include "esp_log.h"
  13. #include "riscv/rv_utils.h"
  14. #include "esp_rom_uart.h"
  15. #include "soc/gpio_reg.h"
  16. #include "soc/timer_group_reg.h"
  17. #include "esp_cpu.h"
  18. #include "soc/rtc.h"
  19. #include "esp_private/rtc_clk.h"
  20. #include "soc/rtc_periph.h"
  21. #include "soc/syscon_reg.h"
  22. #include "soc/system_reg.h"
  23. #include "hal/wdt_hal.h"
  24. #include "esp_private/cache_err_int.h"
  25. #include "esp32c2/rom/cache.h"
  26. #include "esp32c2/rom/rtc.h"
  27. void IRAM_ATTR esp_system_reset_modules_on_exit(void)
  28. {
  29. // Flush any data left in UART FIFOs before reset the UART peripheral
  30. esp_rom_uart_tx_wait_idle(0);
  31. esp_rom_uart_tx_wait_idle(1);
  32. // Reset wifi/bluetooth/ethernet/sdio (bb/mac)
  33. REG_WRITE(SYSTEM_CORE_RST_EN_REG, 0);
  34. // Reset timer/spi/uart
  35. SET_PERI_REG_MASK(SYSTEM_PERIP_RST_EN0_REG,
  36. SYSTEM_SPI01_RST | SYSTEM_UART_RST | SYSTEM_SYSTIMER_RST);
  37. REG_WRITE(SYSTEM_PERIP_RST_EN0_REG, 0);
  38. // Reset dma
  39. SET_PERI_REG_MASK(SYSTEM_PERIP_RST_EN1_REG, SYSTEM_DMA_RST);
  40. REG_WRITE(SYSTEM_PERIP_RST_EN1_REG, 0);
  41. }
  42. /* "inner" restart function for after RTOS, interrupts & anything else on this
  43. * core are already stopped. Stalls other core, resets hardware,
  44. * triggers restart.
  45. */
  46. void IRAM_ATTR esp_restart_noos(void)
  47. {
  48. // Disable interrupts
  49. rv_utils_intr_global_disable();
  50. // Enable RTC watchdog for 1 second
  51. wdt_hal_context_t rtc_wdt_ctx;
  52. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  53. uint32_t stage_timeout_ticks = (uint32_t)(1000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  54. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  55. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
  56. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE1, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  57. //Enable flash boot mode so that flash booting after restart is protected by the RTC WDT.
  58. wdt_hal_set_flashboot_en(&rtc_wdt_ctx, true);
  59. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  60. // Disable TG0/TG1 watchdogs
  61. wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  62. wdt_hal_write_protect_disable(&wdt0_context);
  63. wdt_hal_disable(&wdt0_context);
  64. wdt_hal_write_protect_enable(&wdt0_context);
  65. // Disable cache
  66. Cache_Disable_ICache();
  67. // 2nd stage bootloader reconfigures SPI flash signals.
  68. // Reset them to the defaults expected by ROM.
  69. WRITE_PERI_REG(GPIO_FUNC0_IN_SEL_CFG_REG, 0x30);
  70. WRITE_PERI_REG(GPIO_FUNC1_IN_SEL_CFG_REG, 0x30);
  71. WRITE_PERI_REG(GPIO_FUNC2_IN_SEL_CFG_REG, 0x30);
  72. WRITE_PERI_REG(GPIO_FUNC3_IN_SEL_CFG_REG, 0x30);
  73. WRITE_PERI_REG(GPIO_FUNC4_IN_SEL_CFG_REG, 0x30);
  74. WRITE_PERI_REG(GPIO_FUNC5_IN_SEL_CFG_REG, 0x30);
  75. esp_system_reset_modules_on_exit();
  76. // Set CPU back to XTAL source, same as hard reset. PLL keeps on to match the behavior with chips.
  77. #if !CONFIG_IDF_ENV_FPGA
  78. rtc_clk_cpu_set_to_default_config();
  79. #endif
  80. // Reset CPU
  81. esp_rom_software_reset_cpu(0);
  82. while (true) {
  83. ;
  84. }
  85. }