cmd_onenand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 * const 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 * const 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 * const 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 * const 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. return cmd_usage(cmdtp);
  311. s = strchr(argv[0], '.');
  312. if ((s != NULL) && (!strcmp(s, ".oob")))
  313. oob = 1;
  314. addr = (ulong)simple_strtoul(argv[1], NULL, 16);
  315. printf("\nOneNAND read: ");
  316. if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
  317. return 1;
  318. ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
  319. printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
  320. return ret == 0 ? 0 : 1;
  321. }
  322. static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  323. {
  324. ulong addr, ofs;
  325. size_t len;
  326. int ret = 0;
  327. size_t retlen = 0;
  328. if (argc < 3)
  329. return cmd_usage(cmdtp);
  330. addr = (ulong)simple_strtoul(argv[1], NULL, 16);
  331. printf("\nOneNAND write: ");
  332. if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
  333. return 1;
  334. ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr);
  335. printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
  336. return ret == 0 ? 0 : 1;
  337. }
  338. static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  339. {
  340. ulong ofs;
  341. int ret = 0;
  342. size_t len;
  343. int force;
  344. /*
  345. * Syntax is:
  346. * 0 1 2 3 4
  347. * onenand erase [force] [off size]
  348. */
  349. argc--;
  350. argv++;
  351. if (argc)
  352. {
  353. if (!strcmp("force", argv[0]))
  354. {
  355. force = 1;
  356. argc--;
  357. argv++;
  358. }
  359. }
  360. printf("\nOneNAND erase: ");
  361. /* skip first two or three arguments, look for offset and size */
  362. if (arg_off_size(argc, argv, &ofs, &len) != 0)
  363. return 1;
  364. ret = onenand_block_erase(ofs, len, force);
  365. printf("%s\n", ret ? "ERROR" : "OK");
  366. return ret == 0 ? 0 : 1;
  367. }
  368. static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  369. {
  370. ulong ofs;
  371. int ret = 0;
  372. size_t len;
  373. /*
  374. * Syntax is:
  375. * 0 1 2 3 4
  376. * onenand test [force] [off size]
  377. */
  378. printf("\nOneNAND test: ");
  379. /* skip first two or three arguments, look for offset and size */
  380. if (arg_off_size(argc - 1, argv + 1, &ofs, &len) != 0)
  381. return 1;
  382. ret = onenand_block_test(ofs, len);
  383. printf("%s\n", ret ? "ERROR" : "OK");
  384. return ret == 0 ? 0 : 1;
  385. }
  386. static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  387. {
  388. ulong ofs;
  389. int ret = 0;
  390. char *s;
  391. if (argc < 2)
  392. return cmd_usage(cmdtp);
  393. s = strchr(argv[0], '.');
  394. ofs = (int)simple_strtoul(argv[1], NULL, 16);
  395. if (s != NULL && strcmp(s, ".oob") == 0)
  396. ret = onenand_dump(mtd, ofs, 1);
  397. else
  398. ret = onenand_dump(mtd, ofs, 0);
  399. return ret == 0 ? 1 : 0;
  400. }
  401. static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  402. {
  403. int ret = 0;
  404. ulong addr;
  405. argc -= 2;
  406. argv += 2;
  407. if (argc <= 0)
  408. return cmd_usage(cmdtp);
  409. while (argc > 0) {
  410. addr = simple_strtoul(*argv, NULL, 16);
  411. if (mtd->block_markbad(mtd, addr)) {
  412. printf("block 0x%08lx NOT marked "
  413. "as bad! ERROR %d\n",
  414. addr, ret);
  415. ret = 1;
  416. } else {
  417. printf("block 0x%08lx successfully "
  418. "marked as bad\n",
  419. addr);
  420. }
  421. --argc;
  422. ++argv;
  423. }
  424. return ret;
  425. }
  426. static cmd_tbl_t cmd_onenand_sub[] = {
  427. U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
  428. U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
  429. U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
  430. U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
  431. U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
  432. U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
  433. U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
  434. U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
  435. };
  436. static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  437. {
  438. cmd_tbl_t *c;
  439. mtd = &onenand_mtd;
  440. /* Strip off leading 'onenand' command argument */
  441. argc--;
  442. argv++;
  443. c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
  444. if (c)
  445. return c->cmd(cmdtp, flag, argc, argv);
  446. else
  447. return cmd_usage(cmdtp);
  448. }
  449. U_BOOT_CMD(
  450. onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
  451. "OneNAND sub-system",
  452. "info - show available OneNAND devices\n"
  453. "onenand bad - show bad blocks\n"
  454. "onenand read[.oob] addr off size\n"
  455. "onenand write addr off size\n"
  456. " read/write 'size' bytes starting at offset 'off'\n"
  457. " to/from memory address 'addr', skipping bad blocks.\n"
  458. "onenand erase [force] [off size] - erase 'size' bytes from\n"
  459. "onenand test [off size] - test 'size' bytes from\n"
  460. " offset 'off' (entire device if not specified)\n"
  461. "onenand dump[.oob] off - dump page\n"
  462. "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
  463. );