PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / mtd / tests / mtd_test.c
blobc818a63532e774035e7f81d748622c0b21810c2a
1 #define pr_fmt(fmt) "mtd_test: " fmt
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/printk.h>
8 #include "mtd_test.h"
10 int mtdtest_erase_eraseblock(struct mtd_info *mtd, unsigned int ebnum)
12 int err;
13 struct erase_info ei;
14 loff_t addr = ebnum * mtd->erasesize;
16 memset(&ei, 0, sizeof(struct erase_info));
17 ei.mtd = mtd;
18 ei.addr = addr;
19 ei.len = mtd->erasesize;
21 err = mtd_erase(mtd, &ei);
22 if (err) {
23 pr_info("error %d while erasing EB %d\n", err, ebnum);
24 return err;
27 if (ei.state == MTD_ERASE_FAILED) {
28 pr_info("some erase error occurred at EB %d\n", ebnum);
29 return -EIO;
31 return 0;
34 static int is_block_bad(struct mtd_info *mtd, unsigned int ebnum)
36 int ret;
37 loff_t addr = ebnum * mtd->erasesize;
39 ret = mtd_block_isbad(mtd, addr);
40 if (ret)
41 pr_info("block %d is bad\n", ebnum);
43 return ret;
46 int mtdtest_scan_for_bad_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
47 unsigned int eb, int ebcnt)
49 int i, bad = 0;
51 if (!mtd_can_have_bb(mtd))
52 return 0;
54 pr_info("scanning for bad eraseblocks\n");
55 for (i = 0; i < ebcnt; ++i) {
56 bbt[i] = is_block_bad(mtd, eb + i) ? 1 : 0;
57 if (bbt[i])
58 bad += 1;
59 cond_resched();
61 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
63 return 0;
66 int mtdtest_erase_good_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
67 unsigned int eb, int ebcnt)
69 int err;
70 unsigned int i;
72 for (i = 0; i < ebcnt; ++i) {
73 if (bbt[i])
74 continue;
75 err = mtdtest_erase_eraseblock(mtd, eb + i);
76 if (err)
77 return err;
78 cond_resched();
81 return 0;
84 int mtdtest_read(struct mtd_info *mtd, loff_t addr, size_t size, void *buf)
86 size_t read;
87 int err;
89 err = mtd_read(mtd, addr, size, &read, buf);
90 /* Ignore corrected ECC errors */
91 if (mtd_is_bitflip(err))
92 err = 0;
93 if (!err && read != size)
94 err = -EIO;
95 if (err)
96 pr_err("error: read failed at %#llx\n", addr);
98 return err;
101 int mtdtest_write(struct mtd_info *mtd, loff_t addr, size_t size,
102 const void *buf)
104 size_t written;
105 int err;
107 err = mtd_write(mtd, addr, size, &written, buf);
108 if (!err && written != size)
109 err = -EIO;
110 if (err)
111 pr_err("error: write failed at %#llx\n", addr);
113 return err;