spi_flash.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common SPI flash Interface
  4. *
  5. * Copyright (C) 2008 Atmel Corporation
  6. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  7. */
  8. #ifndef _SPI_FLASH_H_
  9. #define _SPI_FLASH_H_
  10. #include <dm.h> /* Because we dereference struct udevice here */
  11. #include <linux/types.h>
  12. #ifndef CONFIG_SF_DEFAULT_SPEED
  13. # define CONFIG_SF_DEFAULT_SPEED 1000000
  14. #endif
  15. #ifndef CONFIG_SF_DEFAULT_MODE
  16. # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
  17. #endif
  18. #ifndef CONFIG_SF_DEFAULT_CS
  19. # define CONFIG_SF_DEFAULT_CS 0
  20. #endif
  21. #ifndef CONFIG_SF_DEFAULT_BUS
  22. # define CONFIG_SF_DEFAULT_BUS 0
  23. #endif
  24. struct spi_slave;
  25. /**
  26. * struct spi_flash - SPI flash structure
  27. *
  28. * @spi: SPI slave
  29. * @dev: SPI flash device
  30. * @name: Name of SPI flash
  31. * @dual_flash: Indicates dual flash memories - dual stacked, parallel
  32. * @shift: Flash shift useful in dual parallel
  33. * @flags: Indication of spi flash flags
  34. * @size: Total flash size
  35. * @page_size: Write (page) size
  36. * @sector_size: Sector size
  37. * @erase_size: Erase size
  38. * @bank_read_cmd: Bank read cmd
  39. * @bank_write_cmd: Bank write cmd
  40. * @bank_curr: Current flash bank
  41. * @erase_cmd: Erase cmd 4K, 32K, 64K
  42. * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
  43. * @write_cmd: Write cmd - page and quad program.
  44. * @dummy_byte: Dummy cycles for read operation.
  45. * @memory_map: Address of read-only SPI flash access
  46. * @flash_lock: lock a region of the SPI Flash
  47. * @flash_unlock: unlock a region of the SPI Flash
  48. * @flash_is_locked: check if a region of the SPI Flash is completely locked
  49. * @read: Flash read ops: Read len bytes at offset into buf
  50. * Supported cmds: Fast Array Read
  51. * @write: Flash write ops: Write len bytes from buf into offset
  52. * Supported cmds: Page Program
  53. * @erase: Flash erase ops: Erase len bytes from offset
  54. * Supported cmds: Sector erase 4K, 32K, 64K
  55. * return 0 - Success, 1 - Failure
  56. */
  57. struct spi_flash {
  58. struct spi_slave *spi;
  59. #ifdef CONFIG_DM_SPI_FLASH
  60. struct udevice *dev;
  61. #endif
  62. const char *name;
  63. u8 dual_flash;
  64. u8 shift;
  65. u16 flags;
  66. u32 size;
  67. u32 page_size;
  68. u32 sector_size;
  69. u32 erase_size;
  70. #ifdef CONFIG_SPI_FLASH_BAR
  71. u8 bank_read_cmd;
  72. u8 bank_write_cmd;
  73. u8 bank_curr;
  74. #endif
  75. u8 erase_cmd;
  76. u8 read_cmd;
  77. u8 write_cmd;
  78. u8 dummy_byte;
  79. void *memory_map;
  80. int (*flash_lock)(struct spi_flash *flash, u32 ofs, size_t len);
  81. int (*flash_unlock)(struct spi_flash *flash, u32 ofs, size_t len);
  82. int (*flash_is_locked)(struct spi_flash *flash, u32 ofs, size_t len);
  83. #ifndef CONFIG_DM_SPI_FLASH
  84. /*
  85. * These are not strictly needed for driver model, but keep them here
  86. * while the transition is in progress.
  87. *
  88. * Normally each driver would provide its own operations, but for
  89. * SPI flash most chips use the same algorithms. One approach is
  90. * to create a 'common' SPI flash device which knows how to talk
  91. * to most devices, and then allow other drivers to be used instead
  92. * if required, perhaps with a way of scanning through the list to
  93. * find the driver that matches the device.
  94. */
  95. int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
  96. int (*write)(struct spi_flash *flash, u32 offset, size_t len,
  97. const void *buf);
  98. int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
  99. #endif
  100. };
  101. struct dm_spi_flash_ops {
  102. int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
  103. int (*write)(struct udevice *dev, u32 offset, size_t len,
  104. const void *buf);
  105. int (*erase)(struct udevice *dev, u32 offset, size_t len);
  106. };
  107. /* Access the serial operations for a device */
  108. #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
  109. #ifdef CONFIG_DM_SPI_FLASH
  110. /**
  111. * spi_flash_read_dm() - Read data from SPI flash
  112. *
  113. * @dev: SPI flash device
  114. * @offset: Offset into device in bytes to read from
  115. * @len: Number of bytes to read
  116. * @buf: Buffer to put the data that is read
  117. * @return 0 if OK, -ve on error
  118. */
  119. int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
  120. /**
  121. * spi_flash_write_dm() - Write data to SPI flash
  122. *
  123. * @dev: SPI flash device
  124. * @offset: Offset into device in bytes to write to
  125. * @len: Number of bytes to write
  126. * @buf: Buffer containing bytes to write
  127. * @return 0 if OK, -ve on error
  128. */
  129. int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
  130. const void *buf);
  131. /**
  132. * spi_flash_erase_dm() - Erase blocks of the SPI flash
  133. *
  134. * Note that @len must be a muiltiple of the flash sector size.
  135. *
  136. * @dev: SPI flash device
  137. * @offset: Offset into device in bytes to start erasing
  138. * @len: Number of bytes to erase
  139. * @return 0 if OK, -ve on error
  140. */
  141. int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
  142. int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  143. unsigned int max_hz, unsigned int spi_mode,
  144. struct udevice **devp);
  145. /* Compatibility function - this is the old U-Boot API */
  146. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  147. unsigned int max_hz, unsigned int spi_mode);
  148. /* Compatibility function - this is the old U-Boot API */
  149. void spi_flash_free(struct spi_flash *flash);
  150. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  151. size_t len, void *buf)
  152. {
  153. return spi_flash_read_dm(flash->dev, offset, len, buf);
  154. }
  155. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  156. size_t len, const void *buf)
  157. {
  158. return spi_flash_write_dm(flash->dev, offset, len, buf);
  159. }
  160. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  161. size_t len)
  162. {
  163. return spi_flash_erase_dm(flash->dev, offset, len);
  164. }
  165. struct sandbox_state;
  166. int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
  167. struct udevice *bus, ofnode node, const char *spec);
  168. void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
  169. #else
  170. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  171. unsigned int max_hz, unsigned int spi_mode);
  172. void spi_flash_free(struct spi_flash *flash);
  173. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  174. size_t len, void *buf)
  175. {
  176. return flash->read(flash, offset, len, buf);
  177. }
  178. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  179. size_t len, const void *buf)
  180. {
  181. return flash->write(flash, offset, len, buf);
  182. }
  183. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  184. size_t len)
  185. {
  186. return flash->erase(flash, offset, len);
  187. }
  188. #endif
  189. static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
  190. bool prot)
  191. {
  192. if (!flash->flash_lock || !flash->flash_unlock)
  193. return -EOPNOTSUPP;
  194. if (prot)
  195. return flash->flash_lock(flash, ofs, len);
  196. else
  197. return flash->flash_unlock(flash, ofs, len);
  198. }
  199. #endif /* _SPI_FLASH_H_ */