memspi_host_driver.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "spi_flash_defs.h"
  15. #include "memspi_host_driver.h"
  16. #include "string.h"
  17. #include "esp_log.h"
  18. #include "cache_utils.h"
  19. #include "esp_flash_partitions.h"
  20. static const char TAG[] = "memspi";
  21. static const spi_flash_host_driver_t esp_flash_default_host = ESP_FLASH_DEFAULT_HOST_DRIVER();
  22. #ifdef CONFIG_IDF_TARGET_ESP32S2
  23. extern void spi_flash_hal_gpspi_poll_cmd_done(spi_flash_host_driver_t *driver);
  24. extern esp_err_t spi_flash_hal_gpspi_device_config(spi_flash_host_driver_t *driver);
  25. esp_err_t spi_flash_hal_gpspi_configure_host_io_mode(
  26. spi_flash_host_driver_t *host,
  27. uint32_t command,
  28. uint32_t addr_bitlen,
  29. int dummy_cyclelen_base,
  30. esp_flash_io_mode_t io_mode);
  31. extern esp_err_t spi_flash_hal_gpspi_common_command(spi_flash_host_driver_t *chip_drv, spi_flash_trans_t *trans);
  32. extern esp_err_t spi_flash_hal_gpspi_read(spi_flash_host_driver_t *chip_drv, void *buffer, uint32_t address, uint32_t read_len);
  33. extern bool spi_flash_hal_gpspi_host_idle(spi_flash_host_driver_t *chip_drv);
  34. extern bool spi_flash_hal_gpspi_supports_direct_write(spi_flash_host_driver_t *driver, const void *p);
  35. extern bool spi_flash_hal_gpspi_supports_direct_read(spi_flash_host_driver_t *driver, const void *p);
  36. /** Default configuration for GPSPI */
  37. static const spi_flash_host_driver_t esp_flash_gpspi_host = {
  38. .dev_config = spi_flash_hal_gpspi_device_config,
  39. .common_command = spi_flash_hal_gpspi_common_command,
  40. .read_id = memspi_host_read_id_hs,
  41. .erase_chip = memspi_host_erase_chip,
  42. .erase_sector = memspi_host_erase_sector,
  43. .erase_block = memspi_host_erase_block,
  44. .read_status = memspi_host_read_status_hs,
  45. .set_write_protect = memspi_host_set_write_protect,
  46. .supports_direct_write = spi_flash_hal_gpspi_supports_direct_write,
  47. .supports_direct_read = spi_flash_hal_gpspi_supports_direct_read,
  48. .program_page = memspi_host_program_page,
  49. .max_write_bytes = SPI_FLASH_HAL_MAX_WRITE_BYTES,
  50. .read = spi_flash_hal_gpspi_read,
  51. .max_read_bytes = SPI_FLASH_HAL_MAX_READ_BYTES,
  52. .host_idle = spi_flash_hal_gpspi_host_idle,
  53. .configure_host_io_mode = spi_flash_hal_gpspi_configure_host_io_mode,
  54. .poll_cmd_done = spi_flash_hal_gpspi_poll_cmd_done,
  55. .flush_cache = NULL,
  56. };
  57. #endif
  58. esp_err_t memspi_host_init_pointers(spi_flash_host_driver_t *host, memspi_host_data_t *data, const memspi_host_config_t *cfg)
  59. {
  60. #ifdef CONFIG_IDF_TARGET_ESP32
  61. memcpy(host, &esp_flash_default_host, sizeof(spi_flash_host_driver_t));
  62. #elif CONFIG_IDF_TARGET_ESP32S2
  63. if (cfg->host_id == SPI_HOST)
  64. memcpy(host, &esp_flash_default_host, sizeof(spi_flash_host_driver_t));
  65. else {
  66. memcpy(host, &esp_flash_gpspi_host, sizeof(spi_flash_host_driver_t));
  67. }
  68. #endif
  69. esp_err_t err = spi_flash_hal_init(data, cfg);
  70. if (err != ESP_OK) {
  71. return err;
  72. }
  73. host->driver_data = data;
  74. //some functions are not required if not SPI1
  75. if ((void*)data->spi != (void*)spi_flash_ll_get_hw(SPI_HOST)) {
  76. host->flush_cache = NULL;
  77. }
  78. return ESP_OK;
  79. }
  80. esp_err_t memspi_host_read_id_hs(spi_flash_host_driver_t *host, uint32_t *id)
  81. {
  82. uint32_t id_buf = 0;
  83. spi_flash_trans_t t = {
  84. .command = CMD_RDID,
  85. .miso_len = 3,
  86. .miso_data = ((uint8_t*) &id_buf),
  87. };
  88. host->common_command(host, &t);
  89. uint32_t raw_flash_id = id_buf;
  90. ESP_EARLY_LOGV(TAG, "raw_chip_id: %X\n", raw_flash_id);
  91. if (raw_flash_id == 0xFFFFFF || raw_flash_id == 0) {
  92. ESP_EARLY_LOGE(TAG, "no response\n");
  93. return ESP_ERR_FLASH_NO_RESPONSE;
  94. }
  95. // Byte swap the flash id as it's usually written the other way around
  96. uint8_t mfg_id = raw_flash_id & 0xFF;
  97. uint16_t flash_id = (raw_flash_id >> 16) | (raw_flash_id & 0xFF00);
  98. *id = ((uint32_t)mfg_id << 16) | flash_id;
  99. ESP_EARLY_LOGV(TAG, "chip_id: %X\n", *id);
  100. return ESP_OK;
  101. }
  102. esp_err_t memspi_host_read_status_hs(spi_flash_host_driver_t *driver, uint8_t *out_sr)
  103. {
  104. //NOTE: we do have a read id function, however it doesn't work in high freq
  105. uint32_t stat_buf = 0;
  106. spi_flash_trans_t t = {
  107. .command = CMD_RDSR,
  108. .miso_data = ((uint8_t*) &stat_buf),
  109. .miso_len = 1
  110. };
  111. esp_err_t err = driver->common_command(driver, &t);
  112. if (err != ESP_OK) {
  113. return err;
  114. }
  115. *out_sr = stat_buf;
  116. return ESP_OK;
  117. }
  118. esp_err_t memspi_host_flush_cache(spi_flash_host_driver_t* driver, uint32_t addr, uint32_t size)
  119. {
  120. if ((void*)((memspi_host_data_t*)(driver->driver_data))->spi == (void*) spi_flash_ll_get_hw(SPI_HOST)) {
  121. spi_flash_check_and_flush_cache(addr, size);
  122. }
  123. return ESP_OK;
  124. }
  125. void memspi_host_erase_chip(spi_flash_host_driver_t *chip_drv)
  126. {
  127. spi_flash_trans_t t = { 0 };
  128. t.command = CMD_CHIP_ERASE;
  129. chip_drv->common_command(chip_drv, &t);
  130. }
  131. void memspi_host_erase_sector(spi_flash_host_driver_t *chip_drv, uint32_t start_address)
  132. {
  133. spi_flash_trans_t t = {
  134. .command = CMD_SECTOR_ERASE,
  135. .address_bitlen = 24,
  136. .address = start_address
  137. };
  138. chip_drv->common_command(chip_drv, &t);
  139. }
  140. void memspi_host_erase_block(spi_flash_host_driver_t *chip_drv, uint32_t start_address)
  141. {
  142. spi_flash_trans_t t = {
  143. .command = CMD_LARGE_BLOCK_ERASE,
  144. .address_bitlen = 24,
  145. .address = start_address,
  146. };
  147. chip_drv->common_command(chip_drv, &t);
  148. }
  149. void memspi_host_program_page(spi_flash_host_driver_t *chip_drv, const void *buffer, uint32_t address, uint32_t length)
  150. {
  151. spi_flash_trans_t t = {
  152. .command = CMD_PROGRAM_PAGE,
  153. .address_bitlen = 24,
  154. .address = address,
  155. .mosi_len = length,
  156. .mosi_data = buffer
  157. };
  158. chip_drv->common_command(chip_drv, &t);
  159. }
  160. esp_err_t memspi_host_read(spi_flash_host_driver_t *chip_drv, void *buffer, uint32_t address, uint32_t read_len)
  161. {
  162. spi_flash_trans_t t = {
  163. .address_bitlen = 24,
  164. .address = address,
  165. .miso_len = read_len,
  166. .miso_data = buffer
  167. };
  168. chip_drv->common_command(chip_drv, &t);
  169. return ESP_OK;
  170. }
  171. esp_err_t memspi_host_set_write_protect(spi_flash_host_driver_t *chip_drv, bool wp)
  172. {
  173. spi_flash_trans_t t = {
  174. .command = wp ? CMD_WRDI : CMD_WREN
  175. };
  176. chip_drv->common_command(chip_drv, &t);
  177. return ESP_OK;
  178. }