blob: a93b0b7ba08bdb9f259afe36398f47d1c1a889e5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mugunthan V N8c3c9182016-02-15 15:31:37 +05302/*
3 * Direct Memory Access U-Class driver
4 *
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +01005 * 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>
Mugunthan V N8c3c9182016-02-15 15:31:37 +05308 *
9 * Author: Mugunthan V N <mugunthanvnm@ti.com>
Mugunthan V N8c3c9182016-02-15 15:31:37 +053010 */
11
12#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Mugunthan V N8c3c9182016-02-15 15:31:37 +053014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +010018#include <dm/read.h>
Álvaro Fernández Rojas308cd6f2018-11-28 19:17:49 +010019#include <dma-uclass.h>
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +010020#include <dt-structs.h>
Mugunthan V N8c3c9182016-02-15 15:31:37 +053021#include <errno.h>
22
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +010023#ifdef CONFIG_DMA_CHANNELS
24static inline struct dma_ops *dma_dev_ops(struct udevice *dev)
25{
26 return (struct dma_ops *)dev->driver->ops;
27}
28
29# if CONFIG_IS_ENABLED(OF_CONTROL)
30static int dma_of_xlate_default(struct dma *dma,
31 struct ofnode_phandle_args *args)
32{
33 debug("%s(dma=%p)\n", __func__, dma);
34
35 if (args->args_count > 1) {
36 pr_err("Invaild args_count: %d\n", args->args_count);
37 return -EINVAL;
38 }
39
40 if (args->args_count)
41 dma->id = args->args[0];
42 else
43 dma->id = 0;
44
45 return 0;
46}
47
48int dma_get_by_index(struct udevice *dev, int index, struct dma *dma)
49{
50 int ret;
51 struct ofnode_phandle_args args;
52 struct udevice *dev_dma;
53 const struct dma_ops *ops;
54
55 debug("%s(dev=%p, index=%d, dma=%p)\n", __func__, dev, index, dma);
56
57 assert(dma);
58 dma->dev = NULL;
59
60 ret = dev_read_phandle_with_args(dev, "dmas", "#dma-cells", 0, index,
61 &args);
62 if (ret) {
63 pr_err("%s: dev_read_phandle_with_args failed: err=%d\n",
64 __func__, ret);
65 return ret;
66 }
67
68 ret = uclass_get_device_by_ofnode(UCLASS_DMA, args.node, &dev_dma);
69 if (ret) {
70 pr_err("%s: uclass_get_device_by_ofnode failed: err=%d\n",
71 __func__, ret);
72 return ret;
73 }
74
75 dma->dev = dev_dma;
76
77 ops = dma_dev_ops(dev_dma);
78
79 if (ops->of_xlate)
80 ret = ops->of_xlate(dma, &args);
81 else
82 ret = dma_of_xlate_default(dma, &args);
83 if (ret) {
84 pr_err("of_xlate() failed: %d\n", ret);
85 return ret;
86 }
87
88 return dma_request(dev_dma, dma);
89}
90
91int dma_get_by_name(struct udevice *dev, const char *name, struct dma *dma)
92{
93 int index;
94
95 debug("%s(dev=%p, name=%s, dma=%p)\n", __func__, dev, name, dma);
96 dma->dev = NULL;
97
98 index = dev_read_stringlist_search(dev, "dma-names", name);
99 if (index < 0) {
100 pr_err("dev_read_stringlist_search() failed: %d\n", index);
101 return index;
102 }
103
104 return dma_get_by_index(dev, index, dma);
105}
106# endif /* OF_CONTROL */
107
108int dma_request(struct udevice *dev, struct dma *dma)
109{
110 struct dma_ops *ops = dma_dev_ops(dev);
111
112 debug("%s(dev=%p, dma=%p)\n", __func__, dev, dma);
113
114 dma->dev = dev;
115
116 if (!ops->request)
117 return 0;
118
119 return ops->request(dma);
120}
121
122int dma_free(struct dma *dma)
123{
124 struct dma_ops *ops = dma_dev_ops(dma->dev);
125
126 debug("%s(dma=%p)\n", __func__, dma);
127
Simon Glass75c0ad62020-02-03 07:35:55 -0700128 if (!ops->rfree)
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +0100129 return 0;
130
Simon Glass75c0ad62020-02-03 07:35:55 -0700131 return ops->rfree(dma);
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +0100132}
133
134int dma_enable(struct dma *dma)
135{
136 struct dma_ops *ops = dma_dev_ops(dma->dev);
137
138 debug("%s(dma=%p)\n", __func__, dma);
139
140 if (!ops->enable)
141 return -ENOSYS;
142
143 return ops->enable(dma);
144}
145
146int dma_disable(struct dma *dma)
147{
148 struct dma_ops *ops = dma_dev_ops(dma->dev);
149
150 debug("%s(dma=%p)\n", __func__, dma);
151
152 if (!ops->disable)
153 return -ENOSYS;
154
155 return ops->disable(dma);
156}
157
158int dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
159{
160 struct dma_ops *ops = dma_dev_ops(dma->dev);
161
162 debug("%s(dma=%p)\n", __func__, dma);
163
164 if (!ops->prepare_rcv_buf)
165 return -1;
166
167 return ops->prepare_rcv_buf(dma, dst, size);
168}
169
170int dma_receive(struct dma *dma, void **dst, void *metadata)
171{
172 struct dma_ops *ops = dma_dev_ops(dma->dev);
173
174 debug("%s(dma=%p)\n", __func__, dma);
175
176 if (!ops->receive)
177 return -ENOSYS;
178
179 return ops->receive(dma, dst, metadata);
180}
181
182int dma_send(struct dma *dma, void *src, size_t len, void *metadata)
183{
184 struct dma_ops *ops = dma_dev_ops(dma->dev);
185
186 debug("%s(dma=%p)\n", __func__, dma);
187
188 if (!ops->send)
189 return -ENOSYS;
190
191 return ops->send(dma, src, len, metadata);
192}
Vignesh Raghavendrab18fb7e2019-12-04 22:17:20 +0530193
194int dma_get_cfg(struct dma *dma, u32 cfg_id, void **cfg_data)
195{
196 struct dma_ops *ops = dma_dev_ops(dma->dev);
197
198 debug("%s(dma=%p)\n", __func__, dma);
199
200 if (!ops->get_cfg)
201 return -ENOSYS;
202
203 return ops->get_cfg(dma, cfg_id, cfg_data);
204}
Álvaro Fernández Rojasd8cedab2018-11-28 19:17:50 +0100205#endif /* CONFIG_DMA_CHANNELS */
206
Mugunthan V N8c3c9182016-02-15 15:31:37 +0530207int dma_get_device(u32 transfer_type, struct udevice **devp)
208{
209 struct udevice *dev;
210 int ret;
211
212 for (ret = uclass_first_device(UCLASS_DMA, &dev); dev && !ret;
213 ret = uclass_next_device(&dev)) {
214 struct dma_dev_priv *uc_priv;
215
216 uc_priv = dev_get_uclass_priv(dev);
217 if (uc_priv->supported & transfer_type)
218 break;
219 }
220
221 if (!dev) {
Vignesh Raghavendra518d9242020-09-17 16:53:07 +0530222 pr_debug("No DMA device found that supports %x type\n",
223 transfer_type);
Mugunthan V N8c3c9182016-02-15 15:31:37 +0530224 return -EPROTONOSUPPORT;
225 }
226
227 *devp = dev;
228
229 return ret;
230}
231
232int dma_memcpy(void *dst, void *src, size_t len)
233{
234 struct udevice *dev;
235 const struct dma_ops *ops;
236 int ret;
237
238 ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev);
239 if (ret < 0)
240 return ret;
241
242 ops = device_get_ops(dev);
243 if (!ops->transfer)
244 return -ENOSYS;
245
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));
249
250 return ops->transfer(dev, DMA_MEM_TO_MEM, dst, src, len);
251}
252
253UCLASS_DRIVER(dma) = {
254 .id = UCLASS_DMA,
255 .name = "dma",
256 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700257 .per_device_auto = sizeof(struct dma_dev_priv),
Mugunthan V N8c3c9182016-02-15 15:31:37 +0530258};