DMA设备驱动(三)————基于Linux3.4.2的dma设备驱动的简单实现

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/interrupt.h>
#include <mach/hardware.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
#include <linux/dma-mapping.h>
#include <linux/cdev.h>

#define MEM_CPY_NO_DMA  0
#define MEM_CPY_DMA     1

#define BUF_SIZE  (512*1024)

#define DMA0_BASE_ADDR  0x4B000000
#define DMA1_BASE_ADDR  0x4B000040
#define DMA2_BASE_ADDR  0x4B000080
#define DMA3_BASE_ADDR  0x4B0000C0

struct s3c_dma_regs {
    unsigned long disrc;
    unsigned long disrcc;
    unsigned long didst;
    unsigned long didstc;
    unsigned long dcon;
    unsigned long dstat;
    unsigned long dcsrc;
    unsigned long dcdst;
    unsigned long dmasktrig;
};

static char *src;
static u32 src_phys;

static char *dst;
static u32 dst_phys;

static struct cdev dma_cdev;
static dev_t dma_devno;

static struct class  *dma_class;
static struct device *dma_device;

static volatile struct s3c_dma_regs *dma_regs;

static DECLARE_WAIT_QUEUE_HEAD(dma_waitq);
/* 中断事件标志, 中断服务程序将它置1,ioctl将它清0 */
static volatile int ev_dma = 0;

static int s3c_dma_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
{
    int i;

    memset(src, 0xAA, BUF_SIZE);
    memset(dst, 0x55, BUF_SIZE);

    switch (cmd)
    {
        case MEM_CPY_NO_DMA :
        {
            for (i = 0; i < BUF_SIZE; i++)
                dst[i] = src[i];
            if (memcmp(src, dst, BUF_SIZE) == 0)
            {
                printk("MEM_CPY_NO_DMA OK\n");
            }
            else
            {
                printk("MEM_CPY_DMA ERROR\n");
            }
            break;
        }

        case MEM_CPY_DMA :
        {
            ev_dma = 0;

            /* 把源,目的,长度告诉DMA */
            dma_regs->disrc      = src_phys;        /* 源的物理地址 */
            dma_regs->disrcc     = (0<<1) | (0<<0); /* 源位于AHB总线, 源地址递增 */
            dma_regs->didst      = dst_phys;        /* 目的的物理地址 */
            dma_regs->didstc     = (0<<2) | (0<<1) | (0<<0); /* 目的位于AHB总线, 目的地址递增 */
            dma_regs->dcon       = (1<<30)|(1<<29)|(0<<28)|(1<<27)|(0<<23)|(0<<20)|(BUF_SIZE<<0);  /* 使能中断,单个传输,软件触发, */

            /* 启动DMA */
            dma_regs->dmasktrig  = (1<<1) | (1<<0);

            /* 如何知道DMA什么时候完成? */
            /* 休眠 */
            wait_event_interruptible(dma_waitq, ev_dma);

            if (memcmp(src, dst, BUF_SIZE) == 0)
            {
                printk("MEM_CPY_DMA OK\n");
            }
            else
            {
                printk("MEM_CPY_DMA ERROR\n");
            }

            break;
        }
    }

    return 0;
}

static struct file_operations dma_fops = {
    .owner  = THIS_MODULE,
    .unlocked_ioctl  = s3c_dma_ioctl,
};

static irqreturn_t s3c_dma_irq(int irq, void *devid)
{
    /* 唤醒 */
    ev_dma = 1;
    wake_up_interruptible(&dma_waitq);   /* 唤醒休眠的进程 */
    return IRQ_HANDLED;
}

static int s3c_dma_init(void)
{
    int iError;

    if (request_irq(IRQ_DMA3, s3c_dma_irq, 0, "s3c_dma", 1))
    {
        printk("can't request_irq for DMA\n");
        return -EBUSY;
    }

    /* 分配SRC, DST对应的缓冲区 */
    src = dma_alloc_writecombine(NULL, BUF_SIZE, &src_phys, GFP_KERNEL);
    if (NULL == src)
    {
        printk("can't alloc buffer for src\n");
        free_irq(IRQ_DMA3, 1);
        return -ENOMEM;
    }

    dst = dma_alloc_writecombine(NULL, BUF_SIZE, &dst_phys, GFP_KERNEL);
    if (NULL == dst)
    {
        free_irq(IRQ_DMA3, 1);
        dma_free_writecombine(NULL, BUF_SIZE, src, src_phys);
        printk("can't alloc buffer for dst\n");
        return -ENOMEM;
    }

    cdev_init(&dma_cdev, &dma_fops);
    iError = alloc_chrdev_region(&dma_devno, 0, 1, "s3c_dma");
    if(iError < 0)
    {
        printk("alloc_chrdev_region failed!\n");
        return -1;
    }

    iError = cdev_add(&dma_cdev, dma_devno, 1);
    if(iError < 0)
    {
        printk("cdev_add failed!\n");
        return -1;
    }


    /* 为了自动创建设备节点 */
    dma_class = class_create(THIS_MODULE, "s3c_dma");
    dma_device = device_create(dma_class, NULL, dma_devno, NULL, "dma");


    dma_regs = ioremap(DMA3_BASE_ADDR, sizeof(struct s3c_dma_regs));

    return 0;
}

