am_hal_flash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.9 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_FLASH_H
  48. #define AM_HAL_FLASH_H
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. #include <stdint.h>
  54. #include <stdbool.h>
  55. //*****************************************************************************
  56. //
  57. // Flash Program keys.
  58. //
  59. //*****************************************************************************
  60. #define AM_HAL_FLASH_PROGRAM_KEY 0x12344321
  61. #define AM_HAL_FLASH_RECOVERY_KEY 0xA35C9B6D
  62. #define AM_HAL_FLASH_INFO_KEY 0x12344321
  63. #define AM_HAL_FLASH_OTP_KEY (AM_HAL_FLASH_INFO_KEY)
  64. //*****************************************************************************
  65. //
  66. // Some helpful flash values and macros.
  67. //
  68. //*****************************************************************************
  69. #define AM_HAL_FLASH_ADDR 0x00000000
  70. #define AM_HAL_FLASH_PAGE_SIZE ( 8 * 1024 )
  71. #define AM_HAL_FLASH_INFO_SIZE AM_HAL_FLASH_PAGE_SIZE
  72. #define AM_HAL_FLASH_INSTANCE_SIZE ( 512 * 1024 )
  73. #define AM_HAL_FLASH_INSTANCE_PAGES ( AM_HAL_FLASH_INSTANCE_SIZE / AM_HAL_FLASH_PAGE_SIZE )
  74. #define AM_HAL_FLASH_TOTAL_SIZE ( AM_HAL_FLASH_INSTANCE_SIZE * 2 )
  75. #define AM_HAL_FLASH_LARGEST_VALID_ADDR ( AM_HAL_FLASH_ADDR + AM_HAL_FLASH_TOTAL_SIZE - 1 )
  76. //
  77. // Convert an absolute flash address to a instance
  78. //
  79. #define AM_HAL_FLASH_ADDR2INST(addr) ( ( addr >> 19 ) & 1 )
  80. //
  81. // Convert an absolute flash address to a page number relative to the instance
  82. //
  83. #define AM_HAL_FLASH_ADDR2PAGE(addr) ( ( addr >> 13 ) & 0x3F )
  84. //
  85. // Convert an absolute flash address to an absolute page number
  86. //
  87. #define AM_HAL_FLASH_ADDR2ABSPAGE(addr) ( addr >> 13 )
  88. //
  89. // Given a number of microseconds, convert to a value representing the number of
  90. // cycles that will give that delay. This macro is basically taking into account
  91. // some of the call overhead.
  92. // e.g. To provide a 2us delay:
  93. // am_hal_flash_delay( FLASH_CYCLES_US(2) );
  94. //
  95. #define FLASH_CYCLES_US(n) ((n * (AM_HAL_CLKGEN_FREQ_MAX_MHZ / 3)) - 4)
  96. //
  97. // Backward compatibility
  98. //
  99. #define am_hal_flash_program_otp am_hal_flash_program_info
  100. #define am_hal_flash_program_otp_sram am_hal_flash_program_info_sram
  101. //*****************************************************************************
  102. //
  103. // Structure of function pointers to helper functions for invoking various
  104. // flash operations. The functions we are pointing to here are in the Apollo 2
  105. // integrated BOOTROM.
  106. //
  107. //*****************************************************************************
  108. typedef struct am_hal_flash_helper_struct
  109. {
  110. //
  111. // The basics.
  112. //
  113. int (*flash_mass_erase)(uint32_t, uint32_t);
  114. int (*flash_page_erase)(uint32_t, uint32_t, uint32_t);
  115. int (*flash_program_main)(uint32_t, uint32_t *,
  116. uint32_t*, uint32_t);
  117. int (*flash_program_info)(uint32_t, uint32_t,
  118. uint32_t*, uint32_t, uint32_t);
  119. //
  120. // Non-blocking variants, but be careful these are not interrupt safe so
  121. // mask interrupts while these very long operations proceed.
  122. //
  123. int (*flash_mass_erase_nb)(uint32_t, uint32_t);
  124. int (*flash_page_erase_nb)(uint32_t, uint32_t, uint32_t);
  125. bool (*flash_nb_operation_complete)(void);
  126. //
  127. // Essentially these are recovery options.
  128. //
  129. int (*flash_erase_info)(uint32_t, uint32_t);
  130. int (*flash_erase_main_plus_info)(uint32_t, uint32_t);
  131. int (*flash_erase_main_plus_info_both_instances)(uint32_t);
  132. void (*flash_recovery)(uint32_t);
  133. //
  134. // Useful utilities.
  135. //
  136. uint32_t (*flash_util_read_word)(uint32_t*);
  137. void (*flash_util_write_word)(uint32_t*, uint32_t);
  138. void (*delay_cycles)(uint32_t);
  139. //
  140. // The following functions pointers will generally never be called from
  141. // user programs. They are here primarily to document these entry points
  142. // which are usable from a debugger or debugger script.
  143. //
  144. void (*flash_program_main_sram)(void);
  145. void (*flash_program_info_sram)(void);
  146. void (*flash_erase_main_pages_sram)(void);
  147. void (*flash_mass_erase_sram)(void);
  148. void (*flash_erase_info_sram)(void);
  149. void (*flash_erase_main_plus_info_sram)(void);
  150. } g_am_hal_flash_t;
  151. extern g_am_hal_flash_t g_am_hal_flash;
  152. //*****************************************************************************
  153. //
  154. // Define some FLASH INFO SPACE values and macros.
  155. //
  156. //*****************************************************************************
  157. #define AM_HAL_FLASH_INFO_ADDR 0x50020000
  158. #define AM_HAL_FLASH_INFO_SECURITY_O 0x10
  159. #define AM_HAL_FLASH_INFO_WRITPROT_O 0x20
  160. #define AM_HAL_FLASH_INFO_COPYPROT_O 0x30
  161. #define AM_HAL_FLASH_INFO_SECURITY_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_SECURITY_O)
  162. #define AM_HAL_FLASH_INFO_WRITPROT_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_WRITPROT_O)
  163. #define AM_HAL_FLASH_INFO_COPYPROT_ADDR (AM_HAL_FLASH_INFO_ADDR + AM_HAL_FLASH_INFO_COPYPROT_O)
  164. //
  165. // Define the customer info signature data (at AM_HAL_FLASH_INFO_ADDR).
  166. // These bits must exist in the customer info space in order for many of the
  167. // security and protection functions to work.
  168. //
  169. #define AM_HAL_FLASH_INFO_SIGNATURE0 0x48EAAD88
  170. #define AM_HAL_FLASH_INFO_SIGNATURE1 0xC9705737
  171. #define AM_HAL_FLASH_INFO_SIGNATURE2 0x0A6B8458
  172. #define AM_HAL_FLASH_INFO_SIGNATURE3 0xE41A9D74
  173. //
  174. // Define the customer security bits (at AM_HAL_FLASH_INFO_SECURITY_ADDR)
  175. //
  176. #define AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_S 0
  177. #define AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_S 1
  178. #define AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_S 2
  179. #define AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_S 3
  180. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S 4
  181. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_S 8
  182. #define AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S 9
  183. #define AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_DEBUGGERPROT_S))
  184. #define AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_SWOCTRL_S))
  185. #define AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_SRAMWIPE_S))
  186. #define AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_FLASHWIPE_S))
  187. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_M ((uint32_t)(0xF << AM_HAL_FLASH_INFO_SECURITY_ENINFOPRGM_S))
  188. #define AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_ENINFOERASE_S))
  189. #define AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
  190. #define AM_HAL_FLASH_INFO_SECURITY_DEEPSLEEP_M ((uint32_t)(0x1 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
  191. #define AM_HAL_FLASH_INFO_SECURITY_DEEPSLEEP ((uint32_t)(0x0 << AM_HAL_FLASH_INFO_SECURITY_BOOTLOADERSPIN_S))
  192. //
  193. // Protection chunk macros
  194. // AM_HAL_FLASH_INFO_CHUNK2ADDR: Convert a chunk number to an address
  195. // AM_HAL_FLASH_INFO_CHUNK2INST: Convert a chunk number to an instance number
  196. // AM_HAL_FLASH_INFO_ADDR2CHUNK: Convert an address to a chunk number
  197. //
  198. #define AM_HAL_FLASH_INFO_CHUNKSIZE (16*1024)
  199. #define AM_HAL_FLASH_INFO_CHUNK2ADDR(n) (AM_HAL_FLASH_ADDR + (n << 14))
  200. #define AM_HAL_FLASH_INFO_CHUNK2INST(n) ((n >> 5) & 1
  201. #define AM_HAL_FLASH_INFO_ADDR2CHUNK(n) ((n) >> 14)
  202. //*****************************************************************************
  203. //
  204. // Function prototypes for the helper functions
  205. //
  206. //*****************************************************************************
  207. extern int am_hal_flash_mass_erase(uint32_t ui32Value, uint32_t ui32FlashInst);
  208. extern int am_hal_flash_page_erase(uint32_t ui32Value, uint32_t ui32FlashInst,
  209. uint32_t ui32PageNum);
  210. extern int am_hal_flash_program_main(uint32_t value, uint32_t *pSrc,
  211. uint32_t *pDst, uint32_t NumberOfWords);
  212. extern int am_hal_flash_program_info(uint32_t ui32Value, uint32_t ui32InfoInst,
  213. uint32_t *pui32Src, uint32_t ui32Offset,
  214. uint32_t ui32NumWords);
  215. //
  216. // Recovery type functions for Customer INFO space.
  217. //
  218. extern int am_hal_flash_erase_info(uint32_t ui32ProgramKey,
  219. uint32_t ui32Instance);
  220. extern int am_hal_flash_erase_main_plus_info(uint32_t ui32ProgramKey,
  221. uint32_t ui32Instance);
  222. extern int am_hal_flash_erase_main_plus_info_both_instances(
  223. uint32_t ui32ProgramKey);
  224. extern void am_hal_flash_recovery(uint32_t ui32RecoveryKey);
  225. //
  226. // BOOTROM resident reader, writer and delay utility functions.
  227. //
  228. extern uint32_t am_hal_flash_load_ui32(uint32_t ui32Address);
  229. extern void am_hal_flash_store_ui32(uint32_t ui32Address, uint32_t ui32Data);
  230. extern void am_hal_flash_delay(uint32_t ui32Iterations);
  231. //
  232. // These functions update security/protection bits in the customer INFO blOCK.
  233. //
  234. extern bool am_hal_flash_customer_info_signature_check(void);
  235. extern bool am_hal_flash_info_signature_set(void);
  236. extern int32_t am_hal_flash_info_erase_disable(void);
  237. extern bool am_hal_flash_info_erase_disable_check(void);
  238. extern int32_t am_hal_flash_info_program_disable(uint32_t ui32Mask);
  239. extern uint32_t am_hal_flash_info_program_disable_get(void);
  240. extern int32_t am_hal_flash_wipe_flash_enable(void);
  241. extern bool am_hal_flash_wipe_flash_enable_check(void);
  242. extern int32_t am_hal_flash_wipe_sram_enable(void);
  243. extern bool am_hal_flash_wipe_sram_enable_check(void);
  244. extern int32_t am_hal_flash_swo_disable(void);
  245. extern bool am_hal_flash_swo_disable_check(void);
  246. extern int32_t am_hal_flash_debugger_disable(void);
  247. extern bool am_hal_flash_debugger_disable_check(void);
  248. extern int32_t am_hal_flash_copy_protect_set(uint32_t *pui32StartAddress,
  249. uint32_t *pui32StopAddress);
  250. extern bool am_hal_flash_copy_protect_check(uint32_t *pui32StartAddress,
  251. uint32_t *pui32StopAddress);
  252. extern int32_t am_hal_flash_write_protect_set(uint32_t *pui32StartAddress,
  253. uint32_t *pui32StopAddress);
  254. extern bool am_hal_flash_write_protect_check(uint32_t *pui32StartAddress,
  255. uint32_t *pui32StopAddress);
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif // AM_HAL_FLASH_H
  260. //*****************************************************************************
  261. //
  262. // End Doxygen group.
  263. //! @}
  264. //
  265. //*****************************************************************************