u-boot:从NOR FLASH启动并支持NAND FLASH读写的移植

时间:2021-08-26 17:22:36

需要准备的有:TQ2440目标板,ELDK-5.1编译器,u-boot-2011.06

1.我的 u-boot-2011.06.tar.bz2 放在共享文件夹中,工作目录在 /home/huwei/uboot/ 下,因此解压到这个文件夹:tar jxvf u-boot-2011.06.tar.bz2 /home/huwei/uboot/ ;

2.接下来搭建针对硬件的 u-boot 框架,进入 u-boot 代码文件夹 cd /home/huwei/uboot/u-boot-2011.06/ :

    A.cp include/configs/smdk2410.h include/configs/tq2440.h

    B.cp -r board/samsung/smdk2410 board/samsung/tq2440

    C.mv board/samsung/tq2440/smdk2410.c board/samsung/tq2440/tq2440.c    

    D.修改 u-boot 顶层的 Makefile 文件:gedit /home/huwei/uboot/u-boot-2011.06/Makefile ,第28行COBJS:=smdk2410.o 改为 COBJS:=tq2440.o ,保存并退出

    E.配置板子信息,gedit /home/huwei/uboot/u-boot-2011.06/boards.cfg,找到smdk2410,在第70行加入板子名为 tq2440 的信息,即:tq2440 arm arm920t - samsung s3c24x0 ,保存并退出。这样做了就不需要再去顶层 Makefile 中加入 tq2440_config: unconfig 的编译配置信息了。

    可以验证一下:make CROSS_COMPILE=arm-linux-gnueabi- clean

                  make CROSS_COMPILE=arm-linux-gnueabi- tq2440_config

                  make CROSS_COMPILE=arm-linux-gnueabi- all

    在顶层生成了 u-boot.bin 说明 OK。

3.下面开始 NOR FLASH 启动的移植。

    A.修改板子配置头文件的宏定义:

      gedit /home/huwei/uboot/u-boot-2011.06/include/configs/tq2440.h

      第38行,#define CONFIG_S3C2410 改为 #define CONFIG_S3C2440 ,通知编译器目标处理器是 S3C2440      

      第131行,#define CONFIG_SYS_PROMPT "SMDK2410 #" 改为 #define CONFIG_SYS_PROMPT "TQ2440 #",目的是在控制台命令行开头提示符显示为 TQ2440 #

      第185行,#define CONFIG_FLASH_CFI_LEGACY 改为 #define CONFIG_SYS_CFI_FLASH_CONFIG_REGS
{0xFFFF},由于 u-boot-2011.06 里 NOR FLASH 使用的是 CFI 接口,因此不需要定义 CONFIG_FLASH_CFI_LEGACY ,而是要定义 CONFIG_SYS_CFI_FLASH_CONFIG_REGS

      第191行,#define CONFIG_SYS_MAX_FLASH_SECT (19) 改为 #define CONFIG_SYS_MAX_FLASH_SECT
(19),因为开发板使用的 NOR FLASH 的型号是 EN29LV160AB ,有35个扇区

      第212行,#define CONFIG_NAND_S3C2410 改为 #define CONFIG_NAND_S3C2440,因为开发板使用的 NOR FLASH 的型号是 EN29LV160AB ,有35个扇区

      第213行,#define CONFIG_SYS_S3C2410_NAND_HWECC 改为 #define CONFIG_SYS_S3C2440_NAND_HWECC

4.修改时钟频率.

      A.gedit /home/huwei/uboot/u-boot-2011.06/arch/arm/cpu/arm920t/start.s

          第164行,# if defined(CONFIG_S3C2410) 改为 # if defined(CONFIG_S3C2440)

          第165行,ldr r1, =0x3ff 改为 ldr r1, =7fff,因为 S3C2440 的 INTMASK 有15位,目的是屏蔽所有子中断

          第173行,mov r1, #3 改为 mov r1, #5,目的是目的是使 FCLK:HCLK:PCLK = 1:4:8      

      B.gedit /home/huwei/uboot/u-boot-2011.06/board/samsung/tq2440/tq2440.c

          第42至44行,改为:

              #define M_MDIV 92
              #define M_PDIV 1
              #define M_SDIV 1

          因为开发板输入晶振是12Mhz,通过修改,可以使 MPLL 的频率为 400MHz

          第54至56行,改为:

              #define U_M_MDIV 56

              #define U_M_PDIV 2
              #define U_M_SDIV 2

          通过修改,可以使 UPLL 的频率为 48MHz
