am_hal_flash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //*****************************************************************************
  2. //
  3. // am_hal_flash.h
  4. //! @file
  5. //!
  6. //! @brief Functions for performing Flash operations.
  7. //!
  8. //! @addtogroup flash2 Flash
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_FLASH_H
  48. #define AM_HAL_FLASH_H
  49. #include <stdint.h>
  50. #include <stdbool.h>
  51. //*****************************************************************************
  52. //
  53. // Flash Program keys.
  54. //
  55. //*****************************************************************************
  56. #define AM_HAL_FLASH_PROGRAM_KEY 0x12344321
  57. #define AM_HAL_FLASH_RECOVERY_KEY 0xA35C9B6D
  58. #define AM_HAL_FLASH_INFO_KEY 0x12344321
  59. #define AM_HAL_FLASH_OTP_KEY (AM_HAL_FLASH_INFO_KEY)
  60. //*****************************************************************************
  61. //
  62. // Some helpful flash values and macros.
  63. //
  64. //*****************************************************************************
  65. #define AM_HAL_FLASH_ADDR 0x00000000
  66. #define AM_HAL_FLASH_PAGE_SIZE ( 8 * 1024 )
  67. #define AM_HAL_FLASH_INFO_SIZE AM_HAL_FLASH_PAGE_SIZE
  68. #define AM_HAL_FLASH_INSTANCE_SIZE ( 512 * 1024 )
  69. #define AM_HAL_FLASH_INSTANCE_PAGES ( AM_HAL_FLASH_INSTANCE_SIZE / AM_HAL_FLASH_PAGE_SIZE )
  70. #define AM_HAL_FLASH_TOTAL_SIZE ( AM_HAL_FLASH_INSTANCE_SIZE * 2 )
  71. #define AM_HAL_FLASH_LARGEST_VALID_ADDR ( AM_HAL_FLASH_ADDR + AM_HAL_FLASH_TOTAL_SIZE - 1 )
  72. //
  73. // Convert an absolute flash address to a instance
  74. //
  75. #define AM_HAL_FLASH_ADDR2INST(addr) ( ( addr >> 19 ) & 1 )
  76. //
  77. // Convert an absolute flash address to a page number relative to the instance
  78. //
  79. #define AM_HAL_FLASH_ADDR2PAGE(addr) ( ( addr >> 13 ) & 0x3F )
  80. //
  81. // Convert an absolute flash address to an absolute page number
  82. //
  83. #define AM_HAL_FLASH_ADDR2ABSPAGE(addr) ( addr >> 13 )
  84. //*****************************************************************************
  85. //
  86. // Given an integer number of microseconds, convert to a value representing the
  87. // number of am_hal_flash_delay() cycles that will provide that amount of delay.
  88. // This macro is designed to take into account some of the call overhead.
  89. //
  90. // e.g. To provide a 2us delay:
  91. // am_hal_flash_delay( FLASH_CYCLES_US(2) );
  92. //
  93. // IMPORTANT - Apollo2 is spec'ed for only 48MHz operation, so this macro
  94. // assumes that.
  95. //
  96. //*****************************************************************************
  97. #define FLASH_CYCLES_US(n) ((n * (AM_HAL_CLKGEN_FREQ_MAX_MHZ / 3)) - 4)
  98. //
  99. // Backward compatibility
  100. //
  101. #define am_hal_flash_program_otp am_hal_flash_program_info
  102. #define am_hal_flash_program_otp_sram am_hal_flash_program_info_sram
  103. //*****************************************************************************
  104. //
  105. // Structure of function pointers to helper functions for invoking various
  106. // flash operations. The functions we are pointing to here are in the Apollo 2
  107. // integrated BOOTROM.
  108. //
  109. //*****************************************************************************
  110. typedef struct am_hal_flash_helper_struct
  111. {
  112. //
  113. // The basics.
  114. //
  115. int (*flash_mass_erase)(uint32_t, uint32_t);
  116. int (*flash_page_erase)(uint32_t, uint32_t, uint32_t);
  117. int (*flash_program_main)(uint32_t, uint32_t *,
  118. uint32_t*, uint32_t);
  119. int (*flash_program_info)(uint32_t, uint32_t,
  120. uint32_t*, uint32_t, uint32_t);
  121. //
  122. // Non-blocking variants, but be careful these are not interrupt safe so
  123. // mask interrupts while these very long operations proceed.
  124. //
  125. int (*flash_mass_erase_nb)(uint32_t, uint32_t);
  126. int (*flash_page_erase_nb)(uint32_t, uint32_t, uint32_t);
  127. bool (*flash_nb_operation_complete)(void);
  128. //
  129. // Essentially these are recovery options.
  130. //
  131. int (*flash_erase_info)(uint32_t, uint32_t);
  132. int (*flash_erase_main_plus_info)(uint32_t, uint32_t);
  133. int (*flash_erase_main_plus_info_both_instances)(uint32_t);
  134. void (*flash_recovery)(uint32_t);
  135. //
  136. // Useful utilities.
  137. //
  138. uint32_t (*flash_util_read_word)(uint32_t*);
  139. void (*flash_util_write_word)(uint32_t*, uint32_t);
  140. void (*delay_cycles)(uint32_t);
  141. //
  142. // The following functions pointers will generally never be called from
  143. // user programs. They are here primarily to document these entry points
  144. // which are usable from a debugger or debugger script.
  145. //
  146. void (*flash_program_main_sram)(void);
  147. void (*flash_program_info_sram)(void);
  148. void (*flash_erase_main_pages_sram)(void);
  149. void (*flash_mass_erase_sram)(void);
  150. void (*flash_erase_info_sram)(void);
  151. void (*flash_erase_main_plus_info_sram)(void);
  152. } g_am_hal_flash_t;
  153. extern g_am_hal_flash_t g_am_hal_flash;
  154. //*****************************************************************************
  155. //
  156. // Define some FLASH INFO SPACE values and macros.
  157. //
  158. //*****************************************************************************
  159. #define AM_HAL_FLASH_INFO_ADDR 0x50020000
  160. #define AM_HAL_FLASH_INFO_SECURITY_O 0x10
  161. #define AM_HAL_FLASH_INFO_WRITPROT_O 0x20
  162. #define AM_HAL_FLASH_INFO_COPYPROT_O 0x30
  163. #define AM_HAL_FLASH_INFO_SECURITY_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_SECURITY_O)
  164. #define AM_HAL_FLASH_INFO_WRITPROT_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_WRITPROT_O)
  165. #define AM_HAL_FLASH_INFO_COPYPROT_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_COPYPROT_O)
  166. //
  167. // Define the customer info signature data (at AM_HAL_FLASH_INFO_ADDR).
  168. // These bits must exist in the customer info space in order for many of the
  169. // security and protection functions to work.
  170. //
  171. #define AM_HAL_FLASH_INFO_SIGNATURE0 0x48EAAD88
  172. #define AM_HAL_FLASH_INFO_SIGNATURE1 0xC9705737
  173. #define AM_HAL_FLASH_INFO_SIGNATURE2 0x0A6B8458
  174. #define AM_HAL_FLASH_INFO_SIGNATURE3 0xE41A9D74
  175. //
  176. // Define the customer security bits (at AM_HAL_FLASH_INFO_SECURITY_ADDR)
  177. //
  178. #define AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_S 0
  179. #define AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_S 1
  180. #define AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_S 2
  181. #define AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_S 3
  182. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S 4
  183. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_S 8
  184. #define AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S 9
  185. #define AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_S))
  186. #define AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_S))
  187. #define AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_S))
  188. #define AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_S))
  189. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M ((uint32_t)(0xF << AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S))
  190. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_S))
  191. #define AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
  192. #define AM_HAL_FLASH_INFO_SECURITY_DEEPSLEEP_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
  193. #define AM_HAL_FLASH_INFO_SECURITY_DEEPSLEEP ((uint32_t)(0x0 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
  194. //
  195. // Protection chunk macros
  196. // AM_HAL_FLASH_INFO_CHUNK2ADDR: Convert a chunk number to an address
  197. // AM_HAL_FLASH_INFO_CHUNK2INST: Convert a chunk number to an instance number
  198. // AM_HAL_FLASH_INFO_ADDR2CHUNK: Convert an address to a chunk number
  199. //
  200. #define AM_HAL_FLASH_INFO_CHUNKSIZE (16*1024)
  201. #define AM_HAL_FLASH_INFO_CHUNK2ADDR(n) (AM_HAL_FLASH_ADDR + (n << 14))
  202. #define AM_HAL_FLASH_INFO_CHUNK2INST(n) ((n >> 5) & 1
  203. #define AM_HAL_FLASH_INFO_ADDR2CHUNK(n) ((n) >> 14)
  204. #ifdef __cplusplus
  205. extern "C"
  206. {
  207. #endif
  208. //*****************************************************************************
  209. //
  210. // Function prototypes for the helper functions
  211. //
  212. //*****************************************************************************
  213. extern int am_hal_flash_mass_erase(uint32_t ui32Value, uint32_t ui32FlashInst);
  214. extern int am_hal_flash_page_erase(uint32_t ui32Value, uint32_t ui32FlashInst,
  215. uint32_t ui32PageNum);
  216. extern int am_hal_flash_program_main(uint32_t value, uint32_t *pSrc,
  217. uint32_t *pDst, uint32_t NumberOfWords);
  218. extern int am_hal_flash_program_info(uint32_t ui32Value, uint32_t ui32InfoInst,
  219. uint32_t *pui32Src, uint32_t ui32Offset,
  220. uint32_t ui32NumWords);
  221. //
  222. // Recovery type functions for Customer INFO space.
  223. //
  224. extern int am_hal_flash_erase_info(uint32_t ui32ProgramKey,
  225. uint32_t ui32Instance);
  226. extern int am_hal_flash_erase_main_plus_info(uint32_t ui32ProgramKey,
  227. uint32_t ui32Instance);
  228. extern int am_hal_flash_erase_main_plus_info_both_instances(
  229. uint32_t ui32ProgramKey);
  230. extern void am_hal_flash_recovery(uint32_t ui32RecoveryKey);
  231. //
  232. // BOOTROM resident reader, writer and delay utility functions.
  233. //
  234. extern uint32_t am_hal_flash_load_ui32(uint32_t ui32Address);
  235. extern void am_hal_flash_store_ui32(uint32_t ui32Address, uint32_t ui32Data);
  236. extern void am_hal_flash_delay(uint32_t ui32Iterations);
  237. extern uint32_t am_hal_flash_delay_status_change(uint32_t ui32Iterations,
  238. uint32_t ui32Address,
  239. uint32_t ui32Mask,
  240. uint32_t ui32Value);
  241. //
  242. // These functions update security/protection bits in the customer INFO blOCK.
  243. //
  244. extern bool am_hal_flash_customer_info_signature_check(void);
  245. extern bool am_hal_flash_info_signature_set(void);
  246. extern int32_t am_hal_flash_info_erase_disable(void);
  247. extern bool am_hal_flash_info_erase_disable_check(void);
  248. extern int32_t am_hal_flash_info_program_disable(uint32_t ui32Mask);
  249. extern uint32_t am_hal_flash_info_program_disable_get(void);
  250. extern int32_t am_hal_flash_wipe_flash_enable(void);
  251. extern bool am_hal_flash_wipe_flash_enable_check(void);
  252. extern int32_t am_hal_flash_wipe_sram_enable(void);
  253. extern bool am_hal_flash_wipe_sram_enable_check(void);
  254. extern int32_t am_hal_flash_swo_disable(void);
  255. extern bool am_hal_flash_swo_disable_check(void);
  256. extern int32_t am_hal_flash_debugger_disable(void);
  257. extern bool am_hal_flash_debugger_disable_check(void);
  258. extern int32_t am_hal_flash_copy_protect_set(uint32_t *pui32StartAddress,
  259. uint32_t *pui32StopAddress);
  260. extern bool am_hal_flash_copy_protect_check(uint32_t *pui32StartAddress,
  261. uint32_t *pui32StopAddress);
  262. extern int32_t am_hal_flash_write_protect_set(uint32_t *pui32StartAddress,
  263. uint32_t *pui32StopAddress);
  264. extern bool am_hal_flash_write_protect_check(uint32_t *pui32StartAddress,
  265. uint32_t *pui32StopAddress);
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif // AM_HAL_FLASH_H
  270. //*****************************************************************************
  271. //
  272. // End Doxygen group.
  273. //! @}
  274. //
  275. //*****************************************************************************