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 DECLARE_GLOBAL_DATA_PTR;
20
dma_get_device(u32 transfer_type,struct udevice ** devp)21 int dma_get_device(u32 transfer_type, struct udevice **devp)
22 {
23 struct udevice *dev;
24
25 for (uclass_first_device(UCLASS_DMA, &dev); dev;
26 uclass_next_device(&dev)) {
27 struct dma_dev_priv *uc_priv;
28
29 uc_priv = dev_get_uclass_priv(dev);
30 if (uc_priv->supported & transfer_type)
31 break;
32 }
33
34 if (!dev) {
35 pr_err("No DMA device found that supports %x type\n",
36 transfer_type);
37 return -EPROTONOSUPPORT;
38 }
39
40 *devp = dev;
41
42 return 0;
43 }
44
dma_memcpy(void * dst,void * src,size_t len)45 int dma_memcpy(void *dst, void *src, size_t len)
46 {
47 struct udevice *dev;
48 const struct dma_ops *ops;
49 int ret;
50
51 ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev);
52 if (ret < 0)
53 return ret;
54
55 ops = device_get_ops(dev);
56 if (!ops->transfer)
57 return -ENOSYS;
58
59 /* Invalidate the area, so no writeback into the RAM races with DMA */
60 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst +
61 roundup(len, ARCH_DMA_MINALIGN));
62
63 return ops->transfer(dev, DMA_MEM_TO_MEM, dst, src, len);
64 }
65
66 UCLASS_DRIVER(dma) = {
67 .id = UCLASS_DMA,
68 .name = "dma",
69 .flags = DM_UC_FLAG_SEQ_ALIAS,
70 .per_device_auto_alloc_size = sizeof(struct dma_dev_priv),
71 };
72