esp_flash_spi_init.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #define ESP_FLASH_HOST_CONFIG_DEFAULT() (memspi_host_config_t){ \
  49. .host_id = SPI_HOST,\
  50. .speed = DEFAULT_FLASH_SPEED, \
  51. .cs_num = 0, \
  52. .iomux = false, \
  53. .input_delay_ns = 0,\
  54. }
  55. esp_flash_t *esp_flash_default_chip = NULL;
  56. static IRAM_ATTR NOINLINE_ATTR void cs_initialize(esp_flash_t *chip, const esp_flash_spi_device_config_t *config, bool use_iomux)
  57. {
  58. //Not using spicommon_cs_initialize since we don't want to put the whole
  59. //spi_periph_signal into the DRAM. Copy these data from flash before the
  60. //cache disabling
  61. int cs_io_num = config->cs_io_num;
  62. int spics_in = spi_periph_signal[config->host_id].spics_in;
  63. int spics_out = spi_periph_signal[config->host_id].spics_out[config->cs_id];
  64. int spics_func = spi_periph_signal[config->host_id].func;
  65. uint32_t iomux_reg = GPIO_PIN_MUX_REG[cs_io_num];
  66. //To avoid the panic caused by flash data line conflicts during cs line
  67. //initialization, disable the cache temporarily
  68. chip->os_func->start(chip->os_func_data);
  69. if (use_iomux) {
  70. // This requires `gpio_iomux_in` and `gpio_iomux_out` to be in the IRAM.
  71. // `linker.lf` is used fulfill this requirement.
  72. gpio_iomux_in(cs_io_num, spics_in);
  73. gpio_iomux_out(cs_io_num, spics_func, false);
  74. } else {
  75. PIN_INPUT_ENABLE(iomux_reg);
  76. if (cs_io_num < 32) {
  77. GPIO.enable_w1ts = (0x1 << cs_io_num);
  78. } else {
  79. GPIO.enable1_w1ts.data = (0x1 << (cs_io_num - 32));
  80. }
  81. GPIO.pin[cs_io_num].pad_driver = 0;
  82. gpio_matrix_out(cs_io_num, spics_out, false, false);
  83. if (config->cs_id == 0) {
  84. gpio_matrix_in(cs_io_num, spics_in, false);
  85. }
  86. PIN_FUNC_SELECT(iomux_reg, PIN_FUNC_GPIO);
  87. }
  88. chip->os_func->end(chip->os_func_data);
  89. }
  90. esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_device_config_t *config)
  91. {
  92. if (out_chip == NULL) {
  93. return ESP_ERR_INVALID_ARG;
  94. }
  95. esp_flash_t *chip = NULL;
  96. spi_flash_host_driver_t *host = NULL;
  97. memspi_host_data_t *host_data = NULL;
  98. esp_err_t ret = ESP_OK;
  99. uint32_t caps = MALLOC_CAP_DEFAULT;
  100. if (config->host_id == SPI_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
  101. chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
  102. if (!chip) {
  103. ret = ESP_ERR_NO_MEM;
  104. goto fail;
  105. }
  106. host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
  107. *chip = (esp_flash_t) {
  108. .read_mode = config->io_mode,
  109. .host = host,
  110. };
  111. if (!host) {
  112. ret = ESP_ERR_NO_MEM;
  113. goto fail;
  114. }
  115. host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
  116. host->driver_data = host_data;
  117. if (!host_data) {
  118. ret = ESP_ERR_NO_MEM;
  119. goto fail;
  120. }
  121. esp_err_t err = esp_flash_init_os_functions(chip, config->host_id);
  122. if (err != ESP_OK) {
  123. ret = err;
  124. goto fail;
  125. }
  126. bool use_iomux = spicommon_bus_using_iomux(config->host_id);
  127. memspi_host_config_t host_cfg = {
  128. .host_id = config->host_id,
  129. .cs_num = config->cs_id,
  130. .iomux = use_iomux,
  131. .input_delay_ns = config->input_delay_ns,
  132. .speed = config->speed,
  133. };
  134. err = memspi_host_init_pointers(host, host_data, &host_cfg);
  135. if (err != ESP_OK) {
  136. ret = err;
  137. goto fail;
  138. }
  139. cs_initialize(chip, config, use_iomux);
  140. *out_chip = chip;
  141. return ret;
  142. fail:
  143. // The memory allocated are free'd in the `spi_bus_remove_flash_device`.
  144. spi_bus_remove_flash_device(chip);
  145. return ret;
  146. }
  147. esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip)
  148. {
  149. if (chip==NULL) {
  150. return ESP_ERR_INVALID_ARG;
  151. }
  152. if (chip->host) {
  153. free(chip->host->driver_data);
  154. free(chip->host);
  155. }
  156. free(chip);
  157. return ESP_OK;
  158. }
  159. /* The default (ie initial boot) no-OS ROM esp_flash_os_functions_t */
  160. extern const esp_flash_os_functions_t esp_flash_noos_functions;
  161. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  162. static DRAM_ATTR memspi_host_data_t default_driver_data;
  163. static DRAM_ATTR spi_flash_host_driver_t esp_flash_default_host_drv = ESP_FLASH_DEFAULT_HOST_DRIVER();
  164. static DRAM_ATTR esp_flash_t default_chip = {
  165. .read_mode = DEFAULT_FLASH_MODE,
  166. .host = &esp_flash_default_host_drv,
  167. .os_func = &esp_flash_noos_functions,
  168. };
  169. esp_err_t esp_flash_init_default_chip(void)
  170. {
  171. memspi_host_config_t cfg = ESP_FLASH_HOST_CONFIG_DEFAULT();
  172. //the host is already initialized, only do init for the data and load it to the host
  173. spi_flash_hal_init(&default_driver_data, &cfg);
  174. default_chip.host->driver_data = &default_driver_data;
  175. // ROM TODO: account for non-standard default pins in efuse
  176. // ROM TODO: to account for chips which are slow to power on, maybe keep probing in a loop here
  177. esp_err_t err = esp_flash_init(&default_chip);
  178. if (err != ESP_OK) {
  179. return err;
  180. }
  181. if (default_chip.size < g_rom_flashchip.chip_size) {
  182. 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);
  183. return ESP_ERR_FLASH_SIZE_NOT_MATCH;
  184. } else if (default_chip.size > g_rom_flashchip.chip_size) {
  185. 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);
  186. default_chip.size = g_rom_flashchip.chip_size;
  187. }
  188. default_chip.size = g_rom_flashchip.chip_size;
  189. esp_flash_default_chip = &default_chip;
  190. return ESP_OK;
  191. }
  192. esp_err_t esp_flash_app_init(void)
  193. {
  194. return esp_flash_app_init_os_functions(&default_chip);
  195. }
  196. #endif