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>
14 #include <dma-uclass.h>
15 #include <dt-structs.h>
18 #define SANDBOX_DMA_CH_CNT 3
19 #define SANDBOX_DMA_BUF_SIZE 1024
21 struct sandbox_dma_chan {
22 struct sandbox_dma_dev *ud;
25 enum dma_direction dir;
30 struct sandbox_dma_dev {
33 struct sandbox_dma_chan channels[SANDBOX_DMA_CH_CNT];
34 uchar buf[SANDBOX_DMA_BUF_SIZE];
40 static int sandbox_dma_transfer(struct udevice *dev, int direction,
41 void *dst, void *src, size_t len)
43 memcpy(dst, src, len);
48 static int sandbox_dma_of_xlate(struct dma *dma,
49 struct ofnode_phandle_args *args)
51 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
52 struct sandbox_dma_chan *uc;
54 debug("%s(dma id=%u)\n", __func__, args->args[0]);
56 if (args->args[0] >= SANDBOX_DMA_CH_CNT)
59 dma->id = args->args[0];
61 uc = &ud->channels[dma->id];
64 uc->dir = DMA_MEM_TO_DEV;
65 else if (dma->id == 2)
66 uc->dir = DMA_DEV_TO_MEM;
68 uc->dir = DMA_MEM_TO_MEM;
69 debug("%s(dma id=%lu dir=%d)\n", __func__, dma->id, uc->dir);
74 static int sandbox_dma_request(struct dma *dma)
76 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
77 struct sandbox_dma_chan *uc;
79 if (dma->id >= SANDBOX_DMA_CH_CNT)
82 uc = &ud->channels[dma->id];
87 debug("%s(dma id=%lu in_use=%d)\n", __func__, dma->id, uc->in_use);
92 static int sandbox_dma_rfree(struct dma *dma)
94 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
95 struct sandbox_dma_chan *uc;
97 if (dma->id >= SANDBOX_DMA_CH_CNT)
100 uc = &ud->channels[dma->id];
107 debug("%s(dma id=%lu in_use=%d)\n", __func__, dma->id, uc->in_use);
112 static int sandbox_dma_enable(struct dma *dma)
114 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
115 struct sandbox_dma_chan *uc;
117 if (dma->id >= SANDBOX_DMA_CH_CNT)
120 uc = &ud->channels[dma->id];
127 debug("%s(dma id=%lu enabled=%d)\n", __func__, dma->id, uc->enabled);
132 static int sandbox_dma_disable(struct dma *dma)
134 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
135 struct sandbox_dma_chan *uc;
137 if (dma->id >= SANDBOX_DMA_CH_CNT)
140 uc = &ud->channels[dma->id];
147 debug("%s(dma id=%lu enabled=%d)\n", __func__, dma->id, uc->enabled);
152 static int sandbox_dma_send(struct dma *dma,
153 void *src, size_t len, void *metadata)
155 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
156 struct sandbox_dma_chan *uc;
158 if (dma->id >= SANDBOX_DMA_CH_CNT)
160 if (!src || !metadata)
163 debug("%s(dma id=%lu)\n", __func__, dma->id);
165 uc = &ud->channels[dma->id];
166 if (uc->dir != DMA_MEM_TO_DEV)
172 if (len >= SANDBOX_DMA_BUF_SIZE)
175 memcpy(ud->buf, src, len);
177 ud->meta = *((u32 *)metadata);
179 debug("%s(dma id=%lu len=%zu meta=%08x)\n",
180 __func__, dma->id, len, ud->meta);
185 static int sandbox_dma_receive(struct dma *dma, void **dst, void *metadata)
187 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
188 struct sandbox_dma_chan *uc;
190 if (dma->id >= SANDBOX_DMA_CH_CNT)
192 if (!dst || !metadata)
195 uc = &ud->channels[dma->id];
196 if (uc->dir != DMA_DEV_TO_MEM)
206 memcpy(ud->buf_rx, ud->buf, ud->data_len);
209 memcpy(*dst, ud->buf, ud->data_len);
212 *((u32 *)metadata) = ud->meta;
214 debug("%s(dma id=%lu len=%zu meta=%08x %p)\n",
215 __func__, dma->id, ud->data_len, ud->meta, *dst);
220 static int sandbox_dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
222 struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
229 static const struct dma_ops sandbox_dma_ops = {
230 .transfer = sandbox_dma_transfer,
231 .of_xlate = sandbox_dma_of_xlate,
232 .request = sandbox_dma_request,
233 .rfree = sandbox_dma_rfree,
234 .enable = sandbox_dma_enable,
235 .disable = sandbox_dma_disable,
236 .send = sandbox_dma_send,
237 .receive = sandbox_dma_receive,
238 .prepare_rcv_buf = sandbox_dma_prepare_rcv_buf,
241 static int sandbox_dma_probe(struct udevice *dev)
243 struct dma_dev_priv *uc_priv = dev_get_uclass_priv(dev);
244 struct sandbox_dma_dev *ud = dev_get_priv(dev);
247 uc_priv->supported = DMA_SUPPORTS_MEM_TO_MEM |
248 DMA_SUPPORTS_MEM_TO_DEV |
249 DMA_SUPPORTS_DEV_TO_MEM;
251 ud->ch_count = SANDBOX_DMA_CH_CNT;
256 pr_err("Number of channels: %u\n", ud->ch_count);
258 for (i = 0; i < ud->ch_count; i++) {
259 struct sandbox_dma_chan *uc = &ud->channels[i];
263 sprintf(uc->name, "DMA chan%d\n", i);
271 static const struct udevice_id sandbox_dma_ids[] = {
272 { .compatible = "sandbox,dma" },
276 U_BOOT_DRIVER(sandbox_dma) = {
277 .name = "sandbox-dma",
279 .of_match = sandbox_dma_ids,
280 .ops = &sandbox_dma_ops,
281 .probe = sandbox_dma_probe,
282 .priv_auto_alloc_size = sizeof(struct sandbox_dma_dev),