spi-mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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_get_name() - Return the SPI mem device name to be used by the
  266. * upper layer if necessary
  267. * @mem: the SPI memory
  268. *
  269. * This function allows SPI mem users to retrieve the SPI mem device name.
  270. * It is useful if the upper layer needs to expose a custom name for
  271. * compatibility reasons.
  272. *
  273. * Return: a string containing the name of the memory device to be used
  274. * by the SPI mem user
  275. */
  276. const char *spi_mem_get_name(struct spi_mem *mem)
  277. {
  278. return mem->name;
  279. }
  280. EXPORT_SYMBOL_GPL(spi_mem_get_name);
  281. /**
  282. * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
  283. * match controller limitations
  284. * @mem: the SPI memory
  285. * @op: the operation to adjust
  286. *
  287. * Some controllers have FIFO limitations and must split a data transfer
  288. * operation into multiple ones, others require a specific alignment for
  289. * optimized accesses. This function allows SPI mem drivers to split a single
  290. * operation into multiple sub-operations when required.
  291. *
  292. * Return: a negative error code if the controller can't properly adjust @op,
  293. * 0 otherwise. Note that @op->data.nbytes will be updated if @op
  294. * can't be handled in a single step.
  295. */
  296. int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
  297. {
  298. struct spi_controller *ctlr = mem->spi->controller;
  299. size_t len;
  300. len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
  301. if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
  302. return ctlr->mem_ops->adjust_op_size(mem, op);
  303. if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
  304. if (len > spi_max_transfer_size(mem->spi))
  305. return -EINVAL;
  306. op->data.nbytes = min3((size_t)op->data.nbytes,
  307. spi_max_transfer_size(mem->spi),
  308. spi_max_message_size(mem->spi) -
  309. len);
  310. if (!op->data.nbytes)
  311. return -EINVAL;
  312. }
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
  316. static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
  317. {
  318. return container_of(drv, struct spi_mem_driver, spidrv.driver);
  319. }
  320. static int spi_mem_probe(struct spi_device *spi)
  321. {
  322. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  323. struct spi_controller *ctlr = spi->controller;
  324. struct spi_mem *mem;
  325. mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
  326. if (!mem)
  327. return -ENOMEM;
  328. mem->spi = spi;
  329. if (ctlr->mem_ops && ctlr->mem_ops->get_name)
  330. mem->name = ctlr->mem_ops->get_name(mem);
  331. else
  332. mem->name = dev_name(&spi->dev);
  333. if (IS_ERR_OR_NULL(mem->name))
  334. return PTR_ERR(mem->name);
  335. spi_set_drvdata(spi, mem);
  336. return memdrv->probe(mem);
  337. }
  338. static int spi_mem_remove(struct spi_device *spi)
  339. {
  340. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  341. struct spi_mem *mem = spi_get_drvdata(spi);
  342. if (memdrv->remove)
  343. return memdrv->remove(mem);
  344. return 0;
  345. }
  346. static void spi_mem_shutdown(struct spi_device *spi)
  347. {
  348. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  349. struct spi_mem *mem = spi_get_drvdata(spi);
  350. if (memdrv->shutdown)
  351. memdrv->shutdown(mem);
  352. }
  353. /**
  354. * spi_mem_driver_register_with_owner() - Register a SPI memory driver
  355. * @memdrv: the SPI memory driver to register
  356. * @owner: the owner of this driver
  357. *
  358. * Registers a SPI memory driver.
  359. *
  360. * Return: 0 in case of success, a negative error core otherwise.
  361. */
  362. int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
  363. struct module *owner)
  364. {
  365. memdrv->spidrv.probe = spi_mem_probe;
  366. memdrv->spidrv.remove = spi_mem_remove;
  367. memdrv->spidrv.shutdown = spi_mem_shutdown;
  368. return __spi_register_driver(owner, &memdrv->spidrv);
  369. }
  370. EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
  371. /**
  372. * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
  373. * @memdrv: the SPI memory driver to unregister
  374. *
  375. * Unregisters a SPI memory driver.
  376. */
  377. void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
  378. {
  379. spi_unregister_driver(&memdrv->spidrv);
  380. }
  381. EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);