bootloader_flash_config_esp32c3.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdbool.h>
  7. #include <assert.h>
  8. #include "string.h"
  9. #include "sdkconfig.h"
  10. #include "esp_err.h"
  11. #include "esp_log.h"
  12. #include "esp32c3/rom/gpio.h"
  13. #include "esp32c3/rom/spi_flash.h"
  14. #include "esp32c3/rom/efuse.h"
  15. #include "soc/gpio_periph.h"
  16. #include "soc/efuse_reg.h"
  17. #include "soc/spi_reg.h"
  18. #include "soc/spi_mem_reg.h"
  19. #include "soc/soc_caps.h"
  20. #include "flash_qio_mode.h"
  21. #include "bootloader_flash_config.h"
  22. #include "bootloader_common.h"
  23. #define FLASH_IO_MATRIX_DUMMY_40M 0
  24. #define FLASH_IO_MATRIX_DUMMY_80M 0
  25. void bootloader_flash_update_id()
  26. {
  27. esp_rom_spiflash_chip_t *chip = &rom_spiflash_legacy_data->chip;
  28. chip->device_id = bootloader_read_flash_id();
  29. }
  30. void IRAM_ATTR bootloader_flash_cs_timing_config()
  31. {
  32. SET_PERI_REG_MASK(SPI_MEM_USER_REG(0), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  33. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_HOLD_TIME_V, 0, SPI_MEM_CS_HOLD_TIME_S);
  34. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  35. SET_PERI_REG_MASK(SPI_MEM_USER_REG(1), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  36. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_HOLD_TIME_V, 1, SPI_MEM_CS_HOLD_TIME_S);
  37. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  38. }
  39. void IRAM_ATTR bootloader_flash_clock_config(const esp_image_header_t *pfhdr)
  40. {
  41. uint32_t spi_clk_div = 0;
  42. switch (pfhdr->spi_speed) {
  43. case ESP_IMAGE_SPI_SPEED_80M:
  44. spi_clk_div = 1;
  45. break;
  46. case ESP_IMAGE_SPI_SPEED_40M:
  47. spi_clk_div = 2;
  48. break;
  49. case ESP_IMAGE_SPI_SPEED_26M:
  50. spi_clk_div = 3;
  51. break;
  52. case ESP_IMAGE_SPI_SPEED_20M:
  53. spi_clk_div = 4;
  54. break;
  55. default:
  56. break;
  57. }
  58. esp_rom_spiflash_config_clk(spi_clk_div, 0);
  59. }
  60. void IRAM_ATTR bootloader_flash_set_dummy_out(void)
  61. {
  62. REG_SET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
  63. REG_SET_BIT(SPI_MEM_CTRL_REG(1), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
  64. }
  65. void IRAM_ATTR bootloader_flash_dummy_config(const esp_image_header_t *pfhdr)
  66. {
  67. bootloader_configure_spi_pins(1);
  68. bootloader_flash_set_dummy_out();
  69. }