现在的位置: 首页 > 综合 > 正文

arm 驱动进阶:nor flash 驱动程序设计

2012年09月15日 ⁄ 综合 ⁄ 共 2514字 ⁄ 字号 评论关闭

nor flash 驱动程序设计流程:

 

nor flash 驱动程序内核识别分析:

analysis code

NOR FLASH识别过程:
do_map_probe("cfi_probe", s3c_nor_map);
    drv = get_mtd_chip_driver(name)
    ret = drv->probe(map);  // cfi_probe.c
            cfi_probe
                mtd_do_chip_probe(map, &cfi_chip_probe);
                    cfi = genprobe_ident_chips(map, cp);
                                genprobe_new_chip(map, cp, &cfi)
                                    cp->probe_chip(map, 0, NULL, cfi)
                                            cfi_probe_chip
                                                // 进入CFI模式
                                                cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
                                                // 看是否能读出"QRY"
                                                qry_present(map,base,cfi)
                                                .....
                                                
do_map_probe("jedec_probe", s3c_nor_map);
    drv = get_mtd_chip_driver(name)
    ret = drv->probe(map);  // jedec_probe
            jedec_probe
                mtd_do_chip_probe(map, &jedec_chip_probe);
                    genprobe_ident_chips(map, cp);
                        genprobe_new_chip(map, cp, &cfi)
                            cp->probe_chip(map, 0, NULL, cfi)
                                    jedec_probe_chip
                                        // 解锁
                                        cfi_send_gen_cmd(0xaa, cfi->addr_unlock1, base, map, cfi, cfi->device_type, NULL);
                                        cfi_send_gen_cmd(0x55, cfi->addr_unlock2, base, map, cfi, cfi->device_type, NULL);
                                        
                                        // 读ID命令
                                        cfi_send_gen_cmd(0x90, cfi->addr_unlock1, base, map, cfi, cfi->device_type, NULL);                                      
                            
                                        // 得到厂家ID,设备ID
                                        cfi->mfr = jedec_read_mfr(map, base, cfi);
                                        cfi->id = jedec_read_id(map, base, cfi);
                                        
                                        // 和数组比较
                                        jedec_table

 

nor flash 驱动程序代码实现:

s3c_nor.c

/*
 * 参考 drivers\mtd\maps\physmap.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>

static struct map_info *s3c_nor_map;
static struct mtd_info *s3c_nor_mtd;

static struct mtd_partition s3c_nor_parts[] = {
    [0] = {
        .name   = "bootloader_nor",
        .size   = 0x00040000,
        .offset    = 0,
    },
    [1] = {
        .name   = "root_nor",
        .offset = MTDPART_OFS_APPEND,
        .size   = MTDPART_SIZ_FULL,
    }
};


static int s3c_nor_init(void)
{
    /* 1. 分配map_info结构体 */
    s3c_nor_map = kzalloc(sizeof(struct map_info), GFP_KERNEL);;
    
    /* 2. 设置: 物理基地址(phys), 大小(size), 位宽(bankwidth), 虚拟基地址(virt) */
    s3c_nor_map->name = "s3c_nor";
    s3c_nor_map->phys = 0;
    s3c_nor_map->size = 0x1000000; /* >= NOR的真正大小 */
    s3c_nor_map->bankwidth = 2;
    s3c_nor_map->virt = ioremap(s3c_nor_map->phys, s3c_nor_map->size);

    simple_map_init(s3c_nor_map);
    
    /* 3. 使用: 调用NOR FLASH协议层提供的函数来识别 */
    printk("use cfi_probe\n");
    s3c_nor_mtd = do_map_probe("cfi_probe", s3c_nor_map);
    if (!s3c_nor_mtd)
    {
        printk("use jedec_probe\n");
        s3c_nor_mtd = do_map_probe("jedec_probe", s3c_nor_map);
    }

    if (!s3c_nor_mtd)
    {        
        iounmap(s3c_nor_map->virt);
        kfree(s3c_nor_map);
        return -EIO;
    }
    
    /* 4. add_mtd_partitions */
    add_mtd_partitions(s3c_nor_mtd, s3c_nor_parts, 2);
    
    return 0;
}

static void s3c_nor_exit(void)
{
    del_mtd_partitions(s3c_nor_mtd);
    iounmap(s3c_nor_map->virt);
    kfree(s3c_nor_map);
}

module_init(s3c_nor_init);
module_exit(s3c_nor_exit);

MODULE_LICENSE("GPL");

 

 

抱歉!评论已关闭.