cmd_nand.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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(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. /* FIXME: skip bad blocks */
  474. r = nand_read(nand, offset, &cnt, (u_char *) addr);
  475. if (r) {
  476. puts("** Read error\n");
  477. show_boot_progress (-58);
  478. return 1;
  479. }
  480. show_boot_progress (58);
  481. #if defined(CONFIG_FIT)
  482. /* This cannot be done earlier, we need complete FIT image in RAM first */
  483. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  484. if (!fit_check_format (fit_hdr)) {
  485. show_boot_progress (-150);
  486. puts ("** Bad FIT image format\n");
  487. return 1;
  488. }
  489. show_boot_progress (151);
  490. fit_print_contents (fit_hdr);
  491. }
  492. #endif
  493. /* Loading ok, update default load address */
  494. load_addr = addr;
  495. /* Check if we should attempt an auto-start */
  496. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
  497. char *local_args[2];
  498. extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
  499. local_args[0] = cmd;
  500. local_args[1] = NULL;
  501. printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
  502. do_bootm(cmdtp, 0, 1, local_args);
  503. return 1;
  504. }
  505. return 0;
  506. }
  507. int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  508. {
  509. char *boot_device = NULL;
  510. int idx;
  511. ulong addr, offset = 0;
  512. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  513. struct mtd_device *dev;
  514. struct part_info *part;
  515. u8 pnum;
  516. if (argc >= 2) {
  517. char *p = (argc == 2) ? argv[1] : argv[2];
  518. if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
  519. (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
  520. if (dev->id->type != MTD_DEV_TYPE_NAND) {
  521. puts("Not a NAND device\n");
  522. return 1;
  523. }
  524. if (argc > 3)
  525. goto usage;
  526. if (argc == 3)
  527. addr = simple_strtoul(argv[1], NULL, 16);
  528. else
  529. addr = CONFIG_SYS_LOAD_ADDR;
  530. return nand_load_image(cmdtp, &nand_info[dev->id->num],
  531. part->offset, addr, argv[0]);
  532. }
  533. }
  534. #endif
  535. show_boot_progress(52);
  536. switch (argc) {
  537. case 1:
  538. addr = CONFIG_SYS_LOAD_ADDR;
  539. boot_device = getenv("bootdevice");
  540. break;
  541. case 2:
  542. addr = simple_strtoul(argv[1], NULL, 16);
  543. boot_device = getenv("bootdevice");
  544. break;
  545. case 3:
  546. addr = simple_strtoul(argv[1], NULL, 16);
  547. boot_device = argv[2];
  548. break;
  549. case 4:
  550. addr = simple_strtoul(argv[1], NULL, 16);
  551. boot_device = argv[2];
  552. offset = simple_strtoul(argv[3], NULL, 16);
  553. break;
  554. default:
  555. #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
  556. usage:
  557. #endif
  558. cmd_usage(cmdtp);
  559. show_boot_progress(-53);
  560. return 1;
  561. }
  562. show_boot_progress(53);
  563. if (!boot_device) {
  564. puts("\n** No boot device **\n");
  565. show_boot_progress(-54);
  566. return 1;
  567. }
  568. show_boot_progress(54);
  569. idx = simple_strtoul(boot_device, NULL, 16);
  570. if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
  571. printf("\n** Device %d not available\n", idx);
  572. show_boot_progress(-55);
  573. return 1;
  574. }
  575. show_boot_progress(55);
  576. return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
  577. }
  578. U_BOOT_CMD(nboot, 4, 1, do_nandboot,
  579. "boot from NAND device",
  580. "[partition] | [[[loadAddr] dev] offset]\n");
  581. #endif
  582. #else /* CONFIG_NAND_LEGACY */
  583. /*
  584. *
  585. * Legacy NAND support - to be phased out
  586. *
  587. */
  588. #include <command.h>
  589. #include <malloc.h>
  590. #include <asm/io.h>
  591. #include <watchdog.h>
  592. #ifdef CONFIG_show_boot_progress
  593. # include <status_led.h>
  594. # define show_boot_progress(arg) show_boot_progress(arg)
  595. #else
  596. # define show_boot_progress(arg)
  597. #endif
  598. #if defined(CONFIG_CMD_NAND)
  599. #include <linux/mtd/nand_legacy.h>
  600. #if 0
  601. #include <linux/mtd/nand_ids.h>
  602. #include <jffs2/jffs2.h>
  603. #endif
  604. #ifdef CONFIG_OMAP1510
  605. void archflashwp(void *archdata, int wp);
  606. #endif
  607. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  608. #undef NAND_DEBUG
  609. #undef PSYCHO_DEBUG
  610. /* ****************** WARNING *********************
  611. * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
  612. * erase (or at least attempt to erase) blocks that are marked
  613. * bad. This can be very handy if you are _sure_ that the block
  614. * is OK, say because you marked a good block bad to test bad
  615. * block handling and you are done testing, or if you have
  616. * accidentally marked blocks bad.
  617. *
  618. * Erasing factory marked bad blocks is a _bad_ idea. If the
  619. * erase succeeds there is no reliable way to find them again,
  620. * and attempting to program or erase bad blocks can affect
  621. * the data in _other_ (good) blocks.
  622. */
  623. #define ALLOW_ERASE_BAD_DEBUG 0
  624. #define CONFIG_MTD_NAND_ECC /* enable ECC */
  625. #define CONFIG_MTD_NAND_ECC_JFFS2
  626. /* bits for nand_legacy_rw() `cmd'; or together as needed */
  627. #define NANDRW_READ 0x01
  628. #define NANDRW_WRITE 0x00
  629. #define NANDRW_JFFS2 0x02
  630. #define NANDRW_JFFS2_SKIP 0x04
  631. /*
  632. * Imports from nand_legacy.c
  633. */
  634. extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
  635. extern int curr_device;
  636. extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
  637. size_t len, int clean);
  638. extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
  639. size_t len, size_t *retlen, u_char *buf);
  640. extern void nand_print(struct nand_chip *nand);
  641. extern void nand_print_bad(struct nand_chip *nand);
  642. extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
  643. size_t len, size_t *retlen, u_char *buf);
  644. extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
  645. size_t len, size_t *retlen, const u_char *buf);
  646. int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  647. {
  648. int rcode = 0;
  649. switch (argc) {
  650. case 0:
  651. case 1:
  652. cmd_usage(cmdtp);
  653. return 1;
  654. case 2:
  655. if (strcmp (argv[1], "info") == 0) {
  656. int i;
  657. putc ('\n');
  658. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; ++i) {
  659. if (nand_dev_desc[i].ChipID ==
  660. NAND_ChipID_UNKNOWN)
  661. continue; /* list only known devices */
  662. printf ("Device %d: ", i);
  663. nand_print (&nand_dev_desc[i]);
  664. }
  665. return 0;
  666. } else if (strcmp (argv[1], "device") == 0) {
  667. if ((curr_device < 0)
  668. || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
  669. puts ("\nno devices available\n");
  670. return 1;
  671. }
  672. printf ("\nDevice %d: ", curr_device);
  673. nand_print (&nand_dev_desc[curr_device]);
  674. return 0;
  675. } else if (strcmp (argv[1], "bad") == 0) {
  676. if ((curr_device < 0)
  677. || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
  678. puts ("\nno devices available\n");
  679. return 1;
  680. }
  681. printf ("\nDevice %d bad blocks:\n", curr_device);
  682. nand_print_bad (&nand_dev_desc[curr_device]);
  683. return 0;
  684. }
  685. cmd_usage(cmdtp);
  686. return 1;
  687. case 3:
  688. if (strcmp (argv[1], "device") == 0) {
  689. int dev = (int) simple_strtoul (argv[2], NULL, 10);
  690. printf ("\nDevice %d: ", dev);
  691. if (dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  692. puts ("unknown device\n");
  693. return 1;
  694. }
  695. nand_print (&nand_dev_desc[dev]);
  696. /*nand_print (dev); */
  697. if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
  698. return 1;
  699. }
  700. curr_device = dev;
  701. puts ("... is now current device\n");
  702. return 0;
  703. } else if (strcmp (argv[1], "erase") == 0
  704. && strcmp (argv[2], "clean") == 0) {
  705. struct nand_chip *nand = &nand_dev_desc[curr_device];
  706. ulong off = 0;
  707. ulong size = nand->totlen;
  708. int ret;
  709. printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
  710. ret = nand_legacy_erase (nand, off, size, 1);
  711. printf ("%s\n", ret ? "ERROR" : "OK");
  712. return ret;
  713. }
  714. cmd_usage(cmdtp);
  715. return 1;
  716. default:
  717. /* at least 4 args */
  718. if (strncmp (argv[1], "read", 4) == 0 ||
  719. strncmp (argv[1], "write", 5) == 0) {
  720. ulong addr = simple_strtoul (argv[2], NULL, 16);
  721. off_t off = simple_strtoul (argv[3], NULL, 16);
  722. size_t size = simple_strtoul (argv[4], NULL, 16);
  723. int cmd = (strncmp (argv[1], "read", 4) == 0) ?
  724. NANDRW_READ : NANDRW_WRITE;
  725. size_t total;
  726. int ret;
  727. char *cmdtail = strchr (argv[1], '.');
  728. if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
  729. /* read out-of-band data */
  730. if (cmd & NANDRW_READ) {
  731. ret = nand_read_oob (nand_dev_desc + curr_device,
  732. off, size, &total,
  733. (u_char *) addr);
  734. } else {
  735. ret = nand_write_oob (nand_dev_desc + curr_device,
  736. off, size, &total,
  737. (u_char *) addr);
  738. }
  739. return ret;
  740. } else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 7)) {
  741. cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
  742. if (cmd & NANDRW_READ)
  743. cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
  744. } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
  745. cmd |= NANDRW_JFFS2; /* skip bad blocks */
  746. #ifdef SXNI855T
  747. /* need ".e" same as ".j" for compatibility with older units */
  748. else if (cmdtail && !strcmp (cmdtail, ".e"))
  749. cmd |= NANDRW_JFFS2; /* skip bad blocks */
  750. #endif
  751. #ifdef CONFIG_SYS_NAND_SKIP_BAD_DOT_I
  752. /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
  753. /* ".i" for image -> read skips bad block (no 0xff) */
  754. else if (cmdtail && !strcmp (cmdtail, ".i")) {
  755. cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
  756. if (cmd & NANDRW_READ)
  757. cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
  758. }
  759. #endif /* CONFIG_SYS_NAND_SKIP_BAD_DOT_I */
  760. else if (cmdtail) {
  761. cmd_usage(cmdtp);
  762. return 1;
  763. }
  764. printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
  765. (cmd & NANDRW_READ) ? "read" : "write",
  766. curr_device, off, (ulong)size);
  767. ret = nand_legacy_rw (nand_dev_desc + curr_device,
  768. cmd, off, size,
  769. &total, (u_char *) addr);
  770. printf (" %d bytes %s: %s\n", total,
  771. (cmd & NANDRW_READ) ? "read" : "written",
  772. ret ? "ERROR" : "OK");
  773. return ret;
  774. } else if (strcmp (argv[1], "erase") == 0 &&
  775. (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
  776. int clean = argc == 5;
  777. ulong off =
  778. simple_strtoul (argv[2 + clean], NULL, 16);
  779. ulong size =
  780. simple_strtoul (argv[3 + clean], NULL, 16);
  781. int ret;
  782. printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
  783. curr_device, off, size);
  784. ret = nand_legacy_erase (nand_dev_desc + curr_device,
  785. off, size, clean);
  786. printf ("%s\n", ret ? "ERROR" : "OK");
  787. return ret;
  788. } else {
  789. cmd_usage(cmdtp);
  790. rcode = 1;
  791. }
  792. return rcode;
  793. }
  794. }
  795. U_BOOT_CMD(
  796. nand, 5, 1, do_nand,
  797. "legacy NAND sub-system",
  798. "info - show available NAND devices\n"
  799. "nand device [dev] - show or set current device\n"
  800. "nand read[.jffs2[s]] addr off size\n"
  801. "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
  802. " at offset `off' to/from memory address `addr'\n"
  803. "nand erase [clean] [off size] - erase `size' bytes from\n"
  804. " offset `off' (entire device if not specified)\n"
  805. "nand bad - show bad blocks\n"
  806. "nand read.oob addr off size - read out-of-band data\n"
  807. "nand write.oob addr off size - read out-of-band data\n"
  808. );
  809. int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  810. {
  811. char *boot_device = NULL;
  812. char *ep;
  813. int dev;
  814. ulong cnt;
  815. ulong addr;
  816. ulong offset = 0;
  817. image_header_t *hdr;
  818. int rcode = 0;
  819. #if defined(CONFIG_FIT)
  820. const void *fit_hdr = NULL;
  821. #endif
  822. show_boot_progress (52);
  823. switch (argc) {
  824. case 1:
  825. addr = CONFIG_SYS_LOAD_ADDR;
  826. boot_device = getenv ("bootdevice");
  827. break;
  828. case 2:
  829. addr = simple_strtoul(argv[1], NULL, 16);
  830. boot_device = getenv ("bootdevice");
  831. break;
  832. case 3:
  833. addr = simple_strtoul(argv[1], NULL, 16);
  834. boot_device = argv[2];
  835. break;
  836. case 4:
  837. addr = simple_strtoul(argv[1], NULL, 16);
  838. boot_device = argv[2];
  839. offset = simple_strtoul(argv[3], NULL, 16);
  840. break;
  841. default:
  842. cmd_usage(cmdtp);
  843. show_boot_progress (-53);
  844. return 1;
  845. }
  846. show_boot_progress (53);
  847. if (!boot_device) {
  848. puts ("\n** No boot device **\n");
  849. show_boot_progress (-54);
  850. return 1;
  851. }
  852. show_boot_progress (54);
  853. dev = simple_strtoul(boot_device, &ep, 16);
  854. if ((dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
  855. (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
  856. printf ("\n** Device %d not available\n", dev);
  857. show_boot_progress (-55);
  858. return 1;
  859. }
  860. show_boot_progress (55);
  861. printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
  862. dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
  863. offset);
  864. if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
  865. SECTORSIZE, NULL, (u_char *)addr)) {
  866. printf ("** Read error on %d\n", dev);
  867. show_boot_progress (-56);
  868. return 1;
  869. }
  870. show_boot_progress (56);
  871. switch (genimg_get_format ((void *)addr)) {
  872. case IMAGE_FORMAT_LEGACY:
  873. hdr = (image_header_t *)addr;
  874. image_print_contents (hdr);
  875. cnt = image_get_image_size (hdr);
  876. cnt -= SECTORSIZE;
  877. break;
  878. #if defined(CONFIG_FIT)
  879. case IMAGE_FORMAT_FIT:
  880. fit_hdr = (const void *)addr;
  881. puts ("Fit image detected...\n");
  882. cnt = fit_get_size (fit_hdr);
  883. break;
  884. #endif
  885. default:
  886. show_boot_progress (-57);
  887. puts ("** Unknown image type\n");
  888. return 1;
  889. }
  890. show_boot_progress (57);
  891. if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
  892. offset + SECTORSIZE, cnt, NULL,
  893. (u_char *)(addr+SECTORSIZE))) {
  894. printf ("** Read error on %d\n", dev);
  895. show_boot_progress (-58);
  896. return 1;
  897. }
  898. show_boot_progress (58);
  899. #if defined(CONFIG_FIT)
  900. /* This cannot be done earlier, we need complete FIT image in RAM first */
  901. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  902. if (!fit_check_format (fit_hdr)) {
  903. show_boot_progress (-150);
  904. puts ("** Bad FIT image format\n");
  905. return 1;
  906. }
  907. show_boot_progress (151);
  908. fit_print_contents (fit_hdr);
  909. }
  910. #endif
  911. /* Loading ok, update default load address */
  912. load_addr = addr;
  913. /* Check if we should attempt an auto-start */
  914. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  915. char *local_args[2];
  916. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  917. local_args[0] = argv[0];
  918. local_args[1] = NULL;
  919. printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
  920. do_bootm (cmdtp, 0, 1, local_args);
  921. rcode = 1;
  922. }
  923. return rcode;
  924. }
  925. U_BOOT_CMD(
  926. nboot, 4, 1, do_nandboot,
  927. "boot from NAND device",
  928. "loadAddr dev\n"
  929. );
  930. #endif
  931. #endif /* CONFIG_NAND_LEGACY */