Lines Matching +full:dma +full:- +full:mem
1 // SPDX-License-Identifier: GPL-2.0+
12 #include <linux/spi/spi-mem.h>
20 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
24 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
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
32 * op->data.buf.{in,out} is DMA-able before calling this function.
42 if (!op->data.nbytes) in spi_controller_dma_map_mem_op_data()
43 return -EINVAL; in spi_controller_dma_map_mem_op_data()
45 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) in spi_controller_dma_map_mem_op_data()
46 dmadev = ctlr->dma_tx->device->dev; in spi_controller_dma_map_mem_op_data()
47 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) in spi_controller_dma_map_mem_op_data()
48 dmadev = ctlr->dma_rx->device->dev; in spi_controller_dma_map_mem_op_data()
50 dmadev = ctlr->dev.parent; in spi_controller_dma_map_mem_op_data()
53 return -EINVAL; in spi_controller_dma_map_mem_op_data()
55 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes, in spi_controller_dma_map_mem_op_data()
56 op->data.dir == SPI_MEM_DATA_IN ? in spi_controller_dma_map_mem_op_data()
62 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
69 * Some controllers might want to do DMA on the data buffer embedded in @op.
71 * op->data.buf.{in,out} buffer again.
76 * This function should be called after the DMA operation has finished and is
88 if (!op->data.nbytes) in spi_controller_dma_unmap_mem_op_data()
91 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) in spi_controller_dma_unmap_mem_op_data()
92 dmadev = ctlr->dma_tx->device->dev; in spi_controller_dma_unmap_mem_op_data()
93 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) in spi_controller_dma_unmap_mem_op_data()
94 dmadev = ctlr->dma_rx->device->dev; in spi_controller_dma_unmap_mem_op_data()
96 dmadev = ctlr->dev.parent; in spi_controller_dma_unmap_mem_op_data()
99 op->data.dir == SPI_MEM_DATA_IN ? in spi_controller_dma_unmap_mem_op_data()
104 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx) in spi_check_buswidth_req() argument
106 u32 mode = mem->spi->mode; in spi_check_buswidth_req()
139 return -ENOTSUPP; in spi_check_buswidth_req()
142 static bool spi_mem_check_buswidth(struct spi_mem *mem, in spi_mem_check_buswidth() argument
145 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true)) in spi_mem_check_buswidth()
148 if (op->addr.nbytes && in spi_mem_check_buswidth()
149 spi_check_buswidth_req(mem, op->addr.buswidth, true)) in spi_mem_check_buswidth()
152 if (op->dummy.nbytes && in spi_mem_check_buswidth()
153 spi_check_buswidth_req(mem, op->dummy.buswidth, true)) in spi_mem_check_buswidth()
156 if (op->data.dir != SPI_MEM_NO_DATA && in spi_mem_check_buswidth()
157 spi_check_buswidth_req(mem, op->data.buswidth, in spi_mem_check_buswidth()
158 op->data.dir == SPI_MEM_DATA_OUT)) in spi_mem_check_buswidth()
164 bool spi_mem_default_supports_op(struct spi_mem *mem, in spi_mem_default_supports_op() argument
167 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_default_supports_op()
169 op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr; in spi_mem_default_supports_op()
175 if (op->cmd.nbytes != 2) in spi_mem_default_supports_op()
178 if (op->cmd.nbytes != 1) in spi_mem_default_supports_op()
182 if (op->data.ecc) { in spi_mem_default_supports_op()
187 return spi_mem_check_buswidth(mem, op); in spi_mem_default_supports_op()
201 if (!op->cmd.buswidth || !op->cmd.nbytes) in spi_mem_check_op()
202 return -EINVAL; in spi_mem_check_op()
204 if ((op->addr.nbytes && !op->addr.buswidth) || in spi_mem_check_op()
205 (op->dummy.nbytes && !op->dummy.buswidth) || in spi_mem_check_op()
206 (op->data.nbytes && !op->data.buswidth)) in spi_mem_check_op()
207 return -EINVAL; in spi_mem_check_op()
209 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) || in spi_mem_check_op()
210 !spi_mem_buswidth_is_valid(op->addr.buswidth) || in spi_mem_check_op()
211 !spi_mem_buswidth_is_valid(op->dummy.buswidth) || in spi_mem_check_op()
212 !spi_mem_buswidth_is_valid(op->data.buswidth)) in spi_mem_check_op()
213 return -EINVAL; in spi_mem_check_op()
215 /* Buffers must be DMA-able. */ in spi_mem_check_op()
216 if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_IN && in spi_mem_check_op()
217 object_is_on_stack(op->data.buf.in))) in spi_mem_check_op()
218 return -EINVAL; in spi_mem_check_op()
220 if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_OUT && in spi_mem_check_op()
221 object_is_on_stack(op->data.buf.out))) in spi_mem_check_op()
222 return -EINVAL; in spi_mem_check_op()
227 static bool spi_mem_internal_supports_op(struct spi_mem *mem, in spi_mem_internal_supports_op() argument
230 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_internal_supports_op()
232 if (ctlr->mem_ops && ctlr->mem_ops->supports_op) in spi_mem_internal_supports_op()
233 return ctlr->mem_ops->supports_op(mem, op); in spi_mem_internal_supports_op()
235 return spi_mem_default_supports_op(mem, op); in spi_mem_internal_supports_op()
239 * spi_mem_supports_op() - Check if a memory device and the controller it is
241 * @mem: the SPI memory
253 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) in spi_mem_supports_op() argument
258 return spi_mem_internal_supports_op(mem, op); in spi_mem_supports_op()
262 static int spi_mem_access_start(struct spi_mem *mem) in spi_mem_access_start() argument
264 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_access_start()
272 if (ctlr->auto_runtime_pm) { in spi_mem_access_start()
275 ret = pm_runtime_resume_and_get(ctlr->dev.parent); in spi_mem_access_start()
277 dev_err(&ctlr->dev, "Failed to power device: %d\n", in spi_mem_access_start()
283 mutex_lock(&ctlr->bus_lock_mutex); in spi_mem_access_start()
284 mutex_lock(&ctlr->io_mutex); in spi_mem_access_start()
289 static void spi_mem_access_end(struct spi_mem *mem) in spi_mem_access_end() argument
291 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_access_end()
293 mutex_unlock(&ctlr->io_mutex); in spi_mem_access_end()
294 mutex_unlock(&ctlr->bus_lock_mutex); in spi_mem_access_end()
296 if (ctlr->auto_runtime_pm) in spi_mem_access_end()
297 pm_runtime_put(ctlr->dev.parent); in spi_mem_access_end()
308 u64_stats_update_begin(&stats->syncp); in spi_mem_add_op_stats()
314 u64_stats_inc(&stats->messages); in spi_mem_add_op_stats()
315 u64_stats_inc(&stats->transfers); in spi_mem_add_op_stats()
318 len = op->cmd.nbytes + op->addr.nbytes; in spi_mem_add_op_stats()
319 len += op->dummy.nbytes + op->data.nbytes; in spi_mem_add_op_stats()
320 u64_stats_add(&stats->bytes, len); in spi_mem_add_op_stats()
321 l2len = min(fls(len), SPI_STATISTICS_HISTO_SIZE) - 1; in spi_mem_add_op_stats()
322 u64_stats_inc(&stats->transfer_bytes_histo[l2len]); in spi_mem_add_op_stats()
325 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) in spi_mem_add_op_stats()
326 u64_stats_add(&stats->bytes_tx, op->data.nbytes); in spi_mem_add_op_stats()
327 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN) in spi_mem_add_op_stats()
328 u64_stats_add(&stats->bytes_rx, op->data.nbytes); in spi_mem_add_op_stats()
334 if (exec_op_ret == -ETIMEDOUT) in spi_mem_add_op_stats()
335 u64_stats_inc(&stats->timedout); in spi_mem_add_op_stats()
337 u64_stats_inc(&stats->errors); in spi_mem_add_op_stats()
339 u64_stats_update_end(&stats->syncp); in spi_mem_add_op_stats()
344 * spi_mem_exec_op() - Execute a memory operation
345 * @mem: the SPI memory
355 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) in spi_mem_exec_op() argument
358 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_exec_op()
368 if (!spi_mem_internal_supports_op(mem, op)) in spi_mem_exec_op()
369 return -EOPNOTSUPP; in spi_mem_exec_op()
371 if (ctlr->mem_ops && ctlr->mem_ops->exec_op && !spi_get_csgpiod(mem->spi, 0)) { in spi_mem_exec_op()
372 ret = spi_mem_access_start(mem); in spi_mem_exec_op()
376 ret = ctlr->mem_ops->exec_op(mem, op); in spi_mem_exec_op()
378 spi_mem_access_end(mem); in spi_mem_exec_op()
385 if (!ret || (ret != -ENOTSUPP && ret != -EOPNOTSUPP)) { in spi_mem_exec_op()
386 spi_mem_add_op_stats(ctlr->pcpu_statistics, op, ret); in spi_mem_exec_op()
387 spi_mem_add_op_stats(mem->spi->pcpu_statistics, op, ret); in spi_mem_exec_op()
393 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; in spi_mem_exec_op()
397 * we're guaranteed that this buffer is DMA-able, as required by the in spi_mem_exec_op()
402 return -ENOMEM; in spi_mem_exec_op()
406 tmpbuf[0] = op->cmd.opcode; in spi_mem_exec_op()
408 xfers[xferpos].len = op->cmd.nbytes; in spi_mem_exec_op()
409 xfers[xferpos].tx_nbits = op->cmd.buswidth; in spi_mem_exec_op()
414 if (op->addr.nbytes) { in spi_mem_exec_op()
417 for (i = 0; i < op->addr.nbytes; i++) in spi_mem_exec_op()
418 tmpbuf[i + 1] = op->addr.val >> in spi_mem_exec_op()
419 (8 * (op->addr.nbytes - i - 1)); in spi_mem_exec_op()
422 xfers[xferpos].len = op->addr.nbytes; in spi_mem_exec_op()
423 xfers[xferpos].tx_nbits = op->addr.buswidth; in spi_mem_exec_op()
426 totalxferlen += op->addr.nbytes; in spi_mem_exec_op()
429 if (op->dummy.nbytes) { in spi_mem_exec_op()
430 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes); in spi_mem_exec_op()
431 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1; in spi_mem_exec_op()
432 xfers[xferpos].len = op->dummy.nbytes; in spi_mem_exec_op()
433 xfers[xferpos].tx_nbits = op->dummy.buswidth; in spi_mem_exec_op()
437 totalxferlen += op->dummy.nbytes; in spi_mem_exec_op()
440 if (op->data.nbytes) { in spi_mem_exec_op()
441 if (op->data.dir == SPI_MEM_DATA_IN) { in spi_mem_exec_op()
442 xfers[xferpos].rx_buf = op->data.buf.in; in spi_mem_exec_op()
443 xfers[xferpos].rx_nbits = op->data.buswidth; in spi_mem_exec_op()
445 xfers[xferpos].tx_buf = op->data.buf.out; in spi_mem_exec_op()
446 xfers[xferpos].tx_nbits = op->data.buswidth; in spi_mem_exec_op()
449 xfers[xferpos].len = op->data.nbytes; in spi_mem_exec_op()
452 totalxferlen += op->data.nbytes; in spi_mem_exec_op()
455 ret = spi_sync(mem->spi, &msg); in spi_mem_exec_op()
463 return -EIO; in spi_mem_exec_op()
470 * spi_mem_get_name() - Return the SPI mem device name to be used by the
472 * @mem: the SPI memory
474 * This function allows SPI mem users to retrieve the SPI mem device name.
479 * by the SPI mem user
481 const char *spi_mem_get_name(struct spi_mem *mem) in spi_mem_get_name() argument
483 return mem->name; in spi_mem_get_name()
488 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
490 * @mem: the SPI memory
495 * optimized accesses. This function allows SPI mem drivers to split a single
496 * operation into multiple sub-operations when required.
499 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
502 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) in spi_mem_adjust_op_size() argument
504 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_adjust_op_size()
507 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size) in spi_mem_adjust_op_size()
508 return ctlr->mem_ops->adjust_op_size(mem, op); in spi_mem_adjust_op_size()
510 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { in spi_mem_adjust_op_size()
511 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; in spi_mem_adjust_op_size()
513 if (len > spi_max_transfer_size(mem->spi)) in spi_mem_adjust_op_size()
514 return -EINVAL; in spi_mem_adjust_op_size()
516 op->data.nbytes = min3((size_t)op->data.nbytes, in spi_mem_adjust_op_size()
517 spi_max_transfer_size(mem->spi), in spi_mem_adjust_op_size()
518 spi_max_message_size(mem->spi) - in spi_mem_adjust_op_size()
520 if (!op->data.nbytes) in spi_mem_adjust_op_size()
521 return -EINVAL; in spi_mem_adjust_op_size()
531 struct spi_mem_op op = desc->info.op_tmpl; in spi_mem_no_dirmap_read()
534 op.addr.val = desc->info.offset + offs; in spi_mem_no_dirmap_read()
537 ret = spi_mem_adjust_op_size(desc->mem, &op); in spi_mem_no_dirmap_read()
541 ret = spi_mem_exec_op(desc->mem, &op); in spi_mem_no_dirmap_read()
551 struct spi_mem_op op = desc->info.op_tmpl; in spi_mem_no_dirmap_write()
554 op.addr.val = desc->info.offset + offs; in spi_mem_no_dirmap_write()
557 ret = spi_mem_adjust_op_size(desc->mem, &op); in spi_mem_no_dirmap_write()
561 ret = spi_mem_exec_op(desc->mem, &op); in spi_mem_no_dirmap_write()
569 * spi_mem_dirmap_create() - Create a direct mapping descriptor
570 * @mem: SPI mem device this direct mapping should be created for
582 spi_mem_dirmap_create(struct spi_mem *mem, in spi_mem_dirmap_create() argument
585 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_dirmap_create()
587 int ret = -ENOTSUPP; in spi_mem_dirmap_create()
590 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8) in spi_mem_dirmap_create()
591 return ERR_PTR(-EINVAL); in spi_mem_dirmap_create()
594 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA) in spi_mem_dirmap_create()
595 return ERR_PTR(-EINVAL); in spi_mem_dirmap_create()
599 return ERR_PTR(-ENOMEM); in spi_mem_dirmap_create()
601 desc->mem = mem; in spi_mem_dirmap_create()
602 desc->info = *info; in spi_mem_dirmap_create()
603 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) in spi_mem_dirmap_create()
604 ret = ctlr->mem_ops->dirmap_create(desc); in spi_mem_dirmap_create()
607 desc->nodirmap = true; in spi_mem_dirmap_create()
608 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl)) in spi_mem_dirmap_create()
609 ret = -EOPNOTSUPP; in spi_mem_dirmap_create()
624 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
632 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_destroy()
634 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy) in spi_mem_dirmap_destroy()
635 ctlr->mem_ops->dirmap_destroy(desc); in spi_mem_dirmap_destroy()
649 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
652 * @mem: SPI mem device this direct mapping should be created for
661 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem, in devm_spi_mem_dirmap_create() argument
669 return ERR_PTR(-ENOMEM); in devm_spi_mem_dirmap_create()
671 desc = spi_mem_dirmap_create(mem, info); in devm_spi_mem_dirmap_create()
694 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
711 * spi_mem_dirmap_read() - Read data through a direct mapping
717 * @buf: destination buffer. This buffer must be DMA-able
729 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_read()
732 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN) in spi_mem_dirmap_read()
733 return -EINVAL; in spi_mem_dirmap_read()
738 if (desc->nodirmap) { in spi_mem_dirmap_read()
740 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) { in spi_mem_dirmap_read()
741 ret = spi_mem_access_start(desc->mem); in spi_mem_dirmap_read()
745 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf); in spi_mem_dirmap_read()
747 spi_mem_access_end(desc->mem); in spi_mem_dirmap_read()
749 ret = -ENOTSUPP; in spi_mem_dirmap_read()
757 * spi_mem_dirmap_write() - Write data through a direct mapping
763 * @buf: source buffer. This buffer must be DMA-able
775 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_write()
778 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT) in spi_mem_dirmap_write()
779 return -EINVAL; in spi_mem_dirmap_write()
784 if (desc->nodirmap) { in spi_mem_dirmap_write()
786 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) { in spi_mem_dirmap_write()
787 ret = spi_mem_access_start(desc->mem); in spi_mem_dirmap_write()
791 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf); in spi_mem_dirmap_write()
793 spi_mem_access_end(desc->mem); in spi_mem_dirmap_write()
795 ret = -ENOTSUPP; in spi_mem_dirmap_write()
807 static int spi_mem_read_status(struct spi_mem *mem, in spi_mem_read_status() argument
811 const u8 *bytes = (u8 *)op->data.buf.in; in spi_mem_read_status()
814 ret = spi_mem_exec_op(mem, op); in spi_mem_read_status()
818 if (op->data.nbytes > 1) in spi_mem_read_status()
827 * spi_mem_poll_status() - Poll memory device status
828 * @mem: SPI memory device
839 * Return: 0 in case of success, -ETIMEDOUT in case of error,
840 * -EOPNOTSUPP if not supported.
842 int spi_mem_poll_status(struct spi_mem *mem, in spi_mem_poll_status() argument
849 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_poll_status()
850 int ret = -EOPNOTSUPP; in spi_mem_poll_status()
854 if (op->data.nbytes < 1 || op->data.nbytes > 2 || in spi_mem_poll_status()
855 op->data.dir != SPI_MEM_DATA_IN) in spi_mem_poll_status()
856 return -EINVAL; in spi_mem_poll_status()
858 if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !spi_get_csgpiod(mem->spi, 0)) { in spi_mem_poll_status()
859 ret = spi_mem_access_start(mem); in spi_mem_poll_status()
863 ret = ctlr->mem_ops->poll_status(mem, op, mask, match, in spi_mem_poll_status()
867 spi_mem_access_end(mem); in spi_mem_poll_status()
870 if (ret == -EOPNOTSUPP) { in spi_mem_poll_status()
871 if (!spi_mem_supports_op(mem, op)) in spi_mem_poll_status()
882 polling_delay_us, timeout_ms * 1000, false, mem, in spi_mem_poll_status()
894 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_probe()
895 struct spi_controller *ctlr = spi->controller; in spi_mem_probe()
896 struct spi_mem *mem; in spi_mem_probe() local
898 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL); in spi_mem_probe()
899 if (!mem) in spi_mem_probe()
900 return -ENOMEM; in spi_mem_probe()
902 mem->spi = spi; in spi_mem_probe()
904 if (ctlr->mem_ops && ctlr->mem_ops->get_name) in spi_mem_probe()
905 mem->name = ctlr->mem_ops->get_name(mem); in spi_mem_probe()
907 mem->name = dev_name(&spi->dev); in spi_mem_probe()
909 if (IS_ERR_OR_NULL(mem->name)) in spi_mem_probe()
910 return PTR_ERR_OR_ZERO(mem->name); in spi_mem_probe()
912 spi_set_drvdata(spi, mem); in spi_mem_probe()
914 return memdrv->probe(mem); in spi_mem_probe()
919 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_remove()
920 struct spi_mem *mem = spi_get_drvdata(spi); in spi_mem_remove() local
922 if (memdrv->remove) in spi_mem_remove()
923 memdrv->remove(mem); in spi_mem_remove()
928 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_shutdown()
929 struct spi_mem *mem = spi_get_drvdata(spi); in spi_mem_shutdown() local
931 if (memdrv->shutdown) in spi_mem_shutdown()
932 memdrv->shutdown(mem); in spi_mem_shutdown()
936 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
948 memdrv->spidrv.probe = spi_mem_probe; in spi_mem_driver_register_with_owner()
949 memdrv->spidrv.remove = spi_mem_remove; in spi_mem_driver_register_with_owner()
950 memdrv->spidrv.shutdown = spi_mem_shutdown; in spi_mem_driver_register_with_owner()
952 return __spi_register_driver(owner, &memdrv->spidrv); in spi_mem_driver_register_with_owner()
957 * spi_mem_driver_unregister() - Unregister a SPI memory driver
964 spi_unregister_driver(&memdrv->spidrv); in spi_mem_driver_unregister()