linux2.6内核SD Card Driver详细解析之一

http://blog.csdn.net/wavemcu/article/details/7366852

***************************************************************************************************************************
作者:EasyWave                                                                                 时间:2012.03.18

类别:linux驱动开发                                                                           声明:转载,请保留链接

***************************************************************************************************************************

一:MMC/SD/SDIO的概念

  1. MMC:(Multi Media Card)由西门子公司和首推CF的SanDisk于1997年推出的多媒体记忆卡标准。
  2. SD:(Secure Digital Memory Card)由日本松下、东芝及美国SanDisk公司于1999年8月共同开发研制的新一代记忆卡标准,已完全兼容MMC标准。
  3. SDIO:(Secure Digital Input and Output Card)安全数字输入输出卡。SDIO是在SD标准上定义了一种外设接口,通过SD的I/O接脚来连接外围设备,并且通过SD上的 I/O数据接位与这些外围设备进行数据传输。是目前较热门的技术,如下图中的一些设备:GPS、相机、Wi-Fi、调频广播、条形码读卡器、蓝牙等。
  4. 工作模式:工作模式是针对主机控制器来说的。SDI控制器可以在符合MMC的标准下工作,或者可以在符合SD的标准下工作,或者可以在符合SDIO的标准下工作。故就分别简称为:MMC模式、SD模式和SDIO模式。
  5. 传输模式:传输模式也是针对主机控制器来说的,指控制器与卡之间数据的传输模式,或者说是总线类型。SDI控制器可支持SPI、1位和4位的三种传输模式(总线类型)。至于1位和4位又是什么意思呢?他们是指传输数据总线的线宽,具体参考数据手册。

下面使用表格列出了MMC、SD、SDIO的电气特性及性能和不同工作模式下支持的传输模式情况:


二:MMC/SD协议

根据协议,MMC/SD卡的驱动被分为:卡识别阶段和数据传输阶段。在卡识别阶段通过命令使MMC/SD处于:空闲(idle)、准备(ready)、识别(ident)、等待(stby)、不活动(ina)几种不同的状态;而在数据传输阶段通过命令使MMC/SD处于:发送(data)、传输(tran)、接收(rcv)、程序(prg)、断开连接(dis)几种不同的状态。所以可以总结MMC/SD在工作的整个过程中分为两个阶段和十种状态。下面使用图形来描述一下在两个阶段中这十种状态之间的转换关系。

卡识别阶段,如下图:


【图是从网络上抓取】

数据传输阶段,如下图:


【图是从网络上抓取】

MMC/SD设备驱动代码在Linux源码中的位置/linux-2.6.35.4/drivers/mmc/,分别有card、core和host三个文件夹,他们都是MMC/SD卡的驱动。在实际驱动开发中,只需要在host文件夹下实现你具体的MMC/SD设备驱动部分代码,也就是控制器(支持对MMC/SD卡的控制,俗称MMC/SD主机控制器)和SDI控制器与MMC/SD卡的硬件接口电路。同时card、core和host这三层的关系,我们用一幅图来进行描述,图如下:


【图是从网络上抓取】

从图中的关系可以看出,整个MMC/SD模块中最重要的部分是Core核心层,他提供了一系列的接口函数,对上提供了将主机驱动注册到系统,给应用程序提供设备访问接口,对下提供了对主机控制器控制的方法及块设备请求的支持。

三: 分析MMC/SD卡设备驱动程序

  1. MMC/SD卡驱动程序的重要数据结构mmc_host,该结构位于Core核心层,主要用于核心层与主机驱动层的数据交换处理。定义在/include/linux/mmc/host.h中:

[plain]  view plain copy print ?
  1. struct mmc_host {  
  2.     struct device       *parent;  
  3.     struct device       class_dev;  
  4.     int         index;  
  5.     const struct mmc_host_ops *ops;  
  6.     unsigned int        f_min;  
  7.     unsigned int        f_max;  
  8.     u32         ocr_avail;  
  9.   
  10. #define MMC_VDD_165_195     0x00000080  /* VDD voltage 1.65 - 1.95 */  
  11. #define MMC_VDD_20_21       0x00000100  /* VDD voltage 2.0 ~ 2.1 */  
  12. #define MMC_VDD_21_22       0x00000200  /* VDD voltage 2.1 ~ 2.2 */  
  13. #define MMC_VDD_22_23       0x00000400  /* VDD voltage 2.2 ~ 2.3 */  
  14. #define MMC_VDD_23_24       0x00000800  /* VDD voltage 2.3 ~ 2.4 */  
  15. #define MMC_VDD_24_25       0x00001000  /* VDD voltage 2.4 ~ 2.5 */  
  16. #define MMC_VDD_25_26       0x00002000  /* VDD voltage 2.5 ~ 2.6 */  
  17. #define MMC_VDD_26_27       0x00004000  /* VDD voltage 2.6 ~ 2.7 */  
  18. #define MMC_VDD_27_28       0x00008000  /* VDD voltage 2.7 ~ 2.8 */  
  19. #define MMC_VDD_28_29       0x00010000  /* VDD voltage 2.8 ~ 2.9 */  
  20. #define MMC_VDD_29_30       0x00020000  /* VDD voltage 2.9 ~ 3.0 */  
  21. #define MMC_VDD_30_31       0x00040000  /* VDD voltage 3.0 ~ 3.1 */  
  22. #define MMC_VDD_31_32       0x00080000  /* VDD voltage 3.1 ~ 3.2 */  
  23. #define MMC_VDD_32_33       0x00100000  /* VDD voltage 3.2 ~ 3.3 */  
  24. #define MMC_VDD_33_34       0x00200000  /* VDD voltage 3.3 ~ 3.4 */  
  25. #define MMC_VDD_34_35       0x00400000  /* VDD voltage 3.4 ~ 3.5 */  
  26. #define MMC_VDD_35_36       0x00800000  /* VDD voltage 3.5 ~ 3.6 */  
  27.   
  28.     unsigned long       caps;       /* Host capabilities */  
  29.   
  30. #define MMC_CAP_4_BIT_DATA  (1 << 0)  /* Can the host do 4 bit transfers */  
  31. #define MMC_CAP_MMC_HIGHSPEED   (1 << 1)  /* Can do MMC high-speed timing */  
  32. #define MMC_CAP_SD_HIGHSPEED    (1 << 2)  /* Can do SD high-speed timing */  
  33. #define MMC_CAP_SDIO_IRQ    (1 << 3)  /* Can signal pending SDIO IRQs */  
  34. #define MMC_CAP_SPI     (1 << 4)  /* Talks only SPI protocols */  
  35. #define MMC_CAP_NEEDS_POLL  (1 << 5)  /* Needs polling for card-detection */  
  36. #define MMC_CAP_8_BIT_DATA  (1 << 6)  /* Can the host do 8 bit transfers */  
  37. #define MMC_CAP_DISABLE     (1 << 7)  /* Can the host be disabled */  
  38. #define MMC_CAP_NONREMOVABLE    (1 << 8)  /* Nonremovable e.g. eMMC */  
  39. #define MMC_CAP_WAIT_WHILE_BUSY (1 << 9)  /* Waits while card is busy */  
  40.   
  41.     mmc_pm_flag_t       pm_caps;    /* supported pm features */  
  42.   
  43.     /* host specific block data */  
  44.     unsigned int        max_seg_size;   /* see blk_queue_max_segment_size */  
  45.     unsigned short      max_hw_segs;    /* see blk_queue_max_hw_segments */  
  46.     unsigned short      max_phys_segs;  /* see blk_queue_max_phys_segments */  
  47.     unsigned short      unused;  
  48.     unsigned int        max_req_size;   /* maximum number of bytes in one req */  
  49.     unsigned int        max_blk_size;   /* maximum size of one mmc block */  
  50.     unsigned int        max_blk_count;  /* maximum number of blocks in one req */  
  51.   
  52.     /* private data */  
  53.     spinlock_t      lock;       /* lock for claim and bus ops */  
  54.   
  55.     struct mmc_ios      ios;        /* current io bus settings */  
  56.     u32         ocr;        /* the current OCR setting */  
  57.   
  58.     /* group bitfields together to minimize padding */  
  59.     unsigned int        use_spi_crc:1;  
  60.     unsigned int        claimed:1;  /* host exclusively claimed */  
  61.     unsigned int        bus_dead:1; /* bus has been released */  
  62. #ifdef CONFIG_MMC_DEBUG  
  63.     unsigned int        removed:1;  /* host is being removed */  
  64. #endif  
  65.   
  66.     /* Only used with MMC_CAP_DISABLE */  
  67.     int         enabled;    /* host is enabled */  
  68.     int         nesting_cnt;    /* "enable" nesting count */  
  69.     int         en_dis_recurs;  /* detect recursion */  
  70.     unsigned int        disable_delay;  /* disable delay in msecs */  
  71.     struct delayed_work disable;    /* disabling work */  
  72.   
  73.     struct mmc_card     *card;      /* device attached to this host */  
  74.   
  75.     wait_queue_head_t   wq;  
  76.     struct task_struct  *claimer;   /* task that has host claimed */  
  77.     int         claim_cnt;  /* "claim" nesting count */  
  78.   
  79.     struct delayed_work detect;  
  80.   
  81.     const struct mmc_bus_ops *bus_ops;  /* current bus driver */  
  82.     unsigned int        bus_refs;   /* reference counter */  
  83.   
  84.     unsigned int        sdio_irqs;  
  85.     struct task_struct  *sdio_irq_thread;  
  86.     atomic_t        sdio_irq_thread_abort;  
  87.   
  88.     mmc_pm_flag_t       pm_flags;   /* requested pm features */  
  89.   
  90. #ifdef CONFIG_LEDS_TRIGGERS  
  91.     struct led_trigger  *led;       /* activity led */  
  92. #endif  
  93.   
  94.     struct dentry       *debugfs_root;  
  95.   
  96.     unsigned long       private[0] ____cacheline_aligned;  
  97. };  
  1. MMC/SD卡驱动程序的重要数据结构mmc_host_ops,主要用于HOST端命令请求,直接跟芯片中SD卡寄存器打交道,定义在/include/linux/mmc/host.h中:
[plain]  view plain copy print ?
  1. struct mmc_host_ops {  
  2.     /*  
  3.      * Hosts that support power saving can use the 'enable' and 'disable'  
  4.      * methods to exit and enter power saving states. 'enable' is called  
  5.      * when the host is claimed and 'disable' is called (or scheduled with  
  6.      * a delay) when the host is released. The 'disable' is scheduled if  
  7.      * the disable delay set by 'mmc_set_disable_delay()' is non-zero,  
  8.      * otherwise 'disable' is called immediately. 'disable' may be  
  9.      * scheduled repeatedly, to permit ever greater power saving at the  
  10.      * expense of ever greater latency to re-enable. Rescheduling is  
  11.      * determined by the return value of the 'disable' method. A positive  
  12.      * value gives the delay in milliseconds.  
  13.      *  
  14.      * In the case where a host function (like set_ios) may be called  
  15.      * with or without the host claimed, enabling and disabling can be  
  16.      * done directly and will nest correctly. Call 'mmc_host_enable()' and  
  17.      * 'mmc_host_lazy_disable()' for this purpose, but note that these  
  18.      * functions must be paired.  
  19.      *  
  20.      * Alternatively, 'mmc_host_enable()' may be paired with  
  21.      * 'mmc_host_disable()' which calls 'disable' immediately.  In this  
  22.      * case the 'disable' method will be called with 'lazy' set to 0.  
  23.      * This is mainly useful for error paths.  
  24.      *  
  25.      * Because lazy disable may be called from a work queue, the 'disable'  
  26.      * method must claim the host when 'lazy' != 0, which will work  
  27.      * correctly because recursion is detected and handled.  
  28.      */  
  29.     int (*enable)(struct mmc_host *host);  
  30.     int (*disable)(struct mmc_host *host, int lazy);  
  31.     void    (*request)(struct mmc_host *host, struct mmc_request *req);  
  32.     /*  
  33.      * Avoid calling these three functions too often or in a "fast path",  
  34.      * since underlaying controller might implement them in an expensive  
  35.      * and/or slow way.  
  36.      *  
  37.      * Also note that these functions might sleep, so don't call them  
  38.      * in the atomic contexts!  
  39.      *  
  40.      * Return values for the get_ro callback should be:  
  41.      *   0 for a read/write card  
  42.      *   1 for a read-only card  
  43.      *   -ENOSYS when not supported (equal to NULL callback)  
  44.      *   or a negative errno value when something bad happened  
  45.      *  
  46.      * Return values for the get_cd callback should be:  
  47.      *   0 for a absent card  
  48.      *   1 for a present card  
  49.      *   -ENOSYS when not supported (equal to NULL callback)  
  50.      *   or a negative errno value when something bad happened  
  51.      */  
  52.     void    (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);  
  53.     int (*get_ro)(struct mmc_host *host);  
  54.     int (*get_cd)(struct mmc_host *host);  
  55.   
  56.     void    (*enable_sdio_irq)(struct mmc_host *host, int enable);  
  57.   
  58.     /* optional callback for HC quirks */  
  59.     void    (*init_card)(struct mmc_host *host, struct mmc_card *card);  
  60. };  
