Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3044119
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2017-03-06 15:27:52

回顾一下前面的知识,MMC 子系统范围三个部分:

HOST 部分是针对不同主机的驱动程序,这一部是驱动程序工程师需要根据自己的特点平台来完成的。

CORE 部分: 这是整个MMC 的核心存,这部分完成了不同协议和规范的实现,并为HOST 层的驱动提供了接口函数。

CARD 部分:因为这些记忆卡都是块设备,当然需要提供块设备的驱动程序,这部分就是实现了将你的SD 卡如何实现为块设备的。

它们分布于下面的文件夹中 /drivers/mmc中

     其中,card(区块层) 与core(核心层)是linux系统封装好了部分,我们不需要修改,host(主控制器层)中提供与各芯片构架相关的文件,这才是我们所要开发的部分

     核心层根据需要构造各种MMC/SD命令,这些命令怎么发送给MMC/SD卡呢?这通过主机控制器层来实现。这层是相关的,里面针对各款CPU提供一个文件,目前支持的CPU还很少。

      以本节即将移植的s3cmci.c为例,它首先进行一些低层设置,比如设置MMC/SD/SDIO控制器使用到的CPIO引脚使能控制器注册中断处理函数等,然后向上面的核心层增加一个主机(Host),这样核心层就能调用s3cmci.c提供的函数来识别、使用具体存储卡了。

      在向核心层增加主机之前,s3cmci.c 设置了一个mmc_host_ops结构体,它实现两个函数:

a -- 发起访问请求的request函数

b -- 进行一些属性设置(时钟频率、数据线位宽等)的set_ios函数

      下面列出识别存储卡、区块层发起操作请求两种情况下函数的主要调用关系:

1)识别存储卡


2)区块层发起操作请求


      以后上次对存储卡的操作都通过调用这两个函数来完成。下面对HOST层进行分析(Linux内核版本:Linux-3.14)。


一、struct mmc_host 结构体

        主要用来描述卡控制器位 结构体mmc_host定义于/include/linux/mmc/host.c,可以认为是linux为SD卡控制器专门准备的一个类,该类里面的成员是所有SD卡控制器都需要的,放之四海而皆准的,在本例芯片控制器的驱动程序s3cmci.c中,则为该类具体化了一个对象struct mmc_host *mmc,此mmc指针即指代着该ARM芯片SD卡控制器的一个具体化对象(可以看出虽然C是面向过程的语言,但还是用到了一些面向对象思想的)。

linux/driver/mmc/host/s3cmci.h下定义

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct s3cmci_host {  
  2.     struct platform_device  *pdev;  
  3.     struct s3c24xx_mci_pdata *pdata;  
  4.     struct mmc_host     *mmc;  
  5.     struct resource     *mem;  
  6.     struct clk      *clk;  
  7.     void __iomem        *base;  
  8.     int         irq;  
  9.     int         irq_cd;  
  10.     int         dma;  
  11.   
  12.     unsigned long       clk_rate;  
  13.     unsigned long       clk_div;  
  14.     unsigned long       real_rate;  
  15.     u8          prescaler;  
  16.   
  17.     int         is2440;  
  18.     unsigned        sdiimsk;  
  19.     unsigned        sdidata;  
  20.     int         dodma;  
  21.     int         dmatogo;  
  22.   
  23.     bool            irq_disabled;  
  24.     bool            irq_enabled;  
  25.     bool            irq_state;  
  26.     int         sdio_irqen;  
  27.   
  28.     struct mmc_request  *mrq;  
  29.     int         cmd_is_stop;  
  30.   
  31.     spinlock_t      complete_lock;  
  32.     enum s3cmci_waitfor complete_what;  
  33.   
  34.     int         dma_complete;  
  35.   
  36.     u32         pio_sgptr;  
  37.     u32         pio_bytes;  
  38.     u32         pio_count;  
  39.     u32         *pio_ptr;  
  40. #define XFER_NONE 0  
  41. #define XFER_READ 1  
  42. #define XFER_WRITE 2  
  43.     u32         pio_active;  
  44.   
  45.     int         bus_width;  
  46.   
  47.     char            dbgmsg_cmd[301];  
  48.     char            dbgmsg_dat[301];  
  49.     char            *status;  
  50.   
  51.     unsigned int        ccnt, dcnt;  
  52.     struct tasklet_struct   pio_tasklet;  
  53.   
  54. #ifdef CONFIG_DEBUG_FS  
  55.     struct dentry       *debug_root;  
  56.     struct dentry       *debug_state;  
  57.     struct dentry       *debug_regs;  
  58. #endif  
  59.   
  60. #ifdef CONFIG_CPU_FREQ  
  61.     struct notifier_block   freq_transition;  
  62. #endif  
  63. };  

   其中struct mmc_host(linux/include/linux/mmc/host.h)用于与core层的命令请求,数据 传输等信息,这里代码较长,展示部分

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct mmc_host   
  2. {  
  3.     const struct mmc_host_ops *ops;     // SD卡主控制器的操作函数,即该控制器所具备的驱动能力  
  4.     const struct mmc_bus_ops *bus_ops; // SD总线驱动的操作函数,即SD总线所具备的驱动能力  
  5.     struct mmc_ios  ios;  // 配置时钟、总线、电源、片选、时序等  
  6.     struct mmc_card  *card;  // 连接到此主控制器的SD卡设备  
  7.     ... ...  
  8. };  

      本文中struct mmc_host_ops *ops定义

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static struct mmc_host_ops s3cmci_ops = {  
  2.     .request    = s3cmci_request,  
  3.     .set_ios    = s3cmci_set_ios,  
  4.     .get_ro     = s3cmci_get_ro,  
  5.     .get_cd     = s3cmci_card_present,  
  6.     .enable_sdio_irq = s3cmci_enable_sdio_irq,  
  7. };  


二、SD控制器之初始化(linux/driver/mmc/host)

        这一层讲述硬件与硬件之间将要发生的故事,也是最底层驱动的核心。通常所谓的驱动程序设计的任务将落实到这一层上,所以关注host故事的发展也将成为移植整个SD类设备驱动的核心。在host 目录中有各种平台下SD 卡主机驱动器的实例,这里我们选择s3c2440平台作为分析的重点。参看Kconfig和Makefile即可获得相应信息,这里对应的文件即是s3cmci.c。

