2019-05-09 23:34:06 -04:00
|
|
|
/*
|
2022-01-12 02:03:50 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-06-02 10:34:38 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2022-01-12 02:03:50 -05:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------------------------------
|
|
|
|
* Abstraction layer for PSRAM. PSRAM device related registers and MMU/Cache related code shouls be
|
|
|
|
* abstracted to lower layers.
|
|
|
|
*
|
|
|
|
* When we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
|
|
|
|
*----------------------------------------------------------------------------------------------------*/
|
2019-05-09 23:34:06 -04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/xtensa_api.h"
|
|
|
|
#include "esp_heap_caps_init.h"
|
2022-03-23 08:16:08 -04:00
|
|
|
#include "esp_private/spiram_private.h"
|
2022-01-12 02:03:50 -05:00
|
|
|
#include "esp32s2/spiram.h"
|
|
|
|
#include "esp_private/mmu_psram.h"
|
|
|
|
#include "spiram_psram.h"
|
2019-05-09 23:34:06 -04:00
|
|
|
|
|
|
|
#define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
|
|
|
|
|
2019-06-05 00:34:19 -04:00
|
|
|
#if CONFIG_SPIRAM
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2019-08-22 02:17:46 -04:00
|
|
|
#if CONFIG_SPIRAM_SPEED_40M
|
|
|
|
#define PSRAM_SPEED PSRAM_CACHE_S40M
|
|
|
|
#elif CONFIG_SPIRAM_SPEED_80M
|
|
|
|
#define PSRAM_SPEED PSRAM_CACHE_S80M
|
2019-05-09 23:34:06 -04:00
|
|
|
#else
|
2019-08-22 02:17:46 -04:00
|
|
|
#define PSRAM_SPEED PSRAM_CACHE_S20M
|
2019-05-09 23:34:06 -04:00
|
|
|
#endif
|
|
|
|
|
2022-01-12 02:03:50 -05:00
|
|
|
#define MMU_PAGE_TO_BYTES(page_id) ((page_id) << 16)
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2021-08-25 04:06:28 -04:00
|
|
|
|
|
|
|
#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
|
2022-01-12 02:03:50 -05:00
|
|
|
extern uint8_t _ext_ram_bss_start;
|
|
|
|
extern uint8_t _ext_ram_bss_end;
|
|
|
|
#endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
|
2020-04-20 07:35:16 -04:00
|
|
|
|
2022-01-12 02:03:50 -05:00
|
|
|
//These variables are in bytes
|
2022-03-23 08:16:08 -04:00
|
|
|
static intptr_t s_allocable_vaddr_start;
|
|
|
|
static intptr_t s_allocable_vaddr_end;
|
|
|
|
static intptr_t s_mapped_vaddr_start;
|
|
|
|
static intptr_t s_mapped_vaddr_end;
|
2020-04-20 07:35:16 -04:00
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
static bool s_spiram_inited;
|
2022-01-12 02:03:50 -05:00
|
|
|
static const char* TAG = "spiram";
|
2020-04-20 07:35:16 -04:00
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
static bool esp_spiram_test(uint32_t v_start, uint32_t size);
|
2020-04-20 07:35:16 -04:00
|
|
|
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2019-08-11 22:06:07 -04:00
|
|
|
esp_err_t esp_spiram_init(void)
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2022-03-23 08:16:08 -04:00
|
|
|
assert(!s_spiram_inited);
|
|
|
|
esp_err_t ret;
|
|
|
|
ret = psram_enable(PSRAM_SPEED, PSRAM_MODE);
|
|
|
|
if (ret != ESP_OK) {
|
2019-05-09 23:34:06 -04:00
|
|
|
#if CONFIG_SPIRAM_IGNORE_NOTFOUND
|
|
|
|
ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
|
|
|
|
#endif
|
2022-03-23 08:16:08 -04:00
|
|
|
return ret;
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
2022-03-23 08:16:08 -04:00
|
|
|
s_spiram_inited = true;
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
uint32_t psram_physical_size = 0;
|
|
|
|
ret = psram_get_physical_size(&psram_physical_size);
|
|
|
|
assert(ret == ESP_OK);
|
2020-06-16 00:52:10 -04:00
|
|
|
|
2020-03-18 05:49:34 -04:00
|
|
|
#if (CONFIG_SPIRAM_SIZE != -1)
|
2022-03-23 08:16:08 -04:00
|
|
|
if (psram_physical_size != CONFIG_SPIRAM_SIZE) {
|
|
|
|
ESP_EARLY_LOGE(TAG, "Expected %dMB chip but found %dMB chip. Bailing out..", CONFIG_SPIRAM_SIZE / 1024 / 1024, psram_physical_size / 1024 / 1024);
|
2020-03-18 05:49:34 -04:00
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
#endif
|
2022-03-23 08:16:08 -04:00
|
|
|
ESP_EARLY_LOGI(TAG, "Found %dMBit SPI RAM device", psram_physical_size / (1024 * 1024));
|
|
|
|
ESP_EARLY_LOGI(TAG, "Speed: %dMHz", CONFIG_SPIRAM_SPEED);
|
2020-03-18 05:49:34 -04:00
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
uint32_t psram_available_size = 0;
|
|
|
|
ret = psram_get_available_size(&psram_available_size);
|
|
|
|
assert(ret == ESP_OK);
|
2021-08-25 04:06:28 -04:00
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
__attribute__((unused)) uint32_t total_available_size = psram_available_size;
|
2022-01-12 02:03:50 -05:00
|
|
|
/**
|
|
|
|
* `start_page` is the psram physical address in MMU page size.
|
|
|
|
* MMU page size on ESP32S2 is 64KB
|
|
|
|
* e.g.: psram physical address 16 is in page 0
|
|
|
|
*
|
|
|
|
* Here we plan to copy FLASH instructions to psram physical address 0, which is the No.0 page.
|
|
|
|
*/
|
|
|
|
uint32_t start_page = 0;
|
2021-08-25 04:06:28 -04:00
|
|
|
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
|
2022-01-12 02:03:50 -05:00
|
|
|
uint32_t used_page = 0;
|
2021-08-25 04:06:28 -04:00
|
|
|
#endif
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2022-01-12 02:03:50 -05:00
|
|
|
//------------------------------------Copy Flash .text to PSRAM-------------------------------------//
|
|
|
|
#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
|
2022-03-23 08:16:08 -04:00
|
|
|
ret = mmu_config_psram_text_segment(start_page, total_available_size, &used_page);
|
|
|
|
if (ret != ESP_OK) {
|
2022-01-12 02:03:50 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "No enough psram memory for instructon!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
start_page += used_page;
|
|
|
|
psram_available_size -= MMU_PAGE_TO_BYTES(used_page);
|
|
|
|
ESP_EARLY_LOGV(TAG, "after copy .text, used page is %d, start_page is %d, psram_available_size is %d B", used_page, start_page, psram_available_size);
|
|
|
|
#endif //#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
|
|
|
|
|
|
|
|
//------------------------------------Copy Flash .rodata to PSRAM-------------------------------------//
|
|
|
|
#if CONFIG_SPIRAM_RODATA
|
2022-03-23 08:16:08 -04:00
|
|
|
ret = mmu_config_psram_rodata_segment(start_page, total_available_size, &used_page);
|
|
|
|
if (ret != ESP_OK) {
|
2022-01-12 02:03:50 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "No enough psram memory for rodata!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
start_page += used_page;
|
|
|
|
psram_available_size -= MMU_PAGE_TO_BYTES(used_page);
|
|
|
|
ESP_EARLY_LOGV(TAG, "after copy .rodata, used page is %d, start_page is %d, psram_available_size is %d B", used_page, start_page, psram_available_size);
|
|
|
|
#endif //#if CONFIG_SPIRAM_RODATA
|
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
//----------------------------------Map the PSRAM physical range to MMU-----------------------------//
|
2022-01-12 02:03:50 -05:00
|
|
|
static DRAM_ATTR uint32_t vaddr_start = 0;
|
|
|
|
mmu_map_psram(MMU_PAGE_TO_BYTES(start_page), MMU_PAGE_TO_BYTES(start_page) + psram_available_size, &vaddr_start);
|
2022-03-23 08:16:08 -04:00
|
|
|
if (ret != ESP_OK) {
|
2022-01-12 02:03:50 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "MMU PSRAM mapping wrong!");
|
|
|
|
abort();
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
2021-01-06 00:34:29 -05:00
|
|
|
|
2022-01-12 02:03:50 -05:00
|
|
|
#if CONFIG_SPIRAM_MEMTEST
|
|
|
|
//After mapping, simple test SPIRAM first
|
|
|
|
bool ext_ram_ok = esp_spiram_test(vaddr_start, psram_available_size);
|
|
|
|
if (!ext_ram_ok) {
|
|
|
|
ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
|
|
|
|
abort();
|
2021-01-06 00:34:29 -05:00
|
|
|
}
|
2022-01-12 02:03:50 -05:00
|
|
|
#endif //#if CONFIG_SPIRAM_MEMTEST
|
2021-01-06 00:34:29 -05:00
|
|
|
|
2022-01-12 02:03:50 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* After mapping, we DON'T care about the PSRAM PHYSICAL ADDRESSS ANYMORE!
|
|
|
|
*----------------------------------------------------------------------------*/
|
2022-03-23 08:16:08 -04:00
|
|
|
s_mapped_vaddr_start = vaddr_start;
|
|
|
|
s_mapped_vaddr_end = vaddr_start + psram_available_size;
|
2022-01-12 02:03:50 -05:00
|
|
|
s_allocable_vaddr_start = vaddr_start;
|
|
|
|
s_allocable_vaddr_end = vaddr_start + psram_available_size;
|
|
|
|
|
|
|
|
//------------------------------------Configure .bss in PSRAM-------------------------------------//
|
|
|
|
#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
|
|
|
|
//should never be negative number
|
|
|
|
uint32_t ext_bss_size = ((intptr_t)&_ext_ram_bss_end - (intptr_t)&_ext_ram_bss_start);
|
|
|
|
ESP_EARLY_LOGV(TAG, "ext_bss_size is %d", ext_bss_size);
|
|
|
|
|
|
|
|
s_allocable_vaddr_start += ext_bss_size;
|
|
|
|
#endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
|
|
|
|
|
|
|
|
ESP_EARLY_LOGV(TAG, "s_allocable_vaddr_start is 0x%x, s_allocable_vaddr_end is 0x%x", s_allocable_vaddr_start, s_allocable_vaddr_end);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
/**
|
|
|
|
* Add the PSRAM available region to heap allocator. Heap allocator knows the capabilities of this type of memory,
|
|
|
|
* so there's no need to explicitly specify them.
|
|
|
|
*/
|
2022-01-12 02:03:50 -05:00
|
|
|
esp_err_t esp_spiram_add_to_heapalloc(void)
|
|
|
|
{
|
2022-03-23 08:16:08 -04:00
|
|
|
ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (s_allocable_vaddr_end - s_allocable_vaddr_start) / 1024);
|
|
|
|
return heap_caps_add_region(s_allocable_vaddr_start, s_allocable_vaddr_end);
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
esp_err_t IRAM_ATTR esp_spiram_get_mapped_range(intptr_t *out_vstart, intptr_t *out_vend)
|
|
|
|
{
|
|
|
|
if (!out_vstart || !out_vend) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
if (!s_spiram_inited) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_vstart = s_mapped_vaddr_start;
|
|
|
|
*out_vend = s_mapped_vaddr_end;
|
|
|
|
return ESP_OK;
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
esp_err_t esp_spiram_get_alloced_range(intptr_t *out_vstart, intptr_t *out_vend)
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
2022-03-23 08:16:08 -04:00
|
|
|
if (!out_vstart || !out_vend) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
2020-07-03 10:08:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 08:16:08 -04:00
|
|
|
if (!s_spiram_inited) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_vstart = s_allocable_vaddr_start;
|
|
|
|
*out_vend = s_allocable_vaddr_end;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_spiram_reserve_dma_pool(size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0) {
|
|
|
|
return ESP_OK; //no-op
|
|
|
|
}
|
|
|
|
ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
|
|
|
|
uint8_t *dma_heap = heap_caps_malloc(size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
|
|
|
|
if (!dma_heap) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
uint32_t caps[] = {MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT};
|
|
|
|
return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap + size);
|
2019-05-09 23:34:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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,
|
|
|
|
otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
|
|
|
|
*/
|
2019-08-11 22:06:07 -04:00
|
|
|
void IRAM_ATTR esp_spiram_writeback_cache(void)
|
2019-05-09 23:34:06 -04:00
|
|
|
{
|
|
|
|
extern void Cache_WriteBack_All(void);
|
|
|
|
Cache_WriteBack_All();
|
|
|
|
}
|
|
|
|
|
2021-07-29 00:45:29 -04:00
|
|
|
/**
|
|
|
|
* @brief If SPI RAM(PSRAM) has been initialized
|
|
|
|
*
|
|
|
|
* @return true SPI RAM has been initialized successfully
|
|
|
|
* @return false SPI RAM hasn't been initialized or initialized failed
|
|
|
|
*/
|
|
|
|
bool esp_spiram_is_initialized(void)
|
|
|
|
{
|
2022-03-23 08:16:08 -04:00
|
|
|
return s_spiram_inited;
|
2021-07-29 00:45:29 -04:00
|
|
|
}
|
2020-01-08 21:33:40 -05:00
|
|
|
|
2021-07-02 09:46:49 -04:00
|
|
|
uint8_t esp_spiram_get_cs_io(void)
|
|
|
|
{
|
|
|
|
return psram_get_cs_io();
|
|
|
|
}
|
|
|
|
|
2021-08-25 04:06:28 -04:00
|
|
|
/*
|
|
|
|
Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
|
|
|
|
true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
|
|
|
|
initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
|
|
|
|
*/
|
2022-03-23 08:16:08 -04:00
|
|
|
static bool esp_spiram_test(uint32_t v_start, uint32_t size)
|
2021-08-25 04:06:28 -04:00
|
|
|
{
|
2022-01-12 02:03:50 -05:00
|
|
|
volatile int *spiram = (volatile int *)v_start;
|
|
|
|
|
|
|
|
size_t s = size;
|
2021-08-25 04:06:28 -04:00
|
|
|
size_t p;
|
2022-01-12 02:03:50 -05:00
|
|
|
int errct = 0;
|
|
|
|
int initial_err = -1;
|
|
|
|
|
|
|
|
for (p = 0; p < (s / sizeof(int)); p += 8) {
|
|
|
|
spiram[p] = p ^ 0xAAAAAAAA;
|
2021-08-25 04:06:28 -04:00
|
|
|
}
|
2022-01-12 02:03:50 -05:00
|
|
|
for (p = 0; p < (s / sizeof(int)); p += 8) {
|
|
|
|
if (spiram[p] != (p ^ 0xAAAAAAAA)) {
|
2021-08-25 04:06:28 -04:00
|
|
|
errct++;
|
2022-01-12 02:03:50 -05:00
|
|
|
if (errct == 1) {
|
|
|
|
initial_err = p * 4;
|
|
|
|
}
|
2021-08-25 04:06:28 -04:00
|
|
|
if (errct < 4) {
|
2022-01-12 02:03:50 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "SPI SRAM error@%08x:%08x/%08x \n", &spiram[p], spiram[p], p ^ 0xAAAAAAAA);
|
2021-08-25 04:06:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errct) {
|
2022-01-12 02:03:50 -05:00
|
|
|
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);
|
2021-08-25 04:06:28 -04:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 02:03:50 -05:00
|
|
|
#endif //#if CONFIG_SPIRAM
|