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 * Check MTD device read.
19 * 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>
34 static int dev
= -EINVAL
;
35 module_param(dev
, int, S_IRUGO
);
36 MODULE_PARM_DESC(dev
, "MTD device number to use");
38 static struct mtd_info
*mtd
;
39 static unsigned char *iobuf
;
40 static unsigned char *iobuf1
;
41 static unsigned char *bbt
;
47 static int read_eraseblock_by_page(int ebnum
)
50 loff_t addr
= ebnum
* mtd
->erasesize
;
52 void *oobbuf
= iobuf1
;
54 for (i
= 0; i
< pgcnt
; i
++) {
55 memset(buf
, 0 , pgsize
);
56 ret
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
62 struct mtd_oob_ops ops
;
64 ops
.mode
= MTD_OPS_PLACE_OOB
;
67 ops
.ooblen
= mtd
->oobsize
;
72 ret
= mtd_read_oob(mtd
, addr
, &ops
);
73 if ((ret
&& !mtd_is_bitflip(ret
)) ||
74 ops
.oobretlen
!= mtd
->oobsize
) {
75 pr_err("error: read oob failed at "
76 "%#llx\n", (long long)addr
);
82 oobbuf
+= mtd
->oobsize
;
91 static void dump_eraseblock(int ebnum
)
97 pr_info("dumping eraseblock %d\n", ebnum
);
102 p
+= sprintf(p
, "%05x: ", i
);
103 for (j
= 0; j
< 32 && i
< n
; j
++, i
++)
104 p
+= sprintf(p
, "%02x", (unsigned int)iobuf
[i
]);
105 printk(KERN_CRIT
"%s\n", line
);
110 pr_info("dumping oob from eraseblock %d\n", ebnum
);
112 for (pg
= 0, i
= 0; pg
< pgcnt
; pg
++)
113 for (oob
= 0; oob
< n
;) {
116 p
+= sprintf(p
, "%05x: ", i
);
117 for (j
= 0; j
< 32 && oob
< n
; j
++, oob
++, i
++)
118 p
+= sprintf(p
, "%02x",
119 (unsigned int)iobuf1
[i
]);
120 printk(KERN_CRIT
"%s\n", line
);
125 static int __init
mtd_readtest_init(void)
130 printk(KERN_INFO
"\n");
131 printk(KERN_INFO
"=================================================\n");
134 pr_info("Please specify a valid mtd-device via module parameter\n");
138 pr_info("MTD device: %d\n", dev
);
140 mtd
= get_mtd_device(NULL
, dev
);
143 pr_err("error: Cannot get MTD device\n");
147 if (mtd
->writesize
== 1) {
148 pr_info("not NAND flash, assume page size is 512 "
152 pgsize
= mtd
->writesize
;
155 do_div(tmp
, mtd
->erasesize
);
157 pgcnt
= mtd
->erasesize
/ pgsize
;
159 pr_info("MTD device size %llu, eraseblock size %u, "
160 "page size %u, count of eraseblocks %u, pages per "
161 "eraseblock %u, OOB size %u\n",
162 (unsigned long long)mtd
->size
, mtd
->erasesize
,
163 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
166 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
169 iobuf1
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
173 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
176 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
180 /* Read all eraseblocks 1 page at a time */
181 pr_info("testing page read\n");
182 for (i
= 0; i
< ebcnt
; ++i
) {
187 ret
= read_eraseblock_by_page(i
);
197 pr_info("finished with errors\n");
199 pr_info("finished\n");
208 pr_info("error %d occurred\n", err
);
209 printk(KERN_INFO
"=================================================\n");
212 module_init(mtd_readtest_init
);
214 static void __exit
mtd_readtest_exit(void)
218 module_exit(mtd_readtest_exit
);
220 MODULE_DESCRIPTION("Read test module");
221 MODULE_AUTHOR("Adrian Hunter");
222 MODULE_LICENSE("GPL");