欢迎访问电子工程网!   登录 | 免费注册 ]   

yd2763132的个人空间 http://www.eechina.com/space-uid-36266.html [收藏] [复制] [分享] [RSS]

博客

2410 DMA初始化代码导读笔记(2)

已有 425 次阅读2011-5-12 20:03 |个人分类:linux

#include <linux/config.h>
#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/sysdev.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <asm/system.h>
#include <asm/irq.h>
#include <asm/hardware.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/mach/dma.h>
#include <asm/arch/map.h>
/* io map for dma */
static void __iomem *dma_base;
static kmem_cache_t *dma_kmem;//dma缓冲池
/* dma channel state information */
s3c2410_dma_chan_t s3c2410_chans[S3C2410_DMA_CHANNELS];//dma全局数据
/* 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(s3c2410_dma_chan_t *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(s3c2410_dma_chan_t *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_showregs(const char *fname, int line, s3c2410_dma_chan_t *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, s3c2410_dma_chan_t *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_showregs(fname, line, chan, &state);
}
#define dbg_showregs(chan) dmadbg_showregs(__FUNCTION__, __LINE__, (chan))
#define dbg_showchan(chan) dmadbg_showchan(__FUNCTION__, __LINE__, (chan))
#else
#define dbg_showregs(chan) do { } while(0)
#define dbg_showchan(chan) do { } while(0)
#endif /* CONFIG_S3C2410_DMA_DEBUG */
#define check_channel(chan) \
  do { if ((chan) >= S3C2410_DMA_CHANNELS) { \
    printk(KERN_ERR "%s: invalid channel %d\n", __FUNCTION__, (chan)); \
    return -EINVAL; \
  } } while(0)

