flash_ops_esp32c3.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <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 "esp32c3/rom/spi_flash.h"
  20. #include "esp32c3/rom/cache.h"
  21. #include "hal/spi_flash_hal.h"
  22. #include "esp_flash.h"
  23. #include "esp_log.h"
  24. #include "esp_attr.h"
  25. static const char *TAG = "spiflash_c3";
  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. /* src needs to be 32 bit aligned */
  36. if (!esp_ptr_internal(src) || (intptr_t)src & 0x3) {
  37. WORD_ALIGNED_ATTR uint8_t block[128]; // Need to buffer in RAM as we write
  38. while (size > 0) {
  39. size_t next_block = MIN(size, sizeof(block));
  40. memcpy(block, src, next_block);
  41. esp_rom_spiflash_result_t r = spi_flash_write_encrypted_chip(dest_addr, block, next_block);
  42. if (r != ESP_ROM_SPIFLASH_RESULT_OK) {
  43. return r;
  44. }
  45. size -= next_block;
  46. dest_addr += next_block;
  47. src = ((uint8_t *)src) + next_block;
  48. }
  49. bzero(block, sizeof(block));
  50. return ESP_ROM_SPIFLASH_RESULT_OK;
  51. } else { // Already in internal memory
  52. ESP_LOGV(TAG, "calling esp_rom_spiflash_write_encrypted 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 = esp_rom_spiflash_write_encrypted(dest_addr, (uint32_t *)src, size);
  63. if (ops && ops->end) {
  64. ops->end();
  65. }
  66. return rc;
  67. }
  68. }
  69. #define FLASH_WRAP_CMD 0x77
  70. esp_err_t spi_flash_wrap_set(spi_flash_wrap_mode_t mode)
  71. {
  72. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  73. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  74. SPIFLASH.user.fwrite_dio = 0;
  75. SPIFLASH.user.fwrite_dual = 0;
  76. SPIFLASH.user.fwrite_qio = 1;
  77. SPIFLASH.user.fwrite_quad = 0;
  78. SPIFLASH.ctrl.fcmd_dual = 0;
  79. SPIFLASH.ctrl.fcmd_quad = 0;
  80. SPIFLASH.user.usr_dummy = 0;
  81. SPIFLASH.user.usr_addr = 1;
  82. SPIFLASH.user.usr_command = 1;
  83. SPIFLASH.user2.usr_command_bitlen = 7;
  84. SPIFLASH.user2.usr_command_value = FLASH_WRAP_CMD;
  85. SPIFLASH.user1.usr_addr_bitlen = 23;
  86. SPIFLASH.addr = 0;
  87. SPIFLASH.user.usr_miso = 0;
  88. SPIFLASH.user.usr_mosi = 1;
  89. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  90. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  91. SPIFLASH.cmd.usr = 1;
  92. while (SPIFLASH.cmd.usr != 0)
  93. { }
  94. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  95. SPIFLASH.user.val = reg_bkp_usr;
  96. return ESP_OK;
  97. }
  98. esp_err_t spi_flash_enable_wrap(uint32_t wrap_size)
  99. {
  100. switch (wrap_size) {
  101. case 8:
  102. return spi_flash_wrap_set(FLASH_WRAP_MODE_8B);
  103. case 16:
  104. return spi_flash_wrap_set(FLASH_WRAP_MODE_16B);
  105. case 32:
  106. return spi_flash_wrap_set(FLASH_WRAP_MODE_32B);
  107. case 64:
  108. return spi_flash_wrap_set(FLASH_WRAP_MODE_64B);
  109. default:
  110. return ESP_FAIL;
  111. }
  112. }
  113. void spi_flash_disable_wrap(void)
  114. {
  115. spi_flash_wrap_set(FLASH_WRAP_MODE_DISABLE);
  116. }
  117. bool spi_flash_support_wrap_size(uint32_t wrap_size)
  118. {
  119. 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)) {
  120. return ESP_FAIL;
  121. }
  122. switch (wrap_size) {
  123. case 0:
  124. case 8:
  125. case 16:
  126. case 32:
  127. case 64:
  128. return true;
  129. default:
  130. return false;
  131. }
  132. }