gd_sflash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*!
  2. *******************************************************************************
  3. **
  4. ** \file gd_sflash.h
  5. **
  6. ** \brief Serail Flash memory driver.
  7. **
  8. ** (C) Goke Microelectronics China 2002 - 2007
  9. **
  10. ** \attention THIS SAMPLE CODE IS PROVIDED AS IS. GOKE MICROELECTRONICS
  11. ** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
  12. ** OMMISSIONS.
  13. **
  14. *******************************************************************************
  15. */
  16. #ifndef GD_SFLASH_H
  17. #define GD_SFLASH_H
  18. #include <gtypes.h>
  19. #include <gmodids.h>
  20. #include "gd_spi.h"
  21. /*!
  22. *******************************************************************************
  23. **
  24. ** \brief Flash channel number.
  25. **
  26. *******************************************************************************
  27. */
  28. typedef enum
  29. {
  30. GD_SFLASH_CHANNEL_0 = 0, // used gk6202 sflash module, channel 0
  31. GD_SFLASH_CHANNEL_1, // used gk6202 sflash module, channel 1
  32. GD_SFLASH_SPI_CHANNEL_0_0, // used gk7101 spi module, spi 0, channel 0
  33. GD_SFLASH_SPI_CHANNEL_0_1, // used gk7101 spi module, spi 0, channel 1
  34. GD_SFLASH_SPI_CHANNEL_0_4, // used gk7101 spi module, spi 0, channel 4
  35. GD_SFLASH_SPI_CHANNEL_0_5, // used gk7101 spi module, spi 0, channel 5
  36. GD_SFLASH_SPI_CHANNEL_0_6, // used gk7101 spi module, spi 0, channel 6
  37. GD_SFLASH_SPI_CHANNEL_0_7, // used gk7101 spi module, spi 0, channel 7
  38. GD_SFLASH_SPI_CHANNEL_1_0, // used gk7101 spi module, spi 1, channel 0
  39. GD_SFLASH_CHANNEL_NUM
  40. }GD_SFLASH_CHANNEL_E;
  41. /*!
  42. *******************************************************************************
  43. **
  44. ** \brief Flash IOx Mode.
  45. **
  46. *******************************************************************************
  47. */
  48. typedef enum
  49. {
  50. GD_SFLASH_IO1_MODE = 1,
  51. GD_SFLASH_IO2_MODE = 2,
  52. GD_SFLASH_IO4_MODE = 4
  53. }GD_SFLASH_IO_MODE_E;
  54. /*!
  55. *******************************************************************************
  56. **
  57. ** \brief Flash read or write.
  58. **
  59. *******************************************************************************
  60. */
  61. typedef enum
  62. {
  63. GD_SFLASH_READ = 0,
  64. GD_SFLASH_WRITE
  65. }GD_SFLASH_RW_E;
  66. /*!
  67. *******************************************************************************
  68. **
  69. ** \brief Flash Type.
  70. **
  71. *******************************************************************************
  72. */
  73. typedef enum
  74. {
  75. GD_SPI_NOR = 0, // normal SPI nor-flash
  76. GD_SPI_NAND // SPI nand-flash
  77. }GD_SFLASH_TYPE_E;
  78. /*
  79. *******************************************************************************
  80. **
  81. ** serial flash specific commands and statis register bit definitions
  82. **
  83. *******************************************************************************
  84. */
  85. typedef struct
  86. {
  87. U8 readID; // command to read the chip identification
  88. U8 writeEnable; // command to enable a write/erase sequence
  89. U8 writeDisable; // command to disable a write/erase sequence
  90. U8 readStatus; // command to read from status register
  91. U8 writeStatus; // command to write to status register
  92. U8 readData; // command to read data
  93. U8 readDataFast; // command to read data in fast mode
  94. U8 eraseSector; // command to erase a single sector
  95. U8 eraseChip; // command to erase the entire chip
  96. U8 programPage; // command to program a sector page
  97. U32 statusMaskWIP; // status register mask for bit write-in-progress
  98. U32 statusMaskWEL; // status register mask for bit write-enable-latch
  99. U8 readData2; // command to read data by IO2
  100. U8 readData4; // command to read data by IO4
  101. U8 programPage2; // command to program a sector page by IO2
  102. U8 programPage4; // command to program a sector page by IO4
  103. U8 pageRead; // command to read page to cache, new added for spi-nand
  104. U8 programExecute;// command to execute program from cache to page, new added for spi-nand
  105. }GD_SFLASH_CMD_S;
  106. /*
  107. *******************************************************************************
  108. **
  109. ** serial flash specific geometry and information data structure
  110. **
  111. *******************************************************************************
  112. */
  113. typedef struct
  114. {
  115. U8 manufactureID; // Manufacture identification
  116. U16 deviceID; // Device identification (memory type/capacity)
  117. char* manufactureName; // Pointer to manufacture name
  118. char* deviceName; // Pointer to device name
  119. U32 deviceBytes; // Size of flash device in bytes
  120. U32 sectorCount; // Number of sectors
  121. U32 sectorBytes; // Size of a single flash sector in bytes
  122. U32 sectorPages; // Number of pages per sector
  123. U32 pageBytes; // Size of a programmable page in bytes
  124. GD_SFLASH_CMD_S* commands; // Device specific access commands
  125. U32 ioMode; // bit[0:3] for read, bit[4:7] for write
  126. GD_SFLASH_TYPE_E type; // SPI nor flash or nand flash
  127. GD_SFLASH_CHANNEL_E channel; //
  128. U32 devicechannel; // SFLAH/SPI CS channel
  129. }GD_SFLASH_DEV_S;
  130. typedef struct
  131. {
  132. void (*GH_SFLASH_set_Handle)(GD_SPI_STATUS_PARAMS_S*);
  133. void (*GH_SFLASH_set_Command)(U32);
  134. void (*GH_SFLASH_set_Data)(U32);
  135. U32 (*GH_SFLASH_get_Data)(void);
  136. void (*GH_SFLASH_set_CE)(U32);
  137. U32 (*GH_SFLASH_get_CE)(void);
  138. U8 (*GH_SFLASH_get_CE_CHSELECT)(void);
  139. void (*GH_SFLASH_set_Speed)(U32);
  140. void (*GH_SFLASH_set_CE_CHSELECT)(U8);
  141. }GD_SFLASH_FUNC_S;
  142. /*!
  143. *******************************************************************************
  144. **
  145. ** \anchor gd_sflash_error_base
  146. ** \brief he base error code for the serial flash device driver.
  147. **
  148. *******************************************************************************
  149. */
  150. #define GD_SFLASH_ERR_BASE (GD_SFLASH_MODULE_ID<<16)
  151. /*!
  152. *******************************************************************************
  153. **
  154. ** \brief Flash driver error codes.
  155. **
  156. *******************************************************************************
  157. */
  158. enum
  159. {
  160. GD_ERR_SFLASH_TYPE_NOT_SUPPORTED = GD_SFLASH_ERR_BASE, //!< Device not supported.
  161. GD_ERR_SFLASH_IN_USE, //!< Read error.
  162. GD_ERR_SFLASH_READ, //!< Read error.
  163. GD_ERR_SFLASH_WRITE, //!< Write error.
  164. GD_ERR_SFLASH_ERASE,
  165. GD_ERR_UNLOCK_FAIL
  166. };
  167. // TODO: to be confirmed for each clock value
  168. // FPGA: SCLK = 40MHz
  169. // EVM: SCLK = 135MHz
  170. // 000: SCLK/2
  171. // 001: SCLK/4
  172. // 010: SCLK/6
  173. // 011: SCLK/8
  174. // 100: SCLK/10
  175. typedef enum
  176. {
  177. GD_SFLASH_FREQ_DIV2 = 0,
  178. GD_SFLASH_FREQ_DIV4,
  179. GD_SFLASH_FREQ_DIV6,
  180. GD_SFLASH_FREQ_DIV8,
  181. GD_SFLASH_FREQ_DIV10,
  182. }GD_SFLASH_SPEED_MODE;
  183. /*-------------------------------------------------------------------------------*/
  184. /* \brief Flash I/O feature. */
  185. /*-------------------------------------------------------------------------------*/
  186. typedef enum
  187. {
  188. GD_SFLASH_FEATURE_IO1 = 0,
  189. GD_SFLASH_FEATURE_IO2,
  190. GD_SFLASH_FEATURE_IO4
  191. }GD_SFLASH_FEATURE;
  192. /*******************************************************************************************************************************************
  193. * sflash cmd attribute
  194. * sflash cmd register bit definition
  195. * bit 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17
  196. | rsrd | hold time | transfer data bytes |data cycle|adr and dummy cycle | cmd cycle |
  197. 00-100ns 11111 -- 4 bytes 00 -x1
  198. 01-3us other(e.g.,n) -- n bytes 0-3 cycle 0-3 cycle 01 -x2
  199. 10-100us 10 -x4
  200. bit 16 15 14 13 12 11 10 9 8 [7: 0]
  201. | RWN | dummy cycle number | adr byte num | | cmd to DF |
  202. 00 - rd data for SF 0 - 7 bytes cycle 0-7 bytes 1 - send cmd
  203. 01 - wr data to SF 0 - no send cmd see specific flash cmd
  204. 11 - nothing to do
  205. ********************************************************************************************************************************************/
  206. /* send cmd or not [ 8]*/
  207. #define SFLASH_SEND_CMD 0x00000100
  208. #define SFLASH_NO_SEND_CMD 0x00000000
  209. /* byte number of address to send [11:9]*/
  210. #define SFLASH_SEND_ADDR_BYTE_NUM_0 0x00000000
  211. #define SFLASH_SEND_ADDR_BYTE_NUM_1 0x00000200
  212. #define SFLASH_SEND_ADDR_BYTE_NUM_2 0x00000400
  213. #define SFLASH_SEND_ADDR_BYTE_NUM_3 0x00000600
  214. #define SFLASH_SEND_ADDR_BYTE_NUM_4 0x00000800
  215. #define SFLASH_SEND_ADDR_BYTE_NUM_5 0x00000a00
  216. #define SFLASH_SEND_ADDR_BYTE_NUM_6 0x00000c00
  217. #define SFLASH_SEND_ADDR_BYTE_NUM_7 0x00000e00
  218. /* byte number of dummy cycle to send [14:12] */
  219. #define SFLASH_SEND_DUMMY_BYTE_NUM_0 0x000000000
  220. #define SFLASH_SEND_DUMMY_BYTE_NUM_1 0x000001000
  221. #define SFLASH_SEND_DUMMY_BYTE_NUM_2 0x000002000
  222. #define SFLASH_SEND_DUMMY_BYTE_NUM_3 0x000003000
  223. #define SFLASH_SEND_DUMMY_BYTE_NUM_4 0x000004000
  224. #define SFLASH_SEND_DUMMY_BYTE_NUM_5 0x000005000
  225. #define SFLASH_SEND_DUMMY_BYTE_NUM_6 0x000006000
  226. #define SFLASH_SEND_DUMMY_BYTE_NUM_7 0x000007000
  227. /* command operation[16:15]: 00 for read data from SF; 01 for write data to SF; 11 for nothing to do */
  228. #define SFLASH_RWN_READ 0x00000000
  229. #define SFLASH_RWN_WRITE 0x00008000
  230. #define SFLASH_RWN_NOTHING 0x00018000
  231. /* I/O mode of command cycle to SF[18:17]: 00 for x1; 01 for x2; 10 for x4 */
  232. #define SFLASH_CMD_MODE_1X 0x00000000
  233. #define SFLASH_CMD_MODE_2X 0x00020000
  234. #define SFLASH_CMD_MODE_4X 0x00040000
  235. /* I/O mode of address and dummy cycle to SF[20:19] */
  236. #define SFLASH_ADDR_DUMMY_CYCLE_NUM_0 0x00000000
  237. #define SFLASH_ADDR_DUMMY_CYCLE_NUM_1 0x00080000
  238. #define SFLASH_ADDR_DUMMY_CYCLE_NUM_2 0x00100000
  239. #define SFLASH_ADDR_DUMMY_CYCLE_NUM_3 0x00180000
  240. /* I/O mode of data cycle to or from SF [22:21] */
  241. #define SFLASH_DATA_CYCLE_NUM_0 0x00000000
  242. #define SFLASH_DATA_CYCLE_NUM_1 0x00200000
  243. #define SFLASH_DATA_CYCLE_NUM_2 0x00400000
  244. #define SFLASH_DATA_CYCLE_NUM_3 0x00600000
  245. /* transfer data byte number to or from SF[27:23]. For 11111 case, transfer 4bytes per request */
  246. /* for other case, transfer number bytes */
  247. #define SFLASH_TRANSFER_BYTE_NUM_1 0x00800000
  248. #define SFLASH_TRANSFER_BYTE_NUM_2 0x01000000
  249. #define SFLASH_TRANSFER_BYTE_NUM_3 0x01800000
  250. #define SFLASH_TRANSFER_BYTE_NUM_4 0x0f800000
  251. #define SFLASH_TRANSFER_BYTE_LOC 23
  252. #define SFLASH_HOLD_TIME_100ns 0x00000000
  253. #define SFLASH_HOLD_TIME_3us 0x10000000
  254. #define SFLASH_HOLD_TIME_100us 0x20000000
  255. #define GD_SFLASH_1X_READ 0x01
  256. #define GD_SFLASH_2X_READ 0x02
  257. #define GD_SFLASH_4X_READ 0x04
  258. #define GD_SFLASH_1X_WRITE 0x10
  259. #define GD_SFLASH_2X_WRITE 0x20
  260. #define GD_SFLASH_4X_WRITE 0x40
  261. /*
  262. *******************************************************************************
  263. *******************************************************************************
  264. **
  265. ** Generic serial flash specific API functions
  266. **
  267. *******************************************************************************
  268. *******************************************************************************
  269. */
  270. #ifdef __cplusplus
  271. extern "C" {
  272. #endif
  273. GERR GD_SFLASH_Init(void);
  274. GERR GD_SFLASH_Exit(void);
  275. GERR GD_SFLASH_Open(GD_HANDLE* pHandle, GD_SFLASH_SPEED_MODE speed_mode, GD_SFLASH_CHANNEL_E channel);
  276. GERR GD_SFLASH_Close(GD_HANDLE* pHandle);
  277. GERR GD_SFLASH_Read(GD_HANDLE handle, U32 address, U32* buffer, U32 words);
  278. GERR GD_SFLASH_Write(GD_HANDLE handle, U32 address, U32* buffer, U32 words);
  279. GERR GD_SFLASH_Program(GD_HANDLE handle, U32 address, U32* buffer, U32 words);
  280. GERR GD_SFLASH_EraseChip(GD_HANDLE handle);
  281. GERR GD_SFLASH_EraseSector(GD_HANDLE handle, U16 sector);
  282. GERR GD_SFLASH_GetCodes(GD_HANDLE handle, U8* manufactureCode, U16* deviceCode);
  283. GERR GD_SFLASH_GetNames(GD_HANDLE handle, char** manufactureName, char** deviceName);
  284. GERR GD_SFLASH_GetNumberOfSectors(GD_HANDLE handle, U16* numberOfSectors);
  285. GERR GD_SFLASH_GetSectorAddress(GD_HANDLE handle, U16 sector, U32* address);
  286. GERR GD_SFLASH_GetSize(GD_HANDLE handle, U32* deviceWords);
  287. GERR GD_SFLASH_GetSectorSize(GD_HANDLE handle, U16 sector, U32* deviceWords);
  288. GERR GD_SFLASH_GetSector(GD_HANDLE handle, U32 address, U16* sector);
  289. GERR GD_SFLASH_IsChipEmpty(GD_HANDLE handle, GBOOL* isEmpty);
  290. GERR GD_SFLASH_IsSectorEmpty(GD_HANDLE handle, U16 sector, GBOOL* isSectorEmpty);
  291. GERR GD_SFLASH_Unlock(GD_HANDLE handle);
  292. GERR GD_SFLASH_Lock(GD_HANDLE handle);
  293. GERR GD_SFLASH_IsChipLocked(GD_HANDLE handle, GBOOL* isChipLocked);
  294. GERR GD_SFLASH_GetIoMode(GD_HANDLE handle, GD_SFLASH_RW_E op, GD_SFLASH_IO_MODE_E* ioMode);
  295. GERR GD_SFLASH_SetIoMode(GD_HANDLE handle, GD_SFLASH_RW_E op, GD_SFLASH_IO_MODE_E ioMode);
  296. GERR GD_SFLASH_SetResetMode(GD_HANDLE handle, GBOOL mode);
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #endif