Skip to content

Commit 92fa83f

Browse files
swift-tkkartben
authored andcommitted
soc: ambiq: add common cache handling for apollo5x soc
The buf_in_nocache function is to be used by various device drivers to check if buffer is in noncacheable region. The cacheable DMA buffer shall be put into section .ambiq_dma_buff due to certain restrictions of the SoC. Signed-off-by: Swift Tian <[email protected]>
1 parent 5d6d876 commit 92fa83f

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

soc/ambiq/apollo5x/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ zephyr_include_directories(.)
88
zephyr_sources_ifdef(CONFIG_PM power.c)
99
zephyr_include_directories(${ZEPHYR_BASE}/soc/arm/common/cortex_m)
1010

11+
zephyr_linker_sources(SECTIONS shared_ram.ld)
12+
1113
set(SOC_LINKER_SCRIPT ${ZEPHYR_BASE}/include/zephyr/arch/arm/cortex_m/scripts/linker.ld CACHE INTERNAL "")

soc/ambiq/apollo5x/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ config SOC_SERIES_APOLLO5X
1919
select HAS_PM
2020
select SOC_EARLY_INIT_HOOK
2121
select REQUIRES_FULL_LIBC
22+
23+
config SOC_AMBIQ_DCACHE_SIZE
24+
int
25+
default 65536 if SOC_APOLLO510
26+
27+
config SOC_AMBIQ_DMA_BUFF_LOCATION
28+
hex "Byte offset to SRAM_BASE_ADDRESS"
29+
default 0x50000
30+
help
31+
This option specifies the cacheable DMA buffers' start address
32+
33+
config SOC_AMBIQ_DMA_BUFF_ALIGNMENT
34+
int "Byte alignment of the DMA buffer"
35+
default DCACHE_LINE_SIZE if DCACHE
36+
default 1
37+
help
38+
This option specifies the DMA buffers' alignment

soc/ambiq/apollo5x/Kconfig.defconfig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ rsource "Kconfig.defconfig.apollo5*"
99
config CACHE_MANAGEMENT
1010
default y
1111

12-
config AMBIQ_CACHEABLE_DMA_BUFFER_ALIGNMENT
13-
int
14-
default DCACHE_LINE_SIZE
12+
config DCACHE_LINE_SIZE
13+
default 32
14+
15+
config DCACHE
16+
default y
17+
18+
config ICACHE
19+
default y
1520

1621
# Need to enlarge the IDLE stack size because the power
1722
# management operations are executed in the idle task

soc/ambiq/apollo5x/shared_ram.ld

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2025 Ambiq Micro Inc. <www.ambiq.com>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
SECTION_PROLOGUE (ambiq_dma_buff, CONFIG_SRAM_BASE_ADDRESS + CONFIG_SOC_AMBIQ_DMA_BUFF_LOCATION (NOLOAD),)
8+
{
9+
__ambiq_dma_buff_start = .;
10+
KEEP(*(SORT_BY_NAME(".ambiq_dma_buff*")))
11+
. = ALIGN(CONFIG_SOC_AMBIQ_DMA_BUFF_ALIGNMENT);
12+
__ambiq_dma_buff_end = .;
13+
} GROUP_LINK_IN(RAMABLE_REGION)

soc/ambiq/apollo5x/soc.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
#include <zephyr/init.h>
88
#include <zephyr/cache.h>
99
#include <zephyr/logging/log.h>
10-
#include "soc.h"
10+
#include <zephyr/mem_mgmt/mem_attr.h>
11+
#ifdef CONFIG_DCACHE
12+
#include <zephyr/dt-bindings/memory-attr/memory-attr-arm.h>
13+
#endif /* CONFIG_DCACHE */
14+
15+
#ifdef CONFIG_NOCACHE_MEMORY
16+
#include <zephyr/linker/linker-defs.h>
17+
#endif /* CONFIG_NOCACHE_MEMORY */
18+
19+
#include <soc.h>
1120

1221
LOG_MODULE_REGISTER(soc, CONFIG_SOC_LOG_LEVEL);
1322

@@ -48,3 +57,25 @@ void soc_early_init_hook(void)
4857
/* Enable Dcache */
4958
sys_cache_data_enable();
5059
}
60+
61+
#if CONFIG_CACHE_MANAGEMENT
62+
bool buf_in_nocache(uintptr_t buf, size_t len_bytes)
63+
{
64+
bool buf_within_nocache = false;
65+
66+
#if CONFIG_NOCACHE_MEMORY
67+
/* Check if buffer is in nocache region defined by the linker */
68+
buf_within_nocache = (buf >= ((uintptr_t)_nocache_ram_start)) &&
69+
((buf + len_bytes - 1) <= ((uintptr_t)_nocache_ram_end));
70+
if (buf_within_nocache) {
71+
return true;
72+
}
73+
#endif /* CONFIG_NOCACHE_MEMORY */
74+
75+
/* Check if buffer is in nocache memory region defined in DT */
76+
buf_within_nocache = mem_attr_check_buf((void *)buf, len_bytes,
77+
DT_MEM_ARM(ATTR_MPU_RAM_NOCACHE)) == 0;
78+
79+
return buf_within_nocache;
80+
}
81+
#endif

soc/ambiq/apollo5x/soc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99

1010
#include <am_mcu_apollo.h>
1111

12+
bool buf_in_nocache(uintptr_t buf, size_t len_bytes);
13+
1214
#endif /* __SOC_H__ */

0 commit comments

Comments
 (0)