1 /*
2  * Copyright (c) 2017-2022 Arm Limited. All rights reserved.
3  * Copyright (c) 2020 Nuvoton Technology Corp. 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 M2354 with BL2 (single image boot):
22  *
23  * 0x0000_0000 BL2 - MCUBoot (64KB)
24  * 0x0001_0000 Protected Storage Area (28 KB)
25  * 0x0000_7000 Internal Trusted Storage Area (28 KB)
26  * 0x0000_E000 OTP / NV counters area (8 KB)
27  * 0x0002_0000 Primary image area (320KB):
28  *    0x0002_0000 Secure     image primary
29  *    0x0007_0000 Non-secure image primary
30  * 0x0010_0000 Scratch area (4KB)
31  *
32  */
33 
34 /* This header file is included from linker scatter file as well, where only a
35  * limited C constructs are allowed. Therefore it is not possible to include
36  * here the platform_retarget.h to access flash related defines. To resolve this
37  * some of the values are redefined here with different names, these are marked
38  * with comment.
39  */
40 
41 /* Sector size of the flash hardware; same as FLASH0_SECTOR_SIZE */
42 #define FLASH_AREA_IMAGE_SECTOR_SIZE    (0x800)   /* 2 KB */
43 /* Same as FLASH0_SIZE */
44 #define FLASH_TOTAL_SIZE                (0x00100000)
45 
46 /* Flash layout info for BL2 bootloader */
47 /* Same as FLASH0_BASE_S */
48 #define FLASH_BASE_ADDRESS              (0x00000000)
49 
50 /* Offset and size definitions of the flash partitions that are handled by the
51  * bootloader. The image swapping is done between IMAGE_PRIMARY and
52  * IMAGE_SECONDARY, SCRATCH is used as a temporary storage during image
53  * swapping.
54  */
55 #define FLASH_AREA_BL2_OFFSET      (0x0)
56 #define FLASH_AREA_BL2_SIZE        (0x10000) /* 64 KB */
57 
58 #if !defined(MCUBOOT_IMAGE_NUMBER) || (MCUBOOT_IMAGE_NUMBER == 1)
59 /* Size of a Secure and of a Non-secure image */
60 #define FLASH_S_PARTITION_SIZE          (0x60000)
61 #define FLASH_NS_PARTITION_SIZE         (0x40000)
62 
63 #if (FLASH_S_PARTITION_SIZE > FLASH_NS_PARTITION_SIZE)
64 #define FLASH_MAX_PARTITION_SIZE FLASH_S_PARTITION_SIZE
65 #else
66 #define FLASH_MAX_PARTITION_SIZE FLASH_NS_PARTITION_SIZE
67 #endif
68 
69 
70 /* Secure + Non-secure image primary slot */
71 #define FLASH_AREA_0_ID            (1)
72 #define FLASH_AREA_0_OFFSET        (0x20000)
73 #define FLASH_AREA_0_SIZE          (FLASH_S_PARTITION_SIZE + FLASH_NS_PARTITION_SIZE)
74 /* Secure + Non-secure secondary slot */
75 #define FLASH_AREA_2_ID            (FLASH_AREA_0_ID + 1)
76 #define FLASH_AREA_2_OFFSET        (0x80000)
77 #define FLASH_AREA_2_SIZE          (0x80000)
78 
79 /* Scratch area */
80 #define FLASH_AREA_SCRATCH_ID      (FLASH_AREA_2_ID + 1)
81 #define FLASH_AREA_SCRATCH_OFFSET  (0x100800)
82 #define FLASH_AREA_SCRATCH_SIZE    (0x800)
83 
84 /* The maximum number of status entries supported by the bootloader. */
85 #define MCUBOOT_STATUS_MAX_ENTRIES ((FLASH_S_PARTITION_SIZE + \
86                                      FLASH_NS_PARTITION_SIZE) / \
87                                     FLASH_AREA_SCRATCH_SIZE)
88 
89 /* Maximum number of image sectors supported by the bootloader. */
90 #define MCUBOOT_MAX_IMG_SECTORS    ((FLASH_S_PARTITION_SIZE + \
91                                      FLASH_NS_PARTITION_SIZE) / \
92                                     FLASH_AREA_IMAGE_SECTOR_SIZE)
93 
94 #elif (MCUBOOT_IMAGE_NUMBER == 2)
95 
96 /* Size of a Secure and of a Non-secure image */
97 #define FLASH_S_PARTITION_SIZE          (0x40000)
98 #define FLASH_NS_PARTITION_SIZE         (0x40000)
99 
100 #if (FLASH_S_PARTITION_SIZE > FLASH_NS_PARTITION_SIZE)
101 #define FLASH_MAX_PARTITION_SIZE FLASH_S_PARTITION_SIZE
102 #else
103 #define FLASH_MAX_PARTITION_SIZE FLASH_NS_PARTITION_SIZE
104 #endif
105 
106 
107  /* Secure image primary slot */
108 #define FLASH_AREA_0_ID            (1)
109 #define FLASH_AREA_0_OFFSET        (0x20000)
110 #define FLASH_AREA_0_SIZE          (FLASH_S_PARTITION_SIZE)
111 /* Non-secure image primary slot */
112 #define FLASH_AREA_1_ID            (FLASH_AREA_0_ID + 1)
113 #define FLASH_AREA_1_OFFSET        (0x80000)
114 #define FLASH_AREA_1_SIZE          (FLASH_NS_PARTITION_SIZE)
115 /* Secure image secondary slot */
116 #define FLASH_AREA_2_ID            (FLASH_AREA_1_ID + 1)
117 #define FLASH_AREA_2_OFFSET        (FLASH_AREA_0_OFFSET+FLASH_AREA_0_SIZE)
118 #define FLASH_AREA_2_SIZE          (0x20000)
119 /* Non-secure image secondary slot */
120 #define FLASH_AREA_3_ID            (FLASH_AREA_2_ID + 1)
121 #define FLASH_AREA_3_OFFSET        (FLASH_AREA_1_OFFSET+FLASH_AREA_1_SIZE)
122 #define FLASH_AREA_3_SIZE          (0x20000)
123 /* Scratch area */
124 #define FLASH_AREA_SCRATCH_ID      (FLASH_AREA_3_ID + 1)
125 #define FLASH_AREA_SCRATCH_OFFSET  (0x100800)
126 #define FLASH_AREA_SCRATCH_SIZE    (0x800)
127 /* The maximum number of status entries supported by the bootloader. */
128 #define MCUBOOT_STATUS_MAX_ENTRIES ((FLASH_S_PARTITION_SIZE + \
129                                      FLASH_NS_PARTITION_SIZE) / \
130                                     FLASH_AREA_SCRATCH_SIZE)
131 /* Maximum number of image sectors supported by the bootloader. */
132 #define MCUBOOT_MAX_IMG_SECTORS    ((FLASH_S_PARTITION_SIZE + \
133                                      FLASH_NS_PARTITION_SIZE) / \
134                                     FLASH_AREA_IMAGE_SECTOR_SIZE)
135 #else /* MCUBOOT_IMAGE_NUMBER > 2 */
136 #error "Only MCUBOOT_IMAGE_NUMBER 1 and 2 are supported!"
137 #endif /* MCUBOOT_IMAGE_NUMBER */
138 
139 /* Protected Storage (PS) Service definitions */
140 #define FLASH_PS_AREA_OFFSET            (0x10000)
141 #define FLASH_PS_AREA_SIZE              (0x5000)
142 
143 /* Internal Trusted Storage (ITS) Service definitions */
144 #define FLASH_ITS_AREA_OFFSET           (FLASH_PS_AREA_OFFSET + FLASH_PS_AREA_SIZE)
145 #define FLASH_ITS_AREA_SIZE             (0x5000)
146 
147 /* OTP_definitions */
148 #define FLASH_OTP_NV_COUNTERS_AREA_OFFSET (FLASH_ITS_AREA_OFFSET + FLASH_ITS_AREA_SIZE)
149 #define FLASH_OTP_NV_COUNTERS_AREA_SIZE   (FLASH_AREA_IMAGE_SECTOR_SIZE * 2)
150 #define FLASH_OTP_NV_COUNTERS_SECTOR_SIZE FLASH_AREA_IMAGE_SECTOR_SIZE
151 
152 /* Offset and size definition in flash area used by assemble.py */
153 #define SECURE_IMAGE_OFFSET             (0x0)
154 #define SECURE_IMAGE_MAX_SIZE           FLASH_S_PARTITION_SIZE
155 
156 #define NON_SECURE_IMAGE_OFFSET         (SECURE_IMAGE_OFFSET + SECURE_IMAGE_MAX_SIZE)
157 #define NON_SECURE_IMAGE_MAX_SIZE       FLASH_NS_PARTITION_SIZE
158 
159 /* Flash device name used by BL2
160  * Name is defined in flash driver file: Driver_Flash.c
161  */
162 #define FLASH_DEV_NAME Driver_FLASH0
163 /* Smallest flash programmable unit in bytes */
164 #define TFM_HAL_FLASH_PROGRAM_UNIT       (0x4)
165 
166 /* Protected Storage (PS) Service definitions
167  * Note: Further documentation of these definitions can be found in the
168  * TF-M PS Integration Guide.
169  */
170 #define TFM_HAL_PS_FLASH_DRIVER Driver_FLASH0
171 
172 /* In this target the CMSIS driver requires only the offset from the base
173  * address instead of the full memory address.
174  */
175 #define TFM_HAL_PS_FLASH_AREA_ADDR     FLASH_PS_AREA_OFFSET
176 /* Dedicated flash area for PS */
177 #define TFM_HAL_PS_FLASH_AREA_SIZE     FLASH_PS_AREA_SIZE
178 #define PS_SECTOR_SIZE         FLASH_AREA_IMAGE_SECTOR_SIZE
179 /* Number of PS_SECTOR_SIZE per block */
180 #define TFM_HAL_PS_SECTORS_PER_BLOCK   (0x1)
181 /* Specifies the smallest flash programmable unit in bytes */
182 #define TFM_HAL_PS_PROGRAM_UNIT  (0x4)
183 
184 /* Internal Trusted Storage (ITS) Service definitions
185  * Note: Further documentation of these definitions can be found in the
186  * TF-M ITS Integration Guide. The ITS should be in the internal flash, but is
187  * allocated in the external flash just for development platforms that don't
188  * have internal flash available.
189  */
190 #define TFM_HAL_ITS_FLASH_DRIVER Driver_FLASH0
191 
192 /* In this target the CMSIS driver requires only the offset from the base
193  * address instead of the full memory address.
194  */
195 #define TFM_HAL_ITS_FLASH_AREA_ADDR     FLASH_ITS_AREA_OFFSET
196 /* Dedicated flash area for ITS */
197 #define TFM_HAL_ITS_FLASH_AREA_SIZE     FLASH_ITS_AREA_SIZE
198 #define ITS_SECTOR_SIZE         FLASH_AREA_IMAGE_SECTOR_SIZE
199 /* Number of ITS_SECTOR_SIZE per block */
200 #define TFM_HAL_ITS_SECTORS_PER_BLOCK   (0x1)
201 /* Specifies the smallest flash programmable unit in bytes */
202 #define TFM_HAL_ITS_PROGRAM_UNIT  (0x4)
203 
204 /* OTP / NV counter definitions */
205 #define TFM_OTP_NV_COUNTERS_AREA_SIZE   (FLASH_OTP_NV_COUNTERS_AREA_SIZE / 2)
206 #define TFM_OTP_NV_COUNTERS_AREA_ADDR   FLASH_OTP_NV_COUNTERS_AREA_OFFSET
207 #define TFM_OTP_NV_COUNTERS_SECTOR_SIZE FLASH_OTP_NV_COUNTERS_SECTOR_SIZE
208 #define TFM_OTP_NV_COUNTERS_BACKUP_AREA_ADDR (TFM_OTP_NV_COUNTERS_AREA_ADDR + \
209                                               TFM_OTP_NV_COUNTERS_AREA_SIZE)
210 
211 /* Use SRAM1 memory to store Code data */
212 #define S_ROM_ALIAS_BASE  (0x00000000)
213 #define NS_ROM_ALIAS_BASE (0x10000000)
214 
215 /* FIXME: Use SRAM2 memory to store RW data */
216 #define S_RAM_ALIAS_BASE  (0x20000000)
217 #define NS_RAM_ALIAS_BASE (0x30000000)
218 
219 #define TOTAL_ROM_SIZE FLASH_TOTAL_SIZE
220 #define TOTAL_RAM_SIZE (0x00040000)     /* 256 KB */
221 
222 #endif /* __FLASH_LAYOUT_H__ */
223