spi-mem.h 7.1 KB

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