blob: f93a76f881131bc65bbfe4834f77cbcfc5c6c816 [file] [log] [blame]
Artem Bityutskiye73f2172008-12-08 13:34:16 +02001/*
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 Narayananbb998412012-10-10 23:07:40 +053022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Artem Bityutskiye73f2172008-12-08 13:34:16 +020024#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 Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Artem Bityutskiye73f2172008-12-08 13:34:16 +020031#include <linux/sched.h>
32
Wolfram Sang74060602011-10-30 00:11:53 +020033static int dev = -EINVAL;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020034module_param(dev, int, S_IRUGO);
35MODULE_PARM_DESC(dev, "MTD device number to use");
36
37static struct mtd_info *mtd;
38static unsigned char *twopages;
39static unsigned char *writebuf;
40static unsigned char *boundary;
41static unsigned char *bbt;
42
43static int pgsize;
44static int bufsize;
45static int ebcnt;
46static int pgcnt;
47static int errcnt;
48static unsigned long next = 1;
49
50static inline unsigned int simple_rand(void)
51{
52 next = next * 1103515245 + 12345;
53 return (unsigned int)((next / 65536) % 32768);
54}
55
56static inline void simple_srand(unsigned long seed)
57{
58 next = seed;
59}
60
61static 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
69static 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
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020080 err = mtd_erase(mtd, &ei);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020081 if (err) {
Vikram Narayananbb998412012-10-10 23:07:40 +053082 pr_err("error %d while erasing EB %d\n", err, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020083 return err;
84 }
85
86 if (ei.state == MTD_ERASE_FAILED) {
Vikram Narayananbb998412012-10-10 23:07:40 +053087 pr_err("some erase error occurred at EB %d\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +020088 ebnum);
89 return -EIO;
90 }
91
92 return 0;
93}
94
95static int write_eraseblock(int ebnum)
96{
97 int err = 0;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +020098 size_t written;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020099 loff_t addr = ebnum * mtd->erasesize;
100
101 set_random_data(writebuf, mtd->erasesize);
102 cond_resched();
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200103 err = mtd_write(mtd, addr, mtd->erasesize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200104 if (err || written != mtd->erasesize)
Vikram Narayananbb998412012-10-10 23:07:40 +0530105 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200106 (long long)addr);
107
108 return err;
109}
110
111static int verify_eraseblock(int ebnum)
112{
113 uint32_t j;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200114 size_t read;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200115 int err = 0, i;
116 loff_t addr0, addrn;
117 loff_t addr = ebnum * mtd->erasesize;
118
119 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200120 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200121 addr0 += mtd->erasesize;
122
123 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200124 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200125 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 */
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200130 err = mtd_read(mtd, addr0, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700131 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200132 err = 0;
133 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530134 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200135 (long long)addr0);
136 return err;
137 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200138 err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700139 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200140 err = 0;
141 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530142 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200143 (long long)(addrn - bufsize));
144 return err;
145 }
146 memset(twopages, 0, bufsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200147 err = mtd_read(mtd, addr, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700148 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200149 err = 0;
150 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530151 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200152 (long long)addr);
153 break;
154 }
155 if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530156 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200157 (long long)addr);
158 errcnt += 1;
159 }
160 }
161 /* Check boundary between eraseblocks */
162 if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
163 unsigned long oldnext = next;
164 /* Do a read to set the internal dataRAMs to different data */
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200165 err = mtd_read(mtd, addr0, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700166 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200167 err = 0;
168 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530169 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200170 (long long)addr0);
171 return err;
172 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200173 err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700174 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200175 err = 0;
176 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530177 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200178 (long long)(addrn - bufsize));
179 return err;
180 }
181 memset(twopages, 0, bufsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200182 err = mtd_read(mtd, addr, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700183 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200184 err = 0;
185 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530186 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200187 (long long)addr);
188 return err;
189 }
190 memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
191 set_random_data(boundary + pgsize, pgsize);
192 if (memcmp(twopages, boundary, bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530193 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200194 (long long)addr);
195 errcnt += 1;
196 }
197 next = oldnext;
198 }
199 return err;
200}
201
202static int crosstest(void)
203{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200204 size_t read;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200205 int err = 0, i;
206 loff_t addr, addr0, addrn;
207 unsigned char *pp1, *pp2, *pp3, *pp4;
208
Vikram Narayananbb998412012-10-10 23:07:40 +0530209 pr_info("crosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200210 pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
211 if (!pp1) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530212 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200213 return -ENOMEM;
214 }
215 pp2 = pp1 + pgsize;
216 pp3 = pp2 + pgsize;
217 pp4 = pp3 + pgsize;
218 memset(pp1, 0, pgsize * 4);
219
220 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200221 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200222 addr0 += mtd->erasesize;
223
224 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200225 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200226 addrn -= mtd->erasesize;
227
228 /* Read 2nd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200229 addr = addrn - pgsize - pgsize;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200230 err = mtd_read(mtd, addr, pgsize, &read, pp1);
Brian Norrisd57f40542011-09-20 18:34:25 -0700231 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200232 err = 0;
233 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530234 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200235 (long long)addr);
236 kfree(pp1);
237 return err;
238 }
239
240 /* Read 3rd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200241 addr = addrn - pgsize - pgsize - pgsize;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200242 err = mtd_read(mtd, addr, pgsize, &read, pp1);
Brian Norrisd57f40542011-09-20 18:34:25 -0700243 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200244 err = 0;
245 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530246 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200247 (long long)addr);
248 kfree(pp1);
249 return err;
250 }
251
252 /* Read first page to pp2 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200253 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530254 pr_info("reading page at %#llx\n", (long long)addr);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200255 err = mtd_read(mtd, addr, pgsize, &read, pp2);
Brian Norrisd57f40542011-09-20 18:34:25 -0700256 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200257 err = 0;
258 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530259 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200260 (long long)addr);
261 kfree(pp1);
262 return err;
263 }
264
265 /* Read last page to pp3 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200266 addr = addrn - pgsize;
Vikram Narayananbb998412012-10-10 23:07:40 +0530267 pr_info("reading page at %#llx\n", (long long)addr);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200268 err = mtd_read(mtd, addr, pgsize, &read, pp3);
Brian Norrisd57f40542011-09-20 18:34:25 -0700269 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200270 err = 0;
271 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530272 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200273 (long long)addr);
274 kfree(pp1);
275 return err;
276 }
277
278 /* Read first page again to pp4 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200279 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530280 pr_info("reading page at %#llx\n", (long long)addr);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200281 err = mtd_read(mtd, addr, pgsize, &read, pp4);
Brian Norrisd57f40542011-09-20 18:34:25 -0700282 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200283 err = 0;
284 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530285 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200286 (long long)addr);
287 kfree(pp1);
288 return err;
289 }
290
291 /* pp2 and pp4 should be the same */
Vikram Narayananbb998412012-10-10 23:07:40 +0530292 pr_info("verifying pages read at %#llx match\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200293 (long long)addr0);
294 if (memcmp(pp2, pp4, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530295 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200296 errcnt += 1;
297 } else if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530298 pr_info("crosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200299 kfree(pp1);
300 return err;
301}
302
303static int erasecrosstest(void)
304{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200305 size_t read, written;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300306 int err = 0, i, ebnum, ebnum2;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200307 loff_t addr0;
308 char *readbuf = twopages;
309
Vikram Narayananbb998412012-10-10 23:07:40 +0530310 pr_info("erasecrosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200311
312 ebnum = 0;
313 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200314 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200315 addr0 += mtd->erasesize;
316 ebnum += 1;
317 }
318
319 ebnum2 = ebcnt - 1;
320 while (ebnum2 && bbt[ebnum2])
321 ebnum2 -= 1;
322
Vikram Narayananbb998412012-10-10 23:07:40 +0530323 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200324 err = erase_eraseblock(ebnum);
325 if (err)
326 return err;
327
Vikram Narayananbb998412012-10-10 23:07:40 +0530328 pr_info("writing 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200329 set_random_data(writebuf, pgsize);
330 strcpy(writebuf, "There is no data like this!");
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200331 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200332 if (err || written != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530333 pr_info("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200334 (long long)addr0);
335 return err ? err : -1;
336 }
337
Vikram Narayananbb998412012-10-10 23:07:40 +0530338 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200339 memset(readbuf, 0, pgsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200340 err = mtd_read(mtd, addr0, pgsize, &read, readbuf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700341 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200342 err = 0;
343 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530344 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200345 (long long)addr0);
346 return err ? err : -1;
347 }
348
Vikram Narayananbb998412012-10-10 23:07:40 +0530349 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200350 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530351 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200352 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300353 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200354 }
355
Vikram Narayananbb998412012-10-10 23:07:40 +0530356 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200357 err = erase_eraseblock(ebnum);
358 if (err)
359 return err;
360
Vikram Narayananbb998412012-10-10 23:07:40 +0530361 pr_info("writing 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200362 set_random_data(writebuf, pgsize);
363 strcpy(writebuf, "There is no data like this!");
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200364 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200365 if (err || written != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530366 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200367 (long long)addr0);
368 return err ? err : -1;
369 }
370
Vikram Narayananbb998412012-10-10 23:07:40 +0530371 pr_info("erasing block %d\n", ebnum2);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200372 err = erase_eraseblock(ebnum2);
373 if (err)
374 return err;
375
Vikram Narayananbb998412012-10-10 23:07:40 +0530376 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200377 memset(readbuf, 0, pgsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200378 err = mtd_read(mtd, addr0, pgsize, &read, readbuf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700379 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200380 err = 0;
381 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530382 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200383 (long long)addr0);
384 return err ? err : -1;
385 }
386
Vikram Narayananbb998412012-10-10 23:07:40 +0530387 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200388 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530389 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200390 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300391 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200392 }
393
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300394 if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530395 pr_info("erasecrosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200396 return err;
397}
398
399static int erasetest(void)
400{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200401 size_t read, written;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200402 int err = 0, i, ebnum, ok = 1;
403 loff_t addr0;
404
Vikram Narayananbb998412012-10-10 23:07:40 +0530405 pr_info("erasetest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200406
407 ebnum = 0;
408 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200409 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200410 addr0 += mtd->erasesize;
411 ebnum += 1;
412 }
413
Vikram Narayananbb998412012-10-10 23:07:40 +0530414 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200415 err = erase_eraseblock(ebnum);
416 if (err)
417 return err;
418
Vikram Narayananbb998412012-10-10 23:07:40 +0530419 pr_info("writing 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200420 set_random_data(writebuf, pgsize);
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200421 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200422 if (err || written != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530423 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200424 (long long)addr0);
425 return err ? err : -1;
426 }
427
Vikram Narayananbb998412012-10-10 23:07:40 +0530428 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200429 err = erase_eraseblock(ebnum);
430 if (err)
431 return err;
432
Vikram Narayananbb998412012-10-10 23:07:40 +0530433 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200434 err = mtd_read(mtd, addr0, pgsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700435 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200436 err = 0;
437 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530438 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200439 (long long)addr0);
440 return err ? err : -1;
441 }
442
Vikram Narayananbb998412012-10-10 23:07:40 +0530443 pr_info("verifying 1st page of block %d is all 0xff\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200444 ebnum);
445 for (i = 0; i < pgsize; ++i)
446 if (twopages[i] != 0xff) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530447 pr_err("verifying all 0xff failed at %d\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200448 i);
449 errcnt += 1;
450 ok = 0;
451 break;
452 }
453
454 if (ok && !err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530455 pr_info("erasetest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200456
457 return err;
458}
459
460static int is_block_bad(int ebnum)
461{
462 loff_t addr = ebnum * mtd->erasesize;
463 int ret;
464
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200465 ret = mtd_block_isbad(mtd, addr);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200466 if (ret)
Vikram Narayananbb998412012-10-10 23:07:40 +0530467 pr_info("block %d is bad\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200468 return ret;
469}
470
471static int scan_for_bad_eraseblocks(void)
472{
473 int i, bad = 0;
474
Julia Lawall2bfefa42010-05-13 22:03:15 +0200475 bbt = kzalloc(ebcnt, GFP_KERNEL);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200476 if (!bbt) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530477 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200478 return -ENOMEM;
479 }
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200480
Vikram Narayananbb998412012-10-10 23:07:40 +0530481 pr_info("scanning for bad eraseblocks\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200482 for (i = 0; i < ebcnt; ++i) {
483 bbt[i] = is_block_bad(i) ? 1 : 0;
484 if (bbt[i])
485 bad += 1;
486 cond_resched();
487 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530488 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200489 return 0;
490}
491
492static int __init mtd_pagetest_init(void)
493{
494 int err = 0;
495 uint64_t tmp;
496 uint32_t i;
497
498 printk(KERN_INFO "\n");
499 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200500
501 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900502 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananbb998412012-10-10 23:07:40 +0530503 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200504 return -EINVAL;
505 }
506
Vikram Narayananbb998412012-10-10 23:07:40 +0530507 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200508
509 mtd = get_mtd_device(NULL, dev);
510 if (IS_ERR(mtd)) {
511 err = PTR_ERR(mtd);
Vikram Narayananbb998412012-10-10 23:07:40 +0530512 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200513 return err;
514 }
515
516 if (mtd->type != MTD_NANDFLASH) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530517 pr_info("this test requires NAND flash\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200518 goto out;
519 }
520
521 tmp = mtd->size;
522 do_div(tmp, mtd->erasesize);
523 ebcnt = tmp;
524 pgcnt = mtd->erasesize / mtd->writesize;
Artem Bityutskiy4c2b8a62009-11-27 16:58:08 +0200525 pgsize = mtd->writesize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200526
Vikram Narayananbb998412012-10-10 23:07:40 +0530527 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200528 "page size %u, count of eraseblocks %u, pages per "
529 "eraseblock %u, OOB size %u\n",
530 (unsigned long long)mtd->size, mtd->erasesize,
531 pgsize, ebcnt, pgcnt, mtd->oobsize);
532
533 err = -ENOMEM;
534 bufsize = pgsize * 2;
535 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
536 if (!writebuf) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530537 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200538 goto out;
539 }
540 twopages = kmalloc(bufsize, GFP_KERNEL);
541 if (!twopages) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530542 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200543 goto out;
544 }
545 boundary = kmalloc(bufsize, GFP_KERNEL);
546 if (!boundary) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530547 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200548 goto out;
549 }
550
551 err = scan_for_bad_eraseblocks();
552 if (err)
553 goto out;
554
555 /* Erase all eraseblocks */
Vikram Narayananbb998412012-10-10 23:07:40 +0530556 pr_info("erasing whole device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200557 for (i = 0; i < ebcnt; ++i) {
558 if (bbt[i])
559 continue;
560 err = erase_eraseblock(i);
561 if (err)
562 goto out;
563 cond_resched();
564 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530565 pr_info("erased %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200566
567 /* Write all eraseblocks */
568 simple_srand(1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530569 pr_info("writing whole device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200570 for (i = 0; i < ebcnt; ++i) {
571 if (bbt[i])
572 continue;
573 err = write_eraseblock(i);
574 if (err)
575 goto out;
576 if (i % 256 == 0)
Vikram Narayananbb998412012-10-10 23:07:40 +0530577 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200578 cond_resched();
579 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530580 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200581
582 /* Check all eraseblocks */
583 simple_srand(1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530584 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200585 for (i = 0; i < ebcnt; ++i) {
586 if (bbt[i])
587 continue;
588 err = verify_eraseblock(i);
589 if (err)
590 goto out;
591 if (i % 256 == 0)
Vikram Narayananbb998412012-10-10 23:07:40 +0530592 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200593 cond_resched();
594 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530595 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200596
597 err = crosstest();
598 if (err)
599 goto out;
600
601 err = erasecrosstest();
602 if (err)
603 goto out;
604
605 err = erasetest();
606 if (err)
607 goto out;
608
Vikram Narayananbb998412012-10-10 23:07:40 +0530609 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200610out:
611
612 kfree(bbt);
613 kfree(boundary);
614 kfree(twopages);
615 kfree(writebuf);
616 put_mtd_device(mtd);
617 if (err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530618 pr_info("error %d occurred\n", err);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200619 printk(KERN_INFO "=================================================\n");
620 return err;
621}
622module_init(mtd_pagetest_init);
623
624static void __exit mtd_pagetest_exit(void)
625{
626 return;
627}
628module_exit(mtd_pagetest_exit);
629
630MODULE_DESCRIPTION("NAND page test");
631MODULE_AUTHOR("Adrian Hunter");
632MODULE_LICENSE("GPL");