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 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/err.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
30 #define PRINT_PREF KERN_INFO "mtd_speedtest: "
32 static int dev = -EINVAL;
33 module_param(dev, int, S_IRUGO);
34 MODULE_PARM_DESC(dev, "MTD device number to use");
37 module_param(count, int, S_IRUGO);
38 MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
41 static struct mtd_info *mtd;
42 static unsigned char *iobuf;
43 static unsigned char *bbt;
49 static struct timeval start, finish;
50 static unsigned long next = 1;
52 static inline unsigned int simple_rand(void)
54 next = next * 1103515245 + 12345;
55 return (unsigned int)((next / 65536) % 32768);
58 static inline void simple_srand(unsigned long seed)
63 static void set_random_data(unsigned char *buf, size_t len)
67 for (i = 0; i < len; ++i)
68 buf[i] = simple_rand();
71 static int erase_eraseblock(int ebnum)
75 loff_t addr = ebnum * mtd->erasesize;
77 memset(&ei, 0, sizeof(struct erase_info));
80 ei.len = mtd->erasesize;
82 err = mtd->erase(mtd, &ei);
84 printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
88 if (ei.state == MTD_ERASE_FAILED) {
89 printk(PRINT_PREF "some erase error occurred at EB %d\n",
97 static int multiblock_erase(int ebnum, int blocks)
100 struct erase_info ei;
101 loff_t addr = ebnum * mtd->erasesize;
103 memset(&ei, 0, sizeof(struct erase_info));
106 ei.len = mtd->erasesize * blocks;
108 err = mtd->erase(mtd, &ei);
110 printk(PRINT_PREF "error %d while erasing EB %d, blocks %d\n",
115 if (ei.state == MTD_ERASE_FAILED) {
116 printk(PRINT_PREF "some erase error occurred at EB %d,"
117 "blocks %d\n", ebnum, blocks);
124 static int erase_whole_device(void)
129 for (i = 0; i < ebcnt; ++i) {
132 err = erase_eraseblock(i);
140 static int write_eraseblock(int ebnum)
144 loff_t addr = ebnum * mtd->erasesize;
146 err = mtd->write(mtd, addr, mtd->erasesize, &written, iobuf);
147 if (err || written != mtd->erasesize) {
148 printk(PRINT_PREF "error: write failed at %#llx\n", addr);
156 static int write_eraseblock_by_page(int ebnum)
160 loff_t addr = ebnum * mtd->erasesize;
163 for (i = 0; i < pgcnt; i++) {
164 err = mtd->write(mtd, addr, pgsize, &written, buf);
165 if (err || written != pgsize) {
166 printk(PRINT_PREF "error: write failed at %#llx\n",
179 static int write_eraseblock_by_2pages(int ebnum)
181 size_t written = 0, sz = pgsize * 2;
182 int i, n = pgcnt / 2, err = 0;
183 loff_t addr = ebnum * mtd->erasesize;
186 for (i = 0; i < n; i++) {
187 err = mtd->write(mtd, addr, sz, &written, buf);
188 if (err || written != sz) {
189 printk(PRINT_PREF "error: write failed at %#llx\n",
199 err = mtd->write(mtd, addr, pgsize, &written, buf);
200 if (err || written != pgsize) {
201 printk(PRINT_PREF "error: write failed at %#llx\n",
211 static int read_eraseblock(int ebnum)
215 loff_t addr = ebnum * mtd->erasesize;
217 err = mtd->read(mtd, addr, mtd->erasesize, &read, iobuf);
218 /* Ignore corrected ECC errors */
219 if (mtd_is_bitflip(err))
221 if (err || read != mtd->erasesize) {
222 printk(PRINT_PREF "error: read failed at %#llx\n", addr);
230 static int read_eraseblock_by_page(int ebnum)
234 loff_t addr = ebnum * mtd->erasesize;
237 for (i = 0; i < pgcnt; i++) {
238 err = mtd->read(mtd, addr, pgsize, &read, buf);
239 /* Ignore corrected ECC errors */
240 if (mtd_is_bitflip(err))
242 if (err || read != pgsize) {
243 printk(PRINT_PREF "error: read failed at %#llx\n",
256 static int read_eraseblock_by_2pages(int ebnum)
258 size_t read = 0, sz = pgsize * 2;
259 int i, n = pgcnt / 2, err = 0;
260 loff_t addr = ebnum * mtd->erasesize;
263 for (i = 0; i < n; i++) {
264 err = mtd->read(mtd, addr, sz, &read, buf);
265 /* Ignore corrected ECC errors */
266 if (mtd_is_bitflip(err))
268 if (err || read != sz) {
269 printk(PRINT_PREF "error: read failed at %#llx\n",
279 err = mtd->read(mtd, addr, pgsize, &read, buf);
280 /* Ignore corrected ECC errors */
281 if (mtd_is_bitflip(err))
283 if (err || read != pgsize) {
284 printk(PRINT_PREF "error: read failed at %#llx\n",
294 static int is_block_bad(int ebnum)
296 loff_t addr = ebnum * mtd->erasesize;
299 ret = mtd->block_isbad(mtd, addr);
301 printk(PRINT_PREF "block %d is bad\n", ebnum);
305 static inline void start_timing(void)
307 do_gettimeofday(&start);
310 static inline void stop_timing(void)
312 do_gettimeofday(&finish);
315 static long calc_speed(void)
320 ms = (finish.tv_sec - start.tv_sec) * 1000 +
321 (finish.tv_usec - start.tv_usec) / 1000;
324 k = goodebcnt * (mtd->erasesize / 1024) * 1000;
329 static int scan_for_bad_eraseblocks(void)
333 bbt = kzalloc(ebcnt, GFP_KERNEL);
335 printk(PRINT_PREF "error: cannot allocate memory\n");
339 /* NOR flash does not implement block_isbad */
340 if (mtd->block_isbad == NULL)
343 printk(PRINT_PREF "scanning for bad eraseblocks\n");
344 for (i = 0; i < ebcnt; ++i) {
345 bbt[i] = is_block_bad(i) ? 1 : 0;
350 printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
352 goodebcnt = ebcnt - bad;
356 static int __init mtd_speedtest_init(void)
358 int err, i, blocks, j, k;
362 printk(KERN_INFO "\n");
363 printk(KERN_INFO "=================================================\n");
366 printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
367 printk(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
372 printk(PRINT_PREF "MTD device: %d count: %d\n", dev, count);
374 printk(PRINT_PREF "MTD device: %d\n", dev);
376 mtd = get_mtd_device(NULL, dev);
379 printk(PRINT_PREF "error: cannot get MTD device\n");
383 if (mtd->writesize == 1) {
384 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
388 pgsize = mtd->writesize;
391 do_div(tmp, mtd->erasesize);
393 pgcnt = mtd->erasesize / pgsize;
395 printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
396 "page size %u, count of eraseblocks %u, pages per "
397 "eraseblock %u, OOB size %u\n",
398 (unsigned long long)mtd->size, mtd->erasesize,
399 pgsize, ebcnt, pgcnt, mtd->oobsize);
401 if (count > 0 && count < ebcnt)
405 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
407 printk(PRINT_PREF "error: cannot allocate memory\n");
412 set_random_data(iobuf, mtd->erasesize);
414 err = scan_for_bad_eraseblocks();
418 err = erase_whole_device();
422 /* Write all eraseblocks, 1 eraseblock at a time */
423 printk(PRINT_PREF "testing eraseblock write speed\n");
425 for (i = 0; i < ebcnt; ++i) {
428 err = write_eraseblock(i);
434 speed = calc_speed();
435 printk(PRINT_PREF "eraseblock write speed is %ld KiB/s\n", speed);
437 /* Read all eraseblocks, 1 eraseblock at a time */
438 printk(PRINT_PREF "testing eraseblock read speed\n");
440 for (i = 0; i < ebcnt; ++i) {
443 err = read_eraseblock(i);
449 speed = calc_speed();
450 printk(PRINT_PREF "eraseblock read speed is %ld KiB/s\n", speed);
452 err = erase_whole_device();
456 /* Write all eraseblocks, 1 page at a time */
457 printk(PRINT_PREF "testing page write speed\n");
459 for (i = 0; i < ebcnt; ++i) {
462 err = write_eraseblock_by_page(i);
468 speed = calc_speed();
469 printk(PRINT_PREF "page write speed is %ld KiB/s\n", speed);
471 /* Read all eraseblocks, 1 page at a time */
472 printk(PRINT_PREF "testing page read speed\n");
474 for (i = 0; i < ebcnt; ++i) {
477 err = read_eraseblock_by_page(i);
483 speed = calc_speed();
484 printk(PRINT_PREF "page read speed is %ld KiB/s\n", speed);
486 err = erase_whole_device();
490 /* Write all eraseblocks, 2 pages at a time */
491 printk(PRINT_PREF "testing 2 page write speed\n");
493 for (i = 0; i < ebcnt; ++i) {
496 err = write_eraseblock_by_2pages(i);
502 speed = calc_speed();
503 printk(PRINT_PREF "2 page write speed is %ld KiB/s\n", speed);
505 /* Read all eraseblocks, 2 pages at a time */
506 printk(PRINT_PREF "testing 2 page read speed\n");
508 for (i = 0; i < ebcnt; ++i) {
511 err = read_eraseblock_by_2pages(i);
517 speed = calc_speed();
518 printk(PRINT_PREF "2 page read speed is %ld KiB/s\n", speed);
520 /* Erase all eraseblocks */
521 printk(PRINT_PREF "Testing erase speed\n");
523 for (i = 0; i < ebcnt; ++i) {
526 err = erase_eraseblock(i);
532 speed = calc_speed();
533 printk(PRINT_PREF "erase speed is %ld KiB/s\n", speed);
535 /* Multi-block erase all eraseblocks */
536 for (k = 1; k < 7; k++) {
538 printk(PRINT_PREF "Testing %dx multi-block erase speed\n",
541 for (i = 0; i < ebcnt; ) {
542 for (j = 0; j < blocks && (i + j) < ebcnt; j++)
549 err = multiblock_erase(i, j);
556 speed = calc_speed();
557 printk(PRINT_PREF "%dx multi-block erase speed is %ld KiB/s\n",
560 printk(PRINT_PREF "finished\n");
566 printk(PRINT_PREF "error %d occurred\n", err);
567 printk(KERN_INFO "=================================================\n");
570 module_init(mtd_speedtest_init);
572 static void __exit mtd_speedtest_exit(void)
576 module_exit(mtd_speedtest_exit);
578 MODULE_DESCRIPTION("Speed test module");
579 MODULE_AUTHOR("Adrian Hunter");
580 MODULE_LICENSE("GPL");