5.修改 SDRAM 时序,gedit /home/huwei/uboot/u-boot-2011.06/board/samsung/tq2440/lowlevel_init.s

    第54至126行,改为:

        #define B1_BWSCON (DW16)
        #define B2_BWSCON (DW16)
        #define B3_BWSCON (DW16)
        #define B4_BWSCON (DW32)
        #define B5_BWSCON (DW16)
        #define B6_BWSCON (DW32)
        #define B7_BWSCON (DW32)

        /* BANK0CON */
        #define B0_Tacs 0x3
        #define B0_Tcos 0x3
        #define B0_Tacc 0x7
        #define B0_Tcoh 0x3
        #define B0_Tah 0x3
        #define B0_Tacp 0x1
        #define B0_PMC 0x0

        /* BANK1CON */
        #define B1_Tacs 0x1
        #define B1_Tcos 0x1
        #define B1_Tacc 0x6
        #define B1_Tcoh 0x1
        #define B1_Tah  0x1
        #define B1_Tacp 0x0
        #define B1_PMC  0x0
 

        #define B2_Tacs 0x1
        #define B2_Tcos 0x1
        #define B2_Tacc 0x6
        #define B2_Tcoh 0x1
        #define B2_Tah  0x1
        #define B2_Tacp 0x0
        #define B2_PMC  0x0


        #define B3_Tacs 0x1
        #define B3_Tcos 0x1
        #define B3_Tacc 0x6
        #define B3_Tcoh 0x1
        #define B3_Tah  0x1
        #define B3_Tacp 0x0
        #define B3_PMC  0x0


        #define B4_Tacs 0x1
        #define B4_Tcos 0x1
        #define B4_Tacc 0x6
        #define B4_Tcoh 0x1
        #define B4_Tah  0x1
        #define B4_Tacp 0x0
        #define B4_PMC  0x0


        #define B5_Tacs 0x1
        #define B5_Tcos 0x1
        #define B5_Tacc 0x6
        #define B5_Tcoh 0x1
        #define B5_Tah  0x1
        #define B5_Tacp 0x0
        #define B5_PMC  0x0


        #define B6_MT   0x3
        #define B6_Trcd 0x3
        #define B6_SCAN 0x1


        #define B7_MT   0x3
        #define B7_Trcd 0x1
        #define B7_SCAN 0x1

        /* REFRESH parameter */
        #define REFEN  0x1
        #define TREFMD 0x0
        #define Trp    0x1
        #define Trc    0x1
        #define Tchr   0x2
        #define REFCNT 1268

6.修改 NAND FLASH 相关内容,目的只是使编译通过,虽然至此还未使用到 NAND FLASH

    A.cp /home/huwei/uboot/u-boot-2011.06/drivers/mtd/nand/s3c2410_nand.c /home/huwei/uboot/u-boot-2011.06/drivers/mtd/nand/s3c2440_nand.c

    B.gedit /home/huwei/uboot/u-boot-2011.06/drivers/mtd/nand/s3c2440_nand.c

        将代码中所有的 S3C2410 改为 S3C2440

    C.gedit /home/huwei/uboot/u-boot-2011.06/drivers/mtd/nand/Makefile

        第48行,COBJS-$(CONFIG_NAND_S3C2410) += s3c2410_nand.o 改为 COBJS-$(CONFIG_NAND_S3C2440) += s3c2440_nand.o

    D.编译 u-boot 并烧到 NOR FLASH ,上电运行,打印如下信息:

        U-Boot 2011.06 (Jul 30 2011 - 23:40:44)
        DRAM: 64 MiB
        Flash: 2 MiB

        NAND: raise: Signal # 8 caught
        raise: Signal # 8 caught
        raise: Signal # 8 caught
        raise: Signal # 8 caught
        raise: Signal # 8 caught
        raise: Signal # 8 caught
        No NAND device found!!!
        0 MiB
        *** Warning - bad CRC, using defaultenvironment
        In: serial
        Out: serial
        Err: serial
        Net: CS8900-0
        TQ2440 #

    可以看到一些信息:DRAM 为 64MB ,NOR FLASH 为 2MB ,由于并未设置 NAND FLASH ,因此系统没有检测到 NAND FLASH ,此外,命令行提示符也已经改为 TQ2440 # ,如果使用命令 flinfo ,则更多 NOR FLASH 的信息会显示出来。

