Skip to content

Commit 7d27051

Browse files
Glauber Maroto Ferreirasylvioalves
authored andcommitted
hal: esp32: spi_flash: adds support for SPI Flash operations
Includes: - SPI Flash guard functions - Common cache state control API Signed-off-by: Glauber Maroto Ferreira <[email protected]>
1 parent 5c53b7c commit 7d27051

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

components/spi_flash/flash_ops.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
#include "esp_attr.h"
4646
#include "esp_timer.h"
4747

48+
#if defined(__ZEPHYR__)
49+
#include "common/cache_utils.h"
50+
#endif
51+
4852
esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_addr, const void *src, size_t size);
4953

5054
/* bytes erased by SPIEraseBlock() ROM function */
@@ -85,6 +89,15 @@ static bool is_safe_write_address(size_t addr, size_t size);
8589
static void spi_flash_os_yield(void);
8690

8791
const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
92+
#if defined(__ZEPHYR__)
93+
.start = esp32_spiflash_start,
94+
.end = esp32_spiflash_end,
95+
.op_lock = 0,
96+
.op_unlock = 0,
97+
#if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
98+
.is_safe_write_address = 0
99+
#endif
100+
#else
88101
.start = spi_flash_disable_interrupts_caches_and_other_cpu,
89102
.end = spi_flash_enable_interrupts_caches_and_other_cpu,
90103
.op_lock = spi_flash_op_lock,
@@ -93,6 +106,7 @@ const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
93106
.is_safe_write_address = is_safe_write_address,
94107
#endif
95108
.yield = spi_flash_os_yield,
109+
#endif // __ZEPHYR__
96110
};
97111

98112
const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#pragma once
7+
8+
#include <zephyr.h>
9+
10+
#define DPORT_CACHE_MASK 0x3f
11+
12+
static uint32_t s_cache_ops_saved_state[2];
13+
static unsigned int s_intr_saved_state;
14+
15+
static void IRAM_ATTR esp32_disable_cache(uint32_t cpuid, uint32_t *saved_state)
16+
{
17+
uint32_t ret = 0;
18+
19+
if (cpuid == PRO_CPU_NUM) {
20+
ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, DPORT_CACHE_MASK, 0);
21+
while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
22+
;
23+
}
24+
DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
25+
} else {
26+
ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, DPORT_CACHE_MASK, 0);
27+
while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
28+
;
29+
}
30+
DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
31+
}
32+
*saved_state = ret;
33+
}
34+
35+
static void IRAM_ATTR esp32_restore_cache(uint32_t cpuid, uint32_t saved_state)
36+
{
37+
if (cpuid == PRO_CPU_NUM) {
38+
DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
39+
DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, DPORT_CACHE_MASK, saved_state, 0);
40+
} else {
41+
DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
42+
DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, DPORT_CACHE_MASK, saved_state, 0);
43+
}
44+
}
45+
46+
void IRAM_ATTR esp32_spiflash_start(void)
47+
{
48+
k_sched_lock();
49+
50+
s_intr_saved_state = irq_lock();
51+
52+
int cpu_id = arch_curr_cpu()->id;
53+
esp32_disable_cache(cpu_id, &s_cache_ops_saved_state[cpu_id]);
54+
55+
#ifdef CONFIG_SMP
56+
int other_cpu = (cpu_id == PRO_CPU_NUM) ? APP_CPU_NUM : PRO_CPU_NUM;
57+
esp32_disable_cache(other_cpu, &s_cache_ops_saved_state[other_cpu]);
58+
#endif
59+
}
60+
61+
void IRAM_ATTR esp32_spiflash_end(void)
62+
{
63+
int cpu_id = arch_curr_cpu()->id;
64+
65+
esp32_restore_cache(cpu_id, s_cache_ops_saved_state[cpu_id]);
66+
67+
#ifdef CONFIG_SMP
68+
int other_cpu = (cpu_id == PRO_CPU_NUM) ? APP_CPU_NUM : PRO_CPU_NUM;
69+
esp32_restore_cache(other_cpu, s_cache_ops_saved_state[other_cpu]);
70+
#endif
71+
irq_unlock(s_intr_saved_state);
72+
73+
k_sched_unlock();
74+
}
75+

0 commit comments

Comments
 (0)