自娱自乐9之Linux DMA使用1(三星平台DMA分析)

和以前一样,我不说dma基础知识,你可以看看ldd3


这次我说的是三星平台的dma,不是三星的某款芯片的dma使用。这主要得益于三星公司统一了接口。比如我后有的例子是在s3c2440上做的但是我是参考s3c64xx的spi驱动。
当然内核还是linux-3.2.36,我们看dma-ops.h

/* arch/arm/plat-samsung/include/plat/dma-ops.h
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *        http://www.samsung.com
 *
 * Samsung DMA support
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef __SAMSUNG_DMA_OPS_H_
#define __SAMSUNG_DMA_OPS_H_ __FILE__

#include <linux/dmaengine.h>

struct samsung_dma_prep_info {
    enum dma_transaction_type cap;//dma处理类型
    enum dma_data_direction direction;//dma传输方向
    dma_addr_t buf;//内存地址
    unsigned long period;//
    unsigned long len;//buf长度,sizeof(buf) * width,width在下面struct samsung_dma_info
    /*
    .c中调用
    int len = (info->cap == DMA_CYCLIC) ? info->period : info->len;
    ...
    我不是太清楚period和len区别
    */
    void (*fp)(void *data);//dma中断时会调用,一般作为dma传输完成的接口
    void *fp_param;//传入上面fp的参数
};

struct samsung_dma_info {
    enum dma_transaction_type cap;//dma处理类型
    /*
            if (info->cap == DMA_CYCLIC)
                s3c2410_dma_setflags(dma_ch, S3C2410_DMAF_CIRCULAR);//chan->flags设置
    这个可能和芯片有点关系
    我的plat-s3c24xx中,通道请求函数
        if (chan->flags & S3C2410_DMAF_AUTOSTART) {//如果设置为自动运行,就调用start函数
            s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
                     S3C2410_DMAOP_START);
        }
    没有判断S3C2410_DMAF_CIRCULAR标志
    */

    enum dma_data_direction direction;//dma传输方向
    enum dma_slave_buswidth width;//要传输的数据宽度,就是(字节、半字、字)
    dma_addr_t fifo;//外设地址
    struct s3c2410_dma_client *client;
    /*
    struct s3c2410_dma_client {
        char                *name;
    };
    就是一个name,你申请通道时命名就可以了,主要dma中断注册是用、free通道时判断
    是不是正确通道
    */
};

struct samsung_dma_ops {
    unsigned (*request)(enum dma_ch ch, struct samsung_dma_info *info);//请求会有些限制
    //1.总是认为我们的外围设备是一个固定的地址
    //2.总是认为我们的内存地址增加
    //3.硬件触发
    //4.所有传输完成产生中断
    //上面这个从数据手册上看是可以设的,但是三星写的驱动代码没有选的可能
    int (*release)(unsigned ch, struct s3c2410_dma_client *client);//释放
    int (*prepare)(unsigned ch, struct samsung_dma_prep_info *info);//准备
    //准备会把内存数据加入链表中,如果有数据在传输,会打开再加载开关
    int (*trigger)(unsigned ch);//触发
    int (*started)(unsigned ch);//再次开始,实际就是再次载入数据
    int (*flush)(unsigned ch);//清除通道数据
    int (*stop)(unsigned ch);//停止
};

extern void *samsung_dmadev_get_ops(void);
extern void *s3c_dma_get_ops(void);

//去获取一个struct samsung_dma_ops全局变量,
//然后调用驱动,这个倒是给我们提供了一种驱动之间调用的方法
static inline void *__samsung_dma_get_ops(void)
{
    if (samsung_dma_is_dmadev())
        return samsung_dmadev_get_ops();
    else
        return s3c_dma_get_ops();
}

/*
 * samsung_dma_get_ops
 * get the set of samsung dma operations
 */
//在驱动中调用这个
#define samsung_dma_get_ops() __samsung_dma_get_ops()

#endif /* __SAMSUNG_DMA_OPS_H_ */

如果你只是想看看应用接口,你可以在此打住,直接看《自娱自乐10》中dma使用的例子,增加你的理解。

如果你和我一样死脑筋,你可以看看下面的源码分析:主要有三个文件

下面这个文件,看的时候不要纠结,主要是为第二个通道使用的,知道就行。

/* linux/arch/arm/plat-samsung/dma.c
 *
 * Copyright (c) 2003-2009 Simtec Electronics
 *	Ben Dooks <[email protected]>
 *	http://armlinux.simtec.co.uk/
 *
 * S3C DMA core
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

struct s3c2410_dma_buf;
/*
struct s3c2410_dma_buf {
        struct s3c2410_dma_buf  *next;
        int                      magic;         // magic 
        int                      size;          // buffer size in bytes 
        dma_addr_t               data;          // start of DMA data 
        dma_addr_t               ptr;           // where the DMA got to [1] 
        void                    *id;            // client's id 
};

*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/errno.h>

#include <mach/dma.h>
#include <mach/irqs.h>

/* dma channel state information */
struct s3c2410_dma_chan s3c2410_chans[S3C_DMA_CHANNELS];
struct s3c2410_dma_chan *s3c_dma_chan_map[DMACH_MAX];

/* s3c_dma_lookup_channel
 *
 * change the dma channel number given into a real dma channel id
*/
//查找对应通道的struct s3c2410_dma_chan 
//这个通道不是dma每个通道,是指外设对应的。
struct s3c2410_dma_chan *s3c_dma_lookup_channel(unsigned int channel)
{
	if (channel & DMACH_LOW_LEVEL)
		return &s3c2410_chans[channel & ~DMACH_LOW_LEVEL];
	else
		return s3c_dma_chan_map[channel];
}

/* do we need to protect the settings of the fields from
 * irq?
*/

int s3c2410_dma_set_opfn(enum dma_ch channel, s3c2410_dma_opfn_t rtn)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	if (chan == NULL)
		return -EINVAL;

	pr_debug("%s: chan=%p, op rtn=%p\n", __func__, chan, rtn);

	chan->op_fn = rtn;
	//从下面可以看出在开始和结束时会调用这个函数

	return 0;
}
EXPORT_SYMBOL(s3c2410_dma_set_opfn);

int s3c2410_dma_set_buffdone_fn(enum dma_ch channel, s3c2410_dma_cbfn_t rtn)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	if (chan == NULL)
		return -EINVAL;

	pr_debug("%s: chan=%p, callback rtn=%p\n", __func__, chan, rtn);

	//在s3c2410_dma_flush和中断函数中调用
	chan->callback_fn = rtn;

	return 0;
}
EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);

//设置标志
int s3c2410_dma_setflags(enum dma_ch channel, unsigned int flags)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	if (chan == NULL)
		return -EINVAL;

	chan->flags = flags;
	return 0;
}
EXPORT_SYMBOL(s3c2410_dma_setflags);

下面是plat-s3c24xx中的,应该只适用s3c24xx芯片

文件有点大,我提供一个看的方法

1. 看init函数。

2. 看request函数。

3. 看s3c2410_dma_ctrl,里面有停止、开始等。

4. 看中断函数。

5. 看电源管理。

/* linux/arch/arm/plat-s3c24xx/dma.c
 *
 * Copyright 2003-2006 Simtec Electronics
 *	Ben Dooks <[email protected]>
 *
 * S3C2410 DMA core
 *
 * http://armlinux.simtec.co.uk/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/


#ifdef CONFIG_S3C2410_DMA_DEBUG
#define DEBUG
#endif

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/syscore_ops.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/io.h>

#include <asm/system.h>
#include <asm/irq.h>
#include <mach/hardware.h>
#include <mach/dma.h>
#include <mach/map.h>

#include <plat/dma-s3c24xx.h>
#include <plat/regs-dma.h>

/* io map for dma */
static void __iomem *dma_base;
static struct kmem_cache *dma_kmem;

static int dma_channels;

static struct s3c24xx_dma_selection dma_sel;

//调试功能就不看了

/* debugging functions */

#define BUF_MAGIC (0xcafebabe)

#define dmawarn(fmt...) printk(KERN_DEBUG fmt)

#define dma_regaddr(chan, reg) ((chan)->regs + (reg))

#if 1
#define dma_wrreg(chan, reg, val) writel((val), (chan)->regs + (reg))
#else
static inline void
dma_wrreg(struct s3c2410_dma_chan *chan, int reg, unsigned long val)
{
	pr_debug("writing %08x to register %08x\n",(unsigned int)val,reg);
	writel(val, dma_regaddr(chan, reg));
}
#endif

#define dma_rdreg(chan, reg) readl((chan)->regs + (reg))

/* captured register state for debug */

struct s3c2410_dma_regstate {
	unsigned long         dcsrc; //源
	unsigned long         disrc; //目标
	unsigned long         dstat; //状态
	unsigned long         dcon;  //配置
	unsigned long         dmsktrig; //触发屏蔽
};


//下面还是调试,不看了
#ifdef CONFIG_S3C2410_DMA_DEBUG

/* dmadbg_showregs
 *
 * simple debug routine to print the current state of the dma registers
*/

static void
dmadbg_capture(struct s3c2410_dma_chan *chan, struct s3c2410_dma_regstate *regs)
{
	regs->dcsrc    = dma_rdreg(chan, S3C2410_DMA_DCSRC);
	regs->disrc    = dma_rdreg(chan, S3C2410_DMA_DISRC);
	regs->dstat    = dma_rdreg(chan, S3C2410_DMA_DSTAT);
	regs->dcon     = dma_rdreg(chan, S3C2410_DMA_DCON);
	regs->dmsktrig = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
}

static void
dmadbg_dumpregs(const char *fname, int line, struct s3c2410_dma_chan *chan,
		 struct s3c2410_dma_regstate *regs)
{
	printk(KERN_DEBUG "dma%d: %s:%d: DCSRC=%08lx, DISRC=%08lx, DSTAT=%08lx DMT=%02lx, DCON=%08lx\n",
	       chan->number, fname, line,
	       regs->dcsrc, regs->disrc, regs->dstat, regs->dmsktrig,
	       regs->dcon);
}

static void
dmadbg_showchan(const char *fname, int line, struct s3c2410_dma_chan *chan)
{
	struct s3c2410_dma_regstate state;

	dmadbg_capture(chan, &state);

	printk(KERN_DEBUG "dma%d: %s:%d: ls=%d, cur=%p, %p %p\n",
	       chan->number, fname, line, chan->load_state,
	       chan->curr, chan->next, chan->end);

	dmadbg_dumpregs(fname, line, chan, &state);
}

static void
dmadbg_showregs(const char *fname, int line, struct s3c2410_dma_chan *chan)
{
	struct s3c2410_dma_regstate state;

	dmadbg_capture(chan, &state);
	dmadbg_dumpregs(fname, line, chan, &state);
}

#define dbg_showregs(chan) dmadbg_showregs(__func__, __LINE__, (chan))
#define dbg_showchan(chan) dmadbg_showchan(__func__, __LINE__, (chan))
#else
#define dbg_showregs(chan) do { } while(0)
#define dbg_showchan(chan) do { } while(0)
#endif /* CONFIG_S3C2410_DMA_DEBUG */

/* s3c2410_dma_stats_timeout
 *
 * Update DMA stats from timeout info
*/
//记录dma传输用时
static void
s3c2410_dma_stats_timeout(struct s3c2410_dma_stats *stats, int val)
{
	if (stats == NULL)
		return;

	if (val > stats->timeout_longest)
		stats->timeout_longest = val;
	if (val < stats->timeout_shortest)
		stats->timeout_shortest = val;

	stats->timeout_avg += val;
}

/* s3c2410_dma_waitforload
 *
 * wait for the DMA engine to load a buffer, and update the state accordingly
*/
//等待DMA引擎载入一个缓冲
static int
s3c2410_dma_waitforload(struct s3c2410_dma_chan *chan, int line)
{
	int timeout = chan->load_timeout;//1 << 18
	int took;

	if (chan->load_state != S3C2410_DMALOAD_1LOADED) {
		printk(KERN_ERR "dma%d: s3c2410_dma_waitforload() called in loadstate %d from line %d\n", chan->number, chan->load_state, line);
		return 0;
	}

	if (chan->stats != NULL)
		chan->stats->loads++;//载入加一

	while (--timeout > 0) {
		if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {//左移11位,如果等于0,就表示DMA控制器就绪且传输计数值为0
			took = chan->load_timeout - timeout;//已计时间

			s3c2410_dma_stats_timeout(chan->stats, took);//在上面

			switch (chan->load_state) {
			case S3C2410_DMALOAD_1LOADED:
				chan->load_state = S3C2410_DMALOAD_1RUNNING;
				break;

			default:
				printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
			}

			return 1;
		}
	}

	if (chan->stats != NULL) {
		chan->stats->timeout_failed++;//超时,错误加一
	}

	return 0;
}

/* s3c2410_dma_loadbuffer
 *
 * load a buffer, and update the channel state
*/
//载入
static inline int
s3c2410_dma_loadbuffer(struct s3c2410_dma_chan *chan,
		       struct s3c2410_dma_buf *buf)
{
	unsigned long reload;

	if (buf == NULL) {
		dmawarn("buffer is NULL\n");
		return -EINVAL;
	}

	pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
		 buf, (unsigned long)buf->data, buf->size);

	/* check the state of the channel before we do anything */
    //状态检查,并警告
	if (chan->load_state == S3C2410_DMALOAD_1LOADED) {//有一个载入
		dmawarn("load_state is S3C2410_DMALOAD_1LOADED\n");
	}

	if (chan->load_state == S3C2410_DMALOAD_1LOADED_1RUNNING) {//一个载入一个运行
		dmawarn("state is S3C2410_DMALOAD_1LOADED_1RUNNING\n");
	}

	/* it would seem sensible if we are the last buffer to not bother
	 * with the auto-reload bit, so that the DMA engine will not try
	 * and load another transfer after this one has finished...
	 */
	if (chan->load_state == S3C2410_DMALOAD_NONE) {
		pr_debug("load_state is none, checking for noreload (next=%p)\n",
			 buf->next);
		//next为空说明是最后一个,就不需要再次载入
		reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
	} else {
		//pr_debug("load_state is %d => autoreload\n", chan->load_state);
		reload = S3C2410_DCON_AUTORELOAD;
	}

	if ((buf->data & 0xf0000000) != 0x30000000) {//内存物理地址从0x30000000开始
		dmawarn("dmaload: buffer is %p\n", (void *)buf->data);
	}

	writel(buf->data, chan->addr_reg);//数据写入

	dma_wrreg(chan, S3C2410_DMA_DCON,
		  chan->dcon | reload | (buf->size/chan->xfer_unit));//配置

	chan->next = buf->next;

	/* update the state of the channel */

	switch (chan->load_state) {
	case S3C2410_DMALOAD_NONE:
		chan->load_state = S3C2410_DMALOAD_1LOADED;
		break;

	case S3C2410_DMALOAD_1RUNNING:
		chan->load_state = S3C2410_DMALOAD_1LOADED_1RUNNING;
		break;

	default:
		dmawarn("dmaload: unknown state %d in loadbuffer\n",
			chan->load_state);
		break;
	}

	return 0;
}

/* s3c2410_dma_call_op
 *
 * small routine to call the op routine with the given op if it has been
 * registered
*/

static void
s3c2410_dma_call_op(struct s3c2410_dma_chan *chan, enum s3c2410_chan_op op)
{
	//通过s3c2410_dma_set_opfn函数赋值
	if (chan->op_fn != NULL) {
		(chan->op_fn)(chan, op);
	}
}

/* s3c2410_dma_buffdone
 *
 * small wrapper to check if callback routine needs to be called, and
 * if so, call it
*/

static inline void
s3c2410_dma_buffdone(struct s3c2410_dma_chan *chan, struct s3c2410_dma_buf *buf,
		     enum s3c2410_dma_buffresult result)
{
#if 0
	pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
		 chan->callback_fn, buf, buf->id, buf->size, result);
#endif

	if (chan->callback_fn != NULL) {
		(chan->callback_fn)(chan, buf->id, buf->size, result);
	}
}

/* s3c2410_dma_start
 *
 * start a dma channel going
*/

static int s3c2410_dma_start(struct s3c2410_dma_chan *chan)
{
	unsigned long tmp;
	unsigned long flags;

	pr_debug("s3c2410_start_dma: channel=%d\n", chan->number);

	local_irq_save(flags);

	if (chan->state == S3C2410_DMA_RUNNING) {
		pr_debug("s3c2410_start_dma: already running (%d)\n", chan->state);
		local_irq_restore(flags);
		return 0;
	}

	//记录状态
	chan->state = S3C2410_DMA_RUNNING;

	/* check wether there is anything to load, and if not, see
	 * if we can find anything to load
	 */
    //判断是否已载入载入
	if (chan->load_state == S3C2410_DMALOAD_NONE) {
		if (chan->next == NULL) {
			printk(KERN_ERR "dma%d: channel has nothing loaded\n",
			       chan->number);
			chan->state = S3C2410_DMA_IDLE;//没有可载入的state记为空闲
			local_irq_restore(flags);
			return -EINVAL;
		}

		s3c2410_dma_loadbuffer(chan, chan->next);//载入缓存
	}

	dbg_showchan(chan);//调试信息

	/* enable the channel */

	if (!chan->irq_enabled) {
		enable_irq(chan->irq);//如果中断关闭,开启中断
		chan->irq_enabled = 1;
	}

	/* start the channel going */

	tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
	tmp &= ~S3C2410_DMASKTRIG_STOP;
	tmp |= S3C2410_DMASKTRIG_ON;
	dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);//开始运行

	pr_debug("dma%d: %08lx to DMASKTRIG\n", chan->number, tmp);

#if 0
	/* the dma buffer loads should take care of clearing the AUTO
	 * reloading feature */
	tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
	tmp &= ~S3C2410_DCON_NORELOAD;
	dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
#endif

	s3c2410_dma_call_op(chan, S3C2410_DMAOP_START);//回调

	dbg_showchan(chan);

	/* if we've only loaded one buffer onto the channel, then chec
	 * to see if we have another, and if so, try and load it so when
	 * the first buffer is finished, the new one will be loaded onto
	 * the channel */

	if (chan->next != NULL) {
		if (chan->load_state == S3C2410_DMALOAD_1LOADED) {//已有一个正在载入

			if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {//等待...
				pr_debug("%s: buff not yet loaded, no more todo\n",
					 __func__);
			} else {
				//这里还有下面的,可以看出在s3c2410_dma_loadbuffer中
				//S3C2410_DMALOAD_1RUNNING会使用AUTORELOAD模式
				chan->load_state = S3C2410_DMALOAD_1RUNNING;
				s3c2410_dma_loadbuffer(chan, chan->next);
			}

		} else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
			s3c2410_dma_loadbuffer(chan, chan->next);
		}
	}


	local_irq_restore(flags);

	return 0;
}

/* s3c2410_dma_canload
 *
 * work out if we can queue another buffer into the DMA engine
*/

static int
s3c2410_dma_canload(struct s3c2410_dma_chan *chan)
{
	if (chan->load_state == S3C2410_DMALOAD_NONE ||
	    chan->load_state == S3C2410_DMALOAD_1RUNNING)
		return 1;

	return 0;
}

/* s3c2410_dma_enqueue
 *
 * queue an given buffer for dma transfer.
 *
 * id         the device driver's id information for this buffer
 * data       the physical address of the buffer data
 * size       the size of the buffer in bytes
 *
 * If the channel is not running, then the flag S3C2410_DMAF_AUTOSTART
 * is checked, and if set, the channel is started. If this flag isn't set,
 * then an error will be returned.
 *
 * It is possible to queue more than one DMA buffer onto a channel at
 * once, and the code will deal with the re-loading of the next buffer
 * when necessary.
*/
//把数据加入链表
int s3c2410_dma_enqueue(unsigned int channel, void *id,
			dma_addr_t data, int size)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
	struct s3c2410_dma_buf *buf;
	unsigned long flags;

	if (chan == NULL)
		return -EINVAL;

	pr_debug("%s: id=%p, data=%08x, size=%d\n",
		 __func__, id, (unsigned int)data, size);

	buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);//申请内存
	if (buf == NULL) {
		pr_debug("%s: out of memory (%ld alloc)\n",
			 __func__, (long)sizeof(*buf));
		return -ENOMEM;
	}

	//pr_debug("%s: new buffer %p\n", __func__, buf);
	//dbg_showchan(chan);

	//给buf赋值
	buf->next  = NULL;
	buf->data  = buf->ptr = data;
	buf->size  = size;
	buf->id    = id;
	buf->magic = BUF_MAGIC;

	local_irq_save(flags);

	//chan->curr赋值并把buf加入链表中
	if (chan->curr == NULL) {
		/* we've got nothing loaded... */
		pr_debug("%s: buffer %p queued onto empty channel\n",
			 __func__, buf);

		chan->curr = buf;
		chan->end  = buf;
		chan->next = NULL;
	} else {
		pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
			 chan->number, __func__, buf);

		if (chan->end == NULL)
			pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
				 chan->number, __func__, chan);

		chan->end->next = buf;
		chan->end = buf;
	}

	/* if necessary, update the next buffer field */
	if (chan->next == NULL)
		chan->next = buf;

	/* check to see if we can load a buffer */
	if (chan->state == S3C2410_DMA_RUNNING) {
		if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {//当前是否有一个在传输
			if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {//等待
				printk(KERN_ERR "dma%d: loadbuffer:"
				       "timeout loading buffer\n",
				       chan->number);
				dbg_showchan(chan);
				local_irq_restore(flags);
				return -EINVAL;
			}
		}

		while (s3c2410_dma_canload(chan) && chan->next != NULL) {//可加载且chan->next不为空
			s3c2410_dma_loadbuffer(chan, chan->next);
		}
	} else if (chan->state == S3C2410_DMA_IDLE) {
		if (chan->flags & S3C2410_DMAF_AUTOSTART) {//如果设置为自动运行,就调用start函数
			s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
					 S3C2410_DMAOP_START);
		}
	}

	local_irq_restore(flags);
	return 0;
}

EXPORT_SYMBOL(s3c2410_dma_enqueue);

static inline void
s3c2410_dma_freebuf(struct s3c2410_dma_buf *buf)
{
	int magicok = (buf->magic == BUF_MAGIC);

	buf->magic = -1;

	if (magicok) {
		kmem_cache_free(dma_kmem, buf);
	} else {
		printk("s3c2410_dma_freebuf: buff %p with bad magic\n", buf);
	}
}

/* s3c2410_dma_lastxfer
 *
 * called when the system is out of buffers, to ensure that the channel
 * is prepared for shutdown.
*/

static inline void
s3c2410_dma_lastxfer(struct s3c2410_dma_chan *chan)
{
#if 0
	pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
		 chan->number, chan->load_state);
#endif

	switch (chan->load_state) {
	case S3C2410_DMALOAD_NONE:
		break;

	case S3C2410_DMALOAD_1LOADED:
		if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
				/* flag error? */
			printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
			       chan->number, __func__);
			return;
		}
		break;

	case S3C2410_DMALOAD_1LOADED_1RUNNING:
		/* I believe in this case we do not have anything to do
		 * until the next buffer comes along, and we turn off the
		 * reload */
		return;

	default:
		pr_debug("dma%d: lastxfer: unhandled load_state %d with no next\n",
			 chan->number, chan->load_state);
		return;

	}

	/* hopefully this'll shut the damned thing up after the transfer... */
	dma_wrreg(chan, S3C2410_DMA_DCON, chan->dcon | S3C2410_DCON_NORELOAD);
}


#define dmadbg2(x...)

static irqreturn_t
s3c2410_dma_irq(int irq, void *devpw)
{
	struct s3c2410_dma_chan *chan = (struct s3c2410_dma_chan *)devpw;
	struct s3c2410_dma_buf  *buf;

	buf = chan->curr;

	dbg_showchan(chan);

	/* modify the channel state */
    //更新状态
	switch (chan->load_state) {
	case S3C2410_DMALOAD_1RUNNING:
		/* TODO - if we are running only one buffer, we probably
		 * want to reload here, and then worry about the buffer
		 * callback */
        
		chan->load_state = S3C2410_DMALOAD_NONE;
		break;

	case S3C2410_DMALOAD_1LOADED:
		/* iirc, we should go back to NONE loaded here, we
		 * had a buffer, and it was never verified as being
		 * loaded.
		 */

		chan->load_state = S3C2410_DMALOAD_NONE;
		break;

	case S3C2410_DMALOAD_1LOADED_1RUNNING:
		/* we'll worry about checking to see if another buffer is
		 * ready after we've called back the owner. This should
		 * ensure we do not wait around too long for the DMA
		 * engine to start the next transfer
		 */

		chan->load_state = S3C2410_DMALOAD_1LOADED;
		break;

	case S3C2410_DMALOAD_NONE:
		printk(KERN_ERR "dma%d: IRQ with no loaded buffer?\n",
		       chan->number);
		break;

	default:
		printk(KERN_ERR "dma%d: IRQ in invalid load_state %d\n",
		       chan->number, chan->load_state);
		break;
	}

	if (buf != NULL) {
		/* update the chain to make sure that if we load any more
		 * buffers when we call the callback function, things should
		 * work properly */

		//更新链表
		chan->curr = buf->next;
		buf->next  = NULL;

		if (buf->magic != BUF_MAGIC) {
			printk(KERN_ERR "dma%d: %s: buf %p incorrect magic\n",
			       chan->number, __func__, buf);
			return IRQ_HANDLED;
		}

		s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);//回调,使一些事情工作正常

		/* free resouces */
		s3c2410_dma_freebuf(buf);//释放空间
	} else {
	}

	/* only reload if the channel is still running... our buffer done
	 * routine may have altered the state by requesting the dma channel
	 * to stop or shutdown... */

	/* todo: check that when the channel is shut-down from inside this
	 * function, we cope with unsetting reload, etc */

	if (chan->next != NULL && chan->state != S3C2410_DMA_IDLE) {
		unsigned long flags;

		switch (chan->load_state) {
		case S3C2410_DMALOAD_1RUNNING:
			/* don't need to do anything for this state */
			break;

		case S3C2410_DMALOAD_NONE:
			/* can load buffer immediately */
			break;

		case S3C2410_DMALOAD_1LOADED:
			if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {//等待传输完
				/* flag error? */
				printk(KERN_ERR "dma%d: timeout waiting for load (%s)\n",
				       chan->number, __func__);
				return IRQ_HANDLED;
			}

			break;

		case S3C2410_DMALOAD_1LOADED_1RUNNING:
			goto no_load;

		default:
			printk(KERN_ERR "dma%d: unknown load_state in irq, %d\n",
			       chan->number, chan->load_state);
			return IRQ_HANDLED;
		}

		local_irq_save(flags);
		s3c2410_dma_loadbuffer(chan, chan->next);//再次载入
		local_irq_restore(flags);
	} else {
		s3c2410_dma_lastxfer(chan);//最后一个缓冲区

		/* see if we can stop this channel.. */
		if (chan->load_state == S3C2410_DMALOAD_NONE) {
			pr_debug("dma%d: end of transfer, stopping channel (%ld)\n",
				 chan->number, jiffies);
			s3c2410_dma_ctrl(chan->number | DMACH_LOW_LEVEL,
					 S3C2410_DMAOP_STOP);//停止
		}
	}

 no_load:
	return IRQ_HANDLED;
}

static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel);

/* s3c2410_request_dma
 *
 * get control of an dma channel
*/
//获取一个dma通道
//下面你会看到,获取一个通道信息并注册中断
int s3c2410_dma_request(enum dma_ch channel,
			struct s3c2410_dma_client *client,
			void *dev)
{
	struct s3c2410_dma_chan *chan;
	unsigned long flags;
	int err;

	pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
		 channel, client->name, dev);

	local_irq_save(flags);

	//请求一个通道,下面有详细说明
	chan = s3c2410_dma_map_channel(channel);
	if (chan == NULL) {
		local_irq_restore(flags);
		return -EBUSY;
	}

	dbg_showchan(chan);

	chan->client = client;
	chan->in_use = 1;//标志为使用

	if (!chan->irq_claimed) {
		pr_debug("dma%d: %s : requesting irq %d\n",
			 channel, __func__, chan->irq);

		chan->irq_claimed = 1;//中断声明标志为1
		local_irq_restore(flags);

		err = request_irq(chan->irq, s3c2410_dma_irq, IRQF_DISABLED,
				  client->name, (void *)chan);

		local_irq_save(flags);

		if (err) {
			chan->in_use = 0;
			chan->irq_claimed = 0;
			local_irq_restore(flags);

			printk(KERN_ERR "%s: cannot get IRQ %d for DMA %d\n",
			       client->name, chan->irq, chan->number);
			return err;
		}

		chan->irq_enabled = 1;
	}

	local_irq_restore(flags);

	/* need to setup */

	pr_debug("%s: channel initialised, %p\n", __func__, chan);

	return chan->number | DMACH_LOW_LEVEL;
}

EXPORT_SYMBOL(s3c2410_dma_request);

/* s3c2410_dma_free
 *
 * release the given channel back to the system, will stop and flush
 * any outstanding transfers, and ensure the channel is ready for the
 * next claimant.
 *
 * Note, although a warning is currently printed if the freeing client
 * info is not the same as the registrant's client info, the free is still
 * allowed to go through.
*/

int s3c2410_dma_free(enum dma_ch channel, struct s3c2410_dma_client *client)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
	unsigned long flags;

	if (chan == NULL)
		return -EINVAL;

	local_irq_save(flags);

	if (chan->client != client) {//检查是否为正确通道
		printk(KERN_WARNING "dma%d: possible free from different client (channel %p, passed %p)\n",
		       channel, chan->client, client);
	}

	/* sort out stopping and freeing the channel */

	if (chan->state != S3C2410_DMA_IDLE) {
		pr_debug("%s: need to stop dma channel %p\n",
		       __func__, chan);

		/* possibly flush the channel */
		s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);//不是空闲,就停止通道
	}

	chan->client = NULL;
	chan->in_use = 0;

	if (chan->irq_claimed)
		free_irq(chan->irq, (void *)chan);//如果注册的中断就释放

	chan->irq_claimed = 0;

	if (!(channel & DMACH_LOW_LEVEL))
		s3c_dma_chan_map[channel] = NULL;//对应的数据置空

	local_irq_restore(flags);

	return 0;
}

EXPORT_SYMBOL(s3c2410_dma_free);

static int s3c2410_dma_dostop(struct s3c2410_dma_chan *chan)
{
	unsigned long flags;
	unsigned long tmp;

	pr_debug("%s:\n", __func__);

	dbg_showchan(chan);//调试不看了

	local_irq_save(flags);

	s3c2410_dma_call_op(chan,  S3C2410_DMAOP_STOP);
	//(chan->op_fn)(chan, op); 回调函数

	tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);//读对应通道的寄存器
	tmp |= S3C2410_DMASKTRIG_STOP;//DMASKTRIG的第2位是STOP
	//tmp &= ~S3C2410_DMASKTRIG_ON;
	dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);

#if 0 //为了适合WinCE,关闭再加载
	/* should also clear interrupts, according to WinCE BSP */
	tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
	tmp |= S3C2410_DCON_NORELOAD;
	dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
#endif

	/* should stop do this, or should we wait for flush? */
	chan->state      = S3C2410_DMA_IDLE;//dma状态
	chan->load_state = S3C2410_DMALOAD_NONE;//dma数据传输状态

	local_irq_restore(flags);

	return 0;
}

static void s3c2410_dma_waitforstop(struct s3c2410_dma_chan *chan)
{
	unsigned long tmp;
	unsigned int timeout = 0x10000;

	while (timeout-- > 0) {
		tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);

		if (!(tmp & S3C2410_DMASKTRIG_ON))//判断DMASKTRIG寄存器第1位是否为0
			return;
	}

	pr_debug("dma%d: failed to stop?\n", chan->number);
}


/* s3c2410_dma_flush
 *
 * stop the channel, and remove all current and pending transfers
*/
//停止通道,并移除当前和即将发生的传输
static int s3c2410_dma_flush(struct s3c2410_dma_chan *chan)
{
	struct s3c2410_dma_buf *buf, *next;
	unsigned long flags;

	pr_debug("%s: chan %p (%d)\n", __func__, chan, chan->number);

	dbg_showchan(chan);

	local_irq_save(flags);

	if (chan->state != S3C2410_DMA_IDLE) {
		pr_debug("%s: stopping channel...\n", __func__ );
		//不是空闲,调用s3c2410_dma_dostop
		s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_STOP);
	}

	buf = chan->curr;
	if (buf == NULL)
		buf = chan->next;

	//清除数据
	chan->curr = chan->next = chan->end = NULL;

	//释放空间
	if (buf != NULL) {
		for ( ; buf != NULL; buf = next) {
			next = buf->next;

			pr_debug("%s: free buffer %p, next %p\n",
			       __func__, buf, buf->next);

			s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);//回调函数调用
			s3c2410_dma_freebuf(buf);//就是调用kmem_cache_free释放
		}
	}

	dbg_showregs(chan);

	s3c2410_dma_waitforstop(chan);//等待停止

#if 0
	/* should also clear interrupts, according to WinCE BSP */
	{
		unsigned long tmp;

		tmp = dma_rdreg(chan, S3C2410_DMA_DCON);
		tmp |= S3C2410_DCON_NORELOAD;
		dma_wrreg(chan, S3C2410_DMA_DCON, tmp);
	}
#endif

	dbg_showregs(chan);

	local_irq_restore(flags);

	return 0;
}

static int s3c2410_dma_started(struct s3c2410_dma_chan *chan)
{
	unsigned long flags;

	local_irq_save(flags);

	dbg_showchan(chan);

	/* if we've only loaded one buffer onto the channel, then chec
	 * to see if we have another, and if so, try and load it so when
	 * the first buffer is finished, the new one will be loaded onto
	 * the channel */

	//下面的判断可以查看s3c2410_dma_start()
	if (chan->next != NULL) {
		if (chan->load_state == S3C2410_DMALOAD_1LOADED) {

			if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
				pr_debug("%s: buff not yet loaded, no more todo\n",
					 __func__);
			} else {
				chan->load_state = S3C2410_DMALOAD_1RUNNING;
				s3c2410_dma_loadbuffer(chan, chan->next);
			}

		} else if (chan->load_state == S3C2410_DMALOAD_1RUNNING) {
			s3c2410_dma_loadbuffer(chan, chan->next);
		}
	}


	local_irq_restore(flags);

	return 0;

}

//dma 控制函数
int
s3c2410_dma_ctrl(enum dma_ch channel, enum s3c2410_chan_op op)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	if (chan == NULL)
		return -EINVAL;

	switch (op) {
	case S3C2410_DMAOP_START://开始
		return s3c2410_dma_start(chan);

	case S3C2410_DMAOP_STOP://停止
		return s3c2410_dma_dostop(chan);

	case S3C2410_DMAOP_PAUSE://暂停
	case S3C2410_DMAOP_RESUME:
		return -ENOENT;

	case S3C2410_DMAOP_FLUSH://冲掉
		return s3c2410_dma_flush(chan);

	case S3C2410_DMAOP_STARTED://再次开始
		return s3c2410_dma_started(chan);

	case S3C2410_DMAOP_TIMEOUT://超时
		return 0;

	}

	return -ENOENT;      /* unknown, don't bother */
}

EXPORT_SYMBOL(s3c2410_dma_ctrl);

/* DMA configuration for each channel
 *
 * DISRCC -> source of the DMA (AHB,APB)
 * DISRC  -> source address of the DMA
 * DIDSTC -> destination of the DMA (AHB,APD)
 * DIDST  -> destination address of the DMA
*/

/* s3c2410_dma_config
 *
 * xfersize:     size of unit in bytes (1,2,4)
*/

int s3c2410_dma_config(enum dma_ch channel,
		       int xferunit)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
	unsigned int dcon;

	pr_debug("%s: chan=%d, xfer_unit=%d\n", __func__, channel, xferunit);

	if (chan == NULL)
		return -EINVAL;

	dcon = chan->dcon & dma_sel.dcon_mask;
	pr_debug("%s: dcon is %08x\n", __func__, dcon);

	//DCON寄存器的24~26位选择请求源
	switch (chan->req_ch) {
	case DMACH_I2S_IN:
	case DMACH_I2S_OUT:
	case DMACH_PCM_IN:
	case DMACH_PCM_OUT:
	case DMACH_MIC_IN:
	default:
		dcon |= S3C2410_DCON_HANDSHAKE;
		dcon |= S3C2410_DCON_SYNC_PCLK;
		break;

	case DMACH_SDI:
		/* note, ensure if need HANDSHAKE or not */
		dcon |= S3C2410_DCON_SYNC_PCLK;
		break;

	case DMACH_XD0:
	case DMACH_XD1:
		dcon |= S3C2410_DCON_HANDSHAKE;
		dcon |= S3C2410_DCON_SYNC_HCLK;
		break;
	}
	//上面的HANDSHAKE是选择握手模式
	//除了SDI要选,其他的都是握手模式,chan->dcon来记录你的dcon的配置

	//传输大小
	switch (xferunit) {
	case 1:
		dcon |= S3C2410_DCON_BYTE;
		break;

	case 2:
		dcon |= S3C2410_DCON_HALFWORD;
		break;

	case 4:
		dcon |= S3C2410_DCON_WORD;
		break;

	default:
		pr_debug("%s: bad transfer size %d\n", __func__, xferunit);
		return -EINVAL;
	}

	dcon |= S3C2410_DCON_HWTRIG;//硬件触发
	dcon |= S3C2410_DCON_INTREQ;//所有传输完成产生中断

	pr_debug("%s: dcon now %08x\n", __func__, dcon);

	//记录下配置
	chan->dcon = dcon;
	chan->xfer_unit = xferunit;

	return 0;
}

