歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux SD卡驅動開發(三) —— SD 卡驅動分析CORE篇

Linux SD卡驅動開發(三) —— SD 卡驅動分析CORE篇

日期:2017/3/3 11:47:16   编辑:Linux技術
廢話不多說,直接切進主題:
Linux在內核源碼的drivers/mmc/core文件夾下為我們的提供了一系列SD卡的接口服務函數。可以查看Makefile如下

可見,core文件夾下有針對總線的服務bus.c,針對主控制器的服務host.c,針對SD卡的服務sd.c, sd_ops.c等等。
其中,最為核心的一個函數便是之前提到的位於core.c的mmc_rescan,概括來講,主要完成兩項任務,即
掃描SD總線,插入SD卡
掃描SD總線,拔出SD卡
一、 插入SD卡
前面HOST篇最後的中斷篇中講到,插入SD卡,主控制器產生中斷,進入中斷處理函數s3cmci_irq_cd,其中調用的函數 mmc_detect_change,它將最終調用queue_delayed_work執行工作隊列裡的mmc_rescan函數
下面來看看 mmc_rescan
[cpp] view
plain copy





void mmc_rescan(struct work_struct *work)
{
struct mmc_host *host =
container_of(work, struct mmc_host, detect.work);
int i;
if (host->rescan_disable)
return;
/* If there is a non-removable card registered, only scan once */
if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
return;
host->rescan_entered = 1;
mmc_bus_get(host);
/*
* if there is a _removable_ card registered, check whether it is
* still present
*/
if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
&& !(host->caps & MMC_CAP_NONREMOVABLE))
host->bus_ops->detect(host);
host->detect_change = 0;
/*
* Let mmc_bus_put() free the bus/bus_ops if we've found that
* the card is no longer present.
*/
mmc_bus_put(host);
mmc_bus_get(host);
/* if there still is a card present, stop here */
if (host->bus_ops != NULL) {
mmc_bus_put(host);
goto out;
}
/*
* Only we can add a new handler, so it's safe to
* release the lock here.
*/
mmc_bus_put(host);
if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
host->ops->get_cd(host) == 0) {
mmc_claim_host(host);
mmc_power_off(host);
mmc_release_host(host);
goto out;
}
mmc_claim_host(host);
for (i = 0; i < ARRAY_SIZE(freqs); i++) {
if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
break;
if (freqs[i] <= host->f_min)
break;
}
mmc_release_host(host);
out:
if (host->caps & MMC_CAP_NEEDS_POLL)
mmc_schedule_delayed_work(&host->detect, HZ);
}
插入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()的實現過程
[cpp] view
plain copy





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);
... ...
}
[cpp] view
plain copy





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卡設備驅動
二、注冊總線上的操作函數
[cpp] view
plain copy





int mmc_attach_sd(struct mmc_host *host, u32 ocr)
{
mmc_sd_attach_bus_ops(host);
... ...
}
[cpp] view
plain copy





static void mmc_sd_attach_bus_ops(struct mmc_host *host)
{
const struct mmc_bus_ops *bus_ops;
bus_ops = &mmc_sd_ops;
mmc_attach_bus(host, bus_ops);
}
[cpp] view
plain copy





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;
}
[cpp] view
plain copy





static const struct mmc_bus_ops mmc_sd_ops = {
.remove = mmc_sd_remove, // 拔出SD卡的操作函數
.detect = mmc_sd_detect, // 探測SD卡的操作函數
.suspend = NULL,
.resume = NULL,
.power_restore = mmc_sd_power_restore, // 重新啟動SD卡的操作函數
};
這裡的mmc_sd_detect和mmc_sd_remove就是拔出SD卡所需要用到的函數,下文將詳細討論。這裡需要注意的是,插入SD卡的時候,並不執行mmc_sd_detect和mmc_sd_remove這兩個函數,但是會注冊它們,也就是說,這兩個函數的功能已經實現,將來可以使用。
三、設置時鐘和總線
[cpp] view
plain copy





