Linux SD卡/SDIO驱动开发3-core

核心的一个函数便是之前提到的位于core.c的mmc_rescan,概括来讲,主要完成两项任务,即
扫描SD总线,插入SD卡
扫描SD总线,拔出SD卡

插入SD卡

插入SD卡,主控制器产生中断,dw_mci_interrupt 判断host->state == STATE_SENDING_CMD11,调用dw_mci_cmd_interrupt
调用tasklet_schedule(&host->tasklet); 到mmc_rescan. 在dw_mci_probe文章中有。

插入SD卡,mmc_rescan扫描SD总线上是否存在SD卡,具体的实现方法就是通过向SD卡上电,看是否能成功,以普通SD卡为例,为普通SD卡上电的函数mmc_send_app_op_cond(host, 0, &ocr);
如果上电成功,则返回0,即执行if()里的mmc_attach_sd()进行总线与SD卡的绑定。
如果上电失败,则返回非0值,跳过if(),尝试其他上电的方法。

那么,上电方法究竟有何不同呢?具体看看mmc_send_app_op_cond()的实现过程:

mmc_attach_sd
    
int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
{
	struct mmc_command cmd;
	cmd.opcode = SD_APP_OP_COND;    /* #define SD_APP_OP_COND   41   */
	mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
 
	... ...
int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card, struct mmc_command *cmd, int retries)
{
 
  mmc_app_cmd(host, card);   /* #define MMC_APP_CMD   55   */
  mrq.cmd = cmd;
  cmd->data = NULL;
 
  mmc_wait_for_req(host, &mrq);
 
  ... ...
 
}

这里的指令SD_APP_OP_COND只有SD2.0的协议支持,也就是说,只有普通SD卡支持,所以也只有普通SD卡能够成功上电。
在这里插入图片描述
在这里插入图片描述如果上电成功,就开始进行总线与SD卡的绑定,通过mmc_attach_sd(),绑定过程可分为四步,

注册SD总线上的操作函数 - struct mmc_bus_ops mmc_sd_ops
设置主控制器的时钟和总线方式 - 通过回调函数host->ops->set_ios();
启动SD卡 - 根据协议,完成SD卡启动的各个步骤
注册SD卡设备驱动

二、注册总线上的操作函数

int mmc_attach_sd(struct mmc_host *host, u32 ocr)
{
	mmc_attach_bus(host, &mmc_sd_ops);
 	... ...
 
}
void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
{
	host->bus_ops = ops;
	host->bus_refs = 1;
	host->bus_dead = 0;
}
static const struct mmc_bus_ops mmc_sd_ops = {
    .remove = mmc_sd_remove,    // 拔出SD卡的操作函数
    .detect = mmc_sd_detect,    // 探测SD卡的操作函数
    .runtime_suspend = mmc_sd_runtime_suspend,
    .runtime_resume = mmc_sd_runtime_resume,
    .suspend = mmc_sd_suspend,
    .resume = mmc_sd_resume,
    .alive = mmc_sd_alive,
    .shutdown = mmc_sd_suspend,
    .reset = mmc_sd_reset,
};

这里的mmc_sd_detect和mmc_sd_remove就是拔出SD卡所需要用到的函数,下文将详细讨论。这里需要注意的是,插入SD卡的时候,并不执行mmc_sd_detect和mmc_sd_remove这两个函数,但是会注册它们,也就是说,这两个函数的功能已经实现,将来可以使用。

三、设置时钟和总线

mmc_rescan_try_freq
    mmc_power_up
        mmc_set_initial_state
            mmc_set_ios
                host->ops->set_ios(host, ios);//调用到(host/dw_mmc-rockchip.c) .set_ios= dw_mci_rk3288_set_ios,

从这里可以体会到回调函数的精髓:协议层里利用回调函数为所有满足该协议的设备提供统一的接口,而具体实现由底层不同的设备驱动各自完成。注意到,之所以要定义一些放之四海而皆准的公用的类,比如struct mmc_host,就是需要通过struct mmc_host *host指针作为形参传到协议层所提供的接口函数中,从而得以调用。

四、启动SD卡

mmc_sd_init_card主要完成以下任务,
SD卡的启动过程

  1. 得到寄存器CID, CSD, SCR, RCA的数据
  2. 其他操作比如切换到高速模式,初始化card
