From 3d7283b7126783523fa5cccc1ece8a297f62abf9 Mon Sep 17 00:00:00 2001 From: Xiangfu Liu Date: Sun, 28 Jun 2009 15:35:20 +0800 Subject: [PATCH] readnand is not work. will fix this next commit Signed-off-by: Xiangfu Liu --- inflash/src/Makefile.am | 5 +- inflash/src/cmd.c | 165 +++++++++++++++++++++++++++++---- inflash/src/cmd.h | 1 + inflash/src/command_line.c | 81 ++++++++-------- inflash/src/ingenic_usb.c | 76 ++++++++++----- inflash/src/usb_boot_defines.h | 8 +- 6 files changed, 249 insertions(+), 87 deletions(-) diff --git a/inflash/src/Makefile.am b/inflash/src/Makefile.am index e4d8bc1..81f5960 100644 --- a/inflash/src/Makefile.am +++ b/inflash/src/Makefile.am @@ -9,10 +9,11 @@ BUILT_SOURCES = inflash_version.h bin_PROGRAMS = inflash inflash_SOURCES = cmd.c command_line.c ingenic_cfg.c \ ingenic_usb.c main.c -prefix = /usr +prefix = /usr datadir = /usr/share/inflash -data_DATA = ../xburst_stage1/xburst_stage1.bin ../xburst_stage2/xburst_stage2.bin \ +data_DATA = ../xburst_stage1/xburst_stage1.bin \ + ../xburst_stage2/xburst_stage2.bin \ inflash.cfg EXTRA_DIST = $(datadir) diff --git a/inflash/src/cmd.c b/inflash/src/cmd.c index 3ac2740..2bc8f2b 100644 --- a/inflash/src/cmd.c +++ b/inflash/src/cmd.c @@ -27,9 +27,10 @@ extern char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH]; struct ingenic_dev ingenic_dev; struct hand hand; - +struct sdram_in sdram_in; struct nand_in nand_in; static struct nand_out nand_out; + unsigned int total_size; unsigned char code_buf[4 * 512 * 1024]; unsigned char check_buf[4 * 512 * 1024]; @@ -137,7 +138,7 @@ int boot(char *stage1_path, char *stage2_path){ } if (status) { - printf(" Already booted."); + printf(" Already booted.\n"); return 1; } else { printf(" CPU not yet booted, now booting...\n"); @@ -174,7 +175,7 @@ int error_check(unsigned char *org,unsigned char * obj,unsigned int size) for (i = 0; i < size; i++) { if (org[i] != obj[i]) { unsigned int s = (i < 8) ? i : i - 8; // start_dump - printf("FAIL at off %d, wrote %x, read %x\n", i, org[i], obj[i]); + printf("FAIL at off %d, wrote 0x%x, read 0x%x\n", i, org[i], obj[i]); printf(" off %d write: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", s, org[s], org[s+1], org[s+2], org[s+3], org[s+4], org[s+5], org[s+6], org[s+7], org[s+8], org[s+9], org[s+10], org[s+11], org[s+12], org[s+13], org[s+14], org[s+15]); @@ -586,9 +587,8 @@ int nand_query(void) unsigned char csn; if (com_argc < 3) { - printf(" Usage:\n"); - printf(" nquery (1) (2) "); - printf(" (1):device index number\n" + printf(" Usage: nquery (1) (2)\n" + " (1):device index number\n" " (2):flash index number\n"); return -1; } @@ -633,14 +633,15 @@ int nand_read(int mode) unsigned int start_addr, length, page_num; unsigned char csn; unsigned short temp = 0; + unsigned ram_addr = 0; if (com_argc < 5) { - printf(" Usage:\n"); - printf(" nread (1) (2) (3) (4) "); - printf(" 1:start page number\n" + printf(" Usage: nread (1) (2) (3) (4)\n" + " 1:start page number\n" " 2:length in byte\n" " 3:device index number\n" - " 4:flash index number \n"); + " 4:flash index number\n" + " 5:start SDRAM address\n"); return -1; } init_nand_in(); @@ -654,6 +655,10 @@ int nand_read(int mode) nand_in.length= atoi(com_argv[2]); nand_in.dev = atoi(com_argv[3]); + if (com_argc = 6) { + ram_addr = strtoul(com_argv[5], NULL, 0); + printf("==%s==", com_argv[5]); + } start_addr = nand_in.start; length = nand_in.length; @@ -674,9 +679,6 @@ int nand_read(int mode) page_num = length / hand.nand_ps +1; - usb_send_data_address_to_ingenic(&ingenic_dev, start_addr); - usb_send_data_length_to_ingenic(&ingenic_dev, page_num); - switch(mode) { case NAND_READ: temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + NAND_READ; @@ -685,22 +687,33 @@ int nand_read(int mode) temp = ((csn<<4) & 0xff0) + NAND_READ_OOB; break; case NAND_READ_RAW: - temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + NAND_READ_RAW; + temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + + NAND_READ_RAW; + break; + case NAND_READ_TO_RAM: + temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + + NAND_READ_TO_RAM; + printf(" Reading nand to RAM: 0x%x\n", ram_addr); + usb_ingenic_start(&ingenic_dev, VR_PROGRAM_START1, ram_addr); break; default: printf(" unknow mode!\n"); return -1; } + usb_send_data_address_to_ingenic(&ingenic_dev, start_addr); + usb_send_data_length_to_ingenic(&ingenic_dev, page_num); + usb_ingenic_nand_ops(&ingenic_dev, temp); usb_read_data_from_ingenic(&ingenic_dev, nand_in.buf, page_num * hand.nand_ps); - for (j=0;jlength > (unsigned int) MAX_LOAD_SIZE) { + printf(" Image length too long!\n"); + return -1; + } + + ingenic_dev.file_buff = sdram_in->buf; + ingenic_dev.file_len = sdram_in->length; + usb_send_data_to_ingenic(&ingenic_dev); + usb_send_data_address_to_ingenic(&ingenic_dev, sdram_in->start); + usb_send_data_length_to_ingenic(&ingenic_dev, sdram_in->length); + usb_ingenic_sdram_ops(&ingenic_dev, sdram_in); + + usb_read_data_from_ingenic(&ingenic_dev, ret, 8); + printf(" Load last address at 0x%x\n", + ((ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0))); + + return 1; +} + +int sdram_load_file(struct sdram_in *sdram_in, char *file_path) +{ + struct stat fstat; + unsigned int flen,m,j,offset,k; + int fd, status, res = -1; + + status = stat(file_path, &fstat); + if (status < 0) { + fprintf(stderr, "Error - can't get file size from '%s': %s\n", + file_path, strerror(errno)); + goto out; + } + flen = fstat.st_size; + + fd = open(file_path, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Error - can't open file '%s': %s\n", + file_path, strerror(errno)); + goto out; + } + + m = flen / MAX_LOAD_SIZE; + j = flen % MAX_LOAD_SIZE; + offset = 0; + + printf(" Total size to send in byte is :%d\n", flen); + printf(" Loading data to SDRAM :\n"); + + for (k = 0; k < m; k++) { + status = read(fd, sdram_in->buf, MAX_LOAD_SIZE); + if (status < MAX_LOAD_SIZE) { + fprintf(stderr, "Error - can't read file '%s': %s\n", + file_path, strerror(errno)); + goto close; + } + + sdram_in->length = MAX_LOAD_SIZE; + if (sdram_load(sdram_in) < 1) + goto close; + + sdram_in->start += MAX_LOAD_SIZE; + if ( k % 60 == 0) + printf(" 0x%x \n", sdram_in->start); + } + + if (j) { + if (j % 4 !=0) + j += 4 - (j % 4); + status = read(fd, sdram_in->buf, j); + if (status < j) { + fprintf(stderr, "Error - can't read file '%s': %s\n", + file_path, strerror(errno)); + goto close; + } + + sdram_in->length = j; + if (sdram_load(sdram_in) < 1) + goto close; + } + + res = 1; + +close: + close(fd); +out: + return res; +} diff --git a/inflash/src/cmd.h b/inflash/src/cmd.h index 69f5da9..ed64873 100644 --- a/inflash/src/cmd.h +++ b/inflash/src/cmd.h @@ -23,5 +23,6 @@ int nand_query(void); int nand_erase(struct nand_in *nand_in); int debug_memory(int obj, unsigned int start, unsigned int size); int debug_gpio(int obj, unsigned char ops, unsigned char pin); +int debug_go(void); #endif /* __CMD_H__ */ diff --git a/inflash/src/command_line.c b/inflash/src/command_line.c index bd80c43..d3f32f8 100644 --- a/inflash/src/command_line.c +++ b/inflash/src/command_line.c @@ -16,10 +16,11 @@ #include "inflash_version.h" extern struct nand_in nand_in; +extern struct sdram_in sdram_in; +extern unsigned char code_buf[4 * 512 * 1024]; + int com_argc; char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH]; -const char HEX_NUM[17]={'0','1','2','3','4','5','6','7','8','9', - 'a','b','c','d','e','f',' '}; static const char COMMAND[][COMMAND_NUM]= { @@ -83,23 +84,6 @@ static int handle_help(void) return 1; } -unsigned int hex2dec(char *s) -{ - int i,L=(int)strlen(s),j; - unsigned int temp=0; - if (L>8) L=8; - for (i = 0; i < L; i++) { - for (j = 0; j < 16; j++) - if (s[i] == HEX_NUM[j]) - break; - - if (j == 16) - return 0; - temp = temp * 16 + j; - } - return temp; -} - static int handle_version(void) { printf(" USB Boot Software current version: %s\n", INFLASH_VERSION); @@ -110,9 +94,8 @@ static int handle_version(void) int handle_nerase(void) { if (com_argc < 5) { - printf(" Usage:\n"); - printf(" nerase (1) (2) (3) (4) "); - printf(" 1:start block number\n" + printf(" Usage: nerase (1) (2) (3) (4)\n" + " 1:start block number\n" " 2:block length\n" " 3:device index number\n" " 4:flash chip index number\n"); @@ -138,11 +121,9 @@ int handle_nerase(void) int handle_nmark(void) { - int i; if (com_argc < 4) { - printf(" Usage:\n"); - printf(" nerase (1) (2) (3) "); - printf(" 1:bad block number\n" + printf(" Usage: nerase (1) (2) (3)\n" + " 1:bad block number\n" " 2:device index number\n" " 3:flash chip index number\n"); return -1; @@ -167,22 +148,16 @@ int handle_memtest(void) unsigned int start, size; if (com_argc != 2 && com_argc != 4) { - printf(" Usage:\n"); - printf(" memtest (1) [2] [3] "); - printf(" 1:device index number\n" + printf(" Usage: memtest (1) [2] [3]\n" + " 1:device index number\n" " 2:SDRAM start address\n" " 3:test size\n"); return -1; } if (com_argc == 4) { - if (com_argv[2][0]=='0'&&com_argv[2][1]=='x') - start=hex2dec(&com_argv[2][2]); - else start=atol(com_argv[2]); - - if (com_argv[3][0]=='0'&&com_argv[3][1]=='x') - size = hex2dec(&com_argv[3][2]); - else size = atol(com_argv[3]); + start = strtoul(com_argv[2], NULL, 0); + size = strtoul(com_argv[3], NULL, 0); } else { start = 0; size = 0; @@ -194,8 +169,8 @@ int handle_memtest(void) int handle_gpio(int mode) { if (com_argc < 3) { - printf(" Usage:\n" - " gpios (1) (2) " + printf(" Usage:" + " gpios (1) (2)\n" " 1:GPIO pin number\n" " 2:device index number\n"); return -1; @@ -205,6 +180,27 @@ int handle_gpio(int mode) return 1; } +int handle_load(void) +{ + if (com_argc<4) { + printf(" Usage:" + " load (1) (2) (3) \n" + " 1:SDRAM start address\n" + " 2:image file name\n" + " 3:device index number\n"); + + return -1; + } + + sdram_in.start=strtoul(com_argv[1], NULL, 0); + printf(" start:::::: 0x%x\n", sdram_in.start); + + sdram_in.dev = atoi(com_argv[3]); + sdram_in.buf = code_buf; + sdram_load_file(&sdram_in, com_argv[2]); + return 1; +} + int command_interpret(char * com_buf) { char *buf = com_buf; @@ -273,10 +269,16 @@ int command_handle(char *buf) case 13: handle_version(); break; + case 14: + debug_go(); + break; case 16: /* exit */ printf(" exiting inflash software\n"); return -1; /* return -1 to break the main.c while * then run usb_ingenic_cleanup*/ + /*case 17: + nand_read(NAND_READ_TO_RAM); */ + break; case 18: handle_gpio(2); break; @@ -289,6 +291,9 @@ int command_handle(char *buf) case 26: handle_nmark(); break; + case 28: + handle_load(); + break; case 29: handle_memtest(); break; diff --git a/inflash/src/ingenic_usb.c b/inflash/src/ingenic_usb.c index 9cca814..e94ebc6 100644 --- a/inflash/src/ingenic_usb.c +++ b/inflash/src/ingenic_usb.c @@ -8,11 +8,11 @@ * 3 of the License, or (at your option) any later version. */ -#include "ingenic_usb.h" -#include "usb_boot_defines.h" #include #include #include +#include "usb_boot_defines.h" +#include "ingenic_usb.h" extern unsigned int total_size; @@ -155,8 +155,7 @@ int usb_get_ingenic_cpu(struct ingenic_dev *ingenic_dev) } ingenic_dev->cpu_info_buff[8] = '\0'; - printf(" CPU data: %s\n", - ingenic_dev->cpu_info_buff); + /* printf(" CPU data: %s\n", ingenic_dev->cpu_info_buff); */ if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4740V1")) return 1; if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4750V1")) return 2; @@ -267,31 +266,14 @@ int usb_read_data_from_ingenic(struct ingenic_dev *ingenic_dev, return 1; } -int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage) +int usb_ingenic_start(struct ingenic_dev *ingenic_dev, int rqst, int stage_addr) { int status; - unsigned int stage2_addr; - stage2_addr = total_size + 0x80000000; - stage2_addr -= CODE_SIZE; - - int stage_addr = (stage == 1 ? 0x80002000 : stage2_addr); - - usb_send_data_address_to_ingenic(ingenic_dev, stage_addr); - printf(" Download stage %d program and execute at 0x%08x\n", - stage, (stage_addr)); - usb_send_data_to_ingenic(ingenic_dev); - - if (stage == 2) { - if (usb_get_ingenic_cpu(ingenic_dev) < 1) - return -1; - usb_ingenic_flush_cache(ingenic_dev); - } - /* tell the device to start the uploaded device */ status = usb_control_msg(ingenic_dev->usb_handle, /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, - /* bRequest */ (stage == 1 ? VR_PROGRAM_START1 : VR_PROGRAM_START2), + /* bRequest */ rqst, /* wValue */ STAGE_ADDR_MSB(stage_addr), /* wIndex */ STAGE_ADDR_LSB(stage_addr), /* Data */ 0, @@ -303,7 +285,34 @@ int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage) "on the Ingenic device: %i\n", status); return status; } + return 1; +} +int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage) +{ + int status; + + unsigned int stage2_addr; + stage2_addr = total_size + 0x80000000; + stage2_addr -= CODE_SIZE; + + int stage_addr = (stage == 1 ? 0x80002000 : stage2_addr); + int rqst = VR_PROGRAM_START1; + + usb_send_data_address_to_ingenic(ingenic_dev, stage_addr); + printf(" Download stage %d program and execute at 0x%08x\n", + stage, (stage_addr)); + usb_send_data_to_ingenic(ingenic_dev); + + if (stage == 2) { + if (usb_get_ingenic_cpu(ingenic_dev) < 1) + return -1; + usb_ingenic_flush_cache(ingenic_dev); + rqst = VR_PROGRAM_START2; + } + + if (usb_ingenic_start(ingenic_dev, rqst, stage_addr) < 1) + return -1; if (usb_get_ingenic_cpu(ingenic_dev) < 1) return -1; @@ -361,3 +370,24 @@ int usb_ingenic_configration(struct ingenic_dev *ingenic_dev, int ops) return 1; } + +int usb_ingenic_sdram_ops(struct ingenic_dev *ingenic_dev, int ops) +{ + int status; + status = usb_control_msg(ingenic_dev->usb_handle, + /* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + /* bRequest */ VR_SDRAM_OPS, + /* wValue */ ops, + /* wIndex */ 0, + /* Data */ 0, + /* wLength */ 0, + USB_TIMEOUT); + + if (status != 0) { + fprintf(stderr, "Error - " + "Device can't load file to sdram: %i\n", status); + return -1; + } + + return 1; +} diff --git a/inflash/src/usb_boot_defines.h b/inflash/src/usb_boot_defines.h index 2bd093c..c4dc779 100644 --- a/inflash/src/usb_boot_defines.h +++ b/inflash/src/usb_boot_defines.h @@ -10,7 +10,6 @@ #ifndef __USB_BOOT_DEFINES_H__ #define __USB_BOOT_DEFINES_H__ -/* #define dprintf(x...) printf(x) */ #define SDRAM_SIZE ( 16 * 1024 * 1024 ) #define CODE_SIZE ( 4 * 1024 * 1024 ) /* #define START_ADDR ( 0x80000000 + SDRAM_SIZE - CODE_SIZE ) */ @@ -19,10 +18,8 @@ #define NAND_MAX_PAGE_NUM 1073740824 /*Hand.nand_pn */ #define NAND_SECTION_NUM 23 #define MAX_TRANSFER_SIZE 0x100000 -#define MAX_LOAD_SIZE 0x3000 -#define NAND_MAX_BYTE_NUM (Hand.nand_pn * Hand.nand_ps) -#define IOCTL_INBUF_SIZE 512 -#define IOCTL_OUTBUF_SIZE 512 +#define MAX_LOAD_SIZE 0x3000 +#define NAND_MAX_BYTE_NUM (hand.nand_pn * hand.nand_ps) #define MAX_DEV_NUM 16 enum CPUTYPE { @@ -152,3 +149,4 @@ struct sdram_in { }; #endif /* __USB_BOOT_DEFINES_H__ */ +