/* s3c2410_dma_stats_timeout
 *
 * Update DMA stats from timeout info
*/
static void
s3c2410_dma_stats_timeout(s3c2410_dma_stats_t *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
*/
static int s3c2410_dma_waitforload(s3c2410_dma_chan_t *chan, int line)
{
 int timeout = chan->load_timeout;
 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++; //加载次数加1
 while (--timeout > 0) {
  if ((dma_rdreg(chan, S3C2410_DMA_DSTAT) << (32-20)) != 0) {//若DMA没有准备好
   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;//dma在运行时设为运行标志
    break;
   default:
    printk(KERN_ERR "dma%d: unknown load_state in s3c2410_dma_waitforload() %d\n", chan->number, chan->load_state);
   }
   return 1;//DMA忙退出
  }
 }
 if (chan->stats != NULL) {
  chan->stats->timeout_failed++;//等待时间超时记录加1
 }
 return 0;
}
/* s3c2410_dma_loadbuffer
 *
 * load a buffer, and update the channel state
*///加载缓存且更新其通道状态且设置加载方式和传送尺寸大小
static inline int
s3c2410_dma_loadbuffer(s3c2410_dma_chan_t *chan,
         s3c2410_dma_buf_t *buf)
{
 unsigned long reload;
 pr_debug("s3c2410_chan_loadbuffer: loading buff %p (0x%08lx,0x%06x)\n",
   buf, (unsigned long)buf->data, buf->size);
 if (buf == NULL) {//为空时退出
  dmawarn("buffer is NULL\n");
  return -EINVAL;
 }
 /* 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);
  reload = (buf->next == NULL) ? S3C2410_DCON_NORELOAD : 0;
  //没有加载缓存时若没有下个缓存时不自动加载反之自动加载
 } else {
  pr_debug("load_state is %d => autoreload\n", chan->load_state);
  reload = S3C2410_DCON_AUTORELOAD;//有加载缓存时设为自动加载
 }
 writel(buf->data, chan->addr_reg);//给初始目的地地址寄存器赋值
 dma_wrreg(chan, S3C2410_DMA_DCON,
    chan->dcon | reload | (buf->size/chan->xfer_unit));
  //设定加载方式和传输计数值初值到DCON寄存器
 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(s3c2410_dma_chan_t *chan, s3c2410_chan_op_t op)
{
 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
*///对不同的DMA客户端按照result处理,即缓存传送完成后调用其回调函数
static inline void
s3c2410_dma_buffdone(s3c2410_dma_chan_t *chan, s3c2410_dma_buf_t *buf,
       s3c2410_dma_buffresult_t result)
{
 pr_debug("callback_fn=%p, buf=%p, id=%p, size=%d, result=%d\n",
   chan->callback_fn, buf, buf->id, buf->size, result);
 if (chan->callback_fn != NULL) {
  (chan->callback_fn)(chan, buf->id, buf->size, result);
 }
}
/* s3c2410_dma_start
 *
 * start a dma channel going
*///启动一个DMA通道
static int s3c2410_dma_start(s3c2410_dma_chan_t *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;//若没有加载缓存和可加载缓存则通道空闲退出
   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);//读取DMA掩码触发寄存器
 tmp &= ~S3C2410_DMASKTRIG_STOP;//清除停止DMA位
 tmp |= S3C2410_DMASKTRIG_ON;//开启该通道
 dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);
 pr_debug("wrote %08lx to DMASKTRIG\n", 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);
 local_irq_restore(flags);
 return 0;
}
/* s3c2410_dma_canload
 *
 * work out if we can queue another buffer into the DMA engine
*///判断DMA是否能加载缓存
static int
s3c2410_dma_canload(s3c2410_dma_chan_t *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)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];//获取该通道结构
 s3c2410_dma_buf_t *buf;//缓存地址
 unsigned long flags;
 check_channel(channel);
 pr_debug("%s: id=%p, data=%08x, size=%d\n",
   __FUNCTION__, id, (unsigned int)data, size);
 buf = kmem_cache_alloc(dma_kmem, GFP_ATOMIC);
 //在缓存池dma_kmem中分配一个对象,由于用在中断处理中所以所分配的页面
 //采取GFP_ATOMIC禁止睡眠
 if (buf == NULL) {
  pr_debug("%s: out of memory (%ld alloc)\n",
    __FUNCTION__, sizeof(*buf));
  return -ENOMEM;
 }
 pr_debug("%s: new buffer %p\n", __FUNCTION__, buf);
 //dbg_showchan(chan);
 buf->next  = NULL; //没有下个缓存
 buf->data  = buf->ptr = data;//起始地址和当前地址值
 buf->size  = size;//缓存大小
 buf->id    = id;//客户端id
 buf->magic = BUF_MAGIC;//缓存魔数
 local_irq_save(flags);
 if (chan->curr == NULL) {//若该通道缓存链表指针为空
  /* we've got nothing loaded... */
  pr_debug("%s: buffer %p queued onto empty channel\n",
    __FUNCTION__, buf);
  chan->curr = buf;//将通道缓存链表指针指向新建的对象
  chan->end  = buf;//用于挂载下个缓存之用
  chan->next = NULL;
  //即主结构链表在上个缓存传送完后要将chan->next=chan->curr->next 
  //chan->curr = chan->next
 
 } else {//若该通道已有缓存
  pr_debug("dma%d: %s: buffer %p queued onto non-empty channel\n",
    chan->number, __FUNCTION__, buf);
  if (chan->end == NULL)
   pr_debug("dma%d: %s: %p not empty, and chan->end==NULL?\n",
     chan->number, __FUNCTION__, chan);
  chan->end->next = buf;
  //因为之前将chan->end=buf,故对象添加到已存在的缓存的next成员上
  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) {//若DMA引擎为运行态
  if (chan->load_state == S3C2410_DMALOAD_1LOADED && 1) {//若该通道有可加载缓存
   if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {//若等待DMA超时则退出
    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) {//若可加载驱动则配置加载模式
   s3c2410_dma_loadbuffer(chan, chan->next);
  }
 } else if (chan->state == S3C2410_DMA_IDLE) {//若DMA引擎为空闲态
  if (chan->flags & S3C2410_DMAF_AUTOSTART) {//若为自动启动标志
   s3c2410_dma_ctrl(chan->number, S3C2410_DMAOP_START);//发送启动DMA命令
  }
 }
 local_irq_restore(flags);
 return 0;
}
EXPORT_SYMBOL(s3c2410_dma_enqueue);
static inline void
s3c2410_dma_freebuf(s3c2410_dma_buf_t *buf)
{
 int magicok = (buf->magic == BUF_MAGIC);//当该缓存魔数满足要求时
 buf->magic = -1;
 if (magicok) {
  kmem_cache_free(dma_kmem, buf);//释放slab缓存中的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(s3c2410_dma_chan_t *chan)
{
 pr_debug("dma%d: s3c2410_dma_lastxfer: load_state %d\n",
   chan->number, chan->load_state);
 switch (chan->load_state) {
 case S3C2410_DMALOAD_NONE:
  break;
 case S3C2410_DMALOAD_1LOADED://为可加载状态时则等待DMA操作结束
  if (s3c2410_dma_waitforload(chan, __LINE__) == 0) {
    /* flag error? */
   printk(KERN_ERR "dma%d: timeout waiting for load\n",
          chan->number);
   return;
  }
  break;
 default:
  pr_debug("dma%d: lastxfer: unhandled load_state %d with no next",
    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 pt_regs *regs)
{
 s3c2410_dma_chan_t *chan = (s3c2410_dma_chan_t *)devpw;//即传送过来的chan结构体
 s3c2410_dma_buf_t  *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, __FUNCTION__, buf);
   return IRQ_HANDLED;
  }
  s3c2410_dma_buffdone(chan, buf, S3C2410_RES_OK);
  //即缓存传送完成后调用其回调函数,表示由于成功调用
  /* free resouces */
  s3c2410_dma_freebuf(buf);//释放该slab缓存对象
 } else {
 }
 if (chan->next != NULL) {//传送多个缓存时
  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\n",
           chan->number);
    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, S3C2410_DMAOP_STOP);//为没有缓存加载时则关闭该通道
  }
 }
 no_load:
 return IRQ_HANDLED;
}
/* s3c2410_request_dma
 *
 * get control of an dma channel
*///获得一个通道的对应客户端的控制权,即客户端可中断响应该DMA
int s3c2410_dma_request(unsigned int channel, s3c2410_dma_client_t *client,
   void *dev)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 unsigned long flags;
 int err;
 pr_debug("dma%d: s3c2410_request_dma: client=%s, dev=%p\n",
   channel, client->name, dev);
 check_channel(channel);
 local_irq_save(flags);
 dbg_showchan(chan);//调试信息
 if (chan->in_use) {//已使用状态则出错
  if (client != chan->client) {
   printk(KERN_ERR "dma%d: already in use\n", channel);
   local_irq_restore(flags);
   return -EBUSY;
  } else {
   printk(KERN_ERR "dma%d: client already has channel\n", channel);
  }
 }
 chan->client = client;//匹配到该通道
 chan->in_use = 1;//设使用标志
 if (!chan->irq_claimed) {//若未声明中断则申请中断
  pr_debug("dma%d: %s : requesting irq %d\n",
    channel, __FUNCTION__, chan->irq);
  err = request_irq(chan->irq, s3c2410_dma_irq, SA_INTERRUPT,
      client->name, (void *)chan);
      //为该客户端申请中断,处理函数为s3c2410_dma_irq其参数为chan结构
  if (err) {
   chan->in_use = 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_claimed = 1;//标志已声明中断
  chan->irq_enabled = 1;//中断已使能
 }
 local_irq_restore(flags);
 /* need to setup */
 pr_debug("%s: channel initialised, %p\n", __FUNCTION__, chan);
 return 0;
}
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.
*///停止DMA且释放中断资源,即释放某通道上的一个客户端
int s3c2410_dma_free(dmach_t channel, s3c2410_dma_client_t *client)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 unsigned long flags;
 check_channel(channel);
 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",
         __FUNCTION__, chan);
  /* possibly flush the channel */
  s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STOP);
 }
 chan->client = NULL;
 chan->in_use = 0;//通道未分配标志
 if (chan->irq_claimed)//已声明DMA中断功能则释放该中断号
  free_irq(chan->irq, (void *)chan);
 chan->irq_claimed = 0;//设为未声明中断标志
 local_irq_restore(flags);
 return 0;
}
EXPORT_SYMBOL(s3c2410_dma_free);
static int s3c2410_dma_dostop(s3c2410_dma_chan_t *chan)//关闭对应的dma通道
{
 unsigned long tmp;
 unsigned long flags;
 pr_debug("%s:\n", __FUNCTION__);
 dbg_showchan(chan);
 local_irq_save(flags);
 s3c2410_dma_call_op(chan,  S3C2410_DMAOP_STOP);
 tmp = dma_rdreg(chan, S3C2410_DMA_DMASKTRIG);
 tmp |= S3C2410_DMASKTRIG_STOP;
 dma_wrreg(chan, S3C2410_DMA_DMASKTRIG, tmp);//关闭该通道
#if 0
 /* 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
 chan->state      = S3C2410_DMA_IDLE;//通道为空置
 chan->load_state = S3C2410_DMALOAD_NONE;//dma引擎状态为没有缓存
 local_irq_restore(flags);
 return 0;
}
/* s3c2410_dma_flush
 *
 * stop the channel, and remove all current and pending transfers
*///释放内存和硬件资源
static int s3c2410_dma_flush(s3c2410_dma_chan_t *chan)//刷新
{
 s3c2410_dma_buf_t *buf, *next;
 unsigned long flags;
 pr_debug("%s:\n", __FUNCTION__);
 local_irq_save(flags);
 if (chan->state != S3C2410_DMA_IDLE) {//若该通道在运行或暂停则停止DMA
  pr_debug("%s: stopping channel...\n", __FUNCTION__ );
  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",
          __FUNCTION__, buf, buf->next);
   s3c2410_dma_buffdone(chan, buf, S3C2410_RES_ABORT);//缓存数据完成后回调函数中止
   s3c2410_dma_freebuf(buf);//释放slab缓存中的buf对象
  }
 }
 local_irq_restore(flags);
 return 0;
}

