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