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_CONTROLLER_H_
10 #define FSL_K4_CONTROLLER_H_
11 
12 #include "fsl_common.h"
13 #include "fsl_flash_adapter.h"
14 #include "fsl_flash_utilities.h"
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 /*!
19  * @name FLASH status
20  * @{
21  */
22 /*! @brief FLASH driver status group. */
23 #if defined(kStatusGroup_FlashDriver)
24 #define kStatusGroupGeneric     kStatusGroup_Generic
25 #define kStatusGroupFlashDriver kStatusGroup_FlashDriver
26 #elif defined(kStatusGroup_FLASH)
27 #define kStatusGroupGeneric     kStatusGroup_Generic
28 #define kStatusGroupFlashDriver kStatusGroup_FLASH
29 #else
30 #define kStatusGroupGeneric     0
31 #define kStatusGroupFlashDriver 1
32 #endif
33 
34 /*!
35  * @brief FLASH driver status codes.
36  */
37 enum
38 {
39     kStatus_FLASH_Success         = MAKE_STATUS(kStatusGroupGeneric, 0),     /*!< API is executed successfully*/
40     kStatus_FLASH_InvalidArgument = MAKE_STATUS(kStatusGroupGeneric, 4),     /*!< Invalid argument*/
41     kStatus_FLASH_SizeError       = MAKE_STATUS(kStatusGroupFlashDriver, 0), /*!< Error size*/
42     kStatus_FLASH_AlignmentError =
43         MAKE_STATUS(kStatusGroupFlashDriver, 1), /*!< Parameter is not aligned with the specified baseline*/
44     kStatus_FLASH_AddressError = MAKE_STATUS(kStatusGroupFlashDriver, 2), /*!< Address is out of range */
45     kStatus_FLASH_AccessError =
46         MAKE_STATUS(kStatusGroupFlashDriver, 3), /*!< Invalid instruction codes and out-of bound addresses */
47     kStatus_FLASH_ProtectionViolation = MAKE_STATUS(
48         kStatusGroupFlashDriver, 4), /*!< The program/erase operation is requested to execute on protected areas */
49     kStatus_FLASH_CommandFailure =
50         MAKE_STATUS(kStatusGroupFlashDriver, 5), /*!< Run-time error during command execution. */
51     kStatus_FLASH_UnknownProperty = MAKE_STATUS(kStatusGroupFlashDriver, 6), /*!< Unknown property.*/
52     kStatus_FLASH_EraseKeyError   = MAKE_STATUS(kStatusGroupFlashDriver, 7), /*!< API erase key is invalid.*/
53     kStatus_FLASH_RegionExecuteOnly =
54         MAKE_STATUS(kStatusGroupFlashDriver, 8), /*!< The current region is execute-only.*/
55     kStatus_FLASH_ExecuteInRamFunctionNotReady =
56         MAKE_STATUS(kStatusGroupFlashDriver, 9), /*!< Execute-in-RAM function is not available.*/
57     kStatus_FLASH_PartitionStatusUpdateFailure =
58         MAKE_STATUS(kStatusGroupFlashDriver, 10), /*!< Failed to update partition status.*/
59     kStatus_FLASH_SetFlexramAsEepromError =
60         MAKE_STATUS(kStatusGroupFlashDriver, 11), /*!< Failed to set FlexRAM as EEPROM.*/
61     kStatus_FLASH_RecoverFlexramAsRamError =
62         MAKE_STATUS(kStatusGroupFlashDriver, 12), /*!< Failed to recover FlexRAM as RAM.*/
63     kStatus_FLASH_SetFlexramAsRamError = MAKE_STATUS(kStatusGroupFlashDriver, 13), /*!< Failed to set FlexRAM as RAM.*/
64     kStatus_FLASH_RecoverFlexramAsEepromError =
65         MAKE_STATUS(kStatusGroupFlashDriver, 14), /*!< Failed to recover FlexRAM as EEPROM.*/
66     kStatus_FLASH_CommandNotSupported = MAKE_STATUS(kStatusGroupFlashDriver, 15), /*!< Flash API is not supported.*/
67     kStatus_FLASH_SwapSystemNotInUninitialized =
68         MAKE_STATUS(kStatusGroupFlashDriver, 16), /*!< Swap system is not in an uninitialzed state.*/
69     kStatus_FLASH_SwapIndicatorAddressError =
70         MAKE_STATUS(kStatusGroupFlashDriver, 17), /*!< The swap indicator address is invalid.*/
71     kStatus_FLASH_ReadOnlyProperty = MAKE_STATUS(kStatusGroupFlashDriver, 18), /*!< The flash property is read-only.*/
72     kStatus_FLASH_InvalidPropertyValue =
73         MAKE_STATUS(kStatusGroupFlashDriver, 19), /*!< The flash property value is out of range.*/
74     kStatus_FLASH_InvalidSpeculationOption =
75         MAKE_STATUS(kStatusGroupFlashDriver, 20), /*!< The option of flash prefetch speculation is invalid.*/
76     kStatus_FLASH_CommandAborOption =
77         MAKE_STATUS(kStatusGroupFlashDriver, 21), /*!< The option of flash prefetch speculation is invalid.*/
78     kStatus_FLASH_EccFaultDetected = MAKE_STATUS(kStatusGroupFlashDriver, 22), /*!< An ECC double fault occurred.*/
79 };
80 /*! @} */
81 /*******************************************************************************
82  * API
83  ******************************************************************************/
84 
85 #if defined(__cplusplus)
86 extern "C" {
87 #endif
88 
89 /*!
90  * @name Erasing
91  * @{
92  */
93 
94 /*!
95  * @brief Erases the flash sectors encompassed by parameters passed into function.
96  *
97  * This function erases the appropriate number of flash sectors based on the
98  * desired start address and length.
99  */
100 status_t FLASH_CMD_EraseSector(FMU_Type *base, uint32_t start);
101 
102 /*!
103  * @brief Erase all flash and IFR space
104  */
105 status_t FLASH_CMD_EraseAll(FMU_Type *base);
106 
107 /*! @} */
108 
109 /*!
110  * @name Programming
111  * @{
112  */
113 
114 /*!
115  * @brief Programs flash phrases with data at locations passed in through parameters.
116  *
117  * This function programs the flash memory with the desired data for a given
118  * flash area as determined by the start address and the length.
119  */
120 status_t FLASH_CMD_ProgramPhrase(FMU_Type *base, uint32_t start, uint32_t *src);
121 
122 /*!
123  * @brief Programs flash pages with data at locations passed in through parameters.
124  *
125  * This function programs the flash memory with the desired data for a given
126  * flash area as determined by the start address and the length.
127  */
128 status_t FLASH_CMD_ProgramPage(FMU_Type *base, uint32_t start, uint32_t *src);
129 
130 /*! @} */
131 
132 /*!
133  * @name Verification
134  * @{
135  */
136 
137 /*!
138  * @brief Verify that the flash phrases are erased
139  *
140  * This function checks the appropriate number of flash phrases based on
141  * the desired start address and length to check whether the flash is erased
142  */
143 status_t FLASH_CMD_VerifyErasePhrase(FMU_Type *base, uint32_t start);
144 
145 /*!
146  * @brief Verify that the flash pages are erased
147  *
148  * This function checks the appropriate number of flash pages based on
149  * the desired start address and length to check whether the flash is erased
150  */
151 status_t FLASH_CMD_VerifyErasePage(FMU_Type *base, uint32_t start);
152 
153 /*!
154  * @brief Verify that the flash sectors are erased
155  *
156  * This function checks the appropriate number of flash sectors based on
157  * the desired start address and length to check whether the flash is erased
158  */
159 status_t FLASH_CMD_VerifyEraseSector(FMU_Type *base, uint32_t start);
160 
161 /*!
162  * @brief Verify that a flash block is erased
163  */
164 status_t FLASH_CMD_VerifyEraseBlock(FMU_Type *base, uint32_t blockaddr);
165 
166 /*!
167  * @brief Verify that all flash and IFR space is erased
168  */
169 status_t FLASH_CMD_VerifyEraseAll(FMU_Type *base);
170 
171 /*!
172  * @brief Verify that the ifr phrases are erased
173  *
174  * This function checks the appropriate number of ifr phrases based on
175  * the desired start address and length to check whether the flash is erased
176  */
177 status_t FLASH_CMD_VerifyEraseIFRPhrase(FMU_Type *base, uint32_t start);
178 
179 /*!
180  * @brief Verify that the ifr pages are erased
181  *
182  * This function checks the appropriate number of ifr pages based on
183  * the desired start address and length to check whether the flash is erased
184  */
185 status_t FLASH_CMD_VerifyEraseIFRPage(FMU_Type *base, uint32_t start);
186 
187 /*!
188  * @brief Verify that the ifr sectors are erased
189  *
190  * This function checks the appropriate number of ifr sectors based on
191  * the desired start address and length to check whether the flash is erased
192  */
193 status_t FLASH_CMD_VerifyEraseIFRSector(FMU_Type *base, uint32_t start);
194 
195 /*! @} */
196 
197 /*!
198  * @name Read Into MISR
199  * @{
200  */
201 
202 /*!
203  * @brief Read into MISR
204  *
205  * The Read into MISR operation generates a signature based on the contents of the selected
206  * flash memory using an embedded MISR.
207  */
208 status_t FLASH_CMD_ReadIntoMISR(FMU_Type *base, uint32_t start, uint32_t ending, uint32_t *seed, uint32_t *signature);
209 
210 /*!
211  * @brief Read IFR into MISR
212  *
213  * The Read IFR into MISR operation generates a signature based on the contents of the
214  * selected IFR space using an embedded MISR.
215  */
216 status_t FLASH_CMD_ReadIFRIntoMISR(
217     FMU_Type *base, uint32_t start, uint32_t ending, uint32_t *seed, uint32_t *signature);
218 
219 /*! @} */
220 
221 #if defined(__cplusplus)
222 }
223 #endif
224 
225 #endif /* FSL_K4_CONTROLLER_H_ */
226