2 * Copyright (C) 2006-2007 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 sub-page read and write on MTD device.
18 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/err.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/slab.h>
30 #include <linux/sched.h>
31 #include <linux/random.h>
35 static int dev
= -EINVAL
;
36 module_param(dev
, int, S_IRUGO
);
37 MODULE_PARM_DESC(dev
, "MTD device number to use");
39 static struct mtd_info
*mtd
;
40 static unsigned char *writebuf
;
41 static unsigned char *readbuf
;
42 static unsigned char *bbt
;
49 static struct rnd_state rnd_state
;
51 static inline void clear_data(unsigned char *buf
, size_t len
)
56 static int write_eraseblock(int ebnum
)
60 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
62 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
63 err
= mtd_write(mtd
, addr
, subpgsize
, &written
, writebuf
);
64 if (unlikely(err
|| written
!= subpgsize
)) {
65 pr_err("error: write failed at %#llx\n",
67 if (written
!= subpgsize
) {
68 pr_err(" write size: %#x\n", subpgsize
);
69 pr_err(" written: %#zx\n", written
);
71 return err
? err
: -1;
76 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
77 err
= mtd_write(mtd
, addr
, subpgsize
, &written
, writebuf
);
78 if (unlikely(err
|| written
!= subpgsize
)) {
79 pr_err("error: write failed at %#llx\n",
81 if (written
!= subpgsize
) {
82 pr_err(" write size: %#x\n", subpgsize
);
83 pr_err(" written: %#zx\n", written
);
85 return err
? err
: -1;
91 static int write_eraseblock2(int ebnum
)
95 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
97 for (k
= 1; k
< 33; ++k
) {
98 if (addr
+ (subpgsize
* k
) > (loff_t
)(ebnum
+ 1) * mtd
->erasesize
)
100 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
* k
);
101 err
= mtd_write(mtd
, addr
, subpgsize
* k
, &written
, writebuf
);
102 if (unlikely(err
|| written
!= subpgsize
* k
)) {
103 pr_err("error: write failed at %#llx\n",
105 if (written
!= subpgsize
) {
106 pr_err(" write size: %#x\n",
108 pr_err(" written: %#08zx\n",
111 return err
? err
: -1;
113 addr
+= subpgsize
* k
;
119 static void print_subpage(unsigned char *p
)
123 for (i
= 0; i
< subpgsize
; ) {
124 for (j
= 0; i
< subpgsize
&& j
< 32; ++i
, ++j
)
125 printk("%02x", *p
++);
130 static int verify_eraseblock(int ebnum
)
134 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
136 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
137 clear_data(readbuf
, subpgsize
);
138 err
= mtd_read(mtd
, addr
, subpgsize
, &read
, readbuf
);
139 if (unlikely(err
|| read
!= subpgsize
)) {
140 if (mtd_is_bitflip(err
) && read
== subpgsize
) {
141 pr_info("ECC correction at %#llx\n",
145 pr_err("error: read failed at %#llx\n",
147 return err
? err
: -1;
150 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
))) {
151 pr_err("error: verify failed at %#llx\n",
153 pr_info("------------- written----------------\n");
154 print_subpage(writebuf
);
155 pr_info("------------- read ------------------\n");
156 print_subpage(readbuf
);
157 pr_info("-------------------------------------\n");
163 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
164 clear_data(readbuf
, subpgsize
);
165 err
= mtd_read(mtd
, addr
, subpgsize
, &read
, readbuf
);
166 if (unlikely(err
|| read
!= subpgsize
)) {
167 if (mtd_is_bitflip(err
) && read
== subpgsize
) {
168 pr_info("ECC correction at %#llx\n",
172 pr_err("error: read failed at %#llx\n",
174 return err
? err
: -1;
177 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
))) {
178 pr_info("error: verify failed at %#llx\n",
180 pr_info("------------- written----------------\n");
181 print_subpage(writebuf
);
182 pr_info("------------- read ------------------\n");
183 print_subpage(readbuf
);
184 pr_info("-------------------------------------\n");
191 static int verify_eraseblock2(int ebnum
)
195 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
197 for (k
= 1; k
< 33; ++k
) {
198 if (addr
+ (subpgsize
* k
) > (loff_t
)(ebnum
+ 1) * mtd
->erasesize
)
200 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
* k
);
201 clear_data(readbuf
, subpgsize
* k
);
202 err
= mtd_read(mtd
, addr
, subpgsize
* k
, &read
, readbuf
);
203 if (unlikely(err
|| read
!= subpgsize
* k
)) {
204 if (mtd_is_bitflip(err
) && read
== subpgsize
* k
) {
205 pr_info("ECC correction at %#llx\n",
209 pr_err("error: read failed at "
210 "%#llx\n", (long long)addr
);
211 return err
? err
: -1;
214 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
* k
))) {
215 pr_err("error: verify failed at %#llx\n",
219 addr
+= subpgsize
* k
;
225 static int verify_eraseblock_ff(int ebnum
)
230 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
232 memset(writebuf
, 0xff, subpgsize
);
233 for (j
= 0; j
< mtd
->erasesize
/ subpgsize
; ++j
) {
234 clear_data(readbuf
, subpgsize
);
235 err
= mtd_read(mtd
, addr
, subpgsize
, &read
, readbuf
);
236 if (unlikely(err
|| read
!= subpgsize
)) {
237 if (mtd_is_bitflip(err
) && read
== subpgsize
) {
238 pr_info("ECC correction at %#llx\n",
242 pr_err("error: read failed at "
243 "%#llx\n", (long long)addr
);
244 return err
? err
: -1;
247 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
))) {
248 pr_err("error: verify 0xff failed at "
249 "%#llx\n", (long long)addr
);
258 static int verify_all_eraseblocks_ff(void)
263 pr_info("verifying all eraseblocks for 0xff\n");
264 for (i
= 0; i
< ebcnt
; ++i
) {
267 err
= verify_eraseblock_ff(i
);
271 pr_info("verified up to eraseblock %u\n", i
);
273 err
= mtdtest_relax();
277 pr_info("verified %u eraseblocks\n", i
);
281 static int __init
mtd_subpagetest_init(void)
287 printk(KERN_INFO
"\n");
288 printk(KERN_INFO
"=================================================\n");
291 pr_info("Please specify a valid mtd-device via module parameter\n");
292 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
296 pr_info("MTD device: %d\n", dev
);
298 mtd
= get_mtd_device(NULL
, dev
);
301 pr_err("error: cannot get MTD device\n");
305 if (!mtd_type_is_nand(mtd
)) {
306 pr_info("this test requires NAND flash\n");
310 subpgsize
= mtd
->writesize
>> mtd
->subpage_sft
;
312 do_div(tmp
, mtd
->erasesize
);
314 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
316 pr_info("MTD device size %llu, eraseblock size %u, "
317 "page size %u, subpage size %u, count of eraseblocks %u, "
318 "pages per eraseblock %u, OOB size %u\n",
319 (unsigned long long)mtd
->size
, mtd
->erasesize
,
320 mtd
->writesize
, subpgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
323 bufsize
= subpgsize
* 32;
324 writebuf
= kmalloc(bufsize
, GFP_KERNEL
);
327 readbuf
= kmalloc(bufsize
, GFP_KERNEL
);
330 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
334 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
338 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
342 pr_info("writing whole device\n");
343 prandom_seed_state(&rnd_state
, 1);
344 for (i
= 0; i
< ebcnt
; ++i
) {
347 err
= write_eraseblock(i
);
351 pr_info("written up to eraseblock %u\n", i
);
353 err
= mtdtest_relax();
357 pr_info("written %u eraseblocks\n", i
);
359 prandom_seed_state(&rnd_state
, 1);
360 pr_info("verifying all eraseblocks\n");
361 for (i
= 0; i
< ebcnt
; ++i
) {
364 err
= verify_eraseblock(i
);
368 pr_info("verified up to eraseblock %u\n", i
);
370 err
= mtdtest_relax();
374 pr_info("verified %u eraseblocks\n", i
);
376 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
380 err
= verify_all_eraseblocks_ff();
384 /* Write all eraseblocks */
385 prandom_seed_state(&rnd_state
, 3);
386 pr_info("writing whole device\n");
387 for (i
= 0; i
< ebcnt
; ++i
) {
390 err
= write_eraseblock2(i
);
394 pr_info("written up to eraseblock %u\n", i
);
396 err
= mtdtest_relax();
400 pr_info("written %u eraseblocks\n", i
);
402 /* Check all eraseblocks */
403 prandom_seed_state(&rnd_state
, 3);
404 pr_info("verifying all eraseblocks\n");
405 for (i
= 0; i
< ebcnt
; ++i
) {
408 err
= verify_eraseblock2(i
);
412 pr_info("verified up to eraseblock %u\n", i
);
414 err
= mtdtest_relax();
418 pr_info("verified %u eraseblocks\n", i
);
420 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
424 err
= verify_all_eraseblocks_ff();
428 pr_info("finished with %d errors\n", errcnt
);
436 pr_info("error %d occurred\n", err
);
437 printk(KERN_INFO
"=================================================\n");
440 module_init(mtd_subpagetest_init
);
442 static void __exit
mtd_subpagetest_exit(void)
446 module_exit(mtd_subpagetest_exit
);
448 MODULE_DESCRIPTION("Subpage test module");
449 MODULE_AUTHOR("Adrian Hunter");
450 MODULE_LICENSE("GPL");