esp_flash_spi_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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 "esp_private/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_private/cache_utils.h"
  23. #include "esp_spi_flash_counters.h"
  24. #include "esp_rom_spiflash.h"
  25. __attribute__((unused)) static const char TAG[] = "spi_flash";
  26. /* This pointer is defined in ROM and extern-ed on targets where CONFIG_SPI_FLASH_ROM_IMPL = y*/
  27. #if !CONFIG_SPI_FLASH_ROM_IMPL
  28. esp_flash_t *esp_flash_default_chip = NULL;
  29. #endif
  30. #if defined CONFIG_ESPTOOLPY_FLASHFREQ_120M
  31. #define DEFAULT_FLASH_SPEED 120
  32. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_80M
  33. #define DEFAULT_FLASH_SPEED 80
  34. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_60M
  35. #define DEFAULT_FLASH_SPEED 60
  36. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_48M
  37. #define DEFAULT_FLASH_SPEED 48
  38. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_40M
  39. #define DEFAULT_FLASH_SPEED 40
  40. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_30M
  41. #define DEFAULT_FLASH_SPEED 30
  42. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_26M
  43. #define DEFAULT_FLASH_SPEED 26
  44. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_24M
  45. #define DEFAULT_FLASH_SPEED 24
  46. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_20M
  47. #define DEFAULT_FLASH_SPEED 20
  48. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_16M
  49. #define DEFAULT_FLASH_SPEED 16
  50. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_15M
  51. #define DEFAULT_FLASH_SPEED 15
  52. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_12M
  53. #define DEFAULT_FLASH_SPEED 12
  54. #else
  55. #error Flash frequency not defined! Check the ``CONFIG_ESPTOOLPY_FLASHFREQ_*`` options.
  56. #endif
  57. #if defined(CONFIG_ESPTOOLPY_FLASHMODE_QIO)
  58. #define DEFAULT_FLASH_MODE SPI_FLASH_QIO
  59. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_QOUT)
  60. #define DEFAULT_FLASH_MODE SPI_FLASH_QOUT
  61. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_DIO)
  62. #define DEFAULT_FLASH_MODE SPI_FLASH_DIO
  63. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_DOUT)
  64. #define DEFAULT_FLASH_MODE SPI_FLASH_DOUT
  65. #elif defined(CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR)
  66. #define DEFAULT_FLASH_MODE SPI_FLASH_OPI_STR
  67. #elif defined(CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR)
  68. #define DEFAULT_FLASH_MODE SPI_FLASH_OPI_DTR
  69. #else
  70. #define DEFAULT_FLASH_MODE SPI_FLASH_FASTRD
  71. #endif
  72. //TODO: modify cs hold to meet requirements of all chips!!!
  73. #if CONFIG_IDF_TARGET_ESP32
  74. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  75. .host_id = SPI1_HOST,\
  76. .freq_mhz = DEFAULT_FLASH_SPEED, \
  77. .cs_num = 0, \
  78. .iomux = false, \
  79. .input_delay_ns = 0,\
  80. .cs_setup = 1,\
  81. }
  82. #elif CONFIG_IDF_TARGET_ESP32S2
  83. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  84. .host_id = SPI1_HOST,\
  85. .freq_mhz = DEFAULT_FLASH_SPEED, \
  86. .cs_num = 0, \
  87. .iomux = true, \
  88. .input_delay_ns = 0,\
  89. .cs_setup = 1,\
  90. }
  91. #elif CONFIG_IDF_TARGET_ESP32S3
  92. #include "esp32s3/rom/efuse.h"
  93. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  94. .host_id = SPI1_HOST,\
  95. .freq_mhz = DEFAULT_FLASH_SPEED, \
  96. .cs_num = 0, \
  97. .iomux = true, \
  98. .input_delay_ns = 0,\
  99. .cs_setup = 1,\
  100. }
  101. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
  102. #if !CONFIG_SPI_FLASH_AUTO_SUSPEND
  103. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  104. .host_id = SPI1_HOST,\
  105. .freq_mhz = DEFAULT_FLASH_SPEED, \
  106. .cs_num = 0, \
  107. .iomux = true, \
  108. .input_delay_ns = 0,\
  109. .cs_setup = 1,\
  110. }
  111. #else
  112. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  113. .host_id = SPI1_HOST,\
  114. .freq_mhz = DEFAULT_FLASH_SPEED, \
  115. .cs_num = 0, \
  116. .iomux = true, \
  117. .input_delay_ns = 0,\
  118. .auto_sus_en = true,\
  119. .cs_setup = 1,\
  120. }
  121. #endif //!CONFIG_SPI_FLASH_AUTO_SUSPEND
  122. #elif CONFIG_IDF_TARGET_ESP32H2
  123. #include "esp32h2/rom/efuse.h"
  124. #if !CONFIG_SPI_FLASH_AUTO_SUSPEND
  125. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  126. .host_id = SPI1_HOST,\
  127. .freq_mhz = DEFAULT_FLASH_SPEED, \
  128. .cs_num = 0, \
  129. .iomux = true, \
  130. .input_delay_ns = 0,\
  131. }
  132. #else
  133. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  134. .host_id = SPI1_HOST,\
  135. .freq_mhz = DEFAULT_FLASH_SPEED, \
  136. .cs_num = 0, \
  137. .iomux = true, \
  138. .input_delay_ns = 0,\
  139. .auto_sus_en = true,\
  140. }
  141. #endif //!CONFIG_SPI_FLASH_AUTO_SUSPEND
  142. #endif
  143. 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)
  144. {
  145. //Not using spicommon_cs_initialize since we don't want to put the whole
  146. //spi_periph_signal into the DRAM. Copy these data from flash before the
  147. //cache disabling
  148. int cs_io_num = config->cs_io_num;
  149. int spics_in = spi_periph_signal[config->host_id].spics_in;
  150. int spics_out = spi_periph_signal[config->host_id].spics_out[cs_id];
  151. int spics_func = spi_periph_signal[config->host_id].func;
  152. uint32_t iomux_reg = GPIO_PIN_MUX_REG[cs_io_num];
  153. gpio_hal_context_t gpio_hal = {
  154. .dev = GPIO_HAL_GET_HW(GPIO_PORT_0)
  155. };
  156. //To avoid the panic caused by flash data line conflicts during cs line
  157. //initialization, disable the cache temporarily
  158. chip->os_func->start(chip->os_func_data);
  159. PIN_INPUT_ENABLE(iomux_reg);
  160. if (use_iomux) {
  161. gpio_hal_iomux_func_sel(iomux_reg, spics_func);
  162. } else {
  163. gpio_hal_output_enable(&gpio_hal, cs_io_num);
  164. gpio_hal_od_disable(&gpio_hal, cs_io_num);
  165. esp_rom_gpio_connect_out_signal(cs_io_num, spics_out, false, false);
  166. if (cs_id == 0) {
  167. esp_rom_gpio_connect_in_signal(cs_io_num, spics_in, false);
  168. }
  169. gpio_hal_iomux_func_sel(iomux_reg, PIN_FUNC_GPIO);
  170. }
  171. chip->os_func->end(chip->os_func_data);
  172. }
  173. static bool use_bus_lock(int host_id)
  174. {
  175. if (host_id != SPI1_HOST) {
  176. return true;
  177. }
  178. #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS
  179. return true;
  180. #else
  181. return false;
  182. #endif
  183. }
  184. static esp_err_t acquire_spi_device(const esp_flash_spi_device_config_t *config, int* out_dev_id, spi_bus_lock_dev_handle_t* out_dev_handle)
  185. {
  186. esp_err_t ret = ESP_OK;
  187. int dev_id = -1;
  188. spi_bus_lock_dev_handle_t dev_handle = NULL;
  189. if (use_bus_lock(config->host_id)) {
  190. spi_bus_lock_handle_t lock = spi_bus_lock_get_by_id(config->host_id);
  191. spi_bus_lock_dev_config_t config = {.flags = SPI_BUS_LOCK_DEV_FLAG_CS_REQUIRED};
  192. ret = spi_bus_lock_register_dev(lock, &config, &dev_handle);
  193. if (ret == ESP_OK) {
  194. dev_id = spi_bus_lock_get_dev_id(dev_handle);
  195. } else if (ret == ESP_ERR_NOT_SUPPORTED) {
  196. ESP_LOGE(TAG, "No free CS.");
  197. } else if (ret == ESP_ERR_INVALID_ARG) {
  198. ESP_LOGE(TAG, "Bus lock not initialized (check CONFIG_SPI_FLASH_SHARE_SPI1_BUS).");
  199. }
  200. } else {
  201. const bool is_main_flash = (config->host_id == SPI1_HOST && config->cs_id == 0);
  202. if (config->cs_id >= SOC_SPI_PERIPH_CS_NUM(config->host_id) || config->cs_id < 0 || is_main_flash) {
  203. ESP_LOGE(TAG, "Not valid CS.");
  204. ret = ESP_ERR_INVALID_ARG;
  205. } else {
  206. dev_id = config->cs_id;
  207. assert(dev_handle == NULL);
  208. }
  209. }
  210. *out_dev_handle = dev_handle;
  211. *out_dev_id = dev_id;
  212. return ret;
  213. }
  214. esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_device_config_t *config)
  215. {
  216. if (out_chip == NULL) {
  217. return ESP_ERR_INVALID_ARG;
  218. }
  219. if (!GPIO_IS_VALID_OUTPUT_GPIO(config->cs_io_num)) {
  220. return ESP_ERR_INVALID_ARG;
  221. }
  222. esp_flash_t *chip = NULL;
  223. memspi_host_inst_t *host = NULL;
  224. esp_err_t ret = ESP_OK;
  225. uint32_t caps = MALLOC_CAP_DEFAULT;
  226. if (config->host_id == SPI1_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
  227. chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
  228. if (!chip) {
  229. ret = ESP_ERR_NO_MEM;
  230. goto fail;
  231. }
  232. host = (memspi_host_inst_t*)heap_caps_malloc(sizeof(memspi_host_inst_t), caps);
  233. *chip = (esp_flash_t) {
  234. .read_mode = config->io_mode,
  235. .host = (spi_flash_host_inst_t*)host,
  236. };
  237. if (!host) {
  238. ret = ESP_ERR_NO_MEM;
  239. goto fail;
  240. }
  241. int dev_id;
  242. spi_bus_lock_dev_handle_t dev_handle;
  243. esp_err_t err = acquire_spi_device(config, &dev_id, &dev_handle);
  244. if (err != ESP_OK) {
  245. ret = err;
  246. goto fail;
  247. }
  248. err = esp_flash_init_os_functions(chip, config->host_id, dev_handle);
  249. if (err != ESP_OK) {
  250. ret = err;
  251. goto fail;
  252. }
  253. //avoid conflicts with main flash
  254. assert(config->host_id != SPI1_HOST || dev_id != 0);
  255. bool use_iomux = spicommon_bus_using_iomux(config->host_id);
  256. memspi_host_config_t host_cfg = {
  257. .host_id = config->host_id,
  258. .cs_num = dev_id,
  259. .iomux = use_iomux,
  260. .input_delay_ns = config->input_delay_ns,
  261. .freq_mhz = config->freq_mhz,
  262. };
  263. host_cfg.clock_src_freq = spi_flash_ll_get_source_clock_freq_mhz(host_cfg.host_id);
  264. err = memspi_host_init_pointers(host, &host_cfg);
  265. if (err != ESP_OK) {
  266. ret = err;
  267. goto fail;
  268. }
  269. // The cs_id inside `config` is deprecated, use the `dev_id` provided by the bus lock instead.
  270. cs_initialize(chip, config, use_iomux, dev_id);
  271. *out_chip = chip;
  272. return ret;
  273. fail:
  274. // The memory allocated are free'd in the `spi_bus_remove_flash_device`.
  275. spi_bus_remove_flash_device(chip);
  276. return ret;
  277. }
  278. esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip)
  279. {
  280. if (chip == NULL) {
  281. return ESP_ERR_INVALID_ARG;
  282. }
  283. spi_bus_lock_dev_handle_t dev_handle = NULL;
  284. esp_flash_deinit_os_functions(chip, &dev_handle);
  285. if (dev_handle) {
  286. spi_bus_lock_unregister_dev(dev_handle);
  287. }
  288. free(chip->host);
  289. free(chip);
  290. return ESP_OK;
  291. }
  292. /* The default (ie initial boot) no-OS ROM esp_flash_os_functions_t */
  293. extern const esp_flash_os_functions_t esp_flash_noos_functions;
  294. static DRAM_ATTR memspi_host_inst_t esp_flash_default_host;
  295. static DRAM_ATTR esp_flash_t default_chip = {
  296. .read_mode = DEFAULT_FLASH_MODE,
  297. .host = (spi_flash_host_inst_t*)&esp_flash_default_host,
  298. .os_func = &esp_flash_noos_functions,
  299. };
  300. extern esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip);
  301. esp_err_t esp_flash_init_default_chip(void)
  302. {
  303. const esp_rom_spiflash_chip_t *legacy_chip = &g_rom_flashchip;
  304. memspi_host_config_t cfg = ESP_FLASH_HOST_CONFIG_DEFAULT();
  305. #if !CONFIG_IDF_TARGET_ESP32 && !CONFIG_IDF_TARGET_ESP32C2
  306. // For esp32s2 spi IOs are configured as from IO MUX by default
  307. cfg.iomux = esp_rom_efuse_get_flash_gpio_info() == 0 ? true : false;
  308. #endif
  309. #if CONFIG_ESPTOOLPY_OCT_FLASH
  310. cfg.octal_mode_en = 1;
  311. cfg.default_io_mode = DEFAULT_FLASH_MODE;
  312. #endif
  313. // For chips need time tuning, get value directely from system here.
  314. #if SOC_SPI_MEM_SUPPORT_TIME_TUNING
  315. if (spi_timing_is_tuned()) {
  316. cfg.using_timing_tuning = 1;
  317. spi_timing_get_flash_timing_param(&cfg.timing_reg);
  318. }
  319. #endif // SOC_SPI_MEM_SUPPORT_TIME_TUNING
  320. cfg.clock_src_freq = spi_flash_ll_get_source_clock_freq_mhz(cfg.host_id);
  321. //the host is already initialized, only do init for the data and load it to the host
  322. esp_err_t err = memspi_host_init_pointers(&esp_flash_default_host, &cfg);
  323. if (err != ESP_OK) {
  324. return err;
  325. }
  326. // ROM TODO: account for non-standard default pins in efuse
  327. // ROM TODO: to account for chips which are slow to power on, maybe keep probing in a loop here
  328. err = esp_flash_init_main(&default_chip);
  329. if (err != ESP_OK) {
  330. return err;
  331. }
  332. if (default_chip.size < legacy_chip->chip_size) {
  333. 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);
  334. return ESP_ERR_FLASH_SIZE_NOT_MATCH;
  335. }
  336. if (default_chip.size > legacy_chip->chip_size) {
  337. 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);
  338. }
  339. default_chip.size = legacy_chip->chip_size;
  340. esp_flash_default_chip = &default_chip;
  341. #ifdef CONFIG_SPI_FLASH_AUTO_SUSPEND
  342. err = esp_flash_suspend_cmd_init(&default_chip);
  343. if (err != ESP_OK) {
  344. return err;
  345. }
  346. #endif
  347. #if CONFIG_SPI_FLASH_HPM_ENABLE
  348. if (spi_flash_hpm_dummy_adjust()) {
  349. default_chip.hpm_dummy_ena = 1;
  350. }
  351. #endif
  352. return ESP_OK;
  353. }
  354. esp_err_t esp_flash_app_init(void)
  355. {
  356. esp_err_t err = ESP_OK;
  357. spi_flash_init_lock();
  358. spi_flash_guard_set(&g_flash_guard_default_ops);
  359. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  360. spi_flash_reset_counters();
  361. #endif
  362. #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS
  363. err = esp_flash_init_main_bus_lock();
  364. if (err != ESP_OK) return err;
  365. #endif
  366. err = esp_flash_app_enable_os_functions(&default_chip);
  367. return err;
  368. }