spi-mem.c 12 KB

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