1、设备的注册

      旧瓶装新酒,还是那个module_init,不一样的是其中的入口函数。在s3cmci.c中对应的是module_init(s3cmci_init);

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. module_platform_driver(s3cmci_driver);  

 这里是不是很奇怪,不应该是module_init 与 module_exit 吗?module_platform_driver其实是一个宏定义,定义在include/linux/platform_device.h文件中:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* module_platform_driver() - Helper macro for drivers that don't do 
  2.  * anything special in module init/exit.  This eliminates a lot of 
  3.  * boilerplate.  Each module may only use this macro once, and 
  4.  * calling it replaces module_init() and module_exit() 
  5.  */  
  6. #define module_platform_driver(__platform_driver) \  
  7.     module_driver(__platform_driver, platform_driver_register, \  
  8.             platform_driver_unregister)  


宏module_driver定义在include/linux/device.h文件中,其内容如下:


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * module_driver() - Helper macro for drivers that don't do anything 
  3.  * special in module init/exit. This eliminates a lot of boilerplate. 
  4.  * Each module may only use this macro once, and calling it replaces 
  5.  * module_init() and module_exit(). 
  6.  * 
  7.  * @__driver: driver name 
  8.  * @__register: register function for this driver type 
  9.  * @__unregister: unregister function for this driver type 
  10.  * @...: Additional arguments to be passed to __register and __unregister. 
  11.  * 
  12.  * Use this macro to construct bus specific macros for registering 
  13.  * drivers, and do not use it on its own. 
  14.  */  
  15. #define module_driver(__driver, __register, __unregister, ...) \  
  16. static int __init __driver##_init(void) \  
  17. { \  
  18.     return __register(&(__driver) , ##__VA_ARGS__); \  
  19. } \  
  20. module_init(__driver##_init); \  
  21. static void __exit __driver##_exit(void) \  
  22. { \  
  23.     __unregister(&(__driver) , ##__VA_ARGS__); \  
  24. } \  
  25. module_exit(__driver##_exit);  
    可以看出,最终还是调用module_init 与 module_exit。这里注册了一个平台驱动,这个前面分析的串口驱动等是一样的原理。这是新版内核中引入的一个虚拟的平台总线。对应的平台设备早在内核启动时通过platform_add_devices加入到了内核,相关的具体内容前面已经分析的挺多了,这里就不在详细说明。该句调用的结果会导致s3cmci_driver中的probe方法得以调用,由此也就把我们引入了host的世界。



2、probe函数

     在驱动的接口函数中s3cmci_probe() 函数,用于分配mmc_host ,s3cmci_host结构体,并对结构体进行设置在SDI主机控制器操作接口函数mmc_host_ops中会调用s3cmci_host结构体,申请中断并设置中断服务函数,将结构体mmc_host添加到主机。

   SD主控制器驱动程序的初始化函数probe(struct platform_device *pdev),概括地讲,主要完成五大任务,

  • 初始化设备的数据结构,并将数据挂载到pdev->dev.driver_data下
  • 实现设备驱动的功能函数,如mmc->ops = &s3cmci_ops;
  • 申请中断函数request_irq()
  • 注册设备,即注册kobject,建立sys文件,发送uevent等
  • 其他需求,如在/proc/driver下建立用户交互文件等


首先还是来大致看一下s3cmci_driver中所对应的具体内容

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static struct platform_driver s3cmci_driver = {  
  2.     .driver = {  
  3.         .name   = "s3c-sdi",  
  4.         .owner  = THIS_MODULE,  
  5.     },  
  6.     .id_table   = s3cmci_driver_ids,  
  7.     .probe      = s3cmci_probe,  
  8.     .remove     = s3cmci_remove,  
  9.     .shutdown   = s3cmci_shutdown,  
  10. };  
     其中probe是我们关注的核心,由于host与硬件是直接相关的,probe接下来将做部分关于硬件初始化的工作,因此在分析下面一部分代码之前,最好能对s3c2440的sdi相关的内容有所了解。下面进入到probe的相关内容,整个函数洋洋洒洒三百多行,就分段说明吧。


先看这几行

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static int s3cmci_probe(struct platform_device *pdev)  
  2. {  
  3.     struct s3cmci_host *host;  
  4.     struct mmc_host *mmc;  
  5.     int ret;  
  6.     int is2440;  
  7.     int i;  
  8.   
  9.     is2440 = platform_get_device_id(pdev)->driver_data;  
  10.   
  11.     mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);  
  12.     if (!mmc) {  
  13.         ret = -ENOMEM;  
  14.         goto probe_out;  
  15.     }  

       这里,mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev); 分配一个mmc的控制器,同时struct s3cmci_host 结构作为一个私有数据类型将添加到struct mmc_host的private域。

a --  mmc_alloc_host

        mmc_alloc_host相应的代码如下:linux/drivers/mmc/core/host.c

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)  
  2. {  
  3.     int err;  
  4.     struct mmc_host *host;  
  5.   
  6.     host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);  
  7.     if (!host)  
  8.         return NULL;  
  9.   
  10.     /* scanning will be enabled when we're ready */  
  11.     host->rescan_disable = 1;  
  12.     idr_preload(GFP_KERNEL);  
  13.     spin_lock(&mmc_host_lock);  
  14.     err = idr_alloc(&mmc_host_idr, host, 0, 0, GFP_NOWAIT);  
  15.     if (err >= 0)  
  16.         host->index = err;  
  17.     spin_unlock(&mmc_host_lock);  
  18.     idr_preload_end();  
  19.     if (err < 0)  
  20.         goto free;  
  21.   
  22.     dev_set_name(&host->class_dev, "mmc%d", host->index);  
  23.   
  24.     host->parent = dev;  
  25.     host->class_dev.parent = dev;  
  26.     host->class_dev.class = &mmc_host_class;  
  27.     device_initialize(&host->class_dev);  
  28.   
  29.     mmc_host_clk_init(host);  
  30.   
  31.     mutex_init(&host->slot.lock);  
  32.     host->slot.cd_irq = -EINVAL;  
  33.   
  34.     spin_lock_init(&host->lock);  
  35.     init_waitqueue_head(&host->wq);  
  36.     INIT_DELAYED_WORK(&host->detect, mmc_rescan);  
  37. #ifdef CONFIG_PM  
  38.     host->pm_notify.notifier_call = mmc_pm_notify;  
  39. #endif  
  40.   
  41.     /* 
  42.      * By default, hosts do not support SGIO or large requests. 
  43.      * They have to set these according to their abilities. 
  44.      */  
  45.     host->max_segs = 1;  
  46.     host->max_seg_size = PAGE_CACHE_SIZE;  
  47.   
  48.     host->max_req_size = PAGE_CACHE_SIZE;  
  49.     host->max_blk_size = 512;  
  50.     host->max_blk_count = PAGE_CACHE_SIZE / 512;  
  51.   
  52.     return host;  
  53.   
  54. free:  
  55.     kfree(host);  
  56.     return NULL;  
  57. }  
14行是内核的高效搜索树,将host->index与host结构相关联,方便以后查找。
24-27行主要是初始化host->class_dev,这个日后会通过device_register注册进系统。
35行前面已经在这个等待队列上花了不少笔墨,主要是同步对host资源的竞争的。
45-50行这些个都是设置host的一些属性的,是与block.c中请求队列的设置相对应的。

重新回到s3cmci_probe....
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {  
  2.         ret = gpio_request(i, dev_name(&pdev->dev));  
  3.         if (ret) {  
  4.             dev_err(&pdev->dev, "failed to get gpio %d\n", i);  
  5.   
  6.             for (i--; i >= S3C2410_GPE(5); i--)  
  7.                 gpio_free(i);  
  8.   
  9.             goto probe_free_host;  
  10.         }  
  11.     }  

上面这一段申请SD卡驱动器所需的GPIO资源

继续分析...

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host = mmc_priv(mmc);  
  2. host->mmc    = mmc;  
  3. host->pdev   = pdev;  
  4. host->is2440 = is2440;  


    上面这段代码就是对struct s3cmci_host *host这个私有结构 的配置对于core或block层见到的只有struct mmc_host从另外的一个角度可以理解struct mmc_host实际上是struct s3cmci_host的基类,有着所有控制器所必须具有的属性。struct s3cmci_host还包含了与host硬件平台相关的特征
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->pdata = pdev->dev.platform_data;  
  2. if (!host->pdata) {  
  3.     pdev->dev.platform_data = &s3cmci_def_pdata;  
  4.     host->pdata = &s3cmci_def_pdata;  
  5. }  

这是平台设备注册时platform_device所具有的属性.

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. spin_lock_init(&host->complete_lock);  
  2. _init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);  


      tasklet就象一个内核定时器, 在一个"软中断"的上下文中执行(以原子模式),常用在硬件中断处理中,使得可以使得复杂的任务安全地延后到以后的时间处理。task_init 建立一个tasklet,然后调用函数 tasklet_schedule将这个tasklet放在 tasklet_vec链表的头部,并唤醒后台线程 ksoftirqd。当后台线程ksoftirqd 运行调用__do_softirq 时,会执行在中断向量表softirq_vec里中断号TASKLET_SOFTIRQ对应的 tasklet_action函数,然后 tasklet_action遍历 tasklet_vec链表,调用每个 tasklet的函数完成软中断操作,上面例子中即是pio_tasklet函数,另外软中断处理函数只能传递一个long型变量。这里是直接使用host的地址,作为传递参数。关于这个pio_tasklet现在说他还为时过早,等时辰一到自然会对他大书特书。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (is2440) {  
  2.     host->sdiimsk    = S3C2440_SDIIMSK;  
  3.     host->sdidata    = S3C2440_SDIDATA;  
  4.     host->clk_div    = 1;  
  5. else {  
  6.     host->sdiimsk    = S3C2410_SDIIMSK;  
  7.     host->sdidata    = S3C2410_SDIDATA;  
  8.     host->clk_div    = 2;  
  9. }  


host->sdiimsk 、host->sdidata分别用来存放host控制器SDI中断屏蔽寄存器SDI数据寄存器相对SDI 寄存器的偏移地址。对于s3c2440 根据芯片手册SDIIntMsk 偏移地址为0x3C,SDIDAT偏移地址为0x40。最后host->clk_div就是指的SDI使用的时钟分频系数了。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->complete_what  = COMPLETION_NONE;  
  2. t;pio_active    = XFER_NONE;  


host->complete_what 是一个枚举类型变量,实际上用以标示传输完成的状态;host->pio_active标示数据传输的方向,所以在这里一起初始化为空。关于传输完成的标示为了后面分析方便还是一起列举出来:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. enum s3cmci_waitfor {  
  2.     COMPLETION_NONE,  
  3.     COMPLETION_FINALIZE,  
  4.     COMPLETION_CMDSENT,  
  5.     COMPLETION_RSPFIN,  
  6.     COMPLETION_XFERFINISH,  
  7.     COMPLETION_XFERFINISH_RSPFIN,  
  8. };  


继续分析

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifdef CONFIG_MMC_S3C_PIODMA  
  2.     host->dodma      = host->pdata->use_dma;  
  3. #endif  


上面是同时使能了PIO和DMA模式的情况,这里我们对两种传输方式都做相应的分析,所以host->dodma默认为1。
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
  2. if (!host->mem) {  
  3.     dev_err(&pdev->dev,  
  4.         "failed to get io memory region resource.\n");  
  5.   
  6.     ret = -ENOENT;  
  7.     goto probe_free_gpio;  
  8. }  
  9.   
  10. host->mem = request_mem_region(host->mem->start,  
  11.                    resource_size(host->mem), pdev->name);  
  12.   
  13. if (!host->mem) {  
  14.     dev_err(&pdev->dev, "failed to request io memory region.\n");  
  15.     ret = -ENOENT;  
  16.     goto probe_free_gpio;  
  17. }  
  18.   
  19. host->base = ioremap(host->mem->start, resource_size(host->mem));  
  20. if (!host->base) {  
  21.     dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");  
  22.     ret = -EINVAL;  
  23.     goto probe_free_mem_region;  
  24. }  
      上面的一段代码相对比较简单,都是平台驱动设计过程中常用的几个处理函数,就不一一展开了。首先是获取IO 资源, 这当然即使mach-mini2440.c 中所注册的IORESOURCE_MEM,

      这段代码会申请这个资源并检查是否可用,当然只要之前没有使用过SDI寄存器空间,这里都会申请成功。最后就是IO映射,将实地址映射为内核虚拟地址使用。最后,host->base将保持SDI寄存器基地址所对应的内核虚拟地址。


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.        host->irq = platform_get_irq(pdev, 0);  
  2. if (host->irq == 0) {  
  3.     dev_err(&pdev->dev, "failed to get interrupt resource.\n");  
  4.     ret = -EINVAL;  
  5.     goto probe_iounmap;  
  6. }  
  7.   
  8. if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {  
  9.     dev_err(&pdev->dev, "failed to request mci interrupt.\n");  
  10.     ret = -ENOENT;  
  11.     goto probe_iounmap;  
  12. }  


上面一段是对中断资源的申请,并通过request_irq安装了中断处理函数,使能了SDI中断。在上段最为关心的是s3cmci_irq 中断处理函数及其传入的dev_id。关于这个处理函数的分析后面讲述数据传输的时候会进行细致分析。接着向下....

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* We get spurious interrupts even when we have set the IMSK 
  2.      * register to ignore everything, so use disable_irq() to make 
  3.      * ensure we don't lock the system with un-serviceable requests. */  
  4.   
  5.     disable_irq(host->irq);  
  6.     host->irq_state = false;  
前面我们强调了request_irq调用的结果会使能host->irq,但此时系统初始化尚未完成这时候出现的中断可能将处理器带入一个异常状态,所以5行屏蔽中断1650行将中断状态置位无效都是有必要的。
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (!host->pdata->no_detect) {  
  2.         ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");  
  3.         if (ret) {  
  4.             dev_err(&pdev->dev, "failed to get detect gpio\n");  
  5.             goto probe_free_irq;  
  6.         }  


如果SD卡存在的检查一般是通过读取专用引脚状态来实现的,这里如果需要做detect相关的工作的话就必须重新分配一个管脚,在当前系统中前面定义了以GPG8作为检测引脚


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->irq_cd = gpio_to_irq(host->pdata->gpio_detect);  


s3c2410_gpio_getirq是获取这个GPIO的外部中断向量号,可见后面的SD卡的检测可能会用到这个引脚的外部中断。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (host->irq_cd >= 0) {  
  2.             if (request_irq(host->irq_cd, s3cmci_irq_cd,  
  3.                     IRQF_TRIGGER_RISING |  
  4.                     IRQF_TRIGGER_FALLING,  
  5.                     DRIVER_NAME, host)) {  
  6.                 dev_err(&pdev->dev,  
  7.                     "can't get card detect irq.\n");  
  8.                 ret = -ENOENT;  
  9.                 goto probe_free_gpio_cd;  
  10.             }  
  11.         } else {  
  12.             dev_warn(&pdev->dev,  
  13.                  "host detect has no irq available\n");  
  14.             gpio_direction_input(host->pdata->gpio_detect);  
  15.         }  
  16.     } else  
  17.         host->irq_cd = -1;  


上面的这段代码意图很明显就是要申请这个中断了,s3cmci_irq_cd是其处理函数。对像SD卡这种可移出设备来作为块设备存储介质的话,大多会涉及到媒体切换,具体这方面的内容后面用到的时候也会有个详细分析。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (!host->pdata->no_wprotect) {  
  2.         ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");  
  3.         if (ret) {  
  4.             dev_err(&pdev->dev, "failed to get writeprotect\n");  
  5.             goto probe_free_irq_cd;  
  6.         }  
  7.   
  8.         gpio_direction_input(host->pdata->gpio_wprotect);  
  9.     }  
  10.   
  11.     /* depending on the dma state, get a dma channel to use. */  
  12.   
  13.     if (s3cmci_host_usedma(host)) {  
  14.         host->dma = s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client,  
  15.                         host);  
  16.         if (host->dma < 0) {  
  17.             dev_err(&pdev->dev, "cannot get DMA channel.\n");  
  18.             if (!s3cmci_host_canpio()) {  
  19.                 ret = -EBUSY;  
  20.                 goto probe_free_gpio_wp;  
  21.             } else {  
  22.                 dev_warn(&pdev->dev, "falling back to PIO.\n");  
  23.                 host->dodma = 0;  
  24.             }  
  25.         }  
  26.     }  


