7f92d24bc1880dfda544f1a46106647145f9a0b9
[platform/kernel/u-boot.git] / drivers / dma / dma-uclass.c
1 /*
2  * Direct Memory Access U-Class driver
3  *
4  * (C) Copyright 2015
5  *     Texas Instruments Incorporated, <www.ti.com>
6  *
7  * Author: Mugunthan V N <mugunthanvnm@ti.com>
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <common.h>
13 #include <dma.h>
14 #include <dm.h>
15 #include <dm/uclass-internal.h>
16 #include <dm/device-internal.h>
17 #include <errno.h>
18
19 int dma_get_device(u32 transfer_type, struct udevice **devp)
20 {
21         struct udevice *dev;
22         int ret;
23
24         for (ret = uclass_first_device(UCLASS_DMA, &dev); dev && !ret;
25              ret = uclass_next_device(&dev)) {
26                 struct dma_dev_priv *uc_priv;
27
28                 uc_priv = dev_get_uclass_priv(dev);
29                 if (uc_priv->supported & transfer_type)
30                         break;
31         }
32
33         if (!dev) {
34                 pr_err("No DMA device found that supports %x type\n",
35                       transfer_type);
36                 return -EPROTONOSUPPORT;
37         }
38
39         *devp = dev;
40
41         return ret;
42 }
43
44 int dma_memcpy(void *dst, void *src, size_t len)
45 {
46         struct udevice *dev;
47         const struct dma_ops *ops;
48         int ret;
49
50         ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev);
51         if (ret < 0)
52                 return ret;
53
54         ops = device_get_ops(dev);
55         if (!ops->transfer)
56                 return -ENOSYS;
57
58         /* Invalidate the area, so no writeback into the RAM races with DMA */
59         invalidate_dcache_range((unsigned long)dst, (unsigned long)dst +
60                                 roundup(len, ARCH_DMA_MINALIGN));
61
62         return ops->transfer(dev, DMA_MEM_TO_MEM, dst, src, len);
63 }
64
65 UCLASS_DRIVER(dma) = {
66         .id             = UCLASS_DMA,
67         .name           = "dma",
68         .flags          = DM_UC_FLAG_SEQ_ALIAS,
69         .per_device_auto_alloc_size = sizeof(struct dma_dev_priv),
70 };