1 /*
2  * Copyright (c) 2023 Arm Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __DRIVER_FLASH_EMULATED_H__
18 #define __DRIVER_FLASH_EMULATED_H__
19 
20 #include "Driver_Flash_Common.h"
21 #include "emulated_flash_drv.h"
22 
23 /* Driver Capabilities */
24 static const ARM_FLASH_CAPABILITIES DriverCapabilities = {
25     0, /* event_ready */
26     0, /* data_width = 0:8-bit, 1:16-bit, 2:32-bit */
27     1, /* erase_chip */
28     0  /* reserved */
29 };
30 
ARM_Flash_GetCapabilities(void)31 static inline ARM_FLASH_CAPABILITIES ARM_Flash_GetCapabilities(void)
32 {
33     return DriverCapabilities;
34 }
35 
36 /*
37  * \brief Macro for Emulated Flash Driver
38  *
39  * \param[in]  FLASH_DEV          Native driver device \ref emulated_flash_dev_t
40  * \param[out] FLASH_DRIVER_NAME  Resulting Driver name
41  */
42 #define ARM_FLASH_EMULATED(FLASH_DEV, FLASH_DRIVER_NAME)                                        \
43 static int32_t FLASH_DRIVER_NAME##_Initialize(ARM_Flash_SignalEvent_t cb_event)                 \
44 {                                                                                               \
45     ARG_UNUSED(cb_event);                                                                       \
46                                                                                                 \
47     if (DriverCapabilities.data_width >= DATA_WIDTH_ENUM_SIZE) {                                \
48         return ARM_DRIVER_ERROR;                                                                \
49     }                                                                                           \
50                                                                                                 \
51     /* Nothing to be done */                                                                    \
52     return ARM_DRIVER_OK;                                                                       \
53 }                                                                                               \
54                                                                                                 \
55 static int32_t FLASH_DRIVER_NAME##_ReadData(uint32_t addr, void *data, uint32_t cnt)            \
56 {                                                                                               \
57     /* Conversion between data items and bytes */                                               \
58     enum emulated_flash_error_t rc =                                                            \
59             emulated_flash_read_data(&FLASH_DEV, addr, data,                                    \
60                                      cnt * data_width_byte[DriverCapabilities.data_width]);     \
61                                                                                                 \
62     if (EMULATED_FLASH_ERR_NONE == rc) {                                                        \
63         return (int32_t) cnt;                                                                   \
64     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {                                         \
65         return ARM_DRIVER_ERROR_PARAMETER;                                                      \
66     } else {                                                                                    \
67         return ARM_DRIVER_ERROR;                                                                \
68     }                                                                                           \
69 }                                                                                               \
70                                                                                                 \
71 static int32_t FLASH_DRIVER_NAME##_ProgramData(uint32_t addr,                                   \
72                                                const void *data,                                \
73                                                uint32_t cnt)                                    \
74 {                                                                                               \
75     /* Conversion between data items and bytes */                                               \
76     enum emulated_flash_error_t rc =                                                            \
77             emulated_flash_program_data(&FLASH_DEV, addr, data,                                 \
78                                         cnt * data_width_byte[DriverCapabilities.data_width]);  \
79                                                                                                 \
80     if (EMULATED_FLASH_ERR_NONE == rc) {                                                        \
81         return (int32_t) cnt;                                                                   \
82     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {                                         \
83         return ARM_DRIVER_ERROR_PARAMETER;                                                      \
84     } else if(EMULATED_FLASH_NOT_READY == rc) {                                                 \
85         return ARM_DRIVER_ERROR;                                                                \
86     } else {                                                                                    \
87         return ARM_DRIVER_ERROR;                                                                \
88     }                                                                                           \
89 }                                                                                               \
90                                                                                                 \
91 static int32_t FLASH_DRIVER_NAME##_EraseSector(uint32_t addr)                                   \
92 {                                                                                               \
93     enum emulated_flash_error_t rc = emulated_flash_erase_sector(&FLASH_DEV, addr);             \
94                                                                                                 \
95     if (EMULATED_FLASH_ERR_NONE == rc) {                                                        \
96         return ARM_DRIVER_OK;                                                                   \
97     } else if(EMULATED_FLASH_ERR_INVALID_PARAM == rc) {                                         \
98         return ARM_DRIVER_ERROR_PARAMETER;                                                      \
99     } else {                                                                                    \
100         return ARM_DRIVER_ERROR;                                                                \
101     }                                                                                           \
102 }                                                                                               \
103                                                                                                 \
104 static int32_t FLASH_DRIVER_NAME##_EraseChip(void)                                              \
105 {                                                                                               \
106     emulated_flash_erase_chip(&FLASH_DEV);                                                      \
107     return ARM_DRIVER_OK;                                                                       \
108 }                                                                                               \
109                                                                                                 \
110 static ARM_FLASH_INFO * FLASH_DRIVER_NAME##_GetInfo(void)                                       \
111 {                                                                                               \
112     return FLASH_DEV.data;                                                                      \
113 }                                                                                               \
114                                                                                                 \
115 ARM_DRIVER_FLASH FLASH_DRIVER_NAME = {                                                          \
116     ARM_Flash_GetVersion,                                                                       \
117     ARM_Flash_GetCapabilities,                                                                  \
118     FLASH_DRIVER_NAME##_Initialize,                                                             \
119     ARM_Flash_Uninitialize,                                                                     \
120     ARM_Flash_PowerControl,                                                                     \
121     FLASH_DRIVER_NAME##_ReadData,                                                               \
122     FLASH_DRIVER_NAME##_ProgramData,                                                            \
123     FLASH_DRIVER_NAME##_EraseSector,                                                            \
124     FLASH_DRIVER_NAME##_EraseChip,                                                              \
125     ARM_Flash_GetStatus,                                                                        \
126     FLASH_DRIVER_NAME##_GetInfo                                                                 \
127 }                                                                                               \
128 
129 #endif /* __DRIVER_FLASH_EMULATED_H__ */
130