blob: feab01a71e3d3f77b1e66dfa26a9faa660a50d63 [file] [log] [blame]
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09001/*
2 * U-Boot command for OneNAND support
3 *
Stefan Roese9743d282008-11-12 13:47:24 +01004 * Copyright (C) 2005-2008 Samsung Electronics
Kyungmin Parkaf3d11c2007-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 Roese9743d282008-11-12 13:47:24 +010014#include <malloc.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090015
Mike Frysinger11d1a092012-04-09 13:39:55 +000016#include <linux/compat.h>
Kyungmin Parkaf3d11c2007-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 Roese9743d282008-11-12 13:47:24 +010022static struct mtd_info *mtd;
23
24static loff_t next_ofs;
25static loff_t skip_ofs;
26
Heiko Schocher7e5a16c2015-04-27 07:42:05 +020027static int arg_off_size_onenand(int argc, char * const argv[], ulong *off,
28 size_t *size)
Stefan Roese9743d282008-11-12 13:47:24 +010029{
30 if (argc >= 1) {
31 if (!(str2long(argv[0], off))) {
32 printf("'%s' is not a number\n", argv[0]);
33 return -1;
34 }
35 } else {
36 *off = 0;
37 }
38
39 if (argc >= 2) {
40 if (!(str2long(argv[1], (ulong *)size))) {
41 printf("'%s' is not a number\n", argv[1]);
42 return -1;
43 }
44 } else {
45 *size = mtd->size - *off;
46 }
47
48 if ((*off + *size) > mtd->size) {
Stefan Roese586b3a62009-05-11 16:03:55 +020049 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
Stefan Roese9743d282008-11-12 13:47:24 +010050 return -1;
51 }
52
53 if (*size == mtd->size)
54 puts("whole chip\n");
55 else
56 printf("offset 0x%lx, size 0x%x\n", *off, *size);
57
58 return 0;
59}
60
61static int onenand_block_read(loff_t from, size_t len,
62 size_t *retlen, u_char *buf, int oob)
63{
64 struct onenand_chip *this = mtd->priv;
65 int blocks = (int) len >> this->erase_shift;
66 int blocksize = (1 << this->erase_shift);
67 loff_t ofs = from;
68 struct mtd_oob_ops ops = {
69 .retlen = 0,
70 };
71 int ret;
72
73 if (oob)
74 ops.ooblen = blocksize;
75 else
76 ops.len = blocksize;
77
78 while (blocks) {
Sergey Lapin3a38a552013-01-14 03:46:50 +000079 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +010080 if (ret) {
81 printk("Bad blocks %d at 0x%x\n",
82 (u32)(ofs >> this->erase_shift), (u32)ofs);
83 ofs += blocksize;
84 continue;
85 }
86
87 if (oob)
88 ops.oobbuf = buf;
89 else
90 ops.datbuf = buf;
91
92 ops.retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +000093 ret = mtd_read_oob(mtd, ofs, &ops);
Stefan Roese9743d282008-11-12 13:47:24 +010094 if (ret) {
95 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
96 ofs += blocksize;
97 continue;
98 }
99 ofs += blocksize;
100 buf += blocksize;
101 blocks--;
102 *retlen += ops.retlen;
103 }
104
105 return 0;
106}
107
Lei Wen2ca9e142010-12-02 05:17:55 +0000108static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf,
109 size_t *retlen)
110{
111 struct mtd_oob_ops ops = {
112 .len = mtd->writesize,
113 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000114 .mode = MTD_OPS_AUTO_OOB,
Lei Wen2ca9e142010-12-02 05:17:55 +0000115 };
116 int page, ret = 0;
117 for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) {
118 ops.datbuf = (u_char *)buf;
119 buf += mtd->writesize;
120 ops.oobbuf = (u_char *)buf;
121 buf += mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000122 ret = mtd_write_oob(mtd, to, &ops);
Lei Wen2ca9e142010-12-02 05:17:55 +0000123 if (ret)
124 break;
125 to += mtd->writesize;
126 }
127
128 *retlen = (ret) ? 0 : mtd->erasesize;
129 return ret;
130}
131
Stefan Roese9743d282008-11-12 13:47:24 +0100132static int onenand_block_write(loff_t to, size_t len,
Lei Wen2ca9e142010-12-02 05:17:55 +0000133 size_t *retlen, const u_char * buf, int withoob)
Stefan Roese9743d282008-11-12 13:47:24 +0100134{
135 struct onenand_chip *this = mtd->priv;
136 int blocks = len >> this->erase_shift;
137 int blocksize = (1 << this->erase_shift);
138 loff_t ofs;
139 size_t _retlen = 0;
140 int ret;
141
142 if (to == next_ofs) {
143 next_ofs = to + len;
144 to += skip_ofs;
145 } else {
146 next_ofs = to + len;
147 skip_ofs = 0;
148 }
149 ofs = to;
150
151 while (blocks) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000152 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100153 if (ret) {
154 printk("Bad blocks %d at 0x%x\n",
155 (u32)(ofs >> this->erase_shift), (u32)ofs);
156 skip_ofs += blocksize;
157 goto next;
158 }
159
Lei Wen2ca9e142010-12-02 05:17:55 +0000160 if (!withoob)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000161 ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
Lei Wen2ca9e142010-12-02 05:17:55 +0000162 else
163 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
Stefan Roese9743d282008-11-12 13:47:24 +0100164 if (ret) {
165 printk("Write failed 0x%x, %d", (u32)ofs, ret);
166 skip_ofs += blocksize;
167 goto next;
168 }
169
170 buf += blocksize;
171 blocks--;
172 *retlen += _retlen;
173next:
174 ofs += blocksize;
175 }
176
177 return 0;
178}
179
180static int onenand_block_erase(u32 start, u32 size, int force)
181{
182 struct onenand_chip *this = mtd->priv;
183 struct erase_info instr = {
184 .callback = NULL,
185 };
186 loff_t ofs;
187 int ret;
188 int blocksize = 1 << this->erase_shift;
189
190 for (ofs = start; ofs < (start + size); ofs += blocksize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000191 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100192 if (ret && !force) {
193 printf("Skip erase bad block %d at 0x%x\n",
194 (u32)(ofs >> this->erase_shift), (u32)ofs);
195 continue;
196 }
197
198 instr.addr = ofs;
199 instr.len = blocksize;
200 instr.priv = force;
201 instr.mtd = mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000202 ret = mtd_erase(mtd, &instr);
Stefan Roese9743d282008-11-12 13:47:24 +0100203 if (ret) {
204 printf("erase failed block %d at 0x%x\n",
205 (u32)(ofs >> this->erase_shift), (u32)ofs);
206 continue;
207 }
208 }
209
210 return 0;
211}
212
213static int onenand_block_test(u32 start, u32 size)
214{
215 struct onenand_chip *this = mtd->priv;
216 struct erase_info instr = {
217 .callback = NULL,
218 .priv = 0,
219 };
220
221 int blocks;
222 loff_t ofs;
223 int blocksize = 1 << this->erase_shift;
224 int start_block, end_block;
225 size_t retlen;
226 u_char *buf;
227 u_char *verify_buf;
228 int ret;
229
230 buf = malloc(blocksize);
231 if (!buf) {
232 printf("Not enough malloc space available!\n");
233 return -1;
234 }
235
236 verify_buf = malloc(blocksize);
237 if (!verify_buf) {
238 printf("Not enough malloc space available!\n");
239 return -1;
240 }
241
242 start_block = start >> this->erase_shift;
243 end_block = (start + size) >> this->erase_shift;
244
245 /* Protect boot-loader from badblock testing */
246 if (start_block < 2)
247 start_block = 2;
248
249 if (end_block > (mtd->size >> this->erase_shift))
250 end_block = mtd->size >> this->erase_shift;
251
252 blocks = start_block;
253 ofs = start;
254 while (blocks < end_block) {
255 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
256
Sergey Lapin3a38a552013-01-14 03:46:50 +0000257 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100258 if (ret) {
259 printf("Skip erase bad block %d at 0x%x\n",
260 (u32)(ofs >> this->erase_shift), (u32)ofs);
261 goto next;
262 }
263
264 instr.addr = ofs;
265 instr.len = blocksize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000266 ret = mtd_erase(mtd, &instr);
Stefan Roese9743d282008-11-12 13:47:24 +0100267 if (ret) {
268 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
269 goto next;
270 }
271
Sergey Lapin3a38a552013-01-14 03:46:50 +0000272 ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
Stefan Roese9743d282008-11-12 13:47:24 +0100273 if (ret) {
274 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
275 goto next;
276 }
277
Sergey Lapin3a38a552013-01-14 03:46:50 +0000278 ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
Stefan Roese9743d282008-11-12 13:47:24 +0100279 if (ret) {
280 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
281 goto next;
282 }
283
284 if (memcmp(buf, verify_buf, blocksize))
285 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
286
287next:
288 ofs += blocksize;
289 blocks++;
290 }
291 printf("...Done\n");
292
293 free(buf);
294 free(verify_buf);
295
296 return 0;
297}
298
299static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
300{
301 int i;
302 u_char *datbuf, *oobbuf, *p;
303 struct mtd_oob_ops ops;
304 loff_t addr;
305
306 datbuf = malloc(mtd->writesize + mtd->oobsize);
307 oobbuf = malloc(mtd->oobsize);
308 if (!datbuf || !oobbuf) {
309 puts("No memory for page buffer\n");
310 return 1;
311 }
312 off &= ~(mtd->writesize - 1);
313 addr = (loff_t) off;
314 memset(&ops, 0, sizeof(ops));
315 ops.datbuf = datbuf;
Lei Wene816b8e2010-12-07 09:38:18 +0800316 ops.oobbuf = oobbuf;
Stefan Roese9743d282008-11-12 13:47:24 +0100317 ops.len = mtd->writesize;
318 ops.ooblen = mtd->oobsize;
319 ops.retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000320 i = mtd_read_oob(mtd, addr, &ops);
Stefan Roese9743d282008-11-12 13:47:24 +0100321 if (i < 0) {
322 printf("Error (%d) reading page %08lx\n", i, off);
323 free(datbuf);
324 free(oobbuf);
325 return 1;
326 }
327 printf("Page %08lx dump:\n", off);
328 i = mtd->writesize >> 4;
329 p = datbuf;
330
331 while (i--) {
332 if (!only_oob)
333 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
334 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
335 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
336 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
337 p[15]);
338 p += 16;
339 }
340 puts("OOB:\n");
341 i = mtd->oobsize >> 3;
Lei Wene816b8e2010-12-07 09:38:18 +0800342 p = oobbuf;
343
Stefan Roese9743d282008-11-12 13:47:24 +0100344 while (i--) {
345 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
346 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
347 p += 8;
348 }
349 free(datbuf);
350 free(oobbuf);
351
352 return 0;
353}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900354
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200355static int do_onenand_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900356{
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100357 printf("%s\n", mtd->name);
358 return 0;
359}
360
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200361static int do_onenand_bad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100362{
363 ulong ofs;
364
365 mtd = &onenand_mtd;
366 /* Currently only one OneNAND device is supported */
367 printf("\nDevice %d bad blocks:\n", 0);
368 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000369 if (mtd_block_isbad(mtd, ofs))
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100370 printf(" %08x\n", (u32)ofs);
371 }
372
373 return 0;
374}
375
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200376static int do_onenand_read(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100377{
378 char *s;
379 int oob = 0;
Stefan Roese9743d282008-11-12 13:47:24 +0100380 ulong addr, ofs;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100381 size_t len;
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200382 int ret = 0;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100383 size_t retlen = 0;
Stefan Roese9743d282008-11-12 13:47:24 +0100384
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100385 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000386 return CMD_RET_USAGE;
Stefan Roese9743d282008-11-12 13:47:24 +0100387
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100388 s = strchr(argv[0], '.');
389 if ((s != NULL) && (!strcmp(s, ".oob")))
390 oob = 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900391
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100392 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900393
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100394 printf("\nOneNAND read: ");
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200395 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100396 return 1;
Stefan Roese9743d282008-11-12 13:47:24 +0100397
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100398 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
Stefan Roese9743d282008-11-12 13:47:24 +0100399
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100400 printf(" %d bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
401
402 return ret == 0 ? 0 : 1;
403}
404
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200405static int do_onenand_write(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100406{
407 ulong addr, ofs;
408 size_t len;
Lei Wen2ca9e142010-12-02 05:17:55 +0000409 int ret = 0, withoob = 0;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100410 size_t retlen = 0;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900411
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100412 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000413 return CMD_RET_USAGE;
Kyungmin Park012f2aa2008-03-31 10:40:19 +0900414
Lei Wen2ca9e142010-12-02 05:17:55 +0000415 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
416 withoob = 1;
417
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100418 addr = (ulong)simple_strtoul(argv[1], NULL, 16);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900419
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100420 printf("\nOneNAND write: ");
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200421 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100422 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900423
Lei Wen2ca9e142010-12-02 05:17:55 +0000424 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900425
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100426 printf(" %d bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900427
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100428 return ret == 0 ? 0 : 1;
429}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900430
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200431static int do_onenand_erase(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100432{
433 ulong ofs;
434 int ret = 0;
435 size_t len;
436 int force;
437
438 /*
439 * Syntax is:
440 * 0 1 2 3 4
441 * onenand erase [force] [off size]
442 */
443 argc--;
444 argv++;
445 if (argc)
446 {
447 if (!strcmp("force", argv[0]))
448 {
449 force = 1;
450 argc--;
451 argv++;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900452 }
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100453 }
454 printf("\nOneNAND erase: ");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900455
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100456 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200457 if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100458 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900459
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100460 ret = onenand_block_erase(ofs, len, force);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900461
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100462 printf("%s\n", ret ? "ERROR" : "OK");
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900463
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100464 return ret == 0 ? 0 : 1;
465}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900466
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200467static int do_onenand_test(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100468{
469 ulong ofs;
470 int ret = 0;
471 size_t len;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900472
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100473 /*
474 * Syntax is:
475 * 0 1 2 3 4
476 * onenand test [force] [off size]
477 */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900478
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100479 printf("\nOneNAND test: ");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900480
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100481 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200482 if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100483 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900484
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100485 ret = onenand_block_test(ofs, len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900486
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100487 printf("%s\n", ret ? "ERROR" : "OK");
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200488
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100489 return ret == 0 ? 0 : 1;
490}
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200491
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200492static int do_onenand_dump(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100493{
494 ulong ofs;
495 int ret = 0;
496 char *s;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900497
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100498 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000499 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900500
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100501 s = strchr(argv[0], '.');
502 ofs = (int)simple_strtoul(argv[1], NULL, 16);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900503
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100504 if (s != NULL && strcmp(s, ".oob") == 0)
505 ret = onenand_dump(mtd, ofs, 1);
506 else
507 ret = onenand_dump(mtd, ofs, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900508
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100509 return ret == 0 ? 1 : 0;
510}
511
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200512static int do_onenand_markbad(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100513{
514 int ret = 0;
515 ulong addr;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900516
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100517 argc -= 2;
518 argv += 2;
519
520 if (argc <= 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000521 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900522
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100523 while (argc > 0) {
524 addr = simple_strtoul(*argv, NULL, 16);
525
Sergey Lapin3a38a552013-01-14 03:46:50 +0000526 if (mtd_block_markbad(mtd, addr)) {
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100527 printf("block 0x%08lx NOT marked "
528 "as bad! ERROR %d\n",
529 addr, ret);
530 ret = 1;
531 } else {
532 printf("block 0x%08lx successfully "
533 "marked as bad\n",
534 addr);
535 }
536 --argc;
537 ++argv;
538 }
539 return ret;
540}
541
542static cmd_tbl_t cmd_onenand_sub[] = {
543 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
544 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
545 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
546 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
Lei Wen2ca9e142010-12-02 05:17:55 +0000547 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100548 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
549 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
550 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
551 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
552};
Stefan Roese9743d282008-11-12 13:47:24 +0100553
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200554#ifdef CONFIG_NEEDS_MANUAL_RELOC
Enric Balletbo i Serraaac73592010-10-19 11:13:22 +0200555void onenand_reloc(void) {
556 fixup_cmdtable(cmd_onenand_sub, ARRAY_SIZE(cmd_onenand_sub));
557}
558#endif
559
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200560static int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100561{
562 cmd_tbl_t *c;
563
Enric Balletbo i Serraa9153942010-10-19 11:13:21 +0200564 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000565 return CMD_RET_USAGE;
Enric Balletbo i Serraa9153942010-10-19 11:13:21 +0200566
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100567 mtd = &onenand_mtd;
568
569 /* Strip off leading 'onenand' command argument */
570 argc--;
571 argv++;
572
573 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
574
Wolfgang Denk3b683112010-07-17 01:06:04 +0200575 if (c)
576 return c->cmd(cmdtp, flag, argc, argv);
577 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000578 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900579}
580
581U_BOOT_CMD(
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200582 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600583 "OneNAND sub-system",
Stefan Roese9743d282008-11-12 13:47:24 +0100584 "info - show available OneNAND devices\n"
585 "onenand bad - show bad blocks\n"
586 "onenand read[.oob] addr off size\n"
Lei Wen2ca9e142010-12-02 05:17:55 +0000587 "onenand write[.yaffs] addr off size\n"
Stefan Roese9743d282008-11-12 13:47:24 +0100588 " read/write 'size' bytes starting at offset 'off'\n"
589 " to/from memory address 'addr', skipping bad blocks.\n"
590 "onenand erase [force] [off size] - erase 'size' bytes from\n"
591 "onenand test [off size] - test 'size' bytes from\n"
592 " offset 'off' (entire device if not specified)\n"
593 "onenand dump[.oob] off - dump page\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200594 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900595);