Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3047046
  • 博文数量: 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 16:29:01

Core层中有两个重要函数 mmc_alloc_host 用于构造host,前面已经学习过,这里不再阐述;另一个就是 mmc_add_host,用于注册host

     前面探测函数s3cmci_probe,现在就来回顾一下这个函数的作用。先简要的概括一下这个函数的功能:

1、s3cmci_probe 最重要的作用是host 的注册,那么首先必须构造出一个host,这个host就是通过s3cmci_alloc_host函数来构造出来的,它是一个struct s3cmci_host类型的结构体。同时,也通过mmc_alloc_host函数构造了一个struct mmc_host的结构体变量mmc。

2、初始化host的时钟,设置host的gpio等等其他一些“乱七八糟”的参数初始化(前面分析过)。

3、通过s3cmci_add_host函数来注册host。


下面是这个函数的详细分析

mmc_add_host [core/host.c]

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  mmc_add_host - initialise host hardware 
  3.  *  @host: mmc host 
  4.  * 
  5.  *  Register the host with the driver model. The host must be 
  6.  *  prepared to start servicing requests before this function 
  7.  *  completes. 
  8.  */  
  9. int mmc_add_host(struct mmc_host *host)  
  10. {  
  11.     int err;  
  12.   
  13.     WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&  
  14.         !host->ops->enable_sdio_irq);  
  15.   
  16.     err = device_add(&host->class_dev);  
  17.     if (err)  
  18.         return err;  
  19.   
  20.     led_trigger_register_simple(dev_name(&host->class_dev), &host->led);  
  21.   
  22. #ifdef CONFIG_DEBUG_FS  
  23.     mmc_add_host_debugfs(host);  
  24. #endif  
  25.     mmc_host_clk_sysfs_init(host);  
  26.   
  27.     mmc_start_host(host);  
  28.     register_pm_notifier(&host->pm_notify);  
  29.   
  30.     return 0;  
  31. }  


     我们看一下mmc_add_host这个函数,它的功能就是通过device_add函数将设备注册进设备模型,最终的结果就是在sys/bus/platform/devices目录下能见到s3c-sdhci.1,s3c-sdhci.2,s3c-sdhci.3设备节点。

     重点是mmc_start_host(host);这也是core层的函数具体的方法如下:

mmc_start_host  [core/core.c]


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void mmc_start_host(struct mmc_host *host)  
  2. {  
  3.     host->f_init = max(freqs[0], host->f_min);  
  4.     host->rescan_disable = 0;  
  5.     if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)  
  6.         mmc_power_off(host);  
  7.     else  
  8.         mmc_power_up(host, host->ocr_avail);  
  9.     _mmc_detect_change(host, 0, false);  
  10. }  


首先来看一下mmc_power_off,内容如下:

[core/core.c]

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void mmc_power_off(struct mmc_host *host)  
  2. {  
  3.     if (host->ios.power_mode == MMC_POWER_OFF)  
  4.         return;  
  5.   
  6.     mmc_host_clk_hold(host);  
  7.   
  8.     host->ios.clock = 0;  
  9.     host->ios.vdd = 0;  
  10.   
  11.     if (!mmc_host_is_spi(host)) {  
  12.         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;  
  13.         host->ios.chip_select = MMC_CS_DONTCARE;  
  14.     }  
  15.     host->ios.power_mode = MMC_POWER_OFF;  
  16.     host->ios.bus_width = MMC_BUS_WIDTH_1;  
  17.     host->ios.timing = MMC_TIMING_LEGACY;  
  18.     mmc_set_ios(host);  
  19.   
  20.     /* 
  21.      * Some configurations, such as the 802.11 SDIO card in the OLPC 
  22.      * XO-1.5, require a short delay after poweroff before the card 
  23.      * can be successfully turned on again. 
  24.      */  
  25.     mmc_delay(1);  
  26.   
  27.     mmc_host_clk_release(host);  
  28. }  


     关心最多的就是host->当中的内容,前段的赋值真正作用在硬件上是调用host层向上提供的struct mmc_host_ops接口。这里18行实际上就是完成了这个工作。

     回到mmc_start_host,mmc_detect_change(host, 0) 看名字就知道是用来检测SD卡的,内容如下:

mmc_detect_change(host, 0)

[core/core.c]

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,  
  2.                 bool cd_irq)  
  3. {  
  4. #ifdef CONFIG_MMC_DEBUG  
  5.     unsigned long flags;  
  6.     spin_lock_irqsave(&host->lock, flags);  
  7.     WARN_ON(host->removed);  
  8.     spin_unlock_irqrestore(&host->lock, flags);  
  9. #endif  
  10.   
  11.     /* 
  12.      * If the device is configured as wakeup, we prevent a new sleep for 
  13.      * 5 s to give provision for user space to consume the event. 
  14.      */  
  15.     if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&  
  16.         device_can_wakeup(mmc_dev(host)))  
  17.         pm_wakeup_event(mmc_dev(host), 5000);  
  18.   
  19.     host->detect_change = 1;  
  20.     mmc_schedule_delayed_work(&host->detect, delay);  
  21. }  


除了20行说了句人话,其他的百分之九十九的都是废话。曾几何时我们说过内核有个延时工作队列,没错就是他了。当然这可不是随便拿来玩的,与之对应的初始化前面已经说过即INIT_DELAYED_WORK(&host->detect, mmc_rescan);好了20 行作用的结果估计大家都能猜到了,就是延时delay 时间后就会去调用mmc_rescan了。前面我们传递的delay=0,那么这里就没有延时了,既然驱动都等不及要rescan了,我们也就不再卖关子了,直接mmc_rescan它的功能就是扫描所插入的卡

这就回到了Linux SD卡驱动开发(三) —— SD 卡驱动分析CORE篇 了


阅读(1379) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~