嵌入式 分析platform_get_resource

有如下实例:

static struct resource net_resources[] = {

[0] = {

        .start       = ETHERNET_BASE,

        .end = ETHERNET_BASE + SZ_4K - 1,

        .flags      = IORESOURCE_MEM,

},

 

[1] = {

        .start       = IRQ_ETHERNET,

        .end = IRQ_ETHERNET,

        .flags      = IORESOURCE_IRQ,

},

};

当获取平台资源时

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

irq = platform_get_irq(pdev, 0);

源码:

/**

 * platform_get_resource - get a resource for a device

 * @dev: platform device

 * @type: resource type

 * @num: resource index

 */

struct resource *platform_get_resource(struct platform_device *dev,

                                   unsigned int type, unsigned int num)

{

       int i;

 

       for (i = 0; i < dev->num_resources; i++) {

              struct resource *r = &dev->resource[i];

 

              if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|

                             IORESOURCE_IRQ|IORESOURCE_DMA)) == type)

                     if (num-- == 0)

                            return r;

       }

       return NULL;

}

根据实例和源码可以得出结论:num代表的不是resource数组的数组号,而是具有相同资源类型的序号(从0开始的序号)。在实例中,因为IORESOURCE_IRQ类型的资源只有一个,故platform_get_irq()函数的第二个参数是0,而不是resource数组的序号1

       在platform机制下,板级文件BSP一般定义了设备的资源,在系统初始化的过程便对platform_device进行注册,接着注册platform_driver,两者都注册完毕之后哦,platform_driver便去probe,在probe的过程中,会使用platform_get_resource函数来获取设备资源,那么它究竟是怎么获取资源的,platform_get_resource函数的每一个参数都代表什么意思呢?

这边我用的是HTC—legend的内核,使用mmc的板级资源。资源在arch/arm/msm/下。

先看platform_devices

1.在devices.c中,可以看到pdev的资源定义为一个数组

int __init msm_add_sdcc(unsigned int controller, struct mmc_platform_data *plat,

                            unsigned int stat_irq, unsigned long stat_irq_flags)

{

         struct platform_device       *pdev;

         struct resource *res;

 

         if (controller < 1 || controller > 4)

                   return -EINVAL;

 

         pdev = msm_sdcc_devices[controller-1];//此为dev赋值

         pdev->dev.platform_data = plat;

 

         res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "status_irq");

         if (!res)

                   return -EINVAL;

         else if (stat_irq) {

                   res->start = res->end = stat_irq;

                   res->flags &= ~IORESOURCE_DISABLED;

                   res->flags |= stat_irq_flags;

         }

 

#ifdef CONFIG_MMC_SUPPORT_EXTERNEL_DRIVER

         if (plat->use_ext_sdiodrv)

                   pdev->name = plat->ext_sdiodrv_name;

#endif

 

         return platform_device_register(pdev);

}

2.pdev = msm_sdcc_devices[controller-1];//此为dev赋值

所以我们找到这个数组

static struct platform_device *msm_sdcc_devices[] __initdata = {

         &msm_device_sdc1,//其实每一个元素都是一个一个结构体,这边指向每一个结构体的地址

         &msm_device_sdc2,

         &msm_device_sdc3,

         &msm_device_sdc4,

};

       可以看到数组大小为4,元素的类型为struct platform_device,每一个元素都是指向一个struct platform_device 变量的地址,那我们接着看每一个元素的定义。

3.数组元素的定义

struct platform_device msm_device_sdc1 = {

         .name                = "msm_sdcc",

         .id              = 1,

         .num_resources       = ARRAY_SIZE(resources_sdc1),

         .resource = resources_sdc1,

         .dev           = {

                   .coherent_dma_mask      = 0xffffffff,

         },

};

 

struct platform_device msm_device_sdc2 = {

         .name                = "msm_sdcc",

         .id              = 2,

         .num_resources       = ARRAY_SIZE(resources_sdc2),

         .resource = resources_sdc2,

         .dev           = {

                   .coherent_dma_mask      = 0xffffffff,

         },

};

 

struct platform_device msm_device_sdc3 = {

         .name                = "msm_sdcc",

         .id              = 3,

         .num_resources       = ARRAY_SIZE(resources_sdc3),

