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>
35 static int dev = -EINVAL;
36 module_param(dev, int, S_IRUGO);
37 MODULE_PARM_DESC(dev, "MTD device number to use");
40 module_param(count, int, S_IRUGO);
41 MODULE_PARM_DESC(count, "Maximum number of eraseblocks to use "
44 static struct mtd_info *mtd;
45 static unsigned char *iobuf;
46 static unsigned char *bbt;
52 static struct timeval start, finish;
54 static int multiblock_erase(int ebnum, int blocks)
58 loff_t addr = ebnum * mtd->erasesize;
60 memset(&ei, 0, sizeof(struct erase_info));
63 ei.len = mtd->erasesize * blocks;
65 err = mtd_erase(mtd, &ei);
67 pr_err("error %d while erasing EB %d, blocks %d\n",
72 if (ei.state == MTD_ERASE_FAILED) {
73 pr_err("some erase error occurred at EB %d,"
74 "blocks %d\n", ebnum, blocks);
81 static int write_eraseblock(int ebnum)
84 loff_t addr = ebnum * mtd->erasesize;
86 err = mtdtest_write(mtd, addr, mtd->erasesize, iobuf);
88 pr_err("error: write failed at %#llx\n", addr);
93 static int write_eraseblock_by_page(int ebnum)
96 loff_t addr = ebnum * mtd->erasesize;
99 for (i = 0; i < pgcnt; i++) {
100 err = mtdtest_write(mtd, addr, pgsize, buf);
102 pr_err("error: write failed at %#llx\n",
113 static int write_eraseblock_by_2pages(int ebnum)
115 size_t sz = pgsize * 2;
116 int i, n = pgcnt / 2, err = 0;
117 loff_t addr = ebnum * mtd->erasesize;
120 for (i = 0; i < n; i++) {
121 err = mtdtest_write(mtd, addr, sz, buf);
123 pr_err("error: write failed at %#llx\n",
131 err = mtdtest_write(mtd, addr, pgsize, buf);
133 pr_err("error: write failed at %#llx\n",
141 static int read_eraseblock(int ebnum)
143 loff_t addr = ebnum * mtd->erasesize;
145 return mtdtest_read(mtd, addr, mtd->erasesize, iobuf);
148 static int read_eraseblock_by_page(int ebnum)
151 loff_t addr = ebnum * mtd->erasesize;
154 for (i = 0; i < pgcnt; i++) {
155 err = mtdtest_read(mtd, addr, pgsize, buf);
165 static int read_eraseblock_by_2pages(int ebnum)
167 size_t sz = pgsize * 2;
168 int i, n = pgcnt / 2, err = 0;
169 loff_t addr = ebnum * mtd->erasesize;
172 for (i = 0; i < n; i++) {
173 err = mtdtest_read(mtd, addr, sz, buf);
180 err = mtdtest_read(mtd, addr, pgsize, buf);
185 static inline void start_timing(void)
187 do_gettimeofday(&start);
190 static inline void stop_timing(void)
192 do_gettimeofday(&finish);
195 static long calc_speed(void)
200 ms = (finish.tv_sec - start.tv_sec) * 1000 +
201 (finish.tv_usec - start.tv_usec) / 1000;
204 k = goodebcnt * (mtd->erasesize / 1024) * 1000;
209 static int __init mtd_speedtest_init(void)
211 int err, i, blocks, j, k;
215 printk(KERN_INFO "\n");
216 printk(KERN_INFO "=================================================\n");
219 pr_info("Please specify a valid mtd-device via module parameter\n");
220 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
225 pr_info("MTD device: %d count: %d\n", dev, count);
227 pr_info("MTD device: %d\n", dev);
229 mtd = get_mtd_device(NULL, dev);
232 pr_err("error: cannot get MTD device\n");
236 if (mtd->writesize == 1) {
237 pr_info("not NAND flash, assume page size is 512 "
241 pgsize = mtd->writesize;
244 do_div(tmp, mtd->erasesize);
246 pgcnt = mtd->erasesize / pgsize;
248 pr_info("MTD device size %llu, eraseblock size %u, "
249 "page size %u, count of eraseblocks %u, pages per "
250 "eraseblock %u, OOB size %u\n",
251 (unsigned long long)mtd->size, mtd->erasesize,
252 pgsize, ebcnt, pgcnt, mtd->oobsize);
254 if (count > 0 && count < ebcnt)
258 iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
262 prandom_bytes(iobuf, mtd->erasesize);
264 bbt = kzalloc(ebcnt, GFP_KERNEL);
267 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
270 for (i = 0; i < ebcnt; i++) {
275 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
279 /* Write all eraseblocks, 1 eraseblock at a time */
280 pr_info("testing eraseblock write speed\n");
282 for (i = 0; i < ebcnt; ++i) {
285 err = write_eraseblock(i);
291 speed = calc_speed();
292 pr_info("eraseblock write speed is %ld KiB/s\n", speed);
294 /* Read all eraseblocks, 1 eraseblock at a time */
295 pr_info("testing eraseblock read speed\n");
297 for (i = 0; i < ebcnt; ++i) {
300 err = read_eraseblock(i);
306 speed = calc_speed();
307 pr_info("eraseblock read speed is %ld KiB/s\n", speed);
309 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
313 /* Write all eraseblocks, 1 page at a time */
314 pr_info("testing page write speed\n");
316 for (i = 0; i < ebcnt; ++i) {
319 err = write_eraseblock_by_page(i);
325 speed = calc_speed();
326 pr_info("page write speed is %ld KiB/s\n", speed);
328 /* Read all eraseblocks, 1 page at a time */
329 pr_info("testing page read speed\n");
331 for (i = 0; i < ebcnt; ++i) {
334 err = read_eraseblock_by_page(i);
340 speed = calc_speed();
341 pr_info("page read speed is %ld KiB/s\n", speed);
343 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
347 /* Write all eraseblocks, 2 pages at a time */
348 pr_info("testing 2 page write speed\n");
350 for (i = 0; i < ebcnt; ++i) {
353 err = write_eraseblock_by_2pages(i);
359 speed = calc_speed();
360 pr_info("2 page write speed is %ld KiB/s\n", speed);
362 /* Read all eraseblocks, 2 pages at a time */
363 pr_info("testing 2 page read speed\n");
365 for (i = 0; i < ebcnt; ++i) {
368 err = read_eraseblock_by_2pages(i);
374 speed = calc_speed();
375 pr_info("2 page read speed is %ld KiB/s\n", speed);
377 /* Erase all eraseblocks */
378 pr_info("Testing erase speed\n");
380 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
384 speed = calc_speed();
385 pr_info("erase speed is %ld KiB/s\n", speed);
387 /* Multi-block erase all eraseblocks */
388 for (k = 1; k < 7; k++) {
390 pr_info("Testing %dx multi-block erase speed\n",
393 for (i = 0; i < ebcnt; ) {
394 for (j = 0; j < blocks && (i + j) < ebcnt; j++)
401 err = multiblock_erase(i, j);
408 speed = calc_speed();
409 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
412 pr_info("finished\n");
418 pr_info("error %d occurred\n", err);
419 printk(KERN_INFO "=================================================\n");
422 module_init(mtd_speedtest_init);
424 static void __exit mtd_speedtest_exit(void)
428 module_exit(mtd_speedtest_exit);
430 MODULE_DESCRIPTION("Speed test module");
431 MODULE_AUTHOR("Adrian Hunter");
432 MODULE_LICENSE("GPL");