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
12#include <common.h>
13#include <command.h>
14#include <malloc.h>
15#include <linux/printk.h>
16
17#include <linux/compat.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/onenand.h>
20
21#include <asm/io.h>
22
23static struct mtd_info *mtd;
24
25static loff_t next_ofs;
26static loff_t skip_ofs;
27
28static int arg_off_size_onenand(int argc, char *const argv[], ulong *off,
29				size_t *size)
30{
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) {
50		printf("total chip size (0x%llx) exceeded!\n", mtd->size);
51		return -1;
52	}
53
54	if (*size == mtd->size)
55		puts("whole chip\n");
56	else
57		printf("offset 0x%lx, size 0x%zx\n", *off, *size);
58
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) {
80		ret = mtd_block_isbad(mtd, ofs);
81		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;
94		ret = mtd_read_oob(mtd, ofs, &ops);
95		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
109static 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,
115		.mode = MTD_OPS_AUTO_OOB,
116	};
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;
123		ret = mtd_write_oob(mtd, to, &ops);
124		if (ret)
125			break;
126		to += mtd->writesize;
127	}
128
129	*retlen = (ret) ? 0 : mtd->erasesize;
130	return ret;
131}
132
133static int onenand_block_write(loff_t to, size_t len,
134			       size_t *retlen, const u_char * buf, int withoob)
135{
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
143	if ((to & (mtd->writesize - 1)) != 0) {
144		printf("Attempt to write non block-aligned data\n");
145		*retlen = 0;
146		return 1;
147	}
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
167		if (!withoob)
168			ret = mtd_write(mtd, ofs, blocksize, &_retlen, buf);
169		else
170			ret = onenand_write_oneblock_withoob(ofs, buf, &_retlen);
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	loff_t ofs;
192	int ret;
193	int blocksize = 1 << this->erase_shift;
194
195	for (ofs = start; ofs < (start + size); ofs += blocksize) {
196		ret = mtd_block_isbad(mtd, ofs);
197		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;
207		ret = mtd_erase(mtd, &instr);
208		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;
221	struct erase_info instr = {};
222
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
259		ret = mtd_block_isbad(mtd, ofs);
260		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;
268		ret = mtd_erase(mtd, &instr);
269		if (ret) {
270			printk("Erase failed 0x%x, %d\n", (u32)ofs, ret);
271			goto next;
272		}
273
274		ret = mtd_write(mtd, ofs, blocksize, &retlen, buf);
275		if (ret) {
276			printk("Write failed 0x%x, %d\n", (u32)ofs, ret);
277			goto next;
278		}
279
280		ret = mtd_read(mtd, ofs, blocksize, &retlen, verify_buf);
281		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;
318	ops.oobbuf = oobbuf;
319	ops.len = mtd->writesize;
320	ops.ooblen = mtd->oobsize;
321	ops.retlen = 0;
322	i = mtd_read_oob(mtd, addr, &ops);
323	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;
344	p = oobbuf;
345
346	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}
356
357static int do_onenand_info(struct cmd_tbl *cmdtp, int flag, int argc,
358			   char *const argv[])
359{
360	printf("%s\n", mtd->name);
361	return 0;
362}
363
364static int do_onenand_bad(struct cmd_tbl *cmdtp, int flag, int argc,
365			  char *const argv[])
366{
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) {
373		if (mtd_block_isbad(mtd, ofs))
374			printf("  %08x\n", (u32)ofs);
375	}
376
377	return 0;
378}
379
380static int do_onenand_read(struct cmd_tbl *cmdtp, int flag, int argc,
381			   char *const argv[])
382{
383	char *s;
384	int oob = 0;
385	ulong addr, ofs;
386	size_t len;
387	int ret = 0;
388	size_t retlen = 0;
389
390	if (argc < 3)
391		return CMD_RET_USAGE;
392
393	s = strchr(argv[0], '.');
394	if ((s != NULL) && (!strcmp(s, ".oob")))
395		oob = 1;
396
397	addr = (ulong)hextoul(argv[1], NULL);
398
399	printf("\nOneNAND read: ");
400	if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
401		return 1;
402
403	ret = onenand_block_read(ofs, len, &retlen, (u8 *)addr, oob);
404
405	printf(" %zu bytes read: %s\n", retlen, ret ? "ERROR" : "OK");
406
407	return ret == 0 ? 0 : 1;
408}
409
410static int do_onenand_write(struct cmd_tbl *cmdtp, int flag, int argc,
411			    char *const argv[])
412{
413	ulong addr, ofs;
414	size_t len;
415	int ret = 0, withoob = 0;
416	size_t retlen = 0;
417
418	if (argc < 3)
419		return CMD_RET_USAGE;
420
421	if (strncmp(argv[0] + 6, "yaffs", 5) == 0)
422		withoob = 1;
423
424	addr = (ulong)hextoul(argv[1], NULL);
425
426	printf("\nOneNAND write: ");
427	if (arg_off_size_onenand(argc - 2, argv + 2, &ofs, &len) != 0)
428		return 1;
429
430	ret = onenand_block_write(ofs, len, &retlen, (u8 *)addr, withoob);
431
432	printf(" %zu bytes written: %s\n", retlen, ret ? "ERROR" : "OK");
433
434	return ret == 0 ? 0 : 1;
435}
436
437static int do_onenand_erase(struct cmd_tbl *cmdtp, int flag, int argc,
438			    char *const argv[])
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++;
459		}
460	}
461	printf("\nOneNAND erase: ");
462
463	/* skip first two or three arguments, look for offset and size */
464	if (arg_off_size_onenand(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
474static int do_onenand_test(struct cmd_tbl *cmdtp, int flag, int argc,
475			   char *const argv[])
476{
477	ulong ofs;
478	int ret = 0;
479	size_t len;
480
481	/*
482	 * Syntax is:
483	 *   0       1     2       3    4
484	 *   onenand test [force] [off size]
485	 */
486
487	printf("\nOneNAND test: ");
488
489	/* skip first two or three arguments, look for offset and size */
490	if (arg_off_size_onenand(argc - 1, argv + 1, &ofs, &len) != 0)
491		return 1;
492
493	ret = onenand_block_test(ofs, len);
494
495	printf("%s\n", ret ? "ERROR" : "OK");
496
497	return ret == 0 ? 0 : 1;
498}
499
500static int do_onenand_dump(struct cmd_tbl *cmdtp, int flag, int argc,
501			   char *const argv[])
502{
503	ulong ofs;
504	int ret = 0;
505	char *s;
506
507	if (argc < 2)
508		return CMD_RET_USAGE;
509
510	s = strchr(argv[0], '.');
511	ofs = (int)hextoul(argv[1], NULL);
512
513	if (s != NULL && strcmp(s, ".oob") == 0)
514		ret = onenand_dump(mtd, ofs, 1);
515	else
516		ret = onenand_dump(mtd, ofs, 0);
517
518	return ret == 0 ? 1 : 0;
519}
520
521static int do_onenand_markbad(struct cmd_tbl *cmdtp, int flag, int argc,
522			      char *const argv[])
523{
524	int ret = 0;
525	ulong addr;
526
527	argc -= 2;
528	argv += 2;
529
530	if (argc <= 0)
531		return CMD_RET_USAGE;
532
533	while (argc > 0) {
534		addr = hextoul(*argv, NULL);
535
536		if (mtd_block_markbad(mtd, addr)) {
537			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
552static struct cmd_tbl cmd_onenand_sub[] = {
553	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, "", ""),
557	U_BOOT_CMD_MKENT(write.yaffs, 4, 0, do_onenand_write, "", ""),
558	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};
563
564static int do_onenand(struct cmd_tbl *cmdtp, int flag, int argc,
565		      char *const argv[])
566{
567	struct cmd_tbl *c;
568
569	if (argc < 2)
570		return CMD_RET_USAGE;
571
572	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
580	if (c)
581		return c->cmd(cmdtp, flag, argc, argv);
582	else
583		return CMD_RET_USAGE;
584}
585
586U_BOOT_CMD(
587	onenand,	CONFIG_SYS_MAXARGS,	1,	do_onenand,
588	"OneNAND sub-system",
589	"info - show available OneNAND devices\n"
590	"onenand bad - show bad blocks\n"
591	"onenand read[.oob] addr off size\n"
592	"onenand write[.yaffs] addr off size\n"
593	"    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"
599	"onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)"
600);
601