memspi_host_driver.c
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 15 #include "spi_flash_defs.h" 16 #include "memspi_host_driver.h" 17 #include "string.h" 18 #include "esp_log.h" 19 #include "cache_utils.h" 20 #include "esp_flash_partitions.h" 21 22 #define SPI_FLASH_HAL_MAX_WRITE_BYTES 64 23 #define SPI_FLASH_HAL_MAX_READ_BYTES 64 24 25 static const char TAG[] = "memspi"; 26 DRAM_ATTR static const spi_flash_host_driver_t esp_flash_default_host = ESP_FLASH_DEFAULT_HOST_DRIVER(); 27 28 #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 29 extern void spi_flash_hal_gpspi_poll_cmd_done(spi_flash_host_inst_t *host); 30 extern esp_err_t spi_flash_hal_gpspi_device_config(spi_flash_host_inst_t *host); 31 esp_err_t spi_flash_hal_gpspi_configure_host_io_mode( 32 spi_flash_host_inst_t *host, 33 uint32_t command, 34 uint32_t addr_bitlen, 35 int dummy_cyclelen_base, 36 esp_flash_io_mode_t io_mode); 37 extern esp_err_t spi_flash_hal_gpspi_common_command(spi_flash_host_inst_t *host, spi_flash_trans_t *trans); 38 extern esp_err_t spi_flash_hal_gpspi_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len); 39 extern bool spi_flash_hal_gpspi_host_idle(spi_flash_host_inst_t *host); 40 extern bool spi_flash_hal_gpspi_supports_direct_write(spi_flash_host_inst_t *host, const void *p); 41 extern bool spi_flash_hal_gpspi_supports_direct_read(spi_flash_host_inst_t *host, const void *p); 42 43 /** Default configuration for GPSPI */ 44 static const spi_flash_host_driver_t esp_flash_gpspi_host = { 45 .dev_config = spi_flash_hal_gpspi_device_config, 46 .common_command = spi_flash_hal_gpspi_common_command, 47 .read_id = memspi_host_read_id_hs, 48 .erase_chip = memspi_host_erase_chip, 49 .erase_sector = memspi_host_erase_sector, 50 .erase_block = memspi_host_erase_block, 51 .read_status = memspi_host_read_status_hs, 52 .set_write_protect = memspi_host_set_write_protect, 53 .supports_direct_write = spi_flash_hal_gpspi_supports_direct_write, 54 .supports_direct_read = spi_flash_hal_gpspi_supports_direct_read, 55 .program_page = memspi_host_program_page, 56 .write_data_slicer = memspi_host_write_data_slicer, 57 .read = spi_flash_hal_gpspi_read, 58 .read_data_slicer = memspi_host_read_data_slicer, 59 .host_idle = spi_flash_hal_gpspi_host_idle, 60 .configure_host_io_mode = spi_flash_hal_gpspi_configure_host_io_mode, 61 .poll_cmd_done = spi_flash_hal_gpspi_poll_cmd_done, 62 .flush_cache = NULL, 63 }; 64 #endif 65 66 esp_err_t memspi_host_init_pointers(memspi_host_inst_t *host, const memspi_host_config_t *cfg) 67 { 68 #ifdef CONFIG_IDF_TARGET_ESP32 69 host->inst.driver = &esp_flash_default_host; 70 #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 71 if (cfg->host_id == SPI_HOST) 72 host->inst.driver = &esp_flash_default_host; 73 else { 74 host->inst.driver = &esp_flash_gpspi_host; 75 } 76 #endif 77 78 esp_err_t err = spi_flash_hal_init(host, cfg); 79 return err; 80 } 81 82 esp_err_t memspi_host_read_id_hs(spi_flash_host_inst_t *host, uint32_t *id) 83 { 84 uint32_t id_buf = 0; 85 spi_flash_trans_t t = { 86 .command = CMD_RDID, 87 .miso_len = 3, 88 .miso_data = ((uint8_t*) &id_buf), 89 }; 90 host->driver->common_command(host, &t); 91 92 uint32_t raw_flash_id = id_buf; 93 ESP_EARLY_LOGV(TAG, "raw_chip_id: %X\n", raw_flash_id); 94 if (raw_flash_id == 0xFFFFFF || raw_flash_id == 0) { 95 ESP_EARLY_LOGE(TAG, "no response\n"); 96 return ESP_ERR_FLASH_NO_RESPONSE; 97 } 98 // Byte swap the flash id as it's usually written the other way around 99 uint8_t mfg_id = raw_flash_id & 0xFF; 100 uint16_t flash_id = (raw_flash_id >> 16) | (raw_flash_id & 0xFF00); 101 *id = ((uint32_t)mfg_id << 16) | flash_id; 102 ESP_EARLY_LOGV(TAG, "chip_id: %X\n", *id); 103 return ESP_OK; 104 } 105 106 esp_err_t memspi_host_read_status_hs(spi_flash_host_inst_t *host, uint8_t *out_sr) 107 { 108 //NOTE: we do have a read id function, however it doesn't work in high freq 109 uint32_t stat_buf = 0; 110 spi_flash_trans_t t = { 111 .command = CMD_RDSR, 112 .miso_data = ((uint8_t*) &stat_buf), 113 .miso_len = 1 114 }; 115 esp_err_t err = host->driver->common_command(host, &t); 116 if (err != ESP_OK) { 117 return err; 118 } 119 *out_sr = stat_buf; 120 return ESP_OK; 121 } 122 123 esp_err_t memspi_host_flush_cache(spi_flash_host_inst_t *host, uint32_t addr, uint32_t size) 124 { 125 if ((void*)((memspi_host_inst_t*)host)->spi == (void*) spi_flash_ll_get_hw(SPI_HOST)) { 126 spi_flash_check_and_flush_cache(addr, size); 127 } 128 return ESP_OK; 129 } 130 131 void memspi_host_erase_chip(spi_flash_host_inst_t *host) 132 { 133 spi_flash_trans_t t = { 0 }; 134 t.command = CMD_CHIP_ERASE; 135 host->driver->common_command(host, &t); 136 } 137 138 // Only support 24bit address 139 void memspi_host_erase_sector(spi_flash_host_inst_t *host, uint32_t start_address) 140 { 141 assert(start_address < 0x1000000); 142 spi_flash_trans_t t = { 143 .command = CMD_SECTOR_ERASE, 144 .address_bitlen = 24, 145 .address = start_address 146 }; 147 host->driver->common_command(host, &t); 148 } 149 150 // Only support 24bit address 151 void memspi_host_erase_block(spi_flash_host_inst_t *host, uint32_t start_address) 152 { 153 assert(start_address < 0x1000000); 154 spi_flash_trans_t t = { 155 .command = CMD_LARGE_BLOCK_ERASE, 156 .address_bitlen = 24, 157 .address = start_address, 158 }; 159 host->driver->common_command(host, &t); 160 } 161 162 // Only support 24bit address 163 void memspi_host_program_page(spi_flash_host_inst_t *host, const void *buffer, uint32_t address, uint32_t length) 164 { 165 assert(address + length <= 0x1000000); 166 spi_flash_trans_t t = { 167 .command = CMD_PROGRAM_PAGE, 168 .address_bitlen = 24, 169 .address = address, 170 .mosi_len = length, 171 .mosi_data = buffer 172 }; 173 host->driver->common_command(host, &t); 174 } 175 176 esp_err_t memspi_host_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len) 177 { 178 spi_flash_trans_t t = { 179 .command = CMD_READ, 180 .address_bitlen = 24, 181 .address = address, 182 .miso_len = read_len, 183 .miso_data = buffer 184 }; 185 host->driver->common_command(host, &t); 186 return ESP_OK; 187 } 188 189 esp_err_t memspi_host_set_write_protect(spi_flash_host_inst_t *host, bool wp) 190 { 191 spi_flash_trans_t t = { 192 .command = wp ? CMD_WRDI : CMD_WREN 193 }; 194 host->driver->common_command(host, &t); 195 return ESP_OK; 196 } 197 198 // When encryption is enabled, etc. the data slicer may be complicated 199 // This is the simple case where the hardware has no other requirements than the size and page boundary 200 int memspi_host_write_data_slicer(spi_flash_host_inst_t *host, uint32_t address, uint32_t len, uint32_t *align_address, uint32_t page_size) 201 { 202 uint32_t align_addr = address; 203 uint32_t end_bound = (align_addr/page_size + 1) * page_size; 204 // Shouldn't program cross the page, or longer than SPI_FLASH_HAL_MAX_WRITE_BYTES 205 uint32_t max_len = MIN(end_bound - align_addr, SPI_FLASH_HAL_MAX_WRITE_BYTES); 206 *align_address = address; 207 return MIN(max_len, len); 208 } 209 210 int memspi_host_read_data_slicer(spi_flash_host_inst_t *host, uint32_t address, uint32_t len, uint32_t *align_address, uint32_t page_size) 211 { 212 // Shouldn't read longer than SPI_FLASH_HAL_MAX_READ_BYTES 213 uint32_t max_len = SPI_FLASH_HAL_MAX_READ_BYTES; 214 *align_address = address; 215 return MIN(max_len, len); 216 } 217