Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743806
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-08-09 08:45:24

原文地址:SD卡驱动分析之s3cmci.c 作者:yqhminquan

SD卡驱动分析之s3cmci.c  

2010-04-10 16:42:23|  分类: arm linux设备驱 |  标签: |字号 

#include
#include
#include
#include
#include
#include
#include
#include

#include

#include
#include

#include

#include "s3cmci.h"

#define DRIVER_NAME "s3c-mci"

enum dbg_channels {//与调试有关
 dbg_err   = (1 << 0),
 dbg_debug = (1 << 1),
 dbg_info  = (1 << 2),
 dbg_irq   = (1 << 3),
 dbg_sg    = (1 << 4),
 dbg_dma   = (1 << 5),
 dbg_pio   = (1 << 6),
 dbg_fail  = (1 << 7),
 dbg_conf  = (1 << 8),
};

static const int dbgmap_err   = dbg_fail;
static const int dbgmap_info  = dbg_info | dbg_conf;
static const int dbgmap_debug = dbg_err | dbg_debug;

#define dbg(host, channels, args...)    \
 do {       \
 if (dbgmap_err & channels)     \
  dev_err(&host->pdev->dev, args);  \
 else if (dbgmap_info & channels)   \
  dev_info(&host->pdev->dev, args); \
 else if (dbgmap_debug & channels)   \
  dev_dbg(&host->pdev->dev, args);  \
 } while (0)

#define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)

static struct s3c2410_dma_client s3cmci_dma_client = {
 .name  = "s3c-mci",
};//在获取DMA通道时要用到

static void finalize_request(struct s3cmci_host *host);
static void s3cmci_send_request(struct mmc_host *mmc);
static void s3cmci_reset(struct s3cmci_host *host);

#ifdef CONFIG_MMC_DEBUG

static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
{
 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
 u32 datcon, datcnt, datsta, fsta, imask;

 con  = readl(host->base + S3C2410_SDICON);
 pre  = readl(host->base + S3C2410_SDIPRE);
 cmdarg  = readl(host->base + S3C2410_SDICMDARG);
 cmdcon  = readl(host->base + S3C2410_SDICMDCON);
 cmdsta  = readl(host->base + S3C2410_SDICMDSTAT);
 r0  = readl(host->base + S3C2410_SDIRSP0);
 r1  = readl(host->base + S3C2410_SDIRSP1);
 r2  = readl(host->base + S3C2410_SDIRSP2);
 r3  = readl(host->base + S3C2410_SDIRSP3);
 timer  = readl(host->base + S3C2410_SDITIMER);
 bsize  = readl(host->base + S3C2410_SDIBSIZE);
 datcon  = readl(host->base + S3C2410_SDIDCON);
 datcnt  = readl(host->base + S3C2410_SDIDCNT);
 datsta  = readl(host->base + S3C2410_SDIDSTA);
 fsta  = readl(host->base + S3C2410_SDIFSTA);
 imask   = readl(host->base + host->sdiimsk);

 dbg(host, dbg_debug, "%s  CON:[%08x]  PRE:[%08x]  TMR:[%08x]\n",
    prefix, con, pre, timer);

 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
    prefix, cmdcon, cmdarg, cmdsta);

 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
          " DSTA:[%08x] DCNT:[%08x]\n",
    prefix, datcon, fsta, datsta, datcnt);

 dbg(host, dbg_debug, "%s   R0:[%08x]   R1:[%08x]"
          "   R2:[%08x]   R3:[%08x]\n",
    prefix, r0, r1, r2, r3);
}

static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
      int stop)
{
 snprintf(host->dbgmsg_cmd, 300,
   "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
   host->ccnt, (stop ? " (STOP)" : ""),
   cmd->opcode, cmd->arg, cmd->flags, cmd->retries);

 if (cmd->data) {
  snprintf(host->dbgmsg_dat, 300,
    "#%u bsize:%u blocks:%u bytes:%u",
    host->dcnt, cmd->data->blksz,
    cmd->data->blocks,
    cmd->data->blocks * cmd->data->blksz);
 } else {
  host->dbgmsg_dat[0] = '\0';
 }
}

static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
   int fail)
{
 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;

 if (!cmd)
  return;

 if (cmd->error == 0) {
  dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
   host->dbgmsg_cmd, cmd->resp[0]);
 } else {
  dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
   cmd->error, host->dbgmsg_cmd, host->status);
 }

 if (!cmd->data)
  return;

 if (cmd->data->error == 0) {
  dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
 } else {
  dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
   cmd->data->error, host->dbgmsg_dat,
   readl(host->base + S3C2410_SDIDCNT));
 }
}
#else
static void dbg_dumpcmd(struct s3cmci_host *host,
   struct mmc_command *cmd, int fail) { }

static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
      int stop) { }

static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }

#endif /* CONFIG_MMC_DEBUG */

static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
{
 u32 newmask;
//使能imask中设置的中断,中断只有一个但引发中断的因素却有很多
 newmask = readl(host->base + host->sdiimsk);
 newmask |= imask;

 writel(newmask, host->base + host->sdiimsk);

 return newmask;
}

static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
{
 u32 newmask;
//禁能imask中设置的中断
 newmask = readl(host->base + host->sdiimsk);
 newmask &= ~imask;

 writel(newmask, host->base + host->sdiimsk);

 return newmask;
}

static inline void clear_imask(struct s3cmci_host *host)
{//清除中断
 writel(0, host->base + host->sdiimsk);
}

static inline int get_data_buffer(struct s3cmci_host *host,
      u32 *bytes, u32 **pointer)
{
 struct scatterlist *sg;

 if (host->pio_active == XFER_NONE)
  return -EINVAL;

 if ((!host->mrq) || (!host->mrq->data))
  return -EINVAL;

 if (host->pio_sgptr >= host->mrq->data->sg_len) {
  dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
        host->pio_sgptr, host->mrq->data->sg_len);
  return -EBUSY;
 }
 sg = &host->mrq->data->sg[host->pio_sgptr];
