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 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/err.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
30 #define PRINT_PREF KERN_INFO "mtd_readtest: "
32 static int dev = -EINVAL;
33 module_param(dev, int, S_IRUGO);
34 MODULE_PARM_DESC(dev, "MTD device number to use");
36 static struct mtd_info *mtd;
37 static unsigned char *iobuf;
38 static unsigned char *iobuf1;
39 static unsigned char *bbt;
45 static int read_eraseblock_by_page(int ebnum)
49 loff_t addr = ebnum * mtd->erasesize;
51 void *oobbuf = iobuf1;
53 for (i = 0; i < pgcnt; i++) {
54 memset(buf, 0 , pgcnt);
55 ret = mtd->read(mtd, addr, pgsize, &read, buf);
58 if (ret || read != pgsize) {
59 printk(PRINT_PREF "error: read failed at %#llx\n",
67 struct mtd_oob_ops ops;
69 ops.mode = MTD_OPS_PLACE_OOB;
72 ops.ooblen = mtd->oobsize;
77 ret = mtd->read_oob(mtd, addr, &ops);
78 if ((ret && !mtd_is_bitflip(ret)) ||
79 ops.oobretlen != mtd->oobsize) {
80 printk(PRINT_PREF "error: read oob failed at "
81 "%#llx\n", (long long)addr);
87 oobbuf += mtd->oobsize;
96 static void dump_eraseblock(int ebnum)
102 printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
104 for (i = 0; i < n;) {
107 p += sprintf(p, "%05x: ", i);
108 for (j = 0; j < 32 && i < n; j++, i++)
109 p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
110 printk(KERN_CRIT "%s\n", line);
115 printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
117 for (pg = 0, i = 0; pg < pgcnt; pg++)
118 for (oob = 0; oob < n;) {
121 p += sprintf(p, "%05x: ", i);
122 for (j = 0; j < 32 && oob < n; j++, oob++, i++)
123 p += sprintf(p, "%02x",
124 (unsigned int)iobuf1[i]);
125 printk(KERN_CRIT "%s\n", line);
130 static int is_block_bad(int ebnum)
132 loff_t addr = ebnum * mtd->erasesize;
135 ret = mtd->block_isbad(mtd, addr);
137 printk(PRINT_PREF "block %d is bad\n", ebnum);
141 static int scan_for_bad_eraseblocks(void)
145 bbt = kzalloc(ebcnt, GFP_KERNEL);
147 printk(PRINT_PREF "error: cannot allocate memory\n");
151 /* NOR flash does not implement block_isbad */
152 if (mtd->block_isbad == NULL)
155 printk(PRINT_PREF "scanning for bad eraseblocks\n");
156 for (i = 0; i < ebcnt; ++i) {
157 bbt[i] = is_block_bad(i) ? 1 : 0;
162 printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
166 static int __init mtd_readtest_init(void)
171 printk(KERN_INFO "\n");
172 printk(KERN_INFO "=================================================\n");
175 printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
179 printk(PRINT_PREF "MTD device: %d\n", dev);
181 mtd = get_mtd_device(NULL, dev);
184 printk(PRINT_PREF "error: Cannot get MTD device\n");
188 if (mtd->writesize == 1) {
189 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
193 pgsize = mtd->writesize;
196 do_div(tmp, mtd->erasesize);
198 pgcnt = mtd->erasesize / pgsize;
200 printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
201 "page size %u, count of eraseblocks %u, pages per "
202 "eraseblock %u, OOB size %u\n",
203 (unsigned long long)mtd->size, mtd->erasesize,
204 pgsize, ebcnt, pgcnt, mtd->oobsize);
207 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
209 printk(PRINT_PREF "error: cannot allocate memory\n");
212 iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
214 printk(PRINT_PREF "error: cannot allocate memory\n");
218 err = scan_for_bad_eraseblocks();
222 /* Read all eraseblocks 1 page at a time */
223 printk(PRINT_PREF "testing page read\n");
224 for (i = 0; i < ebcnt; ++i) {
229 ret = read_eraseblock_by_page(i);
239 printk(PRINT_PREF "finished with errors\n");
241 printk(PRINT_PREF "finished\n");
250 printk(PRINT_PREF "error %d occurred\n", err);
251 printk(KERN_INFO "=================================================\n");
254 module_init(mtd_readtest_init);
256 static void __exit mtd_readtest_exit(void)
260 module_exit(mtd_readtest_exit);
262 MODULE_DESCRIPTION("Read test module");
263 MODULE_AUTHOR("Adrian Hunter");
264 MODULE_LICENSE("GPL");