u-boot下spi norflash驱动大杂绘
发布时间:2020-12-15 17:38:16 所属栏目:百科 来源:网络整理
导读:一、总述 http://blackfin.uclinux.org/doku.php?id=bootloaders:u-boot:serial-flash 在一般的情况是我们会把bootloader存放到我们的spiflash中去,现在就有一些的CPU可以支持从spiflash中启动,这时我们在uboot要完成针对你的spiflash的驱动,下面我就
一、总述http://blackfin.uclinux.org/doku.php?id=bootloaders:u-boot:serial-flash 在一般的情况是我们会把bootloader存放到我们的spiflash中去,现在就有一些的CPU可以支持从spiflash中启动,这时我们在uboot要完成针对你的spiflash的驱动,下面我就个人在最近的工作中的一些心得总结一下。 二、uboot下spi flash命令
1)probe
uboot> sf probe 2 SF: Got idcode 20 20 15 2048 KiB M25P16 at 0:2 is now current device上面的命令就是去probe指定bus下的spi flash设备,有了这个同时probe成功,才可以去write/read你的spilfahs.
2)read
uboot> sf read 0x2000000 0x300 0x1000 uboot> md.b 0x2000000 02000000: 4a e1 c0 ff 10 62 0a e1 04 0a 40 e1 c2 ff 10 93 J....b....@..... 02000010: 22 6c 10 93 48 60 c2 6f 10 97 4a e1 e0 ff 20 e1 "l..H`.o..J... . 02000020: fd 01 0a e1 04 20 88 4f 10 93 c6 6c 27 01 30 05 ..... .O...l'.0. 02000030: 10 00 00 00 4a e1 e0 ff 40 e1 fa 03 0a e1 0c 20 ....J...@......3)erase uboot> sf erase 0x30000 0x100004)write uboot> sf write 0x20000000 0x0 0x100000 5)同样可以设备env来引导kernel,如下 uboot> set sfboot 'sf probe 2; sf read 0x2000000 0x300 0x100000; bootm 0x2000000' uboot> set bootcmd run sfboot uboot> save通过上面这个,我们就可以引导一个存在spiflash中的kernel 三、代码分析我们当执行sf命令时,执行的函数如下:
common/cmd_sf.c
490 static int do_spi_flash(cmd_tbl_t *cmdtp,int flag,int argc,491 char * const argv[]) 492 { 493 const char *cmd; 494 int ret; 495 496 /* need at least two arguments */ 497 if (argc < 2)//如果参数小于2测,显示使作帮助 498 goto usage; 499 500 cmd = argv[1]; 501 --argc; 502 ++argv; 503 504 if (strcmp(cmd,"probe") == 0) {//执行probe时的分支 505 ret = do_spi_flash_probe(argc,argv); 506 goto done; 507 } 508 509 /* The remaining commands require a selected device */ 510 if (!flash) { 511 puts("No SPI flash selected. Please run `sf probe'n"); 512 return 1; 513 } 514 515 if (strcmp(cmd,"read") == 0 || strcmp(cmd,"write") == 0 || //read/write的分枝 516 strcmp(cmd,"update") == 0) 517 ret = do_spi_flash_read_write(argc,argv); 518 else if (strcmp(cmd,"erase") == 0) 519 ret = do_spi_flash_erase(argc,argv); 520 #ifdef CONFIG_CMD_SF_TEST 521 else if (!strcmp(cmd,"test")) 522 ret = do_spi_flash_test(argc,argv); 523 #endif 524 else 525 ret = -1; 526 527 done: 528 if (ret != -1) 529 return ret; 530 531 usage: 532 return CMD_RET_USAGE;} 89 static int do_spi_flash_probe(int argc,char * const argv[])
90 {
91 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
92 unsigned int cs = CONFIG_SF_DEFAULT_CS;
93 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
94 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
95 char *endp;
96 struct spi_flash *new;
97
98 if (argc >= 2) {
99 cs = simple_strtoul(argv[1],&endp,0);
100 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
101 return -1;
102 if (*endp == ':') {
103 if (endp[1] == 0)
104 return -1;
105
106 bus = cs;
107 cs = simple_strtoul(endp + 1,0);
108 if (*endp != 0)
109 return -1;
110 }
111 }
112
113 if (argc >= 3) {
114 speed = simple_strtoul(argv[2],0);
115 if (*argv[2] == 0 || *endp != 0)
116 return -1;
117 }
118 if (argc >= 4) {
119 mode = simple_strtoul(argv[3],16);
120 if (*argv[3] == 0 || *endp != 0)
121 return -1;
122 }
123
124 new = spi_flash_probe(bus,cs,speed,mode);//根据外面传入的but/CS来probe你的spi flash
125 if (!new) {
126 printf("Failed to initialize SPI flash at %u:%un",bus,cs);
127 return 1;
128 }
129
130 if (flash)
131 spi_flash_free(flash);
132 flash = new;
133
134 return 0;
135 }
.....................................................待续
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |