am_hal_flash.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. //*****************************************************************************
  2. //
  3. // am_hal_flash.c
  4. //! @file
  5. //!
  6. //! @brief Functions for performing Flash operations.
  7. //!
  8. //! @addtogroup flash2 Flash
  9. //! @ingroup apollo2hal
  10. //!
  11. //! IMPORTANT: Interrupts are active during execution of all HAL flash
  12. //! functions. If an interrupt occurs during execution of a flash function
  13. //! that programs or erases flash or INFO space, errors will occur if the
  14. //! interrupt service routine (ISR) is located in on-chip flash.
  15. //! If interrupts are expected during execution of a flash function that
  16. //! programs or erases either flash or INFO space:
  17. //! - Interrupts must be disabled via a critical section handler prior to
  18. //! calling the flash function.
  19. //! - Alternatively, applicable ISRs must be located in non-flash address space
  20. //! (i.e. SRAM, off-chip ROM, etc.).
  21. //!
  22. //! @{
  23. //
  24. //*****************************************************************************
  25. //*****************************************************************************
  26. //
  27. // Copyright (c) 2017, Ambiq Micro
  28. // All rights reserved.
  29. //
  30. // Redistribution and use in source and binary forms, with or without
  31. // modification, are permitted provided that the following conditions are met:
  32. //
  33. // 1. Redistributions of source code must retain the above copyright notice,
  34. // this list of conditions and the following disclaimer.
  35. //
  36. // 2. Redistributions in binary form must reproduce the above copyright
  37. // notice, this list of conditions and the following disclaimer in the
  38. // documentation and/or other materials provided with the distribution.
  39. //
  40. // 3. Neither the name of the copyright holder nor the names of its
  41. // contributors may be used to endorse or promote products derived from this
  42. // software without specific prior written permission.
  43. //
  44. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  48. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. // POSSIBILITY OF SUCH DAMAGE.
  55. //
  56. // This is part of revision 1.2.9 of the AmbiqSuite Development Package.
  57. //
  58. //*****************************************************************************
  59. #include <stdint.h>
  60. #include <stdbool.h>
  61. #include "am_mcu_apollo.h"
  62. //
  63. // Look-up table
  64. //
  65. g_am_hal_flash_t g_am_hal_flash =
  66. {
  67. //
  68. // The basics.
  69. //
  70. // flash_mass_erase()
  71. ((int (*) (uint32_t, uint32_t)) 0x0800004d),
  72. // flash_page_erase()
  73. ((int (*) (uint32_t, uint32_t, uint32_t)) 0x08000051),
  74. // flash_program_main()
  75. ((int (*) (uint32_t, uint32_t *, uint32_t *, uint32_t)) 0x08000055),
  76. // flash_program_info()
  77. ((int (*) (uint32_t, uint32_t, uint32_t *, uint32_t, uint32_t)) 0x08000059),
  78. //
  79. // Non-blocking variants, but be careful these are not interrupt safe so
  80. // mask interrupts while these very long operations proceed.
  81. //
  82. // flash_mass_erase_nb()
  83. ((int (*)(uint32_t, uint32_t)) 0x0800006d),
  84. // flash_page_erase_nb()
  85. ((int (*)(uint32_t, uint32_t, uint32_t)) 0x08000071),
  86. // flash_nb_operation_complete()
  87. ((bool (*)(void)) 0x0800007d),
  88. //
  89. // Essentially these are recovery options.
  90. //
  91. // flash_erase_info()
  92. ((int (*)(uint32_t, uint32_t)) 0x08000081),
  93. // flash_erase_main_plus_info()
  94. ((int (*)(uint32_t, uint32_t)) 0x08000089),
  95. // flash_erase_main_plus_info_both_instances()
  96. ((int (*)(uint32_t)) 0x08000091),
  97. // flash_recovery()
  98. ((void (*)(uint32_t)) 0x08000099),
  99. //
  100. // Useful utilities.
  101. //
  102. // flash_util_read_word()
  103. ((uint32_t (*)(uint32_t*)) 0x08000075),
  104. // flash_util_write_word()
  105. ((void (*)(uint32_t*, uint32_t)) 0x08000079),
  106. // delay_cycles()
  107. ((void (*)(uint32_t)) 0x0800009d),
  108. //
  109. // The following functions pointers must never be called from user
  110. // programs. They are here primarily to document these entry points
  111. // which are usable from a debugger or debugger script.
  112. //
  113. // flash_program_main_sram()
  114. ((void (*) (void)) 0x0800005d),
  115. // flash_program_info_sram()
  116. ((void (*) (void)) 0x08000061),
  117. // flash_erase_main_pages_sram()
  118. ((void (*) (void)) 0x08000065),
  119. // flash_mass_erase_sram()
  120. ((void (*) (void)) 0x08000069),
  121. // flash_erase_info_sram()
  122. ((void (*)(void)) 0x08000085),
  123. // flash_erase_main_plus_info_sram()
  124. ((void (*)(void)) 0x0800008d)
  125. };
  126. //*****************************************************************************
  127. //
  128. //! @brief This function performs a mass erase on a flash instance.
  129. //!
  130. //! @param ui32Value - The flash program key.
  131. //! @param ui32FlashInst - The flash instance to erase.
  132. //!
  133. //! This function will erase the desired instance of flash.
  134. //!
  135. //! @note For Apollo2, each flash instance contains a maximum of 512KB.
  136. //!
  137. //! @note Interrupts are active during execution of this function. Any interrupt
  138. //! taken could cause execution errors. Please see the IMPORTANT note under
  139. //! Detailed Description above for more details.
  140. //!
  141. //! @return 0 for success, non-zero for failure.
  142. //
  143. //*****************************************************************************
  144. int
  145. am_hal_flash_mass_erase(uint32_t ui32Value, uint32_t ui32FlashInst)
  146. {
  147. return g_am_hal_flash.flash_mass_erase(ui32Value, ui32FlashInst);
  148. }
  149. //*****************************************************************************
  150. //
  151. //! @brief This function performs a page erase on a flash instance.
  152. //!
  153. //! @param ui32Value - The flash program key.
  154. //! @param ui32FlashInst - The flash instance to reference the page number with.
  155. //! @param ui32PageNum - The flash page relative to the specified instance.
  156. //!
  157. //! This function will erase the desired flash page in the desired instance of
  158. //! flash.
  159. //!
  160. //! @note For Apollo2, each flash page is 8KB (or AM_HAL_FLASH_PAGE_SIZE).
  161. //! Each flash instance contains a maximum of 64 pages (or
  162. //! AM_HAL_FLASH_INSTANCE_PAGES).
  163. //!
  164. //! @note When given an absolute flash address, a couple of helpful macros can
  165. //! be utilized when calling this function.
  166. //! For example:
  167. //! am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY,
  168. //! AM_HAL_FLASH_ADDR2INST(ui32Addr),
  169. //! AM_HAL_FLASH_ADDR2PAGE(ui32Addr) );
  170. //!
  171. //! @note Interrupts are active during execution of this function. Any interrupt
  172. //! taken could cause execution errors. Please see the IMPORTANT note under
  173. //! Detailed Description above for more details.
  174. //!
  175. //! @return 0 for success, non-zero for failure.
  176. //
  177. //*****************************************************************************
  178. int
  179. am_hal_flash_page_erase(uint32_t ui32Value, uint32_t ui32FlashInst,
  180. uint32_t ui32PageNum)
  181. {
  182. return g_am_hal_flash.flash_page_erase(ui32Value,
  183. ui32FlashInst,
  184. ui32PageNum);
  185. }
  186. //*****************************************************************************
  187. //
  188. //! @brief This programs up to N words of the Main array on one flash instance.
  189. //!
  190. //! @param ui32Value - The programming key, AM_HAL_FLASH_PROGRAM_KEY.
  191. //! @param pui32Src - Pointer to word aligned array of data to program into
  192. //! the flash instance.
  193. //! @param pui32Dst - Pointer to the word aligned flash location where
  194. //! programming of the flash instance is to begin.
  195. //! @param ui32NumWords - The number of words to be programmed.
  196. //!
  197. //! This function will program multiple words in main flash.
  198. //!
  199. //! @note Interrupts are active during execution of this function. Any interrupt
  200. //! taken could cause execution errors. Please see the IMPORTANT note under
  201. //! Detailed Description above for more details.
  202. //!
  203. //! @return 0 for success, non-zero for failure.
  204. //
  205. //*****************************************************************************
  206. int
  207. am_hal_flash_program_main(uint32_t ui32Value, uint32_t *pui32Src,
  208. uint32_t *pui32Dst, uint32_t ui32NumWords)
  209. {
  210. return g_am_hal_flash.flash_program_main(ui32Value, pui32Src,
  211. pui32Dst, ui32NumWords);
  212. }
  213. //*****************************************************************************
  214. //
  215. //! @brief This function programs multiple words in the customer INFO space.
  216. //!
  217. //! @param ui32Value - The customer INFO space key.
  218. //! @param ui32InfoInst - The INFO space instance, 0 or 1.
  219. //! @param *pui32Src - Pointer to word aligned array of data to program into
  220. //! the customer INFO space.
  221. //! @param ui32Offset - Word offset into customer INFO space (offset of 0 is
  222. //! the first word, 1 is second word, etc.).
  223. //! @param ui32NumWords - The number of words to be programmed, must not
  224. //! exceed AM_HAL_FLASH_INFO_SIZE/4.
  225. //!
  226. //! This function will program multiple words in the customer INFO space.
  227. //!
  228. //! @note Interrupts are active during execution of this function. Any interrupt
  229. //! taken could cause execution errors. Please see the IMPORTANT note under
  230. //! Detailed Description above for more details.
  231. //!
  232. //! @return 0 for success, non-zero for failure.
  233. //
  234. //*****************************************************************************
  235. int
  236. am_hal_flash_program_info(uint32_t ui32Value, uint32_t ui32InfoInst,
  237. uint32_t *pui32Src, uint32_t ui32Offset,
  238. uint32_t ui32NumWords)
  239. {
  240. return g_am_hal_flash.flash_program_info(ui32Value, 0, pui32Src,
  241. ui32Offset, ui32NumWords);
  242. }
  243. //*****************************************************************************
  244. //
  245. //! @brief This function erases an instance of the customer INFO space.
  246. //!
  247. //! @param ui32ProgramKey - The customer INFO space programming key
  248. //! (AM_HAL_FLASH_PROGRAM_KEY).
  249. //! @param ui32Inst - The flash instance, either 0 or 1.
  250. //!
  251. //! This function will erase the the customer INFO space of the specified
  252. //! instance.
  253. //!
  254. //! @note Interrupts are active during execution of this function. Any interrupt
  255. //! taken could cause execution errors. Please see the IMPORTANT note under
  256. //! Detailed Description above for more details.
  257. //!
  258. //! @return 0 for success, non-zero for failure.
  259. //
  260. //*****************************************************************************
  261. int
  262. am_hal_flash_erase_info(uint32_t ui32ProgramKey,
  263. uint32_t ui32Inst)
  264. {
  265. return g_am_hal_flash.flash_erase_info(ui32ProgramKey, ui32Inst);
  266. }
  267. //*****************************************************************************
  268. //
  269. //! @brief This function erases the main instance + the customer INFO space.
  270. //!
  271. //! @param ui32ProgramKey - The customer INFO space key.
  272. //! @param ui32Inst - The flash instance, either 0 or 1.
  273. //!
  274. //! This function will erase the main flash + the customer INFO space of the
  275. //! specified instance.
  276. //!
  277. //! @note Interrupts are active during execution of this function. Any interrupt
  278. //! taken could cause execution errors. Please see the IMPORTANT note under
  279. //! Detailed Description above for more details.
  280. //!
  281. //! @return 0 for success, non-zero for failure.
  282. //
  283. //*****************************************************************************
  284. int
  285. am_hal_flash_erase_main_plus_info(uint32_t ui32ProgramKey,
  286. uint32_t ui32Inst)
  287. {
  288. return g_am_hal_flash.flash_erase_main_plus_info(ui32ProgramKey,
  289. ui32Inst);
  290. }
  291. //*****************************************************************************
  292. //
  293. //! @brief This function erases the main flash + the customer INFO space.
  294. //!
  295. //! @param ui32ProgramKey - The customer INFO space key.
  296. //!
  297. //! This function will erase both instances the main flash + the
  298. //! customer INFO space.
  299. //!
  300. //! @note Interrupts are active during execution of this function. Any interrupt
  301. //! taken could cause execution errors. Please see the IMPORTANT note under
  302. //! Detailed Description above for more details.
  303. //!
  304. //! @return 0 for success, non-zero for failure.
  305. //
  306. //*****************************************************************************
  307. int
  308. am_hal_flash_erase_main_plus_info_both_instances(uint32_t ui32ProgramKey)
  309. {
  310. return g_am_hal_flash.flash_erase_main_plus_info_both_instances(
  311. ui32ProgramKey);
  312. }
  313. //*****************************************************************************
  314. //
  315. //! @brief This function erases both main flash instances + both customer INFO
  316. //! space instances.
  317. //!
  318. //! @param ui32RecoveryKey - The recovery key.
  319. //!
  320. //! This function erases both main instances and both customer INFOinstances
  321. //! even if the customer INFO space is programmed to not be erasable. This
  322. //! function completely erases the flash main and info instances and wipes the
  323. //! SRAM. Upon completion of the erasure operations, it does a POI (power on
  324. //! initialization) reset.
  325. //!
  326. //! @note Interrupts are active during execution of this function. Any interrupt
  327. //! taken could cause execution errors. Please see the IMPORTANT note under
  328. //! Detailed Description above for more details.
  329. //!
  330. //! @return Never Returns!!!
  331. //
  332. //*****************************************************************************
  333. void
  334. am_hal_flash_recovery(uint32_t ui32RecoveryKey)
  335. {
  336. g_am_hal_flash.flash_recovery(ui32RecoveryKey);
  337. }
  338. //*****************************************************************************
  339. //
  340. //! @brief Return ui32 value obtained from anywhere in D Code or System Bus
  341. //!
  342. //! @param ui32Address - return the value corresponding to this location.
  343. //!
  344. //! Use this function to read a value from various peripheral locations
  345. //! that must be read from code running external to flash.
  346. //!
  347. //! @return the value found
  348. //
  349. //*****************************************************************************
  350. uint32_t
  351. am_hal_flash_load_ui32(uint32_t ui32Address)
  352. {
  353. return g_am_hal_flash.flash_util_read_word((uint32_t*)ui32Address);
  354. }
  355. //*****************************************************************************
  356. //
  357. //! @brief Use the bootrom to write to a location in SRAM or the system bus.
  358. //!
  359. //! @param ui32Address - Store the data value corresponding to this location.
  360. //! @param ui32Data - 32-bit Data to be stored.
  361. //!
  362. //! Use this function to store a value to various peripheral or SRAM locations
  363. //! that can not be touched from code running in SRAM or FLASH. There is no
  364. //! known need for this function in Apollo2 at this time.
  365. //!
  366. //! @return None.
  367. //
  368. //*****************************************************************************
  369. void
  370. am_hal_flash_store_ui32(uint32_t ui32Address, uint32_t ui32Data)
  371. {
  372. g_am_hal_flash.flash_util_write_word((uint32_t*)ui32Address,
  373. ui32Data);
  374. }
  375. //*****************************************************************************
  376. //
  377. //! @brief Use the bootrom to implement a spin loop.
  378. //!
  379. //! @param ui32Iterations - Number of iterations to delay.
  380. //!
  381. //! Use this function to implement a CPU busy waiting spin loop without cache
  382. //! or delay uncertainties.
  383. //!
  384. //! Note that the ROM-based function executes at 3 cycles per iteration plus
  385. //! the regular function call, entry, and exit overhead.
  386. //! The call and return overhead, including the call to this function, is
  387. //! somewhere in the neighborhood of 14 cycles, or 4.7 iterations.
  388. //!
  389. //! Example:
  390. //! - MCU operating at 48MHz -> 20.83 ns / cycle
  391. //! - Therefore each iteration (once inside the bootrom function) will consume
  392. //! 62.5ns.
  393. //! - The total overhead (assuming 14 cycles) is 292ns.
  394. //! - For ui32Iterations=28: Total delay time = 0.292 + (0.0625 * 28) = 2.04us.
  395. //!
  396. //! The FLASH_CYCLES_US(n) macro can be used with am_hal_flash_delay() to
  397. //! get an approximate microsecond delay.
  398. //! e.g. For a 2us delay, use:
  399. //! am_hal_flash_delay( FLASH_CYCLES_US(2) );
  400. //!
  401. //! @note Interrupts are active during execution of this function. Therefore,
  402. //! any interrupt taken will affect the delay timing.
  403. //!
  404. //! @return None.
  405. //
  406. //*****************************************************************************
  407. void
  408. am_hal_flash_delay(uint32_t ui32Iterations)
  409. {
  410. g_am_hal_flash.delay_cycles(ui32Iterations);
  411. }
  412. //*****************************************************************************
  413. //
  414. //! @brief Static Helper Function to check customer info valid bits erasure.
  415. //!
  416. //! Use this function to test the state of the 128 valid bits at the beginning
  417. //! of customer info space. If these are all erased then return true.
  418. //!
  419. //! @return true if the customer info bits are currently erased.
  420. //
  421. //*****************************************************************************
  422. static bool
  423. customer_info_signature_erased(void)
  424. {
  425. uint32_t *pui32Signature = (uint32_t *) AM_HAL_FLASH_INFO_ADDR;
  426. return ( (pui32Signature[3] == 0xFFFFFFFF) &&
  427. (pui32Signature[2] == 0xFFFFFFFF) &&
  428. (pui32Signature[1] == 0xFFFFFFFF) &&
  429. (pui32Signature[0] == 0xFFFFFFFF) ) ? true : false;
  430. }
  431. //*****************************************************************************
  432. //
  433. //! @brief Static Helper Function to set customer info valid bits
  434. //!
  435. //! Use this function to set the state of the 128 valid bits at the beginning
  436. //! of customer info space. If these bits are not set correctly then the
  437. //! customer protection bits in the INFO space will not be honored by the
  438. //! hardware.
  439. //!
  440. //! @return Zero for success. Non-Zero for errors.
  441. //
  442. //*****************************************************************************
  443. static int
  444. customer_info_signature_set(void)
  445. {
  446. uint32_t ui32Valid[4];
  447. int iRC;
  448. //
  449. // If they are already set then we are done.
  450. //
  451. if ( am_hal_flash_customer_info_signature_check() )
  452. {
  453. return 0;
  454. }
  455. //
  456. // If they are not erased at this point we have an error.
  457. //
  458. if ( !customer_info_signature_erased() )
  459. {
  460. return (2 << 16);
  461. }
  462. //
  463. // OK they need to be set so do it.
  464. //
  465. ui32Valid[3] = AM_HAL_FLASH_INFO_SIGNATURE3;
  466. ui32Valid[2] = AM_HAL_FLASH_INFO_SIGNATURE2;
  467. ui32Valid[1] = AM_HAL_FLASH_INFO_SIGNATURE1;
  468. ui32Valid[0] = AM_HAL_FLASH_INFO_SIGNATURE0;
  469. iRC = g_am_hal_flash.flash_program_info(AM_HAL_FLASH_PROGRAM_KEY,
  470. 0, // instance
  471. ui32Valid, // source data
  472. 0, // offset
  473. 4); // number of words
  474. return iRC | ((iRC) ? (1 << 16) : 0);
  475. }
  476. //*****************************************************************************
  477. //
  478. //! @brief Check that the customer info bits are valid.
  479. //!
  480. //! Use this function to test the state of the 128 valid bits at the beginning
  481. //! of customer info space. If these are not set correctly then the customer
  482. //! protection bits in the INFO space will not be honored by the hardware.
  483. //!
  484. //! @return true if valid.
  485. //
  486. //*****************************************************************************
  487. bool
  488. am_hal_flash_customer_info_signature_check(void)
  489. {
  490. uint32_t *pui32Signature = (uint32_t *)AM_HAL_FLASH_INFO_ADDR;
  491. return ( (pui32Signature[3] == AM_HAL_FLASH_INFO_SIGNATURE3) &&
  492. (pui32Signature[2] == AM_HAL_FLASH_INFO_SIGNATURE2) &&
  493. (pui32Signature[1] == AM_HAL_FLASH_INFO_SIGNATURE1) &&
  494. (pui32Signature[0] == AM_HAL_FLASH_INFO_SIGNATURE0) );
  495. }
  496. //*****************************************************************************
  497. //
  498. //! @brief INFO signature set.
  499. //!
  500. //! Use this function to set the state of the 128 valid bits at the beginning
  501. //! of customer info space, if needed.
  502. //!
  503. //! @note Interrupts are active during execution of this function. Any interrupt
  504. //! taken could cause execution errors. Please see the IMPORTANT note under
  505. //! Detailed Description above for more details.
  506. //!
  507. //! @return Zero for success. Non-Zero for errors.
  508. //
  509. //*****************************************************************************
  510. bool
  511. am_hal_flash_info_signature_set(void)
  512. {
  513. //
  514. // Check and set signature.
  515. //
  516. return customer_info_signature_set() ? false : true;
  517. }
  518. //*****************************************************************************
  519. //
  520. //! @brief Disable FLASH INFO space.
  521. //!
  522. //! Use this function to set the state of the 128 valid bits at the beginning
  523. //! of customer info space, if needed. Then disable FLASH erasure.
  524. //!
  525. //! @note Interrupts are active during execution of this function. Any interrupt
  526. //! taken could cause execution errors. Please see the IMPORTANT note under
  527. //! Detailed Description above for more details.
  528. //!
  529. //! @return Zero for success. Non-Zero for errors.
  530. //
  531. //*****************************************************************************
  532. int32_t
  533. am_hal_flash_info_erase_disable(void)
  534. {
  535. int iRC;
  536. uint32_t ui32SecurityValue;
  537. //
  538. // Security protection only works if the signature data is correct.
  539. //
  540. iRC = customer_info_signature_set();
  541. if ( iRC )
  542. {
  543. return iRC;
  544. }
  545. //
  546. // Clear bit in INFO space to disable erasure.
  547. //
  548. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  549. ~AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M;
  550. //
  551. // Now write the word to the flash INFO space.
  552. //
  553. return g_am_hal_flash.flash_program_info(
  554. AM_HAL_FLASH_PROGRAM_KEY,
  555. 0, // instance
  556. &ui32SecurityValue, // source data
  557. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  558. 1 ); // number of words
  559. }
  560. //*****************************************************************************
  561. //
  562. //! @brief Check for Disabled FLASH INFO space.
  563. //!
  564. //! Use this function to determine whether FLASH INFO erasure is disabled.
  565. //!
  566. //! @return true if FLASH INFO erase is disabled, otherwise false.
  567. //
  568. //*****************************************************************************
  569. bool
  570. am_hal_flash_info_erase_disable_check(void)
  571. {
  572. //
  573. // If they are erased at this point then SRAM wipe can't be enabled.
  574. //
  575. if ( customer_info_signature_erased() )
  576. {
  577. return false;
  578. }
  579. //
  580. // If they are not valid at this point then SRAM wipe can't be enabled.
  581. //
  582. if ( !am_hal_flash_customer_info_signature_check() )
  583. {
  584. return false;
  585. }
  586. //
  587. // Looking good so far, now check the SRAM WIPE bit.
  588. //
  589. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  590. AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M ? false : true;
  591. }
  592. //*****************************************************************************
  593. //
  594. //! @brief Mask off 1 to 4 quadrants of FLASH INFO space for programming.
  595. //!
  596. //! Use this function to set the state of the 128 valid bits at the beginning
  597. //! of customer info space, if needed. Then and the mask bits with the INFO
  598. //! space programming disable bits.
  599. //!
  600. //! @param ui32Mask - A mask of the 4 quadrants of info space where
  601. //! bit0 = First quadrant (first 2KB).
  602. //! bit1 = Second quadrant (second 2KB).
  603. //! bit2 = Third quadrant (third 2KB).
  604. //! bit3 = Fourth quadrant (fourth 2KB).
  605. //!
  606. //! @note This function disables only, any quadrant already disabled is not
  607. //! reenabled. That is, any ui32Mask bits specified as 0 are essentially nops.
  608. //!
  609. //! @note Interrupts are active during execution of this function. Any interrupt
  610. //! taken could cause execution errors. Please see the IMPORTANT note under
  611. //! Detailed Description above for more details.
  612. //!
  613. //! @return Zero for success. Non-Zero for errors.
  614. //
  615. //*****************************************************************************
  616. int32_t
  617. am_hal_flash_info_program_disable(uint32_t ui32Mask)
  618. {
  619. int iRC;
  620. uint32_t ui32SecurityValue;
  621. //
  622. // Security protection only works if the signature data is correct.
  623. //
  624. iRC = customer_info_signature_set();
  625. if ( iRC )
  626. {
  627. return iRC;
  628. }
  629. //
  630. // Make sure we have a valid mask and get the mask into the correct position.
  631. //
  632. ui32Mask <<= AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S;
  633. ui32Mask &= AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M;
  634. //
  635. // The security bit set to 1 enables programming, 0 disables programming.
  636. //
  637. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) & ~ui32Mask;
  638. //
  639. // Now write the word to the flash INFO space.
  640. //
  641. return g_am_hal_flash.flash_program_info(
  642. AM_HAL_FLASH_PROGRAM_KEY,
  643. 0, // instance
  644. &ui32SecurityValue, // source data
  645. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  646. 1 ); // number of words
  647. }
  648. //*****************************************************************************
  649. //
  650. //! @brief Return a mask specifying which quadrants of customer INFO space have
  651. //! been disabled for programming.
  652. //!
  653. //! Use this function to determine whether programming of customer INFO space
  654. //! has been disabled.
  655. //!
  656. //! @return A 4-bit mask of the disabled quadrants.
  657. //! 0xFFFFFFFF indicates an error.
  658. //! 0x0 indicates all customer INFO space programming is enabled.
  659. //! 0xF indicates all customer INFO space programming is disabled.
  660. //! bit0 indicates the first customer INFO space is disabled for programming.
  661. //! bit1 indicates the second customer INFO space is disabled for programming.
  662. //! bit2 indicates the third customer INFO space is disabled for programming.
  663. //! bit3 indicates the fourth customer INFO space is disabled for programming.
  664. //
  665. //*****************************************************************************
  666. uint32_t
  667. am_hal_flash_info_program_disable_get(void)
  668. {
  669. //
  670. // If they are erased at this point then SRAM wipe can't be enabled.
  671. //
  672. if ( customer_info_signature_erased() )
  673. {
  674. return 0xFFFFFFFF;
  675. }
  676. //
  677. // If not valid at this point, then INFO programming can't be enabled.
  678. //
  679. if ( !am_hal_flash_customer_info_signature_check() )
  680. {
  681. return 0xFFFFFFFF;
  682. }
  683. //
  684. // Looking good so far, now return a mask of the disabled bits.
  685. //
  686. return ((AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  687. AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M) ^
  688. AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M) >>
  689. AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S;
  690. }
  691. //*****************************************************************************
  692. //
  693. //! @brief Enable FLASH debugger protection (FLASH gets wiped if a debugger is
  694. //! connected).
  695. //!
  696. //! Use this function to set the state of the 128 valid bits at the beginning
  697. //! of customer info space, if needed. Then set the FLASH wipe bit to zero.
  698. //!
  699. //! @note Interrupts are active during execution of this function. Any interrupt
  700. //! taken could cause execution errors. Please see the IMPORTANT note under
  701. //! Detailed Description above for more details.
  702. //!
  703. //! @return Zero for success. Non-Zero for errors.
  704. //
  705. //*****************************************************************************
  706. int32_t
  707. am_hal_flash_wipe_flash_enable(void)
  708. {
  709. int iRC;
  710. uint32_t ui32SecurityValue;
  711. //
  712. // Security protection only works if the signature data is correct.
  713. //
  714. iRC = customer_info_signature_set();
  715. if ( iRC )
  716. {
  717. return iRC;
  718. }
  719. //
  720. // Clear the FLASH Wipe bit.
  721. //
  722. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  723. ~AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M;
  724. //
  725. // Now write the word to the flash INFO space.
  726. //
  727. return g_am_hal_flash.flash_program_info(
  728. AM_HAL_FLASH_PROGRAM_KEY,
  729. 0, // instance
  730. &ui32SecurityValue, // source data
  731. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  732. 1 ); // number of words
  733. }
  734. //*****************************************************************************
  735. //
  736. //! @brief check for FLASH wipe protection enabled.
  737. //!
  738. //! Use this function to determine if FLASH wipe protection is enabled.
  739. //!
  740. //! @return true if FLASH wipe protection is enabled, otherwise false.
  741. //
  742. //*****************************************************************************
  743. bool
  744. am_hal_flash_wipe_flash_enable_check(void)
  745. {
  746. //
  747. // If they are erased at this point then flash wipe can't be enabled.
  748. //
  749. if ( customer_info_signature_erased() )
  750. {
  751. return false;
  752. }
  753. //
  754. // If they are not valid at this point then flash wipe can't be enabled.
  755. //
  756. if ( !am_hal_flash_customer_info_signature_check() )
  757. {
  758. return false;
  759. }
  760. //
  761. // Looking good so far, now check the Flash WIPE bit.
  762. //
  763. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  764. AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M ? false : true;
  765. }
  766. //*****************************************************************************
  767. //
  768. //! @brief Enable SRAM protection so SRAM gets wiped if a debgger is connected.
  769. //!
  770. //! Use this function to set the state of the 128 valid bits at the beginning
  771. //! of customer info space, if needed. Then set the SRAM wipe bit to zero.
  772. //!
  773. //! @note Interrupts are active during execution of this function. Any interrupt
  774. //! taken could cause execution errors. Please see the IMPORTANT note under
  775. //! Detailed Description above for more details.
  776. //!
  777. //! @return Zero for success. Non-Zero for errors.
  778. //
  779. //*****************************************************************************
  780. int32_t
  781. am_hal_flash_wipe_sram_enable(void)
  782. {
  783. int iRC;
  784. uint32_t ui32SecurityValue;
  785. //
  786. // Security protection only works if the signature data is correct.
  787. //
  788. iRC = customer_info_signature_set();
  789. if ( iRC )
  790. {
  791. return iRC;
  792. }
  793. //
  794. // Clear the SRAM Wipe bit.
  795. //
  796. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  797. ~AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M;
  798. //
  799. // Now write the word to the flash INFO space.
  800. //
  801. return g_am_hal_flash.flash_program_info(
  802. AM_HAL_FLASH_PROGRAM_KEY,
  803. 0, // instance
  804. &ui32SecurityValue, // source data
  805. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  806. 1 ); // number of words
  807. }
  808. //*****************************************************************************
  809. //
  810. //! @brief check for SRAM protection enabled.
  811. //!
  812. //! Use this function to determine if SRAM protection is enabled.
  813. //!
  814. //! @return true if SRAM wipe protection is enabled, otherwise false.
  815. //
  816. //*****************************************************************************
  817. bool
  818. am_hal_flash_wipe_sram_enable_check(void)
  819. {
  820. //
  821. // If they are erased at this point then SRAM wipe can't be enabled.
  822. //
  823. if ( customer_info_signature_erased() )
  824. {
  825. return false;
  826. }
  827. //
  828. // If they are not vale at this point then SRAM wipe can't be enabled.
  829. //
  830. if ( !am_hal_flash_customer_info_signature_check() )
  831. {
  832. return false;
  833. }
  834. //
  835. // Looking good so far, now check the SRAM WIPE bit.
  836. //
  837. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  838. AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M ? false : true;
  839. }
  840. //*****************************************************************************
  841. //
  842. //! @brief Disable Output from ITM/SWO.
  843. //!
  844. //! Use this function to set the state of the 128 valid bits at the beginning
  845. //! of customer info space, if needed. Set the SWO disable bit to zero.
  846. //!
  847. //! @note Interrupts are active during execution of this function. Any interrupt
  848. //! taken could cause execution errors. Please see the IMPORTANT note under
  849. //! Detailed Description above for more details.
  850. //!
  851. //! @return Zero for success. Non-Zero for errors.
  852. //
  853. //*****************************************************************************
  854. int32_t
  855. am_hal_flash_swo_disable(void)
  856. {
  857. int iRC;
  858. uint32_t ui32SecurityValue;
  859. //
  860. // Security protection only works if the signature data is correct.
  861. //
  862. iRC = customer_info_signature_set();
  863. if ( iRC )
  864. {
  865. return iRC;
  866. }
  867. //
  868. // Clear the SWO bit.
  869. //
  870. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  871. ~AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M;
  872. //
  873. // Now write the word to the flash INFO space.
  874. //
  875. return g_am_hal_flash.flash_program_info(
  876. AM_HAL_FLASH_PROGRAM_KEY,
  877. 0, // instance
  878. &ui32SecurityValue, // source data
  879. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  880. 1 ); // number of words
  881. }
  882. //*****************************************************************************
  883. //
  884. //! @brief check for SWO disabled.
  885. //!
  886. //! Use this function to determine if the SWO is disabled.
  887. //!
  888. //! @return true if the ITM/SWO is disabled, otherwise false.
  889. //
  890. //*****************************************************************************
  891. bool
  892. am_hal_flash_swo_disable_check(void)
  893. {
  894. //
  895. // If they are erased at this point then SRAM wipe can't be enabled.
  896. //
  897. if ( customer_info_signature_erased() )
  898. {
  899. return false;
  900. }
  901. //
  902. // If they are not vale at this point then SRAM wipe can't be enabled.
  903. //
  904. if ( !am_hal_flash_customer_info_signature_check() )
  905. {
  906. return false;
  907. }
  908. //
  909. // Looking good so far, now check the SWO bit.
  910. //
  911. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  912. AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M ? false : true;
  913. }
  914. //*****************************************************************************
  915. //
  916. //! @brief Disable Connections from a debugger on the SWD interface.
  917. //!
  918. //! Use this function to set the state of the 128 valid bits at the beginning
  919. //! of customer info space, if needed. Set the debugger disable bit to zero.
  920. //!
  921. //! @note Interrupts are active during execution of this function. Any interrupt
  922. //! taken could cause execution errors. Please see the IMPORTANT note under
  923. //! Detailed Description above for more details.
  924. //!
  925. //! @return Zero for success. Non-Zero for errors.
  926. //
  927. //*****************************************************************************
  928. int32_t
  929. am_hal_flash_debugger_disable(void)
  930. {
  931. int iRC;
  932. uint32_t ui32SecurityValue;
  933. //
  934. // Security protection only works if the signature data is correct.
  935. //
  936. iRC = customer_info_signature_set();
  937. if ( iRC )
  938. {
  939. return iRC;
  940. }
  941. //
  942. // Clear the DEBUGGER bit.
  943. //
  944. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  945. ~AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M;
  946. //
  947. // Now write the word to the flash INFO space.
  948. //
  949. return g_am_hal_flash.flash_program_info(
  950. AM_HAL_FLASH_PROGRAM_KEY,
  951. 0, // instance
  952. &ui32SecurityValue, // source data
  953. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  954. 1 ); // number of words
  955. }
  956. //*****************************************************************************
  957. //
  958. //! @brief check for debugger disabled.
  959. //!
  960. //! Use this function to determine if the debugger is disabled.
  961. //!
  962. //! @return true if the debugger is disabled, otherwise false.
  963. //
  964. //*****************************************************************************
  965. bool
  966. am_hal_flash_debugger_disable_check(void)
  967. {
  968. //
  969. // If they are erased at this point then SRAM wipe can't be enabled.
  970. //
  971. if ( customer_info_signature_erased() )
  972. {
  973. return false;
  974. }
  975. //
  976. // If they are not vale at this point then SRAM wipe can't be enabled.
  977. //
  978. if ( !am_hal_flash_customer_info_signature_check() )
  979. {
  980. return false;
  981. }
  982. //
  983. // Looking good so far, now check the debugger disable bit.
  984. //
  985. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  986. AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M ? false : true;
  987. }
  988. //*****************************************************************************
  989. //
  990. //! @brief This static helper function generates a 64-bit protection mask.
  991. //!
  992. //! @param pui32StartAddress - Starting address in flash to begin protection.
  993. //! @param pui32StopAddress - Ending address in flash to stop protection.
  994. //!
  995. //! This function computes a chunk map for the protection range.
  996. //!
  997. //! @return Inverse of the actual chunk mask. That is, chunks to be protected
  998. //! are represented as 0 in the returned mask, while chunks to be left alone
  999. //! are represented as 1. This value can therefore be directly ANDed with the
  1000. //! existing bits in INFO space.
  1001. //! Note that -1 is returned if input parameters are invalid - this return
  1002. //! value would indicate that no chunks are to be protected.
  1003. //!
  1004. //
  1005. //*****************************************************************************
  1006. static uint64_t
  1007. generate_chunk_mask(uint32_t *pui32StartAddress, uint32_t *pui32StopAddress)
  1008. {
  1009. uint32_t ui32ChunkStart, ui32ChunkStop;
  1010. uint32_t ui32Width;
  1011. uint64_t ui64Mask;
  1012. //
  1013. // Validate the address input parameters
  1014. //
  1015. if ( (pui32StartAddress > pui32StopAddress) ||
  1016. (pui32StopAddress > (uint32_t*)AM_HAL_FLASH_LARGEST_VALID_ADDR) )
  1017. {
  1018. //
  1019. // Argument error, return value to leave all chunks unprotected.
  1020. //
  1021. return 0xFFFFFFFFFFFFFFFF;
  1022. }
  1023. //
  1024. // Extract chunk related information
  1025. //
  1026. ui32ChunkStart = AM_HAL_FLASH_INFO_ADDR2CHUNK((uint32_t)pui32StartAddress);
  1027. ui32ChunkStop = AM_HAL_FLASH_INFO_ADDR2CHUNK((uint32_t)pui32StopAddress);
  1028. ui32Width = ui32ChunkStop - ui32ChunkStart + 1;
  1029. if ( ui32Width == 64 )
  1030. {
  1031. ui64Mask = (uint64_t)0xFFFFFFFFFFFFFFFFLLU;
  1032. }
  1033. else
  1034. {
  1035. ui64Mask = ( ((uint64_t)0x0000000000000001) << ui32Width) - 1;
  1036. ui64Mask <<= ui32ChunkStart;
  1037. }
  1038. //
  1039. // OK now return the chunk mask (inverted).
  1040. //
  1041. return ~ui64Mask;
  1042. }
  1043. //*****************************************************************************
  1044. //
  1045. //! @brief This function sets copy protection for a range of flash chunks.
  1046. //!
  1047. //! @param pui32StartAddress - Starting address in flash to begin protection.
  1048. //! @param pui32StopAddress - Ending address in flash to stop protection.
  1049. //!
  1050. //! This function will set copy protection bits for a range of flash chunks
  1051. //!
  1052. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1053. //! the protection register. Set the bit to zero to enable protection.
  1054. //!
  1055. //! @note Interrupts are active during execution of this function. Any interrupt
  1056. //! taken could cause execution errors. Please see the IMPORTANT note under
  1057. //! Detailed Description above for more details.
  1058. //!
  1059. //! @return
  1060. //! 0 for success.
  1061. //! 0x400000 if the protection bits were already programmed (mask the return
  1062. //! value with 0x3FFFFF to ignore this case and treat as success).
  1063. //! Otherwise, non-zero for failure.
  1064. //
  1065. //*****************************************************************************
  1066. int32_t
  1067. am_hal_flash_copy_protect_set(uint32_t *pui32StartAddress,
  1068. uint32_t *pui32StopAddress)
  1069. {
  1070. int iRC;
  1071. bool bModified = false;
  1072. uint64_t ui64Mask;
  1073. uint32_t ui32Work;
  1074. uint32_t ui32Protection[2];
  1075. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_COPYPROT_ADDR;
  1076. //
  1077. // Extract chunk mask from parameters.
  1078. // Also checks parameter validity (returns -1 if bad parameters).
  1079. //
  1080. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1081. if ( ~ui64Mask == 0x0 )
  1082. {
  1083. return 0x100000;
  1084. }
  1085. //
  1086. // Go get the current settings for copy protection.
  1087. //
  1088. ui32Protection[0] = pui32Protection[0];
  1089. ui32Protection[1] = pui32Protection[1];
  1090. //
  1091. // AND mask off the necessary protection bits in the lower word.
  1092. //
  1093. ui32Work = (uint32_t)ui64Mask;
  1094. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[0] ) )
  1095. {
  1096. bModified = true;
  1097. ui32Protection[0] &= ui32Work;
  1098. iRC = g_am_hal_flash.flash_program_info(
  1099. AM_HAL_FLASH_PROGRAM_KEY,
  1100. 0, // instance
  1101. &ui32Protection[0], // source data
  1102. (AM_HAL_FLASH_INFO_COPYPROT_O / 4) + 0, // word offset
  1103. 1 ); // number of words
  1104. if ( iRC )
  1105. {
  1106. return iRC | 0x10000;
  1107. }
  1108. }
  1109. //
  1110. // AND mask off the necessary protection bits in the upper word.
  1111. //
  1112. ui32Work = (uint32_t)(ui64Mask >> 32);
  1113. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[1] ) )
  1114. {
  1115. bModified = true;
  1116. ui32Protection[1] &= ui32Work;
  1117. iRC = g_am_hal_flash.flash_program_info(
  1118. AM_HAL_FLASH_PROGRAM_KEY,
  1119. 0, // instance
  1120. &ui32Protection[1], // source data
  1121. (AM_HAL_FLASH_INFO_COPYPROT_O / 4) + 1, // word offset
  1122. 1 ); // number of words
  1123. if ( iRC )
  1124. {
  1125. return iRC | 0x20000;
  1126. }
  1127. }
  1128. if ( bModified )
  1129. {
  1130. return 0;
  1131. }
  1132. else
  1133. {
  1134. return 0x400000;
  1135. }
  1136. }
  1137. //*****************************************************************************
  1138. //
  1139. //! @brief This function checks copy protection for a range of flash chunks.
  1140. //!
  1141. //! @param pui32StartAddress - Starting address in flash.
  1142. //! @param pui32StopAddress - Ending address in flash.
  1143. //!
  1144. //! This function will check copy protection bits for a range of flash chunks
  1145. //! it expects all chunks in the range to be protected.
  1146. //!
  1147. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1148. //! the protection register. Set the bit to zero to enable protection.
  1149. //!
  1150. //! @return false for at least one chunk in the covered range is not protected,
  1151. //! and true if all chunks in the covered range are protected.
  1152. //!
  1153. //
  1154. //*****************************************************************************
  1155. bool
  1156. am_hal_flash_copy_protect_check(uint32_t *pui32StartAddress,
  1157. uint32_t *pui32StopAddress)
  1158. {
  1159. uint64_t ui64Mask;
  1160. uint32_t ui32Work;
  1161. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_COPYPROT_ADDR;
  1162. //
  1163. // Extract chunk mask from parameters.
  1164. // Also checks parameter validity (returns -1 if bad parameters).
  1165. //
  1166. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1167. if ( ~ui64Mask == 0x0 )
  1168. {
  1169. return false;
  1170. }
  1171. //
  1172. // Now check the lower word of protection bits.
  1173. //
  1174. ui32Work = (uint32_t)ui64Mask;
  1175. if ( ~ui32Work & pui32Protection[0] )
  1176. {
  1177. return false;
  1178. }
  1179. //
  1180. // Now check the lower word of protection bits.
  1181. //
  1182. ui32Work = (uint32_t)(ui64Mask >> 32);
  1183. if ( ~ui32Work & pui32Protection[1] )
  1184. {
  1185. return false;
  1186. }
  1187. //
  1188. // If we get here, there are no unprotected chunks within specified range.
  1189. //
  1190. return true;
  1191. }
  1192. //*****************************************************************************
  1193. //
  1194. //! @brief This function sets write protection for a range of flash chunks.
  1195. //!
  1196. //! @param pui32StartAddress - Starting address in flash to begin protection.
  1197. //! @param pui32StopAddress - Ending address in flash to stop protection.
  1198. //!
  1199. //! This function will set write protection bits for a range of flash chunks
  1200. //!
  1201. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1202. //! the protection register. Set the bit to zero to enable protection.
  1203. //!
  1204. //! @note Interrupts are active during execution of this function. Any interrupt
  1205. //! taken could cause execution errors. Please see the IMPORTANT note under
  1206. //! Detailed Description above for more details.
  1207. //!
  1208. //! @return
  1209. //! 0 for success.
  1210. //! 0x400000 if the protection bits were already programmed (mask the return
  1211. //! value with 0x3FFFFF to ignore this case and treat as success).
  1212. //! Otherwise, non-zero for failure.
  1213. //
  1214. //*****************************************************************************
  1215. int32_t
  1216. am_hal_flash_write_protect_set(uint32_t *pui32StartAddress,
  1217. uint32_t *pui32StopAddress)
  1218. {
  1219. int iRC;
  1220. bool bModified = false;
  1221. uint64_t ui64Mask;
  1222. uint32_t ui32Work;
  1223. uint32_t ui32Protection[2];
  1224. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_WRITPROT_ADDR;
  1225. //
  1226. // Extract chunk mask from parameters.
  1227. // Also checks parameter validity (returns -1 if bad parameters).
  1228. //
  1229. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1230. if ( ~ui64Mask == 0x0 )
  1231. {
  1232. return 0x100000;
  1233. }
  1234. //
  1235. // Go get the current settings for copy protection.
  1236. //
  1237. ui32Protection[0] = pui32Protection[0];
  1238. ui32Protection[1] = pui32Protection[1];
  1239. //
  1240. // AND mask off the necessary protection bits in the lower word.
  1241. //
  1242. ui32Work = (uint32_t)ui64Mask;
  1243. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[0] ) )
  1244. {
  1245. bModified = true;
  1246. ui32Protection[0] &= ui32Work;
  1247. iRC = g_am_hal_flash.flash_program_info(
  1248. AM_HAL_FLASH_PROGRAM_KEY,
  1249. 0, // instance
  1250. &ui32Protection[0], // source data
  1251. (AM_HAL_FLASH_INFO_WRITPROT_O / 4) + 0, // word offset
  1252. 1 ); // number of words
  1253. if ( iRC )
  1254. {
  1255. return iRC | 0x10000;
  1256. }
  1257. }
  1258. //
  1259. // AND mask off the necessary protection bits in the upper word.
  1260. //
  1261. ui32Work = (uint32_t)(ui64Mask >> 32);
  1262. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[1] ) )
  1263. {
  1264. bModified = true;
  1265. ui32Protection[1] &= ui32Work;
  1266. iRC = g_am_hal_flash.flash_program_info(
  1267. AM_HAL_FLASH_PROGRAM_KEY,
  1268. 0, // instance
  1269. &ui32Protection[1], // source data
  1270. (AM_HAL_FLASH_INFO_WRITPROT_O / 4) + 1, // word offset
  1271. 1 ); // number of words
  1272. if ( iRC )
  1273. {
  1274. return iRC | 0x20000;
  1275. }
  1276. }
  1277. if ( bModified )
  1278. {
  1279. return 0;
  1280. }
  1281. else
  1282. {
  1283. return 0x400000;
  1284. }
  1285. }
  1286. //*****************************************************************************
  1287. //
  1288. //! @brief This function checks write protection for a range of flash chunks.
  1289. //!
  1290. //! @param pui32StartAddress - Starting address in flash.
  1291. //! @param pui32StopAddress - Ending address in flash.
  1292. //!
  1293. //! This function will check write protection bits for a range of flash chunks
  1294. //! it expects all chunks in the range to be protected.
  1295. //!
  1296. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1297. //! the protection register. Set the bit to zero to enable protection.
  1298. //!
  1299. //! @return false for at least one chunk in the covered range is not protected,
  1300. //! and true if all chunks in the covered range are protected.
  1301. //!
  1302. //
  1303. //*****************************************************************************
  1304. bool
  1305. am_hal_flash_write_protect_check(uint32_t *pui32StartAddress,
  1306. uint32_t *pui32StopAddress)
  1307. {
  1308. uint64_t ui64Mask;
  1309. uint32_t ui32Work;
  1310. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_WRITPROT_ADDR;
  1311. //
  1312. // Extract chunk mask from parameters.
  1313. // Also checks parameter validity (returns -1 if bad parameters).
  1314. //
  1315. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1316. if ( ~ui64Mask == 0x0 )
  1317. {
  1318. return false;
  1319. }
  1320. //
  1321. // Now check the lower word of protection bits.
  1322. //
  1323. ui32Work = (uint32_t)ui64Mask;
  1324. if ( ~ui32Work & pui32Protection[0] )
  1325. {
  1326. return false;
  1327. }
  1328. //
  1329. // Now check the lower word of protection bits.
  1330. //
  1331. ui32Work = (uint32_t)(ui64Mask >> 32);
  1332. if ( ~ui32Work & pui32Protection[1] )
  1333. {
  1334. return false;
  1335. }
  1336. //
  1337. // If we get here, there are no unprotected chunks within specified range.
  1338. //
  1339. return true;
  1340. }
  1341. //*****************************************************************************
  1342. //
  1343. // End Doxygen group.
  1344. //! @}
  1345. //
  1346. //*****************************************************************************