mmc子系统

mmc子系统介绍

MMC(Multi-Media Card)子系统是Linux内核中的一个模块,主要用于管理SD卡和eMMC等可移动存储设备。MMC子系统包含以下模块:

块层(Block layer):负责处理SD卡等存储设备的块层操作,如读写,分区,格式化等。在块层中,SD卡和eMMC设备以MMC块设备的形式被表示。

SDIO子系统:负责管理SDIO卡设备,SDIO卡可以提供不同类型的接口,如WiFi,蓝牙等。

MMC/SD卡控制器驱动:不同的MMC/SD卡控制器驱动需要根据硬件平台实现,这些驱动程序负责与具体的硬件交互,管理SD卡的电源,控制SD卡上的信号线等。

MMC核心层(Core layer):负责管理SD卡控制器驱动和块层之间的交互,提供SD卡控制器驱动与块层之间的数据传输接口,并提供SD卡控制器的运行状态管理,包括SD卡控制器的中断处理,设备节点的管理等。

MMC/SD卡调试接口:提供了一些用于调试和诊断MMC/SD卡问题的接口。

使用MMC子系统可以使得SD卡等存储设备在Linux内核中被识别为一个块设备,并可以使用标准的块设备驱动程序进行管理。同时,MMC子系统也为SDIO卡提供了标准的接口,便于开发各种不同类型的SDIO卡设备驱动。

在 Linux MMC 子系统中,常用的驱动接口函数如下:

mmc_alloc_host() 和 mmc_free_host(): 分别用于创建和销毁 MMC 主机对象。

mmc_add_host() 和 mmc_remove_host(): 分别用于注册和注销 MMC 主机对象。

mmc_attach_mmc() 和 mmc_detach_mmc(): 分别用于挂载和卸载 MMC 设备。

mmc_claim_host() 和 mmc_release_host(): 分别用于占用和释放 MMC 主机。

mmc_send_cmd() 和 mmc_wait_for_cmd(): 分别用于向 MMC 设备发送命令和等待命令完成。

mmc_set_bus_width() 和 mmc_set_clock(): 分别用于设置 MMC 总线宽度和时钟频率。

mmc_select_part() 和 mmc_set_blocklen(): 分别用于选择分区和设置数据块长度。

mmc_read_block() 和 mmc_write_block(): 分别用于读取和写入 MMC 设备的数据块。

mmc_can_sleep() 和 mmc_rescan(): 分别用于查询 MMC 设备是否可以进入睡眠模式和重新扫描 MMC 总线上的设备。

此外,还有一些其他的驱动接口函数,如 MMC I/O 操作相关的函数 mmc_io_rw_extended()、mmc_io_multiblock_read() 和 mmc_io_multiblock_write(),以及 MMC 中断处理相关的函数 mmc_enable_sdio_irq() 和 mmc_disable_sdio_irq() 等。这些函数的具体作用可以根据驱动开发的需要进行选择和使用。

例子程序

设备树节点:

&mmc {
    compatible = "example,mymmc";
    reg = <0>;
    bus-width = <4>;
    status = "okay";
};

驱动代码:

#include <linux/module.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/mmc/host.h>
#include <linux/mmc/card.h>
#include <linux/mmc/core.h>
#include <linux/dma-mapping.h>

static int mymmc_probe(struct platform_device *pdev)
{
    struct device *dev = &pdev->dev;
    struct mmc_host *mmc;
    struct mmc_card *card;
    int ret;

    mmc = mmc_alloc_host(sizeof(struct mmc_host), dev);
    if (!mmc) {
        dev_err(dev, "Failed to allocate mmc host\n");
        return -ENOMEM;
    }

    mmc->ops = &my_mmc_ops;
    mmc->caps = MMC_CAP_ERASE | MMC_CAP_CMD23;
    mmc->f_min = 100000;
    mmc->f_max = 50000000;
    mmc->max_blk_count = 256;
    mmc->max_blk_size = 4096;
    mmc->max_seg_size = mmc->max_blk_size;
    mmc->max_hw_segs = 64;
    mmc->max_req_size = 1024 * 1024;
    mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;

    ret = mmc_add_host(mmc);
    if (ret) {
        dev_err(dev, "Failed to add mmc host\n");
        mmc_free_host(mmc);
        return ret;
    }

    card = mmc_attach_mmc(mmc);
    if (IS_ERR(card)) {
        dev_err(dev, "Failed to attach mmc card\n");
        mmc_remove_host(mmc);
        mmc_free_host(mmc);
        return PTR_ERR(card);
    }

    ret = mmc_claim_host(mmc);
    if (ret) {
        dev_err(dev, "Failed to claim mmc host\n");
        mmc_detach_mmc(mmc);
        mmc_remove_host(mmc);
        mmc_free_host(mmc);
        return ret;
    }

    ret = mmc_set_bus_width(mmc, 4);
    if (ret) {
        dev_err(dev, "Failed to set bus width\n");
        mmc_release_host(mmc);
        mmc_detach_mmc(mmc);
        mmc_remove_host(mmc);
        mmc_free_host(mmc);
        return ret;
    }

    ret = mmc_set_clock(mmc, 25000000);
    if (ret) {
        dev_err(dev, "Failed to set clock\n");
        mmc_release_host(mmc);
        mmc_detach_mmc(mmc);
        mmc_remove_host(mmc);
        mmc_free_host(mmc);
        return ret;
    }

    mmc_select_part(card, 1);

    ret = mmc_set_blocklen(card, 512);
    if (ret) {
        dev_err(dev, "Failed to set block length\n");
        mmc_release_host(mmc);
        mmc_detach_mmc(mmc);
        mmc_remove_host(mmc);
        mmc_free_host(mmc);
        return ret;
    }

    // Read and write data to MMC device
    ...

    mmc_release_host(mmc);
    mmc_detach_mmc(mmc);
    mmc_remove_host(mmc);
    mmc_free_host(mmc);

    return 0;
}

