esp_flash_spi_init.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "sdkconfig.h"
  15. #include "esp_flash.h"
  16. #include "memspi_host_driver.h"
  17. #include "esp_flash_spi_init.h"
  18. #include "driver/gpio.h"
  19. #include "esp32/rom/spi_flash.h"
  20. #include "esp_log.h"
  21. #include "esp_heap_caps.h"
  22. #include "hal/spi_types.h"
  23. #include "driver/spi_common_internal.h"
  24. #include "esp_flash_internal.h"
  25. __attribute__((unused)) static const char TAG[] = "spi_flash";
  26. #ifdef CONFIG_ESPTOOLPY_FLASHFREQ_80M
  27. #define DEFAULT_FLASH_SPEED ESP_FLASH_80MHZ
  28. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_40M
  29. #define DEFAULT_FLASH_SPEED ESP_FLASH_40MHZ
  30. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_26M
  31. #define DEFAULT_FLASH_SPEED ESP_FLASH_26MHZ
  32. #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_20M
  33. #define DEFAULT_FLASH_SPEED ESP_FLASH_20MHZ
  34. #else
  35. #error Flash frequency not defined! Check the ``CONFIG_ESPTOOLPY_FLASHFREQ_*`` options.
  36. #endif
  37. #if defined(CONFIG_ESPTOOLPY_FLASHMODE_QIO)
  38. #define DEFAULT_FLASH_MODE SPI_FLASH_QIO
  39. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_QOUT)
  40. #define DEFAULT_FLASH_MODE SPI_FLASH_QOUT
  41. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_DIO)
  42. #define DEFAULT_FLASH_MODE SPI_FLASH_DIO
  43. #elif defined(CONFIG_ESPTOOLPY_FLASHMODE_DOUT)
  44. #define DEFAULT_FLASH_MODE SPI_FLASH_DOUT
  45. #else
  46. #define DEFAULT_FLASH_MODE SPI_FLASH_FASTRD
  47. #endif
  48. #if CONFIG_IDF_TARGET_ESP32
  49. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  50. .host_id = SPI_HOST,\
  51. .speed = DEFAULT_FLASH_SPEED, \
  52. .cs_num = 0, \
  53. .iomux = false, \
  54. .input_delay_ns = 0,\
  55. }
  56. #elif CONFIG_IDF_TARGET_ESP32S2
  57. #include "esp32s2/rom/efuse.h"
  58. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  59. .host_id = SPI_HOST,\
  60. .speed = DEFAULT_FLASH_SPEED, \
  61. .cs_num = 0, \
  62. .iomux = true, \
  63. .input_delay_ns = 0,\
  64. }
  65. #endif
  66. esp_flash_t *esp_flash_default_chip = NULL;
  67. 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)
  68. {
  69. //Not using spicommon_cs_initialize since we don't want to put the whole
  70. //spi_periph_signal into the DRAM. Copy these data from flash before the
  71. //cache disabling
  72. int cs_io_num = config->cs_io_num;
  73. int spics_in = spi_periph_signal[config->host_id].spics_in;
  74. int spics_out = spi_periph_signal[config->host_id].spics_out[cs_id];
  75. int spics_func = spi_periph_signal[config->host_id].func;
  76. uint32_t iomux_reg = GPIO_PIN_MUX_REG[cs_io_num];
  77. //To avoid the panic caused by flash data line conflicts during cs line
  78. //initialization, disable the cache temporarily
  79. chip->os_func->start(chip->os_func_data);
  80. if (use_iomux) {
  81. // This requires `gpio_iomux_in` and `gpio_iomux_out` to be in the IRAM.
  82. // `linker.lf` is used fulfill this requirement.
  83. gpio_iomux_in(cs_io_num, spics_in);
  84. gpio_iomux_out(cs_io_num, spics_func, false);
  85. } else {
  86. PIN_INPUT_ENABLE(iomux_reg);
  87. if (cs_io_num < 32) {
  88. GPIO.enable_w1ts = (0x1 << cs_io_num);
  89. } else {
  90. GPIO.enable1_w1ts.data = (0x1 << (cs_io_num - 32));
  91. }
  92. GPIO.pin[cs_io_num].pad_driver = 0;
  93. gpio_matrix_out(cs_io_num, spics_out, false, false);
  94. if (cs_id == 0) {
  95. gpio_matrix_in(cs_io_num, spics_in, false);
  96. }
  97. PIN_FUNC_SELECT(iomux_reg, PIN_FUNC_GPIO);
  98. }
  99. chip->os_func->end(chip->os_func_data);
  100. }
  101. esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_device_config_t *config)
  102. {
  103. if (out_chip == NULL) {
  104. return ESP_ERR_INVALID_ARG;
  105. }
  106. esp_flash_t *chip = NULL;
  107. spi_flash_host_driver_t *host = NULL;
  108. memspi_host_data_t *host_data = NULL;
  109. esp_err_t ret = ESP_OK;
  110. uint32_t caps = MALLOC_CAP_DEFAULT;
  111. if (config->host_id == SPI_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
  112. chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
  113. if (!chip) {
  114. ret = ESP_ERR_NO_MEM;
  115. goto fail;
  116. }
  117. host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
  118. *chip = (esp_flash_t) {
  119. .read_mode = config->io_mode,
  120. .host = host,
  121. };
  122. if (!host) {
  123. ret = ESP_ERR_NO_MEM;
  124. goto fail;
  125. }
  126. host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
  127. host->driver_data = host_data;
  128. if (!host_data) {
  129. ret = ESP_ERR_NO_MEM;
  130. goto fail;
  131. }
  132. int dev_id = -1;
  133. esp_err_t err = esp_flash_init_os_functions(chip, config->host_id, &dev_id);
  134. if (err == ESP_ERR_NOT_SUPPORTED) {
  135. ESP_LOGE(TAG, "Init os functions failed! No free CS.");
  136. } else if (err == ESP_ERR_INVALID_ARG) {
  137. ESP_LOGE(TAG, "Init os functions failed! Bus lock not initialized (check CONFIG_SPI_FLASH_SHARE_SPI1_BUS).");
  138. }
  139. if (err != ESP_OK) {
  140. ret = err;
  141. goto fail;
  142. }
  143. // When `CONFIG_SPI_FLASH_SHARE_SPI1_BUS` is not enabled on SPI1 bus, the
  144. // `esp_flash_init_os_functions` will not be able to assign a new device ID. In this case, we
  145. // use the `cs_id` in the config structure.
  146. if (dev_id == -1 && config->host_id == SPI_HOST) {
  147. dev_id = config->cs_id;
  148. }
  149. assert(dev_id < SOC_SPI_PERIPH_CS_NUM(config->host_id) && dev_id >= 0);
  150. bool use_iomux = spicommon_bus_using_iomux(config->host_id);
  151. memspi_host_config_t host_cfg = {
  152. .host_id = config->host_id,
  153. .cs_num = dev_id,
  154. .iomux = use_iomux,
  155. .input_delay_ns = config->input_delay_ns,
  156. .speed = config->speed,
  157. };
  158. err = memspi_host_init_pointers(host, host_data, &host_cfg);
  159. if (err != ESP_OK) {
  160. ret = err;
  161. goto fail;
  162. }
  163. // The cs_id inside `config` is deprecated, use the `dev_id` provided by the bus lock instead.
  164. cs_initialize(chip, config, use_iomux, dev_id);
  165. *out_chip = chip;
  166. return ret;
  167. fail:
  168. // The memory allocated are free'd in the `spi_bus_remove_flash_device`.
  169. spi_bus_remove_flash_device(chip);
  170. return ret;
  171. }
  172. esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip)
  173. {
  174. if (chip==NULL) {
  175. return ESP_ERR_INVALID_ARG;
  176. }
  177. esp_flash_deinit_os_functions(chip);
  178. if (chip->host) {
  179. free(chip->host->driver_data);
  180. free(chip->host);
  181. }
  182. free(chip);
  183. return ESP_OK;
  184. }
  185. /* The default (ie initial boot) no-OS ROM esp_flash_os_functions_t */
  186. extern const esp_flash_os_functions_t esp_flash_noos_functions;
  187. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  188. static DRAM_ATTR memspi_host_data_t default_driver_data;
  189. static DRAM_ATTR spi_flash_host_driver_t esp_flash_default_host_drv = ESP_FLASH_DEFAULT_HOST_DRIVER();
  190. static DRAM_ATTR esp_flash_t default_chip = {
  191. .read_mode = DEFAULT_FLASH_MODE,
  192. .host = &esp_flash_default_host_drv,
  193. .os_func = &esp_flash_noos_functions,
  194. };
  195. esp_err_t esp_flash_init_default_chip(void)
  196. {
  197. memspi_host_config_t cfg = ESP_FLASH_HOST_CONFIG_DEFAULT();
  198. #ifdef CONFIG_IDF_TARGET_ESP32S2
  199. // For esp32s2 spi IOs are configured as from IO MUX by default
  200. cfg.iomux = ets_efuse_get_spiconfig() == 0 ? true : false;
  201. #endif
  202. //the host is already initialized, only do init for the data and load it to the host
  203. spi_flash_hal_init(&default_driver_data, &cfg);
  204. default_chip.host->driver_data = &default_driver_data;
  205. // ROM TODO: account for non-standard default pins in efuse
  206. // ROM TODO: to account for chips which are slow to power on, maybe keep probing in a loop here
  207. esp_err_t err = esp_flash_init(&default_chip);
  208. if (err != ESP_OK) {
  209. return err;
  210. }
  211. if (default_chip.size < g_rom_flashchip.chip_size) {
  212. ESP_EARLY_LOGE(TAG, "Detected size(%dk) smaller than the size in the binary image header(%dk). Probe failed.", default_chip.size/1024, g_rom_flashchip.chip_size/1024);
  213. return ESP_ERR_FLASH_SIZE_NOT_MATCH;
  214. } else if (default_chip.size > g_rom_flashchip.chip_size) {
  215. 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, g_rom_flashchip.chip_size/1024);
  216. default_chip.size = g_rom_flashchip.chip_size;
  217. }
  218. default_chip.size = g_rom_flashchip.chip_size;
  219. esp_flash_default_chip = &default_chip;
  220. return ESP_OK;
  221. }
  222. esp_err_t esp_flash_app_init(void)
  223. {
  224. esp_err_t err = ESP_OK;
  225. #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS
  226. err = esp_flash_init_main_bus_lock();
  227. if (err != ESP_OK) return err;
  228. #endif
  229. err = esp_flash_app_enable_os_functions(&default_chip);
  230. return err;
  231. }
  232. #endif //!CONFIG_SPI_FLASH_USE_LEGACY_IMPL