bootloader_flash_config_esp32s3.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. esp_rom_spiflash_chip_t *chip = &rom_spiflash_legacy_data->chip;
  34. chip->device_id = bootloader_read_flash_id();
  35. }
  36. void IRAM_ATTR bootloader_flash_cs_timing_config()
  37. {
  38. SET_PERI_REG_MASK(SPI_MEM_USER_REG(0), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  39. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_HOLD_TIME_V, 0, SPI_MEM_CS_HOLD_TIME_S);
  40. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(0), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  41. SET_PERI_REG_MASK(SPI_MEM_USER_REG(1), SPI_MEM_CS_HOLD_M | SPI_MEM_CS_SETUP_M);
  42. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_HOLD_TIME_V, 1, SPI_MEM_CS_HOLD_TIME_S);
  43. SET_PERI_REG_BITS(SPI_MEM_CTRL2_REG(1), SPI_MEM_CS_SETUP_TIME_V, 0, SPI_MEM_CS_SETUP_TIME_S);
  44. }
  45. void IRAM_ATTR bootloader_flash_clock_config(const esp_image_header_t *pfhdr)
  46. {
  47. uint32_t spi_clk_div = 0;
  48. switch (pfhdr->spi_speed) {
  49. case ESP_IMAGE_SPI_SPEED_80M:
  50. spi_clk_div = 1;
  51. break;
  52. case ESP_IMAGE_SPI_SPEED_40M:
  53. spi_clk_div = 2;
  54. break;
  55. case ESP_IMAGE_SPI_SPEED_26M:
  56. spi_clk_div = 3;
  57. break;
  58. case ESP_IMAGE_SPI_SPEED_20M:
  59. spi_clk_div = 4;
  60. break;
  61. default:
  62. break;
  63. }
  64. esp_rom_spiflash_config_clk(spi_clk_div, 0);
  65. esp_rom_spiflash_config_clk(spi_clk_div, 1);
  66. }
  67. void IRAM_ATTR bootloader_flash_set_dummy_out(void)
  68. {
  69. REG_SET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
  70. REG_SET_BIT(SPI_MEM_CTRL_REG(1), SPI_MEM_FDUMMY_OUT | SPI_MEM_D_POL | SPI_MEM_Q_POL);
  71. }
  72. void IRAM_ATTR bootloader_flash_dummy_config(const esp_image_header_t *pfhdr)
  73. {
  74. bootloader_configure_spi_pins(1);
  75. bootloader_flash_set_dummy_out();
  76. }