spi_flash_oct_flash_init.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "esp_rom_gpio.h"
  10. #include "esp32s3/rom/gpio.h"
  11. #include "esp32s3/rom/spi_flash.h"
  12. #include "esp32s3/rom/opi_flash.h"
  13. #include "esp_private/spi_flash_os.h"
  14. #include "opi_flash_private.h"
  15. #include "soc/spi_mem_reg.h"
  16. #include "soc/io_mux_reg.h"
  17. #include "opi_flash_cmd_format_mxic.h"
  18. #define SPI_FLASH_SPI_CMD_WRCR2 0x72
  19. #define SPI_FLASH_SPI_CMD_RDSR 0x05
  20. #define SPI_FLASH_SPI_CMD_RDCR 0x15
  21. #define SPI_FLASH_SPI_CMD_WRSRCR 0x01
  22. /**
  23. * Supported Flash chip vendor id
  24. */
  25. #define ESP_FLASH_CHIP_MXIC_OCT 0xC2
  26. const static char *TAG = "Octal Flash";
  27. // default value is rom_default_spiflash_legacy_flash_func
  28. extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs;
  29. extern int SPI_write_enable(void *spi);
  30. static uint32_t s_vendor_id;
  31. static void s_register_rom_function(void)
  32. {
  33. static spiflash_legacy_funcs_t rom_func =
  34. {
  35. .read_sub_len = 32,
  36. .write_sub_len = 32,
  37. .unlock = esp_rom_opiflash_wait_idle,
  38. .erase_block = esp_rom_opiflash_erase_block_64k,
  39. .erase_sector = esp_rom_opiflash_erase_sector,
  40. .read = esp_rom_opiflash_read,
  41. .write = esp_rom_opiflash_write,
  42. .wait_idle = esp_rom_opiflash_wait_idle,
  43. .wren = esp_rom_opiflash_wren,
  44. .erase_area = esp_rom_opiflash_erase_area,
  45. };
  46. rom_spiflash_legacy_funcs = &rom_func;
  47. }
  48. #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  49. /*----------------------------------------------------------------------------------------------------
  50. MXIC Specific Functions
  51. -----------------------------------------------------------------------------------------------------*/
  52. static esp_err_t s_probe_mxic_chip(uint32_t chip_id, uint8_t *out_vendor_id)
  53. {
  54. if (chip_id >> 16 != ESP_FLASH_CHIP_MXIC_OCT) {
  55. return ESP_ERR_NOT_FOUND;
  56. }
  57. if (((chip_id >> 8) & 0xff) != 0x80) {
  58. ESP_EARLY_LOGE(TAG, "Detected MXIC Flash, but memory type is not Octal");
  59. return ESP_ERR_NOT_FOUND;
  60. }
  61. *out_vendor_id = ESP_FLASH_CHIP_MXIC_OCT;
  62. return ESP_OK;
  63. }
  64. // 0x00: SPI; 0x01: STR OPI; 0x02: DTR OPI
  65. static void s_set_flash_dtr_str_opi_mode(int spi_num, uint8_t val)
  66. {
  67. uint8_t cmd_len = 8;
  68. int addr_bit_len = 32;
  69. int dummy = 0;
  70. int data_bit_len = 8;
  71. SPI_write_enable(&g_rom_flashchip);
  72. //SPI command, WRCR2
  73. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  74. SPI_FLASH_SPI_CMD_WRCR2, cmd_len,
  75. 0, addr_bit_len,
  76. dummy,
  77. (uint8_t *)&val, data_bit_len,
  78. NULL, 0,
  79. ESP_ROM_OPIFLASH_SEL_CS0,
  80. false);
  81. }
  82. //To set the output driver strength
  83. static void s_set_flash_ouput_driver_strength(int spi_num, uint8_t strength)
  84. {
  85. uint16_t reg_val = 0;
  86. uint8_t sr_reg_val = 0;
  87. uint8_t cr_reg_val = 0;
  88. uint8_t cmd_len = 8;
  89. uint32_t addr = 0;
  90. int addr_bit_len = 0;
  91. int dummy = 0;
  92. int data_bit_len = 8;
  93. //Read
  94. //SPI command, RDSR
  95. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  96. SPI_FLASH_SPI_CMD_RDSR, cmd_len,
  97. addr, addr_bit_len,
  98. dummy,
  99. NULL, 0,
  100. (uint8_t*)&sr_reg_val, data_bit_len,
  101. ESP_ROM_OPIFLASH_SEL_CS0,
  102. false);
  103. //SPI command, RDCR
  104. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  105. SPI_FLASH_SPI_CMD_RDCR, cmd_len,
  106. addr, addr_bit_len,
  107. dummy,
  108. NULL, 0,
  109. (uint8_t*)&cr_reg_val, data_bit_len,
  110. ESP_ROM_OPIFLASH_SEL_CS0,
  111. false);
  112. //Modify
  113. reg_val = (((cr_reg_val & 0xf8) | strength) << 8) | sr_reg_val;
  114. //Write
  115. //SPI command, WRSR/WRCR
  116. data_bit_len = 16;
  117. SPI_write_enable(&g_rom_flashchip);
  118. esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE,
  119. SPI_FLASH_SPI_CMD_WRSRCR, cmd_len,
  120. addr, addr_bit_len,
  121. dummy,
  122. (uint8_t*)&reg_val, data_bit_len,
  123. NULL, 0,
  124. ESP_ROM_OPIFLASH_SEL_CS0,
  125. false);
  126. }
  127. static void s_set_pin_drive_capability(uint8_t drv)
  128. {
  129. //flash clock
  130. REG_SET_FIELD(SPI_MEM_DATE_REG(0), SPI_MEM_SPI_FMEM_SPICLK_FUN_DRV, 3);
  131. //cs0
  132. PIN_SET_DRV(IO_MUX_GPIO29_REG, 3);
  133. }
  134. static void s_flash_init_mxic(esp_rom_spiflash_read_mode_t mode)
  135. {
  136. static const esp_rom_opiflash_def_t opiflash_cmd_def_mxic = OPI_CMD_FORMAT_MXIC();
  137. esp_rom_opiflash_legacy_driver_init(&opiflash_cmd_def_mxic);
  138. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  139. // increase flash output driver strength
  140. s_set_flash_ouput_driver_strength(1, 7);
  141. // STR/DTR specific setting
  142. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  143. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR
  144. s_set_pin_drive_capability(3);
  145. s_set_flash_dtr_str_opi_mode(1, 0x1);
  146. esp_rom_opiflash_cache_mode_config(mode, &rom_opiflash_cmd_def->cache_rd_cmd);
  147. esp_rom_spi_set_dtr_swap_mode(0, false, false);
  148. esp_rom_spi_set_dtr_swap_mode(1, false, false);
  149. #else //CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  150. s_set_pin_drive_capability(3);
  151. s_set_flash_dtr_str_opi_mode(1, 0x2);
  152. esp_rom_opiflash_cache_mode_config(mode, &rom_opiflash_cmd_def->cache_rd_cmd);
  153. esp_rom_spi_set_dtr_swap_mode(0, true, true);
  154. esp_rom_spi_set_dtr_swap_mode(1, true, true);
  155. #endif
  156. esp_rom_opiflash_wait_idle();
  157. }
  158. #endif // #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  159. /*----------------------------------------------------------------------------------------------------
  160. General Functions
  161. -----------------------------------------------------------------------------------------------------*/
  162. typedef struct opi_flash_func_t {
  163. esp_err_t (*probe)(uint32_t flash_id, uint8_t *out_vendor_id); //Function pointer for detecting Flash chip vendor
  164. void (*init)(esp_rom_spiflash_read_mode_t mode); //Function pointer for initialising certain Flash chips
  165. } opi_flash_func_t;
  166. #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  167. static const opi_flash_func_t opi_flash_func_mxic = {
  168. .probe = &s_probe_mxic_chip,
  169. .init = &s_flash_init_mxic
  170. };
  171. #endif
  172. static const opi_flash_func_t *registered_chip_funcs[] = {
  173. #if CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
  174. &opi_flash_func_mxic,
  175. #endif
  176. NULL,
  177. };
  178. esp_err_t esp_opiflash_init(uint32_t chip_id)
  179. {
  180. esp_err_t ret = ESP_FAIL;
  181. esp_rom_spiflash_read_mode_t mode;
  182. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR
  183. mode = ESP_ROM_SPIFLASH_OPI_STR_MODE;
  184. #elif CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  185. mode = ESP_ROM_SPIFLASH_OPI_DTR_MODE;
  186. #else
  187. mode = ESP_ROM_SPIFLASH_FASTRD_MODE;
  188. #endif
  189. //To check which Flash chip is used
  190. const opi_flash_func_t **chip_func = &registered_chip_funcs[0];
  191. uint8_t vendor_id = 0;
  192. while (*chip_func) {
  193. ret = (*chip_func)->probe(chip_id, &vendor_id);
  194. if (ret == ESP_OK) {
  195. // Detect this is the supported chip type
  196. (*chip_func)->init(mode);
  197. s_vendor_id = vendor_id;
  198. s_register_rom_function();
  199. break;
  200. }
  201. chip_func++;
  202. }
  203. if (ret != ESP_OK) {
  204. ESP_EARLY_LOGE(TAG, "No detected Flash chip, please check the menuconfig to see if the chip is supported");
  205. abort();
  206. }
  207. return ESP_OK;
  208. }
  209. /**
  210. * Add Flash chip specifically required MSPI register settings here
  211. */
  212. void esp_opiflash_set_required_regs(void)
  213. {
  214. bool is_swap = false;
  215. #if CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  216. if (s_vendor_id == ESP_FLASH_CHIP_MXIC_OCT) {
  217. is_swap = true;
  218. }
  219. #else
  220. //STR mode does not need to enable ddr_swap registers
  221. #endif
  222. esp_rom_spi_set_dtr_swap_mode(0, is_swap, is_swap);
  223. esp_rom_spi_set_dtr_swap_mode(1, is_swap, is_swap);
  224. }