当前位置: > 华清远见教育科技集团 > 嵌入式学习 > 讲师博文 > Linux3.4内核Nand Flash驱动的移植
Linux3.4内核Nand Flash驱动的移植
时间:2016-12-14作者:华清远见

【实验目的】

Nand flash 是嵌入式系统常用的内部存储设备,通过移植Nand flash驱动,了解Linux内核Nand Flash的实现。

说明:在本系统移植课程实验中命令行提示符“$”表示是在主机上执行,“#”表示在目标板执行。

【实验环境】

● 主机:Ubuntu 10.10 (64bit);

● 目标机:FS_S5PC100平台;

● 交叉编译工具链:arm-eabi-4.4.0(Android4.0.4自带交叉工具链);

【实验步骤】

1. 添加驱动文件

添加针对我们平台的Nand flash驱动。

● 拷贝s3c_nand.c到drivers/mtd/nand下;

● 拷贝regs-nand.h到arch/arm/mach-s5pc100/include/mach下;

cp s3c_nand.c drivers/mtd/nand
        cpregs-nand.h arch/arm/mach-s5pc100/include/mach

2. 修改文件

针对平台上的nand flash设备,修改drivers/mtd/nand/nand_base.c第2975行。

for (i = 0; i< 5; i++)

3. 配置文件修改

● 修改drivers/mtd/nand/Kconfig,在if mtd_nand(216行)下面添加如下内容:

config MTD_NAND_S3C
        tristate "NAND Flash support for S3C SoC"
        depends on (ARCH_S3C64XX || ARCH_S5P64XX || ARCH_S5PC1XX
                                || ARCH_S5PC100) && MTD_NAND
        help
                        This enables the NAND flash controller on the S3C.
                        No board specfic support is done by this driver, each board
        must advertise a platform_device for the driver to attach.

        config MTD_NAND_S3C_DEBUG
        bool "S3C NAND driver debug"
        depends on MTD_NAND_S3C
        help
                        Enable debugging of the S3C NAND driver

        config MTD_NAND_S3C_HWECC
        bool "S3C NAND Hardware ECC"
        depends on MTD_NAND_S3C
        help
                        Enable the use of the S3C's internal ECC generator when
        using NAND. Early versions of the chip have had problems with
        incorrect ECC generation, and if using these, the default of
        software ECC is preferable.

                        If you lay down a device with the hardware ECC, then you will
        currently not be able to switch to software, as there is no
        implementation for ECC method used by the S3C

● 修改drivers/mtd/nand/Makefile添加如下内容(19行):

obj-$(CONFIG_MTD_NAND_S3C) += s3c_nand.o

4. 平台代码修改

修改arch/arm/mach-s5pc100/mach-smdkc100.c添加如下内容:

● 添加头文件

1 #if defined (CONFIG_MTD_NAND_S3C)
        2 #include <linux/mtd/partitions.h>
        3 #include <linux/mtd/mtd.h>
        4 #include <plat/nand.h>
        5 #endif

● 添加平台设备

01 #if defined(CONFIG_MTD_NAND_S3C)
        02 /* Nand Flash Support */
        03 static struct mtd_partition s5pc100_nand_part[] = {
        04        [0] = {
        05                .name = "bootloader",
        06                .size = SZ_1M,
        07                .offset = 0,
        08        },
        09        [1] = {
        10                .name = "kernel",
        11                .offset = MTDPART_OFS_APPEND,
        12                .size = SZ_1M*3,
        13        },
        14        [2] = {
        15                .name = "rootfs",
        16                .offset = MTDPART_OFS_APPEND,
        17                .size = MTDPART_SIZ_FULL,
        18        },
        19 };
        20
        21 struct s3c_nand_mtd_info s5pc100_nand_mtd_part_info = {
        22        .chip_nr = 1,
        23        .mtd_part_nr = ARRAY_SIZE(s5pc100_nand_part),
        24        .partition = s5pc100_nand_part,
        25 };
        26
        27 static struct resource s5pc100_nand_resource[] = {
        28        [0] = {
        29                .start = 0xE7200000,
        30                .end = 0xE7200000 + SZ_1M,
        31                .flags = IORESOURCE_MEM,
        32        }
        33 };
        34
        35 struct platform_device s5pc100_device_nand = {
        36        .name = "s5pc100-nand",
        37        .id = -1,
        38        .num_resources = ARRAY_SIZE(s5pc100_nand_resource),
        39        .resource = s5pc100_nand_resource,
        40        .dev = {
        41                .platform_data = &s5pc100_nand_mtd_part_info,
        42        }
        43 };
        44 #endif

● 添加平台设备列表

在smdkc100_device[]结构体数组中添加如下内容:

1 #if defined(CONFIG_MTD_NAND_S3C)
        2        &s5pc100_device_nand,
        3 #endif

5. 相关头文件修改

修改arch/arm/plat-samsung/include/plat/nand.h添加如下内容:

1 #if defined(CONFIG_MTD_NAND_S3C)
        2 struct s3c_nand_mtd_info {
        3        uint chip_nr;
        4        uint mtd_part_nr;
        5        struct mtd_partition *partition;
        6 };
        7 #endif

6. 配置内核

$ make menuconfig

Device Drivers --->
                    <*> Memory Technology Device (MTD) support --->
                            <*> Caching block device access to MTD devices
                            <*> NAND Device Support --->
                                    <*> NAND Flash support for S3C SoC
                                    [*] S3C NAND Hardware ECC

-*- Enable the block layer --->
        Partition Types --->
                                    [*] Advanced partition selection
                                    [*] PC BIOS (MSDOS partition tables) support
                                    [*] BSD disklabel (FreeBSD partition tables) support

7. 编译内核

编译内核并拷贝到tftpboot下。

$ make zImage
        $ cp arch/arm/boot/zImage /tftpboot

8. 测试

启动目标板,在目标板上完成如下操作:

# cat /proc/mtd

可以看到之前内核对Nand Flash的分区表。

dev: size erasesize name
        mtd0: 00100000 00020000 "bootloader"
        mtd1: 00300000 00020000 "kernel"
        mtd2: 0fc00000 00020000 "rootfs"

发表评论
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)