1 // SPDX-License-Identifier: GPL-2.0+
3 * Direct Memory Access U-Class driver
5 * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
6 * Copyright (C) 2015 - 2018 Texas Instruments Incorporated <www.ti.com>
7 * Written by Mugunthan V N <mugunthanvnm@ti.com>
9 * Author: Mugunthan V N <mugunthanvnm@ti.com>
15 #include <dma-uclass.h>
16 #include <dt-structs.h>
19 #ifdef CONFIG_DMA_CHANNELS
20 static inline struct dma_ops *dma_dev_ops(struct udevice *dev)
22 return (struct dma_ops *)dev->driver->ops;
25 # if CONFIG_IS_ENABLED(OF_CONTROL)
26 static int dma_of_xlate_default(struct dma *dma,
27 struct ofnode_phandle_args *args)
29 debug("%s(dma=%p)\n", __func__, dma);
31 if (args->args_count > 1) {
32 pr_err("Invaild args_count: %d\n", args->args_count);
37 dma->id = args->args[0];
44 int dma_get_by_index(struct udevice *dev, int index, struct dma *dma)
47 struct ofnode_phandle_args args;
48 struct udevice *dev_dma;
49 const struct dma_ops *ops;
51 debug("%s(dev=%p, index=%d, dma=%p)\n", __func__, dev, index, dma);
56 ret = dev_read_phandle_with_args(dev, "dmas", "#dma-cells", 0, index,
59 pr_err("%s: dev_read_phandle_with_args failed: err=%d\n",
64 ret = uclass_get_device_by_ofnode(UCLASS_DMA, args.node, &dev_dma);
66 pr_err("%s: uclass_get_device_by_ofnode failed: err=%d\n",
73 ops = dma_dev_ops(dev_dma);
76 ret = ops->of_xlate(dma, &args);
78 ret = dma_of_xlate_default(dma, &args);
80 pr_err("of_xlate() failed: %d\n", ret);
84 return dma_request(dev_dma, dma);
87 int dma_get_by_name(struct udevice *dev, const char *name, struct dma *dma)
91 debug("%s(dev=%p, name=%s, dma=%p)\n", __func__, dev, name, dma);
94 index = dev_read_stringlist_search(dev, "dma-names", name);
96 pr_err("dev_read_stringlist_search() failed: %d\n", index);
100 return dma_get_by_index(dev, index, dma);
102 # endif /* OF_CONTROL */
104 int dma_request(struct udevice *dev, struct dma *dma)
106 struct dma_ops *ops = dma_dev_ops(dev);
108 debug("%s(dev=%p, dma=%p)\n", __func__, dev, dma);
115 return ops->request(dma);
118 int dma_free(struct dma *dma)
120 struct dma_ops *ops = dma_dev_ops(dma->dev);
122 debug("%s(dma=%p)\n", __func__, dma);
127 return ops->free(dma);
130 int dma_enable(struct dma *dma)
132 struct dma_ops *ops = dma_dev_ops(dma->dev);
134 debug("%s(dma=%p)\n", __func__, dma);
139 return ops->enable(dma);
142 int dma_disable(struct dma *dma)
144 struct dma_ops *ops = dma_dev_ops(dma->dev);
146 debug("%s(dma=%p)\n", __func__, dma);
151 return ops->disable(dma);
154 int dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
156 struct dma_ops *ops = dma_dev_ops(dma->dev);
158 debug("%s(dma=%p)\n", __func__, dma);
160 if (!ops->prepare_rcv_buf)
163 return ops->prepare_rcv_buf(dma, dst, size);
166 int dma_receive(struct dma *dma, void **dst, void *metadata)
168 struct dma_ops *ops = dma_dev_ops(dma->dev);
170 debug("%s(dma=%p)\n", __func__, dma);
175 return ops->receive(dma, dst, metadata);
178 int dma_send(struct dma *dma, void *src, size_t len, void *metadata)
180 struct dma_ops *ops = dma_dev_ops(dma->dev);
182 debug("%s(dma=%p)\n", __func__, dma);
187 return ops->send(dma, src, len, metadata);
189 #endif /* CONFIG_DMA_CHANNELS */
191 int dma_get_device(u32 transfer_type, struct udevice **devp)
196 for (ret = uclass_first_device(UCLASS_DMA, &dev); dev && !ret;
197 ret = uclass_next_device(&dev)) {
198 struct dma_dev_priv *uc_priv;
200 uc_priv = dev_get_uclass_priv(dev);
201 if (uc_priv->supported & transfer_type)
206 pr_err("No DMA device found that supports %x type\n",
208 return -EPROTONOSUPPORT;
216 int dma_memcpy(void *dst, void *src, size_t len)
219 const struct dma_ops *ops;
222 ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev);
226 ops = device_get_ops(dev);
230 /* Invalidate the area, so no writeback into the RAM races with DMA */
231 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst +
232 roundup(len, ARCH_DMA_MINALIGN));
234 return ops->transfer(dev, DMA_MEM_TO_MEM, dst, src, len);
237 UCLASS_DRIVER(dma) = {
240 .flags = DM_UC_FLAG_SEQ_ALIAS,
241 .per_device_auto_alloc_size = sizeof(struct dma_dev_priv),