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 4
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;
111 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
112 (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
118 if ((tx && (mode & SPI_TX_QUAD)) ||
119 (!tx && (mode & SPI_RX_QUAD)))
131 static bool spi_mem_default_supports_op(struct spi_mem *mem,
132 const struct spi_mem_op *op)
134 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
137 if (op->addr.nbytes &&
138 spi_check_buswidth_req(mem, op->addr.buswidth, true))
141 if (op->dummy.nbytes &&
142 spi_check_buswidth_req(mem, op->dummy.buswidth, true))
145 if (op->data.nbytes &&
146 spi_check_buswidth_req(mem, op->data.buswidth,
147 op->data.dir == SPI_MEM_DATA_OUT))
152 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
154 static bool spi_mem_buswidth_is_valid(u8 buswidth)
156 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
162 static int spi_mem_check_op(const struct spi_mem_op *op)
164 if (!op->cmd.buswidth)
167 if ((op->addr.nbytes && !op->addr.buswidth) ||
168 (op->dummy.nbytes && !op->dummy.buswidth) ||
169 (op->data.nbytes && !op->data.buswidth))
172 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
173 !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
174 !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
175 !spi_mem_buswidth_is_valid(op->data.buswidth))
181 static bool spi_mem_internal_supports_op(struct spi_mem *mem,
182 const struct spi_mem_op *op)
184 struct spi_controller *ctlr = mem->spi->controller;
186 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
187 return ctlr->mem_ops->supports_op(mem, op);
189 return spi_mem_default_supports_op(mem, op);
193 * spi_mem_supports_op() - Check if a memory device and the controller it is
194 * connected to support a specific memory operation
195 * @mem: the SPI memory
196 * @op: the memory operation to check
198 * Some controllers are only supporting Single or Dual IOs, others might only
199 * support specific opcodes, or it can even be that the controller and device
200 * both support Quad IOs but the hardware prevents you from using it because
201 * only 2 IO lines are connected.
203 * This function checks whether a specific operation is supported.
205 * Return: true if @op is supported, false otherwise.
207 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
209 if (spi_mem_check_op(op))
212 return spi_mem_internal_supports_op(mem, op);
214 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
217 * spi_mem_exec_op() - Execute a memory operation
218 * @mem: the SPI memory
219 * @op: the memory operation to execute
221 * Executes a memory operation.
223 * This function first checks that @op is supported and then tries to execute
226 * Return: 0 in case of success, a negative error code otherwise.
228 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
230 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
231 struct spi_controller *ctlr = mem->spi->controller;
232 struct spi_transfer xfers[4] = { };
233 struct spi_message msg;
237 ret = spi_mem_check_op(op);
241 if (!spi_mem_internal_supports_op(mem, op))
246 * Flush the message queue before executing our SPI memory
247 * operation to prevent preemption of regular SPI transfers.
249 spi_flush_queue(ctlr);
251 if (ctlr->auto_runtime_pm) {
252 ret = pm_runtime_get_sync(ctlr->dev.parent);
255 "Failed to power device: %d\n",
261 mutex_lock(&ctlr->bus_lock_mutex);
262 mutex_lock(&ctlr->io_mutex);
263 ret = ctlr->mem_ops->exec_op(mem, op);
264 mutex_unlock(&ctlr->io_mutex);
265 mutex_unlock(&ctlr->bus_lock_mutex);
267 if (ctlr->auto_runtime_pm)
268 pm_runtime_put(ctlr->dev.parent);
271 * Some controllers only optimize specific paths (typically the
272 * read path) and expect the core to use the regular SPI
273 * interface in other cases.
275 if (!ret || ret != -ENOTSUPP)
279 tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
283 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
284 * we're guaranteed that this buffer is DMA-able, as required by the
287 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
291 spi_message_init(&msg);
293 tmpbuf[0] = op->cmd.opcode;
294 xfers[xferpos].tx_buf = tmpbuf;
295 xfers[xferpos].len = sizeof(op->cmd.opcode);
296 xfers[xferpos].tx_nbits = op->cmd.buswidth;
297 spi_message_add_tail(&xfers[xferpos], &msg);
301 if (op->addr.nbytes) {
304 for (i = 0; i < op->addr.nbytes; i++)
305 tmpbuf[i + 1] = op->addr.val >>
306 (8 * (op->addr.nbytes - i - 1));
308 xfers[xferpos].tx_buf = tmpbuf + 1;
309 xfers[xferpos].len = op->addr.nbytes;
310 xfers[xferpos].tx_nbits = op->addr.buswidth;
311 spi_message_add_tail(&xfers[xferpos], &msg);
313 totalxferlen += op->addr.nbytes;
316 if (op->dummy.nbytes) {
317 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
318 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
319 xfers[xferpos].len = op->dummy.nbytes;
320 xfers[xferpos].tx_nbits = op->dummy.buswidth;
321 spi_message_add_tail(&xfers[xferpos], &msg);
323 totalxferlen += op->dummy.nbytes;
326 if (op->data.nbytes) {
327 if (op->data.dir == SPI_MEM_DATA_IN) {
328 xfers[xferpos].rx_buf = op->data.buf.in;
329 xfers[xferpos].rx_nbits = op->data.buswidth;
331 xfers[xferpos].tx_buf = op->data.buf.out;
332 xfers[xferpos].tx_nbits = op->data.buswidth;
335 xfers[xferpos].len = op->data.nbytes;
336 spi_message_add_tail(&xfers[xferpos], &msg);
338 totalxferlen += op->data.nbytes;
341 ret = spi_sync(mem->spi, &msg);
348 if (msg.actual_length != totalxferlen)
353 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
356 * spi_mem_get_name() - Return the SPI mem device name to be used by the
357 * upper layer if necessary
358 * @mem: the SPI memory
360 * This function allows SPI mem users to retrieve the SPI mem device name.
361 * It is useful if the upper layer needs to expose a custom name for
362 * compatibility reasons.
364 * Return: a string containing the name of the memory device to be used
365 * by the SPI mem user
367 const char *spi_mem_get_name(struct spi_mem *mem)
371 EXPORT_SYMBOL_GPL(spi_mem_get_name);
374 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
375 * match controller limitations
376 * @mem: the SPI memory
377 * @op: the operation to adjust
379 * Some controllers have FIFO limitations and must split a data transfer
380 * operation into multiple ones, others require a specific alignment for
381 * optimized accesses. This function allows SPI mem drivers to split a single
382 * operation into multiple sub-operations when required.
384 * Return: a negative error code if the controller can't properly adjust @op,
385 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
386 * can't be handled in a single step.
388 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
390 struct spi_controller *ctlr = mem->spi->controller;
393 len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
395 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
396 return ctlr->mem_ops->adjust_op_size(mem, op);
398 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
399 if (len > spi_max_transfer_size(mem->spi))
402 op->data.nbytes = min3((size_t)op->data.nbytes,
403 spi_max_transfer_size(mem->spi),
404 spi_max_message_size(mem->spi) -
406 if (!op->data.nbytes)
412 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
414 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
416 return container_of(drv, struct spi_mem_driver, spidrv.driver);
419 static int spi_mem_probe(struct spi_device *spi)
421 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
422 struct spi_controller *ctlr = spi->controller;
425 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
431 if (ctlr->mem_ops && ctlr->mem_ops->get_name)
432 mem->name = ctlr->mem_ops->get_name(mem);
434 mem->name = dev_name(&spi->dev);
436 if (IS_ERR_OR_NULL(mem->name))
437 return PTR_ERR(mem->name);
439 spi_set_drvdata(spi, mem);
441 return memdrv->probe(mem);
444 static int spi_mem_remove(struct spi_device *spi)
446 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
447 struct spi_mem *mem = spi_get_drvdata(spi);
450 return memdrv->remove(mem);
455 static void spi_mem_shutdown(struct spi_device *spi)
457 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
458 struct spi_mem *mem = spi_get_drvdata(spi);
460 if (memdrv->shutdown)
461 memdrv->shutdown(mem);
465 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
466 * @memdrv: the SPI memory driver to register
467 * @owner: the owner of this driver
469 * Registers a SPI memory driver.
471 * Return: 0 in case of success, a negative error core otherwise.
474 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
475 struct module *owner)
477 memdrv->spidrv.probe = spi_mem_probe;
478 memdrv->spidrv.remove = spi_mem_remove;
479 memdrv->spidrv.shutdown = spi_mem_shutdown;
481 return __spi_register_driver(owner, &memdrv->spidrv);
483 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
486 * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
487 * @memdrv: the SPI memory driver to unregister
489 * Unregisters a SPI memory driver.
491 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
493 spi_unregister_driver(&memdrv->spidrv);
495 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);