/* s3cmci.h */
/*
* linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
*
* Copyright (C) 2004-2006 Thomas Kleffel, All Rights Reserved.
*
* 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.
*/
/* FIXME: DMA Resource management ?! */
#define S3CMCI_DMA 0
enum s3cmci_waitfor { /* 等待类型 */
COMPLETION_NONE, /* 没有完成 */
COMPLETION_FINALIZE, /* 完成 */
COMPLETION_CMDSENT, /* 命令发送 */
COMPLETION_RSPFIN, /* 响应接收结束 */
COMPLETION_XFERFINISH, /* 传输完成 */
COMPLETION_XFERFINISH_RSPFIN, /* 传输完成,响应接收结束 */
};
struct s3cmci_host { /* s3c24xx主机控制器描述结构体 */
struct platform_device *pdev; /* 平台设备 */
struct s3c24xx_mci_pdata *pdata; /* */
struct mmc_host *mmc; /* mmc主机控制器 */
struct resource *mem; /* 平台资源 */
struct clk *clk; /* 时钟 */
void __iomem *base; /* IO内存基地址 */
int irq; /* 中断号 */
int irq_cd; /* CD_SD中断 */
int dma; /* DMA通道=S3CMCI_DMA=0 */
unsigned long clk_rate; /* 时钟频率 */
unsigned long clk_div; /* 时钟分频比 =1*/
unsigned long real_rate; /* 真正的频率 */
u8 prescaler; /* 分频比 */
unsigned sdiimsk; /* =0x3c */
unsigned sdidata; /* =0x40 */
int dodma; /* 使用DMA传输 */
int dmatogo; /* DMA传输计数器 */
struct mmc_request *mrq; /* mmc请求 */
int cmd_is_stop; /* 标记是否为停止命令 */
spinlock_t complete_lock;
enum s3cmci_waitfor complete_what; /* 完成什么*/
int dma_complete; /*DMA完成 */
u32 pio_sgptr; /* 缓冲区引索----作为scatterlist数组的下标引索 */
u32 pio_bytes; /* 缓冲区大小----=struct scatterlist sg->length */
u32 pio_count; /* 缓冲区数据计数器 */
u32 *pio_ptr; /* 指向缓冲区sg 的虚拟地址----=sg_virt(sg)*/
#define XFER_NONE 0 /* 无 */
#define XFER_READ 1 /* 读 */
#define XFER_WRITE 2 /* 写 */
u32 pio_active; /* 读写标志位 ----XFER_WRITE : XFER_READ*/
int bus_width; /* 总线宽度 */
char dbgmsg_cmd[301];
char dbgmsg_dat[301];
char *status; /* 表示状态----"pio tx"/"pio rx" 等*/
unsigned int ccnt, dcnt; /* 计数器 */
struct tasklet_struct pio_tasklet; /* 任务结构 */
#ifdef CONFIG_CPU_FREQ
struct notifier_block freq_transition; /* 通知块 */
#endif
};
/*************************************************************************************************************************************/
/* s3cmci.c */
/*
* linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
*
* Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <>
*
* Current driver maintained by Ben Dooks and Simtec Electronics
* Copyright (C) 2008 Simtec Electronics <>
*
* 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.
*/
/*
MMC驱动程序可以分为通用设备层、MMC抽象层、MMC协议层和具体设备层
通用设备层:其对于块设备来说,主要负责设备内核对象在sysfs文件系统中的管理、请求队列管理、
与文件系统接口
MMC抽象层:其抽象出MMC卡的共性,如MMC卡的请求管理、电源管理等
MMC协议层:其将MMC操作分解成标准的MMC协议
具体设备层:则负责具体物理设备的寄存器控制等,其主要有两部分内容:第一是创建通用硬盘结构项
系统注册,以便系统对MMC设备管理,第二是要完成系统分发过来的读写请求的处理
*/
#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",
};
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 */
/* 中断控制:
mode=1则使能中断
mode=0则禁止中断
mode=-1则清中断*/
static inline u32 enable_imask(struct s3cmci_host *host, u32 imask, int mode)
{
u32 newmask;
newmask = readl(host->base + host->sdiimsk);
if(mode==1)
newmask |= imask;
else if(mode==0)/* 禁止中断 */
newmask &= ~imask;
else if(mode==-1) /* 清中断 */
newmask =0;
writel(newmask, host->base + host->sdiimsk);
return newmask;
}
#define disable_imask(host,imask) enable_imask(host, imask, 0)
#define clear_imask(host) enable_imask(host, 0, -1)
#if 0
/* 禁止中断 */
static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
{
u32 newmask;
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);
}
#endif
/* 获取数据缓冲区 */
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;
}
/* 获取FIFO中的数据量 */
static inline u32 fifo_count(struct s3cmci_host *host)
{
u32 fifostat = readl(host->base + S3C2410_SDIFSTA);/* 读取SDI FIFO状态寄存器SDIFSTA */
fifostat &= S3C2410_SDIFSTA_COUNTMASK; /* 取出FIFO 计数----FIFO中的数据量*/
return fifostat;
}
/* 返回FIFO中空闲区大小 */
static inline u32 fifo_free(struct s3cmci_host *host)
{
u32 fifostat = readl(host->base + S3C2410_SDIFSTA); /* 读取SDI FIFO状态寄存器SDIFSTA */
fifostat &= S3C2410_SDIFSTA_COUNTMASK; /* 获取FIFO计数----FIFO中的数据量(字节) */
return 63 - fifostat; /* 返回FIFO中空闲区大小 */
}
/* 使用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))) { /* fifo中数据量不为0 */
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中数据量大于缓冲区的长度 */
fifo = host->pio_bytes;
else
fifo -= fifo & 3;
host->pio_bytes -= fifo; /* 计数缓冲区中剩余的空间 */
host->pio_count += fifo; /* 计数缓冲区中已有数据 */
fifo_words = fifo >> 2; /* 转换为字 */
ptr = host->pio_ptr; /* 指向缓冲区 */
while (fifo_words--)
*ptr++ = readl(from_ptr); /* 将值读取到缓冲区 */
host->pio_ptr = ptr; /* 指向新的位置 */
if (fifo & 3) { /* 如果数据不是字对齐的 */
u32 n = fifo & 3;
u32 data = readl(from_ptr); /* 读取剩余数据 */
u8 *p = (u8 *)host->pio_ptr; /* 将缓冲区的指针转换为unsigned char */
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,1); /* 使能Rx FIFO半填满中断和Rx FIFO得到最后数据中断 */
}
/* 使用FIFO进行写操作 */
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; /* 指向SDI数据寄存器SDIDAT */
while ((fifo = fifo_free(host)) > 3) { /* 如果FIFO中空闲区大于3 */
if (!host->pio_bytes) { /* 如果没有数据 */
res = get_data_buffer(host, &host->pio_bytes,
&host->pio_ptr); /* 获取数据缓冲区,参数 host->pio_ptr指向缓冲区,参数host->pio_bytes指向缓冲区大小*/
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空闲区大于缓冲区字节数----写的最后一次 */
fifo = host->pio_bytes;
else
fifo -= fifo & 3;
host->pio_bytes -= fifo; /* 计算缓冲区中剩余的数据的大小 */
host->pio_count += fifo; /* 缓冲区计数器加上填入FIFO的数据大小 */
fifo = (fifo + 3) >> 2; /* 将字节转换为字*/
ptr = host->pio_ptr;
while (fifo--)
writel(*ptr++, to_ptr); /* 依次发送数据 */
host->pio_ptr = ptr;
}
enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF,1); /* 如果FIFO中空闲区小于3 则使能Tx FIFO半填满中断 */
}
/* tasklet绑定的函数-----作为中断处理的底半部----在执行tasklet_schedule()函数后,由内核在适当的时候调度运行 */
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); /* 使用FIFO进行写操作 */
if (host->pio_active == XFER_READ) /* 读 */
do_pio_read(host); /* 使用FIFO进行读操作 */
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); /* 读取FIFO状态寄存器的值*/
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)) { /* 如果是写数据并且Tx发现可用FIFO(FIFO状态寄存器的值& S3C2410_SDIFSTA_TFDET)*/
disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF); /* 禁止 Tx半填满中断*/
tasklet_schedule(&host->pio_tasklet); /* 调度pio_tasklet函数运行 */
host->status = "pio tx";
}
if ((host->pio_active == XFER_READ) &&
(mci_fsta & S3C2410_SDIFSTA_RFDET)) { /* 如果是读数据 并且Rx发现可用FIFO(FIFO状态寄存器的值& S3C2410_SDIFSTA_RFDET)*/
disable_imask(host,
S3C2410_SDIIMSK_RXFIFOHALF |
S3C2410_SDIIMSK_RXFIFOLAST); /* 禁止Rx FIFO半填满中断和 Rx FIFO得到最后数据中断*/
tasklet_schedule(&host->pio_tasklet); /* 调度pio_tasklet函数运行 */
host->status = "pio rx";
}
}
if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) { /*如果命令超时(命令状态寄存器的值& (1<<10))*/
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) { /*命令发出(命令状态寄存器的值& (1<<11))*/
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失败(命令状态寄存器的值& (1<<12))*/
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) { /*响应结束(FIFO状态寄存器的值& (1<<9))*/
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 (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {/*FIFO 失败错误(命令状态寄存器的值& (3<<14))*/
dbg(host, dbg_err, "FIFO failure\n");
host->mrq->data->error = -EILSEQ;
host->status = "error: 2440 fifo failure";
goto fail_transfer;
}
if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) { /*数据块接收错误---CRC校验失败(数据状态寄存器的值& (1<<6))*/
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) { /*数据发送时CRC状态错误(数据状态寄存器的值& (1<<7))*/
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) { /*数据传输超时(数据状态寄存器的值& (1<<5))*/
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) { /*数据传输结束(数据状态寄存器的值& (1<<4))*/
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
*/
/* CD_SD中断处理函数 */
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");
mmc_detect_change(host->mmc, msecs_to_jiffies(500)); /* 处理mmc插槽变化的状态 */
return IRQ_HANDLED;
}
/* 设置相应的dma通道完成一次dma传输后的回调函数,也即是s3c2410_dma_enqueue完成后会调用的函数 */
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); /* 读命令状态寄存器SDICMDSTA */
mci_dsta = readl(host->base + S3C2410_SDIDSTA); /* 读数据状态寄存器SDIDATSTA */
mci_fsta = readl(host->base + S3C2410_SDIFSTA); /* 读 FIFO 状态寄存器*/
mci_dcnt = readl(host->base + S3C2410_SDIDCNT); /* 读数据持续计数器寄存器SDIDATCNT */
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) { /* DMA传输未完成 */
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;
}
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); /* 调度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); /* SDI中断屏蔽寄存器=0---->禁止所有的中断 */
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); /* 读取响应寄存器0的值-----卡的状态 */
cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1); /* 读取响应寄存器0的值-----SDIRSP1[31:24]---CRC7/卡状态[95:88]长
---SDIRSP1[23:0]---卡状态[87:64]长*/
cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2); /* 读取响应寄存器0的值-----卡状态[63:32] 长*/
cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3); /* 读取响应寄存器0的值-----卡状态[31:0] 长*/
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); /* 命令参数寄存器SDICMDARG=0 */
writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON); /* 启动数据传输 */
writel(0, host->base + S3C2410_SDICMDCON); /* 命令控制寄存器SDICMDCON=0 */
writel(0, host->base + host->sdiimsk); /* 禁止所有中断 */
if (cmd->data && cmd->error)
cmd->data->error = cmd->error;
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) /* 如果是使用DMA传输 */
s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
/* Clear failure register and reset fifo. */
writel(S3C2440_SDIFSTA_FIFORESET |S3C2440_SDIFSTA_FIFOFAIL,host->base + S3C2410_SDIFSTA); /* FIFP状态寄存器 SDIFSTA----复位FIFO,清除FIFO失败错误*/
}
request_done:
host->complete_what = COMPLETION_NONE;
host->mrq = NULL; /* 设置请求为NULL */
mmc_request_done(host->mmc, mrq); /* 完成处理一个mmc请求 */
}
/* 设置DMA */
static void s3cmci_dma_setup(struct s3cmci_host *host,
enum s3c2410_dmasrc source) /* (host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW) */
{
static enum s3c2410_dmasrc last_source = -1;
static int setup_ok;
if (last_source == source)
return;
last_source = source;
s3c2410_dma_devconfig(host->dma, source, 3, /* 3--->源在外设总线上,源地址固定 */
host->mem->start + host->sdidata); /* 配置设备的DMA -----host->mem->start + host->sdidata=0x5a000040--->SDI数据寄存器的地址*/
if (!setup_ok) {
s3c2410_dma_config(host->dma, 4, 0); /* 配置DMA----以字为传输单位 */
s3c2410_dma_set_buffdone_fn(host->dma,
s3cmci_dma_done_callback); /* 设置相应的dma通道完成一次dma传输后的回调函数,也即是s3c2410_dma_enqueue完成后会调用的函数 */
s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);/* 设置DMA标记 */
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; /* 如果CRC状态错误则产生SDI中断; 如果命令响应超时则SDI产生中断;
如果收到响应则SDI产生中断;如果命令已经发送则SDI产生中断;
如果响应CRC则SDI产生中断*/
enable_imask(host, imsk,1);
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); /* 将命令写入SDI命令参数寄存器SDICMDARG */
ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX; /* 获取命令引索----写到SDICMDCON[7:0] */
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; /* 主机接收136位长响应 */
writel(ccon, host->base + S3C2410_SDICMDCON); /* 将设置好的值写入寄存器SDICOMMAND寄存器 */
}
/* 设置与命令相关的数据 */
static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
{
u32 dcon, imsk, stoptries = 3;
/* write DCON register */
if (!data) { /* 如果data=NULL */
writel(0, host->base + S3C2410_SDIDCON); /* 数据控制寄存器SDIDATCON=0 */
return 0;
}
if ((data->blksz & 3) != 0) { /* 检查块大小是否4字节对齐 */
/* 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)) { /* SDIDATSTA&( (1<<1)|(1<<0) )----当数据在传输中*/
dbg(host, dbg_err,
"mci_setup_data() transfer still in progress.\n");
writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON); /* 数据控制寄存器SDIDATCON=(1<<14)---数据启动传输 */
s3cmci_reset(host); /* 复位主机控制器 */
if ((stoptries--) == 0) { /* 次数超过3次则返回 */
dbg_dumpregs(host, "DRF");
return -EINVAL;
}
}
dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK; /* 取出data->blocks的低12位*/
if (host->dodma) /* 如果使用DMA传输则dcon|(1<<15) --->DMA使能*/
dcon |= S3C2410_SDIDCON_DMAEN;
if (host->bus_width == MMC_BUS_WIDTH_4) /* 如果使用的总线宽度是4位则dcon |(1<<16) --->设置总线宽度模式为4位 */
dcon |= S3C2410_SDIDCON_WIDEBUS;
if (!(data->flags & MMC_DATA_STREAM)) /* 如果没有使用流 则dcon |(1<<17) --->块数据传输*/
dcon |= S3C2410_SDIDCON_BLOCKMODE;
if (data->flags & MMC_DATA_WRITE) { /* 如果是写数据则dcon |(1<<20)|(3<<12) --->datmode:数据发送模式,当数据传输是在收到响应后开始*/
dcon |= S3C2410_SDIDCON_TXAFTERRESP;
dcon |= S3C2410_SDIDCON_XFER_TXSTART;
}
if (data->flags & MMC_DATA_READ) { /* 如果是读数据则dcon |(1<<19)|(2<<12) --->datmode:数据接收模式,当数据接收是在收到响应后开始 */
dcon |= S3C2410_SDIDCON_RXAFTERCMD;
dcon |= S3C2410_SDIDCON_XFER_RXSTART;
}
dcon |= S3C2440_SDIDCON_DS_WORD; /* dcon |(2<<22) ---> 指出带FIFO的传输大小为字传输*/
dcon |= S3C2440_SDIDCON_DATSTART; /* dcon |(1<<14) ---> 数据开始传输*/
writel(dcon, host->base + S3C2410_SDIDCON); /* 将设置好的dcon的值写入数据控制寄存器SDIDATCON */
/* write BSIZE register */
writel(data->blksz, host->base + S3C2410_SDIBSIZE); /* 将数据块的大小写入块大小寄存器 */
/* add to IMASK register */
/* 中断屏蔽寄存器imsk=(1<<11)|(1<<9)|(1<<8)|(1<<7) --->如果FIFO失败发送则SDI产生中断; 如果数据接收CRC失败则SDI产生
中断;如果数据接收超时发送则SDI产生中断;如果数据计数器为0则产生SDI产生中断
*/
imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
enable_imask(host, imsk,1); /* 使能中断 */
/* write TIMER register */
writel(0x007FFFFF, host->base + S3C2410_SDITIMER); /* SDI数据/忙定时器寄存器SDIDTIMER=0x007FFFFF----设置数据/忙超时时间 */
/* FIX: set slow clock to prevent timeouts on read */
if (data->flags & MMC_DATA_READ) /* 如果是读数据 */
writel(0xFF, host->base + S3C2410_SDIPRE); /* 设置SDI波特率预分频寄存器SDIPRE=0xFF--->波特率=PCLK/255+1=50MHZ/256=200KHZ */
}
return 0;
}
#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
/* 准备FIFO进行传输 */
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;
if (rw) { /*如果是写 */
do_pio_write(host); /* 写数据到FIFO */
enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF,1); /* 使能 Tx FIFO半满则产生中断*/
} else { /* 读 */
enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
| S3C2410_SDIIMSK_RXFIFOLAST,1); /* 否则使能 使能 Tx FIFO半满或Rx FIFO 得到最后数据则产生中断**/
}
return 0;
}
/* 为DMA传输做准备 */
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; /* 如果是写数据则rw=1,否则rw=0 */
BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW); /* 设置DMA ----如果是写数据则源数据在内存,如果是读则源在硬件设备*/
s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH); /* /* 释放DMA通道所申请的所有内存资源 */*/
dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
(rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); /* 映射scatterlist缓冲区,返回值为要传输的DMA缓冲区数 */
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]));
res = s3c2410_dma_enqueue(host->dma, (void *) host,
sg_dma_address(&data->sg[i]),
sg_dma_len(&data->sg[i])); /* 对每个DMA缓冲区进行排队,等待处理?*/
if (res) {
s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
return -EBUSY;
}
}
s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START); /* 启动DMA 传输 */
return 0;
}
/* 发送请求 */
static void s3cmci_send_request(struct mmc_host *mmc)
{
struct s3cmci_host *host = mmc_priv(mmc);
struct mmc_request *mrq = host->mrq; /* 获取mmc请求 */
struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd; /* 获取mmc命令 */
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); /* 初始化SDI命令状态寄存器SDICMDSTA*/
writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA); /* 初始化SDI数据状态寄存器SDIDATSTA */
writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA); /* 初始化SDI FIFO寄存器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); /* 完成处理一个mmc请求 */
return;
}
if (host->dodma) /* 使用DMA 传输 */
res = s3cmci_prepare_dma(host, cmd->data);
else
res = s3cmci_prepare_pio(host, cmd->data); /* 准备FIFO进行传输 */
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); /* 使能中断 */
}
/* 回调函数,返回0 表示没有卡,返回1表示当前的卡*/
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; /* 如果pdata->gpio_detect=1 则ret=0,否则ret=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;
host->mrq = mrq;
if (s3cmci_card_present(mmc) == 0) { /* 如果 s3cmci_card_present函数返回0则说明没有卡*/
dbg(host, dbg_err, "%s: no medium present\n", __func__);
host->mrq->cmd->error = -ENOMEDIUM;
mmc_request_done(mmc, mrq); /* 完成处理一个mmc请求 */
} 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;
}
/* 控制器对卡的I/O状态设置函数, 参数ios是核心层传递过来的*/
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); /* 读取 SDI控制寄存器SDICON*/
switch (ios->power_mode) {
/* 根据电源模式配置GPIO引脚 */
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);
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=SDICON */
con |= S3C2440_SDICON_SDRESET; /* SDICON|(1<<8)--->SDMMC复位 */
writel(con, host->base + S3C2410_SDICON); /* 写入值 */
}
/* 回调函数----判断我们的卡是否是写保护的,返回0表示卡可读写 ,返回1表示卡只读*/
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,/* 控制器对卡的I/O状态设置函数, 参数ios是核心层传递过来的*/
.get_ro = s3cmci_get_ro, /* 回调函数----判断我们的卡是否是写保护的,返回0表示卡可读写 ,返回1表示卡只读*/
.get_cd = s3cmci_card_present, /* 回调函数,返回0 表示没有卡,返回-1表示当前的卡*/
};
static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
.gpio_detect=S3C2410_GPG8, /* S3C2410_GPG8---IRQ_EINT16----CD_SD中断 */
.set_power=NULL,
.gpio_wprotect=S3C2410_GPH8, /* S3C2410_GPH8---WP_SD */
.detect_invert =0,
.wprotect_invert=0,
};
#ifdef CONFIG_CPU_FREQ
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;
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
/*
devs.c中定义的
平台设备:
static struct resource s3c_sdi_resource[] = { //SD卡设备资源
[0] = {
.start = S3C24XX_PA_SDI, // 0x5A000000
.end = S3C24XX_PA_SDI + S3C24XX_SZ_SDI - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_SDI,
.end = IRQ_SDI,
.flags = IORESOURCE_IRQ,
}
};
struct platform_device s3c_device_sdi = {
.name = "s3c2410-sdi",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_sdi_resource),
.resource = s3c_sdi_resource,
};
*/
/* 探测函数 */
static int __devinit s3cmci_probe(struct platform_device *pdev)
{
struct s3cmci_host *host;
struct mmc_host *mmc;
int ret;
mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev); /* 分配struct s3cmci_host和struct mmc_host*/
if (!mmc) {
ret = -ENOMEM;
goto probe_out;
}
host = mmc_priv(mmc); /* 获取 struct s3cmci_host在上面分配的内存的起始地址*/
host->mmc = mmc;
host->pdev = pdev;
host->pdata = pdev->dev.platform_data;
if (!host->pdata) {
pdev->dev.platform_data = &s3cmci_def_pdata; /*s3cmci_def_pdata为空 */
host->pdata = &s3cmci_def_pdata;
}
spin_lock_init(&host->complete_lock);
tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host); /* 初始化tasklet,pio_tasklet函数会被内核在合适的时间调度运行 */
host->sdiimsk = S3C2440_SDIIMSK; /* host->sdiimsk=0x3c */
host->sdidata = S3C2440_SDIDATA; /*host->sdidata=0x40 */
host->clk_div = 1;
host->dodma = 0;
host->complete_what = COMPLETION_NONE;
host->pio_active = XFER_NONE;
host->dma = S3CMCI_DMA;
host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); /*获取资源 [0] = {
.start = S3C24XX_PA_SDI, // 0x5A000000
.end = S3C24XX_PA_SDI + S3C24XX_SZ_SDI - 1,
.flags = IORESOURCE_MEM,
},*/
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); /* 分配并初始化一个struct resource结构体 */
if (!host->mem) {
dev_err(&pdev->dev, "failed to request io memory region.\n");
ret = -ENOENT;
goto probe_free_host;
}
host->base = ioremap(host->mem->start, RESSIZE(host->mem)); /* 映射IO端口 */
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); /* 获取中断资源即host->irq=IRQ_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)) { /* 申请中断,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->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect); /* host->irq_cd=IRQ_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); /* 否则配置 gpio_detect为输入*/
}
if (host->pdata->gpio_wprotect)
s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
S3C2410_GPIO_INPUT); /* 配置GPH8为输入 */
if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) { /* 申请通道0的DMA资源 */
dev_err(&pdev->dev, "unable to get DMA channel.\n");
ret = -EBUSY;
goto probe_free_irq_cd;
}
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); /* 使能时钟*/
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支持的电压 范围为3.2~3.4V*/
mmc->caps = MMC_CAP_4_BIT_DATA; /*设置主机能够处理4位传输模式 */
mmc->f_min = host->clk_rate / (host->clk_div * 256); /* 设置最低时钟频率为 host->clk_rate/256=PCLK/256*/
mmc->f_max = host->clk_rate / host->clk_div; /* 设置最大时钟频率为host->clk_rate / 1=PCLK */
if (host->pdata->ocr_avail)
mmc->ocr_avail = host->pdata->ocr_avail;
mmc->max_blk_count = 4095; /* 设置一个请求中最大的块数为4095 */
mmc->max_blk_size = 4095; /* 设置mmc块中最大尺寸为4095 */
mmc->max_req_size = 4095 * 512; /* 设置一个请求中最大字节数为4095 * 512 */
mmc->max_seg_size = mmc->max_req_size; /* 设置最大片段尺寸为4095 * 512 */
mmc->max_phys_segs = 128; /* 设置最大物理片段数为128字节 */
mmc->max_hw_segs = 128; /* 设置最大硬件片段数为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); /* 添加一个主机 */
if (ret) {
dev_err(&pdev->dev, "failed to add mmc host.\n");
goto free_cpufreq;
}
platform_set_drvdata(pdev, mmc); /* struct platform_device *pdev->dev->driver_data = mmc */
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); /* 禁止tasklet */
s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client); /* 释放DMA通道 */
free_irq(host->irq, host); /* 释放中断 */
iounmap(host->base);
release_mem_region(host->mem->start, RESSIZE(host->mem));
mmc_free_host(mmc); /* 释放主机控制器 */
return 0;
}
#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_2440_driver = {
.driver.name = "s3c2440-sdi",
.driver.owner = THIS_MODULE,
.probe = s3cmci_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_2440_driver);
return 0;
}
static void __exit s3cmci_exit(void)
{
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");