spi-mem.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2018 Exceet Electronics GmbH
  4. * Copyright (C) 2018 Bootlin
  5. *
  6. * Author:
  7. * Peter Pan <peterpandong@micron.com>
  8. * Boris Brezillon <boris.brezillon@bootlin.com>
  9. */
  10. #ifndef __UBOOT_SPI_MEM_H
  11. #define __UBOOT_SPI_MEM_H
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <spi.h>
  16. #define SPI_MEM_OP_CMD(__opcode, __buswidth) \
  17. { \
  18. .buswidth = __buswidth, \
  19. .opcode = __opcode, \
  20. }
  21. #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \
  22. { \
  23. .nbytes = __nbytes, \
  24. .val = __val, \
  25. .buswidth = __buswidth, \
  26. }
  27. #define SPI_MEM_OP_NO_ADDR { }
  28. #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \
  29. { \
  30. .nbytes = __nbytes, \
  31. .buswidth = __buswidth, \
  32. }
  33. #define SPI_MEM_OP_NO_DUMMY { }
  34. #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \
  35. { \
  36. .dir = SPI_MEM_DATA_IN, \
  37. .nbytes = __nbytes, \
  38. .buf.in = __buf, \
  39. .buswidth = __buswidth, \
  40. }
  41. #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \
  42. { \
  43. .dir = SPI_MEM_DATA_OUT, \
  44. .nbytes = __nbytes, \
  45. .buf.out = __buf, \
  46. .buswidth = __buswidth, \
  47. }
  48. #define SPI_MEM_OP_NO_DATA { }
  49. /**
  50. * enum spi_mem_data_dir - describes the direction of a SPI memory data
  51. * transfer from the controller perspective
  52. * @SPI_MEM_DATA_IN: data coming from the SPI memory
  53. * @SPI_MEM_DATA_OUT: data sent the SPI memory
  54. */
  55. enum spi_mem_data_dir {
  56. SPI_MEM_DATA_IN,
  57. SPI_MEM_DATA_OUT,
  58. };
  59. /**
  60. * struct spi_mem_op - describes a SPI memory operation
  61. * @cmd.buswidth: number of IO lines used to transmit the command
  62. * @cmd.opcode: operation opcode
  63. * @addr.nbytes: number of address bytes to send. Can be zero if the operation
  64. * does not need to send an address
  65. * @addr.buswidth: number of IO lines used to transmit the address cycles
  66. * @addr.val: address value. This value is always sent MSB first on the bus.
  67. * Note that only @addr.nbytes are taken into account in this
  68. * address value, so users should make sure the value fits in the
  69. * assigned number of bytes.
  70. * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can
  71. * be zero if the operation does not require dummy bytes
  72. * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes
  73. * @data.buswidth: number of IO lanes used to send/receive the data
  74. * @data.dir: direction of the transfer
  75. * @data.buf.in: input buffer
  76. * @data.buf.out: output buffer
  77. */
  78. struct spi_mem_op {
  79. struct {
  80. u8 buswidth;
  81. u8 opcode;
  82. } cmd;
  83. struct {
  84. u8 nbytes;
  85. u8 buswidth;
  86. u64 val;
  87. } addr;
  88. struct {
  89. u8 nbytes;
  90. u8 buswidth;
  91. } dummy;
  92. struct {
  93. u8 buswidth;
  94. enum spi_mem_data_dir dir;
  95. unsigned int nbytes;
  96. /* buf.{in,out} must be DMA-able. */
  97. union {
  98. void *in;
  99. const void *out;
  100. } buf;
  101. } data;
  102. };
  103. #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
  104. { \
  105. .cmd = __cmd, \
  106. .addr = __addr, \
  107. .dummy = __dummy, \
  108. .data = __data, \
  109. }
  110. #ifndef __UBOOT__
  111. /**
  112. * struct spi_mem - describes a SPI memory device
  113. * @spi: the underlying SPI device
  114. * @drvpriv: spi_mem_driver private data
  115. *
  116. * Extra information that describe the SPI memory device and may be needed by
  117. * the controller to properly handle this device should be placed here.
  118. *
  119. * One example would be the device size since some controller expose their SPI
  120. * mem devices through a io-mapped region.
  121. */
  122. struct spi_mem {
  123. struct udevice *dev;
  124. void *drvpriv;
  125. };
  126. /**
  127. * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
  128. * device
  129. * @mem: memory device
  130. * @data: data to attach to the memory device
  131. */
  132. static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
  133. {
  134. mem->drvpriv = data;
  135. }
  136. /**
  137. * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
  138. * device
  139. * @mem: memory device
  140. *
  141. * Return: the data attached to the mem device.
  142. */
  143. static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
  144. {
  145. return mem->drvpriv;
  146. }
  147. #endif /* __UBOOT__ */
  148. /**
  149. * struct spi_controller_mem_ops - SPI memory operations
  150. * @adjust_op_size: shrink the data xfer of an operation to match controller's
  151. * limitations (can be alignment of max RX/TX size
  152. * limitations)
  153. * @supports_op: check if an operation is supported by the controller
  154. * @exec_op: execute a SPI memory operation
  155. *
  156. * This interface should be implemented by SPI controllers providing an
  157. * high-level interface to execute SPI memory operation, which is usually the
  158. * case for QSPI controllers.
  159. */
  160. struct spi_controller_mem_ops {
  161. int (*adjust_op_size)(struct spi_slave *slave, struct spi_mem_op *op);
  162. bool (*supports_op)(struct spi_slave *slave,
  163. const struct spi_mem_op *op);
  164. int (*exec_op)(struct spi_slave *slave,
  165. const struct spi_mem_op *op);
  166. };
  167. #ifndef __UBOOT__
  168. /**
  169. * struct spi_mem_driver - SPI memory driver
  170. * @spidrv: inherit from a SPI driver
  171. * @probe: probe a SPI memory. Usually where detection/initialization takes
  172. * place
  173. * @remove: remove a SPI memory
  174. * @shutdown: take appropriate action when the system is shutdown
  175. *
  176. * This is just a thin wrapper around a spi_driver. The core takes care of
  177. * allocating the spi_mem object and forwarding the probe/remove/shutdown
  178. * request to the spi_mem_driver. The reason we use this wrapper is because
  179. * we might have to stuff more information into the spi_mem struct to let
  180. * SPI controllers know more about the SPI memory they interact with, and
  181. * having this intermediate layer allows us to do that without adding more
  182. * useless fields to the spi_device object.
  183. */
  184. struct spi_mem_driver {
  185. struct spi_driver spidrv;
  186. int (*probe)(struct spi_mem *mem);
  187. int (*remove)(struct spi_mem *mem);
  188. void (*shutdown)(struct spi_mem *mem);
  189. };
  190. #if IS_ENABLED(CONFIG_SPI_MEM)
  191. int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
  192. const struct spi_mem_op *op,
  193. struct sg_table *sg);
  194. void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
  195. const struct spi_mem_op *op,
  196. struct sg_table *sg);
  197. #else
  198. static inline int
  199. spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
  200. const struct spi_mem_op *op,
  201. struct sg_table *sg)
  202. {
  203. return -ENOTSUPP;
  204. }
  205. static inline void
  206. spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
  207. const struct spi_mem_op *op,
  208. struct sg_table *sg)
  209. {
  210. }
  211. #endif /* CONFIG_SPI_MEM */
  212. #endif /* __UBOOT__ */
  213. int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op);
  214. bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op);
  215. int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op);
  216. #ifndef __UBOOT__
  217. int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
  218. struct module *owner);
  219. void spi_mem_driver_unregister(struct spi_mem_driver *drv);
  220. #define spi_mem_driver_register(__drv) \
  221. spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
  222. #define module_spi_mem_driver(__drv) \
  223. module_driver(__drv, spi_mem_driver_register, \
  224. spi_mem_driver_unregister)
  225. #endif
  226. #endif /* __LINUX_SPI_MEM_H */