spiram.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "sdkconfig.h"
  21. #include "esp_attr.h"
  22. #include "esp_err.h"
  23. #include "spiram_psram.h"
  24. #include "esp_log.h"
  25. #include "freertos/FreeRTOS.h"
  26. #include "freertos/xtensa_api.h"
  27. #include "soc/soc.h"
  28. #include "esp_heap_caps_init.h"
  29. #include "soc/soc_memory_layout.h"
  30. #include "soc/dport_reg.h"
  31. #include "rom/cache.h"
  32. #if CONFIG_FREERTOS_UNICORE
  33. #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
  34. #else
  35. #if CONFIG_MEMMAP_SPIRAM_CACHE_EVENODD
  36. #define PSRAM_MODE PSRAM_VADDR_MODE_EVENODD
  37. #else
  38. #define PSRAM_MODE PSRAM_VADDR_MODE_LOWHIGH
  39. #endif
  40. #endif
  41. #if CONFIG_SPIRAM_SUPPORT
  42. static const char* TAG = "spiram";
  43. #if CONFIG_SPIRAM_SPEED_40M && CONFIG_ESPTOOLPY_FLASHFREQ_40M
  44. #define PSRAM_SPEED PSRAM_CACHE_F40M_S40M
  45. #elif CONFIG_SPIRAM_SPEED_40M && CONFIG_ESPTOOLPY_FLASHFREQ_80M
  46. #define PSRAM_SPEED PSRAM_CACHE_F80M_S40M
  47. #elif CONFIG_SPIRAM_SPEED_80M && CONFIG_ESPTOOLPY_FLASHFREQ_80M
  48. #define PSRAM_SPEED PSRAM_CACHE_F80M_S80M
  49. #else
  50. #error "FLASH speed can only be equal to or higher than SRAM speed while SRAM is enabled!"
  51. #endif
  52. static bool spiram_inited=false;
  53. /*
  54. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  55. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  56. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  57. */
  58. bool esp_spiram_test()
  59. {
  60. volatile int *spiram=(volatile int*)SOC_EXTRAM_DATA_LOW;
  61. size_t p;
  62. size_t s=CONFIG_SPIRAM_SIZE;
  63. int errct=0;
  64. int initial_err=-1;
  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. }
  73. }
  74. if (errct) {
  75. 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);
  76. return false;
  77. } else {
  78. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  79. return true;
  80. }
  81. }
  82. void IRAM_ATTR esp_spiram_init_cache()
  83. {
  84. //Enable external RAM in MMU
  85. cache_sram_mmu_set( 0, 0, SOC_EXTRAM_DATA_LOW, 0, 32, 128 );
  86. //Flush and enable icache for APP CPU
  87. #if !CONFIG_FREERTOS_UNICORE
  88. DPORT_CLEAR_PERI_REG_MASK(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DRAM1);
  89. cache_sram_mmu_set( 1, 0, SOC_EXTRAM_DATA_LOW, 0, 32, 128 );
  90. #endif
  91. }
  92. esp_err_t esp_spiram_init()
  93. {
  94. esp_err_t r;
  95. r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
  96. if (r != ESP_OK) {
  97. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  98. ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
  99. #endif
  100. return r;
  101. }
  102. ESP_EARLY_LOGI(TAG, "SPI RAM mode: %s", PSRAM_SPEED == PSRAM_CACHE_F40M_S40M ? "flash 40m sram 40m" : \
  103. PSRAM_SPEED == PSRAM_CACHE_F80M_S40M ? "flash 80m sram 40m" : \
  104. PSRAM_SPEED == PSRAM_CACHE_F80M_S80M ? "flash 80m sram 80m" : "ERROR");
  105. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
  106. (PSRAM_MODE==PSRAM_VADDR_MODE_EVENODD)?"even/odd (2-core)": \
  107. (PSRAM_MODE==PSRAM_VADDR_MODE_LOWHIGH)?"low/high (2-core)": \
  108. (PSRAM_MODE==PSRAM_VADDR_MODE_NORMAL)?"normal (1-core)":"ERROR");
  109. spiram_inited=true;
  110. return ESP_OK;
  111. }
  112. esp_err_t esp_spiram_add_to_heapalloc()
  113. {
  114. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", CONFIG_SPIRAM_SIZE/1024);
  115. //Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's
  116. //no need to explicitly specify them.
  117. return heap_caps_add_region((intptr_t)SOC_EXTRAM_DATA_LOW, (intptr_t)SOC_EXTRAM_DATA_LOW + CONFIG_SPIRAM_SIZE-1);
  118. }
  119. static uint8_t *dma_heap;
  120. esp_err_t esp_spiram_reserve_dma_pool(size_t size) {
  121. if (size==0) return ESP_OK; //no-op
  122. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
  123. dma_heap=heap_caps_malloc(size, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  124. if (!dma_heap) return ESP_ERR_NO_MEM;
  125. uint32_t caps[]={MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT|MALLOC_CAP_32BIT};
  126. return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap+size-1);
  127. }
  128. size_t esp_spiram_get_size()
  129. {
  130. return CONFIG_SPIRAM_SIZE;
  131. }
  132. /*
  133. 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,
  134. otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
  135. */
  136. void IRAM_ATTR esp_spiram_writeback_cache()
  137. {
  138. int x;
  139. volatile int i=0;
  140. volatile uint8_t *psram=(volatile uint8_t*)SOC_EXTRAM_DATA_LOW;
  141. int cache_was_disabled=0;
  142. if (!spiram_inited) return;
  143. //We need cache enabled for this to work. Re-enable it if needed; make sure we
  144. //disable it again on exit as well.
  145. if (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE)==0) {
  146. cache_was_disabled|=(1<<0);
  147. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  148. }
  149. #ifndef CONFIG_FREERTOS_UNICORE
  150. if (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE)==0) {
  151. cache_was_disabled|=(1<<1);
  152. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  153. }
  154. #endif
  155. #if CONFIG_FREERTOS_UNICORE
  156. for (x=0; x<1024*64; x+=32) {
  157. i+=psram[x];
  158. }
  159. #else
  160. /*
  161. Note: this assumes the amount of external RAM is >2M. If it is 2M or less, what this code does is undefined. If
  162. we ever support external RAM chips of 2M or smaller, this may need adjusting.
  163. */
  164. for (x=0; x<1024*64; x+=32) {
  165. i+=psram[x];
  166. i+=psram[x+(1024*1024*2)+(1024*64)]; //address picked to also clear cache of app cpu in low/high mode
  167. }
  168. #endif
  169. if (cache_was_disabled&(1<<0)) {
  170. while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) ;
  171. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  172. }
  173. #ifndef CONFIG_FREERTOS_UNICORE
  174. if (cache_was_disabled&(1<<1)) {
  175. while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1);
  176. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  177. }
  178. #endif
  179. }
  180. #endif