1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
11 #include <dm/devres.h>
12 #include <linux/dmaengine.h>
13 #include <linux/pm_runtime.h>
14 #include "internals.h"
16 #include <dm/device_compat.h>
23 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
25 * @ctlr: the SPI controller requesting this dma_map()
26 * @op: the memory operation containing the buffer to map
27 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
30 * Some controllers might want to do DMA on the data buffer embedded in @op.
31 * This helper prepares everything for you and provides a ready-to-use
32 * sg_table. This function is not intended to be called from spi drivers.
33 * Only SPI controller drivers should use it.
34 * Note that the caller must ensure the memory region pointed by
35 * op->data.buf.{in,out} is DMA-able before calling this function.
37 * Return: 0 in case of success, a negative error code otherwise.
39 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
40 const struct spi_mem_op *op,
43 struct device *dmadev;
48 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
49 dmadev = ctlr->dma_tx->device->dev;
50 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
51 dmadev = ctlr->dma_rx->device->dev;
53 dmadev = ctlr->dev.parent;
58 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
59 op->data.dir == SPI_MEM_DATA_IN ?
60 DMA_FROM_DEVICE : DMA_TO_DEVICE);
62 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
65 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
67 * @ctlr: the SPI controller requesting this dma_unmap()
68 * @op: the memory operation containing the buffer to unmap
69 * @sgt: a pointer to an sg_table previously initialized by
70 * spi_controller_dma_map_mem_op_data()
72 * Some controllers might want to do DMA on the data buffer embedded in @op.
73 * This helper prepares things so that the CPU can access the
74 * op->data.buf.{in,out} buffer again.
76 * This function is not intended to be called from SPI drivers. Only SPI
77 * controller drivers should use it.
79 * This function should be called after the DMA operation has finished and is
80 * only valid if the previous spi_controller_dma_map_mem_op_data() call
83 * Return: 0 in case of success, a negative error code otherwise.
85 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
86 const struct spi_mem_op *op,
89 struct device *dmadev;
94 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
95 dmadev = ctlr->dma_tx->device->dev;
96 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
97 dmadev = ctlr->dma_rx->device->dev;
99 dmadev = ctlr->dev.parent;
101 spi_unmap_buf(ctlr, dmadev, sgt,
102 op->data.dir == SPI_MEM_DATA_IN ?
103 DMA_FROM_DEVICE : DMA_TO_DEVICE);
105 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
106 #endif /* __UBOOT__ */
108 static int spi_check_buswidth_req(struct spi_slave *slave, u8 buswidth, bool tx)
110 u32 mode = slave->mode;
117 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
118 (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
124 if ((tx && (mode & SPI_TX_QUAD)) ||
125 (!tx && (mode & SPI_RX_QUAD)))
130 if ((tx && (mode & SPI_TX_OCTAL)) ||
131 (!tx && (mode & SPI_RX_OCTAL)))
143 bool spi_mem_default_supports_op(struct spi_slave *slave,
144 const struct spi_mem_op *op)
146 if (spi_check_buswidth_req(slave, op->cmd.buswidth, true))
149 if (op->addr.nbytes &&
150 spi_check_buswidth_req(slave, op->addr.buswidth, true))
153 if (op->dummy.nbytes &&
154 spi_check_buswidth_req(slave, op->dummy.buswidth, true))
157 if (op->data.dir != SPI_MEM_NO_DATA &&
158 spi_check_buswidth_req(slave, op->data.buswidth,
159 op->data.dir == SPI_MEM_DATA_OUT))
164 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
167 * spi_mem_supports_op() - Check if a memory device and the controller it is
168 * connected to support a specific memory operation
169 * @slave: the SPI device
170 * @op: the memory operation to check
172 * Some controllers are only supporting Single or Dual IOs, others might only
173 * support specific opcodes, or it can even be that the controller and device
174 * both support Quad IOs but the hardware prevents you from using it because
175 * only 2 IO lines are connected.
177 * This function checks whether a specific operation is supported.
179 * Return: true if @op is supported, false otherwise.
181 bool spi_mem_supports_op(struct spi_slave *slave,
182 const struct spi_mem_op *op)
184 struct udevice *bus = slave->dev->parent;
185 struct dm_spi_ops *ops = spi_get_ops(bus);
187 if (ops->mem_ops && ops->mem_ops->supports_op)
188 return ops->mem_ops->supports_op(slave, op);
190 return spi_mem_default_supports_op(slave, op);
192 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
195 * spi_mem_exec_op() - Execute a memory operation
196 * @slave: the SPI device
197 * @op: the memory operation to execute
199 * Executes a memory operation.
201 * This function first checks that @op is supported and then tries to execute
204 * Return: 0 in case of success, a negative error code otherwise.
206 int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
208 struct udevice *bus = slave->dev->parent;
209 struct dm_spi_ops *ops = spi_get_ops(bus);
210 unsigned int pos = 0;
211 const u8 *tx_buf = NULL;
218 if (!spi_mem_supports_op(slave, op))
221 ret = spi_claim_bus(slave);
225 if (ops->mem_ops && ops->mem_ops->exec_op) {
228 * Flush the message queue before executing our SPI memory
229 * operation to prevent preemption of regular SPI transfers.
231 spi_flush_queue(ctlr);
233 if (ctlr->auto_runtime_pm) {
234 ret = pm_runtime_get_sync(ctlr->dev.parent);
237 "Failed to power device: %d\n",
243 mutex_lock(&ctlr->bus_lock_mutex);
244 mutex_lock(&ctlr->io_mutex);
246 ret = ops->mem_ops->exec_op(slave, op);
249 mutex_unlock(&ctlr->io_mutex);
250 mutex_unlock(&ctlr->bus_lock_mutex);
252 if (ctlr->auto_runtime_pm)
253 pm_runtime_put(ctlr->dev.parent);
257 * Some controllers only optimize specific paths (typically the
258 * read path) and expect the core to use the regular SPI
259 * interface in other cases.
261 if (!ret || ret != -ENOTSUPP) {
262 spi_release_bus(slave);
268 tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
272 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
273 * we're guaranteed that this buffer is DMA-able, as required by the
276 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
280 spi_message_init(&msg);
282 tmpbuf[0] = op->cmd.opcode;
283 xfers[xferpos].tx_buf = tmpbuf;
284 xfers[xferpos].len = sizeof(op->cmd.opcode);
285 xfers[xferpos].tx_nbits = op->cmd.buswidth;
286 spi_message_add_tail(&xfers[xferpos], &msg);
290 if (op->addr.nbytes) {
293 for (i = 0; i < op->addr.nbytes; i++)
294 tmpbuf[i + 1] = op->addr.val >>
295 (8 * (op->addr.nbytes - i - 1));
297 xfers[xferpos].tx_buf = tmpbuf + 1;
298 xfers[xferpos].len = op->addr.nbytes;
299 xfers[xferpos].tx_nbits = op->addr.buswidth;
300 spi_message_add_tail(&xfers[xferpos], &msg);
302 totalxferlen += op->addr.nbytes;
305 if (op->dummy.nbytes) {
306 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
307 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
308 xfers[xferpos].len = op->dummy.nbytes;
309 xfers[xferpos].tx_nbits = op->dummy.buswidth;
310 spi_message_add_tail(&xfers[xferpos], &msg);
312 totalxferlen += op->dummy.nbytes;
315 if (op->data.nbytes) {
316 if (op->data.dir == SPI_MEM_DATA_IN) {
317 xfers[xferpos].rx_buf = op->data.buf.in;
318 xfers[xferpos].rx_nbits = op->data.buswidth;
320 xfers[xferpos].tx_buf = op->data.buf.out;
321 xfers[xferpos].tx_nbits = op->data.buswidth;
324 xfers[xferpos].len = op->data.nbytes;
325 spi_message_add_tail(&xfers[xferpos], &msg);
327 totalxferlen += op->data.nbytes;
330 ret = spi_sync(slave, &msg);
337 if (msg.actual_length != totalxferlen)
341 if (op->data.nbytes) {
342 if (op->data.dir == SPI_MEM_DATA_IN)
343 rx_buf = op->data.buf.in;
345 tx_buf = op->data.buf.out;
348 op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
351 * Avoid using malloc() here so that we can use this code in SPL where
352 * simple malloc may be used. That implementation does not allow free()
353 * so repeated calls to this code can exhaust the space.
355 * The value of op_len is small, since it does not include the actual
356 * data being sent, only the op-code and address. In fact, it should be
357 * possible to just use a small fixed value here instead of op_len.
361 op_buf[pos++] = op->cmd.opcode;
363 if (op->addr.nbytes) {
364 for (i = 0; i < op->addr.nbytes; i++)
365 op_buf[pos + i] = op->addr.val >>
366 (8 * (op->addr.nbytes - i - 1));
368 pos += op->addr.nbytes;
371 if (op->dummy.nbytes)
372 memset(op_buf + pos, 0xff, op->dummy.nbytes);
374 /* 1st transfer: opcode + address + dummy cycles */
375 flag = SPI_XFER_BEGIN;
376 /* Make sure to set END bit if no tx or rx data messages follow */
377 if (!tx_buf && !rx_buf)
378 flag |= SPI_XFER_END;
380 ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
384 /* 2nd transfer: rx or tx data path */
385 if (tx_buf || rx_buf) {
386 ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
387 rx_buf, SPI_XFER_END);
392 spi_release_bus(slave);
394 for (i = 0; i < pos; i++)
395 debug("%02x ", op_buf[i]);
397 tx_buf || rx_buf ? op->data.nbytes : 0,
398 tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
399 for (i = 0; i < op->data.nbytes; i++)
400 debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
401 debug("[ret %d]\n", ret);
405 #endif /* __UBOOT__ */
409 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
412 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
413 * match controller limitations
414 * @slave: the SPI device
415 * @op: the operation to adjust
417 * Some controllers have FIFO limitations and must split a data transfer
418 * operation into multiple ones, others require a specific alignment for
419 * optimized accesses. This function allows SPI mem drivers to split a single
420 * operation into multiple sub-operations when required.
422 * Return: a negative error code if the controller can't properly adjust @op,
423 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
424 * can't be handled in a single step.
426 int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
428 struct udevice *bus = slave->dev->parent;
429 struct dm_spi_ops *ops = spi_get_ops(bus);
431 if (ops->mem_ops && ops->mem_ops->adjust_op_size)
432 return ops->mem_ops->adjust_op_size(slave, op);
434 if (!ops->mem_ops || !ops->mem_ops->exec_op) {
437 len = sizeof(op->cmd.opcode) + op->addr.nbytes +
439 if (slave->max_write_size && len > slave->max_write_size)
442 if (op->data.dir == SPI_MEM_DATA_IN) {
443 if (slave->max_read_size)
444 op->data.nbytes = min(op->data.nbytes,
445 slave->max_read_size);
446 } else if (slave->max_write_size) {
447 op->data.nbytes = min(op->data.nbytes,
448 slave->max_write_size - len);
451 if (!op->data.nbytes)
457 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
460 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
462 return container_of(drv, struct spi_mem_driver, spidrv.driver);
465 static int spi_mem_probe(struct spi_device *spi)
467 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
470 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
475 spi_set_drvdata(spi, mem);
477 return memdrv->probe(mem);
480 static int spi_mem_remove(struct spi_device *spi)
482 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
483 struct spi_mem *mem = spi_get_drvdata(spi);
486 return memdrv->remove(mem);
491 static void spi_mem_shutdown(struct spi_device *spi)
493 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
494 struct spi_mem *mem = spi_get_drvdata(spi);
496 if (memdrv->shutdown)
497 memdrv->shutdown(mem);
501 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
502 * @memdrv: the SPI memory driver to register
503 * @owner: the owner of this driver
505 * Registers a SPI memory driver.
507 * Return: 0 in case of success, a negative error core otherwise.
510 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
511 struct module *owner)
513 memdrv->spidrv.probe = spi_mem_probe;
514 memdrv->spidrv.remove = spi_mem_remove;
515 memdrv->spidrv.shutdown = spi_mem_shutdown;
517 return __spi_register_driver(owner, &memdrv->spidrv);
519 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
522 * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
523 * @memdrv: the SPI memory driver to unregister
525 * Unregisters a SPI memory driver.
527 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
529 spi_unregister_driver(&memdrv->spidrv);
531 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);
532 #endif /* __UBOOT__ */