Skip to content

Commit 6f1d80d

Browse files
joe-aspeedLiPshen5566
authored andcommitted
udc: aspeed: manage TX/RX DMA buffer cache coherency explicitly
RX buffers used NON_CACHED_BSS_ALIGN16 to avoid cache coherency issues, but that non-cached memory region isn't available here anymore. Switch to plain aligned buffers and manage coherency with sys_cache_data_flush_range() before TX DMA and sys_cache_data_invd_range() before reading RX data, in both the legacy and next USB device stacks. Signed-off-by: Joe Wang <[email protected]> Change-Id: I09730e12b0a41e9bfa8cef45383ca669989e73e2
1 parent 56f413c commit 6f1d80d

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

drivers/usb/device/usb_dc_aspeed.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ struct usb_device_ep_data {
195195
usb_dc_ep_callback cb_out;
196196
};
197197

198-
uint8_t rx_dma[AST_UDC_MAX_NUM_EP][RX_DMA_BUFF_SIZE] NON_CACHED_BSS_ALIGN16;
198+
uint8_t rx_dma[AST_UDC_MAX_NUM_EP][RX_DMA_BUFF_SIZE] __aligned(16);
199199
/*
200200
* Endpoint mapping table:
201201
* Each bidirectional endpoint address (e.g., 0x00, 0x80, 0x01, 0x81, 0x02, 0x82, ...)
@@ -1328,6 +1328,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
13281328
LOG_DBG("trigger ep0 tx len: [%d/%d]", tx_len, data_len);
13291329
dev_data.ep_data[0].tx_last = tx_len;
13301330

1331+
sys_cache_data_flush_range(dev_data.ep_data[0].tx_dma, tx_len);
13311332
sys_write32(TO_PHY_ADDR((uintptr_t)dev_data.ep_data[0].tx_dma),
13321333
dev_data.base + ASPEED_USB_EP0_DATA_BUFF);
13331334
aspeed_udc_ep0_tx(tx_len);
@@ -1349,6 +1350,7 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
13491350
if (ret_bytes)
13501351
*ret_bytes = tx_len;
13511352

1353+
sys_cache_data_flush_range((void *)data, tx_len);
13521354
sys_write32(TO_PHY_ADDR((uintptr_t)data), ep_reg + ASPEED_EP_DMA_BUFF);
13531355
sys_write32(EP_TX_LEN(tx_len), ep_reg + ASPEED_EP_DMA_STS);
13541356
sys_write32(EP_TX_LEN(tx_len) | 0x1,
@@ -1517,6 +1519,7 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
15171519
LOG_DBG("Copy data from rx_dma, %s:0x%x",
15181520
"data_len", data_len);
15191521

1522+
sys_cache_data_invd_range(dev_data.ep_data[0].rx_dma, data_len);
15201523
memcpy(data, dev_data.ep_data[0].rx_dma, data_len);
15211524
*read_bytes = data_len;
15221525

@@ -1539,6 +1542,8 @@ int usb_dc_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len,
15391542
}
15401543

15411544
if (byte_to_copy <= RX_DMA_BUFF_SIZE) {
1545+
sys_cache_data_invd_range(dev_data.ep_data[ep_num].rx_dma,
1546+
byte_to_copy);
15421547
memcpy(data, dev_data.ep_data[ep_num].rx_dma,
15431548
byte_to_copy);
15441549
} else {

drivers/usb/udc/udc_aspeed.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ static int aspeed_udc_start_in(const struct device *dev,
251251
tx_len = MIN(buf->len, ep_data->mps);
252252
ep_data->tx_last = tx_len;
253253

254+
sys_cache_data_flush_range(buf->data, tx_len);
255+
254256
if (ep_data->hw_ep == 0) {
255257
sys_write32(TO_PHY_ADDR((uintptr_t)buf->data),
256258
aspeed_udc_base(dev) + ASPEED_USB_EP0_DATA_BUFF);
@@ -471,6 +473,7 @@ static int aspeed_udc_handle_out(const struct device *dev, uint8_t ep)
471473
}
472474

473475
if (data_len != 0) {
476+
sys_cache_data_invd_range(ep_data->rx_dma, data_len);
474477
net_buf_add_mem(buf, ep_data->rx_dma, data_len);
475478
}
476479

@@ -1078,7 +1081,7 @@ static int aspeed_udc_preinit(const struct device *dev)
10781081
ASPEED_UDC_PINCTRL_DT_INST_DEFINE(n); \
10791082
static uint8_t aspeed_udc_rx_dma_##n \
10801083
[DT_INST_PROP(n, num_bidir_endpoints)][RX_DMA_BUFF_SIZE] \
1081-
NON_CACHED_BSS_ALIGN16; \
1084+
__aligned(16); \
10821085
static struct aspeed_udc_ep_data aspeed_udc_ep_data_##n \
10831086
[DT_INST_PROP(n, num_bidir_endpoints)]; \
10841087
static uint8_t aspeed_udc_ep_map_##n[32]; \

0 commit comments

Comments
 (0)