spi_flash.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* 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. /* 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. * spi_flash_std_probe() - Probe a SPI flash device
  70. *
  71. * This is the standard internal method for probing a SPI flash device to
  72. * determine its type. It can be used in chip-specific drivers which need to
  73. * do this, typically with of-platdata
  74. *
  75. * @dev: SPI-flash device to probe
  76. * @return 0 if OK, -ve on error
  77. */
  78. int spi_flash_std_probe(struct udevice *dev);
  79. int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
  80. unsigned int max_hz, unsigned int spi_mode,
  81. struct udevice **devp);
  82. /* Compatibility function - this is the old U-Boot API */
  83. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  84. unsigned int max_hz, unsigned int spi_mode);
  85. /* Compatibility function - this is the old U-Boot API */
  86. void spi_flash_free(struct spi_flash *flash);
  87. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  88. size_t len, void *buf)
  89. {
  90. return spi_flash_read_dm(flash->dev, offset, len, buf);
  91. }
  92. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  93. size_t len, const void *buf)
  94. {
  95. return spi_flash_write_dm(flash->dev, offset, len, buf);
  96. }
  97. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  98. size_t len)
  99. {
  100. return spi_flash_erase_dm(flash->dev, offset, len);
  101. }
  102. struct sandbox_state;
  103. int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
  104. struct udevice *bus, ofnode node, const char *spec);
  105. void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
  106. #else
  107. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  108. unsigned int max_hz, unsigned int spi_mode);
  109. void spi_flash_free(struct spi_flash *flash);
  110. static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
  111. size_t len, void *buf)
  112. {
  113. struct mtd_info *mtd = &flash->mtd;
  114. size_t retlen;
  115. return mtd->_read(mtd, offset, len, &retlen, buf);
  116. }
  117. static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
  118. size_t len, const void *buf)
  119. {
  120. struct mtd_info *mtd = &flash->mtd;
  121. size_t retlen;
  122. return mtd->_write(mtd, offset, len, &retlen, buf);
  123. }
  124. static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
  125. size_t len)
  126. {
  127. struct mtd_info *mtd = &flash->mtd;
  128. struct erase_info instr;
  129. if (offset % mtd->erasesize || len % mtd->erasesize) {
  130. printf("SF: Erase offset/length not multiple of erase size\n");
  131. return -EINVAL;
  132. }
  133. memset(&instr, 0, sizeof(instr));
  134. instr.addr = offset;
  135. instr.len = len;
  136. return mtd->_erase(mtd, &instr);
  137. }
  138. #endif
  139. static inline int spi_flash_protect(struct spi_flash *flash, u32 ofs, u32 len,
  140. bool prot)
  141. {
  142. if (!flash->flash_lock || !flash->flash_unlock)
  143. return -EOPNOTSUPP;
  144. if (prot)
  145. return flash->flash_lock(flash, ofs, len);
  146. else
  147. return flash->flash_unlock(flash, ofs, len);
  148. }
  149. #endif /* _SPI_FLASH_H_ */