新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > [ARM笔记]驱动对设备的识别过程及实例——NAND Flash

[ARM笔记]驱动对设备的识别过程及实例——NAND Flash

作者:时间:2016-12-02来源:网络收藏

  程序识别设备时,有以下两种方法:

本文引用地址:http://www.eepw.com.cn/article/201612/341049.htm

  (1)程序本身带有设备信息,比如开始地址、中断号等;加载程序时,就可以根据这些信息来识别设备。

  (2)驱动程序本身没有设备信息,但是内核中已经(或以后)根据其他方式确定了很多设备的信息;加载驱动程序时,将驱动程序与这些设备逐个比较,确定两者是否匹配(math)。如果驱动程序与某个设备匹配,就可以通过该驱动程序来操作这个设备了。

  内核常使用第二种方法来识别设备,这可以将各种设备集中在一个文件中管理,当开发板的配置改变时,便于修改代码。在内核文件include/linux/platform_device.h中,定义了两个数据结构来表示这些设备和驱动程序:platform_device结构用来描述设备的名称、ID、所占用的资源(比如内存地址/大小、中断号)等;platform_driver结构用来描述各种操作函数,比如枚举函数、移除设备函数、驱动名称等。

  内核启动后,首先构造链表将描述设备的platform_device结构组织起来,得到一个设备的列表;当加载某个驱动程序的platform_driver结构时,使用一些匹配函数来检查驱动程序能否支持这些设备,常用的检查方法很简单:比较驱动程序和设备的名称。

  以S3C2410开发板为例,在include/arch/arm/mach-s3c2410/mach-smdk2410.c中定义了如下设备:

  89 static struct platform_device *smdk2410_devices[] __initdata = {

  90 &s3c_device_usb,

  91 &s3c_device_lcd,

  92 &s3c_device_wdt,

  93 &s3c_device_i2c,

  94 &s3c_device_iis,

  95 };

  104 static void __init smdk2410_init(void)

  105 {

  106 platform_add_devices(smdk2410_devices, ARRAY_SIZE(smdk2410_devices));

  107 smdk_machine_init();

  108 }

  在include/arch/arm/plat-s3c24xx/common-smdk.c中定义了如下设备:

  175 static struct platform_device __initdata *smdk_devs[] = {

  176 &s3c_device_nand,

  177 &smdk_led4,

  178 &smdk_led5,

  179 &smdk_led6,

  180 &smdk_led7,

  181 };

  183 void __init smdk_machine_init(void)

  184 {

  185 /* Configure the LEDs (even if we have no LED support)*/

  186

  187 s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);

  188 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);

  189 s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);

  190 s3c2410_gpio_cfgpin(S3C2410_GPF7, S3C2410_GPF7_OUTP);

  191

  192 s3c2410_gpio_setpin(S3C2410_GPF4, 1);

  193 s3c2410_gpio_setpin(S3C2410_GPF5, 1);

  194 s3c2410_gpio_setpin(S3C2410_GPF6, 1);

  195 s3c2410_gpio_setpin(S3C2410_GPF7, 1);

  196

  197 if (machine_is_smdk2443())

  198 smdk_nand_info.twrph0 = 50;

  199

  200 s3c_device_nand.dev.platform_data = &smdk_nand_info;

  201

  202 platform_add_devices(smdk_devs, ARRAY_SIZE(smdk_devs));

  203

  204 s3c2410_pm_init();

  205 }

  627 int __init s3c2410_pm_init(void)

  628 {

  629 printk("S3C2410 Power Management, (c) 2004 Simtec Electronicsn");

  630

  631 suspend_set_ops(&s3c2410_pm_ops);

  632 return 0;

  633 }

  这些设备在smdk2410_init函数(对应S3C2410)或smdk2440_machine_init函数(对应S3C2440)中,通过plat_add_devices函数注册进内核中。

   Flash设备s3c2410_device_nand在include/arch/arm/plat-s3c24xx/devs.c中的定义如下:

  201 struct platform_device s3c_device_nand = {

  202 .name = "s3c2410-nand",

  203 .id = -1,

  204 .num_resources = ARRAY_SIZE(s3c_nand_resource),

  205 .resource = s3c_nand_resource,

  206 };

  207

  208 EXPORT_SYMBOL(s3c_device_nand); /*导出符号,以便外部文件引用*/

  对于S3C2440开发板,s3c_device_nand结构的名字会在s3c244x_map_io函数中修改为“s3c2440-nand”,这个函数在include/arch/arm/plat-s3c24xx/s3c244x.c中定义如下:

  59 void __init s3c244x_map_io(struct map_desc *mach_desc, int size)

  ...

  68 s3c_device_i2c.name = "s3c2440-i2c";

  69 s3c_device_nand.name = "s3c2440-nand";

  70 s3c_device_usbgadget.name = "s3c2440-usbgadget";

  71 }

  有了 Flash设备,还要有 Flash驱动程序,内核针对S3C2410、S3C2412、S3C2440定义了3个驱动。它们在include/drivers/mtd/nand/s3c2410.c中的s3c2410_nand_init函数中注册进内核,如下所示:

  890 static int __init s3c2410_nand_init(void)

  891 {

  892 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronicsn");

  893

  894 platform_driver_register(&s3c2412_nand_driver);

  895 platform_driver_register(&s3c2440_nand_driver);

  896 return platform_driver_register(&s3c2410_nand_driver);

  897 }

  其中的S3C2410_nand_driver结构是在include/drivers/mtd/nand/s3c2410.c中定义,如下所示:

  857 static struct platform_driver s3c2410_nand_driver = {

  858 .probe = s3c2410_nand_probe,

  859 .remove = s3c2410_nand_remove,

  860 .suspend = s3c24xx_nand_suspend,

  861 .resume = s3c24xx_nand_resume,

  862 .driver = {

  863 .name = "s3c2410-nand",

  864 .owner = THIS_MODULE,

  865 },

  866 };

  可见,s3c_device_nand结构和s3c2410_nand_driver结构中的name成员相同,都是“s3c2410-nand”。platform_driver_register函数就是根据这点确定它们是匹配的,所以调用s3c2410_nand_probe函数枚举NAND Flash设备s3c2410_device_nand。



关键词: NAND 驱动

评论


相关推荐

技术专区

关闭