sd卡驱动分析 linux,linux下SD卡驅動分析(一)

一、一些基礎知識

1、

SD卡在外形上同Multimedia Card卡保持一致,大小尺寸比MMC卡略厚,容量也大很多。並且兼容MMC卡接口規范。另外,SD卡為9引腳,目的是通過把傳輸方式由串行變成並行,以提高傳輸速度。microsd(以前叫做TF卡),大約不到SD卡的四分之一,8針。可以通過ADAPTER(轉換器)來當作SD卡使用。

2、

SD卡使用的是SD卡協議,而SDIO卡使用的是SDIO協議。協議不一樣,初始化/讀寫方式都不一樣。如:相對於SD協議,SDIO協議特有的命令有cmd5,cmd52,cmd53有的控制器同時支持這兩種卡,因此驅動需要判斷插進來的卡是SD卡還是SDIO卡。上電后先發送CMD5命令,如果有響應,且響應中的MP位為0,則說明是SDIO卡。如果沒有響應說明是SD或者MMC卡。如果有響應,但響應中的MP位為1,則說明這個卡不但是SDIO卡,也是SD卡,也就是所謂的combo卡。

3、

SD和SDIO的引腳定義不一樣,盡管引腳個數都一樣。SD和SDIO卡引腳的最大的一個區別是:SDIO有個中斷引腳DAT[1]在4bit模式下DAT[1]既做數據線,又做中斷線,因此是時分復用的,在總線發送命令期間,DAT[1]用作中斷線。

4、

由於SD1.1規范中規定的SD卡使用的是FAT12和FAT16文件系統,這樣一來符合SD1.1規范的SD卡最大只能達到2G的容量(FAT12的最大磁盤管理能力為8M;最終版的FAT16分區格式最大磁盤管理能力為2G)。SD協會指定了SD2.0規范,符合該規范的SD卡就被稱為SDHC(High Capacity SDMemory Card)。SDHC卡采用的是FAT32的文件系統,這樣一來最大就可以支持到32GB的容量。

5、

SD/SDIO 的傳輸模式

SD 傳輸模式有以下 3 種:

· SPI mode(required)

· 1-bit mode

· 4-bit mode

SDIO 同樣也支持以上3種傳輸模式。依據SD標准,所有的SD(記憶卡)與SDIO(外圍)都必須支持SPI mode,因此SPI mode是「required」。此外,早期的MMC卡(使用SPI傳輸)也能接到SD插糟(SD slot),並且使用SPI mode或1-bit mode來讀取。

二、linux下zynq的SD驅動

linux下SD驅動位於drivers/mmc中,在該目錄下有3個子目錄:

CARD:因為這些記憶卡都是塊設備,當然需要提供塊設備的驅動程序,這部分就是實現了將SD卡如何實現為塊設備的。

CORE:這是整個MMC的核心層,這部分完成了不同協議和規范的實現,並且為HOST層的驅動提供接口函數。

HOST:針對不同主機的驅動程序,這一部分需要根據自己的特定平台來完成。

在zc702開發板的linux源碼的SD驅動配置中,有如下配置: (因此在HOST目錄下sdhci.c、sdhci-pltfm.c和sdhci-of-arasan.c這三個文件將被編譯進內核。)

3dd490fb30ea9df680c16a797c1fbb82.jpe

目前發現好像不同的arm CPU的SD控制器都采用同樣的標准(也就是寄存器地址及內容定義都是一樣的),所以通用部分的驅動代碼在sdhci.c和sdhci-pltfm.c中。而在實際CPU中SD控制器與標准還是有少量不同的部分,這不同部分的驅動代碼將放在單獨的一個文件中,如這里zynq中SD控制器與標准控制器不同的部分代碼就放在sdhci-of-arasan.c文件中(這個文件的代碼非常少)。所以做SD控制器驅動應該還是比較容易的(絕大部分代碼都是現成的)。但是sdhci.c中的代碼非常多,目前還沒分析。

三、sdhci-of-arasan.c代碼

#include

#include "sdhci-pltfm.h"

#define SDHCI_ARASAN_CLK_CTRL_OFFSET0x2c

#define CLK_CTRL_TIMEOUT_SHIFT16

#define CLK_CTRL_TIMEOUT_MASK(0xf << CLK_CTRL_TIMEOUT_SHIFT)

#define CLK_CTRL_TIMEOUT_MIN_EXP13

/**

* struct sdhci_arasan_data

* @clk_ahb:Pointer to the AHB clock

*/

struct sdhci_arasan_data {

struct clk*clk_ahb;

};

static unsigned int sdhci_arasan_get_timeout_clock(struct sdhci_host *host)

{

u32 div;

unsigned long freq;

struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);

div = readl(host->ioaddr + SDHCI_ARASAN_CLK_CTRL_OFFSET);

div = (div & CLK_CTRL_TIMEOUT_MASK) >> CLK_CTRL_TIMEOUT_SHIFT;

freq = clk_get_rate(pltfm_host->clk);

freq /= 1 << (CLK_CTRL_TIMEOUT_MIN_EXP + div);

return freq;

}

//一些操作函數,大部分都是直接調用sdhci.c和sdhci-pltfm.c文件中的通用函數

