| 1.kmalloc 分配的虚拟地址连续,物理地址一定连续2.想用DMA必须分配连续的内存
 3. kmalloc特殊之处在于它分配的内存是物理上连续的,这对于要进行DMA的设备十分重要. 而用vmalloc分配的内存只是线性地址连续,物理地址不一定连续,不能直接用于DMA。
 定义源src  目的dst   源物理地址src_phys  目的物理地址dst_phys 大小定义宏
 
 4.在驱动的入口函数分配缓冲区(注意不能用vmalloc分配出来物理地址不一定连续)
 
  
 
 第一个源表示在系统总线上还是外设总线上第二个源地址是递增的还是固定的
  目的地在内存上还是外设上
 6.操作寄存器
  创建结构体封装寄存器unsigned long型
 DMA0和DMA1,DMA2寄存器地址然后定义变量static volatile struct s3c_dma_regs *dma_regs;
 然后再去映射物理地址ioremap。
  2.具体步骤把源,目的,长度告诉DMA
 源地址操作寄存器(注意是物理地址)
 
  第一位控制源地址在内存还是在外设,如下图。
 
  目的地址的物理地址
 
  目的寄存器操作
 
   DMA控制寄存器 (1)DMA模式(2)同步 外设发给单片机请求DMA,答应给我一个DMArequst给我一个回应信号ACK
 (3)中断
 (4)使能中断知道他已经传输完成。
 
  
  第二个模式会一直站住总线知道DMA传输完
  23位设为0用软件触发的
  数据传输大小一个字节,
  还有TSZ和DSZDSZ:读写数据的大小 传输一次一字节还是4字节
 TSZ:单次传输R/W一次 burst传输R/W四次
 TC: 传输多少次
 总长度=TCTSZDSZ。
 dma_regs->dcon = (1<<30)|(1<<29)|(0<<28)|(1<<27)|(0<<23)|(0<<20)|(BUF_SIZE<<0);  
 /* 使能中断,单个传输,软件触发, */ 启动DMA如何意识到已经传输完成?
 (1)完成会产生中断,所以我们先requst_irq
 if (request_irq(IRQ_DMA3, s3c_dma_irq, 0, "s3c_dma", 1))
	{
		printk("can't request_irq for DMA\n");
		return -EBUSY;
	}
tatic irqreturn_t s3c_dma_irq(int irq, void *devid)
{
	
	ev_dma = 1; 
    wake_up_interruptible(&dma_waitq);   
	return IRQ_HANDLED;
}
 在启动DMA后休眠在中断后唤醒 static DECLARE_WAIT_QUEUE_HEAD(dma_waitq);  
wait_event_interruptible(dma_waitq, ev_dma);  
static volatile int ev_dma = 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;
 驱动代码整合 #include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
#include <linux/dma-mapping.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 int major = 0;
static char *src;
static u32 src_phys;
static char *dst;
static u32 dst_phys;
static struct class *cls;
static volatile struct s3c_dma_regs *dma_regs;
static DECLARE_WAIT_QUEUE_HEAD(dma_waitq);
static volatile int ev_dma = 0;
static int s3c_dma_ioctl(struct inode *inode, 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_regs->disrc      = src_phys;        
			dma_regs->disrcc     = (0<<1) | (0<<0); 
			dma_regs->didst      = dst_phys;        
			dma_regs->didstc     = (0<<2) | (0<<1) | (0<<0); 
			dma_regs->dcon       = (1<<30)|(1<<29)|(0<<28)|(1<<27)|(0<<23)|(0<<20)|(BUF_SIZE<<0);  
			
			dma_regs->dmasktrig  = (1<<1) | (1<<0);
			
			
			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,
	.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)
{
	if (request_irq(IRQ_DMA3, s3c_dma_irq, 0, "s3c_dma", 1))
	{
		printk("can't request_irq for DMA\n");
		return -EBUSY;
	}
	
	
	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;
	}
	major = register_chrdev(0, "s3c_dma", &dma_fops);
	
	cls = class_create(THIS_MODULE, "s3c_dma");
	class_device_create(cls, NULL, MKDEV(major, 0), 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_device_destroy(cls, MKDEV(major, 0));
	class_destroy(cls);
	unregister_chrdev(major, "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");
 |