spi-mem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Exceet Electronics GmbH
  4. * Copyright (C) 2018 Bootlin
  5. *
  6. * Author: Boris Brezillon <boris.brezillon@bootlin.com>
  7. */
  8. #include <linux/dmaengine.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/spi/spi-mem.h>
  12. #include "internals.h"
  13. /**
  14. * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
  15. * memory operation
  16. * @ctlr: the SPI controller requesting this dma_map()
  17. * @op: the memory operation containing the buffer to map
  18. * @sgt: a pointer to a non-initialized sg_table that will be filled by this
  19. * function
  20. *
  21. * Some controllers might want to do DMA on the data buffer embedded in @op.
  22. * This helper prepares everything for you and provides a ready-to-use
  23. * sg_table. This function is not intended to be called from spi drivers.
  24. * Only SPI controller drivers should use it.
  25. * Note that the caller must ensure the memory region pointed by
  26. * op->data.buf.{in,out} is DMA-able before calling this function.
  27. *
  28. * Return: 0 in case of success, a negative error code otherwise.
  29. */
  30. int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
  31. const struct spi_mem_op *op,
  32. struct sg_table *sgt)
  33. {
  34. struct device *dmadev;
  35. if (!op->data.nbytes)
  36. return -EINVAL;
  37. if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
  38. dmadev = ctlr->dma_tx->device->dev;
  39. else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
  40. dmadev = ctlr->dma_rx->device->dev;
  41. else
  42. dmadev = ctlr->dev.parent;
  43. if (!dmadev)
  44. return -EINVAL;
  45. return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
  46. op->data.dir == SPI_MEM_DATA_IN ?
  47. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  48. }
  49. EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
  50. /**
  51. * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
  52. * memory operation
  53. * @ctlr: the SPI controller requesting this dma_unmap()
  54. * @op: the memory operation containing the buffer to unmap
  55. * @sgt: a pointer to an sg_table previously initialized by
  56. * spi_controller_dma_map_mem_op_data()
  57. *
  58. * Some controllers might want to do DMA on the data buffer embedded in @op.
  59. * This helper prepares things so that the CPU can access the
  60. * op->data.buf.{in,out} buffer again.
  61. *
  62. * This function is not intended to be called from SPI drivers. Only SPI
  63. * controller drivers should use it.
  64. *
  65. * This function should be called after the DMA operation has finished and is
  66. * only valid if the previous spi_controller_dma_map_mem_op_data() call
  67. * returned 0.
  68. *
  69. * Return: 0 in case of success, a negative error code otherwise.
  70. */
  71. void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
  72. const struct spi_mem_op *op,
  73. struct sg_table *sgt)
  74. {
  75. struct device *dmadev;
  76. if (!op->data.nbytes)
  77. return;
  78. if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
  79. dmadev = ctlr->dma_tx->device->dev;
  80. else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
  81. dmadev = ctlr->dma_rx->device->dev;
  82. else
  83. dmadev = ctlr->dev.parent;
  84. spi_unmap_buf(ctlr, dmadev, sgt,
  85. op->data.dir == SPI_MEM_DATA_IN ?
  86. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  87. }
  88. EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
  89. static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
  90. {
  91. u32 mode = mem->spi->mode;
  92. switch (buswidth) {
  93. case 1:
  94. return 0;
  95. case 2:
  96. if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
  97. (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
  98. return 0;
  99. break;
  100. case 4:
  101. if ((tx && (mode & SPI_TX_QUAD)) ||
  102. (!tx && (mode & SPI_RX_QUAD)))
  103. return 0;
  104. break;
  105. default:
  106. break;
  107. }
  108. return -ENOTSUPP;
  109. }
  110. static bool spi_mem_default_supports_op(struct spi_mem *mem,
  111. const struct spi_mem_op *op)
  112. {
  113. if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
  114. return false;
  115. if (op->addr.nbytes &&
  116. spi_check_buswidth_req(mem, op->addr.buswidth, true))
  117. return false;
  118. if (op->dummy.nbytes &&
  119. spi_check_buswidth_req(mem, op->dummy.buswidth, true))
  120. return false;
  121. if (op->data.nbytes &&
  122. spi_check_buswidth_req(mem, op->data.buswidth,
  123. op->data.dir == SPI_MEM_DATA_OUT))
  124. return false;
  125. return true;
  126. }
  127. EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
  128. /**
  129. * spi_mem_supports_op() - Check if a memory device and the controller it is
  130. * connected to support a specific memory operation
  131. * @mem: the SPI memory
  132. * @op: the memory operation to check
  133. *
  134. * Some controllers are only supporting Single or Dual IOs, others might only
  135. * support specific opcodes, or it can even be that the controller and device
  136. * both support Quad IOs but the hardware prevents you from using it because
  137. * only 2 IO lines are connected.
  138. *
  139. * This function checks whether a specific operation is supported.
  140. *
  141. * Return: true if @op is supported, false otherwise.
  142. */
  143. bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
  144. {
  145. struct spi_controller *ctlr = mem->spi->controller;
  146. if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
  147. return ctlr->mem_ops->supports_op(mem, op);
  148. return spi_mem_default_supports_op(mem, op);
  149. }
  150. EXPORT_SYMBOL_GPL(spi_mem_supports_op);
  151. /**
  152. * spi_mem_exec_op() - Execute a memory operation
  153. * @mem: the SPI memory
  154. * @op: the memory operation to execute
  155. *
  156. * Executes a memory operation.
  157. *
  158. * This function first checks that @op is supported and then tries to execute
  159. * it.
  160. *
  161. * Return: 0 in case of success, a negative error code otherwise.
  162. */
  163. int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
  164. {
  165. unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
  166. struct spi_controller *ctlr = mem->spi->controller;
  167. struct spi_transfer xfers[4] = { };
  168. struct spi_message msg;
  169. u8 *tmpbuf;
  170. int ret;
  171. if (!spi_mem_supports_op(mem, op))
  172. return -ENOTSUPP;
  173. if (ctlr->mem_ops) {
  174. /*
  175. * Flush the message queue before executing our SPI memory
  176. * operation to prevent preemption of regular SPI transfers.
  177. */
  178. spi_flush_queue(ctlr);
  179. if (ctlr->auto_runtime_pm) {
  180. ret = pm_runtime_get_sync(ctlr->dev.parent);
  181. if (ret < 0) {
  182. dev_err(&ctlr->dev,
  183. "Failed to power device: %d\n",
  184. ret);
  185. return ret;
  186. }
  187. }
  188. mutex_lock(&ctlr->bus_lock_mutex);
  189. mutex_lock(&ctlr->io_mutex);
  190. ret = ctlr->mem_ops->exec_op(mem, op);
  191. mutex_unlock(&ctlr->io_mutex);
  192. mutex_unlock(&ctlr->bus_lock_mutex);
  193. if (ctlr->auto_runtime_pm)
  194. pm_runtime_put(ctlr->dev.parent);
  195. /*
  196. * Some controllers only optimize specific paths (typically the
  197. * read path) and expect the core to use the regular SPI
  198. * interface in other cases.
  199. */
  200. if (!ret || ret != -ENOTSUPP)
  201. return ret;
  202. }
  203. tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
  204. op->dummy.nbytes;
  205. /*
  206. * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
  207. * we're guaranteed that this buffer is DMA-able, as required by the
  208. * SPI layer.
  209. */
  210. tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
  211. if (!tmpbuf)
  212. return -ENOMEM;
  213. spi_message_init(&msg);
  214. tmpbuf[0] = op->cmd.opcode;
  215. xfers[xferpos].tx_buf = tmpbuf;
  216. xfers[xferpos].len = sizeof(op->cmd.opcode);
  217. xfers[xferpos].tx_nbits = op->cmd.buswidth;
  218. spi_message_add_tail(&xfers[xferpos], &msg);
  219. xferpos++;
  220. totalxferlen++;
  221. if (op->addr.nbytes) {
  222. int i;
  223. for (i = 0; i < op->addr.nbytes; i++)
  224. tmpbuf[i + 1] = op->addr.val >>
  225. (8 * (op->addr.nbytes - i - 1));
  226. xfers[xferpos].tx_buf = tmpbuf + 1;
  227. xfers[xferpos].len = op->addr.nbytes;
  228. xfers[xferpos].tx_nbits = op->addr.buswidth;
  229. spi_message_add_tail(&xfers[xferpos], &msg);
  230. xferpos++;
  231. totalxferlen += op->addr.nbytes;
  232. }
  233. if (op->dummy.nbytes) {
  234. memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
  235. xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
  236. xfers[xferpos].len = op->dummy.nbytes;
  237. xfers[xferpos].tx_nbits = op->dummy.buswidth;
  238. spi_message_add_tail(&xfers[xferpos], &msg);
  239. xferpos++;
  240. totalxferlen += op->dummy.nbytes;
  241. }
  242. if (op->data.nbytes) {
  243. if (op->data.dir == SPI_MEM_DATA_IN) {
  244. xfers[xferpos].rx_buf = op->data.buf.in;
  245. xfers[xferpos].rx_nbits = op->data.buswidth;
  246. } else {
  247. xfers[xferpos].tx_buf = op->data.buf.out;
  248. xfers[xferpos].tx_nbits = op->data.buswidth;
  249. }
  250. xfers[xferpos].len = op->data.nbytes;
  251. spi_message_add_tail(&xfers[xferpos], &msg);
  252. xferpos++;
  253. totalxferlen += op->data.nbytes;
  254. }
  255. ret = spi_sync(mem->spi, &msg);
  256. kfree(tmpbuf);
  257. if (ret)
  258. return ret;
  259. if (msg.actual_length != totalxferlen)
  260. return -EIO;
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(spi_mem_exec_op);
  264. /**
  265. * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
  266. * match controller limitations
  267. * @mem: the SPI memory
  268. * @op: the operation to adjust
  269. *
  270. * Some controllers have FIFO limitations and must split a data transfer
  271. * operation into multiple ones, others require a specific alignment for
  272. * optimized accesses. This function allows SPI mem drivers to split a single
  273. * operation into multiple sub-operations when required.
  274. *
  275. * Return: a negative error code if the controller can't properly adjust @op,
  276. * 0 otherwise. Note that @op->data.nbytes will be updated if @op
  277. * can't be handled in a single step.
  278. */
  279. int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
  280. {
  281. struct spi_controller *ctlr = mem->spi->controller;
  282. if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
  283. return ctlr->mem_ops->adjust_op_size(mem, op);
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
  287. static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
  288. {
  289. return container_of(drv, struct spi_mem_driver, spidrv.driver);
  290. }
  291. static int spi_mem_probe(struct spi_device *spi)
  292. {
  293. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  294. struct spi_mem *mem;
  295. mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
  296. if (!mem)
  297. return -ENOMEM;
  298. mem->spi = spi;
  299. spi_set_drvdata(spi, mem);
  300. return memdrv->probe(mem);
  301. }
  302. static int spi_mem_remove(struct spi_device *spi)
  303. {
  304. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  305. struct spi_mem *mem = spi_get_drvdata(spi);
  306. if (memdrv->remove)
  307. return memdrv->remove(mem);
  308. return 0;
  309. }
  310. static void spi_mem_shutdown(struct spi_device *spi)
  311. {
  312. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  313. struct spi_mem *mem = spi_get_drvdata(spi);
  314. if (memdrv->shutdown)
  315. memdrv->shutdown(mem);
  316. }
  317. /**
  318. * spi_mem_driver_register_with_owner() - Register a SPI memory driver
  319. * @memdrv: the SPI memory driver to register
  320. * @owner: the owner of this driver
  321. *
  322. * Registers a SPI memory driver.
  323. *
  324. * Return: 0 in case of success, a negative error core otherwise.
  325. */
  326. int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
  327. struct module *owner)
  328. {
  329. memdrv->spidrv.probe = spi_mem_probe;
  330. memdrv->spidrv.remove = spi_mem_remove;
  331. memdrv->spidrv.shutdown = spi_mem_shutdown;
  332. return __spi_register_driver(owner, &memdrv->spidrv);
  333. }
  334. EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
  335. /**
  336. * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
  337. * @memdrv: the SPI memory driver to unregister
  338. *
  339. * Unregisters a SPI memory driver.
  340. */
  341. void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
  342. {
  343. spi_unregister_driver(&memdrv->spidrv);
  344. }
  345. EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);