spi-mem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <[email protected]>
  8. * Boris Brezillon <[email protected]>
  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. .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 to 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.dtr: whether the address should be sent in DTR mode or not
  70. * @addr.val: address value. This value is always sent MSB first on the bus.
  71. * Note that only @addr.nbytes are taken into account in this
  72. * address value, so users should make sure the value fits in the
  73. * assigned number of bytes.
  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.ecc: whether error correction is required or not
  81. * @data.dir: direction of the transfer
  82. * @data.nbytes: number of data bytes to send/receive. Can be zero if the
  83. * operation does not involve transferring data
  84. * @data.buf.in: input buffer (must be DMA-able)
  85. * @data.buf.out: output buffer (must be DMA-able)
  86. */
  87. struct spi_mem_op {
  88. struct {
  89. u8 nbytes;
  90. u8 buswidth;
  91. u8 dtr : 1;
  92. u16 opcode;
  93. } cmd;
  94. struct {
  95. u8 nbytes;
  96. u8 buswidth;
  97. u8 dtr : 1;
  98. u64 val;
  99. } addr;
  100. struct {
  101. u8 nbytes;
  102. u8 buswidth;
  103. u8 dtr : 1;
  104. } dummy;
  105. struct {
  106. u8 buswidth;
  107. u8 dtr : 1;
  108. u8 ecc : 1;
  109. enum spi_mem_data_dir dir;
  110. unsigned int nbytes;
  111. union {
  112. void *in;
  113. const void *out;
  114. } buf;
  115. } data;
  116. };
  117. #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \
  118. { \
  119. .cmd = __cmd, \
  120. .addr = __addr, \
  121. .dummy = __dummy, \
  122. .data = __data, \
  123. }
  124. /**
  125. * struct spi_mem_dirmap_info - Direct mapping information
  126. * @op_tmpl: operation template that should be used by the direct mapping when
  127. * the memory device is accessed
  128. * @offset: absolute offset this direct mapping is pointing to
  129. * @length: length in byte of this direct mapping
  130. *
  131. * These information are used by the controller specific implementation to know
  132. * the portion of memory that is directly mapped and the spi_mem_op that should
  133. * be used to access the device.
  134. * A direct mapping is only valid for one direction (read or write) and this
  135. * direction is directly encoded in the ->op_tmpl.data.dir field.
  136. */
  137. struct spi_mem_dirmap_info {
  138. struct spi_mem_op op_tmpl;
  139. u64 offset;
  140. u64 length;
  141. };
  142. /**
  143. * struct spi_mem_dirmap_desc - Direct mapping descriptor
  144. * @mem: the SPI memory device this direct mapping is attached to
  145. * @info: information passed at direct mapping creation time
  146. * @nodirmap: set to 1 if the SPI controller does not implement
  147. * ->mem_ops->dirmap_create() or when this function returned an
  148. * error. If @nodirmap is true, all spi_mem_dirmap_{read,write}()
  149. * calls will use spi_mem_exec_op() to access the memory. This is a
  150. * degraded mode that allows spi_mem drivers to use the same code
  151. * no matter whether the controller supports direct mapping or not
  152. * @priv: field pointing to controller specific data
  153. *
  154. * Common part of a direct mapping descriptor. This object is created by
  155. * spi_mem_dirmap_create() and controller implementation of ->create_dirmap()
  156. * can create/attach direct mapping resources to the descriptor in the ->priv
  157. * field.
  158. */
  159. struct spi_mem_dirmap_desc {
  160. struct spi_mem *mem;
  161. struct spi_mem_dirmap_info info;
  162. unsigned int nodirmap;
  163. void *priv;
  164. };
  165. /**
  166. * struct spi_mem - describes a SPI memory device
  167. * @spi: the underlying SPI device
  168. * @drvpriv: spi_mem_driver private data
  169. * @name: name of the SPI memory device
  170. *
  171. * Extra information that describe the SPI memory device and may be needed by
  172. * the controller to properly handle this device should be placed here.
  173. *
  174. * One example would be the device size since some controller expose their SPI
  175. * mem devices through a io-mapped region.
  176. */
  177. struct spi_mem {
  178. struct spi_device *spi;
  179. void *drvpriv;
  180. const char *name;
  181. };
  182. /**
  183. * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem
  184. * device
  185. * @mem: memory device
  186. * @data: data to attach to the memory device
  187. */
  188. static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data)
  189. {
  190. mem->drvpriv = data;
  191. }
  192. /**
  193. * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem
  194. * device
  195. * @mem: memory device
  196. *
  197. * Return: the data attached to the mem device.
  198. */
  199. static inline void *spi_mem_get_drvdata(struct spi_mem *mem)
  200. {
  201. return mem->drvpriv;
  202. }
  203. /**
  204. * struct spi_controller_mem_ops - SPI memory operations
  205. * @adjust_op_size: shrink the data xfer of an operation to match controller's
  206. * limitations (can be alignment or max RX/TX size
  207. * limitations)
  208. * @supports_op: check if an operation is supported by the controller
  209. * @exec_op: execute a SPI memory operation
  210. * @get_name: get a custom name for the SPI mem device from the controller.
  211. * This might be needed if the controller driver has been ported
  212. * to use the SPI mem layer and a custom name is used to keep
  213. * mtdparts compatible.
  214. * Note that if the implementation of this function allocates memory
  215. * dynamically, then it should do so with devm_xxx(), as we don't
  216. * have a ->free_name() function.
  217. * @dirmap_create: create a direct mapping descriptor that can later be used to
  218. * access the memory device. This method is optional
  219. * @dirmap_destroy: destroy a memory descriptor previous created by
  220. * ->dirmap_create()
  221. * @dirmap_read: read data from the memory device using the direct mapping
  222. * created by ->dirmap_create(). The function can return less
  223. * data than requested (for example when the request is crossing
  224. * the currently mapped area), and the caller of
  225. * spi_mem_dirmap_read() is responsible for calling it again in
  226. * this case.
  227. * @dirmap_write: write data to the memory device using the direct mapping
  228. * created by ->dirmap_create(). The function can return less
  229. * data than requested (for example when the request is crossing
  230. * the currently mapped area), and the caller of
  231. * spi_mem_dirmap_write() is responsible for calling it again in
  232. * this case.
  233. * @poll_status: poll memory device status until (status & mask) == match or
  234. * when the timeout has expired. It fills the data buffer with
  235. * the last status value.
  236. *
  237. * This interface should be implemented by SPI controllers providing an
  238. * high-level interface to execute SPI memory operation, which is usually the
  239. * case for QSPI controllers.
  240. *
  241. * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct
  242. * mapping from the CPU because doing that can stall the CPU waiting for the
  243. * SPI mem transaction to finish, and this will make real-time maintainers
  244. * unhappy and might make your system less reactive. Instead, drivers should
  245. * use DMA to access this direct mapping.
  246. */
  247. struct spi_controller_mem_ops {
  248. int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op);
  249. bool (*supports_op)(struct spi_mem *mem,
  250. const struct spi_mem_op *op);
  251. int (*exec_op)(struct spi_mem *mem,
  252. const struct spi_mem_op *op);
  253. const char *(*get_name)(struct spi_mem *mem);
  254. int (*dirmap_create)(struct spi_mem_dirmap_desc *desc);
  255. void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc);
  256. ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc,
  257. u64 offs, size_t len, void *buf);
  258. ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc,
  259. u64 offs, size_t len, const void *buf);
  260. int (*poll_status)(struct spi_mem *mem,
  261. const struct spi_mem_op *op,
  262. u16 mask, u16 match,
  263. unsigned long initial_delay_us,
  264. unsigned long polling_rate_us,
  265. unsigned long timeout_ms);
  266. };
  267. /**
  268. * struct spi_controller_mem_caps - SPI memory controller capabilities
  269. * @dtr: Supports DTR operations
  270. * @ecc: Supports operations with error correction
  271. */
  272. struct spi_controller_mem_caps {
  273. bool dtr;
  274. bool ecc;
  275. };
  276. #define spi_mem_controller_is_capable(ctlr, cap) \
  277. ((ctlr)->mem_caps && (ctlr)->mem_caps->cap)
  278. /**
  279. * struct spi_mem_driver - SPI memory driver
  280. * @spidrv: inherit from a SPI driver
  281. * @probe: probe a SPI memory. Usually where detection/initialization takes
  282. * place
  283. * @remove: remove a SPI memory
  284. * @shutdown: take appropriate action when the system is shutdown
  285. *
  286. * This is just a thin wrapper around a spi_driver. The core takes care of
  287. * allocating the spi_mem object and forwarding the probe/remove/shutdown
  288. * request to the spi_mem_driver. The reason we use this wrapper is because
  289. * we might have to stuff more information into the spi_mem struct to let
  290. * SPI controllers know more about the SPI memory they interact with, and
  291. * having this intermediate layer allows us to do that without adding more
  292. * useless fields to the spi_device object.
  293. */
  294. struct spi_mem_driver {
  295. struct spi_driver spidrv;
  296. int (*probe)(struct spi_mem *mem);
  297. int (*remove)(struct spi_mem *mem);
  298. void (*shutdown)(struct spi_mem *mem);
  299. };
  300. #if IS_ENABLED(CONFIG_SPI_MEM)
  301. int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
  302. const struct spi_mem_op *op,
  303. struct sg_table *sg);
  304. void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
  305. const struct spi_mem_op *op,
  306. struct sg_table *sg);
  307. bool spi_mem_default_supports_op(struct spi_mem *mem,
  308. const struct spi_mem_op *op);
  309. #else
  310. static inline int
  311. spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
  312. const struct spi_mem_op *op,
  313. struct sg_table *sg)
  314. {
  315. return -ENOTSUPP;
  316. }
  317. static inline void
  318. spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
  319. const struct spi_mem_op *op,
  320. struct sg_table *sg)
  321. {
  322. }
  323. static inline
  324. bool spi_mem_default_supports_op(struct spi_mem *mem,
  325. const struct spi_mem_op *op)
  326. {
  327. return false;
  328. }
  329. #endif /* CONFIG_SPI_MEM */
  330. int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op);
  331. bool spi_mem_supports_op(struct spi_mem *mem,
  332. const struct spi_mem_op *op);
  333. int spi_mem_exec_op(struct spi_mem *mem,
  334. const struct spi_mem_op *op);
  335. const char *spi_mem_get_name(struct spi_mem *mem);
  336. struct spi_mem_dirmap_desc *
  337. spi_mem_dirmap_create(struct spi_mem *mem,
  338. const struct spi_mem_dirmap_info *info);
  339. void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc);
  340. ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
  341. u64 offs, size_t len, void *buf);
  342. ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
  343. u64 offs, size_t len, const void *buf);
  344. struct spi_mem_dirmap_desc *
  345. devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
  346. const struct spi_mem_dirmap_info *info);
  347. void devm_spi_mem_dirmap_destroy(struct device *dev,
  348. struct spi_mem_dirmap_desc *desc);
  349. int spi_mem_poll_status(struct spi_mem *mem,
  350. const struct spi_mem_op *op,
  351. u16 mask, u16 match,
  352. unsigned long initial_delay_us,
  353. unsigned long polling_delay_us,
  354. u16 timeout_ms);
  355. int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv,
  356. struct module *owner);
  357. void spi_mem_driver_unregister(struct spi_mem_driver *drv);
  358. #define spi_mem_driver_register(__drv) \
  359. spi_mem_driver_register_with_owner(__drv, THIS_MODULE)
  360. #define module_spi_mem_driver(__drv) \
  361. module_driver(__drv, spi_mem_driver_register, \
  362. spi_mem_driver_unregister)
  363. #endif /* __LINUX_SPI_MEM_H */