mini2440 Norflash驱动移植过程

我不知道友善linux2.6.32的内核是否支持了mini2440的norflash,我在这里还是亲自的把mini2440的norflash支持上。移植过程分享如下:
            
.添加配置项
1
、修改 drivers/mtd/maps/Kconfig文件,config_EDB7312配置项下添加如下内容:
config
 MTD_MINI2440
tristate
 "CFI Flash device mapped onMINI2440"
depends
 on ARM && MTD_CFI
help
This
 enables access to the CFI Flash on the CogentMINI2440 board.
If
 you have such a board, say 'Y' here.
2
、修改 drivers/mtd/maps/Makefile文件,obj-$(CONFIG_MTD_EDB7312)+= edb7312.o添加如下内容:
obj-$(CONFIG_MTD_MINI2440)+=
 mini2440.o
3
、复制驱动文件mini2440.cdrivers/mtd/maps目录下
.配置编译内核
#
 make menuconfig
以下内容必选:
Memory
 Technology Devices(MTD)-->
<*>Memory
 Technology Device (MTD) support
[*]MTD
 partitioning support
   <*>Direct
 char device access to MTD devices
   <*>Caching
 block device access to MTD devices
RAM/ROM/Flash
 chip drivers-->
   <*>Detect
 flash chips by Common Flash Interface (CFI) probe
   <*>Support
 for Inter/Sharp flash chips
   <*> Supportfor AMD/Fujitsu/Sp ansion flash chips

   <*>CFI flash device mapped on mini2440
下列内容(flash上架fat16/fat32/ntfs/ext2等文件系统时才需要选上)不要选,否则会出现加载驱动模块时会出现ftl_cs:FTL header not found.
DeviceDrivers ->
  Memory Technology Devices (MTD) ->
      <>FTL (Flash Translation Layer) support
       <> NFTL(NAND Flash Translation Layer) support
       <>INFTL(Inverse NAND Flash Translation Layer) support

