spi_flash_oct_flash_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "esp_log.h"
  8. #include "esp_err.h"
  9. #include "esp32s3/rom/spi_flash.h"
  10. #include "esp32s3/rom/opi_flash.h"
  11. #include "esp_private/spi_flash_os.h"
  12. #include "opi_flash_private.h"
  13. #include "soc/spi_mem_reg.h"
  14. #include "soc/io_mux_reg.h"
  15. #include "opi_flash_cmd_format_mxic.h"
  16. #define SPI_FLASH_SPI_CMD_WRCR2 0x72
  17. #define SPI_FLASH_SPI_CMD_RDSR 0x05
  18. #define SPI_FLASH_SPI_CMD_RDCR 0x15
  19. #define SPI_FLASH_SPI_CMD_WRSRCR 0x01
  20. #define SPI_FLASH_SPI_CMD_RDSFDP 0x5A
  21. /**
  22. * Supported Flash chip vendor id
  23. */
  24. #define ESP_FLASH_CHIP_MXIC_OCT 0xC2
  25. const static char *TAG = "Octal Flash";
  26. // default value is rom_default_spiflash_legacy_flash_func
  27. extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs;
  28. static uint32_t s_chip_id;
  29. static void s_register_rom_function(void)
  30. {
  31. static spiflash_legacy_funcs_t rom_func =
  32. {
  33. .read_sub_len = 32,
  34. .write_sub_len = 32,
  35. .unlock = esp_rom_opiflash_wait_idle,
  36. .erase_block = esp_rom_opiflash_erase_block_64k,
  37. .erase_sector = esp_rom_opiflash_erase_sector,
  38. .read = esp_rom_opiflash_read,
  39. .write = esp_rom_opiflash_write,
  40. .wait_idle = esp_rom_opiflash_wait_idle,
  41. .wren = esp_rom_opiflash_wren,
  42. .erase_area = esp_rom_opiflash_erase_area,
  43. };
  44. rom_spiflash_legacy_funcs = &rom_func;
  45. }
  46. #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  47. /*----------------------------------------------------------------------------------------------------
  48. MXIC Specific Functions
  49. -----------------------------------------------------------------------------------------------------*/
  50. static esp_err_t s_probe_mxic_chip(uint32_t chip_id, uint8_t *out_vendor_id)
  51. {
  52. if (chip_id >> 16 != ESP_FLASH_CHIP_MXIC_OCT) {
  53. return ESP_ERR_NOT_FOUND;
  54. }
  55. if (((chip_id >> 8) & 0xf0) != 0x80) {
  56. // We now suppose that middle id of opi flash is 0x8*.
  57. ESP_EARLY_LOGE(TAG, "Detected MXIC Flash, but memory type is not Octal");
  58. return ESP_ERR_NOT_FOUND;
  59. }
  60. *out_vendor_id = ESP_FLASH_CHIP_MXIC_OCT;
  61. return ESP_OK;
  62. }
  63. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  64. static bool s_mxic_dtr_need_swap(void)
  65. {
  66. // This function is used for judging the data bytes whether need swap.
  67. // For some of opi flash chips, the data bytes are ordered by D1-D0-D3-D2. This kinds of order needs swap.
  68. // On the contrary, some opi flash chips order the data like D0-D1-D2-D3. This kinds of order doesn't need swap.
  69. // Note: this function must be called when flash works under single line mode.
  70. // 1. Send 0x5A to read SFDP regs for getting the first address of JEDEC Flash Parameter table.
  71. // 2. Add offset with first address to get the order in 8D-8D-8D mode.
  72. // 3. Judge whether the BIT(7) is 1, 1 stands for need swap, vice versa.
  73. uint8_t JEDEC_first_address = 0;
  74. uint8_t byte_order_val = 0;
  75. uint8_t dummy = 8;
  76. uint8_t cmd_len = 8;
  77. uint8_t addr_len = 24;
  78. uint8_t miso_bit_len = 8;
  79. esp_rom_opiflash_exec_cmd(1, ESP_ROM_SPIFLASH_FASTRD_MODE,
  80. SPI_FLASH_SPI_CMD_RDSFDP, cmd_len,
  81. 0x0C, addr_len,
  82. dummy,
  83. NULL, 0,
  84. (uint8_t*)&JEDEC_first_address, miso_bit_len,
  85. ESP_ROM_OPIFLASH_SEL_CS0,
  86. false);
  87. esp_rom_opiflash_exec_cmd(1, ESP_ROM_SPIFLASH_FASTRD_MODE,
  88. SPI_FLASH_SPI_CMD_RDSFDP, cmd_len,
  89. (JEDEC_first_address + 0x47), addr_len,
  90. dummy,
  91. NULL, 0,
  92. (uint8_t*)&byte_order_val, miso_bit_len,
  93. ESP_ROM_OPIFLASH_SEL_CS0,
  94. false);
  95. return ((byte_order_val & 0x80) == 0x80) ? true : false;
  96. }
  97. #endif // CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  98. // 0x00: SPI; 0x01: STR OPI; 0x02: DTR OPI
  99. static void s_set_flash_dtr_str_opi_mode(int spi_num, uint8_t val)
  100. {
  101. uint8_t cmd_len = 8;
  102. int addr_bit_len = 32;
  103. int dummy = 0;
  104. int data_bit_len = 8;
  105. esp_rom_spiflash_write_enable(&g_rom_flashchip);
  106. //SPI command, WRCR2
  107. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  108. SPI_FLASH_SPI_CMD_WRCR2, cmd_len,
  109. 0, addr_bit_len,
  110. dummy,
  111. (uint8_t *)&val, data_bit_len,
  112. NULL, 0,
  113. ESP_ROM_OPIFLASH_SEL_CS0,
  114. false);
  115. }
  116. //To set the output driver strength
  117. static void s_set_flash_ouput_driver_strength(int spi_num, uint8_t strength)
  118. {
  119. uint16_t reg_val = 0;
  120. uint8_t sr_reg_val = 0;
  121. uint8_t cr_reg_val = 0;
  122. uint8_t cmd_len = 8;
  123. uint32_t addr = 0;
  124. int addr_bit_len = 0;
  125. int dummy = 0;
  126. int data_bit_len = 8;
  127. //Read
  128. //SPI command, RDSR
  129. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  130. SPI_FLASH_SPI_CMD_RDSR, cmd_len,
  131. addr, addr_bit_len,
  132. dummy,
  133. NULL, 0,
  134. (uint8_t*)&sr_reg_val, data_bit_len,
  135. ESP_ROM_OPIFLASH_SEL_CS0,
  136. false);
  137. //SPI command, RDCR
  138. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  139. SPI_FLASH_SPI_CMD_RDCR, cmd_len,
  140. addr, addr_bit_len,
  141. dummy,
  142. NULL, 0,
  143. (uint8_t*)&cr_reg_val, data_bit_len,
  144. ESP_ROM_OPIFLASH_SEL_CS0,
  145. false);
  146. //Modify
  147. reg_val = (((cr_reg_val & 0xf8) | strength) << 8) | sr_reg_val;
  148. //Write
  149. //SPI command, WRSR/WRCR
  150. data_bit_len = 16;
  151. esp_rom_spiflash_write_enable(&g_rom_flashchip);
  152. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  153. SPI_FLASH_SPI_CMD_WRSRCR, cmd_len,
  154. addr, addr_bit_len,
  155. dummy,
  156. (uint8_t*)&reg_val, data_bit_len,
  157. NULL, 0,
  158. ESP_ROM_OPIFLASH_SEL_CS0,
  159. false);
  160. }
  161. static void s_set_pin_drive_capability(uint8_t drv)
  162. {
  163. //flash clock
  164. REG_SET_FIELD(SPI_MEM_DATE_REG(0), SPI_MEM_SPI_FMEM_SPICLK_FUN_DRV, 3);
  165. //cs0
  166. PIN_SET_DRV(IO_MUX_GPIO29_REG, 3);
  167. }
  168. static void s_flash_init_mxic(esp_rom_spiflash_read_mode_t mode)
  169. {
  170. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR
  171. static const esp_rom_opiflash_def_t opiflash_cmd_def_mxic = OPI_CMD_FORMAT_MXIC_STR();
  172. #elif CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  173. static const esp_rom_opiflash_def_t opiflash_cmd_def_mxic = OPI_CMD_FORMAT_MXIC_DTR();
  174. #endif
  175. esp_rom_opiflash_legacy_driver_init(&opiflash_cmd_def_mxic);
  176. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  177. // increase flash output driver strength
  178. s_set_flash_ouput_driver_strength(1, 7);
  179. // STR/DTR specific setting
  180. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  181. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR
  182. s_set_pin_drive_capability(3);
  183. s_set_flash_dtr_str_opi_mode(1, 0x1);
  184. esp_rom_opiflash_cache_mode_config(mode, &rom_opiflash_cmd_def->cache_rd_cmd);
  185. esp_rom_spi_set_dtr_swap_mode(0, false, false);
  186. esp_rom_spi_set_dtr_swap_mode(1, false, false);
  187. #else //CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  188. s_set_pin_drive_capability(3);
  189. bool need_swap = s_mxic_dtr_need_swap();
  190. s_set_flash_dtr_str_opi_mode(1, 0x2);
  191. esp_rom_opiflash_cache_mode_config(mode, &rom_opiflash_cmd_def->cache_rd_cmd);
  192. esp_rom_spi_set_dtr_swap_mode(0, need_swap, need_swap);
  193. esp_rom_spi_set_dtr_swap_mode(1, need_swap, need_swap);
  194. #endif
  195. esp_rom_opiflash_wait_idle();
  196. }
  197. #endif // #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  198. static void s_mxic_set_required_regs(uint32_t chip_id)
  199. {
  200. bool is_swap = false;
  201. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  202. is_swap = true;
  203. #else
  204. //STR mode does not need to enable ddr_swap registers
  205. #endif
  206. esp_rom_spi_set_dtr_swap_mode(0, is_swap, is_swap);
  207. esp_rom_spi_set_dtr_swap_mode(1, is_swap, is_swap);
  208. }
  209. /*----------------------------------------------------------------------------------------------------
  210. General Functions
  211. -----------------------------------------------------------------------------------------------------*/
  212. typedef struct opi_flash_func_t {
  213. esp_err_t (*probe)(uint32_t flash_id, uint8_t *out_vendor_id); //Function pointer for detecting Flash chip vendor
  214. void (*init)(esp_rom_spiflash_read_mode_t mode); //Function pointer for initialising certain Flash chips
  215. void (*regs_set)(uint32_t flash_id); //Function pointer for setting required registers, decided by certain flash chips.
  216. } opi_flash_func_t;
  217. #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  218. static const opi_flash_func_t opi_flash_func_mxic = {
  219. .probe = &s_probe_mxic_chip,
  220. .init = &s_flash_init_mxic,
  221. .regs_set = &s_mxic_set_required_regs,
  222. };
  223. #endif
  224. static const opi_flash_func_t *registered_chip_funcs[] = {
  225. #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  226. &opi_flash_func_mxic,
  227. #endif
  228. NULL,
  229. };
  230. //To check which Flash chip is used
  231. static const opi_flash_func_t **s_chip_func = NULL;
  232. esp_err_t esp_opiflash_init(uint32_t chip_id)
  233. {
  234. esp_err_t ret = ESP_FAIL;
  235. esp_rom_spiflash_read_mode_t mode;
  236. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR
  237. mode = ESP_ROM_SPIFLASH_OPI_STR_MODE;
  238. #elif CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  239. mode = ESP_ROM_SPIFLASH_OPI_DTR_MODE;
  240. #else
  241. mode = ESP_ROM_SPIFLASH_FASTRD_MODE;
  242. #endif
  243. const opi_flash_func_t **chip_func = &registered_chip_funcs[0];
  244. uint8_t vendor_id = 0;
  245. while (*chip_func) {
  246. ret = (*chip_func)->probe(chip_id, &vendor_id);
  247. if (ret == ESP_OK) {
  248. // Detect this is the supported chip type
  249. s_chip_id = chip_id;
  250. (*chip_func)->init(mode);
  251. s_register_rom_function();
  252. break;
  253. }
  254. chip_func++;
  255. }
  256. s_chip_func = chip_func;
  257. if (ret != ESP_OK) {
  258. ESP_EARLY_LOGE(TAG, "No detected Flash chip, please check the menuconfig to see if the chip is supported");
  259. abort();
  260. }
  261. return ESP_OK;
  262. }
  263. /**
  264. * Add Flash chip specifically required MSPI register settings here
  265. */
  266. void esp_opiflash_set_required_regs(void)
  267. {
  268. (*s_chip_func)->regs_set(s_chip_id);
  269. }