1a995c792SAkinobu Mita /* 2a995c792SAkinobu Mita * Copyright (C) 2006-2008 Nokia Corporation 3a995c792SAkinobu Mita * 4a995c792SAkinobu Mita * This program is free software; you can redistribute it and/or modify it 5a995c792SAkinobu Mita * under the terms of the GNU General Public License version 2 as published by 6a995c792SAkinobu Mita * the Free Software Foundation. 7a995c792SAkinobu Mita * 8a995c792SAkinobu Mita * This program is distributed in the hope that it will be useful, but WITHOUT 9a995c792SAkinobu Mita * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10a995c792SAkinobu Mita * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11a995c792SAkinobu Mita * more details. 12a995c792SAkinobu Mita * 13a995c792SAkinobu Mita * You should have received a copy of the GNU General Public License along with 14a995c792SAkinobu Mita * this program; see the file COPYING. If not, write to the Free Software 15a995c792SAkinobu Mita * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16a995c792SAkinobu Mita * 17a995c792SAkinobu Mita * Check MTD device read. 18a995c792SAkinobu Mita * 19a995c792SAkinobu Mita * Author: Adrian Hunter <ext-adrian.hunter@nokia.com> 20a995c792SAkinobu Mita */ 21a995c792SAkinobu Mita 22a995c792SAkinobu Mita #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 23a995c792SAkinobu Mita 24a995c792SAkinobu Mita #include <linux/init.h> 25a995c792SAkinobu Mita #include <linux/module.h> 26a995c792SAkinobu Mita #include <linux/moduleparam.h> 27a995c792SAkinobu Mita #include <linux/err.h> 28a995c792SAkinobu Mita #include <linux/mtd/mtd.h> 29a995c792SAkinobu Mita #include <linux/slab.h> 30a995c792SAkinobu Mita #include <linux/sched.h> 31a995c792SAkinobu Mita 32*bf9c2236SAkinobu Mita #include "mtd_test.h" 33*bf9c2236SAkinobu Mita 34a995c792SAkinobu Mita static int dev = -EINVAL; 35a995c792SAkinobu Mita module_param(dev, int, S_IRUGO); 36a995c792SAkinobu Mita MODULE_PARM_DESC(dev, "MTD device number to use"); 37a995c792SAkinobu Mita 38a995c792SAkinobu Mita static struct mtd_info *mtd; 39a995c792SAkinobu Mita static unsigned char *iobuf; 40a995c792SAkinobu Mita static unsigned char *iobuf1; 41a995c792SAkinobu Mita static unsigned char *bbt; 42a995c792SAkinobu Mita 43a995c792SAkinobu Mita static int pgsize; 44a995c792SAkinobu Mita static int ebcnt; 45a995c792SAkinobu Mita static int pgcnt; 46a995c792SAkinobu Mita 47a995c792SAkinobu Mita static int read_eraseblock_by_page(int ebnum) 48a995c792SAkinobu Mita { 49a995c792SAkinobu Mita int i, ret, err = 0; 50a995c792SAkinobu Mita loff_t addr = ebnum * mtd->erasesize; 51a995c792SAkinobu Mita void *buf = iobuf; 52a995c792SAkinobu Mita void *oobbuf = iobuf1; 53a995c792SAkinobu Mita 54a995c792SAkinobu Mita for (i = 0; i < pgcnt; i++) { 55a995c792SAkinobu Mita memset(buf, 0 , pgsize); 56*bf9c2236SAkinobu Mita ret = mtdtest_read(mtd, addr, pgsize, buf); 57*bf9c2236SAkinobu Mita if (ret) { 58a995c792SAkinobu Mita pr_err("error: read failed at %#llx\n", 59a995c792SAkinobu Mita (long long)addr); 60a995c792SAkinobu Mita if (!err) 61a995c792SAkinobu Mita err = ret; 62a995c792SAkinobu Mita } 63a995c792SAkinobu Mita if (mtd->oobsize) { 64a995c792SAkinobu Mita struct mtd_oob_ops ops; 65a995c792SAkinobu Mita 66a995c792SAkinobu Mita ops.mode = MTD_OPS_PLACE_OOB; 67a995c792SAkinobu Mita ops.len = 0; 68a995c792SAkinobu Mita ops.retlen = 0; 69a995c792SAkinobu Mita ops.ooblen = mtd->oobsize; 70a995c792SAkinobu Mita ops.oobretlen = 0; 71a995c792SAkinobu Mita ops.ooboffs = 0; 72a995c792SAkinobu Mita ops.datbuf = NULL; 73a995c792SAkinobu Mita ops.oobbuf = oobbuf; 74a995c792SAkinobu Mita ret = mtd_read_oob(mtd, addr, &ops); 75a995c792SAkinobu Mita if ((ret && !mtd_is_bitflip(ret)) || 76a995c792SAkinobu Mita ops.oobretlen != mtd->oobsize) { 77a995c792SAkinobu Mita pr_err("error: read oob failed at " 78a995c792SAkinobu Mita "%#llx\n", (long long)addr); 79a995c792SAkinobu Mita if (!err) 80a995c792SAkinobu Mita err = ret; 81a995c792SAkinobu Mita if (!err) 82a995c792SAkinobu Mita err = -EINVAL; 83a995c792SAkinobu Mita } 84a995c792SAkinobu Mita oobbuf += mtd->oobsize; 85a995c792SAkinobu Mita } 86a995c792SAkinobu Mita addr += pgsize; 87a995c792SAkinobu Mita buf += pgsize; 88a995c792SAkinobu Mita } 89a995c792SAkinobu Mita 90a995c792SAkinobu Mita return err; 91a995c792SAkinobu Mita } 92a995c792SAkinobu Mita 93a995c792SAkinobu Mita static void dump_eraseblock(int ebnum) 94a995c792SAkinobu Mita { 95a995c792SAkinobu Mita int i, j, n; 96a995c792SAkinobu Mita char line[128]; 97a995c792SAkinobu Mita int pg, oob; 98a995c792SAkinobu Mita 99a995c792SAkinobu Mita pr_info("dumping eraseblock %d\n", ebnum); 100a995c792SAkinobu Mita n = mtd->erasesize; 101a995c792SAkinobu Mita for (i = 0; i < n;) { 102a995c792SAkinobu Mita char *p = line; 103a995c792SAkinobu Mita 104a995c792SAkinobu Mita p += sprintf(p, "%05x: ", i); 105a995c792SAkinobu Mita for (j = 0; j < 32 && i < n; j++, i++) 106a995c792SAkinobu Mita p += sprintf(p, "%02x", (unsigned int)iobuf[i]); 107a995c792SAkinobu Mita printk(KERN_CRIT "%s\n", line); 108a995c792SAkinobu Mita cond_resched(); 109a995c792SAkinobu Mita } 110a995c792SAkinobu Mita if (!mtd->oobsize) 111a995c792SAkinobu Mita return; 112a995c792SAkinobu Mita pr_info("dumping oob from eraseblock %d\n", ebnum); 113a995c792SAkinobu Mita n = mtd->oobsize; 114a995c792SAkinobu Mita for (pg = 0, i = 0; pg < pgcnt; pg++) 115a995c792SAkinobu Mita for (oob = 0; oob < n;) { 116a995c792SAkinobu Mita char *p = line; 117a995c792SAkinobu Mita 118a995c792SAkinobu Mita p += sprintf(p, "%05x: ", i); 119a995c792SAkinobu Mita for (j = 0; j < 32 && oob < n; j++, oob++, i++) 120a995c792SAkinobu Mita p += sprintf(p, "%02x", 121a995c792SAkinobu Mita (unsigned int)iobuf1[i]); 122a995c792SAkinobu Mita printk(KERN_CRIT "%s\n", line); 123a995c792SAkinobu Mita cond_resched(); 124a995c792SAkinobu Mita } 125a995c792SAkinobu Mita } 126a995c792SAkinobu Mita 127a995c792SAkinobu Mita static int __init mtd_readtest_init(void) 128a995c792SAkinobu Mita { 129a995c792SAkinobu Mita uint64_t tmp; 130a995c792SAkinobu Mita int err, i; 131a995c792SAkinobu Mita 132a995c792SAkinobu Mita printk(KERN_INFO "\n"); 133a995c792SAkinobu Mita printk(KERN_INFO "=================================================\n"); 134a995c792SAkinobu Mita 135a995c792SAkinobu Mita if (dev < 0) { 136a995c792SAkinobu Mita pr_info("Please specify a valid mtd-device via module parameter\n"); 137a995c792SAkinobu Mita return -EINVAL; 138a995c792SAkinobu Mita } 139a995c792SAkinobu Mita 140a995c792SAkinobu Mita pr_info("MTD device: %d\n", dev); 141a995c792SAkinobu Mita 142a995c792SAkinobu Mita mtd = get_mtd_device(NULL, dev); 143a995c792SAkinobu Mita if (IS_ERR(mtd)) { 144a995c792SAkinobu Mita err = PTR_ERR(mtd); 145a995c792SAkinobu Mita pr_err("error: Cannot get MTD device\n"); 146a995c792SAkinobu Mita return err; 147a995c792SAkinobu Mita } 148a995c792SAkinobu Mita 149a995c792SAkinobu Mita if (mtd->writesize == 1) { 150a995c792SAkinobu Mita pr_info("not NAND flash, assume page size is 512 " 151a995c792SAkinobu Mita "bytes.\n"); 152a995c792SAkinobu Mita pgsize = 512; 153a995c792SAkinobu Mita } else 154a995c792SAkinobu Mita pgsize = mtd->writesize; 155a995c792SAkinobu Mita 156a995c792SAkinobu Mita tmp = mtd->size; 157a995c792SAkinobu Mita do_div(tmp, mtd->erasesize); 158a995c792SAkinobu Mita ebcnt = tmp; 159a995c792SAkinobu Mita pgcnt = mtd->erasesize / pgsize; 160a995c792SAkinobu Mita 161a995c792SAkinobu Mita pr_info("MTD device size %llu, eraseblock size %u, " 162a995c792SAkinobu Mita "page size %u, count of eraseblocks %u, pages per " 163a995c792SAkinobu Mita "eraseblock %u, OOB size %u\n", 164a995c792SAkinobu Mita (unsigned long long)mtd->size, mtd->erasesize, 165a995c792SAkinobu Mita pgsize, ebcnt, pgcnt, mtd->oobsize); 166a995c792SAkinobu Mita 167a995c792SAkinobu Mita err = -ENOMEM; 168a995c792SAkinobu Mita iobuf = kmalloc(mtd->erasesize, GFP_KERNEL); 169a995c792SAkinobu Mita if (!iobuf) 170a995c792SAkinobu Mita goto out; 171a995c792SAkinobu Mita iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL); 172a995c792SAkinobu Mita if (!iobuf1) 173a995c792SAkinobu Mita goto out; 174a995c792SAkinobu Mita 175*bf9c2236SAkinobu Mita bbt = kzalloc(ebcnt, GFP_KERNEL); 176*bf9c2236SAkinobu Mita if (!bbt) 177*bf9c2236SAkinobu Mita goto out; 178*bf9c2236SAkinobu Mita err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); 179a995c792SAkinobu Mita if (err) 180a995c792SAkinobu Mita goto out; 181a995c792SAkinobu Mita 182a995c792SAkinobu Mita /* Read all eraseblocks 1 page at a time */ 183a995c792SAkinobu Mita pr_info("testing page read\n"); 184a995c792SAkinobu Mita for (i = 0; i < ebcnt; ++i) { 185a995c792SAkinobu Mita int ret; 186a995c792SAkinobu Mita 187a995c792SAkinobu Mita if (bbt[i]) 188a995c792SAkinobu Mita continue; 189a995c792SAkinobu Mita ret = read_eraseblock_by_page(i); 190a995c792SAkinobu Mita if (ret) { 191a995c792SAkinobu Mita dump_eraseblock(i); 192a995c792SAkinobu Mita if (!err) 193a995c792SAkinobu Mita err = ret; 194a995c792SAkinobu Mita } 195a995c792SAkinobu Mita cond_resched(); 196a995c792SAkinobu Mita } 197a995c792SAkinobu Mita 198a995c792SAkinobu Mita if (err) 199a995c792SAkinobu Mita pr_info("finished with errors\n"); 200a995c792SAkinobu Mita else 201a995c792SAkinobu Mita pr_info("finished\n"); 202a995c792SAkinobu Mita 203a995c792SAkinobu Mita out: 204a995c792SAkinobu Mita 205a995c792SAkinobu Mita kfree(iobuf); 206a995c792SAkinobu Mita kfree(iobuf1); 207a995c792SAkinobu Mita kfree(bbt); 208a995c792SAkinobu Mita put_mtd_device(mtd); 209a995c792SAkinobu Mita if (err) 210a995c792SAkinobu Mita pr_info("error %d occurred\n", err); 211a995c792SAkinobu Mita printk(KERN_INFO "=================================================\n"); 212a995c792SAkinobu Mita return err; 213a995c792SAkinobu Mita } 214a995c792SAkinobu Mita module_init(mtd_readtest_init); 215a995c792SAkinobu Mita 216a995c792SAkinobu Mita static void __exit mtd_readtest_exit(void) 217a995c792SAkinobu Mita { 218a995c792SAkinobu Mita return; 219a995c792SAkinobu Mita } 220a995c792SAkinobu Mita module_exit(mtd_readtest_exit); 221a995c792SAkinobu Mita 222a995c792SAkinobu Mita MODULE_DESCRIPTION("Read test module"); 223a995c792SAkinobu Mita MODULE_AUTHOR("Adrian Hunter"); 224a995c792SAkinobu Mita MODULE_LICENSE("GPL"); 225