Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

507 řádky
17 KiB

  1. /*
  2. * Copyright 2013-2016 Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. *
  8. */
  9. #include "fsl_ftfx_flexnvm.h"
  10. #if FSL_FEATURE_FLASH_HAS_FLEX_NVM
  11. /*******************************************************************************
  12. * Definitions
  13. ******************************************************************************/
  14. /*******************************************************************************
  15. * Prototypes
  16. ******************************************************************************/
  17. /*! @brief Convert address for Flexnvm dflash.*/
  18. static status_t flexnvm_convert_start_address(flexnvm_config_t *config, uint32_t start);
  19. /*******************************************************************************
  20. * Variables
  21. ******************************************************************************/
  22. /*******************************************************************************
  23. * Code
  24. ******************************************************************************/
  25. status_t FLEXNVM_Init(flexnvm_config_t *config)
  26. {
  27. status_t returnCode;
  28. if (config == NULL)
  29. {
  30. return kStatus_FTFx_InvalidArgument;
  31. }
  32. config->ftfxConfig.flashDesc.type = (uint8_t)kFTFx_MemTypeFlexnvm;
  33. config->ftfxConfig.flashDesc.index = 0U;
  34. /* Set Flexnvm memory operation parameters */
  35. config->ftfxConfig.opsConfig.addrAligment.blockWriteUnitSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_WRITE_UNIT_SIZE;
  36. config->ftfxConfig.opsConfig.addrAligment.sectorCmd = FSL_FEATURE_FLASH_FLEX_NVM_SECTOR_CMD_ADDRESS_ALIGMENT;
  37. config->ftfxConfig.opsConfig.addrAligment.sectionCmd = FSL_FEATURE_FLASH_FLEX_NVM_SECTION_CMD_ADDRESS_ALIGMENT;
  38. config->ftfxConfig.opsConfig.addrAligment.resourceCmd = FSL_FEATURE_FLASH_FLEX_NVM_RESOURCE_CMD_ADDRESS_ALIGMENT;
  39. config->ftfxConfig.opsConfig.addrAligment.checkCmd = FSL_FEATURE_FLASH_FLEX_NVM_CHECK_CMD_ADDRESS_ALIGMENT;
  40. /* Set Flexnvm memory properties */
  41. config->ftfxConfig.flashDesc.blockBase = FSL_FEATURE_FLASH_FLEX_NVM_START_ADDRESS;
  42. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS) && FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS
  43. config->ftfxConfig.flashDesc.aliasBlockBase = FSL_FEATURE_FLASH_FLEX_NVM_ALIAS_START_ADDRESS;
  44. #endif
  45. config->ftfxConfig.flashDesc.sectorSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_SECTOR_SIZE;
  46. config->ftfxConfig.flashDesc.blockCount = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_COUNT;
  47. /* Init FTFx Kernel */
  48. FTFx_API_Init(&config->ftfxConfig);
  49. returnCode = FTFx_API_UpdateFlexnvmPartitionStatus(&config->ftfxConfig);
  50. if (returnCode != kStatus_FTFx_Success)
  51. {
  52. return returnCode;
  53. }
  54. return kStatus_FTFx_Success;
  55. }
  56. /*!
  57. * @brief Erases the Dflash sectors encompassed by parameters passed into function.
  58. */
  59. status_t FLEXNVM_DflashErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
  60. {
  61. status_t returnCode;
  62. returnCode = flexnvm_convert_start_address(config, start);
  63. if (returnCode != kStatus_FTFx_Success)
  64. {
  65. return returnCode;
  66. }
  67. return FTFx_CMD_Erase(&config->ftfxConfig, start, lengthInBytes, key);
  68. }
  69. /*!
  70. * @brief Erases entire flexnvm
  71. */
  72. status_t FLEXNVM_EraseAll(flexnvm_config_t *config, uint32_t key)
  73. {
  74. return FTFx_CMD_EraseAll(&config->ftfxConfig, key);
  75. }
  76. #if defined(FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD) && FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD
  77. /*!
  78. * @brief Erases the entire flexnvm, including protected sectors.
  79. */
  80. status_t FLEXNVM_EraseAllUnsecure(flexnvm_config_t *config, uint32_t key)
  81. {
  82. return FTFx_CMD_EraseAllUnsecure(&config->ftfxConfig, key);
  83. }
  84. #endif /* FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD */
  85. /*!
  86. * @brief Programs flash with data at locations passed in through parameters.
  87. */
  88. status_t FLEXNVM_DflashProgram(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  89. {
  90. status_t returnCode;
  91. returnCode = flexnvm_convert_start_address(config, start);
  92. if (returnCode != kStatus_FTFx_Success)
  93. {
  94. return returnCode;
  95. }
  96. return FTFx_CMD_Program(&config->ftfxConfig, start, src, lengthInBytes);
  97. }
  98. #if defined(FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD) && FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD
  99. /*!
  100. * @brief Programs flash with data at locations passed in through parameters via the Program Section command.
  101. */
  102. status_t FLEXNVM_DflashProgramSection(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  103. {
  104. status_t returnCode;
  105. /* Convert address for Flexnvm dflash */
  106. returnCode = flexnvm_convert_start_address(config, start);
  107. if (returnCode != kStatus_FTFx_Success)
  108. {
  109. return returnCode;
  110. }
  111. return FTFx_CMD_ProgramSection(&config->ftfxConfig, start, src, lengthInBytes);
  112. }
  113. #endif /* FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD */
  114. /*!
  115. * @brief Prepares the FlexNVM block for use as data flash, EEPROM backup, or a combination
  116. * of both and initializes the FlexRAM.
  117. */
  118. status_t FLEXNVM_ProgramPartition(flexnvm_config_t *config,
  119. ftfx_partition_flexram_load_opt_t option,
  120. uint32_t eepromDataSizeCode,
  121. uint32_t flexnvmPartitionCode)
  122. {
  123. return FTFx_CMD_ProgramPartition(&config->ftfxConfig, option, eepromDataSizeCode, flexnvmPartitionCode);
  124. }
  125. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  126. /*!
  127. * @brief Reads the resource with data at locations passed in through parameters.
  128. */
  129. status_t FLEXNVM_ReadResource(
  130. flexnvm_config_t *config, uint32_t start, uint8_t *dst, uint32_t lengthInBytes, ftfx_read_resource_opt_t option)
  131. {
  132. return FTFx_CMD_ReadResource(&config->ftfxConfig, start, dst, lengthInBytes, option);
  133. }
  134. #endif /* FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD */
  135. /*!
  136. * @brief Verifies an erasure of the desired flash area at a specified margin level.
  137. */
  138. status_t FLEXNVM_DflashVerifyErase(flexnvm_config_t *config,
  139. uint32_t start,
  140. uint32_t lengthInBytes,
  141. ftfx_margin_value_t margin)
  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. return FTFx_CMD_VerifyErase(&config->ftfxConfig, start, lengthInBytes, margin);
  150. }
  151. /*!
  152. * @brief Verifies erasure of the entire flash at a specified margin level.
  153. */
  154. status_t FLEXNVM_VerifyEraseAll(flexnvm_config_t *config, ftfx_margin_value_t margin)
  155. {
  156. return FTFx_CMD_VerifyEraseAll(&config->ftfxConfig, margin);
  157. }
  158. /*!
  159. * @brief Verifies programming of the desired flash area at a specified margin level.
  160. */
  161. status_t FLEXNVM_DflashVerifyProgram(flexnvm_config_t *config,
  162. uint32_t start,
  163. uint32_t lengthInBytes,
  164. const uint8_t *expectedData,
  165. ftfx_margin_value_t margin,
  166. uint32_t *failedAddress,
  167. uint32_t *failedData)
  168. {
  169. status_t returnCode;
  170. returnCode = flexnvm_convert_start_address(config, start);
  171. if (returnCode != kStatus_FTFx_Success)
  172. {
  173. return returnCode;
  174. }
  175. return FTFx_CMD_VerifyProgram(&config->ftfxConfig, start, lengthInBytes, expectedData, margin, failedAddress,
  176. failedData);
  177. }
  178. /*!
  179. * @brief Returns the security state via the pointer passed into the function.
  180. */
  181. status_t FLEXNVM_GetSecurityState(flexnvm_config_t *config, ftfx_security_state_t *state)
  182. {
  183. return FTFx_REG_GetSecurityState(&config->ftfxConfig, state);
  184. }
  185. /*!
  186. * @brief Allows users to bypass security with a backdoor key.
  187. */
  188. status_t FLEXNVM_SecurityBypass(flexnvm_config_t *config, const uint8_t *backdoorKey)
  189. {
  190. return FTFx_CMD_SecurityBypass(&config->ftfxConfig, backdoorKey);
  191. }
  192. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  193. /*!
  194. * @brief Sets the FlexRAM function command.
  195. */
  196. status_t FLEXNVM_SetFlexramFunction(flexnvm_config_t *config, ftfx_flexram_func_opt_t option)
  197. {
  198. return FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, option);
  199. }
  200. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  201. /*!
  202. * @brief Programs the EEPROM with data at locations passed in through parameters.
  203. */
  204. status_t FLEXNVM_EepromWrite(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  205. {
  206. status_t returnCode;
  207. bool needSwitchFlexRamMode = false;
  208. if (config == NULL)
  209. {
  210. return kStatus_FTFx_InvalidArgument;
  211. }
  212. /* Validates the range of the given address */
  213. if ((start < config->ftfxConfig.flexramBlockBase) ||
  214. ((start + lengthInBytes) > (config->ftfxConfig.flexramBlockBase + config->ftfxConfig.eepromTotalSize)))
  215. {
  216. return kStatus_FTFx_AddressError;
  217. }
  218. returnCode = kStatus_FTFx_Success;
  219. /* Switch function of FlexRAM if needed */
  220. if (0U == (FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
  221. {
  222. needSwitchFlexRamMode = true;
  223. returnCode = FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, kFTFx_FlexramFuncOptAvailableForEeprom);
  224. if (returnCode != kStatus_FTFx_Success)
  225. {
  226. return kStatus_FTFx_SetFlexramAsEepromError;
  227. }
  228. }
  229. /* Write data to FlexRAM when it is used as EEPROM emulator */
  230. while (lengthInBytes > 0U)
  231. {
  232. if ((0U == (start & 0x3U)) && (0U == ((uint32_t)src & 0x3U)) && (lengthInBytes >= 4U))
  233. {
  234. *(uint32_t *)start = *(uint32_t *)(uint32_t)src;
  235. start += 4U;
  236. src = &src[4];
  237. lengthInBytes -= 4U;
  238. }
  239. else if ((0U == (start & 0x1U)) && (0U == ((uint32_t)src & 0x1U)) && (lengthInBytes >= 2U))
  240. {
  241. *(uint16_t *)start = *(uint16_t *)(uint32_t)src;
  242. start += 2U;
  243. src = &src[2];
  244. lengthInBytes -= 2U;
  245. }
  246. else
  247. {
  248. *(uint8_t *)start = *src;
  249. start += 1U;
  250. src = &src[1];
  251. lengthInBytes -= 1U;
  252. }
  253. /* Wait till EEERDY bit is set */
  254. while (0U == (FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
  255. {
  256. }
  257. /* Check for protection violation error */
  258. if (0U != (FTFx->FSTAT & FTFx_FSTAT_FPVIOL_MASK))
  259. {
  260. return kStatus_FTFx_ProtectionViolation;
  261. }
  262. }
  263. /* Switch function of FlexRAM if needed */
  264. if (needSwitchFlexRamMode)
  265. {
  266. returnCode = FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, kFTFx_FlexramFuncOptAvailableAsRam);
  267. if (returnCode != kStatus_FTFx_Success)
  268. {
  269. return kStatus_FTFx_RecoverFlexramAsRamError;
  270. }
  271. }
  272. return returnCode;
  273. }
  274. /*!
  275. * @brief Sets the DFlash protection to the intended protection status.
  276. */
  277. status_t FLEXNVM_DflashSetProtection(flexnvm_config_t *config, uint8_t protectStatus)
  278. {
  279. if (config == NULL)
  280. {
  281. return kStatus_FTFx_InvalidArgument;
  282. }
  283. if ((config->ftfxConfig.flashDesc.totalSize == 0U) || (config->ftfxConfig.flashDesc.totalSize == 0xFFFFFFFFU))
  284. {
  285. return kStatus_FTFx_CommandNotSupported;
  286. }
  287. /* Individual data flash regions will be protected from program and erase operations by setting the
  288. * associated DPROT bit to the protected state. Each FDPROT bit only be changed from 1s to 0s,
  289. * meaning the protection can only be increased */
  290. FTFx->FDPROT = protectStatus;
  291. if (FTFx->FDPROT != protectStatus)
  292. {
  293. return kStatus_FTFx_CommandFailure;
  294. }
  295. return kStatus_FTFx_Success;
  296. }
  297. /*!
  298. * @brief Gets the DFlash protection status.
  299. */
  300. status_t FLEXNVM_DflashGetProtection(flexnvm_config_t *config, uint8_t *protectStatus)
  301. {
  302. if ((config == NULL) || (protectStatus == NULL))
  303. {
  304. return kStatus_FTFx_InvalidArgument;
  305. }
  306. if ((config->ftfxConfig.flashDesc.totalSize == 0U) || (config->ftfxConfig.flashDesc.totalSize == 0xFFFFFFFFU))
  307. {
  308. return kStatus_FTFx_CommandNotSupported;
  309. }
  310. /* return the flexnvm DFlash protection status */
  311. *protectStatus = FTFx->FDPROT;
  312. return kStatus_FTFx_Success;
  313. }
  314. /*!
  315. * @brief Sets the EEPROM protection to the intended protection status.
  316. */
  317. status_t FLEXNVM_EepromSetProtection(flexnvm_config_t *config, uint8_t protectStatus)
  318. {
  319. if (config == NULL)
  320. {
  321. return kStatus_FTFx_InvalidArgument;
  322. }
  323. if ((config->ftfxConfig.eepromTotalSize == 0U) || (config->ftfxConfig.eepromTotalSize == 0xFFFFU))
  324. {
  325. return kStatus_FTFx_CommandNotSupported;
  326. }
  327. /* Individual data flash regions will be protected from program and erase operations by setting the
  328. * associated FEPROT bit to the protected state; Each FEPROT bit only be changed from 1s to 0s,
  329. * meaning the protection can only be increased. */
  330. FTFx->FEPROT = protectStatus;
  331. if (FTFx->FEPROT != protectStatus)
  332. {
  333. return kStatus_FTFx_CommandFailure;
  334. }
  335. return kStatus_FTFx_Success;
  336. }
  337. /*!
  338. * @brief Gets the EEPROM protection status.
  339. */
  340. status_t FLEXNVM_EepromGetProtection(flexnvm_config_t *config, uint8_t *protectStatus)
  341. {
  342. if ((config == NULL) || (protectStatus == NULL))
  343. {
  344. return kStatus_FTFx_InvalidArgument;
  345. }
  346. if ((config->ftfxConfig.eepromTotalSize == 0U) || (config->ftfxConfig.eepromTotalSize == 0xFFFFU))
  347. {
  348. return kStatus_FTFx_CommandNotSupported;
  349. }
  350. /* return EEPROM protection status */
  351. *protectStatus = FTFx->FEPROT;
  352. return kStatus_FTFx_Success;
  353. }
  354. /*!
  355. * @brief Returns the desired flexnvm property.
  356. */
  357. status_t FLEXNVM_GetProperty(flexnvm_config_t *config, flexnvm_property_tag_t whichProperty, uint32_t *value)
  358. {
  359. if ((config == NULL) || (value == NULL))
  360. {
  361. return kStatus_FTFx_InvalidArgument;
  362. }
  363. status_t status = kStatus_FTFx_Success;
  364. switch (whichProperty)
  365. {
  366. /* get flexnvm date flash sector size */
  367. case kFLEXNVM_PropertyDflashSectorSize:
  368. *value = config->ftfxConfig.flashDesc.sectorSize;
  369. break;
  370. /* get flexnvm date flash total size */
  371. case kFLEXNVM_PropertyDflashTotalSize:
  372. *value = config->ftfxConfig.flashDesc.totalSize;
  373. break;
  374. /* get flexnvm date flash block size */
  375. case kFLEXNVM_PropertyDflashBlockSize:
  376. *value = config->ftfxConfig.flashDesc.totalSize / config->ftfxConfig.flashDesc.blockCount;
  377. break;
  378. /* get flexnvm date flash block cont */
  379. case kFLEXNVM_PropertyDflashBlockCount:
  380. *value = config->ftfxConfig.flashDesc.blockCount;
  381. break;
  382. /* get flexnvm date flash block base address */
  383. case kFLEXNVM_PropertyDflashBlockBaseAddr:
  384. *value = config->ftfxConfig.flashDesc.blockBase;
  385. break;
  386. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS) && FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS
  387. case kFLEXNVM_PropertyAliasDflashBlockBaseAddr:
  388. *value = config->ftfxConfig.flashDesc.aliasBlockBase;
  389. break;
  390. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS */
  391. case kFLEXNVM_PropertyFlexRamBlockBaseAddr:
  392. *value = config->ftfxConfig.flexramBlockBase;
  393. break;
  394. /* get flexram total size */
  395. case kFLEXNVM_PropertyFlexRamTotalSize:
  396. *value = config->ftfxConfig.flexramTotalSize;
  397. break;
  398. /* get flexnvm eeprom total size */
  399. case kFLEXNVM_PropertyEepromTotalSize:
  400. *value = config->ftfxConfig.eepromTotalSize;
  401. break;
  402. /* catch inputs that are not recognized */
  403. default:
  404. status = kStatus_FTFx_UnknownProperty;
  405. break;
  406. }
  407. return status;
  408. }
  409. /*! @brief Convert address for Flexnvm dflash.*/
  410. static status_t flexnvm_convert_start_address(flexnvm_config_t *config, uint32_t start)
  411. {
  412. status_t status = kStatus_FTFx_AddressError;
  413. if (config == NULL)
  414. {
  415. return kStatus_FTFx_InvalidArgument;
  416. }
  417. /* From Spec: When required by the command, address bit 23 selects between program flash memory
  418. * (=0) and data flash memory (=1).*/
  419. if (start >= config->ftfxConfig.flashDesc.blockBase)
  420. {
  421. config->ftfxConfig.opsConfig.convertedAddress = start - config->ftfxConfig.flashDesc.blockBase + 0x800000U;
  422. status = kStatus_FTFx_Success;
  423. }
  424. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS) && FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS
  425. /* for MKW39/38/37 which have alias blockBase */
  426. else if (start >= config->ftfxConfig.flashDesc.aliasBlockBase)
  427. {
  428. config->ftfxConfig.opsConfig.convertedAddress = start - config->ftfxConfig.flashDesc.aliasBlockBase + 0x800000U;
  429. status = kStatus_FTFx_Success;
  430. }
  431. else
  432. {
  433. status = kStatus_FTFx_Success;
  434. }
  435. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM_ALIAS */
  436. return status;
  437. }
  438. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM */