blob: ff1e0565b0204864f2f5487be0001696e2b2fca1 [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{
Brian Norris1001ff72014-07-21 19:07:12 -070055 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020056
Akinobu Mita825b8cc2013-02-27 17:05:35 -080057 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020058 cond_resched();
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +090059 return mtdtest_write(mtd, addr, mtd->erasesize, writebuf);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020060}
61
62static int verify_eraseblock(int ebnum)
63{
64 uint32_t j;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020065 int err = 0, i;
66 loff_t addr0, addrn;
Brian Norris1001ff72014-07-21 19:07:12 -070067 loff_t addr = (loff_t)ebnum * mtd->erasesize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020068
69 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +020070 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020071 addr0 += mtd->erasesize;
72
73 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +020074 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020075 addrn -= mtd->erasesize;
76
Akinobu Mita825b8cc2013-02-27 17:05:35 -080077 prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +020078 for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
79 /* Do a read to set the internal dataRAMs to different data */
Akinobu Mita66b28182013-08-03 18:52:10 +090080 err = mtdtest_read(mtd, addr0, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +090081 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020082 return err;
Akinobu Mita66b28182013-08-03 18:52:10 +090083 err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +090084 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020085 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020086 memset(twopages, 0, bufsize);
Akinobu Mita66b28182013-08-03 18:52:10 +090087 err = mtdtest_read(mtd, addr, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +090088 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +020089 break;
Artem Bityutskiye73f2172008-12-08 13:34:16 +020090 if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +053091 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +020092 (long long)addr);
93 errcnt += 1;
94 }
95 }
96 /* Check boundary between eraseblocks */
97 if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
Akinobu Mita825b8cc2013-02-27 17:05:35 -080098 struct rnd_state old_state = rnd_state;
99
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200100 /* Do a read to set the internal dataRAMs to different data */
Akinobu Mita66b28182013-08-03 18:52:10 +0900101 err = mtdtest_read(mtd, addr0, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900102 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200103 return err;
Akinobu Mita66b28182013-08-03 18:52:10 +0900104 err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900105 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200106 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200107 memset(twopages, 0, bufsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900108 err = mtdtest_read(mtd, addr, bufsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900109 if (err)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200110 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200111 memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800112 prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200113 if (memcmp(twopages, boundary, bufsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530114 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200115 (long long)addr);
116 errcnt += 1;
117 }
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800118 rnd_state = old_state;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200119 }
120 return err;
121}
122
123static int crosstest(void)
124{
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200125 int err = 0, i;
126 loff_t addr, addr0, addrn;
127 unsigned char *pp1, *pp2, *pp3, *pp4;
128
Vikram Narayananbb998412012-10-10 23:07:40 +0530129 pr_info("crosstest\n");
Nicholas Mc Guire6e756322015-12-31 16:21:22 +0100130 pp1 = kzalloc(pgsize * 4, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700131 if (!pp1)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200132 return -ENOMEM;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200133 pp2 = pp1 + pgsize;
134 pp3 = pp2 + pgsize;
135 pp4 = pp3 + pgsize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200136
137 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200138 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200139 addr0 += mtd->erasesize;
140
141 addrn = mtd->size;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200142 for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200143 addrn -= mtd->erasesize;
144
145 /* Read 2nd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200146 addr = addrn - pgsize - pgsize;
Akinobu Mita66b28182013-08-03 18:52:10 +0900147 err = mtdtest_read(mtd, addr, pgsize, pp1);
148 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200149 kfree(pp1);
150 return err;
151 }
152
153 /* Read 3rd-to-last page to pp1 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200154 addr = addrn - pgsize - pgsize - pgsize;
Akinobu Mita66b28182013-08-03 18:52:10 +0900155 err = mtdtest_read(mtd, addr, pgsize, pp1);
156 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200157 kfree(pp1);
158 return err;
159 }
160
161 /* Read first page to pp2 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200162 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530163 pr_info("reading page at %#llx\n", (long long)addr);
Akinobu Mita66b28182013-08-03 18:52:10 +0900164 err = mtdtest_read(mtd, addr, pgsize, pp2);
165 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200166 kfree(pp1);
167 return err;
168 }
169
170 /* Read last page to pp3 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200171 addr = addrn - pgsize;
Vikram Narayananbb998412012-10-10 23:07:40 +0530172 pr_info("reading page at %#llx\n", (long long)addr);
Akinobu Mita66b28182013-08-03 18:52:10 +0900173 err = mtdtest_read(mtd, addr, pgsize, pp3);
174 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200175 kfree(pp1);
176 return err;
177 }
178
179 /* Read first page again to pp4 */
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200180 addr = addr0;
Vikram Narayananbb998412012-10-10 23:07:40 +0530181 pr_info("reading page at %#llx\n", (long long)addr);
Akinobu Mita66b28182013-08-03 18:52:10 +0900182 err = mtdtest_read(mtd, addr, pgsize, pp4);
183 if (err) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200184 kfree(pp1);
185 return err;
186 }
187
188 /* pp2 and pp4 should be the same */
Vikram Narayananbb998412012-10-10 23:07:40 +0530189 pr_info("verifying pages read at %#llx match\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200190 (long long)addr0);
191 if (memcmp(pp2, pp4, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530192 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200193 errcnt += 1;
194 } else if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530195 pr_info("crosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200196 kfree(pp1);
197 return err;
198}
199
200static int erasecrosstest(void)
201{
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300202 int err = 0, i, ebnum, ebnum2;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200203 loff_t addr0;
204 char *readbuf = twopages;
205
Vikram Narayananbb998412012-10-10 23:07:40 +0530206 pr_info("erasecrosstest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200207
208 ebnum = 0;
209 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200210 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200211 addr0 += mtd->erasesize;
212 ebnum += 1;
213 }
214
215 ebnum2 = ebcnt - 1;
216 while (ebnum2 && bbt[ebnum2])
217 ebnum2 -= 1;
218
Vikram Narayananbb998412012-10-10 23:07:40 +0530219 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900220 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200221 if (err)
222 return err;
223
Vikram Narayananbb998412012-10-10 23:07:40 +0530224 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800225 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200226 strcpy(writebuf, "There is no data like this!");
Akinobu Mita66b28182013-08-03 18:52:10 +0900227 err = mtdtest_write(mtd, addr0, pgsize, writebuf);
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +0900228 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900229 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200230
Vikram Narayananbb998412012-10-10 23:07:40 +0530231 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200232 memset(readbuf, 0, pgsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900233 err = mtdtest_read(mtd, addr0, pgsize, readbuf);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900234 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900235 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200236
Vikram Narayananbb998412012-10-10 23:07:40 +0530237 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200238 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530239 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200240 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300241 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200242 }
243
Vikram Narayananbb998412012-10-10 23:07:40 +0530244 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900245 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200246 if (err)
247 return err;
248
Vikram Narayananbb998412012-10-10 23:07:40 +0530249 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800250 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200251 strcpy(writebuf, "There is no data like this!");
Akinobu Mita66b28182013-08-03 18:52:10 +0900252 err = mtdtest_write(mtd, addr0, pgsize, writebuf);
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +0900253 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900254 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200255
Vikram Narayananbb998412012-10-10 23:07:40 +0530256 pr_info("erasing block %d\n", ebnum2);
Akinobu Mita66b28182013-08-03 18:52:10 +0900257 err = mtdtest_erase_eraseblock(mtd, ebnum2);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200258 if (err)
259 return err;
260
Vikram Narayananbb998412012-10-10 23:07:40 +0530261 pr_info("reading 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200262 memset(readbuf, 0, pgsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900263 err = mtdtest_read(mtd, addr0, pgsize, readbuf);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900264 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900265 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200266
Vikram Narayananbb998412012-10-10 23:07:40 +0530267 pr_info("verifying 1st page of block %d\n", ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200268 if (memcmp(writebuf, readbuf, pgsize)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530269 pr_err("verify failed!\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200270 errcnt += 1;
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300271 return -1;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200272 }
273
Roel Kluin7fc14bc2010-07-13 13:24:31 +0300274 if (!err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530275 pr_info("erasecrosstest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200276 return err;
277}
278
279static int erasetest(void)
280{
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200281 int err = 0, i, ebnum, ok = 1;
282 loff_t addr0;
283
Vikram Narayananbb998412012-10-10 23:07:40 +0530284 pr_info("erasetest\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200285
286 ebnum = 0;
287 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200288 for (i = 0; i < ebcnt && bbt[i]; ++i) {
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200289 addr0 += mtd->erasesize;
290 ebnum += 1;
291 }
292
Vikram Narayananbb998412012-10-10 23:07:40 +0530293 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900294 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200295 if (err)
296 return err;
297
Vikram Narayananbb998412012-10-10 23:07:40 +0530298 pr_info("writing 1st page of block %d\n", ebnum);
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800299 prandom_bytes_state(&rnd_state, writebuf, pgsize);
Akinobu Mita66b28182013-08-03 18:52:10 +0900300 err = mtdtest_write(mtd, addr0, pgsize, writebuf);
Akinobu Mita8a9f4aa2013-08-15 22:55:09 +0900301 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900302 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200303
Vikram Narayananbb998412012-10-10 23:07:40 +0530304 pr_info("erasing block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900305 err = mtdtest_erase_eraseblock(mtd, ebnum);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200306 if (err)
307 return err;
308
Vikram Narayananbb998412012-10-10 23:07:40 +0530309 pr_info("reading 1st page of block %d\n", ebnum);
Akinobu Mita66b28182013-08-03 18:52:10 +0900310 err = mtdtest_read(mtd, addr0, pgsize, twopages);
Akinobu Mitaabc173a2013-08-15 22:55:08 +0900311 if (err)
Akinobu Mita66b28182013-08-03 18:52:10 +0900312 return err;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200313
Vikram Narayananbb998412012-10-10 23:07:40 +0530314 pr_info("verifying 1st page of block %d is all 0xff\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200315 ebnum);
316 for (i = 0; i < pgsize; ++i)
317 if (twopages[i] != 0xff) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530318 pr_err("verifying all 0xff failed at %d\n",
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200319 i);
320 errcnt += 1;
321 ok = 0;
322 break;
323 }
324
325 if (ok && !err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530326 pr_info("erasetest ok\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200327
328 return err;
329}
330
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200331static int __init mtd_pagetest_init(void)
332{
333 int err = 0;
334 uint64_t tmp;
335 uint32_t i;
336
337 printk(KERN_INFO "\n");
338 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200339
340 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900341 pr_info("Please specify a valid mtd-device via module parameter\n");
Vikram Narayananbb998412012-10-10 23:07:40 +0530342 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200343 return -EINVAL;
344 }
345
Vikram Narayananbb998412012-10-10 23:07:40 +0530346 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200347
348 mtd = get_mtd_device(NULL, dev);
349 if (IS_ERR(mtd)) {
350 err = PTR_ERR(mtd);
Vikram Narayananbb998412012-10-10 23:07:40 +0530351 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200352 return err;
353 }
354
Huang Shijie818b9732013-09-25 14:58:17 +0800355 if (!mtd_type_is_nand(mtd)) {
Vikram Narayananbb998412012-10-10 23:07:40 +0530356 pr_info("this test requires NAND flash\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200357 goto out;
358 }
359
360 tmp = mtd->size;
361 do_div(tmp, mtd->erasesize);
362 ebcnt = tmp;
363 pgcnt = mtd->erasesize / mtd->writesize;
Artem Bityutskiy4c2b8a62009-11-27 16:58:08 +0200364 pgsize = mtd->writesize;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200365
Vikram Narayananbb998412012-10-10 23:07:40 +0530366 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200367 "page size %u, count of eraseblocks %u, pages per "
368 "eraseblock %u, OOB size %u\n",
369 (unsigned long long)mtd->size, mtd->erasesize,
370 pgsize, ebcnt, pgcnt, mtd->oobsize);
371
372 err = -ENOMEM;
373 bufsize = pgsize * 2;
374 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700375 if (!writebuf)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200376 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200377 twopages = kmalloc(bufsize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700378 if (!twopages)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200379 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200380 boundary = kmalloc(bufsize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700381 if (!boundary)
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200382 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200383
Akinobu Mita66b28182013-08-03 18:52:10 +0900384 bbt = kzalloc(ebcnt, GFP_KERNEL);
385 if (!bbt)
386 goto out;
387 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200388 if (err)
389 goto out;
390
391 /* Erase all eraseblocks */
Vikram Narayananbb998412012-10-10 23:07:40 +0530392 pr_info("erasing whole device\n");
Akinobu Mita66b28182013-08-03 18:52:10 +0900393 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
394 if (err)
395 goto out;
396 pr_info("erased %u eraseblocks\n", ebcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200397
398 /* Write all eraseblocks */
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800399 prandom_seed_state(&rnd_state, 1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530400 pr_info("writing whole device\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200401 for (i = 0; i < ebcnt; ++i) {
402 if (bbt[i])
403 continue;
404 err = write_eraseblock(i);
405 if (err)
406 goto out;
407 if (i % 256 == 0)
Vikram Narayananbb998412012-10-10 23:07:40 +0530408 pr_info("written up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200409
410 err = mtdtest_relax();
411 if (err)
412 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200413 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530414 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200415
416 /* Check all eraseblocks */
Akinobu Mita825b8cc2013-02-27 17:05:35 -0800417 prandom_seed_state(&rnd_state, 1);
Vikram Narayananbb998412012-10-10 23:07:40 +0530418 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200419 for (i = 0; i < ebcnt; ++i) {
420 if (bbt[i])
421 continue;
422 err = verify_eraseblock(i);
423 if (err)
424 goto out;
425 if (i % 256 == 0)
Vikram Narayananbb998412012-10-10 23:07:40 +0530426 pr_info("verified up to eraseblock %u\n", i);
Richard Weinberger2a6a28e72015-03-29 21:52:06 +0200427
428 err = mtdtest_relax();
429 if (err)
430 goto out;
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200431 }
Vikram Narayananbb998412012-10-10 23:07:40 +0530432 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200433
434 err = crosstest();
435 if (err)
436 goto out;
437
438 err = erasecrosstest();
439 if (err)
440 goto out;
441
442 err = erasetest();
443 if (err)
444 goto out;
445
Vikram Narayananbb998412012-10-10 23:07:40 +0530446 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200447out:
448
449 kfree(bbt);
450 kfree(boundary);
451 kfree(twopages);
452 kfree(writebuf);
453 put_mtd_device(mtd);
454 if (err)
Vikram Narayananbb998412012-10-10 23:07:40 +0530455 pr_info("error %d occurred\n", err);
Artem Bityutskiye73f2172008-12-08 13:34:16 +0200456 printk(KERN_INFO "=================================================\n");
457 return err;
458}
459module_init(mtd_pagetest_init);
460
461static void __exit mtd_pagetest_exit(void)
462{
463 return;
464}
465module_exit(mtd_pagetest_exit);
466
467MODULE_DESCRIPTION("NAND page test");
468MODULE_AUTHOR("Adrian Hunter");
469MODULE_LICENSE("GPL");