1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
6 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
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"
16 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
18 * @ctlr: the SPI controller requesting this dma_map()
19 * @op: the memory operation containing the buffer to map
20 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
23 * Some controllers might want to do DMA on the data buffer embedded in @op.
24 * This helper prepares everything for you and provides a ready-to-use
25 * sg_table. This function is not intended to be called from spi drivers.
26 * Only SPI controller drivers should use it.
27 * Note that the caller must ensure the memory region pointed by
28 * op->data.buf.{in,out} is DMA-able before calling this function.
30 * Return: 0 in case of success, a negative error code otherwise.
32 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
33 const struct spi_mem_op *op,
36 struct device *dmadev;
41 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
42 dmadev = ctlr->dma_tx->device->dev;
43 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
44 dmadev = ctlr->dma_rx->device->dev;
46 dmadev = ctlr->dev.parent;
51 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
52 op->data.dir == SPI_MEM_DATA_IN ?
53 DMA_FROM_DEVICE : DMA_TO_DEVICE);
55 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
58 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
60 * @ctlr: the SPI controller requesting this dma_unmap()
61 * @op: the memory operation containing the buffer to unmap
62 * @sgt: a pointer to an sg_table previously initialized by
63 * spi_controller_dma_map_mem_op_data()
65 * Some controllers might want to do DMA on the data buffer embedded in @op.
66 * This helper prepares things so that the CPU can access the
67 * op->data.buf.{in,out} buffer again.
69 * This function is not intended to be called from SPI drivers. Only SPI
70 * controller drivers should use it.
72 * This function should be called after the DMA operation has finished and is
73 * only valid if the previous spi_controller_dma_map_mem_op_data() call
76 * Return: 0 in case of success, a negative error code otherwise.
78 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
79 const struct spi_mem_op *op,
82 struct device *dmadev;
87 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
88 dmadev = ctlr->dma_tx->device->dev;
89 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
90 dmadev = ctlr->dma_rx->device->dev;
92 dmadev = ctlr->dev.parent;
94 spi_unmap_buf(ctlr, dmadev, sgt,
95 op->data.dir == SPI_MEM_DATA_IN ?
96 DMA_FROM_DEVICE : DMA_TO_DEVICE);
98 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
100 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
102 u32 mode = mem->spi->mode;
109 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
110 (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
116 if ((tx && (mode & SPI_TX_QUAD)) ||
117 (!tx && (mode & SPI_RX_QUAD)))
129 static bool spi_mem_default_supports_op(struct spi_mem *mem,
130 const struct spi_mem_op *op)
132 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
135 if (op->addr.nbytes &&
136 spi_check_buswidth_req(mem, op->addr.buswidth, true))
139 if (op->dummy.nbytes &&
140 spi_check_buswidth_req(mem, op->dummy.buswidth, true))
143 if (op->data.nbytes &&
144 spi_check_buswidth_req(mem, op->data.buswidth,
145 op->data.dir == SPI_MEM_DATA_OUT))
150 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
153 * spi_mem_supports_op() - Check if a memory device and the controller it is
154 * connected to support a specific memory operation
155 * @mem: the SPI memory
156 * @op: the memory operation to check
158 * Some controllers are only supporting Single or Dual IOs, others might only
159 * support specific opcodes, or it can even be that the controller and device
160 * both support Quad IOs but the hardware prevents you from using it because
161 * only 2 IO lines are connected.
163 * This function checks whether a specific operation is supported.
165 * Return: true if @op is supported, false otherwise.
167 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
169 struct spi_controller *ctlr = mem->spi->controller;
171 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
172 return ctlr->mem_ops->supports_op(mem, op);
174 return spi_mem_default_supports_op(mem, op);
176 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
179 * spi_mem_exec_op() - Execute a memory operation
180 * @mem: the SPI memory
181 * @op: the memory operation to execute
183 * Executes a memory operation.
185 * This function first checks that @op is supported and then tries to execute
188 * Return: 0 in case of success, a negative error code otherwise.
190 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
192 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
193 struct spi_controller *ctlr = mem->spi->controller;
194 struct spi_transfer xfers[4] = { };
195 struct spi_message msg;
199 if (!spi_mem_supports_op(mem, op))
204 * Flush the message queue before executing our SPI memory
205 * operation to prevent preemption of regular SPI transfers.
207 spi_flush_queue(ctlr);
209 if (ctlr->auto_runtime_pm) {
210 ret = pm_runtime_get_sync(ctlr->dev.parent);
213 "Failed to power device: %d\n",
219 mutex_lock(&ctlr->bus_lock_mutex);
220 mutex_lock(&ctlr->io_mutex);
221 ret = ctlr->mem_ops->exec_op(mem, op);
222 mutex_unlock(&ctlr->io_mutex);
223 mutex_unlock(&ctlr->bus_lock_mutex);
225 if (ctlr->auto_runtime_pm)
226 pm_runtime_put(ctlr->dev.parent);
229 * Some controllers only optimize specific paths (typically the
230 * read path) and expect the core to use the regular SPI
231 * interface in other cases.
233 if (!ret || ret != -ENOTSUPP)
237 tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
241 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
242 * we're guaranteed that this buffer is DMA-able, as required by the
245 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
249 spi_message_init(&msg);
251 tmpbuf[0] = op->cmd.opcode;
252 xfers[xferpos].tx_buf = tmpbuf;
253 xfers[xferpos].len = sizeof(op->cmd.opcode);
254 xfers[xferpos].tx_nbits = op->cmd.buswidth;
255 spi_message_add_tail(&xfers[xferpos], &msg);
259 if (op->addr.nbytes) {
262 for (i = 0; i < op->addr.nbytes; i++)
263 tmpbuf[i + 1] = op->addr.val >>
264 (8 * (op->addr.nbytes - i - 1));
266 xfers[xferpos].tx_buf = tmpbuf + 1;
267 xfers[xferpos].len = op->addr.nbytes;
268 xfers[xferpos].tx_nbits = op->addr.buswidth;
269 spi_message_add_tail(&xfers[xferpos], &msg);
271 totalxferlen += op->addr.nbytes;
274 if (op->dummy.nbytes) {
275 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
276 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
277 xfers[xferpos].len = op->dummy.nbytes;
278 xfers[xferpos].tx_nbits = op->dummy.buswidth;
279 spi_message_add_tail(&xfers[xferpos], &msg);
281 totalxferlen += op->dummy.nbytes;
284 if (op->data.nbytes) {
285 if (op->data.dir == SPI_MEM_DATA_IN) {
286 xfers[xferpos].rx_buf = op->data.buf.in;
287 xfers[xferpos].rx_nbits = op->data.buswidth;
289 xfers[xferpos].tx_buf = op->data.buf.out;
290 xfers[xferpos].tx_nbits = op->data.buswidth;
293 xfers[xferpos].len = op->data.nbytes;
294 spi_message_add_tail(&xfers[xferpos], &msg);
296 totalxferlen += op->data.nbytes;
299 ret = spi_sync(mem->spi, &msg);
306 if (msg.actual_length != totalxferlen)
311 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
314 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
315 * match controller limitations
316 * @mem: the SPI memory
317 * @op: the operation to adjust
319 * Some controllers have FIFO limitations and must split a data transfer
320 * operation into multiple ones, others require a specific alignment for
321 * optimized accesses. This function allows SPI mem drivers to split a single
322 * operation into multiple sub-operations when required.
324 * Return: a negative error code if the controller can't properly adjust @op,
325 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
326 * can't be handled in a single step.
328 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
330 struct spi_controller *ctlr = mem->spi->controller;
332 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
333 return ctlr->mem_ops->adjust_op_size(mem, op);
337 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
339 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
341 return container_of(drv, struct spi_mem_driver, spidrv.driver);
344 static int spi_mem_probe(struct spi_device *spi)
346 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
349 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
354 spi_set_drvdata(spi, mem);
356 return memdrv->probe(mem);
359 static int spi_mem_remove(struct spi_device *spi)
361 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
362 struct spi_mem *mem = spi_get_drvdata(spi);
365 return memdrv->remove(mem);
370 static void spi_mem_shutdown(struct spi_device *spi)
372 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
373 struct spi_mem *mem = spi_get_drvdata(spi);
375 if (memdrv->shutdown)
376 memdrv->shutdown(mem);
380 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
381 * @memdrv: the SPI memory driver to register
382 * @owner: the owner of this driver
384 * Registers a SPI memory driver.
386 * Return: 0 in case of success, a negative error core otherwise.
389 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
390 struct module *owner)
392 memdrv->spidrv.probe = spi_mem_probe;
393 memdrv->spidrv.remove = spi_mem_remove;
394 memdrv->spidrv.shutdown = spi_mem_shutdown;
396 return __spi_register_driver(owner, &memdrv->spidrv);
398 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
401 * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
402 * @memdrv: the SPI memory driver to unregister
404 * Unregisters a SPI memory driver.
406 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
408 spi_unregister_driver(&memdrv->spidrv);
410 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);