cmd_nand.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Driver for NAND support, Rick Bronson
  3. * borrowed heavily from:
  4. * (c) 1999 Machine Vision Holdings, Inc.
  5. * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * Added 16-bit nand support
  8. * (C) 2004 Texas Instruments
  9. */
  10. #include <common.h>
  11. #ifndef CONFIG_NAND_LEGACY
  12. /*
  13. *
  14. * New NAND support
  15. *
  16. */
  17. #include <common.h>
  18. #include <linux/mtd/mtd.h>
  19. #if defined(CONFIG_CMD_NAND)
  20. #include <command.h>
  21. #include <watchdog.h>
  22. #include <malloc.h>
  23. #include <asm/byteorder.h>
  24. #include <jffs2/jffs2.h>
  25. #include <nand.h>
  26. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  27. /* parition handling routines */
  28. int mtdparts_init(void);
  29. int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
  30. int find_dev_and_part(const char *id, struct mtd_device **dev,
  31. u8 *part_num, struct part_info **part);
  32. #endif
  33. static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
  34. {
  35. int i;
  36. u_char *datbuf, *oobbuf, *p;
  37. datbuf = malloc(nand->writesize + nand->oobsize);
  38. oobbuf = malloc(nand->oobsize);
  39. if (!datbuf || !oobbuf) {
  40. puts("No memory for page buffer\n");
  41. return 1;
  42. }
  43. off &= ~(nand->writesize - 1);
  44. loff_t addr = (loff_t) off;
  45. struct mtd_oob_ops ops;
  46. memset(&ops, 0, sizeof(ops));
  47. ops.datbuf = datbuf;
  48. ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
  49. ops.len = nand->writesize;
  50. ops.ooblen = nand->oobsize;
  51. ops.mode = MTD_OOB_RAW;
  52. i = nand->read_oob(nand, addr, &ops);
  53. if (i < 0) {
  54. printf("Error (%d) reading page %08lx\n", i, off);
  55. free(datbuf);
  56. free(oobbuf);
  57. return 1;
  58. }
  59. printf("Page %08lx dump:\n", off);
  60. i = nand->writesize >> 4;
  61. p = datbuf;
  62. while (i--) {
  63. if (!only_oob)
  64. printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
  65. " %02x %02x %02x %02x %02x %02x %02x %02x\n",
  66. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
  67. p[8], p[9], p[10], p[11], p[12], p[13], p[14],
  68. p[15]);
  69. p += 16;
  70. }
  71. puts("OOB:\n");
  72. i = nand->oobsize >> 3;
  73. while (i--) {
  74. printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
  75. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
  76. p += 8;
  77. }
  78. free(datbuf);
  79. free(oobbuf);
  80. return 0;
  81. }
  82. /* ------------------------------------------------------------------------- */
  83. static inline int str2long(char *p, ulong *num)
  84. {
  85. char *endptr;
  86. *num = simple_strtoul(p, &endptr, 16);
  87. return (*p != '\0' && *endptr == '\0') ? 1 : 0;
  88. }
  89. static int
  90. arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
  91. {
  92. int idx = nand_curr_device;
  93. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  94. struct mtd_device *dev;
  95. struct part_info *part;
  96. u8 pnum;
  97. if (argc >= 1 && !(str2long(argv[0], off))) {
  98. if ((mtdparts_init() == 0) &&
  99. (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
  100. if (dev->id->type != MTD_DEV_TYPE_NAND) {
  101. puts("not a NAND device\n");
  102. return -1;
  103. }
  104. *off = part->offset;
  105. if (argc >= 2) {
  106. if (!(str2long(argv[1], (ulong *)size))) {
  107. printf("'%s' is not a number\n", argv[1]);
  108. return -1;
  109. }
  110. if (*size > part->size)
  111. *size = part->size;
  112. } else {
  113. *size = part->size;
  114. }
  115. idx = dev->id->num;
  116. *nand = nand_info[idx];
  117. goto out;
  118. }
  119. }
  120. #endif
  121. if (argc >= 1) {
  122. if (!(str2long(argv[0], off))) {
  123. printf("'%s' is not a number\n", argv[0]);
  124. return -1;
  125. }
  126. } else {
  127. *off = 0;
  128. }
  129. if (argc >= 2) {
  130. if (!(str2long(argv[1], (ulong *)size))) {
  131. printf("'%s' is not a number\n", argv[1]);
  132. return -1;
  133. }
  134. } else {
  135. *size = nand->size - *off;
  136. }
  137. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  138. out:
  139. #endif
  140. printf("device %d ", idx);
  141. if (*size == nand->size)
  142. puts("whole chip\n");
  143. else
  144. printf("offset 0x%lx, size 0x%zx\n", *off, *size);
  145. return 0;
  146. }
  147. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  148. static void print_status(ulong start, ulong end, ulong erasesize, int status)
  149. {
  150. printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
  151. start,
  152. end - 1,
  153. (end - start) / erasesize,
  154. ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
  155. ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
  156. ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
  157. }
  158. static void do_nand_status(nand_info_t *nand)
  159. {
  160. ulong block_start = 0;
  161. ulong off;
  162. int last_status = -1;
  163. struct nand_chip *nand_chip = nand->priv;
  164. /* check the WP bit */
  165. nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
  166. printf("device is %swrite protected\n",
  167. (nand_chip->read_byte(nand) & 0x80 ?
  168. "NOT " : ""));
  169. for (off = 0; off < nand->size; off += nand->erasesize) {
  170. int s = nand_get_lock_status(nand, off);
  171. /* print message only if status has changed */
  172. if (s != last_status && off != 0) {
  173. print_status(block_start, off, nand->erasesize,
  174. last_status);
  175. block_start = off;
  176. }
  177. last_status = s;
  178. }
  179. /* Print the last block info */
  180. print_status(block_start, off, nand->erasesize, last_status);
  181. }
  182. #endif
  183. int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  184. {
  185. int i, dev, ret = 0;
  186. ulong addr, off;
  187. size_t size;
  188. char *cmd, *s;
  189. nand_info_t *nand;
  190. #ifdef CONFIG_SYS_NAND_QUIET
  191. int quiet = CONFIG_SYS_NAND_QUIET;
  192. #else
  193. int quiet = 0;
  194. #endif
  195. const char *quiet_str = getenv("quiet");
  196. /* at least two arguments please */
  197. if (argc < 2)
  198. goto usage;
  199. if (quiet_str)
  200. quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
  201. cmd = argv[1];
  202. if (strcmp(cmd, "info") == 0) {
  203. putc('\n');
  204. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  205. if (nand_info[i].name)
  206. printf("Device %d: %s, sector size %u KiB\n",
  207. i, nand_info[i].name,
  208. nand_info[i].erasesize >> 10);
  209. }
  210. return 0;
  211. }
  212. if (strcmp(cmd, "device") == 0) {
  213. if (argc < 3) {
  214. if ((nand_curr_device < 0) ||
  215. (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
  216. puts("\nno devices available\n");
  217. else
  218. printf("\nDevice %d: %s\n", nand_curr_device,
  219. nand_info[nand_curr_device].name);
  220. return 0;
  221. }
  222. dev = (int)simple_strtoul(argv[2], NULL, 10);
  223. if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
  224. puts("No such device\n");
  225. return 1;
  226. }
  227. printf("Device %d: %s", dev, nand_info[dev].name);
  228. puts("... is now current device\n");
  229. nand_curr_device = dev;
  230. #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
  231. /*
  232. * Select the chip in the board/cpu specific driver
  233. */
  234. board_nand_select_device(nand_info[dev].priv, dev);
  235. #endif
  236. return 0;
  237. }
  238. if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
  239. strncmp(cmd, "dump", 4) != 0 &&
  240. strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
  241. strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
  242. strcmp(cmd, "biterr") != 0 &&
  243. strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
  244. goto usage;
  245. /* the following commands operate on the current device */
  246. if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  247. !nand_info[nand_curr_device].name) {
  248. puts("\nno devices available\n");
  249. return 1;
  250. }
  251. nand = &nand_info[nand_curr_device];
  252. if (strcmp(cmd, "bad") == 0) {
  253. printf("\nDevice %d bad blocks:\n", nand_curr_device);
  254. for (off = 0; off < nand->size; off += nand->erasesize)
  255. if (nand_block_isbad(nand, off))
  256. printf(" %08lx\n", off);
  257. return 0;
  258. }
  259. /*
  260. * Syntax is:
  261. * 0 1 2 3 4
  262. * nand erase [clean] [off size]
  263. */
  264. if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
  265. nand_erase_options_t opts;
  266. /* "clean" at index 2 means request to write cleanmarker */
  267. int clean = argc > 2 && !strcmp("clean", argv[2]);
  268. int o = clean ? 3 : 2;
  269. int scrub = !strcmp(cmd, "scrub");
  270. printf("\nNAND %s: ", scrub ? "scrub" : "erase");
  271. /* skip first two or three arguments, look for offset and size */
  272. if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
  273. return 1;
  274. memset(&opts, 0, sizeof(opts));
  275. opts.offset = off;
  276. opts.length = size;
  277. opts.jffs2 = clean;
  278. opts.quiet = quiet;
  279. if (scrub) {
  280. puts("Warning: "
  281. "scrub option will erase all factory set "
  282. "bad blocks!\n"
  283. " "
  284. "There is no reliable way to recover them.\n"
  285. " "
  286. "Use this command only for testing purposes "
  287. "if you\n"
  288. " "
  289. "are sure of what you are doing!\n"
  290. "\nReally scrub this NAND flash? <y/N>\n");
  291. if (getc() == 'y' && getc() == '\r') {
  292. opts.scrub = 1;
  293. } else {
  294. puts("scrub aborted\n");
  295. return -1;
  296. }
  297. }
  298. ret = nand_erase_opts(nand, &opts);
  299. printf("%s\n", ret ? "ERROR" : "OK");
  300. return ret == 0 ? 0 : 1;
  301. }
  302. if (strncmp(cmd, "dump", 4) == 0) {
  303. if (argc < 3)
  304. goto usage;
  305. s = strchr(cmd, '.');
  306. off = (int)simple_strtoul(argv[2], NULL, 16);
  307. if (s != NULL && strcmp(s, ".oob") == 0)
  308. ret = nand_dump(nand, off, 1);
  309. else
  310. ret = nand_dump(nand, off, 0);
  311. return ret == 0 ? 1 : 0;
  312. }
  313. if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
  314. int read;
  315. if (argc < 4)
  316. goto usage;
  317. addr = (ulong)simple_strtoul(argv[2], NULL, 16);
  318. read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
  319. printf("\nNAND %s: ", read ? "read" : "write");
  320. if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
  321. return 1;
  322. s = strchr(cmd, '.');
  323. if (!s || !strcmp(s, ".jffs2") ||
  324. !strcmp(s, ".e") || !strcmp(s, ".i")) {
  325. if (read)
  326. ret = nand_read_skip_bad(nand, off, &size,
  327. (u_char *)addr);
  328. else
  329. ret = nand_write_skip_bad(nand, off, &size,
  330. (u_char *)addr);
  331. } else if (s != NULL && !strcmp(s, ".oob")) {
  332. /* out-of-band data */
  333. mtd_oob_ops_t ops = {
  334. .oobbuf = (u8 *)addr,
  335. .ooblen = size,
  336. .mode = MTD_OOB_RAW
  337. };
  338. if (read)
  339. ret = nand->read_oob(nand, off, &ops);
  340. else
  341. ret = nand->write_oob(nand, off, &ops);
  342. } else {
  343. printf("Unknown nand command suffix '%s'.\n", s);
  344. return 1;
  345. }
  346. printf(" %zu bytes %s: %s\n", size,
  347. read ? "read" : "written", ret ? "ERROR" : "OK");
  348. return ret == 0 ? 0 : 1;
  349. }
  350. if (strcmp(cmd, "markbad") == 0) {
  351. addr = (ulong)simple_strtoul(argv[2], NULL, 16);
  352. int ret = nand->block_markbad(nand, addr);
  353. if (ret == 0) {
  354. printf("block 0x%08lx successfully marked as bad\n",
  355. (ulong) addr);
  356. return 0;
  357. } else {
  358. printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
  359. (ulong) addr, ret);
  360. }
  361. return 1;
  362. }
  363. if (strcmp(cmd, "biterr") == 0) {
  364. /* todo */
  365. return 1;
  366. }
  367. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  368. if (strcmp(cmd, "lock") == 0) {
  369. int tight = 0;
  370. int status = 0;
  371. if (argc == 3) {
  372. if (!strcmp("tight", argv[2]))
  373. tight = 1;
  374. if (!strcmp("status", argv[2]))
  375. status = 1;
  376. }
  377. if (status) {
  378. do_nand_status(nand);
  379. } else {
  380. if (!nand_lock(nand, tight)) {
  381. puts("NAND flash successfully locked\n");
  382. } else {
  383. puts("Error locking NAND flash\n");
  384. return 1;
  385. }
  386. }
  387. return 0;
  388. }
  389. if (strcmp(cmd, "unlock") == 0) {
  390. if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
  391. return 1;
  392. if (!nand_unlock(nand, off, size)) {
  393. puts("NAND flash successfully unlocked\n");
  394. } else {
  395. puts("Error unlocking NAND flash, "
  396. "write and erase will probably fail\n");
  397. return 1;
  398. }
  399. return 0;
  400. }
  401. #endif
  402. usage:
  403. cmd_usage(cmdtp);
  404. return 1;
  405. }
  406. U_BOOT_CMD(nand, 5, 1, do_nand,
  407. "NAND sub-system",
  408. "info - show available NAND devices\n"
  409. "nand device [dev] - show or set current device\n"
  410. "nand read - addr off|partition size\n"
  411. "nand write - addr off|partition size\n"
  412. " read/write 'size' bytes starting at offset 'off'\n"
  413. " to/from memory address 'addr', skipping bad blocks.\n"
  414. "nand erase [clean] [off size] - erase 'size' bytes from\n"
  415. " offset 'off' (entire device if not specified)\n"
  416. "nand bad - show bad blocks\n"
  417. "nand dump[.oob] off - dump page\n"
  418. "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
  419. "nand markbad off - mark bad block at offset (UNSAFE)\n"
  420. "nand biterr off - make a bit error at offset (UNSAFE)\n"
  421. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  422. "nand lock [tight] [status]\n"
  423. " bring nand to lock state or display locked pages\n"
  424. "nand unlock [offset] [size] - unlock section\n"
  425. #endif
  426. );
  427. static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
  428. ulong offset, ulong addr, char *cmd)
  429. {
  430. int r;
  431. char *ep, *s;
  432. size_t cnt;
  433. image_header_t *hdr;
  434. #if defined(CONFIG_FIT)
  435. const void *fit_hdr = NULL;
  436. #endif
  437. s = strchr(cmd, '.');
  438. if (s != NULL &&
  439. (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) {
  440. printf("Unknown nand load suffix '%s'\n", s);
  441. show_boot_progress(-53);
  442. return 1;
  443. }
  444. printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
  445. cnt = nand->writesize;
  446. r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
  447. if (r) {
  448. puts("** Read error\n");
  449. show_boot_progress (-56);
  450. return 1;
  451. }
  452. show_boot_progress (56);
  453. switch (genimg_get_format ((void *)addr)) {
  454. case IMAGE_FORMAT_LEGACY:
  455. hdr = (image_header_t *)addr;
  456. show_boot_progress (57);
  457. image_print_contents (hdr);
  458. cnt = image_get_image_size (hdr);
  459. break;
  460. #if defined(CONFIG_FIT)
  461. case IMAGE_FORMAT_FIT:
  462. fit_hdr = (const void *)addr;
  463. puts ("Fit image detected...\n");
  464. cnt = fit_get_size (fit_hdr);
  465. break;
  466. #endif
  467. default:
  468. show_boot_progress (-57);
  469. puts ("** Unknown image type\n");
  470. return 1;
  471. }
  472. show_boot_progress (57);
  473. r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
  474. if (r) {
  475. puts("** Read error\n");
  476. show_boot_progress (-58);
  477. return 1;
  478. }
  479. show_boot_progress (58);
  480. #if defined(CONFIG_FIT)
  481. /* This cannot be done earlier, we need complete FIT image in RAM first */
  482. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  483. if (!fit_check_format (fit_hdr)) {
  484. show_boot_progress (-150);
  485. puts ("** Bad FIT image format\n");
  486. return 1;
  487. }
  488. show_boot_progress (151);
  489. fit_print_contents (fit_hdr);
  490. }
  491. #endif
  492. /* Loading ok, update default load address */
  493. load_addr = addr;
  494. /* Check if we should attempt an auto-start */
  495. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
  496. char *local_args[2];
  497. extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
  498. local_args[0] = cmd;
  499. local_args[1] = NULL;
  500. printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
  501. do_bootm(cmdtp, 0, 1, local_args);
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  507. {
  508. char *boot_device = NULL;
  509. int idx;
  510. ulong addr, offset = 0;
  511. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  512. struct mtd_device *dev;
  513. struct part_info *part;
  514. u8 pnum;
  515. if (argc >= 2) {
  516. char *p = (argc == 2) ? argv[1] : argv[2];
  517. if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
  518. (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
  519. if (dev->id->type != MTD_DEV_TYPE_NAND) {
  520. puts("Not a NAND device\n");
  521. return 1;
  522. }
  523. if (argc > 3)
  524. goto usage;
  525. if (argc == 3)
  526. addr = simple_strtoul(argv[1], NULL, 16);
  527. else
  528. addr = CONFIG_SYS_LOAD_ADDR;
  529. return nand_load_image(cmdtp, &nand_info[dev->id->num],
  530. part->offset, addr, argv[0]);
  531. }
  532. }
  533. #endif
  534. show_boot_progress(52);
  535. switch (argc) {
  536. case 1:
  537. addr = CONFIG_SYS_LOAD_ADDR;
  538. boot_device = getenv("bootdevice");
  539. break;
  540. case 2:
  541. addr = simple_strtoul(argv[1], NULL, 16);
  542. boot_device = getenv("bootdevice");
  543. break;
  544. case 3:
  545. addr = simple_strtoul(argv[1], NULL, 16);
  546. boot_device = argv[2];
  547. break;
  548. case 4:
  549. addr = simple_strtoul(argv[1], NULL, 16);
  550. boot_device = argv[2];
  551. offset = simple_strtoul(argv[3], NULL, 16);
  552. break;
  553. default:
  554. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  555. usage:
  556. #endif
  557. cmd_usage(cmdtp);
  558. show_boot_progress(-53);
  559. return 1;
  560. }
  561. show_boot_progress(53);
  562. if (!boot_device) {
  563. puts("\n** No boot device **\n");
  564. show_boot_progress(-54);
  565. return 1;
  566. }
  567. show_boot_progress(54);
  568. idx = simple_strtoul(boot_device, NULL, 16);
  569. if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
  570. printf("\n** Device %d not available\n", idx);
  571. show_boot_progress(-55);
  572. return 1;
  573. }
  574. show_boot_progress(55);
  575. return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
  576. }
  577. U_BOOT_CMD(nboot, 4, 1, do_nandboot,
  578. "boot from NAND device",
  579. "[partition] | [[[loadAddr] dev] offset]\n");
  580. #endif
  581. #else /* CONFIG_NAND_LEGACY */
  582. /*
  583. *
  584. * Legacy NAND support - to be phased out
  585. *
  586. */
  587. #include <command.h>
  588. #include <malloc.h>
  589. #include <asm/io.h>
  590. #include <watchdog.h>
  591. #ifdef CONFIG_show_boot_progress
  592. # include <status_led.h>
  593. # define show_boot_progress(arg) show_boot_progress(arg)
  594. #else
  595. # define show_boot_progress(arg)
  596. #endif
  597. #if defined(CONFIG_CMD_NAND)
  598. #include <linux/mtd/nand_legacy.h>
  599. #if 0
  600. #include <linux/mtd/nand_ids.h>
  601. #include <jffs2/jffs2.h>
  602. #endif
  603. #ifdef CONFIG_OMAP1510
  604. void archflashwp(void *archdata, int wp);
  605. #endif
  606. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  607. #undef NAND_DEBUG
  608. #undef PSYCHO_DEBUG
  609. /* ****************** WARNING *********************
  610. * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
  611. * erase (or at least attempt to erase) blocks that are marked
  612. * bad. This can be very handy if you are _sure_ that the block
  613. * is OK, say because you marked a good block bad to test bad
  614. * block handling and you are done testing, or if you have
  615. * accidentally marked blocks bad.
  616. *
  617. * Erasing factory marked bad blocks is a _bad_ idea. If the
  618. * erase succeeds there is no reliable way to find them again,
  619. * and attempting to program or erase bad blocks can affect
  620. * the data in _other_ (good) blocks.
  621. */
  622. #define ALLOW_ERASE_BAD_DEBUG 0
  623. #define CONFIG_MTD_NAND_ECC /* enable ECC */
  624. #define CONFIG_MTD_NAND_ECC_JFFS2
  625. /* bits for nand_legacy_rw() `cmd'; or together as needed */
  626. #define NANDRW_READ 0x01
  627. #define NANDRW_WRITE 0x00
  628. #define NANDRW_JFFS2 0x02
  629. #define NANDRW_JFFS2_SKIP 0x04
  630. /*
  631. * Imports from nand_legacy.c
  632. */
  633. extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
  634. extern int curr_device;
  635. extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
  636. size_t len, int clean);
  637. extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
  638. size_t len, size_t *retlen, u_char *buf);
  639. extern void nand_print(struct nand_chip *nand);
  640. extern void nand_print_bad(struct nand_chip *nand);
  641. extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
  642. size_t len, size_t *retlen, u_char *buf);
  643. extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
  644. size_t len, size_t *retlen, const u_char *buf);
  645. int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  646. {
  647. int rcode = 0;
  648. switch (argc) {
  649. case 0:
  650. case 1:
  651. cmd_usage(cmdtp);
  652. return 1;
  653. case 2:
  654. if (strcmp (argv[1], "info") == 0) {
  655. int i;
  656. putc ('\n');
  657. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; ++i) {
  658. if (nand_dev_desc[i].ChipID ==
  659. NAND_ChipID_UNKNOWN)
  660. continue; /* list only known devices */
  661. printf ("Device %d: ", i);
  662. nand_print (&nand_dev_desc[i]);
  663. }
  664. return 0;
  665. } else if (strcmp (argv[1], "device") == 0) {
  666. if ((curr_device < 0)
  667. || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
  668. puts ("\nno devices available\n");
  669. return 1;
  670. }
  671. printf ("\nDevice %d: ", curr_device);
  672. nand_print (&nand_dev_desc[curr_device]);
  673. return 0;
  674. } else if (strcmp (argv[1], "bad") == 0) {
  675. if ((curr_device < 0)
  676. || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
  677. puts ("\nno devices available\n");
  678. return 1;
  679. }
  680. printf ("\nDevice %d bad blocks:\n", curr_device);
  681. nand_print_bad (&nand_dev_desc[curr_device]);
  682. return 0;
  683. }
  684. cmd_usage(cmdtp);
  685. return 1;
  686. case 3:
  687. if (strcmp (argv[1], "device") == 0) {
  688. int dev = (int) simple_strtoul (argv[2], NULL, 10);
  689. printf ("\nDevice %d: ", dev);
  690. if (dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  691. puts ("unknown device\n");
  692. return 1;
  693. }
  694. nand_print (&nand_dev_desc[dev]);
  695. /*nand_print (dev); */
  696. if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
  697. return 1;
  698. }
  699. curr_device = dev;
  700. puts ("... is now current device\n");
  701. return 0;
  702. } else if (strcmp (argv[1], "erase") == 0
  703. && strcmp (argv[2], "clean") == 0) {
  704. struct nand_chip *nand = &nand_dev_desc[curr_device];
  705. ulong off = 0;
  706. ulong size = nand->totlen;
  707. int ret;
  708. printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
  709. ret = nand_legacy_erase (nand, off, size, 1);
  710. printf ("%s\n", ret ? "ERROR" : "OK");
  711. return ret;
  712. }
  713. cmd_usage(cmdtp);
  714. return 1;
  715. default:
  716. /* at least 4 args */
  717. if (strncmp (argv[1], "read", 4) == 0 ||
  718. strncmp (argv[1], "write", 5) == 0) {
  719. ulong addr = simple_strtoul (argv[2], NULL, 16);
  720. off_t off = simple_strtoul (argv[3], NULL, 16);
  721. size_t size = simple_strtoul (argv[4], NULL, 16);
  722. int cmd = (strncmp (argv[1], "read", 4) == 0) ?
  723. NANDRW_READ : NANDRW_WRITE;
  724. size_t total;
  725. int ret;
  726. char *cmdtail = strchr (argv[1], '.');
  727. if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
  728. /* read out-of-band data */
  729. if (cmd & NANDRW_READ) {
  730. ret = nand_read_oob (nand_dev_desc + curr_device,
  731. off, size, &total,
  732. (u_char *) addr);
  733. } else {
  734. ret = nand_write_oob (nand_dev_desc + curr_device,
  735. off, size, &total,
  736. (u_char *) addr);
  737. }
  738. return ret;
  739. } else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 7)) {
  740. cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
  741. if (cmd & NANDRW_READ)
  742. cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
  743. } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
  744. cmd |= NANDRW_JFFS2; /* skip bad blocks */
  745. #ifdef SXNI855T
  746. /* need ".e" same as ".j" for compatibility with older units */
  747. else if (cmdtail && !strcmp (cmdtail, ".e"))
  748. cmd |= NANDRW_JFFS2; /* skip bad blocks */
  749. #endif
  750. #ifdef CONFIG_SYS_NAND_SKIP_BAD_DOT_I
  751. /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
  752. /* ".i" for image -> read skips bad block (no 0xff) */
  753. else if (cmdtail && !strcmp (cmdtail, ".i")) {
  754. cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
  755. if (cmd & NANDRW_READ)
  756. cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
  757. }
  758. #endif /* CONFIG_SYS_NAND_SKIP_BAD_DOT_I */
  759. else if (cmdtail) {
  760. cmd_usage(cmdtp);
  761. return 1;
  762. }
  763. printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
  764. (cmd & NANDRW_READ) ? "read" : "write",
  765. curr_device, off, (ulong)size);
  766. ret = nand_legacy_rw (nand_dev_desc + curr_device,
  767. cmd, off, size,
  768. &total, (u_char *) addr);
  769. printf (" %d bytes %s: %s\n", total,
  770. (cmd & NANDRW_READ) ? "read" : "written",
  771. ret ? "ERROR" : "OK");
  772. return ret;
  773. } else if (strcmp (argv[1], "erase") == 0 &&
  774. (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
  775. int clean = argc == 5;
  776. ulong off =
  777. simple_strtoul (argv[2 + clean], NULL, 16);
  778. ulong size =
  779. simple_strtoul (argv[3 + clean], NULL, 16);
  780. int ret;
  781. printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
  782. curr_device, off, size);
  783. ret = nand_legacy_erase (nand_dev_desc + curr_device,
  784. off, size, clean);
  785. printf ("%s\n", ret ? "ERROR" : "OK");
  786. return ret;
  787. } else {
  788. cmd_usage(cmdtp);
  789. rcode = 1;
  790. }
  791. return rcode;
  792. }
  793. }
  794. U_BOOT_CMD(
  795. nand, 5, 1, do_nand,
  796. "legacy NAND sub-system",
  797. "info - show available NAND devices\n"
  798. "nand device [dev] - show or set current device\n"
  799. "nand read[.jffs2[s]] addr off size\n"
  800. "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
  801. " at offset `off' to/from memory address `addr'\n"
  802. "nand erase [clean] [off size] - erase `size' bytes from\n"
  803. " offset `off' (entire device if not specified)\n"
  804. "nand bad - show bad blocks\n"
  805. "nand read.oob addr off size - read out-of-band data\n"
  806. "nand write.oob addr off size - read out-of-band data\n"
  807. );
  808. int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  809. {
  810. char *boot_device = NULL;
  811. char *ep;
  812. int dev;
  813. ulong cnt;
  814. ulong addr;
  815. ulong offset = 0;
  816. image_header_t *hdr;
  817. int rcode = 0;
  818. #if defined(CONFIG_FIT)
  819. const void *fit_hdr = NULL;
  820. #endif
  821. show_boot_progress (52);
  822. switch (argc) {
  823. case 1:
  824. addr = CONFIG_SYS_LOAD_ADDR;
  825. boot_device = getenv ("bootdevice");
  826. break;
  827. case 2:
  828. addr = simple_strtoul(argv[1], NULL, 16);
  829. boot_device = getenv ("bootdevice");
  830. break;
  831. case 3:
  832. addr = simple_strtoul(argv[1], NULL, 16);
  833. boot_device = argv[2];
  834. break;
  835. case 4:
  836. addr = simple_strtoul(argv[1], NULL, 16);
  837. boot_device = argv[2];
  838. offset = simple_strtoul(argv[3], NULL, 16);
  839. break;
  840. default:
  841. cmd_usage(cmdtp);
  842. show_boot_progress (-53);
  843. return 1;
  844. }
  845. show_boot_progress (53);
  846. if (!boot_device) {
  847. puts ("\n** No boot device **\n");
  848. show_boot_progress (-54);
  849. return 1;
  850. }
  851. show_boot_progress (54);
  852. dev = simple_strtoul(boot_device, &ep, 16);
  853. if ((dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
  854. (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
  855. printf ("\n** Device %d not available\n", dev);
  856. show_boot_progress (-55);
  857. return 1;
  858. }
  859. show_boot_progress (55);
  860. printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
  861. dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
  862. offset);
  863. if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
  864. SECTORSIZE, NULL, (u_char *)addr)) {
  865. printf ("** Read error on %d\n", dev);
  866. show_boot_progress (-56);
  867. return 1;
  868. }
  869. show_boot_progress (56);
  870. switch (genimg_get_format ((void *)addr)) {
  871. case IMAGE_FORMAT_LEGACY:
  872. hdr = (image_header_t *)addr;
  873. image_print_contents (hdr);
  874. cnt = image_get_image_size (hdr);
  875. cnt -= SECTORSIZE;
  876. break;
  877. #if defined(CONFIG_FIT)
  878. case IMAGE_FORMAT_FIT:
  879. fit_hdr = (const void *)addr;
  880. puts ("Fit image detected...\n");
  881. cnt = fit_get_size (fit_hdr);
  882. break;
  883. #endif
  884. default:
  885. show_boot_progress (-57);
  886. puts ("** Unknown image type\n");
  887. return 1;
  888. }
  889. show_boot_progress (57);
  890. if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
  891. offset + SECTORSIZE, cnt, NULL,
  892. (u_char *)(addr+SECTORSIZE))) {
  893. printf ("** Read error on %d\n", dev);
  894. show_boot_progress (-58);
  895. return 1;
  896. }
  897. show_boot_progress (58);
  898. #if defined(CONFIG_FIT)
  899. /* This cannot be done earlier, we need complete FIT image in RAM first */
  900. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  901. if (!fit_check_format (fit_hdr)) {
  902. show_boot_progress (-150);
  903. puts ("** Bad FIT image format\n");
  904. return 1;
  905. }
  906. show_boot_progress (151);
  907. fit_print_contents (fit_hdr);
  908. }
  909. #endif
  910. /* Loading ok, update default load address */
  911. load_addr = addr;
  912. /* Check if we should attempt an auto-start */
  913. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  914. char *local_args[2];
  915. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  916. local_args[0] = argv[0];
  917. local_args[1] = NULL;
  918. printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
  919. do_bootm (cmdtp, 0, 1, local_args);
  920. rcode = 1;
  921. }
  922. return rcode;
  923. }
  924. U_BOOT_CMD(
  925. nboot, 4, 1, do_nandboot,
  926. "boot from NAND device",
  927. "loadAddr dev\n"
  928. );
  929. #endif
  930. #endif /* CONFIG_NAND_LEGACY */