10
10
#define FLASH_WRITE_BLK_SZ DT_PROP(SOC_NV_FLASH_NODE, write_block_size)
11
11
#define FLASH_ERASE_BLK_SZ DT_PROP(SOC_NV_FLASH_NODE, erase_block_size)
12
12
13
+ /*
14
+ * HAL includes go first to
15
+ * avoid BIT macro redefinition
16
+ */
17
+ #include <esp_spi_flash.h>
18
+ #include <hal/spi_ll.h>
19
+ #include <hal/spi_flash_ll.h>
20
+ #include <hal/spi_flash_hal.h>
21
+ #include <soc/spi_struct.h>
22
+ #include <spi_flash_defs.h>
23
+
13
24
#include <kernel.h>
14
25
#include <device.h>
15
26
#include <stddef.h>
16
27
#include <string.h>
17
28
#include <errno.h>
18
29
#include <drivers/flash.h>
19
30
#include <soc.h>
20
- #include <esp_spi_flash.h>
21
- #include <hal/spi_ll.h>
22
- #include <hal/spi_flash_ll.h>
23
- #include <hal/spi_flash_hal.h>
24
- #include <soc/spi_struct.h>
25
- #include <spi_flash_defs.h>
26
31
27
32
#if defined(CONFIG_SOC_ESP32 )
28
33
#include "soc/dport_reg.h"
33
38
#include "soc/spi_mem_reg.h"
34
39
#include "esp32s2/rom/cache.h"
35
40
#include "esp32s2/rom/spi_flash.h"
41
+ #elif defined(CONFIG_SOC_ESP32C3 )
42
+ #include "soc/spi_periph.h"
43
+ #include "soc/spi_mem_reg.h"
44
+ #include "soc/dport_access.h"
45
+ #include "esp32c3/dport_access.h"
46
+ #include "esp32c3/rom/cache.h"
47
+ #include "esp32c3/rom/spi_flash.h"
36
48
#endif
37
49
38
50
#include "soc/mmu.h"
@@ -56,7 +68,15 @@ static const struct flash_parameters flash_esp32_parameters = {
56
68
57
69
#define DEV_DATA (dev ) ((struct flash_esp32_dev_data *const)(dev)->data)
58
70
#define DEV_CFG (dev ) ((const struct flash_esp32_dev_config *const)(dev)->config)
71
+
72
+ #if !defined(CONFIG_SOC_ESP32C3 )
59
73
#define SPI1_EXTRA_DUMMIES (g_rom_spiflash_dummy_len_plus[1])
74
+ #else
75
+ #define SPI1_EXTRA_DUMMIES ((uint8_t)((rom_spiflash_legacy_data->dummy_len_plus)[1]))
76
+ #define SPI_FREAD_QIO 0
77
+ #define SPI_FREAD_DIO 0
78
+ #endif
79
+
60
80
#define MAX_BUFF_ALLOC_RETRIES 5
61
81
#define MAX_READ_CHUNK 16384
62
82
#define MAX_WRITE_CHUNK 8192
@@ -67,12 +87,17 @@ static const struct flash_parameters flash_esp32_parameters = {
67
87
#define HOST_FLASH_CONTROLLER SPI0
68
88
#define HOST_FLASH_RDSR SPI_FLASH_RDSR
69
89
#define HOST_FLASH_FASTRD SPI_FASTRD_MODE
70
- #elif defined(CONFIG_SOC_ESP32S2 )
90
+ #elif defined(CONFIG_SOC_ESP32S2 ) || defined( CONFIG_SOC_ESP32C3 )
71
91
#define HOST_FLASH_CONTROLLER SPIMEM0
72
92
#define HOST_FLASH_RDSR SPI_MEM_FLASH_RDSR
73
93
#define HOST_FLASH_FASTRD SPI_MEM_FASTRD_MODE
74
94
#endif
75
95
96
+ #if defined(CONFIG_SOC_ESP32C3 )
97
+ static esp_rom_spiflash_chip_t esp_flashchip_info ;
98
+ #else
99
+ #define esp_flashchip_info g_rom_flashchip
100
+ #endif
76
101
77
102
static inline void flash_esp32_sem_take (const struct device * dev )
78
103
{
@@ -307,7 +332,7 @@ static int flash_esp32_read(const struct device *dev, off_t address, void *buffe
307
332
const spi_flash_guard_funcs_t * guard = spi_flash_guard_get ();
308
333
uint32_t chip_size = cfg -> chip -> chip_size ;
309
334
310
- #if defined(CONFIG_SOC_ESP32S2 )
335
+ #if defined(CONFIG_SOC_ESP32S2 ) || defined( CONFIG_SOC_ESP32C3 )
311
336
WRITE_PERI_REG (PERIPHS_SPI_FLASH_CTRL , 0 );
312
337
#endif
313
338
@@ -426,7 +451,7 @@ static inline bool host_idle(spi_dev_t *hw)
426
451
bool idle = spi_flash_ll_host_idle (hw );
427
452
428
453
idle &= spi_flash_ll_host_idle (& HOST_FLASH_CONTROLLER );
429
- #elif defined(CONFIG_SOC_ESP32S2 )
454
+ #elif defined(CONFIG_SOC_ESP32S2 ) || defined( CONFIG_SOC_ESP32C3 )
430
455
bool idle = spimem_flash_ll_host_idle ((spi_mem_dev_t * )hw );
431
456
432
457
idle &= spimem_flash_ll_host_idle (& HOST_FLASH_CONTROLLER );
@@ -458,7 +483,6 @@ static int wait_idle(const struct device *dev)
458
483
static int write_protect (const struct device * dev , bool write_protect )
459
484
{
460
485
const struct flash_esp32_dev_config * const cfg = DEV_CFG (dev );
461
- uint32_t flash_status = 0 ;
462
486
463
487
wait_idle (dev );
464
488
@@ -470,12 +494,14 @@ static int write_protect(const struct device *dev, bool write_protect)
470
494
if (rc != 0 ) {
471
495
return rc ;
472
496
}
497
+ #if !defined(CONFIG_SOC_ESP32C3 )
498
+ uint32_t flash_status = 0 ;
473
499
474
500
/* make sure the flash is ready for writing */
475
501
while (ESP_ROM_SPIFLASH_WRENABLE_FLAG != (flash_status & ESP_ROM_SPIFLASH_WRENABLE_FLAG )) {
476
502
read_status (dev , & flash_status );
477
503
}
478
-
504
+ #endif
479
505
return 0 ;
480
506
}
481
507
@@ -670,7 +696,6 @@ static int flash_esp32_write(const struct device *dev,
670
696
guard -> start ();
671
697
flash_esp32_flush_cache (address , length );
672
698
guard -> end ();
673
-
674
699
flash_esp32_sem_give (dev );
675
700
676
701
return rc ;
@@ -777,6 +802,14 @@ static int flash_esp32_init(const struct device *dev)
777
802
{
778
803
struct flash_esp32_dev_data * const dev_data = DEV_DATA (dev );
779
804
805
+ #if defined(CONFIG_SOC_ESP32C3 )
806
+ spiflash_legacy_data_t * legacy_data = rom_spiflash_legacy_data ;
807
+
808
+ esp_flashchip_info .chip_size = legacy_data -> chip .chip_size ;
809
+ esp_flashchip_info .sector_size = legacy_data -> chip .sector_size ;
810
+ esp_flashchip_info .page_size = legacy_data -> chip .page_size ;
811
+ #endif
812
+
780
813
k_sem_init (& dev_data -> sem , 1 , 1 );
781
814
782
815
return 0 ;
@@ -796,7 +829,7 @@ static struct flash_esp32_dev_data flash_esp32_data;
796
829
797
830
static const struct flash_esp32_dev_config flash_esp32_config = {
798
831
.controller = (spi_dev_t * ) DT_INST_REG_ADDR (0 ),
799
- .chip = & g_rom_flashchip
832
+ .chip = & esp_flashchip_info
800
833
};
801
834
802
835
DEVICE_DT_INST_DEFINE (0 , flash_esp32_init ,
0 commit comments