Chinaunix首页 | 论坛 | 博客
  • 博客访问: 967851
  • 博文数量: 109
  • 博客积分: 554
  • 博客等级: 中士
  • 技术积分: 2577
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 12:49
文章分类

全部博文(109)

文章存档

2019年(5)

2016年(7)

2015年(9)

2014年(1)

2013年(71)

2012年(16)

分类: 嵌入式

2013-05-19 19:00:49

 

编写Nor map驱动

-rwxr-xr-x   1 enzo enzo 253K  5 18 16:32 u-boot.bin

Nor flash256K存放uboot,所以驱动中定义的mtd_partition 要把uboot的空间留出来,不要破坏uboot

 


  1. /*
  2.  * mini2440 nor flash mappings of chips in physical memory
  3.  *
  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. #ifdef CONFIG_MTD_PARTITIONS
  13. #include <linux/mtd/partitions.h>
  14. #endif

  15. #define WINDOW_ADDR 0x00000000 /* physical properties of flash */
  16. #define WINDOW_SIZE 0x0200000 /*2M*/
  17. #define BUSWIDTH 2 /*16bit bus*/
  18. #define FLASH_BLOCKSIZE_MAIN    0x20000
  19. #define FLASH_NUMBLOCKS_MAIN    128
  20. /* can be "cfi_probe", "jedec_probe", "map_rom", NULL }; */
  21. #define PROBETYPES { "cfi_probe","jedec_probe", "map_rom",NULL } /*mini2440 nor is jedec*/

  22. #define MSG_PREFIX "Mini2440-NOR:" /* prefix for our printk()'s */
  23. #define MTDID "Mini2440-nor" /* for mtdparts= partitioning */

  24. static struct mtd_info *mymtd;

  25. struct map_info mini2440nor_map = {
  26.     .name = "NOR flash on mini2440",
  27.     .size = WINDOW_SIZE,
  28.     .bankwidth = BUSWIDTH,
  29.     .phys = WINDOW_ADDR,
  30. };

  31. #ifdef CONFIG_MTD_PARTITIONS

  32. /*
  33.  * MTD partitioning stuff
  34.  */
  35. static struct mtd_partition static_partitions[] =
  36. {
  37.     {
  38.         .name = "Bootloader",
  39.         .size = 256*1024,
  40.         .offset = 0
  41.     },
  42.     {
  43.         .name = "reserve",
  44.         .size = MTDPART_SIZ_FULL,
  45.         .offset = MTDPART_OFS_APPEND
  46.     },

  47. };

  48. static const char *probes[] = { "RedBoot", "cmdlinepart", NULL };

  49. #endif

  50. static int mtd_parts_nb = 0;
  51. static struct mtd_partition *mtd_parts = 0;

  52. static int __init init_mini2440nor(void)
  53. {
  54.     static const char *rom_probe_types[] = PROBETYPES;
  55.     const char **type;
  56.     const char *part_type = 0;

  57.     printk(KERN_NOTICE MSG_PREFIX "0x%08x at 0x%08xn",
  58.      WINDOW_SIZE, WINDOW_ADDR);
  59.     mini2440nor_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);

  60.     if (!mini2440nor_map.virt) {
  61.         printk(MSG_PREFIX "failed to ioremapn");
  62.         return -EIO;
  63.     }

  64.     simple_map_init(&mini2440nor_map);

  65.     mymtd = 0;
  66.     type = rom_probe_types;
  67.     for(; !mymtd && *type; type++) {
  68.         mymtd = do_map_probe(*type, &mini2440nor_map);
  69.     }
  70.     if (mymtd) {
  71.         mymtd->owner = THIS_MODULE;

  72. #ifdef CONFIG_MTD_PARTITIONS
  73.         mtd_parts_nb = parse_mtd_partitions(mymtd, probes, &mtd_parts, MTDID);
  74.         if (mtd_parts_nb > 0)
  75.             part_type = "detected";

  76.         if (mtd_parts_nb == 0)
  77.         {
  78.             mtd_parts = static_partitions;
  79.             mtd_parts_nb = ARRAY_SIZE(static_partitions);
  80.             part_type = "static";
  81.         }
  82. #endif
  83.         /* 注册设备 */
  84.         add_mtd_device(mymtd);
  85.         if (mtd_parts_nb == 0)
  86.             printk(KERN_NOTICE MSG_PREFIX "no partition info availablen");
  87.         else
  88.         {
  89.             printk(KERN_NOTICE MSG_PREFIX
  90.              "using %s partition definitionn", part_type);
  91.             add_mtd_partitions(mymtd, mtd_parts, mtd_parts_nb);
  92.         }
  93.         return 0;
  94.     }

  95.     iounmap((void *)mini2440nor_map.virt);
  96.     return -ENXIO;
  97. }

  98. static void __exit cleanup_mini2440nor(void)
  99. {
  100.     if (mymtd) {
  101.         del_mtd_device(mymtd);
  102.         map_destroy(mymtd);
  103.     }
  104.     if (mini2440nor_map.virt) {
  105.         iounmap((void *)mini2440nor_map.virt);
  106.         mini2440nor_map.virt = 0;
  107.     }
  108. }

  109. module_init(init_mini2440nor);
  110. module_exit(cleanup_mini2440nor);

  111. MODULE_LICENSE("GPL");
  112. MODULE_AUTHOR("Enzo Fang ");
  113. MODULE_DESCRIPTION("Generic configurable MTD map driver");


 

修改内核MakefileKconfig

 

修改/driver/mtd/maps/Makeconfig

obj-$(CONFIG_MTD_EDB7312)   += edb7312.o

obj-$(CONFIG_MTD_MINI2440) += mini2440flash.o

 

修改/drivers/mtd/maps/Kconfig

config MTD_EDB7312

    tristate "CFI Flash device mapped on EDB7312"

    depends on ARCH_EDB7312 && MTD_CFI

    help

      This enables access to the CFI Flash on the Cogent EDB7312 board.

      If you have such a board, say 'Y' here.

 

config MTD_MINI2440

    tristate "CFI Flash device mapped on MINI2440"

    depends on ARM && MTD_CFI

    help

      This enables access to the CFI Flash on the MINI2440 board.

      If you have such a board, say 'Y' here.

 

结果:

# cat /proc/mtd

dev:    size   erasesize  name

mtd0: 00200000 00001000 "NOR flash on mini2440"

mtd1: 00040000 00001000 "Bootloader"

mtd2: 001c0000 00001000 "reserve"

//todo nor加入平台驱动

 

格式化nor flash

//todo为文件系统添加mtd-utils

  [*] mtd-utils
  [*]   erase

  [*]   eraseall
  [*]   mkfs.jff2

擦除相应的存放jffs2文件系统的块

# eraseall /dev/mtd2

把该块以jffs2格式挂载到mnt下,这样在mnt目录下面就能执行任何写操作了。

# mount -t jffs2 /dev/mtdblock3 /mnt/

6.掉电重启,需要重新执行一遍mount -t jffs2 /dev/mtdblock3 /mnt/

 


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