2 * Copyright (C) 2006-2008 Nokia Corporation
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; see the file COPYING. If not, write to the Free Software
15 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * Test page read and write on MTD device.
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <asm/div64.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/err.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/slab.h>
31 #include <linux/sched.h>
32 #include <linux/random.h>
34 static int dev = -EINVAL;
35 module_param(dev, int, S_IRUGO);
36 MODULE_PARM_DESC(dev, "MTD device number to use");
38 static struct mtd_info *mtd;
39 static unsigned char *twopages;
40 static unsigned char *writebuf;
41 static unsigned char *boundary;
42 static unsigned char *bbt;
49 static struct rnd_state rnd_state;
51 static int erase_eraseblock(int ebnum)
55 loff_t addr = ebnum * mtd->erasesize;
57 memset(&ei, 0, sizeof(struct erase_info));
60 ei.len = mtd->erasesize;
62 err = mtd_erase(mtd, &ei);
64 pr_err("error %d while erasing EB %d\n", err, ebnum);
68 if (ei.state == MTD_ERASE_FAILED) {
69 pr_err("some erase error occurred at EB %d\n",
77 static int write_eraseblock(int ebnum)
81 loff_t addr = ebnum * mtd->erasesize;
83 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
85 err = mtd_write(mtd, addr, mtd->erasesize, &written, writebuf);
86 if (err || written != mtd->erasesize)
87 pr_err("error: write failed at %#llx\n",
93 static int verify_eraseblock(int ebnum)
99 loff_t addr = ebnum * mtd->erasesize;
102 for (i = 0; i < ebcnt && bbt[i]; ++i)
103 addr0 += mtd->erasesize;
106 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
107 addrn -= mtd->erasesize;
109 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
110 for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
111 /* Do a read to set the internal dataRAMs to different data */
112 err = mtd_read(mtd, addr0, bufsize, &read, twopages);
113 if (mtd_is_bitflip(err))
115 if (err || read != bufsize) {
116 pr_err("error: read failed at %#llx\n",
120 err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages);
121 if (mtd_is_bitflip(err))
123 if (err || read != bufsize) {
124 pr_err("error: read failed at %#llx\n",
125 (long long)(addrn - bufsize));
128 memset(twopages, 0, bufsize);
129 err = mtd_read(mtd, addr, bufsize, &read, twopages);
130 if (mtd_is_bitflip(err))
132 if (err || read != bufsize) {
133 pr_err("error: read failed at %#llx\n",
137 if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
138 pr_err("error: verify failed at %#llx\n",
143 /* Check boundary between eraseblocks */
144 if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
145 struct rnd_state old_state = rnd_state;
147 /* Do a read to set the internal dataRAMs to different data */
148 err = mtd_read(mtd, addr0, bufsize, &read, twopages);
149 if (mtd_is_bitflip(err))
151 if (err || read != bufsize) {
152 pr_err("error: read failed at %#llx\n",
156 err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages);
157 if (mtd_is_bitflip(err))
159 if (err || read != bufsize) {
160 pr_err("error: read failed at %#llx\n",
161 (long long)(addrn - bufsize));
164 memset(twopages, 0, bufsize);
165 err = mtd_read(mtd, addr, bufsize, &read, twopages);
166 if (mtd_is_bitflip(err))
168 if (err || read != bufsize) {
169 pr_err("error: read failed at %#llx\n",
173 memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
174 prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize);
175 if (memcmp(twopages, boundary, bufsize)) {
176 pr_err("error: verify failed at %#llx\n",
180 rnd_state = old_state;
185 static int crosstest(void)
189 loff_t addr, addr0, addrn;
190 unsigned char *pp1, *pp2, *pp3, *pp4;
192 pr_info("crosstest\n");
193 pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
195 pr_err("error: cannot allocate memory\n");
201 memset(pp1, 0, pgsize * 4);
204 for (i = 0; i < ebcnt && bbt[i]; ++i)
205 addr0 += mtd->erasesize;
208 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
209 addrn -= mtd->erasesize;
211 /* Read 2nd-to-last page to pp1 */
212 addr = addrn - pgsize - pgsize;
213 err = mtd_read(mtd, addr, pgsize, &read, pp1);
214 if (mtd_is_bitflip(err))
216 if (err || read != pgsize) {
217 pr_err("error: read failed at %#llx\n",
223 /* Read 3rd-to-last page to pp1 */
224 addr = addrn - pgsize - pgsize - pgsize;
225 err = mtd_read(mtd, addr, pgsize, &read, pp1);
226 if (mtd_is_bitflip(err))
228 if (err || read != pgsize) {
229 pr_err("error: read failed at %#llx\n",
235 /* Read first page to pp2 */
237 pr_info("reading page at %#llx\n", (long long)addr);
238 err = mtd_read(mtd, addr, pgsize, &read, pp2);
239 if (mtd_is_bitflip(err))
241 if (err || read != pgsize) {
242 pr_err("error: read failed at %#llx\n",
248 /* Read last page to pp3 */
249 addr = addrn - pgsize;
250 pr_info("reading page at %#llx\n", (long long)addr);
251 err = mtd_read(mtd, addr, pgsize, &read, pp3);
252 if (mtd_is_bitflip(err))
254 if (err || read != pgsize) {
255 pr_err("error: read failed at %#llx\n",
261 /* Read first page again to pp4 */
263 pr_info("reading page at %#llx\n", (long long)addr);
264 err = mtd_read(mtd, addr, pgsize, &read, pp4);
265 if (mtd_is_bitflip(err))
267 if (err || read != pgsize) {
268 pr_err("error: read failed at %#llx\n",
274 /* pp2 and pp4 should be the same */
275 pr_info("verifying pages read at %#llx match\n",
277 if (memcmp(pp2, pp4, pgsize)) {
278 pr_err("verify failed!\n");
281 pr_info("crosstest ok\n");
286 static int erasecrosstest(void)
288 size_t read, written;
289 int err = 0, i, ebnum, ebnum2;
291 char *readbuf = twopages;
293 pr_info("erasecrosstest\n");
297 for (i = 0; i < ebcnt && bbt[i]; ++i) {
298 addr0 += mtd->erasesize;
303 while (ebnum2 && bbt[ebnum2])
306 pr_info("erasing block %d\n", ebnum);
307 err = erase_eraseblock(ebnum);
311 pr_info("writing 1st page of block %d\n", ebnum);
312 prandom_bytes_state(&rnd_state, writebuf, pgsize);
313 strcpy(writebuf, "There is no data like this!");
314 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
315 if (err || written != pgsize) {
316 pr_info("error: write failed at %#llx\n",
318 return err ? err : -1;
321 pr_info("reading 1st page of block %d\n", ebnum);
322 memset(readbuf, 0, pgsize);
323 err = mtd_read(mtd, addr0, pgsize, &read, readbuf);
324 if (mtd_is_bitflip(err))
326 if (err || read != pgsize) {
327 pr_err("error: read failed at %#llx\n",
329 return err ? err : -1;
332 pr_info("verifying 1st page of block %d\n", ebnum);
333 if (memcmp(writebuf, readbuf, pgsize)) {
334 pr_err("verify failed!\n");
339 pr_info("erasing block %d\n", ebnum);
340 err = erase_eraseblock(ebnum);
344 pr_info("writing 1st page of block %d\n", ebnum);
345 prandom_bytes_state(&rnd_state, writebuf, pgsize);
346 strcpy(writebuf, "There is no data like this!");
347 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
348 if (err || written != pgsize) {
349 pr_err("error: write failed at %#llx\n",
351 return err ? err : -1;
354 pr_info("erasing block %d\n", ebnum2);
355 err = erase_eraseblock(ebnum2);
359 pr_info("reading 1st page of block %d\n", ebnum);
360 memset(readbuf, 0, pgsize);
361 err = mtd_read(mtd, addr0, pgsize, &read, readbuf);
362 if (mtd_is_bitflip(err))
364 if (err || read != pgsize) {
365 pr_err("error: read failed at %#llx\n",
367 return err ? err : -1;
370 pr_info("verifying 1st page of block %d\n", ebnum);
371 if (memcmp(writebuf, readbuf, pgsize)) {
372 pr_err("verify failed!\n");
378 pr_info("erasecrosstest ok\n");
382 static int erasetest(void)
384 size_t read, written;
385 int err = 0, i, ebnum, ok = 1;
388 pr_info("erasetest\n");
392 for (i = 0; i < ebcnt && bbt[i]; ++i) {
393 addr0 += mtd->erasesize;
397 pr_info("erasing block %d\n", ebnum);
398 err = erase_eraseblock(ebnum);
402 pr_info("writing 1st page of block %d\n", ebnum);
403 prandom_bytes_state(&rnd_state, writebuf, pgsize);
404 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
405 if (err || written != pgsize) {
406 pr_err("error: write failed at %#llx\n",
408 return err ? err : -1;
411 pr_info("erasing block %d\n", ebnum);
412 err = erase_eraseblock(ebnum);
416 pr_info("reading 1st page of block %d\n", ebnum);
417 err = mtd_read(mtd, addr0, pgsize, &read, twopages);
418 if (mtd_is_bitflip(err))
420 if (err || read != pgsize) {
421 pr_err("error: read failed at %#llx\n",
423 return err ? err : -1;
426 pr_info("verifying 1st page of block %d is all 0xff\n",
428 for (i = 0; i < pgsize; ++i)
429 if (twopages[i] != 0xff) {
430 pr_err("verifying all 0xff failed at %d\n",
438 pr_info("erasetest ok\n");
443 static int is_block_bad(int ebnum)
445 loff_t addr = ebnum * mtd->erasesize;
448 ret = mtd_block_isbad(mtd, addr);
450 pr_info("block %d is bad\n", ebnum);
454 static int scan_for_bad_eraseblocks(void)
458 bbt = kzalloc(ebcnt, GFP_KERNEL);
460 pr_err("error: cannot allocate memory\n");
464 pr_info("scanning for bad eraseblocks\n");
465 for (i = 0; i < ebcnt; ++i) {
466 bbt[i] = is_block_bad(i) ? 1 : 0;
471 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
475 static int __init mtd_pagetest_init(void)
481 printk(KERN_INFO "\n");
482 printk(KERN_INFO "=================================================\n");
485 pr_info("Please specify a valid mtd-device via module parameter\n");
486 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
490 pr_info("MTD device: %d\n", dev);
492 mtd = get_mtd_device(NULL, dev);
495 pr_err("error: cannot get MTD device\n");
499 if (mtd->type != MTD_NANDFLASH) {
500 pr_info("this test requires NAND flash\n");
505 do_div(tmp, mtd->erasesize);
507 pgcnt = mtd->erasesize / mtd->writesize;
508 pgsize = mtd->writesize;
510 pr_info("MTD device size %llu, eraseblock size %u, "
511 "page size %u, count of eraseblocks %u, pages per "
512 "eraseblock %u, OOB size %u\n",
513 (unsigned long long)mtd->size, mtd->erasesize,
514 pgsize, ebcnt, pgcnt, mtd->oobsize);
517 bufsize = pgsize * 2;
518 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
520 pr_err("error: cannot allocate memory\n");
523 twopages = kmalloc(bufsize, GFP_KERNEL);
525 pr_err("error: cannot allocate memory\n");
528 boundary = kmalloc(bufsize, GFP_KERNEL);
530 pr_err("error: cannot allocate memory\n");
534 err = scan_for_bad_eraseblocks();
538 /* Erase all eraseblocks */
539 pr_info("erasing whole device\n");
540 for (i = 0; i < ebcnt; ++i) {
543 err = erase_eraseblock(i);
548 pr_info("erased %u eraseblocks\n", i);
550 /* Write all eraseblocks */
551 prandom_seed_state(&rnd_state, 1);
552 pr_info("writing whole device\n");
553 for (i = 0; i < ebcnt; ++i) {
556 err = write_eraseblock(i);
560 pr_info("written up to eraseblock %u\n", i);
563 pr_info("written %u eraseblocks\n", i);
565 /* Check all eraseblocks */
566 prandom_seed_state(&rnd_state, 1);
567 pr_info("verifying all eraseblocks\n");
568 for (i = 0; i < ebcnt; ++i) {
571 err = verify_eraseblock(i);
575 pr_info("verified up to eraseblock %u\n", i);
578 pr_info("verified %u eraseblocks\n", i);
584 err = erasecrosstest();
592 pr_info("finished with %d errors\n", errcnt);
601 pr_info("error %d occurred\n", err);
602 printk(KERN_INFO "=================================================\n");
605 module_init(mtd_pagetest_init);
607 static void __exit mtd_pagetest_exit(void)
611 module_exit(mtd_pagetest_exit);
613 MODULE_DESCRIPTION("NAND page test");
614 MODULE_AUTHOR("Adrian Hunter");
615 MODULE_LICENSE("GPL");