24
24
#include <soc/spi_struct.h>
25
25
#include <spi_flash_defs.h>
26
26
27
- #if CONFIG_SOC_ESP32
27
+ #if defined( CONFIG_SOC_ESP32 )
28
28
#include "soc/dport_reg.h"
29
29
#include "esp32/rom/cache.h"
30
30
#include "esp32/rom/spi_flash.h"
31
31
#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"
33
36
#endif
34
37
38
+ #include "soc/mmu.h"
39
+
35
40
#include <logging/log.h>
36
41
LOG_MODULE_REGISTER (flash_esp32 , CONFIG_FLASH_LOG_LEVEL );
37
42
@@ -58,6 +63,17 @@ static const struct flash_parameters flash_esp32_parameters = {
58
63
#define ADDRESS_MASK_24BIT 0xFFFFFF
59
64
#define SPI_TIMEOUT_MSEC 500
60
65
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
+
61
77
static inline void flash_esp32_sem_take (const struct device * dev )
62
78
{
63
79
k_sem_take (& DEV_DATA (dev )-> sem , K_FOREVER );
@@ -108,10 +124,12 @@ int configure_read_mode(spi_dev_t *hw,
108
124
return 0 ;
109
125
}
110
126
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 )
112
128
{
113
129
int start [2 ], end [2 ];
114
130
131
+ * out_ptr = NULL ;
132
+
115
133
/* SPI_FLASH_MMAP_DATA */
116
134
start [0 ] = SOC_MMU_DROM0_PAGES_START ;
117
135
end [0 ] = SOC_MMU_DROM0_PAGES_END ;
@@ -126,6 +144,15 @@ static bool IRAM_ATTR flash_esp32_mapped_in_cache(uint32_t phys_page)
126
144
if (DPORT_SEQUENCE_REG_READ (
127
145
(uint32_t )& SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE [i ]) ==
128
146
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
129
156
DPORT_INTERRUPT_RESTORE ();
130
157
return true;
131
158
}
@@ -154,8 +181,10 @@ static void IRAM_ATTR flash_esp32_flush_cache(size_t start_addr, size_t length)
154
181
return ;
155
182
}
156
183
157
- if ( flash_esp32_mapped_in_cache ( page )) {
184
+ const void * vaddr = NULL ;
158
185
186
+ if (flash_esp32_mapped_in_cache (page , & vaddr )) {
187
+ #if defined(CONFIG_SOC_ESP32 )
159
188
#if CONFIG_ESP_SPIRAM
160
189
esp_spiram_writeback_cache ();
161
190
#endif
@@ -164,8 +193,15 @@ static void IRAM_ATTR flash_esp32_flush_cache(size_t start_addr, size_t length)
164
193
esp_rom_Cache_Flush (1 );
165
194
#endif
166
195
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 */
167
202
}
168
203
}
204
+ return ;
169
205
}
170
206
171
207
static int set_read_options (const struct device * dev )
@@ -177,13 +213,13 @@ static int set_read_options(const struct device *dev)
177
213
bool byte_cmd = true;
178
214
uint32_t read_mode = READ_PERI_REG (PERIPHS_SPI_FLASH_CTRL );
179
215
180
- if ((read_mode & SPI_FREAD_QIO ) && (read_mode & SPI_FASTRD_MODE )) {
216
+ if ((read_mode & SPI_FREAD_QIO ) && (read_mode & HOST_FLASH_FASTRD )) {
181
217
spi_ll_enable_mosi (hw , 0 );
182
218
spi_ll_enable_miso (hw , 1 );
183
219
dummy_len = 1 + SPI1_R_QIO_DUMMY_CYCLELEN + SPI1_EXTRA_DUMMIES ;
184
220
addr_len = SPI1_R_QIO_ADDR_BITSLEN + 1 ;
185
221
read_cmd = CMD_FASTRD_QIO ;
186
- } else if (read_mode & SPI_FASTRD_MODE ) {
222
+ } else if (read_mode & HOST_FLASH_FASTRD ) {
187
223
spi_ll_enable_mosi (hw , 0 );
188
224
spi_ll_enable_miso (hw , 1 );
189
225
if (read_mode & SPI_FREAD_DIO ) {
@@ -271,6 +307,10 @@ static int flash_esp32_read(const struct device *dev, off_t address, void *buffe
271
307
const spi_flash_guard_funcs_t * guard = spi_flash_guard_get ();
272
308
uint32_t chip_size = cfg -> chip -> chip_size ;
273
309
310
+ #if defined(CONFIG_SOC_ESP32S2 )
311
+ WRITE_PERI_REG (PERIPHS_SPI_FLASH_CTRL , 0 );
312
+ #endif
313
+
274
314
if (length == 0 ) {
275
315
return 0 ;
276
316
}
@@ -359,7 +399,7 @@ static int read_status(const struct device *dev, uint32_t *status)
359
399
while (ESP_ROM_SPIFLASH_BUSY_FLAG ==
360
400
(status_value & ESP_ROM_SPIFLASH_BUSY_FLAG )) {
361
401
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 );
363
403
364
404
int rc = flash_esp32_wait_cmd_done (cfg -> controller );
365
405
@@ -382,9 +422,15 @@ static int read_status(const struct device *dev, uint32_t *status)
382
422
383
423
static inline bool host_idle (spi_dev_t * hw )
384
424
{
425
+ #if defined(CONFIG_SOC_ESP32 )
385
426
bool idle = spi_flash_ll_host_idle (hw );
386
427
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
388
434
389
435
return idle ;
390
436
}
0 commit comments