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