新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > NOR FLASH驅動程序

        NOR FLASH驅動程序

        作者: 時間:2016-11-21 來源:網絡 收藏
        //
        // 參考 driversmtdmapsphysmap.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; //nor啟動的時候物理地址是0
        s3c_nor_map->size = 0x1000000; // >= NOR的真正大小 //
        s3c_nor_map->bankwidth = 2; //16位
        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_proben");
        s3c_nor_mtd = do_map_probe("cfi_probe", s3c_nor_map);
        if (!s3c_nor_mtd)
        {
        printk("use jedec_proben");
        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");
        =============================================================
        使用UBOOT體驗NOR FLASH的操作(開發板設為NOR啟動,進入UBOOT)
        先使用OpenJTAG燒寫UBOOT到NOR FLASH
        1. 讀數據
        md.b 0
        2. 讀ID
        NOR手冊上:
        往地址555H寫AAH
        往地址2AAH寫55H
        往地址555H寫90H
        讀0地址得到廠家ID: C2H
        讀1地址得到設備ID: 22DAH或225BH
        退出讀ID狀態: 給任意地址寫F0H
        2440的A1接到NOR的A0,所以2440發出(555h<<1), NOR才能收到555h這個地址
        UBOOT怎么操作?
        往地址AAAH寫AAH mw.w aaa aa
        往地址554寫55H mw.w 554 55
        往地址AAAH寫90H mw.w aaa 90
        讀0地址得到廠家ID: C2H md.w 0 1
        讀2地址得到設備ID: 22DAH或225BH md.w 2 1
        退出讀ID狀態: mw.w 0 f0
        3. NOR有兩種規范, jedec, cfi(common flash interface)
        讀取CFI信息
        NOR手冊:
        進入CFI模式 往55H寫入98H
        讀數據: 讀10H得到0051
        讀11H得到0052
        讀12H得到0059
        讀27H得到容量
        2440的A1接到NOR的A0,所以2440發出(555h<<1), NOR才能收到555h這個地址
        UBOOT怎么操作?
        進入CFI模式 往AAH寫入98H mw.w aa 98
        讀數據: 讀20H得到0051 md.w 20 1
        讀22H得到0052 md.w 22 1
        讀24H得到0059 md.w 24 1
        讀4EH得到容量 md.w 4e 1
        退出CFI模式 mw.w 0 f0
        4. 寫數據: 在地址0x100000寫入0x1234
        md.w 100000 1 // 得到ffff
        mw.w 100000 1234
        md.w 100000 1 // 還是ffff
        NOR手冊:
        往地址555H寫AAH
        往地址2AAH寫55H
        往地址555H寫A0H
        往地址PA寫PD
        2440的A1接到NOR的A0,所以2440發出(555h<<1), NOR才能收到555h這個地址
        UBOOT怎么操作?
        往地址AAAH寫AAH mw.w aaa aa
        往地址554H寫55H mw.w 554 55
        往地址AAAH寫A0H mw.w aaa a0
        往地址0x100000寫1234h mw.w 100000 1234
        NOR FLASH驅動程序框架
        測試1:通過配置內核支持NOR FLASH
        1. make menuconfig
        -> Device Drivers
        -> Memory Technology Device (MTD) support
        -> Mapping drivers for chip access
        CFI Flash device in physical memory map
        (0x0) Physical start address of flash mapping // 物理基地址
        (0x1000000) Physical length of flash mapping // 長度
        (2) Bank width in octets (NEW) // 位寬
        2. make modules
        cp drivers/mtd/maps/physmap.ko /work/nfs_root/first_fs
        3. 啟動開發板
        ls /dev/mtd*
        insmod physmap.ko
        ls /dev/mtd*
        cat /proc/mtd
        測試2: 使用自己寫的驅動程序:
        1. ls /dev/mtd*
        2. insmod s3c_nor.ko
        3. ls /dev/mtd*
        4. 格式化:
        flash_eraseall -j /dev/mtd1 //-j:表示格式化成jffs2,一般nor flash用jffs2,nand flash用yaffs
        //格式化的時候用字符設備mtd1
        5. mount -t jffs2 /dev/mtdblock1 /mnt //掛接的時候用塊設備mtdblock1
        在/mnt目錄下操作文件
        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



        關鍵詞: NORFLASH驅動程

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 平湖市| 尖扎县| 正镶白旗| 广宗县| 阳山县| 上虞市| 蕲春县| 阿合奇县| 大渡口区| 嵩明县| 温州市| 左贡县| 西安市| 曲靖市| 江北区| 芦山县| 保康县| 安丘市| 牙克石市| 措美县| 长葛市| 阿巴嘎旗| 白水县| 兴宁市| 宜兰市| 石城县| 阜平县| 中山市| 呼伦贝尔市| 铁岭市| 万源市| 星座| 南宁市| 富平县| 固安县| 兰西县| 双流县| 溆浦县| 成武县| 蒙山县| 麻城市|