对于mmc_host_ops需要重点讲一下:
[plain]  view plain copy print ?
  1. void    (*request)(struct mmc_host *host, struct mmc_request *req);  
这个函数主要用于SD卡命令的传输,比如发送和接收命令,CMD0,CMD8,ACMD41诸如此类的都是在这个函数去实现。
[plain]  view plain copy print ?
  1. void    (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);  
这个函数主要用于设置SD卡的CLK,MMC_POWER_OFF,MMC_POWER_ON的一些初始化。
[plain]  view plain copy print ?
  1. int (*get_ro)(struct mmc_host *host);  
这个函数主要用于检测SD卡的写保护是否打开。
[plain]  view plain copy print ?
  1. int (*get_cd)(struct mmc_host *host);  
这个函数主要用于SD卡的检测,是否有卡插入和弹出。
  1. MMC/SD卡驱动程序的重要函数mmc_alloc_host,用于分配mmc_host结构体指针的内存空间大小,定义在host.c中:
[plain]  view plain copy print ?
  1. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)  
  2. {  
  3.     int err;  
  4.     struct mmc_host *host;  
  5.   
  6.     if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))  
  7.         return NULL;  
  8.   
  9.     host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);  
  10.     if (!host)  
  11.         return NULL;  
  12.   
  13.     spin_lock(&mmc_host_lock);  
  14.     err = idr_get_new(&mmc_host_idr, host, &host->index);  
  15.     spin_unlock(&mmc_host_lock);  
  16.     if (err)  
  17.         goto free;  
  18.   
  19.     dev_set_name(&host->class_dev, "mmc%d", host->index);  
  20.   
  21.     host->parent = dev;  
  22.     host->class_dev.parent = dev;  
  23.     host->class_dev.class = &mmc_host_class;  
  24.     device_initialize(&host->class_dev);  
  25.   
  26.     spin_lock_init(&host->lock);  
  27.     init_waitqueue_head(&host->wq);  
  28.     INIT_DELAYED_WORK(&host->detect, mmc_rescan);  
  29.     INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);  
  30.   
  31.     /*  
  32.      * By default, hosts do not support SGIO or large requests.  
  33.      * They have to set these according to their abilities.  
  34.      */  
  35.     host->max_hw_segs = 1;  
  36.     host->max_phys_segs = 1;  
  37.     host->max_seg_size = PAGE_CACHE_SIZE;  
  38.   
  39.     host->max_req_size = PAGE_CACHE_SIZE;  
  40.     host->max_blk_size = 512;  
  41.     host->max_blk_count = PAGE_CACHE_SIZE / 512;  
  42.   
  43.     return host;  
  44.   
  45. free:  
  46.     kfree(host);  
  47.     return NULL;  
而在mmc_alloc_host函数中被调用的mmc_rescan函数,这个是需要 重点关注的,因为SD卡协议中的检测,以及卡识别等都是在此函数中实现,具体的代码如下:
[plain]  view plain copy print ?
  1. void mmc_rescan(struct work_struct *work)  
  2. {  
  3.     struct mmc_host *host =  
  4.         container_of(work, struct mmc_host, detect.work);  
  5.     u32 ocr;  
  6.     int err;  
  7.   
  8.     mmc_bus_get(host);  
  9.   
  10.     /* if there is a card registered, check whether it is still present */  
  11.     if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)  
  12.         host->bus_ops->detect(host);  
  13.   
  14.     mmc_bus_put(host);  
  15.   
  16.   
  17.     mmc_bus_get(host);  
  18.   
  19.     /* if there still is a card present, stop here */  
  20.     if (host->bus_ops != NULL) {  
  21.         mmc_bus_put(host);  
  22.         goto out;  
  23.     }  
  24.   
  25.     /* detect a newly inserted card */  
  26.   
  27.     /*  
  28.      * Only we can add a new handler, so it's safe to  
  29.      * release the lock here.  
  30.      */  
  31.     mmc_bus_put(host);  
  32.   
  33.     if (host->ops->get_cd && host->ops->get_cd(host) == 0)  
  34.         goto out;  
  35.   
  36.     mmc_claim_host(host);  
  37.   
  38.     mmc_power_up(host);  
  39.     sdio_reset(host);  
  40.     mmc_go_idle(host); //让SD卡处于IDL_STATUS  
  41.   
  42.     mmc_send_if_cond(host, host->ocr_avail); //检测SD卡是否支持SD2.0  
  43.   
  44.     /*  
  45.      * First we search for SDIO...  
  46.      */  
  47.     err = mmc_send_io_op_cond(host, 0, &ocr); //检测是否是支持SDIO的卡,比如:SDIO WIFI等.  
  48.     if (!err) {  
  49.         if (mmc_attach_sdio(host, ocr))  
  50.             mmc_power_off(host);  
  51.         goto out;  
  52.     }  
  53.   
  54.     /*  
  55.      * ...then normal SD...  
  56.      */  
  57.     err = mmc_send_app_op_cond(host, 0, &ocr); //检测是否是支持标准的SD卡.  
  58.     if (!err) {  
  59.         if (mmc_attach_sd(host, ocr))  
  60.             mmc_power_off(host);  
  61.         goto out;  
  62.     }  
  63.   
  64.     /*  
  65.      * ...and finally MMC.  
  66.      */  
  67.     err = mmc_send_op_cond(host, 0, &ocr); //最后才是检测是否是支持MMC的卡  
  68.     if (!err) {  
  69.         if (mmc_attach_mmc(host, ocr))  
  70.             mmc_power_off(host);  
  71.         goto out;  
  72.     }  
  73.   
  74.     mmc_release_host(host);  
  75.     mmc_power_off(host);  
  76.   
  77. out:  
  78.     if (host->caps & MMC_CAP_NEEDS_POLL)  
  79.         mmc_schedule_delayed_work(&host->detect, HZ);  
  80. }  
  1. MMC/SD卡驱动程序的重要函数mmc_add_host,用于挂载一个mmc_host到内核,定义在host.c中:
[plain]  view plain copy print ?
  1. int mmc_add_host(struct mmc_host *host)  
  2. {  
  3.     int err;  
  4.   
  5.     WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&  
  6.         !host->ops->enable_sdio_irq);  
  7.   
  8.     led_trigger_register_simple(dev_name(&host->class_dev), &host->led);  
  9.   
  10.     err = device_add(&host->class_dev);  
  11.     if (err)  
  12.         return err;  
  13.   
  14. #ifdef CONFIG_DEBUG_FS  
  15.     mmc_add_host_debugfs(host);  
  16. #endif  
  17.   
  18.     mmc_start_host(host);  
  19.   
  20.     return 0;  
  21. }  

可以从SD卡平台驱动看到上面函数的调用情况:
static int __devinit s3cmci_probe(struct platform_device *pdev)
{
    
    struct s3cmci_host *host;
    //实例一个名为mmc的结构体指针,用于与Core核心层中的mmc_host结构体指针相关联
    struct mmc_host    *mmc;
    int ret;
 
    spin_lock_init(&host->complete_lock);
    
    //分配mmc_host结构体指针的内存空间大小,该函数在host.c中实现,这里要注意一点,为什么参数
    //是s3cmci_host结构体的大小,到host.c中看,实际这里分配的是mmc_host加s3cmci_host的大小。
    mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
    if (!mmc) 
    {
        ret = -ENOMEM;
        goto probe_out;
    }

    //调用mmc_priv函数将mmc_host和s3cmci_host结构体的对象关联起来,mmc_priv定义在host.h中
    host = mmc_priv(mmc);
    
    //下面就开始初始化s3cmci_host结构体的各成员
    host->mmc     = mmc;
    host->pdev    = pdev;
    host->pdata   = pdev->dev.platform_data;   

    ..................................
    ..................................
    ..................................
    
    //下面对mmc_host进行初始化
     mmc->ops       = &s3cmci_ops;    //SDI主机控制器操作结构体
    mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;   //设置工作电压范围
    mmc->caps      = MMC_CAP_4_BIT_DATA;              //设置总线宽度为4位
    mmc->f_min     = host->clk_rate / (host->clk_div * 256); //设置最小工作频率
    mmc->f_max     = host->clk_rate / host->clk_div;  //设置最大工作频率
    mmc->max_blk_count  = 4095;
    mmc->max_blk_size   = 4095;
    mmc->max_req_size   = 4095 * 512;
    mmc->max_seg_size   = mmc->max_req_size;
    mmc->max_phys_segs  = 128;
    mmc->max_hw_segs    = 128;

    //将SDI host设备注册到系统中
     ret = mmc_add_host(mmc);
    if (ret) 
    {
        dev_err(&pdev->dev, "failed to add mmc host./n");
        goto free_cpufreq;
    }

    //将SDI host设备的数据赋值给系统平台设备
    platform_set_drvdata(pdev, mmc);

    return 0;
   ..................................
   ..................................
   ..................................

 probe_free_host:
    mmc_free_host(mmc);
 probe_out:
    return ret;
}

未完待续.............................

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值