static void s3c_dma_exit(void)
{
    iounmap(dma_regs);
    class_destroy(dma_class);
    device_destroy(dma_class, dma_devno);
    unregister_chrdev(dma_devno, "s3c_dma");
    dma_free_writecombine(NULL, BUF_SIZE, src, src_phys);
    dma_free_writecombine(NULL, BUF_SIZE, dst, dst_phys);   
    free_irq(IRQ_DMA3, 1);
}

module_init(s3c_dma_init);
module_exit(s3c_dma_exit);

MODULE_LICENSE("GPL");
这里提供一个简单DMA驱动程序,仅供参考。需要注意的是,该程序仅适用于特定硬件平台,不可直接复制使用。 ```c #include <linux/module.h> #include <linux/init.h> #include <linux/platform_device.h> #include <linux/dma-mapping.h> #include <linux/dmaengine.h> #include <linux/slab.h> #define DMA_BUF_SIZE 4096 struct dma_buf { dma_addr_t dma_addr; void *virt_addr; }; struct dma_dev { struct device *dev; struct dma_chan *chan; struct dma_buf buf; }; static int dma_dev_probe(struct platform_device *pdev) { struct dma_dev *dev; struct resource *res; int err = -ENOMEM; dev = kzalloc(sizeof(*dev), GFP_KERNEL); if (!dev) goto fail_alloc_dev; dev->dev = &pdev->dev; res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) { dev_err(dev->dev, "failed to get memory resource\n"); goto fail_get_resource; } dev->buf.virt_addr = dma_alloc_coherent(dev->dev, DMA_BUF_SIZE, &dev->buf.dma_addr, GFP_KERNEL); if (!dev->buf.virt_addr) { dev_err(dev->dev, "failed to allocate DMA buffer\n"); goto fail_dma_alloc; } dev->chan = dma_request_channel(DMA_MEMCPY); if (!dev->chan) { dev_err(dev->dev, "failed to request DMA channel\n"); goto fail_dma_request; } return 0; fail_dma_request: dma_free_coherent(dev->dev, DMA_BUF_SIZE, dev->buf.virt_addr, dev->buf.dma_addr); fail_dma_alloc: fail_get_resource: kfree(dev); fail_alloc_dev: return err; } static int dma_dev_remove(struct platform_device *pdev) { struct dma_dev *dev = platform_get_drvdata(pdev); dma_release_channel(dev->chan); dma_free_coherent(dev->dev, DMA_BUF_SIZE, dev->buf.virt_addr, dev->buf.dma_addr); kfree(dev); return 0; } static struct platform_driver dma_dev_driver = { .driver = { .name = "dma_dev", }, .probe = dma_dev_probe, .remove = dma_dev_remove, }; static int __init dma_dev_init(void) { return platform_driver_register(&dma_dev_driver); } static void __exit dma_dev_exit(void) { platform_driver_unregister(&dma_dev_driver); } module_init(dma_dev_init); module_exit(dma_dev_exit); MODULE_AUTHOR("Your Name"); MODULE_LICENSE("GPL"); ``` 在上述代码中,我们定义了一个`dma_dev`结构体,其中包含了一个`dma_addr_t`类型的DMA地址和一个指向DMA缓冲区的虚拟地址。在`dma_dev_probe`函数中,我们首先通过`kzalloc`函数分配了一个`dma_dev`结构体,并将其与设备对象相关联。然后,我们通过`platform_get_resource`函数获取设备的内存资源,并通过`dma_alloc_coherent`函数分配一段大小为`DMA_BUF_SIZE`的DMA缓冲区。接下来,我们调用`dma_request_channel`函数请求DMA通道,并将其与`dma_dev`结构体相关联。 在`dma_dev_remove`函数中,我们首先获取`dma_dev`结构体,并释放DMA通道和DMA缓冲区。最后,我们通过`platform_driver_register`函数将驱动程序注册到Linux内核中,并提供了一个`__exit`函数来卸载驱动程序。 需要注意的是,这段代码并不完整,缺少了一些必要的细节,例如创建设备节点、使用DMA通道传输数据等等。实际上,DMA驱动程序的编写涉及到非常复杂的硬件操作,需要根据具体硬件平台进行调整。因此,在编写DMA驱动程序之前,建议先学习相关的硬件知识和Linux内核编程基础知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值