7.实现 NAND FLASH 的读写。

    在上一步中,只是把 /home/huwei/u-boot-2011.06/drivers/mtd/nand/s3c2410_nand.c 复制 为 s3c2440_nand.c 并将所有 s3c2410 都改为了 s3c2440 ,但这么做只是为了使编译通过,并没有实现对 NAND FLASH 的读写。S3C2410 的 NAND FLASH 控制器跟 S3C2440 的不太一样,因此 s3c2440_nand.c 并不能直接应用,需要修改相关寄存器。

    A.重新定义要用到的寄存器

        第27至37行,去掉之后改为如下内容:

            #define S3C2440_NFCONT_SECCL     (1<<6)
            #define S3C2440_NFCONT_MECCL     (1<<5)
            #define S3C2440_NFCONT_INITECC   (1<<4)
            #define S3C2440_NFCONT_nCE       (1<<1)
            #define S3C2440_NFCONT_MODE      (1<<0)
            #define S3C2440_NFCONF_TACLS(x)  ((x)<<12)
            #define S3C2440_NFCONF_TWRPH0(x) ((x)<<8)
            #define S3C2440_NFCONF_TWRPH1(x) ((x)<<4)
            #define S3C2440_ADDR_NALE 0x08
            #define S3C2440_ADDR_NCLE 0x0C

    B.修改 board_nand_init 和 s3c2440_hwcontrol 这2个函数,其他不变

        board_nand_init 函数主要是对 NAND FLASH 的初始化,修改它的 NANDCONF 和 NANDCONT 寄存器,下面红色字体表示修改的地方:

        int board_nand_init(struct nand_chip *nand)
        {
            u_int32_t cfg;
            u_int8_t tacls, twrph0, twrph1;
            struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
            struct s3c2440_nand *nand_reg = s3c2440_get_base_nand();

        

            debugX(1, "board_nand_init()\n");

            

            writel(readl(&clk_power->clkcon) | (1 << 4), &clk_power->clkcon);

            /* initialize hardware */
            #if defined(CONFIG_S3C24XX_CUSTOM_NAND_TIMING)
                tacls  = CONFIG_S3C24XX_TACLS;
                twrph0 = CONFIG_S3C24XX_TWRPH0;
                twrph1 =  CONFIG_S3C24XX_TWRPH1;
            #else
                tacls = 2;
                twrph0 = 5;
                twrph1 = 1;
            #endif

                 cfg  = S3C2440_NFCONF_TACLS(tacls - 1);
                 cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
                 cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
                 writel(cfg, &nand_reg->nfconf);

                 

                 cfg  = S3C2440_NFCONT_SECCL;
                 cfg |= S3C2440_NFCONT_MECCL;
                 cfg |= S3C2440_NFCONT_MODE;
                 cfg |= S3C2440_NFCONT_INITECC;
                 writel(cfg, &nand_reg->nfcont);    // 注意是 nfcont ,不是 conf

                 

                 /* initialize nand_chip data structure */
                 nand->IO_ADDR_R = (void *)&nand_reg->nfdata;
                 nand->IO_ADDR_W = (void *)&nand_reg->nfdata;

                 nand->select_chip = NULL;

                 

                 /* read_buf and write_buf are default */
                 /* read_byte and write_byte are default */
                #ifdef CONFIG_NAND_SPL
                    nand->read_buf = nand_read_buf;
                #endif

                 

                 /* hwcontrol always must be implemented */
                 nand->cmd_ctrl = s3c2440_hwcontrol;

                 

                 nand->dev_ready = s3c2440_dev_ready;

                 

            #ifdef CONFIG_S3C2440_NAND_HWECC
                nand->ecc.hwctl = s3c2440_nand_enable_hwecc;
                nand->ecc.calculate = s3c2440_nand_calculate_ecc;
                nand->ecc.correct = s3c2440_nand_correct_data;
                nand->ecc.mode = NAND_ECC_HW;
                nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
                nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
            #else
                nand->ecc.mode = NAND_ECC_SOFT;
            #endif

            

            #ifdef CONFIG_S3C2440_NAND_BBT
                nand->options = NAND_USE_FLASH_BBT;
            #else
                nand->options = 0;
            #endif

            

                debugX(1, "end of nand_init\n");

                return 0;
            }
        接着修改 s3c2440_hwcontrol 函数,它用于对 NAND FLASH 写地址和写命令操作
            static void s3c2440_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
            {
                struct nand_chip *chip = mtd->priv;
                struct s3c2440_nand *nand = s3c2440_get_base_nand();

                

                debugX(1, "hwcontrol(): 0x%02x 0x%02x\n", cmd, ctrl);

                

                if (ctrl & NAND_CTRL_CHANGE) {
                    ulong IO_ADDR_W = (ulong)nand;

                if (!(ctrl & NAND_CLE))
                    IO_ADDR_W |= S3C2440_ADDR_NCLE;
                if (!(ctrl & NAND_ALE))
                    IO_ADDR_W |= S3C2440_ADDR_NALE;

                if (cmd == NAND_CMD_NONE)
                    IO_ADDR_W  = &nand->nfdata;

                    chip->IO_ADDR_W = (void *)IO_ADDR_W;

                if (ctrl & NAND_NCE)
                    writel(readl(&nand->nfconf) & ~S3C2440_NFCONT_nCE,
                        &nand->nfconf);
                else
                    writel(readl(&nand->nfconf) | S3C2440_NFCONT_nCE,
                        &nand->nfconf);
            }

            if (cmd != NAND_CMD_NONE)
                writeb(cmd, chip->IO_ADDR_W);
        }

        这么做,除了修改寄存器的值以及设置写地址和写命令的 I/O 端口外,还增加了 if(cmd == NAND_CMD_NONE) 这个判断语句,不加会导致无法向 NAND FLASH 写数据,尽管系统不会提示任何错误并显示 “OK” ,这么做是因为写完地址和命令后,一定还要把 I/O 端口地址重新设置为寄存器 NFDATA 。

        由于系统没有定义 CONFIG_S3C2410_NAND_HWECC ,因 此 我 们 暂 时 先 不 对 s3c2440_nand_enable_hwecc 函 数 、s3c2440_nand_calculate_ecc 函数和 s3c2440_nand_correct_data 函数进行修改。

    编译后烧写 u-boot.bin 到 NOR FLASH ,打印信息如下:

    U-Boot2011.06 (Aug 10 2011 - 23:16:25)
    DRAM: 64 MiB
    Flash: 2MiB
    NAND: 256 MiB
    ***Warning - bad CRC, using default environment
    In:  serial
    Out: serial
    Err: serial
    Net: CS8900-0
    TQ2440# nand info
    Device 0:nand0, sector size 128 KiB
    TQ2440# nand device 0

    TQ2440# nand erase 0x100000 0x300000
    NANDerase: device 0 offset 0x100000, size 0x300000
    Erasingat 0x3e0000 -- 100% complete.
    OK
    TQ2440# nand write 0 0x100000 0x300000
    NANDwrite: device 0 offset 0x100000, size 0x300000
    3145728 bytes written: OK
    TQ2440# nand read 0x30004000 0x100000 0x300000
    NANDread: device 0 offset 0x100000, size 0x300000
    3145728 bytes read: OK
    TQ2440# md.b 0
    00000000:13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5        ................
    00000010:14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5        ................
    00000020:e0 01 00 00 40 02 00 00 a0 02 00 00 00 03 00 00        ....@...........
    00000030:60 03 00 00 c0 03 00 00 20 04 00 00 ef be ad de        ....... .......
    TQ2440# md.b 0x30004000
    30004000:13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5        ................
    30004010:14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5        ................
    30004020:e0 01 00 00 40 02 00 00 a0 02 00 00 00 03 00 00        ....@...........
    30004030:60 03 00 00 c0 03 00 00 20 04 00 00 ef be ad de `      ....... .......
    上电之后显示 NAND FLASH 容量是256MB,说明检测到了 NAND FLASH ,然后将 SDRAM 中的内容写入 NAND FLASH,再从 NAND FLASH 中读出这些数据,通过比较可看出写入和读出的数据一致,因此判断对 NAND FLASH 的读写操作是正确的。

    如果打印出 NAND FLASH 更加详细的信息,可以修改 /home/huwei/uboot/u-boot-2011.06/drivers/mtd/nand/nand_base.c 的第2676行,把 MTDDEBUG 改成 printf ,并且去掉 MTD_DEBUG LEVEL0,编译烧写,重新上电后会有如下打印信息:

    U-Boot2011.06 (Aug 10 2011 - 23:55:48)
    DRAM: 64 MiB
    Flash: 2MiB
    NAND: NAND device: ManufacturerID: 0xec, Chip ID: 0xda (Samsung NAND 256MiB 3,3V 8-bit)
    256 MiB
    ***Warning - bad CRC, using default environment
    In:  serial
    Out: serial
    Err: serial
    Net: CS8900-0
    TQ2440 #