bootloader_flash_config_esp32c3.c 2.8 KB

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