Linux 2.6.33-rc6
[cris-mirror.git] / drivers / mtd / tests / mtd_nandecctest.c
blobc1f31051784c895a244aeef144f20ff5f0cddd70
1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/list.h>
4 #include <linux/slab.h>
5 #include <linux/random.h>
6 #include <linux/string.h>
7 #include <linux/bitops.h>
8 #include <linux/jiffies.h>
9 #include <linux/mtd/nand_ecc.h>
11 #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)
13 static void inject_single_bit_error(void *data, size_t size)
15 unsigned long offset = random32() % (size * BITS_PER_BYTE);
17 __change_bit(offset, data);
20 static unsigned char data[512];
21 static unsigned char error_data[512];
23 static int nand_ecc_test(const size_t size)
25 unsigned char code[3];
26 unsigned char error_code[3];
27 char testname[30];
29 BUG_ON(sizeof(data) < size);
31 sprintf(testname, "nand-ecc-%zu", size);
33 get_random_bytes(data, size);
35 memcpy(error_data, data, size);
36 inject_single_bit_error(error_data, size);
38 __nand_calculate_ecc(data, size, code);
39 __nand_calculate_ecc(error_data, size, error_code);
40 __nand_correct_data(error_data, code, error_code, size);
42 if (!memcmp(data, error_data, size)) {
43 printk(KERN_INFO "mtd_nandecctest: ok - %s\n", testname);
44 return 0;
47 printk(KERN_ERR "mtd_nandecctest: not ok - %s\n", testname);
49 printk(KERN_DEBUG "hexdump of data:\n");
50 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
51 data, size, false);
52 printk(KERN_DEBUG "hexdump of error data:\n");
53 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
54 error_data, size, false);
56 return -1;
59 #else
61 static int nand_ecc_test(const size_t size)
63 return 0;
66 #endif
68 static int __init ecc_test_init(void)
70 srandom32(jiffies);
72 nand_ecc_test(256);
73 nand_ecc_test(512);
75 return 0;
78 static void __exit ecc_test_exit(void)
82 module_init(ecc_test_init);
83 module_exit(ecc_test_exit);
85 MODULE_DESCRIPTION("NAND ECC function test module");
86 MODULE_AUTHOR("Akinobu Mita");
87 MODULE_LICENSE("GPL");