//从分散列表中获取一段数据缓存
 *bytes = sg->length;//该段数据缓存的长度
 *pointer = sg_virt(sg);//该段数据缓存的入口地址

 host->pio_sgptr++;//数据段入口前移

 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
     host->pio_sgptr, host->mrq->data->sg_len);

 return 0;
}

static inline u32 fifo_count(struct s3cmci_host *host)
{
 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);

 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
 return fifostat;//返回fifo中数据的个数
}

static inline u32 fifo_free(struct s3cmci_host *host)
{
 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);

 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
 return 63 - fifostat;//返回fifo中剩余空间数
}

static void do_pio_read(struct s3cmci_host *host)
{
 int res;
 u32 fifo;
 u32 *ptr;
 u32 fifo_words;
 void __iomem *from_ptr;

 /* write real prescaler to host, it might be set slow to fix */
 writel(host->prescaler, host->base + S3C2410_SDIPRE);

 from_ptr = host->base + host->sdidata;

 while ((fifo = fifo_count(host))) {
  if (!host->pio_bytes) {
   res = get_data_buffer(host, &host->pio_bytes,
           &host->pio_ptr);//获取一个数据缓存段
   if (res) {
    host->pio_active = XFER_NONE;
    host->complete_what = COMPLETION_FINALIZE;

    dbg(host, dbg_pio, "pio_read(): "
        "complete (no more data).\n");
    return;
   }

   dbg(host, dbg_pio,
       "pio_read(): new target: [%i]@[%p]\n",
       host->pio_bytes, host->pio_ptr);
  }

  dbg(host, dbg_pio,
      "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
      fifo, host->pio_bytes,
      readl(host->base + S3C2410_SDIDCNT));

  /* If we have reached the end of the block, we can
   * read a word and get 1 to 3 bytes.  If we in the
   * middle of the block, we have to read full words,
   * otherwise we will write garbage, so round down to
   * an even multiple of 4. */
  if (fifo >= host->pio_bytes)
   fifo = host->pio_bytes;
  else
   fifo -= fifo & 3;//以字写入的

  host->pio_bytes -= fifo;//已获取的数据缓存段中未用字数减fifo
  host->pio_count += fifo;//已读取的字数加fifo

  fifo_words = fifo >> 2;//字节转换为字
  ptr = host->pio_ptr;
  while (fifo_words--)
   *ptr++ = readl(from_ptr);
  host->pio_ptr = ptr;

  if (fifo & 3) {//如果fifo中的数据非字对齐则读取非对齐部分
   u32 n = fifo & 3;
   u32 data = readl(from_ptr);
   u8 *p = (u8 *)host->pio_ptr;

   while (n--) {
    *p++ = data;
    data >>= 8;
   }
  }
 }

 if (!host->pio_bytes) {//请求的数据已读完
  res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
  if (res) {
   dbg(host, dbg_pio,
       "pio_read(): complete (no more buffers).\n");
   host->pio_active = XFER_NONE;
   host->complete_what = COMPLETION_FINALIZE;

   return;
  }
 }

 enable_imask(host,
       S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
}

static void do_pio_write(struct s3cmci_host *host)
{
 void __iomem *to_ptr;
 int res;
 u32 fifo;
 u32 *ptr;

 to_ptr = host->base + host->sdidata;

 while ((fifo = fifo_free(host)) > 3) {
  if (!host->pio_bytes) {
   res = get_data_buffer(host, &host->pio_bytes,
       &host->pio_ptr);//获取要写入的一段数据
   if (res) {
    dbg(host, dbg_pio,
        "pio_write(): complete (no more data).\n");
    host->pio_active = XFER_NONE;

    return;
   }

   dbg(host, dbg_pio,
       "pio_write(): new source: [%i]@[%p]\n",
       host->pio_bytes, host->pio_ptr);

  }

  /* If we have reached the end of the block, we have to
   * write exactly the remaining number of bytes.  If we
   * in the middle of the block, we have to write full
   * words, so round down to an even multiple of 4. */
  if (fifo >= host->pio_bytes)
   fifo = host->pio_bytes;
  else
   fifo -= fifo & 3;

  host->pio_bytes -= fifo;//已获取的数据段未被写入的数据数减fifo
  host->pio_count += fifo;

  fifo = (fifo + 3) >> 2;//字对齐
  ptr = host->pio_ptr;
  while (fifo--)
   writel(*ptr++, to_ptr);
  host->pio_ptr = ptr;
 }
//数据写到fifo半满时中断,
 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
}

static void pio_tasklet(unsigned long data)
{
 struct s3cmci_host *host = (struct s3cmci_host *) data;


 disable_irq(host->irq);

 if (host->pio_active == XFER_WRITE)
  do_pio_write(host);

 if (host->pio_active == XFER_READ)
  do_pio_read(host);

 if (host->complete_what == COMPLETION_FINALIZE) {//如果数据传输结束
  clear_imask(host);
  if (host->pio_active != XFER_NONE) {
   dbg(host, dbg_err, "unfinished %s "
       "- pio_count:[%u] pio_bytes:[%u]\n",
       (host->pio_active == XFER_READ) ? "read" : "write",
       host->pio_count, host->pio_bytes);

   if (host->mrq->data)
    host->mrq->data->error = -EINVAL;
  }

  finalize_request(host);//结束请求处理函数
 } else
  enable_irq(host->irq);
}

/*
 * ISR for SDI Interface IRQ
 * Communication between driver and ISR works as follows:
 *   host->mrq    points to current request
 *   host->complete_what Indicates when the request is considered done
 *     COMPLETION_CMDSENT   when the command was sent
 *     COMPLETION_RSPFIN          when a response was received
 *     COMPLETION_XFERFINISH   when the data transfer is finished
 *     COMPLETION_XFERFINISH_RSPFIN both of the above.
 *   host->complete_request is the completion-object the driver waits for
 *
 * 1) Driver sets up host->mrq and host->complete_what
 * 2) Driver prepares the transfer
 * 3) Driver enables interrupts
 * 4) Driver starts transfer
 * 5) Driver waits for host->complete_rquest
 * 6) ISR checks for request status (errors and success)
 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
 * 7) ISR completes host->complete_request
 * 8) ISR disables interrupts
 * 9) Driver wakes up and takes care of the request
 *
 * Note: "->error"-fields are expected to be set to 0 before the request
 *       was issued by mmc.c - therefore they are only set, when an error
 *       contition comes up
 */

static irqreturn_t s3cmci_irq(int irq, void *dev_id)
{
 struct s3cmci_host *host = dev_id;
 struct mmc_command *cmd;
 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
 u32 mci_cclear, mci_dclear;
 unsigned long iflags;

 spin_lock_irqsave(&host->complete_lock, iflags);

 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
 mci_imsk = readl(host->base + host->sdiimsk);
 mci_cclear = 0;
 mci_dclear = 0;

 if ((host->complete_what == COMPLETION_NONE) ||
     (host->complete_what == COMPLETION_FINALIZE)) {
  host->status = "nothing to complete";
  clear_imask(host);
  goto irq_out;
 }

 if (!host->mrq) {
  host->status = "no active mrq";
  clear_imask(host);
  goto irq_out;
 }

 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;

 if (!cmd) {
  host->status = "no active cmd";
  clear_imask(host);
  goto irq_out;
 }

 if (!host->dodma) {//如果不使用DMA传输
  if ((host->pio_active == XFER_WRITE) &&
      (mci_fsta & S3C2410_SDIFSTA_TFDET)) {

   disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
   tasklet_schedule(&host->pio_tasklet);//底半部机制
   host->status = "pio tx";
  }

  if ((host->pio_active == XFER_READ) &&
      (mci_fsta & S3C2410_SDIFSTA_RFDET)) {

   disable_imask(host,
          S3C2410_SDIIMSK_RXFIFOHALF |
          S3C2410_SDIIMSK_RXFIFOLAST);

   tasklet_schedule(&host->pio_tasklet);//底半部机制
   host->status = "pio rx";
  }
 }

 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {//命令发送超时
  dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
  cmd->error = -ETIMEDOUT;
  host->status = "error: command timeout";
  goto fail_transfer;
 }

 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {//命令发送结束
  if (host->complete_what == COMPLETION_CMDSENT) {//完成命令发送
   host->status = "ok: command sent";
   goto close_transfer;
  }

  mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;//清除命令发送完成标志
 }

 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {//当命令收到CRC错误
  if (cmd->flags & MMC_RSP_CRC) {
   if (host->mrq->cmd->flags & MMC_RSP_136) {
    dbg(host, dbg_irq,
        "fixup: ignore CRC fail with long rsp\n");
   } else {
    /* note, we used to fail the transfer
     * here, but it seems that this is just
     * the hardware getting it wrong.
     *
     * cmd->error = -EILSEQ;
     * host->status = "error: bad command crc";
     * goto fail_transfer;
    */
   }
  }

  mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
 }

 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {//收到命令响应
  if (host->complete_what == COMPLETION_RSPFIN) {//如果当前任务是完成 接收命令响应
   host->status = "ok: command response received";
   goto close_transfer;//停止传输
  }

  if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)//当前任务是完成数据传输和接收命令响应
   host->complete_what = COMPLETION_XFERFINISH;//当前任务是完成数据传输

  mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;//清除收到命令响应标志
 }

 /* errors handled after this point are only relevant
    when a data transfer is in progress */

 if (!cmd->data)
  goto clear_status_bits;

 /* Check for FIFO failure */
 if (host->is2440) {
  if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
   dbg(host, dbg_err, "FIFO failure\n");
   host->mrq->data->error = -EILSEQ;
   host->status = "error: 2440 fifo failure";
   goto fail_transfer;
  }
 } else {
  if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
   dbg(host, dbg_err, "FIFO failure\n");
   cmd->data->error = -EILSEQ;
   host->status = "error:  fifo failure";
   goto fail_transfer;
  }
 }

 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {//传输失败
  dbg(host, dbg_err, "bad data crc (outgoing)\n");
  cmd->data->error = -EILSEQ;
  host->status = "error: bad data crc (outgoing)";
  goto fail_transfer;
 }

 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {//校验失败
  dbg(host, dbg_err, "bad data crc (incoming)\n");
  cmd->data->error = -EILSEQ;
  host->status = "error: bad data crc (incoming)";
  goto fail_transfer;
 }

 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {//数据传输超时
  dbg(host, dbg_err, "data timeout\n");
  cmd->data->error = -ETIMEDOUT;
  host->status = "error: data timeout";
  goto fail_transfer;
 }

 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {//数据计数器为0,及本次请求的全部数据传输结束
  if (host->complete_what == COMPLETION_XFERFINISH) {
   host->status = "ok: data transfer completed";
   goto close_transfer;//如果当前任务是完成数据传输则结束数据传输
  }

  if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)//当前任务是完成数据传输和接收命令响应
   host->complete_what = COMPLETION_RSPFIN;//如果当前任务是完成 接收命令响应

  mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;//清除数据传输完标志
 }

