]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_onenand.c
onenand: add yaffs write command
[people/ms/u-boot.git] / common / cmd_onenand.c
CommitLineData
d7e8ce10
KP
1/*
2 * U-Boot command for OneNAND support
3 *
c438ea17 4 * Copyright (C) 2005-2008 Samsung Electronics
d7e8ce10
KP
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
12#include <common.h>
13#include <command.h>
c438ea17 14#include <malloc.h>
d7e8ce10 15
d7e8ce10
KP
16#include <linux/mtd/compat.h>
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/onenand.h>
19
20#include <asm/io.h>
21
c438ea17
SR
22static 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
54841ab5 35static int arg_off_size(int argc, char * const argv[], ulong *off, size_t *size)
c438ea17
SR
36{
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) {
8d2effea 56 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
c438ea17
SR
57 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
41c86240
LW
115static 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
c438ea17 139static int onenand_block_write(loff_t to, size_t len,
41c86240 140 size_t *retlen, const u_char * buf, int withoob)
c438ea17
SR
141{
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
41c86240
LW
167 if (!withoob)
168 ret = mtd->write(mtd, ofs, blocksize, &_retlen, buf);
169 else
170 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
c438ea17
SR
171 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;
a430b137 323 ops.oobbuf = oobbuf;
c438ea17
SR
324 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;
a430b137
LW
349 p = oobbuf;
350
c438ea17
SR
351 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}
d7e8ce10 361
54841ab5 362static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
d7e8ce10 363{
8cd85282
FM
364 printf("%s\n", mtd->name);
365 return 0;
366}
367
54841ab5 368static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
369{
370 ulong ofs;
371
372 mtd = &onenand_mtd;
373 /* 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);
378 }
379
380 return 0;
381}
382
54841ab5 383static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
384{
385 char *s;
386 int oob = 0;
c438ea17 387 ulong addr, ofs;
8cd85282 388 size_t len;
8360b66b 389 int ret = 0;
8cd85282 390 size_t retlen = 0;
c438ea17 391
8cd85282 392 if (argc < 3)
47e26b1b 393 return cmd_usage(cmdtp);
c438ea17 394
8cd85282
FM
395 s = strchr(argv[0], '.');
396 if ((s != NULL) && (!strcmp(s, ".oob")))
397 oob = 1;
d7e8ce10 398
8cd85282 399 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
d7e8ce10 400
8cd85282
FM
401 printf("\nOneNAND read: ");
402 if (arg_off_size(argc - 2, argv + 2, &ofs, &len) != 0)
403 return 1;
c438ea17 404
8cd85282 405 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
c438ea17 406
8cd85282 407 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
d7e8ce10 408
8cd85282
FM
409 return ret == 0 ? 0 : 1;
410}
a9da2b41 411
54841ab5 412static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
413{
414 ulong addr, ofs;
415 size_t len;
41c86240 416 int ret = 0, withoob = 0;
8cd85282 417 size_t retlen = 0;
d7e8ce10 418
8cd85282 419 if (argc < 3)
47e26b1b 420 return cmd_usage(cmdtp);
d7e8ce10 421
41c86240
LW
422 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
423 withoob = 1;
424
8cd85282
FM
425 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;
d7e8ce10 430
41c86240 431 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
d7e8ce10 432
8cd85282 433 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
c438ea17 434
8cd85282
FM
435 return ret == 0 ? 0 : 1;
436}
437
54841ab5 438static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
439{
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++;
d7e8ce10 459 }
8cd85282
FM
460 }
461 printf("\nOneNAND erase: ");
d7e8ce10 462
8cd85282
FM
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;
bfd7f386 466
8cd85282 467 ret = onenand_block_erase(ofs, len, force);
bfd7f386 468
8cd85282 469 printf("%s\n", ret ? "ERROR" : "OK");
d7e8ce10 470
8cd85282
FM
471 return ret == 0 ? 0 : 1;
472}
d7e8ce10 473
54841ab5 474static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
475{
476 ulong ofs;
477 int ret = 0;
478 size_t len;
d7e8ce10 479
8cd85282
FM
480 /*
481 * Syntax is:
482 * 0 1 2 3 4
483 * onenand test [force] [off size]
484 */
d7e8ce10 485
8cd85282 486 printf("\nOneNAND test: ");
d7e8ce10 487
8cd85282
FM
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;
d7e8ce10 491
8cd85282 492 ret = onenand_block_test(ofs, len);
d7e8ce10 493
8cd85282 494 printf("%s\n", ret ? "ERROR" : "OK");
d7e8ce10 495
8cd85282
FM
496 return ret == 0 ? 0 : 1;
497}
bfd7f386 498
54841ab5 499static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
500{
501 ulong ofs;
502 int ret = 0;
503 char *s;
d7e8ce10 504
8cd85282 505 if (argc < 2)
47e26b1b 506 return cmd_usage(cmdtp);
8cd85282
FM
507
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);
d7e8ce10 515
8cd85282
FM
516 return ret == 0 ? 1 : 0;
517}
518
54841ab5 519static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
520{
521 int ret = 0;
522 ulong addr;
523
524 argc -= 2;
525 argv += 2;
526
527 if (argc <= 0)
47e26b1b 528 return cmd_usage(cmdtp);
d7e8ce10 529
8cd85282
FM
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, "", ""),
41c86240 554 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
8cd85282
FM
555 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
2e5167cc 561#ifdef CONFIG_NEEDS_MANUAL_RELOC
cdb1d4f9
EBS
562void onenand_reloc(void) {
563 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
564}
565#endif
566
54841ab5 567static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
8cd85282
FM
568{
569 cmd_tbl_t *c;
570
57ff9f24
EBS
571 if (argc < 2)
572 return cmd_usage(cmdtp);
573
8cd85282
FM
574 mtd = &onenand_mtd;
575
576 /* Strip off leading 'onenand' command argument */
577 argc--;
578 argv++;
c438ea17 579
8cd85282
FM
580 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
581
47e26b1b
WD
582 if (c)
583 return c->cmd(cmdtp, flag, argc, argv);
584 else
585 return cmd_usage(cmdtp);
d7e8ce10
KP
586}
587
588U_BOOT_CMD(
8360b66b 589 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
2fb2604d 590 "OneNAND sub-system",
c438ea17
SR
591 "info - show available OneNAND devices\n"
592 "onenand bad - show bad blocks\n"
593 "onenand read[.oob] addr off size\n"
41c86240 594 "onenand write[.yaffs] addr off size\n"
c438ea17
SR
595 " 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"
a89c33db 601 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
d7e8ce10 602);