fsl_flash.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * o Redistributions of source code must retain the above copyright notice, this list
  10. * of conditions and the following disclaimer.
  11. *
  12. * o Redistributions in binary form must reproduce the above copyright notice, this
  13. * list of conditions and the following disclaimer in the documentation and/or
  14. * other materials provided with the distribution.
  15. *
  16. * o Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  24. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "fsl_flash.h"
  32. /*******************************************************************************
  33. * Definitions
  34. ******************************************************************************/
  35. /*!
  36. * @name Misc utility defines
  37. * @{
  38. */
  39. /*! @brief Alignment utility. */
  40. #ifndef ALIGN_DOWN
  41. #define ALIGN_DOWN(x, a) ((x) & (uint32_t)(-((int32_t)(a))))
  42. #endif
  43. #ifndef ALIGN_UP
  44. #define ALIGN_UP(x, a) (-((int32_t)((uint32_t)(-((int32_t)(x))) & (uint32_t)(-((int32_t)(a))))))
  45. #endif
  46. /*! @brief Join bytes to word utility. */
  47. #define B1P4(b) (((uint32_t)(b)&0xFFU) << 24)
  48. #define B1P3(b) (((uint32_t)(b)&0xFFU) << 16)
  49. #define B1P2(b) (((uint32_t)(b)&0xFFU) << 8)
  50. #define B1P1(b) ((uint32_t)(b)&0xFFU)
  51. #define B2P3(b) (((uint32_t)(b)&0xFFFFU) << 16)
  52. #define B2P2(b) (((uint32_t)(b)&0xFFFFU) << 8)
  53. #define B2P1(b) ((uint32_t)(b)&0xFFFFU)
  54. #define B3P2(b) (((uint32_t)(b)&0xFFFFFFU) << 8)
  55. #define B3P1(b) ((uint32_t)(b)&0xFFFFFFU)
  56. #define BYTES_JOIN_TO_WORD_1_3(x, y) (B1P4(x) | B3P1(y))
  57. #define BYTES_JOIN_TO_WORD_2_2(x, y) (B2P3(x) | B2P1(y))
  58. #define BYTES_JOIN_TO_WORD_3_1(x, y) (B3P2(x) | B1P1(y))
  59. #define BYTES_JOIN_TO_WORD_1_1_2(x, y, z) (B1P4(x) | B1P3(y) | B2P1(z))
  60. #define BYTES_JOIN_TO_WORD_1_2_1(x, y, z) (B1P4(x) | B2P2(y) | B1P1(z))
  61. #define BYTES_JOIN_TO_WORD_2_1_1(x, y, z) (B2P3(x) | B1P2(y) | B1P1(z))
  62. #define BYTES_JOIN_TO_WORD_1_1_1_1(x, y, z, w) (B1P4(x) | B1P3(y) | B1P2(z) | B1P1(w))
  63. /*@}*/
  64. /*!
  65. * @name Secondary flash configuration
  66. * @{
  67. */
  68. /*! @brief Indicates whether the secondary flash has its own protection register in flash module. */
  69. #if defined(FSL_FEATURE_FLASH_HAS_MULTIPLE_FLASH) && defined(FTFE_FPROTS_PROTS_MASK)
  70. #define FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER (1)
  71. #else
  72. #define FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER (0)
  73. #endif
  74. /*! @brief Indicates whether the secondary flash has its own Execute-Only access register in flash module. */
  75. #if defined(FSL_FEATURE_FLASH_HAS_MULTIPLE_FLASH) && defined(FTFE_FACSSS_SGSIZE_S_MASK)
  76. #define FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER (1)
  77. #else
  78. #define FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER (0)
  79. #endif
  80. /*@}*/
  81. /*!
  82. * @name Dual core/flash configuration
  83. * @{
  84. */
  85. /*! @brief Redefines some flash features. */
  86. #if defined(FSL_FEATURE_FLASH_CURRENT_CORE_ID)
  87. #if (FSL_FEATURE_FLASH_CURRENT_CORE_ID == 0u)
  88. #define MAIN_FLASH_FEATURE_PFLASH_START_ADDRESS FSL_FEATURE_FLASH_PFLASH_START_ADDRESS
  89. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_COUNT FSL_FEATURE_FLASH_PFLASH_BLOCK_COUNT
  90. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_SIZE
  91. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE
  92. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE
  93. #define MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT FSL_FEATURE_FLASH_PFLASH_PROTECTION_REGION_COUNT
  94. #define SECONDARY_FLASH_FEATURE_PFLASH_START_ADDRESS FSL_FEATURE_FLASH_PFLASH_1_START_ADDRESS
  95. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_COUNT FSL_FEATURE_FLASH_PFLASH_1_BLOCK_COUNT
  96. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SIZE FSL_FEATURE_FLASH_PFLASH_1_BLOCK_SIZE
  97. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE FSL_FEATURE_FLASH_PFLASH_1_BLOCK_SECTOR_SIZE
  98. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE FSL_FEATURE_FLASH_PFLASH_1_BLOCK_WRITE_UNIT_SIZE
  99. #define SECONDARY_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT FSL_FEATURE_FLASH_PFLASH_1_PROTECTION_REGION_COUNT
  100. #elif (FSL_FEATURE_FLASH_CURRENT_CORE_ID == 1u)
  101. #define MAIN_FLASH_FEATURE_PFLASH_START_ADDRESS FSL_FEATURE_FLASH_PFLASH_1_START_ADDRESS
  102. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_COUNT FSL_FEATURE_FLASH_PFLASH_1_BLOCK_COUNT
  103. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_SIZE FSL_FEATURE_FLASH_PFLASH_1_BLOCK_SIZE
  104. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE FSL_FEATURE_FLASH_PFLASH_1_BLOCK_SECTOR_SIZE
  105. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE FSL_FEATURE_FLASH_PFLASH_1_BLOCK_WRITE_UNIT_SIZE
  106. #define MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT FSL_FEATURE_FLASH_PFLASH_1_PROTECTION_REGION_COUNT
  107. #define SECONDARY_FLASH_FEATURE_PFLASH_START_ADDRESS FSL_FEATURE_FLASH_PFLASH_START_ADDRESS
  108. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_COUNT FSL_FEATURE_FLASH_PFLASH_BLOCK_COUNT
  109. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_SIZE
  110. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE
  111. #define SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE
  112. #define SECONDARY_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT FSL_FEATURE_FLASH_PFLASH_PROTECTION_REGION_COUNT
  113. #endif
  114. #else
  115. #define MAIN_FLASH_FEATURE_PFLASH_START_ADDRESS FSL_FEATURE_FLASH_PFLASH_START_ADDRESS
  116. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_COUNT FSL_FEATURE_FLASH_PFLASH_BLOCK_COUNT
  117. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_SIZE
  118. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE
  119. #define MAIN_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE
  120. #define MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT FSL_FEATURE_FLASH_PFLASH_PROTECTION_REGION_COUNT
  121. #endif
  122. /*@}*/
  123. /*!
  124. * @name Flash cache and speculation control defines
  125. * @{
  126. */
  127. #if defined(MCM_PLACR_CFCC_MASK) || defined(MCM_CPCR2_CCBC_MASK)
  128. #define FLASH_CACHE_IS_CONTROLLED_BY_MCM (1)
  129. #else
  130. #define FLASH_CACHE_IS_CONTROLLED_BY_MCM (0)
  131. #endif
  132. #if defined(FMC_PFB0CR_CINV_WAY_MASK) || defined(FMC_PFB01CR_CINV_WAY_MASK)
  133. #define FLASH_CACHE_IS_CONTROLLED_BY_FMC (1)
  134. #else
  135. #define FLASH_CACHE_IS_CONTROLLED_BY_FMC (0)
  136. #endif
  137. #if defined(MCM_PLACR_DFCS_MASK)
  138. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM (1)
  139. #else
  140. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM (0)
  141. #endif
  142. #if defined(MSCM_OCMDR_OCMC1_MASK) || defined(MSCM_OCMDR_OCM1_MASK) || defined(MSCM_OCMDR0_OCM1_MASK) || \
  143. defined(MSCM_OCMDR1_OCM1_MASK)
  144. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM (1)
  145. #else
  146. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM (0)
  147. #endif
  148. #if defined(FMC_PFB0CR_S_INV_MASK) || defined(FMC_PFB0CR_S_B_INV_MASK) || defined(FMC_PFB01CR_S_INV_MASK) || \
  149. defined(FMC_PFB01CR_S_B_INV_MASK)
  150. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC (1)
  151. #else
  152. #define FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC (0)
  153. #endif
  154. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM || FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC || \
  155. FLASH_CACHE_IS_CONTROLLED_BY_MCM || FLASH_CACHE_IS_CONTROLLED_BY_FMC
  156. #define FLASH_IS_CACHE_INVALIDATION_AVAILABLE (1)
  157. #else
  158. #define FLASH_IS_CACHE_INVALIDATION_AVAILABLE (0)
  159. #endif
  160. /*@}*/
  161. /*! @brief Data flash IFR map Field*/
  162. #if defined(FSL_FEATURE_FLASH_IS_FTFE) && FSL_FEATURE_FLASH_IS_FTFE
  163. #define DFLASH_IFR_READRESOURCE_START_ADDRESS 0x8003F8U
  164. #else /* FSL_FEATURE_FLASH_IS_FTFL == 1 or FSL_FEATURE_FLASH_IS_FTFA = =1 */
  165. #define DFLASH_IFR_READRESOURCE_START_ADDRESS 0x8000F8U
  166. #endif
  167. /*!
  168. * @name Reserved FlexNVM size (For a variety of purposes) defines
  169. * @{
  170. */
  171. #define FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED 0xFFFFFFFFU
  172. #define FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_RESERVED 0xFFFFU
  173. /*@}*/
  174. /*!
  175. * @name Flash Program Once Field defines
  176. * @{
  177. */
  178. #if defined(FSL_FEATURE_FLASH_IS_FTFA) && FSL_FEATURE_FLASH_IS_FTFA
  179. /* FTFA parts(eg. K80, KL80, L5K) support both 4-bytes and 8-bytes unit size */
  180. #define FLASH_PROGRAM_ONCE_MIN_ID_8BYTES \
  181. 0x10U /* Minimum Index indcating one of Progam Once Fields which is accessed in 8-byte records */
  182. #define FLASH_PROGRAM_ONCE_MAX_ID_8BYTES \
  183. 0x13U /* Maximum Index indcating one of Progam Once Fields which is accessed in 8-byte records */
  184. #define FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT 1
  185. #define FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT 1
  186. #elif defined(FSL_FEATURE_FLASH_IS_FTFE) && FSL_FEATURE_FLASH_IS_FTFE
  187. /* FTFE parts(eg. K65, KE18) only support 8-bytes unit size */
  188. #define FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT 0
  189. #define FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT 1
  190. #elif defined(FSL_FEATURE_FLASH_IS_FTFL) && FSL_FEATURE_FLASH_IS_FTFL
  191. /* FTFL parts(eg. K20) only support 4-bytes unit size */
  192. #define FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT 1
  193. #define FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT 0
  194. #endif
  195. /*@}*/
  196. /*!
  197. * @name Flash security status defines
  198. * @{
  199. */
  200. #define FLASH_SECURITY_STATE_KEYEN 0x80U
  201. #define FLASH_SECURITY_STATE_UNSECURED 0x02U
  202. #define FLASH_NOT_SECURE 0x01U
  203. #define FLASH_SECURE_BACKDOOR_ENABLED 0x02U
  204. #define FLASH_SECURE_BACKDOOR_DISABLED 0x04U
  205. /*@}*/
  206. /*!
  207. * @name Flash controller command numbers
  208. * @{
  209. */
  210. #define FTFx_VERIFY_BLOCK 0x00U /*!< RD1BLK*/
  211. #define FTFx_VERIFY_SECTION 0x01U /*!< RD1SEC*/
  212. #define FTFx_PROGRAM_CHECK 0x02U /*!< PGMCHK*/
  213. #define FTFx_READ_RESOURCE 0x03U /*!< RDRSRC*/
  214. #define FTFx_PROGRAM_LONGWORD 0x06U /*!< PGM4*/
  215. #define FTFx_PROGRAM_PHRASE 0x07U /*!< PGM8*/
  216. #define FTFx_ERASE_BLOCK 0x08U /*!< ERSBLK*/
  217. #define FTFx_ERASE_SECTOR 0x09U /*!< ERSSCR*/
  218. #define FTFx_PROGRAM_SECTION 0x0BU /*!< PGMSEC*/
  219. #define FTFx_GENERATE_CRC 0x0CU /*!< CRCGEN*/
  220. #define FTFx_VERIFY_ALL_BLOCK 0x40U /*!< RD1ALL*/
  221. #define FTFx_READ_ONCE 0x41U /*!< RDONCE or RDINDEX*/
  222. #define FTFx_PROGRAM_ONCE 0x43U /*!< PGMONCE or PGMINDEX*/
  223. #define FTFx_ERASE_ALL_BLOCK 0x44U /*!< ERSALL*/
  224. #define FTFx_SECURITY_BY_PASS 0x45U /*!< VFYKEY*/
  225. #define FTFx_SWAP_CONTROL 0x46U /*!< SWAP*/
  226. #define FTFx_ERASE_ALL_BLOCK_UNSECURE 0x49U /*!< ERSALLU*/
  227. #define FTFx_VERIFY_ALL_EXECUTE_ONLY_SEGMENT 0x4AU /*!< RD1XA*/
  228. #define FTFx_ERASE_ALL_EXECUTE_ONLY_SEGMENT 0x4BU /*!< ERSXA*/
  229. #define FTFx_PROGRAM_PARTITION 0x80U /*!< PGMPART)*/
  230. #define FTFx_SET_FLEXRAM_FUNCTION 0x81U /*!< SETRAM*/
  231. /*@}*/
  232. /*!
  233. * @name Common flash register info defines
  234. * @{
  235. */
  236. #if defined(FTFA)
  237. #define FTFx FTFA
  238. #define FTFx_BASE FTFA_BASE
  239. #define FTFx_FSTAT_CCIF_MASK FTFA_FSTAT_CCIF_MASK
  240. #define FTFx_FSTAT_RDCOLERR_MASK FTFA_FSTAT_RDCOLERR_MASK
  241. #define FTFx_FSTAT_ACCERR_MASK FTFA_FSTAT_ACCERR_MASK
  242. #define FTFx_FSTAT_FPVIOL_MASK FTFA_FSTAT_FPVIOL_MASK
  243. #define FTFx_FSTAT_MGSTAT0_MASK FTFA_FSTAT_MGSTAT0_MASK
  244. #define FTFx_FSEC_SEC_MASK FTFA_FSEC_SEC_MASK
  245. #define FTFx_FSEC_KEYEN_MASK FTFA_FSEC_KEYEN_MASK
  246. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_RAM) && FSL_FEATURE_FLASH_HAS_FLEX_RAM
  247. #define FTFx_FCNFG_RAMRDY_MASK FTFA_FCNFG_RAMRDY_MASK
  248. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_RAM */
  249. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_NVM) && FSL_FEATURE_FLASH_HAS_FLEX_NVM
  250. #define FTFx_FCNFG_EEERDY_MASK FTFA_FCNFG_EEERDY_MASK
  251. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM */
  252. #elif defined(FTFE)
  253. #define FTFx FTFE
  254. #define FTFx_BASE FTFE_BASE
  255. #define FTFx_FSTAT_CCIF_MASK FTFE_FSTAT_CCIF_MASK
  256. #define FTFx_FSTAT_RDCOLERR_MASK FTFE_FSTAT_RDCOLERR_MASK
  257. #define FTFx_FSTAT_ACCERR_MASK FTFE_FSTAT_ACCERR_MASK
  258. #define FTFx_FSTAT_FPVIOL_MASK FTFE_FSTAT_FPVIOL_MASK
  259. #define FTFx_FSTAT_MGSTAT0_MASK FTFE_FSTAT_MGSTAT0_MASK
  260. #define FTFx_FSEC_SEC_MASK FTFE_FSEC_SEC_MASK
  261. #define FTFx_FSEC_KEYEN_MASK FTFE_FSEC_KEYEN_MASK
  262. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_RAM) && FSL_FEATURE_FLASH_HAS_FLEX_RAM
  263. #define FTFx_FCNFG_RAMRDY_MASK FTFE_FCNFG_RAMRDY_MASK
  264. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_RAM */
  265. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_NVM) && FSL_FEATURE_FLASH_HAS_FLEX_NVM
  266. #define FTFx_FCNFG_EEERDY_MASK FTFE_FCNFG_EEERDY_MASK
  267. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM */
  268. #elif defined(FTFL)
  269. #define FTFx FTFL
  270. #define FTFx_BASE FTFL_BASE
  271. #define FTFx_FSTAT_CCIF_MASK FTFL_FSTAT_CCIF_MASK
  272. #define FTFx_FSTAT_RDCOLERR_MASK FTFL_FSTAT_RDCOLERR_MASK
  273. #define FTFx_FSTAT_ACCERR_MASK FTFL_FSTAT_ACCERR_MASK
  274. #define FTFx_FSTAT_FPVIOL_MASK FTFL_FSTAT_FPVIOL_MASK
  275. #define FTFx_FSTAT_MGSTAT0_MASK FTFL_FSTAT_MGSTAT0_MASK
  276. #define FTFx_FSEC_SEC_MASK FTFL_FSEC_SEC_MASK
  277. #define FTFx_FSEC_KEYEN_MASK FTFL_FSEC_KEYEN_MASK
  278. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_RAM) && FSL_FEATURE_FLASH_HAS_FLEX_RAM
  279. #define FTFx_FCNFG_RAMRDY_MASK FTFL_FCNFG_RAMRDY_MASK
  280. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_RAM */
  281. #if defined(FSL_FEATURE_FLASH_HAS_FLEX_NVM) && FSL_FEATURE_FLASH_HAS_FLEX_NVM
  282. #define FTFx_FCNFG_EEERDY_MASK FTFL_FCNFG_EEERDY_MASK
  283. #endif /* FSL_FEATURE_FLASH_HAS_FLEX_NVM */
  284. #else
  285. #error "Unknown flash controller"
  286. #endif
  287. /*@}*/
  288. /*!
  289. * @name Common flash register access info defines
  290. * @{
  291. */
  292. #define FTFx_FCCOB3_REG (FTFx->FCCOB3)
  293. #define FTFx_FCCOB5_REG (FTFx->FCCOB5)
  294. #define FTFx_FCCOB6_REG (FTFx->FCCOB6)
  295. #define FTFx_FCCOB7_REG (FTFx->FCCOB7)
  296. #if defined(FTFA_FPROTH0_PROT_MASK) || defined(FTFE_FPROTH0_PROT_MASK) || defined(FTFL_FPROTH0_PROT_MASK)
  297. #define FTFx_FPROT_HIGH_REG (FTFx->FPROTH3)
  298. #define FTFx_FPROTH3_REG (FTFx->FPROTH3)
  299. #define FTFx_FPROTH2_REG (FTFx->FPROTH2)
  300. #define FTFx_FPROTH1_REG (FTFx->FPROTH1)
  301. #define FTFx_FPROTH0_REG (FTFx->FPROTH0)
  302. #endif
  303. #if defined(FTFA_FPROTL0_PROT_MASK) || defined(FTFE_FPROTL0_PROT_MASK) || defined(FTFL_FPROTL0_PROT_MASK)
  304. #define FTFx_FPROT_LOW_REG (FTFx->FPROTL3)
  305. #define FTFx_FPROTL3_REG (FTFx->FPROTL3)
  306. #define FTFx_FPROTL2_REG (FTFx->FPROTL2)
  307. #define FTFx_FPROTL1_REG (FTFx->FPROTL1)
  308. #define FTFx_FPROTL0_REG (FTFx->FPROTL0)
  309. #elif defined(FTFA_FPROT0_PROT_MASK) || defined(FTFE_FPROT0_PROT_MASK) || defined(FTFL_FPROT0_PROT_MASK)
  310. #define FTFx_FPROT_LOW_REG (FTFx->FPROT3)
  311. #define FTFx_FPROTL3_REG (FTFx->FPROT3)
  312. #define FTFx_FPROTL2_REG (FTFx->FPROT2)
  313. #define FTFx_FPROTL1_REG (FTFx->FPROT1)
  314. #define FTFx_FPROTL0_REG (FTFx->FPROT0)
  315. #endif
  316. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER
  317. #define FTFx_FPROTSH_REG (FTFx->FPROTSH)
  318. #define FTFx_FPROTSL_REG (FTFx->FPROTSL)
  319. #endif
  320. #define FTFx_XACCH3_REG (FTFx->XACCH3)
  321. #define FTFx_XACCL3_REG (FTFx->XACCL3)
  322. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER
  323. #define FTFx_XACCSH_REG (FTFx->XACCSH)
  324. #define FTFx_XACCSL_REG (FTFx->XACCSL)
  325. #endif
  326. /*@}*/
  327. /*!
  328. * @brief Enumeration for access segment property.
  329. */
  330. enum _flash_access_segment_property
  331. {
  332. kFLASH_AccessSegmentBase = 256UL,
  333. };
  334. /*!
  335. * @brief Enumeration for flash config area.
  336. */
  337. enum _flash_config_area_range
  338. {
  339. kFLASH_ConfigAreaStart = 0x400U,
  340. kFLASH_ConfigAreaEnd = 0x40FU
  341. };
  342. /*!
  343. * @name Flash register access type defines
  344. * @{
  345. */
  346. #define FTFx_REG8_ACCESS_TYPE volatile uint8_t *
  347. #define FTFx_REG32_ACCESS_TYPE volatile uint32_t *
  348. /*@}*/
  349. /*!
  350. * @brief MCM cache register access info defines.
  351. */
  352. #if defined(MCM_PLACR_CFCC_MASK)
  353. #define MCM_CACHE_CLEAR_MASK MCM_PLACR_CFCC_MASK
  354. #define MCM_CACHE_CLEAR_SHIFT MCM_PLACR_CFCC_SHIFT
  355. #if defined(MCM0)
  356. #define MCM0_CACHE_REG MCM0->PLACR
  357. #elif defined(MCM) && (!defined(MCM1))
  358. #define MCM0_CACHE_REG MCM->PLACR
  359. #endif
  360. #if defined(MCM1)
  361. #define MCM1_CACHE_REG MCM1->PLACR
  362. #elif defined(MCM) && (!defined(MCM0))
  363. #define MCM1_CACHE_REG MCM->PLACR
  364. #endif
  365. #elif defined(MCM_CPCR2_CCBC_MASK)
  366. #define MCM_CACHE_CLEAR_MASK MCM_CPCR2_CCBC_MASK
  367. #define MCM_CACHE_CLEAR_SHIFT MCM_CPCR2_CCBC_SHIFT
  368. #if defined(MCM0)
  369. #define MCM0_CACHE_REG MCM0->CPCR2
  370. #elif defined(MCM) && (!defined(MCM1))
  371. #define MCM0_CACHE_REG MCM->CPCR2
  372. #endif
  373. #if defined(MCM1)
  374. #define MCM1_CACHE_REG MCM1->CPCR2
  375. #elif defined(MCM) && (!defined(MCM0))
  376. #define MCM1_CACHE_REG MCM->CPCR2
  377. #endif
  378. #endif
  379. /*!
  380. * @brief Enumeration for ARM core part number.
  381. */
  382. enum _arm_core_part_number
  383. {
  384. kARM_CorePartNumber_CM0 = 0xc20U,
  385. kARM_CorePartNumber_CM0P = 0xc60U,
  386. kARM_CorePartNumber_CM1 = 0xc21U,
  387. kARM_CorePartNumber_CM3 = 0xc23U,
  388. kARM_CorePartNumber_CM4 = 0xc24U,
  389. kARM_CorePartNumber_CM7 = 0xc27U,
  390. kARM_CorePartNumber_CM23 = 0xd20U,
  391. kARM_CorePartNumber_CM33 = 0xd21U,
  392. kARM_CorePartNumber_Invalid = 0xFFFFU,
  393. };
  394. #if defined(BL_TARGET_ROM) && defined(MCM0_CACHE_REG) && defined(MCM1_CACHE_REG) && \
  395. defined(FSL_FEATURE_FLASH_CURRENT_CORE_ID)
  396. FTFx_REG32_ACCESS_TYPE const s_mcmModuleAccessTypeArray[] = {
  397. (FTFx_REG32_ACCESS_TYPE)&MCM0_CACHE_REG,
  398. (FTFx_REG32_ACCESS_TYPE)&MCM1_CACHE_REG
  399. };
  400. static const uint16_t s_armCorePartNumberArray[] = {
  401. kARM_CorePartNumber_CM0P,
  402. kARM_CorePartNumber_CM1,
  403. kARM_CorePartNumber_Invalid,
  404. kARM_CorePartNumber_CM3,
  405. kARM_CorePartNumber_CM4,
  406. kARM_CorePartNumber_Invalid,
  407. kARM_CorePartNumber_Invalid,
  408. kARM_CorePartNumber_CM7
  409. };
  410. #endif
  411. /*!
  412. * @brief MSCM cache register access info defines.
  413. */
  414. #if defined(MSCM_OCMDR_OCM1_MASK)
  415. #define MSCM_SPECULATION_DISABLE_MASK MSCM_OCMDR_OCM1_MASK
  416. #define MSCM_SPECULATION_DISABLE_SHIFT MSCM_OCMDR_OCM1_SHIFT
  417. #define MSCM_SPECULATION_DISABLE(x) MSCM_OCMDR_OCM1(x)
  418. #elif defined(MSCM_OCMDR0_OCM1_MASK) || defined(MSCM_OCMDR1_OCM1_MASK)
  419. #define MSCM_SPECULATION_DISABLE_MASK MSCM_OCMDR0_OCM1_MASK
  420. #define MSCM_SPECULATION_DISABLE_SHIFT MSCM_OCMDR0_OCM1_SHIFT
  421. #define MSCM_SPECULATION_DISABLE(x) MSCM_OCMDR0_OCM1(x)
  422. #elif defined(MSCM_OCMDR_OCMC1_MASK)
  423. #define MSCM_SPECULATION_DISABLE_MASK MSCM_OCMDR_OCMC1_MASK
  424. #define MSCM_SPECULATION_DISABLE_SHIFT MSCM_OCMDR_OCMC1_SHIFT
  425. #define MSCM_SPECULATION_DISABLE(x) MSCM_OCMDR_OCMC1(x)
  426. #endif
  427. #if defined(MSCM_OCMDR_OCM1_MASK) || defined(MSCM_OCMDR_OCMC1_MASK)
  428. #define MSCM_OCMDR0_REG MSCM->OCMDR[0]
  429. #define MSCM_OCMDR1_REG MSCM->OCMDR[1]
  430. #elif defined(MSCM_OCMDR0_OCM1_MASK) || defined(MSCM_OCMDR1_OCM1_MASK)
  431. #define MSCM_OCMDR0_REG MSCM->OCMDR0
  432. #define MSCM_OCMDR1_REG MSCM->OCMDR1
  433. #endif
  434. /*!
  435. * @brief MSCM prefetch speculation defines.
  436. */
  437. #define MSCM_OCMDR_OCMC1_DFDS_MASK (0x10U)
  438. #define MSCM_OCMDR_OCMC1_DFCS_MASK (0x20U)
  439. #define MSCM_OCMDR_OCMC1_DFDS_SHIFT (4U)
  440. #define MSCM_OCMDR_OCMC1_DFCS_SHIFT (5U)
  441. /*******************************************************************************
  442. * Prototypes
  443. ******************************************************************************/
  444. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  445. /*! @brief Copy flash_run_command() to RAM*/
  446. static void copy_flash_run_command(uint32_t *flashRunCommand);
  447. #if FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  448. /*! @brief Copy flash_cache_clear_command() to RAM*/
  449. static void copy_flash_common_bit_operation(uint32_t *flashCommonBitOperation);
  450. #endif
  451. /*! @brief Check whether flash execute-in-ram functions are ready*/
  452. static status_t flash_check_execute_in_ram_function_info(flash_config_t *config);
  453. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  454. /*! @brief Internal function Flash command sequence. Called by driver APIs only*/
  455. static status_t flash_command_sequence(flash_config_t *config);
  456. /*! @brief Perform the cache clear to the flash*/
  457. void flash_cache_clear(flash_config_t *config);
  458. /*! @brief Process the cache to the flash*/
  459. static void flash_cache_clear_process(flash_config_t *config, flash_cache_clear_process_t process);
  460. /*! @brief Validates the range and alignment of the given address range.*/
  461. static status_t flash_check_range(flash_config_t *config,
  462. uint32_t startAddress,
  463. uint32_t lengthInBytes,
  464. uint32_t alignmentBaseline);
  465. /*! @brief Gets the right address, sector and block size of current flash type which is indicated by address.*/
  466. static status_t flash_get_matched_operation_info(flash_config_t *config,
  467. uint32_t address,
  468. flash_operation_config_t *info);
  469. /*! @brief Validates the given user key for flash erase APIs.*/
  470. static status_t flash_check_user_key(uint32_t key);
  471. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  472. /*! @brief Updates FlexNVM memory partition status according to data flash 0 IFR.*/
  473. static status_t flash_update_flexnvm_memory_partition_status(flash_config_t *config);
  474. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  475. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  476. /*! @brief Validates the range of the given resource address.*/
  477. static status_t flash_check_resource_range(uint32_t start,
  478. uint32_t lengthInBytes,
  479. uint32_t alignmentBaseline,
  480. flash_read_resource_option_t option);
  481. #endif /* FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD */
  482. #if defined(FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD) && FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD
  483. /*! @brief Validates the gived swap control option.*/
  484. static status_t flash_check_swap_control_option(flash_swap_control_option_t option);
  485. #endif /* FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD */
  486. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  487. /*! @brief Validates the gived address to see if it is equal to swap indicator address in pflash swap IFR.*/
  488. static status_t flash_validate_swap_indicator_address(flash_config_t *config, uint32_t address);
  489. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */
  490. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  491. /*! @brief Validates the gived flexram function option.*/
  492. static inline status_t flasn_check_flexram_function_option_range(flash_flexram_function_option_t option);
  493. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  494. /*! @brief Gets the flash protection information (region size, region count).*/
  495. static status_t flash_get_protection_info(flash_config_t *config, flash_protection_config_t *info);
  496. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL) && FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL
  497. /*! @brief Gets the flash Execute-Only access information (Segment size, Segment count).*/
  498. static status_t flash_get_access_info(flash_config_t *config, flash_access_config_t *info);
  499. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */
  500. #if FLASH_CACHE_IS_CONTROLLED_BY_MCM
  501. /*! @brief Performs the cache clear to the flash by MCM.*/
  502. void mcm_flash_cache_clear(void);
  503. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_MCM */
  504. #if FLASH_CACHE_IS_CONTROLLED_BY_FMC
  505. /*! @brief Performs the cache clear to the flash by FMC.*/
  506. void fmc_flash_cache_clear(void);
  507. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_FMC */
  508. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  509. /*! @brief Sets the prefetch speculation buffer to the flash by MSCM.*/
  510. void mscm_flash_prefetch_speculation_enable(uint32_t flashIndex, bool enable);
  511. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM */
  512. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  513. /*! @brief Performs the prefetch speculation buffer clear to the flash by FMC.*/
  514. void fmc_flash_prefetch_speculation_clear(void);
  515. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC */
  516. /*******************************************************************************
  517. * Variables
  518. ******************************************************************************/
  519. /*! @brief Access to FTFx->FCCOB */
  520. volatile uint32_t *const kFCCOBx = (volatile uint32_t *)&FTFx_FCCOB3_REG;
  521. /*! @brief Access to FTFx->FPROT */
  522. volatile uint32_t *const kFPROTL = (volatile uint32_t *)&FTFx_FPROT_LOW_REG;
  523. #if defined(FTFx_FPROT_HIGH_REG)
  524. volatile uint32_t *const kFPROTH = (volatile uint32_t *)&FTFx_FPROT_HIGH_REG;
  525. #endif
  526. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER
  527. volatile uint8_t *const kFPROTSL = (volatile uint8_t *)&FTFx_FPROTSL_REG;
  528. volatile uint8_t *const kFPROTSH = (volatile uint8_t *)&FTFx_FPROTSH_REG;
  529. #endif
  530. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  531. /*! @brief A function pointer used to point to relocated flash_run_command() */
  532. static void (*callFlashRunCommand)(FTFx_REG8_ACCESS_TYPE ftfx_fstat);
  533. /*!
  534. * @brief Position independent code of flash_run_command()
  535. *
  536. * Note1: The prototype of C function is shown as below:
  537. * @code
  538. * void flash_run_command(FTFx_REG8_ACCESS_TYPE ftfx_fstat)
  539. * {
  540. * // clear CCIF bit
  541. * *ftfx_fstat = FTFx_FSTAT_CCIF_MASK;
  542. *
  543. * // Check CCIF bit of the flash status register, wait till it is set.
  544. * // IP team indicates that this loop will always complete.
  545. * while (!((*ftfx_fstat) & FTFx_FSTAT_CCIF_MASK))
  546. * {
  547. * }
  548. * }
  549. * @endcode
  550. * Note2: The binary code is generated by IAR 7.70.1
  551. */
  552. static const uint16_t s_flashRunCommandFunctionCode[] = {
  553. 0x2180, /* MOVS R1, #128 ; 0x80 */
  554. 0x7001, /* STRB R1, [R0] */
  555. /* @4: */
  556. 0x7802, /* LDRB R2, [R0] */
  557. 0x420a, /* TST R2, R1 */
  558. 0xd0fc, /* BEQ.N @4 */
  559. 0x4770 /* BX LR */
  560. };
  561. #if FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  562. /*! @brief A function pointer used to point to relocated flash_common_bit_operation() */
  563. static void (*callFlashCommonBitOperation)(FTFx_REG32_ACCESS_TYPE base,
  564. uint32_t bitMask,
  565. uint32_t bitShift,
  566. uint32_t bitValue);
  567. /*!
  568. * @brief Position independent code of flash_common_bit_operation()
  569. *
  570. * Note1: The prototype of C function is shown as below:
  571. * @code
  572. * void flash_common_bit_operation(FTFx_REG32_ACCESS_TYPE base, uint32_t bitMask, uint32_t bitShift, uint32_t
  573. * bitValue)
  574. * {
  575. * if (bitMask)
  576. * {
  577. * uint32_t value = (((uint32_t)(((uint32_t)(bitValue)) << bitShift)) & bitMask);
  578. * *base = (*base & (~bitMask)) | value;
  579. * }
  580. *
  581. * __ISB();
  582. * __DSB();
  583. * }
  584. * @endcode
  585. * Note2: The binary code is generated by IAR 7.70.1
  586. */
  587. static const uint16_t s_flashCommonBitOperationFunctionCode[] = {
  588. 0xb510, /* PUSH {R4, LR} */
  589. 0x2900, /* CMP R1, #0 */
  590. 0xd005, /* BEQ.N @12 */
  591. 0x6804, /* LDR R4, [R0] */
  592. 0x438c, /* BICS R4, R4, R1 */
  593. 0x4093, /* LSLS R3, R3, R2 */
  594. 0x4019, /* ANDS R1, R1, R3 */
  595. 0x4321, /* ORRS R1, R1, R4 */
  596. 0x6001, /* STR R1, [R0] */
  597. /* @12: */
  598. 0xf3bf, 0x8f6f, /* ISB */
  599. 0xf3bf, 0x8f4f, /* DSB */
  600. 0xbd10 /* POP {R4, PC} */
  601. };
  602. #endif /* FLASH_IS_CACHE_INVALIDATION_AVAILABLE */
  603. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  604. #if (FLASH_DRIVER_IS_FLASH_RESIDENT && !FLASH_DRIVER_IS_EXPORTED)
  605. /*! @brief A static buffer used to hold flash_run_command() */
  606. static uint32_t s_flashRunCommand[kFLASH_ExecuteInRamFunctionMaxSizeInWords];
  607. #if FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  608. /*! @brief A static buffer used to hold flash_common_bit_operation() */
  609. static uint32_t s_flashCommonBitOperation[kFLASH_ExecuteInRamFunctionMaxSizeInWords];
  610. #endif
  611. /*! @brief Flash execute-in-ram function information */
  612. static flash_execute_in_ram_function_config_t s_flashExecuteInRamFunctionInfo;
  613. #endif
  614. /*!
  615. * @brief Table of pflash sizes.
  616. *
  617. * The index into this table is the value of the SIM_FCFG1.PFSIZE bitfield.
  618. *
  619. * The values in this table have been right shifted 10 bits so that they will all fit within
  620. * an 16-bit integer. To get the actual flash density, you must left shift the looked up value
  621. * by 10 bits.
  622. *
  623. * Elements of this table have a value of 0 in cases where the PFSIZE bitfield value is
  624. * reserved.
  625. *
  626. * Code to use the table:
  627. * @code
  628. * uint8_t pfsize = (SIM->FCFG1 & SIM_FCFG1_PFSIZE_MASK) >> SIM_FCFG1_PFSIZE_SHIFT;
  629. * flashDensity = ((uint32_t)kPFlashDensities[pfsize]) << 10;
  630. * @endcode
  631. */
  632. #if defined(FSL_FEATURE_FLASH_SIZE_ENCODING_RULE_VERSION) && (FSL_FEATURE_FLASH_SIZE_ENCODING_RULE_VERSION == 1)
  633. const uint16_t kPFlashDensities[] = {
  634. 0, /* 0x0 - undefined */
  635. 0, /* 0x1 - undefined */
  636. 0, /* 0x2 - undefined */
  637. 0, /* 0x3 - undefined */
  638. 0, /* 0x4 - undefined */
  639. 0, /* 0x5 - undefined */
  640. 0, /* 0x6 - undefined */
  641. 0, /* 0x7 - undefined */
  642. 0, /* 0x8 - undefined */
  643. 0, /* 0x9 - undefined */
  644. 256, /* 0xa - 262144, 256KB */
  645. 0, /* 0xb - undefined */
  646. 1024, /* 0xc - 1048576, 1MB */
  647. 0, /* 0xd - undefined */
  648. 0, /* 0xe - undefined */
  649. 0, /* 0xf - undefined */
  650. };
  651. #else
  652. const uint16_t kPFlashDensities[] = {
  653. 8, /* 0x0 - 8192, 8KB */
  654. 16, /* 0x1 - 16384, 16KB */
  655. 24, /* 0x2 - 24576, 24KB */
  656. 32, /* 0x3 - 32768, 32KB */
  657. 48, /* 0x4 - 49152, 48KB */
  658. 64, /* 0x5 - 65536, 64KB */
  659. 96, /* 0x6 - 98304, 96KB */
  660. 128, /* 0x7 - 131072, 128KB */
  661. 192, /* 0x8 - 196608, 192KB */
  662. 256, /* 0x9 - 262144, 256KB */
  663. 384, /* 0xa - 393216, 384KB */
  664. 512, /* 0xb - 524288, 512KB */
  665. 768, /* 0xc - 786432, 768KB */
  666. 1024, /* 0xd - 1048576, 1MB */
  667. 1536, /* 0xe - 1572864, 1.5MB */
  668. /* 2048, 0xf - 2097152, 2MB */
  669. };
  670. #endif
  671. /*******************************************************************************
  672. * Code
  673. ******************************************************************************/
  674. status_t FLASH_Init(flash_config_t *config)
  675. {
  676. if (config == NULL)
  677. {
  678. return kStatus_FLASH_InvalidArgument;
  679. }
  680. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED
  681. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  682. {
  683. /* calculate the flash density from SIM_FCFG1.PFSIZE */
  684. #if defined(SIM_FCFG1_CORE1_PFSIZE_MASK)
  685. uint32_t flashDensity;
  686. uint8_t pfsize = (SIM->FCFG1 & SIM_FCFG1_CORE1_PFSIZE_MASK) >> SIM_FCFG1_CORE1_PFSIZE_SHIFT;
  687. if (pfsize == 0xf)
  688. {
  689. flashDensity = SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_COUNT * SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SIZE;
  690. }
  691. else
  692. {
  693. flashDensity = ((uint32_t)kPFlashDensities[pfsize]) << 10;
  694. }
  695. config->PFlashTotalSize = flashDensity;
  696. #else
  697. /* Unused code to solve MISRA-C issue*/
  698. config->PFlashBlockBase = kPFlashDensities[0];
  699. config->PFlashTotalSize = SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_COUNT * SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SIZE;
  700. #endif
  701. config->PFlashBlockBase = SECONDARY_FLASH_FEATURE_PFLASH_START_ADDRESS;
  702. config->PFlashBlockCount = SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_COUNT;
  703. config->PFlashSectorSize = SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE;
  704. }
  705. else
  706. #endif /* FLASH_SSD_IS_SECONDARY_FLASH_ENABLED */
  707. {
  708. uint32_t flashDensity;
  709. /* calculate the flash density from SIM_FCFG1.PFSIZE */
  710. #if defined(SIM_FCFG1_CORE0_PFSIZE_MASK)
  711. uint8_t pfsize = (SIM->FCFG1 & SIM_FCFG1_CORE0_PFSIZE_MASK) >> SIM_FCFG1_CORE0_PFSIZE_SHIFT;
  712. #elif defined(SIM_FCFG1_PFSIZE_MASK)
  713. uint8_t pfsize = (SIM->FCFG1 & SIM_FCFG1_PFSIZE_MASK) >> SIM_FCFG1_PFSIZE_SHIFT;
  714. #else
  715. #error "Unknown flash size"
  716. #endif
  717. /* PFSIZE=0xf means that on customer parts the IFR was not correctly programmed.
  718. * We just use the pre-defined flash size in feature file here to support pre-production parts */
  719. if (pfsize == 0xf)
  720. {
  721. flashDensity = MAIN_FLASH_FEATURE_PFLASH_BLOCK_COUNT * MAIN_FLASH_FEATURE_PFLASH_BLOCK_SIZE;
  722. }
  723. else
  724. {
  725. flashDensity = ((uint32_t)kPFlashDensities[pfsize]) << 10;
  726. }
  727. /* fill out a few of the structure members */
  728. config->PFlashBlockBase = MAIN_FLASH_FEATURE_PFLASH_START_ADDRESS;
  729. config->PFlashTotalSize = flashDensity;
  730. config->PFlashBlockCount = MAIN_FLASH_FEATURE_PFLASH_BLOCK_COUNT;
  731. config->PFlashSectorSize = MAIN_FLASH_FEATURE_PFLASH_BLOCK_SECTOR_SIZE;
  732. }
  733. {
  734. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL) && FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL
  735. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER
  736. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  737. {
  738. config->PFlashAccessSegmentSize = kFLASH_AccessSegmentBase << FTFx->FACSSS;
  739. config->PFlashAccessSegmentCount = FTFx->FACSNS;
  740. }
  741. else
  742. #endif
  743. {
  744. config->PFlashAccessSegmentSize = kFLASH_AccessSegmentBase << FTFx->FACSS;
  745. config->PFlashAccessSegmentCount = FTFx->FACSN;
  746. }
  747. #else
  748. config->PFlashAccessSegmentSize = 0;
  749. config->PFlashAccessSegmentCount = 0;
  750. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */
  751. }
  752. /* copy required flash commands to RAM */
  753. #if (FLASH_DRIVER_IS_FLASH_RESIDENT && !FLASH_DRIVER_IS_EXPORTED)
  754. if (kStatus_FLASH_Success != flash_check_execute_in_ram_function_info(config))
  755. {
  756. s_flashExecuteInRamFunctionInfo.activeFunctionCount = 0;
  757. s_flashExecuteInRamFunctionInfo.flashRunCommand = s_flashRunCommand;
  758. #if FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  759. s_flashExecuteInRamFunctionInfo.flashCommonBitOperation = s_flashCommonBitOperation;
  760. #endif
  761. config->flashExecuteInRamFunctionInfo = &s_flashExecuteInRamFunctionInfo.activeFunctionCount;
  762. FLASH_PrepareExecuteInRamFunctions(config);
  763. }
  764. #endif
  765. config->FlexRAMBlockBase = FSL_FEATURE_FLASH_FLEX_RAM_START_ADDRESS;
  766. config->FlexRAMTotalSize = FSL_FEATURE_FLASH_FLEX_RAM_SIZE;
  767. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  768. {
  769. status_t returnCode;
  770. config->DFlashBlockBase = FSL_FEATURE_FLASH_FLEX_NVM_START_ADDRESS;
  771. returnCode = flash_update_flexnvm_memory_partition_status(config);
  772. if (returnCode != kStatus_FLASH_Success)
  773. {
  774. return returnCode;
  775. }
  776. }
  777. #endif
  778. return kStatus_FLASH_Success;
  779. }
  780. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  781. status_t FLASH_PrepareExecuteInRamFunctions(flash_config_t *config)
  782. {
  783. flash_execute_in_ram_function_config_t *flashExecuteInRamFunctionInfo;
  784. if ((config == NULL) || (config->flashExecuteInRamFunctionInfo == NULL))
  785. {
  786. return kStatus_FLASH_InvalidArgument;
  787. }
  788. flashExecuteInRamFunctionInfo = (flash_execute_in_ram_function_config_t *)config->flashExecuteInRamFunctionInfo;
  789. copy_flash_run_command(flashExecuteInRamFunctionInfo->flashRunCommand);
  790. #if FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  791. copy_flash_common_bit_operation(flashExecuteInRamFunctionInfo->flashCommonBitOperation);
  792. #endif
  793. flashExecuteInRamFunctionInfo->activeFunctionCount = kFLASH_ExecuteInRamFunctionTotalNum;
  794. return kStatus_FLASH_Success;
  795. }
  796. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  797. status_t FLASH_EraseAll(flash_config_t *config, uint32_t key)
  798. {
  799. status_t returnCode;
  800. if (config == NULL)
  801. {
  802. return kStatus_FLASH_InvalidArgument;
  803. }
  804. /* preparing passing parameter to erase all flash blocks */
  805. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_ERASE_ALL_BLOCK, 0xFFFFFFU);
  806. /* Validate the user key */
  807. returnCode = flash_check_user_key(key);
  808. if (returnCode)
  809. {
  810. return returnCode;
  811. }
  812. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  813. /* calling flash command sequence function to execute the command */
  814. returnCode = flash_command_sequence(config);
  815. flash_cache_clear(config);
  816. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  817. /* Data flash IFR will be erased by erase all command, so we need to
  818. * update FlexNVM memory partition status synchronously */
  819. if (returnCode == kStatus_FLASH_Success)
  820. {
  821. returnCode = flash_update_flexnvm_memory_partition_status(config);
  822. }
  823. #endif
  824. return returnCode;
  825. }
  826. status_t FLASH_Erase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
  827. {
  828. uint32_t sectorSize;
  829. flash_operation_config_t flashOperationInfo;
  830. uint32_t endAddress; /* storing end address */
  831. uint32_t numberOfSectors; /* number of sectors calculated by endAddress */
  832. status_t returnCode;
  833. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  834. /* Check the supplied address range. */
  835. returnCode = flash_check_range(config, start, lengthInBytes, flashOperationInfo.sectorCmdAddressAligment);
  836. if (returnCode)
  837. {
  838. return returnCode;
  839. }
  840. /* Validate the user key */
  841. returnCode = flash_check_user_key(key);
  842. if (returnCode)
  843. {
  844. return returnCode;
  845. }
  846. start = flashOperationInfo.convertedAddress;
  847. sectorSize = flashOperationInfo.activeSectorSize;
  848. /* calculating Flash end address */
  849. endAddress = start + lengthInBytes - 1;
  850. /* re-calculate the endAddress and align it to the start of the next sector
  851. * which will be used in the comparison below */
  852. if (endAddress % sectorSize)
  853. {
  854. numberOfSectors = endAddress / sectorSize + 1;
  855. endAddress = numberOfSectors * sectorSize - 1;
  856. }
  857. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  858. /* the start address will increment to the next sector address
  859. * until it reaches the endAdddress */
  860. while (start <= endAddress)
  861. {
  862. /* preparing passing parameter to erase a flash block */
  863. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_ERASE_SECTOR, start);
  864. /* calling flash command sequence function to execute the command */
  865. returnCode = flash_command_sequence(config);
  866. /* checking the success of command execution */
  867. if (kStatus_FLASH_Success != returnCode)
  868. {
  869. break;
  870. }
  871. else
  872. {
  873. /* Increment to the next sector */
  874. start += sectorSize;
  875. }
  876. }
  877. flash_cache_clear(config);
  878. return (returnCode);
  879. }
  880. #if defined(FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD) && FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD
  881. status_t FLASH_EraseAllUnsecure(flash_config_t *config, uint32_t key)
  882. {
  883. status_t returnCode;
  884. if (config == NULL)
  885. {
  886. return kStatus_FLASH_InvalidArgument;
  887. }
  888. /* Prepare passing parameter to erase all flash blocks (unsecure). */
  889. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_ERASE_ALL_BLOCK_UNSECURE, 0xFFFFFFU);
  890. /* Validate the user key */
  891. returnCode = flash_check_user_key(key);
  892. if (returnCode)
  893. {
  894. return returnCode;
  895. }
  896. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  897. /* calling flash command sequence function to execute the command */
  898. returnCode = flash_command_sequence(config);
  899. flash_cache_clear(config);
  900. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  901. /* Data flash IFR will be erased by erase all unsecure command, so we need to
  902. * update FlexNVM memory partition status synchronously */
  903. if (returnCode == kStatus_FLASH_Success)
  904. {
  905. returnCode = flash_update_flexnvm_memory_partition_status(config);
  906. }
  907. #endif
  908. return returnCode;
  909. }
  910. #endif /* FSL_FEATURE_FLASH_HAS_ERASE_ALL_BLOCKS_UNSECURE_CMD */
  911. status_t FLASH_EraseAllExecuteOnlySegments(flash_config_t *config, uint32_t key)
  912. {
  913. status_t returnCode;
  914. if (config == NULL)
  915. {
  916. return kStatus_FLASH_InvalidArgument;
  917. }
  918. /* preparing passing parameter to erase all execute-only segments
  919. * 1st element for the FCCOB register */
  920. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_ERASE_ALL_EXECUTE_ONLY_SEGMENT, 0xFFFFFFU);
  921. /* Validate the user key */
  922. returnCode = flash_check_user_key(key);
  923. if (returnCode)
  924. {
  925. return returnCode;
  926. }
  927. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  928. /* calling flash command sequence function to execute the command */
  929. returnCode = flash_command_sequence(config);
  930. flash_cache_clear(config);
  931. return returnCode;
  932. }
  933. status_t FLASH_Program(flash_config_t *config, uint32_t start, uint32_t *src, uint32_t lengthInBytes)
  934. {
  935. status_t returnCode;
  936. flash_operation_config_t flashOperationInfo;
  937. if (src == NULL)
  938. {
  939. return kStatus_FLASH_InvalidArgument;
  940. }
  941. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  942. /* Check the supplied address range. */
  943. returnCode = flash_check_range(config, start, lengthInBytes, flashOperationInfo.blockWriteUnitSize);
  944. if (returnCode)
  945. {
  946. return returnCode;
  947. }
  948. start = flashOperationInfo.convertedAddress;
  949. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  950. while (lengthInBytes > 0)
  951. {
  952. /* preparing passing parameter to program the flash block */
  953. kFCCOBx[1] = *src++;
  954. if (4 == flashOperationInfo.blockWriteUnitSize)
  955. {
  956. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_PROGRAM_LONGWORD, start);
  957. }
  958. else if (8 == flashOperationInfo.blockWriteUnitSize)
  959. {
  960. kFCCOBx[2] = *src++;
  961. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_PROGRAM_PHRASE, start);
  962. }
  963. else
  964. {
  965. }
  966. /* calling flash command sequence function to execute the command */
  967. returnCode = flash_command_sequence(config);
  968. /* checking for the success of command execution */
  969. if (kStatus_FLASH_Success != returnCode)
  970. {
  971. break;
  972. }
  973. else
  974. {
  975. /* update start address for next iteration */
  976. start += flashOperationInfo.blockWriteUnitSize;
  977. /* update lengthInBytes for next iteration */
  978. lengthInBytes -= flashOperationInfo.blockWriteUnitSize;
  979. }
  980. }
  981. flash_cache_clear(config);
  982. return (returnCode);
  983. }
  984. status_t FLASH_ProgramOnce(flash_config_t *config, uint32_t index, uint32_t *src, uint32_t lengthInBytes)
  985. {
  986. status_t returnCode;
  987. if ((config == NULL) || (src == NULL))
  988. {
  989. return kStatus_FLASH_InvalidArgument;
  990. }
  991. /* pass paramters to FTFx */
  992. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_1_2(FTFx_PROGRAM_ONCE, index, 0xFFFFU);
  993. kFCCOBx[1] = *src;
  994. /* Note: Have to seperate the first index from the rest if it equals 0
  995. * to avoid a pointless comparison of unsigned int to 0 compiler warning */
  996. #if FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT
  997. #if FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT
  998. if (((index == FLASH_PROGRAM_ONCE_MIN_ID_8BYTES) ||
  999. /* Range check */
  1000. ((index >= FLASH_PROGRAM_ONCE_MIN_ID_8BYTES + 1) && (index <= FLASH_PROGRAM_ONCE_MAX_ID_8BYTES))) &&
  1001. (lengthInBytes == 8))
  1002. #endif /* FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT */
  1003. {
  1004. kFCCOBx[2] = *(src + 1);
  1005. }
  1006. #endif /* FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT */
  1007. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  1008. /* calling flash command sequence function to execute the command */
  1009. returnCode = flash_command_sequence(config);
  1010. flash_cache_clear(config);
  1011. return returnCode;
  1012. }
  1013. #if defined(FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD) && FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD
  1014. status_t FLASH_ProgramSection(flash_config_t *config, uint32_t start, uint32_t *src, uint32_t lengthInBytes)
  1015. {
  1016. status_t returnCode;
  1017. uint32_t sectorSize;
  1018. flash_operation_config_t flashOperationInfo;
  1019. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  1020. bool needSwitchFlexRamMode = false;
  1021. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  1022. if (src == NULL)
  1023. {
  1024. return kStatus_FLASH_InvalidArgument;
  1025. }
  1026. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  1027. /* Check the supplied address range. */
  1028. returnCode = flash_check_range(config, start, lengthInBytes, flashOperationInfo.sectionCmdAddressAligment);
  1029. if (returnCode)
  1030. {
  1031. return returnCode;
  1032. }
  1033. start = flashOperationInfo.convertedAddress;
  1034. sectorSize = flashOperationInfo.activeSectorSize;
  1035. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  1036. /* Switch function of FlexRAM if needed */
  1037. if (!(FTFx->FCNFG & FTFx_FCNFG_RAMRDY_MASK))
  1038. {
  1039. needSwitchFlexRamMode = true;
  1040. returnCode = FLASH_SetFlexramFunction(config, kFLASH_FlexramFunctionOptionAvailableAsRam);
  1041. if (returnCode != kStatus_FLASH_Success)
  1042. {
  1043. return kStatus_FLASH_SetFlexramAsRamError;
  1044. }
  1045. }
  1046. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  1047. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  1048. while (lengthInBytes > 0)
  1049. {
  1050. /* Make sure the write operation doesn't span two sectors */
  1051. uint32_t endAddressOfCurrentSector = ALIGN_UP(start, sectorSize);
  1052. uint32_t lengthTobeProgrammedOfCurrentSector;
  1053. uint32_t currentOffset = 0;
  1054. if (endAddressOfCurrentSector == start)
  1055. {
  1056. endAddressOfCurrentSector += sectorSize;
  1057. }
  1058. if (lengthInBytes + start > endAddressOfCurrentSector)
  1059. {
  1060. lengthTobeProgrammedOfCurrentSector = endAddressOfCurrentSector - start;
  1061. }
  1062. else
  1063. {
  1064. lengthTobeProgrammedOfCurrentSector = lengthInBytes;
  1065. }
  1066. /* Program Current Sector */
  1067. while (lengthTobeProgrammedOfCurrentSector > 0)
  1068. {
  1069. /* Make sure the program size doesn't exceeds Acceleration RAM size */
  1070. uint32_t programSizeOfCurrentPass;
  1071. uint32_t numberOfPhases;
  1072. if (lengthTobeProgrammedOfCurrentSector > kFLASH_AccelerationRamSize)
  1073. {
  1074. programSizeOfCurrentPass = kFLASH_AccelerationRamSize;
  1075. }
  1076. else
  1077. {
  1078. programSizeOfCurrentPass = lengthTobeProgrammedOfCurrentSector;
  1079. }
  1080. /* Copy data to FlexRAM */
  1081. memcpy((void *)FSL_FEATURE_FLASH_FLEX_RAM_START_ADDRESS, src + currentOffset / 4, programSizeOfCurrentPass);
  1082. /* Set start address of the data to be programmed */
  1083. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_PROGRAM_SECTION, start + currentOffset);
  1084. /* Set program size in terms of FEATURE_FLASH_SECTION_CMD_ADDRESS_ALIGMENT */
  1085. numberOfPhases = programSizeOfCurrentPass / flashOperationInfo.sectionCmdAddressAligment;
  1086. kFCCOBx[1] = BYTES_JOIN_TO_WORD_2_2(numberOfPhases, 0xFFFFU);
  1087. /* Peform command sequence */
  1088. returnCode = flash_command_sequence(config);
  1089. if (returnCode != kStatus_FLASH_Success)
  1090. {
  1091. flash_cache_clear(config);
  1092. return returnCode;
  1093. }
  1094. lengthTobeProgrammedOfCurrentSector -= programSizeOfCurrentPass;
  1095. currentOffset += programSizeOfCurrentPass;
  1096. }
  1097. src += currentOffset / 4;
  1098. start += currentOffset;
  1099. lengthInBytes -= currentOffset;
  1100. }
  1101. flash_cache_clear(config);
  1102. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  1103. /* Restore function of FlexRAM if needed. */
  1104. if (needSwitchFlexRamMode)
  1105. {
  1106. returnCode = FLASH_SetFlexramFunction(config, kFLASH_FlexramFunctionOptionAvailableForEeprom);
  1107. if (returnCode != kStatus_FLASH_Success)
  1108. {
  1109. return kStatus_FLASH_RecoverFlexramAsEepromError;
  1110. }
  1111. }
  1112. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  1113. return returnCode;
  1114. }
  1115. #endif /* FSL_FEATURE_FLASH_HAS_PROGRAM_SECTION_CMD */
  1116. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  1117. status_t FLASH_EepromWrite(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  1118. {
  1119. status_t returnCode;
  1120. bool needSwitchFlexRamMode = false;
  1121. if (config == NULL)
  1122. {
  1123. return kStatus_FLASH_InvalidArgument;
  1124. }
  1125. /* Validates the range of the given address */
  1126. if ((start < config->FlexRAMBlockBase) ||
  1127. ((start + lengthInBytes) > (config->FlexRAMBlockBase + config->EEpromTotalSize)))
  1128. {
  1129. return kStatus_FLASH_AddressError;
  1130. }
  1131. returnCode = kStatus_FLASH_Success;
  1132. /* Switch function of FlexRAM if needed */
  1133. if (!(FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
  1134. {
  1135. needSwitchFlexRamMode = true;
  1136. returnCode = FLASH_SetFlexramFunction(config, kFLASH_FlexramFunctionOptionAvailableForEeprom);
  1137. if (returnCode != kStatus_FLASH_Success)
  1138. {
  1139. return kStatus_FLASH_SetFlexramAsEepromError;
  1140. }
  1141. }
  1142. /* Write data to FlexRAM when it is used as EEPROM emulator */
  1143. while (lengthInBytes > 0)
  1144. {
  1145. if ((!(start & 0x3U)) && (lengthInBytes >= 4))
  1146. {
  1147. *(uint32_t *)start = *(uint32_t *)src;
  1148. start += 4;
  1149. src += 4;
  1150. lengthInBytes -= 4;
  1151. }
  1152. else if ((!(start & 0x1U)) && (lengthInBytes >= 2))
  1153. {
  1154. *(uint16_t *)start = *(uint16_t *)src;
  1155. start += 2;
  1156. src += 2;
  1157. lengthInBytes -= 2;
  1158. }
  1159. else
  1160. {
  1161. *(uint8_t *)start = *src;
  1162. start += 1;
  1163. src += 1;
  1164. lengthInBytes -= 1;
  1165. }
  1166. /* Wait till EEERDY bit is set */
  1167. while (!(FTFx->FCNFG & FTFx_FCNFG_EEERDY_MASK))
  1168. {
  1169. }
  1170. /* Check for protection violation error */
  1171. if (FTFx->FSTAT & FTFx_FSTAT_FPVIOL_MASK)
  1172. {
  1173. return kStatus_FLASH_ProtectionViolation;
  1174. }
  1175. }
  1176. /* Switch function of FlexRAM if needed */
  1177. if (needSwitchFlexRamMode)
  1178. {
  1179. returnCode = FLASH_SetFlexramFunction(config, kFLASH_FlexramFunctionOptionAvailableAsRam);
  1180. if (returnCode != kStatus_FLASH_Success)
  1181. {
  1182. return kStatus_FLASH_RecoverFlexramAsRamError;
  1183. }
  1184. }
  1185. return returnCode;
  1186. }
  1187. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  1188. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  1189. status_t FLASH_ReadResource(
  1190. flash_config_t *config, uint32_t start, uint32_t *dst, uint32_t lengthInBytes, flash_read_resource_option_t option)
  1191. {
  1192. status_t returnCode;
  1193. flash_operation_config_t flashOperationInfo;
  1194. if ((config == NULL) || (dst == NULL))
  1195. {
  1196. return kStatus_FLASH_InvalidArgument;
  1197. }
  1198. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  1199. /* Check the supplied address range. */
  1200. returnCode =
  1201. flash_check_resource_range(start, lengthInBytes, flashOperationInfo.resourceCmdAddressAligment, option);
  1202. if (returnCode != kStatus_FLASH_Success)
  1203. {
  1204. return returnCode;
  1205. }
  1206. while (lengthInBytes > 0)
  1207. {
  1208. /* preparing passing parameter */
  1209. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_READ_RESOURCE, start);
  1210. if (flashOperationInfo.resourceCmdAddressAligment == 4)
  1211. {
  1212. kFCCOBx[2] = BYTES_JOIN_TO_WORD_1_3(option, 0xFFFFFFU);
  1213. }
  1214. else if (flashOperationInfo.resourceCmdAddressAligment == 8)
  1215. {
  1216. kFCCOBx[1] = BYTES_JOIN_TO_WORD_1_3(option, 0xFFFFFFU);
  1217. }
  1218. else
  1219. {
  1220. }
  1221. /* calling flash command sequence function to execute the command */
  1222. returnCode = flash_command_sequence(config);
  1223. if (kStatus_FLASH_Success != returnCode)
  1224. {
  1225. break;
  1226. }
  1227. /* fetch data */
  1228. *dst++ = kFCCOBx[1];
  1229. if (flashOperationInfo.resourceCmdAddressAligment == 8)
  1230. {
  1231. *dst++ = kFCCOBx[2];
  1232. }
  1233. /* update start address for next iteration */
  1234. start += flashOperationInfo.resourceCmdAddressAligment;
  1235. /* update lengthInBytes for next iteration */
  1236. lengthInBytes -= flashOperationInfo.resourceCmdAddressAligment;
  1237. }
  1238. return (returnCode);
  1239. }
  1240. #endif /* FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD */
  1241. status_t FLASH_ReadOnce(flash_config_t *config, uint32_t index, uint32_t *dst, uint32_t lengthInBytes)
  1242. {
  1243. status_t returnCode;
  1244. if ((config == NULL) || (dst == NULL))
  1245. {
  1246. return kStatus_FLASH_InvalidArgument;
  1247. }
  1248. /* pass paramters to FTFx */
  1249. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_1_2(FTFx_READ_ONCE, index, 0xFFFFU);
  1250. /* calling flash command sequence function to execute the command */
  1251. returnCode = flash_command_sequence(config);
  1252. if (kStatus_FLASH_Success == returnCode)
  1253. {
  1254. *dst = kFCCOBx[1];
  1255. /* Note: Have to seperate the first index from the rest if it equals 0
  1256. * to avoid a pointless comparison of unsigned int to 0 compiler warning */
  1257. #if FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT
  1258. #if FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT
  1259. if (((index == FLASH_PROGRAM_ONCE_MIN_ID_8BYTES) ||
  1260. /* Range check */
  1261. ((index >= FLASH_PROGRAM_ONCE_MIN_ID_8BYTES + 1) && (index <= FLASH_PROGRAM_ONCE_MAX_ID_8BYTES))) &&
  1262. (lengthInBytes == 8))
  1263. #endif /* FLASH_PROGRAM_ONCE_IS_4BYTES_UNIT_SUPPORT */
  1264. {
  1265. *(dst + 1) = kFCCOBx[2];
  1266. }
  1267. #endif /* FLASH_PROGRAM_ONCE_IS_8BYTES_UNIT_SUPPORT */
  1268. }
  1269. return returnCode;
  1270. }
  1271. status_t FLASH_GetSecurityState(flash_config_t *config, flash_security_state_t *state)
  1272. {
  1273. /* store data read from flash register */
  1274. uint8_t registerValue;
  1275. if ((config == NULL) || (state == NULL))
  1276. {
  1277. return kStatus_FLASH_InvalidArgument;
  1278. }
  1279. /* Get flash security register value */
  1280. registerValue = FTFx->FSEC;
  1281. /* check the status of the flash security bits in the security register */
  1282. if (FLASH_SECURITY_STATE_UNSECURED == (registerValue & FTFx_FSEC_SEC_MASK))
  1283. {
  1284. /* Flash in unsecured state */
  1285. *state = kFLASH_SecurityStateNotSecure;
  1286. }
  1287. else
  1288. {
  1289. /* Flash in secured state
  1290. * check for backdoor key security enable bit */
  1291. if (FLASH_SECURITY_STATE_KEYEN == (registerValue & FTFx_FSEC_KEYEN_MASK))
  1292. {
  1293. /* Backdoor key security enabled */
  1294. *state = kFLASH_SecurityStateBackdoorEnabled;
  1295. }
  1296. else
  1297. {
  1298. /* Backdoor key security disabled */
  1299. *state = kFLASH_SecurityStateBackdoorDisabled;
  1300. }
  1301. }
  1302. return (kStatus_FLASH_Success);
  1303. }
  1304. status_t FLASH_SecurityBypass(flash_config_t *config, const uint8_t *backdoorKey)
  1305. {
  1306. uint8_t registerValue; /* registerValue */
  1307. status_t returnCode; /* return code variable */
  1308. if ((config == NULL) || (backdoorKey == NULL))
  1309. {
  1310. return kStatus_FLASH_InvalidArgument;
  1311. }
  1312. /* set the default return code as kStatus_Success */
  1313. returnCode = kStatus_FLASH_Success;
  1314. /* Get flash security register value */
  1315. registerValue = FTFx->FSEC;
  1316. /* Check to see if flash is in secure state (any state other than 0x2)
  1317. * If not, then skip this since flash is not secure */
  1318. if (0x02 != (registerValue & 0x03))
  1319. {
  1320. /* preparing passing parameter to erase a flash block */
  1321. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_SECURITY_BY_PASS, 0xFFFFFFU);
  1322. kFCCOBx[1] = BYTES_JOIN_TO_WORD_1_1_1_1(backdoorKey[0], backdoorKey[1], backdoorKey[2], backdoorKey[3]);
  1323. kFCCOBx[2] = BYTES_JOIN_TO_WORD_1_1_1_1(backdoorKey[4], backdoorKey[5], backdoorKey[6], backdoorKey[7]);
  1324. /* calling flash command sequence function to execute the command */
  1325. returnCode = flash_command_sequence(config);
  1326. }
  1327. return (returnCode);
  1328. }
  1329. status_t FLASH_VerifyEraseAll(flash_config_t *config, flash_margin_value_t margin)
  1330. {
  1331. if (config == NULL)
  1332. {
  1333. return kStatus_FLASH_InvalidArgument;
  1334. }
  1335. /* preparing passing parameter to verify all block command */
  1336. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_1_2(FTFx_VERIFY_ALL_BLOCK, margin, 0xFFFFU);
  1337. /* calling flash command sequence function to execute the command */
  1338. return flash_command_sequence(config);
  1339. }
  1340. status_t FLASH_VerifyErase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, flash_margin_value_t margin)
  1341. {
  1342. /* Check arguments. */
  1343. uint32_t blockSize;
  1344. flash_operation_config_t flashOperationInfo;
  1345. uint32_t nextBlockStartAddress;
  1346. uint32_t remainingBytes;
  1347. status_t returnCode;
  1348. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  1349. returnCode = flash_check_range(config, start, lengthInBytes, flashOperationInfo.sectionCmdAddressAligment);
  1350. if (returnCode)
  1351. {
  1352. return returnCode;
  1353. }
  1354. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  1355. start = flashOperationInfo.convertedAddress;
  1356. blockSize = flashOperationInfo.activeBlockSize;
  1357. nextBlockStartAddress = ALIGN_UP(start, blockSize);
  1358. if (nextBlockStartAddress == start)
  1359. {
  1360. nextBlockStartAddress += blockSize;
  1361. }
  1362. remainingBytes = lengthInBytes;
  1363. while (remainingBytes)
  1364. {
  1365. uint32_t numberOfPhrases;
  1366. uint32_t verifyLength = nextBlockStartAddress - start;
  1367. if (verifyLength > remainingBytes)
  1368. {
  1369. verifyLength = remainingBytes;
  1370. }
  1371. numberOfPhrases = verifyLength / flashOperationInfo.sectionCmdAddressAligment;
  1372. /* Fill in verify section command parameters. */
  1373. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_VERIFY_SECTION, start);
  1374. kFCCOBx[1] = BYTES_JOIN_TO_WORD_2_1_1(numberOfPhrases, margin, 0xFFU);
  1375. /* calling flash command sequence function to execute the command */
  1376. returnCode = flash_command_sequence(config);
  1377. if (returnCode)
  1378. {
  1379. return returnCode;
  1380. }
  1381. remainingBytes -= verifyLength;
  1382. start += verifyLength;
  1383. nextBlockStartAddress += blockSize;
  1384. }
  1385. return kStatus_FLASH_Success;
  1386. }
  1387. status_t FLASH_VerifyProgram(flash_config_t *config,
  1388. uint32_t start,
  1389. uint32_t lengthInBytes,
  1390. const uint32_t *expectedData,
  1391. flash_margin_value_t margin,
  1392. uint32_t *failedAddress,
  1393. uint32_t *failedData)
  1394. {
  1395. status_t returnCode;
  1396. flash_operation_config_t flashOperationInfo;
  1397. if (expectedData == NULL)
  1398. {
  1399. return kStatus_FLASH_InvalidArgument;
  1400. }
  1401. flash_get_matched_operation_info(config, start, &flashOperationInfo);
  1402. returnCode = flash_check_range(config, start, lengthInBytes, flashOperationInfo.checkCmdAddressAligment);
  1403. if (returnCode)
  1404. {
  1405. return returnCode;
  1406. }
  1407. start = flashOperationInfo.convertedAddress;
  1408. while (lengthInBytes)
  1409. {
  1410. /* preparing passing parameter to program check the flash block */
  1411. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_PROGRAM_CHECK, start);
  1412. kFCCOBx[1] = BYTES_JOIN_TO_WORD_1_3(margin, 0xFFFFFFU);
  1413. kFCCOBx[2] = *expectedData;
  1414. /* calling flash command sequence function to execute the command */
  1415. returnCode = flash_command_sequence(config);
  1416. /* checking for the success of command execution */
  1417. if (kStatus_FLASH_Success != returnCode)
  1418. {
  1419. if (failedAddress)
  1420. {
  1421. *failedAddress = start;
  1422. }
  1423. if (failedData)
  1424. {
  1425. *failedData = 0;
  1426. }
  1427. break;
  1428. }
  1429. lengthInBytes -= flashOperationInfo.checkCmdAddressAligment;
  1430. expectedData += flashOperationInfo.checkCmdAddressAligment / sizeof(*expectedData);
  1431. start += flashOperationInfo.checkCmdAddressAligment;
  1432. }
  1433. return (returnCode);
  1434. }
  1435. status_t FLASH_VerifyEraseAllExecuteOnlySegments(flash_config_t *config, flash_margin_value_t margin)
  1436. {
  1437. if (config == NULL)
  1438. {
  1439. return kStatus_FLASH_InvalidArgument;
  1440. }
  1441. /* preparing passing parameter to verify erase all execute-only segments command */
  1442. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_1_2(FTFx_VERIFY_ALL_EXECUTE_ONLY_SEGMENT, margin, 0xFFFFU);
  1443. /* calling flash command sequence function to execute the command */
  1444. return flash_command_sequence(config);
  1445. }
  1446. status_t FLASH_IsProtected(flash_config_t *config,
  1447. uint32_t start,
  1448. uint32_t lengthInBytes,
  1449. flash_protection_state_t *protection_state)
  1450. {
  1451. uint32_t endAddress; /* end address for protection check */
  1452. uint32_t regionCheckedCounter; /* increments each time the flash address was checked for
  1453. * protection status */
  1454. uint32_t regionCounter; /* incrementing variable used to increment through the flash
  1455. * protection regions */
  1456. uint32_t protectStatusCounter; /* increments each time a flash region was detected as protected */
  1457. uint8_t flashRegionProtectStatus[MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT]; /* array of the protection
  1458. * status for each
  1459. * protection region */
  1460. uint32_t flashRegionAddress[MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT +
  1461. 1]; /* array of the start addresses for each flash
  1462. * protection region. Note this is REGION_COUNT+1
  1463. * due to requiring the next start address after
  1464. * the end of flash for loop-check purposes below */
  1465. flash_protection_config_t flashProtectionInfo; /* flash protection information */
  1466. status_t returnCode;
  1467. if (protection_state == NULL)
  1468. {
  1469. return kStatus_FLASH_InvalidArgument;
  1470. }
  1471. /* Check the supplied address range. */
  1472. returnCode = flash_check_range(config, start, lengthInBytes, MAIN_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE);
  1473. if (returnCode)
  1474. {
  1475. return returnCode;
  1476. }
  1477. /* Get necessary flash protection information. */
  1478. returnCode = flash_get_protection_info(config, &flashProtectionInfo);
  1479. if (returnCode)
  1480. {
  1481. return returnCode;
  1482. }
  1483. /* calculating Flash end address */
  1484. endAddress = start + lengthInBytes;
  1485. /* populate the flashRegionAddress array with the start address of each flash region */
  1486. regionCounter = 0; /* make sure regionCounter is initialized to 0 first */
  1487. /* populate up to 33rd element of array, this is the next address after end of flash array */
  1488. while (regionCounter <= flashProtectionInfo.regionCount)
  1489. {
  1490. flashRegionAddress[regionCounter] =
  1491. flashProtectionInfo.regionBase + flashProtectionInfo.regionSize * regionCounter;
  1492. regionCounter++;
  1493. }
  1494. /* populate flashRegionProtectStatus array with status information
  1495. * Protection status for each region is stored in the FPROT[3:0] registers
  1496. * Each bit represents one region of flash
  1497. * 4 registers * 8-bits-per-register = 32-bits (32-regions)
  1498. * The convention is:
  1499. * FPROT3[bit 0] is the first protection region (start of flash memory)
  1500. * FPROT0[bit 7] is the last protection region (end of flash memory)
  1501. * regionCounter is used to determine which FPROT[3:0] register to check for protection status
  1502. * Note: FPROT=1 means NOT protected, FPROT=0 means protected */
  1503. regionCounter = 0; /* make sure regionCounter is initialized to 0 first */
  1504. while (regionCounter < flashProtectionInfo.regionCount)
  1505. {
  1506. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER
  1507. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  1508. {
  1509. if (regionCounter < 8)
  1510. {
  1511. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTSL_REG >> regionCounter) & (0x01u);
  1512. }
  1513. else if ((regionCounter >= 8) && (regionCounter < 16))
  1514. {
  1515. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTSH_REG >> (regionCounter - 8)) & (0x01u);
  1516. }
  1517. else
  1518. {
  1519. break;
  1520. }
  1521. }
  1522. else
  1523. #endif
  1524. {
  1525. /* Note: So far protection region count may be 16/20/24/32/64 */
  1526. if (regionCounter < 8)
  1527. {
  1528. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL3_REG >> regionCounter) & (0x01u);
  1529. }
  1530. else if ((regionCounter >= 8) && (regionCounter < 16))
  1531. {
  1532. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL2_REG >> (regionCounter - 8)) & (0x01u);
  1533. }
  1534. #if defined(MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT) && (MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT > 16)
  1535. #if (MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT == 20)
  1536. else if ((regionCounter >= 16) && (regionCounter < 20))
  1537. {
  1538. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL1_REG >> (regionCounter - 16)) & (0x01u);
  1539. }
  1540. #else
  1541. else if ((regionCounter >= 16) && (regionCounter < 24))
  1542. {
  1543. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL1_REG >> (regionCounter - 16)) & (0x01u);
  1544. }
  1545. #endif /* (MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT == 20) */
  1546. #endif
  1547. #if defined(MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT) && (MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT > 24)
  1548. else if ((regionCounter >= 24) && (regionCounter < 32))
  1549. {
  1550. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTL0_REG >> (regionCounter - 24)) & (0x01u);
  1551. }
  1552. #endif
  1553. #if defined(MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT) && \
  1554. (MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT == 64)
  1555. else if (regionCounter < 40)
  1556. {
  1557. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH3_REG >> (regionCounter - 32)) & (0x01u);
  1558. }
  1559. else if (regionCounter < 48)
  1560. {
  1561. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH2_REG >> (regionCounter - 40)) & (0x01u);
  1562. }
  1563. else if (regionCounter < 56)
  1564. {
  1565. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH1_REG >> (regionCounter - 48)) & (0x01u);
  1566. }
  1567. else if (regionCounter < 64)
  1568. {
  1569. flashRegionProtectStatus[regionCounter] = (FTFx_FPROTH0_REG >> (regionCounter - 56)) & (0x01u);
  1570. }
  1571. #endif
  1572. else
  1573. {
  1574. break;
  1575. }
  1576. }
  1577. regionCounter++;
  1578. }
  1579. /* loop through the flash regions and check
  1580. * desired flash address range for protection status
  1581. * loop stops when it is detected that start has exceeded the endAddress */
  1582. regionCounter = 0; /* make sure regionCounter is initialized to 0 first */
  1583. regionCheckedCounter = 0;
  1584. protectStatusCounter = 0; /* make sure protectStatusCounter is initialized to 0 first */
  1585. while (start < endAddress)
  1586. {
  1587. /* check to see if the address falls within this protection region
  1588. * Note that if the entire flash is to be checked, the last protection
  1589. * region checked would consist of the last protection start address and
  1590. * the start address following the end of flash */
  1591. if ((start >= flashRegionAddress[regionCounter]) && (start < flashRegionAddress[regionCounter + 1]))
  1592. {
  1593. /* increment regionCheckedCounter to indicate this region was checked */
  1594. regionCheckedCounter++;
  1595. /* check the protection status of this region
  1596. * Note: FPROT=1 means NOT protected, FPROT=0 means protected */
  1597. if (!flashRegionProtectStatus[regionCounter])
  1598. {
  1599. /* increment protectStatusCounter to indicate this region is protected */
  1600. protectStatusCounter++;
  1601. }
  1602. start += flashProtectionInfo.regionSize; /* increment to an address within the next region */
  1603. }
  1604. regionCounter++; /* increment regionCounter to check for the next flash protection region */
  1605. }
  1606. /* if protectStatusCounter == 0, then no region of the desired flash region is protected */
  1607. if (protectStatusCounter == 0)
  1608. {
  1609. *protection_state = kFLASH_ProtectionStateUnprotected;
  1610. }
  1611. /* if protectStatusCounter == regionCheckedCounter, then each region checked was protected */
  1612. else if (protectStatusCounter == regionCheckedCounter)
  1613. {
  1614. *protection_state = kFLASH_ProtectionStateProtected;
  1615. }
  1616. /* if protectStatusCounter != regionCheckedCounter, then protection status is mixed
  1617. * In other words, some regions are protected while others are unprotected */
  1618. else
  1619. {
  1620. *protection_state = kFLASH_ProtectionStateMixed;
  1621. }
  1622. return (returnCode);
  1623. }
  1624. status_t FLASH_IsExecuteOnly(flash_config_t *config,
  1625. uint32_t start,
  1626. uint32_t lengthInBytes,
  1627. flash_execute_only_access_state_t *access_state)
  1628. {
  1629. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL) && FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL
  1630. flash_access_config_t flashAccessInfo; /* flash Execute-Only information */
  1631. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */
  1632. status_t returnCode;
  1633. if (access_state == NULL)
  1634. {
  1635. return kStatus_FLASH_InvalidArgument;
  1636. }
  1637. /* Check the supplied address range. */
  1638. returnCode = flash_check_range(config, start, lengthInBytes, MAIN_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE);
  1639. if (returnCode)
  1640. {
  1641. return returnCode;
  1642. }
  1643. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL) && FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL
  1644. /* Get necessary flash Execute-Only information. */
  1645. returnCode = flash_get_access_info(config, &flashAccessInfo);
  1646. if (returnCode)
  1647. {
  1648. return returnCode;
  1649. }
  1650. {
  1651. uint32_t executeOnlySegmentCounter = 0;
  1652. /* calculating end address */
  1653. uint32_t endAddress = start + lengthInBytes;
  1654. /* Aligning start address and end address */
  1655. uint32_t alignedStartAddress = ALIGN_DOWN(start, flashAccessInfo.SegmentSize);
  1656. uint32_t alignedEndAddress = ALIGN_UP(endAddress, flashAccessInfo.SegmentSize);
  1657. uint32_t segmentIndex = 0;
  1658. uint32_t maxSupportedExecuteOnlySegmentCount =
  1659. (alignedEndAddress - alignedStartAddress) / flashAccessInfo.SegmentSize;
  1660. while (start < endAddress)
  1661. {
  1662. uint32_t xacc;
  1663. bool isInvalidSegmentIndex = false;
  1664. segmentIndex = (start - flashAccessInfo.SegmentBase) / flashAccessInfo.SegmentSize;
  1665. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER
  1666. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  1667. {
  1668. /* For secondary flash, The two XACCS registers allow up to 16 restricted segments of equal memory size.
  1669. */
  1670. if (segmentIndex < 8)
  1671. {
  1672. xacc = *(const volatile uint8_t *)&FTFx_XACCSL_REG;
  1673. }
  1674. else if (segmentIndex < flashAccessInfo.SegmentCount)
  1675. {
  1676. xacc = *(const volatile uint8_t *)&FTFx_XACCSH_REG;
  1677. segmentIndex -= 8;
  1678. }
  1679. else
  1680. {
  1681. isInvalidSegmentIndex = true;
  1682. }
  1683. }
  1684. else
  1685. #endif
  1686. {
  1687. /* For primary flash, The eight XACC registers allow up to 64 restricted segments of equal memory size.
  1688. */
  1689. if (segmentIndex < 32)
  1690. {
  1691. xacc = *(const volatile uint32_t *)&FTFx_XACCL3_REG;
  1692. }
  1693. else if (segmentIndex < flashAccessInfo.SegmentCount)
  1694. {
  1695. xacc = *(const volatile uint32_t *)&FTFx_XACCH3_REG;
  1696. segmentIndex -= 32;
  1697. }
  1698. else
  1699. {
  1700. isInvalidSegmentIndex = true;
  1701. }
  1702. }
  1703. if (isInvalidSegmentIndex)
  1704. {
  1705. break;
  1706. }
  1707. /* Determine if this address range is in a execute-only protection flash segment. */
  1708. if ((~xacc) & (1u << segmentIndex))
  1709. {
  1710. executeOnlySegmentCounter++;
  1711. }
  1712. start += flashAccessInfo.SegmentSize;
  1713. }
  1714. if (executeOnlySegmentCounter < 1u)
  1715. {
  1716. *access_state = kFLASH_AccessStateUnLimited;
  1717. }
  1718. else if (executeOnlySegmentCounter < maxSupportedExecuteOnlySegmentCount)
  1719. {
  1720. *access_state = kFLASH_AccessStateMixed;
  1721. }
  1722. else
  1723. {
  1724. *access_state = kFLASH_AccessStateExecuteOnly;
  1725. }
  1726. }
  1727. #else
  1728. *access_state = kFLASH_AccessStateUnLimited;
  1729. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */
  1730. return (returnCode);
  1731. }
  1732. status_t FLASH_GetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value)
  1733. {
  1734. if ((config == NULL) || (value == NULL))
  1735. {
  1736. return kStatus_FLASH_InvalidArgument;
  1737. }
  1738. switch (whichProperty)
  1739. {
  1740. case kFLASH_PropertyPflashSectorSize:
  1741. *value = config->PFlashSectorSize;
  1742. break;
  1743. case kFLASH_PropertyPflashTotalSize:
  1744. *value = config->PFlashTotalSize;
  1745. break;
  1746. case kFLASH_PropertyPflashBlockSize:
  1747. *value = config->PFlashTotalSize / (uint32_t)config->PFlashBlockCount;
  1748. break;
  1749. case kFLASH_PropertyPflashBlockCount:
  1750. *value = (uint32_t)config->PFlashBlockCount;
  1751. break;
  1752. case kFLASH_PropertyPflashBlockBaseAddr:
  1753. *value = config->PFlashBlockBase;
  1754. break;
  1755. case kFLASH_PropertyPflashFacSupport:
  1756. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL)
  1757. *value = FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL;
  1758. #else
  1759. *value = 0;
  1760. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */
  1761. break;
  1762. case kFLASH_PropertyPflashAccessSegmentSize:
  1763. *value = config->PFlashAccessSegmentSize;
  1764. break;
  1765. case kFLASH_PropertyPflashAccessSegmentCount:
  1766. *value = config->PFlashAccessSegmentCount;
  1767. break;
  1768. case kFLASH_PropertyFlexRamBlockBaseAddr:
  1769. *value = config->FlexRAMBlockBase;
  1770. break;
  1771. case kFLASH_PropertyFlexRamTotalSize:
  1772. *value = config->FlexRAMTotalSize;
  1773. break;
  1774. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  1775. case kFLASH_PropertyDflashSectorSize:
  1776. *value = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_SECTOR_SIZE;
  1777. break;
  1778. case kFLASH_PropertyDflashTotalSize:
  1779. *value = config->DFlashTotalSize;
  1780. break;
  1781. case kFLASH_PropertyDflashBlockSize:
  1782. *value = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_SIZE;
  1783. break;
  1784. case kFLASH_PropertyDflashBlockCount:
  1785. *value = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_COUNT;
  1786. break;
  1787. case kFLASH_PropertyDflashBlockBaseAddr:
  1788. *value = config->DFlashBlockBase;
  1789. break;
  1790. case kFLASH_PropertyEepromTotalSize:
  1791. *value = config->EEpromTotalSize;
  1792. break;
  1793. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  1794. default: /* catch inputs that are not recognized */
  1795. return kStatus_FLASH_UnknownProperty;
  1796. }
  1797. return kStatus_FLASH_Success;
  1798. }
  1799. status_t FLASH_SetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t value)
  1800. {
  1801. status_t status = kStatus_FLASH_Success;
  1802. if (config == NULL)
  1803. {
  1804. return kStatus_FLASH_InvalidArgument;
  1805. }
  1806. switch (whichProperty)
  1807. {
  1808. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED
  1809. case kFLASH_PropertyFlashMemoryIndex:
  1810. if ((value != (uint32_t)kFLASH_MemoryIndexPrimaryFlash) &&
  1811. (value != (uint32_t)kFLASH_MemoryIndexSecondaryFlash))
  1812. {
  1813. return kStatus_FLASH_InvalidPropertyValue;
  1814. }
  1815. config->FlashMemoryIndex = (uint8_t)value;
  1816. break;
  1817. #endif /* FLASH_SSD_IS_SECONDARY_FLASH_ENABLED */
  1818. case kFLASH_PropertyPflashSectorSize:
  1819. case kFLASH_PropertyPflashTotalSize:
  1820. case kFLASH_PropertyPflashBlockSize:
  1821. case kFLASH_PropertyPflashBlockCount:
  1822. case kFLASH_PropertyPflashBlockBaseAddr:
  1823. case kFLASH_PropertyPflashFacSupport:
  1824. case kFLASH_PropertyPflashAccessSegmentSize:
  1825. case kFLASH_PropertyPflashAccessSegmentCount:
  1826. case kFLASH_PropertyFlexRamBlockBaseAddr:
  1827. case kFLASH_PropertyFlexRamTotalSize:
  1828. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  1829. case kFLASH_PropertyDflashSectorSize:
  1830. case kFLASH_PropertyDflashTotalSize:
  1831. case kFLASH_PropertyDflashBlockSize:
  1832. case kFLASH_PropertyDflashBlockCount:
  1833. case kFLASH_PropertyDflashBlockBaseAddr:
  1834. case kFLASH_PropertyEepromTotalSize:
  1835. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  1836. status = kStatus_FLASH_ReadOnlyProperty;
  1837. break;
  1838. default: /* catch inputs that are not recognized */
  1839. status = kStatus_FLASH_UnknownProperty;
  1840. break;
  1841. }
  1842. return status;
  1843. }
  1844. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  1845. status_t FLASH_SetFlexramFunction(flash_config_t *config, flash_flexram_function_option_t option)
  1846. {
  1847. status_t status;
  1848. if (config == NULL)
  1849. {
  1850. return kStatus_FLASH_InvalidArgument;
  1851. }
  1852. status = flasn_check_flexram_function_option_range(option);
  1853. if (status != kStatus_FLASH_Success)
  1854. {
  1855. return status;
  1856. }
  1857. /* preparing passing parameter to verify all block command */
  1858. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_1_2(FTFx_SET_FLEXRAM_FUNCTION, option, 0xFFFFU);
  1859. /* calling flash command sequence function to execute the command */
  1860. return flash_command_sequence(config);
  1861. }
  1862. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  1863. #if defined(FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD) && FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD
  1864. status_t FLASH_SwapControl(flash_config_t *config,
  1865. uint32_t address,
  1866. flash_swap_control_option_t option,
  1867. flash_swap_state_config_t *returnInfo)
  1868. {
  1869. status_t returnCode;
  1870. if ((config == NULL) || (returnInfo == NULL))
  1871. {
  1872. return kStatus_FLASH_InvalidArgument;
  1873. }
  1874. if (address & (FSL_FEATURE_FLASH_PFLASH_SWAP_CONTROL_CMD_ADDRESS_ALIGMENT - 1))
  1875. {
  1876. return kStatus_FLASH_AlignmentError;
  1877. }
  1878. /* Make sure address provided is in the lower half of Program flash but not in the Flash Configuration Field */
  1879. if ((address >= (config->PFlashTotalSize / 2)) ||
  1880. ((address >= kFLASH_ConfigAreaStart) && (address <= kFLASH_ConfigAreaEnd)))
  1881. {
  1882. return kStatus_FLASH_SwapIndicatorAddressError;
  1883. }
  1884. /* Check the option. */
  1885. returnCode = flash_check_swap_control_option(option);
  1886. if (returnCode)
  1887. {
  1888. return returnCode;
  1889. }
  1890. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_SWAP_CONTROL, address);
  1891. kFCCOBx[1] = BYTES_JOIN_TO_WORD_1_3(option, 0xFFFFFFU);
  1892. returnCode = flash_command_sequence(config);
  1893. returnInfo->flashSwapState = (flash_swap_state_t)FTFx_FCCOB5_REG;
  1894. returnInfo->currentSwapBlockStatus = (flash_swap_block_status_t)FTFx_FCCOB6_REG;
  1895. returnInfo->nextSwapBlockStatus = (flash_swap_block_status_t)FTFx_FCCOB7_REG;
  1896. return returnCode;
  1897. }
  1898. #endif /* FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD */
  1899. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  1900. status_t FLASH_Swap(flash_config_t *config, uint32_t address, flash_swap_function_option_t option)
  1901. {
  1902. flash_swap_state_config_t returnInfo;
  1903. status_t returnCode;
  1904. memset(&returnInfo, 0xFFU, sizeof(returnInfo));
  1905. do
  1906. {
  1907. returnCode = FLASH_SwapControl(config, address, kFLASH_SwapControlOptionReportStatus, &returnInfo);
  1908. if (returnCode != kStatus_FLASH_Success)
  1909. {
  1910. return returnCode;
  1911. }
  1912. if (kFLASH_SwapFunctionOptionDisable == option)
  1913. {
  1914. if (returnInfo.flashSwapState == kFLASH_SwapStateDisabled)
  1915. {
  1916. return kStatus_FLASH_Success;
  1917. }
  1918. else if (returnInfo.flashSwapState == kFLASH_SwapStateUninitialized)
  1919. {
  1920. /* The swap system changed to the DISABLED state with Program flash block 0
  1921. * located at relative flash address 0x0_0000 */
  1922. returnCode = FLASH_SwapControl(config, address, kFLASH_SwapControlOptionDisableSystem, &returnInfo);
  1923. }
  1924. else
  1925. {
  1926. /* Swap disable should be requested only when swap system is in the uninitialized state */
  1927. return kStatus_FLASH_SwapSystemNotInUninitialized;
  1928. }
  1929. }
  1930. else
  1931. {
  1932. /* When first swap: the initial swap state is Uninitialized, flash swap inidicator address is unset,
  1933. * the swap procedure should be Uninitialized -> Update-Erased -> Complete.
  1934. * After the first swap has been completed, the flash swap inidicator address cannot be modified
  1935. * unless EraseAllBlocks command is issued, the swap procedure is changed to Update -> Update-Erased ->
  1936. * Complete. */
  1937. switch (returnInfo.flashSwapState)
  1938. {
  1939. case kFLASH_SwapStateUninitialized:
  1940. /* If current swap mode is Uninitialized, Initialize Swap to Initialized/READY state. */
  1941. returnCode =
  1942. FLASH_SwapControl(config, address, kFLASH_SwapControlOptionIntializeSystem, &returnInfo);
  1943. break;
  1944. case kFLASH_SwapStateReady:
  1945. /* Validate whether the address provided to the swap system is matched to
  1946. * swap indicator address in the IFR */
  1947. returnCode = flash_validate_swap_indicator_address(config, address);
  1948. if (returnCode == kStatus_FLASH_Success)
  1949. {
  1950. /* If current swap mode is Initialized/Ready, Initialize Swap to UPDATE state. */
  1951. returnCode =
  1952. FLASH_SwapControl(config, address, kFLASH_SwapControlOptionSetInUpdateState, &returnInfo);
  1953. }
  1954. break;
  1955. case kFLASH_SwapStateUpdate:
  1956. /* If current swap mode is Update, Erase indicator sector in non active block
  1957. * to proceed swap system to update-erased state */
  1958. returnCode = FLASH_Erase(config, address + (config->PFlashTotalSize >> 1),
  1959. FSL_FEATURE_FLASH_PFLASH_SECTOR_CMD_ADDRESS_ALIGMENT, kFLASH_ApiEraseKey);
  1960. break;
  1961. case kFLASH_SwapStateUpdateErased:
  1962. /* If current swap mode is Update or Update-Erased, progress Swap to COMPLETE State */
  1963. returnCode =
  1964. FLASH_SwapControl(config, address, kFLASH_SwapControlOptionSetInCompleteState, &returnInfo);
  1965. break;
  1966. case kFLASH_SwapStateComplete:
  1967. break;
  1968. case kFLASH_SwapStateDisabled:
  1969. /* When swap system is in disabled state, We need to clear swap system back to uninitialized
  1970. * by issuing EraseAllBlocks command */
  1971. returnCode = kStatus_FLASH_SwapSystemNotInUninitialized;
  1972. break;
  1973. default:
  1974. returnCode = kStatus_FLASH_InvalidArgument;
  1975. break;
  1976. }
  1977. }
  1978. if (returnCode != kStatus_FLASH_Success)
  1979. {
  1980. break;
  1981. }
  1982. } while (!((kFLASH_SwapStateComplete == returnInfo.flashSwapState) && (kFLASH_SwapFunctionOptionEnable == option)));
  1983. return returnCode;
  1984. }
  1985. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */
  1986. #if defined(FSL_FEATURE_FLASH_HAS_PROGRAM_PARTITION_CMD) && FSL_FEATURE_FLASH_HAS_PROGRAM_PARTITION_CMD
  1987. status_t FLASH_ProgramPartition(flash_config_t *config,
  1988. flash_partition_flexram_load_option_t option,
  1989. uint32_t eepromDataSizeCode,
  1990. uint32_t flexnvmPartitionCode)
  1991. {
  1992. status_t returnCode;
  1993. if (config == NULL)
  1994. {
  1995. return kStatus_FLASH_InvalidArgument;
  1996. }
  1997. /* eepromDataSizeCode[7:6], flexnvmPartitionCode[7:4] should be all 1'b0
  1998. * or it will cause access error. */
  1999. /* eepromDataSizeCode &= 0x3FU; */
  2000. /* flexnvmPartitionCode &= 0x0FU; */
  2001. /* preparing passing parameter to program the flash block */
  2002. kFCCOBx[0] = BYTES_JOIN_TO_WORD_1_2_1(FTFx_PROGRAM_PARTITION, 0xFFFFU, option);
  2003. kFCCOBx[1] = BYTES_JOIN_TO_WORD_1_1_2(eepromDataSizeCode, flexnvmPartitionCode, 0xFFFFU);
  2004. flash_cache_clear_process(config, kFLASH_CacheClearProcessPre);
  2005. /* calling flash command sequence function to execute the command */
  2006. returnCode = flash_command_sequence(config);
  2007. flash_cache_clear(config);
  2008. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2009. /* Data flash IFR will be updated by program partition command during reset sequence,
  2010. * so we just set reserved values for partitioned FlexNVM size here */
  2011. config->EEpromTotalSize = FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_RESERVED;
  2012. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2013. #endif
  2014. return (returnCode);
  2015. }
  2016. #endif /* FSL_FEATURE_FLASH_HAS_PROGRAM_PARTITION_CMD */
  2017. status_t FLASH_PflashSetProtection(flash_config_t *config, pflash_protection_status_t *protectStatus)
  2018. {
  2019. if (config == NULL)
  2020. {
  2021. return kStatus_FLASH_InvalidArgument;
  2022. }
  2023. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER
  2024. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  2025. {
  2026. *kFPROTSL = protectStatus->valueLow32b.prots16b.protsl;
  2027. if (protectStatus->valueLow32b.prots16b.protsl != *kFPROTSL)
  2028. {
  2029. return kStatus_FLASH_CommandFailure;
  2030. }
  2031. *kFPROTSH = protectStatus->valueLow32b.prots16b.protsh;
  2032. if (protectStatus->valueLow32b.prots16b.protsh != *kFPROTSH)
  2033. {
  2034. return kStatus_FLASH_CommandFailure;
  2035. }
  2036. }
  2037. else
  2038. #endif
  2039. {
  2040. *kFPROTL = protectStatus->valueLow32b.protl32b;
  2041. if (protectStatus->valueLow32b.protl32b != *kFPROTL)
  2042. {
  2043. return kStatus_FLASH_CommandFailure;
  2044. }
  2045. #if defined(FTFx_FPROT_HIGH_REG)
  2046. *kFPROTH = protectStatus->valueHigh32b.proth32b;
  2047. if (protectStatus->valueHigh32b.proth32b != *kFPROTH)
  2048. {
  2049. return kStatus_FLASH_CommandFailure;
  2050. }
  2051. #endif
  2052. }
  2053. return kStatus_FLASH_Success;
  2054. }
  2055. status_t FLASH_PflashGetProtection(flash_config_t *config, pflash_protection_status_t *protectStatus)
  2056. {
  2057. if ((config == NULL) || (protectStatus == NULL))
  2058. {
  2059. return kStatus_FLASH_InvalidArgument;
  2060. }
  2061. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER
  2062. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  2063. {
  2064. protectStatus->valueLow32b.prots16b.protsl = *kFPROTSL;
  2065. protectStatus->valueLow32b.prots16b.protsh = *kFPROTSH;
  2066. }
  2067. else
  2068. #endif
  2069. {
  2070. protectStatus->valueLow32b.protl32b = *kFPROTL;
  2071. #if defined(FTFx_FPROT_HIGH_REG)
  2072. protectStatus->valueHigh32b.proth32b = *kFPROTH;
  2073. #endif
  2074. }
  2075. return kStatus_FLASH_Success;
  2076. }
  2077. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2078. status_t FLASH_DflashSetProtection(flash_config_t *config, uint8_t protectStatus)
  2079. {
  2080. if (config == NULL)
  2081. {
  2082. return kStatus_FLASH_InvalidArgument;
  2083. }
  2084. if ((config->DFlashTotalSize == 0) || (config->DFlashTotalSize == FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED))
  2085. {
  2086. return kStatus_FLASH_CommandNotSupported;
  2087. }
  2088. FTFx->FDPROT = protectStatus;
  2089. if (FTFx->FDPROT != protectStatus)
  2090. {
  2091. return kStatus_FLASH_CommandFailure;
  2092. }
  2093. return kStatus_FLASH_Success;
  2094. }
  2095. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  2096. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2097. status_t FLASH_DflashGetProtection(flash_config_t *config, uint8_t *protectStatus)
  2098. {
  2099. if ((config == NULL) || (protectStatus == NULL))
  2100. {
  2101. return kStatus_FLASH_InvalidArgument;
  2102. }
  2103. if ((config->DFlashTotalSize == 0) || (config->DFlashTotalSize == FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED))
  2104. {
  2105. return kStatus_FLASH_CommandNotSupported;
  2106. }
  2107. *protectStatus = FTFx->FDPROT;
  2108. return kStatus_FLASH_Success;
  2109. }
  2110. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  2111. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2112. status_t FLASH_EepromSetProtection(flash_config_t *config, uint8_t protectStatus)
  2113. {
  2114. if (config == NULL)
  2115. {
  2116. return kStatus_FLASH_InvalidArgument;
  2117. }
  2118. if ((config->EEpromTotalSize == 0) || (config->EEpromTotalSize == FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_RESERVED))
  2119. {
  2120. return kStatus_FLASH_CommandNotSupported;
  2121. }
  2122. FTFx->FEPROT = protectStatus;
  2123. if (FTFx->FEPROT != protectStatus)
  2124. {
  2125. return kStatus_FLASH_CommandFailure;
  2126. }
  2127. return kStatus_FLASH_Success;
  2128. }
  2129. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  2130. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2131. status_t FLASH_EepromGetProtection(flash_config_t *config, uint8_t *protectStatus)
  2132. {
  2133. if ((config == NULL) || (protectStatus == NULL))
  2134. {
  2135. return kStatus_FLASH_InvalidArgument;
  2136. }
  2137. if ((config->EEpromTotalSize == 0) || (config->EEpromTotalSize == FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_RESERVED))
  2138. {
  2139. return kStatus_FLASH_CommandNotSupported;
  2140. }
  2141. *protectStatus = FTFx->FEPROT;
  2142. return kStatus_FLASH_Success;
  2143. }
  2144. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  2145. status_t FLASH_PflashSetPrefetchSpeculation(flash_prefetch_speculation_status_t *speculationStatus)
  2146. {
  2147. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM
  2148. {
  2149. FTFx_REG32_ACCESS_TYPE regBase;
  2150. #if defined(MCM)
  2151. regBase = (FTFx_REG32_ACCESS_TYPE)&MCM->PLACR;
  2152. #elif defined(MCM0)
  2153. regBase = (FTFx_REG32_ACCESS_TYPE)&MCM0->PLACR;
  2154. #endif
  2155. if (speculationStatus->instructionOption == kFLASH_prefetchSpeculationOptionDisable)
  2156. {
  2157. if (speculationStatus->dataOption == kFLASH_prefetchSpeculationOptionEnable)
  2158. {
  2159. return kStatus_FLASH_InvalidSpeculationOption;
  2160. }
  2161. else
  2162. {
  2163. *regBase |= MCM_PLACR_DFCS_MASK;
  2164. }
  2165. }
  2166. else
  2167. {
  2168. *regBase &= ~MCM_PLACR_DFCS_MASK;
  2169. if (speculationStatus->dataOption == kFLASH_prefetchSpeculationOptionEnable)
  2170. {
  2171. *regBase |= MCM_PLACR_EFDS_MASK;
  2172. }
  2173. else
  2174. {
  2175. *regBase &= ~MCM_PLACR_EFDS_MASK;
  2176. }
  2177. }
  2178. }
  2179. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  2180. {
  2181. FTFx_REG32_ACCESS_TYPE regBase;
  2182. uint32_t b0dpeMask, b0ipeMask;
  2183. #if defined(FMC_PFB01CR_B0DPE_MASK)
  2184. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB01CR;
  2185. b0dpeMask = FMC_PFB01CR_B0DPE_MASK;
  2186. b0ipeMask = FMC_PFB01CR_B0IPE_MASK;
  2187. #elif defined(FMC_PFB0CR_B0DPE_MASK)
  2188. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB0CR;
  2189. b0dpeMask = FMC_PFB0CR_B0DPE_MASK;
  2190. b0ipeMask = FMC_PFB0CR_B0IPE_MASK;
  2191. #endif
  2192. if (speculationStatus->instructionOption == kFLASH_prefetchSpeculationOptionEnable)
  2193. {
  2194. *regBase |= b0ipeMask;
  2195. }
  2196. else
  2197. {
  2198. *regBase &= ~b0ipeMask;
  2199. }
  2200. if (speculationStatus->dataOption == kFLASH_prefetchSpeculationOptionEnable)
  2201. {
  2202. *regBase |= b0dpeMask;
  2203. }
  2204. else
  2205. {
  2206. *regBase &= ~b0dpeMask;
  2207. }
  2208. /* Invalidate Prefetch Speculation Buffer */
  2209. #if defined(FMC_PFB01CR_S_INV_MASK)
  2210. FMC->PFB01CR |= FMC_PFB01CR_S_INV_MASK;
  2211. #elif defined(FMC_PFB01CR_S_B_INV_MASK)
  2212. FMC->PFB01CR |= FMC_PFB01CR_S_B_INV_MASK;
  2213. #elif defined(FMC_PFB0CR_S_INV_MASK)
  2214. FMC->PFB0CR |= FMC_PFB0CR_S_INV_MASK;
  2215. #elif defined(FMC_PFB0CR_S_B_INV_MASK)
  2216. FMC->PFB0CR |= FMC_PFB0CR_S_B_INV_MASK;
  2217. #endif
  2218. }
  2219. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  2220. {
  2221. FTFx_REG32_ACCESS_TYPE regBase;
  2222. uint32_t flashSpeculationMask, dataPrefetchMask;
  2223. regBase = (FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR0_REG;
  2224. flashSpeculationMask = MSCM_OCMDR_OCMC1_DFCS_MASK;
  2225. dataPrefetchMask = MSCM_OCMDR_OCMC1_DFDS_MASK;
  2226. if (speculationStatus->instructionOption == kFLASH_prefetchSpeculationOptionDisable)
  2227. {
  2228. if (speculationStatus->dataOption == kFLASH_prefetchSpeculationOptionEnable)
  2229. {
  2230. return kStatus_FLASH_InvalidSpeculationOption;
  2231. }
  2232. else
  2233. {
  2234. *regBase |= flashSpeculationMask;
  2235. }
  2236. }
  2237. else
  2238. {
  2239. *regBase &= ~flashSpeculationMask;
  2240. if (speculationStatus->dataOption == kFLASH_prefetchSpeculationOptionEnable)
  2241. {
  2242. *regBase &= ~dataPrefetchMask;
  2243. }
  2244. else
  2245. {
  2246. *regBase |= dataPrefetchMask;
  2247. }
  2248. }
  2249. }
  2250. #endif /* FSL_FEATURE_FTFx_MCM_FLASH_CACHE_CONTROLS */
  2251. return kStatus_FLASH_Success;
  2252. }
  2253. status_t FLASH_PflashGetPrefetchSpeculation(flash_prefetch_speculation_status_t *speculationStatus)
  2254. {
  2255. memset(speculationStatus, 0, sizeof(flash_prefetch_speculation_status_t));
  2256. /* Assuming that all speculation options are enabled. */
  2257. speculationStatus->instructionOption = kFLASH_prefetchSpeculationOptionEnable;
  2258. speculationStatus->dataOption = kFLASH_prefetchSpeculationOptionEnable;
  2259. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MCM
  2260. {
  2261. uint32_t value;
  2262. #if defined(MCM)
  2263. value = MCM->PLACR;
  2264. #elif defined(MCM0)
  2265. value = MCM0->PLACR;
  2266. #endif
  2267. if (value & MCM_PLACR_DFCS_MASK)
  2268. {
  2269. /* Speculation buffer is off. */
  2270. speculationStatus->instructionOption = kFLASH_prefetchSpeculationOptionDisable;
  2271. speculationStatus->dataOption = kFLASH_prefetchSpeculationOptionDisable;
  2272. }
  2273. else
  2274. {
  2275. /* Speculation buffer is on for instruction. */
  2276. if (!(value & MCM_PLACR_EFDS_MASK))
  2277. {
  2278. /* Speculation buffer is off for data. */
  2279. speculationStatus->dataOption = kFLASH_prefetchSpeculationOptionDisable;
  2280. }
  2281. }
  2282. }
  2283. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  2284. {
  2285. uint32_t value;
  2286. uint32_t b0dpeMask, b0ipeMask;
  2287. #if defined(FMC_PFB01CR_B0DPE_MASK)
  2288. value = FMC->PFB01CR;
  2289. b0dpeMask = FMC_PFB01CR_B0DPE_MASK;
  2290. b0ipeMask = FMC_PFB01CR_B0IPE_MASK;
  2291. #elif defined(FMC_PFB0CR_B0DPE_MASK)
  2292. value = FMC->PFB0CR;
  2293. b0dpeMask = FMC_PFB0CR_B0DPE_MASK;
  2294. b0ipeMask = FMC_PFB0CR_B0IPE_MASK;
  2295. #endif
  2296. if (!(value & b0dpeMask))
  2297. {
  2298. /* Do not prefetch in response to data references. */
  2299. speculationStatus->dataOption = kFLASH_prefetchSpeculationOptionDisable;
  2300. }
  2301. if (!(value & b0ipeMask))
  2302. {
  2303. /* Do not prefetch in response to instruction fetches. */
  2304. speculationStatus->instructionOption = kFLASH_prefetchSpeculationOptionDisable;
  2305. }
  2306. }
  2307. #elif FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  2308. {
  2309. uint32_t value;
  2310. uint32_t flashSpeculationMask, dataPrefetchMask;
  2311. value = MSCM_OCMDR0_REG;
  2312. flashSpeculationMask = MSCM_OCMDR_OCMC1_DFCS_MASK;
  2313. dataPrefetchMask = MSCM_OCMDR_OCMC1_DFDS_MASK;
  2314. if (value & flashSpeculationMask)
  2315. {
  2316. /* Speculation buffer is off. */
  2317. speculationStatus->instructionOption = kFLASH_prefetchSpeculationOptionDisable;
  2318. speculationStatus->dataOption = kFLASH_prefetchSpeculationOptionDisable;
  2319. }
  2320. else
  2321. {
  2322. /* Speculation buffer is on for instruction. */
  2323. if (value & dataPrefetchMask)
  2324. {
  2325. /* Speculation buffer is off for data. */
  2326. speculationStatus->dataOption = kFLASH_prefetchSpeculationOptionDisable;
  2327. }
  2328. }
  2329. }
  2330. #endif
  2331. return kStatus_FLASH_Success;
  2332. }
  2333. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2334. /*!
  2335. * @brief Copy PIC of flash_run_command() to RAM
  2336. */
  2337. static void copy_flash_run_command(uint32_t *flashRunCommand)
  2338. {
  2339. assert(sizeof(s_flashRunCommandFunctionCode) <= (kFLASH_ExecuteInRamFunctionMaxSizeInWords * 4));
  2340. /* Since the value of ARM function pointer is always odd, but the real start address
  2341. * of function memory should be even, that's why +1 operation exist. */
  2342. memcpy((void *)flashRunCommand, (void *)s_flashRunCommandFunctionCode, sizeof(s_flashRunCommandFunctionCode));
  2343. callFlashRunCommand = (void (*)(FTFx_REG8_ACCESS_TYPE ftfx_fstat))((uint32_t)flashRunCommand + 1);
  2344. }
  2345. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2346. /*!
  2347. * @brief Flash Command Sequence
  2348. *
  2349. * This function is used to perform the command write sequence to the flash.
  2350. *
  2351. * @param driver Pointer to storage for the driver runtime state.
  2352. * @return An error code or kStatus_FLASH_Success
  2353. */
  2354. static status_t flash_command_sequence(flash_config_t *config)
  2355. {
  2356. uint8_t registerValue;
  2357. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2358. /* clear RDCOLERR & ACCERR & FPVIOL flag in flash status register */
  2359. FTFx->FSTAT = FTFx_FSTAT_RDCOLERR_MASK | FTFx_FSTAT_ACCERR_MASK | FTFx_FSTAT_FPVIOL_MASK;
  2360. status_t returnCode = flash_check_execute_in_ram_function_info(config);
  2361. if (kStatus_FLASH_Success != returnCode)
  2362. {
  2363. return returnCode;
  2364. }
  2365. /* We pass the ftfx_fstat address as a parameter to flash_run_comamnd() instead of using
  2366. * pre-processed MICRO sentences or operating global variable in flash_run_comamnd()
  2367. * to make sure that flash_run_command() will be compiled into position-independent code (PIC). */
  2368. callFlashRunCommand((FTFx_REG8_ACCESS_TYPE)(&FTFx->FSTAT));
  2369. #else
  2370. /* clear RDCOLERR & ACCERR & FPVIOL flag in flash status register */
  2371. FTFx->FSTAT = FTFx_FSTAT_RDCOLERR_MASK | FTFx_FSTAT_ACCERR_MASK | FTFx_FSTAT_FPVIOL_MASK;
  2372. /* clear CCIF bit */
  2373. FTFx->FSTAT = FTFx_FSTAT_CCIF_MASK;
  2374. /* Check CCIF bit of the flash status register, wait till it is set.
  2375. * IP team indicates that this loop will always complete. */
  2376. while (!(FTFx->FSTAT & FTFx_FSTAT_CCIF_MASK))
  2377. {
  2378. }
  2379. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2380. /* Check error bits */
  2381. /* Get flash status register value */
  2382. registerValue = FTFx->FSTAT;
  2383. /* checking access error */
  2384. if (registerValue & FTFx_FSTAT_ACCERR_MASK)
  2385. {
  2386. return kStatus_FLASH_AccessError;
  2387. }
  2388. /* checking protection error */
  2389. else if (registerValue & FTFx_FSTAT_FPVIOL_MASK)
  2390. {
  2391. return kStatus_FLASH_ProtectionViolation;
  2392. }
  2393. /* checking MGSTAT0 non-correctable error */
  2394. else if (registerValue & FTFx_FSTAT_MGSTAT0_MASK)
  2395. {
  2396. return kStatus_FLASH_CommandFailure;
  2397. }
  2398. else
  2399. {
  2400. return kStatus_FLASH_Success;
  2401. }
  2402. }
  2403. #if FLASH_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE
  2404. /*!
  2405. * @brief Copy PIC of flash_common_bit_operation() to RAM
  2406. *
  2407. */
  2408. static void copy_flash_common_bit_operation(uint32_t *flashCommonBitOperation)
  2409. {
  2410. assert(sizeof(s_flashCommonBitOperationFunctionCode) <= (kFLASH_ExecuteInRamFunctionMaxSizeInWords * 4));
  2411. /* Since the value of ARM function pointer is always odd, but the real start address
  2412. * of function memory should be even, that's why +1 operation exist. */
  2413. memcpy((void *)flashCommonBitOperation, (void *)s_flashCommonBitOperationFunctionCode,
  2414. sizeof(s_flashCommonBitOperationFunctionCode));
  2415. callFlashCommonBitOperation = (void (*)(FTFx_REG32_ACCESS_TYPE base, uint32_t bitMask, uint32_t bitShift,
  2416. uint32_t bitValue))((uint32_t)flashCommonBitOperation + 1);
  2417. }
  2418. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT && FLASH_IS_CACHE_INVALIDATION_AVAILABLE */
  2419. #if FLASH_CACHE_IS_CONTROLLED_BY_MCM
  2420. /*! @brief Performs the cache clear to the flash by MCM.*/
  2421. void mcm_flash_cache_clear(void)
  2422. {
  2423. FTFx_REG32_ACCESS_TYPE regBase;
  2424. #if defined(BL_TARGET_ROM) && defined(MCM0_CACHE_REG) && defined(MCM1_CACHE_REG) && \
  2425. defined(FSL_FEATURE_FLASH_CURRENT_CORE_ID)
  2426. {
  2427. uint16_t armPartNumber = (uint16_t)((SCB->CPUID & SCB_CPUID_PARTNO_Msk) >> SCB_CPUID_PARTNO_Pos);
  2428. uint32_t cortexVersion = __CORTEX_M;
  2429. uint32_t coreId = FSL_FEATURE_FLASH_CURRENT_CORE_ID;
  2430. #if (__CORTEX_M <= 7)
  2431. /* Note: Below code only apply to dual core device (such K3S) */
  2432. if (s_armCorePartNumberArray[cortexVersion] != armPartNumber)
  2433. {
  2434. coreId ^= 0x1;
  2435. }
  2436. regBase = s_mcmModuleAccessTypeArray[coreId];
  2437. #else
  2438. #error "Inapplicable ARM Cortext Version!"
  2439. #endif
  2440. }
  2441. #elif defined(MCM0_CACHE_REG)
  2442. regBase = (FTFx_REG32_ACCESS_TYPE)&MCM0_CACHE_REG;
  2443. #elif defined(MCM1_CACHE_REG)
  2444. regBase = (FTFx_REG32_ACCESS_TYPE)&MCM1_CACHE_REG;
  2445. #endif
  2446. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2447. callFlashCommonBitOperation(regBase, MCM_CACHE_CLEAR_MASK, MCM_CACHE_CLEAR_SHIFT, 1U);
  2448. #else /* !FLASH_DRIVER_IS_FLASH_RESIDENT */
  2449. *regBase |= MCM_CACHE_CLEAR_MASK;
  2450. /* Memory barriers for good measure.
  2451. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  2452. __ISB();
  2453. __DSB();
  2454. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2455. }
  2456. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_MCM */
  2457. #if FLASH_CACHE_IS_CONTROLLED_BY_FMC
  2458. /*! @brief Performs the cache clear to the flash by FMC.*/
  2459. void fmc_flash_cache_clear(void)
  2460. {
  2461. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2462. FTFx_REG32_ACCESS_TYPE regBase = (FTFx_REG32_ACCESS_TYPE)0;
  2463. #if defined(FMC_PFB01CR_CINV_WAY_MASK)
  2464. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB01CR;
  2465. callFlashCommonBitOperation(regBase, FMC_PFB01CR_CINV_WAY_MASK, FMC_PFB01CR_CINV_WAY_SHIFT, 0xFU);
  2466. #else
  2467. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB0CR;
  2468. callFlashCommonBitOperation(regBase, FMC_PFB0CR_CINV_WAY_MASK, FMC_PFB0CR_CINV_WAY_SHIFT, 0xFU);
  2469. #endif
  2470. #else /* !FLASH_DRIVER_IS_FLASH_RESIDENT */
  2471. #if defined(FMC_PFB01CR_CINV_WAY_MASK)
  2472. FMC->PFB01CR = (FMC->PFB01CR & ~FMC_PFB01CR_CINV_WAY_MASK) | FMC_PFB01CR_CINV_WAY(~0);
  2473. #else
  2474. FMC->PFB0CR = (FMC->PFB0CR & ~FMC_PFB0CR_CINV_WAY_MASK) | FMC_PFB0CR_CINV_WAY(~0);
  2475. #endif
  2476. /* Memory barriers for good measure.
  2477. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  2478. __ISB();
  2479. __DSB();
  2480. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2481. }
  2482. #endif /* FLASH_CACHE_IS_CONTROLLED_BY_FMC */
  2483. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  2484. /*! @brief Performs the prefetch speculation buffer clear to the flash by MSCM.*/
  2485. void mscm_flash_prefetch_speculation_enable(uint32_t flashIndex, bool enable)
  2486. {
  2487. uint8_t setValue;
  2488. if (enable)
  2489. {
  2490. setValue = 0x0U;
  2491. }
  2492. else
  2493. {
  2494. setValue = 0x3U;
  2495. }
  2496. /* The OCMDR[0] is always used to prefetch main Pflash*/
  2497. /* For device with FlexNVM support, the OCMDR[1] is used to prefetch Dflash.
  2498. * For device with secondary flash support, the OCMDR[1] is used to prefetch secondary Pflash. */
  2499. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2500. switch (flashIndex)
  2501. {
  2502. #if FLASH_SSD_IS_FLEXNVM_ENABLED || FLASH_SSD_IS_SECONDARY_FLASH_ENABLED
  2503. case kFLASH_MemoryIndexSecondaryFlash:
  2504. callFlashCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR1_REG, MSCM_SPECULATION_DISABLE_MASK,
  2505. MSCM_SPECULATION_DISABLE_SHIFT, setValue);
  2506. break;
  2507. #endif
  2508. case kFLASH_MemoryIndexPrimaryFlash:
  2509. default:
  2510. callFlashCommonBitOperation((FTFx_REG32_ACCESS_TYPE)&MSCM_OCMDR0_REG, MSCM_SPECULATION_DISABLE_MASK,
  2511. MSCM_SPECULATION_DISABLE_SHIFT, setValue);
  2512. break;
  2513. }
  2514. #else /* !FLASH_DRIVER_IS_FLASH_RESIDENT */
  2515. switch (flashIndex)
  2516. {
  2517. #if FLASH_SSD_IS_FLEXNVM_ENABLED || FLASH_SSD_IS_SECONDARY_FLASH_ENABLED
  2518. case kFLASH_MemoryIndexSecondaryFlash:
  2519. MSCM_OCMDR1_REG = (MSCM_OCMDR1_REG & (~MSCM_SPECULATION_DISABLE_MASK)) | MSCM_SPECULATION_DISABLE(setValue);
  2520. /* Each cahce clear instaruction should be followed by below code*/
  2521. __ISB();
  2522. __DSB();
  2523. break;
  2524. #endif
  2525. case kFLASH_MemoryIndexPrimaryFlash:
  2526. default:
  2527. MSCM_OCMDR0_REG = (MSCM_OCMDR0_REG & (~MSCM_SPECULATION_DISABLE_MASK)) | MSCM_SPECULATION_DISABLE(setValue);
  2528. /* Memory barriers for good measure.
  2529. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  2530. __ISB();
  2531. __DSB();
  2532. break;
  2533. }
  2534. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2535. }
  2536. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM */
  2537. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  2538. /*! @brief Performs the prefetch speculation buffer clear to the flash by FMC.*/
  2539. void fmc_flash_prefetch_speculation_clear(void)
  2540. {
  2541. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2542. FTFx_REG32_ACCESS_TYPE regBase = (FTFx_REG32_ACCESS_TYPE)0;
  2543. #if defined(FMC_PFB01CR_S_INV_MASK)
  2544. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB01CR;
  2545. callFlashCommonBitOperation(regBase, FMC_PFB01CR_S_INV_MASK, FMC_PFB01CR_S_INV_SHIFT, 1U);
  2546. #elif defined(FMC_PFB01CR_S_B_INV_MASK)
  2547. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB01CR;
  2548. callFlashCommonBitOperation(regBase, FMC_PFB01CR_S_B_INV_MASK, FMC_PFB01CR_S_B_INV_SHIFT, 1U);
  2549. #elif defined(FMC_PFB0CR_S_INV_MASK)
  2550. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB0CR;
  2551. callFlashCommonBitOperation(regBase, FMC_PFB0CR_S_INV_MASK, FMC_PFB0CR_S_INV_SHIFT, 1U);
  2552. #elif defined(FMC_PFB0CR_S_B_INV_MASK)
  2553. regBase = (FTFx_REG32_ACCESS_TYPE)&FMC->PFB0CR;
  2554. callFlashCommonBitOperation(regBase, FMC_PFB0CR_S_B_INV_MASK, FMC_PFB0CR_S_B_INV_SHIFT, 1U);
  2555. #endif
  2556. #else /* !FLASH_DRIVER_IS_FLASH_RESIDENT */
  2557. #if defined(FMC_PFB01CR_S_INV_MASK)
  2558. FMC->PFB01CR |= FMC_PFB01CR_S_INV_MASK;
  2559. #elif defined(FMC_PFB01CR_S_B_INV_MASK)
  2560. FMC->PFB01CR |= FMC_PFB01CR_S_B_INV_MASK;
  2561. #elif defined(FMC_PFB0CR_S_INV_MASK)
  2562. FMC->PFB0CR |= FMC_PFB0CR_S_INV_MASK;
  2563. #elif defined(FMC_PFB0CR_S_B_INV_MASK)
  2564. FMC->PFB0CR |= FMC_PFB0CR_S_B_INV_MASK;
  2565. #endif
  2566. /* Memory barriers for good measure.
  2567. * All Cache, Branch predictor and TLB maintenance operations before this instruction complete */
  2568. __ISB();
  2569. __DSB();
  2570. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2571. }
  2572. #endif /* FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC */
  2573. /*!
  2574. * @brief Flash Cache Clear
  2575. *
  2576. * This function is used to perform the cache and prefetch speculation clear to the flash.
  2577. */
  2578. void flash_cache_clear(flash_config_t *config)
  2579. {
  2580. flash_cache_clear_process(config, kFLASH_CacheClearProcessPost);
  2581. }
  2582. /*!
  2583. * @brief Flash Cache Clear Process
  2584. *
  2585. * This function is used to perform the cache and prefetch speculation clear process to the flash.
  2586. */
  2587. static void flash_cache_clear_process(flash_config_t *config, flash_cache_clear_process_t process)
  2588. {
  2589. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2590. status_t returnCode = flash_check_execute_in_ram_function_info(config);
  2591. if (kStatus_FLASH_Success != returnCode)
  2592. {
  2593. return;
  2594. }
  2595. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2596. /* We pass the ftfx register address as a parameter to flash_common_bit_operation() instead of using
  2597. * pre-processed MACROs or a global variable in flash_common_bit_operation()
  2598. * to make sure that flash_common_bit_operation() will be compiled into position-independent code (PIC). */
  2599. if (process == kFLASH_CacheClearProcessPost)
  2600. {
  2601. #if FLASH_CACHE_IS_CONTROLLED_BY_MCM
  2602. mcm_flash_cache_clear();
  2603. #endif
  2604. #if FLASH_CACHE_IS_CONTROLLED_BY_FMC
  2605. fmc_flash_cache_clear();
  2606. #endif
  2607. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  2608. mscm_flash_prefetch_speculation_enable(config->FlashMemoryIndex, true);
  2609. #endif
  2610. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_FMC
  2611. fmc_flash_prefetch_speculation_clear();
  2612. #endif
  2613. }
  2614. if (process == kFLASH_CacheClearProcessPre)
  2615. {
  2616. #if FLASH_PREFETCH_SPECULATION_IS_CONTROLLED_BY_MSCM
  2617. mscm_flash_prefetch_speculation_enable(config->FlashMemoryIndex, false);
  2618. #endif
  2619. }
  2620. }
  2621. #if FLASH_DRIVER_IS_FLASH_RESIDENT
  2622. /*! @brief Check whether flash execute-in-ram functions are ready */
  2623. static status_t flash_check_execute_in_ram_function_info(flash_config_t *config)
  2624. {
  2625. flash_execute_in_ram_function_config_t *flashExecuteInRamFunctionInfo;
  2626. if (config == NULL)
  2627. {
  2628. return kStatus_FLASH_InvalidArgument;
  2629. }
  2630. flashExecuteInRamFunctionInfo = (flash_execute_in_ram_function_config_t *)config->flashExecuteInRamFunctionInfo;
  2631. if ((config->flashExecuteInRamFunctionInfo) &&
  2632. (kFLASH_ExecuteInRamFunctionTotalNum == flashExecuteInRamFunctionInfo->activeFunctionCount))
  2633. {
  2634. return kStatus_FLASH_Success;
  2635. }
  2636. return kStatus_FLASH_ExecuteInRamFunctionNotReady;
  2637. }
  2638. #endif /* FLASH_DRIVER_IS_FLASH_RESIDENT */
  2639. /*! @brief Validates the range and alignment of the given address range.*/
  2640. static status_t flash_check_range(flash_config_t *config,
  2641. uint32_t startAddress,
  2642. uint32_t lengthInBytes,
  2643. uint32_t alignmentBaseline)
  2644. {
  2645. if (config == NULL)
  2646. {
  2647. return kStatus_FLASH_InvalidArgument;
  2648. }
  2649. /* Verify the start and length are alignmentBaseline aligned. */
  2650. if ((startAddress & (alignmentBaseline - 1)) || (lengthInBytes & (alignmentBaseline - 1)))
  2651. {
  2652. return kStatus_FLASH_AlignmentError;
  2653. }
  2654. /* check for valid range of the target addresses */
  2655. if (
  2656. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2657. ((startAddress >= config->DFlashBlockBase) &&
  2658. ((startAddress + lengthInBytes) <= (config->DFlashBlockBase + config->DFlashTotalSize))) ||
  2659. #endif
  2660. ((startAddress >= config->PFlashBlockBase) &&
  2661. ((startAddress + lengthInBytes) <= (config->PFlashBlockBase + config->PFlashTotalSize))))
  2662. {
  2663. return kStatus_FLASH_Success;
  2664. }
  2665. return kStatus_FLASH_AddressError;
  2666. }
  2667. /*! @brief Gets the right address, sector and block size of current flash type which is indicated by address.*/
  2668. static status_t flash_get_matched_operation_info(flash_config_t *config,
  2669. uint32_t address,
  2670. flash_operation_config_t *info)
  2671. {
  2672. if ((config == NULL) || (info == NULL))
  2673. {
  2674. return kStatus_FLASH_InvalidArgument;
  2675. }
  2676. /* Clean up info Structure*/
  2677. memset(info, 0, sizeof(flash_operation_config_t));
  2678. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2679. if ((address >= config->DFlashBlockBase) && (address <= (config->DFlashBlockBase + config->DFlashTotalSize)))
  2680. {
  2681. /* When required by the command, address bit 23 selects between program flash memory
  2682. * (=0) and data flash memory (=1).*/
  2683. info->convertedAddress = address - config->DFlashBlockBase + 0x800000U;
  2684. info->activeSectorSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_SECTOR_SIZE;
  2685. info->activeBlockSize = config->DFlashTotalSize / FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_COUNT;
  2686. info->blockWriteUnitSize = FSL_FEATURE_FLASH_FLEX_NVM_BLOCK_WRITE_UNIT_SIZE;
  2687. info->sectorCmdAddressAligment = FSL_FEATURE_FLASH_FLEX_NVM_SECTOR_CMD_ADDRESS_ALIGMENT;
  2688. info->sectionCmdAddressAligment = FSL_FEATURE_FLASH_FLEX_NVM_SECTION_CMD_ADDRESS_ALIGMENT;
  2689. info->resourceCmdAddressAligment = FSL_FEATURE_FLASH_FLEX_NVM_RESOURCE_CMD_ADDRESS_ALIGMENT;
  2690. info->checkCmdAddressAligment = FSL_FEATURE_FLASH_FLEX_NVM_CHECK_CMD_ADDRESS_ALIGMENT;
  2691. }
  2692. else
  2693. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  2694. {
  2695. info->convertedAddress = address - config->PFlashBlockBase;
  2696. info->activeSectorSize = config->PFlashSectorSize;
  2697. info->activeBlockSize = config->PFlashTotalSize / config->PFlashBlockCount;
  2698. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED
  2699. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  2700. {
  2701. #if FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER || FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER
  2702. /* When required by the command, address bit 23 selects between main flash memory
  2703. * (=0) and secondary flash memory (=1).*/
  2704. info->convertedAddress += 0x800000U;
  2705. #endif
  2706. info->blockWriteUnitSize = SECONDARY_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE;
  2707. }
  2708. else
  2709. #endif /* FLASH_SSD_IS_SECONDARY_FLASH_ENABLED */
  2710. {
  2711. info->blockWriteUnitSize = MAIN_FLASH_FEATURE_PFLASH_BLOCK_WRITE_UNIT_SIZE;
  2712. }
  2713. info->sectorCmdAddressAligment = FSL_FEATURE_FLASH_PFLASH_SECTOR_CMD_ADDRESS_ALIGMENT;
  2714. info->sectionCmdAddressAligment = FSL_FEATURE_FLASH_PFLASH_SECTION_CMD_ADDRESS_ALIGMENT;
  2715. info->resourceCmdAddressAligment = FSL_FEATURE_FLASH_PFLASH_RESOURCE_CMD_ADDRESS_ALIGMENT;
  2716. info->checkCmdAddressAligment = FSL_FEATURE_FLASH_PFLASH_CHECK_CMD_ADDRESS_ALIGMENT;
  2717. }
  2718. return kStatus_FLASH_Success;
  2719. }
  2720. /*! @brief Validates the given user key for flash erase APIs.*/
  2721. static status_t flash_check_user_key(uint32_t key)
  2722. {
  2723. /* Validate the user key */
  2724. if (key != kFLASH_ApiEraseKey)
  2725. {
  2726. return kStatus_FLASH_EraseKeyError;
  2727. }
  2728. return kStatus_FLASH_Success;
  2729. }
  2730. #if FLASH_SSD_IS_FLEXNVM_ENABLED
  2731. /*! @brief Updates FlexNVM memory partition status according to data flash 0 IFR.*/
  2732. static status_t flash_update_flexnvm_memory_partition_status(flash_config_t *config)
  2733. {
  2734. struct
  2735. {
  2736. uint32_t reserved0;
  2737. uint8_t FlexNVMPartitionCode;
  2738. uint8_t EEPROMDataSetSize;
  2739. uint16_t reserved1;
  2740. } dataIFRReadOut;
  2741. status_t returnCode;
  2742. if (config == NULL)
  2743. {
  2744. return kStatus_FLASH_InvalidArgument;
  2745. }
  2746. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  2747. /* Get FlexNVM memory partition info from data flash IFR */
  2748. returnCode = FLASH_ReadResource(config, DFLASH_IFR_READRESOURCE_START_ADDRESS, (uint32_t *)&dataIFRReadOut,
  2749. sizeof(dataIFRReadOut), kFLASH_ResourceOptionFlashIfr);
  2750. if (returnCode != kStatus_FLASH_Success)
  2751. {
  2752. return kStatus_FLASH_PartitionStatusUpdateFailure;
  2753. }
  2754. #else
  2755. #error "Cannot get FlexNVM memory partition info"
  2756. #endif
  2757. /* Fill out partitioned EEPROM size */
  2758. dataIFRReadOut.EEPROMDataSetSize &= 0x0FU;
  2759. switch (dataIFRReadOut.EEPROMDataSetSize)
  2760. {
  2761. case 0x00U:
  2762. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0000;
  2763. break;
  2764. case 0x01U:
  2765. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0001;
  2766. break;
  2767. case 0x02U:
  2768. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0010;
  2769. break;
  2770. case 0x03U:
  2771. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0011;
  2772. break;
  2773. case 0x04U:
  2774. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0100;
  2775. break;
  2776. case 0x05U:
  2777. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0101;
  2778. break;
  2779. case 0x06U:
  2780. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0110;
  2781. break;
  2782. case 0x07U:
  2783. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_0111;
  2784. break;
  2785. case 0x08U:
  2786. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1000;
  2787. break;
  2788. case 0x09U:
  2789. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1001;
  2790. break;
  2791. case 0x0AU:
  2792. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1010;
  2793. break;
  2794. case 0x0BU:
  2795. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1011;
  2796. break;
  2797. case 0x0CU:
  2798. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1100;
  2799. break;
  2800. case 0x0DU:
  2801. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1101;
  2802. break;
  2803. case 0x0EU:
  2804. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1110;
  2805. break;
  2806. case 0x0FU:
  2807. config->EEpromTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_1111;
  2808. break;
  2809. default:
  2810. config->EEpromTotalSize = FLEX_NVM_EEPROM_SIZE_FOR_EEESIZE_RESERVED;
  2811. break;
  2812. }
  2813. /* Fill out partitioned DFlash size */
  2814. dataIFRReadOut.FlexNVMPartitionCode &= 0x0FU;
  2815. switch (dataIFRReadOut.FlexNVMPartitionCode)
  2816. {
  2817. case 0x00U:
  2818. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0000 != 0xFFFFFFFF)
  2819. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0000;
  2820. #else
  2821. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2822. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0000 */
  2823. break;
  2824. case 0x01U:
  2825. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0001 != 0xFFFFFFFF)
  2826. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0001;
  2827. #else
  2828. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2829. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0001 */
  2830. break;
  2831. case 0x02U:
  2832. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0010 != 0xFFFFFFFF)
  2833. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0010;
  2834. #else
  2835. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2836. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0010 */
  2837. break;
  2838. case 0x03U:
  2839. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0011 != 0xFFFFFFFF)
  2840. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0011;
  2841. #else
  2842. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2843. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0011 */
  2844. break;
  2845. case 0x04U:
  2846. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0100 != 0xFFFFFFFF)
  2847. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0100;
  2848. #else
  2849. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2850. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0100 */
  2851. break;
  2852. case 0x05U:
  2853. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0101 != 0xFFFFFFFF)
  2854. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0101;
  2855. #else
  2856. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2857. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0101 */
  2858. break;
  2859. case 0x06U:
  2860. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0110 != 0xFFFFFFFF)
  2861. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0110;
  2862. #else
  2863. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2864. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0110 */
  2865. break;
  2866. case 0x07U:
  2867. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0111 != 0xFFFFFFFF)
  2868. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0111;
  2869. #else
  2870. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2871. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_0111 */
  2872. break;
  2873. case 0x08U:
  2874. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1000 != 0xFFFFFFFF)
  2875. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1000;
  2876. #else
  2877. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2878. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1000 */
  2879. break;
  2880. case 0x09U:
  2881. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1001 != 0xFFFFFFFF)
  2882. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1001;
  2883. #else
  2884. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2885. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1001 */
  2886. break;
  2887. case 0x0AU:
  2888. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1010 != 0xFFFFFFFF)
  2889. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1010;
  2890. #else
  2891. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2892. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1010 */
  2893. break;
  2894. case 0x0BU:
  2895. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1011 != 0xFFFFFFFF)
  2896. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1011;
  2897. #else
  2898. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2899. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1011 */
  2900. break;
  2901. case 0x0CU:
  2902. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1100 != 0xFFFFFFFF)
  2903. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1100;
  2904. #else
  2905. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2906. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1100 */
  2907. break;
  2908. case 0x0DU:
  2909. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1101 != 0xFFFFFFFF)
  2910. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1101;
  2911. #else
  2912. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2913. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1101 */
  2914. break;
  2915. case 0x0EU:
  2916. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1110 != 0xFFFFFFFF)
  2917. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1110;
  2918. #else
  2919. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2920. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1110 */
  2921. break;
  2922. case 0x0FU:
  2923. #if (FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1111 != 0xFFFFFFFF)
  2924. config->DFlashTotalSize = FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1111;
  2925. #else
  2926. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2927. #endif /* FSL_FEATURE_FLASH_FLEX_NVM_DFLASH_SIZE_FOR_DEPART_1111 */
  2928. break;
  2929. default:
  2930. config->DFlashTotalSize = FLEX_NVM_DFLASH_SIZE_FOR_DEPART_RESERVED;
  2931. break;
  2932. }
  2933. return kStatus_FLASH_Success;
  2934. }
  2935. #endif /* FLASH_SSD_IS_FLEXNVM_ENABLED */
  2936. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  2937. /*! @brief Validates the range of the given resource address.*/
  2938. static status_t flash_check_resource_range(uint32_t start,
  2939. uint32_t lengthInBytes,
  2940. uint32_t alignmentBaseline,
  2941. flash_read_resource_option_t option)
  2942. {
  2943. status_t status;
  2944. uint32_t maxReadbleAddress;
  2945. if ((start & (alignmentBaseline - 1)) || (lengthInBytes & (alignmentBaseline - 1)))
  2946. {
  2947. return kStatus_FLASH_AlignmentError;
  2948. }
  2949. status = kStatus_FLASH_Success;
  2950. maxReadbleAddress = start + lengthInBytes - 1;
  2951. if (option == kFLASH_ResourceOptionVersionId)
  2952. {
  2953. if ((start != kFLASH_ResourceRangeVersionIdStart) ||
  2954. ((start + lengthInBytes - 1) != kFLASH_ResourceRangeVersionIdEnd))
  2955. {
  2956. status = kStatus_FLASH_InvalidArgument;
  2957. }
  2958. }
  2959. else if (option == kFLASH_ResourceOptionFlashIfr)
  2960. {
  2961. if (maxReadbleAddress < kFLASH_ResourceRangePflashIfrSizeInBytes)
  2962. {
  2963. }
  2964. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  2965. else if ((start >= kFLASH_ResourceRangePflashSwapIfrStart) &&
  2966. (maxReadbleAddress <= kFLASH_ResourceRangePflashSwapIfrEnd))
  2967. {
  2968. }
  2969. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */
  2970. else if ((start >= kFLASH_ResourceRangeDflashIfrStart) &&
  2971. (maxReadbleAddress <= kFLASH_ResourceRangeDflashIfrEnd))
  2972. {
  2973. }
  2974. else
  2975. {
  2976. status = kStatus_FLASH_InvalidArgument;
  2977. }
  2978. }
  2979. else
  2980. {
  2981. status = kStatus_FLASH_InvalidArgument;
  2982. }
  2983. return status;
  2984. }
  2985. #endif /* FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD */
  2986. #if defined(FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD) && FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD
  2987. /*! @brief Validates the gived swap control option.*/
  2988. static status_t flash_check_swap_control_option(flash_swap_control_option_t option)
  2989. {
  2990. if ((option == kFLASH_SwapControlOptionIntializeSystem) || (option == kFLASH_SwapControlOptionSetInUpdateState) ||
  2991. (option == kFLASH_SwapControlOptionSetInCompleteState) || (option == kFLASH_SwapControlOptionReportStatus) ||
  2992. (option == kFLASH_SwapControlOptionDisableSystem))
  2993. {
  2994. return kStatus_FLASH_Success;
  2995. }
  2996. return kStatus_FLASH_InvalidArgument;
  2997. }
  2998. #endif /* FSL_FEATURE_FLASH_HAS_SWAP_CONTROL_CMD */
  2999. #if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
  3000. /*! @brief Validates the gived address to see if it is equal to swap indicator address in pflash swap IFR.*/
  3001. static status_t flash_validate_swap_indicator_address(flash_config_t *config, uint32_t address)
  3002. {
  3003. flash_swap_ifr_field_data_t flashSwapIfrFieldData;
  3004. uint32_t swapIndicatorAddress;
  3005. status_t returnCode;
  3006. #if defined(FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD) && FSL_FEATURE_FLASH_HAS_READ_RESOURCE_CMD
  3007. returnCode =
  3008. FLASH_ReadResource(config, kFLASH_ResourceRangePflashSwapIfrStart, flashSwapIfrFieldData.flashSwapIfrData,
  3009. sizeof(flashSwapIfrFieldData.flashSwapIfrData), kFLASH_ResourceOptionFlashIfr);
  3010. if (returnCode != kStatus_FLASH_Success)
  3011. {
  3012. return returnCode;
  3013. }
  3014. #else
  3015. {
  3016. /* From RM, the actual info are stored in FCCOB6,7 */
  3017. uint32_t returnValue[2];
  3018. returnCode = FLASH_ReadOnce(config, kFLASH_RecordIndexSwapAddr, returnValue, 4);
  3019. if (returnCode != kStatus_FLASH_Success)
  3020. {
  3021. return returnCode;
  3022. }
  3023. flashSwapIfrFieldData.flashSwapIfrField.swapIndicatorAddress = (uint16_t)returnValue[0];
  3024. returnCode = FLASH_ReadOnce(config, kFLASH_RecordIndexSwapEnable, returnValue, 4);
  3025. if (returnCode != kStatus_FLASH_Success)
  3026. {
  3027. return returnCode;
  3028. }
  3029. flashSwapIfrFieldData.flashSwapIfrField.swapEnableWord = (uint16_t)returnValue[0];
  3030. returnCode = FLASH_ReadOnce(config, kFLASH_RecordIndexSwapDisable, returnValue, 4);
  3031. if (returnCode != kStatus_FLASH_Success)
  3032. {
  3033. return returnCode;
  3034. }
  3035. flashSwapIfrFieldData.flashSwapIfrField.swapDisableWord = (uint16_t)returnValue[0];
  3036. }
  3037. #endif
  3038. /* The high bits value of Swap Indicator Address is stored in Program Flash Swap IFR Field,
  3039. * the low severval bit value of Swap Indicator Address is always 1'b0 */
  3040. swapIndicatorAddress = (uint32_t)flashSwapIfrFieldData.flashSwapIfrField.swapIndicatorAddress *
  3041. FSL_FEATURE_FLASH_PFLASH_SWAP_CONTROL_CMD_ADDRESS_ALIGMENT;
  3042. if (address != swapIndicatorAddress)
  3043. {
  3044. return kStatus_FLASH_SwapIndicatorAddressError;
  3045. }
  3046. return returnCode;
  3047. }
  3048. #endif /* FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP */
  3049. #if defined(FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD) && FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD
  3050. /*! @brief Validates the gived flexram function option.*/
  3051. static inline status_t flasn_check_flexram_function_option_range(flash_flexram_function_option_t option)
  3052. {
  3053. if ((option != kFLASH_FlexramFunctionOptionAvailableAsRam) &&
  3054. (option != kFLASH_FlexramFunctionOptionAvailableForEeprom))
  3055. {
  3056. return kStatus_FLASH_InvalidArgument;
  3057. }
  3058. return kStatus_FLASH_Success;
  3059. }
  3060. #endif /* FSL_FEATURE_FLASH_HAS_SET_FLEXRAM_FUNCTION_CMD */
  3061. /*! @brief Gets the flash protection information (region size, region count).*/
  3062. static status_t flash_get_protection_info(flash_config_t *config, flash_protection_config_t *info)
  3063. {
  3064. uint32_t pflashTotalSize;
  3065. if ((config == NULL) || (info == NULL))
  3066. {
  3067. return kStatus_FLASH_InvalidArgument;
  3068. }
  3069. /* Clean up info Structure*/
  3070. memset(info, 0, sizeof(flash_protection_config_t));
  3071. /* Note: KW40 has a secondary flash, but it doesn't have independent protection register*/
  3072. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && (!FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER)
  3073. pflashTotalSize = MAIN_FLASH_FEATURE_PFLASH_BLOCK_COUNT * MAIN_FLASH_FEATURE_PFLASH_BLOCK_SIZE +
  3074. FSL_FEATURE_FLASH_PFLASH_1_BLOCK_COUNT * FSL_FEATURE_FLASH_PFLASH_1_BLOCK_SIZE;
  3075. info->regionBase = MAIN_FLASH_FEATURE_PFLASH_START_ADDRESS;
  3076. #else
  3077. pflashTotalSize = config->PFlashTotalSize;
  3078. info->regionBase = config->PFlashBlockBase;
  3079. #endif
  3080. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_PROTECTION_REGISTER
  3081. if (config->FlashMemoryIndex == (uint8_t)kFLASH_MemoryIndexSecondaryFlash)
  3082. {
  3083. info->regionCount = SECONDARY_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT;
  3084. }
  3085. else
  3086. #endif
  3087. {
  3088. info->regionCount = MAIN_FLASH_FEATURE_PFLASH_PROTECTION_REGION_COUNT;
  3089. }
  3090. /* Calculate the size of the flash protection region
  3091. * If the flash density is > 32KB, then protection region is 1/32 of total flash density
  3092. * Else if flash density is < 32KB, then flash protection region is set to 1KB */
  3093. if (pflashTotalSize > info->regionCount * 1024)
  3094. {
  3095. info->regionSize = (pflashTotalSize) / info->regionCount;
  3096. }
  3097. else
  3098. {
  3099. info->regionSize = 1024;
  3100. }
  3101. return kStatus_FLASH_Success;
  3102. }
  3103. #if defined(FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL) && FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL
  3104. /*! @brief Gets the flash Execute-Only access information (Segment size, Segment count).*/
  3105. static status_t flash_get_access_info(flash_config_t *config, flash_access_config_t *info)
  3106. {
  3107. if ((config == NULL) || (info == NULL))
  3108. {
  3109. return kStatus_FLASH_InvalidArgument;
  3110. }
  3111. /* Clean up info Structure*/
  3112. memset(info, 0, sizeof(flash_access_config_t));
  3113. /* Note: KW40 has a secondary flash, but it doesn't have independent access register*/
  3114. #if FLASH_SSD_IS_SECONDARY_FLASH_ENABLED && (!FLASH_SSD_SECONDARY_FLASH_HAS_ITS_OWN_ACCESS_REGISTER)
  3115. info->SegmentBase = MAIN_FLASH_FEATURE_PFLASH_START_ADDRESS;
  3116. #else
  3117. info->SegmentBase = config->PFlashBlockBase;
  3118. #endif
  3119. info->SegmentSize = config->PFlashAccessSegmentSize;
  3120. info->SegmentCount = config->PFlashAccessSegmentCount;
  3121. return kStatus_FLASH_Success;
  3122. }
  3123. #endif /* FSL_FEATURE_FLASH_HAS_ACCESS_CONTROL */