spi_flash.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  95. unsigned int max_hz, unsigned int spi_mode,
  96. struct udevice **devp);
  97. /* Compatibility function - this is the old U-Boot API */
  98. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  99. unsigned int max_hz, unsigned int spi_mode);
  100. /* Compatibility function - this is the old U-Boot API */
  101. void spi_flash_free(struct spi_flash *flash);
  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. return mtd->_read(mtd, offset, len, &retlen, buf);
  131. }
  132. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  133. size_t len, const void *buf)
  134. {
  135. struct mtd_info *mtd = &flash->mtd;
  136. size_t retlen;
  137. return mtd->_write(mtd, offset, len, &retlen, buf);
  138. }
  139. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  140. size_t len)
  141. {
  142. struct mtd_info *mtd = &flash->mtd;
  143. struct erase_info instr;
  144. if (offset % mtd->erasesize || len % mtd->erasesize) {
  145. printf("SF: Erase offset/length not multiple of erase size\n");
  146. return -EINVAL;
  147. }
  148. memset(&instr, 0, sizeof(instr));
  149. instr.addr = offset;
  150. instr.len = len;
  151. return mtd->_erase(mtd, &instr);
  152. }
  153. #endif
  154. static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
  155. bool prot)
  156. {
  157. if (!flash->flash_lock || !flash->flash_unlock)
  158. return -EOPNOTSUPP;
  159. if (prot)
  160. return flash->flash_lock(flash, ofs, len);
  161. else
  162. return flash->flash_unlock(flash, ofs, len);
  163. }
  164. #endif /* _SPI_FLASH_H_ */