spi_flash_chip_winbond.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2015-2020 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 <stdlib.h>
  15. #include <string.h>
  16. #include <sys/param.h> // For MIN/MAX
  17. #include "esp_log.h"
  18. #include "spi_flash_chip_generic.h"
  19. #include "spi_flash_defs.h"
  20. #define REGION_32BIT(start, len) ((start) + (len) > (1<<24))
  21. #define ADDR_32BIT(addr) (addr >= (1<<24))
  22. static const char TAG[] = "chip_wb";
  23. /* Driver for Winbond flash chip */
  24. static esp_err_t spi_flash_command_winbond_program_4B(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  25. static esp_err_t spi_flash_command_winbond_erase_sector_4B(esp_flash_t *chip, uint32_t start_address);
  26. static esp_err_t spi_flash_command_erase_block_4B(esp_flash_t *chip, uint32_t start_address);
  27. esp_err_t spi_flash_chip_winbond_probe(esp_flash_t *chip, uint32_t flash_id)
  28. {
  29. /* Check manufacturer and product IDs match our desired masks */
  30. const uint8_t MFG_ID = 0xEF;
  31. if (flash_id >> 16 != MFG_ID) {
  32. return ESP_ERR_NOT_FOUND;
  33. }
  34. return ESP_OK;
  35. }
  36. esp_err_t spi_flash_chip_winbond_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  37. {
  38. esp_err_t err = ESP_OK;
  39. const uint32_t page_size = chip->chip_drv->page_size;
  40. uint32_t align_address;
  41. uint8_t temp_buffer[64]; //spiflash hal max length of read no longer than 64byte
  42. uint32_t config_io_flags = 0;
  43. // Configure the host, and return
  44. if (REGION_32BIT(address, length)) {
  45. config_io_flags |= SPI_FLASH_CONFIG_IO_MODE_32B_ADDR;
  46. }
  47. err = chip->chip_drv->config_host_io_mode(chip, config_io_flags);
  48. if (err == ESP_ERR_NOT_SUPPORTED) {
  49. ESP_LOGE(TAG, "configure host io mode failed - unsupported");
  50. return err;
  51. }
  52. while (err == ESP_OK && length > 0) {
  53. memset(temp_buffer, 0xFF, sizeof(temp_buffer));
  54. uint32_t read_len = chip->host->driver->read_data_slicer(chip->host, address, length, &align_address, page_size);
  55. uint32_t left_off = address - align_address;
  56. uint32_t data_len = MIN(align_address + read_len, address + length) - address;
  57. err = chip->host->driver->read(chip->host, temp_buffer, align_address, read_len);
  58. memcpy(buffer, temp_buffer + left_off, data_len);
  59. address += data_len;
  60. buffer = (void *)((intptr_t)buffer + data_len);
  61. length = length - data_len;
  62. }
  63. return err;
  64. }
  65. esp_err_t spi_flash_chip_winbond_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  66. {
  67. esp_err_t err;
  68. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  69. if (err == ESP_OK) {
  70. // Perform the actual Page Program command
  71. err = spi_flash_command_winbond_program_4B(chip, buffer, address, length);
  72. if (err != ESP_OK) {
  73. return err;
  74. }
  75. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
  76. }
  77. return err;
  78. }
  79. esp_err_t spi_flash_chip_winbond_erase_sector(esp_flash_t *chip, uint32_t start_address)
  80. {
  81. esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
  82. if (err == ESP_OK) {
  83. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  84. }
  85. if (err == ESP_OK) {
  86. err = spi_flash_command_winbond_erase_sector_4B(chip, start_address);
  87. if (err != ESP_OK) {
  88. return err;
  89. }
  90. //to save time, flush cache here
  91. if (chip->host->driver->flush_cache) {
  92. err = chip->host->driver->flush_cache(chip->host, start_address, chip->chip_drv->sector_size);
  93. if (err != ESP_OK) {
  94. return err;
  95. }
  96. }
  97. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->sector_erase_timeout);
  98. }
  99. return err;
  100. }
  101. esp_err_t spi_flash_chip_winbond_erase_block(esp_flash_t *chip, uint32_t start_address)
  102. {
  103. esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
  104. if (err == ESP_OK) {
  105. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  106. }
  107. if (err == ESP_OK) {
  108. err = spi_flash_command_erase_block_4B(chip, start_address);
  109. if (err != ESP_OK) {
  110. return err;
  111. }
  112. //to save time, flush cache here
  113. if (chip->host->driver->flush_cache) {
  114. err = chip->host->driver->flush_cache(chip->host, start_address, chip->chip_drv->block_erase_size);
  115. if (err != ESP_OK) {
  116. return err;
  117. }
  118. }
  119. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->block_erase_timeout);
  120. }
  121. return err;
  122. }
  123. spi_flash_caps_t spi_flash_chip_winbond_get_caps(esp_flash_t *chip)
  124. {
  125. spi_flash_caps_t caps_flags = 0;
  126. // 32M-bits address support
  127. if ((chip->chip_id & 0xFF) >= 0x19) {
  128. caps_flags |= SPI_FLASH_CHIP_CAP_32MB_SUPPORT;
  129. }
  130. // flash-suspend is not supported
  131. // flash read unique id.
  132. caps_flags |= SPI_FLASH_CHIP_CAP_UNIQUE_ID;
  133. return caps_flags;
  134. }
  135. static const char chip_name[] = "winbond";
  136. // The issi chip can use the functions for generic chips except from set read mode and probe,
  137. // So we only replace these two functions.
  138. const spi_flash_chip_t esp_flash_chip_winbond = {
  139. .name = chip_name,
  140. .timeout = &spi_flash_chip_generic_timeout,
  141. .probe = spi_flash_chip_winbond_probe,
  142. .reset = spi_flash_chip_generic_reset,
  143. .detect_size = spi_flash_chip_generic_detect_size,
  144. .erase_chip = spi_flash_chip_generic_erase_chip,
  145. .erase_sector = spi_flash_chip_winbond_erase_sector,
  146. .erase_block = spi_flash_chip_winbond_erase_block,
  147. .sector_size = 4 * 1024,
  148. .block_erase_size = 64 * 1024,
  149. .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
  150. .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
  151. .num_protectable_regions = 0,
  152. .protectable_regions = NULL,
  153. .get_protected_regions = NULL,
  154. .set_protected_regions = NULL,
  155. .read = spi_flash_chip_winbond_read,
  156. .write = spi_flash_chip_generic_write,
  157. .program_page = spi_flash_chip_winbond_page_program,
  158. .page_size = 256,
  159. .write_encrypted = spi_flash_chip_generic_write_encrypted,
  160. .wait_idle = spi_flash_chip_generic_wait_idle,
  161. .set_io_mode = spi_flash_chip_generic_set_io_mode,
  162. .get_io_mode = spi_flash_chip_generic_get_io_mode,
  163. .read_reg = spi_flash_chip_generic_read_reg,
  164. .yield = spi_flash_chip_generic_yield,
  165. .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
  166. .read_unique_id = spi_flash_chip_generic_read_unique_id,
  167. .get_chip_caps = spi_flash_chip_winbond_get_caps,
  168. .config_host_io_mode = spi_flash_chip_generic_config_host_io_mode,
  169. };
  170. static esp_err_t spi_flash_command_winbond_program_4B(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  171. {
  172. bool addr_4b = ADDR_32BIT(address);
  173. spi_flash_trans_t t = {
  174. .command = (addr_4b? CMD_PROGRAM_PAGE_4B: CMD_PROGRAM_PAGE),
  175. .address_bitlen = (addr_4b? 32: 24),
  176. .address = address,
  177. .mosi_len = length,
  178. .mosi_data = buffer,
  179. };
  180. return chip->host->driver->common_command(chip->host, &t);
  181. }
  182. esp_err_t spi_flash_command_winbond_erase_sector_4B(esp_flash_t *chip, uint32_t start_address)
  183. {
  184. bool addr_4b = ADDR_32BIT(start_address);
  185. spi_flash_trans_t t = {
  186. .command = (addr_4b? CMD_SECTOR_ERASE_4B: CMD_SECTOR_ERASE),
  187. .address_bitlen = (addr_4b? 32: 24),
  188. .address = start_address,
  189. };
  190. return chip->host->driver->common_command(chip->host, &t);
  191. }
  192. esp_err_t spi_flash_command_erase_block_4B(esp_flash_t *chip, uint32_t start_address)
  193. {
  194. bool addr_4b = ADDR_32BIT(start_address);
  195. spi_flash_trans_t t = {
  196. .command = (addr_4b? CMD_LARGE_BLOCK_ERASE_4B: CMD_LARGE_BLOCK_ERASE),
  197. .address_bitlen = (addr_4b? 32: 24),
  198. .address = start_address,
  199. };
  200. return chip->host->driver->common_command(chip->host, &t);
  201. }