1行这个我们之前就默认为TURE了,所以接下来的几行代码是免不了。

14 行s3c2410_dma_request 申请DMA 通道,对s3c2440 平台有4 通道的DMA。而DMACH_SDI 是一个虚拟的通道号,由于这部分代码是和硬件紧密相关的,而且整个DMA的管理相对来讲比较复杂,所以这里只是粗略了解一下。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->clk = clk_get(&pdev->dev, "sdi");  
  2.     if (IS_ERR(host->clk)) {  
  3.         dev_err(&pdev->dev, "failed to find clock source.\n");  
  4.         ret = PTR_ERR(host->clk);  
  5.         host->clk = NULL;  
  6.         goto probe_free_dma;  
  7.     }  
  8.   
  9.     ret = clk_enable(host->clk);  
  10.     if (ret) {  
  11.         dev_err(&pdev->dev, "failed to enable clock source.\n");  
  12.         goto clk_free;  
  13.     }  
  14.   
  15.     host->clk_rate = clk_get_rate(host->clk);  


以上是关于sdi时钟和波特率的有关设置,都是内核提供的一些简单的函数调用,相关的内容不是现在研究的重点就不再详细分析了。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.     mmc->ops     = &s3cmci_ops;  
  2.     mmc->ocr_avail   = MMC_VDD_32_33 | MMC_VDD_33_34;  
  3. #ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ  
  4.     mmc->caps    = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;  
  5. #else  
  6.     mmc->caps    = MMC_CAP_4_BIT_DATA;  
  7. #endif  
  8.     mmc->f_min   = host->clk_rate / (host->clk_div * 256);  
  9.     mmc->f_max   = host->clk_rate / host->clk_div;  
  10.   
  11.     if (host->pdata->ocr_avail)  
  12.         mmc->ocr_avail = host->pdata->ocr_avail;  
  13.   
  14.     mmc->max_blk_count   = 4095;  
  15.     mmc->max_blk_size    = 4095;  
  16.     mmc->max_req_size    = 4095 * 512;  
  17.     mmc->max_seg_size    = mmc->max_req_size;  
  18.   
  19.     mmc->max_segs        = 128;  
  20.   
  21.     dbg(host, dbg_debug,  
  22.         "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",  
  23.         (host->is2440?"2440":""),  
  24.         host->base, host->irq, host->irq_cd, host->dma);  


