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