spi_flash.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * @erase_cmd: Erase cmd 4K, 32K, 64K
  48. * @read_cmd: Read cmd - Array Fast, Extn read and quad read.
  49. * @write_cmd: Write cmd - page and quad program.
  50. * @dummy_byte: Dummy cycles for read operation.
  51. * @memory_map: Address of read-only SPI flash access
  52. * @read: Flash read ops: Read len bytes at offset into buf
  53. * Supported cmds: Fast Array Read
  54. * @write: Flash write ops: Write len bytes from buf into offset
  55. * Supported cmds: Page Program
  56. * @erase: Flash erase ops: Erase len bytes from offset
  57. * Supported cmds: Sector erase 4K, 32K, 64K
  58. * return 0 - Success, 1 - Failure
  59. */
  60. struct spi_flash {
  61. struct spi_slave *spi;
  62. #ifdef CONFIG_DM_SPI_FLASH
  63. struct udevice *dev;
  64. #endif
  65. const char *name;
  66. u8 dual_flash;
  67. u8 shift;
  68. u16 flags;
  69. u32 size;
  70. u32 page_size;
  71. u32 sector_size;
  72. u32 erase_size;
  73. #ifdef CONFIG_SPI_FLASH_BAR
  74. u8 bank_read_cmd;
  75. u8 bank_write_cmd;
  76. u8 bank_curr;
  77. #endif
  78. u8 erase_cmd;
  79. u8 read_cmd;
  80. u8 write_cmd;
  81. u8 dummy_byte;
  82. void *memory_map;
  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. int spi_flash_remove(struct udevice *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. void spi_boot(void) __noreturn;
  202. void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
  203. #endif /* _SPI_FLASH_H_ */