上面就是对这个将要出嫁的mmc_host进行最后的设置mmc->ops = &s3cmci_ops; 就是一直以来向core层提供的接口函数集。后面的分析可能部分是围绕它其中的函数展开的。


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.        ret = s3cmci_cpufreq_register(host);  
  2. if (ret) {  
  3.     dev_err(&pdev->dev, "failed to register cpufreq\n");  
  4.     goto free_dmabuf;  
  5. }  

这里使用的是Linux的通告机制,s3cmci_cpufreq_register相对应的代码如下:

static inline int s3cmci_cpufreq_register(struct s3cmci_host *host) {

host->freq_transition.notifier_call = s3cmci_cpufreq_transition;

return cpufreq_register_notifier(&host->freq_transition,

CPUFREQ_TRANSITION_NOTIFIER);
}

cpufreq_register_notifier cpu是将host->freq_transition注册到CPU频率通告链上,这个是由内核维护的,当cpu频率改变时将会调用上面注册的s3cmci_cpufreq_transition的内容。


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ret = mmc_add_host(mmc);  
  2.     if (ret) {  
  3.         dev_err(&pdev->dev, "failed to add mmc host.\n");  
  4.         goto free_cpufreq;  
  5.     }  


b -- mmc_add_host

     这是core层的函数,他保存了所有平台通用的代码。同时看上去简简单单的一个mmc_add_host,可能蕴藏着天大的玄机。为此我们将为mmc_add_host另分一章,作为core层的续集,专门讲述mmc_add_host过程中发生的点点滴滴....在开始分析mmc_add_host之前,让我们还是结束SD主机控制器的probe函数,接下来

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.         s3cmci_debugfs_attach(host);  
  2.   
  3.     platform_set_drvdata(pdev, mmc);  
  4.     dev_info(&pdev->dev, "%s - using %s, %s SDIO IRQ\n", mmc_hostname(mmc),  
  5.          s3cmci_host_usedma(host) ? "dma" : "pio",  
  6.          mmc->caps & MMC_CAP_SDIO_IRQ ? "hw" : "sw");  
  7.   
  8.     return 0;  
  9.   
  10.  free_cpufreq:  
  11.     s3cmci_cpufreq_deregister(host);  
  12.   
  13.  free_dmabuf:  
  14.     clk_disable(host->clk);  
  15.   
  16.  clk_free:  
  17.     clk_put(host->clk);  
  18.   
  19.  probe_free_dma:  
  20.     if (s3cmci_host_usedma(host))  
  21.         s3c2410_dma_free(host->dma, &s3cmci_dma_client);  
  22.   
  23.  probe_free_gpio_wp:  
  24.     if (!host->pdata->no_wprotect)  
  25.         gpio_free(host->pdata->gpio_wprotect);  
  26.   
  27.  probe_free_gpio_cd:  
  28.     if (!host->pdata->no_detect)  
  29.         gpio_free(host->pdata->gpio_detect);  
  30.   
  31.  probe_free_irq_cd:  
  32.     if (host->irq_cd >= 0)  
  33.         free_irq(host->irq_cd, host);  
  34.   
  35.  probe_free_irq:  
  36.     free_irq(host->irq, host);  
  37.   
  38.  probe_iounmap:  
  39.     iounmap(host->base);  
  40.   
  41.  probe_free_mem_region:  
  42.     release_mem_region(host->mem->start, resource_size(host->mem));  
  43.   
  44.  probe_free_gpio:  
  45.     for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)  
  46.         gpio_free(i);  
  47.   
  48.  probe_free_host:  
  49.     mmc_free_host(mmc);  
  50.   
  51.  probe_out:  
  52.     return ret;  
  53. }  
  54.   
  55. static void s3cmci_shutdown(struct platform_device *pdev)  
  56. {  
  57.     struct mmc_host *mmc = platform_get_drvdata(pdev);  
  58.     struct s3cmci_host *host = mmc_priv(mmc);  
  59.   
  60.     if (host->irq_cd >= 0)  
  61.         free_irq(host->irq_cd, host);  
  62.   
  63.     s3cmci_debugfs_remove(host);  
  64.     s3cmci_cpufreq_deregister(host);  
  65.     mmc_remove_host(mmc);  
  66.     clk_disable(host->clk);  
  67. }  


