11/*
2- * Copyright (c) 2022-2024 HPMicro
2+ * Copyright (c) 2022-2025 HPMicro
33 *
44 * SPDX-License-Identifier: BSD-3-Clause
55 *
4444 * Keep this option disabled by default, please enable it if the default setting cannot meet
4545 * real requirement of application.
4646 */
47+ #ifndef HPM_SDXC_ALLOC_CACHELINE_ALIGNED_BUF
4748#define HPM_SDXC_ALLOC_CACHELINE_ALIGNED_BUF 0
49+ #endif
4850
4951struct hpm_mmcsd
5052{
@@ -395,7 +397,7 @@ static void hpm_sdmmc_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *r
395397 struct rt_mmcsd_cmd * cmd = req -> cmd ;
396398 struct rt_mmcsd_data * data = cmd -> data ;
397399
398- /* configure command */
400+ /* configure command */
399401 sdxc_cmd .cmd_index = cmd -> cmd_code ;
400402 sdxc_cmd .cmd_argument = cmd -> arg ;
401403 sdxc_cmd .cmd_type = (cmd -> cmd_code == STOP_TRANSMISSION ) ? sdxc_cmd_type_abort_cmd : sdxc_cmd_type_normal_cmd ;
@@ -451,17 +453,17 @@ static void hpm_sdmmc_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *r
451453 adma_config .adma_table = (uint32_t * ) core_local_mem_to_sys_address (BOARD_RUNNING_CORE ,
452454 (uint32_t ) mmcsd -> sdxc_adma2_table );
453455 adma_config .adma_table_words = SDXC_ADMA_TABLE_WORDS ;
454- size_t xfer_buf_addr = (uint32_t )data -> buf ;
456+ rt_size_t xfer_buf_addr = (uint32_t )data -> buf ;
455457 uint32_t xfer_len = data -> blks * data -> blksize ;
456458 if ((req -> data -> flags & DATA_DIR_WRITE ) != 0U )
457459 {
458460 uint32_t write_size = xfer_len ;
459- size_t aligned_start ;
461+ rt_size_t aligned_start ;
460462 uint32_t aligned_size ;
461463#if defined(HPM_SDXC_ALLOC_CACHELINE_ALIGNED_BUF ) && (HPM_SDXC_ALLOC_CACHELINE_ALIGNED_BUF == 1 )
462464 if (!SDXC_IS_CACHELINE_ALIGNED (xfer_buf_addr ) || !SDXC_IS_CACHELINE_ALIGNED (write_size ))
463465#else
464- if ((xfer_buf_addr % 4 != 0 ) && (write_size % 4 != 0 ))
466+ if ((xfer_buf_addr % 4 != 0 ) || (write_size % 4 != 0 ))
465467#endif
466468 {
467469 write_size = SDXC_CACHELINE_ALIGN_UP (xfer_len );
@@ -481,12 +483,10 @@ static void hpm_sdmmc_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *r
481483 sdxc_data .tx_data = (uint32_t const * ) core_local_mem_to_sys_address (BOARD_RUNNING_CORE , xfer_buf_addr );
482484
483485 aligned_start = SDXC_CACHELINE_ALIGN_DOWN (sdxc_data .tx_data );
484- size_t aligned_end = SDXC_CACHELINE_ALIGN_UP ((uint32_t )sdxc_data .tx_data + write_size );
486+ rt_size_t aligned_end = SDXC_CACHELINE_ALIGN_UP ((uint32_t )sdxc_data .tx_data + write_size );
485487 aligned_size = aligned_end - aligned_start ;
486488 }
487- rt_base_t level = rt_hw_interrupt_disable ();
488489 l1c_dc_flush (aligned_start , aligned_size );
489- rt_hw_interrupt_enable (level );
490490 sdxc_data .rx_data = NULL ;
491491 }
492492 else
@@ -503,16 +503,23 @@ static void hpm_sdmmc_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *r
503503 RT_ASSERT (raw_alloc_buf != RT_NULL );
504504 aligned_buf = (uint32_t * ) SDXC_CACHELINE_ALIGN_UP (raw_alloc_buf );
505505 sdxc_data .rx_data = (uint32_t * ) core_local_mem_to_sys_address (BOARD_RUNNING_CORE , (uint32_t ) aligned_buf );
506+ /* Invalidate cache-line for the new allocated buffer */
507+ l1c_dc_invalidate ((uint32_t ) sdxc_data .rx_data , aligned_read_size );
506508 }
507509 else
508510 {
509511 sdxc_data .rx_data = (uint32_t * ) core_local_mem_to_sys_address (BOARD_RUNNING_CORE , xfer_buf_addr );
510- size_t aligned_start = SDXC_CACHELINE_ALIGN_DOWN (sdxc_data .rx_data );
511- size_t aligned_end = SDXC_CACHELINE_ALIGN_UP ((uint32_t )sdxc_data .rx_data + read_size );
512- uint32_t aligned_size = aligned_end - aligned_start ;
513- rt_base_t level = rt_hw_interrupt_disable ();
514- l1c_dc_flush (aligned_start , aligned_size );
515- rt_hw_interrupt_enable (level );
512+ rt_size_t buf_start = (uint32_t ) sdxc_data .rx_data ;
513+ rt_size_t aligned_start = HPM_L1C_CACHELINE_ALIGN_DOWN (buf_start );
514+ rt_size_t end_addr = buf_start + xfer_len ;
515+ /* FLUSH un-cacheline aligned memory region */
516+ if ((buf_start % HPM_L1C_CACHELINE_SIZE ) != 0 ) {
517+ l1c_dc_writeback (aligned_start , HPM_L1C_CACHELINE_SIZE );
518+ }
519+ if ((end_addr % HPM_L1C_CACHELINE_SIZE ) != 0 ) {
520+ uint32_t aligned_tail = HPM_L1C_CACHELINE_ALIGN_DOWN (end_addr );
521+ l1c_dc_writeback (aligned_tail , HPM_L1C_CACHELINE_SIZE );
522+ }
516523 }
517524 sdxc_data .tx_data = RT_NULL ;
518525 }
@@ -571,8 +578,8 @@ static void hpm_sdmmc_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *r
571578 }
572579 else
573580 {
574- size_t aligned_start = SDXC_CACHELINE_ALIGN_DOWN (sdxc_data .rx_data );
575- size_t aligned_end = SDXC_CACHELINE_ALIGN_UP ((uint32_t )sdxc_data .rx_data + read_size );
581+ rt_size_t aligned_start = SDXC_CACHELINE_ALIGN_DOWN (sdxc_data .rx_data );
582+ rt_size_t aligned_end = SDXC_CACHELINE_ALIGN_UP ((uint32_t )sdxc_data .rx_data + read_size );
576583 uint32_t aligned_size = aligned_end - aligned_start ;
577584 rt_base_t level = rt_hw_interrupt_disable ();
578585 l1c_dc_invalidate (aligned_start , aligned_size );
0 commit comments