esp_flash_spi_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "esp_flash.h"
  8. #include "memspi_host_driver.h"
  9. #include "esp_flash_spi_init.h"
  10. #include "driver/gpio.h"
  11. #include "esp_rom_gpio.h"
  12. #include "esp_rom_efuse.h"
  13. #include "esp_log.h"
  14. #include "esp_heap_caps.h"
  15. #include "hal/spi_types.h"
  16. #include "driver/spi_common_internal.h"
  17. #include "hal/spi_flash_hal.h"
  18. #include "hal/gpio_hal.h"
  19. #include "esp_flash_internal.h"
  20. #include "esp_rom_gpio.h"
  21. #include "esp_private/spi_flash_os.h"
  22. #include "esp_rom_spiflash.h"
  23. __attribute__((unused)) static const char TAG[] = "spi_flash";
  24. /* This pointer is defined in ROM and extern-ed on targets where CONFIG_SPI_FLASH_ROM_IMPL = y*/
  25. #if !CONFIG_SPI_FLASH_ROM_IMPL
  26. esp_flash_t *esp_flash_default_chip = NULL;
  27. #endif
  28. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  29. #ifdef CONFIG_ESPTOOLPY_FLASHFREQ_80M
  30. #define DEFAULT_FLASH_SPEED ESP_FLASH_80MHZ
  31. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_40M
  32. #define DEFAULT_FLASH_SPEED ESP_FLASH_40MHZ
  33. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_26M
  34. #define DEFAULT_FLASH_SPEED ESP_FLASH_26MHZ
  35. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_20M
  36. #define DEFAULT_FLASH_SPEED ESP_FLASH_20MHZ
  37. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_120M
  38. #define DEFAULT_FLASH_SPEED ESP_FLASH_120MHZ
  39. #else
  40. #error Flash frequency not defined! Check the ``CONFIG_ESPTOOLPY_FLASHFREQ_*`` options.
  41. #endif
  42. #if defined(CONFIG_ESPTOOLPY_FLASHMODE_QIO)
  43. #define DEFAULT_FLASH_MODE SPI_FLASH_QIO
  44. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_QOUT)
  45. #define DEFAULT_FLASH_MODE SPI_FLASH_QOUT
  46. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_DIO)
  47. #define DEFAULT_FLASH_MODE SPI_FLASH_DIO
  48. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_DOUT)
  49. #define DEFAULT_FLASH_MODE SPI_FLASH_DOUT
  50. #elif defined(CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR)
  51. #define DEFAULT_FLASH_MODE SPI_FLASH_OPI_STR
  52. #elif defined(CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR)
  53. #define DEFAULT_FLASH_MODE SPI_FLASH_OPI_DTR
  54. #else
  55. #define DEFAULT_FLASH_MODE SPI_FLASH_FASTRD
  56. #endif
  57. //TODO: modify cs hold to meet requirements of all chips!!!
  58. #if CONFIG_IDF_TARGET_ESP32
  59. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  60. .host_id = SPI1_HOST,\
  61. .speed = DEFAULT_FLASH_SPEED, \
  62. .cs_num = 0, \
  63. .iomux = false, \
  64. .input_delay_ns = 0,\
  65. .cs_setup = 1,\
  66. }
  67. #elif CONFIG_IDF_TARGET_ESP32S2
  68. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  69. .host_id = SPI1_HOST,\
  70. .speed = DEFAULT_FLASH_SPEED, \
  71. .cs_num = 0, \
  72. .iomux = true, \
  73. .input_delay_ns = 0,\
  74. .cs_setup = 1,\
  75. }
  76. #elif CONFIG_IDF_TARGET_ESP32S3
  77. #include "esp32s3/rom/efuse.h"
  78. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  79. .host_id = SPI1_HOST,\
  80. .speed = DEFAULT_FLASH_SPEED, \
  81. .cs_num = 0, \
  82. .iomux = true, \
  83. .input_delay_ns = 0,\
  84. .cs_setup = 1,\
  85. }
  86. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP8684
  87. #if !CONFIG_SPI_FLASH_AUTO_SUSPEND
  88. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  89. .host_id = SPI1_HOST,\
  90. .speed = DEFAULT_FLASH_SPEED, \
  91. .cs_num = 0, \
  92. .iomux = true, \
  93. .input_delay_ns = 0,\
  94. .cs_setup = 1,\
  95. }
  96. #else
  97. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  98. .host_id = SPI1_HOST,\
  99. .speed = DEFAULT_FLASH_SPEED, \
  100. .cs_num = 0, \
  101. .iomux = true, \
  102. .input_delay_ns = 0,\
  103. .auto_sus_en = true,\
  104. .cs_setup = 1,\
  105. }
  106. #endif //!CONFIG_SPI_FLASH_AUTO_SUSPEND
  107. #elif CONFIG_IDF_TARGET_ESP32H2
  108. #include "esp32h2/rom/efuse.h"
  109. #if !CONFIG_SPI_FLASH_AUTO_SUSPEND
  110. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  111. .host_id = SPI1_HOST,\
  112. .speed = DEFAULT_FLASH_SPEED, \
  113. .cs_num = 0, \
  114. .iomux = true, \
  115. .input_delay_ns = 0,\
  116. }
  117. #else
  118. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  119. .host_id = SPI1_HOST,\
  120. .speed = DEFAULT_FLASH_SPEED, \
  121. .cs_num = 0, \
  122. .iomux = true, \
  123. .input_delay_ns = 0,\
  124. .auto_sus_en = true,\
  125. }
  126. #endif //!CONFIG_SPI_FLASH_AUTO_SUSPEND
  127. #endif
  128. static IRAM_ATTR NOINLINE_ATTR void cs_initialize(esp_flash_t *chip, const esp_flash_spi_device_config_t *config, bool use_iomux, int cs_id)
  129. {
  130. //Not using spicommon_cs_initialize since we don't want to put the whole
  131. //spi_periph_signal into the DRAM. Copy these data from flash before the
  132. //cache disabling
  133. int cs_io_num = config->cs_io_num;
  134. int spics_in = spi_periph_signal[config->host_id].spics_in;
  135. int spics_out = spi_periph_signal[config->host_id].spics_out[cs_id];
  136. int spics_func = spi_periph_signal[config->host_id].func;
  137. uint32_t iomux_reg = GPIO_PIN_MUX_REG[cs_io_num];
  138. //To avoid the panic caused by flash data line conflicts during cs line
  139. //initialization, disable the cache temporarily
  140. chip->os_func->start(chip->os_func_data);
  141. PIN_INPUT_ENABLE(iomux_reg);
  142. if (use_iomux) {
  143. gpio_hal_iomux_func_sel(iomux_reg, spics_func);
  144. } else {
  145. #if SOC_GPIO_PIN_COUNT <= 32
  146. GPIO.enable_w1ts.val = (0x1 << cs_io_num);
  147. #else
  148. if (cs_io_num < 32) {
  149. GPIO.enable_w1ts = (0x1 << cs_io_num);
  150. } else {
  151. GPIO.enable1_w1ts.data = (0x1 << (cs_io_num - 32));
  152. }
  153. #endif
  154. GPIO.pin[cs_io_num].pad_driver = 0;
  155. esp_rom_gpio_connect_out_signal(cs_io_num, spics_out, false, false);
  156. if (cs_id == 0) {
  157. esp_rom_gpio_connect_in_signal(cs_io_num, spics_in, false);
  158. }
  159. gpio_hal_iomux_func_sel(iomux_reg, PIN_FUNC_GPIO);
  160. }
  161. chip->os_func->end(chip->os_func_data);
  162. }
  163. esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_device_config_t *config)
  164. {
  165. if (out_chip == NULL) {
  166. return ESP_ERR_INVALID_ARG;
  167. }
  168. if (!GPIO_IS_VALID_OUTPUT_GPIO(config->cs_io_num)) {
  169. return ESP_ERR_INVALID_ARG;
  170. }
  171. esp_flash_t *chip = NULL;
  172. memspi_host_inst_t *host = NULL;
  173. esp_err_t ret = ESP_OK;
  174. uint32_t caps = MALLOC_CAP_DEFAULT;
  175. if (config->host_id == SPI1_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
  176. chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
  177. if (!chip) {
  178. ret = ESP_ERR_NO_MEM;
  179. goto fail;
  180. }
  181. host = (memspi_host_inst_t*)heap_caps_malloc(sizeof(memspi_host_inst_t), caps);
  182. *chip = (esp_flash_t) {
  183. .read_mode = config->io_mode,
  184. .host = (spi_flash_host_inst_t*)host,
  185. };
  186. if (!host) {
  187. ret = ESP_ERR_NO_MEM;
  188. goto fail;
  189. }
  190. int dev_id = -1;
  191. esp_err_t err = esp_flash_init_os_functions(chip, config->host_id, &dev_id);
  192. if (err == ESP_ERR_NOT_SUPPORTED) {
  193. ESP_LOGE(TAG, "Init os functions failed! No free CS.");
  194. } else if (err == ESP_ERR_INVALID_ARG) {
  195. ESP_LOGE(TAG, "Init os functions failed! Bus lock not initialized (check CONFIG_SPI_FLASH_SHARE_SPI1_BUS).");
  196. }
  197. if (err != ESP_OK) {
  198. ret = err;
  199. goto fail;
  200. }
  201. // When `CONFIG_SPI_FLASH_SHARE_SPI1_BUS` is not enabled on SPI1 bus, the
  202. // `esp_flash_init_os_functions` will not be able to assign a new device ID. In this case, we
  203. // use the `cs_id` in the config structure.
  204. if (dev_id == -1 && config->host_id == SPI1_HOST) {
  205. dev_id = config->cs_id;
  206. }
  207. assert(dev_id < SOC_SPI_PERIPH_CS_NUM(config->host_id) && dev_id >= 0);
  208. bool use_iomux = spicommon_bus_using_iomux(config->host_id);
  209. memspi_host_config_t host_cfg = {
  210. .host_id = config->host_id,
  211. .cs_num = dev_id,
  212. .iomux = use_iomux,
  213. .input_delay_ns = config->input_delay_ns,
  214. .speed = config->speed,
  215. };
  216. err = memspi_host_init_pointers(host, &host_cfg);
  217. if (err != ESP_OK) {
  218. ret = err;
  219. goto fail;
  220. }
  221. // The cs_id inside `config` is deprecated, use the `dev_id` provided by the bus lock instead.
  222. cs_initialize(chip, config, use_iomux, dev_id);
  223. *out_chip = chip;
  224. return ret;
  225. fail:
  226. // The memory allocated are free'd in the `spi_bus_remove_flash_device`.
  227. spi_bus_remove_flash_device(chip);
  228. return ret;
  229. }
  230. esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip)
  231. {
  232. if (chip==NULL) {
  233. return ESP_ERR_INVALID_ARG;
  234. }
  235. esp_flash_deinit_os_functions(chip);
  236. free(chip->host);
  237. free(chip);
  238. return ESP_OK;
  239. }
  240. /* The default (ie initial boot) no-OS ROM esp_flash_os_functions_t */
  241. extern const esp_flash_os_functions_t esp_flash_noos_functions;
  242. static DRAM_ATTR memspi_host_inst_t esp_flash_default_host;
  243. static DRAM_ATTR esp_flash_t default_chip = {
  244. .read_mode = DEFAULT_FLASH_MODE,
  245. .host = (spi_flash_host_inst_t*)&esp_flash_default_host,
  246. .os_func = &esp_flash_noos_functions,
  247. };
  248. extern esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip);
  249. esp_err_t esp_flash_init_default_chip(void)
  250. {
  251. const esp_rom_spiflash_chip_t *legacy_chip = &g_rom_flashchip;
  252. memspi_host_config_t cfg = ESP_FLASH_HOST_CONFIG_DEFAULT();
  253. #if !CONFIG_IDF_TARGET_ESP32 && !CONFIG_IDF_TARGET_ESP8684
  254. // For esp32s2 spi IOs are configured as from IO MUX by default
  255. cfg.iomux = esp_rom_efuse_get_flash_gpio_info() == 0 ? true : false;
  256. #endif
  257. #if CONFIG_ESPTOOLPY_OCT_FLASH
  258. cfg.octal_mode_en = 1;
  259. cfg.default_io_mode = DEFAULT_FLASH_MODE;
  260. #endif
  261. // For chips need time tuning, get value directely from system here.
  262. #if SOC_SPI_MEM_SUPPORT_TIME_TUNING
  263. if (spi_timing_is_tuned()) {
  264. cfg.using_timing_tuning = 1;
  265. spi_timing_get_flash_timing_param(&cfg.timing_reg);
  266. }
  267. #endif // SOC_SPI_MEM_SUPPORT_TIME_TUNING
  268. //the host is already initialized, only do init for the data and load it to the host
  269. esp_err_t err = memspi_host_init_pointers(&esp_flash_default_host, &cfg);
  270. if (err != ESP_OK) {
  271. return err;
  272. }
  273. // ROM TODO: account for non-standard default pins in efuse
  274. // ROM TODO: to account for chips which are slow to power on, maybe keep probing in a loop here
  275. err = esp_flash_init_main(&default_chip);
  276. if (err != ESP_OK) {
  277. return err;
  278. }
  279. if (default_chip.size < legacy_chip->chip_size) {
  280. ESP_EARLY_LOGE(TAG, "Detected size(%dk) smaller than the size in the binary image header(%dk). Probe failed.", default_chip.size/1024, legacy_chip->chip_size/1024);
  281. return ESP_ERR_FLASH_SIZE_NOT_MATCH;
  282. }
  283. if (default_chip.size > legacy_chip->chip_size) {
  284. ESP_EARLY_LOGW(TAG, "Detected size(%dk) larger than the size in the binary image header(%dk). Using the size in the binary image header.", default_chip.size/1024, legacy_chip->chip_size/1024);
  285. }
  286. default_chip.size = legacy_chip->chip_size;
  287. esp_flash_default_chip = &default_chip;
  288. #ifdef CONFIG_SPI_FLASH_AUTO_SUSPEND
  289. err = esp_flash_suspend_cmd_init(&default_chip);
  290. if (err != ESP_OK) {
  291. return err;
  292. }
  293. #endif
  294. return ESP_OK;
  295. }
  296. esp_err_t esp_flash_app_init(void)
  297. {
  298. esp_err_t err = ESP_OK;
  299. #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS
  300. err = esp_flash_init_main_bus_lock();
  301. if (err != ESP_OK) return err;
  302. #endif
  303. err = esp_flash_app_enable_os_functions(&default_chip);
  304. return err;
  305. }
  306. #endif //!CONFIG_SPI_FLASH_USE_LEGACY_IMPL