EXPORT_SYMBOL(s3c2410_dma_config);


/* s3c2410_dma_devconfig
 *
 * configure the dma source/destination hardware type and address
 *
 * source:    DMA_FROM_DEVICE: source is hardware
 *            DMA_TO_DEVICE: source is memory
 *
 * devaddr:   physical address of the source
*/

int s3c2410_dma_devconfig(enum dma_ch channel,
			  enum dma_data_direction source,
			  unsigned long devaddr)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);
	unsigned int hwcfg;

	if (chan == NULL)
		return -EINVAL;

	pr_debug("%s: source=%d, devaddr=%08lx\n",
		 __func__, (int)source, devaddr);

	chan->source = source;//源类型,内存或者硬件
	chan->dev_addr = devaddr;//

	switch (chan->req_ch) {
	case DMACH_XD0:
	case DMACH_XD1:
		hwcfg = 0; /* AHB */
		break;

	default:
		hwcfg = S3C2410_DISRCC_APB;
	}
    //除了XD0和XD1在AHB总线上,其他都是APB

	/* always assume our peripheral desintation is a fixed
	 * address in memory. */
	//总是认为我们的外围设备是一个固定的地址
	hwcfg |= S3C2410_DISRCC_INC;//在传输后地址不改变

	switch (source) {
	case DMA_FROM_DEVICE://源是硬件
		/* source is hardware */
		pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
			 __func__, devaddr, hwcfg);
		dma_wrreg(chan, S3C2410_DMA_DISRCC, hwcfg & 3);//源控制
		dma_wrreg(chan, S3C2410_DMA_DISRC,  devaddr);//源地址
		dma_wrreg(chan, S3C2410_DMA_DIDSTC, (0<<1) | (0<<0));//目标控制:系统总线,地址增加

		chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);//记下内存地址
		break;

	case DMA_TO_DEVICE://源是内存
		/* source is memory */
		pr_debug("%s: mem source, devaddr=%08lx, hwcfg=%d\n",
			 __func__, devaddr, hwcfg);
		dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));//源:系统总线,地址增加
		dma_wrreg(chan, S3C2410_DMA_DIDST,  devaddr);//目标地址地址
		dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);//目标控制

		chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);//记下内存地址
		break;

	default:
		printk(KERN_ERR "dma%d: invalid source type (%d)\n",
		       channel, source);

		return -EINVAL;
	}

	if (dma_sel.direction != NULL)//s3c2440为空
		(dma_sel.direction)(chan, chan->map, source);

	return 0;
}

EXPORT_SYMBOL(s3c2410_dma_devconfig);

/* s3c2410_dma_getposition
 *
 * returns the current transfer points for the dma source and destination
*/

//当前源或目标地址
int s3c2410_dma_getposition(enum dma_ch channel, dma_addr_t *src, dma_addr_t *dst)
{
	struct s3c2410_dma_chan *chan = s3c_dma_lookup_channel(channel);

	if (chan == NULL)
		return -EINVAL;

	if (src != NULL)
 		*src = dma_rdreg(chan, S3C2410_DMA_DCSRC);

 	if (dst != NULL)
 		*dst = dma_rdreg(chan, S3C2410_DMA_DCDST);

 	return 0;
}

EXPORT_SYMBOL(s3c2410_dma_getposition);

/* system core operations */

#ifdef CONFIG_PM

static void s3c2410_dma_suspend_chan(struct s3c2410_dma_chan *cp)
{
	printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);

	if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {
		/* the dma channel is still working, which is probably
		 * a bad thing to do over suspend/resume. We stop the
		 * channel and assume that the client is either going to
		 * retry after resume, or that it is broken.
		 */

		printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
		       cp->number);

		s3c2410_dma_dostop(cp);
	}
}

static int s3c2410_dma_suspend(void)
{
	struct s3c2410_dma_chan *cp = s3c2410_chans;
	int channel;

	for (channel = 0; channel < dma_channels; cp++, channel++)
		s3c2410_dma_suspend_chan(cp);

	return 0;
}

static void s3c2410_dma_resume_chan(struct s3c2410_dma_chan *cp)
{
	unsigned int no = cp->number | DMACH_LOW_LEVEL;

	/* restore channel's hardware configuration */

	if (!cp->in_use)
		return;

	printk(KERN_INFO "dma%d: restoring configuration\n", cp->number);

	s3c2410_dma_config(no, cp->xfer_unit);
	s3c2410_dma_devconfig(no, cp->source, cp->dev_addr);

	/* re-select the dma source for this channel */

	if (cp->map != NULL)
		dma_sel.select(cp, cp->map);
}

static void s3c2410_dma_resume(void)
{
	struct s3c2410_dma_chan *cp = s3c2410_chans + dma_channels - 1;
	int channel;

	for (channel = dma_channels - 1; channel >= 0; cp++, channel--)
		s3c2410_dma_resume_chan(cp);
}

#else
#define s3c2410_dma_suspend NULL
#define s3c2410_dma_resume  NULL
#endif /* CONFIG_PM */

/*
struct syscore_ops {
        struct list_head node;
        int (*suspend)(void);
        void (*resume)(void);
        void (*shutdown)(void);
};
这个syscore是通过一个全局的list管理的,如下
LIST_HEAD(syscore_ops_list);
*/

struct syscore_ops dma_syscore_ops = {
	.suspend	= s3c2410_dma_suspend,
	.resume		= s3c2410_dma_resume,
};

/* kmem cache implementation */
//下面看到
static void s3c2410_dma_cache_ctor(void *p)
{
	memset(p, 0, sizeof(struct s3c2410_dma_buf));
}

/* initialisation code */

static int __init s3c24xx_dma_syscore_init(void)
{
//注册的实质就是
//list_add_tail(&ops->node, &syscore_ops_list);
//加入链表
	register_syscore_ops(&dma_syscore_ops);

	return 0;
}

//late_initcall启动时会执行,在那个阶段我们就不管了
late_initcall(s3c24xx_dma_syscore_init);
/*
下面这个函数告诉我们这些__init函数的调用位置
static int __init s3c2440_dma_add(struct sys_device *sysdev)
{
        s3c2410_dma_init();
        s3c24xx_dma_order_set(&s3c2440_dma_order);
        return s3c24xx_dma_init_map(&s3c2440_dma_sel);
}

static struct sysdev_driver s3c2440_dma_driver = {
        .add    = s3c2440_dma_add,
};

static int __init s3c2440_dma_init(void)
{
        return sysdev_driver_register(&s3c2440_sysclass, &s3c2440_dma_driver);
}

struct sysdev_class s3c2440_sysclass = {
        .name           = "s3c2440-core",
};

static struct sys_device s3c2440_sysdev = {
        .cls            = &s3c2440_sysclass,
};

在s3c2440_init()中会调用
sysdev_register(&s3c2440_sysdev);
我们从这可以看到设备与驱动的概念,当然这不是我们的重点,我只说想说
s3c2440_dma_add初始化会被调用,我们就安初始化调用过程来看代码

启动时调用
arch_initcall(s3c2440_dma_init);
*/

