blob: 0c1140b6c2863b92087d1b8156895a5a5162e664 [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>
Akinobu Mita825b8cc2013-02-27 17:05:35 -080032#include <linux/random.h>
Artem Bityutskiye73f2172008-12-08 13:34:16 +020033
Wolfram Sang74060602011-10-30 00:11:53 +020034static int dev = -EINVAL;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020035module_param(dev, int, S_IRUGO);
36MODULE_PARM_DESC(dev, "MTD device number to use");
37
38static struct mtd_info *mtd;
39static unsigned char *twopages;
40static unsigned char *writebuf;
41static unsigned char *boundary;
42static unsigned char *bbt;
43
44static int pgsize;
45static int bufsize;
46static int ebcnt;
47static int pgcnt;
48static int errcnt;
Akinobu Mita825b8cc2013-02-27 17:05:35 -080049static struct rnd_state rnd_state;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020050
51static 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 Bityutskiy7e1f0dc2011-12-23 15:25:39 +020062 err = mtd_erase(mtd, &ei);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020063 if (err) {
Vikram Narayananbb998412012-10-10 23:07:40 +053064 pr_err("error %d while erasing EB %d\n", err, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020065 return err;
66 }
67
68 if (ei.state == MTD_ERASE_FAILED) {
Vikram Narayananbb998412012-10-10 23:07:40 +053069 pr_err("some erase error occurred at EB %d\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +020070 ebnum);
71 return -EIO;
72 }
73
74 return 0;
75}
76
77static int write_eraseblock(int ebnum)
78{
79 int err = 0;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +020080 size_t written;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020081 loff_t addr = ebnum * mtd->erasesize;
82
Akinobu Mita825b8cc2013-02-27 17:05:35 -080083 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020084 cond_resched();
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +020085 err = mtd_write(mtd, addr, mtd->erasesize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020086 if (err || written != mtd->erasesize)
Vikram Narayananbb998412012-10-10 23:07:40 +053087 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +020088 (long long)addr);
89
90 return err;
91}
92
93static int verify_eraseblock(int ebnum)
94{
95 uint32_t j;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +020096 size_t read;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020097 int err = 0, i;
98 loff_t addr0, addrn;
99 loff_t addr = ebnum * mtd->erasesize;
100
101 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200102 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200103 addr0 += mtd->erasesize;
104
105 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200106 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200107 addrn -= mtd->erasesize;
108
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800109 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200110 for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
111 /* Do a read to set the internal dataRAMs to different data */
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200112 err = mtd_read(mtd, addr0, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700113 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200114 err = 0;
115 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530116 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200117 (long long)addr0);
118 return err;
119 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200120 err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700121 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200122 err = 0;
123 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530124 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200125 (long long)(addrn - bufsize));
126 return err;
127 }
128 memset(twopages, 0, bufsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200129 err = mtd_read(mtd, addr, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700130 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200131 err = 0;
132 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530133 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200134 (long long)addr);
135 break;
136 }
137 if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530138 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200139 (long long)addr);
140 errcnt += 1;
141 }
142 }
143 /* Check boundary between eraseblocks */
144 if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800145 struct rnd_state old_state = rnd_state;
146
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200147 /* Do a read to set the internal dataRAMs to different data */
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200148 err = mtd_read(mtd, addr0, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700149 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200150 err = 0;
151 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530152 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200153 (long long)addr0);
154 return err;
155 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200156 err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700157 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200158 err = 0;
159 if (err || read != bufsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530160 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200161 (long long)(addrn - bufsize));
162 return err;
163 }
164 memset(twopages, 0, bufsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200165 err = mtd_read(mtd, addr, 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)addr);
171 return err;
172 }
173 memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800174 prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200175 if (memcmp(twopages, boundary, bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530176 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200177 (long long)addr);
178 errcnt += 1;
179 }
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800180 rnd_state = old_state;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200181 }
182 return err;
183}
184
185static int crosstest(void)
186{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200187 size_t read;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200188 int err = 0, i;
189 loff_t addr, addr0, addrn;
190 unsigned char *pp1, *pp2, *pp3, *pp4;
191
Vikram Narayananbb998412012-10-10 23:07:40 +0530192 pr_info("crosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200193 pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
194 if (!pp1) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530195 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200196 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 Kluinc6f7e7b2009-07-31 16:21:01 +0200204 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200205 addr0 += mtd->erasesize;
206
207 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200208 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200209 addrn -= mtd->erasesize;
210
211 /* Read 2nd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200212 addr = addrn - pgsize - pgsize;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200213 err = mtd_read(mtd, addr, pgsize, &read, pp1);
Brian Norrisd57f40542011-09-20 18:34:25 -0700214 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200215 err = 0;
216 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530217 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200218 (long long)addr);
219 kfree(pp1);
220 return err;
221 }
222
223 /* Read 3rd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200224 addr = addrn - pgsize - pgsize - pgsize;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200225 err = mtd_read(mtd, addr, pgsize, &read, pp1);
Brian Norrisd57f40542011-09-20 18:34:25 -0700226 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200227 err = 0;
228 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530229 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200230 (long long)addr);
231 kfree(pp1);
232 return err;
233 }
234
235 /* Read first page to pp2 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200236 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530237 pr_info("reading page at %#llx\n", (long long)addr);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200238 err = mtd_read(mtd, addr, pgsize, &read, pp2);
Brian Norrisd57f40542011-09-20 18:34:25 -0700239 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200240 err = 0;
241 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530242 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200243 (long long)addr);
244 kfree(pp1);
245 return err;
246 }
247
248 /* Read last page to pp3 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200249 addr = addrn - pgsize;
Vikram Narayananbb998412012-10-10 23:07:40 +0530250 pr_info("reading page at %#llx\n", (long long)addr);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200251 err = mtd_read(mtd, addr, pgsize, &read, pp3);
Brian Norrisd57f40542011-09-20 18:34:25 -0700252 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200253 err = 0;
254 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530255 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200256 (long long)addr);
257 kfree(pp1);
258 return err;
259 }
260
261 /* Read first page again to pp4 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200262 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530263 pr_info("reading page at %#llx\n", (long long)addr);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200264 err = mtd_read(mtd, addr, pgsize, &read, pp4);
Brian Norrisd57f40542011-09-20 18:34:25 -0700265 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200266 err = 0;
267 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530268 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200269 (long long)addr);
270 kfree(pp1);
271 return err;
272 }
273
274 /* pp2 and pp4 should be the same */
Vikram Narayananbb998412012-10-10 23:07:40 +0530275 pr_info("verifying pages read at %#llx match\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200276 (long long)addr0);
277 if (memcmp(pp2, pp4, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530278 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200279 errcnt += 1;
280 } else if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530281 pr_info("crosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200282 kfree(pp1);
283 return err;
284}
285
286static int erasecrosstest(void)
287{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200288 size_t read, written;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300289 int err = 0, i, ebnum, ebnum2;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200290 loff_t addr0;
291 char *readbuf = twopages;
292
Vikram Narayananbb998412012-10-10 23:07:40 +0530293 pr_info("erasecrosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200294
295 ebnum = 0;
296 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200297 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200298 addr0 += mtd->erasesize;
299 ebnum += 1;
300 }
301
302 ebnum2 = ebcnt - 1;
303 while (ebnum2 && bbt[ebnum2])
304 ebnum2 -= 1;
305
Vikram Narayananbb998412012-10-10 23:07:40 +0530306 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200307 err = erase_eraseblock(ebnum);
308 if (err)
309 return err;
310
Vikram Narayananbb998412012-10-10 23:07:40 +0530311 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800312 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200313 strcpy(writebuf, "There is no data like this!");
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200314 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200315 if (err || written != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530316 pr_info("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200317 (long long)addr0);
318 return err ? err : -1;
319 }
320
Vikram Narayananbb998412012-10-10 23:07:40 +0530321 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200322 memset(readbuf, 0, pgsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200323 err = mtd_read(mtd, addr0, pgsize, &read, readbuf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700324 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200325 err = 0;
326 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530327 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200328 (long long)addr0);
329 return err ? err : -1;
330 }
331
Vikram Narayananbb998412012-10-10 23:07:40 +0530332 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200333 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530334 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200335 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300336 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200337 }
338
Vikram Narayananbb998412012-10-10 23:07:40 +0530339 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200340 err = erase_eraseblock(ebnum);
341 if (err)
342 return err;
343
Vikram Narayananbb998412012-10-10 23:07:40 +0530344 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800345 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200346 strcpy(writebuf, "There is no data like this!");
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200347 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200348 if (err || written != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530349 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200350 (long long)addr0);
351 return err ? err : -1;
352 }
353
Vikram Narayananbb998412012-10-10 23:07:40 +0530354 pr_info("erasing block %d\n", ebnum2);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200355 err = erase_eraseblock(ebnum2);
356 if (err)
357 return err;
358
Vikram Narayananbb998412012-10-10 23:07:40 +0530359 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200360 memset(readbuf, 0, pgsize);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200361 err = mtd_read(mtd, addr0, pgsize, &read, readbuf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700362 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200363 err = 0;
364 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530365 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200366 (long long)addr0);
367 return err ? err : -1;
368 }
369
Vikram Narayananbb998412012-10-10 23:07:40 +0530370 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200371 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530372 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200373 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300374 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200375 }
376
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300377 if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530378 pr_info("erasecrosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200379 return err;
380}
381
382static int erasetest(void)
383{
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200384 size_t read, written;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200385 int err = 0, i, ebnum, ok = 1;
386 loff_t addr0;
387
Vikram Narayananbb998412012-10-10 23:07:40 +0530388 pr_info("erasetest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200389
390 ebnum = 0;
391 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200392 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200393 addr0 += mtd->erasesize;
394 ebnum += 1;
395 }
396
Vikram Narayananbb998412012-10-10 23:07:40 +0530397 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200398 err = erase_eraseblock(ebnum);
399 if (err)
400 return err;
401
Vikram Narayananbb998412012-10-10 23:07:40 +0530402 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800403 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200404 err = mtd_write(mtd, addr0, pgsize, &written, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200405 if (err || written != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530406 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200407 (long long)addr0);
408 return err ? err : -1;
409 }
410
Vikram Narayananbb998412012-10-10 23:07:40 +0530411 pr_info("erasing block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200412 err = erase_eraseblock(ebnum);
413 if (err)
414 return err;
415
Vikram Narayananbb998412012-10-10 23:07:40 +0530416 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200417 err = mtd_read(mtd, addr0, pgsize, &read, twopages);
Brian Norrisd57f40542011-09-20 18:34:25 -0700418 if (mtd_is_bitflip(err))
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200419 err = 0;
420 if (err || read != pgsize) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530421 pr_err("error: read failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200422 (long long)addr0);
423 return err ? err : -1;
424 }
425
Vikram Narayananbb998412012-10-10 23:07:40 +0530426 pr_info("verifying 1st page of block %d is all 0xff\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200427 ebnum);
428 for (i = 0; i < pgsize; ++i)
429 if (twopages[i] != 0xff) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530430 pr_err("verifying all 0xff failed at %d\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200431 i);
432 errcnt += 1;
433 ok = 0;
434 break;
435 }
436
437 if (ok && !err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530438 pr_info("erasetest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200439
440 return err;
441}
442
443static int is_block_bad(int ebnum)
444{
445 loff_t addr = ebnum * mtd->erasesize;
446 int ret;
447
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200448 ret = mtd_block_isbad(mtd, addr);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200449 if (ret)
Vikram Narayananbb998412012-10-10 23:07:40 +0530450 pr_info("block %d is bad\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200451 return ret;
452}
453
454static int scan_for_bad_eraseblocks(void)
455{
456 int i, bad = 0;
457
Julia Lawall2bfefa42010-05-13 22:03:15 +0200458 bbt = kzalloc(ebcnt, GFP_KERNEL);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200459 if (!bbt) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530460 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200461 return -ENOMEM;
462 }
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200463
Vikram Narayananbb998412012-10-10 23:07:40 +0530464 pr_info("scanning for bad eraseblocks\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200465 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 Narayananbb998412012-10-10 23:07:40 +0530471 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200472 return 0;
473}
474
475static 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 Sang74060602011-10-30 00:11:53 +0200483
484 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900485 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananbb998412012-10-10 23:07:40 +0530486 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200487 return -EINVAL;
488 }
489
Vikram Narayananbb998412012-10-10 23:07:40 +0530490 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200491
492 mtd = get_mtd_device(NULL, dev);
493 if (IS_ERR(mtd)) {
494 err = PTR_ERR(mtd);
Vikram Narayananbb998412012-10-10 23:07:40 +0530495 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200496 return err;
497 }
498
499 if (mtd->type != MTD_NANDFLASH) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530500 pr_info("this test requires NAND flash\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200501 goto out;
502 }
503
504 tmp = mtd->size;
505 do_div(tmp, mtd->erasesize);
506 ebcnt = tmp;
507 pgcnt = mtd->erasesize / mtd->writesize;
Artem Bityutskiy4c2b8a62009-11-27 16:58:08 +0200508 pgsize = mtd->writesize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200509
Vikram Narayananbb998412012-10-10 23:07:40 +0530510 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200511 "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 Narayananbb998412012-10-10 23:07:40 +0530520 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200521 goto out;
522 }
523 twopages = kmalloc(bufsize, GFP_KERNEL);
524 if (!twopages) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530525 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200526 goto out;
527 }
528 boundary = kmalloc(bufsize, GFP_KERNEL);
529 if (!boundary) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530530 pr_err("error: cannot allocate memory\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200531 goto out;
532 }
533
534 err = scan_for_bad_eraseblocks();
535 if (err)
536 goto out;
537
538 /* Erase all eraseblocks */
Vikram Narayananbb998412012-10-10 23:07:40 +0530539 pr_info("erasing whole device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200540 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 Narayananbb998412012-10-10 23:07:40 +0530548 pr_info("erased %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200549
550 /* Write all eraseblocks */
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800551 prandom_seed_state(&rnd_state, 1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530552 pr_info("writing whole device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200553 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 Narayananbb998412012-10-10 23:07:40 +0530560 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200561 cond_resched();
562 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530563 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200564
565 /* Check all eraseblocks */
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800566 prandom_seed_state(&rnd_state, 1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530567 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200568 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 Narayananbb998412012-10-10 23:07:40 +0530575 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200576 cond_resched();
577 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530578 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200579
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 Narayananbb998412012-10-10 23:07:40 +0530592 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200593out:
594
595 kfree(bbt);
596 kfree(boundary);
597 kfree(twopages);
598 kfree(writebuf);
599 put_mtd_device(mtd);
600 if (err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530601 pr_info("error %d occurred\n", err);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200602 printk(KERN_INFO "=================================================\n");
603 return err;
604}
605module_init(mtd_pagetest_init);
606
607static void __exit mtd_pagetest_exit(void)
608{
609 return;
610}
611module_exit(mtd_pagetest_exit);
612
613MODULE_DESCRIPTION("NAND page test");
614MODULE_AUTHOR("Adrian Hunter");
615MODULE_LICENSE("GPL");