flash_ops_esp32s2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "hal/spi_flash_hal.h"
  22. #include "esp_flash.h"
  23. #include "esp_log.h"
  24. static const char *TAG = "spiflash_s2";
  25. #define SPICACHE SPIMEM0
  26. #define SPIFLASH SPIMEM1
  27. extern void IRAM_ATTR flash_rom_init(void);
  28. esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_addr, const void *src, size_t size)
  29. {
  30. const spi_flash_guard_funcs_t *ops = spi_flash_guard_get();
  31. esp_rom_spiflash_result_t rc;
  32. assert((dest_addr % 16) == 0);
  33. assert((size % 16) == 0);
  34. if (!esp_ptr_internal(src)) {
  35. uint8_t block[128]; // Need to buffer in RAM as we write
  36. while (size > 0) {
  37. size_t next_block = MIN(size, sizeof(block));
  38. memcpy(block, src, next_block);
  39. esp_rom_spiflash_result_t r = spi_flash_write_encrypted_chip(dest_addr, block, next_block);
  40. if (r != ESP_ROM_SPIFLASH_RESULT_OK) {
  41. return r;
  42. }
  43. size -= next_block;
  44. dest_addr += next_block;
  45. src = ((uint8_t *)src) + next_block;
  46. }
  47. bzero(block, sizeof(block));
  48. return ESP_ROM_SPIFLASH_RESULT_OK;
  49. }
  50. else { // Already in internal memory
  51. ESP_LOGV(TAG, "calling SPI_Encrypt_Write addr 0x%x src %p size 0x%x", dest_addr, src, size);
  52. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  53. /* The ROM function SPI_Encrypt_Write assumes ADDR_BITLEN is already set but new
  54. implementation doesn't automatically set this to a usable value */
  55. SPIFLASH.user1.usr_addr_bitlen = 23;
  56. #endif
  57. if (ops && ops->start) {
  58. ops->start();
  59. }
  60. flash_rom_init();
  61. rc = SPI_Encrypt_Write(dest_addr, src, size);
  62. if (ops && ops->end) {
  63. ops->end();
  64. }
  65. return rc;
  66. }
  67. }
  68. #define FLASH_WRAP_CMD 0x77
  69. esp_err_t spi_flash_wrap_set(spi_flash_wrap_mode_t mode)
  70. {
  71. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  72. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  73. SPIFLASH.user.fwrite_dio = 0;
  74. SPIFLASH.user.fwrite_dual = 0;
  75. SPIFLASH.user.fwrite_qio = 1;
  76. SPIFLASH.user.fwrite_quad = 0;
  77. SPIFLASH.ctrl.fcmd_dual = 0;
  78. SPIFLASH.ctrl.fcmd_quad = 0;
  79. SPIFLASH.user.usr_dummy = 0;
  80. SPIFLASH.user.usr_addr = 1;
  81. SPIFLASH.user.usr_command = 1;
  82. SPIFLASH.user2.usr_command_bitlen = 7;
  83. SPIFLASH.user2.usr_command_value = FLASH_WRAP_CMD;
  84. SPIFLASH.user1.usr_addr_bitlen = 23;
  85. SPIFLASH.addr = 0;
  86. SPIFLASH.user.usr_miso = 0;
  87. SPIFLASH.user.usr_mosi = 1;
  88. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  89. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  90. SPIFLASH.cmd.usr = 1;
  91. while(SPIFLASH.cmd.usr != 0)
  92. { }
  93. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  94. SPIFLASH.user.val = reg_bkp_usr;
  95. return ESP_OK;
  96. }
  97. esp_err_t spi_flash_enable_wrap(uint32_t wrap_size)
  98. {
  99. switch(wrap_size) {
  100. case 8:
  101. return spi_flash_wrap_set(FLASH_WRAP_MODE_8B);
  102. case 16:
  103. return spi_flash_wrap_set(FLASH_WRAP_MODE_16B);
  104. case 32:
  105. return spi_flash_wrap_set(FLASH_WRAP_MODE_32B);
  106. case 64:
  107. return spi_flash_wrap_set(FLASH_WRAP_MODE_64B);
  108. default:
  109. return ESP_FAIL;
  110. }
  111. }
  112. void spi_flash_disable_wrap(void)
  113. {
  114. spi_flash_wrap_set(FLASH_WRAP_MODE_DISABLE);
  115. }
  116. bool spi_flash_support_wrap_size(uint32_t wrap_size)
  117. {
  118. 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)){
  119. return ESP_FAIL;
  120. }
  121. switch(wrap_size) {
  122. case 0:
  123. case 8:
  124. case 16:
  125. case 32:
  126. case 64:
  127. return true;
  128. default:
  129. return false;
  130. }
  131. }