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>
17 #include <asm/cache.h>
19 #include <dma-uclass.h>
20 #include <dt-structs.h>
23 #ifdef CONFIG_DMA_CHANNELS
24 static inline struct dma_ops *dma_dev_ops(struct udevice *dev)
26 return (struct dma_ops *)dev->driver->ops;
29 # if CONFIG_IS_ENABLED(OF_CONTROL)
30 static int dma_of_xlate_default(struct dma *dma,
31 struct ofnode_phandle_args *args)
33 debug("%s(dma=%p)\n", __func__, dma);
35 if (args->args_count > 1) {
36 pr_err("Invaild args_count: %d\n", args->args_count);
41 dma->id = args->args[0];
48 int dma_get_by_index(struct udevice *dev, int index, struct dma *dma)
51 struct ofnode_phandle_args args;
52 struct udevice *dev_dma;
53 const struct dma_ops *ops;
55 debug("%s(dev=%p, index=%d, dma=%p)\n", __func__, dev, index, dma);
60 ret = dev_read_phandle_with_args(dev, "dmas", "#dma-cells", 0, index,
63 pr_err("%s: dev_read_phandle_with_args failed: err=%d\n",
68 ret = uclass_get_device_by_ofnode(UCLASS_DMA, args.node, &dev_dma);
70 pr_err("%s: uclass_get_device_by_ofnode failed: err=%d\n",
77 ops = dma_dev_ops(dev_dma);
80 ret = ops->of_xlate(dma, &args);
82 ret = dma_of_xlate_default(dma, &args);
84 pr_err("of_xlate() failed: %d\n", ret);
88 return dma_request(dev_dma, dma);
91 int dma_get_by_name(struct udevice *dev, const char *name, struct dma *dma)
95 debug("%s(dev=%p, name=%s, dma=%p)\n", __func__, dev, name, dma);
98 index = dev_read_stringlist_search(dev, "dma-names", name);
100 pr_err("dev_read_stringlist_search() failed: %d\n", index);
104 return dma_get_by_index(dev, index, dma);
106 # endif /* OF_CONTROL */
108 int dma_request(struct udevice *dev, struct dma *dma)
110 struct dma_ops *ops = dma_dev_ops(dev);
112 debug("%s(dev=%p, dma=%p)\n", __func__, dev, dma);
119 return ops->request(dma);
122 int dma_free(struct dma *dma)
124 struct dma_ops *ops = dma_dev_ops(dma->dev);
126 debug("%s(dma=%p)\n", __func__, dma);
131 return ops->rfree(dma);
134 int dma_enable(struct dma *dma)
136 struct dma_ops *ops = dma_dev_ops(dma->dev);
138 debug("%s(dma=%p)\n", __func__, dma);
143 return ops->enable(dma);
146 int dma_disable(struct dma *dma)
148 struct dma_ops *ops = dma_dev_ops(dma->dev);
150 debug("%s(dma=%p)\n", __func__, dma);
155 return ops->disable(dma);
158 int dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
160 struct dma_ops *ops = dma_dev_ops(dma->dev);
162 debug("%s(dma=%p)\n", __func__, dma);
164 if (!ops->prepare_rcv_buf)
167 return ops->prepare_rcv_buf(dma, dst, size);
170 int dma_receive(struct dma *dma, void **dst, void *metadata)
172 struct dma_ops *ops = dma_dev_ops(dma->dev);
174 debug("%s(dma=%p)\n", __func__, dma);
179 return ops->receive(dma, dst, metadata);
182 int dma_send(struct dma *dma, void *src, size_t len, void *metadata)
184 struct dma_ops *ops = dma_dev_ops(dma->dev);
186 debug("%s(dma=%p)\n", __func__, dma);
191 return ops->send(dma, src, len, metadata);
194 int dma_get_cfg(struct dma *dma, u32 cfg_id, void **cfg_data)
196 struct dma_ops *ops = dma_dev_ops(dma->dev);
198 debug("%s(dma=%p)\n", __func__, dma);
203 return ops->get_cfg(dma, cfg_id, cfg_data);
205 #endif /* CONFIG_DMA_CHANNELS */
207 int dma_get_device(u32 transfer_type, struct udevice **devp)
212 for (ret = uclass_first_device(UCLASS_DMA, &dev); dev && !ret;
213 ret = uclass_next_device(&dev)) {
214 struct dma_dev_priv *uc_priv;
216 uc_priv = dev_get_uclass_priv(dev);
217 if (uc_priv->supported & transfer_type)
222 pr_err("No DMA device found that supports %x type\n",
224 return -EPROTONOSUPPORT;
232 int dma_memcpy(void *dst, void *src, size_t len)
235 const struct dma_ops *ops;
238 ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev);
242 ops = device_get_ops(dev);
246 /* Invalidate the area, so no writeback into the RAM races with DMA */
247 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst +
248 roundup(len, ARCH_DMA_MINALIGN));
250 return ops->transfer(dev, DMA_MEM_TO_MEM, dst, src, len);
253 UCLASS_DRIVER(dma) = {
256 .flags = DM_UC_FLAG_SEQ_ALIAS,
257 .per_device_auto_alloc_size = sizeof(struct dma_dev_priv),