spi-mem.h 7.1 KB

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