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
= kmalloc(pgsize
* 4, GFP_KERNEL
);
136 memset(pp1
, 0, pgsize
* 4);
139 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
140 addr0
+= mtd
->erasesize
;
143 for (i
= 0; i
< ebcnt
&& bbt
[ebcnt
- i
- 1]; ++i
)
144 addrn
-= mtd
->erasesize
;
146 /* Read 2nd-to-last page to pp1 */
147 addr
= addrn
- pgsize
- pgsize
;
148 err
= mtdtest_read(mtd
, addr
, pgsize
, pp1
);
154 /* Read 3rd-to-last page to pp1 */
155 addr
= addrn
- pgsize
- pgsize
- pgsize
;
156 err
= mtdtest_read(mtd
, addr
, pgsize
, pp1
);
162 /* Read first page to pp2 */
164 pr_info("reading page at %#llx\n", (long long)addr
);
165 err
= mtdtest_read(mtd
, addr
, pgsize
, pp2
);
171 /* Read last page to pp3 */
172 addr
= addrn
- pgsize
;
173 pr_info("reading page at %#llx\n", (long long)addr
);
174 err
= mtdtest_read(mtd
, addr
, pgsize
, pp3
);
180 /* Read first page again to pp4 */
182 pr_info("reading page at %#llx\n", (long long)addr
);
183 err
= mtdtest_read(mtd
, addr
, pgsize
, pp4
);
189 /* pp2 and pp4 should be the same */
190 pr_info("verifying pages read at %#llx match\n",
192 if (memcmp(pp2
, pp4
, pgsize
)) {
193 pr_err("verify failed!\n");
196 pr_info("crosstest ok\n");
201 static int erasecrosstest(void)
203 int err
= 0, i
, ebnum
, ebnum2
;
205 char *readbuf
= twopages
;
207 pr_info("erasecrosstest\n");
211 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
) {
212 addr0
+= mtd
->erasesize
;
217 while (ebnum2
&& bbt
[ebnum2
])
220 pr_info("erasing block %d\n", ebnum
);
221 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
225 pr_info("writing 1st page of block %d\n", ebnum
);
226 prandom_bytes_state(&rnd_state
, writebuf
, pgsize
);
227 strcpy(writebuf
, "There is no data like this!");
228 err
= mtdtest_write(mtd
, addr0
, pgsize
, writebuf
);
232 pr_info("reading 1st page of block %d\n", ebnum
);
233 memset(readbuf
, 0, pgsize
);
234 err
= mtdtest_read(mtd
, addr0
, pgsize
, readbuf
);
238 pr_info("verifying 1st page of block %d\n", ebnum
);
239 if (memcmp(writebuf
, readbuf
, pgsize
)) {
240 pr_err("verify failed!\n");
245 pr_info("erasing block %d\n", ebnum
);
246 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
250 pr_info("writing 1st page of block %d\n", ebnum
);
251 prandom_bytes_state(&rnd_state
, writebuf
, pgsize
);
252 strcpy(writebuf
, "There is no data like this!");
253 err
= mtdtest_write(mtd
, addr0
, pgsize
, writebuf
);
257 pr_info("erasing block %d\n", ebnum2
);
258 err
= mtdtest_erase_eraseblock(mtd
, ebnum2
);
262 pr_info("reading 1st page of block %d\n", ebnum
);
263 memset(readbuf
, 0, pgsize
);
264 err
= mtdtest_read(mtd
, addr0
, pgsize
, readbuf
);
268 pr_info("verifying 1st page of block %d\n", ebnum
);
269 if (memcmp(writebuf
, readbuf
, pgsize
)) {
270 pr_err("verify failed!\n");
276 pr_info("erasecrosstest ok\n");
280 static int erasetest(void)
282 int err
= 0, i
, ebnum
, ok
= 1;
285 pr_info("erasetest\n");
289 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
) {
290 addr0
+= mtd
->erasesize
;
294 pr_info("erasing block %d\n", ebnum
);
295 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
299 pr_info("writing 1st page of block %d\n", ebnum
);
300 prandom_bytes_state(&rnd_state
, writebuf
, pgsize
);
301 err
= mtdtest_write(mtd
, addr0
, pgsize
, writebuf
);
305 pr_info("erasing block %d\n", ebnum
);
306 err
= mtdtest_erase_eraseblock(mtd
, ebnum
);
310 pr_info("reading 1st page of block %d\n", ebnum
);
311 err
= mtdtest_read(mtd
, addr0
, pgsize
, twopages
);
315 pr_info("verifying 1st page of block %d is all 0xff\n",
317 for (i
= 0; i
< pgsize
; ++i
)
318 if (twopages
[i
] != 0xff) {
319 pr_err("verifying all 0xff failed at %d\n",
327 pr_info("erasetest ok\n");
332 static int __init
mtd_pagetest_init(void)
338 printk(KERN_INFO
"\n");
339 printk(KERN_INFO
"=================================================\n");
342 pr_info("Please specify a valid mtd-device via module parameter\n");
343 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
347 pr_info("MTD device: %d\n", dev
);
349 mtd
= get_mtd_device(NULL
, dev
);
352 pr_err("error: cannot get MTD device\n");
356 if (!mtd_type_is_nand(mtd
)) {
357 pr_info("this test requires NAND flash\n");
362 do_div(tmp
, mtd
->erasesize
);
364 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
365 pgsize
= mtd
->writesize
;
367 pr_info("MTD device size %llu, eraseblock size %u, "
368 "page size %u, count of eraseblocks %u, pages per "
369 "eraseblock %u, OOB size %u\n",
370 (unsigned long long)mtd
->size
, mtd
->erasesize
,
371 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
374 bufsize
= pgsize
* 2;
375 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
378 twopages
= kmalloc(bufsize
, GFP_KERNEL
);
381 boundary
= kmalloc(bufsize
, GFP_KERNEL
);
385 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
388 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
392 /* Erase all eraseblocks */
393 pr_info("erasing whole device\n");
394 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
397 pr_info("erased %u eraseblocks\n", ebcnt
);
399 /* Write all eraseblocks */
400 prandom_seed_state(&rnd_state
, 1);
401 pr_info("writing whole device\n");
402 for (i
= 0; i
< ebcnt
; ++i
) {
405 err
= write_eraseblock(i
);
409 pr_info("written up to eraseblock %u\n", i
);
411 err
= mtdtest_relax();
415 pr_info("written %u eraseblocks\n", i
);
417 /* Check all eraseblocks */
418 prandom_seed_state(&rnd_state
, 1);
419 pr_info("verifying all eraseblocks\n");
420 for (i
= 0; i
< ebcnt
; ++i
) {
423 err
= verify_eraseblock(i
);
427 pr_info("verified up to eraseblock %u\n", i
);
429 err
= mtdtest_relax();
433 pr_info("verified %u eraseblocks\n", i
);
439 err
= erasecrosstest();
447 pr_info("finished with %d errors\n", errcnt
);
456 pr_info("error %d occurred\n", err
);
457 printk(KERN_INFO
"=================================================\n");
460 module_init(mtd_pagetest_init
);
462 static void __exit
mtd_pagetest_exit(void)
466 module_exit(mtd_pagetest_exit
);
468 MODULE_DESCRIPTION("NAND page test");
469 MODULE_AUTHOR("Adrian Hunter");
470 MODULE_LICENSE("GPL");