flash_qio_mode.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "bootloader_flash_config.h"
  9. #include "flash_qio_mode.h"
  10. #include "sdkconfig.h"
  11. #include "bootloader_flash_priv.h"
  12. #include "esp_log.h"
  13. #include "esp_err.h"
  14. #include "esp_rom_spiflash.h"
  15. #include "esp_rom_efuse.h"
  16. #include "flash_qio_mode.h"
  17. #include "soc/efuse_periph.h"
  18. #include "soc/io_mux_reg.h"
  19. static const char *TAG = "qio_mode";
  20. /* Array of known flash chips and data to enable Quad I/O mode
  21. Manufacturer & flash ID can be tested by running "esptool.py
  22. flash_id"
  23. If manufacturer ID matches, and flash ID ORed with flash ID mask
  24. matches, enable_qio_mode() will execute "Read Cmd", test if bit
  25. number "QIE Bit" is set, and if not set it will call "Write Cmd"
  26. with this bit set.
  27. Searching of this table stops when the first match is found.
  28. */
  29. const bootloader_qio_info_t __attribute__((weak)) bootloader_flash_qe_support_list[] = {
  30. /* Manufacturer, mfg_id, flash_id, id mask, Read Status, Write Status, QIE Bit */
  31. { "MXIC", 0xC2, 0x2000, 0xFF00, bootloader_read_status_8b_rdsr, bootloader_write_status_8b_wrsr, 6 },
  32. { "ISSI", 0x9D, 0x4000, 0xCF00, bootloader_read_status_8b_rdsr, bootloader_write_status_8b_wrsr, 6 }, /* IDs 0x40xx, 0x70xx */
  33. { "WinBond", 0xEF, 0x4000, 0xFF00, bootloader_read_status_16b_rdsr_rdsr2, bootloader_write_status_16b_wrsr, 9 },
  34. { "GD", 0xC8, 0x6000, 0xFF00, bootloader_read_status_16b_rdsr_rdsr2, bootloader_write_status_16b_wrsr, 9 },
  35. { "XM25QU64A", 0x20, 0x3817, 0xFFFF, bootloader_read_status_8b_xmc25qu64a, bootloader_write_status_8b_xmc25qu64a, 6 },
  36. { "TH", 0xcd, 0x6000, 0xFF00, bootloader_read_status_16b_rdsr_rdsr2, bootloader_write_status_16b_wrsr, 9 },
  37. /* Final entry is default entry, if no other IDs have matched.
  38. This approach works for chips including:
  39. GigaDevice (mfg ID 0xC8, flash IDs including 4016),
  40. FM25Q32 (QOUT mode only, mfg ID 0xA1, flash IDs including 4016)
  41. BY25Q32 (mfg ID 0x68, flash IDs including 4016)
  42. */
  43. { NULL, 0xFF, 0xFFFF, 0xFFFF, bootloader_read_status_8b_rdsr2, bootloader_write_status_8b_wrsr2, 1 },
  44. };
  45. #define NUM_CHIPS (sizeof(bootloader_flash_qe_support_list) / sizeof(bootloader_qio_info_t))
  46. static esp_err_t enable_qio_mode(bootloader_flash_read_status_fn_t read_status_fn,
  47. bootloader_flash_write_status_fn_t write_status_fn,
  48. uint8_t status_qio_bit);
  49. /* Generic function to use the "user command" SPI controller functionality
  50. to send commands to the SPI flash and read the respopnse.
  51. The command passed here is always the on-the-wire command given to the SPI flash unit.
  52. */
  53. void bootloader_enable_qio_mode(void)
  54. {
  55. uint32_t raw_flash_id;
  56. uint8_t mfg_id;
  57. uint16_t flash_id;
  58. size_t i;
  59. ESP_LOGD(TAG, "Probing for QIO mode enable...");
  60. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  61. raw_flash_id = g_rom_flashchip.device_id;
  62. ESP_LOGD(TAG, "Raw SPI flash chip id 0x%x", raw_flash_id);
  63. mfg_id = (raw_flash_id >> 16) & 0xFF;
  64. flash_id = raw_flash_id & 0xFFFF;
  65. ESP_LOGD(TAG, "Manufacturer ID 0x%02x chip ID 0x%04x", mfg_id, flash_id);
  66. for (i = 0; i < NUM_CHIPS - 1; i++) {
  67. const bootloader_qio_info_t *chip = &bootloader_flash_qe_support_list[i];
  68. if (mfg_id == chip->mfg_id && (flash_id & chip->id_mask) == (chip->flash_id & chip->id_mask)) {
  69. ESP_LOGI(TAG, "Enabling QIO for flash chip %s", bootloader_flash_qe_support_list[i].manufacturer);
  70. break;
  71. }
  72. }
  73. if (i == NUM_CHIPS - 1) {
  74. ESP_LOGI(TAG, "Enabling default flash chip QIO");
  75. }
  76. enable_qio_mode(bootloader_flash_qe_support_list[i].read_status_fn,
  77. bootloader_flash_qe_support_list[i].write_status_fn,
  78. bootloader_flash_qe_support_list[i].status_qio_bit);
  79. #if SOC_CACHE_SUPPORT_WRAP
  80. bootloader_flash_wrap_set(FLASH_WRAP_MODE_DISABLE);
  81. #endif
  82. }
  83. static void s_flash_set_qio_pins(void)
  84. {
  85. #if CONFIG_IDF_TARGET_ESP32
  86. const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
  87. int wp_pin = bootloader_flash_get_wp_pin();
  88. esp_rom_spiflash_select_qio_pins(wp_pin, spiconfig);
  89. #elif CONFIG_IDF_TARGET_ESP32C2
  90. // ESP32C2 doesn't support configure mspi pins. So the second
  91. // parameter is set to 0, means that chip uses default SPI pins
  92. // and wp_gpio_num parameter(the first parameter) is ignored.
  93. esp_rom_spiflash_select_qio_pins(0, 0);
  94. #else
  95. esp_rom_spiflash_select_qio_pins(esp_rom_efuse_get_flash_wp_gpio(), esp_rom_efuse_get_flash_gpio_info());
  96. #endif
  97. }
  98. static esp_err_t enable_qio_mode(bootloader_flash_read_status_fn_t read_status_fn,
  99. bootloader_flash_write_status_fn_t write_status_fn,
  100. uint8_t status_qio_bit)
  101. {
  102. uint32_t status;
  103. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  104. status = read_status_fn();
  105. ESP_LOGD(TAG, "Initial flash chip status 0x%x", status);
  106. if ((status & (1 << status_qio_bit)) == 0) {
  107. bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
  108. write_status_fn(status | (1 << status_qio_bit));
  109. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  110. status = read_status_fn();
  111. ESP_LOGD(TAG, "Updated flash chip status 0x%x", status);
  112. if ((status & (1 << status_qio_bit)) == 0) {
  113. ESP_LOGE(TAG, "Failed to set QIE bit, not enabling QIO mode");
  114. return ESP_FAIL;
  115. }
  116. } else {
  117. ESP_LOGD(TAG, "QIO mode already enabled in flash");
  118. }
  119. ESP_LOGD(TAG, "Enabling QIO mode...");
  120. esp_rom_spiflash_read_mode_t mode;
  121. #if CONFIG_ESPTOOLPY_FLASHMODE_QOUT
  122. mode = ESP_ROM_SPIFLASH_QOUT_MODE;
  123. #else
  124. mode = ESP_ROM_SPIFLASH_QIO_MODE;
  125. #endif
  126. esp_rom_spiflash_config_readmode(mode);
  127. s_flash_set_qio_pins();
  128. return ESP_OK;
  129. }
  130. unsigned bootloader_read_status_8b_rdsr(void)
  131. {
  132. return bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
  133. }
  134. unsigned bootloader_read_status_8b_rdsr2(void)
  135. {
  136. return bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8);
  137. }
  138. unsigned bootloader_read_status_8b_rdsr3(void)
  139. {
  140. return bootloader_execute_flash_command(CMD_RDSR3, 0, 0, 8);
  141. }
  142. unsigned bootloader_read_status_16b_rdsr_rdsr2(void)
  143. {
  144. return bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8) | (bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8) << 8);
  145. }
  146. void bootloader_write_status_8b_wrsr(unsigned new_status)
  147. {
  148. bootloader_execute_flash_command(CMD_WRSR, new_status, 8, 0);
  149. }
  150. void bootloader_write_status_8b_wrsr2(unsigned new_status)
  151. {
  152. bootloader_execute_flash_command(CMD_WRSR2, new_status, 8, 0);
  153. }
  154. void bootloader_write_status_8b_wrsr3(unsigned new_status)
  155. {
  156. bootloader_execute_flash_command(CMD_WRSR3, new_status, 8, 0);
  157. }
  158. void bootloader_write_status_16b_wrsr(unsigned new_status)
  159. {
  160. bootloader_execute_flash_command(CMD_WRSR, new_status, 16, 0);
  161. }
  162. unsigned bootloader_read_status_8b_xmc25qu64a(void)
  163. {
  164. bootloader_execute_flash_command(CMD_OTPEN, 0, 0, 0); /* Enter OTP mode */
  165. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  166. uint32_t read_status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
  167. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
  168. return read_status;
  169. }
  170. void bootloader_write_status_8b_xmc25qu64a(unsigned new_status)
  171. {
  172. bootloader_execute_flash_command(CMD_OTPEN, 0, 0, 0); /* Enter OTP mode */
  173. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  174. bootloader_execute_flash_command(CMD_WRSR, new_status, 8, 0);
  175. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  176. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
  177. }