blob: 9e8329c5d96d720e3bedfacdba88cc400b2e6ad2 [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
Akinobu Mita66b28182013-08-03 18:52:10 +090034#include "mtd_test.h"
35
Wolfram Sang74060602011-10-30 00:11:53 +020036static int dev = -EINVAL;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020037module_param(dev, int, S_IRUGO);
38MODULE_PARM_DESC(dev, "MTD device number to use");
39
40static struct mtd_info *mtd;
41static unsigned char *twopages;
42static unsigned char *writebuf;
43static unsigned char *boundary;
44static unsigned char *bbt;
45
46static int pgsize;
47static int bufsize;
48static int ebcnt;
49static int pgcnt;
50static int errcnt;
Akinobu Mita825b8cc2013-02-27 17:05:35 -080051static struct rnd_state rnd_state;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020052
Artem Bityutskiye73f2172008-12-08 13:34:16 +020053static int write_eraseblock(int ebnum)
54{
Akinobu Mita66b28182013-08-03 18:52:10 +090055 int err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020056 loff_t addr = ebnum * mtd->erasesize;
57
Akinobu Mita825b8cc2013-02-27 17:05:35 -080058 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020059 cond_resched();
Akinobu Mita66b28182013-08-03 18:52:10 +090060 err = mtdtest_write(mtd, addr, mtd->erasesize, writebuf);
61 if (err)
Vikram Narayananbb998412012-10-10 23:07:40 +053062 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +020063 (long long)addr);
64
65 return err;
66}
67
68static int verify_eraseblock(int ebnum)
69{
70 uint32_t j;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020071 int err = 0, i;
72 loff_t addr0, addrn;
73 loff_t addr = ebnum * mtd->erasesize;
74
75 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +020076 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020077 addr0 += mtd->erasesize;
78
79 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +020080 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020081 addrn -= 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 for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
85 /* Do a read to set the internal dataRAMs to different data */
Akinobu Mita66b28182013-08-03 18:52:10 +090086 err = mtdtest_read(mtd, addr0, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +090087 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020088 return err;
Akinobu Mita66b28182013-08-03 18:52:10 +090089 err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +090090 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020091 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020092 memset(twopages, 0, bufsize);
Akinobu Mita66b28182013-08-03 18:52:10 +090093 err = mtdtest_read(mtd, addr, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +090094 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020095 break;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020096 if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +053097 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +020098 (long long)addr);
99 errcnt += 1;
100 }
101 }
102 /* Check boundary between eraseblocks */
103 if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800104 struct rnd_state old_state = rnd_state;
105
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200106 /* Do a read to set the internal dataRAMs to different data */
Akinobu Mita66b28182013-08-03 18:52:10 +0900107 err = mtdtest_read(mtd, addr0, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900108 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200109 return err;
Akinobu Mita66b28182013-08-03 18:52:10 +0900110 err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900111 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200112 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200113 memset(twopages, 0, bufsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900114 err = mtdtest_read(mtd, addr, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900115 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200116 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200117 memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800118 prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200119 if (memcmp(twopages, boundary, bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530120 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200121 (long long)addr);
122 errcnt += 1;
123 }
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800124 rnd_state = old_state;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200125 }
126 return err;
127}
128
129static int crosstest(void)
130{
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200131 int err = 0, i;
132 loff_t addr, addr0, addrn;
133 unsigned char *pp1, *pp2, *pp3, *pp4;
134
Vikram Narayananbb998412012-10-10 23:07:40 +0530135 pr_info("crosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200136 pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700137 if (!pp1)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200138 return -ENOMEM;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200139 pp2 = pp1 + pgsize;
140 pp3 = pp2 + pgsize;
141 pp4 = pp3 + pgsize;
142 memset(pp1, 0, pgsize * 4);
143
144 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200145 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200146 addr0 += mtd->erasesize;
147
148 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200149 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200150 addrn -= mtd->erasesize;
151
152 /* Read 2nd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200153 addr = addrn - pgsize - pgsize;
Akinobu Mita66b28182013-08-03 18:52:10 +0900154 err = mtdtest_read(mtd, addr, pgsize, pp1);
155 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200156 kfree(pp1);
157 return err;
158 }
159
160 /* Read 3rd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200161 addr = addrn - pgsize - pgsize - pgsize;
Akinobu Mita66b28182013-08-03 18:52:10 +0900162 err = mtdtest_read(mtd, addr, pgsize, pp1);
163 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200164 kfree(pp1);
165 return err;
166 }
167
168 /* Read first page to pp2 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200169 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530170 pr_info("reading page at %#llx\n", (long long)addr);
Akinobu Mita66b28182013-08-03 18:52:10 +0900171 err = mtdtest_read(mtd, addr, pgsize, pp2);
172 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200173 kfree(pp1);
174 return err;
175 }
176
177 /* Read last page to pp3 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200178 addr = addrn - pgsize;
Vikram Narayananbb998412012-10-10 23:07:40 +0530179 pr_info("reading page at %#llx\n", (long long)addr);
Akinobu Mita66b28182013-08-03 18:52:10 +0900180 err = mtdtest_read(mtd, addr, pgsize, pp3);
181 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200182 kfree(pp1);
183 return err;
184 }
185
186 /* Read first page again to pp4 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200187 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530188 pr_info("reading page at %#llx\n", (long long)addr);
Akinobu Mita66b28182013-08-03 18:52:10 +0900189 err = mtdtest_read(mtd, addr, pgsize, pp4);
190 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200191 kfree(pp1);
192 return err;
193 }
194
195 /* pp2 and pp4 should be the same */
Vikram Narayananbb998412012-10-10 23:07:40 +0530196 pr_info("verifying pages read at %#llx match\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200197 (long long)addr0);
198 if (memcmp(pp2, pp4, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530199 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200200 errcnt += 1;
201 } else if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530202 pr_info("crosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200203 kfree(pp1);
204 return err;
205}
206
207static int erasecrosstest(void)
208{
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300209 int err = 0, i, ebnum, ebnum2;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200210 loff_t addr0;
211 char *readbuf = twopages;
212
Vikram Narayananbb998412012-10-10 23:07:40 +0530213 pr_info("erasecrosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200214
215 ebnum = 0;
216 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200217 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200218 addr0 += mtd->erasesize;
219 ebnum += 1;
220 }
221
222 ebnum2 = ebcnt - 1;
223 while (ebnum2 && bbt[ebnum2])
224 ebnum2 -= 1;
225
Vikram Narayananbb998412012-10-10 23:07:40 +0530226 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900227 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200228 if (err)
229 return err;
230
Vikram Narayananbb998412012-10-10 23:07:40 +0530231 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800232 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200233 strcpy(writebuf, "There is no data like this!");
Akinobu Mita66b28182013-08-03 18:52:10 +0900234 err = mtdtest_write(mtd, addr0, pgsize, writebuf);
235 if (err) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530236 pr_info("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200237 (long long)addr0);
Akinobu Mita66b28182013-08-03 18:52:10 +0900238 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200239 }
240
Vikram Narayananbb998412012-10-10 23:07:40 +0530241 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200242 memset(readbuf, 0, pgsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900243 err = mtdtest_read(mtd, addr0, pgsize, readbuf);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900244 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900245 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200246
Vikram Narayananbb998412012-10-10 23:07:40 +0530247 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200248 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530249 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200250 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300251 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200252 }
253
Vikram Narayananbb998412012-10-10 23:07:40 +0530254 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900255 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200256 if (err)
257 return err;
258
Vikram Narayananbb998412012-10-10 23:07:40 +0530259 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800260 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200261 strcpy(writebuf, "There is no data like this!");
Akinobu Mita66b28182013-08-03 18:52:10 +0900262 err = mtdtest_write(mtd, addr0, pgsize, writebuf);
263 if (err) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530264 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200265 (long long)addr0);
Akinobu Mita66b28182013-08-03 18:52:10 +0900266 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200267 }
268
Vikram Narayananbb998412012-10-10 23:07:40 +0530269 pr_info("erasing block %d\n", ebnum2);
Akinobu Mita66b28182013-08-03 18:52:10 +0900270 err = mtdtest_erase_eraseblock(mtd, ebnum2);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200271 if (err)
272 return err;
273
Vikram Narayananbb998412012-10-10 23:07:40 +0530274 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200275 memset(readbuf, 0, pgsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900276 err = mtdtest_read(mtd, addr0, pgsize, readbuf);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900277 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900278 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200279
Vikram Narayananbb998412012-10-10 23:07:40 +0530280 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200281 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530282 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200283 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300284 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200285 }
286
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300287 if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530288 pr_info("erasecrosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200289 return err;
290}
291
292static int erasetest(void)
293{
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200294 int err = 0, i, ebnum, ok = 1;
295 loff_t addr0;
296
Vikram Narayananbb998412012-10-10 23:07:40 +0530297 pr_info("erasetest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200298
299 ebnum = 0;
300 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200301 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200302 addr0 += mtd->erasesize;
303 ebnum += 1;
304 }
305
Vikram Narayananbb998412012-10-10 23:07:40 +0530306 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900307 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200308 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);
Akinobu Mita66b28182013-08-03 18:52:10 +0900313 err = mtdtest_write(mtd, addr0, pgsize, writebuf);
314 if (err) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530315 pr_err("error: write failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200316 (long long)addr0);
Akinobu Mita66b28182013-08-03 18:52:10 +0900317 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200318 }
319
Vikram Narayananbb998412012-10-10 23:07:40 +0530320 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900321 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200322 if (err)
323 return err;
324
Vikram Narayananbb998412012-10-10 23:07:40 +0530325 pr_info("reading 1st page of block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900326 err = mtdtest_read(mtd, addr0, pgsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900327 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900328 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200329
Vikram Narayananbb998412012-10-10 23:07:40 +0530330 pr_info("verifying 1st page of block %d is all 0xff\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200331 ebnum);
332 for (i = 0; i < pgsize; ++i)
333 if (twopages[i] != 0xff) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530334 pr_err("verifying all 0xff failed at %d\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200335 i);
336 errcnt += 1;
337 ok = 0;
338 break;
339 }
340
341 if (ok && !err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530342 pr_info("erasetest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200343
344 return err;
345}
346
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200347static int __init mtd_pagetest_init(void)
348{
349 int err = 0;
350 uint64_t tmp;
351 uint32_t i;
352
353 printk(KERN_INFO "\n");
354 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200355
356 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900357 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananbb998412012-10-10 23:07:40 +0530358 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200359 return -EINVAL;
360 }
361
Vikram Narayananbb998412012-10-10 23:07:40 +0530362 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200363
364 mtd = get_mtd_device(NULL, dev);
365 if (IS_ERR(mtd)) {
366 err = PTR_ERR(mtd);
Vikram Narayananbb998412012-10-10 23:07:40 +0530367 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200368 return err;
369 }
370
371 if (mtd->type != MTD_NANDFLASH) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530372 pr_info("this test requires NAND flash\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200373 goto out;
374 }
375
376 tmp = mtd->size;
377 do_div(tmp, mtd->erasesize);
378 ebcnt = tmp;
379 pgcnt = mtd->erasesize / mtd->writesize;
Artem Bityutskiy4c2b8a62009-11-27 16:58:08 +0200380 pgsize = mtd->writesize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200381
Vikram Narayananbb998412012-10-10 23:07:40 +0530382 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200383 "page size %u, count of eraseblocks %u, pages per "
384 "eraseblock %u, OOB size %u\n",
385 (unsigned long long)mtd->size, mtd->erasesize,
386 pgsize, ebcnt, pgcnt, mtd->oobsize);
387
388 err = -ENOMEM;
389 bufsize = pgsize * 2;
390 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700391 if (!writebuf)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200392 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200393 twopages = kmalloc(bufsize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700394 if (!twopages)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200395 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200396 boundary = kmalloc(bufsize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700397 if (!boundary)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200398 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200399
Akinobu Mita66b28182013-08-03 18:52:10 +0900400 bbt = kzalloc(ebcnt, GFP_KERNEL);
401 if (!bbt)
402 goto out;
403 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200404 if (err)
405 goto out;
406
407 /* Erase all eraseblocks */
Vikram Narayananbb998412012-10-10 23:07:40 +0530408 pr_info("erasing whole device\n");
Akinobu Mita66b28182013-08-03 18:52:10 +0900409 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
410 if (err)
411 goto out;
412 pr_info("erased %u eraseblocks\n", ebcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200413
414 /* Write all eraseblocks */
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800415 prandom_seed_state(&rnd_state, 1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530416 pr_info("writing whole device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200417 for (i = 0; i < ebcnt; ++i) {
418 if (bbt[i])
419 continue;
420 err = write_eraseblock(i);
421 if (err)
422 goto out;
423 if (i % 256 == 0)
Vikram Narayananbb998412012-10-10 23:07:40 +0530424 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200425 cond_resched();
426 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530427 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200428
429 /* Check all eraseblocks */
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800430 prandom_seed_state(&rnd_state, 1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530431 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200432 for (i = 0; i < ebcnt; ++i) {
433 if (bbt[i])
434 continue;
435 err = verify_eraseblock(i);
436 if (err)
437 goto out;
438 if (i % 256 == 0)
Vikram Narayananbb998412012-10-10 23:07:40 +0530439 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200440 cond_resched();
441 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530442 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200443
444 err = crosstest();
445 if (err)
446 goto out;
447
448 err = erasecrosstest();
449 if (err)
450 goto out;
451
452 err = erasetest();
453 if (err)
454 goto out;
455
Vikram Narayananbb998412012-10-10 23:07:40 +0530456 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200457out:
458
459 kfree(bbt);
460 kfree(boundary);
461 kfree(twopages);
462 kfree(writebuf);
463 put_mtd_device(mtd);
464 if (err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530465 pr_info("error %d occurred\n", err);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200466 printk(KERN_INFO "=================================================\n");
467 return err;
468}
469module_init(mtd_pagetest_init);
470
471static void __exit mtd_pagetest_exit(void)
472{
473 return;
474}
475module_exit(mtd_pagetest_exit);
476
477MODULE_DESCRIPTION("NAND page test");
478MODULE_AUTHOR("Adrian Hunter");
479MODULE_LICENSE("GPL");