spi-mem.h 7.8 KB

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