blob: 587e1e371c6c50c059785e3b0c354f4f1f6fd450 [file] [log] [blame]
Artem Bityutskiy72091b62008-12-08 13:34:59 +02001/*
2 * Copyright (C) 2006-2008 Nokia Corporation
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 *
17 * Check MTD device read.
18 *
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
20 */
21
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Artem Bityutskiy72091b62008-12-08 13:34:59 +020028#include <linux/sched.h>
29
30#define PRINT_PREF KERN_INFO "mtd_readtest: "
31
32static int dev;
33module_param(dev, int, S_IRUGO);
34MODULE_PARM_DESC(dev, "MTD device number to use");
35
36static struct mtd_info *mtd;
37static unsigned char *iobuf;
38static unsigned char *iobuf1;
39static unsigned char *bbt;
40
41static int pgsize;
42static int ebcnt;
43static int pgcnt;
44
45static int read_eraseblock_by_page(int ebnum)
46{
47 size_t read = 0;
48 int i, ret, err = 0;
49 loff_t addr = ebnum * mtd->erasesize;
50 void *buf = iobuf;
51 void *oobbuf = iobuf1;
52
53 for (i = 0; i < pgcnt; i++) {
54 memset(buf, 0 , pgcnt);
55 ret = mtd->read(mtd, addr, pgsize, &read, buf);
56 if (ret == -EUCLEAN)
57 ret = 0;
58 if (ret || read != pgsize) {
59 printk(PRINT_PREF "error: read failed at %#llx\n",
60 (long long)addr);
61 if (!err)
62 err = ret;
63 if (!err)
64 err = -EINVAL;
65 }
66 if (mtd->oobsize) {
67 struct mtd_oob_ops ops;
68
Brian Norris0612b9d2011-08-30 18:45:40 -070069 ops.mode = MTD_OPS_PLACE_OOB;
Artem Bityutskiy72091b62008-12-08 13:34:59 +020070 ops.len = 0;
71 ops.retlen = 0;
72 ops.ooblen = mtd->oobsize;
73 ops.oobretlen = 0;
74 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +010075 ops.datbuf = NULL;
Artem Bityutskiy72091b62008-12-08 13:34:59 +020076 ops.oobbuf = oobbuf;
77 ret = mtd->read_oob(mtd, addr, &ops);
Brian Norris003bc472011-06-28 16:28:59 -070078 if ((ret && ret != -EUCLEAN) ||
79 ops.oobretlen != mtd->oobsize) {
Artem Bityutskiy72091b62008-12-08 13:34:59 +020080 printk(PRINT_PREF "error: read oob failed at "
81 "%#llx\n", (long long)addr);
82 if (!err)
83 err = ret;
84 if (!err)
85 err = -EINVAL;
86 }
87 oobbuf += mtd->oobsize;
88 }
89 addr += pgsize;
90 buf += pgsize;
91 }
92
93 return err;
94}
95
96static void dump_eraseblock(int ebnum)
97{
98 int i, j, n;
99 char line[128];
100 int pg, oob;
101
102 printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
103 n = mtd->erasesize;
104 for (i = 0; i < n;) {
105 char *p = line;
106
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);
111 cond_resched();
112 }
113 if (!mtd->oobsize)
114 return;
115 printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
116 n = mtd->oobsize;
117 for (pg = 0, i = 0; pg < pgcnt; pg++)
118 for (oob = 0; oob < n;) {
119 char *p = line;
120
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);
126 cond_resched();
127 }
128}
129
130static int is_block_bad(int ebnum)
131{
132 loff_t addr = ebnum * mtd->erasesize;
133 int ret;
134
135 ret = mtd->block_isbad(mtd, addr);
136 if (ret)
137 printk(PRINT_PREF "block %d is bad\n", ebnum);
138 return ret;
139}
140
141static int scan_for_bad_eraseblocks(void)
142{
143 int i, bad = 0;
144
Julia Lawall2bfefa42010-05-13 22:03:15 +0200145 bbt = kzalloc(ebcnt, GFP_KERNEL);
Artem Bityutskiy72091b62008-12-08 13:34:59 +0200146 if (!bbt) {
147 printk(PRINT_PREF "error: cannot allocate memory\n");
148 return -ENOMEM;
149 }
Artem Bityutskiy72091b62008-12-08 13:34:59 +0200150
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100151 /* NOR flash does not implement block_isbad */
152 if (mtd->block_isbad == NULL)
153 return 0;
154
Artem Bityutskiy72091b62008-12-08 13:34:59 +0200155 printk(PRINT_PREF "scanning for bad eraseblocks\n");
156 for (i = 0; i < ebcnt; ++i) {
157 bbt[i] = is_block_bad(i) ? 1 : 0;
158 if (bbt[i])
159 bad += 1;
160 cond_resched();
161 }
162 printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
163 return 0;
164}
165
166static int __init mtd_readtest_init(void)
167{
168 uint64_t tmp;
169 int err, i;
170
171 printk(KERN_INFO "\n");
172 printk(KERN_INFO "=================================================\n");
173 printk(PRINT_PREF "MTD device: %d\n", dev);
174
175 mtd = get_mtd_device(NULL, dev);
176 if (IS_ERR(mtd)) {
177 err = PTR_ERR(mtd);
178 printk(PRINT_PREF "error: Cannot get MTD device\n");
179 return err;
180 }
181
182 if (mtd->writesize == 1) {
183 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
184 "bytes.\n");
185 pgsize = 512;
186 } else
187 pgsize = mtd->writesize;
188
189 tmp = mtd->size;
190 do_div(tmp, mtd->erasesize);
191 ebcnt = tmp;
Morten Thunberg Svendsenf5e2bae2010-01-06 10:48:18 +0100192 pgcnt = mtd->erasesize / pgsize;
Artem Bityutskiy72091b62008-12-08 13:34:59 +0200193
194 printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
195 "page size %u, count of eraseblocks %u, pages per "
196 "eraseblock %u, OOB size %u\n",
197 (unsigned long long)mtd->size, mtd->erasesize,
198 pgsize, ebcnt, pgcnt, mtd->oobsize);
199
200 err = -ENOMEM;
201 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
202 if (!iobuf) {
203 printk(PRINT_PREF "error: cannot allocate memory\n");
204 goto out;
205 }
206 iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
207 if (!iobuf1) {
208 printk(PRINT_PREF "error: cannot allocate memory\n");
209 goto out;
210 }
211
212 err = scan_for_bad_eraseblocks();
213 if (err)
214 goto out;
215
216 /* Read all eraseblocks 1 page at a time */
217 printk(PRINT_PREF "testing page read\n");
218 for (i = 0; i < ebcnt; ++i) {
219 int ret;
220
221 if (bbt[i])
222 continue;
223 ret = read_eraseblock_by_page(i);
224 if (ret) {
225 dump_eraseblock(i);
226 if (!err)
227 err = ret;
228 }
229 cond_resched();
230 }
231
232 if (err)
233 printk(PRINT_PREF "finished with errors\n");
234 else
235 printk(PRINT_PREF "finished\n");
236
237out:
238
239 kfree(iobuf);
240 kfree(iobuf1);
241 kfree(bbt);
242 put_mtd_device(mtd);
243 if (err)
244 printk(PRINT_PREF "error %d occurred\n", err);
245 printk(KERN_INFO "=================================================\n");
246 return err;
247}
248module_init(mtd_readtest_init);
249
250static void __exit mtd_readtest_exit(void)
251{
252 return;
253}
254module_exit(mtd_readtest_exit);
255
256MODULE_DESCRIPTION("Read test module");
257MODULE_AUTHOR("Adrian Hunter");
258MODULE_LICENSE("GPL");