Skip to content

Commit a88dd32

Browse files
committed
feature(dcd_dwc2): Added cache synchronization
1 parent 0569188 commit a88dd32

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ static bool _sof_en;
6767
//--------------------------------------------------------------------
6868
// DMA
6969
//--------------------------------------------------------------------
70+
// When DMA requires cache synchronization for memory
71+
// Data synchronization: cache to memory
72+
#ifndef dsync_c2m
73+
#define dsync_c2m(_addr, _size)
74+
#endif // dsync_c2m
75+
76+
// Data synchronization: memory to cache
77+
#ifndef dsync_m2c
78+
#define dsync_m2c(_addr, _size)
79+
#endif // dsync_m2c
7080

7181
TU_ATTR_ALWAYS_INLINE static inline bool dma_device_enabled(const dwc2_regs_t* dwc2) {
7282
(void) dwc2;
@@ -359,7 +369,7 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c
359369

360370
if(dma_device_enabled(dwc2)) {
361371
dep->diepdma = (uintptr_t)xfer->buffer;
362-
372+
dsync_c2m(xfer->buffer, total_bytes);
363373
// For ISO endpoint set correct odd/even bit for next frame.
364374
if ((dep->diepctl & DIEPCTL_EPTYP) == DIEPCTL_EPTYP_0 && (XFER_CTL_BASE(epnum, dir))->interval == 1) {
365375
// Take odd/even bit from frame counter.
@@ -397,6 +407,7 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c
397407

398408
if(dma_device_enabled(dwc2)) {
399409
dep->doepdma = (uintptr_t)xfer->buffer;
410+
dsync_c2m(xfer->buffer, total_bytes);
400411
}
401412

402413
dep->doepctl |= DOEPCTL_EPENA | DOEPCTL_CNAK;
@@ -775,6 +786,7 @@ static void handle_epout_irq(uint8_t rhport) {
775786

776787
if(dma_device_enabled(dwc2)) {
777788
dma_setup_prepare(rhport);
789+
dsync_m2c((uint8_t*) _setup_packet, sizeof(_setup_packet));
778790
}
779791

780792
dcd_event_setup_received(rhport, (uint8_t*) _setup_packet, true);
@@ -801,7 +813,7 @@ static void handle_epout_irq(uint8_t rhport) {
801813
if(epnum == 0 && xfer->total_len == 0) {
802814
dma_setup_prepare(rhport);
803815
}
804-
816+
dsync_m2c(xfer->buffer, xfer->total_len);
805817
dcd_event_xfer_complete(rhport, epnum, xfer->total_len, XFER_RESULT_SUCCESS, true);
806818
}
807819
} else {

src/portable/synopsys/dwc2/dwc2_esp32.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@
3838
#include "esp_intr_alloc.h"
3939
#include "soc/periph_defs.h"
4040
#include "soc/usb_wrap_struct.h"
41+
#include "esp_log.h"
42+
43+
#if TU_CHECK_MCU(OPT_MCU_ESP32P4)
44+
#if (CFG_TUD_DWC2_DMA && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE)
45+
#include "sdkconfig.h"
46+
#include "hal/cache_hal.h"
47+
#include "esp_cache.h"
48+
#define DWC2_ENABLE_MEM_CACHE 1
49+
#endif // SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
50+
#endif // OPT_MCU_ESP32P4
51+
4152

4253
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
4354
#define DWC2_FS_REG_BASE 0x60080000UL
@@ -111,6 +122,42 @@ TU_ATTR_ALWAYS_INLINE static inline void dwc2_phy_update(dwc2_regs_t* dwc2, uint
111122
// maybe usb_utmi_hal_disable()
112123
}
113124

125+
// MCU specific cache synchronization call
126+
TU_ATTR_ALWAYS_INLINE static inline void dwc2_dcd_sync_cache_to_memory(void *addr, size_t size) {
127+
#if DWC2_ENABLE_MEM_CACHE
128+
ESP_EARLY_LOGV("dwc2_esp32", "cache to mem sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
129+
int flags = ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED;
130+
if (addr != NULL && size) {
131+
esp_err_t ret = esp_cache_msync(addr, size, flags);
132+
assert(ret == ESP_OK);
133+
}
134+
#else
135+
(void) addr;
136+
(void) size;
137+
// nothing to do
138+
#endif // DWC2_ENABLE_MEM_CACHE
139+
}
140+
141+
TU_ATTR_ALWAYS_INLINE static inline void dwc2_dcd_sync_memory_to_cache(void *addr, size_t size) {
142+
#if DWC2_ENABLE_MEM_CACHE
143+
ESP_EARLY_LOGV("dwc2", "mem to cache sync, addr 0x%"PRIx32", size %d", (uintptr_t)addr, size);
144+
int flags = ESP_CACHE_MSYNC_FLAG_DIR_M2C;
145+
if (addr != NULL && size) {
146+
// TODO: size should be multiply of CONFIG_CACHE_L1_CACHE_LINE_SIZE?
147+
size = (size < CONFIG_CACHE_L1_CACHE_LINE_SIZE)? CONFIG_CACHE_L1_CACHE_LINE_SIZE : size;
148+
esp_err_t ret = esp_cache_msync(addr, size, flags);
149+
assert(ret == ESP_OK);
150+
}
151+
#else
152+
(void) addr;
153+
(void) size;
154+
// nothing to do
155+
#endif // DWC2_ENABLE_MEM_CACHE
156+
}
157+
158+
#define dsync_c2m(_addr, _size) dwc2_dcd_sync_cache_to_memory((_addr), (_size))
159+
#define dsync_m2c(_addr, _size) dwc2_dcd_sync_memory_to_cache((_addr), (_size))
160+
114161
#ifdef __cplusplus
115162
}
116163
#endif

0 commit comments

Comments
 (0)