flash_ops_esp32s2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2018 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 <string.h>
  15. #include <sys/param.h>
  16. #include "esp_spi_flash.h"
  17. #include "soc/system_reg.h"
  18. #include "soc/soc_memory_layout.h"
  19. #include "esp32s2/rom/spi_flash.h"
  20. #include "esp32s2/rom/cache.h"
  21. #include "bootloader_flash.h"
  22. #include "hal/spi_flash_hal.h"
  23. #include "esp_flash.h"
  24. #include "esp_log.h"
  25. static const char *TAG = "spiflash_s2";
  26. #define SPICACHE SPIMEM0
  27. #define SPIFLASH SPIMEM1
  28. extern void IRAM_ATTR flash_rom_init(void);
  29. esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_addr, const void *src, size_t size)
  30. {
  31. const spi_flash_guard_funcs_t *ops = spi_flash_guard_get();
  32. esp_rom_spiflash_result_t rc;
  33. assert((dest_addr % 16) == 0);
  34. assert((size % 16) == 0);
  35. if (!esp_ptr_internal(src)) {
  36. uint8_t block[128]; // Need to buffer in RAM as we write
  37. while (size > 0) {
  38. size_t next_block = MIN(size, sizeof(block));
  39. memcpy(block, src, next_block);
  40. esp_rom_spiflash_result_t r = spi_flash_write_encrypted_chip(dest_addr, block, next_block);
  41. if (r != ESP_ROM_SPIFLASH_RESULT_OK) {
  42. return r;
  43. }
  44. size -= next_block;
  45. dest_addr += next_block;
  46. src = ((uint8_t *)src) + next_block;
  47. }
  48. bzero(block, sizeof(block));
  49. return ESP_ROM_SPIFLASH_RESULT_OK;
  50. }
  51. else { // Already in internal memory
  52. ESP_LOGV(TAG, "calling SPI_Encrypt_Write addr 0x%x src %p size 0x%x", dest_addr, src, size);
  53. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  54. /* The ROM function SPI_Encrypt_Write assumes ADDR_BITLEN is already set but new
  55. implementation doesn't automatically set this to a usable value */
  56. SPIFLASH.user1.usr_addr_bitlen = 23;
  57. #endif
  58. if (ops && ops->start) {
  59. ops->start();
  60. }
  61. flash_rom_init();
  62. rc = SPI_Encrypt_Write(dest_addr, src, size);
  63. if (ops && ops->end) {
  64. ops->end();
  65. }
  66. return rc;
  67. }
  68. }
  69. esp_err_t spi_flash_wrap_set(spi_flash_wrap_mode_t mode)
  70. {
  71. return bootloader_flash_wrap_set(mode);
  72. }
  73. esp_err_t spi_flash_enable_wrap(uint32_t wrap_size)
  74. {
  75. switch(wrap_size) {
  76. case 8:
  77. return bootloader_flash_wrap_set(FLASH_WRAP_MODE_8B);
  78. case 16:
  79. return bootloader_flash_wrap_set(FLASH_WRAP_MODE_16B);
  80. case 32:
  81. return bootloader_flash_wrap_set(FLASH_WRAP_MODE_32B);
  82. case 64:
  83. return bootloader_flash_wrap_set(FLASH_WRAP_MODE_64B);
  84. default:
  85. return ESP_FAIL;
  86. }
  87. }
  88. void spi_flash_disable_wrap(void)
  89. {
  90. bootloader_flash_wrap_set(FLASH_WRAP_MODE_DISABLE);
  91. }
  92. bool spi_flash_support_wrap_size(uint32_t wrap_size)
  93. {
  94. if (!REG_GET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FREAD_QIO) || !REG_GET_BIT(SPI_MEM_CTRL_REG(0), SPI_MEM_FASTRD_MODE)){
  95. return ESP_FAIL;
  96. }
  97. switch(wrap_size) {
  98. case 0:
  99. case 8:
  100. case 16:
  101. case 32:
  102. case 64:
  103. return true;
  104. default:
  105. return false;
  106. }
  107. }