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/ktime.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/err.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/slab.h>
31 #include <linux/sched.h>
32 #include <linux/random.h>
36 static int dev
= -EINVAL
;
37 module_param(dev
, int, S_IRUGO
);
38 MODULE_PARM_DESC(dev
, "MTD device number to use");
41 module_param(count
, int, S_IRUGO
);
42 MODULE_PARM_DESC(count
, "Maximum number of eraseblocks to use "
45 static struct mtd_info
*mtd
;
46 static unsigned char *iobuf
;
47 static unsigned char *bbt
;
53 static ktime_t start
, finish
;
55 static int multiblock_erase(int ebnum
, int blocks
)
59 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
61 memset(&ei
, 0, sizeof(struct erase_info
));
64 ei
.len
= mtd
->erasesize
* blocks
;
66 err
= mtd_erase(mtd
, &ei
);
68 pr_err("error %d while erasing EB %d, blocks %d\n",
73 if (ei
.state
== MTD_ERASE_FAILED
) {
74 pr_err("some erase error occurred at EB %d,"
75 "blocks %d\n", ebnum
, blocks
);
82 static int write_eraseblock(int ebnum
)
84 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
86 return mtdtest_write(mtd
, addr
, mtd
->erasesize
, iobuf
);
89 static int write_eraseblock_by_page(int ebnum
)
92 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
95 for (i
= 0; i
< pgcnt
; i
++) {
96 err
= mtdtest_write(mtd
, addr
, pgsize
, buf
);
106 static int write_eraseblock_by_2pages(int ebnum
)
108 size_t sz
= pgsize
* 2;
109 int i
, n
= pgcnt
/ 2, err
= 0;
110 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
113 for (i
= 0; i
< n
; i
++) {
114 err
= mtdtest_write(mtd
, addr
, sz
, buf
);
121 err
= mtdtest_write(mtd
, addr
, pgsize
, buf
);
126 static int read_eraseblock(int ebnum
)
128 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
130 return mtdtest_read(mtd
, addr
, mtd
->erasesize
, iobuf
);
133 static int read_eraseblock_by_page(int ebnum
)
136 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
139 for (i
= 0; i
< pgcnt
; i
++) {
140 err
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
150 static int read_eraseblock_by_2pages(int ebnum
)
152 size_t sz
= pgsize
* 2;
153 int i
, n
= pgcnt
/ 2, err
= 0;
154 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
157 for (i
= 0; i
< n
; i
++) {
158 err
= mtdtest_read(mtd
, addr
, sz
, buf
);
165 err
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
170 static inline void start_timing(void)
175 static inline void stop_timing(void)
177 finish
= ktime_get();
180 static long calc_speed(void)
185 ms
= ktime_ms_delta(finish
, start
);
188 k
= (uint64_t)goodebcnt
* (mtd
->erasesize
/ 1024) * 1000;
193 static int __init
mtd_speedtest_init(void)
195 int err
, i
, blocks
, j
, k
;
199 printk(KERN_INFO
"\n");
200 printk(KERN_INFO
"=================================================\n");
203 pr_info("Please specify a valid mtd-device via module parameter\n");
204 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
209 pr_info("MTD device: %d count: %d\n", dev
, count
);
211 pr_info("MTD device: %d\n", dev
);
213 mtd
= get_mtd_device(NULL
, dev
);
216 pr_err("error: cannot get MTD device\n");
220 if (mtd
->writesize
== 1) {
221 pr_info("not NAND flash, assume page size is 512 "
225 pgsize
= mtd
->writesize
;
228 do_div(tmp
, mtd
->erasesize
);
230 pgcnt
= mtd
->erasesize
/ pgsize
;
232 pr_info("MTD device size %llu, eraseblock size %u, "
233 "page size %u, count of eraseblocks %u, pages per "
234 "eraseblock %u, OOB size %u\n",
235 (unsigned long long)mtd
->size
, mtd
->erasesize
,
236 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
238 if (count
> 0 && count
< ebcnt
)
242 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
246 prandom_bytes(iobuf
, mtd
->erasesize
);
248 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
251 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
254 for (i
= 0; i
< ebcnt
; i
++) {
259 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
263 /* Write all eraseblocks, 1 eraseblock at a time */
264 pr_info("testing eraseblock write speed\n");
266 for (i
= 0; i
< ebcnt
; ++i
) {
269 err
= write_eraseblock(i
);
273 err
= mtdtest_relax();
278 speed
= calc_speed();
279 pr_info("eraseblock write speed is %ld KiB/s\n", speed
);
281 /* Read all eraseblocks, 1 eraseblock at a time */
282 pr_info("testing eraseblock read speed\n");
284 for (i
= 0; i
< ebcnt
; ++i
) {
287 err
= read_eraseblock(i
);
291 err
= mtdtest_relax();
296 speed
= calc_speed();
297 pr_info("eraseblock read speed is %ld KiB/s\n", speed
);
299 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
303 /* Write all eraseblocks, 1 page at a time */
304 pr_info("testing page write speed\n");
306 for (i
= 0; i
< ebcnt
; ++i
) {
309 err
= write_eraseblock_by_page(i
);
313 err
= mtdtest_relax();
318 speed
= calc_speed();
319 pr_info("page write speed is %ld KiB/s\n", speed
);
321 /* Read all eraseblocks, 1 page at a time */
322 pr_info("testing page read speed\n");
324 for (i
= 0; i
< ebcnt
; ++i
) {
327 err
= read_eraseblock_by_page(i
);
331 err
= mtdtest_relax();
336 speed
= calc_speed();
337 pr_info("page read speed is %ld KiB/s\n", speed
);
339 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
343 /* Write all eraseblocks, 2 pages at a time */
344 pr_info("testing 2 page write speed\n");
346 for (i
= 0; i
< ebcnt
; ++i
) {
349 err
= write_eraseblock_by_2pages(i
);
353 err
= mtdtest_relax();
358 speed
= calc_speed();
359 pr_info("2 page write speed is %ld KiB/s\n", speed
);
361 /* Read all eraseblocks, 2 pages at a time */
362 pr_info("testing 2 page read speed\n");
364 for (i
= 0; i
< ebcnt
; ++i
) {
367 err
= read_eraseblock_by_2pages(i
);
371 err
= mtdtest_relax();
376 speed
= calc_speed();
377 pr_info("2 page read speed is %ld KiB/s\n", speed
);
379 /* Erase all eraseblocks */
380 pr_info("Testing erase speed\n");
382 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
386 speed
= calc_speed();
387 pr_info("erase speed is %ld KiB/s\n", speed
);
389 /* Multi-block erase all eraseblocks */
390 for (k
= 1; k
< 7; k
++) {
392 pr_info("Testing %dx multi-block erase speed\n",
395 for (i
= 0; i
< ebcnt
; ) {
396 for (j
= 0; j
< blocks
&& (i
+ j
) < ebcnt
; j
++)
403 err
= multiblock_erase(i
, j
);
407 err
= mtdtest_relax();
414 speed
= calc_speed();
415 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
418 pr_info("finished\n");
424 pr_info("error %d occurred\n", err
);
425 printk(KERN_INFO
"=================================================\n");
428 module_init(mtd_speedtest_init
);
430 static void __exit
mtd_speedtest_exit(void)
434 module_exit(mtd_speedtest_exit
);
436 MODULE_DESCRIPTION("Speed test module");
437 MODULE_AUTHOR("Adrian Hunter");
438 MODULE_LICENSE("GPL");