上面的基本都是出错的处理了,那么也就没有必要纠结于此了,赶快开启新的生活吧....

再多说一句,可以看到probe函数里面,大量的工作是对 host 结构体 mmc结构体的填充,类似于USB probe函数中对 usb_skel 结构体的填充,向上提供接口函数


三、 设备驱动的功能函数

   一般情况下,设备驱动里都有一个行为函数结构体,比如字符设备驱动里的struct file_operations *fops,该结构描述了设备所具备的工作能力,比如open, read, write等,

struct file_operations {
 struct module *owner;
 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
 int (*open) (struct inode *, struct file *);
 ... ...
};

  同理,SD主控制器驱动程序里也有一个类似的结构struct mmc_host_ops *ops,它描述了该控制器所具备驱动的能力。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static struct mmc_host_ops s3cmci_ops = {  
  2.     .request    = s3cmci_request,  
  3.     .set_ios    = s3cmci_set_ios,  
  4.     .get_ro     = s3cmci_get_ro,  
  5.     .get_cd     = s3cmci_card_present,  
  6.     .enable_sdio_irq = s3cmci_enable_sdio_irq,  
  7. };  
     其中,(*set_ios)为主控制器设置总线和时钟等配置(*get_ro)得到只读属性(*enable_sdio_irq)开启sdio中断,本文重点讨论(*request)这个回调函数,它是整个SD主控制器驱动的核心,实现了SD主控制器能与SD卡进行通信的能力。
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)  
  2. {  
  3.     struct s3cmci_host *host = mmc_priv(mmc);  
  4.   
  5.     host->status = "mmc request";  
  6.     host->cmd_is_stop = 0;  
  7.     host->mrq = mrq;  
  8.   
  9.     if (s3cmci_card_present(mmc) == 0) {  
  10.         dbg(host, dbg_err, "%s: no medium present\n", __func__);  
  11.         host->mrq->cmd->error = -ENOMEDIUM;  
  12.         mmc_request_done(mmc, mrq);  
  13.     } else  
  14.         s3cmci_send_request(mmc);  
  15. }  

可以看到这里调用了s3cmci_send_request(mmc) 函数,定义如下:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void s3cmci_send_request(struct mmc_host *mmc)  
  2. {  
  3.     struct s3cmci_host *host = mmc_priv(mmc);  
  4.     struct mmc_request *mrq = host->mrq;  
  5.     struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;  
  6.   
  7.     host->ccnt++;  
  8.     prepare_dbgmsg(host, cmd, host->cmd_is_stop);  
  9.   
  10.     /* Clear command, data and fifo status registers 
  11.        Fifo clear only necessary on 2440, but doesn't hurt on 2410 
  12.     */  
  13.     writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);  
  14.     writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);  
  15.     writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);  
  16.   
  17.     if (cmd->data) {  
  18.         int res = s3cmci_setup_data(host, cmd->data);  
  19.   
  20.         host->dcnt++;  
  21.   
  22.         if (res) {  
  23.             dbg(host, dbg_err, "setup data error %d\n", res);  
  24.             cmd->error = res;  
  25.             cmd->data->error = res;  
  26.   
  27.             mmc_request_done(mmc, mrq);  
  28.             return;  
  29.         }  
  30.   
  31.         if (s3cmci_host_usedma(host))  
  32.             res = s3cmci_prepare_dma(host, cmd->data);  
  33.         else  
  34.             res = s3cmci_prepare_pio(host, cmd->data);  
  35.   
  36.         if (res) {  
  37.             dbg(host, dbg_err, "data prepare error %d\n", res);  
  38.             cmd->error = res;  
  39.             cmd->data->error = res;  
  40.   
  41.             mmc_request_done(mmc, mrq);  
  42.             return;  
  43.         }  
  44.     }  
  45.   
  46.     /* Send command */  
  47.     s3cmci_send_command(host, cmd);  
  48.   
  49.     /* Enable Interrupt */  
  50.     s3cmci_enable_irq(host, true);  
  51. }  


       其中有两个重要的结构体 s3cmci_setup_data(host, cmd->data)实现数据传输 及s3cmci_send_command(host, cmd)实现指令传输,至此,我们需要接触SD主控制器的芯片手册了。

     首先,SD主控制器由一系列32位寄存器组成。通过软件的方式,即对寄存器赋值,来控制SD主控制器,进而扮演SD主控制器的角色与SD卡取得通信。

