]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R5_UltraScale_MPSoC/RTOSDemo_R5_bsp/psu_cortexr5_0/libsrc/standalone_v5_4/src/mpu.c
Correct alignment issue in GCC and RVDS Cortex-A9 port that was preventing full float...
[freertos] / FreeRTOS / Demo / CORTEX_R5_UltraScale_MPSoC / RTOSDemo_R5_bsp / psu_cortexr5_0 / libsrc / standalone_v5_4 / src / mpu.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2014 - 2016 Xilinx, Inc. All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 * @file mpu.c
35 *
36 * This file contains initial configuration of the MPU.
37 *
38 * <pre>
39 * MODIFICATION HISTORY:
40 *
41 * Ver   Who  Date     Changes
42 * ----- ---- -------- ---------------------------------------------------
43 * 5.00  pkp  02/20/14 First release
44 * 5.04  pkp  12/18/15 Updated MPU initialization as per the proper address map
45 * </pre>
46 *
47 * @note
48 *
49 * None.
50 *
51 ******************************************************************************/
52 /***************************** Include Files *********************************/
53
54 #include "xil_types.h"
55 #include "xreg_cortexr5.h"
56 #include "xil_mpu.h"
57 #include "xpseudo_asm.h"
58 #include "xparameters.h"
59
60 /***************** Macros (Inline Functions) Definitions *********************/
61
62 /**************************** Type Definitions *******************************/
63
64 /************************** Constant Definitions *****************************/
65
66 /************************** Variable Definitions *****************************/
67
68 static const struct {
69         u64 size;
70         unsigned int encoding;
71 }region_size[] = {
72         { 0x20, REGION_32B },
73         { 0x40, REGION_64B },
74         { 0x80, REGION_128B },
75         { 0x100, REGION_256B },
76         { 0x200, REGION_512B },
77         { 0x400, REGION_1K },
78         { 0x800, REGION_2K },
79         { 0x1000, REGION_4K },
80         { 0x2000, REGION_8K },
81         { 0x4000, REGION_16K },
82         { 0x8000, REGION_32K },
83         { 0x10000, REGION_64K },
84         { 0x20000, REGION_128K },
85         { 0x40000, REGION_256K },
86         { 0x80000, REGION_512K },
87         { 0x100000, REGION_1M },
88         { 0x200000, REGION_2M },
89         { 0x400000, REGION_4M },
90         { 0x800000, REGION_8M },
91         { 0x1000000, REGION_16M },
92         { 0x2000000, REGION_32M },
93         { 0x4000000, REGION_64M },
94         { 0x8000000, REGION_128M },
95         { 0x10000000, REGION_256M },
96         { 0x20000000, REGION_512M },
97         { 0x40000000, REGION_1G },
98         { 0x80000000, REGION_2G },
99         { 0x100000000, REGION_4G },
100 };
101
102 /************************** Function Prototypes ******************************/
103 void Init_MPU(void);
104 static void Xil_SetAttribute(u32 addr, u32 reg_size,s32 reg_num, u32 attrib);
105 static void Xil_DisableMPURegions(void);
106
107 /*****************************************************************************
108 *
109 * Initialize MPU for a given address map and Enabled the background Region in
110 * MPU with default memory attributes for rest of address range for Cortex R5
111 * processor.
112 *
113 * @param        None.
114 *
115 * @return       None.
116 *
117 *
118 ******************************************************************************/
119
120 void Init_MPU(void)
121 {
122         u32 Addr;
123         u32 RegSize;
124         u32 Attrib;
125         u32 RegNum = 0, i;
126         u64 size;
127
128         Xil_DisableMPURegions();
129
130         Addr = 0x00000000U;
131 #ifdef  XPAR_PSU_R5_DDR_0_S_AXI_BASEADDR
132         /* If the DDR is present, configure region as per DDR size */
133         size = (XPAR_PSU_R5_DDR_0_S_AXI_HIGHADDR - XPAR_PSU_R5_DDR_0_S_AXI_BASEADDR) + 1;
134         if (size < 0x80000000) {
135                 /* Lookup the size.  */
136                 for (i = 0; i < sizeof region_size / sizeof region_size[0]; i++) {
137                         if (size <= region_size[i].size) {
138                                 RegSize = region_size[i].encoding;
139                                 break;
140                         }
141                 }
142         } else {
143                 /* if the DDR size is > 2GB, truncate it to 2GB */
144                 RegSize = REGION_2G;
145         }
146 #else
147         /* For DDRless system, configure region for TCM */
148         RegSize = REGION_256K;
149 #endif
150         Attrib = NORM_NSHARED_WB_WA | PRIV_RW_USER_RW;
151         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
152         RegNum++;
153
154         /*
155          * 1G of strongly ordered memory from 0x80000000 to 0xBFFFFFFF for PL.
156          * 512 MB - LPD-PL interface
157          * 256 MB - FPD-PL (HPM0) interface
158          * 256 MB - FPD-PL (HPM1) interface
159          */
160         Addr = 0x80000000;
161         RegSize = REGION_1G;
162         Attrib = STRONG_ORDERD_SHARED | PRIV_RW_USER_RW   ;
163         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
164         RegNum++;
165
166         /* 512M of device memory from 0xC0000000 to 0xDFFFFFFF for QSPI */
167         Addr = 0xC0000000U;
168         RegSize = REGION_512M;
169         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
170         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
171         RegNum++;
172
173         /* 256M of device memory from 0xE0000000 to 0xEFFFFFFF for PCIe Low */
174         Addr = 0xE0000000U;
175         RegSize = REGION_256M;
176         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
177         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
178         RegNum++;
179
180         /* 16M of device memory from 0xF8000000 to 0xF8FFFFFF for STM_CORESIGHT */
181         Addr = 0xF8000000U;
182         RegSize = REGION_16M;
183         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
184         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
185         RegNum++;
186
187         /* 1M of device memory from 0xF9000000 to 0xF90FFFFF for RPU_A53_GIC */
188         Addr = 0xF9000000U;
189         RegSize = REGION_1M;
190         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
191         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
192         RegNum++;
193
194         /* 16M of device memory from 0xFD000000 to 0xFDFFFFFF for FPS slaves */
195         Addr = 0xFD000000U;
196         RegSize = REGION_16M;
197         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
198         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
199         RegNum++;
200
201         /* 16M of device memory from 0xFE000000 to 0xFEFFFFFF for Upper LPS slaves */
202         Addr = 0xFE000000U;
203         RegSize = REGION_16M;
204         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
205         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
206         RegNum++;
207
208         /*
209          * 16M of device memory from 0xFF000000 to 0xFFFFFFFF for Lower LPS slaves,
210          * CSU, PMU, TCM, OCM
211          */
212         Addr = 0xFF000000U;
213         RegSize = REGION_16M;
214         Attrib = DEVICE_NONSHARED | PRIV_RW_USER_RW   ;
215         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
216         RegNum++;
217
218         /* 256K of OCM RAM from 0xFFFC0000 to 0xFFFFFFFF marked as normal memory */
219         Addr = 0xFFFC0000U;
220         RegSize = REGION_256K;
221         Attrib = NORM_NSHARED_WB_WA| PRIV_RW_USER_RW  ;
222         Xil_SetAttribute(Addr,RegSize,RegNum, Attrib);
223
224         /* A total of 10 MPU regions are allocated with another 6 being free for users */
225
226 }
227
228 /*****************************************************************************
229 *
230 * Set the memory attributes for a section of memory with starting address addr
231 * of the region size defined by reg_size having attributes attrib of region number
232 * reg_num
233 *
234 * @param        addr is the address for which attributes are to be set.
235 * @param        attrib specifies the attributes for that memory region.
236 * @param        reg_size specifies the size for that memory region.
237 * @param        reg_num specifies the number for that memory region.
238 * @return       None.
239 *
240 *
241 ******************************************************************************/
242 static void Xil_SetAttribute(u32 addr, u32 reg_size,s32 reg_num, u32 attrib)
243 {
244         u32 Local_reg_size = reg_size;
245
246         Local_reg_size = Local_reg_size<<1U;
247         Local_reg_size |= REGION_EN;
248         dsb();
249         mtcp(XREG_CP15_MPU_MEMORY_REG_NUMBER,reg_num);
250         isb();
251         mtcp(XREG_CP15_MPU_REG_BASEADDR,addr);          /* Set base address of a region */
252         mtcp(XREG_CP15_MPU_REG_ACCESS_CTRL,attrib);     /* Set the control attribute */
253         mtcp(XREG_CP15_MPU_REG_SIZE_EN,Local_reg_size); /* set the region size and enable it*/
254         dsb();
255         isb();                                          /* synchronize context on this processor */
256 }
257
258
259 /*****************************************************************************
260 *
261 * Disable all the MPU regions if any of them is enabled
262 *
263 * @param        None.
264 *
265 * @return       None.
266 *
267 *
268 ******************************************************************************/
269 static void Xil_DisableMPURegions(void)
270 {
271         u32 Temp;
272         u32 Index;
273         for (Index = 0; Index <= 15; Index++) {
274                 mtcp(XREG_CP15_MPU_MEMORY_REG_NUMBER,Index);
275                 Temp = mfcp(XREG_CP15_MPU_REG_SIZE_EN);
276                 Temp &= (~REGION_EN);
277                 dsb();
278                 mtcp(XREG_CP15_MPU_REG_SIZE_EN,Temp);
279                 dsb();
280                 isb();
281         }
282
283 }