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 #include <asm/div64.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/err.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/sched.h>
30 #define PRINT_PREF KERN_INFO "mtd_pagetest: "
33 module_param(dev, int, S_IRUGO);
34 MODULE_PARM_DESC(dev, "MTD device number to use");
36 static struct mtd_info *mtd;
37 static unsigned char *twopages;
38 static unsigned char *writebuf;
39 static unsigned char *boundary;
40 static unsigned char *bbt;
47 static unsigned long next = 1;
49 static inline unsigned int simple_rand(void)
51 next = next * 1103515245 + 12345;
52 return (unsigned int)((next / 65536) % 32768);
55 static inline void simple_srand(unsigned long seed)
60 static void set_random_data(unsigned char *buf, size_t len)
64 for (i = 0; i < len; ++i)
65 buf[i] = simple_rand();
68 static int erase_eraseblock(int ebnum)
72 loff_t addr = ebnum * mtd->erasesize;
74 memset(&ei, 0, sizeof(struct erase_info));
77 ei.len = mtd->erasesize;
79 err = mtd->erase(mtd, &ei);
81 printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
85 if (ei.state == MTD_ERASE_FAILED) {
86 printk(PRINT_PREF "some erase error occurred at EB %d\n",
94 static int write_eraseblock(int ebnum)
98 loff_t addr = ebnum * mtd->erasesize;
100 set_random_data(writebuf, mtd->erasesize);
102 err = mtd->write(mtd, addr, mtd->erasesize, &written, writebuf);
103 if (err || written != mtd->erasesize)
104 printk(PRINT_PREF "error: write failed at %#llx\n",
110 static int verify_eraseblock(int ebnum)
116 loff_t addr = ebnum * mtd->erasesize;
119 for (i = 0; bbt[i] && i < ebcnt; ++i)
120 addr0 += mtd->erasesize;
123 for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)
124 addrn -= mtd->erasesize;
126 set_random_data(writebuf, mtd->erasesize);
127 for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
128 /* Do a read to set the internal dataRAMs to different data */
129 err = mtd->read(mtd, addr0, bufsize, &read, twopages);
132 if (err || read != bufsize) {
133 printk(PRINT_PREF "error: read failed at %#llx\n",
137 err = mtd->read(mtd, addrn - bufsize, bufsize, &read, twopages);
140 if (err || read != bufsize) {
141 printk(PRINT_PREF "error: read failed at %#llx\n",
142 (long long)(addrn - bufsize));
145 memset(twopages, 0, bufsize);
147 err = mtd->read(mtd, addr, bufsize, &read, twopages);
150 if (err || read != bufsize) {
151 printk(PRINT_PREF "error: read failed at %#llx\n",
155 if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
156 printk(PRINT_PREF "error: verify failed at %#llx\n",
161 /* Check boundary between eraseblocks */
162 if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
163 unsigned long oldnext = next;
164 /* Do a read to set the internal dataRAMs to different data */
165 err = mtd->read(mtd, addr0, bufsize, &read, twopages);
168 if (err || read != bufsize) {
169 printk(PRINT_PREF "error: read failed at %#llx\n",
173 err = mtd->read(mtd, addrn - bufsize, bufsize, &read, twopages);
176 if (err || read != bufsize) {
177 printk(PRINT_PREF "error: read failed at %#llx\n",
178 (long long)(addrn - bufsize));
181 memset(twopages, 0, bufsize);
183 err = mtd->read(mtd, addr, bufsize, &read, twopages);
186 if (err || read != bufsize) {
187 printk(PRINT_PREF "error: read failed at %#llx\n",
191 memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
192 set_random_data(boundary + pgsize, pgsize);
193 if (memcmp(twopages, boundary, bufsize)) {
194 printk(PRINT_PREF "error: verify failed at %#llx\n",
203 static int crosstest(void)
207 loff_t addr, addr0, addrn;
208 unsigned char *pp1, *pp2, *pp3, *pp4;
210 printk(PRINT_PREF "crosstest\n");
211 pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
213 printk(PRINT_PREF "error: cannot allocate memory\n");
219 memset(pp1, 0, pgsize * 4);
222 for (i = 0; bbt[i] && i < ebcnt; ++i)
223 addr0 += mtd->erasesize;
226 for (i = 0; bbt[ebcnt - i - 1] && i < ebcnt; ++i)
227 addrn -= mtd->erasesize;
229 /* Read 2nd-to-last page to pp1 */
231 addr = addrn - pgsize - pgsize;
232 err = mtd->read(mtd, addr, pgsize, &read, pp1);
235 if (err || read != pgsize) {
236 printk(PRINT_PREF "error: read failed at %#llx\n",
242 /* Read 3rd-to-last page to pp1 */
244 addr = addrn - pgsize - pgsize - pgsize;
245 err = mtd->read(mtd, addr, pgsize, &read, pp1);
248 if (err || read != pgsize) {
249 printk(PRINT_PREF "error: read failed at %#llx\n",
255 /* Read first page to pp2 */
258 printk(PRINT_PREF "reading page at %#llx\n", (long long)addr);
259 err = mtd->read(mtd, addr, pgsize, &read, pp2);
262 if (err || read != pgsize) {
263 printk(PRINT_PREF "error: read failed at %#llx\n",
269 /* Read last page to pp3 */
271 addr = addrn - pgsize;
272 printk(PRINT_PREF "reading page at %#llx\n", (long long)addr);
273 err = mtd->read(mtd, addr, pgsize, &read, pp3);
276 if (err || read != pgsize) {
277 printk(PRINT_PREF "error: read failed at %#llx\n",
283 /* Read first page again to pp4 */
286 printk(PRINT_PREF "reading page at %#llx\n", (long long)addr);
287 err = mtd->read(mtd, addr, pgsize, &read, pp4);
290 if (err || read != pgsize) {
291 printk(PRINT_PREF "error: read failed at %#llx\n",
297 /* pp2 and pp4 should be the same */
298 printk(PRINT_PREF "verifying pages read at %#llx match\n",
300 if (memcmp(pp2, pp4, pgsize)) {
301 printk(PRINT_PREF "verify failed!\n");
304 printk(PRINT_PREF "crosstest ok\n");
309 static int erasecrosstest(void)
311 size_t read = 0, written = 0;
312 int err = 0, i, ebnum, ok = 1, ebnum2;
314 char *readbuf = twopages;
316 printk(PRINT_PREF "erasecrosstest\n");
320 for (i = 0; bbt[i] && i < ebcnt; ++i) {
321 addr0 += mtd->erasesize;
326 while (ebnum2 && bbt[ebnum2])
329 printk(PRINT_PREF "erasing block %d\n", ebnum);
330 err = erase_eraseblock(ebnum);
334 printk(PRINT_PREF "writing 1st page of block %d\n", ebnum);
335 set_random_data(writebuf, pgsize);
336 strcpy(writebuf, "There is no data like this!");
337 err = mtd->write(mtd, addr0, pgsize, &written, writebuf);
338 if (err || written != pgsize) {
339 printk(PRINT_PREF "error: write failed at %#llx\n",
341 return err ? err : -1;
344 printk(PRINT_PREF "reading 1st page of block %d\n", ebnum);
345 memset(readbuf, 0, pgsize);
346 err = mtd->read(mtd, addr0, pgsize, &read, readbuf);
349 if (err || read != pgsize) {
350 printk(PRINT_PREF "error: read failed at %#llx\n",
352 return err ? err : -1;
355 printk(PRINT_PREF "verifying 1st page of block %d\n", ebnum);
356 if (memcmp(writebuf, readbuf, pgsize)) {
357 printk(PRINT_PREF "verify failed!\n");
363 printk(PRINT_PREF "erasing block %d\n", ebnum);
364 err = erase_eraseblock(ebnum);
368 printk(PRINT_PREF "writing 1st page of block %d\n", ebnum);
369 set_random_data(writebuf, pgsize);
370 strcpy(writebuf, "There is no data like this!");
371 err = mtd->write(mtd, addr0, pgsize, &written, writebuf);
372 if (err || written != pgsize) {
373 printk(PRINT_PREF "error: write failed at %#llx\n",
375 return err ? err : -1;
378 printk(PRINT_PREF "erasing block %d\n", ebnum2);
379 err = erase_eraseblock(ebnum2);
383 printk(PRINT_PREF "reading 1st page of block %d\n", ebnum);
384 memset(readbuf, 0, pgsize);
385 err = mtd->read(mtd, addr0, pgsize, &read, readbuf);
388 if (err || read != pgsize) {
389 printk(PRINT_PREF "error: read failed at %#llx\n",
391 return err ? err : -1;
394 printk(PRINT_PREF "verifying 1st page of block %d\n", ebnum);
395 if (memcmp(writebuf, readbuf, pgsize)) {
396 printk(PRINT_PREF "verify failed!\n");
402 printk(PRINT_PREF "erasecrosstest ok\n");
406 static int erasetest(void)
408 size_t read = 0, written = 0;
409 int err = 0, i, ebnum, ok = 1;
412 printk(PRINT_PREF "erasetest\n");
416 for (i = 0; bbt[i] && i < ebcnt; ++i) {
417 addr0 += mtd->erasesize;
421 printk(PRINT_PREF "erasing block %d\n", ebnum);
422 err = erase_eraseblock(ebnum);
426 printk(PRINT_PREF "writing 1st page of block %d\n", ebnum);
427 set_random_data(writebuf, pgsize);
428 err = mtd->write(mtd, addr0, pgsize, &written, writebuf);
429 if (err || written != pgsize) {
430 printk(PRINT_PREF "error: write failed at %#llx\n",
432 return err ? err : -1;
435 printk(PRINT_PREF "erasing block %d\n", ebnum);
436 err = erase_eraseblock(ebnum);
440 printk(PRINT_PREF "reading 1st page of block %d\n", ebnum);
441 err = mtd->read(mtd, addr0, pgsize, &read, twopages);
444 if (err || read != pgsize) {
445 printk(PRINT_PREF "error: read failed at %#llx\n",
447 return err ? err : -1;
450 printk(PRINT_PREF "verifying 1st page of block %d is all 0xff\n",
452 for (i = 0; i < pgsize; ++i)
453 if (twopages[i] != 0xff) {
454 printk(PRINT_PREF "verifying all 0xff failed at %d\n",
462 printk(PRINT_PREF "erasetest ok\n");
467 static int is_block_bad(int ebnum)
469 loff_t addr = ebnum * mtd->erasesize;
472 ret = mtd->block_isbad(mtd, addr);
474 printk(PRINT_PREF "block %d is bad\n", ebnum);
478 static int scan_for_bad_eraseblocks(void)
482 bbt = kmalloc(ebcnt, GFP_KERNEL);
484 printk(PRINT_PREF "error: cannot allocate memory\n");
487 memset(bbt, 0 , ebcnt);
489 printk(PRINT_PREF "scanning for bad eraseblocks\n");
490 for (i = 0; i < ebcnt; ++i) {
491 bbt[i] = is_block_bad(i) ? 1 : 0;
496 printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
500 static int __init mtd_pagetest_init(void)
506 printk(KERN_INFO "\n");
507 printk(KERN_INFO "=================================================\n");
508 printk(PRINT_PREF "MTD device: %d\n", dev);
510 mtd = get_mtd_device(NULL, dev);
513 printk(PRINT_PREF "error: cannot get MTD device\n");
517 if (mtd->type != MTD_NANDFLASH) {
518 printk(PRINT_PREF "this test requires NAND flash\n");
523 do_div(tmp, mtd->erasesize);
525 pgcnt = mtd->erasesize / mtd->writesize;
527 printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
528 "page size %u, count of eraseblocks %u, pages per "
529 "eraseblock %u, OOB size %u\n",
530 (unsigned long long)mtd->size, mtd->erasesize,
531 pgsize, ebcnt, pgcnt, mtd->oobsize);
534 bufsize = pgsize * 2;
535 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
537 printk(PRINT_PREF "error: cannot allocate memory\n");
540 twopages = kmalloc(bufsize, GFP_KERNEL);
542 printk(PRINT_PREF "error: cannot allocate memory\n");
545 boundary = kmalloc(bufsize, GFP_KERNEL);
547 printk(PRINT_PREF "error: cannot allocate memory\n");
551 err = scan_for_bad_eraseblocks();
555 /* Erase all eraseblocks */
556 printk(PRINT_PREF "erasing whole device\n");
557 for (i = 0; i < ebcnt; ++i) {
560 err = erase_eraseblock(i);
565 printk(PRINT_PREF "erased %u eraseblocks\n", i);
567 /* Write all eraseblocks */
569 printk(PRINT_PREF "writing whole device\n");
570 for (i = 0; i < ebcnt; ++i) {
573 err = write_eraseblock(i);
577 printk(PRINT_PREF "written up to eraseblock %u\n", i);
580 printk(PRINT_PREF "written %u eraseblocks\n", i);
582 /* Check all eraseblocks */
584 printk(PRINT_PREF "verifying all eraseblocks\n");
585 for (i = 0; i < ebcnt; ++i) {
588 err = verify_eraseblock(i);
592 printk(PRINT_PREF "verified up to eraseblock %u\n", i);
595 printk(PRINT_PREF "verified %u eraseblocks\n", i);
601 err = erasecrosstest();
609 printk(PRINT_PREF "finished with %d errors\n", errcnt);
618 printk(PRINT_PREF "error %d occurred\n", err);
619 printk(KERN_INFO "=================================================\n");
622 module_init(mtd_pagetest_init);
624 static void __exit mtd_pagetest_exit(void)
628 module_exit(mtd_pagetest_exit);
630 MODULE_DESCRIPTION("NAND page test");
631 MODULE_AUTHOR("Adrian Hunter");
632 MODULE_LICENSE("GPL");