//s3c24xx_dma_init在下面s3c2410_dma_init()被调用
//s3c2440或s3c2410平台s3c24xx_dma_init(4, IRQ_DMA0, 0x40);
//4个通道,中断号为IRQ_DMA0,0x40 * 4就是io要映射的大小
int __init s3c24xx_dma_init(unsigned int channels, unsigned int irq,
			    unsigned int stride)
{
	struct s3c2410_dma_chan *cp;
	int channel;
	int ret;

	printk("S3C24XX DMA Driver, Copyright 2003-2006 Simtec Electronics\n");

	dma_channels = channels;

    //寄存器映射
	dma_base = ioremap(S3C24XX_PA_DMA, stride * channels);
	if (dma_base == NULL) {
		printk(KERN_ERR "dma failed to remap register block\n");
		return -ENOMEM;
	}

	//申请后备缓存
	dma_kmem = kmem_cache_create("dma_desc",
				     sizeof(struct s3c2410_dma_buf), 0,
				     SLAB_HWCACHE_ALIGN,
				     s3c2410_dma_cache_ctor);
	/*
	原型:
    struct kmem_cache *kmem_cache_create(const char *name, size_t size,
                size_t align, unsigned long flags, void (*ctor)(void *))
    SLAB_HWCACHE_ALIGN:
	这个标志需要每个数据对象被对齐到一个缓存行; 实际对齐依赖主机平
    台的缓存分布. 这个选项可以是一个好的选择, 如果在 SMP 机器上你的
    缓存包含频繁存取的项. 但是, 用来获得缓存行对齐的填充可以浪费可
    观的内存量. 
	s3c2410_dma_cache_ctor:
	构造函数,上面可以看到就是把申请的内存空间清零。
	*/

	if (dma_kmem == NULL) {
		printk(KERN_ERR "dma failed to make kmem cache\n");
		ret = -ENOMEM;
		goto err;
	}
    /*
    在mach-s3c2410/include/mach/dma.h下定义的结构体
    struct s3c2410_dma_chan s3c2410_chans[S3C_DMA_CHANNELS];
    */
	for (channel = 0; channel < channels;  channel++) {
		cp = &s3c2410_chans[channel];

		memset(cp, 0, sizeof(struct s3c2410_dma_chan));

		/* dma channel irqs are in order.. */
		cp->number = channel; //通道号
		cp->irq    = channel + irq; //中断号 
		cp->regs   = dma_base + (channel * stride); //寄存器基地址

		/* point current stats somewhere */
		/*
        struct s3c2410_dma_stats *stats;
        struct s3c2410_dma_stats  stats_store;
		下面就是个指针初始化
		*/
		cp->stats  = &cp->stats_store;
		cp->stats_store.timeout_shortest = LONG_MAX;//#define LONG_MAX       ((long)(~0UL>>1))

		/* basic channel configuration */

		cp->load_timeout = 1<<18;//向dma载入缓冲的超时时间

		printk("DMA channel %d at %p, irq %d\n",
		       cp->number, cp->regs, cp->irq);
	}

	return 0;

 err:
	kmem_cache_destroy(dma_kmem);
	iounmap(dma_base);
	dma_base = NULL;
	return ret;
}

int __init s3c2410_dma_init(void)
{
	return s3c24xx_dma_init(4, IRQ_DMA0, 0x40);
}

static inline int is_channel_valid(unsigned int channel)
{
//#define DMA_CH_VALID          (1<<31)
	return (channel & DMA_CH_VALID);
}

static struct s3c24xx_dma_order *dma_order;


/* s3c2410_dma_map_channel()
 *
 * turn the virtual channel number into a real, and un-used hardware
 * channel.
 *
 * first, try the dma ordering given to us by either the relevant
 * dma code, or the board. Then just find the first usable free
 * channel
*/
//根据虚拟通道号,找到一个可以的通道
//实际就是根据channel从数组s3c2440_dma_order和
//s3c2440_dma_mapping和中找到对应且可用的元素
//s3c2410_dma_request()会调用这个函数
static struct s3c2410_dma_chan *s3c2410_dma_map_channel(int channel)
{
	struct s3c24xx_dma_order_ch *ord = NULL;
	struct s3c24xx_dma_map *ch_map;
	struct s3c2410_dma_chan *dmach;
	int ch;

	//dma_sel.map就是s3c2440_dma_mappings[]
	//dma_sel.map_size就是ARRAY_SIZE(s3c2440_dma_mappings)
	if (dma_sel.map == NULL || channel > dma_sel.map_size)
		return NULL;

	ch_map = dma_sel.map + channel;
	//从上面这句可以看出这个channel不是指dma的4个通道
	//而是s3c2440_dma_mappings[]对应的数组元素,下面有
	/*channel的值如下
	enum dma_ch {
	DMACH_XD0,
	DMACH_XD1,
	DMACH_SDI,
	DMACH_SPI0,
	DMACH_SPI1,
	DMACH_UART0,
	DMACH_UART1,
	...
	和s3c2440_dma_mappings[]对应
	*/
	
	/* first, try the board mapping */

	if (dma_order) {
		ord = &dma_order->channels[channel];//找到对应的

		for (ch = 0; ch < dma_channels; ch++) {
			int tmp;
			if (!is_channel_valid(ord->list[ch]))//判断通道可用
				continue;

			tmp = ord->list[ch] & ~DMA_CH_VALID;//标志为不可用
			//s3c2410_dma_request函数会使in_use变为1
			if (s3c2410_chans[tmp].in_use == 0) {
				ch = tmp;
				goto found;
			}
		}

		if (ord->flags & DMA_CH_NEVER)
			return NULL;
	}

	/* second, search the channel map for first free */

	for (ch = 0; ch < dma_channels; ch++) {
		if (!is_channel_valid(ch_map->channels[ch]))//判断可用
			continue;

		if (s3c2410_chans[ch].in_use == 0) {
			printk("mapped channel %d to %d\n", channel, ch);
			break;
		}
	}
    /*
	上面的is_channel_valid是在平台提供的数组中的标记
	in_use是此代码中的标记。
	*/
	
	if (ch >= dma_channels)
		return NULL;

	/* update our channel mapping */

 found:
	dmach = &s3c2410_chans[ch];
	dmach->map = ch_map; //对应的struct s3c24xx_dma_map结构体
	dmach->req_ch = channel;
	s3c_dma_chan_map[channel] = dmach;//放入struct s3c2410_dma_chan结构体中

	/* select the channel */

	(dma_sel.select)(dmach, ch_map);//下面有s3c2440的select函数s3c2440_dma_select()

	return dmach;
}

static int s3c24xx_dma_check_entry(struct s3c24xx_dma_map *map, int ch)
{
	return 0;
}

