spi_flash.h 6.8 KB

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