spi_flash_chip_gd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "spi_flash_chip_generic.h"
  16. #include "spi_flash_defs.h"
  17. #define FLASH_ID_MASK 0xFF00
  18. #define FLASH_SIZE_MASK 0xFF
  19. #define GD25Q_PRODUCT_ID 0x4000
  20. #define GD25LQ_PRODUCT_ID 0x6000
  21. #define WRSR_16B_REQUIRED(chip_id) (((chip_id) & FLASH_ID_MASK) == GD25LQ_PRODUCT_ID || \
  22. ((chip_id) & FLASH_SIZE_MASK) <= 0x15)
  23. /* Driver for GD flash chip */
  24. esp_err_t spi_flash_chip_gd_probe(esp_flash_t *chip, uint32_t flash_id)
  25. {
  26. /* Check manufacturer and product IDs match our desired masks */
  27. const uint8_t MFG_ID = 0xC8;
  28. if (flash_id >> 16 != MFG_ID) {
  29. return ESP_ERR_NOT_FOUND;
  30. }
  31. uint32_t product_id = flash_id & FLASH_ID_MASK;
  32. if (product_id != GD25Q_PRODUCT_ID && product_id != GD25LQ_PRODUCT_ID) {
  33. return ESP_ERR_NOT_FOUND;
  34. }
  35. return ESP_OK;
  36. }
  37. esp_err_t spi_flash_chip_gd_set_io_mode(esp_flash_t *chip)
  38. {
  39. if (WRSR_16B_REQUIRED(chip->chip_id)) {
  40. const uint32_t qe = 1<<9;
  41. return spi_flash_common_set_io_mode(chip,
  42. spi_flash_common_write_status_16b_wrsr,
  43. spi_flash_common_read_status_16b_rdsr_rdsr2,
  44. qe);
  45. } else {
  46. const uint32_t qe = 1<<1;
  47. return spi_flash_common_set_io_mode(chip,
  48. spi_flash_common_write_status_8b_wrsr2,
  49. spi_flash_common_read_status_8b_rdsr2,
  50. qe);
  51. }
  52. }
  53. esp_err_t spi_flash_chip_gd_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode)
  54. {
  55. /* GD uses bit 1 of SR2 as Quad Enable */
  56. const uint8_t BIT_QE = 1 << 1;
  57. uint32_t sr;
  58. esp_err_t ret = spi_flash_common_read_status_8b_rdsr2(chip, &sr);
  59. if (ret == ESP_OK) {
  60. *out_io_mode = ((sr & BIT_QE)? SPI_FLASH_QOUT: 0);
  61. }
  62. return ret;
  63. }
  64. static const char chip_name[] = "gd";
  65. // The issi chip can use the functions for generic chips except from set read mode and probe,
  66. // So we only replace these two functions.
  67. const spi_flash_chip_t esp_flash_chip_gd = {
  68. .name = chip_name,
  69. .probe = spi_flash_chip_gd_probe,
  70. .reset = spi_flash_chip_generic_reset,
  71. .detect_size = spi_flash_chip_generic_detect_size,
  72. .erase_chip = spi_flash_chip_generic_erase_chip,
  73. .erase_sector = spi_flash_chip_generic_erase_sector,
  74. .erase_block = spi_flash_chip_generic_erase_block,
  75. .sector_size = 4 * 1024,
  76. .block_erase_size = 64 * 1024,
  77. .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
  78. .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
  79. // TODO support protected regions on ISSI flash
  80. .num_protectable_regions = 0,
  81. .protectable_regions = NULL,
  82. .get_protected_regions = NULL,
  83. .set_protected_regions = NULL,
  84. .read = spi_flash_chip_generic_read,
  85. .write = spi_flash_chip_generic_write,
  86. .program_page = spi_flash_chip_generic_page_program,
  87. .page_size = 256,
  88. .write_encrypted = spi_flash_chip_generic_write_encrypted,
  89. .wait_idle = spi_flash_chip_generic_wait_idle,
  90. .set_io_mode = spi_flash_chip_gd_set_io_mode,
  91. .get_io_mode = spi_flash_chip_gd_get_io_mode,
  92. };