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>
55 module_param(dev
, int, S_IRUGO
);
56 MODULE_PARM_DESC(dev
, "MTD device number to use");
58 static unsigned page_offset
;
59 module_param(page_offset
, uint
, S_IRUGO
);
60 MODULE_PARM_DESC(page_offset
, "Page number relative to dev start");
63 module_param(seed
, uint
, S_IRUGO
);
64 MODULE_PARM_DESC(seed
, "Random seed");
67 module_param(mode
, int, S_IRUGO
);
68 MODULE_PARM_DESC(mode
, "0=incremental errors, 1=overwrite test");
70 static unsigned max_overwrite
= 10000;
72 static loff_t offset
; /* Offset of the page we're using. */
73 static unsigned eraseblock
; /* Eraseblock number for our page. */
75 /* We assume that the ECC can correct up to a certain number
76 * of biterrors per subpage. */
77 static unsigned subsize
; /* Size of subpages */
78 static unsigned subcount
; /* Number of subpages per page */
80 static struct mtd_info
*mtd
; /* MTD device */
82 static uint8_t *wbuffer
; /* One page write / compare buffer */
83 static uint8_t *rbuffer
; /* One page read buffer */
85 /* 'random' bytes from known offsets */
86 static uint8_t hash(unsigned offset
)
95 /* Reverse bits of result. */
96 c
= (c
& 0x0F) << 4 | (c
& 0xF0) >> 4;
97 c
= (c
& 0x33) << 2 | (c
& 0xCC) >> 2;
98 c
= (c
& 0x55) << 1 | (c
& 0xAA) >> 1;
102 /* Writes wbuffer to page */
103 static int write_page(int log
)
106 pr_info("write_page\n");
108 return mtdtest_write(mtd
, offset
, mtd
->writesize
, wbuffer
);
111 /* Re-writes the data area while leaving the OOB alone. */
112 static int rewrite_page(int log
)
115 struct mtd_oob_ops ops
;
118 pr_info("rewrite page\n");
120 ops
.mode
= MTD_OPS_RAW
; /* No ECC */
121 ops
.len
= mtd
->writesize
;
126 ops
.datbuf
= wbuffer
;
129 err
= mtd_write_oob(mtd
, offset
, &ops
);
130 if (err
|| ops
.retlen
!= mtd
->writesize
) {
131 pr_err("error: write_oob failed (%d)\n", err
);
139 /* Reads page into rbuffer. Returns number of corrected bit errors (>=0)
141 static int read_page(int log
)
145 struct mtd_ecc_stats oldstats
;
148 pr_info("read_page\n");
150 /* Saving last mtd stats */
151 memcpy(&oldstats
, &mtd
->ecc_stats
, sizeof(oldstats
));
153 err
= mtd_read(mtd
, offset
, mtd
->writesize
, &read
, rbuffer
);
155 err
= mtd
->ecc_stats
.corrected
- oldstats
.corrected
;
157 if (err
< 0 || read
!= mtd
->writesize
) {
158 pr_err("error: read failed at %#llx\n", (long long)offset
);
166 /* Verifies rbuffer against random sequence */
167 static int verify_page(int log
)
169 unsigned i
, errs
= 0;
172 pr_info("verify_page\n");
174 for (i
= 0; i
< mtd
->writesize
; i
++) {
175 if (rbuffer
[i
] != hash(i
+seed
)) {
176 pr_err("Error: page offset %u, expected %02x, got %02x\n",
177 i
, hash(i
+seed
), rbuffer
[i
]);
188 #define CBIT(v, n) ((v) & (1 << (n)))
189 #define BCLR(v, n) ((v) = (v) & ~(1 << (n)))
191 /* Finds the first '1' bit in wbuffer starting at offset 'byte'
192 * and sets it to '0'. */
193 static int insert_biterror(unsigned byte
)
197 while (byte
< mtd
->writesize
) {
198 for (bit
= 7; bit
>= 0; bit
--) {
199 if (CBIT(wbuffer
[byte
], bit
)) {
200 BCLR(wbuffer
[byte
], bit
);
201 pr_info("Inserted biterror @ %u/%u\n", byte
, bit
);
207 pr_err("biterror: Failed to find a '1' bit\n");
211 /* Writes 'random' data to page and then introduces deliberate bit
212 * errors into the page, while verifying each step. */
213 static int incremental_errors_test(void)
217 unsigned errs_per_subpage
= 0;
219 pr_info("incremental biterrors test\n");
221 for (i
= 0; i
< mtd
->writesize
; i
++)
222 wbuffer
[i
] = hash(i
+seed
);
230 err
= rewrite_page(1);
236 pr_info("Read reported %d corrected bit errors\n", err
);
238 pr_err("After %d biterrors per subpage, read reported error %d\n",
239 errs_per_subpage
, err
);
244 err
= verify_page(1);
246 pr_err("ECC failure, read data is incorrect despite read success\n");
250 pr_info("Successfully corrected %d bit errors per subpage\n",
253 for (i
= 0; i
< subcount
; i
++) {
254 err
= insert_biterror(i
* subsize
);
266 /* Writes 'random' data to page and then re-writes that same data repeatedly.
267 This eventually develops bit errors (bits written as '1' will slowly become
268 '0'), which are corrected as far as the ECC is capable of. */
269 static int overwrite_test(void)
273 unsigned max_corrected
= 0;
275 /* We don't expect more than this many correctable bit errors per
278 static unsigned bitstats
[MAXBITS
]; /* bit error histogram. */
280 memset(bitstats
, 0, sizeof(bitstats
));
282 pr_info("overwrite biterrors test\n");
284 for (i
= 0; i
< mtd
->writesize
; i
++)
285 wbuffer
[i
] = hash(i
+seed
);
291 while (opno
< max_overwrite
) {
293 err
= rewrite_page(0);
299 if (err
>= MAXBITS
) {
300 pr_info("Implausible number of bit errors corrected\n");
305 if (err
> max_corrected
) {
307 pr_info("Read reported %d corrected bit errors\n",
310 } else { /* err < 0 */
311 pr_info("Read reported error %d\n", err
);
316 err
= verify_page(0);
318 bitstats
[max_corrected
] = opno
;
319 pr_info("ECC failure, read data is incorrect despite read success\n");
323 err
= mtdtest_relax();
330 /* At this point bitstats[0] contains the number of ops with no bit
331 * errors, bitstats[1] the number of ops with 1 bit error, etc. */
332 pr_info("Bit error histogram (%d operations total):\n", opno
);
333 for (i
= 0; i
< max_corrected
; i
++)
334 pr_info("Page reads with %3d corrected bit errors: %d\n",
341 static int __init
mtd_nandbiterrs_init(void)
346 printk(KERN_INFO
"==================================================\n");
347 pr_info("MTD device: %d\n", dev
);
349 mtd
= get_mtd_device(NULL
, dev
);
352 pr_err("error: cannot get MTD device\n");
356 if (!mtd_type_is_nand(mtd
)) {
357 pr_info("this test requires NAND flash\n");
362 pr_info("MTD device size %llu, eraseblock=%u, page=%u, oob=%u\n",
363 (unsigned long long)mtd
->size
, mtd
->erasesize
,
364 mtd
->writesize
, mtd
->oobsize
);
366 subsize
= mtd
->writesize
>> mtd
->subpage_sft
;
367 subcount
= mtd
->writesize
/ subsize
;
369 pr_info("Device uses %d subpages of %d bytes\n", subcount
, subsize
);
371 offset
= (loff_t
)page_offset
* mtd
->writesize
;
372 eraseblock
= mtd_div_by_eb(offset
, mtd
);
374 pr_info("Using page=%u, offset=%llu, eraseblock=%u\n",
375 page_offset
, offset
, eraseblock
);
377 wbuffer
= kmalloc(mtd
->writesize
, GFP_KERNEL
);
383 rbuffer
= kmalloc(mtd
->writesize
, GFP_KERNEL
);
389 err
= mtdtest_erase_eraseblock(mtd
, eraseblock
);
394 err
= incremental_errors_test();
396 err
= overwrite_test();
401 /* We leave the block un-erased in case of test failure. */
402 err
= mtdtest_erase_eraseblock(mtd
, eraseblock
);
407 pr_info("finished successfully.\n");
408 printk(KERN_INFO
"==================================================\n");
422 static void __exit
mtd_nandbiterrs_exit(void)
427 module_init(mtd_nandbiterrs_init
);
428 module_exit(mtd_nandbiterrs_exit
);
430 MODULE_DESCRIPTION("NAND bit error recovery test");
431 MODULE_AUTHOR("Iwo Mergler");
432 MODULE_LICENSE("GPL");