spi_flash.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * @name: Name of SPI flash
  36. * @dual_flash: Indicates dual flash memories - dual stacked, parallel
  37. * @shift: Flash shift useful in dual parallel
  38. * @size: Total flash size
  39. * @page_size: Write (page) size
  40. * @sector_size: Sector size
  41. * @erase_size: Erase size
  42. * @bank_read_cmd: Bank read cmd
  43. * @bank_write_cmd: Bank write cmd
  44. * @bank_curr: Current flash bank
  45. * @poll_cmd: Poll cmd - for flash erase/program
  46. * @erase_cmd: Erase cmd 4K, 32K, 64K
  47. * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
  48. * @write_cmd: Write cmd - page and quad program.
  49. * @dummy_byte: Dummy cycles for read operation.
  50. * @memory_map: Address of read-only SPI flash access
  51. * @read: Flash read ops: Read len bytes at offset into buf
  52. * Supported cmds: Fast Array Read
  53. * @write: Flash write ops: Write len bytes from buf into offset
  54. * Supported cmds: Page Program
  55. * @erase: Flash erase ops: Erase len bytes from offset
  56. * Supported cmds: Sector erase 4K, 32K, 64K
  57. * return 0 - Success, 1 - Failure
  58. */
  59. struct spi_flash {
  60. struct spi_slave *spi;
  61. #ifdef CONFIG_DM_SPI_FLASH
  62. struct udevice *dev;
  63. #endif
  64. const char *name;
  65. u8 dual_flash;
  66. u8 shift;
  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 poll_cmd;
  77. u8 erase_cmd;
  78. u8 read_cmd;
  79. u8 write_cmd;
  80. u8 dummy_byte;
  81. void *memory_map;
  82. #ifndef CONFIG_DM_SPI_FLASH
  83. /*
  84. * These are not strictly needed for driver model, but keep them here
  85. * while the transition is in progress.
  86. *
  87. * Normally each driver would provide its own operations, but for
  88. * SPI flash most chips use the same algorithms. One approach is
  89. * to create a 'common' SPI flash device which knows how to talk
  90. * to most devices, and then allow other drivers to be used instead
  91. * if required, perhaps with a way of scanning through the list to
  92. * find the driver that matches the device.
  93. */
  94. int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
  95. int (*write)(struct spi_flash *flash, u32 offset, size_t len,
  96. const void *buf);
  97. int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
  98. #endif
  99. };
  100. struct dm_spi_flash_ops {
  101. int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
  102. int (*write)(struct udevice *dev, u32 offset, size_t len,
  103. const void *buf);
  104. int (*erase)(struct udevice *dev, u32 offset, size_t len);
  105. };
  106. /* Access the serial operations for a device */
  107. #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
  108. #ifdef CONFIG_DM_SPI_FLASH
  109. /**
  110. * spi_flash_read_dm() - Read data from SPI flash
  111. *
  112. * @dev: SPI flash device
  113. * @offset: Offset into device in bytes to read from
  114. * @len: Number of bytes to read
  115. * @buf: Buffer to put the data that is read
  116. * @return 0 if OK, -ve on error
  117. */
  118. int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
  119. /**
  120. * spi_flash_write_dm() - Write data to SPI flash
  121. *
  122. * @dev: SPI flash device
  123. * @offset: Offset into device in bytes to write to
  124. * @len: Number of bytes to write
  125. * @buf: Buffer containing bytes to write
  126. * @return 0 if OK, -ve on error
  127. */
  128. int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
  129. const void *buf);
  130. /**
  131. * spi_flash_erase_dm() - Erase blocks of the SPI flash
  132. *
  133. * Note that @len must be a muiltiple of the flash sector size.
  134. *
  135. * @dev: SPI flash device
  136. * @offset: Offset into device in bytes to start erasing
  137. * @len: Number of bytes to erase
  138. * @return 0 if OK, -ve on error
  139. */
  140. int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
  141. int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  142. unsigned int max_hz, unsigned int spi_mode,
  143. struct udevice **devp);
  144. /* Compatibility function - this is the old U-Boot API */
  145. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  146. unsigned int max_hz, unsigned int spi_mode);
  147. /* Compatibility function - this is the old U-Boot API */
  148. void spi_flash_free(struct spi_flash *flash);
  149. int spi_flash_remove(struct udevice *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, int of_offset, 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. /**
  173. * Set up a new SPI flash from an fdt node
  174. *
  175. * @param blob Device tree blob
  176. * @param slave_node Pointer to this SPI slave node in the device tree
  177. * @param spi_node Cached pointer to the SPI interface this node belongs
  178. * to
  179. * @return 0 if ok, -1 on error
  180. */
  181. struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
  182. int spi_node);
  183. void spi_flash_free(struct spi_flash *flash);
  184. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  185. size_t len, void *buf)
  186. {
  187. return flash->read(flash, offset, len, buf);
  188. }
  189. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  190. size_t len, const void *buf)
  191. {
  192. return flash->write(flash, offset, len, buf);
  193. }
  194. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  195. size_t len)
  196. {
  197. return flash->erase(flash, offset, len);
  198. }
  199. #endif
  200. void spi_boot(void) __noreturn;
  201. void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
  202. #endif /* _SPI_FLASH_H_ */