opi_flash.h 11 KB

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