blob: fad781583a313503db6962deb4153f01be11db11 [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090016
Mike Frysinger11d1a092012-04-09 13:39:55 +000017#include <linux/compat.h>
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090018#include <linux/mtd/mtd.h>
19#include <linux/mtd/onenand.h>
20
21#include <asm/io.h>
22
Stefan Roese9743d282008-11-12 13:47:24 +010023static struct mtd_info *mtd;
24
25static loff_t next_ofs;
26static loff_t skip_ofs;
27
Simon Glassed38aef2020-05-10 11:40:03 -060028static int arg_off_size_onenand(int argc, char *const argv[], ulong *off,
Heiko Schocher7e5a16c2015-04-27 07:42:05 +020029 size_t *size)
Stefan Roese9743d282008-11-12 13:47:24 +010030{
31 if (argc >= 1) {
32 if (!(str2long(argv[0], off))) {
33 printf("'%s' is not a number\n", argv[0]);
34 return -1;
35 }
36 } else {
37 *off = 0;
38 }
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
49 if ((*off + *size) > mtd->size) {
Stefan Roese586b3a62009-05-11 16:03:55 +020050 printf("total chip size (0x%llx) exceeded!\n", mtd->size);
Stefan Roese9743d282008-11-12 13:47:24 +010051 return -1;
52 }
53
54 if (*size == mtd->size)
55 puts("whole chip\n");
56 else
Heinrich Schuchardt4b0449f2022-04-25 23:01:44 +020057 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
Stefan Roese9743d282008-11-12 13:47:24 +010058
59 return 0;
60}
61
62static int onenand_block_read(loff_t from, size_t len,
63 size_t *retlen, u_char *buf, int oob)
64{
65 struct onenand_chip *this = mtd->priv;
66 int blocks = (int) len >> this->erase_shift;
67 int blocksize = (1 << this->erase_shift);
68 loff_t ofs = from;
69 struct mtd_oob_ops ops = {
70 .retlen = 0,
71 };
72 int ret;
73
74 if (oob)
75 ops.ooblen = blocksize;
76 else
77 ops.len = blocksize;
78
79 while (blocks) {
Sergey Lapin3a38a552013-01-14 03:46:50 +000080 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +010081 if (ret) {
82 printk("Bad blocks %d at 0x%x\n",
83 (u32)(ofs >> this->erase_shift), (u32)ofs);
84 ofs += blocksize;
85 continue;
86 }
87
88 if (oob)
89 ops.oobbuf = buf;
90 else
91 ops.datbuf = buf;
92
93 ops.retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +000094 ret = mtd_read_oob(mtd, ofs, &ops);
Stefan Roese9743d282008-11-12 13:47:24 +010095 if (ret) {
96 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
97 ofs += blocksize;
98 continue;
99 }
100 ofs += blocksize;
101 buf += blocksize;
102 blocks--;
103 *retlen += ops.retlen;
104 }
105
106 return 0;
107}
108
Lei Wen2ca9e142010-12-02 05:17:55 +0000109static int onenand_write_oneblock_withoob(loff_t to, const u_char * buf,
110 size_t *retlen)
111{
112 struct mtd_oob_ops ops = {
113 .len = mtd->writesize,
114 .ooblen = mtd->oobsize,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000115 .mode = MTD_OPS_AUTO_OOB,
Lei Wen2ca9e142010-12-02 05:17:55 +0000116 };
117 int page, ret = 0;
118 for (page = 0; page < (mtd->erasesize / mtd->writesize); page ++) {
119 ops.datbuf = (u_char *)buf;
120 buf += mtd->writesize;
121 ops.oobbuf = (u_char *)buf;
122 buf += mtd->oobsize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000123 ret = mtd_write_oob(mtd, to, &ops);
Lei Wen2ca9e142010-12-02 05:17:55 +0000124 if (ret)
125 break;
126 to += mtd->writesize;
127 }
128
129 *retlen = (ret) ? 0 : mtd->erasesize;
130 return ret;
131}
132
Stefan Roese9743d282008-11-12 13:47:24 +0100133static int onenand_block_write(loff_t to, size_t len,
Lei Wen2ca9e142010-12-02 05:17:55 +0000134 size_t *retlen, const u_char * buf, int withoob)
Stefan Roese9743d282008-11-12 13:47:24 +0100135{
136 struct onenand_chip *this = mtd->priv;
137 int blocks = len >> this->erase_shift;
138 int blocksize = (1 << this->erase_shift);
139 loff_t ofs;
140 size_t _retlen = 0;
141 int ret;
142
Ladislav Michl6cf2ddb2016-09-13 07:40:00 +0200143 if ((to & (mtd->writesize - 1)) != 0) {
144 printf("Attempt to write non block-aligned data\n");
145 *retlen = 0;
146 return 1;
147 }
148
Stefan Roese9743d282008-11-12 13:47:24 +0100149 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) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000159 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100160 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 Wen2ca9e142010-12-02 05:17:55 +0000167 if (!withoob)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000168 ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
Lei Wen2ca9e142010-12-02 05:17:55 +0000169 else
170 ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
Stefan Roese9743d282008-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;
Marek Behún4b4896d2021-10-05 15:56:06 +0200190 struct erase_info instr = {};
Stefan Roese9743d282008-11-12 13:47:24 +0100191 loff_t ofs;
192 int ret;
193 int blocksize = 1 << this->erase_shift;
194
195 for (ofs = start; ofs < (start + size); ofs += blocksize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000196 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100197 if (ret && !force) {
198 printf("Skip erase bad block %d at 0x%x\n",
199 (u32)(ofs >> this->erase_shift), (u32)ofs);
200 continue;
201 }
202
203 instr.addr = ofs;
204 instr.len = blocksize;
205 instr.priv = force;
206 instr.mtd = mtd;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000207 ret = mtd_erase(mtd, &instr);
Stefan Roese9743d282008-11-12 13:47:24 +0100208 if (ret) {
209 printf("erase failed block %d at 0x%x\n",
210 (u32)(ofs >> this->erase_shift), (u32)ofs);
211 continue;
212 }
213 }
214
215 return 0;
216}
217
218static int onenand_block_test(u32 start, u32 size)
219{
220 struct onenand_chip *this = mtd->priv;
Marek Behún4b4896d2021-10-05 15:56:06 +0200221 struct erase_info instr = {};
Stefan Roese9743d282008-11-12 13:47:24 +0100222
223 int blocks;
224 loff_t ofs;
225 int blocksize = 1 << this->erase_shift;
226 int start_block, end_block;
227 size_t retlen;
228 u_char *buf;
229 u_char *verify_buf;
230 int ret;
231
232 buf = malloc(blocksize);
233 if (!buf) {
234 printf("Not enough malloc space available!\n");
235 return -1;
236 }
237
238 verify_buf = malloc(blocksize);
239 if (!verify_buf) {
240 printf("Not enough malloc space available!\n");
241 return -1;
242 }
243
244 start_block = start >> this->erase_shift;
245 end_block = (start + size) >> this->erase_shift;
246
247 /* Protect boot-loader from badblock testing */
248 if (start_block < 2)
249 start_block = 2;
250
251 if (end_block > (mtd->size >> this->erase_shift))
252 end_block = mtd->size >> this->erase_shift;
253
254 blocks = start_block;
255 ofs = start;
256 while (blocks < end_block) {
257 printf("\rTesting block %d at 0x%x", (u32)(ofs >> this->erase_shift), (u32)ofs);
258
Sergey Lapin3a38a552013-01-14 03:46:50 +0000259 ret = mtd_block_isbad(mtd, ofs);
Stefan Roese9743d282008-11-12 13:47:24 +0100260 if (ret) {
261 printf("Skip erase bad block %d at 0x%x\n",
262 (u32)(ofs >> this->erase_shift), (u32)ofs);
263 goto next;
264 }
265
266 instr.addr = ofs;
267 instr.len = blocksize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000268 ret = mtd_erase(mtd, &instr);
Stefan Roese9743d282008-11-12 13:47:24 +0100269 if (ret) {
270 printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
271 goto next;
272 }
273
Sergey Lapin3a38a552013-01-14 03:46:50 +0000274 ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
Stefan Roese9743d282008-11-12 13:47:24 +0100275 if (ret) {
276 printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
277 goto next;
278 }
279
Sergey Lapin3a38a552013-01-14 03:46:50 +0000280 ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
Stefan Roese9743d282008-11-12 13:47:24 +0100281 if (ret) {
282 printk("Read failed 0x%x, %d\n", (u32)ofs, ret);
283 goto next;
284 }
285
286 if (memcmp(buf, verify_buf, blocksize))
287 printk("\nRead/Write test failed at 0x%x\n", (u32)ofs);
288
289next:
290 ofs += blocksize;
291 blocks++;
292 }
293 printf("...Done\n");
294
295 free(buf);
296 free(verify_buf);
297
298 return 0;
299}
300
301static int onenand_dump(struct mtd_info *mtd, ulong off, int only_oob)
302{
303 int i;
304 u_char *datbuf, *oobbuf, *p;
305 struct mtd_oob_ops ops;
306 loff_t addr;
307
308 datbuf = malloc(mtd->writesize + mtd->oobsize);
309 oobbuf = malloc(mtd->oobsize);
310 if (!datbuf || !oobbuf) {
311 puts("No memory for page buffer\n");
312 return 1;
313 }
314 off &= ~(mtd->writesize - 1);
315 addr = (loff_t) off;
316 memset(&ops, 0, sizeof(ops));
317 ops.datbuf = datbuf;
Lei Wene816b8e2010-12-07 09:38:18 +0800318 ops.oobbuf = oobbuf;
Stefan Roese9743d282008-11-12 13:47:24 +0100319 ops.len = mtd->writesize;
320 ops.ooblen = mtd->oobsize;
321 ops.retlen = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000322 i = mtd_read_oob(mtd, addr, &ops);
Stefan Roese9743d282008-11-12 13:47:24 +0100323 if (i < 0) {
324 printf("Error (%d) reading page %08lx\n", i, off);
325 free(datbuf);
326 free(oobbuf);
327 return 1;
328 }
329 printf("Page %08lx dump:\n", off);
330 i = mtd->writesize >> 4;
331 p = datbuf;
332
333 while (i--) {
334 if (!only_oob)
335 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
336 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
337 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
338 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
339 p[15]);
340 p += 16;
341 }
342 puts("OOB:\n");
343 i = mtd->oobsize >> 3;
Lei Wene816b8e2010-12-07 09:38:18 +0800344 p = oobbuf;
345
Stefan Roese9743d282008-11-12 13:47:24 +0100346 while (i--) {
347 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
348 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
349 p += 8;
350 }
351 free(datbuf);
352 free(oobbuf);
353
354 return 0;
355}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900356
Simon Glassed38aef2020-05-10 11:40:03 -0600357static int do_onenand_info(struct cmd_tbl *cmdtp, int flag, int argc,
358 char *const argv[])
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900359{
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100360 printf("%s\n", mtd->name);
361 return 0;
362}
363
Simon Glassed38aef2020-05-10 11:40:03 -0600364static int do_onenand_bad(struct cmd_tbl *cmdtp, int flag, int argc,
365 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100366{
367 ulong ofs;
368
369 mtd = &onenand_mtd;
370 /* Currently only one OneNAND device is supported */
371 printf("\nDevice %d bad blocks:\n", 0);
372 for (ofs = 0; ofs < mtd->size; ofs += mtd->erasesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000373 if (mtd_block_isbad(mtd, ofs))
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100374 printf(" %08x\n", (u32)ofs);
375 }
376
377 return 0;
378}
379
Simon Glassed38aef2020-05-10 11:40:03 -0600380static int do_onenand_read(struct cmd_tbl *cmdtp, int flag, int argc,
381 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100382{
383 char *s;
384 int oob = 0;
Stefan Roese9743d282008-11-12 13:47:24 +0100385 ulong addr, ofs;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100386 size_t len;
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200387 int ret = 0;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100388 size_t retlen = 0;
Stefan Roese9743d282008-11-12 13:47:24 +0100389
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100390 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000391 return CMD_RET_USAGE;
Stefan Roese9743d282008-11-12 13:47:24 +0100392
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100393 s = strchr(argv[0], '.');
394 if ((s != NULL) && (!strcmp(s, ".oob")))
395 oob = 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900396
Simon Glass3ff49ec2021-07-24 09:03:29 -0600397 addr = (ulong)hextoul(argv[1], NULL);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900398
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100399 printf("\nOneNAND read: ");
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200400 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100401 return 1;
Stefan Roese9743d282008-11-12 13:47:24 +0100402
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100403 ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
Stefan Roese9743d282008-11-12 13:47:24 +0100404
Heinrich Schuchardt4b0449f2022-04-25 23:01:44 +0200405 printf(" %zu bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100406
407 return ret == 0 ? 0 : 1;
408}
409
Simon Glassed38aef2020-05-10 11:40:03 -0600410static int do_onenand_write(struct cmd_tbl *cmdtp, int flag, int argc,
411 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100412{
413 ulong addr, ofs;
414 size_t len;
Lei Wen2ca9e142010-12-02 05:17:55 +0000415 int ret = 0, withoob = 0;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100416 size_t retlen = 0;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900417
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100418 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000419 return CMD_RET_USAGE;
Kyungmin Park012f2aa2008-03-31 10:40:19 +0900420
Lei Wen2ca9e142010-12-02 05:17:55 +0000421 if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
422 withoob = 1;
423
Simon Glass3ff49ec2021-07-24 09:03:29 -0600424 addr = (ulong)hextoul(argv[1], NULL);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900425
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100426 printf("\nOneNAND write: ");
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200427 if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100428 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900429
Lei Wen2ca9e142010-12-02 05:17:55 +0000430 ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900431
Heinrich Schuchardt4b0449f2022-04-25 23:01:44 +0200432 printf(" %zu bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900433
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100434 return ret == 0 ? 0 : 1;
435}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900436
Simon Glassed38aef2020-05-10 11:40:03 -0600437static int do_onenand_erase(struct cmd_tbl *cmdtp, int flag, int argc,
438 char *const argv[])
Frans Meulenbroeks889ef812010-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++;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900459 }
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100460 }
461 printf("\nOneNAND erase: ");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900462
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100463 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200464 if (arg_off_size_onenand(argc, argv, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100465 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900466
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100467 ret = onenand_block_erase(ofs, len, force);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900468
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100469 printf("%s\n", ret ? "ERROR" : "OK");
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900470
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100471 return ret == 0 ? 0 : 1;
472}
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900473
Simon Glassed38aef2020-05-10 11:40:03 -0600474static int do_onenand_test(struct cmd_tbl *cmdtp, int flag, int argc,
475 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100476{
477 ulong ofs;
478 int ret = 0;
479 size_t len;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900480
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100481 /*
482 * Syntax is:
483 * 0 1 2 3 4
484 * onenand test [force] [off size]
485 */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900486
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100487 printf("\nOneNAND test: ");
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900488
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100489 /* skip first two or three arguments, look for offset and size */
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200490 if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100491 return 1;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900492
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100493 ret = onenand_block_test(ofs, len);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900494
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100495 printf("%s\n", ret ? "ERROR" : "OK");
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200496
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100497 return ret == 0 ? 0 : 1;
498}
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200499
Simon Glassed38aef2020-05-10 11:40:03 -0600500static int do_onenand_dump(struct cmd_tbl *cmdtp, int flag, int argc,
501 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100502{
503 ulong ofs;
504 int ret = 0;
505 char *s;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900506
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100507 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000508 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900509
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100510 s = strchr(argv[0], '.');
Simon Glass3ff49ec2021-07-24 09:03:29 -0600511 ofs = (int)hextoul(argv[1], NULL);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900512
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100513 if (s != NULL && strcmp(s, ".oob") == 0)
514 ret = onenand_dump(mtd, ofs, 1);
515 else
516 ret = onenand_dump(mtd, ofs, 0);
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900517
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100518 return ret == 0 ? 1 : 0;
519}
520
Simon Glassed38aef2020-05-10 11:40:03 -0600521static int do_onenand_markbad(struct cmd_tbl *cmdtp, int flag, int argc,
522 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100523{
524 int ret = 0;
525 ulong addr;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900526
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100527 argc -= 2;
528 argv += 2;
529
530 if (argc <= 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000531 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900532
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100533 while (argc > 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600534 addr = hextoul(*argv, NULL);
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100535
Sergey Lapin3a38a552013-01-14 03:46:50 +0000536 if (mtd_block_markbad(mtd, addr)) {
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100537 printf("block 0x%08lx NOT marked "
538 "as bad! ERROR %d\n",
539 addr, ret);
540 ret = 1;
541 } else {
542 printf("block 0x%08lx successfully "
543 "marked as bad\n",
544 addr);
545 }
546 --argc;
547 ++argv;
548 }
549 return ret;
550}
551
Simon Glassed38aef2020-05-10 11:40:03 -0600552static struct cmd_tbl cmd_onenand_sub[] = {
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100553 U_BOOT_CMD_MKENT(info, 1, 0, do_onenand_info, "", ""),
554 U_BOOT_CMD_MKENT(bad, 1, 0, do_onenand_bad, "", ""),
555 U_BOOT_CMD_MKENT(read, 4, 0, do_onenand_read, "", ""),
556 U_BOOT_CMD_MKENT(write, 4, 0, do_onenand_write, "", ""),
Lei Wen2ca9e142010-12-02 05:17:55 +0000557 U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100558 U_BOOT_CMD_MKENT(erase, 3, 0, do_onenand_erase, "", ""),
559 U_BOOT_CMD_MKENT(test, 3, 0, do_onenand_test, "", ""),
560 U_BOOT_CMD_MKENT(dump, 2, 0, do_onenand_dump, "", ""),
561 U_BOOT_CMD_MKENT(markbad, CONFIG_SYS_MAXARGS, 0, do_onenand_markbad, "", ""),
562};
Stefan Roese9743d282008-11-12 13:47:24 +0100563
Simon Glassed38aef2020-05-10 11:40:03 -0600564static int do_onenand(struct cmd_tbl *cmdtp, int flag, int argc,
565 char *const argv[])
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100566{
Simon Glassed38aef2020-05-10 11:40:03 -0600567 struct cmd_tbl *c;
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100568
Enric Balletbo i Serraa9153942010-10-19 11:13:21 +0200569 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000570 return CMD_RET_USAGE;
Enric Balletbo i Serraa9153942010-10-19 11:13:21 +0200571
Frans Meulenbroeks889ef812010-03-27 17:14:36 +0100572 mtd = &onenand_mtd;
573
574 /* Strip off leading 'onenand' command argument */
575 argc--;
576 argv++;
577
578 c = find_cmd_tbl(argv[0], &cmd_onenand_sub[0], ARRAY_SIZE(cmd_onenand_sub));
579
Wolfgang Denk3b683112010-07-17 01:06:04 +0200580 if (c)
581 return c->cmd(cmdtp, flag, argc, argv);
582 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000583 return CMD_RET_USAGE;
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900584}
585
586U_BOOT_CMD(
Wolfgang Denk7a15ca12009-05-24 17:34:33 +0200587 onenand, CONFIG_SYS_MAXARGS, 1, do_onenand,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600588 "OneNAND sub-system",
Stefan Roese9743d282008-11-12 13:47:24 +0100589 "info - show available OneNAND devices\n"
590 "onenand bad - show bad blocks\n"
591 "onenand read[.oob] addr off size\n"
Lei Wen2ca9e142010-12-02 05:17:55 +0000592 "onenand write[.yaffs] addr off size\n"
Stefan Roese9743d282008-11-12 13:47:24 +0100593 " read/write 'size' bytes starting at offset 'off'\n"
594 " to/from memory address 'addr', skipping bad blocks.\n"
595 "onenand erase [force] [off size] - erase 'size' bytes from\n"
596 "onenand test [off size] - test 'size' bytes from\n"
597 " offset 'off' (entire device if not specified)\n"
598 "onenand dump[.oob] off - dump page\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200599 "onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900600);