clear_status_bits:
 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
 writel(mci_dclear, host->base + S3C2410_SDIDSTA);

 goto irq_out;

fail_transfer:
 host->pio_active = XFER_NONE;

close_transfer://结束传输
 host->complete_what = COMPLETION_FINALIZE;//但前任务是完成本次请求

 clear_imask(host);
 tasklet_schedule(&host->pio_tasklet);

 goto irq_out;

irq_out:
 dbg(host, dbg_irq,
     "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
     mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);

 spin_unlock_irqrestore(&host->complete_lock, iflags);
 return IRQ_HANDLED;

}

/*
 * ISR for the CardDetect Pin
*/

static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
{
 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;

 dbg(host, dbg_irq, "card detect\n");
/*
 当卡插入卡槽时引发的中断。该中断将struct delayed_work detect;加入共享工作队列,
 其处理函数为mmc_rescan,识别并初始化卡,
*/
 mmc_detect_change(host->mmc, msecs_to_jiffies(500));

 return IRQ_HANDLED;
}
//该函数在DMA中断中调用的函数s3c2410_dma_buffdone()中调用,调用的条件是一段数据缓存用完
static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
         void *buf_id, int size,
         enum s3c2410_dma_buffresult result)
{
 struct s3cmci_host *host = buf_id;
 unsigned long iflags;
 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;

 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);

 BUG_ON(!host->mrq);
 BUG_ON(!host->mrq->data);
 BUG_ON(!host->dmatogo);

 spin_lock_irqsave(&host->complete_lock, iflags);

 if (result != S3C2410_RES_OK) {
  dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
   "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
   mci_csta, mci_dsta, mci_fsta,
   mci_dcnt, result, host->dmatogo);

  goto fail_request;
 }

 host->dmatogo--;//DMA映射的数据段数
 if (host->dmatogo) {
  dbg(host, dbg_dma, "DMA DONE  Size:%i DSTA:[%08x] "
   "DCNT:[%08x] toGo:%u\n",
   size, mci_dsta, mci_dcnt, host->dmatogo);

  goto out;//还有为传输完的段则out
 }

 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
  size, mci_dsta, mci_dcnt);
