cmd_onenand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  285. {
  286. struct onenand_chip *this;
  287. int blocksize;
  288. ulong addr, ofs;
  289. size_t len, retlen = 0;
  290. int ret = 0;
  291. char *cmd, *s;
  292. mtd = &onenand_mtd;
  293. this = mtd->priv;
  294. blocksize = (1 << this->erase_shift);
  295. cmd = argv[1];
  296. switch (argc) {
  297. case 0:
  298. case 1:
  299. goto usage;
  300. case 2:
  301. if (strcmp(cmd, "info") == 0) {
  302. printf("%s\n", mtd->name);
  303. return 0;
  304. }
  305. if (strcmp(cmd, "bad") == 0) {
  306. /* Currently only one OneNAND device is supported */
  307. printf("\nDevice %d bad blocks:\n", 0);
  308. for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
  309. if (mtd->block_isbad(mtd, ofs))
  310. printf(" %08x\n", (u32)ofs);
  311. }
  312. return 0;
  313. }
  314. default:
  315. /* At least 4 args */
  316. /*
  317. * Syntax is:
  318. * 0 1 2 3 4
  319. * onenand erase [force] [off size]
  320. */
  321. if ((strcmp(cmd, "erase") == 0) || (strcmp(cmd, "test") == 0)) {
  322. int force = argc > 2 && !strcmp("force", argv[2]);
  323. int o = force ? 3 : 2;
  324. int erase;
  325. erase = strcmp(cmd, "erase") == 0; /* 1 = erase, 0 = test */
  326. printf("\nOneNAND %s: ", erase ? "erase" : "test");
  327. /* skip first two or three arguments, look for offset and size */
  328. if (arg_off_size(argc - o, argv + o, &ofs, &len) != 0)
  329. return 1;
  330. if (erase)
  331. ret = onenand_block_erase(ofs, len, force);
  332. else
  333. ret = onenand_block_test(ofs, len);
  334. printf("%s\n", ret ? "ERROR" : "OK");
  335. return ret == 0 ? 0 : 1;
  336. }
  337. if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
  338. int read;
  339. int oob = 0;
  340. if (argc < 4)
  341. goto usage;
  342. addr = (ulong)simple_strtoul(argv[2], NULL, 16);
  343. read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
  344. printf("\nOneNAND %s: ", read ? "read" : "write");
  345. if (arg_off_size(argc - 3, argv + 3, &ofs, &len) != 0)
  346. return 1;
  347. s = strchr(cmd, '.');
  348. if ((s != NULL) && (!strcmp(s, ".oob")))
  349. oob = 1;
  350. if (read) {
  351. ret = onenand_block_read(ofs, len, &retlen,
  352. (u8 *)addr, oob);
  353. } else {
  354. ret = onenand_block_write(ofs, len, &retlen,
  355. (u8 *)addr);
  356. }
  357. printf(" %d bytes %s: %s\n", retlen,
  358. read ? "read" : "written", ret ? "ERROR" : "OK");
  359. return ret == 0 ? 0 : 1;
  360. }
  361. if (strcmp(cmd, "markbad") == 0) {
  362. argc -= 2;
  363. argv += 2;
  364. if (argc <= 0)
  365. goto usage;
  366. while (argc > 0) {
  367. addr = simple_strtoul(*argv, NULL, 16);
  368. if (mtd->block_markbad(mtd, addr)) {
  369. printf("block 0x%08lx NOT marked "
  370. "as bad! ERROR %d\n",
  371. addr, ret);
  372. ret = 1;
  373. } else {
  374. printf("block 0x%08lx successfully "
  375. "marked as bad\n",
  376. addr);
  377. }
  378. --argc;
  379. ++argv;
  380. }
  381. return ret;
  382. }
  383. if (strncmp(cmd, "dump", 4) == 0) {
  384. if (argc < 3)
  385. goto usage;
  386. s = strchr(cmd, '.');
  387. ofs = (int)simple_strtoul(argv[2], NULL, 16);
  388. if (s != NULL && strcmp(s, ".oob") == 0)
  389. ret = onenand_dump(mtd, ofs, 1);
  390. else
  391. ret = onenand_dump(mtd, ofs, 0);
  392. return ret == 0 ? 1 : 0;
  393. }
  394. break;
  395. }
  396. return 0;
  397. usage:
  398. cmd_usage(cmdtp);
  399. return 1;
  400. }
  401. U_BOOT_CMD(
  402. onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
  403. "OneNAND sub-system",
  404. "info - show available OneNAND devices\n"
  405. "onenand bad - show bad blocks\n"
  406. "onenand read[.oob] addr off size\n"
  407. "onenand write[.oob] addr off size\n"
  408. " read/write 'size' bytes starting at offset 'off'\n"
  409. " to/from memory address 'addr', skipping bad blocks.\n"
  410. "onenand erase [force] [off size] - erase 'size' bytes from\n"
  411. "onenand test [off size] - test 'size' bytes from\n"
  412. " offset 'off' (entire device if not specified)\n"
  413. "onenand dump[.oob] off - dump page\n"
  414. "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
  415. );