int s3c2410_dma_ctrl(dmach_t channel, s3c2410_chan_op_t op)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 check_channel(channel);
 switch (op) {
 case S3C2410_DMAOP_START://启动DMA
  return s3c2410_dma_start(chan);
 case S3C2410_DMAOP_STOP://停止DMA
  return s3c2410_dma_dostop(chan);
 case S3C2410_DMAOP_PAUSE://暂停
  return -ENOENT;
 case S3C2410_DMAOP_RESUME://恢复
  return -ENOENT;
 case S3C2410_DMAOP_FLUSH://刷新
  return s3c2410_dma_flush(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)
 * dcon:         base value of the DCONx register
*///配置传送尺寸 使能中断和硬件触发DMA功能
int s3c2410_dma_config(dmach_t channel,
         int xferunit,
         int dcon)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 pr_debug("%s: chan=%d, xfer_unit=%d, dcon=%08x\n",
   __FUNCTION__, channel, xferunit, dcon);
 check_channel(channel);
 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", __FUNCTION__, xferunit);
  return -EINVAL;
 }
 dcon |= S3C2410_DCON_HWTRIG;//硬件方式产生DMA请求
 dcon |= S3C2410_DCON_INTREQ;//产生中断请求
 pr_debug("%s: dcon now %08x\n", __FUNCTION__, dcon);
 chan->dcon = dcon;
 chan->xfer_unit = xferunit;
 return 0;
}
EXPORT_SYMBOL(s3c2410_dma_config);
//设置通道标志位
int s3c2410_dma_setflags(dmach_t channel, unsigned int flags)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 check_channel(channel);
 pr_debug("%s: chan=%p, flags=%08x\n", __FUNCTION__, chan, flags);
 chan->flags = flags;
 return 0;
}
EXPORT_SYMBOL(s3c2410_dma_setflags);
//设置通道完成后回调函数
int s3c2410_dma_set_opfn(dmach_t channel, s3c2410_dma_opfn_t rtn)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 check_channel(channel);
 pr_debug("%s: chan=%p, op rtn=%p\n", __FUNCTION__, chan, rtn);
 chan->op_fn = rtn;
 return 0;
}
EXPORT_SYMBOL(s3c2410_dma_set_opfn);
//设置缓存数据完成后回调函数
int s3c2410_dma_set_buffdone_fn(dmach_t channel, s3c2410_dma_cbfn_t rtn)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 check_channel(channel);
 pr_debug("%s: chan=%p, callback rtn=%p\n", __FUNCTION__, chan, rtn);
 chan->callback_fn = rtn;
 return 0;
}
EXPORT_SYMBOL(s3c2410_dma_set_buffdone_fn);
/* s3c2410_dma_devconfig
 *
 * configure the dma source/destination hardware type and address
 *
 * source:    S3C2410_DMASRC_HW: source is hardware
 *            S3C2410_DMASRC_MEM: source is memory
 *
 * hwcfg:     the value for xxxSTCn register,
 *            bit 0: 0=increment pointer, 1=leave pointer
 *            bit 1: 0=soucre is AHB, 1=soucre is APB
 *
 * devaddr:   physical address of the device
*///设置外设种类 配置值 外设物理首地址
int s3c2410_dma_devconfig(int channel,
     s3c2410_dmasrc_t source,
     int hwcfg,
     unsigned long devaddr)
{
 s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];
 check_channel(channel);
 pr_debug("%s: source=%d, hwcfg=%08x, devaddr=%08lx\n",
   __FUNCTION__, (int)source, hwcfg, devaddr);
 chan->source = source;
 chan->dev_addr = devaddr;//设备类型和物理地址
 switch (source) {
 case S3C2410_DMASRC_HW://设备为原时
  pr_debug("%s: hw source, devaddr=%08lx, hwcfg=%d\n",
    __FUNCTION__, 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));
  //目的数据位于系统总线AHB,每一次传输完毕,地址值增加相应的数据尺寸
  chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DIDST);//通道目的地地址寄存器
  return 0;
 case S3C2410_DMASRC_MEM://内存为源时
  pr_debug( "%s: mem source, devaddr=%08lx, hwcfg=%d\n",
     __FUNCTION__, devaddr, hwcfg);
  dma_wrreg(chan, S3C2410_DMA_DISRCC, (0<<1) | (0<<0));
  //源数据位于系统总线AHB,每一次传输完毕,地址值增加相应的数据尺寸
  dma_wrreg(chan, S3C2410_DMA_DIDST,  devaddr);
  dma_wrreg(chan, S3C2410_DMA_DIDSTC, hwcfg & 3);//按照设定值配置
  chan->addr_reg = dma_regaddr(chan, S3C2410_DMA_DISRC);
  return 0;
 }
 printk(KERN_ERR "dma%d: invalid source type (%d)\n", channel, source);
 return -EINVAL;
}
EXPORT_SYMBOL(s3c2410_dma_devconfig);
/* s3c2410_dma_getposition
 *
 * returns the current transfer points for the dma source and destination
*///获取当前传送指针的源地址和目的地地址
int s3c2410_dma_getposition(dmach_t channel, dma_addr_t *src, dma_addr_t *dst)
{
  s3c2410_dma_chan_t *chan = &s3c2410_chans[channel];//获取通道相应的数据结构
  check_channel(channel);//检测是否超出
 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 device class */
#ifdef CONFIG_PM
static int s3c2410_dma_suspend(struct sys_device *dev, pm_message_t state)
{
 s3c2410_dma_chan_t *cp = container_of(dev, s3c2410_dma_chan_t, dev);
 //获取设备的s3c2410_dma_chan_t地址
 printk(KERN_DEBUG "suspending dma channel %d\n", cp->number);
 if (dma_rdreg(cp, S3C2410_DMA_DMASKTRIG) & S3C2410_DMASKTRIG_ON) {//若该通道开启
  printk(KERN_INFO "dma: stopping channel %d due to suspend\n",
         cp->number);
  s3c2410_dma_dostop(cp);//关闭dma通道
 }
 return 0;
}
static int s3c2410_dma_resume(struct sys_device *dev)//唤醒后没有什么需要恢复
{
 return 0;
}
#else
#define s3c2410_dma_suspend NULL
#define s3c2410_dma_resume  NULL
#endif /* CONFIG_PM */
static struct sysdev_class dma_sysclass = {//sysfs class操作集
 set_kset_name("s3c24xx-dma"),
 .suspend = s3c2410_dma_suspend,
 .resume  = s3c2410_dma_resume,
};
/* kmem cache implementation */
static void s3c2410_dma_cache_ctor(void *p, kmem_cache_t *c, unsigned long f)
{
 memset(p, 0, sizeof(s3c2410_dma_buf_t));//slab对象设为0
}
/* initialisation code */
static int __init s3c2410_init_dma(void)
{
 s3c2410_dma_chan_t *cp;//dma描述结构
 int channel;
 int ret;
 printk("S3C2410 DMA Driver, (c) 2003-2004 Simtec Electronics\n");
 dma_base = ioremap(S3C2410_PA_DMA, 0x200);//io内存映射
 if (dma_base == NULL) {
  printk(KERN_ERR "dma failed to remap register block\n");
  return -ENOMEM;
 }
 ret = sysdev_class_register(&dma_sysclass);//注册dma class到class链表
 if (ret != 0) {
  printk(KERN_ERR "dma sysclass registration failed\n");
  goto err;
 }
 dma_kmem = kmem_cache_create("dma_desc", sizeof(s3c2410_dma_buf_t), 0,
         SLAB_HWCACHE_ALIGN,
         s3c2410_dma_cache_ctor, NULL);
  //建立dma高速缓存池,slab层把所有对象按高速缓存对其,添加对象时调用构造函数s3c2410_dma_cache_ctor
 if (dma_kmem == NULL) {
  printk(KERN_ERR "dma failed to make kmem cache\n");
  ret = -ENOMEM;
  goto err;
 }
 for (channel = 0; channel < S3C2410_DMA_CHANNELS; channel++) {
  cp = &s3c2410_chans[channel];
  memset(cp, 0, sizeof(s3c2410_dma_chan_t));//设为0
  /* dma channel irqs are in order.. */
  cp->number = channel;//通道数
  cp->irq    = channel + IRQ_DMA0;//通道中断号
  cp->regs   = dma_base + (channel*0x40);//通道初始源地址寄存器
  /* point current stats somewhere */
  cp->stats  = &cp->stats_store;
  cp->stats_store.timeout_shortest = LONG_MAX;//超时值设置为初始值
  /* basic channel configuration */
  cp->load_timeout = 1<<18;//装载时间
  /* register system device */
  cp->dev.cls = &dma_sysclass;//sysfs设备的class
  cp->dev.id  = channel;//sysfs设备的id
  ret = sysdev_register(&cp->dev);//注册sysfs设备
  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;
}
__initcall(s3c2410_init_dma);//先于模块初始化
小结:
1.DMA以sysfs中的class某个成员来使用的,其设备为sysfs中的sys_device 结构体。
先将DMA相关的dma_sysclass注册到sysfs的class中后,然后再将以dma_sysclass为基础的
sys_device设备注册,通过以上就完成了DMA注册。
2.DMA的传送是以slab缓存为基础的,在初始化时建立了一个以dma_kmem为首地址的缓存池。
然后通过函数s3c2410_dma_enqueue为某个DMA通道支持的客户端分配一个slab缓存对象以供
传递使用,若此缓存数据传递完成后调用s3c2410_dma_freebuf将该缓存对象释放。
3.此中最重要的函数为中断处理函数s3c2410_dma_irq,其主要处理为调整该通道缓存状态且
调用传送完成后回调函数;为下一个缓存的传送设置对应的配置值。
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yd4330152763132/archive/2010/02/01/5275751.aspx

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

回顶部