1) cmdat

    根据主控制器的芯片手册,寄存器MMC_CMDAT控制命令和数据的传输,具体内容如下

结合对寄存器MMC_CMDAT的描述,分析代码

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->cmdat &= ~CMDAT_INIT;               // 非初始化状态  
  2. if (mrq->data) {                                       // 如果存在数据需要传输  
  3.   pxamci_setup_data(host, mrq->data);  // 实现主控制器与SD卡之间数据的传输  
  4.   cmdat &= ~CMDAT_BUSY;                      // 没有忙碌busy信号  
  5.   cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;   // 有数据传输,使用DMA  
  6.   if (mrq->data->flags & MMC_DATA_WRITE)      
  7.    cmdat |= CMDAT_WRITE;                               // 设置为写数据  
  8.   if (mrq->data->flags & MMC_DATA_STREAM)  
  9.    cmdat |= CMDAT_STREAM;                             // 设置为数据流stream模式  
  10. }  



2) s3cmci_setup_data  

    通过DMA实现主控制器与SD卡之间数据的传输

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)  
  2. {  
  3.     u32 dcon, imsk, stoptries = 3;  
  4.   
  5.     /* write DCON register */  
  6.   
  7.     if (!data) {  
  8.         writel(0, host->base + S3C2410_SDIDCON);  
  9.         return 0;  
  10.     }  
  11.   
  12.     if ((data->blksz & 3) != 0) {  
  13.         /* We cannot deal with unaligned blocks with more than 
  14.          * one block being transferred. */  
  15.   
  16.         if (data->blocks > 1) {  
  17.             pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);  
  18.             return -EINVAL;  
  19.         }  
  20.     }  
  21.   
  22.     while (readl(host->base + S3C2410_SDIDSTA) &  
  23.            (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {  
  24.   
  25.         dbg(host, dbg_err,  
  26.             "mci_setup_data() transfer stillin progress.\n");  
  27.   
  28.         writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);  
  29.         s3cmci_reset(host);  
  30.   
  31.         if ((stoptries--) == 0) {  
  32.             dbg_dumpregs(host, "DRF");  
  33.             return -EINVAL;  
  34.         }  
  35.     }  
  36.   
  37.     dcon  = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;  
  38.   
  39.     if (s3cmci_host_usedma(host))  
  40.         dcon |= S3C2410_SDIDCON_DMAEN;  
  41.   
  42.     if (host->bus_width == MMC_BUS_WIDTH_4)  
  43.         dcon |= S3C2410_SDIDCON_WIDEBUS;  
  44.   
  45.     if (!(data->flags & MMC_DATA_STREAM))  
  46.         dcon |= S3C2410_SDIDCON_BLOCKMODE;  
  47.   
  48.     if (data->flags & MMC_DATA_WRITE) {  
  49.         dcon |= S3C2410_SDIDCON_TXAFTERRESP;  
  50.         dcon |= S3C2410_SDIDCON_XFER_TXSTART;  
  51.     }  
  52.   
  53.     if (data->flags & MMC_DATA_READ) {  
  54.         dcon |= S3C2410_SDIDCON_RXAFTERCMD;  
  55.         dcon |= S3C2410_SDIDCON_XFER_RXSTART;  
  56.     }  
  57.   
  58.     if (host->is2440) {  
  59.         dcon |= S3C2440_SDIDCON_DS_WORD;  
  60.         dcon |= S3C2440_SDIDCON_DATSTART;  
  61.     }  
  62.   
  63.     writel(dcon, host->base + S3C2410_SDIDCON);  
  64.   
  65.     /* write BSIZE register */  
  66.   
  67.     writel(data->blksz, host->base + S3C2410_SDIBSIZE);  
  68.   
  69.     /* add to IMASK register */  
  70.     imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |  
  71.            S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;  
  72.   
  73.     enable_imask(host, imsk);  
  74.   
  75.     /* write TIMER register */  
  76.   
  77.     if (host->is2440) {  
  78.         writel(0x007FFFFF, host->base + S3C2410_SDITIMER);  
  79.     } else {  
  80.         writel(0x0000FFFF, host->base + S3C2410_SDITIMER);  
  81.   
  82.         /* FIX: set slow clock to prevent timeouts on read */  
  83.         if (data->flags & MMC_DATA_READ)  
  84.             writel(0xFF, host->base + S3C2410_SDIPRE);  
  85.     }  
  86.   
  87.     return 0;  
  88. }  
 for()循环里的内容是整个SD卡主控制器设备驱动的实质,通过DMA的方式实现主控制器与SD卡之间数据的读写操作。


3) m3cmci_start_cmd  

     实现主控制器与SD卡之间指令的传输

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void s3cmci_send_command(struct s3cmci_host *host,  
  2.                     struct mmc_command *cmd)  
  3. {  
  4.     u32 ccon, imsk;  
  5.   
  6.     imsk  = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |  
  7.         S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |  
  8.         S3C2410_SDIIMSK_RESPONSECRC;  
  9.   
  10.     enable_imask(host, imsk);  
  11.   
  12.     if (cmd->data)  
  13.         host->complete_what = COMPLETION_XFERFINISH_RSPFIN;  
  14.     else if (cmd->flags & MMC_RSP_PRESENT)  
  15.         host->complete_what = COMPLETION_RSPFIN;  
  16.     else  
  17.         host->complete_what = COMPLETION_CMDSENT;  
  18.   
  19.     writel(cmd->arg, host->base + S3C2410_SDICMDARG);  
  20.   
  21.     ccon  = cmd->opcode & S3C2410_SDICMDCON_INDEX;  
  22.     ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;  
  23.   
  24.     if (cmd->flags & MMC_RSP_PRESENT)  
  25.         ccon |= S3C2410_SDICMDCON_WAITRSP;  
  26.   
  27.     if (cmd->flags & MMC_RSP_136)  
  28.         ccon |= S3C2410_SDICMDCON_LONGRSP;  
  29.   
  30.     writel(ccon, host->base + S3C2410_SDICMDCON);  
  31. }  


