1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2024, Arm Limited. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  * - Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice, this
11  *    list of conditions and the following disclaimer in the documentation and/or
12  *    other materials provided with the distribution.
13  * - Neither the name of ARM nor the names of its contributors may be used to
14  *    endorse or promote products derived from this software without specific prior
15  *    written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include "target_cfg.h"
31 #include "Driver_MPC.h"
32 #include "device_definition.h"
33 #include "region_defs.h"
34 #include "region.h"
35 #include "cmsis_driver_config.h"
36 #include "target_cfg.h"
37 #include "boot_hal.h"
38 #include "Driver_Flash.h"
39 #include "flash_layout.h"
40 
41 /* Import MPC driver */
42 extern ARM_DRIVER_MPC Driver_MRAM_MPC;
43 extern ARM_DRIVER_MPC Driver_ISRAM0_MPC, Driver_ISRAM1_MPC;
44 extern ARM_DRIVER_MPC Driver_ISRAM2_MPC, Driver_ISRAM3_MPC;
45 extern ARM_DRIVER_FLASH Driver_FLASH0;
46 
47 REGION_DECLARE(Image$$, ER_DATA, $$Base)[];
48 REGION_DECLARE(Image$$, ARM_LIB_HEAP, $$ZI$$Limit)[];
49 REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
50 
51 #if defined(__ICCARM__)
52 #pragma required = ER_DATA$$Base
53 #pragma required = ARM_LIB_HEAP$$Limit
54 #endif
55 
56 /* bootloader platform-specific hw initialization */
boot_platform_init(void)57 int32_t boot_platform_init(void)
58 {
59     int32_t result;
60 
61     ARM_DRIVER_MPC* mpc_data_region2 = &Driver_ISRAM2_MPC;
62     ARM_DRIVER_MPC* mpc_data_region3 = &Driver_ISRAM3_MPC;
63 
64     /* Rever MPC configuration */
65     Driver_MRAM_MPC.Initialize();
66     Driver_MRAM_MPC.ConfigRegion(MPC_MRAM_RANGE_BASE_S,
67                                  MPC_MRAM_RANGE_LIMIT_S,
68                                  ARM_MPC_ATTR_SECURE);
69 
70     mpc_data_region2->Initialize();
71     mpc_data_region2->ConfigRegion(MPC_ISRAM2_RANGE_BASE_S,
72                                    MPC_ISRAM2_RANGE_LIMIT_S,
73                                    ARM_MPC_ATTR_SECURE);
74 
75     mpc_data_region3->Initialize();
76     mpc_data_region3->ConfigRegion(MPC_ISRAM3_RANGE_BASE_S,
77                                    MPC_ISRAM3_RANGE_LIMIT_S,
78                                    ARM_MPC_ATTR_SECURE);
79 
80     /* Add barriers to assure the MPC configuration is done before continue
81      * the execution.
82      */
83     __DSB();
84     __ISB();
85 
86     /* Initialize stack limit register */
87     uint32_t msp_stack_bottom =
88             (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
89 
90     __set_MSPLIM(msp_stack_bottom);
91 
92     result = FLASH_DEV_NAME.Initialize(NULL);
93     if (result != ARM_DRIVER_OK) {
94         return 1;
95     }
96 
97     return 0;
98 }
99