2016-08-17 15:08:22 +00:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-10-19 09:08:05 +00:00
|
|
|
//
|
2016-08-17 15:08:22 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-09-12 10:54:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2016-09-13 08:40:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
#include <rom/spi_flash.h>
|
|
|
|
#include <rom/cache.h>
|
2016-09-12 10:54:45 +00:00
|
|
|
#include <soc/soc.h>
|
2016-08-17 15:08:22 +00:00
|
|
|
#include <soc/dport_reg.h>
|
2016-08-24 10:01:01 +00:00
|
|
|
#include "sdkconfig.h"
|
2016-09-12 10:54:45 +00:00
|
|
|
#include "esp_ipc.h"
|
|
|
|
#include "esp_attr.h"
|
2016-11-25 09:33:51 +00:00
|
|
|
#include "esp_intr_alloc.h"
|
2016-09-12 10:54:45 +00:00
|
|
|
#include "esp_spi_flash.h"
|
2016-09-23 00:44:45 +00:00
|
|
|
#include "esp_log.h"
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2016-08-24 10:01:01 +00:00
|
|
|
|
2016-09-12 10:54:45 +00:00
|
|
|
static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state);
|
|
|
|
static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
|
2016-08-24 10:01:01 +00:00
|
|
|
|
2016-09-12 10:54:45 +00:00
|
|
|
static uint32_t s_flash_op_cache_state[2];
|
2016-08-24 10:01:01 +00:00
|
|
|
|
2016-09-12 10:54:45 +00:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
2016-08-24 10:01:01 +00:00
|
|
|
static SemaphoreHandle_t s_flash_op_mutex;
|
2016-11-08 01:02:30 +00:00
|
|
|
static volatile bool s_flash_op_can_start = false;
|
|
|
|
static volatile bool s_flash_op_complete = false;
|
2017-01-18 07:07:27 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static volatile int s_flash_op_cpu = -1;
|
|
|
|
#endif
|
2016-09-13 08:40:05 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void spi_flash_init_lock()
|
|
|
|
{
|
|
|
|
s_flash_op_mutex = xSemaphoreCreateMutex();
|
2017-07-12 03:33:51 +00:00
|
|
|
assert(s_flash_op_mutex != NULL);
|
2016-10-19 09:08:05 +00:00
|
|
|
}
|
2016-09-13 08:40:05 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void spi_flash_op_lock()
|
|
|
|
{
|
|
|
|
xSemaphoreTake(s_flash_op_mutex, portMAX_DELAY);
|
|
|
|
}
|
2016-08-24 10:01:01 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void spi_flash_op_unlock()
|
|
|
|
{
|
|
|
|
xSemaphoreGive(s_flash_op_mutex);
|
|
|
|
}
|
2016-09-12 10:54:45 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void IRAM_ATTR spi_flash_op_block_func(void* arg)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2016-09-12 10:54:45 +00:00
|
|
|
// Disable scheduler on this CPU
|
|
|
|
vTaskSuspendAll();
|
2016-11-25 09:33:51 +00:00
|
|
|
// Restore interrupts that aren't located in IRAM
|
|
|
|
esp_intr_noniram_disable();
|
2016-08-24 10:01:01 +00:00
|
|
|
uint32_t cpuid = (uint32_t) arg;
|
2016-09-12 10:54:45 +00:00
|
|
|
// Disable cache so that flash operation can start
|
|
|
|
spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
|
2017-03-27 17:02:58 +00:00
|
|
|
// s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
|
|
|
|
// CPU may reset the flag back to false before IPC task has a chance to check it
|
|
|
|
// (if it is preempted by an ISR taking non-trivial amount of time)
|
|
|
|
s_flash_op_complete = false;
|
2016-09-12 10:54:45 +00:00
|
|
|
s_flash_op_can_start = true;
|
|
|
|
while (!s_flash_op_complete) {
|
|
|
|
// busy loop here and wait for the other CPU to finish flash operation
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-09-12 10:54:45 +00:00
|
|
|
// Flash operation is complete, re-enable cache
|
|
|
|
spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
|
2016-11-25 09:33:51 +00:00
|
|
|
// Restore interrupts that aren't located in IRAM
|
|
|
|
esp_intr_noniram_enable();
|
2016-09-12 10:54:45 +00:00
|
|
|
// Re-enable scheduler
|
|
|
|
xTaskResumeAll();
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
|
2016-08-24 10:01:01 +00:00
|
|
|
{
|
2016-10-19 09:08:05 +00:00
|
|
|
spi_flash_op_lock();
|
2016-09-12 10:54:45 +00:00
|
|
|
|
2016-08-24 10:01:01 +00:00
|
|
|
const uint32_t cpuid = xPortGetCoreID();
|
2016-09-13 04:47:21 +00:00
|
|
|
const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
|
2017-01-18 07:07:27 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// For sanity check later: record the CPU which has started doing flash operation
|
|
|
|
assert(s_flash_op_cpu == -1);
|
|
|
|
s_flash_op_cpu = cpuid;
|
|
|
|
#endif
|
2016-09-12 10:54:45 +00:00
|
|
|
|
|
|
|
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
|
2016-09-13 04:47:21 +00:00
|
|
|
// Scheduler hasn't been started yet, it means that spi_flash API is being
|
|
|
|
// called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
|
|
|
|
// PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
|
|
|
|
// which is in IRAM. So it is safe to disable cache for the other_cpuid here.
|
|
|
|
assert(other_cpuid == 1);
|
2016-09-12 10:54:45 +00:00
|
|
|
spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
|
|
|
|
} else {
|
|
|
|
// Signal to the spi_flash_op_block_task on the other CPU that we need it to
|
|
|
|
// disable cache there and block other tasks from executing.
|
|
|
|
s_flash_op_can_start = false;
|
2017-01-18 07:07:27 +00:00
|
|
|
esp_err_t ret = esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void*) other_cpuid);
|
|
|
|
assert(ret == ESP_OK);
|
2016-09-12 10:54:45 +00:00
|
|
|
while (!s_flash_op_can_start) {
|
|
|
|
// Busy loop and wait for spi_flash_op_block_func to disable cache
|
|
|
|
// on the other CPU
|
|
|
|
}
|
2017-01-18 07:07:27 +00:00
|
|
|
// Disable scheduler on the current CPU
|
2016-09-12 10:54:45 +00:00
|
|
|
vTaskSuspendAll();
|
|
|
|
// This is guaranteed to run on CPU <cpuid> because the other CPU is now
|
|
|
|
// occupied by highest priority task
|
|
|
|
assert(xPortGetCoreID() == cpuid);
|
2016-08-24 10:01:01 +00:00
|
|
|
}
|
2016-11-25 09:33:51 +00:00
|
|
|
// Kill interrupts that aren't located in IRAM
|
|
|
|
esp_intr_noniram_disable();
|
2016-08-24 10:01:01 +00:00
|
|
|
// Disable cache on this CPU as well
|
2016-09-12 10:54:45 +00:00
|
|
|
spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
|
2016-08-24 10:01:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
|
2016-08-24 10:01:01 +00:00
|
|
|
{
|
2016-09-12 10:54:45 +00:00
|
|
|
const uint32_t cpuid = xPortGetCoreID();
|
2016-09-13 04:47:21 +00:00
|
|
|
const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
|
2017-01-18 07:07:27 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Sanity check: flash operation ends on the same CPU as it has started
|
|
|
|
assert(cpuid == s_flash_op_cpu);
|
|
|
|
s_flash_op_cpu = -1;
|
|
|
|
#endif
|
2016-09-12 10:54:45 +00:00
|
|
|
|
2016-08-24 10:01:01 +00:00
|
|
|
// Re-enable cache on this CPU
|
2016-09-12 10:54:45 +00:00
|
|
|
spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
|
|
|
|
|
|
|
|
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
|
2016-09-13 04:47:21 +00:00
|
|
|
// Scheduler is not running yet — this means we are running on PRO CPU.
|
|
|
|
// other_cpuid is APP CPU, and it is either in reset or is spinning in
|
|
|
|
// user_start_cpu1, which is in IRAM. So we can simply reenable cache.
|
|
|
|
assert(other_cpuid == 1);
|
2016-09-12 10:54:45 +00:00
|
|
|
spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
|
|
|
|
} else {
|
|
|
|
// Signal to spi_flash_op_block_task that flash operation is complete
|
|
|
|
s_flash_op_complete = true;
|
2017-01-18 07:07:27 +00:00
|
|
|
}
|
|
|
|
// Re-enable non-iram interrupts
|
|
|
|
esp_intr_noniram_enable();
|
|
|
|
|
|
|
|
// Resume tasks on the current CPU, if the scheduler has started.
|
|
|
|
// NOTE: enabling non-IRAM interrupts has to happen before this,
|
|
|
|
// because once the scheduler has started, due to preemption the
|
|
|
|
// current task can end up being moved to the other CPU.
|
|
|
|
// But esp_intr_noniram_enable has to be called on the same CPU which
|
|
|
|
// called esp_intr_noniram_disable
|
|
|
|
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
2016-09-12 10:54:45 +00:00
|
|
|
xTaskResumeAll();
|
|
|
|
}
|
2016-08-24 10:01:01 +00:00
|
|
|
// Release API lock
|
2016-10-19 09:08:05 +00:00
|
|
|
spi_flash_op_unlock();
|
2016-08-24 10:01:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 19:01:40 +00:00
|
|
|
void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
|
2016-12-21 23:56:23 +00:00
|
|
|
{
|
|
|
|
const uint32_t cpuid = xPortGetCoreID();
|
|
|
|
const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
|
|
|
|
|
|
|
|
// do not care about other CPU, it was halted upon entering panic handler
|
|
|
|
spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
|
|
|
|
// Kill interrupts that aren't located in IRAM
|
|
|
|
esp_intr_noniram_disable();
|
|
|
|
// Disable cache on this CPU as well
|
|
|
|
spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:01:40 +00:00
|
|
|
void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os()
|
2016-12-21 23:56:23 +00:00
|
|
|
{
|
|
|
|
const uint32_t cpuid = xPortGetCoreID();
|
|
|
|
|
|
|
|
// Re-enable cache on this CPU
|
|
|
|
spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
|
|
|
|
// Re-enable non-iram interrupts
|
|
|
|
esp_intr_noniram_enable();
|
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
#else // CONFIG_FREERTOS_UNICORE
|
2016-08-24 10:01:01 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void spi_flash_init_lock()
|
2016-08-24 10:01:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void spi_flash_op_lock()
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
vTaskSuspendAll();
|
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void spi_flash_op_unlock()
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
xTaskResumeAll();
|
|
|
|
}
|
|
|
|
|
2016-08-24 10:01:01 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu()
|
2016-09-13 07:16:36 +00:00
|
|
|
{
|
2016-10-19 09:08:05 +00:00
|
|
|
spi_flash_op_lock();
|
2017-02-21 13:57:53 +00:00
|
|
|
esp_intr_noniram_disable();
|
2016-10-19 09:08:05 +00:00
|
|
|
spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
|
2016-09-13 07:16:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu()
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2016-10-19 09:08:05 +00:00
|
|
|
spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
|
2016-11-25 09:33:51 +00:00
|
|
|
esp_intr_noniram_enable();
|
2017-02-21 13:57:53 +00:00
|
|
|
spi_flash_op_unlock();
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 19:01:40 +00:00
|
|
|
void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os()
|
2016-12-21 23:56:23 +00:00
|
|
|
{
|
|
|
|
// Kill interrupts that aren't located in IRAM
|
|
|
|
esp_intr_noniram_disable();
|
|
|
|
// Disable cache on this CPU as well
|
|
|
|
spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:01:40 +00:00
|
|
|
void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os()
|
2016-12-21 23:56:23 +00:00
|
|
|
{
|
|
|
|
// Re-enable cache on this CPU
|
|
|
|
spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
|
|
|
|
// Re-enable non-iram interrupts
|
|
|
|
esp_intr_noniram_enable();
|
|
|
|
}
|
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
#endif // CONFIG_FREERTOS_UNICORE
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2016-10-19 09:08:05 +00:00
|
|
|
/**
|
|
|
|
* The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
|
|
|
|
* function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
|
|
|
|
* Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
|
|
|
|
*/
|
2016-09-12 10:54:45 +00:00
|
|
|
|
2016-09-13 04:47:21 +00:00
|
|
|
static const uint32_t cache_mask = DPORT_APP_CACHE_MASK_OPSDRAM | DPORT_APP_CACHE_MASK_DROM0 |
|
|
|
|
DPORT_APP_CACHE_MASK_DRAM1 | DPORT_APP_CACHE_MASK_IROM0 |
|
|
|
|
DPORT_APP_CACHE_MASK_IRAM1 | DPORT_APP_CACHE_MASK_IRAM0;
|
|
|
|
|
2016-09-12 10:54:45 +00:00
|
|
|
static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state)
|
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
if (cpuid == 0) {
|
2017-05-08 12:03:04 +00:00
|
|
|
ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
|
|
|
|
while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
|
2016-09-12 10:54:45 +00:00
|
|
|
;
|
|
|
|
}
|
2017-05-08 12:03:04 +00:00
|
|
|
DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
|
2016-09-12 10:54:45 +00:00
|
|
|
} else {
|
2017-05-08 12:03:04 +00:00
|
|
|
ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
|
|
|
|
while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
|
2016-09-12 10:54:45 +00:00
|
|
|
;
|
|
|
|
}
|
2017-05-08 12:03:04 +00:00
|
|
|
DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
|
2016-09-12 10:54:45 +00:00
|
|
|
}
|
|
|
|
*saved_state = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
|
|
|
|
{
|
|
|
|
if (cpuid == 0) {
|
2017-05-08 12:03:04 +00:00
|
|
|
DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
|
|
|
|
DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
|
2016-09-12 10:54:45 +00:00
|
|
|
} else {
|
2017-05-08 12:03:04 +00:00
|
|
|
DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
|
|
|
|
DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
|
2016-09-12 10:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 08:40:05 +00:00
|
|
|
|
2017-03-02 04:21:03 +00:00
|
|
|
|
|
|
|
IRAM_ATTR bool spi_flash_cache_enabled()
|
|
|
|
{
|
2017-05-08 12:03:04 +00:00
|
|
|
return DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE)
|
|
|
|
&& DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE);
|
2017-03-02 04:21:03 +00:00
|
|
|
}
|