         .resource = resources_sdc3,

         .dev           = {

                   .coherent_dma_mask      = 0xffffffff,

         },

};

 

struct platform_device msm_device_sdc4 = {

         .name                = "msm_sdcc",

         .id              = 4,

         .num_resources       = ARRAY_SIZE(resources_sdc4),

         .resource = resources_sdc4,

         .dev           = {

                   .coherent_dma_mask      = 0xffffffff,

         },

};

       这4个结构体都定义了:name资源名,id编号, num_resources,资源数量,resource资源。

4.所以我们必须再看看resource所指向的值。这边就不全部列出了,就以.resource  = resources_sdc1,指向的resources_sdc4,我们看下。

static struct resource resources_sdc1[] = {

         {

                   .start          = MSM_SDC1_PHYS,

                   .end = MSM_SDC1_PHYS + MSM_SDC1_SIZE - 1,

                   .flags         = IORESOURCE_MEM,

         },

         {

                   .start          = INT_SDC1_0,

                   .end = INT_SDC1_0,

                   .flags         = IORESOURCE_IRQ,

                   .name       = "cmd_irq",

         },

         {

                   .start          = INT_SDC1_1,

                   .end = INT_SDC1_1,

                   .flags         = IORESOURCE_IRQ,

                   .name       = "pio_irq",

         },

         {

                   .flags         = IORESOURCE_IRQ | IORESOURCE_DISABLED,

                   .name       = "status_irq"

         },

         {

                   .start          = 8,

                   .end = 8,

                   .flags         = IORESOURCE_DMA,

         },

};

       好家伙,这个资源又是一个数组,每一个元素是struct resource类型,那就要看看没一个struct resource变量都定义了什么:

.start 起始位

.end 终止位

.flags 旗帜,其实就是资源类型,比如IORESOURCE_MEM(内存),IORESOURCE_IRQ(中断),IORESOURCE_DMA(dma通道)。

       另外start和end再说下,如果资源类型是IORESOURCE_MEM(内存)则分别表示起始地址,如果资源类型是IORESOURCE_IRQ(中断)则表示中断向量的起始值和终止值,如果相同则表示一个中断向量。

所以下面在platform_driver方面就要获取这些资源啦。

下面就分析下platform_driver

       根据上面分析的设备,那我们就理所当然的看下driver/mmc/host下的msm-sdcc.c咯。

msmsdcc_probe(struct platform_device *pdev)

{

         struct mmc_platform_data *plat = pdev->dev.platform_data;

         struct msmsdcc_host *host;

         struct mmc_host *mmc;

         struct resource *cmd_irqres = NULL;

         struct resource *pio_irqres = NULL;

         struct resource *stat_irqres = NULL;这三个都是中断资源

         struct resource *memres = NULL; //定义内存资源

         struct resource *dmares = NULL;//dma资源

         int ret;

 

         ....

 

         memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);

         dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);

         cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,

                                                          "cmd_irq");

         pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,

                                                          "pio_irq");

         stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,

                                                           "status_irq");

         ....

}

       在platform_driver中的probe函数中会使用我们前面交代的platform_get_resource,先看看这个函数的原型:

/**

 * platform_get_resource - get a resource for a device

 * @dev: platform device

 * @type: resource type

 * @num: resource index

 */

struct resource *platform_get_resource(struct platform_device *dev,

                                            unsigned int type, unsigned int num)

{

         int i;

 

         for (i = 0; i < dev->num_resources; i++) {

                   struct resource *r = &dev->resource[i];

 

                   if (type == resource_type(r) && num-- == 0)

                            return r;

         }

         return NULL;

}

       可以看到上面的参数说明是platform_device ,资源类型,以及索引值。

       对于上面在这个设备(msm_device_sdc1 )的资源我们不难看得出,通过设备类型就可以找到该类型对应的资源啦,可是这个索引是什么意思呢???

       其实索引值是针对相同资源类型设置的,通过上面函数内幕可以得出,某些设备相同类型的资源不止一个,但是为找到对应的正确的资源就必须通过该索引值来实现。

所以我想上面的中断资源使用函数platform_get_resource_byname调用相同类型的不同中段估计就是这方面的改进吧。



你可能感兴趣的:(嵌入式 分析platform_get_resource)