Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/list.h> |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 4 | #include <linux/random.h> |
| 5 | #include <linux/string.h> |
| 6 | #include <linux/bitops.h> |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 7 | #include <linux/mtd/nand_ecc.h> |
| 8 | |
| 9 | #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE) |
| 10 | |
| 11 | static void inject_single_bit_error(void *data, size_t size) |
| 12 | { |
| 13 | unsigned long offset = random32() % (size * BITS_PER_BYTE); |
| 14 | |
| 15 | __change_bit(offset, data); |
| 16 | } |
| 17 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 18 | static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data, |
| 19 | void *correct_ecc, const size_t size) |
| 20 | { |
| 21 | pr_info("hexdump of error data:\n"); |
| 22 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, |
| 23 | error_data, size, false); |
| 24 | print_hex_dump(KERN_INFO, "hexdump of error ecc: ", |
| 25 | DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false); |
| 26 | |
| 27 | pr_info("hexdump of correct data:\n"); |
| 28 | print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4, |
| 29 | correct_data, size, false); |
| 30 | print_hex_dump(KERN_INFO, "hexdump of correct ecc: ", |
| 31 | DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false); |
| 32 | } |
| 33 | |
| 34 | static unsigned char correct_data[512]; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 35 | static unsigned char error_data[512]; |
| 36 | |
| 37 | static int nand_ecc_test(const size_t size) |
| 38 | { |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 39 | unsigned char correct_ecc[3]; |
| 40 | unsigned char error_ecc[3]; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 41 | char testname[30]; |
| 42 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 43 | BUG_ON(sizeof(correct_data) < size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 44 | |
| 45 | sprintf(testname, "nand-ecc-%zu", size); |
| 46 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 47 | get_random_bytes(correct_data, size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 48 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 49 | memcpy(error_data, correct_data, size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 50 | inject_single_bit_error(error_data, size); |
| 51 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 52 | __nand_calculate_ecc(correct_data, size, correct_ecc); |
| 53 | __nand_calculate_ecc(error_data, size, error_ecc); |
| 54 | __nand_correct_data(error_data, correct_ecc, error_ecc, size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 55 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 56 | if (!memcmp(correct_data, error_data, size)) { |
| 57 | pr_info("mtd_nandecctest: ok - %s\n", testname); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Akinobu Mita | c5b8384 | 2012-09-03 22:00:00 +0900 | [diff] [blame^] | 61 | pr_err("mtd_nandecctest: not ok - %s\n", testname); |
| 62 | dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc, size); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 63 | |
Akinobu Mita | bb82477 | 2012-09-03 21:59:59 +0900 | [diff] [blame] | 64 | return -EINVAL; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | #else |
| 68 | |
| 69 | static int nand_ecc_test(const size_t size) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | #endif |
| 75 | |
| 76 | static int __init ecc_test_init(void) |
| 77 | { |
Akinobu Mita | f45c299 | 2012-08-26 21:06:44 +0900 | [diff] [blame] | 78 | int err; |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 79 | |
Akinobu Mita | f45c299 | 2012-08-26 21:06:44 +0900 | [diff] [blame] | 80 | err = nand_ecc_test(256); |
| 81 | if (err) |
| 82 | return err; |
| 83 | |
| 84 | return nand_ecc_test(512); |
Akinobu Mita | 7126bd8 | 2009-10-22 16:53:33 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void __exit ecc_test_exit(void) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | module_init(ecc_test_init); |
| 92 | module_exit(ecc_test_exit); |
| 93 | |
| 94 | MODULE_DESCRIPTION("NAND ECC function test module"); |
| 95 | MODULE_AUTHOR("Akinobu Mita"); |
| 96 | MODULE_LICENSE("GPL"); |