> response类型

  根据SD卡的协议,当SD卡收到从控制器发来的cmd指令后,SD卡会发出response相应,而response的类型分为R1,R1b,R2,R3,R6,R7,这些类型分别对应不同的指令,各自的数据包结构也不同(具体内容参考SD卡协议)。这里,通过RSP_TYPE对指令cmd的opcode的解析得到相对应的reponse类型,再通过swich赋给寄存器MMC_CMDAT对应的[1:0]位。

-> 将指令和参数写入寄存器

  4行writel()是整个SD卡主控制器设备驱动的实质,通过对主控制器芯片寄存器MMC_CMD,MMC_ARGH,MMC_ARGL,MMC_CMDAT的设置,实现主控制器发送指令到SD卡的功能。


四、申请中断

        这里对前面的中断函数进行详细描述:

        s3cmci_probe(struct platform_device *pdev)中有两个中断,一个为SD主控制器芯片内电路固有的内部中断另一个为探测引脚的探测到外部有SD卡插拔引起的中断

1) 由主控芯片内部电路引起的中断

    (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)

回顾一下,host->irq就是刚才从platform_device里得到的中断号,

  irq = platform_get_irq(pdev, 0);

  host->irq = irq;

接下来,s3cmci_irq便是为该中断host->irq申请的中断函数

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * ISR for SDI Interface IRQ 
  3.  * Communication between driver and ISR works as follows: 
  4.  *   host->mrq           points to current request 
  5.  *   host->complete_what Indicates when the request is considered done 
  6.  *     COMPLETION_CMDSENT     when the command was sent 
  7.  *     COMPLETION_RSPFIN          when a response was received 
  8.  *     COMPLETION_XFERFINISH      when the data transfer is finished 
  9.  *     COMPLETION_XFERFINISH_RSPFIN both of the above. 
  10.  *   host->complete_request  is the completion-object the driver waits for 
  11.  * 
  12.  * 1) Driver sets up host->mrq and host->complete_what 
  13.  * 2) Driver prepares the transfer 
  14.  * 3) Driver enables interrupts 
  15.  * 4) Driver starts transfer 
  16.  * 5) Driver waits for host->complete_rquest 
  17.  * 6) ISR checks for request status (errors and success) 
  18.  * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error 
  19.  * 7) ISR completes host->complete_request 
  20.  * 8) ISR disables interrupts 
  21.  * 9) Driver wakes up and takes care of the request 
  22.  * 
  23.  * Note: "->error"-fields are expected to be set to 0 before the request 
  24.  *       was issued by mmc.c - therefore they are only set, when an error 
  25.  *       contition comes up 
  26.  */  
  27.   
  28. static irqreturn_t s3cmci_irq(int irq, void *dev_id)  
  29. {  
  30.     struct s3cmci_host *host = dev_id;  
  31.     struct mmc_command *cmd;  
  32.     u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;  
  33.     u32 mci_cclear = 0, mci_dclear;  
  34.     unsigned long iflags;  
  35.   
  36.     mci_dsta = readl(host->base + S3C2410_SDIDSTA);  
  37.     mci_imsk = readl(host->base + host->sdiimsk);  
  38.   
  39.     if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {  
  40.         if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {  
  41.             mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;  
  42.             writel(mci_dclear, host->base + S3C2410_SDIDSTA);  
  43.   
  44.             mmc_signal_sdio_irq(host->mmc);  
  45.             return IRQ_HANDLED;  
  46.         }  
  47.     }  
  48.   
  49.     spin_lock_irqsave(&host->complete_lock, iflags);  
  50.   
  51.     mci_csta = readl(host->base + S3C2410_SDICMDSTAT);  
  52.     mci_dcnt = readl(host->base + S3C2410_SDIDCNT);  
  53.     mci_fsta = readl(host->base + S3C2410_SDIFSTA);  
  54.     mci_dclear = 0;  
  55.   
  56.     if ((host->complete_what == COMPLETION_NONE) ||  
  57.         (host->complete_what == COMPLETION_FINALIZE)) {  
  58.         host->status = "nothing to complete";  
  59.         clear_imask(host);  
  60.         goto irq_out;  
  61.     }  
  62.   
  63.     if (!host->mrq) {  
  64.         host->status = "no active mrq";  
  65.         clear_imask(host);  
  66.         goto irq_out;  
  67.     }  
  68.   
  69.     cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;  
  70.   
  71.     if (!cmd) {  
  72.         host->status = "no active cmd";  
  73.         clear_imask(host);  
  74.         goto irq_out;  
  75.     }  
  76.   
  77.     if (!s3cmci_host_usedma(host)) {  
  78.         if ((host->pio_active == XFER_WRITE) &&  
  79.             (mci_fsta & S3C2410_SDIFSTA_TFDET)) {  
  80.   
  81.             disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);  
  82.             tasklet_schedule(&host->pio_tasklet);  
  83.             host->status = "pio tx";  
  84.         }  
  85.   
  86.         if ((host->pio_active == XFER_READ) &&  
  87.             (mci_fsta & S3C2410_SDIFSTA_RFDET)) {  
  88.   
  89.             disable_imask(host,  
  90.                       S3C2410_SDIIMSK_RXFIFOHALF |  
  91.                       S3C2410_SDIIMSK_RXFIFOLAST);  
  92.   
  93.             tasklet_schedule(&host->pio_tasklet);  
  94.             host->status = "pio rx";  
  95.         }  
  96.     }  
  97.   
  98.     if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {  
  99.         dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");  
  100.         cmd->error = -ETIMEDOUT;  
  101.         host->status = "error: command timeout";  
  102.         goto fail_transfer;  
  103.     }  
  104.   
  105.     if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {  
  106.         if (host->complete_what == COMPLETION_CMDSENT) {  
  107.             host->status = "ok: command sent";  
  108.             goto close_transfer;  
  109.         }  
  110.   
  111.         mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;  
  112.     }  
  113.   
  114.     if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {  
  115.         if (cmd->flags & MMC_RSP_CRC) {  
  116.             if (host->mrq->cmd->flags & MMC_RSP_136) {  
  117.                 dbg(host, dbg_irq,  
  118.                     "fixup: ignore CRC fail with long rsp\n");  
  119.             } else {  
  120.                 /* note, we used to fail the transfer 
  121.                  * here, but it seems that this is just 
  122.                  * the hardware getting it wrong. 
  123.                  * 
  124.                  * cmd->error = -EILSEQ; 
  125.                  * host->status = "error: bad command crc"; 
  126.                  * goto fail_transfer; 
  127.                 */  
  128.             }  
  129.         }  
  130.   
  131.         mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;  
  132.     }  
  133.   
  134.     if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {  
  135.         if (host->complete_what == COMPLETION_RSPFIN) {  
  136.             host->status = "ok: command response received";  
  137.             goto close_transfer;  
  138.         }  
  139.   
  140.         if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)  
  141.             host->complete_what = COMPLETION_XFERFINISH;  
  142.   
  143.         mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;  
  144.     }  
  145.   
  146.     /* errors handled after this point are only relevant 
  147.        when a data transfer is in progress */  
  148.   
  149.     if (!cmd->data)  
  150.         goto clear_status_bits;  
  151.   
  152.     /* Check for FIFO failure */  
  153.     if (host->is2440) {  
  154.         if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {  
  155.             dbg(host, dbg_err, "FIFO failure\n");  
  156.             host->mrq->data->error = -EILSEQ;  
  157.             host->status = "error: 2440 fifo failure";  
  158.             goto fail_transfer;  
  159.         }  
  160.     } else {  
  161.         if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {  
  162.             dbg(host, dbg_err, "FIFO failure\n");  
  163.             cmd->data->error = -EILSEQ;  
  164.             host->status = "error:  fifo failure";  
  165.             goto fail_transfer;  
  166.         }  
  167.     }  
  168.   
  169.     if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {  
  170.         dbg(host, dbg_err, "bad data crc (outgoing)\n");  
  171.         cmd->data->error = -EILSEQ;  
  172.         host->status = "error: bad data crc (outgoing)";  
  173.         goto fail_transfer;  
  174.     }  
  175.   
  176.     if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {  
  177.         dbg(host, dbg_err, "bad data crc (incoming)\n");  
  178.         cmd->data->error = -EILSEQ;  
  179.         host->status = "error: bad data crc (incoming)";  
  180.         goto fail_transfer;  
  181.     }  
  182.   
  183.     if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {  
  184.         dbg(host, dbg_err, "data timeout\n");  
  185.         cmd->data->error = -ETIMEDOUT;  
  186.         host->status = "error: data timeout";  
  187.         goto fail_transfer;  
  188.     }  
  189.   
  190.     if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {  
  191.         if (host->complete_what == COMPLETION_XFERFINISH) {  
  192.             host->status = "ok: data transfer completed";  
  193.             goto close_transfer;  
  194.         }  
  195.   
  196.         if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)  
  197.             host->complete_what = COMPLETION_RSPFIN;  
  198.   
  199.         mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;  
  200.     }  
  201.   
  202. clear_status_bits:  
  203.     writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);  
  204.     writel(mci_dclear, host->base + S3C2410_SDIDSTA);  
  205.   
  206.     goto irq_out;  
  207.   
  208. fail_transfer:  
  209.     host->pio_active = XFER_NONE;  
  210.   
  211. close_transfer:  
  212.     host->complete_what = COMPLETION_FINALIZE;  
  213.   
  214.     clear_imask(host);  
  215.     tasklet_schedule(&host->pio_tasklet);  
  216.   
  217.     goto irq_out;  
  218.   
  219. irq_out:  
  220.     dbg(host, dbg_irq,  
  221.         "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",  
  222.         mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);  
  223.   
  224.     spin_unlock_irqrestore(&host->complete_lock, iflags);  
  225.     return IRQ_HANDLED;  
  226.   
  227. }  


    当调用(*request),即host->ops->request(host, mrq),即上文中的s3cmci_request()后,控制器与SD卡之间开始进行一次指令或数据传输,通信完毕后,主控芯片将产生一个内部中断,以告知此次指令或数据传输完毕。这个中断的具体值将保存在一个名为MMC_I_REG的中断寄存器中以供读取,中断寄存器MMC_I_REG中相关描述如下