static struct sdhci_ops sdhci_arasan_ops = {

.set_clock = sdhci_set_clock,

.get_max_clock = sdhci_pltfm_clk_get_max_clock,

.get_timeout_clock = sdhci_arasan_get_timeout_clock,

.set_bus_width = sdhci_set_bus_width,

.reset = sdhci_reset,

.set_uhs_signaling = sdhci_set_uhs_signaling,

};

static struct sdhci_pltfm_data sdhci_arasan_pdata = {

.ops = &sdhci_arasan_ops,

};

#ifdef CONFIG_PM_SLEEP

/**

* sdhci_arasan_suspend - Suspend method for the driver

* @dev:Address of the device structure

* Returns 0 on success and error value on error

*

* Put the device in a low power state.

*/

static int sdhci_arasan_suspend(struct device *dev)

{

struct platform_device *pdev = to_platform_device(dev);

struct sdhci_host *host = platform_get_drvdata(pdev);

struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);

struct sdhci_arasan_data *sdhci_arasan = pltfm_host->priv;

int ret;

ret = sdhci_suspend_host(host);

if (ret)

return ret;

clk_disable(pltfm_host->clk);

clk_disable(sdhci_arasan->clk_ahb);

return 0;

}

/**

* sdhci_arasan_resume - Resume method for the driver

* @dev:Address of the device structure

* Returns 0 on success and error value on error

*

* Resume operation after suspend

*/

static int sdhci_arasan_resume(struct device *dev)

{

struct platform_device *pdev = to_platform_device(dev);

struct sdhci_host *host = platform_get_drvdata(pdev);

struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);

struct sdhci_arasan_data *sdhci_arasan = pltfm_host->priv;

int ret;

ret = clk_enable(sdhci_arasan->clk_ahb);

if (ret) {

dev_err(dev, "Cannot enable AHB clock.\n");

return ret;

}

ret = clk_enable(pltfm_host->clk);

if (ret) {

dev_err(dev, "Cannot enable SD clock.\n");

clk_disable(sdhci_arasan->clk_ahb);

return ret;

}

return sdhci_resume_host(host);

}

#endif /* ! CONFIG_PM_SLEEP */

static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,

sdhci_arasan_resume);

static int sdhci_arasan_probe(struct platform_device *pdev)

{

int ret;

struct clk *clk_xin;

struct sdhci_host *host;

struct sdhci_pltfm_host *pltfm_host;

struct sdhci_arasan_data *sdhci_arasan;

sdhci_arasan = devm_kzalloc(&pdev->dev, sizeof(*sdhci_arasan),

GFP_KERNEL);

if (!sdhci_arasan)

return -ENOMEM;

sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");

if (IS_ERR(sdhci_arasan->clk_ahb)) {

dev_err(&pdev->dev, "clk_ahb clock not found.\n");

return PTR_ERR(sdhci_arasan->clk_ahb);

}

clk_xin = devm_clk_get(&pdev->dev, "clk_xin");

if (IS_ERR(clk_xin)) {

dev_err(&pdev->dev, "clk_xin clock not found.\n");

return PTR_ERR(clk_xin);

}

ret = clk_prepare_enable(sdhci_arasan->clk_ahb);

if (ret) {

dev_err(&pdev->dev, "Unable to enable AHB clock.\n");

return ret;

}

ret = clk_prepare_enable(clk_xin);

if (ret) {

dev_err(&pdev->dev, "Unable to enable SD clock.\n");

goto clk_dis_ahb;

}

//創建一個sdhci_host結構體變量,並初始化賦值

host = sdhci_pltfm_init(pdev, &sdhci_arasan_pdata, 0);

if (IS_ERR(host)) {

ret = PTR_ERR(host);

dev_err(&pdev->dev, "platform init failed (%d)\n", ret);

goto clk_disable_all;

}

sdhci_get_of_property(pdev); //通過這個函數可從設備節點中獲取參數

pltfm_host = sdhci_priv(host);

pltfm_host->priv = sdhci_arasan;

pltfm_host->clk = clk_xin;

ret = sdhci_add_host(host);//讀取SD控制器中相關寄存器的值,設置sdhci_host的參數,並注冊

if (ret) {

dev_err(&pdev->dev, "platform register failed (%d)\n", ret);

goto err_pltfm_free;

}

return 0;

err_pltfm_free:

sdhci_pltfm_free(pdev);

clk_disable_all:

clk_disable_unprepare(clk_xin);

clk_dis_ahb:

clk_disable_unprepare(sdhci_arasan->clk_ahb);

return ret;

}

static int sdhci_arasan_remove(struct platform_device *pdev)

{

struct sdhci_host *host = platform_get_drvdata(pdev);

struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);

struct sdhci_arasan_data *sdhci_arasan = pltfm_host->priv;

clk_disable_unprepare(pltfm_host->clk);

clk_disable_unprepare(sdhci_arasan->clk_ahb);

return sdhci_pltfm_unregister(pdev);

}

static const struct of_device_id sdhci_arasan_of_match[] = {

{ .compatible = "arasan,sdhci-8.9a" },

{ }

};

MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);

static struct platform_driver sdhci_arasan_driver = {

.driver = {

.name = "sdhci-arasan",

.of_match_table = sdhci_arasan_of_match,

.pm = &sdhci_arasan_dev_pm_ops,

},

.probe = sdhci_arasan_probe,

.remove = sdhci_arasan_remove,

};

module_platform_driver(sdhci_arasan_driver);

MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");

MODULE_AUTHOR("Soeren Brinkmann ");

MODULE_LICENSE("GPL");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值