int __init s3c24xx_dma_init_map(struct s3c24xx_dma_selection *sel)
{
	//传入的是s3c2440_dma_sel
	/*
	struct s3c24xx_dma_map {
			const char              *name;

			unsigned long            channels[S3C_DMA_CHANNELS];
			unsigned long            channels_rx[S3C_DMA_CHANNELS];
	};

	struct s3c24xx_dma_selection {
        struct s3c24xx_dma_map  *map;
        unsigned long            map_size;
        unsigned long            dcon_mask;

        void    (*select)(struct s3c2410_dma_chan *chan,
                          struct s3c24xx_dma_map *map);

        void    (*direction)(struct s3c2410_dma_chan *chan,
                             struct s3c24xx_dma_map *map,
                             enum dma_data_direction dir);
    };
    static struct s3c24xx_dma_map __initdata s3c2440_dma_mappings[] = {
        [DMACH_XD0] = {
                .name           = "xdreq0",
                .channels[0]    = S3C2410_DCON_CH0_XDREQ0 | DMA_CH_VALID,
        },
        [DMACH_XD1] = {
                .name           = "xdreq1",
                .channels[1]    = S3C2410_DCON_CH1_XDREQ1 | DMA_CH_VALID,
        },
        [DMACH_SDI] = {
                .name           = "sdi",
                .channels[0]    = S3C2410_DCON_CH0_SDI | DMA_CH_VALID,
                .channels[1]    = S3C2440_DCON_CH1_SDI | DMA_CH_VALID,
                .channels[2]    = S3C2410_DCON_CH2_SDI | DMA_CH_VALID,
                .channels[3]    = S3C2410_DCON_CH3_SDI | DMA_CH_VALID,
        },
	//太长没贴完,就是支持的外设信息

	static void s3c2440_dma_select(struct s3c2410_dma_chan *chan,
								   struct s3c24xx_dma_map *map)
	{
			chan->dcon = map->channels[chan->number] & ~DMA_CH_VALID;//选择一个通道并置为不可用
	}

	static struct s3c24xx_dma_selection __initdata s3c2440_dma_sel = {
			.select         = s3c2440_dma_select,
			.dcon_mask      = 7 << 24, //dcon寄存器的24~26位 源选择
			.map            = s3c2440_dma_mappings,
			.map_size       = ARRAY_SIZE(s3c2440_dma_mappings),
	};
	*/
	struct s3c24xx_dma_map *nmap;
	size_t map_sz = sizeof(*nmap) * sel->map_size;
	int ptr;

	nmap = kmalloc(map_sz, GFP_KERNEL);
	if (nmap == NULL)
		return -ENOMEM;

	memcpy(nmap, sel->map, map_sz);
	memcpy(&dma_sel, sel, sizeof(*sel));

	dma_sel.map = nmap;
    //上面一段就是,申请内存,把s3c2440_dma_sel的信息放入dma_sel中
	for (ptr = 0; ptr < sel->map_size; ptr++)
		s3c24xx_dma_check_entry(nmap+ptr, ptr);//在上面,空函数

	return 0;
}

int __init s3c24xx_dma_order_set(struct s3c24xx_dma_order *ord)
{
    //传入的就是s3c2440_dma_order
	/*
	struct s3c24xx_dma_order_ch {
			unsigned int    list[S3C_DMA_CHANNELS]; // list of channels 
			unsigned int    flags;                          // flags 
	};


	struct s3c24xx_dma_order {
			struct s3c24xx_dma_order_ch     channels[DMACH_MAX];
	};
    DMACH_MAX指的是外设的最大ID,通过类似DMACH_UART0_TX来表示外设
	S3C_DMA_CHANNELS是最大通道数,s3c2440是4个通道

	static struct s3c24xx_dma_order __initdata s3c2440_dma_order = {
	.channels	= {
		[DMACH_SDI]	= {
			.list	= {
				[0]	= 3 | DMA_CH_VALID,
				[1]	= 2 | DMA_CH_VALID,
				[2]	= 1 | DMA_CH_VALID,
				[3]	= 0 | DMA_CH_VALID,
			},
		},
		[DMACH_I2S_IN]	= {
			.list	= {
				[0]	= 1 | DMA_CH_VALID,
				[1]	= 2 | DMA_CH_VALID,
			},
		},
		[DMACH_I2S_OUT]	= {
			.list	= {
				[0]	= 2 | DMA_CH_VALID,
				[1]	= 1 | DMA_CH_VALID,
			},
		},
		[DMACH_PCM_IN] = {
			.list	= {
				[0]	= 2 | DMA_CH_VALID,
				[1]	= 1 | DMA_CH_VALID,
	...
	太多,没贴完。
	*/
	struct s3c24xx_dma_order *nord = dma_order;

	if (nord == NULL)//为空的话重分配
		nord = kmalloc(sizeof(struct s3c24xx_dma_order), GFP_KERNEL);

	if (nord == NULL) {
		printk(KERN_ERR "no memory to store dma channel order\n");
		return -ENOMEM;
	}

	dma_order = nord;
	memcpy(nord, ord, sizeof(struct s3c24xx_dma_order));
	return 0;
}

如果你看懂了上面的代码,下面的plat-samsung中的代码基本不用分析了

/* linux/arch/arm/plat-samsung/s3c-dma-ops.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com
 *
 * Samsung S3C-DMA Operations
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/export.h>

#include <mach/dma.h>

struct cb_data {
        void (*fp) (void *);
        void *fp_param;
        unsigned ch;
        struct list_head node;
};

static LIST_HEAD(dma_list);
//s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);
//s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
/*
	if (chan->callback_fn != NULL) {
		(chan->callback_fn)(chan, buf->id, buf->size, result);
	}
*/

static void s3c_dma_cb(struct s3c2410_dma_chan *channel, void *param,
                       int size, enum s3c2410_dma_buffresult res)
{
        struct cb_data *data = param;

        data->fp(data->fp_param);
}

static unsigned s3c_dma_request(enum dma_ch dma_ch,
                                 struct samsung_dma_info *info)
{
        struct cb_data *data;

        if (s3c2410_dma_request(dma_ch, info->client, NULL) < 0) {
                s3c2410_dma_free(dma_ch, info->client);
                return 0;
        }

        data = kzalloc(sizeof(struct cb_data), GFP_KERNEL);
        data->ch = dma_ch;
        list_add_tail(&data->node, &dma_list);

        s3c2410_dma_devconfig(dma_ch, info->direction, info->fifo);

        if (info->cap == DMA_CYCLIC)
                s3c2410_dma_setflags(dma_ch, S3C2410_DMAF_CIRCULAR);

        s3c2410_dma_config(dma_ch, info->width);

        return (unsigned)dma_ch;
}

static int s3c_dma_release(unsigned ch, struct s3c2410_dma_client *client)
{
        struct cb_data *data;

        list_for_each_entry(data, &dma_list, node)
                if (data->ch == ch)
                        break;
        list_del(&data->node);

        s3c2410_dma_free(ch, client);
        kfree(data);

        return 0;
}

static int s3c_dma_prepare(unsigned ch, struct samsung_dma_prep_info *info)
{
        struct cb_data *data;
        int len = (info->cap == DMA_CYCLIC) ? info->period : info->len;

        list_for_each_entry(data, &dma_list, node)
                if (data->ch == ch)
                        break;

        if (!data->fp) {
                s3c2410_dma_set_buffdone_fn(ch, s3c_dma_cb);
                data->fp = info->fp;
                data->fp_param = info->fp_param;
        }

        s3c2410_dma_enqueue(ch, (void *)data, info->buf, len);

        return 0;
}

static inline int s3c_dma_trigger(unsigned ch)
{
        return s3c2410_dma_ctrl(ch, S3C2410_DMAOP_START);
}

static inline int s3c_dma_started(unsigned ch)
{
        return s3c2410_dma_ctrl(ch, S3C2410_DMAOP_STARTED);
}

static inline int s3c_dma_flush(unsigned ch)
{
        return s3c2410_dma_ctrl(ch, S3C2410_DMAOP_FLUSH);
}

static inline int s3c_dma_stop(unsigned ch)
{
        return s3c2410_dma_ctrl(ch, S3C2410_DMAOP_STOP);
}

static struct samsung_dma_ops s3c_dma_ops = {
        .request        = s3c_dma_request,
        .release        = s3c_dma_release,
        .prepare        = s3c_dma_prepare,
        .trigger        = s3c_dma_trigger,
        .started        = s3c_dma_started,
        .flush          = s3c_dma_flush,
        .stop           = s3c_dma_stop,
};

void *s3c_dma_get_ops(void)
{
        return &s3c_dma_ops;
}
EXPORT_SYMBOL(s3c_dma_get_ops);


你可能感兴趣的:(ARM,移动设备,linux内核)