2 * Copyright © 2012 NetCommWireless
3 * Iwo Mergler <Iwo.Mergler@netcommwireless.com.au>
5 * Test for multi-bit error recovery on a NAND page This mostly tests the
6 * ECC controller / driver.
8 * There are two test modes:
10 * 0 - artificially inserting bit errors until the ECC fails
11 * This is the default method and fairly quick. It should
12 * be independent of the quality of the FLASH.
14 * 1 - re-writing the same pattern repeatedly until the ECC fails.
15 * This method relies on the physics of NAND FLASH to eventually
16 * generate '0' bits if '1' has been written sufficient times.
17 * Depending on the NAND, the first bit errors will appear after
18 * 1000 or more writes and then will usually snowball, reaching the
19 * limits of the ECC quickly.
21 * The test stops after 10000 cycles, should your FLASH be
22 * exceptionally good and not generate bit errors before that. Try
23 * a different page in that case.
25 * Please note that neither of these tests will significantly 'use up' any
26 * FLASH endurance. Only a maximum of two erase operations will be performed.
29 * This program is free software; you can redistribute it and/or modify it
30 * under the terms of the GNU General Public License version 2 as published by
31 * the Free Software Foundation.
33 * This program is distributed in the hope that it will be useful, but WITHOUT
34 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
38 * You should have received a copy of the GNU General Public License along with
39 * this program; see the file COPYING. If not, write to the Free Software
40 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include <linux/init.h>
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/err.h>
50 #include <linux/mtd/nand.h>
51 #include <linux/slab.h>
54 module_param(dev, int, S_IRUGO);
55 MODULE_PARM_DESC(dev, "MTD device number to use");
57 static unsigned page_offset;
58 module_param(page_offset, uint, S_IRUGO);
59 MODULE_PARM_DESC(page_offset, "Page number relative to dev start");
62 module_param(seed, uint, S_IRUGO);
63 MODULE_PARM_DESC(seed, "Random seed");
66 module_param(mode, int, S_IRUGO);
67 MODULE_PARM_DESC(mode, "0=incremental errors, 1=overwrite test");
69 static unsigned max_overwrite = 10000;
71 static loff_t offset; /* Offset of the page we're using. */
72 static unsigned eraseblock; /* Eraseblock number for our page. */
74 /* We assume that the ECC can correct up to a certain number
75 * of biterrors per subpage. */
76 static unsigned subsize; /* Size of subpages */
77 static unsigned subcount; /* Number of subpages per page */
79 static struct mtd_info *mtd; /* MTD device */
81 static uint8_t *wbuffer; /* One page write / compare buffer */
82 static uint8_t *rbuffer; /* One page read buffer */
84 /* 'random' bytes from known offsets */
85 static uint8_t hash(unsigned offset)
94 /* Reverse bits of result. */
95 c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
96 c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
97 c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
101 static int erase_block(void)
104 struct erase_info ei;
105 loff_t addr = eraseblock * mtd->erasesize;
107 pr_info("erase_block\n");
109 memset(&ei, 0, sizeof(struct erase_info));
112 ei.len = mtd->erasesize;
114 err = mtd_erase(mtd, &ei);
115 if (err || ei.state == MTD_ERASE_FAILED) {
116 pr_err("error %d while erasing\n", err);
125 /* Writes wbuffer to page */
126 static int write_page(int log)
132 pr_info("write_page\n");
134 err = mtd_write(mtd, offset, mtd->writesize, &written, wbuffer);
135 if (err || written != mtd->writesize) {
136 pr_err("error: write failed at %#llx\n", (long long)offset);
144 /* Re-writes the data area while leaving the OOB alone. */
145 static int rewrite_page(int log)
148 struct mtd_oob_ops ops;
151 pr_info("rewrite page\n");
153 ops.mode = MTD_OPS_RAW; /* No ECC */
154 ops.len = mtd->writesize;
159 ops.datbuf = wbuffer;
162 err = mtd_write_oob(mtd, offset, &ops);
163 if (err || ops.retlen != mtd->writesize) {
164 pr_err("error: write_oob failed (%d)\n", err);
172 /* Reads page into rbuffer. Returns number of corrected bit errors (>=0)
174 static int read_page(int log)
178 struct mtd_ecc_stats oldstats;
181 pr_info("read_page\n");
183 /* Saving last mtd stats */
184 memcpy(&oldstats, &mtd->ecc_stats, sizeof(oldstats));
186 err = mtd_read(mtd, offset, mtd->writesize, &read, rbuffer);
188 err = mtd->ecc_stats.corrected - oldstats.corrected;
190 if (err < 0 || read != mtd->writesize) {
191 pr_err("error: read failed at %#llx\n", (long long)offset);
199 /* Verifies rbuffer against random sequence */
200 static int verify_page(int log)
202 unsigned i, errs = 0;
205 pr_info("verify_page\n");
207 for (i = 0; i < mtd->writesize; i++) {
208 if (rbuffer[i] != hash(i+seed)) {
209 pr_err("Error: page offset %u, expected %02x, got %02x\n",
210 i, hash(i+seed), rbuffer[i]);
221 #define CBIT(v, n) ((v) & (1 << (n)))
222 #define BCLR(v, n) ((v) = (v) & ~(1 << (n)))
224 /* Finds the first '1' bit in wbuffer starting at offset 'byte'
225 * and sets it to '0'. */
226 static int insert_biterror(unsigned byte)
230 while (byte < mtd->writesize) {
231 for (bit = 7; bit >= 0; bit--) {
232 if (CBIT(wbuffer[byte], bit)) {
233 BCLR(wbuffer[byte], bit);
234 pr_info("Inserted biterror @ %u/%u\n", byte, bit);
240 pr_err("biterror: Failed to find a '1' bit\n");
244 /* Writes 'random' data to page and then introduces deliberate bit
245 * errors into the page, while verifying each step. */
246 static int incremental_errors_test(void)
250 unsigned errs_per_subpage = 0;
252 pr_info("incremental biterrors test\n");
254 for (i = 0; i < mtd->writesize; i++)
255 wbuffer[i] = hash(i+seed);
263 err = rewrite_page(1);
269 pr_info("Read reported %d corrected bit errors\n", err);
271 pr_err("After %d biterrors per subpage, read reported error %d\n",
272 errs_per_subpage, err);
277 err = verify_page(1);
279 pr_err("ECC failure, read data is incorrect despite read success\n");
283 pr_info("Successfully corrected %d bit errors per subpage\n",
286 for (i = 0; i < subcount; i++) {
287 err = insert_biterror(i * subsize);
299 /* Writes 'random' data to page and then re-writes that same data repeatedly.
300 This eventually develops bit errors (bits written as '1' will slowly become
301 '0'), which are corrected as far as the ECC is capable of. */
302 static int overwrite_test(void)
306 unsigned max_corrected = 0;
308 /* We don't expect more than this many correctable bit errors per
311 static unsigned bitstats[MAXBITS]; /* bit error histogram. */
313 memset(bitstats, 0, sizeof(bitstats));
315 pr_info("overwrite biterrors test\n");
317 for (i = 0; i < mtd->writesize; i++)
318 wbuffer[i] = hash(i+seed);
324 while (opno < max_overwrite) {
326 err = rewrite_page(0);
332 if (err >= MAXBITS) {
333 pr_info("Implausible number of bit errors corrected\n");
338 if (err > max_corrected) {
340 pr_info("Read reported %d corrected bit errors\n",
343 } else { /* err < 0 */
344 pr_info("Read reported error %d\n", err);
349 err = verify_page(0);
351 bitstats[max_corrected] = opno;
352 pr_info("ECC failure, read data is incorrect despite read success\n");
359 /* At this point bitstats[0] contains the number of ops with no bit
360 * errors, bitstats[1] the number of ops with 1 bit error, etc. */
361 pr_info("Bit error histogram (%d operations total):\n", opno);
362 for (i = 0; i < max_corrected; i++)
363 pr_info("Page reads with %3d corrected bit errors: %d\n",
370 static int __init mtd_nandbiterrs_init(void)
375 printk(KERN_INFO "==================================================\n");
376 pr_info("MTD device: %d\n", dev);
378 mtd = get_mtd_device(NULL, dev);
381 pr_err("error: cannot get MTD device\n");
385 if (mtd->type != MTD_NANDFLASH) {
386 pr_info("this test requires NAND flash\n");
391 pr_info("MTD device size %llu, eraseblock=%u, page=%u, oob=%u\n",
392 (unsigned long long)mtd->size, mtd->erasesize,
393 mtd->writesize, mtd->oobsize);
395 subsize = mtd->writesize >> mtd->subpage_sft;
396 subcount = mtd->writesize / subsize;
398 pr_info("Device uses %d subpages of %d bytes\n", subcount, subsize);
400 offset = page_offset * mtd->writesize;
401 eraseblock = mtd_div_by_eb(offset, mtd);
403 pr_info("Using page=%u, offset=%llu, eraseblock=%u\n",
404 page_offset, offset, eraseblock);
406 wbuffer = kmalloc(mtd->writesize, GFP_KERNEL);
412 rbuffer = kmalloc(mtd->writesize, GFP_KERNEL);
423 err = incremental_errors_test();
425 err = overwrite_test();
430 /* We leave the block un-erased in case of test failure. */
436 pr_info("finished successfully.\n");
437 printk(KERN_INFO "==================================================\n");
451 static void __exit mtd_nandbiterrs_exit(void)
456 module_init(mtd_nandbiterrs_init);
457 module_exit(mtd_nandbiterrs_exit);
459 MODULE_DESCRIPTION("NAND bit error recovery test");
460 MODULE_AUTHOR("Iwo Mergler");
461 MODULE_LICENSE("GPL");