blob: a0d25e55212a0c9633e89d62331f3ed223c694df [file] [log] [blame]
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09001/*
2 * U-Boot command for OneNAND support
3 *
Stefan Roesec438ea12008-11-12 13:47:24 +01004 * Copyright (C) 2005-2008 Samsung Electronics
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +09005 * 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
12#include <common.h>
13#include <command.h>
Stefan Roesec438ea12008-11-12 13:47:24 +010014#include <malloc.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090015
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000016#include <linux/compat.h>
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +090017#include <linux/mtd/mtd.h>
18#include <linux/mtd/onenand.h>
19
20#include <asm/io.h>
21
Stefan Roesec438ea12008-11-12 13:47:24 +010022static struct mtd_info *mtd;
23
24static loff_t next_ofs;
25static loff_t skip_ofs;
26
27static inline int str2long(char *p, ulong *num)
28{
29 char *endptr;
30
31 *num = simple_strtoul(p, &endptr, 16);
32 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
33}
34
Wolfgang Denk54841ab2010-06-28 22:00:46 +020035static int arg_off_size(int argc, char * const argv[], ulong *off, size_t *size)
Stefan Roesec438ea12008-11-12 13:47:24 +010036{
37 if (argc >= 1) {
38 if (!(str2long(argv[0], off))) {
39 printf("'%s' is not a number\n", argv[0]);
40 return -1;
41 }
42 } else {
43 *off = 0;
44 }
45
46 if (argc >= 2) {
47 if (!(str2long(argv[1], (ulong *)size))) {
48 printf("'%s' is not a number\n", argv[1]);
49 return -1;
50 }
51 } else {
52 *size = mtd->size - *off;
53 }
54
55 if ((*off + *size) > mtd->size) {
Stefan Roese8d2effe2009-05-11 16:03:55 +020056 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
Stefan Roesec438ea12008-11-12 13:47:24 +010057 return -1;
58 }
59
60 if (*size == mtd->size)
61 puts("whole chip\n");
62 else
63 printf("offset 0x%lx, size 0x%x\n", *off, *size);
64
65 return 0;
66}
67
68static int onenand_block_read(loff_t from, size_t len,
69 size_t *retlen, u_char *buf, int oob)
70{
71 struct onenand_chip *this = mtd->priv;
72 int blocks = (int) len >> this->erase_shift;
73 int blocksize = (1 << this->erase_shift);
74 loff_t ofs = from;
75 struct mtd_oob_ops ops = {
76 .retlen = 0,
77 };
78 int ret;
79
80 if (oob)
81 ops.ooblen = blocksize;
82 else
83 ops.len = blocksize;
84
85 while (blocks) {
86 ret = mtd->block_isbad(mtd, ofs);
87 if (ret) {
88 printk("Bad blocks %d at 0x%x\n",
89 (u32)(ofs >> this->erase_shift), (u32)ofs);
90 ofs += blocksize;
91 continue;
92 }
93
94 if (oob)
95 ops.oobbuf = buf;
96 else
97 ops.datbuf = buf;
98
99 ops.retlen = 0;
100 ret = mtd->read_oob(mtd, ofs, &ops);
101 if (ret) {
102 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
103 ofs += blocksize;
104 continue;
105 }
106 ofs += blocksize;
107 buf += blocksize;
108 blocks--;
109 *retlen += ops.retlen;
110 }
111
112 return 0;
113}
114
Lei Wen41c86242010-12-02 05:17:55 +0000115static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf,
116 size_t *retlen)
117{
118 struct mtd_oob_ops ops = {
119 .len = mtd->writesize,
120 .ooblen = mtd->oobsize,
121 .mode = MTD_OOB_AUTO,
122 };
123 int page, ret = 0;
124 for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) {
125 ops.datbuf = (u_char *)buf;
126 buf += mtd->writesize;
127 ops.oobbuf = (u_char *)buf;
128 buf += mtd->oobsize;
129 ret = mtd->write_oob(mtd, to, &ops);
130 if (ret)
131 break;
132 to += mtd->writesize;
133 }
134
135 *retlen = (ret) ? 0 : mtd->erasesize;
136 return ret;
137}
138
Stefan Roesec438ea12008-11-12 13:47:24 +0100139static int onenand_block_write(loff_t to, size_t len,
Lei Wen41c86242010-12-02 05:17:55 +0000140 size_t *retlen, const u_char * buf, int withoob)
Stefan Roesec438ea12008-11-12 13:47:24 +0100141{
142 struct onenand_chip *this = mtd->priv;
143 int blocks = len >> this->erase_shift;
144 int blocksize = (1 << this->erase_shift);
145 loff_t ofs;
146 size_t _retlen = 0;
147 int ret;
148
149 if (to == next_ofs) {
150 next_ofs = to + len;
151 to += skip_ofs;
152 } else {
153 next_ofs = to + len;
154 skip_ofs = 0;
155 }
156 ofs = to;
157
158 while (blocks) {
159 ret = mtd->block_isbad(mtd, ofs);
160 if (ret) {
161 printk("Bad blocks %d at 0x%x\n",
162 (u32)(ofs >> this->erase_shift), (u32)ofs);
163 skip_ofs += blocksize;
164 goto next;
165 }
166
Lei Wen41c86242010-12-02 05:17:55 +0000167 if (!withoob)
168 ret = mtd->write(mtd, ofs, blocksize, &_retlen, buf);
169 else
170 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
Stefan Roesec438ea12008-11-12 13:47:24 +0100171 if (ret) {
172 printk("Write failed 0x%x, %d", (u32)ofs, ret);
173 skip_ofs += blocksize;
174 goto next;
175 }
176
177 buf += blocksize;
178 blocks--;
179 *retlen += _retlen;
180next:
181 ofs += blocksize;
182 }
183
184 return 0;
185}
186
187static int onenand_block_erase(u32 start, u32 size, int force)
188{
189 struct onenand_chip *this = mtd->priv;
190 struct erase_info instr = {
191 .callback = NULL,
192 };
193 loff_t ofs;
194 int ret;
195 int blocksize = 1 << this->erase_shift;
196
197 for (ofs = start; ofs < (start + size); ofs += blocksize) {
198 ret = mtd->block_isbad(mtd, ofs);
199 if (ret && !force) {
200 printf("Skip erase bad block %d at 0x%x\n",
201 (u32)(ofs >> this->erase_shift), (u32)ofs);
202 continue;
203 }
204
205 instr.addr = ofs;
206 instr.len = blocksize;
207 instr.priv = force;
208 instr.mtd = mtd;
209 ret = mtd->erase(mtd, &instr);
210 if (ret) {
211 printf("erase failed block %d at 0x%x\n",
212 (u32)(ofs >> this->erase_shift), (u32)ofs);
213 continue;
214 }
215 }
216
217 return 0;
218}
219
220static int onenand_block_test(u32 start, u32 size)
221{
222 struct onenand_chip *this = mtd->priv;
223 struct erase_info instr = {
224 .callback = NULL,
225 .priv = 0,
226 };
227
228 int blocks;
229 loff_t ofs;
230 int blocksize = 1 << this->erase_shift;
231 int start_block, end_block;
232 size_t retlen;
233 u_char *buf;
234 u_char *verify_buf;
235 int ret;
236
237 buf = malloc(blocksize);
238 if (!buf) {
239 printf("Not enough malloc space available!\n");
240 return -1;
241 }
242
243 verify_buf = malloc(blocksize);
244 if (!verify_buf) {
245 printf("Not enough malloc space available!\n");
246 return -1;
247 }
248
249 start_block = start >> this->erase_shift;
250 end_block = (start + size) >> this->erase_shift;
251
252 /* Protect boot-loader from badblock testing */
253 if (start_block < 2)
254 start_block = 2;
255
256 if (end_block > (mtd->size >> this->erase_shift))
257 end_block = mtd->size >> this->erase_shift;
258
259 blocks = start_block;
260 ofs = start;
261 while (blocks < end_block) {
262 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
263
264 ret = mtd->block_isbad(mtd, ofs);
265 if (ret) {
266 printf("Skip erase bad block %d at 0x%x\n",
267 (u32)(ofs >> this->erase_shift), (u32)ofs);
268 goto next;
269 }
270
271 instr.addr = ofs;
272 instr.len = blocksize;
273 ret = mtd->erase(mtd, &instr);
274 if (ret) {
275 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
276 goto next;
277 }
278
279 ret = mtd->write(mtd, ofs, blocksize, &retlen, buf);
280 if (ret) {
281 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
282 goto next;
283 }
284
285 ret = mtd->read(mtd, ofs, blocksize, &retlen, verify_buf);
286 if (ret) {
287 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
288 goto next;
289 }
290
291 if (memcmp(buf, verify_buf, blocksize))
292 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
293
294next:
295 ofs += blocksize;
296 blocks++;
297 }
298 printf("...Done\n");
299
300 free(buf);
301 free(verify_buf);
302
303 return 0;
304}
305
306static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
307{
308 int i;
309 u_char *datbuf, *oobbuf, *p;
310 struct mtd_oob_ops ops;
311 loff_t addr;
312
313 datbuf = malloc(mtd->writesize + mtd->oobsize);
314 oobbuf = malloc(mtd->oobsize);
315 if (!datbuf || !oobbuf) {
316 puts("No memory for page buffer\n");
317 return 1;
318 }
319 off &= ~(mtd->writesize - 1);
320 addr = (loff_t) off;
321 memset(&ops, 0, sizeof(ops));
322 ops.datbuf = datbuf;
Lei Wena430b132010-12-07 09:38:18 +0800323 ops.oobbuf = oobbuf;
Stefan Roesec438ea12008-11-12 13:47:24 +0100324 ops.len = mtd->writesize;
325 ops.ooblen = mtd->oobsize;
326 ops.retlen = 0;
327 i = mtd->read_oob(mtd, addr, &ops);
328 if (i < 0) {
329 printf("Error (%d) reading page %08lx\n", i, off);
330 free(datbuf);
331 free(oobbuf);
332 return 1;
333 }
334 printf("Page %08lx dump:\n", off);
335 i = mtd->writesize >> 4;
336 p = datbuf;
337
338 while (i--) {
339 if (!only_oob)
340 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
341 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
342 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
343 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
344 p[15]);
345 p += 16;
346 }
347 puts("OOB:\n");
348 i = mtd->oobsize >> 3;
Lei Wena430b132010-12-07 09:38:18 +0800349 p = oobbuf;
350
Stefan Roesec438ea12008-11-12 13:47:24 +0100351 while (i--) {
352 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
353 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
354 p += 8;
355 }
356 free(datbuf);
357 free(oobbuf);
358
359 return 0;
360}
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900361
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200362static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900363{
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100364 printf("%s\n", mtd->name);
365 return 0;
366}
367
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200368static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100369{
370 ulong ofs;
Stefan Roesec438ea12008-11-12 13:47:24 +0100371
372 mtd = &onenand_mtd;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100373 /* Currently only one OneNAND device is supported */
374 printf("\nDevice %d bad blocks:\n", 0);
375 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
376 if (mtd->block_isbad(mtd, ofs))
377 printf(" %08x\n", (u32)ofs);
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900378 }
379
380 return 0;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100381}
Stefan Roesec438ea12008-11-12 13:47:24 +0100382
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200383static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100384{
385 char *s;
386 int oob = 0;
387 ulong addr, ofs;
388 size_t len;
389 int ret = 0;
390 size_t retlen = 0;
391
392 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000393 return CMD_RET_USAGE;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100394
395 s = strchr(argv[0], '.');
396 if ((s != NULL) && (!strcmp(s, ".oob")))
397 oob = 1;
398
399 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
400
401 printf("\nOneNAND read: ");
402 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
403 return 1;
404
405 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
406
407 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
408
409 return ret == 0 ? 0 : 1;
410}
411
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200412static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100413{
414 ulong addr, ofs;
415 size_t len;
Lei Wen41c86242010-12-02 05:17:55 +0000416 int ret = 0, withoob = 0;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100417 size_t retlen = 0;
418
419 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000420 return CMD_RET_USAGE;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100421
Lei Wen41c86242010-12-02 05:17:55 +0000422 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
423 withoob = 1;
424
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100425 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
426
427 printf("\nOneNAND write: ");
428 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
429 return 1;
430
Lei Wen41c86242010-12-02 05:17:55 +0000431 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100432
433 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
434
435 return ret == 0 ? 0 : 1;
436}
437
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200438static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100439{
440 ulong ofs;
441 int ret = 0;
442 size_t len;
443 int force;
444
445 /*
446 * Syntax is:
447 * 0 1 2 3 4
448 * onenand erase [force] [off size]
449 */
450 argc--;
451 argv++;
452 if (argc)
453 {
454 if (!strcmp("force", argv[0]))
455 {
456 force = 1;
457 argc--;
458 argv++;
459 }
460 }
461 printf("\nOneNAND erase: ");
462
463 /* skip first two or three arguments, look for offset and size */
464 if (arg_off_size(argc, argv, &ofs, &len) != 0)
465 return 1;
466
467 ret = onenand_block_erase(ofs, len, force);
468
469 printf("%s\n", ret ? "ERROR" : "OK");
470
471 return ret == 0 ? 0 : 1;
472}
473
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200474static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100475{
476 ulong ofs;
477 int ret = 0;
478 size_t len;
479
480 /*
481 * Syntax is:
482 * 0 1 2 3 4
483 * onenand test [force] [off size]
484 */
485
486 printf("\nOneNAND test: ");
487
488 /* skip first two or three arguments, look for offset and size */
489 if (arg_off_size(argc - 1, argv + 1, &ofs, &len) != 0)
490 return 1;
491
492 ret = onenand_block_test(ofs, len);
493
494 printf("%s\n", ret ? "ERROR" : "OK");
495
496 return ret == 0 ? 0 : 1;
497}
498
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200499static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100500{
501 ulong ofs;
502 int ret = 0;
503 char *s;
504
505 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000506 return CMD_RET_USAGE;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100507
508 s = strchr(argv[0], '.');
509 ofs = (int)simple_strtoul(argv[1], NULL, 16);
510
511 if (s != NULL && strcmp(s, ".oob") == 0)
512 ret = onenand_dump(mtd, ofs, 1);
513 else
514 ret = onenand_dump(mtd, ofs, 0);
515
516 return ret == 0 ? 1 : 0;
517}
518
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200519static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100520{
521 int ret = 0;
522 ulong addr;
523
524 argc -= 2;
525 argv += 2;
526
527 if (argc <= 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000528 return CMD_RET_USAGE;
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100529
530 while (argc > 0) {
531 addr = simple_strtoul(*argv, NULL, 16);
532
533 if (mtd->block_markbad(mtd, addr)) {
534 printf("block 0x%08lx NOT marked "
535 "as bad! ERROR %d\n",
536 addr, ret);
537 ret = 1;
538 } else {
539 printf("block 0x%08lx successfully "
540 "marked as bad\n",
541 addr);
542 }
543 --argc;
544 ++argv;
545 }
546 return ret;
547}
548
549static cmd_tbl_t cmd_onenand_sub[] = {
550 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
551 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
552 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
553 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
Lei Wen41c86242010-12-02 05:17:55 +0000554 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100555 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
556 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
557 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
558 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
559};
560
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200561#ifdef CONFIG_NEEDS_MANUAL_RELOC
Enric Balletbo i Serracdb1d4f2010-10-19 11:13:22 +0200562void onenand_reloc(void) {
563 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
564}
565#endif
566
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200567static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100568{
569 cmd_tbl_t *c;
570
Enric Balletbo i Serra57ff9f22010-10-19 11:13:21 +0200571 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000572 return CMD_RET_USAGE;
Enric Balletbo i Serra57ff9f22010-10-19 11:13:21 +0200573
Frans Meulenbroeks8cd85282010-03-27 17:14:36 +0100574 mtd = &onenand_mtd;
575
576 /* Strip off leading 'onenand' command argument */
577 argc--;
578 argv++;
579
580 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
581
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200582 if (c)
583 return c->cmd(cmdtp, flag, argc, argv);
584 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000585 return CMD_RET_USAGE;
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900586}
587
588U_BOOT_CMD(
Wolfgang Denk8360b662009-05-24 17:34:33 +0200589 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
Peter Tyser2fb26042009-01-27 18:03:12 -0600590 "OneNAND sub-system",
Stefan Roesec438ea12008-11-12 13:47:24 +0100591 "info - show available OneNAND devices\n"
592 "onenand bad - show bad blocks\n"
593 "onenand read[.oob] addr off size\n"
Lei Wen41c86242010-12-02 05:17:55 +0000594 "onenand write[.yaffs] addr off size\n"
Stefan Roesec438ea12008-11-12 13:47:24 +0100595 " read/write 'size' bytes starting at offset 'off'\n"
596 " to/from memory address 'addr', skipping bad blocks.\n"
597 "onenand erase [force] [off size] - erase 'size' bytes from\n"
598 "onenand test [off size] - test 'size' bytes from\n"
599 " offset 'off' (entire device if not specified)\n"
600 "onenand dump[.oob] off - dump page\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200601 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
Kyungmin Parkd7e8ce12007-09-10 17:15:14 +0900602);