spi-mem.c 14 KB

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