rusEFI
The most advanced open source ECU
fsl_ftfx_flexnvm.c
Go to the documentation of this file.
1 /*
2 * The Clear BSD License
3 * Copyright 2013-2016 Freescale Semiconductor, Inc.
4 * Copyright 2016-2018 NXP
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted (subject to the limitations in the
9 * disclaimer below) provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
23 * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
24 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37 
38 #include "hal.h"
39 #include "fsl_ftfx_flexnvm.h"
40 
41 #if FSL_FEATURE_FLASH_HAS_FLEX_NVM
42 
43 /*******************************************************************************
44  * Definitions
45  ******************************************************************************/
46 
47 
48 /*******************************************************************************
49  * Prototypes
50  ******************************************************************************/
51 
52 /*! @brief Convert address for Flexnvm dflash.*/
54 
55 /*******************************************************************************
56  * Variables
57  ******************************************************************************/
58 
59 
60 /*******************************************************************************
61  * Code
62  ******************************************************************************/
63 
65 {
66  status_t returnCode;
67 
68  if (config == NULL)
69  {
71  }
72 
73  config->ftfxConfig.flashDesc.type = kFTFx_MemTypeFlexnvm;
74  config->ftfxConfig.flashDesc.index = 0;
75 
76  /* Set Flexnvm memory operation parameters */
77  config->ftfxConfig.opsConfig.addrAligment.blockWriteUnitSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_WRITE_UNIT_SIZE;
78  config->ftfxConfig.opsConfig.addrAligment.sectorCmd = FSL_FEATURE_FLASH_FLEX_NVM_SECTOR_CMD_ADDRESS_ALIGMENT;
79  config->ftfxConfig.opsConfig.addrAligment.sectionCmd = FSL_FEATURE_FLASH_FLEX_NVM_SECTION_CMD_ADDRESS_ALIGMENT;
80  config->ftfxConfig.opsConfig.addrAligment.resourceCmd = FSL_FEATURE_FLASH_FLEX_NVM_RESOURCE_CMD_ADDRESS_ALIGMENT;
81  config->ftfxConfig.opsConfig.addrAligment.checkCmd = FSL_FEATURE_FLASH_FLEX_NVM_CHECK_CMD_ADDRESS_ALIGMENT;
82 
83  /* Set Flexnvm memory properties */
84  config->ftfxConfig.flashDesc.blockBase = FSL_FEATURE_FLASH_FLEX_NVM_START_ADDRESS;
85  config->ftfxConfig.flashDesc.sectorSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_SECTOR_SIZE;
86  config->ftfxConfig.flashDesc.blockCount = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_COUNT;
87 
88  /* Init FTFx Kernel */
89  returnCode = FTFx_API_Init(&config->ftfxConfig);
90  if (returnCode != kStatus_FTFx_Success)
91  {
92  return returnCode;
93  }
94 
95  returnCode = FTFx_API_UpdateFlexnvmPartitionStatus(&config->ftfxConfig);
96  if (returnCode != kStatus_FTFx_Success)
97  {
98  return returnCode;
99  }
100 
101  return kStatus_FTFx_Success;
102 }
103 
104 status_t FLEXNVM_DflashErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
105 {
106  status_t returnCode;
107  returnCode = flexnvm_convert_start_address(config, start);
108  if (returnCode != kStatus_FTFx_Success)
109  {
110  return returnCode;
111  }
112 
113  return FTFx_CMD_Erase(&config->ftfxConfig, start, lengthInBytes, key);
114 }
115 
117 {
118  return FTFx_CMD_EraseAll(&config->ftfxConfig, key);
119 }
120 
121 #if defined(FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD) && FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD
123 {
124  return FTFx_CMD_EraseAllUnsecure(&config->ftfxConfig, key);
125 }
126 #endif
127 
128 status_t FLEXNVM_DflashProgram(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
129 {
130  status_t returnCode;
131  returnCode = flexnvm_convert_start_address(config, start);
132  if (returnCode != kStatus_FTFx_Success)
133  {
134  return returnCode;
135  }
136 
137  return FTFx_CMD_Program(&config->ftfxConfig, start, src, lengthInBytes);
138 }
139 
140 #if defined(FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD) && FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD
141 status_t FLEXNVM_DflashProgramSection(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
142 {
143  status_t returnCode;
144  returnCode = flexnvm_convert_start_address(config, start);
145  if (returnCode != kStatus_FTFx_Success)
146  {
147  return returnCode;
148  }
149 
150  return FTFx_CMD_ProgramSection(&config->ftfxConfig, start, src, lengthInBytes);
151 }
152 #endif
153 
156  uint32_t eepromDataSizeCode,
157  uint32_t flexnvmPartitionCode)
158 {
159  return FTFx_CMD_ProgramPartition(&config->ftfxConfig, option, eepromDataSizeCode, flexnvmPartitionCode);
160 }
161 
162 #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
164  uint32_t start,
165  uint8_t *dst,
166  uint32_t lengthInBytes,
168 {
169  return FTFx_CMD_ReadResource(&config->ftfxConfig, start, dst, lengthInBytes, option);
170 }
171 #endif
172 
173 status_t FLEXNVM_DflashVerifyErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, ftfx_margin_value_t margin)
174 {
175  status_t returnCode;
176  returnCode = flexnvm_convert_start_address(config, start);
177  if (returnCode != kStatus_FTFx_Success)
178  {
179  return returnCode;
180  }
181 
182  return FTFx_CMD_VerifyErase(&config->ftfxConfig, start, lengthInBytes, margin);
183 }
184 
186 {
187  return FTFx_CMD_VerifyEraseAll(&config->ftfxConfig, margin);
188 }
189 
191  uint32_t start,
192  uint32_t lengthInBytes,
193  const uint8_t *expectedData,
194  ftfx_margin_value_t margin,
195  uint32_t *failedAddress,
196  uint32_t *failedData)
197 {
198  status_t returnCode;
199  returnCode = flexnvm_convert_start_address(config, start);
200  if (returnCode != kStatus_FTFx_Success)
201  {
202  return returnCode;
203  }
204 
205  return FTFx_CMD_VerifyProgram(&config->ftfxConfig, start, lengthInBytes, expectedData, margin, failedAddress, failedData);
206 }
207 
209 {
210  return FTFx_REG_GetSecurityState(&config->ftfxConfig, state);
211 }
212 
214 {
215  return FTFx_CMD_SecurityBypass(&config->ftfxConfig, backdoorKey);
216 }
217 
218 #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
220 {
221  return FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, option);
222 }
223 #endif
224 
225 status_t FLEXNVM_EepromWrite(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
226 {
227  status_t returnCode;
228  bool needSwitchFlexRamMode = false;
229 
230  if (config == NULL)
231  {
233  }
234 
235  /* Validates the range of the given address */
236  if ((start < config->ftfxConfig.flexramBlockBase) ||
237  ((start + lengthInBytes) > (config->ftfxConfig.flexramBlockBase + config->ftfxConfig.eepromTotalSize)))
238  {
240  }
241 
242  returnCode = kStatus_FTFx_Success;
243 
244  /* Switch function of FlexRAM if needed */
245  if (!(FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
246  {
247  needSwitchFlexRamMode = true;
248 
250  if (returnCode != kStatus_FTFx_Success)
251  {
253  }
254  }
255 
256  /* Write data to FlexRAM when it is used as EEPROM emulator */
257  while (lengthInBytes > 0)
258  {
259  if ((!(start & 0x3U)) && (lengthInBytes >= 4))
260  {
261  *(uint32_t *)start = *(uint32_t *)src;
262  start += 4;
263  src += 4;
264  lengthInBytes -= 4;
265  }
266  else if ((!(start & 0x1U)) && (lengthInBytes >= 2))
267  {
268  *(uint16_t *)start = *(uint16_t *)src;
269  start += 2;
270  src += 2;
271  lengthInBytes -= 2;
272  }
273  else
274  {
275  *(uint8_t *)start = *src;
276  start += 1;
277  src += 1;
278  lengthInBytes -= 1;
279  }
280  /* Wait till EEERDY bit is set */
281  while (!(FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
282  {
283  }
284 
285  /* Check for protection violation error */
286  if (FTFx->FSTAT & FTFx_FSTAT_FPVIOL_MASK)
287  {
289  }
290  }
291 
292  /* Switch function of FlexRAM if needed */
293  if (needSwitchFlexRamMode)
294  {
296  if (returnCode != kStatus_FTFx_Success)
297  {
299  }
300  }
301 
302  return returnCode;
303 }
304 
306 {
307  if (config == NULL)
308  {
310  }
311 
312  if ((config->ftfxConfig.flashDesc.totalSize == 0) || (config->ftfxConfig.flashDesc.totalSize == 0xFFFFFFFFU))
313  {
315  }
316 
317  FTFx->FDPROT = protectStatus;
318 
319  if (FTFx->FDPROT != protectStatus)
320  {
322  }
323 
324  return kStatus_FTFx_Success;
325 }
326 
328 {
329  if ((config == NULL) || (protectStatus == NULL))
330  {
332  }
333 
334  if ((config->ftfxConfig.flashDesc.totalSize == 0) || (config->ftfxConfig.flashDesc.totalSize == 0xFFFFFFFFU))
335  {
337  }
338 
339  *protectStatus = FTFx->FDPROT;
340 
341  return kStatus_FTFx_Success;
342 }
343 
345 {
346  if (config == NULL)
347  {
349  }
350 
351  if ((config->ftfxConfig.eepromTotalSize == 0) || (config->ftfxConfig.eepromTotalSize == 0xFFFFU))
352  {
354  }
355 
356  FTFx->FEPROT = protectStatus;
357 
358  if (FTFx->FEPROT != protectStatus)
359  {
361  }
362 
363  return kStatus_FTFx_Success;
364 }
365 
367 {
368  if ((config == NULL) || (protectStatus == NULL))
369  {
371  }
372 
373  if ((config->ftfxConfig.eepromTotalSize == 0) || (config->ftfxConfig.eepromTotalSize == 0xFFFFU))
374  {
376  }
377 
378  *protectStatus = FTFx->FEPROT;
379 
380  return kStatus_FTFx_Success;
381 }
382 
384 {
385  if ((config == NULL) || (value == NULL))
386  {
388  }
389 
390  switch (whichProperty)
391  {
393  *value = config->ftfxConfig.flashDesc.sectorSize;
394  break;
396  *value = config->ftfxConfig.flashDesc.totalSize;
397  break;
399  *value = config->ftfxConfig.flashDesc.totalSize / config->ftfxConfig.flashDesc.blockCount;
400  break;
402  *value = config->ftfxConfig.flashDesc.blockCount;
403  break;
405  *value = config->ftfxConfig.flashDesc.blockBase;
406  break;
408  *value = config->ftfxConfig.flexramBlockBase;
409  break;
411  *value = config->ftfxConfig.flexramTotalSize;
412  break;
414  *value = config->ftfxConfig.eepromTotalSize;
415  break;
416 
417  default: /* catch inputs that are not recognized */
419  }
420 
421  return kStatus_FTFx_Success;
422 }
423 
425 {
426  if (config == NULL)
427  {
429  }
430 
431  /* From Spec: When required by the command, address bit 23 selects between program flash memory
432  * (=0) and data flash memory (=1).*/
433  config->ftfxConfig.opsConfig.convertedAddress = start - config->ftfxConfig.flashDesc.blockBase + 0x800000U;
434 
435  return kStatus_FTFx_Success;
436 }
437 
438 #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM */
439 
status_t FTFx_CMD_Program(ftfx_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
Programs flash with data at locations passed in through parameters.
status_t FTFx_REG_GetSecurityState(ftfx_config_t *config, ftfx_security_state_t *state)
Returns the security state via the pointer passed into the function.
status_t FTFx_CMD_ProgramSection(ftfx_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
Programs flash with data at locations passed in through parameters via the Program Section command.
status_t FTFx_CMD_VerifyProgram(ftfx_config_t *config, uint32_t start, uint32_t lengthInBytes, const uint8_t *expectedData, ftfx_margin_value_t margin, uint32_t *failedAddress, uint32_t *failedData)
Verifies programming of the desired flash area at a specified margin level.
status_t FTFx_API_UpdateFlexnvmPartitionStatus(ftfx_config_t *config)
Updates FlexNVM memory partition status according to data flash 0 IFR.
status_t FTFx_API_Init(ftfx_config_t *config)
Initializes the global flash properties structure members.
status_t FTFx_CMD_EraseAll(ftfx_config_t *config, uint32_t key)
Erases entire flash.
status_t FTFx_CMD_EraseAllUnsecure(ftfx_config_t *config, uint32_t key)
Erases the entire flash, including protected sectors.
status_t FTFx_CMD_VerifyEraseAll(ftfx_config_t *config, ftfx_margin_value_t margin)
Verifies erasure of the entire flash at a specified margin level.
status_t FTFx_CMD_ReadResource(ftfx_config_t *config, uint32_t start, uint8_t *dst, uint32_t lengthInBytes, ftfx_read_resource_opt_t option)
Reads the resource with data at locations passed in through parameters.
status_t FTFx_CMD_Erase(ftfx_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
Erases the flash sectors encompassed by parameters passed into function.
status_t FTFx_CMD_SecurityBypass(ftfx_config_t *config, const uint8_t *backdoorKey)
Allows users to bypass security with a backdoor key.
status_t FTFx_CMD_VerifyErase(ftfx_config_t *config, uint32_t start, uint32_t lengthInBytes, ftfx_margin_value_t margin)
Verifies an erasure of the desired flash area at a specified margin level.
status_t FTFx_CMD_SetFlexramFunction(ftfx_config_t *config, ftfx_flexram_func_opt_t option)
Sets the FlexRAM function command.
status_t FTFx_CMD_ProgramPartition(ftfx_config_t *config, ftfx_partition_flexram_load_opt_t option, uint32_t eepromDataSizeCode, uint32_t flexnvmPartitionCode)
Prepares the FlexNVM block for use as data flash, EEPROM backup, or a combination of both and initial...
enum _ftfx_read_resource_opt ftfx_read_resource_opt_t
Enumeration for the two possible options of flash read resource command.
@ kStatus_FTFx_CommandFailure
@ kStatus_FTFx_UnknownProperty
@ kStatus_FTFx_RecoverFlexramAsRamError
@ kStatus_FTFx_Success
@ kStatus_FTFx_InvalidArgument
@ kStatus_FTFx_AddressError
@ kStatus_FTFx_CommandNotSupported
@ kStatus_FTFx_ProtectionViolation
@ kStatus_FTFx_SetFlexramAsEepromError
enum _ftfx_margin_value ftfx_margin_value_t
Enumeration for supported FTFx margin levels.
@ kFTFx_FlexramFuncOptAvailableForEeprom
@ kFTFx_FlexramFuncOptAvailableAsRam
enum _ftfx_flexram_function_option ftfx_flexram_func_opt_t
Enumeration for the two possilbe options of set FlexRAM function command.
@ kFTFx_MemTypeFlexnvm
enum _ftfx_security_state ftfx_security_state_t
Enumeration for the three possible FTFx security states.
enum _ftfx_partition_flexram_load_option ftfx_partition_flexram_load_opt_t
Enumeration for the FlexRAM load during reset option.
static status_t flexnvm_convert_start_address(flexnvm_config_t *config, uint32_t start)
Convert address for Flexnvm dflash.
status_t FLEXNVM_EraseAllUnsecure(flexnvm_config_t *config, uint32_t key)
Erases the entire flexnvm, including protected sectors.
status_t FLEXNVM_SecurityBypass(flexnvm_config_t *config, const uint8_t *backdoorKey)
Allows users to bypass security with a backdoor key.
status_t FLEXNVM_DflashSetProtection(flexnvm_config_t *config, uint8_t protectStatus)
Sets the DFlash protection to the intended protection status.
enum _flexnvm_property_tag flexnvm_property_tag_t
Enumeration for various flexnvm properties.
status_t FLEXNVM_DflashVerifyProgram(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, const uint8_t *expectedData, ftfx_margin_value_t margin, uint32_t *failedAddress, uint32_t *failedData)
Verifies programming of the desired flash area at a specified margin level.
status_t FLEXNVM_EepromWrite(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
Programs the EEPROM with data at locations passed in through parameters.
status_t FLEXNVM_VerifyEraseAll(flexnvm_config_t *config, ftfx_margin_value_t margin)
Verifies erasure of the entire flash at a specified margin level.
status_t FLEXNVM_GetSecurityState(flexnvm_config_t *config, ftfx_security_state_t *state)
Returns the security state via the pointer passed into the function.
status_t FLEXNVM_DflashErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
Erases the Dflash sectors encompassed by parameters passed into function.
status_t FLEXNVM_EepromSetProtection(flexnvm_config_t *config, uint8_t protectStatus)
Sets the EEPROM protection to the intended protection status.
status_t FLEXNVM_DflashProgram(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
Programs flash with data at locations passed in through parameters.
status_t FLEXNVM_EraseAll(flexnvm_config_t *config, uint32_t key)
Erases entire flexnvm.
status_t FLEXNVM_DflashProgramSection(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
Programs flash with data at locations passed in through parameters via the Program Section command.
status_t FLEXNVM_ProgramPartition(flexnvm_config_t *config, ftfx_partition_flexram_load_opt_t option, uint32_t eepromDataSizeCode, uint32_t flexnvmPartitionCode)
Prepares the FlexNVM block for use as data flash, EEPROM backup, or a combination of both and initial...
status_t FLEXNVM_Init(flexnvm_config_t *config)
Initializes the global flash properties structure members.
status_t FLEXNVM_ReadResource(flexnvm_config_t *config, uint32_t start, uint8_t *dst, uint32_t lengthInBytes, ftfx_read_resource_opt_t option)
Reads the resource with data at locations passed in through parameters.
status_t FLEXNVM_GetProperty(flexnvm_config_t *config, flexnvm_property_tag_t whichProperty, uint32_t *value)
Returns the desired flexnvm property.
status_t FLEXNVM_DflashVerifyErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, ftfx_margin_value_t margin)
Verifies an erasure of the desired flash area at a specified margin level.
status_t FLEXNVM_EepromGetProtection(flexnvm_config_t *config, uint8_t *protectStatus)
Gets the EEPROM protection status.
status_t FLEXNVM_DflashGetProtection(flexnvm_config_t *config, uint8_t *protectStatus)
Gets the DFlash protection status.
status_t FLEXNVM_SetFlexramFunction(flexnvm_config_t *config, ftfx_flexram_func_opt_t option)
Sets the FlexRAM function command.
@ kFLEXNVM_PropertyDflashBlockSize
@ kFLEXNVM_PropertyDflashBlockCount
@ kFLEXNVM_PropertyEepromTotalSize
@ kFLEXNVM_PropertyFlexRamBlockBaseAddr
@ kFLEXNVM_PropertyDflashSectorSize
@ kFLEXNVM_PropertyFlexRamTotalSize
@ kFLEXNVM_PropertyDflashTotalSize
@ kFLEXNVM_PropertyDflashBlockBaseAddr
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:169
static status_t protectStatus
Definition: flash_int.cpp:27
persistent_config_s * config
static ScState state
Flexnvm driver state information.