//如果所有数据段都传输完了,则设当前任务为,结束任务
 host->complete_what = COMPLETION_FINALIZE;

out:
 tasklet_schedule(&host->pio_tasklet);
 spin_unlock_irqrestore(&host->complete_lock, iflags);
 return;

fail_request:
 host->mrq->data->error = -EINVAL;
 host->complete_what = COMPLETION_FINALIZE;
 writel(0, host->base + host->sdiimsk);
 goto out;

}

static void finalize_request(struct s3cmci_host *host)
{
 struct mmc_request *mrq = host->mrq;
 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
 int debug_as_failure = 0;

 if (host->complete_what != COMPLETION_FINALIZE)
  return;

 if (!mrq)
  return;

 if (cmd->data && (cmd->error == 0) &&
     (cmd->data->error == 0)) {
  if (host->dodma && (!host->dma_complete)) {
   dbg(host, dbg_dma, "DMA Missing!\n");
   return;
  }
 }

 /* Read response from controller. */
 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);//读取响应
 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);

 writel(host->prescaler, host->base + S3C2410_SDIPRE);

 if (cmd->error)
  debug_as_failure = 1;

 if (cmd->data && cmd->data->error)
  debug_as_failure = 1;

 dbg_dumpcmd(host, cmd, debug_as_failure);

 /* Cleanup controller */
 writel(0, host->base + S3C2410_SDICMDARG);
 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
 writel(0, host->base + S3C2410_SDICMDCON);
 writel(0, host->base + host->sdiimsk);

 if (cmd->data && cmd->error)
  cmd->data->error = cmd->error;
//data有数据请求,有传输停止命令stop,!host->cmd_is_stop数据和数据传输命令已发送
 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
  host->cmd_is_stop = 1;
  s3cmci_send_request(host->mmc);//传输停止命令
  return;
 }

 /* If we have no data transfer we are finished here */
 if (!mrq->data)
  goto request_done;

 /* Calulate the amout of bytes transfer if there was no error */
 if (mrq->data->error == 0) {
  mrq->data->bytes_xfered =
   (mrq->data->blocks * mrq->data->blksz);//计算已传输的数据量
 } else {
  mrq->data->bytes_xfered = 0;
 }

 /* If we had an error while transfering data we flush the
  * DMA channel and the fifo to clear out any garbage. */
 if (mrq->data->error != 0) {
  if (host->dodma)
   s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);//DMA通道控制函数

  if (host->is2440) {
   /* Clear failure register and reset fifo. */
   writel(S3C2440_SDIFSTA_FIFORESET |
          S3C2440_SDIFSTA_FIFOFAIL,
          host->base + S3C2410_SDIFSTA);
  } else {
   u32 mci_con;

   /* reset fifo */
   mci_con = readl(host->base + S3C2410_SDICON);
   mci_con |= S3C2410_SDICON_FIFORESET;

   writel(mci_con, host->base + S3C2410_SDICON);
  }
 }

request_done:
 host->complete_what = COMPLETION_NONE;
 host->mrq = NULL;
 mmc_request_done(host->mmc, mrq);
}

static void s3cmci_dma_setup(struct s3cmci_host *host,
        enum s3c2410_dmasrc source)
{
 static enum s3c2410_dmasrc last_source = -1;
 static int setup_ok;

 if (last_source == source)
  return;

 last_source = source;
//如果为读初始化初始源寄存器,为写则初始化初始目的寄存器,以及其相应的控制寄存器
// host->mem->start + host->sdidata放的是物理地址,而非虚拟地址
 s3c2410_dma_devconfig(host->dma, source, 3,
         host->mem->start + host->sdidata);

