Skip to content

Commit dcf26d7

Browse files
Glauber Maroto Ferreiracfriedt
authored andcommitted
soc: esp32s2: drivers: flash: add support
to host SPI Flash driver. Signed-off-by: Glauber Maroto Ferreira <[email protected]>
1 parent 813a74d commit dcf26d7

File tree

8 files changed

+87
-10
lines changed

8 files changed

+87
-10
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
/drivers/flash/ @nashif @nvlsianpu
243243
/drivers/flash/*b91* @yurvyn
244244
/drivers/flash/*nrf* @nvlsianpu
245+
/drivers/flash/*esp32* @glaubermaroto
245246
/drivers/fpga/ @tgorochowik @kgugala
246247
/drivers/gpio/ @mnkp
247248
/drivers/gpio/*b91* @yurvyn

boards/xtensa/esp32s2_saola/esp32s2_saola.dts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
zephyr,sram = &sram0;
1717
zephyr,console = &uart0;
1818
zephyr,shell-uart = &uart0;
19+
zephyr,flash = &flash0;
1920
};
2021
};
2122

drivers/flash/Kconfig.esp32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ config SOC_FLASH_ESP32
66
default y
77
select FLASH_HAS_DRIVER_ENABLED
88
select FLASH_HAS_PAGE_LAYOUT
9-
depends on SOC_ESP32
9+
depends on SOC_ESP32 || SOC_ESP32S2
1010
help
1111
Enable ESP32 internal flash driver.
1212

drivers/flash/flash_esp32.c

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424
#include <soc/spi_struct.h>
2525
#include <spi_flash_defs.h>
2626

27-
#if CONFIG_SOC_ESP32
27+
#if defined(CONFIG_SOC_ESP32)
2828
#include "soc/dport_reg.h"
2929
#include "esp32/rom/cache.h"
3030
#include "esp32/rom/spi_flash.h"
3131
#include "esp32/spiram.h"
32-
#include "soc/mmu.h"
32+
#elif defined(CONFIG_SOC_ESP32S2)
33+
#include "soc/spi_mem_reg.h"
34+
#include "esp32s2/rom/cache.h"
35+
#include "esp32s2/rom/spi_flash.h"
3336
#endif
3437

38+
#include "soc/mmu.h"
39+
3540
#include <logging/log.h>
3641
LOG_MODULE_REGISTER(flash_esp32, CONFIG_FLASH_LOG_LEVEL);
3742

@@ -58,6 +63,17 @@ static const struct flash_parameters flash_esp32_parameters = {
5863
#define ADDRESS_MASK_24BIT 0xFFFFFF
5964
#define SPI_TIMEOUT_MSEC 500
6065

66+
#if defined(CONFIG_SOC_ESP32)
67+
#define HOST_FLASH_CONTROLLER SPI0
68+
#define HOST_FLASH_RDSR SPI_FLASH_RDSR
69+
#define HOST_FLASH_FASTRD SPI_FASTRD_MODE
70+
#elif defined(CONFIG_SOC_ESP32S2)
71+
#define HOST_FLASH_CONTROLLER SPIMEM0
72+
#define HOST_FLASH_RDSR SPI_MEM_FLASH_RDSR
73+
#define HOST_FLASH_FASTRD SPI_MEM_FASTRD_MODE
74+
#endif
75+
76+
6177
static inline void flash_esp32_sem_take(const struct device *dev)
6278
{
6379
k_sem_take(&DEV_DATA(dev)->sem, K_FOREVER);
@@ -108,10 +124,12 @@ int configure_read_mode(spi_dev_t *hw,
108124
return 0;
109125
}
110126

111-
static bool IRAM_ATTR flash_esp32_mapped_in_cache(uint32_t phys_page)
127+
static bool IRAM_ATTR flash_esp32_mapped_in_cache(uint32_t phys_page, const void **out_ptr)
112128
{
113129
int start[2], end[2];
114130

131+
*out_ptr = NULL;
132+
115133
/* SPI_FLASH_MMAP_DATA */
116134
start[0] = SOC_MMU_DROM0_PAGES_START;
117135
end[0] = SOC_MMU_DROM0_PAGES_END;
@@ -126,6 +144,15 @@ static bool IRAM_ATTR flash_esp32_mapped_in_cache(uint32_t phys_page)
126144
if (DPORT_SEQUENCE_REG_READ(
127145
(uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i]) ==
128146
SOC_MMU_PAGE_IN_FLASH(phys_page)) {
147+
#if !defined(CONFIG_SOC_ESP32)
148+
if (j == 0) { /* SPI_FLASH_MMAP_DATA */
149+
*out_ptr = (const void *)(SOC_MMU_VADDR0_START_ADDR +
150+
SPI_FLASH_MMU_PAGE_SIZE * (i - start[0]));
151+
} else {
152+
*out_ptr = (const void *)(SOC_MMU_VADDR1_FIRST_USABLE_ADDR +
153+
SPI_FLASH_MMU_PAGE_SIZE * (i - start[1]));
154+
}
155+
#endif
129156
DPORT_INTERRUPT_RESTORE();
130157
return true;
131158
}
@@ -154,8 +181,10 @@ static void IRAM_ATTR flash_esp32_flush_cache(size_t start_addr, size_t length)
154181
return;
155182
}
156183

