Skip to content

Commit 101e1ee

Browse files
ttmutfabiobaltieri
authored andcommitted
soc: adi: max32: Use fixed MPU regions
Default MPU configuration marks whole flash area as cacheable. When reading from an erased section of flash, cache controller may fill cache lines with ECC corrected data. To prevent this, disable caching on storage section so that ECC workaround can be applied during reads and correct data is returned. Signed-off-by: Tahsin Mutlugun <[email protected]>
1 parent 20b93d7 commit 101e1ee

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

soc/adi/max32/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
55
zephyr_include_directories(common)
66
zephyr_sources(soc.c)
77

8+
zephyr_library_sources_ifdef(CONFIG_CPU_HAS_CUSTOM_FIXED_SOC_MPU_REGIONS mpu_regions.c)
9+
810
zephyr_library_sources_ifdef(CONFIG_PM power.c)
911
zephyr_linker_sources_ifdef(CONFIG_SOC_FLASH_MAX32 SECTIONS flash.ld)
1012
if(CONFIG_SOC_MAX78000 OR CONFIG_SOC_MAX78002)

soc/adi/max32/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ config SOC_FAMILY_MAX32_M33
1313
select ARM
1414
select CPU_CORTEX_M_HAS_SYSTICK
1515
select CPU_HAS_ARM_MPU
16+
select CPU_HAS_CUSTOM_FIXED_SOC_MPU_REGIONS
1617
select CPU_HAS_FPU
1718
select CLOCK_CONTROL
1819
select CPU_CORTEX_M33

soc/adi/max32/mpu_regions.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024 Analog Devices, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/devicetree.h>
8+
#include <zephyr/storage/flash_map.h>
9+
#include <zephyr/arch/arm/cortex_m/arm_mpu_mem_cfg.h>
10+
11+
/*
12+
* Define noncacheable flash region attributes using noncacheable SRAM memory
13+
* attribute index.
14+
*/
15+
#define MAX32_FLASH_NON_CACHEABLE(base, size) \
16+
{ \
17+
.rbar = RO_Msk | NON_SHAREABLE_Msk, \
18+
.mair_idx = MPU_MAIR_INDEX_SRAM_NOCACHE, \
19+
.r_limit = REGION_LIMIT_ADDR(base, size), \
20+
}
21+
22+
#define MAX32_MPU_REGION(name, base, attr, size) MPU_REGION_ENTRY(name, (base), attr((base), size))
23+
24+
/*
25+
* The MPU regions are defined in the following way:
26+
* - Cacheable flash region
27+
* - Non-cacheable flash region, i.e., storage area at the end of the flash
28+
* - SRAM region
29+
* If the storage partition is not defined, the flash region spans the whole
30+
* flash.
31+
*/
32+
static const struct arm_mpu_region mpu_regions[] = {
33+
#if FIXED_PARTITION_EXISTS(storage_partition)
34+
#define STORAGE_ADDR (CONFIG_FLASH_BASE_ADDRESS + FIXED_PARTITION_OFFSET(storage_partition))
35+
#define STORAGE_SIZE (FIXED_PARTITION_SIZE(storage_partition) >> 10)
36+
MAX32_MPU_REGION("FLASH", CONFIG_FLASH_BASE_ADDRESS, REGION_FLASH_ATTR,
37+
KB(CONFIG_FLASH_SIZE - STORAGE_SIZE)),
38+
MAX32_MPU_REGION("STORAGE", STORAGE_ADDR, MAX32_FLASH_NON_CACHEABLE, KB(STORAGE_SIZE)),
39+
#else
40+
MAX32_MPU_REGION("FLASH", CONFIG_FLASH_BASE_ADDRESS, REGION_FLASH_ATTR,
41+
KB(CONFIG_FLASH_SIZE)),
42+
#endif
43+
MAX32_MPU_REGION("SRAM", CONFIG_SRAM_BASE_ADDRESS, REGION_RAM_ATTR, KB(CONFIG_SRAM_SIZE)),
44+
};
45+
46+
const struct arm_mpu_config mpu_config = {
47+
.num_regions = ARRAY_SIZE(mpu_regions),
48+
.mpu_regions = mpu_regions,
49+
};

0 commit comments

Comments
 (0)