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/sched.h>
29 #define PRINT_PREF KERN_INFO "mtd_readtest: "
32 module_param(dev, int, S_IRUGO);
33 MODULE_PARM_DESC(dev, "MTD device number to use");
35 static struct mtd_info *mtd;
36 static unsigned char *iobuf;
37 static unsigned char *iobuf1;
38 static unsigned char *bbt;
44 static int read_eraseblock_by_page(int ebnum)
48 loff_t addr = ebnum * mtd->erasesize;
50 void *oobbuf = iobuf1;
52 for (i = 0; i < pgcnt; i++) {
53 memset(buf, 0 , pgcnt);
54 ret = mtd->read(mtd, addr, pgsize, &read, buf);
57 if (ret || read != pgsize) {
58 printk(PRINT_PREF "error: read failed at %#llx\n",
66 struct mtd_oob_ops ops;
68 ops.mode = MTD_OOB_PLACE;
71 ops.ooblen = mtd->oobsize;
76 ret = mtd->read_oob(mtd, addr, &ops);
77 if (ret || ops.oobretlen != mtd->oobsize) {
78 printk(PRINT_PREF "error: read oob failed at "
79 "%#llx\n", (long long)addr);
85 oobbuf += mtd->oobsize;
94 static void dump_eraseblock(int ebnum)
100 printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
102 for (i = 0; i < n;) {
105 p += sprintf(p, "%05x: ", i);
106 for (j = 0; j < 32 && i < n; j++, i++)
107 p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
108 printk(KERN_CRIT "%s\n", line);
113 printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
115 for (pg = 0, i = 0; pg < pgcnt; pg++)
116 for (oob = 0; oob < n;) {
119 p += sprintf(p, "%05x: ", i);
120 for (j = 0; j < 32 && oob < n; j++, oob++, i++)
121 p += sprintf(p, "%02x",
122 (unsigned int)iobuf1[i]);
123 printk(KERN_CRIT "%s\n", line);
128 static int is_block_bad(int ebnum)
130 loff_t addr = ebnum * mtd->erasesize;
133 ret = mtd->block_isbad(mtd, addr);
135 printk(PRINT_PREF "block %d is bad\n", ebnum);
139 static int scan_for_bad_eraseblocks(void)
143 bbt = kmalloc(ebcnt, GFP_KERNEL);
145 printk(PRINT_PREF "error: cannot allocate memory\n");
148 memset(bbt, 0 , ebcnt);
150 printk(PRINT_PREF "scanning for bad eraseblocks\n");
151 for (i = 0; i < ebcnt; ++i) {
152 bbt[i] = is_block_bad(i) ? 1 : 0;
157 printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
161 static int __init mtd_readtest_init(void)
166 printk(KERN_INFO "\n");
167 printk(KERN_INFO "=================================================\n");
168 printk(PRINT_PREF "MTD device: %d\n", dev);
170 mtd = get_mtd_device(NULL, dev);
173 printk(PRINT_PREF "error: Cannot get MTD device\n");
177 if (mtd->writesize == 1) {
178 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
182 pgsize = mtd->writesize;
185 do_div(tmp, mtd->erasesize);
187 pgcnt = mtd->erasesize / mtd->writesize;
189 printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
190 "page size %u, count of eraseblocks %u, pages per "
191 "eraseblock %u, OOB size %u\n",
192 (unsigned long long)mtd->size, mtd->erasesize,
193 pgsize, ebcnt, pgcnt, mtd->oobsize);
196 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
198 printk(PRINT_PREF "error: cannot allocate memory\n");
201 iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
203 printk(PRINT_PREF "error: cannot allocate memory\n");
207 err = scan_for_bad_eraseblocks();
211 /* Read all eraseblocks 1 page at a time */
212 printk(PRINT_PREF "testing page read\n");
213 for (i = 0; i < ebcnt; ++i) {
218 ret = read_eraseblock_by_page(i);
228 printk(PRINT_PREF "finished with errors\n");
230 printk(PRINT_PREF "finished\n");
239 printk(PRINT_PREF "error %d occurred\n", err);
240 printk(KERN_INFO "=================================================\n");
243 module_init(mtd_readtest_init);
245 static void __exit mtd_readtest_exit(void)
249 module_exit(mtd_readtest_exit);
251 MODULE_DESCRIPTION("Read test module");
252 MODULE_AUTHOR("Adrian Hunter");
253 MODULE_LICENSE("GPL");