| 1 | /* |
| 2 | * Copyright(C) 2009 Qi Hardware Inc., |
| 3 | * Authors: Marek Lindner <lindner_marek@yahoo.de> |
| 4 | * Xiangfu Liu <xiangfu@sharism.cc> |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <ctype.h> |
| 29 | #include "cmd.h" |
| 30 | #include "ingenic_cfg.h" |
| 31 | #include "ingenic_usb.h" |
| 32 | #include "ingenic_request.h" |
| 33 | #include "usb_boot_defines.h" |
| 34 | |
| 35 | extern int com_argc; |
| 36 | extern char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH]; |
| 37 | extern char * stage1; |
| 38 | |
| 39 | struct ingenic_dev ingenic_dev; |
| 40 | struct hand hand; |
| 41 | struct sdram_in sdram_in; |
| 42 | struct nand_in nand_in; |
| 43 | |
| 44 | unsigned int total_size; |
| 45 | unsigned char code_buf[4 * 512 * 1024]; |
| 46 | unsigned char check_buf[4 * 512 * 1024]; |
| 47 | unsigned char cs[16]; |
| 48 | unsigned char ret[8]; |
| 49 | |
| 50 | static const char IMAGE_TYPE[][30] = { |
| 51 | "with oob and ecc", |
| 52 | "with oob and without ecc", |
| 53 | "without oob", |
| 54 | }; |
| 55 | |
| 56 | static int load_file(struct ingenic_dev *ingenic_dev, const char *file_path) |
| 57 | { |
| 58 | struct stat fstat; |
| 59 | int fd, status, res = -1; |
| 60 | |
| 61 | status = stat(file_path, &fstat); |
| 62 | |
| 63 | if (status < 0) { |
| 64 | fprintf(stderr, "Error - can't get file size from '%s': %s\n", |
| 65 | file_path, strerror(errno)); |
| 66 | goto out; |
| 67 | } |
| 68 | |
| 69 | ingenic_dev->file_len = fstat.st_size; |
| 70 | ingenic_dev->file_buff = code_buf; |
| 71 | |
| 72 | fd = open(file_path, O_RDONLY); |
| 73 | |
| 74 | if (fd < 0) { |
| 75 | fprintf(stderr, "Error - can't open file '%s': %s\n", |
| 76 | file_path, strerror(errno)); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | status = read(fd, ingenic_dev->file_buff, ingenic_dev->file_len); |
| 81 | |
| 82 | if (status < ingenic_dev->file_len) { |
| 83 | fprintf(stderr, "Error - can't read file '%s': %s\n", |
| 84 | file_path, strerror(errno)); |
| 85 | goto close; |
| 86 | } |
| 87 | |
| 88 | /* write args to code */ |
| 89 | memcpy(ingenic_dev->file_buff + 8, &hand.fw_args, |
| 90 | sizeof(struct fw_args)); |
| 91 | |
| 92 | res = 1; |
| 93 | |
| 94 | close: |
| 95 | close(fd); |
| 96 | out: |
| 97 | return res; |
| 98 | } |
| 99 | |
| 100 | int get_ingenic_cpu() |
| 101 | { |
| 102 | int status; |
| 103 | |
| 104 | status = usb_get_ingenic_cpu(&ingenic_dev); |
| 105 | |
| 106 | switch (status) { |
| 107 | case JZ4740V1: |
| 108 | hand.fw_args.cpu_id = 0x4740; |
| 109 | break; |
| 110 | case JZ4750V1: |
| 111 | hand.fw_args.cpu_id = 0x4750; |
| 112 | break; |
| 113 | case JZ4760V1: |
| 114 | hand.fw_args.cpu_id = 0x4760; |
| 115 | break; |
| 116 | case BOOT4740: |
| 117 | hand.fw_args.cpu_id = 0x4740; |
| 118 | break; |
| 119 | case BOOT4750: |
| 120 | hand.fw_args.cpu_id = 0x4750; |
| 121 | break; |
| 122 | case BOOT4760: |
| 123 | hand.fw_args.cpu_id = 0x4760; |
| 124 | break; |
| 125 | default: |
| 126 | hand.fw_args.cpu_id = 0; |
| 127 | } |
| 128 | |
| 129 | return status; |
| 130 | } |
| 131 | |
| 132 | /* after upload stage2. must init device */ |
| 133 | void init_cfg() |
| 134 | { |
| 135 | int cpu = get_ingenic_cpu(); |
| 136 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 137 | printf(" Device unboot! Boot it first!\n"); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | ingenic_dev.file_buff = &hand; |
| 142 | ingenic_dev.file_len = sizeof(hand); |
| 143 | if (usb_send_data_to_ingenic(&ingenic_dev) != 1) |
| 144 | goto xout; |
| 145 | |
| 146 | sleep(1); |
| 147 | if (usb_ingenic_configration(&ingenic_dev, DS_hand) != 1) |
| 148 | goto xout; |
| 149 | |
| 150 | if (usb_read_data_from_ingenic(&ingenic_dev, ret, 8) != 1) |
| 151 | goto xout; |
| 152 | |
| 153 | printf(" Configuring XBurst CPU succeeded.\n"); |
| 154 | return; |
| 155 | xout: |
| 156 | printf(" Configuring XBurst CPU failed.\n"); |
| 157 | } |
| 158 | |
| 159 | int boot(char *stage1_path, char *stage2_path){ |
| 160 | int status = get_ingenic_cpu(); |
| 161 | |
| 162 | if (status == BOOT4740 || status == BOOT4750 || status == BOOT4760) { |
| 163 | printf(" Already booted.\n"); |
| 164 | return 1; |
| 165 | } else { |
| 166 | printf(" CPU not yet booted, now booting...\n"); |
| 167 | |
| 168 | /* now we upload the boot stage1 */ |
| 169 | printf(" Loading stage1 from '%s'\n", stage1_path); |
| 170 | if (load_file(&ingenic_dev, stage1_path) < 1) |
| 171 | return -1; |
| 172 | |
| 173 | if (usb_ingenic_upload(&ingenic_dev, 1) < 1) |
| 174 | return -1; |
| 175 | |
| 176 | /* now we upload the boot stage2 */ |
| 177 | usleep(100); |
| 178 | printf(" Loading stage2 from '%s'\n", stage2_path); |
| 179 | if (load_file(&ingenic_dev, stage2_path) < 1) |
| 180 | return -1; |
| 181 | |
| 182 | if (usb_ingenic_upload(&ingenic_dev, 2) < 1) |
| 183 | return -1; |
| 184 | |
| 185 | printf(" Booted successfully!\n"); |
| 186 | } |
| 187 | |
| 188 | usleep(100); |
| 189 | init_cfg(); |
| 190 | |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | /* nand function */ |
| 195 | int error_check(unsigned char *org,unsigned char * obj,unsigned int size) |
| 196 | { |
| 197 | unsigned int i; |
| 198 | printf(" Comparing %d bytes - ", size); |
| 199 | for (i = 0; i < size; i++) { |
| 200 | if (org[i] != obj[i]) { |
| 201 | unsigned int s = (i < 8) ? i : i - 8; // start_dump |
| 202 | printf("FAIL at off %d, wrote 0x%x, read 0x%x\n", i, org[i], obj[i]); |
| 203 | printf(" off %d write: %02x %02x %02x %02x %02x %02x %02x %02x" |
| 204 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", s, |
| 205 | org[s], org[s+1], org[s+2], org[s+3], org[s+4], org[s+5], org[s+6], org[s+7], |
| 206 | org[s+8], org[s+9], org[s+10], org[s+11], org[s+12], org[s+13], org[s+14], org[s+15]); |
| 207 | printf(" off %d read: %02x %02x %02x %02x %02x %02x %02x %02x" |
| 208 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", s, |
| 209 | obj[s], obj[s+1], obj[s+2], obj[s+3], obj[s+4], obj[s+5], obj[s+6], obj[s+7], |
| 210 | obj[s+8], obj[s+9], obj[s+10], obj[s+11], obj[s+12], obj[s+13], obj[s+14], obj[s+15]); |
| 211 | fflush(NULL); |
| 212 | return 0; |
| 213 | } |
| 214 | } |
| 215 | printf("SUCCESS\n"); |
| 216 | fflush(NULL); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | int nand_markbad(struct nand_in *nand_in) |
| 221 | { |
| 222 | int cpu = get_ingenic_cpu(); |
| 223 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 224 | printf(" Device unboot! Boot it first!\n"); |
| 225 | return -1; |
| 226 | } |
| 227 | printf(" mark bad block : %d\n",nand_in->start); |
| 228 | usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start); |
| 229 | usb_ingenic_nand_ops(&ingenic_dev, NAND_MARK_BAD); |
| 230 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 231 | printf(" Mark bad block at %d\n",((ret[3] << 24) | |
| 232 | (ret[2] << 16) | |
| 233 | (ret[1] << 8) | |
| 234 | (ret[0] << 0)) / hand.nand_ppb); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int nand_program_check(struct nand_in *nand_in, unsigned int *start_page) |
| 239 | { |
| 240 | unsigned int i, page_num, cur_page = -1; |
| 241 | unsigned int start_addr; |
| 242 | unsigned short temp; |
| 243 | int status = -1; |
| 244 | |
| 245 | printf(" Writing NAND page %d len %d...\n", nand_in->start, nand_in->length); |
| 246 | if (nand_in->length > (unsigned int)MAX_TRANSFER_SIZE) { |
| 247 | printf(" Buffer size too long!\n"); |
| 248 | goto err; |
| 249 | } |
| 250 | |
| 251 | int cpu = get_ingenic_cpu(); |
| 252 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 253 | printf(" Device unboot! Boot it first!\n"); |
| 254 | goto err; |
| 255 | } |
| 256 | |
| 257 | ingenic_dev.file_buff = nand_in->buf; |
| 258 | ingenic_dev.file_len = nand_in->length; |
| 259 | usb_send_data_to_ingenic(&ingenic_dev); |
| 260 | for (i = 0; i < nand_in->max_chip; i++) { |
| 261 | if ((nand_in->cs_map)[i] == 0) |
| 262 | continue; |
| 263 | if (nand_in->option == NO_OOB) { |
| 264 | page_num = nand_in->length / hand.nand_ps; |
| 265 | if ((nand_in->length % hand.nand_ps) !=0) |
| 266 | page_num++; |
| 267 | } else { |
| 268 | page_num = nand_in->length / |
| 269 | (hand.nand_ps + hand.nand_os); |
| 270 | if ((nand_in->length% (hand.nand_ps + hand.nand_os)) !=0) |
| 271 | page_num++; |
| 272 | } |
| 273 | temp = ((nand_in->option << 12) & 0xf000) + |
| 274 | ((i<<4) & 0xff0) + NAND_PROGRAM; |
| 275 | if (usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start) != 1) |
| 276 | goto err; |
| 277 | if (usb_send_data_length_to_ingenic(&ingenic_dev, page_num) != 1) |
| 278 | goto err; |
| 279 | if (usb_ingenic_nand_ops(&ingenic_dev, temp) != 1) |
| 280 | goto err; |
| 281 | if (usb_read_data_from_ingenic(&ingenic_dev, ret, 8) != 1) |
| 282 | goto err; |
| 283 | |
| 284 | printf(" Finish! (len %d start_page %d page_num %d)\n", |
| 285 | nand_in->length, nand_in->start, page_num); |
| 286 | |
| 287 | /* Read back to check! */ |
| 288 | usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start); |
| 289 | usb_send_data_length_to_ingenic(&ingenic_dev, page_num); |
| 290 | |
| 291 | switch (nand_in->option) { |
| 292 | case OOB_ECC: |
| 293 | temp = ((OOB_ECC << 12) & 0xf000) + |
| 294 | ((i << 4) & 0xff0) + NAND_READ; |
| 295 | start_addr = page_num * (hand.nand_ps + hand.nand_os); |
| 296 | break; |
| 297 | case OOB_NO_ECC: /* do not support data verify */ |
| 298 | temp = ((OOB_NO_ECC << 12) & 0xf000) + |
| 299 | ((i << 4) & 0xff0) + NAND_READ; |
| 300 | start_addr = page_num * (hand.nand_ps + hand.nand_os); |
| 301 | break; |
| 302 | case NO_OOB: |
| 303 | temp = ((NO_OOB << 12) & 0xf000) + |
| 304 | ((i << 4) & 0xff0) + NAND_READ; |
| 305 | start_addr = page_num * hand.nand_ps; |
| 306 | break; |
| 307 | default: |
| 308 | ; |
| 309 | } |
| 310 | |
| 311 | printf(" Checking %d bytes...", nand_in->length); |
| 312 | usb_ingenic_nand_ops(&ingenic_dev, temp); |
| 313 | usb_read_data_from_ingenic(&ingenic_dev, check_buf, start_addr); |
| 314 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 315 | |
| 316 | cur_page = (ret[3] << 24) | (ret[2] << 16) | (ret[1] << 8) | |
| 317 | (ret[0] << 0); |
| 318 | |
| 319 | if (nand_in->start == 0 && hand.nand_ps == 4096 && |
| 320 | hand.fw_args.cpu_id == 0x4740) { |
| 321 | printf(" no check! End at Page: %d\n", cur_page); |
| 322 | fflush(NULL); |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | if (!nand_in->check(nand_in->buf, check_buf, nand_in->length)) { |
| 327 | struct nand_in bad; |
| 328 | // tbd: doesn't the other side skip bad blocks too? Can we just deduct 1 from cur_page? |
| 329 | // tbd: why do we only mark a block as bad if the last page in the block was written? |
| 330 | bad.start = (cur_page - 1) / hand.nand_ppb; |
| 331 | if (cur_page % hand.nand_ppb == 0) |
| 332 | nand_markbad(&bad); |
| 333 | } |
| 334 | |
| 335 | printf(" End at Page: %d\n", cur_page); |
| 336 | fflush(NULL); |
| 337 | } |
| 338 | |
| 339 | *start_page = cur_page; |
| 340 | |
| 341 | status = 1; |
| 342 | err: |
| 343 | return status; |
| 344 | } |
| 345 | |
| 346 | int nand_erase(struct nand_in *nand_in) |
| 347 | { |
| 348 | unsigned int start_blk, blk_num, end_block; |
| 349 | int i; |
| 350 | |
| 351 | start_blk = nand_in->start; |
| 352 | blk_num = nand_in->length; |
| 353 | if (start_blk > (unsigned int)NAND_MAX_BLK_NUM) { |
| 354 | printf(" Start block number overflow!\n"); |
| 355 | return -1; |
| 356 | } |
| 357 | if (blk_num > (unsigned int)NAND_MAX_BLK_NUM) { |
| 358 | printf(" Length block number overflow!\n"); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | int cpu = get_ingenic_cpu(); |
| 363 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 364 | printf(" Device unboot! Boot it first!\n"); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | for (i = 0; i < nand_in->max_chip; i++) { |
| 369 | if ((nand_in->cs_map)[i]==0) |
| 370 | continue; |
| 371 | printf(" Erasing No.%d device No.%d flash (start_blk %u blk_num %u)......\n", |
| 372 | nand_in->dev, i, start_blk, blk_num); |
| 373 | |
| 374 | usb_send_data_address_to_ingenic(&ingenic_dev, start_blk); |
| 375 | usb_send_data_length_to_ingenic(&ingenic_dev, blk_num); |
| 376 | |
| 377 | unsigned short temp = ((i << 4) & 0xff0) + NAND_ERASE; |
| 378 | usb_ingenic_nand_ops(&ingenic_dev, temp); |
| 379 | |
| 380 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 381 | printf(" Finish!"); |
| 382 | } |
| 383 | end_block = ((ret[3] << 24) | |
| 384 | (ret[2] << 16) | |
| 385 | (ret[1] << 8) | |
| 386 | (ret[0] << 0)) / hand.nand_ppb; |
| 387 | printf(" Return: %02x %02x %02x %02x %02x %02x %02x %02x (position %d)\n", |
| 388 | ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], end_block); |
| 389 | if (!hand.nand_force_erase) { |
| 390 | /* not force erase, show bad block infomation */ |
| 391 | printf(" There are marked bad blocks: %d\n", |
| 392 | end_block - start_blk - blk_num ); |
| 393 | } else { |
| 394 | /* force erase, no bad block infomation can show */ |
| 395 | printf(" Force erase, no bad block infomation!\n" ); |
| 396 | } |
| 397 | |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | int nand_program_file(struct nand_in *nand_in, char *fname) |
| 402 | { |
| 403 | |
| 404 | int flen, m, j, k; |
| 405 | unsigned int start_page = 0, page_num, code_len, offset, transfer_size; |
| 406 | int fd, status; |
| 407 | struct stat fstat; |
| 408 | struct nand_in n_in; |
| 409 | |
| 410 | status = stat(fname, &fstat); |
| 411 | |
| 412 | if (status < 0) { |
| 413 | fprintf(stderr, "Error - can't get file size from '%s': %s\n", |
| 414 | fname, strerror(errno)); |
| 415 | return -1; |
| 416 | } |
| 417 | flen = fstat.st_size; |
| 418 | |
| 419 | fd = open(fname, O_RDONLY); |
| 420 | if (fd < 0) { |
| 421 | fprintf(stderr, "Error - can't open file '%s': %s\n", |
| 422 | fname, strerror(errno)); |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | printf(" Programing No.%d device, flen %d, start page %d...\n",nand_in->dev, flen, nand_in->start); |
| 427 | n_in.start = nand_in->start / hand.nand_ppb; |
| 428 | if (nand_in->option == NO_OOB) { |
| 429 | if (flen % (hand.nand_ppb * hand.nand_ps) == 0) |
| 430 | n_in.length = flen / (hand.nand_ps * hand.nand_ppb); |
| 431 | else |
| 432 | n_in.length = flen / (hand.nand_ps * hand.nand_ppb) + 1; |
| 433 | } else { |
| 434 | if (flen % (hand.nand_ppb * (hand.nand_ps + hand.nand_os)) == 0) |
| 435 | n_in.length = flen / |
| 436 | ((hand.nand_ps + hand.nand_os) * hand.nand_ppb); |
| 437 | else |
| 438 | n_in.length = flen / |
| 439 | ((hand.nand_ps + hand.nand_os) * hand.nand_ppb) |
| 440 | + 1; |
| 441 | } |
| 442 | n_in.cs_map = nand_in->cs_map; |
| 443 | n_in.dev = nand_in->dev; |
| 444 | n_in.max_chip = nand_in->max_chip; |
| 445 | if (nand_erase(&n_in) != 1) |
| 446 | return -1; |
| 447 | if (nand_in->option == NO_OOB) |
| 448 | transfer_size = (hand.nand_ppb * hand.nand_ps); |
| 449 | else |
| 450 | transfer_size = (hand.nand_ppb * (hand.nand_ps + hand.nand_os)); |
| 451 | |
| 452 | m = flen / transfer_size; |
| 453 | j = flen % transfer_size; |
| 454 | printf(" Size to send %d, transfer_size %d\n", flen, transfer_size); |
| 455 | printf(" Image type : %s\n", IMAGE_TYPE[nand_in->option]); |
| 456 | printf(" It will cause %d times buffer transfer.\n", j == 0 ? m : m + 1); |
| 457 | fflush(NULL); |
| 458 | |
| 459 | offset = 0; |
| 460 | for (k = 0; k < m; k++) { |
| 461 | if (nand_in->option == NO_OOB) |
| 462 | page_num = transfer_size / hand.nand_ps; |
| 463 | else |
| 464 | page_num = transfer_size / (hand.nand_ps + hand.nand_os); |
| 465 | |
| 466 | code_len = transfer_size; |
| 467 | status = read(fd, code_buf, code_len); |
| 468 | if (status < code_len) { |
| 469 | fprintf(stderr, "Error - can't read file '%s': %s\n", |
| 470 | fname, strerror(errno)); |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | nand_in->length = code_len; /* code length,not page number! */ |
| 475 | nand_in->buf = code_buf; |
| 476 | if (nand_program_check(nand_in, &start_page) == -1) |
| 477 | return -1; |
| 478 | |
| 479 | if (start_page - nand_in->start > hand.nand_ppb) |
| 480 | printf(" Info - skip bad block!\n"); |
| 481 | nand_in->start = start_page; |
| 482 | |
| 483 | offset += code_len ; |
| 484 | } |
| 485 | |
| 486 | if (j) { |
| 487 | code_len = j; |
| 488 | if (j % hand.nand_ps) |
| 489 | j += hand.nand_ps - (j % hand.nand_ps); |
| 490 | memset(code_buf, 0, j); /* set all to null */ |
| 491 | |
| 492 | status = read(fd, code_buf, code_len); |
| 493 | |
| 494 | if (status < code_len) { |
| 495 | fprintf(stderr, "Error - can't read file '%s': %s\n", |
| 496 | fname, strerror(errno)); |
| 497 | return -1; |
| 498 | } |
| 499 | |
| 500 | nand_in->length = j; |
| 501 | nand_in->buf = code_buf; |
| 502 | if (nand_program_check(nand_in, &start_page) == -1) |
| 503 | return -1; |
| 504 | |
| 505 | if (start_page - nand_in->start > hand.nand_ppb) |
| 506 | printf(" Info - skip bad block!"); |
| 507 | |
| 508 | } |
| 509 | |
| 510 | close(fd); |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | int nand_program_file_planes(struct nand_in *nand_in, char *fname) |
| 515 | { |
| 516 | printf(" not implement yet !\n"); |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | int init_nand_in(void) |
| 521 | { |
| 522 | nand_in.buf = code_buf; |
| 523 | nand_in.check = error_check; |
| 524 | nand_in.dev = 0; |
| 525 | nand_in.cs_map = cs; |
| 526 | memset(nand_in.cs_map, 0, MAX_DEV_NUM); |
| 527 | |
| 528 | nand_in.max_chip = 16; |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | int nand_prog(void) |
| 533 | { |
| 534 | int status = -1; |
| 535 | char *image_file; |
| 536 | char *help = " Usage: nprog (1) (2) (3) (4) (5)\n" |
| 537 | " (1)\tstart page number\n" |
| 538 | " (2)\timage file name\n" |
| 539 | " (3)\tdevice index number\n" |
| 540 | " (4)\tflash index number\n" |
| 541 | " (5) image type must be:\n" |
| 542 | " \t-n:\tno oob\n" |
| 543 | " \t-o:\twith oob no ecc\n" |
| 544 | " \t-e:\twith oob and ecc\n"; |
| 545 | |
| 546 | if (com_argc != 6) { |
| 547 | printf("%s", help); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | init_nand_in(); |
| 552 | |
| 553 | nand_in.start = atoi(com_argv[1]); |
| 554 | image_file = com_argv[2]; |
| 555 | nand_in.dev = atoi(com_argv[3]); |
| 556 | |
| 557 | (nand_in.cs_map)[atoi(com_argv[4])] = 1; |
| 558 | if (!strcmp(com_argv[5], "-e")) |
| 559 | nand_in.option = OOB_ECC; |
| 560 | else if (!strcmp(com_argv[5], "-o")) |
| 561 | nand_in.option = OOB_NO_ECC; |
| 562 | else if (!strcmp(com_argv[5], "-n")) |
| 563 | nand_in.option = NO_OOB; |
| 564 | else |
| 565 | printf("%s", help); |
| 566 | |
| 567 | if (hand.nand_plane > 1) |
| 568 | nand_program_file_planes(&nand_in, image_file); |
| 569 | else |
| 570 | nand_program_file(&nand_in, image_file); |
| 571 | |
| 572 | status = 1; |
| 573 | err: |
| 574 | return status; |
| 575 | } |
| 576 | |
| 577 | int nand_query(void) |
| 578 | { |
| 579 | int i; |
| 580 | unsigned char csn; |
| 581 | |
| 582 | if (com_argc < 3) { |
| 583 | printf(" Usage: nquery (1) (2)\n" |
| 584 | " (1):device index number\n" |
| 585 | " (2):flash index number\n"); |
| 586 | return -1; |
| 587 | } |
| 588 | init_nand_in(); |
| 589 | |
| 590 | nand_in.dev = atoi(com_argv[1]); |
| 591 | (nand_in.cs_map)[atoi(com_argv[2])] = 1; |
| 592 | |
| 593 | for (i = 0; i < nand_in.max_chip; i++) { |
| 594 | if ((nand_in.cs_map)[i] != 0) |
| 595 | break; |
| 596 | } |
| 597 | if (i >= nand_in.max_chip) |
| 598 | return -1; |
| 599 | |
| 600 | int cpu = get_ingenic_cpu(); |
| 601 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 602 | printf(" Device unboot! Boot it first!\n"); |
| 603 | return -1; |
| 604 | } |
| 605 | |
| 606 | csn = i; |
| 607 | printf(" ID of No.%d device No.%d flash: \n", nand_in.dev, csn); |
| 608 | |
| 609 | unsigned short ops = ((csn << 4) & 0xff0) + NAND_QUERY; |
| 610 | usb_ingenic_nand_ops(&ingenic_dev, ops); |
| 611 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 612 | printf(" Vendor ID :0x%x \n",(unsigned char)ret[0]); |
| 613 | printf(" Product ID :0x%x \n",(unsigned char)ret[1]); |
| 614 | printf(" Chip ID :0x%x \n",(unsigned char)ret[2]); |
| 615 | printf(" Page ID :0x%x \n",(unsigned char)ret[3]); |
| 616 | printf(" Plane ID :0x%x \n",(unsigned char)ret[4]); |
| 617 | |
| 618 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 619 | |
| 620 | return 1; |
| 621 | } |
| 622 | |
| 623 | int nand_read(int mode) |
| 624 | { |
| 625 | unsigned int i,j; |
| 626 | unsigned int start_addr, length, page_num; |
| 627 | unsigned char csn; |
| 628 | unsigned short temp = 0; |
| 629 | unsigned ram_addr = 0; |
| 630 | |
| 631 | if (com_argc < 5) { |
| 632 | printf(" Usage: nread (1) (2) (3) (4)\n" |
| 633 | " 1:start page number\n" |
| 634 | " 2:length in byte\n" |
| 635 | " 3:device index number\n" |
| 636 | " 4:flash index number\n" |
| 637 | " 5:start SDRAM address\n"); |
| 638 | return -1; |
| 639 | } |
| 640 | init_nand_in(); |
| 641 | |
| 642 | if (atoi(com_argv[4]) >= MAX_DEV_NUM) { |
| 643 | printf(" Flash index number overflow!\n"); |
| 644 | return -1; |
| 645 | } |
| 646 | (nand_in.cs_map)[atoi(com_argv[4])] = 1; |
| 647 | nand_in.start = atoi(com_argv[1]); |
| 648 | nand_in.length= atoi(com_argv[2]); |
| 649 | nand_in.dev = atoi(com_argv[3]); |
| 650 | |
| 651 | if (com_argc = 6) { |
| 652 | ram_addr = strtoul(com_argv[5], NULL, 0); |
| 653 | printf("==%s==", com_argv[5]); |
| 654 | } |
| 655 | start_addr = nand_in.start; |
| 656 | length = nand_in.length; |
| 657 | |
| 658 | if (start_addr > NAND_MAX_PAGE_NUM || length > NAND_MAX_PAGE_NUM ) { |
| 659 | printf(" Page number overflow!\n"); |
| 660 | return -1; |
| 661 | } |
| 662 | int cpu = get_ingenic_cpu(); |
| 663 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 664 | printf(" Device unboot! Boot it first!\n"); |
| 665 | return -1; |
| 666 | } |
| 667 | for (i = 0; i < nand_in.max_chip; i++) |
| 668 | if ((nand_in.cs_map)[i] != 0) |
| 669 | break; |
| 670 | if (i >= nand_in.max_chip) return 1; |
| 671 | csn = i; |
| 672 | printf(" Reading from No.%d device No.%d flash....\n",nand_in.dev,csn); |
| 673 | |
| 674 | page_num = length / hand.nand_ps +1; |
| 675 | |
| 676 | switch(mode) { |
| 677 | case NAND_READ: |
| 678 | temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + NAND_READ; |
| 679 | break; |
| 680 | case NAND_READ_OOB: |
| 681 | temp = ((csn<<4) & 0xff0) + NAND_READ_OOB; |
| 682 | break; |
| 683 | case NAND_READ_RAW: |
| 684 | temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + |
| 685 | NAND_READ_RAW; |
| 686 | break; |
| 687 | case NAND_READ_TO_RAM: |
| 688 | temp = ((NO_OOB<<12) & 0xf000) + ((csn<<4) & 0xff0) + |
| 689 | NAND_READ_TO_RAM; |
| 690 | printf(" Reading nand to RAM: 0x%x\n", ram_addr); |
| 691 | usb_ingenic_start(&ingenic_dev, VR_PROGRAM_START1, ram_addr); |
| 692 | break; |
| 693 | default: |
| 694 | printf(" unknow mode!\n"); |
| 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | usb_send_data_address_to_ingenic(&ingenic_dev, start_addr); |
| 699 | usb_send_data_length_to_ingenic(&ingenic_dev, page_num); |
| 700 | |
| 701 | usb_ingenic_nand_ops(&ingenic_dev, temp); |
| 702 | |
| 703 | usb_read_data_from_ingenic(&ingenic_dev, nand_in.buf, page_num * hand.nand_ps); |
| 704 | |
| 705 | for (j = 0; j < length; j++) { |
| 706 | if (j % 16 == 0) |
| 707 | printf("\n 0x%08x : ",j); |
| 708 | printf("%02x ",(nand_in.buf)[j]); |
| 709 | } |
| 710 | printf("\n"); |
| 711 | |
| 712 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 713 | printf(" Operation end position : %d \n", |
| 714 | (ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0)); |
| 715 | |
| 716 | return 1; |
| 717 | } |
| 718 | |
| 719 | int debug_memory(int obj, unsigned int start, unsigned int size) |
| 720 | { |
| 721 | unsigned int buffer[8],tmp; |
| 722 | |
| 723 | tmp = get_ingenic_cpu(); |
| 724 | if (tmp == BOOT4740 || tmp == BOOT4750 || tmp == BOOT4760) { |
| 725 | printf(" This command only run under UNBOOT state!\n"); |
| 726 | return -1; |
| 727 | } |
| 728 | |
| 729 | switch (tmp) { |
| 730 | case 1: |
| 731 | tmp = 0; |
| 732 | hand.fw_args.cpu_id = 0x4740; |
| 733 | break; |
| 734 | case 2: |
| 735 | tmp = 0; |
| 736 | hand.fw_args.cpu_id = 0x4750; |
| 737 | break; |
| 738 | } |
| 739 | |
| 740 | hand.fw_args.debug_ops = 1;/* tell device it's memory debug */ |
| 741 | hand.fw_args.start = start; |
| 742 | |
| 743 | if (size == 0) |
| 744 | hand.fw_args.size = total_size; |
| 745 | else |
| 746 | hand.fw_args.size = size; |
| 747 | |
| 748 | printf(" Now test memory from 0x%x to 0x%x: \n", |
| 749 | start, start + hand.fw_args.size); |
| 750 | |
| 751 | if (load_file(&ingenic_dev, stage1) < 1) |
| 752 | return -1; |
| 753 | if (usb_ingenic_upload(&ingenic_dev, 1) < 1) |
| 754 | return -1; |
| 755 | |
| 756 | usleep(100); |
| 757 | usb_read_data_from_ingenic(&ingenic_dev, buffer, 8); |
| 758 | if (buffer[0] != 0) |
| 759 | printf(" Test memory fail! Last error address is 0x%x !\n", |
| 760 | buffer[0]); |
| 761 | else |
| 762 | printf(" Test memory pass!\n"); |
| 763 | |
| 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | int debug_gpio(int obj, unsigned char ops, unsigned char pin) |
| 768 | { |
| 769 | unsigned int tmp; |
| 770 | |
| 771 | tmp = get_ingenic_cpu(); |
| 772 | if (tmp == BOOT4740 || tmp == BOOT4750 || tmp == BOOT4760) { |
| 773 | printf(" This command only run under UNBOOT state!\n"); |
| 774 | return -1; |
| 775 | } |
| 776 | |
| 777 | switch (tmp) { |
| 778 | case 1: |
| 779 | tmp = 0; |
| 780 | hand.fw_args.cpu_id = 0x4740; |
| 781 | if (pin > 124) { |
| 782 | printf(" Jz4740 has 124 GPIO pin in all!\n"); |
| 783 | return -1; |
| 784 | } |
| 785 | break; |
| 786 | case 2: |
| 787 | tmp = 0; |
| 788 | hand.fw_args.cpu_id = 0x4750; |
| 789 | if (pin > 178) { |
| 790 | printf(" Jz4750 has 178 GPIO pin in all!\n"); |
| 791 | return -1; |
| 792 | } |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | hand.fw_args.debug_ops = ops;/* tell device it's memory debug */ |
| 797 | hand.fw_args.pin_num = pin; |
| 798 | |
| 799 | if (ops == 2) |
| 800 | printf(" GPIO %d set!\n",pin); |
| 801 | else |
| 802 | printf(" GPIO %d clear!\n",pin); |
| 803 | |
| 804 | if (load_file(&ingenic_dev, stage1) < 1) |
| 805 | return -1; |
| 806 | if (usb_ingenic_upload(&ingenic_dev, 1) < 1) |
| 807 | return -1; |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | int debug_go(void) |
| 813 | { |
| 814 | unsigned int addr,obj; |
| 815 | if (com_argc<3) { |
| 816 | printf(" Usage: go (1) (2) \n" |
| 817 | " 1:start SDRAM address\n" |
| 818 | " 2:device index number\n"); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | addr = strtoul(com_argv[1], NULL, 0); |
| 823 | obj = atoi(com_argv[2]); |
| 824 | |
| 825 | printf(" Executing No.%d device at address 0x%x\n", obj, addr); |
| 826 | |
| 827 | if (usb_ingenic_start(&ingenic_dev, VR_PROGRAM_START2, addr) < 1) |
| 828 | return -1; |
| 829 | |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | int sdram_load(struct sdram_in *sdram_in) |
| 834 | { |
| 835 | int cpu = get_ingenic_cpu(); |
| 836 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 837 | printf(" Device unboot! Boot it first!\n"); |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | if (sdram_in->length > (unsigned int) MAX_LOAD_SIZE) { |
| 842 | printf(" Image length too long!\n"); |
| 843 | return -1; |
| 844 | } |
| 845 | |
| 846 | ingenic_dev.file_buff = sdram_in->buf; |
| 847 | ingenic_dev.file_len = sdram_in->length; |
| 848 | usb_send_data_to_ingenic(&ingenic_dev); |
| 849 | usb_send_data_address_to_ingenic(&ingenic_dev, sdram_in->start); |
| 850 | usb_send_data_length_to_ingenic(&ingenic_dev, sdram_in->length); |
| 851 | usb_ingenic_sdram_ops(&ingenic_dev, sdram_in); |
| 852 | |
| 853 | usb_read_data_from_ingenic(&ingenic_dev, ret, 8); |
| 854 | printf(" Load last address at 0x%x\n", |
| 855 | ((ret[3]<<24)|(ret[2]<<16)|(ret[1]<<8)|(ret[0]<<0))); |
| 856 | |
| 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | int sdram_load_file(struct sdram_in *sdram_in, char *file_path) |
| 861 | { |
| 862 | struct stat fstat; |
| 863 | unsigned int flen,m,j,offset,k; |
| 864 | int fd, status, res = -1; |
| 865 | |
| 866 | status = stat(file_path, &fstat); |
| 867 | if (status < 0) { |
| 868 | fprintf(stderr, "Error - can't get file size from '%s': %s\n", |
| 869 | file_path, strerror(errno)); |
| 870 | goto out; |
| 871 | } |
| 872 | flen = fstat.st_size; |
| 873 | |
| 874 | fd = open(file_path, O_RDONLY); |
| 875 | if (fd < 0) { |
| 876 | fprintf(stderr, "Error - can't open file '%s': %s\n", |
| 877 | file_path, strerror(errno)); |
| 878 | goto out; |
| 879 | } |
| 880 | |
| 881 | m = flen / MAX_LOAD_SIZE; |
| 882 | j = flen % MAX_LOAD_SIZE; |
| 883 | offset = 0; |
| 884 | |
| 885 | printf(" Total size to send in byte is :%d\n", flen); |
| 886 | printf(" Loading data to SDRAM :\n"); |
| 887 | |
| 888 | for (k = 0; k < m; k++) { |
| 889 | status = read(fd, sdram_in->buf, MAX_LOAD_SIZE); |
| 890 | if (status < MAX_LOAD_SIZE) { |
| 891 | fprintf(stderr, "Error - can't read file '%s': %s\n", |
| 892 | file_path, strerror(errno)); |
| 893 | goto close; |
| 894 | } |
| 895 | |
| 896 | sdram_in->length = MAX_LOAD_SIZE; |
| 897 | if (sdram_load(sdram_in) < 1) |
| 898 | goto close; |
| 899 | |
| 900 | sdram_in->start += MAX_LOAD_SIZE; |
| 901 | if ( k % 60 == 0) |
| 902 | printf(" 0x%x \n", sdram_in->start); |
| 903 | } |
| 904 | |
| 905 | if (j) { |
| 906 | if (j % 4 !=0) |
| 907 | j += 4 - (j % 4); |
| 908 | status = read(fd, sdram_in->buf, j); |
| 909 | if (status < j) { |
| 910 | fprintf(stderr, "Error - can't read file '%s': %s\n", |
| 911 | file_path, strerror(errno)); |
| 912 | goto close; |
| 913 | } |
| 914 | |
| 915 | sdram_in->length = j; |
| 916 | if (sdram_load(sdram_in) < 1) |
| 917 | goto close; |
| 918 | } |
| 919 | |
| 920 | res = 1; |
| 921 | |
| 922 | close: |
| 923 | close(fd); |
| 924 | out: |
| 925 | return res; |
| 926 | } |
| 927 | |
| 928 | int device_reset(int ops) |
| 929 | { |
| 930 | int cpu = get_ingenic_cpu(); |
| 931 | if (cpu != BOOT4740 && cpu != BOOT4750 && cpu != BOOT4760) { |
| 932 | printf(" Device unboot! Boot it first!\n"); |
| 933 | return -1; |
| 934 | } |
| 935 | |
| 936 | if (usb_ingenic_reset(&ingenic_dev, ops) < 1) |
| 937 | return -1; |
| 938 | |
| 939 | return 1; |
| 940 | } |
| 941 | |