1 /*
2  * Copyright (c) 2017-2022 Arm Limited. All rights reserved.
3  * Copyright (c) 2020 Cypress Semiconductor Corporation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __FLASH_LAYOUT_H__
19 #define __FLASH_LAYOUT_H__
20 
21 /* Flash layout on MPS2 AN521 with BL2 (multiple image boot):
22  *
23  * 0x0000_0000 BL2 - MCUBoot (0.5 MB)
24  * 0x0008_0000 Secure image     primary slot (0.5 MB)
25  * 0x0010_0000 Non-secure image primary slot (0.5 MB)
26  * 0x0018_0000 Secure image     secondary slot (0.5 MB)
27  * 0x0020_0000 Non-secure image secondary slot (0.5 MB)
28  * 0x0028_0000 Scratch area (0.5 MB)
29  * 0x0030_0000 Protected Storage Area (20 KB)
30  * 0x0030_5000 Internal Trusted Storage Area (16 KB)
31  * 0x0030_9000 OTP / NV counters area (8 KB)
32  * 0x0030_B000 Unused (980 KB)
33  *
34  * Flash layout on MPS2 AN521 with BL2 (single image boot):
35  *
36  * 0x0000_0000 BL2 - MCUBoot (0.5 MB)
37  * 0x0008_0000 Primary image area (1 MB):
38  *    0x0008_0000 Secure     image primary
39  *    0x0010_0000 Non-secure image primary
40  * 0x0018_0000 Secondary image area (1 MB):
41  *    0x0018_0000 Secure     image secondary
42  *    0x0020_0000 Non-secure image secondary
43  * 0x0028_0000 Scratch area (1 MB)
44  * 0x0038_0000 Protected Storage Area (20 KB)
45  * 0x0038_5000 Internal Trusted Storage Area (16 KB)
46  * 0x0038_9000 OTP / NV counters area (8 KB)
47  * 0x0038_B000 Unused (468 KB)
48  *
49  * Flash layout on MPS2 AN521, if BL2 not defined:
50  *
51  * 0x0000_0000 Secure     image (1 MB)
52  * 0x0010_0000 Non-secure image (1 MB)
53  */
54 
55 /* This header file is included from linker scatter file as well, where only a
56  * limited C constructs are allowed. Therefore it is not possible to include
57  * here the platform_retarget.h to access flash related defines. To resolve this
58  * some of the values are redefined here with different names, these are marked
59  * with comment.
60  */
61 
62 /* Size of a Secure and of a Non-secure image */
63 #define FLASH_S_PARTITION_SIZE          (0x80000) /* S partition: 512 KB */
64 #define FLASH_NS_PARTITION_SIZE         (0x80000) /* NS partition: 512 KB */
65 
66 #if (FLASH_S_PARTITION_SIZE > FLASH_NS_PARTITION_SIZE)
67 #define FLASH_MAX_PARTITION_SIZE FLASH_S_PARTITION_SIZE
68 #else
69 #define FLASH_MAX_PARTITION_SIZE FLASH_NS_PARTITION_SIZE
70 #endif
71 /* Sector size of the flash hardware; same as FLASH0_SECTOR_SIZE */
72 #define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x1000)     /* 4 KB */
73 /* Same as FLASH0_SIZE */
74 #define FLASH_TOTAL_SIZE                (0x00400000) /* 4 MB */
75 
76 /* Flash layout info for BL2 bootloader */
77 /* Same as FLASH0_BASE_S */
78 #define FLASH_BASE_ADDRESS              (0x10000000)
79 
80 /* Offset and size definitions of the flash partitions that are handled by the
81  * bootloader. The image swapping is done between IMAGE_PRIMARY and
82  * IMAGE_SECONDARY, SCRATCH is used as a temporary storage during image
83  * swapping.
84  */
85 #define FLASH_AREA_BL2_OFFSET      (0x0)
86 #define FLASH_AREA_BL2_SIZE        (0x80000) /* 512 KB */
87 
88 #if !defined(MCUBOOT_IMAGE_NUMBER) || (MCUBOOT_IMAGE_NUMBER == 1)
89 /* Secure + Non-secure image primary slot */
90 #define FLASH_AREA_0_ID            (1)
91 #define FLASH_AREA_0_OFFSET        (FLASH_AREA_BL2_OFFSET + FLASH_AREA_BL2_SIZE)
92 #define FLASH_AREA_0_SIZE          (FLASH_S_PARTITION_SIZE + \
93                                     FLASH_NS_PARTITION_SIZE)
94 /* Secure + Non-secure secondary slot */
95 #define FLASH_AREA_2_ID            (FLASH_AREA_0_ID + 1)
96 #define FLASH_AREA_2_OFFSET        (FLASH_AREA_0_OFFSET + FLASH_AREA_0_SIZE)
97 #define FLASH_AREA_2_SIZE          (FLASH_S_PARTITION_SIZE + \
98                                     FLASH_NS_PARTITION_SIZE)
99 /* Scratch area */
100 #define FLASH_AREA_SCRATCH_ID      (FLASH_AREA_2_ID + 1)
101 #define FLASH_AREA_SCRATCH_OFFSET  (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE)
102 #define FLASH_AREA_SCRATCH_SIZE    (FLASH_S_PARTITION_SIZE + \
103                                     FLASH_NS_PARTITION_SIZE)
104 /* The maximum number of status entries supported by the bootloader. */
105 #define MCUBOOT_STATUS_MAX_ENTRIES ((FLASH_S_PARTITION_SIZE + \
106                                      FLASH_NS_PARTITION_SIZE) / \
107                                     FLASH_AREA_SCRATCH_SIZE)
108 /* Maximum number of image sectors supported by the bootloader. */
109 #define MCUBOOT_MAX_IMG_SECTORS    ((FLASH_S_PARTITION_SIZE + \
110                                      FLASH_NS_PARTITION_SIZE) / \
111                                     FLASH_AREA_IMAGE_SECTOR_SIZE)
112 #elif (MCUBOOT_IMAGE_NUMBER == 2)
113 /* Secure image primary slot */
114 #define FLASH_AREA_0_ID            (1)
115 #define FLASH_AREA_0_OFFSET        (FLASH_AREA_BL2_OFFSET + FLASH_AREA_BL2_SIZE)
116 #define FLASH_AREA_0_SIZE          (FLASH_S_PARTITION_SIZE)
117 /* Non-secure image primary slot */
118 #define FLASH_AREA_1_ID            (FLASH_AREA_0_ID + 1)
119 #define FLASH_AREA_1_OFFSET        (FLASH_AREA_0_OFFSET + FLASH_AREA_0_SIZE)
120 #define FLASH_AREA_1_SIZE          (FLASH_NS_PARTITION_SIZE)
121 /* Secure image secondary slot */
122 #define FLASH_AREA_2_ID            (FLASH_AREA_1_ID + 1)
123 #define FLASH_AREA_2_OFFSET        (FLASH_AREA_1_OFFSET + FLASH_AREA_1_SIZE)
124 #define FLASH_AREA_2_SIZE          (FLASH_S_PARTITION_SIZE)
125 /* Non-secure image secondary slot */
126 #define FLASH_AREA_3_ID            (FLASH_AREA_2_ID + 1)
127 #define FLASH_AREA_3_OFFSET        (FLASH_AREA_2_OFFSET + FLASH_AREA_2_SIZE)
128 #define FLASH_AREA_3_SIZE          (FLASH_NS_PARTITION_SIZE)
129 /* Scratch area */
130 #define FLASH_AREA_SCRATCH_ID      (FLASH_AREA_3_ID + 1)
131 #define FLASH_AREA_SCRATCH_OFFSET  (FLASH_AREA_3_OFFSET + FLASH_AREA_3_SIZE)
132 #define FLASH_AREA_SCRATCH_SIZE    (FLASH_MAX_PARTITION_SIZE)
133 /* The maximum number of status entries supported by the bootloader. */
134 #define MCUBOOT_STATUS_MAX_ENTRIES (FLASH_MAX_PARTITION_SIZE / \
135                                     FLASH_AREA_SCRATCH_SIZE)
136 /* Maximum number of image sectors supported by the bootloader. */
137 #define MCUBOOT_MAX_IMG_SECTORS    (FLASH_MAX_PARTITION_SIZE / \
138                                     FLASH_AREA_IMAGE_SECTOR_SIZE)
139 #else /* MCUBOOT_IMAGE_NUMBER > 2 */
140 #error "Only MCUBOOT_IMAGE_NUMBER 1 and 2 are supported!"
141 #endif /* MCUBOOT_IMAGE_NUMBER */
142 
143 /* Protected Storage (PS) Service definitions */
144 #define FLASH_PS_AREA_OFFSET            (FLASH_AREA_SCRATCH_OFFSET + \
145                                          FLASH_AREA_SCRATCH_SIZE)
146 #define FLASH_PS_AREA_SIZE              (0x5000)   /* 20 KB */
147 
148 /* Internal Trusted Storage (ITS) Service definitions */
149 #define FLASH_ITS_AREA_OFFSET           (FLASH_PS_AREA_OFFSET + \
150                                          FLASH_PS_AREA_SIZE)
151 #define FLASH_ITS_AREA_SIZE             (0x4000)   /* 16 KB */
152 
153 /* OTP_definitions */
154 #define FLASH_OTP_NV_COUNTERS_AREA_OFFSET (FLASH_ITS_AREA_OFFSET + \
155                                            FLASH_ITS_AREA_SIZE)
156 #define FLASH_OTP_NV_COUNTERS_AREA_SIZE   (FLASH_AREA_IMAGE_SECTOR_SIZE * 2)
157 #define FLASH_OTP_NV_COUNTERS_SECTOR_SIZE FLASH_AREA_IMAGE_SECTOR_SIZE
158 
159 /* Offset and size definition in flash area used by assemble.py */
160 #define SECURE_IMAGE_OFFSET             (0x0)
161 #define SECURE_IMAGE_MAX_SIZE           FLASH_S_PARTITION_SIZE
162 
163 #define NON_SECURE_IMAGE_OFFSET         (SECURE_IMAGE_OFFSET + \
164                                          SECURE_IMAGE_MAX_SIZE)
165 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_NS_PARTITION_SIZE
166 
167 /* Flash device name used by BL2
168  * Name is defined in flash driver file: Driver_Flash.c
169  */
170 #define FLASH_DEV_NAME Driver_FLASH0
171 /* Smallest flash programmable unit in bytes */
172 #define TFM_HAL_FLASH_PROGRAM_UNIT       (0x1)
173 
174 /* Protected Storage (PS) Service definitions
175  * Note: Further documentation of these definitions can be found in the
176  * TF-M PS Integration Guide.
177  */
178 #define TFM_HAL_PS_FLASH_DRIVER Driver_FLASH0
179 
180 /* In this target the CMSIS driver requires only the offset from the base
181  * address instead of the full memory address.
182  */
183 /* Base address of dedicated flash area for PS */
184 #define TFM_HAL_PS_FLASH_AREA_ADDR    FLASH_PS_AREA_OFFSET
185 /* Size of dedicated flash area for PS */
186 #define TFM_HAL_PS_FLASH_AREA_SIZE    FLASH_PS_AREA_SIZE
187 #define PS_RAM_FS_SIZE                TFM_HAL_PS_FLASH_AREA_SIZE
188 /* Number of physical erase sectors per logical FS block */
189 #define TFM_HAL_PS_SECTORS_PER_BLOCK  (1)
190 /* Smallest flash programmable unit in bytes */
191 #define TFM_HAL_PS_PROGRAM_UNIT       (0x1)
192 
193 /* Internal Trusted Storage (ITS) Service definitions
194  * Note: Further documentation of these definitions can be found in the
195  * TF-M ITS Integration Guide. The ITS should be in the internal flash, but is
196  * allocated in the external flash just for development platforms that don't
197  * have internal flash available.
198  */
199 #define TFM_HAL_ITS_FLASH_DRIVER Driver_FLASH0
200 
201 /* In this target the CMSIS driver requires only the offset from the base
202  * address instead of the full memory address.
203  */
204 /* Base address of dedicated flash area for ITS */
205 #define TFM_HAL_ITS_FLASH_AREA_ADDR    FLASH_ITS_AREA_OFFSET
206 /* Size of dedicated flash area for ITS */
207 #define TFM_HAL_ITS_FLASH_AREA_SIZE    FLASH_ITS_AREA_SIZE
208 #define ITS_RAM_FS_SIZE                TFM_HAL_ITS_FLASH_AREA_SIZE
209 /* Number of physical erase sectors per logical FS block */
210 #define TFM_HAL_ITS_SECTORS_PER_BLOCK  (1)
211 /* Smallest flash programmable unit in bytes */
212 #define TFM_HAL_ITS_PROGRAM_UNIT       (0x1)
213 
214 /* OTP / NV counter definitions */
215 #define TFM_OTP_NV_COUNTERS_AREA_SIZE   (FLASH_OTP_NV_COUNTERS_AREA_SIZE / 2)
216 #define TFM_OTP_NV_COUNTERS_AREA_ADDR   FLASH_OTP_NV_COUNTERS_AREA_OFFSET
217 #define TFM_OTP_NV_COUNTERS_SECTOR_SIZE FLASH_OTP_NV_COUNTERS_SECTOR_SIZE
218 #define TFM_OTP_NV_COUNTERS_BACKUP_AREA_ADDR (TFM_OTP_NV_COUNTERS_AREA_ADDR + \
219                                               TFM_OTP_NV_COUNTERS_AREA_SIZE)
220 
221 /* Use SRAM1 memory to store Code data */
222 #define S_ROM_ALIAS_BASE  (0x10000000)
223 #define NS_ROM_ALIAS_BASE (0x00000000)
224 
225 /* FIXME: Use SRAM2 memory to store RW data */
226 #define S_RAM_ALIAS_BASE  (0x38000000)
227 #define NS_RAM_ALIAS_BASE (0x28000000)
228 
229 #define TOTAL_ROM_SIZE FLASH_TOTAL_SIZE
230 #define TOTAL_RAM_SIZE (0x200000)     /* 2 MB */
231 
232 #endif /* __FLASH_LAYOUT_H__ */
233