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
;
53 static int erase_eraseblock(int ebnum
)
57 loff_t addr
= ebnum
* mtd
->erasesize
;
59 memset(&ei
, 0, sizeof(struct erase_info
));
62 ei
.len
= mtd
->erasesize
;
64 err
= mtd_erase(mtd
, &ei
);
66 pr_err("error %d while erasing EB %d\n", err
, ebnum
);
70 if (ei
.state
== MTD_ERASE_FAILED
) {
71 pr_err("some erase error occurred at EB %d\n",
79 static int multiblock_erase(int ebnum
, int blocks
)
83 loff_t addr
= ebnum
* mtd
->erasesize
;
85 memset(&ei
, 0, sizeof(struct erase_info
));
88 ei
.len
= mtd
->erasesize
* blocks
;
90 err
= mtd_erase(mtd
, &ei
);
92 pr_err("error %d while erasing EB %d, blocks %d\n",
97 if (ei
.state
== MTD_ERASE_FAILED
) {
98 pr_err("some erase error occurred at EB %d,"
99 "blocks %d\n", ebnum
, blocks
);
106 static int erase_whole_device(void)
111 for (i
= 0; i
< ebcnt
; ++i
) {
114 err
= erase_eraseblock(i
);
122 static int write_eraseblock(int ebnum
)
126 loff_t addr
= ebnum
* mtd
->erasesize
;
128 err
= mtd_write(mtd
, addr
, mtd
->erasesize
, &written
, iobuf
);
129 if (err
|| written
!= mtd
->erasesize
) {
130 pr_err("error: write failed at %#llx\n", addr
);
138 static int write_eraseblock_by_page(int ebnum
)
142 loff_t addr
= ebnum
* mtd
->erasesize
;
145 for (i
= 0; i
< pgcnt
; i
++) {
146 err
= mtd_write(mtd
, addr
, pgsize
, &written
, buf
);
147 if (err
|| written
!= pgsize
) {
148 pr_err("error: write failed at %#llx\n",
161 static int write_eraseblock_by_2pages(int ebnum
)
163 size_t written
, sz
= pgsize
* 2;
164 int i
, n
= pgcnt
/ 2, err
= 0;
165 loff_t addr
= ebnum
* mtd
->erasesize
;
168 for (i
= 0; i
< n
; i
++) {
169 err
= mtd_write(mtd
, addr
, sz
, &written
, buf
);
170 if (err
|| written
!= sz
) {
171 pr_err("error: write failed at %#llx\n",
181 err
= mtd_write(mtd
, addr
, pgsize
, &written
, buf
);
182 if (err
|| written
!= pgsize
) {
183 pr_err("error: write failed at %#llx\n",
193 static int read_eraseblock(int ebnum
)
197 loff_t addr
= ebnum
* mtd
->erasesize
;
199 err
= mtd_read(mtd
, addr
, mtd
->erasesize
, &read
, iobuf
);
200 /* Ignore corrected ECC errors */
201 if (mtd_is_bitflip(err
))
203 if (err
|| read
!= mtd
->erasesize
) {
204 pr_err("error: read failed at %#llx\n", addr
);
212 static int read_eraseblock_by_page(int ebnum
)
216 loff_t addr
= ebnum
* mtd
->erasesize
;
219 for (i
= 0; i
< pgcnt
; i
++) {
220 err
= mtd_read(mtd
, addr
, pgsize
, &read
, buf
);
221 /* Ignore corrected ECC errors */
222 if (mtd_is_bitflip(err
))
224 if (err
|| read
!= pgsize
) {
225 pr_err("error: read failed at %#llx\n",
238 static int read_eraseblock_by_2pages(int ebnum
)
240 size_t read
, sz
= pgsize
* 2;
241 int i
, n
= pgcnt
/ 2, err
= 0;
242 loff_t addr
= ebnum
* mtd
->erasesize
;
245 for (i
= 0; i
< n
; i
++) {
246 err
= mtd_read(mtd
, addr
, sz
, &read
, buf
);
247 /* Ignore corrected ECC errors */
248 if (mtd_is_bitflip(err
))
250 if (err
|| read
!= sz
) {
251 pr_err("error: read failed at %#llx\n",
261 err
= mtd_read(mtd
, addr
, pgsize
, &read
, buf
);
262 /* Ignore corrected ECC errors */
263 if (mtd_is_bitflip(err
))
265 if (err
|| read
!= pgsize
) {
266 pr_err("error: read failed at %#llx\n",
276 static int is_block_bad(int ebnum
)
278 loff_t addr
= ebnum
* mtd
->erasesize
;
281 ret
= mtd_block_isbad(mtd
, addr
);
283 pr_info("block %d is bad\n", ebnum
);
287 static inline void start_timing(void)
289 do_gettimeofday(&start
);
292 static inline void stop_timing(void)
294 do_gettimeofday(&finish
);
297 static long calc_speed(void)
302 ms
= (finish
.tv_sec
- start
.tv_sec
) * 1000 +
303 (finish
.tv_usec
- start
.tv_usec
) / 1000;
306 k
= goodebcnt
* (mtd
->erasesize
/ 1024) * 1000;
311 static int scan_for_bad_eraseblocks(void)
315 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
317 pr_err("error: cannot allocate memory\n");
321 if (!mtd_can_have_bb(mtd
))
324 pr_info("scanning for bad eraseblocks\n");
325 for (i
= 0; i
< ebcnt
; ++i
) {
326 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
331 pr_info("scanned %d eraseblocks, %d are bad\n", i
, bad
);
333 goodebcnt
= ebcnt
- bad
;
337 static int __init
mtd_speedtest_init(void)
339 int err
, i
, blocks
, j
, k
;
343 printk(KERN_INFO
"\n");
344 printk(KERN_INFO
"=================================================\n");
347 pr_info("Please specify a valid mtd-device via module parameter\n");
348 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
353 pr_info("MTD device: %d count: %d\n", dev
, count
);
355 pr_info("MTD device: %d\n", dev
);
357 mtd
= get_mtd_device(NULL
, dev
);
360 pr_err("error: cannot get MTD device\n");
364 if (mtd
->writesize
== 1) {
365 pr_info("not NAND flash, assume page size is 512 "
369 pgsize
= mtd
->writesize
;
372 do_div(tmp
, mtd
->erasesize
);
374 pgcnt
= mtd
->erasesize
/ pgsize
;
376 pr_info("MTD device size %llu, eraseblock size %u, "
377 "page size %u, count of eraseblocks %u, pages per "
378 "eraseblock %u, OOB size %u\n",
379 (unsigned long long)mtd
->size
, mtd
->erasesize
,
380 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
382 if (count
> 0 && count
< ebcnt
)
386 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
388 pr_err("error: cannot allocate memory\n");
392 prandom_bytes(iobuf
, mtd
->erasesize
);
394 err
= scan_for_bad_eraseblocks();
398 err
= erase_whole_device();
402 /* Write all eraseblocks, 1 eraseblock at a time */
403 pr_info("testing eraseblock write speed\n");
405 for (i
= 0; i
< ebcnt
; ++i
) {
408 err
= write_eraseblock(i
);
414 speed
= calc_speed();
415 pr_info("eraseblock write speed is %ld KiB/s\n", speed
);
417 /* Read all eraseblocks, 1 eraseblock at a time */
418 pr_info("testing eraseblock read speed\n");
420 for (i
= 0; i
< ebcnt
; ++i
) {
423 err
= read_eraseblock(i
);
429 speed
= calc_speed();
430 pr_info("eraseblock read speed is %ld KiB/s\n", speed
);
432 err
= erase_whole_device();
436 /* Write all eraseblocks, 1 page at a time */
437 pr_info("testing page write speed\n");
439 for (i
= 0; i
< ebcnt
; ++i
) {
442 err
= write_eraseblock_by_page(i
);
448 speed
= calc_speed();
449 pr_info("page write speed is %ld KiB/s\n", speed
);
451 /* Read all eraseblocks, 1 page at a time */
452 pr_info("testing page read speed\n");
454 for (i
= 0; i
< ebcnt
; ++i
) {
457 err
= read_eraseblock_by_page(i
);
463 speed
= calc_speed();
464 pr_info("page read speed is %ld KiB/s\n", speed
);
466 err
= erase_whole_device();
470 /* Write all eraseblocks, 2 pages at a time */
471 pr_info("testing 2 page write speed\n");
473 for (i
= 0; i
< ebcnt
; ++i
) {
476 err
= write_eraseblock_by_2pages(i
);
482 speed
= calc_speed();
483 pr_info("2 page write speed is %ld KiB/s\n", speed
);
485 /* Read all eraseblocks, 2 pages at a time */
486 pr_info("testing 2 page read speed\n");
488 for (i
= 0; i
< ebcnt
; ++i
) {
491 err
= read_eraseblock_by_2pages(i
);
497 speed
= calc_speed();
498 pr_info("2 page read speed is %ld KiB/s\n", speed
);
500 /* Erase all eraseblocks */
501 pr_info("Testing erase speed\n");
503 for (i
= 0; i
< ebcnt
; ++i
) {
506 err
= erase_eraseblock(i
);
512 speed
= calc_speed();
513 pr_info("erase speed is %ld KiB/s\n", speed
);
515 /* Multi-block erase all eraseblocks */
516 for (k
= 1; k
< 7; k
++) {
518 pr_info("Testing %dx multi-block erase speed\n",
521 for (i
= 0; i
< ebcnt
; ) {
522 for (j
= 0; j
< blocks
&& (i
+ j
) < ebcnt
; j
++)
529 err
= multiblock_erase(i
, j
);
536 speed
= calc_speed();
537 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
540 pr_info("finished\n");
546 pr_info("error %d occurred\n", err
);
547 printk(KERN_INFO
"=================================================\n");
550 module_init(mtd_speedtest_init
);
552 static void __exit
mtd_speedtest_exit(void)
556 module_exit(mtd_speedtest_exit
);
558 MODULE_DESCRIPTION("Speed test module");
559 MODULE_AUTHOR("Adrian Hunter");
560 MODULE_LICENSE("GPL");