spi-mem.c 14 KB

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