1 /*
2  * Copyright (c) 2013-2022 Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the License); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <string.h>
20 #include <stdint.h>
21 #include "Driver_Flash.h"
22 #include "RTE_Device.h"
23 #include "platform_base_address.h"
24 #include "emulated_flash_drv.h"
25 
26 #ifndef ARG_UNUSED
27 #define ARG_UNUSED(arg)  ((void)arg)
28 #endif
29 
30 #define FLASH0_BASE_S         SRAM_BASE_S
31 #define FLASH0_BASE_NS        SRAM_BASE_NS
32 #define FLASH0_SIZE           SRAM_SIZE
33 #define FLASH0_SECTOR_SIZE    0x00001000 /* 4 kB */
34 #define FLASH0_PAGE_SIZE      0x00001000 /* 4 kB */
35 #define FLASH0_PROGRAM_UNIT   0x1        /* Minimum write size */
36 
37 #define FLASH1_BASE_S         ISRAM1_BASE_S
38 #define FLASH1_BASE_NS        ISRAM1_BASE_NS
39 #define FLASH1_SIZE           ISRAM1_SIZE
40 #define FLASH1_SECTOR_SIZE    0x00001000 /* 4 kB */
41 #define FLASH1_PAGE_SIZE      0x00001000 /* 4 kB */
42 #define FLASH1_PROGRAM_UNIT   0x1        /* Minimum write size */
43 
44 /* Driver version */
45 #define ARM_FLASH_DRV_VERSION      ARM_DRIVER_VERSION_MAJOR_MINOR(1, 1)
46 
47 #if (defined (RTE_SRAM) && (RTE_SRAM == 1)) || \
48     (defined (RTE_ISRAM1) && (RTE_ISRAM1 == 1))
49 
50 /**
51  * Data width values for ARM_FLASH_CAPABILITIES::data_width
52  * \ref ARM_FLASH_CAPABILITIES
53  */
54  enum {
55     DATA_WIDTH_8BIT   = 0u,
56     DATA_WIDTH_16BIT,
57     DATA_WIDTH_32BIT,
58     DATA_WIDTH_ENUM_SIZE
59 };
60 
61 static const uint32_t data_width_byte[DATA_WIDTH_ENUM_SIZE] = {
62     sizeof(uint8_t),
63     sizeof(uint16_t),
64     sizeof(uint32_t),
65 };
66 
67 /* Driver Version */
68 static const ARM_DRIVER_VERSION DriverVersion = {
69     ARM_FLASH_API_VERSION,
70     ARM_FLASH_DRV_VERSION
71 };
72 
73 /* Driver Capabilities */
74 static const ARM_FLASH_CAPABILITIES DriverCapabilities = {
75     0, /* event_ready */
76     0, /* data_width = 0:8-bit, 1:16-bit, 2:32-bit */
77     1, /* erase_chip */
78     0  /* reserved */
79 };
80 
ARM_Flash_GetVersion(void)81 static ARM_DRIVER_VERSION ARM_Flash_GetVersion(void)
82 {
83     return DriverVersion;
84 }
85 
ARM_Flash_GetCapabilities(void)86 static ARM_FLASH_CAPABILITIES ARM_Flash_GetCapabilities(void)
87 {
88     return DriverCapabilities;
89 }
90 
ARM_Flash_Initialize(ARM_Flash_SignalEvent_t cb_event)91 static int32_t ARM_Flash_Initialize(ARM_Flash_SignalEvent_t cb_event)
92 {
93     ARG_UNUSED(cb_event);
94 
95     if (DriverCapabilities.data_width >= DATA_WIDTH_ENUM_SIZE) {
96         return ARM_DRIVER_ERROR;
97     }
98 
99     /* Nothing to be done */
100     return ARM_DRIVER_OK;
101 }
102 
ARM_Flash_Uninitialize(void)103 static int32_t ARM_Flash_Uninitialize(void)
104 {
105     /* Nothing to be done */
106     return ARM_DRIVER_OK;
107 }
108 
ARM_Flash_PowerControl(ARM_POWER_STATE state)109 static int32_t ARM_Flash_PowerControl(ARM_POWER_STATE state)
110 {
111     switch (state) {
112     case ARM_POWER_FULL:
113         /* Nothing to be done */
114         return ARM_DRIVER_OK;
115 
116     case ARM_POWER_OFF:
117     case ARM_POWER_LOW:
118     default:
119         return ARM_DRIVER_ERROR_UNSUPPORTED;
120     }
121 }
122 
123 #if (defined (RTE_SRAM) && (RTE_SRAM == 1))
124 
125 /* Flash Status */
126 static ARM_FLASH_STATUS FlashStatus_FLASH0 = {0, 0, 0};
127 
128 static ARM_FLASH_INFO ARM_FLASH0_DEV_INFO = {
129     .sector_info  = NULL,                  /* Uniform sector layout */
130     .sector_count = FLASH0_SIZE / FLASH0_SECTOR_SIZE,
131     .sector_size  = FLASH0_SECTOR_SIZE,
132     .page_size    = FLASH0_PAGE_SIZE,
133     .program_unit = FLASH0_PROGRAM_UNIT,
134     .erased_value = EMULATED_FLASH_DRV_ERASE_VALUE};
135 
136 static struct emulated_flash_dev_t ARM_FLASH0_DEV = {
137 #if (defined (__DOMAIN_NS) && (__DOMAIN_NS == 1))
138     .memory_base_ns = FLASH0_BASE_NS,
139 #else
140     .memory_base_ns = FLASH0_BASE_NS,
141     .memory_base_s = FLASH0_BASE_S,
142 #endif /* __DOMAIN_NS == 1 */
143     .data        = &(ARM_FLASH0_DEV_INFO)};
144 
145 struct emulated_flash_dev_t *FLASH0_DEV = &ARM_FLASH0_DEV;
146 
147 /*
148  * Functions
149  */
150 
ARM_Flash_FLASH0_ReadData(uint32_t addr,void * data,uint32_t cnt)151 static int32_t ARM_Flash_FLASH0_ReadData(uint32_t addr, void *data, uint32_t cnt)
152 {
153     /* Conversion between data items and bytes */
154     cnt *= data_width_byte[DriverCapabilities.data_width];
155 
156     enum emulated_flash_error_t rc = emulated_flash_read_data(FLASH0_DEV, addr, data, cnt);
157     if (EMULATED_FLASH_ERR_NONE == rc) {
158         cnt /= data_width_byte[DriverCapabilities.data_width];
159         return cnt;
160     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {
161         return ARM_DRIVER_ERROR_PARAMETER;
162     } else {
163         return ARM_DRIVER_ERROR;
164     }
165 }
166 
ARM_Flash_FLASH0_ProgramData(uint32_t addr,const void * data,uint32_t cnt)167 static int32_t ARM_Flash_FLASH0_ProgramData(uint32_t addr, const void *data,
168                                      uint32_t cnt)
169 {
170     /* Conversion between data items and bytes */
171     cnt *= data_width_byte[DriverCapabilities.data_width];
172     enum emulated_flash_error_t rc = emulated_flash_program_data(FLASH0_DEV, addr, data, cnt);
173 
174     if (EMULATED_FLASH_ERR_NONE == rc) {
175         cnt /= data_width_byte[DriverCapabilities.data_width];
176         return (int32_t)cnt;
177     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {
178         return ARM_DRIVER_ERROR_PARAMETER;
179     } else if(EMULATED_FLASH_NOT_READY == rc) {
180         return ARM_DRIVER_ERROR;
181     } else {
182         return ARM_DRIVER_ERROR;
183     }
184 }
185 
ARM_Flash_FLASH0_EraseSector(uint32_t addr)186 static int32_t ARM_Flash_FLASH0_EraseSector(uint32_t addr)
187 {
188     enum emulated_flash_error_t rc = emulated_flash_erase_sector(FLASH0_DEV, addr);
189 
190     if (EMULATED_FLASH_ERR_NONE == rc) {
191         return ARM_DRIVER_OK;
192     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {
193         return ARM_DRIVER_ERROR_PARAMETER;
194     } else {
195         return ARM_DRIVER_ERROR;
196     }
197 }
198 
ARM_Flash_FLASH0_EraseChip(void)199 static int32_t ARM_Flash_FLASH0_EraseChip(void)
200 {
201     emulated_flash_erase_chip(FLASH0_DEV);
202     return ARM_DRIVER_OK;
203 }
204 
ARM_Flash_FLASH0_GetInfo(void)205 static ARM_FLASH_INFO * ARM_Flash_FLASH0_GetInfo(void)
206 {
207     return &ARM_FLASH0_DEV_INFO;
208 }
209 
ARM_Flash_FLASH0_GetStatus(void)210 static ARM_FLASH_STATUS ARM_Flash_FLASH0_GetStatus(void)
211 {
212     return FlashStatus_FLASH0;
213 }
214 
215 
216 ARM_DRIVER_FLASH Driver_FLASH0 = {
217     ARM_Flash_GetVersion,
218     ARM_Flash_GetCapabilities,
219     ARM_Flash_Initialize,
220     ARM_Flash_Uninitialize,
221     ARM_Flash_PowerControl,
222     ARM_Flash_FLASH0_ReadData,
223     ARM_Flash_FLASH0_ProgramData,
224     ARM_Flash_FLASH0_EraseSector,
225     ARM_Flash_FLASH0_EraseChip,
226     ARM_Flash_FLASH0_GetStatus,
227     ARM_Flash_FLASH0_GetInfo
228 };
229 #endif /* RTE_SRAM */
230 
231 #if (defined (RTE_ISRAM1) && (RTE_ISRAM1 == 1))
232 
233 /* Flash Status */
234 static ARM_FLASH_STATUS FlashStatus_FLASH1 = {0, 0, 0};
235 
236 static ARM_FLASH_INFO ARM_FLASH1_DEV_INFO = {
237     .sector_info  = NULL,                  /* Uniform sector layout */
238     .sector_count = FLASH1_SIZE / FLASH1_SECTOR_SIZE,
239     .sector_size  = FLASH1_SECTOR_SIZE,
240     .page_size    = FLASH1_PAGE_SIZE,
241     .program_unit = FLASH1_PROGRAM_UNIT,
242     .erased_value = EMULATED_FLASH_DRV_ERASE_VALUE};
243 
244 static struct emulated_flash_dev_t ARM_FLASH1_DEV = {
245 #if (defined (__DOMAIN_NS) && (__DOMAIN_NS == 1))
246     .memory_base_ns = FLASH1_BASE_NS,
247 #else
248     .memory_base_ns = FLASH1_BASE_NS,
249     .memory_base_s = FLASH1_BASE_S,
250 #endif /* __DOMAIN_NS == 1 */
251     .data        = &(ARM_FLASH1_DEV_INFO)};
252 
253 struct emulated_flash_dev_t *FLASH1_DEV = &ARM_FLASH1_DEV;
254 
255 /*
256  * Functions
257  */
258 
ARM_Flash_FLASH1_ReadData(uint32_t addr,void * data,uint32_t cnt)259 static int32_t ARM_Flash_FLASH1_ReadData(uint32_t addr, void *data, uint32_t cnt)
260 {
261     /* Conversion between data items and bytes */
262     cnt *= data_width_byte[DriverCapabilities.data_width];
263     enum emulated_flash_error_t rc = emulated_flash_read_data(FLASH1_DEV, addr, data, cnt);
264     if (EMULATED_FLASH_ERR_NONE == rc) {
265         cnt /= data_width_byte[DriverCapabilities.data_width];
266         return (int32_t)cnt;
267     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {
268         return ARM_DRIVER_ERROR_PARAMETER;
269     } else {
270         return ARM_DRIVER_ERROR;
271     }
272 }
273 
ARM_Flash_FLASH1_ProgramData(uint32_t addr,const void * data,uint32_t cnt)274 static int32_t ARM_Flash_FLASH1_ProgramData(uint32_t addr, const void *data,
275                                      uint32_t cnt)
276 {
277     /* Conversion between data items and bytes */
278     cnt *= data_width_byte[DriverCapabilities.data_width];
279     enum emulated_flash_error_t rc = emulated_flash_program_data(FLASH1_DEV, addr, data, cnt);
280 
281     if (EMULATED_FLASH_ERR_NONE == rc) {
282         cnt /= data_width_byte[DriverCapabilities.data_width];
283         return (int32_t)cnt;
284     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {
285         return ARM_DRIVER_ERROR_PARAMETER;
286     } else if(EMULATED_FLASH_NOT_READY == rc) {
287         return ARM_DRIVER_ERROR;
288     } else {
289         return ARM_DRIVER_ERROR;
290     }
291 }
292 
ARM_Flash_FLASH1_EraseSector(uint32_t addr)293 static int32_t ARM_Flash_FLASH1_EraseSector(uint32_t addr)
294 {
295     enum emulated_flash_error_t rc = emulated_flash_erase_sector(FLASH1_DEV, addr);
296 
297     if (EMULATED_FLASH_ERR_NONE == rc) {
298         return ARM_DRIVER_OK;
299     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {
300         return ARM_DRIVER_ERROR_PARAMETER;
301     } else {
302         return ARM_DRIVER_ERROR;
303     }
304 }
305 
ARM_Flash_FLASH1_EraseChip(void)306 static int32_t ARM_Flash_FLASH1_EraseChip(void)
307 {
308     emulated_flash_erase_chip(FLASH1_DEV);
309     return ARM_DRIVER_OK;
310 }
311 
ARM_Flash_FLASH1_GetInfo(void)312 static ARM_FLASH_INFO * ARM_Flash_FLASH1_GetInfo(void)
313 {
314     return &ARM_FLASH1_DEV_INFO;
315 }
316 
ARM_Flash_FLASH1_GetStatus(void)317 static ARM_FLASH_STATUS ARM_Flash_FLASH1_GetStatus(void)
318 {
319     return FlashStatus_FLASH1;
320 }
321 
322 
323 ARM_DRIVER_FLASH Driver_FLASH1 = {
324     ARM_Flash_GetVersion,
325     ARM_Flash_GetCapabilities,
326     ARM_Flash_Initialize,
327     ARM_Flash_Uninitialize,
328     ARM_Flash_PowerControl,
329     ARM_Flash_FLASH1_ReadData,
330     ARM_Flash_FLASH1_ProgramData,
331     ARM_Flash_FLASH1_EraseSector,
332     ARM_Flash_FLASH1_EraseChip,
333     ARM_Flash_FLASH1_GetStatus,
334     ARM_Flash_FLASH1_GetInfo
335 };
336 
337 #endif /* RTE_ISRAM1 */
338 #endif
339