2 * Copyright (C) 2007 Nokia Corporation
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.
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
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.
17 * Test read and write speed of a MTD device.
19 * Author: Adrian Hunter <adrian.hunter@nokia.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/err.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/slab.h>
30 #include <linux/sched.h>
31 #include <linux/random.h>
33 static int dev = -EINVAL;
34 module_param(dev, int, S_IRUGO);
35 MODULE_PARM_DESC(dev, "MTD device number to use");
38 module_param(count, int, S_IRUGO);
39 MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
42 static struct mtd_info *mtd;
43 static unsigned char *iobuf;
44 static unsigned char *bbt;
50 static struct timeval start, finish;
52 static void set_random_data(unsigned char *buf, size_t len)
56 for (i = 0; i < len; ++i)
60 static int erase_eraseblock(int ebnum)
64 loff_t addr = ebnum * mtd->erasesize;
66 memset(&ei, 0, sizeof(struct erase_info));
69 ei.len = mtd->erasesize;
71 err = mtd_erase(mtd, &ei);
73 pr_err("error %d while erasing EB %d\n", err, ebnum);
77 if (ei.state == MTD_ERASE_FAILED) {
78 pr_err("some erase error occurred at EB %d\n",
86 static int multiblock_erase(int ebnum, int blocks)
90 loff_t addr = ebnum * mtd->erasesize;
92 memset(&ei, 0, sizeof(struct erase_info));
95 ei.len = mtd->erasesize * blocks;
97 err = mtd_erase(mtd, &ei);
99 pr_err("error %d while erasing EB %d, blocks %d\n",
104 if (ei.state == MTD_ERASE_FAILED) {
105 pr_err("some erase error occurred at EB %d,"
106 "blocks %d\n", ebnum, blocks);
113 static int erase_whole_device(void)
118 for (i = 0; i < ebcnt; ++i) {
121 err = erase_eraseblock(i);
129 static int write_eraseblock(int ebnum)
133 loff_t addr = ebnum * mtd->erasesize;
135 err = mtd_write(mtd, addr, mtd->erasesize, &written, iobuf);
136 if (err || written != mtd->erasesize) {
137 pr_err("error: write failed at %#llx\n", addr);
145 static int write_eraseblock_by_page(int ebnum)
149 loff_t addr = ebnum * mtd->erasesize;
152 for (i = 0; i < pgcnt; i++) {
153 err = mtd_write(mtd, addr, pgsize, &written, buf);
154 if (err || written != pgsize) {
155 pr_err("error: write failed at %#llx\n",
168 static int write_eraseblock_by_2pages(int ebnum)
170 size_t written, sz = pgsize * 2;
171 int i, n = pgcnt / 2, err = 0;
172 loff_t addr = ebnum * mtd->erasesize;
175 for (i = 0; i < n; i++) {
176 err = mtd_write(mtd, addr, sz, &written, buf);
177 if (err || written != sz) {
178 pr_err("error: write failed at %#llx\n",
188 err = mtd_write(mtd, addr, pgsize, &written, buf);
189 if (err || written != pgsize) {
190 pr_err("error: write failed at %#llx\n",
200 static int read_eraseblock(int ebnum)
204 loff_t addr = ebnum * mtd->erasesize;
206 err = mtd_read(mtd, addr, mtd->erasesize, &read, iobuf);
207 /* Ignore corrected ECC errors */
208 if (mtd_is_bitflip(err))
210 if (err || read != mtd->erasesize) {
211 pr_err("error: read failed at %#llx\n", addr);
219 static int read_eraseblock_by_page(int ebnum)
223 loff_t addr = ebnum * mtd->erasesize;
226 for (i = 0; i < pgcnt; i++) {
227 err = mtd_read(mtd, addr, pgsize, &read, buf);
228 /* Ignore corrected ECC errors */
229 if (mtd_is_bitflip(err))
231 if (err || read != pgsize) {
232 pr_err("error: read failed at %#llx\n",
245 static int read_eraseblock_by_2pages(int ebnum)
247 size_t read, sz = pgsize * 2;
248 int i, n = pgcnt / 2, err = 0;
249 loff_t addr = ebnum * mtd->erasesize;
252 for (i = 0; i < n; i++) {
253 err = mtd_read(mtd, addr, sz, &read, buf);
254 /* Ignore corrected ECC errors */
255 if (mtd_is_bitflip(err))
257 if (err || read != sz) {
258 pr_err("error: read failed at %#llx\n",
268 err = mtd_read(mtd, addr, pgsize, &read, buf);
269 /* Ignore corrected ECC errors */
270 if (mtd_is_bitflip(err))
272 if (err || read != pgsize) {
273 pr_err("error: read failed at %#llx\n",
283 static int is_block_bad(int ebnum)
285 loff_t addr = ebnum * mtd->erasesize;
288 ret = mtd_block_isbad(mtd, addr);
290 pr_info("block %d is bad\n", ebnum);
294 static inline void start_timing(void)
296 do_gettimeofday(&start);
299 static inline void stop_timing(void)
301 do_gettimeofday(&finish);
304 static long calc_speed(void)
309 ms = (finish.tv_sec - start.tv_sec) * 1000 +
310 (finish.tv_usec - start.tv_usec) / 1000;
313 k = goodebcnt * (mtd->erasesize / 1024) * 1000;
318 static int scan_for_bad_eraseblocks(void)
322 bbt = kzalloc(ebcnt, GFP_KERNEL);
324 pr_err("error: cannot allocate memory\n");
328 if (!mtd_can_have_bb(mtd))
331 pr_info("scanning for bad eraseblocks\n");
332 for (i = 0; i < ebcnt; ++i) {
333 bbt[i] = is_block_bad(i) ? 1 : 0;
338 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
340 goodebcnt = ebcnt - bad;
344 static int __init mtd_speedtest_init(void)
346 int err, i, blocks, j, k;
350 printk(KERN_INFO "\n");
351 printk(KERN_INFO "=================================================\n");
354 pr_info("Please specify a valid mtd-device via module parameter\n");
355 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
360 pr_info("MTD device: %d count: %d\n", dev, count);
362 pr_info("MTD device: %d\n", dev);
364 mtd = get_mtd_device(NULL, dev);
367 pr_err("error: cannot get MTD device\n");
371 if (mtd->writesize == 1) {
372 pr_info("not NAND flash, assume page size is 512 "
376 pgsize = mtd->writesize;
379 do_div(tmp, mtd->erasesize);
381 pgcnt = mtd->erasesize / pgsize;
383 pr_info("MTD device size %llu, eraseblock size %u, "
384 "page size %u, count of eraseblocks %u, pages per "
385 "eraseblock %u, OOB size %u\n",
386 (unsigned long long)mtd->size, mtd->erasesize,
387 pgsize, ebcnt, pgcnt, mtd->oobsize);
389 if (count > 0 && count < ebcnt)
393 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
395 pr_err("error: cannot allocate memory\n");
399 set_random_data(iobuf, mtd->erasesize);
401 err = scan_for_bad_eraseblocks();
405 err = erase_whole_device();
409 /* Write all eraseblocks, 1 eraseblock at a time */
410 pr_info("testing eraseblock write speed\n");
412 for (i = 0; i < ebcnt; ++i) {
415 err = write_eraseblock(i);
421 speed = calc_speed();
422 pr_info("eraseblock write speed is %ld KiB/s\n", speed);
424 /* Read all eraseblocks, 1 eraseblock at a time */
425 pr_info("testing eraseblock read speed\n");
427 for (i = 0; i < ebcnt; ++i) {
430 err = read_eraseblock(i);
436 speed = calc_speed();
437 pr_info("eraseblock read speed is %ld KiB/s\n", speed);
439 err = erase_whole_device();
443 /* Write all eraseblocks, 1 page at a time */
444 pr_info("testing page write speed\n");
446 for (i = 0; i < ebcnt; ++i) {
449 err = write_eraseblock_by_page(i);
455 speed = calc_speed();
456 pr_info("page write speed is %ld KiB/s\n", speed);
458 /* Read all eraseblocks, 1 page at a time */
459 pr_info("testing page read speed\n");
461 for (i = 0; i < ebcnt; ++i) {
464 err = read_eraseblock_by_page(i);
470 speed = calc_speed();
471 pr_info("page read speed is %ld KiB/s\n", speed);
473 err = erase_whole_device();
477 /* Write all eraseblocks, 2 pages at a time */
478 pr_info("testing 2 page write speed\n");
480 for (i = 0; i < ebcnt; ++i) {
483 err = write_eraseblock_by_2pages(i);
489 speed = calc_speed();
490 pr_info("2 page write speed is %ld KiB/s\n", speed);
492 /* Read all eraseblocks, 2 pages at a time */
493 pr_info("testing 2 page read speed\n");
495 for (i = 0; i < ebcnt; ++i) {
498 err = read_eraseblock_by_2pages(i);
504 speed = calc_speed();
505 pr_info("2 page read speed is %ld KiB/s\n", speed);
507 /* Erase all eraseblocks */
508 pr_info("Testing erase speed\n");
510 for (i = 0; i < ebcnt; ++i) {
513 err = erase_eraseblock(i);
519 speed = calc_speed();
520 pr_info("erase speed is %ld KiB/s\n", speed);
522 /* Multi-block erase all eraseblocks */
523 for (k = 1; k < 7; k++) {
525 pr_info("Testing %dx multi-block erase speed\n",
528 for (i = 0; i < ebcnt; ) {
529 for (j = 0; j < blocks && (i + j) < ebcnt; j++)
536 err = multiblock_erase(i, j);
543 speed = calc_speed();
544 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
547 pr_info("finished\n");
553 pr_info("error %d occurred\n", err);
554 printk(KERN_INFO "=================================================\n");
557 module_init(mtd_speedtest_init);
559 static void __exit mtd_speedtest_exit(void)
563 module_exit(mtd_speedtest_exit);
565 MODULE_DESCRIPTION("Speed test module");
566 MODULE_AUTHOR("Adrian Hunter");
567 MODULE_LICENSE("GPL");