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()
301 * spi_mem_exec_op() - Execute a memory operation
302 * @mem: the SPI memory
312 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) in spi_mem_exec_op() argument
315 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_exec_op()
325 if (!spi_mem_internal_supports_op(mem, op)) in spi_mem_exec_op()
326 return -ENOTSUPP; in spi_mem_exec_op()
328 if (ctlr->mem_ops && !mem->spi->cs_gpiod) { in spi_mem_exec_op()
329 ret = spi_mem_access_start(mem); in spi_mem_exec_op()
333 ret = ctlr->mem_ops->exec_op(mem, op); in spi_mem_exec_op()
335 spi_mem_access_end(mem); in spi_mem_exec_op()
342 if (!ret || ret != -ENOTSUPP) in spi_mem_exec_op()
346 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; in spi_mem_exec_op()
350 * we're guaranteed that this buffer is DMA-able, as required by the in spi_mem_exec_op()
355 return -ENOMEM; in spi_mem_exec_op()
359 tmpbuf[0] = op->cmd.opcode; in spi_mem_exec_op()
361 xfers[xferpos].len = op->cmd.nbytes; in spi_mem_exec_op()
362 xfers[xferpos].tx_nbits = op->cmd.buswidth; in spi_mem_exec_op()
367 if (op->addr.nbytes) { in spi_mem_exec_op()
370 for (i = 0; i < op->addr.nbytes; i++) in spi_mem_exec_op()
371 tmpbuf[i + 1] = op->addr.val >> in spi_mem_exec_op()
372 (8 * (op->addr.nbytes - i - 1)); in spi_mem_exec_op()
375 xfers[xferpos].len = op->addr.nbytes; in spi_mem_exec_op()
376 xfers[xferpos].tx_nbits = op->addr.buswidth; in spi_mem_exec_op()
379 totalxferlen += op->addr.nbytes; in spi_mem_exec_op()
382 if (op->dummy.nbytes) { in spi_mem_exec_op()
383 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes); in spi_mem_exec_op()
384 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1; in spi_mem_exec_op()
385 xfers[xferpos].len = op->dummy.nbytes; in spi_mem_exec_op()
386 xfers[xferpos].tx_nbits = op->dummy.buswidth; in spi_mem_exec_op()
390 totalxferlen += op->dummy.nbytes; in spi_mem_exec_op()
393 if (op->data.nbytes) { in spi_mem_exec_op()
394 if (op->data.dir == SPI_MEM_DATA_IN) { in spi_mem_exec_op()
395 xfers[xferpos].rx_buf = op->data.buf.in; in spi_mem_exec_op()
396 xfers[xferpos].rx_nbits = op->data.buswidth; in spi_mem_exec_op()
398 xfers[xferpos].tx_buf = op->data.buf.out; in spi_mem_exec_op()
399 xfers[xferpos].tx_nbits = op->data.buswidth; in spi_mem_exec_op()
402 xfers[xferpos].len = op->data.nbytes; in spi_mem_exec_op()
405 totalxferlen += op->data.nbytes; in spi_mem_exec_op()
408 ret = spi_sync(mem->spi, &msg); in spi_mem_exec_op()
416 return -EIO; in spi_mem_exec_op()
423 * spi_mem_get_name() - Return the SPI mem device name to be used by the
425 * @mem: the SPI memory
427 * This function allows SPI mem users to retrieve the SPI mem device name.
432 * by the SPI mem user
434 const char *spi_mem_get_name(struct spi_mem *mem) in spi_mem_get_name() argument
436 return mem->name; in spi_mem_get_name()
441 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
443 * @mem: the SPI memory
448 * optimized accesses. This function allows SPI mem drivers to split a single
449 * operation into multiple sub-operations when required.
452 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
455 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) in spi_mem_adjust_op_size() argument
457 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_adjust_op_size()
460 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size) in spi_mem_adjust_op_size()
461 return ctlr->mem_ops->adjust_op_size(mem, op); in spi_mem_adjust_op_size()
463 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { in spi_mem_adjust_op_size()
464 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; in spi_mem_adjust_op_size()
466 if (len > spi_max_transfer_size(mem->spi)) in spi_mem_adjust_op_size()
467 return -EINVAL; in spi_mem_adjust_op_size()
469 op->data.nbytes = min3((size_t)op->data.nbytes, in spi_mem_adjust_op_size()
470 spi_max_transfer_size(mem->spi), in spi_mem_adjust_op_size()
471 spi_max_message_size(mem->spi) - in spi_mem_adjust_op_size()
473 if (!op->data.nbytes) in spi_mem_adjust_op_size()
474 return -EINVAL; in spi_mem_adjust_op_size()
484 struct spi_mem_op op = desc->info.op_tmpl; in spi_mem_no_dirmap_read()
487 op.addr.val = desc->info.offset + offs; in spi_mem_no_dirmap_read()
490 ret = spi_mem_adjust_op_size(desc->mem, &op); in spi_mem_no_dirmap_read()
494 ret = spi_mem_exec_op(desc->mem, &op); in spi_mem_no_dirmap_read()
504 struct spi_mem_op op = desc->info.op_tmpl; in spi_mem_no_dirmap_write()
507 op.addr.val = desc->info.offset + offs; in spi_mem_no_dirmap_write()
510 ret = spi_mem_adjust_op_size(desc->mem, &op); in spi_mem_no_dirmap_write()
514 ret = spi_mem_exec_op(desc->mem, &op); in spi_mem_no_dirmap_write()
522 * spi_mem_dirmap_create() - Create a direct mapping descriptor
523 * @mem: SPI mem device this direct mapping should be created for
535 spi_mem_dirmap_create(struct spi_mem *mem, in spi_mem_dirmap_create() argument
538 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_dirmap_create()
540 int ret = -ENOTSUPP; in spi_mem_dirmap_create()
543 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8) in spi_mem_dirmap_create()
544 return ERR_PTR(-EINVAL); in spi_mem_dirmap_create()
547 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA) in spi_mem_dirmap_create()
548 return ERR_PTR(-EINVAL); in spi_mem_dirmap_create()
552 return ERR_PTR(-ENOMEM); in spi_mem_dirmap_create()
554 desc->mem = mem; in spi_mem_dirmap_create()
555 desc->info = *info; in spi_mem_dirmap_create()
556 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) in spi_mem_dirmap_create()
557 ret = ctlr->mem_ops->dirmap_create(desc); in spi_mem_dirmap_create()
560 desc->nodirmap = true; in spi_mem_dirmap_create()
561 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl)) in spi_mem_dirmap_create()
562 ret = -ENOTSUPP; in spi_mem_dirmap_create()
577 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
585 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_destroy()
587 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy) in spi_mem_dirmap_destroy()
588 ctlr->mem_ops->dirmap_destroy(desc); in spi_mem_dirmap_destroy()
602 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
605 * @mem: SPI mem device this direct mapping should be created for
614 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem, in devm_spi_mem_dirmap_create() argument
622 return ERR_PTR(-ENOMEM); in devm_spi_mem_dirmap_create()
624 desc = spi_mem_dirmap_create(mem, info); in devm_spi_mem_dirmap_create()
647 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
664 * spi_mem_dirmap_read() - Read data through a direct mapping
670 * @buf: destination buffer. This buffer must be DMA-able
682 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_read()
685 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN) in spi_mem_dirmap_read()
686 return -EINVAL; in spi_mem_dirmap_read()
691 if (desc->nodirmap) { in spi_mem_dirmap_read()
693 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) { in spi_mem_dirmap_read()
694 ret = spi_mem_access_start(desc->mem); in spi_mem_dirmap_read()
698 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf); in spi_mem_dirmap_read()
700 spi_mem_access_end(desc->mem); in spi_mem_dirmap_read()
702 ret = -ENOTSUPP; in spi_mem_dirmap_read()
710 * spi_mem_dirmap_write() - Write data through a direct mapping
716 * @buf: source buffer. This buffer must be DMA-able
728 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_write()
731 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT) in spi_mem_dirmap_write()
732 return -EINVAL; in spi_mem_dirmap_write()
737 if (desc->nodirmap) { in spi_mem_dirmap_write()
739 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) { in spi_mem_dirmap_write()
740 ret = spi_mem_access_start(desc->mem); in spi_mem_dirmap_write()
744 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf); in spi_mem_dirmap_write()
746 spi_mem_access_end(desc->mem); in spi_mem_dirmap_write()
748 ret = -ENOTSUPP; in spi_mem_dirmap_write()
760 static int spi_mem_read_status(struct spi_mem *mem, in spi_mem_read_status() argument
764 const u8 *bytes = (u8 *)op->data.buf.in; in spi_mem_read_status()
767 ret = spi_mem_exec_op(mem, op); in spi_mem_read_status()
771 if (op->data.nbytes > 1) in spi_mem_read_status()
780 * spi_mem_poll_status() - Poll memory device status
781 * @mem: SPI memory device
792 * Return: 0 in case of success, -ETIMEDOUT in case of error,
793 * -EOPNOTSUPP if not supported.
795 int spi_mem_poll_status(struct spi_mem *mem, in spi_mem_poll_status() argument
802 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_poll_status()
803 int ret = -EOPNOTSUPP; in spi_mem_poll_status()
807 if (op->data.nbytes < 1 || op->data.nbytes > 2 || in spi_mem_poll_status()
808 op->data.dir != SPI_MEM_DATA_IN) in spi_mem_poll_status()
809 return -EINVAL; in spi_mem_poll_status()
811 if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !mem->spi->cs_gpiod) { in spi_mem_poll_status()
812 ret = spi_mem_access_start(mem); in spi_mem_poll_status()
816 ret = ctlr->mem_ops->poll_status(mem, op, mask, match, in spi_mem_poll_status()
820 spi_mem_access_end(mem); in spi_mem_poll_status()
823 if (ret == -EOPNOTSUPP) { in spi_mem_poll_status()
824 if (!spi_mem_supports_op(mem, op)) in spi_mem_poll_status()
835 polling_delay_us, timeout_ms * 1000, false, mem, in spi_mem_poll_status()
847 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_probe()
848 struct spi_controller *ctlr = spi->controller; in spi_mem_probe()
849 struct spi_mem *mem; in spi_mem_probe() local
851 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL); in spi_mem_probe()
852 if (!mem) in spi_mem_probe()
853 return -ENOMEM; in spi_mem_probe()
855 mem->spi = spi; in spi_mem_probe()
857 if (ctlr->mem_ops && ctlr->mem_ops->get_name) in spi_mem_probe()
858 mem->name = ctlr->mem_ops->get_name(mem); in spi_mem_probe()
860 mem->name = dev_name(&spi->dev); in spi_mem_probe()
862 if (IS_ERR_OR_NULL(mem->name)) in spi_mem_probe()
863 return PTR_ERR_OR_ZERO(mem->name); in spi_mem_probe()
865 spi_set_drvdata(spi, mem); in spi_mem_probe()
867 return memdrv->probe(mem); in spi_mem_probe()
872 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_remove()
873 struct spi_mem *mem = spi_get_drvdata(spi); in spi_mem_remove() local
875 if (memdrv->remove) in spi_mem_remove()
876 memdrv->remove(mem); in spi_mem_remove()
881 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_shutdown()
882 struct spi_mem *mem = spi_get_drvdata(spi); in spi_mem_shutdown() local
884 if (memdrv->shutdown) in spi_mem_shutdown()
885 memdrv->shutdown(mem); in spi_mem_shutdown()
889 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
901 memdrv->spidrv.probe = spi_mem_probe; in spi_mem_driver_register_with_owner()
902 memdrv->spidrv.remove = spi_mem_remove; in spi_mem_driver_register_with_owner()
903 memdrv->spidrv.shutdown = spi_mem_shutdown; in spi_mem_driver_register_with_owner()
905 return __spi_register_driver(owner, &memdrv->spidrv); in spi_mem_driver_register_with_owner()
910 * spi_mem_driver_unregister() - Unregister a SPI memory driver
917 spi_unregister_driver(&memdrv->spidrv); in spi_mem_driver_unregister()