1 /*
2  * Copyright 2018-2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  */
8 
9 #ifndef FSL_K4_FLASH_H_
10 #define FSL_K4_FLASH_H_
11 
12 #include "fsl_k4_controller.h"
13 
14 /*!
15  * @addtogroup flash_driver
16  * @{
17  */
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /*!
23  * @name Flash version
24  * @{
25  */
26 /*! @brief Flash driver version for SDK*/
27 #define FSL_FLASH_DRIVER_VERSION (MAKE_VERSION(2, 1, 1)) /*!< Version 2.1.1. */
28 
29 /*! @brief Flash driver version for ROM*/
30 enum _flash_driver_version_constants
31 {
32     kFLASH_DriverVersionName   = 'F', /*!< Flash driver version name.*/
33     kFLASH_DriverVersionMajor  = 2,   /*!< Major flash driver version.*/
34     kFLASH_DriverVersionMinor  = 1,   /*!< Minor flash driver version.*/
35     kFLASH_DriverVersionBugfix = 0    /*!< Bugfix for flash driver version.*/
36 };
37 
38 /*!
39  * @brief Enumeration for various flash properties.
40  */
41 typedef enum _flash_property_tag
42 {
43     kFLASH_PropertyPflash0SectorSize         = 0x00U, /*!< Pflash sector size property.*/
44     kFLASH_PropertyPflash0TotalSize          = 0x01U, /*!< Pflash total size property.*/
45     kFLASH_PropertyPflash0BlockSize          = 0x02U, /*!< Pflash block size property.*/
46     kFLASH_PropertyPflash0BlockCount         = 0x03U, /*!< Pflash block count property.*/
47     kFLASH_PropertyPflash0BlockBaseAddr      = 0x04U, /*!< Pflash block base address property.*/
48     kFLASH_PropertyPflash0FacSupport         = 0x05U, /*!< Pflash fac support property.*/
49     kFLASH_PropertyPflash0AccessSegmentSize  = 0x06U, /*!< Pflash access segment size property.*/
50     kFLASH_PropertyPflash0AccessSegmentCount = 0x07U, /*!< Pflash access segment count property.*/
51 
52     kFLASH_PropertyPflash1SectorSize         = 0x10U, /*!< Pflash sector size property.*/
53     kFLASH_PropertyPflash1TotalSize          = 0x11U, /*!< Pflash total size property.*/
54     kFLASH_PropertyPflash1BlockSize          = 0x12U, /*!< Pflash block size property.*/
55     kFLASH_PropertyPflash1BlockCount         = 0x13U, /*!< Pflash block count property.*/
56     kFLASH_PropertyPflash1BlockBaseAddr      = 0x14U, /*!< Pflash block base address property.*/
57     kFLASH_PropertyPflash1FacSupport         = 0x15U, /*!< Pflash fac support property.*/
58     kFLASH_PropertyPflash1AccessSegmentSize  = 0x16U, /*!< Pflash access segment size property.*/
59     kFLASH_PropertyPflash1AccessSegmentCount = 0x17U, /*!< Pflash access segment count property.*/
60 
61     kFLASH_PropertyFlexRamBlockBaseAddr = 0x20U, /*!< FlexRam block base address property.*/
62     kFLASH_PropertyFlexRamTotalSize     = 0x21U, /*!< FlexRam total size property.*/
63 } flash_property_tag_t;
64 
65 #define FLASH_ADDR_MASK 0xEFFFFFFFu
66 
67 /*!
68  * @name Flash API key
69  * @{
70  */
71 /*!
72  * @brief Enumeration for Flash driver API keys.
73  *
74  * @note The resulting value is built with a byte order such that the string
75  * being readable in expected order when viewed in a hex editor, if the value
76  * is treated as a 32-bit little endian value.
77  */
78 enum _flash_driver_api_keys
79 {
80     kFLASH_ApiEraseKey = FOUR_CHAR_CODE('l', 'f', 'e', 'k') /*!< Key value used to validate all flash erase APIs.*/
81 };
82 /*! @} */
83 
84 /*!
85  * @brief Flash memory descriptor.
86  */
87 typedef struct _flash_mem_descriptor
88 {
89     uint32_t blockBase;  /*!< Base address of the flash block */
90     uint32_t totalSize;  /*!< The size of the flash block. */
91     uint32_t blockCount; /*!< A number of flash blocks. */
92 } flash_mem_desc_t;
93 
94 typedef struct _flash_ifr_desc
95 {
96     uint32_t pflashIfr0Start;
97     uint32_t pflashIfr0MemSize;
98 } flash_ifr_desc_t;
99 
100 typedef struct _msf1_config
101 {
102     flash_mem_desc_t flashDesc;
103     flash_ifr_desc_t ifrDesc;
104 } msf1_config_t;
105 
106 /*! @brief Flash driver state information.
107  *
108  * An instance of this structure is allocated by the user of the flash driver and
109  * passed into each of the driver APIs.
110  */
111 typedef struct _flash_config
112 {
113     msf1_config_t msf1Config[2];
114 } flash_config_t;
115 
116 /*******************************************************************************
117  * API
118  ******************************************************************************/
119 
120 #if defined(__cplusplus)
121 extern "C" {
122 #endif
123 
124 /*!
125  * @name Initialization
126  * @{
127  */
128 
129 /*!
130  * @brief Initializes the global flash properties structure members.
131  *
132  * This function checks and initializes the Flash module for the other Flash APIs.
133  *
134  * @param config Pointer to the storage for the driver runtime state.
135  *
136  * @retval #kStatus_FLASH_Success API was executed successfully.
137  * @retval #kStatus_FLASH_InvalidArgument An invalid argument is provided.
138  * @retval #kStatus_FLASH_CommandFailure Run-time error during the command execution.
139  * @retval #kStatus_FLASH_CommandNotSupported Flash API is not supported.
140  */
141 status_t FLASH_Init(flash_config_t *config);
142 
143 /*! @} */
144 
145 /*!
146  * @name Erasing
147  * @{
148  */
149 
150 /*!
151  * @brief Erases the flash sectors encompassed by parameters passed into function.
152  */
153 status_t FLASH_Erase(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes, uint32_t key);
154 
155 /*!
156  * @brief Erases entire flash and ifr
157  */
158 status_t FLASH_EraseAll(FMU_Type *base, uint32_t key);
159 
160 /*! @} */
161 
162 /*!
163  * @name Programming
164  * @{
165  */
166 
167 /*!
168  * @brief Programs flash phrases with data at locations passed in through parameters.
169  */
170 status_t FLASH_Program(flash_config_t *config, FMU_Type *base, uint32_t start, uint8_t *src, uint32_t lengthInBytes);
171 
172 /*!
173  * @brief Programs flash pages with data at locations passed in through parameters.
174  */
175 status_t FLASH_ProgramPage(
176     flash_config_t *config, FMU_Type *base, uint32_t start, uint8_t *src, uint32_t lengthInBytes);
177 
178 /*! @} */
179 
180 /*!
181  * @name Verification
182  * @{
183  */
184 
185 /*!
186  * @brief Verify that the flash phrases are erased
187  */
188 status_t FLASH_VerifyErasePhrase(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes);
189 
190 /*!
191  * @brief Verify that the flash pages are erased
192  */
193 status_t FLASH_VerifyErasePage(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes);
194 
195 /*!
196  * @brief Verify that the flash sectors are erased
197  */
198 status_t FLASH_VerifyEraseSector(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes);
199 
200 /*!
201  * @brief Verify that all flash and IFR space is erased
202  */
203 status_t FLASH_VerifyEraseAll(FMU_Type *base);
204 
205 /*!
206  * @brief Verify that a flash block is erased
207  */
208 status_t FLASH_VerifyEraseBlock(flash_config_t *config, FMU_Type *base, uint32_t blockaddr);
209 
210 /*!
211  * @brief Verify that the ifr phrases are erased
212  */
213 status_t FLASH_VerifyEraseIFRPhrase(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes);
214 
215 /*!
216  * @brief Verify that the ifr pages are erased
217  */
218 status_t FLASH_VerifyEraseIFRPage(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes);
219 
220 /*!
221  * @brief Verify that the ifr sectors are erased
222  */
223 status_t FLASH_VerifyEraseIFRSector(flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t lengthInBytes);
224 
225 /*! @} */
226 
227 /*!
228  * @name Properties
229  * @{
230  */
231 
232 /*!
233  * @brief Returns the desired flash property.
234  */
235 status_t FLASH_GetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value);
236 
237 /*! @} */
238 
239 /*!
240  * @name Read (Main Array or IFR) Into MISR
241  * @{
242  */
243 
244 /*!
245  * @brief Read into MISR
246  *
247  * The Read into MISR operation generates a signature based on the contents of the selected
248  * flash memory using an embedded MISR.
249  */
250 status_t Read_Into_MISR(
251     flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t ending, uint32_t *seed, uint32_t *signature);
252 
253 /*!
254  * @brief Read IFR into MISR
255  *
256  * The Read IFR into MISR operation generates a signature based on the contents of the
257  * selected IFR space using an embedded MISR.
258  */
259 status_t Read_IFR_Into_MISR(
260     flash_config_t *config, FMU_Type *base, uint32_t start, uint32_t ending, uint32_t *seed, uint32_t *signature);
261 
262 /*! @} */
263 
264 void flash_cache_disable(void);
265 
266 void flash_cache_speculation_control(bool isPreProcess, FMU_Type *base);
267 
268 #if defined(__cplusplus)
269 }
270 #endif
271 
272 /*! @}*/
273 
274 #endif /* FSL_K4_FLASH_H_ */
275