global: Move remaining CONFIG_SYS_* to CFG_SYS_*
[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 #include <dm/devres.h>
25 #include <linux/bug.h>
26 #endif
27
28 #ifndef __UBOOT__
29 /**
30  * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
31  *                                        memory operation
32  * @ctlr: the SPI controller requesting this dma_map()
33  * @op: the memory operation containing the buffer to map
34  * @sgt: a pointer to a non-initialized sg_table that will be filled by this
35  *       function
36  *
37  * Some controllers might want to do DMA on the data buffer embedded in @op.
38  * This helper prepares everything for you and provides a ready-to-use
39  * sg_table. This function is not intended to be called from spi drivers.
40  * Only SPI controller drivers should use it.
41  * Note that the caller must ensure the memory region pointed by
42  * op->data.buf.{in,out} is DMA-able before calling this function.
43  *
44  * Return: 0 in case of success, a negative error code otherwise.
45  */
46 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
47                                        const struct spi_mem_op *op,
48                                        struct sg_table *sgt)
49 {
50         struct device *dmadev;
51
52         if (!op->data.nbytes)
53                 return -EINVAL;
54
55         if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
56                 dmadev = ctlr->dma_tx->device->dev;
57         else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
58                 dmadev = ctlr->dma_rx->device->dev;
59         else
60                 dmadev = ctlr->dev.parent;
61
62         if (!dmadev)
63                 return -EINVAL;
64
65         return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
66                            op->data.dir == SPI_MEM_DATA_IN ?
67                            DMA_FROM_DEVICE : DMA_TO_DEVICE);
68 }
69 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
70
71 /**
72  * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
73  *                                          memory operation
74  * @ctlr: the SPI controller requesting this dma_unmap()
75  * @op: the memory operation containing the buffer to unmap
76  * @sgt: a pointer to an sg_table previously initialized by
77  *       spi_controller_dma_map_mem_op_data()
78  *
79  * Some controllers might want to do DMA on the data buffer embedded in @op.
80  * This helper prepares things so that the CPU can access the
81  * op->data.buf.{in,out} buffer again.
82  *
83  * This function is not intended to be called from SPI drivers. Only SPI
84  * controller drivers should use it.
85  *
86  * This function should be called after the DMA operation has finished and is
87  * only valid if the previous spi_controller_dma_map_mem_op_data() call
88  * returned 0.
89  *
90  * Return: 0 in case of success, a negative error code otherwise.
91  */
92 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
93                                           const struct spi_mem_op *op,
94                                           struct sg_table *sgt)
95 {
96         struct device *dmadev;
97
98         if (!op->data.nbytes)
99                 return;
100
101         if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
102                 dmadev = ctlr->dma_tx->device->dev;
103         else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
104                 dmadev = ctlr->dma_rx->device->dev;
105         else
106                 dmadev = ctlr->dev.parent;
107
108         spi_unmap_buf(ctlr, dmadev, sgt,
109                       op->data.dir == SPI_MEM_DATA_IN ?
110                       DMA_FROM_DEVICE : DMA_TO_DEVICE);
111 }
112 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
113 #endif /* __UBOOT__ */
114
115 static int spi_check_buswidth_req(struct spi_slave *slave, u8 buswidth, bool tx)
116 {
117         u32 mode = slave->mode;
118
119         switch (buswidth) {
120         case 1:
121                 return 0;
122
123         case 2:
124                 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
125                     (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
126                         return 0;
127
128                 break;
129
130         case 4:
131                 if ((tx && (mode & SPI_TX_QUAD)) ||
132                     (!tx && (mode & SPI_RX_QUAD)))
133                         return 0;
134
135                 break;
136         case 8:
137                 if ((tx && (mode & SPI_TX_OCTAL)) ||
138                     (!tx && (mode & SPI_RX_OCTAL)))
139                         return 0;
140
141                 break;
142
143         default:
144                 break;
145         }
146
147         return -ENOTSUPP;
148 }
149
150 static bool spi_mem_check_buswidth(struct spi_slave *slave,
151                                    const struct spi_mem_op *op)
152 {
153         if (spi_check_buswidth_req(slave, op->cmd.buswidth, true))
154                 return false;
155
156         if (op->addr.nbytes &&
157             spi_check_buswidth_req(slave, op->addr.buswidth, true))
158                 return false;
159
160         if (op->dummy.nbytes &&
161             spi_check_buswidth_req(slave, op->dummy.buswidth, true))
162                 return false;
163
164         if (op->data.dir != SPI_MEM_NO_DATA &&
165             spi_check_buswidth_req(slave, op->data.buswidth,
166                                    op->data.dir == SPI_MEM_DATA_OUT))
167                 return false;
168
169         return true;
170 }
171
172 bool spi_mem_dtr_supports_op(struct spi_slave *slave,
173                              const struct spi_mem_op *op)
174 {
175         if (op->cmd.buswidth == 8 && op->cmd.nbytes % 2)
176                 return false;
177
178         if (op->addr.nbytes && op->addr.buswidth == 8 && op->addr.nbytes % 2)
179                 return false;
180
181         if (op->dummy.nbytes && op->dummy.buswidth == 8 && op->dummy.nbytes % 2)
182                 return false;
183
184         if (op->data.dir != SPI_MEM_NO_DATA &&
185             op->dummy.buswidth == 8 && op->data.nbytes % 2)
186                 return false;
187
188         return spi_mem_check_buswidth(slave, op);
189 }
190 EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
191
192 bool spi_mem_default_supports_op(struct spi_slave *slave,
193                                  const struct spi_mem_op *op)
194 {
195         if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
196                 return false;
197
198         if (op->cmd.nbytes != 1)
199                 return false;
200
201         return spi_mem_check_buswidth(slave, op);
202 }
203 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
204
205 /**
206  * spi_mem_supports_op() - Check if a memory device and the controller it is
207  *                         connected to support a specific memory operation
208  * @slave: the SPI device
209  * @op: the memory operation to check
210  *
211  * Some controllers are only supporting Single or Dual IOs, others might only
212  * support specific opcodes, or it can even be that the controller and device
213  * both support Quad IOs but the hardware prevents you from using it because
214  * only 2 IO lines are connected.
215  *
216  * This function checks whether a specific operation is supported.
217  *
218  * Return: true if @op is supported, false otherwise.
219  */
220 bool spi_mem_supports_op(struct spi_slave *slave,
221                          const struct spi_mem_op *op)
222 {
223         struct udevice *bus = slave->dev->parent;
224         struct dm_spi_ops *ops = spi_get_ops(bus);
225
226         if (ops->mem_ops && ops->mem_ops->supports_op)
227                 return ops->mem_ops->supports_op(slave, op);
228
229         return spi_mem_default_supports_op(slave, op);
230 }
231 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
232
233 /**
234  * spi_mem_exec_op() - Execute a memory operation
235  * @slave: the SPI device
236  * @op: the memory operation to execute
237  *
238  * Executes a memory operation.
239  *
240  * This function first checks that @op is supported and then tries to execute
241  * it.
242  *
243  * Return: 0 in case of success, a negative error code otherwise.
244  */
245 int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
246 {
247         struct udevice *bus = slave->dev->parent;
248         struct dm_spi_ops *ops = spi_get_ops(bus);
249         unsigned int pos = 0;
250         const u8 *tx_buf = NULL;
251         u8 *rx_buf = NULL;
252         int op_len;
253         u32 flag;
254         int ret;
255         int i;
256
257         if (!spi_mem_supports_op(slave, op))
258                 return -ENOTSUPP;
259
260         ret = spi_claim_bus(slave);
261         if (ret < 0)
262                 return ret;
263
264         if (ops->mem_ops && ops->mem_ops->exec_op) {
265 #ifndef __UBOOT__
266                 /*
267                  * Flush the message queue before executing our SPI memory
268                  * operation to prevent preemption of regular SPI transfers.
269                  */
270                 spi_flush_queue(ctlr);
271
272                 if (ctlr->auto_runtime_pm) {
273                         ret = pm_runtime_get_sync(ctlr->dev.parent);
274                         if (ret < 0) {
275                                 dev_err(&ctlr->dev,
276                                         "Failed to power device: %d\n",
277                                         ret);
278                                 return ret;
279                         }
280                 }
281
282                 mutex_lock(&ctlr->bus_lock_mutex);
283                 mutex_lock(&ctlr->io_mutex);
284 #endif
285                 ret = ops->mem_ops->exec_op(slave, op);
286
287 #ifndef __UBOOT__
288                 mutex_unlock(&ctlr->io_mutex);
289                 mutex_unlock(&ctlr->bus_lock_mutex);
290
291                 if (ctlr->auto_runtime_pm)
292                         pm_runtime_put(ctlr->dev.parent);
293 #endif
294
295                 /*
296                  * Some controllers only optimize specific paths (typically the
297                  * read path) and expect the core to use the regular SPI
298                  * interface in other cases.
299                  */
300                 if (!ret || ret != -ENOTSUPP) {
301                         spi_release_bus(slave);
302                         return ret;
303                 }
304         }
305
306 #ifndef __UBOOT__
307         tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
308
309         /*
310          * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
311          * we're guaranteed that this buffer is DMA-able, as required by the
312          * SPI layer.
313          */
314         tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
315         if (!tmpbuf)
316                 return -ENOMEM;
317
318         spi_message_init(&msg);
319
320         tmpbuf[0] = op->cmd.opcode;
321         xfers[xferpos].tx_buf = tmpbuf;
322         xfers[xferpos].len = op->cmd.nbytes;
323         xfers[xferpos].tx_nbits = op->cmd.buswidth;
324         spi_message_add_tail(&xfers[xferpos], &msg);
325         xferpos++;
326         totalxferlen++;
327
328         if (op->addr.nbytes) {
329                 int i;
330
331                 for (i = 0; i < op->addr.nbytes; i++)
332                         tmpbuf[i + 1] = op->addr.val >>
333                                         (8 * (op->addr.nbytes - i - 1));
334
335                 xfers[xferpos].tx_buf = tmpbuf + 1;
336                 xfers[xferpos].len = op->addr.nbytes;
337                 xfers[xferpos].tx_nbits = op->addr.buswidth;
338                 spi_message_add_tail(&xfers[xferpos], &msg);
339                 xferpos++;
340                 totalxferlen += op->addr.nbytes;
341         }
342
343         if (op->dummy.nbytes) {
344                 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
345                 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
346                 xfers[xferpos].len = op->dummy.nbytes;
347                 xfers[xferpos].tx_nbits = op->dummy.buswidth;
348                 spi_message_add_tail(&xfers[xferpos], &msg);
349                 xferpos++;
350                 totalxferlen += op->dummy.nbytes;
351         }
352
353         if (op->data.nbytes) {
354                 if (op->data.dir == SPI_MEM_DATA_IN) {
355                         xfers[xferpos].rx_buf = op->data.buf.in;
356                         xfers[xferpos].rx_nbits = op->data.buswidth;
357                 } else {
358                         xfers[xferpos].tx_buf = op->data.buf.out;
359                         xfers[xferpos].tx_nbits = op->data.buswidth;
360                 }
361
362                 xfers[xferpos].len = op->data.nbytes;
363                 spi_message_add_tail(&xfers[xferpos], &msg);
364                 xferpos++;
365                 totalxferlen += op->data.nbytes;
366         }
367
368         ret = spi_sync(slave, &msg);
369
370         kfree(tmpbuf);
371
372         if (ret)
373                 return ret;
374
375         if (msg.actual_length != totalxferlen)
376                 return -EIO;
377 #else
378
379         if (op->data.nbytes) {
380                 if (op->data.dir == SPI_MEM_DATA_IN)
381                         rx_buf = op->data.buf.in;
382                 else
383                         tx_buf = op->data.buf.out;
384         }
385
386         op_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
387
388         /*
389          * Avoid using malloc() here so that we can use this code in SPL where
390          * simple malloc may be used. That implementation does not allow free()
391          * so repeated calls to this code can exhaust the space.
392          *
393          * The value of op_len is small, since it does not include the actual
394          * data being sent, only the op-code and address. In fact, it should be
395          * possible to just use a small fixed value here instead of op_len.
396          */
397         u8 op_buf[op_len];
398
399         op_buf[pos++] = op->cmd.opcode;
400
401         if (op->addr.nbytes) {
402                 for (i = 0; i < op->addr.nbytes; i++)
403                         op_buf[pos + i] = op->addr.val >>
404                                 (8 * (op->addr.nbytes - i - 1));
405
406                 pos += op->addr.nbytes;
407         }
408
409         if (op->dummy.nbytes)
410                 memset(op_buf + pos, 0xff, op->dummy.nbytes);
411
412         /* 1st transfer: opcode + address + dummy cycles */
413         flag = SPI_XFER_BEGIN;
414         /* Make sure to set END bit if no tx or rx data messages follow */
415         if (!tx_buf && !rx_buf)
416                 flag |= SPI_XFER_END;
417
418         ret = spi_xfer(slave, op_len * 8, op_buf, NULL, flag);
419         if (ret)
420                 return ret;
421
422         /* 2nd transfer: rx or tx data path */
423         if (tx_buf || rx_buf) {
424                 ret = spi_xfer(slave, op->data.nbytes * 8, tx_buf,
425                                rx_buf, SPI_XFER_END);
426                 if (ret)
427                         return ret;
428         }
429
430         spi_release_bus(slave);
431
432         for (i = 0; i < pos; i++)
433                 debug("%02x ", op_buf[i]);
434         debug("| [%dB %s] ",
435               tx_buf || rx_buf ? op->data.nbytes : 0,
436               tx_buf || rx_buf ? (tx_buf ? "out" : "in") : "-");
437         for (i = 0; i < op->data.nbytes; i++)
438                 debug("%02x ", tx_buf ? tx_buf[i] : rx_buf[i]);
439         debug("[ret %d]\n", ret);
440
441         if (ret < 0)
442                 return ret;
443 #endif /* __UBOOT__ */
444
445         return 0;
446 }
447 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
448
449 /**
450  * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
451  *                               match controller limitations
452  * @slave: the SPI device
453  * @op: the operation to adjust
454  *
455  * Some controllers have FIFO limitations and must split a data transfer
456  * operation into multiple ones, others require a specific alignment for
457  * optimized accesses. This function allows SPI mem drivers to split a single
458  * operation into multiple sub-operations when required.
459  *
460  * Return: a negative error code if the controller can't properly adjust @op,
461  *         0 otherwise. Note that @op->data.nbytes will be updated if @op
462  *         can't be handled in a single step.
463  */
464 int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
465 {
466         struct udevice *bus = slave->dev->parent;
467         struct dm_spi_ops *ops = spi_get_ops(bus);
468
469         if (ops->mem_ops && ops->mem_ops->adjust_op_size)
470                 return ops->mem_ops->adjust_op_size(slave, op);
471
472         if (!ops->mem_ops || !ops->mem_ops->exec_op) {
473                 unsigned int len;
474
475                 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
476                 if (slave->max_write_size && len > slave->max_write_size)
477                         return -EINVAL;
478
479                 if (op->data.dir == SPI_MEM_DATA_IN) {
480                         if (slave->max_read_size)
481                                 op->data.nbytes = min(op->data.nbytes,
482                                               slave->max_read_size);
483                 } else if (slave->max_write_size) {
484                         op->data.nbytes = min(op->data.nbytes,
485                                               slave->max_write_size - len);
486                 }
487
488                 if (!op->data.nbytes)
489                         return -EINVAL;
490         }
491
492         return 0;
493 }
494 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
495
496 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
497                                       u64 offs, size_t len, void *buf)
498 {
499         struct spi_mem_op op = desc->info.op_tmpl;
500         int ret;
501
502         op.addr.val = desc->info.offset + offs;
503         op.data.buf.in = buf;
504         op.data.nbytes = len;
505         ret = spi_mem_adjust_op_size(desc->slave, &op);
506         if (ret)
507                 return ret;
508
509         ret = spi_mem_exec_op(desc->slave, &op);
510         if (ret)
511                 return ret;
512
513         return op.data.nbytes;
514 }
515
516 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
517                                        u64 offs, size_t len, const void *buf)
518 {
519         struct spi_mem_op op = desc->info.op_tmpl;
520         int ret;
521
522         op.addr.val = desc->info.offset + offs;
523         op.data.buf.out = buf;
524         op.data.nbytes = len;
525         ret = spi_mem_adjust_op_size(desc->slave, &op);
526         if (ret)
527                 return ret;
528
529         ret = spi_mem_exec_op(desc->slave, &op);
530         if (ret)
531                 return ret;
532
533         return op.data.nbytes;
534 }
535
536 /**
537  * spi_mem_dirmap_create() - Create a direct mapping descriptor
538  * @mem: SPI mem device this direct mapping should be created for
539  * @info: direct mapping information
540  *
541  * This function is creating a direct mapping descriptor which can then be used
542  * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write().
543  * If the SPI controller driver does not support direct mapping, this function
544  * falls back to an implementation using spi_mem_exec_op(), so that the caller
545  * doesn't have to bother implementing a fallback on his own.
546  *
547  * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
548  */
549 struct spi_mem_dirmap_desc *
550 spi_mem_dirmap_create(struct spi_slave *slave,
551                       const struct spi_mem_dirmap_info *info)
552 {
553         struct udevice *bus = slave->dev->parent;
554         struct dm_spi_ops *ops = spi_get_ops(bus);
555         struct spi_mem_dirmap_desc *desc;
556         int ret = -EOPNOTSUPP;
557
558         /* Make sure the number of address cycles is between 1 and 8 bytes. */
559         if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
560                 return ERR_PTR(-EINVAL);
561
562         /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */
563         if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
564                 return ERR_PTR(-EINVAL);
565
566         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
567         if (!desc)
568                 return ERR_PTR(-ENOMEM);
569
570         desc->slave = slave;
571         desc->info = *info;
572         if (ops->mem_ops && ops->mem_ops->dirmap_create)
573                 ret = ops->mem_ops->dirmap_create(desc);
574
575         if (ret) {
576                 desc->nodirmap = true;
577                 if (!spi_mem_supports_op(desc->slave, &desc->info.op_tmpl))
578                         ret = -EOPNOTSUPP;
579                 else
580                         ret = 0;
581         }
582
583         if (ret) {
584                 kfree(desc);
585                 return ERR_PTR(ret);
586         }
587
588         return desc;
589 }
590 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
591
592 /**
593  * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
594  * @desc: the direct mapping descriptor to destroy
595  *
596  * This function destroys a direct mapping descriptor previously created by
597  * spi_mem_dirmap_create().
598  */
599 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
600 {
601         struct udevice *bus = desc->slave->dev->parent;
602         struct dm_spi_ops *ops = spi_get_ops(bus);
603
604         if (!desc->nodirmap && ops->mem_ops && ops->mem_ops->dirmap_destroy)
605                 ops->mem_ops->dirmap_destroy(desc);
606
607         kfree(desc);
608 }
609 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
610
611 #ifndef __UBOOT__
612 static void devm_spi_mem_dirmap_release(struct udevice *dev, void *res)
613 {
614         struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
615
616         spi_mem_dirmap_destroy(desc);
617 }
618
619 /**
620  * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
621  *                                it to a device
622  * @dev: device the dirmap desc will be attached to
623  * @mem: SPI mem device this direct mapping should be created for
624  * @info: direct mapping information
625  *
626  * devm_ variant of the spi_mem_dirmap_create() function. See
627  * spi_mem_dirmap_create() for more details.
628  *
629  * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
630  */
631 struct spi_mem_dirmap_desc *
632 devm_spi_mem_dirmap_create(struct udevice *dev, struct spi_slave *slave,
633                            const struct spi_mem_dirmap_info *info)
634 {
635         struct spi_mem_dirmap_desc **ptr, *desc;
636
637         ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
638                            GFP_KERNEL);
639         if (!ptr)
640                 return ERR_PTR(-ENOMEM);
641
642         desc = spi_mem_dirmap_create(slave, info);
643         if (IS_ERR(desc)) {
644                 devres_free(ptr);
645         } else {
646                 *ptr = desc;
647                 devres_add(dev, ptr);
648         }
649
650         return desc;
651 }
652 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
653
654 static int devm_spi_mem_dirmap_match(struct udevice *dev, void *res, void *data)
655 {
656         struct spi_mem_dirmap_desc **ptr = res;
657
658         if (WARN_ON(!ptr || !*ptr))
659                 return 0;
660
661         return *ptr == data;
662 }
663
664 /**
665  * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
666  *                                 to a device
667  * @dev: device the dirmap desc is attached to
668  * @desc: the direct mapping descriptor to destroy
669  *
670  * devm_ variant of the spi_mem_dirmap_destroy() function. See
671  * spi_mem_dirmap_destroy() for more details.
672  */
673 void devm_spi_mem_dirmap_destroy(struct udevice *dev,
674                                  struct spi_mem_dirmap_desc *desc)
675 {
676         devres_release(dev, devm_spi_mem_dirmap_release,
677                        devm_spi_mem_dirmap_match, desc);
678 }
679 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
680 #endif /* __UBOOT__ */
681
682 /**
683  * spi_mem_dirmap_read() - Read data through a direct mapping
684  * @desc: direct mapping descriptor
685  * @offs: offset to start reading from. Note that this is not an absolute
686  *        offset, but the offset within the direct mapping which already has
687  *        its own offset
688  * @len: length in bytes
689  * @buf: destination buffer. This buffer must be DMA-able
690  *
691  * This function reads data from a memory device using a direct mapping
692  * previously instantiated with spi_mem_dirmap_create().
693  *
694  * Return: the amount of data read from the memory device or a negative error
695  * code. Note that the returned size might be smaller than @len, and the caller
696  * is responsible for calling spi_mem_dirmap_read() again when that happens.
697  */
698 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
699                             u64 offs, size_t len, void *buf)
700 {
701         struct udevice *bus = desc->slave->dev->parent;
702         struct dm_spi_ops *ops = spi_get_ops(bus);
703         ssize_t ret;
704
705         if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
706                 return -EINVAL;
707
708         if (!len)
709                 return 0;
710
711         if (desc->nodirmap)
712                 ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
713         else if (ops->mem_ops && ops->mem_ops->dirmap_read)
714                 ret = ops->mem_ops->dirmap_read(desc, offs, len, buf);
715         else
716                 ret = -EOPNOTSUPP;
717
718         return ret;
719 }
720 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
721
722 /**
723  * spi_mem_dirmap_write() - Write data through a direct mapping
724  * @desc: direct mapping descriptor
725  * @offs: offset to start writing from. Note that this is not an absolute
726  *        offset, but the offset within the direct mapping which already has
727  *        its own offset
728  * @len: length in bytes
729  * @buf: source buffer. This buffer must be DMA-able
730  *
731  * This function writes data to a memory device using a direct mapping
732  * previously instantiated with spi_mem_dirmap_create().
733  *
734  * Return: the amount of data written to the memory device or a negative error
735  * code. Note that the returned size might be smaller than @len, and the caller
736  * is responsible for calling spi_mem_dirmap_write() again when that happens.
737  */
738 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
739                              u64 offs, size_t len, const void *buf)
740 {
741         struct udevice *bus = desc->slave->dev->parent;
742         struct dm_spi_ops *ops = spi_get_ops(bus);
743         ssize_t ret;
744
745         if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
746                 return -EINVAL;
747
748         if (!len)
749                 return 0;
750
751         if (desc->nodirmap)
752                 ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
753         else if (ops->mem_ops && ops->mem_ops->dirmap_write)
754                 ret = ops->mem_ops->dirmap_write(desc, offs, len, buf);
755         else
756                 ret = -EOPNOTSUPP;
757
758         return ret;
759 }
760 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
761
762 #ifndef __UBOOT__
763 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
764 {
765         return container_of(drv, struct spi_mem_driver, spidrv.driver);
766 }
767
768 static int spi_mem_probe(struct spi_device *spi)
769 {
770         struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
771         struct spi_mem *mem;
772
773         mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
774         if (!mem)
775                 return -ENOMEM;
776
777         mem->spi = spi;
778         spi_set_drvdata(spi, mem);
779
780         return memdrv->probe(mem);
781 }
782
783 static int spi_mem_remove(struct spi_device *spi)
784 {
785         struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
786         struct spi_mem *mem = spi_get_drvdata(spi);
787
788         if (memdrv->remove)
789                 return memdrv->remove(mem);
790
791         return 0;
792 }
793
794 static void spi_mem_shutdown(struct spi_device *spi)
795 {
796         struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
797         struct spi_mem *mem = spi_get_drvdata(spi);
798
799         if (memdrv->shutdown)
800                 memdrv->shutdown(mem);
801 }
802
803 /**
804  * spi_mem_driver_register_with_owner() - Register a SPI memory driver
805  * @memdrv: the SPI memory driver to register
806  * @owner: the owner of this driver
807  *
808  * Registers a SPI memory driver.
809  *
810  * Return: 0 in case of success, a negative error core otherwise.
811  */
812
813 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
814                                        struct module *owner)
815 {
816         memdrv->spidrv.probe = spi_mem_probe;
817         memdrv->spidrv.remove = spi_mem_remove;
818         memdrv->spidrv.shutdown = spi_mem_shutdown;
819
820         return __spi_register_driver(owner, &memdrv->spidrv);
821 }
822 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
823
824 /**
825  * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
826  * @memdrv: the SPI memory driver to unregister
827  *
828  * Unregisters a SPI memory driver.
829  */
830 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
831 {
832         spi_unregister_driver(&memdrv->spidrv);
833 }
834 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);
835 #endif /* __UBOOT__ */