sf_internal.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SPI flash internal definitions
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef _SF_INTERNAL_H_
  10. #define _SF_INTERNAL_H_
  11. #include <linux/types.h>
  12. #include <linux/compiler.h>
  13. /* Dual SPI flash memories - see SPI_COMM_DUAL_... */
  14. enum spi_dual_flash {
  15. SF_SINGLE_FLASH = 0,
  16. SF_DUAL_STACKED_FLASH = BIT(0),
  17. SF_DUAL_PARALLEL_FLASH = BIT(1),
  18. };
  19. enum spi_nor_option_flags {
  20. SNOR_F_SST_WR = BIT(0),
  21. SNOR_F_USE_FSR = BIT(1),
  22. };
  23. #define SPI_FLASH_3B_ADDR_LEN 3
  24. #define SPI_FLASH_CMD_LEN (1 + SPI_FLASH_3B_ADDR_LEN)
  25. #define SPI_FLASH_16MB_BOUN 0x1000000
  26. /* CFI Manufacture ID's */
  27. #define SPI_FLASH_CFI_MFR_SPANSION 0x01
  28. #define SPI_FLASH_CFI_MFR_STMICRO 0x20
  29. #define SPI_FLASH_CFI_MFR_MACRONIX 0xc2
  30. #define SPI_FLASH_CFI_MFR_SST 0xbf
  31. #define SPI_FLASH_CFI_MFR_WINBOND 0xef
  32. #define SPI_FLASH_CFI_MFR_ATMEL 0x1f
  33. /* Erase commands */
  34. #define CMD_ERASE_4K 0x20
  35. #define CMD_ERASE_CHIP 0xc7
  36. #define CMD_ERASE_64K 0xd8
  37. /* Write commands */
  38. #define CMD_WRITE_STATUS 0x01
  39. #define CMD_PAGE_PROGRAM 0x02
  40. #define CMD_WRITE_DISABLE 0x04
  41. #define CMD_WRITE_ENABLE 0x06
  42. #define CMD_QUAD_PAGE_PROGRAM 0x32
  43. #define CMD_WRITE_EVCR 0x61
  44. /* Read commands */
  45. #define CMD_READ_ARRAY_SLOW 0x03
  46. #define CMD_READ_ARRAY_FAST 0x0b
  47. #define CMD_READ_DUAL_OUTPUT_FAST 0x3b
  48. #define CMD_READ_DUAL_IO_FAST 0xbb
  49. #define CMD_READ_QUAD_OUTPUT_FAST 0x6b
  50. #define CMD_READ_QUAD_IO_FAST 0xeb
  51. #define CMD_READ_ID 0x9f
  52. #define CMD_READ_STATUS 0x05
  53. #define CMD_READ_STATUS1 0x35
  54. #define CMD_READ_CONFIG 0x35
  55. #define CMD_FLAG_STATUS 0x70
  56. #define CMD_READ_EVCR 0x65
  57. /* Bank addr access commands */
  58. #ifdef CONFIG_SPI_FLASH_BAR
  59. # define CMD_BANKADDR_BRWR 0x17
  60. # define CMD_BANKADDR_BRRD 0x16
  61. # define CMD_EXTNADDR_WREAR 0xC5
  62. # define CMD_EXTNADDR_RDEAR 0xC8
  63. #endif
  64. /* Common status */
  65. #define STATUS_WIP BIT(0)
  66. #define STATUS_QEB_WINSPAN BIT(1)
  67. #define STATUS_QEB_MXIC BIT(6)
  68. #define STATUS_PEC BIT(7)
  69. #define STATUS_QEB_MICRON BIT(7)
  70. #define SR_BP0 BIT(2) /* Block protect 0 */
  71. #define SR_BP1 BIT(3) /* Block protect 1 */
  72. #define SR_BP2 BIT(4) /* Block protect 2 */
  73. /* Flash timeout values */
  74. #define SPI_FLASH_PROG_TIMEOUT (2 * CONFIG_SYS_HZ)
  75. #define SPI_FLASH_PAGE_ERASE_TIMEOUT (5 * CONFIG_SYS_HZ)
  76. #define SPI_FLASH_SECTOR_ERASE_TIMEOUT (10 * CONFIG_SYS_HZ)
  77. /* SST specific */
  78. #ifdef CONFIG_SPI_FLASH_SST
  79. # define CMD_SST_BP 0x02 /* Byte Program */
  80. # define CMD_SST_AAI_WP 0xAD /* Auto Address Incr Word Program */
  81. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  82. const void *buf);
  83. int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
  84. const void *buf);
  85. #endif
  86. #define JEDEC_MFR(info) ((info)->id[0])
  87. #define JEDEC_ID(info) (((info)->id[1]) << 8 | ((info)->id[2]))
  88. #define JEDEC_EXT(info) (((info)->id[3]) << 8 | ((info)->id[4]))
  89. #define SPI_FLASH_MAX_ID_LEN 6
  90. struct spi_flash_info {
  91. /* Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO]) */
  92. const char *name;
  93. /*
  94. * This array stores the ID bytes.
  95. * The first three bytes are the JEDIC ID.
  96. * JEDEC ID zero means "no ID" (mostly older chips).
  97. */
  98. u8 id[SPI_FLASH_MAX_ID_LEN];
  99. u8 id_len;
  100. /*
  101. * The size listed here is what works with SPINOR_OP_SE, which isn't
  102. * necessarily called a "sector" by the vendor.
  103. */
  104. u32 sector_size;
  105. u32 n_sectors;
  106. u16 page_size;
  107. u16 flags;
  108. #define SECT_4K BIT(0) /* CMD_ERASE_4K works uniformly */
  109. #define E_FSR BIT(1) /* use flag status register for */
  110. #define SST_WR BIT(2) /* use SST byte/word programming */
  111. #define WR_QPP BIT(3) /* use Quad Page Program */
  112. #define RD_QUAD BIT(4) /* use Quad Read */
  113. #define RD_DUAL BIT(5) /* use Dual Read */
  114. #define RD_QUADIO BIT(6) /* use Quad IO Read */
  115. #define RD_DUALIO BIT(7) /* use Dual IO Read */
  116. #define RD_FULL (RD_QUAD | RD_DUAL | RD_QUADIO | RD_DUALIO)
  117. };
  118. extern const struct spi_flash_info spi_flash_ids[];
  119. /* Send a single-byte command to the device and read the response */
  120. int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len);
  121. /*
  122. * Send a multi-byte command to the device and read the response. Used
  123. * for flash array reads, etc.
  124. */
  125. int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
  126. size_t cmd_len, void *data, size_t data_len);
  127. /*
  128. * Send a multi-byte command to the device followed by (optional)
  129. * data. Used for programming the flash array, etc.
  130. */
  131. int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
  132. const void *data, size_t data_len);
  133. /* Flash erase(sectors) operation, support all possible erase commands */
  134. int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len);
  135. /* Lock stmicro spi flash region */
  136. int stm_lock(struct spi_flash *flash, u32 ofs, size_t len);
  137. /* Unlock stmicro spi flash region */
  138. int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len);
  139. /* Check if a stmicro spi flash region is completely locked */
  140. int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len);
  141. /* Enable writing on the SPI flash */
  142. static inline int spi_flash_cmd_write_enable(struct spi_flash *flash)
  143. {
  144. return spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, NULL, 0);
  145. }
  146. /* Disable writing on the SPI flash */
  147. static inline int spi_flash_cmd_write_disable(struct spi_flash *flash)
  148. {
  149. return spi_flash_cmd(flash->spi, CMD_WRITE_DISABLE, NULL, 0);
  150. }
  151. /*
  152. * Used for spi_flash write operation
  153. * - SPI claim
  154. * - spi_flash_cmd_write_enable
  155. * - spi_flash_cmd_write
  156. * - spi_flash_wait_till_ready
  157. * - SPI release
  158. */
  159. int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
  160. size_t cmd_len, const void *buf, size_t buf_len);
  161. /*
  162. * Flash write operation, support all possible write commands.
  163. * Write the requested data out breaking it up into multiple write
  164. * commands as needed per the write size.
  165. */
  166. int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
  167. size_t len, const void *buf);
  168. /*
  169. * Same as spi_flash_cmd_read() except it also claims/releases the SPI
  170. * bus. Used as common part of the ->read() operation.
  171. */
  172. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  173. size_t cmd_len, void *data, size_t data_len);
  174. /* Flash read operation, support all possible read commands */
  175. int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
  176. size_t len, void *data);
  177. #ifdef CONFIG_SPI_FLASH_MTD
  178. int spi_flash_mtd_register(struct spi_flash *flash);
  179. void spi_flash_mtd_unregister(void);
  180. #endif
  181. /**
  182. * spi_flash_scan - scan the SPI FLASH
  183. * @flash: the spi flash structure
  184. *
  185. * The drivers can use this fuction to scan the SPI FLASH.
  186. * In the scanning, it will try to get all the necessary information to
  187. * fill the spi_flash{}.
  188. *
  189. * Return: 0 for success, others for failure.
  190. */
  191. int spi_flash_scan(struct spi_flash *flash);
  192. #endif /* _SF_INTERNAL_H_ */