bootloader_flash_config_esp32s3.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2020 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 <stdbool.h>
  15. #include <assert.h>
  16. #include "string.h"
  17. #include "sdkconfig.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #include "esp32s3/rom/spi_flash.h"
  21. #include "soc/efuse_reg.h"
  22. #include "soc/spi_reg.h"
  23. #include "soc/spi_mem_reg.h"
  24. #include "soc/soc_caps.h"
  25. #include "flash_qio_mode.h"
  26. #include "bootloader_flash_config.h"
  27. #include "bootloader_common.h"
  28. #define FLASH_IO_MATRIX_DUMMY_40M 0
  29. #define FLASH_IO_MATRIX_DUMMY_80M 0
  30. #define FLASH_IO_DRIVE_GD_WITH_1V8PSRAM 3
  31. void bootloader_flash_update_id()
  32. {
  33. g_rom_flashchip.device_id = bootloader_read_flash_id();
  34. }
  35. void IRAM_ATTR bootloader_flash_cs_timing_config()
  36. {
  37. SET_PERI_REG_MASK(SPI_MEM_USER_REG(0), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  38. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_HOLD_TIME_V, 0, SPI_MEM_CS_HOLD_TIME_S);
  39. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  40. SET_PERI_REG_MASK(SPI_MEM_USER_REG(1), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  41. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_HOLD_TIME_V, 1, SPI_MEM_CS_HOLD_TIME_S);
  42. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  43. }
  44. void IRAM_ATTR bootloader_flash_clock_config(const esp_image_header_t *pfhdr)
  45. {
  46. uint32_t spi_clk_div = 0;
  47. switch (pfhdr->spi_speed) {
  48. case ESP_IMAGE_SPI_SPEED_80M:
  49. spi_clk_div = 1;
  50. break;
  51. case ESP_IMAGE_SPI_SPEED_40M:
  52. spi_clk_div = 2;
  53. break;
  54. case ESP_IMAGE_SPI_SPEED_26M:
  55. spi_clk_div = 3;
  56. break;
  57. case ESP_IMAGE_SPI_SPEED_20M:
  58. spi_clk_div = 4;
  59. break;
  60. default:
  61. break;
  62. }
  63. esp_rom_spiflash_config_clk(spi_clk_div, 0);
  64. esp_rom_spiflash_config_clk(spi_clk_div, 1);
  65. }
  66. void IRAM_ATTR bootloader_flash_set_dummy_out(void)
  67. {
  68. REG_SET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
  69. REG_SET_BIT(SPI_MEM_CTRL_REG(1), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
  70. }
  71. void IRAM_ATTR bootloader_flash_dummy_config(const esp_image_header_t *pfhdr)
  72. {
  73. bootloader_configure_spi_pins(1);
  74. bootloader_flash_set_dummy_out();
  75. }