|
33 | 33 | /* MPU configuration. */
|
34 | 34 | void BOARD_ConfigMPU(void)
|
35 | 35 | {
|
| 36 | + #if defined(__CC_ARM) || defined(__ARMCC_VERSION) |
| 37 | + extern uint32_t Image$$RW_m_ncache$$Base[]; |
| 38 | + /* RW_m_ncache_unused is a auxiliary region which is used to get the whole size of noncache section */ |
| 39 | + extern uint32_t Image$$RW_m_ncache_unused$$Base[]; |
| 40 | + extern uint32_t Image$$RW_m_ncache_unused$$ZI$$Limit[]; |
| 41 | + uint32_t nonCacheStart = (uint32_t)Image$$RW_m_ncache$$Base; |
| 42 | + uint32_t size = ((uint32_t)Image$$RW_m_ncache_unused$$Base == nonCacheStart) ? |
| 43 | + 0 : |
| 44 | + ((uint32_t)Image$$RW_m_ncache_unused$$ZI$$Limit - nonCacheStart); |
| 45 | +#elif defined(__MCUXPRESSO) |
| 46 | + extern uint32_t __base_NCACHE_REGION; |
| 47 | + extern uint32_t __top_NCACHE_REGION; |
| 48 | + uint32_t nonCacheStart = (uint32_t)(&__base_NCACHE_REGION); |
| 49 | + uint32_t size = (uint32_t)(&__top_NCACHE_REGION) - nonCacheStart; |
| 50 | +#elif defined(__ICCARM__) || defined(__GNUC__) |
| 51 | + extern uint32_t __NCACHE_REGION_START[]; |
| 52 | + extern uint32_t __NCACHE_REGION_SIZE[]; |
| 53 | + uint32_t nonCacheStart = (uint32_t)__NCACHE_REGION_START; |
| 54 | + uint32_t size = (uint32_t)__NCACHE_REGION_SIZE; |
| 55 | +#endif |
| 56 | + volatile uint32_t i = 0; |
| 57 | + |
36 | 58 | /* Disable I cache and D cache */
|
37 | 59 | if (SCB_CCR_IC_Msk == (SCB_CCR_IC_Msk & SCB->CCR))
|
38 | 60 | {
|
@@ -85,59 +107,69 @@ void BOARD_ConfigMPU(void)
|
85 | 107 | * mpu_armv7.h.
|
86 | 108 | */
|
87 | 109 |
|
88 |
| - /* Region 0 setting: Memory with Device type, not shareable, non-cacheable. */ |
89 |
| - MPU->RBAR = ARM_MPU_RBAR(0, 0xC0000000U); |
90 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB); |
| 110 | + /* |
| 111 | + * Add default region to deny access to whole address space to workaround speculative prefetch. |
| 112 | + * Refer to Arm errata 1013783-B for more details. |
| 113 | + * |
| 114 | + */ |
| 115 | + /* Region 0 setting: Instruction access disabled, No data access permission. */ |
| 116 | + MPU->RBAR = ARM_MPU_RBAR(0, 0x00000000U); |
| 117 | + MPU->RASR = ARM_MPU_RASR(1, ARM_MPU_AP_NONE, 0, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_4GB); |
91 | 118 |
|
92 |
| - /* Region 1 setting: Memory with Device type, not shareable, non-cacheable. */ |
| 119 | + /* Region 1 setting: Memory with Device type, not shareable, non-cacheable. */ |
93 | 120 | MPU->RBAR = ARM_MPU_RBAR(1, 0x80000000U);
|
94 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB); |
| 121 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB); |
95 | 122 |
|
96 |
| -/* Region 2 setting */ |
97 |
| -#if defined(XIP_EXTERNAL_FLASH) && (XIP_EXTERNAL_FLASH == 1) |
98 |
| - /* Setting Memory with Normal type, not shareable, outer/inner write back. */ |
| 123 | + /* Region 2 setting: Memory with Device type, not shareable, non-cacheable. */ |
99 | 124 | MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
|
| 125 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB); |
| 126 | + |
| 127 | +#if defined(XIP_EXTERNAL_FLASH) && (XIP_EXTERNAL_FLASH == 1) |
| 128 | + /* Region 3 setting: Memory with Normal type, not shareable, outer/inner write back. */ |
| 129 | + MPU->RBAR = ARM_MPU_RBAR(3, 0x60000000U); |
100 | 130 | MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_8MB);
|
101 |
| -#else |
102 |
| - /* Setting Memory with Device type, not shareable, non-cacheable. */ |
103 |
| - MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U); |
104 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_8MB); |
105 | 131 | #endif
|
106 | 132 |
|
107 |
| - /* Region 3 setting: Memory with Device type, not shareable, non-cacheable. */ |
108 |
| - MPU->RBAR = ARM_MPU_RBAR(3, 0x00000000U); |
109 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB); |
110 |
| - |
111 |
| - /* Region 4 setting: Memory with Normal type, not shareable, outer/inner write back */ |
| 133 | + /* Region 4 setting: Memory with Device type, not shareable, non-cacheable. */ |
112 | 134 | MPU->RBAR = ARM_MPU_RBAR(4, 0x00000000U);
|
113 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB); |
| 135 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB); |
114 | 136 |
|
115 | 137 | /* Region 5 setting: Memory with Normal type, not shareable, outer/inner write back */
|
116 |
| - MPU->RBAR = ARM_MPU_RBAR(5, 0x20000000U); |
| 138 | + MPU->RBAR = ARM_MPU_RBAR(5, 0x00000000U); |
117 | 139 | MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
|
118 | 140 |
|
119 | 141 | /* Region 6 setting: Memory with Normal type, not shareable, outer/inner write back */
|
120 |
| - MPU->RBAR = ARM_MPU_RBAR(6, 0x20200000U); |
121 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_128KB); |
| 142 | + MPU->RBAR = ARM_MPU_RBAR(6, 0x20000000U); |
| 143 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB); |
122 | 144 |
|
123 |
| -/* The define sets the cacheable memory to shareable, |
124 |
| - * this suggestion is referred from chapter 2.2.1 Memory regions, |
125 |
| - * types and attributes in Cortex-M7 Devices, Generic User Guide */ |
126 |
| -#if defined(SDRAM_IS_SHAREABLE) |
127 |
| - /* Region 7 setting: Memory with Normal type, shareable, outer/inner write back */ |
128 |
| - MPU->RBAR = ARM_MPU_RBAR(7, 0x80000000U); |
129 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 1, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB); |
130 |
| -#else |
131 | 145 | /* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back */
|
132 |
| - MPU->RBAR = ARM_MPU_RBAR(7, 0x80000000U); |
| 146 | + MPU->RBAR = ARM_MPU_RBAR(7, 0x20200000U); |
| 147 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_128KB); |
| 148 | + |
| 149 | + /* Region 8 setting: Memory with Normal type, not shareable, outer/inner write back */ |
| 150 | + MPU->RBAR = ARM_MPU_RBAR(8, 0x80000000U); |
133 | 151 | MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
|
134 |
| -#endif |
135 | 152 |
|
136 |
| - /* Region 8 setting, set last 2MB of SDRAM can't be accessed by cache, glocal variables which are not expected to be |
137 |
| - * accessed by cache can be put here */ |
138 |
| - /* Memory with Normal type, not shareable, non-cacheable */ |
139 |
| - MPU->RBAR = ARM_MPU_RBAR(8, 0x81E00000U); |
140 |
| - MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_2MB); |
| 153 | + while ((size >> i) > 0x1U) |
| 154 | + { |
| 155 | + i++; |
| 156 | + } |
| 157 | + |
| 158 | + if (i != 0) |
| 159 | + { |
| 160 | + /* The MPU region size should be 2^N, 5<=N<=32, region base should be multiples of size. */ |
| 161 | + assert(!(nonCacheStart % size)); |
| 162 | + assert(size == (uint32_t)(1 << i)); |
| 163 | + assert(i >= 5); |
| 164 | + |
| 165 | + /* Region 9 setting: Memory with Normal type, not shareable, non-cacheable */ |
| 166 | + MPU->RBAR = ARM_MPU_RBAR(9, nonCacheStart); |
| 167 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 0, 0, 0, i - 1); |
| 168 | + } |
| 169 | + |
| 170 | + /* Region 10 setting: Memory with Device type, not shareable, non-cacheable */ |
| 171 | + MPU->RBAR = ARM_MPU_RBAR(10, 0x40000000); |
| 172 | + MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_4MB); |
141 | 173 |
|
142 | 174 | /* Enable MPU */
|
143 | 175 | ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
|
|
0 commit comments