spi_flash.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include <linux/mtd/spi-nor.h>
  13. /* by default ENV use the same parameters than SF command */
  14. #ifndef CONFIG_ENV_SPI_BUS
  15. # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
  16. #endif
  17. #ifndef CONFIG_ENV_SPI_CS
  18. # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
  19. #endif
  20. #ifndef CONFIG_ENV_SPI_MAX_HZ
  21. # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
  22. #endif
  23. #ifndef CONFIG_ENV_SPI_MODE
  24. # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
  25. #endif
  26. struct spi_slave;
  27. struct dm_spi_flash_ops {
  28. int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
  29. int (*write)(struct udevice *dev, u32 offset, size_t len,
  30. const void *buf);
  31. int (*erase)(struct udevice *dev, u32 offset, size_t len);
  32. /**
  33. * get_sw_write_prot() - Check state of software write-protect feature
  34. *
  35. * SPI flash chips can lock a region of the flash defined by a
  36. * 'protected area'. This function checks if this protected area is
  37. * defined.
  38. *
  39. * @dev: SPI flash device
  40. * @return 0 if no region is write-protected, 1 if a region is
  41. * write-protected, -ENOSYS if the driver does not implement this,
  42. * other -ve value on error
  43. */
  44. int (*get_sw_write_prot)(struct udevice *dev);
  45. };
  46. /* Access the serial operations for a device */
  47. #define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
  48. #ifdef CONFIG_DM_SPI_FLASH
  49. /**
  50. * spi_flash_read_dm() - Read data from SPI flash
  51. *
  52. * @dev: SPI flash device
  53. * @offset: Offset into device in bytes to read from
  54. * @len: Number of bytes to read
  55. * @buf: Buffer to put the data that is read
  56. * @return 0 if OK, -ve on error
  57. */
  58. int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf);
  59. /**
  60. * spi_flash_write_dm() - Write data to SPI flash
  61. *
  62. * @dev: SPI flash device
  63. * @offset: Offset into device in bytes to write to
  64. * @len: Number of bytes to write
  65. * @buf: Buffer containing bytes to write
  66. * @return 0 if OK, -ve on error
  67. */
  68. int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len,
  69. const void *buf);
  70. /**
  71. * spi_flash_erase_dm() - Erase blocks of the SPI flash
  72. *
  73. * Note that @len must be a muiltiple of the flash sector size.
  74. *
  75. * @dev: SPI flash device
  76. * @offset: Offset into device in bytes to start erasing
  77. * @len: Number of bytes to erase
  78. * @return 0 if OK, -ve on error
  79. */
  80. int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len);
  81. /**
  82. * spl_flash_get_sw_write_prot() - Check state of software write-protect feature
  83. *
  84. * SPI flash chips can lock a region of the flash defined by a
  85. * 'protected area'. This function checks if this protected area is
  86. * defined.
  87. *
  88. * @dev: SPI flash device
  89. * @return 0 if no region is write-protected, 1 if a region is
  90. * write-protected, -ENOSYS if the driver does not implement this,
  91. * other -ve value on error
  92. */
  93. int spl_flash_get_sw_write_prot(struct udevice *dev);
  94. /**
  95. * spi_flash_std_probe() - Probe a SPI flash device
  96. *
  97. * This is the standard internal method for probing a SPI flash device to
  98. * determine its type. It can be used in chip-specific drivers which need to
  99. * do this, typically with of-platdata
  100. *
  101. * @dev: SPI-flash device to probe
  102. * @return 0 if OK, -ve on error
  103. */
  104. int spi_flash_std_probe(struct udevice *dev);
  105. int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  106. unsigned int max_hz, unsigned int spi_mode,
  107. struct udevice **devp);
  108. /* Compatibility function - this is the old U-Boot API */
  109. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  110. unsigned int max_hz, unsigned int spi_mode);
  111. /* Compatibility function - this is the old U-Boot API */
  112. void spi_flash_free(struct spi_flash *flash);
  113. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  114. size_t len, void *buf)
  115. {
  116. return spi_flash_read_dm(flash->dev, offset, len, buf);
  117. }
  118. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  119. size_t len, const void *buf)
  120. {
  121. return spi_flash_write_dm(flash->dev, offset, len, buf);
  122. }
  123. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  124. size_t len)
  125. {
  126. return spi_flash_erase_dm(flash->dev, offset, len);
  127. }
  128. struct sandbox_state;
  129. int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
  130. struct udevice *bus, ofnode node, const char *spec);
  131. void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
  132. #else
  133. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  134. unsigned int max_hz, unsigned int spi_mode);
  135. void spi_flash_free(struct spi_flash *flash);
  136. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  137. size_t len, void *buf)
  138. {
  139. struct mtd_info *mtd = &flash->mtd;
  140. size_t retlen;
  141. return mtd->_read(mtd, offset, len, &retlen, buf);
  142. }
  143. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  144. size_t len, const void *buf)
  145. {
  146. struct mtd_info *mtd = &flash->mtd;
  147. size_t retlen;
  148. return mtd->_write(mtd, offset, len, &retlen, buf);
  149. }
  150. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  151. size_t len)
  152. {
  153. struct mtd_info *mtd = &flash->mtd;
  154. struct erase_info instr;
  155. if (offset % mtd->erasesize || len % mtd->erasesize) {
  156. printf("SF: Erase offset/length not multiple of erase size\n");
  157. return -EINVAL;
  158. }
  159. memset(&instr, 0, sizeof(instr));
  160. instr.addr = offset;
  161. instr.len = len;
  162. return mtd->_erase(mtd, &instr);
  163. }
  164. #endif
  165. static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
  166. bool prot)
  167. {
  168. if (!flash->flash_lock || !flash->flash_unlock)
  169. return -EOPNOTSUPP;
  170. if (prot)
  171. return flash->flash_lock(flash, ofs, len);
  172. else
  173. return flash->flash_unlock(flash, ofs, len);
  174. }
  175. #endif /* _SPI_FLASH_H_ */