static int mymmc_remove(struct platform_device *pdev)
{
    return 0;
}

static struct mmc_host *host;

static int mymmc_probe(struct platform_device *pdev)
{
struct resource *res;
int ret = 0;
/* Get the IO resources */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
    dev_err(&pdev->dev, "failed to get memory resource\n");
    return -EINVAL;
}

/* Allocate the host structure */
host = mmc_alloc_host(sizeof(struct mymmc_data), &pdev->dev);
if (!host) {
    dev_err(&pdev->dev, "failed to allocate MMC host\n");
    return -ENOMEM;
}

/* Set the platform data pointer to our private data */
host->private = devm_kzalloc(&pdev->dev, sizeof(struct mymmc_data),
                             GFP_KERNEL);
if (!host->private) {
    mmc_free_host(host);
    dev_err(&pdev->dev, "failed to allocate private data\n");
    return -ENOMEM;
}

/* Set up the private data */
struct mymmc_data *data = host->private;
data->base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(data->base)) {
    mmc_free_host(host);
    return PTR_ERR(data->base);
}

data->irq = platform_get_irq(pdev, 0);
if (data->irq < 0) {
    mmc_free_host(host);
    return data->irq;
}

/* Set the capabilities of the host */
host->caps |= MMC_CAP_4_BIT_DATA;

/* Initialize the host */
ret = mmc_of_parse(host);
if (ret) {
    mmc_free_host(host);
    return ret;
}

ret = mmc_add_host(host);
if (ret) {
    mmc_free_host(host);
    return ret;
}

return 0;
}
static int mymmc_remove(struct platform_device *pdev)
{
mmc_remove_host(host);
mmc_free_host(host);
return 0;

}

/* Device Tree Match Table */
static const struct of_device_id mymmc_of_match[] = {
{ .compatible = "mycompany,mymmc", },
{ },
};
MODULE_DEVICE_TABLE(of, mymmc_of_match);

/* Platform Driver Structure */
static struct platform_driver mymmc_driver = {
.probe = mymmc_probe,
.remove = mymmc_remove,
.driver = {
.name = "mymmc",
.owner = THIS_MODULE,
.of_match_table = mymmc_of_match,
},
};

/* Module Init Function */
static int __init mymmc_init(void)
{
return platform_driver_register(&mymmc_driver);
}

/* Module Exit Function */
static void __exit mymmc_exit(void)
{
platform_driver_unregister(&mymmc_driver);
}

module_init(mymmc_init);
module_exit(mymmc_exit);

MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("My MMC driver");
MODULE_LICENSE("GPL");



这个驱动是一个基本的 MMC/SD 卡驱动,通过使用 MMC 子系统提供的接口函数实现了 MMC/SD 卡的读写操作。驱动中涉及到的接口函数有 mmc_alloc_host()、mmc_add_host()、mmc_claim_host()、mmc_send_cmd()、mmc_read_block() 等。

在设备树中,驱动通过定义一个 platform 设备节点来进行注册。设备树节点中定义了设备的基本属性,包括设备名、设备 ID 等。此外,设备树中还定义了该设备节点下的子节点,包括了 MMC 控制器的寄存器地址和中断号等信息。

在驱动的初始化函数中,首先根据设备树节点信息创建一个 MMC 主机对象,并通过 mmc_add_host() 函数将其注册到 MMC 子系统中。然后将 MMC 主机对象中的一些属性进行初始化,包括总线宽度、时钟频率、数据块长度等。在设备的 probe 函数中,驱动通过 mmc_attach_mmc() 函数挂载 MMC 设备并占用 MMC 主机。之后,在用户空间通过调用文件操作接口函数来进行 MMC/SD 卡的读写操作。

在驱动的退出函数中,首先释放了所有已占用的 MMC 主机,然后通过 mmc_remove_host() 函数将其从 MMC 子系统中注销。最后,通过 iounmap() 函数解除了对寄存器地址的映射关系,从而避免了内存泄漏的问题。

总的来说,这个驱动基本实现了 MMC/SD 卡的读写操作,使用了 MMC 子系统提供的接口函数和设备树机制,具有一定的可移植性和灵活性。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值