wm_internal_flash.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * @file wm_internal_flash.h
  3. *
  4. * @brief inter flash driver
  5. *
  6. * @author dave
  7. *
  8. * @copyright (c) 2015 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_INTERNAL_FLASH_H
  11. #define WM_INTERNAL_FLASH_H
  12. #include "wm_type_def.h"
  13. #include "wm_osal.h"
  14. typedef volatile unsigned char vu8;
  15. typedef volatile unsigned short vu16;
  16. typedef volatile unsigned long vu32;
  17. #define M8(adr) (*((vu8 *) (adr)))
  18. #define M16(adr) (*((vu16*) (adr)))
  19. #define M32(adr) (*((vu32*) (adr)))
  20. #define INSIDE_FLS_SECTOR_SIZE 0x1000
  21. #define INSIDE_FLS_PAGE_SIZE 256
  22. #define INSIDE_FLS_BASE_ADDR 0x8000000UL
  23. #define INSIDE_FLS_SECBOOT_ADDR (INSIDE_FLS_BASE_ADDR + 0x02000)
  24. enum TYPE_FLASH_ID{
  25. SPIFLASH_MID_GD = 0xC8,
  26. SPIFLASH_MID_ESMT = 0x1C,
  27. SPIFLASH_MID_PUYA = 0x85,
  28. SPIFLASH_MID_WINBOND = 0xEF,
  29. SPIFLASH_MID_FUDANMICRO = 0xA1,
  30. SPIFLASH_MID_BOYA = 0x68,
  31. SPIFLASH_MID_XMC = 0x20,
  32. SPIFLASH_MID_XTX = 0x0B,
  33. SPIFLASH_MID_TSINGTENG = 0xEB, /*UNIGROUP TSINGTENG*/
  34. };
  35. typedef union {
  36. struct {
  37. uint32_t _reserved0: 1; /*!< bit: 0 Reserved */
  38. uint32_t code_decrypt: 1; /*!< bit: 1 read code from AHB decrypt flag */
  39. uint32_t dbus_decrypt: 1; /*!< bit: 2 read data from Flash register controller decrypt flag */
  40. uint32_t data_decrypt: 1; /*!< bit: 3 read data from AHB decrypt flag */
  41. uint32_t prikey_sel: 3; /*!< bit: 4.. 6 private key selection: 0 : first one; 1 : second one; */
  42. uint32_t decrypt_start: 1; /*!< bit: 7 write 1 to start RSA decryption operation */
  43. uint32_t _reserved2: 24; /*!< bit: 8.. 31 Reserved */
  44. } b; /*!< Structure Access by bit */
  45. uint32_t w; /*!< Type Access by whole register */
  46. } FLASH_ENCRYPT_CTRL_Type;
  47. /**
  48. * @typedef struct Flash Registers
  49. */
  50. typedef struct
  51. {
  52. vu32 ACR; /**< offset 0x000 */
  53. vu32 KEYR; /**< offset 0x004 */
  54. vu32 SR; /**< offset 0x008 */
  55. vu32 CR; /**< offset 0x00C */
  56. vu32 AR; /**< offset 0x010 */
  57. } FLASH_TypeDef;
  58. #define FLASH_HS 0x00000001
  59. /** Flash Keys */
  60. #define RDPRT_KEY 0x5AA5
  61. #define FLASH_KEY1 0x57696E6E
  62. #define FLASH_KEY2 0x65724D69
  63. #define FLASH_KEY3 0x63726F21
  64. /** Flash Control Register definitions */
  65. #define FLASH_PG 0x00000001
  66. #define FLASH_PER 0x00000002
  67. #define FLASH_MER 0x00000004
  68. #define FLASH_STRT 0x00000008
  69. #define FLASH_LOCK 0x00000020
  70. #define FLASH_ERRIE 0x00000040
  71. #define FLASH_EOPIE 0x00000080
  72. /** Flash Status Register definitions */
  73. #define FLASH_BSY 0x00000001
  74. #define FLASH_PGERR 0x00000002
  75. #define FLASH_EOP 0x00000004
  76. #define TLS_FLS_STATUS_OK (0)
  77. #define TLS_FLS_STATUS_EINVAL (1)
  78. #define TLS_FLS_STATUS_EBUSY (2)
  79. #define TLS_FLS_STATUS_EPERM (3)
  80. #define TLS_FLS_STATUS_ENOSUPPORT (4)
  81. #define TLS_FLS_STATUS_EEXIST (5)
  82. #define TLS_FLS_STATUS_ENOMEM (6)
  83. #define TLS_FLS_STATUS_EOVERFLOW (7)
  84. #define TLS_FLS_STATUS_ENODEV (8)
  85. #define TLS_FLS_STATUS_EDEV (9)
  86. #define TLS_FLS_STATUS_EIO (10)
  87. #define TLS_FLS_STATUS_ENODRV (11)
  88. #define TLS_FLS_PARAM_TYPE_ID (0)
  89. #define TLS_FLS_PARAM_TYPE_SIZE (1)
  90. #define TLS_FLS_PARAM_TYPE_PAGE_SIZE (2)
  91. #define TLS_FLS_PARAM_TYPE_PROG_SIZE (3)
  92. #define TLS_FLS_PARAM_TYPE_SECTOR_SIZE (4)
  93. #define TLS_FLS_FLAG_UNDER_PROTECT (1<<0)
  94. #define TLS_FLS_FLAG_FAST_READ (1<<1)
  95. #define TLS_FLS_FLAG_AAAI (1<<2)
  96. #define CMD_START_Pos 8U /*!< CMD start position */
  97. #define CMD_START_Msk (1UL << CMD_START_Pos) /*!< CMD start Mask */
  98. typedef struct {
  99. uint16_t eraseSize;
  100. uint16_t pageSize;
  101. } FLASH_OTP_WR_PARAM_ST;
  102. /**
  103. * @struct tls_inside_fls
  104. */
  105. struct tls_inside_fls
  106. {
  107. tls_os_sem_t *fls_lock;
  108. unsigned char flashid;
  109. unsigned int density;
  110. FLASH_OTP_WR_PARAM_ST OTPWRParam;
  111. };
  112. /**
  113. * @defgroup Driver_APIs Driver APIs
  114. * @brief Driver APIs
  115. */
  116. /**
  117. * @addtogroup Driver_APIs
  118. * @{
  119. */
  120. /**
  121. * @defgroup INNER_FLASH_Driver_APIs INNER FLASH Driver APIs
  122. * @brief INNER FLASH driver APIs
  123. */
  124. /**
  125. * @addtogroup INNER_FLASH_Driver_APIs
  126. * @{
  127. */
  128. /**
  129. * @brief This function is used to unlock flash protect area [0x0~0x2000].
  130. *
  131. * @param None
  132. *
  133. * @return None
  134. *
  135. * @note None
  136. */
  137. int tls_flash_unlock(void);
  138. /**
  139. * @brief This function is used to lock flash protect area [0x0~0x2000].
  140. *
  141. * @param None
  142. *
  143. * @return None
  144. *
  145. * @note None
  146. */
  147. int tls_flash_lock(void);
  148. /**
  149. * @brief This function is used to get the flash semaphore.
  150. *
  151. * @param None
  152. *
  153. * @return None
  154. *
  155. * @note None
  156. */
  157. void tls_fls_sem_lock(void);
  158. /**
  159. * @brief This function is used to release the flash semaphore.
  160. *
  161. * @param None
  162. *
  163. * @return None
  164. *
  165. * @note None
  166. */
  167. void tls_fls_sem_unlock(void);
  168. /**
  169. * @brief This function is used to read the unique id of the internal flash.
  170. *
  171. * @param[out] uuid Specified the address to save the uuid, the length must be greater than or equals to 18 bytes.
  172. *
  173. * @retval TLS_FLS_STATUS_OK if read sucsess
  174. * @retval TLS_FLS_STATUS_EIO if read fail
  175. *
  176. * @note The uuid's length must be greater than or equals to 18 bytes.
  177. */
  178. int tls_fls_read_unique_id(unsigned char *uuid);
  179. /**
  180. * @brief This function is used to initial flash module structer.
  181. *
  182. * @param[in] None
  183. *
  184. * @retval TLS_FLS_STATUS_OK if init sucsess
  185. * @retval TLS_FLS_STATUS_EBUSY already inited
  186. * @retval TLS_FLS_STATUS_ENOMEM memory error
  187. *
  188. * @note None
  189. */
  190. int tls_fls_init(void);
  191. /**
  192. * @brief This function is used to read data from the flash.
  193. *
  194. * @param[in] addr Specifies the starting address to read from.
  195. * @param[in] buf Specified the address to save the readback data.
  196. * @param[in] len Specifies the length of the data to read.
  197. *
  198. * @retval TLS_FLS_STATUS_OK if read sucsess
  199. * @retval TLS_FLS_STATUS_EIO if read fail
  200. *
  201. * @note None
  202. */
  203. int tls_fls_read(u32 addr, u8 * buf, u32 len);
  204. /**
  205. * @brief This function is used to write data into the flash.
  206. *
  207. * @param[in] addr Specifies the starting address to write to
  208. * @param[in] buf Pointer to a byte array that is to be written
  209. * @param[in] len Specifies the length of the data to be written
  210. *
  211. * @retval TLS_FLS_STATUS_OK if write flash success
  212. * @retval TLS_FLS_STATUS_EPERM if flash struct point is null
  213. * @retval TLS_FLS_STATUS_ENODRV if flash driver is not installed
  214. * @retval TLS_FLS_STATUS_EINVAL if argument is invalid
  215. * @retval TLS_FLS_STATUS_EIO if io error
  216. *
  217. * @note None
  218. */
  219. int tls_fls_write(u32 addr, u8 * buf, u32 len);
  220. /**
  221. * @brief This function is used to erase the appointed sector
  222. *
  223. * @param[in] sector sector num of the flash, 4K bytes every sector
  224. *
  225. * @retval TLS_FLS_STATUS_OK if read sucsess
  226. * @retval other if read fail
  227. *
  228. * @note None
  229. */
  230. int tls_fls_erase(u32 sector);
  231. /**
  232. * @brief This function is used to initialize system parameter postion by flash density
  233. *
  234. * @param None
  235. *
  236. * @retval None
  237. *
  238. * @note must be called before tls_param_init
  239. */
  240. void tls_fls_sys_param_postion_init(void);
  241. /**
  242. * @brief This function is used to read data from the security registers.
  243. *
  244. * @param[in] addr Specifies the starting address to read from.
  245. * @param[in] buf Specified the address to save the readback data.
  246. * @param[in] len Specifies the length of the data to read.
  247. *
  248. * @retval TLS_FLS_STATUS_OK if read sucsess
  249. * @retval TLS_FLS_STATUS_EPERM if flash struct point is null
  250. *
  251. * @note None
  252. */
  253. int tls_fls_otp_read(u32 addr, u8 *buf, u32 len);
  254. /**
  255. * @brief This function is used to write data into the security registers.
  256. *
  257. * @param[in] addr Specifies the starting address to write to
  258. * @param[in] buf Pointer to a byte array that is to be written
  259. * @param[in] len Specifies the length of the data to be written
  260. *
  261. * @retval TLS_FLS_STATUS_OK if write the security registers success
  262. * @retval TLS_FLS_STATUS_EPERM if flash struct point is null
  263. * @retval TLS_FLS_STATUS_ENOSUPPORT if flash is not supported
  264. * @retval TLS_FLS_STATUS_EINVAL if argument is invalid
  265. * @retval TLS_FLS_STATUS_ENOMEN if no memory
  266. *
  267. * @note None
  268. */
  269. int tls_fls_otp_write(u32 addr, u8 *buf, u32 len);
  270. /**
  271. * @brief This function is used to lock the security registers.
  272. *
  273. * @param None
  274. *
  275. * @return None
  276. *
  277. * @note None
  278. */
  279. int tls_fls_otp_lock(void);
  280. #endif /* WM_INTERNAL_FLASH_H */