CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 6960|回复: 0

u-boot的nand驱动写过程分析

[复制链接]
发表于 2013-7-16 15:51:43 | 显示全部楼层 |阅读模式
原文:http://blog.csdn.net/andy_wsj/article/details/9343779

从命令说起,在u-boot输入下列命令:
nand write 40008000 0 20000
命令的意思是将内存0x40008000开始的部分写入nand,从nand地址0开始写,写入长度是0x200000
回车之后,代码如何运行呢?命令的输入,执行之前都已经分析过了,执行这条命令,将调用
\u-boot-sunxi-sunxi\common\cmd_nand.c内的函数do_nand。
int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])

nand write 40008000 0 20000在参数argv中,而且
argv[0] = "nand"
argv[1] = "write"
argv[2] = "40008000"
argv[3] = "0"
argv[4] = "20000"
argc = 5 参数的个数

分析一下do_nand函数的片段,篇幅关系,只保留写操作部分:
nt do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
{
        int i, ret = 0;
        ulong addr;
        loff_t off, size;
        char *cmd, *s;
        nand_info_t *nand;
#ifdef CONFIG_SYS_NAND_QUIET
        int quiet = CONFIG_SYS_NAND_QUIET;
#else
        int quiet = 0;
#endif
        const char *quiet_str = getenv("quiet");
        int dev = nand_curr_device;                 //当前NAND芯片,如果板上有多个芯片,则不能直接赋值,大部分板子都是一个NAND
        int repeat = flag & CMD_FLAG_REPEAT;

        /* at least two arguments please */
        if (argc < 2)
                goto usage;

        if (quiet_str)
                quiet = simple_strtoul(quiet_str, NULL, 0) != 0;

        cmd = argv[1];   //cmd就指向命令“write”,

   ........判断是什么命令,多余判断删除了..............

        /* The following commands operate on the current device, unless
         * overridden by a partition specifier.  Note that if somehow the
         * current device is invalid, it will have to be changed to a valid
         * one before these commands can run, even if a partition specifier
         * for another device is to be used.
         */
        if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||  //判断芯片是否存在或是否定义
            !nand_info[dev].name) {
                puts("\nno devices available\n");
                return 1;
        }
        nand = &nand_info[dev];   //获取定义的nand芯片信息

  ................

        if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {  //nand读写操作
                size_t rwsize;
                ulong pagecount = 1;
                int read;
                int raw;

                if (argc < 4)  
                        goto usage;

                addr = (ulong)simple_strtoul(argv[2], NULL, 16);  //将argv[2] = "40008000"转换成16进制,0x40008000

                read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */  //判断读写操作类型
                printf("\nNAND %s: ", read ? "read" : "write");

                nand = &nand_info[dev];

                s = strchr(cmd, '.');   //看看是否带有扩展命令,如write.raw, write.jffs2等等,输入是“write”,结果s = NULL;

                if (s && !strcmp(s, ".raw")) {
               ......省略.....
               
                } else {  //执行这里,计算地址偏移量,长度
                        if (arg_off_size(argc - 3, argv + 3, &dev,
                                                &off, &size) != 0)
                                return 1;

                        rwsize = size;
                }


=======论坛帖子长度限制,以下内容请看原文==========


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-11-23 01:27 , Processed in 0.019651 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部