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/slab.h>
29 #include <linux/sched.h>
31 #define PRINT_PREF KERN_INFO "mtd_pagetest: "
34 module_param(dev
, int, S_IRUGO
);
35 MODULE_PARM_DESC(dev
, "MTD device number to use");
37 static struct mtd_info
*mtd
;
38 static unsigned char *twopages
;
39 static unsigned char *writebuf
;
40 static unsigned char *boundary
;
41 static unsigned char *bbt
;
48 static unsigned long next
= 1;
50 static inline unsigned int simple_rand(void)
52 next
= next
* 1103515245 + 12345;
53 return (unsigned int)((next
/ 65536) % 32768);
56 static inline void simple_srand(unsigned long seed
)
61 static void set_random_data(unsigned char *buf
, size_t len
)
65 for (i
= 0; i
< len
; ++i
)
66 buf
[i
] = simple_rand();
69 static int erase_eraseblock(int ebnum
)
73 loff_t addr
= ebnum
* mtd
->erasesize
;
75 memset(&ei
, 0, sizeof(struct erase_info
));
78 ei
.len
= mtd
->erasesize
;
80 err
= mtd
->erase(mtd
, &ei
);
82 printk(PRINT_PREF
"error %d while erasing EB %d\n", err
, ebnum
);
86 if (ei
.state
== MTD_ERASE_FAILED
) {
87 printk(PRINT_PREF
"some erase error occurred at EB %d\n",
95 static int write_eraseblock(int ebnum
)
99 loff_t addr
= ebnum
* mtd
->erasesize
;
101 set_random_data(writebuf
, mtd
->erasesize
);
103 err
= mtd
->write(mtd
, addr
, mtd
->erasesize
, &written
, writebuf
);
104 if (err
|| written
!= mtd
->erasesize
)
105 printk(PRINT_PREF
"error: write failed at %#llx\n",
111 static int verify_eraseblock(int ebnum
)
117 loff_t addr
= ebnum
* mtd
->erasesize
;
120 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
121 addr0
+= mtd
->erasesize
;
124 for (i
= 0; i
< ebcnt
&& bbt
[ebcnt
- i
- 1]; ++i
)
125 addrn
-= mtd
->erasesize
;
127 set_random_data(writebuf
, mtd
->erasesize
);
128 for (j
= 0; j
< pgcnt
- 1; ++j
, addr
+= pgsize
) {
129 /* Do a read to set the internal dataRAMs to different data */
130 err
= mtd
->read(mtd
, addr0
, bufsize
, &read
, twopages
);
133 if (err
|| read
!= bufsize
) {
134 printk(PRINT_PREF
"error: read failed at %#llx\n",
138 err
= mtd
->read(mtd
, addrn
- bufsize
, bufsize
, &read
, twopages
);
141 if (err
|| read
!= bufsize
) {
142 printk(PRINT_PREF
"error: read failed at %#llx\n",
143 (long long)(addrn
- bufsize
));
146 memset(twopages
, 0, bufsize
);
148 err
= mtd
->read(mtd
, addr
, bufsize
, &read
, twopages
);
151 if (err
|| read
!= bufsize
) {
152 printk(PRINT_PREF
"error: read failed at %#llx\n",
156 if (memcmp(twopages
, writebuf
+ (j
* pgsize
), bufsize
)) {
157 printk(PRINT_PREF
"error: verify failed at %#llx\n",
162 /* Check boundary between eraseblocks */
163 if (addr
<= addrn
- pgsize
- pgsize
&& !bbt
[ebnum
+ 1]) {
164 unsigned long oldnext
= next
;
165 /* Do a read to set the internal dataRAMs to different data */
166 err
= mtd
->read(mtd
, addr0
, bufsize
, &read
, twopages
);
169 if (err
|| read
!= bufsize
) {
170 printk(PRINT_PREF
"error: read failed at %#llx\n",
174 err
= mtd
->read(mtd
, addrn
- bufsize
, bufsize
, &read
, twopages
);
177 if (err
|| read
!= bufsize
) {
178 printk(PRINT_PREF
"error: read failed at %#llx\n",
179 (long long)(addrn
- bufsize
));
182 memset(twopages
, 0, bufsize
);
184 err
= mtd
->read(mtd
, addr
, bufsize
, &read
, twopages
);
187 if (err
|| read
!= bufsize
) {
188 printk(PRINT_PREF
"error: read failed at %#llx\n",
192 memcpy(boundary
, writebuf
+ mtd
->erasesize
- pgsize
, pgsize
);
193 set_random_data(boundary
+ pgsize
, pgsize
);
194 if (memcmp(twopages
, boundary
, bufsize
)) {
195 printk(PRINT_PREF
"error: verify failed at %#llx\n",
204 static int crosstest(void)
208 loff_t addr
, addr0
, addrn
;
209 unsigned char *pp1
, *pp2
, *pp3
, *pp4
;
211 printk(PRINT_PREF
"crosstest\n");
212 pp1
= kmalloc(pgsize
* 4, GFP_KERNEL
);
214 printk(PRINT_PREF
"error: cannot allocate memory\n");
220 memset(pp1
, 0, pgsize
* 4);
223 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
224 addr0
+= mtd
->erasesize
;
227 for (i
= 0; i
< ebcnt
&& bbt
[ebcnt
- i
- 1]; ++i
)
228 addrn
-= mtd
->erasesize
;
230 /* Read 2nd-to-last page to pp1 */
232 addr
= addrn
- pgsize
- pgsize
;
233 err
= mtd
->read(mtd
, addr
, pgsize
, &read
, pp1
);
236 if (err
|| read
!= pgsize
) {
237 printk(PRINT_PREF
"error: read failed at %#llx\n",
243 /* Read 3rd-to-last page to pp1 */
245 addr
= addrn
- pgsize
- pgsize
- pgsize
;
246 err
= mtd
->read(mtd
, addr
, pgsize
, &read
, pp1
);
249 if (err
|| read
!= pgsize
) {
250 printk(PRINT_PREF
"error: read failed at %#llx\n",
256 /* Read first page to pp2 */
259 printk(PRINT_PREF
"reading page at %#llx\n", (long long)addr
);
260 err
= mtd
->read(mtd
, addr
, pgsize
, &read
, pp2
);
263 if (err
|| read
!= pgsize
) {
264 printk(PRINT_PREF
"error: read failed at %#llx\n",
270 /* Read last page to pp3 */
272 addr
= addrn
- pgsize
;
273 printk(PRINT_PREF
"reading page at %#llx\n", (long long)addr
);
274 err
= mtd
->read(mtd
, addr
, pgsize
, &read
, pp3
);
277 if (err
|| read
!= pgsize
) {
278 printk(PRINT_PREF
"error: read failed at %#llx\n",
284 /* Read first page again to pp4 */
287 printk(PRINT_PREF
"reading page at %#llx\n", (long long)addr
);
288 err
= mtd
->read(mtd
, addr
, pgsize
, &read
, pp4
);
291 if (err
|| read
!= pgsize
) {
292 printk(PRINT_PREF
"error: read failed at %#llx\n",
298 /* pp2 and pp4 should be the same */
299 printk(PRINT_PREF
"verifying pages read at %#llx match\n",
301 if (memcmp(pp2
, pp4
, pgsize
)) {
302 printk(PRINT_PREF
"verify failed!\n");
305 printk(PRINT_PREF
"crosstest ok\n");
310 static int erasecrosstest(void)
312 size_t read
= 0, written
= 0;
313 int err
= 0, i
, ebnum
, ebnum2
;
315 char *readbuf
= twopages
;
317 printk(PRINT_PREF
"erasecrosstest\n");
321 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
) {
322 addr0
+= mtd
->erasesize
;
327 while (ebnum2
&& bbt
[ebnum2
])
330 printk(PRINT_PREF
"erasing block %d\n", ebnum
);
331 err
= erase_eraseblock(ebnum
);
335 printk(PRINT_PREF
"writing 1st page of block %d\n", ebnum
);
336 set_random_data(writebuf
, pgsize
);
337 strcpy(writebuf
, "There is no data like this!");
338 err
= mtd
->write(mtd
, addr0
, pgsize
, &written
, writebuf
);
339 if (err
|| written
!= pgsize
) {
340 printk(PRINT_PREF
"error: write failed at %#llx\n",
342 return err
? err
: -1;
345 printk(PRINT_PREF
"reading 1st page of block %d\n", ebnum
);
346 memset(readbuf
, 0, pgsize
);
347 err
= mtd
->read(mtd
, addr0
, pgsize
, &read
, readbuf
);
350 if (err
|| read
!= pgsize
) {
351 printk(PRINT_PREF
"error: read failed at %#llx\n",
353 return err
? err
: -1;
356 printk(PRINT_PREF
"verifying 1st page of block %d\n", ebnum
);
357 if (memcmp(writebuf
, readbuf
, pgsize
)) {
358 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; i
< ebcnt
&& bbt
[i
]; ++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
= kzalloc(ebcnt
, GFP_KERNEL
);
484 printk(PRINT_PREF
"error: cannot allocate memory\n");
488 printk(PRINT_PREF
"scanning for bad eraseblocks\n");
489 for (i
= 0; i
< ebcnt
; ++i
) {
490 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
495 printk(PRINT_PREF
"scanned %d eraseblocks, %d are bad\n", i
, bad
);
499 static int __init
mtd_pagetest_init(void)
505 printk(KERN_INFO
"\n");
506 printk(KERN_INFO
"=================================================\n");
507 printk(PRINT_PREF
"MTD device: %d\n", dev
);
509 mtd
= get_mtd_device(NULL
, dev
);
512 printk(PRINT_PREF
"error: cannot get MTD device\n");
516 if (mtd
->type
!= MTD_NANDFLASH
) {
517 printk(PRINT_PREF
"this test requires NAND flash\n");
522 do_div(tmp
, mtd
->erasesize
);
524 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
525 pgsize
= 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");