1 /*
2  * Copyright (c) 2021-2022 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 
18 #ifndef __EMULATED_FLASH_DRV_H__
19 #define __EMULATED_FLASH_DRV_H__
20 
21 #include <stdint.h>
22 #include <stdbool.h>
23 
24 /* This include is neceasary because of the ARM_FLASH_INFO structure. Since this driver is not used
25  * as a standalone driver, only with tf-m, we can include it here to prevent code duplication.
26 */
27 #include "Driver_Flash.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define EMULATED_FLASH_DRV_ERASE_VALUE  0xFF
34 
35 /**
36 \brief Flash Sector information
37 */
38 typedef struct _emulated_flash_sector_t {
39   uint32_t start;                       ///< Sector Start address
40   uint32_t end;                         ///< Sector End address (start+size-1)
41 } const emulated_flash_sector_t;
42 
43 /*
44  * Emulated flash device structure
45  *
46  * This driver just emulates a flash interface and behaviour on top of any
47  * type of memory.
48  */
49 struct emulated_flash_dev_t {
50     const uint32_t memory_base_s;        /*!< FLASH memory base address, secure alias */
51     const uint32_t memory_base_ns;       /*!< FLASH memory base address, non-secure alias */
52     ARM_FLASH_INFO *data;                /*!< FLASH data */
53 };
54 
55 enum emulated_flash_error_t {
56     EMULATED_FLASH_ERR_NONE = 0,      /*!< No error */
57     EMULATED_FLASH_ERR_INVALID_PARAM, /*!< Invalid parameter error */
58     EMULATED_FLASH_NOT_READY,         /*!< Not ready error */
59 };
60 
61 
62 /**
63  * \brief Reads data from emulated flash in a blocking call
64  *
65  * \param[in]  dev       Emulated flash device struct \ref emulated_flash_dev_t
66  * \param[in]  addr      Address to read data from the flash
67  * \param[out] data      Pointer to store data read from the flash
68  * \param[in]  cnt       Number of data items to read
69  *
70  * \return Returns error code as specified in \ref emulated_flash_error_t
71  *
72  * \note This function doesn't check if dev is NULL.
73  * \note The whole region needs to be set to the same security to use this
74  *       function.
75  */
76 enum emulated_flash_error_t
77 emulated_flash_read_data(struct emulated_flash_dev_t* dev, uint32_t addr,
78                          void* data, uint32_t cnt);
79 
80 /**
81  * \brief Writes data to the flash in a blocking call
82  *
83  * \param[in] dev      Emulated flash device struct \ref emulated_flash_dev_t
84  * \param[in] addr     Address to write data to the flash
85  * \param[in] data     Pointer to the data to be written
86  * \param[in] cnt      Number of bytes to write
87  *
88  * \return Returns error code as specified in \ref emulated_flash_error_t
89  *
90  * \note Flash area needs to be pre-erased before writing to it
91  * \note Addr is expected to be within the [0x0 - Flash size] range
92  * \note For better performance, this function doesn't check if dev is NULL
93  * \note The whole region needs to be set to the same security to use this
94  *       function.
95  */
96 enum emulated_flash_error_t
97 emulated_flash_program_data(struct emulated_flash_dev_t* dev, uint32_t addr,
98                             const void* data, uint32_t cnt);
99 
100 /**
101  * \brief Erases a sector of the flash
102  *
103  * \param[in] dev      Emulated flash device struct \ref emulated_flash_dev_t
104  * \param[in] addr     Address of the sector to erase
105  *
106  * \return Returns error code as specified in \ref emulated_flash_error_t
107  *
108  * \note For better performance, this function doesn't check if dev is NULL
109  * \note Addr is expected to be within the [0x0 - Flash size] range
110  * \note The whole sector needs to be set to the same security to use this
111  *       function.
112  */
113 enum emulated_flash_error_t
114 emulated_flash_erase_sector(struct emulated_flash_dev_t* dev, uint32_t addr);
115 
116 /**
117  * \brief Erases the whole flash
118  *
119  * \param[in] dev      Emulated flash device struct \ref emulated_flash_dev_t
120  *
121  * \note For better performance, this function doesn't check if dev is NULL
122  * \note The whole memory needs to be set to the same security to use this
123  *       function.
124  */
125 void emulated_flash_erase_chip(struct emulated_flash_dev_t* dev);
126 
127 /**
128  * \brief Returns the information of the emulated flash
129  *
130  * \param[in] dev      Emulated flash device struct \ref emulated_flash_dev_t
131  *
132  * \return Returns the info  \ref ARM_FLASH_INFO
133  *
134  * \note For better performance, this function doesn't check if dev is NULL
135  * \note The \ref ARM_FLASH_INFO is coming from the CMSIS Flash driver. This
136  *       native driver is only used together with CMSIS, so instead of
137  *       duplicating the structure, it is used here as well.
138  */
139 ARM_FLASH_INFO*
140 emulated_flash_get_info(struct emulated_flash_dev_t* dev);
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 #endif /* __EMULATED_FLASH_DRV_H__ */
146