cmd_onenand.c 11 KB

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