bootloader_flash_config_esp32s2.c 2.9 KB

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