hal_spi_flash_defs.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _HAL_SPI_FLASH_DEFS_H_
  13. #define _HAL_SPI_FLASH_DEFS_H_
  14. #include "osi_api.h"
  15. #include "hwregs.h"
  16. #include "quec_proj_config.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. typedef enum
  21. {
  22. HAL_SPI_FLASH_TYPE_UNKNOWN,
  23. HAL_SPI_FLASH_TYPE_GD,
  24. HAL_SPI_FLASH_TYPE_WINBOND,
  25. HAL_SPI_FLASH_TYPE_XTX,
  26. HAL_SPI_FLASH_TYPE_XMCA,
  27. HAL_SPI_FLASH_TYPE_XMCC,
  28. HAL_SPI_FLASH_TYPE_XMCB,
  29. HAL_SPI_FLASH_TYPE_PUYA,
  30. HAL_SPI_FLASH_TYPE_MX,
  31. } halSpiFlashType_t;
  32. typedef enum
  33. {
  34. HAL_SPI_FLASH_UID_NONE,
  35. HAL_SPI_FLASH_UID_4BH_8,
  36. HAL_SPI_FLASH_UID_4BH_16,
  37. HAL_SPI_FLASH_UID_SFDP_80H_12,
  38. HAL_SPI_FLASH_UID_SFDP_94H_16,
  39. HAL_SPI_FLASH_UID_SFDP_194H_16,
  40. } halSpiFlashUidType_t;
  41. typedef enum
  42. {
  43. HAL_SPI_FLASH_CPID_NONE,
  44. HAL_SPI_FLASH_CPID_4BH, // byte [17:16]
  45. } halSpiFlashCpidType_t;
  46. typedef enum
  47. {
  48. HAL_SPI_FLASH_WP_NONE,
  49. HAL_SPI_FLASH_WP_GD, // GD compatible BP0/1/2/3/4/CMP
  50. HAL_SPI_FLASH_WP_XMCA, // XMCA compatible SR.2/3/4/5
  51. HAL_SPI_FLASH_WP_MX, // MX WPSEL=1
  52. } halSpiFlashWpType_t;
  53. #define DELAY_AFTER_RESET (100) // us, tRST(20), tRST_E(12ms) won't happen
  54. #define DELAY_AFTER_RELEASE_DEEP_POWER_DOWN (50) // us, tRES1(20), tRES2(20)
  55. // There are 2 styles of capacity byte:
  56. // 1. Typically, (1<<n) is the capacity bytes. For example, 0x18 is (1<<24) 16MB
  57. // 2. MX25U12832F uses 0x38 for 16MB.
  58. #define MID_MANUFACTURE(mid) ((mid)&0xff)
  59. #define MID_MEMTYPE(mid) (((mid) >> 8) & 0xff)
  60. #define MID_CAPBITS(mid) ((((mid) >> 16) & 0x1f))
  61. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  62. #define SIZE_4K (4096)
  63. #define SHIFT_BIT_NUM_4K (12) //4096=1<<12
  64. #define SIZE_8K (8192)
  65. #define SIZE_16K (16384)
  66. #define SIZE_32K (32768)
  67. #define SIZE_64K (65536)
  68. #else
  69. #define SIZE_4K (4 * 1024)
  70. #define SIZE_8K (8 * 1024)
  71. #define SIZE_16K (16 * 1024)
  72. #define SIZE_32K (32 * 1024)
  73. #define SIZE_64K (64 * 1024)
  74. #endif
  75. #if defined(CONFIG_SOC_8910) && defined(CONFIG_SUPPORT_LC_FLASH)
  76. #define SIZE_128K (128 * 1024)
  77. #define SIZE_256K (256 * 1024)
  78. #endif
  79. #define PAGE_SIZE (256)
  80. // status register bits, based on GD
  81. #define STREG_WIP REG_BIT(0)
  82. #define STREG_WEL REG_BIT(1)
  83. #define GD_SR_BP0 REG_BIT(2)
  84. #define GD_SR_BP1 REG_BIT(3)
  85. #define GD_SR_BP2 REG_BIT(4)
  86. #define GD_SR_BP3 REG_BIT(5)
  87. #define GD_SR_BP4 REG_BIT(6)
  88. #define GD_SR_SRP0 REG_BIT(7)
  89. #define GD_SR_SRP1 REG_BIT(8)
  90. #define GD_SR_QE REG_BIT(9)
  91. #define GD_SR_SUS2 REG_BIT(10)
  92. #define GD_SR_LB1 REG_BIT(11)
  93. #define GD_SR_LB2 REG_BIT(12)
  94. #define GD_SR_LB3 REG_BIT(13)
  95. #define GD_SR_CMP REG_BIT(14)
  96. #define GD_SR_SUS1 REG_BIT(15)
  97. #define XTX_SR_LB REG_BIT(10)
  98. #define XMCA_SR_BP0 REG_BIT(2)
  99. #define XMCA_SR_BP1 REG_BIT(3)
  100. #define XMCA_SR_BP2 REG_BIT(4)
  101. #define XMCA_SR_BP3 REG_BIT(5)
  102. #define XMCA_SR_EBL REG_BIT(6) // Enable boot lock
  103. #define XMCA_SR_SRP REG_BIT(7)
  104. #define XMCA_SR_OTP_TB REG_BIT(3)
  105. #define XMCA_SR_OTP_4KBL REG_BIT(4)
  106. #define XMCA_SR_OTP_LOCK REG_BIT(7)
  107. #define XMCB_SR_BP0 REG_BIT(2)
  108. #define XMCB_SR_BP1 REG_BIT(3)
  109. #define XMCB_SR_BP2 REG_BIT(4)
  110. #define XMCB_SR_BP3 REG_BIT(5)
  111. #define XMCB_SR_QE REG_BIT(6)
  112. #define XMCB_SR_SRWD REG_BIT(7)
  113. #define XMCB_FR_TBS REG_BIT(1)
  114. #define XMCB_FR_PSUS REG_BIT(2)
  115. #define XMCB_FR_ESUS REG_BIT(3)
  116. #define XMCB_FR_IRL0 REG_BIT(4)
  117. #define XMCB_FR_IRL1 REG_BIT(5)
  118. #define XMCB_FR_IRL2 REG_BIT(6)
  119. #define XMCB_FR_IRL3 REG_BIT(7)
  120. #define MX_SR_BP0 REG_BIT(2)
  121. #define MX_SR_BP1 REG_BIT(3)
  122. #define MX_SR_BP2 REG_BIT(4)
  123. #define MX_SR_BP3 REG_BIT(5)
  124. #define MX_SR_QE REG_BIT(6)
  125. #define MX_SCUR_PSB REG_BIT(2)
  126. #define MX_SCUR_ESB REG_BIT(3)
  127. #define MX_SCUR_WPSEL REG_BIT(7)
  128. #define MX_LR_SPBLKDN REG_BIT(6)
  129. #define BITSEL(sel, val) ((sel) ? (val) : 0)
  130. #define GD_WP_BITEXP(n) (BITSEL((n)&1, GD_SR_BP0) | \
  131. BITSEL((n)&2, GD_SR_BP1) | \
  132. BITSEL((n)&4, GD_SR_BP2) | \
  133. BITSEL((n)&8, GD_SR_BP3) | \
  134. BITSEL((n)&16, GD_SR_BP4) | \
  135. BITSEL((n)&32, GD_SR_CMP))
  136. #if defined(CONFIG_SOC_8910) && defined(CONFIG_SUPPORT_LC_FLASH)
  137. // This matches GD/WINBOND 32/64MB flash.
  138. #define GD_WP256M_MASK GD_WP_BITEXP(0b111111)
  139. #define GD_WP256M_NONE GD_WP_BITEXP(0b000000)
  140. #define GD_WP256M_64K GD_WP_BITEXP(0b010001)
  141. #define GD_WP256M_128K GD_WP_BITEXP(0b010010)
  142. #define GD_WP256M_256K GD_WP_BITEXP(0b010011)
  143. #define GD_WP256M_1_64 GD_WP_BITEXP(0b010100)
  144. #define GD_WP256M_1_32 GD_WP_BITEXP(0b010101)
  145. #define GD_WP256M_1_16 GD_WP_BITEXP(0b010110)
  146. #define GD_WP256M_1_8 GD_WP_BITEXP(0b010111)
  147. #define GD_WP256M_1_4 GD_WP_BITEXP(0b011000)
  148. #define GD_WP256M_1_2 GD_WP_BITEXP(0b011001)
  149. #define GD_WP512M_1_2 GD_WP_BITEXP(0b011010)
  150. #define GD_WP256M_ALL GD_WP_BITEXP(0b011111)
  151. #endif
  152. // This matches GD/WINBOND 4/8/16 MB flash. Though 8/16 MB flash
  153. // support more options to open-protect smaller area at upper address.
  154. // However, it is not worth to bringing this complexity.
  155. #define GD_WP32M_MASK GD_WP_BITEXP(0b111111)
  156. #define GD_WP32M_NONE GD_WP_BITEXP(0b000000)
  157. #define GD_WP32M_4K GD_WP_BITEXP(0b011001)
  158. #define GD_WP32M_8K GD_WP_BITEXP(0b011010)
  159. #define GD_WP32M_16K GD_WP_BITEXP(0b011011)
  160. #define GD_WP32M_32K GD_WP_BITEXP(0b011100)
  161. #define GD_WP32M_1_64 GD_WP_BITEXP(0b001001)
  162. #define GD_WP32M_1_32 GD_WP_BITEXP(0b001010)
  163. #define GD_WP32M_1_16 GD_WP_BITEXP(0b001011)
  164. #define GD_WP32M_1_8 GD_WP_BITEXP(0b001100)
  165. #define GD_WP32M_1_4 GD_WP_BITEXP(0b001101)
  166. #define GD_WP32M_1_2 GD_WP_BITEXP(0b001110)
  167. #define GD_WP32M_3_4 GD_WP_BITEXP(0b100101)
  168. #define GD_WP32M_7_8 GD_WP_BITEXP(0b100100)
  169. #define GD_WP32M_15_16 GD_WP_BITEXP(0b100011)
  170. #define GD_WP32M_31_32 GD_WP_BITEXP(0b100010)
  171. #define GD_WP32M_63_64 GD_WP_BITEXP(0b100001)
  172. #define GD_WP32M_ALL GD_WP_BITEXP(0b011111)
  173. // This matches GD/WINBOND 2MB flash.
  174. #define GD_WP16M_MASK GD_WP_BITEXP(0b111111)
  175. #define GD_WP16M_NONE GD_WP_BITEXP(0b000000)
  176. #define GD_WP16M_4K GD_WP_BITEXP(0b011001)
  177. #define GD_WP16M_8K GD_WP_BITEXP(0b011010)
  178. #define GD_WP16M_16K GD_WP_BITEXP(0b011011)
  179. #define GD_WP16M_32K GD_WP_BITEXP(0b011100)
  180. #define GD_WP16M_1_32 GD_WP_BITEXP(0b001001)
  181. #define GD_WP16M_1_16 GD_WP_BITEXP(0b001010)
  182. #define GD_WP16M_1_8 GD_WP_BITEXP(0b001011)
  183. #define GD_WP16M_1_4 GD_WP_BITEXP(0b001100)
  184. #define GD_WP16M_1_2 GD_WP_BITEXP(0b001101)
  185. #define GD_WP16M_3_4 GD_WP_BITEXP(0b100100)
  186. #define GD_WP16M_7_8 GD_WP_BITEXP(0b100011)
  187. #define GD_WP16M_15_16 GD_WP_BITEXP(0b100010)
  188. #define GD_WP16M_31_32 GD_WP_BITEXP(0b100001)
  189. #define GD_WP16M_ALL GD_WP_BITEXP(0b011111)
  190. // This matches GD/WINBOND 1MB flash.
  191. #define GD_WP8M_MASK GD_WP_BITEXP(0b111111)
  192. #define GD_WP8M_NONE GD_WP_BITEXP(0b000000)
  193. #define GD_WP8M_4K GD_WP_BITEXP(0b011001)
  194. #define GD_WP8M_8K GD_WP_BITEXP(0b011010)
  195. #define GD_WP8M_16K GD_WP_BITEXP(0b011011)
  196. #define GD_WP8M_32K GD_WP_BITEXP(0b011100)
  197. #define GD_WP8M_1_16 GD_WP_BITEXP(0b001001)
  198. #define GD_WP8M_1_8 GD_WP_BITEXP(0b001010)
  199. #define GD_WP8M_1_4 GD_WP_BITEXP(0b001011)
  200. #define GD_WP8M_1_2 GD_WP_BITEXP(0b001100)
  201. #define GD_WP8M_3_4 GD_WP_BITEXP(0b100011)
  202. #define GD_WP8M_7_8 GD_WP_BITEXP(0b100010)
  203. #define GD_WP8M_15_16 GD_WP_BITEXP(0b100001)
  204. #define GD_WP8M_ALL GD_WP_BITEXP(0b011111)
  205. #define XMCA_WP_BITEXP(n) (BITSEL((n)&1, XMCA_SR_BP0) | \
  206. BITSEL((n)&2, XMCA_SR_BP1) | \
  207. BITSEL((n)&4, XMCA_SR_BP2) | \
  208. BITSEL((n)&8, XMCA_SR_BP3))
  209. #define XMCA_WP_MASK XMCA_WP_BITEXP(0b1111)
  210. #define XMCA_WP_NONE XMCA_WP_BITEXP(0b0000)
  211. #define XMCA_WP_1_128 XMCA_WP_BITEXP(0b0001)
  212. #define XMCA_WP_2_128 XMCA_WP_BITEXP(0b0010)
  213. #define XMCA_WP_4_128 XMCA_WP_BITEXP(0b0011)
  214. #define XMCA_WP_8_128 XMCA_WP_BITEXP(0b0100)
  215. #define XMCA_WP_16_128 XMCA_WP_BITEXP(0b0101)
  216. #define XMCA_WP_32_128 XMCA_WP_BITEXP(0b0110)
  217. #define XMCA_WP_64_128 XMCA_WP_BITEXP(0b0111)
  218. #define XMCA_WP_96_128 XMCA_WP_BITEXP(0b1000)
  219. #define XMCA_WP_112_128 XMCA_WP_BITEXP(0b1001)
  220. #define XMCA_WP_120_128 XMCA_WP_BITEXP(0b1010)
  221. #define XMCA_WP_124_128 XMCA_WP_BITEXP(0b1011)
  222. #define XMCA_WP_126_128 XMCA_WP_BITEXP(0b1100)
  223. #define XMCA_WP_127_128 XMCA_WP_BITEXP(0b1101)
  224. #define XMCA_WP_ALL XMCA_WP_BITEXP(0b1111)
  225. #define MX_WP_BITEXP(n) (BITSEL((n)&1, XMCA_SR_BP0) | \
  226. BITSEL((n)&2, XMCA_SR_BP1) | \
  227. BITSEL((n)&4, XMCA_SR_BP2) | \
  228. BITSEL((n)&8, XMCA_SR_BP3))
  229. #define MX_WP_MASK MX_WP_BITEXP(0b1111)
  230. #define MX_WP_NONE MX_WP_BITEXP(0b0000)
  231. #define MX_WP_1_BLOCK MX_WP_BITEXP(0b0001)
  232. #define MX_WP_2_BLOCK MX_WP_BITEXP(0b0010)
  233. #define MX_WP_4_BLOCK MX_WP_BITEXP(0b0011)
  234. #define MX_WP_8_BLOCK MX_WP_BITEXP(0b0100)
  235. #define MX_WP_16_BLOCK MX_WP_BITEXP(0b0101)
  236. #define MX_WP_32_BLOCK MX_WP_BITEXP(0b0110)
  237. #define MX_WP_64_BLOCK MX_WP_BITEXP(0b0111)
  238. #define MX_WP_128_BLOCK MX_WP_BITEXP(0b1000)
  239. #define MX_WP_ALL MX_WP_BITEXP(0b1111)
  240. #define OPNAND_WRITE_ENABLE 0x06
  241. #define OPNAND_WRITE_DISABLE 0x04
  242. #define OPNAND_GET_FEATURE 0x0f
  243. #define OPNAND_SET_FEATURE 0x1f
  244. #define OPNAND_PAGE_READ_TO_CACHE 0x13
  245. #define OPNAND_PAGE_READ 0x03 // or 0x0b
  246. #define OPNAND_PAGE_READ_X4 0x6b
  247. #define OPNAND_PAGE_READ_QUAD 0xeb
  248. #define OPNAND_READ_ID 0x9f
  249. #define OPNAND_PROGRAM_LOAD 0x02
  250. #define OPNAND_PROGRAM_LOAD_X4 0x32
  251. #define OPNAND_PROGRAM_EXEC 0x10
  252. #define OPNAND_PROGRAM_LOAD_RANDOM 0x84
  253. #define OPNAND_PROGRAM_LOAD_RANDOM_X4 0x34 // or 0xc4
  254. #define OPNAND_PROGRAM_LOAD_RANDOM_QUAD 0x72
  255. #define OPNAND_BLOCK_ERASE 0xd8
  256. #define OPNAND_RESET 0xff
  257. #ifdef __cplusplus
  258. }
  259. #endif
  260. #endif