cmd_onenand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * U-Boot command for OneNAND support
  3. *
  4. * Copyright (C) 2005-2008 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <malloc.h>
  14. #include <linux/mtd/compat.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/onenand.h>
  17. #include <asm/io.h>
  18. static struct mtd_info *mtd;
  19. static loff_t next_ofs;
  20. static loff_t skip_ofs;
  21. static inline int str2long(char *p, ulong *num)
  22. {
  23. char *endptr;
  24. *num = simple_strtoul(p, &endptr, 16);
  25. return (*p != '\0' && *endptr == '\0') ? 1 : 0;
  26. }
  27. static int arg_off_size(int argc, char *argv[], ulong *off, size_t *size)
  28. {
  29. if (argc >= 1) {
  30. if (!(str2long(argv[0], off))) {
  31. printf("'%s' is not a number\n", argv[0]);
  32. return -1;
  33. }
  34. } else {
  35. *off = 0;
  36. }
  37. if (argc >= 2) {
  38. if (!(str2long(argv[1], (ulong *)size))) {
  39. printf("'%s' is not a number\n", argv[1]);
  40. return -1;
  41. }
  42. } else {
  43. *size = mtd->size - *off;
  44. }
  45. if ((*off + *size) > mtd->size) {
  46. printf("total chip size (0x%llx) exceeded!\n", mtd->size);
  47. return -1;
  48. }
  49. if (*size == mtd->size)
  50. puts("whole chip\n");
  51. else
  52. printf("offset 0x%lx, size 0x%x\n", *off, *size);
  53. return 0;
  54. }
  55. static int onenand_block_read(loff_t from, size_t len,
  56. size_t *retlen, u_char *buf, int oob)
  57. {
  58. struct onenand_chip *this = mtd->priv;
  59. int blocks = (int) len >> this->erase_shift;
  60. int blocksize = (1 << this->erase_shift);
  61. loff_t ofs = from;
  62. struct mtd_oob_ops ops = {
  63. .retlen = 0,
  64. };
  65. int ret;
  66. if (oob)
  67. ops.ooblen = blocksize;
  68. else
  69. ops.len = blocksize;
  70. while (blocks) {
  71. ret = mtd->block_isbad(mtd, ofs);
  72. if (ret) {
  73. printk("Bad blocks %d at 0x%x\n",
  74. (u32)(ofs >> this->erase_shift), (u32)ofs);
  75. ofs += blocksize;
  76. continue;
  77. }
  78. if (oob)
  79. ops.oobbuf = buf;
  80. else
  81. ops.datbuf = buf;
  82. ops.retlen = 0;
  83. ret = mtd->read_oob(mtd, ofs, &ops);
  84. if (ret) {
  85. printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
  86. ofs += blocksize;
  87. continue;
  88. }
  89. ofs += blocksize;
  90. buf += blocksize;
  91. blocks--;
  92. *retlen += ops.retlen;
  93. }
  94. return 0;
  95. }
  96. static int onenand_block_write(loff_t to, size_t len,
  97. size_t *retlen, const u_char * buf)
  98. {
  99. struct onenand_chip *this = mtd->priv;
  100. int blocks = len >> this->erase_shift;
  101. int blocksize = (1 << this->erase_shift);
  102. loff_t ofs;
  103. size_t _retlen = 0;
  104. int ret;
  105. if (to == next_ofs) {
  106. next_ofs = to + len;
  107. to += skip_ofs;
  108. } else {
  109. next_ofs = to + len;
  110. skip_ofs = 0;
  111. }
  112. ofs = to;
  113. while (blocks) {
  114. ret = mtd->block_isbad(mtd, ofs);
  115. if (ret) {
  116. printk("Bad blocks %d at 0x%x\n",
  117. (u32)(ofs >> this->erase_shift), (u32)ofs);
  118. skip_ofs += blocksize;
  119. goto next;
  120. }
  121. ret = mtd->write(mtd, ofs, blocksize, &_retlen, buf);
  122. if (ret) {
  123. printk("Write failed 0x%x, %d", (u32)ofs, ret);
  124. skip_ofs += blocksize;
  125. goto next;
  126. }
  127. buf += blocksize;
  128. blocks--;
  129. *retlen += _retlen;
  130. next:
  131. ofs += blocksize;
  132. }
  133. return 0;
  134. }
  135. static int onenand_block_erase(u32 start, u32 size, int force)
  136. {
  137. struct onenand_chip *this = mtd->priv;
  138. struct erase_info instr = {
  139. .callback = NULL,
  140. };
  141. loff_t ofs;
  142. int ret;
  143. int blocksize = 1 << this->erase_shift;
  144. for (ofs = start; ofs < (start + size); ofs += blocksize) {
  145. ret = mtd->block_isbad(mtd, ofs);
  146. if (ret && !force) {
  147. printf("Skip erase bad block %d at 0x%x\n",
  148. (u32)(ofs >> this->erase_shift), (u32)ofs);
  149. continue;
  150. }
  151. instr.addr = ofs;
  152. instr.len = blocksize;
  153. instr.priv = force;
  154. instr.mtd = mtd;
  155. ret = mtd->erase(mtd, &instr);
  156. if (ret) {
  157. printf("erase failed block %d at 0x%x\n",
  158. (u32)(ofs >> this->erase_shift), (u32)ofs);
  159. continue;
  160. }
  161. }
  162. return 0;
  163. }
  164. static int onenand_block_test(u32 start, u32 size)
  165. {
  166. struct onenand_chip *this = mtd->priv;
  167. struct erase_info instr = {
  168. .callback = NULL,
  169. .priv = 0,
  170. };
  171. int blocks;
  172. loff_t ofs;
  173. int blocksize = 1 << this->erase_shift;
  174. int start_block, end_block;
  175. size_t retlen;
  176. u_char *buf;
  177. u_char *verify_buf;
  178. int ret;
  179. buf = malloc(blocksize);
  180. if (!buf) {
  181. printf("Not enough malloc space available!\n");
  182. return -1;
  183. }
  184. verify_buf = malloc(blocksize);
  185. if (!verify_buf) {
  186. printf("Not enough malloc space available!\n");
  187. return -1;
  188. }
  189. start_block = start >> this->erase_shift;
  190. end_block = (start + size) >> this->erase_shift;
  191. /* Protect boot-loader from badblock testing */
  192. if (start_block < 2)
  193. start_block = 2;
  194. if (end_block > (mtd->size >> this->erase_shift))
  195. end_block = mtd->size >> this->erase_shift;
  196. blocks = start_block;
  197. ofs = start;
  198. while (blocks < end_block) {
  199. printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
  200. ret = mtd->block_isbad(mtd, ofs);
  201. if (ret) {
  202. printf("Skip erase bad block %d at 0x%x\n",
  203. (u32)(ofs >> this->erase_shift), (u32)ofs);
  204. goto next;
  205. }
  206. instr.addr = ofs;
  207. instr.len = blocksize;
  208. ret = mtd->erase(mtd, &instr);
  209. if (ret) {
  210. printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
  211. goto next;
  212. }
  213. ret = mtd->write(mtd, ofs, blocksize, &retlen, buf);
  214. if (ret) {
  215. printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
  216. goto next;
  217. }
  218. ret = mtd->read(mtd, ofs, blocksize, &retlen, verify_buf);
  219. if (ret) {
  220. printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
  221. goto next;
  222. }
  223. if (memcmp(buf, verify_buf, blocksize))
  224. printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
  225. next:
  226. ofs += blocksize;
  227. blocks++;
  228. }
  229. printf("...Done\n");
  230. free(buf);
  231. free(verify_buf);
  232. return 0;
  233. }
  234. static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
  235. {
  236. int i;
  237. u_char *datbuf, *oobbuf, *p;
  238. struct mtd_oob_ops ops;
  239. loff_t addr;
  240. datbuf = malloc(mtd->writesize + mtd->oobsize);
  241. oobbuf = malloc(mtd->oobsize);
  242. if (!datbuf || !oobbuf) {
  243. puts("No memory for page buffer\n");
  244. return 1;
  245. }
  246. off &= ~(mtd->writesize - 1);
  247. addr = (loff_t) off;
  248. memset(&ops, 0, sizeof(ops));
  249. ops.datbuf = datbuf;
  250. ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
  251. ops.len = mtd->writesize;
  252. ops.ooblen = mtd->oobsize;
  253. ops.retlen = 0;
  254. i = mtd->read_oob(mtd, addr, &ops);
  255. if (i < 0) {
  256. printf("Error (%d) reading page %08lx\n", i, off);
  257. free(datbuf);
  258. free(oobbuf);
  259. return 1;
  260. }
  261. printf("Page %08lx dump:\n", off);
  262. i = mtd->writesize >> 4;
  263. p = datbuf;
  264. while (i--) {
  265. if (!only_oob)
  266. printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
  267. " %02x %02x %02x %02x %02x %02x %02x %02x\n",
  268. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
  269. p[8], p[9], p[10], p[11], p[12], p[13], p[14],
  270. p[15]);
  271. p += 16;
  272. }
  273. puts("OOB:\n");
  274. i = mtd->oobsize >> 3;
  275. while (i--) {
  276. printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
  277. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
  278. p += 8;
  279. }
  280. free(datbuf);
  281. free(oobbuf);
  282. return 0;
  283. }
  284. static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  285. {
  286. printf("%s\n", mtd->name);
  287. return 0;
  288. }
  289. static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  290. {
  291. ulong ofs;
  292. mtd = &onenand_mtd;
  293. /* Currently only one OneNAND device is supported */
  294. printf("\nDevice %d bad blocks:\n", 0);
  295. for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
  296. if (mtd->block_isbad(mtd, ofs))
  297. printf(" %08x\n", (u32)ofs);
  298. }
  299. return 0;
  300. }
  301. static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  302. {
  303. char *s;
  304. int oob = 0;
  305. ulong addr, ofs;
  306. size_t len;
  307. int ret = 0;
  308. size_t retlen = 0;
  309. if (argc < 3)
  310. {
  311. cmd_usage(cmdtp);
  312. return 1;
  313. }
  314. s = strchr(argv[0], '.');
  315. if ((s != NULL) && (!strcmp(s, ".oob")))
  316. oob = 1;
  317. addr = (ulong)simple_strtoul(argv[1], NULL, 16);
  318. printf("\nOneNAND read: ");
  319. if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
  320. return 1;
  321. ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
  322. printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
  323. return ret == 0 ? 0 : 1;
  324. }
  325. static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  326. {
  327. ulong addr, ofs;
  328. size_t len;
  329. int ret = 0;
  330. size_t retlen = 0;
  331. if (argc < 3)
  332. {
  333. cmd_usage(cmdtp);
  334. return 1;
  335. }
  336. addr = (ulong)simple_strtoul(argv[1], NULL, 16);
  337. printf("\nOneNAND write: ");
  338. if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
  339. return 1;
  340. ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr);
  341. printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
  342. return ret == 0 ? 0 : 1;
  343. }
  344. static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  345. {
  346. ulong ofs;
  347. int ret = 0;
  348. size_t len;
  349. int force;
  350. /*
  351. * Syntax is:
  352. * 0 1 2 3 4
  353. * onenand erase [force] [off size]
  354. */
  355. argc--;
  356. argv++;
  357. if (argc)
  358. {
  359. if (!strcmp("force", argv[0]))
  360. {
  361. force = 1;
  362. argc--;
  363. argv++;
  364. }
  365. }
  366. printf("\nOneNAND erase: ");
  367. /* skip first two or three arguments, look for offset and size */
  368. if (arg_off_size(argc, argv, &ofs, &len) != 0)
  369. return 1;
  370. ret = onenand_block_erase(ofs, len, force);
  371. printf("%s\n", ret ? "ERROR" : "OK");
  372. return ret == 0 ? 0 : 1;
  373. }
  374. static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  375. {
  376. ulong ofs;
  377. int ret = 0;
  378. size_t len;
  379. /*
  380. * Syntax is:
  381. * 0 1 2 3 4
  382. * onenand test [force] [off size]
  383. */
  384. printf("\nOneNAND test: ");
  385. /* skip first two or three arguments, look for offset and size */
  386. if (arg_off_size(argc - 1, argv + 1, &ofs, &len) != 0)
  387. return 1;
  388. ret = onenand_block_test(ofs, len);
  389. printf("%s\n", ret ? "ERROR" : "OK");
  390. return ret == 0 ? 0 : 1;
  391. }
  392. static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  393. {
  394. ulong ofs;
  395. int ret = 0;
  396. char *s;
  397. if (argc < 2)
  398. {
  399. cmd_usage(cmdtp);
  400. return 1;
  401. }
  402. s = strchr(argv[0], '.');
  403. ofs = (int)simple_strtoul(argv[1], NULL, 16);
  404. if (s != NULL && strcmp(s, ".oob") == 0)
  405. ret = onenand_dump(mtd, ofs, 1);
  406. else
  407. ret = onenand_dump(mtd, ofs, 0);
  408. return ret == 0 ? 1 : 0;
  409. }
  410. static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  411. {
  412. int ret = 0;
  413. ulong addr;
  414. argc -= 2;
  415. argv += 2;
  416. if (argc <= 0)
  417. {
  418. cmd_usage(cmdtp);
  419. return 1;
  420. }
  421. while (argc > 0) {
  422. addr = simple_strtoul(*argv, NULL, 16);
  423. if (mtd->block_markbad(mtd, addr)) {
  424. printf("block 0x%08lx NOT marked "
  425. "as bad! ERROR %d\n",
  426. addr, ret);
  427. ret = 1;
  428. } else {
  429. printf("block 0x%08lx successfully "
  430. "marked as bad\n",
  431. addr);
  432. }
  433. --argc;
  434. ++argv;
  435. }
  436. return ret;
  437. }
  438. static cmd_tbl_t cmd_onenand_sub[] = {
  439. U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
  440. U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
  441. U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
  442. U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
  443. U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
  444. U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
  445. U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
  446. U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
  447. };
  448. static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  449. {
  450. cmd_tbl_t *c;
  451. mtd = &onenand_mtd;
  452. /* Strip off leading 'onenand' command argument */
  453. argc--;
  454. argv++;
  455. c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
  456. if (c) {
  457. return c->cmd(cmdtp, flag, argc, argv);
  458. } else {
  459. cmd_usage(cmdtp);
  460. return 1;
  461. }
  462. }
  463. U_BOOT_CMD(
  464. onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
  465. "OneNAND sub-system",
  466. "info - show available OneNAND devices\n"
  467. "onenand bad - show bad blocks\n"
  468. "onenand read[.oob] addr off size\n"
  469. "onenand write addr off size\n"
  470. " read/write 'size' bytes starting at offset 'off'\n"
  471. " to/from memory address 'addr', skipping bad blocks.\n"
  472. "onenand erase [force] [off size] - erase 'size' bytes from\n"
  473. "onenand test [off size] - test 'size' bytes from\n"
  474. " offset 'off' (entire device if not specified)\n"
  475. "onenand dump[.oob] off - dump page\n"
  476. "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
  477. );