Linux SD卡驱动开发(六) —— SD卡启动过程总体分析

本文深入剖析Linux系统中SD卡驱动的工作流程,从内核启动时的mmc_init开始,讲解mmc、sd总线的注册,以及核心文件的作用。详细介绍了平台总线、mmc总线和sdio总线的注册与操作结构,同时阐述了mmc卡的检测流程和总线操作函数。最后,概述了设备驱动的挂载过程,包括mmc_host的创建、mmc_rescan函数的执行以及mmc块设备驱动的初始化,展示了如何进行SD卡的初始化和数据传输。
摘要由CSDN通过智能技术生成

一、工作流程

mmc驱动主要文件包括

drivers/mmc/card/block.c
drivers/mmc/card/queue.c
drivers/mmc/core/core.c
drivers/mmc/core/host.c
drivers/mmc/core/

内核启动时,首先执行core/core.c的mmc_init,注册mmc、sd总线,以及一个host class设备。接着执行card/block.c中,申请一个块设备

二、数据结构

这里涉及三种总线

[cpp]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. 1. platform bus //MMC host controller 作为一种 platform device, 它是需要注册到 platform bus上 的    
  2. driver/base/platform.c    
  3. struct bus_type platform_bus_type = {    
  4.     .name        = "platform",    
  5.     .dev_attrs    = platform_dev_attrs,    
  6.     .match        = platform_match,    
  7.     .uevent        = platform_uevent,    
  8.     .pm        = &platform_dev_pm_ops,    
  9. };    
  10.     
  11. 2. mmc bus type  //在mmc_init()中被创建的.通过调用 mmc_register_bus() 来注册 MMC 总线    
  12. drivers\mmc\core\bus.c    
  13. static struct bus_type mmc_bus_type = {    
  14.     .name        = "mmc",    
  15.     .dev_attrs    = mmc_dev_attrs,    
  16.     .match        = mmc_bus_match,    
  17.     .uevent        = mmc_bus_uevent,    
  18.     .probe        = mmc_bus_probe,    
  19.     .remove        = mmc_bus_remove,    
  20.     .shutdown        = mmc_bus_shutdown,    
  21.     .pm        = &mmc_bus_pm_ops,    
  22. };    
  23.     
  24. 3. sdio bus type    //在mmc_init()中被创建的.通过调用sdio_register_bus() 来注册 SDIO 总线    
  25. drivers\mmc\core\sdio_bus.c    
  26. static struct bus_type sdio_bus_type = {    
  27.     .name        = "sdio",    
  28.     .dev_attrs    = sdio_dev_attrs,    
  29.     .match        = sdio_bus_match,    
  30.     .uevent        = sdio_bus_uevent,    
  31.     .probe        = sdio_bus_probe,    
  32.     .remove        = sdio_bus_remove,    
  33.     .pm        = SDIO_PM_OPS_PTR,    
  34. };    
     其中mmc总线操作相关函数,由于mmc卡支持多种总数据线,如SPI、SDIO、8LineMMC而不同的总线的操作控制方式不尽相同,所以通过此结构与相应的总线回调函数相关联。
[cpp]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. //总线操作结构  
  2. struct mmc_bus_ops {  
  3.     void (*remove)(struct mmc_host *);  
  4.     void (*detect)(struct mmc_host *);  
  5.     int (*sysfs_add)(struct mmc_host *, struct mmc_card *card);  
  6.     void (*sysfs_remove)(struct mmc_host *, struct mmc_card *card);  
  7.     void (*suspend)(struct mmc_host *);  
  8.     void (*resume)(struct mmc_host *);  
  9. };  
  10. //  mmc卡的总线操作 core/mmc.c  
  11. static const struct mmc_bus_ops mmc_ops = {  
  12.     .remove = mmc_remove,  
  13.     .detect = mmc_detect,  
  14.     .sysfs_add = mmc_sysfs_add,  
  15.     .sysfs_remove = mmc_sysfs_remove,  
  16.     .suspend = mmc_suspend,  
  17.     .resume = mmc_resume,  
  18. };  
  19. // sd卡的总线操作 core/sd.c  
  20. static const struct mmc_bus_ops mmc_sd_ops = {  
  21.     .remove = mmc_sd_remove,  
  22.     .detect = mmc_sd_detect,  
  23.     .sysfs_add = mmc_sd_sysfs_add,  
  24.     .sysfs_remove = mmc_sd_sysfs_remove,  
  25.     .suspend = mmc_sd_suspend,  
  26.     .resume = mmc_sd_resume,  
  27. };  
  28. // sdio的总线操作 core/sdio.c  
  29. static const struct mmc_bus_ops mmc_sdio_ops = {  
  30.     .remove = mmc_sdio_remove,  
  31.     .detect = mmc_sdio_detect,  
  32. };  

关于总线操作的函数:

.detect,驱动程序经常需要调用此函数去检测mmc卡的状态,具体实现是发送CMD13命令,并读回响应,如果响应错误,则依次调用.remove、detach_bus来移除卡及释放总线。


三、总体架构

1、kernel启动时,先后执行mmc_init()及mmc_blk_init(),以对mmc设备及mmc块模块进行初始

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值