sf.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2019-2020, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef SF_H
  7. #define SF_H
  8. #include <stdint.h>
  9. #include <stddef.h>
  10. #ifdef SPI_DEBUG
  11. #define SPI_DEBUG(fmt, ...) INFO(fmt, ##__VA_ARGS__)
  12. #else
  13. #define SPI_DEBUG(fmt, ...)
  14. #endif
  15. #define SPI_FLASH_MAX_ID_LEN 6
  16. #define CMD_WRSR 0x01 /* Write status register */
  17. #define CMD_PAGE_PROGRAM 0x02
  18. #define CMD_READ_NORMAL 0x03
  19. #define CMD_RDSR 0x05
  20. #define CMD_WRITE_ENABLE 0x06
  21. #define CMD_RDFSR 0x70
  22. #define CMD_READ_ID 0x9f
  23. #define CMD_ERASE_4K 0x20
  24. #define CMD_ERASE_64K 0xd8
  25. #define ERASE_SIZE_64K (64 * 1024)
  26. /* Common status */
  27. #define STATUS_WIP BIT(0)
  28. struct spi_flash {
  29. struct spi_slave *spi;
  30. uint32_t size;
  31. uint32_t page_size;
  32. uint32_t sector_size;
  33. uint32_t erase_size;
  34. uint8_t erase_cmd;
  35. uint8_t read_cmd;
  36. uint8_t write_cmd;
  37. uint8_t flags;
  38. };
  39. struct spi_flash_info {
  40. const char *name;
  41. /*
  42. * This array stores the ID bytes.
  43. * The first three bytes are the JEDIC ID.
  44. * JEDEC ID zero means "no ID" (mostly older chips).
  45. */
  46. uint8_t id[SPI_FLASH_MAX_ID_LEN];
  47. uint8_t id_len;
  48. uint32_t sector_size;
  49. uint32_t n_sectors;
  50. uint16_t page_size;
  51. uint8_t flags;
  52. };
  53. /* Enum list - Full read commands */
  54. enum spi_read_cmds {
  55. ARRAY_SLOW = BIT(0),
  56. ARRAY_FAST = BIT(1),
  57. DUAL_OUTPUT_FAST = BIT(2),
  58. DUAL_IO_FAST = BIT(3),
  59. QUAD_OUTPUT_FAST = BIT(4),
  60. QUAD_IO_FAST = BIT(5),
  61. };
  62. /* sf param flags */
  63. enum spi_param_flag {
  64. SECT_4K = BIT(0),
  65. SECT_32K = BIT(1),
  66. E_FSR = BIT(2),
  67. SST_BP = BIT(3),
  68. SST_WP = BIT(4),
  69. WR_QPP = BIT(5),
  70. };
  71. int spi_flash_cmd_read(const uint8_t *cmd, size_t cmd_len,
  72. void *data, size_t data_len);
  73. int spi_flash_cmd(uint8_t cmd, void *response, size_t len);
  74. int spi_flash_cmd_write(const uint8_t *cmd, size_t cmd_len,
  75. const void *data, size_t data_len);
  76. #endif