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); |
| 194 | if (!pp1) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 195 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 196 | return -ENOMEM; |
| 197 | } |
| 198 | pp2 = pp1 + pgsize; |
| 199 | pp3 = pp2 + pgsize; |
| 200 | pp4 = pp3 + pgsize; |
| 201 | memset(pp1, 0, pgsize * 4); |
| 202 | |
| 203 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 204 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 205 | addr0 += mtd->erasesize; |
| 206 | |
| 207 | addrn = mtd->size; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 208 | for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 209 | addrn -= mtd->erasesize; |
| 210 | |
| 211 | /* Read 2nd-to-last page to pp1 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 212 | addr = addrn - pgsize - pgsize; |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 213 | err = mtd_read(mtd, addr, pgsize, &read, pp1); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 214 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 215 | err = 0; |
| 216 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 217 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 218 | (long long)addr); |
| 219 | kfree(pp1); |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | /* Read 3rd-to-last page to pp1 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 224 | addr = addrn - pgsize - pgsize - pgsize; |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 225 | err = mtd_read(mtd, addr, pgsize, &read, pp1); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 226 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 227 | err = 0; |
| 228 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 229 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 230 | (long long)addr); |
| 231 | kfree(pp1); |
| 232 | return err; |
| 233 | } |
| 234 | |
| 235 | /* Read first page to pp2 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 236 | addr = addr0; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 237 | pr_info("reading page at %#llx\n", (long long)addr); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 238 | err = mtd_read(mtd, addr, pgsize, &read, pp2); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 239 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 240 | err = 0; |
| 241 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 242 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 243 | (long long)addr); |
| 244 | kfree(pp1); |
| 245 | return err; |
| 246 | } |
| 247 | |
| 248 | /* Read last page to pp3 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 249 | addr = addrn - pgsize; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 250 | pr_info("reading page at %#llx\n", (long long)addr); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 251 | err = mtd_read(mtd, addr, pgsize, &read, pp3); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 252 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 253 | err = 0; |
| 254 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 255 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 256 | (long long)addr); |
| 257 | kfree(pp1); |
| 258 | return err; |
| 259 | } |
| 260 | |
| 261 | /* Read first page again to pp4 */ |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 262 | addr = addr0; |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 263 | pr_info("reading page at %#llx\n", (long long)addr); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 264 | err = mtd_read(mtd, addr, pgsize, &read, pp4); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 265 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 266 | err = 0; |
| 267 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 268 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 269 | (long long)addr); |
| 270 | kfree(pp1); |
| 271 | return err; |
| 272 | } |
| 273 | |
| 274 | /* pp2 and pp4 should be the same */ |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 275 | pr_info("verifying pages read at %#llx match\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 276 | (long long)addr0); |
| 277 | if (memcmp(pp2, pp4, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 278 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 279 | errcnt += 1; |
| 280 | } else if (!err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 281 | pr_info("crosstest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 282 | kfree(pp1); |
| 283 | return err; |
| 284 | } |
| 285 | |
| 286 | static int erasecrosstest(void) |
| 287 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 288 | size_t read, written; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 289 | int err = 0, i, ebnum, ebnum2; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 290 | loff_t addr0; |
| 291 | char *readbuf = twopages; |
| 292 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 293 | pr_info("erasecrosstest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 294 | |
| 295 | ebnum = 0; |
| 296 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 297 | for (i = 0; i < ebcnt && bbt[i]; ++i) { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 298 | addr0 += mtd->erasesize; |
| 299 | ebnum += 1; |
| 300 | } |
| 301 | |
| 302 | ebnum2 = ebcnt - 1; |
| 303 | while (ebnum2 && bbt[ebnum2]) |
| 304 | ebnum2 -= 1; |
| 305 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 306 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 307 | err = erase_eraseblock(ebnum); |
| 308 | if (err) |
| 309 | return err; |
| 310 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 311 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 312 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 313 | strcpy(writebuf, "There is no data like this!"); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 314 | err = mtd_write(mtd, addr0, pgsize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 315 | if (err || written != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 316 | pr_info("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 317 | (long long)addr0); |
| 318 | return err ? err : -1; |
| 319 | } |
| 320 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 321 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 322 | memset(readbuf, 0, pgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 323 | err = mtd_read(mtd, addr0, pgsize, &read, readbuf); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 324 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 325 | err = 0; |
| 326 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 327 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 328 | (long long)addr0); |
| 329 | return err ? err : -1; |
| 330 | } |
| 331 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 332 | pr_info("verifying 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 333 | if (memcmp(writebuf, readbuf, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 334 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 335 | errcnt += 1; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 336 | return -1; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 339 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 340 | err = erase_eraseblock(ebnum); |
| 341 | if (err) |
| 342 | return err; |
| 343 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 344 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 345 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 346 | strcpy(writebuf, "There is no data like this!"); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 347 | err = mtd_write(mtd, addr0, pgsize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 348 | if (err || written != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 349 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 350 | (long long)addr0); |
| 351 | return err ? err : -1; |
| 352 | } |
| 353 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 354 | pr_info("erasing block %d\n", ebnum2); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 355 | err = erase_eraseblock(ebnum2); |
| 356 | if (err) |
| 357 | return err; |
| 358 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 359 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 360 | memset(readbuf, 0, pgsize); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 361 | err = mtd_read(mtd, addr0, pgsize, &read, readbuf); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 362 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 363 | err = 0; |
| 364 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 365 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 366 | (long long)addr0); |
| 367 | return err ? err : -1; |
| 368 | } |
| 369 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 370 | pr_info("verifying 1st page of block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 371 | if (memcmp(writebuf, readbuf, pgsize)) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 372 | pr_err("verify failed!\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 373 | errcnt += 1; |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 374 | return -1; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Roel Kluin | 7fc14bc | 2010-07-13 13:24:31 +0300 | [diff] [blame] | 377 | if (!err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 378 | pr_info("erasecrosstest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 379 | return err; |
| 380 | } |
| 381 | |
| 382 | static int erasetest(void) |
| 383 | { |
Artem Bityutskiy | 30fa984 | 2011-12-29 15:16:28 +0200 | [diff] [blame] | 384 | size_t read, written; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 385 | int err = 0, i, ebnum, ok = 1; |
| 386 | loff_t addr0; |
| 387 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 388 | pr_info("erasetest\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 389 | |
| 390 | ebnum = 0; |
| 391 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 392 | for (i = 0; i < ebcnt && bbt[i]; ++i) { |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 393 | addr0 += mtd->erasesize; |
| 394 | ebnum += 1; |
| 395 | } |
| 396 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 397 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 398 | err = erase_eraseblock(ebnum); |
| 399 | if (err) |
| 400 | return err; |
| 401 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 402 | pr_info("writing 1st page of block %d\n", ebnum); |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 403 | prandom_bytes_state(&rnd_state, writebuf, pgsize); |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 404 | err = mtd_write(mtd, addr0, pgsize, &written, writebuf); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 405 | if (err || written != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 406 | pr_err("error: write failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 407 | (long long)addr0); |
| 408 | return err ? err : -1; |
| 409 | } |
| 410 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 411 | pr_info("erasing block %d\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 412 | err = erase_eraseblock(ebnum); |
| 413 | if (err) |
| 414 | return err; |
| 415 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 416 | pr_info("reading 1st page of block %d\n", ebnum); |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 417 | err = mtd_read(mtd, addr0, pgsize, &read, twopages); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 418 | if (mtd_is_bitflip(err)) |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 419 | err = 0; |
| 420 | if (err || read != pgsize) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 421 | pr_err("error: read failed at %#llx\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 422 | (long long)addr0); |
| 423 | return err ? err : -1; |
| 424 | } |
| 425 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 426 | pr_info("verifying 1st page of block %d is all 0xff\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 427 | ebnum); |
| 428 | for (i = 0; i < pgsize; ++i) |
| 429 | if (twopages[i] != 0xff) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 430 | pr_err("verifying all 0xff failed at %d\n", |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 431 | i); |
| 432 | errcnt += 1; |
| 433 | ok = 0; |
| 434 | break; |
| 435 | } |
| 436 | |
| 437 | if (ok && !err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 438 | pr_info("erasetest ok\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 439 | |
| 440 | return err; |
| 441 | } |
| 442 | |
| 443 | static int is_block_bad(int ebnum) |
| 444 | { |
| 445 | loff_t addr = ebnum * mtd->erasesize; |
| 446 | int ret; |
| 447 | |
Artem Bityutskiy | 7086c19 | 2011-12-23 19:35:30 +0200 | [diff] [blame] | 448 | ret = mtd_block_isbad(mtd, addr); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 449 | if (ret) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 450 | pr_info("block %d is bad\n", ebnum); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | static int scan_for_bad_eraseblocks(void) |
| 455 | { |
| 456 | int i, bad = 0; |
| 457 | |
Julia Lawall | 2bfefa4 | 2010-05-13 22:03:15 +0200 | [diff] [blame] | 458 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 459 | if (!bbt) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 460 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 461 | return -ENOMEM; |
| 462 | } |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 463 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 464 | pr_info("scanning for bad eraseblocks\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 465 | for (i = 0; i < ebcnt; ++i) { |
| 466 | bbt[i] = is_block_bad(i) ? 1 : 0; |
| 467 | if (bbt[i]) |
| 468 | bad += 1; |
| 469 | cond_resched(); |
| 470 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 471 | pr_info("scanned %d eraseblocks, %d are bad\n", i, bad); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int __init mtd_pagetest_init(void) |
| 476 | { |
| 477 | int err = 0; |
| 478 | uint64_t tmp; |
| 479 | uint32_t i; |
| 480 | |
| 481 | printk(KERN_INFO "\n"); |
| 482 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 483 | |
| 484 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 485 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 486 | 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] | 487 | return -EINVAL; |
| 488 | } |
| 489 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 490 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 491 | |
| 492 | mtd = get_mtd_device(NULL, dev); |
| 493 | if (IS_ERR(mtd)) { |
| 494 | err = PTR_ERR(mtd); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 495 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 496 | return err; |
| 497 | } |
| 498 | |
| 499 | if (mtd->type != MTD_NANDFLASH) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 500 | pr_info("this test requires NAND flash\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 501 | goto out; |
| 502 | } |
| 503 | |
| 504 | tmp = mtd->size; |
| 505 | do_div(tmp, mtd->erasesize); |
| 506 | ebcnt = tmp; |
| 507 | pgcnt = mtd->erasesize / mtd->writesize; |
Artem Bityutskiy | 4c2b8a6 | 2009-11-27 16:58:08 +0200 | [diff] [blame] | 508 | pgsize = mtd->writesize; |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 509 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 510 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 511 | "page size %u, count of eraseblocks %u, pages per " |
| 512 | "eraseblock %u, OOB size %u\n", |
| 513 | (unsigned long long)mtd->size, mtd->erasesize, |
| 514 | pgsize, ebcnt, pgcnt, mtd->oobsize); |
| 515 | |
| 516 | err = -ENOMEM; |
| 517 | bufsize = pgsize * 2; |
| 518 | writebuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
| 519 | if (!writebuf) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 520 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 521 | goto out; |
| 522 | } |
| 523 | twopages = kmalloc(bufsize, GFP_KERNEL); |
| 524 | if (!twopages) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 525 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 526 | goto out; |
| 527 | } |
| 528 | boundary = kmalloc(bufsize, GFP_KERNEL); |
| 529 | if (!boundary) { |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 530 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 531 | goto out; |
| 532 | } |
| 533 | |
| 534 | err = scan_for_bad_eraseblocks(); |
| 535 | if (err) |
| 536 | goto out; |
| 537 | |
| 538 | /* Erase all eraseblocks */ |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 539 | pr_info("erasing whole device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 540 | for (i = 0; i < ebcnt; ++i) { |
| 541 | if (bbt[i]) |
| 542 | continue; |
| 543 | err = erase_eraseblock(i); |
| 544 | if (err) |
| 545 | goto out; |
| 546 | cond_resched(); |
| 547 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 548 | pr_info("erased %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 549 | |
| 550 | /* Write all eraseblocks */ |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 551 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 552 | pr_info("writing whole device\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 553 | for (i = 0; i < ebcnt; ++i) { |
| 554 | if (bbt[i]) |
| 555 | continue; |
| 556 | err = write_eraseblock(i); |
| 557 | if (err) |
| 558 | goto out; |
| 559 | if (i % 256 == 0) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 560 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 561 | cond_resched(); |
| 562 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 563 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 564 | |
| 565 | /* Check all eraseblocks */ |
Akinobu Mita | 825b8cc | 2013-02-27 17:05:35 -0800 | [diff] [blame] | 566 | prandom_seed_state(&rnd_state, 1); |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 567 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 568 | for (i = 0; i < ebcnt; ++i) { |
| 569 | if (bbt[i]) |
| 570 | continue; |
| 571 | err = verify_eraseblock(i); |
| 572 | if (err) |
| 573 | goto out; |
| 574 | if (i % 256 == 0) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 575 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 576 | cond_resched(); |
| 577 | } |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 578 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 579 | |
| 580 | err = crosstest(); |
| 581 | if (err) |
| 582 | goto out; |
| 583 | |
| 584 | err = erasecrosstest(); |
| 585 | if (err) |
| 586 | goto out; |
| 587 | |
| 588 | err = erasetest(); |
| 589 | if (err) |
| 590 | goto out; |
| 591 | |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 592 | pr_info("finished with %d errors\n", errcnt); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 593 | out: |
| 594 | |
| 595 | kfree(bbt); |
| 596 | kfree(boundary); |
| 597 | kfree(twopages); |
| 598 | kfree(writebuf); |
| 599 | put_mtd_device(mtd); |
| 600 | if (err) |
Vikram Narayanan | bb99841 | 2012-10-10 23:07:40 +0530 | [diff] [blame] | 601 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | e73f217 | 2008-12-08 13:34:16 +0200 | [diff] [blame] | 602 | printk(KERN_INFO "=================================================\n"); |
| 603 | return err; |
| 604 | } |
| 605 | module_init(mtd_pagetest_init); |
| 606 | |
| 607 | static void __exit mtd_pagetest_exit(void) |
| 608 | { |
| 609 | return; |
| 610 | } |
| 611 | module_exit(mtd_pagetest_exit); |
| 612 | |
| 613 | MODULE_DESCRIPTION("NAND page test"); |
| 614 | MODULE_AUTHOR("Adrian Hunter"); |
| 615 | MODULE_LICENSE("GPL"); |