spi-mem.h 6.9 KB

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