1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
8 #include <linux/dmaengine.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/spi/spi.h>
11 #include <linux/spi/spi-mem.h>
13 #include "internals.h"
15 #define SPI_MEM_MAX_BUSWIDTH 8
18 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
20 * @ctlr: the SPI controller requesting this dma_map()
21 * @op: the memory operation containing the buffer to map
22 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
25 * Some controllers might want to do DMA on the data buffer embedded in @op.
26 * This helper prepares everything for you and provides a ready-to-use
27 * sg_table. This function is not intended to be called from spi drivers.
28 * Only SPI controller drivers should use it.
29 * Note that the caller must ensure the memory region pointed by
30 * op->data.buf.{in,out} is DMA-able before calling this function.
32 * Return: 0 in case of success, a negative error code otherwise.
34 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
35 const struct spi_mem_op *op,
38 struct device *dmadev;
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;
48 dmadev = ctlr->dev.parent;
53 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
54 op->data.dir == SPI_MEM_DATA_IN ?
55 DMA_FROM_DEVICE : DMA_TO_DEVICE);
57 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
60 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
62 * @ctlr: the SPI controller requesting this dma_unmap()
63 * @op: the memory operation containing the buffer to unmap
64 * @sgt: a pointer to an sg_table previously initialized by
65 * spi_controller_dma_map_mem_op_data()
67 * Some controllers might want to do DMA on the data buffer embedded in @op.
68 * This helper prepares things so that the CPU can access the
69 * op->data.buf.{in,out} buffer again.
71 * This function is not intended to be called from SPI drivers. Only SPI
72 * controller drivers should use it.
74 * This function should be called after the DMA operation has finished and is
75 * only valid if the previous spi_controller_dma_map_mem_op_data() call
78 * Return: 0 in case of success, a negative error code otherwise.
80 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
81 const struct spi_mem_op *op,
84 struct device *dmadev;
89 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
90 dmadev = ctlr->dma_tx->device->dev;
91 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
92 dmadev = ctlr->dma_rx->device->dev;
94 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);
100 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
102 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
104 u32 mode = mem->spi->mode;
112 (mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) ||
114 (mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))))
120 if ((tx && (mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) ||
121 (!tx && (mode & (SPI_RX_QUAD | SPI_RX_OCTAL))))
127 if ((tx && (mode & SPI_TX_OCTAL)) ||
128 (!tx && (mode & SPI_RX_OCTAL)))
140 bool spi_mem_default_supports_op(struct spi_mem *mem,
141 const struct spi_mem_op *op)
143 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
146 if (op->addr.nbytes &&
147 spi_check_buswidth_req(mem, op->addr.buswidth, true))
150 if (op->dummy.nbytes &&
151 spi_check_buswidth_req(mem, op->dummy.buswidth, true))
154 if (op->data.dir != SPI_MEM_NO_DATA &&
155 spi_check_buswidth_req(mem, op->data.buswidth,
156 op->data.dir == SPI_MEM_DATA_OUT))
159 if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
162 if (op->cmd.nbytes != 1)
167 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
169 static bool spi_mem_buswidth_is_valid(u8 buswidth)
171 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
177 static int spi_mem_check_op(const struct spi_mem_op *op)
179 if (!op->cmd.buswidth || !op->cmd.nbytes)
182 if ((op->addr.nbytes && !op->addr.buswidth) ||
183 (op->dummy.nbytes && !op->dummy.buswidth) ||
184 (op->data.nbytes && !op->data.buswidth))
187 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
188 !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
189 !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
190 !spi_mem_buswidth_is_valid(op->data.buswidth))
196 static bool spi_mem_internal_supports_op(struct spi_mem *mem,
197 const struct spi_mem_op *op)
199 struct spi_controller *ctlr = mem->spi->controller;
201 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
202 return ctlr->mem_ops->supports_op(mem, op);
204 return spi_mem_default_supports_op(mem, op);
208 * spi_mem_supports_op() - Check if a memory device and the controller it is
209 * connected to support a specific memory operation
210 * @mem: the SPI memory
211 * @op: the memory operation to check
213 * Some controllers are only supporting Single or Dual IOs, others might only
214 * support specific opcodes, or it can even be that the controller and device
215 * both support Quad IOs but the hardware prevents you from using it because
216 * only 2 IO lines are connected.
218 * This function checks whether a specific operation is supported.
220 * Return: true if @op is supported, false otherwise.
222 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
224 if (spi_mem_check_op(op))
227 return spi_mem_internal_supports_op(mem, op);
229 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
231 static int spi_mem_access_start(struct spi_mem *mem)
233 struct spi_controller *ctlr = mem->spi->controller;
236 * Flush the message queue before executing our SPI memory
237 * operation to prevent preemption of regular SPI transfers.
239 spi_flush_queue(ctlr);
241 if (ctlr->auto_runtime_pm) {
244 ret = pm_runtime_get_sync(ctlr->dev.parent);
246 dev_err(&ctlr->dev, "Failed to power device: %d\n",
252 mutex_lock(&ctlr->bus_lock_mutex);
253 mutex_lock(&ctlr->io_mutex);
258 static void spi_mem_access_end(struct spi_mem *mem)
260 struct spi_controller *ctlr = mem->spi->controller;
262 mutex_unlock(&ctlr->io_mutex);
263 mutex_unlock(&ctlr->bus_lock_mutex);
265 if (ctlr->auto_runtime_pm)
266 pm_runtime_put(ctlr->dev.parent);
270 * spi_mem_exec_op() - Execute a memory operation
271 * @mem: the SPI memory
272 * @op: the memory operation to execute
274 * Executes a memory operation.
276 * This function first checks that @op is supported and then tries to execute
279 * Return: 0 in case of success, a negative error code otherwise.
281 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
283 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
284 struct spi_controller *ctlr = mem->spi->controller;
285 struct spi_transfer xfers[4] = { };
286 struct spi_message msg;
290 ret = spi_mem_check_op(op);
294 if (!spi_mem_internal_supports_op(mem, op))
297 if (ctlr->mem_ops && !mem->spi->cs_gpiod) {
298 ret = spi_mem_access_start(mem);
302 ret = ctlr->mem_ops->exec_op(mem, op);
304 spi_mem_access_end(mem);
307 * Some controllers only optimize specific paths (typically the
308 * read path) and expect the core to use the regular SPI
309 * interface in other cases.
311 if (!ret || ret != -ENOTSUPP)
315 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
318 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
319 * we're guaranteed that this buffer is DMA-able, as required by the
322 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
326 spi_message_init(&msg);
328 tmpbuf[0] = op->cmd.opcode;
329 xfers[xferpos].tx_buf = tmpbuf;
330 xfers[xferpos].len = op->cmd.nbytes;
331 xfers[xferpos].tx_nbits = op->cmd.buswidth;
332 spi_message_add_tail(&xfers[xferpos], &msg);
336 if (op->addr.nbytes) {
339 for (i = 0; i < op->addr.nbytes; i++)
340 tmpbuf[i + 1] = op->addr.val >>
341 (8 * (op->addr.nbytes - i - 1));
343 xfers[xferpos].tx_buf = tmpbuf + 1;
344 xfers[xferpos].len = op->addr.nbytes;
345 xfers[xferpos].tx_nbits = op->addr.buswidth;
346 spi_message_add_tail(&xfers[xferpos], &msg);
348 totalxferlen += op->addr.nbytes;
351 if (op->dummy.nbytes) {
352 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
353 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
354 xfers[xferpos].len = op->dummy.nbytes;
355 xfers[xferpos].tx_nbits = op->dummy.buswidth;
356 spi_message_add_tail(&xfers[xferpos], &msg);
358 totalxferlen += op->dummy.nbytes;
361 if (op->data.nbytes) {
362 if (op->data.dir == SPI_MEM_DATA_IN) {
363 xfers[xferpos].rx_buf = op->data.buf.in;
364 xfers[xferpos].rx_nbits = op->data.buswidth;
366 xfers[xferpos].tx_buf = op->data.buf.out;
367 xfers[xferpos].tx_nbits = op->data.buswidth;
370 xfers[xferpos].len = op->data.nbytes;
371 spi_message_add_tail(&xfers[xferpos], &msg);
373 totalxferlen += op->data.nbytes;
376 ret = spi_sync(mem->spi, &msg);
383 if (msg.actual_length != totalxferlen)
388 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
391 * spi_mem_get_name() - Return the SPI mem device name to be used by the
392 * upper layer if necessary
393 * @mem: the SPI memory
395 * This function allows SPI mem users to retrieve the SPI mem device name.
396 * It is useful if the upper layer needs to expose a custom name for
397 * compatibility reasons.
399 * Return: a string containing the name of the memory device to be used
400 * by the SPI mem user
402 const char *spi_mem_get_name(struct spi_mem *mem)
406 EXPORT_SYMBOL_GPL(spi_mem_get_name);
409 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
410 * match controller limitations
411 * @mem: the SPI memory
412 * @op: the operation to adjust
414 * Some controllers have FIFO limitations and must split a data transfer
415 * operation into multiple ones, others require a specific alignment for
416 * optimized accesses. This function allows SPI mem drivers to split a single
417 * operation into multiple sub-operations when required.
419 * Return: a negative error code if the controller can't properly adjust @op,
420 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
421 * can't be handled in a single step.
423 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
425 struct spi_controller *ctlr = mem->spi->controller;
428 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
429 return ctlr->mem_ops->adjust_op_size(mem, op);
431 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
432 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
434 if (len > spi_max_transfer_size(mem->spi))
437 op->data.nbytes = min3((size_t)op->data.nbytes,
438 spi_max_transfer_size(mem->spi),
439 spi_max_message_size(mem->spi) -
441 if (!op->data.nbytes)
447 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
449 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
450 u64 offs, size_t len, void *buf)
452 struct spi_mem_op op = desc->info.op_tmpl;
455 op.addr.val = desc->info.offset + offs;
456 op.data.buf.in = buf;
457 op.data.nbytes = len;
458 ret = spi_mem_adjust_op_size(desc->mem, &op);
462 ret = spi_mem_exec_op(desc->mem, &op);
466 return op.data.nbytes;
469 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
470 u64 offs, size_t len, const void *buf)
472 struct spi_mem_op op = desc->info.op_tmpl;
475 op.addr.val = desc->info.offset + offs;
476 op.data.buf.out = buf;
477 op.data.nbytes = len;
478 ret = spi_mem_adjust_op_size(desc->mem, &op);
482 ret = spi_mem_exec_op(desc->mem, &op);
486 return op.data.nbytes;
490 * spi_mem_dirmap_create() - Create a direct mapping descriptor
491 * @mem: SPI mem device this direct mapping should be created for
492 * @info: direct mapping information
494 * This function is creating a direct mapping descriptor which can then be used
495 * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write().
496 * If the SPI controller driver does not support direct mapping, this function
497 * falls back to an implementation using spi_mem_exec_op(), so that the caller
498 * doesn't have to bother implementing a fallback on his own.
500 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
502 struct spi_mem_dirmap_desc *
503 spi_mem_dirmap_create(struct spi_mem *mem,
504 const struct spi_mem_dirmap_info *info)
506 struct spi_controller *ctlr = mem->spi->controller;
507 struct spi_mem_dirmap_desc *desc;
510 /* Make sure the number of address cycles is between 1 and 8 bytes. */
511 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
512 return ERR_PTR(-EINVAL);
514 /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */
515 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
516 return ERR_PTR(-EINVAL);
518 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
520 return ERR_PTR(-ENOMEM);
524 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create)
525 ret = ctlr->mem_ops->dirmap_create(desc);
528 desc->nodirmap = true;
529 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
542 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
545 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
546 * @desc: the direct mapping descriptor to destroy
548 * This function destroys a direct mapping descriptor previously created by
549 * spi_mem_dirmap_create().
551 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
553 struct spi_controller *ctlr = desc->mem->spi->controller;
555 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy)
556 ctlr->mem_ops->dirmap_destroy(desc);
560 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
562 static void devm_spi_mem_dirmap_release(struct device *dev, void *res)
564 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
566 spi_mem_dirmap_destroy(desc);
570 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
572 * @dev: device the dirmap desc will be attached to
573 * @mem: SPI mem device this direct mapping should be created for
574 * @info: direct mapping information
576 * devm_ variant of the spi_mem_dirmap_create() function. See
577 * spi_mem_dirmap_create() for more details.
579 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
581 struct spi_mem_dirmap_desc *
582 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
583 const struct spi_mem_dirmap_info *info)
585 struct spi_mem_dirmap_desc **ptr, *desc;
587 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
590 return ERR_PTR(-ENOMEM);
592 desc = spi_mem_dirmap_create(mem, info);
597 devres_add(dev, ptr);
602 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
604 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data)
606 struct spi_mem_dirmap_desc **ptr = res;
608 if (WARN_ON(!ptr || !*ptr))
615 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
617 * @dev: device the dirmap desc is attached to
618 * @desc: the direct mapping descriptor to destroy
620 * devm_ variant of the spi_mem_dirmap_destroy() function. See
621 * spi_mem_dirmap_destroy() for more details.
623 void devm_spi_mem_dirmap_destroy(struct device *dev,
624 struct spi_mem_dirmap_desc *desc)
626 devres_release(dev, devm_spi_mem_dirmap_release,
627 devm_spi_mem_dirmap_match, desc);
629 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
632 * spi_mem_dirmap_read() - Read data through a direct mapping
633 * @desc: direct mapping descriptor
634 * @offs: offset to start reading from. Note that this is not an absolute
635 * offset, but the offset within the direct mapping which already has
637 * @len: length in bytes
638 * @buf: destination buffer. This buffer must be DMA-able
640 * This function reads data from a memory device using a direct mapping
641 * previously instantiated with spi_mem_dirmap_create().
643 * Return: the amount of data read from the memory device or a negative error
644 * code. Note that the returned size might be smaller than @len, and the caller
645 * is responsible for calling spi_mem_dirmap_read() again when that happens.
647 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
648 u64 offs, size_t len, void *buf)
650 struct spi_controller *ctlr = desc->mem->spi->controller;
653 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
659 if (desc->nodirmap) {
660 ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
661 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
662 ret = spi_mem_access_start(desc->mem);
666 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);
668 spi_mem_access_end(desc->mem);
675 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
678 * spi_mem_dirmap_write() - Write data through a direct mapping
679 * @desc: direct mapping descriptor
680 * @offs: offset to start writing from. Note that this is not an absolute
681 * offset, but the offset within the direct mapping which already has
683 * @len: length in bytes
684 * @buf: source buffer. This buffer must be DMA-able
686 * This function writes data to a memory device using a direct mapping
687 * previously instantiated with spi_mem_dirmap_create().
689 * Return: the amount of data written to the memory device or a negative error
690 * code. Note that the returned size might be smaller than @len, and the caller
691 * is responsible for calling spi_mem_dirmap_write() again when that happens.
693 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
694 u64 offs, size_t len, const void *buf)
696 struct spi_controller *ctlr = desc->mem->spi->controller;
699 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
705 if (desc->nodirmap) {
706 ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
707 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) {
708 ret = spi_mem_access_start(desc->mem);
712 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf);
714 spi_mem_access_end(desc->mem);
721 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
723 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
725 return container_of(drv, struct spi_mem_driver, spidrv.driver);
728 static int spi_mem_probe(struct spi_device *spi)
730 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
731 struct spi_controller *ctlr = spi->controller;
734 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
740 if (ctlr->mem_ops && ctlr->mem_ops->get_name)
741 mem->name = ctlr->mem_ops->get_name(mem);
743 mem->name = dev_name(&spi->dev);
745 if (IS_ERR_OR_NULL(mem->name))
746 return PTR_ERR(mem->name);
748 spi_set_drvdata(spi, mem);
750 return memdrv->probe(mem);
753 static int spi_mem_remove(struct spi_device *spi)
755 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
756 struct spi_mem *mem = spi_get_drvdata(spi);
759 return memdrv->remove(mem);
764 static void spi_mem_shutdown(struct spi_device *spi)
766 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
767 struct spi_mem *mem = spi_get_drvdata(spi);
769 if (memdrv->shutdown)
770 memdrv->shutdown(mem);
774 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
775 * @memdrv: the SPI memory driver to register
776 * @owner: the owner of this driver
778 * Registers a SPI memory driver.
780 * Return: 0 in case of success, a negative error core otherwise.
783 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
784 struct module *owner)
786 memdrv->spidrv.probe = spi_mem_probe;
787 memdrv->spidrv.remove = spi_mem_remove;
788 memdrv->spidrv.shutdown = spi_mem_shutdown;
790 return __spi_register_driver(owner, &memdrv->spidrv);
792 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
795 * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
796 * @memdrv: the SPI memory driver to unregister
798 * Unregisters a SPI memory driver.
800 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
802 spi_unregister_driver(&memdrv->spidrv);
804 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);