.根据edb7312单板的norflash驱动(linux/mtd/maps/edb7312.c)来写mini2440norflash驱动
代码在mini2440.c(带详细注释)

  1. /*支持单板:mini2440
  2.  *支持内核:3.1.2版本
  3.  *模仿驱动:/linux/drivers/mtd/maps/edb7312.c
  4.  */
  5. #include <linux/module.h>
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <asm/io.h>
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/map.h>
  12. #include <linux/mtd/partitions.h>
  13. #define WINDOW_ADDR 0x00000000     /* NOR FLASH物理地址 */
  14. #define WINDOW_SIZE 0x00200000         /* NOR FLASH大小 */
  15. #define BUSWIDTH    2 /*估计是2Byte*/
  16. /* 探测的接口类型,可以是"cfi_probe", "jedec_probe", "map_rom", NULL }; */
  17. #define PROBETYPES { "cfi_probe", NULL }
  18. #define MSG_PREFIX "MINI2440-NOR:"   /*printk的前缀 */
  19. #define MTDID      "mini2440-nor"    /*MTD驱动*/
  20. static struct mtd_info *mymtd;
  21. /*定义一个map_info,代表一块norflash*/
  22. struct map_info mini2440nor_map =
  23. {
  24.         .name = "NOR flash on MINI2440",
  25.         .size = WINDOW_SIZE,
  26.         .bankwidth = BUSWIDTH,
  27.         .phys = WINDOW_ADDR,
  28. };
  29. /* 默认分区信息*/
  30. static struct mtd_partition static_partitions[3] =
  31. {
  32. {
  33. .name = "bootloader",
  34. .size = 0x40000,//256K
  35. .offset = 0
  36. },
  37. {
  38. .name = "Kernel",
  39. .size = 0x100000,//1M
  40. .offset = 0x40000
  41. },
  42. {
  43. .name = "RootFS",
  44. .size = 0xC0000,//768K
  45. .offset = 0x140000
  46. },
  47. };
  48. static const char *probes[] = {NULL};
  49. static int mtd_parts_nb = 0;
  50. static struct mtd_partition *mtd_parts = 0;
  51. /*Norflash模型初始化函数
  52.  *<1>物理地址映射为虚拟地址: mini2440nor_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
  53.  *<2>探测norflash芯片,得到map_info:do_map_probe();
  54.  *<3>探测分区信息,得到mtd_partition:parse_mtd_partitions();
  55.  *<4>利用map_info和mtd_partition注册设备:mtd_device_register();
  56.  */
  57. int __init init_mini2440nor(void)
  58. {
  59.         static const char *rom_probe_types[] = PROBETYPES;
  60.         const char **type;
  61.         const char *part_type = 0;
  62.         printk(KERN_NOTICE MSG_PREFIX "0x%08x at 0x%08x\n", WINDOW_SIZE, WINDOW_ADDR);
  63.         /*物理地址映射为虚拟地址*/
  64.         mini2440nor_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
  65.         if (!mini2440nor_map.virt)
  66.         {
  67.                 printk(MSG_PREFIX "failed to ioremap\n");
  68.                 return  -EIO;
  69.         }
  70.         simple_map_init(&mini2440nor_map);
  71.         mymtd = 0;
  72.         type = rom_probe_types;
  73.         for (; !mymtd &&  *type; type++)
  74.         {
  75.                 /*根据map_info探测norflash芯片,若成功则返回mtd_info*/
  76.                 mymtd = do_map_probe(*type, &mini2440nor_map);
  77.         }
  78.         if (mymtd)
  79.         {
  80.                 mymtd->owner = THIS_MODULE;
  81.                 /*探测分区信息,若探测成功,则mtd_parts中保存了分区信息;若探测失败,使用数组定义的默认分区信息*/
  82.                 mtd_parts_nb = parse_mtd_partitions(mymtd, probes, &mtd_parts, MTDID);
  83.                 if (mtd_parts_nb > 0)
  84.                         part_type = "detected";
  85.                 if (mtd_parts_nb == 0)
  86.                 {
  87.                         mtd_parts = static_partitions;
  88.                         mtd_parts_nb = ARRAY_SIZE(static_partitions);
  89.                         part_type = "static";
  90.                 }
  91.                 if (mtd_parts_nb == 0)
  92.                         printk(KERN_NOTICE MSG_PREFIX "no partition info available\n");
  93.                 else
  94.                         printk(KERN_NOTICE MSG_PREFIX "using %s partition definition\n",
  95.                                part_type);
  96.                 /*注册设备*/
  97.                 mtd_device_register(mymtd, NULL, 0);
  98.                 mtd_device_register(mymtd, mtd_parts, mtd_parts_nb);
  99.                 return 0;
  100.         }
  101.         iounmap((void *)mini2440nor_map.virt);
  102.         return -ENXIO;
  103. }
  104. /*Norflash模块卸载函数
  105.  *<1>注销设备:mtd_device_unregister();
  106.  *<2>销毁map_info:map_destroy();
  107.  *<3>取消映射:iounmap();
  108.  */
  109. static void __exit cleanup_mini2440nor(void)
  110. {
  111.         if (mymtd)
  112.         {
  113.                 /*注销设备*/
  114.                 mtd_device_unregister(mymtd);
  115.                 /*销毁map_info*/
  116.                 map_destroy(mymtd);
  117.         }
  118.         if (mini2440nor_map.virt)
  119.         {
  120.                 iounmap((void*)mini2440nor_map.virt);
  121.                 mini2440nor_map.virt = 0;
  122.         }
  123. }
  124. module_init(init_mini2440nor);
  125. module_exit(cleanup_mini2440nor);
  126. MODULE_LICENSE("GPL");
  127. MODULE_AUTHOR("WeiDong Wu <[email protected]>");
  128. MODULE_DESCRIPTION("Generic configurable MTD map driver");

你可能感兴趣的:(mini2440 Norflash驱动移植过程)