spi: spi-mem: allow specifying whether an op is DTR or not
[platform/kernel/u-boot.git] / drivers / spi / spi-mem.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Exceet Electronics GmbH
4  * Copyright (C) 2018 Bootlin
5  *
6  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
7  */
8
9 #ifndef __UBOOT__
10 #include <log.h>
11 #include <dm/devres.h>
12 #include <linux/dmaengine.h>
13 #include <linux/pm_runtime.h>
14 #include "internals.h"
15 #else
16 #include <common.h>
17 #include <dm.h>
18 #include <errno.h>
19 #include <malloc.h>
20 #include <spi.h>
21 #include <spi.h>
22 #include <spi-mem.h>
23 #include <dm/device_compat.h>
24 #endif
25
26 #ifndef __UBOOT__
27 /**
28  * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
29  *                                        memory operation
30  * @ctlr: the SPI controller requesting this dma_map()
31  * @op: the memory operation containing the buffer to map
32  * @sgt: a pointer to a non-initialized sg_table that will be filled by this
33  *       function
34  *
35  * Some controllers might want to do DMA on the data buffer embedded in @op.
36  * This helper prepares everything for you and provides a ready-to-use
37  * sg_table. This function is not intended to be called from spi drivers.
38  * Only SPI controller drivers should use it.
39  * Note that the caller must ensure the memory region pointed by
40  * op->data.buf.{in,out} is DMA-able before calling this function.
41  *
42  * Return: 0 in case of success, a negative error code otherwise.
43  */
44 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
45                                        const struct spi_mem_op *op,
46                                        struct sg_table *sgt)
47 {
48         struct device *dmadev;
49
50         if (!op->data.nbytes)
51                 return -EINVAL;
52
53         if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
54                 dmadev = ctlr->dma_tx->device->dev;
55         else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
56                 dmadev = ctlr->dma_rx->device->dev;
57         else
58                 dmadev = ctlr->dev.parent;
59
60         if (!dmadev)
61                 return -EINVAL;
62
63         return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
64                            op->data.dir == SPI_MEM_DATA_IN ?
65                            DMA_FROM_DEVICE : DMA_TO_DEVICE);
66 }
67 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
68
69 /**
70  * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
71  *                                          memory operation
72  * @ctlr: the SPI controller requesting this dma_unmap()
73  * @op: the memory operation containing the buffer to unmap
74  * @sgt: a pointer to an sg_table previously initialized by
75  *       spi_controller_dma_map_mem_op_data()
76  *
77  * Some controllers might want to do DMA on the data buffer embedded in @op.
78  * This helper prepares things so that the CPU can access the
79  * op->data.buf.{in,out} buffer again.
80  *
81  * This function is not intended to be called from SPI drivers. Only SPI
82  * controller drivers should use it.
83  *
84  * This function should be called after the DMA operation has finished and is
85  * only valid if the previous spi_controller_dma_map_mem_op_data() call
86  * returned 0.
87  *
88  * Return: 0 in case of success, a negative error code otherwise.
89  */
90 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
91                                           const struct spi_mem_op *op,
92                                           struct sg_table *sgt)
93 {
94         struct device *dmadev;
95
96         if (!op->data.nbytes)
97                 return;
98
99         if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
100                 dmadev = ctlr->dma_tx->device->dev;
101         else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
102                 dmadev = ctlr->dma_rx->device->dev;
103         else
104                 dmadev = ctlr->dev.parent;
105
106         spi_unmap_buf(ctlr, dmadev, sgt,
107                       op->data.dir == SPI_MEM_DATA_IN ?
108                       DMA_FROM_DEVICE : DMA_TO_DEVICE);
109 }
110 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
111 #endif /* __UBOOT__ */
112
113 static int spi_check_buswidth_req(struct spi_slave *slave, u8 buswidth, bool tx)
114 {
115         u32 mode = slave->mode;
116
117         switch (buswidth) {
118         case 1:
119                 return 0;
120
121         case 2:
122                 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
123                     (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
124                         return 0;
125
126                 break;
127
128         case 4:
129                 if ((tx && (mode & SPI_TX_QUAD)) ||
130                     (!tx && (mode & SPI_RX_QUAD)))
131                         return 0;
132
133                 break;
134         case 8:
135                 if ((tx && (mode & SPI_TX_OCTAL)) ||
136                     (!tx && (mode & SPI_RX_OCTAL)))
137                         return 0;
138
139                 break;
140
141         default:
142                 break;
143         }
144
145         return -ENOTSUPP;
146 }
147
148 bool spi_mem_default_supports_op(struct spi_slave *slave,
149                                  const struct spi_mem_op *op)
150 {
151         if (spi_check_buswidth_req(slave, op->cmd.buswidth, true))
152                 return false;
153
154         if (op->addr.nbytes &&
155             spi_check_buswidth_req(slave, op->addr.buswidth, true))
156                 return false;
157
158         if (op->dummy.nbytes &&
159             spi_check_buswidth_req(slave, op->dummy.buswidth, true))
160                 return false;
161
162         if (op->data.dir != SPI_MEM_NO_DATA &&
163             spi_check_buswidth_req(slave, op->data.buswidth,
164                                    op->data.dir == SPI_MEM_DATA_OUT))
165                 return false;
166
167         if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
168                 return false;
169
170         return true;
171 }
172 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
173
174 /**
175  * spi_mem_supports_op() - Check if a memory device and the controller it is
176  *                         connected to support a specific memory operation
177  * @slave: the SPI device
178  * @op: the memory operation to check
179  *
180  * Some controllers are only supporting Single or Dual IOs, others might only
181  * support specific opcodes, or it can even be that the controller and device
182  * both support Quad IOs but the hardware prevents you from using it because
183  * only 2 IO lines are connected.
184  *
185  * This function checks whether a specific operation is supported.
186  *
187  * Return: true if @op is supported, false otherwise.
188  */
189 bool spi_mem_supports_op(struct spi_slave *slave,
190                          const struct spi_mem_op *op)
191 {
192         struct udevice *bus = slave->dev->parent;
193         struct dm_spi_ops *ops = spi_get_ops(bus);
194
195         if (ops->mem_ops && ops->mem_ops->supports_op)
196                 return ops->mem_ops->supports_op(slave, op);
197
198         return spi_mem_default_supports_op(slave, op);
199 }
200 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
201
202 /**
203  * spi_mem_exec_op() - Execute a memory operation
204  * @slave: the SPI device
205  * @op: the memory operation to execute
206  *
207  * Executes a memory operation.
208  *
209  * This function first checks that @op is supported and then tries to execute
210  * it.
211  *
212  * Return: 0 in case of success, a negative error code otherwise.
213  */
214 int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
215 {
216         struct udevice *bus = slave->dev->parent;
217         struct dm_spi_ops *ops = spi_get_ops(bus);
218         unsigned int pos = 0;
219         const u8 *tx_buf = NULL;
220         u8 *rx_buf = NULL;
221         int op_len;
222         u32 flag;
223         int ret;
224         int i;
225
226         if (!spi_mem_supports_op(slave, op))
227                 return -ENOTSUPP;
228
229         ret = spi_claim_bus(slave);
230         if (ret < 0)
231                 return ret;
232
233         if (ops->mem_ops && ops->mem_ops->exec_op) {
234 #ifndef __UBOOT__
235                 /*
236                  * Flush the message queue before executing our SPI memory
237                  * operation to prevent preemption of regular SPI transfers.
238                  */
239                 spi_flush_queue(ctlr);
240
241                 if (ctlr->auto_runtime_pm) {
242                         ret = pm_runtime_get_sync(ctlr->dev.parent);
243                         if (ret < 0) {
244                                 dev_err(&ctlr->dev,
245                                         "Failed to power device: %d\n",
246                                         ret);
247                                 return ret;
248                         }
249                 }
250
251                 mutex_lock(&ctlr->bus_lock_mutex);
252                 mutex_lock(&ctlr->io_mutex);
253 #endif
254                 ret = ops->mem_ops->exec_op(slave, op);
255
256 #ifndef __UBOOT__
257                 mutex_unlock(&ctlr->io_mutex);
258                 mutex_unlock(&ctlr->bus_lock_mutex);
259
260                 if (ctlr->auto_runtime_pm)
261                         pm_runtime_put(ctlr->dev.parent);
262 #endif
263
264                 /*
265                  * Some controllers only optimize specific paths (typically the
266                  * read path) and expect the core to use the regular SPI
267                  * interface in other cases.
268                  */
269                 if (!ret || ret != -ENOTSUPP) {
270                         spi_release_bus(slave);
271                         return ret;
272                 }
273         }
274
275 #ifndef __UBOOT__
276         tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
277                      op->dummy.nbytes;
278
279         /*
280          * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
281          * we're guaranteed that this buffer is DMA-able, as required by the
282          * SPI layer.
283          */
284         tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
285         if (!tmpbuf)
286                 return -ENOMEM;
287
288         spi_message_init(&msg);
289
290         tmpbuf[0] = op->cmd.opcode;
291         xfers[xferpos].tx_buf = tmpbuf;
292         xfers[xferpos].len = sizeof(op->cmd.opcode);
293         xfers[xferpos].tx_nbits = op->cmd.buswidth;
294         spi_message_add_tail(&xfers[xferpos], &msg);
295         xferpos++;
296         totalxferlen++;
297
298         if (op->addr.nbytes) {
299                 int i;
300
301                 for (i = 0; i < op->addr.nbytes; i++)
302                         tmpbuf[i + 1] = op->addr.val >>
303                                         (8 * (op->addr.nbytes - i - 1));
304
305                 xfers[xferpos].tx_buf = tmpbuf + 1;
306                 xfers[xferpos].len = op->addr.nbytes;
307                 xfers[xferpos].tx_nbits = op->addr.buswidth;
308                 spi_message_add_tail(&xfers[xferpos], &msg);
309                 xferpos++;
310                 totalxferlen += op->addr.nbytes;
311         }
312
313         if (op->dummy.nbytes) {
314                 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
315                 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
316                 xfers[xferpos].len = op->dummy.nbytes;
317                 xfers[xferpos].tx_nbits = op->dummy.buswidth;
318                 spi_message_add_tail(&xfers[xferpos], &msg);
319                 xferpos++;
320                 totalxferlen += op->dummy.nbytes;
321         }
322
323         if (op->data.nbytes) {
324                 if (op->data.dir == SPI_MEM_DATA_IN) {
325                         xfers[xferpos].rx_buf = op->data.buf.in;
326                         xfers[xferpos].rx_nbits = op->data.buswidth;
327                 } else {
328                         xfers[xferpos].tx_buf = op->data.buf.out;
329                         xfers[xferpos].tx_nbits = op->data.buswidth;
330                 }
331
332                 xfers[xferpos].len = op->data.nbytes;
333                 spi_message_add_tail(&xfers[xferpos], &msg);
334                 xferpos++;
335                 totalxferlen += op->data.nbytes;
336         }
337
338         ret = spi_sync(slave, &msg);
339
340         kfree(tmpbuf);
341
342         if (ret)
343                 return ret;
344
345         if (msg.actual_length != totalxferlen)
346                 return -EIO;
347 #else
348
349         if (op->data.nbytes) {
350                 if (op->data.dir == SPI_MEM_DATA_IN)
351                         rx_buf = op->data.buf.in;
352                 else
353                         tx_buf = op->data.buf.out;
354         }
355
356         op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
357
358         /*
359          * Avoid using malloc() here so that we can use this code in SPL where
360          * simple malloc may be used. That implementation does not allow free()
361          * so repeated calls to this code can exhaust the space.
362          *
363          * The value of op_len is small, since it does not include the actual
364          * data being sent, only the op-code and address. In fact, it should be
365          * possible to just use a small fixed value here instead of op_len.
366          */
367         u8 op_buf[op_len];
368
369         op_buf[pos++] = op->cmd.opcode;
370
371         if (op->addr.nbytes) {
372                 for (i = 0; i < op->addr.nbytes; i++)
373                         op_buf[pos + i] = op->addr.val >>
374                                 (8 * (op->addr.nbytes - i - 1));
375
376                 pos += op->addr.nbytes;
377         }
378
379         if (op->dummy.nbytes)
380                 memset(op_buf + pos, 0xff, op->dummy.nbytes);
381
382         /* 1st transfer: opcode + address + dummy cycles */
383         flag = SPI_XFER_BEGIN;
384         /* Make sure to set END bit if no tx or rx data messages follow */
385         if (!tx_buf && !rx_buf)
386                 flag |= SPI_XFER_END;
387
388         ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
389         if (ret)
390                 return ret;
391
392         /* 2nd transfer: rx or tx data path */
393         if (tx_buf || rx_buf) {
394                 ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
395                                rx_buf, SPI_XFER_END);
396                 if (ret)
397                         return ret;
398         }
399
400         spi_release_bus(slave);
401
402         for (i = 0; i < pos; i++)
403                 debug("%02x ", op_buf[i]);
404         debug("| [%dB %s] ",
405               tx_buf || rx_buf ? op->data.nbytes : 0,
406               tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
407         for (i = 0; i < op->data.nbytes; i++)
408                 debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
409         debug("[ret %d]\n", ret);
410
411         if (ret < 0)
412                 return ret;
413 #endif /* __UBOOT__ */
414
415         return 0;
416 }
417 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
418
419 /**
420  * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
421  *                               match controller limitations
422  * @slave: the SPI device
423  * @op: the operation to adjust
424  *
425  * Some controllers have FIFO limitations and must split a data transfer
426  * operation into multiple ones, others require a specific alignment for
427  * optimized accesses. This function allows SPI mem drivers to split a single
428  * operation into multiple sub-operations when required.
429  *
430  * Return: a negative error code if the controller can't properly adjust @op,
431  *         0 otherwise. Note that @op->data.nbytes will be updated if @op
432  *         can't be handled in a single step.
433  */
434 int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
435 {
436         struct udevice *bus = slave->dev->parent;
437         struct dm_spi_ops *ops = spi_get_ops(bus);
438
439         if (ops->mem_ops && ops->mem_ops->adjust_op_size)
440                 return ops->mem_ops->adjust_op_size(slave, op);
441
442         if (!ops->mem_ops || !ops->mem_ops->exec_op) {
443                 unsigned int len;
444
445                 len = sizeof(op->cmd.opcode) + op->addr.nbytes +
446                         op->dummy.nbytes;
447                 if (slave->max_write_size && len > slave->max_write_size)
448                         return -EINVAL;
449
450                 if (op->data.dir == SPI_MEM_DATA_IN) {
451                         if (slave->max_read_size)
452                                 op->data.nbytes = min(op->data.nbytes,
453                                               slave->max_read_size);
454                 } else if (slave->max_write_size) {
455                         op->data.nbytes = min(op->data.nbytes,
456                                               slave->max_write_size - len);
457                 }
458
459                 if (!op->data.nbytes)
460                         return -EINVAL;
461         }
462
463         return 0;
464 }
465 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
466
467 #ifndef __UBOOT__
468 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
469 {
470         return container_of(drv, struct spi_mem_driver, spidrv.driver);
471 }
472
473 static int spi_mem_probe(struct spi_device *spi)
474 {
475         struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
476         struct spi_mem *mem;
477
478         mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
479         if (!mem)
480                 return -ENOMEM;
481
482         mem->spi = spi;
483         spi_set_drvdata(spi, mem);
484
485         return memdrv->probe(mem);
486 }
487
488 static int spi_mem_remove(struct spi_device *spi)
489 {
490         struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
491         struct spi_mem *mem = spi_get_drvdata(spi);
492
493         if (memdrv->remove)
494                 return memdrv->remove(mem);
495
496         return 0;
497 }
498
499 static void spi_mem_shutdown(struct spi_device *spi)
500 {
501         struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
502         struct spi_mem *mem = spi_get_drvdata(spi);
503
504         if (memdrv->shutdown)
505                 memdrv->shutdown(mem);
506 }
507
508 /**
509  * spi_mem_driver_register_with_owner() - Register a SPI memory driver
510  * @memdrv: the SPI memory driver to register
511  * @owner: the owner of this driver
512  *
513  * Registers a SPI memory driver.
514  *
515  * Return: 0 in case of success, a negative error core otherwise.
516  */
517
518 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
519                                        struct module *owner)
520 {
521         memdrv->spidrv.probe = spi_mem_probe;
522         memdrv->spidrv.remove = spi_mem_remove;
523         memdrv->spidrv.shutdown = spi_mem_shutdown;
524
525         return __spi_register_driver(owner, &memdrv->spidrv);
526 }
527 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
528
529 /**
530  * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
531  * @memdrv: the SPI memory driver to unregister
532  *
533  * Unregisters a SPI memory driver.
534  */
535 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
536 {
537         spi_unregister_driver(&memdrv->spidrv);
538 }
539 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);
540 #endif /* __UBOOT__ */