码迷,mamicode.com
首页 > 其他好文 > 详细

kobox : dma_s3c.ko -v1 操作寄存器方式操作S3C2440的DMA

时间:2014-10-19 23:23:14      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:linux   dma   

平台:TQ2440

linux版本:Linux EmbedSky 3.16.1-svn57 #56 Sat Oct 18 21:46:22 PDT 2014 armv4tl GNU/Linux

kobox : dma_s3c.ko -v1 操作寄存器方式操作S3C2440的DMA

目标:v2中改成s3c2410_dma_xxx方式来操作DMA,看这里的寄存器映射是怎么使用系统接口来操作的!

#include "dma.h"

#define MEM_CPY_NO_DMA  0
#define MEM_CPY_DMA     1

//#define BUF_SIZE  (512*1024)
#define BUF_SIZE  (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;	 	 //DISRC3  0x4B0000C0 R/W DMA 3 initial source register
unsigned long disrcc;	 	 //DISRCC3 0x4B0000C4 R/W DMA 3 initial source control register
unsigned long didst;	 	 //DIDST3  0x4B0000C8 R/W DMA 3 initial destination register
unsigned long didstc;	 	 //DIDSTC3 0x4B0000CC R/W DMA 3 initial destination control register 
unsigned long dcon;		 //DCON3   0x4B0000D0 R/W DMA 3 control registe
unsigned long dstat;	 	 //DSTAT3  0x4B0000D4 R   DMA 3 count register
unsigned long dcsrc;	 	 //DCSRC3  0x4B0000D8 R   DMA 3 current source register
unsigned long dcdst;	  	//DCDST3   0x4B0000DC R   DMA 3 current destination register
unsigned long dmasktrig; //DMASKTRIG3 0x4B0000E0 R/W DMA 3 mask trigger register
};

static char *srcbuff;
static u32 src_dma_phys;
static phys_addr_t src_buff_phys;

static char *dstbuff;
static u32 dst_dma_phys;
static phys_addr_t dst_buff_phys;


static struct class *cls;

static volatile struct s3c_dma_regs *dma_regs = NULL;
static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
				       dma_addr_t *dma_handle, gfp_t flag);
static inline phys_addr_t virt_to_phys(const volatile void *x);


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

static int buff_dump(void)
{
	int i;

	printk( "srcbuff:\n"
			"------------------------------------------------------------\n");
	for(i=0; i<BUF_SIZE;i++)
	{
		printk("%x ", srcbuff[i]);
		if((i+1)%32 == 0)
		{
			printk("\n");
		}
	}
	printk( "\n------------------------------------------------------------\n");

	printk( "dstbuff:\n"
			"------------------------------------------------------------\n");
	for(i=0; i<BUF_SIZE;i++)
	{
		printk("%x ", dstbuff[i]);
		if((i+1)%32 == 0)
		{
			printk("\n");
		}
	}
	printk( "\n------------------------------------------------------------\n");

	
}

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

	memset(srcbuff, 0xAA, BUF_SIZE);
	memset(dstbuff, 0x55, BUF_SIZE);
	switch (cmd)
	{
		//这是非DMA模式
		case MEM_CPY_NO_DMA :
		{
			for (i = 0; i < BUF_SIZE; i++)
			{
				dstbuff[i] = srcbuff[i];  //CPU直接将源拷贝到目的
			}	

			if (memcmp(srcbuff, dstbuff, BUF_SIZE) == 0)//这个函数见注释2
			{
				printk("MEM_CPY_NO_DMA OK\n");
			}
			else
			{
				printk("MEM_CPY_DMA ERROR\n");
			}
			
			break;
		}

		//这是DMA模式
		case MEM_CPY_DMA :
		{
			ev_dma = 0;
			/* 把源,目的,长度告诉DMA */
			/* 关于下面寄存器的具体情况,我们在注释3里面来详细讲一下 */
			dma_regs->disrc      = src_dma_phys;        /* 源的物理地址 */
			dma_regs->disrcc     = (0<<1) | (0<<0); /* 源位于AHB总线, 源地址递增 */
			dma_regs->didst      = dst_dma_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什么时候完成?--DMA完成会产生中断,然后会唤醒等待队列 */
			wait_event_interruptible(dma_waitq, 1 == ev_dma);

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

			buff_dump();	
			
			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;
}

int major;
int minor;
struct cdev cdev;
struct class *my_s3c_dma_class;
struct device *pstdev = NULL;

static int s3c_dma_init(void)
{
	/* 这里注册一个中断,当DMA数据传输完毕之后会发生此中断 */
	if (request_irq(IRQ_DMA3, s3c_dma_irq, 0, "s3c_dma", 1))
	{
		printk("can't request_irq for DMA\n");
		return -EBUSY;
	}
	
	/* 分配SRC, DST对应的缓冲区,关于此函数详见注释1 */
	srcbuff = dma_alloc_writecombine(NULL, BUF_SIZE, &src_dma_phys, GFP_KERNEL);//源
	if (NULL == srcbuff)
	{
		printk("can't alloc buffer for src\n");
		free_irq(IRQ_DMA3, 1);
		return -ENOMEM;
	}

	dstbuff = dma_alloc_writecombine(NULL, BUF_SIZE, &dst_dma_phys, GFP_KERNEL);//目的
	if (NULL == dstbuff)
	{
		free_irq(IRQ_DMA3, 1);
		dma_free_writecombine(NULL, BUF_SIZE, srcbuff, src_dma_phys);
		printk("can't alloc buffer for dst\n");
		return -ENOMEM;
	}
	
	major = register_chrdev(0, "s3c_dma", &dma_fops);
	if (major < 0)
	{
		printk(" can't register major number\n");
		return major;
	}

	/* create class */
	my_s3c_dma_class = class_create(THIS_MODULE, "s3c_dma");
	if(IS_ERR(my_s3c_dma_class))
	{
		printk("class_create failed!\n");
		return -1;
	}

	/* create /dev/s3c_dma */
	pstdev = device_create(my_s3c_dma_class, NULL, MKDEV(major, 0), NULL, "s3c_dma");
	if(!pstdev)
	{
		printk("device_create failed!\n");
		return -1;
	}

	dma_regs = ioremap(DMA3_BASE_ADDR, sizeof(struct s3c_dma_regs));//这边是将DMA控制寄存器映射到内核空间
	if(NULL == dma_regs)
	{
		printk("ioremap failed!\n");
		return -1;
	}

	return 0;
}

static void s3c_dma_exit(void)
{
	return;
}

module_init(s3c_dma_init);
module_exit(s3c_dma_exit);

MODULE_LICENSE("GPL");


kobox : dma_s3c.ko -v1 操作寄存器方式操作S3C2440的DMA

标签:linux   dma   

原文地址:http://blog.csdn.net/xiangpingli/article/details/40269675

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!