157-
if (flash_esp32_mapped_in_cache(page)) {
184+
const void *vaddr = NULL;
158185

186+
if (flash_esp32_mapped_in_cache(page, &vaddr)) {
187+
#if defined(CONFIG_SOC_ESP32)
159188
#if CONFIG_ESP_SPIRAM
160189
esp_spiram_writeback_cache();
161190
#endif
@@ -164,8 +193,15 @@ static void IRAM_ATTR flash_esp32_flush_cache(size_t start_addr, size_t length)
164193
esp_rom_Cache_Flush(1);
165194
#endif
166195
return;
196+
#else /* CONFIG_SOC_ESP32 */
197+
if (vaddr != NULL) {
198+
esp_rom_Cache_Invalidate_Addr((uint32_t)vaddr,
199+
SPI_FLASH_MMU_PAGE_SIZE);
200+
}
201+
#endif /* CONFIG_SOC_ESP32 */
167202
}
168203
}
204+
return;
169205
}
170206

171207
static int set_read_options(const struct device *dev)
@@ -177,13 +213,13 @@ static int set_read_options(const struct device *dev)
177213
bool byte_cmd = true;
178214
uint32_t read_mode = READ_PERI_REG(PERIPHS_SPI_FLASH_CTRL);
179215

180-
if ((read_mode & SPI_FREAD_QIO) && (read_mode & SPI_FASTRD_MODE)) {
216+
if ((read_mode & SPI_FREAD_QIO) && (read_mode & HOST_FLASH_FASTRD)) {
181217
spi_ll_enable_mosi(hw, 0);
182218
spi_ll_enable_miso(hw, 1);
183219
dummy_len = 1 + SPI1_R_QIO_DUMMY_CYCLELEN + SPI1_EXTRA_DUMMIES;
184220
addr_len = SPI1_R_QIO_ADDR_BITSLEN + 1;
185221
read_cmd = CMD_FASTRD_QIO;
186-
} else if (read_mode & SPI_FASTRD_MODE) {
222+
} else if (read_mode & HOST_FLASH_FASTRD) {
187223
spi_ll_enable_mosi(hw, 0);
188224
spi_ll_enable_miso(hw, 1);
189225
if (read_mode & SPI_FREAD_DIO) {
@@ -271,6 +307,10 @@ static int flash_esp32_read(const struct device *dev, off_t address, void *buffe
271307
const spi_flash_guard_funcs_t *guard = spi_flash_guard_get();
272308
uint32_t chip_size = cfg->chip->chip_size;
273309

310+
#if defined(CONFIG_SOC_ESP32S2)
311+
WRITE_PERI_REG(PERIPHS_SPI_FLASH_CTRL, 0);
312+
#endif
313+
274314
if (length == 0) {
275315
return 0;
276316
}
@@ -359,7 +399,7 @@ static int read_status(const struct device *dev, uint32_t *status)
359399
while (ESP_ROM_SPIFLASH_BUSY_FLAG ==
360400
(status_value & ESP_ROM_SPIFLASH_BUSY_FLAG)) {
361401
WRITE_PERI_REG(PERIPHS_SPI_FLASH_STATUS, 0);
362-
WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_RDSR);
402+
WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, HOST_FLASH_RDSR);
363403

364404
int rc = flash_esp32_wait_cmd_done(cfg->controller);
365405

@@ -382,9 +422,15 @@ static int read_status(const struct device *dev, uint32_t *status)
382422

383423
static inline bool host_idle(spi_dev_t *hw)
384424
{
425+
#if defined(CONFIG_SOC_ESP32)
385426
bool idle = spi_flash_ll_host_idle(hw);
386427

387-
idle &= spi_flash_ll_host_idle(&SPI0);
428+
idle &= spi_flash_ll_host_idle(&HOST_FLASH_CONTROLLER);
429+
#elif defined(CONFIG_SOC_ESP32S2)
430+
bool idle = spimem_flash_ll_host_idle((spi_mem_dev_t *)hw);
431+
432+
idle &= spimem_flash_ll_host_idle(&HOST_FLASH_CONTROLLER);
433+
#endif
388434

389435
return idle;
390436
}

dts/xtensa/espressif/esp32s2.dtsi

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
chosen {
1717
zephyr,entropy = &trng0;
18+
zephyr,flash-controller = &flash;
1819
};
1920

2021
cpus {
@@ -64,6 +65,23 @@
6465
status = "ok";
6566
};
6667

68+
flash: flash-controller@3f402000 {
69+
compatible = "espressif,esp32-flash-controller";
70+
label = "FLASH_CTRL";
71+
reg = <0x3f402000 0x1000>;
72+
73+
#address-cells = <1>;
74+
#size-cells = <1>;
75+
76+
flash0: flash@0 {
77+
compatible = "soc-nv-flash";
78+
label = "FLASH_ESP32S2";
79+
reg = <0 0x400000>;
80+
erase-block-size = <4096>;
81+
write-block-size = <4>;
82+
};
83+
};
84+
6785
uart0: uart@3f400000 {
6886
compatible = "espressif,esp32-uart";
6987
reg = <0x3f400000 0x400>;

soc/xtensa/esp32s2/linker.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ _net_buf_pool_list = _esp_net_buf_pool_list;
220220
*libzephyr.a:log_core.*(.rodata .rodata.*)
221221
*libzephyr.a:log_backend_uart.*(.rodata .rodata.*)
222222
*libzephyr.a:log_output.*(.rodata .rodata.*)
223+
*libdrivers__flash.a:flash_esp32.*(.rodata .rodata.*)
223224

224225
. = ALIGN(4);
225226
__esp_log_const_start = .;
@@ -310,6 +311,7 @@ __shell_root_cmds_end = __esp_shell_root_cmds_end;
310311
*libsoc.a:rtc_*.*(.literal .text .literal.* .text.*)
311312
*libsoc.a:cpu_util.*(.literal .text .literal.* .text.*)
312313
*libgcc.a:lib2funcs.*(.literal .text .literal.* .text.*)
314+
*libdrivers__flash.a:flash_esp32.*(.literal .text .literal.* .text.*)
313315
*libzephyr.a:windowspill_asm.*(.literal .text .literal.* .text.*)
314316
*libzephyr.a:log_noos.*(.literal .text .literal.* .text.*)
315317
*libdrivers__timer.a:xtensa_sys_timer.*(.literal .text .literal.* .text.*)

soc/xtensa/esp32s2/soc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "esp_private/system_internal.h"
2121
#include "esp32s2/rom/cache.h"
2222
#include "soc/gpio_periph.h"
23+
#include "esp_spi_flash.h"
2324
#include "hal/cpu_ll.h"
2425
#include "esp_err.h"
2526
#include "sys/printk.h"
@@ -118,6 +119,9 @@ void __attribute__((section(".iram1"))) __start(void)
118119
*wdt_rtc_protect = 0;
119120
#endif
120121

122+
#if CONFIG_SOC_FLASH_ESP32
123+
spi_flash_guard_set(&g_flash_guard_default_ops);
124+
#endif
121125
esp_intr_initialize();
122126
/* Start Zephyr */
123127
z_cstart();

soc/xtensa/esp32s2/soc.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ extern void esp_rom_Cache_Set_ICache_Mode(cache_size_t cache_size, cache_ways_t
4242
cache_line_size_t cache_line_size);
4343

4444
extern void esp_rom_Cache_Invalidate_ICache_All(void);
45-
void esp_rom_Cache_Resume_ICache(uint32_t autoload);
45+
extern void esp_rom_Cache_Resume_ICache(uint32_t autoload);
46+
extern int esp_rom_Cache_Invalidate_Addr(uint32_t addr, uint32_t size);
47+
48+
/* ROM information related to SPI Flash chip timing and device */
49+
extern esp_rom_spiflash_chip_t g_rom_flashchip;
50+
extern uint8_t g_rom_spiflash_dummy_len_plus[];
4651

4752
extern uint32_t esp_rom_g_ticks_per_us_pro;
4853

0 commit comments

Comments
 (0)