spiram.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. Abstraction layer for spi-ram. For now, it's no more than a stub for the spiram_psram functions, but if
  3. we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
  4. */
  5. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <sys/param.h>
  21. #include "sdkconfig.h"
  22. #include "esp_attr.h"
  23. #include "esp_err.h"
  24. #include "esp32s2/spiram.h"
  25. #include "spiram_psram.h"
  26. #include "esp_log.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/xtensa_api.h"
  29. #include "soc/soc.h"
  30. #include "esp_heap_caps_init.h"
  31. #include "soc/soc_memory_layout.h"
  32. #include "soc/dport_reg.h"
  33. #include "esp32s2/rom/cache.h"
  34. #include "soc/cache_memory.h"
  35. #include "soc/extmem_reg.h"
  36. #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
  37. #if CONFIG_SPIRAM
  38. static const char* TAG = "spiram";
  39. #if CONFIG_SPIRAM_SPEED_40M
  40. #define PSRAM_SPEED PSRAM_CACHE_S40M
  41. #elif CONFIG_SPIRAM_SPEED_80M
  42. #define PSRAM_SPEED PSRAM_CACHE_S80M
  43. #else
  44. #define PSRAM_SPEED PSRAM_CACHE_S20M
  45. #endif
  46. static bool spiram_inited=false;
  47. /*
  48. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  49. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  50. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  51. */
  52. bool esp_spiram_test(void)
  53. {
  54. size_t spiram_size = esp_spiram_get_size();
  55. volatile int *spiram=(volatile int*)(SOC_EXTRAM_DATA_HIGH - spiram_size);
  56. size_t p;
  57. size_t s = spiram_size;
  58. int errct=0;
  59. int initial_err=-1;
  60. if (SOC_EXTRAM_DATA_SIZE < spiram_size) {
  61. ESP_EARLY_LOGW(TAG, "Only test spiram from %08x to %08x\n", SOC_EXTRAM_DATA_LOW, SOC_EXTRAM_DATA_HIGH);
  62. spiram=(volatile int*)SOC_EXTRAM_DATA_LOW;
  63. s = SOC_EXTRAM_DATA_SIZE;
  64. }
  65. for (p=0; p<(s/sizeof(int)); p+=8) {
  66. spiram[p]=p^0xAAAAAAAA;
  67. }
  68. for (p=0; p<(s/sizeof(int)); p+=8) {
  69. if (spiram[p]!=(p^0xAAAAAAAA)) {
  70. errct++;
  71. if (errct==1) initial_err=p*4;
  72. if (errct < 4) {
  73. ESP_EARLY_LOGE(TAG, "SPI SRAM error@%08x:%08x/%08x \n", &spiram[p], spiram[p], p^0xAAAAAAAA);
  74. }
  75. }
  76. }
  77. if (errct) {
  78. ESP_EARLY_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, s/32, initial_err+SOC_EXTRAM_DATA_LOW);
  79. return false;
  80. } else {
  81. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  82. return true;
  83. }
  84. }
  85. #define DRAM0_ONLY_CACHE_SIZE BUS_IRAM0_CACHE_SIZE
  86. #define DRAM0_DRAM1_CACHE_SIZE (BUS_IRAM0_CACHE_SIZE + BUS_IRAM1_CACHE_SIZE)
  87. #define DRAM0_DRAM1_DPORT_CACHE_SIZE (BUS_IRAM0_CACHE_SIZE + BUS_IRAM1_CACHE_SIZE + BUS_DPORT_CACHE_SIZE)
  88. #define DBUS3_ONLY_CACHE_SIZE BUS_AHB_DBUS3_CACHE_SIZE
  89. #define DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE (DRAM0_DRAM1_DPORT_CACHE_SIZE + DBUS3_ONLY_CACHE_SIZE)
  90. #define SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT (spiram_size - DRAM0_DRAM1_DPORT_CACHE_SIZE)
  91. #define SPIRAM_SIZE_EXC_DATA_CACHE (spiram_size - DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE)
  92. #define SPIRAM_SMALL_SIZE_MAP_VADDR (DRAM0_CACHE_ADDRESS_HIGH - spiram_size)
  93. #define SPIRAM_SMALL_SIZE_MAP_PADDR 0
  94. #define SPIRAM_SMALL_SIZE_MAP_SIZE spiram_size
  95. #define SPIRAM_MID_SIZE_MAP_VADDR (AHB_DBUS3_ADDRESS_HIGH - SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT)
  96. #define SPIRAM_MID_SIZE_MAP_PADDR 0
  97. #define SPIRAM_MID_SIZE_MAP_SIZE (SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT)
  98. #define SPIRAM_BIG_SIZE_MAP_VADDR AHB_DBUS3_ADDRESS_LOW
  99. #define SPIRAM_BIG_SIZE_MAP_PADDR (AHB_DBUS3_ADDRESS_HIGH - DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE)
  100. #define SPIRAM_BIG_SIZE_MAP_SIZE DBUS3_ONLY_CACHE_SIZE
  101. #define SPIRAM_MID_BIG_SIZE_MAP_VADDR DPORT_CACHE_ADDRESS_LOW
  102. #define SPIRAM_MID_BIG_SIZE_MAP_PADDR SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT
  103. #define SPIRAM_MID_BIG_SIZE_MAP_SIZE DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE
  104. void IRAM_ATTR esp_spiram_init_cache(void)
  105. {
  106. size_t spiram_size = esp_spiram_get_size();
  107. Cache_Suspend_DCache();
  108. /* map the address from SPIRAM end to the start, map the address in order: DRAM1, DRAM1, DPORT, DBUS3 */
  109. if (spiram_size <= DRAM0_ONLY_CACHE_SIZE) {
  110. /* cache size <= 3MB + 512 KB, only map DRAM0 bus */
  111. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_SMALL_SIZE_MAP_VADDR, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, SPIRAM_SMALL_SIZE_MAP_SIZE >> 16, 0);
  112. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM0);
  113. } else if (spiram_size <= DRAM0_DRAM1_CACHE_SIZE) {
  114. /* cache size <= 7MB + 512KB, only map DRAM0 and DRAM1 bus */
  115. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_SMALL_SIZE_MAP_VADDR, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, SPIRAM_SMALL_SIZE_MAP_SIZE >> 16, 0);
  116. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0);
  117. } else if (spiram_size <= DRAM0_DRAM1_DPORT_CACHE_SIZE) {
  118. /* cache size <= 10MB + 512KB, map DRAM0, DRAM1, DPORT bus */
  119. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_SMALL_SIZE_MAP_VADDR, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, SPIRAM_SMALL_SIZE_MAP_SIZE >> 16, 0);
  120. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT);
  121. } else {
  122. #if CONFIG_SPIRAM_USE_AHB_DBUS3// TODO Ready to remove this macro esp32s2 no AHB bus access cache
  123. if (spiram_size <= DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE) {
  124. /* cache size <= 14MB + 512KB, map DRAM0, DRAM1, DPORT bus, as well as data bus3 */
  125. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MID_SIZE_MAP_VADDR, SPIRAM_MID_SIZE_MAP_PADDR, 64, SPIRAM_MID_SIZE_MAP_SIZE >> 16, 0);
  126. } else {
  127. /* cache size > 14MB + 512KB, map DRAM0, DRAM1, DPORT bus, as well as data bus3 */
  128. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_BIG_SIZE_MAP_VADDR, SPIRAM_BIG_SIZE_MAP_PADDR, 64, SPIRAM_BIG_SIZE_MAP_SIZE >> 16, 0);
  129. }
  130. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MID_BIG_SIZE_MAP_VADDR, SPIRAM_MID_BIG_SIZE_MAP_PADDR, 64, SPIRAM_MID_BIG_SIZE_MAP_SIZE >> 16, 0);
  131. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT | EXTMEM_PRO_DCACHE_MASK_BUS3);
  132. #else
  133. /* cache size > 10MB + 512KB, map DRAM0, DRAM1, DPORT bus , only remap 0x3f500000 ~ 0x3ff90000*/
  134. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, DPORT_CACHE_ADDRESS_LOW, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, DRAM0_DRAM1_DPORT_CACHE_SIZE >> 16, 0);
  135. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT);
  136. #endif
  137. }
  138. Cache_Resume_DCache(0);
  139. }
  140. static uint32_t pages_for_flash = 0;
  141. static uint32_t instrcution_in_spiram = 0;
  142. static uint32_t rodata_in_spiram = 0;
  143. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  144. static int instr_flash2spiram_offs = 0;
  145. static uint32_t instr_start_page = 0;
  146. static uint32_t instr_end_page = 0;
  147. #endif
  148. #if CONFIG_SPIRAM_RODATA
  149. static int rodata_flash2spiram_offs = 0;
  150. static uint32_t rodata_start_page = 0;
  151. static uint32_t rodata_end_page = 0;
  152. #endif
  153. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
  154. static uint32_t page0_mapped = 0;
  155. static uint32_t page0_page = INVALID_PHY_PAGE;
  156. #endif
  157. uint32_t esp_spiram_instruction_access_enabled(void)
  158. {
  159. return instrcution_in_spiram;
  160. }
  161. uint32_t esp_spiram_rodata_access_enabled(void)
  162. {
  163. return rodata_in_spiram;
  164. }
  165. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  166. esp_err_t esp_spiram_enable_instruction_access(void)
  167. {
  168. size_t spiram_size = esp_spiram_get_size();
  169. uint32_t pages_in_flash = 0;
  170. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS0, &page0_mapped);
  171. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS1, &page0_mapped);
  172. if ((pages_in_flash + pages_for_flash) > (spiram_size >> 16)) {
  173. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the instructions, has %d pages, need %d pages.", (spiram_size >> 16), (pages_in_flash + pages_for_flash));
  174. return ESP_FAIL;
  175. }
  176. ESP_EARLY_LOGI(TAG, "Instructions copied and mapped to SPIRAM");
  177. uint32_t instr_mmu_offset = ((uint32_t)&_instruction_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  178. uint32_t mmu_value = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS0_MMU_START + instr_mmu_offset*sizeof(uint32_t));
  179. mmu_value &= MMU_ADDRESS_MASK;
  180. instr_flash2spiram_offs = mmu_value - pages_for_flash;
  181. ESP_EARLY_LOGV(TAG, "Instructions from flash page%d copy to SPIRAM page%d, Offset: %d", mmu_value, pages_for_flash, instr_flash2spiram_offs);
  182. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS0, IRAM0_ADDRESS_LOW, pages_for_flash, &page0_page);
  183. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS1, IRAM1_ADDRESS_LOW, pages_for_flash, &page0_page);
  184. instrcution_in_spiram = 1;
  185. return ESP_OK;
  186. }
  187. #endif
  188. #if CONFIG_SPIRAM_RODATA
  189. esp_err_t esp_spiram_enable_rodata_access(void)
  190. {
  191. uint32_t pages_in_flash = 0;
  192. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS2, &page0_mapped);
  193. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS0, &page0_mapped);
  194. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS1, &page0_mapped);
  195. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS2, &page0_mapped);
  196. if ((pages_in_flash + pages_for_flash) > (esp_spiram_get_size() >> 16)) {
  197. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the read only data.");
  198. return ESP_FAIL;
  199. }
  200. ESP_EARLY_LOGI(TAG, "Read only data copied and mapped to SPIRAM");
  201. uint32_t rodata_mmu_offset = ((uint32_t)&_rodata_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  202. uint32_t mmu_value = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS2_MMU_START + rodata_mmu_offset*sizeof(uint32_t));
  203. mmu_value &= MMU_ADDRESS_MASK;
  204. rodata_flash2spiram_offs = mmu_value - pages_for_flash;
  205. ESP_EARLY_LOGV(TAG, "Rodata from flash page%d copy to SPIRAM page%d, Offset: %d", mmu_value, pages_for_flash, rodata_flash2spiram_offs);
  206. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS2, DROM0_ADDRESS_LOW, pages_for_flash, &page0_page);
  207. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS0, DRAM0_ADDRESS_LOW, pages_for_flash, &page0_page);
  208. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS1, DRAM1_ADDRESS_LOW, pages_for_flash, &page0_page);
  209. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS2, DPORT_ADDRESS_LOW, pages_for_flash, &page0_page);
  210. rodata_in_spiram = 1;
  211. return ESP_OK;
  212. }
  213. #endif
  214. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  215. void instruction_flash_page_info_init(void)
  216. {
  217. uint32_t instr_page_cnt = ((uint32_t)&_instruction_reserved_end - SOC_IROM_LOW + MMU_PAGE_SIZE - 1)/MMU_PAGE_SIZE;
  218. uint32_t instr_mmu_offset = ((uint32_t)&_instruction_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  219. instr_start_page = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS0_MMU_START + instr_mmu_offset*sizeof(uint32_t));
  220. instr_start_page &= MMU_ADDRESS_MASK;
  221. instr_end_page = instr_start_page + instr_page_cnt - 1;
  222. }
  223. uint32_t IRAM_ATTR instruction_flash_start_page_get(void)
  224. {
  225. return instr_start_page;
  226. }
  227. uint32_t IRAM_ATTR instruction_flash_end_page_get(void)
  228. {
  229. return instr_end_page;
  230. }
  231. int IRAM_ATTR instruction_flash2spiram_offset(void)
  232. {
  233. return instr_flash2spiram_offs;
  234. }
  235. #endif
  236. #if CONFIG_SPIRAM_RODATA
  237. void rodata_flash_page_info_init(void)
  238. {
  239. uint32_t rodata_page_cnt = ((uint32_t)&_rodata_reserved_end - SOC_DROM_LOW + MMU_PAGE_SIZE - 1)/MMU_PAGE_SIZE;
  240. uint32_t rodata_mmu_offset = ((uint32_t)&_rodata_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  241. rodata_start_page = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS2_MMU_START + rodata_mmu_offset*sizeof(uint32_t));
  242. rodata_start_page &= MMU_ADDRESS_MASK;
  243. rodata_end_page = rodata_start_page + rodata_page_cnt - 1;
  244. }
  245. uint32_t IRAM_ATTR rodata_flash_start_page_get(void)
  246. {
  247. return rodata_start_page;
  248. }
  249. uint32_t IRAM_ATTR rodata_flash_end_page_get(void)
  250. {
  251. return rodata_end_page;
  252. }
  253. int IRAM_ATTR rodata_flash2spiram_offset(void)
  254. {
  255. return rodata_flash2spiram_offs;
  256. }
  257. #endif
  258. esp_err_t esp_spiram_init(void)
  259. {
  260. esp_err_t r;
  261. r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
  262. if (r != ESP_OK) {
  263. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  264. ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
  265. #endif
  266. return r;
  267. }
  268. spiram_inited = true;
  269. size_t spiram_size = esp_spiram_get_size();
  270. #if (CONFIG_SPIRAM_SIZE != -1)
  271. if (spiram_size != CONFIG_SPIRAM_SIZE) {
  272. ESP_EARLY_LOGE(TAG, "Expected %dKiB chip but found %dKiB chip. Bailing out..", CONFIG_SPIRAM_SIZE/1024, spiram_size/1024);
  273. return ESP_ERR_INVALID_SIZE;
  274. }
  275. #endif
  276. ESP_EARLY_LOGI(TAG, "Found %dMBit SPI RAM device",
  277. (spiram_size*8)/(1024*1024));
  278. ESP_EARLY_LOGI(TAG, "SPI RAM mode: %s", PSRAM_SPEED == PSRAM_CACHE_S40M ? "sram 40m" : \
  279. PSRAM_SPEED == PSRAM_CACHE_S80M ? "sram 80m" : "sram 20m");
  280. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
  281. (PSRAM_MODE==PSRAM_VADDR_MODE_EVENODD)?"even/odd (2-core)": \
  282. (PSRAM_MODE==PSRAM_VADDR_MODE_LOWHIGH)?"low/high (2-core)": \
  283. (PSRAM_MODE==PSRAM_VADDR_MODE_NORMAL)?"normal (1-core)":"ERROR");
  284. return ESP_OK;
  285. }
  286. esp_err_t esp_spiram_add_to_heapalloc(void)
  287. {
  288. size_t spiram_size = esp_spiram_get_size();
  289. uint32_t size_for_flash = (pages_for_flash << 16);
  290. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (spiram_size - (pages_for_flash << 16))/1024);
  291. //Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's
  292. //no need to explicitly specify them.
  293. if (spiram_size <= DRAM0_DRAM1_DPORT_CACHE_SIZE) {
  294. /* cache size <= 10MB + 512KB, map DRAM0, DRAM1, DPORT bus */
  295. return heap_caps_add_region((intptr_t)SPIRAM_SMALL_SIZE_MAP_VADDR + size_for_flash, (intptr_t)SPIRAM_SMALL_SIZE_MAP_VADDR + SPIRAM_SMALL_SIZE_MAP_SIZE -1);
  296. } else {
  297. #if CONFIG_SPIRAM_USE_AHB_DBUS3 //TODO
  298. if (spiram_size <= DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE) {
  299. /* cache size <= 14MB + 512KB, map DRAM0, DRAM1, DPORT bus, as well as data bus3 */
  300. if (size_for_flash <= SPIRAM_MID_SIZE_MAP_SIZE) {
  301. esp_err_t err = heap_caps_add_region((intptr_t)SPIRAM_MID_SIZE_MAP_VADDR + size_for_flash, (intptr_t)SPIRAM_MID_SIZE_MAP_VADDR + SPIRAM_MID_SIZE_MAP_SIZE -1);
  302. if (err) {
  303. return err;
  304. }
  305. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  306. } else {
  307. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + size_for_flash - SPIRAM_MID_SIZE_MAP_SIZE, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  308. }
  309. } else {
  310. if (size_for_flash <= SPIRAM_SIZE_EXC_DATA_CACHE) {
  311. esp_err_t err = heap_caps_add_region((intptr_t)SPIRAM_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_BIG_SIZE_MAP_VADDR + SPIRAM_BIG_SIZE_MAP_SIZE -1);
  312. if (err) {
  313. return err;
  314. }
  315. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  316. } else if (size_for_flash <= SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT) {
  317. esp_err_t err = heap_caps_add_region((intptr_t)SPIRAM_BIG_SIZE_MAP_VADDR + size_for_flash - SPIRAM_SIZE_EXC_DATA_CACHE, (intptr_t)SPIRAM_MID_SIZE_MAP_VADDR + SPIRAM_MID_SIZE_MAP_SIZE -1);
  318. if (err) {
  319. return err;
  320. }
  321. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  322. } else {
  323. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + size_for_flash - SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  324. }
  325. }
  326. #else
  327. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, DPORT_CACHE_ADDRESS_LOW, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, DRAM0_DRAM1_DPORT_CACHE_SIZE >> 16, 0);
  328. if (size_for_flash <= SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT) {
  329. return heap_caps_add_region((intptr_t)DPORT_CACHE_ADDRESS_LOW, (intptr_t)DPORT_CACHE_ADDRESS_LOW + DRAM0_DRAM1_DPORT_CACHE_SIZE -1);
  330. } else {
  331. return heap_caps_add_region((intptr_t)DPORT_CACHE_ADDRESS_LOW + size_for_flash, (intptr_t)DPORT_CACHE_ADDRESS_LOW + DRAM0_DRAM1_DPORT_CACHE_SIZE -1);
  332. }
  333. return ESP_OK;
  334. #endif
  335. }
  336. }
  337. static uint8_t *dma_heap;
  338. esp_err_t esp_spiram_reserve_dma_pool(size_t size) {
  339. if (size==0) return ESP_OK; //no-op
  340. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
  341. dma_heap=heap_caps_malloc(size, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  342. if (!dma_heap) return ESP_ERR_NO_MEM;
  343. uint32_t caps[]={MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT|MALLOC_CAP_32BIT};
  344. return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap+size-1);
  345. }
  346. size_t esp_spiram_get_size(void)
  347. {
  348. if (!spiram_inited) {
  349. ESP_EARLY_LOGE(TAG, "SPI RAM not initialized");
  350. abort();
  351. }
  352. psram_size_t size=psram_get_size();
  353. if (size==PSRAM_SIZE_16MBITS) return 2*1024*1024;
  354. if (size==PSRAM_SIZE_32MBITS) return 4*1024*1024;
  355. if (size==PSRAM_SIZE_64MBITS) return 8*1024*1024;
  356. return CONFIG_SPIRAM_SIZE;
  357. }
  358. /*
  359. Before flushing the cache, if psram is enabled as a memory-mapped thing, we need to write back the data in the cache to the psram first,
  360. otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
  361. */
  362. void IRAM_ATTR esp_spiram_writeback_cache(void)
  363. {
  364. extern void Cache_WriteBack_All(void);
  365. Cache_WriteBack_All();
  366. }
  367. #endif