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