wm_internal_flash.h 10 KB

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