int mmc_attach_sd(struct mmc_host *host, u32 ocr)
{
host->ocr = mmc_select_voltage(host, ocr);
... ...
}
u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
{
mmc_set_ios(host);
... ...
}
static inline void mmc_set_ios(struct mmc_host *host)
{
struct mmc_ios *ios = &host->ios;
host->ops->set_ios(host, ios); // 設置主控制器時鐘和總線的回調函數,具體實現由主控制器驅動完成
}
從這裡可以體會到回調函數的精髓:協議層裡利用回調函數為所有滿足該協議的設備提供統一的接口,而具體實現由底層不同的設備驅動各自完成。注意到,之所以要定義一些放之四海而皆准的公用的類,比如struct mmc_host,就是需要通過struct mmc_host *host指針作為形參傳到協議層所提供的接口函數中,從而得以調用。
四、啟動SD卡
[cpp] view
plain copy





int mmc_attach_sd(struct mmc_host *host, u32 ocr)
{
mmc_sd_init_card(host, host->ocr, NULL);
... ...
}
mmc_sd_init_card主要完成以下任務,
SD卡的啟動過程
得到寄存器CID, CSD, SCR, RCA的數據
其他操作比如切換到高速模式,初始化card
[cpp] view
plain copy





static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, struct mmc_card *oldcard)
{
/* SD卡的啟動過程 */
mmc_go_idle(host);
mmc_send_if_cond(host, ocr);
mmc_send_app_op_cond(host, ocr, NULL);
mmc_all_send_cid(host, cid);
mmc_send_relative_addr(host, &card->rca);
/* 得到寄存器CID, CSD, SCR的數據 */
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卡的卡識別模式。

綜合代碼:
[cpp] view
plain copy





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
2) 寄存器CID, CSD, SCR, RCA
-> 發送指令並得到寄存器的值
當主控制器向SD卡發送cmd指令,比如mmc_send_cid(card, card->raw_cid),請求得到SD卡CID寄存器的值,當主控制器發送cmd完成後,芯片產生一個內部中斷,處理結束cmd的中斷函數,之後得到來自SD卡的response,即CID寄存器的值,存放於host->cmd->resp[i]中。關於內部中斷處理,參看上文的中斷一節裡的 mmc_wait_for_cmd()
mmc_send_cid(card, card->raw_cid);這個函數發送了接收CSD寄存器的請求,並且得到了來自SD卡的CSD寄存器的值。
[cpp] view
plain copy





int mmc_send_cid(struct mmc_card *card, u32 *cid)
{
return mmc_send_cxd_native(card->host, card->rca << 16, cid, MMC_SEND_CID);
}
static int mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
{
cmd.opcode = opcode;
cmd.arg = arg;
cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
memcpy(cxd, cmd.resp, sizeof(u32) * 4); // 得到response賦給cxd,即card->raw_cid
... ...
}
-> 解析寄存器的值
為什麼要解析?先來看看寄存器CID在SD卡協議裡的定義,它是一個128位的寄存器,存放了關於這塊SD卡的基本信息,就像自己的身份證。通過mmc_send_cid()將這個寄存器的數值賦給了card->raw_cid (定義 u32 raw_cid[4];) ,為了方便得到具體某一個信息,協議層為我們解析了寄存器裡的域,並賦給card->cid,比如廠商名稱,就可以通過card->cid.manfid直接讀取到。

[cpp] view
plain copy





static int mmc_decode_cid(struct mmc_card *card)
{
u32 *resp = card->raw_cid;
card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
card->cid.month = UNSTUFF_BITS(resp, 12, 4);
card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
return 0;
}
五、 注冊SD卡設備驅動
[cpp] view
plain copy





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卡
[cpp] view
plain copy





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()注冊完成了。
[cpp] view
plain copy





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卡。
[cpp] view
plain copy





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()
[cpp] view
plain copy





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);
}
Copyright © Linux教程網 All Rights Reserved