spi_flash_hal_common.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <string.h>
  15. #include <stdlib.h>
  16. #include "hal/spi_flash_hal.h"
  17. #include "hal/assert.h"
  18. #include "soc/soc_caps.h"
  19. #include "sdkconfig.h"
  20. #define ADDRESS_MASK_24BIT 0xFFFFFF
  21. #define COMPUTE_DUMMY_CYCLELEN(host, base) ((base) + ((spi_flash_hal_context_t*)host)->extra_dummy)
  22. static inline spi_dev_t *get_spi_dev(spi_flash_host_inst_t *host)
  23. {
  24. return ((spi_flash_hal_context_t*)host)->spi;
  25. }
  26. static inline int get_host_id(spi_flash_host_inst_t* host)
  27. {
  28. spi_dev_t *dev = get_spi_dev(host);
  29. return spi_flash_ll_hw_get_id(dev);
  30. }
  31. void spi_flash_hal_poll_cmd_done(spi_flash_host_inst_t *host)
  32. {
  33. while (!spi_flash_ll_cmd_is_done(get_spi_dev(host))) {
  34. //nop
  35. }
  36. }
  37. esp_err_t spi_flash_hal_device_config(spi_flash_host_inst_t *host)
  38. {
  39. spi_flash_hal_context_t* ctx = (spi_flash_hal_context_t*)host;
  40. spi_dev_t *dev = get_spi_dev(host);
  41. spi_flash_ll_reset(dev);
  42. spi_flash_ll_set_cs_pin(dev, ctx->cs_num);
  43. spi_flash_ll_set_clock(dev, &ctx->clock_conf);
  44. int cs_hold = ctx->cs_hold;
  45. spi_flash_ll_set_hold(dev, cs_hold);
  46. spi_flash_ll_set_cs_setup(dev, ctx->cs_setup);
  47. #ifndef GPSPI_BUILD
  48. #if SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
  49. if ((ctx->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND) != 0) {
  50. spi_flash_hal_setup_auto_suspend_mode(host);
  51. } else {
  52. spi_flash_hal_disable_auto_suspend_mode(host);
  53. }
  54. if ((ctx->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_RESUME) != 0) {
  55. spi_flash_hal_setup_auto_resume_mode(host);
  56. } else {
  57. spi_flash_hal_disable_auto_resume_mode(host);
  58. }
  59. #endif //SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
  60. #if SOC_SPI_MEM_SUPPORT_TIME_TUNING
  61. // Always keep the extra dummy on SPI1 is 0, add extra dummy to user dummy
  62. spimem_flash_ll_set_extra_dummy((spi_mem_dev_t*)dev, 0);
  63. #endif
  64. #else
  65. gpspi_flash_ll_set_hold_pol(dev, 1);
  66. #endif //GPSPI_BUILD
  67. return ESP_OK;
  68. }
  69. esp_err_t spi_flash_hal_configure_host_io_mode(
  70. spi_flash_host_inst_t *host,
  71. uint32_t command,
  72. uint32_t addr_bitlen,
  73. int dummy_cyclelen_base,
  74. esp_flash_io_mode_t io_mode)
  75. {
  76. spi_dev_t *dev = get_spi_dev(host);
  77. int host_id = spi_flash_ll_hw_get_id(dev);
  78. uint32_t extra_bits = io_mode & 0xFFFF0000;
  79. io_mode = io_mode & 0xFFFF;
  80. /*
  81. * Some flash chips, when working under some IO modes (DIO, QIO and OIO in the future), treat
  82. * the first 8 bits of the dummy bits as the bits. When the bits meet some pattern, the chip
  83. * will go into a "continuous (XIP)" mode, where the command field will be skipped in the next
  84. * transaction. We have to output all ones in these cycles because we don't need this feature.
  85. */
  86. bool conf_required = ((extra_bits & SPI_FLASH_CONFIG_CONF_BITS) != 0);
  87. if (!SOC_SPI_PERIPH_SUPPORT_MULTILINE_MODE(host_id) && io_mode > SPI_FLASH_FASTRD) {
  88. return ESP_ERR_NOT_SUPPORTED;
  89. }
  90. #if SOC_SPI_PERIPH_SUPPORT_CONTROL_DUMMY_OUTPUT
  91. // The CONTROL_DUMMY_OUTPUT feature is used to control M7-M0 bits.
  92. spi_flash_ll_set_dummy_out(dev, (conf_required? 1: 0), 1);
  93. #else
  94. // On ESP32, dummy output is not supported. These dummy bits will be moved into the address
  95. // phase (and appended as ones).
  96. if (conf_required) {
  97. int line_width = (io_mode == SPI_FLASH_DIO? 2: 4);
  98. dummy_cyclelen_base -= 4 / line_width;
  99. addr_bitlen += 4; //extra 4 bits indicate the conf bits is included
  100. }
  101. #endif
  102. if (command >= 0x100) {
  103. spi_flash_ll_set_command(dev, command, 16);
  104. } else {
  105. spi_flash_ll_set_command(dev, command, 8);
  106. }
  107. spi_flash_ll_set_addr_bitlen(dev, addr_bitlen);
  108. // Add dummy cycles to compensate for latency of GPIO matrix and external delay, if necessary...
  109. spi_flash_ll_set_dummy(dev, COMPUTE_DUMMY_CYCLELEN(host, dummy_cyclelen_base));
  110. //disable all data phases, enable them later if needed
  111. spi_flash_ll_set_miso_bitlen(dev, 0);
  112. spi_flash_ll_set_mosi_bitlen(dev, 0);
  113. spi_flash_ll_set_read_mode(dev, io_mode);
  114. return ESP_OK;
  115. }
  116. esp_err_t spi_flash_hal_common_command(spi_flash_host_inst_t *host, spi_flash_trans_t *trans)
  117. {
  118. spi_dev_t *dev = get_spi_dev(host);
  119. esp_flash_io_mode_t io_mode = ((spi_flash_hal_context_t*)host)->base_io_mode;
  120. uint16_t command;
  121. uint8_t dummy_bitlen;
  122. if (trans->reserved != 0) {
  123. // Back-compatible with caller functions of ESP32-S3 ROM
  124. command = (uint8_t)trans->reserved;
  125. dummy_bitlen = 0;
  126. } else {
  127. command = trans->command;
  128. dummy_bitlen = trans->dummy_bitlen;
  129. if ((trans->flags & SPI_FLASH_TRANS_FLAG_IGNORE_BASEIO) != 0) {
  130. io_mode = trans->io_mode;
  131. }
  132. }
  133. host->driver->configure_host_io_mode(host, command, trans->address_bitlen, dummy_bitlen, io_mode);
  134. spi_flash_ll_set_usr_address(dev, trans->address, trans->address_bitlen);
  135. //No extra dummy cycles for compensation if no input data
  136. if (trans->miso_len == 0) {
  137. spi_flash_ll_set_dummy(dev, dummy_bitlen);
  138. }
  139. spi_flash_ll_set_mosi_bitlen(dev, trans->mosi_len * 8);
  140. spi_flash_ll_set_buffer_data(dev, trans->mosi_data, trans->mosi_len);
  141. spi_flash_ll_set_miso_bitlen(dev, trans->miso_len * 8);
  142. spi_flash_ll_user_start(dev);
  143. host->driver->poll_cmd_done(host);
  144. if (trans->miso_len > 0) {
  145. spi_flash_ll_get_buffer_data(dev, trans->miso_data, trans->miso_len);
  146. }
  147. return ESP_OK;
  148. }
  149. esp_err_t spi_flash_hal_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len)
  150. {
  151. spi_dev_t *dev = get_spi_dev(host);
  152. int bitlen = spi_flash_ll_get_addr_bitlen(dev);
  153. //Only 24-bit and 32-bit address are supported. The extra length are for M7-M0, which should be
  154. //filled with ones by the function below
  155. spi_flash_ll_set_usr_address(dev, address, bitlen & (~7));
  156. spi_flash_ll_set_miso_bitlen(dev, read_len * 8);
  157. spi_flash_ll_user_start(dev);
  158. host->driver->poll_cmd_done(host);
  159. if (read_len > 0) {
  160. spi_flash_ll_get_buffer_data(dev, buffer, read_len);
  161. }
  162. return ESP_OK;
  163. }