linux XDMA驱动程序代码
时间: 2023-06-15 16:03:30 浏览: 186
下面是一个简单的 Linux XDMA 驱动程序的代码示例:
```
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/io.h>
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
// XDMA 寄存器的偏移量
#define XDMA_CTRL_OFFSET 0x00
#define XDMA_STATUS_OFFSET 0x04
#define XDMA_SRC_ADDR_OFFSET 0x08
#define XDMA_DEST_ADDR_OFFSET 0x0C
#define XDMA_TRANSFER_LEN_OFFSET 0x10
// XDMA 寄存器的位域定义
#define XDMA_CTRL_START_BIT 0
#define XDMA_CTRL_RESET_BIT 1
#define XDMA_STATUS_DONE_BIT 0
struct xdma_device {
struct platform_device *pdev;
void __iomem *regs;
dma_addr_t src_phys;
dma_addr_t dest_phys;
size_t len;
struct dma_chan *chan;
};
static void xdma_transfer_complete(void *arg)
{
struct xdma_device *dev = arg;
unsigned long flags;
spin_lock_irqsave(&dev->chan->lock, flags);
dma_cookie_complete(dev->chan, dev->cookie);
spin_unlock_irqrestore(&dev->chan->lock, flags);
}
static int xdma_transfer(struct xdma_device *dev)
{
int ret;
ret = dmaengine_prep_dma_memcpy(dev->chan, dev->dest_phys,
dev->src_phys, dev->len,
DMA_PREP_INTERRUPT |
DMA_CTRL_ACK);
if (ret < 0) {
dev_err(&dev->pdev->dev, "dmaengine_prep_dma_memcpy failed: %d\n", ret);
return ret;
}
dev->cookie = dmaengine_submit(dev->chan, &desc);
dma_async_issue_pending(dev->chan);
return 0;
}
static int xdma_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct xdma_device *xdma;
struct resource *res;
int ret;
xdma = devm_kzalloc(dev, sizeof(*xdma), GFP_KERNEL);
if (!xdma)
return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
xdma->regs = devm_ioremap_resource(dev, res);
if (IS_ERR(xdma->regs))
return PTR_ERR(xdma->regs);
xdma->src_phys = dma_map_single(dev, xdma->src, xdma->len, DMA_TO_DEVICE);
if (dma_mapping_error(dev, xdma->src_phys)) {
dev_err(dev, "dma_map_single failed for source\n");
return -ENOMEM;
}
xdma->dest_phys = dma_map_single(dev, xdma->dest, xdma->len, DMA_FROM_DEVICE);
if (dma_mapping_error(dev, xdma->dest_phys)) {
dev_err(dev, "dma_map_single failed for destination\n");
ret = -ENOMEM;
goto unmap_src;
}
xdma->chan = dma_request_chan(dev, "dma0");
if (IS_ERR(xdma->chan)) {
dev_err(dev, "dma_request_chan failed\n");
ret = PTR_ERR(xdma->chan);
goto unmap_dest;
}
ret = xdma_transfer(xdma);
if (ret < 0) {
dev_err(dev, "xdma_transfer failed: %d\n", ret);
goto release_chan;
}
return 0;
release_chan:
dma_release_channel(xdma->chan);
unmap_dest:
dma_unmap_single(dev, xdma->dest_phys, xdma->len, DMA_FROM_DEVICE);
unmap_src:
dma_unmap_single(dev, xdma->src_phys, xdma->len, DMA_TO_DEVICE);
return ret;
}
static int xdma_remove(struct platform_device *pdev)
{
struct xdma_device *xdma = platform_get_drvdata(pdev);
dmaengine_terminate_all(xdma->chan);
dma_release_channel(xdma->chan);
dma_unmap_single(&pdev->dev, xdma->dest_phys, xdma->len, DMA_FROM_DEVICE);
dma_unmap_single(&pdev->dev, xdma->src_phys, xdma->len, DMA_TO_DEVICE);
return 0;
}
static const struct of_device_id xdma_of_match[] = {
{ .compatible = "xlnx,axi-dma-1.00.a", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, xdma_of_match);
static struct platform_driver xdma_driver = {
.driver = {
.name = "xdma",
.of_match_table = xdma_of_match,
},
.probe = xdma_probe,
.remove = xdma_remove,
};
module_platform_driver(xdma_driver);
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Simple XDMA Driver");
MODULE_LICENSE("GPL");
```
请注意,上述代码只是一个简单的示例,可能需要针对您的特定硬件进行修改和调整。建议您仔细阅读 Linux DMA 引擎的文档以及您的硬件的数据手册。
阅读全文