如果中断寄存器MMC_I_REG中的第0位有值,则意味着数据传输完成,执行s3cmci_cmd_done(host, stat);

如果中断寄存器MMC_I_REG中的第2位有值,则意味着指令传输完成,执行s3cmci_data_done(host, stat);

其中stat是从状态寄存器MMC_STAT中读取的值,在代码里主要起到处理错误状态的作用。

-> s3cmci_cmd_done 收到结束指令的内部中断信号,主控制器从SD卡那里得到response,结束这次指令传输

这里需要注意,寄存器MMC_RES里已经存放了来自SD卡发送过来的response,以供读取。


2、探测引脚引起的中断


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. host->irq_cd = gpio_to_irq(host->pdata->gpio_detect);  
  2.   
  3. if (host->irq_cd >= 0) {  
  4.     if (request_irq(host->irq_cd, s3cmci_irq_cd,  
  5.             IRQF_TRIGGER_RISING |  
  6.             IRQF_TRIGGER_FALLING,  
  7.             DRIVER_NAME, host)) {  
  8.         dev_err(&pdev->dev,  
  9.             "can't get card detect irq.\n");  
  10.         ret = -ENOENT;  
  11.         goto probe_free_gpio_cd;  
  12.     }  
  13. else {  
  14.     dev_warn(&pdev->dev,  
  15.          "host detect has no irq available\n");  
  16.     gpio_direction_input(host->pdata->gpio_detect);  
  17. }  


     其中,irq_cd是通过GPIO转换得到的中断号,s3cmci_irq_cd便是该中断实现的函数

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)  
  2. {  
  3.     struct s3cmci_host *host = (struct s3cmci_host *)dev_id;  
  4.   
  5.     dbg(host, dbg_irq, "card detect\n");  
  6.   
  7.     mmc_detect_change(host->mmc, msecs_to_jiffies(500));  
  8.   
  9.     return IRQ_HANDLED;  
  10. }  


     之前已经提到过mmc_detect_change,它将最终调用queue_delayed_work执行工作队列里的mmc_rescan函数

    当有SD卡插入或拔出时,硬件主控制器芯片的探测pin脚产生外部中断,进入中断处理函数,执行工作队列里的mmc_rescan,扫描SD总线,对插入或拔出SD卡作相应的处理。下文协议层将讨论mmc_rescan()。
阅读(1429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~