spi_flash.h 5.4 KB

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