spi_flash.h 7.0 KB

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