spi_flash.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. unsigned int max_hz, unsigned int spi_mode,
  94. struct udevice **devp);
  95. /* Compatibility function - this is the old U-Boot API */
  96. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  97. unsigned int max_hz, unsigned int spi_mode);
  98. /* Compatibility function - this is the old U-Boot API */
  99. static inline void spi_flash_free(struct spi_flash *flash)
  100. {
  101. }
  102. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  103. size_t len, void *buf)
  104. {
  105. return spi_flash_read_dm(flash->dev, offset, len, buf);
  106. }
  107. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  108. size_t len, const void *buf)
  109. {
  110. return spi_flash_write_dm(flash->dev, offset, len, buf);
  111. }
  112. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  113. size_t len)
  114. {
  115. return spi_flash_erase_dm(flash->dev, offset, len);
  116. }
  117. struct sandbox_state;
  118. int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
  119. struct udevice *bus, ofnode node, const char *spec);
  120. void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
  121. #else
  122. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  123. unsigned int max_hz, unsigned int spi_mode);
  124. void spi_flash_free(struct spi_flash *flash);
  125. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  126. size_t len, void *buf)
  127. {
  128. struct mtd_info *mtd = &flash->mtd;
  129. size_t retlen;
  130. if (!len)
  131. return 0;
  132. return mtd->_read(mtd, offset, len, &retlen, buf);
  133. }
  134. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  135. size_t len, const void *buf)
  136. {
  137. struct mtd_info *mtd = &flash->mtd;
  138. size_t retlen;
  139. if (!len)
  140. return 0;
  141. return mtd->_write(mtd, offset, len, &retlen, buf);
  142. }
  143. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  144. size_t len)
  145. {
  146. struct mtd_info *mtd = &flash->mtd;
  147. struct erase_info instr;
  148. if (offset % mtd->erasesize || len % mtd->erasesize) {
  149. printf("SF: Erase offset/length not multiple of erase size\n");
  150. return -EINVAL;
  151. }
  152. if (!len)
  153. return 0;
  154. memset(&instr, 0, sizeof(instr));
  155. instr.addr = offset;
  156. instr.len = len;
  157. return mtd->_erase(mtd, &instr);
  158. }
  159. #endif
  160. static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
  161. bool prot)
  162. {
  163. if (!flash->flash_lock || !flash->flash_unlock)
  164. return -EOPNOTSUPP;
  165. if (prot)
  166. return flash->flash_lock(flash, ofs, len);
  167. else
  168. return flash->flash_unlock(flash, ofs, len);
  169. }
  170. #endif /* _SPI_FLASH_H_ */