opi_flash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * copyright (c) Espressif System 2019
  3. *
  4. */
  5. #ifndef _ROM_OPI_FLASH_H_
  6. #define _ROM_OPI_FLASH_H_
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include "spi_flash.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. typedef struct {
  16. uint8_t mode;
  17. uint8_t cmd_bit_len;
  18. uint16_t cmd;
  19. uint32_t addr;
  20. uint8_t addr_bit_len;
  21. uint8_t dummy_bit_len;
  22. uint8_t data_bit_len;
  23. uint8_t cs_sel: 4;
  24. uint8_t is_pe: 4;
  25. } esp_rom_opiflash_cmd_t;
  26. typedef struct {
  27. uint8_t addr_bit_len;
  28. uint8_t dummy_bit_len;
  29. uint16_t cmd;
  30. uint8_t cmd_bit_len;
  31. uint8_t var_dummy_en;
  32. } esp_rom_opiflash_spi0rd_t;
  33. typedef struct {
  34. esp_rom_opiflash_cmd_t rdid;
  35. esp_rom_opiflash_cmd_t rdsr;
  36. esp_rom_opiflash_cmd_t wren;
  37. esp_rom_opiflash_cmd_t se;
  38. esp_rom_opiflash_cmd_t be64k;
  39. esp_rom_opiflash_cmd_t read;
  40. esp_rom_opiflash_cmd_t pp;
  41. esp_rom_opiflash_spi0rd_t cache_rd_cmd;
  42. } esp_rom_opiflash_def_t;
  43. typedef struct {
  44. uint16_t cmd; /*!< Command value */
  45. uint16_t cmdBitLen; /*!< Command byte length*/
  46. uint32_t *addr; /*!< Point to address value*/
  47. uint32_t addrBitLen; /*!< Address byte length*/
  48. uint32_t *txData; /*!< Point to send data buffer*/
  49. uint32_t txDataBitLen; /*!< Send data byte length.*/
  50. uint32_t *rxData; /*!< Point to recevie data buffer*/
  51. uint32_t rxDataBitLen; /*!< Recevie Data byte length.*/
  52. uint32_t dummyBitLen;
  53. } esp_rom_spi_cmd_t;
  54. #define ESP_ROM_OPIFLASH_MUX_TAKE()
  55. #define ESP_ROM_OPIFLASH_MUX_GIVE()
  56. #define ESP_ROM_OPIFLASH_SEL_CS0 (BIT(0))
  57. #define ESP_ROM_OPIFLASH_SEL_CS1 (BIT(1))
  58. // Definition of MX25UM25645G Octa Flash
  59. // SPI status register
  60. #define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0
  61. #define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1
  62. #define ESP_ROM_SPIFLASH_BP0 BIT2
  63. #define ESP_ROM_SPIFLASH_BP1 BIT3
  64. #define ESP_ROM_SPIFLASH_BP2 BIT4
  65. #define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2)
  66. #define ESP_ROM_SPIFLASH_QE BIT9
  67. #define FLASH_OP_MODE_RDCMD_DOUT 0x3B
  68. #define ESP_ROM_FLASH_SECTOR_SIZE 0x1000
  69. #define ESP_ROM_FLASH_BLOCK_SIZE_64K 0x10000
  70. #define ESP_ROM_FLASH_PAGE_SIZE 256
  71. // FLASH commands
  72. #define ROM_FLASH_CMD_RDID 0x9F
  73. #define ROM_FLASH_CMD_WRSR 0x01
  74. #define ROM_FLASH_CMD_WRSR2 0x31 /* Not all SPI flash uses this command */
  75. #define ROM_FLASH_CMD_WREN 0x06
  76. #define ROM_FLASH_CMD_WRDI 0x04
  77. #define ROM_FLASH_CMD_RDSR 0x05
  78. #define ROM_FLASH_CMD_RDSR2 0x35 /* Not all SPI flash uses this command */
  79. #define ROM_FLASH_CMD_ERASE_SEC 0x20
  80. #define ROM_FLASH_CMD_ERASE_BLK_32K 0x52
  81. #define ROM_FLASH_CMD_ERASE_BLK_64K 0xD8
  82. #define ROM_FLASH_CMD_OTPEN 0x3A /* Enable OTP mode, not all SPI flash uses this command */
  83. #define ROM_FLASH_CMD_RSTEN 0x66
  84. #define ROM_FLASH_CMD_RST 0x99
  85. #define ROM_FLASH_CMD_SE4B 0x21
  86. #define ROM_FLASH_CMD_SE4B_OCT 0xDE21
  87. #define ROM_FLASH_CMD_BE4B 0xDC
  88. #define ROM_FLASH_CMD_BE4B_OCT 0x23DC
  89. #define ROM_FLASH_CMD_RSTEN_OCT 0x9966
  90. #define ROM_FLASH_CMD_RST_OCT 0x6699
  91. #define ROM_FLASH_CMD_FSTRD4B_STR 0x13EC
  92. #define ROM_FLASH_CMD_FSTRD4B_DTR 0x11EE
  93. #define ROM_FLASH_CMD_FSTRD4B 0x0C
  94. #define ROM_FLASH_CMD_PP4B 0x12
  95. #define ROM_FLASH_CMD_PP4B_OCT 0xED12
  96. #define ROM_FLASH_CMD_RDID_OCT 0x609F
  97. #define ROM_FLASH_CMD_WREN_OCT 0xF906
  98. #define ROM_FLASH_CMD_RDSR_OCT 0xFA05
  99. #define ROM_FLASH_CMD_RDCR2 0x71
  100. #define ROM_FLASH_CMD_RDCR2_OCT 0x8E71
  101. #define ROM_FLASH_CMD_WRCR2 0x72
  102. #define ROM_FLASH_CMD_WRCR2_OCT 0x8D72
  103. // Definitions for GigaDevice GD25LX256E Flash
  104. #define ROM_FLASH_CMD_RDFSR_GD 0x70
  105. #define ROM_FLASH_CMD_RD_GD 0x03
  106. #define ROM_FLASH_CMD_RD4B_GD 0x13
  107. #define ROM_FLASH_CMD_FSTRD_GD 0x0B
  108. #define ROM_FLASH_CMD_FSTRD4B_GD 0x0C
  109. #define ROM_FLASH_CMD_FSTRD_OOUT_GD 0x8B
  110. #define ROM_FLASH_CMD_FSTRD4B_OOUT_GD 0x7C
  111. #define ROM_FLASH_CMD_FSTRD_OIOSTR_GD 0xCB
  112. #define ROM_FLASH_CMD_FSTRD4B_OIOSTR_GD 0xCC
  113. #define ROM_FLASH_CMD_FSTRD4B_OIODTR_GD 0xFD
  114. #define ROM_FLASH_CMD_PP_GD 0x02
  115. #define ROM_FLASH_CMD_PP4B_GD 0x12
  116. #define ROM_FLASH_CMD_PP_OOUT_GD 0x82
  117. #define ROM_FLASH_CMD_PP4B_OOUT_GD 0x84
  118. #define ROM_FLASH_CMD_PP_OIO_GD 0xC2
  119. #define ROM_FLASH_CMD_PP4B_OIOSTR_GD 0x8E
  120. #define ROM_FLASH_CMD_SE_GD 0x20
  121. #define ROM_FLASH_CMD_SE4B_GD 0x21
  122. #define ROM_FLASH_CMD_BE32K_GD 0x52
  123. #define ROM_FLASH_CMD_BE32K4B_GD 0x5C
  124. #define ROM_FLASH_CMD_BE64K_GD 0xD8
  125. #define ROM_FLASH_CMD_BE64K4B_GD 0xDC
  126. #define ROM_FLASH_CMD_EN4B_GD 0xB7
  127. #define ROM_FLASH_CMD_DIS4B_GD 0xE9
  128. extern const esp_rom_opiflash_def_t *rom_opiflash_cmd_def;
  129. /**
  130. * @brief init legacy driver for Octal Flash
  131. */
  132. void esp_rom_opiflash_legacy_driver_init(const esp_rom_opiflash_def_t *flash_cmd_def);
  133. // spi user mode command config
  134. /**
  135. * @brief Config the spi user command
  136. * @param spi_num spi port
  137. * @param pcmd pointer to accept the spi command struct
  138. */
  139. void esp_rom_spi_cmd_config(int spi_num, esp_rom_spi_cmd_t* pcmd);
  140. /**
  141. * @brief Start a spi user command sequence
  142. * @param spi_num spi port
  143. * @param rx_buf buffer pointer to receive data
  144. * @param rx_len receive data length in byte
  145. * @param cs_en_mask decide which cs to use, 0 for cs0, 1 for cs1
  146. * @param is_write_erase to indicate whether this is a write or erase operation, since the CPU would check permission
  147. */
  148. void esp_rom_spi_cmd_start(int spi_num, uint8_t* rx_buf, uint16_t rx_len, uint8_t cs_en_mask, bool is_write_erase);
  149. /**
  150. * @brief Config opi flash pads according to efuse settings.
  151. */
  152. void esp_rom_opiflash_pin_config(void);
  153. // set SPI read/write mode
  154. /**
  155. * @brief Set SPI operation mode
  156. * @param spi_num spi port
  157. * @param mode Flash Read Mode
  158. */
  159. void esp_rom_spi_set_op_mode(int spi_num, esp_rom_spiflash_read_mode_t mode);
  160. /**
  161. * @brief Set data swap mode in DTR(DDR) mode
  162. * @param spi_num spi port
  163. * @param wr_swap to decide whether to swap fifo data in dtr write operation
  164. * @param rd_swap to decide whether to swap fifo data in dtr read operation
  165. */
  166. void esp_rom_spi_set_dtr_swap_mode(int spi, bool wr_swap, bool rd_swap);
  167. /**
  168. * @brief to send reset command in spi/opi-str/opi-dtr mode(for MX25UM25645G)
  169. * @param spi_num spi port
  170. */
  171. void esp_rom_opiflash_mode_reset(int spi_num);
  172. /**
  173. * @brief To execute a flash operation command
  174. * @param spi_num spi port
  175. * @param mode Flash Read Mode
  176. * @param cmd data to send in command field
  177. * @param cmd_bit_len bit length of command field
  178. * @param addr data to send in address field
  179. * @param addr_bit_len bit length of address field
  180. * @param dummy_bits bit length of dummy field
  181. * @param mosi_data data buffer to be sent in mosi field
  182. * @param mosi_bit_len bit length of data buffer to be sent in mosi field
  183. * @param miso_data data buffer to accept data in miso field
  184. * @param miso_bit_len bit length of data buffer to accept data in miso field
  185. * @param cs_mark decide which cs pin to use. 0: cs0, 1: cs1
  186. * @param is_write_erase_operation to indicate whether this a write or erase flash operation
  187. */
  188. void esp_rom_opiflash_exec_cmd(int spi_num, esp_rom_spiflash_read_mode_t mode,
  189. uint32_t cmd, int cmd_bit_len,
  190. uint32_t addr, int addr_bit_len,
  191. int dummy_bits,
  192. uint8_t* mosi_data, int mosi_bit_len,
  193. uint8_t* miso_data, int miso_bit_len,
  194. uint32_t cs_mask,
  195. bool is_write_erase_operation);
  196. /**
  197. * @brief send reset command to opi flash
  198. * @param spi_num spi port
  199. * @param mode Flash Operation Mode
  200. */
  201. void esp_rom_opiflash_soft_reset(int spi_num, esp_rom_spiflash_read_mode_t mode);
  202. /**
  203. * @brief to read opi flash ID
  204. * @note command format would be defined in initialization
  205. * @param[out] out_id buffer to accept id
  206. * @return flash operation result
  207. */
  208. esp_rom_spiflash_result_t esp_rom_opiflash_read_id(uint8_t *out_id);
  209. /**
  210. * @brief to read opi flash status register
  211. * @note command format would be defined in initialization
  212. * @return opi flash status value
  213. */
  214. uint8_t esp_rom_opiflash_rdsr(void);
  215. /**
  216. * @brief wait opi flash status register to be idle
  217. * @note command format would be defined in initialization
  218. * @return flash operation result
  219. */
  220. esp_rom_spiflash_result_t esp_rom_opiflash_wait_idle(void);
  221. /**
  222. * @brief to erase flash sector
  223. * @note command format would be defined in initialization
  224. * @param sector_num the sector to be erased
  225. * @return flash operation result
  226. */
  227. esp_rom_spiflash_result_t esp_rom_opiflash_erase_sector(uint32_t sector_num);
  228. /**
  229. * @brief to erase flash block
  230. * @note command format would be defined in initialization
  231. * @param block_num the block to be erased
  232. * @return flash operation result
  233. */
  234. esp_rom_spiflash_result_t esp_rom_opiflash_erase_block_64k(uint32_t block_num);
  235. /**
  236. * @brief to erase a flash area define by start address and length
  237. * @note command format would be defined in initialization
  238. * @param start_addr the start address to be erased
  239. * @param area_len the erea length to be erased
  240. * @return flash operation result
  241. */
  242. esp_rom_spiflash_result_t esp_rom_opiflash_erase_area(uint32_t start_addr, uint32_t area_len);
  243. /**
  244. * @brief to read data from opi flash
  245. * @note command format would be defined in initialization
  246. * @param flash_addr flash address to read data from
  247. * @param data_addr data buffer to accept the data
  248. * @param len data length to be read
  249. * @return flash operation result
  250. */
  251. esp_rom_spiflash_result_t esp_rom_opiflash_read(uint32_t flash_addr, void *data_addr, int len);
  252. /**
  253. * @brief to write data to opi flash
  254. * @note command format would be defined in initialization
  255. * @param flash_addr flash address to write data to
  256. * @param data_addr data buffer to write to flash
  257. * @param len data length to write
  258. * @return flash operation result
  259. */
  260. esp_rom_spiflash_result_t esp_rom_opiflash_write(uint32_t flash_addr, const uint32_t *data_addr, int len);
  261. /**
  262. * @brief send WREN command
  263. * @note command format would be defined in initialization
  264. * @param arg not used, set to NULL
  265. * @return flash operation result
  266. */
  267. esp_rom_spiflash_result_t esp_rom_opiflash_wren(void* arg);
  268. /**
  269. * @brief to configure SPI0 read flash command format for cache
  270. * @note command format would be defined in initialization
  271. *
  272. */
  273. void esp_rom_opiflash_cache_mode_config(esp_rom_spiflash_read_mode_t mode, const esp_rom_opiflash_spi0rd_t *cache);
  274. esp_rom_spiflash_result_t esp_rom_opiflash_read_raw(uint32_t flash_addr, uint8_t* buf, int len);
  275. #ifdef __cplusplus
  276. }
  277. #endif
  278. #endif