43 #include <linux/module.h>
48 #include <linux/slab.h>
50 #define msg(FMT, VA...) pr_info("mtd_nandbiterrs: "FMT, ##VA)
68 static unsigned max_overwrite = 10000;
71 static unsigned eraseblock;
75 static unsigned subsize;
76 static unsigned subcount;
94 c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
95 c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
96 c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
100 static int erase_block(
void)
106 msg(
"erase_block\n");
115 msg(
"error %d while erasing\n", err);
125 static int write_page(
int log)
135 msg(
"error: write failed at %#llx\n", (
long long)offset);
144 static int rewrite_page(
int log)
150 msg(
"rewrite page\n");
158 ops.datbuf = wbuffer;
161 err = mtd_write_oob(mtd, offset, &ops);
163 msg(
"error: write_oob failed (%d)\n", err);
173 static int read_page(
int log)
187 err = mtd->
ecc_stats.corrected - oldstats.corrected;
189 if (err < 0 || read != mtd->writesize) {
190 msg(
"error: read failed at %#llx\n", (
long long)offset);
199 static int verify_page(
int log)
201 unsigned i, errs = 0;
204 msg(
"verify_page\n");
207 if (rbuffer[i] !=
hash(i+seed)) {
208 msg(
"Error: page offset %u, expected %02x, got %02x\n",
209 i,
hash(i+seed), rbuffer[i]);
220 #define CBIT(v, n) ((v) & (1 << (n)))
221 #define BCLR(v, n) ((v) = (v) & ~(1 << (n)))
225 static int insert_biterror(
unsigned byte)
230 for (bit = 7; bit >= 0; bit--) {
231 if (
CBIT(wbuffer[byte], bit)) {
232 BCLR(wbuffer[byte], bit);
233 msg(
"Inserted biterror @ %u/%u\n", byte, bit);
239 msg(
"biterror: Failed to find a '1' bit\n");
245 static int incremental_errors_test(
void)
249 unsigned errs_per_subpage = 0;
251 msg(
"incremental biterrors test\n");
254 wbuffer[i] =
hash(i+seed);
262 err = rewrite_page(1);
268 msg(
"Read reported %d corrected bit errors\n", err);
270 msg(
"After %d biterrors per subpage, read reported error %d\n",
271 errs_per_subpage, err);
276 err = verify_page(1);
278 msg(
"ECC failure, read data is incorrect despite read success\n");
282 msg(
"Successfully corrected %d bit errors per subpage\n",
285 for (i = 0; i < subcount; i++) {
286 err = insert_biterror(i * subsize);
301 static int overwrite_test(
void)
305 unsigned max_corrected = 0;
310 static unsigned bitstats[
MAXBITS];
312 memset(bitstats, 0,
sizeof(bitstats));
314 msg(
"overwrite biterrors test\n");
317 wbuffer[i] =
hash(i+seed);
323 while (opno < max_overwrite) {
325 err = rewrite_page(0);
331 if (err >= MAXBITS) {
332 msg(
"Implausible number of bit errors corrected\n");
337 if (err > max_corrected) {
339 msg(
"Read reported %d corrected bit errors\n",
343 msg(
"Read reported error %d\n", err);
348 err = verify_page(0);
350 bitstats[max_corrected] = opno;
351 msg(
"ECC failure, read data is incorrect despite read success\n");
360 msg(
"Bit error histogram (%d operations total):\n", opno);
361 for (i = 0; i < max_corrected; i++)
362 msg(
"Page reads with %3d corrected bit errors: %d\n",
369 static int __init mtd_nandbiterrs_init(
void)
374 msg(
"==================================================\n");
375 msg(
"MTD device: %d\n",
dev);
380 msg(
"error: cannot get MTD device\n");
385 msg(
"this test requires NAND flash\n");
390 msg(
"MTD device size %llu, eraseblock=%u, page=%u, oob=%u\n",
397 msg(
"Device uses %d subpages of %d bytes\n", subcount, subsize);
400 eraseblock = mtd_div_by_eb(offset, mtd);
402 msg(
"Using page=%u, offset=%llu, eraseblock=%u\n",
422 err = incremental_errors_test();
424 err = overwrite_test();
435 msg(
"finished successfully.\n");
436 msg(
"==================================================\n");
450 static void __exit mtd_nandbiterrs_exit(
void)