2 * Copyright (C) 2006-2007 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 * Test sub-page read and write on MTD device.
18 * 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>
31 #include <linux/random.h>
33 static int dev = -EINVAL;
34 module_param(dev, int, S_IRUGO);
35 MODULE_PARM_DESC(dev, "MTD device number to use");
37 static struct mtd_info *mtd;
38 static unsigned char *writebuf;
39 static unsigned char *readbuf;
40 static unsigned char *bbt;
47 static struct rnd_state rnd_state;
49 static inline void clear_data(unsigned char *buf, size_t len)
54 static int erase_eraseblock(int ebnum)
58 loff_t addr = ebnum * mtd->erasesize;
60 memset(&ei, 0, sizeof(struct erase_info));
63 ei.len = mtd->erasesize;
65 err = mtd_erase(mtd, &ei);
67 pr_err("error %d while erasing EB %d\n", err, ebnum);
71 if (ei.state == MTD_ERASE_FAILED) {
72 pr_err("some erase error occurred at EB %d\n",
80 static int erase_whole_device(void)
85 pr_info("erasing whole device\n");
86 for (i = 0; i < ebcnt; ++i) {
89 err = erase_eraseblock(i);
94 pr_info("erased %u eraseblocks\n", i);
98 static int write_eraseblock(int ebnum)
102 loff_t addr = ebnum * mtd->erasesize;
104 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
105 err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
106 if (unlikely(err || written != subpgsize)) {
107 pr_err("error: write failed at %#llx\n",
109 if (written != subpgsize) {
110 pr_err(" write size: %#x\n", subpgsize);
111 pr_err(" written: %#zx\n", written);
113 return err ? err : -1;
118 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
119 err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
120 if (unlikely(err || written != subpgsize)) {
121 pr_err("error: write failed at %#llx\n",
123 if (written != subpgsize) {
124 pr_err(" write size: %#x\n", subpgsize);
125 pr_err(" written: %#zx\n", written);
127 return err ? err : -1;
133 static int write_eraseblock2(int ebnum)
137 loff_t addr = ebnum * mtd->erasesize;
139 for (k = 1; k < 33; ++k) {
140 if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
142 prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
143 err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
144 if (unlikely(err || written != subpgsize * k)) {
145 pr_err("error: write failed at %#llx\n",
147 if (written != subpgsize) {
148 pr_err(" write size: %#x\n",
150 pr_err(" written: %#08zx\n",
153 return err ? err : -1;
155 addr += subpgsize * k;
161 static void print_subpage(unsigned char *p)
165 for (i = 0; i < subpgsize; ) {
166 for (j = 0; i < subpgsize && j < 32; ++i, ++j)
167 printk("%02x", *p++);
172 static int verify_eraseblock(int ebnum)
176 loff_t addr = ebnum * mtd->erasesize;
178 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
179 clear_data(readbuf, subpgsize);
180 err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
181 if (unlikely(err || read != subpgsize)) {
182 if (mtd_is_bitflip(err) && read == subpgsize) {
183 pr_info("ECC correction at %#llx\n",
187 pr_err("error: read failed at %#llx\n",
189 return err ? err : -1;
192 if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
193 pr_err("error: verify failed at %#llx\n",
195 pr_info("------------- written----------------\n");
196 print_subpage(writebuf);
197 pr_info("------------- read ------------------\n");
198 print_subpage(readbuf);
199 pr_info("-------------------------------------\n");
205 prandom_bytes_state(&rnd_state, writebuf, subpgsize);
206 clear_data(readbuf, subpgsize);
207 err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
208 if (unlikely(err || read != subpgsize)) {
209 if (mtd_is_bitflip(err) && read == subpgsize) {
210 pr_info("ECC correction at %#llx\n",
214 pr_err("error: read failed at %#llx\n",
216 return err ? err : -1;
219 if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
220 pr_info("error: verify failed at %#llx\n",
222 pr_info("------------- written----------------\n");
223 print_subpage(writebuf);
224 pr_info("------------- read ------------------\n");
225 print_subpage(readbuf);
226 pr_info("-------------------------------------\n");
233 static int verify_eraseblock2(int ebnum)
237 loff_t addr = ebnum * mtd->erasesize;
239 for (k = 1; k < 33; ++k) {
240 if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
242 prandom_bytes_state(&rnd_state, writebuf, subpgsize * k);
243 clear_data(readbuf, subpgsize * k);
244 err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf);
245 if (unlikely(err || read != subpgsize * k)) {
246 if (mtd_is_bitflip(err) && read == subpgsize * k) {
247 pr_info("ECC correction at %#llx\n",
251 pr_err("error: read failed at "
252 "%#llx\n", (long long)addr);
253 return err ? err : -1;
256 if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
257 pr_err("error: verify failed at %#llx\n",
261 addr += subpgsize * k;
267 static int verify_eraseblock_ff(int ebnum)
272 loff_t addr = ebnum * mtd->erasesize;
274 memset(writebuf, 0xff, subpgsize);
275 for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
276 clear_data(readbuf, subpgsize);
277 err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
278 if (unlikely(err || read != subpgsize)) {
279 if (mtd_is_bitflip(err) && read == subpgsize) {
280 pr_info("ECC correction at %#llx\n",
284 pr_err("error: read failed at "
285 "%#llx\n", (long long)addr);
286 return err ? err : -1;
289 if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
290 pr_err("error: verify 0xff failed at "
291 "%#llx\n", (long long)addr);
300 static int verify_all_eraseblocks_ff(void)
305 pr_info("verifying all eraseblocks for 0xff\n");
306 for (i = 0; i < ebcnt; ++i) {
309 err = verify_eraseblock_ff(i);
313 pr_info("verified up to eraseblock %u\n", i);
316 pr_info("verified %u eraseblocks\n", i);
320 static int is_block_bad(int ebnum)
322 loff_t addr = ebnum * mtd->erasesize;
325 ret = mtd_block_isbad(mtd, addr);
327 pr_info("block %d is bad\n", ebnum);
331 static int scan_for_bad_eraseblocks(void)
335 bbt = kzalloc(ebcnt, GFP_KERNEL);
337 pr_err("error: cannot allocate memory\n");
341 pr_info("scanning for bad eraseblocks\n");
342 for (i = 0; i < ebcnt; ++i) {
343 bbt[i] = is_block_bad(i) ? 1 : 0;
348 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
352 static int __init mtd_subpagetest_init(void)
358 printk(KERN_INFO "\n");
359 printk(KERN_INFO "=================================================\n");
362 pr_info("Please specify a valid mtd-device via module parameter\n");
363 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
367 pr_info("MTD device: %d\n", dev);
369 mtd = get_mtd_device(NULL, dev);
372 pr_err("error: cannot get MTD device\n");
376 if (mtd->type != MTD_NANDFLASH) {
377 pr_info("this test requires NAND flash\n");
381 subpgsize = mtd->writesize >> mtd->subpage_sft;
383 do_div(tmp, mtd->erasesize);
385 pgcnt = mtd->erasesize / mtd->writesize;
387 pr_info("MTD device size %llu, eraseblock size %u, "
388 "page size %u, subpage size %u, count of eraseblocks %u, "
389 "pages per eraseblock %u, OOB size %u\n",
390 (unsigned long long)mtd->size, mtd->erasesize,
391 mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
394 bufsize = subpgsize * 32;
395 writebuf = kmalloc(bufsize, GFP_KERNEL);
397 pr_info("error: cannot allocate memory\n");
400 readbuf = kmalloc(bufsize, GFP_KERNEL);
402 pr_info("error: cannot allocate memory\n");
406 err = scan_for_bad_eraseblocks();
410 err = erase_whole_device();
414 pr_info("writing whole device\n");
415 prandom_seed_state(&rnd_state, 1);
416 for (i = 0; i < ebcnt; ++i) {
419 err = write_eraseblock(i);
423 pr_info("written up to eraseblock %u\n", i);
426 pr_info("written %u eraseblocks\n", i);
428 prandom_seed_state(&rnd_state, 1);
429 pr_info("verifying all eraseblocks\n");
430 for (i = 0; i < ebcnt; ++i) {
433 err = verify_eraseblock(i);
437 pr_info("verified up to eraseblock %u\n", i);
440 pr_info("verified %u eraseblocks\n", i);
442 err = erase_whole_device();
446 err = verify_all_eraseblocks_ff();
450 /* Write all eraseblocks */
451 prandom_seed_state(&rnd_state, 3);
452 pr_info("writing whole device\n");
453 for (i = 0; i < ebcnt; ++i) {
456 err = write_eraseblock2(i);
460 pr_info("written up to eraseblock %u\n", i);
463 pr_info("written %u eraseblocks\n", i);
465 /* Check all eraseblocks */
466 prandom_seed_state(&rnd_state, 3);
467 pr_info("verifying all eraseblocks\n");
468 for (i = 0; i < ebcnt; ++i) {
471 err = verify_eraseblock2(i);
475 pr_info("verified up to eraseblock %u\n", i);
478 pr_info("verified %u eraseblocks\n", i);
480 err = erase_whole_device();
484 err = verify_all_eraseblocks_ff();
488 pr_info("finished with %d errors\n", errcnt);
496 pr_info("error %d occurred\n", err);
497 printk(KERN_INFO "=================================================\n");
500 module_init(mtd_subpagetest_init);
502 static void __exit mtd_subpagetest_exit(void)
506 module_exit(mtd_subpagetest_exit);
508 MODULE_DESCRIPTION("Subpage test module");
509 MODULE_AUTHOR("Adrian Hunter");
510 MODULE_LICENSE("GPL");