Nor Flash 驱动框架

框架入口源文件: lcd.c

(可根据入口源文件,再按着框架到内核走一遍)

内核版本:linux_2.6.22.6      硬件平台:JZ2440

 以下是驱动框架:

以下是驱动代码 s3c_nor_flash.c :

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <asm/io.h>

//定义一个 map_info
static struct map_info *s3c_map_info;

//定义一个 mtd_info
static struct mtd_info *s3c_mtd_info;

//定义一个 分区表
static  struct mtd_partition s3c_mtd_partition[] =
{
     [0] = { 
             .name = "bootloader_nor",
             .size = 0x40000,
             .offset = 0,
           },
     [1] = {
             .name = "nothing here",
             .size = MTDPART_OFS_APPEND,
             .size = MTDPART_SIZ_FULL,
           }
};


static int s3c_nor_flash_init(void)
{
  //分配 map_info
  s3c_map_info = kzalloc(sizeof(struct map_info),GFP_KERNEL);

  //设置 map_info
  s3c_map_info->bankwidth = 2;
  s3c_map_info->name = "s3c_nro_flash";
  s3c_map_info->phys =  0;
  s3c_map_info->size = 0x1000000;
  s3c_map_info->virt =  ioremap(s3c_map_info->phys,s3c_map_info->size);

  simple_map_init(s3c_map_info);
  
  //识别芯片
  s3c_mtd_info = do_map_probe("cfi_probe",s3c_map_info);
  if(s3c_mtd_info) printk("use cfi probe ");
  if(!s3c_mtd_info) 
      {
      printk("use jedec probe ");
      s3c_mtd_info = do_map_probe("jedec_probe",s3c_map_info);
    }
  if(!s3c_mtd_info) 
      {
      printk("sorry : can't creat mtd_info ");
          kfree(s3c_map_info);
        iounmap(s3c_map_info->virt);
        return -EIO;
    }
  //添加分区
  add_mtd_partitions(s3c_mtd_info, s3c_mtd_partition , 2);

  return 0;
}

static void s3c_nor_flash_exit(void)
{
  iounmap(s3c_map_info->virt);
  kfree(s3c_map_info);
  del_mtd_partitions(s3c_mtd_info);
}

module_init(s3c_nor_flash_init);
module_exit(s3c_nor_flash_exit);
MODULE_LICENSE("GPL");

以下是编译驱动的Makefile:

KERN_DIR = /work/systems/kernel/linux-2/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order

obj-m    +=s3c_nor_flash.o

猜你喜欢

转载自www.cnblogs.com/zsy12138/p/10392694.html
今日推荐