2009-04-28 18:57:53 +03:00
|
|
|
/*
|
2009-07-09 17:42:29 +03:00
|
|
|
* Copyright(C) 2009 Qi Hardware Inc.,
|
2009-06-25 07:33:46 +03:00
|
|
|
* Authors: Marek Lindner <lindner_marek@yahoo.de>
|
2009-07-05 07:43:21 +03:00
|
|
|
* Xiangfu Liu <xiangfu@qi-hardware.com>
|
2009-04-28 18:57:53 +03:00
|
|
|
*
|
2009-07-09 17:42:29 +03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
2009-04-28 18:57:53 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2009-04-29 16:26:28 +03:00
|
|
|
#include <ctype.h>
|
2009-04-30 09:41:49 +03:00
|
|
|
#include "cmd.h"
|
2009-04-30 20:26:14 +03:00
|
|
|
#include "ingenic_cfg.h"
|
2009-05-03 18:06:23 +03:00
|
|
|
#include "ingenic_usb.h"
|
|
|
|
#include "usb_boot_defines.h"
|
2009-04-30 09:41:49 +03:00
|
|
|
|
|
|
|
extern int com_argc;
|
|
|
|
extern char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH];
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
struct ingenic_dev ingenic_dev;
|
2009-05-20 11:13:10 +03:00
|
|
|
struct hand hand;
|
2009-06-28 10:35:20 +03:00
|
|
|
struct sdram_in sdram_in;
|
2009-06-08 11:52:39 +03:00
|
|
|
struct nand_in nand_in;
|
2009-05-20 11:13:10 +03:00
|
|
|
static struct nand_out nand_out;
|
2009-06-28 10:35:20 +03:00
|
|
|
|
2009-04-28 18:57:53 +03:00
|
|
|
unsigned int total_size;
|
2009-04-30 09:41:49 +03:00
|
|
|
unsigned char code_buf[4 * 512 * 1024];
|
2009-05-03 18:33:56 +03:00
|
|
|
unsigned char check_buf[4 * 512 * 1024];
|
2009-04-30 09:41:49 +03:00
|
|
|
unsigned char cs[16];
|
2009-05-03 18:33:56 +03:00
|
|
|
unsigned char ret[8];
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
static const char IMAGE_TYPE[][30] = {
|
|
|
|
"with oob and ecc",
|
|
|
|
"with oob and without ecc",
|
|
|
|
"without oob",
|
|
|
|
};
|
2009-04-28 18:57:53 +03:00
|
|
|
|
|
|
|
static int load_file(struct ingenic_dev *ingenic_dev, const char *file_path)
|
|
|
|
{
|
|
|
|
struct stat fstat;
|
|
|
|
int fd, status, res = -1;
|
|
|
|
|
|
|
|
status = stat(file_path, &fstat);
|
|
|
|
|
|
|
|
if (status < 0) {
|
2009-04-30 20:26:14 +03:00
|
|
|
fprintf(stderr, "Error - can't get file size from '%s': %s\n",
|
|
|
|
file_path, strerror(errno));
|
2009-04-28 18:57:53 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ingenic_dev->file_len = fstat.st_size;
|
2009-05-26 20:05:54 +03:00
|
|
|
ingenic_dev->file_buff = code_buf;
|
2009-04-28 18:57:53 +03:00
|
|
|
|
|
|
|
fd = open(file_path, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
2009-04-30 20:26:14 +03:00
|
|
|
fprintf(stderr, "Error - can't open file '%s': %s\n",
|
|
|
|
file_path, strerror(errno));
|
2009-04-28 18:57:53 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = read(fd, ingenic_dev->file_buff, ingenic_dev->file_len);
|
|
|
|
|
|
|
|
if (status < ingenic_dev->file_len) {
|
2009-04-30 20:26:14 +03:00
|
|
|
fprintf(stderr, "Error - can't read file '%s': %s\n",
|
|
|
|
file_path, strerror(errno));
|
2009-04-28 18:57:53 +03:00
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
/* write args to code */
|
|
|
|
memcpy(ingenic_dev->file_buff + 8, &hand.fw_args,
|
2009-05-20 11:13:10 +03:00
|
|
|
sizeof(struct fw_args));
|
2009-04-28 18:57:53 +03:00
|
|
|
|
|
|
|
res = 1;
|
|
|
|
|
|
|
|
close:
|
|
|
|
close(fd);
|
|
|
|
out:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-05-16 05:57:10 +03:00
|
|
|
/* after upload stage2. must init device */
|
2009-06-25 08:37:17 +03:00
|
|
|
void init_cfg()
|
2009-05-12 17:41:52 +03:00
|
|
|
{
|
2009-05-14 20:11:03 +03:00
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" XBurst CPU not booted yet, boot it first!\n");
|
2009-06-25 08:37:17 +03:00
|
|
|
return;
|
2009-05-14 20:11:03 +03:00
|
|
|
}
|
|
|
|
|
2009-05-12 17:41:52 +03:00
|
|
|
ingenic_dev.file_buff = &hand;
|
2009-05-14 20:11:03 +03:00
|
|
|
ingenic_dev.file_len = sizeof(hand);
|
2009-05-12 17:41:52 +03:00
|
|
|
if (usb_send_data_to_ingenic(&ingenic_dev) != 1)
|
2009-06-25 08:37:17 +03:00
|
|
|
goto xout;
|
2009-05-12 17:41:52 +03:00
|
|
|
|
|
|
|
if (usb_ingenic_configration(&ingenic_dev, DS_hand) != 1)
|
2009-06-25 08:37:17 +03:00
|
|
|
goto xout;
|
2009-05-12 17:41:52 +03:00
|
|
|
|
2009-05-14 12:15:11 +03:00
|
|
|
if (usb_read_data_from_ingenic(&ingenic_dev, ret, 8) != 1)
|
2009-06-25 08:37:17 +03:00
|
|
|
goto xout;
|
2009-05-12 17:41:52 +03:00
|
|
|
|
2009-06-25 19:39:24 +03:00
|
|
|
printf(" Configuring XBurst CPU succeeded.\n");
|
2009-06-25 08:37:17 +03:00
|
|
|
return;
|
|
|
|
xout:
|
|
|
|
printf("Configuring XBurst CPU failed.\n");
|
2009-05-12 17:41:52 +03:00
|
|
|
}
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-05-12 17:41:52 +03:00
|
|
|
int boot(char *stage1_path, char *stage2_path){
|
2009-04-28 18:57:53 +03:00
|
|
|
int status;
|
|
|
|
|
|
|
|
status = usb_get_ingenic_cpu(&ingenic_dev);
|
|
|
|
switch (status) {
|
|
|
|
case 1: /* Jz4740v1 */
|
|
|
|
status = 0;
|
2009-04-30 10:06:33 +03:00
|
|
|
hand.fw_args.cpu_id = 0x4740;
|
2009-04-28 18:57:53 +03:00
|
|
|
break;
|
|
|
|
case 2: /* Jz4750v1 */
|
|
|
|
status = 0;
|
2009-04-30 10:06:33 +03:00
|
|
|
hand.fw_args.cpu_id = 0x4750;
|
2009-04-28 18:57:53 +03:00
|
|
|
break;
|
|
|
|
case 3: /* Boot4740 */
|
|
|
|
status = 1;
|
2009-04-30 10:06:33 +03:00
|
|
|
hand.fw_args.cpu_id = 0x4740;
|
2009-04-28 18:57:53 +03:00
|
|
|
break;
|
|
|
|
case 4: /* Boot4750 */
|
|
|
|
status = 1;
|
2009-04-30 10:06:33 +03:00
|
|
|
hand.fw_args.cpu_id = 0x4750;
|
2009-04-28 18:57:53 +03:00
|
|
|
break;
|
|
|
|
default:
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
2009-04-28 18:57:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status) {
|
2009-06-28 10:35:20 +03:00
|
|
|
printf(" Already booted.\n");
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
2009-04-28 18:57:53 +03:00
|
|
|
} else {
|
2009-06-25 19:39:24 +03:00
|
|
|
printf(" CPU not yet booted, now booting...\n");
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-06-25 08:37:17 +03:00
|
|
|
/* now we upload the boot stage1 */
|
|
|
|
printf(" Loading stage1 from '%s'\n", stage1_path);
|
2009-04-28 18:57:53 +03:00
|
|
|
if (load_file(&ingenic_dev, stage1_path) < 1)
|
2009-05-12 17:41:52 +03:00
|
|
|
return -1;
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-05-26 20:05:54 +03:00
|
|
|
if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
|
2009-05-12 17:41:52 +03:00
|
|
|
return -1;
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-06-25 08:37:17 +03:00
|
|
|
/* now we upload the boot stage2 */
|
2009-06-25 19:39:24 +03:00
|
|
|
usleep(100);
|
2009-06-25 08:37:17 +03:00
|
|
|
printf(" Loading stage2 from '%s'\n", stage2_path);
|
2009-05-26 20:05:54 +03:00
|
|
|
if (load_file(&ingenic_dev, stage2_path) < 1)
|
2009-05-12 17:41:52 +03:00
|
|
|
return -1;
|
2009-04-28 18:57:53 +03:00
|
|
|
|
2009-05-26 20:05:54 +03:00
|
|
|
if (usb_ingenic_upload(&ingenic_dev, 2) < 1)
|
2009-05-12 17:41:52 +03:00
|
|
|
return -1;
|
2009-05-26 20:05:54 +03:00
|
|
|
|
2009-06-25 19:39:24 +03:00
|
|
|
printf(" Booted successfully!\n");
|
2009-04-28 18:57:53 +03:00
|
|
|
}
|
2009-06-25 19:39:24 +03:00
|
|
|
usleep(100);
|
2009-06-25 08:37:17 +03:00
|
|
|
init_cfg();
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
2009-04-28 18:57:53 +03:00
|
|
|
}
|
2009-04-30 20:26:14 +03:00
|
|
|
|
|
|
|
/* nand function */
|
2009-05-12 17:41:52 +03:00
|
|
|
int error_check(unsigned char *org,unsigned char * obj,unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2009-06-26 07:59:53 +03:00
|
|
|
printf(" Comparing %d bytes - ", size);
|
2009-05-12 17:41:52 +03:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (org[i] != obj[i]) {
|
2009-06-26 07:59:53 +03:00
|
|
|
unsigned int s = (i < 8) ? i : i - 8; // start_dump
|
2009-06-28 10:35:20 +03:00
|
|
|
printf("FAIL at off %d, wrote 0x%x, read 0x%x\n", i, org[i], obj[i]);
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" off %d write: %02x %02x %02x %02x %02x %02x %02x %02x"
|
|
|
|
" %02x %02x %02x %02x %02x %02x %02x %02x\n", s,
|
2009-06-26 07:59:53 +03:00
|
|
|
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]);
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" off %d read: %02x %02x %02x %02x %02x %02x %02x %02x"
|
|
|
|
" %02x %02x %02x %02x %02x %02x %02x %02x\n", s,
|
2009-06-26 07:59:53 +03:00
|
|
|
obj[s], obj[s+1], obj[s+2], obj[s+3], obj[s+4], obj[s+5], obj[s+6], obj[s+7],
|
|
|
|
obj[s+8], obj[s+9], obj[s+10], obj[s+11], obj[s+12], obj[s+13], obj[s+14], obj[s+15]);
|
2009-05-12 17:41:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-06-26 07:59:53 +03:00
|
|
|
printf("SUCCESS\n");
|
2009-05-12 17:41:52 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-20 11:13:10 +03:00
|
|
|
int nand_markbad(struct nand_in *nand_in)
|
2009-05-04 17:36:51 +03:00
|
|
|
{
|
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Device unboot! Boot it first!\n");
|
2009-05-04 17:36:51 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" mark bad block : %d\n",nand_in->start);
|
2009-05-04 17:36:51 +03:00
|
|
|
usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
|
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, NAND_MARK_BAD);
|
2009-05-14 12:15:11 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Mark bad block at %d\n",((ret[3] << 24) |
|
2009-05-04 17:36:51 +03:00
|
|
|
(ret[2] << 16) |
|
2009-05-14 12:15:11 +03:00
|
|
|
(ret[1] << 8) |
|
2009-05-04 17:36:51 +03:00
|
|
|
(ret[0] << 0)) / hand.nand_ppb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-20 11:13:10 +03:00
|
|
|
int nand_program_check(struct nand_in *nand_in,
|
|
|
|
struct nand_out *nand_out,
|
2009-05-07 06:07:52 +03:00
|
|
|
unsigned int *start_page)
|
2009-04-30 20:26:14 +03:00
|
|
|
{
|
2009-06-25 11:31:13 +03:00
|
|
|
unsigned int i, page_num, cur_page = -1;
|
2009-05-03 18:33:56 +03:00
|
|
|
unsigned short temp;
|
|
|
|
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Writing NAND page %d len %d...\n", nand_in->start, nand_in->length);
|
2009-05-03 18:33:56 +03:00
|
|
|
if (nand_in->length > (unsigned int)MAX_TRANSFER_SIZE) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Buffer size too long!\n");
|
2009-05-03 18:33:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
#ifdef CONFIG_NAND_OUT
|
2009-05-31 06:32:19 +03:00
|
|
|
unsigned char status_buf[32];
|
2009-05-03 18:33:56 +03:00
|
|
|
nand_out->status = status_buf;
|
|
|
|
for (i = 0; i < nand_in->max_chip; i++)
|
|
|
|
(nand_out->status)[i] = 0; /* set all status to fail */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Device unboot! Boot it first!\n");
|
2009-05-03 18:33:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-04 17:36:51 +03:00
|
|
|
ingenic_dev.file_buff = nand_in->buf;
|
|
|
|
ingenic_dev.file_len = nand_in->length;
|
|
|
|
usb_send_data_to_ingenic(&ingenic_dev);
|
2009-05-03 18:33:56 +03:00
|
|
|
for (i = 0; i < nand_in->max_chip; i++) {
|
2009-05-14 20:11:03 +03:00
|
|
|
if ((nand_in->cs_map)[i]==0)
|
|
|
|
continue;
|
2009-05-14 12:15:11 +03:00
|
|
|
if (nand_in->option == NO_OOB) {
|
|
|
|
page_num = nand_in->length / hand.nand_ps;
|
|
|
|
if ((nand_in->length % hand.nand_ps) !=0)
|
|
|
|
page_num++;
|
|
|
|
} else {
|
2009-05-14 20:11:03 +03:00
|
|
|
page_num = nand_in->length /
|
|
|
|
(hand.nand_ps + hand.nand_os);
|
|
|
|
if ((nand_in->length% (hand.nand_ps + hand.nand_os)) !=0)
|
2009-05-14 12:15:11 +03:00
|
|
|
page_num++;
|
|
|
|
}
|
2009-05-14 20:11:03 +03:00
|
|
|
temp = ((nand_in->option << 12) & 0xf000) +
|
|
|
|
((i<<4) & 0xff0) + NAND_PROGRAM;
|
2009-05-14 12:15:11 +03:00
|
|
|
usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
|
|
|
|
usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
|
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, temp);
|
2009-05-14 20:11:03 +03:00
|
|
|
|
2009-05-14 12:15:11 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" Finish! (len %d start_page %d page_num %d)\n",
|
|
|
|
nand_in->length, nand_in->start, page_num);
|
2009-05-14 12:15:11 +03:00
|
|
|
|
|
|
|
usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
|
|
|
|
/* Read back to check! */
|
|
|
|
usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
|
|
|
|
|
|
|
|
switch (nand_in->option) {
|
2009-05-03 18:33:56 +03:00
|
|
|
case OOB_ECC:
|
2009-05-14 20:11:03 +03:00
|
|
|
temp = ((OOB_ECC << 12) & 0xf000) +
|
|
|
|
((i << 4) & 0xff0) + NAND_READ;
|
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, temp);
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" Checking %d bytes...", nand_in->length);
|
2009-05-14 20:11:03 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, check_buf,
|
|
|
|
page_num * (hand.nand_ps + hand.nand_os));
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-05-03 18:33:56 +03:00
|
|
|
break;
|
2009-05-14 12:15:11 +03:00
|
|
|
case OOB_NO_ECC: /* do not support data verify */
|
2009-05-14 20:11:03 +03:00
|
|
|
temp = ((OOB_NO_ECC << 12) & 0xf000) +
|
|
|
|
((i << 4) & 0xff0) + NAND_READ;
|
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, temp);
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" Checking %d bytes...", nand_in->length);
|
2009-05-14 20:11:03 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, check_buf,
|
|
|
|
page_num * (hand.nand_ps + hand.nand_os));
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-05-03 18:33:56 +03:00
|
|
|
break;
|
|
|
|
case NO_OOB:
|
2009-05-14 20:11:03 +03:00
|
|
|
temp = ((NO_OOB << 12) & 0xf000) +
|
|
|
|
((i << 4) & 0xff0) + NAND_READ;
|
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, temp);
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" Checking %d bytes...", nand_in->length);
|
2009-05-14 20:11:03 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, check_buf,
|
|
|
|
page_num * hand.nand_ps);
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-05-03 18:33:56 +03:00
|
|
|
break;
|
2009-05-14 12:15:11 +03:00
|
|
|
default:
|
|
|
|
;
|
2009-05-03 18:33:56 +03:00
|
|
|
}
|
2009-05-14 12:15:11 +03:00
|
|
|
|
2009-06-24 13:28:10 +03:00
|
|
|
cur_page = (ret[3] << 24) | (ret[2] << 16) | (ret[1] << 8) |
|
|
|
|
(ret[0] << 0);
|
|
|
|
|
2009-07-10 07:15:38 +03:00
|
|
|
#ifdef CONFIG_NAND_OUT
|
|
|
|
(nand_out->status)[i] = 1;
|
|
|
|
#endif
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
if (nand_in->start < 1 &&
|
|
|
|
hand.nand_ps == 4096 &&
|
|
|
|
hand.fw_args.cpu_id == 0x4740) {
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" no check! End at Page: %d\n", cur_page);
|
2009-05-03 18:33:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-14 12:15:11 +03:00
|
|
|
|
2009-07-10 07:15:38 +03:00
|
|
|
if (!nand_in->check(nand_in->buf, check_buf, nand_in->length)) {
|
|
|
|
#ifdef CONFIG_NAND_OUT
|
|
|
|
(nand_out->status)[i] = 0;
|
|
|
|
#endif
|
2009-05-20 11:13:10 +03:00
|
|
|
struct nand_in bad;
|
2009-06-26 07:59:53 +03:00
|
|
|
// tbd: doesn't the other side skip bad blocks too? Can we just deduct 1 from cur_page?
|
|
|
|
// tbd: why do we only mark a block as bad if the last page in the block was written?
|
2009-05-14 12:15:11 +03:00
|
|
|
bad.start = (cur_page - 1) / hand.nand_ppb;
|
|
|
|
if (cur_page % hand.nand_ppb == 0)
|
|
|
|
nand_markbad(&bad);
|
|
|
|
}
|
2009-07-10 07:15:38 +03:00
|
|
|
|
|
|
|
printf(" End at Page: %d\n",cur_page);
|
2009-05-03 18:33:56 +03:00
|
|
|
}
|
2009-06-24 13:28:10 +03:00
|
|
|
|
2009-05-07 06:07:52 +03:00
|
|
|
*start_page = cur_page;
|
|
|
|
return 0;
|
2009-04-30 20:26:14 +03:00
|
|
|
}
|
|
|
|
|
2009-05-20 11:13:10 +03:00
|
|
|
int nand_erase(struct nand_in *nand_in)
|
2009-04-30 20:26:14 +03:00
|
|
|
{
|
|
|
|
unsigned int start_blk, blk_num, end_block;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
start_blk = nand_in->start;
|
|
|
|
blk_num = nand_in->length;
|
|
|
|
if (start_blk > (unsigned int)NAND_MAX_BLK_NUM) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Start block number overflow!\n");
|
2009-04-30 20:26:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (blk_num > (unsigned int)NAND_MAX_BLK_NUM) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Length block number overflow!\n");
|
2009-04-30 20:26:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Device unboot! Boot it first!\n");
|
2009-04-30 20:26:14 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nand_in->max_chip; i++) {
|
|
|
|
if ((nand_in->cs_map)[i]==0)
|
|
|
|
continue;
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Erasing No.%d device No.%d flash (start_blk %u blk_num %u)......\n",
|
2009-06-26 07:59:53 +03:00
|
|
|
nand_in->dev, i, start_blk, blk_num);
|
2009-04-30 20:26:14 +03:00
|
|
|
|
2009-05-03 18:06:23 +03:00
|
|
|
usb_send_data_address_to_ingenic(&ingenic_dev, start_blk);
|
2009-05-08 19:14:14 +03:00
|
|
|
usb_send_data_length_to_ingenic(&ingenic_dev, blk_num);
|
|
|
|
|
2009-05-03 18:06:23 +03:00
|
|
|
unsigned short temp = ((i << 4) & 0xff0) + NAND_ERASE;
|
2009-05-03 18:33:56 +03:00
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, temp);
|
2009-05-08 19:14:14 +03:00
|
|
|
|
2009-05-14 12:15:11 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-04-30 20:26:14 +03:00
|
|
|
printf(" Finish!");
|
|
|
|
}
|
2009-05-01 19:14:55 +03:00
|
|
|
end_block = ((ret[3] << 24) |
|
|
|
|
(ret[2] << 16) |
|
|
|
|
(ret[1] << 8) |
|
2009-05-03 18:33:56 +03:00
|
|
|
(ret[0] << 0)) / hand.nand_ppb;
|
2009-07-10 07:15:38 +03:00
|
|
|
printf(" Return: %02x %02x %02x %02x %02x %02x %02x %02x (position %d)\n",
|
|
|
|
ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], end_block);
|
2009-05-14 20:11:03 +03:00
|
|
|
if (!hand.nand_force_erase) {
|
|
|
|
/* not force erase, show bad block infomation */
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" There are marked bad blocks: %d\n",
|
2009-05-14 20:11:03 +03:00
|
|
|
end_block - start_blk - blk_num );
|
|
|
|
} else {
|
|
|
|
/* force erase, no bad block infomation can show */
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Force erase, no bad block infomation!\n" );
|
2009-04-30 20:26:14 +03:00
|
|
|
}
|
2009-07-10 07:15:38 +03:00
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-20 11:13:10 +03:00
|
|
|
int nand_program_file(struct nand_in *nand_in,
|
|
|
|
struct nand_out *nand_out,
|
2009-04-30 20:26:14 +03:00
|
|
|
char *fname)
|
|
|
|
{
|
|
|
|
|
|
|
|
int flen, m, j, k;
|
|
|
|
unsigned int start_page = 0, page_num, code_len, offset, transfer_size;
|
2009-05-31 06:32:19 +03:00
|
|
|
int fd, status;
|
|
|
|
struct stat fstat;
|
2009-05-20 11:13:10 +03:00
|
|
|
struct nand_in n_in;
|
|
|
|
struct nand_out n_out;
|
2009-04-30 20:26:14 +03:00
|
|
|
|
2009-05-14 12:15:11 +03:00
|
|
|
#ifdef CONFIG_NAND_OUT
|
2009-05-31 06:32:19 +03:00
|
|
|
unsigned char status_buf[32];
|
2009-04-30 20:26:14 +03:00
|
|
|
nand_out->status = status_buf;
|
2009-05-03 18:06:23 +03:00
|
|
|
for (i=0; i<nand_in->max_chip; i++)
|
2009-04-30 20:26:14 +03:00
|
|
|
(nand_out->status)[i] = 0; /* set all status to fail */
|
|
|
|
#endif
|
2009-05-31 06:32:19 +03:00
|
|
|
status = stat(fname, &fstat);
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
fprintf(stderr, "Error - can't get file size from '%s': %s\n",
|
|
|
|
fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
flen = fstat.st_size;
|
|
|
|
|
|
|
|
fd = open(fname, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Error - can't open file '%s': %s\n",
|
|
|
|
fname, strerror(errno));
|
|
|
|
return -1;
|
2009-04-30 20:26:14 +03:00
|
|
|
}
|
|
|
|
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Programing No.%d device, flen %d, start page %d...\n",nand_in->dev, flen, nand_in->start);
|
2009-04-30 20:26:14 +03:00
|
|
|
n_in.start = nand_in->start / hand.nand_ppb;
|
|
|
|
if (nand_in->option == NO_OOB) {
|
|
|
|
if (flen % (hand.nand_ppb * hand.nand_ps) == 0)
|
|
|
|
n_in.length = flen / (hand.nand_ps * hand.nand_ppb);
|
|
|
|
else
|
|
|
|
n_in.length = flen / (hand.nand_ps * hand.nand_ppb) + 1;
|
|
|
|
} else {
|
|
|
|
if (flen % (hand.nand_ppb * (hand.nand_ps + hand.nand_os)) == 0)
|
2009-05-14 20:11:03 +03:00
|
|
|
n_in.length = flen /
|
|
|
|
((hand.nand_ps + hand.nand_os) * hand.nand_ppb);
|
2009-04-30 20:26:14 +03:00
|
|
|
else
|
2009-05-14 20:11:03 +03:00
|
|
|
n_in.length = flen /
|
|
|
|
((hand.nand_ps + hand.nand_os) * hand.nand_ppb)
|
|
|
|
+ 1;
|
2009-04-30 20:26:14 +03:00
|
|
|
}
|
2009-06-28 06:38:48 +03:00
|
|
|
/* printf(" length %d flen %d\n", n_in.length, flen); */
|
2009-04-30 20:26:14 +03:00
|
|
|
n_in.cs_map = nand_in->cs_map;
|
|
|
|
n_in.dev = nand_in->dev;
|
|
|
|
n_in.max_chip = nand_in->max_chip;
|
2009-05-03 18:06:23 +03:00
|
|
|
if (nand_erase(&n_in) != 1)
|
2009-04-30 20:26:14 +03:00
|
|
|
return -1;
|
|
|
|
if (nand_in->option == NO_OOB)
|
|
|
|
transfer_size = (hand.nand_ppb * hand.nand_ps);
|
|
|
|
else
|
|
|
|
transfer_size = (hand.nand_ppb * (hand.nand_ps + hand.nand_os));
|
|
|
|
m = flen / transfer_size;
|
|
|
|
j = flen % transfer_size;
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Size to send %d, transfer_size %d\n", flen, transfer_size);
|
|
|
|
printf(" Image type : %s\n", IMAGE_TYPE[nand_in->option]);
|
|
|
|
printf(" It will cause %d times buffer transfer.\n", j == 0 ? m : m + 1);
|
2009-05-14 12:15:11 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_NAND_OUT
|
2009-04-30 20:26:14 +03:00
|
|
|
for (i = 0; i < nand_in->max_chip; i++)
|
|
|
|
(nand_out->status)[i] = 1; /* set all status to success! */
|
|
|
|
#endif
|
2009-05-31 06:32:19 +03:00
|
|
|
|
|
|
|
offset = 0;
|
2009-04-30 20:26:14 +03:00
|
|
|
for (k = 0; k < m; k++) {
|
2009-05-04 19:08:03 +03:00
|
|
|
if (nand_in->option == NO_OOB)
|
2009-04-30 20:26:14 +03:00
|
|
|
page_num = transfer_size / hand.nand_ps;
|
2009-05-04 19:08:03 +03:00
|
|
|
else
|
2009-04-30 20:26:14 +03:00
|
|
|
page_num = transfer_size / (hand.nand_ps + hand.nand_os);
|
2009-05-04 19:08:03 +03:00
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
code_len = transfer_size;
|
2009-05-31 06:32:19 +03:00
|
|
|
status = read(fd, code_buf, code_len);
|
|
|
|
if (status < code_len) {
|
|
|
|
fprintf(stderr, "Error - can't read file '%s': %s\n",
|
|
|
|
fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
nand_in->length = code_len; /* code length,not page number! */
|
|
|
|
nand_in->buf = code_buf;
|
2009-05-08 19:14:14 +03:00
|
|
|
if (nand_program_check(nand_in, &n_out, &start_page) == -1)
|
2009-05-07 06:07:52 +03:00
|
|
|
return -1;
|
|
|
|
|
2009-05-08 19:14:14 +03:00
|
|
|
if (start_page - nand_in->start > hand.nand_ppb)
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Skip a old bad block !\n");
|
2009-04-30 20:26:14 +03:00
|
|
|
nand_in->start = start_page;
|
2009-05-14 12:15:11 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_NAND_OUT
|
2009-04-30 20:26:14 +03:00
|
|
|
for (i = 0; i < nand_in->max_chip; i++) {
|
2009-05-14 20:11:03 +03:00
|
|
|
(nand_out->status)[i] = (nand_out->status)[i] *
|
|
|
|
(n_out.status)[i];
|
2009-04-30 20:26:14 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
offset += code_len ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j) {
|
2009-05-31 06:32:19 +03:00
|
|
|
code_len = j;
|
2009-06-26 07:59:53 +03:00
|
|
|
if (j % hand.nand_ps)
|
|
|
|
j += hand.nand_ps - (j % hand.nand_ps);
|
2009-05-14 20:11:03 +03:00
|
|
|
memset(code_buf, 0, j); /* set all to null */
|
2009-05-31 06:32:19 +03:00
|
|
|
|
2009-06-26 07:59:53 +03:00
|
|
|
status = read(fd, code_buf, code_len);
|
2009-05-31 06:32:19 +03:00
|
|
|
|
|
|
|
if (status < code_len) {
|
|
|
|
fprintf(stderr, "Error - can't read file '%s': %s\n",
|
|
|
|
fname, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-30 20:26:14 +03:00
|
|
|
nand_in->length = j;
|
|
|
|
nand_in->buf = code_buf;
|
2009-05-07 06:07:52 +03:00
|
|
|
if (nand_program_check(nand_in, &n_out, &start_page) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (start_page - nand_in->start > hand.nand_ppb)
|
2009-04-30 20:26:14 +03:00
|
|
|
printf(" Skip a old bad block !");
|
2009-05-14 12:15:11 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_NAND_OUT
|
2009-05-03 18:06:23 +03:00
|
|
|
for (i=0; i < nand_in->max_chip; i++) {
|
2009-05-14 20:11:03 +03:00
|
|
|
(nand_out->status)[i] = (nand_out->status)[i] *
|
|
|
|
(n_out.status)[i];
|
2009-04-30 20:26:14 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-31 06:32:19 +03:00
|
|
|
close(fd);
|
2009-04-30 20:26:14 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-20 11:13:10 +03:00
|
|
|
int nand_program_file_planes(struct nand_in *nand_in,
|
|
|
|
struct nand_out *nand_out,
|
2009-05-16 05:57:10 +03:00
|
|
|
char *fname)
|
|
|
|
{
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" not implement yet !\n");
|
2009-05-16 05:57:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
int init_nand_in(void)
|
|
|
|
{
|
|
|
|
nand_in.buf = code_buf;
|
|
|
|
nand_in.check = error_check;
|
|
|
|
nand_in.dev = 0;
|
|
|
|
nand_in.cs_map = cs;
|
2009-06-08 11:52:39 +03:00
|
|
|
memset(nand_in.cs_map, 0, MAX_DEV_NUM);
|
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
nand_in.max_chip = 16;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nand_prog(void)
|
2009-04-29 10:25:12 +03:00
|
|
|
{
|
2009-04-29 16:26:28 +03:00
|
|
|
char *image_file;
|
2009-06-28 06:38:48 +03:00
|
|
|
char *help = " Usage: nprog (1) (2) (3) (4) (5)\n"
|
|
|
|
" (1)\tstart page number\n"
|
|
|
|
" (2)\timage file name\n"
|
|
|
|
" (3)\tdevice index number\n"
|
|
|
|
" (4)\tflash index number\n"
|
|
|
|
" (5) image type must be:\n"
|
|
|
|
" \t-n:\tno oob\n"
|
|
|
|
" \t-o:\twith oob no ecc\n"
|
|
|
|
" \t-e:\twith oob and ecc\n";
|
2009-04-30 09:41:49 +03:00
|
|
|
|
|
|
|
if (com_argc != 6) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" not enough argument.\n");
|
2009-04-29 16:26:28 +03:00
|
|
|
printf("%s", help);
|
2009-04-29 10:25:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-04 17:36:51 +03:00
|
|
|
|
2009-06-08 11:52:39 +03:00
|
|
|
init_nand_in();
|
2009-04-29 10:25:12 +03:00
|
|
|
|
2009-04-30 09:41:49 +03:00
|
|
|
nand_in.start = atoi(com_argv[1]);
|
|
|
|
image_file = com_argv[2];
|
|
|
|
nand_in.dev = atoi(com_argv[3]);
|
|
|
|
(nand_in.cs_map)[atoi(com_argv[4])] = 1;
|
2009-05-14 20:11:03 +03:00
|
|
|
if (!strcmp(com_argv[5], "-e"))
|
2009-04-30 09:41:49 +03:00
|
|
|
nand_in.option = OOB_ECC;
|
2009-05-14 20:11:03 +03:00
|
|
|
else if (!strcmp(com_argv[5], "-o"))
|
2009-04-30 09:41:49 +03:00
|
|
|
nand_in.option = OOB_NO_ECC;
|
2009-05-14 20:11:03 +03:00
|
|
|
else if (!strcmp(com_argv[5], "-n"))
|
2009-04-30 09:41:49 +03:00
|
|
|
nand_in.option = NO_OOB;
|
|
|
|
else
|
|
|
|
printf("%s", help);
|
2009-04-30 20:26:14 +03:00
|
|
|
|
2009-04-30 10:18:44 +03:00
|
|
|
if (hand.nand_plane > 1)
|
2009-05-16 05:57:10 +03:00
|
|
|
nand_program_file_planes(&nand_in, &nand_out, image_file);
|
2009-04-29 10:25:12 +03:00
|
|
|
else
|
2009-05-16 05:57:10 +03:00
|
|
|
nand_program_file(&nand_in, &nand_out, image_file);
|
2009-04-29 16:26:28 +03:00
|
|
|
|
2009-05-14 20:11:03 +03:00
|
|
|
#ifdef CONFIG_NAND_OUT
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Flash check result:\n");
|
2009-06-25 11:31:13 +03:00
|
|
|
int i;
|
2009-04-29 10:25:12 +03:00
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
printf(" %d", (nand_out.status)[i]);
|
2009-04-30 09:41:49 +03:00
|
|
|
#endif
|
2009-05-14 20:11:03 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nand_query(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char csn;
|
|
|
|
|
|
|
|
if (com_argc < 3) {
|
2009-06-28 10:35:20 +03:00
|
|
|
printf(" Usage: nquery (1) (2)\n"
|
|
|
|
" (1):device index number\n"
|
2009-06-28 06:38:48 +03:00
|
|
|
" (2):flash index number\n");
|
2009-05-14 20:11:03 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
init_nand_in();
|
|
|
|
|
|
|
|
nand_in.dev = atoi(com_argv[1]);
|
|
|
|
(nand_in.cs_map)[atoi(com_argv[2])] = 1;
|
|
|
|
|
|
|
|
for (i = 0; i < nand_in.max_chip; i++) {
|
|
|
|
if ((nand_in.cs_map)[i] != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= nand_in.max_chip)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Device unboot! Boot it first!\n");
|
2009-05-14 20:11:03 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
csn = i;
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" ID of No.%d device No.%d flash: \n", nand_in.dev, csn);
|
2009-05-14 20:11:03 +03:00
|
|
|
|
|
|
|
unsigned short ops = ((csn << 4) & 0xff0) + NAND_QUERY;
|
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, ops);
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Vendor ID :0x%x \n",(unsigned char)ret[0]);
|
|
|
|
printf(" Product ID :0x%x \n",(unsigned char)ret[1]);
|
|
|
|
printf(" Chip ID :0x%x \n",(unsigned char)ret[2]);
|
|
|
|
printf(" Page ID :0x%x \n",(unsigned char)ret[3]);
|
|
|
|
printf(" Plane ID :0x%x \n",(unsigned char)ret[4]);
|
2009-05-14 20:11:03 +03:00
|
|
|
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Operation status: Success!\n");
|
2009-05-14 20:11:03 +03:00
|
|
|
|
2009-04-29 10:25:12 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2009-06-25 11:20:59 +03:00
|
|
|
|
|
|
|
int nand_read(int mode)
|
|
|
|
{
|
|
|
|
unsigned int i,j;
|
|
|
|
unsigned int start_addr, length, page_num;
|
|
|
|
unsigned char csn;
|
2009-06-25 11:31:13 +03:00
|
|
|
unsigned short temp = 0;
|
2009-06-28 10:35:20 +03:00
|
|
|
unsigned ram_addr = 0;
|
2009-06-25 11:20:59 +03:00
|
|
|
|
|
|
|
if (com_argc < 5) {
|
2009-06-28 10:35:20 +03:00
|
|
|
printf(" Usage: nread (1) (2) (3) (4)\n"
|
|
|
|
" 1:start page number\n"
|
2009-06-28 06:38:48 +03:00
|
|
|
" 2:length in byte\n"
|
|
|
|
" 3:device index number\n"
|
2009-06-28 10:35:20 +03:00
|
|
|
" 4:flash index number\n"
|
|
|
|
" 5:start SDRAM address\n");
|
2009-06-25 11:20:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
init_nand_in();
|
|
|
|
|
|
|
|
if (atoi(com_argv[4]) >= MAX_DEV_NUM) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Flash index number overflow!\n");
|
2009-06-25 11:20:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
(nand_in.cs_map)[atoi(com_argv[4])] = 1;
|
|
|
|
nand_in.start = atoi(com_argv[1]);
|
|
|
|
nand_in.length= atoi(com_argv[2]);
|
|
|
|
nand_in.dev = atoi(com_argv[3]);
|
|
|
|
|
2009-06-28 10:35:20 +03:00
|
|
|
if (com_argc = 6) {
|
|
|
|
ram_addr = strtoul(com_argv[5], NULL, 0);
|
|
|
|
printf("==%s==", com_argv[5]);
|
|
|
|
}
|
2009-06-25 11:31:13 +03:00
|
|
|
start_addr = nand_in.start;
|
|
|
|
length = nand_in.length;
|
2009-06-25 11:20:59 +03:00
|
|
|
|
|
|
|
if (start_addr > NAND_MAX_PAGE_NUM || length > NAND_MAX_PAGE_NUM ) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Page number overflow!\n");
|
2009-06-25 11:20:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Device unboot! Boot it first!\n");
|
2009-06-25 11:20:59 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2009-06-25 11:31:13 +03:00
|
|
|
for (i = 0; i < nand_in.max_chip; i++)
|
|
|
|
if ((nand_in.cs_map)[i] != 0)
|
2009-06-25 11:20:59 +03:00
|
|
|
break;
|
2009-06-25 11:31:13 +03:00
|
|
|
if (i >= nand_in.max_chip) return 1;
|
2009-06-25 11:20:59 +03:00
|
|
|
csn = i;
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Reading from No.%d device No.%d flash....\n",nand_in.dev,csn);
|
2009-06-25 11:20:59 +03:00
|
|
|
|
2009-06-25 11:31:13 +03:00
|
|
|
page_num = length / hand.nand_ps +1;
|
2009-06-25 11:20:59 +03:00
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case NAND_READ:
|
|
|
|
temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + NAND_READ;
|
|
|
|
break;
|
|
|
|
case NAND_READ_OOB:
|
|
|
|
temp = ((csn<<4) & 0xff0) + NAND_READ_OOB;
|
|
|
|
break;
|
|
|
|
case NAND_READ_RAW:
|
2009-06-28 10:35:20 +03:00
|
|
|
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);
|
2009-06-25 11:20:59 +03:00
|
|
|
break;
|
|
|
|
default:
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" unknow mode!\n");
|
2009-06-25 11:31:13 +03:00
|
|
|
return -1;
|
2009-06-25 11:20:59 +03:00
|
|
|
}
|
|
|
|
|
2009-06-28 10:35:20 +03:00
|
|
|
usb_send_data_address_to_ingenic(&ingenic_dev, start_addr);
|
|
|
|
usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
|
|
|
|
|
2009-06-25 11:20:59 +03:00
|
|
|
usb_ingenic_nand_ops(&ingenic_dev, temp);
|
|
|
|
|
2009-06-25 11:31:13 +03:00
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, nand_in.buf, page_num * hand.nand_ps);
|
2009-06-25 11:20:59 +03:00
|
|
|
|
2009-06-28 10:35:20 +03:00
|
|
|
for (j = 0; j < length; j++) {
|
|
|
|
if (j % 16 == 0)
|
|
|
|
printf("\n 0x%08x : ",j);
|
2009-06-25 11:31:13 +03:00
|
|
|
printf("%02x ",(nand_in.buf)[j]);
|
2009-06-25 11:20:59 +03:00
|
|
|
}
|
2009-06-28 10:35:20 +03:00
|
|
|
printf("\n");
|
2009-06-25 11:20:59 +03:00
|
|
|
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Operation end position : %d \n",
|
2009-06-25 11:20:59 +03:00
|
|
|
(ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2009-06-26 06:38:32 +03:00
|
|
|
|
|
|
|
int debug_memory(int obj, unsigned int start, unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int buffer[8],tmp;
|
|
|
|
|
|
|
|
tmp = usb_get_ingenic_cpu(&ingenic_dev);
|
|
|
|
if (tmp > 2) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" This command only run under UNBOOT state!\n");
|
2009-06-26 06:38:32 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tmp) {
|
|
|
|
case 1:
|
|
|
|
tmp = 0;
|
|
|
|
hand.fw_args.cpu_id = 0x4740;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tmp = 0;
|
|
|
|
hand.fw_args.cpu_id = 0x4750;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hand.fw_args.debug_ops = 1;/* tell device it's memory debug */
|
|
|
|
hand.fw_args.start = start;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
hand.fw_args.size = total_size;
|
|
|
|
else
|
|
|
|
hand.fw_args.size = size;
|
|
|
|
|
2009-06-28 10:35:20 +03:00
|
|
|
printf(" Now test memory from 0x%x to 0x%x: \n",
|
2009-06-26 06:38:32 +03:00
|
|
|
start, start + hand.fw_args.size);
|
|
|
|
|
|
|
|
if (load_file(&ingenic_dev, STAGE1_FILE_PATH) < 1)
|
|
|
|
return -1;
|
|
|
|
if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
usleep(100);
|
|
|
|
usb_read_data_from_ingenic(&ingenic_dev, buffer, 8);
|
|
|
|
if (buffer[0] != 0)
|
2009-06-28 10:35:20 +03:00
|
|
|
printf(" Test memory fail! Last error address is 0x%x !\n",
|
2009-06-26 06:38:32 +03:00
|
|
|
buffer[0]);
|
|
|
|
else
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Test memory pass!\n");
|
2009-06-26 06:38:32 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int debug_gpio(int obj, unsigned char ops, unsigned char pin)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
tmp = usb_get_ingenic_cpu(&ingenic_dev);
|
|
|
|
if (tmp > 2) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" This command only run under UNBOOT state!\n");
|
2009-06-26 06:38:32 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tmp) {
|
|
|
|
case 1:
|
|
|
|
tmp = 0;
|
|
|
|
hand.fw_args.cpu_id = 0x4740;
|
|
|
|
if (pin > 124) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Jz4740 has 124 GPIO pin in all!\n");
|
2009-06-26 06:38:32 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tmp = 0;
|
|
|
|
hand.fw_args.cpu_id = 0x4750;
|
|
|
|
if (pin > 178) {
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" Jz4750 has 178 GPIO pin in all!\n");
|
2009-06-26 06:38:32 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hand.fw_args.debug_ops = ops;/* tell device it's memory debug */
|
|
|
|
hand.fw_args.pin_num = pin;
|
|
|
|
|
|
|
|
if (ops == 2)
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" GPIO %d set!\n",pin);
|
2009-06-26 06:38:32 +03:00
|
|
|
else
|
2009-06-28 06:38:48 +03:00
|
|
|
printf(" GPIO %d clear!\n",pin);
|
2009-06-26 06:38:32 +03:00
|
|
|
|
|
|
|
if (load_file(&ingenic_dev, STAGE1_FILE_PATH) < 1)
|
|
|
|
return -1;
|
|
|
|
if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-28 10:35:20 +03:00
|
|
|
int debug_go(void)
|
|
|
|
{
|
|
|
|
unsigned int addr,obj;
|
|
|
|
if (com_argc<3) {
|
|
|
|
printf(" Usage: go (1) (2) \n"
|
|
|
|
" 1:start SDRAM address\n"
|
|
|
|
" 2:device index number\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = strtoul(com_argv[1], NULL, 0);
|
|
|
|
obj = atoi(com_argv[2]);
|
|
|
|
|
|
|
|
printf(" Executing No.%d device at address 0x%x\n", obj, addr);
|
|
|
|
|
|
|
|
if (usb_ingenic_start(&ingenic_dev, VR_PROGRAM_START2, addr) < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sdram_load(struct sdram_in *sdram_in)
|
|
|
|
{
|
|
|
|
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
|
|
|
|
printf(" Device unboot! Boot it first!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdram_in->length > (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;
|
|
|
|
}
|