1 // SPDX-License-Identifier: GPL-2.0+
3 * Direct Memory Access U-Class Simulation driver
5 * Copyright (C) 2018 Texas Instruments Incorporated <www.ti.com>
7 * Author: Grygorii Strashko <grygorii.strashko@ti.com>
13 #include <dma-uclass.h>
14 #include <dt-structs.h>
17 #define SANDBOX_DMA_CH_CNT 3
18 #define SANDBOX_DMA_BUF_SIZE 1024
20 struct sandbox_dma_chan {
21 struct sandbox_dma_dev *ud;
24 enum dma_direction dir;
29 struct sandbox_dma_dev {
32 struct sandbox_dma_chan channels[SANDBOX_DMA_CH_CNT];
33 uchar buf[SANDBOX_DMA_BUF_SIZE];
39 static int sandbox_dma_transfer(struct udevice *dev, int direction,
40 void *dst, void *src, size_t len)
42 memcpy(dst, src, len);
47 static int sandbox_dma_of_xlate(struct dma *dma,
48 struct ofnode_phandle_args *args)
50 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
51 struct sandbox_dma_chan *uc;
53 debug("%s(dma id=%u)\n", __func__, args->args[0]);
55 if (args->args[0] >= SANDBOX_DMA_CH_CNT)
58 dma->id = args->args[0];
60 uc = &ud->channels[dma->id];
63 uc->dir = DMA_MEM_TO_DEV;
64 else if (dma->id == 2)
65 uc->dir = DMA_DEV_TO_MEM;
67 uc->dir = DMA_MEM_TO_MEM;
68 debug("%s(dma id=%lu dir=%d)\n", __func__, dma->id, uc->dir);
73 static int sandbox_dma_request(struct dma *dma)
75 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
76 struct sandbox_dma_chan *uc;
78 if (dma->id >= SANDBOX_DMA_CH_CNT)
81 uc = &ud->channels[dma->id];
86 debug("%s(dma id=%lu in_use=%d)\n", __func__, dma->id, uc->in_use);
91 static int sandbox_dma_free(struct dma *dma)
93 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
94 struct sandbox_dma_chan *uc;
96 if (dma->id >= SANDBOX_DMA_CH_CNT)
99 uc = &ud->channels[dma->id];
106 debug("%s(dma id=%lu in_use=%d)\n", __func__, dma->id, uc->in_use);
111 static int sandbox_dma_enable(struct dma *dma)
113 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
114 struct sandbox_dma_chan *uc;
116 if (dma->id >= SANDBOX_DMA_CH_CNT)
119 uc = &ud->channels[dma->id];
126 debug("%s(dma id=%lu enabled=%d)\n", __func__, dma->id, uc->enabled);
131 static int sandbox_dma_disable(struct dma *dma)
133 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
134 struct sandbox_dma_chan *uc;
136 if (dma->id >= SANDBOX_DMA_CH_CNT)
139 uc = &ud->channels[dma->id];
146 debug("%s(dma id=%lu enabled=%d)\n", __func__, dma->id, uc->enabled);
151 static int sandbox_dma_send(struct dma *dma,
152 void *src, size_t len, void *metadata)
154 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
155 struct sandbox_dma_chan *uc;
157 if (dma->id >= SANDBOX_DMA_CH_CNT)
159 if (!src || !metadata)
162 debug("%s(dma id=%lu)\n", __func__, dma->id);
164 uc = &ud->channels[dma->id];
165 if (uc->dir != DMA_MEM_TO_DEV)
171 if (len >= SANDBOX_DMA_BUF_SIZE)
174 memcpy(ud->buf, src, len);
176 ud->meta = *((u32 *)metadata);
178 debug("%s(dma id=%lu len=%zu meta=%08x)\n",
179 __func__, dma->id, len, ud->meta);
184 static int sandbox_dma_receive(struct dma *dma, void **dst, void *metadata)
186 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
187 struct sandbox_dma_chan *uc;
189 if (dma->id >= SANDBOX_DMA_CH_CNT)
191 if (!dst || !metadata)
194 uc = &ud->channels[dma->id];
195 if (uc->dir != DMA_DEV_TO_MEM)
205 memcpy(ud->buf_rx, ud->buf, ud->data_len);
208 memcpy(*dst, ud->buf, ud->data_len);
211 *((u32 *)metadata) = ud->meta;
213 debug("%s(dma id=%lu len=%zu meta=%08x %p)\n",
214 __func__, dma->id, ud->data_len, ud->meta, *dst);
219 static int sandbox_dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
221 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
228 static const struct dma_ops sandbox_dma_ops = {
229 .transfer = sandbox_dma_transfer,
230 .of_xlate = sandbox_dma_of_xlate,
231 .request = sandbox_dma_request,
232 .free = sandbox_dma_free,
233 .enable = sandbox_dma_enable,
234 .disable = sandbox_dma_disable,
235 .send = sandbox_dma_send,
236 .receive = sandbox_dma_receive,
237 .prepare_rcv_buf = sandbox_dma_prepare_rcv_buf,
240 static int sandbox_dma_probe(struct udevice *dev)
242 struct dma_dev_priv *uc_priv = dev_get_uclass_priv(dev);
243 struct sandbox_dma_dev *ud = dev_get_priv(dev);
246 uc_priv->supported = DMA_SUPPORTS_MEM_TO_MEM |
247 DMA_SUPPORTS_MEM_TO_DEV |
248 DMA_SUPPORTS_DEV_TO_MEM;
250 ud->ch_count = SANDBOX_DMA_CH_CNT;
255 pr_err("Number of channels: %u\n", ud->ch_count);
257 for (i = 0; i < ud->ch_count; i++) {
258 struct sandbox_dma_chan *uc = &ud->channels[i];
262 sprintf(uc->name, "DMA chan%d\n", i);
270 static const struct udevice_id sandbox_dma_ids[] = {
271 { .compatible = "sandbox,dma" },
275 U_BOOT_DRIVER(sandbox_dma) = {
276 .name = "sandbox-dma",
278 .of_match = sandbox_dma_ids,
279 .ops = &sandbox_dma_ops,
280 .probe = sandbox_dma_probe,
281 .priv_auto_alloc_size = sizeof(struct sandbox_dma_dev),