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
);
319 if (!mtd_can_have_bb(mtd
))
322 pr_info("scanning for bad eraseblocks\n");
323 for (i
= 0; i
< ebcnt
; ++i
) {
324 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
329 pr_info("scanned %d eraseblocks, %d are bad\n", i
, bad
);
331 goodebcnt
= ebcnt
- bad
;
335 static int __init
mtd_speedtest_init(void)
337 int err
, i
, blocks
, j
, k
;
341 printk(KERN_INFO
"\n");
342 printk(KERN_INFO
"=================================================\n");
345 pr_info("Please specify a valid mtd-device via module parameter\n");
346 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
351 pr_info("MTD device: %d count: %d\n", dev
, count
);
353 pr_info("MTD device: %d\n", dev
);
355 mtd
= get_mtd_device(NULL
, dev
);
358 pr_err("error: cannot get MTD device\n");
362 if (mtd
->writesize
== 1) {
363 pr_info("not NAND flash, assume page size is 512 "
367 pgsize
= mtd
->writesize
;
370 do_div(tmp
, mtd
->erasesize
);
372 pgcnt
= mtd
->erasesize
/ pgsize
;
374 pr_info("MTD device size %llu, eraseblock size %u, "
375 "page size %u, count of eraseblocks %u, pages per "
376 "eraseblock %u, OOB size %u\n",
377 (unsigned long long)mtd
->size
, mtd
->erasesize
,
378 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
380 if (count
> 0 && count
< ebcnt
)
384 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
388 prandom_bytes(iobuf
, mtd
->erasesize
);
390 err
= scan_for_bad_eraseblocks();
394 err
= erase_whole_device();
398 /* Write all eraseblocks, 1 eraseblock at a time */
399 pr_info("testing eraseblock write speed\n");
401 for (i
= 0; i
< ebcnt
; ++i
) {
404 err
= write_eraseblock(i
);
410 speed
= calc_speed();
411 pr_info("eraseblock write speed is %ld KiB/s\n", speed
);
413 /* Read all eraseblocks, 1 eraseblock at a time */
414 pr_info("testing eraseblock read speed\n");
416 for (i
= 0; i
< ebcnt
; ++i
) {
419 err
= read_eraseblock(i
);
425 speed
= calc_speed();
426 pr_info("eraseblock read speed is %ld KiB/s\n", speed
);
428 err
= erase_whole_device();
432 /* Write all eraseblocks, 1 page at a time */
433 pr_info("testing page write speed\n");
435 for (i
= 0; i
< ebcnt
; ++i
) {
438 err
= write_eraseblock_by_page(i
);
444 speed
= calc_speed();
445 pr_info("page write speed is %ld KiB/s\n", speed
);
447 /* Read all eraseblocks, 1 page at a time */
448 pr_info("testing page read speed\n");
450 for (i
= 0; i
< ebcnt
; ++i
) {
453 err
= read_eraseblock_by_page(i
);
459 speed
= calc_speed();
460 pr_info("page read speed is %ld KiB/s\n", speed
);
462 err
= erase_whole_device();
466 /* Write all eraseblocks, 2 pages at a time */
467 pr_info("testing 2 page write speed\n");
469 for (i
= 0; i
< ebcnt
; ++i
) {
472 err
= write_eraseblock_by_2pages(i
);
478 speed
= calc_speed();
479 pr_info("2 page write speed is %ld KiB/s\n", speed
);
481 /* Read all eraseblocks, 2 pages at a time */
482 pr_info("testing 2 page read speed\n");
484 for (i
= 0; i
< ebcnt
; ++i
) {
487 err
= read_eraseblock_by_2pages(i
);
493 speed
= calc_speed();
494 pr_info("2 page read speed is %ld KiB/s\n", speed
);
496 /* Erase all eraseblocks */
497 pr_info("Testing erase speed\n");
499 for (i
= 0; i
< ebcnt
; ++i
) {
502 err
= erase_eraseblock(i
);
508 speed
= calc_speed();
509 pr_info("erase speed is %ld KiB/s\n", speed
);
511 /* Multi-block erase all eraseblocks */
512 for (k
= 1; k
< 7; k
++) {
514 pr_info("Testing %dx multi-block erase speed\n",
517 for (i
= 0; i
< ebcnt
; ) {
518 for (j
= 0; j
< blocks
&& (i
+ j
) < ebcnt
; j
++)
525 err
= multiblock_erase(i
, j
);
532 speed
= calc_speed();
533 pr_info("%dx multi-block erase speed is %ld KiB/s\n",
536 pr_info("finished\n");
542 pr_info("error %d occurred\n", err
);
543 printk(KERN_INFO
"=================================================\n");
546 module_init(mtd_speedtest_init
);
548 static void __exit
mtd_speedtest_exit(void)
552 module_exit(mtd_speedtest_exit
);
554 MODULE_DESCRIPTION("Speed test module");
555 MODULE_AUTHOR("Adrian Hunter");
556 MODULE_LICENSE("GPL");