Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2008 Nokia Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; see the file COPYING. If not, write to the Free Software |
| 15 | * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | * |
| 17 | * Test page read and write on MTD device. |
| 18 | * |
| 19 | * Author: Adrian Hunter <ext-adrian.hunter@nokia.com> |
| 20 | */ |
| 21 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 24 | #include <asm/div64.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/mtd/mtd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 31 | #include <linux/sched.h> |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 32 | #include <linux/random.h> |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 33 | |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 34 | static int dev = -EINVAL; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 35 | module_param(dev, int, S_IRUGO); |
| 36 | MODULE_PARM_DESC(dev, "MTD device number to use"); |
| 37 | |
| 38 | static struct mtd_info *mtd; |
| 39 | static unsigned char *twopages; |
| 40 | static unsigned char *writebuf; |
| 41 | static unsigned char *boundary; |
| 42 | static unsigned char *bbt; |
| 43 | |
| 44 | static int pgsize; |
| 45 | static int bufsize; |
| 46 | static int ebcnt; |
| 47 | static int pgcnt; |
| 48 | static int errcnt; |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 49 | static struct rnd_state rnd_state; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 50 | |
| 51 | static int erase_eraseblock(int ebnum) |
| 52 | { |
| 53 | int err; |
| 54 | struct erase_info ei; |
| 55 | loff_t addr = ebnum * mtd->erasesize; |
| 56 | |
| 57 | memset(&ei, 0, sizeof(struct erase_info)); |
| 58 | ei.mtd = mtd; |
| 59 | ei.addr = addr; |
| 60 | ei.len = mtd->erasesize; |
| 61 | |
Artem Bityutskiy | 7e1f0dc | 2011-12-23 15:25:39 +0200 | [diff] [blame] | 62 | err = mtd_erase(mtd, &ei); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 63 | if (err) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 64 | pr_err("error %d while erasing EB %d\n", err, ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 65 | return err; |
| 66 | } |
| 67 | |
| 68 | if (ei.state == MTD_ERASE_FAILED) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 69 | pr_err("some erase error occurred at EB %d\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 70 | ebnum); |
| 71 | return -EIO; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int write_eraseblock(int ebnum) |
| 78 | { |
| 79 | int err = 0; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 80 | size_t written; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 81 | loff_t addr = ebnum * mtd->erasesize; |
| 82 | |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 83 | prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 84 | cond_resched(); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 85 | err = mtd_write(mtd, addr, mtd->erasesize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 86 | if (err || written != mtd->erasesize) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 87 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 88 | (long long)addr); |
| 89 | |
| 90 | return err; |
| 91 | } |
| 92 | |
| 93 | static int verify_eraseblock(int ebnum) |
| 94 | { |
| 95 | uint32_t j; |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 96 | size_t read; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 97 | int err = 0, i; |
| 98 | loff_t addr0, addrn; |
| 99 | loff_t addr = ebnum * mtd->erasesize; |
| 100 | |
| 101 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 102 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 103 | addr0 += mtd->erasesize; |
| 104 | |
| 105 | addrn = mtd->size; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 106 | for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 107 | addrn -= mtd->erasesize; |
| 108 | |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 109 | prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 110 | for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) { |
| 111 | /* Do a read to set the internal dataRAMs to different data */ |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 112 | err = mtd_read(mtd, addr0, bufsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 113 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 114 | err = 0; |
| 115 | if (err || read != bufsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 116 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 117 | (long long)addr0); |
| 118 | return err; |
| 119 | } |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 120 | err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 121 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 122 | err = 0; |
| 123 | if (err || read != bufsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 124 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 125 | (long long)(addrn - bufsize)); |
| 126 | return err; |
| 127 | } |
| 128 | memset(twopages, 0, bufsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 129 | err = mtd_read(mtd, addr, bufsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 130 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 131 | err = 0; |
| 132 | if (err || read != bufsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 133 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 134 | (long long)addr); |
| 135 | break; |
| 136 | } |
| 137 | if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 138 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 139 | (long long)addr); |
| 140 | errcnt += 1; |
| 141 | } |
| 142 | } |
| 143 | /* Check boundary between eraseblocks */ |
| 144 | if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) { |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 145 | struct rnd_state old_state = rnd_state; |
| 146 | |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 147 | /* Do a read to set the internal dataRAMs to different data */ |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 148 | err = mtd_read(mtd, addr0, bufsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 149 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 150 | err = 0; |
| 151 | if (err || read != bufsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 152 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 153 | (long long)addr0); |
| 154 | return err; |
| 155 | } |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 156 | err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 157 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 158 | err = 0; |
| 159 | if (err || read != bufsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 160 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 161 | (long long)(addrn - bufsize)); |
| 162 | return err; |
| 163 | } |
| 164 | memset(twopages, 0, bufsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 165 | err = mtd_read(mtd, addr, bufsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 166 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 167 | err = 0; |
| 168 | if (err || read != bufsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 169 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 170 | (long long)addr); |
| 171 | return err; |
| 172 | } |
| 173 | memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 174 | prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 175 | if (memcmp(twopages, boundary, bufsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 176 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 177 | (long long)addr); |
| 178 | errcnt += 1; |
| 179 | } |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 180 | rnd_state = old_state; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 181 | } |
| 182 | return err; |
| 183 | } |
| 184 | |
| 185 | static int crosstest(void) |
| 186 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 187 | size_t read; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 188 | int err = 0, i; |
| 189 | loff_t addr, addr0, addrn; |
| 190 | unsigned char *pp1, *pp2, *pp3, *pp4; |
| 191 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 192 | pr_info("crosstest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 193 | pp1 = kmalloc(pgsize * 4, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 194 | if (!pp1) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 195 | return -ENOMEM; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 196 | pp2 = pp1 + pgsize; |
| 197 | pp3 = pp2 + pgsize; |
| 198 | pp4 = pp3 + pgsize; |
| 199 | memset(pp1, 0, pgsize * 4); |
| 200 | |
| 201 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 202 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 203 | addr0 += mtd->erasesize; |
| 204 | |
| 205 | addrn = mtd->size; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 206 | for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 207 | addrn -= mtd->erasesize; |
| 208 | |
| 209 | /* Read 2nd-to-last page to pp1 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 210 | addr = addrn - pgsize - pgsize; |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 211 | err = mtd_read(mtd, addr, pgsize, &read, pp1); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 212 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 213 | err = 0; |
| 214 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 215 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 216 | (long long)addr); |
| 217 | kfree(pp1); |
| 218 | return err; |
| 219 | } |
| 220 | |
| 221 | /* Read 3rd-to-last page to pp1 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 222 | addr = addrn - pgsize - pgsize - pgsize; |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 223 | err = mtd_read(mtd, addr, pgsize, &read, pp1); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 224 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 225 | err = 0; |
| 226 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 227 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 228 | (long long)addr); |
| 229 | kfree(pp1); |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | /* Read first page to pp2 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 234 | addr = addr0; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 235 | pr_info("reading page at %#llx\n", (long long)addr); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 236 | err = mtd_read(mtd, addr, pgsize, &read, pp2); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 237 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 238 | err = 0; |
| 239 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 240 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 241 | (long long)addr); |
| 242 | kfree(pp1); |
| 243 | return err; |
| 244 | } |
| 245 | |
| 246 | /* Read last page to pp3 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 247 | addr = addrn - pgsize; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 248 | pr_info("reading page at %#llx\n", (long long)addr); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 249 | err = mtd_read(mtd, addr, pgsize, &read, pp3); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 250 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 251 | err = 0; |
| 252 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 253 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 254 | (long long)addr); |
| 255 | kfree(pp1); |
| 256 | return err; |
| 257 | } |
| 258 | |
| 259 | /* Read first page again to pp4 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 260 | addr = addr0; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 261 | pr_info("reading page at %#llx\n", (long long)addr); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 262 | err = mtd_read(mtd, addr, pgsize, &read, pp4); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 263 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 264 | err = 0; |
| 265 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 266 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 267 | (long long)addr); |
| 268 | kfree(pp1); |
| 269 | return err; |
| 270 | } |
| 271 | |
| 272 | /* pp2 and pp4 should be the same */ |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 273 | pr_info("verifying pages read at %#llx match\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 274 | (long long)addr0); |
| 275 | if (memcmp(pp2, pp4, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 276 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 277 | errcnt += 1; |
| 278 | } else if (!err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 279 | pr_info("crosstest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 280 | kfree(pp1); |
| 281 | return err; |
| 282 | } |
| 283 | |
| 284 | static int erasecrosstest(void) |
| 285 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 286 | size_t read, written; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 287 | int err = 0, i, ebnum, ebnum2; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 288 | loff_t addr0; |
| 289 | char *readbuf = twopages; |
| 290 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 291 | pr_info("erasecrosstest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 292 | |
| 293 | ebnum = 0; |
| 294 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 295 | for (i = 0; i < ebcnt && bbt[i]; ++i) { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 296 | addr0 += mtd->erasesize; |
| 297 | ebnum += 1; |
| 298 | } |
| 299 | |
| 300 | ebnum2 = ebcnt - 1; |
| 301 | while (ebnum2 && bbt[ebnum2]) |
| 302 | ebnum2 -= 1; |
| 303 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 304 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 305 | err = erase_eraseblock(ebnum); |
| 306 | if (err) |
| 307 | return err; |
| 308 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 309 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 310 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 311 | strcpy(writebuf, "There is no data like this!"); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 312 | err = mtd_write(mtd, addr0, pgsize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 313 | if (err || written != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 314 | pr_info("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 315 | (long long)addr0); |
| 316 | return err ? err : -1; |
| 317 | } |
| 318 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 319 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 320 | memset(readbuf, 0, pgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 321 | err = mtd_read(mtd, addr0, pgsize, &read, readbuf); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 322 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 323 | err = 0; |
| 324 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 325 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 326 | (long long)addr0); |
| 327 | return err ? err : -1; |
| 328 | } |
| 329 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 330 | pr_info("verifying 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 331 | if (memcmp(writebuf, readbuf, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 332 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 333 | errcnt += 1; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 334 | return -1; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 337 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 338 | err = erase_eraseblock(ebnum); |
| 339 | if (err) |
| 340 | return err; |
| 341 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 342 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 343 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 344 | strcpy(writebuf, "There is no data like this!"); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 345 | err = mtd_write(mtd, addr0, pgsize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 346 | if (err || written != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 347 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 348 | (long long)addr0); |
| 349 | return err ? err : -1; |
| 350 | } |
| 351 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 352 | pr_info("erasing block %d\n", ebnum2); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 353 | err = erase_eraseblock(ebnum2); |
| 354 | if (err) |
| 355 | return err; |
| 356 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 357 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 358 | memset(readbuf, 0, pgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 359 | err = mtd_read(mtd, addr0, pgsize, &read, readbuf); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 360 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 361 | err = 0; |
| 362 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 363 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 364 | (long long)addr0); |
| 365 | return err ? err : -1; |
| 366 | } |
| 367 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 368 | pr_info("verifying 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 369 | if (memcmp(writebuf, readbuf, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 370 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 371 | errcnt += 1; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 372 | return -1; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 375 | if (!err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 376 | pr_info("erasecrosstest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 377 | return err; |
| 378 | } |
| 379 | |
| 380 | static int erasetest(void) |
| 381 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 382 | size_t read, written; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 383 | int err = 0, i, ebnum, ok = 1; |
| 384 | loff_t addr0; |
| 385 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 386 | pr_info("erasetest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 387 | |
| 388 | ebnum = 0; |
| 389 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 390 | for (i = 0; i < ebcnt && bbt[i]; ++i) { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 391 | addr0 += mtd->erasesize; |
| 392 | ebnum += 1; |
| 393 | } |
| 394 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 395 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 396 | err = erase_eraseblock(ebnum); |
| 397 | if (err) |
| 398 | return err; |
| 399 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 400 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 401 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 402 | err = mtd_write(mtd, addr0, pgsize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 403 | if (err || written != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 404 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 405 | (long long)addr0); |
| 406 | return err ? err : -1; |
| 407 | } |
| 408 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 409 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 410 | err = erase_eraseblock(ebnum); |
| 411 | if (err) |
| 412 | return err; |
| 413 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 414 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 415 | err = mtd_read(mtd, addr0, pgsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 416 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 417 | err = 0; |
| 418 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 419 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 420 | (long long)addr0); |
| 421 | return err ? err : -1; |
| 422 | } |
| 423 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 424 | pr_info("verifying 1st page of block %d is all 0xff\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 425 | ebnum); |
| 426 | for (i = 0; i < pgsize; ++i) |
| 427 | if (twopages[i] != 0xff) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 428 | pr_err("verifying all 0xff failed at %d\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 429 | i); |
| 430 | errcnt += 1; |
| 431 | ok = 0; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | if (ok && !err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 436 | pr_info("erasetest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 437 | |
| 438 | return err; |
| 439 | } |
| 440 | |
| 441 | static int is_block_bad(int ebnum) |
| 442 | { |
| 443 | loff_t addr = ebnum * mtd->erasesize; |
| 444 | int ret; |
| 445 | |
Artem Bityutskiy | 7086c19 | 2011-12-23 19:35:30 +0200 | [diff] [blame] | 446 | ret = mtd_block_isbad(mtd, addr); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 447 | if (ret) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 448 | pr_info("block %d is bad\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | static int scan_for_bad_eraseblocks(void) |
| 453 | { |
| 454 | int i, bad = 0; |
| 455 | |
Julia Lawall | 2bfefa4c | 2010-05-13 22:03:15 +0200 | [diff] [blame] | 456 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 457 | if (!bbt) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 458 | return -ENOMEM; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 459 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 460 | pr_info("scanning for bad eraseblocks\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 461 | for (i = 0; i < ebcnt; ++i) { |
| 462 | bbt[i] = is_block_bad(i) ? 1 : 0; |
| 463 | if (bbt[i]) |
| 464 | bad += 1; |
| 465 | cond_resched(); |
| 466 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 467 | pr_info("scanned %d eraseblocks, %d are bad\n", i, bad); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int __init mtd_pagetest_init(void) |
| 472 | { |
| 473 | int err = 0; |
| 474 | uint64_t tmp; |
| 475 | uint32_t i; |
| 476 | |
| 477 | printk(KERN_INFO "\n"); |
| 478 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 479 | |
| 480 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 481 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 482 | pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 483 | return -EINVAL; |
| 484 | } |
| 485 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 486 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 487 | |
| 488 | mtd = get_mtd_device(NULL, dev); |
| 489 | if (IS_ERR(mtd)) { |
| 490 | err = PTR_ERR(mtd); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 491 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 492 | return err; |
| 493 | } |
| 494 | |
| 495 | if (mtd->type != MTD_NANDFLASH) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 496 | pr_info("this test requires NAND flash\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 497 | goto out; |
| 498 | } |
| 499 | |
| 500 | tmp = mtd->size; |
| 501 | do_div(tmp, mtd->erasesize); |
| 502 | ebcnt = tmp; |
| 503 | pgcnt = mtd->erasesize / mtd->writesize; |
Artem Bityutskiy | 4c2b8a6 | 2009-11-27 16:58:08 +0200 | [diff] [blame] | 504 | pgsize = mtd->writesize; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 505 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 506 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 507 | "page size %u, count of eraseblocks %u, pages per " |
| 508 | "eraseblock %u, OOB size %u\n", |
| 509 | (unsigned long long)mtd->size, mtd->erasesize, |
| 510 | pgsize, ebcnt, pgcnt, mtd->oobsize); |
| 511 | |
| 512 | err = -ENOMEM; |
| 513 | bufsize = pgsize * 2; |
| 514 | writebuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 515 | if (!writebuf) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 516 | goto out; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 517 | twopages = kmalloc(bufsize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 518 | if (!twopages) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 519 | goto out; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 520 | boundary = kmalloc(bufsize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 521 | if (!boundary) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 522 | goto out; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 523 | |
| 524 | err = scan_for_bad_eraseblocks(); |
| 525 | if (err) |
| 526 | goto out; |
| 527 | |
| 528 | /* Erase all eraseblocks */ |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 529 | pr_info("erasing whole device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 530 | for (i = 0; i < ebcnt; ++i) { |
| 531 | if (bbt[i]) |
| 532 | continue; |
| 533 | err = erase_eraseblock(i); |
| 534 | if (err) |
| 535 | goto out; |
| 536 | cond_resched(); |
| 537 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 538 | pr_info("erased %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 539 | |
| 540 | /* Write all eraseblocks */ |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 541 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 542 | pr_info("writing whole device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 543 | for (i = 0; i < ebcnt; ++i) { |
| 544 | if (bbt[i]) |
| 545 | continue; |
| 546 | err = write_eraseblock(i); |
| 547 | if (err) |
| 548 | goto out; |
| 549 | if (i % 256 == 0) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 550 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 551 | cond_resched(); |
| 552 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 553 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 554 | |
| 555 | /* Check all eraseblocks */ |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 556 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 557 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 558 | for (i = 0; i < ebcnt; ++i) { |
| 559 | if (bbt[i]) |
| 560 | continue; |
| 561 | err = verify_eraseblock(i); |
| 562 | if (err) |
| 563 | goto out; |
| 564 | if (i % 256 == 0) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 565 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 566 | cond_resched(); |
| 567 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 568 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 569 | |
| 570 | err = crosstest(); |
| 571 | if (err) |
| 572 | goto out; |
| 573 | |
| 574 | err = erasecrosstest(); |
| 575 | if (err) |
| 576 | goto out; |
| 577 | |
| 578 | err = erasetest(); |
| 579 | if (err) |
| 580 | goto out; |
| 581 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 582 | pr_info("finished with %d errors\n", errcnt); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 583 | out: |
| 584 | |
| 585 | kfree(bbt); |
| 586 | kfree(boundary); |
| 587 | kfree(twopages); |
| 588 | kfree(writebuf); |
| 589 | put_mtd_device(mtd); |
| 590 | if (err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 591 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 592 | printk(KERN_INFO "=================================================\n"); |
| 593 | return err; |
| 594 | } |
| 595 | module_init(mtd_pagetest_init); |
| 596 | |
| 597 | static void __exit mtd_pagetest_exit(void) |
| 598 | { |
| 599 | return; |
| 600 | } |
| 601 | module_exit(mtd_pagetest_exit); |
| 602 | |
| 603 | MODULE_DESCRIPTION("NAND page test"); |
| 604 | MODULE_AUTHOR("Adrian Hunter"); |
| 605 | MODULE_LICENSE("GPL"); |