2009-04-28 18:57:53 +03:00
|
|
|
/*
|
|
|
|
* "Ingenic flash tool" - flash the Ingenic CPU via USB
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009
|
|
|
|
* Author: Marek Lindner <lindner_marek@yahoo.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* version 3 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2009-04-28 20:07:43 +03:00
|
|
|
#include "ingenic_usb.h"
|
2009-04-28 18:57:53 +03:00
|
|
|
#include "usb_boot_defines.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <confuse.h>
|
2009-04-29 16:26:28 +03:00
|
|
|
#include <ctype.h>
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-04-29 16:26:28 +03:00
|
|
|
static struct nand_in_t nand_in;
|
|
|
|
static struct nand_out_t nand_out;
|
|
|
|
static struct fw_args_t fw_args;
|
2009-04-28 18:57:53 +03:00
|
|
|
unsigned int total_size;
|
|
|
|
|
|
|
|
static int parse_configure(char * file_path)
|
|
|
|
{
|
|
|
|
cfg_opt_t opts[] = {
|
|
|
|
CFG_SIMPLE_INT("EXTCLK", &fw_args.ext_clk),
|
|
|
|
CFG_SIMPLE_INT("CPUSPEED", &fw_args.cpu_speed),
|
|
|
|
CFG_SIMPLE_INT("PHMDIV", &fw_args.phm_div),
|
|
|
|
CFG_SIMPLE_INT("BOUDRATE", &fw_args.boudrate),
|
|
|
|
CFG_SIMPLE_INT("USEUART", &fw_args.use_uart),
|
|
|
|
|
|
|
|
CFG_SIMPLE_INT("BUSWIDTH", &fw_args.bus_width),
|
|
|
|
CFG_SIMPLE_INT("BANKS", &fw_args.bank_num),
|
|
|
|
CFG_SIMPLE_INT("ROWADDR", &fw_args.row_addr),
|
|
|
|
CFG_SIMPLE_INT("COLADDR", &fw_args.col_addr),
|
|
|
|
|
|
|
|
CFG_SIMPLE_INT("ISMOBILE", &fw_args.is_mobile),
|
|
|
|
CFG_SIMPLE_INT("ISBUSSHARE", &fw_args.is_busshare),
|
|
|
|
CFG_SIMPLE_INT("DEBUGOPS", &fw_args.debug_ops),
|
|
|
|
CFG_SIMPLE_INT("PINNUM", &fw_args.pin_num),
|
|
|
|
CFG_SIMPLE_INT("START", &fw_args.start),
|
|
|
|
CFG_SIMPLE_INT("SIZE", &fw_args.size),
|
|
|
|
|
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
cfg_t *cfg;
|
|
|
|
cfg = cfg_init(opts, 0);
|
|
|
|
if (cfg_parse(cfg, file_path) == CFG_PARSE_ERROR)
|
|
|
|
return -1;
|
|
|
|
cfg_free(cfg);
|
|
|
|
|
|
|
|
fw_args.cpu_id = 0x4740;
|
|
|
|
if (fw_args.bus_width == 32)
|
|
|
|
fw_args.bus_width = 0 ;
|
|
|
|
else
|
|
|
|
fw_args.bus_width = 1 ;
|
|
|
|
fw_args.bank_num = fw_args.bank_num / 4;
|
|
|
|
fw_args.cpu_speed = fw_args.cpu_speed / fw_args.ext_clk;
|
|
|
|
|
|
|
|
total_size = (unsigned int)
|
|
|
|
(2 << (fw_args.row_addr + fw_args.col_addr - 1)) * 2
|
|
|
|
* (fw_args.bank_num + 1) * 2
|
|
|
|
* (2 - fw_args.bus_width);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_dump_cfg()
|
|
|
|
{
|
|
|
|
printf("\n Now checking whether all configure args valid: ");
|
|
|
|
/* check PLL */
|
|
|
|
if (fw_args.ext_clk > 27 || fw_args.ext_clk < 12 ) {
|
|
|
|
printf("\n EXTCLK setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fw_args.phm_div > 32 || fw_args.ext_clk < 2 ) {
|
|
|
|
printf("\n PHMDIV setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( (fw_args.cpu_speed * fw_args.ext_clk ) % 12 != 0 ) {
|
|
|
|
printf("\n CPUSPEED setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check SDRAM */
|
|
|
|
if (fw_args.bus_width > 1 ) {
|
|
|
|
printf("\n SDRAMWIDTH setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fw_args.bank_num > 1 ) {
|
|
|
|
printf("\n BANKNUM setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fw_args.row_addr > 13 && fw_args.row_addr < 11 ) {
|
|
|
|
printf("\n ROWADDR setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fw_args.col_addr > 13 && fw_args.col_addr < 11 ) {
|
|
|
|
printf("\n COLADDR setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check NAND */
|
|
|
|
/* if ( Hand.nand_ps < 2048 && Hand.nand_os > 16 )
|
|
|
|
{
|
|
|
|
printf("\n PAGESIZE or OOBSIZE setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( Hand.nand_ps < 2048 && Hand.nand_ppb > 32 )
|
|
|
|
{
|
|
|
|
printf("\n PAGESIZE or PAGEPERBLOCK setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Hand.nand_ps > 512 && Hand.nand_os <= 16 )
|
|
|
|
{
|
|
|
|
printf("\n PAGESIZE or OOBSIZE setting invalid!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( Hand.nand_ps > 512 && Hand.nand_ppb < 64 )
|
|
|
|
{
|
|
|
|
printf("\n PAGESIZE or PAGEPERBLOCK setting invalid!");
|
|
|
|
return 0;
|
|
|
|
} */
|
|
|
|
|
|
|
|
printf("\n Current device information:");
|
|
|
|
printf(" CPU is Jz%x",fw_args.cpu_id);
|
|
|
|
printf("\n Crystal work at %dMHz, the CCLK up to %dMHz and PMH_CLK up to %dMHz",
|
|
|
|
fw_args.ext_clk,
|
|
|
|
(unsigned int)fw_args.cpu_speed * fw_args.ext_clk,
|
|
|
|
((unsigned int)fw_args.cpu_speed * fw_args.ext_clk) / fw_args.phm_div);
|
|
|
|
|
|
|
|
printf("\n Total SDRAM size is %d MB, work in %d bank and %d bit mode",
|
|
|
|
total_size / 0x100000, 2 * (fw_args.bank_num + 1), 16 * (2 - fw_args.bus_width));
|
|
|
|
|
|
|
|
/* printf("\n Nand page size %d, ECC offset %d, ",
|
|
|
|
Hand.nand_ps,Hand.nand_eccpos);
|
|
|
|
|
|
|
|
printf("bad block ID %d, ",Hand.nand_bbpage);
|
|
|
|
|
|
|
|
printf("use %d plane mode",Hand.nand_plane); */
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_file(struct ingenic_dev *ingenic_dev, const char *file_path)
|
|
|
|
{
|
|
|
|
struct stat fstat;
|
|
|
|
int fd, status, res = -1;
|
|
|
|
|
|
|
|
if (ingenic_dev->file_buff)
|
|
|
|
free(ingenic_dev->file_buff);
|
|
|
|
|
|
|
|
ingenic_dev->file_buff = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ingenic_dev->file_len = fstat.st_size;
|
|
|
|
ingenic_dev->file_buff = malloc(ingenic_dev->file_len);
|
|
|
|
|
|
|
|
if (!ingenic_dev->file_buff) {
|
|
|
|
fprintf(stderr, "Error - can't allocate memory to read file '%s': %s\n", file_path, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = read(fd, ingenic_dev->file_buff, ingenic_dev->file_len);
|
|
|
|
|
|
|
|
if (status < ingenic_dev->file_len) {
|
|
|
|
fprintf(stderr, "Error - can't read file '%s': %s\n", file_path, strerror(errno));
|
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
2009-04-29 10:25:12 +03:00
|
|
|
memcpy(ingenic_dev->file_buff + 8, &fw_args, sizeof(struct fw_args_t));
|
2009-04-28 18:57:53 +03:00
|
|
|
|
|
|
|
res = 1;
|
|
|
|
|
|
|
|
close:
|
|
|
|
close(fd);
|
|
|
|
out:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int boot(char *stage1_path, char *stage2_path, char *config_path ){
|
|
|
|
struct ingenic_dev ingenic_dev;
|
|
|
|
|
2009-04-28 21:13:19 +03:00
|
|
|
int res = 0;
|
2009-04-28 18:57:53 +03:00
|
|
|
int status;
|
|
|
|
|
|
|
|
memset(&ingenic_dev, 0, sizeof(struct ingenic_dev));
|
2009-04-29 10:25:12 +03:00
|
|
|
memset(&fw_args, 0, sizeof(struct fw_args_t));
|
2009-04-28 18:57:53 +03:00
|
|
|
|
|
|
|
if (parse_configure(config_path) < 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (usb_ingenic_init(&ingenic_dev) < 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
status = usb_get_ingenic_cpu(&ingenic_dev);
|
|
|
|
switch (status) {
|
|
|
|
case 1: /* Jz4740v1 */
|
|
|
|
status = 0;
|
|
|
|
fw_args.cpu_id = 0x4740;
|
|
|
|
break;
|
|
|
|
case 2: /* Jz4750v1 */
|
|
|
|
status = 0;
|
|
|
|
fw_args.cpu_id = 0x4750;
|
|
|
|
break;
|
|
|
|
case 3: /* Boot4740 */
|
|
|
|
status = 1;
|
|
|
|
fw_args.cpu_id = 0x4740;
|
|
|
|
break;
|
|
|
|
case 4: /* Boot4750 */
|
|
|
|
status = 1;
|
|
|
|
fw_args.cpu_id = 0x4750;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
printf("Booted");
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
printf("Unboot");
|
|
|
|
printf("\n Now booting device");
|
|
|
|
|
|
|
|
/* now we upload the usb boot stage1 */
|
|
|
|
printf("\n Upload usb boot stage1");
|
|
|
|
if (load_file(&ingenic_dev, stage1_path) < 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* now we upload the usb boot stage2 */
|
|
|
|
sleep(1);
|
|
|
|
printf("\n Upload usb boot stage2");
|
|
|
|
if (load_file(&ingenic_dev, stage2_path) < 1)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (usb_ingenic_upload(&ingenic_dev, 2) < 1)
|
|
|
|
goto cleanup;
|
|
|
|
check_dump_cfg();
|
|
|
|
}
|
|
|
|
|
2009-04-28 21:13:19 +03:00
|
|
|
res = 1;
|
2009-04-28 18:57:53 +03:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (ingenic_dev.file_buff)
|
|
|
|
free(ingenic_dev.file_buff);
|
|
|
|
out:
|
|
|
|
usb_ingenic_cleanup(&ingenic_dev);
|
|
|
|
return res;
|
|
|
|
}
|
2009-04-29 10:25:12 +03:00
|
|
|
int nprog(int com_argc, char **com_argv)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
unsigned int i;
|
2009-04-29 16:26:28 +03:00
|
|
|
int index;
|
|
|
|
int c;
|
|
|
|
char *image_file;
|
|
|
|
char *help = "\n Usage: nprog [options] ..."
|
|
|
|
"\n -s\tstart page number"
|
|
|
|
"\n -i\timage file name"
|
|
|
|
"\n -d\tdevice index number"
|
|
|
|
"\n -f\tflash index number"
|
|
|
|
"\n image type:"
|
|
|
|
"\n \t-n:\tno oob"
|
|
|
|
"\n \t-o:\twith oob no ecc"
|
|
|
|
"\n \t-e:\twith oob and ecc";
|
|
|
|
opterr = 0;
|
|
|
|
if (com_argc != 10) {
|
|
|
|
printf("\n not enough argument.");
|
|
|
|
printf("%s", help);
|
2009-04-29 10:25:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < MAX_DEV_NUM; i++)
|
|
|
|
(nand_in.cs_map)[i] = 0;
|
|
|
|
|
2009-04-29 16:26:28 +03:00
|
|
|
while ((c = getopt (com_argc, com_argv, "s:i:d:f:noe")) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
nand_in.start = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
image_file = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
nand_in.dev = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
(nand_in.cs_map)[atoi(optarg)] = 1;
|
|
|
|
if (atoi(optarg) >= MAX_DEV_NUM) {
|
|
|
|
printf("\n Flash index number overflow!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nand_in.option = NO_OOB;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
nand_in.option = OOB_NO_ECC;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
nand_in.option = OOB_ECC;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
if (optopt == 's' ||
|
|
|
|
optopt == 'i' ||
|
|
|
|
optopt == 'd' ||
|
|
|
|
optopt == 'f')
|
|
|
|
printf ("Option -%c requires an argument.\n", optopt);
|
|
|
|
else if (isprint (optopt))
|
|
|
|
printf ("Unknown option `-%c'.\n", optopt);
|
|
|
|
else
|
|
|
|
printf ("Unknown option character `\\x%x'.\n", optopt);
|
|
|
|
printf("%s", help);
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
printf("%s", help);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-29 10:25:12 +03:00
|
|
|
if (Hand.nand_plane > 1)
|
|
|
|
/* API_Nand_Program_File_Planes(&nand_in,&nand_out,com_argv[2]); */
|
|
|
|
else
|
|
|
|
/* API_Nand_Program_File(&nand_in,&nand_out,com_argv[2]); */
|
2009-04-29 16:26:28 +03:00
|
|
|
|
2009-04-29 10:25:12 +03:00
|
|
|
printf("\n Flash check result:");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf(" %d", (nand_out.status)[i]);
|
2009-04-29 16:26:28 +03:00
|
|
|
printf("nprog %d %s %d %d %d", nand_in.start,
|
|
|
|
image_file,
|
|
|
|
nand_in.dev,
|
|
|
|
(nand_in.cs_map)[atoi(optarg)],
|
|
|
|
nand_in.option);
|
2009-04-29 10:25:12 +03:00
|
|
|
#endif
|
2009-04-29 16:26:28 +03:00
|
|
|
printf("\n not implement yet!! ");
|
|
|
|
|
2009-04-29 10:25:12 +03:00
|
|
|
return 1;
|
|
|
|
}
|