 if (!setup_ok) {
  s3c2410_dma_config(host->dma, 4, 0);//配置数据传输大小存于chan->dcon 以后写入DMA控制寄存器
  s3c2410_dma_set_buffdone_fn(host->dma,
         s3cmci_dma_done_callback);//设置DMA的一个回调函数,该函数做一段数据传输完后的处理
  s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
  setup_ok = 1;
 }
}

static void s3cmci_send_command(struct s3cmci_host *host,
     struct mmc_command *cmd)
{
 u32 ccon, imsk;
//使能各种诱发中断的因素
 imsk  = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
  S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
  S3C2410_SDIIMSK_RESPONSECRC;

 enable_imask(host, imsk);

 if (cmd->data)//如果有数据传输,则设当前任务为完成数据传输且接收命令响应
  host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
 else if (cmd->flags & MMC_RSP_PRESENT)
  host->complete_what = COMPLETION_RSPFIN;
 else
  host->complete_what = COMPLETION_CMDSENT;//设定当前任务为完成任务发送

 writel(cmd->arg, host->base + S3C2410_SDICMDARG);

 ccon  = cmd->opcode & S3C2410_SDICMDCON_INDEX;
 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;

 if (cmd->flags & MMC_RSP_PRESENT)
  ccon |= S3C2410_SDICMDCON_WAITRSP;

 if (cmd->flags & MMC_RSP_136)
  ccon |= S3C2410_SDICMDCON_LONGRSP;//长响应

 writel(ccon, host->base + S3C2410_SDICMDCON);//开始数据传输
}

static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
{
 u32 dcon, imsk, stoptries = 3;

 /* write DCON register */

 if (!data) {
  writel(0, host->base + S3C2410_SDIDCON);//清零数据控制寄存器
  return 0;
 }

 if ((data->blksz & 3) != 0) {
  /* We cannot deal with unaligned blocks with more than
   * one block being transfered. */

  if (data->blocks > 1) {
   pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
   return -EINVAL;
  }
 }

 while (readl(host->base + S3C2410_SDIDSTA) &
        (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {

  dbg(host, dbg_err,
      "mci_setup_data() transfer stillin progress.\n");
//如果数据正在传输过程中则停止
  writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
  s3cmci_reset(host);//复位SD卡

  if ((stoptries--) == 0) {
   dbg_dumpregs(host, "DRF");
   return -EINVAL;
  }
 }

 dcon  = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;

 if (host->dodma)
  dcon |= S3C2410_SDIDCON_DMAEN;//使能DMA

 if (host->bus_width == MMC_BUS_WIDTH_4)
  dcon |= S3C2410_SDIDCON_WIDEBUS;//数据线宽设置

 if (!(data->flags & MMC_DATA_STREAM))
  dcon |= S3C2410_SDIDCON_BLOCKMODE;//块数据传输

 if (data->flags & MMC_DATA_WRITE) {
  dcon |= S3C2410_SDIDCON_TXAFTERRESP;//数据发送命令响应收到后开始数据传输
  dcon |= S3C2410_SDIDCON_XFER_TXSTART;//数据发送模式
 }

 if (data->flags & MMC_DATA_READ) {
  dcon |= S3C2410_SDIDCON_RXAFTERCMD;//数据发送命令响应收到后开始数据接收
  dcon |= S3C2410_SDIDCON_XFER_RXSTART;//数据接收模式
 }

 if (host->is2440) {
  dcon |= S3C2440_SDIDCON_DS_WORD;//字传输
  dcon |= S3C2440_SDIDCON_DATSTART;//数据开始
 }

 writel(dcon, host->base + S3C2410_SDIDCON);

 /* write BSIZE register */

 writel(data->blksz, host->base + S3C2410_SDIBSIZE);//把块大小写入模块大小寄存器

 /* add to IMASK register */
 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
        S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;

 enable_imask(host, imsk);

 /* write TIMER register */

 if (host->is2440) {
  writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
 } else {
  writel(0x0000FFFF, host->base + S3C2410_SDITIMER);

  /* FIX: set slow clock to prevent timeouts on read */
  if (data->flags & MMC_DATA_READ)
   writel(0xFF, host->base + S3C2410_SDIPRE);
 }

 return 0;
}

#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)

static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
{
 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;

 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);

 host->pio_sgptr = 0;
 host->pio_bytes = 0;
 host->pio_count = 0;
 host->pio_active = rw ? XFER_WRITE : XFER_READ;
//为IO传输初始化一些变量。如果为写则将数据写入fifo,如果为读则使能一些中断。
 if (rw) {
  do_pio_write(host);
  enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
 } else {
  enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
        | S3C2410_SDIIMSK_RXFIFOLAST);
 }

 return 0;
}

static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
{
 int dma_len, i;
 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;

 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
//如果为读初始化初始源寄存器,为写则初始化初始目的寄存器,以及其相应的控制寄存器
//配置数据传输大小存于chan->dcon 以后写入DMA控制寄存器
 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
//合并data->sg上相邻的段,映射一个发散/汇聚 DMA 操作
 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
        (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);

 if (dma_len == 0)
  return -ENOMEM;

 host->dma_complete = 0;
 host->dmatogo = dma_len;//保存合并后的段数

 for (i = 0; i < dma_len; i++) {
  int res;

  dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
   sg_dma_address(&data->sg[i]),
   sg_dma_len(&data->sg[i]));
//分配一个数据段管理结构体,并将各数据段穿成单向链表,以及加载一个数据段到DMA通道
  res = s3c2410_dma_enqueue(host->dma, (void *) host,
       sg_dma_address(&data->sg[i]),
       sg_dma_len(&data->sg[i]));

  if (res) {
   s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
   return -EBUSY;
  }
 }
//开始DMA数据传输,数据传输会在接收到请求后真正开始
 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);

 return 0;
}

static void s3cmci_send_request(struct mmc_host *mmc)
{
 struct s3cmci_host *host = mmc_priv(mmc);
 struct mmc_request *mrq = host->mrq;
 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;

 host->ccnt++;
 prepare_dbgmsg(host, cmd, host->cmd_is_stop);

 /* Clear command, data and fifo status registers
    Fifo clear only necessary on 2440, but doesn't hurt on 2410
 *///清零各状态寄存器
 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);

 if (cmd->data) {
  int res = s3cmci_setup_data(host, cmd->data);//主要是数据控制寄存器的配置

  host->dcnt++;

  if (res) {
   dbg(host, dbg_err, "setup data error %d\n", res);
   cmd->error = res;
   cmd->data->error = res;

   mmc_request_done(mmc, mrq);
   return;
  }

  if (host->dodma)//如果用DMA传输则启动DMA传输,不过数据会在数据传输命令发出后开始传输
   res = s3cmci_prepare_dma(host, cmd->data);
  else//为IO传输初始化一些变量。如果为写则将数据写入fifo,如果为读则使能一些中断。
   res = s3cmci_prepare_pio(host, cmd->data);

  if (res) {
   dbg(host, dbg_err, "data prepare error %d\n", res);
   cmd->error = res;
   cmd->data->error = res;

   mmc_request_done(mmc, mrq);
   return;
  }
 }

 /* Send command */
 s3cmci_send_command(host, cmd);//发送命令

 /* Enable Interrupt */
 enable_irq(host->irq);
}

static int s3cmci_card_present(struct mmc_host *mmc)
{
 struct s3cmci_host *host = mmc_priv(mmc);
 struct s3c24xx_mci_pdata *pdata = host->pdata;
 int ret;

 if (pdata->gpio_detect == 0)
  return -ENOSYS;
//获取卡插入时导致中断的脚的状态
 ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
 return ret ^ pdata->detect_invert;
}

static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
 struct s3cmci_host *host = mmc_priv(mmc);

 host->status = "mmc request";
 host->cmd_is_stop = 0;//表示接下来发送的是CMD而不是STOP
 host->mrq = mrq;

 if (s3cmci_card_present(mmc) == 0) {
  dbg(host, dbg_err, "%s: no medium present\n", __func__);
  host->mrq->cmd->error = -ENOMEDIUM;
  mmc_request_done(mmc, mrq);
 } else
  s3cmci_send_request(mmc);//
}

static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
{
 u32 mci_psc;

 /* Set clock *///寻找一个合适的分频值
 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
  host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));

  if (host->real_rate <= ios->clock)
   break;
 }

 if (mci_psc > 255)
  mci_psc = 255;

 host->prescaler = mci_psc;
 writel(host->prescaler, host->base + S3C2410_SDIPRE);

 /* If requested clock is 0, real_rate will be 0, too */
 if (ios->clock == 0)
  host->real_rate = 0;
}

static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
 struct s3cmci_host *host = mmc_priv(mmc);
 u32 mci_con;

 /* Set the power state */

 mci_con = readl(host->base + S3C2410_SDICON);

 switch (ios->power_mode) {
 case MMC_POWER_ON:
 case MMC_POWER_UP://配置相应的管脚的工作模式
  s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
  s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
  s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
  s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
  s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
  s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);

  if (host->pdata->set_power)
   host->pdata->set_power(ios->power_mode, ios->vdd);

  if (!host->is2440)
   mci_con |= S3C2410_SDICON_FIFORESET;

  break;

 case MMC_POWER_OFF:
 default:
  s3c2410_gpio_setpin(S3C2410_GPE5, 0);
  s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);

  if (host->is2440)
   mci_con |= S3C2440_SDICON_SDRESET;

  if (host->pdata->set_power)
   host->pdata->set_power(ios->power_mode, ios->vdd);

  break;
 }

 s3cmci_set_clk(host, ios);

 /* Set CLOCK_ENABLE */
 if (ios->clock)
  mci_con |= S3C2410_SDICON_CLOCKTYPE;
 else
  mci_con &= ~S3C2410_SDICON_CLOCKTYPE;

 writel(mci_con, host->base + S3C2410_SDICON);

 if ((ios->power_mode == MMC_POWER_ON) ||
     (ios->power_mode == MMC_POWER_UP)) {
//  dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
//   host->real_rate/1000, ios->clock/1000);
 } else {
  dbg(host, dbg_conf, "powered down.\n");
 }

 host->bus_width = ios->bus_width;
}

static void s3cmci_reset(struct s3cmci_host *host)
{
 u32 con = readl(host->base + S3C2410_SDICON);

 con |= S3C2440_SDICON_SDRESET;
 writel(con, host->base + S3C2410_SDICON);//复位卡
}

static int s3cmci_get_ro(struct mmc_host *mmc)
{
 struct s3cmci_host *host = mmc_priv(mmc);
 struct s3c24xx_mci_pdata *pdata = host->pdata;
 int ret;

 if (pdata->gpio_wprotect == 0)
  return 0;

 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);

 if (pdata->wprotect_invert)
  ret = !ret;

 return ret;
}

static struct mmc_host_ops s3cmci_ops = {
 .request = s3cmci_request,//初始化操作函数结构体
 .set_ios = s3cmci_set_ios,
 .get_ro  = s3cmci_get_ro,
 .get_cd  = s3cmci_card_present,
};

static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
 /* This is currently here to avoid a number of if (host->pdata)
  * checks. Any zero fields to ensure reaonable defaults are picked. */
};

