spi-mem.h 7.2 KB

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