static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, struct mmc_card *oldcard)
{
	
	/* SD卡的启动过程 */
    mmc_sd_get_cid
	mmc_go_idle(host);    //CMD0
	mmc_send_if_cond(host, ocr);   //CMD8
	mmc_send_app_op_cond(host, ocr, NULL);  //ACMD41
	mmc_all_send_cid(host, cid);		//CMD2
	mmc_send_relative_addr(host, &card->rca); 	//CMD3
	
	/* 得到寄存器CID, CSD, SCR的数据 */
    mmc_sd_get_csd
	mmc_send_csd(card, card->raw_csd);
	mmc_decode_csd(card);
	mmc_decode_cid(card);
	mmc_app_send_scr(card, card->raw_scr);
	mmc_decode_scr(card);
 
	/* 其它操作 */
	mmc_alloc_card(host, &sd_type);
	mmc_select_card(card); 
	mmc_read_switch(card);
	mmc_switch_hs(card);  //切换速率
	... ...
 
}
  1. SD卡的启动过程
    根据SD2.0协议,SD卡的状态可分为两种模式:卡识别模式(card-identification mode)和数据传输模式(data-transfer mode)。这里,我们关注启动SD卡的卡识别模式。
    在这里插入图片描述综合代码:
 mmc_go_idle(host);                     CMD0
 Idle State
 mmc_send_if_cond(host, ocr);     CMD8
 mmc_send_app_op_cond(host, ocr, NULL);       ACMD41
 Ready State
 mmc_all_send_cid(host, cid);       CMD2
 Identification State
 mmc_send_relative_addr(host, &card->rca);     CMD3
 Stand-by State
  1. 寄存器CID, CSD, SCR, RCA
    -> 发送指令并得到寄存器的值
    mmc_sd_get_csd
    mmc_send_csd(card, card->raw_csd);
    mmc_decode_csd(card);
    mmc_decode_cid(card);
    mmc_app_send_scr(card, card->raw_scr);
    mmc_decode_scr(card);

五、注册SD卡设备驱动

 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
{
 
  /* mmc_alloc_card(host, &sd_type); 在mmc_sd_init_card()已完成 */
 
  mmc_add_card(host->card);
 
  ... ...
 
}

上文已经提到,设备驱动程序都会通过alloc_xxx()和add_xxx()两步来注册驱动,其实质是调用/drivers/base/core.c里的device_initialize()和device_add(),device_add()完成建立kobject,sys文件,发送uevent,等工作。

六、拔出SD卡

 void mmc_rescan(struct work_struct *work)
{
 struct mmc_host *host = container_of(work, struct mmc_host, detect.work);
 mmc_bus_get(host);
 
 /* if there is a card registered, check whether it is still present */
 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
  host->bus_ops->detect(host);
 mmc_bus_put(host);
 ... ...
}

这里的mmc_bus_get/put(),为SD总线加上一个自旋锁,规定同时只能有一个线程在SD总线上操作。
1、 bus_ops->detect()
mmc_rescan()扫描SD总线,如果发现host->ops上赋了值,即之前已有SD卡注册过,就执行bus_ops->detect()操作去探测SD总线上是否还存在SD卡,如果不存在了,就执行bus_ops->remove()拔出SD卡。之前已经提到,这个bus_ops->detect()已在mmc_attach_sd()注册完成了。

static void mmc_sd_detect(struct mmc_host *host)
{
 mmc_claim_host(host);
 
 /*
  * Just check if our card has been removed.
  */
 err = mmc_send_status(host->card, NULL);
 
 mmc_release_host(host);
 
 if (err) {
  mmc_sd_remove(host);
 
  mmc_claim_host(host);
  mmc_detach_bus(host);
  mmc_release_host(host);
 }
}

这里的mmc_claim_host(host)通过set_current_state(TASK_RUNNING);将当前进程设置为正在运行进程。
mmc_send_status()发送得到SD卡状态的请求,如果未能得到状态数据,则执行mmc_sd_remove(host)拔出SD卡。

int mmc_send_status(struct mmc_card *card, u32 *status)
{
 struct mmc_command cmd;
 
 cmd.opcode = MMC_SEND_STATUS;    /* #define MMC_SEND_STATUS   13 */
 cmd.arg = card->rca << 16;
 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
 
 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
 
 if (err)               
  return err;           // 接收来自SD卡的response失败,即没有发现SD卡
 if (status)
  *status = cmd.resp[0];
 
 return 0;
 
}

2、bus_ops->remove()
拔出SD卡,其实就是注册SD卡驱动的反操作,实质就是执行device_del()和device_put()

static void mmc_sd_remove(struct mmc_host *host)
{
 mmc_remove_card(host->card);
 host->card = NULL;
}
void mmc_remove_card(struct mmc_card *card)
{
 if (mmc_card_present(card))
  device_del(&card->dev);
 
 put_device(&card->dev);
}

参考:https://blog.csdn.net/zqixiao_09/article/details/51042640

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值