am_hal_flash.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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.11 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 Delays for a desired amount of cycles while also waiting for a
  415. //! status change.
  416. //!
  417. //! @param ui32usMaxDelay - Maximum number of ~1uS delay loops.
  418. //! @param ui32Address - Address of the register for the status change.
  419. //! @param ui32Mask - Mask for the status change.
  420. //! @param ui32Value - Target value for the status change.
  421. //!
  422. //! This function will delay for approximately the given number of microseconds
  423. //! while checking for a status change, exiting when either the given time has
  424. //! expired or the status change is detected.
  425. //!
  426. //! @returns 0 = timeout.
  427. //! 1 = status change detected.
  428. //
  429. //*****************************************************************************
  430. uint32_t
  431. am_hal_flash_delay_status_change(uint32_t ui32usMaxDelay, uint32_t ui32Address,
  432. uint32_t ui32Mask, uint32_t ui32Value)
  433. {
  434. while ( ui32usMaxDelay-- )
  435. {
  436. //
  437. // Check the status
  438. //
  439. if ( ( AM_REGVAL(ui32Address) & ui32Mask ) == ui32Value )
  440. {
  441. return 1;
  442. }
  443. //
  444. // Call the BOOTROM cycle function to delay for about 1 microsecond.
  445. //
  446. am_hal_flash_delay( FLASH_CYCLES_US(1) );
  447. }
  448. return 0;
  449. } // am_hal_flash_delay_status_change()
  450. //*****************************************************************************
  451. //
  452. //! @brief Static Helper Function to check customer info valid bits erasure.
  453. //!
  454. //! Use this function to test the state of the 128 valid bits at the beginning
  455. //! of customer info space. If these are all erased then return true.
  456. //!
  457. //! @return true if the customer info bits are currently erased.
  458. //
  459. //*****************************************************************************
  460. static bool
  461. customer_info_signature_erased(void)
  462. {
  463. uint32_t *pui32Signature = (uint32_t *) AM_HAL_FLASH_INFO_ADDR;
  464. return ( (pui32Signature[3] == 0xFFFFFFFF) &&
  465. (pui32Signature[2] == 0xFFFFFFFF) &&
  466. (pui32Signature[1] == 0xFFFFFFFF) &&
  467. (pui32Signature[0] == 0xFFFFFFFF) ) ? true : false;
  468. }
  469. //*****************************************************************************
  470. //
  471. //! @brief Static Helper Function to set customer info valid bits
  472. //!
  473. //! Use this function to set the state of the 128 valid bits at the beginning
  474. //! of customer info space. If these bits are not set correctly then the
  475. //! customer protection bits in the INFO space will not be honored by the
  476. //! hardware.
  477. //!
  478. //! @return Zero for success. Non-Zero for errors.
  479. //
  480. //*****************************************************************************
  481. static int
  482. customer_info_signature_set(void)
  483. {
  484. uint32_t ui32Valid[4];
  485. int iRC;
  486. //
  487. // If they are already set then we are done.
  488. //
  489. if ( am_hal_flash_customer_info_signature_check() )
  490. {
  491. return 0;
  492. }
  493. //
  494. // If they are not erased at this point we have an error.
  495. //
  496. if ( !customer_info_signature_erased() )
  497. {
  498. return (2 << 16);
  499. }
  500. //
  501. // OK they need to be set so do it.
  502. //
  503. ui32Valid[3] = AM_HAL_FLASH_INFO_SIGNATURE3;
  504. ui32Valid[2] = AM_HAL_FLASH_INFO_SIGNATURE2;
  505. ui32Valid[1] = AM_HAL_FLASH_INFO_SIGNATURE1;
  506. ui32Valid[0] = AM_HAL_FLASH_INFO_SIGNATURE0;
  507. iRC = g_am_hal_flash.flash_program_info(AM_HAL_FLASH_PROGRAM_KEY,
  508. 0, // instance
  509. ui32Valid, // source data
  510. 0, // offset
  511. 4); // number of words
  512. return iRC | ((iRC) ? (1 << 16) : 0);
  513. }
  514. //*****************************************************************************
  515. //
  516. //! @brief Check that the customer info bits are valid.
  517. //!
  518. //! Use this function to test the state of the 128 valid bits at the beginning
  519. //! of customer info space. If these are not set correctly then the customer
  520. //! protection bits in the INFO space will not be honored by the hardware.
  521. //!
  522. //! @return true if valid.
  523. //
  524. //*****************************************************************************
  525. bool
  526. am_hal_flash_customer_info_signature_check(void)
  527. {
  528. uint32_t *pui32Signature = (uint32_t *)AM_HAL_FLASH_INFO_ADDR;
  529. return ( (pui32Signature[3] == AM_HAL_FLASH_INFO_SIGNATURE3) &&
  530. (pui32Signature[2] == AM_HAL_FLASH_INFO_SIGNATURE2) &&
  531. (pui32Signature[1] == AM_HAL_FLASH_INFO_SIGNATURE1) &&
  532. (pui32Signature[0] == AM_HAL_FLASH_INFO_SIGNATURE0) );
  533. }
  534. //*****************************************************************************
  535. //
  536. //! @brief INFO signature set.
  537. //!
  538. //! Use this function to set the state of the 128 valid bits at the beginning
  539. //! of customer info space, if needed.
  540. //!
  541. //! @note Interrupts are active during execution of this function. Any interrupt
  542. //! taken could cause execution errors. Please see the IMPORTANT note under
  543. //! Detailed Description above for more details.
  544. //!
  545. //! @return Zero for success. Non-Zero for errors.
  546. //
  547. //*****************************************************************************
  548. bool
  549. am_hal_flash_info_signature_set(void)
  550. {
  551. //
  552. // Check and set signature.
  553. //
  554. return customer_info_signature_set() ? false : true;
  555. }
  556. //*****************************************************************************
  557. //
  558. //! @brief Disable FLASH INFO space.
  559. //!
  560. //! Use this function to set the state of the 128 valid bits at the beginning
  561. //! of customer info space, if needed. Then disable FLASH erasure.
  562. //!
  563. //! @note Interrupts are active during execution of this function. Any interrupt
  564. //! taken could cause execution errors. Please see the IMPORTANT note under
  565. //! Detailed Description above for more details.
  566. //!
  567. //! @return Zero for success. Non-Zero for errors.
  568. //
  569. //*****************************************************************************
  570. int32_t
  571. am_hal_flash_info_erase_disable(void)
  572. {
  573. int iRC;
  574. uint32_t ui32SecurityValue;
  575. //
  576. // Security protection only works if the signature data is correct.
  577. //
  578. iRC = customer_info_signature_set();
  579. if ( iRC )
  580. {
  581. return iRC;
  582. }
  583. //
  584. // Clear bit in INFO space to disable erasure.
  585. //
  586. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  587. ~AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M;
  588. //
  589. // Now write the word to the flash INFO space.
  590. //
  591. return g_am_hal_flash.flash_program_info(
  592. AM_HAL_FLASH_PROGRAM_KEY,
  593. 0, // instance
  594. &ui32SecurityValue, // source data
  595. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  596. 1 ); // number of words
  597. }
  598. //*****************************************************************************
  599. //
  600. //! @brief Check for Disabled FLASH INFO space.
  601. //!
  602. //! Use this function to determine whether FLASH INFO erasure is disabled.
  603. //!
  604. //! @return true if FLASH INFO erase is disabled, otherwise false.
  605. //
  606. //*****************************************************************************
  607. bool
  608. am_hal_flash_info_erase_disable_check(void)
  609. {
  610. //
  611. // If they are erased at this point then SRAM wipe can't be enabled.
  612. //
  613. if ( customer_info_signature_erased() )
  614. {
  615. return false;
  616. }
  617. //
  618. // If they are not valid at this point then SRAM wipe can't be enabled.
  619. //
  620. if ( !am_hal_flash_customer_info_signature_check() )
  621. {
  622. return false;
  623. }
  624. //
  625. // Looking good so far, now check the SRAM WIPE bit.
  626. //
  627. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  628. AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M ? false : true;
  629. }
  630. //*****************************************************************************
  631. //
  632. //! @brief Mask off 1 to 4 quadrants of FLASH INFO space for programming.
  633. //!
  634. //! Use this function to set the state of the 128 valid bits at the beginning
  635. //! of customer info space, if needed. Then and the mask bits with the INFO
  636. //! space programming disable bits.
  637. //!
  638. //! @param ui32Mask - A mask of the 4 quadrants of info space where
  639. //! bit0 = First quadrant (first 2KB).
  640. //! bit1 = Second quadrant (second 2KB).
  641. //! bit2 = Third quadrant (third 2KB).
  642. //! bit3 = Fourth quadrant (fourth 2KB).
  643. //!
  644. //! @note This function disables only, any quadrant already disabled is not
  645. //! reenabled. That is, any ui32Mask bits specified as 0 are essentially nops.
  646. //!
  647. //! @note Interrupts are active during execution of this function. Any interrupt
  648. //! taken could cause execution errors. Please see the IMPORTANT note under
  649. //! Detailed Description above for more details.
  650. //!
  651. //! @return Zero for success. Non-Zero for errors.
  652. //
  653. //*****************************************************************************
  654. int32_t
  655. am_hal_flash_info_program_disable(uint32_t ui32Mask)
  656. {
  657. int iRC;
  658. uint32_t ui32SecurityValue;
  659. //
  660. // Security protection only works if the signature data is correct.
  661. //
  662. iRC = customer_info_signature_set();
  663. if ( iRC )
  664. {
  665. return iRC;
  666. }
  667. //
  668. // Make sure we have a valid mask and get the mask into the correct position.
  669. //
  670. ui32Mask <<= AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S;
  671. ui32Mask &= AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M;
  672. //
  673. // The security bit set to 1 enables programming, 0 disables programming.
  674. //
  675. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) & ~ui32Mask;
  676. //
  677. // Now write the word to the flash INFO space.
  678. //
  679. return g_am_hal_flash.flash_program_info(
  680. AM_HAL_FLASH_PROGRAM_KEY,
  681. 0, // instance
  682. &ui32SecurityValue, // source data
  683. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  684. 1 ); // number of words
  685. }
  686. //*****************************************************************************
  687. //
  688. //! @brief Return a mask specifying which quadrants of customer INFO space have
  689. //! been disabled for programming.
  690. //!
  691. //! Use this function to determine whether programming of customer INFO space
  692. //! has been disabled.
  693. //!
  694. //! @return A 4-bit mask of the disabled quadrants.
  695. //! 0xFFFFFFFF indicates an error.
  696. //! 0x0 indicates all customer INFO space programming is enabled.
  697. //! 0xF indicates all customer INFO space programming is disabled.
  698. //! bit0 indicates the first customer INFO space is disabled for programming.
  699. //! bit1 indicates the second customer INFO space is disabled for programming.
  700. //! bit2 indicates the third customer INFO space is disabled for programming.
  701. //! bit3 indicates the fourth customer INFO space is disabled for programming.
  702. //
  703. //*****************************************************************************
  704. uint32_t
  705. am_hal_flash_info_program_disable_get(void)
  706. {
  707. //
  708. // If they are erased at this point then SRAM wipe can't be enabled.
  709. //
  710. if ( customer_info_signature_erased() )
  711. {
  712. return 0xFFFFFFFF;
  713. }
  714. //
  715. // If not valid at this point, then INFO programming can't be enabled.
  716. //
  717. if ( !am_hal_flash_customer_info_signature_check() )
  718. {
  719. return 0xFFFFFFFF;
  720. }
  721. //
  722. // Looking good so far, now return a mask of the disabled bits.
  723. //
  724. return ((AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  725. AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M) ^
  726. AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M) >>
  727. AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S;
  728. }
  729. //*****************************************************************************
  730. //
  731. //! @brief Enable FLASH debugger protection (FLASH gets wiped if a debugger is
  732. //! connected).
  733. //!
  734. //! Use this function to set the state of the 128 valid bits at the beginning
  735. //! of customer info space, if needed. Then set the FLASH wipe bit to zero.
  736. //!
  737. //! @note Interrupts are active during execution of this function. Any interrupt
  738. //! taken could cause execution errors. Please see the IMPORTANT note under
  739. //! Detailed Description above for more details.
  740. //!
  741. //! @return Zero for success. Non-Zero for errors.
  742. //
  743. //*****************************************************************************
  744. int32_t
  745. am_hal_flash_wipe_flash_enable(void)
  746. {
  747. int iRC;
  748. uint32_t ui32SecurityValue;
  749. //
  750. // Security protection only works if the signature data is correct.
  751. //
  752. iRC = customer_info_signature_set();
  753. if ( iRC )
  754. {
  755. return iRC;
  756. }
  757. //
  758. // Clear the FLASH Wipe bit.
  759. //
  760. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  761. ~AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M;
  762. //
  763. // Now write the word to the flash INFO space.
  764. //
  765. return g_am_hal_flash.flash_program_info(
  766. AM_HAL_FLASH_PROGRAM_KEY,
  767. 0, // instance
  768. &ui32SecurityValue, // source data
  769. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  770. 1 ); // number of words
  771. }
  772. //*****************************************************************************
  773. //
  774. //! @brief check for FLASH wipe protection enabled.
  775. //!
  776. //! Use this function to determine if FLASH wipe protection is enabled.
  777. //!
  778. //! @return true if FLASH wipe protection is enabled, otherwise false.
  779. //
  780. //*****************************************************************************
  781. bool
  782. am_hal_flash_wipe_flash_enable_check(void)
  783. {
  784. //
  785. // If they are erased at this point then flash wipe can't be enabled.
  786. //
  787. if ( customer_info_signature_erased() )
  788. {
  789. return false;
  790. }
  791. //
  792. // If they are not valid at this point then flash wipe can't be enabled.
  793. //
  794. if ( !am_hal_flash_customer_info_signature_check() )
  795. {
  796. return false;
  797. }
  798. //
  799. // Looking good so far, now check the Flash WIPE bit.
  800. //
  801. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  802. AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M ? false : true;
  803. }
  804. //*****************************************************************************
  805. //
  806. //! @brief Enable SRAM protection so SRAM gets wiped if a debgger is connected.
  807. //!
  808. //! Use this function to set the state of the 128 valid bits at the beginning
  809. //! of customer info space, if needed. Then set the SRAM wipe bit to zero.
  810. //!
  811. //! @note Interrupts are active during execution of this function. Any interrupt
  812. //! taken could cause execution errors. Please see the IMPORTANT note under
  813. //! Detailed Description above for more details.
  814. //!
  815. //! @return Zero for success. Non-Zero for errors.
  816. //
  817. //*****************************************************************************
  818. int32_t
  819. am_hal_flash_wipe_sram_enable(void)
  820. {
  821. int iRC;
  822. uint32_t ui32SecurityValue;
  823. //
  824. // Security protection only works if the signature data is correct.
  825. //
  826. iRC = customer_info_signature_set();
  827. if ( iRC )
  828. {
  829. return iRC;
  830. }
  831. //
  832. // Clear the SRAM Wipe bit.
  833. //
  834. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  835. ~AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M;
  836. //
  837. // Now write the word to the flash INFO space.
  838. //
  839. return g_am_hal_flash.flash_program_info(
  840. AM_HAL_FLASH_PROGRAM_KEY,
  841. 0, // instance
  842. &ui32SecurityValue, // source data
  843. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  844. 1 ); // number of words
  845. }
  846. //*****************************************************************************
  847. //
  848. //! @brief check for SRAM protection enabled.
  849. //!
  850. //! Use this function to determine if SRAM protection is enabled.
  851. //!
  852. //! @return true if SRAM wipe protection is enabled, otherwise false.
  853. //
  854. //*****************************************************************************
  855. bool
  856. am_hal_flash_wipe_sram_enable_check(void)
  857. {
  858. //
  859. // If they are erased at this point then SRAM wipe can't be enabled.
  860. //
  861. if ( customer_info_signature_erased() )
  862. {
  863. return false;
  864. }
  865. //
  866. // If they are not vale at this point then SRAM wipe can't be enabled.
  867. //
  868. if ( !am_hal_flash_customer_info_signature_check() )
  869. {
  870. return false;
  871. }
  872. //
  873. // Looking good so far, now check the SRAM WIPE bit.
  874. //
  875. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  876. AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M ? false : true;
  877. }
  878. //*****************************************************************************
  879. //
  880. //! @brief Disable Output from ITM/SWO.
  881. //!
  882. //! Use this function to set the state of the 128 valid bits at the beginning
  883. //! of customer info space, if needed. Set the SWO disable bit to zero.
  884. //!
  885. //! @note Interrupts are active during execution of this function. Any interrupt
  886. //! taken could cause execution errors. Please see the IMPORTANT note under
  887. //! Detailed Description above for more details.
  888. //!
  889. //! @return Zero for success. Non-Zero for errors.
  890. //
  891. //*****************************************************************************
  892. int32_t
  893. am_hal_flash_swo_disable(void)
  894. {
  895. int iRC;
  896. uint32_t ui32SecurityValue;
  897. //
  898. // Security protection only works if the signature data is correct.
  899. //
  900. iRC = customer_info_signature_set();
  901. if ( iRC )
  902. {
  903. return iRC;
  904. }
  905. //
  906. // Clear the SWO bit.
  907. //
  908. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  909. ~AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M;
  910. //
  911. // Now write the word to the flash INFO space.
  912. //
  913. return g_am_hal_flash.flash_program_info(
  914. AM_HAL_FLASH_PROGRAM_KEY,
  915. 0, // instance
  916. &ui32SecurityValue, // source data
  917. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  918. 1 ); // number of words
  919. }
  920. //*****************************************************************************
  921. //
  922. //! @brief check for SWO disabled.
  923. //!
  924. //! Use this function to determine if the SWO is disabled.
  925. //!
  926. //! @return true if the ITM/SWO is disabled, otherwise false.
  927. //
  928. //*****************************************************************************
  929. bool
  930. am_hal_flash_swo_disable_check(void)
  931. {
  932. //
  933. // If they are erased at this point then SRAM wipe can't be enabled.
  934. //
  935. if ( customer_info_signature_erased() )
  936. {
  937. return false;
  938. }
  939. //
  940. // If they are not vale at this point then SRAM wipe can't be enabled.
  941. //
  942. if ( !am_hal_flash_customer_info_signature_check() )
  943. {
  944. return false;
  945. }
  946. //
  947. // Looking good so far, now check the SWO bit.
  948. //
  949. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  950. AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M ? false : true;
  951. }
  952. //*****************************************************************************
  953. //
  954. //! @brief Disable Connections from a debugger on the SWD interface.
  955. //!
  956. //! Use this function to set the state of the 128 valid bits at the beginning
  957. //! of customer info space, if needed. Set the debugger disable bit to zero.
  958. //!
  959. //! @note Interrupts are active during execution of this function. Any interrupt
  960. //! taken could cause execution errors. Please see the IMPORTANT note under
  961. //! Detailed Description above for more details.
  962. //!
  963. //! @return Zero for success. Non-Zero for errors.
  964. //
  965. //*****************************************************************************
  966. int32_t
  967. am_hal_flash_debugger_disable(void)
  968. {
  969. int iRC;
  970. uint32_t ui32SecurityValue;
  971. //
  972. // Security protection only works if the signature data is correct.
  973. //
  974. iRC = customer_info_signature_set();
  975. if ( iRC )
  976. {
  977. return iRC;
  978. }
  979. //
  980. // Clear the DEBUGGER bit.
  981. //
  982. ui32SecurityValue = AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  983. ~AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M;
  984. //
  985. // Now write the word to the flash INFO space.
  986. //
  987. return g_am_hal_flash.flash_program_info(
  988. AM_HAL_FLASH_PROGRAM_KEY,
  989. 0, // instance
  990. &ui32SecurityValue, // source data
  991. AM_HAL_FLASH_INFO_SECURITY_O / 4, // word offset
  992. 1 ); // number of words
  993. }
  994. //*****************************************************************************
  995. //
  996. //! @brief check for debugger disabled.
  997. //!
  998. //! Use this function to determine if the debugger is disabled.
  999. //!
  1000. //! @return true if the debugger is disabled, otherwise false.
  1001. //
  1002. //*****************************************************************************
  1003. bool
  1004. am_hal_flash_debugger_disable_check(void)
  1005. {
  1006. //
  1007. // If they are erased at this point then SRAM wipe can't be enabled.
  1008. //
  1009. if ( customer_info_signature_erased() )
  1010. {
  1011. return false;
  1012. }
  1013. //
  1014. // If they are not vale at this point then SRAM wipe can't be enabled.
  1015. //
  1016. if ( !am_hal_flash_customer_info_signature_check() )
  1017. {
  1018. return false;
  1019. }
  1020. //
  1021. // Looking good so far, now check the debugger disable bit.
  1022. //
  1023. return AM_REGVAL(AM_HAL_FLASH_INFO_SECURITY_ADDR) &
  1024. AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M ? false : true;
  1025. }
  1026. //*****************************************************************************
  1027. //
  1028. //! @brief This static helper function generates a 64-bit protection mask.
  1029. //!
  1030. //! @param pui32StartAddress - Starting address in flash to begin protection.
  1031. //! @param pui32StopAddress - Ending address in flash to stop protection.
  1032. //!
  1033. //! This function computes a chunk map for the protection range.
  1034. //!
  1035. //! @return Inverse of the actual chunk mask. That is, chunks to be protected
  1036. //! are represented as 0 in the returned mask, while chunks to be left alone
  1037. //! are represented as 1. This value can therefore be directly ANDed with the
  1038. //! existing bits in INFO space.
  1039. //! Note that -1 is returned if input parameters are invalid - this return
  1040. //! value would indicate that no chunks are to be protected.
  1041. //!
  1042. //
  1043. //*****************************************************************************
  1044. static uint64_t
  1045. generate_chunk_mask(uint32_t *pui32StartAddress, uint32_t *pui32StopAddress)
  1046. {
  1047. uint32_t ui32ChunkStart, ui32ChunkStop;
  1048. uint32_t ui32Width;
  1049. uint64_t ui64Mask;
  1050. //
  1051. // Validate the address input parameters
  1052. //
  1053. if ( (pui32StartAddress > pui32StopAddress) ||
  1054. (pui32StopAddress > (uint32_t*)AM_HAL_FLASH_LARGEST_VALID_ADDR) )
  1055. {
  1056. //
  1057. // Argument error, return value to leave all chunks unprotected.
  1058. //
  1059. return 0xFFFFFFFFFFFFFFFF;
  1060. }
  1061. //
  1062. // Extract chunk related information
  1063. //
  1064. ui32ChunkStart = AM_HAL_FLASH_INFO_ADDR2CHUNK((uint32_t)pui32StartAddress);
  1065. ui32ChunkStop = AM_HAL_FLASH_INFO_ADDR2CHUNK((uint32_t)pui32StopAddress);
  1066. ui32Width = ui32ChunkStop - ui32ChunkStart + 1;
  1067. if ( ui32Width == 64 )
  1068. {
  1069. ui64Mask = (uint64_t)0xFFFFFFFFFFFFFFFFLLU;
  1070. }
  1071. else
  1072. {
  1073. ui64Mask = ( ((uint64_t)0x0000000000000001) << ui32Width) - 1;
  1074. ui64Mask <<= ui32ChunkStart;
  1075. }
  1076. //
  1077. // OK now return the chunk mask (inverted).
  1078. //
  1079. return ~ui64Mask;
  1080. }
  1081. //*****************************************************************************
  1082. //
  1083. //! @brief This function sets copy protection for a range of flash chunks.
  1084. //!
  1085. //! @param pui32StartAddress - Starting address in flash to begin protection.
  1086. //! @param pui32StopAddress - Ending address in flash to stop protection.
  1087. //!
  1088. //! This function will set copy protection bits for a range of flash chunks
  1089. //!
  1090. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1091. //! the protection register. Set the bit to zero to enable protection.
  1092. //!
  1093. //! @note Interrupts are active during execution of this function. Any interrupt
  1094. //! taken could cause execution errors. Please see the IMPORTANT note under
  1095. //! Detailed Description above for more details.
  1096. //!
  1097. //! @return
  1098. //! 0 for success.
  1099. //! 0x400000 if the protection bits were already programmed (mask the return
  1100. //! value with 0x3FFFFF to ignore this case and treat as success).
  1101. //! Otherwise, non-zero for failure.
  1102. //
  1103. //*****************************************************************************
  1104. int32_t
  1105. am_hal_flash_copy_protect_set(uint32_t *pui32StartAddress,
  1106. uint32_t *pui32StopAddress)
  1107. {
  1108. int iRC;
  1109. bool bModified = false;
  1110. uint64_t ui64Mask;
  1111. uint32_t ui32Work;
  1112. uint32_t ui32Protection[2];
  1113. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_COPYPROT_ADDR;
  1114. //
  1115. // Extract chunk mask from parameters.
  1116. // Also checks parameter validity (returns -1 if bad parameters).
  1117. //
  1118. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1119. if ( ~ui64Mask == 0x0 )
  1120. {
  1121. return 0x100000;
  1122. }
  1123. //
  1124. // Go get the current settings for copy protection.
  1125. //
  1126. ui32Protection[0] = pui32Protection[0];
  1127. ui32Protection[1] = pui32Protection[1];
  1128. //
  1129. // AND mask off the necessary protection bits in the lower word.
  1130. //
  1131. ui32Work = (uint32_t)ui64Mask;
  1132. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[0] ) )
  1133. {
  1134. bModified = true;
  1135. ui32Protection[0] &= ui32Work;
  1136. iRC = g_am_hal_flash.flash_program_info(
  1137. AM_HAL_FLASH_PROGRAM_KEY,
  1138. 0, // instance
  1139. &ui32Protection[0], // source data
  1140. (AM_HAL_FLASH_INFO_COPYPROT_O / 4) + 0, // word offset
  1141. 1 ); // number of words
  1142. if ( iRC )
  1143. {
  1144. return iRC | 0x10000;
  1145. }
  1146. }
  1147. //
  1148. // AND mask off the necessary protection bits in the upper word.
  1149. //
  1150. ui32Work = (uint32_t)(ui64Mask >> 32);
  1151. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[1] ) )
  1152. {
  1153. bModified = true;
  1154. ui32Protection[1] &= ui32Work;
  1155. iRC = g_am_hal_flash.flash_program_info(
  1156. AM_HAL_FLASH_PROGRAM_KEY,
  1157. 0, // instance
  1158. &ui32Protection[1], // source data
  1159. (AM_HAL_FLASH_INFO_COPYPROT_O / 4) + 1, // word offset
  1160. 1 ); // number of words
  1161. if ( iRC )
  1162. {
  1163. return iRC | 0x20000;
  1164. }
  1165. }
  1166. if ( bModified )
  1167. {
  1168. return 0;
  1169. }
  1170. else
  1171. {
  1172. return 0x400000;
  1173. }
  1174. }
  1175. //*****************************************************************************
  1176. //
  1177. //! @brief This function checks copy protection for a range of flash chunks.
  1178. //!
  1179. //! @param pui32StartAddress - Starting address in flash.
  1180. //! @param pui32StopAddress - Ending address in flash.
  1181. //!
  1182. //! This function will check copy protection bits for a range of flash chunks
  1183. //! it expects all chunks in the range to be protected.
  1184. //!
  1185. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1186. //! the protection register. Set the bit to zero to enable protection.
  1187. //!
  1188. //! @return false for at least one chunk in the covered range is not protected,
  1189. //! and true if all chunks in the covered range are protected.
  1190. //!
  1191. //
  1192. //*****************************************************************************
  1193. bool
  1194. am_hal_flash_copy_protect_check(uint32_t *pui32StartAddress,
  1195. uint32_t *pui32StopAddress)
  1196. {
  1197. uint64_t ui64Mask;
  1198. uint32_t ui32Work;
  1199. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_COPYPROT_ADDR;
  1200. //
  1201. // Extract chunk mask from parameters.
  1202. // Also checks parameter validity (returns -1 if bad parameters).
  1203. //
  1204. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1205. if ( ~ui64Mask == 0x0 )
  1206. {
  1207. return false;
  1208. }
  1209. //
  1210. // Now check the lower word of protection bits.
  1211. //
  1212. ui32Work = (uint32_t)ui64Mask;
  1213. if ( ~ui32Work & pui32Protection[0] )
  1214. {
  1215. return false;
  1216. }
  1217. //
  1218. // Now check the lower word of protection bits.
  1219. //
  1220. ui32Work = (uint32_t)(ui64Mask >> 32);
  1221. if ( ~ui32Work & pui32Protection[1] )
  1222. {
  1223. return false;
  1224. }
  1225. //
  1226. // If we get here, there are no unprotected chunks within specified range.
  1227. //
  1228. return true;
  1229. }
  1230. //*****************************************************************************
  1231. //
  1232. //! @brief This function sets write protection for a range of flash chunks.
  1233. //!
  1234. //! @param pui32StartAddress - Starting address in flash to begin protection.
  1235. //! @param pui32StopAddress - Ending address in flash to stop protection.
  1236. //!
  1237. //! This function will set write protection bits for a range of flash chunks
  1238. //!
  1239. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1240. //! the protection register. Set the bit to zero to enable protection.
  1241. //!
  1242. //! @note Interrupts are active during execution of this function. Any interrupt
  1243. //! taken could cause execution errors. Please see the IMPORTANT note under
  1244. //! Detailed Description above for more details.
  1245. //!
  1246. //! @return
  1247. //! 0 for success.
  1248. //! 0x400000 if the protection bits were already programmed (mask the return
  1249. //! value with 0x3FFFFF to ignore this case and treat as success).
  1250. //! Otherwise, non-zero for failure.
  1251. //
  1252. //*****************************************************************************
  1253. int32_t
  1254. am_hal_flash_write_protect_set(uint32_t *pui32StartAddress,
  1255. uint32_t *pui32StopAddress)
  1256. {
  1257. int iRC;
  1258. bool bModified = false;
  1259. uint64_t ui64Mask;
  1260. uint32_t ui32Work;
  1261. uint32_t ui32Protection[2];
  1262. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_WRITPROT_ADDR;
  1263. //
  1264. // Extract chunk mask from parameters.
  1265. // Also checks parameter validity (returns -1 if bad parameters).
  1266. //
  1267. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1268. if ( ~ui64Mask == 0x0 )
  1269. {
  1270. return 0x100000;
  1271. }
  1272. //
  1273. // Go get the current settings for copy protection.
  1274. //
  1275. ui32Protection[0] = pui32Protection[0];
  1276. ui32Protection[1] = pui32Protection[1];
  1277. //
  1278. // AND mask off the necessary protection bits in the lower word.
  1279. //
  1280. ui32Work = (uint32_t)ui64Mask;
  1281. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[0] ) )
  1282. {
  1283. bModified = true;
  1284. ui32Protection[0] &= ui32Work;
  1285. iRC = g_am_hal_flash.flash_program_info(
  1286. AM_HAL_FLASH_PROGRAM_KEY,
  1287. 0, // instance
  1288. &ui32Protection[0], // source data
  1289. (AM_HAL_FLASH_INFO_WRITPROT_O / 4) + 0, // word offset
  1290. 1 ); // number of words
  1291. if ( iRC )
  1292. {
  1293. return iRC | 0x10000;
  1294. }
  1295. }
  1296. //
  1297. // AND mask off the necessary protection bits in the upper word.
  1298. //
  1299. ui32Work = (uint32_t)(ui64Mask >> 32);
  1300. if ( ( ~ui32Work ) && ( ui32Work != ui32Protection[1] ) )
  1301. {
  1302. bModified = true;
  1303. ui32Protection[1] &= ui32Work;
  1304. iRC = g_am_hal_flash.flash_program_info(
  1305. AM_HAL_FLASH_PROGRAM_KEY,
  1306. 0, // instance
  1307. &ui32Protection[1], // source data
  1308. (AM_HAL_FLASH_INFO_WRITPROT_O / 4) + 1, // word offset
  1309. 1 ); // number of words
  1310. if ( iRC )
  1311. {
  1312. return iRC | 0x20000;
  1313. }
  1314. }
  1315. if ( bModified )
  1316. {
  1317. return 0;
  1318. }
  1319. else
  1320. {
  1321. return 0x400000;
  1322. }
  1323. }
  1324. //*****************************************************************************
  1325. //
  1326. //! @brief This function checks write protection for a range of flash chunks.
  1327. //!
  1328. //! @param pui32StartAddress - Starting address in flash.
  1329. //! @param pui32StopAddress - Ending address in flash.
  1330. //!
  1331. //! This function will check write protection bits for a range of flash chunks
  1332. //! it expects all chunks in the range to be protected.
  1333. //!
  1334. //! @note Each flash chunk contains 16KBytes and corresponds to one bit in
  1335. //! the protection register. Set the bit to zero to enable protection.
  1336. //!
  1337. //! @return false for at least one chunk in the covered range is not protected,
  1338. //! and true if all chunks in the covered range are protected.
  1339. //!
  1340. //
  1341. //*****************************************************************************
  1342. bool
  1343. am_hal_flash_write_protect_check(uint32_t *pui32StartAddress,
  1344. uint32_t *pui32StopAddress)
  1345. {
  1346. uint64_t ui64Mask;
  1347. uint32_t ui32Work;
  1348. uint32_t *pui32Protection = (uint32_t *)AM_HAL_FLASH_INFO_WRITPROT_ADDR;
  1349. //
  1350. // Extract chunk mask from parameters.
  1351. // Also checks parameter validity (returns -1 if bad parameters).
  1352. //
  1353. ui64Mask = generate_chunk_mask(pui32StartAddress, pui32StopAddress);
  1354. if ( ~ui64Mask == 0x0 )
  1355. {
  1356. return false;
  1357. }
  1358. //
  1359. // Now check the lower word of protection bits.
  1360. //
  1361. ui32Work = (uint32_t)ui64Mask;
  1362. if ( ~ui32Work & pui32Protection[0] )
  1363. {
  1364. return false;
  1365. }
  1366. //
  1367. // Now check the lower word of protection bits.
  1368. //
  1369. ui32Work = (uint32_t)(ui64Mask >> 32);
  1370. if ( ~ui32Work & pui32Protection[1] )
  1371. {
  1372. return false;
  1373. }
  1374. //
  1375. // If we get here, there are no unprotected chunks within specified range.
  1376. //
  1377. return true;
  1378. }
  1379. //*****************************************************************************
  1380. //
  1381. // End Doxygen group.
  1382. //! @}
  1383. //
  1384. //*****************************************************************************