#ifdef CONFIG_CPU_FREQ
//当cpu频率发生变化时通知链调用该函数,进行数据传输波特率设置
static int s3cmci_cpufreq_transition(struct notifier_block *nb,
         unsigned long val, void *data)
{
 struct s3cmci_host *host;
 struct mmc_host *mmc;
 unsigned long newclk;
 unsigned long flags;

 host = container_of(nb, struct s3cmci_host, freq_transition);
 newclk = clk_get_rate(host->clk);
 mmc = host->mmc;

 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
     (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
  spin_lock_irqsave(&mmc->lock, flags);

  host->clk_rate = newclk;

  if (mmc->ios.power_mode != MMC_POWER_OFF &&
      mmc->ios.clock != 0)
   s3cmci_set_clk(host, &mmc->ios);

  spin_unlock_irqrestore(&mmc->lock, flags);
 }

 return 0;
}

static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
{
 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
//在通知链cpufreq_transition_notifier_list上增加一个notifier_block即host->freq_transition
 return cpufreq_register_notifier(&host->freq_transition,
      CPUFREQ_TRANSITION_NOTIFIER);
}

static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
{
 cpufreq_unregister_notifier(&host->freq_transition,
        CPUFREQ_TRANSITION_NOTIFIER);
}

#else
static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
{
 return 0;
}

static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
{
}
#endif

static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
{
 struct s3cmci_host *host;
 struct mmc_host *mmc;
 int ret;
//为s3cmci_host和mmc_host分配内存并对mmc_host的一些成员做响应初始化
 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
 if (!mmc) {
  ret = -ENOMEM;
  goto probe_out;
 }

 host = mmc_priv(mmc);//获取私有数据成员
 host->mmc  = mmc;
 host->pdev = pdev;
 host->is2440 = is2440;

 host->pdata = pdev->dev.platform_data;// pdev->dev.platform_data为空
 if (!host->pdata) {
  pdev->dev.platform_data = &s3cmci_def_pdata;
  host->pdata = &s3cmci_def_pdata;
 }

 spin_lock_init(&host->complete_lock);
 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);//实现一个底半部机制,初始化host->pio_tasklet

 if (is2440) {
  host->sdiimsk = S3C2440_SDIIMSK;//中断屏蔽寄存器地址
  host->sdidata = S3C2440_SDIDATA;//数据寄存器地址
  host->clk_div = 1;//预分频值
 } else {
  host->sdiimsk = S3C2410_SDIIMSK;
  host->sdidata = S3C2410_SDIDATA;
  host->clk_div = 2;
 }

 host->dodma  = 0;//默认DMA不是能
 host->complete_what  = COMPLETION_NONE;
 host->pio_active  = XFER_NONE;

 host->dma  = S3CMCI_DMA;//SDI选用的DMA通道

 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 if (!host->mem) {
  dev_err(&pdev->dev,
   "failed to get io memory region resouce.\n");

  ret = -ENOENT;
  goto probe_free_host;
 }

 host->mem = request_mem_region(host->mem->start,
           RESSIZE(host->mem), pdev->name);//申请一段内存,为物理地址,为以后DMA映射

 if (!host->mem) {
  dev_err(&pdev->dev, "failed to request io memory region.\n");
  ret = -ENOENT;
  goto probe_free_host;
 }
//将这段IO内存映射为虚拟地址,返回其基地址
 host->base = ioremap(host->mem->start, RESSIZE(host->mem));
 if (!host->base) {
  dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
  ret = -EINVAL;
  goto probe_free_mem_region;
 }

 host->irq = platform_get_irq(pdev, 0);//获取sdi中断
 if (host->irq == 0) {
  dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
  ret = -EINVAL;
  goto probe_iounmap;
 }

 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {//中断请求,其中断函数实现了底半部机制
  dev_err(&pdev->dev, "failed to request mci interrupt.\n");
  ret = -ENOENT;
  goto probe_iounmap;
 }

 /* We get spurious interrupts even when we have set the IMSK
  * register to ignore everything, so use disable_irq() to make
  * ensure we don't lock the system with un-serviceable requests. */

 disable_irq(host->irq);
//host->pdata->gpio_detect为空所以获取的中断host->irq_cd也应为空,
 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
 host->irq_cd = IRQ_EINT16;//卡的插入中断
 s3c2410_gpio_cfgpin(S3C2410_GPG8, S3C2410_GPG8_EINT16);

 if (host->irq_cd >= 0) {
  if (request_irq(host->irq_cd, s3cmci_irq_cd,//中断申请
    IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,//上升沿和下降沿中断
    DRIVER_NAME, host)) {
   dev_err(&pdev->dev, "can't get card detect irq.\n");
   ret = -ENOENT;
   goto probe_free_irq;
  }
 } else {
  dev_warn(&pdev->dev, "host detect has no irq available\n");
  s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
        S3C2410_GPIO_INPUT);
 }

 if (host->pdata->gpio_wprotect)//写入保护,tq2440开发板上通过0欧电阻接地了
  s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
        S3C2410_GPIO_INPUT);
//获取DMA通道并申请DMA中断
 if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
  dev_err(&pdev->dev, "unable to get DMA channel.\n");
  ret = -EBUSY;
  goto probe_free_irq_cd;
 }
