一、一些基础知识
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下SD驱动位于drivers/mmc中,在该目录下有3个子目录:
CARD:因为这些记忆卡都是块设备,当然需要提供块设备的驱动程序,这部分就是实现了将SD卡如何实现为块设备的。
CORE:这是整个MMC的核心层,这部分完成了不同协议和规范的实现,并且为HOST层的驱动提供接口函数。
HOST:针对不同主机的驱动程序,这一部分需要根据自己的特定平台来完成。
在zc702开发板的linux源码的SD驱动配置中,有如下配置: (因此在HOST目录下sdhci.c、sdhci-pltfm.c和sdhci-of-arasan.c这三个文件将被编译进内核。)
目前发现好像不同的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_OFFSET 0x2c
#define CLK_CTRL_TIMEOUT_SHIFT 16
#define CLK_CTRL_TIMEOUT_MASK (0xf << CLK_CTRL_TIMEOUT_SHIFT)
#define CLK_CTRL_TIMEOUT_MIN_EXP 13
/**
* 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");