mtd: introduce mtd_read interface
[deliverable/linux.git] / drivers / mtd / tests / mtd_readtest.c
CommitLineData
72091b68
AB
1/*
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>
5a0e3ad6 27#include <linux/slab.h>
72091b68
AB
28#include <linux/sched.h>
29
30#define PRINT_PREF KERN_INFO "mtd_readtest: "
31
7406060e 32static int dev = -EINVAL;
72091b68
AB
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);
329ad399 55 ret = mtd_read(mtd, addr, pgsize, &read, buf);
72091b68
AB
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
0612b9dd 69 ops.mode = MTD_OPS_PLACE_OOB;
72091b68
AB
70 ops.len = 0;
71 ops.retlen = 0;
72 ops.ooblen = mtd->oobsize;
73 ops.oobretlen = 0;
74 ops.ooboffs = 0;
23d42494 75 ops.datbuf = NULL;
72091b68
AB
76 ops.oobbuf = oobbuf;
77 ret = mtd->read_oob(mtd, addr, &ops);
d57f4054 78 if ((ret && !mtd_is_bitflip(ret)) ||
003bc479 79 ops.oobretlen != mtd->oobsize) {
72091b68
AB
80 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
2bfefa4c 145 bbt = kzalloc(ebcnt, GFP_KERNEL);
72091b68
AB
146 if (!bbt) {
147 printk(PRINT_PREF "error: cannot allocate memory\n");
148 return -ENOMEM;
149 }
72091b68 150
f5e2bae0
MTS
151 /* NOR flash does not implement block_isbad */
152 if (mtd->block_isbad == NULL)
153 return 0;
154
72091b68
AB
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;
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");
7406060e
WS
173
174 if (dev < 0) {
175 printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
176 return -EINVAL;
177 }
178
72091b68
AB
179 printk(PRINT_PREF "MTD device: %d\n", dev);
180
181 mtd = get_mtd_device(NULL, dev);
182 if (IS_ERR(mtd)) {
183 err = PTR_ERR(mtd);
184 printk(PRINT_PREF "error: Cannot get MTD device\n");
185 return err;
186 }
187
188 if (mtd->writesize == 1) {
189 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
190 "bytes.\n");
191 pgsize = 512;
192 } else
193 pgsize = mtd->writesize;
194
195 tmp = mtd->size;
196 do_div(tmp, mtd->erasesize);
197 ebcnt = tmp;
f5e2bae0 198 pgcnt = mtd->erasesize / pgsize;
72091b68
AB
199
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);
205
206 err = -ENOMEM;
207 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
208 if (!iobuf) {
209 printk(PRINT_PREF "error: cannot allocate memory\n");
210 goto out;
211 }
212 iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
213 if (!iobuf1) {
214 printk(PRINT_PREF "error: cannot allocate memory\n");
215 goto out;
216 }
217
218 err = scan_for_bad_eraseblocks();
219 if (err)
220 goto out;
221
222 /* Read all eraseblocks 1 page at a time */
223 printk(PRINT_PREF "testing page read\n");
224 for (i = 0; i < ebcnt; ++i) {
225 int ret;
226
227 if (bbt[i])
228 continue;
229 ret = read_eraseblock_by_page(i);
230 if (ret) {
231 dump_eraseblock(i);
232 if (!err)
233 err = ret;
234 }
235 cond_resched();
236 }
237
238 if (err)
239 printk(PRINT_PREF "finished with errors\n");
240 else
241 printk(PRINT_PREF "finished\n");
242
243out:
244
245 kfree(iobuf);
246 kfree(iobuf1);
247 kfree(bbt);
248 put_mtd_device(mtd);
249 if (err)
250 printk(PRINT_PREF "error %d occurred\n", err);
251 printk(KERN_INFO "=================================================\n");
252 return err;
253}
254module_init(mtd_readtest_init);
255
256static void __exit mtd_readtest_exit(void)
257{
258 return;
259}
260module_exit(mtd_readtest_exit);
261
262MODULE_DESCRIPTION("Read test module");
263MODULE_AUTHOR("Adrian Hunter");
264MODULE_LICENSE("GPL");
This page took 0.219077 seconds and 5 git commands to generate.