fsl_ftfx_flash.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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_flash.h"
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. /*!
  42. * @brief Enumeration for special memory property.
  43. */
  44. enum _ftfx_special_mem_property
  45. {
  46. kFTFx_AccessSegmentUnitSize = 256UL,
  47. kFTFx_MinProtectBlockSize = 1024UL,
  48. };
  49. /*!
  50. * @brief Enumeration for the index of read/program once record
  51. */
  52. enum _k3_flash_read_once_index
  53. {
  54. kFLASH_RecordIndexSwapAddr = 0xA1U, /*!< Index of Swap indicator address.*/
  55. kFLASH_RecordIndexSwapEnable = 0xA2U, /*!< Index of Swap system enable.*/
  56. kFLASH_RecordIndexSwapDisable = 0xA3U, /*!< Index of Swap system disable.*/
  57. };
  58. /*******************************************************************************
  59. * Prototypes
  60. ******************************************************************************/
  61. static void flash_init_features(ftfx_config_t *config);
  62. static uint32_t flash_calculate_mem_size(uint32_t pflashBlockCount,
  63. uint32_t pflashBlockSize,
  64. uint32_t pfsizeMask,
  65. uint32_t pfsizeShift);
  66. static uint32_t flash_calculate_prot_segment_size(uint32_t flashSize, uint32_t segmentCount);
  67. static status_t flash_check_range_to_get_index(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint8_t *flashIndex);
  68. /*! @brief Convert address for flash.*/
  69. static status_t flash_convert_start_address(ftfx_config_t *config, uint32_t start);
  70. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  71. /*! @brief Validates the gived address to see if it is equal to swap indicator address in pflash swap IFR.*/
  72. static status_t flash_validate_swap_indicator_address(ftfx_config_t *config, uint32_t address);
  73. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */
  74. /*******************************************************************************
  75. * Variables
  76. ******************************************************************************/
  77. static volatile uint32_t *const kFPROTL = (volatile uint32_t *)&FTFx_FPROT_LOW_REG;
  78. static volatile uint32_t *const kFPROTH = (volatile uint32_t *)&FTFx_FPROT_HIGH_REG;
  79. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  80. volatile uint8_t *const kFPROTSL = (volatile uint8_t *)&FTFx_FPROTSL_REG;
  81. volatile uint8_t *const kFPROTSH = (volatile uint8_t *)&FTFx_FPROTSH_REG;
  82. #endif
  83. /*!
  84. * @brief Table of pflash sizes.
  85. *
  86. * The index into this table is the value of the SIM_FCFG1.PFSIZE bitfield.
  87. *
  88. * The values in this table have been right shifted 10 bits so that they will all fit within
  89. * an 16-bit integer. To get the actual flash density, you must left shift the looked up value
  90. * by 10 bits.
  91. *
  92. * Elements of this table have a value of 0 in cases where the PFSIZE bitfield value is
  93. * reserved.
  94. *
  95. * Code to use the table:
  96. * @code
  97. * uint8_t pfsize = (SIM->FCFG1 & SIM_FCFG1_PFSIZE_MASK) >> SIM_FCFG1_PFSIZE_SHIFT;
  98. * flashDensity = ((uint32_t)kPFlashDensities[pfsize]) << 10;
  99. * @endcode
  100. */
  101. #if defined(FSL_FEATURE_FLASH_SIZE_ENCODING_RULE_VERSION) && (FSL_FEATURE_FLASH_SIZE_ENCODING_RULE_VERSION == 1)
  102. static const uint16_t kPFlashDensities[] = {
  103. 0, /* 0x0 - undefined */
  104. 0, /* 0x1 - undefined */
  105. 0, /* 0x2 - undefined */
  106. 0, /* 0x3 - undefined */
  107. 0, /* 0x4 - undefined */
  108. 0, /* 0x5 - undefined */
  109. 0, /* 0x6 - undefined */
  110. 0, /* 0x7 - undefined */
  111. 0, /* 0x8 - undefined */
  112. 0, /* 0x9 - undefined */
  113. 256, /* 0xa - 262144, 256KB */
  114. 0, /* 0xb - undefined */
  115. 1024, /* 0xc - 1048576, 1MB */
  116. 0, /* 0xd - undefined */
  117. 0, /* 0xe - undefined */
  118. 0, /* 0xf - undefined */
  119. };
  120. #else
  121. static const uint16_t kPFlashDensities[] = {
  122. 8, /* 0x0 - 8192, 8KB */
  123. 16, /* 0x1 - 16384, 16KB */
  124. 24, /* 0x2 - 24576, 24KB */
  125. 32, /* 0x3 - 32768, 32KB */
  126. 48, /* 0x4 - 49152, 48KB */
  127. 64, /* 0x5 - 65536, 64KB */
  128. 96, /* 0x6 - 98304, 96KB */
  129. 128, /* 0x7 - 131072, 128KB */
  130. 192, /* 0x8 - 196608, 192KB */
  131. 256, /* 0x9 - 262144, 256KB */
  132. 384, /* 0xa - 393216, 384KB */
  133. 512, /* 0xb - 524288, 512KB */
  134. 768, /* 0xc - 786432, 768KB */
  135. 1024, /* 0xd - 1048576, 1MB */
  136. 1536, /* 0xe - 1572864, 1.5MB */
  137. /* 2048, 0xf - 2097152, 2MB */
  138. };
  139. #endif
  140. /*******************************************************************************
  141. * Code
  142. ******************************************************************************/
  143. status_t FLASH_Init(flash_config_t *config)
  144. {
  145. status_t returnCode;
  146. if (config == NULL)
  147. {
  148. return kStatus_FTFx_InvalidArgument;
  149. }
  150. for (uint8_t flashIndex = 0; flashIndex < FTFx_FLASH_COUNT; flashIndex++)
  151. {
  152. uint32_t pflashStartAddress;
  153. uint32_t pflashBlockSize;
  154. uint32_t pflashBlockCount;
  155. uint32_t pflashBlockSectorSize;
  156. uint32_t pflashProtectionRegionCount;
  157. uint32_t pflashBlockWriteUnitSize;
  158. uint32_t pflashSectorCmdAlignment;
  159. uint32_t pflashSectionCmdAlignment;
  160. uint32_t pfsizeMask;
  161. uint32_t pfsizeShift;
  162. uint32_t facssValue;
  163. uint32_t facsnValue;
  164. config->ftfxConfig[flashIndex].flashDesc.type = kFTFx_MemTypePflash;
  165. config->ftfxConfig[flashIndex].flashDesc.index = flashIndex;
  166. flash_init_features(&config->ftfxConfig[flashIndex]);
  167. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  168. if(flashIndex == 1)
  169. {
  170. pflashStartAddress = FLASH1_FEATURE_PFLASH_START_ADDRESS;
  171. pflashBlockSize = FLASH1_FEATURE_PFLASH_BLOCK_SIZE;
  172. pflashBlockCount = FLASH1_FEATURE_PFLASH_BLOCK_COUNT;
  173. pflashBlockSectorSize = FLASH1_FEATURE_PFLASH_BLOCK_SECTOR_SIZE;
  174. pflashProtectionRegionCount = FLASH1_FEATURE_PFLASH_PROTECTION_REGION_COUNT;
  175. pflashBlockWriteUnitSize = FLASH1_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE;
  176. pflashSectorCmdAlignment = FLASH1_FEATURE_PFLASH_SECTOR_CMD_ADDRESS_ALIGMENT;
  177. pflashSectionCmdAlignment = FLASH1_FEATURE_PFLASH_SECTION_CMD_ADDRESS_ALIGMENT;
  178. pfsizeMask = SIM_FLASH1_PFSIZE_MASK;
  179. pfsizeShift = SIM_FLASH1_PFSIZE_SHIFT;
  180. facssValue = FTFx_FACSSS_REG;
  181. facsnValue = FTFx_FACSNS_REG;
  182. }
  183. else
  184. #endif
  185. {
  186. pflashStartAddress = FLASH0_FEATURE_PFLASH_START_ADDRESS;
  187. pflashBlockSize = FLASH0_FEATURE_PFLASH_BLOCK_SIZE;
  188. pflashBlockCount = FLASH0_FEATURE_PFLASH_BLOCK_COUNT;
  189. pflashBlockSectorSize = FLASH0_FEATURE_PFLASH_BLOCK_SECTOR_SIZE;
  190. pflashProtectionRegionCount = FLASH0_FEATURE_PFLASH_PROTECTION_REGION_COUNT;
  191. pflashBlockWriteUnitSize = FLASH0_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE;
  192. pflashSectorCmdAlignment = FLASH0_FEATURE_PFLASH_SECTOR_CMD_ADDRESS_ALIGMENT;
  193. pflashSectionCmdAlignment = FLASH0_FEATURE_PFLASH_SECTION_CMD_ADDRESS_ALIGMENT;
  194. pfsizeMask = SIM_FLASH0_PFSIZE_MASK;
  195. pfsizeShift = SIM_FLASH0_PFSIZE_SHIFT;
  196. facssValue = FTFx_FACSS_REG;
  197. facsnValue = FTFx_FACSN_REG;
  198. }
  199. config->ftfxConfig[flashIndex].flashDesc.blockBase = pflashStartAddress;
  200. config->ftfxConfig[flashIndex].flashDesc.blockCount = pflashBlockCount;
  201. config->ftfxConfig[flashIndex].flashDesc.sectorSize = pflashBlockSectorSize;
  202. if (config->ftfxConfig[flashIndex].flashDesc.feature.isIndBlock &&
  203. config->ftfxConfig[flashIndex].flashDesc.feature.hasIndPfsizeReg)
  204. {
  205. config->ftfxConfig[flashIndex].flashDesc.totalSize = flash_calculate_mem_size(pflashBlockCount, pflashBlockSize, pfsizeMask, pfsizeShift);
  206. }
  207. else
  208. {
  209. config->ftfxConfig[flashIndex].flashDesc.totalSize = pflashBlockCount * pflashBlockSize;
  210. }
  211. if (config->ftfxConfig[flashIndex].flashDesc.feature.hasXaccControl)
  212. {
  213. ftfx_spec_mem_t *specMem;
  214. specMem = &config->ftfxConfig[flashIndex].flashDesc.accessSegmentMem;
  215. if (config->ftfxConfig[flashIndex].flashDesc.feature.hasIndXaccReg)
  216. {
  217. specMem->base = config->ftfxConfig[flashIndex].flashDesc.blockBase;
  218. specMem->size = kFTFx_AccessSegmentUnitSize << facssValue;
  219. specMem->count = facsnValue;
  220. }
  221. else
  222. {
  223. specMem->base = config->ftfxConfig[0].flashDesc.blockBase;
  224. specMem->size = kFTFx_AccessSegmentUnitSize << FTFx_FACSS_REG;
  225. specMem->count = FTFx_FACSN_REG;
  226. }
  227. }
  228. if (config->ftfxConfig[flashIndex].flashDesc.feature.hasProtControl)
  229. {
  230. ftfx_spec_mem_t *specMem;
  231. specMem = &config->ftfxConfig[flashIndex].flashDesc.protectRegionMem;
  232. if (config->ftfxConfig[flashIndex].flashDesc.feature.hasIndProtReg)
  233. {
  234. specMem->base = config->ftfxConfig[flashIndex].flashDesc.blockBase;
  235. specMem->count = pflashProtectionRegionCount;
  236. specMem->size = flash_calculate_prot_segment_size(config->ftfxConfig[flashIndex].flashDesc.totalSize, specMem->count);
  237. }
  238. else
  239. {
  240. uint32_t pflashTotalSize = 0;
  241. specMem->base = config->ftfxConfig[0].flashDesc.blockBase;
  242. specMem->count = FLASH0_FEATURE_PFLASH_PROTECTION_REGION_COUNT;
  243. #if (FTFx_FLASH_COUNT != 1)
  244. if (flashIndex == FTFx_FLASH_COUNT - 1)
  245. #endif
  246. {
  247. uint32_t segmentSize;
  248. for (uint32_t i = 0; i < FTFx_FLASH_COUNT; i++)
  249. {
  250. pflashTotalSize += config->ftfxConfig[flashIndex].flashDesc.totalSize;
  251. }
  252. segmentSize = flash_calculate_prot_segment_size(pflashTotalSize, specMem->count);
  253. for (uint32_t i = 0; i < FTFx_FLASH_COUNT; i++)
  254. {
  255. config->ftfxConfig[i].flashDesc.protectRegionMem.size = segmentSize;
  256. }
  257. }
  258. }
  259. }
  260. config->ftfxConfig[flashIndex].opsConfig.addrAligment.blockWriteUnitSize = pflashBlockWriteUnitSize;
  261. config->ftfxConfig[flashIndex].opsConfig.addrAligment.sectorCmd = pflashSectorCmdAlignment;
  262. config->ftfxConfig[flashIndex].opsConfig.addrAligment.sectionCmd = pflashSectionCmdAlignment;
  263. config->ftfxConfig[flashIndex].opsConfig.addrAligment.resourceCmd = FSL_FEATURE_FLASH_PFLASH_RESOURCE_CMD_ADDRESS_ALIGMENT;
  264. config->ftfxConfig[flashIndex].opsConfig.addrAligment.checkCmd = FSL_FEATURE_FLASH_PFLASH_CHECK_CMD_ADDRESS_ALIGMENT;
  265. config->ftfxConfig[flashIndex].opsConfig.addrAligment.swapCtrlCmd = FSL_FEATURE_FLASH_PFLASH_SWAP_CONTROL_CMD_ADDRESS_ALIGMENT;
  266. /* Init FTFx Kernel */
  267. returnCode = FTFx_API_Init(&config->ftfxConfig[flashIndex]);
  268. if (returnCode != kStatus_FTFx_Success)
  269. {
  270. return returnCode;
  271. }
  272. }
  273. return kStatus_FTFx_Success;
  274. }
  275. status_t FLASH_Erase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
  276. {
  277. status_t returnCode;
  278. uint8_t flashIndex;
  279. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  280. if (returnCode != kStatus_FTFx_Success)
  281. {
  282. return returnCode;
  283. }
  284. returnCode = flash_convert_start_address(&config->ftfxConfig[flashIndex], start);
  285. if (returnCode != kStatus_FTFx_Success)
  286. {
  287. return returnCode;
  288. }
  289. return FTFx_CMD_Erase(&config->ftfxConfig[flashIndex], start, lengthInBytes, key);
  290. }
  291. status_t FLASH_EraseAll(flash_config_t *config, uint32_t key)
  292. {
  293. return FTFx_CMD_EraseAll(&config->ftfxConfig[0], key);
  294. }
  295. #if defined(FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD) && FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD
  296. status_t FLASH_EraseAllUnsecure(flash_config_t *config, uint32_t key)
  297. {
  298. return FTFx_CMD_EraseAllUnsecure(&config->ftfxConfig[0], key);
  299. }
  300. #endif
  301. status_t FLASH_Program(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  302. {
  303. status_t returnCode;
  304. uint8_t flashIndex;
  305. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  306. if (returnCode != kStatus_FTFx_Success)
  307. {
  308. return returnCode;
  309. }
  310. returnCode = flash_convert_start_address(&config->ftfxConfig[flashIndex], start);
  311. if (returnCode != kStatus_FTFx_Success)
  312. {
  313. return returnCode;
  314. }
  315. return FTFx_CMD_Program(&config->ftfxConfig[flashIndex], start, src, lengthInBytes);
  316. }
  317. #if defined(FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD) && FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD
  318. status_t FLASH_ProgramSection(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  319. {
  320. status_t returnCode;
  321. uint8_t flashIndex;
  322. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  323. if (returnCode != kStatus_FTFx_Success)
  324. {
  325. return returnCode;
  326. }
  327. returnCode = flash_convert_start_address(&config->ftfxConfig[flashIndex], start);
  328. if (returnCode != kStatus_FTFx_Success)
  329. {
  330. return returnCode;
  331. }
  332. return FTFx_CMD_ProgramSection(&config->ftfxConfig[flashIndex], start, src, lengthInBytes);
  333. }
  334. #endif
  335. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  336. status_t FLASH_ReadResource(flash_config_t *config,
  337. uint32_t start,
  338. uint8_t *dst,
  339. uint32_t lengthInBytes,
  340. ftfx_read_resource_opt_t option)
  341. {
  342. return FTFx_CMD_ReadResource(&config->ftfxConfig[0], start, dst, lengthInBytes, option);
  343. }
  344. #endif
  345. status_t FLASH_VerifyErase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, ftfx_margin_value_t margin)
  346. {
  347. status_t returnCode;
  348. uint8_t flashIndex;
  349. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  350. if (returnCode != kStatus_FTFx_Success)
  351. {
  352. return returnCode;
  353. }
  354. returnCode = flash_convert_start_address(&config->ftfxConfig[flashIndex], start);
  355. if (returnCode != kStatus_FTFx_Success)
  356. {
  357. return returnCode;
  358. }
  359. return FTFx_CMD_VerifyErase(&config->ftfxConfig[flashIndex], start, lengthInBytes, margin);
  360. }
  361. status_t FLASH_VerifyEraseAll(flash_config_t *config, ftfx_margin_value_t margin)
  362. {
  363. return FTFx_CMD_VerifyEraseAll(&config->ftfxConfig[0], margin);
  364. }
  365. status_t FLASH_VerifyProgram(flash_config_t *config,
  366. uint32_t start,
  367. uint32_t lengthInBytes,
  368. const uint8_t *expectedData,
  369. ftfx_margin_value_t margin,
  370. uint32_t *failedAddress,
  371. uint32_t *failedData)
  372. {
  373. status_t returnCode;
  374. uint8_t flashIndex;
  375. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  376. if (returnCode != kStatus_FTFx_Success)
  377. {
  378. return returnCode;
  379. }
  380. returnCode = flash_convert_start_address(&config->ftfxConfig[flashIndex], start);
  381. if (returnCode != kStatus_FTFx_Success)
  382. {
  383. return returnCode;
  384. }
  385. return FTFx_CMD_VerifyProgram(&config->ftfxConfig[flashIndex], start, lengthInBytes, expectedData, margin, failedAddress, failedData);
  386. }
  387. status_t FLASH_GetSecurityState(flash_config_t *config, ftfx_security_state_t *state)
  388. {
  389. return FTFx_REG_GetSecurityState(&config->ftfxConfig[0], state);
  390. }
  391. status_t FLASH_SecurityBypass(flash_config_t *config, const uint8_t *backdoorKey)
  392. {
  393. return FTFx_CMD_SecurityBypass(&config->ftfxConfig[0], backdoorKey);
  394. }
  395. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  396. status_t FLASH_SetFlexramFunction(flash_config_t *config, ftfx_flexram_func_opt_t option)
  397. {
  398. return FTFx_CMD_SetFlexramFunction(&config->ftfxConfig[0], option);
  399. }
  400. #endif
  401. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  402. status_t FLASH_Swap(flash_config_t *config, uint32_t address, bool isSetEnable)
  403. {
  404. status_t returnCode;
  405. ftfx_swap_state_config_t returnInfo;
  406. ftfx_config_t *ftfxConfig;
  407. uint8_t flashIndex;
  408. returnCode = flash_check_range_to_get_index(config, address, 1, &flashIndex);
  409. if (returnCode != kStatus_FTFx_Success)
  410. {
  411. return returnCode;
  412. }
  413. ftfxConfig = &config->ftfxConfig[flashIndex];
  414. memset(&returnInfo, 0xFFU, sizeof(returnInfo));
  415. do
  416. {
  417. returnCode = FTFx_CMD_SwapControl(ftfxConfig, address, kFTFx_SwapControlOptionReportStatus, &returnInfo);
  418. if (returnCode != kStatus_FTFx_Success)
  419. {
  420. return returnCode;
  421. }
  422. if (!isSetEnable)
  423. {
  424. if (returnInfo.flashSwapState == kFTFx_SwapStateDisabled)
  425. {
  426. return kStatus_FTFx_Success;
  427. }
  428. else if (returnInfo.flashSwapState == kFTFx_SwapStateUninitialized)
  429. {
  430. /* The swap system changed to the DISABLED state with Program flash block 0
  431. * located at relative flash address 0x0_0000 */
  432. returnCode = FTFx_CMD_SwapControl(ftfxConfig, address, kFTFx_SwapControlOptionDisableSystem, &returnInfo);
  433. }
  434. else
  435. {
  436. /* Swap disable should be requested only when swap system is in the uninitialized state */
  437. return kStatus_FTFx_SwapSystemNotInUninitialized;
  438. }
  439. }
  440. else
  441. {
  442. /* When first swap: the initial swap state is Uninitialized, flash swap inidicator address is unset,
  443. * the swap procedure should be Uninitialized -> Update-Erased -> Complete.
  444. * After the first swap has been completed, the flash swap inidicator address cannot be modified
  445. * unless EraseAllBlocks command is issued, the swap procedure is changed to Update -> Update-Erased ->
  446. * Complete. */
  447. switch (returnInfo.flashSwapState)
  448. {
  449. case kFTFx_SwapStateUninitialized:
  450. /* If current swap mode is Uninitialized, Initialize Swap to Initialized/READY state. */
  451. returnCode =
  452. FTFx_CMD_SwapControl(ftfxConfig, address, kFTFx_SwapControlOptionIntializeSystem, &returnInfo);
  453. break;
  454. case kFTFx_SwapStateReady:
  455. /* Validate whether the address provided to the swap system is matched to
  456. * swap indicator address in the IFR */
  457. returnCode = flash_validate_swap_indicator_address(ftfxConfig, address);
  458. if (returnCode == kStatus_FTFx_Success)
  459. {
  460. /* If current swap mode is Initialized/Ready, Initialize Swap to UPDATE state. */
  461. returnCode =
  462. FTFx_CMD_SwapControl(ftfxConfig, address, kFTFx_SwapControlOptionSetInUpdateState, &returnInfo);
  463. }
  464. break;
  465. case kFTFx_SwapStateUpdate:
  466. /* If current swap mode is Update, Erase indicator sector in non active block
  467. * to proceed swap system to update-erased state */
  468. returnCode = FLASH_Erase(config, address + (ftfxConfig->flashDesc.totalSize >> 1),
  469. ftfxConfig->opsConfig.addrAligment.sectorCmd, kFTFx_ApiEraseKey);
  470. break;
  471. case kFTFx_SwapStateUpdateErased:
  472. /* If current swap mode is Update or Update-Erased, progress Swap to COMPLETE State */
  473. returnCode =
  474. FTFx_CMD_SwapControl(ftfxConfig, address, kFTFx_SwapControlOptionSetInCompleteState, &returnInfo);
  475. break;
  476. case kFTFx_SwapStateComplete:
  477. break;
  478. case kFTFx_SwapStateDisabled:
  479. /* When swap system is in disabled state, We need to clear swap system back to uninitialized
  480. * by issuing EraseAllBlocks command */
  481. returnCode = kStatus_FTFx_SwapSystemNotInUninitialized;
  482. break;
  483. default:
  484. returnCode = kStatus_FTFx_InvalidArgument;
  485. break;
  486. }
  487. }
  488. if (returnCode != kStatus_FTFx_Success)
  489. {
  490. break;
  491. }
  492. } while (!((kFTFx_SwapStateComplete == returnInfo.flashSwapState) && isSetEnable));
  493. return returnCode;
  494. }
  495. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */
  496. status_t FLASH_IsProtected(flash_config_t *config,
  497. uint32_t start,
  498. uint32_t lengthInBytes,
  499. flash_prot_state_t *protection_state)
  500. {
  501. status_t returnCode;
  502. ftfx_config_t *ftfxConfig;
  503. uint8_t flashIndex;
  504. if (protection_state == NULL)
  505. {
  506. return kStatus_FTFx_InvalidArgument;
  507. }
  508. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  509. if (returnCode != kStatus_FTFx_Success)
  510. {
  511. return returnCode;
  512. }
  513. ftfxConfig = &config->ftfxConfig[flashIndex];
  514. if (ftfxConfig->flashDesc.feature.hasProtControl)
  515. {
  516. uint32_t endAddress; /* end address for protection check */
  517. uint32_t regionCheckedCounter; /* increments each time the flash address was checked for
  518. * protection status */
  519. uint32_t regionCounter; /* incrementing variable used to increment through the flash
  520. * protection regions */
  521. uint32_t protectStatusCounter; /* increments each time a flash region was detected as protected */
  522. uint8_t flashRegionProtectStatus[MAX_FLASH_PROT_REGION_COUNT]; /* array of the protection
  523. * status for each
  524. * protection region */
  525. uint32_t flashRegionAddress[MAX_FLASH_PROT_REGION_COUNT + 1]; /* array of the start addresses for each flash
  526. * protection region. Note this is REGION_COUNT+1
  527. * due to requiring the next start address after
  528. * the end of flash for loop-check purposes below */
  529. bool isBreakNeeded = false;
  530. /* calculating Flash end address */
  531. endAddress = start + lengthInBytes;
  532. /* populate the flashRegionAddress array with the start address of each flash region */
  533. regionCounter = 0; /* make sure regionCounter is initialized to 0 first */
  534. /* populate up to 33rd element of array, this is the next address after end of flash array */
  535. while (regionCounter <= ftfxConfig->flashDesc.protectRegionMem.count)
  536. {
  537. flashRegionAddress[regionCounter] =
  538. ftfxConfig->flashDesc.protectRegionMem.base + ftfxConfig->flashDesc.protectRegionMem.size * regionCounter;
  539. regionCounter++;
  540. }
  541. /* populate flashRegionProtectStatus array with status information
  542. * Protection status for each region is stored in the FPROT[3:0] registers
  543. * Each bit represents one region of flash
  544. * 4 registers * 8-bits-per-register = 32-bits (32-regions)
  545. * The convention is:
  546. * FPROT3[bit 0] is the first protection region (start of flash memory)
  547. * FPROT0[bit 7] is the last protection region (end of flash memory)
  548. * regionCounter is used to determine which FPROT[3:0] register to check for protection status
  549. * Note: FPROT=1 means NOT protected, FPROT=0 means protected */
  550. regionCounter = 0; /* make sure regionCounter is initialized to 0 first */
  551. while (regionCounter < ftfxConfig->flashDesc.protectRegionMem.count)
  552. {
  553. if ((ftfxConfig->flashDesc.index == 0) || (!ftfxConfig->flashDesc.feature.hasIndProtReg))
  554. {
  555. /* Note: So far protection region count may be 16/20/24/32/64 */
  556. if (regionCounter < 8)
  557. {
  558. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL3_REG >> regionCounter) & (0x01u);
  559. }
  560. else if (regionCounter < 16)
  561. {
  562. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL2_REG >> (regionCounter - 8)) & (0x01u);
  563. }
  564. #if defined(MAX_FLASH_PROT_REGION_COUNT) && (MAX_FLASH_PROT_REGION_COUNT > 16)
  565. #if (MAX_FLASH_PROT_REGION_COUNT == 20)
  566. else if (regionCounter < 20)
  567. {
  568. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL1_REG >> (regionCounter - 16)) & (0x01u);
  569. }
  570. #else
  571. else if (regionCounter < 24)
  572. {
  573. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL1_REG >> (regionCounter - 16)) & (0x01u);
  574. }
  575. #endif /*(MAX_FLASH_PROT_REGION_COUNT == 20)*/
  576. #endif
  577. #if defined(MAX_FLASH_PROT_REGION_COUNT) && (MAX_FLASH_PROT_REGION_COUNT > 24)
  578. else if (regionCounter < 32)
  579. {
  580. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL0_REG >> (regionCounter - 24)) & (0x01u);
  581. }
  582. #endif
  583. #if defined(MAX_FLASH_PROT_REGION_COUNT) && (MAX_FLASH_PROT_REGION_COUNT == 64)
  584. else if (regionCounter < 40)
  585. {
  586. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH3_REG >> (regionCounter - 32)) & (0x01u);
  587. }
  588. else if (regionCounter < 48)
  589. {
  590. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH2_REG >> (regionCounter - 40)) & (0x01u);
  591. }
  592. else if (regionCounter < 56)
  593. {
  594. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH1_REG >> (regionCounter - 48)) & (0x01u);
  595. }
  596. else if (regionCounter < 64)
  597. {
  598. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH0_REG >> (regionCounter - 56)) & (0x01u);
  599. }
  600. #endif
  601. else
  602. {
  603. isBreakNeeded = true;
  604. }
  605. regionCounter++;
  606. }
  607. else if ((ftfxConfig->flashDesc.index == 1) && ftfxConfig->flashDesc.feature.hasIndProtReg)
  608. {
  609. /* Note: So far protection region count may be 8/16 */
  610. if (regionCounter < 8)
  611. {
  612. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTSL_REG >> regionCounter) & (0x01u);
  613. }
  614. else if (regionCounter < 16)
  615. {
  616. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTSH_REG >> (regionCounter - 8)) & (0x01u);
  617. }
  618. else
  619. {
  620. isBreakNeeded = true;
  621. }
  622. regionCounter++;
  623. }
  624. else
  625. {}
  626. if (isBreakNeeded)
  627. {
  628. break;
  629. }
  630. }
  631. /* loop through the flash regions and check
  632. * desired flash address range for protection status
  633. * loop stops when it is detected that start has exceeded the endAddress */
  634. regionCounter = 0; /* make sure regionCounter is initialized to 0 first */
  635. regionCheckedCounter = 0;
  636. protectStatusCounter = 0; /* make sure protectStatusCounter is initialized to 0 first */
  637. while (start < endAddress)
  638. {
  639. /* check to see if the address falls within this protection region
  640. * Note that if the entire flash is to be checked, the last protection
  641. * region checked would consist of the last protection start address and
  642. * the start address following the end of flash */
  643. if ((start >= flashRegionAddress[regionCounter]) && (start < flashRegionAddress[regionCounter + 1]))
  644. {
  645. /* increment regionCheckedCounter to indicate this region was checked */
  646. regionCheckedCounter++;
  647. /* check the protection status of this region
  648. * Note: FPROT=1 means NOT protected, FPROT=0 means protected */
  649. if (!flashRegionProtectStatus[regionCounter])
  650. {
  651. /* increment protectStatusCounter to indicate this region is protected */
  652. protectStatusCounter++;
  653. }
  654. start += ftfxConfig->flashDesc.protectRegionMem.size; /* increment to an address within the next region */
  655. }
  656. regionCounter++; /* increment regionCounter to check for the next flash protection region */
  657. }
  658. /* if protectStatusCounter == 0, then no region of the desired flash region is protected */
  659. if (protectStatusCounter == 0)
  660. {
  661. *protection_state = kFLASH_ProtectionStateUnprotected;
  662. }
  663. /* if protectStatusCounter == regionCheckedCounter, then each region checked was protected */
  664. else if (protectStatusCounter == regionCheckedCounter)
  665. {
  666. *protection_state = kFLASH_ProtectionStateProtected;
  667. }
  668. /* if protectStatusCounter != regionCheckedCounter, then protection status is mixed
  669. * In other words, some regions are protected while others are unprotected */
  670. else
  671. {
  672. *protection_state = kFLASH_ProtectionStateMixed;
  673. }
  674. }
  675. else
  676. {
  677. *protection_state = kFLASH_ProtectionStateUnprotected;
  678. }
  679. return kStatus_FTFx_Success;
  680. }
  681. status_t FLASH_IsExecuteOnly(flash_config_t *config,
  682. uint32_t start,
  683. uint32_t lengthInBytes,
  684. flash_xacc_state_t *access_state)
  685. {
  686. status_t returnCode;
  687. ftfx_config_t *ftfxConfig;
  688. uint8_t flashIndex;
  689. if (access_state == NULL)
  690. {
  691. return kStatus_FTFx_InvalidArgument;
  692. }
  693. returnCode = flash_check_range_to_get_index(config, start, lengthInBytes, &flashIndex);
  694. if (returnCode != kStatus_FTFx_Success)
  695. {
  696. return returnCode;
  697. }
  698. ftfxConfig = &config->ftfxConfig[flashIndex];
  699. if (ftfxConfig->flashDesc.feature.hasXaccControl)
  700. {
  701. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL) && FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL
  702. uint32_t executeOnlySegmentCounter = 0;
  703. /* calculating end address */
  704. uint32_t endAddress = start + lengthInBytes;
  705. /* Aligning start address and end address */
  706. uint32_t alignedStartAddress = ALIGN_DOWN(start, ftfxConfig->flashDesc.accessSegmentMem.size);
  707. uint32_t alignedEndAddress = ALIGN_UP(endAddress, ftfxConfig->flashDesc.accessSegmentMem.size);
  708. uint32_t segmentIndex = 0;
  709. uint32_t maxSupportedExecuteOnlySegmentCount =
  710. (alignedEndAddress - alignedStartAddress) / ftfxConfig->flashDesc.accessSegmentMem.size;
  711. while (start < endAddress)
  712. {
  713. uint32_t xacc = 0;
  714. bool isInvalidSegmentIndex = false;
  715. segmentIndex = (start - ftfxConfig->flashDesc.accessSegmentMem.base) / ftfxConfig->flashDesc.accessSegmentMem.size;
  716. if ((ftfxConfig->flashDesc.index == 0) || (!ftfxConfig->flashDesc.feature.hasIndXaccReg))
  717. {
  718. /* For primary flash, The eight XACC registers allow up to 64 restricted segments of equal memory size.
  719. */
  720. if (segmentIndex < 32)
  721. {
  722. xacc = *(const volatile uint32_t *)&FTFx_XACCL3_REG;
  723. }
  724. else if (segmentIndex < ftfxConfig->flashDesc.accessSegmentMem.count)
  725. {
  726. xacc = *(const volatile uint32_t *)&FTFx_XACCH3_REG;
  727. segmentIndex -= 32;
  728. }
  729. else
  730. {
  731. isInvalidSegmentIndex = true;
  732. }
  733. }
  734. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  735. else if ((ftfxConfig->flashDesc.index == 1) && ftfxConfig->flashDesc.feature.hasIndXaccReg)
  736. {
  737. /* For secondary flash, The two XACCS registers allow up to 16 restricted segments of equal memory size.
  738. */
  739. if (segmentIndex < 8)
  740. {
  741. xacc = *(const volatile uint8_t *)&FTFx_XACCSL_REG;
  742. }
  743. else if (segmentIndex < ftfxConfig->flashDesc.accessSegmentMem.count)
  744. {
  745. xacc = *(const volatile uint8_t *)&FTFx_XACCSH_REG;
  746. segmentIndex -= 8;
  747. }
  748. else
  749. {
  750. isInvalidSegmentIndex = true;
  751. }
  752. }
  753. #endif
  754. else
  755. {}
  756. if (isInvalidSegmentIndex)
  757. {
  758. break;
  759. }
  760. /* Determine if this address range is in a execute-only protection flash segment. */
  761. if ((~xacc) & (1u << segmentIndex))
  762. {
  763. executeOnlySegmentCounter++;
  764. }
  765. start += ftfxConfig->flashDesc.accessSegmentMem.size;
  766. }
  767. if (executeOnlySegmentCounter < 1u)
  768. {
  769. *access_state = kFLASH_AccessStateUnLimited;
  770. }
  771. else if (executeOnlySegmentCounter < maxSupportedExecuteOnlySegmentCount)
  772. {
  773. *access_state = kFLASH_AccessStateMixed;
  774. }
  775. else
  776. {
  777. *access_state = kFLASH_AccessStateExecuteOnly;
  778. }
  779. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */
  780. }
  781. else
  782. {
  783. *access_state = kFLASH_AccessStateUnLimited;
  784. }
  785. return kStatus_FTFx_Success;
  786. }
  787. status_t FLASH_PflashSetProtection(flash_config_t *config, pflash_prot_status_t *protectStatus)
  788. {
  789. if (config == NULL)
  790. {
  791. return kStatus_FTFx_InvalidArgument;
  792. }
  793. if (config->ftfxConfig[0].flashDesc.feature.hasProtControl)
  794. {
  795. if (config->ftfxConfig[0].flashDesc.feature.ProtRegBits >= 32)
  796. {
  797. *kFPROTL = protectStatus->protl;
  798. if (protectStatus->protl != *kFPROTL)
  799. {
  800. return kStatus_FTFx_CommandFailure;
  801. }
  802. }
  803. if (config->ftfxConfig[0].flashDesc.feature.ProtRegBits == 64)
  804. {
  805. *kFPROTH = protectStatus->proth;
  806. if (protectStatus->proth != *kFPROTH)
  807. {
  808. return kStatus_FTFx_CommandFailure;
  809. }
  810. }
  811. }
  812. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  813. else if (config->ftfxConfig[1].flashDesc.feature.hasProtControl && \
  814. config->ftfxConfig[1].flashDesc.feature.hasIndProtReg)
  815. {
  816. if (config->ftfxConfig[1].flashDesc.feature.ProtRegBits == 16)
  817. {
  818. *kFPROTSL = protectStatus->protsl;
  819. if (protectStatus->protsl != *kFPROTSL)
  820. {
  821. return kStatus_FTFx_CommandFailure;
  822. }
  823. *kFPROTSH = protectStatus->protsh;
  824. if (protectStatus->protsh != *kFPROTSH)
  825. {
  826. return kStatus_FTFx_CommandFailure;
  827. }
  828. }
  829. }
  830. #endif
  831. return kStatus_FTFx_Success;
  832. }
  833. status_t FLASH_PflashGetProtection(flash_config_t *config, pflash_prot_status_t *protectStatus)
  834. {
  835. if ((config == NULL) || (protectStatus == NULL))
  836. {
  837. return kStatus_FTFx_InvalidArgument;
  838. }
  839. if (config->ftfxConfig[0].flashDesc.feature.hasProtControl)
  840. {
  841. if (config->ftfxConfig[0].flashDesc.feature.ProtRegBits >= 32)
  842. {
  843. protectStatus->protl = *kFPROTL;
  844. }
  845. if (config->ftfxConfig[0].flashDesc.feature.ProtRegBits == 64)
  846. {
  847. protectStatus->proth = *kFPROTH;
  848. }
  849. }
  850. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  851. else if (config->ftfxConfig[1].flashDesc.feature.hasProtControl && \
  852. config->ftfxConfig[1].flashDesc.feature.hasIndProtReg)
  853. {
  854. if (config->ftfxConfig[0].flashDesc.feature.ProtRegBits == 16)
  855. {
  856. protectStatus->protsl = *kFPROTSL;
  857. protectStatus->protsh = *kFPROTSH;
  858. }
  859. }
  860. #endif
  861. return kStatus_FTFx_Success;
  862. }
  863. status_t FLASH_GetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value)
  864. {
  865. if ((config == NULL) || (value == NULL))
  866. {
  867. return kStatus_FTFx_InvalidArgument;
  868. }
  869. switch (whichProperty)
  870. {
  871. case kFLASH_PropertyPflash0SectorSize:
  872. *value = config->ftfxConfig[0].flashDesc.sectorSize;
  873. break;
  874. case kFLASH_PropertyPflash0TotalSize:
  875. *value = config->ftfxConfig[0].flashDesc.totalSize;
  876. break;
  877. case kFLASH_PropertyPflash0BlockSize:
  878. *value = config->ftfxConfig[0].flashDesc.totalSize / config->ftfxConfig[0].flashDesc.blockCount;
  879. break;
  880. case kFLASH_PropertyPflash0BlockCount:
  881. *value = config->ftfxConfig[0].flashDesc.blockCount;
  882. break;
  883. case kFLASH_PropertyPflash0BlockBaseAddr:
  884. *value = config->ftfxConfig[0].flashDesc.blockBase;
  885. break;
  886. case kFLASH_PropertyPflash0FacSupport:
  887. *value = (uint32_t)config->ftfxConfig[0].flashDesc.feature.hasXaccControl;
  888. break;
  889. case kFLASH_PropertyPflash0AccessSegmentSize:
  890. *value = config->ftfxConfig[0].flashDesc.accessSegmentMem.size;
  891. break;
  892. case kFLASH_PropertyPflash0AccessSegmentCount:
  893. *value = config->ftfxConfig[0].flashDesc.accessSegmentMem.count;
  894. break;
  895. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  896. case kFLASH_PropertyPflash1SectorSize:
  897. *value = config->ftfxConfig[1].flashDesc.sectorSize;
  898. break;
  899. case kFLASH_PropertyPflash1TotalSize:
  900. *value = config->ftfxConfig[1].flashDesc.totalSize;
  901. break;
  902. case kFLASH_PropertyPflash1BlockSize:
  903. *value = config->ftfxConfig[1].flashDesc.totalSize / config->ftfxConfig[1].flashDesc.blockCount;
  904. break;
  905. case kFLASH_PropertyPflash1BlockCount:
  906. *value = config->ftfxConfig[1].flashDesc.blockCount;
  907. break;
  908. case kFLASH_PropertyPflash1BlockBaseAddr:
  909. *value = config->ftfxConfig[1].flashDesc.blockBase;
  910. break;
  911. case kFLASH_PropertyPflash1FacSupport:
  912. *value = (uint32_t)config->ftfxConfig[1].flashDesc.feature.hasXaccControl;
  913. break;
  914. case kFLASH_PropertyPflash1AccessSegmentSize:
  915. *value = config->ftfxConfig[1].flashDesc.accessSegmentMem.size;
  916. break;
  917. case kFLASH_PropertyPflash1AccessSegmentCount:
  918. *value = config->ftfxConfig[1].flashDesc.accessSegmentMem.count;
  919. break;
  920. #endif
  921. case kFLASH_PropertyFlexRamBlockBaseAddr:
  922. *value = config->ftfxConfig[0].flexramBlockBase;
  923. break;
  924. case kFLASH_PropertyFlexRamTotalSize:
  925. *value = config->ftfxConfig[0].flexramTotalSize;
  926. break;
  927. default: /* catch inputs that are not recognized */
  928. return kStatus_FTFx_UnknownProperty;
  929. }
  930. return kStatus_FTFx_Success;
  931. }
  932. static void flash_init_features(ftfx_config_t *config)
  933. {
  934. if (config->flashDesc.index == 0)
  935. {
  936. config->flashDesc.feature.isIndBlock = 1;
  937. config->flashDesc.feature.hasIndPfsizeReg = 1;
  938. config->flashDesc.feature.hasIndProtReg = 1;
  939. config->flashDesc.feature.hasIndXaccReg = 1;
  940. }
  941. #if FTFx_DRIVER_HAS_FLASH1_SUPPORT
  942. else if (config->flashDesc.index == 1)
  943. {
  944. config->flashDesc.feature.isIndBlock = FTFx_FLASH1_IS_INDEPENDENT_BLOCK;
  945. config->flashDesc.feature.hasIndPfsizeReg = config->flashDesc.feature.isIndBlock;
  946. config->flashDesc.feature.hasIndProtReg = FTFx_FLASH1_HAS_INT_PROT_REG;
  947. config->flashDesc.feature.hasIndXaccReg = FTFx_FLASH1_HAS_INT_XACC_REG;
  948. }
  949. #endif
  950. config->flashDesc.feature.hasProtControl = 1;
  951. config->flashDesc.feature.hasXaccControl = FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL;
  952. }
  953. static uint32_t flash_calculate_mem_size(uint32_t pflashBlockCount,
  954. uint32_t pflashBlockSize,
  955. uint32_t pfsizeMask,
  956. uint32_t pfsizeShift)
  957. {
  958. uint8_t pfsize;
  959. uint32_t flashDensity;
  960. /* PFSIZE=0xf means that on customer parts the IFR was not correctly programmed.
  961. * We just use the pre-defined flash size in feature file here to support pre-production parts */
  962. pfsize = (SIM_FCFG1_REG & pfsizeMask) >> pfsizeShift;
  963. if (pfsize == 0xf)
  964. {
  965. flashDensity = pflashBlockCount * pflashBlockSize;
  966. }
  967. else
  968. {
  969. flashDensity = ((uint32_t)kPFlashDensities[pfsize]) << 10;
  970. }
  971. return flashDensity;
  972. }
  973. static uint32_t flash_calculate_prot_segment_size(uint32_t flashSize, uint32_t segmentCount)
  974. {
  975. uint32_t segmentSize;
  976. /* Calculate the size of the flash protection region
  977. * If the flash density is > 32KB, then protection region is 1/32 of total flash density
  978. * Else if flash density is < 32KB, then flash protection region is set to 1KB */
  979. if (flashSize > segmentCount * kFTFx_MinProtectBlockSize)
  980. {
  981. segmentSize = flashSize / segmentCount;
  982. }
  983. else
  984. {
  985. segmentSize = kFTFx_MinProtectBlockSize;
  986. }
  987. return segmentSize;
  988. }
  989. static status_t flash_check_range_to_get_index(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint8_t *flashIndex)
  990. {
  991. if (config == NULL)
  992. {
  993. return kStatus_FTFx_InvalidArgument;
  994. }
  995. /* Validates the range of the given address */
  996. for (uint8_t index = 0; index < FTFx_FLASH_COUNT; index++)
  997. {
  998. if ((start >= config->ftfxConfig[index].flashDesc.blockBase) &&
  999. ((start + lengthInBytes) <= (config->ftfxConfig[index].flashDesc.blockBase + config->ftfxConfig[index].flashDesc.totalSize)))
  1000. {
  1001. *flashIndex = config->ftfxConfig[index].flashDesc.index;
  1002. return kStatus_FTFx_Success;
  1003. }
  1004. }
  1005. return kStatus_FTFx_AddressError;
  1006. }
  1007. static status_t flash_convert_start_address(ftfx_config_t *config, uint32_t start)
  1008. {
  1009. if (config == NULL)
  1010. {
  1011. return kStatus_FTFx_InvalidArgument;
  1012. }
  1013. if (config->flashDesc.index && config->flashDesc.feature.isIndBlock)
  1014. {
  1015. /* When required by the command, address bit 23 selects between main flash memory
  1016. * (=0) and secondary flash memory (=1).*/
  1017. config->opsConfig.convertedAddress = start - config->flashDesc.blockBase + 0x800000U;
  1018. }
  1019. else
  1020. {
  1021. config->opsConfig.convertedAddress = start;
  1022. }
  1023. return kStatus_FTFx_Success;
  1024. }
  1025. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  1026. /*! @brief Validates the gived address to see if it is equal to swap indicator address in pflash swap IFR.*/
  1027. static status_t flash_validate_swap_indicator_address(ftfx_config_t *config, uint32_t address)
  1028. {
  1029. status_t returnCode;
  1030. struct _flash_swap_ifr_field_config
  1031. {
  1032. uint16_t swapIndicatorAddress; /*!< A Swap indicator address field.*/
  1033. uint16_t swapEnableWord; /*!< A Swap enable word field.*/
  1034. uint8_t reserved0[4]; /*!< A reserved field.*/
  1035. uint8_t reserved1[2]; /*!< A reserved field.*/
  1036. uint16_t swapDisableWord; /*!< A Swap disable word field.*/
  1037. uint8_t reserved2[4]; /*!< A reserved field.*/
  1038. } flashSwapIfrFieldData;
  1039. uint32_t swapIndicatorAddress;
  1040. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  1041. returnCode =
  1042. FTFx_CMD_ReadResource(config, config->ifrDesc.resRange.pflashSwapIfrStart, (uint8_t *)&flashSwapIfrFieldData,
  1043. sizeof(flashSwapIfrFieldData), kFTFx_ResourceOptionFlashIfr);
  1044. if (returnCode != kStatus_FTFx_Success)
  1045. {
  1046. return returnCode;
  1047. }
  1048. #else
  1049. {
  1050. /* From RM, the actual info are stored in FCCOB6,7 */
  1051. uint32_t returnValue[2];
  1052. returnCode = FTFx_CMD_ReadOnce(config, kFLASH_RecordIndexSwapAddr, (uint8_t *)returnValue, 4);
  1053. if (returnCode != kStatus_FTFx_Success)
  1054. {
  1055. return returnCode;
  1056. }
  1057. flashSwapIfrFieldData.swapIndicatorAddress = (uint16_t)returnValue[0];
  1058. returnCode = FTFx_CMD_ReadOnce(config, kFLASH_RecordIndexSwapEnable, (uint8_t *)returnValue, 4);
  1059. if (returnCode != kStatus_FTFx_Success)
  1060. {
  1061. return returnCode;
  1062. }
  1063. flashSwapIfrFieldData.swapEnableWord = (uint16_t)returnValue[0];
  1064. returnCode = FTFx_CMD_ReadOnce(config, kFLASH_RecordIndexSwapDisable, (uint8_t *)returnValue, 4);
  1065. if (returnCode != kStatus_FTFx_Success)
  1066. {
  1067. return returnCode;
  1068. }
  1069. flashSwapIfrFieldData.swapDisableWord = (uint16_t)returnValue[0];
  1070. }
  1071. #endif
  1072. /* The high bits value of Swap Indicator Address is stored in Program Flash Swap IFR Field,
  1073. * the low severval bit value of Swap Indicator Address is always 1'b0 */
  1074. swapIndicatorAddress = (uint32_t)flashSwapIfrFieldData.swapIndicatorAddress *
  1075. config->opsConfig.addrAligment.swapCtrlCmd;
  1076. if (address != swapIndicatorAddress)
  1077. {
  1078. return kStatus_FTFx_SwapIndicatorAddressError;
  1079. }
  1080. return returnCode;
  1081. }
  1082. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */