esp_flash_spi_init.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. uint32_t iomux_reg = GPIO_PIN_MUX_REG[cs_io_num];
  65. //To avoid the panic caused by flash data line conflicts during cs line
  66. //initialization, disable the cache temporarily
  67. chip->os_func->start(chip->os_func_data);
  68. if (use_iomux) {
  69. GPIO.func_in_sel_cfg[spics_in].sig_in_sel = 0;
  70. PIN_INPUT_ENABLE(iomux_reg);
  71. GPIO.func_out_sel_cfg[spics_out].oen_sel = 0;
  72. GPIO.func_out_sel_cfg[spics_out].oen_inv_sel = false;
  73. PIN_FUNC_SELECT(iomux_reg, 1);
  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. host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
  103. host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
  104. if (!chip || !host || !host_data) {
  105. ret = ESP_ERR_NO_MEM;
  106. goto fail;
  107. }
  108. *chip = (esp_flash_t) {
  109. .read_mode = config->io_mode,
  110. .host = host,
  111. };
  112. esp_err_t err = esp_flash_init_os_functions(chip, config->host_id);
  113. if (err != ESP_OK) {
  114. ret = err;
  115. goto fail;
  116. }
  117. bool use_iomux = spicommon_bus_using_iomux(config->host_id);
  118. memspi_host_config_t host_cfg = {
  119. .host_id = config->host_id,
  120. .cs_num = config->cs_id,
  121. .iomux = use_iomux,
  122. .input_delay_ns = config->input_delay_ns,
  123. .speed = config->speed,
  124. };
  125. err = memspi_host_init_pointers(host, host_data, &host_cfg);
  126. if (err != ESP_OK) {
  127. ret = err;
  128. goto fail;
  129. }
  130. cs_initialize(chip, config, use_iomux);
  131. *out_chip = chip;
  132. return ret;
  133. fail:
  134. spi_bus_remove_flash_device(chip);
  135. return ret;
  136. }
  137. esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip)
  138. {
  139. if (chip==NULL) {
  140. return ESP_ERR_INVALID_ARG;
  141. }
  142. if (chip->host) {
  143. free(chip->host->driver_data);
  144. free(chip->host);
  145. }
  146. free(chip);
  147. return ESP_OK;
  148. }
  149. /* The default (ie initial boot) no-OS ROM esp_flash_os_functions_t */
  150. extern const esp_flash_os_functions_t esp_flash_noos_functions;
  151. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  152. static DRAM_ATTR memspi_host_data_t default_driver_data;
  153. static DRAM_ATTR spi_flash_host_driver_t esp_flash_default_host_drv = ESP_FLASH_DEFAULT_HOST_DRIVER();
  154. static DRAM_ATTR esp_flash_t default_chip = {
  155. .read_mode = DEFAULT_FLASH_MODE,
  156. .host = &esp_flash_default_host_drv,
  157. .os_func = &esp_flash_noos_functions,
  158. };
  159. esp_err_t esp_flash_init_default_chip()
  160. {
  161. memspi_host_config_t cfg = ESP_FLASH_HOST_CONFIG_DEFAULT();
  162. //the host is already initialized, only do init for the data and load it to the host
  163. spi_flash_hal_init(&default_driver_data, &cfg);
  164. default_chip.host->driver_data = &default_driver_data;
  165. // ROM TODO: account for non-standard default pins in efuse
  166. // ROM TODO: to account for chips which are slow to power on, maybe keep probing in a loop here
  167. esp_err_t err = esp_flash_init(&default_chip);
  168. if (err != ESP_OK) {
  169. return err;
  170. }
  171. if (default_chip.size < g_rom_flashchip.chip_size) {
  172. 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);
  173. return ESP_ERR_FLASH_SIZE_NOT_MATCH;
  174. } else if (default_chip.size > g_rom_flashchip.chip_size) {
  175. 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);
  176. default_chip.size = g_rom_flashchip.chip_size;
  177. }
  178. default_chip.size = g_rom_flashchip.chip_size;
  179. esp_flash_default_chip = &default_chip;
  180. return ESP_OK;
  181. }
  182. esp_err_t esp_flash_app_init()
  183. {
  184. return esp_flash_app_init_os_functions(&default_chip);
  185. }
  186. #endif