spi_flash_chip_gd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <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_chip_gd.h"
  20. #include "spi_flash_defs.h"
  21. #define ADDR_32BIT(addr) (addr >= (1<<24))
  22. #define REGION_32BIT(start, len) ((start) + (len) > (1<<24))
  23. extern esp_err_t spi_flash_chip_winbond_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  24. extern esp_err_t spi_flash_chip_winbond_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  25. extern esp_err_t spi_flash_chip_winbond_erase_sector(esp_flash_t *chip, uint32_t start_address);
  26. extern esp_err_t spi_flash_chip_winbond_erase_block(esp_flash_t *chip, uint32_t start_address);
  27. #define spi_flash_chip_gd_read spi_flash_chip_winbond_read
  28. #define spi_flash_chip_gd_page_program spi_flash_chip_winbond_page_program
  29. #define spi_flash_chip_gd_erase_sector spi_flash_chip_winbond_erase_sector
  30. #define spi_flash_chip_gd_erase_block spi_flash_chip_winbond_erase_block
  31. spi_flash_caps_t spi_flash_chip_gd_get_caps(esp_flash_t *chip)
  32. {
  33. spi_flash_caps_t caps_flags = 0;
  34. // 32M-bits address support
  35. if ((chip->chip_id & 0xFF) >= 0x19) {
  36. caps_flags |= SPI_FLASH_CHIP_CAP_32MB_SUPPORT;
  37. }
  38. // flash-suspend is not supported
  39. // flash read unique id.
  40. caps_flags |= SPI_FLASH_CHIP_CAP_UNIQUE_ID;
  41. return caps_flags;
  42. }
  43. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  44. #define FLASH_ID_MASK 0xFF00
  45. #define FLASH_SIZE_MASK 0xFF
  46. #define GD25Q_PRODUCT_ID 0x4000
  47. #define GD25LQ_PRODUCT_ID 0x6000
  48. #define WRSR_16B_REQUIRED(chip_id) (((chip_id) & FLASH_ID_MASK) == GD25LQ_PRODUCT_ID || \
  49. ((chip_id) & FLASH_SIZE_MASK) <= 0x15)
  50. /* Driver for GD flash chip */
  51. esp_err_t spi_flash_chip_gd_probe(esp_flash_t *chip, uint32_t flash_id)
  52. {
  53. /* Check manufacturer and product IDs match our desired masks */
  54. const uint8_t MFG_ID = 0xC8;
  55. if (flash_id >> 16 != MFG_ID) {
  56. return ESP_ERR_NOT_FOUND;
  57. }
  58. uint32_t product_id = flash_id & FLASH_ID_MASK;
  59. if (product_id != GD25Q_PRODUCT_ID && product_id != GD25LQ_PRODUCT_ID) {
  60. return ESP_ERR_NOT_FOUND;
  61. }
  62. return ESP_OK;
  63. }
  64. esp_err_t spi_flash_chip_gd_set_io_mode(esp_flash_t *chip)
  65. {
  66. if (WRSR_16B_REQUIRED(chip->chip_id)) {
  67. const uint32_t qe = 1<<9;
  68. return spi_flash_common_set_io_mode(chip,
  69. spi_flash_common_write_status_16b_wrsr,
  70. spi_flash_common_read_status_16b_rdsr_rdsr2,
  71. qe);
  72. } else {
  73. const uint32_t qe = 1<<1;
  74. return spi_flash_common_set_io_mode(chip,
  75. spi_flash_common_write_status_8b_wrsr2,
  76. spi_flash_common_read_status_8b_rdsr2,
  77. qe);
  78. }
  79. }
  80. esp_err_t spi_flash_chip_gd_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode)
  81. {
  82. /* GD uses bit 1 of SR2 as Quad Enable */
  83. const uint8_t BIT_QE = 1 << 1;
  84. uint32_t sr;
  85. esp_err_t ret = spi_flash_common_read_status_8b_rdsr2(chip, &sr);
  86. if (ret == ESP_OK) {
  87. *out_io_mode = ((sr & BIT_QE)? SPI_FLASH_QOUT: 0);
  88. }
  89. return ret;
  90. }
  91. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  92. static const char chip_name[] = "gd";
  93. // The issi chip can use the functions for generic chips except from set read mode and probe,
  94. // So we only replace these two functions.
  95. const spi_flash_chip_t esp_flash_chip_gd = {
  96. .name = chip_name,
  97. .timeout = &spi_flash_chip_generic_timeout,
  98. .probe = spi_flash_chip_gd_probe,
  99. .reset = spi_flash_chip_generic_reset,
  100. .detect_size = spi_flash_chip_generic_detect_size,
  101. .erase_chip = spi_flash_chip_generic_erase_chip,
  102. .erase_sector = spi_flash_chip_gd_erase_sector,
  103. .erase_block = spi_flash_chip_gd_erase_block,
  104. .sector_size = 4 * 1024,
  105. .block_erase_size = 64 * 1024,
  106. .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
  107. .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
  108. .num_protectable_regions = 0,
  109. .protectable_regions = NULL,
  110. .get_protected_regions = NULL,
  111. .set_protected_regions = NULL,
  112. .read = spi_flash_chip_gd_read,
  113. .write = spi_flash_chip_generic_write,
  114. .program_page = spi_flash_chip_gd_page_program,
  115. .page_size = 256,
  116. .write_encrypted = spi_flash_chip_generic_write_encrypted,
  117. .wait_idle = spi_flash_chip_generic_wait_idle,
  118. .set_io_mode = spi_flash_chip_gd_set_io_mode,
  119. .get_io_mode = spi_flash_chip_gd_get_io_mode,
  120. .read_reg = spi_flash_chip_generic_read_reg,
  121. .yield = spi_flash_chip_generic_yield,
  122. .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
  123. .read_unique_id = spi_flash_chip_generic_read_unique_id,
  124. .get_chip_caps = spi_flash_chip_gd_get_caps,
  125. .config_host_io_mode = spi_flash_chip_generic_config_host_io_mode,
  126. };