spi-mem.c 15 KB

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