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
));
63 ei
.len
= mtd
->erasesize
* blocks
;
65 err
= mtd_erase(mtd
, &ei
);
67 pr_err("error %d while erasing EB %d, blocks %d\n",
75 static int write_eraseblock(int ebnum
)
77 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
79 return mtdtest_write(mtd
, addr
, mtd
->erasesize
, iobuf
);
82 static int write_eraseblock_by_page(int ebnum
)
85 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
88 for (i
= 0; i
< pgcnt
; i
++) {
89 err
= mtdtest_write(mtd
, addr
, pgsize
, buf
);
99 static int write_eraseblock_by_2pages(int ebnum
)
101 size_t sz
= pgsize
* 2;
102 int i
, n
= pgcnt
/ 2, err
= 0;
103 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
106 for (i
= 0; i
< n
; i
++) {
107 err
= mtdtest_write(mtd
, addr
, sz
, buf
);
114 err
= mtdtest_write(mtd
, addr
, pgsize
, buf
);
119 static int read_eraseblock(int ebnum
)
121 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
123 return mtdtest_read(mtd
, addr
, mtd
->erasesize
, iobuf
);
126 static int read_eraseblock_by_page(int ebnum
)
129 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
132 for (i
= 0; i
< pgcnt
; i
++) {
133 err
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
143 static int read_eraseblock_by_2pages(int ebnum
)
145 size_t sz
= pgsize
* 2;
146 int i
, n
= pgcnt
/ 2, err
= 0;
147 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
150 for (i
= 0; i
< n
; i
++) {
151 err
= mtdtest_read(mtd
, addr
, sz
, buf
);
158 err
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
163 static inline void start_timing(void)
168 static inline void stop_timing(void)
170 finish
= ktime_get();
173 static long calc_speed(void)
178 ms
= ktime_ms_delta(finish
, start
);
181 k
= (uint64_t)goodebcnt
* (mtd
->erasesize
/ 1024) * 1000;
186 static int __init
mtd_speedtest_init(void)
188 int err
, i
, blocks
, j
, k
;
192 printk(KERN_INFO
"\n");
193 printk(KERN_INFO
"=================================================\n");
196 pr_info("Please specify a valid mtd-device via module parameter\n");
197 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
202 pr_info("MTD device: %d count: %d\n", dev
, count
);
204 pr_info("MTD device: %d\n", dev
);
206 mtd
= get_mtd_device(NULL
, dev
);
209 pr_err("error: cannot get MTD device\n");
213 if (mtd
->writesize
== 1) {
214 pr_info("not NAND flash, assume page size is 512 "
218 pgsize
= mtd
->writesize
;
221 do_div(tmp
, mtd
->erasesize
);
223 pgcnt
= mtd
->erasesize
/ pgsize
;
225 pr_info("MTD device size %llu, eraseblock size %u, "
226 "page size %u, count of eraseblocks %u, pages per "
227 "eraseblock %u, OOB size %u\n",
228 (unsigned long long)mtd
->size
, mtd
->erasesize
,
229 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
231 if (count
> 0 && count
< ebcnt
)
235 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
239 prandom_bytes(iobuf
, mtd
->erasesize
);
241 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
244 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
247 for (i
= 0; i
< ebcnt
; i
++) {
252 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
256 /* Write all eraseblocks, 1 eraseblock at a time */
257 pr_info("testing eraseblock write speed\n");
259 for (i
= 0; i
< ebcnt
; ++i
) {
262 err
= write_eraseblock(i
);
266 err
= mtdtest_relax();
271 speed
= calc_speed();
272 pr_info("eraseblock write speed is %ld KiB/s\n", speed
);
274 /* Read all eraseblocks, 1 eraseblock at a time */
275 pr_info("testing eraseblock read speed\n");
277 for (i
= 0; i
< ebcnt
; ++i
) {
280 err
= read_eraseblock(i
);
284 err
= mtdtest_relax();
289 speed
= calc_speed();
290 pr_info("eraseblock read speed is %ld KiB/s\n", speed
);
292 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
296 /* Write all eraseblocks, 1 page at a time */
297 pr_info("testing page write speed\n");
299 for (i
= 0; i
< ebcnt
; ++i
) {
302 err
= write_eraseblock_by_page(i
);
306 err
= mtdtest_relax();
311 speed
= calc_speed();
312 pr_info("page write speed is %ld KiB/s\n", speed
);
314 /* Read all eraseblocks, 1 page at a time */
315 pr_info("testing page read speed\n");
317 for (i
= 0; i
< ebcnt
; ++i
) {
320 err
= read_eraseblock_by_page(i
);
324 err
= mtdtest_relax();
329 speed
= calc_speed();
330 pr_info("page read speed is %ld KiB/s\n", speed
);
332 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
336 /* Write all eraseblocks, 2 pages at a time */
337 pr_info("testing 2 page write speed\n");
339 for (i
= 0; i
< ebcnt
; ++i
) {
342 err
= write_eraseblock_by_2pages(i
);
346 err
= mtdtest_relax();
351 speed
= calc_speed();
352 pr_info("2 page write speed is %ld KiB/s\n", speed
);
354 /* Read all eraseblocks, 2 pages at a time */
355 pr_info("testing 2 page read speed\n");
357 for (i
= 0; i
< ebcnt
; ++i
) {
360 err
= read_eraseblock_by_2pages(i
);
364 err
= mtdtest_relax();
369 speed
= calc_speed();
370 pr_info("2 page read speed is %ld KiB/s\n", speed
);
372 /* Erase all eraseblocks */
373 pr_info("Testing erase speed\n");
375 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
379 speed
= calc_speed();
380 pr_info("erase speed is %ld KiB/s\n", speed
);
382 /* Multi-block erase all eraseblocks */
383 for (k
= 1; k
< 7; k
++) {
385 pr_info("Testing %dx multi-block erase speed\n",
388 for (i
= 0; i
< ebcnt
; ) {
389 for (j
= 0; j
< blocks
&& (i
+ j
) < ebcnt
; j
++)
396 err
= multiblock_erase(i
, j
);
400 err
= mtdtest_relax();
407 speed
= calc_speed();
408 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
411 pr_info("finished\n");
417 pr_info("error %d occurred\n", err
);
418 printk(KERN_INFO
"=================================================\n");
421 module_init(mtd_speedtest_init
);
423 static void __exit
mtd_speedtest_exit(void)
427 module_exit(mtd_speedtest_exit
);
429 MODULE_DESCRIPTION("Speed test module");
430 MODULE_AUTHOR("Adrian Hunter");
431 MODULE_LICENSE("GPL");