fsl_ftfx_flexnvm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include "fsl_ftfx_flexnvm.h"
  38. #if FSL_FEATURE_FLASH_HAS_FLEX_NVM
  39. /*******************************************************************************
  40. * Definitions
  41. ******************************************************************************/
  42. /*******************************************************************************
  43. * Prototypes
  44. ******************************************************************************/
  45. /*! @brief Convert address for Flexnvm dflash.*/
  46. static status_t flexnvm_convert_start_address(flexnvm_config_t *config, uint32_t start);
  47. /*******************************************************************************
  48. * Variables
  49. ******************************************************************************/
  50. /*******************************************************************************
  51. * Code
  52. ******************************************************************************/
  53. status_t FLEXNVM_Init(flexnvm_config_t *config)
  54. {
  55. status_t returnCode;
  56. if (config == NULL)
  57. {
  58. return kStatus_FTFx_InvalidArgument;
  59. }
  60. config->ftfxConfig.flashDesc.type = kFTFx_MemTypeFlexnvm;
  61. config->ftfxConfig.flashDesc.index = 0;
  62. /* Set Flexnvm memory operation parameters */
  63. config->ftfxConfig.opsConfig.addrAligment.blockWriteUnitSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_WRITE_UNIT_SIZE;
  64. config->ftfxConfig.opsConfig.addrAligment.sectorCmd = FSL_FEATURE_FLASH_FLEX_NVM_SECTOR_CMD_ADDRESS_ALIGMENT;
  65. config->ftfxConfig.opsConfig.addrAligment.sectionCmd = FSL_FEATURE_FLASH_FLEX_NVM_SECTION_CMD_ADDRESS_ALIGMENT;
  66. config->ftfxConfig.opsConfig.addrAligment.resourceCmd = FSL_FEATURE_FLASH_FLEX_NVM_RESOURCE_CMD_ADDRESS_ALIGMENT;
  67. config->ftfxConfig.opsConfig.addrAligment.checkCmd = FSL_FEATURE_FLASH_FLEX_NVM_CHECK_CMD_ADDRESS_ALIGMENT;
  68. /* Set Flexnvm memory properties */
  69. config->ftfxConfig.flashDesc.blockBase = FSL_FEATURE_FLASH_FLEX_NVM_START_ADDRESS;
  70. config->ftfxConfig.flashDesc.sectorSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_SECTOR_SIZE;
  71. config->ftfxConfig.flashDesc.blockCount = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_COUNT;
  72. /* Init FTFx Kernel */
  73. returnCode = FTFx_API_Init(&config->ftfxConfig);
  74. if (returnCode != kStatus_FTFx_Success)
  75. {
  76. return returnCode;
  77. }
  78. returnCode = FTFx_API_UpdateFlexnvmPartitionStatus(&config->ftfxConfig);
  79. if (returnCode != kStatus_FTFx_Success)
  80. {
  81. return returnCode;
  82. }
  83. return kStatus_FTFx_Success;
  84. }
  85. status_t FLEXNVM_DflashErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
  86. {
  87. status_t returnCode;
  88. returnCode = flexnvm_convert_start_address(config, start);
  89. if (returnCode != kStatus_FTFx_Success)
  90. {
  91. return returnCode;
  92. }
  93. return FTFx_CMD_Erase(&config->ftfxConfig, start, lengthInBytes, key);
  94. }
  95. status_t FLEXNVM_EraseAll(flexnvm_config_t *config, uint32_t key)
  96. {
  97. return FTFx_CMD_EraseAll(&config->ftfxConfig, key);
  98. }
  99. #if defined(FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD) && FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD
  100. status_t FLEXNVM_EraseAllUnsecure(flexnvm_config_t *config, uint32_t key)
  101. {
  102. return FTFx_CMD_EraseAllUnsecure(&config->ftfxConfig, key);
  103. }
  104. #endif
  105. status_t FLEXNVM_DflashProgram(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  106. {
  107. status_t returnCode;
  108. returnCode = flexnvm_convert_start_address(config, start);
  109. if (returnCode != kStatus_FTFx_Success)
  110. {
  111. return returnCode;
  112. }
  113. return FTFx_CMD_Program(&config->ftfxConfig, start, src, lengthInBytes);
  114. }
  115. #if defined(FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD) && FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD
  116. status_t FLEXNVM_DflashProgramSection(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  117. {
  118. status_t returnCode;
  119. returnCode = flexnvm_convert_start_address(config, start);
  120. if (returnCode != kStatus_FTFx_Success)
  121. {
  122. return returnCode;
  123. }
  124. return FTFx_CMD_ProgramSection(&config->ftfxConfig, start, src, lengthInBytes);
  125. }
  126. #endif
  127. status_t FLEXNVM_ProgramPartition(flexnvm_config_t *config,
  128. ftfx_partition_flexram_load_opt_t option,
  129. uint32_t eepromDataSizeCode,
  130. uint32_t flexnvmPartitionCode)
  131. {
  132. return FTFx_CMD_ProgramPartition(&config->ftfxConfig, option, eepromDataSizeCode, flexnvmPartitionCode);
  133. }
  134. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  135. status_t FLEXNVM_ReadResource(flexnvm_config_t *config,
  136. uint32_t start,
  137. uint8_t *dst,
  138. uint32_t lengthInBytes,
  139. ftfx_read_resource_opt_t option)
  140. {
  141. return FTFx_CMD_ReadResource(&config->ftfxConfig, start, dst, lengthInBytes, option);
  142. }
  143. #endif
  144. status_t FLEXNVM_DflashVerifyErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, ftfx_margin_value_t margin)
  145. {
  146. status_t returnCode;
  147. returnCode = flexnvm_convert_start_address(config, start);
  148. if (returnCode != kStatus_FTFx_Success)
  149. {
  150. return returnCode;
  151. }
  152. return FTFx_CMD_VerifyErase(&config->ftfxConfig, start, lengthInBytes, margin);
  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. status_t FLEXNVM_DflashVerifyProgram(flexnvm_config_t *config,
  159. uint32_t start,
  160. uint32_t lengthInBytes,
  161. const uint8_t *expectedData,
  162. ftfx_margin_value_t margin,
  163. uint32_t *failedAddress,
  164. uint32_t *failedData)
  165. {
  166. status_t returnCode;
  167. returnCode = flexnvm_convert_start_address(config, start);
  168. if (returnCode != kStatus_FTFx_Success)
  169. {
  170. return returnCode;
  171. }
  172. return FTFx_CMD_VerifyProgram(&config->ftfxConfig, start, lengthInBytes, expectedData, margin, failedAddress, failedData);
  173. }
  174. status_t FLEXNVM_GetSecurityState(flexnvm_config_t *config, ftfx_security_state_t *state)
  175. {
  176. return FTFx_REG_GetSecurityState(&config->ftfxConfig, state);
  177. }
  178. status_t FLEXNVM_SecurityBypass(flexnvm_config_t *config, const uint8_t *backdoorKey)
  179. {
  180. return FTFx_CMD_SecurityBypass(&config->ftfxConfig, backdoorKey);
  181. }
  182. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  183. status_t FLEXNVM_SetFlexramFunction(flexnvm_config_t *config, ftfx_flexram_func_opt_t option)
  184. {
  185. return FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, option);
  186. }
  187. #endif
  188. status_t FLEXNVM_EepromWrite(flexnvm_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  189. {
  190. status_t returnCode;
  191. bool needSwitchFlexRamMode = false;
  192. if (config == NULL)
  193. {
  194. return kStatus_FTFx_InvalidArgument;
  195. }
  196. /* Validates the range of the given address */
  197. if ((start < config->ftfxConfig.flexramBlockBase) ||
  198. ((start + lengthInBytes) > (config->ftfxConfig.flexramBlockBase + config->ftfxConfig.eepromTotalSize)))
  199. {
  200. return kStatus_FTFx_AddressError;
  201. }
  202. returnCode = kStatus_FTFx_Success;
  203. /* Switch function of FlexRAM if needed */
  204. if (!(FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
  205. {
  206. needSwitchFlexRamMode = true;
  207. returnCode = FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, kFTFx_FlexramFuncOptAvailableForEeprom);
  208. if (returnCode != kStatus_FTFx_Success)
  209. {
  210. return kStatus_FTFx_SetFlexramAsEepromError;
  211. }
  212. }
  213. /* Write data to FlexRAM when it is used as EEPROM emulator */
  214. while (lengthInBytes > 0)
  215. {
  216. if ((!(start & 0x3U)) && (lengthInBytes >= 4))
  217. {
  218. *(uint32_t *)start = *(uint32_t *)src;
  219. start += 4;
  220. src += 4;
  221. lengthInBytes -= 4;
  222. }
  223. else if ((!(start & 0x1U)) && (lengthInBytes >= 2))
  224. {
  225. *(uint16_t *)start = *(uint16_t *)src;
  226. start += 2;
  227. src += 2;
  228. lengthInBytes -= 2;
  229. }
  230. else
  231. {
  232. *(uint8_t *)start = *src;
  233. start += 1;
  234. src += 1;
  235. lengthInBytes -= 1;
  236. }
  237. /* Wait till EEERDY bit is set */
  238. while (!(FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
  239. {
  240. }
  241. /* Check for protection violation error */
  242. if (FTFx->FSTAT & FTFx_FSTAT_FPVIOL_MASK)
  243. {
  244. return kStatus_FTFx_ProtectionViolation;
  245. }
  246. }
  247. /* Switch function of FlexRAM if needed */
  248. if (needSwitchFlexRamMode)
  249. {
  250. returnCode = FTFx_CMD_SetFlexramFunction(&config->ftfxConfig, kFTFx_FlexramFuncOptAvailableAsRam);
  251. if (returnCode != kStatus_FTFx_Success)
  252. {
  253. return kStatus_FTFx_RecoverFlexramAsRamError;
  254. }
  255. }
  256. return returnCode;
  257. }
  258. status_t FLEXNVM_DflashSetProtection(flexnvm_config_t *config, uint8_t protectStatus)
  259. {
  260. if (config == NULL)
  261. {
  262. return kStatus_FTFx_InvalidArgument;
  263. }
  264. if ((config->ftfxConfig.flashDesc.totalSize == 0) || (config->ftfxConfig.flashDesc.totalSize == 0xFFFFFFFFU))
  265. {
  266. return kStatus_FTFx_CommandNotSupported;
  267. }
  268. FTFx->FDPROT = protectStatus;
  269. if (FTFx->FDPROT != protectStatus)
  270. {
  271. return kStatus_FTFx_CommandFailure;
  272. }
  273. return kStatus_FTFx_Success;
  274. }
  275. status_t FLEXNVM_DflashGetProtection(flexnvm_config_t *config, uint8_t *protectStatus)
  276. {
  277. if ((config == NULL) || (protectStatus == NULL))
  278. {
  279. return kStatus_FTFx_InvalidArgument;
  280. }
  281. if ((config->ftfxConfig.flashDesc.totalSize == 0) || (config->ftfxConfig.flashDesc.totalSize == 0xFFFFFFFFU))
  282. {
  283. return kStatus_FTFx_CommandNotSupported;
  284. }
  285. *protectStatus = FTFx->FDPROT;
  286. return kStatus_FTFx_Success;
  287. }
  288. status_t FLEXNVM_EepromSetProtection(flexnvm_config_t *config, uint8_t protectStatus)
  289. {
  290. if (config == NULL)
  291. {
  292. return kStatus_FTFx_InvalidArgument;
  293. }
  294. if ((config->ftfxConfig.eepromTotalSize == 0) || (config->ftfxConfig.eepromTotalSize == 0xFFFFU))
  295. {
  296. return kStatus_FTFx_CommandNotSupported;
  297. }
  298. FTFx->FEPROT = protectStatus;
  299. if (FTFx->FEPROT != protectStatus)
  300. {
  301. return kStatus_FTFx_CommandFailure;
  302. }
  303. return kStatus_FTFx_Success;
  304. }
  305. status_t FLEXNVM_EepromGetProtection(flexnvm_config_t *config, uint8_t *protectStatus)
  306. {
  307. if ((config == NULL) || (protectStatus == NULL))
  308. {
  309. return kStatus_FTFx_InvalidArgument;
  310. }
  311. if ((config->ftfxConfig.eepromTotalSize == 0) || (config->ftfxConfig.eepromTotalSize == 0xFFFFU))
  312. {
  313. return kStatus_FTFx_CommandNotSupported;
  314. }
  315. *protectStatus = FTFx->FEPROT;
  316. return kStatus_FTFx_Success;
  317. }
  318. status_t FLEXNVM_GetProperty(flexnvm_config_t *config, flexnvm_property_tag_t whichProperty, uint32_t *value)
  319. {
  320. if ((config == NULL) || (value == NULL))
  321. {
  322. return kStatus_FTFx_InvalidArgument;
  323. }
  324. switch (whichProperty)
  325. {
  326. case kFLEXNVM_PropertyDflashSectorSize:
  327. *value = config->ftfxConfig.flashDesc.sectorSize;
  328. break;
  329. case kFLEXNVM_PropertyDflashTotalSize:
  330. *value = config->ftfxConfig.flashDesc.totalSize;
  331. break;
  332. case kFLEXNVM_PropertyDflashBlockSize:
  333. *value = config->ftfxConfig.flashDesc.totalSize / config->ftfxConfig.flashDesc.blockCount;
  334. break;
  335. case kFLEXNVM_PropertyDflashBlockCount:
  336. *value = config->ftfxConfig.flashDesc.blockCount;
  337. break;
  338. case kFLEXNVM_PropertyDflashBlockBaseAddr:
  339. *value = config->ftfxConfig.flashDesc.blockBase;
  340. break;
  341. case kFLEXNVM_PropertyFlexRamBlockBaseAddr:
  342. *value = config->ftfxConfig.flexramBlockBase;
  343. break;
  344. case kFLEXNVM_PropertyFlexRamTotalSize:
  345. *value = config->ftfxConfig.flexramTotalSize;
  346. break;
  347. case kFLEXNVM_PropertyEepromTotalSize:
  348. *value = config->ftfxConfig.eepromTotalSize;
  349. break;
  350. default: /* catch inputs that are not recognized */
  351. return kStatus_FTFx_UnknownProperty;
  352. }
  353. return kStatus_FTFx_Success;
  354. }
  355. static status_t flexnvm_convert_start_address(flexnvm_config_t *config, uint32_t start)
  356. {
  357. if (config == NULL)
  358. {
  359. return kStatus_FTFx_InvalidArgument;
  360. }
  361. /* From Spec: When required by the command, address bit 23 selects between program flash memory
  362. * (=0) and data flash memory (=1).*/
  363. config->ftfxConfig.opsConfig.convertedAddress = start - config->ftfxConfig.flashDesc.blockBase + 0x800000U;
  364. return kStatus_FTFx_Success;
  365. }
  366. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM */