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>
36 static int dev
= -EINVAL
;
37 module_param(dev
, int, S_IRUGO
);
38 MODULE_PARM_DESC(dev
, "MTD device number to use");
40 static struct mtd_info
*mtd
;
41 static unsigned char *twopages
;
42 static unsigned char *writebuf
;
43 static unsigned char *boundary
;
44 static unsigned char *bbt
;
51 static struct rnd_state rnd_state
;
53 static int write_eraseblock(int ebnum
)
55 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
57 prandom_bytes_state(&rnd_state
, writebuf
, mtd
->erasesize
);
59 return mtdtest_write(mtd
, addr
, mtd
->erasesize
, writebuf
);
62 static int verify_eraseblock(int ebnum
)
67 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
70 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
71 addr0
+= mtd
->erasesize
;
74 for (i
= 0; i
< ebcnt
&& bbt
[ebcnt
- i
- 1]; ++i
)
75 addrn
-= mtd
->erasesize
;
77 prandom_bytes_state(&rnd_state
, writebuf
, mtd
->erasesize
);
78 for (j
= 0; j
< pgcnt
- 1; ++j
, addr
+= pgsize
) {
79 /* Do a read to set the internal dataRAMs to different data */
80 err
= mtdtest_read(mtd
, addr0
, bufsize
, twopages
);
83 err
= mtdtest_read(mtd
, addrn
- bufsize
, bufsize
, twopages
);
86 memset(twopages
, 0, bufsize
);
87 err
= mtdtest_read(mtd
, addr
, bufsize
, twopages
);
90 if (memcmp(twopages
, writebuf
+ (j
* pgsize
), bufsize
)) {
91 pr_err("error: verify failed at %#llx\n",
96 /* Check boundary between eraseblocks */
97 if (addr
<= addrn
- pgsize
- pgsize
&& !bbt
[ebnum
+ 1]) {
98 struct rnd_state old_state
= rnd_state
;
100 /* Do a read to set the internal dataRAMs to different data */
101 err
= mtdtest_read(mtd
, addr0
, bufsize
, twopages
);
104 err
= mtdtest_read(mtd
, addrn
- bufsize
, bufsize
, twopages
);
107 memset(twopages
, 0, bufsize
);
108 err
= mtdtest_read(mtd
, addr
, bufsize
, twopages
);
111 memcpy(boundary
, writebuf
+ mtd
->erasesize
- pgsize
, pgsize
);
112 prandom_bytes_state(&rnd_state
, boundary
+ pgsize
, pgsize
);
113 if (memcmp(twopages
, boundary
, bufsize
)) {
114 pr_err("error: verify failed at %#llx\n",
118 rnd_state
= old_state
;
123 static int crosstest(void)
126 loff_t addr
, addr0
, addrn
;
127 unsigned char *pp1
, *pp2
, *pp3
, *pp4
;
129 pr_info("crosstest\n");
130 pp1
= kcalloc(pgsize
, 4, GFP_KERNEL
);
138 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
139 addr0
+= mtd
->erasesize
;
142 for (i
= 0; i
< ebcnt
&& bbt
[ebcnt
- i
- 1]; ++i
)
143 addrn
-= mtd
->erasesize
;
145 /* Read 2nd-to-last page to pp1 */
146 addr
= addrn
- pgsize
- pgsize
;
147 err
= mtdtest_read(mtd
, addr
, pgsize
, pp1
);
153 /* Read 3rd-to-last page to pp1 */
154 addr
= addrn
- pgsize
- pgsize
- pgsize
;
155 err
= mtdtest_read(mtd
, addr
, pgsize
, pp1
);
161 /* Read first page to pp2 */
163 pr_info("reading page at %#llx\n", (long long)addr
);
164 err
= mtdtest_read(mtd
, addr
, pgsize
, pp2
);
170 /* Read last page to pp3 */
171 addr
= addrn
- pgsize
;
172 pr_info("reading page at %#llx\n", (long long)addr
);
173 err
= mtdtest_read(mtd
, addr
, pgsize
, pp3
);
179 /* Read first page again to pp4 */
181 pr_info("reading page at %#llx\n", (long long)addr
);
182 err
= mtdtest_read(mtd
, addr
, pgsize
, pp4
);
188 /* pp2 and pp4 should be the same */
189 pr_info("verifying pages read at %#llx match\n",
191 if (memcmp(pp2
, pp4
, pgsize
)) {
192 pr_err("verify failed!\n");
195 pr_info("crosstest ok\n");
200 static int erasecrosstest(void)
202 int err
= 0, i
, ebnum
, ebnum2
;
204 char *readbuf
= twopages
;
206 pr_info("erasecrosstest\n");
210 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
) {
211 addr0
+= mtd
->erasesize
;
216 while (ebnum2
&& bbt
[ebnum2
])
219 pr_info("erasing block %d\n", ebnum
);
220 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
224 pr_info("writing 1st page of block %d\n", ebnum
);
225 prandom_bytes_state(&rnd_state
, writebuf
, pgsize
);
226 strcpy(writebuf
, "There is no data like this!");
227 err
= mtdtest_write(mtd
, addr0
, pgsize
, writebuf
);
231 pr_info("reading 1st page of block %d\n", ebnum
);
232 memset(readbuf
, 0, pgsize
);
233 err
= mtdtest_read(mtd
, addr0
, pgsize
, readbuf
);
237 pr_info("verifying 1st page of block %d\n", ebnum
);
238 if (memcmp(writebuf
, readbuf
, pgsize
)) {
239 pr_err("verify failed!\n");
244 pr_info("erasing block %d\n", ebnum
);
245 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
249 pr_info("writing 1st page of block %d\n", ebnum
);
250 prandom_bytes_state(&rnd_state
, writebuf
, pgsize
);
251 strcpy(writebuf
, "There is no data like this!");
252 err
= mtdtest_write(mtd
, addr0
, pgsize
, writebuf
);
256 pr_info("erasing block %d\n", ebnum2
);
257 err
= mtdtest_erase_eraseblock(mtd
, ebnum2
);
261 pr_info("reading 1st page of block %d\n", ebnum
);
262 memset(readbuf
, 0, pgsize
);
263 err
= mtdtest_read(mtd
, addr0
, pgsize
, readbuf
);
267 pr_info("verifying 1st page of block %d\n", ebnum
);
268 if (memcmp(writebuf
, readbuf
, pgsize
)) {
269 pr_err("verify failed!\n");
275 pr_info("erasecrosstest ok\n");
279 static int erasetest(void)
281 int err
= 0, i
, ebnum
, ok
= 1;
284 pr_info("erasetest\n");
288 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
) {
289 addr0
+= mtd
->erasesize
;
293 pr_info("erasing block %d\n", ebnum
);
294 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
298 pr_info("writing 1st page of block %d\n", ebnum
);
299 prandom_bytes_state(&rnd_state
, writebuf
, pgsize
);
300 err
= mtdtest_write(mtd
, addr0
, pgsize
, writebuf
);
304 pr_info("erasing block %d\n", ebnum
);
305 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
309 pr_info("reading 1st page of block %d\n", ebnum
);
310 err
= mtdtest_read(mtd
, addr0
, pgsize
, twopages
);
314 pr_info("verifying 1st page of block %d is all 0xff\n",
316 for (i
= 0; i
< pgsize
; ++i
)
317 if (twopages
[i
] != 0xff) {
318 pr_err("verifying all 0xff failed at %d\n",
326 pr_info("erasetest ok\n");
331 static int __init
mtd_pagetest_init(void)
337 printk(KERN_INFO
"\n");
338 printk(KERN_INFO
"=================================================\n");
341 pr_info("Please specify a valid mtd-device via module parameter\n");
342 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
346 pr_info("MTD device: %d\n", dev
);
348 mtd
= get_mtd_device(NULL
, dev
);
351 pr_err("error: cannot get MTD device\n");
355 if (!mtd_type_is_nand(mtd
)) {
356 pr_info("this test requires NAND flash\n");
361 do_div(tmp
, mtd
->erasesize
);
363 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
364 pgsize
= mtd
->writesize
;
366 pr_info("MTD device size %llu, eraseblock size %u, "
367 "page size %u, count of eraseblocks %u, pages per "
368 "eraseblock %u, OOB size %u\n",
369 (unsigned long long)mtd
->size
, mtd
->erasesize
,
370 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
373 bufsize
= pgsize
* 2;
374 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
377 twopages
= kmalloc(bufsize
, GFP_KERNEL
);
380 boundary
= kmalloc(bufsize
, GFP_KERNEL
);
384 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
387 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
391 /* Erase all eraseblocks */
392 pr_info("erasing whole device\n");
393 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
396 pr_info("erased %u eraseblocks\n", ebcnt
);
398 /* Write all eraseblocks */
399 prandom_seed_state(&rnd_state
, 1);
400 pr_info("writing whole device\n");
401 for (i
= 0; i
< ebcnt
; ++i
) {
404 err
= write_eraseblock(i
);
408 pr_info("written up to eraseblock %u\n", i
);
410 err
= mtdtest_relax();
414 pr_info("written %u eraseblocks\n", i
);
416 /* Check all eraseblocks */
417 prandom_seed_state(&rnd_state
, 1);
418 pr_info("verifying all eraseblocks\n");
419 for (i
= 0; i
< ebcnt
; ++i
) {
422 err
= verify_eraseblock(i
);
426 pr_info("verified up to eraseblock %u\n", i
);
428 err
= mtdtest_relax();
432 pr_info("verified %u eraseblocks\n", i
);
439 err
= erasecrosstest();
443 pr_info("skipping erasecrosstest, 2 erase blocks needed\n");
450 pr_info("finished with %d errors\n", errcnt
);
459 pr_info("error %d occurred\n", err
);
460 printk(KERN_INFO
"=================================================\n");
463 module_init(mtd_pagetest_init
);
465 static void __exit
mtd_pagetest_exit(void)
469 module_exit(mtd_pagetest_exit
);
471 MODULE_DESCRIPTION("NAND page test");
472 MODULE_AUTHOR("Adrian Hunter");
473 MODULE_LICENSE("GPL");