//获取"sdi"时钟
 host->clk = clk_get(&pdev->dev, "sdi");
 if (IS_ERR(host->clk)) {
  dev_err(&pdev->dev, "failed to find clock source.\n");
  ret = PTR_ERR(host->clk);
  host->clk = NULL;
  goto probe_free_host;
 }

 ret = clk_enable(host->clk);//使能"sdi"时钟
 if (ret) {
  dev_err(&pdev->dev, "failed to enable clock source.\n");
  goto clk_free;
 }

 host->clk_rate = clk_get_rate(host->clk);//获取其输入频率

 mmc->ops  = &s3cmci_ops;//
 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;//电源管理
 mmc->caps = MMC_CAP_4_BIT_DATA;//线宽为四位
 mmc->f_min  = host->clk_rate / (host->clk_div * 256);//设定最小工作频率
 mmc->f_max  = host->clk_rate / host->clk_div;//设定最大工作频率

 if (host->pdata->ocr_avail)//host->pdata为空,tq2440开发板上移植的SD驱动设这样的
  mmc->ocr_avail = host->pdata->ocr_avail;

 mmc->max_blk_count = 4095;
 mmc->max_blk_size = 4095;
 mmc->max_req_size = 4095 * 512;
 mmc->max_seg_size = mmc->max_req_size;

 mmc->max_phys_segs = 128;
 mmc->max_hw_segs = 128;

 dbg(host, dbg_debug,
     "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
     (host->is2440?"2440":""),
     host->base, host->irq, host->irq_cd, host->dma);

 ret = s3cmci_cpufreq_register(host);//cpu频率改变通知链注册
 if (ret) {
  dev_err(&pdev->dev, "failed to register cpufreq\n");
  goto free_dmabuf;
 }

 ret = mmc_add_host(mmc);//添加mmc_host调用device_add(&host->class_dev);
 if (ret) {
  dev_err(&pdev->dev, "failed to add mmc host.\n");
  goto free_cpufreq;
 }

 platform_set_drvdata(pdev, mmc);//将mmc_host设为pdev的drvdata
 dev_info(&pdev->dev, "initialisation done.\n");

 return 0;

 free_cpufreq:
 s3cmci_cpufreq_deregister(host);

 free_dmabuf:
 clk_disable(host->clk);

 clk_free:
 clk_put(host->clk);

 probe_free_irq_cd:
 if (host->irq_cd >= 0)
  free_irq(host->irq_cd, host);

 probe_free_irq:
 free_irq(host->irq, host);

 probe_iounmap:
 iounmap(host->base);

 probe_free_mem_region:
 release_mem_region(host->mem->start, RESSIZE(host->mem));

 probe_free_host:
 mmc_free_host(mmc);
 probe_out:
 return ret;
}

static void s3cmci_shutdown(struct platform_device *pdev)
{
 struct mmc_host *mmc = platform_get_drvdata(pdev);
 struct s3cmci_host *host = mmc_priv(mmc);

 if (host->irq_cd >= 0)
  free_irq(host->irq_cd, host);

 s3cmci_cpufreq_deregister(host);
 mmc_remove_host(mmc);
 clk_disable(host->clk);
}

static int __devexit s3cmci_remove(struct platform_device *pdev)
{
 struct mmc_host  *mmc  = platform_get_drvdata(pdev);
 struct s3cmci_host *host = mmc_priv(mmc);

 s3cmci_shutdown(pdev);

 clk_put(host->clk);

 tasklet_disable(&host->pio_tasklet);
 s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);

 free_irq(host->irq, host);

 iounmap(host->base);
 release_mem_region(host->mem->start, RESSIZE(host->mem));

 mmc_free_host(mmc);
 return 0;
}

static int __devinit s3cmci_2410_probe(struct platform_device *dev)
{
 return s3cmci_probe(dev, 0);
}

static int __devinit s3cmci_2412_probe(struct platform_device *dev)
{
 return s3cmci_probe(dev, 1);
}

static int __devinit s3cmci_2440_probe(struct platform_device *dev)
{
 return s3cmci_probe(dev, 1);
}

#ifdef CONFIG_PM

static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
{
 struct mmc_host *mmc = platform_get_drvdata(dev);

 return  mmc_suspend_host(mmc, state);
}

static int s3cmci_resume(struct platform_device *dev)
{
 struct mmc_host *mmc = platform_get_drvdata(dev);

 return mmc_resume_host(mmc);
}

#else /* CONFIG_PM */
#define s3cmci_suspend NULL
#define s3cmci_resume NULL
#endif /* CONFIG_PM */


static struct platform_driver s3cmci_2410_driver = {
 .driver.name = "s3c2410-sdi",
 .driver.owner = THIS_MODULE,
 .probe  = s3cmci_2410_probe,
 .remove  = __devexit_p(s3cmci_remove),
 .shutdown = s3cmci_shutdown,
 .suspend = s3cmci_suspend,
 .resume  = s3cmci_resume,
};

static struct platform_driver s3cmci_2412_driver = {
 .driver.name = "s3c2412-sdi",
 .driver.owner = THIS_MODULE,
 .probe  = s3cmci_2412_probe,
 .remove  = __devexit_p(s3cmci_remove),
 .shutdown = s3cmci_shutdown,
 .suspend = s3cmci_suspend,
 .resume  = s3cmci_resume,
};

static struct platform_driver s3cmci_2440_driver = {
 .driver.name = "s3c2440-sdi",
 .driver.owner = THIS_MODULE,
 .probe  = s3cmci_2440_probe,
 .remove  = __devexit_p(s3cmci_remove),
 .shutdown = s3cmci_shutdown,
 .suspend = s3cmci_suspend,
 .resume  = s3cmci_resume,
};


static int __init s3cmci_init(void)
{
 platform_driver_register(&s3cmci_2410_driver);
 platform_driver_register(&s3cmci_2412_driver);
 platform_driver_register(&s3cmci_2440_driver);
 return 0;
}

static void __exit s3cmci_exit(void)
{
 platform_driver_unregister(&s3cmci_2410_driver);
 platform_driver_unregister(&s3cmci_2412_driver);
 platform_driver_unregister(&s3cmci_2440_driver);
}

module_init(s3cmci_init);
module_exit(s3cmci_exit);

MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Thomas Kleffel <>, Ben Dooks <>");
MODULE_ALIAS("platform:s3c2410-sdi");
MODULE_ALIAS("platform:s3c2412-sdi");
MODULE_ALIAS("platform:s3c2440-sdi");

阅读(202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~